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
@@ -622,25 +622,53 @@ export function _resetState() {
622
622
 
623
623
  export const _constants = { PUBLIC_AUDIENCE, CONTEXT, DEFAULT_ORIGIN };
624
624
 
625
-
626
625
  // ===== V2 Surface: ActivityPub Bridge governance overlay (CLI v0.135.0) =====
627
626
  export const AP_ACTOR_MATURITY_V2 = Object.freeze({
628
- PENDING: "pending", ACTIVE: "active", SUSPENDED: "suspended", DEACTIVATED: "deactivated",
627
+ PENDING: "pending",
628
+ ACTIVE: "active",
629
+ SUSPENDED: "suspended",
630
+ DEACTIVATED: "deactivated",
629
631
  });
630
632
  export const AP_ACTIVITY_LIFECYCLE_V2 = Object.freeze({
631
- QUEUED: "queued", DELIVERING: "delivering", DELIVERED: "delivered", FAILED: "failed", CANCELLED: "cancelled",
633
+ QUEUED: "queued",
634
+ DELIVERING: "delivering",
635
+ DELIVERED: "delivered",
636
+ FAILED: "failed",
637
+ CANCELLED: "cancelled",
632
638
  });
633
639
 
634
640
  const _apActorTrans = new Map([
635
- [AP_ACTOR_MATURITY_V2.PENDING, new Set([AP_ACTOR_MATURITY_V2.ACTIVE, AP_ACTOR_MATURITY_V2.DEACTIVATED])],
636
- [AP_ACTOR_MATURITY_V2.ACTIVE, new Set([AP_ACTOR_MATURITY_V2.SUSPENDED, AP_ACTOR_MATURITY_V2.DEACTIVATED])],
637
- [AP_ACTOR_MATURITY_V2.SUSPENDED, new Set([AP_ACTOR_MATURITY_V2.ACTIVE, AP_ACTOR_MATURITY_V2.DEACTIVATED])],
641
+ [
642
+ AP_ACTOR_MATURITY_V2.PENDING,
643
+ new Set([AP_ACTOR_MATURITY_V2.ACTIVE, AP_ACTOR_MATURITY_V2.DEACTIVATED]),
644
+ ],
645
+ [
646
+ AP_ACTOR_MATURITY_V2.ACTIVE,
647
+ new Set([AP_ACTOR_MATURITY_V2.SUSPENDED, AP_ACTOR_MATURITY_V2.DEACTIVATED]),
648
+ ],
649
+ [
650
+ AP_ACTOR_MATURITY_V2.SUSPENDED,
651
+ new Set([AP_ACTOR_MATURITY_V2.ACTIVE, AP_ACTOR_MATURITY_V2.DEACTIVATED]),
652
+ ],
638
653
  [AP_ACTOR_MATURITY_V2.DEACTIVATED, new Set()],
639
654
  ]);
640
655
  const _apActorTerminal = new Set([AP_ACTOR_MATURITY_V2.DEACTIVATED]);
641
656
  const _apActTrans = new Map([
642
- [AP_ACTIVITY_LIFECYCLE_V2.QUEUED, new Set([AP_ACTIVITY_LIFECYCLE_V2.DELIVERING, AP_ACTIVITY_LIFECYCLE_V2.CANCELLED])],
643
- [AP_ACTIVITY_LIFECYCLE_V2.DELIVERING, new Set([AP_ACTIVITY_LIFECYCLE_V2.DELIVERED, AP_ACTIVITY_LIFECYCLE_V2.FAILED, AP_ACTIVITY_LIFECYCLE_V2.CANCELLED])],
657
+ [
658
+ AP_ACTIVITY_LIFECYCLE_V2.QUEUED,
659
+ new Set([
660
+ AP_ACTIVITY_LIFECYCLE_V2.DELIVERING,
661
+ AP_ACTIVITY_LIFECYCLE_V2.CANCELLED,
662
+ ]),
663
+ ],
664
+ [
665
+ AP_ACTIVITY_LIFECYCLE_V2.DELIVERING,
666
+ new Set([
667
+ AP_ACTIVITY_LIFECYCLE_V2.DELIVERED,
668
+ AP_ACTIVITY_LIFECYCLE_V2.FAILED,
669
+ AP_ACTIVITY_LIFECYCLE_V2.CANCELLED,
670
+ ]),
671
+ ],
644
672
  [AP_ACTIVITY_LIFECYCLE_V2.DELIVERED, new Set()],
645
673
  [AP_ACTIVITY_LIFECYCLE_V2.FAILED, new Set()],
646
674
  [AP_ACTIVITY_LIFECYCLE_V2.CANCELLED, new Set()],
@@ -653,76 +681,620 @@ let _apMaxPendingPerActor = 25;
653
681
  let _apActorIdleMs = 24 * 60 * 60 * 1000;
654
682
  let _apActStuckMs = 3 * 60 * 1000;
655
683
 
656
- function _apPos(n, lbl) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${lbl} must be positive integer`); return v; }
684
+ function _apPos(n, lbl) {
685
+ const v = Math.floor(Number(n));
686
+ if (!Number.isFinite(v) || v <= 0)
687
+ throw new Error(`${lbl} must be positive integer`);
688
+ return v;
689
+ }
657
690
 
658
- export function setMaxActiveApActorsPerOwnerV2(n) { _apMaxActivePerOwner = _apPos(n, "maxActiveApActorsPerOwner"); }
659
- export function getMaxActiveApActorsPerOwnerV2() { return _apMaxActivePerOwner; }
660
- export function setMaxPendingApActivitiesPerActorV2(n) { _apMaxPendingPerActor = _apPos(n, "maxPendingApActivitiesPerActor"); }
661
- export function getMaxPendingApActivitiesPerActorV2() { return _apMaxPendingPerActor; }
662
- export function setApActorIdleMsV2(n) { _apActorIdleMs = _apPos(n, "apActorIdleMs"); }
663
- export function getApActorIdleMsV2() { return _apActorIdleMs; }
664
- export function setApActivityStuckMsV2(n) { _apActStuckMs = _apPos(n, "apActivityStuckMs"); }
665
- export function getApActivityStuckMsV2() { return _apActStuckMs; }
691
+ export function setMaxActiveApActorsPerOwnerV2(n) {
692
+ _apMaxActivePerOwner = _apPos(n, "maxActiveApActorsPerOwner");
693
+ }
694
+ export function getMaxActiveApActorsPerOwnerV2() {
695
+ return _apMaxActivePerOwner;
696
+ }
697
+ export function setMaxPendingApActivitiesPerActorV2(n) {
698
+ _apMaxPendingPerActor = _apPos(n, "maxPendingApActivitiesPerActor");
699
+ }
700
+ export function getMaxPendingApActivitiesPerActorV2() {
701
+ return _apMaxPendingPerActor;
702
+ }
703
+ export function setApActorIdleMsV2(n) {
704
+ _apActorIdleMs = _apPos(n, "apActorIdleMs");
705
+ }
706
+ export function getApActorIdleMsV2() {
707
+ return _apActorIdleMs;
708
+ }
709
+ export function setApActivityStuckMsV2(n) {
710
+ _apActStuckMs = _apPos(n, "apActivityStuckMs");
711
+ }
712
+ export function getApActivityStuckMsV2() {
713
+ return _apActStuckMs;
714
+ }
666
715
 
667
716
  export function _resetStateActivityPubBridgeV2() {
668
- _apActors.clear(); _apActs.clear();
669
- _apMaxActivePerOwner = 15; _apMaxPendingPerActor = 25;
670
- _apActorIdleMs = 24 * 60 * 60 * 1000; _apActStuckMs = 3 * 60 * 1000;
717
+ _apActors.clear();
718
+ _apActs.clear();
719
+ _apMaxActivePerOwner = 15;
720
+ _apMaxPendingPerActor = 25;
721
+ _apActorIdleMs = 24 * 60 * 60 * 1000;
722
+ _apActStuckMs = 3 * 60 * 1000;
671
723
  }
672
724
 
673
725
  export function registerApActorV2({ id, owner, handle, metadata } = {}) {
674
726
  if (!id || typeof id !== "string") throw new Error("id is required");
675
727
  if (!owner || typeof owner !== "string") throw new Error("owner is required");
676
- if (_apActors.has(id)) throw new Error(`activitypub actor ${id} already registered`);
728
+ if (_apActors.has(id))
729
+ throw new Error(`activitypub actor ${id} already registered`);
677
730
  const now = Date.now();
678
- const a = { id, owner, handle: handle || id, status: AP_ACTOR_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, deactivatedAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
731
+ const a = {
732
+ id,
733
+ owner,
734
+ handle: handle || id,
735
+ status: AP_ACTOR_MATURITY_V2.PENDING,
736
+ createdAt: now,
737
+ updatedAt: now,
738
+ activatedAt: null,
739
+ deactivatedAt: null,
740
+ lastTouchedAt: now,
741
+ metadata: { ...(metadata || {}) },
742
+ };
679
743
  _apActors.set(id, a);
680
744
  return { ...a, metadata: { ...a.metadata } };
681
745
  }
682
- function _apCheckA(from, to) { const al = _apActorTrans.get(from); if (!al || !al.has(to)) throw new Error(`invalid activitypub actor transition ${from} → ${to}`); }
683
- function _apCountActive(owner) { let n = 0; for (const a of _apActors.values()) if (a.owner === owner && a.status === AP_ACTOR_MATURITY_V2.ACTIVE) n++; return n; }
746
+ function _apCheckA(from, to) {
747
+ const al = _apActorTrans.get(from);
748
+ if (!al || !al.has(to))
749
+ throw new Error(`invalid activitypub actor transition ${from} → ${to}`);
750
+ }
751
+ function _apCountActive(owner) {
752
+ let n = 0;
753
+ for (const a of _apActors.values())
754
+ if (a.owner === owner && a.status === AP_ACTOR_MATURITY_V2.ACTIVE) n++;
755
+ return n;
756
+ }
684
757
 
685
758
  export function activateApActorV2(id) {
686
- const a = _apActors.get(id); if (!a) throw new Error(`activitypub actor ${id} not found`);
759
+ const a = _apActors.get(id);
760
+ if (!a) throw new Error(`activitypub actor ${id} not found`);
687
761
  _apCheckA(a.status, AP_ACTOR_MATURITY_V2.ACTIVE);
688
762
  const recovery = a.status === AP_ACTOR_MATURITY_V2.SUSPENDED;
689
- if (!recovery) { const c = _apCountActive(a.owner); if (c >= _apMaxActivePerOwner) throw new Error(`max active activitypub actors per owner (${_apMaxActivePerOwner}) reached for ${a.owner}`); }
690
- const now = Date.now(); a.status = AP_ACTOR_MATURITY_V2.ACTIVE; a.updatedAt = now; a.lastTouchedAt = now; if (!a.activatedAt) a.activatedAt = now;
763
+ if (!recovery) {
764
+ const c = _apCountActive(a.owner);
765
+ if (c >= _apMaxActivePerOwner)
766
+ throw new Error(
767
+ `max active activitypub actors per owner (${_apMaxActivePerOwner}) reached for ${a.owner}`,
768
+ );
769
+ }
770
+ const now = Date.now();
771
+ a.status = AP_ACTOR_MATURITY_V2.ACTIVE;
772
+ a.updatedAt = now;
773
+ a.lastTouchedAt = now;
774
+ if (!a.activatedAt) a.activatedAt = now;
775
+ return { ...a, metadata: { ...a.metadata } };
776
+ }
777
+ export function suspendApActorV2(id) {
778
+ const a = _apActors.get(id);
779
+ if (!a) throw new Error(`activitypub actor ${id} not found`);
780
+ _apCheckA(a.status, AP_ACTOR_MATURITY_V2.SUSPENDED);
781
+ a.status = AP_ACTOR_MATURITY_V2.SUSPENDED;
782
+ a.updatedAt = Date.now();
783
+ return { ...a, metadata: { ...a.metadata } };
784
+ }
785
+ export function deactivateApActorV2(id) {
786
+ const a = _apActors.get(id);
787
+ if (!a) throw new Error(`activitypub actor ${id} not found`);
788
+ _apCheckA(a.status, AP_ACTOR_MATURITY_V2.DEACTIVATED);
789
+ const now = Date.now();
790
+ a.status = AP_ACTOR_MATURITY_V2.DEACTIVATED;
791
+ a.updatedAt = now;
792
+ if (!a.deactivatedAt) a.deactivatedAt = now;
793
+ return { ...a, metadata: { ...a.metadata } };
794
+ }
795
+ export function touchApActorV2(id) {
796
+ const a = _apActors.get(id);
797
+ if (!a) throw new Error(`activitypub actor ${id} not found`);
798
+ if (_apActorTerminal.has(a.status))
799
+ throw new Error(`cannot touch terminal activitypub actor ${id}`);
800
+ const now = Date.now();
801
+ a.lastTouchedAt = now;
802
+ a.updatedAt = now;
803
+ return { ...a, metadata: { ...a.metadata } };
804
+ }
805
+ export function getApActorV2(id) {
806
+ const a = _apActors.get(id);
807
+ if (!a) return null;
691
808
  return { ...a, metadata: { ...a.metadata } };
692
809
  }
693
- export function suspendApActorV2(id) { const a = _apActors.get(id); if (!a) throw new Error(`activitypub actor ${id} not found`); _apCheckA(a.status, AP_ACTOR_MATURITY_V2.SUSPENDED); a.status = AP_ACTOR_MATURITY_V2.SUSPENDED; a.updatedAt = Date.now(); return { ...a, metadata: { ...a.metadata } }; }
694
- export function deactivateApActorV2(id) { const a = _apActors.get(id); if (!a) throw new Error(`activitypub actor ${id} not found`); _apCheckA(a.status, AP_ACTOR_MATURITY_V2.DEACTIVATED); const now = Date.now(); a.status = AP_ACTOR_MATURITY_V2.DEACTIVATED; a.updatedAt = now; if (!a.deactivatedAt) a.deactivatedAt = now; return { ...a, metadata: { ...a.metadata } }; }
695
- export function touchApActorV2(id) { const a = _apActors.get(id); if (!a) throw new Error(`activitypub actor ${id} not found`); if (_apActorTerminal.has(a.status)) throw new Error(`cannot touch terminal activitypub actor ${id}`); const now = Date.now(); a.lastTouchedAt = now; a.updatedAt = now; return { ...a, metadata: { ...a.metadata } }; }
696
- export function getApActorV2(id) { const a = _apActors.get(id); if (!a) return null; return { ...a, metadata: { ...a.metadata } }; }
697
- export function listApActorsV2() { return [..._apActors.values()].map((a) => ({ ...a, metadata: { ...a.metadata } })); }
810
+ export function listApActorsV2() {
811
+ return [..._apActors.values()].map((a) => ({
812
+ ...a,
813
+ metadata: { ...a.metadata },
814
+ }));
815
+ }
698
816
 
699
- function _apCountPending(aid) { let n = 0; for (const ac of _apActs.values()) if (ac.actorId === aid && (ac.status === AP_ACTIVITY_LIFECYCLE_V2.QUEUED || ac.status === AP_ACTIVITY_LIFECYCLE_V2.DELIVERING)) n++; return n; }
817
+ function _apCountPending(aid) {
818
+ let n = 0;
819
+ for (const ac of _apActs.values())
820
+ if (
821
+ ac.actorId === aid &&
822
+ (ac.status === AP_ACTIVITY_LIFECYCLE_V2.QUEUED ||
823
+ ac.status === AP_ACTIVITY_LIFECYCLE_V2.DELIVERING)
824
+ )
825
+ n++;
826
+ return n;
827
+ }
700
828
 
701
829
  export function createApActivityV2({ id, actorId, kind, metadata } = {}) {
702
830
  if (!id || typeof id !== "string") throw new Error("id is required");
703
- if (!actorId || typeof actorId !== "string") throw new Error("actorId is required");
704
- if (_apActs.has(id)) throw new Error(`activitypub activity ${id} already exists`);
705
- if (!_apActors.has(actorId)) throw new Error(`activitypub actor ${actorId} not found`);
831
+ if (!actorId || typeof actorId !== "string")
832
+ throw new Error("actorId is required");
833
+ if (_apActs.has(id))
834
+ throw new Error(`activitypub activity ${id} already exists`);
835
+ if (!_apActors.has(actorId))
836
+ throw new Error(`activitypub actor ${actorId} not found`);
706
837
  const pending = _apCountPending(actorId);
707
- if (pending >= _apMaxPendingPerActor) throw new Error(`max pending activitypub activities per actor (${_apMaxPendingPerActor}) reached for ${actorId}`);
838
+ if (pending >= _apMaxPendingPerActor)
839
+ throw new Error(
840
+ `max pending activitypub activities per actor (${_apMaxPendingPerActor}) reached for ${actorId}`,
841
+ );
708
842
  const now = Date.now();
709
- const ac = { id, actorId, kind: kind || "Note", status: AP_ACTIVITY_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
843
+ const ac = {
844
+ id,
845
+ actorId,
846
+ kind: kind || "Note",
847
+ status: AP_ACTIVITY_LIFECYCLE_V2.QUEUED,
848
+ createdAt: now,
849
+ updatedAt: now,
850
+ startedAt: null,
851
+ settledAt: null,
852
+ metadata: { ...(metadata || {}) },
853
+ };
710
854
  _apActs.set(id, ac);
711
855
  return { ...ac, metadata: { ...ac.metadata } };
712
856
  }
713
- function _apCheckAct(from, to) { const al = _apActTrans.get(from); if (!al || !al.has(to)) throw new Error(`invalid activitypub activity transition ${from} → ${to}`); }
714
- export function startApActivityV2(id) { const ac = _apActs.get(id); if (!ac) throw new Error(`activitypub activity ${id} not found`); _apCheckAct(ac.status, AP_ACTIVITY_LIFECYCLE_V2.DELIVERING); const now = Date.now(); ac.status = AP_ACTIVITY_LIFECYCLE_V2.DELIVERING; ac.updatedAt = now; if (!ac.startedAt) ac.startedAt = now; return { ...ac, metadata: { ...ac.metadata } }; }
715
- export function deliverApActivityV2(id) { const ac = _apActs.get(id); if (!ac) throw new Error(`activitypub activity ${id} not found`); _apCheckAct(ac.status, AP_ACTIVITY_LIFECYCLE_V2.DELIVERED); const now = Date.now(); ac.status = AP_ACTIVITY_LIFECYCLE_V2.DELIVERED; ac.updatedAt = now; if (!ac.settledAt) ac.settledAt = now; return { ...ac, metadata: { ...ac.metadata } }; }
716
- export function failApActivityV2(id, reason) { const ac = _apActs.get(id); if (!ac) throw new Error(`activitypub activity ${id} not found`); _apCheckAct(ac.status, AP_ACTIVITY_LIFECYCLE_V2.FAILED); const now = Date.now(); ac.status = AP_ACTIVITY_LIFECYCLE_V2.FAILED; ac.updatedAt = now; if (!ac.settledAt) ac.settledAt = now; if (reason) ac.metadata.failReason = String(reason); return { ...ac, metadata: { ...ac.metadata } }; }
717
- export function cancelApActivityV2(id, reason) { const ac = _apActs.get(id); if (!ac) throw new Error(`activitypub activity ${id} not found`); _apCheckAct(ac.status, AP_ACTIVITY_LIFECYCLE_V2.CANCELLED); const now = Date.now(); ac.status = AP_ACTIVITY_LIFECYCLE_V2.CANCELLED; ac.updatedAt = now; if (!ac.settledAt) ac.settledAt = now; if (reason) ac.metadata.cancelReason = String(reason); return { ...ac, metadata: { ...ac.metadata } }; }
718
- export function getApActivityV2(id) { const ac = _apActs.get(id); if (!ac) return null; return { ...ac, metadata: { ...ac.metadata } }; }
719
- export function listApActivitiesV2() { return [..._apActs.values()].map((ac) => ({ ...ac, metadata: { ...ac.metadata } })); }
857
+ function _apCheckAct(from, to) {
858
+ const al = _apActTrans.get(from);
859
+ if (!al || !al.has(to))
860
+ throw new Error(`invalid activitypub activity transition ${from} ${to}`);
861
+ }
862
+ export function startApActivityV2(id) {
863
+ const ac = _apActs.get(id);
864
+ if (!ac) throw new Error(`activitypub activity ${id} not found`);
865
+ _apCheckAct(ac.status, AP_ACTIVITY_LIFECYCLE_V2.DELIVERING);
866
+ const now = Date.now();
867
+ ac.status = AP_ACTIVITY_LIFECYCLE_V2.DELIVERING;
868
+ ac.updatedAt = now;
869
+ if (!ac.startedAt) ac.startedAt = now;
870
+ return { ...ac, metadata: { ...ac.metadata } };
871
+ }
872
+ export function deliverApActivityV2(id) {
873
+ const ac = _apActs.get(id);
874
+ if (!ac) throw new Error(`activitypub activity ${id} not found`);
875
+ _apCheckAct(ac.status, AP_ACTIVITY_LIFECYCLE_V2.DELIVERED);
876
+ const now = Date.now();
877
+ ac.status = AP_ACTIVITY_LIFECYCLE_V2.DELIVERED;
878
+ ac.updatedAt = now;
879
+ if (!ac.settledAt) ac.settledAt = now;
880
+ return { ...ac, metadata: { ...ac.metadata } };
881
+ }
882
+ export function failApActivityV2(id, reason) {
883
+ const ac = _apActs.get(id);
884
+ if (!ac) throw new Error(`activitypub activity ${id} not found`);
885
+ _apCheckAct(ac.status, AP_ACTIVITY_LIFECYCLE_V2.FAILED);
886
+ const now = Date.now();
887
+ ac.status = AP_ACTIVITY_LIFECYCLE_V2.FAILED;
888
+ ac.updatedAt = now;
889
+ if (!ac.settledAt) ac.settledAt = now;
890
+ if (reason) ac.metadata.failReason = String(reason);
891
+ return { ...ac, metadata: { ...ac.metadata } };
892
+ }
893
+ export function cancelApActivityV2(id, reason) {
894
+ const ac = _apActs.get(id);
895
+ if (!ac) throw new Error(`activitypub activity ${id} not found`);
896
+ _apCheckAct(ac.status, AP_ACTIVITY_LIFECYCLE_V2.CANCELLED);
897
+ const now = Date.now();
898
+ ac.status = AP_ACTIVITY_LIFECYCLE_V2.CANCELLED;
899
+ ac.updatedAt = now;
900
+ if (!ac.settledAt) ac.settledAt = now;
901
+ if (reason) ac.metadata.cancelReason = String(reason);
902
+ return { ...ac, metadata: { ...ac.metadata } };
903
+ }
904
+ export function getApActivityV2(id) {
905
+ const ac = _apActs.get(id);
906
+ if (!ac) return null;
907
+ return { ...ac, metadata: { ...ac.metadata } };
908
+ }
909
+ export function listApActivitiesV2() {
910
+ return [..._apActs.values()].map((ac) => ({
911
+ ...ac,
912
+ metadata: { ...ac.metadata },
913
+ }));
914
+ }
720
915
 
721
- export function autoSuspendIdleApActorsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const a of _apActors.values()) if (a.status === AP_ACTOR_MATURITY_V2.ACTIVE && (t - a.lastTouchedAt) >= _apActorIdleMs) { a.status = AP_ACTOR_MATURITY_V2.SUSPENDED; a.updatedAt = t; flipped.push(a.id); } return { flipped, count: flipped.length }; }
722
- export function autoFailStuckApActivitiesV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const ac of _apActs.values()) if (ac.status === AP_ACTIVITY_LIFECYCLE_V2.DELIVERING && ac.startedAt != null && (t - ac.startedAt) >= _apActStuckMs) { ac.status = AP_ACTIVITY_LIFECYCLE_V2.FAILED; ac.updatedAt = t; if (!ac.settledAt) ac.settledAt = t; ac.metadata.failReason = "auto-fail-stuck"; flipped.push(ac.id); } return { flipped, count: flipped.length }; }
916
+ export function autoSuspendIdleApActorsV2({ now } = {}) {
917
+ const t = now ?? Date.now();
918
+ const flipped = [];
919
+ for (const a of _apActors.values())
920
+ if (
921
+ a.status === AP_ACTOR_MATURITY_V2.ACTIVE &&
922
+ t - a.lastTouchedAt >= _apActorIdleMs
923
+ ) {
924
+ a.status = AP_ACTOR_MATURITY_V2.SUSPENDED;
925
+ a.updatedAt = t;
926
+ flipped.push(a.id);
927
+ }
928
+ return { flipped, count: flipped.length };
929
+ }
930
+ export function autoFailStuckApActivitiesV2({ now } = {}) {
931
+ const t = now ?? Date.now();
932
+ const flipped = [];
933
+ for (const ac of _apActs.values())
934
+ if (
935
+ ac.status === AP_ACTIVITY_LIFECYCLE_V2.DELIVERING &&
936
+ ac.startedAt != null &&
937
+ t - ac.startedAt >= _apActStuckMs
938
+ ) {
939
+ ac.status = AP_ACTIVITY_LIFECYCLE_V2.FAILED;
940
+ ac.updatedAt = t;
941
+ if (!ac.settledAt) ac.settledAt = t;
942
+ ac.metadata.failReason = "auto-fail-stuck";
943
+ flipped.push(ac.id);
944
+ }
945
+ return { flipped, count: flipped.length };
946
+ }
723
947
 
724
948
  export function getActivityPubBridgeStatsV2() {
725
- const actorsByStatus = {}; for (const s of Object.values(AP_ACTOR_MATURITY_V2)) actorsByStatus[s] = 0; for (const a of _apActors.values()) actorsByStatus[a.status]++;
726
- const activitiesByStatus = {}; for (const s of Object.values(AP_ACTIVITY_LIFECYCLE_V2)) activitiesByStatus[s] = 0; for (const ac of _apActs.values()) activitiesByStatus[ac.status]++;
727
- return { totalActorsV2: _apActors.size, totalActivitiesV2: _apActs.size, maxActiveApActorsPerOwner: _apMaxActivePerOwner, maxPendingApActivitiesPerActor: _apMaxPendingPerActor, apActorIdleMs: _apActorIdleMs, apActivityStuckMs: _apActStuckMs, actorsByStatus, activitiesByStatus };
949
+ const actorsByStatus = {};
950
+ for (const s of Object.values(AP_ACTOR_MATURITY_V2)) actorsByStatus[s] = 0;
951
+ for (const a of _apActors.values()) actorsByStatus[a.status]++;
952
+ const activitiesByStatus = {};
953
+ for (const s of Object.values(AP_ACTIVITY_LIFECYCLE_V2))
954
+ activitiesByStatus[s] = 0;
955
+ for (const ac of _apActs.values()) activitiesByStatus[ac.status]++;
956
+ return {
957
+ totalActorsV2: _apActors.size,
958
+ totalActivitiesV2: _apActs.size,
959
+ maxActiveApActorsPerOwner: _apMaxActivePerOwner,
960
+ maxPendingApActivitiesPerActor: _apMaxPendingPerActor,
961
+ apActorIdleMs: _apActorIdleMs,
962
+ apActivityStuckMs: _apActStuckMs,
963
+ actorsByStatus,
964
+ activitiesByStatus,
965
+ };
966
+ }
967
+
968
+ // =====================================================================
969
+ // activitypub-bridge V2 governance overlay (iter21)
970
+ // =====================================================================
971
+ export const APGOV_PROFILE_MATURITY_V2 = Object.freeze({
972
+ PENDING: "pending",
973
+ ACTIVE: "active",
974
+ SUSPENDED: "suspended",
975
+ ARCHIVED: "archived",
976
+ });
977
+ export const APGOV_DELIVERY_LIFECYCLE_V2 = Object.freeze({
978
+ QUEUED: "queued",
979
+ DELIVERING: "delivering",
980
+ DELIVERED: "delivered",
981
+ FAILED: "failed",
982
+ CANCELLED: "cancelled",
983
+ });
984
+ const _apgovPTrans = new Map([
985
+ [
986
+ APGOV_PROFILE_MATURITY_V2.PENDING,
987
+ new Set([
988
+ APGOV_PROFILE_MATURITY_V2.ACTIVE,
989
+ APGOV_PROFILE_MATURITY_V2.ARCHIVED,
990
+ ]),
991
+ ],
992
+ [
993
+ APGOV_PROFILE_MATURITY_V2.ACTIVE,
994
+ new Set([
995
+ APGOV_PROFILE_MATURITY_V2.SUSPENDED,
996
+ APGOV_PROFILE_MATURITY_V2.ARCHIVED,
997
+ ]),
998
+ ],
999
+ [
1000
+ APGOV_PROFILE_MATURITY_V2.SUSPENDED,
1001
+ new Set([
1002
+ APGOV_PROFILE_MATURITY_V2.ACTIVE,
1003
+ APGOV_PROFILE_MATURITY_V2.ARCHIVED,
1004
+ ]),
1005
+ ],
1006
+ [APGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
1007
+ ]);
1008
+ const _apgovPTerminal = new Set([APGOV_PROFILE_MATURITY_V2.ARCHIVED]);
1009
+ const _apgovJTrans = new Map([
1010
+ [
1011
+ APGOV_DELIVERY_LIFECYCLE_V2.QUEUED,
1012
+ new Set([
1013
+ APGOV_DELIVERY_LIFECYCLE_V2.DELIVERING,
1014
+ APGOV_DELIVERY_LIFECYCLE_V2.CANCELLED,
1015
+ ]),
1016
+ ],
1017
+ [
1018
+ APGOV_DELIVERY_LIFECYCLE_V2.DELIVERING,
1019
+ new Set([
1020
+ APGOV_DELIVERY_LIFECYCLE_V2.DELIVERED,
1021
+ APGOV_DELIVERY_LIFECYCLE_V2.FAILED,
1022
+ APGOV_DELIVERY_LIFECYCLE_V2.CANCELLED,
1023
+ ]),
1024
+ ],
1025
+ [APGOV_DELIVERY_LIFECYCLE_V2.DELIVERED, new Set()],
1026
+ [APGOV_DELIVERY_LIFECYCLE_V2.FAILED, new Set()],
1027
+ [APGOV_DELIVERY_LIFECYCLE_V2.CANCELLED, new Set()],
1028
+ ]);
1029
+ const _apgovPsV2 = new Map();
1030
+ const _apgovJsV2 = new Map();
1031
+ let _apgovMaxActive = 8,
1032
+ _apgovMaxPending = 25,
1033
+ _apgovIdleMs = 30 * 24 * 60 * 60 * 1000,
1034
+ _apgovStuckMs = 60 * 1000;
1035
+ function _apgovPos(n, label) {
1036
+ const v = Math.floor(Number(n));
1037
+ if (!Number.isFinite(v) || v <= 0)
1038
+ throw new Error(`${label} must be positive integer`);
1039
+ return v;
1040
+ }
1041
+ function _apgovCheckP(from, to) {
1042
+ const a = _apgovPTrans.get(from);
1043
+ if (!a || !a.has(to))
1044
+ throw new Error(`invalid apgov profile transition ${from} → ${to}`);
1045
+ }
1046
+ function _apgovCheckJ(from, to) {
1047
+ const a = _apgovJTrans.get(from);
1048
+ if (!a || !a.has(to))
1049
+ throw new Error(`invalid apgov delivery transition ${from} → ${to}`);
1050
+ }
1051
+ function _apgovCountActive(owner) {
1052
+ let c = 0;
1053
+ for (const p of _apgovPsV2.values())
1054
+ if (p.owner === owner && p.status === APGOV_PROFILE_MATURITY_V2.ACTIVE) c++;
1055
+ return c;
1056
+ }
1057
+ function _apgovCountPending(profileId) {
1058
+ let c = 0;
1059
+ for (const j of _apgovJsV2.values())
1060
+ if (
1061
+ j.profileId === profileId &&
1062
+ (j.status === APGOV_DELIVERY_LIFECYCLE_V2.QUEUED ||
1063
+ j.status === APGOV_DELIVERY_LIFECYCLE_V2.DELIVERING)
1064
+ )
1065
+ c++;
1066
+ return c;
1067
+ }
1068
+ export function setMaxActiveApgovProfilesPerOwnerV2(n) {
1069
+ _apgovMaxActive = _apgovPos(n, "maxActiveApgovProfilesPerOwner");
1070
+ }
1071
+ export function getMaxActiveApgovProfilesPerOwnerV2() {
1072
+ return _apgovMaxActive;
1073
+ }
1074
+ export function setMaxPendingApgovDeliverysPerProfileV2(n) {
1075
+ _apgovMaxPending = _apgovPos(n, "maxPendingApgovDeliverysPerProfile");
1076
+ }
1077
+ export function getMaxPendingApgovDeliverysPerProfileV2() {
1078
+ return _apgovMaxPending;
1079
+ }
1080
+ export function setApgovProfileIdleMsV2(n) {
1081
+ _apgovIdleMs = _apgovPos(n, "apgovProfileIdleMs");
1082
+ }
1083
+ export function getApgovProfileIdleMsV2() {
1084
+ return _apgovIdleMs;
1085
+ }
1086
+ export function setApgovDeliveryStuckMsV2(n) {
1087
+ _apgovStuckMs = _apgovPos(n, "apgovDeliveryStuckMs");
1088
+ }
1089
+ export function getApgovDeliveryStuckMsV2() {
1090
+ return _apgovStuckMs;
1091
+ }
1092
+ export function _resetStateActivityPubBridgeGovV2() {
1093
+ _apgovPsV2.clear();
1094
+ _apgovJsV2.clear();
1095
+ _apgovMaxActive = 8;
1096
+ _apgovMaxPending = 25;
1097
+ _apgovIdleMs = 30 * 24 * 60 * 60 * 1000;
1098
+ _apgovStuckMs = 60 * 1000;
1099
+ }
1100
+ export function registerApgovProfileV2({ id, owner, actor, metadata } = {}) {
1101
+ if (!id || !owner) throw new Error("id and owner required");
1102
+ if (_apgovPsV2.has(id)) throw new Error(`apgov profile ${id} already exists`);
1103
+ const now = Date.now();
1104
+ const p = {
1105
+ id,
1106
+ owner,
1107
+ actor: actor || "person",
1108
+ status: APGOV_PROFILE_MATURITY_V2.PENDING,
1109
+ createdAt: now,
1110
+ updatedAt: now,
1111
+ lastTouchedAt: now,
1112
+ activatedAt: null,
1113
+ archivedAt: null,
1114
+ metadata: { ...(metadata || {}) },
1115
+ };
1116
+ _apgovPsV2.set(id, p);
1117
+ return { ...p, metadata: { ...p.metadata } };
1118
+ }
1119
+ export function activateApgovProfileV2(id) {
1120
+ const p = _apgovPsV2.get(id);
1121
+ if (!p) throw new Error(`apgov profile ${id} not found`);
1122
+ const isInitial = p.status === APGOV_PROFILE_MATURITY_V2.PENDING;
1123
+ _apgovCheckP(p.status, APGOV_PROFILE_MATURITY_V2.ACTIVE);
1124
+ if (isInitial && _apgovCountActive(p.owner) >= _apgovMaxActive)
1125
+ throw new Error(`max active apgov profiles for owner ${p.owner} reached`);
1126
+ const now = Date.now();
1127
+ p.status = APGOV_PROFILE_MATURITY_V2.ACTIVE;
1128
+ p.updatedAt = now;
1129
+ p.lastTouchedAt = now;
1130
+ if (!p.activatedAt) p.activatedAt = now;
1131
+ return { ...p, metadata: { ...p.metadata } };
1132
+ }
1133
+ export function suspendApgovProfileV2(id) {
1134
+ const p = _apgovPsV2.get(id);
1135
+ if (!p) throw new Error(`apgov profile ${id} not found`);
1136
+ _apgovCheckP(p.status, APGOV_PROFILE_MATURITY_V2.SUSPENDED);
1137
+ p.status = APGOV_PROFILE_MATURITY_V2.SUSPENDED;
1138
+ p.updatedAt = Date.now();
1139
+ return { ...p, metadata: { ...p.metadata } };
1140
+ }
1141
+ export function archiveApgovProfileV2(id) {
1142
+ const p = _apgovPsV2.get(id);
1143
+ if (!p) throw new Error(`apgov profile ${id} not found`);
1144
+ _apgovCheckP(p.status, APGOV_PROFILE_MATURITY_V2.ARCHIVED);
1145
+ const now = Date.now();
1146
+ p.status = APGOV_PROFILE_MATURITY_V2.ARCHIVED;
1147
+ p.updatedAt = now;
1148
+ if (!p.archivedAt) p.archivedAt = now;
1149
+ return { ...p, metadata: { ...p.metadata } };
1150
+ }
1151
+ export function touchApgovProfileV2(id) {
1152
+ const p = _apgovPsV2.get(id);
1153
+ if (!p) throw new Error(`apgov profile ${id} not found`);
1154
+ if (_apgovPTerminal.has(p.status))
1155
+ throw new Error(`cannot touch terminal apgov profile ${id}`);
1156
+ const now = Date.now();
1157
+ p.lastTouchedAt = now;
1158
+ p.updatedAt = now;
1159
+ return { ...p, metadata: { ...p.metadata } };
1160
+ }
1161
+ export function getApgovProfileV2(id) {
1162
+ const p = _apgovPsV2.get(id);
1163
+ if (!p) return null;
1164
+ return { ...p, metadata: { ...p.metadata } };
1165
+ }
1166
+ export function listApgovProfilesV2() {
1167
+ return [..._apgovPsV2.values()].map((p) => ({
1168
+ ...p,
1169
+ metadata: { ...p.metadata },
1170
+ }));
1171
+ }
1172
+ export function createApgovDeliveryV2({ id, profileId, inbox, metadata } = {}) {
1173
+ if (!id || !profileId) throw new Error("id and profileId required");
1174
+ if (_apgovJsV2.has(id))
1175
+ throw new Error(`apgov delivery ${id} already exists`);
1176
+ if (!_apgovPsV2.has(profileId))
1177
+ throw new Error(`apgov profile ${profileId} not found`);
1178
+ if (_apgovCountPending(profileId) >= _apgovMaxPending)
1179
+ throw new Error(
1180
+ `max pending apgov deliverys for profile ${profileId} reached`,
1181
+ );
1182
+ const now = Date.now();
1183
+ const j = {
1184
+ id,
1185
+ profileId,
1186
+ inbox: inbox || "",
1187
+ status: APGOV_DELIVERY_LIFECYCLE_V2.QUEUED,
1188
+ createdAt: now,
1189
+ updatedAt: now,
1190
+ startedAt: null,
1191
+ settledAt: null,
1192
+ metadata: { ...(metadata || {}) },
1193
+ };
1194
+ _apgovJsV2.set(id, j);
1195
+ return { ...j, metadata: { ...j.metadata } };
1196
+ }
1197
+ export function deliveringApgovDeliveryV2(id) {
1198
+ const j = _apgovJsV2.get(id);
1199
+ if (!j) throw new Error(`apgov delivery ${id} not found`);
1200
+ _apgovCheckJ(j.status, APGOV_DELIVERY_LIFECYCLE_V2.DELIVERING);
1201
+ const now = Date.now();
1202
+ j.status = APGOV_DELIVERY_LIFECYCLE_V2.DELIVERING;
1203
+ j.updatedAt = now;
1204
+ if (!j.startedAt) j.startedAt = now;
1205
+ return { ...j, metadata: { ...j.metadata } };
1206
+ }
1207
+ export function completeDeliveryApgovV2(id) {
1208
+ const j = _apgovJsV2.get(id);
1209
+ if (!j) throw new Error(`apgov delivery ${id} not found`);
1210
+ _apgovCheckJ(j.status, APGOV_DELIVERY_LIFECYCLE_V2.DELIVERED);
1211
+ const now = Date.now();
1212
+ j.status = APGOV_DELIVERY_LIFECYCLE_V2.DELIVERED;
1213
+ j.updatedAt = now;
1214
+ if (!j.settledAt) j.settledAt = now;
1215
+ return { ...j, metadata: { ...j.metadata } };
1216
+ }
1217
+ export function failApgovDeliveryV2(id, reason) {
1218
+ const j = _apgovJsV2.get(id);
1219
+ if (!j) throw new Error(`apgov delivery ${id} not found`);
1220
+ _apgovCheckJ(j.status, APGOV_DELIVERY_LIFECYCLE_V2.FAILED);
1221
+ const now = Date.now();
1222
+ j.status = APGOV_DELIVERY_LIFECYCLE_V2.FAILED;
1223
+ j.updatedAt = now;
1224
+ if (!j.settledAt) j.settledAt = now;
1225
+ if (reason) j.metadata.failReason = String(reason);
1226
+ return { ...j, metadata: { ...j.metadata } };
1227
+ }
1228
+ export function cancelApgovDeliveryV2(id, reason) {
1229
+ const j = _apgovJsV2.get(id);
1230
+ if (!j) throw new Error(`apgov delivery ${id} not found`);
1231
+ _apgovCheckJ(j.status, APGOV_DELIVERY_LIFECYCLE_V2.CANCELLED);
1232
+ const now = Date.now();
1233
+ j.status = APGOV_DELIVERY_LIFECYCLE_V2.CANCELLED;
1234
+ j.updatedAt = now;
1235
+ if (!j.settledAt) j.settledAt = now;
1236
+ if (reason) j.metadata.cancelReason = String(reason);
1237
+ return { ...j, metadata: { ...j.metadata } };
1238
+ }
1239
+ export function getApgovDeliveryV2(id) {
1240
+ const j = _apgovJsV2.get(id);
1241
+ if (!j) return null;
1242
+ return { ...j, metadata: { ...j.metadata } };
1243
+ }
1244
+ export function listApgovDeliverysV2() {
1245
+ return [..._apgovJsV2.values()].map((j) => ({
1246
+ ...j,
1247
+ metadata: { ...j.metadata },
1248
+ }));
1249
+ }
1250
+ export function autoSuspendIdleApgovProfilesV2({ now } = {}) {
1251
+ const t = now ?? Date.now();
1252
+ const flipped = [];
1253
+ for (const p of _apgovPsV2.values())
1254
+ if (
1255
+ p.status === APGOV_PROFILE_MATURITY_V2.ACTIVE &&
1256
+ t - p.lastTouchedAt >= _apgovIdleMs
1257
+ ) {
1258
+ p.status = APGOV_PROFILE_MATURITY_V2.SUSPENDED;
1259
+ p.updatedAt = t;
1260
+ flipped.push(p.id);
1261
+ }
1262
+ return { flipped, count: flipped.length };
1263
+ }
1264
+ export function autoFailStuckApgovDeliverysV2({ now } = {}) {
1265
+ const t = now ?? Date.now();
1266
+ const flipped = [];
1267
+ for (const j of _apgovJsV2.values())
1268
+ if (
1269
+ j.status === APGOV_DELIVERY_LIFECYCLE_V2.DELIVERING &&
1270
+ j.startedAt != null &&
1271
+ t - j.startedAt >= _apgovStuckMs
1272
+ ) {
1273
+ j.status = APGOV_DELIVERY_LIFECYCLE_V2.FAILED;
1274
+ j.updatedAt = t;
1275
+ if (!j.settledAt) j.settledAt = t;
1276
+ j.metadata.failReason = "auto-fail-stuck";
1277
+ flipped.push(j.id);
1278
+ }
1279
+ return { flipped, count: flipped.length };
1280
+ }
1281
+ export function getActivityPubBridgeGovStatsV2() {
1282
+ const profilesByStatus = {};
1283
+ for (const v of Object.values(APGOV_PROFILE_MATURITY_V2))
1284
+ profilesByStatus[v] = 0;
1285
+ for (const p of _apgovPsV2.values()) profilesByStatus[p.status]++;
1286
+ const deliverysByStatus = {};
1287
+ for (const v of Object.values(APGOV_DELIVERY_LIFECYCLE_V2))
1288
+ deliverysByStatus[v] = 0;
1289
+ for (const j of _apgovJsV2.values()) deliverysByStatus[j.status]++;
1290
+ return {
1291
+ totalApgovProfilesV2: _apgovPsV2.size,
1292
+ totalApgovDeliverysV2: _apgovJsV2.size,
1293
+ maxActiveApgovProfilesPerOwner: _apgovMaxActive,
1294
+ maxPendingApgovDeliverysPerProfile: _apgovMaxPending,
1295
+ apgovProfileIdleMs: _apgovIdleMs,
1296
+ apgovDeliveryStuckMs: _apgovStuckMs,
1297
+ profilesByStatus,
1298
+ deliverysByStatus,
1299
+ };
728
1300
  }