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
@@ -636,25 +636,50 @@ export function _resetV2State() {
636
636
  _statusHistoryV2.clear();
637
637
  }
638
638
 
639
-
640
639
  // ===== V2 Surface: BI Engine governance overlay (CLI v0.135.0) =====
641
640
  export const BI_DATASET_MATURITY_V2 = Object.freeze({
642
- PENDING: "pending", ACTIVE: "active", STALE: "stale", ARCHIVED: "archived",
641
+ PENDING: "pending",
642
+ ACTIVE: "active",
643
+ STALE: "stale",
644
+ ARCHIVED: "archived",
643
645
  });
644
646
  export const BI_QUERY_LIFECYCLE_V2 = Object.freeze({
645
- QUEUED: "queued", RUNNING: "running", COMPLETED: "completed", FAILED: "failed", CANCELLED: "cancelled",
647
+ QUEUED: "queued",
648
+ RUNNING: "running",
649
+ COMPLETED: "completed",
650
+ FAILED: "failed",
651
+ CANCELLED: "cancelled",
646
652
  });
647
653
 
648
654
  const _biDsTrans = new Map([
649
- [BI_DATASET_MATURITY_V2.PENDING, new Set([BI_DATASET_MATURITY_V2.ACTIVE, BI_DATASET_MATURITY_V2.ARCHIVED])],
650
- [BI_DATASET_MATURITY_V2.ACTIVE, new Set([BI_DATASET_MATURITY_V2.STALE, BI_DATASET_MATURITY_V2.ARCHIVED])],
651
- [BI_DATASET_MATURITY_V2.STALE, new Set([BI_DATASET_MATURITY_V2.ACTIVE, BI_DATASET_MATURITY_V2.ARCHIVED])],
655
+ [
656
+ BI_DATASET_MATURITY_V2.PENDING,
657
+ new Set([BI_DATASET_MATURITY_V2.ACTIVE, BI_DATASET_MATURITY_V2.ARCHIVED]),
658
+ ],
659
+ [
660
+ BI_DATASET_MATURITY_V2.ACTIVE,
661
+ new Set([BI_DATASET_MATURITY_V2.STALE, BI_DATASET_MATURITY_V2.ARCHIVED]),
662
+ ],
663
+ [
664
+ BI_DATASET_MATURITY_V2.STALE,
665
+ new Set([BI_DATASET_MATURITY_V2.ACTIVE, BI_DATASET_MATURITY_V2.ARCHIVED]),
666
+ ],
652
667
  [BI_DATASET_MATURITY_V2.ARCHIVED, new Set()],
653
668
  ]);
654
669
  const _biDsTerminal = new Set([BI_DATASET_MATURITY_V2.ARCHIVED]);
655
670
  const _biQTrans = new Map([
656
- [BI_QUERY_LIFECYCLE_V2.QUEUED, new Set([BI_QUERY_LIFECYCLE_V2.RUNNING, BI_QUERY_LIFECYCLE_V2.CANCELLED])],
657
- [BI_QUERY_LIFECYCLE_V2.RUNNING, new Set([BI_QUERY_LIFECYCLE_V2.COMPLETED, BI_QUERY_LIFECYCLE_V2.FAILED, BI_QUERY_LIFECYCLE_V2.CANCELLED])],
671
+ [
672
+ BI_QUERY_LIFECYCLE_V2.QUEUED,
673
+ new Set([BI_QUERY_LIFECYCLE_V2.RUNNING, BI_QUERY_LIFECYCLE_V2.CANCELLED]),
674
+ ],
675
+ [
676
+ BI_QUERY_LIFECYCLE_V2.RUNNING,
677
+ new Set([
678
+ BI_QUERY_LIFECYCLE_V2.COMPLETED,
679
+ BI_QUERY_LIFECYCLE_V2.FAILED,
680
+ BI_QUERY_LIFECYCLE_V2.CANCELLED,
681
+ ]),
682
+ ],
658
683
  [BI_QUERY_LIFECYCLE_V2.COMPLETED, new Set()],
659
684
  [BI_QUERY_LIFECYCLE_V2.FAILED, new Set()],
660
685
  [BI_QUERY_LIFECYCLE_V2.CANCELLED, new Set()],
@@ -667,76 +692,618 @@ let _biMaxPendingPerDs = 10;
667
692
  let _biDsIdleMs = 7 * 24 * 60 * 60 * 1000;
668
693
  let _biQStuckMs = 5 * 60 * 1000;
669
694
 
670
- function _biPos(n, lbl) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${lbl} must be positive integer`); return v; }
695
+ function _biPos(n, lbl) {
696
+ const v = Math.floor(Number(n));
697
+ if (!Number.isFinite(v) || v <= 0)
698
+ throw new Error(`${lbl} must be positive integer`);
699
+ return v;
700
+ }
671
701
 
672
- export function setMaxActiveBiDatasetsPerOwnerV2(n) { _biMaxActivePerOwner = _biPos(n, "maxActiveBiDatasetsPerOwner"); }
673
- export function getMaxActiveBiDatasetsPerOwnerV2() { return _biMaxActivePerOwner; }
674
- export function setMaxPendingBiQueriesPerDatasetV2(n) { _biMaxPendingPerDs = _biPos(n, "maxPendingBiQueriesPerDataset"); }
675
- export function getMaxPendingBiQueriesPerDatasetV2() { return _biMaxPendingPerDs; }
676
- export function setBiDatasetIdleMsV2(n) { _biDsIdleMs = _biPos(n, "biDatasetIdleMs"); }
677
- export function getBiDatasetIdleMsV2() { return _biDsIdleMs; }
678
- export function setBiQueryStuckMsV2(n) { _biQStuckMs = _biPos(n, "biQueryStuckMs"); }
679
- export function getBiQueryStuckMsV2() { return _biQStuckMs; }
702
+ export function setMaxActiveBiDatasetsPerOwnerV2(n) {
703
+ _biMaxActivePerOwner = _biPos(n, "maxActiveBiDatasetsPerOwner");
704
+ }
705
+ export function getMaxActiveBiDatasetsPerOwnerV2() {
706
+ return _biMaxActivePerOwner;
707
+ }
708
+ export function setMaxPendingBiQueriesPerDatasetV2(n) {
709
+ _biMaxPendingPerDs = _biPos(n, "maxPendingBiQueriesPerDataset");
710
+ }
711
+ export function getMaxPendingBiQueriesPerDatasetV2() {
712
+ return _biMaxPendingPerDs;
713
+ }
714
+ export function setBiDatasetIdleMsV2(n) {
715
+ _biDsIdleMs = _biPos(n, "biDatasetIdleMs");
716
+ }
717
+ export function getBiDatasetIdleMsV2() {
718
+ return _biDsIdleMs;
719
+ }
720
+ export function setBiQueryStuckMsV2(n) {
721
+ _biQStuckMs = _biPos(n, "biQueryStuckMs");
722
+ }
723
+ export function getBiQueryStuckMsV2() {
724
+ return _biQStuckMs;
725
+ }
680
726
 
681
727
  export function _resetStateBiEngineV2() {
682
- _biDatasets.clear(); _biQueries.clear();
683
- _biMaxActivePerOwner = 8; _biMaxPendingPerDs = 10;
684
- _biDsIdleMs = 7 * 24 * 60 * 60 * 1000; _biQStuckMs = 5 * 60 * 1000;
728
+ _biDatasets.clear();
729
+ _biQueries.clear();
730
+ _biMaxActivePerOwner = 8;
731
+ _biMaxPendingPerDs = 10;
732
+ _biDsIdleMs = 7 * 24 * 60 * 60 * 1000;
733
+ _biQStuckMs = 5 * 60 * 1000;
685
734
  }
686
735
 
687
736
  export function registerBiDatasetV2({ id, owner, source, metadata } = {}) {
688
737
  if (!id || typeof id !== "string") throw new Error("id is required");
689
738
  if (!owner || typeof owner !== "string") throw new Error("owner is required");
690
- if (_biDatasets.has(id)) throw new Error(`bi dataset ${id} already registered`);
739
+ if (_biDatasets.has(id))
740
+ throw new Error(`bi dataset ${id} already registered`);
691
741
  const now = Date.now();
692
- const d = { id, owner, source: source || "", status: BI_DATASET_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, archivedAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
742
+ const d = {
743
+ id,
744
+ owner,
745
+ source: source || "",
746
+ status: BI_DATASET_MATURITY_V2.PENDING,
747
+ createdAt: now,
748
+ updatedAt: now,
749
+ activatedAt: null,
750
+ archivedAt: null,
751
+ lastTouchedAt: now,
752
+ metadata: { ...(metadata || {}) },
753
+ };
693
754
  _biDatasets.set(id, d);
694
755
  return { ...d, metadata: { ...d.metadata } };
695
756
  }
696
- function _biCheckD(from, to) { const a = _biDsTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid bi dataset transition ${from} → ${to}`); }
697
- function _biCountActive(owner) { let n = 0; for (const d of _biDatasets.values()) if (d.owner === owner && d.status === BI_DATASET_MATURITY_V2.ACTIVE) n++; return n; }
757
+ function _biCheckD(from, to) {
758
+ const a = _biDsTrans.get(from);
759
+ if (!a || !a.has(to))
760
+ throw new Error(`invalid bi dataset transition ${from} → ${to}`);
761
+ }
762
+ function _biCountActive(owner) {
763
+ let n = 0;
764
+ for (const d of _biDatasets.values())
765
+ if (d.owner === owner && d.status === BI_DATASET_MATURITY_V2.ACTIVE) n++;
766
+ return n;
767
+ }
698
768
 
699
769
  export function activateBiDatasetV2(id) {
700
- const d = _biDatasets.get(id); if (!d) throw new Error(`bi dataset ${id} not found`);
770
+ const d = _biDatasets.get(id);
771
+ if (!d) throw new Error(`bi dataset ${id} not found`);
701
772
  _biCheckD(d.status, BI_DATASET_MATURITY_V2.ACTIVE);
702
773
  const recovery = d.status === BI_DATASET_MATURITY_V2.STALE;
703
- if (!recovery) { const a = _biCountActive(d.owner); if (a >= _biMaxActivePerOwner) throw new Error(`max active bi datasets per owner (${_biMaxActivePerOwner}) reached for ${d.owner}`); }
704
- const now = Date.now(); d.status = BI_DATASET_MATURITY_V2.ACTIVE; d.updatedAt = now; d.lastTouchedAt = now; if (!d.activatedAt) d.activatedAt = now;
774
+ if (!recovery) {
775
+ const a = _biCountActive(d.owner);
776
+ if (a >= _biMaxActivePerOwner)
777
+ throw new Error(
778
+ `max active bi datasets per owner (${_biMaxActivePerOwner}) reached for ${d.owner}`,
779
+ );
780
+ }
781
+ const now = Date.now();
782
+ d.status = BI_DATASET_MATURITY_V2.ACTIVE;
783
+ d.updatedAt = now;
784
+ d.lastTouchedAt = now;
785
+ if (!d.activatedAt) d.activatedAt = now;
705
786
  return { ...d, metadata: { ...d.metadata } };
706
787
  }
707
- export function staleBiDatasetV2(id) { const d = _biDatasets.get(id); if (!d) throw new Error(`bi dataset ${id} not found`); _biCheckD(d.status, BI_DATASET_MATURITY_V2.STALE); d.status = BI_DATASET_MATURITY_V2.STALE; d.updatedAt = Date.now(); return { ...d, metadata: { ...d.metadata } }; }
708
- export function archiveBiDatasetV2(id) { const d = _biDatasets.get(id); if (!d) throw new Error(`bi dataset ${id} not found`); _biCheckD(d.status, BI_DATASET_MATURITY_V2.ARCHIVED); const now = Date.now(); d.status = BI_DATASET_MATURITY_V2.ARCHIVED; d.updatedAt = now; if (!d.archivedAt) d.archivedAt = now; return { ...d, metadata: { ...d.metadata } }; }
709
- export function touchBiDatasetV2(id) { const d = _biDatasets.get(id); if (!d) throw new Error(`bi dataset ${id} not found`); if (_biDsTerminal.has(d.status)) throw new Error(`cannot touch terminal bi dataset ${id}`); const now = Date.now(); d.lastTouchedAt = now; d.updatedAt = now; return { ...d, metadata: { ...d.metadata } }; }
710
- export function getBiDatasetV2(id) { const d = _biDatasets.get(id); if (!d) return null; return { ...d, metadata: { ...d.metadata } }; }
711
- export function listBiDatasetsV2() { return [..._biDatasets.values()].map((d) => ({ ...d, metadata: { ...d.metadata } })); }
788
+ export function staleBiDatasetV2(id) {
789
+ const d = _biDatasets.get(id);
790
+ if (!d) throw new Error(`bi dataset ${id} not found`);
791
+ _biCheckD(d.status, BI_DATASET_MATURITY_V2.STALE);
792
+ d.status = BI_DATASET_MATURITY_V2.STALE;
793
+ d.updatedAt = Date.now();
794
+ return { ...d, metadata: { ...d.metadata } };
795
+ }
796
+ export function archiveBiDatasetV2(id) {
797
+ const d = _biDatasets.get(id);
798
+ if (!d) throw new Error(`bi dataset ${id} not found`);
799
+ _biCheckD(d.status, BI_DATASET_MATURITY_V2.ARCHIVED);
800
+ const now = Date.now();
801
+ d.status = BI_DATASET_MATURITY_V2.ARCHIVED;
802
+ d.updatedAt = now;
803
+ if (!d.archivedAt) d.archivedAt = now;
804
+ return { ...d, metadata: { ...d.metadata } };
805
+ }
806
+ export function touchBiDatasetV2(id) {
807
+ const d = _biDatasets.get(id);
808
+ if (!d) throw new Error(`bi dataset ${id} not found`);
809
+ if (_biDsTerminal.has(d.status))
810
+ throw new Error(`cannot touch terminal bi dataset ${id}`);
811
+ const now = Date.now();
812
+ d.lastTouchedAt = now;
813
+ d.updatedAt = now;
814
+ return { ...d, metadata: { ...d.metadata } };
815
+ }
816
+ export function getBiDatasetV2(id) {
817
+ const d = _biDatasets.get(id);
818
+ if (!d) return null;
819
+ return { ...d, metadata: { ...d.metadata } };
820
+ }
821
+ export function listBiDatasetsV2() {
822
+ return [..._biDatasets.values()].map((d) => ({
823
+ ...d,
824
+ metadata: { ...d.metadata },
825
+ }));
826
+ }
712
827
 
713
- function _biCountPending(did) { let n = 0; for (const q of _biQueries.values()) if (q.datasetId === did && (q.status === BI_QUERY_LIFECYCLE_V2.QUEUED || q.status === BI_QUERY_LIFECYCLE_V2.RUNNING)) n++; return n; }
828
+ function _biCountPending(did) {
829
+ let n = 0;
830
+ for (const q of _biQueries.values())
831
+ if (
832
+ q.datasetId === did &&
833
+ (q.status === BI_QUERY_LIFECYCLE_V2.QUEUED ||
834
+ q.status === BI_QUERY_LIFECYCLE_V2.RUNNING)
835
+ )
836
+ n++;
837
+ return n;
838
+ }
714
839
 
715
840
  export function createBiQueryV2({ id, datasetId, sql, metadata } = {}) {
716
841
  if (!id || typeof id !== "string") throw new Error("id is required");
717
- if (!datasetId || typeof datasetId !== "string") throw new Error("datasetId is required");
842
+ if (!datasetId || typeof datasetId !== "string")
843
+ throw new Error("datasetId is required");
718
844
  if (_biQueries.has(id)) throw new Error(`bi query ${id} already exists`);
719
- if (!_biDatasets.has(datasetId)) throw new Error(`bi dataset ${datasetId} not found`);
845
+ if (!_biDatasets.has(datasetId))
846
+ throw new Error(`bi dataset ${datasetId} not found`);
720
847
  const pending = _biCountPending(datasetId);
721
- if (pending >= _biMaxPendingPerDs) throw new Error(`max pending bi queries per dataset (${_biMaxPendingPerDs}) reached for ${datasetId}`);
848
+ if (pending >= _biMaxPendingPerDs)
849
+ throw new Error(
850
+ `max pending bi queries per dataset (${_biMaxPendingPerDs}) reached for ${datasetId}`,
851
+ );
722
852
  const now = Date.now();
723
- const q = { id, datasetId, sql: sql || "", status: BI_QUERY_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
853
+ const q = {
854
+ id,
855
+ datasetId,
856
+ sql: sql || "",
857
+ status: BI_QUERY_LIFECYCLE_V2.QUEUED,
858
+ createdAt: now,
859
+ updatedAt: now,
860
+ startedAt: null,
861
+ settledAt: null,
862
+ metadata: { ...(metadata || {}) },
863
+ };
724
864
  _biQueries.set(id, q);
725
865
  return { ...q, metadata: { ...q.metadata } };
726
866
  }
727
- function _biCheckQ(from, to) { const a = _biQTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid bi query transition ${from} → ${to}`); }
728
- export function startBiQueryV2(id) { const q = _biQueries.get(id); if (!q) throw new Error(`bi query ${id} not found`); _biCheckQ(q.status, BI_QUERY_LIFECYCLE_V2.RUNNING); const now = Date.now(); q.status = BI_QUERY_LIFECYCLE_V2.RUNNING; q.updatedAt = now; if (!q.startedAt) q.startedAt = now; return { ...q, metadata: { ...q.metadata } }; }
729
- export function completeBiQueryV2(id) { const q = _biQueries.get(id); if (!q) throw new Error(`bi query ${id} not found`); _biCheckQ(q.status, BI_QUERY_LIFECYCLE_V2.COMPLETED); const now = Date.now(); q.status = BI_QUERY_LIFECYCLE_V2.COMPLETED; q.updatedAt = now; if (!q.settledAt) q.settledAt = now; return { ...q, metadata: { ...q.metadata } }; }
730
- export function failBiQueryV2(id, reason) { const q = _biQueries.get(id); if (!q) throw new Error(`bi query ${id} not found`); _biCheckQ(q.status, BI_QUERY_LIFECYCLE_V2.FAILED); const now = Date.now(); q.status = BI_QUERY_LIFECYCLE_V2.FAILED; q.updatedAt = now; if (!q.settledAt) q.settledAt = now; if (reason) q.metadata.failReason = String(reason); return { ...q, metadata: { ...q.metadata } }; }
731
- export function cancelBiQueryV2(id, reason) { const q = _biQueries.get(id); if (!q) throw new Error(`bi query ${id} not found`); _biCheckQ(q.status, BI_QUERY_LIFECYCLE_V2.CANCELLED); const now = Date.now(); q.status = BI_QUERY_LIFECYCLE_V2.CANCELLED; q.updatedAt = now; if (!q.settledAt) q.settledAt = now; if (reason) q.metadata.cancelReason = String(reason); return { ...q, metadata: { ...q.metadata } }; }
732
- export function getBiQueryV2(id) { const q = _biQueries.get(id); if (!q) return null; return { ...q, metadata: { ...q.metadata } }; }
733
- export function listBiQueriesV2() { return [..._biQueries.values()].map((q) => ({ ...q, metadata: { ...q.metadata } })); }
867
+ function _biCheckQ(from, to) {
868
+ const a = _biQTrans.get(from);
869
+ if (!a || !a.has(to))
870
+ throw new Error(`invalid bi query transition ${from} ${to}`);
871
+ }
872
+ export function startBiQueryV2(id) {
873
+ const q = _biQueries.get(id);
874
+ if (!q) throw new Error(`bi query ${id} not found`);
875
+ _biCheckQ(q.status, BI_QUERY_LIFECYCLE_V2.RUNNING);
876
+ const now = Date.now();
877
+ q.status = BI_QUERY_LIFECYCLE_V2.RUNNING;
878
+ q.updatedAt = now;
879
+ if (!q.startedAt) q.startedAt = now;
880
+ return { ...q, metadata: { ...q.metadata } };
881
+ }
882
+ export function completeBiQueryV2(id) {
883
+ const q = _biQueries.get(id);
884
+ if (!q) throw new Error(`bi query ${id} not found`);
885
+ _biCheckQ(q.status, BI_QUERY_LIFECYCLE_V2.COMPLETED);
886
+ const now = Date.now();
887
+ q.status = BI_QUERY_LIFECYCLE_V2.COMPLETED;
888
+ q.updatedAt = now;
889
+ if (!q.settledAt) q.settledAt = now;
890
+ return { ...q, metadata: { ...q.metadata } };
891
+ }
892
+ export function failBiQueryV2(id, reason) {
893
+ const q = _biQueries.get(id);
894
+ if (!q) throw new Error(`bi query ${id} not found`);
895
+ _biCheckQ(q.status, BI_QUERY_LIFECYCLE_V2.FAILED);
896
+ const now = Date.now();
897
+ q.status = BI_QUERY_LIFECYCLE_V2.FAILED;
898
+ q.updatedAt = now;
899
+ if (!q.settledAt) q.settledAt = now;
900
+ if (reason) q.metadata.failReason = String(reason);
901
+ return { ...q, metadata: { ...q.metadata } };
902
+ }
903
+ export function cancelBiQueryV2(id, reason) {
904
+ const q = _biQueries.get(id);
905
+ if (!q) throw new Error(`bi query ${id} not found`);
906
+ _biCheckQ(q.status, BI_QUERY_LIFECYCLE_V2.CANCELLED);
907
+ const now = Date.now();
908
+ q.status = BI_QUERY_LIFECYCLE_V2.CANCELLED;
909
+ q.updatedAt = now;
910
+ if (!q.settledAt) q.settledAt = now;
911
+ if (reason) q.metadata.cancelReason = String(reason);
912
+ return { ...q, metadata: { ...q.metadata } };
913
+ }
914
+ export function getBiQueryV2(id) {
915
+ const q = _biQueries.get(id);
916
+ if (!q) return null;
917
+ return { ...q, metadata: { ...q.metadata } };
918
+ }
919
+ export function listBiQueriesV2() {
920
+ return [..._biQueries.values()].map((q) => ({
921
+ ...q,
922
+ metadata: { ...q.metadata },
923
+ }));
924
+ }
734
925
 
735
- export function autoStaleIdleBiDatasetsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const d of _biDatasets.values()) if (d.status === BI_DATASET_MATURITY_V2.ACTIVE && (t - d.lastTouchedAt) >= _biDsIdleMs) { d.status = BI_DATASET_MATURITY_V2.STALE; d.updatedAt = t; flipped.push(d.id); } return { flipped, count: flipped.length }; }
736
- export function autoFailStuckBiQueriesV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const q of _biQueries.values()) if (q.status === BI_QUERY_LIFECYCLE_V2.RUNNING && q.startedAt != null && (t - q.startedAt) >= _biQStuckMs) { q.status = BI_QUERY_LIFECYCLE_V2.FAILED; q.updatedAt = t; if (!q.settledAt) q.settledAt = t; q.metadata.failReason = "auto-fail-stuck"; flipped.push(q.id); } return { flipped, count: flipped.length }; }
926
+ export function autoStaleIdleBiDatasetsV2({ now } = {}) {
927
+ const t = now ?? Date.now();
928
+ const flipped = [];
929
+ for (const d of _biDatasets.values())
930
+ if (
931
+ d.status === BI_DATASET_MATURITY_V2.ACTIVE &&
932
+ t - d.lastTouchedAt >= _biDsIdleMs
933
+ ) {
934
+ d.status = BI_DATASET_MATURITY_V2.STALE;
935
+ d.updatedAt = t;
936
+ flipped.push(d.id);
937
+ }
938
+ return { flipped, count: flipped.length };
939
+ }
940
+ export function autoFailStuckBiQueriesV2({ now } = {}) {
941
+ const t = now ?? Date.now();
942
+ const flipped = [];
943
+ for (const q of _biQueries.values())
944
+ if (
945
+ q.status === BI_QUERY_LIFECYCLE_V2.RUNNING &&
946
+ q.startedAt != null &&
947
+ t - q.startedAt >= _biQStuckMs
948
+ ) {
949
+ q.status = BI_QUERY_LIFECYCLE_V2.FAILED;
950
+ q.updatedAt = t;
951
+ if (!q.settledAt) q.settledAt = t;
952
+ q.metadata.failReason = "auto-fail-stuck";
953
+ flipped.push(q.id);
954
+ }
955
+ return { flipped, count: flipped.length };
956
+ }
737
957
 
738
958
  export function getBiEngineStatsV2() {
739
- const datasetsByStatus = {}; for (const s of Object.values(BI_DATASET_MATURITY_V2)) datasetsByStatus[s] = 0; for (const d of _biDatasets.values()) datasetsByStatus[d.status]++;
740
- const queriesByStatus = {}; for (const s of Object.values(BI_QUERY_LIFECYCLE_V2)) queriesByStatus[s] = 0; for (const q of _biQueries.values()) queriesByStatus[q.status]++;
741
- return { totalDatasetsV2: _biDatasets.size, totalQueriesV2: _biQueries.size, maxActiveBiDatasetsPerOwner: _biMaxActivePerOwner, maxPendingBiQueriesPerDataset: _biMaxPendingPerDs, biDatasetIdleMs: _biDsIdleMs, biQueryStuckMs: _biQStuckMs, datasetsByStatus, queriesByStatus };
959
+ const datasetsByStatus = {};
960
+ for (const s of Object.values(BI_DATASET_MATURITY_V2))
961
+ datasetsByStatus[s] = 0;
962
+ for (const d of _biDatasets.values()) datasetsByStatus[d.status]++;
963
+ const queriesByStatus = {};
964
+ for (const s of Object.values(BI_QUERY_LIFECYCLE_V2)) queriesByStatus[s] = 0;
965
+ for (const q of _biQueries.values()) queriesByStatus[q.status]++;
966
+ return {
967
+ totalDatasetsV2: _biDatasets.size,
968
+ totalQueriesV2: _biQueries.size,
969
+ maxActiveBiDatasetsPerOwner: _biMaxActivePerOwner,
970
+ maxPendingBiQueriesPerDataset: _biMaxPendingPerDs,
971
+ biDatasetIdleMs: _biDsIdleMs,
972
+ biQueryStuckMs: _biQStuckMs,
973
+ datasetsByStatus,
974
+ queriesByStatus,
975
+ };
976
+ }
977
+
978
+ // =====================================================================
979
+ // bi-engine V2 governance overlay (iter21)
980
+ // =====================================================================
981
+ export const BIGOV_PROFILE_MATURITY_V2 = Object.freeze({
982
+ PENDING: "pending",
983
+ ACTIVE: "active",
984
+ STALE: "stale",
985
+ ARCHIVED: "archived",
986
+ });
987
+ export const BIGOV_QUERY_LIFECYCLE_V2 = Object.freeze({
988
+ QUEUED: "queued",
989
+ QUERYING: "querying",
990
+ QUERIED: "queried",
991
+ FAILED: "failed",
992
+ CANCELLED: "cancelled",
993
+ });
994
+ const _bigovPTrans = new Map([
995
+ [
996
+ BIGOV_PROFILE_MATURITY_V2.PENDING,
997
+ new Set([
998
+ BIGOV_PROFILE_MATURITY_V2.ACTIVE,
999
+ BIGOV_PROFILE_MATURITY_V2.ARCHIVED,
1000
+ ]),
1001
+ ],
1002
+ [
1003
+ BIGOV_PROFILE_MATURITY_V2.ACTIVE,
1004
+ new Set([
1005
+ BIGOV_PROFILE_MATURITY_V2.STALE,
1006
+ BIGOV_PROFILE_MATURITY_V2.ARCHIVED,
1007
+ ]),
1008
+ ],
1009
+ [
1010
+ BIGOV_PROFILE_MATURITY_V2.STALE,
1011
+ new Set([
1012
+ BIGOV_PROFILE_MATURITY_V2.ACTIVE,
1013
+ BIGOV_PROFILE_MATURITY_V2.ARCHIVED,
1014
+ ]),
1015
+ ],
1016
+ [BIGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
1017
+ ]);
1018
+ const _bigovPTerminal = new Set([BIGOV_PROFILE_MATURITY_V2.ARCHIVED]);
1019
+ const _bigovJTrans = new Map([
1020
+ [
1021
+ BIGOV_QUERY_LIFECYCLE_V2.QUEUED,
1022
+ new Set([
1023
+ BIGOV_QUERY_LIFECYCLE_V2.QUERYING,
1024
+ BIGOV_QUERY_LIFECYCLE_V2.CANCELLED,
1025
+ ]),
1026
+ ],
1027
+ [
1028
+ BIGOV_QUERY_LIFECYCLE_V2.QUERYING,
1029
+ new Set([
1030
+ BIGOV_QUERY_LIFECYCLE_V2.QUERIED,
1031
+ BIGOV_QUERY_LIFECYCLE_V2.FAILED,
1032
+ BIGOV_QUERY_LIFECYCLE_V2.CANCELLED,
1033
+ ]),
1034
+ ],
1035
+ [BIGOV_QUERY_LIFECYCLE_V2.QUERIED, new Set()],
1036
+ [BIGOV_QUERY_LIFECYCLE_V2.FAILED, new Set()],
1037
+ [BIGOV_QUERY_LIFECYCLE_V2.CANCELLED, new Set()],
1038
+ ]);
1039
+ const _bigovPsV2 = new Map();
1040
+ const _bigovJsV2 = new Map();
1041
+ let _bigovMaxActive = 6,
1042
+ _bigovMaxPending = 15,
1043
+ _bigovIdleMs = 30 * 24 * 60 * 60 * 1000,
1044
+ _bigovStuckMs = 60 * 1000;
1045
+ function _bigovPos(n, label) {
1046
+ const v = Math.floor(Number(n));
1047
+ if (!Number.isFinite(v) || v <= 0)
1048
+ throw new Error(`${label} must be positive integer`);
1049
+ return v;
1050
+ }
1051
+ function _bigovCheckP(from, to) {
1052
+ const a = _bigovPTrans.get(from);
1053
+ if (!a || !a.has(to))
1054
+ throw new Error(`invalid bigov profile transition ${from} → ${to}`);
1055
+ }
1056
+ function _bigovCheckJ(from, to) {
1057
+ const a = _bigovJTrans.get(from);
1058
+ if (!a || !a.has(to))
1059
+ throw new Error(`invalid bigov query transition ${from} → ${to}`);
1060
+ }
1061
+ function _bigovCountActive(owner) {
1062
+ let c = 0;
1063
+ for (const p of _bigovPsV2.values())
1064
+ if (p.owner === owner && p.status === BIGOV_PROFILE_MATURITY_V2.ACTIVE) c++;
1065
+ return c;
1066
+ }
1067
+ function _bigovCountPending(profileId) {
1068
+ let c = 0;
1069
+ for (const j of _bigovJsV2.values())
1070
+ if (
1071
+ j.profileId === profileId &&
1072
+ (j.status === BIGOV_QUERY_LIFECYCLE_V2.QUEUED ||
1073
+ j.status === BIGOV_QUERY_LIFECYCLE_V2.QUERYING)
1074
+ )
1075
+ c++;
1076
+ return c;
1077
+ }
1078
+ export function setMaxActiveBigovProfilesPerOwnerV2(n) {
1079
+ _bigovMaxActive = _bigovPos(n, "maxActiveBigovProfilesPerOwner");
1080
+ }
1081
+ export function getMaxActiveBigovProfilesPerOwnerV2() {
1082
+ return _bigovMaxActive;
1083
+ }
1084
+ export function setMaxPendingBigovQuerysPerProfileV2(n) {
1085
+ _bigovMaxPending = _bigovPos(n, "maxPendingBigovQuerysPerProfile");
1086
+ }
1087
+ export function getMaxPendingBigovQuerysPerProfileV2() {
1088
+ return _bigovMaxPending;
1089
+ }
1090
+ export function setBigovProfileIdleMsV2(n) {
1091
+ _bigovIdleMs = _bigovPos(n, "bigovProfileIdleMs");
1092
+ }
1093
+ export function getBigovProfileIdleMsV2() {
1094
+ return _bigovIdleMs;
1095
+ }
1096
+ export function setBigovQueryStuckMsV2(n) {
1097
+ _bigovStuckMs = _bigovPos(n, "bigovQueryStuckMs");
1098
+ }
1099
+ export function getBigovQueryStuckMsV2() {
1100
+ return _bigovStuckMs;
1101
+ }
1102
+ export function _resetStateBiEngineGovV2() {
1103
+ _bigovPsV2.clear();
1104
+ _bigovJsV2.clear();
1105
+ _bigovMaxActive = 6;
1106
+ _bigovMaxPending = 15;
1107
+ _bigovIdleMs = 30 * 24 * 60 * 60 * 1000;
1108
+ _bigovStuckMs = 60 * 1000;
1109
+ }
1110
+ export function registerBigovProfileV2({ id, owner, dataset, metadata } = {}) {
1111
+ if (!id || !owner) throw new Error("id and owner required");
1112
+ if (_bigovPsV2.has(id)) throw new Error(`bigov profile ${id} already exists`);
1113
+ const now = Date.now();
1114
+ const p = {
1115
+ id,
1116
+ owner,
1117
+ dataset: dataset || "default",
1118
+ status: BIGOV_PROFILE_MATURITY_V2.PENDING,
1119
+ createdAt: now,
1120
+ updatedAt: now,
1121
+ lastTouchedAt: now,
1122
+ activatedAt: null,
1123
+ archivedAt: null,
1124
+ metadata: { ...(metadata || {}) },
1125
+ };
1126
+ _bigovPsV2.set(id, p);
1127
+ return { ...p, metadata: { ...p.metadata } };
1128
+ }
1129
+ export function activateBigovProfileV2(id) {
1130
+ const p = _bigovPsV2.get(id);
1131
+ if (!p) throw new Error(`bigov profile ${id} not found`);
1132
+ const isInitial = p.status === BIGOV_PROFILE_MATURITY_V2.PENDING;
1133
+ _bigovCheckP(p.status, BIGOV_PROFILE_MATURITY_V2.ACTIVE);
1134
+ if (isInitial && _bigovCountActive(p.owner) >= _bigovMaxActive)
1135
+ throw new Error(`max active bigov profiles for owner ${p.owner} reached`);
1136
+ const now = Date.now();
1137
+ p.status = BIGOV_PROFILE_MATURITY_V2.ACTIVE;
1138
+ p.updatedAt = now;
1139
+ p.lastTouchedAt = now;
1140
+ if (!p.activatedAt) p.activatedAt = now;
1141
+ return { ...p, metadata: { ...p.metadata } };
1142
+ }
1143
+ export function staleBigovProfileV2(id) {
1144
+ const p = _bigovPsV2.get(id);
1145
+ if (!p) throw new Error(`bigov profile ${id} not found`);
1146
+ _bigovCheckP(p.status, BIGOV_PROFILE_MATURITY_V2.STALE);
1147
+ p.status = BIGOV_PROFILE_MATURITY_V2.STALE;
1148
+ p.updatedAt = Date.now();
1149
+ return { ...p, metadata: { ...p.metadata } };
1150
+ }
1151
+ export function archiveBigovProfileV2(id) {
1152
+ const p = _bigovPsV2.get(id);
1153
+ if (!p) throw new Error(`bigov profile ${id} not found`);
1154
+ _bigovCheckP(p.status, BIGOV_PROFILE_MATURITY_V2.ARCHIVED);
1155
+ const now = Date.now();
1156
+ p.status = BIGOV_PROFILE_MATURITY_V2.ARCHIVED;
1157
+ p.updatedAt = now;
1158
+ if (!p.archivedAt) p.archivedAt = now;
1159
+ return { ...p, metadata: { ...p.metadata } };
1160
+ }
1161
+ export function touchBigovProfileV2(id) {
1162
+ const p = _bigovPsV2.get(id);
1163
+ if (!p) throw new Error(`bigov profile ${id} not found`);
1164
+ if (_bigovPTerminal.has(p.status))
1165
+ throw new Error(`cannot touch terminal bigov profile ${id}`);
1166
+ const now = Date.now();
1167
+ p.lastTouchedAt = now;
1168
+ p.updatedAt = now;
1169
+ return { ...p, metadata: { ...p.metadata } };
1170
+ }
1171
+ export function getBigovProfileV2(id) {
1172
+ const p = _bigovPsV2.get(id);
1173
+ if (!p) return null;
1174
+ return { ...p, metadata: { ...p.metadata } };
1175
+ }
1176
+ export function listBigovProfilesV2() {
1177
+ return [..._bigovPsV2.values()].map((p) => ({
1178
+ ...p,
1179
+ metadata: { ...p.metadata },
1180
+ }));
1181
+ }
1182
+ export function createBigovQueryV2({ id, profileId, kpi, metadata } = {}) {
1183
+ if (!id || !profileId) throw new Error("id and profileId required");
1184
+ if (_bigovJsV2.has(id)) throw new Error(`bigov query ${id} already exists`);
1185
+ if (!_bigovPsV2.has(profileId))
1186
+ throw new Error(`bigov profile ${profileId} not found`);
1187
+ if (_bigovCountPending(profileId) >= _bigovMaxPending)
1188
+ throw new Error(
1189
+ `max pending bigov querys for profile ${profileId} reached`,
1190
+ );
1191
+ const now = Date.now();
1192
+ const j = {
1193
+ id,
1194
+ profileId,
1195
+ kpi: kpi || "",
1196
+ status: BIGOV_QUERY_LIFECYCLE_V2.QUEUED,
1197
+ createdAt: now,
1198
+ updatedAt: now,
1199
+ startedAt: null,
1200
+ settledAt: null,
1201
+ metadata: { ...(metadata || {}) },
1202
+ };
1203
+ _bigovJsV2.set(id, j);
1204
+ return { ...j, metadata: { ...j.metadata } };
1205
+ }
1206
+ export function queryingBigovQueryV2(id) {
1207
+ const j = _bigovJsV2.get(id);
1208
+ if (!j) throw new Error(`bigov query ${id} not found`);
1209
+ _bigovCheckJ(j.status, BIGOV_QUERY_LIFECYCLE_V2.QUERYING);
1210
+ const now = Date.now();
1211
+ j.status = BIGOV_QUERY_LIFECYCLE_V2.QUERYING;
1212
+ j.updatedAt = now;
1213
+ if (!j.startedAt) j.startedAt = now;
1214
+ return { ...j, metadata: { ...j.metadata } };
1215
+ }
1216
+ export function completeQueryBigovV2(id) {
1217
+ const j = _bigovJsV2.get(id);
1218
+ if (!j) throw new Error(`bigov query ${id} not found`);
1219
+ _bigovCheckJ(j.status, BIGOV_QUERY_LIFECYCLE_V2.QUERIED);
1220
+ const now = Date.now();
1221
+ j.status = BIGOV_QUERY_LIFECYCLE_V2.QUERIED;
1222
+ j.updatedAt = now;
1223
+ if (!j.settledAt) j.settledAt = now;
1224
+ return { ...j, metadata: { ...j.metadata } };
1225
+ }
1226
+ export function failBigovQueryV2(id, reason) {
1227
+ const j = _bigovJsV2.get(id);
1228
+ if (!j) throw new Error(`bigov query ${id} not found`);
1229
+ _bigovCheckJ(j.status, BIGOV_QUERY_LIFECYCLE_V2.FAILED);
1230
+ const now = Date.now();
1231
+ j.status = BIGOV_QUERY_LIFECYCLE_V2.FAILED;
1232
+ j.updatedAt = now;
1233
+ if (!j.settledAt) j.settledAt = now;
1234
+ if (reason) j.metadata.failReason = String(reason);
1235
+ return { ...j, metadata: { ...j.metadata } };
1236
+ }
1237
+ export function cancelBigovQueryV2(id, reason) {
1238
+ const j = _bigovJsV2.get(id);
1239
+ if (!j) throw new Error(`bigov query ${id} not found`);
1240
+ _bigovCheckJ(j.status, BIGOV_QUERY_LIFECYCLE_V2.CANCELLED);
1241
+ const now = Date.now();
1242
+ j.status = BIGOV_QUERY_LIFECYCLE_V2.CANCELLED;
1243
+ j.updatedAt = now;
1244
+ if (!j.settledAt) j.settledAt = now;
1245
+ if (reason) j.metadata.cancelReason = String(reason);
1246
+ return { ...j, metadata: { ...j.metadata } };
1247
+ }
1248
+ export function getBigovQueryV2(id) {
1249
+ const j = _bigovJsV2.get(id);
1250
+ if (!j) return null;
1251
+ return { ...j, metadata: { ...j.metadata } };
1252
+ }
1253
+ export function listBigovQuerysV2() {
1254
+ return [..._bigovJsV2.values()].map((j) => ({
1255
+ ...j,
1256
+ metadata: { ...j.metadata },
1257
+ }));
1258
+ }
1259
+ export function autoStaleIdleBigovProfilesV2({ now } = {}) {
1260
+ const t = now ?? Date.now();
1261
+ const flipped = [];
1262
+ for (const p of _bigovPsV2.values())
1263
+ if (
1264
+ p.status === BIGOV_PROFILE_MATURITY_V2.ACTIVE &&
1265
+ t - p.lastTouchedAt >= _bigovIdleMs
1266
+ ) {
1267
+ p.status = BIGOV_PROFILE_MATURITY_V2.STALE;
1268
+ p.updatedAt = t;
1269
+ flipped.push(p.id);
1270
+ }
1271
+ return { flipped, count: flipped.length };
1272
+ }
1273
+ export function autoFailStuckBigovQuerysV2({ now } = {}) {
1274
+ const t = now ?? Date.now();
1275
+ const flipped = [];
1276
+ for (const j of _bigovJsV2.values())
1277
+ if (
1278
+ j.status === BIGOV_QUERY_LIFECYCLE_V2.QUERYING &&
1279
+ j.startedAt != null &&
1280
+ t - j.startedAt >= _bigovStuckMs
1281
+ ) {
1282
+ j.status = BIGOV_QUERY_LIFECYCLE_V2.FAILED;
1283
+ j.updatedAt = t;
1284
+ if (!j.settledAt) j.settledAt = t;
1285
+ j.metadata.failReason = "auto-fail-stuck";
1286
+ flipped.push(j.id);
1287
+ }
1288
+ return { flipped, count: flipped.length };
1289
+ }
1290
+ export function getBiEngineGovStatsV2() {
1291
+ const profilesByStatus = {};
1292
+ for (const v of Object.values(BIGOV_PROFILE_MATURITY_V2))
1293
+ profilesByStatus[v] = 0;
1294
+ for (const p of _bigovPsV2.values()) profilesByStatus[p.status]++;
1295
+ const querysByStatus = {};
1296
+ for (const v of Object.values(BIGOV_QUERY_LIFECYCLE_V2))
1297
+ querysByStatus[v] = 0;
1298
+ for (const j of _bigovJsV2.values()) querysByStatus[j.status]++;
1299
+ return {
1300
+ totalBigovProfilesV2: _bigovPsV2.size,
1301
+ totalBigovQuerysV2: _bigovJsV2.size,
1302
+ maxActiveBigovProfilesPerOwner: _bigovMaxActive,
1303
+ maxPendingBigovQuerysPerProfile: _bigovMaxPending,
1304
+ bigovProfileIdleMs: _bigovIdleMs,
1305
+ bigovQueryStuckMs: _bigovStuckMs,
1306
+ profilesByStatus,
1307
+ querysByStatus,
1308
+ };
742
1309
  }