Skip to content

armgit / Orkas

Public Imported from GitHub
0

Orkas is an open-source (MIT), local-first AI team for your desktop: a Commander turns your goal into to-dos and coordinates 16 built-in specialist agents plus Claude Code, Codex, OpenCode, OpenClaw and Hermes, with projects, scheduled automations and connectors. Free app for macOS & Windows; built-in models or your own keys.

Remove empty chat_attachments/<cid>/ dirs after attachment delete or upload-write failure

wyt9771012e9639
diff --git a/src/main/features/chat_attachments.ts b/src/main/features/chat_attachments.ts
index 16ebd00b..8a31ab3b 100644
--- a/src/main/features/chat_attachments.ts
+++ b/src/main/features/chat_attachments.ts
@@ -178,7 +178,16 @@ export async function uploadAttachment(
const target = uniqueTarget(dir, safeName);
const finalName = path.basename(target);
try { fs.writeFileSync(target, buf); }
- catch (err) { return { ok: false, error: (err as Error).message }; }
+ catch (err) {
+ // Roll back the mkdir from `ensureDir` if the directory ended up empty
+ // (i.e., this was the first attachment for the cid and the write failed
+ // before producing anything). Best-effort: a non-empty dir means a
+ // previous successful upload owns it and must not be touched.
+ try {
+ if (fs.readdirSync(dir).length === 0) fs.rmdirSync(dir);
+ } catch { /* best-effort */ }
+ return { ok: false, error: (err as Error).message };
+ }
// No upload-time preprocessing: extract / grayscale happen lazily in
// features/file_indexer when the model's tools first touch this file.
@@ -278,6 +287,12 @@ export function deleteAttachment(userId: string, cid: string, name: string): Res
// Drop the lazy file_cache entry for this source (if any was materialised).
try { invalidateFileCache(userId, p); }
catch (err) { log.warn(`invalidate cache ${p}: ${(err as Error).message}`); }
+ // Remove the per-cid directory if this was the last attachment — leaving
+ // empty `chat_attachments/<cid>/` shells around violates the "no payload,
+ // no directory" expectation, and the user has no UI to clean them up.
+ try {
+ if (fs.readdirSync(dir).length === 0) fs.rmdirSync(dir);
+ } catch { /* best-effort */ }
return { ok: true };
}