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
@@ -1293,3 +1293,334 @@ export function getKnowledgeGraphGovStatsV2() {
1293
1293
  importsByStatus,
1294
1294
  };
1295
1295
  }
1296
+
1297
+ // === Iter28 V2 governance overlay: Kggov ===
1298
+ export const KGGOV_PROFILE_MATURITY_V2 = Object.freeze({
1299
+ PENDING: "pending",
1300
+ ACTIVE: "active",
1301
+ STALE: "stale",
1302
+ ARCHIVED: "archived",
1303
+ });
1304
+ export const KGGOV_QUERY_LIFECYCLE_V2 = Object.freeze({
1305
+ QUEUED: "queued",
1306
+ QUERYING: "querying",
1307
+ ANSWERED: "answered",
1308
+ FAILED: "failed",
1309
+ CANCELLED: "cancelled",
1310
+ });
1311
+ const _kggovPTrans = new Map([
1312
+ [
1313
+ KGGOV_PROFILE_MATURITY_V2.PENDING,
1314
+ new Set([
1315
+ KGGOV_PROFILE_MATURITY_V2.ACTIVE,
1316
+ KGGOV_PROFILE_MATURITY_V2.ARCHIVED,
1317
+ ]),
1318
+ ],
1319
+ [
1320
+ KGGOV_PROFILE_MATURITY_V2.ACTIVE,
1321
+ new Set([
1322
+ KGGOV_PROFILE_MATURITY_V2.STALE,
1323
+ KGGOV_PROFILE_MATURITY_V2.ARCHIVED,
1324
+ ]),
1325
+ ],
1326
+ [
1327
+ KGGOV_PROFILE_MATURITY_V2.STALE,
1328
+ new Set([
1329
+ KGGOV_PROFILE_MATURITY_V2.ACTIVE,
1330
+ KGGOV_PROFILE_MATURITY_V2.ARCHIVED,
1331
+ ]),
1332
+ ],
1333
+ [KGGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
1334
+ ]);
1335
+ const _kggovPTerminal = new Set([KGGOV_PROFILE_MATURITY_V2.ARCHIVED]);
1336
+ const _kggovJTrans = new Map([
1337
+ [
1338
+ KGGOV_QUERY_LIFECYCLE_V2.QUEUED,
1339
+ new Set([
1340
+ KGGOV_QUERY_LIFECYCLE_V2.QUERYING,
1341
+ KGGOV_QUERY_LIFECYCLE_V2.CANCELLED,
1342
+ ]),
1343
+ ],
1344
+ [
1345
+ KGGOV_QUERY_LIFECYCLE_V2.QUERYING,
1346
+ new Set([
1347
+ KGGOV_QUERY_LIFECYCLE_V2.ANSWERED,
1348
+ KGGOV_QUERY_LIFECYCLE_V2.FAILED,
1349
+ KGGOV_QUERY_LIFECYCLE_V2.CANCELLED,
1350
+ ]),
1351
+ ],
1352
+ [KGGOV_QUERY_LIFECYCLE_V2.ANSWERED, new Set()],
1353
+ [KGGOV_QUERY_LIFECYCLE_V2.FAILED, new Set()],
1354
+ [KGGOV_QUERY_LIFECYCLE_V2.CANCELLED, new Set()],
1355
+ ]);
1356
+ const _kggovPsV2 = new Map();
1357
+ const _kggovJsV2 = new Map();
1358
+ let _kggovMaxActive = 8,
1359
+ _kggovMaxPending = 20,
1360
+ _kggovIdleMs = 2592000000,
1361
+ _kggovStuckMs = 60 * 1000;
1362
+ function _kggovPos(n, label) {
1363
+ const v = Math.floor(Number(n));
1364
+ if (!Number.isFinite(v) || v <= 0)
1365
+ throw new Error(`${label} must be positive integer`);
1366
+ return v;
1367
+ }
1368
+ function _kggovCheckP(from, to) {
1369
+ const a = _kggovPTrans.get(from);
1370
+ if (!a || !a.has(to))
1371
+ throw new Error(`invalid kggov profile transition ${from} → ${to}`);
1372
+ }
1373
+ function _kggovCheckJ(from, to) {
1374
+ const a = _kggovJTrans.get(from);
1375
+ if (!a || !a.has(to))
1376
+ throw new Error(`invalid kggov query transition ${from} → ${to}`);
1377
+ }
1378
+ function _kggovCountActive(owner) {
1379
+ let c = 0;
1380
+ for (const p of _kggovPsV2.values())
1381
+ if (p.owner === owner && p.status === KGGOV_PROFILE_MATURITY_V2.ACTIVE) c++;
1382
+ return c;
1383
+ }
1384
+ function _kggovCountPending(profileId) {
1385
+ let c = 0;
1386
+ for (const j of _kggovJsV2.values())
1387
+ if (
1388
+ j.profileId === profileId &&
1389
+ (j.status === KGGOV_QUERY_LIFECYCLE_V2.QUEUED ||
1390
+ j.status === KGGOV_QUERY_LIFECYCLE_V2.QUERYING)
1391
+ )
1392
+ c++;
1393
+ return c;
1394
+ }
1395
+ export function setMaxActiveKgProfilesPerOwnerV2(n) {
1396
+ _kggovMaxActive = _kggovPos(n, "maxActiveKgProfilesPerOwner");
1397
+ }
1398
+ export function getMaxActiveKgProfilesPerOwnerV2() {
1399
+ return _kggovMaxActive;
1400
+ }
1401
+ export function setMaxPendingKgQuerysPerProfileV2(n) {
1402
+ _kggovMaxPending = _kggovPos(n, "maxPendingKgQuerysPerProfile");
1403
+ }
1404
+ export function getMaxPendingKgQuerysPerProfileV2() {
1405
+ return _kggovMaxPending;
1406
+ }
1407
+ export function setKgProfileIdleMsV2(n) {
1408
+ _kggovIdleMs = _kggovPos(n, "kggovProfileIdleMs");
1409
+ }
1410
+ export function getKgProfileIdleMsV2() {
1411
+ return _kggovIdleMs;
1412
+ }
1413
+ export function setKgQueryStuckMsV2(n) {
1414
+ _kggovStuckMs = _kggovPos(n, "kggovQueryStuckMs");
1415
+ }
1416
+ export function getKgQueryStuckMsV2() {
1417
+ return _kggovStuckMs;
1418
+ }
1419
+ export function _resetStateKggovV2() {
1420
+ _kggovPsV2.clear();
1421
+ _kggovJsV2.clear();
1422
+ _kggovMaxActive = 8;
1423
+ _kggovMaxPending = 20;
1424
+ _kggovIdleMs = 2592000000;
1425
+ _kggovStuckMs = 60 * 1000;
1426
+ }
1427
+ export function registerKgProfileV2({ id, owner, kind, metadata } = {}) {
1428
+ if (!id || !owner) throw new Error("id and owner required");
1429
+ if (_kggovPsV2.has(id)) throw new Error(`kggov profile ${id} already exists`);
1430
+ const now = Date.now();
1431
+ const p = {
1432
+ id,
1433
+ owner,
1434
+ kind: kind || "default",
1435
+ status: KGGOV_PROFILE_MATURITY_V2.PENDING,
1436
+ createdAt: now,
1437
+ updatedAt: now,
1438
+ lastTouchedAt: now,
1439
+ activatedAt: null,
1440
+ archivedAt: null,
1441
+ metadata: { ...(metadata || {}) },
1442
+ };
1443
+ _kggovPsV2.set(id, p);
1444
+ return { ...p, metadata: { ...p.metadata } };
1445
+ }
1446
+ export function activateKgProfileV2(id) {
1447
+ const p = _kggovPsV2.get(id);
1448
+ if (!p) throw new Error(`kggov profile ${id} not found`);
1449
+ const isInitial = p.status === KGGOV_PROFILE_MATURITY_V2.PENDING;
1450
+ _kggovCheckP(p.status, KGGOV_PROFILE_MATURITY_V2.ACTIVE);
1451
+ if (isInitial && _kggovCountActive(p.owner) >= _kggovMaxActive)
1452
+ throw new Error(`max active kggov profiles for owner ${p.owner} reached`);
1453
+ const now = Date.now();
1454
+ p.status = KGGOV_PROFILE_MATURITY_V2.ACTIVE;
1455
+ p.updatedAt = now;
1456
+ p.lastTouchedAt = now;
1457
+ if (!p.activatedAt) p.activatedAt = now;
1458
+ return { ...p, metadata: { ...p.metadata } };
1459
+ }
1460
+ export function staleKgProfileV2(id) {
1461
+ const p = _kggovPsV2.get(id);
1462
+ if (!p) throw new Error(`kggov profile ${id} not found`);
1463
+ _kggovCheckP(p.status, KGGOV_PROFILE_MATURITY_V2.STALE);
1464
+ p.status = KGGOV_PROFILE_MATURITY_V2.STALE;
1465
+ p.updatedAt = Date.now();
1466
+ return { ...p, metadata: { ...p.metadata } };
1467
+ }
1468
+ export function archiveKgProfileV2(id) {
1469
+ const p = _kggovPsV2.get(id);
1470
+ if (!p) throw new Error(`kggov profile ${id} not found`);
1471
+ _kggovCheckP(p.status, KGGOV_PROFILE_MATURITY_V2.ARCHIVED);
1472
+ const now = Date.now();
1473
+ p.status = KGGOV_PROFILE_MATURITY_V2.ARCHIVED;
1474
+ p.updatedAt = now;
1475
+ if (!p.archivedAt) p.archivedAt = now;
1476
+ return { ...p, metadata: { ...p.metadata } };
1477
+ }
1478
+ export function touchKgProfileV2(id) {
1479
+ const p = _kggovPsV2.get(id);
1480
+ if (!p) throw new Error(`kggov profile ${id} not found`);
1481
+ if (_kggovPTerminal.has(p.status))
1482
+ throw new Error(`cannot touch terminal kggov profile ${id}`);
1483
+ const now = Date.now();
1484
+ p.lastTouchedAt = now;
1485
+ p.updatedAt = now;
1486
+ return { ...p, metadata: { ...p.metadata } };
1487
+ }
1488
+ export function getKgProfileV2(id) {
1489
+ const p = _kggovPsV2.get(id);
1490
+ if (!p) return null;
1491
+ return { ...p, metadata: { ...p.metadata } };
1492
+ }
1493
+ export function listKgProfilesV2() {
1494
+ return [..._kggovPsV2.values()].map((p) => ({
1495
+ ...p,
1496
+ metadata: { ...p.metadata },
1497
+ }));
1498
+ }
1499
+ export function createKgQueryV2({ id, profileId, queryId, metadata } = {}) {
1500
+ if (!id || !profileId) throw new Error("id and profileId required");
1501
+ if (_kggovJsV2.has(id)) throw new Error(`kggov query ${id} already exists`);
1502
+ if (!_kggovPsV2.has(profileId))
1503
+ throw new Error(`kggov profile ${profileId} not found`);
1504
+ if (_kggovCountPending(profileId) >= _kggovMaxPending)
1505
+ throw new Error(
1506
+ `max pending kggov querys for profile ${profileId} reached`,
1507
+ );
1508
+ const now = Date.now();
1509
+ const j = {
1510
+ id,
1511
+ profileId,
1512
+ queryId: queryId || "",
1513
+ status: KGGOV_QUERY_LIFECYCLE_V2.QUEUED,
1514
+ createdAt: now,
1515
+ updatedAt: now,
1516
+ startedAt: null,
1517
+ settledAt: null,
1518
+ metadata: { ...(metadata || {}) },
1519
+ };
1520
+ _kggovJsV2.set(id, j);
1521
+ return { ...j, metadata: { ...j.metadata } };
1522
+ }
1523
+ export function queryingKgQueryV2(id) {
1524
+ const j = _kggovJsV2.get(id);
1525
+ if (!j) throw new Error(`kggov query ${id} not found`);
1526
+ _kggovCheckJ(j.status, KGGOV_QUERY_LIFECYCLE_V2.QUERYING);
1527
+ const now = Date.now();
1528
+ j.status = KGGOV_QUERY_LIFECYCLE_V2.QUERYING;
1529
+ j.updatedAt = now;
1530
+ if (!j.startedAt) j.startedAt = now;
1531
+ return { ...j, metadata: { ...j.metadata } };
1532
+ }
1533
+ export function completeQueryKgV2(id) {
1534
+ const j = _kggovJsV2.get(id);
1535
+ if (!j) throw new Error(`kggov query ${id} not found`);
1536
+ _kggovCheckJ(j.status, KGGOV_QUERY_LIFECYCLE_V2.ANSWERED);
1537
+ const now = Date.now();
1538
+ j.status = KGGOV_QUERY_LIFECYCLE_V2.ANSWERED;
1539
+ j.updatedAt = now;
1540
+ if (!j.settledAt) j.settledAt = now;
1541
+ return { ...j, metadata: { ...j.metadata } };
1542
+ }
1543
+ export function failKgQueryV2(id, reason) {
1544
+ const j = _kggovJsV2.get(id);
1545
+ if (!j) throw new Error(`kggov query ${id} not found`);
1546
+ _kggovCheckJ(j.status, KGGOV_QUERY_LIFECYCLE_V2.FAILED);
1547
+ const now = Date.now();
1548
+ j.status = KGGOV_QUERY_LIFECYCLE_V2.FAILED;
1549
+ j.updatedAt = now;
1550
+ if (!j.settledAt) j.settledAt = now;
1551
+ if (reason) j.metadata.failReason = String(reason);
1552
+ return { ...j, metadata: { ...j.metadata } };
1553
+ }
1554
+ export function cancelKgQueryV2(id, reason) {
1555
+ const j = _kggovJsV2.get(id);
1556
+ if (!j) throw new Error(`kggov query ${id} not found`);
1557
+ _kggovCheckJ(j.status, KGGOV_QUERY_LIFECYCLE_V2.CANCELLED);
1558
+ const now = Date.now();
1559
+ j.status = KGGOV_QUERY_LIFECYCLE_V2.CANCELLED;
1560
+ j.updatedAt = now;
1561
+ if (!j.settledAt) j.settledAt = now;
1562
+ if (reason) j.metadata.cancelReason = String(reason);
1563
+ return { ...j, metadata: { ...j.metadata } };
1564
+ }
1565
+ export function getKgQueryV2(id) {
1566
+ const j = _kggovJsV2.get(id);
1567
+ if (!j) return null;
1568
+ return { ...j, metadata: { ...j.metadata } };
1569
+ }
1570
+ export function listKgQuerysV2() {
1571
+ return [..._kggovJsV2.values()].map((j) => ({
1572
+ ...j,
1573
+ metadata: { ...j.metadata },
1574
+ }));
1575
+ }
1576
+ export function autoStaleIdleKgProfilesV2({ now } = {}) {
1577
+ const t = now ?? Date.now();
1578
+ const flipped = [];
1579
+ for (const p of _kggovPsV2.values())
1580
+ if (
1581
+ p.status === KGGOV_PROFILE_MATURITY_V2.ACTIVE &&
1582
+ t - p.lastTouchedAt >= _kggovIdleMs
1583
+ ) {
1584
+ p.status = KGGOV_PROFILE_MATURITY_V2.STALE;
1585
+ p.updatedAt = t;
1586
+ flipped.push(p.id);
1587
+ }
1588
+ return { flipped, count: flipped.length };
1589
+ }
1590
+ export function autoFailStuckKgQuerysV2({ now } = {}) {
1591
+ const t = now ?? Date.now();
1592
+ const flipped = [];
1593
+ for (const j of _kggovJsV2.values())
1594
+ if (
1595
+ j.status === KGGOV_QUERY_LIFECYCLE_V2.QUERYING &&
1596
+ j.startedAt != null &&
1597
+ t - j.startedAt >= _kggovStuckMs
1598
+ ) {
1599
+ j.status = KGGOV_QUERY_LIFECYCLE_V2.FAILED;
1600
+ j.updatedAt = t;
1601
+ if (!j.settledAt) j.settledAt = t;
1602
+ j.metadata.failReason = "auto-fail-stuck";
1603
+ flipped.push(j.id);
1604
+ }
1605
+ return { flipped, count: flipped.length };
1606
+ }
1607
+ export function getKggovStatsV2() {
1608
+ const profilesByStatus = {};
1609
+ for (const v of Object.values(KGGOV_PROFILE_MATURITY_V2))
1610
+ profilesByStatus[v] = 0;
1611
+ for (const p of _kggovPsV2.values()) profilesByStatus[p.status]++;
1612
+ const querysByStatus = {};
1613
+ for (const v of Object.values(KGGOV_QUERY_LIFECYCLE_V2))
1614
+ querysByStatus[v] = 0;
1615
+ for (const j of _kggovJsV2.values()) querysByStatus[j.status]++;
1616
+ return {
1617
+ totalKgProfilesV2: _kggovPsV2.size,
1618
+ totalKgQuerysV2: _kggovJsV2.size,
1619
+ maxActiveKgProfilesPerOwner: _kggovMaxActive,
1620
+ maxPendingKgQuerysPerProfile: _kggovMaxPending,
1621
+ kggovProfileIdleMs: _kggovIdleMs,
1622
+ kggovQueryStuckMs: _kggovStuckMs,
1623
+ profilesByStatus,
1624
+ querysByStatus,
1625
+ };
1626
+ }
@@ -670,3 +670,344 @@ export function _resetStateKnowledgeImporterV2() {
670
670
  _stateV2.sourceIdleMs = IMPORTER_DEFAULT_SOURCE_IDLE_MS;
671
671
  _stateV2.jobStuckMs = IMPORTER_DEFAULT_JOB_STUCK_MS;
672
672
  }
673
+
674
+ // =====================================================================
675
+ // knowledge-importer V2 governance overlay (iter22)
676
+ // =====================================================================
677
+ export const KIMPGOV_PROFILE_MATURITY_V2 = Object.freeze({
678
+ PENDING: "pending",
679
+ ACTIVE: "active",
680
+ STALE: "stale",
681
+ ARCHIVED: "archived",
682
+ });
683
+ export const KIMPGOV_IMPORT_LIFECYCLE_V2 = Object.freeze({
684
+ QUEUED: "queued",
685
+ IMPORTING: "importing",
686
+ IMPORTED: "imported",
687
+ FAILED: "failed",
688
+ CANCELLED: "cancelled",
689
+ });
690
+ const _kimpgovPTrans = new Map([
691
+ [
692
+ KIMPGOV_PROFILE_MATURITY_V2.PENDING,
693
+ new Set([
694
+ KIMPGOV_PROFILE_MATURITY_V2.ACTIVE,
695
+ KIMPGOV_PROFILE_MATURITY_V2.ARCHIVED,
696
+ ]),
697
+ ],
698
+ [
699
+ KIMPGOV_PROFILE_MATURITY_V2.ACTIVE,
700
+ new Set([
701
+ KIMPGOV_PROFILE_MATURITY_V2.STALE,
702
+ KIMPGOV_PROFILE_MATURITY_V2.ARCHIVED,
703
+ ]),
704
+ ],
705
+ [
706
+ KIMPGOV_PROFILE_MATURITY_V2.STALE,
707
+ new Set([
708
+ KIMPGOV_PROFILE_MATURITY_V2.ACTIVE,
709
+ KIMPGOV_PROFILE_MATURITY_V2.ARCHIVED,
710
+ ]),
711
+ ],
712
+ [KIMPGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
713
+ ]);
714
+ const _kimpgovPTerminal = new Set([KIMPGOV_PROFILE_MATURITY_V2.ARCHIVED]);
715
+ const _kimpgovJTrans = new Map([
716
+ [
717
+ KIMPGOV_IMPORT_LIFECYCLE_V2.QUEUED,
718
+ new Set([
719
+ KIMPGOV_IMPORT_LIFECYCLE_V2.IMPORTING,
720
+ KIMPGOV_IMPORT_LIFECYCLE_V2.CANCELLED,
721
+ ]),
722
+ ],
723
+ [
724
+ KIMPGOV_IMPORT_LIFECYCLE_V2.IMPORTING,
725
+ new Set([
726
+ KIMPGOV_IMPORT_LIFECYCLE_V2.IMPORTED,
727
+ KIMPGOV_IMPORT_LIFECYCLE_V2.FAILED,
728
+ KIMPGOV_IMPORT_LIFECYCLE_V2.CANCELLED,
729
+ ]),
730
+ ],
731
+ [KIMPGOV_IMPORT_LIFECYCLE_V2.IMPORTED, new Set()],
732
+ [KIMPGOV_IMPORT_LIFECYCLE_V2.FAILED, new Set()],
733
+ [KIMPGOV_IMPORT_LIFECYCLE_V2.CANCELLED, new Set()],
734
+ ]);
735
+ const _kimpgovPsV2 = new Map();
736
+ const _kimpgovJsV2 = new Map();
737
+ let _kimpgovMaxActive = 6,
738
+ _kimpgovMaxPending = 15,
739
+ _kimpgovIdleMs = 30 * 24 * 60 * 60 * 1000,
740
+ _kimpgovStuckMs = 60 * 1000;
741
+ function _kimpgovPos(n, label) {
742
+ const v = Math.floor(Number(n));
743
+ if (!Number.isFinite(v) || v <= 0)
744
+ throw new Error(`${label} must be positive integer`);
745
+ return v;
746
+ }
747
+ function _kimpgovCheckP(from, to) {
748
+ const a = _kimpgovPTrans.get(from);
749
+ if (!a || !a.has(to))
750
+ throw new Error(`invalid kimpgov profile transition ${from} → ${to}`);
751
+ }
752
+ function _kimpgovCheckJ(from, to) {
753
+ const a = _kimpgovJTrans.get(from);
754
+ if (!a || !a.has(to))
755
+ throw new Error(`invalid kimpgov import transition ${from} → ${to}`);
756
+ }
757
+ function _kimpgovCountActive(owner) {
758
+ let c = 0;
759
+ for (const p of _kimpgovPsV2.values())
760
+ if (p.owner === owner && p.status === KIMPGOV_PROFILE_MATURITY_V2.ACTIVE)
761
+ c++;
762
+ return c;
763
+ }
764
+ function _kimpgovCountPending(profileId) {
765
+ let c = 0;
766
+ for (const j of _kimpgovJsV2.values())
767
+ if (
768
+ j.profileId === profileId &&
769
+ (j.status === KIMPGOV_IMPORT_LIFECYCLE_V2.QUEUED ||
770
+ j.status === KIMPGOV_IMPORT_LIFECYCLE_V2.IMPORTING)
771
+ )
772
+ c++;
773
+ return c;
774
+ }
775
+ export function setMaxActiveKimpgovProfilesPerOwnerV2(n) {
776
+ _kimpgovMaxActive = _kimpgovPos(n, "maxActiveKimpgovProfilesPerOwner");
777
+ }
778
+ export function getMaxActiveKimpgovProfilesPerOwnerV2() {
779
+ return _kimpgovMaxActive;
780
+ }
781
+ export function setMaxPendingKimpgovImportsPerProfileV2(n) {
782
+ _kimpgovMaxPending = _kimpgovPos(n, "maxPendingKimpgovImportsPerProfile");
783
+ }
784
+ export function getMaxPendingKimpgovImportsPerProfileV2() {
785
+ return _kimpgovMaxPending;
786
+ }
787
+ export function setKimpgovProfileIdleMsV2(n) {
788
+ _kimpgovIdleMs = _kimpgovPos(n, "kimpgovProfileIdleMs");
789
+ }
790
+ export function getKimpgovProfileIdleMsV2() {
791
+ return _kimpgovIdleMs;
792
+ }
793
+ export function setKimpgovImportStuckMsV2(n) {
794
+ _kimpgovStuckMs = _kimpgovPos(n, "kimpgovImportStuckMs");
795
+ }
796
+ export function getKimpgovImportStuckMsV2() {
797
+ return _kimpgovStuckMs;
798
+ }
799
+ export function _resetStateKnowledgeImporterGovV2() {
800
+ _kimpgovPsV2.clear();
801
+ _kimpgovJsV2.clear();
802
+ _kimpgovMaxActive = 6;
803
+ _kimpgovMaxPending = 15;
804
+ _kimpgovIdleMs = 30 * 24 * 60 * 60 * 1000;
805
+ _kimpgovStuckMs = 60 * 1000;
806
+ }
807
+ export function registerKimpgovProfileV2({ id, owner, format, metadata } = {}) {
808
+ if (!id || !owner) throw new Error("id and owner required");
809
+ if (_kimpgovPsV2.has(id))
810
+ throw new Error(`kimpgov profile ${id} already exists`);
811
+ const now = Date.now();
812
+ const p = {
813
+ id,
814
+ owner,
815
+ format: format || "json",
816
+ status: KIMPGOV_PROFILE_MATURITY_V2.PENDING,
817
+ createdAt: now,
818
+ updatedAt: now,
819
+ lastTouchedAt: now,
820
+ activatedAt: null,
821
+ archivedAt: null,
822
+ metadata: { ...(metadata || {}) },
823
+ };
824
+ _kimpgovPsV2.set(id, p);
825
+ return { ...p, metadata: { ...p.metadata } };
826
+ }
827
+ export function activateKimpgovProfileV2(id) {
828
+ const p = _kimpgovPsV2.get(id);
829
+ if (!p) throw new Error(`kimpgov profile ${id} not found`);
830
+ const isInitial = p.status === KIMPGOV_PROFILE_MATURITY_V2.PENDING;
831
+ _kimpgovCheckP(p.status, KIMPGOV_PROFILE_MATURITY_V2.ACTIVE);
832
+ if (isInitial && _kimpgovCountActive(p.owner) >= _kimpgovMaxActive)
833
+ throw new Error(`max active kimpgov profiles for owner ${p.owner} reached`);
834
+ const now = Date.now();
835
+ p.status = KIMPGOV_PROFILE_MATURITY_V2.ACTIVE;
836
+ p.updatedAt = now;
837
+ p.lastTouchedAt = now;
838
+ if (!p.activatedAt) p.activatedAt = now;
839
+ return { ...p, metadata: { ...p.metadata } };
840
+ }
841
+ export function staleKimpgovProfileV2(id) {
842
+ const p = _kimpgovPsV2.get(id);
843
+ if (!p) throw new Error(`kimpgov profile ${id} not found`);
844
+ _kimpgovCheckP(p.status, KIMPGOV_PROFILE_MATURITY_V2.STALE);
845
+ p.status = KIMPGOV_PROFILE_MATURITY_V2.STALE;
846
+ p.updatedAt = Date.now();
847
+ return { ...p, metadata: { ...p.metadata } };
848
+ }
849
+ export function archiveKimpgovProfileV2(id) {
850
+ const p = _kimpgovPsV2.get(id);
851
+ if (!p) throw new Error(`kimpgov profile ${id} not found`);
852
+ _kimpgovCheckP(p.status, KIMPGOV_PROFILE_MATURITY_V2.ARCHIVED);
853
+ const now = Date.now();
854
+ p.status = KIMPGOV_PROFILE_MATURITY_V2.ARCHIVED;
855
+ p.updatedAt = now;
856
+ if (!p.archivedAt) p.archivedAt = now;
857
+ return { ...p, metadata: { ...p.metadata } };
858
+ }
859
+ export function touchKimpgovProfileV2(id) {
860
+ const p = _kimpgovPsV2.get(id);
861
+ if (!p) throw new Error(`kimpgov profile ${id} not found`);
862
+ if (_kimpgovPTerminal.has(p.status))
863
+ throw new Error(`cannot touch terminal kimpgov profile ${id}`);
864
+ const now = Date.now();
865
+ p.lastTouchedAt = now;
866
+ p.updatedAt = now;
867
+ return { ...p, metadata: { ...p.metadata } };
868
+ }
869
+ export function getKimpgovProfileV2(id) {
870
+ const p = _kimpgovPsV2.get(id);
871
+ if (!p) return null;
872
+ return { ...p, metadata: { ...p.metadata } };
873
+ }
874
+ export function listKimpgovProfilesV2() {
875
+ return [..._kimpgovPsV2.values()].map((p) => ({
876
+ ...p,
877
+ metadata: { ...p.metadata },
878
+ }));
879
+ }
880
+ export function createKimpgovImportV2({
881
+ id,
882
+ profileId,
883
+ source,
884
+ metadata,
885
+ } = {}) {
886
+ if (!id || !profileId) throw new Error("id and profileId required");
887
+ if (_kimpgovJsV2.has(id))
888
+ throw new Error(`kimpgov import ${id} already exists`);
889
+ if (!_kimpgovPsV2.has(profileId))
890
+ throw new Error(`kimpgov profile ${profileId} not found`);
891
+ if (_kimpgovCountPending(profileId) >= _kimpgovMaxPending)
892
+ throw new Error(
893
+ `max pending kimpgov imports for profile ${profileId} reached`,
894
+ );
895
+ const now = Date.now();
896
+ const j = {
897
+ id,
898
+ profileId,
899
+ source: source || "",
900
+ status: KIMPGOV_IMPORT_LIFECYCLE_V2.QUEUED,
901
+ createdAt: now,
902
+ updatedAt: now,
903
+ startedAt: null,
904
+ settledAt: null,
905
+ metadata: { ...(metadata || {}) },
906
+ };
907
+ _kimpgovJsV2.set(id, j);
908
+ return { ...j, metadata: { ...j.metadata } };
909
+ }
910
+ export function importingKimpgovImportV2(id) {
911
+ const j = _kimpgovJsV2.get(id);
912
+ if (!j) throw new Error(`kimpgov import ${id} not found`);
913
+ _kimpgovCheckJ(j.status, KIMPGOV_IMPORT_LIFECYCLE_V2.IMPORTING);
914
+ const now = Date.now();
915
+ j.status = KIMPGOV_IMPORT_LIFECYCLE_V2.IMPORTING;
916
+ j.updatedAt = now;
917
+ if (!j.startedAt) j.startedAt = now;
918
+ return { ...j, metadata: { ...j.metadata } };
919
+ }
920
+ export function completeImportKimpgovV2(id) {
921
+ const j = _kimpgovJsV2.get(id);
922
+ if (!j) throw new Error(`kimpgov import ${id} not found`);
923
+ _kimpgovCheckJ(j.status, KIMPGOV_IMPORT_LIFECYCLE_V2.IMPORTED);
924
+ const now = Date.now();
925
+ j.status = KIMPGOV_IMPORT_LIFECYCLE_V2.IMPORTED;
926
+ j.updatedAt = now;
927
+ if (!j.settledAt) j.settledAt = now;
928
+ return { ...j, metadata: { ...j.metadata } };
929
+ }
930
+ export function failKimpgovImportV2(id, reason) {
931
+ const j = _kimpgovJsV2.get(id);
932
+ if (!j) throw new Error(`kimpgov import ${id} not found`);
933
+ _kimpgovCheckJ(j.status, KIMPGOV_IMPORT_LIFECYCLE_V2.FAILED);
934
+ const now = Date.now();
935
+ j.status = KIMPGOV_IMPORT_LIFECYCLE_V2.FAILED;
936
+ j.updatedAt = now;
937
+ if (!j.settledAt) j.settledAt = now;
938
+ if (reason) j.metadata.failReason = String(reason);
939
+ return { ...j, metadata: { ...j.metadata } };
940
+ }
941
+ export function cancelKimpgovImportV2(id, reason) {
942
+ const j = _kimpgovJsV2.get(id);
943
+ if (!j) throw new Error(`kimpgov import ${id} not found`);
944
+ _kimpgovCheckJ(j.status, KIMPGOV_IMPORT_LIFECYCLE_V2.CANCELLED);
945
+ const now = Date.now();
946
+ j.status = KIMPGOV_IMPORT_LIFECYCLE_V2.CANCELLED;
947
+ j.updatedAt = now;
948
+ if (!j.settledAt) j.settledAt = now;
949
+ if (reason) j.metadata.cancelReason = String(reason);
950
+ return { ...j, metadata: { ...j.metadata } };
951
+ }
952
+ export function getKimpgovImportV2(id) {
953
+ const j = _kimpgovJsV2.get(id);
954
+ if (!j) return null;
955
+ return { ...j, metadata: { ...j.metadata } };
956
+ }
957
+ export function listKimpgovImportsV2() {
958
+ return [..._kimpgovJsV2.values()].map((j) => ({
959
+ ...j,
960
+ metadata: { ...j.metadata },
961
+ }));
962
+ }
963
+ export function autoStaleIdleKimpgovProfilesV2({ now } = {}) {
964
+ const t = now ?? Date.now();
965
+ const flipped = [];
966
+ for (const p of _kimpgovPsV2.values())
967
+ if (
968
+ p.status === KIMPGOV_PROFILE_MATURITY_V2.ACTIVE &&
969
+ t - p.lastTouchedAt >= _kimpgovIdleMs
970
+ ) {
971
+ p.status = KIMPGOV_PROFILE_MATURITY_V2.STALE;
972
+ p.updatedAt = t;
973
+ flipped.push(p.id);
974
+ }
975
+ return { flipped, count: flipped.length };
976
+ }
977
+ export function autoFailStuckKimpgovImportsV2({ now } = {}) {
978
+ const t = now ?? Date.now();
979
+ const flipped = [];
980
+ for (const j of _kimpgovJsV2.values())
981
+ if (
982
+ j.status === KIMPGOV_IMPORT_LIFECYCLE_V2.IMPORTING &&
983
+ j.startedAt != null &&
984
+ t - j.startedAt >= _kimpgovStuckMs
985
+ ) {
986
+ j.status = KIMPGOV_IMPORT_LIFECYCLE_V2.FAILED;
987
+ j.updatedAt = t;
988
+ if (!j.settledAt) j.settledAt = t;
989
+ j.metadata.failReason = "auto-fail-stuck";
990
+ flipped.push(j.id);
991
+ }
992
+ return { flipped, count: flipped.length };
993
+ }
994
+ export function getKnowledgeImporterGovStatsV2() {
995
+ const profilesByStatus = {};
996
+ for (const v of Object.values(KIMPGOV_PROFILE_MATURITY_V2))
997
+ profilesByStatus[v] = 0;
998
+ for (const p of _kimpgovPsV2.values()) profilesByStatus[p.status]++;
999
+ const importsByStatus = {};
1000
+ for (const v of Object.values(KIMPGOV_IMPORT_LIFECYCLE_V2))
1001
+ importsByStatus[v] = 0;
1002
+ for (const j of _kimpgovJsV2.values()) importsByStatus[j.status]++;
1003
+ return {
1004
+ totalKimpgovProfilesV2: _kimpgovPsV2.size,
1005
+ totalKimpgovImportsV2: _kimpgovJsV2.size,
1006
+ maxActiveKimpgovProfilesPerOwner: _kimpgovMaxActive,
1007
+ maxPendingKimpgovImportsPerProfile: _kimpgovMaxPending,
1008
+ kimpgovProfileIdleMs: _kimpgovIdleMs,
1009
+ kimpgovImportStuckMs: _kimpgovStuckMs,
1010
+ profilesByStatus,
1011
+ importsByStatus,
1012
+ };
1013
+ }