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
@@ -345,3 +345,336 @@ export function getServer(idOrName) {
345
345
  ) || null
346
346
  );
347
347
  }
348
+
349
+ // ===== V2 Surface (cli 0.131.0) — in-memory governance =====
350
+ export const MCP_SERVER_MATURITY_V2 = Object.freeze({
351
+ PENDING: "pending",
352
+ ACTIVE: "active",
353
+ DEGRADED: "degraded",
354
+ RETIRED: "retired",
355
+ });
356
+ export const MCP_INVOCATION_LIFECYCLE_V2 = Object.freeze({
357
+ QUEUED: "queued",
358
+ DISPATCHING: "dispatching",
359
+ COMPLETED: "completed",
360
+ FAILED: "failed",
361
+ CANCELLED: "cancelled",
362
+ });
363
+
364
+ const _MS_V2 = MCP_SERVER_MATURITY_V2;
365
+ const _MI_V2 = MCP_INVOCATION_LIFECYCLE_V2;
366
+ const _MS_TRANS_V2 = new Map([
367
+ [_MS_V2.PENDING, new Set([_MS_V2.ACTIVE, _MS_V2.RETIRED])],
368
+ [_MS_V2.ACTIVE, new Set([_MS_V2.DEGRADED, _MS_V2.RETIRED])],
369
+ [_MS_V2.DEGRADED, new Set([_MS_V2.ACTIVE, _MS_V2.RETIRED])],
370
+ [_MS_V2.RETIRED, new Set()],
371
+ ]);
372
+ const _MI_TRANS_V2 = new Map([
373
+ [_MI_V2.QUEUED, new Set([_MI_V2.DISPATCHING, _MI_V2.CANCELLED])],
374
+ [
375
+ _MI_V2.DISPATCHING,
376
+ new Set([_MI_V2.COMPLETED, _MI_V2.FAILED, _MI_V2.CANCELLED]),
377
+ ],
378
+ [_MI_V2.COMPLETED, new Set()],
379
+ [_MI_V2.FAILED, new Set()],
380
+ [_MI_V2.CANCELLED, new Set()],
381
+ ]);
382
+ const _MI_TERM_V2 = new Set([
383
+ _MI_V2.COMPLETED,
384
+ _MI_V2.FAILED,
385
+ _MI_V2.CANCELLED,
386
+ ]);
387
+
388
+ const MCP_DEFAULT_MAX_ACTIVE_SERVERS_PER_OWNER = 10;
389
+ const MCP_DEFAULT_MAX_PENDING_INVOCATIONS_PER_SERVER = 20;
390
+ const MCP_DEFAULT_SERVER_IDLE_MS = 7 * 24 * 60 * 60 * 1000;
391
+ const MCP_DEFAULT_INVOCATION_STUCK_MS = 2 * 60 * 1000;
392
+
393
+ const _mcpServersV2 = new Map();
394
+ const _mcpInvocationsV2 = new Map();
395
+ let _mcpConfigV2 = {
396
+ maxActiveServersPerOwner: MCP_DEFAULT_MAX_ACTIVE_SERVERS_PER_OWNER,
397
+ maxPendingInvocationsPerServer:
398
+ MCP_DEFAULT_MAX_PENDING_INVOCATIONS_PER_SERVER,
399
+ serverIdleMs: MCP_DEFAULT_SERVER_IDLE_MS,
400
+ invocationStuckMs: MCP_DEFAULT_INVOCATION_STUCK_MS,
401
+ };
402
+
403
+ function _mcpPosIntV2(n, label) {
404
+ if (typeof n !== "number" || !isFinite(n) || isNaN(n))
405
+ throw new Error(`${label} must be positive integer`);
406
+ const v = Math.floor(n);
407
+ if (v <= 0) throw new Error(`${label} must be positive integer`);
408
+ return v;
409
+ }
410
+
411
+ export function _resetStateMcpRegistryV2() {
412
+ _mcpServersV2.clear();
413
+ _mcpInvocationsV2.clear();
414
+ _mcpConfigV2 = {
415
+ maxActiveServersPerOwner: MCP_DEFAULT_MAX_ACTIVE_SERVERS_PER_OWNER,
416
+ maxPendingInvocationsPerServer:
417
+ MCP_DEFAULT_MAX_PENDING_INVOCATIONS_PER_SERVER,
418
+ serverIdleMs: MCP_DEFAULT_SERVER_IDLE_MS,
419
+ invocationStuckMs: MCP_DEFAULT_INVOCATION_STUCK_MS,
420
+ };
421
+ }
422
+
423
+ export function setMaxActiveServersPerOwnerV2(n) {
424
+ _mcpConfigV2.maxActiveServersPerOwner = _mcpPosIntV2(
425
+ n,
426
+ "maxActiveServersPerOwner",
427
+ );
428
+ }
429
+ export function setMaxPendingInvocationsPerServerV2(n) {
430
+ _mcpConfigV2.maxPendingInvocationsPerServer = _mcpPosIntV2(
431
+ n,
432
+ "maxPendingInvocationsPerServer",
433
+ );
434
+ }
435
+ export function setServerIdleMsV2(n) {
436
+ _mcpConfigV2.serverIdleMs = _mcpPosIntV2(n, "serverIdleMs");
437
+ }
438
+ export function setInvocationStuckMsV2(n) {
439
+ _mcpConfigV2.invocationStuckMs = _mcpPosIntV2(n, "invocationStuckMs");
440
+ }
441
+ export function getMaxActiveServersPerOwnerV2() {
442
+ return _mcpConfigV2.maxActiveServersPerOwner;
443
+ }
444
+ export function getMaxPendingInvocationsPerServerV2() {
445
+ return _mcpConfigV2.maxPendingInvocationsPerServer;
446
+ }
447
+ export function getServerIdleMsV2() {
448
+ return _mcpConfigV2.serverIdleMs;
449
+ }
450
+ export function getInvocationStuckMsV2() {
451
+ return _mcpConfigV2.invocationStuckMs;
452
+ }
453
+
454
+ function _copyServerV2(s) {
455
+ return { ...s, metadata: { ...(s.metadata || {}) } };
456
+ }
457
+ function _copyInvocationV2(i) {
458
+ return { ...i, metadata: { ...(i.metadata || {}) } };
459
+ }
460
+
461
+ export function registerServerV2({
462
+ id,
463
+ owner,
464
+ transport,
465
+ name,
466
+ metadata,
467
+ } = {}) {
468
+ if (!id || typeof id !== "string") throw new Error("id required");
469
+ if (!owner || typeof owner !== "string") throw new Error("owner required");
470
+ if (!transport || typeof transport !== "string")
471
+ throw new Error("transport required");
472
+ if (_mcpServersV2.has(id)) throw new Error(`server ${id} already registered`);
473
+ const now = Date.now();
474
+ const s = {
475
+ id,
476
+ owner,
477
+ transport,
478
+ name: name || id,
479
+ status: _MS_V2.PENDING,
480
+ activatedAt: null,
481
+ retiredAt: null,
482
+ lastSeenAt: now,
483
+ createdAt: now,
484
+ metadata: metadata && typeof metadata === "object" ? { ...metadata } : {},
485
+ };
486
+ _mcpServersV2.set(id, s);
487
+ return _copyServerV2(s);
488
+ }
489
+
490
+ function _activeServerCountForOwnerV2(owner) {
491
+ let c = 0;
492
+ for (const s of _mcpServersV2.values())
493
+ if (s.owner === owner && s.status === _MS_V2.ACTIVE) c++;
494
+ return c;
495
+ }
496
+
497
+ function _transitionServerV2(id, next) {
498
+ const s = _mcpServersV2.get(id);
499
+ if (!s) throw new Error(`server ${id} not found`);
500
+ const allowed = _MS_TRANS_V2.get(s.status);
501
+ if (!allowed || !allowed.has(next))
502
+ throw new Error(`invalid transition ${s.status} -> ${next}`);
503
+ if (next === _MS_V2.ACTIVE && s.status === _MS_V2.PENDING) {
504
+ if (
505
+ _activeServerCountForOwnerV2(s.owner) >=
506
+ _mcpConfigV2.maxActiveServersPerOwner
507
+ ) {
508
+ throw new Error(
509
+ `owner ${s.owner} active-server cap reached (${_mcpConfigV2.maxActiveServersPerOwner})`,
510
+ );
511
+ }
512
+ }
513
+ const now = Date.now();
514
+ s.status = next;
515
+ if (next === _MS_V2.ACTIVE && !s.activatedAt) s.activatedAt = now;
516
+ if (next === _MS_V2.RETIRED && !s.retiredAt) s.retiredAt = now;
517
+ s.lastSeenAt = now;
518
+ return _copyServerV2(s);
519
+ }
520
+
521
+ export function activateServerV2(id) {
522
+ return _transitionServerV2(id, _MS_V2.ACTIVE);
523
+ }
524
+ export function degradeServerV2(id) {
525
+ return _transitionServerV2(id, _MS_V2.DEGRADED);
526
+ }
527
+ export function retireServerV2(id) {
528
+ return _transitionServerV2(id, _MS_V2.RETIRED);
529
+ }
530
+ export function touchServerV2(id) {
531
+ const s = _mcpServersV2.get(id);
532
+ if (!s) throw new Error(`server ${id} not found`);
533
+ s.lastSeenAt = Date.now();
534
+ return _copyServerV2(s);
535
+ }
536
+ export function getServerV2(id) {
537
+ const s = _mcpServersV2.get(id);
538
+ return s ? _copyServerV2(s) : null;
539
+ }
540
+ export function listServersV2({ owner, status, transport } = {}) {
541
+ const out = [];
542
+ for (const s of _mcpServersV2.values()) {
543
+ if (owner && s.owner !== owner) continue;
544
+ if (status && s.status !== status) continue;
545
+ if (transport && s.transport !== transport) continue;
546
+ out.push(_copyServerV2(s));
547
+ }
548
+ return out;
549
+ }
550
+
551
+ function _pendingInvocationCountForServerV2(serverId) {
552
+ let c = 0;
553
+ for (const i of _mcpInvocationsV2.values()) {
554
+ if (i.serverId !== serverId) continue;
555
+ if (i.status === _MI_V2.QUEUED || i.status === _MI_V2.DISPATCHING) c++;
556
+ }
557
+ return c;
558
+ }
559
+
560
+ export function createInvocationV2({ id, serverId, tool, metadata } = {}) {
561
+ if (!id || typeof id !== "string") throw new Error("id required");
562
+ if (!serverId || typeof serverId !== "string")
563
+ throw new Error("serverId required");
564
+ if (_mcpInvocationsV2.has(id))
565
+ throw new Error(`invocation ${id} already exists`);
566
+ const server = _mcpServersV2.get(serverId);
567
+ if (!server) throw new Error(`server ${serverId} not found`);
568
+ if (server.status === _MS_V2.RETIRED)
569
+ throw new Error(`server ${serverId} retired`);
570
+ if (
571
+ _pendingInvocationCountForServerV2(serverId) >=
572
+ _mcpConfigV2.maxPendingInvocationsPerServer
573
+ ) {
574
+ throw new Error(
575
+ `server ${serverId} pending-invocation cap reached (${_mcpConfigV2.maxPendingInvocationsPerServer})`,
576
+ );
577
+ }
578
+ const now = Date.now();
579
+ const i = {
580
+ id,
581
+ serverId,
582
+ tool: tool || "unknown",
583
+ status: _MI_V2.QUEUED,
584
+ startedAt: null,
585
+ settledAt: null,
586
+ createdAt: now,
587
+ metadata: metadata && typeof metadata === "object" ? { ...metadata } : {},
588
+ };
589
+ _mcpInvocationsV2.set(id, i);
590
+ return _copyInvocationV2(i);
591
+ }
592
+
593
+ function _transitionInvocationV2(id, next, extra = {}) {
594
+ const inv = _mcpInvocationsV2.get(id);
595
+ if (!inv) throw new Error(`invocation ${id} not found`);
596
+ const allowed = _MI_TRANS_V2.get(inv.status);
597
+ if (!allowed || !allowed.has(next))
598
+ throw new Error(`invalid transition ${inv.status} -> ${next}`);
599
+ const now = Date.now();
600
+ inv.status = next;
601
+ if (next === _MI_V2.DISPATCHING && !inv.startedAt) inv.startedAt = now;
602
+ if (_MI_TERM_V2.has(next) && !inv.settledAt) inv.settledAt = now;
603
+ if (extra.error) inv.metadata.error = extra.error;
604
+ return _copyInvocationV2(inv);
605
+ }
606
+
607
+ export function dispatchInvocationV2(id) {
608
+ return _transitionInvocationV2(id, _MI_V2.DISPATCHING);
609
+ }
610
+ export function completeInvocationV2(id) {
611
+ return _transitionInvocationV2(id, _MI_V2.COMPLETED);
612
+ }
613
+ export function failInvocationV2(id, error) {
614
+ return _transitionInvocationV2(id, _MI_V2.FAILED, { error });
615
+ }
616
+ export function cancelInvocationV2(id) {
617
+ return _transitionInvocationV2(id, _MI_V2.CANCELLED);
618
+ }
619
+ export function getInvocationV2(id) {
620
+ const i = _mcpInvocationsV2.get(id);
621
+ return i ? _copyInvocationV2(i) : null;
622
+ }
623
+ export function listInvocationsV2({ serverId, status, tool } = {}) {
624
+ const out = [];
625
+ for (const i of _mcpInvocationsV2.values()) {
626
+ if (serverId && i.serverId !== serverId) continue;
627
+ if (status && i.status !== status) continue;
628
+ if (tool && i.tool !== tool) continue;
629
+ out.push(_copyInvocationV2(i));
630
+ }
631
+ return out;
632
+ }
633
+
634
+ export function autoDegradeIdleServersV2({ now } = {}) {
635
+ const t = typeof now === "number" ? now : Date.now();
636
+ const flipped = [];
637
+ for (const s of _mcpServersV2.values()) {
638
+ if (s.status !== _MS_V2.ACTIVE) continue;
639
+ if (t - s.lastSeenAt > _mcpConfigV2.serverIdleMs) {
640
+ s.status = _MS_V2.DEGRADED;
641
+ s.lastSeenAt = t;
642
+ flipped.push(_copyServerV2(s));
643
+ }
644
+ }
645
+ return flipped;
646
+ }
647
+
648
+ export function autoFailStuckInvocationsV2({ now } = {}) {
649
+ const t = typeof now === "number" ? now : Date.now();
650
+ const flipped = [];
651
+ for (const i of _mcpInvocationsV2.values()) {
652
+ if (i.status !== _MI_V2.DISPATCHING) continue;
653
+ if (i.startedAt && t - i.startedAt > _mcpConfigV2.invocationStuckMs) {
654
+ i.status = _MI_V2.FAILED;
655
+ i.settledAt = t;
656
+ i.metadata.error = "stuck-timeout";
657
+ flipped.push(_copyInvocationV2(i));
658
+ }
659
+ }
660
+ return flipped;
661
+ }
662
+
663
+ export function getMcpRegistryStatsV2() {
664
+ const serversByStatus = {};
665
+ for (const s of Object.values(_MS_V2)) serversByStatus[s] = 0;
666
+ for (const s of _mcpServersV2.values()) serversByStatus[s.status]++;
667
+ const invocationsByStatus = {};
668
+ for (const s of Object.values(_MI_V2)) invocationsByStatus[s] = 0;
669
+ for (const i of _mcpInvocationsV2.values()) invocationsByStatus[i.status]++;
670
+ return {
671
+ totalServersV2: _mcpServersV2.size,
672
+ totalInvocationsV2: _mcpInvocationsV2.size,
673
+ maxActiveServersPerOwner: _mcpConfigV2.maxActiveServersPerOwner,
674
+ maxPendingInvocationsPerServer: _mcpConfigV2.maxPendingInvocationsPerServer,
675
+ serverIdleMs: _mcpConfigV2.serverIdleMs,
676
+ invocationStuckMs: _mcpConfigV2.invocationStuckMs,
677
+ serversByStatus,
678
+ invocationsByStatus,
679
+ };
680
+ }
@@ -383,3 +383,84 @@ function renderGitignore() {
383
383
  !.env.example
384
384
  `;
385
385
  }
386
+
387
+ // ===== V2 Surface: MCP Scaffold governance overlay (CLI v0.142.0) =====
388
+ export const MSCAF_PROFILE_MATURITY_V2 = Object.freeze({
389
+ PENDING: "pending", ACTIVE: "active", STALE: "stale", ARCHIVED: "archived",
390
+ });
391
+ export const MSCAF_GENERATION_LIFECYCLE_V2 = Object.freeze({
392
+ QUEUED: "queued", GENERATING: "generating", GENERATED: "generated", FAILED: "failed", CANCELLED: "cancelled",
393
+ });
394
+ const _mscafPTrans = new Map([
395
+ [MSCAF_PROFILE_MATURITY_V2.PENDING, new Set([MSCAF_PROFILE_MATURITY_V2.ACTIVE, MSCAF_PROFILE_MATURITY_V2.ARCHIVED])],
396
+ [MSCAF_PROFILE_MATURITY_V2.ACTIVE, new Set([MSCAF_PROFILE_MATURITY_V2.STALE, MSCAF_PROFILE_MATURITY_V2.ARCHIVED])],
397
+ [MSCAF_PROFILE_MATURITY_V2.STALE, new Set([MSCAF_PROFILE_MATURITY_V2.ACTIVE, MSCAF_PROFILE_MATURITY_V2.ARCHIVED])],
398
+ [MSCAF_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
399
+ ]);
400
+ const _mscafPTerminal = new Set([MSCAF_PROFILE_MATURITY_V2.ARCHIVED]);
401
+ const _mscafGTrans = new Map([
402
+ [MSCAF_GENERATION_LIFECYCLE_V2.QUEUED, new Set([MSCAF_GENERATION_LIFECYCLE_V2.GENERATING, MSCAF_GENERATION_LIFECYCLE_V2.CANCELLED])],
403
+ [MSCAF_GENERATION_LIFECYCLE_V2.GENERATING, new Set([MSCAF_GENERATION_LIFECYCLE_V2.GENERATED, MSCAF_GENERATION_LIFECYCLE_V2.FAILED, MSCAF_GENERATION_LIFECYCLE_V2.CANCELLED])],
404
+ [MSCAF_GENERATION_LIFECYCLE_V2.GENERATED, new Set()],
405
+ [MSCAF_GENERATION_LIFECYCLE_V2.FAILED, new Set()],
406
+ [MSCAF_GENERATION_LIFECYCLE_V2.CANCELLED, new Set()],
407
+ ]);
408
+ const _mscafPsV2 = new Map();
409
+ const _mscafGsV2 = new Map();
410
+ let _mscafMaxActive = 6, _mscafMaxPending = 15, _mscafIdleMs = 30 * 24 * 60 * 60 * 1000, _mscafStuckMs = 60 * 1000;
411
+ function _mscafPos(n, label) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${label} must be positive integer`); return v; }
412
+ function _mscafCheckP(from, to) { const a = _mscafPTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid mscaf profile transition ${from} → ${to}`); }
413
+ function _mscafCheckG(from, to) { const a = _mscafGTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid mscaf generation transition ${from} → ${to}`); }
414
+ export function setMaxActiveMscafProfilesPerOwnerV2(n) { _mscafMaxActive = _mscafPos(n, "maxActiveMscafProfilesPerOwner"); }
415
+ export function getMaxActiveMscafProfilesPerOwnerV2() { return _mscafMaxActive; }
416
+ export function setMaxPendingMscafGenerationsPerProfileV2(n) { _mscafMaxPending = _mscafPos(n, "maxPendingMscafGenerationsPerProfile"); }
417
+ export function getMaxPendingMscafGenerationsPerProfileV2() { return _mscafMaxPending; }
418
+ export function setMscafProfileIdleMsV2(n) { _mscafIdleMs = _mscafPos(n, "mscafProfileIdleMs"); }
419
+ export function getMscafProfileIdleMsV2() { return _mscafIdleMs; }
420
+ export function setMscafGenerationStuckMsV2(n) { _mscafStuckMs = _mscafPos(n, "mscafGenerationStuckMs"); }
421
+ export function getMscafGenerationStuckMsV2() { return _mscafStuckMs; }
422
+ export function _resetStateMcpScaffoldV2() { _mscafPsV2.clear(); _mscafGsV2.clear(); _mscafMaxActive = 6; _mscafMaxPending = 15; _mscafIdleMs = 30 * 24 * 60 * 60 * 1000; _mscafStuckMs = 60 * 1000; }
423
+ export function registerMscafProfileV2({ id, owner, transport, metadata } = {}) {
424
+ if (!id) throw new Error("mscaf profile id required"); if (!owner) throw new Error("mscaf profile owner required");
425
+ if (_mscafPsV2.has(id)) throw new Error(`mscaf profile ${id} already registered`);
426
+ const now = Date.now();
427
+ const p = { id, owner, transport: transport || "stdio", status: MSCAF_PROFILE_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, archivedAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
428
+ _mscafPsV2.set(id, p); return { ...p, metadata: { ...p.metadata } };
429
+ }
430
+ function _mscafCountActive(owner) { let n = 0; for (const p of _mscafPsV2.values()) if (p.owner === owner && p.status === MSCAF_PROFILE_MATURITY_V2.ACTIVE) n++; return n; }
431
+ export function activateMscafProfileV2(id) {
432
+ const p = _mscafPsV2.get(id); if (!p) throw new Error(`mscaf profile ${id} not found`);
433
+ _mscafCheckP(p.status, MSCAF_PROFILE_MATURITY_V2.ACTIVE);
434
+ const recovery = p.status === MSCAF_PROFILE_MATURITY_V2.STALE;
435
+ if (!recovery && _mscafCountActive(p.owner) >= _mscafMaxActive) throw new Error(`max active mscaf profiles for owner ${p.owner} reached`);
436
+ const now = Date.now(); p.status = MSCAF_PROFILE_MATURITY_V2.ACTIVE; p.updatedAt = now; p.lastTouchedAt = now; if (!p.activatedAt) p.activatedAt = now;
437
+ return { ...p, metadata: { ...p.metadata } };
438
+ }
439
+ export function staleMscafProfileV2(id) { const p = _mscafPsV2.get(id); if (!p) throw new Error(`mscaf profile ${id} not found`); _mscafCheckP(p.status, MSCAF_PROFILE_MATURITY_V2.STALE); p.status = MSCAF_PROFILE_MATURITY_V2.STALE; p.updatedAt = Date.now(); return { ...p, metadata: { ...p.metadata } }; }
440
+ export function archiveMscafProfileV2(id) { const p = _mscafPsV2.get(id); if (!p) throw new Error(`mscaf profile ${id} not found`); _mscafCheckP(p.status, MSCAF_PROFILE_MATURITY_V2.ARCHIVED); const now = Date.now(); p.status = MSCAF_PROFILE_MATURITY_V2.ARCHIVED; p.updatedAt = now; if (!p.archivedAt) p.archivedAt = now; return { ...p, metadata: { ...p.metadata } }; }
441
+ export function touchMscafProfileV2(id) { const p = _mscafPsV2.get(id); if (!p) throw new Error(`mscaf profile ${id} not found`); if (_mscafPTerminal.has(p.status)) throw new Error(`cannot touch terminal mscaf profile ${id}`); const now = Date.now(); p.lastTouchedAt = now; p.updatedAt = now; return { ...p, metadata: { ...p.metadata } }; }
442
+ export function getMscafProfileV2(id) { const p = _mscafPsV2.get(id); if (!p) return null; return { ...p, metadata: { ...p.metadata } }; }
443
+ export function listMscafProfilesV2() { return [..._mscafPsV2.values()].map((p) => ({ ...p, metadata: { ...p.metadata } })); }
444
+ function _mscafCountPending(profileId) { let n = 0; for (const g of _mscafGsV2.values()) if (g.profileId === profileId && (g.status === MSCAF_GENERATION_LIFECYCLE_V2.QUEUED || g.status === MSCAF_GENERATION_LIFECYCLE_V2.GENERATING)) n++; return n; }
445
+ export function createMscafGenerationV2({ id, profileId, target, metadata } = {}) {
446
+ if (!id) throw new Error("mscaf generation id required"); if (!profileId) throw new Error("mscaf generation profileId required");
447
+ if (_mscafGsV2.has(id)) throw new Error(`mscaf generation ${id} already exists`);
448
+ if (!_mscafPsV2.has(profileId)) throw new Error(`mscaf profile ${profileId} not found`);
449
+ if (_mscafCountPending(profileId) >= _mscafMaxPending) throw new Error(`max pending mscaf generations for profile ${profileId} reached`);
450
+ const now = Date.now();
451
+ const g = { id, profileId, target: target || "", status: MSCAF_GENERATION_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
452
+ _mscafGsV2.set(id, g); return { ...g, metadata: { ...g.metadata } };
453
+ }
454
+ export function generatingMscafGenerationV2(id) { const g = _mscafGsV2.get(id); if (!g) throw new Error(`mscaf generation ${id} not found`); _mscafCheckG(g.status, MSCAF_GENERATION_LIFECYCLE_V2.GENERATING); const now = Date.now(); g.status = MSCAF_GENERATION_LIFECYCLE_V2.GENERATING; g.updatedAt = now; if (!g.startedAt) g.startedAt = now; return { ...g, metadata: { ...g.metadata } }; }
455
+ export function generateMscafGenerationV2(id) { const g = _mscafGsV2.get(id); if (!g) throw new Error(`mscaf generation ${id} not found`); _mscafCheckG(g.status, MSCAF_GENERATION_LIFECYCLE_V2.GENERATED); const now = Date.now(); g.status = MSCAF_GENERATION_LIFECYCLE_V2.GENERATED; g.updatedAt = now; if (!g.settledAt) g.settledAt = now; return { ...g, metadata: { ...g.metadata } }; }
456
+ export function failMscafGenerationV2(id, reason) { const g = _mscafGsV2.get(id); if (!g) throw new Error(`mscaf generation ${id} not found`); _mscafCheckG(g.status, MSCAF_GENERATION_LIFECYCLE_V2.FAILED); const now = Date.now(); g.status = MSCAF_GENERATION_LIFECYCLE_V2.FAILED; g.updatedAt = now; if (!g.settledAt) g.settledAt = now; if (reason) g.metadata.failReason = String(reason); return { ...g, metadata: { ...g.metadata } }; }
457
+ export function cancelMscafGenerationV2(id, reason) { const g = _mscafGsV2.get(id); if (!g) throw new Error(`mscaf generation ${id} not found`); _mscafCheckG(g.status, MSCAF_GENERATION_LIFECYCLE_V2.CANCELLED); const now = Date.now(); g.status = MSCAF_GENERATION_LIFECYCLE_V2.CANCELLED; g.updatedAt = now; if (!g.settledAt) g.settledAt = now; if (reason) g.metadata.cancelReason = String(reason); return { ...g, metadata: { ...g.metadata } }; }
458
+ export function getMscafGenerationV2(id) { const g = _mscafGsV2.get(id); if (!g) return null; return { ...g, metadata: { ...g.metadata } }; }
459
+ export function listMscafGenerationsV2() { return [..._mscafGsV2.values()].map((g) => ({ ...g, metadata: { ...g.metadata } })); }
460
+ export function autoStaleIdleMscafProfilesV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const p of _mscafPsV2.values()) if (p.status === MSCAF_PROFILE_MATURITY_V2.ACTIVE && (t - p.lastTouchedAt) >= _mscafIdleMs) { p.status = MSCAF_PROFILE_MATURITY_V2.STALE; p.updatedAt = t; flipped.push(p.id); } return { flipped, count: flipped.length }; }
461
+ export function autoFailStuckMscafGenerationsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const g of _mscafGsV2.values()) if (g.status === MSCAF_GENERATION_LIFECYCLE_V2.GENERATING && g.startedAt != null && (t - g.startedAt) >= _mscafStuckMs) { g.status = MSCAF_GENERATION_LIFECYCLE_V2.FAILED; g.updatedAt = t; if (!g.settledAt) g.settledAt = t; g.metadata.failReason = "auto-fail-stuck"; flipped.push(g.id); } return { flipped, count: flipped.length }; }
462
+ export function getMcpScaffoldGovStatsV2() {
463
+ const profilesByStatus = {}; for (const v of Object.values(MSCAF_PROFILE_MATURITY_V2)) profilesByStatus[v] = 0; for (const p of _mscafPsV2.values()) profilesByStatus[p.status]++;
464
+ const generationsByStatus = {}; for (const v of Object.values(MSCAF_GENERATION_LIFECYCLE_V2)) generationsByStatus[v] = 0; for (const g of _mscafGsV2.values()) generationsByStatus[g.status]++;
465
+ return { totalMscafProfilesV2: _mscafPsV2.size, totalMscafGenerationsV2: _mscafGsV2.size, maxActiveMscafProfilesPerOwner: _mscafMaxActive, maxPendingMscafGenerationsPerProfile: _mscafMaxPending, mscafProfileIdleMs: _mscafIdleMs, mscafGenerationStuckMs: _mscafStuckMs, profilesByStatus, generationsByStatus };
466
+ }
@@ -88,3 +88,84 @@ export function buildMemoryInjection(options = {}) {
88
88
  });
89
89
  return content ? { role: "system", content, count: memories.length } : null;
90
90
  }
91
+
92
+ // ===== V2 Surface: Memory Injection governance overlay (CLI v0.142.0) =====
93
+ export const MINJ_RULE_MATURITY_V2 = Object.freeze({
94
+ PENDING: "pending", ACTIVE: "active", PAUSED: "paused", ARCHIVED: "archived",
95
+ });
96
+ export const MINJ_INJECTION_LIFECYCLE_V2 = Object.freeze({
97
+ QUEUED: "queued", INJECTING: "injecting", APPLIED: "applied", FAILED: "failed", CANCELLED: "cancelled",
98
+ });
99
+ const _minjRTrans = new Map([
100
+ [MINJ_RULE_MATURITY_V2.PENDING, new Set([MINJ_RULE_MATURITY_V2.ACTIVE, MINJ_RULE_MATURITY_V2.ARCHIVED])],
101
+ [MINJ_RULE_MATURITY_V2.ACTIVE, new Set([MINJ_RULE_MATURITY_V2.PAUSED, MINJ_RULE_MATURITY_V2.ARCHIVED])],
102
+ [MINJ_RULE_MATURITY_V2.PAUSED, new Set([MINJ_RULE_MATURITY_V2.ACTIVE, MINJ_RULE_MATURITY_V2.ARCHIVED])],
103
+ [MINJ_RULE_MATURITY_V2.ARCHIVED, new Set()],
104
+ ]);
105
+ const _minjRTerminal = new Set([MINJ_RULE_MATURITY_V2.ARCHIVED]);
106
+ const _minjITrans = new Map([
107
+ [MINJ_INJECTION_LIFECYCLE_V2.QUEUED, new Set([MINJ_INJECTION_LIFECYCLE_V2.INJECTING, MINJ_INJECTION_LIFECYCLE_V2.CANCELLED])],
108
+ [MINJ_INJECTION_LIFECYCLE_V2.INJECTING, new Set([MINJ_INJECTION_LIFECYCLE_V2.APPLIED, MINJ_INJECTION_LIFECYCLE_V2.FAILED, MINJ_INJECTION_LIFECYCLE_V2.CANCELLED])],
109
+ [MINJ_INJECTION_LIFECYCLE_V2.APPLIED, new Set()],
110
+ [MINJ_INJECTION_LIFECYCLE_V2.FAILED, new Set()],
111
+ [MINJ_INJECTION_LIFECYCLE_V2.CANCELLED, new Set()],
112
+ ]);
113
+ const _minjRsV2 = new Map();
114
+ const _minjIsV2 = new Map();
115
+ let _minjMaxActive = 10, _minjMaxPending = 25, _minjIdleMs = 30 * 24 * 60 * 60 * 1000, _minjStuckMs = 30 * 1000;
116
+ function _minjPos(n, label) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${label} must be positive integer`); return v; }
117
+ function _minjCheckR(from, to) { const a = _minjRTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid minj rule transition ${from} → ${to}`); }
118
+ function _minjCheckI(from, to) { const a = _minjITrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid minj injection transition ${from} → ${to}`); }
119
+ export function setMaxActiveMinjRulesPerOwnerV2(n) { _minjMaxActive = _minjPos(n, "maxActiveMinjRulesPerOwner"); }
120
+ export function getMaxActiveMinjRulesPerOwnerV2() { return _minjMaxActive; }
121
+ export function setMaxPendingMinjInjectionsPerRuleV2(n) { _minjMaxPending = _minjPos(n, "maxPendingMinjInjectionsPerRule"); }
122
+ export function getMaxPendingMinjInjectionsPerRuleV2() { return _minjMaxPending; }
123
+ export function setMinjRuleIdleMsV2(n) { _minjIdleMs = _minjPos(n, "minjRuleIdleMs"); }
124
+ export function getMinjRuleIdleMsV2() { return _minjIdleMs; }
125
+ export function setMinjInjectionStuckMsV2(n) { _minjStuckMs = _minjPos(n, "minjInjectionStuckMs"); }
126
+ export function getMinjInjectionStuckMsV2() { return _minjStuckMs; }
127
+ export function _resetStateMemoryInjectionV2() { _minjRsV2.clear(); _minjIsV2.clear(); _minjMaxActive = 10; _minjMaxPending = 25; _minjIdleMs = 30 * 24 * 60 * 60 * 1000; _minjStuckMs = 30 * 1000; }
128
+ export function registerMinjRuleV2({ id, owner, scope, metadata } = {}) {
129
+ if (!id) throw new Error("minj rule id required"); if (!owner) throw new Error("minj rule owner required");
130
+ if (_minjRsV2.has(id)) throw new Error(`minj rule ${id} already registered`);
131
+ const now = Date.now();
132
+ const r = { id, owner, scope: scope || "*", status: MINJ_RULE_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, archivedAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
133
+ _minjRsV2.set(id, r); return { ...r, metadata: { ...r.metadata } };
134
+ }
135
+ function _minjCountActive(owner) { let n = 0; for (const r of _minjRsV2.values()) if (r.owner === owner && r.status === MINJ_RULE_MATURITY_V2.ACTIVE) n++; return n; }
136
+ export function activateMinjRuleV2(id) {
137
+ const r = _minjRsV2.get(id); if (!r) throw new Error(`minj rule ${id} not found`);
138
+ _minjCheckR(r.status, MINJ_RULE_MATURITY_V2.ACTIVE);
139
+ const recovery = r.status === MINJ_RULE_MATURITY_V2.PAUSED;
140
+ if (!recovery && _minjCountActive(r.owner) >= _minjMaxActive) throw new Error(`max active minj rules for owner ${r.owner} reached`);
141
+ const now = Date.now(); r.status = MINJ_RULE_MATURITY_V2.ACTIVE; r.updatedAt = now; r.lastTouchedAt = now; if (!r.activatedAt) r.activatedAt = now;
142
+ return { ...r, metadata: { ...r.metadata } };
143
+ }
144
+ export function pauseMinjRuleV2(id) { const r = _minjRsV2.get(id); if (!r) throw new Error(`minj rule ${id} not found`); _minjCheckR(r.status, MINJ_RULE_MATURITY_V2.PAUSED); r.status = MINJ_RULE_MATURITY_V2.PAUSED; r.updatedAt = Date.now(); return { ...r, metadata: { ...r.metadata } }; }
145
+ export function archiveMinjRuleV2(id) { const r = _minjRsV2.get(id); if (!r) throw new Error(`minj rule ${id} not found`); _minjCheckR(r.status, MINJ_RULE_MATURITY_V2.ARCHIVED); const now = Date.now(); r.status = MINJ_RULE_MATURITY_V2.ARCHIVED; r.updatedAt = now; if (!r.archivedAt) r.archivedAt = now; return { ...r, metadata: { ...r.metadata } }; }
146
+ export function touchMinjRuleV2(id) { const r = _minjRsV2.get(id); if (!r) throw new Error(`minj rule ${id} not found`); if (_minjRTerminal.has(r.status)) throw new Error(`cannot touch terminal minj rule ${id}`); const now = Date.now(); r.lastTouchedAt = now; r.updatedAt = now; return { ...r, metadata: { ...r.metadata } }; }
147
+ export function getMinjRuleV2(id) { const r = _minjRsV2.get(id); if (!r) return null; return { ...r, metadata: { ...r.metadata } }; }
148
+ export function listMinjRulesV2() { return [..._minjRsV2.values()].map((r) => ({ ...r, metadata: { ...r.metadata } })); }
149
+ function _minjCountPending(ruleId) { let n = 0; for (const i of _minjIsV2.values()) if (i.ruleId === ruleId && (i.status === MINJ_INJECTION_LIFECYCLE_V2.QUEUED || i.status === MINJ_INJECTION_LIFECYCLE_V2.INJECTING)) n++; return n; }
150
+ export function createMinjInjectionV2({ id, ruleId, payload, metadata } = {}) {
151
+ if (!id) throw new Error("minj injection id required"); if (!ruleId) throw new Error("minj injection ruleId required");
152
+ if (_minjIsV2.has(id)) throw new Error(`minj injection ${id} already exists`);
153
+ if (!_minjRsV2.has(ruleId)) throw new Error(`minj rule ${ruleId} not found`);
154
+ if (_minjCountPending(ruleId) >= _minjMaxPending) throw new Error(`max pending minj injections for rule ${ruleId} reached`);
155
+ const now = Date.now();
156
+ const i = { id, ruleId, payload: payload || "", status: MINJ_INJECTION_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
157
+ _minjIsV2.set(id, i); return { ...i, metadata: { ...i.metadata } };
158
+ }
159
+ export function injectingMinjInjectionV2(id) { const i = _minjIsV2.get(id); if (!i) throw new Error(`minj injection ${id} not found`); _minjCheckI(i.status, MINJ_INJECTION_LIFECYCLE_V2.INJECTING); const now = Date.now(); i.status = MINJ_INJECTION_LIFECYCLE_V2.INJECTING; i.updatedAt = now; if (!i.startedAt) i.startedAt = now; return { ...i, metadata: { ...i.metadata } }; }
160
+ export function applyMinjInjectionV2(id) { const i = _minjIsV2.get(id); if (!i) throw new Error(`minj injection ${id} not found`); _minjCheckI(i.status, MINJ_INJECTION_LIFECYCLE_V2.APPLIED); const now = Date.now(); i.status = MINJ_INJECTION_LIFECYCLE_V2.APPLIED; i.updatedAt = now; if (!i.settledAt) i.settledAt = now; return { ...i, metadata: { ...i.metadata } }; }
161
+ export function failMinjInjectionV2(id, reason) { const i = _minjIsV2.get(id); if (!i) throw new Error(`minj injection ${id} not found`); _minjCheckI(i.status, MINJ_INJECTION_LIFECYCLE_V2.FAILED); const now = Date.now(); i.status = MINJ_INJECTION_LIFECYCLE_V2.FAILED; i.updatedAt = now; if (!i.settledAt) i.settledAt = now; if (reason) i.metadata.failReason = String(reason); return { ...i, metadata: { ...i.metadata } }; }
162
+ export function cancelMinjInjectionV2(id, reason) { const i = _minjIsV2.get(id); if (!i) throw new Error(`minj injection ${id} not found`); _minjCheckI(i.status, MINJ_INJECTION_LIFECYCLE_V2.CANCELLED); const now = Date.now(); i.status = MINJ_INJECTION_LIFECYCLE_V2.CANCELLED; i.updatedAt = now; if (!i.settledAt) i.settledAt = now; if (reason) i.metadata.cancelReason = String(reason); return { ...i, metadata: { ...i.metadata } }; }
163
+ export function getMinjInjectionV2(id) { const i = _minjIsV2.get(id); if (!i) return null; return { ...i, metadata: { ...i.metadata } }; }
164
+ export function listMinjInjectionsV2() { return [..._minjIsV2.values()].map((i) => ({ ...i, metadata: { ...i.metadata } })); }
165
+ export function autoPauseIdleMinjRulesV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const r of _minjRsV2.values()) if (r.status === MINJ_RULE_MATURITY_V2.ACTIVE && (t - r.lastTouchedAt) >= _minjIdleMs) { r.status = MINJ_RULE_MATURITY_V2.PAUSED; r.updatedAt = t; flipped.push(r.id); } return { flipped, count: flipped.length }; }
166
+ export function autoFailStuckMinjInjectionsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const i of _minjIsV2.values()) if (i.status === MINJ_INJECTION_LIFECYCLE_V2.INJECTING && i.startedAt != null && (t - i.startedAt) >= _minjStuckMs) { i.status = MINJ_INJECTION_LIFECYCLE_V2.FAILED; i.updatedAt = t; if (!i.settledAt) i.settledAt = t; i.metadata.failReason = "auto-fail-stuck"; flipped.push(i.id); } return { flipped, count: flipped.length }; }
167
+ export function getMemoryInjectionGovStatsV2() {
168
+ const rulesByStatus = {}; for (const v of Object.values(MINJ_RULE_MATURITY_V2)) rulesByStatus[v] = 0; for (const r of _minjRsV2.values()) rulesByStatus[r.status]++;
169
+ const injectionsByStatus = {}; for (const v of Object.values(MINJ_INJECTION_LIFECYCLE_V2)) injectionsByStatus[v] = 0; for (const i of _minjIsV2.values()) injectionsByStatus[i.status]++;
170
+ return { totalMinjRulesV2: _minjRsV2.size, totalMinjInjectionsV2: _minjIsV2.size, maxActiveMinjRulesPerOwner: _minjMaxActive, maxPendingMinjInjectionsPerRule: _minjMaxPending, minjRuleIdleMs: _minjIdleMs, minjInjectionStuckMs: _minjStuckMs, rulesByStatus, injectionsByStatus };
171
+ }