Separate collaboration from personal finance#
A Shared Space is a collaborative expense ledger. Members share expenses, splits, reporting periods and balance results. Their personal budgets and unrelated transactions remain separate.
An expense answers who paid? and who owes which share? A personal transaction answers how much did this person consume? Those are not interchangeable questions.
Record relationships#
shared_space
├── members (may link to Auth users)
└── periods
├── expenses (payer, total, date, description)
│ └── splits (member, amount)
└── closed report (members, expenses, balances, settlements)
authenticated member's split
└── derived personal transaction
origin_type = shared
origin_id = source expense UUID
sharedSpaces.ts, sharedPeriods.ts and sharedExpenses.ts perform local persistence. shared-expense-service.ts owns calculations and validation.
Resolving a split#
resolveSplit first requires a positive integer total. It dispatches to equal, exact or percentage resolution.
| Method | Input | Invariant |
|---|---|---|
| Equal | Selected member IDs | Every cent is distributed |
| Exact | Per-member integer cents | Sum equals the expense total |
| Percentage | Per-member basis points | Sum is exactly 10000, representing 100% |
parsePercentToBasisPoints accepts nonnegative decimal percentages with at most two fractional digits. Percentage strings are converted to integer weights before allocation.
Equal and percentage splits use largest-remainder allocation: floor each exact share, then distribute remaining cents by descending fractional remainder, with original input order breaking ties. Splitting 1000 cents equally among three members yields 334, 333, 333 in that order—not an unaccounted fractional cent.
canFillRemaining / fillRemainingSplit support the form's “rest” operation only when exactly one selected member has a zero/empty value and a positive remainder exists. Equal splitting does not need this operation.
Validation is separate from splitting#
validateExpense checks description, positive integer total, a selected known payer, known split members, nonnegative integer shares, an exact total, a date no earlier than the period start and no later than today.
This separation is intentional: obtaining numbers from a split method does not by itself establish that the entire expense is valid. Callers must enforce both the allocation invariant and the surrounding domain constraints.
Balances and suggested settlements#
computeBalances accumulates each member's paid totals and owed shares:
net cents = paid cents - owed cents
positive net: member should receive money
negative net: member should pay money
If Alice pays 6000 cents for an equal two-person expense, each member owes 3000. Alice's net is +3000; Bob's is -3000. Alice's personal consumption is still 3000, not 6000.
settleBalances sorts creditors and debtors by remaining magnitude with stable member-ID tie-breaking. It matches them and emits transfers until balances are settled. These are deterministic settlement suggestions; the function does not execute a bank transfer or prove payment happened.
buildPeriodReport snapshots member information, expenses, paid totals, balances and suggested settlements. A closed report preserves the meaning of that reporting period independently from later UI calculations.
Derived personal transactions#
deriveLinkedTransaction constructs a personal transaction from one member's split: date/description from the source expense, amount from the member's obligation, and provenance pointing to the shared expense UUID.
sharedLinking.ts reconciles those mirrors with local transactions. shared-mirror-service.ts owns resolveSharedMirrorPlacement so a mirrored transaction follows the app's month-placement rules.
This explains two common surprises:
- A payer's personal spending reflects their share, not necessarily the full sum they initially advanced.
- Editing a shared expense changes its derived personal reflection. Editing a second independent “copy” would create divergent financial truth.
The cloud mirror-identity migration deduplicates older copies and prevents a second personal transaction for the same shared source. Inspect 20261112090000_shared_transaction_mirror_identity.sql when investigating duplicate history.
Synchronization and access#
Shared push/pull is implemented in sync/shared.ts, with a browser variant. It has its own tombstones, FK mappings and incremental watermark. The engine reconciles personal mirrors after shared pulls, and again after a full personal pull because personal sync can otherwise reintroduce stale placement.
Remote access is based on membership in the corresponding Shared Space, not simply on being signed into SeedWorks. The backend adds membership hardening in 20261112120000_coconut_shared_membership_hardening.sql.
On account deletion, the backend handles shared history differently from private rows: an owned space can transfer to the oldest remaining active member; it is removed when no other active member remains. A departing non-owner is detached rather than causing all shared history to disappear.
Where to debug#
| Symptom | Follow this path |
|---|---|
| Split sum off by a cent | resolveSplit → allocation helper → split fixtures |
| Wrong person owes money | Expense payer and stored splits → computeBalances |
| Duplicate personal spending | Provenance UUID → sharedLinking.ts → mirror-identity migration |
| Expense appears in wrong month | resolveSharedMirrorPlacement → reconciliation after pull |
| Non-member can access a space | Server membership policies/RPCs; UI hiding is not authorization |
| Former member's cache still shows data offline | Local cache semantics, not immediate remote erasure |