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
@@ -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
+ }
@@ -846,3 +846,108 @@ export function _resetV2State() {
846
846
  _v2TaskContributions.clear();
847
847
  _v2Distributions.length = 0;
848
848
  }
849
+
850
+
851
+ // ===== V2 Surface: Agent Economy governance overlay (CLI v0.137.0) =====
852
+ export const ECONOMY_ACCOUNT_MATURITY_V2 = Object.freeze({
853
+ PENDING: "pending", ACTIVE: "active", FROZEN: "frozen", CLOSED: "closed",
854
+ });
855
+ export const ECONOMY_TX_LIFECYCLE_V2 = Object.freeze({
856
+ QUEUED: "queued", PROCESSING: "processing", SETTLED: "settled", FAILED: "failed", CANCELLED: "cancelled",
857
+ });
858
+
859
+ const _econAcctTrans = new Map([
860
+ [ECONOMY_ACCOUNT_MATURITY_V2.PENDING, new Set([ECONOMY_ACCOUNT_MATURITY_V2.ACTIVE, ECONOMY_ACCOUNT_MATURITY_V2.CLOSED])],
861
+ [ECONOMY_ACCOUNT_MATURITY_V2.ACTIVE, new Set([ECONOMY_ACCOUNT_MATURITY_V2.FROZEN, ECONOMY_ACCOUNT_MATURITY_V2.CLOSED])],
862
+ [ECONOMY_ACCOUNT_MATURITY_V2.FROZEN, new Set([ECONOMY_ACCOUNT_MATURITY_V2.ACTIVE, ECONOMY_ACCOUNT_MATURITY_V2.CLOSED])],
863
+ [ECONOMY_ACCOUNT_MATURITY_V2.CLOSED, new Set()],
864
+ ]);
865
+ const _econAcctTerminal = new Set([ECONOMY_ACCOUNT_MATURITY_V2.CLOSED]);
866
+ const _econTxTrans = new Map([
867
+ [ECONOMY_TX_LIFECYCLE_V2.QUEUED, new Set([ECONOMY_TX_LIFECYCLE_V2.PROCESSING, ECONOMY_TX_LIFECYCLE_V2.CANCELLED])],
868
+ [ECONOMY_TX_LIFECYCLE_V2.PROCESSING, new Set([ECONOMY_TX_LIFECYCLE_V2.SETTLED, ECONOMY_TX_LIFECYCLE_V2.FAILED, ECONOMY_TX_LIFECYCLE_V2.CANCELLED])],
869
+ [ECONOMY_TX_LIFECYCLE_V2.SETTLED, new Set()],
870
+ [ECONOMY_TX_LIFECYCLE_V2.FAILED, new Set()],
871
+ [ECONOMY_TX_LIFECYCLE_V2.CANCELLED, new Set()],
872
+ ]);
873
+
874
+ const _econAccts = new Map();
875
+ const _econTxs = new Map();
876
+ let _econMaxActivePerHolder = 20;
877
+ let _econMaxPendingPerAcct = 30;
878
+ let _econAcctIdleMs = 7 * 24 * 60 * 60 * 1000;
879
+ let _econTxStuckMs = 5 * 60 * 1000;
880
+
881
+ function _econPos(n, lbl) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${lbl} must be positive integer`); return v; }
882
+
883
+ export function setMaxActiveEconomyAccountsPerHolderV2(n) { _econMaxActivePerHolder = _econPos(n, "maxActiveEconomyAccountsPerHolder"); }
884
+ export function getMaxActiveEconomyAccountsPerHolderV2() { return _econMaxActivePerHolder; }
885
+ export function setMaxPendingEconomyTxsPerAccountV2(n) { _econMaxPendingPerAcct = _econPos(n, "maxPendingEconomyTxsPerAccount"); }
886
+ export function getMaxPendingEconomyTxsPerAccountV2() { return _econMaxPendingPerAcct; }
887
+ export function setEconomyAccountIdleMsV2(n) { _econAcctIdleMs = _econPos(n, "economyAccountIdleMs"); }
888
+ export function getEconomyAccountIdleMsV2() { return _econAcctIdleMs; }
889
+ export function setEconomyTxStuckMsV2(n) { _econTxStuckMs = _econPos(n, "economyTxStuckMs"); }
890
+ export function getEconomyTxStuckMsV2() { return _econTxStuckMs; }
891
+
892
+ export function _resetStateAgentEconomyV2() {
893
+ _econAccts.clear(); _econTxs.clear();
894
+ _econMaxActivePerHolder = 20; _econMaxPendingPerAcct = 30;
895
+ _econAcctIdleMs = 7 * 24 * 60 * 60 * 1000; _econTxStuckMs = 5 * 60 * 1000;
896
+ }
897
+
898
+ export function registerEconomyAccountV2({ id, holder, currency, metadata } = {}) {
899
+ if (!id || typeof id !== "string") throw new Error("id is required");
900
+ if (!holder || typeof holder !== "string") throw new Error("holder is required");
901
+ if (_econAccts.has(id)) throw new Error(`economy account ${id} already registered`);
902
+ const now = Date.now();
903
+ const a = { id, holder, currency: currency || "CLC", status: ECONOMY_ACCOUNT_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, closedAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
904
+ _econAccts.set(id, a);
905
+ return { ...a, metadata: { ...a.metadata } };
906
+ }
907
+ function _econCheckA(from, to) { const a = _econAcctTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid economy account transition ${from} → ${to}`); }
908
+ function _econCountActive(holder) { let n = 0; for (const a of _econAccts.values()) if (a.holder === holder && a.status === ECONOMY_ACCOUNT_MATURITY_V2.ACTIVE) n++; return n; }
909
+
910
+ export function activateEconomyAccountV2(id) {
911
+ const a = _econAccts.get(id); if (!a) throw new Error(`economy account ${id} not found`);
912
+ _econCheckA(a.status, ECONOMY_ACCOUNT_MATURITY_V2.ACTIVE);
913
+ const recovery = a.status === ECONOMY_ACCOUNT_MATURITY_V2.FROZEN;
914
+ if (!recovery) { const c = _econCountActive(a.holder); if (c >= _econMaxActivePerHolder) throw new Error(`max active economy accounts per holder (${_econMaxActivePerHolder}) reached for ${a.holder}`); }
915
+ const now = Date.now(); a.status = ECONOMY_ACCOUNT_MATURITY_V2.ACTIVE; a.updatedAt = now; a.lastTouchedAt = now; if (!a.activatedAt) a.activatedAt = now;
916
+ return { ...a, metadata: { ...a.metadata } };
917
+ }
918
+ export function freezeEconomyAccountV2(id) { const a = _econAccts.get(id); if (!a) throw new Error(`economy account ${id} not found`); _econCheckA(a.status, ECONOMY_ACCOUNT_MATURITY_V2.FROZEN); a.status = ECONOMY_ACCOUNT_MATURITY_V2.FROZEN; a.updatedAt = Date.now(); return { ...a, metadata: { ...a.metadata } }; }
919
+ export function closeEconomyAccountV2(id) { const a = _econAccts.get(id); if (!a) throw new Error(`economy account ${id} not found`); _econCheckA(a.status, ECONOMY_ACCOUNT_MATURITY_V2.CLOSED); const now = Date.now(); a.status = ECONOMY_ACCOUNT_MATURITY_V2.CLOSED; a.updatedAt = now; if (!a.closedAt) a.closedAt = now; return { ...a, metadata: { ...a.metadata } }; }
920
+ export function touchEconomyAccountV2(id) { const a = _econAccts.get(id); if (!a) throw new Error(`economy account ${id} not found`); if (_econAcctTerminal.has(a.status)) throw new Error(`cannot touch terminal economy account ${id}`); const now = Date.now(); a.lastTouchedAt = now; a.updatedAt = now; return { ...a, metadata: { ...a.metadata } }; }
921
+ export function getEconomyAccountV2(id) { const a = _econAccts.get(id); if (!a) return null; return { ...a, metadata: { ...a.metadata } }; }
922
+ export function listEconomyAccountsV2() { return [..._econAccts.values()].map((a) => ({ ...a, metadata: { ...a.metadata } })); }
923
+
924
+ function _econCountPending(aid) { let n = 0; for (const t of _econTxs.values()) if (t.accountId === aid && (t.status === ECONOMY_TX_LIFECYCLE_V2.QUEUED || t.status === ECONOMY_TX_LIFECYCLE_V2.PROCESSING)) n++; return n; }
925
+
926
+ export function createEconomyTxV2({ id, accountId, amount, metadata } = {}) {
927
+ if (!id || typeof id !== "string") throw new Error("id is required");
928
+ if (!accountId || typeof accountId !== "string") throw new Error("accountId is required");
929
+ if (_econTxs.has(id)) throw new Error(`economy tx ${id} already exists`);
930
+ if (!_econAccts.has(accountId)) throw new Error(`economy account ${accountId} not found`);
931
+ const pending = _econCountPending(accountId);
932
+ if (pending >= _econMaxPendingPerAcct) throw new Error(`max pending economy txs per account (${_econMaxPendingPerAcct}) reached for ${accountId}`);
933
+ const now = Date.now();
934
+ const t = { id, accountId, amount: amount || "0", status: ECONOMY_TX_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
935
+ _econTxs.set(id, t);
936
+ return { ...t, metadata: { ...t.metadata } };
937
+ }
938
+ function _econCheckT(from, to) { const a = _econTxTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid economy tx transition ${from} → ${to}`); }
939
+ export function startEconomyTxV2(id) { const t = _econTxs.get(id); if (!t) throw new Error(`economy tx ${id} not found`); _econCheckT(t.status, ECONOMY_TX_LIFECYCLE_V2.PROCESSING); const now = Date.now(); t.status = ECONOMY_TX_LIFECYCLE_V2.PROCESSING; t.updatedAt = now; if (!t.startedAt) t.startedAt = now; return { ...t, metadata: { ...t.metadata } }; }
940
+ export function settleEconomyTxV2(id) { const t = _econTxs.get(id); if (!t) throw new Error(`economy tx ${id} not found`); _econCheckT(t.status, ECONOMY_TX_LIFECYCLE_V2.SETTLED); const now = Date.now(); t.status = ECONOMY_TX_LIFECYCLE_V2.SETTLED; t.updatedAt = now; if (!t.settledAt) t.settledAt = now; return { ...t, metadata: { ...t.metadata } }; }
941
+ export function failEconomyTxV2(id, reason) { const t = _econTxs.get(id); if (!t) throw new Error(`economy tx ${id} not found`); _econCheckT(t.status, ECONOMY_TX_LIFECYCLE_V2.FAILED); const now = Date.now(); t.status = ECONOMY_TX_LIFECYCLE_V2.FAILED; t.updatedAt = now; if (!t.settledAt) t.settledAt = now; if (reason) t.metadata.failReason = String(reason); return { ...t, metadata: { ...t.metadata } }; }
942
+ export function cancelEconomyTxV2(id, reason) { const t = _econTxs.get(id); if (!t) throw new Error(`economy tx ${id} not found`); _econCheckT(t.status, ECONOMY_TX_LIFECYCLE_V2.CANCELLED); const now = Date.now(); t.status = ECONOMY_TX_LIFECYCLE_V2.CANCELLED; t.updatedAt = now; if (!t.settledAt) t.settledAt = now; if (reason) t.metadata.cancelReason = String(reason); return { ...t, metadata: { ...t.metadata } }; }
943
+ export function getEconomyTxV2(id) { const t = _econTxs.get(id); if (!t) return null; return { ...t, metadata: { ...t.metadata } }; }
944
+ export function listEconomyTxsV2() { return [..._econTxs.values()].map((t) => ({ ...t, metadata: { ...t.metadata } })); }
945
+
946
+ export function autoFreezeIdleEconomyAccountsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const a of _econAccts.values()) if (a.status === ECONOMY_ACCOUNT_MATURITY_V2.ACTIVE && (t - a.lastTouchedAt) >= _econAcctIdleMs) { a.status = ECONOMY_ACCOUNT_MATURITY_V2.FROZEN; a.updatedAt = t; flipped.push(a.id); } return { flipped, count: flipped.length }; }
947
+ export function autoFailStuckEconomyTxsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const tx of _econTxs.values()) if (tx.status === ECONOMY_TX_LIFECYCLE_V2.PROCESSING && tx.startedAt != null && (t - tx.startedAt) >= _econTxStuckMs) { tx.status = ECONOMY_TX_LIFECYCLE_V2.FAILED; tx.updatedAt = t; if (!tx.settledAt) tx.settledAt = t; tx.metadata.failReason = "auto-fail-stuck"; flipped.push(tx.id); } return { flipped, count: flipped.length }; }
948
+
949
+ export function getAgentEconomyGovStatsV2() {
950
+ const accountsByStatus = {}; for (const s of Object.values(ECONOMY_ACCOUNT_MATURITY_V2)) accountsByStatus[s] = 0; for (const a of _econAccts.values()) accountsByStatus[a.status]++;
951
+ const txsByStatus = {}; for (const s of Object.values(ECONOMY_TX_LIFECYCLE_V2)) txsByStatus[s] = 0; for (const t of _econTxs.values()) txsByStatus[t.status]++;
952
+ return { totalAccountsV2: _econAccts.size, totalTxsV2: _econTxs.size, maxActiveEconomyAccountsPerHolder: _econMaxActivePerHolder, maxPendingEconomyTxsPerAccount: _econMaxPendingPerAcct, economyAccountIdleMs: _econAcctIdleMs, economyTxStuckMs: _econTxStuckMs, accountsByStatus, txsByStatus };
953
+ }