Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

remove the orphaned image when a shell session dies mid-write

[._.]/ Adam Eivy24b3b47b3fc8
diff --git a/client/src/hooks/useShellSession.js b/client/src/hooks/useShellSession.js
index 0ddd0b398c..3ede874f54 100644
--- a/client/src/hooks/useShellSession.js
+++ b/client/src/hooks/useShellSession.js
@@ -177,6 +177,9 @@ export function useShellSession({ isFullscreen } = {}) {
toast.error('Session is still attaching — try again in a moment');
return false;
}
+ // Capture the target BEFORE the read: a big photo takes a moment to encode, and
+ // the send belongs to the session the user was looking at when they hit Send —
+ // not to whichever one happens to be active by the time the bytes are ready.
const sessionId = sessionIdRef.current;
const data = await readFileAsBase64(file).catch(() => null);
if (!data) {
diff --git a/server/routes/shell.test.js b/server/routes/shell.test.js
index 448203cec1..9b13cac79f 100644
--- a/server/routes/shell.test.js
+++ b/server/routes/shell.test.js
@@ -93,6 +93,16 @@ describe('POST /api/shell/sessions/:sessionId/image', () => {
expect(pasteToSession).not.toHaveBeenCalled();
});
+ // Nothing will ever read the file, and this bucket is shared with screenshot
+ // uploads — an orphan here is indistinguishable from a real one.
+ it('404s and removes the written file when the session dies mid-write', async () => {
+ vi.mocked(pasteToSession).mockReturnValue(false);
+ const before = readdirSync(PATHS.screenshots).length;
+ const res = await post({ data: pngBase64, filename: 'photo.png' });
+ expect(res.status).toBe(404);
+ expect(readdirSync(PATHS.screenshots).length - before).toBe(0);
+ });
+
it('rejects bytes that are not a supported image', async () => {
const res = await post({ data: Buffer.from('not an image at all').toString('base64'), filename: 'photo.png' });
expect(res.status).toBe(400);
diff --git a/server/services/shellImageDrop.js b/server/services/shellImageDrop.js
index eed9698cdf..665bead3ea 100644
--- a/server/services/shellImageDrop.js
+++ b/server/services/shellImageDrop.js
@@ -17,6 +17,7 @@
* and over, and a second drop must not overwrite bytes the agent hasn't read yet.
*/
+import { unlink } from 'fs/promises';
import { PATHS, saveImageUpload } from '../lib/fileUtils.js';
import { MAX_SCREENSHOT_BYTES } from '../lib/uploadLimits.js';
import { ServerError } from '../lib/errorHandler.js';
@@ -70,8 +71,12 @@ export async function dropImageIntoShellSession({ sessionId, filename, data, mes
data,
}, { maxBytes: MAX_SCREENSHOT_BYTES });
- // Re-check liveness: the PTY can exit while the bytes are being written.
+ // Re-check liveness: the PTY can exit while the bytes are being written. Drop
+ // the file we just wrote — nothing will ever read it, and this bucket is shared
+ // with the screenshot uploads, so an orphan here is indistinguishable from a
+ // real one. Best-effort: a failed unlink must not mask the 404.
if (!pasteToSession(sessionId, buildImageDropText(saved.filePath, message), { label: 'image drop' })) {
+ await unlink(saved.filePath).catch(() => {});
throw new ServerError('Session not found', { status: 404, code: 'NOT_FOUND' });
}