diff --git a/src/servers/api/chat/websocket.ts b/src/servers/api/chat/websocket.ts index 41b80af2..a0f2a602 100644 --- a/src/servers/api/chat/websocket.ts +++ b/src/servers/api/chat/websocket.ts @@ -39,7 +39,7 @@ import { import { resolveHomeDir } from '@@/user-home'; import { renameClaudeSession, type ChatIdentity } from './claude-sessions'; import { claudeLoginState } from '@@/os-user-claude'; -import { mkdirSync } from 'node:fs'; +import { mkdirSync, existsSync } from 'node:fs'; import { logger } from './logger'; import { isSuperAdmin } from '@@/super-admin'; import { snapshotClaudeUsage } from './usage-store'; @@ -201,8 +201,25 @@ async function resolveChatCwd( `[command-hq] no commander row for ${msg.commanderId !== undefined ? `id ${msg.commanderId}` : `session ${msg.resumeSessionId ?? '(none given)'}`} — falling back to the account home`, ); } - const dir = row ? getCommanderDir(email, row.dirName) : home; - mkdirSync(dir, { recursive: true }); + if (!row) return home; + + /* + The directory must already exist. `POST /command-hq/commanders` creates it with its `CLAUDE.md` + before any turn spawns, so a missing one means the commander was DELETED — and a late turn from the + conversation that outlived it would otherwise recreate it here. + + That happened: an empty `commanders/46` reappeared after a clean delete, from one in-flight turn + calling this. An empty directory is worse than none, because it is a commander with no CLAUDE.md + that knows nothing about itself, and it shows up in the file browser as something that still exists. + + So: no mkdir. Fall back to the home, and say so — the same degradation the missing-row branch above + reports, for the same reason. + */ + const dir = getCommanderDir(email, row.dirName); + if (!existsSync(dir)) { + console.warn(`[command-hq] ${row.dirName}: directory is gone — deleted mid-turn? Falling back to the home.`); + return home; + } return dir; } if (msg.context === 'email') return resolveEmailCwd(email, msg.contextId); diff --git a/src/workspaces/officerdev/src/apps/CommandHQ/CommandHqDetail.tsx b/src/workspaces/officerdev/src/apps/CommandHQ/CommandHqDetail.tsx index 8ebd019f..6dd139ed 100644 --- a/src/workspaces/officerdev/src/apps/CommandHQ/CommandHqDetail.tsx +++ b/src/workspaces/officerdev/src/apps/CommandHQ/CommandHqDetail.tsx @@ -676,7 +676,14 @@ function ResumedCommander({ sessionId, resume }: { sessionId: string; resume: Re