chainlesschain 0.143.0 → 0.152.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 (227) hide show
  1. package/package.json +1 -1
  2. package/src/commands/a2a.js +196 -28
  3. package/src/commands/activitypub.js +364 -27
  4. package/src/commands/agent-network.js +217 -0
  5. package/src/commands/agent.js +587 -103
  6. package/src/commands/audit.js +206 -0
  7. package/src/commands/automation.js +201 -0
  8. package/src/commands/bi.js +355 -27
  9. package/src/commands/bm25.js +111 -27
  10. package/src/commands/browse.js +403 -29
  11. package/src/commands/ccron.js +128 -28
  12. package/src/commands/chat.js +207 -0
  13. package/src/commands/codegen.js +207 -0
  14. package/src/commands/collab.js +211 -0
  15. package/src/commands/compliance.js +824 -34
  16. package/src/commands/compt.js +127 -29
  17. package/src/commands/consol.js +8 -2
  18. package/src/commands/cowork.js +783 -34
  19. package/src/commands/crosschain.js +182 -28
  20. package/src/commands/dao.js +182 -28
  21. package/src/commands/dbevo.js +227 -0
  22. package/src/commands/dev.js +207 -0
  23. package/src/commands/did-v2.js +217 -0
  24. package/src/commands/did.js +221 -0
  25. package/src/commands/dlp.js +385 -27
  26. package/src/commands/economy.js +205 -50
  27. package/src/commands/evolution.js +203 -50
  28. package/src/commands/evomap.js +387 -27
  29. package/src/commands/export.js +213 -0
  30. package/src/commands/federation.js +209 -0
  31. package/src/commands/fflag.js +74 -22
  32. package/src/commands/fusion.js +205 -0
  33. package/src/commands/git.js +177 -37
  34. package/src/commands/governance.js +209 -0
  35. package/src/commands/hardening.js +209 -0
  36. package/src/commands/hmemory.js +204 -50
  37. package/src/commands/hook.js +209 -0
  38. package/src/commands/import.js +209 -0
  39. package/src/commands/incentive.js +209 -0
  40. package/src/commands/inference.js +170 -34
  41. package/src/commands/infra.js +203 -0
  42. package/src/commands/instinct.js +209 -0
  43. package/src/commands/ipfs.js +207 -0
  44. package/src/commands/itbudget.js +149 -33
  45. package/src/commands/kg.js +206 -0
  46. package/src/commands/llm.js +217 -0
  47. package/src/commands/lowcode.js +195 -38
  48. package/src/commands/marketplace.js +206 -0
  49. package/src/commands/matrix.js +386 -27
  50. package/src/commands/mcp.js +217 -0
  51. package/src/commands/mcpscaf.js +145 -33
  52. package/src/commands/meminj.js +145 -33
  53. package/src/commands/memory.js +209 -0
  54. package/src/commands/multimodal.js +203 -0
  55. package/src/commands/nlprog.js +225 -0
  56. package/src/commands/nostr.js +387 -27
  57. package/src/commands/note.js +205 -0
  58. package/src/commands/ops.js +219 -0
  59. package/src/commands/orchestrate.js +217 -0
  60. package/src/commands/orchgov.js +144 -33
  61. package/src/commands/org.js +209 -0
  62. package/src/commands/p2p.js +209 -0
  63. package/src/commands/pdfp.js +109 -27
  64. package/src/commands/perception.js +209 -0
  65. package/src/commands/perf.js +169 -32
  66. package/src/commands/perm.js +144 -33
  67. package/src/commands/permmem.js +203 -0
  68. package/src/commands/pipeline.js +207 -52
  69. package/src/commands/planmode.js +141 -32
  70. package/src/commands/plugin-ecosystem.js +209 -0
  71. package/src/commands/pqc.js +213 -0
  72. package/src/commands/privacy.js +203 -0
  73. package/src/commands/promcomp.js +111 -27
  74. package/src/commands/quantization.js +207 -0
  75. package/src/commands/rcache.js +205 -0
  76. package/src/commands/recommend.js +410 -34
  77. package/src/commands/reputation.js +208 -0
  78. package/src/commands/runtime.js +205 -0
  79. package/src/commands/sandbox.js +206 -0
  80. package/src/commands/scim.js +209 -0
  81. package/src/commands/seshhook.js +145 -33
  82. package/src/commands/seshsearch.js +141 -33
  83. package/src/commands/seshtail.js +144 -33
  84. package/src/commands/seshu.js +152 -33
  85. package/src/commands/session.js +209 -0
  86. package/src/commands/sganal.js +123 -29
  87. package/src/commands/siem.js +201 -34
  88. package/src/commands/skill.js +207 -0
  89. package/src/commands/sla.js +212 -0
  90. package/src/commands/slotfill.js +146 -33
  91. package/src/commands/social.js +358 -32
  92. package/src/commands/sso.js +209 -0
  93. package/src/commands/stress.js +206 -0
  94. package/src/commands/svccont.js +145 -33
  95. package/src/commands/sync.js +209 -0
  96. package/src/commands/tech.js +209 -0
  97. package/src/commands/tenant.js +217 -0
  98. package/src/commands/terraform.js +206 -0
  99. package/src/commands/tms.js +171 -33
  100. package/src/commands/tokens.js +209 -0
  101. package/src/commands/topiccls.js +146 -33
  102. package/src/commands/trust.js +217 -0
  103. package/src/commands/uprof.js +141 -32
  104. package/src/commands/vcheck.js +122 -28
  105. package/src/commands/wallet.js +209 -0
  106. package/src/commands/webfetch.js +141 -32
  107. package/src/commands/workflow.js +203 -0
  108. package/src/commands/zkp.js +184 -28
  109. package/src/index.js +180 -0
  110. package/src/lib/a2a-protocol.js +319 -51
  111. package/src/lib/activitypub-bridge.js +622 -50
  112. package/src/lib/agent-economy.js +304 -51
  113. package/src/lib/agent-network.js +341 -0
  114. package/src/lib/aiops.js +346 -0
  115. package/src/lib/app-builder.js +279 -46
  116. package/src/lib/audit-logger.js +321 -0
  117. package/src/lib/automation-engine.js +335 -0
  118. package/src/lib/autonomous-agent.js +284 -48
  119. package/src/lib/autonomous-developer.js +332 -0
  120. package/src/lib/bi-engine.js +616 -49
  121. package/src/lib/bm25-search.js +301 -49
  122. package/src/lib/browser-automation.js +630 -49
  123. package/src/lib/chat-core.js +336 -0
  124. package/src/lib/claude-code-bridge.js +341 -0
  125. package/src/lib/code-agent.js +339 -0
  126. package/src/lib/collaboration-governance.js +334 -0
  127. package/src/lib/community-governance.js +346 -0
  128. package/src/lib/compliance-framework-reporter.js +305 -51
  129. package/src/lib/compliance-manager.js +330 -0
  130. package/src/lib/compression-telemetry.js +301 -49
  131. package/src/lib/content-recommendation.js +351 -0
  132. package/src/lib/content-recommender.js +317 -52
  133. package/src/lib/cowork-cron.js +298 -49
  134. package/src/lib/cowork-learning.js +333 -0
  135. package/src/lib/cowork-share.js +338 -0
  136. package/src/lib/cowork-task-runner.js +308 -51
  137. package/src/lib/cowork-workflow.js +327 -0
  138. package/src/lib/cross-chain.js +311 -51
  139. package/src/lib/dao-governance.js +280 -46
  140. package/src/lib/dbevo.js +351 -0
  141. package/src/lib/decentral-infra.js +330 -0
  142. package/src/lib/did-manager.js +341 -0
  143. package/src/lib/did-v2-manager.js +341 -0
  144. package/src/lib/dlp-engine.js +626 -49
  145. package/src/lib/evolution-system.js +278 -47
  146. package/src/lib/evomap-governance.js +334 -0
  147. package/src/lib/evomap-manager.js +280 -46
  148. package/src/lib/execution-backend.js +294 -48
  149. package/src/lib/feature-flags.js +294 -49
  150. package/src/lib/federation-hardening.js +340 -0
  151. package/src/lib/git-integration.js +285 -47
  152. package/src/lib/hardening-manager.js +341 -0
  153. package/src/lib/hierarchical-memory.js +284 -48
  154. package/src/lib/hook-manager.js +341 -0
  155. package/src/lib/inference-network.js +308 -51
  156. package/src/lib/instinct-manager.js +346 -0
  157. package/src/lib/ipfs-storage.js +334 -0
  158. package/src/lib/iteration-budget.js +302 -50
  159. package/src/lib/knowledge-exporter.js +341 -0
  160. package/src/lib/knowledge-graph.js +333 -0
  161. package/src/lib/knowledge-importer.js +341 -0
  162. package/src/lib/llm-providers.js +346 -0
  163. package/src/lib/matrix-bridge.js +620 -47
  164. package/src/lib/mcp-registry.js +346 -0
  165. package/src/lib/mcp-scaffold.js +318 -54
  166. package/src/lib/memory-injection.js +288 -49
  167. package/src/lib/memory-manager.js +336 -0
  168. package/src/lib/multimodal.js +330 -0
  169. package/src/lib/nl-programming.js +341 -0
  170. package/src/lib/nostr-bridge.js +622 -49
  171. package/src/lib/note-versioning.js +339 -0
  172. package/src/lib/orchestrator.js +293 -48
  173. package/src/lib/org-manager.js +336 -0
  174. package/src/lib/p2p-manager.js +341 -0
  175. package/src/lib/pdf-parser.js +298 -49
  176. package/src/lib/perception.js +346 -0
  177. package/src/lib/perf-tuning.js +309 -50
  178. package/src/lib/permanent-memory.js +327 -0
  179. package/src/lib/permission-engine.js +287 -49
  180. package/src/lib/pipeline-orchestrator.js +289 -49
  181. package/src/lib/plan-mode.js +298 -51
  182. package/src/lib/plugin-ecosystem.js +346 -0
  183. package/src/lib/pqc-manager.js +346 -0
  184. package/src/lib/privacy-computing.js +335 -0
  185. package/src/lib/protocol-fusion.js +338 -0
  186. package/src/lib/quantization.js +337 -0
  187. package/src/lib/reputation-optimizer.js +340 -0
  188. package/src/lib/response-cache.js +333 -0
  189. package/src/lib/sandbox-v2.js +327 -0
  190. package/src/lib/scim-manager.js +346 -0
  191. package/src/lib/service-container.js +313 -52
  192. package/src/lib/session-consolidator.js +296 -49
  193. package/src/lib/session-hooks.js +312 -53
  194. package/src/lib/session-manager.js +334 -0
  195. package/src/lib/session-search.js +304 -51
  196. package/src/lib/session-tail.js +288 -49
  197. package/src/lib/session-usage.js +298 -52
  198. package/src/lib/siem-exporter.js +298 -51
  199. package/src/lib/skill-loader.js +334 -0
  200. package/src/lib/skill-marketplace.js +345 -0
  201. package/src/lib/sla-manager.js +341 -0
  202. package/src/lib/slot-filler.js +303 -51
  203. package/src/lib/social-graph-analytics.js +295 -49
  204. package/src/lib/social-graph.js +272 -49
  205. package/src/lib/social-manager.js +330 -0
  206. package/src/lib/sso-manager.js +340 -0
  207. package/src/lib/stress-tester.js +342 -0
  208. package/src/lib/sub-agent-registry.js +302 -53
  209. package/src/lib/sync-manager.js +336 -0
  210. package/src/lib/task-model-selector.js +302 -50
  211. package/src/lib/tech-learning-engine.js +341 -0
  212. package/src/lib/tenant-saas.js +341 -0
  213. package/src/lib/terraform-manager.js +333 -0
  214. package/src/lib/threat-intel.js +330 -0
  215. package/src/lib/todo-manager.js +281 -47
  216. package/src/lib/token-incentive.js +341 -0
  217. package/src/lib/token-tracker.js +336 -0
  218. package/src/lib/topic-classifier.js +297 -49
  219. package/src/lib/trust-security.js +343 -0
  220. package/src/lib/ueba.js +340 -0
  221. package/src/lib/universal-runtime.js +330 -0
  222. package/src/lib/user-profile.js +294 -50
  223. package/src/lib/version-checker.js +304 -50
  224. package/src/lib/wallet-manager.js +336 -0
  225. package/src/lib/web-fetch.js +292 -51
  226. package/src/lib/workflow-engine.js +330 -0
  227. package/src/lib/zkp-engine.js +286 -49
@@ -804,3 +804,344 @@ export function _resetStateV2() {
804
804
  _claimExpiryMs = TOKEN_DEFAULT_CLAIM_EXPIRY_MS;
805
805
  _maxClaimAmount = TOKEN_DEFAULT_MAX_CLAIM_AMOUNT;
806
806
  }
807
+
808
+ // =====================================================================
809
+ // token-incentive V2 governance overlay (iter17)
810
+ // =====================================================================
811
+ export const INCGOV_PROFILE_MATURITY_V2 = Object.freeze({
812
+ PENDING: "pending",
813
+ ACTIVE: "active",
814
+ PAUSED: "paused",
815
+ ARCHIVED: "archived",
816
+ });
817
+ export const INCGOV_PAYOUT_LIFECYCLE_V2 = Object.freeze({
818
+ QUEUED: "queued",
819
+ PROCESSING: "processing",
820
+ PAID: "paid",
821
+ FAILED: "failed",
822
+ CANCELLED: "cancelled",
823
+ });
824
+ const _incgovPTrans = new Map([
825
+ [
826
+ INCGOV_PROFILE_MATURITY_V2.PENDING,
827
+ new Set([
828
+ INCGOV_PROFILE_MATURITY_V2.ACTIVE,
829
+ INCGOV_PROFILE_MATURITY_V2.ARCHIVED,
830
+ ]),
831
+ ],
832
+ [
833
+ INCGOV_PROFILE_MATURITY_V2.ACTIVE,
834
+ new Set([
835
+ INCGOV_PROFILE_MATURITY_V2.PAUSED,
836
+ INCGOV_PROFILE_MATURITY_V2.ARCHIVED,
837
+ ]),
838
+ ],
839
+ [
840
+ INCGOV_PROFILE_MATURITY_V2.PAUSED,
841
+ new Set([
842
+ INCGOV_PROFILE_MATURITY_V2.ACTIVE,
843
+ INCGOV_PROFILE_MATURITY_V2.ARCHIVED,
844
+ ]),
845
+ ],
846
+ [INCGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
847
+ ]);
848
+ const _incgovPTerminal = new Set([INCGOV_PROFILE_MATURITY_V2.ARCHIVED]);
849
+ const _incgovJTrans = new Map([
850
+ [
851
+ INCGOV_PAYOUT_LIFECYCLE_V2.QUEUED,
852
+ new Set([
853
+ INCGOV_PAYOUT_LIFECYCLE_V2.PROCESSING,
854
+ INCGOV_PAYOUT_LIFECYCLE_V2.CANCELLED,
855
+ ]),
856
+ ],
857
+ [
858
+ INCGOV_PAYOUT_LIFECYCLE_V2.PROCESSING,
859
+ new Set([
860
+ INCGOV_PAYOUT_LIFECYCLE_V2.PAID,
861
+ INCGOV_PAYOUT_LIFECYCLE_V2.FAILED,
862
+ INCGOV_PAYOUT_LIFECYCLE_V2.CANCELLED,
863
+ ]),
864
+ ],
865
+ [INCGOV_PAYOUT_LIFECYCLE_V2.PAID, new Set()],
866
+ [INCGOV_PAYOUT_LIFECYCLE_V2.FAILED, new Set()],
867
+ [INCGOV_PAYOUT_LIFECYCLE_V2.CANCELLED, new Set()],
868
+ ]);
869
+ const _incgovPsV2 = new Map();
870
+ const _incgovJsV2 = new Map();
871
+ let _incgovMaxActive = 10,
872
+ _incgovMaxPending = 30,
873
+ _incgovIdleMs = 30 * 24 * 60 * 60 * 1000,
874
+ _incgovStuckMs = 60 * 1000;
875
+ function _incgovPos(n, label) {
876
+ const v = Math.floor(Number(n));
877
+ if (!Number.isFinite(v) || v <= 0)
878
+ throw new Error(`${label} must be positive integer`);
879
+ return v;
880
+ }
881
+ function _incgovCheckP(from, to) {
882
+ const a = _incgovPTrans.get(from);
883
+ if (!a || !a.has(to))
884
+ throw new Error(`invalid incgov profile transition ${from} → ${to}`);
885
+ }
886
+ function _incgovCheckJ(from, to) {
887
+ const a = _incgovJTrans.get(from);
888
+ if (!a || !a.has(to))
889
+ throw new Error(`invalid incgov payout transition ${from} → ${to}`);
890
+ }
891
+ function _incgovCountActive(owner) {
892
+ let c = 0;
893
+ for (const p of _incgovPsV2.values())
894
+ if (p.owner === owner && p.status === INCGOV_PROFILE_MATURITY_V2.ACTIVE)
895
+ c++;
896
+ return c;
897
+ }
898
+ function _incgovCountPending(profileId) {
899
+ let c = 0;
900
+ for (const j of _incgovJsV2.values())
901
+ if (
902
+ j.profileId === profileId &&
903
+ (j.status === INCGOV_PAYOUT_LIFECYCLE_V2.QUEUED ||
904
+ j.status === INCGOV_PAYOUT_LIFECYCLE_V2.PROCESSING)
905
+ )
906
+ c++;
907
+ return c;
908
+ }
909
+ export function setMaxActiveIncgovProfilesPerOwnerV2(n) {
910
+ _incgovMaxActive = _incgovPos(n, "maxActiveIncgovProfilesPerOwner");
911
+ }
912
+ export function getMaxActiveIncgovProfilesPerOwnerV2() {
913
+ return _incgovMaxActive;
914
+ }
915
+ export function setMaxPendingIncgovPayoutsPerProfileV2(n) {
916
+ _incgovMaxPending = _incgovPos(n, "maxPendingIncgovPayoutsPerProfile");
917
+ }
918
+ export function getMaxPendingIncgovPayoutsPerProfileV2() {
919
+ return _incgovMaxPending;
920
+ }
921
+ export function setIncgovProfileIdleMsV2(n) {
922
+ _incgovIdleMs = _incgovPos(n, "incgovProfileIdleMs");
923
+ }
924
+ export function getIncgovProfileIdleMsV2() {
925
+ return _incgovIdleMs;
926
+ }
927
+ export function setIncgovPayoutStuckMsV2(n) {
928
+ _incgovStuckMs = _incgovPos(n, "incgovPayoutStuckMs");
929
+ }
930
+ export function getIncgovPayoutStuckMsV2() {
931
+ return _incgovStuckMs;
932
+ }
933
+ export function _resetStateTokenIncentiveV2() {
934
+ _incgovPsV2.clear();
935
+ _incgovJsV2.clear();
936
+ _incgovMaxActive = 10;
937
+ _incgovMaxPending = 30;
938
+ _incgovIdleMs = 30 * 24 * 60 * 60 * 1000;
939
+ _incgovStuckMs = 60 * 1000;
940
+ }
941
+ export function registerIncgovProfileV2({ id, owner, token, metadata } = {}) {
942
+ if (!id || !owner) throw new Error("id and owner required");
943
+ if (_incgovPsV2.has(id))
944
+ throw new Error(`incgov profile ${id} already exists`);
945
+ const now = Date.now();
946
+ const p = {
947
+ id,
948
+ owner,
949
+ token: token || "CLC",
950
+ status: INCGOV_PROFILE_MATURITY_V2.PENDING,
951
+ createdAt: now,
952
+ updatedAt: now,
953
+ lastTouchedAt: now,
954
+ activatedAt: null,
955
+ archivedAt: null,
956
+ metadata: { ...(metadata || {}) },
957
+ };
958
+ _incgovPsV2.set(id, p);
959
+ return { ...p, metadata: { ...p.metadata } };
960
+ }
961
+ export function activateIncgovProfileV2(id) {
962
+ const p = _incgovPsV2.get(id);
963
+ if (!p) throw new Error(`incgov profile ${id} not found`);
964
+ const isInitial = p.status === INCGOV_PROFILE_MATURITY_V2.PENDING;
965
+ _incgovCheckP(p.status, INCGOV_PROFILE_MATURITY_V2.ACTIVE);
966
+ if (isInitial && _incgovCountActive(p.owner) >= _incgovMaxActive)
967
+ throw new Error(`max active incgov profiles for owner ${p.owner} reached`);
968
+ const now = Date.now();
969
+ p.status = INCGOV_PROFILE_MATURITY_V2.ACTIVE;
970
+ p.updatedAt = now;
971
+ p.lastTouchedAt = now;
972
+ if (!p.activatedAt) p.activatedAt = now;
973
+ return { ...p, metadata: { ...p.metadata } };
974
+ }
975
+ export function pauseIncgovProfileV2(id) {
976
+ const p = _incgovPsV2.get(id);
977
+ if (!p) throw new Error(`incgov profile ${id} not found`);
978
+ _incgovCheckP(p.status, INCGOV_PROFILE_MATURITY_V2.PAUSED);
979
+ p.status = INCGOV_PROFILE_MATURITY_V2.PAUSED;
980
+ p.updatedAt = Date.now();
981
+ return { ...p, metadata: { ...p.metadata } };
982
+ }
983
+ export function archiveIncgovProfileV2(id) {
984
+ const p = _incgovPsV2.get(id);
985
+ if (!p) throw new Error(`incgov profile ${id} not found`);
986
+ _incgovCheckP(p.status, INCGOV_PROFILE_MATURITY_V2.ARCHIVED);
987
+ const now = Date.now();
988
+ p.status = INCGOV_PROFILE_MATURITY_V2.ARCHIVED;
989
+ p.updatedAt = now;
990
+ if (!p.archivedAt) p.archivedAt = now;
991
+ return { ...p, metadata: { ...p.metadata } };
992
+ }
993
+ export function touchIncgovProfileV2(id) {
994
+ const p = _incgovPsV2.get(id);
995
+ if (!p) throw new Error(`incgov profile ${id} not found`);
996
+ if (_incgovPTerminal.has(p.status))
997
+ throw new Error(`cannot touch terminal incgov profile ${id}`);
998
+ const now = Date.now();
999
+ p.lastTouchedAt = now;
1000
+ p.updatedAt = now;
1001
+ return { ...p, metadata: { ...p.metadata } };
1002
+ }
1003
+ export function getIncgovProfileV2(id) {
1004
+ const p = _incgovPsV2.get(id);
1005
+ if (!p) return null;
1006
+ return { ...p, metadata: { ...p.metadata } };
1007
+ }
1008
+ export function listIncgovProfilesV2() {
1009
+ return [..._incgovPsV2.values()].map((p) => ({
1010
+ ...p,
1011
+ metadata: { ...p.metadata },
1012
+ }));
1013
+ }
1014
+ export function createIncgovPayoutV2({
1015
+ id,
1016
+ profileId,
1017
+ recipient,
1018
+ metadata,
1019
+ } = {}) {
1020
+ if (!id || !profileId) throw new Error("id and profileId required");
1021
+ if (_incgovJsV2.has(id))
1022
+ throw new Error(`incgov payout ${id} already exists`);
1023
+ if (!_incgovPsV2.has(profileId))
1024
+ throw new Error(`incgov profile ${profileId} not found`);
1025
+ if (_incgovCountPending(profileId) >= _incgovMaxPending)
1026
+ throw new Error(
1027
+ `max pending incgov payouts for profile ${profileId} reached`,
1028
+ );
1029
+ const now = Date.now();
1030
+ const j = {
1031
+ id,
1032
+ profileId,
1033
+ recipient: recipient || "",
1034
+ status: INCGOV_PAYOUT_LIFECYCLE_V2.QUEUED,
1035
+ createdAt: now,
1036
+ updatedAt: now,
1037
+ startedAt: null,
1038
+ settledAt: null,
1039
+ metadata: { ...(metadata || {}) },
1040
+ };
1041
+ _incgovJsV2.set(id, j);
1042
+ return { ...j, metadata: { ...j.metadata } };
1043
+ }
1044
+ export function processingIncgovPayoutV2(id) {
1045
+ const j = _incgovJsV2.get(id);
1046
+ if (!j) throw new Error(`incgov payout ${id} not found`);
1047
+ _incgovCheckJ(j.status, INCGOV_PAYOUT_LIFECYCLE_V2.PROCESSING);
1048
+ const now = Date.now();
1049
+ j.status = INCGOV_PAYOUT_LIFECYCLE_V2.PROCESSING;
1050
+ j.updatedAt = now;
1051
+ if (!j.startedAt) j.startedAt = now;
1052
+ return { ...j, metadata: { ...j.metadata } };
1053
+ }
1054
+ export function completePayoutIncgovV2(id) {
1055
+ const j = _incgovJsV2.get(id);
1056
+ if (!j) throw new Error(`incgov payout ${id} not found`);
1057
+ _incgovCheckJ(j.status, INCGOV_PAYOUT_LIFECYCLE_V2.PAID);
1058
+ const now = Date.now();
1059
+ j.status = INCGOV_PAYOUT_LIFECYCLE_V2.PAID;
1060
+ j.updatedAt = now;
1061
+ if (!j.settledAt) j.settledAt = now;
1062
+ return { ...j, metadata: { ...j.metadata } };
1063
+ }
1064
+ export function failIncgovPayoutV2(id, reason) {
1065
+ const j = _incgovJsV2.get(id);
1066
+ if (!j) throw new Error(`incgov payout ${id} not found`);
1067
+ _incgovCheckJ(j.status, INCGOV_PAYOUT_LIFECYCLE_V2.FAILED);
1068
+ const now = Date.now();
1069
+ j.status = INCGOV_PAYOUT_LIFECYCLE_V2.FAILED;
1070
+ j.updatedAt = now;
1071
+ if (!j.settledAt) j.settledAt = now;
1072
+ if (reason) j.metadata.failReason = String(reason);
1073
+ return { ...j, metadata: { ...j.metadata } };
1074
+ }
1075
+ export function cancelIncgovPayoutV2(id, reason) {
1076
+ const j = _incgovJsV2.get(id);
1077
+ if (!j) throw new Error(`incgov payout ${id} not found`);
1078
+ _incgovCheckJ(j.status, INCGOV_PAYOUT_LIFECYCLE_V2.CANCELLED);
1079
+ const now = Date.now();
1080
+ j.status = INCGOV_PAYOUT_LIFECYCLE_V2.CANCELLED;
1081
+ j.updatedAt = now;
1082
+ if (!j.settledAt) j.settledAt = now;
1083
+ if (reason) j.metadata.cancelReason = String(reason);
1084
+ return { ...j, metadata: { ...j.metadata } };
1085
+ }
1086
+ export function getIncgovPayoutV2(id) {
1087
+ const j = _incgovJsV2.get(id);
1088
+ if (!j) return null;
1089
+ return { ...j, metadata: { ...j.metadata } };
1090
+ }
1091
+ export function listIncgovPayoutsV2() {
1092
+ return [..._incgovJsV2.values()].map((j) => ({
1093
+ ...j,
1094
+ metadata: { ...j.metadata },
1095
+ }));
1096
+ }
1097
+ export function autoPauseIdleIncgovProfilesV2({ now } = {}) {
1098
+ const t = now ?? Date.now();
1099
+ const flipped = [];
1100
+ for (const p of _incgovPsV2.values())
1101
+ if (
1102
+ p.status === INCGOV_PROFILE_MATURITY_V2.ACTIVE &&
1103
+ t - p.lastTouchedAt >= _incgovIdleMs
1104
+ ) {
1105
+ p.status = INCGOV_PROFILE_MATURITY_V2.PAUSED;
1106
+ p.updatedAt = t;
1107
+ flipped.push(p.id);
1108
+ }
1109
+ return { flipped, count: flipped.length };
1110
+ }
1111
+ export function autoFailStuckIncgovPayoutsV2({ now } = {}) {
1112
+ const t = now ?? Date.now();
1113
+ const flipped = [];
1114
+ for (const j of _incgovJsV2.values())
1115
+ if (
1116
+ j.status === INCGOV_PAYOUT_LIFECYCLE_V2.PROCESSING &&
1117
+ j.startedAt != null &&
1118
+ t - j.startedAt >= _incgovStuckMs
1119
+ ) {
1120
+ j.status = INCGOV_PAYOUT_LIFECYCLE_V2.FAILED;
1121
+ j.updatedAt = t;
1122
+ if (!j.settledAt) j.settledAt = t;
1123
+ j.metadata.failReason = "auto-fail-stuck";
1124
+ flipped.push(j.id);
1125
+ }
1126
+ return { flipped, count: flipped.length };
1127
+ }
1128
+ export function getTokenIncentiveGovStatsV2() {
1129
+ const profilesByStatus = {};
1130
+ for (const v of Object.values(INCGOV_PROFILE_MATURITY_V2))
1131
+ profilesByStatus[v] = 0;
1132
+ for (const p of _incgovPsV2.values()) profilesByStatus[p.status]++;
1133
+ const payoutsByStatus = {};
1134
+ for (const v of Object.values(INCGOV_PAYOUT_LIFECYCLE_V2))
1135
+ payoutsByStatus[v] = 0;
1136
+ for (const j of _incgovJsV2.values()) payoutsByStatus[j.status]++;
1137
+ return {
1138
+ totalIncgovProfilesV2: _incgovPsV2.size,
1139
+ totalIncgovPayoutsV2: _incgovJsV2.size,
1140
+ maxActiveIncgovProfilesPerOwner: _incgovMaxActive,
1141
+ maxPendingIncgovPayoutsPerProfile: _incgovMaxPending,
1142
+ incgovProfileIdleMs: _incgovIdleMs,
1143
+ incgovPayoutStuckMs: _incgovStuckMs,
1144
+ profilesByStatus,
1145
+ payoutsByStatus,
1146
+ };
1147
+ }
@@ -527,3 +527,339 @@ export function _resetStateTokenTrackerV2() {
527
527
  _budgetIdleMsV2 = TOKEN_DEFAULT_BUDGET_IDLE_MS;
528
528
  _recordStuckMsV2 = TOKEN_DEFAULT_RECORD_STUCK_MS;
529
529
  }
530
+
531
+ // =====================================================================
532
+ // token-tracker V2 governance overlay (iter24)
533
+ // =====================================================================
534
+ export const TOKTGOV_PROFILE_MATURITY_V2 = Object.freeze({
535
+ PENDING: "pending",
536
+ ACTIVE: "active",
537
+ STALE: "stale",
538
+ ARCHIVED: "archived",
539
+ });
540
+ export const TOKTGOV_USAGE_LIFECYCLE_V2 = Object.freeze({
541
+ QUEUED: "queued",
542
+ RECORDING: "recording",
543
+ RECORDED: "recorded",
544
+ FAILED: "failed",
545
+ CANCELLED: "cancelled",
546
+ });
547
+ const _toktgovPTrans = new Map([
548
+ [
549
+ TOKTGOV_PROFILE_MATURITY_V2.PENDING,
550
+ new Set([
551
+ TOKTGOV_PROFILE_MATURITY_V2.ACTIVE,
552
+ TOKTGOV_PROFILE_MATURITY_V2.ARCHIVED,
553
+ ]),
554
+ ],
555
+ [
556
+ TOKTGOV_PROFILE_MATURITY_V2.ACTIVE,
557
+ new Set([
558
+ TOKTGOV_PROFILE_MATURITY_V2.STALE,
559
+ TOKTGOV_PROFILE_MATURITY_V2.ARCHIVED,
560
+ ]),
561
+ ],
562
+ [
563
+ TOKTGOV_PROFILE_MATURITY_V2.STALE,
564
+ new Set([
565
+ TOKTGOV_PROFILE_MATURITY_V2.ACTIVE,
566
+ TOKTGOV_PROFILE_MATURITY_V2.ARCHIVED,
567
+ ]),
568
+ ],
569
+ [TOKTGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
570
+ ]);
571
+ const _toktgovPTerminal = new Set([TOKTGOV_PROFILE_MATURITY_V2.ARCHIVED]);
572
+ const _toktgovJTrans = new Map([
573
+ [
574
+ TOKTGOV_USAGE_LIFECYCLE_V2.QUEUED,
575
+ new Set([
576
+ TOKTGOV_USAGE_LIFECYCLE_V2.RECORDING,
577
+ TOKTGOV_USAGE_LIFECYCLE_V2.CANCELLED,
578
+ ]),
579
+ ],
580
+ [
581
+ TOKTGOV_USAGE_LIFECYCLE_V2.RECORDING,
582
+ new Set([
583
+ TOKTGOV_USAGE_LIFECYCLE_V2.RECORDED,
584
+ TOKTGOV_USAGE_LIFECYCLE_V2.FAILED,
585
+ TOKTGOV_USAGE_LIFECYCLE_V2.CANCELLED,
586
+ ]),
587
+ ],
588
+ [TOKTGOV_USAGE_LIFECYCLE_V2.RECORDED, new Set()],
589
+ [TOKTGOV_USAGE_LIFECYCLE_V2.FAILED, new Set()],
590
+ [TOKTGOV_USAGE_LIFECYCLE_V2.CANCELLED, new Set()],
591
+ ]);
592
+ const _toktgovPsV2 = new Map();
593
+ const _toktgovJsV2 = new Map();
594
+ let _toktgovMaxActive = 8,
595
+ _toktgovMaxPending = 20,
596
+ _toktgovIdleMs = 30 * 24 * 60 * 60 * 1000,
597
+ _toktgovStuckMs = 60 * 1000;
598
+ function _toktgovPos(n, label) {
599
+ const v = Math.floor(Number(n));
600
+ if (!Number.isFinite(v) || v <= 0)
601
+ throw new Error(`${label} must be positive integer`);
602
+ return v;
603
+ }
604
+ function _toktgovCheckP(from, to) {
605
+ const a = _toktgovPTrans.get(from);
606
+ if (!a || !a.has(to))
607
+ throw new Error(`invalid toktgov profile transition ${from} → ${to}`);
608
+ }
609
+ function _toktgovCheckJ(from, to) {
610
+ const a = _toktgovJTrans.get(from);
611
+ if (!a || !a.has(to))
612
+ throw new Error(`invalid toktgov usage transition ${from} → ${to}`);
613
+ }
614
+ function _toktgovCountActive(owner) {
615
+ let c = 0;
616
+ for (const p of _toktgovPsV2.values())
617
+ if (p.owner === owner && p.status === TOKTGOV_PROFILE_MATURITY_V2.ACTIVE)
618
+ c++;
619
+ return c;
620
+ }
621
+ function _toktgovCountPending(profileId) {
622
+ let c = 0;
623
+ for (const j of _toktgovJsV2.values())
624
+ if (
625
+ j.profileId === profileId &&
626
+ (j.status === TOKTGOV_USAGE_LIFECYCLE_V2.QUEUED ||
627
+ j.status === TOKTGOV_USAGE_LIFECYCLE_V2.RECORDING)
628
+ )
629
+ c++;
630
+ return c;
631
+ }
632
+ export function setMaxActiveToktgovProfilesPerOwnerV2(n) {
633
+ _toktgovMaxActive = _toktgovPos(n, "maxActiveToktgovProfilesPerOwner");
634
+ }
635
+ export function getMaxActiveToktgovProfilesPerOwnerV2() {
636
+ return _toktgovMaxActive;
637
+ }
638
+ export function setMaxPendingToktgovUsagesPerProfileV2(n) {
639
+ _toktgovMaxPending = _toktgovPos(n, "maxPendingToktgovUsagesPerProfile");
640
+ }
641
+ export function getMaxPendingToktgovUsagesPerProfileV2() {
642
+ return _toktgovMaxPending;
643
+ }
644
+ export function setToktgovProfileIdleMsV2(n) {
645
+ _toktgovIdleMs = _toktgovPos(n, "toktgovProfileIdleMs");
646
+ }
647
+ export function getToktgovProfileIdleMsV2() {
648
+ return _toktgovIdleMs;
649
+ }
650
+ export function setToktgovUsageStuckMsV2(n) {
651
+ _toktgovStuckMs = _toktgovPos(n, "toktgovUsageStuckMs");
652
+ }
653
+ export function getToktgovUsageStuckMsV2() {
654
+ return _toktgovStuckMs;
655
+ }
656
+ export function _resetStateTokenTrackerGovV2() {
657
+ _toktgovPsV2.clear();
658
+ _toktgovJsV2.clear();
659
+ _toktgovMaxActive = 8;
660
+ _toktgovMaxPending = 20;
661
+ _toktgovIdleMs = 30 * 24 * 60 * 60 * 1000;
662
+ _toktgovStuckMs = 60 * 1000;
663
+ }
664
+ export function registerToktgovProfileV2({ id, owner, budget, metadata } = {}) {
665
+ if (!id || !owner) throw new Error("id and owner required");
666
+ if (_toktgovPsV2.has(id))
667
+ throw new Error(`toktgov profile ${id} already exists`);
668
+ const now = Date.now();
669
+ const p = {
670
+ id,
671
+ owner,
672
+ budget: budget || "default",
673
+ status: TOKTGOV_PROFILE_MATURITY_V2.PENDING,
674
+ createdAt: now,
675
+ updatedAt: now,
676
+ lastTouchedAt: now,
677
+ activatedAt: null,
678
+ archivedAt: null,
679
+ metadata: { ...(metadata || {}) },
680
+ };
681
+ _toktgovPsV2.set(id, p);
682
+ return { ...p, metadata: { ...p.metadata } };
683
+ }
684
+ export function activateToktgovProfileV2(id) {
685
+ const p = _toktgovPsV2.get(id);
686
+ if (!p) throw new Error(`toktgov profile ${id} not found`);
687
+ const isInitial = p.status === TOKTGOV_PROFILE_MATURITY_V2.PENDING;
688
+ _toktgovCheckP(p.status, TOKTGOV_PROFILE_MATURITY_V2.ACTIVE);
689
+ if (isInitial && _toktgovCountActive(p.owner) >= _toktgovMaxActive)
690
+ throw new Error(`max active toktgov profiles for owner ${p.owner} reached`);
691
+ const now = Date.now();
692
+ p.status = TOKTGOV_PROFILE_MATURITY_V2.ACTIVE;
693
+ p.updatedAt = now;
694
+ p.lastTouchedAt = now;
695
+ if (!p.activatedAt) p.activatedAt = now;
696
+ return { ...p, metadata: { ...p.metadata } };
697
+ }
698
+ export function staleToktgovProfileV2(id) {
699
+ const p = _toktgovPsV2.get(id);
700
+ if (!p) throw new Error(`toktgov profile ${id} not found`);
701
+ _toktgovCheckP(p.status, TOKTGOV_PROFILE_MATURITY_V2.STALE);
702
+ p.status = TOKTGOV_PROFILE_MATURITY_V2.STALE;
703
+ p.updatedAt = Date.now();
704
+ return { ...p, metadata: { ...p.metadata } };
705
+ }
706
+ export function archiveToktgovProfileV2(id) {
707
+ const p = _toktgovPsV2.get(id);
708
+ if (!p) throw new Error(`toktgov profile ${id} not found`);
709
+ _toktgovCheckP(p.status, TOKTGOV_PROFILE_MATURITY_V2.ARCHIVED);
710
+ const now = Date.now();
711
+ p.status = TOKTGOV_PROFILE_MATURITY_V2.ARCHIVED;
712
+ p.updatedAt = now;
713
+ if (!p.archivedAt) p.archivedAt = now;
714
+ return { ...p, metadata: { ...p.metadata } };
715
+ }
716
+ export function touchToktgovProfileV2(id) {
717
+ const p = _toktgovPsV2.get(id);
718
+ if (!p) throw new Error(`toktgov profile ${id} not found`);
719
+ if (_toktgovPTerminal.has(p.status))
720
+ throw new Error(`cannot touch terminal toktgov profile ${id}`);
721
+ const now = Date.now();
722
+ p.lastTouchedAt = now;
723
+ p.updatedAt = now;
724
+ return { ...p, metadata: { ...p.metadata } };
725
+ }
726
+ export function getToktgovProfileV2(id) {
727
+ const p = _toktgovPsV2.get(id);
728
+ if (!p) return null;
729
+ return { ...p, metadata: { ...p.metadata } };
730
+ }
731
+ export function listToktgovProfilesV2() {
732
+ return [..._toktgovPsV2.values()].map((p) => ({
733
+ ...p,
734
+ metadata: { ...p.metadata },
735
+ }));
736
+ }
737
+ export function createToktgovUsageV2({ id, profileId, model, metadata } = {}) {
738
+ if (!id || !profileId) throw new Error("id and profileId required");
739
+ if (_toktgovJsV2.has(id))
740
+ throw new Error(`toktgov usage ${id} already exists`);
741
+ if (!_toktgovPsV2.has(profileId))
742
+ throw new Error(`toktgov profile ${profileId} not found`);
743
+ if (_toktgovCountPending(profileId) >= _toktgovMaxPending)
744
+ throw new Error(
745
+ `max pending toktgov usages for profile ${profileId} reached`,
746
+ );
747
+ const now = Date.now();
748
+ const j = {
749
+ id,
750
+ profileId,
751
+ model: model || "",
752
+ status: TOKTGOV_USAGE_LIFECYCLE_V2.QUEUED,
753
+ createdAt: now,
754
+ updatedAt: now,
755
+ startedAt: null,
756
+ settledAt: null,
757
+ metadata: { ...(metadata || {}) },
758
+ };
759
+ _toktgovJsV2.set(id, j);
760
+ return { ...j, metadata: { ...j.metadata } };
761
+ }
762
+ export function recordingToktgovUsageV2(id) {
763
+ const j = _toktgovJsV2.get(id);
764
+ if (!j) throw new Error(`toktgov usage ${id} not found`);
765
+ _toktgovCheckJ(j.status, TOKTGOV_USAGE_LIFECYCLE_V2.RECORDING);
766
+ const now = Date.now();
767
+ j.status = TOKTGOV_USAGE_LIFECYCLE_V2.RECORDING;
768
+ j.updatedAt = now;
769
+ if (!j.startedAt) j.startedAt = now;
770
+ return { ...j, metadata: { ...j.metadata } };
771
+ }
772
+ export function completeUsageToktgovV2(id) {
773
+ const j = _toktgovJsV2.get(id);
774
+ if (!j) throw new Error(`toktgov usage ${id} not found`);
775
+ _toktgovCheckJ(j.status, TOKTGOV_USAGE_LIFECYCLE_V2.RECORDED);
776
+ const now = Date.now();
777
+ j.status = TOKTGOV_USAGE_LIFECYCLE_V2.RECORDED;
778
+ j.updatedAt = now;
779
+ if (!j.settledAt) j.settledAt = now;
780
+ return { ...j, metadata: { ...j.metadata } };
781
+ }
782
+ export function failToktgovUsageV2(id, reason) {
783
+ const j = _toktgovJsV2.get(id);
784
+ if (!j) throw new Error(`toktgov usage ${id} not found`);
785
+ _toktgovCheckJ(j.status, TOKTGOV_USAGE_LIFECYCLE_V2.FAILED);
786
+ const now = Date.now();
787
+ j.status = TOKTGOV_USAGE_LIFECYCLE_V2.FAILED;
788
+ j.updatedAt = now;
789
+ if (!j.settledAt) j.settledAt = now;
790
+ if (reason) j.metadata.failReason = String(reason);
791
+ return { ...j, metadata: { ...j.metadata } };
792
+ }
793
+ export function cancelToktgovUsageV2(id, reason) {
794
+ const j = _toktgovJsV2.get(id);
795
+ if (!j) throw new Error(`toktgov usage ${id} not found`);
796
+ _toktgovCheckJ(j.status, TOKTGOV_USAGE_LIFECYCLE_V2.CANCELLED);
797
+ const now = Date.now();
798
+ j.status = TOKTGOV_USAGE_LIFECYCLE_V2.CANCELLED;
799
+ j.updatedAt = now;
800
+ if (!j.settledAt) j.settledAt = now;
801
+ if (reason) j.metadata.cancelReason = String(reason);
802
+ return { ...j, metadata: { ...j.metadata } };
803
+ }
804
+ export function getToktgovUsageV2(id) {
805
+ const j = _toktgovJsV2.get(id);
806
+ if (!j) return null;
807
+ return { ...j, metadata: { ...j.metadata } };
808
+ }
809
+ export function listToktgovUsagesV2() {
810
+ return [..._toktgovJsV2.values()].map((j) => ({
811
+ ...j,
812
+ metadata: { ...j.metadata },
813
+ }));
814
+ }
815
+ export function autoStaleIdleToktgovProfilesV2({ now } = {}) {
816
+ const t = now ?? Date.now();
817
+ const flipped = [];
818
+ for (const p of _toktgovPsV2.values())
819
+ if (
820
+ p.status === TOKTGOV_PROFILE_MATURITY_V2.ACTIVE &&
821
+ t - p.lastTouchedAt >= _toktgovIdleMs
822
+ ) {
823
+ p.status = TOKTGOV_PROFILE_MATURITY_V2.STALE;
824
+ p.updatedAt = t;
825
+ flipped.push(p.id);
826
+ }
827
+ return { flipped, count: flipped.length };
828
+ }
829
+ export function autoFailStuckToktgovUsagesV2({ now } = {}) {
830
+ const t = now ?? Date.now();
831
+ const flipped = [];
832
+ for (const j of _toktgovJsV2.values())
833
+ if (
834
+ j.status === TOKTGOV_USAGE_LIFECYCLE_V2.RECORDING &&
835
+ j.startedAt != null &&
836
+ t - j.startedAt >= _toktgovStuckMs
837
+ ) {
838
+ j.status = TOKTGOV_USAGE_LIFECYCLE_V2.FAILED;
839
+ j.updatedAt = t;
840
+ if (!j.settledAt) j.settledAt = t;
841
+ j.metadata.failReason = "auto-fail-stuck";
842
+ flipped.push(j.id);
843
+ }
844
+ return { flipped, count: flipped.length };
845
+ }
846
+ export function getTokenTrackerGovStatsV2() {
847
+ const profilesByStatus = {};
848
+ for (const v of Object.values(TOKTGOV_PROFILE_MATURITY_V2))
849
+ profilesByStatus[v] = 0;
850
+ for (const p of _toktgovPsV2.values()) profilesByStatus[p.status]++;
851
+ const usagesByStatus = {};
852
+ for (const v of Object.values(TOKTGOV_USAGE_LIFECYCLE_V2))
853
+ usagesByStatus[v] = 0;
854
+ for (const j of _toktgovJsV2.values()) usagesByStatus[j.status]++;
855
+ return {
856
+ totalToktgovProfilesV2: _toktgovPsV2.size,
857
+ totalToktgovUsagesV2: _toktgovJsV2.size,
858
+ maxActiveToktgovProfilesPerOwner: _toktgovMaxActive,
859
+ maxPendingToktgovUsagesPerProfile: _toktgovMaxPending,
860
+ toktgovProfileIdleMs: _toktgovIdleMs,
861
+ toktgovUsageStuckMs: _toktgovStuckMs,
862
+ profilesByStatus,
863
+ usagesByStatus,
864
+ };
865
+ }