Let a user classify a security the providers will not

Merged
JA
2mo ago
+452-2

Part of #122 (slice 3.3). Triage plan: https://github.com/jaysbeekay/sure/issues/122#issuecomment-5741213585

The problem

3.1 (#193) and 3.2 (#192) shipped a precedence ladder on Security and left one
rung of it unreachable:

  • Security::Provided#classification_attributes_from refuses to overwrite a
    classification whose classification_source is "manual".
  • Security::Provided#import_provider_details stops asking a provider at all
    when classification_locked?.
  • Security#apply_classification_defaults returns early on the same flag.

Nothing in the application could set either value. Checked rather than assumed:
no controller anywhere permits Security params, and every write to a
securities row today is machine-sourced — the provider ingestion path,
Security::Resolver, Security::HealthChecker, and the per-integration
resolvers.

The securities that need this are precisely the ones the two merged slices
deliberately leave alone. An ETF is given no asset class because the wrapper does
not imply one (a bond ETF and an equity ETF are the same wrapper around different
asset classes), and a security whose country config/regions.yml does not name is
given no region. Both are correct decisions, and both strand the holding in 3.4's
"Unclassified" bucket permanently.

What changed

A Classification section in the holding drawer, over two new member actions
behind the existing require_holding_write_permission!:

  • PATCH /holdings/:id/classification — writes asset class, sub-class, sector and
    region, plus classification_source: "manual" and classification_locked: true.
    Setting those two is the point of the slice; the four columns alone would leave
    the precedence rules still unreachable.
  • POST /holdings/:id/reset_classification — clears the classification and the
    lock, so providers answer again on the next sync. It clears the values as
    well as the lock deliberately: the provider path only fills a field it finds
    empty, so lifting the lock alone would leave the user's answer standing forever
    and make the reset a no-op for sector.

Three design calls worth flagging:

  • Sector stays free text. 3.1 stores whatever EODHD (General.Sector), Alpha
    Vantage (OVERVIEW.Sector) and Yahoo (assetProfile.sector) return, and those
    three share no vocabulary — Alpha Vantage even returns TECHNOLOGY where the
    others return Technology. A select would invent a scheme none of them speaks
    and make a provider's own sector unrepresentable in the form that edits it.
  • industry is not editable. 3.1 writes it, but nothing reads it — no chart,
    no grouping, and it is not in 3.4's ALLOCATION_GROUPINGS. It comes back when
    something groups by it.
  • Blank normalises to nil, not "". The skip gate asks sector.blank?,
    which an empty string satisfies, while a WHERE sector IS NULL would not. One
    shape in the column keeps the two agreeing. For region it matters more: the
    column is validated by inclusion, so an unnormalised blank does not merely
    store badly, it refuses a legitimate "clear this field" edit.

The blast radius, stated plainly

securities has no family_id. This is the first user-facing write to a
globally shared table in the application. One family marking a security manual
and locked changes it for every family on the instance holding that security,
and the lock silences the provider and the defaults for all of them.

The epic specifies the slice exactly that way, so this implements that rather than
redesigning it in a PR. But it is worth recording that 3.2 wrote the opposite
reasoning into apply_default_region on main:

Securities are global rather than family-scoped, so deriving a region from it
would publish one family's guess to everyone holding that security.

The distinction I think holds is that 3.2 declined to infer from a field meaning
something else (country_code is the user's own country on the offline path),
whereas this is a user deliberately stating a correction — and "AAPL is equity /
stock / Technology" is true for everyone. It does not hold for a family with an
idiosyncratic scheme, and on a managed instance it is a cross-tenant write.

Consequently: self-hosted is unaffected in practice (one family per instance),
and a family-scoped override table is needed before any managed rollout — an
M, not an S, since 3.4's grouping queries, 3.5's allocation by tag and 3.6's
proposals would all have to join it. Raised as a follow-up on #122 rather than
expanded into this slice.

Evidence

Written before the implementation and watched to fail — all 11 errored on the
missing route first.

Eleven tests in test/controllers/holdings_classification_test.rb. The load-bearing
one is not the write: it is "a manual classification survives a provider that
disagrees"
, which saves through the controller and then runs
import_provider_details(include_classification: true) against a provider stubbed
to return different values. That fails if the action writes the four columns but
not the provenance — the exact shape a careless implementation takes. Its negative,
"resetting the classification lets the provider back in", is what stops a
permanently-stuck lock passing the first one.

Ten mutations, all caught:

MutationCaught by
drop classification_source = "manual"manual-survives-provider (2 failures)
drop classification_locked = truethe write test (2 failures)
reset lifts the lock but keeps the valuesreset-lets-provider-back-in
reset does not clear the lockreset-lets-provider-back-in
blank stored as "" rather than nilboth blank-value tests
save(validate: false) — check constraint firesout-of-vocabulary refusal
drop the write-permission gateread-only-member refusal
remove the drawer formdrawer-offers-the-form
form scope renamed (security[…] → holding[…])the field-name assertions
reset control rendered unconditionallyreset-appears-only-when-locked

One of these found a real defect in my own test. The first version of the
blank-value test submitted a blank region alongside the blank sector; without the
normalisation that fails inclusion and rejects the whole save, leaving sector at
the nil it already held — so the test passed while the thing it tested was gone.
It now asserts the success flash first, so a refused save cannot masquerade as a
normalised one. Both rewritten tests were re-mutated and do fail.

The permission test explicitly shares the account with the guest first, so it
exercises require_holding_write_permission! rather than falling at set_holding
and passing for the wrong reason.

Checks:

  • Full suite: 9946 runs, 41545 assertions, 0 failures, 1 error, 33 skips.
  • The single error is RecurringTransaction::PipelineTest — password authentication failed for user "root", which is #168, fixed by the open #196.
    Confirmed rather than assumed: config/database.yml is untouched by this branch,
    and applying #196's one-line user: → username: change makes that file pass
    4 runs / 0 errors. The file was then restored (git diff empty).
  • bin/rubocop: 2773 files inspected, no offenses.
  • erb_lint on the changed template: no errors.
  • bin/brakeman --no-pager: 0 security warnings, 0 errors.

Note for review

The taxonomy labels land in a new securities.taxonomy.* namespace (en + de).
#194 carries the same vocabulary under portfolios.allocation.*; the wording is
copied verbatim from that branch so the two agree. Once #194 merges, a follow-up
should point the allocation view at the shared namespace and delete its copy —
they are duplicated only because #194 is not merged yet and stacking would cost
the PR its CodeRabbit review.