chainlesschain 0.132.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 (135) hide show
  1. package/package.json +1 -1
  2. package/src/commands/a2a.js +230 -0
  3. package/src/commands/activitypub.js +191 -0
  4. package/src/commands/agent.js +601 -0
  5. package/src/commands/audit.js +206 -0
  6. package/src/commands/bi.js +186 -0
  7. package/src/commands/bm25.js +162 -0
  8. package/src/commands/browse.js +225 -0
  9. package/src/commands/ccron.js +178 -0
  10. package/src/commands/chat.js +207 -0
  11. package/src/commands/compliance.js +420 -0
  12. package/src/commands/compt.js +176 -0
  13. package/src/commands/consol.js +237 -0
  14. package/src/commands/cowork.js +588 -0
  15. package/src/commands/crosschain.js +216 -0
  16. package/src/commands/dao.js +216 -0
  17. package/src/commands/dlp.js +206 -0
  18. package/src/commands/economy.js +211 -0
  19. package/src/commands/evolution.js +209 -0
  20. package/src/commands/evomap.js +216 -0
  21. package/src/commands/fflag.js +230 -0
  22. package/src/commands/git.js +185 -0
  23. package/src/commands/hardening.js +209 -0
  24. package/src/commands/hmemory.js +210 -0
  25. package/src/commands/incentive.js +209 -0
  26. package/src/commands/inference.js +178 -0
  27. package/src/commands/itbudget.js +161 -0
  28. package/src/commands/kg.js +206 -0
  29. package/src/commands/lowcode.js +201 -0
  30. package/src/commands/marketplace.js +206 -0
  31. package/src/commands/matrix.js +214 -0
  32. package/src/commands/mcpscaf.js +153 -0
  33. package/src/commands/meminj.js +153 -0
  34. package/src/commands/nostr.js +213 -0
  35. package/src/commands/orchestrate.js +217 -0
  36. package/src/commands/orchgov.js +156 -0
  37. package/src/commands/pdfp.js +160 -0
  38. package/src/commands/perf.js +176 -0
  39. package/src/commands/perm.js +156 -0
  40. package/src/commands/pipeline.js +211 -0
  41. package/src/commands/planmode.js +154 -0
  42. package/src/commands/privacy.js +203 -0
  43. package/src/commands/promcomp.js +166 -0
  44. package/src/commands/recommend.js +185 -0
  45. package/src/commands/reputation.js +208 -0
  46. package/src/commands/sandbox.js +206 -0
  47. package/src/commands/seshhook.js +153 -0
  48. package/src/commands/seshsearch.js +149 -0
  49. package/src/commands/seshtail.js +152 -0
  50. package/src/commands/seshu.js +160 -0
  51. package/src/commands/sganal.js +172 -0
  52. package/src/commands/siem.js +207 -0
  53. package/src/commands/sla.js +212 -0
  54. package/src/commands/slotfill.js +154 -0
  55. package/src/commands/social.js +159 -0
  56. package/src/commands/stress.js +206 -0
  57. package/src/commands/svccont.js +157 -0
  58. package/src/commands/terraform.js +206 -0
  59. package/src/commands/tms.js +183 -0
  60. package/src/commands/topiccls.js +158 -0
  61. package/src/commands/uprof.js +154 -0
  62. package/src/commands/vcheck.js +172 -0
  63. package/src/commands/webfetch.js +150 -0
  64. package/src/commands/zkp.js +218 -0
  65. package/src/harness/prompt-compressor.js +331 -0
  66. package/src/index.js +101 -1
  67. package/src/lib/a2a-protocol.js +373 -0
  68. package/src/lib/activitypub-bridge.js +343 -0
  69. package/src/lib/agent-economy.js +358 -0
  70. package/src/lib/app-builder.js +338 -0
  71. package/src/lib/audit-logger.js +321 -0
  72. package/src/lib/autonomous-agent.js +341 -0
  73. package/src/lib/bi-engine.js +339 -0
  74. package/src/lib/bm25-search.js +333 -0
  75. package/src/lib/browser-automation.js +352 -0
  76. package/src/lib/chat-core.js +336 -0
  77. package/src/lib/claude-code-bridge.js +341 -0
  78. package/src/lib/compliance-framework-reporter.js +359 -0
  79. package/src/lib/compliance-manager.js +330 -0
  80. package/src/lib/compression-telemetry.js +333 -0
  81. package/src/lib/content-recommender.js +370 -0
  82. package/src/lib/cowork-cron.js +330 -0
  83. package/src/lib/cowork-learning.js +333 -0
  84. package/src/lib/cowork-task-runner.js +362 -0
  85. package/src/lib/cowork-workflow.js +327 -0
  86. package/src/lib/cross-chain.js +365 -0
  87. package/src/lib/dao-governance.js +339 -0
  88. package/src/lib/dlp-engine.js +343 -0
  89. package/src/lib/evolution-system.js +336 -0
  90. package/src/lib/evomap-manager.js +339 -0
  91. package/src/lib/execution-backend.js +351 -0
  92. package/src/lib/feature-flags.js +330 -0
  93. package/src/lib/git-integration.js +343 -0
  94. package/src/lib/hardening-manager.js +341 -0
  95. package/src/lib/hierarchical-memory.js +341 -0
  96. package/src/lib/inference-network.js +362 -0
  97. package/src/lib/iteration-budget.js +357 -0
  98. package/src/lib/knowledge-graph.js +333 -0
  99. package/src/lib/matrix-bridge.js +339 -0
  100. package/src/lib/mcp-scaffold.js +345 -0
  101. package/src/lib/memory-injection.js +320 -0
  102. package/src/lib/nostr-bridge.js +342 -0
  103. package/src/lib/orchestrator.js +350 -0
  104. package/src/lib/pdf-parser.js +330 -0
  105. package/src/lib/perf-tuning.js +364 -0
  106. package/src/lib/permission-engine.js +319 -0
  107. package/src/lib/pipeline-orchestrator.js +345 -0
  108. package/src/lib/plan-mode.js +328 -0
  109. package/src/lib/privacy-computing.js +335 -0
  110. package/src/lib/prompt-compressor.js +1 -10
  111. package/src/lib/reputation-optimizer.js +340 -0
  112. package/src/lib/sandbox-v2.js +327 -0
  113. package/src/lib/service-container.js +342 -0
  114. package/src/lib/session-consolidator.js +352 -0
  115. package/src/lib/session-hooks.js +340 -0
  116. package/src/lib/session-search.js +334 -0
  117. package/src/lib/session-tail.js +320 -0
  118. package/src/lib/session-usage.js +329 -0
  119. package/src/lib/siem-exporter.js +352 -0
  120. package/src/lib/skill-marketplace.js +345 -0
  121. package/src/lib/sla-manager.js +341 -0
  122. package/src/lib/slot-filler.js +333 -0
  123. package/src/lib/social-graph-analytics.js +327 -0
  124. package/src/lib/social-graph.js +304 -0
  125. package/src/lib/stress-tester.js +342 -0
  126. package/src/lib/sub-agent-registry.js +359 -0
  127. package/src/lib/task-model-selector.js +333 -0
  128. package/src/lib/terraform-manager.js +333 -0
  129. package/src/lib/todo-manager.js +339 -0
  130. package/src/lib/token-incentive.js +341 -0
  131. package/src/lib/topic-classifier.js +353 -0
  132. package/src/lib/user-profile.js +325 -0
  133. package/src/lib/version-checker.js +335 -0
  134. package/src/lib/web-fetch.js +322 -0
  135. package/src/lib/zkp-engine.js +342 -0
package/src/index.js CHANGED
@@ -13,7 +13,37 @@ import { registerNoteCommand } from "./commands/note.js";
13
13
  import { registerChatCommand } from "./commands/chat.js";
14
14
  import { registerAskCommand } from "./commands/ask.js";
15
15
  import { registerLlmCommand } from "./commands/llm.js";
16
- import { registerAgentCommand } from "./commands/agent.js";
16
+ import {
17
+ registerAgentCommand,
18
+ registerSubAgentV2Command,
19
+ registerExecBackendV2Command,
20
+ registerTodoV2Command,
21
+ registerAutoAgentV2Command,
22
+ } from "./commands/agent.js";
23
+ import { registerOrchGovCommand } from "./commands/orchgov.js";
24
+ import { registerTopicClsCommand } from "./commands/topiccls.js";
25
+ import { registerItBudgetCommand } from "./commands/itbudget.js";
26
+ import { registerPlanModeCommand } from "./commands/planmode.js";
27
+ import { registerPermCommand } from "./commands/perm.js";
28
+ import { registerUprofCommand } from "./commands/uprof.js";
29
+ import { registerSvcContCommand } from "./commands/svccont.js";
30
+ import { registerTmsCommand } from "./commands/tms.js";
31
+ import { registerSlotfillCommand } from "./commands/slotfill.js";
32
+ import { registerWebfetchCommand } from "./commands/webfetch.js";
33
+ import { registerMeminjCommand } from "./commands/meminj.js";
34
+ import { registerSeshsearchCommand } from "./commands/seshsearch.js";
35
+ import { registerSeshtailCommand } from "./commands/seshtail.js";
36
+ import { registerSeshuCommand } from "./commands/seshu.js";
37
+ import { registerSeshhookCommand } from "./commands/seshhook.js";
38
+ import { registerMcpscafCommand } from "./commands/mcpscaf.js";
39
+ import { registerFflagCommand } from "./commands/fflag.js";
40
+ import { registerPromcompCommand } from "./commands/promcomp.js";
41
+ import { registerCcronCommand } from "./commands/ccron.js";
42
+ import { registerVcheckCommand } from "./commands/vcheck.js";
43
+ import { registerPdfpCommand } from "./commands/pdfp.js";
44
+ import { registerBm25Command } from "./commands/bm25.js";
45
+ import { registerComptCommand } from "./commands/compt.js";
46
+ import { registerSganalCommand } from "./commands/sganal.js";
17
47
  import { registerStreamCommand } from "./commands/stream.js";
18
48
  import { registerSkillCommand } from "./commands/skill.js";
19
49
  import { registerSearchCommand } from "./commands/search.js";
@@ -22,6 +52,7 @@ import { registerMemoryCommand } from "./commands/memory.js";
22
52
  import { registerPermMemCommand } from "./commands/permmem.js";
23
53
  import { registerRCacheCommand } from "./commands/rcache.js";
24
54
  import { registerSessionCommand } from "./commands/session.js";
55
+ import { registerConsolCommand } from "./commands/consol.js";
25
56
  import { registerImportCommand } from "./commands/import.js";
26
57
  import { registerExportCommand } from "./commands/export.js";
27
58
  import { registerGitCommand } from "./commands/git.js";
@@ -178,6 +209,26 @@ import { registerAutomationCommand } from "./commands/automation.js";
178
209
  // Phase 14: SSO Enterprise Authentication (SAML / OAuth2 / OIDC + session lifecycle + DID bridge)
179
210
  import { registerSsoCommand } from "./commands/sso.js";
180
211
 
212
+ // Iter16 V2 governance overlays (appended to existing parent commands)
213
+ import { registerAuditGovV2Commands } from "./commands/audit.js";
214
+ import { registerKgovV2Commands } from "./commands/kg.js";
215
+ import { registerSboxGovV2Commands } from "./commands/sandbox.js";
216
+ import { registerSlagovV2Commands } from "./commands/sla.js";
217
+ import { registerStrgovV2Commands } from "./commands/stress.js";
218
+ import { registerTfgovV2Commands } from "./commands/terraform.js";
219
+ import { registerRepgovV2Commands } from "./commands/reputation.js";
220
+ import { registerMktgovV2Commands } from "./commands/marketplace.js";
221
+
222
+ // Iter17 V2 governance overlays
223
+ import { registerChatgovV2Commands } from "./commands/chat.js";
224
+ import { registerCcbgovV2Commands } from "./commands/orchestrate.js";
225
+ import { registerCmgrV2Commands } from "./commands/compliance.js";
226
+ import { registerCwLearnV2Commands } from "./commands/cowork.js";
227
+ import { registerCwwfV2Commands } from "./commands/cowork.js";
228
+ import { registerPcgovV2Commands } from "./commands/privacy.js";
229
+ import { registerIncgovV2Commands } from "./commands/incentive.js";
230
+ import { registerHardgovV2Commands } from "./commands/hardening.js";
231
+
181
232
  export function createProgram() {
182
233
  const program = new Command();
183
234
 
@@ -211,6 +262,34 @@ export function createProgram() {
211
262
  registerAskCommand(program);
212
263
  registerLlmCommand(program);
213
264
  registerAgentCommand(program);
265
+ registerSubAgentV2Command(program);
266
+ registerExecBackendV2Command(program);
267
+ registerTodoV2Command(program);
268
+ registerAutoAgentV2Command(program);
269
+ registerOrchGovCommand(program);
270
+ registerTopicClsCommand(program);
271
+ registerItBudgetCommand(program);
272
+ registerPlanModeCommand(program);
273
+ registerPermCommand(program);
274
+ registerUprofCommand(program);
275
+ registerSvcContCommand(program);
276
+ registerTmsCommand(program);
277
+ registerSlotfillCommand(program);
278
+ registerWebfetchCommand(program);
279
+ registerMeminjCommand(program);
280
+ registerSeshsearchCommand(program);
281
+ registerSeshtailCommand(program);
282
+ registerSeshuCommand(program);
283
+ registerSeshhookCommand(program);
284
+ registerMcpscafCommand(program);
285
+ registerFflagCommand(program);
286
+ registerPromcompCommand(program);
287
+ registerCcronCommand(program);
288
+ registerVcheckCommand(program);
289
+ registerPdfpCommand(program);
290
+ registerBm25Command(program);
291
+ registerComptCommand(program);
292
+ registerSganalCommand(program);
214
293
  registerStreamCommand(program);
215
294
  registerSkillCommand(program);
216
295
 
@@ -221,6 +300,7 @@ export function createProgram() {
221
300
  registerPermMemCommand(program);
222
301
  registerRCacheCommand(program);
223
302
  registerSessionCommand(program);
303
+ registerConsolCommand(program);
224
304
 
225
305
  // Phase 2: Knowledge & content management
226
306
  registerImportCommand(program);
@@ -368,5 +448,25 @@ export function createProgram() {
368
448
  registerAutomationCommand(program);
369
449
  registerSsoCommand(program);
370
450
 
451
+ // Iter16 V2 governance overlays (must run after parent commands above)
452
+ registerAuditGovV2Commands(program);
453
+ registerKgovV2Commands(program);
454
+ registerSboxGovV2Commands(program);
455
+ registerSlagovV2Commands(program);
456
+ registerStrgovV2Commands(program);
457
+ registerTfgovV2Commands(program);
458
+ registerRepgovV2Commands(program);
459
+ registerMktgovV2Commands(program);
460
+
461
+ // Iter17 V2 governance overlays
462
+ registerChatgovV2Commands(program);
463
+ registerCcbgovV2Commands(program);
464
+ registerCmgrV2Commands(program);
465
+ registerCwLearnV2Commands(program);
466
+ registerCwwfV2Commands(program);
467
+ registerPcgovV2Commands(program);
468
+ registerIncgovV2Commands(program);
469
+ registerHardgovV2Commands(program);
470
+
371
471
  return program;
372
472
  }
@@ -820,3 +820,376 @@ export function _resetV2State() {
820
820
  }
821
821
 
822
822
  export { _v2Tasks, _v2Cards, _v2TypedSubs };
823
+
824
+ // ===== V2 Surface: A2A Protocol governance overlay (CLI v0.136.0) =====
825
+ export const A2A_AGENT_MATURITY_V2 = Object.freeze({
826
+ PENDING: "pending",
827
+ ACTIVE: "active",
828
+ SUSPENDED: "suspended",
829
+ RETIRED: "retired",
830
+ });
831
+ export const A2A_MESSAGE_LIFECYCLE_V2 = Object.freeze({
832
+ QUEUED: "queued",
833
+ SENDING: "sending",
834
+ DELIVERED: "delivered",
835
+ FAILED: "failed",
836
+ CANCELLED: "cancelled",
837
+ });
838
+
839
+ const _a2aAgentTrans = new Map([
840
+ [
841
+ A2A_AGENT_MATURITY_V2.PENDING,
842
+ new Set([A2A_AGENT_MATURITY_V2.ACTIVE, A2A_AGENT_MATURITY_V2.RETIRED]),
843
+ ],
844
+ [
845
+ A2A_AGENT_MATURITY_V2.ACTIVE,
846
+ new Set([A2A_AGENT_MATURITY_V2.SUSPENDED, A2A_AGENT_MATURITY_V2.RETIRED]),
847
+ ],
848
+ [
849
+ A2A_AGENT_MATURITY_V2.SUSPENDED,
850
+ new Set([A2A_AGENT_MATURITY_V2.ACTIVE, A2A_AGENT_MATURITY_V2.RETIRED]),
851
+ ],
852
+ [A2A_AGENT_MATURITY_V2.RETIRED, new Set()],
853
+ ]);
854
+ const _a2aAgentTerminal = new Set([A2A_AGENT_MATURITY_V2.RETIRED]);
855
+ const _a2aMsgTrans = new Map([
856
+ [
857
+ A2A_MESSAGE_LIFECYCLE_V2.QUEUED,
858
+ new Set([
859
+ A2A_MESSAGE_LIFECYCLE_V2.SENDING,
860
+ A2A_MESSAGE_LIFECYCLE_V2.CANCELLED,
861
+ ]),
862
+ ],
863
+ [
864
+ A2A_MESSAGE_LIFECYCLE_V2.SENDING,
865
+ new Set([
866
+ A2A_MESSAGE_LIFECYCLE_V2.DELIVERED,
867
+ A2A_MESSAGE_LIFECYCLE_V2.FAILED,
868
+ A2A_MESSAGE_LIFECYCLE_V2.CANCELLED,
869
+ ]),
870
+ ],
871
+ [A2A_MESSAGE_LIFECYCLE_V2.DELIVERED, new Set()],
872
+ [A2A_MESSAGE_LIFECYCLE_V2.FAILED, new Set()],
873
+ [A2A_MESSAGE_LIFECYCLE_V2.CANCELLED, new Set()],
874
+ ]);
875
+
876
+ const _a2aAgents = new Map();
877
+ const _a2aMsgs = new Map();
878
+ let _a2aMaxActivePerOwner = 12;
879
+ let _a2aMaxPendingPerAgent = 20;
880
+ let _a2aAgentIdleMs = 6 * 60 * 60 * 1000;
881
+ let _a2aMsgStuckMs = 3 * 60 * 1000;
882
+
883
+ function _a2aPos(n, lbl) {
884
+ const v = Math.floor(Number(n));
885
+ if (!Number.isFinite(v) || v <= 0)
886
+ throw new Error(`${lbl} must be positive integer`);
887
+ return v;
888
+ }
889
+
890
+ export function setMaxActiveA2aAgentsPerOwnerV2(n) {
891
+ _a2aMaxActivePerOwner = _a2aPos(n, "maxActiveA2aAgentsPerOwner");
892
+ }
893
+ export function getMaxActiveA2aAgentsPerOwnerV2() {
894
+ return _a2aMaxActivePerOwner;
895
+ }
896
+ export function setMaxPendingA2aMessagesPerAgentV2(n) {
897
+ _a2aMaxPendingPerAgent = _a2aPos(n, "maxPendingA2aMessagesPerAgent");
898
+ }
899
+ export function getMaxPendingA2aMessagesPerAgentV2() {
900
+ return _a2aMaxPendingPerAgent;
901
+ }
902
+ export function setA2aAgentIdleMsV2(n) {
903
+ _a2aAgentIdleMs = _a2aPos(n, "a2aAgentIdleMs");
904
+ }
905
+ export function getA2aAgentIdleMsV2() {
906
+ return _a2aAgentIdleMs;
907
+ }
908
+ export function setA2aMessageStuckMsV2(n) {
909
+ _a2aMsgStuckMs = _a2aPos(n, "a2aMessageStuckMs");
910
+ }
911
+ export function getA2aMessageStuckMsV2() {
912
+ return _a2aMsgStuckMs;
913
+ }
914
+
915
+ export function _resetStateA2aProtocolV2() {
916
+ _a2aAgents.clear();
917
+ _a2aMsgs.clear();
918
+ _a2aMaxActivePerOwner = 12;
919
+ _a2aMaxPendingPerAgent = 20;
920
+ _a2aAgentIdleMs = 6 * 60 * 60 * 1000;
921
+ _a2aMsgStuckMs = 3 * 60 * 1000;
922
+ }
923
+
924
+ export function registerA2aAgentV2({ id, owner, capabilities, metadata } = {}) {
925
+ if (!id || typeof id !== "string") throw new Error("id is required");
926
+ if (!owner || typeof owner !== "string") throw new Error("owner is required");
927
+ if (_a2aAgents.has(id)) throw new Error(`a2a agent ${id} already registered`);
928
+ const now = Date.now();
929
+ const a = {
930
+ id,
931
+ owner,
932
+ capabilities: Array.isArray(capabilities) ? [...capabilities] : [],
933
+ status: A2A_AGENT_MATURITY_V2.PENDING,
934
+ createdAt: now,
935
+ updatedAt: now,
936
+ activatedAt: null,
937
+ retiredAt: null,
938
+ lastTouchedAt: now,
939
+ metadata: { ...(metadata || {}) },
940
+ };
941
+ _a2aAgents.set(id, a);
942
+ return {
943
+ ...a,
944
+ capabilities: [...a.capabilities],
945
+ metadata: { ...a.metadata },
946
+ };
947
+ }
948
+ function _a2aCheckA(from, to) {
949
+ const al = _a2aAgentTrans.get(from);
950
+ if (!al || !al.has(to))
951
+ throw new Error(`invalid a2a agent transition ${from} → ${to}`);
952
+ }
953
+ function _a2aCountActive(owner) {
954
+ let n = 0;
955
+ for (const a of _a2aAgents.values())
956
+ if (a.owner === owner && a.status === A2A_AGENT_MATURITY_V2.ACTIVE) n++;
957
+ return n;
958
+ }
959
+
960
+ export function activateA2aAgentV2(id) {
961
+ const a = _a2aAgents.get(id);
962
+ if (!a) throw new Error(`a2a agent ${id} not found`);
963
+ _a2aCheckA(a.status, A2A_AGENT_MATURITY_V2.ACTIVE);
964
+ const recovery = a.status === A2A_AGENT_MATURITY_V2.SUSPENDED;
965
+ if (!recovery) {
966
+ const c = _a2aCountActive(a.owner);
967
+ if (c >= _a2aMaxActivePerOwner)
968
+ throw new Error(
969
+ `max active a2a agents per owner (${_a2aMaxActivePerOwner}) reached for ${a.owner}`,
970
+ );
971
+ }
972
+ const now = Date.now();
973
+ a.status = A2A_AGENT_MATURITY_V2.ACTIVE;
974
+ a.updatedAt = now;
975
+ a.lastTouchedAt = now;
976
+ if (!a.activatedAt) a.activatedAt = now;
977
+ return {
978
+ ...a,
979
+ capabilities: [...a.capabilities],
980
+ metadata: { ...a.metadata },
981
+ };
982
+ }
983
+ export function suspendA2aAgentV2(id) {
984
+ const a = _a2aAgents.get(id);
985
+ if (!a) throw new Error(`a2a agent ${id} not found`);
986
+ _a2aCheckA(a.status, A2A_AGENT_MATURITY_V2.SUSPENDED);
987
+ a.status = A2A_AGENT_MATURITY_V2.SUSPENDED;
988
+ a.updatedAt = Date.now();
989
+ return {
990
+ ...a,
991
+ capabilities: [...a.capabilities],
992
+ metadata: { ...a.metadata },
993
+ };
994
+ }
995
+ export function retireA2aAgentV2(id) {
996
+ const a = _a2aAgents.get(id);
997
+ if (!a) throw new Error(`a2a agent ${id} not found`);
998
+ _a2aCheckA(a.status, A2A_AGENT_MATURITY_V2.RETIRED);
999
+ const now = Date.now();
1000
+ a.status = A2A_AGENT_MATURITY_V2.RETIRED;
1001
+ a.updatedAt = now;
1002
+ if (!a.retiredAt) a.retiredAt = now;
1003
+ return {
1004
+ ...a,
1005
+ capabilities: [...a.capabilities],
1006
+ metadata: { ...a.metadata },
1007
+ };
1008
+ }
1009
+ export function touchA2aAgentV2(id) {
1010
+ const a = _a2aAgents.get(id);
1011
+ if (!a) throw new Error(`a2a agent ${id} not found`);
1012
+ if (_a2aAgentTerminal.has(a.status))
1013
+ throw new Error(`cannot touch terminal a2a agent ${id}`);
1014
+ const now = Date.now();
1015
+ a.lastTouchedAt = now;
1016
+ a.updatedAt = now;
1017
+ return {
1018
+ ...a,
1019
+ capabilities: [...a.capabilities],
1020
+ metadata: { ...a.metadata },
1021
+ };
1022
+ }
1023
+ export function getA2aAgentV2(id) {
1024
+ const a = _a2aAgents.get(id);
1025
+ if (!a) return null;
1026
+ return {
1027
+ ...a,
1028
+ capabilities: [...a.capabilities],
1029
+ metadata: { ...a.metadata },
1030
+ };
1031
+ }
1032
+ export function listA2aAgentsV2() {
1033
+ return [..._a2aAgents.values()].map((a) => ({
1034
+ ...a,
1035
+ capabilities: [...a.capabilities],
1036
+ metadata: { ...a.metadata },
1037
+ }));
1038
+ }
1039
+
1040
+ function _a2aCountPending(aid) {
1041
+ let n = 0;
1042
+ for (const m of _a2aMsgs.values())
1043
+ if (
1044
+ m.agentId === aid &&
1045
+ (m.status === A2A_MESSAGE_LIFECYCLE_V2.QUEUED ||
1046
+ m.status === A2A_MESSAGE_LIFECYCLE_V2.SENDING)
1047
+ )
1048
+ n++;
1049
+ return n;
1050
+ }
1051
+
1052
+ export function createA2aMessageV2({
1053
+ id,
1054
+ agentId,
1055
+ peerId,
1056
+ payload,
1057
+ metadata,
1058
+ } = {}) {
1059
+ if (!id || typeof id !== "string") throw new Error("id is required");
1060
+ if (!agentId || typeof agentId !== "string")
1061
+ throw new Error("agentId is required");
1062
+ if (_a2aMsgs.has(id)) throw new Error(`a2a message ${id} already exists`);
1063
+ if (!_a2aAgents.has(agentId))
1064
+ throw new Error(`a2a agent ${agentId} not found`);
1065
+ const pending = _a2aCountPending(agentId);
1066
+ if (pending >= _a2aMaxPendingPerAgent)
1067
+ throw new Error(
1068
+ `max pending a2a messages per agent (${_a2aMaxPendingPerAgent}) reached for ${agentId}`,
1069
+ );
1070
+ const now = Date.now();
1071
+ const m = {
1072
+ id,
1073
+ agentId,
1074
+ peerId: peerId || "",
1075
+ payload: payload || "",
1076
+ status: A2A_MESSAGE_LIFECYCLE_V2.QUEUED,
1077
+ createdAt: now,
1078
+ updatedAt: now,
1079
+ startedAt: null,
1080
+ settledAt: null,
1081
+ metadata: { ...(metadata || {}) },
1082
+ };
1083
+ _a2aMsgs.set(id, m);
1084
+ return { ...m, metadata: { ...m.metadata } };
1085
+ }
1086
+ function _a2aCheckM(from, to) {
1087
+ const al = _a2aMsgTrans.get(from);
1088
+ if (!al || !al.has(to))
1089
+ throw new Error(`invalid a2a message transition ${from} → ${to}`);
1090
+ }
1091
+ export function startA2aMessageV2(id) {
1092
+ const m = _a2aMsgs.get(id);
1093
+ if (!m) throw new Error(`a2a message ${id} not found`);
1094
+ _a2aCheckM(m.status, A2A_MESSAGE_LIFECYCLE_V2.SENDING);
1095
+ const now = Date.now();
1096
+ m.status = A2A_MESSAGE_LIFECYCLE_V2.SENDING;
1097
+ m.updatedAt = now;
1098
+ if (!m.startedAt) m.startedAt = now;
1099
+ return { ...m, metadata: { ...m.metadata } };
1100
+ }
1101
+ export function deliverA2aMessageV2(id) {
1102
+ const m = _a2aMsgs.get(id);
1103
+ if (!m) throw new Error(`a2a message ${id} not found`);
1104
+ _a2aCheckM(m.status, A2A_MESSAGE_LIFECYCLE_V2.DELIVERED);
1105
+ const now = Date.now();
1106
+ m.status = A2A_MESSAGE_LIFECYCLE_V2.DELIVERED;
1107
+ m.updatedAt = now;
1108
+ if (!m.settledAt) m.settledAt = now;
1109
+ return { ...m, metadata: { ...m.metadata } };
1110
+ }
1111
+ export function failA2aMessageV2(id, reason) {
1112
+ const m = _a2aMsgs.get(id);
1113
+ if (!m) throw new Error(`a2a message ${id} not found`);
1114
+ _a2aCheckM(m.status, A2A_MESSAGE_LIFECYCLE_V2.FAILED);
1115
+ const now = Date.now();
1116
+ m.status = A2A_MESSAGE_LIFECYCLE_V2.FAILED;
1117
+ m.updatedAt = now;
1118
+ if (!m.settledAt) m.settledAt = now;
1119
+ if (reason) m.metadata.failReason = String(reason);
1120
+ return { ...m, metadata: { ...m.metadata } };
1121
+ }
1122
+ export function cancelA2aMessageV2(id, reason) {
1123
+ const m = _a2aMsgs.get(id);
1124
+ if (!m) throw new Error(`a2a message ${id} not found`);
1125
+ _a2aCheckM(m.status, A2A_MESSAGE_LIFECYCLE_V2.CANCELLED);
1126
+ const now = Date.now();
1127
+ m.status = A2A_MESSAGE_LIFECYCLE_V2.CANCELLED;
1128
+ m.updatedAt = now;
1129
+ if (!m.settledAt) m.settledAt = now;
1130
+ if (reason) m.metadata.cancelReason = String(reason);
1131
+ return { ...m, metadata: { ...m.metadata } };
1132
+ }
1133
+ export function getA2aMessageV2(id) {
1134
+ const m = _a2aMsgs.get(id);
1135
+ if (!m) return null;
1136
+ return { ...m, metadata: { ...m.metadata } };
1137
+ }
1138
+ export function listA2aMessagesV2() {
1139
+ return [..._a2aMsgs.values()].map((m) => ({
1140
+ ...m,
1141
+ metadata: { ...m.metadata },
1142
+ }));
1143
+ }
1144
+
1145
+ export function autoSuspendIdleA2aAgentsV2({ now } = {}) {
1146
+ const t = now ?? Date.now();
1147
+ const flipped = [];
1148
+ for (const a of _a2aAgents.values())
1149
+ if (
1150
+ a.status === A2A_AGENT_MATURITY_V2.ACTIVE &&
1151
+ t - a.lastTouchedAt >= _a2aAgentIdleMs
1152
+ ) {
1153
+ a.status = A2A_AGENT_MATURITY_V2.SUSPENDED;
1154
+ a.updatedAt = t;
1155
+ flipped.push(a.id);
1156
+ }
1157
+ return { flipped, count: flipped.length };
1158
+ }
1159
+ export function autoFailStuckA2aMessagesV2({ now } = {}) {
1160
+ const t = now ?? Date.now();
1161
+ const flipped = [];
1162
+ for (const m of _a2aMsgs.values())
1163
+ if (
1164
+ m.status === A2A_MESSAGE_LIFECYCLE_V2.SENDING &&
1165
+ m.startedAt != null &&
1166
+ t - m.startedAt >= _a2aMsgStuckMs
1167
+ ) {
1168
+ m.status = A2A_MESSAGE_LIFECYCLE_V2.FAILED;
1169
+ m.updatedAt = t;
1170
+ if (!m.settledAt) m.settledAt = t;
1171
+ m.metadata.failReason = "auto-fail-stuck";
1172
+ flipped.push(m.id);
1173
+ }
1174
+ return { flipped, count: flipped.length };
1175
+ }
1176
+
1177
+ export function getA2aProtocolGovStatsV2() {
1178
+ const agentsByStatus = {};
1179
+ for (const s of Object.values(A2A_AGENT_MATURITY_V2)) agentsByStatus[s] = 0;
1180
+ for (const a of _a2aAgents.values()) agentsByStatus[a.status]++;
1181
+ const messagesByStatus = {};
1182
+ for (const s of Object.values(A2A_MESSAGE_LIFECYCLE_V2))
1183
+ messagesByStatus[s] = 0;
1184
+ for (const m of _a2aMsgs.values()) messagesByStatus[m.status]++;
1185
+ return {
1186
+ totalAgentsV2: _a2aAgents.size,
1187
+ totalMessagesV2: _a2aMsgs.size,
1188
+ maxActiveA2aAgentsPerOwner: _a2aMaxActivePerOwner,
1189
+ maxPendingA2aMessagesPerAgent: _a2aMaxPendingPerAgent,
1190
+ a2aAgentIdleMs: _a2aAgentIdleMs,
1191
+ a2aMessageStuckMs: _a2aMsgStuckMs,
1192
+ agentsByStatus,
1193
+ messagesByStatus,
1194
+ };
1195
+ }