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
@@ -1067,3 +1067,354 @@ export function _resetStateV2() {
1067
1067
  _profileIdleMsV2 = REC_DEFAULT_PROFILE_IDLE_MS;
1068
1068
  _feedStaleMsV2 = REC_DEFAULT_FEED_STALE_MS;
1069
1069
  }
1070
+
1071
+ // =====================================================================
1072
+ // content-recommendation V2 governance overlay (iter24)
1073
+ // =====================================================================
1074
+ export const RCMDGOV_PROFILE_MATURITY_V2 = Object.freeze({
1075
+ PENDING: "pending",
1076
+ ACTIVE: "active",
1077
+ STALE: "stale",
1078
+ ARCHIVED: "archived",
1079
+ });
1080
+ export const RCMDGOV_RECOMMENDATION_LIFECYCLE_V2 = Object.freeze({
1081
+ QUEUED: "queued",
1082
+ SCORING: "scoring",
1083
+ RECOMMENDED: "recommended",
1084
+ FAILED: "failed",
1085
+ CANCELLED: "cancelled",
1086
+ });
1087
+ const _rcmdgovPTrans = new Map([
1088
+ [
1089
+ RCMDGOV_PROFILE_MATURITY_V2.PENDING,
1090
+ new Set([
1091
+ RCMDGOV_PROFILE_MATURITY_V2.ACTIVE,
1092
+ RCMDGOV_PROFILE_MATURITY_V2.ARCHIVED,
1093
+ ]),
1094
+ ],
1095
+ [
1096
+ RCMDGOV_PROFILE_MATURITY_V2.ACTIVE,
1097
+ new Set([
1098
+ RCMDGOV_PROFILE_MATURITY_V2.STALE,
1099
+ RCMDGOV_PROFILE_MATURITY_V2.ARCHIVED,
1100
+ ]),
1101
+ ],
1102
+ [
1103
+ RCMDGOV_PROFILE_MATURITY_V2.STALE,
1104
+ new Set([
1105
+ RCMDGOV_PROFILE_MATURITY_V2.ACTIVE,
1106
+ RCMDGOV_PROFILE_MATURITY_V2.ARCHIVED,
1107
+ ]),
1108
+ ],
1109
+ [RCMDGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
1110
+ ]);
1111
+ const _rcmdgovPTerminal = new Set([RCMDGOV_PROFILE_MATURITY_V2.ARCHIVED]);
1112
+ const _rcmdgovJTrans = new Map([
1113
+ [
1114
+ RCMDGOV_RECOMMENDATION_LIFECYCLE_V2.QUEUED,
1115
+ new Set([
1116
+ RCMDGOV_RECOMMENDATION_LIFECYCLE_V2.SCORING,
1117
+ RCMDGOV_RECOMMENDATION_LIFECYCLE_V2.CANCELLED,
1118
+ ]),
1119
+ ],
1120
+ [
1121
+ RCMDGOV_RECOMMENDATION_LIFECYCLE_V2.SCORING,
1122
+ new Set([
1123
+ RCMDGOV_RECOMMENDATION_LIFECYCLE_V2.RECOMMENDED,
1124
+ RCMDGOV_RECOMMENDATION_LIFECYCLE_V2.FAILED,
1125
+ RCMDGOV_RECOMMENDATION_LIFECYCLE_V2.CANCELLED,
1126
+ ]),
1127
+ ],
1128
+ [RCMDGOV_RECOMMENDATION_LIFECYCLE_V2.RECOMMENDED, new Set()],
1129
+ [RCMDGOV_RECOMMENDATION_LIFECYCLE_V2.FAILED, new Set()],
1130
+ [RCMDGOV_RECOMMENDATION_LIFECYCLE_V2.CANCELLED, new Set()],
1131
+ ]);
1132
+ const _rcmdgovPsV2 = new Map();
1133
+ const _rcmdgovJsV2 = new Map();
1134
+ let _rcmdgovMaxActive = 8,
1135
+ _rcmdgovMaxPending = 20,
1136
+ _rcmdgovIdleMs = 30 * 24 * 60 * 60 * 1000,
1137
+ _rcmdgovStuckMs = 60 * 1000;
1138
+ function _rcmdgovPos(n, label) {
1139
+ const v = Math.floor(Number(n));
1140
+ if (!Number.isFinite(v) || v <= 0)
1141
+ throw new Error(`${label} must be positive integer`);
1142
+ return v;
1143
+ }
1144
+ function _rcmdgovCheckP(from, to) {
1145
+ const a = _rcmdgovPTrans.get(from);
1146
+ if (!a || !a.has(to))
1147
+ throw new Error(`invalid rcmdgov profile transition ${from} → ${to}`);
1148
+ }
1149
+ function _rcmdgovCheckJ(from, to) {
1150
+ const a = _rcmdgovJTrans.get(from);
1151
+ if (!a || !a.has(to))
1152
+ throw new Error(
1153
+ `invalid rcmdgov recommendation transition ${from} → ${to}`,
1154
+ );
1155
+ }
1156
+ function _rcmdgovCountActive(owner) {
1157
+ let c = 0;
1158
+ for (const p of _rcmdgovPsV2.values())
1159
+ if (p.owner === owner && p.status === RCMDGOV_PROFILE_MATURITY_V2.ACTIVE)
1160
+ c++;
1161
+ return c;
1162
+ }
1163
+ function _rcmdgovCountPending(profileId) {
1164
+ let c = 0;
1165
+ for (const j of _rcmdgovJsV2.values())
1166
+ if (
1167
+ j.profileId === profileId &&
1168
+ (j.status === RCMDGOV_RECOMMENDATION_LIFECYCLE_V2.QUEUED ||
1169
+ j.status === RCMDGOV_RECOMMENDATION_LIFECYCLE_V2.SCORING)
1170
+ )
1171
+ c++;
1172
+ return c;
1173
+ }
1174
+ export function setMaxActiveRcmdgovProfilesPerOwnerV2(n) {
1175
+ _rcmdgovMaxActive = _rcmdgovPos(n, "maxActiveRcmdgovProfilesPerOwner");
1176
+ }
1177
+ export function getMaxActiveRcmdgovProfilesPerOwnerV2() {
1178
+ return _rcmdgovMaxActive;
1179
+ }
1180
+ export function setMaxPendingRcmdgovRecommendationsPerProfileV2(n) {
1181
+ _rcmdgovMaxPending = _rcmdgovPos(
1182
+ n,
1183
+ "maxPendingRcmdgovRecommendationsPerProfile",
1184
+ );
1185
+ }
1186
+ export function getMaxPendingRcmdgovRecommendationsPerProfileV2() {
1187
+ return _rcmdgovMaxPending;
1188
+ }
1189
+ export function setRcmdgovProfileIdleMsV2(n) {
1190
+ _rcmdgovIdleMs = _rcmdgovPos(n, "rcmdgovProfileIdleMs");
1191
+ }
1192
+ export function getRcmdgovProfileIdleMsV2() {
1193
+ return _rcmdgovIdleMs;
1194
+ }
1195
+ export function setRcmdgovRecommendationStuckMsV2(n) {
1196
+ _rcmdgovStuckMs = _rcmdgovPos(n, "rcmdgovRecommendationStuckMs");
1197
+ }
1198
+ export function getRcmdgovRecommendationStuckMsV2() {
1199
+ return _rcmdgovStuckMs;
1200
+ }
1201
+ export function _resetStateContentRecommendationGovV2() {
1202
+ _rcmdgovPsV2.clear();
1203
+ _rcmdgovJsV2.clear();
1204
+ _rcmdgovMaxActive = 8;
1205
+ _rcmdgovMaxPending = 20;
1206
+ _rcmdgovIdleMs = 30 * 24 * 60 * 60 * 1000;
1207
+ _rcmdgovStuckMs = 60 * 1000;
1208
+ }
1209
+ export function registerRcmdgovProfileV2({
1210
+ id,
1211
+ owner,
1212
+ channel,
1213
+ metadata,
1214
+ } = {}) {
1215
+ if (!id || !owner) throw new Error("id and owner required");
1216
+ if (_rcmdgovPsV2.has(id))
1217
+ throw new Error(`rcmdgov profile ${id} already exists`);
1218
+ const now = Date.now();
1219
+ const p = {
1220
+ id,
1221
+ owner,
1222
+ channel: channel || "default",
1223
+ status: RCMDGOV_PROFILE_MATURITY_V2.PENDING,
1224
+ createdAt: now,
1225
+ updatedAt: now,
1226
+ lastTouchedAt: now,
1227
+ activatedAt: null,
1228
+ archivedAt: null,
1229
+ metadata: { ...(metadata || {}) },
1230
+ };
1231
+ _rcmdgovPsV2.set(id, p);
1232
+ return { ...p, metadata: { ...p.metadata } };
1233
+ }
1234
+ export function activateRcmdgovProfileV2(id) {
1235
+ const p = _rcmdgovPsV2.get(id);
1236
+ if (!p) throw new Error(`rcmdgov profile ${id} not found`);
1237
+ const isInitial = p.status === RCMDGOV_PROFILE_MATURITY_V2.PENDING;
1238
+ _rcmdgovCheckP(p.status, RCMDGOV_PROFILE_MATURITY_V2.ACTIVE);
1239
+ if (isInitial && _rcmdgovCountActive(p.owner) >= _rcmdgovMaxActive)
1240
+ throw new Error(`max active rcmdgov profiles for owner ${p.owner} reached`);
1241
+ const now = Date.now();
1242
+ p.status = RCMDGOV_PROFILE_MATURITY_V2.ACTIVE;
1243
+ p.updatedAt = now;
1244
+ p.lastTouchedAt = now;
1245
+ if (!p.activatedAt) p.activatedAt = now;
1246
+ return { ...p, metadata: { ...p.metadata } };
1247
+ }
1248
+ export function staleRcmdgovProfileV2(id) {
1249
+ const p = _rcmdgovPsV2.get(id);
1250
+ if (!p) throw new Error(`rcmdgov profile ${id} not found`);
1251
+ _rcmdgovCheckP(p.status, RCMDGOV_PROFILE_MATURITY_V2.STALE);
1252
+ p.status = RCMDGOV_PROFILE_MATURITY_V2.STALE;
1253
+ p.updatedAt = Date.now();
1254
+ return { ...p, metadata: { ...p.metadata } };
1255
+ }
1256
+ export function archiveRcmdgovProfileV2(id) {
1257
+ const p = _rcmdgovPsV2.get(id);
1258
+ if (!p) throw new Error(`rcmdgov profile ${id} not found`);
1259
+ _rcmdgovCheckP(p.status, RCMDGOV_PROFILE_MATURITY_V2.ARCHIVED);
1260
+ const now = Date.now();
1261
+ p.status = RCMDGOV_PROFILE_MATURITY_V2.ARCHIVED;
1262
+ p.updatedAt = now;
1263
+ if (!p.archivedAt) p.archivedAt = now;
1264
+ return { ...p, metadata: { ...p.metadata } };
1265
+ }
1266
+ export function touchRcmdgovProfileV2(id) {
1267
+ const p = _rcmdgovPsV2.get(id);
1268
+ if (!p) throw new Error(`rcmdgov profile ${id} not found`);
1269
+ if (_rcmdgovPTerminal.has(p.status))
1270
+ throw new Error(`cannot touch terminal rcmdgov profile ${id}`);
1271
+ const now = Date.now();
1272
+ p.lastTouchedAt = now;
1273
+ p.updatedAt = now;
1274
+ return { ...p, metadata: { ...p.metadata } };
1275
+ }
1276
+ export function getRcmdgovProfileV2(id) {
1277
+ const p = _rcmdgovPsV2.get(id);
1278
+ if (!p) return null;
1279
+ return { ...p, metadata: { ...p.metadata } };
1280
+ }
1281
+ export function listRcmdgovProfilesV2() {
1282
+ return [..._rcmdgovPsV2.values()].map((p) => ({
1283
+ ...p,
1284
+ metadata: { ...p.metadata },
1285
+ }));
1286
+ }
1287
+ export function createRcmdgovRecommendationV2({
1288
+ id,
1289
+ profileId,
1290
+ user,
1291
+ metadata,
1292
+ } = {}) {
1293
+ if (!id || !profileId) throw new Error("id and profileId required");
1294
+ if (_rcmdgovJsV2.has(id))
1295
+ throw new Error(`rcmdgov recommendation ${id} already exists`);
1296
+ if (!_rcmdgovPsV2.has(profileId))
1297
+ throw new Error(`rcmdgov profile ${profileId} not found`);
1298
+ if (_rcmdgovCountPending(profileId) >= _rcmdgovMaxPending)
1299
+ throw new Error(
1300
+ `max pending rcmdgov recommendations for profile ${profileId} reached`,
1301
+ );
1302
+ const now = Date.now();
1303
+ const j = {
1304
+ id,
1305
+ profileId,
1306
+ user: user || "",
1307
+ status: RCMDGOV_RECOMMENDATION_LIFECYCLE_V2.QUEUED,
1308
+ createdAt: now,
1309
+ updatedAt: now,
1310
+ startedAt: null,
1311
+ settledAt: null,
1312
+ metadata: { ...(metadata || {}) },
1313
+ };
1314
+ _rcmdgovJsV2.set(id, j);
1315
+ return { ...j, metadata: { ...j.metadata } };
1316
+ }
1317
+ export function scoringRcmdgovRecommendationV2(id) {
1318
+ const j = _rcmdgovJsV2.get(id);
1319
+ if (!j) throw new Error(`rcmdgov recommendation ${id} not found`);
1320
+ _rcmdgovCheckJ(j.status, RCMDGOV_RECOMMENDATION_LIFECYCLE_V2.SCORING);
1321
+ const now = Date.now();
1322
+ j.status = RCMDGOV_RECOMMENDATION_LIFECYCLE_V2.SCORING;
1323
+ j.updatedAt = now;
1324
+ if (!j.startedAt) j.startedAt = now;
1325
+ return { ...j, metadata: { ...j.metadata } };
1326
+ }
1327
+ export function completeRecommendationRcmdgovV2(id) {
1328
+ const j = _rcmdgovJsV2.get(id);
1329
+ if (!j) throw new Error(`rcmdgov recommendation ${id} not found`);
1330
+ _rcmdgovCheckJ(j.status, RCMDGOV_RECOMMENDATION_LIFECYCLE_V2.RECOMMENDED);
1331
+ const now = Date.now();
1332
+ j.status = RCMDGOV_RECOMMENDATION_LIFECYCLE_V2.RECOMMENDED;
1333
+ j.updatedAt = now;
1334
+ if (!j.settledAt) j.settledAt = now;
1335
+ return { ...j, metadata: { ...j.metadata } };
1336
+ }
1337
+ export function failRcmdgovRecommendationV2(id, reason) {
1338
+ const j = _rcmdgovJsV2.get(id);
1339
+ if (!j) throw new Error(`rcmdgov recommendation ${id} not found`);
1340
+ _rcmdgovCheckJ(j.status, RCMDGOV_RECOMMENDATION_LIFECYCLE_V2.FAILED);
1341
+ const now = Date.now();
1342
+ j.status = RCMDGOV_RECOMMENDATION_LIFECYCLE_V2.FAILED;
1343
+ j.updatedAt = now;
1344
+ if (!j.settledAt) j.settledAt = now;
1345
+ if (reason) j.metadata.failReason = String(reason);
1346
+ return { ...j, metadata: { ...j.metadata } };
1347
+ }
1348
+ export function cancelRcmdgovRecommendationV2(id, reason) {
1349
+ const j = _rcmdgovJsV2.get(id);
1350
+ if (!j) throw new Error(`rcmdgov recommendation ${id} not found`);
1351
+ _rcmdgovCheckJ(j.status, RCMDGOV_RECOMMENDATION_LIFECYCLE_V2.CANCELLED);
1352
+ const now = Date.now();
1353
+ j.status = RCMDGOV_RECOMMENDATION_LIFECYCLE_V2.CANCELLED;
1354
+ j.updatedAt = now;
1355
+ if (!j.settledAt) j.settledAt = now;
1356
+ if (reason) j.metadata.cancelReason = String(reason);
1357
+ return { ...j, metadata: { ...j.metadata } };
1358
+ }
1359
+ export function getRcmdgovRecommendationV2(id) {
1360
+ const j = _rcmdgovJsV2.get(id);
1361
+ if (!j) return null;
1362
+ return { ...j, metadata: { ...j.metadata } };
1363
+ }
1364
+ export function listRcmdgovRecommendationsV2() {
1365
+ return [..._rcmdgovJsV2.values()].map((j) => ({
1366
+ ...j,
1367
+ metadata: { ...j.metadata },
1368
+ }));
1369
+ }
1370
+ export function autoStaleIdleRcmdgovProfilesV2({ now } = {}) {
1371
+ const t = now ?? Date.now();
1372
+ const flipped = [];
1373
+ for (const p of _rcmdgovPsV2.values())
1374
+ if (
1375
+ p.status === RCMDGOV_PROFILE_MATURITY_V2.ACTIVE &&
1376
+ t - p.lastTouchedAt >= _rcmdgovIdleMs
1377
+ ) {
1378
+ p.status = RCMDGOV_PROFILE_MATURITY_V2.STALE;
1379
+ p.updatedAt = t;
1380
+ flipped.push(p.id);
1381
+ }
1382
+ return { flipped, count: flipped.length };
1383
+ }
1384
+ export function autoFailStuckRcmdgovRecommendationsV2({ now } = {}) {
1385
+ const t = now ?? Date.now();
1386
+ const flipped = [];
1387
+ for (const j of _rcmdgovJsV2.values())
1388
+ if (
1389
+ j.status === RCMDGOV_RECOMMENDATION_LIFECYCLE_V2.SCORING &&
1390
+ j.startedAt != null &&
1391
+ t - j.startedAt >= _rcmdgovStuckMs
1392
+ ) {
1393
+ j.status = RCMDGOV_RECOMMENDATION_LIFECYCLE_V2.FAILED;
1394
+ j.updatedAt = t;
1395
+ if (!j.settledAt) j.settledAt = t;
1396
+ j.metadata.failReason = "auto-fail-stuck";
1397
+ flipped.push(j.id);
1398
+ }
1399
+ return { flipped, count: flipped.length };
1400
+ }
1401
+ export function getContentRecommendationGovStatsV2() {
1402
+ const profilesByStatus = {};
1403
+ for (const v of Object.values(RCMDGOV_PROFILE_MATURITY_V2))
1404
+ profilesByStatus[v] = 0;
1405
+ for (const p of _rcmdgovPsV2.values()) profilesByStatus[p.status]++;
1406
+ const recommendationsByStatus = {};
1407
+ for (const v of Object.values(RCMDGOV_RECOMMENDATION_LIFECYCLE_V2))
1408
+ recommendationsByStatus[v] = 0;
1409
+ for (const j of _rcmdgovJsV2.values()) recommendationsByStatus[j.status]++;
1410
+ return {
1411
+ totalRcmdgovProfilesV2: _rcmdgovPsV2.size,
1412
+ totalRcmdgovRecommendationsV2: _rcmdgovJsV2.size,
1413
+ maxActiveRcmdgovProfilesPerOwner: _rcmdgovMaxActive,
1414
+ maxPendingRcmdgovRecommendationsPerProfile: _rcmdgovMaxPending,
1415
+ rcmdgovProfileIdleMs: _rcmdgovIdleMs,
1416
+ rcmdgovRecommendationStuckMs: _rcmdgovStuckMs,
1417
+ profilesByStatus,
1418
+ recommendationsByStatus,
1419
+ };
1420
+ }
@@ -320,3 +320,341 @@ export function importResultPacket(cwd, packet) {
320
320
  _deps.writeFileSync(file, JSON.stringify(packet, null, 2), "utf-8");
321
321
  return { file, taskId: packet.payload.taskId };
322
322
  }
323
+
324
+ // =====================================================================
325
+ // cowork-share V2 governance overlay (iter22)
326
+ // =====================================================================
327
+ export const SHGOV_PROFILE_MATURITY_V2 = Object.freeze({
328
+ PENDING: "pending",
329
+ ACTIVE: "active",
330
+ PAUSED: "paused",
331
+ ARCHIVED: "archived",
332
+ });
333
+ export const SHGOV_SHARE_LIFECYCLE_V2 = Object.freeze({
334
+ QUEUED: "queued",
335
+ SHARING: "sharing",
336
+ SHARED: "shared",
337
+ FAILED: "failed",
338
+ CANCELLED: "cancelled",
339
+ });
340
+ const _shgovPTrans = new Map([
341
+ [
342
+ SHGOV_PROFILE_MATURITY_V2.PENDING,
343
+ new Set([
344
+ SHGOV_PROFILE_MATURITY_V2.ACTIVE,
345
+ SHGOV_PROFILE_MATURITY_V2.ARCHIVED,
346
+ ]),
347
+ ],
348
+ [
349
+ SHGOV_PROFILE_MATURITY_V2.ACTIVE,
350
+ new Set([
351
+ SHGOV_PROFILE_MATURITY_V2.PAUSED,
352
+ SHGOV_PROFILE_MATURITY_V2.ARCHIVED,
353
+ ]),
354
+ ],
355
+ [
356
+ SHGOV_PROFILE_MATURITY_V2.PAUSED,
357
+ new Set([
358
+ SHGOV_PROFILE_MATURITY_V2.ACTIVE,
359
+ SHGOV_PROFILE_MATURITY_V2.ARCHIVED,
360
+ ]),
361
+ ],
362
+ [SHGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
363
+ ]);
364
+ const _shgovPTerminal = new Set([SHGOV_PROFILE_MATURITY_V2.ARCHIVED]);
365
+ const _shgovJTrans = new Map([
366
+ [
367
+ SHGOV_SHARE_LIFECYCLE_V2.QUEUED,
368
+ new Set([
369
+ SHGOV_SHARE_LIFECYCLE_V2.SHARING,
370
+ SHGOV_SHARE_LIFECYCLE_V2.CANCELLED,
371
+ ]),
372
+ ],
373
+ [
374
+ SHGOV_SHARE_LIFECYCLE_V2.SHARING,
375
+ new Set([
376
+ SHGOV_SHARE_LIFECYCLE_V2.SHARED,
377
+ SHGOV_SHARE_LIFECYCLE_V2.FAILED,
378
+ SHGOV_SHARE_LIFECYCLE_V2.CANCELLED,
379
+ ]),
380
+ ],
381
+ [SHGOV_SHARE_LIFECYCLE_V2.SHARED, new Set()],
382
+ [SHGOV_SHARE_LIFECYCLE_V2.FAILED, new Set()],
383
+ [SHGOV_SHARE_LIFECYCLE_V2.CANCELLED, new Set()],
384
+ ]);
385
+ const _shgovPsV2 = new Map();
386
+ const _shgovJsV2 = new Map();
387
+ let _shgovMaxActive = 8,
388
+ _shgovMaxPending = 20,
389
+ _shgovIdleMs = 30 * 24 * 60 * 60 * 1000,
390
+ _shgovStuckMs = 60 * 1000;
391
+ function _shgovPos(n, label) {
392
+ const v = Math.floor(Number(n));
393
+ if (!Number.isFinite(v) || v <= 0)
394
+ throw new Error(`${label} must be positive integer`);
395
+ return v;
396
+ }
397
+ function _shgovCheckP(from, to) {
398
+ const a = _shgovPTrans.get(from);
399
+ if (!a || !a.has(to))
400
+ throw new Error(`invalid shgov profile transition ${from} → ${to}`);
401
+ }
402
+ function _shgovCheckJ(from, to) {
403
+ const a = _shgovJTrans.get(from);
404
+ if (!a || !a.has(to))
405
+ throw new Error(`invalid shgov share transition ${from} → ${to}`);
406
+ }
407
+ function _shgovCountActive(owner) {
408
+ let c = 0;
409
+ for (const p of _shgovPsV2.values())
410
+ if (p.owner === owner && p.status === SHGOV_PROFILE_MATURITY_V2.ACTIVE) c++;
411
+ return c;
412
+ }
413
+ function _shgovCountPending(profileId) {
414
+ let c = 0;
415
+ for (const j of _shgovJsV2.values())
416
+ if (
417
+ j.profileId === profileId &&
418
+ (j.status === SHGOV_SHARE_LIFECYCLE_V2.QUEUED ||
419
+ j.status === SHGOV_SHARE_LIFECYCLE_V2.SHARING)
420
+ )
421
+ c++;
422
+ return c;
423
+ }
424
+ export function setMaxActiveShgovProfilesPerOwnerV2(n) {
425
+ _shgovMaxActive = _shgovPos(n, "maxActiveShgovProfilesPerOwner");
426
+ }
427
+ export function getMaxActiveShgovProfilesPerOwnerV2() {
428
+ return _shgovMaxActive;
429
+ }
430
+ export function setMaxPendingShgovSharesPerProfileV2(n) {
431
+ _shgovMaxPending = _shgovPos(n, "maxPendingShgovSharesPerProfile");
432
+ }
433
+ export function getMaxPendingShgovSharesPerProfileV2() {
434
+ return _shgovMaxPending;
435
+ }
436
+ export function setShgovProfileIdleMsV2(n) {
437
+ _shgovIdleMs = _shgovPos(n, "shgovProfileIdleMs");
438
+ }
439
+ export function getShgovProfileIdleMsV2() {
440
+ return _shgovIdleMs;
441
+ }
442
+ export function setShgovShareStuckMsV2(n) {
443
+ _shgovStuckMs = _shgovPos(n, "shgovShareStuckMs");
444
+ }
445
+ export function getShgovShareStuckMsV2() {
446
+ return _shgovStuckMs;
447
+ }
448
+ export function _resetStateCoworkShareGovV2() {
449
+ _shgovPsV2.clear();
450
+ _shgovJsV2.clear();
451
+ _shgovMaxActive = 8;
452
+ _shgovMaxPending = 20;
453
+ _shgovIdleMs = 30 * 24 * 60 * 60 * 1000;
454
+ _shgovStuckMs = 60 * 1000;
455
+ }
456
+ export function registerShgovProfileV2({
457
+ id,
458
+ owner,
459
+ visibility,
460
+ metadata,
461
+ } = {}) {
462
+ if (!id || !owner) throw new Error("id and owner required");
463
+ if (_shgovPsV2.has(id)) throw new Error(`shgov profile ${id} already exists`);
464
+ const now = Date.now();
465
+ const p = {
466
+ id,
467
+ owner,
468
+ visibility: visibility || "private",
469
+ status: SHGOV_PROFILE_MATURITY_V2.PENDING,
470
+ createdAt: now,
471
+ updatedAt: now,
472
+ lastTouchedAt: now,
473
+ activatedAt: null,
474
+ archivedAt: null,
475
+ metadata: { ...(metadata || {}) },
476
+ };
477
+ _shgovPsV2.set(id, p);
478
+ return { ...p, metadata: { ...p.metadata } };
479
+ }
480
+ export function activateShgovProfileV2(id) {
481
+ const p = _shgovPsV2.get(id);
482
+ if (!p) throw new Error(`shgov profile ${id} not found`);
483
+ const isInitial = p.status === SHGOV_PROFILE_MATURITY_V2.PENDING;
484
+ _shgovCheckP(p.status, SHGOV_PROFILE_MATURITY_V2.ACTIVE);
485
+ if (isInitial && _shgovCountActive(p.owner) >= _shgovMaxActive)
486
+ throw new Error(`max active shgov profiles for owner ${p.owner} reached`);
487
+ const now = Date.now();
488
+ p.status = SHGOV_PROFILE_MATURITY_V2.ACTIVE;
489
+ p.updatedAt = now;
490
+ p.lastTouchedAt = now;
491
+ if (!p.activatedAt) p.activatedAt = now;
492
+ return { ...p, metadata: { ...p.metadata } };
493
+ }
494
+ export function pauseShgovProfileV2(id) {
495
+ const p = _shgovPsV2.get(id);
496
+ if (!p) throw new Error(`shgov profile ${id} not found`);
497
+ _shgovCheckP(p.status, SHGOV_PROFILE_MATURITY_V2.PAUSED);
498
+ p.status = SHGOV_PROFILE_MATURITY_V2.PAUSED;
499
+ p.updatedAt = Date.now();
500
+ return { ...p, metadata: { ...p.metadata } };
501
+ }
502
+ export function archiveShgovProfileV2(id) {
503
+ const p = _shgovPsV2.get(id);
504
+ if (!p) throw new Error(`shgov profile ${id} not found`);
505
+ _shgovCheckP(p.status, SHGOV_PROFILE_MATURITY_V2.ARCHIVED);
506
+ const now = Date.now();
507
+ p.status = SHGOV_PROFILE_MATURITY_V2.ARCHIVED;
508
+ p.updatedAt = now;
509
+ if (!p.archivedAt) p.archivedAt = now;
510
+ return { ...p, metadata: { ...p.metadata } };
511
+ }
512
+ export function touchShgovProfileV2(id) {
513
+ const p = _shgovPsV2.get(id);
514
+ if (!p) throw new Error(`shgov profile ${id} not found`);
515
+ if (_shgovPTerminal.has(p.status))
516
+ throw new Error(`cannot touch terminal shgov profile ${id}`);
517
+ const now = Date.now();
518
+ p.lastTouchedAt = now;
519
+ p.updatedAt = now;
520
+ return { ...p, metadata: { ...p.metadata } };
521
+ }
522
+ export function getShgovProfileV2(id) {
523
+ const p = _shgovPsV2.get(id);
524
+ if (!p) return null;
525
+ return { ...p, metadata: { ...p.metadata } };
526
+ }
527
+ export function listShgovProfilesV2() {
528
+ return [..._shgovPsV2.values()].map((p) => ({
529
+ ...p,
530
+ metadata: { ...p.metadata },
531
+ }));
532
+ }
533
+ export function createShgovShareV2({ id, profileId, target, metadata } = {}) {
534
+ if (!id || !profileId) throw new Error("id and profileId required");
535
+ if (_shgovJsV2.has(id)) throw new Error(`shgov share ${id} already exists`);
536
+ if (!_shgovPsV2.has(profileId))
537
+ throw new Error(`shgov profile ${profileId} not found`);
538
+ if (_shgovCountPending(profileId) >= _shgovMaxPending)
539
+ throw new Error(
540
+ `max pending shgov shares for profile ${profileId} reached`,
541
+ );
542
+ const now = Date.now();
543
+ const j = {
544
+ id,
545
+ profileId,
546
+ target: target || "",
547
+ status: SHGOV_SHARE_LIFECYCLE_V2.QUEUED,
548
+ createdAt: now,
549
+ updatedAt: now,
550
+ startedAt: null,
551
+ settledAt: null,
552
+ metadata: { ...(metadata || {}) },
553
+ };
554
+ _shgovJsV2.set(id, j);
555
+ return { ...j, metadata: { ...j.metadata } };
556
+ }
557
+ export function sharingShgovShareV2(id) {
558
+ const j = _shgovJsV2.get(id);
559
+ if (!j) throw new Error(`shgov share ${id} not found`);
560
+ _shgovCheckJ(j.status, SHGOV_SHARE_LIFECYCLE_V2.SHARING);
561
+ const now = Date.now();
562
+ j.status = SHGOV_SHARE_LIFECYCLE_V2.SHARING;
563
+ j.updatedAt = now;
564
+ if (!j.startedAt) j.startedAt = now;
565
+ return { ...j, metadata: { ...j.metadata } };
566
+ }
567
+ export function completeShareShgovV2(id) {
568
+ const j = _shgovJsV2.get(id);
569
+ if (!j) throw new Error(`shgov share ${id} not found`);
570
+ _shgovCheckJ(j.status, SHGOV_SHARE_LIFECYCLE_V2.SHARED);
571
+ const now = Date.now();
572
+ j.status = SHGOV_SHARE_LIFECYCLE_V2.SHARED;
573
+ j.updatedAt = now;
574
+ if (!j.settledAt) j.settledAt = now;
575
+ return { ...j, metadata: { ...j.metadata } };
576
+ }
577
+ export function failShgovShareV2(id, reason) {
578
+ const j = _shgovJsV2.get(id);
579
+ if (!j) throw new Error(`shgov share ${id} not found`);
580
+ _shgovCheckJ(j.status, SHGOV_SHARE_LIFECYCLE_V2.FAILED);
581
+ const now = Date.now();
582
+ j.status = SHGOV_SHARE_LIFECYCLE_V2.FAILED;
583
+ j.updatedAt = now;
584
+ if (!j.settledAt) j.settledAt = now;
585
+ if (reason) j.metadata.failReason = String(reason);
586
+ return { ...j, metadata: { ...j.metadata } };
587
+ }
588
+ export function cancelShgovShareV2(id, reason) {
589
+ const j = _shgovJsV2.get(id);
590
+ if (!j) throw new Error(`shgov share ${id} not found`);
591
+ _shgovCheckJ(j.status, SHGOV_SHARE_LIFECYCLE_V2.CANCELLED);
592
+ const now = Date.now();
593
+ j.status = SHGOV_SHARE_LIFECYCLE_V2.CANCELLED;
594
+ j.updatedAt = now;
595
+ if (!j.settledAt) j.settledAt = now;
596
+ if (reason) j.metadata.cancelReason = String(reason);
597
+ return { ...j, metadata: { ...j.metadata } };
598
+ }
599
+ export function getShgovShareV2(id) {
600
+ const j = _shgovJsV2.get(id);
601
+ if (!j) return null;
602
+ return { ...j, metadata: { ...j.metadata } };
603
+ }
604
+ export function listShgovSharesV2() {
605
+ return [..._shgovJsV2.values()].map((j) => ({
606
+ ...j,
607
+ metadata: { ...j.metadata },
608
+ }));
609
+ }
610
+ export function autoPauseIdleShgovProfilesV2({ now } = {}) {
611
+ const t = now ?? Date.now();
612
+ const flipped = [];
613
+ for (const p of _shgovPsV2.values())
614
+ if (
615
+ p.status === SHGOV_PROFILE_MATURITY_V2.ACTIVE &&
616
+ t - p.lastTouchedAt >= _shgovIdleMs
617
+ ) {
618
+ p.status = SHGOV_PROFILE_MATURITY_V2.PAUSED;
619
+ p.updatedAt = t;
620
+ flipped.push(p.id);
621
+ }
622
+ return { flipped, count: flipped.length };
623
+ }
624
+ export function autoFailStuckShgovSharesV2({ now } = {}) {
625
+ const t = now ?? Date.now();
626
+ const flipped = [];
627
+ for (const j of _shgovJsV2.values())
628
+ if (
629
+ j.status === SHGOV_SHARE_LIFECYCLE_V2.SHARING &&
630
+ j.startedAt != null &&
631
+ t - j.startedAt >= _shgovStuckMs
632
+ ) {
633
+ j.status = SHGOV_SHARE_LIFECYCLE_V2.FAILED;
634
+ j.updatedAt = t;
635
+ if (!j.settledAt) j.settledAt = t;
636
+ j.metadata.failReason = "auto-fail-stuck";
637
+ flipped.push(j.id);
638
+ }
639
+ return { flipped, count: flipped.length };
640
+ }
641
+ export function getCoworkShareGovStatsV2() {
642
+ const profilesByStatus = {};
643
+ for (const v of Object.values(SHGOV_PROFILE_MATURITY_V2))
644
+ profilesByStatus[v] = 0;
645
+ for (const p of _shgovPsV2.values()) profilesByStatus[p.status]++;
646
+ const sharesByStatus = {};
647
+ for (const v of Object.values(SHGOV_SHARE_LIFECYCLE_V2))
648
+ sharesByStatus[v] = 0;
649
+ for (const j of _shgovJsV2.values()) sharesByStatus[j.status]++;
650
+ return {
651
+ totalShgovProfilesV2: _shgovPsV2.size,
652
+ totalShgovSharesV2: _shgovJsV2.size,
653
+ maxActiveShgovProfilesPerOwner: _shgovMaxActive,
654
+ maxPendingShgovSharesPerProfile: _shgovMaxPending,
655
+ shgovProfileIdleMs: _shgovIdleMs,
656
+ shgovShareStuckMs: _shgovStuckMs,
657
+ profilesByStatus,
658
+ sharesByStatus,
659
+ };
660
+ }