Classify what a provider cannot: cash, crypto and region
Moved 2026-11-18. we-promise/sure#3755 is closed and replaced by we-promise/sure#5286, opened from a we-promise/sure branch at the bottom of the security classification stack. This pre-flight was merged into the mirror earlier. The fork's up/security-classification-defaults is now at 9de0e72b4, the same commits as we-promise/sure#5286: this PR's commits replayed onto current upstream main with no conflicts. The re-cut keeps every change. Commits authored as Claude are re-authored to jaysbeekay, and fork references are dropped from commit messages, because upstream commits carry none. Full unit suite on the stack top (7215c0a99) on beastpc: 17,359 runs, 0 failures. The 8 errors are ReleaseNotesWorkflowTest, which needs node, missing in the test image. Rubocop is clean on the changed files.
Upstream: https://github.com/we-promise/sure/pull/3755, part of we-promise/sure#3494. This is its fork pre-flight: same commits (4b8afbf). It is based on mirror/upstream-main, so CI here exercises exactly what we-promise/sure receives.
Summary
This is U1 of the drop 3 upstream plan (#122, comment of 2026-09-26): drop 3.2 of #122, and the first model slice on top of the classification schema that landed upstream as we-promise/sure#3608.
It is a port of fork #192 (merged 2026-09-19), including #218's rename of one test.
Providers can say what a listed instrument is. They cannot classify a cash balance or a crypto asset, and none of them supplies a region. This PR fills those three on save, acting as the weakest writer:
| Security | Gets | classification_source |
|---|---|---|
cash (kind: "cash") | liquidity / cash | default |
crypto (crypto?, Binance MIC) | alternative_investment / cryptocurrency | default |
online, country in config/regions.yml | region (one of five); development_status derived, not stored | unchanged |
| anything else | nothing: an ordinary listing is left for a provider to answer | unchanged |
- A value that is already present, or a classification the user has locked, is never overwritten.
- An offline security's
country_codeis a search hint about the person, not the instrument's listing country. Securities are global, so it is not turned into a region.
Scope and dependencies
- Part of #122 (slice 3.2); upstream coordination issue we-promise/sure#3494
- Upstream PR: https://github.com/we-promise/sure/pull/3755
- Parent PR/branch, if stacked: none; base is
mirror/upstream-mainat7cc6aa0 - Intentionally out of scope:
- provider ingestion (3.1, U2)
- tags, constituents, proposals (U3–U8)
- all UI, held for Phase C per #118
Blast radius
before_validation :apply_classification_defaults runs on every Security save. That covers Security::Resolver (online and offline), Security.cash_for, every provider importer, HoldingsController#sync_prices, and fixture-built securities.
The full suite was run for that reason (results below). No existing test changed behaviour.
Single-copy rule (#118, duty 3)
app/models/security.rb here follows the mirror's layout, with the we-promise/sure#3608 taxonomy block after brandfetch_crypto_url. Fork main places it next to KINDS, so the file is not byte-identical to fork main. That divergence is declared here.
- The added constants, callback, validation and methods are identical to fork
main. - Two mirror comments that this change makes false are reworded: "nothing writes these columns yet", and "
regionis unconstrained today only because nothing does". config/regions.ymlandtest/models/security/classification_defaults_test.rbare byte-identical to forkmain.
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. Not applicable: the fork contract gate is not sent upstream.
- Any gate test has an observed failing mutation transcript attached: the fail-first run below.
- Fixtures used as evidence assert internal consistency. The tests build their own securities with explicit attributes rather than relying on fixtures.
- Financial calculations use
BigDecimaland documented rounding points. Not applicable. - De-identification uses token-shape allowlists; no personal data is included.
- Files changed, decisions, tests/runtime, risks, and next unblocked issue are documented below.
Validation
| Check | Result |
|---|---|
bin/rails test test/models/security/classification_defaults_test.rb test/models/security_test.rb | 54 runs, 387 assertions, 0 failures |
Fail-first: the same test file against the mirror's security.rb | 23 runs, 10 failures, 6 errors |
bin/rails test (full suite) | 9716 runs, 4 failures, 3 errors, 33 skips. All 7 are environmental; see below |
bin/rubocop (changed files) | no offences |
config/regions.yml | loads with YAML.safe_load_file |
CI on 9f596cb (Ruby 3.4.9) | all green: test_unit, test_system, lint, lint_js, scan_ruby, scan_js, Codacy |
Current upstream main (1052443) | the branch merges cleanly (git merge-tree) |
The 7 local full-suite failures are the same tests that fail on unmerged fork main in the same environment, and none touches securities:
Provider::RegistryTest(Anthropic env)Provider::OpenaiTest(Ruby-style hash inspect)Settings::HostingsControllerTest×3 (Anthropic base URL)ActiveStorageAuthorizationTestandAdmin::UsersControllerTest(image variants)
The local run used Ruby 3.3.6 because the environment could not download 3.4.9. CI on 3.4.9 is green.
Checked against upstream AGENTS.md:
- logic lives in the model
- Minitest, no new dependency
- no migration
- the commit subject is imperative and ≤72 characters
Risks, blockers, and follow-up
- Behaviour change for existing securities. They pick up defaults as they are next saved; there is no backfill job, which is deliberate. So on an existing install, allocation by class or region fills in gradually rather than all at once.
config/regions.ymlis 228 lines of data. That takes the PR past the ~300-line soft ceiling in raw count, but the code itself is about 95 lines. The data file carries its own reasoning for the five-region split and for the developed/emerging calls where index families disagree.- A family's country hint can become a region once an offline security goes online. This is known and pinned (owner decision of 2026-10-06, option A, recorded in 6016519154).
Security::Resolver#offline_securitystores the family's country on a security that no provider matched, and theoffline?guard keeps it from becoming a region while the row stays offline. Two routes flip such a row online through callbacks. On that save,regionis derived from the hint anddevelopment_statusreads the same country. The routes are the daily health check (HealthChecker#handle_success, whenproviders.firstcan price the ticker and MIC) andHoldingsController#remap_security(finding 4). A transition-only guard cannot stop it, becauserun_checksaves the row again in itsensureblock (health_checker.rb:62). The testa hint-country offline security takes a region from the hint once the health check brings it online(classification_defaults_test.rb:171, commitff3cbd6fb) pins this as a delta, nil before andeurope/developedafter the wholerun_check. If a provider match later resolves the row,Resolver#find_or_create_provider_match!writes the listing country (resolver.rb:137), and the correction branch atsecurity.rb:387moves the derived region. The same decision covers the LOW finding: a row that the health check takes offline (convert_to_offline_security!,health_checker.rb:120-127) keeps its stored region whiledevelopment_statusreads nil. The fix is a follow-up after U1, beside U2 (option B, tracked in #336): record wherecountry_codecame from, in acountry_sourcecolumn, and deriveregionanddevelopment_statusonly from a non-hint country. That needs a migration, which U1 excludes. - Next unblocked: U2, provider classification ingestion (3.1).
Review follow-up
Covers the Production Readiness Review of 2026-09-28 at 9f596cb. Gatekeeper (APPROVE), Codacy (up to standards, 0 issues) and cubic (neutral, no flags) raised nothing. CodeRabbit has not reviewed because the PR is a draft. There are no inline threads.
The fixes are in c9f72c0, on top of 9f596cb, pushed on 2026-09-29 with the owner's go-ahead. This branch is the head of we-promise/sure#3755, so the upstream PR carries every commit here; the current shared head is the one named in the first line. Single-copy note: security.rb and classification_defaults_test.rb now differ from fork main until the same change lands there (a follow-up for the owner).
| Finding (source) | Status |
|---|---|
1. "Crypto" means only Binance's BNCX MIC (PRR, MEDIUM) | Owner decision, 2026-10-13: document only. Crypto defaults stay Binance-only; widening crypto? is its own change. Kraken (XKRA), CoinSpot (XCSO), Binance-account (XBNC) and Coinbase (XCBS) securities get no default classification. crypto? also drives the logo paths (display_logo_url, generate_logo_url_from_brandfetch, import_brand_logo, crypto_base_asset), so widening it would change more than classification. Scope note below. |
| 2. Existing cash securities never pick up the default (PRR, MEDIUM) | Owner decision, 2026-10-13: document only, no backfill. Confirmed from the code: cash_for only reads an existing row (find_or_create_by!), the health check covers only Security.standard, and the price importer writes with update_column. Release note below. |
| 3. The "weakest writer" order is described in a comment but not enforced (PRR, SHOULD) | Fixed in c9f72c0. The comment now says the order is a contract for later writers: nothing at this base reads classification_source to replace a default, so a stored default stays until provider ingestion (U2) lands. |
| 4. Remapping can turn a user's country hint into a listing region (PRR, LOW/SHOULD) | Deferred, with evidence. The suggested fix is unsafe. Clearing country_code in remap_security would stop Resolver#exact_match_from_db from matching the row when the next import passes the same country (security.rb:241 filters on it). With no provider hit, the resolver then falls through to offline_security, which writes the user's country back and sets offline: true, undoing the remap. Skipping region derivation for the remap save alone doesn't help either, because the next routine save derives it anyway. A proper fix needs a record of where the country came from, which belongs with U2. Only a remap of a row created by the offline fallback is affected. |
| 5. A cash or crypto default could complete a contradictory pair (PRR, LOW) | Fixed in c9f72c0. The missing half is filled only when the half already present agrees with the default. Otherwise the row is left as it is, and default is not recorded as the source. |
| Test adequacy: no test that a re-save leaves a classified row unchanged (PRR) | Fixed in c9f72c0: validating an already-classified security leaves it unchanged, which covers cash, listed and provider-classified rows. |
Test adequacy: no test for crypto MICs other than BNCX (PRR) | Finding 1 is decided (Binance-only), so such a test would now pin the decided scope. It is not added in this PR. |
| Rebase onto the current mirror (PRR, SHOULD) | Not done here. Rebasing rewrites the upstream PR's head. The branch merges cleanly (GitHub mergeable_state: clean), and the only schema difference is the version stamp. Bringing it up to date is for the owner to decide when the upstream PR is refreshed. |
Evidence for c9f72c0:
- Fail-first. The new pair test failed at
9f596cbwith "the default invented an equity/cash pair". - Mutations. Removing either new guard fails that test (lines 264 and 269). Restating
defaulton every save fails the new no-churn test. - Neighbouring tests.
test/models/security,security_test,holding_test,holdings_controller_test,trade_testandprovider_import_adapter_test: 265 runs, 0 failures. - Lint and security checks. rubocop and brakeman are clean.
Scope note (finding 1, documentation only, as decided on 2026-10-13). "Crypto defaults apply to securities on Binance's public MIC (BNCX) only. Crypto held through Kraken, CoinSpot, the Binance account integration or Coinbase is left unclassified until Security#crypto? is widened in its own change, because that predicate also drives the crypto logo paths."
Release note (finding 2, documentation only, as decided on 2026-10-13). "Existing securities pick up default classifications as they are next saved. Synthetic cash securities that already exist are not re-saved by any routine job. To classify them now, run Security.where(kind: "cash").find_each(&:save) in a Rails console."