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.
- package/README.md +52 -3
- package/package.json +1 -1
- package/src/commands/a2a.js +201 -0
- package/src/commands/agent.js +1250 -0
- package/src/commands/chat.js +605 -0
- package/src/commands/cli-anything.js +426 -0
- package/src/commands/compliance.js +412 -0
- package/src/commands/config.js +213 -0
- package/src/commands/cowork.js +1463 -0
- package/src/commands/crosschain.js +203 -0
- package/src/commands/dao.js +203 -0
- package/src/commands/economy.js +199 -0
- package/src/commands/encrypt.js +201 -0
- package/src/commands/evolution.js +199 -0
- package/src/commands/evomap.js +625 -0
- package/src/commands/hmemory.js +203 -0
- package/src/commands/inference.js +207 -0
- package/src/commands/kg.js +195 -0
- package/src/commands/llm.js +209 -0
- package/src/commands/memory.js +203 -0
- package/src/commands/orchestrate.js +406 -0
- package/src/commands/pipeline.js +199 -0
- package/src/commands/planmode.js +426 -0
- package/src/commands/plugin.js +209 -0
- package/src/commands/services.js +207 -0
- package/src/commands/setup.js +205 -0
- package/src/commands/skill.js +207 -0
- package/src/commands/start.js +209 -0
- package/src/commands/stream.js +213 -0
- package/src/commands/ui.js +225 -0
- package/src/commands/workflow.js +209 -0
- package/src/index.js +112 -0
- package/src/lib/a2a-protocol.js +332 -0
- package/src/lib/agent-coordinator.js +334 -0
- package/src/lib/agent-economy.js +334 -0
- package/src/lib/agent-router.js +333 -0
- package/src/lib/autonomous-agent.js +332 -0
- package/src/lib/chat-core.js +335 -0
- package/src/lib/cli-anything-bridge.js +341 -0
- package/src/lib/cli-context-engineering.js +351 -0
- package/src/lib/compliance-manager.js +334 -0
- package/src/lib/cowork-adapter.js +336 -0
- package/src/lib/cowork-evomap-adapter.js +341 -0
- package/src/lib/cowork-mcp-tools.js +341 -0
- package/src/lib/cowork-observe-html.js +341 -0
- package/src/lib/cowork-observe.js +341 -0
- package/src/lib/cowork-task-templates.js +342 -1
- package/src/lib/cowork-template-marketplace.js +340 -0
- package/src/lib/cross-chain.js +339 -0
- package/src/lib/crypto-manager.js +334 -0
- package/src/lib/dao-governance.js +339 -0
- package/src/lib/downloader.js +334 -0
- package/src/lib/evolution-system.js +334 -0
- package/src/lib/evomap-client.js +342 -0
- package/src/lib/evomap-federation.js +338 -0
- package/src/lib/evomap-manager.js +330 -0
- package/src/lib/execution-backend.js +330 -0
- package/src/lib/hashline.js +338 -0
- package/src/lib/hierarchical-memory.js +334 -0
- package/src/lib/inference-network.js +341 -0
- package/src/lib/interaction-adapter.js +330 -0
- package/src/lib/interactive-planner.js +354 -0
- package/src/lib/knowledge-graph.js +331 -0
- package/src/lib/pipeline-orchestrator.js +332 -0
- package/src/lib/plan-mode.js +336 -0
- package/src/lib/plugin-autodiscovery.js +334 -0
- package/src/lib/process-manager.js +336 -0
- package/src/lib/provider-options.js +346 -0
- package/src/lib/provider-stream.js +348 -0
- package/src/lib/service-manager.js +337 -0
- package/src/lib/session-core-singletons.js +341 -0
- package/src/lib/skill-mcp.js +336 -0
- package/src/lib/stix-parser.js +346 -0
- package/src/lib/sub-agent-context.js +343 -0
- package/src/lib/sub-agent-profiles.js +335 -0
- package/src/lib/sub-agent-registry.js +336 -0
- package/src/lib/todo-manager.js +336 -0
- package/src/lib/web-ui-server.js +348 -0
- package/src/lib/workflow-expr.js +346 -0
- package/src/lib/ws-chat-handler.js +337 -0
package/src/lib/chat-core.js
CHANGED
|
@@ -688,3 +688,338 @@ export function getChatCoreGovStatsV2() {
|
|
|
688
688
|
messagesByStatus,
|
|
689
689
|
};
|
|
690
690
|
}
|
|
691
|
+
|
|
692
|
+
// === Iter28 V2 governance overlay: Ccoregov ===
|
|
693
|
+
export const CCOREGOV_PROFILE_MATURITY_V2 = Object.freeze({
|
|
694
|
+
PENDING: "pending",
|
|
695
|
+
ACTIVE: "active",
|
|
696
|
+
IDLE: "idle",
|
|
697
|
+
ARCHIVED: "archived",
|
|
698
|
+
});
|
|
699
|
+
export const CCOREGOV_MSG_LIFECYCLE_V2 = Object.freeze({
|
|
700
|
+
QUEUED: "queued",
|
|
701
|
+
SENDING: "sending",
|
|
702
|
+
SENT: "sent",
|
|
703
|
+
FAILED: "failed",
|
|
704
|
+
CANCELLED: "cancelled",
|
|
705
|
+
});
|
|
706
|
+
const _ccoregovPTrans = new Map([
|
|
707
|
+
[
|
|
708
|
+
CCOREGOV_PROFILE_MATURITY_V2.PENDING,
|
|
709
|
+
new Set([
|
|
710
|
+
CCOREGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
711
|
+
CCOREGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
712
|
+
]),
|
|
713
|
+
],
|
|
714
|
+
[
|
|
715
|
+
CCOREGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
716
|
+
new Set([
|
|
717
|
+
CCOREGOV_PROFILE_MATURITY_V2.IDLE,
|
|
718
|
+
CCOREGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
719
|
+
]),
|
|
720
|
+
],
|
|
721
|
+
[
|
|
722
|
+
CCOREGOV_PROFILE_MATURITY_V2.IDLE,
|
|
723
|
+
new Set([
|
|
724
|
+
CCOREGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
725
|
+
CCOREGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
726
|
+
]),
|
|
727
|
+
],
|
|
728
|
+
[CCOREGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
|
|
729
|
+
]);
|
|
730
|
+
const _ccoregovPTerminal = new Set([CCOREGOV_PROFILE_MATURITY_V2.ARCHIVED]);
|
|
731
|
+
const _ccoregovJTrans = new Map([
|
|
732
|
+
[
|
|
733
|
+
CCOREGOV_MSG_LIFECYCLE_V2.QUEUED,
|
|
734
|
+
new Set([
|
|
735
|
+
CCOREGOV_MSG_LIFECYCLE_V2.SENDING,
|
|
736
|
+
CCOREGOV_MSG_LIFECYCLE_V2.CANCELLED,
|
|
737
|
+
]),
|
|
738
|
+
],
|
|
739
|
+
[
|
|
740
|
+
CCOREGOV_MSG_LIFECYCLE_V2.SENDING,
|
|
741
|
+
new Set([
|
|
742
|
+
CCOREGOV_MSG_LIFECYCLE_V2.SENT,
|
|
743
|
+
CCOREGOV_MSG_LIFECYCLE_V2.FAILED,
|
|
744
|
+
CCOREGOV_MSG_LIFECYCLE_V2.CANCELLED,
|
|
745
|
+
]),
|
|
746
|
+
],
|
|
747
|
+
[CCOREGOV_MSG_LIFECYCLE_V2.SENT, new Set()],
|
|
748
|
+
[CCOREGOV_MSG_LIFECYCLE_V2.FAILED, new Set()],
|
|
749
|
+
[CCOREGOV_MSG_LIFECYCLE_V2.CANCELLED, new Set()],
|
|
750
|
+
]);
|
|
751
|
+
const _ccoregovPsV2 = new Map();
|
|
752
|
+
const _ccoregovJsV2 = new Map();
|
|
753
|
+
let _ccoregovMaxActive = 10,
|
|
754
|
+
_ccoregovMaxPending = 25,
|
|
755
|
+
_ccoregovIdleMs = 2592000000,
|
|
756
|
+
_ccoregovStuckMs = 60 * 1000;
|
|
757
|
+
function _ccoregovPos(n, label) {
|
|
758
|
+
const v = Math.floor(Number(n));
|
|
759
|
+
if (!Number.isFinite(v) || v <= 0)
|
|
760
|
+
throw new Error(`${label} must be positive integer`);
|
|
761
|
+
return v;
|
|
762
|
+
}
|
|
763
|
+
function _ccoregovCheckP(from, to) {
|
|
764
|
+
const a = _ccoregovPTrans.get(from);
|
|
765
|
+
if (!a || !a.has(to))
|
|
766
|
+
throw new Error(`invalid ccoregov profile transition ${from} → ${to}`);
|
|
767
|
+
}
|
|
768
|
+
function _ccoregovCheckJ(from, to) {
|
|
769
|
+
const a = _ccoregovJTrans.get(from);
|
|
770
|
+
if (!a || !a.has(to))
|
|
771
|
+
throw new Error(`invalid ccoregov msg transition ${from} → ${to}`);
|
|
772
|
+
}
|
|
773
|
+
function _ccoregovCountActive(owner) {
|
|
774
|
+
let c = 0;
|
|
775
|
+
for (const p of _ccoregovPsV2.values())
|
|
776
|
+
if (p.owner === owner && p.status === CCOREGOV_PROFILE_MATURITY_V2.ACTIVE)
|
|
777
|
+
c++;
|
|
778
|
+
return c;
|
|
779
|
+
}
|
|
780
|
+
function _ccoregovCountPending(profileId) {
|
|
781
|
+
let c = 0;
|
|
782
|
+
for (const j of _ccoregovJsV2.values())
|
|
783
|
+
if (
|
|
784
|
+
j.profileId === profileId &&
|
|
785
|
+
(j.status === CCOREGOV_MSG_LIFECYCLE_V2.QUEUED ||
|
|
786
|
+
j.status === CCOREGOV_MSG_LIFECYCLE_V2.SENDING)
|
|
787
|
+
)
|
|
788
|
+
c++;
|
|
789
|
+
return c;
|
|
790
|
+
}
|
|
791
|
+
export function setMaxActiveCcoreProfilesPerOwnerV2(n) {
|
|
792
|
+
_ccoregovMaxActive = _ccoregovPos(n, "maxActiveCcoreProfilesPerOwner");
|
|
793
|
+
}
|
|
794
|
+
export function getMaxActiveCcoreProfilesPerOwnerV2() {
|
|
795
|
+
return _ccoregovMaxActive;
|
|
796
|
+
}
|
|
797
|
+
export function setMaxPendingCcoreMsgsPerProfileV2(n) {
|
|
798
|
+
_ccoregovMaxPending = _ccoregovPos(n, "maxPendingCcoreMsgsPerProfile");
|
|
799
|
+
}
|
|
800
|
+
export function getMaxPendingCcoreMsgsPerProfileV2() {
|
|
801
|
+
return _ccoregovMaxPending;
|
|
802
|
+
}
|
|
803
|
+
export function setCcoreProfileIdleMsV2(n) {
|
|
804
|
+
_ccoregovIdleMs = _ccoregovPos(n, "ccoregovProfileIdleMs");
|
|
805
|
+
}
|
|
806
|
+
export function getCcoreProfileIdleMsV2() {
|
|
807
|
+
return _ccoregovIdleMs;
|
|
808
|
+
}
|
|
809
|
+
export function setCcoreMsgStuckMsV2(n) {
|
|
810
|
+
_ccoregovStuckMs = _ccoregovPos(n, "ccoregovMsgStuckMs");
|
|
811
|
+
}
|
|
812
|
+
export function getCcoreMsgStuckMsV2() {
|
|
813
|
+
return _ccoregovStuckMs;
|
|
814
|
+
}
|
|
815
|
+
export function _resetStateCcoregovV2() {
|
|
816
|
+
_ccoregovPsV2.clear();
|
|
817
|
+
_ccoregovJsV2.clear();
|
|
818
|
+
_ccoregovMaxActive = 10;
|
|
819
|
+
_ccoregovMaxPending = 25;
|
|
820
|
+
_ccoregovIdleMs = 2592000000;
|
|
821
|
+
_ccoregovStuckMs = 60 * 1000;
|
|
822
|
+
}
|
|
823
|
+
export function registerCcoreProfileV2({ id, owner, channel, metadata } = {}) {
|
|
824
|
+
if (!id || !owner) throw new Error("id and owner required");
|
|
825
|
+
if (_ccoregovPsV2.has(id))
|
|
826
|
+
throw new Error(`ccoregov profile ${id} already exists`);
|
|
827
|
+
const now = Date.now();
|
|
828
|
+
const p = {
|
|
829
|
+
id,
|
|
830
|
+
owner,
|
|
831
|
+
channel: channel || "default",
|
|
832
|
+
status: CCOREGOV_PROFILE_MATURITY_V2.PENDING,
|
|
833
|
+
createdAt: now,
|
|
834
|
+
updatedAt: now,
|
|
835
|
+
lastTouchedAt: now,
|
|
836
|
+
activatedAt: null,
|
|
837
|
+
archivedAt: null,
|
|
838
|
+
metadata: { ...(metadata || {}) },
|
|
839
|
+
};
|
|
840
|
+
_ccoregovPsV2.set(id, p);
|
|
841
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
842
|
+
}
|
|
843
|
+
export function activateCcoreProfileV2(id) {
|
|
844
|
+
const p = _ccoregovPsV2.get(id);
|
|
845
|
+
if (!p) throw new Error(`ccoregov profile ${id} not found`);
|
|
846
|
+
const isInitial = p.status === CCOREGOV_PROFILE_MATURITY_V2.PENDING;
|
|
847
|
+
_ccoregovCheckP(p.status, CCOREGOV_PROFILE_MATURITY_V2.ACTIVE);
|
|
848
|
+
if (isInitial && _ccoregovCountActive(p.owner) >= _ccoregovMaxActive)
|
|
849
|
+
throw new Error(
|
|
850
|
+
`max active ccoregov profiles for owner ${p.owner} reached`,
|
|
851
|
+
);
|
|
852
|
+
const now = Date.now();
|
|
853
|
+
p.status = CCOREGOV_PROFILE_MATURITY_V2.ACTIVE;
|
|
854
|
+
p.updatedAt = now;
|
|
855
|
+
p.lastTouchedAt = now;
|
|
856
|
+
if (!p.activatedAt) p.activatedAt = now;
|
|
857
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
858
|
+
}
|
|
859
|
+
export function idleCcoreProfileV2(id) {
|
|
860
|
+
const p = _ccoregovPsV2.get(id);
|
|
861
|
+
if (!p) throw new Error(`ccoregov profile ${id} not found`);
|
|
862
|
+
_ccoregovCheckP(p.status, CCOREGOV_PROFILE_MATURITY_V2.IDLE);
|
|
863
|
+
p.status = CCOREGOV_PROFILE_MATURITY_V2.IDLE;
|
|
864
|
+
p.updatedAt = Date.now();
|
|
865
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
866
|
+
}
|
|
867
|
+
export function archiveCcoreProfileV2(id) {
|
|
868
|
+
const p = _ccoregovPsV2.get(id);
|
|
869
|
+
if (!p) throw new Error(`ccoregov profile ${id} not found`);
|
|
870
|
+
_ccoregovCheckP(p.status, CCOREGOV_PROFILE_MATURITY_V2.ARCHIVED);
|
|
871
|
+
const now = Date.now();
|
|
872
|
+
p.status = CCOREGOV_PROFILE_MATURITY_V2.ARCHIVED;
|
|
873
|
+
p.updatedAt = now;
|
|
874
|
+
if (!p.archivedAt) p.archivedAt = now;
|
|
875
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
876
|
+
}
|
|
877
|
+
export function touchCcoreProfileV2(id) {
|
|
878
|
+
const p = _ccoregovPsV2.get(id);
|
|
879
|
+
if (!p) throw new Error(`ccoregov profile ${id} not found`);
|
|
880
|
+
if (_ccoregovPTerminal.has(p.status))
|
|
881
|
+
throw new Error(`cannot touch terminal ccoregov profile ${id}`);
|
|
882
|
+
const now = Date.now();
|
|
883
|
+
p.lastTouchedAt = now;
|
|
884
|
+
p.updatedAt = now;
|
|
885
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
886
|
+
}
|
|
887
|
+
export function getCcoreProfileV2(id) {
|
|
888
|
+
const p = _ccoregovPsV2.get(id);
|
|
889
|
+
if (!p) return null;
|
|
890
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
891
|
+
}
|
|
892
|
+
export function listCcoreProfilesV2() {
|
|
893
|
+
return [..._ccoregovPsV2.values()].map((p) => ({
|
|
894
|
+
...p,
|
|
895
|
+
metadata: { ...p.metadata },
|
|
896
|
+
}));
|
|
897
|
+
}
|
|
898
|
+
export function createCcoreMsgV2({ id, profileId, messageId, metadata } = {}) {
|
|
899
|
+
if (!id || !profileId) throw new Error("id and profileId required");
|
|
900
|
+
if (_ccoregovJsV2.has(id))
|
|
901
|
+
throw new Error(`ccoregov msg ${id} already exists`);
|
|
902
|
+
if (!_ccoregovPsV2.has(profileId))
|
|
903
|
+
throw new Error(`ccoregov profile ${profileId} not found`);
|
|
904
|
+
if (_ccoregovCountPending(profileId) >= _ccoregovMaxPending)
|
|
905
|
+
throw new Error(
|
|
906
|
+
`max pending ccoregov msgs for profile ${profileId} reached`,
|
|
907
|
+
);
|
|
908
|
+
const now = Date.now();
|
|
909
|
+
const j = {
|
|
910
|
+
id,
|
|
911
|
+
profileId,
|
|
912
|
+
messageId: messageId || "",
|
|
913
|
+
status: CCOREGOV_MSG_LIFECYCLE_V2.QUEUED,
|
|
914
|
+
createdAt: now,
|
|
915
|
+
updatedAt: now,
|
|
916
|
+
startedAt: null,
|
|
917
|
+
settledAt: null,
|
|
918
|
+
metadata: { ...(metadata || {}) },
|
|
919
|
+
};
|
|
920
|
+
_ccoregovJsV2.set(id, j);
|
|
921
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
922
|
+
}
|
|
923
|
+
export function sendingCcoreMsgV2(id) {
|
|
924
|
+
const j = _ccoregovJsV2.get(id);
|
|
925
|
+
if (!j) throw new Error(`ccoregov msg ${id} not found`);
|
|
926
|
+
_ccoregovCheckJ(j.status, CCOREGOV_MSG_LIFECYCLE_V2.SENDING);
|
|
927
|
+
const now = Date.now();
|
|
928
|
+
j.status = CCOREGOV_MSG_LIFECYCLE_V2.SENDING;
|
|
929
|
+
j.updatedAt = now;
|
|
930
|
+
if (!j.startedAt) j.startedAt = now;
|
|
931
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
932
|
+
}
|
|
933
|
+
export function completeMsgCcoreV2(id) {
|
|
934
|
+
const j = _ccoregovJsV2.get(id);
|
|
935
|
+
if (!j) throw new Error(`ccoregov msg ${id} not found`);
|
|
936
|
+
_ccoregovCheckJ(j.status, CCOREGOV_MSG_LIFECYCLE_V2.SENT);
|
|
937
|
+
const now = Date.now();
|
|
938
|
+
j.status = CCOREGOV_MSG_LIFECYCLE_V2.SENT;
|
|
939
|
+
j.updatedAt = now;
|
|
940
|
+
if (!j.settledAt) j.settledAt = now;
|
|
941
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
942
|
+
}
|
|
943
|
+
export function failCcoreMsgV2(id, reason) {
|
|
944
|
+
const j = _ccoregovJsV2.get(id);
|
|
945
|
+
if (!j) throw new Error(`ccoregov msg ${id} not found`);
|
|
946
|
+
_ccoregovCheckJ(j.status, CCOREGOV_MSG_LIFECYCLE_V2.FAILED);
|
|
947
|
+
const now = Date.now();
|
|
948
|
+
j.status = CCOREGOV_MSG_LIFECYCLE_V2.FAILED;
|
|
949
|
+
j.updatedAt = now;
|
|
950
|
+
if (!j.settledAt) j.settledAt = now;
|
|
951
|
+
if (reason) j.metadata.failReason = String(reason);
|
|
952
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
953
|
+
}
|
|
954
|
+
export function cancelCcoreMsgV2(id, reason) {
|
|
955
|
+
const j = _ccoregovJsV2.get(id);
|
|
956
|
+
if (!j) throw new Error(`ccoregov msg ${id} not found`);
|
|
957
|
+
_ccoregovCheckJ(j.status, CCOREGOV_MSG_LIFECYCLE_V2.CANCELLED);
|
|
958
|
+
const now = Date.now();
|
|
959
|
+
j.status = CCOREGOV_MSG_LIFECYCLE_V2.CANCELLED;
|
|
960
|
+
j.updatedAt = now;
|
|
961
|
+
if (!j.settledAt) j.settledAt = now;
|
|
962
|
+
if (reason) j.metadata.cancelReason = String(reason);
|
|
963
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
964
|
+
}
|
|
965
|
+
export function getCcoreMsgV2(id) {
|
|
966
|
+
const j = _ccoregovJsV2.get(id);
|
|
967
|
+
if (!j) return null;
|
|
968
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
969
|
+
}
|
|
970
|
+
export function listCcoreMsgsV2() {
|
|
971
|
+
return [..._ccoregovJsV2.values()].map((j) => ({
|
|
972
|
+
...j,
|
|
973
|
+
metadata: { ...j.metadata },
|
|
974
|
+
}));
|
|
975
|
+
}
|
|
976
|
+
export function autoIdleIdleCcoreProfilesV2({ now } = {}) {
|
|
977
|
+
const t = now ?? Date.now();
|
|
978
|
+
const flipped = [];
|
|
979
|
+
for (const p of _ccoregovPsV2.values())
|
|
980
|
+
if (
|
|
981
|
+
p.status === CCOREGOV_PROFILE_MATURITY_V2.ACTIVE &&
|
|
982
|
+
t - p.lastTouchedAt >= _ccoregovIdleMs
|
|
983
|
+
) {
|
|
984
|
+
p.status = CCOREGOV_PROFILE_MATURITY_V2.IDLE;
|
|
985
|
+
p.updatedAt = t;
|
|
986
|
+
flipped.push(p.id);
|
|
987
|
+
}
|
|
988
|
+
return { flipped, count: flipped.length };
|
|
989
|
+
}
|
|
990
|
+
export function autoFailStuckCcoreMsgsV2({ now } = {}) {
|
|
991
|
+
const t = now ?? Date.now();
|
|
992
|
+
const flipped = [];
|
|
993
|
+
for (const j of _ccoregovJsV2.values())
|
|
994
|
+
if (
|
|
995
|
+
j.status === CCOREGOV_MSG_LIFECYCLE_V2.SENDING &&
|
|
996
|
+
j.startedAt != null &&
|
|
997
|
+
t - j.startedAt >= _ccoregovStuckMs
|
|
998
|
+
) {
|
|
999
|
+
j.status = CCOREGOV_MSG_LIFECYCLE_V2.FAILED;
|
|
1000
|
+
j.updatedAt = t;
|
|
1001
|
+
if (!j.settledAt) j.settledAt = t;
|
|
1002
|
+
j.metadata.failReason = "auto-fail-stuck";
|
|
1003
|
+
flipped.push(j.id);
|
|
1004
|
+
}
|
|
1005
|
+
return { flipped, count: flipped.length };
|
|
1006
|
+
}
|
|
1007
|
+
export function getCcoregovStatsV2() {
|
|
1008
|
+
const profilesByStatus = {};
|
|
1009
|
+
for (const v of Object.values(CCOREGOV_PROFILE_MATURITY_V2))
|
|
1010
|
+
profilesByStatus[v] = 0;
|
|
1011
|
+
for (const p of _ccoregovPsV2.values()) profilesByStatus[p.status]++;
|
|
1012
|
+
const msgsByStatus = {};
|
|
1013
|
+
for (const v of Object.values(CCOREGOV_MSG_LIFECYCLE_V2)) msgsByStatus[v] = 0;
|
|
1014
|
+
for (const j of _ccoregovJsV2.values()) msgsByStatus[j.status]++;
|
|
1015
|
+
return {
|
|
1016
|
+
totalCcoreProfilesV2: _ccoregovPsV2.size,
|
|
1017
|
+
totalCcoreMsgsV2: _ccoregovJsV2.size,
|
|
1018
|
+
maxActiveCcoreProfilesPerOwner: _ccoregovMaxActive,
|
|
1019
|
+
maxPendingCcoreMsgsPerProfile: _ccoregovMaxPending,
|
|
1020
|
+
ccoregovProfileIdleMs: _ccoregovIdleMs,
|
|
1021
|
+
ccoregovMsgStuckMs: _ccoregovStuckMs,
|
|
1022
|
+
profilesByStatus,
|
|
1023
|
+
msgsByStatus,
|
|
1024
|
+
};
|
|
1025
|
+
}
|
|
@@ -377,3 +377,344 @@ export function listTools(db) {
|
|
|
377
377
|
)
|
|
378
378
|
.all();
|
|
379
379
|
}
|
|
380
|
+
|
|
381
|
+
// =====================================================================
|
|
382
|
+
// cli-anything-bridge V2 governance overlay (iter25)
|
|
383
|
+
// =====================================================================
|
|
384
|
+
export const CLIBGOV_PROFILE_MATURITY_V2 = Object.freeze({
|
|
385
|
+
PENDING: "pending",
|
|
386
|
+
ACTIVE: "active",
|
|
387
|
+
DEGRADED: "degraded",
|
|
388
|
+
ARCHIVED: "archived",
|
|
389
|
+
});
|
|
390
|
+
export const CLIBGOV_BRIDGE_LIFECYCLE_V2 = Object.freeze({
|
|
391
|
+
QUEUED: "queued",
|
|
392
|
+
BRIDGING: "bridging",
|
|
393
|
+
BRIDGED: "bridged",
|
|
394
|
+
FAILED: "failed",
|
|
395
|
+
CANCELLED: "cancelled",
|
|
396
|
+
});
|
|
397
|
+
const _clibgovPTrans = new Map([
|
|
398
|
+
[
|
|
399
|
+
CLIBGOV_PROFILE_MATURITY_V2.PENDING,
|
|
400
|
+
new Set([
|
|
401
|
+
CLIBGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
402
|
+
CLIBGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
403
|
+
]),
|
|
404
|
+
],
|
|
405
|
+
[
|
|
406
|
+
CLIBGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
407
|
+
new Set([
|
|
408
|
+
CLIBGOV_PROFILE_MATURITY_V2.DEGRADED,
|
|
409
|
+
CLIBGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
410
|
+
]),
|
|
411
|
+
],
|
|
412
|
+
[
|
|
413
|
+
CLIBGOV_PROFILE_MATURITY_V2.DEGRADED,
|
|
414
|
+
new Set([
|
|
415
|
+
CLIBGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
416
|
+
CLIBGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
417
|
+
]),
|
|
418
|
+
],
|
|
419
|
+
[CLIBGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
|
|
420
|
+
]);
|
|
421
|
+
const _clibgovPTerminal = new Set([CLIBGOV_PROFILE_MATURITY_V2.ARCHIVED]);
|
|
422
|
+
const _clibgovJTrans = new Map([
|
|
423
|
+
[
|
|
424
|
+
CLIBGOV_BRIDGE_LIFECYCLE_V2.QUEUED,
|
|
425
|
+
new Set([
|
|
426
|
+
CLIBGOV_BRIDGE_LIFECYCLE_V2.BRIDGING,
|
|
427
|
+
CLIBGOV_BRIDGE_LIFECYCLE_V2.CANCELLED,
|
|
428
|
+
]),
|
|
429
|
+
],
|
|
430
|
+
[
|
|
431
|
+
CLIBGOV_BRIDGE_LIFECYCLE_V2.BRIDGING,
|
|
432
|
+
new Set([
|
|
433
|
+
CLIBGOV_BRIDGE_LIFECYCLE_V2.BRIDGED,
|
|
434
|
+
CLIBGOV_BRIDGE_LIFECYCLE_V2.FAILED,
|
|
435
|
+
CLIBGOV_BRIDGE_LIFECYCLE_V2.CANCELLED,
|
|
436
|
+
]),
|
|
437
|
+
],
|
|
438
|
+
[CLIBGOV_BRIDGE_LIFECYCLE_V2.BRIDGED, new Set()],
|
|
439
|
+
[CLIBGOV_BRIDGE_LIFECYCLE_V2.FAILED, new Set()],
|
|
440
|
+
[CLIBGOV_BRIDGE_LIFECYCLE_V2.CANCELLED, new Set()],
|
|
441
|
+
]);
|
|
442
|
+
const _clibgovPsV2 = new Map();
|
|
443
|
+
const _clibgovJsV2 = new Map();
|
|
444
|
+
let _clibgovMaxActive = 8,
|
|
445
|
+
_clibgovMaxPending = 20,
|
|
446
|
+
_clibgovIdleMs = 30 * 24 * 60 * 60 * 1000,
|
|
447
|
+
_clibgovStuckMs = 60 * 1000;
|
|
448
|
+
function _clibgovPos(n, label) {
|
|
449
|
+
const v = Math.floor(Number(n));
|
|
450
|
+
if (!Number.isFinite(v) || v <= 0)
|
|
451
|
+
throw new Error(`${label} must be positive integer`);
|
|
452
|
+
return v;
|
|
453
|
+
}
|
|
454
|
+
function _clibgovCheckP(from, to) {
|
|
455
|
+
const a = _clibgovPTrans.get(from);
|
|
456
|
+
if (!a || !a.has(to))
|
|
457
|
+
throw new Error(`invalid clibgov profile transition ${from} → ${to}`);
|
|
458
|
+
}
|
|
459
|
+
function _clibgovCheckJ(from, to) {
|
|
460
|
+
const a = _clibgovJTrans.get(from);
|
|
461
|
+
if (!a || !a.has(to))
|
|
462
|
+
throw new Error(`invalid clibgov bridge transition ${from} → ${to}`);
|
|
463
|
+
}
|
|
464
|
+
function _clibgovCountActive(owner) {
|
|
465
|
+
let c = 0;
|
|
466
|
+
for (const p of _clibgovPsV2.values())
|
|
467
|
+
if (p.owner === owner && p.status === CLIBGOV_PROFILE_MATURITY_V2.ACTIVE)
|
|
468
|
+
c++;
|
|
469
|
+
return c;
|
|
470
|
+
}
|
|
471
|
+
function _clibgovCountPending(profileId) {
|
|
472
|
+
let c = 0;
|
|
473
|
+
for (const j of _clibgovJsV2.values())
|
|
474
|
+
if (
|
|
475
|
+
j.profileId === profileId &&
|
|
476
|
+
(j.status === CLIBGOV_BRIDGE_LIFECYCLE_V2.QUEUED ||
|
|
477
|
+
j.status === CLIBGOV_BRIDGE_LIFECYCLE_V2.BRIDGING)
|
|
478
|
+
)
|
|
479
|
+
c++;
|
|
480
|
+
return c;
|
|
481
|
+
}
|
|
482
|
+
export function setMaxActiveClibgovProfilesPerOwnerV2(n) {
|
|
483
|
+
_clibgovMaxActive = _clibgovPos(n, "maxActiveClibgovProfilesPerOwner");
|
|
484
|
+
}
|
|
485
|
+
export function getMaxActiveClibgovProfilesPerOwnerV2() {
|
|
486
|
+
return _clibgovMaxActive;
|
|
487
|
+
}
|
|
488
|
+
export function setMaxPendingClibgovBridgesPerProfileV2(n) {
|
|
489
|
+
_clibgovMaxPending = _clibgovPos(n, "maxPendingClibgovBridgesPerProfile");
|
|
490
|
+
}
|
|
491
|
+
export function getMaxPendingClibgovBridgesPerProfileV2() {
|
|
492
|
+
return _clibgovMaxPending;
|
|
493
|
+
}
|
|
494
|
+
export function setClibgovProfileIdleMsV2(n) {
|
|
495
|
+
_clibgovIdleMs = _clibgovPos(n, "clibgovProfileIdleMs");
|
|
496
|
+
}
|
|
497
|
+
export function getClibgovProfileIdleMsV2() {
|
|
498
|
+
return _clibgovIdleMs;
|
|
499
|
+
}
|
|
500
|
+
export function setClibgovBridgeStuckMsV2(n) {
|
|
501
|
+
_clibgovStuckMs = _clibgovPos(n, "clibgovBridgeStuckMs");
|
|
502
|
+
}
|
|
503
|
+
export function getClibgovBridgeStuckMsV2() {
|
|
504
|
+
return _clibgovStuckMs;
|
|
505
|
+
}
|
|
506
|
+
export function _resetStateCliAnythingBridgeGovV2() {
|
|
507
|
+
_clibgovPsV2.clear();
|
|
508
|
+
_clibgovJsV2.clear();
|
|
509
|
+
_clibgovMaxActive = 8;
|
|
510
|
+
_clibgovMaxPending = 20;
|
|
511
|
+
_clibgovIdleMs = 30 * 24 * 60 * 60 * 1000;
|
|
512
|
+
_clibgovStuckMs = 60 * 1000;
|
|
513
|
+
}
|
|
514
|
+
export function registerClibgovProfileV2({ id, owner, tool, metadata } = {}) {
|
|
515
|
+
if (!id || !owner) throw new Error("id and owner required");
|
|
516
|
+
if (_clibgovPsV2.has(id))
|
|
517
|
+
throw new Error(`clibgov profile ${id} already exists`);
|
|
518
|
+
const now = Date.now();
|
|
519
|
+
const p = {
|
|
520
|
+
id,
|
|
521
|
+
owner,
|
|
522
|
+
tool: tool || "generic",
|
|
523
|
+
status: CLIBGOV_PROFILE_MATURITY_V2.PENDING,
|
|
524
|
+
createdAt: now,
|
|
525
|
+
updatedAt: now,
|
|
526
|
+
lastTouchedAt: now,
|
|
527
|
+
activatedAt: null,
|
|
528
|
+
archivedAt: null,
|
|
529
|
+
metadata: { ...(metadata || {}) },
|
|
530
|
+
};
|
|
531
|
+
_clibgovPsV2.set(id, p);
|
|
532
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
533
|
+
}
|
|
534
|
+
export function activateClibgovProfileV2(id) {
|
|
535
|
+
const p = _clibgovPsV2.get(id);
|
|
536
|
+
if (!p) throw new Error(`clibgov profile ${id} not found`);
|
|
537
|
+
const isInitial = p.status === CLIBGOV_PROFILE_MATURITY_V2.PENDING;
|
|
538
|
+
_clibgovCheckP(p.status, CLIBGOV_PROFILE_MATURITY_V2.ACTIVE);
|
|
539
|
+
if (isInitial && _clibgovCountActive(p.owner) >= _clibgovMaxActive)
|
|
540
|
+
throw new Error(`max active clibgov profiles for owner ${p.owner} reached`);
|
|
541
|
+
const now = Date.now();
|
|
542
|
+
p.status = CLIBGOV_PROFILE_MATURITY_V2.ACTIVE;
|
|
543
|
+
p.updatedAt = now;
|
|
544
|
+
p.lastTouchedAt = now;
|
|
545
|
+
if (!p.activatedAt) p.activatedAt = now;
|
|
546
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
547
|
+
}
|
|
548
|
+
export function degradeClibgovProfileV2(id) {
|
|
549
|
+
const p = _clibgovPsV2.get(id);
|
|
550
|
+
if (!p) throw new Error(`clibgov profile ${id} not found`);
|
|
551
|
+
_clibgovCheckP(p.status, CLIBGOV_PROFILE_MATURITY_V2.DEGRADED);
|
|
552
|
+
p.status = CLIBGOV_PROFILE_MATURITY_V2.DEGRADED;
|
|
553
|
+
p.updatedAt = Date.now();
|
|
554
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
555
|
+
}
|
|
556
|
+
export function archiveClibgovProfileV2(id) {
|
|
557
|
+
const p = _clibgovPsV2.get(id);
|
|
558
|
+
if (!p) throw new Error(`clibgov profile ${id} not found`);
|
|
559
|
+
_clibgovCheckP(p.status, CLIBGOV_PROFILE_MATURITY_V2.ARCHIVED);
|
|
560
|
+
const now = Date.now();
|
|
561
|
+
p.status = CLIBGOV_PROFILE_MATURITY_V2.ARCHIVED;
|
|
562
|
+
p.updatedAt = now;
|
|
563
|
+
if (!p.archivedAt) p.archivedAt = now;
|
|
564
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
565
|
+
}
|
|
566
|
+
export function touchClibgovProfileV2(id) {
|
|
567
|
+
const p = _clibgovPsV2.get(id);
|
|
568
|
+
if (!p) throw new Error(`clibgov profile ${id} not found`);
|
|
569
|
+
if (_clibgovPTerminal.has(p.status))
|
|
570
|
+
throw new Error(`cannot touch terminal clibgov profile ${id}`);
|
|
571
|
+
const now = Date.now();
|
|
572
|
+
p.lastTouchedAt = now;
|
|
573
|
+
p.updatedAt = now;
|
|
574
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
575
|
+
}
|
|
576
|
+
export function getClibgovProfileV2(id) {
|
|
577
|
+
const p = _clibgovPsV2.get(id);
|
|
578
|
+
if (!p) return null;
|
|
579
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
580
|
+
}
|
|
581
|
+
export function listClibgovProfilesV2() {
|
|
582
|
+
return [..._clibgovPsV2.values()].map((p) => ({
|
|
583
|
+
...p,
|
|
584
|
+
metadata: { ...p.metadata },
|
|
585
|
+
}));
|
|
586
|
+
}
|
|
587
|
+
export function createClibgovBridgeV2({
|
|
588
|
+
id,
|
|
589
|
+
profileId,
|
|
590
|
+
command,
|
|
591
|
+
metadata,
|
|
592
|
+
} = {}) {
|
|
593
|
+
if (!id || !profileId) throw new Error("id and profileId required");
|
|
594
|
+
if (_clibgovJsV2.has(id))
|
|
595
|
+
throw new Error(`clibgov bridge ${id} already exists`);
|
|
596
|
+
if (!_clibgovPsV2.has(profileId))
|
|
597
|
+
throw new Error(`clibgov profile ${profileId} not found`);
|
|
598
|
+
if (_clibgovCountPending(profileId) >= _clibgovMaxPending)
|
|
599
|
+
throw new Error(
|
|
600
|
+
`max pending clibgov bridges for profile ${profileId} reached`,
|
|
601
|
+
);
|
|
602
|
+
const now = Date.now();
|
|
603
|
+
const j = {
|
|
604
|
+
id,
|
|
605
|
+
profileId,
|
|
606
|
+
command: command || "",
|
|
607
|
+
status: CLIBGOV_BRIDGE_LIFECYCLE_V2.QUEUED,
|
|
608
|
+
createdAt: now,
|
|
609
|
+
updatedAt: now,
|
|
610
|
+
startedAt: null,
|
|
611
|
+
settledAt: null,
|
|
612
|
+
metadata: { ...(metadata || {}) },
|
|
613
|
+
};
|
|
614
|
+
_clibgovJsV2.set(id, j);
|
|
615
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
616
|
+
}
|
|
617
|
+
export function bridgingClibgovBridgeV2(id) {
|
|
618
|
+
const j = _clibgovJsV2.get(id);
|
|
619
|
+
if (!j) throw new Error(`clibgov bridge ${id} not found`);
|
|
620
|
+
_clibgovCheckJ(j.status, CLIBGOV_BRIDGE_LIFECYCLE_V2.BRIDGING);
|
|
621
|
+
const now = Date.now();
|
|
622
|
+
j.status = CLIBGOV_BRIDGE_LIFECYCLE_V2.BRIDGING;
|
|
623
|
+
j.updatedAt = now;
|
|
624
|
+
if (!j.startedAt) j.startedAt = now;
|
|
625
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
626
|
+
}
|
|
627
|
+
export function completeBridgeClibgovV2(id) {
|
|
628
|
+
const j = _clibgovJsV2.get(id);
|
|
629
|
+
if (!j) throw new Error(`clibgov bridge ${id} not found`);
|
|
630
|
+
_clibgovCheckJ(j.status, CLIBGOV_BRIDGE_LIFECYCLE_V2.BRIDGED);
|
|
631
|
+
const now = Date.now();
|
|
632
|
+
j.status = CLIBGOV_BRIDGE_LIFECYCLE_V2.BRIDGED;
|
|
633
|
+
j.updatedAt = now;
|
|
634
|
+
if (!j.settledAt) j.settledAt = now;
|
|
635
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
636
|
+
}
|
|
637
|
+
export function failClibgovBridgeV2(id, reason) {
|
|
638
|
+
const j = _clibgovJsV2.get(id);
|
|
639
|
+
if (!j) throw new Error(`clibgov bridge ${id} not found`);
|
|
640
|
+
_clibgovCheckJ(j.status, CLIBGOV_BRIDGE_LIFECYCLE_V2.FAILED);
|
|
641
|
+
const now = Date.now();
|
|
642
|
+
j.status = CLIBGOV_BRIDGE_LIFECYCLE_V2.FAILED;
|
|
643
|
+
j.updatedAt = now;
|
|
644
|
+
if (!j.settledAt) j.settledAt = now;
|
|
645
|
+
if (reason) j.metadata.failReason = String(reason);
|
|
646
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
647
|
+
}
|
|
648
|
+
export function cancelClibgovBridgeV2(id, reason) {
|
|
649
|
+
const j = _clibgovJsV2.get(id);
|
|
650
|
+
if (!j) throw new Error(`clibgov bridge ${id} not found`);
|
|
651
|
+
_clibgovCheckJ(j.status, CLIBGOV_BRIDGE_LIFECYCLE_V2.CANCELLED);
|
|
652
|
+
const now = Date.now();
|
|
653
|
+
j.status = CLIBGOV_BRIDGE_LIFECYCLE_V2.CANCELLED;
|
|
654
|
+
j.updatedAt = now;
|
|
655
|
+
if (!j.settledAt) j.settledAt = now;
|
|
656
|
+
if (reason) j.metadata.cancelReason = String(reason);
|
|
657
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
658
|
+
}
|
|
659
|
+
export function getClibgovBridgeV2(id) {
|
|
660
|
+
const j = _clibgovJsV2.get(id);
|
|
661
|
+
if (!j) return null;
|
|
662
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
663
|
+
}
|
|
664
|
+
export function listClibgovBridgesV2() {
|
|
665
|
+
return [..._clibgovJsV2.values()].map((j) => ({
|
|
666
|
+
...j,
|
|
667
|
+
metadata: { ...j.metadata },
|
|
668
|
+
}));
|
|
669
|
+
}
|
|
670
|
+
export function autoDegradeIdleClibgovProfilesV2({ now } = {}) {
|
|
671
|
+
const t = now ?? Date.now();
|
|
672
|
+
const flipped = [];
|
|
673
|
+
for (const p of _clibgovPsV2.values())
|
|
674
|
+
if (
|
|
675
|
+
p.status === CLIBGOV_PROFILE_MATURITY_V2.ACTIVE &&
|
|
676
|
+
t - p.lastTouchedAt >= _clibgovIdleMs
|
|
677
|
+
) {
|
|
678
|
+
p.status = CLIBGOV_PROFILE_MATURITY_V2.DEGRADED;
|
|
679
|
+
p.updatedAt = t;
|
|
680
|
+
flipped.push(p.id);
|
|
681
|
+
}
|
|
682
|
+
return { flipped, count: flipped.length };
|
|
683
|
+
}
|
|
684
|
+
export function autoFailStuckClibgovBridgesV2({ now } = {}) {
|
|
685
|
+
const t = now ?? Date.now();
|
|
686
|
+
const flipped = [];
|
|
687
|
+
for (const j of _clibgovJsV2.values())
|
|
688
|
+
if (
|
|
689
|
+
j.status === CLIBGOV_BRIDGE_LIFECYCLE_V2.BRIDGING &&
|
|
690
|
+
j.startedAt != null &&
|
|
691
|
+
t - j.startedAt >= _clibgovStuckMs
|
|
692
|
+
) {
|
|
693
|
+
j.status = CLIBGOV_BRIDGE_LIFECYCLE_V2.FAILED;
|
|
694
|
+
j.updatedAt = t;
|
|
695
|
+
if (!j.settledAt) j.settledAt = t;
|
|
696
|
+
j.metadata.failReason = "auto-fail-stuck";
|
|
697
|
+
flipped.push(j.id);
|
|
698
|
+
}
|
|
699
|
+
return { flipped, count: flipped.length };
|
|
700
|
+
}
|
|
701
|
+
export function getCliAnythingBridgeGovStatsV2() {
|
|
702
|
+
const profilesByStatus = {};
|
|
703
|
+
for (const v of Object.values(CLIBGOV_PROFILE_MATURITY_V2))
|
|
704
|
+
profilesByStatus[v] = 0;
|
|
705
|
+
for (const p of _clibgovPsV2.values()) profilesByStatus[p.status]++;
|
|
706
|
+
const bridgesByStatus = {};
|
|
707
|
+
for (const v of Object.values(CLIBGOV_BRIDGE_LIFECYCLE_V2))
|
|
708
|
+
bridgesByStatus[v] = 0;
|
|
709
|
+
for (const j of _clibgovJsV2.values()) bridgesByStatus[j.status]++;
|
|
710
|
+
return {
|
|
711
|
+
totalClibgovProfilesV2: _clibgovPsV2.size,
|
|
712
|
+
totalClibgovBridgesV2: _clibgovJsV2.size,
|
|
713
|
+
maxActiveClibgovProfilesPerOwner: _clibgovMaxActive,
|
|
714
|
+
maxPendingClibgovBridgesPerProfile: _clibgovMaxPending,
|
|
715
|
+
clibgovProfileIdleMs: _clibgovIdleMs,
|
|
716
|
+
clibgovBridgeStuckMs: _clibgovStuckMs,
|
|
717
|
+
profilesByStatus,
|
|
718
|
+
bridgesByStatus,
|
|
719
|
+
};
|
|
720
|
+
}
|