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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: retire a temporary Persistent Mind session on every abandonment path (#6273)
[._.]/ Adam Eivyae4adaca402d
diff --git a/server/routes/cosMindRoutes.js b/server/routes/cosMindRoutes.jsindex 0842b33458..502c5ba171 100644--- a/server/routes/cosMindRoutes.js+++ b/server/routes/cosMindRoutes.js@@ -82,8 +82,14 @@ const messageSchema = z.object({text: messageText,images: z.array(imageReference).max(PERSISTENT_MIND_LIMITS.MAX_MESSAGE_IMAGES).optional(),// "Send with another model": one saved preset, for this message's single turn- // only. Absent means the mind's unchanged default route.- thinkingPresetId: persistentMindThinkingPresetSchema.shape.id.optional(),+ // only. Absent, empty, and null all mean the same thing here — the mind's+ // unchanged default route — so a composer that clears its picker does not+ // have to omit the key to say "no override".+ thinkingPresetId: z.union([+ persistentMindThinkingPresetSchema.shape.id,+ z.literal(''),+ z.null(),+ ]).optional(),}).strict().superRefine((value, ctx) => {const images = Array.isArray(value.images) ? value.images : [];const ids = images.map((image) => typeof image === 'string' ? image : image.attachmentId);diff --git a/server/services/persistentMindSupervisor.js b/server/services/persistentMindSupervisor.jsindex 3f6fe08c69..6c861778f7 100644--- a/server/services/persistentMindSupervisor.js+++ b/server/services/persistentMindSupervisor.js@@ -522,6 +522,21 @@ function quietSelfWake(turnId, quietPeriodMs = PERSISTENT_MIND_LIMITS.MAX_QUIET_};}+/**+ * Hand a claimed wake back to durable state when its turn is abandoned.+ *+ * An interrupted temporary thinking session is an uncertain consumed attempt:+ * the abort races whatever the provider already started, and no durable record+ * can say whether that call was billed. Every such wake is retired rather than+ * requeued, so resuming it stays an explicit user decision; an ordinary message+ * or self-wake keeps its free automatic retry.+ */+const releaseClaimedWake = (mind, wake) => (+ persistentMindWakeConsumesAttempt(wake)+ ? holdPersistentMindWake(mind, wake)+ : requeuePersistentMindWake(mind, wake)+);+async function interruptActiveTurn(reason, status, { retry = false, expectedTurnId = null } = {}) {const result = await mutateMindState((mind) => {if (expectedTurnId && mind.activeTurn?.id !== expectedTurnId) {@@ -529,15 +544,7 @@ async function interruptActiveTurn(reason, status, { retry = false, expectedTurn}const interrupted = Boolean(mind.activeTurn);let next = mind;- if (mind.activeTurn) {- // An interrupted temporary thinking session is an uncertain consumed- // attempt: the abort races whatever the provider already started, and no- // durable record can say whether that call was billed. Retire it instead- // of requeueing, so resuming it stays an explicit user decision.- next = persistentMindWakeConsumesAttempt(mind.activeTurn.wake)- ? holdPersistentMindWake(next, mind.activeTurn.wake)- : requeuePersistentMindWake(next, mind.activeTurn.wake);- }+ if (mind.activeTurn) next = releaseClaimedWake(next, mind.activeTurn.wake);const failureCount = retry ? next.failureCount + 1 : next.failureCount;return {mind: {@@ -580,6 +587,8 @@ async function interruptActiveTurn(reason, status, { retry = false, expectedTurnasync function parkActiveTurn(turnId, reason, status = 'waiting', { retryAt = null, consumedAttempt = false } = {}) {const result = await mutateMindState((mind) => {if (mind.activeTurn?.id !== turnId) return { mind, value: false };+ // A refusal decided before the provider span opened spent nothing, so it+ // requeues even for a temporary session.const held = consumedAttempt && persistentMindWakeConsumesAttempt(mind.activeTurn.wake);const next = held? holdPersistentMindWake(mind, mind.activeTurn.wake)@@ -1141,7 +1150,7 @@ export async function setPersistentMindEnabled(enabled) {const changedToDisabled = !enabled && (mind.enabled || Boolean(mind.activeTurn));const interruptedTurnId = !enabled ? mind.activeTurn?.id || null : null;let next = mind;- if (!enabled && mind.activeTurn) next = requeuePersistentMindWake(next, mind.activeTurn.wake);+ if (!enabled && mind.activeTurn) next = releaseClaimedWake(next, mind.activeTurn.wake);return {mind: {...next,@@ -1239,7 +1248,7 @@ export async function stopPersistentMind({ waitForTurn = false } = {}) {const wasStarted = mind.started;const interruptedTurnId = mind.activeTurn?.id || null;let next = mind;- if (mind.activeTurn) next = requeuePersistentMindWake(next, mind.activeTurn.wake);+ if (mind.activeTurn) next = releaseClaimedWake(next, mind.activeTurn.wake);return {mind: {...next,@@ -1540,7 +1549,7 @@ export async function initializePersistentMindSupervisor() {let next = mind;const orphanedTurnId = mind.activeTurn?.id || null;if (mind.activeTurn) {- next = requeuePersistentMindWake(next, mind.activeTurn.wake);+ next = releaseClaimedWake(next, mind.activeTurn.wake);const failureCount = next.failureCount + 1;next = {...next,diff --git a/server/services/persistentMindSupervisor.test.js b/server/services/persistentMindSupervisor.test.jsindex cb548608ec..9cb6074515 100644--- a/server/services/persistentMindSupervisor.test.js+++ b/server/services/persistentMindSupervisor.test.js@@ -859,4 +859,56 @@ describe('persistent mind supervisor', () => {await supervisor.drainPersistentMind();expect(run).toHaveBeenCalledTimes(1);});++ it('retires, never replays, a temporary session abandoned by a restart, a stop, or a disable', async () => {+ withDeepPreset();+ const temporaryTurn = (turnId, messageId) => ({+ id: turnId,+ wake: {+ kind: 'message',+ message: { id: messageId, text: 'Deep pass please.', thinkingPresetId: 'deep', createdAt: new Date().toISOString() },+ },+ startedAt: new Date().toISOString(),+ heartbeatAt: new Date().toISOString(),+ });+ const startedWith = (turn) => {+ mock.root.persistentMind = {+ ...createDefaultPersistentMindState(),+ enabled: true,+ started: true,+ status: 'thinking',+ activeTurn: turn,+ };+ };+ await supervisor.registerPersistentMindTurnAdapter({ prepare: echoProfileAdapter(), run: vi.fn() });++ // A hard crash leaves started:true on disk, so boot recovery — not the+ // state normalizer — owns this one.+ startedWith(temporaryTurn('mind-turn-crash', 'paid-crash'));+ await supervisor.initializePersistentMindSupervisor();+ expect(mock.root.persistentMind.queuedMessages).toEqual([]);+ expect(mock.root.persistentMind.recentMessageIds).toEqual(['paid-crash']);++ startedWith(temporaryTurn('mind-turn-stop', 'paid-stop'));+ await supervisor.stopPersistentMind();+ expect(mock.root.persistentMind.queuedMessages).toEqual([]);+ expect(mock.root.persistentMind.recentMessageIds).toEqual(['paid-stop']);++ startedWith(temporaryTurn('mind-turn-disable', 'paid-disable'));+ await supervisor.setPersistentMindEnabled(false);+ expect(mock.root.persistentMind.queuedMessages).toEqual([]);+ expect(mock.root.persistentMind.recentMessageIds).toEqual(['paid-disable']);++ // An ordinary message keeps its free automatic recovery on every path.+ const ordinaryTurn = {+ id: 'mind-turn-plain',+ wake: { kind: 'message', message: { id: 'plain-1', text: 'Ordinary.', createdAt: new Date().toISOString() } },+ startedAt: new Date().toISOString(),+ heartbeatAt: new Date().toISOString(),+ };+ startedWith(ordinaryTurn);+ await supervisor.stopPersistentMind();+ expect(mock.root.persistentMind.queuedMessages.map((item) => item.id)).toEqual(['plain-1']);+ expect(mock.root.persistentMind.recentMessageIds).toEqual([]);+ });});