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.
- 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
|
@@ -707,3 +707,337 @@ export function getAgentCoordinatorStatsV2() {
|
|
|
707
707
|
assignmentsByStatus,
|
|
708
708
|
};
|
|
709
709
|
}
|
|
710
|
+
|
|
711
|
+
// === Iter28 V2 governance overlay: Acrdgov ===
|
|
712
|
+
export const ACRDGOV_PROFILE_MATURITY_V2 = Object.freeze({
|
|
713
|
+
PENDING: "pending",
|
|
714
|
+
ACTIVE: "active",
|
|
715
|
+
IDLE: "idle",
|
|
716
|
+
ARCHIVED: "archived",
|
|
717
|
+
});
|
|
718
|
+
export const ACRDGOV_COORD_LIFECYCLE_V2 = Object.freeze({
|
|
719
|
+
QUEUED: "queued",
|
|
720
|
+
COORDINATING: "coordinating",
|
|
721
|
+
COORDINATED: "coordinated",
|
|
722
|
+
FAILED: "failed",
|
|
723
|
+
CANCELLED: "cancelled",
|
|
724
|
+
});
|
|
725
|
+
const _acrdgovPTrans = new Map([
|
|
726
|
+
[
|
|
727
|
+
ACRDGOV_PROFILE_MATURITY_V2.PENDING,
|
|
728
|
+
new Set([
|
|
729
|
+
ACRDGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
730
|
+
ACRDGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
731
|
+
]),
|
|
732
|
+
],
|
|
733
|
+
[
|
|
734
|
+
ACRDGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
735
|
+
new Set([
|
|
736
|
+
ACRDGOV_PROFILE_MATURITY_V2.IDLE,
|
|
737
|
+
ACRDGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
738
|
+
]),
|
|
739
|
+
],
|
|
740
|
+
[
|
|
741
|
+
ACRDGOV_PROFILE_MATURITY_V2.IDLE,
|
|
742
|
+
new Set([
|
|
743
|
+
ACRDGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
744
|
+
ACRDGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
745
|
+
]),
|
|
746
|
+
],
|
|
747
|
+
[ACRDGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
|
|
748
|
+
]);
|
|
749
|
+
const _acrdgovPTerminal = new Set([ACRDGOV_PROFILE_MATURITY_V2.ARCHIVED]);
|
|
750
|
+
const _acrdgovJTrans = new Map([
|
|
751
|
+
[
|
|
752
|
+
ACRDGOV_COORD_LIFECYCLE_V2.QUEUED,
|
|
753
|
+
new Set([
|
|
754
|
+
ACRDGOV_COORD_LIFECYCLE_V2.COORDINATING,
|
|
755
|
+
ACRDGOV_COORD_LIFECYCLE_V2.CANCELLED,
|
|
756
|
+
]),
|
|
757
|
+
],
|
|
758
|
+
[
|
|
759
|
+
ACRDGOV_COORD_LIFECYCLE_V2.COORDINATING,
|
|
760
|
+
new Set([
|
|
761
|
+
ACRDGOV_COORD_LIFECYCLE_V2.COORDINATED,
|
|
762
|
+
ACRDGOV_COORD_LIFECYCLE_V2.FAILED,
|
|
763
|
+
ACRDGOV_COORD_LIFECYCLE_V2.CANCELLED,
|
|
764
|
+
]),
|
|
765
|
+
],
|
|
766
|
+
[ACRDGOV_COORD_LIFECYCLE_V2.COORDINATED, new Set()],
|
|
767
|
+
[ACRDGOV_COORD_LIFECYCLE_V2.FAILED, new Set()],
|
|
768
|
+
[ACRDGOV_COORD_LIFECYCLE_V2.CANCELLED, new Set()],
|
|
769
|
+
]);
|
|
770
|
+
const _acrdgovPsV2 = new Map();
|
|
771
|
+
const _acrdgovJsV2 = new Map();
|
|
772
|
+
let _acrdgovMaxActive = 6,
|
|
773
|
+
_acrdgovMaxPending = 15,
|
|
774
|
+
_acrdgovIdleMs = 2592000000,
|
|
775
|
+
_acrdgovStuckMs = 60 * 1000;
|
|
776
|
+
function _acrdgovPos(n, label) {
|
|
777
|
+
const v = Math.floor(Number(n));
|
|
778
|
+
if (!Number.isFinite(v) || v <= 0)
|
|
779
|
+
throw new Error(`${label} must be positive integer`);
|
|
780
|
+
return v;
|
|
781
|
+
}
|
|
782
|
+
function _acrdgovCheckP(from, to) {
|
|
783
|
+
const a = _acrdgovPTrans.get(from);
|
|
784
|
+
if (!a || !a.has(to))
|
|
785
|
+
throw new Error(`invalid acrdgov profile transition ${from} → ${to}`);
|
|
786
|
+
}
|
|
787
|
+
function _acrdgovCheckJ(from, to) {
|
|
788
|
+
const a = _acrdgovJTrans.get(from);
|
|
789
|
+
if (!a || !a.has(to))
|
|
790
|
+
throw new Error(`invalid acrdgov coord transition ${from} → ${to}`);
|
|
791
|
+
}
|
|
792
|
+
function _acrdgovCountActive(owner) {
|
|
793
|
+
let c = 0;
|
|
794
|
+
for (const p of _acrdgovPsV2.values())
|
|
795
|
+
if (p.owner === owner && p.status === ACRDGOV_PROFILE_MATURITY_V2.ACTIVE)
|
|
796
|
+
c++;
|
|
797
|
+
return c;
|
|
798
|
+
}
|
|
799
|
+
function _acrdgovCountPending(profileId) {
|
|
800
|
+
let c = 0;
|
|
801
|
+
for (const j of _acrdgovJsV2.values())
|
|
802
|
+
if (
|
|
803
|
+
j.profileId === profileId &&
|
|
804
|
+
(j.status === ACRDGOV_COORD_LIFECYCLE_V2.QUEUED ||
|
|
805
|
+
j.status === ACRDGOV_COORD_LIFECYCLE_V2.COORDINATING)
|
|
806
|
+
)
|
|
807
|
+
c++;
|
|
808
|
+
return c;
|
|
809
|
+
}
|
|
810
|
+
export function setMaxActiveAcrdProfilesPerOwnerV2(n) {
|
|
811
|
+
_acrdgovMaxActive = _acrdgovPos(n, "maxActiveAcrdProfilesPerOwner");
|
|
812
|
+
}
|
|
813
|
+
export function getMaxActiveAcrdProfilesPerOwnerV2() {
|
|
814
|
+
return _acrdgovMaxActive;
|
|
815
|
+
}
|
|
816
|
+
export function setMaxPendingAcrdCoordsPerProfileV2(n) {
|
|
817
|
+
_acrdgovMaxPending = _acrdgovPos(n, "maxPendingAcrdCoordsPerProfile");
|
|
818
|
+
}
|
|
819
|
+
export function getMaxPendingAcrdCoordsPerProfileV2() {
|
|
820
|
+
return _acrdgovMaxPending;
|
|
821
|
+
}
|
|
822
|
+
export function setAcrdProfileIdleMsV2(n) {
|
|
823
|
+
_acrdgovIdleMs = _acrdgovPos(n, "acrdgovProfileIdleMs");
|
|
824
|
+
}
|
|
825
|
+
export function getAcrdProfileIdleMsV2() {
|
|
826
|
+
return _acrdgovIdleMs;
|
|
827
|
+
}
|
|
828
|
+
export function setAcrdCoordStuckMsV2(n) {
|
|
829
|
+
_acrdgovStuckMs = _acrdgovPos(n, "acrdgovCoordStuckMs");
|
|
830
|
+
}
|
|
831
|
+
export function getAcrdCoordStuckMsV2() {
|
|
832
|
+
return _acrdgovStuckMs;
|
|
833
|
+
}
|
|
834
|
+
export function _resetStateAcrdgovV2() {
|
|
835
|
+
_acrdgovPsV2.clear();
|
|
836
|
+
_acrdgovJsV2.clear();
|
|
837
|
+
_acrdgovMaxActive = 6;
|
|
838
|
+
_acrdgovMaxPending = 15;
|
|
839
|
+
_acrdgovIdleMs = 2592000000;
|
|
840
|
+
_acrdgovStuckMs = 60 * 1000;
|
|
841
|
+
}
|
|
842
|
+
export function registerAcrdProfileV2({ id, owner, role, metadata } = {}) {
|
|
843
|
+
if (!id || !owner) throw new Error("id and owner required");
|
|
844
|
+
if (_acrdgovPsV2.has(id))
|
|
845
|
+
throw new Error(`acrdgov profile ${id} already exists`);
|
|
846
|
+
const now = Date.now();
|
|
847
|
+
const p = {
|
|
848
|
+
id,
|
|
849
|
+
owner,
|
|
850
|
+
role: role || "leader",
|
|
851
|
+
status: ACRDGOV_PROFILE_MATURITY_V2.PENDING,
|
|
852
|
+
createdAt: now,
|
|
853
|
+
updatedAt: now,
|
|
854
|
+
lastTouchedAt: now,
|
|
855
|
+
activatedAt: null,
|
|
856
|
+
archivedAt: null,
|
|
857
|
+
metadata: { ...(metadata || {}) },
|
|
858
|
+
};
|
|
859
|
+
_acrdgovPsV2.set(id, p);
|
|
860
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
861
|
+
}
|
|
862
|
+
export function activateAcrdProfileV2(id) {
|
|
863
|
+
const p = _acrdgovPsV2.get(id);
|
|
864
|
+
if (!p) throw new Error(`acrdgov profile ${id} not found`);
|
|
865
|
+
const isInitial = p.status === ACRDGOV_PROFILE_MATURITY_V2.PENDING;
|
|
866
|
+
_acrdgovCheckP(p.status, ACRDGOV_PROFILE_MATURITY_V2.ACTIVE);
|
|
867
|
+
if (isInitial && _acrdgovCountActive(p.owner) >= _acrdgovMaxActive)
|
|
868
|
+
throw new Error(`max active acrdgov profiles for owner ${p.owner} reached`);
|
|
869
|
+
const now = Date.now();
|
|
870
|
+
p.status = ACRDGOV_PROFILE_MATURITY_V2.ACTIVE;
|
|
871
|
+
p.updatedAt = now;
|
|
872
|
+
p.lastTouchedAt = now;
|
|
873
|
+
if (!p.activatedAt) p.activatedAt = now;
|
|
874
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
875
|
+
}
|
|
876
|
+
export function idleAcrdProfileV2(id) {
|
|
877
|
+
const p = _acrdgovPsV2.get(id);
|
|
878
|
+
if (!p) throw new Error(`acrdgov profile ${id} not found`);
|
|
879
|
+
_acrdgovCheckP(p.status, ACRDGOV_PROFILE_MATURITY_V2.IDLE);
|
|
880
|
+
p.status = ACRDGOV_PROFILE_MATURITY_V2.IDLE;
|
|
881
|
+
p.updatedAt = Date.now();
|
|
882
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
883
|
+
}
|
|
884
|
+
export function archiveAcrdProfileV2(id) {
|
|
885
|
+
const p = _acrdgovPsV2.get(id);
|
|
886
|
+
if (!p) throw new Error(`acrdgov profile ${id} not found`);
|
|
887
|
+
_acrdgovCheckP(p.status, ACRDGOV_PROFILE_MATURITY_V2.ARCHIVED);
|
|
888
|
+
const now = Date.now();
|
|
889
|
+
p.status = ACRDGOV_PROFILE_MATURITY_V2.ARCHIVED;
|
|
890
|
+
p.updatedAt = now;
|
|
891
|
+
if (!p.archivedAt) p.archivedAt = now;
|
|
892
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
893
|
+
}
|
|
894
|
+
export function touchAcrdProfileV2(id) {
|
|
895
|
+
const p = _acrdgovPsV2.get(id);
|
|
896
|
+
if (!p) throw new Error(`acrdgov profile ${id} not found`);
|
|
897
|
+
if (_acrdgovPTerminal.has(p.status))
|
|
898
|
+
throw new Error(`cannot touch terminal acrdgov profile ${id}`);
|
|
899
|
+
const now = Date.now();
|
|
900
|
+
p.lastTouchedAt = now;
|
|
901
|
+
p.updatedAt = now;
|
|
902
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
903
|
+
}
|
|
904
|
+
export function getAcrdProfileV2(id) {
|
|
905
|
+
const p = _acrdgovPsV2.get(id);
|
|
906
|
+
if (!p) return null;
|
|
907
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
908
|
+
}
|
|
909
|
+
export function listAcrdProfilesV2() {
|
|
910
|
+
return [..._acrdgovPsV2.values()].map((p) => ({
|
|
911
|
+
...p,
|
|
912
|
+
metadata: { ...p.metadata },
|
|
913
|
+
}));
|
|
914
|
+
}
|
|
915
|
+
export function createAcrdCoordV2({ id, profileId, taskId, metadata } = {}) {
|
|
916
|
+
if (!id || !profileId) throw new Error("id and profileId required");
|
|
917
|
+
if (_acrdgovJsV2.has(id))
|
|
918
|
+
throw new Error(`acrdgov coord ${id} already exists`);
|
|
919
|
+
if (!_acrdgovPsV2.has(profileId))
|
|
920
|
+
throw new Error(`acrdgov profile ${profileId} not found`);
|
|
921
|
+
if (_acrdgovCountPending(profileId) >= _acrdgovMaxPending)
|
|
922
|
+
throw new Error(
|
|
923
|
+
`max pending acrdgov coords for profile ${profileId} reached`,
|
|
924
|
+
);
|
|
925
|
+
const now = Date.now();
|
|
926
|
+
const j = {
|
|
927
|
+
id,
|
|
928
|
+
profileId,
|
|
929
|
+
taskId: taskId || "",
|
|
930
|
+
status: ACRDGOV_COORD_LIFECYCLE_V2.QUEUED,
|
|
931
|
+
createdAt: now,
|
|
932
|
+
updatedAt: now,
|
|
933
|
+
startedAt: null,
|
|
934
|
+
settledAt: null,
|
|
935
|
+
metadata: { ...(metadata || {}) },
|
|
936
|
+
};
|
|
937
|
+
_acrdgovJsV2.set(id, j);
|
|
938
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
939
|
+
}
|
|
940
|
+
export function coordinatingAcrdCoordV2(id) {
|
|
941
|
+
const j = _acrdgovJsV2.get(id);
|
|
942
|
+
if (!j) throw new Error(`acrdgov coord ${id} not found`);
|
|
943
|
+
_acrdgovCheckJ(j.status, ACRDGOV_COORD_LIFECYCLE_V2.COORDINATING);
|
|
944
|
+
const now = Date.now();
|
|
945
|
+
j.status = ACRDGOV_COORD_LIFECYCLE_V2.COORDINATING;
|
|
946
|
+
j.updatedAt = now;
|
|
947
|
+
if (!j.startedAt) j.startedAt = now;
|
|
948
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
949
|
+
}
|
|
950
|
+
export function completeCoordAcrdV2(id) {
|
|
951
|
+
const j = _acrdgovJsV2.get(id);
|
|
952
|
+
if (!j) throw new Error(`acrdgov coord ${id} not found`);
|
|
953
|
+
_acrdgovCheckJ(j.status, ACRDGOV_COORD_LIFECYCLE_V2.COORDINATED);
|
|
954
|
+
const now = Date.now();
|
|
955
|
+
j.status = ACRDGOV_COORD_LIFECYCLE_V2.COORDINATED;
|
|
956
|
+
j.updatedAt = now;
|
|
957
|
+
if (!j.settledAt) j.settledAt = now;
|
|
958
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
959
|
+
}
|
|
960
|
+
export function failAcrdCoordV2(id, reason) {
|
|
961
|
+
const j = _acrdgovJsV2.get(id);
|
|
962
|
+
if (!j) throw new Error(`acrdgov coord ${id} not found`);
|
|
963
|
+
_acrdgovCheckJ(j.status, ACRDGOV_COORD_LIFECYCLE_V2.FAILED);
|
|
964
|
+
const now = Date.now();
|
|
965
|
+
j.status = ACRDGOV_COORD_LIFECYCLE_V2.FAILED;
|
|
966
|
+
j.updatedAt = now;
|
|
967
|
+
if (!j.settledAt) j.settledAt = now;
|
|
968
|
+
if (reason) j.metadata.failReason = String(reason);
|
|
969
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
970
|
+
}
|
|
971
|
+
export function cancelAcrdCoordV2(id, reason) {
|
|
972
|
+
const j = _acrdgovJsV2.get(id);
|
|
973
|
+
if (!j) throw new Error(`acrdgov coord ${id} not found`);
|
|
974
|
+
_acrdgovCheckJ(j.status, ACRDGOV_COORD_LIFECYCLE_V2.CANCELLED);
|
|
975
|
+
const now = Date.now();
|
|
976
|
+
j.status = ACRDGOV_COORD_LIFECYCLE_V2.CANCELLED;
|
|
977
|
+
j.updatedAt = now;
|
|
978
|
+
if (!j.settledAt) j.settledAt = now;
|
|
979
|
+
if (reason) j.metadata.cancelReason = String(reason);
|
|
980
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
981
|
+
}
|
|
982
|
+
export function getAcrdCoordV2(id) {
|
|
983
|
+
const j = _acrdgovJsV2.get(id);
|
|
984
|
+
if (!j) return null;
|
|
985
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
986
|
+
}
|
|
987
|
+
export function listAcrdCoordsV2() {
|
|
988
|
+
return [..._acrdgovJsV2.values()].map((j) => ({
|
|
989
|
+
...j,
|
|
990
|
+
metadata: { ...j.metadata },
|
|
991
|
+
}));
|
|
992
|
+
}
|
|
993
|
+
export function autoIdleIdleAcrdProfilesV2({ now } = {}) {
|
|
994
|
+
const t = now ?? Date.now();
|
|
995
|
+
const flipped = [];
|
|
996
|
+
for (const p of _acrdgovPsV2.values())
|
|
997
|
+
if (
|
|
998
|
+
p.status === ACRDGOV_PROFILE_MATURITY_V2.ACTIVE &&
|
|
999
|
+
t - p.lastTouchedAt >= _acrdgovIdleMs
|
|
1000
|
+
) {
|
|
1001
|
+
p.status = ACRDGOV_PROFILE_MATURITY_V2.IDLE;
|
|
1002
|
+
p.updatedAt = t;
|
|
1003
|
+
flipped.push(p.id);
|
|
1004
|
+
}
|
|
1005
|
+
return { flipped, count: flipped.length };
|
|
1006
|
+
}
|
|
1007
|
+
export function autoFailStuckAcrdCoordsV2({ now } = {}) {
|
|
1008
|
+
const t = now ?? Date.now();
|
|
1009
|
+
const flipped = [];
|
|
1010
|
+
for (const j of _acrdgovJsV2.values())
|
|
1011
|
+
if (
|
|
1012
|
+
j.status === ACRDGOV_COORD_LIFECYCLE_V2.COORDINATING &&
|
|
1013
|
+
j.startedAt != null &&
|
|
1014
|
+
t - j.startedAt >= _acrdgovStuckMs
|
|
1015
|
+
) {
|
|
1016
|
+
j.status = ACRDGOV_COORD_LIFECYCLE_V2.FAILED;
|
|
1017
|
+
j.updatedAt = t;
|
|
1018
|
+
if (!j.settledAt) j.settledAt = t;
|
|
1019
|
+
j.metadata.failReason = "auto-fail-stuck";
|
|
1020
|
+
flipped.push(j.id);
|
|
1021
|
+
}
|
|
1022
|
+
return { flipped, count: flipped.length };
|
|
1023
|
+
}
|
|
1024
|
+
export function getAcrdgovStatsV2() {
|
|
1025
|
+
const profilesByStatus = {};
|
|
1026
|
+
for (const v of Object.values(ACRDGOV_PROFILE_MATURITY_V2))
|
|
1027
|
+
profilesByStatus[v] = 0;
|
|
1028
|
+
for (const p of _acrdgovPsV2.values()) profilesByStatus[p.status]++;
|
|
1029
|
+
const coordsByStatus = {};
|
|
1030
|
+
for (const v of Object.values(ACRDGOV_COORD_LIFECYCLE_V2))
|
|
1031
|
+
coordsByStatus[v] = 0;
|
|
1032
|
+
for (const j of _acrdgovJsV2.values()) coordsByStatus[j.status]++;
|
|
1033
|
+
return {
|
|
1034
|
+
totalAcrdProfilesV2: _acrdgovPsV2.size,
|
|
1035
|
+
totalAcrdCoordsV2: _acrdgovJsV2.size,
|
|
1036
|
+
maxActiveAcrdProfilesPerOwner: _acrdgovMaxActive,
|
|
1037
|
+
maxPendingAcrdCoordsPerProfile: _acrdgovMaxPending,
|
|
1038
|
+
acrdgovProfileIdleMs: _acrdgovIdleMs,
|
|
1039
|
+
acrdgovCoordStuckMs: _acrdgovStuckMs,
|
|
1040
|
+
profilesByStatus,
|
|
1041
|
+
coordsByStatus,
|
|
1042
|
+
};
|
|
1043
|
+
}
|
package/src/lib/agent-economy.js
CHANGED
|
@@ -1204,3 +1204,337 @@ export function getAgentEconomyGovStatsV2() {
|
|
|
1204
1204
|
txsByStatus,
|
|
1205
1205
|
};
|
|
1206
1206
|
}
|
|
1207
|
+
|
|
1208
|
+
// === Iter28 V2 governance overlay: Aecogov ===
|
|
1209
|
+
export const AECOGOV_PROFILE_MATURITY_V2 = Object.freeze({
|
|
1210
|
+
PENDING: "pending",
|
|
1211
|
+
ACTIVE: "active",
|
|
1212
|
+
PAUSED: "paused",
|
|
1213
|
+
ARCHIVED: "archived",
|
|
1214
|
+
});
|
|
1215
|
+
export const AECOGOV_TRADE_LIFECYCLE_V2 = Object.freeze({
|
|
1216
|
+
QUEUED: "queued",
|
|
1217
|
+
TRADING: "trading",
|
|
1218
|
+
SETTLED: "settled",
|
|
1219
|
+
FAILED: "failed",
|
|
1220
|
+
CANCELLED: "cancelled",
|
|
1221
|
+
});
|
|
1222
|
+
const _aecogovPTrans = new Map([
|
|
1223
|
+
[
|
|
1224
|
+
AECOGOV_PROFILE_MATURITY_V2.PENDING,
|
|
1225
|
+
new Set([
|
|
1226
|
+
AECOGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
1227
|
+
AECOGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
1228
|
+
]),
|
|
1229
|
+
],
|
|
1230
|
+
[
|
|
1231
|
+
AECOGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
1232
|
+
new Set([
|
|
1233
|
+
AECOGOV_PROFILE_MATURITY_V2.PAUSED,
|
|
1234
|
+
AECOGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
1235
|
+
]),
|
|
1236
|
+
],
|
|
1237
|
+
[
|
|
1238
|
+
AECOGOV_PROFILE_MATURITY_V2.PAUSED,
|
|
1239
|
+
new Set([
|
|
1240
|
+
AECOGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
1241
|
+
AECOGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
1242
|
+
]),
|
|
1243
|
+
],
|
|
1244
|
+
[AECOGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
|
|
1245
|
+
]);
|
|
1246
|
+
const _aecogovPTerminal = new Set([AECOGOV_PROFILE_MATURITY_V2.ARCHIVED]);
|
|
1247
|
+
const _aecogovJTrans = new Map([
|
|
1248
|
+
[
|
|
1249
|
+
AECOGOV_TRADE_LIFECYCLE_V2.QUEUED,
|
|
1250
|
+
new Set([
|
|
1251
|
+
AECOGOV_TRADE_LIFECYCLE_V2.TRADING,
|
|
1252
|
+
AECOGOV_TRADE_LIFECYCLE_V2.CANCELLED,
|
|
1253
|
+
]),
|
|
1254
|
+
],
|
|
1255
|
+
[
|
|
1256
|
+
AECOGOV_TRADE_LIFECYCLE_V2.TRADING,
|
|
1257
|
+
new Set([
|
|
1258
|
+
AECOGOV_TRADE_LIFECYCLE_V2.SETTLED,
|
|
1259
|
+
AECOGOV_TRADE_LIFECYCLE_V2.FAILED,
|
|
1260
|
+
AECOGOV_TRADE_LIFECYCLE_V2.CANCELLED,
|
|
1261
|
+
]),
|
|
1262
|
+
],
|
|
1263
|
+
[AECOGOV_TRADE_LIFECYCLE_V2.SETTLED, new Set()],
|
|
1264
|
+
[AECOGOV_TRADE_LIFECYCLE_V2.FAILED, new Set()],
|
|
1265
|
+
[AECOGOV_TRADE_LIFECYCLE_V2.CANCELLED, new Set()],
|
|
1266
|
+
]);
|
|
1267
|
+
const _aecogovPsV2 = new Map();
|
|
1268
|
+
const _aecogovJsV2 = new Map();
|
|
1269
|
+
let _aecogovMaxActive = 8,
|
|
1270
|
+
_aecogovMaxPending = 25,
|
|
1271
|
+
_aecogovIdleMs = 2592000000,
|
|
1272
|
+
_aecogovStuckMs = 60 * 1000;
|
|
1273
|
+
function _aecogovPos(n, label) {
|
|
1274
|
+
const v = Math.floor(Number(n));
|
|
1275
|
+
if (!Number.isFinite(v) || v <= 0)
|
|
1276
|
+
throw new Error(`${label} must be positive integer`);
|
|
1277
|
+
return v;
|
|
1278
|
+
}
|
|
1279
|
+
function _aecogovCheckP(from, to) {
|
|
1280
|
+
const a = _aecogovPTrans.get(from);
|
|
1281
|
+
if (!a || !a.has(to))
|
|
1282
|
+
throw new Error(`invalid aecogov profile transition ${from} → ${to}`);
|
|
1283
|
+
}
|
|
1284
|
+
function _aecogovCheckJ(from, to) {
|
|
1285
|
+
const a = _aecogovJTrans.get(from);
|
|
1286
|
+
if (!a || !a.has(to))
|
|
1287
|
+
throw new Error(`invalid aecogov trade transition ${from} → ${to}`);
|
|
1288
|
+
}
|
|
1289
|
+
function _aecogovCountActive(owner) {
|
|
1290
|
+
let c = 0;
|
|
1291
|
+
for (const p of _aecogovPsV2.values())
|
|
1292
|
+
if (p.owner === owner && p.status === AECOGOV_PROFILE_MATURITY_V2.ACTIVE)
|
|
1293
|
+
c++;
|
|
1294
|
+
return c;
|
|
1295
|
+
}
|
|
1296
|
+
function _aecogovCountPending(profileId) {
|
|
1297
|
+
let c = 0;
|
|
1298
|
+
for (const j of _aecogovJsV2.values())
|
|
1299
|
+
if (
|
|
1300
|
+
j.profileId === profileId &&
|
|
1301
|
+
(j.status === AECOGOV_TRADE_LIFECYCLE_V2.QUEUED ||
|
|
1302
|
+
j.status === AECOGOV_TRADE_LIFECYCLE_V2.TRADING)
|
|
1303
|
+
)
|
|
1304
|
+
c++;
|
|
1305
|
+
return c;
|
|
1306
|
+
}
|
|
1307
|
+
export function setMaxActiveAecoProfilesPerOwnerV2(n) {
|
|
1308
|
+
_aecogovMaxActive = _aecogovPos(n, "maxActiveAecoProfilesPerOwner");
|
|
1309
|
+
}
|
|
1310
|
+
export function getMaxActiveAecoProfilesPerOwnerV2() {
|
|
1311
|
+
return _aecogovMaxActive;
|
|
1312
|
+
}
|
|
1313
|
+
export function setMaxPendingAecoTradesPerProfileV2(n) {
|
|
1314
|
+
_aecogovMaxPending = _aecogovPos(n, "maxPendingAecoTradesPerProfile");
|
|
1315
|
+
}
|
|
1316
|
+
export function getMaxPendingAecoTradesPerProfileV2() {
|
|
1317
|
+
return _aecogovMaxPending;
|
|
1318
|
+
}
|
|
1319
|
+
export function setAecoProfileIdleMsV2(n) {
|
|
1320
|
+
_aecogovIdleMs = _aecogovPos(n, "aecogovProfileIdleMs");
|
|
1321
|
+
}
|
|
1322
|
+
export function getAecoProfileIdleMsV2() {
|
|
1323
|
+
return _aecogovIdleMs;
|
|
1324
|
+
}
|
|
1325
|
+
export function setAecoTradeStuckMsV2(n) {
|
|
1326
|
+
_aecogovStuckMs = _aecogovPos(n, "aecogovTradeStuckMs");
|
|
1327
|
+
}
|
|
1328
|
+
export function getAecoTradeStuckMsV2() {
|
|
1329
|
+
return _aecogovStuckMs;
|
|
1330
|
+
}
|
|
1331
|
+
export function _resetStateAecogovV2() {
|
|
1332
|
+
_aecogovPsV2.clear();
|
|
1333
|
+
_aecogovJsV2.clear();
|
|
1334
|
+
_aecogovMaxActive = 8;
|
|
1335
|
+
_aecogovMaxPending = 25;
|
|
1336
|
+
_aecogovIdleMs = 2592000000;
|
|
1337
|
+
_aecogovStuckMs = 60 * 1000;
|
|
1338
|
+
}
|
|
1339
|
+
export function registerAecoProfileV2({ id, owner, market, metadata } = {}) {
|
|
1340
|
+
if (!id || !owner) throw new Error("id and owner required");
|
|
1341
|
+
if (_aecogovPsV2.has(id))
|
|
1342
|
+
throw new Error(`aecogov profile ${id} already exists`);
|
|
1343
|
+
const now = Date.now();
|
|
1344
|
+
const p = {
|
|
1345
|
+
id,
|
|
1346
|
+
owner,
|
|
1347
|
+
market: market || "default",
|
|
1348
|
+
status: AECOGOV_PROFILE_MATURITY_V2.PENDING,
|
|
1349
|
+
createdAt: now,
|
|
1350
|
+
updatedAt: now,
|
|
1351
|
+
lastTouchedAt: now,
|
|
1352
|
+
activatedAt: null,
|
|
1353
|
+
archivedAt: null,
|
|
1354
|
+
metadata: { ...(metadata || {}) },
|
|
1355
|
+
};
|
|
1356
|
+
_aecogovPsV2.set(id, p);
|
|
1357
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
1358
|
+
}
|
|
1359
|
+
export function activateAecoProfileV2(id) {
|
|
1360
|
+
const p = _aecogovPsV2.get(id);
|
|
1361
|
+
if (!p) throw new Error(`aecogov profile ${id} not found`);
|
|
1362
|
+
const isInitial = p.status === AECOGOV_PROFILE_MATURITY_V2.PENDING;
|
|
1363
|
+
_aecogovCheckP(p.status, AECOGOV_PROFILE_MATURITY_V2.ACTIVE);
|
|
1364
|
+
if (isInitial && _aecogovCountActive(p.owner) >= _aecogovMaxActive)
|
|
1365
|
+
throw new Error(`max active aecogov profiles for owner ${p.owner} reached`);
|
|
1366
|
+
const now = Date.now();
|
|
1367
|
+
p.status = AECOGOV_PROFILE_MATURITY_V2.ACTIVE;
|
|
1368
|
+
p.updatedAt = now;
|
|
1369
|
+
p.lastTouchedAt = now;
|
|
1370
|
+
if (!p.activatedAt) p.activatedAt = now;
|
|
1371
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
1372
|
+
}
|
|
1373
|
+
export function pausedAecoProfileV2(id) {
|
|
1374
|
+
const p = _aecogovPsV2.get(id);
|
|
1375
|
+
if (!p) throw new Error(`aecogov profile ${id} not found`);
|
|
1376
|
+
_aecogovCheckP(p.status, AECOGOV_PROFILE_MATURITY_V2.PAUSED);
|
|
1377
|
+
p.status = AECOGOV_PROFILE_MATURITY_V2.PAUSED;
|
|
1378
|
+
p.updatedAt = Date.now();
|
|
1379
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
1380
|
+
}
|
|
1381
|
+
export function archiveAecoProfileV2(id) {
|
|
1382
|
+
const p = _aecogovPsV2.get(id);
|
|
1383
|
+
if (!p) throw new Error(`aecogov profile ${id} not found`);
|
|
1384
|
+
_aecogovCheckP(p.status, AECOGOV_PROFILE_MATURITY_V2.ARCHIVED);
|
|
1385
|
+
const now = Date.now();
|
|
1386
|
+
p.status = AECOGOV_PROFILE_MATURITY_V2.ARCHIVED;
|
|
1387
|
+
p.updatedAt = now;
|
|
1388
|
+
if (!p.archivedAt) p.archivedAt = now;
|
|
1389
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
1390
|
+
}
|
|
1391
|
+
export function touchAecoProfileV2(id) {
|
|
1392
|
+
const p = _aecogovPsV2.get(id);
|
|
1393
|
+
if (!p) throw new Error(`aecogov profile ${id} not found`);
|
|
1394
|
+
if (_aecogovPTerminal.has(p.status))
|
|
1395
|
+
throw new Error(`cannot touch terminal aecogov profile ${id}`);
|
|
1396
|
+
const now = Date.now();
|
|
1397
|
+
p.lastTouchedAt = now;
|
|
1398
|
+
p.updatedAt = now;
|
|
1399
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
1400
|
+
}
|
|
1401
|
+
export function getAecoProfileV2(id) {
|
|
1402
|
+
const p = _aecogovPsV2.get(id);
|
|
1403
|
+
if (!p) return null;
|
|
1404
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
1405
|
+
}
|
|
1406
|
+
export function listAecoProfilesV2() {
|
|
1407
|
+
return [..._aecogovPsV2.values()].map((p) => ({
|
|
1408
|
+
...p,
|
|
1409
|
+
metadata: { ...p.metadata },
|
|
1410
|
+
}));
|
|
1411
|
+
}
|
|
1412
|
+
export function createAecoTradeV2({ id, profileId, orderId, metadata } = {}) {
|
|
1413
|
+
if (!id || !profileId) throw new Error("id and profileId required");
|
|
1414
|
+
if (_aecogovJsV2.has(id))
|
|
1415
|
+
throw new Error(`aecogov trade ${id} already exists`);
|
|
1416
|
+
if (!_aecogovPsV2.has(profileId))
|
|
1417
|
+
throw new Error(`aecogov profile ${profileId} not found`);
|
|
1418
|
+
if (_aecogovCountPending(profileId) >= _aecogovMaxPending)
|
|
1419
|
+
throw new Error(
|
|
1420
|
+
`max pending aecogov trades for profile ${profileId} reached`,
|
|
1421
|
+
);
|
|
1422
|
+
const now = Date.now();
|
|
1423
|
+
const j = {
|
|
1424
|
+
id,
|
|
1425
|
+
profileId,
|
|
1426
|
+
orderId: orderId || "",
|
|
1427
|
+
status: AECOGOV_TRADE_LIFECYCLE_V2.QUEUED,
|
|
1428
|
+
createdAt: now,
|
|
1429
|
+
updatedAt: now,
|
|
1430
|
+
startedAt: null,
|
|
1431
|
+
settledAt: null,
|
|
1432
|
+
metadata: { ...(metadata || {}) },
|
|
1433
|
+
};
|
|
1434
|
+
_aecogovJsV2.set(id, j);
|
|
1435
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
1436
|
+
}
|
|
1437
|
+
export function tradingAecoTradeV2(id) {
|
|
1438
|
+
const j = _aecogovJsV2.get(id);
|
|
1439
|
+
if (!j) throw new Error(`aecogov trade ${id} not found`);
|
|
1440
|
+
_aecogovCheckJ(j.status, AECOGOV_TRADE_LIFECYCLE_V2.TRADING);
|
|
1441
|
+
const now = Date.now();
|
|
1442
|
+
j.status = AECOGOV_TRADE_LIFECYCLE_V2.TRADING;
|
|
1443
|
+
j.updatedAt = now;
|
|
1444
|
+
if (!j.startedAt) j.startedAt = now;
|
|
1445
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
1446
|
+
}
|
|
1447
|
+
export function completeTradeAecoV2(id) {
|
|
1448
|
+
const j = _aecogovJsV2.get(id);
|
|
1449
|
+
if (!j) throw new Error(`aecogov trade ${id} not found`);
|
|
1450
|
+
_aecogovCheckJ(j.status, AECOGOV_TRADE_LIFECYCLE_V2.SETTLED);
|
|
1451
|
+
const now = Date.now();
|
|
1452
|
+
j.status = AECOGOV_TRADE_LIFECYCLE_V2.SETTLED;
|
|
1453
|
+
j.updatedAt = now;
|
|
1454
|
+
if (!j.settledAt) j.settledAt = now;
|
|
1455
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
1456
|
+
}
|
|
1457
|
+
export function failAecoTradeV2(id, reason) {
|
|
1458
|
+
const j = _aecogovJsV2.get(id);
|
|
1459
|
+
if (!j) throw new Error(`aecogov trade ${id} not found`);
|
|
1460
|
+
_aecogovCheckJ(j.status, AECOGOV_TRADE_LIFECYCLE_V2.FAILED);
|
|
1461
|
+
const now = Date.now();
|
|
1462
|
+
j.status = AECOGOV_TRADE_LIFECYCLE_V2.FAILED;
|
|
1463
|
+
j.updatedAt = now;
|
|
1464
|
+
if (!j.settledAt) j.settledAt = now;
|
|
1465
|
+
if (reason) j.metadata.failReason = String(reason);
|
|
1466
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
1467
|
+
}
|
|
1468
|
+
export function cancelAecoTradeV2(id, reason) {
|
|
1469
|
+
const j = _aecogovJsV2.get(id);
|
|
1470
|
+
if (!j) throw new Error(`aecogov trade ${id} not found`);
|
|
1471
|
+
_aecogovCheckJ(j.status, AECOGOV_TRADE_LIFECYCLE_V2.CANCELLED);
|
|
1472
|
+
const now = Date.now();
|
|
1473
|
+
j.status = AECOGOV_TRADE_LIFECYCLE_V2.CANCELLED;
|
|
1474
|
+
j.updatedAt = now;
|
|
1475
|
+
if (!j.settledAt) j.settledAt = now;
|
|
1476
|
+
if (reason) j.metadata.cancelReason = String(reason);
|
|
1477
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
1478
|
+
}
|
|
1479
|
+
export function getAecoTradeV2(id) {
|
|
1480
|
+
const j = _aecogovJsV2.get(id);
|
|
1481
|
+
if (!j) return null;
|
|
1482
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
1483
|
+
}
|
|
1484
|
+
export function listAecoTradesV2() {
|
|
1485
|
+
return [..._aecogovJsV2.values()].map((j) => ({
|
|
1486
|
+
...j,
|
|
1487
|
+
metadata: { ...j.metadata },
|
|
1488
|
+
}));
|
|
1489
|
+
}
|
|
1490
|
+
export function autoPausedIdleAecoProfilesV2({ now } = {}) {
|
|
1491
|
+
const t = now ?? Date.now();
|
|
1492
|
+
const flipped = [];
|
|
1493
|
+
for (const p of _aecogovPsV2.values())
|
|
1494
|
+
if (
|
|
1495
|
+
p.status === AECOGOV_PROFILE_MATURITY_V2.ACTIVE &&
|
|
1496
|
+
t - p.lastTouchedAt >= _aecogovIdleMs
|
|
1497
|
+
) {
|
|
1498
|
+
p.status = AECOGOV_PROFILE_MATURITY_V2.PAUSED;
|
|
1499
|
+
p.updatedAt = t;
|
|
1500
|
+
flipped.push(p.id);
|
|
1501
|
+
}
|
|
1502
|
+
return { flipped, count: flipped.length };
|
|
1503
|
+
}
|
|
1504
|
+
export function autoFailStuckAecoTradesV2({ now } = {}) {
|
|
1505
|
+
const t = now ?? Date.now();
|
|
1506
|
+
const flipped = [];
|
|
1507
|
+
for (const j of _aecogovJsV2.values())
|
|
1508
|
+
if (
|
|
1509
|
+
j.status === AECOGOV_TRADE_LIFECYCLE_V2.TRADING &&
|
|
1510
|
+
j.startedAt != null &&
|
|
1511
|
+
t - j.startedAt >= _aecogovStuckMs
|
|
1512
|
+
) {
|
|
1513
|
+
j.status = AECOGOV_TRADE_LIFECYCLE_V2.FAILED;
|
|
1514
|
+
j.updatedAt = t;
|
|
1515
|
+
if (!j.settledAt) j.settledAt = t;
|
|
1516
|
+
j.metadata.failReason = "auto-fail-stuck";
|
|
1517
|
+
flipped.push(j.id);
|
|
1518
|
+
}
|
|
1519
|
+
return { flipped, count: flipped.length };
|
|
1520
|
+
}
|
|
1521
|
+
export function getAecogovStatsV2() {
|
|
1522
|
+
const profilesByStatus = {};
|
|
1523
|
+
for (const v of Object.values(AECOGOV_PROFILE_MATURITY_V2))
|
|
1524
|
+
profilesByStatus[v] = 0;
|
|
1525
|
+
for (const p of _aecogovPsV2.values()) profilesByStatus[p.status]++;
|
|
1526
|
+
const tradesByStatus = {};
|
|
1527
|
+
for (const v of Object.values(AECOGOV_TRADE_LIFECYCLE_V2))
|
|
1528
|
+
tradesByStatus[v] = 0;
|
|
1529
|
+
for (const j of _aecogovJsV2.values()) tradesByStatus[j.status]++;
|
|
1530
|
+
return {
|
|
1531
|
+
totalAecoProfilesV2: _aecogovPsV2.size,
|
|
1532
|
+
totalAecoTradesV2: _aecogovJsV2.size,
|
|
1533
|
+
maxActiveAecoProfilesPerOwner: _aecogovMaxActive,
|
|
1534
|
+
maxPendingAecoTradesPerProfile: _aecogovMaxPending,
|
|
1535
|
+
aecogovProfileIdleMs: _aecogovIdleMs,
|
|
1536
|
+
aecogovTradeStuckMs: _aecogovStuckMs,
|
|
1537
|
+
profilesByStatus,
|
|
1538
|
+
tradesByStatus,
|
|
1539
|
+
};
|
|
1540
|
+
}
|