Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
Preserve draft terminal state during snapshot cleanup
Julius Marmingeb9e529e0b56b
diff --git a/apps/web/src/lib/terminalStateCleanup.test.ts b/apps/web/src/lib/terminalStateCleanup.test.tsnew file mode 100644index 000000000..8bc3c3730--- /dev/null+++ b/apps/web/src/lib/terminalStateCleanup.test.ts@@ -0,0 +1,32 @@+import { ThreadId } from "@t3tools/contracts";+import { describe, expect, it } from "vitest";++import { collectActiveTerminalThreadIds } from "./terminalStateCleanup";++const threadId = (id: string): ThreadId => ThreadId.makeUnsafe(id);++describe("collectActiveTerminalThreadIds", () => {+ it("retains non-deleted server threads", () => {+ const activeThreadIds = collectActiveTerminalThreadIds({+ snapshotThreads: [+ { id: threadId("server-1"), deletedAt: null },+ { id: threadId("server-2"), deletedAt: null },+ ],+ draftThreadIds: [],+ });++ expect(activeThreadIds).toEqual(new Set([threadId("server-1"), threadId("server-2")]));+ });++ it("ignores deleted server threads and keeps local draft threads", () => {+ const activeThreadIds = collectActiveTerminalThreadIds({+ snapshotThreads: [+ { id: threadId("server-active"), deletedAt: null },+ { id: threadId("server-deleted"), deletedAt: "2026-03-05T08:00:00.000Z" },+ ],+ draftThreadIds: [threadId("local-draft")],+ });++ expect(activeThreadIds).toEqual(new Set([threadId("server-active"), threadId("local-draft")]));+ });+});diff --git a/apps/web/src/lib/terminalStateCleanup.ts b/apps/web/src/lib/terminalStateCleanup.tsnew file mode 100644index 000000000..5f2bfdafa--- /dev/null+++ b/apps/web/src/lib/terminalStateCleanup.ts@@ -0,0 +1,25 @@+import type { ThreadId } from "@t3tools/contracts";++interface TerminalRetentionThread {+ id: ThreadId;+ deletedAt: string | null;+}++interface CollectActiveTerminalThreadIdsInput {+ snapshotThreads: readonly TerminalRetentionThread[];+ draftThreadIds: Iterable<ThreadId>;+}++export function collectActiveTerminalThreadIds(+ input: CollectActiveTerminalThreadIdsInput,+): Set<ThreadId> {+ const activeThreadIds = new Set<ThreadId>();+ for (const thread of input.snapshotThreads) {+ if (thread.deletedAt !== null) continue;+ activeThreadIds.add(thread.id);+ }+ for (const draftThreadId of input.draftThreadIds) {+ activeThreadIds.add(draftThreadId);+ }+ return activeThreadIds;+}diff --git a/apps/web/src/routes/__root.tsx b/apps/web/src/routes/__root.tsxindex 89037c218..eb3eca9cb 100644--- a/apps/web/src/routes/__root.tsx+++ b/apps/web/src/routes/__root.tsx@@ -14,12 +14,14 @@ import { Button } from "../components/ui/button";import { AnchoredToastProvider, ToastProvider, toastManager } from "../components/ui/toast";import { serverConfigQueryOptions, serverQueryKeys } from "../lib/serverReactQuery";import { readNativeApi } from "../nativeApi";+import { useComposerDraftStore } from "../composerDraftStore";import { useStore } from "../store";import { useTerminalStateStore } from "../terminalStateStore";import { preferredTerminalEditor } from "../terminal-links";import { terminalRunningSubprocessFromEvent } from "../terminalActivity";import { onServerConfigUpdated, onServerWelcome } from "../wsNativeApi";import { providerQueryKeys } from "../lib/providerReactQuery";+import { collectActiveTerminalThreadIds } from "../lib/terminalStateCleanup";export const Route = createRootRouteWithContext<{queryClient: QueryClient;@@ -154,9 +156,13 @@ function EventRouter() {if (disposed) return;latestSequence = Math.max(latestSequence, snapshot.snapshotSequence);syncServerReadModel(snapshot);- const activeThreadIds = new Set(- snapshot.threads.filter((t) => t.deletedAt === null).map((t) => t.id),- );+ const draftThreadIds = Object.keys(+ useComposerDraftStore.getState().draftThreadsByThreadId,+ ) as ThreadId[];+ const activeThreadIds = collectActiveTerminalThreadIds({+ snapshotThreads: snapshot.threads,+ draftThreadIds,+ });removeOrphanedTerminalStates(activeThreadIds);if (pending) {pending = false;