Skip to content

[Duplicate Code] parsePositiveInteger logic triplicated across three guard modules #3559

@github-actions

Description

@github-actions

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 ·

  • expires on Jun 20, 2026, 10:10 PM UTC

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions