Skip to content

The agent that grows with you

refactor(desktop): give session tombstones their own store

Brooklyn Nicholson86acfee94964
diff --git a/apps/desktop/src/app/chat/sidebar/index.tsx b/apps/desktop/src/app/chat/sidebar/index.tsx
index e6afa1247f2..41459bd2e1e 100644
--- a/apps/desktop/src/app/chat/sidebar/index.tsx
+++ b/apps/desktop/src/app/chat/sidebar/index.tsx
@@ -89,7 +89,6 @@ import {
$projectScope,
$projectTree,
$projectTreeLoading,
- $removedSessionIds,
$reposScanning,
ALL_PROJECTS,
enterProject,
@@ -127,6 +126,7 @@ import {
} from '@/store/session'
import { $sessionDotStateById, sessionStatusBucket } from '@/store/session-dot-state'
import { $unconfirmedPinWrites } from '@/store/session-pin-sync'
+import { $removedSessionIds } from '@/store/session-removal'
import { $focusedStoredSessionId, $workingSessionIds, type SplitDir } from '@/store/session-states'
import { ackAllSessionsRead } from '@/store/session-unread'
import { markSessionUnread } from '@/store/session-unread-remote'
diff --git a/apps/desktop/src/app/session/hooks/use-session-list-actions.test.tsx b/apps/desktop/src/app/session/hooks/use-session-list-actions.test.tsx
index 1bcb6bb3da0..eb7f96ee887 100644
--- a/apps/desktop/src/app/session/hooks/use-session-list-actions.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-session-list-actions.test.tsx
@@ -98,7 +98,8 @@ vi.mock('@/store/gateway', async importOriginal => ({
// the whole projects store (gateway / fs / git) into this hook's test.
const removed = vi.hoisted(() => ({ ids: new Set<string>() }))
-vi.mock('@/store/projects', () => ({
+vi.mock('@/store/session-removal', async importActual => ({
+ ...(await importActual<Record<string, unknown>>()),
$removedSessionIds: { get: () => removed.ids }
}))
diff --git a/apps/desktop/src/app/session/hooks/use-session-list-actions.ts b/apps/desktop/src/app/session/hooks/use-session-list-actions.ts
index 935fb745e62..79ed487ea2e 100644
--- a/apps/desktop/src/app/session/hooks/use-session-list-actions.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-list-actions.ts
@@ -19,7 +19,6 @@ import {
SIDEBAR_SESSIONS_PAGE_SIZE
} from '@/store/layout'
import { messagingTotalsKey, normalizeProfileKey, sidebarProfileForScope } from '@/store/profile'
-import { $removedSessionIds } from '@/store/projects'
import {
$messagingSessions,
$selectedStoredSessionId,
@@ -38,6 +37,7 @@ import {
setSessions,
setSessionsLoading
} from '@/store/session'
+import { $removedSessionIds } from '@/store/session-removal'
import { $sessionTiles, $workingSessionIds, getRecentlySettledSessionIds } from '@/store/session-states'
import { refreshCronJobs as refreshCronJobsStore } from '../../cron/cron-actions'
diff --git a/apps/desktop/src/app/settings/sessions-settings.tsx b/apps/desktop/src/app/settings/sessions-settings.tsx
index e990557e7a4..a194a6f0a68 100644
--- a/apps/desktop/src/app/settings/sessions-settings.tsx
+++ b/apps/desktop/src/app/settings/sessions-settings.tsx
@@ -17,8 +17,8 @@ import { triggerHaptic } from '@/lib/haptics'
import { Archive, ArchiveOff, FolderOpen, Loader2, Trash2 } from '@/lib/icons'
import { confirm } from '@/store/confirm'
import { notify, notifyError } from '@/store/notifications'
-import { untombstoneSessions } from '@/store/projects'
import { applyConfiguredDefaultProjectDir, ensureDefaultWorkspaceCwd, setSessions } from '@/store/session'
+import { untombstoneSessions } from '@/store/session-removal'
import { forgetSessionUnread } from '@/store/session-unread'
import type { HermesConfigRecord, SessionInfo } from '@/types/hermes'
diff --git a/apps/desktop/src/store/projects.test.ts b/apps/desktop/src/store/projects.test.ts
index 7c6f62c117d..a334d98dd58 100644
--- a/apps/desktop/src/store/projects.test.ts
+++ b/apps/desktop/src/store/projects.test.ts
@@ -12,13 +12,9 @@ import {
$projectScope,
$projectsRpcAvailable,
$projectTree,
- $removedSessionIds,
- $sessionMutationsInFlight,
$worktreeRefreshToken,
ALL_PROJECTS,
- beginSessionMutation,
createProject,
- endSessionMutation,
enterProject,
exitProjectScope,
fetchProjectSessions,
@@ -31,9 +27,15 @@ import {
refreshWorktrees,
resolveNewSessionCwd,
scanAndRecordRepos,
- startWorkInRepo,
- tombstoneSessions
+ startWorkInRepo
} from './projects'
+import {
+ $removedSessionIds,
+ $sessionMutationsInFlight,
+ beginSessionMutation,
+ endSessionMutation,
+ tombstoneSessions
+} from './session-removal'
vi.mock('@/i18n', () => ({
translateNow: (key: string) => key
diff --git a/apps/desktop/src/store/projects.ts b/apps/desktop/src/store/projects.ts
index 5fc59ab4b7a..9730ae6b082 100644
--- a/apps/desktop/src/store/projects.ts
+++ b/apps/desktop/src/store/projects.ts
@@ -30,6 +30,7 @@ import {
setSessions,
workspaceCwdForNewSession
} from '@/store/session'
+import { $removedSessionIds, $sessionMutationsInFlight } from '@/store/session-removal'
import type { ProjectInfo, ProjectsPayload } from '@/types/hermes'
// First-class, per-profile Projects (named, multi-folder workspaces). State is
@@ -65,78 +66,6 @@ function projectsStaleBackendError(): Error {
return new Error(translateNow('sidebar.projects.staleBackend'))
}
-// Client-side cache eviction (Apollo-style optimistic layer): ids the user just
-// deleted/archived. The backend tree is a snapshot that still lists them until
-// its next refresh, so the render-time overlay strips these so the tree matches
-// the live `$sessions` cache exactly — same as the flat Recents list. Pruned on
-// refresh once the server snapshot has caught up.
-export const $removedSessionIds = atom<Set<string>>(new Set())
-
-export function tombstoneSessions(ids: Array<null | string | undefined>): void {
- const next = new Set($removedSessionIds.get())
- const before = next.size
-
- for (const id of ids) {
- const trimmed = id?.trim()
-
- if (trimmed) {
- next.add(trimmed)
- }
- }
-
- if (next.size !== before) {
- $removedSessionIds.set(next)
- }
-}
-
-export function untombstoneSessions(ids: Array<null | string | undefined>): void {
- const current = $removedSessionIds.get()
-
- if (!current.size) {
- return
- }
-
- const next = new Set(current)
-
- for (const id of ids) {
- const trimmed = id?.trim()
-
- if (trimmed) {
- next.delete(trimmed)
- }
- }
-
- if (next.size !== current.size) {
- $removedSessionIds.set(next)
- }
-}
-
-// Ids whose delete/archive RPC is still in flight. Their tombstones are pinned
-// against the projects.tree prune below: a refresh whose snapshot predates the
-// mutation completing must NOT drop the tombstone, or the row flashes back until
-// the backend catches up. Keyed by id, so concurrent deletes stay independent.
-export const $sessionMutationsInFlight = atom<Set<string>>(new Set())
-
-function mutateInFlight(ids: Array<null | string | undefined>, add: boolean): void {
- const current = $sessionMutationsInFlight.get()
- const next = new Set(current)
-
- for (const id of ids) {
- const trimmed = id?.trim()
-
- if (trimmed) {
- add ? next.add(trimmed) : next.delete(trimmed)
- }
- }
-
- if (next.size !== current.size) {
- $sessionMutationsInFlight.set(next)
- }
-}
-
-export const beginSessionMutation = (ids: Array<null | string | undefined>): void => mutateInFlight(ids, true)
-export const endSessionMutation = (ids: Array<null | string | undefined>): void => mutateInFlight(ids, false)
-
// True while the disk scan is in flight (drives the "finding repos" hint).
export const $reposScanning = atom(false)
diff --git a/apps/desktop/src/store/session-removal.ts b/apps/desktop/src/store/session-removal.ts
new file mode 100644
index 00000000000..ae67b64e090
--- /dev/null
+++ b/apps/desktop/src/store/session-removal.ts
@@ -0,0 +1,95 @@
+import { atom } from 'nanostores'
+
+// Client-side cache eviction (Apollo-style optimistic layer): ids the user just
+// deleted/archived. The backend tree is a snapshot that still lists them until
+// its next refresh, so the render-time overlay strips these so the tree matches
+// the live `$sessions` cache exactly — same as the flat Recents list. Pruned on
+// refresh once the server snapshot has caught up.
+//
+// This lives beside the session stores rather than in `projects.ts` so the
+// resume path can consult it without importing the project tree (which itself
+// reads `store/session`).
+export const $removedSessionIds = atom<Set<string>>(new Set())
+
+export function tombstoneSessions(ids: Array<null | string | undefined>): void {
+ const next = new Set($removedSessionIds.get())
+ const before = next.size
+
+ for (const id of ids) {
+ const trimmed = id?.trim()
+
+ if (trimmed) {
+ next.add(trimmed)
+ }
+ }
+
+ if (next.size !== before) {
+ $removedSessionIds.set(next)
+ }
+}
+
+export function untombstoneSessions(ids: Array<null | string | undefined>): void {
+ const current = $removedSessionIds.get()
+
+ if (!current.size) {
+ return
+ }
+
+ const next = new Set(current)
+
+ for (const id of ids) {
+ const trimmed = id?.trim()
+
+ if (trimmed) {
+ next.delete(trimmed)
+ }
+ }
+
+ if (next.size !== current.size) {
+ $removedSessionIds.set(next)
+ }
+}
+
+// Ids whose delete/archive RPC is still in flight. Their tombstones are pinned
+// against the projects.tree prune: a refresh whose snapshot predates the
+// mutation completing must NOT drop the tombstone, or the row flashes back until
+// the backend catches up. Keyed by id, so concurrent deletes stay independent.
+export const $sessionMutationsInFlight = atom<Set<string>>(new Set())
+
+function mutateInFlight(ids: Array<null | string | undefined>, add: boolean): void {
+ const current = $sessionMutationsInFlight.get()
+ const next = new Set(current)
+
+ for (const id of ids) {
+ const trimmed = id?.trim()
+
+ if (trimmed) {
+ add ? next.add(trimmed) : next.delete(trimmed)
+ }
+ }
+
+ if (next.size !== current.size) {
+ $sessionMutationsInFlight.set(next)
+ }
+}
+
+export const beginSessionMutation = (ids: Array<null | string | undefined>): void => mutateInFlight(ids, true)
+export const endSessionMutation = (ids: Array<null | string | undefined>): void => mutateInFlight(ids, false)
+
+/** The session is on its way out: already tombstoned, or its delete/archive RPC
+ * is still in flight. Either way the durable row is doomed, so nothing may
+ * resume it — a resume would 404 and toast "Resume failed / Session not found"
+ * for a chat the user deliberately removed.
+ *
+ * Deletion tombstones synchronously and only untombstones if the RPC fails
+ * (which restores the row and the route), so this predicate is the single
+ * answer every resume actuator asks. */
+export function isSessionRemovalPending(sessionId: null | string | undefined): boolean {
+ const id = sessionId?.trim()
+
+ if (!id) {
+ return false
+ }
+
+ return $removedSessionIds.get().has(id) || $sessionMutationsInFlight.get().has(id)
+}