chainlesschain 0.81.0 → 0.132.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 (110) hide show
  1. package/bin/chainlesschain.js +0 -0
  2. package/package.json +1 -1
  3. package/src/commands/agent-network.js +254 -1
  4. package/src/commands/audit.js +302 -0
  5. package/src/commands/automation.js +271 -1
  6. package/src/commands/codegen.js +224 -0
  7. package/src/commands/collab.js +341 -0
  8. package/src/commands/compliance.js +1035 -0
  9. package/src/commands/cowork.js +221 -0
  10. package/src/commands/dbevo.js +284 -0
  11. package/src/commands/dev.js +252 -0
  12. package/src/commands/did.js +358 -0
  13. package/src/commands/encrypt.js +341 -0
  14. package/src/commands/export.js +256 -1
  15. package/src/commands/fusion.js +258 -0
  16. package/src/commands/governance.js +325 -0
  17. package/src/commands/hardening.js +411 -0
  18. package/src/commands/hook.js +148 -0
  19. package/src/commands/import.js +252 -0
  20. package/src/commands/incentive.js +322 -0
  21. package/src/commands/infra.js +244 -0
  22. package/src/commands/instinct.js +260 -0
  23. package/src/commands/ipfs.js +318 -0
  24. package/src/commands/kg.js +387 -0
  25. package/src/commands/llm.js +263 -0
  26. package/src/commands/mcp.js +221 -0
  27. package/src/commands/memory.js +248 -0
  28. package/src/commands/multimodal.js +296 -0
  29. package/src/commands/nlprog.js +356 -0
  30. package/src/commands/note.js +244 -0
  31. package/src/commands/ops.js +354 -0
  32. package/src/commands/orchestrate.js +166 -0
  33. package/src/commands/org.js +277 -0
  34. package/src/commands/p2p.js +390 -0
  35. package/src/commands/perception.js +290 -0
  36. package/src/commands/permmem.js +251 -0
  37. package/src/commands/plugin-ecosystem.js +273 -0
  38. package/src/commands/pqc.js +393 -0
  39. package/src/commands/quantization.js +351 -0
  40. package/src/commands/rcache.js +271 -0
  41. package/src/commands/recommend.js +340 -0
  42. package/src/commands/runtime.js +307 -0
  43. package/src/commands/scim.js +262 -0
  44. package/src/commands/session.js +258 -0
  45. package/src/commands/skill.js +267 -1
  46. package/src/commands/social.js +256 -0
  47. package/src/commands/sso.js +186 -1
  48. package/src/commands/sync.js +256 -0
  49. package/src/commands/tech.js +338 -0
  50. package/src/commands/tenant.js +351 -0
  51. package/src/commands/tokens.js +269 -0
  52. package/src/commands/trust.js +249 -0
  53. package/src/commands/wallet.js +277 -0
  54. package/src/commands/workflow.js +171 -0
  55. package/src/index.js +4 -0
  56. package/src/lib/agent-coordinator.js +325 -0
  57. package/src/lib/agent-network.js +387 -0
  58. package/src/lib/agent-router.js +395 -0
  59. package/src/lib/aiops.js +478 -0
  60. package/src/lib/audit-logger.js +379 -0
  61. package/src/lib/automation-engine.js +330 -0
  62. package/src/lib/autonomous-developer.js +350 -0
  63. package/src/lib/code-agent.js +323 -0
  64. package/src/lib/collaboration-governance.js +364 -0
  65. package/src/lib/community-governance.js +436 -0
  66. package/src/lib/compliance-manager.js +434 -0
  67. package/src/lib/content-recommendation.js +469 -0
  68. package/src/lib/crypto-manager.js +350 -0
  69. package/src/lib/dbevo.js +338 -0
  70. package/src/lib/decentral-infra.js +340 -0
  71. package/src/lib/did-manager.js +367 -0
  72. package/src/lib/hardening-manager.js +348 -0
  73. package/src/lib/hook-manager.js +380 -0
  74. package/src/lib/instinct-manager.js +332 -0
  75. package/src/lib/ipfs-storage.js +334 -0
  76. package/src/lib/knowledge-exporter.js +381 -0
  77. package/src/lib/knowledge-graph.js +432 -0
  78. package/src/lib/knowledge-importer.js +379 -0
  79. package/src/lib/llm-providers.js +391 -0
  80. package/src/lib/mcp-registry.js +333 -0
  81. package/src/lib/memory-manager.js +330 -0
  82. package/src/lib/multimodal.js +346 -0
  83. package/src/lib/nl-programming.js +343 -0
  84. package/src/lib/note-versioning.js +327 -0
  85. package/src/lib/org-manager.js +323 -0
  86. package/src/lib/p2p-manager.js +387 -0
  87. package/src/lib/perception.js +346 -0
  88. package/src/lib/perf-tuning.js +4 -1
  89. package/src/lib/permanent-memory.js +320 -0
  90. package/src/lib/plugin-ecosystem.js +377 -0
  91. package/src/lib/pqc-manager.js +368 -0
  92. package/src/lib/protocol-fusion.js +417 -0
  93. package/src/lib/quantization.js +325 -0
  94. package/src/lib/response-cache.js +327 -0
  95. package/src/lib/scim-manager.js +329 -0
  96. package/src/lib/session-manager.js +329 -0
  97. package/src/lib/skill-loader.js +377 -0
  98. package/src/lib/social-manager.js +326 -0
  99. package/src/lib/sso-manager.js +332 -0
  100. package/src/lib/sync-manager.js +326 -0
  101. package/src/lib/tech-learning-engine.js +369 -0
  102. package/src/lib/tenant-saas.js +460 -0
  103. package/src/lib/threat-intel.js +335 -0
  104. package/src/lib/token-incentive.js +293 -0
  105. package/src/lib/token-tracker.js +329 -0
  106. package/src/lib/trust-security.js +390 -0
  107. package/src/lib/ueba.js +389 -0
  108. package/src/lib/universal-runtime.js +325 -0
  109. package/src/lib/wallet-manager.js +326 -0
  110. package/src/lib/workflow-engine.js +322 -0
@@ -382,3 +382,328 @@ export async function executeDecomposedTask(decomposition, options = {}) {
382
382
  summary: aggregated.summary,
383
383
  };
384
384
  }
385
+
386
+ // ===== V2 Surface (cli 0.131.0) — in-memory governance =====
387
+ export const COORD_AGENT_MATURITY_V2 = Object.freeze({
388
+ PENDING: "pending",
389
+ ACTIVE: "active",
390
+ IDLE: "idle",
391
+ RETIRED: "retired",
392
+ });
393
+ export const COORD_ASSIGNMENT_LIFECYCLE_V2 = Object.freeze({
394
+ QUEUED: "queued",
395
+ DISPATCHED: "dispatched",
396
+ COMPLETED: "completed",
397
+ FAILED: "failed",
398
+ CANCELLED: "cancelled",
399
+ });
400
+
401
+ const _CA_V2 = COORD_AGENT_MATURITY_V2;
402
+ const _CL_V2 = COORD_ASSIGNMENT_LIFECYCLE_V2;
403
+ const _CA_TRANS_V2 = new Map([
404
+ [_CA_V2.PENDING, new Set([_CA_V2.ACTIVE, _CA_V2.RETIRED])],
405
+ [_CA_V2.ACTIVE, new Set([_CA_V2.IDLE, _CA_V2.RETIRED])],
406
+ [_CA_V2.IDLE, new Set([_CA_V2.ACTIVE, _CA_V2.RETIRED])],
407
+ [_CA_V2.RETIRED, new Set()],
408
+ ]);
409
+ const _CL_TRANS_V2 = new Map([
410
+ [_CL_V2.QUEUED, new Set([_CL_V2.DISPATCHED, _CL_V2.CANCELLED])],
411
+ [
412
+ _CL_V2.DISPATCHED,
413
+ new Set([_CL_V2.COMPLETED, _CL_V2.FAILED, _CL_V2.CANCELLED]),
414
+ ],
415
+ [_CL_V2.COMPLETED, new Set()],
416
+ [_CL_V2.FAILED, new Set()],
417
+ [_CL_V2.CANCELLED, new Set()],
418
+ ]);
419
+ const _CL_TERM_V2 = new Set([
420
+ _CL_V2.COMPLETED,
421
+ _CL_V2.FAILED,
422
+ _CL_V2.CANCELLED,
423
+ ]);
424
+
425
+ const COORD_DEFAULT_MAX_ACTIVE_AGENTS_PER_OWNER = 8;
426
+ const COORD_DEFAULT_MAX_PENDING_ASSIGNMENTS_PER_AGENT = 12;
427
+ const COORD_DEFAULT_AGENT_IDLE_MS = 60 * 60 * 1000;
428
+ const COORD_DEFAULT_ASSIGNMENT_STUCK_MS = 5 * 60 * 1000;
429
+
430
+ const _coordAgentsV2 = new Map();
431
+ const _coordAssignmentsV2 = new Map();
432
+ let _coordConfigV2 = {
433
+ maxActiveAgentsPerOwner: COORD_DEFAULT_MAX_ACTIVE_AGENTS_PER_OWNER,
434
+ maxPendingAssignmentsPerAgent:
435
+ COORD_DEFAULT_MAX_PENDING_ASSIGNMENTS_PER_AGENT,
436
+ agentIdleMs: COORD_DEFAULT_AGENT_IDLE_MS,
437
+ assignmentStuckMs: COORD_DEFAULT_ASSIGNMENT_STUCK_MS,
438
+ };
439
+
440
+ function _coordPosIntV2(n, label) {
441
+ if (typeof n !== "number" || !isFinite(n) || isNaN(n))
442
+ throw new Error(`${label} must be positive integer`);
443
+ const v = Math.floor(n);
444
+ if (v <= 0) throw new Error(`${label} must be positive integer`);
445
+ return v;
446
+ }
447
+
448
+ export function _resetStateAgentCoordinatorV2() {
449
+ _coordAgentsV2.clear();
450
+ _coordAssignmentsV2.clear();
451
+ _coordConfigV2 = {
452
+ maxActiveAgentsPerOwner: COORD_DEFAULT_MAX_ACTIVE_AGENTS_PER_OWNER,
453
+ maxPendingAssignmentsPerAgent:
454
+ COORD_DEFAULT_MAX_PENDING_ASSIGNMENTS_PER_AGENT,
455
+ agentIdleMs: COORD_DEFAULT_AGENT_IDLE_MS,
456
+ assignmentStuckMs: COORD_DEFAULT_ASSIGNMENT_STUCK_MS,
457
+ };
458
+ }
459
+
460
+ export function setMaxActiveAgentsPerOwnerCoordV2(n) {
461
+ _coordConfigV2.maxActiveAgentsPerOwner = _coordPosIntV2(
462
+ n,
463
+ "maxActiveAgentsPerOwner",
464
+ );
465
+ }
466
+ export function setMaxPendingAssignmentsPerAgentV2(n) {
467
+ _coordConfigV2.maxPendingAssignmentsPerAgent = _coordPosIntV2(
468
+ n,
469
+ "maxPendingAssignmentsPerAgent",
470
+ );
471
+ }
472
+ export function setAgentIdleMsCoordV2(n) {
473
+ _coordConfigV2.agentIdleMs = _coordPosIntV2(n, "agentIdleMs");
474
+ }
475
+ export function setAssignmentStuckMsV2(n) {
476
+ _coordConfigV2.assignmentStuckMs = _coordPosIntV2(n, "assignmentStuckMs");
477
+ }
478
+ export function getMaxActiveAgentsPerOwnerCoordV2() {
479
+ return _coordConfigV2.maxActiveAgentsPerOwner;
480
+ }
481
+ export function getMaxPendingAssignmentsPerAgentV2() {
482
+ return _coordConfigV2.maxPendingAssignmentsPerAgent;
483
+ }
484
+ export function getAgentIdleMsCoordV2() {
485
+ return _coordConfigV2.agentIdleMs;
486
+ }
487
+ export function getAssignmentStuckMsV2() {
488
+ return _coordConfigV2.assignmentStuckMs;
489
+ }
490
+
491
+ function _copyCoordAgentV2(a) {
492
+ return { ...a, metadata: { ...(a.metadata || {}) } };
493
+ }
494
+ function _copyAssignmentV2(a) {
495
+ return { ...a, metadata: { ...(a.metadata || {}) } };
496
+ }
497
+
498
+ export function registerCoordAgentV2({ id, owner, role, name, metadata } = {}) {
499
+ if (!id || typeof id !== "string") throw new Error("id required");
500
+ if (!owner || typeof owner !== "string") throw new Error("owner required");
501
+ if (!role || typeof role !== "string") throw new Error("role required");
502
+ if (_coordAgentsV2.has(id)) throw new Error(`agent ${id} already registered`);
503
+ const now = Date.now();
504
+ const a = {
505
+ id,
506
+ owner,
507
+ role,
508
+ name: name || id,
509
+ status: _CA_V2.PENDING,
510
+ activatedAt: null,
511
+ retiredAt: null,
512
+ lastSeenAt: now,
513
+ createdAt: now,
514
+ metadata: metadata && typeof metadata === "object" ? { ...metadata } : {},
515
+ };
516
+ _coordAgentsV2.set(id, a);
517
+ return _copyCoordAgentV2(a);
518
+ }
519
+
520
+ function _activeCoordAgentCountForOwnerV2(owner) {
521
+ let c = 0;
522
+ for (const a of _coordAgentsV2.values())
523
+ if (a.owner === owner && a.status === _CA_V2.ACTIVE) c++;
524
+ return c;
525
+ }
526
+
527
+ function _transitionCoordAgentV2(id, next) {
528
+ const a = _coordAgentsV2.get(id);
529
+ if (!a) throw new Error(`agent ${id} not found`);
530
+ const allowed = _CA_TRANS_V2.get(a.status);
531
+ if (!allowed || !allowed.has(next))
532
+ throw new Error(`invalid transition ${a.status} -> ${next}`);
533
+ if (next === _CA_V2.ACTIVE && a.status === _CA_V2.PENDING) {
534
+ if (
535
+ _activeCoordAgentCountForOwnerV2(a.owner) >=
536
+ _coordConfigV2.maxActiveAgentsPerOwner
537
+ ) {
538
+ throw new Error(
539
+ `owner ${a.owner} active-agent cap reached (${_coordConfigV2.maxActiveAgentsPerOwner})`,
540
+ );
541
+ }
542
+ }
543
+ const now = Date.now();
544
+ a.status = next;
545
+ if (next === _CA_V2.ACTIVE && !a.activatedAt) a.activatedAt = now;
546
+ if (next === _CA_V2.RETIRED && !a.retiredAt) a.retiredAt = now;
547
+ a.lastSeenAt = now;
548
+ return _copyCoordAgentV2(a);
549
+ }
550
+
551
+ export function activateCoordAgentV2(id) {
552
+ return _transitionCoordAgentV2(id, _CA_V2.ACTIVE);
553
+ }
554
+ export function idleCoordAgentV2(id) {
555
+ return _transitionCoordAgentV2(id, _CA_V2.IDLE);
556
+ }
557
+ export function retireCoordAgentV2(id) {
558
+ return _transitionCoordAgentV2(id, _CA_V2.RETIRED);
559
+ }
560
+ export function touchCoordAgentV2(id) {
561
+ const a = _coordAgentsV2.get(id);
562
+ if (!a) throw new Error(`agent ${id} not found`);
563
+ a.lastSeenAt = Date.now();
564
+ return _copyCoordAgentV2(a);
565
+ }
566
+ export function getCoordAgentV2(id) {
567
+ const a = _coordAgentsV2.get(id);
568
+ return a ? _copyCoordAgentV2(a) : null;
569
+ }
570
+ export function listCoordAgentsV2({ owner, status, role } = {}) {
571
+ const out = [];
572
+ for (const a of _coordAgentsV2.values()) {
573
+ if (owner && a.owner !== owner) continue;
574
+ if (status && a.status !== status) continue;
575
+ if (role && a.role !== role) continue;
576
+ out.push(_copyCoordAgentV2(a));
577
+ }
578
+ return out;
579
+ }
580
+
581
+ function _pendingAssignmentCountForAgentV2(agentId) {
582
+ let c = 0;
583
+ for (const a of _coordAssignmentsV2.values()) {
584
+ if (a.agentId !== agentId) continue;
585
+ if (a.status === _CL_V2.QUEUED || a.status === _CL_V2.DISPATCHED) c++;
586
+ }
587
+ return c;
588
+ }
589
+
590
+ export function createAssignmentV2({ id, agentId, subtask, metadata } = {}) {
591
+ if (!id || typeof id !== "string") throw new Error("id required");
592
+ if (!agentId || typeof agentId !== "string")
593
+ throw new Error("agentId required");
594
+ if (_coordAssignmentsV2.has(id))
595
+ throw new Error(`assignment ${id} already exists`);
596
+ const agent = _coordAgentsV2.get(agentId);
597
+ if (!agent) throw new Error(`agent ${agentId} not found`);
598
+ if (agent.status === _CA_V2.RETIRED)
599
+ throw new Error(`agent ${agentId} retired`);
600
+ if (
601
+ _pendingAssignmentCountForAgentV2(agentId) >=
602
+ _coordConfigV2.maxPendingAssignmentsPerAgent
603
+ ) {
604
+ throw new Error(
605
+ `agent ${agentId} pending-assignment cap reached (${_coordConfigV2.maxPendingAssignmentsPerAgent})`,
606
+ );
607
+ }
608
+ const now = Date.now();
609
+ const a = {
610
+ id,
611
+ agentId,
612
+ subtask: subtask || "untitled",
613
+ status: _CL_V2.QUEUED,
614
+ startedAt: null,
615
+ settledAt: null,
616
+ createdAt: now,
617
+ metadata: metadata && typeof metadata === "object" ? { ...metadata } : {},
618
+ };
619
+ _coordAssignmentsV2.set(id, a);
620
+ return _copyAssignmentV2(a);
621
+ }
622
+
623
+ function _transitionAssignmentV2(id, next, extra = {}) {
624
+ const a = _coordAssignmentsV2.get(id);
625
+ if (!a) throw new Error(`assignment ${id} not found`);
626
+ const allowed = _CL_TRANS_V2.get(a.status);
627
+ if (!allowed || !allowed.has(next))
628
+ throw new Error(`invalid transition ${a.status} -> ${next}`);
629
+ const now = Date.now();
630
+ a.status = next;
631
+ if (next === _CL_V2.DISPATCHED && !a.startedAt) a.startedAt = now;
632
+ if (_CL_TERM_V2.has(next) && !a.settledAt) a.settledAt = now;
633
+ if (extra.error) a.metadata.error = extra.error;
634
+ return _copyAssignmentV2(a);
635
+ }
636
+
637
+ export function dispatchAssignmentV2(id) {
638
+ return _transitionAssignmentV2(id, _CL_V2.DISPATCHED);
639
+ }
640
+ export function completeAssignmentV2(id) {
641
+ return _transitionAssignmentV2(id, _CL_V2.COMPLETED);
642
+ }
643
+ export function failAssignmentV2(id, error) {
644
+ return _transitionAssignmentV2(id, _CL_V2.FAILED, { error });
645
+ }
646
+ export function cancelAssignmentV2(id) {
647
+ return _transitionAssignmentV2(id, _CL_V2.CANCELLED);
648
+ }
649
+ export function getAssignmentV2(id) {
650
+ const a = _coordAssignmentsV2.get(id);
651
+ return a ? _copyAssignmentV2(a) : null;
652
+ }
653
+ export function listAssignmentsV2({ agentId, status } = {}) {
654
+ const out = [];
655
+ for (const a of _coordAssignmentsV2.values()) {
656
+ if (agentId && a.agentId !== agentId) continue;
657
+ if (status && a.status !== status) continue;
658
+ out.push(_copyAssignmentV2(a));
659
+ }
660
+ return out;
661
+ }
662
+
663
+ export function autoIdleCoordAgentsV2({ now } = {}) {
664
+ const t = typeof now === "number" ? now : Date.now();
665
+ const flipped = [];
666
+ for (const a of _coordAgentsV2.values()) {
667
+ if (a.status !== _CA_V2.ACTIVE) continue;
668
+ if (t - a.lastSeenAt > _coordConfigV2.agentIdleMs) {
669
+ a.status = _CA_V2.IDLE;
670
+ a.lastSeenAt = t;
671
+ flipped.push(_copyCoordAgentV2(a));
672
+ }
673
+ }
674
+ return flipped;
675
+ }
676
+
677
+ export function autoFailStuckAssignmentsV2({ now } = {}) {
678
+ const t = typeof now === "number" ? now : Date.now();
679
+ const flipped = [];
680
+ for (const a of _coordAssignmentsV2.values()) {
681
+ if (a.status !== _CL_V2.DISPATCHED) continue;
682
+ if (a.startedAt && t - a.startedAt > _coordConfigV2.assignmentStuckMs) {
683
+ a.status = _CL_V2.FAILED;
684
+ a.settledAt = t;
685
+ a.metadata.error = "stuck-timeout";
686
+ flipped.push(_copyAssignmentV2(a));
687
+ }
688
+ }
689
+ return flipped;
690
+ }
691
+
692
+ export function getAgentCoordinatorStatsV2() {
693
+ const agentsByStatus = {};
694
+ for (const s of Object.values(_CA_V2)) agentsByStatus[s] = 0;
695
+ for (const a of _coordAgentsV2.values()) agentsByStatus[a.status]++;
696
+ const assignmentsByStatus = {};
697
+ for (const s of Object.values(_CL_V2)) assignmentsByStatus[s] = 0;
698
+ for (const a of _coordAssignmentsV2.values()) assignmentsByStatus[a.status]++;
699
+ return {
700
+ totalAgentsV2: _coordAgentsV2.size,
701
+ totalAssignmentsV2: _coordAssignmentsV2.size,
702
+ maxActiveAgentsPerOwner: _coordConfigV2.maxActiveAgentsPerOwner,
703
+ maxPendingAssignmentsPerAgent: _coordConfigV2.maxPendingAssignmentsPerAgent,
704
+ agentIdleMs: _coordConfigV2.agentIdleMs,
705
+ assignmentStuckMs: _coordConfigV2.assignmentStuckMs,
706
+ agentsByStatus,
707
+ assignmentsByStatus,
708
+ };
709
+ }
@@ -1119,3 +1119,390 @@ export function getNetworkConfig() {
1119
1119
  credStatuses: Object.values(CRED_STATUS),
1120
1120
  };
1121
1121
  }
1122
+
1123
+ /* ─────────────────────────────────────────────────────────────────
1124
+ * V2 Governance Layer (in-memory, independent of SQLite tables)
1125
+ *
1126
+ * Agent maturity: pending → active → suspended → revoked
1127
+ * - revoked terminal
1128
+ * - suspended → active recovery (cap-exempt)
1129
+ *
1130
+ * Task lifecycle: queued → running → completed | failed | cancelled
1131
+ * - 3 terminals
1132
+ * - per-agent pending-task cap counts queued+running
1133
+ *
1134
+ * Per-network active-agent cap on pending→active only (recovery exempt).
1135
+ * Per-agent pending-task cap enforced at createTaskV2.
1136
+ *
1137
+ * Auto-flip:
1138
+ * - autoSuspendIdleAgentsV2 active w/ lastSeenAt past idle threshold → suspended
1139
+ * - autoFailStuckTasksV2 running w/ startedAt past stuck threshold → failed
1140
+ * ───────────────────────────────────────────────────────────────── */
1141
+
1142
+ export const AGENT_MATURITY_V2 = Object.freeze({
1143
+ PENDING: "pending",
1144
+ ACTIVE: "active",
1145
+ SUSPENDED: "suspended",
1146
+ REVOKED: "revoked",
1147
+ });
1148
+
1149
+ export const TASK_LIFECYCLE_V2 = Object.freeze({
1150
+ QUEUED: "queued",
1151
+ RUNNING: "running",
1152
+ COMPLETED: "completed",
1153
+ FAILED: "failed",
1154
+ CANCELLED: "cancelled",
1155
+ });
1156
+
1157
+ const _AGENT_TRANSITIONS_V2 = new Map([
1158
+ [
1159
+ AGENT_MATURITY_V2.PENDING,
1160
+ new Set([AGENT_MATURITY_V2.ACTIVE, AGENT_MATURITY_V2.REVOKED]),
1161
+ ],
1162
+ [
1163
+ AGENT_MATURITY_V2.ACTIVE,
1164
+ new Set([AGENT_MATURITY_V2.SUSPENDED, AGENT_MATURITY_V2.REVOKED]),
1165
+ ],
1166
+ [
1167
+ AGENT_MATURITY_V2.SUSPENDED,
1168
+ new Set([AGENT_MATURITY_V2.ACTIVE, AGENT_MATURITY_V2.REVOKED]),
1169
+ ],
1170
+ [AGENT_MATURITY_V2.REVOKED, new Set()],
1171
+ ]);
1172
+ const _AGENT_TERMINALS_V2 = new Set([AGENT_MATURITY_V2.REVOKED]);
1173
+
1174
+ const _TASK_TRANSITIONS_V2 = new Map([
1175
+ [
1176
+ TASK_LIFECYCLE_V2.QUEUED,
1177
+ new Set([TASK_LIFECYCLE_V2.RUNNING, TASK_LIFECYCLE_V2.CANCELLED]),
1178
+ ],
1179
+ [
1180
+ TASK_LIFECYCLE_V2.RUNNING,
1181
+ new Set([
1182
+ TASK_LIFECYCLE_V2.COMPLETED,
1183
+ TASK_LIFECYCLE_V2.FAILED,
1184
+ TASK_LIFECYCLE_V2.CANCELLED,
1185
+ ]),
1186
+ ],
1187
+ [TASK_LIFECYCLE_V2.COMPLETED, new Set()],
1188
+ [TASK_LIFECYCLE_V2.FAILED, new Set()],
1189
+ [TASK_LIFECYCLE_V2.CANCELLED, new Set()],
1190
+ ]);
1191
+ const _TASK_TERMINALS_V2 = new Set([
1192
+ TASK_LIFECYCLE_V2.COMPLETED,
1193
+ TASK_LIFECYCLE_V2.FAILED,
1194
+ TASK_LIFECYCLE_V2.CANCELLED,
1195
+ ]);
1196
+
1197
+ export const AGENT_DEFAULT_MAX_ACTIVE_PER_NETWORK = 50;
1198
+ export const AGENT_DEFAULT_MAX_PENDING_TASKS_PER_AGENT = 10;
1199
+ export const AGENT_DEFAULT_AGENT_IDLE_MS = 7 * 24 * 60 * 60 * 1000; // 7 days
1200
+ export const AGENT_DEFAULT_TASK_STUCK_MS = 30 * 60 * 1000; // 30 min
1201
+
1202
+ const _stateAnetV2 = {
1203
+ agents: new Map(),
1204
+ tasks: new Map(),
1205
+ maxActiveAgentsPerNetwork: AGENT_DEFAULT_MAX_ACTIVE_PER_NETWORK,
1206
+ maxPendingTasksPerAgent: AGENT_DEFAULT_MAX_PENDING_TASKS_PER_AGENT,
1207
+ agentIdleMs: AGENT_DEFAULT_AGENT_IDLE_MS,
1208
+ taskStuckMs: AGENT_DEFAULT_TASK_STUCK_MS,
1209
+ };
1210
+
1211
+ function _posIntAnetV2(n, label) {
1212
+ const v = Math.floor(n);
1213
+ if (!Number.isFinite(v) || v <= 0) {
1214
+ throw new Error(`${label} must be a positive integer`);
1215
+ }
1216
+ return v;
1217
+ }
1218
+
1219
+ function _copyAgentV2(a) {
1220
+ return { ...a, metadata: { ...a.metadata } };
1221
+ }
1222
+
1223
+ function _copyTaskV2(t) {
1224
+ return { ...t, metadata: { ...t.metadata } };
1225
+ }
1226
+
1227
+ export function getMaxActiveAgentsPerNetworkV2() {
1228
+ return _stateAnetV2.maxActiveAgentsPerNetwork;
1229
+ }
1230
+
1231
+ export function setMaxActiveAgentsPerNetworkV2(n) {
1232
+ _stateAnetV2.maxActiveAgentsPerNetwork = _posIntAnetV2(
1233
+ n,
1234
+ "maxActiveAgentsPerNetwork",
1235
+ );
1236
+ }
1237
+
1238
+ export function getMaxPendingTasksPerAgentV2() {
1239
+ return _stateAnetV2.maxPendingTasksPerAgent;
1240
+ }
1241
+
1242
+ export function setMaxPendingTasksPerAgentV2(n) {
1243
+ _stateAnetV2.maxPendingTasksPerAgent = _posIntAnetV2(
1244
+ n,
1245
+ "maxPendingTasksPerAgent",
1246
+ );
1247
+ }
1248
+
1249
+ export function getAgentIdleMsV2() {
1250
+ return _stateAnetV2.agentIdleMs;
1251
+ }
1252
+
1253
+ export function setAgentIdleMsV2(ms) {
1254
+ _stateAnetV2.agentIdleMs = _posIntAnetV2(ms, "agentIdleMs");
1255
+ }
1256
+
1257
+ export function getTaskStuckMsV2() {
1258
+ return _stateAnetV2.taskStuckMs;
1259
+ }
1260
+
1261
+ export function setTaskStuckMsV2(ms) {
1262
+ _stateAnetV2.taskStuckMs = _posIntAnetV2(ms, "taskStuckMs");
1263
+ }
1264
+
1265
+ export function getActiveAgentCountV2(networkId) {
1266
+ let count = 0;
1267
+ for (const a of _stateAnetV2.agents.values()) {
1268
+ if (a.networkId === networkId && a.status === AGENT_MATURITY_V2.ACTIVE) {
1269
+ count++;
1270
+ }
1271
+ }
1272
+ return count;
1273
+ }
1274
+
1275
+ export function getPendingTaskCountV2(agentId) {
1276
+ let count = 0;
1277
+ for (const t of _stateAnetV2.tasks.values()) {
1278
+ if (
1279
+ t.agentId === agentId &&
1280
+ (t.status === TASK_LIFECYCLE_V2.QUEUED ||
1281
+ t.status === TASK_LIFECYCLE_V2.RUNNING)
1282
+ ) {
1283
+ count++;
1284
+ }
1285
+ }
1286
+ return count;
1287
+ }
1288
+
1289
+ export function registerAgentV2(
1290
+ id,
1291
+ { networkId, did, displayName, metadata } = {},
1292
+ ) {
1293
+ if (!id) throw new Error("agent id is required");
1294
+ if (!networkId) throw new Error("networkId is required");
1295
+ if (!did) throw new Error("did is required");
1296
+ if (_stateAnetV2.agents.has(id))
1297
+ throw new Error(`agent ${id} already exists`);
1298
+ const now = Date.now();
1299
+ const agent = {
1300
+ id,
1301
+ networkId,
1302
+ did,
1303
+ displayName: displayName || id,
1304
+ status: AGENT_MATURITY_V2.PENDING,
1305
+ createdAt: now,
1306
+ lastSeenAt: now,
1307
+ activatedAt: null,
1308
+ revokedAt: null,
1309
+ metadata: metadata ? { ...metadata } : {},
1310
+ };
1311
+ _stateAnetV2.agents.set(id, agent);
1312
+ return _copyAgentV2(agent);
1313
+ }
1314
+
1315
+ export function getAgentV2(id) {
1316
+ const a = _stateAnetV2.agents.get(id);
1317
+ return a ? _copyAgentV2(a) : null;
1318
+ }
1319
+
1320
+ export function listAgentsV2({ networkId, status } = {}) {
1321
+ const out = [];
1322
+ for (const a of _stateAnetV2.agents.values()) {
1323
+ if (networkId && a.networkId !== networkId) continue;
1324
+ if (status && a.status !== status) continue;
1325
+ out.push(_copyAgentV2(a));
1326
+ }
1327
+ return out;
1328
+ }
1329
+
1330
+ export function setAgentStatusV2(id, next) {
1331
+ const a = _stateAnetV2.agents.get(id);
1332
+ if (!a) throw new Error(`agent ${id} not found`);
1333
+ const allowed = _AGENT_TRANSITIONS_V2.get(a.status);
1334
+ if (!allowed || !allowed.has(next)) {
1335
+ throw new Error(`invalid agent transition: ${a.status} → ${next}`);
1336
+ }
1337
+ if (
1338
+ a.status === AGENT_MATURITY_V2.PENDING &&
1339
+ next === AGENT_MATURITY_V2.ACTIVE
1340
+ ) {
1341
+ const count = getActiveAgentCountV2(a.networkId);
1342
+ if (count >= _stateAnetV2.maxActiveAgentsPerNetwork) {
1343
+ throw new Error(
1344
+ `network ${a.networkId} active-agent cap reached (${count}/${_stateAnetV2.maxActiveAgentsPerNetwork})`,
1345
+ );
1346
+ }
1347
+ }
1348
+ const now = Date.now();
1349
+ a.status = next;
1350
+ a.lastSeenAt = now;
1351
+ if (next === AGENT_MATURITY_V2.ACTIVE && !a.activatedAt) a.activatedAt = now;
1352
+ if (_AGENT_TERMINALS_V2.has(next) && !a.revokedAt) a.revokedAt = now;
1353
+ return _copyAgentV2(a);
1354
+ }
1355
+
1356
+ export function activateAgentV2(id) {
1357
+ return setAgentStatusV2(id, AGENT_MATURITY_V2.ACTIVE);
1358
+ }
1359
+
1360
+ export function suspendAgentV2(id) {
1361
+ return setAgentStatusV2(id, AGENT_MATURITY_V2.SUSPENDED);
1362
+ }
1363
+
1364
+ export function revokeAgentV2(id) {
1365
+ return setAgentStatusV2(id, AGENT_MATURITY_V2.REVOKED);
1366
+ }
1367
+
1368
+ export function touchAgentV2(id) {
1369
+ const a = _stateAnetV2.agents.get(id);
1370
+ if (!a) throw new Error(`agent ${id} not found`);
1371
+ a.lastSeenAt = Date.now();
1372
+ return _copyAgentV2(a);
1373
+ }
1374
+
1375
+ export function createTaskV2(id, { agentId, kind, metadata } = {}) {
1376
+ if (!id) throw new Error("task id is required");
1377
+ if (!agentId) throw new Error("agentId is required");
1378
+ if (_stateAnetV2.tasks.has(id)) throw new Error(`task ${id} already exists`);
1379
+ const agent = _stateAnetV2.agents.get(agentId);
1380
+ if (!agent) throw new Error(`agent ${agentId} not found`);
1381
+ const pending = getPendingTaskCountV2(agentId);
1382
+ if (pending >= _stateAnetV2.maxPendingTasksPerAgent) {
1383
+ throw new Error(
1384
+ `agent ${agentId} pending-task cap reached (${pending}/${_stateAnetV2.maxPendingTasksPerAgent})`,
1385
+ );
1386
+ }
1387
+ const now = Date.now();
1388
+ const task = {
1389
+ id,
1390
+ agentId,
1391
+ kind: kind || "invoke",
1392
+ status: TASK_LIFECYCLE_V2.QUEUED,
1393
+ createdAt: now,
1394
+ lastSeenAt: now,
1395
+ startedAt: null,
1396
+ settledAt: null,
1397
+ metadata: metadata ? { ...metadata } : {},
1398
+ };
1399
+ _stateAnetV2.tasks.set(id, task);
1400
+ return _copyTaskV2(task);
1401
+ }
1402
+
1403
+ export function getTaskV2(id) {
1404
+ const t = _stateAnetV2.tasks.get(id);
1405
+ return t ? _copyTaskV2(t) : null;
1406
+ }
1407
+
1408
+ export function listTasksV2({ agentId, status } = {}) {
1409
+ const out = [];
1410
+ for (const t of _stateAnetV2.tasks.values()) {
1411
+ if (agentId && t.agentId !== agentId) continue;
1412
+ if (status && t.status !== status) continue;
1413
+ out.push(_copyTaskV2(t));
1414
+ }
1415
+ return out;
1416
+ }
1417
+
1418
+ export function setTaskStatusV2(id, next) {
1419
+ const t = _stateAnetV2.tasks.get(id);
1420
+ if (!t) throw new Error(`task ${id} not found`);
1421
+ const allowed = _TASK_TRANSITIONS_V2.get(t.status);
1422
+ if (!allowed || !allowed.has(next)) {
1423
+ throw new Error(`invalid task transition: ${t.status} → ${next}`);
1424
+ }
1425
+ const now = Date.now();
1426
+ t.status = next;
1427
+ t.lastSeenAt = now;
1428
+ if (next === TASK_LIFECYCLE_V2.RUNNING && !t.startedAt) t.startedAt = now;
1429
+ if (_TASK_TERMINALS_V2.has(next) && !t.settledAt) t.settledAt = now;
1430
+ return _copyTaskV2(t);
1431
+ }
1432
+
1433
+ export function startTaskV2(id) {
1434
+ return setTaskStatusV2(id, TASK_LIFECYCLE_V2.RUNNING);
1435
+ }
1436
+
1437
+ export function completeTaskV2(id) {
1438
+ return setTaskStatusV2(id, TASK_LIFECYCLE_V2.COMPLETED);
1439
+ }
1440
+
1441
+ export function failTaskV2(id) {
1442
+ return setTaskStatusV2(id, TASK_LIFECYCLE_V2.FAILED);
1443
+ }
1444
+
1445
+ export function cancelTaskV2(id) {
1446
+ return setTaskStatusV2(id, TASK_LIFECYCLE_V2.CANCELLED);
1447
+ }
1448
+
1449
+ export function autoSuspendIdleAgentsV2({ now = Date.now() } = {}) {
1450
+ const flipped = [];
1451
+ for (const a of _stateAnetV2.agents.values()) {
1452
+ if (
1453
+ a.status === AGENT_MATURITY_V2.ACTIVE &&
1454
+ now - a.lastSeenAt >= _stateAnetV2.agentIdleMs
1455
+ ) {
1456
+ a.status = AGENT_MATURITY_V2.SUSPENDED;
1457
+ a.lastSeenAt = now;
1458
+ flipped.push(a.id);
1459
+ }
1460
+ }
1461
+ return { flipped, count: flipped.length };
1462
+ }
1463
+
1464
+ export function autoFailStuckTasksV2({ now = Date.now() } = {}) {
1465
+ const flipped = [];
1466
+ for (const t of _stateAnetV2.tasks.values()) {
1467
+ if (
1468
+ t.status === TASK_LIFECYCLE_V2.RUNNING &&
1469
+ t.startedAt &&
1470
+ now - t.startedAt >= _stateAnetV2.taskStuckMs
1471
+ ) {
1472
+ t.status = TASK_LIFECYCLE_V2.FAILED;
1473
+ t.lastSeenAt = now;
1474
+ if (!t.settledAt) t.settledAt = now;
1475
+ flipped.push(t.id);
1476
+ }
1477
+ }
1478
+ return { flipped, count: flipped.length };
1479
+ }
1480
+
1481
+ export function getAgentNetworkStatsV2() {
1482
+ const agentsByStatus = {};
1483
+ for (const s of Object.values(AGENT_MATURITY_V2)) agentsByStatus[s] = 0;
1484
+ for (const a of _stateAnetV2.agents.values()) agentsByStatus[a.status]++;
1485
+ const tasksByStatus = {};
1486
+ for (const s of Object.values(TASK_LIFECYCLE_V2)) tasksByStatus[s] = 0;
1487
+ for (const t of _stateAnetV2.tasks.values()) tasksByStatus[t.status]++;
1488
+ return {
1489
+ totalAgentsV2: _stateAnetV2.agents.size,
1490
+ totalTasksV2: _stateAnetV2.tasks.size,
1491
+ maxActiveAgentsPerNetwork: _stateAnetV2.maxActiveAgentsPerNetwork,
1492
+ maxPendingTasksPerAgent: _stateAnetV2.maxPendingTasksPerAgent,
1493
+ agentIdleMs: _stateAnetV2.agentIdleMs,
1494
+ taskStuckMs: _stateAnetV2.taskStuckMs,
1495
+ agentsByStatus,
1496
+ tasksByStatus,
1497
+ };
1498
+ }
1499
+
1500
+ export function _resetStateAgentNetworkV2() {
1501
+ _stateAnetV2.agents.clear();
1502
+ _stateAnetV2.tasks.clear();
1503
+ _stateAnetV2.maxActiveAgentsPerNetwork = AGENT_DEFAULT_MAX_ACTIVE_PER_NETWORK;
1504
+ _stateAnetV2.maxPendingTasksPerAgent =
1505
+ AGENT_DEFAULT_MAX_PENDING_TASKS_PER_AGENT;
1506
+ _stateAnetV2.agentIdleMs = AGENT_DEFAULT_AGENT_IDLE_MS;
1507
+ _stateAnetV2.taskStuckMs = AGENT_DEFAULT_TASK_STUCK_MS;
1508
+ }