Duplicate Code Opportunity
Summary
- Pattern: A 5-line "parse positive integer from env var" function body is copy-pasted identically into three separate guard modules, each giving it a different name
- Locations:
containers/api-proxy/guards/max-runs-guard.js line 13, containers/api-proxy/guards/timeout-steering.js line 28, containers/api-proxy/guards/effective-token-guard.js line 36
- Impact: 3 identical functions; a logic fix (e.g. allowing float parsing, changing the zero check) must be applied in three places
Evidence
All three files contain the exact same function body, only differing in the function name:
guards/max-runs-guard.js:13
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;
}
guards/timeout-steering.js:28
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;
}
guards/effective-token-guard.js:36
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;
}
Suggested Refactoring
Extract a shared parsePositiveInteger(raw) helper in a new containers/api-proxy/guards/guard-utils.js:
/**
* Parses a raw value as a positive integer.
* Returns null if the value is absent, blank, non-integer, or <= 0.
*/
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 };
Each guard module then replaces its local function with:
const { parsePositiveInteger } = require('./guard-utils');
// ...
const max = parsePositiveInteger(process.env.AWF_MAX_RUNS);
Affected Files
containers/api-proxy/guards/max-runs-guard.js — lines 13–17
containers/api-proxy/guards/timeout-steering.js — lines 28–32
containers/api-proxy/guards/effective-token-guard.js — lines 36–40
Effort Estimate
Low
Detected by Duplicate Code Detector workflow. Run date: 2026-05-21
Generated by Duplicate Code Detector · ● 10.6M · ◷
Duplicate Code Opportunity
Summary
containers/api-proxy/guards/max-runs-guard.jsline 13,containers/api-proxy/guards/timeout-steering.jsline 28,containers/api-proxy/guards/effective-token-guard.jsline 36Evidence
All three files contain the exact same function body, only differing in the function name:
guards/max-runs-guard.js:13guards/timeout-steering.js:28guards/effective-token-guard.js:36Suggested Refactoring
Extract a shared
parsePositiveInteger(raw)helper in a newcontainers/api-proxy/guards/guard-utils.js:Each guard module then replaces its local function with:
Affected Files
containers/api-proxy/guards/max-runs-guard.js— lines 13–17containers/api-proxy/guards/timeout-steering.js— lines 28–32containers/api-proxy/guards/effective-token-guard.js— lines 36–40Effort Estimate
Low
Detected by Duplicate Code Detector workflow. Run date: 2026-05-21