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
@@ -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
+ }