chainlesschain 0.143.0 → 0.145.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (133) hide show
  1. package/package.json +1 -1
  2. package/src/commands/a2a.js +196 -28
  3. package/src/commands/activitypub.js +157 -27
  4. package/src/commands/agent.js +587 -103
  5. package/src/commands/audit.js +206 -0
  6. package/src/commands/bi.js +152 -27
  7. package/src/commands/bm25.js +111 -27
  8. package/src/commands/browse.js +190 -29
  9. package/src/commands/ccron.js +128 -28
  10. package/src/commands/chat.js +207 -0
  11. package/src/commands/compliance.js +414 -34
  12. package/src/commands/compt.js +127 -29
  13. package/src/commands/consol.js +8 -2
  14. package/src/commands/cowork.js +580 -34
  15. package/src/commands/crosschain.js +182 -28
  16. package/src/commands/dao.js +182 -28
  17. package/src/commands/dlp.js +172 -27
  18. package/src/commands/economy.js +205 -50
  19. package/src/commands/evolution.js +203 -50
  20. package/src/commands/evomap.js +182 -27
  21. package/src/commands/fflag.js +74 -22
  22. package/src/commands/git.js +177 -37
  23. package/src/commands/hardening.js +209 -0
  24. package/src/commands/hmemory.js +204 -50
  25. package/src/commands/incentive.js +209 -0
  26. package/src/commands/inference.js +170 -34
  27. package/src/commands/itbudget.js +149 -33
  28. package/src/commands/kg.js +206 -0
  29. package/src/commands/lowcode.js +195 -38
  30. package/src/commands/marketplace.js +206 -0
  31. package/src/commands/matrix.js +179 -27
  32. package/src/commands/mcpscaf.js +145 -33
  33. package/src/commands/meminj.js +145 -33
  34. package/src/commands/nostr.js +178 -27
  35. package/src/commands/orchestrate.js +217 -0
  36. package/src/commands/orchgov.js +144 -33
  37. package/src/commands/pdfp.js +109 -27
  38. package/src/commands/perf.js +169 -32
  39. package/src/commands/perm.js +144 -33
  40. package/src/commands/pipeline.js +207 -52
  41. package/src/commands/planmode.js +141 -32
  42. package/src/commands/privacy.js +203 -0
  43. package/src/commands/promcomp.js +111 -27
  44. package/src/commands/recommend.js +177 -34
  45. package/src/commands/reputation.js +208 -0
  46. package/src/commands/sandbox.js +206 -0
  47. package/src/commands/seshhook.js +145 -33
  48. package/src/commands/seshsearch.js +141 -33
  49. package/src/commands/seshtail.js +144 -33
  50. package/src/commands/seshu.js +152 -33
  51. package/src/commands/sganal.js +123 -29
  52. package/src/commands/siem.js +201 -34
  53. package/src/commands/sla.js +212 -0
  54. package/src/commands/slotfill.js +146 -33
  55. package/src/commands/social.js +157 -32
  56. package/src/commands/stress.js +206 -0
  57. package/src/commands/svccont.js +145 -33
  58. package/src/commands/terraform.js +206 -0
  59. package/src/commands/tms.js +171 -33
  60. package/src/commands/topiccls.js +146 -33
  61. package/src/commands/uprof.js +141 -32
  62. package/src/commands/vcheck.js +122 -28
  63. package/src/commands/webfetch.js +141 -32
  64. package/src/commands/zkp.js +184 -28
  65. package/src/index.js +40 -0
  66. package/src/lib/a2a-protocol.js +319 -51
  67. package/src/lib/activitypub-bridge.js +288 -50
  68. package/src/lib/agent-economy.js +304 -51
  69. package/src/lib/app-builder.js +279 -46
  70. package/src/lib/audit-logger.js +321 -0
  71. package/src/lib/autonomous-agent.js +284 -48
  72. package/src/lib/bi-engine.js +283 -49
  73. package/src/lib/bm25-search.js +301 -49
  74. package/src/lib/browser-automation.js +296 -49
  75. package/src/lib/chat-core.js +336 -0
  76. package/src/lib/claude-code-bridge.js +341 -0
  77. package/src/lib/compliance-framework-reporter.js +305 -51
  78. package/src/lib/compliance-manager.js +330 -0
  79. package/src/lib/compression-telemetry.js +301 -49
  80. package/src/lib/content-recommender.js +317 -52
  81. package/src/lib/cowork-cron.js +298 -49
  82. package/src/lib/cowork-learning.js +333 -0
  83. package/src/lib/cowork-task-runner.js +308 -51
  84. package/src/lib/cowork-workflow.js +327 -0
  85. package/src/lib/cross-chain.js +311 -51
  86. package/src/lib/dao-governance.js +280 -46
  87. package/src/lib/dlp-engine.js +287 -49
  88. package/src/lib/evolution-system.js +278 -47
  89. package/src/lib/evomap-manager.js +280 -46
  90. package/src/lib/execution-backend.js +294 -48
  91. package/src/lib/feature-flags.js +294 -49
  92. package/src/lib/git-integration.js +285 -47
  93. package/src/lib/hardening-manager.js +341 -0
  94. package/src/lib/hierarchical-memory.js +284 -48
  95. package/src/lib/inference-network.js +308 -51
  96. package/src/lib/iteration-budget.js +302 -50
  97. package/src/lib/knowledge-graph.js +333 -0
  98. package/src/lib/matrix-bridge.js +281 -47
  99. package/src/lib/mcp-scaffold.js +318 -54
  100. package/src/lib/memory-injection.js +288 -49
  101. package/src/lib/nostr-bridge.js +286 -49
  102. package/src/lib/orchestrator.js +293 -48
  103. package/src/lib/pdf-parser.js +298 -49
  104. package/src/lib/perf-tuning.js +309 -50
  105. package/src/lib/permission-engine.js +287 -49
  106. package/src/lib/pipeline-orchestrator.js +289 -49
  107. package/src/lib/plan-mode.js +298 -51
  108. package/src/lib/privacy-computing.js +335 -0
  109. package/src/lib/reputation-optimizer.js +340 -0
  110. package/src/lib/sandbox-v2.js +327 -0
  111. package/src/lib/service-container.js +313 -52
  112. package/src/lib/session-consolidator.js +296 -49
  113. package/src/lib/session-hooks.js +312 -53
  114. package/src/lib/session-search.js +304 -51
  115. package/src/lib/session-tail.js +288 -49
  116. package/src/lib/session-usage.js +298 -52
  117. package/src/lib/siem-exporter.js +298 -51
  118. package/src/lib/skill-marketplace.js +345 -0
  119. package/src/lib/sla-manager.js +341 -0
  120. package/src/lib/slot-filler.js +303 -51
  121. package/src/lib/social-graph-analytics.js +295 -49
  122. package/src/lib/social-graph.js +272 -49
  123. package/src/lib/stress-tester.js +342 -0
  124. package/src/lib/sub-agent-registry.js +302 -53
  125. package/src/lib/task-model-selector.js +302 -50
  126. package/src/lib/terraform-manager.js +333 -0
  127. package/src/lib/todo-manager.js +281 -47
  128. package/src/lib/token-incentive.js +341 -0
  129. package/src/lib/topic-classifier.js +297 -49
  130. package/src/lib/user-profile.js +294 -50
  131. package/src/lib/version-checker.js +304 -50
  132. package/src/lib/web-fetch.js +292 -51
  133. package/src/lib/zkp-engine.js +286 -49
@@ -219,25 +219,62 @@ export class SubAgentRegistry {
219
219
  }
220
220
  }
221
221
 
222
-
223
222
  // ===== V2 Surface: Sub-Agent Registry governance overlay (CLI v0.133.0) =====
224
223
  export const SUBAGENT_PROFILE_MATURITY_V2 = Object.freeze({
225
- PENDING: "pending", ACTIVE: "active", PAUSED: "paused", RETIRED: "retired",
224
+ PENDING: "pending",
225
+ ACTIVE: "active",
226
+ PAUSED: "paused",
227
+ RETIRED: "retired",
226
228
  });
227
229
  export const SUBAGENT_TASK_LIFECYCLE_V2 = Object.freeze({
228
- QUEUED: "queued", RUNNING: "running", COMPLETED: "completed", FAILED: "failed", CANCELLED: "cancelled",
230
+ QUEUED: "queued",
231
+ RUNNING: "running",
232
+ COMPLETED: "completed",
233
+ FAILED: "failed",
234
+ CANCELLED: "cancelled",
229
235
  });
230
236
 
231
237
  const _saProfileTrans = new Map([
232
- [SUBAGENT_PROFILE_MATURITY_V2.PENDING, new Set([SUBAGENT_PROFILE_MATURITY_V2.ACTIVE, SUBAGENT_PROFILE_MATURITY_V2.RETIRED])],
233
- [SUBAGENT_PROFILE_MATURITY_V2.ACTIVE, new Set([SUBAGENT_PROFILE_MATURITY_V2.PAUSED, SUBAGENT_PROFILE_MATURITY_V2.RETIRED])],
234
- [SUBAGENT_PROFILE_MATURITY_V2.PAUSED, new Set([SUBAGENT_PROFILE_MATURITY_V2.ACTIVE, SUBAGENT_PROFILE_MATURITY_V2.RETIRED])],
238
+ [
239
+ SUBAGENT_PROFILE_MATURITY_V2.PENDING,
240
+ new Set([
241
+ SUBAGENT_PROFILE_MATURITY_V2.ACTIVE,
242
+ SUBAGENT_PROFILE_MATURITY_V2.RETIRED,
243
+ ]),
244
+ ],
245
+ [
246
+ SUBAGENT_PROFILE_MATURITY_V2.ACTIVE,
247
+ new Set([
248
+ SUBAGENT_PROFILE_MATURITY_V2.PAUSED,
249
+ SUBAGENT_PROFILE_MATURITY_V2.RETIRED,
250
+ ]),
251
+ ],
252
+ [
253
+ SUBAGENT_PROFILE_MATURITY_V2.PAUSED,
254
+ new Set([
255
+ SUBAGENT_PROFILE_MATURITY_V2.ACTIVE,
256
+ SUBAGENT_PROFILE_MATURITY_V2.RETIRED,
257
+ ]),
258
+ ],
235
259
  [SUBAGENT_PROFILE_MATURITY_V2.RETIRED, new Set()],
236
260
  ]);
237
261
  const _saProfileTerminal = new Set([SUBAGENT_PROFILE_MATURITY_V2.RETIRED]);
238
262
  const _saTaskTrans = new Map([
239
- [SUBAGENT_TASK_LIFECYCLE_V2.QUEUED, new Set([SUBAGENT_TASK_LIFECYCLE_V2.RUNNING, SUBAGENT_TASK_LIFECYCLE_V2.CANCELLED])],
240
- [SUBAGENT_TASK_LIFECYCLE_V2.RUNNING, new Set([SUBAGENT_TASK_LIFECYCLE_V2.COMPLETED, SUBAGENT_TASK_LIFECYCLE_V2.FAILED, SUBAGENT_TASK_LIFECYCLE_V2.CANCELLED])],
263
+ [
264
+ SUBAGENT_TASK_LIFECYCLE_V2.QUEUED,
265
+ new Set([
266
+ SUBAGENT_TASK_LIFECYCLE_V2.RUNNING,
267
+ SUBAGENT_TASK_LIFECYCLE_V2.CANCELLED,
268
+ ]),
269
+ ],
270
+ [
271
+ SUBAGENT_TASK_LIFECYCLE_V2.RUNNING,
272
+ new Set([
273
+ SUBAGENT_TASK_LIFECYCLE_V2.COMPLETED,
274
+ SUBAGENT_TASK_LIFECYCLE_V2.FAILED,
275
+ SUBAGENT_TASK_LIFECYCLE_V2.CANCELLED,
276
+ ]),
277
+ ],
241
278
  [SUBAGENT_TASK_LIFECYCLE_V2.COMPLETED, new Set()],
242
279
  [SUBAGENT_TASK_LIFECYCLE_V2.FAILED, new Set()],
243
280
  [SUBAGENT_TASK_LIFECYCLE_V2.CANCELLED, new Set()],
@@ -250,81 +287,293 @@ let _saMaxPendingPerProfile = 24;
250
287
  let _saProfileIdleMs = 2 * 60 * 60 * 1000;
251
288
  let _saTaskStuckMs = 5 * 60 * 1000;
252
289
 
253
- function _saPos(n, lbl) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${lbl} must be positive integer`); return v; }
290
+ function _saPos(n, lbl) {
291
+ const v = Math.floor(Number(n));
292
+ if (!Number.isFinite(v) || v <= 0)
293
+ throw new Error(`${lbl} must be positive integer`);
294
+ return v;
295
+ }
254
296
 
255
- export function setMaxActiveSubagentsPerOwnerV2(n) { _saMaxActivePerOwner = _saPos(n, "maxActiveSubagentsPerOwner"); }
256
- export function getMaxActiveSubagentsPerOwnerV2() { return _saMaxActivePerOwner; }
257
- export function setMaxPendingTasksPerSubagentV2(n) { _saMaxPendingPerProfile = _saPos(n, "maxPendingTasksPerSubagent"); }
258
- export function getMaxPendingTasksPerSubagentV2() { return _saMaxPendingPerProfile; }
259
- export function setSubagentIdleMsV2(n) { _saProfileIdleMs = _saPos(n, "subagentIdleMs"); }
260
- export function getSubagentIdleMsV2() { return _saProfileIdleMs; }
261
- export function setSubagentTaskStuckMsV2(n) { _saTaskStuckMs = _saPos(n, "subagentTaskStuckMs"); }
262
- export function getSubagentTaskStuckMsV2() { return _saTaskStuckMs; }
297
+ export function setMaxActiveSubagentsPerOwnerV2(n) {
298
+ _saMaxActivePerOwner = _saPos(n, "maxActiveSubagentsPerOwner");
299
+ }
300
+ export function getMaxActiveSubagentsPerOwnerV2() {
301
+ return _saMaxActivePerOwner;
302
+ }
303
+ export function setMaxPendingTasksPerSubagentV2(n) {
304
+ _saMaxPendingPerProfile = _saPos(n, "maxPendingTasksPerSubagent");
305
+ }
306
+ export function getMaxPendingTasksPerSubagentV2() {
307
+ return _saMaxPendingPerProfile;
308
+ }
309
+ export function setSubagentIdleMsV2(n) {
310
+ _saProfileIdleMs = _saPos(n, "subagentIdleMs");
311
+ }
312
+ export function getSubagentIdleMsV2() {
313
+ return _saProfileIdleMs;
314
+ }
315
+ export function setSubagentTaskStuckMsV2(n) {
316
+ _saTaskStuckMs = _saPos(n, "subagentTaskStuckMs");
317
+ }
318
+ export function getSubagentTaskStuckMsV2() {
319
+ return _saTaskStuckMs;
320
+ }
263
321
 
264
322
  export function _resetStateSubAgentRegistryV2() {
265
- _saProfiles.clear(); _saTasks.clear();
266
- _saMaxActivePerOwner = 12; _saMaxPendingPerProfile = 24;
267
- _saProfileIdleMs = 2 * 60 * 60 * 1000; _saTaskStuckMs = 5 * 60 * 1000;
323
+ _saProfiles.clear();
324
+ _saTasks.clear();
325
+ _saMaxActivePerOwner = 12;
326
+ _saMaxPendingPerProfile = 24;
327
+ _saProfileIdleMs = 2 * 60 * 60 * 1000;
328
+ _saTaskStuckMs = 5 * 60 * 1000;
268
329
  }
269
330
 
270
331
  export function registerSubagentProfileV2({ id, owner, role, metadata } = {}) {
271
332
  if (!id || typeof id !== "string") throw new Error("id is required");
272
333
  if (!owner || typeof owner !== "string") throw new Error("owner is required");
273
- if (_saProfiles.has(id)) throw new Error(`subagent profile ${id} already registered`);
334
+ if (_saProfiles.has(id))
335
+ throw new Error(`subagent profile ${id} already registered`);
274
336
  const now = Date.now();
275
- const p = { id, owner, role: role || "generic", status: SUBAGENT_PROFILE_MATURITY_V2.PENDING,
276
- createdAt: now, updatedAt: now, activatedAt: null, retiredAt: null, lastTouchedAt: now,
277
- metadata: { ...(metadata || {}) } };
337
+ const p = {
338
+ id,
339
+ owner,
340
+ role: role || "generic",
341
+ status: SUBAGENT_PROFILE_MATURITY_V2.PENDING,
342
+ createdAt: now,
343
+ updatedAt: now,
344
+ activatedAt: null,
345
+ retiredAt: null,
346
+ lastTouchedAt: now,
347
+ metadata: { ...(metadata || {}) },
348
+ };
278
349
  _saProfiles.set(id, p);
279
350
  return { ...p, metadata: { ...p.metadata } };
280
351
  }
281
352
 
282
- function _saCheckP(from, to) { const allowed = _saProfileTrans.get(from); if (!allowed || !allowed.has(to)) throw new Error(`invalid subagent profile transition ${from} → ${to}`); }
283
- function _saCountActiveByOwner(owner) { let n = 0; for (const p of _saProfiles.values()) if (p.owner === owner && p.status === SUBAGENT_PROFILE_MATURITY_V2.ACTIVE) n++; return n; }
353
+ function _saCheckP(from, to) {
354
+ const allowed = _saProfileTrans.get(from);
355
+ if (!allowed || !allowed.has(to))
356
+ throw new Error(`invalid subagent profile transition ${from} → ${to}`);
357
+ }
358
+ function _saCountActiveByOwner(owner) {
359
+ let n = 0;
360
+ for (const p of _saProfiles.values())
361
+ if (p.owner === owner && p.status === SUBAGENT_PROFILE_MATURITY_V2.ACTIVE)
362
+ n++;
363
+ return n;
364
+ }
284
365
 
285
366
  export function activateSubagentProfileV2(id) {
286
- const p = _saProfiles.get(id); if (!p) throw new Error(`subagent profile ${id} not found`);
367
+ const p = _saProfiles.get(id);
368
+ if (!p) throw new Error(`subagent profile ${id} not found`);
287
369
  _saCheckP(p.status, SUBAGENT_PROFILE_MATURITY_V2.ACTIVE);
288
370
  const recovery = p.status === SUBAGENT_PROFILE_MATURITY_V2.PAUSED;
289
- if (!recovery) { const a = _saCountActiveByOwner(p.owner); if (a >= _saMaxActivePerOwner) throw new Error(`max active subagents per owner (${_saMaxActivePerOwner}) reached for ${p.owner}`); }
290
- const now = Date.now(); p.status = SUBAGENT_PROFILE_MATURITY_V2.ACTIVE; p.updatedAt = now; p.lastTouchedAt = now;
371
+ if (!recovery) {
372
+ const a = _saCountActiveByOwner(p.owner);
373
+ if (a >= _saMaxActivePerOwner)
374
+ throw new Error(
375
+ `max active subagents per owner (${_saMaxActivePerOwner}) reached for ${p.owner}`,
376
+ );
377
+ }
378
+ const now = Date.now();
379
+ p.status = SUBAGENT_PROFILE_MATURITY_V2.ACTIVE;
380
+ p.updatedAt = now;
381
+ p.lastTouchedAt = now;
291
382
  if (!p.activatedAt) p.activatedAt = now;
292
383
  return { ...p, metadata: { ...p.metadata } };
293
384
  }
294
- export function pauseSubagentProfileV2(id) { const p = _saProfiles.get(id); if (!p) throw new Error(`subagent profile ${id} not found`); _saCheckP(p.status, SUBAGENT_PROFILE_MATURITY_V2.PAUSED); p.status = SUBAGENT_PROFILE_MATURITY_V2.PAUSED; p.updatedAt = Date.now(); return { ...p, metadata: { ...p.metadata } }; }
295
- export function retireSubagentProfileV2(id) { const p = _saProfiles.get(id); if (!p) throw new Error(`subagent profile ${id} not found`); _saCheckP(p.status, SUBAGENT_PROFILE_MATURITY_V2.RETIRED); const now = Date.now(); p.status = SUBAGENT_PROFILE_MATURITY_V2.RETIRED; p.updatedAt = now; if (!p.retiredAt) p.retiredAt = now; return { ...p, metadata: { ...p.metadata } }; }
296
- export function touchSubagentProfileV2(id) { const p = _saProfiles.get(id); if (!p) throw new Error(`subagent profile ${id} not found`); if (_saProfileTerminal.has(p.status)) throw new Error(`cannot touch terminal subagent profile ${id}`); const now = Date.now(); p.lastTouchedAt = now; p.updatedAt = now; return { ...p, metadata: { ...p.metadata } }; }
297
- export function getSubagentProfileV2(id) { const p = _saProfiles.get(id); if (!p) return null; return { ...p, metadata: { ...p.metadata } }; }
298
- export function listSubagentProfilesV2() { return [..._saProfiles.values()].map((p) => ({ ...p, metadata: { ...p.metadata } })); }
385
+ export function pauseSubagentProfileV2(id) {
386
+ const p = _saProfiles.get(id);
387
+ if (!p) throw new Error(`subagent profile ${id} not found`);
388
+ _saCheckP(p.status, SUBAGENT_PROFILE_MATURITY_V2.PAUSED);
389
+ p.status = SUBAGENT_PROFILE_MATURITY_V2.PAUSED;
390
+ p.updatedAt = Date.now();
391
+ return { ...p, metadata: { ...p.metadata } };
392
+ }
393
+ export function retireSubagentProfileV2(id) {
394
+ const p = _saProfiles.get(id);
395
+ if (!p) throw new Error(`subagent profile ${id} not found`);
396
+ _saCheckP(p.status, SUBAGENT_PROFILE_MATURITY_V2.RETIRED);
397
+ const now = Date.now();
398
+ p.status = SUBAGENT_PROFILE_MATURITY_V2.RETIRED;
399
+ p.updatedAt = now;
400
+ if (!p.retiredAt) p.retiredAt = now;
401
+ return { ...p, metadata: { ...p.metadata } };
402
+ }
403
+ export function touchSubagentProfileV2(id) {
404
+ const p = _saProfiles.get(id);
405
+ if (!p) throw new Error(`subagent profile ${id} not found`);
406
+ if (_saProfileTerminal.has(p.status))
407
+ throw new Error(`cannot touch terminal subagent profile ${id}`);
408
+ const now = Date.now();
409
+ p.lastTouchedAt = now;
410
+ p.updatedAt = now;
411
+ return { ...p, metadata: { ...p.metadata } };
412
+ }
413
+ export function getSubagentProfileV2(id) {
414
+ const p = _saProfiles.get(id);
415
+ if (!p) return null;
416
+ return { ...p, metadata: { ...p.metadata } };
417
+ }
418
+ export function listSubagentProfilesV2() {
419
+ return [..._saProfiles.values()].map((p) => ({
420
+ ...p,
421
+ metadata: { ...p.metadata },
422
+ }));
423
+ }
299
424
 
300
- function _saCountPendingByProfile(pid) { let n = 0; for (const t of _saTasks.values()) if (t.profileId === pid && (t.status === SUBAGENT_TASK_LIFECYCLE_V2.QUEUED || t.status === SUBAGENT_TASK_LIFECYCLE_V2.RUNNING)) n++; return n; }
425
+ function _saCountPendingByProfile(pid) {
426
+ let n = 0;
427
+ for (const t of _saTasks.values())
428
+ if (
429
+ t.profileId === pid &&
430
+ (t.status === SUBAGENT_TASK_LIFECYCLE_V2.QUEUED ||
431
+ t.status === SUBAGENT_TASK_LIFECYCLE_V2.RUNNING)
432
+ )
433
+ n++;
434
+ return n;
435
+ }
301
436
 
302
- export function createSubagentTaskV2({ id, profileId, description, metadata } = {}) {
437
+ export function createSubagentTaskV2({
438
+ id,
439
+ profileId,
440
+ description,
441
+ metadata,
442
+ } = {}) {
303
443
  if (!id || typeof id !== "string") throw new Error("id is required");
304
- if (!profileId || typeof profileId !== "string") throw new Error("profileId is required");
444
+ if (!profileId || typeof profileId !== "string")
445
+ throw new Error("profileId is required");
305
446
  if (_saTasks.has(id)) throw new Error(`subagent task ${id} already exists`);
306
- if (!_saProfiles.has(profileId)) throw new Error(`subagent profile ${profileId} not found`);
447
+ if (!_saProfiles.has(profileId))
448
+ throw new Error(`subagent profile ${profileId} not found`);
307
449
  const pending = _saCountPendingByProfile(profileId);
308
- if (pending >= _saMaxPendingPerProfile) throw new Error(`max pending tasks per subagent (${_saMaxPendingPerProfile}) reached for ${profileId}`);
450
+ if (pending >= _saMaxPendingPerProfile)
451
+ throw new Error(
452
+ `max pending tasks per subagent (${_saMaxPendingPerProfile}) reached for ${profileId}`,
453
+ );
309
454
  const now = Date.now();
310
- const t = { id, profileId, description: description || "", status: SUBAGENT_TASK_LIFECYCLE_V2.QUEUED,
311
- createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
455
+ const t = {
456
+ id,
457
+ profileId,
458
+ description: description || "",
459
+ status: SUBAGENT_TASK_LIFECYCLE_V2.QUEUED,
460
+ createdAt: now,
461
+ updatedAt: now,
462
+ startedAt: null,
463
+ settledAt: null,
464
+ metadata: { ...(metadata || {}) },
465
+ };
312
466
  _saTasks.set(id, t);
313
467
  return { ...t, metadata: { ...t.metadata } };
314
468
  }
315
- function _saCheckT(from, to) { const allowed = _saTaskTrans.get(from); if (!allowed || !allowed.has(to)) throw new Error(`invalid subagent task transition ${from} → ${to}`); }
316
- export function startSubagentTaskV2(id) { const t = _saTasks.get(id); if (!t) throw new Error(`subagent task ${id} not found`); _saCheckT(t.status, SUBAGENT_TASK_LIFECYCLE_V2.RUNNING); const now = Date.now(); t.status = SUBAGENT_TASK_LIFECYCLE_V2.RUNNING; t.updatedAt = now; if (!t.startedAt) t.startedAt = now; return { ...t, metadata: { ...t.metadata } }; }
317
- export function completeSubagentTaskV2(id) { const t = _saTasks.get(id); if (!t) throw new Error(`subagent task ${id} not found`); _saCheckT(t.status, SUBAGENT_TASK_LIFECYCLE_V2.COMPLETED); const now = Date.now(); t.status = SUBAGENT_TASK_LIFECYCLE_V2.COMPLETED; t.updatedAt = now; if (!t.settledAt) t.settledAt = now; return { ...t, metadata: { ...t.metadata } }; }
318
- export function failSubagentTaskV2(id, reason) { const t = _saTasks.get(id); if (!t) throw new Error(`subagent task ${id} not found`); _saCheckT(t.status, SUBAGENT_TASK_LIFECYCLE_V2.FAILED); const now = Date.now(); t.status = SUBAGENT_TASK_LIFECYCLE_V2.FAILED; t.updatedAt = now; if (!t.settledAt) t.settledAt = now; if (reason) t.metadata.failReason = String(reason); return { ...t, metadata: { ...t.metadata } }; }
319
- export function cancelSubagentTaskV2(id, reason) { const t = _saTasks.get(id); if (!t) throw new Error(`subagent task ${id} not found`); _saCheckT(t.status, SUBAGENT_TASK_LIFECYCLE_V2.CANCELLED); const now = Date.now(); t.status = SUBAGENT_TASK_LIFECYCLE_V2.CANCELLED; t.updatedAt = now; if (!t.settledAt) t.settledAt = now; if (reason) t.metadata.cancelReason = String(reason); return { ...t, metadata: { ...t.metadata } }; }
320
- export function getSubagentTaskV2(id) { const t = _saTasks.get(id); if (!t) return null; return { ...t, metadata: { ...t.metadata } }; }
321
- export function listSubagentTasksV2() { return [..._saTasks.values()].map((t) => ({ ...t, metadata: { ...t.metadata } })); }
469
+ function _saCheckT(from, to) {
470
+ const allowed = _saTaskTrans.get(from);
471
+ if (!allowed || !allowed.has(to))
472
+ throw new Error(`invalid subagent task transition ${from} ${to}`);
473
+ }
474
+ export function startSubagentTaskV2(id) {
475
+ const t = _saTasks.get(id);
476
+ if (!t) throw new Error(`subagent task ${id} not found`);
477
+ _saCheckT(t.status, SUBAGENT_TASK_LIFECYCLE_V2.RUNNING);
478
+ const now = Date.now();
479
+ t.status = SUBAGENT_TASK_LIFECYCLE_V2.RUNNING;
480
+ t.updatedAt = now;
481
+ if (!t.startedAt) t.startedAt = now;
482
+ return { ...t, metadata: { ...t.metadata } };
483
+ }
484
+ export function completeSubagentTaskV2(id) {
485
+ const t = _saTasks.get(id);
486
+ if (!t) throw new Error(`subagent task ${id} not found`);
487
+ _saCheckT(t.status, SUBAGENT_TASK_LIFECYCLE_V2.COMPLETED);
488
+ const now = Date.now();
489
+ t.status = SUBAGENT_TASK_LIFECYCLE_V2.COMPLETED;
490
+ t.updatedAt = now;
491
+ if (!t.settledAt) t.settledAt = now;
492
+ return { ...t, metadata: { ...t.metadata } };
493
+ }
494
+ export function failSubagentTaskV2(id, reason) {
495
+ const t = _saTasks.get(id);
496
+ if (!t) throw new Error(`subagent task ${id} not found`);
497
+ _saCheckT(t.status, SUBAGENT_TASK_LIFECYCLE_V2.FAILED);
498
+ const now = Date.now();
499
+ t.status = SUBAGENT_TASK_LIFECYCLE_V2.FAILED;
500
+ t.updatedAt = now;
501
+ if (!t.settledAt) t.settledAt = now;
502
+ if (reason) t.metadata.failReason = String(reason);
503
+ return { ...t, metadata: { ...t.metadata } };
504
+ }
505
+ export function cancelSubagentTaskV2(id, reason) {
506
+ const t = _saTasks.get(id);
507
+ if (!t) throw new Error(`subagent task ${id} not found`);
508
+ _saCheckT(t.status, SUBAGENT_TASK_LIFECYCLE_V2.CANCELLED);
509
+ const now = Date.now();
510
+ t.status = SUBAGENT_TASK_LIFECYCLE_V2.CANCELLED;
511
+ t.updatedAt = now;
512
+ if (!t.settledAt) t.settledAt = now;
513
+ if (reason) t.metadata.cancelReason = String(reason);
514
+ return { ...t, metadata: { ...t.metadata } };
515
+ }
516
+ export function getSubagentTaskV2(id) {
517
+ const t = _saTasks.get(id);
518
+ if (!t) return null;
519
+ return { ...t, metadata: { ...t.metadata } };
520
+ }
521
+ export function listSubagentTasksV2() {
522
+ return [..._saTasks.values()].map((t) => ({
523
+ ...t,
524
+ metadata: { ...t.metadata },
525
+ }));
526
+ }
322
527
 
323
- export function autoPauseIdleSubagentsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const p of _saProfiles.values()) if (p.status === SUBAGENT_PROFILE_MATURITY_V2.ACTIVE && (t - p.lastTouchedAt) >= _saProfileIdleMs) { p.status = SUBAGENT_PROFILE_MATURITY_V2.PAUSED; p.updatedAt = t; flipped.push(p.id); } return { flipped, count: flipped.length }; }
324
- export function autoFailStuckSubagentTasksV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const k of _saTasks.values()) if (k.status === SUBAGENT_TASK_LIFECYCLE_V2.RUNNING && k.startedAt != null && (t - k.startedAt) >= _saTaskStuckMs) { k.status = SUBAGENT_TASK_LIFECYCLE_V2.FAILED; k.updatedAt = t; if (!k.settledAt) k.settledAt = t; k.metadata.failReason = "auto-fail-stuck"; flipped.push(k.id); } return { flipped, count: flipped.length }; }
528
+ export function autoPauseIdleSubagentsV2({ now } = {}) {
529
+ const t = now ?? Date.now();
530
+ const flipped = [];
531
+ for (const p of _saProfiles.values())
532
+ if (
533
+ p.status === SUBAGENT_PROFILE_MATURITY_V2.ACTIVE &&
534
+ t - p.lastTouchedAt >= _saProfileIdleMs
535
+ ) {
536
+ p.status = SUBAGENT_PROFILE_MATURITY_V2.PAUSED;
537
+ p.updatedAt = t;
538
+ flipped.push(p.id);
539
+ }
540
+ return { flipped, count: flipped.length };
541
+ }
542
+ export function autoFailStuckSubagentTasksV2({ now } = {}) {
543
+ const t = now ?? Date.now();
544
+ const flipped = [];
545
+ for (const k of _saTasks.values())
546
+ if (
547
+ k.status === SUBAGENT_TASK_LIFECYCLE_V2.RUNNING &&
548
+ k.startedAt != null &&
549
+ t - k.startedAt >= _saTaskStuckMs
550
+ ) {
551
+ k.status = SUBAGENT_TASK_LIFECYCLE_V2.FAILED;
552
+ k.updatedAt = t;
553
+ if (!k.settledAt) k.settledAt = t;
554
+ k.metadata.failReason = "auto-fail-stuck";
555
+ flipped.push(k.id);
556
+ }
557
+ return { flipped, count: flipped.length };
558
+ }
325
559
 
326
560
  export function getSubAgentRegistryStatsV2() {
327
- const profilesByStatus = {}; for (const s of Object.values(SUBAGENT_PROFILE_MATURITY_V2)) profilesByStatus[s] = 0; for (const p of _saProfiles.values()) profilesByStatus[p.status]++;
328
- const tasksByStatus = {}; for (const s of Object.values(SUBAGENT_TASK_LIFECYCLE_V2)) tasksByStatus[s] = 0; for (const t of _saTasks.values()) tasksByStatus[t.status]++;
329
- return { totalProfilesV2: _saProfiles.size, totalTasksV2: _saTasks.size, maxActiveSubagentsPerOwner: _saMaxActivePerOwner, maxPendingTasksPerSubagent: _saMaxPendingPerProfile, subagentIdleMs: _saProfileIdleMs, subagentTaskStuckMs: _saTaskStuckMs, profilesByStatus, tasksByStatus };
561
+ const profilesByStatus = {};
562
+ for (const s of Object.values(SUBAGENT_PROFILE_MATURITY_V2))
563
+ profilesByStatus[s] = 0;
564
+ for (const p of _saProfiles.values()) profilesByStatus[p.status]++;
565
+ const tasksByStatus = {};
566
+ for (const s of Object.values(SUBAGENT_TASK_LIFECYCLE_V2))
567
+ tasksByStatus[s] = 0;
568
+ for (const t of _saTasks.values()) tasksByStatus[t.status]++;
569
+ return {
570
+ totalProfilesV2: _saProfiles.size,
571
+ totalTasksV2: _saTasks.size,
572
+ maxActiveSubagentsPerOwner: _saMaxActivePerOwner,
573
+ maxPendingTasksPerSubagent: _saMaxPendingPerProfile,
574
+ subagentIdleMs: _saProfileIdleMs,
575
+ subagentTaskStuckMs: _saTaskStuckMs,
576
+ profilesByStatus,
577
+ tasksByStatus,
578
+ };
330
579
  }