chainlesschain 0.143.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 (133) hide show
  1. package/package.json +1 -1
  2. package/src/commands/a2a.js +196 -28
  3. package/src/commands/activitypub.js +157 -27
  4. package/src/commands/agent.js +587 -103
  5. package/src/commands/audit.js +206 -0
  6. package/src/commands/bi.js +152 -27
  7. package/src/commands/bm25.js +111 -27
  8. package/src/commands/browse.js +190 -29
  9. package/src/commands/ccron.js +128 -28
  10. package/src/commands/chat.js +207 -0
  11. package/src/commands/compliance.js +414 -34
  12. package/src/commands/compt.js +127 -29
  13. package/src/commands/consol.js +8 -2
  14. package/src/commands/cowork.js +580 -34
  15. package/src/commands/crosschain.js +182 -28
  16. package/src/commands/dao.js +182 -28
  17. package/src/commands/dlp.js +172 -27
  18. package/src/commands/economy.js +205 -50
  19. package/src/commands/evolution.js +203 -50
  20. package/src/commands/evomap.js +182 -27
  21. package/src/commands/fflag.js +74 -22
  22. package/src/commands/git.js +177 -37
  23. package/src/commands/hardening.js +209 -0
  24. package/src/commands/hmemory.js +204 -50
  25. package/src/commands/incentive.js +209 -0
  26. package/src/commands/inference.js +170 -34
  27. package/src/commands/itbudget.js +149 -33
  28. package/src/commands/kg.js +206 -0
  29. package/src/commands/lowcode.js +195 -38
  30. package/src/commands/marketplace.js +206 -0
  31. package/src/commands/matrix.js +179 -27
  32. package/src/commands/mcpscaf.js +145 -33
  33. package/src/commands/meminj.js +145 -33
  34. package/src/commands/nostr.js +178 -27
  35. package/src/commands/orchestrate.js +217 -0
  36. package/src/commands/orchgov.js +144 -33
  37. package/src/commands/pdfp.js +109 -27
  38. package/src/commands/perf.js +169 -32
  39. package/src/commands/perm.js +144 -33
  40. package/src/commands/pipeline.js +207 -52
  41. package/src/commands/planmode.js +141 -32
  42. package/src/commands/privacy.js +203 -0
  43. package/src/commands/promcomp.js +111 -27
  44. package/src/commands/recommend.js +177 -34
  45. package/src/commands/reputation.js +208 -0
  46. package/src/commands/sandbox.js +206 -0
  47. package/src/commands/seshhook.js +145 -33
  48. package/src/commands/seshsearch.js +141 -33
  49. package/src/commands/seshtail.js +144 -33
  50. package/src/commands/seshu.js +152 -33
  51. package/src/commands/sganal.js +123 -29
  52. package/src/commands/siem.js +201 -34
  53. package/src/commands/sla.js +212 -0
  54. package/src/commands/slotfill.js +146 -33
  55. package/src/commands/social.js +157 -32
  56. package/src/commands/stress.js +206 -0
  57. package/src/commands/svccont.js +145 -33
  58. package/src/commands/terraform.js +206 -0
  59. package/src/commands/tms.js +171 -33
  60. package/src/commands/topiccls.js +146 -33
  61. package/src/commands/uprof.js +141 -32
  62. package/src/commands/vcheck.js +122 -28
  63. package/src/commands/webfetch.js +141 -32
  64. package/src/commands/zkp.js +184 -28
  65. package/src/index.js +40 -0
  66. package/src/lib/a2a-protocol.js +319 -51
  67. package/src/lib/activitypub-bridge.js +288 -50
  68. package/src/lib/agent-economy.js +304 -51
  69. package/src/lib/app-builder.js +279 -46
  70. package/src/lib/audit-logger.js +321 -0
  71. package/src/lib/autonomous-agent.js +284 -48
  72. package/src/lib/bi-engine.js +283 -49
  73. package/src/lib/bm25-search.js +301 -49
  74. package/src/lib/browser-automation.js +296 -49
  75. package/src/lib/chat-core.js +336 -0
  76. package/src/lib/claude-code-bridge.js +341 -0
  77. package/src/lib/compliance-framework-reporter.js +305 -51
  78. package/src/lib/compliance-manager.js +330 -0
  79. package/src/lib/compression-telemetry.js +301 -49
  80. package/src/lib/content-recommender.js +317 -52
  81. package/src/lib/cowork-cron.js +298 -49
  82. package/src/lib/cowork-learning.js +333 -0
  83. package/src/lib/cowork-task-runner.js +308 -51
  84. package/src/lib/cowork-workflow.js +327 -0
  85. package/src/lib/cross-chain.js +311 -51
  86. package/src/lib/dao-governance.js +280 -46
  87. package/src/lib/dlp-engine.js +287 -49
  88. package/src/lib/evolution-system.js +278 -47
  89. package/src/lib/evomap-manager.js +280 -46
  90. package/src/lib/execution-backend.js +294 -48
  91. package/src/lib/feature-flags.js +294 -49
  92. package/src/lib/git-integration.js +285 -47
  93. package/src/lib/hardening-manager.js +341 -0
  94. package/src/lib/hierarchical-memory.js +284 -48
  95. package/src/lib/inference-network.js +308 -51
  96. package/src/lib/iteration-budget.js +302 -50
  97. package/src/lib/knowledge-graph.js +333 -0
  98. package/src/lib/matrix-bridge.js +281 -47
  99. package/src/lib/mcp-scaffold.js +318 -54
  100. package/src/lib/memory-injection.js +288 -49
  101. package/src/lib/nostr-bridge.js +286 -49
  102. package/src/lib/orchestrator.js +293 -48
  103. package/src/lib/pdf-parser.js +298 -49
  104. package/src/lib/perf-tuning.js +309 -50
  105. package/src/lib/permission-engine.js +287 -49
  106. package/src/lib/pipeline-orchestrator.js +289 -49
  107. package/src/lib/plan-mode.js +298 -51
  108. package/src/lib/privacy-computing.js +335 -0
  109. package/src/lib/reputation-optimizer.js +340 -0
  110. package/src/lib/sandbox-v2.js +327 -0
  111. package/src/lib/service-container.js +313 -52
  112. package/src/lib/session-consolidator.js +296 -49
  113. package/src/lib/session-hooks.js +312 -53
  114. package/src/lib/session-search.js +304 -51
  115. package/src/lib/session-tail.js +288 -49
  116. package/src/lib/session-usage.js +298 -52
  117. package/src/lib/siem-exporter.js +298 -51
  118. package/src/lib/skill-marketplace.js +345 -0
  119. package/src/lib/sla-manager.js +341 -0
  120. package/src/lib/slot-filler.js +303 -51
  121. package/src/lib/social-graph-analytics.js +295 -49
  122. package/src/lib/social-graph.js +272 -49
  123. package/src/lib/stress-tester.js +342 -0
  124. package/src/lib/sub-agent-registry.js +302 -53
  125. package/src/lib/task-model-selector.js +302 -50
  126. package/src/lib/terraform-manager.js +333 -0
  127. package/src/lib/todo-manager.js +281 -47
  128. package/src/lib/token-incentive.js +341 -0
  129. package/src/lib/topic-classifier.js +297 -49
  130. package/src/lib/user-profile.js +294 -50
  131. package/src/lib/version-checker.js +304 -50
  132. package/src/lib/web-fetch.js +292 -51
  133. package/src/lib/zkp-engine.js +286 -49
@@ -847,25 +847,62 @@ export function _resetV2State() {
847
847
  _v2Distributions.length = 0;
848
848
  }
849
849
 
850
-
851
850
  // ===== V2 Surface: Agent Economy governance overlay (CLI v0.137.0) =====
852
851
  export const ECONOMY_ACCOUNT_MATURITY_V2 = Object.freeze({
853
- PENDING: "pending", ACTIVE: "active", FROZEN: "frozen", CLOSED: "closed",
852
+ PENDING: "pending",
853
+ ACTIVE: "active",
854
+ FROZEN: "frozen",
855
+ CLOSED: "closed",
854
856
  });
855
857
  export const ECONOMY_TX_LIFECYCLE_V2 = Object.freeze({
856
- QUEUED: "queued", PROCESSING: "processing", SETTLED: "settled", FAILED: "failed", CANCELLED: "cancelled",
858
+ QUEUED: "queued",
859
+ PROCESSING: "processing",
860
+ SETTLED: "settled",
861
+ FAILED: "failed",
862
+ CANCELLED: "cancelled",
857
863
  });
858
864
 
859
865
  const _econAcctTrans = new Map([
860
- [ECONOMY_ACCOUNT_MATURITY_V2.PENDING, new Set([ECONOMY_ACCOUNT_MATURITY_V2.ACTIVE, ECONOMY_ACCOUNT_MATURITY_V2.CLOSED])],
861
- [ECONOMY_ACCOUNT_MATURITY_V2.ACTIVE, new Set([ECONOMY_ACCOUNT_MATURITY_V2.FROZEN, ECONOMY_ACCOUNT_MATURITY_V2.CLOSED])],
862
- [ECONOMY_ACCOUNT_MATURITY_V2.FROZEN, new Set([ECONOMY_ACCOUNT_MATURITY_V2.ACTIVE, ECONOMY_ACCOUNT_MATURITY_V2.CLOSED])],
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
+ ],
863
887
  [ECONOMY_ACCOUNT_MATURITY_V2.CLOSED, new Set()],
864
888
  ]);
865
889
  const _econAcctTerminal = new Set([ECONOMY_ACCOUNT_MATURITY_V2.CLOSED]);
866
890
  const _econTxTrans = new Map([
867
- [ECONOMY_TX_LIFECYCLE_V2.QUEUED, new Set([ECONOMY_TX_LIFECYCLE_V2.PROCESSING, ECONOMY_TX_LIFECYCLE_V2.CANCELLED])],
868
- [ECONOMY_TX_LIFECYCLE_V2.PROCESSING, new Set([ECONOMY_TX_LIFECYCLE_V2.SETTLED, ECONOMY_TX_LIFECYCLE_V2.FAILED, ECONOMY_TX_LIFECYCLE_V2.CANCELLED])],
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
+ ],
869
906
  [ECONOMY_TX_LIFECYCLE_V2.SETTLED, new Set()],
870
907
  [ECONOMY_TX_LIFECYCLE_V2.FAILED, new Set()],
871
908
  [ECONOMY_TX_LIFECYCLE_V2.CANCELLED, new Set()],
@@ -878,76 +915,292 @@ let _econMaxPendingPerAcct = 30;
878
915
  let _econAcctIdleMs = 7 * 24 * 60 * 60 * 1000;
879
916
  let _econTxStuckMs = 5 * 60 * 1000;
880
917
 
881
- function _econPos(n, lbl) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${lbl} must be positive integer`); return v; }
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
+ }
882
924
 
883
- export function setMaxActiveEconomyAccountsPerHolderV2(n) { _econMaxActivePerHolder = _econPos(n, "maxActiveEconomyAccountsPerHolder"); }
884
- export function getMaxActiveEconomyAccountsPerHolderV2() { return _econMaxActivePerHolder; }
885
- export function setMaxPendingEconomyTxsPerAccountV2(n) { _econMaxPendingPerAcct = _econPos(n, "maxPendingEconomyTxsPerAccount"); }
886
- export function getMaxPendingEconomyTxsPerAccountV2() { return _econMaxPendingPerAcct; }
887
- export function setEconomyAccountIdleMsV2(n) { _econAcctIdleMs = _econPos(n, "economyAccountIdleMs"); }
888
- export function getEconomyAccountIdleMsV2() { return _econAcctIdleMs; }
889
- export function setEconomyTxStuckMsV2(n) { _econTxStuckMs = _econPos(n, "economyTxStuckMs"); }
890
- export function getEconomyTxStuckMsV2() { return _econTxStuckMs; }
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
+ }
891
949
 
892
950
  export function _resetStateAgentEconomyV2() {
893
- _econAccts.clear(); _econTxs.clear();
894
- _econMaxActivePerHolder = 20; _econMaxPendingPerAcct = 30;
895
- _econAcctIdleMs = 7 * 24 * 60 * 60 * 1000; _econTxStuckMs = 5 * 60 * 1000;
951
+ _econAccts.clear();
952
+ _econTxs.clear();
953
+ _econMaxActivePerHolder = 20;
954
+ _econMaxPendingPerAcct = 30;
955
+ _econAcctIdleMs = 7 * 24 * 60 * 60 * 1000;
956
+ _econTxStuckMs = 5 * 60 * 1000;
896
957
  }
897
958
 
898
- export function registerEconomyAccountV2({ id, holder, currency, metadata } = {}) {
959
+ export function registerEconomyAccountV2({
960
+ id,
961
+ holder,
962
+ currency,
963
+ metadata,
964
+ } = {}) {
899
965
  if (!id || typeof id !== "string") throw new Error("id is required");
900
- if (!holder || typeof holder !== "string") throw new Error("holder is required");
901
- if (_econAccts.has(id)) throw new Error(`economy account ${id} already registered`);
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`);
902
970
  const now = Date.now();
903
- const a = { id, holder, currency: currency || "CLC", status: ECONOMY_ACCOUNT_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, closedAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
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
+ };
904
983
  _econAccts.set(id, a);
905
984
  return { ...a, metadata: { ...a.metadata } };
906
985
  }
907
- function _econCheckA(from, to) { const a = _econAcctTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid economy account transition ${from} → ${to}`); }
908
- function _econCountActive(holder) { let n = 0; for (const a of _econAccts.values()) if (a.holder === holder && a.status === ECONOMY_ACCOUNT_MATURITY_V2.ACTIVE) n++; return n; }
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
+ }
909
998
 
910
999
  export function activateEconomyAccountV2(id) {
911
- const a = _econAccts.get(id); if (!a) throw new Error(`economy account ${id} not found`);
1000
+ const a = _econAccts.get(id);
1001
+ if (!a) throw new Error(`economy account ${id} not found`);
912
1002
  _econCheckA(a.status, ECONOMY_ACCOUNT_MATURITY_V2.ACTIVE);
913
1003
  const recovery = a.status === ECONOMY_ACCOUNT_MATURITY_V2.FROZEN;
914
- if (!recovery) { const c = _econCountActive(a.holder); if (c >= _econMaxActivePerHolder) throw new Error(`max active economy accounts per holder (${_econMaxActivePerHolder}) reached for ${a.holder}`); }
915
- const now = Date.now(); a.status = ECONOMY_ACCOUNT_MATURITY_V2.ACTIVE; a.updatedAt = now; a.lastTouchedAt = now; if (!a.activatedAt) a.activatedAt = now;
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();
916
1024
  return { ...a, metadata: { ...a.metadata } };
917
1025
  }
918
- export function freezeEconomyAccountV2(id) { const a = _econAccts.get(id); if (!a) throw new Error(`economy account ${id} not found`); _econCheckA(a.status, ECONOMY_ACCOUNT_MATURITY_V2.FROZEN); a.status = ECONOMY_ACCOUNT_MATURITY_V2.FROZEN; a.updatedAt = Date.now(); return { ...a, metadata: { ...a.metadata } }; }
919
- export function closeEconomyAccountV2(id) { const a = _econAccts.get(id); if (!a) throw new Error(`economy account ${id} not found`); _econCheckA(a.status, ECONOMY_ACCOUNT_MATURITY_V2.CLOSED); const now = Date.now(); a.status = ECONOMY_ACCOUNT_MATURITY_V2.CLOSED; a.updatedAt = now; if (!a.closedAt) a.closedAt = now; return { ...a, metadata: { ...a.metadata } }; }
920
- export function touchEconomyAccountV2(id) { const a = _econAccts.get(id); if (!a) throw new Error(`economy account ${id} not found`); if (_econAcctTerminal.has(a.status)) throw new Error(`cannot touch terminal economy account ${id}`); const now = Date.now(); a.lastTouchedAt = now; a.updatedAt = now; return { ...a, metadata: { ...a.metadata } }; }
921
- export function getEconomyAccountV2(id) { const a = _econAccts.get(id); if (!a) return null; return { ...a, metadata: { ...a.metadata } }; }
922
- export function listEconomyAccountsV2() { return [..._econAccts.values()].map((a) => ({ ...a, metadata: { ...a.metadata } })); }
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
+ }
923
1057
 
924
- function _econCountPending(aid) { let n = 0; for (const t of _econTxs.values()) if (t.accountId === aid && (t.status === ECONOMY_TX_LIFECYCLE_V2.QUEUED || t.status === ECONOMY_TX_LIFECYCLE_V2.PROCESSING)) n++; return n; }
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
+ }
925
1069
 
926
1070
  export function createEconomyTxV2({ id, accountId, amount, metadata } = {}) {
927
1071
  if (!id || typeof id !== "string") throw new Error("id is required");
928
- if (!accountId || typeof accountId !== "string") throw new Error("accountId is required");
1072
+ if (!accountId || typeof accountId !== "string")
1073
+ throw new Error("accountId is required");
929
1074
  if (_econTxs.has(id)) throw new Error(`economy tx ${id} already exists`);
930
- if (!_econAccts.has(accountId)) throw new Error(`economy account ${accountId} not found`);
1075
+ if (!_econAccts.has(accountId))
1076
+ throw new Error(`economy account ${accountId} not found`);
931
1077
  const pending = _econCountPending(accountId);
932
- if (pending >= _econMaxPendingPerAcct) throw new Error(`max pending economy txs per account (${_econMaxPendingPerAcct}) reached for ${accountId}`);
1078
+ if (pending >= _econMaxPendingPerAcct)
1079
+ throw new Error(
1080
+ `max pending economy txs per account (${_econMaxPendingPerAcct}) reached for ${accountId}`,
1081
+ );
933
1082
  const now = Date.now();
934
- const t = { id, accountId, amount: amount || "0", status: ECONOMY_TX_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
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
+ };
935
1094
  _econTxs.set(id, t);
936
1095
  return { ...t, metadata: { ...t.metadata } };
937
1096
  }
938
- function _econCheckT(from, to) { const a = _econTxTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid economy tx transition ${from} → ${to}`); }
939
- export function startEconomyTxV2(id) { const t = _econTxs.get(id); if (!t) throw new Error(`economy tx ${id} not found`); _econCheckT(t.status, ECONOMY_TX_LIFECYCLE_V2.PROCESSING); const now = Date.now(); t.status = ECONOMY_TX_LIFECYCLE_V2.PROCESSING; t.updatedAt = now; if (!t.startedAt) t.startedAt = now; return { ...t, metadata: { ...t.metadata } }; }
940
- export function settleEconomyTxV2(id) { const t = _econTxs.get(id); if (!t) throw new Error(`economy tx ${id} not found`); _econCheckT(t.status, ECONOMY_TX_LIFECYCLE_V2.SETTLED); const now = Date.now(); t.status = ECONOMY_TX_LIFECYCLE_V2.SETTLED; t.updatedAt = now; if (!t.settledAt) t.settledAt = now; return { ...t, metadata: { ...t.metadata } }; }
941
- export function failEconomyTxV2(id, reason) { const t = _econTxs.get(id); if (!t) throw new Error(`economy tx ${id} not found`); _econCheckT(t.status, ECONOMY_TX_LIFECYCLE_V2.FAILED); const now = Date.now(); t.status = ECONOMY_TX_LIFECYCLE_V2.FAILED; t.updatedAt = now; if (!t.settledAt) t.settledAt = now; if (reason) t.metadata.failReason = String(reason); return { ...t, metadata: { ...t.metadata } }; }
942
- export function cancelEconomyTxV2(id, reason) { const t = _econTxs.get(id); if (!t) throw new Error(`economy tx ${id} not found`); _econCheckT(t.status, ECONOMY_TX_LIFECYCLE_V2.CANCELLED); const now = Date.now(); t.status = ECONOMY_TX_LIFECYCLE_V2.CANCELLED; t.updatedAt = now; if (!t.settledAt) t.settledAt = now; if (reason) t.metadata.cancelReason = String(reason); return { ...t, metadata: { ...t.metadata } }; }
943
- export function getEconomyTxV2(id) { const t = _econTxs.get(id); if (!t) return null; return { ...t, metadata: { ...t.metadata } }; }
944
- export function listEconomyTxsV2() { return [..._econTxs.values()].map((t) => ({ ...t, metadata: { ...t.metadata } })); }
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
+ }
945
1155
 
946
- export function autoFreezeIdleEconomyAccountsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const a of _econAccts.values()) if (a.status === ECONOMY_ACCOUNT_MATURITY_V2.ACTIVE && (t - a.lastTouchedAt) >= _econAcctIdleMs) { a.status = ECONOMY_ACCOUNT_MATURITY_V2.FROZEN; a.updatedAt = t; flipped.push(a.id); } return { flipped, count: flipped.length }; }
947
- export function autoFailStuckEconomyTxsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const tx of _econTxs.values()) if (tx.status === ECONOMY_TX_LIFECYCLE_V2.PROCESSING && tx.startedAt != null && (t - tx.startedAt) >= _econTxStuckMs) { tx.status = ECONOMY_TX_LIFECYCLE_V2.FAILED; tx.updatedAt = t; if (!tx.settledAt) tx.settledAt = t; tx.metadata.failReason = "auto-fail-stuck"; flipped.push(tx.id); } return { flipped, count: flipped.length }; }
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
+ }
948
1187
 
949
1188
  export function getAgentEconomyGovStatsV2() {
950
- const accountsByStatus = {}; for (const s of Object.values(ECONOMY_ACCOUNT_MATURITY_V2)) accountsByStatus[s] = 0; for (const a of _econAccts.values()) accountsByStatus[a.status]++;
951
- const txsByStatus = {}; for (const s of Object.values(ECONOMY_TX_LIFECYCLE_V2)) txsByStatus[s] = 0; for (const t of _econTxs.values()) txsByStatus[t.status]++;
952
- return { totalAccountsV2: _econAccts.size, totalTxsV2: _econTxs.size, maxActiveEconomyAccountsPerHolder: _econMaxActivePerHolder, maxPendingEconomyTxsPerAccount: _econMaxPendingPerAcct, economyAccountIdleMs: _econAcctIdleMs, economyTxStuckMs: _econTxStuckMs, accountsByStatus, txsByStatus };
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
+ };
953
1206
  }