chainlesschain 0.81.0 → 0.143.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 (209) hide show
  1. package/bin/chainlesschain.js +0 -0
  2. package/package.json +1 -1
  3. package/src/commands/a2a.js +62 -0
  4. package/src/commands/activitypub.js +61 -0
  5. package/src/commands/agent-network.js +254 -1
  6. package/src/commands/agent.js +117 -0
  7. package/src/commands/audit.js +302 -0
  8. package/src/commands/automation.js +271 -1
  9. package/src/commands/bi.js +61 -0
  10. package/src/commands/bm25.js +78 -0
  11. package/src/commands/browse.js +64 -0
  12. package/src/commands/ccron.js +78 -0
  13. package/src/commands/codegen.js +224 -0
  14. package/src/commands/collab.js +341 -0
  15. package/src/commands/compliance.js +1075 -0
  16. package/src/commands/compt.js +78 -0
  17. package/src/commands/consol.js +231 -0
  18. package/src/commands/cowork.js +263 -0
  19. package/src/commands/crosschain.js +62 -0
  20. package/src/commands/dao.js +62 -0
  21. package/src/commands/dbevo.js +284 -0
  22. package/src/commands/dev.js +252 -0
  23. package/src/commands/did.js +358 -0
  24. package/src/commands/dlp.js +61 -0
  25. package/src/commands/economy.js +56 -0
  26. package/src/commands/encrypt.js +341 -0
  27. package/src/commands/evolution.js +56 -0
  28. package/src/commands/evomap.js +61 -0
  29. package/src/commands/export.js +256 -1
  30. package/src/commands/fflag.js +178 -0
  31. package/src/commands/fusion.js +258 -0
  32. package/src/commands/git.js +45 -0
  33. package/src/commands/governance.js +325 -0
  34. package/src/commands/hardening.js +411 -0
  35. package/src/commands/hmemory.js +56 -0
  36. package/src/commands/hook.js +148 -0
  37. package/src/commands/import.js +252 -0
  38. package/src/commands/incentive.js +322 -0
  39. package/src/commands/inference.js +42 -0
  40. package/src/commands/infra.js +244 -0
  41. package/src/commands/instinct.js +260 -0
  42. package/src/commands/ipfs.js +318 -0
  43. package/src/commands/itbudget.js +45 -0
  44. package/src/commands/kg.js +387 -0
  45. package/src/commands/llm.js +263 -0
  46. package/src/commands/lowcode.js +44 -0
  47. package/src/commands/matrix.js +62 -0
  48. package/src/commands/mcp.js +221 -0
  49. package/src/commands/mcpscaf.js +41 -0
  50. package/src/commands/meminj.js +41 -0
  51. package/src/commands/memory.js +248 -0
  52. package/src/commands/multimodal.js +296 -0
  53. package/src/commands/nlprog.js +356 -0
  54. package/src/commands/nostr.js +62 -0
  55. package/src/commands/note.js +244 -0
  56. package/src/commands/ops.js +354 -0
  57. package/src/commands/orchestrate.js +166 -0
  58. package/src/commands/orchgov.js +45 -0
  59. package/src/commands/org.js +277 -0
  60. package/src/commands/p2p.js +390 -0
  61. package/src/commands/pdfp.js +78 -0
  62. package/src/commands/perception.js +290 -0
  63. package/src/commands/perf.js +39 -0
  64. package/src/commands/perm.js +45 -0
  65. package/src/commands/permmem.js +251 -0
  66. package/src/commands/pipeline.js +57 -1
  67. package/src/commands/planmode.js +45 -0
  68. package/src/commands/plugin-ecosystem.js +273 -0
  69. package/src/commands/pqc.js +393 -0
  70. package/src/commands/promcomp.js +82 -0
  71. package/src/commands/quantization.js +351 -0
  72. package/src/commands/rcache.js +271 -0
  73. package/src/commands/recommend.js +382 -0
  74. package/src/commands/runtime.js +307 -0
  75. package/src/commands/scim.js +262 -0
  76. package/src/commands/seshhook.js +41 -0
  77. package/src/commands/seshsearch.js +41 -0
  78. package/src/commands/seshtail.js +41 -0
  79. package/src/commands/seshu.js +41 -0
  80. package/src/commands/session.js +258 -0
  81. package/src/commands/sganal.js +78 -0
  82. package/src/commands/siem.js +40 -0
  83. package/src/commands/skill.js +267 -1
  84. package/src/commands/slotfill.js +41 -0
  85. package/src/commands/social.js +290 -0
  86. package/src/commands/sso.js +186 -1
  87. package/src/commands/svccont.js +45 -0
  88. package/src/commands/sync.js +256 -0
  89. package/src/commands/tech.js +338 -0
  90. package/src/commands/tenant.js +351 -0
  91. package/src/commands/tms.js +45 -0
  92. package/src/commands/tokens.js +269 -0
  93. package/src/commands/topiccls.js +45 -0
  94. package/src/commands/trust.js +249 -0
  95. package/src/commands/uprof.js +45 -0
  96. package/src/commands/vcheck.js +78 -0
  97. package/src/commands/wallet.js +277 -0
  98. package/src/commands/webfetch.js +41 -0
  99. package/src/commands/workflow.js +171 -0
  100. package/src/commands/zkp.js +62 -0
  101. package/src/harness/prompt-compressor.js +331 -0
  102. package/src/index.js +65 -1
  103. package/src/lib/a2a-protocol.js +105 -0
  104. package/src/lib/activitypub-bridge.js +105 -0
  105. package/src/lib/agent-coordinator.js +325 -0
  106. package/src/lib/agent-economy.js +105 -0
  107. package/src/lib/agent-network.js +387 -0
  108. package/src/lib/agent-router.js +395 -0
  109. package/src/lib/aiops.js +478 -0
  110. package/src/lib/app-builder.js +105 -0
  111. package/src/lib/audit-logger.js +379 -0
  112. package/src/lib/automation-engine.js +330 -0
  113. package/src/lib/autonomous-agent.js +105 -0
  114. package/src/lib/autonomous-developer.js +350 -0
  115. package/src/lib/bi-engine.js +105 -0
  116. package/src/lib/bm25-search.js +81 -0
  117. package/src/lib/browser-automation.js +105 -0
  118. package/src/lib/code-agent.js +323 -0
  119. package/src/lib/collaboration-governance.js +364 -0
  120. package/src/lib/community-governance.js +436 -0
  121. package/src/lib/compliance-framework-reporter.js +105 -0
  122. package/src/lib/compliance-manager.js +434 -0
  123. package/src/lib/compression-telemetry.js +81 -0
  124. package/src/lib/content-recommendation.js +469 -0
  125. package/src/lib/content-recommender.js +105 -0
  126. package/src/lib/cowork-cron.js +81 -0
  127. package/src/lib/cowork-task-runner.js +105 -0
  128. package/src/lib/cross-chain.js +105 -0
  129. package/src/lib/crypto-manager.js +350 -0
  130. package/src/lib/dao-governance.js +105 -0
  131. package/src/lib/dbevo.js +338 -0
  132. package/src/lib/decentral-infra.js +340 -0
  133. package/src/lib/did-manager.js +367 -0
  134. package/src/lib/dlp-engine.js +105 -0
  135. package/src/lib/evolution-system.js +105 -0
  136. package/src/lib/evomap-manager.js +105 -0
  137. package/src/lib/execution-backend.js +105 -0
  138. package/src/lib/feature-flags.js +85 -0
  139. package/src/lib/git-integration.js +105 -0
  140. package/src/lib/hardening-manager.js +348 -0
  141. package/src/lib/hierarchical-memory.js +105 -0
  142. package/src/lib/hook-manager.js +380 -0
  143. package/src/lib/inference-network.js +105 -0
  144. package/src/lib/instinct-manager.js +332 -0
  145. package/src/lib/ipfs-storage.js +334 -0
  146. package/src/lib/iteration-budget.js +105 -0
  147. package/src/lib/knowledge-exporter.js +381 -0
  148. package/src/lib/knowledge-graph.js +432 -0
  149. package/src/lib/knowledge-importer.js +379 -0
  150. package/src/lib/llm-providers.js +391 -0
  151. package/src/lib/matrix-bridge.js +105 -0
  152. package/src/lib/mcp-registry.js +333 -0
  153. package/src/lib/mcp-scaffold.js +81 -0
  154. package/src/lib/memory-injection.js +81 -0
  155. package/src/lib/memory-manager.js +330 -0
  156. package/src/lib/multimodal.js +346 -0
  157. package/src/lib/nl-programming.js +343 -0
  158. package/src/lib/nostr-bridge.js +105 -0
  159. package/src/lib/note-versioning.js +327 -0
  160. package/src/lib/orchestrator.js +105 -0
  161. package/src/lib/org-manager.js +323 -0
  162. package/src/lib/p2p-manager.js +387 -0
  163. package/src/lib/pdf-parser.js +81 -0
  164. package/src/lib/perception.js +346 -0
  165. package/src/lib/perf-tuning.js +109 -1
  166. package/src/lib/permanent-memory.js +320 -0
  167. package/src/lib/permission-engine.js +81 -0
  168. package/src/lib/pipeline-orchestrator.js +105 -0
  169. package/src/lib/plan-mode.js +81 -0
  170. package/src/lib/plugin-ecosystem.js +377 -0
  171. package/src/lib/pqc-manager.js +368 -0
  172. package/src/lib/prompt-compressor.js +1 -10
  173. package/src/lib/protocol-fusion.js +417 -0
  174. package/src/lib/quantization.js +325 -0
  175. package/src/lib/response-cache.js +327 -0
  176. package/src/lib/scim-manager.js +329 -0
  177. package/src/lib/service-container.js +81 -0
  178. package/src/lib/session-consolidator.js +105 -0
  179. package/src/lib/session-hooks.js +81 -0
  180. package/src/lib/session-manager.js +329 -0
  181. package/src/lib/session-search.js +81 -0
  182. package/src/lib/session-tail.js +81 -0
  183. package/src/lib/session-usage.js +83 -0
  184. package/src/lib/siem-exporter.js +105 -0
  185. package/src/lib/skill-loader.js +377 -0
  186. package/src/lib/slot-filler.js +81 -0
  187. package/src/lib/social-graph-analytics.js +81 -0
  188. package/src/lib/social-graph.js +81 -0
  189. package/src/lib/social-manager.js +326 -0
  190. package/src/lib/sso-manager.js +332 -0
  191. package/src/lib/sub-agent-registry.js +110 -0
  192. package/src/lib/sync-manager.js +326 -0
  193. package/src/lib/task-model-selector.js +81 -0
  194. package/src/lib/tech-learning-engine.js +369 -0
  195. package/src/lib/tenant-saas.js +460 -0
  196. package/src/lib/threat-intel.js +335 -0
  197. package/src/lib/todo-manager.js +105 -0
  198. package/src/lib/token-incentive.js +293 -0
  199. package/src/lib/token-tracker.js +329 -0
  200. package/src/lib/topic-classifier.js +105 -0
  201. package/src/lib/trust-security.js +390 -0
  202. package/src/lib/ueba.js +389 -0
  203. package/src/lib/universal-runtime.js +325 -0
  204. package/src/lib/user-profile.js +81 -0
  205. package/src/lib/version-checker.js +81 -0
  206. package/src/lib/wallet-manager.js +326 -0
  207. package/src/lib/web-fetch.js +81 -0
  208. package/src/lib/workflow-engine.js +322 -0
  209. package/src/lib/zkp-engine.js +105 -0
@@ -414,3 +414,334 @@ export class PromptCompressor {
414
414
  return result;
415
415
  }
416
416
  }
417
+
418
+
419
+ // =====================================================================
420
+ // Prompt Compressor V2 governance overlay
421
+ // =====================================================================
422
+ export const PCOMP_PROFILE_MATURITY_V2 = Object.freeze({
423
+ PENDING: "pending",
424
+ ACTIVE: "active",
425
+ STALE: "stale",
426
+ ARCHIVED: "archived",
427
+ });
428
+ export const PCOMP_RUN_LIFECYCLE_V2 = Object.freeze({
429
+ QUEUED: "queued",
430
+ COMPRESSING: "compressing",
431
+ COMPRESSED: "compressed",
432
+ FAILED: "failed",
433
+ CANCELLED: "cancelled",
434
+ });
435
+ const _pcompPTrans = new Map([
436
+ [
437
+ PCOMP_PROFILE_MATURITY_V2.PENDING,
438
+ new Set([
439
+ PCOMP_PROFILE_MATURITY_V2.ACTIVE,
440
+ PCOMP_PROFILE_MATURITY_V2.ARCHIVED,
441
+ ]),
442
+ ],
443
+ [
444
+ PCOMP_PROFILE_MATURITY_V2.ACTIVE,
445
+ new Set([
446
+ PCOMP_PROFILE_MATURITY_V2.STALE,
447
+ PCOMP_PROFILE_MATURITY_V2.ARCHIVED,
448
+ ]),
449
+ ],
450
+ [
451
+ PCOMP_PROFILE_MATURITY_V2.STALE,
452
+ new Set([
453
+ PCOMP_PROFILE_MATURITY_V2.ACTIVE,
454
+ PCOMP_PROFILE_MATURITY_V2.ARCHIVED,
455
+ ]),
456
+ ],
457
+ [PCOMP_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
458
+ ]);
459
+ const _pcompPTerminal = new Set([PCOMP_PROFILE_MATURITY_V2.ARCHIVED]);
460
+ const _pcompRTrans = new Map([
461
+ [
462
+ PCOMP_RUN_LIFECYCLE_V2.QUEUED,
463
+ new Set([
464
+ PCOMP_RUN_LIFECYCLE_V2.COMPRESSING,
465
+ PCOMP_RUN_LIFECYCLE_V2.CANCELLED,
466
+ ]),
467
+ ],
468
+ [
469
+ PCOMP_RUN_LIFECYCLE_V2.COMPRESSING,
470
+ new Set([
471
+ PCOMP_RUN_LIFECYCLE_V2.COMPRESSED,
472
+ PCOMP_RUN_LIFECYCLE_V2.FAILED,
473
+ PCOMP_RUN_LIFECYCLE_V2.CANCELLED,
474
+ ]),
475
+ ],
476
+ [PCOMP_RUN_LIFECYCLE_V2.COMPRESSED, new Set()],
477
+ [PCOMP_RUN_LIFECYCLE_V2.FAILED, new Set()],
478
+ [PCOMP_RUN_LIFECYCLE_V2.CANCELLED, new Set()],
479
+ ]);
480
+ const _pcompPsV2 = new Map();
481
+ const _pcompRsV2 = new Map();
482
+ let _pcompMaxActive = 8,
483
+ _pcompMaxPending = 20,
484
+ _pcompIdleMs = 30 * 24 * 60 * 60 * 1000,
485
+ _pcompStuckMs = 60 * 1000;
486
+ function _pcompPos(n, label) {
487
+ const v = Math.floor(Number(n));
488
+ if (!Number.isFinite(v) || v <= 0)
489
+ throw new Error(`${label} must be positive integer`);
490
+ return v;
491
+ }
492
+ function _pcompCheckP(from, to) {
493
+ const a = _pcompPTrans.get(from);
494
+ if (!a || !a.has(to))
495
+ throw new Error(`invalid pcomp profile transition ${from} → ${to}`);
496
+ }
497
+ function _pcompCheckR(from, to) {
498
+ const a = _pcompRTrans.get(from);
499
+ if (!a || !a.has(to))
500
+ throw new Error(`invalid pcomp run transition ${from} → ${to}`);
501
+ }
502
+ function _pcompCountActive(owner) {
503
+ let c = 0;
504
+ for (const p of _pcompPsV2.values())
505
+ if (p.owner === owner && p.status === PCOMP_PROFILE_MATURITY_V2.ACTIVE) c++;
506
+ return c;
507
+ }
508
+ function _pcompCountPending(profileId) {
509
+ let c = 0;
510
+ for (const r of _pcompRsV2.values())
511
+ if (
512
+ r.profileId === profileId &&
513
+ (r.status === PCOMP_RUN_LIFECYCLE_V2.QUEUED ||
514
+ r.status === PCOMP_RUN_LIFECYCLE_V2.COMPRESSING)
515
+ )
516
+ c++;
517
+ return c;
518
+ }
519
+ export function setMaxActivePcompProfilesPerOwnerV2(n) {
520
+ _pcompMaxActive = _pcompPos(n, "maxActivePcompProfilesPerOwner");
521
+ }
522
+ export function getMaxActivePcompProfilesPerOwnerV2() {
523
+ return _pcompMaxActive;
524
+ }
525
+ export function setMaxPendingPcompRunsPerProfileV2(n) {
526
+ _pcompMaxPending = _pcompPos(n, "maxPendingPcompRunsPerProfile");
527
+ }
528
+ export function getMaxPendingPcompRunsPerProfileV2() {
529
+ return _pcompMaxPending;
530
+ }
531
+ export function setPcompProfileIdleMsV2(n) {
532
+ _pcompIdleMs = _pcompPos(n, "pcompProfileIdleMs");
533
+ }
534
+ export function getPcompProfileIdleMsV2() {
535
+ return _pcompIdleMs;
536
+ }
537
+ export function setPcompRunStuckMsV2(n) {
538
+ _pcompStuckMs = _pcompPos(n, "pcompRunStuckMs");
539
+ }
540
+ export function getPcompRunStuckMsV2() {
541
+ return _pcompStuckMs;
542
+ }
543
+ export function _resetStatePromptCompressorV2() {
544
+ _pcompPsV2.clear();
545
+ _pcompRsV2.clear();
546
+ _pcompMaxActive = 8;
547
+ _pcompMaxPending = 20;
548
+ _pcompIdleMs = 30 * 24 * 60 * 60 * 1000;
549
+ _pcompStuckMs = 60 * 1000;
550
+ }
551
+ export function registerPcompProfileV2({ id, owner, variant, metadata } = {}) {
552
+ if (!id || !owner) throw new Error("id and owner required");
553
+ if (_pcompPsV2.has(id)) throw new Error(`pcomp profile ${id} already exists`);
554
+ const now = Date.now();
555
+ const p = {
556
+ id,
557
+ owner,
558
+ variant: variant || "default",
559
+ status: PCOMP_PROFILE_MATURITY_V2.PENDING,
560
+ createdAt: now,
561
+ updatedAt: now,
562
+ lastTouchedAt: now,
563
+ activatedAt: null,
564
+ archivedAt: null,
565
+ metadata: { ...(metadata || {}) },
566
+ };
567
+ _pcompPsV2.set(id, p);
568
+ return { ...p, metadata: { ...p.metadata } };
569
+ }
570
+ export function activatePcompProfileV2(id) {
571
+ const p = _pcompPsV2.get(id);
572
+ if (!p) throw new Error(`pcomp profile ${id} not found`);
573
+ const isInitial = p.status === PCOMP_PROFILE_MATURITY_V2.PENDING;
574
+ _pcompCheckP(p.status, PCOMP_PROFILE_MATURITY_V2.ACTIVE);
575
+ if (isInitial && _pcompCountActive(p.owner) >= _pcompMaxActive)
576
+ throw new Error(`max active pcomp profiles for owner ${p.owner} reached`);
577
+ const now = Date.now();
578
+ p.status = PCOMP_PROFILE_MATURITY_V2.ACTIVE;
579
+ p.updatedAt = now;
580
+ p.lastTouchedAt = now;
581
+ if (!p.activatedAt) p.activatedAt = now;
582
+ return { ...p, metadata: { ...p.metadata } };
583
+ }
584
+ export function stalePcompProfileV2(id) {
585
+ const p = _pcompPsV2.get(id);
586
+ if (!p) throw new Error(`pcomp profile ${id} not found`);
587
+ _pcompCheckP(p.status, PCOMP_PROFILE_MATURITY_V2.STALE);
588
+ p.status = PCOMP_PROFILE_MATURITY_V2.STALE;
589
+ p.updatedAt = Date.now();
590
+ return { ...p, metadata: { ...p.metadata } };
591
+ }
592
+ export function archivePcompProfileV2(id) {
593
+ const p = _pcompPsV2.get(id);
594
+ if (!p) throw new Error(`pcomp profile ${id} not found`);
595
+ _pcompCheckP(p.status, PCOMP_PROFILE_MATURITY_V2.ARCHIVED);
596
+ const now = Date.now();
597
+ p.status = PCOMP_PROFILE_MATURITY_V2.ARCHIVED;
598
+ p.updatedAt = now;
599
+ if (!p.archivedAt) p.archivedAt = now;
600
+ return { ...p, metadata: { ...p.metadata } };
601
+ }
602
+ export function touchPcompProfileV2(id) {
603
+ const p = _pcompPsV2.get(id);
604
+ if (!p) throw new Error(`pcomp profile ${id} not found`);
605
+ if (_pcompPTerminal.has(p.status))
606
+ throw new Error(`cannot touch terminal pcomp profile ${id}`);
607
+ const now = Date.now();
608
+ p.lastTouchedAt = now;
609
+ p.updatedAt = now;
610
+ return { ...p, metadata: { ...p.metadata } };
611
+ }
612
+ export function getPcompProfileV2(id) {
613
+ const p = _pcompPsV2.get(id);
614
+ if (!p) return null;
615
+ return { ...p, metadata: { ...p.metadata } };
616
+ }
617
+ export function listPcompProfilesV2() {
618
+ return [..._pcompPsV2.values()].map((p) => ({
619
+ ...p,
620
+ metadata: { ...p.metadata },
621
+ }));
622
+ }
623
+ export function createPcompRunV2({ id, profileId, input, metadata } = {}) {
624
+ if (!id || !profileId) throw new Error("id and profileId required");
625
+ if (_pcompRsV2.has(id)) throw new Error(`pcomp run ${id} already exists`);
626
+ if (!_pcompPsV2.has(profileId))
627
+ throw new Error(`pcomp profile ${profileId} not found`);
628
+ if (_pcompCountPending(profileId) >= _pcompMaxPending)
629
+ throw new Error(`max pending pcomp runs for profile ${profileId} reached`);
630
+ const now = Date.now();
631
+ const r = {
632
+ id,
633
+ profileId,
634
+ input: input || "",
635
+ status: PCOMP_RUN_LIFECYCLE_V2.QUEUED,
636
+ createdAt: now,
637
+ updatedAt: now,
638
+ startedAt: null,
639
+ settledAt: null,
640
+ metadata: { ...(metadata || {}) },
641
+ };
642
+ _pcompRsV2.set(id, r);
643
+ return { ...r, metadata: { ...r.metadata } };
644
+ }
645
+ export function compressingPcompRunV2(id) {
646
+ const r = _pcompRsV2.get(id);
647
+ if (!r) throw new Error(`pcomp run ${id} not found`);
648
+ _pcompCheckR(r.status, PCOMP_RUN_LIFECYCLE_V2.COMPRESSING);
649
+ const now = Date.now();
650
+ r.status = PCOMP_RUN_LIFECYCLE_V2.COMPRESSING;
651
+ r.updatedAt = now;
652
+ if (!r.startedAt) r.startedAt = now;
653
+ return { ...r, metadata: { ...r.metadata } };
654
+ }
655
+ export function compressPcompRunV2(id) {
656
+ const r = _pcompRsV2.get(id);
657
+ if (!r) throw new Error(`pcomp run ${id} not found`);
658
+ _pcompCheckR(r.status, PCOMP_RUN_LIFECYCLE_V2.COMPRESSED);
659
+ const now = Date.now();
660
+ r.status = PCOMP_RUN_LIFECYCLE_V2.COMPRESSED;
661
+ r.updatedAt = now;
662
+ if (!r.settledAt) r.settledAt = now;
663
+ return { ...r, metadata: { ...r.metadata } };
664
+ }
665
+ export function failPcompRunV2(id, reason) {
666
+ const r = _pcompRsV2.get(id);
667
+ if (!r) throw new Error(`pcomp run ${id} not found`);
668
+ _pcompCheckR(r.status, PCOMP_RUN_LIFECYCLE_V2.FAILED);
669
+ const now = Date.now();
670
+ r.status = PCOMP_RUN_LIFECYCLE_V2.FAILED;
671
+ r.updatedAt = now;
672
+ if (!r.settledAt) r.settledAt = now;
673
+ if (reason) r.metadata.failReason = String(reason);
674
+ return { ...r, metadata: { ...r.metadata } };
675
+ }
676
+ export function cancelPcompRunV2(id, reason) {
677
+ const r = _pcompRsV2.get(id);
678
+ if (!r) throw new Error(`pcomp run ${id} not found`);
679
+ _pcompCheckR(r.status, PCOMP_RUN_LIFECYCLE_V2.CANCELLED);
680
+ const now = Date.now();
681
+ r.status = PCOMP_RUN_LIFECYCLE_V2.CANCELLED;
682
+ r.updatedAt = now;
683
+ if (!r.settledAt) r.settledAt = now;
684
+ if (reason) r.metadata.cancelReason = String(reason);
685
+ return { ...r, metadata: { ...r.metadata } };
686
+ }
687
+ export function getPcompRunV2(id) {
688
+ const r = _pcompRsV2.get(id);
689
+ if (!r) return null;
690
+ return { ...r, metadata: { ...r.metadata } };
691
+ }
692
+ export function listPcompRunsV2() {
693
+ return [..._pcompRsV2.values()].map((r) => ({
694
+ ...r,
695
+ metadata: { ...r.metadata },
696
+ }));
697
+ }
698
+ export function autoStaleIdlePcompProfilesV2({ now } = {}) {
699
+ const t = now ?? Date.now();
700
+ const flipped = [];
701
+ for (const p of _pcompPsV2.values())
702
+ if (
703
+ p.status === PCOMP_PROFILE_MATURITY_V2.ACTIVE &&
704
+ t - p.lastTouchedAt >= _pcompIdleMs
705
+ ) {
706
+ p.status = PCOMP_PROFILE_MATURITY_V2.STALE;
707
+ p.updatedAt = t;
708
+ flipped.push(p.id);
709
+ }
710
+ return { flipped, count: flipped.length };
711
+ }
712
+ export function autoFailStuckPcompRunsV2({ now } = {}) {
713
+ const t = now ?? Date.now();
714
+ const flipped = [];
715
+ for (const r of _pcompRsV2.values())
716
+ if (
717
+ r.status === PCOMP_RUN_LIFECYCLE_V2.COMPRESSING &&
718
+ r.startedAt != null &&
719
+ t - r.startedAt >= _pcompStuckMs
720
+ ) {
721
+ r.status = PCOMP_RUN_LIFECYCLE_V2.FAILED;
722
+ r.updatedAt = t;
723
+ if (!r.settledAt) r.settledAt = t;
724
+ r.metadata.failReason = "auto-fail-stuck";
725
+ flipped.push(r.id);
726
+ }
727
+ return { flipped, count: flipped.length };
728
+ }
729
+ export function getPromptCompressorGovStatsV2() {
730
+ const profilesByStatus = {};
731
+ for (const v of Object.values(PCOMP_PROFILE_MATURITY_V2))
732
+ profilesByStatus[v] = 0;
733
+ for (const p of _pcompPsV2.values()) profilesByStatus[p.status]++;
734
+ const runsByStatus = {};
735
+ for (const v of Object.values(PCOMP_RUN_LIFECYCLE_V2)) runsByStatus[v] = 0;
736
+ for (const r of _pcompRsV2.values()) runsByStatus[r.status]++;
737
+ return {
738
+ totalPcompProfilesV2: _pcompPsV2.size,
739
+ totalPcompRunsV2: _pcompRsV2.size,
740
+ maxActivePcompProfilesPerOwner: _pcompMaxActive,
741
+ maxPendingPcompRunsPerProfile: _pcompMaxPending,
742
+ pcompProfileIdleMs: _pcompIdleMs,
743
+ pcompRunStuckMs: _pcompStuckMs,
744
+ profilesByStatus,
745
+ runsByStatus,
746
+ };
747
+ }
package/src/index.js CHANGED
@@ -13,13 +13,46 @@ import { registerNoteCommand } from "./commands/note.js";
13
13
  import { registerChatCommand } from "./commands/chat.js";
14
14
  import { registerAskCommand } from "./commands/ask.js";
15
15
  import { registerLlmCommand } from "./commands/llm.js";
16
- import { registerAgentCommand } from "./commands/agent.js";
16
+ import {
17
+ registerAgentCommand,
18
+ registerSubAgentV2Command,
19
+ registerExecBackendV2Command,
20
+ registerTodoV2Command,
21
+ registerAutoAgentV2Command,
22
+ } from "./commands/agent.js";
23
+ import { registerOrchGovCommand } from "./commands/orchgov.js";
24
+ import { registerTopicClsCommand } from "./commands/topiccls.js";
25
+ import { registerItBudgetCommand } from "./commands/itbudget.js";
26
+ import { registerPlanModeCommand } from "./commands/planmode.js";
27
+ import { registerPermCommand } from "./commands/perm.js";
28
+ import { registerUprofCommand } from "./commands/uprof.js";
29
+ import { registerSvcContCommand } from "./commands/svccont.js";
30
+ import { registerTmsCommand } from "./commands/tms.js";
31
+ import { registerSlotfillCommand } from "./commands/slotfill.js";
32
+ import { registerWebfetchCommand } from "./commands/webfetch.js";
33
+ import { registerMeminjCommand } from "./commands/meminj.js";
34
+ import { registerSeshsearchCommand } from "./commands/seshsearch.js";
35
+ import { registerSeshtailCommand } from "./commands/seshtail.js";
36
+ import { registerSeshuCommand } from "./commands/seshu.js";
37
+ import { registerSeshhookCommand } from "./commands/seshhook.js";
38
+ import { registerMcpscafCommand } from "./commands/mcpscaf.js";
39
+ import { registerFflagCommand } from "./commands/fflag.js";
40
+ import { registerPromcompCommand } from "./commands/promcomp.js";
41
+ import { registerCcronCommand } from "./commands/ccron.js";
42
+ import { registerVcheckCommand } from "./commands/vcheck.js";
43
+ import { registerPdfpCommand } from "./commands/pdfp.js";
44
+ import { registerBm25Command } from "./commands/bm25.js";
45
+ import { registerComptCommand } from "./commands/compt.js";
46
+ import { registerSganalCommand } from "./commands/sganal.js";
17
47
  import { registerStreamCommand } from "./commands/stream.js";
18
48
  import { registerSkillCommand } from "./commands/skill.js";
19
49
  import { registerSearchCommand } from "./commands/search.js";
20
50
  import { registerTokensCommand } from "./commands/tokens.js";
21
51
  import { registerMemoryCommand } from "./commands/memory.js";
52
+ import { registerPermMemCommand } from "./commands/permmem.js";
53
+ import { registerRCacheCommand } from "./commands/rcache.js";
22
54
  import { registerSessionCommand } from "./commands/session.js";
55
+ import { registerConsolCommand } from "./commands/consol.js";
23
56
  import { registerImportCommand } from "./commands/import.js";
24
57
  import { registerExportCommand } from "./commands/export.js";
25
58
  import { registerGitCommand } from "./commands/git.js";
@@ -209,6 +242,34 @@ export function createProgram() {
209
242
  registerAskCommand(program);
210
243
  registerLlmCommand(program);
211
244
  registerAgentCommand(program);
245
+ registerSubAgentV2Command(program);
246
+ registerExecBackendV2Command(program);
247
+ registerTodoV2Command(program);
248
+ registerAutoAgentV2Command(program);
249
+ registerOrchGovCommand(program);
250
+ registerTopicClsCommand(program);
251
+ registerItBudgetCommand(program);
252
+ registerPlanModeCommand(program);
253
+ registerPermCommand(program);
254
+ registerUprofCommand(program);
255
+ registerSvcContCommand(program);
256
+ registerTmsCommand(program);
257
+ registerSlotfillCommand(program);
258
+ registerWebfetchCommand(program);
259
+ registerMeminjCommand(program);
260
+ registerSeshsearchCommand(program);
261
+ registerSeshtailCommand(program);
262
+ registerSeshuCommand(program);
263
+ registerSeshhookCommand(program);
264
+ registerMcpscafCommand(program);
265
+ registerFflagCommand(program);
266
+ registerPromcompCommand(program);
267
+ registerCcronCommand(program);
268
+ registerVcheckCommand(program);
269
+ registerPdfpCommand(program);
270
+ registerBm25Command(program);
271
+ registerComptCommand(program);
272
+ registerSganalCommand(program);
212
273
  registerStreamCommand(program);
213
274
  registerSkillCommand(program);
214
275
 
@@ -216,7 +277,10 @@ export function createProgram() {
216
277
  registerSearchCommand(program);
217
278
  registerTokensCommand(program);
218
279
  registerMemoryCommand(program);
280
+ registerPermMemCommand(program);
281
+ registerRCacheCommand(program);
219
282
  registerSessionCommand(program);
283
+ registerConsolCommand(program);
220
284
 
221
285
  // Phase 2: Knowledge & content management
222
286
  registerImportCommand(program);
@@ -820,3 +820,108 @@ export function _resetV2State() {
820
820
  }
821
821
 
822
822
  export { _v2Tasks, _v2Cards, _v2TypedSubs };
823
+
824
+
825
+ // ===== V2 Surface: A2A Protocol governance overlay (CLI v0.136.0) =====
826
+ export const A2A_AGENT_MATURITY_V2 = Object.freeze({
827
+ PENDING: "pending", ACTIVE: "active", SUSPENDED: "suspended", RETIRED: "retired",
828
+ });
829
+ export const A2A_MESSAGE_LIFECYCLE_V2 = Object.freeze({
830
+ QUEUED: "queued", SENDING: "sending", DELIVERED: "delivered", FAILED: "failed", CANCELLED: "cancelled",
831
+ });
832
+
833
+ const _a2aAgentTrans = new Map([
834
+ [A2A_AGENT_MATURITY_V2.PENDING, new Set([A2A_AGENT_MATURITY_V2.ACTIVE, A2A_AGENT_MATURITY_V2.RETIRED])],
835
+ [A2A_AGENT_MATURITY_V2.ACTIVE, new Set([A2A_AGENT_MATURITY_V2.SUSPENDED, A2A_AGENT_MATURITY_V2.RETIRED])],
836
+ [A2A_AGENT_MATURITY_V2.SUSPENDED, new Set([A2A_AGENT_MATURITY_V2.ACTIVE, A2A_AGENT_MATURITY_V2.RETIRED])],
837
+ [A2A_AGENT_MATURITY_V2.RETIRED, new Set()],
838
+ ]);
839
+ const _a2aAgentTerminal = new Set([A2A_AGENT_MATURITY_V2.RETIRED]);
840
+ const _a2aMsgTrans = new Map([
841
+ [A2A_MESSAGE_LIFECYCLE_V2.QUEUED, new Set([A2A_MESSAGE_LIFECYCLE_V2.SENDING, A2A_MESSAGE_LIFECYCLE_V2.CANCELLED])],
842
+ [A2A_MESSAGE_LIFECYCLE_V2.SENDING, new Set([A2A_MESSAGE_LIFECYCLE_V2.DELIVERED, A2A_MESSAGE_LIFECYCLE_V2.FAILED, A2A_MESSAGE_LIFECYCLE_V2.CANCELLED])],
843
+ [A2A_MESSAGE_LIFECYCLE_V2.DELIVERED, new Set()],
844
+ [A2A_MESSAGE_LIFECYCLE_V2.FAILED, new Set()],
845
+ [A2A_MESSAGE_LIFECYCLE_V2.CANCELLED, new Set()],
846
+ ]);
847
+
848
+ const _a2aAgents = new Map();
849
+ const _a2aMsgs = new Map();
850
+ let _a2aMaxActivePerOwner = 12;
851
+ let _a2aMaxPendingPerAgent = 20;
852
+ let _a2aAgentIdleMs = 6 * 60 * 60 * 1000;
853
+ let _a2aMsgStuckMs = 3 * 60 * 1000;
854
+
855
+ function _a2aPos(n, lbl) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${lbl} must be positive integer`); return v; }
856
+
857
+ export function setMaxActiveA2aAgentsPerOwnerV2(n) { _a2aMaxActivePerOwner = _a2aPos(n, "maxActiveA2aAgentsPerOwner"); }
858
+ export function getMaxActiveA2aAgentsPerOwnerV2() { return _a2aMaxActivePerOwner; }
859
+ export function setMaxPendingA2aMessagesPerAgentV2(n) { _a2aMaxPendingPerAgent = _a2aPos(n, "maxPendingA2aMessagesPerAgent"); }
860
+ export function getMaxPendingA2aMessagesPerAgentV2() { return _a2aMaxPendingPerAgent; }
861
+ export function setA2aAgentIdleMsV2(n) { _a2aAgentIdleMs = _a2aPos(n, "a2aAgentIdleMs"); }
862
+ export function getA2aAgentIdleMsV2() { return _a2aAgentIdleMs; }
863
+ export function setA2aMessageStuckMsV2(n) { _a2aMsgStuckMs = _a2aPos(n, "a2aMessageStuckMs"); }
864
+ export function getA2aMessageStuckMsV2() { return _a2aMsgStuckMs; }
865
+
866
+ export function _resetStateA2aProtocolV2() {
867
+ _a2aAgents.clear(); _a2aMsgs.clear();
868
+ _a2aMaxActivePerOwner = 12; _a2aMaxPendingPerAgent = 20;
869
+ _a2aAgentIdleMs = 6 * 60 * 60 * 1000; _a2aMsgStuckMs = 3 * 60 * 1000;
870
+ }
871
+
872
+ export function registerA2aAgentV2({ id, owner, capabilities, metadata } = {}) {
873
+ if (!id || typeof id !== "string") throw new Error("id is required");
874
+ if (!owner || typeof owner !== "string") throw new Error("owner is required");
875
+ if (_a2aAgents.has(id)) throw new Error(`a2a agent ${id} already registered`);
876
+ const now = Date.now();
877
+ const a = { id, owner, capabilities: Array.isArray(capabilities) ? [...capabilities] : [], status: A2A_AGENT_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, retiredAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
878
+ _a2aAgents.set(id, a);
879
+ return { ...a, capabilities: [...a.capabilities], metadata: { ...a.metadata } };
880
+ }
881
+ function _a2aCheckA(from, to) { const al = _a2aAgentTrans.get(from); if (!al || !al.has(to)) throw new Error(`invalid a2a agent transition ${from} → ${to}`); }
882
+ function _a2aCountActive(owner) { let n = 0; for (const a of _a2aAgents.values()) if (a.owner === owner && a.status === A2A_AGENT_MATURITY_V2.ACTIVE) n++; return n; }
883
+
884
+ export function activateA2aAgentV2(id) {
885
+ const a = _a2aAgents.get(id); if (!a) throw new Error(`a2a agent ${id} not found`);
886
+ _a2aCheckA(a.status, A2A_AGENT_MATURITY_V2.ACTIVE);
887
+ const recovery = a.status === A2A_AGENT_MATURITY_V2.SUSPENDED;
888
+ if (!recovery) { const c = _a2aCountActive(a.owner); if (c >= _a2aMaxActivePerOwner) throw new Error(`max active a2a agents per owner (${_a2aMaxActivePerOwner}) reached for ${a.owner}`); }
889
+ const now = Date.now(); a.status = A2A_AGENT_MATURITY_V2.ACTIVE; a.updatedAt = now; a.lastTouchedAt = now; if (!a.activatedAt) a.activatedAt = now;
890
+ return { ...a, capabilities: [...a.capabilities], metadata: { ...a.metadata } };
891
+ }
892
+ export function suspendA2aAgentV2(id) { const a = _a2aAgents.get(id); if (!a) throw new Error(`a2a agent ${id} not found`); _a2aCheckA(a.status, A2A_AGENT_MATURITY_V2.SUSPENDED); a.status = A2A_AGENT_MATURITY_V2.SUSPENDED; a.updatedAt = Date.now(); return { ...a, capabilities: [...a.capabilities], metadata: { ...a.metadata } }; }
893
+ export function retireA2aAgentV2(id) { const a = _a2aAgents.get(id); if (!a) throw new Error(`a2a agent ${id} not found`); _a2aCheckA(a.status, A2A_AGENT_MATURITY_V2.RETIRED); const now = Date.now(); a.status = A2A_AGENT_MATURITY_V2.RETIRED; a.updatedAt = now; if (!a.retiredAt) a.retiredAt = now; return { ...a, capabilities: [...a.capabilities], metadata: { ...a.metadata } }; }
894
+ export function touchA2aAgentV2(id) { const a = _a2aAgents.get(id); if (!a) throw new Error(`a2a agent ${id} not found`); if (_a2aAgentTerminal.has(a.status)) throw new Error(`cannot touch terminal a2a agent ${id}`); const now = Date.now(); a.lastTouchedAt = now; a.updatedAt = now; return { ...a, capabilities: [...a.capabilities], metadata: { ...a.metadata } }; }
895
+ export function getA2aAgentV2(id) { const a = _a2aAgents.get(id); if (!a) return null; return { ...a, capabilities: [...a.capabilities], metadata: { ...a.metadata } }; }
896
+ export function listA2aAgentsV2() { return [..._a2aAgents.values()].map((a) => ({ ...a, capabilities: [...a.capabilities], metadata: { ...a.metadata } })); }
897
+
898
+ function _a2aCountPending(aid) { let n = 0; for (const m of _a2aMsgs.values()) if (m.agentId === aid && (m.status === A2A_MESSAGE_LIFECYCLE_V2.QUEUED || m.status === A2A_MESSAGE_LIFECYCLE_V2.SENDING)) n++; return n; }
899
+
900
+ export function createA2aMessageV2({ id, agentId, peerId, payload, metadata } = {}) {
901
+ if (!id || typeof id !== "string") throw new Error("id is required");
902
+ if (!agentId || typeof agentId !== "string") throw new Error("agentId is required");
903
+ if (_a2aMsgs.has(id)) throw new Error(`a2a message ${id} already exists`);
904
+ if (!_a2aAgents.has(agentId)) throw new Error(`a2a agent ${agentId} not found`);
905
+ const pending = _a2aCountPending(agentId);
906
+ if (pending >= _a2aMaxPendingPerAgent) throw new Error(`max pending a2a messages per agent (${_a2aMaxPendingPerAgent}) reached for ${agentId}`);
907
+ const now = Date.now();
908
+ const m = { id, agentId, peerId: peerId || "", payload: payload || "", status: A2A_MESSAGE_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
909
+ _a2aMsgs.set(id, m);
910
+ return { ...m, metadata: { ...m.metadata } };
911
+ }
912
+ function _a2aCheckM(from, to) { const al = _a2aMsgTrans.get(from); if (!al || !al.has(to)) throw new Error(`invalid a2a message transition ${from} → ${to}`); }
913
+ export function startA2aMessageV2(id) { const m = _a2aMsgs.get(id); if (!m) throw new Error(`a2a message ${id} not found`); _a2aCheckM(m.status, A2A_MESSAGE_LIFECYCLE_V2.SENDING); const now = Date.now(); m.status = A2A_MESSAGE_LIFECYCLE_V2.SENDING; m.updatedAt = now; if (!m.startedAt) m.startedAt = now; return { ...m, metadata: { ...m.metadata } }; }
914
+ export function deliverA2aMessageV2(id) { const m = _a2aMsgs.get(id); if (!m) throw new Error(`a2a message ${id} not found`); _a2aCheckM(m.status, A2A_MESSAGE_LIFECYCLE_V2.DELIVERED); const now = Date.now(); m.status = A2A_MESSAGE_LIFECYCLE_V2.DELIVERED; m.updatedAt = now; if (!m.settledAt) m.settledAt = now; return { ...m, metadata: { ...m.metadata } }; }
915
+ export function failA2aMessageV2(id, reason) { const m = _a2aMsgs.get(id); if (!m) throw new Error(`a2a message ${id} not found`); _a2aCheckM(m.status, A2A_MESSAGE_LIFECYCLE_V2.FAILED); const now = Date.now(); m.status = A2A_MESSAGE_LIFECYCLE_V2.FAILED; m.updatedAt = now; if (!m.settledAt) m.settledAt = now; if (reason) m.metadata.failReason = String(reason); return { ...m, metadata: { ...m.metadata } }; }
916
+ export function cancelA2aMessageV2(id, reason) { const m = _a2aMsgs.get(id); if (!m) throw new Error(`a2a message ${id} not found`); _a2aCheckM(m.status, A2A_MESSAGE_LIFECYCLE_V2.CANCELLED); const now = Date.now(); m.status = A2A_MESSAGE_LIFECYCLE_V2.CANCELLED; m.updatedAt = now; if (!m.settledAt) m.settledAt = now; if (reason) m.metadata.cancelReason = String(reason); return { ...m, metadata: { ...m.metadata } }; }
917
+ export function getA2aMessageV2(id) { const m = _a2aMsgs.get(id); if (!m) return null; return { ...m, metadata: { ...m.metadata } }; }
918
+ export function listA2aMessagesV2() { return [..._a2aMsgs.values()].map((m) => ({ ...m, metadata: { ...m.metadata } })); }
919
+
920
+ export function autoSuspendIdleA2aAgentsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const a of _a2aAgents.values()) if (a.status === A2A_AGENT_MATURITY_V2.ACTIVE && (t - a.lastTouchedAt) >= _a2aAgentIdleMs) { a.status = A2A_AGENT_MATURITY_V2.SUSPENDED; a.updatedAt = t; flipped.push(a.id); } return { flipped, count: flipped.length }; }
921
+ export function autoFailStuckA2aMessagesV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const m of _a2aMsgs.values()) if (m.status === A2A_MESSAGE_LIFECYCLE_V2.SENDING && m.startedAt != null && (t - m.startedAt) >= _a2aMsgStuckMs) { m.status = A2A_MESSAGE_LIFECYCLE_V2.FAILED; m.updatedAt = t; if (!m.settledAt) m.settledAt = t; m.metadata.failReason = "auto-fail-stuck"; flipped.push(m.id); } return { flipped, count: flipped.length }; }
922
+
923
+ export function getA2aProtocolGovStatsV2() {
924
+ const agentsByStatus = {}; for (const s of Object.values(A2A_AGENT_MATURITY_V2)) agentsByStatus[s] = 0; for (const a of _a2aAgents.values()) agentsByStatus[a.status]++;
925
+ const messagesByStatus = {}; for (const s of Object.values(A2A_MESSAGE_LIFECYCLE_V2)) messagesByStatus[s] = 0; for (const m of _a2aMsgs.values()) messagesByStatus[m.status]++;
926
+ return { totalAgentsV2: _a2aAgents.size, totalMessagesV2: _a2aMsgs.size, maxActiveA2aAgentsPerOwner: _a2aMaxActivePerOwner, maxPendingA2aMessagesPerAgent: _a2aMaxPendingPerAgent, a2aAgentIdleMs: _a2aAgentIdleMs, a2aMessageStuckMs: _a2aMsgStuckMs, agentsByStatus, messagesByStatus };
927
+ }
@@ -621,3 +621,108 @@ export function _resetState() {
621
621
  }
622
622
 
623
623
  export const _constants = { PUBLIC_AUDIENCE, CONTEXT, DEFAULT_ORIGIN };
624
+
625
+
626
+ // ===== V2 Surface: ActivityPub Bridge governance overlay (CLI v0.135.0) =====
627
+ export const AP_ACTOR_MATURITY_V2 = Object.freeze({
628
+ PENDING: "pending", ACTIVE: "active", SUSPENDED: "suspended", DEACTIVATED: "deactivated",
629
+ });
630
+ export const AP_ACTIVITY_LIFECYCLE_V2 = Object.freeze({
631
+ QUEUED: "queued", DELIVERING: "delivering", DELIVERED: "delivered", FAILED: "failed", CANCELLED: "cancelled",
632
+ });
633
+
634
+ 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])],
638
+ [AP_ACTOR_MATURITY_V2.DEACTIVATED, new Set()],
639
+ ]);
640
+ const _apActorTerminal = new Set([AP_ACTOR_MATURITY_V2.DEACTIVATED]);
641
+ 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])],
644
+ [AP_ACTIVITY_LIFECYCLE_V2.DELIVERED, new Set()],
645
+ [AP_ACTIVITY_LIFECYCLE_V2.FAILED, new Set()],
646
+ [AP_ACTIVITY_LIFECYCLE_V2.CANCELLED, new Set()],
647
+ ]);
648
+
649
+ const _apActors = new Map();
650
+ const _apActs = new Map();
651
+ let _apMaxActivePerOwner = 15;
652
+ let _apMaxPendingPerActor = 25;
653
+ let _apActorIdleMs = 24 * 60 * 60 * 1000;
654
+ let _apActStuckMs = 3 * 60 * 1000;
655
+
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; }
657
+
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; }
666
+
667
+ export function _resetStateActivityPubBridgeV2() {
668
+ _apActors.clear(); _apActs.clear();
669
+ _apMaxActivePerOwner = 15; _apMaxPendingPerActor = 25;
670
+ _apActorIdleMs = 24 * 60 * 60 * 1000; _apActStuckMs = 3 * 60 * 1000;
671
+ }
672
+
673
+ export function registerApActorV2({ id, owner, handle, metadata } = {}) {
674
+ if (!id || typeof id !== "string") throw new Error("id is required");
675
+ if (!owner || typeof owner !== "string") throw new Error("owner is required");
676
+ if (_apActors.has(id)) throw new Error(`activitypub actor ${id} already registered`);
677
+ 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 || {}) } };
679
+ _apActors.set(id, a);
680
+ return { ...a, metadata: { ...a.metadata } };
681
+ }
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; }
684
+
685
+ export function activateApActorV2(id) {
686
+ const a = _apActors.get(id); if (!a) throw new Error(`activitypub actor ${id} not found`);
687
+ _apCheckA(a.status, AP_ACTOR_MATURITY_V2.ACTIVE);
688
+ 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;
691
+ return { ...a, metadata: { ...a.metadata } };
692
+ }
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 } })); }
698
+
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; }
700
+
701
+ export function createApActivityV2({ id, actorId, kind, metadata } = {}) {
702
+ 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`);
706
+ const pending = _apCountPending(actorId);
707
+ if (pending >= _apMaxPendingPerActor) throw new Error(`max pending activitypub activities per actor (${_apMaxPendingPerActor}) reached for ${actorId}`);
708
+ 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 || {}) } };
710
+ _apActs.set(id, ac);
711
+ return { ...ac, metadata: { ...ac.metadata } };
712
+ }
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 } })); }
720
+
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 }; }
723
+
724
+ 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 };
728
+ }