Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions apps/web/src/lib/terminalStateCleanup.test.ts
Original file line number Diff line number Diff line change
@@ -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")]));
});
});
25 changes: 25 additions & 0 deletions apps/web/src/lib/terminalStateCleanup.ts
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 9 additions & 3 deletions apps/web/src/routes/__root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down