Skip to content

feat(session): restore the exact last file, not just the folder - #25

Open
lampietti wants to merge 1 commit into
ekino:mainfrom
lampietti:feat/restore-last-file
Open

lampietti wants to merge 1 commit into
ekino:mainfrom
lampietti:feat/restore-last-file

Conversation

@lampietti

Copy link
Copy Markdown
Member

Description

What

Session restore now reopens the exact document you were last reading, instead of always falling back to the folder's README.

Why

The store only ever persisted lastFolder. On every normal relaunch (no CLI arg, no Finder "Open With"), setRootPath was called with no specific file, which always falls through to autoSelectReadme() — so no matter which document you had open when you quit, the next launch showed the folder's README (if one exists).

Changes

  • New lastFile store key, written on every successful loadFile() (fire-and-forget, so it doesn't affect render timing or the debug HUD's phase measurements)
  • resolveInitialView (src/utils.ts) takes the saved file as a third argument. When it's actually nested under the saved folder, it's returned as a path relative to that folder — ready for setRootPath's existing fileToOpen parameter — otherwise (stale/corrupted store) it falls back to today's folder-only behavior. 2 new unit tests plus the 6 existing ones updated for the new signature
  • Restoring goes through setRootPath(savedFolder, relativeFile), not openFileFromPath(absolutePath): the latter derives its root from the file's own immediate parent, which would have silently narrowed the sidebar root for any file nested more than one level deep (e.g. reading docs/guide/api.md from a docs root would have reopened with root = docs/guide, hiding everything else under docs)
  • If the saved file no longer exists (deleted/renamed/moved), the folder still opens but with nothing selected, rather than silently substituting the README — this needed no new code, loadFile's existing error handling already leaves the UI in exactly that state when the read fails
  • README and docs/guide/features.md updated to describe the new behavior

Out of scope, for reviewers: only the main window ever falls through to this restore path — spawned viewer-N windows always receive an explicit PendingOpen (folder, file, or empty) and are unaffected, matching how lastFolder already behaves.

Verification

  • npx tsc --noEmit, 316 tests (up from 307), all passing
  • Manually verified in a running debug build:
    • Set lastFolder to the repo root and lastFile to a doc two levels deep (docs/guide/features.md), relaunched with no CLI arg — the file reopened (confirmed via the read_document log line matching the file's exact byte count) and lastFolder in the store was unchanged after relaunch, proving the root stayed at the repo root rather than narrowing to docs/guide
    • Same setup, but with features.md moved aside before relaunch — zero read_document calls in the log, even though the repo root does have its own README.md, confirming there is no silent fallback to it

Type of Change

  • New feature
  • Bug fix
  • Refactoring
  • Documentation

Checklist

  • I have tested my changes locally
  • I have updated the documentation if needed
  • My code follows the existing code style

🤖 Generated with Claude Code

Session restore only ever persisted `lastFolder`. Every cold start with no
pending open (no CLI arg, no Finder "Open With") called setRootPath with
no file, which always falls through to autoSelectReadme — so whichever
document you were reading, the next launch showed the folder's README
instead.

A new `lastFile` store key is now written on every successful loadFile,
and threaded through the existing resolveInitialView (a relative path,
validated to actually sit inside the saved folder — a corrupted or
stale store just falls back to the folder-only behavior).

Restoring goes through setRootPath(savedFolder, relativeFile) rather than
reusing openFileFromPath: the latter derives its root from the file's own
immediate parent, which would have silently narrowed the sidebar root for
any file nested more than one level deep. If the saved file no longer
exists, the existing loadFile error handling already leaves the folder
open with nothing selected — no extra fallback logic needed for that case.

Out of scope: only the "main" window ever falls through to this restore
path; spawned viewer-N windows always get an explicit PendingOpen and are
unaffected, matching lastFolder's existing scope.
@lampietti
lampietti force-pushed the feat/restore-last-file branch from 9a04181 to 686557f Compare August 14, 2026 07:58

This branch has not been deployed

No deployments
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.

1 participant