Skip to content

[Duplicate Code] parseModelMultipliers and parsePositiveNumber duplicated verbatim in two guard modules #4291

Description

@github-actions

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 ·

  • expires on Jul 3, 2026, 10:29 PM UTC

Metadata

Metadata

Assignees

Type

No type

Fields

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