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
@@ -685,3 +685,340 @@ export function _resetStateV2() {
685
685
  _modelsV2.clear();
686
686
  _jobTicketsV2.clear();
687
687
  }
688
+
689
+ // =====================================================================
690
+ // quantization V2 governance overlay (iter18)
691
+ // =====================================================================
692
+ export const QNTGOV_PROFILE_MATURITY_V2 = Object.freeze({
693
+ PENDING: "pending",
694
+ ACTIVE: "active",
695
+ STALE: "stale",
696
+ ARCHIVED: "archived",
697
+ });
698
+ export const QNTGOV_JOB_LIFECYCLE_V2 = Object.freeze({
699
+ QUEUED: "queued",
700
+ QUANTIZING: "quantizing",
701
+ QUANTIZED: "quantized",
702
+ FAILED: "failed",
703
+ CANCELLED: "cancelled",
704
+ });
705
+ const _qntgovPTrans = new Map([
706
+ [
707
+ QNTGOV_PROFILE_MATURITY_V2.PENDING,
708
+ new Set([
709
+ QNTGOV_PROFILE_MATURITY_V2.ACTIVE,
710
+ QNTGOV_PROFILE_MATURITY_V2.ARCHIVED,
711
+ ]),
712
+ ],
713
+ [
714
+ QNTGOV_PROFILE_MATURITY_V2.ACTIVE,
715
+ new Set([
716
+ QNTGOV_PROFILE_MATURITY_V2.STALE,
717
+ QNTGOV_PROFILE_MATURITY_V2.ARCHIVED,
718
+ ]),
719
+ ],
720
+ [
721
+ QNTGOV_PROFILE_MATURITY_V2.STALE,
722
+ new Set([
723
+ QNTGOV_PROFILE_MATURITY_V2.ACTIVE,
724
+ QNTGOV_PROFILE_MATURITY_V2.ARCHIVED,
725
+ ]),
726
+ ],
727
+ [QNTGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
728
+ ]);
729
+ const _qntgovPTerminal = new Set([QNTGOV_PROFILE_MATURITY_V2.ARCHIVED]);
730
+ const _qntgovJTrans = new Map([
731
+ [
732
+ QNTGOV_JOB_LIFECYCLE_V2.QUEUED,
733
+ new Set([
734
+ QNTGOV_JOB_LIFECYCLE_V2.QUANTIZING,
735
+ QNTGOV_JOB_LIFECYCLE_V2.CANCELLED,
736
+ ]),
737
+ ],
738
+ [
739
+ QNTGOV_JOB_LIFECYCLE_V2.QUANTIZING,
740
+ new Set([
741
+ QNTGOV_JOB_LIFECYCLE_V2.QUANTIZED,
742
+ QNTGOV_JOB_LIFECYCLE_V2.FAILED,
743
+ QNTGOV_JOB_LIFECYCLE_V2.CANCELLED,
744
+ ]),
745
+ ],
746
+ [QNTGOV_JOB_LIFECYCLE_V2.QUANTIZED, new Set()],
747
+ [QNTGOV_JOB_LIFECYCLE_V2.FAILED, new Set()],
748
+ [QNTGOV_JOB_LIFECYCLE_V2.CANCELLED, new Set()],
749
+ ]);
750
+ const _qntgovPsV2 = new Map();
751
+ const _qntgovJsV2 = new Map();
752
+ let _qntgovMaxActive = 6,
753
+ _qntgovMaxPending = 12,
754
+ _qntgovIdleMs = 30 * 24 * 60 * 60 * 1000,
755
+ _qntgovStuckMs = 60 * 1000;
756
+ function _qntgovPos(n, label) {
757
+ const v = Math.floor(Number(n));
758
+ if (!Number.isFinite(v) || v <= 0)
759
+ throw new Error(`${label} must be positive integer`);
760
+ return v;
761
+ }
762
+ function _qntgovCheckP(from, to) {
763
+ const a = _qntgovPTrans.get(from);
764
+ if (!a || !a.has(to))
765
+ throw new Error(`invalid qntgov profile transition ${from} → ${to}`);
766
+ }
767
+ function _qntgovCheckJ(from, to) {
768
+ const a = _qntgovJTrans.get(from);
769
+ if (!a || !a.has(to))
770
+ throw new Error(`invalid qntgov job transition ${from} → ${to}`);
771
+ }
772
+ function _qntgovCountActive(owner) {
773
+ let c = 0;
774
+ for (const p of _qntgovPsV2.values())
775
+ if (p.owner === owner && p.status === QNTGOV_PROFILE_MATURITY_V2.ACTIVE)
776
+ c++;
777
+ return c;
778
+ }
779
+ function _qntgovCountPending(profileId) {
780
+ let c = 0;
781
+ for (const j of _qntgovJsV2.values())
782
+ if (
783
+ j.profileId === profileId &&
784
+ (j.status === QNTGOV_JOB_LIFECYCLE_V2.QUEUED ||
785
+ j.status === QNTGOV_JOB_LIFECYCLE_V2.QUANTIZING)
786
+ )
787
+ c++;
788
+ return c;
789
+ }
790
+ export function setMaxActiveQntgovProfilesPerOwnerV2(n) {
791
+ _qntgovMaxActive = _qntgovPos(n, "maxActiveQntgovProfilesPerOwner");
792
+ }
793
+ export function getMaxActiveQntgovProfilesPerOwnerV2() {
794
+ return _qntgovMaxActive;
795
+ }
796
+ export function setMaxPendingQntgovJobsPerProfileV2(n) {
797
+ _qntgovMaxPending = _qntgovPos(n, "maxPendingQntgovJobsPerProfile");
798
+ }
799
+ export function getMaxPendingQntgovJobsPerProfileV2() {
800
+ return _qntgovMaxPending;
801
+ }
802
+ export function setQntgovProfileIdleMsV2(n) {
803
+ _qntgovIdleMs = _qntgovPos(n, "qntgovProfileIdleMs");
804
+ }
805
+ export function getQntgovProfileIdleMsV2() {
806
+ return _qntgovIdleMs;
807
+ }
808
+ export function setQntgovJobStuckMsV2(n) {
809
+ _qntgovStuckMs = _qntgovPos(n, "qntgovJobStuckMs");
810
+ }
811
+ export function getQntgovJobStuckMsV2() {
812
+ return _qntgovStuckMs;
813
+ }
814
+ export function _resetStateQuantizationGovV2() {
815
+ _qntgovPsV2.clear();
816
+ _qntgovJsV2.clear();
817
+ _qntgovMaxActive = 6;
818
+ _qntgovMaxPending = 12;
819
+ _qntgovIdleMs = 30 * 24 * 60 * 60 * 1000;
820
+ _qntgovStuckMs = 60 * 1000;
821
+ }
822
+ export function registerQntgovProfileV2({
823
+ id,
824
+ owner,
825
+ precision,
826
+ metadata,
827
+ } = {}) {
828
+ if (!id || !owner) throw new Error("id and owner required");
829
+ if (_qntgovPsV2.has(id))
830
+ throw new Error(`qntgov profile ${id} already exists`);
831
+ const now = Date.now();
832
+ const p = {
833
+ id,
834
+ owner,
835
+ precision: precision || "int8",
836
+ status: QNTGOV_PROFILE_MATURITY_V2.PENDING,
837
+ createdAt: now,
838
+ updatedAt: now,
839
+ lastTouchedAt: now,
840
+ activatedAt: null,
841
+ archivedAt: null,
842
+ metadata: { ...(metadata || {}) },
843
+ };
844
+ _qntgovPsV2.set(id, p);
845
+ return { ...p, metadata: { ...p.metadata } };
846
+ }
847
+ export function activateQntgovProfileV2(id) {
848
+ const p = _qntgovPsV2.get(id);
849
+ if (!p) throw new Error(`qntgov profile ${id} not found`);
850
+ const isInitial = p.status === QNTGOV_PROFILE_MATURITY_V2.PENDING;
851
+ _qntgovCheckP(p.status, QNTGOV_PROFILE_MATURITY_V2.ACTIVE);
852
+ if (isInitial && _qntgovCountActive(p.owner) >= _qntgovMaxActive)
853
+ throw new Error(`max active qntgov profiles for owner ${p.owner} reached`);
854
+ const now = Date.now();
855
+ p.status = QNTGOV_PROFILE_MATURITY_V2.ACTIVE;
856
+ p.updatedAt = now;
857
+ p.lastTouchedAt = now;
858
+ if (!p.activatedAt) p.activatedAt = now;
859
+ return { ...p, metadata: { ...p.metadata } };
860
+ }
861
+ export function staleQntgovProfileV2(id) {
862
+ const p = _qntgovPsV2.get(id);
863
+ if (!p) throw new Error(`qntgov profile ${id} not found`);
864
+ _qntgovCheckP(p.status, QNTGOV_PROFILE_MATURITY_V2.STALE);
865
+ p.status = QNTGOV_PROFILE_MATURITY_V2.STALE;
866
+ p.updatedAt = Date.now();
867
+ return { ...p, metadata: { ...p.metadata } };
868
+ }
869
+ export function archiveQntgovProfileV2(id) {
870
+ const p = _qntgovPsV2.get(id);
871
+ if (!p) throw new Error(`qntgov profile ${id} not found`);
872
+ _qntgovCheckP(p.status, QNTGOV_PROFILE_MATURITY_V2.ARCHIVED);
873
+ const now = Date.now();
874
+ p.status = QNTGOV_PROFILE_MATURITY_V2.ARCHIVED;
875
+ p.updatedAt = now;
876
+ if (!p.archivedAt) p.archivedAt = now;
877
+ return { ...p, metadata: { ...p.metadata } };
878
+ }
879
+ export function touchQntgovProfileV2(id) {
880
+ const p = _qntgovPsV2.get(id);
881
+ if (!p) throw new Error(`qntgov profile ${id} not found`);
882
+ if (_qntgovPTerminal.has(p.status))
883
+ throw new Error(`cannot touch terminal qntgov profile ${id}`);
884
+ const now = Date.now();
885
+ p.lastTouchedAt = now;
886
+ p.updatedAt = now;
887
+ return { ...p, metadata: { ...p.metadata } };
888
+ }
889
+ export function getQntgovProfileV2(id) {
890
+ const p = _qntgovPsV2.get(id);
891
+ if (!p) return null;
892
+ return { ...p, metadata: { ...p.metadata } };
893
+ }
894
+ export function listQntgovProfilesV2() {
895
+ return [..._qntgovPsV2.values()].map((p) => ({
896
+ ...p,
897
+ metadata: { ...p.metadata },
898
+ }));
899
+ }
900
+ export function createQntgovJobV2({ id, profileId, model, metadata } = {}) {
901
+ if (!id || !profileId) throw new Error("id and profileId required");
902
+ if (_qntgovJsV2.has(id)) throw new Error(`qntgov job ${id} already exists`);
903
+ if (!_qntgovPsV2.has(profileId))
904
+ throw new Error(`qntgov profile ${profileId} not found`);
905
+ if (_qntgovCountPending(profileId) >= _qntgovMaxPending)
906
+ throw new Error(`max pending qntgov jobs for profile ${profileId} reached`);
907
+ const now = Date.now();
908
+ const j = {
909
+ id,
910
+ profileId,
911
+ model: model || "",
912
+ status: QNTGOV_JOB_LIFECYCLE_V2.QUEUED,
913
+ createdAt: now,
914
+ updatedAt: now,
915
+ startedAt: null,
916
+ settledAt: null,
917
+ metadata: { ...(metadata || {}) },
918
+ };
919
+ _qntgovJsV2.set(id, j);
920
+ return { ...j, metadata: { ...j.metadata } };
921
+ }
922
+ export function quantizingQntgovJobV2(id) {
923
+ const j = _qntgovJsV2.get(id);
924
+ if (!j) throw new Error(`qntgov job ${id} not found`);
925
+ _qntgovCheckJ(j.status, QNTGOV_JOB_LIFECYCLE_V2.QUANTIZING);
926
+ const now = Date.now();
927
+ j.status = QNTGOV_JOB_LIFECYCLE_V2.QUANTIZING;
928
+ j.updatedAt = now;
929
+ if (!j.startedAt) j.startedAt = now;
930
+ return { ...j, metadata: { ...j.metadata } };
931
+ }
932
+ export function completeJobQntgovV2(id) {
933
+ const j = _qntgovJsV2.get(id);
934
+ if (!j) throw new Error(`qntgov job ${id} not found`);
935
+ _qntgovCheckJ(j.status, QNTGOV_JOB_LIFECYCLE_V2.QUANTIZED);
936
+ const now = Date.now();
937
+ j.status = QNTGOV_JOB_LIFECYCLE_V2.QUANTIZED;
938
+ j.updatedAt = now;
939
+ if (!j.settledAt) j.settledAt = now;
940
+ return { ...j, metadata: { ...j.metadata } };
941
+ }
942
+ export function failQntgovJobV2(id, reason) {
943
+ const j = _qntgovJsV2.get(id);
944
+ if (!j) throw new Error(`qntgov job ${id} not found`);
945
+ _qntgovCheckJ(j.status, QNTGOV_JOB_LIFECYCLE_V2.FAILED);
946
+ const now = Date.now();
947
+ j.status = QNTGOV_JOB_LIFECYCLE_V2.FAILED;
948
+ j.updatedAt = now;
949
+ if (!j.settledAt) j.settledAt = now;
950
+ if (reason) j.metadata.failReason = String(reason);
951
+ return { ...j, metadata: { ...j.metadata } };
952
+ }
953
+ export function cancelQntgovJobV2(id, reason) {
954
+ const j = _qntgovJsV2.get(id);
955
+ if (!j) throw new Error(`qntgov job ${id} not found`);
956
+ _qntgovCheckJ(j.status, QNTGOV_JOB_LIFECYCLE_V2.CANCELLED);
957
+ const now = Date.now();
958
+ j.status = QNTGOV_JOB_LIFECYCLE_V2.CANCELLED;
959
+ j.updatedAt = now;
960
+ if (!j.settledAt) j.settledAt = now;
961
+ if (reason) j.metadata.cancelReason = String(reason);
962
+ return { ...j, metadata: { ...j.metadata } };
963
+ }
964
+ export function getQntgovJobV2(id) {
965
+ const j = _qntgovJsV2.get(id);
966
+ if (!j) return null;
967
+ return { ...j, metadata: { ...j.metadata } };
968
+ }
969
+ export function listQntgovJobsV2() {
970
+ return [..._qntgovJsV2.values()].map((j) => ({
971
+ ...j,
972
+ metadata: { ...j.metadata },
973
+ }));
974
+ }
975
+ export function autoStaleIdleQntgovProfilesV2({ now } = {}) {
976
+ const t = now ?? Date.now();
977
+ const flipped = [];
978
+ for (const p of _qntgovPsV2.values())
979
+ if (
980
+ p.status === QNTGOV_PROFILE_MATURITY_V2.ACTIVE &&
981
+ t - p.lastTouchedAt >= _qntgovIdleMs
982
+ ) {
983
+ p.status = QNTGOV_PROFILE_MATURITY_V2.STALE;
984
+ p.updatedAt = t;
985
+ flipped.push(p.id);
986
+ }
987
+ return { flipped, count: flipped.length };
988
+ }
989
+ export function autoFailStuckQntgovJobsV2({ now } = {}) {
990
+ const t = now ?? Date.now();
991
+ const flipped = [];
992
+ for (const j of _qntgovJsV2.values())
993
+ if (
994
+ j.status === QNTGOV_JOB_LIFECYCLE_V2.QUANTIZING &&
995
+ j.startedAt != null &&
996
+ t - j.startedAt >= _qntgovStuckMs
997
+ ) {
998
+ j.status = QNTGOV_JOB_LIFECYCLE_V2.FAILED;
999
+ j.updatedAt = t;
1000
+ if (!j.settledAt) j.settledAt = t;
1001
+ j.metadata.failReason = "auto-fail-stuck";
1002
+ flipped.push(j.id);
1003
+ }
1004
+ return { flipped, count: flipped.length };
1005
+ }
1006
+ export function getQuantizationGovStatsV2() {
1007
+ const profilesByStatus = {};
1008
+ for (const v of Object.values(QNTGOV_PROFILE_MATURITY_V2))
1009
+ profilesByStatus[v] = 0;
1010
+ for (const p of _qntgovPsV2.values()) profilesByStatus[p.status]++;
1011
+ const jobsByStatus = {};
1012
+ for (const v of Object.values(QNTGOV_JOB_LIFECYCLE_V2)) jobsByStatus[v] = 0;
1013
+ for (const j of _qntgovJsV2.values()) jobsByStatus[j.status]++;
1014
+ return {
1015
+ totalQntgovProfilesV2: _qntgovPsV2.size,
1016
+ totalQntgovJobsV2: _qntgovJsV2.size,
1017
+ maxActiveQntgovProfilesPerOwner: _qntgovMaxActive,
1018
+ maxPendingQntgovJobsPerProfile: _qntgovMaxPending,
1019
+ qntgovProfileIdleMs: _qntgovIdleMs,
1020
+ qntgovJobStuckMs: _qntgovStuckMs,
1021
+ profilesByStatus,
1022
+ jobsByStatus,
1023
+ };
1024
+ }
@@ -806,3 +806,343 @@ export function getReputationStatsV2() {
806
806
  bestScoreEver: bestScore,
807
807
  };
808
808
  }
809
+
810
+ // =====================================================================
811
+ // reputation-optimizer V2 governance overlay (iter16)
812
+ // =====================================================================
813
+ export const REPGOV_PROFILE_MATURITY_V2 = Object.freeze({
814
+ PENDING: "pending",
815
+ ACTIVE: "active",
816
+ STALE: "stale",
817
+ ARCHIVED: "archived",
818
+ });
819
+ export const REPGOV_CYCLE_LIFECYCLE_V2 = Object.freeze({
820
+ QUEUED: "queued",
821
+ RUNNING: "running",
822
+ COMPLETED: "completed",
823
+ FAILED: "failed",
824
+ CANCELLED: "cancelled",
825
+ });
826
+ const _repgovPTrans = new Map([
827
+ [
828
+ REPGOV_PROFILE_MATURITY_V2.PENDING,
829
+ new Set([
830
+ REPGOV_PROFILE_MATURITY_V2.ACTIVE,
831
+ REPGOV_PROFILE_MATURITY_V2.ARCHIVED,
832
+ ]),
833
+ ],
834
+ [
835
+ REPGOV_PROFILE_MATURITY_V2.ACTIVE,
836
+ new Set([
837
+ REPGOV_PROFILE_MATURITY_V2.STALE,
838
+ REPGOV_PROFILE_MATURITY_V2.ARCHIVED,
839
+ ]),
840
+ ],
841
+ [
842
+ REPGOV_PROFILE_MATURITY_V2.STALE,
843
+ new Set([
844
+ REPGOV_PROFILE_MATURITY_V2.ACTIVE,
845
+ REPGOV_PROFILE_MATURITY_V2.ARCHIVED,
846
+ ]),
847
+ ],
848
+ [REPGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
849
+ ]);
850
+ const _repgovPTerminal = new Set([REPGOV_PROFILE_MATURITY_V2.ARCHIVED]);
851
+ const _repgovJTrans = new Map([
852
+ [
853
+ REPGOV_CYCLE_LIFECYCLE_V2.QUEUED,
854
+ new Set([
855
+ REPGOV_CYCLE_LIFECYCLE_V2.RUNNING,
856
+ REPGOV_CYCLE_LIFECYCLE_V2.CANCELLED,
857
+ ]),
858
+ ],
859
+ [
860
+ REPGOV_CYCLE_LIFECYCLE_V2.RUNNING,
861
+ new Set([
862
+ REPGOV_CYCLE_LIFECYCLE_V2.COMPLETED,
863
+ REPGOV_CYCLE_LIFECYCLE_V2.FAILED,
864
+ REPGOV_CYCLE_LIFECYCLE_V2.CANCELLED,
865
+ ]),
866
+ ],
867
+ [REPGOV_CYCLE_LIFECYCLE_V2.COMPLETED, new Set()],
868
+ [REPGOV_CYCLE_LIFECYCLE_V2.FAILED, new Set()],
869
+ [REPGOV_CYCLE_LIFECYCLE_V2.CANCELLED, new Set()],
870
+ ]);
871
+ const _repgovPsV2 = new Map();
872
+ const _repgovJsV2 = new Map();
873
+ let _repgovMaxActive = 8,
874
+ _repgovMaxPending = 20,
875
+ _repgovIdleMs = 30 * 24 * 60 * 60 * 1000,
876
+ _repgovStuckMs = 60 * 1000;
877
+ function _repgovPos(n, label) {
878
+ const v = Math.floor(Number(n));
879
+ if (!Number.isFinite(v) || v <= 0)
880
+ throw new Error(`${label} must be positive integer`);
881
+ return v;
882
+ }
883
+ function _repgovCheckP(from, to) {
884
+ const a = _repgovPTrans.get(from);
885
+ if (!a || !a.has(to))
886
+ throw new Error(`invalid repgov profile transition ${from} → ${to}`);
887
+ }
888
+ function _repgovCheckJ(from, to) {
889
+ const a = _repgovJTrans.get(from);
890
+ if (!a || !a.has(to))
891
+ throw new Error(`invalid repgov cycle transition ${from} → ${to}`);
892
+ }
893
+ function _repgovCountActive(owner) {
894
+ let c = 0;
895
+ for (const p of _repgovPsV2.values())
896
+ if (p.owner === owner && p.status === REPGOV_PROFILE_MATURITY_V2.ACTIVE)
897
+ c++;
898
+ return c;
899
+ }
900
+ function _repgovCountPending(profileId) {
901
+ let c = 0;
902
+ for (const j of _repgovJsV2.values())
903
+ if (
904
+ j.profileId === profileId &&
905
+ (j.status === REPGOV_CYCLE_LIFECYCLE_V2.QUEUED ||
906
+ j.status === REPGOV_CYCLE_LIFECYCLE_V2.RUNNING)
907
+ )
908
+ c++;
909
+ return c;
910
+ }
911
+ export function setMaxActiveRepgovProfilesPerOwnerV2(n) {
912
+ _repgovMaxActive = _repgovPos(n, "maxActiveRepgovProfilesPerOwner");
913
+ }
914
+ export function getMaxActiveRepgovProfilesPerOwnerV2() {
915
+ return _repgovMaxActive;
916
+ }
917
+ export function setMaxPendingRepgovCyclesPerProfileV2(n) {
918
+ _repgovMaxPending = _repgovPos(n, "maxPendingRepgovCyclesPerProfile");
919
+ }
920
+ export function getMaxPendingRepgovCyclesPerProfileV2() {
921
+ return _repgovMaxPending;
922
+ }
923
+ export function setRepgovProfileIdleMsV2(n) {
924
+ _repgovIdleMs = _repgovPos(n, "repgovProfileIdleMs");
925
+ }
926
+ export function getRepgovProfileIdleMsV2() {
927
+ return _repgovIdleMs;
928
+ }
929
+ export function setRepgovCycleStuckMsV2(n) {
930
+ _repgovStuckMs = _repgovPos(n, "repgovCycleStuckMs");
931
+ }
932
+ export function getRepgovCycleStuckMsV2() {
933
+ return _repgovStuckMs;
934
+ }
935
+ export function _resetStateReputationOptimizerV2() {
936
+ _repgovPsV2.clear();
937
+ _repgovJsV2.clear();
938
+ _repgovMaxActive = 8;
939
+ _repgovMaxPending = 20;
940
+ _repgovIdleMs = 30 * 24 * 60 * 60 * 1000;
941
+ _repgovStuckMs = 60 * 1000;
942
+ }
943
+ export function registerRepgovProfileV2({
944
+ id,
945
+ owner,
946
+ objective,
947
+ metadata,
948
+ } = {}) {
949
+ if (!id || !owner) throw new Error("id and owner required");
950
+ if (_repgovPsV2.has(id))
951
+ throw new Error(`repgov profile ${id} already exists`);
952
+ const now = Date.now();
953
+ const p = {
954
+ id,
955
+ owner,
956
+ objective: objective || "quality",
957
+ status: REPGOV_PROFILE_MATURITY_V2.PENDING,
958
+ createdAt: now,
959
+ updatedAt: now,
960
+ lastTouchedAt: now,
961
+ activatedAt: null,
962
+ archivedAt: null,
963
+ metadata: { ...(metadata || {}) },
964
+ };
965
+ _repgovPsV2.set(id, p);
966
+ return { ...p, metadata: { ...p.metadata } };
967
+ }
968
+ export function activateRepgovProfileV2(id) {
969
+ const p = _repgovPsV2.get(id);
970
+ if (!p) throw new Error(`repgov profile ${id} not found`);
971
+ const isInitial = p.status === REPGOV_PROFILE_MATURITY_V2.PENDING;
972
+ _repgovCheckP(p.status, REPGOV_PROFILE_MATURITY_V2.ACTIVE);
973
+ if (isInitial && _repgovCountActive(p.owner) >= _repgovMaxActive)
974
+ throw new Error(`max active repgov profiles for owner ${p.owner} reached`);
975
+ const now = Date.now();
976
+ p.status = REPGOV_PROFILE_MATURITY_V2.ACTIVE;
977
+ p.updatedAt = now;
978
+ p.lastTouchedAt = now;
979
+ if (!p.activatedAt) p.activatedAt = now;
980
+ return { ...p, metadata: { ...p.metadata } };
981
+ }
982
+ export function staleRepgovProfileV2(id) {
983
+ const p = _repgovPsV2.get(id);
984
+ if (!p) throw new Error(`repgov profile ${id} not found`);
985
+ _repgovCheckP(p.status, REPGOV_PROFILE_MATURITY_V2.STALE);
986
+ p.status = REPGOV_PROFILE_MATURITY_V2.STALE;
987
+ p.updatedAt = Date.now();
988
+ return { ...p, metadata: { ...p.metadata } };
989
+ }
990
+ export function archiveRepgovProfileV2(id) {
991
+ const p = _repgovPsV2.get(id);
992
+ if (!p) throw new Error(`repgov profile ${id} not found`);
993
+ _repgovCheckP(p.status, REPGOV_PROFILE_MATURITY_V2.ARCHIVED);
994
+ const now = Date.now();
995
+ p.status = REPGOV_PROFILE_MATURITY_V2.ARCHIVED;
996
+ p.updatedAt = now;
997
+ if (!p.archivedAt) p.archivedAt = now;
998
+ return { ...p, metadata: { ...p.metadata } };
999
+ }
1000
+ export function touchRepgovProfileV2(id) {
1001
+ const p = _repgovPsV2.get(id);
1002
+ if (!p) throw new Error(`repgov profile ${id} not found`);
1003
+ if (_repgovPTerminal.has(p.status))
1004
+ throw new Error(`cannot touch terminal repgov profile ${id}`);
1005
+ const now = Date.now();
1006
+ p.lastTouchedAt = now;
1007
+ p.updatedAt = now;
1008
+ return { ...p, metadata: { ...p.metadata } };
1009
+ }
1010
+ export function getRepgovProfileV2(id) {
1011
+ const p = _repgovPsV2.get(id);
1012
+ if (!p) return null;
1013
+ return { ...p, metadata: { ...p.metadata } };
1014
+ }
1015
+ export function listRepgovProfilesV2() {
1016
+ return [..._repgovPsV2.values()].map((p) => ({
1017
+ ...p,
1018
+ metadata: { ...p.metadata },
1019
+ }));
1020
+ }
1021
+ export function createRepgovCycleV2({ id, profileId, subject, metadata } = {}) {
1022
+ if (!id || !profileId) throw new Error("id and profileId required");
1023
+ if (_repgovJsV2.has(id)) throw new Error(`repgov cycle ${id} already exists`);
1024
+ if (!_repgovPsV2.has(profileId))
1025
+ throw new Error(`repgov profile ${profileId} not found`);
1026
+ if (_repgovCountPending(profileId) >= _repgovMaxPending)
1027
+ throw new Error(
1028
+ `max pending repgov cycles for profile ${profileId} reached`,
1029
+ );
1030
+ const now = Date.now();
1031
+ const j = {
1032
+ id,
1033
+ profileId,
1034
+ subject: subject || "",
1035
+ status: REPGOV_CYCLE_LIFECYCLE_V2.QUEUED,
1036
+ createdAt: now,
1037
+ updatedAt: now,
1038
+ startedAt: null,
1039
+ settledAt: null,
1040
+ metadata: { ...(metadata || {}) },
1041
+ };
1042
+ _repgovJsV2.set(id, j);
1043
+ return { ...j, metadata: { ...j.metadata } };
1044
+ }
1045
+ export function runningRepgovCycleV2(id) {
1046
+ const j = _repgovJsV2.get(id);
1047
+ if (!j) throw new Error(`repgov cycle ${id} not found`);
1048
+ _repgovCheckJ(j.status, REPGOV_CYCLE_LIFECYCLE_V2.RUNNING);
1049
+ const now = Date.now();
1050
+ j.status = REPGOV_CYCLE_LIFECYCLE_V2.RUNNING;
1051
+ j.updatedAt = now;
1052
+ if (!j.startedAt) j.startedAt = now;
1053
+ return { ...j, metadata: { ...j.metadata } };
1054
+ }
1055
+ export function completeCycleRepgovV2(id) {
1056
+ const j = _repgovJsV2.get(id);
1057
+ if (!j) throw new Error(`repgov cycle ${id} not found`);
1058
+ _repgovCheckJ(j.status, REPGOV_CYCLE_LIFECYCLE_V2.COMPLETED);
1059
+ const now = Date.now();
1060
+ j.status = REPGOV_CYCLE_LIFECYCLE_V2.COMPLETED;
1061
+ j.updatedAt = now;
1062
+ if (!j.settledAt) j.settledAt = now;
1063
+ return { ...j, metadata: { ...j.metadata } };
1064
+ }
1065
+ export function failRepgovCycleV2(id, reason) {
1066
+ const j = _repgovJsV2.get(id);
1067
+ if (!j) throw new Error(`repgov cycle ${id} not found`);
1068
+ _repgovCheckJ(j.status, REPGOV_CYCLE_LIFECYCLE_V2.FAILED);
1069
+ const now = Date.now();
1070
+ j.status = REPGOV_CYCLE_LIFECYCLE_V2.FAILED;
1071
+ j.updatedAt = now;
1072
+ if (!j.settledAt) j.settledAt = now;
1073
+ if (reason) j.metadata.failReason = String(reason);
1074
+ return { ...j, metadata: { ...j.metadata } };
1075
+ }
1076
+ export function cancelRepgovCycleV2(id, reason) {
1077
+ const j = _repgovJsV2.get(id);
1078
+ if (!j) throw new Error(`repgov cycle ${id} not found`);
1079
+ _repgovCheckJ(j.status, REPGOV_CYCLE_LIFECYCLE_V2.CANCELLED);
1080
+ const now = Date.now();
1081
+ j.status = REPGOV_CYCLE_LIFECYCLE_V2.CANCELLED;
1082
+ j.updatedAt = now;
1083
+ if (!j.settledAt) j.settledAt = now;
1084
+ if (reason) j.metadata.cancelReason = String(reason);
1085
+ return { ...j, metadata: { ...j.metadata } };
1086
+ }
1087
+ export function getRepgovCycleV2(id) {
1088
+ const j = _repgovJsV2.get(id);
1089
+ if (!j) return null;
1090
+ return { ...j, metadata: { ...j.metadata } };
1091
+ }
1092
+ export function listRepgovCyclesV2() {
1093
+ return [..._repgovJsV2.values()].map((j) => ({
1094
+ ...j,
1095
+ metadata: { ...j.metadata },
1096
+ }));
1097
+ }
1098
+ export function autoStaleIdleRepgovProfilesV2({ now } = {}) {
1099
+ const t = now ?? Date.now();
1100
+ const flipped = [];
1101
+ for (const p of _repgovPsV2.values())
1102
+ if (
1103
+ p.status === REPGOV_PROFILE_MATURITY_V2.ACTIVE &&
1104
+ t - p.lastTouchedAt >= _repgovIdleMs
1105
+ ) {
1106
+ p.status = REPGOV_PROFILE_MATURITY_V2.STALE;
1107
+ p.updatedAt = t;
1108
+ flipped.push(p.id);
1109
+ }
1110
+ return { flipped, count: flipped.length };
1111
+ }
1112
+ export function autoFailStuckRepgovCyclesV2({ now } = {}) {
1113
+ const t = now ?? Date.now();
1114
+ const flipped = [];
1115
+ for (const j of _repgovJsV2.values())
1116
+ if (
1117
+ j.status === REPGOV_CYCLE_LIFECYCLE_V2.RUNNING &&
1118
+ j.startedAt != null &&
1119
+ t - j.startedAt >= _repgovStuckMs
1120
+ ) {
1121
+ j.status = REPGOV_CYCLE_LIFECYCLE_V2.FAILED;
1122
+ j.updatedAt = t;
1123
+ if (!j.settledAt) j.settledAt = t;
1124
+ j.metadata.failReason = "auto-fail-stuck";
1125
+ flipped.push(j.id);
1126
+ }
1127
+ return { flipped, count: flipped.length };
1128
+ }
1129
+ export function getReputationOptimizerGovStatsV2() {
1130
+ const profilesByStatus = {};
1131
+ for (const v of Object.values(REPGOV_PROFILE_MATURITY_V2))
1132
+ profilesByStatus[v] = 0;
1133
+ for (const p of _repgovPsV2.values()) profilesByStatus[p.status]++;
1134
+ const cyclesByStatus = {};
1135
+ for (const v of Object.values(REPGOV_CYCLE_LIFECYCLE_V2))
1136
+ cyclesByStatus[v] = 0;
1137
+ for (const j of _repgovJsV2.values()) cyclesByStatus[j.status]++;
1138
+ return {
1139
+ totalRepgovProfilesV2: _repgovPsV2.size,
1140
+ totalRepgovCyclesV2: _repgovJsV2.size,
1141
+ maxActiveRepgovProfilesPerOwner: _repgovMaxActive,
1142
+ maxPendingRepgovCyclesPerProfile: _repgovMaxPending,
1143
+ repgovProfileIdleMs: _repgovIdleMs,
1144
+ repgovCycleStuckMs: _repgovStuckMs,
1145
+ profilesByStatus,
1146
+ cyclesByStatus,
1147
+ };
1148
+ }