chainlesschain 0.152.0 → 0.156.2

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 (80) hide show
  1. package/README.md +52 -3
  2. package/package.json +1 -1
  3. package/src/commands/a2a.js +201 -0
  4. package/src/commands/agent.js +1250 -0
  5. package/src/commands/chat.js +605 -0
  6. package/src/commands/cli-anything.js +426 -0
  7. package/src/commands/compliance.js +412 -0
  8. package/src/commands/config.js +213 -0
  9. package/src/commands/cowork.js +1463 -0
  10. package/src/commands/crosschain.js +203 -0
  11. package/src/commands/dao.js +203 -0
  12. package/src/commands/economy.js +199 -0
  13. package/src/commands/encrypt.js +201 -0
  14. package/src/commands/evolution.js +199 -0
  15. package/src/commands/evomap.js +625 -0
  16. package/src/commands/hmemory.js +203 -0
  17. package/src/commands/inference.js +207 -0
  18. package/src/commands/kg.js +195 -0
  19. package/src/commands/llm.js +209 -0
  20. package/src/commands/memory.js +203 -0
  21. package/src/commands/orchestrate.js +406 -0
  22. package/src/commands/pipeline.js +199 -0
  23. package/src/commands/planmode.js +426 -0
  24. package/src/commands/plugin.js +209 -0
  25. package/src/commands/services.js +207 -0
  26. package/src/commands/setup.js +205 -0
  27. package/src/commands/skill.js +207 -0
  28. package/src/commands/start.js +209 -0
  29. package/src/commands/stream.js +213 -0
  30. package/src/commands/ui.js +225 -0
  31. package/src/commands/workflow.js +209 -0
  32. package/src/index.js +112 -0
  33. package/src/lib/a2a-protocol.js +332 -0
  34. package/src/lib/agent-coordinator.js +334 -0
  35. package/src/lib/agent-economy.js +334 -0
  36. package/src/lib/agent-router.js +333 -0
  37. package/src/lib/autonomous-agent.js +332 -0
  38. package/src/lib/chat-core.js +335 -0
  39. package/src/lib/cli-anything-bridge.js +341 -0
  40. package/src/lib/cli-context-engineering.js +351 -0
  41. package/src/lib/compliance-manager.js +334 -0
  42. package/src/lib/cowork-adapter.js +336 -0
  43. package/src/lib/cowork-evomap-adapter.js +341 -0
  44. package/src/lib/cowork-mcp-tools.js +341 -0
  45. package/src/lib/cowork-observe-html.js +341 -0
  46. package/src/lib/cowork-observe.js +341 -0
  47. package/src/lib/cowork-task-templates.js +342 -1
  48. package/src/lib/cowork-template-marketplace.js +340 -0
  49. package/src/lib/cross-chain.js +339 -0
  50. package/src/lib/crypto-manager.js +334 -0
  51. package/src/lib/dao-governance.js +339 -0
  52. package/src/lib/downloader.js +334 -0
  53. package/src/lib/evolution-system.js +334 -0
  54. package/src/lib/evomap-client.js +342 -0
  55. package/src/lib/evomap-federation.js +338 -0
  56. package/src/lib/evomap-manager.js +330 -0
  57. package/src/lib/execution-backend.js +330 -0
  58. package/src/lib/hashline.js +338 -0
  59. package/src/lib/hierarchical-memory.js +334 -0
  60. package/src/lib/inference-network.js +341 -0
  61. package/src/lib/interaction-adapter.js +330 -0
  62. package/src/lib/interactive-planner.js +354 -0
  63. package/src/lib/knowledge-graph.js +331 -0
  64. package/src/lib/pipeline-orchestrator.js +332 -0
  65. package/src/lib/plan-mode.js +336 -0
  66. package/src/lib/plugin-autodiscovery.js +334 -0
  67. package/src/lib/process-manager.js +336 -0
  68. package/src/lib/provider-options.js +346 -0
  69. package/src/lib/provider-stream.js +348 -0
  70. package/src/lib/service-manager.js +337 -0
  71. package/src/lib/session-core-singletons.js +341 -0
  72. package/src/lib/skill-mcp.js +336 -0
  73. package/src/lib/stix-parser.js +346 -0
  74. package/src/lib/sub-agent-context.js +343 -0
  75. package/src/lib/sub-agent-profiles.js +335 -0
  76. package/src/lib/sub-agent-registry.js +336 -0
  77. package/src/lib/todo-manager.js +336 -0
  78. package/src/lib/web-ui-server.js +348 -0
  79. package/src/lib/workflow-expr.js +346 -0
  80. package/src/lib/ws-chat-handler.js +337 -0
@@ -222,3 +222,337 @@ export function extractPluginCommands(plugins) {
222
222
  }
223
223
  return commands;
224
224
  }
225
+
226
+ // =====================================================================
227
+ // plugin-autodiscovery V2 governance overlay (iter26)
228
+ // =====================================================================
229
+ export const PADGOV_PROFILE_MATURITY_V2 = Object.freeze({
230
+ PENDING: "pending",
231
+ ACTIVE: "active",
232
+ STALE: "stale",
233
+ ARCHIVED: "archived",
234
+ });
235
+ export const PADGOV_SCAN_LIFECYCLE_V2 = Object.freeze({
236
+ QUEUED: "queued",
237
+ SCANNING: "scanning",
238
+ SCANNED: "scanned",
239
+ FAILED: "failed",
240
+ CANCELLED: "cancelled",
241
+ });
242
+ const _padgovPTrans = new Map([
243
+ [
244
+ PADGOV_PROFILE_MATURITY_V2.PENDING,
245
+ new Set([
246
+ PADGOV_PROFILE_MATURITY_V2.ACTIVE,
247
+ PADGOV_PROFILE_MATURITY_V2.ARCHIVED,
248
+ ]),
249
+ ],
250
+ [
251
+ PADGOV_PROFILE_MATURITY_V2.ACTIVE,
252
+ new Set([
253
+ PADGOV_PROFILE_MATURITY_V2.STALE,
254
+ PADGOV_PROFILE_MATURITY_V2.ARCHIVED,
255
+ ]),
256
+ ],
257
+ [
258
+ PADGOV_PROFILE_MATURITY_V2.STALE,
259
+ new Set([
260
+ PADGOV_PROFILE_MATURITY_V2.ACTIVE,
261
+ PADGOV_PROFILE_MATURITY_V2.ARCHIVED,
262
+ ]),
263
+ ],
264
+ [PADGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
265
+ ]);
266
+ const _padgovPTerminal = new Set([PADGOV_PROFILE_MATURITY_V2.ARCHIVED]);
267
+ const _padgovJTrans = new Map([
268
+ [
269
+ PADGOV_SCAN_LIFECYCLE_V2.QUEUED,
270
+ new Set([
271
+ PADGOV_SCAN_LIFECYCLE_V2.SCANNING,
272
+ PADGOV_SCAN_LIFECYCLE_V2.CANCELLED,
273
+ ]),
274
+ ],
275
+ [
276
+ PADGOV_SCAN_LIFECYCLE_V2.SCANNING,
277
+ new Set([
278
+ PADGOV_SCAN_LIFECYCLE_V2.SCANNED,
279
+ PADGOV_SCAN_LIFECYCLE_V2.FAILED,
280
+ PADGOV_SCAN_LIFECYCLE_V2.CANCELLED,
281
+ ]),
282
+ ],
283
+ [PADGOV_SCAN_LIFECYCLE_V2.SCANNED, new Set()],
284
+ [PADGOV_SCAN_LIFECYCLE_V2.FAILED, new Set()],
285
+ [PADGOV_SCAN_LIFECYCLE_V2.CANCELLED, new Set()],
286
+ ]);
287
+ const _padgovPsV2 = new Map();
288
+ const _padgovJsV2 = new Map();
289
+ let _padgovMaxActive = 6,
290
+ _padgovMaxPending = 15,
291
+ _padgovIdleMs = 30 * 24 * 60 * 60 * 1000,
292
+ _padgovStuckMs = 60 * 1000;
293
+ function _padgovPos(n, label) {
294
+ const v = Math.floor(Number(n));
295
+ if (!Number.isFinite(v) || v <= 0)
296
+ throw new Error(`${label} must be positive integer`);
297
+ return v;
298
+ }
299
+ function _padgovCheckP(from, to) {
300
+ const a = _padgovPTrans.get(from);
301
+ if (!a || !a.has(to))
302
+ throw new Error(`invalid padgov profile transition ${from} → ${to}`);
303
+ }
304
+ function _padgovCheckJ(from, to) {
305
+ const a = _padgovJTrans.get(from);
306
+ if (!a || !a.has(to))
307
+ throw new Error(`invalid padgov scan transition ${from} → ${to}`);
308
+ }
309
+ function _padgovCountActive(owner) {
310
+ let c = 0;
311
+ for (const p of _padgovPsV2.values())
312
+ if (p.owner === owner && p.status === PADGOV_PROFILE_MATURITY_V2.ACTIVE)
313
+ c++;
314
+ return c;
315
+ }
316
+ function _padgovCountPending(profileId) {
317
+ let c = 0;
318
+ for (const j of _padgovJsV2.values())
319
+ if (
320
+ j.profileId === profileId &&
321
+ (j.status === PADGOV_SCAN_LIFECYCLE_V2.QUEUED ||
322
+ j.status === PADGOV_SCAN_LIFECYCLE_V2.SCANNING)
323
+ )
324
+ c++;
325
+ return c;
326
+ }
327
+ export function setMaxActivePadgovProfilesPerOwnerV2(n) {
328
+ _padgovMaxActive = _padgovPos(n, "maxActivePadgovProfilesPerOwner");
329
+ }
330
+ export function getMaxActivePadgovProfilesPerOwnerV2() {
331
+ return _padgovMaxActive;
332
+ }
333
+ export function setMaxPendingPadgovScansPerProfileV2(n) {
334
+ _padgovMaxPending = _padgovPos(n, "maxPendingPadgovScansPerProfile");
335
+ }
336
+ export function getMaxPendingPadgovScansPerProfileV2() {
337
+ return _padgovMaxPending;
338
+ }
339
+ export function setPadgovProfileIdleMsV2(n) {
340
+ _padgovIdleMs = _padgovPos(n, "padgovProfileIdleMs");
341
+ }
342
+ export function getPadgovProfileIdleMsV2() {
343
+ return _padgovIdleMs;
344
+ }
345
+ export function setPadgovScanStuckMsV2(n) {
346
+ _padgovStuckMs = _padgovPos(n, "padgovScanStuckMs");
347
+ }
348
+ export function getPadgovScanStuckMsV2() {
349
+ return _padgovStuckMs;
350
+ }
351
+ export function _resetStatePluginAutodiscoveryGovV2() {
352
+ _padgovPsV2.clear();
353
+ _padgovJsV2.clear();
354
+ _padgovMaxActive = 6;
355
+ _padgovMaxPending = 15;
356
+ _padgovIdleMs = 30 * 24 * 60 * 60 * 1000;
357
+ _padgovStuckMs = 60 * 1000;
358
+ }
359
+ export function registerPadgovProfileV2({ id, owner, root, metadata } = {}) {
360
+ if (!id || !owner) throw new Error("id and owner required");
361
+ if (_padgovPsV2.has(id))
362
+ throw new Error(`padgov profile ${id} already exists`);
363
+ const now = Date.now();
364
+ const p = {
365
+ id,
366
+ owner,
367
+ root: root || ".chainlesschain",
368
+ status: PADGOV_PROFILE_MATURITY_V2.PENDING,
369
+ createdAt: now,
370
+ updatedAt: now,
371
+ lastTouchedAt: now,
372
+ activatedAt: null,
373
+ archivedAt: null,
374
+ metadata: { ...(metadata || {}) },
375
+ };
376
+ _padgovPsV2.set(id, p);
377
+ return { ...p, metadata: { ...p.metadata } };
378
+ }
379
+ export function activatePadgovProfileV2(id) {
380
+ const p = _padgovPsV2.get(id);
381
+ if (!p) throw new Error(`padgov profile ${id} not found`);
382
+ const isInitial = p.status === PADGOV_PROFILE_MATURITY_V2.PENDING;
383
+ _padgovCheckP(p.status, PADGOV_PROFILE_MATURITY_V2.ACTIVE);
384
+ if (isInitial && _padgovCountActive(p.owner) >= _padgovMaxActive)
385
+ throw new Error(`max active padgov profiles for owner ${p.owner} reached`);
386
+ const now = Date.now();
387
+ p.status = PADGOV_PROFILE_MATURITY_V2.ACTIVE;
388
+ p.updatedAt = now;
389
+ p.lastTouchedAt = now;
390
+ if (!p.activatedAt) p.activatedAt = now;
391
+ return { ...p, metadata: { ...p.metadata } };
392
+ }
393
+ export function stalePadgovProfileV2(id) {
394
+ const p = _padgovPsV2.get(id);
395
+ if (!p) throw new Error(`padgov profile ${id} not found`);
396
+ _padgovCheckP(p.status, PADGOV_PROFILE_MATURITY_V2.STALE);
397
+ p.status = PADGOV_PROFILE_MATURITY_V2.STALE;
398
+ p.updatedAt = Date.now();
399
+ return { ...p, metadata: { ...p.metadata } };
400
+ }
401
+ export function archivePadgovProfileV2(id) {
402
+ const p = _padgovPsV2.get(id);
403
+ if (!p) throw new Error(`padgov profile ${id} not found`);
404
+ _padgovCheckP(p.status, PADGOV_PROFILE_MATURITY_V2.ARCHIVED);
405
+ const now = Date.now();
406
+ p.status = PADGOV_PROFILE_MATURITY_V2.ARCHIVED;
407
+ p.updatedAt = now;
408
+ if (!p.archivedAt) p.archivedAt = now;
409
+ return { ...p, metadata: { ...p.metadata } };
410
+ }
411
+ export function touchPadgovProfileV2(id) {
412
+ const p = _padgovPsV2.get(id);
413
+ if (!p) throw new Error(`padgov profile ${id} not found`);
414
+ if (_padgovPTerminal.has(p.status))
415
+ throw new Error(`cannot touch terminal padgov profile ${id}`);
416
+ const now = Date.now();
417
+ p.lastTouchedAt = now;
418
+ p.updatedAt = now;
419
+ return { ...p, metadata: { ...p.metadata } };
420
+ }
421
+ export function getPadgovProfileV2(id) {
422
+ const p = _padgovPsV2.get(id);
423
+ if (!p) return null;
424
+ return { ...p, metadata: { ...p.metadata } };
425
+ }
426
+ export function listPadgovProfilesV2() {
427
+ return [..._padgovPsV2.values()].map((p) => ({
428
+ ...p,
429
+ metadata: { ...p.metadata },
430
+ }));
431
+ }
432
+ export function createPadgovScanV2({ id, profileId, path, metadata } = {}) {
433
+ if (!id || !profileId) throw new Error("id and profileId required");
434
+ if (_padgovJsV2.has(id)) throw new Error(`padgov scan ${id} already exists`);
435
+ if (!_padgovPsV2.has(profileId))
436
+ throw new Error(`padgov profile ${profileId} not found`);
437
+ if (_padgovCountPending(profileId) >= _padgovMaxPending)
438
+ throw new Error(
439
+ `max pending padgov scans for profile ${profileId} reached`,
440
+ );
441
+ const now = Date.now();
442
+ const j = {
443
+ id,
444
+ profileId,
445
+ path: path || "",
446
+ status: PADGOV_SCAN_LIFECYCLE_V2.QUEUED,
447
+ createdAt: now,
448
+ updatedAt: now,
449
+ startedAt: null,
450
+ settledAt: null,
451
+ metadata: { ...(metadata || {}) },
452
+ };
453
+ _padgovJsV2.set(id, j);
454
+ return { ...j, metadata: { ...j.metadata } };
455
+ }
456
+ export function scanningPadgovScanV2(id) {
457
+ const j = _padgovJsV2.get(id);
458
+ if (!j) throw new Error(`padgov scan ${id} not found`);
459
+ _padgovCheckJ(j.status, PADGOV_SCAN_LIFECYCLE_V2.SCANNING);
460
+ const now = Date.now();
461
+ j.status = PADGOV_SCAN_LIFECYCLE_V2.SCANNING;
462
+ j.updatedAt = now;
463
+ if (!j.startedAt) j.startedAt = now;
464
+ return { ...j, metadata: { ...j.metadata } };
465
+ }
466
+ export function completeScanPadgovV2(id) {
467
+ const j = _padgovJsV2.get(id);
468
+ if (!j) throw new Error(`padgov scan ${id} not found`);
469
+ _padgovCheckJ(j.status, PADGOV_SCAN_LIFECYCLE_V2.SCANNED);
470
+ const now = Date.now();
471
+ j.status = PADGOV_SCAN_LIFECYCLE_V2.SCANNED;
472
+ j.updatedAt = now;
473
+ if (!j.settledAt) j.settledAt = now;
474
+ return { ...j, metadata: { ...j.metadata } };
475
+ }
476
+ export function failPadgovScanV2(id, reason) {
477
+ const j = _padgovJsV2.get(id);
478
+ if (!j) throw new Error(`padgov scan ${id} not found`);
479
+ _padgovCheckJ(j.status, PADGOV_SCAN_LIFECYCLE_V2.FAILED);
480
+ const now = Date.now();
481
+ j.status = PADGOV_SCAN_LIFECYCLE_V2.FAILED;
482
+ j.updatedAt = now;
483
+ if (!j.settledAt) j.settledAt = now;
484
+ if (reason) j.metadata.failReason = String(reason);
485
+ return { ...j, metadata: { ...j.metadata } };
486
+ }
487
+ export function cancelPadgovScanV2(id, reason) {
488
+ const j = _padgovJsV2.get(id);
489
+ if (!j) throw new Error(`padgov scan ${id} not found`);
490
+ _padgovCheckJ(j.status, PADGOV_SCAN_LIFECYCLE_V2.CANCELLED);
491
+ const now = Date.now();
492
+ j.status = PADGOV_SCAN_LIFECYCLE_V2.CANCELLED;
493
+ j.updatedAt = now;
494
+ if (!j.settledAt) j.settledAt = now;
495
+ if (reason) j.metadata.cancelReason = String(reason);
496
+ return { ...j, metadata: { ...j.metadata } };
497
+ }
498
+ export function getPadgovScanV2(id) {
499
+ const j = _padgovJsV2.get(id);
500
+ if (!j) return null;
501
+ return { ...j, metadata: { ...j.metadata } };
502
+ }
503
+ export function listPadgovScansV2() {
504
+ return [..._padgovJsV2.values()].map((j) => ({
505
+ ...j,
506
+ metadata: { ...j.metadata },
507
+ }));
508
+ }
509
+ export function autoStaleIdlePadgovProfilesV2({ now } = {}) {
510
+ const t = now ?? Date.now();
511
+ const flipped = [];
512
+ for (const p of _padgovPsV2.values())
513
+ if (
514
+ p.status === PADGOV_PROFILE_MATURITY_V2.ACTIVE &&
515
+ t - p.lastTouchedAt >= _padgovIdleMs
516
+ ) {
517
+ p.status = PADGOV_PROFILE_MATURITY_V2.STALE;
518
+ p.updatedAt = t;
519
+ flipped.push(p.id);
520
+ }
521
+ return { flipped, count: flipped.length };
522
+ }
523
+ export function autoFailStuckPadgovScansV2({ now } = {}) {
524
+ const t = now ?? Date.now();
525
+ const flipped = [];
526
+ for (const j of _padgovJsV2.values())
527
+ if (
528
+ j.status === PADGOV_SCAN_LIFECYCLE_V2.SCANNING &&
529
+ j.startedAt != null &&
530
+ t - j.startedAt >= _padgovStuckMs
531
+ ) {
532
+ j.status = PADGOV_SCAN_LIFECYCLE_V2.FAILED;
533
+ j.updatedAt = t;
534
+ if (!j.settledAt) j.settledAt = t;
535
+ j.metadata.failReason = "auto-fail-stuck";
536
+ flipped.push(j.id);
537
+ }
538
+ return { flipped, count: flipped.length };
539
+ }
540
+ export function getPluginAutodiscoveryGovStatsV2() {
541
+ const profilesByStatus = {};
542
+ for (const v of Object.values(PADGOV_PROFILE_MATURITY_V2))
543
+ profilesByStatus[v] = 0;
544
+ for (const p of _padgovPsV2.values()) profilesByStatus[p.status]++;
545
+ const scansByStatus = {};
546
+ for (const v of Object.values(PADGOV_SCAN_LIFECYCLE_V2)) scansByStatus[v] = 0;
547
+ for (const j of _padgovJsV2.values()) scansByStatus[j.status]++;
548
+ return {
549
+ totalPadgovProfilesV2: _padgovPsV2.size,
550
+ totalPadgovScansV2: _padgovJsV2.size,
551
+ maxActivePadgovProfilesPerOwner: _padgovMaxActive,
552
+ maxPendingPadgovScansPerProfile: _padgovMaxPending,
553
+ padgovProfileIdleMs: _padgovIdleMs,
554
+ padgovScanStuckMs: _padgovStuckMs,
555
+ profilesByStatus,
556
+ scansByStatus,
557
+ };
558
+ }
@@ -147,3 +147,339 @@ function searchDir(dir, extension, results) {
147
147
  }
148
148
  }
149
149
  }
150
+
151
+ // =====================================================================
152
+ // process-manager V2 governance overlay (iter27)
153
+ // =====================================================================
154
+ export const PMGRGOV_PROFILE_MATURITY_V2 = Object.freeze({
155
+ PENDING: "pending",
156
+ ACTIVE: "active",
157
+ STOPPED: "stopped",
158
+ ARCHIVED: "archived",
159
+ });
160
+ export const PMGRGOV_PROC_LIFECYCLE_V2 = Object.freeze({
161
+ QUEUED: "queued",
162
+ STARTING: "starting",
163
+ RUNNING: "running",
164
+ FAILED: "failed",
165
+ CANCELLED: "cancelled",
166
+ });
167
+ const _pmgrgovPTrans = new Map([
168
+ [
169
+ PMGRGOV_PROFILE_MATURITY_V2.PENDING,
170
+ new Set([
171
+ PMGRGOV_PROFILE_MATURITY_V2.ACTIVE,
172
+ PMGRGOV_PROFILE_MATURITY_V2.ARCHIVED,
173
+ ]),
174
+ ],
175
+ [
176
+ PMGRGOV_PROFILE_MATURITY_V2.ACTIVE,
177
+ new Set([
178
+ PMGRGOV_PROFILE_MATURITY_V2.STOPPED,
179
+ PMGRGOV_PROFILE_MATURITY_V2.ARCHIVED,
180
+ ]),
181
+ ],
182
+ [
183
+ PMGRGOV_PROFILE_MATURITY_V2.STOPPED,
184
+ new Set([
185
+ PMGRGOV_PROFILE_MATURITY_V2.ACTIVE,
186
+ PMGRGOV_PROFILE_MATURITY_V2.ARCHIVED,
187
+ ]),
188
+ ],
189
+ [PMGRGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
190
+ ]);
191
+ const _pmgrgovPTerminal = new Set([PMGRGOV_PROFILE_MATURITY_V2.ARCHIVED]);
192
+ const _pmgrgovJTrans = new Map([
193
+ [
194
+ PMGRGOV_PROC_LIFECYCLE_V2.QUEUED,
195
+ new Set([
196
+ PMGRGOV_PROC_LIFECYCLE_V2.STARTING,
197
+ PMGRGOV_PROC_LIFECYCLE_V2.CANCELLED,
198
+ ]),
199
+ ],
200
+ [
201
+ PMGRGOV_PROC_LIFECYCLE_V2.STARTING,
202
+ new Set([
203
+ PMGRGOV_PROC_LIFECYCLE_V2.RUNNING,
204
+ PMGRGOV_PROC_LIFECYCLE_V2.FAILED,
205
+ PMGRGOV_PROC_LIFECYCLE_V2.CANCELLED,
206
+ ]),
207
+ ],
208
+ [PMGRGOV_PROC_LIFECYCLE_V2.RUNNING, new Set()],
209
+ [PMGRGOV_PROC_LIFECYCLE_V2.FAILED, new Set()],
210
+ [PMGRGOV_PROC_LIFECYCLE_V2.CANCELLED, new Set()],
211
+ ]);
212
+ const _pmgrgovPsV2 = new Map();
213
+ const _pmgrgovJsV2 = new Map();
214
+ let _pmgrgovMaxActive = 8,
215
+ _pmgrgovMaxPending = 20,
216
+ _pmgrgovIdleMs = 30 * 24 * 60 * 60 * 1000,
217
+ _pmgrgovStuckMs = 60 * 1000;
218
+ function _pmgrgovPos(n, label) {
219
+ const v = Math.floor(Number(n));
220
+ if (!Number.isFinite(v) || v <= 0)
221
+ throw new Error(`${label} must be positive integer`);
222
+ return v;
223
+ }
224
+ function _pmgrgovCheckP(from, to) {
225
+ const a = _pmgrgovPTrans.get(from);
226
+ if (!a || !a.has(to))
227
+ throw new Error(`invalid pmgrgov profile transition ${from} → ${to}`);
228
+ }
229
+ function _pmgrgovCheckJ(from, to) {
230
+ const a = _pmgrgovJTrans.get(from);
231
+ if (!a || !a.has(to))
232
+ throw new Error(`invalid pmgrgov proc transition ${from} → ${to}`);
233
+ }
234
+ function _pmgrgovCountActive(owner) {
235
+ let c = 0;
236
+ for (const p of _pmgrgovPsV2.values())
237
+ if (p.owner === owner && p.status === PMGRGOV_PROFILE_MATURITY_V2.ACTIVE)
238
+ c++;
239
+ return c;
240
+ }
241
+ function _pmgrgovCountPending(profileId) {
242
+ let c = 0;
243
+ for (const j of _pmgrgovJsV2.values())
244
+ if (
245
+ j.profileId === profileId &&
246
+ (j.status === PMGRGOV_PROC_LIFECYCLE_V2.QUEUED ||
247
+ j.status === PMGRGOV_PROC_LIFECYCLE_V2.STARTING)
248
+ )
249
+ c++;
250
+ return c;
251
+ }
252
+ export function setMaxActivePmgrgovProfilesPerOwnerV2(n) {
253
+ _pmgrgovMaxActive = _pmgrgovPos(n, "maxActivePmgrgovProfilesPerOwner");
254
+ }
255
+ export function getMaxActivePmgrgovProfilesPerOwnerV2() {
256
+ return _pmgrgovMaxActive;
257
+ }
258
+ export function setMaxPendingPmgrgovProcsPerProfileV2(n) {
259
+ _pmgrgovMaxPending = _pmgrgovPos(n, "maxPendingPmgrgovProcsPerProfile");
260
+ }
261
+ export function getMaxPendingPmgrgovProcsPerProfileV2() {
262
+ return _pmgrgovMaxPending;
263
+ }
264
+ export function setPmgrgovProfileIdleMsV2(n) {
265
+ _pmgrgovIdleMs = _pmgrgovPos(n, "pmgrgovProfileIdleMs");
266
+ }
267
+ export function getPmgrgovProfileIdleMsV2() {
268
+ return _pmgrgovIdleMs;
269
+ }
270
+ export function setPmgrgovProcStuckMsV2(n) {
271
+ _pmgrgovStuckMs = _pmgrgovPos(n, "pmgrgovProcStuckMs");
272
+ }
273
+ export function getPmgrgovProcStuckMsV2() {
274
+ return _pmgrgovStuckMs;
275
+ }
276
+ export function _resetStateProcessManagerGovV2() {
277
+ _pmgrgovPsV2.clear();
278
+ _pmgrgovJsV2.clear();
279
+ _pmgrgovMaxActive = 8;
280
+ _pmgrgovMaxPending = 20;
281
+ _pmgrgovIdleMs = 30 * 24 * 60 * 60 * 1000;
282
+ _pmgrgovStuckMs = 60 * 1000;
283
+ }
284
+ export function registerPmgrgovProfileV2({ id, owner, kind, metadata } = {}) {
285
+ if (!id || !owner) throw new Error("id and owner required");
286
+ if (_pmgrgovPsV2.has(id))
287
+ throw new Error(`pmgrgov profile ${id} already exists`);
288
+ const now = Date.now();
289
+ const p = {
290
+ id,
291
+ owner,
292
+ kind: kind || "service",
293
+ status: PMGRGOV_PROFILE_MATURITY_V2.PENDING,
294
+ createdAt: now,
295
+ updatedAt: now,
296
+ lastTouchedAt: now,
297
+ activatedAt: null,
298
+ archivedAt: null,
299
+ metadata: { ...(metadata || {}) },
300
+ };
301
+ _pmgrgovPsV2.set(id, p);
302
+ return { ...p, metadata: { ...p.metadata } };
303
+ }
304
+ export function activatePmgrgovProfileV2(id) {
305
+ const p = _pmgrgovPsV2.get(id);
306
+ if (!p) throw new Error(`pmgrgov profile ${id} not found`);
307
+ const isInitial = p.status === PMGRGOV_PROFILE_MATURITY_V2.PENDING;
308
+ _pmgrgovCheckP(p.status, PMGRGOV_PROFILE_MATURITY_V2.ACTIVE);
309
+ if (isInitial && _pmgrgovCountActive(p.owner) >= _pmgrgovMaxActive)
310
+ throw new Error(`max active pmgrgov profiles for owner ${p.owner} reached`);
311
+ const now = Date.now();
312
+ p.status = PMGRGOV_PROFILE_MATURITY_V2.ACTIVE;
313
+ p.updatedAt = now;
314
+ p.lastTouchedAt = now;
315
+ if (!p.activatedAt) p.activatedAt = now;
316
+ return { ...p, metadata: { ...p.metadata } };
317
+ }
318
+ export function stopPmgrgovProfileV2(id) {
319
+ const p = _pmgrgovPsV2.get(id);
320
+ if (!p) throw new Error(`pmgrgov profile ${id} not found`);
321
+ _pmgrgovCheckP(p.status, PMGRGOV_PROFILE_MATURITY_V2.STOPPED);
322
+ p.status = PMGRGOV_PROFILE_MATURITY_V2.STOPPED;
323
+ p.updatedAt = Date.now();
324
+ return { ...p, metadata: { ...p.metadata } };
325
+ }
326
+ export function archivePmgrgovProfileV2(id) {
327
+ const p = _pmgrgovPsV2.get(id);
328
+ if (!p) throw new Error(`pmgrgov profile ${id} not found`);
329
+ _pmgrgovCheckP(p.status, PMGRGOV_PROFILE_MATURITY_V2.ARCHIVED);
330
+ const now = Date.now();
331
+ p.status = PMGRGOV_PROFILE_MATURITY_V2.ARCHIVED;
332
+ p.updatedAt = now;
333
+ if (!p.archivedAt) p.archivedAt = now;
334
+ return { ...p, metadata: { ...p.metadata } };
335
+ }
336
+ export function touchPmgrgovProfileV2(id) {
337
+ const p = _pmgrgovPsV2.get(id);
338
+ if (!p) throw new Error(`pmgrgov profile ${id} not found`);
339
+ if (_pmgrgovPTerminal.has(p.status))
340
+ throw new Error(`cannot touch terminal pmgrgov profile ${id}`);
341
+ const now = Date.now();
342
+ p.lastTouchedAt = now;
343
+ p.updatedAt = now;
344
+ return { ...p, metadata: { ...p.metadata } };
345
+ }
346
+ export function getPmgrgovProfileV2(id) {
347
+ const p = _pmgrgovPsV2.get(id);
348
+ if (!p) return null;
349
+ return { ...p, metadata: { ...p.metadata } };
350
+ }
351
+ export function listPmgrgovProfilesV2() {
352
+ return [..._pmgrgovPsV2.values()].map((p) => ({
353
+ ...p,
354
+ metadata: { ...p.metadata },
355
+ }));
356
+ }
357
+ export function createPmgrgovProcV2({ id, profileId, command, metadata } = {}) {
358
+ if (!id || !profileId) throw new Error("id and profileId required");
359
+ if (_pmgrgovJsV2.has(id))
360
+ throw new Error(`pmgrgov proc ${id} already exists`);
361
+ if (!_pmgrgovPsV2.has(profileId))
362
+ throw new Error(`pmgrgov profile ${profileId} not found`);
363
+ if (_pmgrgovCountPending(profileId) >= _pmgrgovMaxPending)
364
+ throw new Error(
365
+ `max pending pmgrgov procs for profile ${profileId} reached`,
366
+ );
367
+ const now = Date.now();
368
+ const j = {
369
+ id,
370
+ profileId,
371
+ command: command || "",
372
+ status: PMGRGOV_PROC_LIFECYCLE_V2.QUEUED,
373
+ createdAt: now,
374
+ updatedAt: now,
375
+ startedAt: null,
376
+ settledAt: null,
377
+ metadata: { ...(metadata || {}) },
378
+ };
379
+ _pmgrgovJsV2.set(id, j);
380
+ return { ...j, metadata: { ...j.metadata } };
381
+ }
382
+ export function startingPmgrgovProcV2(id) {
383
+ const j = _pmgrgovJsV2.get(id);
384
+ if (!j) throw new Error(`pmgrgov proc ${id} not found`);
385
+ _pmgrgovCheckJ(j.status, PMGRGOV_PROC_LIFECYCLE_V2.STARTING);
386
+ const now = Date.now();
387
+ j.status = PMGRGOV_PROC_LIFECYCLE_V2.STARTING;
388
+ j.updatedAt = now;
389
+ if (!j.startedAt) j.startedAt = now;
390
+ return { ...j, metadata: { ...j.metadata } };
391
+ }
392
+ export function completeProcPmgrgovV2(id) {
393
+ const j = _pmgrgovJsV2.get(id);
394
+ if (!j) throw new Error(`pmgrgov proc ${id} not found`);
395
+ _pmgrgovCheckJ(j.status, PMGRGOV_PROC_LIFECYCLE_V2.RUNNING);
396
+ const now = Date.now();
397
+ j.status = PMGRGOV_PROC_LIFECYCLE_V2.RUNNING;
398
+ j.updatedAt = now;
399
+ if (!j.settledAt) j.settledAt = now;
400
+ return { ...j, metadata: { ...j.metadata } };
401
+ }
402
+ export function failPmgrgovProcV2(id, reason) {
403
+ const j = _pmgrgovJsV2.get(id);
404
+ if (!j) throw new Error(`pmgrgov proc ${id} not found`);
405
+ _pmgrgovCheckJ(j.status, PMGRGOV_PROC_LIFECYCLE_V2.FAILED);
406
+ const now = Date.now();
407
+ j.status = PMGRGOV_PROC_LIFECYCLE_V2.FAILED;
408
+ j.updatedAt = now;
409
+ if (!j.settledAt) j.settledAt = now;
410
+ if (reason) j.metadata.failReason = String(reason);
411
+ return { ...j, metadata: { ...j.metadata } };
412
+ }
413
+ export function cancelPmgrgovProcV2(id, reason) {
414
+ const j = _pmgrgovJsV2.get(id);
415
+ if (!j) throw new Error(`pmgrgov proc ${id} not found`);
416
+ _pmgrgovCheckJ(j.status, PMGRGOV_PROC_LIFECYCLE_V2.CANCELLED);
417
+ const now = Date.now();
418
+ j.status = PMGRGOV_PROC_LIFECYCLE_V2.CANCELLED;
419
+ j.updatedAt = now;
420
+ if (!j.settledAt) j.settledAt = now;
421
+ if (reason) j.metadata.cancelReason = String(reason);
422
+ return { ...j, metadata: { ...j.metadata } };
423
+ }
424
+ export function getPmgrgovProcV2(id) {
425
+ const j = _pmgrgovJsV2.get(id);
426
+ if (!j) return null;
427
+ return { ...j, metadata: { ...j.metadata } };
428
+ }
429
+ export function listPmgrgovProcsV2() {
430
+ return [..._pmgrgovJsV2.values()].map((j) => ({
431
+ ...j,
432
+ metadata: { ...j.metadata },
433
+ }));
434
+ }
435
+ export function autoStopIdlePmgrgovProfilesV2({ now } = {}) {
436
+ const t = now ?? Date.now();
437
+ const flipped = [];
438
+ for (const p of _pmgrgovPsV2.values())
439
+ if (
440
+ p.status === PMGRGOV_PROFILE_MATURITY_V2.ACTIVE &&
441
+ t - p.lastTouchedAt >= _pmgrgovIdleMs
442
+ ) {
443
+ p.status = PMGRGOV_PROFILE_MATURITY_V2.STOPPED;
444
+ p.updatedAt = t;
445
+ flipped.push(p.id);
446
+ }
447
+ return { flipped, count: flipped.length };
448
+ }
449
+ export function autoFailStuckPmgrgovProcsV2({ now } = {}) {
450
+ const t = now ?? Date.now();
451
+ const flipped = [];
452
+ for (const j of _pmgrgovJsV2.values())
453
+ if (
454
+ j.status === PMGRGOV_PROC_LIFECYCLE_V2.STARTING &&
455
+ j.startedAt != null &&
456
+ t - j.startedAt >= _pmgrgovStuckMs
457
+ ) {
458
+ j.status = PMGRGOV_PROC_LIFECYCLE_V2.FAILED;
459
+ j.updatedAt = t;
460
+ if (!j.settledAt) j.settledAt = t;
461
+ j.metadata.failReason = "auto-fail-stuck";
462
+ flipped.push(j.id);
463
+ }
464
+ return { flipped, count: flipped.length };
465
+ }
466
+ export function getProcessManagerGovStatsV2() {
467
+ const profilesByStatus = {};
468
+ for (const v of Object.values(PMGRGOV_PROFILE_MATURITY_V2))
469
+ profilesByStatus[v] = 0;
470
+ for (const p of _pmgrgovPsV2.values()) profilesByStatus[p.status]++;
471
+ const procsByStatus = {};
472
+ for (const v of Object.values(PMGRGOV_PROC_LIFECYCLE_V2))
473
+ procsByStatus[v] = 0;
474
+ for (const j of _pmgrgovJsV2.values()) procsByStatus[j.status]++;
475
+ return {
476
+ totalPmgrgovProfilesV2: _pmgrgovPsV2.size,
477
+ totalPmgrgovProcsV2: _pmgrgovJsV2.size,
478
+ maxActivePmgrgovProfilesPerOwner: _pmgrgovMaxActive,
479
+ maxPendingPmgrgovProcsPerProfile: _pmgrgovMaxPending,
480
+ pmgrgovProfileIdleMs: _pmgrgovIdleMs,
481
+ pmgrgovProcStuckMs: _pmgrgovStuckMs,
482
+ profilesByStatus,
483
+ procsByStatus,
484
+ };
485
+ }