The personal finance app for everyone (by everyone) https://sure.am
  • Ruby 81.9%
  • HTML 9.6%
  • Dart 4.8%
  • JavaScript 2.9%
  • Rust 0.3%
  • Other 0.3%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
jaysbeekay dfe6e87f18
Store holding lots and re-key them on remap and reset (#4899)
Replaces #4496, closed so the stack can be a native GitHub stack (heads
in this repo). Its review history, including jjmata's threads and their
answers, stays there.

Part of #4449.

**Slice 2 (U2)** of the per-lot cost basis series that #4449 proposes.
Slice 1 (#4495) is merged. **This is the bottom of a GitHub stack** of
the remaining slices: each one targets the branch below it, and the
stack map in the merge box shows the order. Merge bottom-up.

**In short.** Lots now persist: a `holding_lots` table holds each
account's lots per security, written by the materializer from the
calculators' lot books, and remap/reset re-key them. **Look first:**
`Holding::Materializer`, `HoldingLot.store!`,
`Holding::ReverseCalculator#open_lots`. **Check:** `bin/rails test
test/models/holding_lot_test.rb test/models/holding/lot_book_test.rb`.

**Fork pre-flight:** jaysbeekay/sure#600
(https://github.com/jaysbeekay/sure/pull/600), which holds this slice's
CI and review history. Same commits: the pre-flight runs on the fork's
copy of this branch. It ports the merged fork PR jaysbeekay/sure#579,
whose review history is there too.

## Problem

Slice 1 keeps lots in memory during a sync and throws them away. Every
later step in #4449 (FIFO disposals, a user's cost on one lot, remap
moving lots) needs lots that persist between syncs and follow their
trades.

## What this changes

A new `holding_lots` table holds each account's lots per security. The
holdings materializer writes them from the calculators' lot books on
every sync.

- **Derived lots.** One per acquiring trade (a buy or a transfer-in),
keyed by its source entry. A closed lot stays at qty 0.
- **Opening lots.** One per position, for units no imported trade
accounts for. On the reverse path this is the baseline the walk already
computes: the units held before the first trade, or the whole provider
quantity when there are no trades. It is seeded as an unknown-cost lot,
so sales relieve it pro rata like any other, and it stays out of the
average.
- **Clamp.** Where the provider reports fewer units than the trades add
up to, the opening lot is stored at qty 0 with `clamped: true`. Nothing
else changes.
- **Forward keeps opening lots.** Only the reverse walk produces opening
lots, so a forward materialization (for example, the first sync after a
provider is unlinked) leaves them in place and keeps only derived lots
in step.
- **Remap and reset.** `remap_security!` and
`reset_security_to_provider!` move lots with their trades in the same
transaction. An opening lot keeps `origin_security_id`, so a reset
returns it.

Two book rules land with storage:
- **Shortfall.** A sale beyond the open lots becomes a shortfall. The
next acquisitions fill it before they open a lot, so open lots equal the
position. A split restates the shortfall.
- **Rounding.** Pro-rata relief rounds each lot to the stored scale
(18), and the residual goes to the last open lot.

**Every stored figure is unchanged.** Nothing reads lots yet, and the
pool the figure comes from is untouched. Slice 1's characterisation pins
and byte-identity dumps pass unedited. No UI, no params, `average` only.

## Blast radius

**Who else hits these lines, and what do they pass?** Only
`Balance::Materializer` calls
`Holding::Materializer#materialize_holdings`, from account sync,
remap/reset and trade edits. It passes `account`, `strategy` and an
optional `security_ids`. The same account and security scope now also
writes `holding_lots`, and lots for out-of-scope securities are left
alone.

**Siblings:**

| Code | Status |
|---|---|
| Both calculators | Changed: they expose `lots_by_security`; reverse
seeds opening lots and records clamps |
| `CostBasisTracker < LotBook` | Unchanged interface; its pool and
`average_cost` are line-for-line unchanged |
| Transfer gates, `Holding#calculate_avg_cost`, `FlowClassifier` |
Unchanged: they read trades, not lots |
| `Trade#basis_holding` | Unchanged: it reads holdings rows, which do
not change |
| `Holding#remap_security!`, `#reset_security_to_provider!` | Changed:
lots move with the trades |
| Snaptrade split repair, Trade Republic rematch, `securities.rake`,
`data_migration.rake` | Unchanged. They move trades without lots, and
the next materialization's upsert re-keys each derived lot to its
trade's security. The `security_id` FK cascades, so destroying a
duplicate security is not blocked by its lots |
| `import_holding` and the provider processors | Unchanged: they write
provider holdings rows, from which the opening lot is derived |

## Migration notes

`db/migrate/20261023090000_create_holding_lots.rb`: schema only,
reversible (`change`), **no data backfill**. Lots appear at each
account's next materialization.
- **Columns:** `account_id`, `security_id` (FKs, cascade);
`source_entry_id` (FK to `entries`, cascade, so deleting a trade deletes
its lot; nil marks the opening lot); `origin_security_id` (FK, nullify);
`acquired_on`; `qty` decimal(34,18) as `holdings.qty`; `unit_cost`
decimal(25,10), the same 15 integer digits as `holdings.cost_basis`;
`currency`; `clamped`; timestamps.
- **Indexes and constraints:** unique `(account_id, source_entry_id)`;
partial unique `(account_id, security_id) WHERE source_entry_id IS
NULL`, one opening lot per position; `source_entry_id`, `security_id`,
`origin_security_id`; check `qty >= 0`.
- **Verified:** migrate, rollback and migrate again in a throwaway test
container. `db/schema.rb` takes only this table's hunks.

## Tests

`test/models/holding_lot_test.rb`, 21 tests.
- **Book rules, failing before:** the shortfall after a sale with
nothing held, then a transfer-in; a split restating the shortfall; the
rounding residual on the last open lot.
- **Storage:** a buy adds one lot and leaves earlier ones untouched
(ids, `created_at`); open lots equal the snapshot quantity on every
forward date across a buy, sale, 3:1 split and transfer-in, and equal
the provider snapshot on reverse, opening lot included; a provider-only
position keeps its provider basis; the opening lot does not move the
figure; a clamp is stored and recorded, and flips both ways on
re-materialization; an opening lot goes once the trades explain the
position; a forward sync after unlinking keeps opening lots.
- **Lifecycle:** deleting a trade deletes its lot; a buy edited into a
sale loses its lot; a trade moved to another security without its lot
takes the lot along; remap and reset move lots, keeping ids; remap onto
a position with its own opening lot leaves one there.
- **Mutations (22, all caught):** each book rule, each storage rule, the
remap and reset moves, the upsert keys, and the forward/reverse
opening-lot handling. Two survived the first round, and the tests that
catch them were added.

## Validation

- **Full suite** at `d9290e2ec`: `13477 runs, 62070 assertions, 0
failures, 0 errors, 36 skips`.
- `bin/rubocop`: no offenses. `bin/brakeman`: 0 warnings. No ERB.

## Known limit

**Opening-lot collision on remap.** With one opening lot per position,
an opening lot cannot move onto a security that already has one. This
slice drops the moved opening lot there, and the re-materialization the
controller runs straight after a remap restates the target's opening lot
over the merged provider quantity. Derived lots always move intact, and
opening lots carry nothing user-set yet. Slice 4 (U4) merges them
instead.

## AGENTS.md

- **Code placement:** model logic, a current-version migration with
notes above, and Minitest with no new fixtures.
- **Comments:** they describe current behaviour.
- **Commits:** subjects are ≤72 characters, with no fork references.
- **UI, API, providers:** none.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **New Features**
* Holdings now retain acquisition-level lot details, including
quantities, costs, and dates, alongside position records.
* Opening positions can include provider-reported cost basis, and lot
details remain aligned when securities are remapped or reset.
* Lot details update alongside calculated holdings and reflect trades,
transfers, and security splits.
* **Bug Fixes**
* Improved lot and position reconciliation for shortfalls, splits,
rounding, and provider-reported quantities and costs. Acquisitions cover
outstanding shortfalls before contributing to open lots, and positions
that close to zero can reopen with known average cost.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Jays Beekay <beekay@users.noreply.github.com>
2026-11-12 13:44:07 -08:00
.cursor/rules Consolidate repository instructions after auditing their history (#3409) 2026-09-06 07:14:43 +02:00
.devcontainer fix(devcontainer): use the pgvector Postgres image so document import works (#4263) 2026-10-16 20:59:48 +02:00
.gemini Disable Gemini Code Assist for now (#115) 2025-08-15 06:38:35 +02:00
.github Start Pipelock's MCP listener with Pipelock 3.2 and later (#4947) 2026-11-12 13:40:00 -08:00
.junie Consolidate repository instructions after auditing their history (#3409) 2026-09-06 07:14:43 +02:00
app Store holding lots and re-key them on remap and reset (#4899) 2026-11-12 13:44:07 -08:00
bin Move LLM prices to a config file refreshed on release (#4666) 2026-11-11 23:14:00 -08:00
charts/sure Start Pipelock's MCP listener with Pipelock 3.2 and later (#4947) 2026-11-12 13:40:00 -08:00
config fix(mcp): answer GET and DELETE /mcp with 405 Method Not Allowed (#4920) 2026-11-12 12:49:18 -08:00
db Store holding lots and re-key them on remap and reset (#4899) 2026-11-12 13:44:07 -08:00
design/tokens Let themed utilities reach pseudo-elements in dark mode (#3817) 2026-09-29 01:36:41 +02:00
desktop Let the macOS desktop app reach a server on the local network (#4843) 2026-11-10 10:42:45 -08:00
docs Start Pipelock's MCP listener with Pipelock 3.2 and later (#4947) 2026-11-12 13:40:00 -08:00
lib security: close remaining plaintext-storage gaps found while auditing #3142 (#3190) 2026-11-12 12:33:21 -08:00
log Initial commit 2024-02-02 09:05:04 -06:00
mobile fix(mobile): load every page of the chat list, not just the first 20 (#4741) 2026-11-06 18:09:39 +01:00
public fix(pwa): give the installed app icon a safe area (#2966) 2026-08-11 02:58:42 +02:00
spec Stop accepting category_id on the trades API (#4734) 2026-11-11 21:02:24 -08:00
storage Initial commit 2024-02-02 09:05:04 -06:00
test Store holding lots and re-key them on remap and reset (#4899) 2026-11-12 13:44:07 -08:00
tmp Initial commit 2024-02-02 09:05:04 -06:00
vendor Anchor menus inside size containers to their button (#4370) 2026-10-22 21:22:15 -07:00
.coderabbit.yaml Add pre-merge checks configuration for docstrings 2026-11-07 19:06:09 +01:00
.dockerignore Initial commit 2024-02-02 09:05:04 -06:00
.editorconfig Rubocop updates (#1118) 2024-08-23 10:06:24 -04:00
.env.example Encrypt every table in the backfill and document adding keys to an instance with data (#4936) 2026-11-12 07:31:03 -08:00
.env.local.example Add authenticated version API with explicit public opt-in (#4691) 2026-11-06 23:41:22 +01:00
.env.test.example Catch up .env files changes 2026-09-16 06:31:30 +00:00
.erb_lint.yml refactor(design-system): migrate single-color tokens to @theme + lint @utility /N footgun (#1849) 2026-05-20 18:20:38 +02:00
.gitattributes Enforce LF line endings for .devcontainer/.bashrc (#2480) 2026-06-24 08:42:32 +02:00
.gitignore Adopt CalVer release tagging with SemVer compatibility alias (#4539) 2026-11-02 06:58:25 +01:00
.rspec Add RSwag coverage for /chat and /transactions API endpoints (#210) 2025-12-17 14:14:17 +01:00
.rubocop.yml Keep the provider lists sorted with a custom cop (#4459) 2026-10-30 23:42:18 -07:00
.ruby-version Update Ruby to 3.4.9 (#2360) 2026-06-15 23:29:36 +02:00
AGENTS.md Update AGENTS.md with comment and coding style guidelines 2026-10-22 19:23:07 -07:00
biome.json Add BiomeJS for Linting and Formatting JavaScript relates to #1295 (#1299) 2024-10-14 17:09:27 -04:00
CLAUDE.md Consolidate repository instructions after auditing their history (#3409) 2026-09-06 07:14:43 +02:00
compose.example.ai.yml Start Pipelock's MCP listener with Pipelock 3.2 and later (#4947) 2026-11-12 13:40:00 -08:00
compose.example.yml security: close remaining plaintext-storage gaps found while auditing #3142 (#3190) 2026-11-12 12:33:21 -08:00
config.ru Initial commit 2024-02-02 09:05:04 -06:00
CONTRIBUTING.md Consolidate repository instructions after auditing their history (#3409) 2026-09-06 07:14:43 +02:00
Dockerfile fix(pdf-vision): render PDFs with poppler-utils + surface a concrete admin failure reason (#3275) 2026-08-31 23:27:04 +02:00
Gemfile chore: update to posthog-rails 3.26.0 (#4882) 2026-11-10 22:12:48 -08:00
Gemfile.lock chore: update to posthog-rails 3.26.0 (#4882) 2026-11-10 22:12:48 -08:00
LICENSE Initial commit 2024-02-02 09:05:04 -06:00
package-lock.json Rename package from 'maybe' to 'sure' 2025-09-16 13:54:33 +02:00
package.json refactor(design-system): single-source design tokens via DTCG JSON (#1604) 2026-05-01 14:46:33 +02:00
perf.rake More rebranding changes (#159) 2025-09-24 00:19:51 +02:00
pipelock.example.yaml chore(security): update Pipelock integration to 3.4.0 (#3122) 2026-08-22 05:41:00 +02:00
Procfile.dev Ipv6 support (#1437) 2026-04-13 13:44:37 +02:00
Rakefile Initial commit 2024-02-02 09:05:04 -06:00
README.md Fix the README's broken badge and refresh its hero screenshot (#4100) 2026-10-09 17:43:11 +02:00
SECURITY.md Minimalistic SECURITY.md 2026-06-05 15:16:41 +02:00

Ask DeepWiki View performance data on Skylight Dosu Pipelock Security Scan

The Sure dashboard in two columns (cashflow, net worth, spending and outflows) beside the AI assistant answering a question about savings

Deutsch | Español | Français | 日本語 | 한국어 | Português | Русский | 中文

Sure: The personal finance app for everyone

Get involved: Discord • Website • Issues

Important

This repository is a community fork of the now-abandoned Maybe Finance project.
Learn more in their final release doc.

Backstory

The Maybe Finance (archived/abandoned repo) team spent most of 2021–2022 building a full-featured personal finance and wealth management app. It even included an “Ask an Advisor” feature that connected users with a real CFP/CFA — all included with your subscription.

The business end of things didn't work out, and so they stopped developing the app in mid-2023.

After spending nearly $1 million on development (employees, contractors, data providers, infra, etc.), the team open-sourced the app. Their goal was to let users self-host it for free — and eventually launch a hosted version for a small fee.

They actually did launch that hosted version … briefly.

That also didn’t work out — at least not as a sustainable B2C business — so now here we are: hosting a community-maintained fork to keep the codebase alive and see where this can go next.

Join us!

Hosting Sure

Sure is a fully working personal finance app that can be self hosted with Docker. Sure can be accessed from a browser, the macOS desktop app, the mobile app, API clients, and LLM agents. See Sure Clients for an overview.

Forking and Attribution

This repo is a community fork of the archived Maybe Finance repo. You’re free to fork it under the AGPLv3 license — but we’d love it if you stuck around and contributed here instead.

To stay compliant and avoid trademark issues:

  • Be sure to include the original AGPLv3 license and clearly state in your README that your fork is based on Maybe Finance but is not affiliated with or endorsed by Maybe Finance Inc.
  • "Maybe" is a trademark of Maybe Finance Inc. and therefore, use of it is NOT allowed in forked repositories (or the logo)

Performance Issues

With data-heavy apps, inevitably, there are performance issues. We've set up a public dashboard showing the problematic requests seen on the demo site, along with the stacktraces to help debug them.

https://www.skylight.io/app/applications/s6PEZSKwcklL/recent/6h/endpoints

Any contributions that help improve performance are very much welcome.

Local Development Setup

If you are trying to self-host the app, read this guide to get started.

The instructions below are for developers to get started with contributing to the app.

Requirements

  • See .ruby-version file for required Ruby version
  • PostgreSQL >9.3 (latest stable version recommended)
  • Redis > 5.4 (latest stable version recommended)

Getting Started

cd sure
cp .env.local.example .env.local
bin/setup
bin/dev

# Optionally, load demo data
rake demo_data:default

Visit http://localhost:3000 to view the app.

If you loaded the optional demo data, log in with these credentials:

  • Email: user@example.com
  • Password: Password1!

For further instructions, see guides below.

Setup Guides

One-click Install

Render expects each button target branch to have a render.yaml at the repository root. The branch-ready Blueprint files live in branches/<branch-name>/render.yaml in a separate repo and can be copied to each corresponding branch root.

Option What it deploys latest stable
Sure - No AI Sure web, Sidekiq worker, Render Postgres, and Render Key Value. AI tokens are intentionally blank. Deploy to Render Deploy to Render
Sure - Simple AI Sure with OpenAI-backed AI settings, pgvector-ready Postgres, Sidekiq, and Render Key Value. Render prompts for the OpenAI token. Deploy to Render Deploy to Render
Sure - External AI Sure with external assistant settings plus AlphaClaw, which manages the OpenClaw gateway on Render. Deploy to Render Deploy to Render

Run on PikaPods

Deploy on Hostim

License and Trademarks

Maybe and Sure are both distributed under an AGPLv3 license.

  • "Maybe" is a trademark of Maybe Finance, Inc.
  • "Sure" is not, and refers to this community fork.