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
@@ -192,3 +192,337 @@ function formatBytes(bytes) {
192
192
  }
193
193
 
194
194
  export { resolveAssetUrl, formatBytes };
195
+
196
+ // =====================================================================
197
+ // downloader V2 governance overlay (iter27)
198
+ // =====================================================================
199
+ export const DLGOV_PROFILE_MATURITY_V2 = Object.freeze({
200
+ PENDING: "pending",
201
+ ACTIVE: "active",
202
+ STALE: "stale",
203
+ ARCHIVED: "archived",
204
+ });
205
+ export const DLGOV_DOWNLOAD_LIFECYCLE_V2 = Object.freeze({
206
+ QUEUED: "queued",
207
+ FETCHING: "fetching",
208
+ FETCHED: "fetched",
209
+ FAILED: "failed",
210
+ CANCELLED: "cancelled",
211
+ });
212
+ const _dlgovPTrans = new Map([
213
+ [
214
+ DLGOV_PROFILE_MATURITY_V2.PENDING,
215
+ new Set([
216
+ DLGOV_PROFILE_MATURITY_V2.ACTIVE,
217
+ DLGOV_PROFILE_MATURITY_V2.ARCHIVED,
218
+ ]),
219
+ ],
220
+ [
221
+ DLGOV_PROFILE_MATURITY_V2.ACTIVE,
222
+ new Set([
223
+ DLGOV_PROFILE_MATURITY_V2.STALE,
224
+ DLGOV_PROFILE_MATURITY_V2.ARCHIVED,
225
+ ]),
226
+ ],
227
+ [
228
+ DLGOV_PROFILE_MATURITY_V2.STALE,
229
+ new Set([
230
+ DLGOV_PROFILE_MATURITY_V2.ACTIVE,
231
+ DLGOV_PROFILE_MATURITY_V2.ARCHIVED,
232
+ ]),
233
+ ],
234
+ [DLGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
235
+ ]);
236
+ const _dlgovPTerminal = new Set([DLGOV_PROFILE_MATURITY_V2.ARCHIVED]);
237
+ const _dlgovJTrans = new Map([
238
+ [
239
+ DLGOV_DOWNLOAD_LIFECYCLE_V2.QUEUED,
240
+ new Set([
241
+ DLGOV_DOWNLOAD_LIFECYCLE_V2.FETCHING,
242
+ DLGOV_DOWNLOAD_LIFECYCLE_V2.CANCELLED,
243
+ ]),
244
+ ],
245
+ [
246
+ DLGOV_DOWNLOAD_LIFECYCLE_V2.FETCHING,
247
+ new Set([
248
+ DLGOV_DOWNLOAD_LIFECYCLE_V2.FETCHED,
249
+ DLGOV_DOWNLOAD_LIFECYCLE_V2.FAILED,
250
+ DLGOV_DOWNLOAD_LIFECYCLE_V2.CANCELLED,
251
+ ]),
252
+ ],
253
+ [DLGOV_DOWNLOAD_LIFECYCLE_V2.FETCHED, new Set()],
254
+ [DLGOV_DOWNLOAD_LIFECYCLE_V2.FAILED, new Set()],
255
+ [DLGOV_DOWNLOAD_LIFECYCLE_V2.CANCELLED, new Set()],
256
+ ]);
257
+ const _dlgovPsV2 = new Map();
258
+ const _dlgovJsV2 = new Map();
259
+ let _dlgovMaxActive = 6,
260
+ _dlgovMaxPending = 15,
261
+ _dlgovIdleMs = 30 * 24 * 60 * 60 * 1000,
262
+ _dlgovStuckMs = 60 * 1000;
263
+ function _dlgovPos(n, label) {
264
+ const v = Math.floor(Number(n));
265
+ if (!Number.isFinite(v) || v <= 0)
266
+ throw new Error(`${label} must be positive integer`);
267
+ return v;
268
+ }
269
+ function _dlgovCheckP(from, to) {
270
+ const a = _dlgovPTrans.get(from);
271
+ if (!a || !a.has(to))
272
+ throw new Error(`invalid dlgov profile transition ${from} → ${to}`);
273
+ }
274
+ function _dlgovCheckJ(from, to) {
275
+ const a = _dlgovJTrans.get(from);
276
+ if (!a || !a.has(to))
277
+ throw new Error(`invalid dlgov download transition ${from} → ${to}`);
278
+ }
279
+ function _dlgovCountActive(owner) {
280
+ let c = 0;
281
+ for (const p of _dlgovPsV2.values())
282
+ if (p.owner === owner && p.status === DLGOV_PROFILE_MATURITY_V2.ACTIVE) c++;
283
+ return c;
284
+ }
285
+ function _dlgovCountPending(profileId) {
286
+ let c = 0;
287
+ for (const j of _dlgovJsV2.values())
288
+ if (
289
+ j.profileId === profileId &&
290
+ (j.status === DLGOV_DOWNLOAD_LIFECYCLE_V2.QUEUED ||
291
+ j.status === DLGOV_DOWNLOAD_LIFECYCLE_V2.FETCHING)
292
+ )
293
+ c++;
294
+ return c;
295
+ }
296
+ export function setMaxActiveDlgovProfilesPerOwnerV2(n) {
297
+ _dlgovMaxActive = _dlgovPos(n, "maxActiveDlgovProfilesPerOwner");
298
+ }
299
+ export function getMaxActiveDlgovProfilesPerOwnerV2() {
300
+ return _dlgovMaxActive;
301
+ }
302
+ export function setMaxPendingDlgovDownloadsPerProfileV2(n) {
303
+ _dlgovMaxPending = _dlgovPos(n, "maxPendingDlgovDownloadsPerProfile");
304
+ }
305
+ export function getMaxPendingDlgovDownloadsPerProfileV2() {
306
+ return _dlgovMaxPending;
307
+ }
308
+ export function setDlgovProfileIdleMsV2(n) {
309
+ _dlgovIdleMs = _dlgovPos(n, "dlgovProfileIdleMs");
310
+ }
311
+ export function getDlgovProfileIdleMsV2() {
312
+ return _dlgovIdleMs;
313
+ }
314
+ export function setDlgovDownloadStuckMsV2(n) {
315
+ _dlgovStuckMs = _dlgovPos(n, "dlgovDownloadStuckMs");
316
+ }
317
+ export function getDlgovDownloadStuckMsV2() {
318
+ return _dlgovStuckMs;
319
+ }
320
+ export function _resetStateDownloaderGovV2() {
321
+ _dlgovPsV2.clear();
322
+ _dlgovJsV2.clear();
323
+ _dlgovMaxActive = 6;
324
+ _dlgovMaxPending = 15;
325
+ _dlgovIdleMs = 30 * 24 * 60 * 60 * 1000;
326
+ _dlgovStuckMs = 60 * 1000;
327
+ }
328
+ export function registerDlgovProfileV2({ id, owner, mirror, metadata } = {}) {
329
+ if (!id || !owner) throw new Error("id and owner required");
330
+ if (_dlgovPsV2.has(id)) throw new Error(`dlgov profile ${id} already exists`);
331
+ const now = Date.now();
332
+ const p = {
333
+ id,
334
+ owner,
335
+ mirror: mirror || "default",
336
+ status: DLGOV_PROFILE_MATURITY_V2.PENDING,
337
+ createdAt: now,
338
+ updatedAt: now,
339
+ lastTouchedAt: now,
340
+ activatedAt: null,
341
+ archivedAt: null,
342
+ metadata: { ...(metadata || {}) },
343
+ };
344
+ _dlgovPsV2.set(id, p);
345
+ return { ...p, metadata: { ...p.metadata } };
346
+ }
347
+ export function activateDlgovProfileV2(id) {
348
+ const p = _dlgovPsV2.get(id);
349
+ if (!p) throw new Error(`dlgov profile ${id} not found`);
350
+ const isInitial = p.status === DLGOV_PROFILE_MATURITY_V2.PENDING;
351
+ _dlgovCheckP(p.status, DLGOV_PROFILE_MATURITY_V2.ACTIVE);
352
+ if (isInitial && _dlgovCountActive(p.owner) >= _dlgovMaxActive)
353
+ throw new Error(`max active dlgov profiles for owner ${p.owner} reached`);
354
+ const now = Date.now();
355
+ p.status = DLGOV_PROFILE_MATURITY_V2.ACTIVE;
356
+ p.updatedAt = now;
357
+ p.lastTouchedAt = now;
358
+ if (!p.activatedAt) p.activatedAt = now;
359
+ return { ...p, metadata: { ...p.metadata } };
360
+ }
361
+ export function staleDlgovProfileV2(id) {
362
+ const p = _dlgovPsV2.get(id);
363
+ if (!p) throw new Error(`dlgov profile ${id} not found`);
364
+ _dlgovCheckP(p.status, DLGOV_PROFILE_MATURITY_V2.STALE);
365
+ p.status = DLGOV_PROFILE_MATURITY_V2.STALE;
366
+ p.updatedAt = Date.now();
367
+ return { ...p, metadata: { ...p.metadata } };
368
+ }
369
+ export function archiveDlgovProfileV2(id) {
370
+ const p = _dlgovPsV2.get(id);
371
+ if (!p) throw new Error(`dlgov profile ${id} not found`);
372
+ _dlgovCheckP(p.status, DLGOV_PROFILE_MATURITY_V2.ARCHIVED);
373
+ const now = Date.now();
374
+ p.status = DLGOV_PROFILE_MATURITY_V2.ARCHIVED;
375
+ p.updatedAt = now;
376
+ if (!p.archivedAt) p.archivedAt = now;
377
+ return { ...p, metadata: { ...p.metadata } };
378
+ }
379
+ export function touchDlgovProfileV2(id) {
380
+ const p = _dlgovPsV2.get(id);
381
+ if (!p) throw new Error(`dlgov profile ${id} not found`);
382
+ if (_dlgovPTerminal.has(p.status))
383
+ throw new Error(`cannot touch terminal dlgov profile ${id}`);
384
+ const now = Date.now();
385
+ p.lastTouchedAt = now;
386
+ p.updatedAt = now;
387
+ return { ...p, metadata: { ...p.metadata } };
388
+ }
389
+ export function getDlgovProfileV2(id) {
390
+ const p = _dlgovPsV2.get(id);
391
+ if (!p) return null;
392
+ return { ...p, metadata: { ...p.metadata } };
393
+ }
394
+ export function listDlgovProfilesV2() {
395
+ return [..._dlgovPsV2.values()].map((p) => ({
396
+ ...p,
397
+ metadata: { ...p.metadata },
398
+ }));
399
+ }
400
+ export function createDlgovDownloadV2({ id, profileId, url, metadata } = {}) {
401
+ if (!id || !profileId) throw new Error("id and profileId required");
402
+ if (_dlgovJsV2.has(id))
403
+ throw new Error(`dlgov download ${id} already exists`);
404
+ if (!_dlgovPsV2.has(profileId))
405
+ throw new Error(`dlgov profile ${profileId} not found`);
406
+ if (_dlgovCountPending(profileId) >= _dlgovMaxPending)
407
+ throw new Error(
408
+ `max pending dlgov downloads for profile ${profileId} reached`,
409
+ );
410
+ const now = Date.now();
411
+ const j = {
412
+ id,
413
+ profileId,
414
+ url: url || "",
415
+ status: DLGOV_DOWNLOAD_LIFECYCLE_V2.QUEUED,
416
+ createdAt: now,
417
+ updatedAt: now,
418
+ startedAt: null,
419
+ settledAt: null,
420
+ metadata: { ...(metadata || {}) },
421
+ };
422
+ _dlgovJsV2.set(id, j);
423
+ return { ...j, metadata: { ...j.metadata } };
424
+ }
425
+ export function fetchingDlgovDownloadV2(id) {
426
+ const j = _dlgovJsV2.get(id);
427
+ if (!j) throw new Error(`dlgov download ${id} not found`);
428
+ _dlgovCheckJ(j.status, DLGOV_DOWNLOAD_LIFECYCLE_V2.FETCHING);
429
+ const now = Date.now();
430
+ j.status = DLGOV_DOWNLOAD_LIFECYCLE_V2.FETCHING;
431
+ j.updatedAt = now;
432
+ if (!j.startedAt) j.startedAt = now;
433
+ return { ...j, metadata: { ...j.metadata } };
434
+ }
435
+ export function completeDownloadDlgovV2(id) {
436
+ const j = _dlgovJsV2.get(id);
437
+ if (!j) throw new Error(`dlgov download ${id} not found`);
438
+ _dlgovCheckJ(j.status, DLGOV_DOWNLOAD_LIFECYCLE_V2.FETCHED);
439
+ const now = Date.now();
440
+ j.status = DLGOV_DOWNLOAD_LIFECYCLE_V2.FETCHED;
441
+ j.updatedAt = now;
442
+ if (!j.settledAt) j.settledAt = now;
443
+ return { ...j, metadata: { ...j.metadata } };
444
+ }
445
+ export function failDlgovDownloadV2(id, reason) {
446
+ const j = _dlgovJsV2.get(id);
447
+ if (!j) throw new Error(`dlgov download ${id} not found`);
448
+ _dlgovCheckJ(j.status, DLGOV_DOWNLOAD_LIFECYCLE_V2.FAILED);
449
+ const now = Date.now();
450
+ j.status = DLGOV_DOWNLOAD_LIFECYCLE_V2.FAILED;
451
+ j.updatedAt = now;
452
+ if (!j.settledAt) j.settledAt = now;
453
+ if (reason) j.metadata.failReason = String(reason);
454
+ return { ...j, metadata: { ...j.metadata } };
455
+ }
456
+ export function cancelDlgovDownloadV2(id, reason) {
457
+ const j = _dlgovJsV2.get(id);
458
+ if (!j) throw new Error(`dlgov download ${id} not found`);
459
+ _dlgovCheckJ(j.status, DLGOV_DOWNLOAD_LIFECYCLE_V2.CANCELLED);
460
+ const now = Date.now();
461
+ j.status = DLGOV_DOWNLOAD_LIFECYCLE_V2.CANCELLED;
462
+ j.updatedAt = now;
463
+ if (!j.settledAt) j.settledAt = now;
464
+ if (reason) j.metadata.cancelReason = String(reason);
465
+ return { ...j, metadata: { ...j.metadata } };
466
+ }
467
+ export function getDlgovDownloadV2(id) {
468
+ const j = _dlgovJsV2.get(id);
469
+ if (!j) return null;
470
+ return { ...j, metadata: { ...j.metadata } };
471
+ }
472
+ export function listDlgovDownloadsV2() {
473
+ return [..._dlgovJsV2.values()].map((j) => ({
474
+ ...j,
475
+ metadata: { ...j.metadata },
476
+ }));
477
+ }
478
+ export function autoStaleIdleDlgovProfilesV2({ now } = {}) {
479
+ const t = now ?? Date.now();
480
+ const flipped = [];
481
+ for (const p of _dlgovPsV2.values())
482
+ if (
483
+ p.status === DLGOV_PROFILE_MATURITY_V2.ACTIVE &&
484
+ t - p.lastTouchedAt >= _dlgovIdleMs
485
+ ) {
486
+ p.status = DLGOV_PROFILE_MATURITY_V2.STALE;
487
+ p.updatedAt = t;
488
+ flipped.push(p.id);
489
+ }
490
+ return { flipped, count: flipped.length };
491
+ }
492
+ export function autoFailStuckDlgovDownloadsV2({ now } = {}) {
493
+ const t = now ?? Date.now();
494
+ const flipped = [];
495
+ for (const j of _dlgovJsV2.values())
496
+ if (
497
+ j.status === DLGOV_DOWNLOAD_LIFECYCLE_V2.FETCHING &&
498
+ j.startedAt != null &&
499
+ t - j.startedAt >= _dlgovStuckMs
500
+ ) {
501
+ j.status = DLGOV_DOWNLOAD_LIFECYCLE_V2.FAILED;
502
+ j.updatedAt = t;
503
+ if (!j.settledAt) j.settledAt = t;
504
+ j.metadata.failReason = "auto-fail-stuck";
505
+ flipped.push(j.id);
506
+ }
507
+ return { flipped, count: flipped.length };
508
+ }
509
+ export function getDownloaderGovStatsV2() {
510
+ const profilesByStatus = {};
511
+ for (const v of Object.values(DLGOV_PROFILE_MATURITY_V2))
512
+ profilesByStatus[v] = 0;
513
+ for (const p of _dlgovPsV2.values()) profilesByStatus[p.status]++;
514
+ const downloadsByStatus = {};
515
+ for (const v of Object.values(DLGOV_DOWNLOAD_LIFECYCLE_V2))
516
+ downloadsByStatus[v] = 0;
517
+ for (const j of _dlgovJsV2.values()) downloadsByStatus[j.status]++;
518
+ return {
519
+ totalDlgovProfilesV2: _dlgovPsV2.size,
520
+ totalDlgovDownloadsV2: _dlgovJsV2.size,
521
+ maxActiveDlgovProfilesPerOwner: _dlgovMaxActive,
522
+ maxPendingDlgovDownloadsPerProfile: _dlgovMaxPending,
523
+ dlgovProfileIdleMs: _dlgovIdleMs,
524
+ dlgovDownloadStuckMs: _dlgovStuckMs,
525
+ profilesByStatus,
526
+ downloadsByStatus,
527
+ };
528
+ }
@@ -1295,3 +1295,337 @@ export function getEvolutionSystemGovStatsV2() {
1295
1295
  cyclesByStatus,
1296
1296
  };
1297
1297
  }
1298
+
1299
+ // === Iter28 V2 governance overlay: Esysgov ===
1300
+ export const ESYSGOV_PROFILE_MATURITY_V2 = Object.freeze({
1301
+ PENDING: "pending",
1302
+ ACTIVE: "active",
1303
+ PAUSED: "paused",
1304
+ ARCHIVED: "archived",
1305
+ });
1306
+ export const ESYSGOV_CYCLE_LIFECYCLE_V2 = Object.freeze({
1307
+ QUEUED: "queued",
1308
+ EVOLVING: "evolving",
1309
+ EVOLVED: "evolved",
1310
+ FAILED: "failed",
1311
+ CANCELLED: "cancelled",
1312
+ });
1313
+ const _esysgovPTrans = new Map([
1314
+ [
1315
+ ESYSGOV_PROFILE_MATURITY_V2.PENDING,
1316
+ new Set([
1317
+ ESYSGOV_PROFILE_MATURITY_V2.ACTIVE,
1318
+ ESYSGOV_PROFILE_MATURITY_V2.ARCHIVED,
1319
+ ]),
1320
+ ],
1321
+ [
1322
+ ESYSGOV_PROFILE_MATURITY_V2.ACTIVE,
1323
+ new Set([
1324
+ ESYSGOV_PROFILE_MATURITY_V2.PAUSED,
1325
+ ESYSGOV_PROFILE_MATURITY_V2.ARCHIVED,
1326
+ ]),
1327
+ ],
1328
+ [
1329
+ ESYSGOV_PROFILE_MATURITY_V2.PAUSED,
1330
+ new Set([
1331
+ ESYSGOV_PROFILE_MATURITY_V2.ACTIVE,
1332
+ ESYSGOV_PROFILE_MATURITY_V2.ARCHIVED,
1333
+ ]),
1334
+ ],
1335
+ [ESYSGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
1336
+ ]);
1337
+ const _esysgovPTerminal = new Set([ESYSGOV_PROFILE_MATURITY_V2.ARCHIVED]);
1338
+ const _esysgovJTrans = new Map([
1339
+ [
1340
+ ESYSGOV_CYCLE_LIFECYCLE_V2.QUEUED,
1341
+ new Set([
1342
+ ESYSGOV_CYCLE_LIFECYCLE_V2.EVOLVING,
1343
+ ESYSGOV_CYCLE_LIFECYCLE_V2.CANCELLED,
1344
+ ]),
1345
+ ],
1346
+ [
1347
+ ESYSGOV_CYCLE_LIFECYCLE_V2.EVOLVING,
1348
+ new Set([
1349
+ ESYSGOV_CYCLE_LIFECYCLE_V2.EVOLVED,
1350
+ ESYSGOV_CYCLE_LIFECYCLE_V2.FAILED,
1351
+ ESYSGOV_CYCLE_LIFECYCLE_V2.CANCELLED,
1352
+ ]),
1353
+ ],
1354
+ [ESYSGOV_CYCLE_LIFECYCLE_V2.EVOLVED, new Set()],
1355
+ [ESYSGOV_CYCLE_LIFECYCLE_V2.FAILED, new Set()],
1356
+ [ESYSGOV_CYCLE_LIFECYCLE_V2.CANCELLED, new Set()],
1357
+ ]);
1358
+ const _esysgovPsV2 = new Map();
1359
+ const _esysgovJsV2 = new Map();
1360
+ let _esysgovMaxActive = 6,
1361
+ _esysgovMaxPending = 15,
1362
+ _esysgovIdleMs = 2592000000,
1363
+ _esysgovStuckMs = 60 * 1000;
1364
+ function _esysgovPos(n, label) {
1365
+ const v = Math.floor(Number(n));
1366
+ if (!Number.isFinite(v) || v <= 0)
1367
+ throw new Error(`${label} must be positive integer`);
1368
+ return v;
1369
+ }
1370
+ function _esysgovCheckP(from, to) {
1371
+ const a = _esysgovPTrans.get(from);
1372
+ if (!a || !a.has(to))
1373
+ throw new Error(`invalid esysgov profile transition ${from} → ${to}`);
1374
+ }
1375
+ function _esysgovCheckJ(from, to) {
1376
+ const a = _esysgovJTrans.get(from);
1377
+ if (!a || !a.has(to))
1378
+ throw new Error(`invalid esysgov cycle transition ${from} → ${to}`);
1379
+ }
1380
+ function _esysgovCountActive(owner) {
1381
+ let c = 0;
1382
+ for (const p of _esysgovPsV2.values())
1383
+ if (p.owner === owner && p.status === ESYSGOV_PROFILE_MATURITY_V2.ACTIVE)
1384
+ c++;
1385
+ return c;
1386
+ }
1387
+ function _esysgovCountPending(profileId) {
1388
+ let c = 0;
1389
+ for (const j of _esysgovJsV2.values())
1390
+ if (
1391
+ j.profileId === profileId &&
1392
+ (j.status === ESYSGOV_CYCLE_LIFECYCLE_V2.QUEUED ||
1393
+ j.status === ESYSGOV_CYCLE_LIFECYCLE_V2.EVOLVING)
1394
+ )
1395
+ c++;
1396
+ return c;
1397
+ }
1398
+ export function setMaxActiveEsysProfilesPerOwnerV2(n) {
1399
+ _esysgovMaxActive = _esysgovPos(n, "maxActiveEsysProfilesPerOwner");
1400
+ }
1401
+ export function getMaxActiveEsysProfilesPerOwnerV2() {
1402
+ return _esysgovMaxActive;
1403
+ }
1404
+ export function setMaxPendingEsysCyclesPerProfileV2(n) {
1405
+ _esysgovMaxPending = _esysgovPos(n, "maxPendingEsysCyclesPerProfile");
1406
+ }
1407
+ export function getMaxPendingEsysCyclesPerProfileV2() {
1408
+ return _esysgovMaxPending;
1409
+ }
1410
+ export function setEsysProfileIdleMsV2(n) {
1411
+ _esysgovIdleMs = _esysgovPos(n, "esysgovProfileIdleMs");
1412
+ }
1413
+ export function getEsysProfileIdleMsV2() {
1414
+ return _esysgovIdleMs;
1415
+ }
1416
+ export function setEsysCycleStuckMsV2(n) {
1417
+ _esysgovStuckMs = _esysgovPos(n, "esysgovCycleStuckMs");
1418
+ }
1419
+ export function getEsysCycleStuckMsV2() {
1420
+ return _esysgovStuckMs;
1421
+ }
1422
+ export function _resetStateEsysgovV2() {
1423
+ _esysgovPsV2.clear();
1424
+ _esysgovJsV2.clear();
1425
+ _esysgovMaxActive = 6;
1426
+ _esysgovMaxPending = 15;
1427
+ _esysgovIdleMs = 2592000000;
1428
+ _esysgovStuckMs = 60 * 1000;
1429
+ }
1430
+ export function registerEsysProfileV2({ id, owner, lane, metadata } = {}) {
1431
+ if (!id || !owner) throw new Error("id and owner required");
1432
+ if (_esysgovPsV2.has(id))
1433
+ throw new Error(`esysgov profile ${id} already exists`);
1434
+ const now = Date.now();
1435
+ const p = {
1436
+ id,
1437
+ owner,
1438
+ lane: lane || "default",
1439
+ status: ESYSGOV_PROFILE_MATURITY_V2.PENDING,
1440
+ createdAt: now,
1441
+ updatedAt: now,
1442
+ lastTouchedAt: now,
1443
+ activatedAt: null,
1444
+ archivedAt: null,
1445
+ metadata: { ...(metadata || {}) },
1446
+ };
1447
+ _esysgovPsV2.set(id, p);
1448
+ return { ...p, metadata: { ...p.metadata } };
1449
+ }
1450
+ export function activateEsysProfileV2(id) {
1451
+ const p = _esysgovPsV2.get(id);
1452
+ if (!p) throw new Error(`esysgov profile ${id} not found`);
1453
+ const isInitial = p.status === ESYSGOV_PROFILE_MATURITY_V2.PENDING;
1454
+ _esysgovCheckP(p.status, ESYSGOV_PROFILE_MATURITY_V2.ACTIVE);
1455
+ if (isInitial && _esysgovCountActive(p.owner) >= _esysgovMaxActive)
1456
+ throw new Error(`max active esysgov profiles for owner ${p.owner} reached`);
1457
+ const now = Date.now();
1458
+ p.status = ESYSGOV_PROFILE_MATURITY_V2.ACTIVE;
1459
+ p.updatedAt = now;
1460
+ p.lastTouchedAt = now;
1461
+ if (!p.activatedAt) p.activatedAt = now;
1462
+ return { ...p, metadata: { ...p.metadata } };
1463
+ }
1464
+ export function pausedEsysProfileV2(id) {
1465
+ const p = _esysgovPsV2.get(id);
1466
+ if (!p) throw new Error(`esysgov profile ${id} not found`);
1467
+ _esysgovCheckP(p.status, ESYSGOV_PROFILE_MATURITY_V2.PAUSED);
1468
+ p.status = ESYSGOV_PROFILE_MATURITY_V2.PAUSED;
1469
+ p.updatedAt = Date.now();
1470
+ return { ...p, metadata: { ...p.metadata } };
1471
+ }
1472
+ export function archiveEsysProfileV2(id) {
1473
+ const p = _esysgovPsV2.get(id);
1474
+ if (!p) throw new Error(`esysgov profile ${id} not found`);
1475
+ _esysgovCheckP(p.status, ESYSGOV_PROFILE_MATURITY_V2.ARCHIVED);
1476
+ const now = Date.now();
1477
+ p.status = ESYSGOV_PROFILE_MATURITY_V2.ARCHIVED;
1478
+ p.updatedAt = now;
1479
+ if (!p.archivedAt) p.archivedAt = now;
1480
+ return { ...p, metadata: { ...p.metadata } };
1481
+ }
1482
+ export function touchEsysProfileV2(id) {
1483
+ const p = _esysgovPsV2.get(id);
1484
+ if (!p) throw new Error(`esysgov profile ${id} not found`);
1485
+ if (_esysgovPTerminal.has(p.status))
1486
+ throw new Error(`cannot touch terminal esysgov profile ${id}`);
1487
+ const now = Date.now();
1488
+ p.lastTouchedAt = now;
1489
+ p.updatedAt = now;
1490
+ return { ...p, metadata: { ...p.metadata } };
1491
+ }
1492
+ export function getEsysProfileV2(id) {
1493
+ const p = _esysgovPsV2.get(id);
1494
+ if (!p) return null;
1495
+ return { ...p, metadata: { ...p.metadata } };
1496
+ }
1497
+ export function listEsysProfilesV2() {
1498
+ return [..._esysgovPsV2.values()].map((p) => ({
1499
+ ...p,
1500
+ metadata: { ...p.metadata },
1501
+ }));
1502
+ }
1503
+ export function createEsysCycleV2({ id, profileId, cycleId, metadata } = {}) {
1504
+ if (!id || !profileId) throw new Error("id and profileId required");
1505
+ if (_esysgovJsV2.has(id))
1506
+ throw new Error(`esysgov cycle ${id} already exists`);
1507
+ if (!_esysgovPsV2.has(profileId))
1508
+ throw new Error(`esysgov profile ${profileId} not found`);
1509
+ if (_esysgovCountPending(profileId) >= _esysgovMaxPending)
1510
+ throw new Error(
1511
+ `max pending esysgov cycles for profile ${profileId} reached`,
1512
+ );
1513
+ const now = Date.now();
1514
+ const j = {
1515
+ id,
1516
+ profileId,
1517
+ cycleId: cycleId || "",
1518
+ status: ESYSGOV_CYCLE_LIFECYCLE_V2.QUEUED,
1519
+ createdAt: now,
1520
+ updatedAt: now,
1521
+ startedAt: null,
1522
+ settledAt: null,
1523
+ metadata: { ...(metadata || {}) },
1524
+ };
1525
+ _esysgovJsV2.set(id, j);
1526
+ return { ...j, metadata: { ...j.metadata } };
1527
+ }
1528
+ export function evolvingEsysCycleV2(id) {
1529
+ const j = _esysgovJsV2.get(id);
1530
+ if (!j) throw new Error(`esysgov cycle ${id} not found`);
1531
+ _esysgovCheckJ(j.status, ESYSGOV_CYCLE_LIFECYCLE_V2.EVOLVING);
1532
+ const now = Date.now();
1533
+ j.status = ESYSGOV_CYCLE_LIFECYCLE_V2.EVOLVING;
1534
+ j.updatedAt = now;
1535
+ if (!j.startedAt) j.startedAt = now;
1536
+ return { ...j, metadata: { ...j.metadata } };
1537
+ }
1538
+ export function completeCycleEsysV2(id) {
1539
+ const j = _esysgovJsV2.get(id);
1540
+ if (!j) throw new Error(`esysgov cycle ${id} not found`);
1541
+ _esysgovCheckJ(j.status, ESYSGOV_CYCLE_LIFECYCLE_V2.EVOLVED);
1542
+ const now = Date.now();
1543
+ j.status = ESYSGOV_CYCLE_LIFECYCLE_V2.EVOLVED;
1544
+ j.updatedAt = now;
1545
+ if (!j.settledAt) j.settledAt = now;
1546
+ return { ...j, metadata: { ...j.metadata } };
1547
+ }
1548
+ export function failEsysCycleV2(id, reason) {
1549
+ const j = _esysgovJsV2.get(id);
1550
+ if (!j) throw new Error(`esysgov cycle ${id} not found`);
1551
+ _esysgovCheckJ(j.status, ESYSGOV_CYCLE_LIFECYCLE_V2.FAILED);
1552
+ const now = Date.now();
1553
+ j.status = ESYSGOV_CYCLE_LIFECYCLE_V2.FAILED;
1554
+ j.updatedAt = now;
1555
+ if (!j.settledAt) j.settledAt = now;
1556
+ if (reason) j.metadata.failReason = String(reason);
1557
+ return { ...j, metadata: { ...j.metadata } };
1558
+ }
1559
+ export function cancelEsysCycleV2(id, reason) {
1560
+ const j = _esysgovJsV2.get(id);
1561
+ if (!j) throw new Error(`esysgov cycle ${id} not found`);
1562
+ _esysgovCheckJ(j.status, ESYSGOV_CYCLE_LIFECYCLE_V2.CANCELLED);
1563
+ const now = Date.now();
1564
+ j.status = ESYSGOV_CYCLE_LIFECYCLE_V2.CANCELLED;
1565
+ j.updatedAt = now;
1566
+ if (!j.settledAt) j.settledAt = now;
1567
+ if (reason) j.metadata.cancelReason = String(reason);
1568
+ return { ...j, metadata: { ...j.metadata } };
1569
+ }
1570
+ export function getEsysCycleV2(id) {
1571
+ const j = _esysgovJsV2.get(id);
1572
+ if (!j) return null;
1573
+ return { ...j, metadata: { ...j.metadata } };
1574
+ }
1575
+ export function listEsysCyclesV2() {
1576
+ return [..._esysgovJsV2.values()].map((j) => ({
1577
+ ...j,
1578
+ metadata: { ...j.metadata },
1579
+ }));
1580
+ }
1581
+ export function autoPausedIdleEsysProfilesV2({ now } = {}) {
1582
+ const t = now ?? Date.now();
1583
+ const flipped = [];
1584
+ for (const p of _esysgovPsV2.values())
1585
+ if (
1586
+ p.status === ESYSGOV_PROFILE_MATURITY_V2.ACTIVE &&
1587
+ t - p.lastTouchedAt >= _esysgovIdleMs
1588
+ ) {
1589
+ p.status = ESYSGOV_PROFILE_MATURITY_V2.PAUSED;
1590
+ p.updatedAt = t;
1591
+ flipped.push(p.id);
1592
+ }
1593
+ return { flipped, count: flipped.length };
1594
+ }
1595
+ export function autoFailStuckEsysCyclesV2({ now } = {}) {
1596
+ const t = now ?? Date.now();
1597
+ const flipped = [];
1598
+ for (const j of _esysgovJsV2.values())
1599
+ if (
1600
+ j.status === ESYSGOV_CYCLE_LIFECYCLE_V2.EVOLVING &&
1601
+ j.startedAt != null &&
1602
+ t - j.startedAt >= _esysgovStuckMs
1603
+ ) {
1604
+ j.status = ESYSGOV_CYCLE_LIFECYCLE_V2.FAILED;
1605
+ j.updatedAt = t;
1606
+ if (!j.settledAt) j.settledAt = t;
1607
+ j.metadata.failReason = "auto-fail-stuck";
1608
+ flipped.push(j.id);
1609
+ }
1610
+ return { flipped, count: flipped.length };
1611
+ }
1612
+ export function getEsysgovStatsV2() {
1613
+ const profilesByStatus = {};
1614
+ for (const v of Object.values(ESYSGOV_PROFILE_MATURITY_V2))
1615
+ profilesByStatus[v] = 0;
1616
+ for (const p of _esysgovPsV2.values()) profilesByStatus[p.status]++;
1617
+ const cyclesByStatus = {};
1618
+ for (const v of Object.values(ESYSGOV_CYCLE_LIFECYCLE_V2))
1619
+ cyclesByStatus[v] = 0;
1620
+ for (const j of _esysgovJsV2.values()) cyclesByStatus[j.status]++;
1621
+ return {
1622
+ totalEsysProfilesV2: _esysgovPsV2.size,
1623
+ totalEsysCyclesV2: _esysgovJsV2.size,
1624
+ maxActiveEsysProfilesPerOwner: _esysgovMaxActive,
1625
+ maxPendingEsysCyclesPerProfile: _esysgovMaxPending,
1626
+ esysgovProfileIdleMs: _esysgovIdleMs,
1627
+ esysgovCycleStuckMs: _esysgovStuckMs,
1628
+ profilesByStatus,
1629
+ cyclesByStatus,
1630
+ };
1631
+ }