feat(schema): add phase 22 calculation engine - #24
Merged
II-ricky-bobby-II merged 3 commits intoMay 15, 2026
Conversation
Comment on lines
+554
to
+558
| net: createDecimalValue( | ||
| income - expense, | ||
| precision.scale, | ||
| rowsResult.rows.length, | ||
| ), |
There was a problem hiding this comment.
net.count reports total source rows, not contributing rows
The API contract for CalculationDecimalValue documents count as the number of contributing rows. income.count = incomeCount and expense.count = expenseCount follow this contract, but net.count is set to rowsResult.rows.length, which includes every row regardless of whether it contributed — rows with missing categories, unknown categories, or invalid/missing amount fields are all counted despite being skipped in the loop. A caller auditing the net total with net.count would see an inflated figure whenever any row is invalid or uncategorized.
Suggested change
| net: createDecimalValue( | |
| income - expense, | |
| precision.scale, | |
| rowsResult.rows.length, | |
| ), | |
| net: createDecimalValue( | |
| income - expense, | |
| precision.scale, | |
| incomeCount + expenseCount, | |
| ), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
@asym/pdf-template-schema.Review fixes included
e/Edecimal notation instead of parsing it throughNumber(...).toFixed(...), preserving the no-floating-point decimal parsing contract.Validation
corepack pnpm --filter @asym/pdf-template-schema test -- calculations.spec.ts- red first for the two review issues, then passed after the fix.corepack pnpm --filter @asym/pdf-template-schema test- passed, 66 tests.corepack pnpm --filter @asym/pdf-template-schema typecheck- passed.corepack pnpm --filter @asym/pdf-template-schema build- passed.corepack pnpm test- passed, 16 Turbo tasks.corepack pnpm lint- passed with one existing warning in untouchedapps/web/src/app/editor/editor-overrides.css.corepack pnpm --filter /pdf-renderer test- passed, 83 tests.corepack pnpm --filter /pdf-renderer typecheck- passed.corepack pnpm --filter /pdf-renderer build- passed.corepack pnpm --filter /pdf-editor test- passed, 38 tests after building legacy workspace package dist entries required by the test harness.corepack pnpm --filter /pdf-editor typecheck- passed.corepack pnpm --filter /pdf-editor build- passed.corepack pnpm --filter -email/editor test- passed, 460 passed and 1 skipped, with existing Vite/React warnings.corepack pnpm asym:editor-export-smoke- passed.corepack pnpm dlx -ai/openspec validate build-pdf-document-builder- passed.corepack pnpm dlx -ai/openspec validate --all- passed.git diff --check- passed.Earlier Phase 22 validation also passed renderer/editor package tests, typechecks, builds,
@react-email/editortests, export smoke, OpenSpec validation, and full repo checks before this clean PR branch was created.Known gaps
Rollback
Revert the commits in this PR to remove the calculation source and tests, restore the schema export/maturity metadata, and undo the Phase 22 documentation/OpenSpec updates. Then rerun schema, renderer, editor, lint, and OpenSpec validation.
OpenSpec
Phase 22: Build Calculation Engine for Totals, Subtotals, and Grouping. Next phase entry point: Phase 23 exposes summary blocks and total rows.
Greptile Summary
This PR adds the Phase 22 deterministic calculation engine to
@asym/pdf-template-schema: six exported helpers (calculateNumericAggregate,calculateTableTotals,calculateGroupedTableTotals,calculateInvoiceTotals,calculateFinancialTotals,calculateTaxDeductibleAmount) built on BigInt minor-unit arithmetic with no new dependencies. Documentation, OpenSpec, and maturity metadata are updated throughout.calculations.ts(~1 119 lines): pure BigInt decimal engine withhalf_away_from_zerorounding, exponential-notation rejection, global source-index preservation in grouped diagnostics, and a grand-total scoped to rows with valid group keys.Confidence Score: 4/5
Safe to merge after fixing the net.count defect in calculateFinancialTotals; all other calculation paths are correct.
In calculateFinancialTotals, net.count is set to rowsResult.rows.length (all source rows) rather than incomeCount + expenseCount (rows that actually contributed). The API contract documents count as the contributing count, and income.count/expense.count both honour it correctly. Any row with a missing category, unknown category, or invalid amount is skipped but still inflates net.count, so callers using that field for auditing or display would see wrong data from the moment the first invalid row appears in production financial data.
packages/pdf-template-schema/src/calculations.ts — specifically the net count argument in calculateFinancialTotals
Important Files Changed
Reviews (2): Last reviewed commit: "fix(schema): reconcile grouped calculati..." | Re-trigger Greptile