/**
 * Cognigate Lua Scripts - Atomic Redis Operations
 *
 * Lua scripts for atomic resource tracking operations in Redis.
 * These ensure consistency under concurrent access across multiple instances.
 *
 * @packageDocumentation
 * @module @vorionsys/cognigate/lua-scripts
 */
/**
 * Atomic increment with limit check.
 *
 * KEYS[1] - The hash key for execution state
 * ARGV[1] - Field name to increment (e.g., 'networkRequests', 'fileSystemOps')
 * ARGV[2] - Increment amount
 * ARGV[3] - Maximum allowed value (limit)
 *
 * Returns: [allowed (0 or 1), newValue]
 */
export declare const INCREMENT_AND_CHECK_LUA = "\nlocal key = KEYS[1]\nlocal field = ARGV[1]\nlocal increment = tonumber(ARGV[2])\nlocal limit = tonumber(ARGV[3])\n\n-- Get current value or default to 0\nlocal current = tonumber(redis.call('HGET', key, field) or '0')\nlocal newValue = current + increment\n\n-- Check if within limit\nif newValue <= limit then\n  redis.call('HSET', key, field, tostring(newValue))\n  return {1, newValue}\nelse\n  return {0, current}\nend\n";
/**
 * Update peak memory (only if higher than current peak).
 *
 * KEYS[1] - The hash key for execution state
 * ARGV[1] - New memory value in MB
 *
 * Returns: Current peak memory value
 */
export declare const UPDATE_MEMORY_PEAK_LUA = "\nlocal key = KEYS[1]\nlocal newMemory = tonumber(ARGV[1])\n\n-- Get current peak or default to 0\nlocal currentPeak = tonumber(redis.call('HGET', key, 'memoryPeakMb') or '0')\n\n-- Update only if new value is higher\nif newMemory > currentPeak then\n  redis.call('HSET', key, 'memoryPeakMb', tostring(newMemory))\n  return newMemory\nelse\n  return currentPeak\nend\n";
/**
 * Get all resource metrics atomically.
 *
 * KEYS[1] - The hash key for execution state
 *
 * Returns: [memoryPeakMb, cpuTimeMs, networkRequests, fileSystemOps, startTime]
 */
export declare const GET_RESOURCE_SNAPSHOT_LUA = "\nlocal key = KEYS[1]\n\nlocal memoryPeakMb = tonumber(redis.call('HGET', key, 'memoryPeakMb') or '0')\nlocal cpuTimeMs = tonumber(redis.call('HGET', key, 'cpuTimeMs') or '0')\nlocal networkRequests = tonumber(redis.call('HGET', key, 'networkRequests') or '0')\nlocal fileSystemOps = tonumber(redis.call('HGET', key, 'fileSystemOps') or '0')\nlocal startTime = tonumber(redis.call('HGET', key, 'startTime') or '0')\n\nreturn {memoryPeakMb, cpuTimeMs, networkRequests, fileSystemOps, startTime}\n";
/**
 * Check all resource limits atomically and return violation if any.
 *
 * KEYS[1] - The hash key for execution state
 * ARGV[1] - Memory limit in MB
 * ARGV[2] - CPU time limit in milliseconds (calculated from percent and elapsed time)
 * ARGV[3] - Network requests limit
 * ARGV[4] - File system operations limit
 * ARGV[5] - Current timestamp for wall time calculation
 * ARGV[6] - Timeout in milliseconds
 *
 * Returns: [shouldTerminate (0 or 1), violationType, currentValue, limit]
 * violationType: 0=none, 1=memory, 2=cpu, 3=timeout, 4=network, 5=filesystem
 */
export declare const CHECK_LIMITS_LUA = "\nlocal key = KEYS[1]\nlocal memoryLimit = tonumber(ARGV[1])\nlocal cpuTimeLimit = tonumber(ARGV[2])\nlocal networkLimit = tonumber(ARGV[3])\nlocal fsLimit = tonumber(ARGV[4])\nlocal currentTime = tonumber(ARGV[5])\nlocal timeoutMs = tonumber(ARGV[6])\n\n-- Get all current values\nlocal memoryPeakMb = tonumber(redis.call('HGET', key, 'memoryPeakMb') or '0')\nlocal cpuTimeMs = tonumber(redis.call('HGET', key, 'cpuTimeMs') or '0')\nlocal networkRequests = tonumber(redis.call('HGET', key, 'networkRequests') or '0')\nlocal fileSystemOps = tonumber(redis.call('HGET', key, 'fileSystemOps') or '0')\nlocal startTime = tonumber(redis.call('HGET', key, 'startTime') or '0')\n\n-- Calculate wall time\nlocal wallTimeMs = 0\nif startTime > 0 then\n  wallTimeMs = currentTime - startTime\nend\n\n-- Check memory limit\nif memoryLimit > 0 and memoryPeakMb > memoryLimit then\n  return {1, 1, memoryPeakMb, memoryLimit}\nend\n\n-- Check CPU time limit\nif cpuTimeLimit > 0 and cpuTimeMs > cpuTimeLimit then\n  return {1, 2, cpuTimeMs, cpuTimeLimit}\nend\n\n-- Check timeout\nif timeoutMs > 0 and wallTimeMs > timeoutMs then\n  return {1, 3, wallTimeMs, timeoutMs}\nend\n\n-- Check network requests limit\nif networkLimit > 0 and networkRequests > networkLimit then\n  return {1, 4, networkRequests, networkLimit}\nend\n\n-- Check file system operations limit\nif fsLimit > 0 and fileSystemOps > fsLimit then\n  return {1, 5, fileSystemOps, fsLimit}\nend\n\n-- No violations\nreturn {0, 0, 0, 0}\n";
/**
 * Initialize execution state atomically.
 *
 * KEYS[1] - The hash key for execution state
 * ARGV[1] - Start time timestamp
 * ARGV[2] - TTL in seconds
 * ARGV[3] - Memory limit in MB
 * ARGV[4] - CPU percent limit
 * ARGV[5] - Timeout in milliseconds
 * ARGV[6] - Network requests limit (or -1 for unlimited)
 * ARGV[7] - File system ops limit (or -1 for unlimited)
 *
 * Returns: 1 on success
 */
export declare const INIT_EXECUTION_LUA = "\nlocal key = KEYS[1]\nlocal startTime = ARGV[1]\nlocal ttl = tonumber(ARGV[2])\nlocal memoryLimit = ARGV[3]\nlocal cpuLimit = ARGV[4]\nlocal timeoutMs = ARGV[5]\nlocal networkLimit = ARGV[6]\nlocal fsLimit = ARGV[7]\n\n-- Set all initial values\nredis.call('HMSET', key,\n  'startTime', startTime,\n  'memoryPeakMb', '0',\n  'cpuTimeMs', '0',\n  'networkRequests', '0',\n  'fileSystemOps', '0',\n  'memoryLimit', memoryLimit,\n  'cpuLimit', cpuLimit,\n  'timeoutMs', timeoutMs,\n  'networkLimit', networkLimit,\n  'fsLimit', fsLimit,\n  'terminated', '0'\n)\n\n-- Set expiry\nredis.call('EXPIRE', key, ttl)\n\nreturn 1\n";
/**
 * Increment CPU time atomically.
 *
 * KEYS[1] - The hash key for execution state
 * ARGV[1] - Delta CPU time in milliseconds
 *
 * Returns: New total CPU time
 */
export declare const INCREMENT_CPU_TIME_LUA = "\nlocal key = KEYS[1]\nlocal delta = tonumber(ARGV[1])\n\nlocal current = tonumber(redis.call('HGET', key, 'cpuTimeMs') or '0')\nlocal newValue = current + delta\nredis.call('HSET', key, 'cpuTimeMs', tostring(newValue))\n\nreturn newValue\n";
/**
 * Mark execution as terminated.
 *
 * KEYS[1] - The hash key for execution state
 * ARGV[1] - Termination reason
 * ARGV[2] - Violation type
 *
 * Returns: 1 if marked, 0 if already terminated
 */
export declare const MARK_TERMINATED_LUA = "\nlocal key = KEYS[1]\nlocal reason = ARGV[1]\nlocal violation = ARGV[2]\n\nlocal terminated = redis.call('HGET', key, 'terminated')\nif terminated == '1' then\n  return 0\nend\n\nredis.call('HMSET', key,\n  'terminated', '1',\n  'terminationReason', reason,\n  'terminationViolation', violation,\n  'terminatedAt', tostring(redis.call('TIME')[1] * 1000)\n)\n\nreturn 1\n";
/**
 * Cleanup and get final metrics.
 *
 * KEYS[1] - The hash key for execution state
 *
 * Returns: [memoryPeakMb, cpuTimeMs, wallTimeMs, networkRequests, fileSystemOps]
 * (also deletes the key)
 */
export declare const CLEANUP_EXECUTION_LUA = "\nlocal key = KEYS[1]\n\n-- Get all values before deletion\nlocal memoryPeakMb = tonumber(redis.call('HGET', key, 'memoryPeakMb') or '0')\nlocal cpuTimeMs = tonumber(redis.call('HGET', key, 'cpuTimeMs') or '0')\nlocal networkRequests = tonumber(redis.call('HGET', key, 'networkRequests') or '0')\nlocal fileSystemOps = tonumber(redis.call('HGET', key, 'fileSystemOps') or '0')\nlocal startTime = tonumber(redis.call('HGET', key, 'startTime') or '0')\n\n-- Calculate wall time\nlocal currentTime = redis.call('TIME')\nlocal currentMs = tonumber(currentTime[1]) * 1000 + math.floor(tonumber(currentTime[2]) / 1000)\nlocal wallTimeMs = 0\nif startTime > 0 then\n  wallTimeMs = currentMs - startTime\nend\n\n-- Delete the key\nredis.call('DEL', key)\n\nreturn {memoryPeakMb, cpuTimeMs, wallTimeMs, networkRequests, fileSystemOps}\n";
//# sourceMappingURL=lua-scripts.d.ts.map