chainlesschain 0.81.0 → 0.143.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/chainlesschain.js +0 -0
- package/package.json +1 -1
- package/src/commands/a2a.js +62 -0
- package/src/commands/activitypub.js +61 -0
- package/src/commands/agent-network.js +254 -1
- package/src/commands/agent.js +117 -0
- package/src/commands/audit.js +302 -0
- package/src/commands/automation.js +271 -1
- package/src/commands/bi.js +61 -0
- package/src/commands/bm25.js +78 -0
- package/src/commands/browse.js +64 -0
- package/src/commands/ccron.js +78 -0
- package/src/commands/codegen.js +224 -0
- package/src/commands/collab.js +341 -0
- package/src/commands/compliance.js +1075 -0
- package/src/commands/compt.js +78 -0
- package/src/commands/consol.js +231 -0
- package/src/commands/cowork.js +263 -0
- package/src/commands/crosschain.js +62 -0
- package/src/commands/dao.js +62 -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 +61 -0
- package/src/commands/economy.js +56 -0
- package/src/commands/encrypt.js +341 -0
- package/src/commands/evolution.js +56 -0
- package/src/commands/evomap.js +61 -0
- package/src/commands/export.js +256 -1
- package/src/commands/fflag.js +178 -0
- package/src/commands/fusion.js +258 -0
- package/src/commands/git.js +45 -0
- package/src/commands/governance.js +325 -0
- package/src/commands/hardening.js +411 -0
- package/src/commands/hmemory.js +56 -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 +42 -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/itbudget.js +45 -0
- package/src/commands/kg.js +387 -0
- package/src/commands/llm.js +263 -0
- package/src/commands/lowcode.js +44 -0
- package/src/commands/matrix.js +62 -0
- package/src/commands/mcp.js +221 -0
- package/src/commands/mcpscaf.js +41 -0
- package/src/commands/meminj.js +41 -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/nostr.js +62 -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/orchgov.js +45 -0
- package/src/commands/org.js +277 -0
- package/src/commands/p2p.js +390 -0
- package/src/commands/pdfp.js +78 -0
- package/src/commands/perception.js +290 -0
- package/src/commands/perf.js +39 -0
- package/src/commands/perm.js +45 -0
- package/src/commands/permmem.js +251 -0
- package/src/commands/pipeline.js +57 -1
- package/src/commands/planmode.js +45 -0
- package/src/commands/plugin-ecosystem.js +273 -0
- package/src/commands/pqc.js +393 -0
- package/src/commands/promcomp.js +82 -0
- package/src/commands/quantization.js +351 -0
- package/src/commands/rcache.js +271 -0
- package/src/commands/recommend.js +382 -0
- package/src/commands/runtime.js +307 -0
- package/src/commands/scim.js +262 -0
- package/src/commands/seshhook.js +41 -0
- package/src/commands/seshsearch.js +41 -0
- package/src/commands/seshtail.js +41 -0
- package/src/commands/seshu.js +41 -0
- package/src/commands/session.js +258 -0
- package/src/commands/sganal.js +78 -0
- package/src/commands/siem.js +40 -0
- package/src/commands/skill.js +267 -1
- package/src/commands/slotfill.js +41 -0
- package/src/commands/social.js +290 -0
- package/src/commands/sso.js +186 -1
- package/src/commands/svccont.js +45 -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/tms.js +45 -0
- package/src/commands/tokens.js +269 -0
- package/src/commands/topiccls.js +45 -0
- package/src/commands/trust.js +249 -0
- package/src/commands/uprof.js +45 -0
- package/src/commands/vcheck.js +78 -0
- package/src/commands/wallet.js +277 -0
- package/src/commands/webfetch.js +41 -0
- package/src/commands/workflow.js +171 -0
- package/src/commands/zkp.js +62 -0
- package/src/harness/prompt-compressor.js +331 -0
- package/src/index.js +65 -1
- package/src/lib/a2a-protocol.js +105 -0
- package/src/lib/activitypub-bridge.js +105 -0
- package/src/lib/agent-coordinator.js +325 -0
- package/src/lib/agent-economy.js +105 -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 +105 -0
- package/src/lib/audit-logger.js +379 -0
- package/src/lib/automation-engine.js +330 -0
- package/src/lib/autonomous-agent.js +105 -0
- package/src/lib/autonomous-developer.js +350 -0
- package/src/lib/bi-engine.js +105 -0
- package/src/lib/bm25-search.js +81 -0
- package/src/lib/browser-automation.js +105 -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-framework-reporter.js +105 -0
- package/src/lib/compliance-manager.js +434 -0
- package/src/lib/compression-telemetry.js +81 -0
- package/src/lib/content-recommendation.js +469 -0
- package/src/lib/content-recommender.js +105 -0
- package/src/lib/cowork-cron.js +81 -0
- package/src/lib/cowork-task-runner.js +105 -0
- package/src/lib/cross-chain.js +105 -0
- package/src/lib/crypto-manager.js +350 -0
- package/src/lib/dao-governance.js +105 -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 +105 -0
- package/src/lib/evolution-system.js +105 -0
- package/src/lib/evomap-manager.js +105 -0
- package/src/lib/execution-backend.js +105 -0
- package/src/lib/feature-flags.js +85 -0
- package/src/lib/git-integration.js +105 -0
- package/src/lib/hardening-manager.js +348 -0
- package/src/lib/hierarchical-memory.js +105 -0
- package/src/lib/hook-manager.js +380 -0
- package/src/lib/inference-network.js +105 -0
- package/src/lib/instinct-manager.js +332 -0
- package/src/lib/ipfs-storage.js +334 -0
- package/src/lib/iteration-budget.js +105 -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/matrix-bridge.js +105 -0
- package/src/lib/mcp-registry.js +333 -0
- package/src/lib/mcp-scaffold.js +81 -0
- package/src/lib/memory-injection.js +81 -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/nostr-bridge.js +105 -0
- package/src/lib/note-versioning.js +327 -0
- package/src/lib/orchestrator.js +105 -0
- package/src/lib/org-manager.js +323 -0
- package/src/lib/p2p-manager.js +387 -0
- package/src/lib/pdf-parser.js +81 -0
- package/src/lib/perception.js +346 -0
- package/src/lib/perf-tuning.js +109 -1
- package/src/lib/permanent-memory.js +320 -0
- package/src/lib/permission-engine.js +81 -0
- package/src/lib/pipeline-orchestrator.js +105 -0
- package/src/lib/plan-mode.js +81 -0
- package/src/lib/plugin-ecosystem.js +377 -0
- package/src/lib/pqc-manager.js +368 -0
- package/src/lib/prompt-compressor.js +1 -10
- package/src/lib/protocol-fusion.js +417 -0
- package/src/lib/quantization.js +325 -0
- package/src/lib/response-cache.js +327 -0
- package/src/lib/scim-manager.js +329 -0
- package/src/lib/service-container.js +81 -0
- package/src/lib/session-consolidator.js +105 -0
- package/src/lib/session-hooks.js +81 -0
- package/src/lib/session-manager.js +329 -0
- package/src/lib/session-search.js +81 -0
- package/src/lib/session-tail.js +81 -0
- package/src/lib/session-usage.js +83 -0
- package/src/lib/siem-exporter.js +105 -0
- package/src/lib/skill-loader.js +377 -0
- package/src/lib/slot-filler.js +81 -0
- package/src/lib/social-graph-analytics.js +81 -0
- package/src/lib/social-graph.js +81 -0
- package/src/lib/social-manager.js +326 -0
- package/src/lib/sso-manager.js +332 -0
- package/src/lib/sub-agent-registry.js +110 -0
- package/src/lib/sync-manager.js +326 -0
- package/src/lib/task-model-selector.js +81 -0
- package/src/lib/tech-learning-engine.js +369 -0
- package/src/lib/tenant-saas.js +460 -0
- package/src/lib/threat-intel.js +335 -0
- package/src/lib/todo-manager.js +105 -0
- package/src/lib/token-incentive.js +293 -0
- package/src/lib/token-tracker.js +329 -0
- package/src/lib/topic-classifier.js +105 -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/user-profile.js +81 -0
- package/src/lib/version-checker.js +81 -0
- package/src/lib/wallet-manager.js +326 -0
- package/src/lib/web-fetch.js +81 -0
- package/src/lib/workflow-engine.js +322 -0
- package/src/lib/zkp-engine.js +105 -0
package/src/lib/dlp-engine.js
CHANGED
|
@@ -691,3 +691,108 @@ export function getHighestUnresolvedSeverity() {
|
|
|
691
691
|
}
|
|
692
692
|
|
|
693
693
|
export { _v2PolicyMeta, _v2IncidentMeta, _builtinPolicyTemplates };
|
|
694
|
+
|
|
695
|
+
|
|
696
|
+
// ===== V2 Surface: DLP Engine governance overlay (CLI v0.135.0) =====
|
|
697
|
+
export const DLP_POLICY_MATURITY_V2 = Object.freeze({
|
|
698
|
+
PENDING: "pending", ACTIVE: "active", SUSPENDED: "suspended", RETIRED: "retired",
|
|
699
|
+
});
|
|
700
|
+
export const DLP_SCAN_LIFECYCLE_V2 = Object.freeze({
|
|
701
|
+
QUEUED: "queued", SCANNING: "scanning", COMPLETED: "completed", FAILED: "failed", CANCELLED: "cancelled",
|
|
702
|
+
});
|
|
703
|
+
|
|
704
|
+
const _dlpPolTrans = new Map([
|
|
705
|
+
[DLP_POLICY_MATURITY_V2.PENDING, new Set([DLP_POLICY_MATURITY_V2.ACTIVE, DLP_POLICY_MATURITY_V2.RETIRED])],
|
|
706
|
+
[DLP_POLICY_MATURITY_V2.ACTIVE, new Set([DLP_POLICY_MATURITY_V2.SUSPENDED, DLP_POLICY_MATURITY_V2.RETIRED])],
|
|
707
|
+
[DLP_POLICY_MATURITY_V2.SUSPENDED, new Set([DLP_POLICY_MATURITY_V2.ACTIVE, DLP_POLICY_MATURITY_V2.RETIRED])],
|
|
708
|
+
[DLP_POLICY_MATURITY_V2.RETIRED, new Set()],
|
|
709
|
+
]);
|
|
710
|
+
const _dlpPolTerminal = new Set([DLP_POLICY_MATURITY_V2.RETIRED]);
|
|
711
|
+
const _dlpScanTrans = new Map([
|
|
712
|
+
[DLP_SCAN_LIFECYCLE_V2.QUEUED, new Set([DLP_SCAN_LIFECYCLE_V2.SCANNING, DLP_SCAN_LIFECYCLE_V2.CANCELLED])],
|
|
713
|
+
[DLP_SCAN_LIFECYCLE_V2.SCANNING, new Set([DLP_SCAN_LIFECYCLE_V2.COMPLETED, DLP_SCAN_LIFECYCLE_V2.FAILED, DLP_SCAN_LIFECYCLE_V2.CANCELLED])],
|
|
714
|
+
[DLP_SCAN_LIFECYCLE_V2.COMPLETED, new Set()],
|
|
715
|
+
[DLP_SCAN_LIFECYCLE_V2.FAILED, new Set()],
|
|
716
|
+
[DLP_SCAN_LIFECYCLE_V2.CANCELLED, new Set()],
|
|
717
|
+
]);
|
|
718
|
+
|
|
719
|
+
const _dlpPols = new Map();
|
|
720
|
+
const _dlpScans = new Map();
|
|
721
|
+
let _dlpMaxActivePerOwner = 16;
|
|
722
|
+
let _dlpMaxPendingPerPol = 20;
|
|
723
|
+
let _dlpPolIdleMs = 12 * 60 * 60 * 1000;
|
|
724
|
+
let _dlpScanStuckMs = 5 * 60 * 1000;
|
|
725
|
+
|
|
726
|
+
function _dlpPos(n, lbl) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${lbl} must be positive integer`); return v; }
|
|
727
|
+
|
|
728
|
+
export function setMaxActiveDlpPoliciesPerOwnerV2(n) { _dlpMaxActivePerOwner = _dlpPos(n, "maxActiveDlpPoliciesPerOwner"); }
|
|
729
|
+
export function getMaxActiveDlpPoliciesPerOwnerV2() { return _dlpMaxActivePerOwner; }
|
|
730
|
+
export function setMaxPendingDlpScansPerPolicyV2(n) { _dlpMaxPendingPerPol = _dlpPos(n, "maxPendingDlpScansPerPolicy"); }
|
|
731
|
+
export function getMaxPendingDlpScansPerPolicyV2() { return _dlpMaxPendingPerPol; }
|
|
732
|
+
export function setDlpPolicyIdleMsV2(n) { _dlpPolIdleMs = _dlpPos(n, "dlpPolicyIdleMs"); }
|
|
733
|
+
export function getDlpPolicyIdleMsV2() { return _dlpPolIdleMs; }
|
|
734
|
+
export function setDlpScanStuckMsV2(n) { _dlpScanStuckMs = _dlpPos(n, "dlpScanStuckMs"); }
|
|
735
|
+
export function getDlpScanStuckMsV2() { return _dlpScanStuckMs; }
|
|
736
|
+
|
|
737
|
+
export function _resetStateDlpEngineV2() {
|
|
738
|
+
_dlpPols.clear(); _dlpScans.clear();
|
|
739
|
+
_dlpMaxActivePerOwner = 16; _dlpMaxPendingPerPol = 20;
|
|
740
|
+
_dlpPolIdleMs = 12 * 60 * 60 * 1000; _dlpScanStuckMs = 5 * 60 * 1000;
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
export function registerDlpPolicyV2({ id, owner, classification, metadata } = {}) {
|
|
744
|
+
if (!id || typeof id !== "string") throw new Error("id is required");
|
|
745
|
+
if (!owner || typeof owner !== "string") throw new Error("owner is required");
|
|
746
|
+
if (_dlpPols.has(id)) throw new Error(`dlp policy ${id} already registered`);
|
|
747
|
+
const now = Date.now();
|
|
748
|
+
const p = { id, owner, classification: classification || "internal", status: DLP_POLICY_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, retiredAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
|
|
749
|
+
_dlpPols.set(id, p);
|
|
750
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
751
|
+
}
|
|
752
|
+
function _dlpCheckP(from, to) { const a = _dlpPolTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid dlp policy transition ${from} → ${to}`); }
|
|
753
|
+
function _dlpCountActive(owner) { let n = 0; for (const p of _dlpPols.values()) if (p.owner === owner && p.status === DLP_POLICY_MATURITY_V2.ACTIVE) n++; return n; }
|
|
754
|
+
|
|
755
|
+
export function activateDlpPolicyV2(id) {
|
|
756
|
+
const p = _dlpPols.get(id); if (!p) throw new Error(`dlp policy ${id} not found`);
|
|
757
|
+
_dlpCheckP(p.status, DLP_POLICY_MATURITY_V2.ACTIVE);
|
|
758
|
+
const recovery = p.status === DLP_POLICY_MATURITY_V2.SUSPENDED;
|
|
759
|
+
if (!recovery) { const a = _dlpCountActive(p.owner); if (a >= _dlpMaxActivePerOwner) throw new Error(`max active dlp policies per owner (${_dlpMaxActivePerOwner}) reached for ${p.owner}`); }
|
|
760
|
+
const now = Date.now(); p.status = DLP_POLICY_MATURITY_V2.ACTIVE; p.updatedAt = now; p.lastTouchedAt = now; if (!p.activatedAt) p.activatedAt = now;
|
|
761
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
762
|
+
}
|
|
763
|
+
export function suspendDlpPolicyV2(id) { const p = _dlpPols.get(id); if (!p) throw new Error(`dlp policy ${id} not found`); _dlpCheckP(p.status, DLP_POLICY_MATURITY_V2.SUSPENDED); p.status = DLP_POLICY_MATURITY_V2.SUSPENDED; p.updatedAt = Date.now(); return { ...p, metadata: { ...p.metadata } }; }
|
|
764
|
+
export function retireDlpPolicyV2(id) { const p = _dlpPols.get(id); if (!p) throw new Error(`dlp policy ${id} not found`); _dlpCheckP(p.status, DLP_POLICY_MATURITY_V2.RETIRED); const now = Date.now(); p.status = DLP_POLICY_MATURITY_V2.RETIRED; p.updatedAt = now; if (!p.retiredAt) p.retiredAt = now; return { ...p, metadata: { ...p.metadata } }; }
|
|
765
|
+
export function touchDlpPolicyV2(id) { const p = _dlpPols.get(id); if (!p) throw new Error(`dlp policy ${id} not found`); if (_dlpPolTerminal.has(p.status)) throw new Error(`cannot touch terminal dlp policy ${id}`); const now = Date.now(); p.lastTouchedAt = now; p.updatedAt = now; return { ...p, metadata: { ...p.metadata } }; }
|
|
766
|
+
export function getDlpPolicyV2(id) { const p = _dlpPols.get(id); if (!p) return null; return { ...p, metadata: { ...p.metadata } }; }
|
|
767
|
+
export function listDlpPoliciesV2() { return [..._dlpPols.values()].map((p) => ({ ...p, metadata: { ...p.metadata } })); }
|
|
768
|
+
|
|
769
|
+
function _dlpCountPending(pid) { let n = 0; for (const s of _dlpScans.values()) if (s.policyId === pid && (s.status === DLP_SCAN_LIFECYCLE_V2.QUEUED || s.status === DLP_SCAN_LIFECYCLE_V2.SCANNING)) n++; return n; }
|
|
770
|
+
|
|
771
|
+
export function createDlpScanV2({ id, policyId, target, metadata } = {}) {
|
|
772
|
+
if (!id || typeof id !== "string") throw new Error("id is required");
|
|
773
|
+
if (!policyId || typeof policyId !== "string") throw new Error("policyId is required");
|
|
774
|
+
if (_dlpScans.has(id)) throw new Error(`dlp scan ${id} already exists`);
|
|
775
|
+
if (!_dlpPols.has(policyId)) throw new Error(`dlp policy ${policyId} not found`);
|
|
776
|
+
const pending = _dlpCountPending(policyId);
|
|
777
|
+
if (pending >= _dlpMaxPendingPerPol) throw new Error(`max pending dlp scans per policy (${_dlpMaxPendingPerPol}) reached for ${policyId}`);
|
|
778
|
+
const now = Date.now();
|
|
779
|
+
const s = { id, policyId, target: target || "", status: DLP_SCAN_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
|
|
780
|
+
_dlpScans.set(id, s);
|
|
781
|
+
return { ...s, metadata: { ...s.metadata } };
|
|
782
|
+
}
|
|
783
|
+
function _dlpCheckS(from, to) { const a = _dlpScanTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid dlp scan transition ${from} → ${to}`); }
|
|
784
|
+
export function startDlpScanV2(id) { const s = _dlpScans.get(id); if (!s) throw new Error(`dlp scan ${id} not found`); _dlpCheckS(s.status, DLP_SCAN_LIFECYCLE_V2.SCANNING); const now = Date.now(); s.status = DLP_SCAN_LIFECYCLE_V2.SCANNING; s.updatedAt = now; if (!s.startedAt) s.startedAt = now; return { ...s, metadata: { ...s.metadata } }; }
|
|
785
|
+
export function completeDlpScanV2(id) { const s = _dlpScans.get(id); if (!s) throw new Error(`dlp scan ${id} not found`); _dlpCheckS(s.status, DLP_SCAN_LIFECYCLE_V2.COMPLETED); const now = Date.now(); s.status = DLP_SCAN_LIFECYCLE_V2.COMPLETED; s.updatedAt = now; if (!s.settledAt) s.settledAt = now; return { ...s, metadata: { ...s.metadata } }; }
|
|
786
|
+
export function failDlpScanV2(id, reason) { const s = _dlpScans.get(id); if (!s) throw new Error(`dlp scan ${id} not found`); _dlpCheckS(s.status, DLP_SCAN_LIFECYCLE_V2.FAILED); const now = Date.now(); s.status = DLP_SCAN_LIFECYCLE_V2.FAILED; s.updatedAt = now; if (!s.settledAt) s.settledAt = now; if (reason) s.metadata.failReason = String(reason); return { ...s, metadata: { ...s.metadata } }; }
|
|
787
|
+
export function cancelDlpScanV2(id, reason) { const s = _dlpScans.get(id); if (!s) throw new Error(`dlp scan ${id} not found`); _dlpCheckS(s.status, DLP_SCAN_LIFECYCLE_V2.CANCELLED); const now = Date.now(); s.status = DLP_SCAN_LIFECYCLE_V2.CANCELLED; s.updatedAt = now; if (!s.settledAt) s.settledAt = now; if (reason) s.metadata.cancelReason = String(reason); return { ...s, metadata: { ...s.metadata } }; }
|
|
788
|
+
export function getDlpScanV2(id) { const s = _dlpScans.get(id); if (!s) return null; return { ...s, metadata: { ...s.metadata } }; }
|
|
789
|
+
export function listDlpScansV2() { return [..._dlpScans.values()].map((s) => ({ ...s, metadata: { ...s.metadata } })); }
|
|
790
|
+
|
|
791
|
+
export function autoSuspendIdleDlpPoliciesV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const p of _dlpPols.values()) if (p.status === DLP_POLICY_MATURITY_V2.ACTIVE && (t - p.lastTouchedAt) >= _dlpPolIdleMs) { p.status = DLP_POLICY_MATURITY_V2.SUSPENDED; p.updatedAt = t; flipped.push(p.id); } return { flipped, count: flipped.length }; }
|
|
792
|
+
export function autoFailStuckDlpScansV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const s of _dlpScans.values()) if (s.status === DLP_SCAN_LIFECYCLE_V2.SCANNING && s.startedAt != null && (t - s.startedAt) >= _dlpScanStuckMs) { s.status = DLP_SCAN_LIFECYCLE_V2.FAILED; s.updatedAt = t; if (!s.settledAt) s.settledAt = t; s.metadata.failReason = "auto-fail-stuck"; flipped.push(s.id); } return { flipped, count: flipped.length }; }
|
|
793
|
+
|
|
794
|
+
export function getDlpEngineStatsV2() {
|
|
795
|
+
const policiesByStatus = {}; for (const s of Object.values(DLP_POLICY_MATURITY_V2)) policiesByStatus[s] = 0; for (const p of _dlpPols.values()) policiesByStatus[p.status]++;
|
|
796
|
+
const scansByStatus = {}; for (const s of Object.values(DLP_SCAN_LIFECYCLE_V2)) scansByStatus[s] = 0; for (const sc of _dlpScans.values()) scansByStatus[sc.status]++;
|
|
797
|
+
return { totalPoliciesV2: _dlpPols.size, totalScansV2: _dlpScans.size, maxActiveDlpPoliciesPerOwner: _dlpMaxActivePerOwner, maxPendingDlpScansPerPolicy: _dlpMaxPendingPerPol, dlpPolicyIdleMs: _dlpPolIdleMs, dlpScanStuckMs: _dlpScanStuckMs, policiesByStatus, scansByStatus };
|
|
798
|
+
}
|
|
@@ -959,3 +959,108 @@ export function _resetV2State() {
|
|
|
959
959
|
_v2Config.predictionHorizonMs = 86400000;
|
|
960
960
|
_v2Config.growthLogRetentionDays = 365;
|
|
961
961
|
}
|
|
962
|
+
|
|
963
|
+
|
|
964
|
+
// ===== V2 Surface: Evolution System governance overlay (CLI v0.137.0) =====
|
|
965
|
+
export const EVO_GOAL_MATURITY_V2 = Object.freeze({
|
|
966
|
+
PENDING: "pending", ACTIVE: "active", PAUSED: "paused", ARCHIVED: "archived",
|
|
967
|
+
});
|
|
968
|
+
export const EVO_CYCLE_LIFECYCLE_V2 = Object.freeze({
|
|
969
|
+
QUEUED: "queued", RUNNING: "running", COMPLETED: "completed", FAILED: "failed", CANCELLED: "cancelled",
|
|
970
|
+
});
|
|
971
|
+
|
|
972
|
+
const _evoGoalTrans = new Map([
|
|
973
|
+
[EVO_GOAL_MATURITY_V2.PENDING, new Set([EVO_GOAL_MATURITY_V2.ACTIVE, EVO_GOAL_MATURITY_V2.ARCHIVED])],
|
|
974
|
+
[EVO_GOAL_MATURITY_V2.ACTIVE, new Set([EVO_GOAL_MATURITY_V2.PAUSED, EVO_GOAL_MATURITY_V2.ARCHIVED])],
|
|
975
|
+
[EVO_GOAL_MATURITY_V2.PAUSED, new Set([EVO_GOAL_MATURITY_V2.ACTIVE, EVO_GOAL_MATURITY_V2.ARCHIVED])],
|
|
976
|
+
[EVO_GOAL_MATURITY_V2.ARCHIVED, new Set()],
|
|
977
|
+
]);
|
|
978
|
+
const _evoGoalTerminal = new Set([EVO_GOAL_MATURITY_V2.ARCHIVED]);
|
|
979
|
+
const _evoCycleTrans = new Map([
|
|
980
|
+
[EVO_CYCLE_LIFECYCLE_V2.QUEUED, new Set([EVO_CYCLE_LIFECYCLE_V2.RUNNING, EVO_CYCLE_LIFECYCLE_V2.CANCELLED])],
|
|
981
|
+
[EVO_CYCLE_LIFECYCLE_V2.RUNNING, new Set([EVO_CYCLE_LIFECYCLE_V2.COMPLETED, EVO_CYCLE_LIFECYCLE_V2.FAILED, EVO_CYCLE_LIFECYCLE_V2.CANCELLED])],
|
|
982
|
+
[EVO_CYCLE_LIFECYCLE_V2.COMPLETED, new Set()],
|
|
983
|
+
[EVO_CYCLE_LIFECYCLE_V2.FAILED, new Set()],
|
|
984
|
+
[EVO_CYCLE_LIFECYCLE_V2.CANCELLED, new Set()],
|
|
985
|
+
]);
|
|
986
|
+
|
|
987
|
+
const _evoGoals = new Map();
|
|
988
|
+
const _evoCycles = new Map();
|
|
989
|
+
let _evoMaxActivePerOwner = 6;
|
|
990
|
+
let _evoMaxPendingPerGoal = 12;
|
|
991
|
+
let _evoGoalIdleMs = 14 * 24 * 60 * 60 * 1000;
|
|
992
|
+
let _evoCycleStuckMs = 10 * 60 * 1000;
|
|
993
|
+
|
|
994
|
+
function _evoPos(n, lbl) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${lbl} must be positive integer`); return v; }
|
|
995
|
+
|
|
996
|
+
export function setMaxActiveEvoGoalsPerOwnerV2(n) { _evoMaxActivePerOwner = _evoPos(n, "maxActiveEvoGoalsPerOwner"); }
|
|
997
|
+
export function getMaxActiveEvoGoalsPerOwnerV2() { return _evoMaxActivePerOwner; }
|
|
998
|
+
export function setMaxPendingEvoCyclesPerGoalV2(n) { _evoMaxPendingPerGoal = _evoPos(n, "maxPendingEvoCyclesPerGoal"); }
|
|
999
|
+
export function getMaxPendingEvoCyclesPerGoalV2() { return _evoMaxPendingPerGoal; }
|
|
1000
|
+
export function setEvoGoalIdleMsV2(n) { _evoGoalIdleMs = _evoPos(n, "evoGoalIdleMs"); }
|
|
1001
|
+
export function getEvoGoalIdleMsV2() { return _evoGoalIdleMs; }
|
|
1002
|
+
export function setEvoCycleStuckMsV2(n) { _evoCycleStuckMs = _evoPos(n, "evoCycleStuckMs"); }
|
|
1003
|
+
export function getEvoCycleStuckMsV2() { return _evoCycleStuckMs; }
|
|
1004
|
+
|
|
1005
|
+
export function _resetStateEvolutionSystemV2() {
|
|
1006
|
+
_evoGoals.clear(); _evoCycles.clear();
|
|
1007
|
+
_evoMaxActivePerOwner = 6; _evoMaxPendingPerGoal = 12;
|
|
1008
|
+
_evoGoalIdleMs = 14 * 24 * 60 * 60 * 1000; _evoCycleStuckMs = 10 * 60 * 1000;
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
export function registerEvoGoalV2({ id, owner, objective, metadata } = {}) {
|
|
1012
|
+
if (!id || typeof id !== "string") throw new Error("id is required");
|
|
1013
|
+
if (!owner || typeof owner !== "string") throw new Error("owner is required");
|
|
1014
|
+
if (_evoGoals.has(id)) throw new Error(`evo goal ${id} already registered`);
|
|
1015
|
+
const now = Date.now();
|
|
1016
|
+
const g = { id, owner, objective: objective || "", status: EVO_GOAL_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, archivedAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
|
|
1017
|
+
_evoGoals.set(id, g);
|
|
1018
|
+
return { ...g, metadata: { ...g.metadata } };
|
|
1019
|
+
}
|
|
1020
|
+
function _evoCheckG(from, to) { const a = _evoGoalTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid evo goal transition ${from} → ${to}`); }
|
|
1021
|
+
function _evoCountActive(owner) { let n = 0; for (const g of _evoGoals.values()) if (g.owner === owner && g.status === EVO_GOAL_MATURITY_V2.ACTIVE) n++; return n; }
|
|
1022
|
+
|
|
1023
|
+
export function activateEvoGoalV2(id) {
|
|
1024
|
+
const g = _evoGoals.get(id); if (!g) throw new Error(`evo goal ${id} not found`);
|
|
1025
|
+
_evoCheckG(g.status, EVO_GOAL_MATURITY_V2.ACTIVE);
|
|
1026
|
+
const recovery = g.status === EVO_GOAL_MATURITY_V2.PAUSED;
|
|
1027
|
+
if (!recovery) { const a = _evoCountActive(g.owner); if (a >= _evoMaxActivePerOwner) throw new Error(`max active evo goals per owner (${_evoMaxActivePerOwner}) reached for ${g.owner}`); }
|
|
1028
|
+
const now = Date.now(); g.status = EVO_GOAL_MATURITY_V2.ACTIVE; g.updatedAt = now; g.lastTouchedAt = now; if (!g.activatedAt) g.activatedAt = now;
|
|
1029
|
+
return { ...g, metadata: { ...g.metadata } };
|
|
1030
|
+
}
|
|
1031
|
+
export function pauseEvoGoalV2(id) { const g = _evoGoals.get(id); if (!g) throw new Error(`evo goal ${id} not found`); _evoCheckG(g.status, EVO_GOAL_MATURITY_V2.PAUSED); g.status = EVO_GOAL_MATURITY_V2.PAUSED; g.updatedAt = Date.now(); return { ...g, metadata: { ...g.metadata } }; }
|
|
1032
|
+
export function archiveEvoGoalV2(id) { const g = _evoGoals.get(id); if (!g) throw new Error(`evo goal ${id} not found`); _evoCheckG(g.status, EVO_GOAL_MATURITY_V2.ARCHIVED); const now = Date.now(); g.status = EVO_GOAL_MATURITY_V2.ARCHIVED; g.updatedAt = now; if (!g.archivedAt) g.archivedAt = now; return { ...g, metadata: { ...g.metadata } }; }
|
|
1033
|
+
export function touchEvoGoalV2(id) { const g = _evoGoals.get(id); if (!g) throw new Error(`evo goal ${id} not found`); if (_evoGoalTerminal.has(g.status)) throw new Error(`cannot touch terminal evo goal ${id}`); const now = Date.now(); g.lastTouchedAt = now; g.updatedAt = now; return { ...g, metadata: { ...g.metadata } }; }
|
|
1034
|
+
export function getEvoGoalV2(id) { const g = _evoGoals.get(id); if (!g) return null; return { ...g, metadata: { ...g.metadata } }; }
|
|
1035
|
+
export function listEvoGoalsV2() { return [..._evoGoals.values()].map((g) => ({ ...g, metadata: { ...g.metadata } })); }
|
|
1036
|
+
|
|
1037
|
+
function _evoCountPending(gid) { let n = 0; for (const c of _evoCycles.values()) if (c.goalId === gid && (c.status === EVO_CYCLE_LIFECYCLE_V2.QUEUED || c.status === EVO_CYCLE_LIFECYCLE_V2.RUNNING)) n++; return n; }
|
|
1038
|
+
|
|
1039
|
+
export function createEvoCycleV2({ id, goalId, generation, metadata } = {}) {
|
|
1040
|
+
if (!id || typeof id !== "string") throw new Error("id is required");
|
|
1041
|
+
if (!goalId || typeof goalId !== "string") throw new Error("goalId is required");
|
|
1042
|
+
if (_evoCycles.has(id)) throw new Error(`evo cycle ${id} already exists`);
|
|
1043
|
+
if (!_evoGoals.has(goalId)) throw new Error(`evo goal ${goalId} not found`);
|
|
1044
|
+
const pending = _evoCountPending(goalId);
|
|
1045
|
+
if (pending >= _evoMaxPendingPerGoal) throw new Error(`max pending evo cycles per goal (${_evoMaxPendingPerGoal}) reached for ${goalId}`);
|
|
1046
|
+
const now = Date.now();
|
|
1047
|
+
const c = { id, goalId, generation: generation ?? 0, status: EVO_CYCLE_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
|
|
1048
|
+
_evoCycles.set(id, c);
|
|
1049
|
+
return { ...c, metadata: { ...c.metadata } };
|
|
1050
|
+
}
|
|
1051
|
+
function _evoCheckC(from, to) { const a = _evoCycleTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid evo cycle transition ${from} → ${to}`); }
|
|
1052
|
+
export function startEvoCycleV2(id) { const c = _evoCycles.get(id); if (!c) throw new Error(`evo cycle ${id} not found`); _evoCheckC(c.status, EVO_CYCLE_LIFECYCLE_V2.RUNNING); const now = Date.now(); c.status = EVO_CYCLE_LIFECYCLE_V2.RUNNING; c.updatedAt = now; if (!c.startedAt) c.startedAt = now; return { ...c, metadata: { ...c.metadata } }; }
|
|
1053
|
+
export function completeEvoCycleV2(id) { const c = _evoCycles.get(id); if (!c) throw new Error(`evo cycle ${id} not found`); _evoCheckC(c.status, EVO_CYCLE_LIFECYCLE_V2.COMPLETED); const now = Date.now(); c.status = EVO_CYCLE_LIFECYCLE_V2.COMPLETED; c.updatedAt = now; if (!c.settledAt) c.settledAt = now; return { ...c, metadata: { ...c.metadata } }; }
|
|
1054
|
+
export function failEvoCycleV2(id, reason) { const c = _evoCycles.get(id); if (!c) throw new Error(`evo cycle ${id} not found`); _evoCheckC(c.status, EVO_CYCLE_LIFECYCLE_V2.FAILED); const now = Date.now(); c.status = EVO_CYCLE_LIFECYCLE_V2.FAILED; c.updatedAt = now; if (!c.settledAt) c.settledAt = now; if (reason) c.metadata.failReason = String(reason); return { ...c, metadata: { ...c.metadata } }; }
|
|
1055
|
+
export function cancelEvoCycleV2(id, reason) { const c = _evoCycles.get(id); if (!c) throw new Error(`evo cycle ${id} not found`); _evoCheckC(c.status, EVO_CYCLE_LIFECYCLE_V2.CANCELLED); const now = Date.now(); c.status = EVO_CYCLE_LIFECYCLE_V2.CANCELLED; c.updatedAt = now; if (!c.settledAt) c.settledAt = now; if (reason) c.metadata.cancelReason = String(reason); return { ...c, metadata: { ...c.metadata } }; }
|
|
1056
|
+
export function getEvoCycleV2(id) { const c = _evoCycles.get(id); if (!c) return null; return { ...c, metadata: { ...c.metadata } }; }
|
|
1057
|
+
export function listEvoCyclesV2() { return [..._evoCycles.values()].map((c) => ({ ...c, metadata: { ...c.metadata } })); }
|
|
1058
|
+
|
|
1059
|
+
export function autoPauseIdleEvoGoalsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const g of _evoGoals.values()) if (g.status === EVO_GOAL_MATURITY_V2.ACTIVE && (t - g.lastTouchedAt) >= _evoGoalIdleMs) { g.status = EVO_GOAL_MATURITY_V2.PAUSED; g.updatedAt = t; flipped.push(g.id); } return { flipped, count: flipped.length }; }
|
|
1060
|
+
export function autoFailStuckEvoCyclesV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const c of _evoCycles.values()) if (c.status === EVO_CYCLE_LIFECYCLE_V2.RUNNING && c.startedAt != null && (t - c.startedAt) >= _evoCycleStuckMs) { c.status = EVO_CYCLE_LIFECYCLE_V2.FAILED; c.updatedAt = t; if (!c.settledAt) c.settledAt = t; c.metadata.failReason = "auto-fail-stuck"; flipped.push(c.id); } return { flipped, count: flipped.length }; }
|
|
1061
|
+
|
|
1062
|
+
export function getEvolutionSystemGovStatsV2() {
|
|
1063
|
+
const goalsByStatus = {}; for (const s of Object.values(EVO_GOAL_MATURITY_V2)) goalsByStatus[s] = 0; for (const g of _evoGoals.values()) goalsByStatus[g.status]++;
|
|
1064
|
+
const cyclesByStatus = {}; for (const s of Object.values(EVO_CYCLE_LIFECYCLE_V2)) cyclesByStatus[s] = 0; for (const c of _evoCycles.values()) cyclesByStatus[c.status]++;
|
|
1065
|
+
return { totalGoalsV2: _evoGoals.size, totalCyclesV2: _evoCycles.size, maxActiveEvoGoalsPerOwner: _evoMaxActivePerOwner, maxPendingEvoCyclesPerGoal: _evoMaxPendingPerGoal, evoGoalIdleMs: _evoGoalIdleMs, evoCycleStuckMs: _evoCycleStuckMs, goalsByStatus, cyclesByStatus };
|
|
1066
|
+
}
|
|
@@ -225,3 +225,108 @@ export class EvoMapManager {
|
|
|
225
225
|
};
|
|
226
226
|
}
|
|
227
227
|
}
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
// ===== V2 Surface: EvoMap Manager governance overlay (CLI v0.135.0) =====
|
|
231
|
+
export const EVOMAP_MAP_MATURITY_V2 = Object.freeze({
|
|
232
|
+
PENDING: "pending", ACTIVE: "active", STALE: "stale", ARCHIVED: "archived",
|
|
233
|
+
});
|
|
234
|
+
export const EVOMAP_EVOLUTION_LIFECYCLE_V2 = Object.freeze({
|
|
235
|
+
QUEUED: "queued", RUNNING: "running", COMPLETED: "completed", FAILED: "failed", CANCELLED: "cancelled",
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
const _emMapTrans = new Map([
|
|
239
|
+
[EVOMAP_MAP_MATURITY_V2.PENDING, new Set([EVOMAP_MAP_MATURITY_V2.ACTIVE, EVOMAP_MAP_MATURITY_V2.ARCHIVED])],
|
|
240
|
+
[EVOMAP_MAP_MATURITY_V2.ACTIVE, new Set([EVOMAP_MAP_MATURITY_V2.STALE, EVOMAP_MAP_MATURITY_V2.ARCHIVED])],
|
|
241
|
+
[EVOMAP_MAP_MATURITY_V2.STALE, new Set([EVOMAP_MAP_MATURITY_V2.ACTIVE, EVOMAP_MAP_MATURITY_V2.ARCHIVED])],
|
|
242
|
+
[EVOMAP_MAP_MATURITY_V2.ARCHIVED, new Set()],
|
|
243
|
+
]);
|
|
244
|
+
const _emMapTerminal = new Set([EVOMAP_MAP_MATURITY_V2.ARCHIVED]);
|
|
245
|
+
const _emEvoTrans = new Map([
|
|
246
|
+
[EVOMAP_EVOLUTION_LIFECYCLE_V2.QUEUED, new Set([EVOMAP_EVOLUTION_LIFECYCLE_V2.RUNNING, EVOMAP_EVOLUTION_LIFECYCLE_V2.CANCELLED])],
|
|
247
|
+
[EVOMAP_EVOLUTION_LIFECYCLE_V2.RUNNING, new Set([EVOMAP_EVOLUTION_LIFECYCLE_V2.COMPLETED, EVOMAP_EVOLUTION_LIFECYCLE_V2.FAILED, EVOMAP_EVOLUTION_LIFECYCLE_V2.CANCELLED])],
|
|
248
|
+
[EVOMAP_EVOLUTION_LIFECYCLE_V2.COMPLETED, new Set()],
|
|
249
|
+
[EVOMAP_EVOLUTION_LIFECYCLE_V2.FAILED, new Set()],
|
|
250
|
+
[EVOMAP_EVOLUTION_LIFECYCLE_V2.CANCELLED, new Set()],
|
|
251
|
+
]);
|
|
252
|
+
|
|
253
|
+
const _emMaps = new Map();
|
|
254
|
+
const _emEvos = new Map();
|
|
255
|
+
let _emMaxActivePerOwner = 10;
|
|
256
|
+
let _emMaxPendingPerMap = 15;
|
|
257
|
+
let _emMapIdleMs = 7 * 24 * 60 * 60 * 1000;
|
|
258
|
+
let _emEvoStuckMs = 5 * 60 * 1000;
|
|
259
|
+
|
|
260
|
+
function _emPos(n, lbl) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${lbl} must be positive integer`); return v; }
|
|
261
|
+
|
|
262
|
+
export function setMaxActiveEvoMapsPerOwnerV2(n) { _emMaxActivePerOwner = _emPos(n, "maxActiveEvoMapsPerOwner"); }
|
|
263
|
+
export function getMaxActiveEvoMapsPerOwnerV2() { return _emMaxActivePerOwner; }
|
|
264
|
+
export function setMaxPendingEvoEvolutionsPerMapV2(n) { _emMaxPendingPerMap = _emPos(n, "maxPendingEvoEvolutionsPerMap"); }
|
|
265
|
+
export function getMaxPendingEvoEvolutionsPerMapV2() { return _emMaxPendingPerMap; }
|
|
266
|
+
export function setEvoMapIdleMsV2(n) { _emMapIdleMs = _emPos(n, "evoMapIdleMs"); }
|
|
267
|
+
export function getEvoMapIdleMsV2() { return _emMapIdleMs; }
|
|
268
|
+
export function setEvoEvolutionStuckMsV2(n) { _emEvoStuckMs = _emPos(n, "evoEvolutionStuckMs"); }
|
|
269
|
+
export function getEvoEvolutionStuckMsV2() { return _emEvoStuckMs; }
|
|
270
|
+
|
|
271
|
+
export function _resetStateEvoMapManagerV2() {
|
|
272
|
+
_emMaps.clear(); _emEvos.clear();
|
|
273
|
+
_emMaxActivePerOwner = 10; _emMaxPendingPerMap = 15;
|
|
274
|
+
_emMapIdleMs = 7 * 24 * 60 * 60 * 1000; _emEvoStuckMs = 5 * 60 * 1000;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export function registerEvoMapV2({ id, owner, name, metadata } = {}) {
|
|
278
|
+
if (!id || typeof id !== "string") throw new Error("id is required");
|
|
279
|
+
if (!owner || typeof owner !== "string") throw new Error("owner is required");
|
|
280
|
+
if (_emMaps.has(id)) throw new Error(`evomap ${id} already registered`);
|
|
281
|
+
const now = Date.now();
|
|
282
|
+
const m = { id, owner, name: name || id, status: EVOMAP_MAP_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, archivedAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
|
|
283
|
+
_emMaps.set(id, m);
|
|
284
|
+
return { ...m, metadata: { ...m.metadata } };
|
|
285
|
+
}
|
|
286
|
+
function _emCheckM(from, to) { const a = _emMapTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid evomap transition ${from} → ${to}`); }
|
|
287
|
+
function _emCountActive(owner) { let n = 0; for (const m of _emMaps.values()) if (m.owner === owner && m.status === EVOMAP_MAP_MATURITY_V2.ACTIVE) n++; return n; }
|
|
288
|
+
|
|
289
|
+
export function activateEvoMapV2(id) {
|
|
290
|
+
const m = _emMaps.get(id); if (!m) throw new Error(`evomap ${id} not found`);
|
|
291
|
+
_emCheckM(m.status, EVOMAP_MAP_MATURITY_V2.ACTIVE);
|
|
292
|
+
const recovery = m.status === EVOMAP_MAP_MATURITY_V2.STALE;
|
|
293
|
+
if (!recovery) { const a = _emCountActive(m.owner); if (a >= _emMaxActivePerOwner) throw new Error(`max active evomaps per owner (${_emMaxActivePerOwner}) reached for ${m.owner}`); }
|
|
294
|
+
const now = Date.now(); m.status = EVOMAP_MAP_MATURITY_V2.ACTIVE; m.updatedAt = now; m.lastTouchedAt = now; if (!m.activatedAt) m.activatedAt = now;
|
|
295
|
+
return { ...m, metadata: { ...m.metadata } };
|
|
296
|
+
}
|
|
297
|
+
export function staleEvoMapV2(id) { const m = _emMaps.get(id); if (!m) throw new Error(`evomap ${id} not found`); _emCheckM(m.status, EVOMAP_MAP_MATURITY_V2.STALE); m.status = EVOMAP_MAP_MATURITY_V2.STALE; m.updatedAt = Date.now(); return { ...m, metadata: { ...m.metadata } }; }
|
|
298
|
+
export function archiveEvoMapV2(id) { const m = _emMaps.get(id); if (!m) throw new Error(`evomap ${id} not found`); _emCheckM(m.status, EVOMAP_MAP_MATURITY_V2.ARCHIVED); const now = Date.now(); m.status = EVOMAP_MAP_MATURITY_V2.ARCHIVED; m.updatedAt = now; if (!m.archivedAt) m.archivedAt = now; return { ...m, metadata: { ...m.metadata } }; }
|
|
299
|
+
export function touchEvoMapV2(id) { const m = _emMaps.get(id); if (!m) throw new Error(`evomap ${id} not found`); if (_emMapTerminal.has(m.status)) throw new Error(`cannot touch terminal evomap ${id}`); const now = Date.now(); m.lastTouchedAt = now; m.updatedAt = now; return { ...m, metadata: { ...m.metadata } }; }
|
|
300
|
+
export function getEvoMapV2(id) { const m = _emMaps.get(id); if (!m) return null; return { ...m, metadata: { ...m.metadata } }; }
|
|
301
|
+
export function listEvoMapsV2() { return [..._emMaps.values()].map((m) => ({ ...m, metadata: { ...m.metadata } })); }
|
|
302
|
+
|
|
303
|
+
function _emCountPending(mid) { let n = 0; for (const e of _emEvos.values()) if (e.mapId === mid && (e.status === EVOMAP_EVOLUTION_LIFECYCLE_V2.QUEUED || e.status === EVOMAP_EVOLUTION_LIFECYCLE_V2.RUNNING)) n++; return n; }
|
|
304
|
+
|
|
305
|
+
export function createEvoEvolutionV2({ id, mapId, strategy, metadata } = {}) {
|
|
306
|
+
if (!id || typeof id !== "string") throw new Error("id is required");
|
|
307
|
+
if (!mapId || typeof mapId !== "string") throw new Error("mapId is required");
|
|
308
|
+
if (_emEvos.has(id)) throw new Error(`evo evolution ${id} already exists`);
|
|
309
|
+
if (!_emMaps.has(mapId)) throw new Error(`evomap ${mapId} not found`);
|
|
310
|
+
const pending = _emCountPending(mapId);
|
|
311
|
+
if (pending >= _emMaxPendingPerMap) throw new Error(`max pending evo evolutions per map (${_emMaxPendingPerMap}) reached for ${mapId}`);
|
|
312
|
+
const now = Date.now();
|
|
313
|
+
const e = { id, mapId, strategy: strategy || "default", status: EVOMAP_EVOLUTION_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
|
|
314
|
+
_emEvos.set(id, e);
|
|
315
|
+
return { ...e, metadata: { ...e.metadata } };
|
|
316
|
+
}
|
|
317
|
+
function _emCheckE(from, to) { const a = _emEvoTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid evo evolution transition ${from} → ${to}`); }
|
|
318
|
+
export function startEvoEvolutionV2(id) { const e = _emEvos.get(id); if (!e) throw new Error(`evo evolution ${id} not found`); _emCheckE(e.status, EVOMAP_EVOLUTION_LIFECYCLE_V2.RUNNING); const now = Date.now(); e.status = EVOMAP_EVOLUTION_LIFECYCLE_V2.RUNNING; e.updatedAt = now; if (!e.startedAt) e.startedAt = now; return { ...e, metadata: { ...e.metadata } }; }
|
|
319
|
+
export function completeEvoEvolutionV2(id) { const e = _emEvos.get(id); if (!e) throw new Error(`evo evolution ${id} not found`); _emCheckE(e.status, EVOMAP_EVOLUTION_LIFECYCLE_V2.COMPLETED); const now = Date.now(); e.status = EVOMAP_EVOLUTION_LIFECYCLE_V2.COMPLETED; e.updatedAt = now; if (!e.settledAt) e.settledAt = now; return { ...e, metadata: { ...e.metadata } }; }
|
|
320
|
+
export function failEvoEvolutionV2(id, reason) { const e = _emEvos.get(id); if (!e) throw new Error(`evo evolution ${id} not found`); _emCheckE(e.status, EVOMAP_EVOLUTION_LIFECYCLE_V2.FAILED); const now = Date.now(); e.status = EVOMAP_EVOLUTION_LIFECYCLE_V2.FAILED; e.updatedAt = now; if (!e.settledAt) e.settledAt = now; if (reason) e.metadata.failReason = String(reason); return { ...e, metadata: { ...e.metadata } }; }
|
|
321
|
+
export function cancelEvoEvolutionV2(id, reason) { const e = _emEvos.get(id); if (!e) throw new Error(`evo evolution ${id} not found`); _emCheckE(e.status, EVOMAP_EVOLUTION_LIFECYCLE_V2.CANCELLED); const now = Date.now(); e.status = EVOMAP_EVOLUTION_LIFECYCLE_V2.CANCELLED; e.updatedAt = now; if (!e.settledAt) e.settledAt = now; if (reason) e.metadata.cancelReason = String(reason); return { ...e, metadata: { ...e.metadata } }; }
|
|
322
|
+
export function getEvoEvolutionV2(id) { const e = _emEvos.get(id); if (!e) return null; return { ...e, metadata: { ...e.metadata } }; }
|
|
323
|
+
export function listEvoEvolutionsV2() { return [..._emEvos.values()].map((e) => ({ ...e, metadata: { ...e.metadata } })); }
|
|
324
|
+
|
|
325
|
+
export function autoStaleIdleEvoMapsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const m of _emMaps.values()) if (m.status === EVOMAP_MAP_MATURITY_V2.ACTIVE && (t - m.lastTouchedAt) >= _emMapIdleMs) { m.status = EVOMAP_MAP_MATURITY_V2.STALE; m.updatedAt = t; flipped.push(m.id); } return { flipped, count: flipped.length }; }
|
|
326
|
+
export function autoFailStuckEvoEvolutionsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const e of _emEvos.values()) if (e.status === EVOMAP_EVOLUTION_LIFECYCLE_V2.RUNNING && e.startedAt != null && (t - e.startedAt) >= _emEvoStuckMs) { e.status = EVOMAP_EVOLUTION_LIFECYCLE_V2.FAILED; e.updatedAt = t; if (!e.settledAt) e.settledAt = t; e.metadata.failReason = "auto-fail-stuck"; flipped.push(e.id); } return { flipped, count: flipped.length }; }
|
|
327
|
+
|
|
328
|
+
export function getEvoMapManagerStatsV2() {
|
|
329
|
+
const mapsByStatus = {}; for (const s of Object.values(EVOMAP_MAP_MATURITY_V2)) mapsByStatus[s] = 0; for (const m of _emMaps.values()) mapsByStatus[m.status]++;
|
|
330
|
+
const evosByStatus = {}; for (const s of Object.values(EVOMAP_EVOLUTION_LIFECYCLE_V2)) evosByStatus[s] = 0; for (const e of _emEvos.values()) evosByStatus[e.status]++;
|
|
331
|
+
return { totalMapsV2: _emMaps.size, totalEvolutionsV2: _emEvos.size, maxActiveEvoMapsPerOwner: _emMaxActivePerOwner, maxPendingEvoEvolutionsPerMap: _emMaxPendingPerMap, evoMapIdleMs: _emMapIdleMs, evoEvolutionStuckMs: _emEvoStuckMs, mapsByStatus, evosByStatus };
|
|
332
|
+
}
|
|
@@ -237,3 +237,108 @@ export function createBackend(config = {}) {
|
|
|
237
237
|
return new LocalBackend(config.options || {});
|
|
238
238
|
}
|
|
239
239
|
}
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
// ===== V2 Surface: Execution Backend governance overlay (CLI v0.133.0) =====
|
|
243
|
+
export const EXECBE_BACKEND_MATURITY_V2 = Object.freeze({
|
|
244
|
+
PENDING: "pending", ACTIVE: "active", DEGRADED: "degraded", RETIRED: "retired",
|
|
245
|
+
});
|
|
246
|
+
export const EXECBE_JOB_LIFECYCLE_V2 = Object.freeze({
|
|
247
|
+
QUEUED: "queued", RUNNING: "running", SUCCEEDED: "succeeded", FAILED: "failed", CANCELLED: "cancelled",
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
const _ebBackendTrans = new Map([
|
|
251
|
+
[EXECBE_BACKEND_MATURITY_V2.PENDING, new Set([EXECBE_BACKEND_MATURITY_V2.ACTIVE, EXECBE_BACKEND_MATURITY_V2.RETIRED])],
|
|
252
|
+
[EXECBE_BACKEND_MATURITY_V2.ACTIVE, new Set([EXECBE_BACKEND_MATURITY_V2.DEGRADED, EXECBE_BACKEND_MATURITY_V2.RETIRED])],
|
|
253
|
+
[EXECBE_BACKEND_MATURITY_V2.DEGRADED, new Set([EXECBE_BACKEND_MATURITY_V2.ACTIVE, EXECBE_BACKEND_MATURITY_V2.RETIRED])],
|
|
254
|
+
[EXECBE_BACKEND_MATURITY_V2.RETIRED, new Set()],
|
|
255
|
+
]);
|
|
256
|
+
const _ebBackendTerminal = new Set([EXECBE_BACKEND_MATURITY_V2.RETIRED]);
|
|
257
|
+
const _ebJobTrans = new Map([
|
|
258
|
+
[EXECBE_JOB_LIFECYCLE_V2.QUEUED, new Set([EXECBE_JOB_LIFECYCLE_V2.RUNNING, EXECBE_JOB_LIFECYCLE_V2.CANCELLED])],
|
|
259
|
+
[EXECBE_JOB_LIFECYCLE_V2.RUNNING, new Set([EXECBE_JOB_LIFECYCLE_V2.SUCCEEDED, EXECBE_JOB_LIFECYCLE_V2.FAILED, EXECBE_JOB_LIFECYCLE_V2.CANCELLED])],
|
|
260
|
+
[EXECBE_JOB_LIFECYCLE_V2.SUCCEEDED, new Set()],
|
|
261
|
+
[EXECBE_JOB_LIFECYCLE_V2.FAILED, new Set()],
|
|
262
|
+
[EXECBE_JOB_LIFECYCLE_V2.CANCELLED, new Set()],
|
|
263
|
+
]);
|
|
264
|
+
|
|
265
|
+
const _ebBackends = new Map();
|
|
266
|
+
const _ebJobs = new Map();
|
|
267
|
+
let _ebMaxActivePerOwner = 6;
|
|
268
|
+
let _ebMaxPendingPerBackend = 20;
|
|
269
|
+
let _ebBackendIdleMs = 12 * 60 * 60 * 1000;
|
|
270
|
+
let _ebJobStuckMs = 10 * 60 * 1000;
|
|
271
|
+
|
|
272
|
+
function _ebPos(n, lbl) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${lbl} must be positive integer`); return v; }
|
|
273
|
+
|
|
274
|
+
export function setMaxActiveBackendsPerOwnerV2(n) { _ebMaxActivePerOwner = _ebPos(n, "maxActiveBackendsPerOwner"); }
|
|
275
|
+
export function getMaxActiveBackendsPerOwnerV2() { return _ebMaxActivePerOwner; }
|
|
276
|
+
export function setMaxPendingJobsPerBackendV2(n) { _ebMaxPendingPerBackend = _ebPos(n, "maxPendingJobsPerBackend"); }
|
|
277
|
+
export function getMaxPendingJobsPerBackendV2() { return _ebMaxPendingPerBackend; }
|
|
278
|
+
export function setBackendIdleMsV2(n) { _ebBackendIdleMs = _ebPos(n, "backendIdleMs"); }
|
|
279
|
+
export function getBackendIdleMsV2() { return _ebBackendIdleMs; }
|
|
280
|
+
export function setExecJobStuckMsV2(n) { _ebJobStuckMs = _ebPos(n, "execJobStuckMs"); }
|
|
281
|
+
export function getExecJobStuckMsV2() { return _ebJobStuckMs; }
|
|
282
|
+
|
|
283
|
+
export function _resetStateExecutionBackendV2() {
|
|
284
|
+
_ebBackends.clear(); _ebJobs.clear();
|
|
285
|
+
_ebMaxActivePerOwner = 6; _ebMaxPendingPerBackend = 20;
|
|
286
|
+
_ebBackendIdleMs = 12 * 60 * 60 * 1000; _ebJobStuckMs = 10 * 60 * 1000;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export function registerBackendV2({ id, owner, kind, metadata } = {}) {
|
|
290
|
+
if (!id || typeof id !== "string") throw new Error("id is required");
|
|
291
|
+
if (!owner || typeof owner !== "string") throw new Error("owner is required");
|
|
292
|
+
if (_ebBackends.has(id)) throw new Error(`backend ${id} already registered`);
|
|
293
|
+
const now = Date.now();
|
|
294
|
+
const b = { id, owner, kind: kind || "local", status: EXECBE_BACKEND_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, retiredAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
|
|
295
|
+
_ebBackends.set(id, b);
|
|
296
|
+
return { ...b, metadata: { ...b.metadata } };
|
|
297
|
+
}
|
|
298
|
+
function _ebCheckB(from, to) { const allowed = _ebBackendTrans.get(from); if (!allowed || !allowed.has(to)) throw new Error(`invalid backend transition ${from} → ${to}`); }
|
|
299
|
+
function _ebCountActive(owner) { let n = 0; for (const b of _ebBackends.values()) if (b.owner === owner && b.status === EXECBE_BACKEND_MATURITY_V2.ACTIVE) n++; return n; }
|
|
300
|
+
|
|
301
|
+
export function activateBackendV2(id) {
|
|
302
|
+
const b = _ebBackends.get(id); if (!b) throw new Error(`backend ${id} not found`);
|
|
303
|
+
_ebCheckB(b.status, EXECBE_BACKEND_MATURITY_V2.ACTIVE);
|
|
304
|
+
const recovery = b.status === EXECBE_BACKEND_MATURITY_V2.DEGRADED;
|
|
305
|
+
if (!recovery) { const a = _ebCountActive(b.owner); if (a >= _ebMaxActivePerOwner) throw new Error(`max active backends per owner (${_ebMaxActivePerOwner}) reached for ${b.owner}`); }
|
|
306
|
+
const now = Date.now(); b.status = EXECBE_BACKEND_MATURITY_V2.ACTIVE; b.updatedAt = now; b.lastTouchedAt = now; if (!b.activatedAt) b.activatedAt = now;
|
|
307
|
+
return { ...b, metadata: { ...b.metadata } };
|
|
308
|
+
}
|
|
309
|
+
export function degradeBackendV2(id) { const b = _ebBackends.get(id); if (!b) throw new Error(`backend ${id} not found`); _ebCheckB(b.status, EXECBE_BACKEND_MATURITY_V2.DEGRADED); b.status = EXECBE_BACKEND_MATURITY_V2.DEGRADED; b.updatedAt = Date.now(); return { ...b, metadata: { ...b.metadata } }; }
|
|
310
|
+
export function retireBackendV2(id) { const b = _ebBackends.get(id); if (!b) throw new Error(`backend ${id} not found`); _ebCheckB(b.status, EXECBE_BACKEND_MATURITY_V2.RETIRED); const now = Date.now(); b.status = EXECBE_BACKEND_MATURITY_V2.RETIRED; b.updatedAt = now; if (!b.retiredAt) b.retiredAt = now; return { ...b, metadata: { ...b.metadata } }; }
|
|
311
|
+
export function touchBackendV2(id) { const b = _ebBackends.get(id); if (!b) throw new Error(`backend ${id} not found`); if (_ebBackendTerminal.has(b.status)) throw new Error(`cannot touch terminal backend ${id}`); const now = Date.now(); b.lastTouchedAt = now; b.updatedAt = now; return { ...b, metadata: { ...b.metadata } }; }
|
|
312
|
+
export function getBackendV2(id) { const b = _ebBackends.get(id); if (!b) return null; return { ...b, metadata: { ...b.metadata } }; }
|
|
313
|
+
export function listBackendsV2() { return [..._ebBackends.values()].map((b) => ({ ...b, metadata: { ...b.metadata } })); }
|
|
314
|
+
|
|
315
|
+
function _ebCountPending(bid) { let n = 0; for (const j of _ebJobs.values()) if (j.backendId === bid && (j.status === EXECBE_JOB_LIFECYCLE_V2.QUEUED || j.status === EXECBE_JOB_LIFECYCLE_V2.RUNNING)) n++; return n; }
|
|
316
|
+
|
|
317
|
+
export function createExecJobV2({ id, backendId, command, metadata } = {}) {
|
|
318
|
+
if (!id || typeof id !== "string") throw new Error("id is required");
|
|
319
|
+
if (!backendId || typeof backendId !== "string") throw new Error("backendId is required");
|
|
320
|
+
if (_ebJobs.has(id)) throw new Error(`exec job ${id} already exists`);
|
|
321
|
+
if (!_ebBackends.has(backendId)) throw new Error(`backend ${backendId} not found`);
|
|
322
|
+
const pending = _ebCountPending(backendId);
|
|
323
|
+
if (pending >= _ebMaxPendingPerBackend) throw new Error(`max pending jobs per backend (${_ebMaxPendingPerBackend}) reached for ${backendId}`);
|
|
324
|
+
const now = Date.now();
|
|
325
|
+
const j = { id, backendId, command: command || "", status: EXECBE_JOB_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
|
|
326
|
+
_ebJobs.set(id, j);
|
|
327
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
328
|
+
}
|
|
329
|
+
function _ebCheckJ(from, to) { const allowed = _ebJobTrans.get(from); if (!allowed || !allowed.has(to)) throw new Error(`invalid exec job transition ${from} → ${to}`); }
|
|
330
|
+
export function startExecJobV2(id) { const j = _ebJobs.get(id); if (!j) throw new Error(`exec job ${id} not found`); _ebCheckJ(j.status, EXECBE_JOB_LIFECYCLE_V2.RUNNING); const now = Date.now(); j.status = EXECBE_JOB_LIFECYCLE_V2.RUNNING; j.updatedAt = now; if (!j.startedAt) j.startedAt = now; return { ...j, metadata: { ...j.metadata } }; }
|
|
331
|
+
export function succeedExecJobV2(id) { const j = _ebJobs.get(id); if (!j) throw new Error(`exec job ${id} not found`); _ebCheckJ(j.status, EXECBE_JOB_LIFECYCLE_V2.SUCCEEDED); const now = Date.now(); j.status = EXECBE_JOB_LIFECYCLE_V2.SUCCEEDED; j.updatedAt = now; if (!j.settledAt) j.settledAt = now; return { ...j, metadata: { ...j.metadata } }; }
|
|
332
|
+
export function failExecJobV2(id, reason) { const j = _ebJobs.get(id); if (!j) throw new Error(`exec job ${id} not found`); _ebCheckJ(j.status, EXECBE_JOB_LIFECYCLE_V2.FAILED); const now = Date.now(); j.status = EXECBE_JOB_LIFECYCLE_V2.FAILED; j.updatedAt = now; if (!j.settledAt) j.settledAt = now; if (reason) j.metadata.failReason = String(reason); return { ...j, metadata: { ...j.metadata } }; }
|
|
333
|
+
export function cancelExecJobV2(id, reason) { const j = _ebJobs.get(id); if (!j) throw new Error(`exec job ${id} not found`); _ebCheckJ(j.status, EXECBE_JOB_LIFECYCLE_V2.CANCELLED); const now = Date.now(); j.status = EXECBE_JOB_LIFECYCLE_V2.CANCELLED; j.updatedAt = now; if (!j.settledAt) j.settledAt = now; if (reason) j.metadata.cancelReason = String(reason); return { ...j, metadata: { ...j.metadata } }; }
|
|
334
|
+
export function getExecJobV2(id) { const j = _ebJobs.get(id); if (!j) return null; return { ...j, metadata: { ...j.metadata } }; }
|
|
335
|
+
export function listExecJobsV2() { return [..._ebJobs.values()].map((j) => ({ ...j, metadata: { ...j.metadata } })); }
|
|
336
|
+
|
|
337
|
+
export function autoDegradeIdleBackendsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const b of _ebBackends.values()) if (b.status === EXECBE_BACKEND_MATURITY_V2.ACTIVE && (t - b.lastTouchedAt) >= _ebBackendIdleMs) { b.status = EXECBE_BACKEND_MATURITY_V2.DEGRADED; b.updatedAt = t; flipped.push(b.id); } return { flipped, count: flipped.length }; }
|
|
338
|
+
export function autoFailStuckExecJobsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const j of _ebJobs.values()) if (j.status === EXECBE_JOB_LIFECYCLE_V2.RUNNING && j.startedAt != null && (t - j.startedAt) >= _ebJobStuckMs) { j.status = EXECBE_JOB_LIFECYCLE_V2.FAILED; j.updatedAt = t; if (!j.settledAt) j.settledAt = t; j.metadata.failReason = "auto-fail-stuck"; flipped.push(j.id); } return { flipped, count: flipped.length }; }
|
|
339
|
+
|
|
340
|
+
export function getExecutionBackendStatsV2() {
|
|
341
|
+
const backendsByStatus = {}; for (const s of Object.values(EXECBE_BACKEND_MATURITY_V2)) backendsByStatus[s] = 0; for (const b of _ebBackends.values()) backendsByStatus[b.status]++;
|
|
342
|
+
const jobsByStatus = {}; for (const s of Object.values(EXECBE_JOB_LIFECYCLE_V2)) jobsByStatus[s] = 0; for (const j of _ebJobs.values()) jobsByStatus[j.status]++;
|
|
343
|
+
return { totalBackendsV2: _ebBackends.size, totalJobsV2: _ebJobs.size, maxActiveBackendsPerOwner: _ebMaxActivePerOwner, maxPendingJobsPerBackend: _ebMaxPendingPerBackend, backendIdleMs: _ebBackendIdleMs, execJobStuckMs: _ebJobStuckMs, backendsByStatus, jobsByStatus };
|
|
344
|
+
}
|
package/src/lib/feature-flags.js
CHANGED
|
@@ -5,3 +5,88 @@ export {
|
|
|
5
5
|
setFeature,
|
|
6
6
|
getFlagInfo,
|
|
7
7
|
} from "../harness/feature-flags.js";
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
// =====================================================================
|
|
11
|
+
// Feature Flags V2 governance overlay (in-memory, atop legacy flags)
|
|
12
|
+
// =====================================================================
|
|
13
|
+
export const FFLAG_PROFILE_MATURITY_V2 = Object.freeze({
|
|
14
|
+
PENDING: "pending", ACTIVE: "active", PAUSED: "paused", ARCHIVED: "archived",
|
|
15
|
+
});
|
|
16
|
+
export const FFLAG_EVAL_LIFECYCLE_V2 = Object.freeze({
|
|
17
|
+
QUEUED: "queued", EVALUATING: "evaluating", EVALUATED: "evaluated", FAILED: "failed", CANCELLED: "cancelled",
|
|
18
|
+
});
|
|
19
|
+
const _fflagPTrans = new Map([
|
|
20
|
+
[FFLAG_PROFILE_MATURITY_V2.PENDING, new Set([FFLAG_PROFILE_MATURITY_V2.ACTIVE, FFLAG_PROFILE_MATURITY_V2.ARCHIVED])],
|
|
21
|
+
[FFLAG_PROFILE_MATURITY_V2.ACTIVE, new Set([FFLAG_PROFILE_MATURITY_V2.PAUSED, FFLAG_PROFILE_MATURITY_V2.ARCHIVED])],
|
|
22
|
+
[FFLAG_PROFILE_MATURITY_V2.PAUSED, new Set([FFLAG_PROFILE_MATURITY_V2.ACTIVE, FFLAG_PROFILE_MATURITY_V2.ARCHIVED])],
|
|
23
|
+
[FFLAG_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
|
|
24
|
+
]);
|
|
25
|
+
const _fflagPTerminal = new Set([FFLAG_PROFILE_MATURITY_V2.ARCHIVED]);
|
|
26
|
+
const _fflagETrans = new Map([
|
|
27
|
+
[FFLAG_EVAL_LIFECYCLE_V2.QUEUED, new Set([FFLAG_EVAL_LIFECYCLE_V2.EVALUATING, FFLAG_EVAL_LIFECYCLE_V2.CANCELLED])],
|
|
28
|
+
[FFLAG_EVAL_LIFECYCLE_V2.EVALUATING, new Set([FFLAG_EVAL_LIFECYCLE_V2.EVALUATED, FFLAG_EVAL_LIFECYCLE_V2.FAILED, FFLAG_EVAL_LIFECYCLE_V2.CANCELLED])],
|
|
29
|
+
[FFLAG_EVAL_LIFECYCLE_V2.EVALUATED, new Set()],
|
|
30
|
+
[FFLAG_EVAL_LIFECYCLE_V2.FAILED, new Set()],
|
|
31
|
+
[FFLAG_EVAL_LIFECYCLE_V2.CANCELLED, new Set()],
|
|
32
|
+
]);
|
|
33
|
+
const _fflagPsV2 = new Map();
|
|
34
|
+
const _fflagEsV2 = new Map();
|
|
35
|
+
let _fflagMaxActive = 15, _fflagMaxPending = 30, _fflagIdleMs = 30 * 24 * 60 * 60 * 1000, _fflagStuckMs = 30 * 1000;
|
|
36
|
+
function _fflagPos(n, label) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${label} must be positive integer`); return v; }
|
|
37
|
+
function _fflagCheckP(from, to) { const a = _fflagPTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid fflag profile transition ${from} → ${to}`); }
|
|
38
|
+
function _fflagCheckE(from, to) { const a = _fflagETrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid fflag eval transition ${from} → ${to}`); }
|
|
39
|
+
function _fflagCountActive(owner) { let c = 0; for (const p of _fflagPsV2.values()) if (p.owner === owner && p.status === FFLAG_PROFILE_MATURITY_V2.ACTIVE) c++; return c; }
|
|
40
|
+
function _fflagCountPending(profileId) { let c = 0; for (const e of _fflagEsV2.values()) if (e.profileId === profileId && (e.status === FFLAG_EVAL_LIFECYCLE_V2.QUEUED || e.status === FFLAG_EVAL_LIFECYCLE_V2.EVALUATING)) c++; return c; }
|
|
41
|
+
export function setMaxActiveFflagProfilesPerOwnerV2(n) { _fflagMaxActive = _fflagPos(n, "maxActiveFflagProfilesPerOwner"); }
|
|
42
|
+
export function getMaxActiveFflagProfilesPerOwnerV2() { return _fflagMaxActive; }
|
|
43
|
+
export function setMaxPendingFflagEvalsPerProfileV2(n) { _fflagMaxPending = _fflagPos(n, "maxPendingFflagEvalsPerProfile"); }
|
|
44
|
+
export function getMaxPendingFflagEvalsPerProfileV2() { return _fflagMaxPending; }
|
|
45
|
+
export function setFflagProfileIdleMsV2(n) { _fflagIdleMs = _fflagPos(n, "fflagProfileIdleMs"); }
|
|
46
|
+
export function getFflagProfileIdleMsV2() { return _fflagIdleMs; }
|
|
47
|
+
export function setFflagEvalStuckMsV2(n) { _fflagStuckMs = _fflagPos(n, "fflagEvalStuckMs"); }
|
|
48
|
+
export function getFflagEvalStuckMsV2() { return _fflagStuckMs; }
|
|
49
|
+
export function _resetStateFeatureFlagsV2() { _fflagPsV2.clear(); _fflagEsV2.clear(); _fflagMaxActive = 15; _fflagMaxPending = 30; _fflagIdleMs = 30 * 24 * 60 * 60 * 1000; _fflagStuckMs = 30 * 1000; }
|
|
50
|
+
export function registerFflagProfileV2({ id, owner, scope, metadata } = {}) {
|
|
51
|
+
if (!id || !owner) throw new Error("id and owner required");
|
|
52
|
+
if (_fflagPsV2.has(id)) throw new Error(`fflag profile ${id} already exists`);
|
|
53
|
+
const now = Date.now();
|
|
54
|
+
const p = { id, owner, scope: scope || "*", status: FFLAG_PROFILE_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, lastTouchedAt: now, activatedAt: null, archivedAt: null, metadata: { ...(metadata || {}) } };
|
|
55
|
+
_fflagPsV2.set(id, p); return { ...p, metadata: { ...p.metadata } };
|
|
56
|
+
}
|
|
57
|
+
export function activateFflagProfileV2(id) {
|
|
58
|
+
const p = _fflagPsV2.get(id); if (!p) throw new Error(`fflag profile ${id} not found`);
|
|
59
|
+
const isInitial = p.status === FFLAG_PROFILE_MATURITY_V2.PENDING;
|
|
60
|
+
_fflagCheckP(p.status, FFLAG_PROFILE_MATURITY_V2.ACTIVE);
|
|
61
|
+
if (isInitial && _fflagCountActive(p.owner) >= _fflagMaxActive) throw new Error(`max active fflag profiles for owner ${p.owner} reached`);
|
|
62
|
+
const now = Date.now(); p.status = FFLAG_PROFILE_MATURITY_V2.ACTIVE; p.updatedAt = now; p.lastTouchedAt = now;
|
|
63
|
+
if (!p.activatedAt) p.activatedAt = now;
|
|
64
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
65
|
+
}
|
|
66
|
+
export function pauseFflagProfileV2(id) { const p = _fflagPsV2.get(id); if (!p) throw new Error(`fflag profile ${id} not found`); _fflagCheckP(p.status, FFLAG_PROFILE_MATURITY_V2.PAUSED); p.status = FFLAG_PROFILE_MATURITY_V2.PAUSED; p.updatedAt = Date.now(); return { ...p, metadata: { ...p.metadata } }; }
|
|
67
|
+
export function archiveFflagProfileV2(id) { const p = _fflagPsV2.get(id); if (!p) throw new Error(`fflag profile ${id} not found`); _fflagCheckP(p.status, FFLAG_PROFILE_MATURITY_V2.ARCHIVED); const now = Date.now(); p.status = FFLAG_PROFILE_MATURITY_V2.ARCHIVED; p.updatedAt = now; if (!p.archivedAt) p.archivedAt = now; return { ...p, metadata: { ...p.metadata } }; }
|
|
68
|
+
export function touchFflagProfileV2(id) { const p = _fflagPsV2.get(id); if (!p) throw new Error(`fflag profile ${id} not found`); if (_fflagPTerminal.has(p.status)) throw new Error(`cannot touch terminal fflag profile ${id}`); const now = Date.now(); p.lastTouchedAt = now; p.updatedAt = now; return { ...p, metadata: { ...p.metadata } }; }
|
|
69
|
+
export function getFflagProfileV2(id) { const p = _fflagPsV2.get(id); if (!p) return null; return { ...p, metadata: { ...p.metadata } }; }
|
|
70
|
+
export function listFflagProfilesV2() { return [..._fflagPsV2.values()].map((p) => ({ ...p, metadata: { ...p.metadata } })); }
|
|
71
|
+
export function createFflagEvalV2({ id, profileId, key, metadata } = {}) {
|
|
72
|
+
if (!id || !profileId) throw new Error("id and profileId required");
|
|
73
|
+
if (_fflagEsV2.has(id)) throw new Error(`fflag eval ${id} already exists`);
|
|
74
|
+
if (!_fflagPsV2.has(profileId)) throw new Error(`fflag profile ${profileId} not found`);
|
|
75
|
+
if (_fflagCountPending(profileId) >= _fflagMaxPending) throw new Error(`max pending fflag evals for profile ${profileId} reached`);
|
|
76
|
+
const now = Date.now();
|
|
77
|
+
const e = { id, profileId, key: key || "", status: FFLAG_EVAL_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
|
|
78
|
+
_fflagEsV2.set(id, e); return { ...e, metadata: { ...e.metadata } };
|
|
79
|
+
}
|
|
80
|
+
export function evaluatingFflagEvalV2(id) { const e = _fflagEsV2.get(id); if (!e) throw new Error(`fflag eval ${id} not found`); _fflagCheckE(e.status, FFLAG_EVAL_LIFECYCLE_V2.EVALUATING); const now = Date.now(); e.status = FFLAG_EVAL_LIFECYCLE_V2.EVALUATING; e.updatedAt = now; if (!e.startedAt) e.startedAt = now; return { ...e, metadata: { ...e.metadata } }; }
|
|
81
|
+
export function evaluateFflagEvalV2(id) { const e = _fflagEsV2.get(id); if (!e) throw new Error(`fflag eval ${id} not found`); _fflagCheckE(e.status, FFLAG_EVAL_LIFECYCLE_V2.EVALUATED); const now = Date.now(); e.status = FFLAG_EVAL_LIFECYCLE_V2.EVALUATED; e.updatedAt = now; if (!e.settledAt) e.settledAt = now; return { ...e, metadata: { ...e.metadata } }; }
|
|
82
|
+
export function failFflagEvalV2(id, reason) { const e = _fflagEsV2.get(id); if (!e) throw new Error(`fflag eval ${id} not found`); _fflagCheckE(e.status, FFLAG_EVAL_LIFECYCLE_V2.FAILED); const now = Date.now(); e.status = FFLAG_EVAL_LIFECYCLE_V2.FAILED; e.updatedAt = now; if (!e.settledAt) e.settledAt = now; if (reason) e.metadata.failReason = String(reason); return { ...e, metadata: { ...e.metadata } }; }
|
|
83
|
+
export function cancelFflagEvalV2(id, reason) { const e = _fflagEsV2.get(id); if (!e) throw new Error(`fflag eval ${id} not found`); _fflagCheckE(e.status, FFLAG_EVAL_LIFECYCLE_V2.CANCELLED); const now = Date.now(); e.status = FFLAG_EVAL_LIFECYCLE_V2.CANCELLED; e.updatedAt = now; if (!e.settledAt) e.settledAt = now; if (reason) e.metadata.cancelReason = String(reason); return { ...e, metadata: { ...e.metadata } }; }
|
|
84
|
+
export function getFflagEvalV2(id) { const e = _fflagEsV2.get(id); if (!e) return null; return { ...e, metadata: { ...e.metadata } }; }
|
|
85
|
+
export function listFflagEvalsV2() { return [..._fflagEsV2.values()].map((e) => ({ ...e, metadata: { ...e.metadata } })); }
|
|
86
|
+
export function autoPauseIdleFflagProfilesV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const p of _fflagPsV2.values()) if (p.status === FFLAG_PROFILE_MATURITY_V2.ACTIVE && (t - p.lastTouchedAt) >= _fflagIdleMs) { p.status = FFLAG_PROFILE_MATURITY_V2.PAUSED; p.updatedAt = t; flipped.push(p.id); } return { flipped, count: flipped.length }; }
|
|
87
|
+
export function autoFailStuckFflagEvalsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const e of _fflagEsV2.values()) if (e.status === FFLAG_EVAL_LIFECYCLE_V2.EVALUATING && e.startedAt != null && (t - e.startedAt) >= _fflagStuckMs) { e.status = FFLAG_EVAL_LIFECYCLE_V2.FAILED; e.updatedAt = t; if (!e.settledAt) e.settledAt = t; e.metadata.failReason = "auto-fail-stuck"; flipped.push(e.id); } return { flipped, count: flipped.length }; }
|
|
88
|
+
export function getFeatureFlagsGovStatsV2() {
|
|
89
|
+
const profilesByStatus = {}; for (const v of Object.values(FFLAG_PROFILE_MATURITY_V2)) profilesByStatus[v] = 0; for (const p of _fflagPsV2.values()) profilesByStatus[p.status]++;
|
|
90
|
+
const evalsByStatus = {}; for (const v of Object.values(FFLAG_EVAL_LIFECYCLE_V2)) evalsByStatus[v] = 0; for (const e of _fflagEsV2.values()) evalsByStatus[e.status]++;
|
|
91
|
+
return { totalFflagProfilesV2: _fflagPsV2.size, totalFflagEvalsV2: _fflagEsV2.size, maxActiveFflagProfilesPerOwner: _fflagMaxActive, maxPendingFflagEvalsPerProfile: _fflagMaxPending, fflagProfileIdleMs: _fflagIdleMs, fflagEvalStuckMs: _fflagStuckMs, profilesByStatus, evalsByStatus };
|
|
92
|
+
}
|