Port #3999's SnapTrade provider filter and fallback logging
Summary
Closes jaysbeekay/sure#425
Ports two upstream commits from we-promise/sure#3999 onto fork main, unchanged, with git cherry-pick -x. The two commits:
| Fork commit | Upstream commit | Subject |
|---|---|---|
3e6022d4c | 553c850d1 | fix(snaptrade): follow only SnapTrade's own or trade-derived holdings |
d8da25515 | dda1f44b3 | fix(snaptrade): log ambiguous fallback picks between duplicate rows |
Both are on the head branch of jaysbeekay/sure#335 (claude/project-thread-5d47hn, head c74d5555a), which is the fork pre-flight of we-promise/sure#3999. They applied to origin/main at 3b1e600e2 with no conflicts. The touched files are byte-identical to dda1f44b3 (git diff dda1f44b3 HEAD on those paths is empty). Neither commit carries an AI trailer. Each message gains only the (cherry picked from commit …) line.
Problem. Fork main's SnaptradeAccount::DataHelpers#held_security_for follows the account's latest holding of a ticker from any provider. SnapTrade sends no external_id, so Account::ProviderImportAdapter#import_holding matches on security, date and currency alone. On an account where another provider (CoinStats, say) also holds the ticker, a same-day SnapTrade sync therefore overwrites that provider's quantity and moves the holding to SnapTrade's account provider. Separately, the last-resort pick between duplicate Security rows happens with no record.
Behaviour after.
held_security_forreads only holdings with SnapTrade's own account provider, or with none (those materialised from the account's trades, which is also where a user's remap lives). Both processors pass@snaptrade_account.account_provider&.id.- When the fallback tier picks between more than one candidate, it writes one
DebugLogEntry:provider_sync,warn,provider_key: "snaptrade", against the account and its SnapTrade account provider, with each candidate's id, exchange and pricing fields. A single candidate, or a pick decided by the held-position or reported-exchange tier, logs nothing.
Why port now (sync risk). Fork main's data_helpers.rb is identical to #3999's first commit, 1b1bd7ecc, which reached the fork through jaysbeekay/sure#334. When we-promise/sure#3999 merges, the weekly upstream sync will touch these same lines. With the two follow-up commits already on main, as the same patches, that sync applies cleanly instead of conflicting.
Scope and dependencies
- Issue: jaysbeekay/sure#425
- Source: we-promise/sure#3999, via its fork pre-flight jaysbeekay/sure#335. No upstream PR is opened from this branch, because the change is already upstream in #3999.
- Parent PR/branch, if stacked: none (base
main) - Related: jaysbeekay/sure#423, being implemented in parallel, reuses
held_security_forin its repair task. Whichever lands second must keep the three-argument signatureheld_security_for(account, ticker, account_provider_id). If #423 lands after this port, its repair inherits the provider filter. - Intentionally out of scope:
- the split-data repair (jaysbeekay/sure#423);
- the other providers' ticker-only lookups listed in jaysbeekay/sure#423;
- #335's third commit,
c74d5555a(a comment pointing at the upstream issue), which the plan does not include; - anything beyond what #3999 contains.
Blast radius: who else hits this line, and what do they pass? On fork main at 3b1e600e2, SnapTrade's resolve_security is called only by:
SnaptradeAccount::HoldingsProcessor#process_holding(holdings_processor.rb:100);SnaptradeAccount::ActivitiesProcessor#process_trade(activities_processor.rb:209).
Both now pass account_provider_id:. The new keyword defaults to nil, so a caller that omits it does not raise, but it is narrowed rather than unchanged: held_security_for then filters on [ nil ] and follows only holdings with no provider (trade remaps), never another provider's holding. No such caller exists today; these two are the only callers of resolve_security in SnaptradeAccount::DataHelpers.
SnaptradeAccount::DataHelpers is also included by:
SnaptradeAccount;SnaptradeAccount::Processor;SnaptradeItem::Importer;SnaptradeActivitiesFetchJob.
None of these calls resolve_security or held_security_for. The test stub ActivitiesProcessor.any_instance.stubs(:resolve_security) (activities_processor_test.rb:986) ignores arguments. Every other resolve_security in app/ and lib/ is a separate private method of another provider: Binance, Kraken, SimpleFIN, CoinStats, Questrade, IBKR, Indexa, Trade Republic, Lunchflow, Onchain, Coinbase, and the generator template. None of them is affected. held_security_for has no caller outside existing_security_for. The SnapTrade files have not changed on fork main since 3727ee4e0, the commit the issue was written against.
What else reads what this changes?
- The holdings
held_security_forreads also feedAccount::ProviderImportAdapter#import_holding's matching, which is unchanged;test/models/account/provider_import_adapter_test.rbandtest/models/holding_test.rbare green. - The new
DebugLogEntryrows are read only by/settings/debug.
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 (below).
- Any gate test has an observed failing mutation transcript attached.
- Fixtures used as evidence assert internal consistency. Not applicable; the tests build their securities and holdings inline.
- Financial calculations use
BigDecimaland documented rounding points. Not applicable; no calculation changes. - De-identification uses token-shape allowlists; no personal data is included.
- Files changed, decisions, tests/runtime, risks, and next unblocked issue are documented below.
Files changed (all from the two upstream commits):
app/models/snaptrade_account/data_helpers.rbapp/models/snaptrade_account/holdings_processor.rbapp/models/snaptrade_account/activities_processor.rbtest/models/snaptrade_data_helpers_test.rbtest/models/snaptrade_account/holdings_processor_test.rb
Validation
Fail-first
origin/main code (3b1e600e2) with the test files from dda1f44b3 applied:
93 runs, 276 assertions, 2 failures, 20 errors, 0 skips
SnaptradeAccount::HoldingsProcessorTest#test_a_sync_leaves_another_provider's_holding_of_the_same_ticker_alone [holdings_processor_test.rb:64]:
--- expected
+++ actual
-[3, "92e49f70-…"]
+[0.1e2, "2babf97e-…"]
SnaptradeAccount::HoldingsProcessorTest#test_an_ambiguous_fallback_pick_is_logged_against_this_connection [holdings_processor_test.rb:72]:
`DebugLogEntry.count` didn't change by 1, but by 0.
These two failures are the behaviour.
- The first shows the other provider's holding taken over: qty 3 becomes 10, and its
account_provider_idbecomes SnapTrade's. - The second shows that nothing is logged.
The 20 errors are all ArgumentError: unknown keyword: :account_provider_id. They come from the test helper's wrapper, test_resolve_security(symbol, symbol_data, account:, account_provider_id:) at snaptrade_data_helpers_test.rb:18. The upstream test file changed it to pass the new keyword, so every SnaptradeDataHelpersTest test that calls resolve_security raises before reaching any assertion. That is an API difference, not behaviour, so those 20 are not counted as fail-first evidence. They include the upstream negative tests: still follows a holding with no provider, keeps a holding the user remapped…, and the three does not log when… tests. The negatives are therefore shown green only after the port. The behavioural fail-first rests on the two processor tests above, and on the mutations below.
After the picks
bin/rails test test/models/snaptrade_data_helpers_test.rb test/models/snaptrade_account/
93 runs, 325 assertions, 0 failures, 0 errors, 0 skips
bin/rails test test/models/snaptrade_data_helpers_test.rb test/models/snaptrade_account/ \
test/models/holding_test.rb test/models/account/provider_import_adapter_test.rb
203 runs, 710 assertions, 0 failures, 0 errors, 0 skips
These are green after the port:
- Positive:
a sync leaves another provider's holding of the same ticker alone,an ambiguous fallback pick is logged against this connection,resolve_security does not follow another provider's holding,…logs a fallback pick when the reported exchange matches no row, and without an account, and…picks the older of two priced rows differing only by exchange, and logs it. - Negative:
resolve_security still follows a holding with no provider,keeps a holding the user remapped to another row of the same ticker,does not log when the ticker has a single row,does not log when the held position decides, anddoes not log when the reported exchange decides.
Mutations
Each mutation was made on the committed fix and reverted afterwards; git diff HEAD is empty after each.
| Mutation | Result (93 runs) | Failing tests |
|---|---|---|
Drop .where(account_provider_id: [ account_provider_id, nil ].uniq) in held_security_for | 2 failures | test_a_sync_leaves_another_provider's_holding_of_the_same_ticker_alone (holdings_processor_test.rb:64); test_resolve_security_does_not_follow_another_provider's_holding (snaptrade_data_helpers_test.rb:249) |
Replace the capture_ambiguous_security(...) call in preferred_security_for with nil | 3 failures | test_an_ambiguous_fallback_pick_is_logged_against_this_connection (holdings_processor_test.rb:72); test_resolve_security_picks_the_older_of_two_priced_rows_differing_only_by_exchange,_and_logs_it (:276); test_resolve_security_logs_a_fallback_pick_when_the_reported_exchange_matches_no_row,_and_without_an_account (:294) |
Full suite, lint, security
-
Full
bin/rails testatd8da25515, private Postgres and Redis:12392 runs, 53469 assertions, 0 failures, 0 errors, 35 skips. No environmental failures this run. -
bin/rubocop -f githubon the five changed files: no offences. -
bin/brakeman --no-pager: 0 security warnings (9 ignored, unchanged). -
No ERB, JS, migration or API changes.
-
Checked against upstream
AGENTS.md:- commit subjects are 72 characters or fewer;
- diagnostics go through
DebugLogEntry.capturewith account and account-provider context; - tests are Minitest.
Risks, blockers, and follow-up
- Low merge risk: the patches are byte-identical to upstream's, so the later upstream sync sees the same content.
- The new fallback log can be noisy on families with many duplicate rows. That is the intended signal for support, and it matches upstream.
- Next unblocked: jaysbeekay/sure#423's repair, which calls
held_security_forand will inherit the provider filter.