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
This commit is contained in:
Julius Marminge authored and Julius Marminge committed 2026-03-05 23:41:23 -08:00
1 parent 0f702fcd62
commit b9e529e0b5
3 files changed
+66 -3

No files matched your search

@@ -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
View File
@@ -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;
}
+9 -3
View File
@@ -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;