Skip to content

v1.9.3: fix finlens-state.json not loading or saving after reload (folder permission) - #7

Merged
gitluis merged 3 commits into
masterfrom
fix-folder-permission
Oct 25, 2026
Merged

gitluis merged 3 commits into
masterfrom
fix-folder-permission

Conversation

@gitluis

@gitluis gitluis commented Oct 25, 2026 •

Copy link
Copy Markdown
Owner

Stacked on #5 (docs-branch-pr-workflow), so it can reuse folderWithWriteAccess() and saveStateNow() from #3. Merge after #2 → #3 → #4 → #5.

Summary

Fixes the folder-permission bug found while reviewing #1 in Edge: after a page reload, finlens-state.json was neither loaded nor saved, and Save Now gave no feedback. Patch bump to v1.9.3.

Cause

  • Permission lapses on reload. Chromium browsers (Edge, Chrome) reset a stored folder's permission to "prompt" on every page load.
  • Never re-requested. loadStateFromFile() and saveStateToFile() in core.js never called queryPermission / requestPermission, so both threw NotAllowedError. That's the [FinLens] File load error: … not allowed by the user agent … line at startup.
  • Read-only grant. The folder picker and reloadDataFiles() only ever asked for read. Writing the state file needs readwrite.
  • Silent Save Now. The button called saveStateToFile() and ignored the result, so a failure only reached console.warn.

Effect: the app booted from IndexedDB (data safe in the browser), but every automatic save failed silently, so finlens-state.json went stale. The bug was already on master; #1–#6 don't fix it.

What changed

js/core.js

  • New folderPermission(dir, mode): wraps queryPermission and never prompts.
  • loadStateFromFile() checks access first. Without it, it skips the file with a plain log line (no error) and sets State._folderNeedsAccess.
  • saveStateToFile() skips quietly without readwrite (no warning every 5 s) and shows the banner. A successful save clears the banner. Write errors are kept in State._lastSaveError.
  • saveStateNow() returns 'error' for a failed write instead of 'denied'.
  • forgetDataFolder() also clears the banner.

js/views-data.js

  • Banner (updateFolderBanner()): Backups paused plus a Reconnect folder button, shown above every view while access has lapsed.
  • Reconnect (reconnectDataFolder() / reconnectFolderClick()): asks for readwrite from the click, then writes this browser's data to finlens-state.json. Per your decision, the browser copy wins: the file is not loaded over it. If access is refused, the banner says so and keeps the button. On success it shows a confirmation that hides after 8 s.
  • Save Now (saveNowClick()): asks for readwrite on click, saves through saveStateNow(), and shows "Saved to finlens-state.json … at h:mm A" or the reason it failed (no folder / permission refused / the write error).
  • The Auto-Backup card says Backup paused while access is missing, instead of "Backup enabled".
  • The folder picker, Reload and Reconnect Folder now ask for readwrite. If Reload's write request is refused but reading still works, it reloads anyway and backups stay paused.

Other files

  • index.html: #folderBanner slot between the top bar and the content, plus two CSS rules (tokens only).
  • js/init.js: shows the banner after the first render.
  • js/views-cleanup.js: Data & Reset reports a failed state-file write as a warning with the reason.
  • Version v1.9.3 in CHANGELOG.md, index.html, README.md, views-settings.js and CLAUDE.md §1. CLAUDE.md §14 documents the permission rules.

One addition beyond the plan: an empty browser never overwrites the backup

Before this fix the file was never written after a reload, which accidentally protected it. Once saving works, a browser with no data of its own (a new browser, or site data cleared) that picks or reconnects the folder would replace a good finlens-state.json with an empty state.

So when the browser has zero transactions, picking or reconnecting restores from the file, exactly as boot does (_restoreStateIfBrowserEmpty()). In every other case the browser copy wins, as decided. Please confirm you're OK with this exception.

How it was tested

Headless Chromium (Playwright) against samples/, with the CDN libraries served from local npm copies (SRI hashes match). The folder is simulated with a fake directory handle that behaves like Chromium: permission resets to "prompt" on every load, requestPermission() throws without a click, and file access without permission throws NotAllowedError.

Baseline (#5's code): the harness reproduces both symptoms. It logs the File load error at startup, shows no Save Now message, and never writes the file.

This branch: 29/29 checks pass.

# Scenario Result
1 Pick folder → 257 sample transactions imported; picker asks for readwrite; autosave writes the file; no banner ✅
2 Reload → no File load error; boots from IndexedDB with all 257 transactions; banner with Reconnect shown ✅
3 Edit while access lapsed → no write, no warnings or errors in the console ✅
4 Auto-Backup card says paused; Save Now with access refused explains why ✅
5 Save Now with access granted → "Saved …"; file contains the edit made while lapsed; banner cleared; request came from the click ✅
6 Reconnect refused → banner says so and keeps the button. Reconnect granted → success; file overwritten with the browser copy (a sentinel budget planted in the file is gone from the file and never appears in the app); no CSV reload; autosave works again; success banner hides itself ✅
7 Empty browser + stored folder → 0 transactions + banner; Reconnect restores all 257 transactions (real Dates) from the file; file left intact ✅
8 Permission already granted on load (Chrome "Allow on every visit") → boots from the file, no banner. Read-only grant → boots from the file but shows the banner ✅
9 Every view and every Settings tab renders without console errors ✅
10 Disconnecting the folder hides the banner ✅

Not verified:

  • Real Edge/Chrome permission prompts. The harness simulates them, but real browsers vary (e.g. Chrome's 3-way "Allow on every visit" prompt).
  • Safari/Firefox. They have no showDirectoryPicker, so this code path isn't reached there.

Please check before merging

  • In Edge: reload → the banner appears and the console has no File load error → Reconnect folder → Allow → the banner confirms, and finlens-state.json's modified time updates.
  • Save Now: shows "Saved …", or a reason when you click Block.
  • The empty-browser exception described above is what you want.
  • Version order with v1.10.0: AI Eval — grounding eval for AI Analysis #6: v1.10.0: AI Eval — grounding eval for AI Analysis #6 is v1.10.0 and is based on v1.9.0: Settings → Data & Reset tab (clean up data, DB, logs; start over) #3, not on this stack. Whichever merges second needs its CHANGELOG entry and version strings reconciled.
  • Known, not changed here:
    • Edge will still ask on every visit. That's the browser's behavior; the fix is that FinLens now asks and reports it, instead of failing silently.
    • "Last saved" shows Never after a reload, because lastFsyncTime isn't persisted.
    • State restored from the file (at boot or via the new exception) still isn't copied into IndexedDB. That's the same as the existing boot path.

Generated by Claude Code

Chromium browsers reset a stored folder's permission to "prompt" on every
page load. loadStateFromFile() and saveStateToFile() never checked or
re-requested it, so both threw NotAllowedError; the folder was only ever
granted read access; and Save Now ignored the result.

- folderPermission() checks access without prompting; the file functions
  skip quietly when it's missing and set State._folderNeedsAccess.
- "Backups paused" banner above every view with a Reconnect folder button.
  Reconnecting asks for readwrite and writes the browser copy to the file
  (browser copy wins, per Luis 2026-10-24).
- A browser with no transactions restores from the file instead, so an
  empty state never overwrites the only backup.
- Save Now asks for readwrite on click, saves via saveStateNow(), and
  shows "Saved" or why it failed.
- Folder picker, Reload and Reconnect Folder ask for readwrite.
- saveStateNow() returns 'error' for a failed write; Data & Reset reports it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFQhVNzHpvwQsfPoduecJF
@gitluis gitluis self-assigned this Oct 25, 2026
@gitluis gitluis removed their assignment Oct 25, 2026
…lder-permission

Conflict in forgetDataFolder(): kept both resets (#7's folder-access
banner and #3's csvAutoImportPaused).

Also makes #7's Reconnect respect the pause: reconnectDataFolder() no
longer re-imports the CSVs, and _restoreStateIfBrowserEmpty() no longer
loads finlens-state.json over a browser that was cleared on purpose.
The Import tab's paused text matches the Reconnect button. CHANGELOG 1.9.3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QoR1wxcaDjVVk3xmd5rRCt

gitluis commented Oct 25, 2026

Copy link
Copy Markdown
Owner Author

Update from #3's review (merged forward in 1ba8c57)

#3 now pauses the folder's automatic CSV import after Clear transactions & accounts, so a cleared database stays empty after a reload. That change touched #7's Reconnect, so this merge also adjusts it:

  • No re-import on Reconnect while paused: reconnectDataFolder() no longer re-imports the CSVs while the pause is set.
  • No file restore over a cleared browser: _restoreStateIfBrowserEmpty() treats a cleared browser as cleared on purpose and doesn't load finlens-state.json over it. This follows the "browser copy wins" decision.
  • Wording: Settings → Import's status line now matches the Reconnect button.
  • Conflict: forgetDataFolder() had a conflict, and both resets are kept.

Tested headless:

  • After a browser restart, Clear, then reload, then Reconnect: stays at 0 transactions, backups resume, and the file is saved with 0.
  • Reconnect without a clear: still restores or re-imports 257 as before.
  • 26/26 clear-and-reload checks and the all-views smoke test pass on this branch.

🤖 Generated with Claude Code

https://claude.ai/code/session_01QoR1wxcaDjVVk3xmd5rRCt


Generated by Claude Code

@gitluis

gitluis commented Oct 25, 2026

Copy link
Copy Markdown
Owner Author

Review: manual test of fix-folder-permission (1ba8c57)

Tester: Luis
Date: 2026-10-25
Environment: Windows, Edge InPrivate, index.html opened via file://
Test data: the synthetic CSVs in samples/ (257 transactions across 3 accounts), copied to Documents\finlens-test. No real data was used.

All tests below were run on 1ba8c57 and passed.


Results (1ba8c57)

I started fresh: a new InPrivate window, and a test folder with only the three CSVs (no finlens-state.json).

# Test Expected Result
1 Settings → Import → Pick a Folder → finlens-test 257 transactions loaded; ✓ Backup enabled; no banner ✅ Pass (see note)
2 Make access lapse: close the FinLens tab, open index.html in a new tab Backups paused banner with Reconnect folder; no File load error; "Folder access needs to be renewed this session…"; boots from IndexedDB with 257; readwrite: 'prompt' ✅ Pass
3 Settings → Import while paused Auto-Backup card says ⚠ Backup paused with an amber border; folder row shows Reconnect Folder; 257 loaded ✅ Pass
4 Save Now → Don't allow, twice Red "Not saved: FinLens doesn't have permission to write to 'finlens-test'…"; Edge asks again on the second click, so the message's "click again and choose Allow" advice works ✅ Pass
5 Add budget PR7 TEST while paused, wait 10 s No new warnings or errors in the console (before the fix, a failed save was logged every 5 s) ✅ Pass
6 Banner → Reconnect folder → Allow Green "Reconnected to 'finlens-test'. This browser's data was saved to finlens-state.json, and backups are back on."; file modified at 4:35 PM; the file contains PR7 TEST (the browser copy wins) ✅ Pass
7 Save Now with access "Saved to finlens-state.json in 'finlens-test' at 4:38 PM."; ✓ Backup enabled; Last saved 4:38 PM ✅ Pass
8 New browser (new InPrivate window) → Pick a Folder "Browser had no data; restored 257 transactions from finlens-state.json"; PR7 TEST in Budget, so the data came from the file and not the CSVs; the file is intact (same size, still has PR7 TEST) ✅ Pass
9 Make access lapse with 0 transactions in browser storage → Reconnect folder → Allow 0 transactions and the banner before; green "…This browser had no data, so it was restored from finlens-state.json."; 257 back ✅ Pass
Screenshots

Test 2: a reload in the same tab keeps access (boots from the file, no banner)

pr7-02a-same-tab-reload-boot-from-file pr7-02b-same-tab-reload-access-kept

Test 2: closing the tab and reopening lapses access (banner, boots from IndexedDB)

pr7-02c-new-tab-banner pr7-02d-new-tab-access-lapsed

Test 3: Import tab while paused

pr7-03-backup-paused

Test 4: Save Now refused

pr7-04-save-now-refused

Test 5: budget added while paused, console quiet

pr7-05-budget-added-while-paused

Test 6: Reconnect allowed, browser copy saved to the file

pr7-06a-reconnected pr7-06b-file-modified-time pr7-06c-file-has-pr7-test

Test 7: Save Now with access

pr7-07-save-now-saved

Test 8: new browser restores from the file

pr7-08a-restored-log pr7-08b-budget-from-file pr7-08c-file-intact

Test 9: empty browser, then Reconnect restores

pr7-09-reconnect-restored

Notes

  • Edge keeps access through a reload in the same tab.
    • On F5, readwrite stayed granted, so FinLens booted from finlens-state.json with no banner. That's the correct behavior (the PR's headless case 8).
    • Access only lapsed after I closed the tab and opened index.html in a new one.
    • The PR description, the v1.9.3 CHANGELOG entry, CLAUDE.md §14 and the comment in core.js all say Chromium resets the permission on every page load, which isn't what Edge did here. The fix works either way; only the wording is off.
  • Test 1: finlens-state.json wasn't in the folder yet when I checked, so I clicked Save Now. FinLens saves about 5 seconds after an import, and Test 8's console shows that save running after a folder pick (Auto-saved state to finlens-state.json). I most likely checked too early.
  • Console: the only errors are the red TypeError … views-db.js:734 (fixed in v1.9.4: fix Changelog view crashing on Windows (CRLF) checkouts #8) and "Canvas is already in use" (fixed in v1.9.5: fix "Canvas is already in use" errors and leaked charts #9). The Canvas errors appear on every startup, not only after a re-import (Tests 2 and 9). The 12 "Tracking Prevention blocked access to storage" warnings come from Edge and are unrelated to this PR.

Please check before merging

  • In Edge: with access lapsed, the banner appears, the console has no File load error, and Reconnect → Allow confirms and updates the file's modified time (Tests 2 and 6).
  • Save Now: shows "Saved …", or the reason when access is refused (Tests 4 and 7).
  • The empty-browser exception: confirmed, it's what I want (Tests 8 and 9).
  • Version order with v1.10.0: AI Eval — grounding eval for AI Analysis #6: not blocking. v1.10.0: AI Eval — grounding eval for AI Analysis #6 is still a draft at v1.10.0; its CHANGELOG entry and version strings get reconciled when it's ready.
  • Known, not changed here: seen as described. "Last saved" shows Never after access lapses (Test 3), and data restored from the file isn't copied into browser storage, so the next lapse shows 0 transactions until Reconnect (Test 9).

Not tested

  • Chrome: only Edge was tested, so Chrome's "Allow on every visit" prompt wasn't seen.
  • Reconnect refused: only Save Now's refusal was tested by hand. The banner's refusal message is covered by the headless tests.
  • Disconnect hides the banner, and Reconnect after Clear transactions & accounts: headless only.
  • Real data: not tested with my real CSV folder.

Verdict

Approved for 1ba8c57. Every test passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants