chainlesschain 0.145.0 → 0.156.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 (184) hide show
  1. package/README.md +52 -3
  2. package/package.json +1 -1
  3. package/src/commands/a2a.js +201 -0
  4. package/src/commands/activitypub.js +207 -0
  5. package/src/commands/agent-network.js +217 -0
  6. package/src/commands/agent.js +1250 -0
  7. package/src/commands/automation.js +201 -0
  8. package/src/commands/bi.js +203 -0
  9. package/src/commands/browse.js +213 -0
  10. package/src/commands/chat.js +605 -0
  11. package/src/commands/cli-anything.js +426 -0
  12. package/src/commands/codegen.js +207 -0
  13. package/src/commands/collab.js +211 -0
  14. package/src/commands/compliance.js +822 -0
  15. package/src/commands/config.js +213 -0
  16. package/src/commands/cowork.js +1666 -0
  17. package/src/commands/crosschain.js +203 -0
  18. package/src/commands/dao.js +203 -0
  19. package/src/commands/dbevo.js +227 -0
  20. package/src/commands/dev.js +207 -0
  21. package/src/commands/did-v2.js +217 -0
  22. package/src/commands/did.js +221 -0
  23. package/src/commands/dlp.js +213 -0
  24. package/src/commands/economy.js +199 -0
  25. package/src/commands/encrypt.js +201 -0
  26. package/src/commands/evolution.js +199 -0
  27. package/src/commands/evomap.js +830 -0
  28. package/src/commands/export.js +213 -0
  29. package/src/commands/federation.js +209 -0
  30. package/src/commands/fusion.js +205 -0
  31. package/src/commands/governance.js +209 -0
  32. package/src/commands/hmemory.js +203 -0
  33. package/src/commands/hook.js +209 -0
  34. package/src/commands/import.js +209 -0
  35. package/src/commands/inference.js +207 -0
  36. package/src/commands/infra.js +203 -0
  37. package/src/commands/instinct.js +209 -0
  38. package/src/commands/ipfs.js +207 -0
  39. package/src/commands/kg.js +195 -0
  40. package/src/commands/llm.js +426 -0
  41. package/src/commands/matrix.js +207 -0
  42. package/src/commands/mcp.js +217 -0
  43. package/src/commands/memory.js +412 -0
  44. package/src/commands/multimodal.js +203 -0
  45. package/src/commands/nlprog.js +225 -0
  46. package/src/commands/nostr.js +209 -0
  47. package/src/commands/note.js +205 -0
  48. package/src/commands/ops.js +219 -0
  49. package/src/commands/orchestrate.js +406 -0
  50. package/src/commands/org.js +209 -0
  51. package/src/commands/p2p.js +209 -0
  52. package/src/commands/perception.js +209 -0
  53. package/src/commands/permmem.js +203 -0
  54. package/src/commands/pipeline.js +199 -0
  55. package/src/commands/planmode.js +426 -0
  56. package/src/commands/plugin-ecosystem.js +209 -0
  57. package/src/commands/plugin.js +209 -0
  58. package/src/commands/pqc.js +213 -0
  59. package/src/commands/quantization.js +207 -0
  60. package/src/commands/rcache.js +205 -0
  61. package/src/commands/recommend.js +233 -0
  62. package/src/commands/runtime.js +205 -0
  63. package/src/commands/scim.js +209 -0
  64. package/src/commands/services.js +207 -0
  65. package/src/commands/session.js +209 -0
  66. package/src/commands/setup.js +205 -0
  67. package/src/commands/skill.js +414 -0
  68. package/src/commands/social.js +201 -0
  69. package/src/commands/sso.js +209 -0
  70. package/src/commands/start.js +209 -0
  71. package/src/commands/stream.js +213 -0
  72. package/src/commands/sync.js +209 -0
  73. package/src/commands/tech.js +209 -0
  74. package/src/commands/tenant.js +217 -0
  75. package/src/commands/tokens.js +209 -0
  76. package/src/commands/trust.js +217 -0
  77. package/src/commands/ui.js +225 -0
  78. package/src/commands/wallet.js +209 -0
  79. package/src/commands/workflow.js +412 -0
  80. package/src/index.js +252 -0
  81. package/src/lib/a2a-protocol.js +332 -0
  82. package/src/lib/activitypub-bridge.js +334 -0
  83. package/src/lib/agent-coordinator.js +334 -0
  84. package/src/lib/agent-economy.js +334 -0
  85. package/src/lib/agent-network.js +341 -0
  86. package/src/lib/agent-router.js +333 -0
  87. package/src/lib/aiops.js +346 -0
  88. package/src/lib/automation-engine.js +335 -0
  89. package/src/lib/autonomous-agent.js +332 -0
  90. package/src/lib/autonomous-developer.js +332 -0
  91. package/src/lib/bi-engine.js +333 -0
  92. package/src/lib/browser-automation.js +334 -0
  93. package/src/lib/chat-core.js +335 -0
  94. package/src/lib/cli-anything-bridge.js +341 -0
  95. package/src/lib/cli-context-engineering.js +351 -0
  96. package/src/lib/code-agent.js +339 -0
  97. package/src/lib/collaboration-governance.js +334 -0
  98. package/src/lib/community-governance.js +346 -0
  99. package/src/lib/compliance-manager.js +334 -0
  100. package/src/lib/content-recommendation.js +351 -0
  101. package/src/lib/cowork-adapter.js +336 -0
  102. package/src/lib/cowork-evomap-adapter.js +341 -0
  103. package/src/lib/cowork-mcp-tools.js +341 -0
  104. package/src/lib/cowork-observe-html.js +341 -0
  105. package/src/lib/cowork-observe.js +341 -0
  106. package/src/lib/cowork-share.js +338 -0
  107. package/src/lib/cowork-task-templates.js +342 -1
  108. package/src/lib/cowork-template-marketplace.js +340 -0
  109. package/src/lib/cross-chain.js +339 -0
  110. package/src/lib/crypto-manager.js +334 -0
  111. package/src/lib/dao-governance.js +339 -0
  112. package/src/lib/dbevo.js +351 -0
  113. package/src/lib/decentral-infra.js +330 -0
  114. package/src/lib/did-manager.js +341 -0
  115. package/src/lib/did-v2-manager.js +341 -0
  116. package/src/lib/dlp-engine.js +339 -0
  117. package/src/lib/downloader.js +334 -0
  118. package/src/lib/evolution-system.js +334 -0
  119. package/src/lib/evomap-client.js +342 -0
  120. package/src/lib/evomap-federation.js +338 -0
  121. package/src/lib/evomap-governance.js +334 -0
  122. package/src/lib/evomap-manager.js +330 -0
  123. package/src/lib/execution-backend.js +330 -0
  124. package/src/lib/federation-hardening.js +340 -0
  125. package/src/lib/hashline.js +338 -0
  126. package/src/lib/hierarchical-memory.js +334 -0
  127. package/src/lib/hook-manager.js +341 -0
  128. package/src/lib/inference-network.js +341 -0
  129. package/src/lib/instinct-manager.js +346 -0
  130. package/src/lib/interaction-adapter.js +330 -0
  131. package/src/lib/interactive-planner.js +354 -0
  132. package/src/lib/ipfs-storage.js +334 -0
  133. package/src/lib/knowledge-exporter.js +341 -0
  134. package/src/lib/knowledge-graph.js +331 -0
  135. package/src/lib/knowledge-importer.js +341 -0
  136. package/src/lib/llm-providers.js +346 -0
  137. package/src/lib/matrix-bridge.js +339 -0
  138. package/src/lib/mcp-registry.js +346 -0
  139. package/src/lib/memory-manager.js +336 -0
  140. package/src/lib/multimodal.js +330 -0
  141. package/src/lib/nl-programming.js +341 -0
  142. package/src/lib/nostr-bridge.js +336 -0
  143. package/src/lib/note-versioning.js +339 -0
  144. package/src/lib/org-manager.js +336 -0
  145. package/src/lib/p2p-manager.js +341 -0
  146. package/src/lib/perception.js +346 -0
  147. package/src/lib/permanent-memory.js +327 -0
  148. package/src/lib/pipeline-orchestrator.js +332 -0
  149. package/src/lib/plan-mode.js +336 -0
  150. package/src/lib/plugin-autodiscovery.js +334 -0
  151. package/src/lib/plugin-ecosystem.js +346 -0
  152. package/src/lib/pqc-manager.js +346 -0
  153. package/src/lib/process-manager.js +336 -0
  154. package/src/lib/protocol-fusion.js +338 -0
  155. package/src/lib/provider-options.js +346 -0
  156. package/src/lib/provider-stream.js +348 -0
  157. package/src/lib/quantization.js +337 -0
  158. package/src/lib/response-cache.js +333 -0
  159. package/src/lib/scim-manager.js +346 -0
  160. package/src/lib/service-manager.js +337 -0
  161. package/src/lib/session-core-singletons.js +341 -0
  162. package/src/lib/session-manager.js +334 -0
  163. package/src/lib/skill-loader.js +334 -0
  164. package/src/lib/skill-mcp.js +336 -0
  165. package/src/lib/social-manager.js +330 -0
  166. package/src/lib/sso-manager.js +340 -0
  167. package/src/lib/stix-parser.js +346 -0
  168. package/src/lib/sub-agent-context.js +343 -0
  169. package/src/lib/sub-agent-profiles.js +335 -0
  170. package/src/lib/sub-agent-registry.js +336 -0
  171. package/src/lib/sync-manager.js +336 -0
  172. package/src/lib/tech-learning-engine.js +341 -0
  173. package/src/lib/tenant-saas.js +341 -0
  174. package/src/lib/threat-intel.js +330 -0
  175. package/src/lib/todo-manager.js +336 -0
  176. package/src/lib/token-tracker.js +336 -0
  177. package/src/lib/trust-security.js +343 -0
  178. package/src/lib/ueba.js +340 -0
  179. package/src/lib/universal-runtime.js +330 -0
  180. package/src/lib/wallet-manager.js +336 -0
  181. package/src/lib/web-ui-server.js +348 -0
  182. package/src/lib/workflow-engine.js +330 -0
  183. package/src/lib/workflow-expr.js +346 -0
  184. package/src/lib/ws-chat-handler.js +337 -0
@@ -481,3 +481,336 @@ export function _resetStateResponseCacheV2() {
481
481
  _profileIdleMsV2 = CACHE_DEFAULT_PROFILE_IDLE_MS;
482
482
  _refreshStuckMsV2 = CACHE_DEFAULT_REFRESH_STUCK_MS;
483
483
  }
484
+
485
+ // =====================================================================
486
+ // response-cache V2 governance overlay (iter23)
487
+ // =====================================================================
488
+ export const RCGOV_PROFILE_MATURITY_V2 = Object.freeze({
489
+ PENDING: "pending",
490
+ ACTIVE: "active",
491
+ STALE: "stale",
492
+ ARCHIVED: "archived",
493
+ });
494
+ export const RCGOV_REFRESH_LIFECYCLE_V2 = Object.freeze({
495
+ QUEUED: "queued",
496
+ REFRESHING: "refreshing",
497
+ REFRESHED: "refreshed",
498
+ FAILED: "failed",
499
+ CANCELLED: "cancelled",
500
+ });
501
+ const _rcgovPTrans = new Map([
502
+ [
503
+ RCGOV_PROFILE_MATURITY_V2.PENDING,
504
+ new Set([
505
+ RCGOV_PROFILE_MATURITY_V2.ACTIVE,
506
+ RCGOV_PROFILE_MATURITY_V2.ARCHIVED,
507
+ ]),
508
+ ],
509
+ [
510
+ RCGOV_PROFILE_MATURITY_V2.ACTIVE,
511
+ new Set([
512
+ RCGOV_PROFILE_MATURITY_V2.STALE,
513
+ RCGOV_PROFILE_MATURITY_V2.ARCHIVED,
514
+ ]),
515
+ ],
516
+ [
517
+ RCGOV_PROFILE_MATURITY_V2.STALE,
518
+ new Set([
519
+ RCGOV_PROFILE_MATURITY_V2.ACTIVE,
520
+ RCGOV_PROFILE_MATURITY_V2.ARCHIVED,
521
+ ]),
522
+ ],
523
+ [RCGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
524
+ ]);
525
+ const _rcgovPTerminal = new Set([RCGOV_PROFILE_MATURITY_V2.ARCHIVED]);
526
+ const _rcgovJTrans = new Map([
527
+ [
528
+ RCGOV_REFRESH_LIFECYCLE_V2.QUEUED,
529
+ new Set([
530
+ RCGOV_REFRESH_LIFECYCLE_V2.REFRESHING,
531
+ RCGOV_REFRESH_LIFECYCLE_V2.CANCELLED,
532
+ ]),
533
+ ],
534
+ [
535
+ RCGOV_REFRESH_LIFECYCLE_V2.REFRESHING,
536
+ new Set([
537
+ RCGOV_REFRESH_LIFECYCLE_V2.REFRESHED,
538
+ RCGOV_REFRESH_LIFECYCLE_V2.FAILED,
539
+ RCGOV_REFRESH_LIFECYCLE_V2.CANCELLED,
540
+ ]),
541
+ ],
542
+ [RCGOV_REFRESH_LIFECYCLE_V2.REFRESHED, new Set()],
543
+ [RCGOV_REFRESH_LIFECYCLE_V2.FAILED, new Set()],
544
+ [RCGOV_REFRESH_LIFECYCLE_V2.CANCELLED, new Set()],
545
+ ]);
546
+ const _rcgovPsV2 = new Map();
547
+ const _rcgovJsV2 = new Map();
548
+ let _rcgovMaxActive = 8,
549
+ _rcgovMaxPending = 15,
550
+ _rcgovIdleMs = 30 * 24 * 60 * 60 * 1000,
551
+ _rcgovStuckMs = 60 * 1000;
552
+ function _rcgovPos(n, label) {
553
+ const v = Math.floor(Number(n));
554
+ if (!Number.isFinite(v) || v <= 0)
555
+ throw new Error(`${label} must be positive integer`);
556
+ return v;
557
+ }
558
+ function _rcgovCheckP(from, to) {
559
+ const a = _rcgovPTrans.get(from);
560
+ if (!a || !a.has(to))
561
+ throw new Error(`invalid rcgov profile transition ${from} → ${to}`);
562
+ }
563
+ function _rcgovCheckJ(from, to) {
564
+ const a = _rcgovJTrans.get(from);
565
+ if (!a || !a.has(to))
566
+ throw new Error(`invalid rcgov refresh transition ${from} → ${to}`);
567
+ }
568
+ function _rcgovCountActive(owner) {
569
+ let c = 0;
570
+ for (const p of _rcgovPsV2.values())
571
+ if (p.owner === owner && p.status === RCGOV_PROFILE_MATURITY_V2.ACTIVE) c++;
572
+ return c;
573
+ }
574
+ function _rcgovCountPending(profileId) {
575
+ let c = 0;
576
+ for (const j of _rcgovJsV2.values())
577
+ if (
578
+ j.profileId === profileId &&
579
+ (j.status === RCGOV_REFRESH_LIFECYCLE_V2.QUEUED ||
580
+ j.status === RCGOV_REFRESH_LIFECYCLE_V2.REFRESHING)
581
+ )
582
+ c++;
583
+ return c;
584
+ }
585
+ export function setMaxActiveRcgovProfilesPerOwnerV2(n) {
586
+ _rcgovMaxActive = _rcgovPos(n, "maxActiveRcgovProfilesPerOwner");
587
+ }
588
+ export function getMaxActiveRcgovProfilesPerOwnerV2() {
589
+ return _rcgovMaxActive;
590
+ }
591
+ export function setMaxPendingRcgovRefreshsPerProfileV2(n) {
592
+ _rcgovMaxPending = _rcgovPos(n, "maxPendingRcgovRefreshsPerProfile");
593
+ }
594
+ export function getMaxPendingRcgovRefreshsPerProfileV2() {
595
+ return _rcgovMaxPending;
596
+ }
597
+ export function setRcgovProfileIdleMsV2(n) {
598
+ _rcgovIdleMs = _rcgovPos(n, "rcgovProfileIdleMs");
599
+ }
600
+ export function getRcgovProfileIdleMsV2() {
601
+ return _rcgovIdleMs;
602
+ }
603
+ export function setRcgovRefreshStuckMsV2(n) {
604
+ _rcgovStuckMs = _rcgovPos(n, "rcgovRefreshStuckMs");
605
+ }
606
+ export function getRcgovRefreshStuckMsV2() {
607
+ return _rcgovStuckMs;
608
+ }
609
+ export function _resetStateResponseCacheGovV2() {
610
+ _rcgovPsV2.clear();
611
+ _rcgovJsV2.clear();
612
+ _rcgovMaxActive = 8;
613
+ _rcgovMaxPending = 15;
614
+ _rcgovIdleMs = 30 * 24 * 60 * 60 * 1000;
615
+ _rcgovStuckMs = 60 * 1000;
616
+ }
617
+ export function registerRcgovProfileV2({ id, owner, lane, metadata } = {}) {
618
+ if (!id || !owner) throw new Error("id and owner required");
619
+ if (_rcgovPsV2.has(id)) throw new Error(`rcgov profile ${id} already exists`);
620
+ const now = Date.now();
621
+ const p = {
622
+ id,
623
+ owner,
624
+ lane: lane || "default",
625
+ status: RCGOV_PROFILE_MATURITY_V2.PENDING,
626
+ createdAt: now,
627
+ updatedAt: now,
628
+ lastTouchedAt: now,
629
+ activatedAt: null,
630
+ archivedAt: null,
631
+ metadata: { ...(metadata || {}) },
632
+ };
633
+ _rcgovPsV2.set(id, p);
634
+ return { ...p, metadata: { ...p.metadata } };
635
+ }
636
+ export function activateRcgovProfileV2(id) {
637
+ const p = _rcgovPsV2.get(id);
638
+ if (!p) throw new Error(`rcgov profile ${id} not found`);
639
+ const isInitial = p.status === RCGOV_PROFILE_MATURITY_V2.PENDING;
640
+ _rcgovCheckP(p.status, RCGOV_PROFILE_MATURITY_V2.ACTIVE);
641
+ if (isInitial && _rcgovCountActive(p.owner) >= _rcgovMaxActive)
642
+ throw new Error(`max active rcgov profiles for owner ${p.owner} reached`);
643
+ const now = Date.now();
644
+ p.status = RCGOV_PROFILE_MATURITY_V2.ACTIVE;
645
+ p.updatedAt = now;
646
+ p.lastTouchedAt = now;
647
+ if (!p.activatedAt) p.activatedAt = now;
648
+ return { ...p, metadata: { ...p.metadata } };
649
+ }
650
+ export function staleRcgovProfileV2(id) {
651
+ const p = _rcgovPsV2.get(id);
652
+ if (!p) throw new Error(`rcgov profile ${id} not found`);
653
+ _rcgovCheckP(p.status, RCGOV_PROFILE_MATURITY_V2.STALE);
654
+ p.status = RCGOV_PROFILE_MATURITY_V2.STALE;
655
+ p.updatedAt = Date.now();
656
+ return { ...p, metadata: { ...p.metadata } };
657
+ }
658
+ export function archiveRcgovProfileV2(id) {
659
+ const p = _rcgovPsV2.get(id);
660
+ if (!p) throw new Error(`rcgov profile ${id} not found`);
661
+ _rcgovCheckP(p.status, RCGOV_PROFILE_MATURITY_V2.ARCHIVED);
662
+ const now = Date.now();
663
+ p.status = RCGOV_PROFILE_MATURITY_V2.ARCHIVED;
664
+ p.updatedAt = now;
665
+ if (!p.archivedAt) p.archivedAt = now;
666
+ return { ...p, metadata: { ...p.metadata } };
667
+ }
668
+ export function touchRcgovProfileV2(id) {
669
+ const p = _rcgovPsV2.get(id);
670
+ if (!p) throw new Error(`rcgov profile ${id} not found`);
671
+ if (_rcgovPTerminal.has(p.status))
672
+ throw new Error(`cannot touch terminal rcgov profile ${id}`);
673
+ const now = Date.now();
674
+ p.lastTouchedAt = now;
675
+ p.updatedAt = now;
676
+ return { ...p, metadata: { ...p.metadata } };
677
+ }
678
+ export function getRcgovProfileV2(id) {
679
+ const p = _rcgovPsV2.get(id);
680
+ if (!p) return null;
681
+ return { ...p, metadata: { ...p.metadata } };
682
+ }
683
+ export function listRcgovProfilesV2() {
684
+ return [..._rcgovPsV2.values()].map((p) => ({
685
+ ...p,
686
+ metadata: { ...p.metadata },
687
+ }));
688
+ }
689
+ export function createRcgovRefreshV2({ id, profileId, source, metadata } = {}) {
690
+ if (!id || !profileId) throw new Error("id and profileId required");
691
+ if (_rcgovJsV2.has(id)) throw new Error(`rcgov refresh ${id} already exists`);
692
+ if (!_rcgovPsV2.has(profileId))
693
+ throw new Error(`rcgov profile ${profileId} not found`);
694
+ if (_rcgovCountPending(profileId) >= _rcgovMaxPending)
695
+ throw new Error(
696
+ `max pending rcgov refreshs for profile ${profileId} reached`,
697
+ );
698
+ const now = Date.now();
699
+ const j = {
700
+ id,
701
+ profileId,
702
+ source: source || "",
703
+ status: RCGOV_REFRESH_LIFECYCLE_V2.QUEUED,
704
+ createdAt: now,
705
+ updatedAt: now,
706
+ startedAt: null,
707
+ settledAt: null,
708
+ metadata: { ...(metadata || {}) },
709
+ };
710
+ _rcgovJsV2.set(id, j);
711
+ return { ...j, metadata: { ...j.metadata } };
712
+ }
713
+ export function refreshingRcgovRefreshV2(id) {
714
+ const j = _rcgovJsV2.get(id);
715
+ if (!j) throw new Error(`rcgov refresh ${id} not found`);
716
+ _rcgovCheckJ(j.status, RCGOV_REFRESH_LIFECYCLE_V2.REFRESHING);
717
+ const now = Date.now();
718
+ j.status = RCGOV_REFRESH_LIFECYCLE_V2.REFRESHING;
719
+ j.updatedAt = now;
720
+ if (!j.startedAt) j.startedAt = now;
721
+ return { ...j, metadata: { ...j.metadata } };
722
+ }
723
+ export function completeRefreshRcgovV2(id) {
724
+ const j = _rcgovJsV2.get(id);
725
+ if (!j) throw new Error(`rcgov refresh ${id} not found`);
726
+ _rcgovCheckJ(j.status, RCGOV_REFRESH_LIFECYCLE_V2.REFRESHED);
727
+ const now = Date.now();
728
+ j.status = RCGOV_REFRESH_LIFECYCLE_V2.REFRESHED;
729
+ j.updatedAt = now;
730
+ if (!j.settledAt) j.settledAt = now;
731
+ return { ...j, metadata: { ...j.metadata } };
732
+ }
733
+ export function failRcgovRefreshV2(id, reason) {
734
+ const j = _rcgovJsV2.get(id);
735
+ if (!j) throw new Error(`rcgov refresh ${id} not found`);
736
+ _rcgovCheckJ(j.status, RCGOV_REFRESH_LIFECYCLE_V2.FAILED);
737
+ const now = Date.now();
738
+ j.status = RCGOV_REFRESH_LIFECYCLE_V2.FAILED;
739
+ j.updatedAt = now;
740
+ if (!j.settledAt) j.settledAt = now;
741
+ if (reason) j.metadata.failReason = String(reason);
742
+ return { ...j, metadata: { ...j.metadata } };
743
+ }
744
+ export function cancelRcgovRefreshV2(id, reason) {
745
+ const j = _rcgovJsV2.get(id);
746
+ if (!j) throw new Error(`rcgov refresh ${id} not found`);
747
+ _rcgovCheckJ(j.status, RCGOV_REFRESH_LIFECYCLE_V2.CANCELLED);
748
+ const now = Date.now();
749
+ j.status = RCGOV_REFRESH_LIFECYCLE_V2.CANCELLED;
750
+ j.updatedAt = now;
751
+ if (!j.settledAt) j.settledAt = now;
752
+ if (reason) j.metadata.cancelReason = String(reason);
753
+ return { ...j, metadata: { ...j.metadata } };
754
+ }
755
+ export function getRcgovRefreshV2(id) {
756
+ const j = _rcgovJsV2.get(id);
757
+ if (!j) return null;
758
+ return { ...j, metadata: { ...j.metadata } };
759
+ }
760
+ export function listRcgovRefreshsV2() {
761
+ return [..._rcgovJsV2.values()].map((j) => ({
762
+ ...j,
763
+ metadata: { ...j.metadata },
764
+ }));
765
+ }
766
+ export function autoStaleIdleRcgovProfilesV2({ now } = {}) {
767
+ const t = now ?? Date.now();
768
+ const flipped = [];
769
+ for (const p of _rcgovPsV2.values())
770
+ if (
771
+ p.status === RCGOV_PROFILE_MATURITY_V2.ACTIVE &&
772
+ t - p.lastTouchedAt >= _rcgovIdleMs
773
+ ) {
774
+ p.status = RCGOV_PROFILE_MATURITY_V2.STALE;
775
+ p.updatedAt = t;
776
+ flipped.push(p.id);
777
+ }
778
+ return { flipped, count: flipped.length };
779
+ }
780
+ export function autoFailStuckRcgovRefreshsV2({ now } = {}) {
781
+ const t = now ?? Date.now();
782
+ const flipped = [];
783
+ for (const j of _rcgovJsV2.values())
784
+ if (
785
+ j.status === RCGOV_REFRESH_LIFECYCLE_V2.REFRESHING &&
786
+ j.startedAt != null &&
787
+ t - j.startedAt >= _rcgovStuckMs
788
+ ) {
789
+ j.status = RCGOV_REFRESH_LIFECYCLE_V2.FAILED;
790
+ j.updatedAt = t;
791
+ if (!j.settledAt) j.settledAt = t;
792
+ j.metadata.failReason = "auto-fail-stuck";
793
+ flipped.push(j.id);
794
+ }
795
+ return { flipped, count: flipped.length };
796
+ }
797
+ export function getResponseCacheGovStatsV2() {
798
+ const profilesByStatus = {};
799
+ for (const v of Object.values(RCGOV_PROFILE_MATURITY_V2))
800
+ profilesByStatus[v] = 0;
801
+ for (const p of _rcgovPsV2.values()) profilesByStatus[p.status]++;
802
+ const refreshsByStatus = {};
803
+ for (const v of Object.values(RCGOV_REFRESH_LIFECYCLE_V2))
804
+ refreshsByStatus[v] = 0;
805
+ for (const j of _rcgovJsV2.values()) refreshsByStatus[j.status]++;
806
+ return {
807
+ totalRcgovProfilesV2: _rcgovPsV2.size,
808
+ totalRcgovRefreshsV2: _rcgovJsV2.size,
809
+ maxActiveRcgovProfilesPerOwner: _rcgovMaxActive,
810
+ maxPendingRcgovRefreshsPerProfile: _rcgovMaxPending,
811
+ rcgovProfileIdleMs: _rcgovIdleMs,
812
+ rcgovRefreshStuckMs: _rcgovStuckMs,
813
+ profilesByStatus,
814
+ refreshsByStatus,
815
+ };
816
+ }
@@ -539,3 +539,349 @@ export function _resetStateScimManagerV2() {
539
539
  _identityIdleMsV2 = SCIM_DEFAULT_IDENTITY_IDLE_MS;
540
540
  _syncStuckMsV2 = SCIM_DEFAULT_SYNC_STUCK_MS;
541
541
  }
542
+
543
+ // =====================================================================
544
+ // scim-manager V2 governance overlay (iter19)
545
+ // =====================================================================
546
+ export const SCIMGOV_PROFILE_MATURITY_V2 = Object.freeze({
547
+ PENDING: "pending",
548
+ ACTIVE: "active",
549
+ STALE: "stale",
550
+ ARCHIVED: "archived",
551
+ });
552
+ export const SCIMGOV_SYNC_LIFECYCLE_V2 = Object.freeze({
553
+ QUEUED: "queued",
554
+ SYNCING: "syncing",
555
+ SYNCED: "synced",
556
+ FAILED: "failed",
557
+ CANCELLED: "cancelled",
558
+ });
559
+ const _scimgovPTrans = new Map([
560
+ [
561
+ SCIMGOV_PROFILE_MATURITY_V2.PENDING,
562
+ new Set([
563
+ SCIMGOV_PROFILE_MATURITY_V2.ACTIVE,
564
+ SCIMGOV_PROFILE_MATURITY_V2.ARCHIVED,
565
+ ]),
566
+ ],
567
+ [
568
+ SCIMGOV_PROFILE_MATURITY_V2.ACTIVE,
569
+ new Set([
570
+ SCIMGOV_PROFILE_MATURITY_V2.STALE,
571
+ SCIMGOV_PROFILE_MATURITY_V2.ARCHIVED,
572
+ ]),
573
+ ],
574
+ [
575
+ SCIMGOV_PROFILE_MATURITY_V2.STALE,
576
+ new Set([
577
+ SCIMGOV_PROFILE_MATURITY_V2.ACTIVE,
578
+ SCIMGOV_PROFILE_MATURITY_V2.ARCHIVED,
579
+ ]),
580
+ ],
581
+ [SCIMGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
582
+ ]);
583
+ const _scimgovPTerminal = new Set([SCIMGOV_PROFILE_MATURITY_V2.ARCHIVED]);
584
+ const _scimgovJTrans = new Map([
585
+ [
586
+ SCIMGOV_SYNC_LIFECYCLE_V2.QUEUED,
587
+ new Set([
588
+ SCIMGOV_SYNC_LIFECYCLE_V2.SYNCING,
589
+ SCIMGOV_SYNC_LIFECYCLE_V2.CANCELLED,
590
+ ]),
591
+ ],
592
+ [
593
+ SCIMGOV_SYNC_LIFECYCLE_V2.SYNCING,
594
+ new Set([
595
+ SCIMGOV_SYNC_LIFECYCLE_V2.SYNCED,
596
+ SCIMGOV_SYNC_LIFECYCLE_V2.FAILED,
597
+ SCIMGOV_SYNC_LIFECYCLE_V2.CANCELLED,
598
+ ]),
599
+ ],
600
+ [SCIMGOV_SYNC_LIFECYCLE_V2.SYNCED, new Set()],
601
+ [SCIMGOV_SYNC_LIFECYCLE_V2.FAILED, new Set()],
602
+ [SCIMGOV_SYNC_LIFECYCLE_V2.CANCELLED, new Set()],
603
+ ]);
604
+ const _scimgovPsV2 = new Map();
605
+ const _scimgovJsV2 = new Map();
606
+ let _scimgovMaxActive = 6,
607
+ _scimgovMaxPending = 15,
608
+ _scimgovIdleMs = 30 * 24 * 60 * 60 * 1000,
609
+ _scimgovStuckMs = 60 * 1000;
610
+ function _scimgovPos(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 _scimgovCheckP(from, to) {
617
+ const a = _scimgovPTrans.get(from);
618
+ if (!a || !a.has(to))
619
+ throw new Error(`invalid scimgov profile transition ${from} → ${to}`);
620
+ }
621
+ function _scimgovCheckJ(from, to) {
622
+ const a = _scimgovJTrans.get(from);
623
+ if (!a || !a.has(to))
624
+ throw new Error(`invalid scimgov sync transition ${from} → ${to}`);
625
+ }
626
+ function _scimgovCountActive(owner) {
627
+ let c = 0;
628
+ for (const p of _scimgovPsV2.values())
629
+ if (p.owner === owner && p.status === SCIMGOV_PROFILE_MATURITY_V2.ACTIVE)
630
+ c++;
631
+ return c;
632
+ }
633
+ function _scimgovCountPending(profileId) {
634
+ let c = 0;
635
+ for (const j of _scimgovJsV2.values())
636
+ if (
637
+ j.profileId === profileId &&
638
+ (j.status === SCIMGOV_SYNC_LIFECYCLE_V2.QUEUED ||
639
+ j.status === SCIMGOV_SYNC_LIFECYCLE_V2.SYNCING)
640
+ )
641
+ c++;
642
+ return c;
643
+ }
644
+ export function setMaxActiveScimgovProfilesPerOwnerV2(n) {
645
+ _scimgovMaxActive = _scimgovPos(n, "maxActiveScimgovProfilesPerOwner");
646
+ }
647
+ export function getMaxActiveScimgovProfilesPerOwnerV2() {
648
+ return _scimgovMaxActive;
649
+ }
650
+ export function setMaxPendingScimgovSyncsPerProfileV2(n) {
651
+ _scimgovMaxPending = _scimgovPos(n, "maxPendingScimgovSyncsPerProfile");
652
+ }
653
+ export function getMaxPendingScimgovSyncsPerProfileV2() {
654
+ return _scimgovMaxPending;
655
+ }
656
+ export function setScimgovProfileIdleMsV2(n) {
657
+ _scimgovIdleMs = _scimgovPos(n, "scimgovProfileIdleMs");
658
+ }
659
+ export function getScimgovProfileIdleMsV2() {
660
+ return _scimgovIdleMs;
661
+ }
662
+ export function setScimgovSyncStuckMsV2(n) {
663
+ _scimgovStuckMs = _scimgovPos(n, "scimgovSyncStuckMs");
664
+ }
665
+ export function getScimgovSyncStuckMsV2() {
666
+ return _scimgovStuckMs;
667
+ }
668
+ export function _resetStateScimManagerGovV2() {
669
+ _scimgovPsV2.clear();
670
+ _scimgovJsV2.clear();
671
+ _scimgovMaxActive = 6;
672
+ _scimgovMaxPending = 15;
673
+ _scimgovIdleMs = 30 * 24 * 60 * 60 * 1000;
674
+ _scimgovStuckMs = 60 * 1000;
675
+ }
676
+ export function registerScimgovProfileV2({
677
+ id,
678
+ owner,
679
+ resource,
680
+ metadata,
681
+ } = {}) {
682
+ if (!id || !owner) throw new Error("id and owner required");
683
+ if (_scimgovPsV2.has(id))
684
+ throw new Error(`scimgov profile ${id} already exists`);
685
+ const now = Date.now();
686
+ const p = {
687
+ id,
688
+ owner,
689
+ resource: resource || "users",
690
+ status: SCIMGOV_PROFILE_MATURITY_V2.PENDING,
691
+ createdAt: now,
692
+ updatedAt: now,
693
+ lastTouchedAt: now,
694
+ activatedAt: null,
695
+ archivedAt: null,
696
+ metadata: { ...(metadata || {}) },
697
+ };
698
+ _scimgovPsV2.set(id, p);
699
+ return { ...p, metadata: { ...p.metadata } };
700
+ }
701
+ export function activateScimgovProfileV2(id) {
702
+ const p = _scimgovPsV2.get(id);
703
+ if (!p) throw new Error(`scimgov profile ${id} not found`);
704
+ const isInitial = p.status === SCIMGOV_PROFILE_MATURITY_V2.PENDING;
705
+ _scimgovCheckP(p.status, SCIMGOV_PROFILE_MATURITY_V2.ACTIVE);
706
+ if (isInitial && _scimgovCountActive(p.owner) >= _scimgovMaxActive)
707
+ throw new Error(`max active scimgov profiles for owner ${p.owner} reached`);
708
+ const now = Date.now();
709
+ p.status = SCIMGOV_PROFILE_MATURITY_V2.ACTIVE;
710
+ p.updatedAt = now;
711
+ p.lastTouchedAt = now;
712
+ if (!p.activatedAt) p.activatedAt = now;
713
+ return { ...p, metadata: { ...p.metadata } };
714
+ }
715
+ export function staleScimgovProfileV2(id) {
716
+ const p = _scimgovPsV2.get(id);
717
+ if (!p) throw new Error(`scimgov profile ${id} not found`);
718
+ _scimgovCheckP(p.status, SCIMGOV_PROFILE_MATURITY_V2.STALE);
719
+ p.status = SCIMGOV_PROFILE_MATURITY_V2.STALE;
720
+ p.updatedAt = Date.now();
721
+ return { ...p, metadata: { ...p.metadata } };
722
+ }
723
+ export function archiveScimgovProfileV2(id) {
724
+ const p = _scimgovPsV2.get(id);
725
+ if (!p) throw new Error(`scimgov profile ${id} not found`);
726
+ _scimgovCheckP(p.status, SCIMGOV_PROFILE_MATURITY_V2.ARCHIVED);
727
+ const now = Date.now();
728
+ p.status = SCIMGOV_PROFILE_MATURITY_V2.ARCHIVED;
729
+ p.updatedAt = now;
730
+ if (!p.archivedAt) p.archivedAt = now;
731
+ return { ...p, metadata: { ...p.metadata } };
732
+ }
733
+ export function touchScimgovProfileV2(id) {
734
+ const p = _scimgovPsV2.get(id);
735
+ if (!p) throw new Error(`scimgov profile ${id} not found`);
736
+ if (_scimgovPTerminal.has(p.status))
737
+ throw new Error(`cannot touch terminal scimgov profile ${id}`);
738
+ const now = Date.now();
739
+ p.lastTouchedAt = now;
740
+ p.updatedAt = now;
741
+ return { ...p, metadata: { ...p.metadata } };
742
+ }
743
+ export function getScimgovProfileV2(id) {
744
+ const p = _scimgovPsV2.get(id);
745
+ if (!p) return null;
746
+ return { ...p, metadata: { ...p.metadata } };
747
+ }
748
+ export function listScimgovProfilesV2() {
749
+ return [..._scimgovPsV2.values()].map((p) => ({
750
+ ...p,
751
+ metadata: { ...p.metadata },
752
+ }));
753
+ }
754
+ export function createScimgovSyncV2({
755
+ id,
756
+ profileId,
757
+ endpoint,
758
+ metadata,
759
+ } = {}) {
760
+ if (!id || !profileId) throw new Error("id and profileId required");
761
+ if (_scimgovJsV2.has(id))
762
+ throw new Error(`scimgov sync ${id} already exists`);
763
+ if (!_scimgovPsV2.has(profileId))
764
+ throw new Error(`scimgov profile ${profileId} not found`);
765
+ if (_scimgovCountPending(profileId) >= _scimgovMaxPending)
766
+ throw new Error(
767
+ `max pending scimgov syncs for profile ${profileId} reached`,
768
+ );
769
+ const now = Date.now();
770
+ const j = {
771
+ id,
772
+ profileId,
773
+ endpoint: endpoint || "",
774
+ status: SCIMGOV_SYNC_LIFECYCLE_V2.QUEUED,
775
+ createdAt: now,
776
+ updatedAt: now,
777
+ startedAt: null,
778
+ settledAt: null,
779
+ metadata: { ...(metadata || {}) },
780
+ };
781
+ _scimgovJsV2.set(id, j);
782
+ return { ...j, metadata: { ...j.metadata } };
783
+ }
784
+ export function syncingScimgovSyncV2(id) {
785
+ const j = _scimgovJsV2.get(id);
786
+ if (!j) throw new Error(`scimgov sync ${id} not found`);
787
+ _scimgovCheckJ(j.status, SCIMGOV_SYNC_LIFECYCLE_V2.SYNCING);
788
+ const now = Date.now();
789
+ j.status = SCIMGOV_SYNC_LIFECYCLE_V2.SYNCING;
790
+ j.updatedAt = now;
791
+ if (!j.startedAt) j.startedAt = now;
792
+ return { ...j, metadata: { ...j.metadata } };
793
+ }
794
+ export function completeSyncScimgovV2(id) {
795
+ const j = _scimgovJsV2.get(id);
796
+ if (!j) throw new Error(`scimgov sync ${id} not found`);
797
+ _scimgovCheckJ(j.status, SCIMGOV_SYNC_LIFECYCLE_V2.SYNCED);
798
+ const now = Date.now();
799
+ j.status = SCIMGOV_SYNC_LIFECYCLE_V2.SYNCED;
800
+ j.updatedAt = now;
801
+ if (!j.settledAt) j.settledAt = now;
802
+ return { ...j, metadata: { ...j.metadata } };
803
+ }
804
+ export function failScimgovSyncV2(id, reason) {
805
+ const j = _scimgovJsV2.get(id);
806
+ if (!j) throw new Error(`scimgov sync ${id} not found`);
807
+ _scimgovCheckJ(j.status, SCIMGOV_SYNC_LIFECYCLE_V2.FAILED);
808
+ const now = Date.now();
809
+ j.status = SCIMGOV_SYNC_LIFECYCLE_V2.FAILED;
810
+ j.updatedAt = now;
811
+ if (!j.settledAt) j.settledAt = now;
812
+ if (reason) j.metadata.failReason = String(reason);
813
+ return { ...j, metadata: { ...j.metadata } };
814
+ }
815
+ export function cancelScimgovSyncV2(id, reason) {
816
+ const j = _scimgovJsV2.get(id);
817
+ if (!j) throw new Error(`scimgov sync ${id} not found`);
818
+ _scimgovCheckJ(j.status, SCIMGOV_SYNC_LIFECYCLE_V2.CANCELLED);
819
+ const now = Date.now();
820
+ j.status = SCIMGOV_SYNC_LIFECYCLE_V2.CANCELLED;
821
+ j.updatedAt = now;
822
+ if (!j.settledAt) j.settledAt = now;
823
+ if (reason) j.metadata.cancelReason = String(reason);
824
+ return { ...j, metadata: { ...j.metadata } };
825
+ }
826
+ export function getScimgovSyncV2(id) {
827
+ const j = _scimgovJsV2.get(id);
828
+ if (!j) return null;
829
+ return { ...j, metadata: { ...j.metadata } };
830
+ }
831
+ export function listScimgovSyncsV2() {
832
+ return [..._scimgovJsV2.values()].map((j) => ({
833
+ ...j,
834
+ metadata: { ...j.metadata },
835
+ }));
836
+ }
837
+ export function autoStaleIdleScimgovProfilesV2({ now } = {}) {
838
+ const t = now ?? Date.now();
839
+ const flipped = [];
840
+ for (const p of _scimgovPsV2.values())
841
+ if (
842
+ p.status === SCIMGOV_PROFILE_MATURITY_V2.ACTIVE &&
843
+ t - p.lastTouchedAt >= _scimgovIdleMs
844
+ ) {
845
+ p.status = SCIMGOV_PROFILE_MATURITY_V2.STALE;
846
+ p.updatedAt = t;
847
+ flipped.push(p.id);
848
+ }
849
+ return { flipped, count: flipped.length };
850
+ }
851
+ export function autoFailStuckScimgovSyncsV2({ now } = {}) {
852
+ const t = now ?? Date.now();
853
+ const flipped = [];
854
+ for (const j of _scimgovJsV2.values())
855
+ if (
856
+ j.status === SCIMGOV_SYNC_LIFECYCLE_V2.SYNCING &&
857
+ j.startedAt != null &&
858
+ t - j.startedAt >= _scimgovStuckMs
859
+ ) {
860
+ j.status = SCIMGOV_SYNC_LIFECYCLE_V2.FAILED;
861
+ j.updatedAt = t;
862
+ if (!j.settledAt) j.settledAt = t;
863
+ j.metadata.failReason = "auto-fail-stuck";
864
+ flipped.push(j.id);
865
+ }
866
+ return { flipped, count: flipped.length };
867
+ }
868
+ export function getScimManagerGovStatsV2() {
869
+ const profilesByStatus = {};
870
+ for (const v of Object.values(SCIMGOV_PROFILE_MATURITY_V2))
871
+ profilesByStatus[v] = 0;
872
+ for (const p of _scimgovPsV2.values()) profilesByStatus[p.status]++;
873
+ const syncsByStatus = {};
874
+ for (const v of Object.values(SCIMGOV_SYNC_LIFECYCLE_V2))
875
+ syncsByStatus[v] = 0;
876
+ for (const j of _scimgovJsV2.values()) syncsByStatus[j.status]++;
877
+ return {
878
+ totalScimgovProfilesV2: _scimgovPsV2.size,
879
+ totalScimgovSyncsV2: _scimgovJsV2.size,
880
+ maxActiveScimgovProfilesPerOwner: _scimgovMaxActive,
881
+ maxPendingScimgovSyncsPerProfile: _scimgovMaxPending,
882
+ scimgovProfileIdleMs: _scimgovIdleMs,
883
+ scimgovSyncStuckMs: _scimgovStuckMs,
884
+ profilesByStatus,
885
+ syncsByStatus,
886
+ };
887
+ }