Carry the classification providers already send us
Slice 3.1 of #122 (IP3 — classification and allocation). Triage plan: #122 comment. Pairs with #192 (3.2), which fills what no provider can answer.
What this is
All three named providers already fetch the payload carrying sector and industry, and we parse the response and discard those fields:
| provider | already fetched | key |
|---|---|---|
| EODHD | General | Sector, Industry |
| Alpha Vantage | OVERVIEW | Sector, Industry |
| Yahoo | assetProfile | sector, industry |
So this costs no additional provider call. It is plumbing, not fetching.
The skip gate — the part that would otherwise make this a no-op
Security::Provided#import_provider_details reads:
unless name.present? && (logo_url.present? || website_url.present?) && !clear_cache
Any security that already has a name and a logo — which is most of them — never refetches, so classification would have landed only on newly created securities.
Widening it needed more than an extra condition. import_provider_details has three callers:
| caller | cadence |
|---|---|
holdings_controller.rb:117 (#sync_prices) | user-initiated POST, behind a write-permission check |
account/market_data_importer.rb:90 | per account sync |
market_data_importer.rb:33 | scheduled bulk |
Widening unconditionally would make every one of those callers inherit the importers' cost: they walk every security on every sync, so a gate that never closes is one provider request per security per sync, indefinitely.
So the widening is opt-in: include_classification: defaults to false, the two importers pass true, and the view keeps today's gate. Classification backfills at sync cadence, which is the same shape as existing behaviour — a security missing a logo already refetches each sync.
Two deliberate deviations from the plan
1. SecurityInfo gains sector and industry, not asset_class. No provider supplies an asset class; they supply a type, which the Data already carries as kind and which nothing persisted. Adding a field no implementer populates is dead weight across eleven of them, so the asset class is derived centrally from kind instead.
2. That derivation is deliberately narrow. "Common stock" is an equity whatever else is true of it. An ETF is not — a bond ETF and an equity ETF are the same wrapper around different asset classes, and answering "equity" for both would put a figure in the allocation chart that nothing supports. Wrapper types are left for the fund look-through (3.7), which can see what they hold.
Happy to be overruled on either, but I would rather leave an ETF unclassified than classify it wrongly at the scale this runs at.
Blast radius
SecurityInfo has eleven implementers and is a Data.define, so adding fields without defaults breaks every one at construction. Handled the way the file already does it — the sibling Security Data defaults currency: in an initialize override — and the eight unwired providers keep working and return nil. All 772 provider tests pass untouched, which is the practical proof rather than inspection.
Precedence. The provider may fill what nothing has answered and may replace a default, but never a manual classification, and never anything on a locked security. The lock is checked twice on purpose: a locked security missing its name still fetches for metadata reasons, and the response carries classification whether we asked or not. That second check is where the lock would otherwise leak — and a mutation found it had no test until I added one.
Evidence
- 16 tests in
test/models/security/classification_ingestion_test.rb— the gate both ways, the page-view path making no call, precedence, the lock leak, and the original seven-argumentSecurityInfo. - 3 tests in
test/models/provider/security_classification_fields_test.rbasserting each provider's own key names against a payload shaped like its real response. The ingestion tests build aSecurityInfodirectly, so a typo ingeneral.dig("Sector")would sail past them; these catch it. Verified by mutating each key and watching the matching test fail. - Mutation-checked: 10 deliberate breakages, all caught — each provider key, the gate reverted, the gate always-widened, the precedence guard, ETF mapped to equity, the sector blank-guard, the lock guard, and the
SecurityInfodefaults. - Targeted: 910 runs, 2845 assertions, 0 failures across
test/models/security/,test/models/provider/, both market-data importers and the provider interface test. - Rubocop and Brakeman clean.
Full suite: queued, and I will post the result before this leaves draft.
Leaves out
3.3 (manual override UI), 3.4 (allocation), 3.7 (fund look-through), and any backfill job — classification arrives as securities are refreshed through the existing path.