fix: repair stale conversation working directories#3318
Conversation
Letta Code (agent-c2adbf5c-8419-4211-8cd8-3740db164974) Prune deleted or non-directory conversation cwd entries before listener exports cwd maps so clients do not reuse stale worktree paths. 👾 Generated with [Letta Code](https://letta.com) Co-Authored-By: Letta Code <noreply@letta.com>
cpacker
left a comment
There was a problem hiding this comment.
Requesting changes because the exported map is clean only in memory; startup pruning is not durable.
P1: stale rows filtered during load are never persisted. loadRemoteSettings() drops missing paths at lines 60-68 before ListenerRuntime.workingDirectoryByConversation is built. Later, pruneStaleConversationWorkingDirectories() sees no stale entries, returns false, and never calls persistCwdMap(). The dead row remains in remote-settings.json; if that filesystem path is recreated and the listener restarts, the stale conversation mapping resurrects.
The same validation uses existsSync, so a regular file is accepted even though a cwd must be a directory.
Please make startup validation durable (and directory-aware), then add a disk/restart regression: write a stale persisted row, load the runtime, verify the persisted file is repaired, recreate the old path, reset/reload, and verify the mapping does not return. The current tests construct stale entries directly in the already-loaded runtime map, so they do not exercise this producer path.
cpacker
left a comment
There was a problem hiding this comment.
Follow-up 4260d4e4 resolves the requested changes. Startup validation now requires a real directory and synchronously writes the repaired remote-settings.json before returning. The new filesystem-backed regression begins from persisted stale state, verifies writeback, recreates the deleted path, resets the cache, and proves the old scope does not resurrect.
Verified locally: focused listener suites 165/165 and bun run check 12/12.
Summary
Companion Desktop live-state fix: https://github.com/letta-ai/letta-cloud/pull/12985
Fixes https://linear.app/letta/issue/LET-9647/got-an-enoent-users-should-never-see-these-unless-its-a-true
Also repairs https://linear.app/letta/issue/LET-9597/repair-conversation-cwd-after-deleting-active-worktree
Root cause
Deleted worktree paths survived in listener and Desktop cwd maps. Export paths could replay those stale values, handleCwdChange surfaced realpath failures verbatim, and unordered settings writes could restore a mapping after repair. Multiple listener processes shared one settings file but previously published process-local full snapshots, allowing one listener to overwrite another's newer map.
Test plan
Risk / rollback