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
@@ -493,25 +493,53 @@ Reply with a JSON object: { "action": "retry|add_step|skip", "newParams": {...},
493
493
  }
494
494
  }
495
495
 
496
-
497
496
  // ===== V2 Surface: Autonomous Agent governance overlay (CLI v0.138.0) =====
498
497
  export const AUTOAGENT_MATURITY_V2 = Object.freeze({
499
- PENDING: "pending", ACTIVE: "active", PAUSED: "paused", ARCHIVED: "archived",
498
+ PENDING: "pending",
499
+ ACTIVE: "active",
500
+ PAUSED: "paused",
501
+ ARCHIVED: "archived",
500
502
  });
501
503
  export const AUTOAGENT_RUN_LIFECYCLE_V2 = Object.freeze({
502
- QUEUED: "queued", RUNNING: "running", COMPLETED: "completed", FAILED: "failed", CANCELLED: "cancelled",
504
+ QUEUED: "queued",
505
+ RUNNING: "running",
506
+ COMPLETED: "completed",
507
+ FAILED: "failed",
508
+ CANCELLED: "cancelled",
503
509
  });
504
510
 
505
511
  const _aaTrans = new Map([
506
- [AUTOAGENT_MATURITY_V2.PENDING, new Set([AUTOAGENT_MATURITY_V2.ACTIVE, AUTOAGENT_MATURITY_V2.ARCHIVED])],
507
- [AUTOAGENT_MATURITY_V2.ACTIVE, new Set([AUTOAGENT_MATURITY_V2.PAUSED, AUTOAGENT_MATURITY_V2.ARCHIVED])],
508
- [AUTOAGENT_MATURITY_V2.PAUSED, new Set([AUTOAGENT_MATURITY_V2.ACTIVE, AUTOAGENT_MATURITY_V2.ARCHIVED])],
512
+ [
513
+ AUTOAGENT_MATURITY_V2.PENDING,
514
+ new Set([AUTOAGENT_MATURITY_V2.ACTIVE, AUTOAGENT_MATURITY_V2.ARCHIVED]),
515
+ ],
516
+ [
517
+ AUTOAGENT_MATURITY_V2.ACTIVE,
518
+ new Set([AUTOAGENT_MATURITY_V2.PAUSED, AUTOAGENT_MATURITY_V2.ARCHIVED]),
519
+ ],
520
+ [
521
+ AUTOAGENT_MATURITY_V2.PAUSED,
522
+ new Set([AUTOAGENT_MATURITY_V2.ACTIVE, AUTOAGENT_MATURITY_V2.ARCHIVED]),
523
+ ],
509
524
  [AUTOAGENT_MATURITY_V2.ARCHIVED, new Set()],
510
525
  ]);
511
526
  const _aaTerminal = new Set([AUTOAGENT_MATURITY_V2.ARCHIVED]);
512
527
  const _aaRunTrans = new Map([
513
- [AUTOAGENT_RUN_LIFECYCLE_V2.QUEUED, new Set([AUTOAGENT_RUN_LIFECYCLE_V2.RUNNING, AUTOAGENT_RUN_LIFECYCLE_V2.CANCELLED])],
514
- [AUTOAGENT_RUN_LIFECYCLE_V2.RUNNING, new Set([AUTOAGENT_RUN_LIFECYCLE_V2.COMPLETED, AUTOAGENT_RUN_LIFECYCLE_V2.FAILED, AUTOAGENT_RUN_LIFECYCLE_V2.CANCELLED])],
528
+ [
529
+ AUTOAGENT_RUN_LIFECYCLE_V2.QUEUED,
530
+ new Set([
531
+ AUTOAGENT_RUN_LIFECYCLE_V2.RUNNING,
532
+ AUTOAGENT_RUN_LIFECYCLE_V2.CANCELLED,
533
+ ]),
534
+ ],
535
+ [
536
+ AUTOAGENT_RUN_LIFECYCLE_V2.RUNNING,
537
+ new Set([
538
+ AUTOAGENT_RUN_LIFECYCLE_V2.COMPLETED,
539
+ AUTOAGENT_RUN_LIFECYCLE_V2.FAILED,
540
+ AUTOAGENT_RUN_LIFECYCLE_V2.CANCELLED,
541
+ ]),
542
+ ],
515
543
  [AUTOAGENT_RUN_LIFECYCLE_V2.COMPLETED, new Set()],
516
544
  [AUTOAGENT_RUN_LIFECYCLE_V2.FAILED, new Set()],
517
545
  [AUTOAGENT_RUN_LIFECYCLE_V2.CANCELLED, new Set()],
@@ -524,21 +552,45 @@ let _aaMaxPendingRunsPerAgent = 10;
524
552
  let _aaAgentIdleMs = 7 * 24 * 60 * 60 * 1000;
525
553
  let _aaRunStuckMs = 30 * 60 * 1000;
526
554
 
527
- function _aaPos(n, lbl) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${lbl} must be positive integer`); return v; }
555
+ function _aaPos(n, lbl) {
556
+ const v = Math.floor(Number(n));
557
+ if (!Number.isFinite(v) || v <= 0)
558
+ throw new Error(`${lbl} must be positive integer`);
559
+ return v;
560
+ }
528
561
 
529
- export function setMaxActiveAutoAgentsPerOwnerV2(n) { _aaMaxActivePerOwner = _aaPos(n, "maxActiveAutoAgentsPerOwner"); }
530
- export function getMaxActiveAutoAgentsPerOwnerV2() { return _aaMaxActivePerOwner; }
531
- export function setMaxPendingAutoAgentRunsPerAgentV2(n) { _aaMaxPendingRunsPerAgent = _aaPos(n, "maxPendingAutoAgentRunsPerAgent"); }
532
- export function getMaxPendingAutoAgentRunsPerAgentV2() { return _aaMaxPendingRunsPerAgent; }
533
- export function setAutoAgentIdleMsV2(n) { _aaAgentIdleMs = _aaPos(n, "autoAgentIdleMs"); }
534
- export function getAutoAgentIdleMsV2() { return _aaAgentIdleMs; }
535
- export function setAutoAgentRunStuckMsV2(n) { _aaRunStuckMs = _aaPos(n, "autoAgentRunStuckMs"); }
536
- export function getAutoAgentRunStuckMsV2() { return _aaRunStuckMs; }
562
+ export function setMaxActiveAutoAgentsPerOwnerV2(n) {
563
+ _aaMaxActivePerOwner = _aaPos(n, "maxActiveAutoAgentsPerOwner");
564
+ }
565
+ export function getMaxActiveAutoAgentsPerOwnerV2() {
566
+ return _aaMaxActivePerOwner;
567
+ }
568
+ export function setMaxPendingAutoAgentRunsPerAgentV2(n) {
569
+ _aaMaxPendingRunsPerAgent = _aaPos(n, "maxPendingAutoAgentRunsPerAgent");
570
+ }
571
+ export function getMaxPendingAutoAgentRunsPerAgentV2() {
572
+ return _aaMaxPendingRunsPerAgent;
573
+ }
574
+ export function setAutoAgentIdleMsV2(n) {
575
+ _aaAgentIdleMs = _aaPos(n, "autoAgentIdleMs");
576
+ }
577
+ export function getAutoAgentIdleMsV2() {
578
+ return _aaAgentIdleMs;
579
+ }
580
+ export function setAutoAgentRunStuckMsV2(n) {
581
+ _aaRunStuckMs = _aaPos(n, "autoAgentRunStuckMs");
582
+ }
583
+ export function getAutoAgentRunStuckMsV2() {
584
+ return _aaRunStuckMs;
585
+ }
537
586
 
538
587
  export function _resetStateAutonomousAgentV2() {
539
- _aaAgents.clear(); _aaRuns.clear();
540
- _aaMaxActivePerOwner = 5; _aaMaxPendingRunsPerAgent = 10;
541
- _aaAgentIdleMs = 7 * 24 * 60 * 60 * 1000; _aaRunStuckMs = 30 * 60 * 1000;
588
+ _aaAgents.clear();
589
+ _aaRuns.clear();
590
+ _aaMaxActivePerOwner = 5;
591
+ _aaMaxPendingRunsPerAgent = 10;
592
+ _aaAgentIdleMs = 7 * 24 * 60 * 60 * 1000;
593
+ _aaRunStuckMs = 30 * 60 * 1000;
542
594
  }
543
595
 
544
596
  export function registerAutoAgentV2({ id, owner, goal, metadata } = {}) {
@@ -546,54 +598,238 @@ export function registerAutoAgentV2({ id, owner, goal, metadata } = {}) {
546
598
  if (!owner || typeof owner !== "string") throw new Error("owner is required");
547
599
  if (_aaAgents.has(id)) throw new Error(`auto-agent ${id} already registered`);
548
600
  const now = Date.now();
549
- const a = { id, owner, goal: goal || "", status: AUTOAGENT_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, archivedAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
601
+ const a = {
602
+ id,
603
+ owner,
604
+ goal: goal || "",
605
+ status: AUTOAGENT_MATURITY_V2.PENDING,
606
+ createdAt: now,
607
+ updatedAt: now,
608
+ activatedAt: null,
609
+ archivedAt: null,
610
+ lastTouchedAt: now,
611
+ metadata: { ...(metadata || {}) },
612
+ };
550
613
  _aaAgents.set(id, a);
551
614
  return { ...a, metadata: { ...a.metadata } };
552
615
  }
553
- function _aaCheckA(from, to) { const a = _aaTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid auto-agent transition ${from} → ${to}`); }
554
- function _aaCountActive(owner) { let n = 0; for (const a of _aaAgents.values()) if (a.owner === owner && a.status === AUTOAGENT_MATURITY_V2.ACTIVE) n++; return n; }
616
+ function _aaCheckA(from, to) {
617
+ const a = _aaTrans.get(from);
618
+ if (!a || !a.has(to))
619
+ throw new Error(`invalid auto-agent transition ${from} → ${to}`);
620
+ }
621
+ function _aaCountActive(owner) {
622
+ let n = 0;
623
+ for (const a of _aaAgents.values())
624
+ if (a.owner === owner && a.status === AUTOAGENT_MATURITY_V2.ACTIVE) n++;
625
+ return n;
626
+ }
555
627
 
556
628
  export function activateAutoAgentV2(id) {
557
- const a = _aaAgents.get(id); if (!a) throw new Error(`auto-agent ${id} not found`);
629
+ const a = _aaAgents.get(id);
630
+ if (!a) throw new Error(`auto-agent ${id} not found`);
558
631
  _aaCheckA(a.status, AUTOAGENT_MATURITY_V2.ACTIVE);
559
632
  const recovery = a.status === AUTOAGENT_MATURITY_V2.PAUSED;
560
- if (!recovery) { const c = _aaCountActive(a.owner); if (c >= _aaMaxActivePerOwner) throw new Error(`max active auto-agents per owner (${_aaMaxActivePerOwner}) reached for ${a.owner}`); }
561
- const now = Date.now(); a.status = AUTOAGENT_MATURITY_V2.ACTIVE; a.updatedAt = now; a.lastTouchedAt = now; if (!a.activatedAt) a.activatedAt = now;
633
+ if (!recovery) {
634
+ const c = _aaCountActive(a.owner);
635
+ if (c >= _aaMaxActivePerOwner)
636
+ throw new Error(
637
+ `max active auto-agents per owner (${_aaMaxActivePerOwner}) reached for ${a.owner}`,
638
+ );
639
+ }
640
+ const now = Date.now();
641
+ a.status = AUTOAGENT_MATURITY_V2.ACTIVE;
642
+ a.updatedAt = now;
643
+ a.lastTouchedAt = now;
644
+ if (!a.activatedAt) a.activatedAt = now;
645
+ return { ...a, metadata: { ...a.metadata } };
646
+ }
647
+ export function pauseAutoAgentV2(id) {
648
+ const a = _aaAgents.get(id);
649
+ if (!a) throw new Error(`auto-agent ${id} not found`);
650
+ _aaCheckA(a.status, AUTOAGENT_MATURITY_V2.PAUSED);
651
+ a.status = AUTOAGENT_MATURITY_V2.PAUSED;
652
+ a.updatedAt = Date.now();
562
653
  return { ...a, metadata: { ...a.metadata } };
563
654
  }
564
- export function pauseAutoAgentV2(id) { const a = _aaAgents.get(id); if (!a) throw new Error(`auto-agent ${id} not found`); _aaCheckA(a.status, AUTOAGENT_MATURITY_V2.PAUSED); a.status = AUTOAGENT_MATURITY_V2.PAUSED; a.updatedAt = Date.now(); return { ...a, metadata: { ...a.metadata } }; }
565
- export function archiveAutoAgentV2(id) { const a = _aaAgents.get(id); if (!a) throw new Error(`auto-agent ${id} not found`); _aaCheckA(a.status, AUTOAGENT_MATURITY_V2.ARCHIVED); const now = Date.now(); a.status = AUTOAGENT_MATURITY_V2.ARCHIVED; a.updatedAt = now; if (!a.archivedAt) a.archivedAt = now; return { ...a, metadata: { ...a.metadata } }; }
566
- export function touchAutoAgentV2(id) { const a = _aaAgents.get(id); if (!a) throw new Error(`auto-agent ${id} not found`); if (_aaTerminal.has(a.status)) throw new Error(`cannot touch terminal auto-agent ${id}`); const now = Date.now(); a.lastTouchedAt = now; a.updatedAt = now; return { ...a, metadata: { ...a.metadata } }; }
567
- export function getAutoAgentV2(id) { const a = _aaAgents.get(id); if (!a) return null; return { ...a, metadata: { ...a.metadata } }; }
568
- export function listAutoAgentsV2() { return [..._aaAgents.values()].map((a) => ({ ...a, metadata: { ...a.metadata } })); }
655
+ export function archiveAutoAgentV2(id) {
656
+ const a = _aaAgents.get(id);
657
+ if (!a) throw new Error(`auto-agent ${id} not found`);
658
+ _aaCheckA(a.status, AUTOAGENT_MATURITY_V2.ARCHIVED);
659
+ const now = Date.now();
660
+ a.status = AUTOAGENT_MATURITY_V2.ARCHIVED;
661
+ a.updatedAt = now;
662
+ if (!a.archivedAt) a.archivedAt = now;
663
+ return { ...a, metadata: { ...a.metadata } };
664
+ }
665
+ export function touchAutoAgentV2(id) {
666
+ const a = _aaAgents.get(id);
667
+ if (!a) throw new Error(`auto-agent ${id} not found`);
668
+ if (_aaTerminal.has(a.status))
669
+ throw new Error(`cannot touch terminal auto-agent ${id}`);
670
+ const now = Date.now();
671
+ a.lastTouchedAt = now;
672
+ a.updatedAt = now;
673
+ return { ...a, metadata: { ...a.metadata } };
674
+ }
675
+ export function getAutoAgentV2(id) {
676
+ const a = _aaAgents.get(id);
677
+ if (!a) return null;
678
+ return { ...a, metadata: { ...a.metadata } };
679
+ }
680
+ export function listAutoAgentsV2() {
681
+ return [..._aaAgents.values()].map((a) => ({
682
+ ...a,
683
+ metadata: { ...a.metadata },
684
+ }));
685
+ }
569
686
 
570
- function _aaCountPendingRuns(aid) { let n = 0; for (const r of _aaRuns.values()) if (r.agentId === aid && (r.status === AUTOAGENT_RUN_LIFECYCLE_V2.QUEUED || r.status === AUTOAGENT_RUN_LIFECYCLE_V2.RUNNING)) n++; return n; }
687
+ function _aaCountPendingRuns(aid) {
688
+ let n = 0;
689
+ for (const r of _aaRuns.values())
690
+ if (
691
+ r.agentId === aid &&
692
+ (r.status === AUTOAGENT_RUN_LIFECYCLE_V2.QUEUED ||
693
+ r.status === AUTOAGENT_RUN_LIFECYCLE_V2.RUNNING)
694
+ )
695
+ n++;
696
+ return n;
697
+ }
571
698
 
572
699
  export function createAutoAgentRunV2({ id, agentId, prompt, metadata } = {}) {
573
700
  if (!id || typeof id !== "string") throw new Error("id is required");
574
- if (!agentId || typeof agentId !== "string") throw new Error("agentId is required");
701
+ if (!agentId || typeof agentId !== "string")
702
+ throw new Error("agentId is required");
575
703
  if (_aaRuns.has(id)) throw new Error(`auto-agent run ${id} already exists`);
576
- if (!_aaAgents.has(agentId)) throw new Error(`auto-agent ${agentId} not found`);
704
+ if (!_aaAgents.has(agentId))
705
+ throw new Error(`auto-agent ${agentId} not found`);
577
706
  const pending = _aaCountPendingRuns(agentId);
578
- if (pending >= _aaMaxPendingRunsPerAgent) throw new Error(`max pending auto-agent runs per agent (${_aaMaxPendingRunsPerAgent}) reached for ${agentId}`);
707
+ if (pending >= _aaMaxPendingRunsPerAgent)
708
+ throw new Error(
709
+ `max pending auto-agent runs per agent (${_aaMaxPendingRunsPerAgent}) reached for ${agentId}`,
710
+ );
579
711
  const now = Date.now();
580
- const r = { id, agentId, prompt: prompt || "", status: AUTOAGENT_RUN_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
712
+ const r = {
713
+ id,
714
+ agentId,
715
+ prompt: prompt || "",
716
+ status: AUTOAGENT_RUN_LIFECYCLE_V2.QUEUED,
717
+ createdAt: now,
718
+ updatedAt: now,
719
+ startedAt: null,
720
+ settledAt: null,
721
+ metadata: { ...(metadata || {}) },
722
+ };
581
723
  _aaRuns.set(id, r);
582
724
  return { ...r, metadata: { ...r.metadata } };
583
725
  }
584
- function _aaCheckR(from, to) { const a = _aaRunTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid auto-agent run transition ${from} → ${to}`); }
585
- export function startAutoAgentRunV2(id) { const r = _aaRuns.get(id); if (!r) throw new Error(`auto-agent run ${id} not found`); _aaCheckR(r.status, AUTOAGENT_RUN_LIFECYCLE_V2.RUNNING); const now = Date.now(); r.status = AUTOAGENT_RUN_LIFECYCLE_V2.RUNNING; r.updatedAt = now; if (!r.startedAt) r.startedAt = now; return { ...r, metadata: { ...r.metadata } }; }
586
- export function completeAutoAgentRunV2(id) { const r = _aaRuns.get(id); if (!r) throw new Error(`auto-agent run ${id} not found`); _aaCheckR(r.status, AUTOAGENT_RUN_LIFECYCLE_V2.COMPLETED); const now = Date.now(); r.status = AUTOAGENT_RUN_LIFECYCLE_V2.COMPLETED; r.updatedAt = now; if (!r.settledAt) r.settledAt = now; return { ...r, metadata: { ...r.metadata } }; }
587
- export function failAutoAgentRunV2(id, reason) { const r = _aaRuns.get(id); if (!r) throw new Error(`auto-agent run ${id} not found`); _aaCheckR(r.status, AUTOAGENT_RUN_LIFECYCLE_V2.FAILED); const now = Date.now(); r.status = AUTOAGENT_RUN_LIFECYCLE_V2.FAILED; r.updatedAt = now; if (!r.settledAt) r.settledAt = now; if (reason) r.metadata.failReason = String(reason); return { ...r, metadata: { ...r.metadata } }; }
588
- export function cancelAutoAgentRunV2(id, reason) { const r = _aaRuns.get(id); if (!r) throw new Error(`auto-agent run ${id} not found`); _aaCheckR(r.status, AUTOAGENT_RUN_LIFECYCLE_V2.CANCELLED); const now = Date.now(); r.status = AUTOAGENT_RUN_LIFECYCLE_V2.CANCELLED; r.updatedAt = now; if (!r.settledAt) r.settledAt = now; if (reason) r.metadata.cancelReason = String(reason); return { ...r, metadata: { ...r.metadata } }; }
589
- export function getAutoAgentRunV2(id) { const r = _aaRuns.get(id); if (!r) return null; return { ...r, metadata: { ...r.metadata } }; }
590
- export function listAutoAgentRunsV2() { return [..._aaRuns.values()].map((r) => ({ ...r, metadata: { ...r.metadata } })); }
726
+ function _aaCheckR(from, to) {
727
+ const a = _aaRunTrans.get(from);
728
+ if (!a || !a.has(to))
729
+ throw new Error(`invalid auto-agent run transition ${from} ${to}`);
730
+ }
731
+ export function startAutoAgentRunV2(id) {
732
+ const r = _aaRuns.get(id);
733
+ if (!r) throw new Error(`auto-agent run ${id} not found`);
734
+ _aaCheckR(r.status, AUTOAGENT_RUN_LIFECYCLE_V2.RUNNING);
735
+ const now = Date.now();
736
+ r.status = AUTOAGENT_RUN_LIFECYCLE_V2.RUNNING;
737
+ r.updatedAt = now;
738
+ if (!r.startedAt) r.startedAt = now;
739
+ return { ...r, metadata: { ...r.metadata } };
740
+ }
741
+ export function completeAutoAgentRunV2(id) {
742
+ const r = _aaRuns.get(id);
743
+ if (!r) throw new Error(`auto-agent run ${id} not found`);
744
+ _aaCheckR(r.status, AUTOAGENT_RUN_LIFECYCLE_V2.COMPLETED);
745
+ const now = Date.now();
746
+ r.status = AUTOAGENT_RUN_LIFECYCLE_V2.COMPLETED;
747
+ r.updatedAt = now;
748
+ if (!r.settledAt) r.settledAt = now;
749
+ return { ...r, metadata: { ...r.metadata } };
750
+ }
751
+ export function failAutoAgentRunV2(id, reason) {
752
+ const r = _aaRuns.get(id);
753
+ if (!r) throw new Error(`auto-agent run ${id} not found`);
754
+ _aaCheckR(r.status, AUTOAGENT_RUN_LIFECYCLE_V2.FAILED);
755
+ const now = Date.now();
756
+ r.status = AUTOAGENT_RUN_LIFECYCLE_V2.FAILED;
757
+ r.updatedAt = now;
758
+ if (!r.settledAt) r.settledAt = now;
759
+ if (reason) r.metadata.failReason = String(reason);
760
+ return { ...r, metadata: { ...r.metadata } };
761
+ }
762
+ export function cancelAutoAgentRunV2(id, reason) {
763
+ const r = _aaRuns.get(id);
764
+ if (!r) throw new Error(`auto-agent run ${id} not found`);
765
+ _aaCheckR(r.status, AUTOAGENT_RUN_LIFECYCLE_V2.CANCELLED);
766
+ const now = Date.now();
767
+ r.status = AUTOAGENT_RUN_LIFECYCLE_V2.CANCELLED;
768
+ r.updatedAt = now;
769
+ if (!r.settledAt) r.settledAt = now;
770
+ if (reason) r.metadata.cancelReason = String(reason);
771
+ return { ...r, metadata: { ...r.metadata } };
772
+ }
773
+ export function getAutoAgentRunV2(id) {
774
+ const r = _aaRuns.get(id);
775
+ if (!r) return null;
776
+ return { ...r, metadata: { ...r.metadata } };
777
+ }
778
+ export function listAutoAgentRunsV2() {
779
+ return [..._aaRuns.values()].map((r) => ({
780
+ ...r,
781
+ metadata: { ...r.metadata },
782
+ }));
783
+ }
591
784
 
592
- export function autoPauseIdleAutoAgentsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const a of _aaAgents.values()) if (a.status === AUTOAGENT_MATURITY_V2.ACTIVE && (t - a.lastTouchedAt) >= _aaAgentIdleMs) { a.status = AUTOAGENT_MATURITY_V2.PAUSED; a.updatedAt = t; flipped.push(a.id); } return { flipped, count: flipped.length }; }
593
- export function autoFailStuckAutoAgentRunsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const r of _aaRuns.values()) if (r.status === AUTOAGENT_RUN_LIFECYCLE_V2.RUNNING && r.startedAt != null && (t - r.startedAt) >= _aaRunStuckMs) { r.status = AUTOAGENT_RUN_LIFECYCLE_V2.FAILED; r.updatedAt = t; if (!r.settledAt) r.settledAt = t; r.metadata.failReason = "auto-fail-stuck"; flipped.push(r.id); } return { flipped, count: flipped.length }; }
785
+ export function autoPauseIdleAutoAgentsV2({ now } = {}) {
786
+ const t = now ?? Date.now();
787
+ const flipped = [];
788
+ for (const a of _aaAgents.values())
789
+ if (
790
+ a.status === AUTOAGENT_MATURITY_V2.ACTIVE &&
791
+ t - a.lastTouchedAt >= _aaAgentIdleMs
792
+ ) {
793
+ a.status = AUTOAGENT_MATURITY_V2.PAUSED;
794
+ a.updatedAt = t;
795
+ flipped.push(a.id);
796
+ }
797
+ return { flipped, count: flipped.length };
798
+ }
799
+ export function autoFailStuckAutoAgentRunsV2({ now } = {}) {
800
+ const t = now ?? Date.now();
801
+ const flipped = [];
802
+ for (const r of _aaRuns.values())
803
+ if (
804
+ r.status === AUTOAGENT_RUN_LIFECYCLE_V2.RUNNING &&
805
+ r.startedAt != null &&
806
+ t - r.startedAt >= _aaRunStuckMs
807
+ ) {
808
+ r.status = AUTOAGENT_RUN_LIFECYCLE_V2.FAILED;
809
+ r.updatedAt = t;
810
+ if (!r.settledAt) r.settledAt = t;
811
+ r.metadata.failReason = "auto-fail-stuck";
812
+ flipped.push(r.id);
813
+ }
814
+ return { flipped, count: flipped.length };
815
+ }
594
816
 
595
817
  export function getAutonomousAgentGovStatsV2() {
596
- const agentsByStatus = {}; for (const s of Object.values(AUTOAGENT_MATURITY_V2)) agentsByStatus[s] = 0; for (const a of _aaAgents.values()) agentsByStatus[a.status]++;
597
- const runsByStatus = {}; for (const s of Object.values(AUTOAGENT_RUN_LIFECYCLE_V2)) runsByStatus[s] = 0; for (const r of _aaRuns.values()) runsByStatus[r.status]++;
598
- return { totalAutoAgentsV2: _aaAgents.size, totalAutoAgentRunsV2: _aaRuns.size, maxActiveAutoAgentsPerOwner: _aaMaxActivePerOwner, maxPendingAutoAgentRunsPerAgent: _aaMaxPendingRunsPerAgent, autoAgentIdleMs: _aaAgentIdleMs, autoAgentRunStuckMs: _aaRunStuckMs, agentsByStatus, runsByStatus };
818
+ const agentsByStatus = {};
819
+ for (const s of Object.values(AUTOAGENT_MATURITY_V2)) agentsByStatus[s] = 0;
820
+ for (const a of _aaAgents.values()) agentsByStatus[a.status]++;
821
+ const runsByStatus = {};
822
+ for (const s of Object.values(AUTOAGENT_RUN_LIFECYCLE_V2))
823
+ runsByStatus[s] = 0;
824
+ for (const r of _aaRuns.values()) runsByStatus[r.status]++;
825
+ return {
826
+ totalAutoAgentsV2: _aaAgents.size,
827
+ totalAutoAgentRunsV2: _aaRuns.size,
828
+ maxActiveAutoAgentsPerOwner: _aaMaxActivePerOwner,
829
+ maxPendingAutoAgentRunsPerAgent: _aaMaxPendingRunsPerAgent,
830
+ autoAgentIdleMs: _aaAgentIdleMs,
831
+ autoAgentRunStuckMs: _aaRunStuckMs,
832
+ agentsByStatus,
833
+ runsByStatus,
834
+ };
599
835
  }