Skip to content

Commit 5dc3c89

Browse files
Preserve draft terminal state during snapshot cleanup
- add `collectActiveTerminalThreadIds` to keep non-deleted server threads plus local draft threads - use helper in root event router before orphan terminal state removal - add unit tests for retained and deleted thread behavior
1 parent 8c5b8d3 commit 5dc3c89

3 files changed

Lines changed: 66 additions & 3 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { ThreadId } from "@t3tools/contracts";
2+
import { describe, expect, it } from "vitest";
3+
4+
import { collectActiveTerminalThreadIds } from "./terminalStateCleanup";
5+
6+
const threadId = (id: string): ThreadId => ThreadId.makeUnsafe(id);
7+
8+
describe("collectActiveTerminalThreadIds", () => {
9+
it("retains non-deleted server threads", () => {
10+
const activeThreadIds = collectActiveTerminalThreadIds({
11+
snapshotThreads: [
12+
{ id: threadId("server-1"), deletedAt: null },
13+
{ id: threadId("server-2"), deletedAt: null },
14+
],
15+
draftThreadIds: [],
16+
});
17+
18+
expect(activeThreadIds).toEqual(new Set([threadId("server-1"), threadId("server-2")]));
19+
});
20+
21+
it("ignores deleted server threads and keeps local draft threads", () => {
22+
const activeThreadIds = collectActiveTerminalThreadIds({
23+
snapshotThreads: [
24+
{ id: threadId("server-active"), deletedAt: null },
25+
{ id: threadId("server-deleted"), deletedAt: "2026-03-05T08:00:00.000Z" },
26+
],
27+
draftThreadIds: [threadId("local-draft")],
28+
});
29+
30+
expect(activeThreadIds).toEqual(new Set([threadId("server-active"), threadId("local-draft")]));
31+
});
32+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { ThreadId } from "@t3tools/contracts";
2+
3+
interface TerminalRetentionThread {
4+
id: ThreadId;
5+
deletedAt: string | null;
6+
}
7+
8+
interface CollectActiveTerminalThreadIdsInput {
9+
snapshotThreads: readonly TerminalRetentionThread[];
10+
draftThreadIds: Iterable<ThreadId>;
11+
}
12+
13+
export function collectActiveTerminalThreadIds(
14+
input: CollectActiveTerminalThreadIdsInput,
15+
): Set<ThreadId> {
16+
const activeThreadIds = new Set<ThreadId>();
17+
for (const thread of input.snapshotThreads) {
18+
if (thread.deletedAt !== null) continue;
19+
activeThreadIds.add(thread.id);
20+
}
21+
for (const draftThreadId of input.draftThreadIds) {
22+
activeThreadIds.add(draftThreadId);
23+
}
24+
return activeThreadIds;
25+
}

‎apps/web/src/routes/__root.tsx‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ import { Button } from "../components/ui/button";
1414
import { AnchoredToastProvider, ToastProvider, toastManager } from "../components/ui/toast";
1515
import { serverConfigQueryOptions, serverQueryKeys } from "../lib/serverReactQuery";
1616
import { readNativeApi } from "../nativeApi";
17+
import { useComposerDraftStore } from "../composerDraftStore";
1718
import { useStore } from "../store";
1819
import { useTerminalStateStore } from "../terminalStateStore";
1920
import { preferredTerminalEditor } from "../terminal-links";
2021
import { terminalRunningSubprocessFromEvent } from "../terminalActivity";
2122
import { onServerConfigUpdated, onServerWelcome } from "../wsNativeApi";
2223
import { providerQueryKeys } from "../lib/providerReactQuery";
24+
import { collectActiveTerminalThreadIds } from "../lib/terminalStateCleanup";
2325

2426
export const Route = createRootRouteWithContext<{
2527
queryClient: QueryClient;
@@ -154,9 +156,13 @@ function EventRouter() {
154156
if (disposed) return;
155157
latestSequence = Math.max(latestSequence, snapshot.snapshotSequence);
156158
syncServerReadModel(snapshot);
157-
const activeThreadIds = new Set(
158-
snapshot.threads.filter((t) => t.deletedAt === null).map((t) => t.id),
159-
);
159+
const draftThreadIds = Object.keys(
160+
useComposerDraftStore.getState().draftThreadsByThreadId,
161+
) as ThreadId[];
162+
const activeThreadIds = collectActiveTerminalThreadIds({
163+
snapshotThreads: snapshot.threads,
164+
draftThreadIds,
165+
});
160166
removeOrphanedTerminalStates(activeThreadIds);
161167
if (pending) {
162168
pending = false;

0 commit comments

Comments
 (0)