chainlesschain 0.152.0 → 0.156.2

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 (80) 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/agent.js +1250 -0
  5. package/src/commands/chat.js +605 -0
  6. package/src/commands/cli-anything.js +426 -0
  7. package/src/commands/compliance.js +412 -0
  8. package/src/commands/config.js +213 -0
  9. package/src/commands/cowork.js +1463 -0
  10. package/src/commands/crosschain.js +203 -0
  11. package/src/commands/dao.js +203 -0
  12. package/src/commands/economy.js +199 -0
  13. package/src/commands/encrypt.js +201 -0
  14. package/src/commands/evolution.js +199 -0
  15. package/src/commands/evomap.js +625 -0
  16. package/src/commands/hmemory.js +203 -0
  17. package/src/commands/inference.js +207 -0
  18. package/src/commands/kg.js +195 -0
  19. package/src/commands/llm.js +209 -0
  20. package/src/commands/memory.js +203 -0
  21. package/src/commands/orchestrate.js +406 -0
  22. package/src/commands/pipeline.js +199 -0
  23. package/src/commands/planmode.js +426 -0
  24. package/src/commands/plugin.js +209 -0
  25. package/src/commands/services.js +207 -0
  26. package/src/commands/setup.js +205 -0
  27. package/src/commands/skill.js +207 -0
  28. package/src/commands/start.js +209 -0
  29. package/src/commands/stream.js +213 -0
  30. package/src/commands/ui.js +225 -0
  31. package/src/commands/workflow.js +209 -0
  32. package/src/index.js +112 -0
  33. package/src/lib/a2a-protocol.js +332 -0
  34. package/src/lib/agent-coordinator.js +334 -0
  35. package/src/lib/agent-economy.js +334 -0
  36. package/src/lib/agent-router.js +333 -0
  37. package/src/lib/autonomous-agent.js +332 -0
  38. package/src/lib/chat-core.js +335 -0
  39. package/src/lib/cli-anything-bridge.js +341 -0
  40. package/src/lib/cli-context-engineering.js +351 -0
  41. package/src/lib/compliance-manager.js +334 -0
  42. package/src/lib/cowork-adapter.js +336 -0
  43. package/src/lib/cowork-evomap-adapter.js +341 -0
  44. package/src/lib/cowork-mcp-tools.js +341 -0
  45. package/src/lib/cowork-observe-html.js +341 -0
  46. package/src/lib/cowork-observe.js +341 -0
  47. package/src/lib/cowork-task-templates.js +342 -1
  48. package/src/lib/cowork-template-marketplace.js +340 -0
  49. package/src/lib/cross-chain.js +339 -0
  50. package/src/lib/crypto-manager.js +334 -0
  51. package/src/lib/dao-governance.js +339 -0
  52. package/src/lib/downloader.js +334 -0
  53. package/src/lib/evolution-system.js +334 -0
  54. package/src/lib/evomap-client.js +342 -0
  55. package/src/lib/evomap-federation.js +338 -0
  56. package/src/lib/evomap-manager.js +330 -0
  57. package/src/lib/execution-backend.js +330 -0
  58. package/src/lib/hashline.js +338 -0
  59. package/src/lib/hierarchical-memory.js +334 -0
  60. package/src/lib/inference-network.js +341 -0
  61. package/src/lib/interaction-adapter.js +330 -0
  62. package/src/lib/interactive-planner.js +354 -0
  63. package/src/lib/knowledge-graph.js +331 -0
  64. package/src/lib/pipeline-orchestrator.js +332 -0
  65. package/src/lib/plan-mode.js +336 -0
  66. package/src/lib/plugin-autodiscovery.js +334 -0
  67. package/src/lib/process-manager.js +336 -0
  68. package/src/lib/provider-options.js +346 -0
  69. package/src/lib/provider-stream.js +348 -0
  70. package/src/lib/service-manager.js +337 -0
  71. package/src/lib/session-core-singletons.js +341 -0
  72. package/src/lib/skill-mcp.js +336 -0
  73. package/src/lib/stix-parser.js +346 -0
  74. package/src/lib/sub-agent-context.js +343 -0
  75. package/src/lib/sub-agent-profiles.js +335 -0
  76. package/src/lib/sub-agent-registry.js +336 -0
  77. package/src/lib/todo-manager.js +336 -0
  78. package/src/lib/web-ui-server.js +348 -0
  79. package/src/lib/workflow-expr.js +346 -0
  80. package/src/lib/ws-chat-handler.js +337 -0
@@ -522,3 +522,357 @@ Keep plans concise (3-8 steps). Use appropriate tools for each step.`;
522
522
  return Array.from(words);
523
523
  }
524
524
  }
525
+
526
+ // =====================================================================
527
+ // interactive-planner V2 governance overlay (iter26)
528
+ // =====================================================================
529
+ export const PLANNERGOV_PROFILE_MATURITY_V2 = Object.freeze({
530
+ PENDING: "pending",
531
+ ACTIVE: "active",
532
+ PAUSED: "paused",
533
+ ARCHIVED: "archived",
534
+ });
535
+ export const PLANNERGOV_PROMPT_LIFECYCLE_V2 = Object.freeze({
536
+ QUEUED: "queued",
537
+ ASKING: "asking",
538
+ ANSWERED: "answered",
539
+ FAILED: "failed",
540
+ CANCELLED: "cancelled",
541
+ });
542
+ const _plannergovPTrans = new Map([
543
+ [
544
+ PLANNERGOV_PROFILE_MATURITY_V2.PENDING,
545
+ new Set([
546
+ PLANNERGOV_PROFILE_MATURITY_V2.ACTIVE,
547
+ PLANNERGOV_PROFILE_MATURITY_V2.ARCHIVED,
548
+ ]),
549
+ ],
550
+ [
551
+ PLANNERGOV_PROFILE_MATURITY_V2.ACTIVE,
552
+ new Set([
553
+ PLANNERGOV_PROFILE_MATURITY_V2.PAUSED,
554
+ PLANNERGOV_PROFILE_MATURITY_V2.ARCHIVED,
555
+ ]),
556
+ ],
557
+ [
558
+ PLANNERGOV_PROFILE_MATURITY_V2.PAUSED,
559
+ new Set([
560
+ PLANNERGOV_PROFILE_MATURITY_V2.ACTIVE,
561
+ PLANNERGOV_PROFILE_MATURITY_V2.ARCHIVED,
562
+ ]),
563
+ ],
564
+ [PLANNERGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
565
+ ]);
566
+ const _plannergovPTerminal = new Set([PLANNERGOV_PROFILE_MATURITY_V2.ARCHIVED]);
567
+ const _plannergovJTrans = new Map([
568
+ [
569
+ PLANNERGOV_PROMPT_LIFECYCLE_V2.QUEUED,
570
+ new Set([
571
+ PLANNERGOV_PROMPT_LIFECYCLE_V2.ASKING,
572
+ PLANNERGOV_PROMPT_LIFECYCLE_V2.CANCELLED,
573
+ ]),
574
+ ],
575
+ [
576
+ PLANNERGOV_PROMPT_LIFECYCLE_V2.ASKING,
577
+ new Set([
578
+ PLANNERGOV_PROMPT_LIFECYCLE_V2.ANSWERED,
579
+ PLANNERGOV_PROMPT_LIFECYCLE_V2.FAILED,
580
+ PLANNERGOV_PROMPT_LIFECYCLE_V2.CANCELLED,
581
+ ]),
582
+ ],
583
+ [PLANNERGOV_PROMPT_LIFECYCLE_V2.ANSWERED, new Set()],
584
+ [PLANNERGOV_PROMPT_LIFECYCLE_V2.FAILED, new Set()],
585
+ [PLANNERGOV_PROMPT_LIFECYCLE_V2.CANCELLED, new Set()],
586
+ ]);
587
+ const _plannergovPsV2 = new Map();
588
+ const _plannergovJsV2 = new Map();
589
+ let _plannergovMaxActive = 6,
590
+ _plannergovMaxPending = 15,
591
+ _plannergovIdleMs = 30 * 24 * 60 * 60 * 1000,
592
+ _plannergovStuckMs = 60 * 1000;
593
+ function _plannergovPos(n, label) {
594
+ const v = Math.floor(Number(n));
595
+ if (!Number.isFinite(v) || v <= 0)
596
+ throw new Error(`${label} must be positive integer`);
597
+ return v;
598
+ }
599
+ function _plannergovCheckP(from, to) {
600
+ const a = _plannergovPTrans.get(from);
601
+ if (!a || !a.has(to))
602
+ throw new Error(`invalid plannergov profile transition ${from} → ${to}`);
603
+ }
604
+ function _plannergovCheckJ(from, to) {
605
+ const a = _plannergovJTrans.get(from);
606
+ if (!a || !a.has(to))
607
+ throw new Error(`invalid plannergov prompt transition ${from} → ${to}`);
608
+ }
609
+ function _plannergovCountActive(owner) {
610
+ let c = 0;
611
+ for (const p of _plannergovPsV2.values())
612
+ if (p.owner === owner && p.status === PLANNERGOV_PROFILE_MATURITY_V2.ACTIVE)
613
+ c++;
614
+ return c;
615
+ }
616
+ function _plannergovCountPending(profileId) {
617
+ let c = 0;
618
+ for (const j of _plannergovJsV2.values())
619
+ if (
620
+ j.profileId === profileId &&
621
+ (j.status === PLANNERGOV_PROMPT_LIFECYCLE_V2.QUEUED ||
622
+ j.status === PLANNERGOV_PROMPT_LIFECYCLE_V2.ASKING)
623
+ )
624
+ c++;
625
+ return c;
626
+ }
627
+ export function setMaxActivePlannergovProfilesPerOwnerV2(n) {
628
+ _plannergovMaxActive = _plannergovPos(
629
+ n,
630
+ "maxActivePlannergovProfilesPerOwner",
631
+ );
632
+ }
633
+ export function getMaxActivePlannergovProfilesPerOwnerV2() {
634
+ return _plannergovMaxActive;
635
+ }
636
+ export function setMaxPendingPlannergovPromptsPerProfileV2(n) {
637
+ _plannergovMaxPending = _plannergovPos(
638
+ n,
639
+ "maxPendingPlannergovPromptsPerProfile",
640
+ );
641
+ }
642
+ export function getMaxPendingPlannergovPromptsPerProfileV2() {
643
+ return _plannergovMaxPending;
644
+ }
645
+ export function setPlannergovProfileIdleMsV2(n) {
646
+ _plannergovIdleMs = _plannergovPos(n, "plannergovProfileIdleMs");
647
+ }
648
+ export function getPlannergovProfileIdleMsV2() {
649
+ return _plannergovIdleMs;
650
+ }
651
+ export function setPlannergovPromptStuckMsV2(n) {
652
+ _plannergovStuckMs = _plannergovPos(n, "plannergovPromptStuckMs");
653
+ }
654
+ export function getPlannergovPromptStuckMsV2() {
655
+ return _plannergovStuckMs;
656
+ }
657
+ export function _resetStateInteractivePlannerGovV2() {
658
+ _plannergovPsV2.clear();
659
+ _plannergovJsV2.clear();
660
+ _plannergovMaxActive = 6;
661
+ _plannergovMaxPending = 15;
662
+ _plannergovIdleMs = 30 * 24 * 60 * 60 * 1000;
663
+ _plannergovStuckMs = 60 * 1000;
664
+ }
665
+ export function registerPlannergovProfileV2({
666
+ id,
667
+ owner,
668
+ persona,
669
+ metadata,
670
+ } = {}) {
671
+ if (!id || !owner) throw new Error("id and owner required");
672
+ if (_plannergovPsV2.has(id))
673
+ throw new Error(`plannergov profile ${id} already exists`);
674
+ const now = Date.now();
675
+ const p = {
676
+ id,
677
+ owner,
678
+ persona: persona || "default",
679
+ status: PLANNERGOV_PROFILE_MATURITY_V2.PENDING,
680
+ createdAt: now,
681
+ updatedAt: now,
682
+ lastTouchedAt: now,
683
+ activatedAt: null,
684
+ archivedAt: null,
685
+ metadata: { ...(metadata || {}) },
686
+ };
687
+ _plannergovPsV2.set(id, p);
688
+ return { ...p, metadata: { ...p.metadata } };
689
+ }
690
+ export function activatePlannergovProfileV2(id) {
691
+ const p = _plannergovPsV2.get(id);
692
+ if (!p) throw new Error(`plannergov profile ${id} not found`);
693
+ const isInitial = p.status === PLANNERGOV_PROFILE_MATURITY_V2.PENDING;
694
+ _plannergovCheckP(p.status, PLANNERGOV_PROFILE_MATURITY_V2.ACTIVE);
695
+ if (isInitial && _plannergovCountActive(p.owner) >= _plannergovMaxActive)
696
+ throw new Error(
697
+ `max active plannergov profiles for owner ${p.owner} reached`,
698
+ );
699
+ const now = Date.now();
700
+ p.status = PLANNERGOV_PROFILE_MATURITY_V2.ACTIVE;
701
+ p.updatedAt = now;
702
+ p.lastTouchedAt = now;
703
+ if (!p.activatedAt) p.activatedAt = now;
704
+ return { ...p, metadata: { ...p.metadata } };
705
+ }
706
+ export function pausePlannergovProfileV2(id) {
707
+ const p = _plannergovPsV2.get(id);
708
+ if (!p) throw new Error(`plannergov profile ${id} not found`);
709
+ _plannergovCheckP(p.status, PLANNERGOV_PROFILE_MATURITY_V2.PAUSED);
710
+ p.status = PLANNERGOV_PROFILE_MATURITY_V2.PAUSED;
711
+ p.updatedAt = Date.now();
712
+ return { ...p, metadata: { ...p.metadata } };
713
+ }
714
+ export function archivePlannergovProfileV2(id) {
715
+ const p = _plannergovPsV2.get(id);
716
+ if (!p) throw new Error(`plannergov profile ${id} not found`);
717
+ _plannergovCheckP(p.status, PLANNERGOV_PROFILE_MATURITY_V2.ARCHIVED);
718
+ const now = Date.now();
719
+ p.status = PLANNERGOV_PROFILE_MATURITY_V2.ARCHIVED;
720
+ p.updatedAt = now;
721
+ if (!p.archivedAt) p.archivedAt = now;
722
+ return { ...p, metadata: { ...p.metadata } };
723
+ }
724
+ export function touchPlannergovProfileV2(id) {
725
+ const p = _plannergovPsV2.get(id);
726
+ if (!p) throw new Error(`plannergov profile ${id} not found`);
727
+ if (_plannergovPTerminal.has(p.status))
728
+ throw new Error(`cannot touch terminal plannergov profile ${id}`);
729
+ const now = Date.now();
730
+ p.lastTouchedAt = now;
731
+ p.updatedAt = now;
732
+ return { ...p, metadata: { ...p.metadata } };
733
+ }
734
+ export function getPlannergovProfileV2(id) {
735
+ const p = _plannergovPsV2.get(id);
736
+ if (!p) return null;
737
+ return { ...p, metadata: { ...p.metadata } };
738
+ }
739
+ export function listPlannergovProfilesV2() {
740
+ return [..._plannergovPsV2.values()].map((p) => ({
741
+ ...p,
742
+ metadata: { ...p.metadata },
743
+ }));
744
+ }
745
+ export function createPlannergovPromptV2({
746
+ id,
747
+ profileId,
748
+ question,
749
+ metadata,
750
+ } = {}) {
751
+ if (!id || !profileId) throw new Error("id and profileId required");
752
+ if (_plannergovJsV2.has(id))
753
+ throw new Error(`plannergov prompt ${id} already exists`);
754
+ if (!_plannergovPsV2.has(profileId))
755
+ throw new Error(`plannergov profile ${profileId} not found`);
756
+ if (_plannergovCountPending(profileId) >= _plannergovMaxPending)
757
+ throw new Error(
758
+ `max pending plannergov prompts for profile ${profileId} reached`,
759
+ );
760
+ const now = Date.now();
761
+ const j = {
762
+ id,
763
+ profileId,
764
+ question: question || "",
765
+ status: PLANNERGOV_PROMPT_LIFECYCLE_V2.QUEUED,
766
+ createdAt: now,
767
+ updatedAt: now,
768
+ startedAt: null,
769
+ settledAt: null,
770
+ metadata: { ...(metadata || {}) },
771
+ };
772
+ _plannergovJsV2.set(id, j);
773
+ return { ...j, metadata: { ...j.metadata } };
774
+ }
775
+ export function askingPlannergovPromptV2(id) {
776
+ const j = _plannergovJsV2.get(id);
777
+ if (!j) throw new Error(`plannergov prompt ${id} not found`);
778
+ _plannergovCheckJ(j.status, PLANNERGOV_PROMPT_LIFECYCLE_V2.ASKING);
779
+ const now = Date.now();
780
+ j.status = PLANNERGOV_PROMPT_LIFECYCLE_V2.ASKING;
781
+ j.updatedAt = now;
782
+ if (!j.startedAt) j.startedAt = now;
783
+ return { ...j, metadata: { ...j.metadata } };
784
+ }
785
+ export function completePromptPlannergovV2(id) {
786
+ const j = _plannergovJsV2.get(id);
787
+ if (!j) throw new Error(`plannergov prompt ${id} not found`);
788
+ _plannergovCheckJ(j.status, PLANNERGOV_PROMPT_LIFECYCLE_V2.ANSWERED);
789
+ const now = Date.now();
790
+ j.status = PLANNERGOV_PROMPT_LIFECYCLE_V2.ANSWERED;
791
+ j.updatedAt = now;
792
+ if (!j.settledAt) j.settledAt = now;
793
+ return { ...j, metadata: { ...j.metadata } };
794
+ }
795
+ export function failPlannergovPromptV2(id, reason) {
796
+ const j = _plannergovJsV2.get(id);
797
+ if (!j) throw new Error(`plannergov prompt ${id} not found`);
798
+ _plannergovCheckJ(j.status, PLANNERGOV_PROMPT_LIFECYCLE_V2.FAILED);
799
+ const now = Date.now();
800
+ j.status = PLANNERGOV_PROMPT_LIFECYCLE_V2.FAILED;
801
+ j.updatedAt = now;
802
+ if (!j.settledAt) j.settledAt = now;
803
+ if (reason) j.metadata.failReason = String(reason);
804
+ return { ...j, metadata: { ...j.metadata } };
805
+ }
806
+ export function cancelPlannergovPromptV2(id, reason) {
807
+ const j = _plannergovJsV2.get(id);
808
+ if (!j) throw new Error(`plannergov prompt ${id} not found`);
809
+ _plannergovCheckJ(j.status, PLANNERGOV_PROMPT_LIFECYCLE_V2.CANCELLED);
810
+ const now = Date.now();
811
+ j.status = PLANNERGOV_PROMPT_LIFECYCLE_V2.CANCELLED;
812
+ j.updatedAt = now;
813
+ if (!j.settledAt) j.settledAt = now;
814
+ if (reason) j.metadata.cancelReason = String(reason);
815
+ return { ...j, metadata: { ...j.metadata } };
816
+ }
817
+ export function getPlannergovPromptV2(id) {
818
+ const j = _plannergovJsV2.get(id);
819
+ if (!j) return null;
820
+ return { ...j, metadata: { ...j.metadata } };
821
+ }
822
+ export function listPlannergovPromptsV2() {
823
+ return [..._plannergovJsV2.values()].map((j) => ({
824
+ ...j,
825
+ metadata: { ...j.metadata },
826
+ }));
827
+ }
828
+ export function autoPauseIdlePlannergovProfilesV2({ now } = {}) {
829
+ const t = now ?? Date.now();
830
+ const flipped = [];
831
+ for (const p of _plannergovPsV2.values())
832
+ if (
833
+ p.status === PLANNERGOV_PROFILE_MATURITY_V2.ACTIVE &&
834
+ t - p.lastTouchedAt >= _plannergovIdleMs
835
+ ) {
836
+ p.status = PLANNERGOV_PROFILE_MATURITY_V2.PAUSED;
837
+ p.updatedAt = t;
838
+ flipped.push(p.id);
839
+ }
840
+ return { flipped, count: flipped.length };
841
+ }
842
+ export function autoFailStuckPlannergovPromptsV2({ now } = {}) {
843
+ const t = now ?? Date.now();
844
+ const flipped = [];
845
+ for (const j of _plannergovJsV2.values())
846
+ if (
847
+ j.status === PLANNERGOV_PROMPT_LIFECYCLE_V2.ASKING &&
848
+ j.startedAt != null &&
849
+ t - j.startedAt >= _plannergovStuckMs
850
+ ) {
851
+ j.status = PLANNERGOV_PROMPT_LIFECYCLE_V2.FAILED;
852
+ j.updatedAt = t;
853
+ if (!j.settledAt) j.settledAt = t;
854
+ j.metadata.failReason = "auto-fail-stuck";
855
+ flipped.push(j.id);
856
+ }
857
+ return { flipped, count: flipped.length };
858
+ }
859
+ export function getInteractivePlannerGovStatsV2() {
860
+ const profilesByStatus = {};
861
+ for (const v of Object.values(PLANNERGOV_PROFILE_MATURITY_V2))
862
+ profilesByStatus[v] = 0;
863
+ for (const p of _plannergovPsV2.values()) profilesByStatus[p.status]++;
864
+ const promptsByStatus = {};
865
+ for (const v of Object.values(PLANNERGOV_PROMPT_LIFECYCLE_V2))
866
+ promptsByStatus[v] = 0;
867
+ for (const j of _plannergovJsV2.values()) promptsByStatus[j.status]++;
868
+ return {
869
+ totalPlannergovProfilesV2: _plannergovPsV2.size,
870
+ totalPlannergovPromptsV2: _plannergovJsV2.size,
871
+ maxActivePlannergovProfilesPerOwner: _plannergovMaxActive,
872
+ maxPendingPlannergovPromptsPerProfile: _plannergovMaxPending,
873
+ plannergovProfileIdleMs: _plannergovIdleMs,
874
+ plannergovPromptStuckMs: _plannergovStuckMs,
875
+ profilesByStatus,
876
+ promptsByStatus,
877
+ };
878
+ }
@@ -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
+ }