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.
- package/README.md +52 -3
- package/package.json +1 -1
- package/src/commands/a2a.js +201 -0
- package/src/commands/activitypub.js +207 -0
- package/src/commands/agent-network.js +217 -0
- package/src/commands/agent.js +1250 -0
- package/src/commands/automation.js +201 -0
- package/src/commands/bi.js +203 -0
- package/src/commands/browse.js +213 -0
- package/src/commands/chat.js +605 -0
- package/src/commands/cli-anything.js +426 -0
- package/src/commands/codegen.js +207 -0
- package/src/commands/collab.js +211 -0
- package/src/commands/compliance.js +822 -0
- package/src/commands/config.js +213 -0
- package/src/commands/cowork.js +1666 -0
- package/src/commands/crosschain.js +203 -0
- package/src/commands/dao.js +203 -0
- package/src/commands/dbevo.js +227 -0
- package/src/commands/dev.js +207 -0
- package/src/commands/did-v2.js +217 -0
- package/src/commands/did.js +221 -0
- package/src/commands/dlp.js +213 -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 +830 -0
- package/src/commands/export.js +213 -0
- package/src/commands/federation.js +209 -0
- package/src/commands/fusion.js +205 -0
- package/src/commands/governance.js +209 -0
- package/src/commands/hmemory.js +203 -0
- package/src/commands/hook.js +209 -0
- package/src/commands/import.js +209 -0
- package/src/commands/inference.js +207 -0
- package/src/commands/infra.js +203 -0
- package/src/commands/instinct.js +209 -0
- package/src/commands/ipfs.js +207 -0
- package/src/commands/kg.js +195 -0
- package/src/commands/llm.js +426 -0
- package/src/commands/matrix.js +207 -0
- package/src/commands/mcp.js +217 -0
- package/src/commands/memory.js +412 -0
- package/src/commands/multimodal.js +203 -0
- package/src/commands/nlprog.js +225 -0
- package/src/commands/nostr.js +209 -0
- package/src/commands/note.js +205 -0
- package/src/commands/ops.js +219 -0
- package/src/commands/orchestrate.js +406 -0
- package/src/commands/org.js +209 -0
- package/src/commands/p2p.js +209 -0
- package/src/commands/perception.js +209 -0
- package/src/commands/permmem.js +203 -0
- package/src/commands/pipeline.js +199 -0
- package/src/commands/planmode.js +426 -0
- package/src/commands/plugin-ecosystem.js +209 -0
- package/src/commands/plugin.js +209 -0
- package/src/commands/pqc.js +213 -0
- package/src/commands/quantization.js +207 -0
- package/src/commands/rcache.js +205 -0
- package/src/commands/recommend.js +233 -0
- package/src/commands/runtime.js +205 -0
- package/src/commands/scim.js +209 -0
- package/src/commands/services.js +207 -0
- package/src/commands/session.js +209 -0
- package/src/commands/setup.js +205 -0
- package/src/commands/skill.js +414 -0
- package/src/commands/social.js +201 -0
- package/src/commands/sso.js +209 -0
- package/src/commands/start.js +209 -0
- package/src/commands/stream.js +213 -0
- package/src/commands/sync.js +209 -0
- package/src/commands/tech.js +209 -0
- package/src/commands/tenant.js +217 -0
- package/src/commands/tokens.js +209 -0
- package/src/commands/trust.js +217 -0
- package/src/commands/ui.js +225 -0
- package/src/commands/wallet.js +209 -0
- package/src/commands/workflow.js +412 -0
- package/src/index.js +252 -0
- package/src/lib/a2a-protocol.js +332 -0
- package/src/lib/activitypub-bridge.js +334 -0
- package/src/lib/agent-coordinator.js +334 -0
- package/src/lib/agent-economy.js +334 -0
- package/src/lib/agent-network.js +341 -0
- package/src/lib/agent-router.js +333 -0
- package/src/lib/aiops.js +346 -0
- package/src/lib/automation-engine.js +335 -0
- package/src/lib/autonomous-agent.js +332 -0
- package/src/lib/autonomous-developer.js +332 -0
- package/src/lib/bi-engine.js +333 -0
- package/src/lib/browser-automation.js +334 -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/code-agent.js +339 -0
- package/src/lib/collaboration-governance.js +334 -0
- package/src/lib/community-governance.js +346 -0
- package/src/lib/compliance-manager.js +334 -0
- package/src/lib/content-recommendation.js +351 -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-share.js +338 -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/dbevo.js +351 -0
- package/src/lib/decentral-infra.js +330 -0
- package/src/lib/did-manager.js +341 -0
- package/src/lib/did-v2-manager.js +341 -0
- package/src/lib/dlp-engine.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-governance.js +334 -0
- package/src/lib/evomap-manager.js +330 -0
- package/src/lib/execution-backend.js +330 -0
- package/src/lib/federation-hardening.js +340 -0
- package/src/lib/hashline.js +338 -0
- package/src/lib/hierarchical-memory.js +334 -0
- package/src/lib/hook-manager.js +341 -0
- package/src/lib/inference-network.js +341 -0
- package/src/lib/instinct-manager.js +346 -0
- package/src/lib/interaction-adapter.js +330 -0
- package/src/lib/interactive-planner.js +354 -0
- package/src/lib/ipfs-storage.js +334 -0
- package/src/lib/knowledge-exporter.js +341 -0
- package/src/lib/knowledge-graph.js +331 -0
- package/src/lib/knowledge-importer.js +341 -0
- package/src/lib/llm-providers.js +346 -0
- package/src/lib/matrix-bridge.js +339 -0
- package/src/lib/mcp-registry.js +346 -0
- package/src/lib/memory-manager.js +336 -0
- package/src/lib/multimodal.js +330 -0
- package/src/lib/nl-programming.js +341 -0
- package/src/lib/nostr-bridge.js +336 -0
- package/src/lib/note-versioning.js +339 -0
- package/src/lib/org-manager.js +336 -0
- package/src/lib/p2p-manager.js +341 -0
- package/src/lib/perception.js +346 -0
- package/src/lib/permanent-memory.js +327 -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/plugin-ecosystem.js +346 -0
- package/src/lib/pqc-manager.js +346 -0
- package/src/lib/process-manager.js +336 -0
- package/src/lib/protocol-fusion.js +338 -0
- package/src/lib/provider-options.js +346 -0
- package/src/lib/provider-stream.js +348 -0
- package/src/lib/quantization.js +337 -0
- package/src/lib/response-cache.js +333 -0
- package/src/lib/scim-manager.js +346 -0
- package/src/lib/service-manager.js +337 -0
- package/src/lib/session-core-singletons.js +341 -0
- package/src/lib/session-manager.js +334 -0
- package/src/lib/skill-loader.js +334 -0
- package/src/lib/skill-mcp.js +336 -0
- package/src/lib/social-manager.js +330 -0
- package/src/lib/sso-manager.js +340 -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/sync-manager.js +336 -0
- package/src/lib/tech-learning-engine.js +341 -0
- package/src/lib/tenant-saas.js +341 -0
- package/src/lib/threat-intel.js +330 -0
- package/src/lib/todo-manager.js +336 -0
- package/src/lib/token-tracker.js +336 -0
- package/src/lib/trust-security.js +343 -0
- package/src/lib/ueba.js +340 -0
- package/src/lib/universal-runtime.js +330 -0
- package/src/lib/wallet-manager.js +336 -0
- package/src/lib/web-ui-server.js +348 -0
- package/src/lib/workflow-engine.js +330 -0
- package/src/lib/workflow-expr.js +346 -0
- package/src/lib/ws-chat-handler.js +337 -0
package/src/lib/llm-providers.js
CHANGED
|
@@ -756,3 +756,349 @@ export function _resetStateLlmProvidersV2() {
|
|
|
756
756
|
_stateLlmV2.profileIdleMs = PROVIDER_DEFAULT_PROFILE_IDLE_MS;
|
|
757
757
|
_stateLlmV2.requestStuckMs = PROVIDER_DEFAULT_REQUEST_STUCK_MS;
|
|
758
758
|
}
|
|
759
|
+
|
|
760
|
+
// =====================================================================
|
|
761
|
+
// llm-providers V2 governance overlay (iter22)
|
|
762
|
+
// =====================================================================
|
|
763
|
+
export const LLMGOV_PROFILE_MATURITY_V2 = Object.freeze({
|
|
764
|
+
PENDING: "pending",
|
|
765
|
+
ACTIVE: "active",
|
|
766
|
+
DEGRADED: "degraded",
|
|
767
|
+
ARCHIVED: "archived",
|
|
768
|
+
});
|
|
769
|
+
export const LLMGOV_COMPLETION_LIFECYCLE_V2 = Object.freeze({
|
|
770
|
+
QUEUED: "queued",
|
|
771
|
+
INFERRING: "inferring",
|
|
772
|
+
INFERRED: "inferred",
|
|
773
|
+
FAILED: "failed",
|
|
774
|
+
CANCELLED: "cancelled",
|
|
775
|
+
});
|
|
776
|
+
const _llmgovPTrans = new Map([
|
|
777
|
+
[
|
|
778
|
+
LLMGOV_PROFILE_MATURITY_V2.PENDING,
|
|
779
|
+
new Set([
|
|
780
|
+
LLMGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
781
|
+
LLMGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
782
|
+
]),
|
|
783
|
+
],
|
|
784
|
+
[
|
|
785
|
+
LLMGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
786
|
+
new Set([
|
|
787
|
+
LLMGOV_PROFILE_MATURITY_V2.DEGRADED,
|
|
788
|
+
LLMGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
789
|
+
]),
|
|
790
|
+
],
|
|
791
|
+
[
|
|
792
|
+
LLMGOV_PROFILE_MATURITY_V2.DEGRADED,
|
|
793
|
+
new Set([
|
|
794
|
+
LLMGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
795
|
+
LLMGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
796
|
+
]),
|
|
797
|
+
],
|
|
798
|
+
[LLMGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
|
|
799
|
+
]);
|
|
800
|
+
const _llmgovPTerminal = new Set([LLMGOV_PROFILE_MATURITY_V2.ARCHIVED]);
|
|
801
|
+
const _llmgovJTrans = new Map([
|
|
802
|
+
[
|
|
803
|
+
LLMGOV_COMPLETION_LIFECYCLE_V2.QUEUED,
|
|
804
|
+
new Set([
|
|
805
|
+
LLMGOV_COMPLETION_LIFECYCLE_V2.INFERRING,
|
|
806
|
+
LLMGOV_COMPLETION_LIFECYCLE_V2.CANCELLED,
|
|
807
|
+
]),
|
|
808
|
+
],
|
|
809
|
+
[
|
|
810
|
+
LLMGOV_COMPLETION_LIFECYCLE_V2.INFERRING,
|
|
811
|
+
new Set([
|
|
812
|
+
LLMGOV_COMPLETION_LIFECYCLE_V2.INFERRED,
|
|
813
|
+
LLMGOV_COMPLETION_LIFECYCLE_V2.FAILED,
|
|
814
|
+
LLMGOV_COMPLETION_LIFECYCLE_V2.CANCELLED,
|
|
815
|
+
]),
|
|
816
|
+
],
|
|
817
|
+
[LLMGOV_COMPLETION_LIFECYCLE_V2.INFERRED, new Set()],
|
|
818
|
+
[LLMGOV_COMPLETION_LIFECYCLE_V2.FAILED, new Set()],
|
|
819
|
+
[LLMGOV_COMPLETION_LIFECYCLE_V2.CANCELLED, new Set()],
|
|
820
|
+
]);
|
|
821
|
+
const _llmgovPsV2 = new Map();
|
|
822
|
+
const _llmgovJsV2 = new Map();
|
|
823
|
+
let _llmgovMaxActive = 8,
|
|
824
|
+
_llmgovMaxPending = 25,
|
|
825
|
+
_llmgovIdleMs = 30 * 24 * 60 * 60 * 1000,
|
|
826
|
+
_llmgovStuckMs = 60 * 1000;
|
|
827
|
+
function _llmgovPos(n, label) {
|
|
828
|
+
const v = Math.floor(Number(n));
|
|
829
|
+
if (!Number.isFinite(v) || v <= 0)
|
|
830
|
+
throw new Error(`${label} must be positive integer`);
|
|
831
|
+
return v;
|
|
832
|
+
}
|
|
833
|
+
function _llmgovCheckP(from, to) {
|
|
834
|
+
const a = _llmgovPTrans.get(from);
|
|
835
|
+
if (!a || !a.has(to))
|
|
836
|
+
throw new Error(`invalid llmgov profile transition ${from} → ${to}`);
|
|
837
|
+
}
|
|
838
|
+
function _llmgovCheckJ(from, to) {
|
|
839
|
+
const a = _llmgovJTrans.get(from);
|
|
840
|
+
if (!a || !a.has(to))
|
|
841
|
+
throw new Error(`invalid llmgov completion transition ${from} → ${to}`);
|
|
842
|
+
}
|
|
843
|
+
function _llmgovCountActive(owner) {
|
|
844
|
+
let c = 0;
|
|
845
|
+
for (const p of _llmgovPsV2.values())
|
|
846
|
+
if (p.owner === owner && p.status === LLMGOV_PROFILE_MATURITY_V2.ACTIVE)
|
|
847
|
+
c++;
|
|
848
|
+
return c;
|
|
849
|
+
}
|
|
850
|
+
function _llmgovCountPending(profileId) {
|
|
851
|
+
let c = 0;
|
|
852
|
+
for (const j of _llmgovJsV2.values())
|
|
853
|
+
if (
|
|
854
|
+
j.profileId === profileId &&
|
|
855
|
+
(j.status === LLMGOV_COMPLETION_LIFECYCLE_V2.QUEUED ||
|
|
856
|
+
j.status === LLMGOV_COMPLETION_LIFECYCLE_V2.INFERRING)
|
|
857
|
+
)
|
|
858
|
+
c++;
|
|
859
|
+
return c;
|
|
860
|
+
}
|
|
861
|
+
export function setMaxActiveLlmgovProfilesPerOwnerV2(n) {
|
|
862
|
+
_llmgovMaxActive = _llmgovPos(n, "maxActiveLlmgovProfilesPerOwner");
|
|
863
|
+
}
|
|
864
|
+
export function getMaxActiveLlmgovProfilesPerOwnerV2() {
|
|
865
|
+
return _llmgovMaxActive;
|
|
866
|
+
}
|
|
867
|
+
export function setMaxPendingLlmgovCompletionsPerProfileV2(n) {
|
|
868
|
+
_llmgovMaxPending = _llmgovPos(n, "maxPendingLlmgovCompletionsPerProfile");
|
|
869
|
+
}
|
|
870
|
+
export function getMaxPendingLlmgovCompletionsPerProfileV2() {
|
|
871
|
+
return _llmgovMaxPending;
|
|
872
|
+
}
|
|
873
|
+
export function setLlmgovProfileIdleMsV2(n) {
|
|
874
|
+
_llmgovIdleMs = _llmgovPos(n, "llmgovProfileIdleMs");
|
|
875
|
+
}
|
|
876
|
+
export function getLlmgovProfileIdleMsV2() {
|
|
877
|
+
return _llmgovIdleMs;
|
|
878
|
+
}
|
|
879
|
+
export function setLlmgovCompletionStuckMsV2(n) {
|
|
880
|
+
_llmgovStuckMs = _llmgovPos(n, "llmgovCompletionStuckMs");
|
|
881
|
+
}
|
|
882
|
+
export function getLlmgovCompletionStuckMsV2() {
|
|
883
|
+
return _llmgovStuckMs;
|
|
884
|
+
}
|
|
885
|
+
export function _resetStateLlmProvidersGovV2() {
|
|
886
|
+
_llmgovPsV2.clear();
|
|
887
|
+
_llmgovJsV2.clear();
|
|
888
|
+
_llmgovMaxActive = 8;
|
|
889
|
+
_llmgovMaxPending = 25;
|
|
890
|
+
_llmgovIdleMs = 30 * 24 * 60 * 60 * 1000;
|
|
891
|
+
_llmgovStuckMs = 60 * 1000;
|
|
892
|
+
}
|
|
893
|
+
export function registerLlmgovProfileV2({
|
|
894
|
+
id,
|
|
895
|
+
owner,
|
|
896
|
+
provider,
|
|
897
|
+
metadata,
|
|
898
|
+
} = {}) {
|
|
899
|
+
if (!id || !owner) throw new Error("id and owner required");
|
|
900
|
+
if (_llmgovPsV2.has(id))
|
|
901
|
+
throw new Error(`llmgov profile ${id} already exists`);
|
|
902
|
+
const now = Date.now();
|
|
903
|
+
const p = {
|
|
904
|
+
id,
|
|
905
|
+
owner,
|
|
906
|
+
provider: provider || "ollama",
|
|
907
|
+
status: LLMGOV_PROFILE_MATURITY_V2.PENDING,
|
|
908
|
+
createdAt: now,
|
|
909
|
+
updatedAt: now,
|
|
910
|
+
lastTouchedAt: now,
|
|
911
|
+
activatedAt: null,
|
|
912
|
+
archivedAt: null,
|
|
913
|
+
metadata: { ...(metadata || {}) },
|
|
914
|
+
};
|
|
915
|
+
_llmgovPsV2.set(id, p);
|
|
916
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
917
|
+
}
|
|
918
|
+
export function activateLlmgovProfileV2(id) {
|
|
919
|
+
const p = _llmgovPsV2.get(id);
|
|
920
|
+
if (!p) throw new Error(`llmgov profile ${id} not found`);
|
|
921
|
+
const isInitial = p.status === LLMGOV_PROFILE_MATURITY_V2.PENDING;
|
|
922
|
+
_llmgovCheckP(p.status, LLMGOV_PROFILE_MATURITY_V2.ACTIVE);
|
|
923
|
+
if (isInitial && _llmgovCountActive(p.owner) >= _llmgovMaxActive)
|
|
924
|
+
throw new Error(`max active llmgov profiles for owner ${p.owner} reached`);
|
|
925
|
+
const now = Date.now();
|
|
926
|
+
p.status = LLMGOV_PROFILE_MATURITY_V2.ACTIVE;
|
|
927
|
+
p.updatedAt = now;
|
|
928
|
+
p.lastTouchedAt = now;
|
|
929
|
+
if (!p.activatedAt) p.activatedAt = now;
|
|
930
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
931
|
+
}
|
|
932
|
+
export function degradeLlmgovProfileV2(id) {
|
|
933
|
+
const p = _llmgovPsV2.get(id);
|
|
934
|
+
if (!p) throw new Error(`llmgov profile ${id} not found`);
|
|
935
|
+
_llmgovCheckP(p.status, LLMGOV_PROFILE_MATURITY_V2.DEGRADED);
|
|
936
|
+
p.status = LLMGOV_PROFILE_MATURITY_V2.DEGRADED;
|
|
937
|
+
p.updatedAt = Date.now();
|
|
938
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
939
|
+
}
|
|
940
|
+
export function archiveLlmgovProfileV2(id) {
|
|
941
|
+
const p = _llmgovPsV2.get(id);
|
|
942
|
+
if (!p) throw new Error(`llmgov profile ${id} not found`);
|
|
943
|
+
_llmgovCheckP(p.status, LLMGOV_PROFILE_MATURITY_V2.ARCHIVED);
|
|
944
|
+
const now = Date.now();
|
|
945
|
+
p.status = LLMGOV_PROFILE_MATURITY_V2.ARCHIVED;
|
|
946
|
+
p.updatedAt = now;
|
|
947
|
+
if (!p.archivedAt) p.archivedAt = now;
|
|
948
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
949
|
+
}
|
|
950
|
+
export function touchLlmgovProfileV2(id) {
|
|
951
|
+
const p = _llmgovPsV2.get(id);
|
|
952
|
+
if (!p) throw new Error(`llmgov profile ${id} not found`);
|
|
953
|
+
if (_llmgovPTerminal.has(p.status))
|
|
954
|
+
throw new Error(`cannot touch terminal llmgov profile ${id}`);
|
|
955
|
+
const now = Date.now();
|
|
956
|
+
p.lastTouchedAt = now;
|
|
957
|
+
p.updatedAt = now;
|
|
958
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
959
|
+
}
|
|
960
|
+
export function getLlmgovProfileV2(id) {
|
|
961
|
+
const p = _llmgovPsV2.get(id);
|
|
962
|
+
if (!p) return null;
|
|
963
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
964
|
+
}
|
|
965
|
+
export function listLlmgovProfilesV2() {
|
|
966
|
+
return [..._llmgovPsV2.values()].map((p) => ({
|
|
967
|
+
...p,
|
|
968
|
+
metadata: { ...p.metadata },
|
|
969
|
+
}));
|
|
970
|
+
}
|
|
971
|
+
export function createLlmgovCompletionV2({
|
|
972
|
+
id,
|
|
973
|
+
profileId,
|
|
974
|
+
model,
|
|
975
|
+
metadata,
|
|
976
|
+
} = {}) {
|
|
977
|
+
if (!id || !profileId) throw new Error("id and profileId required");
|
|
978
|
+
if (_llmgovJsV2.has(id))
|
|
979
|
+
throw new Error(`llmgov completion ${id} already exists`);
|
|
980
|
+
if (!_llmgovPsV2.has(profileId))
|
|
981
|
+
throw new Error(`llmgov profile ${profileId} not found`);
|
|
982
|
+
if (_llmgovCountPending(profileId) >= _llmgovMaxPending)
|
|
983
|
+
throw new Error(
|
|
984
|
+
`max pending llmgov completions for profile ${profileId} reached`,
|
|
985
|
+
);
|
|
986
|
+
const now = Date.now();
|
|
987
|
+
const j = {
|
|
988
|
+
id,
|
|
989
|
+
profileId,
|
|
990
|
+
model: model || "",
|
|
991
|
+
status: LLMGOV_COMPLETION_LIFECYCLE_V2.QUEUED,
|
|
992
|
+
createdAt: now,
|
|
993
|
+
updatedAt: now,
|
|
994
|
+
startedAt: null,
|
|
995
|
+
settledAt: null,
|
|
996
|
+
metadata: { ...(metadata || {}) },
|
|
997
|
+
};
|
|
998
|
+
_llmgovJsV2.set(id, j);
|
|
999
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
1000
|
+
}
|
|
1001
|
+
export function inferringLlmgovCompletionV2(id) {
|
|
1002
|
+
const j = _llmgovJsV2.get(id);
|
|
1003
|
+
if (!j) throw new Error(`llmgov completion ${id} not found`);
|
|
1004
|
+
_llmgovCheckJ(j.status, LLMGOV_COMPLETION_LIFECYCLE_V2.INFERRING);
|
|
1005
|
+
const now = Date.now();
|
|
1006
|
+
j.status = LLMGOV_COMPLETION_LIFECYCLE_V2.INFERRING;
|
|
1007
|
+
j.updatedAt = now;
|
|
1008
|
+
if (!j.startedAt) j.startedAt = now;
|
|
1009
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
1010
|
+
}
|
|
1011
|
+
export function completeCompletionLlmgovV2(id) {
|
|
1012
|
+
const j = _llmgovJsV2.get(id);
|
|
1013
|
+
if (!j) throw new Error(`llmgov completion ${id} not found`);
|
|
1014
|
+
_llmgovCheckJ(j.status, LLMGOV_COMPLETION_LIFECYCLE_V2.INFERRED);
|
|
1015
|
+
const now = Date.now();
|
|
1016
|
+
j.status = LLMGOV_COMPLETION_LIFECYCLE_V2.INFERRED;
|
|
1017
|
+
j.updatedAt = now;
|
|
1018
|
+
if (!j.settledAt) j.settledAt = now;
|
|
1019
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
1020
|
+
}
|
|
1021
|
+
export function failLlmgovCompletionV2(id, reason) {
|
|
1022
|
+
const j = _llmgovJsV2.get(id);
|
|
1023
|
+
if (!j) throw new Error(`llmgov completion ${id} not found`);
|
|
1024
|
+
_llmgovCheckJ(j.status, LLMGOV_COMPLETION_LIFECYCLE_V2.FAILED);
|
|
1025
|
+
const now = Date.now();
|
|
1026
|
+
j.status = LLMGOV_COMPLETION_LIFECYCLE_V2.FAILED;
|
|
1027
|
+
j.updatedAt = now;
|
|
1028
|
+
if (!j.settledAt) j.settledAt = now;
|
|
1029
|
+
if (reason) j.metadata.failReason = String(reason);
|
|
1030
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
1031
|
+
}
|
|
1032
|
+
export function cancelLlmgovCompletionV2(id, reason) {
|
|
1033
|
+
const j = _llmgovJsV2.get(id);
|
|
1034
|
+
if (!j) throw new Error(`llmgov completion ${id} not found`);
|
|
1035
|
+
_llmgovCheckJ(j.status, LLMGOV_COMPLETION_LIFECYCLE_V2.CANCELLED);
|
|
1036
|
+
const now = Date.now();
|
|
1037
|
+
j.status = LLMGOV_COMPLETION_LIFECYCLE_V2.CANCELLED;
|
|
1038
|
+
j.updatedAt = now;
|
|
1039
|
+
if (!j.settledAt) j.settledAt = now;
|
|
1040
|
+
if (reason) j.metadata.cancelReason = String(reason);
|
|
1041
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
1042
|
+
}
|
|
1043
|
+
export function getLlmgovCompletionV2(id) {
|
|
1044
|
+
const j = _llmgovJsV2.get(id);
|
|
1045
|
+
if (!j) return null;
|
|
1046
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
1047
|
+
}
|
|
1048
|
+
export function listLlmgovCompletionsV2() {
|
|
1049
|
+
return [..._llmgovJsV2.values()].map((j) => ({
|
|
1050
|
+
...j,
|
|
1051
|
+
metadata: { ...j.metadata },
|
|
1052
|
+
}));
|
|
1053
|
+
}
|
|
1054
|
+
export function autoDegradeIdleLlmgovProfilesV2({ now } = {}) {
|
|
1055
|
+
const t = now ?? Date.now();
|
|
1056
|
+
const flipped = [];
|
|
1057
|
+
for (const p of _llmgovPsV2.values())
|
|
1058
|
+
if (
|
|
1059
|
+
p.status === LLMGOV_PROFILE_MATURITY_V2.ACTIVE &&
|
|
1060
|
+
t - p.lastTouchedAt >= _llmgovIdleMs
|
|
1061
|
+
) {
|
|
1062
|
+
p.status = LLMGOV_PROFILE_MATURITY_V2.DEGRADED;
|
|
1063
|
+
p.updatedAt = t;
|
|
1064
|
+
flipped.push(p.id);
|
|
1065
|
+
}
|
|
1066
|
+
return { flipped, count: flipped.length };
|
|
1067
|
+
}
|
|
1068
|
+
export function autoFailStuckLlmgovCompletionsV2({ now } = {}) {
|
|
1069
|
+
const t = now ?? Date.now();
|
|
1070
|
+
const flipped = [];
|
|
1071
|
+
for (const j of _llmgovJsV2.values())
|
|
1072
|
+
if (
|
|
1073
|
+
j.status === LLMGOV_COMPLETION_LIFECYCLE_V2.INFERRING &&
|
|
1074
|
+
j.startedAt != null &&
|
|
1075
|
+
t - j.startedAt >= _llmgovStuckMs
|
|
1076
|
+
) {
|
|
1077
|
+
j.status = LLMGOV_COMPLETION_LIFECYCLE_V2.FAILED;
|
|
1078
|
+
j.updatedAt = t;
|
|
1079
|
+
if (!j.settledAt) j.settledAt = t;
|
|
1080
|
+
j.metadata.failReason = "auto-fail-stuck";
|
|
1081
|
+
flipped.push(j.id);
|
|
1082
|
+
}
|
|
1083
|
+
return { flipped, count: flipped.length };
|
|
1084
|
+
}
|
|
1085
|
+
export function getLlmProvidersGovStatsV2() {
|
|
1086
|
+
const profilesByStatus = {};
|
|
1087
|
+
for (const v of Object.values(LLMGOV_PROFILE_MATURITY_V2))
|
|
1088
|
+
profilesByStatus[v] = 0;
|
|
1089
|
+
for (const p of _llmgovPsV2.values()) profilesByStatus[p.status]++;
|
|
1090
|
+
const completionsByStatus = {};
|
|
1091
|
+
for (const v of Object.values(LLMGOV_COMPLETION_LIFECYCLE_V2))
|
|
1092
|
+
completionsByStatus[v] = 0;
|
|
1093
|
+
for (const j of _llmgovJsV2.values()) completionsByStatus[j.status]++;
|
|
1094
|
+
return {
|
|
1095
|
+
totalLlmgovProfilesV2: _llmgovPsV2.size,
|
|
1096
|
+
totalLlmgovCompletionsV2: _llmgovJsV2.size,
|
|
1097
|
+
maxActiveLlmgovProfilesPerOwner: _llmgovMaxActive,
|
|
1098
|
+
maxPendingLlmgovCompletionsPerProfile: _llmgovMaxPending,
|
|
1099
|
+
llmgovProfileIdleMs: _llmgovIdleMs,
|
|
1100
|
+
llmgovCompletionStuckMs: _llmgovStuckMs,
|
|
1101
|
+
profilesByStatus,
|
|
1102
|
+
completionsByStatus,
|
|
1103
|
+
};
|
|
1104
|
+
}
|
package/src/lib/matrix-bridge.js
CHANGED
|
@@ -785,3 +785,342 @@ export function getMatrixBridgeStatsV2() {
|
|
|
785
785
|
msgsByStatus,
|
|
786
786
|
};
|
|
787
787
|
}
|
|
788
|
+
|
|
789
|
+
// =====================================================================
|
|
790
|
+
// matrix-bridge V2 governance overlay (iter21)
|
|
791
|
+
// =====================================================================
|
|
792
|
+
export const MATGOV_PROFILE_MATURITY_V2 = Object.freeze({
|
|
793
|
+
PENDING: "pending",
|
|
794
|
+
ACTIVE: "active",
|
|
795
|
+
SUSPENDED: "suspended",
|
|
796
|
+
ARCHIVED: "archived",
|
|
797
|
+
});
|
|
798
|
+
export const MATGOV_SEND_LIFECYCLE_V2 = Object.freeze({
|
|
799
|
+
QUEUED: "queued",
|
|
800
|
+
SENDING: "sending",
|
|
801
|
+
SENT: "sent",
|
|
802
|
+
FAILED: "failed",
|
|
803
|
+
CANCELLED: "cancelled",
|
|
804
|
+
});
|
|
805
|
+
const _matgovPTrans = new Map([
|
|
806
|
+
[
|
|
807
|
+
MATGOV_PROFILE_MATURITY_V2.PENDING,
|
|
808
|
+
new Set([
|
|
809
|
+
MATGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
810
|
+
MATGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
811
|
+
]),
|
|
812
|
+
],
|
|
813
|
+
[
|
|
814
|
+
MATGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
815
|
+
new Set([
|
|
816
|
+
MATGOV_PROFILE_MATURITY_V2.SUSPENDED,
|
|
817
|
+
MATGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
818
|
+
]),
|
|
819
|
+
],
|
|
820
|
+
[
|
|
821
|
+
MATGOV_PROFILE_MATURITY_V2.SUSPENDED,
|
|
822
|
+
new Set([
|
|
823
|
+
MATGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
824
|
+
MATGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
825
|
+
]),
|
|
826
|
+
],
|
|
827
|
+
[MATGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
|
|
828
|
+
]);
|
|
829
|
+
const _matgovPTerminal = new Set([MATGOV_PROFILE_MATURITY_V2.ARCHIVED]);
|
|
830
|
+
const _matgovJTrans = new Map([
|
|
831
|
+
[
|
|
832
|
+
MATGOV_SEND_LIFECYCLE_V2.QUEUED,
|
|
833
|
+
new Set([
|
|
834
|
+
MATGOV_SEND_LIFECYCLE_V2.SENDING,
|
|
835
|
+
MATGOV_SEND_LIFECYCLE_V2.CANCELLED,
|
|
836
|
+
]),
|
|
837
|
+
],
|
|
838
|
+
[
|
|
839
|
+
MATGOV_SEND_LIFECYCLE_V2.SENDING,
|
|
840
|
+
new Set([
|
|
841
|
+
MATGOV_SEND_LIFECYCLE_V2.SENT,
|
|
842
|
+
MATGOV_SEND_LIFECYCLE_V2.FAILED,
|
|
843
|
+
MATGOV_SEND_LIFECYCLE_V2.CANCELLED,
|
|
844
|
+
]),
|
|
845
|
+
],
|
|
846
|
+
[MATGOV_SEND_LIFECYCLE_V2.SENT, new Set()],
|
|
847
|
+
[MATGOV_SEND_LIFECYCLE_V2.FAILED, new Set()],
|
|
848
|
+
[MATGOV_SEND_LIFECYCLE_V2.CANCELLED, new Set()],
|
|
849
|
+
]);
|
|
850
|
+
const _matgovPsV2 = new Map();
|
|
851
|
+
const _matgovJsV2 = new Map();
|
|
852
|
+
let _matgovMaxActive = 6,
|
|
853
|
+
_matgovMaxPending = 20,
|
|
854
|
+
_matgovIdleMs = 30 * 24 * 60 * 60 * 1000,
|
|
855
|
+
_matgovStuckMs = 60 * 1000;
|
|
856
|
+
function _matgovPos(n, label) {
|
|
857
|
+
const v = Math.floor(Number(n));
|
|
858
|
+
if (!Number.isFinite(v) || v <= 0)
|
|
859
|
+
throw new Error(`${label} must be positive integer`);
|
|
860
|
+
return v;
|
|
861
|
+
}
|
|
862
|
+
function _matgovCheckP(from, to) {
|
|
863
|
+
const a = _matgovPTrans.get(from);
|
|
864
|
+
if (!a || !a.has(to))
|
|
865
|
+
throw new Error(`invalid matgov profile transition ${from} → ${to}`);
|
|
866
|
+
}
|
|
867
|
+
function _matgovCheckJ(from, to) {
|
|
868
|
+
const a = _matgovJTrans.get(from);
|
|
869
|
+
if (!a || !a.has(to))
|
|
870
|
+
throw new Error(`invalid matgov send transition ${from} → ${to}`);
|
|
871
|
+
}
|
|
872
|
+
function _matgovCountActive(owner) {
|
|
873
|
+
let c = 0;
|
|
874
|
+
for (const p of _matgovPsV2.values())
|
|
875
|
+
if (p.owner === owner && p.status === MATGOV_PROFILE_MATURITY_V2.ACTIVE)
|
|
876
|
+
c++;
|
|
877
|
+
return c;
|
|
878
|
+
}
|
|
879
|
+
function _matgovCountPending(profileId) {
|
|
880
|
+
let c = 0;
|
|
881
|
+
for (const j of _matgovJsV2.values())
|
|
882
|
+
if (
|
|
883
|
+
j.profileId === profileId &&
|
|
884
|
+
(j.status === MATGOV_SEND_LIFECYCLE_V2.QUEUED ||
|
|
885
|
+
j.status === MATGOV_SEND_LIFECYCLE_V2.SENDING)
|
|
886
|
+
)
|
|
887
|
+
c++;
|
|
888
|
+
return c;
|
|
889
|
+
}
|
|
890
|
+
export function setMaxActiveMatgovProfilesPerOwnerV2(n) {
|
|
891
|
+
_matgovMaxActive = _matgovPos(n, "maxActiveMatgovProfilesPerOwner");
|
|
892
|
+
}
|
|
893
|
+
export function getMaxActiveMatgovProfilesPerOwnerV2() {
|
|
894
|
+
return _matgovMaxActive;
|
|
895
|
+
}
|
|
896
|
+
export function setMaxPendingMatgovSendsPerProfileV2(n) {
|
|
897
|
+
_matgovMaxPending = _matgovPos(n, "maxPendingMatgovSendsPerProfile");
|
|
898
|
+
}
|
|
899
|
+
export function getMaxPendingMatgovSendsPerProfileV2() {
|
|
900
|
+
return _matgovMaxPending;
|
|
901
|
+
}
|
|
902
|
+
export function setMatgovProfileIdleMsV2(n) {
|
|
903
|
+
_matgovIdleMs = _matgovPos(n, "matgovProfileIdleMs");
|
|
904
|
+
}
|
|
905
|
+
export function getMatgovProfileIdleMsV2() {
|
|
906
|
+
return _matgovIdleMs;
|
|
907
|
+
}
|
|
908
|
+
export function setMatgovSendStuckMsV2(n) {
|
|
909
|
+
_matgovStuckMs = _matgovPos(n, "matgovSendStuckMs");
|
|
910
|
+
}
|
|
911
|
+
export function getMatgovSendStuckMsV2() {
|
|
912
|
+
return _matgovStuckMs;
|
|
913
|
+
}
|
|
914
|
+
export function _resetStateMatrixBridgeGovV2() {
|
|
915
|
+
_matgovPsV2.clear();
|
|
916
|
+
_matgovJsV2.clear();
|
|
917
|
+
_matgovMaxActive = 6;
|
|
918
|
+
_matgovMaxPending = 20;
|
|
919
|
+
_matgovIdleMs = 30 * 24 * 60 * 60 * 1000;
|
|
920
|
+
_matgovStuckMs = 60 * 1000;
|
|
921
|
+
}
|
|
922
|
+
export function registerMatgovProfileV2({
|
|
923
|
+
id,
|
|
924
|
+
owner,
|
|
925
|
+
homeserver,
|
|
926
|
+
metadata,
|
|
927
|
+
} = {}) {
|
|
928
|
+
if (!id || !owner) throw new Error("id and owner required");
|
|
929
|
+
if (_matgovPsV2.has(id))
|
|
930
|
+
throw new Error(`matgov profile ${id} already exists`);
|
|
931
|
+
const now = Date.now();
|
|
932
|
+
const p = {
|
|
933
|
+
id,
|
|
934
|
+
owner,
|
|
935
|
+
homeserver: homeserver || "matrix.org",
|
|
936
|
+
status: MATGOV_PROFILE_MATURITY_V2.PENDING,
|
|
937
|
+
createdAt: now,
|
|
938
|
+
updatedAt: now,
|
|
939
|
+
lastTouchedAt: now,
|
|
940
|
+
activatedAt: null,
|
|
941
|
+
archivedAt: null,
|
|
942
|
+
metadata: { ...(metadata || {}) },
|
|
943
|
+
};
|
|
944
|
+
_matgovPsV2.set(id, p);
|
|
945
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
946
|
+
}
|
|
947
|
+
export function activateMatgovProfileV2(id) {
|
|
948
|
+
const p = _matgovPsV2.get(id);
|
|
949
|
+
if (!p) throw new Error(`matgov profile ${id} not found`);
|
|
950
|
+
const isInitial = p.status === MATGOV_PROFILE_MATURITY_V2.PENDING;
|
|
951
|
+
_matgovCheckP(p.status, MATGOV_PROFILE_MATURITY_V2.ACTIVE);
|
|
952
|
+
if (isInitial && _matgovCountActive(p.owner) >= _matgovMaxActive)
|
|
953
|
+
throw new Error(`max active matgov profiles for owner ${p.owner} reached`);
|
|
954
|
+
const now = Date.now();
|
|
955
|
+
p.status = MATGOV_PROFILE_MATURITY_V2.ACTIVE;
|
|
956
|
+
p.updatedAt = now;
|
|
957
|
+
p.lastTouchedAt = now;
|
|
958
|
+
if (!p.activatedAt) p.activatedAt = now;
|
|
959
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
960
|
+
}
|
|
961
|
+
export function suspendMatgovProfileV2(id) {
|
|
962
|
+
const p = _matgovPsV2.get(id);
|
|
963
|
+
if (!p) throw new Error(`matgov profile ${id} not found`);
|
|
964
|
+
_matgovCheckP(p.status, MATGOV_PROFILE_MATURITY_V2.SUSPENDED);
|
|
965
|
+
p.status = MATGOV_PROFILE_MATURITY_V2.SUSPENDED;
|
|
966
|
+
p.updatedAt = Date.now();
|
|
967
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
968
|
+
}
|
|
969
|
+
export function archiveMatgovProfileV2(id) {
|
|
970
|
+
const p = _matgovPsV2.get(id);
|
|
971
|
+
if (!p) throw new Error(`matgov profile ${id} not found`);
|
|
972
|
+
_matgovCheckP(p.status, MATGOV_PROFILE_MATURITY_V2.ARCHIVED);
|
|
973
|
+
const now = Date.now();
|
|
974
|
+
p.status = MATGOV_PROFILE_MATURITY_V2.ARCHIVED;
|
|
975
|
+
p.updatedAt = now;
|
|
976
|
+
if (!p.archivedAt) p.archivedAt = now;
|
|
977
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
978
|
+
}
|
|
979
|
+
export function touchMatgovProfileV2(id) {
|
|
980
|
+
const p = _matgovPsV2.get(id);
|
|
981
|
+
if (!p) throw new Error(`matgov profile ${id} not found`);
|
|
982
|
+
if (_matgovPTerminal.has(p.status))
|
|
983
|
+
throw new Error(`cannot touch terminal matgov profile ${id}`);
|
|
984
|
+
const now = Date.now();
|
|
985
|
+
p.lastTouchedAt = now;
|
|
986
|
+
p.updatedAt = now;
|
|
987
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
988
|
+
}
|
|
989
|
+
export function getMatgovProfileV2(id) {
|
|
990
|
+
const p = _matgovPsV2.get(id);
|
|
991
|
+
if (!p) return null;
|
|
992
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
993
|
+
}
|
|
994
|
+
export function listMatgovProfilesV2() {
|
|
995
|
+
return [..._matgovPsV2.values()].map((p) => ({
|
|
996
|
+
...p,
|
|
997
|
+
metadata: { ...p.metadata },
|
|
998
|
+
}));
|
|
999
|
+
}
|
|
1000
|
+
export function createMatgovSendV2({ id, profileId, room, metadata } = {}) {
|
|
1001
|
+
if (!id || !profileId) throw new Error("id and profileId required");
|
|
1002
|
+
if (_matgovJsV2.has(id)) throw new Error(`matgov send ${id} already exists`);
|
|
1003
|
+
if (!_matgovPsV2.has(profileId))
|
|
1004
|
+
throw new Error(`matgov profile ${profileId} not found`);
|
|
1005
|
+
if (_matgovCountPending(profileId) >= _matgovMaxPending)
|
|
1006
|
+
throw new Error(
|
|
1007
|
+
`max pending matgov sends for profile ${profileId} reached`,
|
|
1008
|
+
);
|
|
1009
|
+
const now = Date.now();
|
|
1010
|
+
const j = {
|
|
1011
|
+
id,
|
|
1012
|
+
profileId,
|
|
1013
|
+
room: room || "",
|
|
1014
|
+
status: MATGOV_SEND_LIFECYCLE_V2.QUEUED,
|
|
1015
|
+
createdAt: now,
|
|
1016
|
+
updatedAt: now,
|
|
1017
|
+
startedAt: null,
|
|
1018
|
+
settledAt: null,
|
|
1019
|
+
metadata: { ...(metadata || {}) },
|
|
1020
|
+
};
|
|
1021
|
+
_matgovJsV2.set(id, j);
|
|
1022
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
1023
|
+
}
|
|
1024
|
+
export function sendingMatgovSendV2(id) {
|
|
1025
|
+
const j = _matgovJsV2.get(id);
|
|
1026
|
+
if (!j) throw new Error(`matgov send ${id} not found`);
|
|
1027
|
+
_matgovCheckJ(j.status, MATGOV_SEND_LIFECYCLE_V2.SENDING);
|
|
1028
|
+
const now = Date.now();
|
|
1029
|
+
j.status = MATGOV_SEND_LIFECYCLE_V2.SENDING;
|
|
1030
|
+
j.updatedAt = now;
|
|
1031
|
+
if (!j.startedAt) j.startedAt = now;
|
|
1032
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
1033
|
+
}
|
|
1034
|
+
export function completeSendMatgovV2(id) {
|
|
1035
|
+
const j = _matgovJsV2.get(id);
|
|
1036
|
+
if (!j) throw new Error(`matgov send ${id} not found`);
|
|
1037
|
+
_matgovCheckJ(j.status, MATGOV_SEND_LIFECYCLE_V2.SENT);
|
|
1038
|
+
const now = Date.now();
|
|
1039
|
+
j.status = MATGOV_SEND_LIFECYCLE_V2.SENT;
|
|
1040
|
+
j.updatedAt = now;
|
|
1041
|
+
if (!j.settledAt) j.settledAt = now;
|
|
1042
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
1043
|
+
}
|
|
1044
|
+
export function failMatgovSendV2(id, reason) {
|
|
1045
|
+
const j = _matgovJsV2.get(id);
|
|
1046
|
+
if (!j) throw new Error(`matgov send ${id} not found`);
|
|
1047
|
+
_matgovCheckJ(j.status, MATGOV_SEND_LIFECYCLE_V2.FAILED);
|
|
1048
|
+
const now = Date.now();
|
|
1049
|
+
j.status = MATGOV_SEND_LIFECYCLE_V2.FAILED;
|
|
1050
|
+
j.updatedAt = now;
|
|
1051
|
+
if (!j.settledAt) j.settledAt = now;
|
|
1052
|
+
if (reason) j.metadata.failReason = String(reason);
|
|
1053
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
1054
|
+
}
|
|
1055
|
+
export function cancelMatgovSendV2(id, reason) {
|
|
1056
|
+
const j = _matgovJsV2.get(id);
|
|
1057
|
+
if (!j) throw new Error(`matgov send ${id} not found`);
|
|
1058
|
+
_matgovCheckJ(j.status, MATGOV_SEND_LIFECYCLE_V2.CANCELLED);
|
|
1059
|
+
const now = Date.now();
|
|
1060
|
+
j.status = MATGOV_SEND_LIFECYCLE_V2.CANCELLED;
|
|
1061
|
+
j.updatedAt = now;
|
|
1062
|
+
if (!j.settledAt) j.settledAt = now;
|
|
1063
|
+
if (reason) j.metadata.cancelReason = String(reason);
|
|
1064
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
1065
|
+
}
|
|
1066
|
+
export function getMatgovSendV2(id) {
|
|
1067
|
+
const j = _matgovJsV2.get(id);
|
|
1068
|
+
if (!j) return null;
|
|
1069
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
1070
|
+
}
|
|
1071
|
+
export function listMatgovSendsV2() {
|
|
1072
|
+
return [..._matgovJsV2.values()].map((j) => ({
|
|
1073
|
+
...j,
|
|
1074
|
+
metadata: { ...j.metadata },
|
|
1075
|
+
}));
|
|
1076
|
+
}
|
|
1077
|
+
export function autoSuspendIdleMatgovProfilesV2({ now } = {}) {
|
|
1078
|
+
const t = now ?? Date.now();
|
|
1079
|
+
const flipped = [];
|
|
1080
|
+
for (const p of _matgovPsV2.values())
|
|
1081
|
+
if (
|
|
1082
|
+
p.status === MATGOV_PROFILE_MATURITY_V2.ACTIVE &&
|
|
1083
|
+
t - p.lastTouchedAt >= _matgovIdleMs
|
|
1084
|
+
) {
|
|
1085
|
+
p.status = MATGOV_PROFILE_MATURITY_V2.SUSPENDED;
|
|
1086
|
+
p.updatedAt = t;
|
|
1087
|
+
flipped.push(p.id);
|
|
1088
|
+
}
|
|
1089
|
+
return { flipped, count: flipped.length };
|
|
1090
|
+
}
|
|
1091
|
+
export function autoFailStuckMatgovSendsV2({ now } = {}) {
|
|
1092
|
+
const t = now ?? Date.now();
|
|
1093
|
+
const flipped = [];
|
|
1094
|
+
for (const j of _matgovJsV2.values())
|
|
1095
|
+
if (
|
|
1096
|
+
j.status === MATGOV_SEND_LIFECYCLE_V2.SENDING &&
|
|
1097
|
+
j.startedAt != null &&
|
|
1098
|
+
t - j.startedAt >= _matgovStuckMs
|
|
1099
|
+
) {
|
|
1100
|
+
j.status = MATGOV_SEND_LIFECYCLE_V2.FAILED;
|
|
1101
|
+
j.updatedAt = t;
|
|
1102
|
+
if (!j.settledAt) j.settledAt = t;
|
|
1103
|
+
j.metadata.failReason = "auto-fail-stuck";
|
|
1104
|
+
flipped.push(j.id);
|
|
1105
|
+
}
|
|
1106
|
+
return { flipped, count: flipped.length };
|
|
1107
|
+
}
|
|
1108
|
+
export function getMatrixBridgeGovStatsV2() {
|
|
1109
|
+
const profilesByStatus = {};
|
|
1110
|
+
for (const v of Object.values(MATGOV_PROFILE_MATURITY_V2))
|
|
1111
|
+
profilesByStatus[v] = 0;
|
|
1112
|
+
for (const p of _matgovPsV2.values()) profilesByStatus[p.status]++;
|
|
1113
|
+
const sendsByStatus = {};
|
|
1114
|
+
for (const v of Object.values(MATGOV_SEND_LIFECYCLE_V2)) sendsByStatus[v] = 0;
|
|
1115
|
+
for (const j of _matgovJsV2.values()) sendsByStatus[j.status]++;
|
|
1116
|
+
return {
|
|
1117
|
+
totalMatgovProfilesV2: _matgovPsV2.size,
|
|
1118
|
+
totalMatgovSendsV2: _matgovJsV2.size,
|
|
1119
|
+
maxActiveMatgovProfilesPerOwner: _matgovMaxActive,
|
|
1120
|
+
maxPendingMatgovSendsPerProfile: _matgovMaxPending,
|
|
1121
|
+
matgovProfileIdleMs: _matgovIdleMs,
|
|
1122
|
+
matgovSendStuckMs: _matgovStuckMs,
|
|
1123
|
+
profilesByStatus,
|
|
1124
|
+
sendsByStatus,
|
|
1125
|
+
};
|
|
1126
|
+
}
|