DEV Community

Cover image for 1 Cent Feels Too Small to Care About (Until Your AI SQL Rewrite Hits Production)
Taufiqurrohman
Taufiqurrohman

Posted on Edited on AI-assisted

1 Cent Feels Too Small to Care About (Until Your AI SQL Rewrite Hits Production)

One cent feels too small to bother with.

Most engineers think so too.

"Got a dusty 1998 PL/pgSQL stored procedure running payroll? Just paste it into an LLM, ask for TypeScript, write five unit tests, and ship it."

I tested that exact advice.

All five unit tests turned green. The code looked clean. Any code reviewer on a Friday afternoon would have approved the PR.

Then I pointed a fuzzer at it against live PostgreSQL 16.

Within milliseconds, PostgreSQL returned 1872.62.
The "clean" LLM TypeScript returned 1872.61.

One penny off. Not a crash. Not an exception. Just a quiet -$0.01 leak sitting inside a payroll ledger.

Run that across a few thousand employees or loan accounts every month, and that "tiny" penny turns into a reconciliation nightmare nobody can trace.


Why "Passing Tests" Lied to Us

The bug has nothing to do with writing a better prompt.

It has to do with two quiet defaults nobody notices until month-end closing:

  1. IEEE-754 floating-point math: In JavaScript, 0.1 + 0.2 is 0.30000000000000004. Multiply fractional hours by hourly rates and progressive tax brackets, and binary floats start drifting at the third decimal place.
  2. Rounding mode mismatch: PostgreSQL NUMERIC rounds half away from zero (ROUND_HALF_UP). Naive JavaScript rewrites reach for Math.round(val * 100) / 100, which breaks on boundary halves and floating-point representation errors.

Five hand-picked unit tests almost never land on the exact fractional boundary where those two defaults collide.

A property-based fuzzer finds it in under 200 milliseconds.


Handing the Hard Part to Kiro

For Kiroween 2026 (Category: Resurrection), I built NecroSQL — a deterministic legacy SQL resurrection engine that refuses to guess.

Instead of asking an AI to "vibe-code" a migration and praying it holds up, I handed the strict engineering rules to Kiro inside a version-controlled /.kiro/ directory at the root of the repo.

Here is the 5-part loop that keeps the AI honest:

1. Specs Before Code (.kiro/specs/necrosql-resurrection/requirements.md)

Before a single line of TypeScript is written, NecroSQL validates the legacy SQL directly inside PostgreSQL 16, extracts a deterministic AST and Control-Flow Graph (CFG), and compiles every IF / ELSIF / ELSE branch and ROUND() call into formal Kiro EARS specifications (WHEN ... THE SYSTEM SHALL ...).

2. Zero-Float Steering Guardrails (.kiro/steering/resurrection-guardrails.md)

This file is set to inclusion: always. Its job is blunt:

  • Strictly ban native JS floating-point arithmetic (+, -, *, /, Math.round) in financial services.
  • Mandate decimal.js with explicit Decimal.ROUND_HALF_UP.
  • Mandate Zod input validation schemas so malformed numbers never sneak in.

3. Automated PBT Agent Hook (.kiro/hooks/pbt-on-resurrect.kiro.hook)

Whenever any resurrected service under packages/resurrected-services/src/**/*.ts is edited, Kiro automatically fires npm run test:pbt.

Under the hood, fast-check blasts 1,000+ randomized edge-case inputs simultaneously at two targets:

  • The authentic 1998 stored procedure running in a live PostgreSQL 16 container
  • The resurrected TypeScript microservice

If even one run out of 1,000 drifts by a single cent, fast-check shrinks it to the smallest reproducible counterexample and blocks the pass.

4. Custom Slash Workflow & MCP Server

  • .kiro/prompts/resurrect-legacy.md chains AST extraction, guardrail checks, synthesis, and PBT into one repeatable workflow.
  • .kiro/settings/mcp.json connects a custom stdio JSON-RPC 2.0 MCP server (packages/mcp-server/src/index.ts) exposing extract_legacy_ast and compare_single_execution directly to Kiro.

Don't Take My Word for It — Break It Yourself

A static screenshot saying "100% passed" is easy to fake. Skeptical engineers want to poke the numbers themselves.

So I deployed NecroSQL live with a real PostgreSQL 16 oracle behind it — zero login, zero OAuth wall:

Here is the 3-minute walkthrough:

Here is the 30-second test drive:

  1. Resurrect a 1998 procedure: Pick any of the 3 built-in 1998 benchmarks (payroll_1998.sql, invoice_tax_1998.sql, or loan_interest_1998.sql) — or paste/upload your own .sql file — and click Resurrect SQL.
  2. Toggle Diff vs Naive: See side-by-side why standard LLM output leaks pennies, and click any Kiro badge directly in the UI to inspect the live .kiro/ spec, steering, and hook files.
  3. Open the Live Proof Lab (⚡ Prove Zero Drift →):
    • Click 1. Expose Bug to watch fast-check catch a real $0.01 drift in naive JS floats against PostgreSQL 16.
    • Click 2. Prove Fix to watch NecroSQL's Decimal.js service clear 1,000/1,000 randomized runs with 100.0000% behavioral equivalence — complete with downloadable .md and .json audit reports.
  4. Play with the Interactive 3-Way Sandbox: Hit ⚡ Load 1-Cent Drift Preset (for example on loan_interest_1998.sql, where Naive JS outputs 1081.56 (-0.01) while Live Postgres 16 and NecroSQL both lock at 1081.57), or type your own ugly decimals.

The Takeaway
Legacy SQL stays buried in production for 25 years because nobody wants to be the person who broke payroll by one cent.

AI makes rewriting legacy code fast. Deterministic guardrails — formal EARS specs, Zero-Float steering, and live differential property-based testing — are what make it safe.

Try NecroSQL live: https://necro.dosan.my.id

Resurrecting 1998 SQL is weirdly fun,

Top comments (2)

Collapse
 
lycore profile image
Lycore Development •

The point that five hand-picked unit tests never land on the rounding boundary is the key one here. Running a fuzzer against live PostgreSQL as the oracle is a strong pattern, since the database's NUMERIC behaviour becomes the spec. We wonder whether you also compared the intermediate values and not just the final rounded result, because that can show where in a multi-step payroll calculation the first divergence appears.

Collapse
 
afiqxeon profile image
Taufiqurrohman • • Edited

Good catch. Two rounding bugs canceling each other out at the final step is a nasty trap.

Right now runPbt.ts checks 3 checkpoints, not just the final number: gross_pay, tax_withheld, and net_pay. I cast all three straight to ::text out of Postgres so the JS driver never sneaks a float in between.

So when fast-check catches a drift, I can see right away if it broke on gross/overtime, tax, or the final net.

I don't trace the internal local variables inside the SQL body yet though. I kept the 1998 procedure untouched on purpose, since nobody wants to edit legacy SQL just to test it.

That said, the AST extractor already maps every ROUND() call. Spinning up a throwaway shadow copy inside Docker to log each step would catch the exact line of first drift without touching the original file.

Adding that to the list for v2. Thanks for poking at that!