chainlesschain 0.143.0 → 0.145.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 (133) hide show
  1. package/package.json +1 -1
  2. package/src/commands/a2a.js +196 -28
  3. package/src/commands/activitypub.js +157 -27
  4. package/src/commands/agent.js +587 -103
  5. package/src/commands/audit.js +206 -0
  6. package/src/commands/bi.js +152 -27
  7. package/src/commands/bm25.js +111 -27
  8. package/src/commands/browse.js +190 -29
  9. package/src/commands/ccron.js +128 -28
  10. package/src/commands/chat.js +207 -0
  11. package/src/commands/compliance.js +414 -34
  12. package/src/commands/compt.js +127 -29
  13. package/src/commands/consol.js +8 -2
  14. package/src/commands/cowork.js +580 -34
  15. package/src/commands/crosschain.js +182 -28
  16. package/src/commands/dao.js +182 -28
  17. package/src/commands/dlp.js +172 -27
  18. package/src/commands/economy.js +205 -50
  19. package/src/commands/evolution.js +203 -50
  20. package/src/commands/evomap.js +182 -27
  21. package/src/commands/fflag.js +74 -22
  22. package/src/commands/git.js +177 -37
  23. package/src/commands/hardening.js +209 -0
  24. package/src/commands/hmemory.js +204 -50
  25. package/src/commands/incentive.js +209 -0
  26. package/src/commands/inference.js +170 -34
  27. package/src/commands/itbudget.js +149 -33
  28. package/src/commands/kg.js +206 -0
  29. package/src/commands/lowcode.js +195 -38
  30. package/src/commands/marketplace.js +206 -0
  31. package/src/commands/matrix.js +179 -27
  32. package/src/commands/mcpscaf.js +145 -33
  33. package/src/commands/meminj.js +145 -33
  34. package/src/commands/nostr.js +178 -27
  35. package/src/commands/orchestrate.js +217 -0
  36. package/src/commands/orchgov.js +144 -33
  37. package/src/commands/pdfp.js +109 -27
  38. package/src/commands/perf.js +169 -32
  39. package/src/commands/perm.js +144 -33
  40. package/src/commands/pipeline.js +207 -52
  41. package/src/commands/planmode.js +141 -32
  42. package/src/commands/privacy.js +203 -0
  43. package/src/commands/promcomp.js +111 -27
  44. package/src/commands/recommend.js +177 -34
  45. package/src/commands/reputation.js +208 -0
  46. package/src/commands/sandbox.js +206 -0
  47. package/src/commands/seshhook.js +145 -33
  48. package/src/commands/seshsearch.js +141 -33
  49. package/src/commands/seshtail.js +144 -33
  50. package/src/commands/seshu.js +152 -33
  51. package/src/commands/sganal.js +123 -29
  52. package/src/commands/siem.js +201 -34
  53. package/src/commands/sla.js +212 -0
  54. package/src/commands/slotfill.js +146 -33
  55. package/src/commands/social.js +157 -32
  56. package/src/commands/stress.js +206 -0
  57. package/src/commands/svccont.js +145 -33
  58. package/src/commands/terraform.js +206 -0
  59. package/src/commands/tms.js +171 -33
  60. package/src/commands/topiccls.js +146 -33
  61. package/src/commands/uprof.js +141 -32
  62. package/src/commands/vcheck.js +122 -28
  63. package/src/commands/webfetch.js +141 -32
  64. package/src/commands/zkp.js +184 -28
  65. package/src/index.js +40 -0
  66. package/src/lib/a2a-protocol.js +319 -51
  67. package/src/lib/activitypub-bridge.js +288 -50
  68. package/src/lib/agent-economy.js +304 -51
  69. package/src/lib/app-builder.js +279 -46
  70. package/src/lib/audit-logger.js +321 -0
  71. package/src/lib/autonomous-agent.js +284 -48
  72. package/src/lib/bi-engine.js +283 -49
  73. package/src/lib/bm25-search.js +301 -49
  74. package/src/lib/browser-automation.js +296 -49
  75. package/src/lib/chat-core.js +336 -0
  76. package/src/lib/claude-code-bridge.js +341 -0
  77. package/src/lib/compliance-framework-reporter.js +305 -51
  78. package/src/lib/compliance-manager.js +330 -0
  79. package/src/lib/compression-telemetry.js +301 -49
  80. package/src/lib/content-recommender.js +317 -52
  81. package/src/lib/cowork-cron.js +298 -49
  82. package/src/lib/cowork-learning.js +333 -0
  83. package/src/lib/cowork-task-runner.js +308 -51
  84. package/src/lib/cowork-workflow.js +327 -0
  85. package/src/lib/cross-chain.js +311 -51
  86. package/src/lib/dao-governance.js +280 -46
  87. package/src/lib/dlp-engine.js +287 -49
  88. package/src/lib/evolution-system.js +278 -47
  89. package/src/lib/evomap-manager.js +280 -46
  90. package/src/lib/execution-backend.js +294 -48
  91. package/src/lib/feature-flags.js +294 -49
  92. package/src/lib/git-integration.js +285 -47
  93. package/src/lib/hardening-manager.js +341 -0
  94. package/src/lib/hierarchical-memory.js +284 -48
  95. package/src/lib/inference-network.js +308 -51
  96. package/src/lib/iteration-budget.js +302 -50
  97. package/src/lib/knowledge-graph.js +333 -0
  98. package/src/lib/matrix-bridge.js +281 -47
  99. package/src/lib/mcp-scaffold.js +318 -54
  100. package/src/lib/memory-injection.js +288 -49
  101. package/src/lib/nostr-bridge.js +286 -49
  102. package/src/lib/orchestrator.js +293 -48
  103. package/src/lib/pdf-parser.js +298 -49
  104. package/src/lib/perf-tuning.js +309 -50
  105. package/src/lib/permission-engine.js +287 -49
  106. package/src/lib/pipeline-orchestrator.js +289 -49
  107. package/src/lib/plan-mode.js +298 -51
  108. package/src/lib/privacy-computing.js +335 -0
  109. package/src/lib/reputation-optimizer.js +340 -0
  110. package/src/lib/sandbox-v2.js +327 -0
  111. package/src/lib/service-container.js +313 -52
  112. package/src/lib/session-consolidator.js +296 -49
  113. package/src/lib/session-hooks.js +312 -53
  114. package/src/lib/session-search.js +304 -51
  115. package/src/lib/session-tail.js +288 -49
  116. package/src/lib/session-usage.js +298 -52
  117. package/src/lib/siem-exporter.js +298 -51
  118. package/src/lib/skill-marketplace.js +345 -0
  119. package/src/lib/sla-manager.js +341 -0
  120. package/src/lib/slot-filler.js +303 -51
  121. package/src/lib/social-graph-analytics.js +295 -49
  122. package/src/lib/social-graph.js +272 -49
  123. package/src/lib/stress-tester.js +342 -0
  124. package/src/lib/sub-agent-registry.js +302 -53
  125. package/src/lib/task-model-selector.js +302 -50
  126. package/src/lib/terraform-manager.js +333 -0
  127. package/src/lib/todo-manager.js +281 -47
  128. package/src/lib/token-incentive.js +341 -0
  129. package/src/lib/topic-classifier.js +297 -49
  130. package/src/lib/user-profile.js +294 -50
  131. package/src/lib/version-checker.js +304 -50
  132. package/src/lib/web-fetch.js +292 -51
  133. package/src/lib/zkp-engine.js +286 -49
@@ -547,81 +547,328 @@ export function destroyPlanModeManager() {
547
547
 
548
548
  // ===== V2 Surface: Plan Mode governance overlay (CLI v0.141.0) =====
549
549
  export const PLAN_PROFILE_MATURITY_V2 = Object.freeze({
550
- PENDING: "pending", ACTIVE: "active", PAUSED: "paused", ARCHIVED: "archived",
550
+ PENDING: "pending",
551
+ ACTIVE: "active",
552
+ PAUSED: "paused",
553
+ ARCHIVED: "archived",
551
554
  });
552
555
  export const PLAN_STEP_LIFECYCLE_V2 = Object.freeze({
553
- QUEUED: "queued", RUNNING: "running", COMPLETED: "completed", FAILED: "failed", CANCELLED: "cancelled",
556
+ QUEUED: "queued",
557
+ RUNNING: "running",
558
+ COMPLETED: "completed",
559
+ FAILED: "failed",
560
+ CANCELLED: "cancelled",
554
561
  });
555
562
  const _planPTrans = new Map([
556
- [PLAN_PROFILE_MATURITY_V2.PENDING, new Set([PLAN_PROFILE_MATURITY_V2.ACTIVE, PLAN_PROFILE_MATURITY_V2.ARCHIVED])],
557
- [PLAN_PROFILE_MATURITY_V2.ACTIVE, new Set([PLAN_PROFILE_MATURITY_V2.PAUSED, PLAN_PROFILE_MATURITY_V2.ARCHIVED])],
558
- [PLAN_PROFILE_MATURITY_V2.PAUSED, new Set([PLAN_PROFILE_MATURITY_V2.ACTIVE, PLAN_PROFILE_MATURITY_V2.ARCHIVED])],
563
+ [
564
+ PLAN_PROFILE_MATURITY_V2.PENDING,
565
+ new Set([
566
+ PLAN_PROFILE_MATURITY_V2.ACTIVE,
567
+ PLAN_PROFILE_MATURITY_V2.ARCHIVED,
568
+ ]),
569
+ ],
570
+ [
571
+ PLAN_PROFILE_MATURITY_V2.ACTIVE,
572
+ new Set([
573
+ PLAN_PROFILE_MATURITY_V2.PAUSED,
574
+ PLAN_PROFILE_MATURITY_V2.ARCHIVED,
575
+ ]),
576
+ ],
577
+ [
578
+ PLAN_PROFILE_MATURITY_V2.PAUSED,
579
+ new Set([
580
+ PLAN_PROFILE_MATURITY_V2.ACTIVE,
581
+ PLAN_PROFILE_MATURITY_V2.ARCHIVED,
582
+ ]),
583
+ ],
559
584
  [PLAN_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
560
585
  ]);
561
586
  const _planPTerminal = new Set([PLAN_PROFILE_MATURITY_V2.ARCHIVED]);
562
587
  const _planSTrans = new Map([
563
- [PLAN_STEP_LIFECYCLE_V2.QUEUED, new Set([PLAN_STEP_LIFECYCLE_V2.RUNNING, PLAN_STEP_LIFECYCLE_V2.CANCELLED])],
564
- [PLAN_STEP_LIFECYCLE_V2.RUNNING, new Set([PLAN_STEP_LIFECYCLE_V2.COMPLETED, PLAN_STEP_LIFECYCLE_V2.FAILED, PLAN_STEP_LIFECYCLE_V2.CANCELLED])],
588
+ [
589
+ PLAN_STEP_LIFECYCLE_V2.QUEUED,
590
+ new Set([PLAN_STEP_LIFECYCLE_V2.RUNNING, PLAN_STEP_LIFECYCLE_V2.CANCELLED]),
591
+ ],
592
+ [
593
+ PLAN_STEP_LIFECYCLE_V2.RUNNING,
594
+ new Set([
595
+ PLAN_STEP_LIFECYCLE_V2.COMPLETED,
596
+ PLAN_STEP_LIFECYCLE_V2.FAILED,
597
+ PLAN_STEP_LIFECYCLE_V2.CANCELLED,
598
+ ]),
599
+ ],
565
600
  [PLAN_STEP_LIFECYCLE_V2.COMPLETED, new Set()],
566
601
  [PLAN_STEP_LIFECYCLE_V2.FAILED, new Set()],
567
602
  [PLAN_STEP_LIFECYCLE_V2.CANCELLED, new Set()],
568
603
  ]);
569
604
  const _planPsV2 = new Map();
570
605
  const _planSsV2 = new Map();
571
- let _planMaxActivePerOwner = 6, _planMaxPendingStepsPerProfile = 15, _planIdleMs = 7 * 24 * 60 * 60 * 1000, _planStuckMs = 30 * 60 * 1000;
572
- function _planPos(n, label) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${label} must be positive integer`); return v; }
573
- function _planCheckP(from, to) { const a = _planPTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid plan profile transition ${from} → ${to}`); }
574
- function _planCheckS(from, to) { const a = _planSTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid plan step transition ${from} → ${to}`); }
575
- export function setMaxActivePlanProfilesPerOwnerV2(n) { _planMaxActivePerOwner = _planPos(n, "maxActivePlanProfilesPerOwner"); }
576
- export function getMaxActivePlanProfilesPerOwnerV2() { return _planMaxActivePerOwner; }
577
- export function setMaxPendingPlanStepsPerProfileV2(n) { _planMaxPendingStepsPerProfile = _planPos(n, "maxPendingPlanStepsPerProfile"); }
578
- export function getMaxPendingPlanStepsPerProfileV2() { return _planMaxPendingStepsPerProfile; }
579
- export function setPlanProfileIdleMsV2(n) { _planIdleMs = _planPos(n, "planProfileIdleMs"); }
580
- export function getPlanProfileIdleMsV2() { return _planIdleMs; }
581
- export function setPlanStepStuckMsV2(n) { _planStuckMs = _planPos(n, "planStepStuckMs"); }
582
- export function getPlanStepStuckMsV2() { return _planStuckMs; }
583
- export function _resetStatePlanModeV2() { _planPsV2.clear(); _planSsV2.clear(); _planMaxActivePerOwner = 6; _planMaxPendingStepsPerProfile = 15; _planIdleMs = 7 * 24 * 60 * 60 * 1000; _planStuckMs = 30 * 60 * 1000; }
606
+ let _planMaxActivePerOwner = 6,
607
+ _planMaxPendingStepsPerProfile = 15,
608
+ _planIdleMs = 7 * 24 * 60 * 60 * 1000,
609
+ _planStuckMs = 30 * 60 * 1000;
610
+ function _planPos(n, label) {
611
+ const v = Math.floor(Number(n));
612
+ if (!Number.isFinite(v) || v <= 0)
613
+ throw new Error(`${label} must be positive integer`);
614
+ return v;
615
+ }
616
+ function _planCheckP(from, to) {
617
+ const a = _planPTrans.get(from);
618
+ if (!a || !a.has(to))
619
+ throw new Error(`invalid plan profile transition ${from} → ${to}`);
620
+ }
621
+ function _planCheckS(from, to) {
622
+ const a = _planSTrans.get(from);
623
+ if (!a || !a.has(to))
624
+ throw new Error(`invalid plan step transition ${from} → ${to}`);
625
+ }
626
+ export function setMaxActivePlanProfilesPerOwnerV2(n) {
627
+ _planMaxActivePerOwner = _planPos(n, "maxActivePlanProfilesPerOwner");
628
+ }
629
+ export function getMaxActivePlanProfilesPerOwnerV2() {
630
+ return _planMaxActivePerOwner;
631
+ }
632
+ export function setMaxPendingPlanStepsPerProfileV2(n) {
633
+ _planMaxPendingStepsPerProfile = _planPos(n, "maxPendingPlanStepsPerProfile");
634
+ }
635
+ export function getMaxPendingPlanStepsPerProfileV2() {
636
+ return _planMaxPendingStepsPerProfile;
637
+ }
638
+ export function setPlanProfileIdleMsV2(n) {
639
+ _planIdleMs = _planPos(n, "planProfileIdleMs");
640
+ }
641
+ export function getPlanProfileIdleMsV2() {
642
+ return _planIdleMs;
643
+ }
644
+ export function setPlanStepStuckMsV2(n) {
645
+ _planStuckMs = _planPos(n, "planStepStuckMs");
646
+ }
647
+ export function getPlanStepStuckMsV2() {
648
+ return _planStuckMs;
649
+ }
650
+ export function _resetStatePlanModeV2() {
651
+ _planPsV2.clear();
652
+ _planSsV2.clear();
653
+ _planMaxActivePerOwner = 6;
654
+ _planMaxPendingStepsPerProfile = 15;
655
+ _planIdleMs = 7 * 24 * 60 * 60 * 1000;
656
+ _planStuckMs = 30 * 60 * 1000;
657
+ }
584
658
  export function registerPlanProfileV2({ id, owner, goal, metadata } = {}) {
585
- if (!id) throw new Error("plan profile id required"); if (!owner) throw new Error("plan profile owner required");
586
- if (_planPsV2.has(id)) throw new Error(`plan profile ${id} already registered`);
659
+ if (!id) throw new Error("plan profile id required");
660
+ if (!owner) throw new Error("plan profile owner required");
661
+ if (_planPsV2.has(id))
662
+ throw new Error(`plan profile ${id} already registered`);
587
663
  const now = Date.now();
588
- const p = { id, owner, goal: goal || "", status: PLAN_PROFILE_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, archivedAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
589
- _planPsV2.set(id, p); return { ...p, metadata: { ...p.metadata } };
664
+ const p = {
665
+ id,
666
+ owner,
667
+ goal: goal || "",
668
+ status: PLAN_PROFILE_MATURITY_V2.PENDING,
669
+ createdAt: now,
670
+ updatedAt: now,
671
+ activatedAt: null,
672
+ archivedAt: null,
673
+ lastTouchedAt: now,
674
+ metadata: { ...(metadata || {}) },
675
+ };
676
+ _planPsV2.set(id, p);
677
+ return { ...p, metadata: { ...p.metadata } };
678
+ }
679
+ function _planCountActive(owner) {
680
+ let n = 0;
681
+ for (const p of _planPsV2.values())
682
+ if (p.owner === owner && p.status === PLAN_PROFILE_MATURITY_V2.ACTIVE) n++;
683
+ return n;
590
684
  }
591
- function _planCountActive(owner) { let n = 0; for (const p of _planPsV2.values()) if (p.owner === owner && p.status === PLAN_PROFILE_MATURITY_V2.ACTIVE) n++; return n; }
592
685
  export function activatePlanProfileV2(id) {
593
- const p = _planPsV2.get(id); if (!p) throw new Error(`plan profile ${id} not found`);
686
+ const p = _planPsV2.get(id);
687
+ if (!p) throw new Error(`plan profile ${id} not found`);
594
688
  _planCheckP(p.status, PLAN_PROFILE_MATURITY_V2.ACTIVE);
595
689
  const recovery = p.status === PLAN_PROFILE_MATURITY_V2.PAUSED;
596
- if (!recovery && _planCountActive(p.owner) >= _planMaxActivePerOwner) throw new Error(`max active plan profiles for owner ${p.owner} reached`);
597
- const now = Date.now(); p.status = PLAN_PROFILE_MATURITY_V2.ACTIVE; p.updatedAt = now; p.lastTouchedAt = now; if (!p.activatedAt) p.activatedAt = now;
690
+ if (!recovery && _planCountActive(p.owner) >= _planMaxActivePerOwner)
691
+ throw new Error(`max active plan profiles for owner ${p.owner} reached`);
692
+ const now = Date.now();
693
+ p.status = PLAN_PROFILE_MATURITY_V2.ACTIVE;
694
+ p.updatedAt = now;
695
+ p.lastTouchedAt = now;
696
+ if (!p.activatedAt) p.activatedAt = now;
697
+ return { ...p, metadata: { ...p.metadata } };
698
+ }
699
+ export function pausePlanProfileV2(id) {
700
+ const p = _planPsV2.get(id);
701
+ if (!p) throw new Error(`plan profile ${id} not found`);
702
+ _planCheckP(p.status, PLAN_PROFILE_MATURITY_V2.PAUSED);
703
+ p.status = PLAN_PROFILE_MATURITY_V2.PAUSED;
704
+ p.updatedAt = Date.now();
705
+ return { ...p, metadata: { ...p.metadata } };
706
+ }
707
+ export function archivePlanProfileV2(id) {
708
+ const p = _planPsV2.get(id);
709
+ if (!p) throw new Error(`plan profile ${id} not found`);
710
+ _planCheckP(p.status, PLAN_PROFILE_MATURITY_V2.ARCHIVED);
711
+ const now = Date.now();
712
+ p.status = PLAN_PROFILE_MATURITY_V2.ARCHIVED;
713
+ p.updatedAt = now;
714
+ if (!p.archivedAt) p.archivedAt = now;
715
+ return { ...p, metadata: { ...p.metadata } };
716
+ }
717
+ export function touchPlanProfileV2(id) {
718
+ const p = _planPsV2.get(id);
719
+ if (!p) throw new Error(`plan profile ${id} not found`);
720
+ if (_planPTerminal.has(p.status))
721
+ throw new Error(`cannot touch terminal plan profile ${id}`);
722
+ const now = Date.now();
723
+ p.lastTouchedAt = now;
724
+ p.updatedAt = now;
725
+ return { ...p, metadata: { ...p.metadata } };
726
+ }
727
+ export function getPlanProfileV2(id) {
728
+ const p = _planPsV2.get(id);
729
+ if (!p) return null;
598
730
  return { ...p, metadata: { ...p.metadata } };
599
731
  }
600
- export function pausePlanProfileV2(id) { const p = _planPsV2.get(id); if (!p) throw new Error(`plan profile ${id} not found`); _planCheckP(p.status, PLAN_PROFILE_MATURITY_V2.PAUSED); p.status = PLAN_PROFILE_MATURITY_V2.PAUSED; p.updatedAt = Date.now(); return { ...p, metadata: { ...p.metadata } }; }
601
- export function archivePlanProfileV2(id) { const p = _planPsV2.get(id); if (!p) throw new Error(`plan profile ${id} not found`); _planCheckP(p.status, PLAN_PROFILE_MATURITY_V2.ARCHIVED); const now = Date.now(); p.status = PLAN_PROFILE_MATURITY_V2.ARCHIVED; p.updatedAt = now; if (!p.archivedAt) p.archivedAt = now; return { ...p, metadata: { ...p.metadata } }; }
602
- export function touchPlanProfileV2(id) { const p = _planPsV2.get(id); if (!p) throw new Error(`plan profile ${id} not found`); if (_planPTerminal.has(p.status)) throw new Error(`cannot touch terminal plan profile ${id}`); const now = Date.now(); p.lastTouchedAt = now; p.updatedAt = now; return { ...p, metadata: { ...p.metadata } }; }
603
- export function getPlanProfileV2(id) { const p = _planPsV2.get(id); if (!p) return null; return { ...p, metadata: { ...p.metadata } }; }
604
- export function listPlanProfilesV2() { return [..._planPsV2.values()].map((p) => ({ ...p, metadata: { ...p.metadata } })); }
605
- function _planCountPending(profileId) { let n = 0; for (const s of _planSsV2.values()) if (s.profileId === profileId && (s.status === PLAN_STEP_LIFECYCLE_V2.QUEUED || s.status === PLAN_STEP_LIFECYCLE_V2.RUNNING)) n++; return n; }
732
+ export function listPlanProfilesV2() {
733
+ return [..._planPsV2.values()].map((p) => ({
734
+ ...p,
735
+ metadata: { ...p.metadata },
736
+ }));
737
+ }
738
+ function _planCountPending(profileId) {
739
+ let n = 0;
740
+ for (const s of _planSsV2.values())
741
+ if (
742
+ s.profileId === profileId &&
743
+ (s.status === PLAN_STEP_LIFECYCLE_V2.QUEUED ||
744
+ s.status === PLAN_STEP_LIFECYCLE_V2.RUNNING)
745
+ )
746
+ n++;
747
+ return n;
748
+ }
606
749
  export function createPlanStepV2({ id, profileId, action, metadata } = {}) {
607
- if (!id) throw new Error("plan step id required"); if (!profileId) throw new Error("plan step profileId required");
750
+ if (!id) throw new Error("plan step id required");
751
+ if (!profileId) throw new Error("plan step profileId required");
608
752
  if (_planSsV2.has(id)) throw new Error(`plan step ${id} already exists`);
609
- if (!_planPsV2.has(profileId)) throw new Error(`plan profile ${profileId} not found`);
610
- if (_planCountPending(profileId) >= _planMaxPendingStepsPerProfile) throw new Error(`max pending plan steps for profile ${profileId} reached`);
753
+ if (!_planPsV2.has(profileId))
754
+ throw new Error(`plan profile ${profileId} not found`);
755
+ if (_planCountPending(profileId) >= _planMaxPendingStepsPerProfile)
756
+ throw new Error(`max pending plan steps for profile ${profileId} reached`);
611
757
  const now = Date.now();
612
- const s = { id, profileId, action: action || "", status: PLAN_STEP_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
613
- _planSsV2.set(id, s); return { ...s, metadata: { ...s.metadata } };
614
- }
615
- export function startPlanStepV2(id) { const s = _planSsV2.get(id); if (!s) throw new Error(`plan step ${id} not found`); _planCheckS(s.status, PLAN_STEP_LIFECYCLE_V2.RUNNING); const now = Date.now(); s.status = PLAN_STEP_LIFECYCLE_V2.RUNNING; s.updatedAt = now; if (!s.startedAt) s.startedAt = now; return { ...s, metadata: { ...s.metadata } }; }
616
- export function completePlanStepV2(id) { const s = _planSsV2.get(id); if (!s) throw new Error(`plan step ${id} not found`); _planCheckS(s.status, PLAN_STEP_LIFECYCLE_V2.COMPLETED); const now = Date.now(); s.status = PLAN_STEP_LIFECYCLE_V2.COMPLETED; s.updatedAt = now; if (!s.settledAt) s.settledAt = now; return { ...s, metadata: { ...s.metadata } }; }
617
- export function failPlanStepV2(id, reason) { const s = _planSsV2.get(id); if (!s) throw new Error(`plan step ${id} not found`); _planCheckS(s.status, PLAN_STEP_LIFECYCLE_V2.FAILED); const now = Date.now(); s.status = PLAN_STEP_LIFECYCLE_V2.FAILED; s.updatedAt = now; if (!s.settledAt) s.settledAt = now; if (reason) s.metadata.failReason = String(reason); return { ...s, metadata: { ...s.metadata } }; }
618
- export function cancelPlanStepV2(id, reason) { const s = _planSsV2.get(id); if (!s) throw new Error(`plan step ${id} not found`); _planCheckS(s.status, PLAN_STEP_LIFECYCLE_V2.CANCELLED); const now = Date.now(); s.status = PLAN_STEP_LIFECYCLE_V2.CANCELLED; s.updatedAt = now; if (!s.settledAt) s.settledAt = now; if (reason) s.metadata.cancelReason = String(reason); return { ...s, metadata: { ...s.metadata } }; }
619
- export function getPlanStepV2(id) { const s = _planSsV2.get(id); if (!s) return null; return { ...s, metadata: { ...s.metadata } }; }
620
- export function listPlanStepsV2() { return [..._planSsV2.values()].map((s) => ({ ...s, metadata: { ...s.metadata } })); }
621
- export function autoPauseIdlePlanProfilesV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const p of _planPsV2.values()) if (p.status === PLAN_PROFILE_MATURITY_V2.ACTIVE && (t - p.lastTouchedAt) >= _planIdleMs) { p.status = PLAN_PROFILE_MATURITY_V2.PAUSED; p.updatedAt = t; flipped.push(p.id); } return { flipped, count: flipped.length }; }
622
- export function autoFailStuckPlanStepsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const s of _planSsV2.values()) if (s.status === PLAN_STEP_LIFECYCLE_V2.RUNNING && s.startedAt != null && (t - s.startedAt) >= _planStuckMs) { s.status = PLAN_STEP_LIFECYCLE_V2.FAILED; s.updatedAt = t; if (!s.settledAt) s.settledAt = t; s.metadata.failReason = "auto-fail-stuck"; flipped.push(s.id); } return { flipped, count: flipped.length }; }
758
+ const s = {
759
+ id,
760
+ profileId,
761
+ action: action || "",
762
+ status: PLAN_STEP_LIFECYCLE_V2.QUEUED,
763
+ createdAt: now,
764
+ updatedAt: now,
765
+ startedAt: null,
766
+ settledAt: null,
767
+ metadata: { ...(metadata || {}) },
768
+ };
769
+ _planSsV2.set(id, s);
770
+ return { ...s, metadata: { ...s.metadata } };
771
+ }
772
+ export function startPlanStepV2(id) {
773
+ const s = _planSsV2.get(id);
774
+ if (!s) throw new Error(`plan step ${id} not found`);
775
+ _planCheckS(s.status, PLAN_STEP_LIFECYCLE_V2.RUNNING);
776
+ const now = Date.now();
777
+ s.status = PLAN_STEP_LIFECYCLE_V2.RUNNING;
778
+ s.updatedAt = now;
779
+ if (!s.startedAt) s.startedAt = now;
780
+ return { ...s, metadata: { ...s.metadata } };
781
+ }
782
+ export function completePlanStepV2(id) {
783
+ const s = _planSsV2.get(id);
784
+ if (!s) throw new Error(`plan step ${id} not found`);
785
+ _planCheckS(s.status, PLAN_STEP_LIFECYCLE_V2.COMPLETED);
786
+ const now = Date.now();
787
+ s.status = PLAN_STEP_LIFECYCLE_V2.COMPLETED;
788
+ s.updatedAt = now;
789
+ if (!s.settledAt) s.settledAt = now;
790
+ return { ...s, metadata: { ...s.metadata } };
791
+ }
792
+ export function failPlanStepV2(id, reason) {
793
+ const s = _planSsV2.get(id);
794
+ if (!s) throw new Error(`plan step ${id} not found`);
795
+ _planCheckS(s.status, PLAN_STEP_LIFECYCLE_V2.FAILED);
796
+ const now = Date.now();
797
+ s.status = PLAN_STEP_LIFECYCLE_V2.FAILED;
798
+ s.updatedAt = now;
799
+ if (!s.settledAt) s.settledAt = now;
800
+ if (reason) s.metadata.failReason = String(reason);
801
+ return { ...s, metadata: { ...s.metadata } };
802
+ }
803
+ export function cancelPlanStepV2(id, reason) {
804
+ const s = _planSsV2.get(id);
805
+ if (!s) throw new Error(`plan step ${id} not found`);
806
+ _planCheckS(s.status, PLAN_STEP_LIFECYCLE_V2.CANCELLED);
807
+ const now = Date.now();
808
+ s.status = PLAN_STEP_LIFECYCLE_V2.CANCELLED;
809
+ s.updatedAt = now;
810
+ if (!s.settledAt) s.settledAt = now;
811
+ if (reason) s.metadata.cancelReason = String(reason);
812
+ return { ...s, metadata: { ...s.metadata } };
813
+ }
814
+ export function getPlanStepV2(id) {
815
+ const s = _planSsV2.get(id);
816
+ if (!s) return null;
817
+ return { ...s, metadata: { ...s.metadata } };
818
+ }
819
+ export function listPlanStepsV2() {
820
+ return [..._planSsV2.values()].map((s) => ({
821
+ ...s,
822
+ metadata: { ...s.metadata },
823
+ }));
824
+ }
825
+ export function autoPauseIdlePlanProfilesV2({ now } = {}) {
826
+ const t = now ?? Date.now();
827
+ const flipped = [];
828
+ for (const p of _planPsV2.values())
829
+ if (
830
+ p.status === PLAN_PROFILE_MATURITY_V2.ACTIVE &&
831
+ t - p.lastTouchedAt >= _planIdleMs
832
+ ) {
833
+ p.status = PLAN_PROFILE_MATURITY_V2.PAUSED;
834
+ p.updatedAt = t;
835
+ flipped.push(p.id);
836
+ }
837
+ return { flipped, count: flipped.length };
838
+ }
839
+ export function autoFailStuckPlanStepsV2({ now } = {}) {
840
+ const t = now ?? Date.now();
841
+ const flipped = [];
842
+ for (const s of _planSsV2.values())
843
+ if (
844
+ s.status === PLAN_STEP_LIFECYCLE_V2.RUNNING &&
845
+ s.startedAt != null &&
846
+ t - s.startedAt >= _planStuckMs
847
+ ) {
848
+ s.status = PLAN_STEP_LIFECYCLE_V2.FAILED;
849
+ s.updatedAt = t;
850
+ if (!s.settledAt) s.settledAt = t;
851
+ s.metadata.failReason = "auto-fail-stuck";
852
+ flipped.push(s.id);
853
+ }
854
+ return { flipped, count: flipped.length };
855
+ }
623
856
  export function getPlanModeGovStatsV2() {
624
- const profilesByStatus = {}; for (const v of Object.values(PLAN_PROFILE_MATURITY_V2)) profilesByStatus[v] = 0; for (const p of _planPsV2.values()) profilesByStatus[p.status]++;
625
- const stepsByStatus = {}; for (const v of Object.values(PLAN_STEP_LIFECYCLE_V2)) stepsByStatus[v] = 0; for (const s of _planSsV2.values()) stepsByStatus[s.status]++;
626
- return { totalPlanProfilesV2: _planPsV2.size, totalPlanStepsV2: _planSsV2.size, maxActivePlanProfilesPerOwner: _planMaxActivePerOwner, maxPendingPlanStepsPerProfile: _planMaxPendingStepsPerProfile, planProfileIdleMs: _planIdleMs, planStepStuckMs: _planStuckMs, profilesByStatus, stepsByStatus };
857
+ const profilesByStatus = {};
858
+ for (const v of Object.values(PLAN_PROFILE_MATURITY_V2))
859
+ profilesByStatus[v] = 0;
860
+ for (const p of _planPsV2.values()) profilesByStatus[p.status]++;
861
+ const stepsByStatus = {};
862
+ for (const v of Object.values(PLAN_STEP_LIFECYCLE_V2)) stepsByStatus[v] = 0;
863
+ for (const s of _planSsV2.values()) stepsByStatus[s.status]++;
864
+ return {
865
+ totalPlanProfilesV2: _planPsV2.size,
866
+ totalPlanStepsV2: _planSsV2.size,
867
+ maxActivePlanProfilesPerOwner: _planMaxActivePerOwner,
868
+ maxPendingPlanStepsPerProfile: _planMaxPendingStepsPerProfile,
869
+ planProfileIdleMs: _planIdleMs,
870
+ planStepStuckMs: _planStuckMs,
871
+ profilesByStatus,
872
+ stepsByStatus,
873
+ };
627
874
  }