Duplicate Code Opportunity
Summary
- Pattern: Two parsing helper functions —
parseModelMultipliers (18 lines) and parsePositiveNumber (3 lines) — are copy-pasted verbatim into both effective-token-guard.js and max-model-multiplier-guard.js.
- Locations:
containers/api-proxy/guards/effective-token-guard.js lines 41–62 and containers/api-proxy/guards/max-model-multiplier-guard.js lines 12–33
- Impact: ~27 duplicated lines in production guard logic; any bug fix or behaviour change must be applied twice
Evidence
containers/api-proxy/guards/effective-token-guard.js (lines 41–62)
function parseModelMultipliers(raw) {
if (!raw || String(raw).trim() === '') return {};
try {
const parsed = JSON.parse(raw);
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return {};
const result = {};
for (const [model, value] of Object.entries(parsed)) {
const num = Number(value);
if (Number.isFinite(num) && num > 0) {
result[model] = num;
}
}
return result;
} catch {
return {};
}
}
function parsePositiveNumber(raw) {
const value = Number(raw);
return Number.isFinite(value) && value > 0 ? value : null;
}
containers/api-proxy/guards/max-model-multiplier-guard.js (lines 12–33) — identical block.
Suggested Refactoring
A guard-utils.js module already exists at containers/api-proxy/guards/guard-utils.js and already exports parsePositiveInteger. Export the two duplicated helpers from that module:
// guard-utils.js — add these two exports
function parseModelMultipliers(raw) { /* ... */ }
function parsePositiveNumber(raw) { /* ... */ }
module.exports = { parsePositiveInteger, parseModelMultipliers, parsePositiveNumber };
Then in both guard files, replace the inline definitions with:
const { parsePositiveInteger, parseModelMultipliers, parsePositiveNumber } = require('./guard-utils');
Affected Files
containers/api-proxy/guards/effective-token-guard.js — lines 41–62
containers/api-proxy/guards/max-model-multiplier-guard.js — lines 12–33
containers/api-proxy/guards/guard-utils.js — add the two helpers here
Effort Estimate
Low
Detected by Duplicate Code Detector workflow. Run date: 2026-06-03
Generated by Duplicate Code Detector · sonnet46 935.6K · ◷
Duplicate Code Opportunity
Summary
parseModelMultipliers(18 lines) andparsePositiveNumber(3 lines) — are copy-pasted verbatim into botheffective-token-guard.jsandmax-model-multiplier-guard.js.containers/api-proxy/guards/effective-token-guard.jslines 41–62 andcontainers/api-proxy/guards/max-model-multiplier-guard.jslines 12–33Evidence
containers/api-proxy/guards/effective-token-guard.js(lines 41–62)containers/api-proxy/guards/max-model-multiplier-guard.js(lines 12–33) — identical block.Suggested Refactoring
A
guard-utils.jsmodule already exists atcontainers/api-proxy/guards/guard-utils.jsand already exportsparsePositiveInteger. Export the two duplicated helpers from that module:Then in both guard files, replace the inline definitions with:
Affected Files
containers/api-proxy/guards/effective-token-guard.js— lines 41–62containers/api-proxy/guards/max-model-multiplier-guard.js— lines 12–33containers/api-proxy/guards/guard-utils.js— add the two helpers hereEffort Estimate
Low
Detected by Duplicate Code Detector workflow. Run date: 2026-06-03