Repository navigation
Description
Summary
On Windows the desktop app can fail to persist app-state.json (atomic rename returns EPERM) without any user-visible signal, and a turn can be logged as starting turn without ever reaching turn running / turn resolved — in the one case observed, the same message payload was then re-submitted into a newly created thread ~42s later.
Two related symptoms, same file:
- Silent state-write failure.
app-state.jsonis written via temp file + rename. On Windows that rename can fail while the target is held, and the app only logs it. - A turn that never starts.
[renderer] [queue] send: starting turnis emitted for a thread, but no[send] turn runningfollows, and the content appears in a different, brand-new thread.
Expected Behavior
- If the atomic rename of
app-state.jsonfails, the app retries (and/or surfaces a visible warning). State changes should not be dropped silently. - Every
send: starting turnshould be followed by either[send] turn runningor an explicit error for the same threadId. A message should never be silently re-routed into a different thread.
Actual Behavior
1. EPERM on state write — 3 consecutive failures, no retry, no user-visible error
%APPDATA%\Command Code\logs\main.log:
[2026-11-20 02:30:46.165] [error] [store] setAppState failed Error: EPERM: operation not permitted, rename 'C:\Users\<user>\AppData\Roaming\Command Code\app-state.json.tmp-5127445163b9bebc' -> 'C:\Users\<user>\AppData\Roaming\Command Code\app-state.json'
[2026-11-20 02:30:47.198] [error] [store] setAppState failed Error: EPERM: operation not permitted, rename 'C:\Users\<user>\AppData\Roaming\Command Code\app-state.json.tmp-512744619678b1f6' -> 'C:\Users\<user>\AppData\Roaming\Command Code\app-state.json'
[2026-11-20 02:30:48.222] [error] [store] setAppState failed Error: EPERM: operation not permitted, rename 'C:\Users\<user>\AppData\Roaming\Command Code\app-state.json.tmp-5127447220f58bf3' -> 'C:\Users\<user>\AppData\Roaming\Command Code\app-state.json'
All three fail, ~1s apart, with no backoff and no further attempt logged. No .tmp-* files were left behind afterwards, so the temp files were cleaned up — the write itself was simply lost. No UI indication, so state loss is invisible.
2. Two launches within 41s, then the EPERM storm
[2026-11-20 02:29:51.095] [info] Command Code quitting
[2026-11-20 02:29:51.491] [info] Command Code starting — v0.1.63 (packaged: true) <- no [startup] line, no [thread-index] ready
[2026-11-20 02:30:32.420] [info] Command Code starting — v0.1.63 (packaged: true) <- full startup completes
...
[2026-11-20 02:30:46.165] [error] [store] setAppState failed ... EPERM
The 02:29:51 launch logs starting but never reaches main.ready / thread-index ready. The EPERM failures begin 14s after the second launch. A second writer holding app-state.json would produce exactly this, so this looks like an instance/ownership gap rather than a corrupt file.
3. A turn that starts but never runs, then is replayed into a new thread
Thread 0958b07a-… (project E:\gitreps\chizhik-menu):
[2026-11-24 22:56:03.921] [renderer] [queue] send: starting turn {"threadId":"0958b07a-…","chars":6,"images":0}
[2026-11-24 22:56:03.933] [send] turn running {"sessionId":"0958b07a-…","chars":6,"images":0}
[2026-11-24 22:56:30.610] [send] turn resolved {"sessionId":"0958b07a-…","stopReason":"end_turn","pendingSteers":[]}
[2026-11-24 23:22:40.114] [renderer] [queue] send: starting turn {"threadId":"0958b07a-…","chars":831,"images":0}
<- no "turn running" for 0958b07a, ever
[2026-11-24 23:23:22.734] [renderer] [queue] send: starting turn {"threadId":"1594e4f1-…","chars":831,"images":0}
[2026-11-24 23:23:23.055] [send] turn running {"sessionId":"1594e4f1-…","chars":831,"images":0}
Token-for-token identical payload (chars: 831) moved from an existing thread to a thread created 42s later (1594e4f1-…, title "Grocery Dashboard MVP", same project). 0958b07a is left with an unbalanced turn: overall the log has 85 starting turn / 84 turn running / 83 turn resolved.
Steps to reproduce the issue
State-write failure (intermittent, observed once in ~200 launches):
- Launch the Command Code desktop app on Windows.
- Launch it again while the first instance is starting or shutting down (e.g. quit + immediately relaunch, or double-click the shortcut twice).
- Use the app so it writes state; check
main.logfor[store] setAppState failedandEPERM.
Dropped turn (observed once):
- Have an existing chat open in a project.
- Send a long prompt (~800+ chars) while the app has just restarted.
- Observe in
main.logthatsend: starting turnfor the original threadId has no matchingturn running, and the same payload is submitted under a different, newly created threadId.
Command Code Version
0.1.63 (desktop, packaged: true; ProductVersion 0.1.63.0)
Operating System
Windows
Terminal/IDE
Command Code desktop app (Electron), launched from the Start Menu shortcut — not a terminal session.
Shell
N/A (desktop app)
Session file (optional)
Redacted excerpts from %APPDATA%\Command Code\logs\main.log are included above. I can attach the full log if you want it.
Fix prompt (optional)
In the desktop main process (out/main/index.js), setAppState performs temp-file + rename into app-state.json and only logs on failure (M.error("[store] setAppState failed", t)), with no retry. On Windows, fs.rename over an existing file can fail with EPERM when the destination is momentarily held (another instance, AV scanner, or an open handle). Fix: retry the rename with bounded backoff (e.g. 3–5 attempts, 50/100/200 ms), and if it still fails, keep the temp file and surface a visible, non-blocking warning instead of dropping the write silently.
For the turn gap: [renderer] [queue] send: starting turn is logged before the harness accepts the run, but there is no timeout or error path that resolves a starting turn that never becomes turn running. Fix: if no acknowledgement arrives for a started turn within N seconds, emit an explicit failure for that threadId and do not let the composer re-submit the same payload into a different thread.
Verify by: launching two instances in quick succession and confirming either a clean single-instance handoff or successful retried writes (no EPERM without a follow-up success); and by parsing main.log and asserting starting turn count == turn running count == turn resolved count per threadId.
Additional context
Full window: 2026-11-19 → 2026-11-25, ~200 app launches, 1 occurrence of the EPERM cluster and 1 orphaned turn out of 85 turns. Related but distinct: #925 (Windows main-process crash on output-heavy turns).
Activity