chainlesschain 0.132.0 → 0.145.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (135) hide show
  1. package/package.json +1 -1
  2. package/src/commands/a2a.js +230 -0
  3. package/src/commands/activitypub.js +191 -0
  4. package/src/commands/agent.js +601 -0
  5. package/src/commands/audit.js +206 -0
  6. package/src/commands/bi.js +186 -0
  7. package/src/commands/bm25.js +162 -0
  8. package/src/commands/browse.js +225 -0
  9. package/src/commands/ccron.js +178 -0
  10. package/src/commands/chat.js +207 -0
  11. package/src/commands/compliance.js +420 -0
  12. package/src/commands/compt.js +176 -0
  13. package/src/commands/consol.js +237 -0
  14. package/src/commands/cowork.js +588 -0
  15. package/src/commands/crosschain.js +216 -0
  16. package/src/commands/dao.js +216 -0
  17. package/src/commands/dlp.js +206 -0
  18. package/src/commands/economy.js +211 -0
  19. package/src/commands/evolution.js +209 -0
  20. package/src/commands/evomap.js +216 -0
  21. package/src/commands/fflag.js +230 -0
  22. package/src/commands/git.js +185 -0
  23. package/src/commands/hardening.js +209 -0
  24. package/src/commands/hmemory.js +210 -0
  25. package/src/commands/incentive.js +209 -0
  26. package/src/commands/inference.js +178 -0
  27. package/src/commands/itbudget.js +161 -0
  28. package/src/commands/kg.js +206 -0
  29. package/src/commands/lowcode.js +201 -0
  30. package/src/commands/marketplace.js +206 -0
  31. package/src/commands/matrix.js +214 -0
  32. package/src/commands/mcpscaf.js +153 -0
  33. package/src/commands/meminj.js +153 -0
  34. package/src/commands/nostr.js +213 -0
  35. package/src/commands/orchestrate.js +217 -0
  36. package/src/commands/orchgov.js +156 -0
  37. package/src/commands/pdfp.js +160 -0
  38. package/src/commands/perf.js +176 -0
  39. package/src/commands/perm.js +156 -0
  40. package/src/commands/pipeline.js +211 -0
  41. package/src/commands/planmode.js +154 -0
  42. package/src/commands/privacy.js +203 -0
  43. package/src/commands/promcomp.js +166 -0
  44. package/src/commands/recommend.js +185 -0
  45. package/src/commands/reputation.js +208 -0
  46. package/src/commands/sandbox.js +206 -0
  47. package/src/commands/seshhook.js +153 -0
  48. package/src/commands/seshsearch.js +149 -0
  49. package/src/commands/seshtail.js +152 -0
  50. package/src/commands/seshu.js +160 -0
  51. package/src/commands/sganal.js +172 -0
  52. package/src/commands/siem.js +207 -0
  53. package/src/commands/sla.js +212 -0
  54. package/src/commands/slotfill.js +154 -0
  55. package/src/commands/social.js +159 -0
  56. package/src/commands/stress.js +206 -0
  57. package/src/commands/svccont.js +157 -0
  58. package/src/commands/terraform.js +206 -0
  59. package/src/commands/tms.js +183 -0
  60. package/src/commands/topiccls.js +158 -0
  61. package/src/commands/uprof.js +154 -0
  62. package/src/commands/vcheck.js +172 -0
  63. package/src/commands/webfetch.js +150 -0
  64. package/src/commands/zkp.js +218 -0
  65. package/src/harness/prompt-compressor.js +331 -0
  66. package/src/index.js +101 -1
  67. package/src/lib/a2a-protocol.js +373 -0
  68. package/src/lib/activitypub-bridge.js +343 -0
  69. package/src/lib/agent-economy.js +358 -0
  70. package/src/lib/app-builder.js +338 -0
  71. package/src/lib/audit-logger.js +321 -0
  72. package/src/lib/autonomous-agent.js +341 -0
  73. package/src/lib/bi-engine.js +339 -0
  74. package/src/lib/bm25-search.js +333 -0
  75. package/src/lib/browser-automation.js +352 -0
  76. package/src/lib/chat-core.js +336 -0
  77. package/src/lib/claude-code-bridge.js +341 -0
  78. package/src/lib/compliance-framework-reporter.js +359 -0
  79. package/src/lib/compliance-manager.js +330 -0
  80. package/src/lib/compression-telemetry.js +333 -0
  81. package/src/lib/content-recommender.js +370 -0
  82. package/src/lib/cowork-cron.js +330 -0
  83. package/src/lib/cowork-learning.js +333 -0
  84. package/src/lib/cowork-task-runner.js +362 -0
  85. package/src/lib/cowork-workflow.js +327 -0
  86. package/src/lib/cross-chain.js +365 -0
  87. package/src/lib/dao-governance.js +339 -0
  88. package/src/lib/dlp-engine.js +343 -0
  89. package/src/lib/evolution-system.js +336 -0
  90. package/src/lib/evomap-manager.js +339 -0
  91. package/src/lib/execution-backend.js +351 -0
  92. package/src/lib/feature-flags.js +330 -0
  93. package/src/lib/git-integration.js +343 -0
  94. package/src/lib/hardening-manager.js +341 -0
  95. package/src/lib/hierarchical-memory.js +341 -0
  96. package/src/lib/inference-network.js +362 -0
  97. package/src/lib/iteration-budget.js +357 -0
  98. package/src/lib/knowledge-graph.js +333 -0
  99. package/src/lib/matrix-bridge.js +339 -0
  100. package/src/lib/mcp-scaffold.js +345 -0
  101. package/src/lib/memory-injection.js +320 -0
  102. package/src/lib/nostr-bridge.js +342 -0
  103. package/src/lib/orchestrator.js +350 -0
  104. package/src/lib/pdf-parser.js +330 -0
  105. package/src/lib/perf-tuning.js +364 -0
  106. package/src/lib/permission-engine.js +319 -0
  107. package/src/lib/pipeline-orchestrator.js +345 -0
  108. package/src/lib/plan-mode.js +328 -0
  109. package/src/lib/privacy-computing.js +335 -0
  110. package/src/lib/prompt-compressor.js +1 -10
  111. package/src/lib/reputation-optimizer.js +340 -0
  112. package/src/lib/sandbox-v2.js +327 -0
  113. package/src/lib/service-container.js +342 -0
  114. package/src/lib/session-consolidator.js +352 -0
  115. package/src/lib/session-hooks.js +340 -0
  116. package/src/lib/session-search.js +334 -0
  117. package/src/lib/session-tail.js +320 -0
  118. package/src/lib/session-usage.js +329 -0
  119. package/src/lib/siem-exporter.js +352 -0
  120. package/src/lib/skill-marketplace.js +345 -0
  121. package/src/lib/sla-manager.js +341 -0
  122. package/src/lib/slot-filler.js +333 -0
  123. package/src/lib/social-graph-analytics.js +327 -0
  124. package/src/lib/social-graph.js +304 -0
  125. package/src/lib/stress-tester.js +342 -0
  126. package/src/lib/sub-agent-registry.js +359 -0
  127. package/src/lib/task-model-selector.js +333 -0
  128. package/src/lib/terraform-manager.js +333 -0
  129. package/src/lib/todo-manager.js +339 -0
  130. package/src/lib/token-incentive.js +341 -0
  131. package/src/lib/topic-classifier.js +353 -0
  132. package/src/lib/user-profile.js +325 -0
  133. package/src/lib/version-checker.js +335 -0
  134. package/src/lib/web-fetch.js +322 -0
  135. package/src/lib/zkp-engine.js +342 -0
@@ -621,3 +621,346 @@ export function _resetState() {
621
621
  }
622
622
 
623
623
  export const _constants = { PUBLIC_AUDIENCE, CONTEXT, DEFAULT_ORIGIN };
624
+
625
+ // ===== V2 Surface: ActivityPub Bridge governance overlay (CLI v0.135.0) =====
626
+ export const AP_ACTOR_MATURITY_V2 = Object.freeze({
627
+ PENDING: "pending",
628
+ ACTIVE: "active",
629
+ SUSPENDED: "suspended",
630
+ DEACTIVATED: "deactivated",
631
+ });
632
+ export const AP_ACTIVITY_LIFECYCLE_V2 = Object.freeze({
633
+ QUEUED: "queued",
634
+ DELIVERING: "delivering",
635
+ DELIVERED: "delivered",
636
+ FAILED: "failed",
637
+ CANCELLED: "cancelled",
638
+ });
639
+
640
+ const _apActorTrans = new Map([
641
+ [
642
+ AP_ACTOR_MATURITY_V2.PENDING,
643
+ new Set([AP_ACTOR_MATURITY_V2.ACTIVE, AP_ACTOR_MATURITY_V2.DEACTIVATED]),
644
+ ],
645
+ [
646
+ AP_ACTOR_MATURITY_V2.ACTIVE,
647
+ new Set([AP_ACTOR_MATURITY_V2.SUSPENDED, AP_ACTOR_MATURITY_V2.DEACTIVATED]),
648
+ ],
649
+ [
650
+ AP_ACTOR_MATURITY_V2.SUSPENDED,
651
+ new Set([AP_ACTOR_MATURITY_V2.ACTIVE, AP_ACTOR_MATURITY_V2.DEACTIVATED]),
652
+ ],
653
+ [AP_ACTOR_MATURITY_V2.DEACTIVATED, new Set()],
654
+ ]);
655
+ const _apActorTerminal = new Set([AP_ACTOR_MATURITY_V2.DEACTIVATED]);
656
+ const _apActTrans = new Map([
657
+ [
658
+ AP_ACTIVITY_LIFECYCLE_V2.QUEUED,
659
+ new Set([
660
+ AP_ACTIVITY_LIFECYCLE_V2.DELIVERING,
661
+ AP_ACTIVITY_LIFECYCLE_V2.CANCELLED,
662
+ ]),
663
+ ],
664
+ [
665
+ AP_ACTIVITY_LIFECYCLE_V2.DELIVERING,
666
+ new Set([
667
+ AP_ACTIVITY_LIFECYCLE_V2.DELIVERED,
668
+ AP_ACTIVITY_LIFECYCLE_V2.FAILED,
669
+ AP_ACTIVITY_LIFECYCLE_V2.CANCELLED,
670
+ ]),
671
+ ],
672
+ [AP_ACTIVITY_LIFECYCLE_V2.DELIVERED, new Set()],
673
+ [AP_ACTIVITY_LIFECYCLE_V2.FAILED, new Set()],
674
+ [AP_ACTIVITY_LIFECYCLE_V2.CANCELLED, new Set()],
675
+ ]);
676
+
677
+ const _apActors = new Map();
678
+ const _apActs = new Map();
679
+ let _apMaxActivePerOwner = 15;
680
+ let _apMaxPendingPerActor = 25;
681
+ let _apActorIdleMs = 24 * 60 * 60 * 1000;
682
+ let _apActStuckMs = 3 * 60 * 1000;
683
+
684
+ function _apPos(n, lbl) {
685
+ const v = Math.floor(Number(n));
686
+ if (!Number.isFinite(v) || v <= 0)
687
+ throw new Error(`${lbl} must be positive integer`);
688
+ return v;
689
+ }
690
+
691
+ export function setMaxActiveApActorsPerOwnerV2(n) {
692
+ _apMaxActivePerOwner = _apPos(n, "maxActiveApActorsPerOwner");
693
+ }
694
+ export function getMaxActiveApActorsPerOwnerV2() {
695
+ return _apMaxActivePerOwner;
696
+ }
697
+ export function setMaxPendingApActivitiesPerActorV2(n) {
698
+ _apMaxPendingPerActor = _apPos(n, "maxPendingApActivitiesPerActor");
699
+ }
700
+ export function getMaxPendingApActivitiesPerActorV2() {
701
+ return _apMaxPendingPerActor;
702
+ }
703
+ export function setApActorIdleMsV2(n) {
704
+ _apActorIdleMs = _apPos(n, "apActorIdleMs");
705
+ }
706
+ export function getApActorIdleMsV2() {
707
+ return _apActorIdleMs;
708
+ }
709
+ export function setApActivityStuckMsV2(n) {
710
+ _apActStuckMs = _apPos(n, "apActivityStuckMs");
711
+ }
712
+ export function getApActivityStuckMsV2() {
713
+ return _apActStuckMs;
714
+ }
715
+
716
+ export function _resetStateActivityPubBridgeV2() {
717
+ _apActors.clear();
718
+ _apActs.clear();
719
+ _apMaxActivePerOwner = 15;
720
+ _apMaxPendingPerActor = 25;
721
+ _apActorIdleMs = 24 * 60 * 60 * 1000;
722
+ _apActStuckMs = 3 * 60 * 1000;
723
+ }
724
+
725
+ export function registerApActorV2({ id, owner, handle, metadata } = {}) {
726
+ if (!id || typeof id !== "string") throw new Error("id is required");
727
+ if (!owner || typeof owner !== "string") throw new Error("owner is required");
728
+ if (_apActors.has(id))
729
+ throw new Error(`activitypub actor ${id} already registered`);
730
+ const now = Date.now();
731
+ const a = {
732
+ id,
733
+ owner,
734
+ handle: handle || id,
735
+ status: AP_ACTOR_MATURITY_V2.PENDING,
736
+ createdAt: now,
737
+ updatedAt: now,
738
+ activatedAt: null,
739
+ deactivatedAt: null,
740
+ lastTouchedAt: now,
741
+ metadata: { ...(metadata || {}) },
742
+ };
743
+ _apActors.set(id, a);
744
+ return { ...a, metadata: { ...a.metadata } };
745
+ }
746
+ function _apCheckA(from, to) {
747
+ const al = _apActorTrans.get(from);
748
+ if (!al || !al.has(to))
749
+ throw new Error(`invalid activitypub actor transition ${from} → ${to}`);
750
+ }
751
+ function _apCountActive(owner) {
752
+ let n = 0;
753
+ for (const a of _apActors.values())
754
+ if (a.owner === owner && a.status === AP_ACTOR_MATURITY_V2.ACTIVE) n++;
755
+ return n;
756
+ }
757
+
758
+ export function activateApActorV2(id) {
759
+ const a = _apActors.get(id);
760
+ if (!a) throw new Error(`activitypub actor ${id} not found`);
761
+ _apCheckA(a.status, AP_ACTOR_MATURITY_V2.ACTIVE);
762
+ const recovery = a.status === AP_ACTOR_MATURITY_V2.SUSPENDED;
763
+ if (!recovery) {
764
+ const c = _apCountActive(a.owner);
765
+ if (c >= _apMaxActivePerOwner)
766
+ throw new Error(
767
+ `max active activitypub actors per owner (${_apMaxActivePerOwner}) reached for ${a.owner}`,
768
+ );
769
+ }
770
+ const now = Date.now();
771
+ a.status = AP_ACTOR_MATURITY_V2.ACTIVE;
772
+ a.updatedAt = now;
773
+ a.lastTouchedAt = now;
774
+ if (!a.activatedAt) a.activatedAt = now;
775
+ return { ...a, metadata: { ...a.metadata } };
776
+ }
777
+ export function suspendApActorV2(id) {
778
+ const a = _apActors.get(id);
779
+ if (!a) throw new Error(`activitypub actor ${id} not found`);
780
+ _apCheckA(a.status, AP_ACTOR_MATURITY_V2.SUSPENDED);
781
+ a.status = AP_ACTOR_MATURITY_V2.SUSPENDED;
782
+ a.updatedAt = Date.now();
783
+ return { ...a, metadata: { ...a.metadata } };
784
+ }
785
+ export function deactivateApActorV2(id) {
786
+ const a = _apActors.get(id);
787
+ if (!a) throw new Error(`activitypub actor ${id} not found`);
788
+ _apCheckA(a.status, AP_ACTOR_MATURITY_V2.DEACTIVATED);
789
+ const now = Date.now();
790
+ a.status = AP_ACTOR_MATURITY_V2.DEACTIVATED;
791
+ a.updatedAt = now;
792
+ if (!a.deactivatedAt) a.deactivatedAt = now;
793
+ return { ...a, metadata: { ...a.metadata } };
794
+ }
795
+ export function touchApActorV2(id) {
796
+ const a = _apActors.get(id);
797
+ if (!a) throw new Error(`activitypub actor ${id} not found`);
798
+ if (_apActorTerminal.has(a.status))
799
+ throw new Error(`cannot touch terminal activitypub actor ${id}`);
800
+ const now = Date.now();
801
+ a.lastTouchedAt = now;
802
+ a.updatedAt = now;
803
+ return { ...a, metadata: { ...a.metadata } };
804
+ }
805
+ export function getApActorV2(id) {
806
+ const a = _apActors.get(id);
807
+ if (!a) return null;
808
+ return { ...a, metadata: { ...a.metadata } };
809
+ }
810
+ export function listApActorsV2() {
811
+ return [..._apActors.values()].map((a) => ({
812
+ ...a,
813
+ metadata: { ...a.metadata },
814
+ }));
815
+ }
816
+
817
+ function _apCountPending(aid) {
818
+ let n = 0;
819
+ for (const ac of _apActs.values())
820
+ if (
821
+ ac.actorId === aid &&
822
+ (ac.status === AP_ACTIVITY_LIFECYCLE_V2.QUEUED ||
823
+ ac.status === AP_ACTIVITY_LIFECYCLE_V2.DELIVERING)
824
+ )
825
+ n++;
826
+ return n;
827
+ }
828
+
829
+ export function createApActivityV2({ id, actorId, kind, metadata } = {}) {
830
+ if (!id || typeof id !== "string") throw new Error("id is required");
831
+ if (!actorId || typeof actorId !== "string")
832
+ throw new Error("actorId is required");
833
+ if (_apActs.has(id))
834
+ throw new Error(`activitypub activity ${id} already exists`);
835
+ if (!_apActors.has(actorId))
836
+ throw new Error(`activitypub actor ${actorId} not found`);
837
+ const pending = _apCountPending(actorId);
838
+ if (pending >= _apMaxPendingPerActor)
839
+ throw new Error(
840
+ `max pending activitypub activities per actor (${_apMaxPendingPerActor}) reached for ${actorId}`,
841
+ );
842
+ const now = Date.now();
843
+ const ac = {
844
+ id,
845
+ actorId,
846
+ kind: kind || "Note",
847
+ status: AP_ACTIVITY_LIFECYCLE_V2.QUEUED,
848
+ createdAt: now,
849
+ updatedAt: now,
850
+ startedAt: null,
851
+ settledAt: null,
852
+ metadata: { ...(metadata || {}) },
853
+ };
854
+ _apActs.set(id, ac);
855
+ return { ...ac, metadata: { ...ac.metadata } };
856
+ }
857
+ function _apCheckAct(from, to) {
858
+ const al = _apActTrans.get(from);
859
+ if (!al || !al.has(to))
860
+ throw new Error(`invalid activitypub activity transition ${from} → ${to}`);
861
+ }
862
+ export function startApActivityV2(id) {
863
+ const ac = _apActs.get(id);
864
+ if (!ac) throw new Error(`activitypub activity ${id} not found`);
865
+ _apCheckAct(ac.status, AP_ACTIVITY_LIFECYCLE_V2.DELIVERING);
866
+ const now = Date.now();
867
+ ac.status = AP_ACTIVITY_LIFECYCLE_V2.DELIVERING;
868
+ ac.updatedAt = now;
869
+ if (!ac.startedAt) ac.startedAt = now;
870
+ return { ...ac, metadata: { ...ac.metadata } };
871
+ }
872
+ export function deliverApActivityV2(id) {
873
+ const ac = _apActs.get(id);
874
+ if (!ac) throw new Error(`activitypub activity ${id} not found`);
875
+ _apCheckAct(ac.status, AP_ACTIVITY_LIFECYCLE_V2.DELIVERED);
876
+ const now = Date.now();
877
+ ac.status = AP_ACTIVITY_LIFECYCLE_V2.DELIVERED;
878
+ ac.updatedAt = now;
879
+ if (!ac.settledAt) ac.settledAt = now;
880
+ return { ...ac, metadata: { ...ac.metadata } };
881
+ }
882
+ export function failApActivityV2(id, reason) {
883
+ const ac = _apActs.get(id);
884
+ if (!ac) throw new Error(`activitypub activity ${id} not found`);
885
+ _apCheckAct(ac.status, AP_ACTIVITY_LIFECYCLE_V2.FAILED);
886
+ const now = Date.now();
887
+ ac.status = AP_ACTIVITY_LIFECYCLE_V2.FAILED;
888
+ ac.updatedAt = now;
889
+ if (!ac.settledAt) ac.settledAt = now;
890
+ if (reason) ac.metadata.failReason = String(reason);
891
+ return { ...ac, metadata: { ...ac.metadata } };
892
+ }
893
+ export function cancelApActivityV2(id, reason) {
894
+ const ac = _apActs.get(id);
895
+ if (!ac) throw new Error(`activitypub activity ${id} not found`);
896
+ _apCheckAct(ac.status, AP_ACTIVITY_LIFECYCLE_V2.CANCELLED);
897
+ const now = Date.now();
898
+ ac.status = AP_ACTIVITY_LIFECYCLE_V2.CANCELLED;
899
+ ac.updatedAt = now;
900
+ if (!ac.settledAt) ac.settledAt = now;
901
+ if (reason) ac.metadata.cancelReason = String(reason);
902
+ return { ...ac, metadata: { ...ac.metadata } };
903
+ }
904
+ export function getApActivityV2(id) {
905
+ const ac = _apActs.get(id);
906
+ if (!ac) return null;
907
+ return { ...ac, metadata: { ...ac.metadata } };
908
+ }
909
+ export function listApActivitiesV2() {
910
+ return [..._apActs.values()].map((ac) => ({
911
+ ...ac,
912
+ metadata: { ...ac.metadata },
913
+ }));
914
+ }
915
+
916
+ export function autoSuspendIdleApActorsV2({ now } = {}) {
917
+ const t = now ?? Date.now();
918
+ const flipped = [];
919
+ for (const a of _apActors.values())
920
+ if (
921
+ a.status === AP_ACTOR_MATURITY_V2.ACTIVE &&
922
+ t - a.lastTouchedAt >= _apActorIdleMs
923
+ ) {
924
+ a.status = AP_ACTOR_MATURITY_V2.SUSPENDED;
925
+ a.updatedAt = t;
926
+ flipped.push(a.id);
927
+ }
928
+ return { flipped, count: flipped.length };
929
+ }
930
+ export function autoFailStuckApActivitiesV2({ now } = {}) {
931
+ const t = now ?? Date.now();
932
+ const flipped = [];
933
+ for (const ac of _apActs.values())
934
+ if (
935
+ ac.status === AP_ACTIVITY_LIFECYCLE_V2.DELIVERING &&
936
+ ac.startedAt != null &&
937
+ t - ac.startedAt >= _apActStuckMs
938
+ ) {
939
+ ac.status = AP_ACTIVITY_LIFECYCLE_V2.FAILED;
940
+ ac.updatedAt = t;
941
+ if (!ac.settledAt) ac.settledAt = t;
942
+ ac.metadata.failReason = "auto-fail-stuck";
943
+ flipped.push(ac.id);
944
+ }
945
+ return { flipped, count: flipped.length };
946
+ }
947
+
948
+ export function getActivityPubBridgeStatsV2() {
949
+ const actorsByStatus = {};
950
+ for (const s of Object.values(AP_ACTOR_MATURITY_V2)) actorsByStatus[s] = 0;
951
+ for (const a of _apActors.values()) actorsByStatus[a.status]++;
952
+ const activitiesByStatus = {};
953
+ for (const s of Object.values(AP_ACTIVITY_LIFECYCLE_V2))
954
+ activitiesByStatus[s] = 0;
955
+ for (const ac of _apActs.values()) activitiesByStatus[ac.status]++;
956
+ return {
957
+ totalActorsV2: _apActors.size,
958
+ totalActivitiesV2: _apActs.size,
959
+ maxActiveApActorsPerOwner: _apMaxActivePerOwner,
960
+ maxPendingApActivitiesPerActor: _apMaxPendingPerActor,
961
+ apActorIdleMs: _apActorIdleMs,
962
+ apActivityStuckMs: _apActStuckMs,
963
+ actorsByStatus,
964
+ activitiesByStatus,
965
+ };
966
+ }
@@ -846,3 +846,361 @@ export function _resetV2State() {
846
846
  _v2TaskContributions.clear();
847
847
  _v2Distributions.length = 0;
848
848
  }
849
+
850
+ // ===== V2 Surface: Agent Economy governance overlay (CLI v0.137.0) =====
851
+ export const ECONOMY_ACCOUNT_MATURITY_V2 = Object.freeze({
852
+ PENDING: "pending",
853
+ ACTIVE: "active",
854
+ FROZEN: "frozen",
855
+ CLOSED: "closed",
856
+ });
857
+ export const ECONOMY_TX_LIFECYCLE_V2 = Object.freeze({
858
+ QUEUED: "queued",
859
+ PROCESSING: "processing",
860
+ SETTLED: "settled",
861
+ FAILED: "failed",
862
+ CANCELLED: "cancelled",
863
+ });
864
+
865
+ const _econAcctTrans = new Map([
866
+ [
867
+ ECONOMY_ACCOUNT_MATURITY_V2.PENDING,
868
+ new Set([
869
+ ECONOMY_ACCOUNT_MATURITY_V2.ACTIVE,
870
+ ECONOMY_ACCOUNT_MATURITY_V2.CLOSED,
871
+ ]),
872
+ ],
873
+ [
874
+ ECONOMY_ACCOUNT_MATURITY_V2.ACTIVE,
875
+ new Set([
876
+ ECONOMY_ACCOUNT_MATURITY_V2.FROZEN,
877
+ ECONOMY_ACCOUNT_MATURITY_V2.CLOSED,
878
+ ]),
879
+ ],
880
+ [
881
+ ECONOMY_ACCOUNT_MATURITY_V2.FROZEN,
882
+ new Set([
883
+ ECONOMY_ACCOUNT_MATURITY_V2.ACTIVE,
884
+ ECONOMY_ACCOUNT_MATURITY_V2.CLOSED,
885
+ ]),
886
+ ],
887
+ [ECONOMY_ACCOUNT_MATURITY_V2.CLOSED, new Set()],
888
+ ]);
889
+ const _econAcctTerminal = new Set([ECONOMY_ACCOUNT_MATURITY_V2.CLOSED]);
890
+ const _econTxTrans = new Map([
891
+ [
892
+ ECONOMY_TX_LIFECYCLE_V2.QUEUED,
893
+ new Set([
894
+ ECONOMY_TX_LIFECYCLE_V2.PROCESSING,
895
+ ECONOMY_TX_LIFECYCLE_V2.CANCELLED,
896
+ ]),
897
+ ],
898
+ [
899
+ ECONOMY_TX_LIFECYCLE_V2.PROCESSING,
900
+ new Set([
901
+ ECONOMY_TX_LIFECYCLE_V2.SETTLED,
902
+ ECONOMY_TX_LIFECYCLE_V2.FAILED,
903
+ ECONOMY_TX_LIFECYCLE_V2.CANCELLED,
904
+ ]),
905
+ ],
906
+ [ECONOMY_TX_LIFECYCLE_V2.SETTLED, new Set()],
907
+ [ECONOMY_TX_LIFECYCLE_V2.FAILED, new Set()],
908
+ [ECONOMY_TX_LIFECYCLE_V2.CANCELLED, new Set()],
909
+ ]);
910
+
911
+ const _econAccts = new Map();
912
+ const _econTxs = new Map();
913
+ let _econMaxActivePerHolder = 20;
914
+ let _econMaxPendingPerAcct = 30;
915
+ let _econAcctIdleMs = 7 * 24 * 60 * 60 * 1000;
916
+ let _econTxStuckMs = 5 * 60 * 1000;
917
+
918
+ function _econPos(n, lbl) {
919
+ const v = Math.floor(Number(n));
920
+ if (!Number.isFinite(v) || v <= 0)
921
+ throw new Error(`${lbl} must be positive integer`);
922
+ return v;
923
+ }
924
+
925
+ export function setMaxActiveEconomyAccountsPerHolderV2(n) {
926
+ _econMaxActivePerHolder = _econPos(n, "maxActiveEconomyAccountsPerHolder");
927
+ }
928
+ export function getMaxActiveEconomyAccountsPerHolderV2() {
929
+ return _econMaxActivePerHolder;
930
+ }
931
+ export function setMaxPendingEconomyTxsPerAccountV2(n) {
932
+ _econMaxPendingPerAcct = _econPos(n, "maxPendingEconomyTxsPerAccount");
933
+ }
934
+ export function getMaxPendingEconomyTxsPerAccountV2() {
935
+ return _econMaxPendingPerAcct;
936
+ }
937
+ export function setEconomyAccountIdleMsV2(n) {
938
+ _econAcctIdleMs = _econPos(n, "economyAccountIdleMs");
939
+ }
940
+ export function getEconomyAccountIdleMsV2() {
941
+ return _econAcctIdleMs;
942
+ }
943
+ export function setEconomyTxStuckMsV2(n) {
944
+ _econTxStuckMs = _econPos(n, "economyTxStuckMs");
945
+ }
946
+ export function getEconomyTxStuckMsV2() {
947
+ return _econTxStuckMs;
948
+ }
949
+
950
+ export function _resetStateAgentEconomyV2() {
951
+ _econAccts.clear();
952
+ _econTxs.clear();
953
+ _econMaxActivePerHolder = 20;
954
+ _econMaxPendingPerAcct = 30;
955
+ _econAcctIdleMs = 7 * 24 * 60 * 60 * 1000;
956
+ _econTxStuckMs = 5 * 60 * 1000;
957
+ }
958
+
959
+ export function registerEconomyAccountV2({
960
+ id,
961
+ holder,
962
+ currency,
963
+ metadata,
964
+ } = {}) {
965
+ if (!id || typeof id !== "string") throw new Error("id is required");
966
+ if (!holder || typeof holder !== "string")
967
+ throw new Error("holder is required");
968
+ if (_econAccts.has(id))
969
+ throw new Error(`economy account ${id} already registered`);
970
+ const now = Date.now();
971
+ const a = {
972
+ id,
973
+ holder,
974
+ currency: currency || "CLC",
975
+ status: ECONOMY_ACCOUNT_MATURITY_V2.PENDING,
976
+ createdAt: now,
977
+ updatedAt: now,
978
+ activatedAt: null,
979
+ closedAt: null,
980
+ lastTouchedAt: now,
981
+ metadata: { ...(metadata || {}) },
982
+ };
983
+ _econAccts.set(id, a);
984
+ return { ...a, metadata: { ...a.metadata } };
985
+ }
986
+ function _econCheckA(from, to) {
987
+ const a = _econAcctTrans.get(from);
988
+ if (!a || !a.has(to))
989
+ throw new Error(`invalid economy account transition ${from} → ${to}`);
990
+ }
991
+ function _econCountActive(holder) {
992
+ let n = 0;
993
+ for (const a of _econAccts.values())
994
+ if (a.holder === holder && a.status === ECONOMY_ACCOUNT_MATURITY_V2.ACTIVE)
995
+ n++;
996
+ return n;
997
+ }
998
+
999
+ export function activateEconomyAccountV2(id) {
1000
+ const a = _econAccts.get(id);
1001
+ if (!a) throw new Error(`economy account ${id} not found`);
1002
+ _econCheckA(a.status, ECONOMY_ACCOUNT_MATURITY_V2.ACTIVE);
1003
+ const recovery = a.status === ECONOMY_ACCOUNT_MATURITY_V2.FROZEN;
1004
+ if (!recovery) {
1005
+ const c = _econCountActive(a.holder);
1006
+ if (c >= _econMaxActivePerHolder)
1007
+ throw new Error(
1008
+ `max active economy accounts per holder (${_econMaxActivePerHolder}) reached for ${a.holder}`,
1009
+ );
1010
+ }
1011
+ const now = Date.now();
1012
+ a.status = ECONOMY_ACCOUNT_MATURITY_V2.ACTIVE;
1013
+ a.updatedAt = now;
1014
+ a.lastTouchedAt = now;
1015
+ if (!a.activatedAt) a.activatedAt = now;
1016
+ return { ...a, metadata: { ...a.metadata } };
1017
+ }
1018
+ export function freezeEconomyAccountV2(id) {
1019
+ const a = _econAccts.get(id);
1020
+ if (!a) throw new Error(`economy account ${id} not found`);
1021
+ _econCheckA(a.status, ECONOMY_ACCOUNT_MATURITY_V2.FROZEN);
1022
+ a.status = ECONOMY_ACCOUNT_MATURITY_V2.FROZEN;
1023
+ a.updatedAt = Date.now();
1024
+ return { ...a, metadata: { ...a.metadata } };
1025
+ }
1026
+ export function closeEconomyAccountV2(id) {
1027
+ const a = _econAccts.get(id);
1028
+ if (!a) throw new Error(`economy account ${id} not found`);
1029
+ _econCheckA(a.status, ECONOMY_ACCOUNT_MATURITY_V2.CLOSED);
1030
+ const now = Date.now();
1031
+ a.status = ECONOMY_ACCOUNT_MATURITY_V2.CLOSED;
1032
+ a.updatedAt = now;
1033
+ if (!a.closedAt) a.closedAt = now;
1034
+ return { ...a, metadata: { ...a.metadata } };
1035
+ }
1036
+ export function touchEconomyAccountV2(id) {
1037
+ const a = _econAccts.get(id);
1038
+ if (!a) throw new Error(`economy account ${id} not found`);
1039
+ if (_econAcctTerminal.has(a.status))
1040
+ throw new Error(`cannot touch terminal economy account ${id}`);
1041
+ const now = Date.now();
1042
+ a.lastTouchedAt = now;
1043
+ a.updatedAt = now;
1044
+ return { ...a, metadata: { ...a.metadata } };
1045
+ }
1046
+ export function getEconomyAccountV2(id) {
1047
+ const a = _econAccts.get(id);
1048
+ if (!a) return null;
1049
+ return { ...a, metadata: { ...a.metadata } };
1050
+ }
1051
+ export function listEconomyAccountsV2() {
1052
+ return [..._econAccts.values()].map((a) => ({
1053
+ ...a,
1054
+ metadata: { ...a.metadata },
1055
+ }));
1056
+ }
1057
+
1058
+ function _econCountPending(aid) {
1059
+ let n = 0;
1060
+ for (const t of _econTxs.values())
1061
+ if (
1062
+ t.accountId === aid &&
1063
+ (t.status === ECONOMY_TX_LIFECYCLE_V2.QUEUED ||
1064
+ t.status === ECONOMY_TX_LIFECYCLE_V2.PROCESSING)
1065
+ )
1066
+ n++;
1067
+ return n;
1068
+ }
1069
+
1070
+ export function createEconomyTxV2({ id, accountId, amount, metadata } = {}) {
1071
+ if (!id || typeof id !== "string") throw new Error("id is required");
1072
+ if (!accountId || typeof accountId !== "string")
1073
+ throw new Error("accountId is required");
1074
+ if (_econTxs.has(id)) throw new Error(`economy tx ${id} already exists`);
1075
+ if (!_econAccts.has(accountId))
1076
+ throw new Error(`economy account ${accountId} not found`);
1077
+ const pending = _econCountPending(accountId);
1078
+ if (pending >= _econMaxPendingPerAcct)
1079
+ throw new Error(
1080
+ `max pending economy txs per account (${_econMaxPendingPerAcct}) reached for ${accountId}`,
1081
+ );
1082
+ const now = Date.now();
1083
+ const t = {
1084
+ id,
1085
+ accountId,
1086
+ amount: amount || "0",
1087
+ status: ECONOMY_TX_LIFECYCLE_V2.QUEUED,
1088
+ createdAt: now,
1089
+ updatedAt: now,
1090
+ startedAt: null,
1091
+ settledAt: null,
1092
+ metadata: { ...(metadata || {}) },
1093
+ };
1094
+ _econTxs.set(id, t);
1095
+ return { ...t, metadata: { ...t.metadata } };
1096
+ }
1097
+ function _econCheckT(from, to) {
1098
+ const a = _econTxTrans.get(from);
1099
+ if (!a || !a.has(to))
1100
+ throw new Error(`invalid economy tx transition ${from} → ${to}`);
1101
+ }
1102
+ export function startEconomyTxV2(id) {
1103
+ const t = _econTxs.get(id);
1104
+ if (!t) throw new Error(`economy tx ${id} not found`);
1105
+ _econCheckT(t.status, ECONOMY_TX_LIFECYCLE_V2.PROCESSING);
1106
+ const now = Date.now();
1107
+ t.status = ECONOMY_TX_LIFECYCLE_V2.PROCESSING;
1108
+ t.updatedAt = now;
1109
+ if (!t.startedAt) t.startedAt = now;
1110
+ return { ...t, metadata: { ...t.metadata } };
1111
+ }
1112
+ export function settleEconomyTxV2(id) {
1113
+ const t = _econTxs.get(id);
1114
+ if (!t) throw new Error(`economy tx ${id} not found`);
1115
+ _econCheckT(t.status, ECONOMY_TX_LIFECYCLE_V2.SETTLED);
1116
+ const now = Date.now();
1117
+ t.status = ECONOMY_TX_LIFECYCLE_V2.SETTLED;
1118
+ t.updatedAt = now;
1119
+ if (!t.settledAt) t.settledAt = now;
1120
+ return { ...t, metadata: { ...t.metadata } };
1121
+ }
1122
+ export function failEconomyTxV2(id, reason) {
1123
+ const t = _econTxs.get(id);
1124
+ if (!t) throw new Error(`economy tx ${id} not found`);
1125
+ _econCheckT(t.status, ECONOMY_TX_LIFECYCLE_V2.FAILED);
1126
+ const now = Date.now();
1127
+ t.status = ECONOMY_TX_LIFECYCLE_V2.FAILED;
1128
+ t.updatedAt = now;
1129
+ if (!t.settledAt) t.settledAt = now;
1130
+ if (reason) t.metadata.failReason = String(reason);
1131
+ return { ...t, metadata: { ...t.metadata } };
1132
+ }
1133
+ export function cancelEconomyTxV2(id, reason) {
1134
+ const t = _econTxs.get(id);
1135
+ if (!t) throw new Error(`economy tx ${id} not found`);
1136
+ _econCheckT(t.status, ECONOMY_TX_LIFECYCLE_V2.CANCELLED);
1137
+ const now = Date.now();
1138
+ t.status = ECONOMY_TX_LIFECYCLE_V2.CANCELLED;
1139
+ t.updatedAt = now;
1140
+ if (!t.settledAt) t.settledAt = now;
1141
+ if (reason) t.metadata.cancelReason = String(reason);
1142
+ return { ...t, metadata: { ...t.metadata } };
1143
+ }
1144
+ export function getEconomyTxV2(id) {
1145
+ const t = _econTxs.get(id);
1146
+ if (!t) return null;
1147
+ return { ...t, metadata: { ...t.metadata } };
1148
+ }
1149
+ export function listEconomyTxsV2() {
1150
+ return [..._econTxs.values()].map((t) => ({
1151
+ ...t,
1152
+ metadata: { ...t.metadata },
1153
+ }));
1154
+ }
1155
+
1156
+ export function autoFreezeIdleEconomyAccountsV2({ now } = {}) {
1157
+ const t = now ?? Date.now();
1158
+ const flipped = [];
1159
+ for (const a of _econAccts.values())
1160
+ if (
1161
+ a.status === ECONOMY_ACCOUNT_MATURITY_V2.ACTIVE &&
1162
+ t - a.lastTouchedAt >= _econAcctIdleMs
1163
+ ) {
1164
+ a.status = ECONOMY_ACCOUNT_MATURITY_V2.FROZEN;
1165
+ a.updatedAt = t;
1166
+ flipped.push(a.id);
1167
+ }
1168
+ return { flipped, count: flipped.length };
1169
+ }
1170
+ export function autoFailStuckEconomyTxsV2({ now } = {}) {
1171
+ const t = now ?? Date.now();
1172
+ const flipped = [];
1173
+ for (const tx of _econTxs.values())
1174
+ if (
1175
+ tx.status === ECONOMY_TX_LIFECYCLE_V2.PROCESSING &&
1176
+ tx.startedAt != null &&
1177
+ t - tx.startedAt >= _econTxStuckMs
1178
+ ) {
1179
+ tx.status = ECONOMY_TX_LIFECYCLE_V2.FAILED;
1180
+ tx.updatedAt = t;
1181
+ if (!tx.settledAt) tx.settledAt = t;
1182
+ tx.metadata.failReason = "auto-fail-stuck";
1183
+ flipped.push(tx.id);
1184
+ }
1185
+ return { flipped, count: flipped.length };
1186
+ }
1187
+
1188
+ export function getAgentEconomyGovStatsV2() {
1189
+ const accountsByStatus = {};
1190
+ for (const s of Object.values(ECONOMY_ACCOUNT_MATURITY_V2))
1191
+ accountsByStatus[s] = 0;
1192
+ for (const a of _econAccts.values()) accountsByStatus[a.status]++;
1193
+ const txsByStatus = {};
1194
+ for (const s of Object.values(ECONOMY_TX_LIFECYCLE_V2)) txsByStatus[s] = 0;
1195
+ for (const t of _econTxs.values()) txsByStatus[t.status]++;
1196
+ return {
1197
+ totalAccountsV2: _econAccts.size,
1198
+ totalTxsV2: _econTxs.size,
1199
+ maxActiveEconomyAccountsPerHolder: _econMaxActivePerHolder,
1200
+ maxPendingEconomyTxsPerAccount: _econMaxPendingPerAcct,
1201
+ economyAccountIdleMs: _econAcctIdleMs,
1202
+ economyTxStuckMs: _econTxStuckMs,
1203
+ accountsByStatus,
1204
+ txsByStatus,
1205
+ };
1206
+ }