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: retain claimed Mind assets during cleanup (#5169)

Adam Eivy2691a0230818
diff --git a/server/lib/persistentMind.js b/server/lib/persistentMind.js
index fdfd797a07..2ccc40cbc3 100644
--- a/server/lib/persistentMind.js
+++ b/server/lib/persistentMind.js
@@ -272,6 +272,7 @@ export function createDefaultPersistentMindState() {
selfWake: null,
activeTurn: null,
recentMessageIds: [],
+ recentMessageFingerprints: [],
lastCompletedTurnId: null,
lastCompletedAt: null,
nextEligibleWakeAt: null,
diff --git a/server/lib/persistentMind.test.js b/server/lib/persistentMind.test.js
index 30e48906a6..46a1cce914 100644
--- a/server/lib/persistentMind.test.js
+++ b/server/lib/persistentMind.test.js
@@ -20,6 +20,7 @@ describe('persistent mind state', () => {
status: 'disabled',
queuedMessages: [],
pendingAttachments: [],
+ recentMessageFingerprints: [],
selfWake: null,
activeTurn: null,
});
diff --git a/server/services/persistentMindSupervisor.attachments.test.js b/server/services/persistentMindSupervisor.attachments.test.js
index 33ae8db2d3..ddcc69edbd 100644
--- a/server/services/persistentMindSupervisor.attachments.test.js
+++ b/server/services/persistentMindSupervisor.attachments.test.js
@@ -207,6 +207,27 @@ describe('persistent mind image attachment lifecycle', () => {
expect(mocks.root.persistentMind.pendingAttachments).toEqual([]);
});
+ it('retains claimed metadata when a leftover marker cannot be removed', async () => {
+ const claimed = uploadRecord({ attachmentId: 'claimed-old', filename: 'mind-claimed-old.png', claimedBy: 'completed-message', expiresAt: null });
+ mocks.root.persistentMind.pendingAttachments = [claimed];
+ mocks.readdir.mockResolvedValue(['.mind-pending-claimed-old']);
+ mocks.unlink.mockImplementation(async (path) => {
+ if (path.endsWith('.mind-pending-claimed-old')) {
+ const error = new Error('marker unavailable');
+ error.code = 'EPERM';
+ throw error;
+ }
+ });
+
+ const uploaded = await supervisor.createPersistentMindAttachment({ filename: 'new.png', data: 'encoded-image' });
+
+ expect(uploaded.success).toBe(true);
+ expect(mocks.root.persistentMind.pendingAttachments).toEqual(expect.arrayContaining([
+ expect.objectContaining({ attachmentId: 'claimed-old', claimedBy: 'completed-message' }),
+ ]));
+ expect(mocks.unlink).not.toHaveBeenCalledWith('/tmp/portos-mind-attachments/mind-claimed-old.png');
+ });
+
it('deletes only unclaimed files and leaves claimed historical assets alone', async () => {
mocks.root.persistentMind.pendingAttachments = [uploadRecord()];
await expect(supervisor.deletePersistentMindAttachment('attachment-1')).resolves.toEqual({
diff --git a/server/services/persistentMindSupervisor.js b/server/services/persistentMindSupervisor.js
index 5489244b1e..71749a3ccd 100644
--- a/server/services/persistentMindSupervisor.js
+++ b/server/services/persistentMindSupervisor.js
@@ -352,7 +352,7 @@ export async function createPersistentMindAttachment({ filename, data } = {}) {
await removeUploadAfterStateFailure(saved.filePath, attachmentId);
return attachmentFailure('Stored image metadata was invalid', { code: 'INVALID_ATTACHMENT' });
}
- const result = await mutateMindState((mind) => {
+ const result = await mutateMindState(async (mind) => {
const retainedMessageIds = new Set([
...mind.recentMessageIds,
...mind.queuedMessages.map((message) => message.id),
@@ -360,10 +360,17 @@ export async function createPersistentMindAttachment({ filename, data } = {}) {
]);
// Old claimed records are metadata only once their message leaves the
// idempotency window. Keep their files and message references durable, but
- // do not let the pending-record index grow without bound.
- const pendingAttachments = mind.pendingAttachments.filter((item) => (
- !item.claimedBy || retainedMessageIds.has(item.claimedBy)
- ));
+ // do not let the pending-record index grow without bound. If removing the
+ // claim marker fails, retain the metadata: without it the marker-based
+ // orphan sweep could mistake the durable image for an unindexed upload.
+ const pendingAttachments = [];
+ for (const item of mind.pendingAttachments) {
+ if (!item.claimedBy || retainedMessageIds.has(item.claimedBy)) {
+ pendingAttachments.push(item);
+ } else if (!await removePendingAttachmentMarker(item.attachmentId)) {
+ pendingAttachments.push(item);
+ }
+ }
if (pendingAttachments.length >= PERSISTENT_MIND_LIMITS.MAX_PENDING_ATTACHMENTS) {
return {
mind,