From 2a4f53b74096589e11febbdc81552aec95ca27fd Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 2 Nov 2026 14:29:50 +0000 Subject: [PATCH] Fix a file's rows showing under two accounts after changing its "Read as" (v1.16.17) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changing a file's Read as in Settings -> Import, then Reload CSVs, left the file's rows under both the old account and the new one: 268 transactions instead of 257 on the samples (savings read as checking), in memory, IndexedDB and finlens-state.json, until Rebuild from your folder. The account id is part of every transaction id, so the re-read rows had new ids, and since v1.16.3 an import keeps every loaded transaction a file doesn't replace. master has it in IndexedDB: its import replaces the loaded transactions, but the store keeps both copies and a boot from IndexedDB shows them, with the old account left empty. - earlierCopies() finds each file's earlier rows before the import writes anything: only in accounts the file went into before (the account's new `files` list; for older accounts its sourceFile or the import log), and each row by its exact old id, worked out from the new one with IdMigration._rekey(). - moveEarlierCopies() removes them from memory and IndexedDB, moves their edits and plan links to the new ids, and removes an account left empty (its name, balances, card details and order stay, by id). The autosave writes the state file. Last import says how many rows moved, and from where. - A kind that can't read the file (a card export read as checking) moves nothing. - Accounts record every CSV imported into them (`files`, accountFiles()). Settings -> Import lists every file, so a moved file can be moved back, and Rebuild's check counts them all as imported before. - db.js: DB.removeTransactions(), DB.patchAccount(). - The Read as message says the rows move with their edits on the next reload, and stay if the file can't be read that way. - Version bumped to v1.16.17 (CHANGELOG, footer, README, Settings, CLAUDE.md); CLAUDE.md §4.3, §4.5, §8, §10, §19, §22 updated. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013Z8iBXzUjiQbYziDZ8CZft --- CHANGELOG.md | 22 ++++++++ CLAUDE.md | 28 ++++++++-- README.md | 4 +- index.html | 2 +- js/core.js | 125 ++++++++++++++++++++++++++++++++++++++++++- js/db.js | 49 +++++++++++++++++ js/views-cleanup.js | 4 +- js/views-data.js | 20 ++++--- js/views-settings.js | 2 +- 9 files changed, 239 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8fa11fd..40dd30f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,28 @@ Format follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). --- +## [1.16.17] — 2026-11-02 + +### Fixed +- **Changing a file's "Read as", then reloading, showed its transactions under both accounts.** + - **When:** Settings → Import → Files in the folder: change a file's **Read as** (savings read as checking, say), then **Reload CSVs**. Also Choose CSV files with a different Read as for a file imported before. The rows stayed doubled, in memory, IndexedDB and `finlens-state.json`, until **Rebuild from your folder**. On the samples: 268 transactions instead of 257 for savings read as checking, 338 for checking read as savings. + - **Cause:** the account id is part of every transaction id (`mkTx()`, CLAUDE.md §10), so the file's rows came back with new ids. Since v1.16.3 an import keeps every loaded transaction a file doesn't replace (`mergeImported()`), so the old copies stayed in the old account. `master` has it in IndexedDB: its import replaces the loaded transactions, so memory and the state file are right, but the store keeps both copies, and a boot from IndexedDB (no folder, or access lapsed) shows them. It also keeps the old account, empty. + - **Fix:** before importing, `earlierCopies()` finds each file's earlier rows under another account. It looks only in accounts the file went into before, and finds each row by its exact id there, worked out from its new id (`IdMigration._rekey()`, the same step the v1.12.0 migration uses). `moveEarlierCopies()` then removes them from memory and IndexedDB; the autosave writes the state file. + - **Edits and plan links go with the row:** they move to its new id, since it's the same bank transaction in another account. The id keeps the bank's time (or the card's post date), so two rows with the same date, description and amount keep their own edits, which a match on those three couldn't promise. + - **An account left empty is removed** (memory and IndexedDB), unless a file in the same import went into it. Its name, balances, card details and place in the order are kept by id, so they come back if the file goes back. + - **A kind that can't read the file** (a card export read as checking) moves nothing: the file isn't imported, Last import says why, and its rows stay where they are. + - **Duplicates left by earlier versions** go at the next reload too. Accounts saved before v1.16.17 record only their last file, so the import log also counts as "the file went into this account". +- **Settings → Import listed one file per account.** A second file read into an account (with Read as, or two exports of one account) wasn't listed, so after moving a file it could disappear from the list, with no way to change it back. Each account now records every CSV imported into it (`files`), and the list shows them all; an account's rows and dates span its files' rows. Rebuild from your folder's check uses the same list (`_rebuildBlockers()`), so a file that wasn't an account's last one still counts as imported before, and Rebuild won't silently drop it if FinLens can no longer read it. + +### Changed +- **The "Read as" message** says what the next reload does: the file's transactions move to that account with their edits, and stay where they are if FinLens can't read the file that way. It no longer sends you to Rebuild. +- **Last import** says how many rows moved, and from which account ("11 moved from Bank Savings"). + +### Docs +- **CLAUDE.md:** §1 version; §4.3 the account's `files`; §4.5 an import moves a file's rows when its account changes; §8 `accountFiles()`, `earlierCopies()`, `moveEarlierCopies()`; §10 Read as changes the ids; §19 `DB.removeTransactions()`, `DB.patchAccount()`; §22 `_rekey()` is also used by imports. + +--- + ## [1.16.16] — 2026-11-02 ### Fixed diff --git a/CLAUDE.md b/CLAUDE.md index 01dc108..9a39450 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -59,7 +59,7 @@ Until then, this section is the rule. FinLens is a **single-origin, zero-build, vanilla JavaScript personal finance dashboard** that runs entirely in the browser. There is no server, no npm, no bundler, no framework, and no TypeScript. The entry point is `index.html`; everything else is a plain `.js` file in the `js/` folder. -**Current version:** v1.16.16 (updated in `index.html` footer, `README.md`, and `CHANGELOG.md` on every release). +**Current version:** v1.16.17 (updated in `index.html` footer, `README.md`, and `CHANGELOG.md` on every release). **What it does:** Imports CSV exports from a bank (checking + savings) and credit cards, parses and normalizes them into a unified transaction model, and renders a multi-tab financial dashboard with budgeting, debt tracking, savings forecasting, investment planning, and Claude-powered AI analysis. @@ -284,10 +284,15 @@ Transactions are created by `mkTx()` in `core.js` and never mutated directly — kind: 'bank_checking' | 'bank_savings' | 'credit_card' | 'unknown', balance: null, // Not used directly — use currentBalances() or periodEndBalance() limit: null, // Not used directly — use State.manualBalances[id+'_limit'] - sourceFile: string, + sourceFile: string, // the last CSV imported into it + files?: string[], // every CSV imported into it (v1.16.17). Read it with accountFiles(a): an account + // saved earlier has no list, and its sourceFile stands in. A file whose rows move + // to another account (Read as, §10) leaves the list } ``` +The account record lives in `State.accounts`, the IndexedDB `accounts` store and `finlens-state.json`, so `files` needs no storage of its own. Settings → Import lists every file of every account (`importedFiles()`). + ### 4.4 View Registration Pattern Every view is registered in one of the `views-*.js` files. The pattern is strict: @@ -351,7 +356,8 @@ loadStateFromFile() → found? - Don't call `saveStateToFile()` directly in hot paths — it's meant to be debounced via `_saveStateDebounced()` in `lsSet()`. - **Never use transactions straight from JSON.** `JSON.parse` turns `date` into a string and freezes the `category` getter. `loadStateFromFile()` rebuilds them with `reconstituteTx()`, as `DB.loadAll()` does; any new code that reads transactions from JSON must do the same. - **IndexedDB may not hold the loaded transactions.** After a boot or restore from `finlens-state.json` (a new browser, cleared site data, `_restoreStateIfBrowserEmpty()`), the `transactions` store can be empty or hold only some of them. Code that changes the loaded transactions works from `State.transactions`, never by replacing them with the store's rows: a store row may only fill in a value a transaction lacks. Up to v1.16.0, Clear transaction edits replaced them and wiped every transaction (§20). -- **An import never drops loaded transactions** (v1.16.3). `loadFiles()` adds the files' transactions to the loaded ones (`mergeImported()`): the same id is replaced by the file's, every other loaded transaction stays, as `DB.importTransactions()` keeps every row (§10). Up to v1.16.2 it replaced all of them with the files' ones, so importing only the card file left only the card's in memory, and the autosave wrote that to the file that boot loads first. Removing rows is Rebuild from your folder's job (§20). +- **An import never drops loaded transactions** (v1.16.3). `loadFiles()` adds the files' transactions to the loaded ones (`mergeImported()`): the same id is replaced by the file's, every other loaded transaction stays, as `DB.importTransactions()` keeps every row (§10). Up to v1.16.2 it replaced all of them with the files' ones, so importing only the card file left only the card's in memory, and the autosave wrote that to the file that boot loads first. Removing rows is Rebuild from your folder's job (§20), with one exception, next. +- **A file read into another account takes its rows with it** (v1.16.17). Changing a file's **Read as** changes the account, so its rows come back with new ids (§10). `loadFiles()` then removes the file's earlier rows from the old account: memory, IndexedDB and, through the autosave, the state file. Their edits and plan links move to the new ids. Up to v1.16.16 both copies stayed, until Rebuild. Rules in §10. - **Secrets never reach the file.** `saveStateToFile()` deliberately omits `apiKey`, and `loadStateFromFile()` strips it from files written by ≤v1.7.0 (moving it to browser storage if needed, then scheduling a re-save). Keep both behaviors when changing the serialized field list. --- @@ -611,6 +617,9 @@ importProblemText(res) // why a loadFiles() result's files gave no transacti reloadFromDirHandle(handle, opts?) // (views-data.js) the folder's CSVs → loadFiles(); opts as loadFiles()'s; returns its result mergeImported(loaded, fresh) // loaded + the files' transactions by id, leftover copies out, kept transfers undetected (v1.16.3) undoTransferDetection(t, row?) // a detected transfer back to its CSV type and category, with its edit, before detectTransfers() +accountFiles(a) // the CSV names an account came from: a.files, else [a.sourceFile] (saved before v1.16.17) +earlierCopies(read) // (v1.16.17) loaded rows a re-read file gave under another account → Map(old id → { id, from, file }); reads only +moveEarlierCopies(moves, read, fresh) // removes them (memory, IndexedDB), moves their edits to the new ids, drops accounts left empty ``` --- @@ -762,6 +771,14 @@ The account id comes last on purpose: `migrate.js` relies on it to rename accoun **Memory follows the same rules** (v1.16.3). `loadFiles()` puts the files' transactions into `State.transactions` the way `DB.importTransactions()` puts them into the store (`mergeImported()`): an id it already has is replaced by the file's, a new one is added, a leftover copy as above is removed, and every other loaded transaction stays. So importing one file, or a newer export that starts later, keeps the other accounts and the older rows, and `finlens-state.json` gets them all. +**"Read as" changes the ids** (v1.16.17). A file read as another kind than before (Settings → Import → Files in the folder, or the Choose CSV files card) goes into another account, so every row gets a new id. Before importing, `earlierCopies()` finds the file's earlier rows; after, `moveEarlierCopies()` takes them out. +- **Where it looks:** only accounts the file went into before: `accountFiles(a)` lists it, or, for an account saved before v1.16.17 (no `files`), its `sourceFile` is the file or the import log says the file went into it. Without this, re-importing one file would take another account's row that happens to match exactly (checking and savings can both post the same fee at the same second). +- **What it finds:** for each of the file's rows, the id it had in that account, from its new id (`IdMigration._rekey()`, §22). That keeps the time or post date, so it's exact; a match on date, description and amount would mix up two same-day rows of the same amount. A loaded row with that id goes, unless a file in this import gives that id too. +- **Edits and links** move to the new id. If the new id has an edit already (only a copy left by v1.16.16 or earlier can), its fields win and the old one's fill the rest. +- **The old account** loses the file from `files`. If no transaction is left in it and no file of this import went into it, it's removed from `State.accounts` and the `accounts` store. Its labels, balances, card details and `accountOrder` place stay, by id. +- **Nothing moves** when the new kind can't read the file (a card export read as checking): it gives no transactions, so it isn't imported (`problem: 'no-rows'`), and its rows stay. +- **Limits:** rows the old account holds from an earlier export of the file, which the file no longer has, can't be traced to it and stay there. A row `_rekey()` can't trace stays too: a bank time not written `HH:MM:SS` or `H:MM:SS`, or a card post date more than 10 days before or 90 days after the transaction date. + **A stored row holds the CSV's values, never an edit.** `DB.importTransactions()` stores the transaction's `_originalDesc`, `_originalCategories` and `_originalType` as the row's `description`, `categories` and `type`, and the flags from the rules (`txRuleFlag()`), not what `mkTx()` shows. An import can run while edits exist (Rebuild from your folder, importing again after Clear transactions). Up to v1.16.0 the row took the edited category, type and flags, so they came back after Clear transaction edits. Rows stored that way by older versions keep them until Rebuild from your folder rewrites them. --- @@ -1052,7 +1069,7 @@ Multiple credit card files can be loaded simultaneously. Each gets a unique `acc --- -*Last updated: v1.16.16 — 2026-11-02 (the transaction and transfer drawers store only the fields you change: §4.1 TxOverride, §11 transfer keywords, §12 state mutations; v1.16.15: §1 desktop only; §3 `views-database.js` removed, the Changelog view reads CHANGELOG.md; §6, §18, §19 point to the current screens; v1.16.14: §9 the drawer is a modal dialog: inert page, Tab wraps, first field focused, focus returns after a re-render; v1.16.13: account order: §4.1 `accountOrder` keeps ids not loaded; §8 `orderedAccounts()`; §18 display order; v1.16.12: account names: §4.1 `accountLabels`; §4.3 an account's `name` is its default; §8 `defaultAccountName()`; §9 `acctName()`; §13 the summary lists accounts by `acctName()`; §18 names you type, and clearing them; v1.16.11: AI summary balances at the end of the selected period: §13 `summaryBalanceEnd()` and the date notes; §21 the parser reads the balance lines by how they start; v1.16.10: starting balance counted in every range: §8 `startingBalance()`, `currentBalances()`; §13 summary balances; §18 one balance rule; v1.16.9: the transaction drawer stores only the flags you change: §4.2 flags, §12 state mutations; v1.16.8: flags from transaction rules are worked out on load, never read from storage: §4.2 flags, §8 `txRuleFlag()` / `txFlags()` / `applyTxRulesToAll()`, §19 flags in the transactions store; v1.16.7: transaction links to budgets, buckets and debts by id: §1 version; §3 migrate.js role; §4.1 the `id` fields, TxOverride and its `link` shape; §8 `PLAN_LINK_TYPES`, `newPlanId()`; §9 `planItem()`, `linkedTransactions()`, `unlinkPlanItems()`, `ensurePlanIds()`; §20 clearing budgets, buckets or debts removes their links; §22 the plan-link migration; v1.16.6: JSON export carries account names and order, Read as, flag rules, transfer keywords and theme: §9 `JSON_EXPORT_KEYS`; §12 adding a persisted field; §20 what the export holds; v1.16.5: a new database copies every saved setting: §4.5 the keys that go through `storageKey()`; §12 adding a persisted field; §19 `DB.APP_STATE_KEYS`; v1.16.4: JSON import saves under the storage keys: §4.5 `STORAGE_KEYS` / `storageKey()`; §8 `storageKey()`; §12 adding a persisted field uses the State name; v1.16.3: An import keeps the loaded transactions it doesn't replace: §4.5 an import never drops loaded transactions; §8 `mergeImported()`, `undoTransferDetection()`; §10 memory follows the same re-upload rules; §11 kept transfers are detected again; v1.16.2: Imports read every file before changing anything: §8 `loadFiles()` options and result, `parseCsvFile()` reads only, `importParsedCsv()`, `importProblemText()`, `reloadFromDirHandle()`; §20 Rebuild from your folder reads before it clears, read before clearing; v1.16.1: Clear transaction edits works from memory: §4.2 `_originalCategories`, `_originalType` and what a transaction keeps of its row; §4.5 IndexedDB may not hold the loaded transactions; §10 a stored row holds the CSV's values; §20 Clear transaction edits; v1.16.0: Trends, AI Analysis, Settings and the Welcome rebuilt: §3 file roles; §4.1 `cardDetails`, `sampleData`, AI history `model`; §6 tile tokens and Insights/Settings/Welcome components; §7 rule 7 covers Trends; §8 `loadFiles()` sample option; §9 `shellHero()`, `NO_RANGE_VIEWS`, ☰ → ×, JSON export/import buttons; §12 Settings tabs renamed, the Welcome, range label hidden on Trends and Settings; §13 quick analyses, tab state, history; §20 new fields, Use my own files, Export/Import JSON; §21 quick analyses send `AI_PROMPTS`; v1.15.0: Plan screens rebuilt: §3 file roles; §4.1 `debtPlan` and the Budget / Bucket / Debt / Scenario shapes; §6 `--teal-text` and Plan components; §7 rule 7 covers the Plan charts; §8 plan helpers (`payoffPlan()`, `growthByYear()` …); §9 plan form helpers, `simulatePayoff()` kept unused; §12 Plan titles, Forecast tabs, range label hidden on Plan screens; §13 debt balances in the summary; §20 Clear forecast scenarios / investment mix, Clear debts resets `debtPlan`; v1.14.0: Finances screens rebuilt: §4.1 Bill shape and `recurringConfirmed`; §6 new tokens and Finances components; §7 rule 7 inline SVG charts; §8 new aggregation helpers; §9 drawer, `TX_VIEWS`, SVG/heatmap/format helpers, bill helpers, removed renderers; §11 keywords re-run detection, `transferPairs()`, card payments; §12 page actions and Transactions pills; §20 Clear bills also clears spotted-charge choices; v1.13.0: Clarity redesign — §6 tokens and components; §9 theme, chart colors and app bar helpers; §12 app bar navigation replaces the sidebar and the emoji rule; §17 footer; §20 Data & Reset; v1.12.0: bank-neutral account codes, names, samples and docs; §22 `migrate.js` added; §3, §4, §5, §10, §18, §21 updated; v1.10.5: no section changes: Import Logs race fix; v1.10.4: §8, §13 single `~` is literal; §21 excerpts escape `~`; v1.10.3: §13 refusals; §21 declined runs; v1.10.2: §13 summary states its dates; §21 per-month derivations; v1.10.1: §13 `AI_MAX_TOKENS`, thinking and cut-off answers; v1.10.0: §21 AI Eval added; §13 `callClaude()` raw mode; v1.9.6: §14 folder access lasts until the last tab closes, not one page load; v1.9.5: §7 chart guard; v1.9.4: views-db.js changelog reads CRLF checkouts; v1.9.3: §14 folder permission: re-request on click, Reconnect banner, browser copy wins; v1.9.2: §0 branch, PR and approval workflow added; v1.9.1: §4.5 rehydrate transactions loaded from JSON; v1.9.0: §20 Data & Reset added; v1.8.1: §10 re-upload dedup rewritten for the occurrence-suffix fix; v1.8.0: AI Analysis restored with API-key and output hardening; `renderMarkdown()` added; `samples/` added; §1–3, §13, §14, §17 corrected to match the code)* +*Last updated: v1.16.17 — 2026-11-02 (a file read into another account takes its rows with it: §4.3 the account's `files`, `accountFiles()`; §4.5 the exception to an import never dropping rows; §8 `accountFiles()`, `earlierCopies()`, `moveEarlierCopies()`; §10 Read as changes the ids; §19 `DB.removeTransactions()`, `DB.patchAccount()`; §22 `_rekey()` is also used by imports; v1.16.16: the transaction and transfer drawers store only the fields you change: §4.1 TxOverride, §11 transfer keywords, §12 state mutations; v1.16.15: §1 desktop only; §3 `views-database.js` removed, the Changelog view reads CHANGELOG.md; §6, §18, §19 point to the current screens; v1.16.14: §9 the drawer is a modal dialog: inert page, Tab wraps, first field focused, focus returns after a re-render; v1.16.13: account order: §4.1 `accountOrder` keeps ids not loaded; §8 `orderedAccounts()`; §18 display order; v1.16.12: account names: §4.1 `accountLabels`; §4.3 an account's `name` is its default; §8 `defaultAccountName()`; §9 `acctName()`; §13 the summary lists accounts by `acctName()`; §18 names you type, and clearing them; v1.16.11: AI summary balances at the end of the selected period: §13 `summaryBalanceEnd()` and the date notes; §21 the parser reads the balance lines by how they start; v1.16.10: starting balance counted in every range: §8 `startingBalance()`, `currentBalances()`; §13 summary balances; §18 one balance rule; v1.16.9: the transaction drawer stores only the flags you change: §4.2 flags, §12 state mutations; v1.16.8: flags from transaction rules are worked out on load, never read from storage: §4.2 flags, §8 `txRuleFlag()` / `txFlags()` / `applyTxRulesToAll()`, §19 flags in the transactions store; v1.16.7: transaction links to budgets, buckets and debts by id: §1 version; §3 migrate.js role; §4.1 the `id` fields, TxOverride and its `link` shape; §8 `PLAN_LINK_TYPES`, `newPlanId()`; §9 `planItem()`, `linkedTransactions()`, `unlinkPlanItems()`, `ensurePlanIds()`; §20 clearing budgets, buckets or debts removes their links; §22 the plan-link migration; v1.16.6: JSON export carries account names and order, Read as, flag rules, transfer keywords and theme: §9 `JSON_EXPORT_KEYS`; §12 adding a persisted field; §20 what the export holds; v1.16.5: a new database copies every saved setting: §4.5 the keys that go through `storageKey()`; §12 adding a persisted field; §19 `DB.APP_STATE_KEYS`; v1.16.4: JSON import saves under the storage keys: §4.5 `STORAGE_KEYS` / `storageKey()`; §8 `storageKey()`; §12 adding a persisted field uses the State name; v1.16.3: An import keeps the loaded transactions it doesn't replace: §4.5 an import never drops loaded transactions; §8 `mergeImported()`, `undoTransferDetection()`; §10 memory follows the same re-upload rules; §11 kept transfers are detected again; v1.16.2: Imports read every file before changing anything: §8 `loadFiles()` options and result, `parseCsvFile()` reads only, `importParsedCsv()`, `importProblemText()`, `reloadFromDirHandle()`; §20 Rebuild from your folder reads before it clears, read before clearing; v1.16.1: Clear transaction edits works from memory: §4.2 `_originalCategories`, `_originalType` and what a transaction keeps of its row; §4.5 IndexedDB may not hold the loaded transactions; §10 a stored row holds the CSV's values; §20 Clear transaction edits; v1.16.0: Trends, AI Analysis, Settings and the Welcome rebuilt: §3 file roles; §4.1 `cardDetails`, `sampleData`, AI history `model`; §6 tile tokens and Insights/Settings/Welcome components; §7 rule 7 covers Trends; §8 `loadFiles()` sample option; §9 `shellHero()`, `NO_RANGE_VIEWS`, ☰ → ×, JSON export/import buttons; §12 Settings tabs renamed, the Welcome, range label hidden on Trends and Settings; §13 quick analyses, tab state, history; §20 new fields, Use my own files, Export/Import JSON; §21 quick analyses send `AI_PROMPTS`; v1.15.0: Plan screens rebuilt: §3 file roles; §4.1 `debtPlan` and the Budget / Bucket / Debt / Scenario shapes; §6 `--teal-text` and Plan components; §7 rule 7 covers the Plan charts; §8 plan helpers (`payoffPlan()`, `growthByYear()` …); §9 plan form helpers, `simulatePayoff()` kept unused; §12 Plan titles, Forecast tabs, range label hidden on Plan screens; §13 debt balances in the summary; §20 Clear forecast scenarios / investment mix, Clear debts resets `debtPlan`; v1.14.0: Finances screens rebuilt: §4.1 Bill shape and `recurringConfirmed`; §6 new tokens and Finances components; §7 rule 7 inline SVG charts; §8 new aggregation helpers; §9 drawer, `TX_VIEWS`, SVG/heatmap/format helpers, bill helpers, removed renderers; §11 keywords re-run detection, `transferPairs()`, card payments; §12 page actions and Transactions pills; §20 Clear bills also clears spotted-charge choices; v1.13.0: Clarity redesign — §6 tokens and components; §9 theme, chart colors and app bar helpers; §12 app bar navigation replaces the sidebar and the emoji rule; §17 footer; §20 Data & Reset; v1.12.0: bank-neutral account codes, names, samples and docs; §22 `migrate.js` added; §3, §4, §5, §10, §18, §21 updated; v1.10.5: no section changes: Import Logs race fix; v1.10.4: §8, §13 single `~` is literal; §21 excerpts escape `~`; v1.10.3: §13 refusals; §21 declined runs; v1.10.2: §13 summary states its dates; §21 per-month derivations; v1.10.1: §13 `AI_MAX_TOKENS`, thinking and cut-off answers; v1.10.0: §21 AI Eval added; §13 `callClaude()` raw mode; v1.9.6: §14 folder access lasts until the last tab closes, not one page load; v1.9.5: §7 chart guard; v1.9.4: views-db.js changelog reads CRLF checkouts; v1.9.3: §14 folder permission: re-request on click, Reconnect banner, browser copy wins; v1.9.2: §0 branch, PR and approval workflow added; v1.9.1: §4.5 rehydrate transactions loaded from JSON; v1.9.0: §20 Data & Reset added; v1.8.1: §10 re-upload dedup rewritten for the occurrence-suffix fix; v1.8.0: AI Analysis restored with API-key and output hardening; `renderMarkdown()` added; `samples/` added; §1–3, §13, §14, §17 corrected to match the code)* --- @@ -1069,6 +1086,7 @@ The full IndexedDB implementation is live. See **`docs/archive/INDEXEDDB_PLAN.md - **Bank Schema Registry** — `BANK_SCHEMAS` (global in `core.js`) drives CSV parsing. `detectSchema()` + `normalizeRow()` replace the old hardcoded normalizers. - **Transfer Keywords** — `State.transferKeywords` is edited on the Transfers screen (§11). `detectTransfers()` builds its regex from this list. - **Flags in the `transactions` store (v1.16.8)** — `DB.importTransactions()` still writes `isRecurring`, `isBill` and `isSubscription` into each new row, as they are at import, but FinLens never reads them back (only the developer DB Inspector shows them). Rows already stored are skipped on re-import, so they are never updated. `reconstituteTx()` works the flags out from `txOverrides` and the current rules (§4.2). +- **A file read into another account (v1.16.17, §10)** is one of the few times rows leave the store (with Data & reset's clears, §20; an import's leftover copies, §10; the account-code migration, §22). `DB.removeTransactions([{ id, account }])` deletes the old rows and lowers each account's `txCount` (its date range stays). `DB.patchAccount(id, patch | null)` updates or deletes an account record, and never creates one. ### Adding a new bank Add a new object to `BANK_SCHEMAS` in `core.js`. No other code changes needed if the bank follows standard column conventions (`date`, `amount`, `description`). For non-standard conventions use: `amountSign: 'inverted'`, custom `columns` map, `dateFormat` (`'YYYY-MM-DD'` | `'MM/DD/YYYY'` | `'DD/MM/YYYY'`), and `postDateFormat` if the post-date column uses a different format than the primary date column (see the `credit_card` schema). @@ -1184,6 +1202,8 @@ Each step is idempotent: on new data nothing matches and nothing is written. **Transaction ids.** The id hashes the account id (§10), so renaming an account changes its transactions' ids. Stored transactions lack the bank time and post date, so the id can't be recomputed from them. `hash()` is linear instead (h = 31·h + c mod 2³²), so `_rekey()` takes the prefix hash out of the old id and extends it with the new account id. The id keeps only |h|, so there are two candidates. `_fits()` keeps the one that matches the transaction's date, description and amount, with a time or post date found by search. If neither or both match, the transaction keeps its old id, and the count is logged. That didn't happen in testing (20,000 random ids, plus the samples). +**Also used by imports (v1.16.17).** `earlierCopies()` (§10) calls `_rekey(tx, newAccount, oldAccount)` on a freshly read row to find the id it had under the account its file was read into before. Same rules: a null result (no candidate or two) means the row isn't traced. + **Rules** - **Changing `hash()` or the hash-key layout in `mkTx()`** changes every transaction id. It breaks re-import dedup and every edit. Don't, unless you add a migration for it here. - **Renaming a code again** needs a new mapping here. Old data must still load. diff --git a/README.md b/README.md index 11f763b..da1e986 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ > A zero-install, browser-native finance dashboard. Drop in your bank CSVs, get instant clarity on your money. -**Version:** v1.16.16  ·  **Stack:** Vanilla JS, HTML, CSS  ·  **Install:** None +**Version:** v1.16.17  ·  **Stack:** Vanilla JS, HTML, CSS  ·  **Install:** None --- @@ -263,4 +263,4 @@ See [CHANGELOG.md](CHANGELOG.md) for the full release history. --- -*FinLens v1.16.16 — Built for one person's finances, designed to be useful for anyone's.* +*FinLens v1.16.17 — Built for one person's finances, designed to be useful for anyone's.* diff --git a/index.html b/index.html index 032791e..57c5b7c 100644 --- a/index.html +++ b/index.html @@ -1202,7 +1202,7 @@
-
v1.16.16 · FinLens
+
v1.16.17 · FinLens
diff --git a/js/core.js b/js/core.js index b24c3c3..24474e8 100644 --- a/js/core.js +++ b/js/core.js @@ -679,6 +679,15 @@ function defaultAccountName(a) { return file; } +// The CSV files an account's transactions came from (v1.16.17): its `files`, kept by +// importParsedCsv() and by a file moving to another account (moveEarlierCopies()). An +// account saved before v1.16.17 has no list: its sourceFile, the last file imported into it. +function accountFiles(a) { + if (!a) return []; + if (Array.isArray(a.files)) return a.files.slice(); + return a.sourceFile && a.sourceFile !== 'manual' ? [a.sourceFile] : []; +} + // Reads one CSV file and turns its rows into transactions. It changes nothing: no State, // browser storage or IndexedDB writes (v1.16.2). loadFiles() reads every file this way // first and imports (importParsedCsv()) only when at least one transaction was read. @@ -786,8 +795,15 @@ async function importParsedCsv(p) { State.accounts[accountId] = State.accounts[accountId] || { id: accountId, name: accountName, kind, balance: null, limit: null, sourceFile: file.name }; - State.accounts[accountId].sourceFile = file.name; - State.accounts[accountId].name = accountName; + const acct = State.accounts[accountId]; + // Every CSV imported into the account, not only the last one (sourceFile): Settings → + // Import lists them all, and a file re-read under another account ("Read as") takes its + // rows out of this one (earlierCopies(), v1.16.17). An account saved before has none + // and starts from its sourceFile. + const files = accountFiles(acct); + acct.files = files.includes(file.name) ? files : files.concat(file.name); + acct.sourceFile = file.name; + acct.name = accountName; // Persist to IndexedDB (if DB is ready) let importResult = null; @@ -799,6 +815,7 @@ async function importParsedCsv(p) { kind, schemaId: schema.id, sourceFile: file.name, + files: acct.files, displayOrder: State.accountOrder.indexOf(accountId), manualBalance: State.manualBalances[accountId] != null ? Number(State.manualBalances[accountId]) : null, creditLimit: State.manualBalances[accountId+'_limit'] != null ? Number(State.manualBalances[accountId+'_limit']) : null, @@ -906,11 +923,16 @@ async function loadFiles(fileList, assignments, opts = {}) { if (typeof updateFolderBanner === 'function') updateFolderBanner(); } State.fileMeta = []; + // A file read as another kind than before ("Read as") gives its rows under another + // account, so with other ids. Its earlier rows are found before anything is written + // (v1.16.17), then leave their old account below. + const moves = await earlierCopies(read); const fresh = []; for (const p of read) { if (!p.schema || p.problem) continue; // a file that gave no transactions creates no account fresh.push(...await importParsedCsv(p)); } + if (moves.size) await moveEarlierCopies(moves, read, fresh); // The files' transactions join the loaded ones; none of the others is dropped (v1.16.3). State.transactions = await mergeImported(State.transactions, fresh); // Edits still saved under a transaction's id from before v1.12.0 (e.g. kept while the @@ -990,6 +1012,105 @@ function undoTransferDetection(t, row) { t.categories = ov.categories && ov.categories.length ? [...ov.categories] : (ov.category ? [ov.category] : [...cats]); } +// A file's earlier rows under another account (v1.16.17, for loadFiles(), before anything is +// written). The account id is part of every transaction id (mkTx(), CLAUDE.md §10), so a file +// read as another kind than before ("Read as" savings → checking, say) gives the same rows +// with new ids. Up to v1.16.16 the merge kept the old ones too, as it keeps every loaded +// transaction a file doesn't replace (mergeImported()): the file's rows showed under both +// accounts, in memory, IndexedDB and finlens-state.json, until Rebuild from your folder. +// - The accounts that held the file: those listing it in `files` (accountFiles()); an account +// saved before v1.16.17 has no list, so its sourceFile or an import-log entry counts. +// - The rows: each of the file's transactions gives the id it had in such an account, worked +// out from its own id (IdMigration._rekey(): the hash takes the account id last, CLAUDE.md +// §22). The id keeps the bank's time or the card's post date, so two rows with the same +// date, description and amount stay apart. A loaded row with that id is the file's earlier +// copy, unless a file in this import gives that id too: then it stays, and the merge +// replaces it as usual. +// A file that gave no transactions (a kind that can't read it) moves nothing. +// Returns Map(old id → { id: the new id, from: the old account, file }). +async function earlierCopies(read) { + const moves = new Map(); + const loaded = new Set(State.transactions.map(t => t.id)); + const files = read.filter(p => p.schema && !p.problem && p.txs.length); + if (!loaded.size || !files.length) return moves; + const fresh = new Set(files.flatMap(p => p.txs.map(t => t.id))); + const accts = Object.values(State.accounts); + let log = []; + if (accts.some(a => !Array.isArray(a.files)) && typeof DB !== 'undefined' && DB._db) { + try { log = await DB.getImportLog(); } catch (e) { log = []; } + } + const held = (a, name) => Array.isArray(a.files) ? a.files.includes(name) + : a.sourceFile === name || log.some(e => e.filename === name && e.accountId === a.id); + for (const p of files) { + const from = accts.filter(a => a.id !== p.accountId && held(a, p.name)).map(a => a.id); + for (const t of p.txs) { + for (const a of from) { + const old = IdMigration._rekey(t, p.accountId, a); + if (old && loaded.has(old) && !fresh.has(old)) moves.set(old, { id: t.id, from: a, file: p.name }); + } + } + } + return moves; +} + +// Takes a file's earlier rows out of their old account (v1.16.17, for loadFiles(), after the +// files are imported). `moves` is earlierCopies()'s result; `fresh` the files' transactions. +// - Edits go with the row: description, category, type, flags, notes and plan link move to +// the new id, since it's the same bank transaction in another account. If the new id has +// an edit already (only a copy left by an earlier version can), its fields win. +// - Memory: the old rows leave State.transactions. The files' rows that got an edit are +// rebuilt with it (in `fresh`, in place). +// - Accounts: the file leaves the old account's `files`. An account left with no transaction, +// that no file of this import went into, is removed (memory and IndexedDB). Its name, +// balances, card details and place in the order stay, by id, for when a file goes back. +// - IndexedDB: the old rows are deleted from the transactions store. +// loadFiles()'s autosave writes it all to finlens-state.json. State.fileMeta notes what moved +// (the Last import card shows it). +async function moveEarlierCopies(moves, read, fresh) { + const ovs = State.txOverrides; + const edited = new Set(); + for (const [old, m] of moves) { + if (!ovs[old]) continue; + ovs[m.id] = { ...ovs[old], ...(ovs[m.id] || {}) }; + delete ovs[old]; + edited.add(m.id); + } + if (edited.size) { + lsSet('txOverrides', ovs); + fresh.forEach((t, i) => { if (edited.has(t.id)) fresh[i] = reconstituteTx({ ...t, description: t._originalDesc }, ovs[t.id]); }); + } + State.transactions = State.transactions.filter(t => !moves.has(t.id)); + // per old account: the files that left it; per file: how many rows moved, and from where + const left = new Map(), perFile = new Map(); + for (const m of moves.values()) { + if (!left.has(m.from)) left.set(m.from, new Set()); + left.get(m.from).add(m.file); + const f = perFile.get(m.file) || { count: 0, from: new Set() }; + f.count++; f.from.add(m.from); perFile.set(m.file, f); + } + const names = new Map([...left.keys()].map(id => [id, typeof acctName === 'function' ? acctName(id) : id])); + const into = new Set(read.filter(p => p.schema && !p.problem).map(p => p.accountId)); + const db = typeof DB !== 'undefined' && DB._db; + if (db) await DB.removeTransactions([...moves].map(([id, m]) => ({ id, account: m.from }))); + for (const [id, gone] of left) { + const a = State.accounts[id]; + if (!a) continue; + a.files = accountFiles(a).filter(f => !gone.has(f)); + if (!into.has(id) && !State.transactions.some(t => t.account === id)) { + delete State.accounts[id]; + if (db) await DB.patchAccount(id, null); + continue; + } + if (gone.has(a.sourceFile) && a.files.length) a.sourceFile = a.files[a.files.length - 1]; + if (db) await DB.patchAccount(id, { files: a.files, sourceFile: a.sourceFile }); + } + for (const fm of State.fileMeta) { + const f = perFile.get(fm.name); + if (f) fm.moved = { count: f.count, from: [...f.from].map(id => names.get(id)) }; + } + console.log(`[FinLens] Import: ${moves.size} transaction${moves.size === 1 ? '' : 's'} moved to the account ${perFile.size === 1 ? 'its file is' : 'their files are'} read as now (${edited.size} with edits)`); +} + function detectTransfers() { // Build keyword regex from user-editable list (falls back to defaults if empty) const kws = (State.transferKeywords && State.transferKeywords.length) diff --git a/js/db.js b/js/db.js index 8ba995a..87bb556 100644 --- a/js/db.js +++ b/js/db.js @@ -300,6 +300,55 @@ const DB = { return { inserted, skipped, removed, dateRangeMin: fileMinDate, dateRangeMax: fileMaxDate, gapWarning }; }, + /** + * Deletes the rows a file gave under its old account once it's read as another kind + * (v1.16.17, moveEarlierCopies() in core.js). `rows` is [{ id, account }]; a row the store + * doesn't hold is skipped (after a restore from finlens-state.json it may hold none). + * Each account's txCount goes down by the rows deleted from it; its date range is left as + * it is. Resolves the number of rows deleted. + */ + async removeTransactions(rows) { + if (!this._db || !rows.length) return 0; + let have; + try { have = new Set(await this._req(this._getStore('transactions').getAllKeys())); } + catch (e) { console.warn('DB.removeTransactions:', e); return 0; } + const del = rows.filter(r => have.has(r.id)); + if (!del.length) return 0; + const ok = await new Promise((res) => { + const t = this._db.transaction('transactions', 'readwrite'); + const store = t.objectStore('transactions'); + del.forEach(r => store.delete(r.id)); + t.oncomplete = () => res(true); + t.onerror = t.onabort = () => res(false); + }); + if (!ok) { console.warn('DB.removeTransactions: the delete failed'); return 0; } + const per = new Map(); + del.forEach(r => per.set(r.account, (per.get(r.account) || 0) + 1)); + for (const [id, n] of per) { + try { + const store = this._db.transaction('accounts', 'readwrite').objectStore('accounts'); + const a = await this._req(store.get(id)); + if (a) { a.txCount = Math.max(0, (a.txCount || 0) - n); await this._req(store.put(a)); } + } catch (e) { console.warn('DB.removeTransactions: account count', id, e); } + } + return del.length; + }, + + /** + * Changes fields of a stored account record, or deletes it when `patch` is null + * (v1.16.17, moveEarlierCopies()). A record the store doesn't have isn't created: an + * import (upsertAccount()) writes whole records. + */ + async patchAccount(id, patch) { + if (!this._db) return; + try { + const store = this._db.transaction('accounts', 'readwrite').objectStore('accounts'); + if (patch === null) { await this._req(store.delete(id)); return; } + const a = await this._req(store.get(id)); + if (a) await this._req(store.put({ ...a, ...patch })); + } catch (e) { console.warn('DB.patchAccount:', id, e); } + }, + async _updateAccountCoverage(accountId, fileMin, fileMax, newlyInserted) { if (!this._db) return; try { diff --git a/js/views-cleanup.js b/js/views-cleanup.js index 5151e7a..bd2f339 100644 --- a/js/views-cleanup.js +++ b/js/views-cleanup.js @@ -253,7 +253,9 @@ const DataCleanup = { // Other CSVs in a format it doesn't read are skipped, as every import skips them. // `r` is loadFiles()'s result, before anything is cleared. _rebuildBlockers(r) { - const imported = new Set(Object.values(State.accounts).map(a => a.sourceFile).filter(Boolean)); + // Every file of every account (accountFiles(), v1.16.17), not only each one's last: a file + // read into an account that another file fed last is imported too. + const imported = new Set(Object.values(State.accounts).flatMap(a => accountFiles(a))); return r.files.filter(f => f.problem === 'unreadable' || f.problem === 'no-rows' || (f.problem === 'unknown-format' && imported.has(f.name))); }, diff --git a/js/views-data.js b/js/views-data.js index 638ab5e..65486c9 100644 --- a/js/views-data.js +++ b/js/views-data.js @@ -57,12 +57,16 @@ function parseMoneyInput(raw) { if (s==='') return null; return /^-?\d*\.?\d+$/.test(s)?parseFloat(s):NaN; } -// Files FinLens has imported, one per account that came from a CSV. +// Files FinLens has imported: every CSV of each account that came from one (accountFiles(), +// v1.16.17; before, only the account's last file, so a second file read into an account, +// with "Read as" say, wasn't listed). Rows and dates are the account's: with several files +// they're shown once, across that account's rows (`span` = how many; 0 on the others). function importedFiles() { - return orderedAccounts().filter(a=>a.sourceFile&&a.sourceFile!=='manual').map(a=>{ + return orderedAccounts().filter(a=>a.sourceFile&&a.sourceFile!=='manual').flatMap(a=>{ const own=State.transactions.filter(t=>t.account===a.id); const r=accountDateRange(a.id); - return { name:a.sourceFile, acct:a, rows:own.length, min:r.min, max:r.max }; + const fl=accountFiles(a), names=fl.length?fl:[a.sourceFile]; + return names.map((name,i)=>({ name, acct:a, rows:own.length, min:r.min, max:r.max, span:i?0:names.length })); }); } function readAsOptions(sel, extra) { @@ -157,7 +161,7 @@ registerView('import','Import',(root)=>{ const next=ir.dateRangeMax?dayjs(ir.dateRangeMax).add(1,'day').format('MMM D, YYYY'):'—'; return `
${escapeHtml(nm)} ${escapeHtml(r.filename)} · covers ${fmtD(ir.dateRangeMin)} – ${fmtD(ir.dateRangeMax)}${cov?` · ${cov.count.toLocaleString()} in FinLens`:''} · start the next export on ${next}
- ${ir.inserted.toLocaleString()} new · ${ir.skipped.toLocaleString()} already imported${ir.removed?` · ${ir.removed.toLocaleString()} leftover duplicate cop${ir.removed!==1?'ies':'y'} removed`:''} + ${ir.inserted.toLocaleString()} new · ${ir.skipped.toLocaleString()} already imported${ir.removed?` · ${ir.removed.toLocaleString()} leftover duplicate cop${ir.removed!==1?'ies':'y'} removed`:''}${r.moved?` · ${r.moved.count.toLocaleString()} moved from ${escapeHtml(r.moved.from.join(', '))}`:''} ${ir.gapWarning?callout('warn',`No data for ${fmtD(ir.gapWarning.from)} – ${fmtD(ir.gapWarning.to)}. Import a file that covers those dates to fill the gap.`):''}
`; }).join('')} `); @@ -172,9 +176,10 @@ registerView('import','Import',(root)=>{ FileRead asRowsDates ${files.map(f=>{ const sel=State.fileAssignments[f.name]||(f.acct.kind==='unknown'?'other':f.acct.kind); + const rs=f.span>1?` rowspan="${f.span}"`:''; return `${escapeHtml(f.name)}
${spanText(f.min,f.max)}
- ${f.rows.toLocaleString()}${spanText(f.min,f.max)}`; + ${f.span?`${f.rows.toLocaleString()}${spanText(f.min,f.max)}`:''}`; }).join('')} ${statusLine('readAsMsg',m)} `); @@ -184,7 +189,9 @@ registerView('import','Import',(root)=>{ lsSet('fileAssignments',State.fileAssignments); const label=(READ_AS.find(r=>r[0]===s.value)||[])[1]||s.value; const msg=$('#readAsMsg'); - if (msg) { msg.className='status-line good'; msg.textContent=`Saved. FinLens reads ${name} as ${label} from the next reload. To re-read the rows already imported, use Rebuild in Data & reset.`; } + // The next reload re-reads the file under the account it's read as now, and its rows + // already imported leave the old one with their edits (earlierCopies(), v1.16.17). + if (msg) { msg.className='status-line good'; msg.textContent=`Saved. FinLens reads ${name} as ${label} from the next reload, and its transactions move to that account with their edits. If FinLens can't read the file that way, they stay where they are.`; } }); } @@ -412,6 +419,7 @@ async function captureImportResults(res) { account: f.account, accountId: Object.values(State.accounts).find(a=>a.sourceFile===f.name||a.name===f.account)?.id||'', importResult: f.importResult, + moved: f.moved||null, // { count, from: [account names] }: rows that left another account (v1.16.17) })) .concat(res.files.filter(f=>f.problem&&f.problem!=='skipped').map(f=>({ filename:f.name, problem:importFileProblem(f) }))); if (typeof DB!=='undefined'&&DB._db) _coverageCache=await DB.getCoverage(); diff --git a/js/views-settings.js b/js/views-settings.js index 2263264..81d2c40 100644 --- a/js/views-settings.js +++ b/js/views-settings.js @@ -67,7 +67,7 @@ function _renderGeneralSettings(root) { ``).join('')} -
VersionFinLens v1.16.16
+
VersionFinLens v1.16.17
`); const opts=$$('[data-theme-opt]',root); opts.forEach((b,i)=>{