Repository navigation
fix(inventory): preserve ledger chain continuity for legacy stock precision - #1248
Conversation
…cision (#1217) Remediate legacy inventory precision defect F6 (#1217) in main/services/inventory.ts so 12-, 15-, and 16-decimal IEEE-754 stock values maintain machine-epsilon ledger chain continuity across stock adjustments and backup/restore round-trips. - Compute stockAfter and effectiveQuantityDelta without premature 14-decimal clipping when legacy precision > 8 decimals, while preserving 8-decimal rounding for normal operations. - Add regression tests in tests/inventory-legacy-precision-backup.test.ts covering 12-, 15-, and 16-decimal stock adjustments across product, variant, and add-on pools, backup/restore round-trips, and populated historical schema v114 restore. - Add regression test in tests/cloud-live-orders-batching.test.ts covering live-order and table chunk boundaries with >400 orders and >400 tables. - Add regression test in tests/database-tools-api.test.ts covering order and customer FTS search index rebuild following partial data import.
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configuration
You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@greptile review |
[Medium impact] The change reaches stock adjustments across the inventory workflow. The runtime change appears sound, but the new comments must meet the repository’s explicit rule before merging. Findings
|
…tions, and package.json - Replace verbose header and inline issue tags in tests/inventory-legacy-precision-backup.test.ts with concise comments per AGENTS.md rules. - Strengthen precision assertions across product, variant, and add-on pools by verifying exact IEEE-754 values on returned results, stock tables, and inventory movements. - Verify restored stock quantities and movements match exact pre-backup values after backup/restore round-trip. - Read expectedSchemaVersion dynamically from getCurrentSchemaVersion() before restore in historical v114 fixture. - Chain test:inventory-legacy-precision-backup onto test:inventory-ledger in package.json and revert line 54 to prevent merge conflicts.
Closes #1217
Problem & Root Cause
In
main/services/inventory.ts,adjustInventoryStockPoolclippedstock_afterto 14 decimal places via:and updated the database using SQLite
ROUND(COALESCE(stock_quantity, 0) + ?, ?), followed by computing:When legacy stock quantities had 15 or 16 decimal places (such as
0.123456789012345), applying an adjustment (e.g.+0.01) roundedstock_afterto0.13345678901235and seteffectiveQuantityDeltato0.01000000000001. This created a floating-point discrepancy of4.996e-15against the true IEEE-754 sum, exceeding the ledger's machine-epsilon tolerance (Math.max(1e-9, Math.max(1, ...) * Number.EPSILON * 16) = 2.220e-15). As a result,validateInventoryLedgerRowsinmain/db.tsfailed with:Inventory movement history contains a broken stock chainSolution & Minimal Fix
ROUND(..., 8)in SQLite and.toFixed(8)in JavaScript so ordinary fractional stock calculations never accumulate floating-point residue.ROUND(), andeffectiveQuantityDeltais computed asstockAfter - stockBefore. This guaranteesstockBefore + effectiveQuantityDelta == stockAfterwithin<= 1 ULP, preserving ledger chain continuity across sequential adjustments and backup/restore cycles.Additional Regression Coverage Added
tests/cloud-live-orders-batching.test.tstesting 450 orders across 450 distinct tables and add-on rows across theCHUNK_SIZE = 400boundary, verifying complete decoration without data loss.tests/database-tools-api.test.tsverifying that partial imports (POST /api/db/importwith orders and customers) synchronizeorders_fts, making newly imported orders immediately searchable by order number, customer name, and customer phone digits viaGET /api/orders?search=....tests/inventory-legacy-precision-backup.test.tscreating a populated schema v114 database (orders, items, bills, payments, inventory movements), taking a backup snapshot, restoring into a clean v115 database, verifying clean migration upgrade, PRAGMA integrity check, and inventory ledger validation.Reproduction & Verification
/Users/gurkiratkhaira/tools/firstmate/data/remediate-f6-inventory-precision/repro-red.txtreproducing failure on unfixedmain(Inventory movement history contains a broken stock chain: expected stock 0.13345678901235 vs calculated 0.13345678901234502 (diff: 4.996003610813204e-15 > tolerance: 2.220446049250313e-15))./Users/gurkiratkhaira/tools/firstmate/data/remediate-f6-inventory-precision/repro-green.txtpassing all scenarios.Verification Commands & Results
npm run test:inventory-legacy-precision-backup- Passed (Scenarios 1-5 green)npm run test:cloud-live-orders-batching- Passed (7/7 tests passed)npm run test:database-tools-api- Passed (150/150 tests passed)npm run test:inventory-ledger- Passed (82/82 + 60/60 tests passed)npm run test:addon-inventory-db- Passednpm run test:addon-inventory-api- Passednpm run test:backup- Passednpm run test:script-coverage- Passed (245 reachable test scripts, 302 test files covered)npx oxfmt --check- Passed (0 formatting errors across all modified/added files)npm run lint&npm run lint:budget- Passed (0 errors, 998/1000 backend warning budget)npm run build- Passed (clean TypeScript compilation todist/)Invariant Checks