nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] With sessions.autoSave off, every start deletes the plan, tasks and walkthrough of saved sessions

Open diyaavirmani opened this issue 1 month ago • 6 comments

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):

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.

  1. Run export NANOCODER_DATA_DIR=$(mktemp -d). Then, with default settings (autoSave on), start nanocoder in any trusted project, send one prompt and exit. Note the session id in $NANOCODER_DATA_DIR/sessions/sessions.json.
  2. 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>
    
  3. Turn autoSave off in nanocoder-preferences.json ({"nanocoder":{"sessions":{"autoSave":false}}}). Start nanocoder again, either interactively or with nanocoder run "hi" (without --plain).
  4. Run ls "$NANOCODER_DATA_DIR/artifacts/". The session's folder is gone, but the session is still listed in sessions.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 example await this.initialize() before reading the index), or call initialize() before the sweep in useAppInitialization.
  • 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:

  1. 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.
  2. The startup path with autoSave: false keeps a saved session's artifacts.

@addyCooks I'd like to work on this. Could you assign it to me if that works for you?

diyaavirmani avatar Oct 11 '26 13:10 diyaavirmani

I'll take this one: resolve the sessions directory before the startup artifact sweep reads the index, with a regression test.

mayuriphad avatar Oct 12 '26 05:10 mayuriphad

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 :)

diyaavirmani avatar Oct 12 '26 05:10 diyaavirmani

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.

addyCooks avatar Oct 12 '26 06:10 addyCooks

@diyaavirmani I've already assigned you a few issues. Ping me once those PRs are reviewed and I'll assign this one too.

addyCooks avatar Oct 12 '26 06:10 addyCooks

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 :)

diyaavirmani avatar Oct 12 '26 13:10 diyaavirmani

Hey @addyCooks . All PR's are reviewed and merged , kindly assign this one now. :)

diyaavirmani avatar Oct 14 '26 07:10 diyaavirmani