Port the flow classifier and stale-pending follow-ups from the pre-flight to main
Summary
This closes the drop 0 single-copy gap recorded on #118 (duty 3): "porting 4c0f3a404 + 8d2492901 to fork main is an outstanding follow-up". Both commits now live on #258's branch (up/pending-sql-parity, the pre-flight for we-promise/sure#3557), but main never received them. Checking the gap file by file turned up a third missing fix, so this ports three commits:
| Pre-flight commit | Here | What it does on main |
|---|---|---|
6533077e4 (was 4c0f3a404) | 64fa66d99 | States the classifier's caller contract: signed sums and family scoping. Documentation plus tests; applied cleanly. |
03fc2b21f (was 8d2492901) | 7c1a41740 | Stale pending is counted for review before the exclusion hides it, through review_and_exclude_stale_pending. |
e01ed2f87 | 6403d2ee0 | A Contribution/Withdrawal label no longer outranks a linked transfer in scope. main did not have this. A transfer between two in-scope accounts with those labels was read as money leaving and arriving from outside, once on each leg. Applied cleanly. |
e899942c0 merges main after #259, the month-end loan calendar fix, so CI runs on the current base. It brings in no change of this PR's own.
Deliberately not ported: da6ebef3b ("Drop the contract-gate claim this branch cannot back"). It removes the docs/portfolio/methodology.md and portfolio:verify_contract_coverage references from the classifier's comments. That is right for the upstream copy, which has no gate, and wrong for main, where the gate runs.
Scope and dependencies
- Issue: #118, drop 0, duty 3 (single-copy rule). There's no separate issue, because the epic already tracks this follow-up.
- Parent PR/branch, if stacked: none; this is based on
main. The source is #258 (merged intomirror/upstream-mainon 2026-09-29) and upstream we-promise/sure#3557. - Intentionally out of scope:
- The pending-SQL half of #258 (
transaction.rb,entry_search.rb, the provider processors).main's copies of those files differ from the pre-flight partly becausemainis behind upstream. They'll be reconciled after the weekly sync, not by porting onto a stale base.
- The pending-SQL half of #258 (
Evidence checklist
- Each issue in this PR has its own acceptance criteria and no unrelated issue work is included.
- Contract rows touched by this change name existing tests.
portfolio:verify_contract_coveragepasses: 41 methodology and 18 returns rows. - Any gate test has an observed failing mutation transcript attached (see Validation).
- Fixtures used as evidence assert internal consistency. (Not applicable: the tests build their own transfers and stale entries.)
- Financial calculations use
BigDecimaland documented rounding points. (Not applicable: this is classification only.) - De-identification uses token-shape allowlists; no personal data is included.
- Files changed, decisions, tests/runtime, risks, and next unblocked issue are documented below.
How the 8d2492901 conflict was resolved. main already had its own fix for the same bug, run_pending_reconciliation, which does the same three steps in the same order. It conflicted with the pre-flight's review_and_exclude_stale_pending. Two implementations of one fix is exactly what the single-copy rule forbids. The epic had already set the direction (port the pre-flight to main), so importer.rb and importer_test.rb are now byte-identical to the pre-flight.
Declared divergence (single-copy rule). After this PR, main differs from the pre-flight in these files only:
flow_classifier.rb: 1 comment line, thedocs/portfolio/methodology.mdreference that the pre-flight dropped inda6ebef3b.flow_classifier_test.rb: the 5-line header comment describing the contract gate.importer_stale_unmatched_pending_test.rb: +13 lines, one test.mainhad a negative case the pre-flight lacks: an entry a previous sync already excluded must be neither re-counted nor re-excluded. It's kept, rewritten to callreview_and_exclude_stale_pending.main's other order test was dropped, because the pre-flight'sImporterTestalready pins the order.
Validation
- CI at
e899942c0: every check passes, includingtest_unitandtest_system(run36667520205). - Local runs used PostgreSQL 16 and Ruby 3.3.6 with the pin overridden locally, because 3.4.9 couldn't be installed in this environment. CI is authoritative.
test/models/portfolioandtest/models/simplefin_item: 251 runs, 915 assertions, 0 failures.- Mutations, each caught by a named test:
- Put back
when :external then transaction_direction(entry). It failsa Contribution or Withdrawal label does not outrank a linked transfer in scope. - Swap the two calls in
review_and_exclude_stale_pending. It failsstale pending review counts every provider's flag before the exclusion hides it. - Make
track_stale_unmatched_pendingignoreexcluded: false. It failsthe review pass does not re-count an entry a previous sync excluded.
- Put back
- After merging
main(e899942c0): the loan tests,accounts_controller_test,test/models/simplefin_itemandtest/models/portfoliogave 452 runs, 2,301 assertions, 0 failures. That includes the four month-end loan tests that were red at6403d2ee0. bin/rubocopon the 5 changed files: no offenses.
Risks, blockers, and follow-up
- Behaviour change on
main: a linked transfer between two in-scope investment accounts whose legs are labelled Contribution/Withdrawal now reads as internal instead of an external inflow plus an outflow. Portfolio flows and returns for such families change to the correct figure. - The SimpleFIN pass is unchanged in behaviour. Both implementations did reconcile, then review, then exclude; this only swaps
main's implementation for the pre-flight's. - Next: the weekly upstream sync, then reconcile the pending-SQL files with the pre-flight on the synced base.
Summary by CodeRabbit
- Bug Fixes
- Contribution and withdrawal transfers are now classified as internal when both accounts are in scope; otherwise, classification reflects the transaction’s direction.
- Interest and fee classifications now follow their labels rather than the transaction amount’s sign.
- Pending transactions are reconciled and reviewed before stale entries are excluded, improving stale-pending counts and exclusion behavior.