[Bug] With sessions.autoSave off, every start deletes the plan, tasks and walkthrough of saved sessions
Description
With nanocoder.sessions.autoSave set to false, every interactive start deletes the artifacts/<session id>/ folder (implementation plan, task list and walkthrough) of every saved session whose artifacts are more than a day old. The same happens with nanocoder run without --plain. Those sessions are still in sessions.json and can still be resumed, but they come back without their Plan, Tasks and Walkthrough.
The docs say the startup sweep only removes directories of sessions that were never saved (session-management.md, Session Artifacts).
Root cause (links pinned to 8c50d68d):
useAppInitialization.tsx#L624firessessionManager.cleanupOrphanedArtifacts()without initializing the session manager first.initialize()is the only thing that resolves the sessions directory. With autoSave off,useSessionAutosavenever calls it.- So
readIndex()has no path. It falls through torebuildIndex(), which fails and returns[]. cleanupOrphanedArtifacts()then treats every artifact folder as orphaned and deletes the ones older than a day.
With autoSave on (the default), the same call is only safe because useSessionAutosave's effect happens to run initialize() first.
From reading the code, sessions saved through ACP / the VS Code extension are swept the same way, because they are saved regardless of autoSave. I only reproduced it with sessions saved by the CLI.
Environment
- OS: macOS 26.6.2
- Node version: v22.23.3
- Nanocoder version: 1.31.0 (published npm package), and current
main(8c50d68d) - Provider: any. I reproduced it with a local OpenAI-compatible endpoint; the deletion happens at startup, before any model request.
- Model: any (not model-dependent)
Steps to Reproduce
Use a throwaway data dir so real sessions aren't touched.
- Run
export NANOCODER_DATA_DIR=$(mktemp -d). Then, with default settings (autoSave on), startnanocoderin any trusted project, send one prompt and exit. Note the session id in$NANOCODER_DATA_DIR/sessions/sessions.json. - Give that session an artifact older than a day, as plan mode would:
SID=<session id from step 1> mkdir -p "$NANOCODER_DATA_DIR/artifacts/$SID" echo "# plan" > "$NANOCODER_DATA_DIR/artifacts/$SID/implementation_plan.md" touch -t "$(date -v-3d +%Y%m%d%H%M)" "$NANOCODER_DATA_DIR/artifacts/$SID/implementation_plan.md" "$NANOCODER_DATA_DIR/artifacts/$SID" # Linux: touch -d '3 days ago' <same two paths> - Turn autoSave off in
nanocoder-preferences.json({"nanocoder":{"sessions":{"autoSave":false}}}). Startnanocoderagain, either interactively or withnanocoder run "hi"(without--plain). - Run
ls "$NANOCODER_DATA_DIR/artifacts/". The session's folder is gone, but the session is still listed insessions.json.
Expected Behavior
The saved session keeps its artifacts. Only folders of sessions that were never saved, and that are older than a day, are removed at startup (as the Session Artifacts section of the docs describes).
Actual Behavior
The saved session's artifacts folder is deleted at startup.
With autoSave left on, the same steps keep the folder. A folder that belongs to no session is deleted in both cases, as documented.
Logs/Screenshots
Two fresh data dirs, same steps, only autoSave differs on the second start:
[autoSave=true ] saved session 218fe470…: in sessions.json=yes artifacts=KEPT | unowned artifact dir=DELETED
[autoSave=false] saved session e77cb8d7…: in sessions.json=yes artifacts=DELETED | unowned artifact dir=DELETED
Same result on the published 1.31.0 and on main @ 8c50d68d, on every run.
Additional Context
- [x] I have searched existing issues to ensure this is not a duplicate (open and closed issues/PRs for
cleanupOrphanedArtifacts, autoSave + artifacts, deleted plan/walkthrough, orphaned artifact sweep) - [x] I can reproduce this issue consistently
- [x] This issue occurs with the latest version of nanocoder
Proposed fix:
- Make
cleanupOrphanedArtifacts()resolve the sessions directory itself (for exampleawait this.initialize()before reading the index), or callinitialize()before the sweep inuseAppInitialization. - Make the sweep fail safe: if the index can't be read, skip the sweep instead of treating the set of known sessions as empty.
Regression tests:
- Calling
cleanupOrphanedArtifacts()on a fresh, uninitialized manager keeps an indexed session's old artifacts and still removes an old folder that belongs to no session. - The startup path with
autoSave: falsekeeps a saved session's artifacts.
@addyCooks I'd like to work on this. Could you assign it to me if that works for you?
I'll take this one: resolve the sessions directory before the startup artifact sweep reads the index, with a regression test.
I'll take this one: resolve the sessions directory before the startup artifact sweep reads the index, with a regression test.
Thanks @mayuriphad , but I reproduced this big and I plan to raise a PR as well as soon as @addyCooks reviews and assigns it to me :)
Hey @mayuriphad, thanks for the PR! but @diyaavirmani asked for this one first. Please wait for assignment before opening a PR next time, and feel free to grab another unassigned issue.
@diyaavirmani I've already assigned you a few issues. Ping me once those PRs are reviewed and I'll assign this one too.
7. @diyaavirmani I've already assigned you a few issues. Ping me once those PRs are reviewed and I'll assign this one too.
Alright :)
Hey @addyCooks . All PR's are reviewed and merged , kindly assign this one now. :)