chainlesschain 0.81.0 → 0.132.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 (110) hide show
  1. package/bin/chainlesschain.js +0 -0
  2. package/package.json +1 -1
  3. package/src/commands/agent-network.js +254 -1
  4. package/src/commands/audit.js +302 -0
  5. package/src/commands/automation.js +271 -1
  6. package/src/commands/codegen.js +224 -0
  7. package/src/commands/collab.js +341 -0
  8. package/src/commands/compliance.js +1035 -0
  9. package/src/commands/cowork.js +221 -0
  10. package/src/commands/dbevo.js +284 -0
  11. package/src/commands/dev.js +252 -0
  12. package/src/commands/did.js +358 -0
  13. package/src/commands/encrypt.js +341 -0
  14. package/src/commands/export.js +256 -1
  15. package/src/commands/fusion.js +258 -0
  16. package/src/commands/governance.js +325 -0
  17. package/src/commands/hardening.js +411 -0
  18. package/src/commands/hook.js +148 -0
  19. package/src/commands/import.js +252 -0
  20. package/src/commands/incentive.js +322 -0
  21. package/src/commands/infra.js +244 -0
  22. package/src/commands/instinct.js +260 -0
  23. package/src/commands/ipfs.js +318 -0
  24. package/src/commands/kg.js +387 -0
  25. package/src/commands/llm.js +263 -0
  26. package/src/commands/mcp.js +221 -0
  27. package/src/commands/memory.js +248 -0
  28. package/src/commands/multimodal.js +296 -0
  29. package/src/commands/nlprog.js +356 -0
  30. package/src/commands/note.js +244 -0
  31. package/src/commands/ops.js +354 -0
  32. package/src/commands/orchestrate.js +166 -0
  33. package/src/commands/org.js +277 -0
  34. package/src/commands/p2p.js +390 -0
  35. package/src/commands/perception.js +290 -0
  36. package/src/commands/permmem.js +251 -0
  37. package/src/commands/plugin-ecosystem.js +273 -0
  38. package/src/commands/pqc.js +393 -0
  39. package/src/commands/quantization.js +351 -0
  40. package/src/commands/rcache.js +271 -0
  41. package/src/commands/recommend.js +340 -0
  42. package/src/commands/runtime.js +307 -0
  43. package/src/commands/scim.js +262 -0
  44. package/src/commands/session.js +258 -0
  45. package/src/commands/skill.js +267 -1
  46. package/src/commands/social.js +256 -0
  47. package/src/commands/sso.js +186 -1
  48. package/src/commands/sync.js +256 -0
  49. package/src/commands/tech.js +338 -0
  50. package/src/commands/tenant.js +351 -0
  51. package/src/commands/tokens.js +269 -0
  52. package/src/commands/trust.js +249 -0
  53. package/src/commands/wallet.js +277 -0
  54. package/src/commands/workflow.js +171 -0
  55. package/src/index.js +4 -0
  56. package/src/lib/agent-coordinator.js +325 -0
  57. package/src/lib/agent-network.js +387 -0
  58. package/src/lib/agent-router.js +395 -0
  59. package/src/lib/aiops.js +478 -0
  60. package/src/lib/audit-logger.js +379 -0
  61. package/src/lib/automation-engine.js +330 -0
  62. package/src/lib/autonomous-developer.js +350 -0
  63. package/src/lib/code-agent.js +323 -0
  64. package/src/lib/collaboration-governance.js +364 -0
  65. package/src/lib/community-governance.js +436 -0
  66. package/src/lib/compliance-manager.js +434 -0
  67. package/src/lib/content-recommendation.js +469 -0
  68. package/src/lib/crypto-manager.js +350 -0
  69. package/src/lib/dbevo.js +338 -0
  70. package/src/lib/decentral-infra.js +340 -0
  71. package/src/lib/did-manager.js +367 -0
  72. package/src/lib/hardening-manager.js +348 -0
  73. package/src/lib/hook-manager.js +380 -0
  74. package/src/lib/instinct-manager.js +332 -0
  75. package/src/lib/ipfs-storage.js +334 -0
  76. package/src/lib/knowledge-exporter.js +381 -0
  77. package/src/lib/knowledge-graph.js +432 -0
  78. package/src/lib/knowledge-importer.js +379 -0
  79. package/src/lib/llm-providers.js +391 -0
  80. package/src/lib/mcp-registry.js +333 -0
  81. package/src/lib/memory-manager.js +330 -0
  82. package/src/lib/multimodal.js +346 -0
  83. package/src/lib/nl-programming.js +343 -0
  84. package/src/lib/note-versioning.js +327 -0
  85. package/src/lib/org-manager.js +323 -0
  86. package/src/lib/p2p-manager.js +387 -0
  87. package/src/lib/perception.js +346 -0
  88. package/src/lib/perf-tuning.js +4 -1
  89. package/src/lib/permanent-memory.js +320 -0
  90. package/src/lib/plugin-ecosystem.js +377 -0
  91. package/src/lib/pqc-manager.js +368 -0
  92. package/src/lib/protocol-fusion.js +417 -0
  93. package/src/lib/quantization.js +325 -0
  94. package/src/lib/response-cache.js +327 -0
  95. package/src/lib/scim-manager.js +329 -0
  96. package/src/lib/session-manager.js +329 -0
  97. package/src/lib/skill-loader.js +377 -0
  98. package/src/lib/social-manager.js +326 -0
  99. package/src/lib/sso-manager.js +332 -0
  100. package/src/lib/sync-manager.js +326 -0
  101. package/src/lib/tech-learning-engine.js +369 -0
  102. package/src/lib/tenant-saas.js +460 -0
  103. package/src/lib/threat-intel.js +335 -0
  104. package/src/lib/token-incentive.js +293 -0
  105. package/src/lib/token-tracker.js +329 -0
  106. package/src/lib/trust-security.js +390 -0
  107. package/src/lib/ueba.js +389 -0
  108. package/src/lib/universal-runtime.js +325 -0
  109. package/src/lib/wallet-manager.js +326 -0
  110. package/src/lib/workflow-engine.js +322 -0
@@ -244,3 +244,353 @@ export function getEncryptedFileInfo(filePath) {
244
244
  modified: stats.mtime.toISOString(),
245
245
  };
246
246
  }
247
+
248
+ /* ─────────────────────────────────────────────────────────────────────────
249
+ * V2 in-memory governance layer (independent of file/buffer crypto helpers)
250
+ * ───────────────────────────────────────────────────────────────────────── */
251
+
252
+ export const KEY_MATURITY_V2 = Object.freeze({
253
+ PENDING: "pending",
254
+ ACTIVE: "active",
255
+ ROTATED: "rotated",
256
+ RETIRED: "retired",
257
+ });
258
+
259
+ export const CRYPTO_JOB_LIFECYCLE_V2 = Object.freeze({
260
+ QUEUED: "queued",
261
+ RUNNING: "running",
262
+ COMPLETED: "completed",
263
+ FAILED: "failed",
264
+ CANCELLED: "cancelled",
265
+ });
266
+
267
+ const KEY_TERMINAL_V2 = new Set([KEY_MATURITY_V2.RETIRED]);
268
+ const JOB_TERMINAL_V2 = new Set([
269
+ CRYPTO_JOB_LIFECYCLE_V2.COMPLETED,
270
+ CRYPTO_JOB_LIFECYCLE_V2.FAILED,
271
+ CRYPTO_JOB_LIFECYCLE_V2.CANCELLED,
272
+ ]);
273
+
274
+ const KEY_TRANSITIONS_V2 = new Map([
275
+ [
276
+ KEY_MATURITY_V2.PENDING,
277
+ new Set([KEY_MATURITY_V2.ACTIVE, KEY_MATURITY_V2.RETIRED]),
278
+ ],
279
+ [
280
+ KEY_MATURITY_V2.ACTIVE,
281
+ new Set([KEY_MATURITY_V2.ROTATED, KEY_MATURITY_V2.RETIRED]),
282
+ ],
283
+ [
284
+ KEY_MATURITY_V2.ROTATED,
285
+ new Set([KEY_MATURITY_V2.ACTIVE, KEY_MATURITY_V2.RETIRED]),
286
+ ],
287
+ ]);
288
+
289
+ const JOB_TRANSITIONS_V2 = new Map([
290
+ [
291
+ CRYPTO_JOB_LIFECYCLE_V2.QUEUED,
292
+ new Set([
293
+ CRYPTO_JOB_LIFECYCLE_V2.RUNNING,
294
+ CRYPTO_JOB_LIFECYCLE_V2.CANCELLED,
295
+ ]),
296
+ ],
297
+ [
298
+ CRYPTO_JOB_LIFECYCLE_V2.RUNNING,
299
+ new Set([
300
+ CRYPTO_JOB_LIFECYCLE_V2.COMPLETED,
301
+ CRYPTO_JOB_LIFECYCLE_V2.FAILED,
302
+ CRYPTO_JOB_LIFECYCLE_V2.CANCELLED,
303
+ ]),
304
+ ],
305
+ ]);
306
+
307
+ export const CRYPTO_DEFAULT_MAX_ACTIVE_KEYS_PER_OWNER = 12;
308
+ export const CRYPTO_DEFAULT_MAX_PENDING_JOBS_PER_KEY = 16;
309
+ export const CRYPTO_DEFAULT_KEY_IDLE_MS = 14 * 24 * 60 * 60 * 1000;
310
+ export const CRYPTO_DEFAULT_JOB_STUCK_MS = 5 * 60 * 1000;
311
+
312
+ let _maxActiveKeysPerOwnerV2 = CRYPTO_DEFAULT_MAX_ACTIVE_KEYS_PER_OWNER;
313
+ let _maxPendingJobsPerKeyV2 = CRYPTO_DEFAULT_MAX_PENDING_JOBS_PER_KEY;
314
+ let _keyIdleMsV2 = CRYPTO_DEFAULT_KEY_IDLE_MS;
315
+ let _jobStuckMsV2 = CRYPTO_DEFAULT_JOB_STUCK_MS;
316
+
317
+ const _keysV2 = new Map();
318
+ const _jobsV2 = new Map();
319
+
320
+ function _posIntCryptoV2(n, label) {
321
+ if (typeof n !== "number" || !Number.isFinite(n) || n <= 0) {
322
+ throw new Error(`${label} must be a positive number`);
323
+ }
324
+ return Math.floor(n);
325
+ }
326
+
327
+ export function getMaxActiveKeysPerOwnerV2() {
328
+ return _maxActiveKeysPerOwnerV2;
329
+ }
330
+ export function setMaxActiveKeysPerOwnerV2(n) {
331
+ _maxActiveKeysPerOwnerV2 = _posIntCryptoV2(n, "maxActiveKeysPerOwner");
332
+ return _maxActiveKeysPerOwnerV2;
333
+ }
334
+ export function getMaxPendingJobsPerKeyV2() {
335
+ return _maxPendingJobsPerKeyV2;
336
+ }
337
+ export function setMaxPendingJobsPerKeyV2(n) {
338
+ _maxPendingJobsPerKeyV2 = _posIntCryptoV2(n, "maxPendingJobsPerKey");
339
+ return _maxPendingJobsPerKeyV2;
340
+ }
341
+ export function getKeyIdleMsV2() {
342
+ return _keyIdleMsV2;
343
+ }
344
+ export function setKeyIdleMsV2(ms) {
345
+ _keyIdleMsV2 = _posIntCryptoV2(ms, "keyIdleMs");
346
+ return _keyIdleMsV2;
347
+ }
348
+ export function getJobStuckMsV2() {
349
+ return _jobStuckMsV2;
350
+ }
351
+ export function setJobStuckMsV2(ms) {
352
+ _jobStuckMsV2 = _posIntCryptoV2(ms, "jobStuckMs");
353
+ return _jobStuckMsV2;
354
+ }
355
+
356
+ export function getActiveKeyCountV2(ownerId) {
357
+ let n = 0;
358
+ for (const k of _keysV2.values()) {
359
+ if (k.status !== KEY_MATURITY_V2.ACTIVE) continue;
360
+ if (ownerId && k.ownerId !== ownerId) continue;
361
+ n++;
362
+ }
363
+ return n;
364
+ }
365
+
366
+ export function getPendingJobCountV2(keyId) {
367
+ let n = 0;
368
+ for (const j of _jobsV2.values()) {
369
+ if (
370
+ j.status !== CRYPTO_JOB_LIFECYCLE_V2.QUEUED &&
371
+ j.status !== CRYPTO_JOB_LIFECYCLE_V2.RUNNING
372
+ )
373
+ continue;
374
+ if (keyId && j.keyId !== keyId) continue;
375
+ n++;
376
+ }
377
+ return n;
378
+ }
379
+
380
+ function _cloneKeyCryptoV2(k) {
381
+ return { ...k, metadata: { ...k.metadata } };
382
+ }
383
+ function _cloneJobCryptoV2(j) {
384
+ return { ...j, metadata: { ...j.metadata } };
385
+ }
386
+
387
+ export function registerKeyV2(
388
+ id,
389
+ { ownerId, algorithm, purpose, metadata } = {},
390
+ ) {
391
+ if (!id || typeof id !== "string") throw new Error("key id required");
392
+ if (!ownerId || typeof ownerId !== "string")
393
+ throw new Error("ownerId required");
394
+ if (!algorithm || typeof algorithm !== "string")
395
+ throw new Error("algorithm required");
396
+ if (_keysV2.has(id)) throw new Error(`key ${id} already exists`);
397
+ const now = Date.now();
398
+ const key = {
399
+ id,
400
+ ownerId,
401
+ algorithm,
402
+ purpose: purpose || "encryption",
403
+ status: KEY_MATURITY_V2.PENDING,
404
+ createdAt: now,
405
+ activatedAt: null,
406
+ retiredAt: null,
407
+ lastSeenAt: now,
408
+ metadata: metadata ? { ...metadata } : {},
409
+ };
410
+ _keysV2.set(id, key);
411
+ return _cloneKeyCryptoV2(key);
412
+ }
413
+
414
+ export function getKeyV2(id) {
415
+ const k = _keysV2.get(id);
416
+ return k ? _cloneKeyCryptoV2(k) : null;
417
+ }
418
+
419
+ export function listKeysV2({ ownerId, status, algorithm } = {}) {
420
+ const out = [];
421
+ for (const k of _keysV2.values()) {
422
+ if (ownerId && k.ownerId !== ownerId) continue;
423
+ if (status && k.status !== status) continue;
424
+ if (algorithm && k.algorithm !== algorithm) continue;
425
+ out.push(_cloneKeyCryptoV2(k));
426
+ }
427
+ return out;
428
+ }
429
+
430
+ export function setKeyStatusV2(id, next) {
431
+ const k = _keysV2.get(id);
432
+ if (!k) throw new Error(`unknown key ${id}`);
433
+ if (KEY_TERMINAL_V2.has(k.status)) {
434
+ throw new Error(`key ${id} is terminal (${k.status})`);
435
+ }
436
+ const allowed = KEY_TRANSITIONS_V2.get(k.status);
437
+ if (!allowed || !allowed.has(next)) {
438
+ throw new Error(`invalid key transition ${k.status} -> ${next}`);
439
+ }
440
+ if (next === KEY_MATURITY_V2.ACTIVE && k.status === KEY_MATURITY_V2.PENDING) {
441
+ const owned = getActiveKeyCountV2(k.ownerId);
442
+ if (owned >= _maxActiveKeysPerOwnerV2) {
443
+ throw new Error(
444
+ `owner ${k.ownerId} active key cap reached (${_maxActiveKeysPerOwnerV2})`,
445
+ );
446
+ }
447
+ }
448
+ const now = Date.now();
449
+ k.status = next;
450
+ if (next === KEY_MATURITY_V2.ACTIVE && !k.activatedAt) k.activatedAt = now;
451
+ if (next === KEY_MATURITY_V2.RETIRED && !k.retiredAt) k.retiredAt = now;
452
+ k.lastSeenAt = now;
453
+ return _cloneKeyCryptoV2(k);
454
+ }
455
+
456
+ export function activateKeyV2(id) {
457
+ return setKeyStatusV2(id, KEY_MATURITY_V2.ACTIVE);
458
+ }
459
+ export function rotateKeyV2(id) {
460
+ return setKeyStatusV2(id, KEY_MATURITY_V2.ROTATED);
461
+ }
462
+ export function retireKeyV2(id) {
463
+ return setKeyStatusV2(id, KEY_MATURITY_V2.RETIRED);
464
+ }
465
+
466
+ export function touchKeyV2(id) {
467
+ const k = _keysV2.get(id);
468
+ if (!k) throw new Error(`unknown key ${id}`);
469
+ k.lastSeenAt = Date.now();
470
+ return _cloneKeyCryptoV2(k);
471
+ }
472
+
473
+ export function createJobV2(id, { keyId, kind, metadata } = {}) {
474
+ if (!id || typeof id !== "string") throw new Error("job id required");
475
+ if (!keyId || typeof keyId !== "string") throw new Error("keyId required");
476
+ if (_jobsV2.has(id)) throw new Error(`job ${id} already exists`);
477
+ if (!_keysV2.has(keyId)) throw new Error(`unknown key ${keyId}`);
478
+ const pending = getPendingJobCountV2(keyId);
479
+ if (pending >= _maxPendingJobsPerKeyV2) {
480
+ throw new Error(
481
+ `key ${keyId} pending job cap reached (${_maxPendingJobsPerKeyV2})`,
482
+ );
483
+ }
484
+ const now = Date.now();
485
+ const job = {
486
+ id,
487
+ keyId,
488
+ kind: kind || "encrypt",
489
+ status: CRYPTO_JOB_LIFECYCLE_V2.QUEUED,
490
+ createdAt: now,
491
+ startedAt: null,
492
+ settledAt: null,
493
+ metadata: metadata ? { ...metadata } : {},
494
+ };
495
+ _jobsV2.set(id, job);
496
+ return _cloneJobCryptoV2(job);
497
+ }
498
+
499
+ export function getJobV2(id) {
500
+ const j = _jobsV2.get(id);
501
+ return j ? _cloneJobCryptoV2(j) : null;
502
+ }
503
+
504
+ export function listJobsV2({ keyId, status, kind } = {}) {
505
+ const out = [];
506
+ for (const j of _jobsV2.values()) {
507
+ if (keyId && j.keyId !== keyId) continue;
508
+ if (status && j.status !== status) continue;
509
+ if (kind && j.kind !== kind) continue;
510
+ out.push(_cloneJobCryptoV2(j));
511
+ }
512
+ return out;
513
+ }
514
+
515
+ export function setJobStatusV2(id, next) {
516
+ const j = _jobsV2.get(id);
517
+ if (!j) throw new Error(`unknown job ${id}`);
518
+ if (JOB_TERMINAL_V2.has(j.status)) {
519
+ throw new Error(`job ${id} is terminal (${j.status})`);
520
+ }
521
+ const allowed = JOB_TRANSITIONS_V2.get(j.status);
522
+ if (!allowed || !allowed.has(next)) {
523
+ throw new Error(`invalid job transition ${j.status} -> ${next}`);
524
+ }
525
+ const now = Date.now();
526
+ j.status = next;
527
+ if (next === CRYPTO_JOB_LIFECYCLE_V2.RUNNING && !j.startedAt)
528
+ j.startedAt = now;
529
+ if (JOB_TERMINAL_V2.has(next) && !j.settledAt) j.settledAt = now;
530
+ return _cloneJobCryptoV2(j);
531
+ }
532
+
533
+ export function startJobV2(id) {
534
+ return setJobStatusV2(id, CRYPTO_JOB_LIFECYCLE_V2.RUNNING);
535
+ }
536
+ export function completeJobV2(id) {
537
+ return setJobStatusV2(id, CRYPTO_JOB_LIFECYCLE_V2.COMPLETED);
538
+ }
539
+ export function failJobV2(id) {
540
+ return setJobStatusV2(id, CRYPTO_JOB_LIFECYCLE_V2.FAILED);
541
+ }
542
+ export function cancelJobV2(id) {
543
+ return setJobStatusV2(id, CRYPTO_JOB_LIFECYCLE_V2.CANCELLED);
544
+ }
545
+
546
+ export function autoRotateIdleKeysV2({ now = Date.now() } = {}) {
547
+ const out = [];
548
+ for (const k of _keysV2.values()) {
549
+ if (k.status !== KEY_MATURITY_V2.ACTIVE) continue;
550
+ if (now - k.lastSeenAt < _keyIdleMsV2) continue;
551
+ k.status = KEY_MATURITY_V2.ROTATED;
552
+ k.lastSeenAt = now;
553
+ out.push(_cloneKeyCryptoV2(k));
554
+ }
555
+ return out;
556
+ }
557
+
558
+ export function autoFailStuckJobsV2({ now = Date.now() } = {}) {
559
+ const out = [];
560
+ for (const j of _jobsV2.values()) {
561
+ if (j.status !== CRYPTO_JOB_LIFECYCLE_V2.RUNNING) continue;
562
+ if (!j.startedAt || now - j.startedAt < _jobStuckMsV2) continue;
563
+ j.status = CRYPTO_JOB_LIFECYCLE_V2.FAILED;
564
+ j.settledAt = now;
565
+ out.push(_cloneJobCryptoV2(j));
566
+ }
567
+ return out;
568
+ }
569
+
570
+ export function getCryptoManagerStatsV2() {
571
+ const keysByStatus = {};
572
+ for (const s of Object.values(KEY_MATURITY_V2)) keysByStatus[s] = 0;
573
+ for (const k of _keysV2.values()) keysByStatus[k.status]++;
574
+ const jobsByStatus = {};
575
+ for (const s of Object.values(CRYPTO_JOB_LIFECYCLE_V2)) jobsByStatus[s] = 0;
576
+ for (const j of _jobsV2.values()) jobsByStatus[j.status]++;
577
+ return {
578
+ totalKeysV2: _keysV2.size,
579
+ totalJobsV2: _jobsV2.size,
580
+ maxActiveKeysPerOwner: _maxActiveKeysPerOwnerV2,
581
+ maxPendingJobsPerKey: _maxPendingJobsPerKeyV2,
582
+ keyIdleMs: _keyIdleMsV2,
583
+ jobStuckMs: _jobStuckMsV2,
584
+ keysByStatus,
585
+ jobsByStatus,
586
+ };
587
+ }
588
+
589
+ export function _resetStateCryptoManagerV2() {
590
+ _keysV2.clear();
591
+ _jobsV2.clear();
592
+ _maxActiveKeysPerOwnerV2 = CRYPTO_DEFAULT_MAX_ACTIVE_KEYS_PER_OWNER;
593
+ _maxPendingJobsPerKeyV2 = CRYPTO_DEFAULT_MAX_PENDING_JOBS_PER_KEY;
594
+ _keyIdleMsV2 = CRYPTO_DEFAULT_KEY_IDLE_MS;
595
+ _jobStuckMsV2 = CRYPTO_DEFAULT_JOB_STUCK_MS;
596
+ }
package/src/lib/dbevo.js CHANGED
@@ -667,3 +667,341 @@ export function _resetState() {
667
667
  _suggestions.clear();
668
668
  _slowQueryThresholdMs = 100;
669
669
  }
670
+
671
+ /* ═════════════════════════════════════════════════════════ *
672
+ * Phase 80 V2 — Schema Baseline + Migration-Run Lifecycle
673
+ * ═════════════════════════════════════════════════════════ */
674
+
675
+ export const SCHEMA_BASELINE_V2 = Object.freeze({
676
+ DRAFT: "draft",
677
+ VALIDATED: "validated",
678
+ ACTIVE: "active",
679
+ DEPRECATED: "deprecated",
680
+ RETIRED: "retired",
681
+ });
682
+
683
+ export const MIGRATION_RUN_V2 = Object.freeze({
684
+ QUEUED: "queued",
685
+ RUNNING: "running",
686
+ APPLIED: "applied",
687
+ FAILED: "failed",
688
+ ROLLED_BACK: "rolled_back",
689
+ });
690
+
691
+ const BASELINE_TRANSITIONS_V2 = new Map([
692
+ ["draft", new Set(["validated", "retired"])],
693
+ ["validated", new Set(["active", "retired"])],
694
+ ["active", new Set(["deprecated", "retired"])],
695
+ ["deprecated", new Set(["active", "retired"])],
696
+ ]);
697
+ const BASELINE_TERMINALS_V2 = new Set(["retired"]);
698
+
699
+ const RUN_TRANSITIONS_V2 = new Map([
700
+ ["queued", new Set(["running", "failed"])],
701
+ ["running", new Set(["applied", "failed", "rolled_back"])],
702
+ ["applied", new Set(["rolled_back"])],
703
+ ]);
704
+ const RUN_TERMINALS_V2 = new Set(["failed", "rolled_back"]);
705
+
706
+ export const DBEVO_DEFAULT_MAX_ACTIVE_BASELINES_PER_DB = 1;
707
+ export const DBEVO_DEFAULT_MAX_RUNNING_MIGRATIONS_PER_DB = 1;
708
+ export const DBEVO_DEFAULT_BASELINE_IDLE_MS = 180 * 86400000; // 180 days
709
+ export const DBEVO_DEFAULT_MIGRATION_STUCK_MS = 30 * 60000; // 30 minutes
710
+
711
+ let _maxActiveBaselinesPerDbV2 = DBEVO_DEFAULT_MAX_ACTIVE_BASELINES_PER_DB;
712
+ let _maxRunningMigrationsPerDbV2 = DBEVO_DEFAULT_MAX_RUNNING_MIGRATIONS_PER_DB;
713
+ let _baselineIdleMsV2 = DBEVO_DEFAULT_BASELINE_IDLE_MS;
714
+ let _migrationStuckMsV2 = DBEVO_DEFAULT_MIGRATION_STUCK_MS;
715
+
716
+ function _positiveIntV2(n, label) {
717
+ const v = Math.floor(Number(n));
718
+ if (!Number.isFinite(v) || v <= 0)
719
+ throw new Error(`${label} must be a positive integer`);
720
+ return v;
721
+ }
722
+
723
+ export function getDefaultMaxActiveBaselinesPerDbV2() {
724
+ return DBEVO_DEFAULT_MAX_ACTIVE_BASELINES_PER_DB;
725
+ }
726
+ export function getMaxActiveBaselinesPerDbV2() {
727
+ return _maxActiveBaselinesPerDbV2;
728
+ }
729
+ export function setMaxActiveBaselinesPerDbV2(n) {
730
+ return (_maxActiveBaselinesPerDbV2 = _positiveIntV2(
731
+ n,
732
+ "maxActiveBaselinesPerDb",
733
+ ));
734
+ }
735
+ export function getDefaultMaxRunningMigrationsPerDbV2() {
736
+ return DBEVO_DEFAULT_MAX_RUNNING_MIGRATIONS_PER_DB;
737
+ }
738
+ export function getMaxRunningMigrationsPerDbV2() {
739
+ return _maxRunningMigrationsPerDbV2;
740
+ }
741
+ export function setMaxRunningMigrationsPerDbV2(n) {
742
+ return (_maxRunningMigrationsPerDbV2 = _positiveIntV2(
743
+ n,
744
+ "maxRunningMigrationsPerDb",
745
+ ));
746
+ }
747
+ export function getDefaultBaselineIdleMsV2() {
748
+ return DBEVO_DEFAULT_BASELINE_IDLE_MS;
749
+ }
750
+ export function getBaselineIdleMsV2() {
751
+ return _baselineIdleMsV2;
752
+ }
753
+ export function setBaselineIdleMsV2(ms) {
754
+ return (_baselineIdleMsV2 = _positiveIntV2(ms, "baselineIdleMs"));
755
+ }
756
+ export function getDefaultMigrationStuckMsV2() {
757
+ return DBEVO_DEFAULT_MIGRATION_STUCK_MS;
758
+ }
759
+ export function getMigrationStuckMsV2() {
760
+ return _migrationStuckMsV2;
761
+ }
762
+ export function setMigrationStuckMsV2(ms) {
763
+ return (_migrationStuckMsV2 = _positiveIntV2(ms, "migrationStuckMs"));
764
+ }
765
+
766
+ const _baselinesV2 = new Map();
767
+ const _runsV2 = new Map();
768
+
769
+ export function registerBaselineV2(
770
+ _db,
771
+ { baselineId, databaseId, version, initialStatus, metadata } = {},
772
+ ) {
773
+ if (!baselineId) throw new Error("baselineId is required");
774
+ if (!databaseId) throw new Error("databaseId is required");
775
+ if (!version) throw new Error("version is required");
776
+ if (_baselinesV2.has(baselineId))
777
+ throw new Error(`Baseline ${baselineId} already exists`);
778
+ const status = initialStatus || SCHEMA_BASELINE_V2.DRAFT;
779
+ if (!Object.values(SCHEMA_BASELINE_V2).includes(status))
780
+ throw new Error(`Invalid initial status: ${status}`);
781
+ if (BASELINE_TERMINALS_V2.has(status))
782
+ throw new Error(`Cannot register in terminal status: ${status}`);
783
+ if (status === SCHEMA_BASELINE_V2.ACTIVE) {
784
+ if (getActiveBaselineCount(databaseId) >= _maxActiveBaselinesPerDbV2)
785
+ throw new Error(
786
+ `Database ${databaseId} reached active-baseline cap (${_maxActiveBaselinesPerDbV2})`,
787
+ );
788
+ }
789
+ const now = Date.now();
790
+ const record = {
791
+ baselineId,
792
+ databaseId,
793
+ version,
794
+ status,
795
+ metadata: metadata || {},
796
+ createdAt: now,
797
+ updatedAt: now,
798
+ lastTouchedAt: now,
799
+ };
800
+ _baselinesV2.set(baselineId, record);
801
+ return { ...record, metadata: { ...record.metadata } };
802
+ }
803
+
804
+ export function getBaselineV2(baselineId) {
805
+ const r = _baselinesV2.get(baselineId);
806
+ return r ? { ...r, metadata: { ...r.metadata } } : null;
807
+ }
808
+
809
+ export function setBaselineStatusV2(_db, baselineId, newStatus, patch = {}) {
810
+ const record = _baselinesV2.get(baselineId);
811
+ if (!record) throw new Error(`Unknown baseline: ${baselineId}`);
812
+ if (!Object.values(SCHEMA_BASELINE_V2).includes(newStatus))
813
+ throw new Error(`Invalid status: ${newStatus}`);
814
+ const allowed = BASELINE_TRANSITIONS_V2.get(record.status) || new Set();
815
+ if (!allowed.has(newStatus))
816
+ throw new Error(`Invalid transition: ${record.status} -> ${newStatus}`);
817
+ if (newStatus === SCHEMA_BASELINE_V2.ACTIVE) {
818
+ if (getActiveBaselineCount(record.databaseId) >= _maxActiveBaselinesPerDbV2)
819
+ throw new Error(
820
+ `Database ${record.databaseId} reached active-baseline cap (${_maxActiveBaselinesPerDbV2})`,
821
+ );
822
+ }
823
+ record.status = newStatus;
824
+ record.updatedAt = Date.now();
825
+ if (patch.reason !== undefined) record.lastReason = patch.reason;
826
+ if (patch.metadata)
827
+ record.metadata = { ...record.metadata, ...patch.metadata };
828
+ return { ...record, metadata: { ...record.metadata } };
829
+ }
830
+
831
+ export function validateBaseline(db, id, reason) {
832
+ return setBaselineStatusV2(db, id, SCHEMA_BASELINE_V2.VALIDATED, { reason });
833
+ }
834
+ export function activateBaseline(db, id, reason) {
835
+ return setBaselineStatusV2(db, id, SCHEMA_BASELINE_V2.ACTIVE, { reason });
836
+ }
837
+ export function deprecateBaseline(db, id, reason) {
838
+ return setBaselineStatusV2(db, id, SCHEMA_BASELINE_V2.DEPRECATED, { reason });
839
+ }
840
+ export function retireBaseline(db, id, reason) {
841
+ return setBaselineStatusV2(db, id, SCHEMA_BASELINE_V2.RETIRED, { reason });
842
+ }
843
+
844
+ export function touchBaselineActivity(baselineId) {
845
+ const record = _baselinesV2.get(baselineId);
846
+ if (!record) throw new Error(`Unknown baseline: ${baselineId}`);
847
+ record.lastTouchedAt = Date.now();
848
+ record.updatedAt = record.lastTouchedAt;
849
+ return { ...record, metadata: { ...record.metadata } };
850
+ }
851
+
852
+ export function enqueueMigrationRunV2(
853
+ _db,
854
+ { runId, databaseId, migrationId, direction, metadata } = {},
855
+ ) {
856
+ if (!runId) throw new Error("runId is required");
857
+ if (!databaseId) throw new Error("databaseId is required");
858
+ if (!migrationId) throw new Error("migrationId is required");
859
+ if (!direction) throw new Error("direction is required");
860
+ if (!Object.values(MIGRATION_DIRECTION).includes(direction))
861
+ throw new Error(`Invalid direction: ${direction}`);
862
+ if (_runsV2.has(runId)) throw new Error(`Run ${runId} already exists`);
863
+ const now = Date.now();
864
+ const record = {
865
+ runId,
866
+ databaseId,
867
+ migrationId,
868
+ direction,
869
+ status: MIGRATION_RUN_V2.QUEUED,
870
+ metadata: metadata || {},
871
+ createdAt: now,
872
+ updatedAt: now,
873
+ };
874
+ _runsV2.set(runId, record);
875
+ return { ...record, metadata: { ...record.metadata } };
876
+ }
877
+
878
+ export function getMigrationRunV2(runId) {
879
+ const r = _runsV2.get(runId);
880
+ return r ? { ...r, metadata: { ...r.metadata } } : null;
881
+ }
882
+
883
+ export function setMigrationRunStatusV2(_db, runId, newStatus, patch = {}) {
884
+ const record = _runsV2.get(runId);
885
+ if (!record) throw new Error(`Unknown run: ${runId}`);
886
+ if (!Object.values(MIGRATION_RUN_V2).includes(newStatus))
887
+ throw new Error(`Invalid status: ${newStatus}`);
888
+ const allowed = RUN_TRANSITIONS_V2.get(record.status) || new Set();
889
+ if (!allowed.has(newStatus))
890
+ throw new Error(`Invalid transition: ${record.status} -> ${newStatus}`);
891
+ if (newStatus === MIGRATION_RUN_V2.RUNNING) {
892
+ if (
893
+ getRunningMigrationCount(record.databaseId) >=
894
+ _maxRunningMigrationsPerDbV2
895
+ )
896
+ throw new Error(
897
+ `Database ${record.databaseId} reached running-migration cap (${_maxRunningMigrationsPerDbV2})`,
898
+ );
899
+ if (!record.startedAt) record.startedAt = Date.now();
900
+ }
901
+ record.status = newStatus;
902
+ record.updatedAt = Date.now();
903
+ if (patch.reason !== undefined) record.lastReason = patch.reason;
904
+ if (patch.metadata)
905
+ record.metadata = { ...record.metadata, ...patch.metadata };
906
+ return { ...record, metadata: { ...record.metadata } };
907
+ }
908
+
909
+ export function startMigrationRun(db, id, reason) {
910
+ return setMigrationRunStatusV2(db, id, MIGRATION_RUN_V2.RUNNING, { reason });
911
+ }
912
+ export function applyMigrationRun(db, id, reason) {
913
+ return setMigrationRunStatusV2(db, id, MIGRATION_RUN_V2.APPLIED, { reason });
914
+ }
915
+ export function failMigrationRun(db, id, reason) {
916
+ return setMigrationRunStatusV2(db, id, MIGRATION_RUN_V2.FAILED, { reason });
917
+ }
918
+ export function rollbackMigrationRun(db, id, reason) {
919
+ return setMigrationRunStatusV2(db, id, MIGRATION_RUN_V2.ROLLED_BACK, {
920
+ reason,
921
+ });
922
+ }
923
+
924
+ export function getActiveBaselineCount(databaseId) {
925
+ let n = 0;
926
+ for (const r of _baselinesV2.values()) {
927
+ if (r.status !== SCHEMA_BASELINE_V2.ACTIVE) continue;
928
+ if (databaseId && r.databaseId !== databaseId) continue;
929
+ n++;
930
+ }
931
+ return n;
932
+ }
933
+
934
+ export function getRunningMigrationCount(databaseId) {
935
+ let n = 0;
936
+ for (const r of _runsV2.values()) {
937
+ if (r.status !== MIGRATION_RUN_V2.RUNNING) continue;
938
+ if (databaseId && r.databaseId !== databaseId) continue;
939
+ n++;
940
+ }
941
+ return n;
942
+ }
943
+
944
+ export function autoRetireIdleBaselines(_db, nowMs) {
945
+ const now = nowMs ?? Date.now();
946
+ const flipped = [];
947
+ for (const r of _baselinesV2.values()) {
948
+ if (
949
+ r.status === SCHEMA_BASELINE_V2.DEPRECATED ||
950
+ r.status === SCHEMA_BASELINE_V2.DRAFT ||
951
+ r.status === SCHEMA_BASELINE_V2.VALIDATED
952
+ ) {
953
+ if (now - r.lastTouchedAt > _baselineIdleMsV2) {
954
+ r.status = SCHEMA_BASELINE_V2.RETIRED;
955
+ r.updatedAt = now;
956
+ r.lastReason = "idle_timeout";
957
+ flipped.push(r.baselineId);
958
+ }
959
+ }
960
+ }
961
+ return { flipped, count: flipped.length };
962
+ }
963
+
964
+ export function autoFailStuckMigrationRuns(_db, nowMs) {
965
+ const now = nowMs ?? Date.now();
966
+ const flipped = [];
967
+ for (const r of _runsV2.values()) {
968
+ if (r.status === MIGRATION_RUN_V2.RUNNING) {
969
+ const anchor = r.startedAt || r.createdAt;
970
+ if (now - anchor > _migrationStuckMsV2) {
971
+ r.status = MIGRATION_RUN_V2.FAILED;
972
+ r.updatedAt = now;
973
+ r.lastReason = "migration_timeout";
974
+ flipped.push(r.runId);
975
+ }
976
+ }
977
+ }
978
+ return { flipped, count: flipped.length };
979
+ }
980
+
981
+ export function getDbEvoStatsV2() {
982
+ const baselinesByStatus = {};
983
+ for (const s of Object.values(SCHEMA_BASELINE_V2)) baselinesByStatus[s] = 0;
984
+ const runsByStatus = {};
985
+ for (const s of Object.values(MIGRATION_RUN_V2)) runsByStatus[s] = 0;
986
+ for (const r of _baselinesV2.values()) baselinesByStatus[r.status]++;
987
+ for (const r of _runsV2.values()) runsByStatus[r.status]++;
988
+ return {
989
+ totalBaselinesV2: _baselinesV2.size,
990
+ totalRunsV2: _runsV2.size,
991
+ maxActiveBaselinesPerDb: _maxActiveBaselinesPerDbV2,
992
+ maxRunningMigrationsPerDb: _maxRunningMigrationsPerDbV2,
993
+ baselineIdleMs: _baselineIdleMsV2,
994
+ migrationStuckMs: _migrationStuckMsV2,
995
+ baselinesByStatus,
996
+ runsByStatus,
997
+ };
998
+ }
999
+
1000
+ export function _resetStateV2() {
1001
+ _maxActiveBaselinesPerDbV2 = DBEVO_DEFAULT_MAX_ACTIVE_BASELINES_PER_DB;
1002
+ _maxRunningMigrationsPerDbV2 = DBEVO_DEFAULT_MAX_RUNNING_MIGRATIONS_PER_DB;
1003
+ _baselineIdleMsV2 = DBEVO_DEFAULT_BASELINE_IDLE_MS;
1004
+ _migrationStuckMsV2 = DBEVO_DEFAULT_MIGRATION_STUCK_MS;
1005
+ _baselinesV2.clear();
1006
+ _runsV2.clear();
1007
+ }