chainlesschain 0.66.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.
- package/bin/chainlesschain.js +0 -0
- package/package.json +1 -1
- package/src/commands/a2a.js +380 -0
- package/src/commands/agent-network.js +254 -1
- package/src/commands/audit.js +302 -0
- package/src/commands/automation.js +271 -1
- package/src/commands/bi.js +348 -0
- package/src/commands/codegen.js +224 -0
- package/src/commands/collab.js +341 -0
- package/src/commands/compliance.js +1035 -0
- package/src/commands/cowork.js +221 -0
- package/src/commands/crosschain.js +218 -0
- package/src/commands/dbevo.js +284 -0
- package/src/commands/dev.js +252 -0
- package/src/commands/did.js +358 -0
- package/src/commands/dlp.js +341 -0
- package/src/commands/encrypt.js +341 -0
- package/src/commands/evomap.js +394 -0
- package/src/commands/export.js +256 -1
- package/src/commands/federation.js +283 -0
- package/src/commands/fusion.js +258 -0
- package/src/commands/governance.js +325 -0
- package/src/commands/hardening.js +411 -0
- package/src/commands/hook.js +148 -0
- package/src/commands/import.js +252 -0
- package/src/commands/incentive.js +322 -0
- package/src/commands/inference.js +318 -0
- package/src/commands/infra.js +244 -0
- package/src/commands/instinct.js +260 -0
- package/src/commands/ipfs.js +318 -0
- package/src/commands/kg.js +387 -0
- package/src/commands/llm.js +263 -0
- package/src/commands/lowcode.js +356 -0
- package/src/commands/marketplace.js +256 -0
- package/src/commands/mcp.js +221 -0
- package/src/commands/memory.js +248 -0
- package/src/commands/multimodal.js +296 -0
- package/src/commands/nlprog.js +356 -0
- package/src/commands/note.js +244 -0
- package/src/commands/ops.js +354 -0
- package/src/commands/orchestrate.js +166 -0
- package/src/commands/org.js +277 -0
- package/src/commands/p2p.js +390 -0
- package/src/commands/perception.js +290 -0
- package/src/commands/permmem.js +251 -0
- package/src/commands/plugin-ecosystem.js +273 -0
- package/src/commands/pqc.js +393 -0
- package/src/commands/privacy.js +321 -0
- package/src/commands/quantization.js +351 -0
- package/src/commands/rcache.js +271 -0
- package/src/commands/recommend.js +340 -0
- package/src/commands/reputation.js +261 -0
- package/src/commands/runtime.js +307 -0
- package/src/commands/scim.js +262 -0
- package/src/commands/session.js +258 -0
- package/src/commands/siem.js +246 -0
- package/src/commands/skill.js +267 -1
- package/src/commands/sla.js +259 -0
- package/src/commands/social.js +256 -0
- package/src/commands/sso.js +186 -1
- package/src/commands/stress.js +230 -0
- package/src/commands/sync.js +256 -0
- package/src/commands/tech.js +338 -0
- package/src/commands/tenant.js +351 -0
- package/src/commands/terraform.js +245 -0
- package/src/commands/tokens.js +269 -0
- package/src/commands/trust.js +249 -0
- package/src/commands/wallet.js +277 -0
- package/src/commands/workflow.js +171 -0
- package/src/commands/zkp.js +335 -0
- package/src/index.js +4 -0
- package/src/lib/a2a-protocol.js +451 -0
- package/src/lib/agent-coordinator.js +325 -0
- package/src/lib/agent-network.js +387 -0
- package/src/lib/agent-router.js +395 -0
- package/src/lib/aiops.js +478 -0
- package/src/lib/app-builder.js +239 -0
- package/src/lib/audit-logger.js +379 -0
- package/src/lib/automation-engine.js +330 -0
- package/src/lib/autonomous-developer.js +350 -0
- package/src/lib/bi-engine.js +338 -0
- package/src/lib/code-agent.js +323 -0
- package/src/lib/collaboration-governance.js +364 -0
- package/src/lib/community-governance.js +436 -0
- package/src/lib/compliance-manager.js +434 -0
- package/src/lib/content-recommendation.js +469 -0
- package/src/lib/cross-chain.js +345 -0
- package/src/lib/crypto-manager.js +350 -0
- package/src/lib/dbevo.js +338 -0
- package/src/lib/decentral-infra.js +340 -0
- package/src/lib/did-manager.js +367 -0
- package/src/lib/dlp-engine.js +389 -0
- package/src/lib/evomap-federation.js +177 -0
- package/src/lib/evomap-governance.js +276 -0
- package/src/lib/federation-hardening.js +259 -0
- package/src/lib/hardening-manager.js +348 -0
- package/src/lib/hook-manager.js +380 -0
- package/src/lib/inference-network.js +330 -0
- package/src/lib/instinct-manager.js +332 -0
- package/src/lib/ipfs-storage.js +334 -0
- package/src/lib/knowledge-exporter.js +381 -0
- package/src/lib/knowledge-graph.js +432 -0
- package/src/lib/knowledge-importer.js +379 -0
- package/src/lib/llm-providers.js +391 -0
- package/src/lib/mcp-registry.js +333 -0
- package/src/lib/memory-manager.js +330 -0
- package/src/lib/multimodal.js +346 -0
- package/src/lib/nl-programming.js +343 -0
- package/src/lib/note-versioning.js +327 -0
- package/src/lib/org-manager.js +323 -0
- package/src/lib/p2p-manager.js +387 -0
- package/src/lib/perception.js +346 -0
- package/src/lib/perf-tuning.js +4 -1
- package/src/lib/permanent-memory.js +320 -0
- package/src/lib/plugin-ecosystem.js +377 -0
- package/src/lib/pqc-manager.js +368 -0
- package/src/lib/privacy-computing.js +427 -0
- package/src/lib/protocol-fusion.js +417 -0
- package/src/lib/quantization.js +325 -0
- package/src/lib/reputation-optimizer.js +299 -0
- package/src/lib/response-cache.js +327 -0
- package/src/lib/scim-manager.js +329 -0
- package/src/lib/session-manager.js +329 -0
- package/src/lib/siem-exporter.js +333 -0
- package/src/lib/skill-loader.js +377 -0
- package/src/lib/skill-marketplace.js +325 -0
- package/src/lib/sla-manager.js +275 -0
- package/src/lib/social-manager.js +326 -0
- package/src/lib/sso-manager.js +332 -0
- package/src/lib/stress-tester.js +330 -0
- package/src/lib/sync-manager.js +326 -0
- package/src/lib/tech-learning-engine.js +369 -0
- package/src/lib/tenant-saas.js +460 -0
- package/src/lib/terraform-manager.js +363 -0
- package/src/lib/threat-intel.js +335 -0
- package/src/lib/token-incentive.js +293 -0
- package/src/lib/token-tracker.js +329 -0
- package/src/lib/trust-security.js +390 -0
- package/src/lib/ueba.js +389 -0
- package/src/lib/universal-runtime.js +325 -0
- package/src/lib/wallet-manager.js +326 -0
- package/src/lib/workflow-engine.js +322 -0
- package/src/lib/zkp-engine.js +274 -0
package/src/lib/sync-manager.js
CHANGED
|
@@ -345,3 +345,329 @@ export function clearSyncData(db) {
|
|
|
345
345
|
db.prepare("DELETE FROM sync_log").run();
|
|
346
346
|
return true;
|
|
347
347
|
}
|
|
348
|
+
|
|
349
|
+
/* ═══════════════════════════════════════════════════════════════
|
|
350
|
+
* V2 Surface — Sync Manager governance layer.
|
|
351
|
+
* Tracks tracked-resource maturity + sync-run lifecycle independent
|
|
352
|
+
* of legacy registerResource/pushResources/pullResources flows above.
|
|
353
|
+
* ═══════════════════════════════════════════════════════════════ */
|
|
354
|
+
|
|
355
|
+
export const RESOURCE_MATURITY_V2 = Object.freeze({
|
|
356
|
+
PENDING: "pending",
|
|
357
|
+
ACTIVE: "active",
|
|
358
|
+
PAUSED: "paused",
|
|
359
|
+
ARCHIVED: "archived",
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
export const SYNC_RUN_V2 = Object.freeze({
|
|
363
|
+
QUEUED: "queued",
|
|
364
|
+
RUNNING: "running",
|
|
365
|
+
SUCCEEDED: "succeeded",
|
|
366
|
+
FAILED: "failed",
|
|
367
|
+
CANCELLED: "cancelled",
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
const RESOURCE_TRANSITIONS_V2 = new Map([
|
|
371
|
+
["pending", new Set(["active", "archived"])],
|
|
372
|
+
["active", new Set(["paused", "archived"])],
|
|
373
|
+
["paused", new Set(["active", "archived"])],
|
|
374
|
+
["archived", new Set()],
|
|
375
|
+
]);
|
|
376
|
+
const RESOURCE_TERMINALS_V2 = new Set(["archived"]);
|
|
377
|
+
|
|
378
|
+
const RUN_TRANSITIONS_V2 = new Map([
|
|
379
|
+
["queued", new Set(["running", "cancelled"])],
|
|
380
|
+
["running", new Set(["succeeded", "failed", "cancelled"])],
|
|
381
|
+
["succeeded", new Set()],
|
|
382
|
+
["failed", new Set()],
|
|
383
|
+
["cancelled", new Set()],
|
|
384
|
+
]);
|
|
385
|
+
const RUN_TERMINALS_V2 = new Set(["succeeded", "failed", "cancelled"]);
|
|
386
|
+
|
|
387
|
+
export const SYNC_DEFAULT_MAX_ACTIVE_RESOURCES_PER_OWNER = 200;
|
|
388
|
+
export const SYNC_DEFAULT_MAX_RUNNING_RUNS_PER_RESOURCE = 1;
|
|
389
|
+
export const SYNC_DEFAULT_RESOURCE_IDLE_MS = 1000 * 60 * 60 * 24 * 30; // 30 days
|
|
390
|
+
export const SYNC_DEFAULT_RUN_STUCK_MS = 1000 * 60 * 15; // 15 min
|
|
391
|
+
|
|
392
|
+
const _resourcesV2 = new Map();
|
|
393
|
+
const _runsV2 = new Map();
|
|
394
|
+
let _maxActiveResourcesPerOwnerV2 = SYNC_DEFAULT_MAX_ACTIVE_RESOURCES_PER_OWNER;
|
|
395
|
+
let _maxRunningRunsPerResourceV2 = SYNC_DEFAULT_MAX_RUNNING_RUNS_PER_RESOURCE;
|
|
396
|
+
let _resourceIdleMsV2 = SYNC_DEFAULT_RESOURCE_IDLE_MS;
|
|
397
|
+
let _runStuckMsV2 = SYNC_DEFAULT_RUN_STUCK_MS;
|
|
398
|
+
|
|
399
|
+
function _posIntSyncV2(n, label) {
|
|
400
|
+
const v = Math.floor(Number(n));
|
|
401
|
+
if (!Number.isFinite(v) || v <= 0)
|
|
402
|
+
throw new Error(`${label} must be a positive integer`);
|
|
403
|
+
return v;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
export function getMaxActiveResourcesPerOwnerV2() {
|
|
407
|
+
return _maxActiveResourcesPerOwnerV2;
|
|
408
|
+
}
|
|
409
|
+
export function setMaxActiveResourcesPerOwnerV2(n) {
|
|
410
|
+
_maxActiveResourcesPerOwnerV2 = _posIntSyncV2(
|
|
411
|
+
n,
|
|
412
|
+
"maxActiveResourcesPerOwner",
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
export function getMaxRunningRunsPerResourceV2() {
|
|
416
|
+
return _maxRunningRunsPerResourceV2;
|
|
417
|
+
}
|
|
418
|
+
export function setMaxRunningRunsPerResourceV2(n) {
|
|
419
|
+
_maxRunningRunsPerResourceV2 = _posIntSyncV2(n, "maxRunningRunsPerResource");
|
|
420
|
+
}
|
|
421
|
+
export function getResourceIdleMsV2() {
|
|
422
|
+
return _resourceIdleMsV2;
|
|
423
|
+
}
|
|
424
|
+
export function setResourceIdleMsV2(n) {
|
|
425
|
+
_resourceIdleMsV2 = _posIntSyncV2(n, "resourceIdleMs");
|
|
426
|
+
}
|
|
427
|
+
export function getRunStuckMsV2() {
|
|
428
|
+
return _runStuckMsV2;
|
|
429
|
+
}
|
|
430
|
+
export function setRunStuckMsV2(n) {
|
|
431
|
+
_runStuckMsV2 = _posIntSyncV2(n, "runStuckMs");
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
export function getActiveResourceCountV2(owner) {
|
|
435
|
+
let n = 0;
|
|
436
|
+
for (const r of _resourcesV2.values()) {
|
|
437
|
+
if (r.owner === owner && r.status === "active") n += 1;
|
|
438
|
+
}
|
|
439
|
+
return n;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
export function getRunningRunCountV2(resourceId) {
|
|
443
|
+
let n = 0;
|
|
444
|
+
for (const j of _runsV2.values()) {
|
|
445
|
+
if (j.resourceId === resourceId && j.status === "running") n += 1;
|
|
446
|
+
}
|
|
447
|
+
return n;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
function _copyResV2(r) {
|
|
451
|
+
return { ...r, metadata: { ...r.metadata } };
|
|
452
|
+
}
|
|
453
|
+
function _copyRunV2(j) {
|
|
454
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
export function registerResourceV2(
|
|
458
|
+
id,
|
|
459
|
+
{ owner, kind, metadata = {}, now = Date.now() } = {},
|
|
460
|
+
) {
|
|
461
|
+
if (!id || typeof id !== "string") throw new Error("id must be a string");
|
|
462
|
+
if (!owner || typeof owner !== "string")
|
|
463
|
+
throw new Error("owner must be a string");
|
|
464
|
+
if (!kind || typeof kind !== "string")
|
|
465
|
+
throw new Error("kind must be a string");
|
|
466
|
+
if (_resourcesV2.has(id)) throw new Error(`resource ${id} already exists`);
|
|
467
|
+
const r = {
|
|
468
|
+
id,
|
|
469
|
+
owner,
|
|
470
|
+
kind,
|
|
471
|
+
status: "pending",
|
|
472
|
+
createdAt: now,
|
|
473
|
+
lastSeenAt: now,
|
|
474
|
+
activatedAt: null,
|
|
475
|
+
archivedAt: null,
|
|
476
|
+
metadata: { ...metadata },
|
|
477
|
+
};
|
|
478
|
+
_resourcesV2.set(id, r);
|
|
479
|
+
return _copyResV2(r);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
export function getResourceV2(id) {
|
|
483
|
+
const r = _resourcesV2.get(id);
|
|
484
|
+
return r ? _copyResV2(r) : null;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
export function listResourcesV2({ owner, kind, status } = {}) {
|
|
488
|
+
const out = [];
|
|
489
|
+
for (const r of _resourcesV2.values()) {
|
|
490
|
+
if (owner && r.owner !== owner) continue;
|
|
491
|
+
if (kind && r.kind !== kind) continue;
|
|
492
|
+
if (status && r.status !== status) continue;
|
|
493
|
+
out.push(_copyResV2(r));
|
|
494
|
+
}
|
|
495
|
+
return out;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
export function setResourceStatusV2(id, next, { now = Date.now() } = {}) {
|
|
499
|
+
const r = _resourcesV2.get(id);
|
|
500
|
+
if (!r) throw new Error(`resource ${id} not found`);
|
|
501
|
+
if (!RESOURCE_TRANSITIONS_V2.has(next))
|
|
502
|
+
throw new Error(`unknown resource status: ${next}`);
|
|
503
|
+
if (RESOURCE_TERMINALS_V2.has(r.status))
|
|
504
|
+
throw new Error(`resource ${id} is in terminal state ${r.status}`);
|
|
505
|
+
const allowed = RESOURCE_TRANSITIONS_V2.get(r.status);
|
|
506
|
+
if (!allowed.has(next))
|
|
507
|
+
throw new Error(`cannot transition resource from ${r.status} to ${next}`);
|
|
508
|
+
if (next === "active") {
|
|
509
|
+
if (r.status === "pending") {
|
|
510
|
+
const count = getActiveResourceCountV2(r.owner);
|
|
511
|
+
if (count >= _maxActiveResourcesPerOwnerV2)
|
|
512
|
+
throw new Error(
|
|
513
|
+
`owner ${r.owner} already at active-resource cap (${_maxActiveResourcesPerOwnerV2})`,
|
|
514
|
+
);
|
|
515
|
+
}
|
|
516
|
+
if (!r.activatedAt) r.activatedAt = now;
|
|
517
|
+
}
|
|
518
|
+
if (next === "archived" && !r.archivedAt) r.archivedAt = now;
|
|
519
|
+
r.status = next;
|
|
520
|
+
r.lastSeenAt = now;
|
|
521
|
+
return _copyResV2(r);
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
export function activateResourceV2(id, opts) {
|
|
525
|
+
return setResourceStatusV2(id, "active", opts);
|
|
526
|
+
}
|
|
527
|
+
export function pauseResourceV2(id, opts) {
|
|
528
|
+
return setResourceStatusV2(id, "paused", opts);
|
|
529
|
+
}
|
|
530
|
+
export function archiveResourceV2(id, opts) {
|
|
531
|
+
return setResourceStatusV2(id, "archived", opts);
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
export function touchResourceV2(id, { now = Date.now() } = {}) {
|
|
535
|
+
const r = _resourcesV2.get(id);
|
|
536
|
+
if (!r) throw new Error(`resource ${id} not found`);
|
|
537
|
+
r.lastSeenAt = now;
|
|
538
|
+
return _copyResV2(r);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
export function createSyncRunV2(
|
|
542
|
+
id,
|
|
543
|
+
{ resourceId, kind = "push", metadata = {}, now = Date.now() } = {},
|
|
544
|
+
) {
|
|
545
|
+
if (!id || typeof id !== "string") throw new Error("id must be a string");
|
|
546
|
+
if (!resourceId || typeof resourceId !== "string")
|
|
547
|
+
throw new Error("resourceId must be a string");
|
|
548
|
+
if (_runsV2.has(id)) throw new Error(`syncRun ${id} already exists`);
|
|
549
|
+
const j = {
|
|
550
|
+
id,
|
|
551
|
+
resourceId,
|
|
552
|
+
kind,
|
|
553
|
+
status: "queued",
|
|
554
|
+
createdAt: now,
|
|
555
|
+
lastSeenAt: now,
|
|
556
|
+
startedAt: null,
|
|
557
|
+
finishedAt: null,
|
|
558
|
+
metadata: { ...metadata },
|
|
559
|
+
};
|
|
560
|
+
_runsV2.set(id, j);
|
|
561
|
+
return _copyRunV2(j);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
export function getSyncRunV2(id) {
|
|
565
|
+
const j = _runsV2.get(id);
|
|
566
|
+
return j ? _copyRunV2(j) : null;
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
export function listSyncRunsV2({ resourceId, status } = {}) {
|
|
570
|
+
const out = [];
|
|
571
|
+
for (const j of _runsV2.values()) {
|
|
572
|
+
if (resourceId && j.resourceId !== resourceId) continue;
|
|
573
|
+
if (status && j.status !== status) continue;
|
|
574
|
+
out.push(_copyRunV2(j));
|
|
575
|
+
}
|
|
576
|
+
return out;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
export function setSyncRunStatusV2(id, next, { now = Date.now() } = {}) {
|
|
580
|
+
const j = _runsV2.get(id);
|
|
581
|
+
if (!j) throw new Error(`syncRun ${id} not found`);
|
|
582
|
+
if (!RUN_TRANSITIONS_V2.has(next))
|
|
583
|
+
throw new Error(`unknown syncRun status: ${next}`);
|
|
584
|
+
if (RUN_TERMINALS_V2.has(j.status))
|
|
585
|
+
throw new Error(`syncRun ${id} is in terminal state ${j.status}`);
|
|
586
|
+
const allowed = RUN_TRANSITIONS_V2.get(j.status);
|
|
587
|
+
if (!allowed.has(next))
|
|
588
|
+
throw new Error(`cannot transition syncRun from ${j.status} to ${next}`);
|
|
589
|
+
if (next === "running" && j.status === "queued") {
|
|
590
|
+
const count = getRunningRunCountV2(j.resourceId);
|
|
591
|
+
if (count >= _maxRunningRunsPerResourceV2)
|
|
592
|
+
throw new Error(
|
|
593
|
+
`resource ${j.resourceId} already at running-run cap (${_maxRunningRunsPerResourceV2})`,
|
|
594
|
+
);
|
|
595
|
+
if (!j.startedAt) j.startedAt = now;
|
|
596
|
+
}
|
|
597
|
+
if (RUN_TERMINALS_V2.has(next) && !j.finishedAt) j.finishedAt = now;
|
|
598
|
+
j.status = next;
|
|
599
|
+
j.lastSeenAt = now;
|
|
600
|
+
return _copyRunV2(j);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
export function startSyncRunV2(id, opts) {
|
|
604
|
+
return setSyncRunStatusV2(id, "running", opts);
|
|
605
|
+
}
|
|
606
|
+
export function succeedSyncRunV2(id, opts) {
|
|
607
|
+
return setSyncRunStatusV2(id, "succeeded", opts);
|
|
608
|
+
}
|
|
609
|
+
export function failSyncRunV2(id, opts) {
|
|
610
|
+
return setSyncRunStatusV2(id, "failed", opts);
|
|
611
|
+
}
|
|
612
|
+
export function cancelSyncRunV2(id, opts) {
|
|
613
|
+
return setSyncRunStatusV2(id, "cancelled", opts);
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
export function autoArchiveIdleResourcesV2({ now = Date.now() } = {}) {
|
|
617
|
+
const flipped = [];
|
|
618
|
+
for (const r of _resourcesV2.values()) {
|
|
619
|
+
if (r.status === "archived" || r.status === "pending") continue;
|
|
620
|
+
if (now - r.lastSeenAt > _resourceIdleMsV2) {
|
|
621
|
+
r.status = "archived";
|
|
622
|
+
r.lastSeenAt = now;
|
|
623
|
+
if (!r.archivedAt) r.archivedAt = now;
|
|
624
|
+
flipped.push(_copyResV2(r));
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
return flipped;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
export function autoFailStuckSyncRunsV2({ now = Date.now() } = {}) {
|
|
631
|
+
const flipped = [];
|
|
632
|
+
for (const j of _runsV2.values()) {
|
|
633
|
+
if (j.status !== "running") continue;
|
|
634
|
+
const ref = j.startedAt ?? j.lastSeenAt;
|
|
635
|
+
if (now - ref > _runStuckMsV2) {
|
|
636
|
+
j.status = "failed";
|
|
637
|
+
j.lastSeenAt = now;
|
|
638
|
+
if (!j.finishedAt) j.finishedAt = now;
|
|
639
|
+
flipped.push(_copyRunV2(j));
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
return flipped;
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
export function getSyncManagerStatsV2() {
|
|
646
|
+
const resourcesByStatus = {};
|
|
647
|
+
for (const v of Object.values(RESOURCE_MATURITY_V2)) resourcesByStatus[v] = 0;
|
|
648
|
+
for (const r of _resourcesV2.values()) resourcesByStatus[r.status] += 1;
|
|
649
|
+
|
|
650
|
+
const runsByStatus = {};
|
|
651
|
+
for (const v of Object.values(SYNC_RUN_V2)) runsByStatus[v] = 0;
|
|
652
|
+
for (const j of _runsV2.values()) runsByStatus[j.status] += 1;
|
|
653
|
+
|
|
654
|
+
return {
|
|
655
|
+
totalResourcesV2: _resourcesV2.size,
|
|
656
|
+
totalSyncRunsV2: _runsV2.size,
|
|
657
|
+
maxActiveResourcesPerOwner: _maxActiveResourcesPerOwnerV2,
|
|
658
|
+
maxRunningRunsPerResource: _maxRunningRunsPerResourceV2,
|
|
659
|
+
resourceIdleMs: _resourceIdleMsV2,
|
|
660
|
+
runStuckMs: _runStuckMsV2,
|
|
661
|
+
resourcesByStatus,
|
|
662
|
+
runsByStatus,
|
|
663
|
+
};
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
export function _resetStateSyncManagerV2() {
|
|
667
|
+
_resourcesV2.clear();
|
|
668
|
+
_runsV2.clear();
|
|
669
|
+
_maxActiveResourcesPerOwnerV2 = SYNC_DEFAULT_MAX_ACTIVE_RESOURCES_PER_OWNER;
|
|
670
|
+
_maxRunningRunsPerResourceV2 = SYNC_DEFAULT_MAX_RUNNING_RUNS_PER_RESOURCE;
|
|
671
|
+
_resourceIdleMsV2 = SYNC_DEFAULT_RESOURCE_IDLE_MS;
|
|
672
|
+
_runStuckMsV2 = SYNC_DEFAULT_RUN_STUCK_MS;
|
|
673
|
+
}
|
|
@@ -649,3 +649,372 @@ export function _resetState() {
|
|
|
649
649
|
_practices.clear();
|
|
650
650
|
_seq = 0;
|
|
651
651
|
}
|
|
652
|
+
|
|
653
|
+
/* ═══════════════════════════════════════════════════════════════
|
|
654
|
+
* V2 Surface — Tech Learning Engine V2 (additive)
|
|
655
|
+
* State machines + caps + auto-flip for tech profiles / learning runs
|
|
656
|
+
* ═══════════════════════════════════════════════════════════════ */
|
|
657
|
+
|
|
658
|
+
export const PROFILE_MATURITY_V2 = Object.freeze({
|
|
659
|
+
DRAFT: "draft",
|
|
660
|
+
ACTIVE: "active",
|
|
661
|
+
STALE: "stale",
|
|
662
|
+
ARCHIVED: "archived",
|
|
663
|
+
});
|
|
664
|
+
|
|
665
|
+
export const LEARNING_RUN_V2 = Object.freeze({
|
|
666
|
+
QUEUED: "queued",
|
|
667
|
+
STUDYING: "studying",
|
|
668
|
+
COMPLETED: "completed",
|
|
669
|
+
ABANDONED: "abandoned",
|
|
670
|
+
FAILED: "failed",
|
|
671
|
+
});
|
|
672
|
+
|
|
673
|
+
const _PROFILE_TRANS_V2 = new Map([
|
|
674
|
+
[
|
|
675
|
+
PROFILE_MATURITY_V2.DRAFT,
|
|
676
|
+
new Set([PROFILE_MATURITY_V2.ACTIVE, PROFILE_MATURITY_V2.ARCHIVED]),
|
|
677
|
+
],
|
|
678
|
+
[
|
|
679
|
+
PROFILE_MATURITY_V2.ACTIVE,
|
|
680
|
+
new Set([PROFILE_MATURITY_V2.STALE, PROFILE_MATURITY_V2.ARCHIVED]),
|
|
681
|
+
],
|
|
682
|
+
[
|
|
683
|
+
PROFILE_MATURITY_V2.STALE,
|
|
684
|
+
new Set([PROFILE_MATURITY_V2.ACTIVE, PROFILE_MATURITY_V2.ARCHIVED]),
|
|
685
|
+
],
|
|
686
|
+
[PROFILE_MATURITY_V2.ARCHIVED, new Set()],
|
|
687
|
+
]);
|
|
688
|
+
|
|
689
|
+
const _RUN_TRANS_V2 = new Map([
|
|
690
|
+
[
|
|
691
|
+
LEARNING_RUN_V2.QUEUED,
|
|
692
|
+
new Set([
|
|
693
|
+
LEARNING_RUN_V2.STUDYING,
|
|
694
|
+
LEARNING_RUN_V2.ABANDONED,
|
|
695
|
+
LEARNING_RUN_V2.FAILED,
|
|
696
|
+
]),
|
|
697
|
+
],
|
|
698
|
+
[
|
|
699
|
+
LEARNING_RUN_V2.STUDYING,
|
|
700
|
+
new Set([
|
|
701
|
+
LEARNING_RUN_V2.COMPLETED,
|
|
702
|
+
LEARNING_RUN_V2.FAILED,
|
|
703
|
+
LEARNING_RUN_V2.ABANDONED,
|
|
704
|
+
]),
|
|
705
|
+
],
|
|
706
|
+
[LEARNING_RUN_V2.COMPLETED, new Set()],
|
|
707
|
+
[LEARNING_RUN_V2.ABANDONED, new Set()],
|
|
708
|
+
[LEARNING_RUN_V2.FAILED, new Set()],
|
|
709
|
+
]);
|
|
710
|
+
|
|
711
|
+
const _PROFILE_TERMINAL_V2 = new Set([PROFILE_MATURITY_V2.ARCHIVED]);
|
|
712
|
+
const _RUN_TERMINAL_V2 = new Set([
|
|
713
|
+
LEARNING_RUN_V2.COMPLETED,
|
|
714
|
+
LEARNING_RUN_V2.ABANDONED,
|
|
715
|
+
LEARNING_RUN_V2.FAILED,
|
|
716
|
+
]);
|
|
717
|
+
|
|
718
|
+
export const TLE_DEFAULT_MAX_ACTIVE_PROFILES_PER_OWNER = 10;
|
|
719
|
+
export const TLE_DEFAULT_MAX_STUDYING_RUNS_PER_LEARNER = 5;
|
|
720
|
+
export const TLE_DEFAULT_PROFILE_STALE_MS = 60 * 24 * 60 * 60 * 1000;
|
|
721
|
+
export const TLE_DEFAULT_RUN_STUCK_MS = 7 * 24 * 60 * 60 * 1000;
|
|
722
|
+
|
|
723
|
+
let _tleMaxActiveProfiles = TLE_DEFAULT_MAX_ACTIVE_PROFILES_PER_OWNER;
|
|
724
|
+
let _tleMaxStudyingRuns = TLE_DEFAULT_MAX_STUDYING_RUNS_PER_LEARNER;
|
|
725
|
+
let _tleProfileStaleMs = TLE_DEFAULT_PROFILE_STALE_MS;
|
|
726
|
+
let _tleRunStuckMs = TLE_DEFAULT_RUN_STUCK_MS;
|
|
727
|
+
|
|
728
|
+
const _profilesV2 = new Map();
|
|
729
|
+
const _runsV2 = new Map();
|
|
730
|
+
|
|
731
|
+
function _posIntV2(n, label) {
|
|
732
|
+
const v = Number.isInteger(n) ? n : Math.floor(n);
|
|
733
|
+
if (!Number.isFinite(v) || v <= 0)
|
|
734
|
+
throw new Error(`${label} must be a positive integer`);
|
|
735
|
+
return v;
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
export function getMaxActiveProfilesPerOwnerV2() {
|
|
739
|
+
return _tleMaxActiveProfiles;
|
|
740
|
+
}
|
|
741
|
+
export function setMaxActiveProfilesPerOwnerV2(n) {
|
|
742
|
+
_tleMaxActiveProfiles = _posIntV2(n, "maxActiveProfilesPerOwner");
|
|
743
|
+
return _tleMaxActiveProfiles;
|
|
744
|
+
}
|
|
745
|
+
export function getMaxStudyingRunsPerLearnerV2() {
|
|
746
|
+
return _tleMaxStudyingRuns;
|
|
747
|
+
}
|
|
748
|
+
export function setMaxStudyingRunsPerLearnerV2(n) {
|
|
749
|
+
_tleMaxStudyingRuns = _posIntV2(n, "maxStudyingRunsPerLearner");
|
|
750
|
+
return _tleMaxStudyingRuns;
|
|
751
|
+
}
|
|
752
|
+
export function getProfileStaleMsV2() {
|
|
753
|
+
return _tleProfileStaleMs;
|
|
754
|
+
}
|
|
755
|
+
export function setProfileStaleMsV2(n) {
|
|
756
|
+
_tleProfileStaleMs = _posIntV2(n, "profileStaleMs");
|
|
757
|
+
return _tleProfileStaleMs;
|
|
758
|
+
}
|
|
759
|
+
export function getRunStuckMsV2() {
|
|
760
|
+
return _tleRunStuckMs;
|
|
761
|
+
}
|
|
762
|
+
export function setRunStuckMsV2(n) {
|
|
763
|
+
_tleRunStuckMs = _posIntV2(n, "runStuckMs");
|
|
764
|
+
return _tleRunStuckMs;
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
export function getActiveProfileCountV2(owner) {
|
|
768
|
+
if (!owner) throw new Error("owner is required");
|
|
769
|
+
let c = 0;
|
|
770
|
+
for (const p of _profilesV2.values()) {
|
|
771
|
+
if (p.owner !== owner) continue;
|
|
772
|
+
if (p.status === PROFILE_MATURITY_V2.ARCHIVED) continue;
|
|
773
|
+
if (p.status === PROFILE_MATURITY_V2.DRAFT) continue;
|
|
774
|
+
c++;
|
|
775
|
+
}
|
|
776
|
+
return c;
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
export function getStudyingRunCountV2(learner) {
|
|
780
|
+
if (!learner) throw new Error("learner is required");
|
|
781
|
+
let c = 0;
|
|
782
|
+
for (const r of _runsV2.values()) {
|
|
783
|
+
if (r.learner !== learner) continue;
|
|
784
|
+
if (r.status !== LEARNING_RUN_V2.STUDYING) continue;
|
|
785
|
+
c++;
|
|
786
|
+
}
|
|
787
|
+
return c;
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
export function createProfileV2({ id, owner, stackName, metadata }) {
|
|
791
|
+
if (!id) throw new Error("id is required");
|
|
792
|
+
if (!owner) throw new Error("owner is required");
|
|
793
|
+
if (!stackName) throw new Error("stackName is required");
|
|
794
|
+
if (_profilesV2.has(id)) throw new Error(`profile ${id} already exists`);
|
|
795
|
+
const now = Date.now();
|
|
796
|
+
const profile = {
|
|
797
|
+
id,
|
|
798
|
+
owner,
|
|
799
|
+
stackName: String(stackName),
|
|
800
|
+
status: PROFILE_MATURITY_V2.DRAFT,
|
|
801
|
+
createdAt: now,
|
|
802
|
+
updatedAt: now,
|
|
803
|
+
activatedAt: null,
|
|
804
|
+
lastTouchedAt: now,
|
|
805
|
+
metadata: metadata ? { ...metadata } : {},
|
|
806
|
+
};
|
|
807
|
+
_profilesV2.set(id, profile);
|
|
808
|
+
return { ...profile, metadata: { ...profile.metadata } };
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
export function getProfileV2(id) {
|
|
812
|
+
const p = _profilesV2.get(id);
|
|
813
|
+
if (!p) return null;
|
|
814
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
export function listProfilesV2({ owner, status } = {}) {
|
|
818
|
+
const out = [];
|
|
819
|
+
for (const p of _profilesV2.values()) {
|
|
820
|
+
if (owner && p.owner !== owner) continue;
|
|
821
|
+
if (status && p.status !== status) continue;
|
|
822
|
+
out.push({ ...p, metadata: { ...p.metadata } });
|
|
823
|
+
}
|
|
824
|
+
return out;
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
export function setProfileMaturityV2(
|
|
828
|
+
id,
|
|
829
|
+
nextStatus,
|
|
830
|
+
{ reason, metadata } = {},
|
|
831
|
+
) {
|
|
832
|
+
const p = _profilesV2.get(id);
|
|
833
|
+
if (!p) throw new Error(`profile ${id} not found`);
|
|
834
|
+
if (!_PROFILE_TRANS_V2.has(p.status))
|
|
835
|
+
throw new Error(`unknown status ${p.status}`);
|
|
836
|
+
const allowed = _PROFILE_TRANS_V2.get(p.status);
|
|
837
|
+
if (!allowed.has(nextStatus)) {
|
|
838
|
+
throw new Error(
|
|
839
|
+
`cannot transition profile ${id} from ${p.status} to ${nextStatus}`,
|
|
840
|
+
);
|
|
841
|
+
}
|
|
842
|
+
if (nextStatus === PROFILE_MATURITY_V2.ACTIVE && p.owner) {
|
|
843
|
+
const count = getActiveProfileCountV2(p.owner);
|
|
844
|
+
const wasActive =
|
|
845
|
+
p.status === PROFILE_MATURITY_V2.ACTIVE ||
|
|
846
|
+
p.status === PROFILE_MATURITY_V2.STALE;
|
|
847
|
+
if (!wasActive && count >= _tleMaxActiveProfiles) {
|
|
848
|
+
throw new Error(
|
|
849
|
+
`owner ${p.owner} exceeds max active profile cap ${_tleMaxActiveProfiles}`,
|
|
850
|
+
);
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
const now = Date.now();
|
|
854
|
+
p.status = nextStatus;
|
|
855
|
+
p.updatedAt = now;
|
|
856
|
+
p.lastTouchedAt = now;
|
|
857
|
+
if (nextStatus === PROFILE_MATURITY_V2.ACTIVE && !p.activatedAt)
|
|
858
|
+
p.activatedAt = now;
|
|
859
|
+
if (reason) p.reason = reason;
|
|
860
|
+
if (metadata) p.metadata = { ...p.metadata, ...metadata };
|
|
861
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
export function activateProfileV2(id, opts) {
|
|
865
|
+
return setProfileMaturityV2(id, PROFILE_MATURITY_V2.ACTIVE, opts);
|
|
866
|
+
}
|
|
867
|
+
export function markProfileStaleV2(id, opts) {
|
|
868
|
+
return setProfileMaturityV2(id, PROFILE_MATURITY_V2.STALE, opts);
|
|
869
|
+
}
|
|
870
|
+
export function archiveProfileV2(id, opts) {
|
|
871
|
+
return setProfileMaturityV2(id, PROFILE_MATURITY_V2.ARCHIVED, opts);
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
export function touchProfileV2(id) {
|
|
875
|
+
const p = _profilesV2.get(id);
|
|
876
|
+
if (!p) throw new Error(`profile ${id} not found`);
|
|
877
|
+
if (_PROFILE_TERMINAL_V2.has(p.status))
|
|
878
|
+
throw new Error(`profile ${id} is terminal`);
|
|
879
|
+
p.lastTouchedAt = Date.now();
|
|
880
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
export function enqueueRunV2({ id, learner, topic, metadata }) {
|
|
884
|
+
if (!id) throw new Error("id is required");
|
|
885
|
+
if (!learner) throw new Error("learner is required");
|
|
886
|
+
if (!topic) throw new Error("topic is required");
|
|
887
|
+
if (_runsV2.has(id)) throw new Error(`run ${id} already exists`);
|
|
888
|
+
const now = Date.now();
|
|
889
|
+
const run = {
|
|
890
|
+
id,
|
|
891
|
+
learner,
|
|
892
|
+
topic: String(topic),
|
|
893
|
+
status: LEARNING_RUN_V2.QUEUED,
|
|
894
|
+
createdAt: now,
|
|
895
|
+
updatedAt: now,
|
|
896
|
+
startedAt: null,
|
|
897
|
+
endedAt: null,
|
|
898
|
+
metadata: metadata ? { ...metadata } : {},
|
|
899
|
+
};
|
|
900
|
+
_runsV2.set(id, run);
|
|
901
|
+
return { ...run, metadata: { ...run.metadata } };
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
export function getRunV2(id) {
|
|
905
|
+
const r = _runsV2.get(id);
|
|
906
|
+
if (!r) return null;
|
|
907
|
+
return { ...r, metadata: { ...r.metadata } };
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
export function listRunsV2({ learner, status } = {}) {
|
|
911
|
+
const out = [];
|
|
912
|
+
for (const r of _runsV2.values()) {
|
|
913
|
+
if (learner && r.learner !== learner) continue;
|
|
914
|
+
if (status && r.status !== status) continue;
|
|
915
|
+
out.push({ ...r, metadata: { ...r.metadata } });
|
|
916
|
+
}
|
|
917
|
+
return out;
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
export function setRunStatusV2(id, nextStatus, { reason, metadata } = {}) {
|
|
921
|
+
const r = _runsV2.get(id);
|
|
922
|
+
if (!r) throw new Error(`run ${id} not found`);
|
|
923
|
+
if (!_RUN_TRANS_V2.has(r.status))
|
|
924
|
+
throw new Error(`unknown status ${r.status}`);
|
|
925
|
+
const allowed = _RUN_TRANS_V2.get(r.status);
|
|
926
|
+
if (!allowed.has(nextStatus)) {
|
|
927
|
+
throw new Error(
|
|
928
|
+
`cannot transition run ${id} from ${r.status} to ${nextStatus}`,
|
|
929
|
+
);
|
|
930
|
+
}
|
|
931
|
+
if (nextStatus === LEARNING_RUN_V2.STUDYING) {
|
|
932
|
+
const count = getStudyingRunCountV2(r.learner);
|
|
933
|
+
if (count >= _tleMaxStudyingRuns) {
|
|
934
|
+
throw new Error(
|
|
935
|
+
`learner ${r.learner} exceeds max studying run cap ${_tleMaxStudyingRuns}`,
|
|
936
|
+
);
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
const now = Date.now();
|
|
940
|
+
r.status = nextStatus;
|
|
941
|
+
r.updatedAt = now;
|
|
942
|
+
if (nextStatus === LEARNING_RUN_V2.STUDYING && !r.startedAt)
|
|
943
|
+
r.startedAt = now;
|
|
944
|
+
if (_RUN_TERMINAL_V2.has(nextStatus)) r.endedAt = now;
|
|
945
|
+
if (reason) r.reason = reason;
|
|
946
|
+
if (metadata) r.metadata = { ...r.metadata, ...metadata };
|
|
947
|
+
return { ...r, metadata: { ...r.metadata } };
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
export function startRunV2(id, opts) {
|
|
951
|
+
return setRunStatusV2(id, LEARNING_RUN_V2.STUDYING, opts);
|
|
952
|
+
}
|
|
953
|
+
export function completeRunV2(id, opts) {
|
|
954
|
+
return setRunStatusV2(id, LEARNING_RUN_V2.COMPLETED, opts);
|
|
955
|
+
}
|
|
956
|
+
export function abandonRunV2(id, opts) {
|
|
957
|
+
return setRunStatusV2(id, LEARNING_RUN_V2.ABANDONED, opts);
|
|
958
|
+
}
|
|
959
|
+
export function failRunV2(id, opts) {
|
|
960
|
+
return setRunStatusV2(id, LEARNING_RUN_V2.FAILED, opts);
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
export function autoMarkStaleProfilesV2({ now } = {}) {
|
|
964
|
+
const t = now ?? Date.now();
|
|
965
|
+
const out = [];
|
|
966
|
+
for (const p of _profilesV2.values()) {
|
|
967
|
+
if (p.status !== PROFILE_MATURITY_V2.ACTIVE) continue;
|
|
968
|
+
if (t - p.lastTouchedAt > _tleProfileStaleMs) {
|
|
969
|
+
p.status = PROFILE_MATURITY_V2.STALE;
|
|
970
|
+
p.updatedAt = t;
|
|
971
|
+
out.push(p.id);
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
return out;
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
export function autoFailStuckRunsV2({ now } = {}) {
|
|
978
|
+
const t = now ?? Date.now();
|
|
979
|
+
const out = [];
|
|
980
|
+
for (const r of _runsV2.values()) {
|
|
981
|
+
if (r.status !== LEARNING_RUN_V2.STUDYING) continue;
|
|
982
|
+
if (r.startedAt == null) continue;
|
|
983
|
+
if (t - r.startedAt > _tleRunStuckMs) {
|
|
984
|
+
r.status = LEARNING_RUN_V2.FAILED;
|
|
985
|
+
r.endedAt = t;
|
|
986
|
+
r.updatedAt = t;
|
|
987
|
+
r.reason = r.reason || "auto-fail: stuck studying";
|
|
988
|
+
out.push(r.id);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
return out;
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
export function getTechLearningStatsV2() {
|
|
995
|
+
const profilesByStatus = {};
|
|
996
|
+
for (const v of Object.values(PROFILE_MATURITY_V2)) profilesByStatus[v] = 0;
|
|
997
|
+
for (const p of _profilesV2.values()) profilesByStatus[p.status]++;
|
|
998
|
+
const runsByStatus = {};
|
|
999
|
+
for (const v of Object.values(LEARNING_RUN_V2)) runsByStatus[v] = 0;
|
|
1000
|
+
for (const r of _runsV2.values()) runsByStatus[r.status]++;
|
|
1001
|
+
return {
|
|
1002
|
+
totalProfilesV2: _profilesV2.size,
|
|
1003
|
+
totalRunsV2: _runsV2.size,
|
|
1004
|
+
maxActiveProfilesPerOwner: _tleMaxActiveProfiles,
|
|
1005
|
+
maxStudyingRunsPerLearner: _tleMaxStudyingRuns,
|
|
1006
|
+
profileStaleMs: _tleProfileStaleMs,
|
|
1007
|
+
runStuckMs: _tleRunStuckMs,
|
|
1008
|
+
profilesByStatus,
|
|
1009
|
+
runsByStatus,
|
|
1010
|
+
};
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
export function _resetStateV2() {
|
|
1014
|
+
_profilesV2.clear();
|
|
1015
|
+
_runsV2.clear();
|
|
1016
|
+
_tleMaxActiveProfiles = TLE_DEFAULT_MAX_ACTIVE_PROFILES_PER_OWNER;
|
|
1017
|
+
_tleMaxStudyingRuns = TLE_DEFAULT_MAX_STUDYING_RUNS_PER_LEARNER;
|
|
1018
|
+
_tleProfileStaleMs = TLE_DEFAULT_PROFILE_STALE_MS;
|
|
1019
|
+
_tleRunStuckMs = TLE_DEFAULT_RUN_STUCK_MS;
|
|
1020
|
+
}
|