diff --git a/containers/api-proxy/guards/effective-token-guard.js b/containers/api-proxy/guards/effective-token-guard.js index bc7aeaf3c..f7fa65752 100644 --- a/containers/api-proxy/guards/effective-token-guard.js +++ b/containers/api-proxy/guards/effective-token-guard.js @@ -1,5 +1,7 @@ 'use strict'; +const { parsePositiveInteger } = require('./guard-utils'); + const ET_WARNING_THRESHOLDS = [80, 90, 95, 99]; const ET_DEFAULT_WEIGHTS = Object.freeze({ @@ -33,13 +35,6 @@ const effectiveTokenConfigCache = { parsed: { max: null, multipliers: {} }, }; -function parseMaxEffectiveTokens(raw) { - if (raw === undefined || raw === null || String(raw).trim() === '') return null; - const parsed = Number(raw); - if (!Number.isInteger(parsed) || parsed <= 0) return null; - return parsed; -} - function parseModelMultipliers(raw) { if (!raw || String(raw).trim() === '') return {}; try { @@ -69,7 +64,7 @@ function getEffectiveTokenConfig() { effectiveTokenConfigCache.rawMultipliers = rawMultipliers; const parsedMultipliers = Object.freeze(parseModelMultipliers(rawMultipliers)); effectiveTokenConfigCache.parsed = { - max: parseMaxEffectiveTokens(rawMax), + max: parsePositiveInteger(rawMax), multipliers: parsedMultipliers, }; return effectiveTokenConfigCache.parsed; diff --git a/containers/api-proxy/guards/guard-utils.js b/containers/api-proxy/guards/guard-utils.js new file mode 100644 index 000000000..84b0a9c72 --- /dev/null +++ b/containers/api-proxy/guards/guard-utils.js @@ -0,0 +1,12 @@ +'use strict'; + +function parsePositiveInteger(raw) { + if (raw === undefined || raw === null || String(raw).trim() === '') return null; + const parsed = Number(raw); + if (!Number.isInteger(parsed) || parsed <= 0) return null; + return parsed; +} + +module.exports = { + parsePositiveInteger, +}; diff --git a/containers/api-proxy/guards/guard-utils.test.js b/containers/api-proxy/guards/guard-utils.test.js new file mode 100644 index 000000000..9edcfe7f7 --- /dev/null +++ b/containers/api-proxy/guards/guard-utils.test.js @@ -0,0 +1,28 @@ +const { parsePositiveInteger } = require('./guard-utils'); + +describe('guard-utils', () => { + describe('parsePositiveInteger', () => { + it.each([ + undefined, + null, + '', + ' ', + 0, + '0', + -1, + '-1', + '1.5', + 'abc', + ])('returns null for %p', (raw) => { + expect(parsePositiveInteger(raw)).toBeNull(); + }); + + it.each([ + [1, 1], + ['1', 1], + [' 42 ', 42], + ])('for raw value %p returns %p', (raw, expected) => { + expect(parsePositiveInteger(raw)).toBe(expected); + }); + }); +}); diff --git a/containers/api-proxy/guards/max-runs-guard.js b/containers/api-proxy/guards/max-runs-guard.js index 13f598e21..e6d153a20 100644 --- a/containers/api-proxy/guards/max-runs-guard.js +++ b/containers/api-proxy/guards/max-runs-guard.js @@ -1,5 +1,7 @@ 'use strict'; +const { parsePositiveInteger } = require('./guard-utils'); + let maxRunsGuardState = { configKey: null, invocationCount: 0, @@ -10,20 +12,13 @@ const maxRunsConfigCache = { parsed: null, }; -function parseMaxRuns(raw) { - if (raw === undefined || raw === null || String(raw).trim() === '') return null; - const parsed = Number(raw); - if (!Number.isInteger(parsed) || parsed <= 0) return null; - return parsed; -} - function getMaxRunsConfig() { const rawMax = process.env.AWF_MAX_RUNS; if (maxRunsConfigCache.rawMax === rawMax) { return maxRunsConfigCache.parsed; } maxRunsConfigCache.rawMax = rawMax; - maxRunsConfigCache.parsed = parseMaxRuns(rawMax); + maxRunsConfigCache.parsed = parsePositiveInteger(rawMax); return maxRunsConfigCache.parsed; } diff --git a/containers/api-proxy/guards/timeout-steering.js b/containers/api-proxy/guards/timeout-steering.js index 58406fb22..035527b7e 100644 --- a/containers/api-proxy/guards/timeout-steering.js +++ b/containers/api-proxy/guards/timeout-steering.js @@ -1,6 +1,7 @@ 'use strict'; const { ET_WARNING_THRESHOLDS } = require('./effective-token-guard'); +const { parsePositiveInteger } = require('./guard-utils'); const TIMEOUT_STEERING_MESSAGES = { 80: 'You have used 80% of your allotted run time. Begin planning to wrap up your current work.', @@ -25,20 +26,13 @@ const timeoutSteeringConfigCache = { parsedMinutes: null, }; -function parseAgentTimeoutMinutes(raw) { - if (raw === undefined || raw === null || String(raw).trim() === '') return null; - const parsed = Number(raw); - if (!Number.isInteger(parsed) || parsed <= 0) return null; - return parsed; -} - function getTimeoutSteeringConfig() { const rawMinutes = process.env.AWF_AGENT_TIMEOUT_MINUTES; if (timeoutSteeringConfigCache.rawMinutes === rawMinutes) { return timeoutSteeringConfigCache.parsedMinutes; } timeoutSteeringConfigCache.rawMinutes = rawMinutes; - timeoutSteeringConfigCache.parsedMinutes = parseAgentTimeoutMinutes(rawMinutes); + timeoutSteeringConfigCache.parsedMinutes = parsePositiveInteger(rawMinutes); return timeoutSteeringConfigCache.parsedMinutes; }