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
@@ -539,3 +539,339 @@ export function _resetStateMemoryManagerV2() {
539
539
  _entryIdleMsV2 = MEMORY_DEFAULT_ENTRY_IDLE_MS;
540
540
  _jobStuckMsV2 = MEMORY_DEFAULT_JOB_STUCK_MS;
541
541
  }
542
+
543
+ // =====================================================================
544
+ // memory-manager V2 governance overlay (iter21)
545
+ // =====================================================================
546
+ export const MEMGOV_PROFILE_MATURITY_V2 = Object.freeze({
547
+ PENDING: "pending",
548
+ ACTIVE: "active",
549
+ STALE: "stale",
550
+ ARCHIVED: "archived",
551
+ });
552
+ export const MEMGOV_RECALL_LIFECYCLE_V2 = Object.freeze({
553
+ QUEUED: "queued",
554
+ RECALLING: "recalling",
555
+ RECALLED: "recalled",
556
+ FAILED: "failed",
557
+ CANCELLED: "cancelled",
558
+ });
559
+ const _memgovPTrans = new Map([
560
+ [
561
+ MEMGOV_PROFILE_MATURITY_V2.PENDING,
562
+ new Set([
563
+ MEMGOV_PROFILE_MATURITY_V2.ACTIVE,
564
+ MEMGOV_PROFILE_MATURITY_V2.ARCHIVED,
565
+ ]),
566
+ ],
567
+ [
568
+ MEMGOV_PROFILE_MATURITY_V2.ACTIVE,
569
+ new Set([
570
+ MEMGOV_PROFILE_MATURITY_V2.STALE,
571
+ MEMGOV_PROFILE_MATURITY_V2.ARCHIVED,
572
+ ]),
573
+ ],
574
+ [
575
+ MEMGOV_PROFILE_MATURITY_V2.STALE,
576
+ new Set([
577
+ MEMGOV_PROFILE_MATURITY_V2.ACTIVE,
578
+ MEMGOV_PROFILE_MATURITY_V2.ARCHIVED,
579
+ ]),
580
+ ],
581
+ [MEMGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
582
+ ]);
583
+ const _memgovPTerminal = new Set([MEMGOV_PROFILE_MATURITY_V2.ARCHIVED]);
584
+ const _memgovJTrans = new Map([
585
+ [
586
+ MEMGOV_RECALL_LIFECYCLE_V2.QUEUED,
587
+ new Set([
588
+ MEMGOV_RECALL_LIFECYCLE_V2.RECALLING,
589
+ MEMGOV_RECALL_LIFECYCLE_V2.CANCELLED,
590
+ ]),
591
+ ],
592
+ [
593
+ MEMGOV_RECALL_LIFECYCLE_V2.RECALLING,
594
+ new Set([
595
+ MEMGOV_RECALL_LIFECYCLE_V2.RECALLED,
596
+ MEMGOV_RECALL_LIFECYCLE_V2.FAILED,
597
+ MEMGOV_RECALL_LIFECYCLE_V2.CANCELLED,
598
+ ]),
599
+ ],
600
+ [MEMGOV_RECALL_LIFECYCLE_V2.RECALLED, new Set()],
601
+ [MEMGOV_RECALL_LIFECYCLE_V2.FAILED, new Set()],
602
+ [MEMGOV_RECALL_LIFECYCLE_V2.CANCELLED, new Set()],
603
+ ]);
604
+ const _memgovPsV2 = new Map();
605
+ const _memgovJsV2 = new Map();
606
+ let _memgovMaxActive = 10,
607
+ _memgovMaxPending = 30,
608
+ _memgovIdleMs = 30 * 24 * 60 * 60 * 1000,
609
+ _memgovStuckMs = 60 * 1000;
610
+ function _memgovPos(n, label) {
611
+ const v = Math.floor(Number(n));
612
+ if (!Number.isFinite(v) || v <= 0)
613
+ throw new Error(`${label} must be positive integer`);
614
+ return v;
615
+ }
616
+ function _memgovCheckP(from, to) {
617
+ const a = _memgovPTrans.get(from);
618
+ if (!a || !a.has(to))
619
+ throw new Error(`invalid memgov profile transition ${from} → ${to}`);
620
+ }
621
+ function _memgovCheckJ(from, to) {
622
+ const a = _memgovJTrans.get(from);
623
+ if (!a || !a.has(to))
624
+ throw new Error(`invalid memgov recall transition ${from} → ${to}`);
625
+ }
626
+ function _memgovCountActive(owner) {
627
+ let c = 0;
628
+ for (const p of _memgovPsV2.values())
629
+ if (p.owner === owner && p.status === MEMGOV_PROFILE_MATURITY_V2.ACTIVE)
630
+ c++;
631
+ return c;
632
+ }
633
+ function _memgovCountPending(profileId) {
634
+ let c = 0;
635
+ for (const j of _memgovJsV2.values())
636
+ if (
637
+ j.profileId === profileId &&
638
+ (j.status === MEMGOV_RECALL_LIFECYCLE_V2.QUEUED ||
639
+ j.status === MEMGOV_RECALL_LIFECYCLE_V2.RECALLING)
640
+ )
641
+ c++;
642
+ return c;
643
+ }
644
+ export function setMaxActiveMemgovProfilesPerOwnerV2(n) {
645
+ _memgovMaxActive = _memgovPos(n, "maxActiveMemgovProfilesPerOwner");
646
+ }
647
+ export function getMaxActiveMemgovProfilesPerOwnerV2() {
648
+ return _memgovMaxActive;
649
+ }
650
+ export function setMaxPendingMemgovRecallsPerProfileV2(n) {
651
+ _memgovMaxPending = _memgovPos(n, "maxPendingMemgovRecallsPerProfile");
652
+ }
653
+ export function getMaxPendingMemgovRecallsPerProfileV2() {
654
+ return _memgovMaxPending;
655
+ }
656
+ export function setMemgovProfileIdleMsV2(n) {
657
+ _memgovIdleMs = _memgovPos(n, "memgovProfileIdleMs");
658
+ }
659
+ export function getMemgovProfileIdleMsV2() {
660
+ return _memgovIdleMs;
661
+ }
662
+ export function setMemgovRecallStuckMsV2(n) {
663
+ _memgovStuckMs = _memgovPos(n, "memgovRecallStuckMs");
664
+ }
665
+ export function getMemgovRecallStuckMsV2() {
666
+ return _memgovStuckMs;
667
+ }
668
+ export function _resetStateMemoryManagerGovV2() {
669
+ _memgovPsV2.clear();
670
+ _memgovJsV2.clear();
671
+ _memgovMaxActive = 10;
672
+ _memgovMaxPending = 30;
673
+ _memgovIdleMs = 30 * 24 * 60 * 60 * 1000;
674
+ _memgovStuckMs = 60 * 1000;
675
+ }
676
+ export function registerMemgovProfileV2({ id, owner, scope, metadata } = {}) {
677
+ if (!id || !owner) throw new Error("id and owner required");
678
+ if (_memgovPsV2.has(id))
679
+ throw new Error(`memgov profile ${id} already exists`);
680
+ const now = Date.now();
681
+ const p = {
682
+ id,
683
+ owner,
684
+ scope: scope || "user",
685
+ status: MEMGOV_PROFILE_MATURITY_V2.PENDING,
686
+ createdAt: now,
687
+ updatedAt: now,
688
+ lastTouchedAt: now,
689
+ activatedAt: null,
690
+ archivedAt: null,
691
+ metadata: { ...(metadata || {}) },
692
+ };
693
+ _memgovPsV2.set(id, p);
694
+ return { ...p, metadata: { ...p.metadata } };
695
+ }
696
+ export function activateMemgovProfileV2(id) {
697
+ const p = _memgovPsV2.get(id);
698
+ if (!p) throw new Error(`memgov profile ${id} not found`);
699
+ const isInitial = p.status === MEMGOV_PROFILE_MATURITY_V2.PENDING;
700
+ _memgovCheckP(p.status, MEMGOV_PROFILE_MATURITY_V2.ACTIVE);
701
+ if (isInitial && _memgovCountActive(p.owner) >= _memgovMaxActive)
702
+ throw new Error(`max active memgov profiles for owner ${p.owner} reached`);
703
+ const now = Date.now();
704
+ p.status = MEMGOV_PROFILE_MATURITY_V2.ACTIVE;
705
+ p.updatedAt = now;
706
+ p.lastTouchedAt = now;
707
+ if (!p.activatedAt) p.activatedAt = now;
708
+ return { ...p, metadata: { ...p.metadata } };
709
+ }
710
+ export function staleMemgovProfileV2(id) {
711
+ const p = _memgovPsV2.get(id);
712
+ if (!p) throw new Error(`memgov profile ${id} not found`);
713
+ _memgovCheckP(p.status, MEMGOV_PROFILE_MATURITY_V2.STALE);
714
+ p.status = MEMGOV_PROFILE_MATURITY_V2.STALE;
715
+ p.updatedAt = Date.now();
716
+ return { ...p, metadata: { ...p.metadata } };
717
+ }
718
+ export function archiveMemgovProfileV2(id) {
719
+ const p = _memgovPsV2.get(id);
720
+ if (!p) throw new Error(`memgov profile ${id} not found`);
721
+ _memgovCheckP(p.status, MEMGOV_PROFILE_MATURITY_V2.ARCHIVED);
722
+ const now = Date.now();
723
+ p.status = MEMGOV_PROFILE_MATURITY_V2.ARCHIVED;
724
+ p.updatedAt = now;
725
+ if (!p.archivedAt) p.archivedAt = now;
726
+ return { ...p, metadata: { ...p.metadata } };
727
+ }
728
+ export function touchMemgovProfileV2(id) {
729
+ const p = _memgovPsV2.get(id);
730
+ if (!p) throw new Error(`memgov profile ${id} not found`);
731
+ if (_memgovPTerminal.has(p.status))
732
+ throw new Error(`cannot touch terminal memgov profile ${id}`);
733
+ const now = Date.now();
734
+ p.lastTouchedAt = now;
735
+ p.updatedAt = now;
736
+ return { ...p, metadata: { ...p.metadata } };
737
+ }
738
+ export function getMemgovProfileV2(id) {
739
+ const p = _memgovPsV2.get(id);
740
+ if (!p) return null;
741
+ return { ...p, metadata: { ...p.metadata } };
742
+ }
743
+ export function listMemgovProfilesV2() {
744
+ return [..._memgovPsV2.values()].map((p) => ({
745
+ ...p,
746
+ metadata: { ...p.metadata },
747
+ }));
748
+ }
749
+ export function createMemgovRecallV2({ id, profileId, key, metadata } = {}) {
750
+ if (!id || !profileId) throw new Error("id and profileId required");
751
+ if (_memgovJsV2.has(id))
752
+ throw new Error(`memgov recall ${id} already exists`);
753
+ if (!_memgovPsV2.has(profileId))
754
+ throw new Error(`memgov profile ${profileId} not found`);
755
+ if (_memgovCountPending(profileId) >= _memgovMaxPending)
756
+ throw new Error(
757
+ `max pending memgov recalls for profile ${profileId} reached`,
758
+ );
759
+ const now = Date.now();
760
+ const j = {
761
+ id,
762
+ profileId,
763
+ key: key || "",
764
+ status: MEMGOV_RECALL_LIFECYCLE_V2.QUEUED,
765
+ createdAt: now,
766
+ updatedAt: now,
767
+ startedAt: null,
768
+ settledAt: null,
769
+ metadata: { ...(metadata || {}) },
770
+ };
771
+ _memgovJsV2.set(id, j);
772
+ return { ...j, metadata: { ...j.metadata } };
773
+ }
774
+ export function recallingMemgovRecallV2(id) {
775
+ const j = _memgovJsV2.get(id);
776
+ if (!j) throw new Error(`memgov recall ${id} not found`);
777
+ _memgovCheckJ(j.status, MEMGOV_RECALL_LIFECYCLE_V2.RECALLING);
778
+ const now = Date.now();
779
+ j.status = MEMGOV_RECALL_LIFECYCLE_V2.RECALLING;
780
+ j.updatedAt = now;
781
+ if (!j.startedAt) j.startedAt = now;
782
+ return { ...j, metadata: { ...j.metadata } };
783
+ }
784
+ export function completeRecallMemgovV2(id) {
785
+ const j = _memgovJsV2.get(id);
786
+ if (!j) throw new Error(`memgov recall ${id} not found`);
787
+ _memgovCheckJ(j.status, MEMGOV_RECALL_LIFECYCLE_V2.RECALLED);
788
+ const now = Date.now();
789
+ j.status = MEMGOV_RECALL_LIFECYCLE_V2.RECALLED;
790
+ j.updatedAt = now;
791
+ if (!j.settledAt) j.settledAt = now;
792
+ return { ...j, metadata: { ...j.metadata } };
793
+ }
794
+ export function failMemgovRecallV2(id, reason) {
795
+ const j = _memgovJsV2.get(id);
796
+ if (!j) throw new Error(`memgov recall ${id} not found`);
797
+ _memgovCheckJ(j.status, MEMGOV_RECALL_LIFECYCLE_V2.FAILED);
798
+ const now = Date.now();
799
+ j.status = MEMGOV_RECALL_LIFECYCLE_V2.FAILED;
800
+ j.updatedAt = now;
801
+ if (!j.settledAt) j.settledAt = now;
802
+ if (reason) j.metadata.failReason = String(reason);
803
+ return { ...j, metadata: { ...j.metadata } };
804
+ }
805
+ export function cancelMemgovRecallV2(id, reason) {
806
+ const j = _memgovJsV2.get(id);
807
+ if (!j) throw new Error(`memgov recall ${id} not found`);
808
+ _memgovCheckJ(j.status, MEMGOV_RECALL_LIFECYCLE_V2.CANCELLED);
809
+ const now = Date.now();
810
+ j.status = MEMGOV_RECALL_LIFECYCLE_V2.CANCELLED;
811
+ j.updatedAt = now;
812
+ if (!j.settledAt) j.settledAt = now;
813
+ if (reason) j.metadata.cancelReason = String(reason);
814
+ return { ...j, metadata: { ...j.metadata } };
815
+ }
816
+ export function getMemgovRecallV2(id) {
817
+ const j = _memgovJsV2.get(id);
818
+ if (!j) return null;
819
+ return { ...j, metadata: { ...j.metadata } };
820
+ }
821
+ export function listMemgovRecallsV2() {
822
+ return [..._memgovJsV2.values()].map((j) => ({
823
+ ...j,
824
+ metadata: { ...j.metadata },
825
+ }));
826
+ }
827
+ export function autoStaleIdleMemgovProfilesV2({ now } = {}) {
828
+ const t = now ?? Date.now();
829
+ const flipped = [];
830
+ for (const p of _memgovPsV2.values())
831
+ if (
832
+ p.status === MEMGOV_PROFILE_MATURITY_V2.ACTIVE &&
833
+ t - p.lastTouchedAt >= _memgovIdleMs
834
+ ) {
835
+ p.status = MEMGOV_PROFILE_MATURITY_V2.STALE;
836
+ p.updatedAt = t;
837
+ flipped.push(p.id);
838
+ }
839
+ return { flipped, count: flipped.length };
840
+ }
841
+ export function autoFailStuckMemgovRecallsV2({ now } = {}) {
842
+ const t = now ?? Date.now();
843
+ const flipped = [];
844
+ for (const j of _memgovJsV2.values())
845
+ if (
846
+ j.status === MEMGOV_RECALL_LIFECYCLE_V2.RECALLING &&
847
+ j.startedAt != null &&
848
+ t - j.startedAt >= _memgovStuckMs
849
+ ) {
850
+ j.status = MEMGOV_RECALL_LIFECYCLE_V2.FAILED;
851
+ j.updatedAt = t;
852
+ if (!j.settledAt) j.settledAt = t;
853
+ j.metadata.failReason = "auto-fail-stuck";
854
+ flipped.push(j.id);
855
+ }
856
+ return { flipped, count: flipped.length };
857
+ }
858
+ export function getMemoryManagerGovStatsV2() {
859
+ const profilesByStatus = {};
860
+ for (const v of Object.values(MEMGOV_PROFILE_MATURITY_V2))
861
+ profilesByStatus[v] = 0;
862
+ for (const p of _memgovPsV2.values()) profilesByStatus[p.status]++;
863
+ const recallsByStatus = {};
864
+ for (const v of Object.values(MEMGOV_RECALL_LIFECYCLE_V2))
865
+ recallsByStatus[v] = 0;
866
+ for (const j of _memgovJsV2.values()) recallsByStatus[j.status]++;
867
+ return {
868
+ totalMemgovProfilesV2: _memgovPsV2.size,
869
+ totalMemgovRecallsV2: _memgovJsV2.size,
870
+ maxActiveMemgovProfilesPerOwner: _memgovMaxActive,
871
+ maxPendingMemgovRecallsPerProfile: _memgovMaxPending,
872
+ memgovProfileIdleMs: _memgovIdleMs,
873
+ memgovRecallStuckMs: _memgovStuckMs,
874
+ profilesByStatus,
875
+ recallsByStatus,
876
+ };
877
+ }
@@ -1069,3 +1069,333 @@ export function _resetStateV2() {
1069
1069
  _sessionsV2.clear();
1070
1070
  _artifactsV2.clear();
1071
1071
  }
1072
+
1073
+ // =====================================================================
1074
+ // multimodal V2 governance overlay (iter18)
1075
+ // =====================================================================
1076
+ export const MMGOV_PROFILE_MATURITY_V2 = Object.freeze({
1077
+ PENDING: "pending",
1078
+ ACTIVE: "active",
1079
+ STALE: "stale",
1080
+ ARCHIVED: "archived",
1081
+ });
1082
+ export const MMGOV_JOB_LIFECYCLE_V2 = Object.freeze({
1083
+ QUEUED: "queued",
1084
+ PROCESSING: "processing",
1085
+ PROCESSED: "processed",
1086
+ FAILED: "failed",
1087
+ CANCELLED: "cancelled",
1088
+ });
1089
+ const _mmgovPTrans = new Map([
1090
+ [
1091
+ MMGOV_PROFILE_MATURITY_V2.PENDING,
1092
+ new Set([
1093
+ MMGOV_PROFILE_MATURITY_V2.ACTIVE,
1094
+ MMGOV_PROFILE_MATURITY_V2.ARCHIVED,
1095
+ ]),
1096
+ ],
1097
+ [
1098
+ MMGOV_PROFILE_MATURITY_V2.ACTIVE,
1099
+ new Set([
1100
+ MMGOV_PROFILE_MATURITY_V2.STALE,
1101
+ MMGOV_PROFILE_MATURITY_V2.ARCHIVED,
1102
+ ]),
1103
+ ],
1104
+ [
1105
+ MMGOV_PROFILE_MATURITY_V2.STALE,
1106
+ new Set([
1107
+ MMGOV_PROFILE_MATURITY_V2.ACTIVE,
1108
+ MMGOV_PROFILE_MATURITY_V2.ARCHIVED,
1109
+ ]),
1110
+ ],
1111
+ [MMGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
1112
+ ]);
1113
+ const _mmgovPTerminal = new Set([MMGOV_PROFILE_MATURITY_V2.ARCHIVED]);
1114
+ const _mmgovJTrans = new Map([
1115
+ [
1116
+ MMGOV_JOB_LIFECYCLE_V2.QUEUED,
1117
+ new Set([
1118
+ MMGOV_JOB_LIFECYCLE_V2.PROCESSING,
1119
+ MMGOV_JOB_LIFECYCLE_V2.CANCELLED,
1120
+ ]),
1121
+ ],
1122
+ [
1123
+ MMGOV_JOB_LIFECYCLE_V2.PROCESSING,
1124
+ new Set([
1125
+ MMGOV_JOB_LIFECYCLE_V2.PROCESSED,
1126
+ MMGOV_JOB_LIFECYCLE_V2.FAILED,
1127
+ MMGOV_JOB_LIFECYCLE_V2.CANCELLED,
1128
+ ]),
1129
+ ],
1130
+ [MMGOV_JOB_LIFECYCLE_V2.PROCESSED, new Set()],
1131
+ [MMGOV_JOB_LIFECYCLE_V2.FAILED, new Set()],
1132
+ [MMGOV_JOB_LIFECYCLE_V2.CANCELLED, new Set()],
1133
+ ]);
1134
+ const _mmgovPsV2 = new Map();
1135
+ const _mmgovJsV2 = new Map();
1136
+ let _mmgovMaxActive = 6,
1137
+ _mmgovMaxPending = 15,
1138
+ _mmgovIdleMs = 30 * 24 * 60 * 60 * 1000,
1139
+ _mmgovStuckMs = 60 * 1000;
1140
+ function _mmgovPos(n, label) {
1141
+ const v = Math.floor(Number(n));
1142
+ if (!Number.isFinite(v) || v <= 0)
1143
+ throw new Error(`${label} must be positive integer`);
1144
+ return v;
1145
+ }
1146
+ function _mmgovCheckP(from, to) {
1147
+ const a = _mmgovPTrans.get(from);
1148
+ if (!a || !a.has(to))
1149
+ throw new Error(`invalid mmgov profile transition ${from} → ${to}`);
1150
+ }
1151
+ function _mmgovCheckJ(from, to) {
1152
+ const a = _mmgovJTrans.get(from);
1153
+ if (!a || !a.has(to))
1154
+ throw new Error(`invalid mmgov job transition ${from} → ${to}`);
1155
+ }
1156
+ function _mmgovCountActive(owner) {
1157
+ let c = 0;
1158
+ for (const p of _mmgovPsV2.values())
1159
+ if (p.owner === owner && p.status === MMGOV_PROFILE_MATURITY_V2.ACTIVE) c++;
1160
+ return c;
1161
+ }
1162
+ function _mmgovCountPending(profileId) {
1163
+ let c = 0;
1164
+ for (const j of _mmgovJsV2.values())
1165
+ if (
1166
+ j.profileId === profileId &&
1167
+ (j.status === MMGOV_JOB_LIFECYCLE_V2.QUEUED ||
1168
+ j.status === MMGOV_JOB_LIFECYCLE_V2.PROCESSING)
1169
+ )
1170
+ c++;
1171
+ return c;
1172
+ }
1173
+ export function setMaxActiveMmgovProfilesPerOwnerV2(n) {
1174
+ _mmgovMaxActive = _mmgovPos(n, "maxActiveMmgovProfilesPerOwner");
1175
+ }
1176
+ export function getMaxActiveMmgovProfilesPerOwnerV2() {
1177
+ return _mmgovMaxActive;
1178
+ }
1179
+ export function setMaxPendingMmgovJobsPerProfileV2(n) {
1180
+ _mmgovMaxPending = _mmgovPos(n, "maxPendingMmgovJobsPerProfile");
1181
+ }
1182
+ export function getMaxPendingMmgovJobsPerProfileV2() {
1183
+ return _mmgovMaxPending;
1184
+ }
1185
+ export function setMmgovProfileIdleMsV2(n) {
1186
+ _mmgovIdleMs = _mmgovPos(n, "mmgovProfileIdleMs");
1187
+ }
1188
+ export function getMmgovProfileIdleMsV2() {
1189
+ return _mmgovIdleMs;
1190
+ }
1191
+ export function setMmgovJobStuckMsV2(n) {
1192
+ _mmgovStuckMs = _mmgovPos(n, "mmgovJobStuckMs");
1193
+ }
1194
+ export function getMmgovJobStuckMsV2() {
1195
+ return _mmgovStuckMs;
1196
+ }
1197
+ export function _resetStateMultimodalGovV2() {
1198
+ _mmgovPsV2.clear();
1199
+ _mmgovJsV2.clear();
1200
+ _mmgovMaxActive = 6;
1201
+ _mmgovMaxPending = 15;
1202
+ _mmgovIdleMs = 30 * 24 * 60 * 60 * 1000;
1203
+ _mmgovStuckMs = 60 * 1000;
1204
+ }
1205
+ export function registerMmgovProfileV2({ id, owner, kind, metadata } = {}) {
1206
+ if (!id || !owner) throw new Error("id and owner required");
1207
+ if (_mmgovPsV2.has(id)) throw new Error(`mmgov profile ${id} already exists`);
1208
+ const now = Date.now();
1209
+ const p = {
1210
+ id,
1211
+ owner,
1212
+ kind: kind || "text",
1213
+ status: MMGOV_PROFILE_MATURITY_V2.PENDING,
1214
+ createdAt: now,
1215
+ updatedAt: now,
1216
+ lastTouchedAt: now,
1217
+ activatedAt: null,
1218
+ archivedAt: null,
1219
+ metadata: { ...(metadata || {}) },
1220
+ };
1221
+ _mmgovPsV2.set(id, p);
1222
+ return { ...p, metadata: { ...p.metadata } };
1223
+ }
1224
+ export function activateMmgovProfileV2(id) {
1225
+ const p = _mmgovPsV2.get(id);
1226
+ if (!p) throw new Error(`mmgov profile ${id} not found`);
1227
+ const isInitial = p.status === MMGOV_PROFILE_MATURITY_V2.PENDING;
1228
+ _mmgovCheckP(p.status, MMGOV_PROFILE_MATURITY_V2.ACTIVE);
1229
+ if (isInitial && _mmgovCountActive(p.owner) >= _mmgovMaxActive)
1230
+ throw new Error(`max active mmgov profiles for owner ${p.owner} reached`);
1231
+ const now = Date.now();
1232
+ p.status = MMGOV_PROFILE_MATURITY_V2.ACTIVE;
1233
+ p.updatedAt = now;
1234
+ p.lastTouchedAt = now;
1235
+ if (!p.activatedAt) p.activatedAt = now;
1236
+ return { ...p, metadata: { ...p.metadata } };
1237
+ }
1238
+ export function staleMmgovProfileV2(id) {
1239
+ const p = _mmgovPsV2.get(id);
1240
+ if (!p) throw new Error(`mmgov profile ${id} not found`);
1241
+ _mmgovCheckP(p.status, MMGOV_PROFILE_MATURITY_V2.STALE);
1242
+ p.status = MMGOV_PROFILE_MATURITY_V2.STALE;
1243
+ p.updatedAt = Date.now();
1244
+ return { ...p, metadata: { ...p.metadata } };
1245
+ }
1246
+ export function archiveMmgovProfileV2(id) {
1247
+ const p = _mmgovPsV2.get(id);
1248
+ if (!p) throw new Error(`mmgov profile ${id} not found`);
1249
+ _mmgovCheckP(p.status, MMGOV_PROFILE_MATURITY_V2.ARCHIVED);
1250
+ const now = Date.now();
1251
+ p.status = MMGOV_PROFILE_MATURITY_V2.ARCHIVED;
1252
+ p.updatedAt = now;
1253
+ if (!p.archivedAt) p.archivedAt = now;
1254
+ return { ...p, metadata: { ...p.metadata } };
1255
+ }
1256
+ export function touchMmgovProfileV2(id) {
1257
+ const p = _mmgovPsV2.get(id);
1258
+ if (!p) throw new Error(`mmgov profile ${id} not found`);
1259
+ if (_mmgovPTerminal.has(p.status))
1260
+ throw new Error(`cannot touch terminal mmgov profile ${id}`);
1261
+ const now = Date.now();
1262
+ p.lastTouchedAt = now;
1263
+ p.updatedAt = now;
1264
+ return { ...p, metadata: { ...p.metadata } };
1265
+ }
1266
+ export function getMmgovProfileV2(id) {
1267
+ const p = _mmgovPsV2.get(id);
1268
+ if (!p) return null;
1269
+ return { ...p, metadata: { ...p.metadata } };
1270
+ }
1271
+ export function listMmgovProfilesV2() {
1272
+ return [..._mmgovPsV2.values()].map((p) => ({
1273
+ ...p,
1274
+ metadata: { ...p.metadata },
1275
+ }));
1276
+ }
1277
+ export function createMmgovJobV2({ id, profileId, input, metadata } = {}) {
1278
+ if (!id || !profileId) throw new Error("id and profileId required");
1279
+ if (_mmgovJsV2.has(id)) throw new Error(`mmgov job ${id} already exists`);
1280
+ if (!_mmgovPsV2.has(profileId))
1281
+ throw new Error(`mmgov profile ${profileId} not found`);
1282
+ if (_mmgovCountPending(profileId) >= _mmgovMaxPending)
1283
+ throw new Error(`max pending mmgov jobs for profile ${profileId} reached`);
1284
+ const now = Date.now();
1285
+ const j = {
1286
+ id,
1287
+ profileId,
1288
+ input: input || "",
1289
+ status: MMGOV_JOB_LIFECYCLE_V2.QUEUED,
1290
+ createdAt: now,
1291
+ updatedAt: now,
1292
+ startedAt: null,
1293
+ settledAt: null,
1294
+ metadata: { ...(metadata || {}) },
1295
+ };
1296
+ _mmgovJsV2.set(id, j);
1297
+ return { ...j, metadata: { ...j.metadata } };
1298
+ }
1299
+ export function processingMmgovJobV2(id) {
1300
+ const j = _mmgovJsV2.get(id);
1301
+ if (!j) throw new Error(`mmgov job ${id} not found`);
1302
+ _mmgovCheckJ(j.status, MMGOV_JOB_LIFECYCLE_V2.PROCESSING);
1303
+ const now = Date.now();
1304
+ j.status = MMGOV_JOB_LIFECYCLE_V2.PROCESSING;
1305
+ j.updatedAt = now;
1306
+ if (!j.startedAt) j.startedAt = now;
1307
+ return { ...j, metadata: { ...j.metadata } };
1308
+ }
1309
+ export function completeJobMmgovV2(id) {
1310
+ const j = _mmgovJsV2.get(id);
1311
+ if (!j) throw new Error(`mmgov job ${id} not found`);
1312
+ _mmgovCheckJ(j.status, MMGOV_JOB_LIFECYCLE_V2.PROCESSED);
1313
+ const now = Date.now();
1314
+ j.status = MMGOV_JOB_LIFECYCLE_V2.PROCESSED;
1315
+ j.updatedAt = now;
1316
+ if (!j.settledAt) j.settledAt = now;
1317
+ return { ...j, metadata: { ...j.metadata } };
1318
+ }
1319
+ export function failMmgovJobV2(id, reason) {
1320
+ const j = _mmgovJsV2.get(id);
1321
+ if (!j) throw new Error(`mmgov job ${id} not found`);
1322
+ _mmgovCheckJ(j.status, MMGOV_JOB_LIFECYCLE_V2.FAILED);
1323
+ const now = Date.now();
1324
+ j.status = MMGOV_JOB_LIFECYCLE_V2.FAILED;
1325
+ j.updatedAt = now;
1326
+ if (!j.settledAt) j.settledAt = now;
1327
+ if (reason) j.metadata.failReason = String(reason);
1328
+ return { ...j, metadata: { ...j.metadata } };
1329
+ }
1330
+ export function cancelMmgovJobV2(id, reason) {
1331
+ const j = _mmgovJsV2.get(id);
1332
+ if (!j) throw new Error(`mmgov job ${id} not found`);
1333
+ _mmgovCheckJ(j.status, MMGOV_JOB_LIFECYCLE_V2.CANCELLED);
1334
+ const now = Date.now();
1335
+ j.status = MMGOV_JOB_LIFECYCLE_V2.CANCELLED;
1336
+ j.updatedAt = now;
1337
+ if (!j.settledAt) j.settledAt = now;
1338
+ if (reason) j.metadata.cancelReason = String(reason);
1339
+ return { ...j, metadata: { ...j.metadata } };
1340
+ }
1341
+ export function getMmgovJobV2(id) {
1342
+ const j = _mmgovJsV2.get(id);
1343
+ if (!j) return null;
1344
+ return { ...j, metadata: { ...j.metadata } };
1345
+ }
1346
+ export function listMmgovJobsV2() {
1347
+ return [..._mmgovJsV2.values()].map((j) => ({
1348
+ ...j,
1349
+ metadata: { ...j.metadata },
1350
+ }));
1351
+ }
1352
+ export function autoStaleIdleMmgovProfilesV2({ now } = {}) {
1353
+ const t = now ?? Date.now();
1354
+ const flipped = [];
1355
+ for (const p of _mmgovPsV2.values())
1356
+ if (
1357
+ p.status === MMGOV_PROFILE_MATURITY_V2.ACTIVE &&
1358
+ t - p.lastTouchedAt >= _mmgovIdleMs
1359
+ ) {
1360
+ p.status = MMGOV_PROFILE_MATURITY_V2.STALE;
1361
+ p.updatedAt = t;
1362
+ flipped.push(p.id);
1363
+ }
1364
+ return { flipped, count: flipped.length };
1365
+ }
1366
+ export function autoFailStuckMmgovJobsV2({ now } = {}) {
1367
+ const t = now ?? Date.now();
1368
+ const flipped = [];
1369
+ for (const j of _mmgovJsV2.values())
1370
+ if (
1371
+ j.status === MMGOV_JOB_LIFECYCLE_V2.PROCESSING &&
1372
+ j.startedAt != null &&
1373
+ t - j.startedAt >= _mmgovStuckMs
1374
+ ) {
1375
+ j.status = MMGOV_JOB_LIFECYCLE_V2.FAILED;
1376
+ j.updatedAt = t;
1377
+ if (!j.settledAt) j.settledAt = t;
1378
+ j.metadata.failReason = "auto-fail-stuck";
1379
+ flipped.push(j.id);
1380
+ }
1381
+ return { flipped, count: flipped.length };
1382
+ }
1383
+ export function getMultimodalGovStatsV2() {
1384
+ const profilesByStatus = {};
1385
+ for (const v of Object.values(MMGOV_PROFILE_MATURITY_V2))
1386
+ profilesByStatus[v] = 0;
1387
+ for (const p of _mmgovPsV2.values()) profilesByStatus[p.status]++;
1388
+ const jobsByStatus = {};
1389
+ for (const v of Object.values(MMGOV_JOB_LIFECYCLE_V2)) jobsByStatus[v] = 0;
1390
+ for (const j of _mmgovJsV2.values()) jobsByStatus[j.status]++;
1391
+ return {
1392
+ totalMmgovProfilesV2: _mmgovPsV2.size,
1393
+ totalMmgovJobsV2: _mmgovJsV2.size,
1394
+ maxActiveMmgovProfilesPerOwner: _mmgovMaxActive,
1395
+ maxPendingMmgovJobsPerProfile: _mmgovMaxPending,
1396
+ mmgovProfileIdleMs: _mmgovIdleMs,
1397
+ mmgovJobStuckMs: _mmgovStuckMs,
1398
+ profilesByStatus,
1399
+ jobsByStatus,
1400
+ };
1401
+ }