import type { ThrottleMode } from '../../shared/schemas/settings.schema.js'; export interface RuntimeLimits { mode: ThrottleMode; argon2MemoryCost: number; argon2Concurrency: number; argon2MaxQueue: number; scryptConcurrency: number; backupMaxGunzipBytes: number; backupMaxBodyBytes: number; sqliteCacheKb: number; sqliteMmapBytes: number; sqliteTempStore: 'FILE' | 'MEMORY'; maxActivePtys: number; maxRetainedExitedPtys: number; defaultPtyScrollbackBytes: number; maxConcurrentMetricPolls: number; staggerPolls: boolean; maxParallelSystemInspect: number; maxSystemCacheEntries: number; maxConcurrentWorkflows: number; maxWorkflowStepTargets: number; maxWorkflowQueue: number; maxStepLogBytes: number; streamReplayBytes: number; maxRamBroadcastJobs: number; maxConcurrentAiStreams: number; maxTunnelSocketsPerTunnel: number; maxGlobalTunnelSockets: number; } const PRESETS: Record = { 'low-resource': { mode: 'low-resource', argon2MemoryCost: 32768, argon2Concurrency: 1, argon2MaxQueue: 8, scryptConcurrency: 1, backupMaxGunzipBytes: 16 * 1024 * 1024, backupMaxBodyBytes: 8 * 1024 * 1024, sqliteCacheKb: 8000, sqliteMmapBytes: 0, sqliteTempStore: 'FILE', maxActivePtys: 12, maxRetainedExitedPtys: 5, defaultPtyScrollbackBytes: 262144, maxConcurrentMetricPolls: 4, staggerPolls: true, maxParallelSystemInspect: 2, maxSystemCacheEntries: 64, maxConcurrentWorkflows: 2, maxWorkflowStepTargets: 4, maxWorkflowQueue: 10, maxStepLogBytes: 131072, streamReplayBytes: 65536, maxRamBroadcastJobs: 5, maxConcurrentAiStreams: 2, maxTunnelSocketsPerTunnel: 64, maxGlobalTunnelSockets: 256, }, balanced: { mode: 'balanced', argon2MemoryCost: 65536, argon2Concurrency: 2, argon2MaxQueue: 16, scryptConcurrency: 2, backupMaxGunzipBytes: 32 * 1024 * 1024, backupMaxBodyBytes: 16 * 1024 * 1024, sqliteCacheKb: 32000, sqliteMmapBytes: 0, sqliteTempStore: 'FILE', maxActivePtys: 32, maxRetainedExitedPtys: 12, defaultPtyScrollbackBytes: 524288, maxConcurrentMetricPolls: 12, staggerPolls: true, maxParallelSystemInspect: 4, maxSystemCacheEntries: 128, maxConcurrentWorkflows: 6, maxWorkflowStepTargets: 12, maxWorkflowQueue: 25, maxStepLogBytes: 262144, streamReplayBytes: 131072, maxRamBroadcastJobs: 15, maxConcurrentAiStreams: 6, maxTunnelSocketsPerTunnel: 128, maxGlobalTunnelSockets: 512, }, unthrottled: { mode: 'unthrottled', argon2MemoryCost: 65536, argon2Concurrency: 4, argon2MaxQueue: 32, scryptConcurrency: 4, backupMaxGunzipBytes: 64 * 1024 * 1024, backupMaxBodyBytes: 32 * 1024 * 1024, sqliteCacheKb: 64000, sqliteMmapBytes: 134217728, sqliteTempStore: 'MEMORY', maxActivePtys: 128, maxRetainedExitedPtys: 30, defaultPtyScrollbackBytes: 524288, maxConcurrentMetricPolls: 0, staggerPolls: false, maxParallelSystemInspect: 16, maxSystemCacheEntries: 512, maxConcurrentWorkflows: 32, maxWorkflowStepTargets: 0, maxWorkflowQueue: 100, maxStepLogBytes: 524288, streamReplayBytes: 262144, maxRamBroadcastJobs: 25, maxConcurrentAiStreams: 16, maxTunnelSocketsPerTunnel: 512, maxGlobalTunnelSockets: 2048, }, }; export function normalizeThrottleMode(raw: string | undefined): ThrottleMode | undefined { if (raw === 'low-resource' || raw === 'balanced' || raw === 'unthrottled') { return raw; } return undefined; } export function resolveRuntimeLimits( mode: ThrottleMode = 'low-resource', envOverride?: string, ): RuntimeLimits { const effective = normalizeThrottleMode(envOverride) ?? mode; return PRESETS[effective] ?? PRESETS['low-resource']; }