Treat a pending transfer counterpart as absent

JA
7d ago
+49-11

Part of #4078 (part 3, returns engine)

Part of #3494

What: a posted transfer leg whose counterpart is still pending classifies by its direction, as an external flow, until the counterpart posts.
Why: it read as internal, though the pending side has moved no balance yet, so the posted leg's move went to market as a gain or loss.
Start reading: sql_joins, transfer_resolution_sql and transfer_counterpart in app/models/portfolio/flow_classifier.rb.
Key test (test/models/portfolio/flow_classifier_test.rb): "a linked transfer whose counterpart is still pending is a flow by direction in both forms".

Behaviour change: none for users. In the model, a posted leg against a pending counterpart becomes an external flow, and the performance cache version moves to v13 so returns cached with the old resolution are not served after deploy.

Split from #5285 on 2026-11-20 (same commits, unchanged; the cache bump is its own commit here).
Stack: 13 of 13, on #5395. Merge order: … → #5285 → #5296 → #5297 → #5395 → this PR.
Fork pre-flight: jaysbeekay/sure#876. Same branch, same commits: up/portfolio-pending-counterpart-v3 is at the same head in both repositories.

Evidence
  • Fail-first. The pending test read Expected: :external_outflow, Actual: :internal before the change.
  • Mutation. The SQL and the Ruby pending checks, each removed on its own, are caught; so is the cache version.
  • Tests. Full suite on the stack top: 0 failures; the 8 errors are ReleaseNotesWorkflowTest, no node in the test image.
  • Blast radius. FlowClassifier readers: DailyReturns, plus Drivers, Performance and income attribution through it. Income attribution keys on 'income' only. ReturnScope builds single-account classifiers whose counterparts are never in scope, so it is unaffected.

Summary by CodeRabbit

  • Bug Fixes
    • Transfers with a pending counterpart are classified as external flows until both legs are posted. Once both legs are posted, the transfer is classified as internal.
    • Portfolio performance cache entries now reflect the corrected transfer classification, so cached results align with updated flow reporting and transaction status.