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

fix: preserve pending Mind images when verification cannot read them (#9719)

[._.]/ Adam Eivyad172b3af844
diff --git a/server/routes/cosMindRoutes.test.js b/server/routes/cosMindRoutes.test.js
index 8a00f1a1dd..da3522f002 100644
--- a/server/routes/cosMindRoutes.test.js
+++ b/server/routes/cosMindRoutes.test.js
@@ -533,6 +533,18 @@ describe('persistent mind routes', () => {
});
});
+ it('surfaces unreadable pending image storage as a retryable 503', async () => {
+ mocks.enqueuePersistentMindMessage.mockResolvedValueOnce({
+ success: false,
+ error: 'The stored image could not be read right now.',
+ code: 'ATTACHMENT_UNAVAILABLE',
+ status: 503,
+ });
+ const res = await post('/mind/messages', { id: 'message-image', images: ['attachment-1'] });
+ expect(res.status).toBe(503);
+ expect(res.body.code).toBe('ATTACHMENT_UNAVAILABLE');
+ });
+
it('validates the displayed temporary selection and requires its preset id to match', async () => {
const thinkingPreset = { id: 'deep', label: 'Deep', providerId: 'example-provider', model: 'example-model', effort: '' };
const input = { id: 'selected-message', text: 'Use this selection.', thinkingPresetId: 'deep', thinkingPreset };
diff --git a/server/services/persistentMindAttachments.js b/server/services/persistentMindAttachments.js
index b95d58b07e..fabeaf9aff 100644
--- a/server/services/persistentMindAttachments.js
+++ b/server/services/persistentMindAttachments.js
@@ -122,16 +122,36 @@ export const cleanupUnindexedPendingAttachments = async ({ knownAttachments, now
return { examined: markers.length, removed };
};
+export const ATTACHMENT_VERIFICATION = Object.freeze({
+ VALID: 'valid',
+ INVALID: 'invalid',
+ UNAVAILABLE: 'unavailable',
+});
+
+/**
+ * Classify a stored image. Only proof of absence (ENOENT / unresolvable path)
+ * or a successful read whose bytes mismatch is INVALID. Any other read error
+ * (EACCES, EIO, EBUSY, ...) says nothing about the bytes, so it is UNAVAILABLE
+ * and callers must preserve the record and file.
+ */
export const verifyStoredAttachment = async (attachment) => {
const filePath = resolveScreenshot(attachment.filename);
- if (!filePath) return false;
- const bytes = await readFile(filePath).then((value) => value, () => null);
- const detected = detectImageFormat(bytes);
- return Boolean(
- detected
- && detected.mime === attachment.mimeType
- && bytes.length === attachment.size,
+ if (!filePath) return ATTACHMENT_VERIFICATION.INVALID;
+ const read = await readFile(filePath).then(
+ (bytes) => ({ bytes }),
+ (error) => ({ error }),
);
+ if (read.error) {
+ if (read.error.code === 'ENOENT') return ATTACHMENT_VERIFICATION.INVALID;
+ console.warn(`⚠️ Persistent Mind attachment ${attachment.attachmentId} could not be read (${read.error.code || read.error.message}); preserving it`);
+ return ATTACHMENT_VERIFICATION.UNAVAILABLE;
+ }
+ const detected = detectImageFormat(read.bytes);
+ return detected
+ && detected.mime === attachment.mimeType
+ && read.bytes.length === attachment.size
+ ? ATTACHMENT_VERIFICATION.VALID
+ : ATTACHMENT_VERIFICATION.INVALID;
};
export const removeStoredAttachmentFile = async (attachment) => {
@@ -173,7 +193,16 @@ export const resolveMessageAttachments = async (mind, attachmentIds, messageId)
if (!attachment.claimedBy && (!Number.isFinite(expiresAt) || expiresAt <= Date.now())) {
return { error: attachmentFailure('Persistent mind attachment has expired', { code: 'ATTACHMENT_EXPIRED' }) };
}
- if (!await verifyStoredAttachment(attachment)) {
+ const verification = await verifyStoredAttachment(attachment);
+ if (verification === ATTACHMENT_VERIFICATION.UNAVAILABLE) {
+ return {
+ error: attachmentFailure(
+ 'The stored image could not be read right now. Your upload is kept; retry sending once storage access recovers.',
+ { code: 'ATTACHMENT_UNAVAILABLE', status: 503 },
+ ),
+ };
+ }
+ if (verification !== ATTACHMENT_VERIFICATION.VALID) {
return { error: attachmentFailure('Persistent mind attachment is missing or invalid', { code: 'INVALID_ATTACHMENT' }) };
}
attachments.push(attachment);
@@ -212,8 +241,9 @@ export async function cleanupPersistentMindAttachments({ now = Date.now() } = {}
examined += 1;
const expiresAt = attachment.expiresAt ? Date.parse(attachment.expiresAt) : null;
const expired = !attachment.claimedBy && Number.isFinite(expiresAt) && expiresAt <= now;
- const valid = expired ? false : await verifyStoredAttachment(attachment);
- if (!expired && valid) {
+ const verification = expired ? ATTACHMENT_VERIFICATION.INVALID : await verifyStoredAttachment(attachment);
+ // Unreadable is not invalid: keep the record and bytes for a later pass.
+ if (verification !== ATTACHMENT_VERIFICATION.INVALID) {
pendingAttachments.push(attachment);
continue;
}
diff --git a/server/services/persistentMindSupervisor.attachments.test.js b/server/services/persistentMindSupervisor.attachments.test.js
index 0c68df5c73..43b7bbdfa0 100644
--- a/server/services/persistentMindSupervisor.attachments.test.js
+++ b/server/services/persistentMindSupervisor.attachments.test.js
@@ -331,6 +331,43 @@ describe('persistent mind image attachment lifecycle', () => {
expect(mocks.root.persistentMind.pendingAttachments).toEqual([]);
});
+ it.each(['EACCES', 'EIO', 'EBUSY'])('preserves a valid pending image through cleanup and admission while reads fail with %s', async (code) => {
+ mocks.root.persistentMind.pendingAttachments = [
+ uploadRecord(),
+ uploadRecord({ attachmentId: 'expired', filename: 'mind-expired.png', expiresAt: '2026-08-26T00:00:00.000Z' }),
+ ];
+ mocks.readFile.mockRejectedValue(Object.assign(new Error('storage unavailable'), { code }));
+
+ await expect(supervisor.cleanupPersistentMindAttachments({ now: Date.parse('2026-08-27T00:00:00.000Z') }))
+ .resolves.toMatchObject({ success: true, removed: 1, examined: 2 });
+ expect(mocks.unlink).toHaveBeenCalledTimes(1);
+ expect(mocks.unlink.mock.calls[0][0]).toContain('mind-expired.png');
+ expect(mocks.root.persistentMind.pendingAttachments).toMatchObject([{ attachmentId: 'attachment-1' }]);
+
+ // Unrelated text admission is not blocked by the unreadable image.
+ expect(await supervisor.enqueuePersistentMindMessage({ id: 'text-only', text: 'Hello.' })).toMatchObject({ success: true });
+
+ const unavailable = await supervisor.enqueuePersistentMindMessage({ id: 'message-1', text: 'Look.', images: ['attachment-1'] });
+ expect(unavailable).toMatchObject({ success: false, code: 'ATTACHMENT_UNAVAILABLE', status: 503 });
+ expect(unavailable.error).not.toMatch(/missing|invalid|corrupt/i);
+ expect(mocks.root.persistentMind.pendingAttachments).toMatchObject([{ attachmentId: 'attachment-1' }]);
+ expect(mocks.root.persistentMind.pendingAttachments[0].claimedBy).toBeUndefined();
+
+ // Recovery: same attachment id is admitted without a new upload.
+ mocks.readFile.mockResolvedValue(PNG);
+ expect(await supervisor.enqueuePersistentMindMessage({ id: 'message-1', text: 'Look.', images: ['attachment-1'] }))
+ .toMatchObject({ success: true });
+ expect(mocks.root.persistentMind.pendingAttachments).toMatchObject([{ attachmentId: 'attachment-1', claimedBy: 'message-1' }]);
+ });
+
+ it('still removes a pending image whose file is definitively missing (ENOENT)', async () => {
+ mocks.root.persistentMind.pendingAttachments = [uploadRecord()];
+ mocks.readFile.mockRejectedValue(Object.assign(new Error('gone'), { code: 'ENOENT' }));
+ await expect(supervisor.cleanupPersistentMindAttachments())
+ .resolves.toMatchObject({ success: true, removed: 1, examined: 1 });
+ expect(mocks.root.persistentMind.pendingAttachments).toEqual([]);
+ });
+
it('rejects a changed retry and a missing or invalid image before queue mutation', async () => {
mocks.root.persistentMind.pendingAttachments = [uploadRecord()];
const first = await supervisor.enqueuePersistentMindMessage({ id: 'message-1', text: 'Original caption.', images: ['attachment-1'] });