chainlesschain 0.152.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 (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
@@ -564,3 +564,333 @@ export function getEvoMapManagerStatsV2() {
564
564
  evosByStatus,
565
565
  };
566
566
  }
567
+
568
+ // === Iter28 V2 governance overlay: Emgrgov ===
569
+ export const EMGRGOV_PROFILE_MATURITY_V2 = Object.freeze({
570
+ PENDING: "pending",
571
+ ACTIVE: "active",
572
+ STALE: "stale",
573
+ ARCHIVED: "archived",
574
+ });
575
+ export const EMGRGOV_OP_LIFECYCLE_V2 = Object.freeze({
576
+ QUEUED: "queued",
577
+ OPERATING: "operating",
578
+ OPERATED: "operated",
579
+ FAILED: "failed",
580
+ CANCELLED: "cancelled",
581
+ });
582
+ const _emgrgovPTrans = new Map([
583
+ [
584
+ EMGRGOV_PROFILE_MATURITY_V2.PENDING,
585
+ new Set([
586
+ EMGRGOV_PROFILE_MATURITY_V2.ACTIVE,
587
+ EMGRGOV_PROFILE_MATURITY_V2.ARCHIVED,
588
+ ]),
589
+ ],
590
+ [
591
+ EMGRGOV_PROFILE_MATURITY_V2.ACTIVE,
592
+ new Set([
593
+ EMGRGOV_PROFILE_MATURITY_V2.STALE,
594
+ EMGRGOV_PROFILE_MATURITY_V2.ARCHIVED,
595
+ ]),
596
+ ],
597
+ [
598
+ EMGRGOV_PROFILE_MATURITY_V2.STALE,
599
+ new Set([
600
+ EMGRGOV_PROFILE_MATURITY_V2.ACTIVE,
601
+ EMGRGOV_PROFILE_MATURITY_V2.ARCHIVED,
602
+ ]),
603
+ ],
604
+ [EMGRGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
605
+ ]);
606
+ const _emgrgovPTerminal = new Set([EMGRGOV_PROFILE_MATURITY_V2.ARCHIVED]);
607
+ const _emgrgovJTrans = new Map([
608
+ [
609
+ EMGRGOV_OP_LIFECYCLE_V2.QUEUED,
610
+ new Set([
611
+ EMGRGOV_OP_LIFECYCLE_V2.OPERATING,
612
+ EMGRGOV_OP_LIFECYCLE_V2.CANCELLED,
613
+ ]),
614
+ ],
615
+ [
616
+ EMGRGOV_OP_LIFECYCLE_V2.OPERATING,
617
+ new Set([
618
+ EMGRGOV_OP_LIFECYCLE_V2.OPERATED,
619
+ EMGRGOV_OP_LIFECYCLE_V2.FAILED,
620
+ EMGRGOV_OP_LIFECYCLE_V2.CANCELLED,
621
+ ]),
622
+ ],
623
+ [EMGRGOV_OP_LIFECYCLE_V2.OPERATED, new Set()],
624
+ [EMGRGOV_OP_LIFECYCLE_V2.FAILED, new Set()],
625
+ [EMGRGOV_OP_LIFECYCLE_V2.CANCELLED, new Set()],
626
+ ]);
627
+ const _emgrgovPsV2 = new Map();
628
+ const _emgrgovJsV2 = new Map();
629
+ let _emgrgovMaxActive = 8,
630
+ _emgrgovMaxPending = 20,
631
+ _emgrgovIdleMs = 2592000000,
632
+ _emgrgovStuckMs = 60 * 1000;
633
+ function _emgrgovPos(n, label) {
634
+ const v = Math.floor(Number(n));
635
+ if (!Number.isFinite(v) || v <= 0)
636
+ throw new Error(`${label} must be positive integer`);
637
+ return v;
638
+ }
639
+ function _emgrgovCheckP(from, to) {
640
+ const a = _emgrgovPTrans.get(from);
641
+ if (!a || !a.has(to))
642
+ throw new Error(`invalid emgrgov profile transition ${from} → ${to}`);
643
+ }
644
+ function _emgrgovCheckJ(from, to) {
645
+ const a = _emgrgovJTrans.get(from);
646
+ if (!a || !a.has(to))
647
+ throw new Error(`invalid emgrgov op transition ${from} → ${to}`);
648
+ }
649
+ function _emgrgovCountActive(owner) {
650
+ let c = 0;
651
+ for (const p of _emgrgovPsV2.values())
652
+ if (p.owner === owner && p.status === EMGRGOV_PROFILE_MATURITY_V2.ACTIVE)
653
+ c++;
654
+ return c;
655
+ }
656
+ function _emgrgovCountPending(profileId) {
657
+ let c = 0;
658
+ for (const j of _emgrgovJsV2.values())
659
+ if (
660
+ j.profileId === profileId &&
661
+ (j.status === EMGRGOV_OP_LIFECYCLE_V2.QUEUED ||
662
+ j.status === EMGRGOV_OP_LIFECYCLE_V2.OPERATING)
663
+ )
664
+ c++;
665
+ return c;
666
+ }
667
+ export function setMaxActiveEmgrProfilesPerOwnerV2(n) {
668
+ _emgrgovMaxActive = _emgrgovPos(n, "maxActiveEmgrProfilesPerOwner");
669
+ }
670
+ export function getMaxActiveEmgrProfilesPerOwnerV2() {
671
+ return _emgrgovMaxActive;
672
+ }
673
+ export function setMaxPendingEmgrOpsPerProfileV2(n) {
674
+ _emgrgovMaxPending = _emgrgovPos(n, "maxPendingEmgrOpsPerProfile");
675
+ }
676
+ export function getMaxPendingEmgrOpsPerProfileV2() {
677
+ return _emgrgovMaxPending;
678
+ }
679
+ export function setEmgrProfileIdleMsV2(n) {
680
+ _emgrgovIdleMs = _emgrgovPos(n, "emgrgovProfileIdleMs");
681
+ }
682
+ export function getEmgrProfileIdleMsV2() {
683
+ return _emgrgovIdleMs;
684
+ }
685
+ export function setEmgrOpStuckMsV2(n) {
686
+ _emgrgovStuckMs = _emgrgovPos(n, "emgrgovOpStuckMs");
687
+ }
688
+ export function getEmgrOpStuckMsV2() {
689
+ return _emgrgovStuckMs;
690
+ }
691
+ export function _resetStateEmgrgovV2() {
692
+ _emgrgovPsV2.clear();
693
+ _emgrgovJsV2.clear();
694
+ _emgrgovMaxActive = 8;
695
+ _emgrgovMaxPending = 20;
696
+ _emgrgovIdleMs = 2592000000;
697
+ _emgrgovStuckMs = 60 * 1000;
698
+ }
699
+ export function registerEmgrProfileV2({ id, owner, map, metadata } = {}) {
700
+ if (!id || !owner) throw new Error("id and owner required");
701
+ if (_emgrgovPsV2.has(id))
702
+ throw new Error(`emgrgov profile ${id} already exists`);
703
+ const now = Date.now();
704
+ const p = {
705
+ id,
706
+ owner,
707
+ map: map || "default",
708
+ status: EMGRGOV_PROFILE_MATURITY_V2.PENDING,
709
+ createdAt: now,
710
+ updatedAt: now,
711
+ lastTouchedAt: now,
712
+ activatedAt: null,
713
+ archivedAt: null,
714
+ metadata: { ...(metadata || {}) },
715
+ };
716
+ _emgrgovPsV2.set(id, p);
717
+ return { ...p, metadata: { ...p.metadata } };
718
+ }
719
+ export function activateEmgrProfileV2(id) {
720
+ const p = _emgrgovPsV2.get(id);
721
+ if (!p) throw new Error(`emgrgov profile ${id} not found`);
722
+ const isInitial = p.status === EMGRGOV_PROFILE_MATURITY_V2.PENDING;
723
+ _emgrgovCheckP(p.status, EMGRGOV_PROFILE_MATURITY_V2.ACTIVE);
724
+ if (isInitial && _emgrgovCountActive(p.owner) >= _emgrgovMaxActive)
725
+ throw new Error(`max active emgrgov profiles for owner ${p.owner} reached`);
726
+ const now = Date.now();
727
+ p.status = EMGRGOV_PROFILE_MATURITY_V2.ACTIVE;
728
+ p.updatedAt = now;
729
+ p.lastTouchedAt = now;
730
+ if (!p.activatedAt) p.activatedAt = now;
731
+ return { ...p, metadata: { ...p.metadata } };
732
+ }
733
+ export function staleEmgrProfileV2(id) {
734
+ const p = _emgrgovPsV2.get(id);
735
+ if (!p) throw new Error(`emgrgov profile ${id} not found`);
736
+ _emgrgovCheckP(p.status, EMGRGOV_PROFILE_MATURITY_V2.STALE);
737
+ p.status = EMGRGOV_PROFILE_MATURITY_V2.STALE;
738
+ p.updatedAt = Date.now();
739
+ return { ...p, metadata: { ...p.metadata } };
740
+ }
741
+ export function archiveEmgrProfileV2(id) {
742
+ const p = _emgrgovPsV2.get(id);
743
+ if (!p) throw new Error(`emgrgov profile ${id} not found`);
744
+ _emgrgovCheckP(p.status, EMGRGOV_PROFILE_MATURITY_V2.ARCHIVED);
745
+ const now = Date.now();
746
+ p.status = EMGRGOV_PROFILE_MATURITY_V2.ARCHIVED;
747
+ p.updatedAt = now;
748
+ if (!p.archivedAt) p.archivedAt = now;
749
+ return { ...p, metadata: { ...p.metadata } };
750
+ }
751
+ export function touchEmgrProfileV2(id) {
752
+ const p = _emgrgovPsV2.get(id);
753
+ if (!p) throw new Error(`emgrgov profile ${id} not found`);
754
+ if (_emgrgovPTerminal.has(p.status))
755
+ throw new Error(`cannot touch terminal emgrgov profile ${id}`);
756
+ const now = Date.now();
757
+ p.lastTouchedAt = now;
758
+ p.updatedAt = now;
759
+ return { ...p, metadata: { ...p.metadata } };
760
+ }
761
+ export function getEmgrProfileV2(id) {
762
+ const p = _emgrgovPsV2.get(id);
763
+ if (!p) return null;
764
+ return { ...p, metadata: { ...p.metadata } };
765
+ }
766
+ export function listEmgrProfilesV2() {
767
+ return [..._emgrgovPsV2.values()].map((p) => ({
768
+ ...p,
769
+ metadata: { ...p.metadata },
770
+ }));
771
+ }
772
+ export function createEmgrOpV2({ id, profileId, opId, metadata } = {}) {
773
+ if (!id || !profileId) throw new Error("id and profileId required");
774
+ if (_emgrgovJsV2.has(id)) throw new Error(`emgrgov op ${id} already exists`);
775
+ if (!_emgrgovPsV2.has(profileId))
776
+ throw new Error(`emgrgov profile ${profileId} not found`);
777
+ if (_emgrgovCountPending(profileId) >= _emgrgovMaxPending)
778
+ throw new Error(`max pending emgrgov ops for profile ${profileId} reached`);
779
+ const now = Date.now();
780
+ const j = {
781
+ id,
782
+ profileId,
783
+ opId: opId || "",
784
+ status: EMGRGOV_OP_LIFECYCLE_V2.QUEUED,
785
+ createdAt: now,
786
+ updatedAt: now,
787
+ startedAt: null,
788
+ settledAt: null,
789
+ metadata: { ...(metadata || {}) },
790
+ };
791
+ _emgrgovJsV2.set(id, j);
792
+ return { ...j, metadata: { ...j.metadata } };
793
+ }
794
+ export function operatingEmgrOpV2(id) {
795
+ const j = _emgrgovJsV2.get(id);
796
+ if (!j) throw new Error(`emgrgov op ${id} not found`);
797
+ _emgrgovCheckJ(j.status, EMGRGOV_OP_LIFECYCLE_V2.OPERATING);
798
+ const now = Date.now();
799
+ j.status = EMGRGOV_OP_LIFECYCLE_V2.OPERATING;
800
+ j.updatedAt = now;
801
+ if (!j.startedAt) j.startedAt = now;
802
+ return { ...j, metadata: { ...j.metadata } };
803
+ }
804
+ export function completeOpEmgrV2(id) {
805
+ const j = _emgrgovJsV2.get(id);
806
+ if (!j) throw new Error(`emgrgov op ${id} not found`);
807
+ _emgrgovCheckJ(j.status, EMGRGOV_OP_LIFECYCLE_V2.OPERATED);
808
+ const now = Date.now();
809
+ j.status = EMGRGOV_OP_LIFECYCLE_V2.OPERATED;
810
+ j.updatedAt = now;
811
+ if (!j.settledAt) j.settledAt = now;
812
+ return { ...j, metadata: { ...j.metadata } };
813
+ }
814
+ export function failEmgrOpV2(id, reason) {
815
+ const j = _emgrgovJsV2.get(id);
816
+ if (!j) throw new Error(`emgrgov op ${id} not found`);
817
+ _emgrgovCheckJ(j.status, EMGRGOV_OP_LIFECYCLE_V2.FAILED);
818
+ const now = Date.now();
819
+ j.status = EMGRGOV_OP_LIFECYCLE_V2.FAILED;
820
+ j.updatedAt = now;
821
+ if (!j.settledAt) j.settledAt = now;
822
+ if (reason) j.metadata.failReason = String(reason);
823
+ return { ...j, metadata: { ...j.metadata } };
824
+ }
825
+ export function cancelEmgrOpV2(id, reason) {
826
+ const j = _emgrgovJsV2.get(id);
827
+ if (!j) throw new Error(`emgrgov op ${id} not found`);
828
+ _emgrgovCheckJ(j.status, EMGRGOV_OP_LIFECYCLE_V2.CANCELLED);
829
+ const now = Date.now();
830
+ j.status = EMGRGOV_OP_LIFECYCLE_V2.CANCELLED;
831
+ j.updatedAt = now;
832
+ if (!j.settledAt) j.settledAt = now;
833
+ if (reason) j.metadata.cancelReason = String(reason);
834
+ return { ...j, metadata: { ...j.metadata } };
835
+ }
836
+ export function getEmgrOpV2(id) {
837
+ const j = _emgrgovJsV2.get(id);
838
+ if (!j) return null;
839
+ return { ...j, metadata: { ...j.metadata } };
840
+ }
841
+ export function listEmgrOpsV2() {
842
+ return [..._emgrgovJsV2.values()].map((j) => ({
843
+ ...j,
844
+ metadata: { ...j.metadata },
845
+ }));
846
+ }
847
+ export function autoStaleIdleEmgrProfilesV2({ now } = {}) {
848
+ const t = now ?? Date.now();
849
+ const flipped = [];
850
+ for (const p of _emgrgovPsV2.values())
851
+ if (
852
+ p.status === EMGRGOV_PROFILE_MATURITY_V2.ACTIVE &&
853
+ t - p.lastTouchedAt >= _emgrgovIdleMs
854
+ ) {
855
+ p.status = EMGRGOV_PROFILE_MATURITY_V2.STALE;
856
+ p.updatedAt = t;
857
+ flipped.push(p.id);
858
+ }
859
+ return { flipped, count: flipped.length };
860
+ }
861
+ export function autoFailStuckEmgrOpsV2({ now } = {}) {
862
+ const t = now ?? Date.now();
863
+ const flipped = [];
864
+ for (const j of _emgrgovJsV2.values())
865
+ if (
866
+ j.status === EMGRGOV_OP_LIFECYCLE_V2.OPERATING &&
867
+ j.startedAt != null &&
868
+ t - j.startedAt >= _emgrgovStuckMs
869
+ ) {
870
+ j.status = EMGRGOV_OP_LIFECYCLE_V2.FAILED;
871
+ j.updatedAt = t;
872
+ if (!j.settledAt) j.settledAt = t;
873
+ j.metadata.failReason = "auto-fail-stuck";
874
+ flipped.push(j.id);
875
+ }
876
+ return { flipped, count: flipped.length };
877
+ }
878
+ export function getEmgrgovStatsV2() {
879
+ const profilesByStatus = {};
880
+ for (const v of Object.values(EMGRGOV_PROFILE_MATURITY_V2))
881
+ profilesByStatus[v] = 0;
882
+ for (const p of _emgrgovPsV2.values()) profilesByStatus[p.status]++;
883
+ const opsByStatus = {};
884
+ for (const v of Object.values(EMGRGOV_OP_LIFECYCLE_V2)) opsByStatus[v] = 0;
885
+ for (const j of _emgrgovJsV2.values()) opsByStatus[j.status]++;
886
+ return {
887
+ totalEmgrProfilesV2: _emgrgovPsV2.size,
888
+ totalEmgrOpsV2: _emgrgovJsV2.size,
889
+ maxActiveEmgrProfilesPerOwner: _emgrgovMaxActive,
890
+ maxPendingEmgrOpsPerProfile: _emgrgovMaxPending,
891
+ emgrgovProfileIdleMs: _emgrgovIdleMs,
892
+ emgrgovOpStuckMs: _emgrgovStuckMs,
893
+ profilesByStatus,
894
+ opsByStatus,
895
+ };
896
+ }
@@ -588,3 +588,333 @@ export function getExecutionBackendStatsV2() {
588
588
  jobsByStatus,
589
589
  };
590
590
  }
591
+
592
+ // =====================================================================
593
+ // execution-backend V2 governance overlay (iter25)
594
+ // =====================================================================
595
+ export const EBGOV_PROFILE_MATURITY_V2 = Object.freeze({
596
+ PENDING: "pending",
597
+ ACTIVE: "active",
598
+ DEGRADED: "degraded",
599
+ ARCHIVED: "archived",
600
+ });
601
+ export const EBGOV_JOB_LIFECYCLE_V2 = Object.freeze({
602
+ QUEUED: "queued",
603
+ EXECUTING: "executing",
604
+ SUCCEEDED: "succeeded",
605
+ FAILED: "failed",
606
+ CANCELLED: "cancelled",
607
+ });
608
+ const _ebgovPTrans = new Map([
609
+ [
610
+ EBGOV_PROFILE_MATURITY_V2.PENDING,
611
+ new Set([
612
+ EBGOV_PROFILE_MATURITY_V2.ACTIVE,
613
+ EBGOV_PROFILE_MATURITY_V2.ARCHIVED,
614
+ ]),
615
+ ],
616
+ [
617
+ EBGOV_PROFILE_MATURITY_V2.ACTIVE,
618
+ new Set([
619
+ EBGOV_PROFILE_MATURITY_V2.DEGRADED,
620
+ EBGOV_PROFILE_MATURITY_V2.ARCHIVED,
621
+ ]),
622
+ ],
623
+ [
624
+ EBGOV_PROFILE_MATURITY_V2.DEGRADED,
625
+ new Set([
626
+ EBGOV_PROFILE_MATURITY_V2.ACTIVE,
627
+ EBGOV_PROFILE_MATURITY_V2.ARCHIVED,
628
+ ]),
629
+ ],
630
+ [EBGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
631
+ ]);
632
+ const _ebgovPTerminal = new Set([EBGOV_PROFILE_MATURITY_V2.ARCHIVED]);
633
+ const _ebgovJTrans = new Map([
634
+ [
635
+ EBGOV_JOB_LIFECYCLE_V2.QUEUED,
636
+ new Set([
637
+ EBGOV_JOB_LIFECYCLE_V2.EXECUTING,
638
+ EBGOV_JOB_LIFECYCLE_V2.CANCELLED,
639
+ ]),
640
+ ],
641
+ [
642
+ EBGOV_JOB_LIFECYCLE_V2.EXECUTING,
643
+ new Set([
644
+ EBGOV_JOB_LIFECYCLE_V2.SUCCEEDED,
645
+ EBGOV_JOB_LIFECYCLE_V2.FAILED,
646
+ EBGOV_JOB_LIFECYCLE_V2.CANCELLED,
647
+ ]),
648
+ ],
649
+ [EBGOV_JOB_LIFECYCLE_V2.SUCCEEDED, new Set()],
650
+ [EBGOV_JOB_LIFECYCLE_V2.FAILED, new Set()],
651
+ [EBGOV_JOB_LIFECYCLE_V2.CANCELLED, new Set()],
652
+ ]);
653
+ const _ebgovPsV2 = new Map();
654
+ const _ebgovJsV2 = new Map();
655
+ let _ebgovMaxActive = 6,
656
+ _ebgovMaxPending = 15,
657
+ _ebgovIdleMs = 30 * 24 * 60 * 60 * 1000,
658
+ _ebgovStuckMs = 60 * 1000;
659
+ function _ebgovPos(n, label) {
660
+ const v = Math.floor(Number(n));
661
+ if (!Number.isFinite(v) || v <= 0)
662
+ throw new Error(`${label} must be positive integer`);
663
+ return v;
664
+ }
665
+ function _ebgovCheckP(from, to) {
666
+ const a = _ebgovPTrans.get(from);
667
+ if (!a || !a.has(to))
668
+ throw new Error(`invalid ebgov profile transition ${from} → ${to}`);
669
+ }
670
+ function _ebgovCheckJ(from, to) {
671
+ const a = _ebgovJTrans.get(from);
672
+ if (!a || !a.has(to))
673
+ throw new Error(`invalid ebgov job transition ${from} → ${to}`);
674
+ }
675
+ function _ebgovCountActive(owner) {
676
+ let c = 0;
677
+ for (const p of _ebgovPsV2.values())
678
+ if (p.owner === owner && p.status === EBGOV_PROFILE_MATURITY_V2.ACTIVE) c++;
679
+ return c;
680
+ }
681
+ function _ebgovCountPending(profileId) {
682
+ let c = 0;
683
+ for (const j of _ebgovJsV2.values())
684
+ if (
685
+ j.profileId === profileId &&
686
+ (j.status === EBGOV_JOB_LIFECYCLE_V2.QUEUED ||
687
+ j.status === EBGOV_JOB_LIFECYCLE_V2.EXECUTING)
688
+ )
689
+ c++;
690
+ return c;
691
+ }
692
+ export function setMaxActiveEbgovProfilesPerOwnerV2(n) {
693
+ _ebgovMaxActive = _ebgovPos(n, "maxActiveEbgovProfilesPerOwner");
694
+ }
695
+ export function getMaxActiveEbgovProfilesPerOwnerV2() {
696
+ return _ebgovMaxActive;
697
+ }
698
+ export function setMaxPendingEbgovJobsPerProfileV2(n) {
699
+ _ebgovMaxPending = _ebgovPos(n, "maxPendingEbgovJobsPerProfile");
700
+ }
701
+ export function getMaxPendingEbgovJobsPerProfileV2() {
702
+ return _ebgovMaxPending;
703
+ }
704
+ export function setEbgovProfileIdleMsV2(n) {
705
+ _ebgovIdleMs = _ebgovPos(n, "ebgovProfileIdleMs");
706
+ }
707
+ export function getEbgovProfileIdleMsV2() {
708
+ return _ebgovIdleMs;
709
+ }
710
+ export function setEbgovJobStuckMsV2(n) {
711
+ _ebgovStuckMs = _ebgovPos(n, "ebgovJobStuckMs");
712
+ }
713
+ export function getEbgovJobStuckMsV2() {
714
+ return _ebgovStuckMs;
715
+ }
716
+ export function _resetStateExecutionBackendGovV2() {
717
+ _ebgovPsV2.clear();
718
+ _ebgovJsV2.clear();
719
+ _ebgovMaxActive = 6;
720
+ _ebgovMaxPending = 15;
721
+ _ebgovIdleMs = 30 * 24 * 60 * 60 * 1000;
722
+ _ebgovStuckMs = 60 * 1000;
723
+ }
724
+ export function registerEbgovProfileV2({ id, owner, backend, metadata } = {}) {
725
+ if (!id || !owner) throw new Error("id and owner required");
726
+ if (_ebgovPsV2.has(id)) throw new Error(`ebgov profile ${id} already exists`);
727
+ const now = Date.now();
728
+ const p = {
729
+ id,
730
+ owner,
731
+ backend: backend || "local",
732
+ status: EBGOV_PROFILE_MATURITY_V2.PENDING,
733
+ createdAt: now,
734
+ updatedAt: now,
735
+ lastTouchedAt: now,
736
+ activatedAt: null,
737
+ archivedAt: null,
738
+ metadata: { ...(metadata || {}) },
739
+ };
740
+ _ebgovPsV2.set(id, p);
741
+ return { ...p, metadata: { ...p.metadata } };
742
+ }
743
+ export function activateEbgovProfileV2(id) {
744
+ const p = _ebgovPsV2.get(id);
745
+ if (!p) throw new Error(`ebgov profile ${id} not found`);
746
+ const isInitial = p.status === EBGOV_PROFILE_MATURITY_V2.PENDING;
747
+ _ebgovCheckP(p.status, EBGOV_PROFILE_MATURITY_V2.ACTIVE);
748
+ if (isInitial && _ebgovCountActive(p.owner) >= _ebgovMaxActive)
749
+ throw new Error(`max active ebgov profiles for owner ${p.owner} reached`);
750
+ const now = Date.now();
751
+ p.status = EBGOV_PROFILE_MATURITY_V2.ACTIVE;
752
+ p.updatedAt = now;
753
+ p.lastTouchedAt = now;
754
+ if (!p.activatedAt) p.activatedAt = now;
755
+ return { ...p, metadata: { ...p.metadata } };
756
+ }
757
+ export function degradeEbgovProfileV2(id) {
758
+ const p = _ebgovPsV2.get(id);
759
+ if (!p) throw new Error(`ebgov profile ${id} not found`);
760
+ _ebgovCheckP(p.status, EBGOV_PROFILE_MATURITY_V2.DEGRADED);
761
+ p.status = EBGOV_PROFILE_MATURITY_V2.DEGRADED;
762
+ p.updatedAt = Date.now();
763
+ return { ...p, metadata: { ...p.metadata } };
764
+ }
765
+ export function archiveEbgovProfileV2(id) {
766
+ const p = _ebgovPsV2.get(id);
767
+ if (!p) throw new Error(`ebgov profile ${id} not found`);
768
+ _ebgovCheckP(p.status, EBGOV_PROFILE_MATURITY_V2.ARCHIVED);
769
+ const now = Date.now();
770
+ p.status = EBGOV_PROFILE_MATURITY_V2.ARCHIVED;
771
+ p.updatedAt = now;
772
+ if (!p.archivedAt) p.archivedAt = now;
773
+ return { ...p, metadata: { ...p.metadata } };
774
+ }
775
+ export function touchEbgovProfileV2(id) {
776
+ const p = _ebgovPsV2.get(id);
777
+ if (!p) throw new Error(`ebgov profile ${id} not found`);
778
+ if (_ebgovPTerminal.has(p.status))
779
+ throw new Error(`cannot touch terminal ebgov profile ${id}`);
780
+ const now = Date.now();
781
+ p.lastTouchedAt = now;
782
+ p.updatedAt = now;
783
+ return { ...p, metadata: { ...p.metadata } };
784
+ }
785
+ export function getEbgovProfileV2(id) {
786
+ const p = _ebgovPsV2.get(id);
787
+ if (!p) return null;
788
+ return { ...p, metadata: { ...p.metadata } };
789
+ }
790
+ export function listEbgovProfilesV2() {
791
+ return [..._ebgovPsV2.values()].map((p) => ({
792
+ ...p,
793
+ metadata: { ...p.metadata },
794
+ }));
795
+ }
796
+ export function createEbgovJobV2({ id, profileId, task, metadata } = {}) {
797
+ if (!id || !profileId) throw new Error("id and profileId required");
798
+ if (_ebgovJsV2.has(id)) throw new Error(`ebgov job ${id} already exists`);
799
+ if (!_ebgovPsV2.has(profileId))
800
+ throw new Error(`ebgov profile ${profileId} not found`);
801
+ if (_ebgovCountPending(profileId) >= _ebgovMaxPending)
802
+ throw new Error(`max pending ebgov jobs for profile ${profileId} reached`);
803
+ const now = Date.now();
804
+ const j = {
805
+ id,
806
+ profileId,
807
+ task: task || "",
808
+ status: EBGOV_JOB_LIFECYCLE_V2.QUEUED,
809
+ createdAt: now,
810
+ updatedAt: now,
811
+ startedAt: null,
812
+ settledAt: null,
813
+ metadata: { ...(metadata || {}) },
814
+ };
815
+ _ebgovJsV2.set(id, j);
816
+ return { ...j, metadata: { ...j.metadata } };
817
+ }
818
+ export function executingEbgovJobV2(id) {
819
+ const j = _ebgovJsV2.get(id);
820
+ if (!j) throw new Error(`ebgov job ${id} not found`);
821
+ _ebgovCheckJ(j.status, EBGOV_JOB_LIFECYCLE_V2.EXECUTING);
822
+ const now = Date.now();
823
+ j.status = EBGOV_JOB_LIFECYCLE_V2.EXECUTING;
824
+ j.updatedAt = now;
825
+ if (!j.startedAt) j.startedAt = now;
826
+ return { ...j, metadata: { ...j.metadata } };
827
+ }
828
+ export function completeJobEbgovV2(id) {
829
+ const j = _ebgovJsV2.get(id);
830
+ if (!j) throw new Error(`ebgov job ${id} not found`);
831
+ _ebgovCheckJ(j.status, EBGOV_JOB_LIFECYCLE_V2.SUCCEEDED);
832
+ const now = Date.now();
833
+ j.status = EBGOV_JOB_LIFECYCLE_V2.SUCCEEDED;
834
+ j.updatedAt = now;
835
+ if (!j.settledAt) j.settledAt = now;
836
+ return { ...j, metadata: { ...j.metadata } };
837
+ }
838
+ export function failEbgovJobV2(id, reason) {
839
+ const j = _ebgovJsV2.get(id);
840
+ if (!j) throw new Error(`ebgov job ${id} not found`);
841
+ _ebgovCheckJ(j.status, EBGOV_JOB_LIFECYCLE_V2.FAILED);
842
+ const now = Date.now();
843
+ j.status = EBGOV_JOB_LIFECYCLE_V2.FAILED;
844
+ j.updatedAt = now;
845
+ if (!j.settledAt) j.settledAt = now;
846
+ if (reason) j.metadata.failReason = String(reason);
847
+ return { ...j, metadata: { ...j.metadata } };
848
+ }
849
+ export function cancelEbgovJobV2(id, reason) {
850
+ const j = _ebgovJsV2.get(id);
851
+ if (!j) throw new Error(`ebgov job ${id} not found`);
852
+ _ebgovCheckJ(j.status, EBGOV_JOB_LIFECYCLE_V2.CANCELLED);
853
+ const now = Date.now();
854
+ j.status = EBGOV_JOB_LIFECYCLE_V2.CANCELLED;
855
+ j.updatedAt = now;
856
+ if (!j.settledAt) j.settledAt = now;
857
+ if (reason) j.metadata.cancelReason = String(reason);
858
+ return { ...j, metadata: { ...j.metadata } };
859
+ }
860
+ export function getEbgovJobV2(id) {
861
+ const j = _ebgovJsV2.get(id);
862
+ if (!j) return null;
863
+ return { ...j, metadata: { ...j.metadata } };
864
+ }
865
+ export function listEbgovJobsV2() {
866
+ return [..._ebgovJsV2.values()].map((j) => ({
867
+ ...j,
868
+ metadata: { ...j.metadata },
869
+ }));
870
+ }
871
+ export function autoDegradeIdleEbgovProfilesV2({ now } = {}) {
872
+ const t = now ?? Date.now();
873
+ const flipped = [];
874
+ for (const p of _ebgovPsV2.values())
875
+ if (
876
+ p.status === EBGOV_PROFILE_MATURITY_V2.ACTIVE &&
877
+ t - p.lastTouchedAt >= _ebgovIdleMs
878
+ ) {
879
+ p.status = EBGOV_PROFILE_MATURITY_V2.DEGRADED;
880
+ p.updatedAt = t;
881
+ flipped.push(p.id);
882
+ }
883
+ return { flipped, count: flipped.length };
884
+ }
885
+ export function autoFailStuckEbgovJobsV2({ now } = {}) {
886
+ const t = now ?? Date.now();
887
+ const flipped = [];
888
+ for (const j of _ebgovJsV2.values())
889
+ if (
890
+ j.status === EBGOV_JOB_LIFECYCLE_V2.EXECUTING &&
891
+ j.startedAt != null &&
892
+ t - j.startedAt >= _ebgovStuckMs
893
+ ) {
894
+ j.status = EBGOV_JOB_LIFECYCLE_V2.FAILED;
895
+ j.updatedAt = t;
896
+ if (!j.settledAt) j.settledAt = t;
897
+ j.metadata.failReason = "auto-fail-stuck";
898
+ flipped.push(j.id);
899
+ }
900
+ return { flipped, count: flipped.length };
901
+ }
902
+ export function getExecutionBackendGovStatsV2() {
903
+ const profilesByStatus = {};
904
+ for (const v of Object.values(EBGOV_PROFILE_MATURITY_V2))
905
+ profilesByStatus[v] = 0;
906
+ for (const p of _ebgovPsV2.values()) profilesByStatus[p.status]++;
907
+ const jobsByStatus = {};
908
+ for (const v of Object.values(EBGOV_JOB_LIFECYCLE_V2)) jobsByStatus[v] = 0;
909
+ for (const j of _ebgovJsV2.values()) jobsByStatus[j.status]++;
910
+ return {
911
+ totalEbgovProfilesV2: _ebgovPsV2.size,
912
+ totalEbgovJobsV2: _ebgovJsV2.size,
913
+ maxActiveEbgovProfilesPerOwner: _ebgovMaxActive,
914
+ maxPendingEbgovJobsPerProfile: _ebgovMaxPending,
915
+ ebgovProfileIdleMs: _ebgovIdleMs,
916
+ ebgovJobStuckMs: _ebgovStuckMs,
917
+ profilesByStatus,
918
+ jobsByStatus,
919
+ };
920
+ }