DEV Community

Cover image for "I moved 58 magic numbers with an AI coding tool. Two checks made me trust the result."
Houssain El marouni
Houssain El marouni

Posted on AI-assisted

"I moved 58 magic numbers with an AI coding tool. Two checks made me trust the result."

published: true
description: "An open-source refactor, a lint rule that kept missing cases, and the two checks I ran before pushing AI-written code."

tags: ai, opensource, typescript, testing

GitNexus turns a codebase into a knowledge graph: symbols, calls, imports, files inside folders. Links in that graph carry a confidence value: a number for how sure the graph is that the link is real.

Its ticket #948 asks for the confidence and weight constants to come from gitnexus-shared, with no numbers typed inline. The weights already did; the edge confidences didn't. My PR, #4187, does that. An AI coding agent made the edits. This post is about what it took before I trusted them.

What changed

There is now one as const map, EdgeConfidence, with 32 named keys, and 58 places in the code read from it. No value changed. Some before/after pairs from the diff:

// before
const ROUTE_EDGE_CONFIDENCE = 0.5;
confidence: confidence * 0.8,

// after
const ROUTE_EDGE_CONFIDENCE = EdgeConfidence.routeControllerCall;
confidence: confidence * EdgeConfidence.guessedRouteMethodMultiplier,
Enter fullscreen mode Exit fullscreen mode

The PR also adds a local ESLint rule, gitnexus/no-inline-confidence, so a new inline value in the covered files fails lint.

The first pass missed values

The first audit searched for lines shaped like confidence: 0.85. That missed two shapes:

// a conditional
const confidence = site.kind === 'write' || site.kind === 'read' ? 1.0 : 0.85;

// a positional argument: the 8th parameter of tryEmitEdge is `confidence`
const ok = tryEmitEdge(
  graph,
  scopes,
  nodeLookup,
  site,
  memberDef,
  'global',
  seen,
  0.85,
  collapse,
  calleeCapture,
Enter fullscreen mode Exit fullscreen mode

The project's review bot flagged a constant that doesn't exist, in a file that doesn't exist. But it was pointing in the right direction: the pass it meant still had inline values in exactly these shapes.

So instead of fixing one more shape, a sweep listed every decimal literal in the covered files, and each one was read. That found 20 more sites. After converting them, the same sweep leaves 6 lines, and none of them is a confidence: five langRatio progress values in pipeline/phase.ts and one heap-size check in pipeline/run.ts.

The lint rule kept missing cases

The rule had the same weakness as the audit. It started with plain literals (confidence: 0.85), and every version listed the shapes it checked. The bot kept finding the next one:

  1. A static template key: { [`confidence`]: 0.5 }.
  2. Compound assignment and arithmetic other than *: edge.confidence *= 0.8, 1 - penalty.

What ended it was to stop listing. The final rule walks the whole value assigned to a confidence name, including *=, += and the rest, and reports every numeric literal inside it. It skips only what can't be a confidence:

  • the test of a conditional (targets.size === 1 ? … : …)
  • the operands of a comparison
  • a computed index like a[0]

One limit stays: a positional argument can't be matched by name, so the tryEmitEdge calls were converted but the rule can't guard them.

Check 1: compare every number, without the agent

The agent reported that every value was unchanged. Instead of taking its word, a separate script walked the diff from main to the PR, hunk by hunk. For each hunk it collected the numbers that were removed and the EdgeConfidence keys that were added, looked up each key's value in the map, and compared the two lists.

53 of 54 hunks matched automatically. The 54th had a comparison (targets.size === 1) on the same line as the two values. It was checked by hand: 0.8 became singleCallableValueTarget (0.8), and 0.7 became multipleCallableValueTargets (0.7).

Check 2: run the rule on real code, then plant bad values

The last commit only touched the rule and its test, so it couldn't change how GitNexus behaves. It could still be wrong. Before pushing, I ran three checks in my own terminal:

  1. The rule's tests pass.
  2. The rule runs clean on all the real code it covers.
  3. Planted values: append known-bad lines to a covered file, count the reports, then restore the file.
f=gitnexus/src/core/ingestion/structure-processor.ts
printf '\nexport const vA = { confidence: 1 - 0.2 };\nexport const vB = { confidence: 0 };\nvB.confidence *= 0.8;\nexport const vC = { confidence: vA.confidence === 1 ? vA.confidence : vB.confidence };\n' >> "$f"
n=$(npx eslint --quiet "$f" | grep -c "no-inline-confidence"); git checkout -- "$f"
[ "$n" = "4" ] && echo PASS || echo "FAIL: $n"
Enter fullscreen mode Exit fullscreen mode

vA holds two bad numbers, vB and the *= line hold one each, so the guard must report exactly 4, and nothing for the === 1 in vC.

The first time through, check 2 failed. The rule flagged the 1 in targets.size === 1 in callable-value-flow.ts. That's a count, not a confidence. At first it looked like a copy-paste error, but the log showed the command had run correctly. The fix was the "skip comparisons and conditional tests" rule above, and the planted vC line now guards it.

Where it stands

All four findings from the review bot are resolved. The PR is open and waiting for a maintainer's review.

What I'm keeping from this: when the agent's summary says "all values unchanged" and "lint passes", I run a check that doesn't depend on its summary.

If you review AI-written code, what do you check that the tool's own report can't tell you?


This article and the code changes in the PR were made with the help of AI (Claude and an AI coding agent). Every number above was checked against the PR before publishing.

Top comments (5)

Collapse
 
userxnom profile image
Abdullah Naseer •

The part I keep coming back to is that the audit and the lint rule failed in exactly the same way, and you fixed both in exactly the same way. Listing the shapes you knew about — confidence: 0.85, then the conditional, then the positional argument — kept missing the next shape, and the review bot kept finding it. Walking the whole value assigned to a confidence name instead of listing shapes is the real fix, and it is a lesson that transfers well beyond this PR.

Check 1 is what made me trust the result, not the agent's summary. Comparing every number hunk by hunk with a separate script, getting 53 of 54 to match automatically, and then hand-checking the 54th — where 0.8 became singleCallableValueTarget(0.8) and 0.7 became multipleCallableValueTargets(0.7) — is the kind of verification that actually catches a silent value change. And planting bad values in check 2, expecting exactly 4 reports and nothing for the === 1 in vC, is a proper test of the guard itself, not just of the code it guards.

To answer your closing question: the thing I check that a tool's report cannot tell me is behavior, not the diff. I would run the graph on the same input before and after and compare the outputs, because numbers can match while the wiring behind them changed. Have you thought about closing the last gap — the positional tryEmitEdge calls the rule cannot guard — or would you rather leave that as a documented exception?

Collapse
 
houssain_elmarouni_09942 profile image
Houssain El marouni •

Thanks, Abdullah. You're right that Check 1 only proves the numbers. I didn't run a before/after graph diff. My evidence for behavior is the repo's test suite, and that's weaker than diffing the graph itself.

There's one gap neither check closes. Six keys share 0.85 (resolvedReference, subtypeDispatch, resolvedFreeCall, resolvedInheritance, interfaceImplementation, mroSingleInterfaceDefault). A site pointing at the wrong 0.85 key would pass the value match, and a graph diff wouldn't see it either. The only way to catch it is to read each key's name against its call site.

On tryEmitEdge: I'm leaving it as a documented exception in this PR. confidence is its 8th positional parameter, with 17 call sites in src. The clean fix is an options object, so the existing rule catches confidence: by name. But that changes a shared API beyond what #948 asked for, so I'd rather the maintainers decide. A quicker fix would be a rule option naming the function and the argument position, but it breaks silently if the signature changes.

Collapse
 
dosaygo profile image
DOSAYGO •

Beyond inline numeric literals, how do you check that the coding agent reused existing shared constants across packages instead of defining a second constants map in another module?

Collapse
 
houssain_elmarouni_09942 profile image
Houssain El marouni •

Good question. The lint rule doesn't answer it: it only looks at names (confidence, *CONFIDENCE). A second map with a neutral name, like scores = { free: 0.85 }, would pass lint.

What I checked on the diff (154b0b5 → 69dcc0b):

  1. git diff --name-status --diff-filter=A shows 3 new files: the map, the rule and the rule's test. No other new module.
  2. All 17 files that use the values import EdgeConfidence from the gitnexus-shared package. None imports a local copy.
  3. Outside the map and the rule, the only number in the added lines is the targets.size === 1 count.
  4. Check 1 ties each replaced number to an EdgeConfidence key, so a value pointing at another map would have failed it.

The harder question was whether a new map should exist at all, since gitnexus-shared already has EvidenceWeights. Those weights are added together to rank candidates (local 0.55, import 0.45…). They aren't the fixed value written on an edge, so the confidences got their own map next to it.

Collapse
 
dosaygo profile image
DOSAYGO •

Thanks for spelling out the distinction: EvidenceWeights ranks candidates, while EdgeConfidence records fixed edge values, and the diff and imports show where the new map is used. I’m building AI Lint, whose checklist and reuse rule ask agents to inspect existing code before adding a parallel solution; its free edition is for personal use, and company teams use paid packs or a Company license: ai-lint.dosaygo.com/go/forum-20261...