Classify what a provider cannot: cash, crypto and region

Merged
JA
2mo ago
+579-0

Slice 3.2 of #122 (IP3 — classification and allocation). Triage plan: #122 comment.

What this is

The classification columns have existed since #133 and nothing writes them, so every security is unclassified and the allocation work in 3.4 has nothing to group by.

This fills the part no provider can answer. A provider returns nothing useful for a synthetic cash security or a crypto pair, and region has no provider field at all — it is a property of where the instrument is listed, not something EODHD or Yahoo reports. So these are not a stopgap until 3.1 lands; they are the cases 3.1 structurally cannot cover.

caseclassification
Security#cash?liquidity / cash
Security#crypto? (Binance MIC)alternative_investment / cryptocurrency
country_coderegion, via the new config/regions.yml

What it deliberately does not guess

An ordinary listed instrument is left unclassified. "Listed in the US" says nothing about whether it is a stock, an ETF or a bond. Marking it default would mean thousands of securities carrying a source, and would make 3.1's provider answer look like an overwrite rather than the first real classification.

Blast radius — who else hits this line, and what do they pass?

A before_validation callback fires on every Security save: providers creating securities, Security.cash_for, the demo generator, imports, the securities controller. That is deliberate — it is how existing securities pick a classification up as they are next written to, with no backfill job and its own provider-budget argument.

It is only safe because of the guards, which are the real content of this change. It is the weakest writer in the precedence order (default → provider → ai → manual):

  • fills a field only when that field is empty
  • writes nothing at all when classification_locked
  • claims classification_source only when it actually set the asset class — filling a region does not make the classification ours, so a provider-classified security missing a region gets one without being relabelled

The callback is declared after :upcase_symbols because crypto? compares against a canonical MIC and that callback is what canonicalises it.

config/regions.yml

60 countries → region plus developed/emerging, with the reasoning recorded in the file rather than in code: the five-region split, and the developed/emerging calls the index families disagree on (South Korea, Poland, Greece). Changing a call is a data edit, not a deploy.

Two decisions worth flagging:

  • Snake_case keys, not display text. I first wrote "North America" and corrected it: ASSET_CLASSES and ASSET_SUB_CLASSES are snake_case keys the UI translates, and storing English would leave 3.4 unable to localise without a reverse map. The locale labels belong with the view that renders them, in 3.4.
  • A country the file does not name leaves region nil. An unclassified holding is honest; 3.4 gives it an explicit "Unclassified" bucket. Guessing would put a holding in a slice no one can justify.

region has no check constraint in the database (unlike asset class and sub-class), so REGION_KEYS plus a test over the config is what keeps it a vocabulary at all — a typo in a new country's entry fails rather than quietly creating a sixth region an allocation chart would draw as its own slice.

development_status is derived from the same config rather than stored: there is no column for it and so nothing to keep in sync.

Evidence

16 tests in test/models/security/classification_defaults_test.rb, written before the implementation and watched fail. The ones that matter are the writes that must not happen: a manual classification is not overwritten; a locked security is untouched even when it has nothing to protect yet; a region already set survives the country lookup; an unknown country code leaves region nil.

Mutation-checked — 8 breakages, all caught. Two survived the first pass and were genuine gaps: return if region.present? and the classification_source ||= claim had no coverage (the "without claiming it" test returned early and never reached that line). Two tests were added and all eight are now caught.

  • Targeted: 190 runs across security/trade/holding, 772 runs across test/models/provider/ — 0 failures.
  • Rubocop and Brakeman clean.

Full suite: a run came back 9907 runs / 1 failure / 1 error, but that run was invalid — two full suites were competing for the same parallel test databases, and the captured error was PG::ConnectionBad: password authentication failed for user "root" in RecurringTransaction::PipelineTest, which is environmental and pre-existing on main. A clean single run is queued and I will post the result before this leaves draft.

Leaves out

3.1 (provider ingestion — separate PR, next), 3.3 (manual override UI), 3.4 (allocation), and any backfill job.