Skip to content

The Drift Log · Audit, then actually repair

Replacing Monorepo Duplicate Helpers with Compatibility Shims

19 November 2026 · 3 min read · 616 words · established

A repository drawn as stacked file strata with a blue scanning plane sweeping through

Prevent monorepo refactoring regressions by replacing duplicate utility implementations with forwarding shims before updating downstream call sites.

The Mass-Deletion Trap

An audit flag finds eleven distinct implementations of an exponential backoff retry loop across packages in a single monorepo. An engineer opens a pull request that deletes ten of them, introduces a shared utility package, and rewires forty-two call sites.

The pull request breaks in staging. Two services expected the retry helper to swallow ECONNRESET. A third relied on options passed as positional arguments rather than a configuration object. Another had subtle clock-drift handling baked into its jitter calculation. The pull request collects twenty review comments, incurs merge conflicts with active feature branches, and is closed without merging. The technical debt remains.

Direct mass-deletion fails because duplicate helpers rarely stay identical. Over time, code duplication develops behavioral drift. When monorepo refactoring couples the extraction of a canonical helper to the simultaneous modification of dozens of consumer files, the surface area for regression becomes too wide to merge cleanly.

The Geometry of Compatibility Shims

The correct posture isolates canonicalisation from call-site migration. You do not delete the duplicate files immediately; you turn them into compatibility shims that forward calls to the canonical implementation.

First, extract the most robust implementation into your shared library and harden its interface:

// packages/core-utils/src/retry.ts
export interface RetryOptions {
  attempts: number;
  backoffMs: number;
  shouldRetry?: (err: unknown) => boolean;
}

export async function retry<T>(
  fn: () => Promise<T>,
  options: RetryOptions
): Promise<T> {
  let attempt = 0;
  while (true) {
    try {
      return await fn();
    } catch (err) {
      attempt++;
      if (attempt >= options.attempts || (options.shouldRetry && !options.shouldRetry(err))) {
        throw err;
      }
      await new Promise((r) => setTimeout(r, options.backoffMs * 2 ** (attempt - 1)));
    }
  }
}

Second, leave the legacy helper file in place inside the consumer package, but replace its body with a translation layer:

// services/billing/src/utils/legacyRetry.ts
import { retry as canonicalRetry } from '@monorepo/core-utils';

/**
 * @deprecated Use `@monorepo/core-utils/retry` directly.
 */
export async function legacyRetry<T>(
  operation: () => Promise<T>,
  maxRetries = 3,
  delay = 1000
): Promise<T> {
  return canonicalRetry(operation, {
    attempts: maxRetries,
    backoffMs: delay,
    shouldRetry: (err) => (err as { code?: string })?.code !== 'FATAL_BILLING_ERROR',
  });
}

Downstream call sites inside services/billing do not change. The package's exports remain stable, unit tests pass without edits, and runtime execution collapses onto the unified engine.

Preserving Capabilities While Reducing Surface Area

As discussed in Turning Static Repository Audits into Executable Code Repairs, static audits that produce only lists of duplicate files add friction rather than value. An actionable remediation plan must produce code that can merge without requiring team-wide coordination.

Compatibility shims offer three distinct operational advantages:

  1. Zero downstream blast radius: The pull request touches only the shared utility package and the eleven duplicate file locations. Not a single consumer call site changes syntax.
  2. Preserved edge-case handling: If one consumer had a custom error filter, that policy lives explicitly inside the shim rather than being silently discarded or forced onto unrelated callers.
  3. Decoupled deprecation: You can apply @deprecated annotations and linters to prevent new imports while migrating legacy call sites opportunistically during normal feature development.

If a shim has no custom logic beyond parameter translation, it compiles down to an inline call. The runtime cost is zero, but the engineering savings are immediate: you have unified the core logic while keeping pull requests small, isolated, and safe to merge.

How SHPBL Handles Code Extraction

Harvesting reusable software from working repositories requires verifying that the extracted capability behaves correctly before it replaces local implementations.

SHPBL runs a model-independent extraction pipeline over existing codebases. Extracted primitives are tested inside a dedicated certification harness, which executes the artifact against real contracts and records a formal verdict: CERTIFIED, PROVISIONAL, INCONCLUSIVE, or FAILED.

When refactoring monorepos, automated workflows access these verified components via @shpbl/sdk or our HTTP API to supply certified canonical replacements. You can run a free repository evaluation to map duplicated helper logic across your existing codebases and identify clean candidates for extraction.

What to Do on Monday

Do not open a pull request that deletes duplicate utility files across multiple packages at once.

Pick one utility repeated across your monorepo—a retry wrapper, an HTTP header parser, or an error serializer. Build or promote a single canonical version in a shared directory with test coverage. Replace the bodies of the duplicate helpers with single-line forwarding shims that adapt legacy signatures to the canonical contract.

Merge the shims first. Migrate the call sites when you are already editing those files.

This post supports the longer argument in Turning Static Repository Audits into Executable Code Repairs.

Keep reading

Next in the log

The Strategic Master Library · written and reviewed under the house's own epistemic rules: nothing claimed that we cannot show.