Add per-person ownership percentage to accounts

HA
13d ago
+1173-63

Add per-person ownership percentage to accounts

Refs: Discussion #1170 (fractional ownership of properties and mortgages)

Problem / Use-case

Net worth counts 100% of every balance a user can see. That is wrong for co-owned assets and liabilities: a jointly held mortgage, an inherited property split three ways, a shared account where each person owns half. Discussion #1170 asked for this on properties and mortgages. This PR implements it for all account types, and for each person separately, so 60/40, 33/33/33, 100/100, or one-sided 50% splits work.

Resulting behavior

  • Every account has an ownership percentage (0-100, default 100) for the owner. Every AccountShare (a co-owner) has its own percentage, default 100.
  • A person's net worth, dashboard totals, net worth chart and account group sparklines count only their share of each account. Existing data is unaffected: everything defaults to 100.
  • Stored balances never change. The account page still shows the full balance and adds a "Your share" line.
  • Sidebar rows for a partly-owned account show a small percentage badge (only below 100%). The row balance stays the full balance.
  • Only the account owner can set percentages (their own and each co-owner's). A co-owner sees their share as read-only text.
  • The API exposes the share as new fields; balance keeps meaning the full account.
  • The assistant's get_balance_sheet history uses the same share as its totals, and the assistant tool descriptions say which figures are whole-account and which are personal.

Demo data used below

Seeded with rake demo_data:default (seed 42). Owner ("Jack") holds the Primary Residence ($350,000) and Home Mortgage ($297,813.13) at 60%, shared with the partner ("Eve") at 40%. Ally Online Checking ($9,910) is split 50/50.

Owner (60%)Co-owner (40%)
Account page (property)owner-2-propertypartner-2-property
Account page (mortgage, a liability)owner-4-mortgagepartner-4-mortgage
Sharing dialogowner-3-sharingpartner-3-sharing
Dashboardowner-1-dashboardpartner-1-dashboard

Numbers check out for the co-owner: $140,000 (40% of the house) + $4,955 (50% of the checking account) - $119,125.25 (40% of the mortgage) = $25,829.75, which is what their Net Worth card shows.

Decisions and why

  1. A share per person, not one number per account. A single column would force everyone who can see an account to use the same percentage, so 60/40 would be impossible.
  2. Applied when reading, never stored. accounts.balance and the balances rows stay at 100%, so syncing, reconciliation and the materializer are untouched and the change is reversible.
  3. All account types. The columns are on the shared accounts table, so no per-type migrations or forms.
  4. Defaults to 100 and shares need not sum to 100. A fully shared account (both people count all of it) stays valid and is what every existing share keeps. No warning or validation on sums in v1.
  5. Only the owner edits percentages. Co-owners see theirs read-only; to keep it simple and centralized.
  6. balance in the API is unchanged. Scaling it would silently break existing API consumers, so new fields carry the share (ownership_percentage, owned_balance, owned_balance_cents).
  7. Scoped to net worth and account value for v1. Transactions, cash flow and spending are not scaled. This could potentially be done in the future.
  8. History uses today's percentage. Every past balance is multiplied by the viewer's current fraction, so changing 50% to 25% restates the whole chart. Dated ownership records would be a larger change and are left as a possible future enhancement.
  9. Removing an owner keeps the new owner's own percentage. The departed owner's percentage is dropped, and an account the new owner had no share in starts at 100.
  10. Sharing starts at 100. Unsharing deletes the share, so resharing starts again at 100.
  11. Family-wide insights stay on whole-account amounts. There is no household total, because shares need not sum to 100.
  12. Export/import carries the owner's percentage only. Co-owner shares are not exported or imported yet.

How it works

  • Migration: ownership_percentage decimal(5,2) default 100 not null on accounts and account_shares, each with a 0..100 check constraint.
  • Account#ownership_percentage_for(user) returns the owner's column, the user's AccountShare value, or 100 for anyone else. Related helpers: ownership_fraction_for, owned_balance_for, owned_balance_money_for.
  • Dashboard: BalanceSheet::AccountTotals#converted_balance_for is the single choke point for group and net worth totals, and applies the viewer's fraction.
  • Chart: Balance::ChartSeriesBuilder takes an optional user: and multiplies every sum by a per-account viewer_fraction computed in SQL. Callers that don't pass a user (for example single-account charts) still count accounts in full. The net worth builders pass the user, so the chart and the dashboard agree.
  • Group sparklines: AccountableSparklinesController passes the viewer. Its cache key digests the viewer's percentages, so a co-owner is never served the owner's series and a percentage edit refreshes it. Per-account sparklines stay whole-account, like the row balance.
  • Sidebar badge: AccountsHelper#ownership_badge_label returns the viewer's percentage (nil at 100) and the row renders a DS::Pill. The sidebar fragment cache version is bumped, so cached sidebars refresh.
  • Cache: existing net worth cache keys already change when an account (accounts.updated_at) or the viewer's share (AccountShare.updated_at) changes, and a test with a real in-memory cache covers editing the percentage. The share version keeps sub-second precision (in the net worth series builders and in BalanceSheet::AccountTotals) so two edits within the same second don't show a stale chart or a missing account.
  • Sharing UI: AccountSharingsController#update accepts the percentages, owner only (the existing owned_by? guard). Out-of-range values are rejected and the user is redirected with the validation message.
  • Removing an owner: User#reassign_owned_accounts! gives the new owner the percentage they held as a co-owner, in one transaction.
  • Export/import: accounts.csv gets an ownership_percentage column; Family::DataImporter reads the owner's percentage (a missing value keeps the default, an invalid one skips the account).
  • Assistant / MCP: get_balance_sheet history passes the viewer; the descriptions of get_balance_sheet, get_accounts and record_valuation state the value basis.
  • Guide: docs/llm-guides/ownership-percentage.md has a "calculation basis" section: every balance-based feature declares whether it is whole-account or personal.

Not included (future work)

These stay on the whole-account basis, and the guide says so.

  • Scaling transactions, cash flow and spending by ownership share.
  • Investment statement, gains and holdings views, goals and insights.
  • Ownership fields on the assistant's get_accounts tool, and any system prompt changes.
  • Per-row balances in the dashboard sidebar still show the full balance, while group totals and net worth are scaled. The percentage badge now shows why.
  • Exporting/importing co-owner account_shares.
  • Translations: new strings exist in en only.
  • A warning when shares add up to more than 100.
  • Dated ownership records, so that history does not restate when a percentage changes.
  • Flutter app: mobile/ reads /api/v1/balance_sheet (now share-weighted) and /api/v1/accounts (balance unchanged, new fields ignored). It shows no share, and I haven't run it. Swift if/when we ship that.
  • AI Assistant prompt updates (MCP/tool calls as well!). The tool descriptions are updated, the prompts are not.

Validation (AI-supported)

  • New and changed tests (132 tests across the files touched by the original change): all pass.
    • Model: bounds validation and per-user resolution (account_test, account_share_test).
    • Balance sheet: owner and co-owner views, an asset and a liability, sorting.
    • Chart builder: series scaled per viewer, nil user and non-owner see the full account.
    • Net worth series equals the balance sheet total; editing the percentage changes the series.
    • Controller: owner can set percentages, out-of-range is rejected, co-owner cannot edit, owner sees inputs, co-owner sees read-only text.
    • API field test, and the account page share line component test.
  • Added with the downstream-impact follow-up (each fails without its change):
    • Assistant history scales an account's contribution to the viewer's share.
    • Removing an owner: the new owner keeps their own percentage, or 100 when they had none.
    • Group sparklines are built for the viewer; the cache is not shared between viewers with different percentages and is refreshed by a co-owner's edit.
    • Sidebar badge: shown at 60%, hidden at 100%, and a co-owner sees their own percentage.
    • Export/import: the CSV column, the round trip through a real export, and an invalid percentage skipping the account.
  • Checked end to end on the demo data (throwaway database, test environment), as the owner and the co-owner, with the screenshots above.
  • bin/rubocop, ERB lint on the touched templates and Brakeman: clean. No JavaScript changed; Biome reports formatting issues in 20 existing JavaScript files, none of which this PR touches.
  • docs/api/openapi.yaml regenerated (rswag specs are documentation only).
  • Full suite (before merging the latest main): 12,478 runs, 98 failures and 6 errors in this environment. I re-ran those 104 tests on the PR head without the follow-up commits and the same 104 fail there too, so none come from these commits. They are environment-related (chat/LLM, subscriptions, market data, hosting settings and similar).
  • After merging the latest main (conflicts in the chart builder, balance sheet sort, sidebar cache version and sparklines): the 922 tests covering the touched areas pass (chart builder, balance sheet, sparklines, helper, dashboard, import/export, assistant tools, accounts and sharing). I have not re-run the whole suite since the merge.
  • Not run: the full set of system tests.

Summary by CodeRabbit

  • New Features
    • Account owners can set ownership percentages for themselves and co-owners. Co-owners can view their share, and account charts show it when it is below 100%.
    • Account balances, charts, and net worth reflect each viewer’s ownership share.
    • Account API responses include the viewer’s ownership percentage and owned balance.
  • Bug Fixes
    • Balance-sheet sorting uses converted balances, and ownership-share changes are reflected more reliably in account totals and charts.
    • Ownership percentages outside the 0–100% range are rejected.