Skip to content
TypeScript

refactor(web): ChatView's thread derivations are tested functions in ChatView.logic (#23071)

Julius Marminge9952a7aa5182
diff --git a/apps/web/src/components/ChatView.logic.test.ts b/apps/web/src/components/ChatView.logic.test.ts
index f0aa2ab8b6b..a03a7929b09 100644
--- a/apps/web/src/components/ChatView.logic.test.ts
+++ b/apps/web/src/components/ChatView.logic.test.ts
@@ -5,6 +5,8 @@ import {
planComposerTurn,
snapshotComposerSend,
withMessageContext,
+ deriveAvailableRevertTurnCounts,
+ deriveWorkspaceMutationId,
} from "./ChatView.logic";
import { recallCheckoutIsRepo, rememberCheckoutIsRepo } from "./ChatView.logic";
import { ProviderDriverKind, type ServerProvider } from "@t3tools/contracts";
@@ -1158,3 +1160,82 @@ describe("boundAttachmentIds", () => {
).toEqual(["upload-1", "local-2"]);
});
});
+
+describe("deriveAvailableRevertTurnCounts", () => {
+ const providerThreadId = ProviderThreadId.make("provider-thread");
+ const otherProviderThreadId = ProviderThreadId.make("other-provider-thread");
+
+ function projection(input: { readonly providerSessionId: ProviderSessionId | null }) {
+ const base = makeThreadProjectionFixture();
+ const run = (ordinal: number) => ({
+ id: RunId.make(`run-${ordinal}`),
+ ordinal,
+ activeAttemptId: `attempt-${ordinal}`,
+ });
+ const turn = (ordinal: number, attempt: number, threadId: ProviderThreadId) => ({
+ id: `turn-${attempt}-${ordinal}`,
+ runAttemptId: `attempt-${attempt}`,
+ ordinal,
+ providerThreadId: threadId,
+ });
+ return {
+ ...base,
+ thread: { ...base.thread, activeProviderThreadId: providerThreadId },
+ providerThreads: [
+ {
+ id: providerThreadId,
+ providerInstanceId: base.thread.modelSelection.instanceId,
+ providerSessionId: input.providerSessionId,
+ },
+ ],
+ runs: [run(1), run(2), run(3)],
+ attempts: [1, 2, 3].map((ordinal) => ({ id: `attempt-${ordinal}`, providerTurnId: null })),
+ // Run 2's latest turn moved to another provider thread, so its checkpoint cannot restore
+ // this conversation.
+ providerTurns: [
+ turn(1, 1, providerThreadId),
+ turn(1, 2, providerThreadId),
+ turn(2, 2, otherProviderThreadId),
+ turn(1, 3, providerThreadId),
+ ],
+ } as unknown as ReturnType<typeof makeThreadProjectionFixture>;
+ }
+
+ it("offers the thread start and runs whose latest turn is on the active provider thread", () => {
+ const available = deriveAvailableRevertTurnCounts(
+ projection({ providerSessionId: ProviderSessionId.make("session") }),
+ );
+ expect([...available].toSorted()).toEqual([0, 1, 3]);
+ });
+
+ it("offers nothing without a provider session to rewind", () => {
+ expect(deriveAvailableRevertTurnCounts(projection({ providerSessionId: null })).size).toBe(0);
+ expect(deriveAvailableRevertTurnCounts(null).size).toBe(0);
+ });
+});
+
+describe("deriveWorkspaceMutationId", () => {
+ const item = (id: string, type: string, status: string) =>
+ ({
+ item: { id: TurnItemId.make(id), type, status },
+ }) as unknown as OrchestrationV2ProjectedTurnItem;
+
+ it("keys on the latest finished command or file change and the latest checkpoint", () => {
+ expect(
+ deriveWorkspaceMutationId(
+ [
+ item("edit", "file_change", "completed"),
+ item("reply", "assistant_message", "completed"),
+ item("command", "command_execution", "running"),
+ ],
+ "2026-01-01T00:00:00.000Z",
+ ),
+ ).toBe(JSON.stringify(["edit", "2026-01-01T00:00:00.000Z"]));
+ });
+
+ it("is null before anything could have touched the workspace", () => {
+ expect(deriveWorkspaceMutationId([item("reply", "assistant_message", "completed")], null)).toBe(
+ null,
+ );
+ });
+});
diff --git a/apps/web/src/components/ChatView.logic.ts b/apps/web/src/components/ChatView.logic.ts
index 64abb91c5c3..1928cd21b12 100644
--- a/apps/web/src/components/ChatView.logic.ts
+++ b/apps/web/src/components/ChatView.logic.ts
@@ -1,9 +1,22 @@
+import { type OrchestrationV2ProviderTurn, type RunAttemptId } from "@t3tools/contracts";
+import {
+ deriveLatestThreadRun,
+ deriveThreadRuntime,
+} from "@t3tools/client-runtime/state/thread-execution";
+import { derivePendingBackgroundWork } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
+import {
+ latestUnheldRun,
+ usageLimitRunPresentedAsLatest,
+} from "@t3tools/shared/orchestrationV2ThreadError";
import * as Option from "effect/Option";
import type { SetStateAction } from "react";
import { applyClaudePromptEffortPrefix, resolvePromptInjectedEffort } from "@t3tools/shared/model";
import { getProviderModelCapabilities } from "../providerModels";
import { sanitizeThreadErrorMessage } from "~/rpc/transportError";
-import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
+import type {
+ EnvironmentThread,
+ EnvironmentThreadShell,
+} from "@t3tools/client-runtime/state/shell";
import {
ANTIGRAVITY_DEFAULT_MODEL,
PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
@@ -92,6 +105,8 @@ import {
type ProviderInstanceEntry,
} from "../providerInstances";
+type ThreadProjection = EnvironmentThread["projection"];
+
export const LAST_INVOKED_SCRIPT_BY_PROJECT_KEY = "t3code:last-invoked-script-by-project";
export const MAX_HIDDEN_MOUNTED_TERMINAL_THREADS = 10;
@@ -1610,3 +1625,136 @@ export function sendTitleFallbackLabels(snapshot: ComposerSendSnapshot) {
previewAnnotation ? previewAnnotationContextLabel(previewAnnotation) : undefined,
];
}
+
+/** A thread shell built from its loaded detail, for direct links that hydrate the detail first. */
+export function presentThreadShellFromProjection(
+ environmentId: EnvironmentId,
+ threadId: ThreadId,
+ projection: ThreadProjection,
+) {
+ const latestRun = deriveLatestThreadRun(projection);
+ const runtime = deriveThreadRuntime(projection);
+ const pendingRuntimeRequest =
+ projection.runtimeRequests.findLast((request) => request.status === "pending") ?? null;
+ // Direct links can hydrate detail before the thread enters the shell index.
+ return {
+ ...presentThreadShell(environmentId, {
+ ...projection.thread,
+ latestRunId: latestRun?.runId ?? null,
+ activeRunId: runtime?.activeRunId ?? null,
+ status: runtime?.status ?? "idle",
+ pendingRuntimeRequest,
+ latestVisibleMessage: null,
+ latestUserMessageAt:
+ projection.messages.findLast((message) => message.role === "user")?.updatedAt ?? null,
+ hasActionableProposedPlan: projection.plans.some(
+ (plan) => plan.kind === "proposed_plan" && plan.status === "active",
+ ),
+ providerInstanceHistory: [
+ ...new Set(
+ projection.providerThreads
+ .filter((thread) => thread.appThreadId === threadId && thread.ownerNodeId === null)
+ .map((thread) => thread.providerInstanceId),
+ ),
+ ],
+ goal:
+ projection.providerThreads.find(
+ (thread) => thread.id === projection.thread.activeProviderThreadId,
+ )?.goal ?? null,
+ itemCount: projection.turnItems.length,
+ visibleItemCount: projection.visibleTurnItems.length,
+ updatedAt: projection.updatedAt,
+ }),
+ latestRun,
+ runtime,
+ };
+}
+
+/** Background work the provider still runs after the latest run ended. */
+export function deriveThreadPendingBackgroundTasks(projection: ThreadProjection | null) {
+ if (projection === null) return [];
+ const sessionError =
+ projection.providerSessions.findLast(
+ (session) => session.providerInstanceId === projection.thread.providerInstanceId,
+ )?.lastError ?? null;
+ const latestRun =
+ usageLimitRunPresentedAsLatest(projection.runs, projection.turnItems, sessionError) ??
+ latestUnheldRun(projection.runs);
+ return [
+ ...derivePendingBackgroundWork({
+ latestRun,
+ providerThreads: projection.providerThreads,
+ turnItems: projection.turnItems,
+ activeProviderThreadId: projection.thread.activeProviderThreadId,
+ runs: projection.runs,
+ pullRequests: projection.thread.pullRequests,
+ }),
+ ];
+}
+
+/**
+ * Run ordinals the conversation can be reverted to: those whose provider turn belongs to the
+ * active provider thread, plus 0 for the thread start. Empty without a live provider session
+ * on the thread's selected instance.
+ */
+export function deriveAvailableRevertTurnCounts(projection: ThreadProjection | null) {
+ const available = new Set<number>();
+ const providerThread = projection?.providerThreads.find(
+ (candidate) => candidate.id === projection.thread.activeProviderThreadId,
+ );
+ if (
+ !providerThread?.providerSessionId ||
+ providerThread.providerInstanceId !== projection?.thread.modelSelection.instanceId
+ )
+ return available;
+ available.add(0);
+ const attemptsById = new Map(projection.attempts.map((attempt) => [attempt.id, attempt]));
+ const providerTurnsById = new Map<
+ OrchestrationV2ProviderTurn["id"],
+ OrchestrationV2ProviderTurn
+ >();
+ const latestTurnByAttempt = new Map<RunAttemptId, OrchestrationV2ProviderTurn>();
+ for (const turn of projection.providerTurns) {
+ providerTurnsById.set(turn.id, turn);
+ if (turn.runAttemptId === null) continue;
+ const latest = latestTurnByAttempt.get(turn.runAttemptId);
+ if (latest === undefined || turn.ordinal > latest.ordinal) {
+ latestTurnByAttempt.set(turn.runAttemptId, turn);
+ }
+ }
+ for (const run of projection.runs) {
+ if (run.activeAttemptId === null) continue;
+ const attempt = attemptsById.get(run.activeAttemptId);
+ const providerTurn =
+ latestTurnByAttempt.get(run.activeAttemptId) ??
+ (attempt?.providerTurnId == null ? undefined : providerTurnsById.get(attempt.providerTurnId));
+ // A ready file checkpoint alone cannot restore the provider conversation.
+ if (providerTurn?.providerThreadId === providerThread.id) available.add(run.ordinal);
+ }
+ return available;
+}
+
+/**
+ * Changes when the agent may have changed the workspace, so file views refresh. File tools are
+ * explicit; completed commands are included because a shell command can mutate the workspace
+ * without reporting the paths it touched.
+ */
+export function deriveWorkspaceMutationId(
+ visibleTurnItems: ReadonlyArray<OrchestrationV2ProjectedTurnItem>,
+ latestCheckpointCompletedAt: string | null,
+) {
+ let itemId: string | null = null;
+ for (let index = visibleTurnItems.length - 1; index >= 0; index -= 1) {
+ const item = visibleTurnItems[index]?.item;
+ if (!item) continue;
+ if (item.type !== "command_execution" && item.type !== "file_change") continue;
+ if (item.status === "pending" || item.status === "running" || item.status === "waiting") {
+ continue;
+ }
+ itemId = item.id;
+ break;
+ }
+ return itemId === null && latestCheckpointCompletedAt === null
+ ? null
+ : JSON.stringify([itemId, latestCheckpointCompletedAt]);
+}
diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx
index bd9ff5cfe4b..37b2e902974 100644
--- a/apps/web/src/components/ChatView.tsx
+++ b/apps/web/src/components/ChatView.tsx
@@ -1,3 +1,9 @@
+import {
+ deriveAvailableRevertTurnCounts,
+ deriveThreadPendingBackgroundTasks,
+ deriveWorkspaceMutationId,
+ presentThreadShellFromProjection,
+} from "./ChatView.logic";
import { usePullRequestDraftThread } from "./chat/usePullRequestDraftThread";
import { useComposerSelection } from "./chat/useComposerSelection";
import { useProjectCloneBanner } from "./chat/useProjectCloneBanner";
@@ -55,21 +61,18 @@ import {
type EnvironmentId,
type MessageId,
type ModelSelection,
- type OrchestrationV2ProviderTurn,
type PreviewAnnotationPayload,
ProviderInstanceId,
type ServerProvider,
type ScopedThreadRef,
type ThreadId,
type RunId,
- type RunAttemptId,
ProviderInteractionMode,
resolveIdleCompaction,
RuntimeMode,
} from "@t3tools/contracts";
import { readPastedComposerContext } from "./composerInlineTokenPaste";
import { isPasteAsTextShortcut } from "@t3tools/client-runtime/text-paste";
-import { presentThreadShell } from "@t3tools/client-runtime/state/shell";
import { useThreadActions } from "../hooks/useThreadActions";
import {
deriveProviderSubagentStatus,
@@ -100,11 +103,6 @@ import {
} from "@t3tools/client-runtime/environment";
import { formatModelSlugName, resolveSelectableModel } from "@t3tools/shared/model";
import { projectScriptCwd, resolveProjectScripts } from "@t3tools/shared/projectScripts";
-import { derivePendingBackgroundWork } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
-import {
- latestUnheldRun,
- usageLimitRunPresentedAsLatest,
-} from "@t3tools/shared/orchestrationV2ThreadError";
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
import { sourceControlRepositorySelector } from "@t3tools/shared/sourceControl";
import { resolveThreadReferenceCopyTarget } from "@t3tools/shared/threadReference";
@@ -477,48 +475,10 @@ export default function ChatView(props: ChatViewProps) {
});
const serverThreadProjection = useThreadProjection(routeThreadDetailRef);
const serverProjection = serverThreadProjection?.projection ?? null;
- const serverThread = (() => {
- if (serverThreadShell !== null || serverProjection === null) {
- return serverThreadShell;
- }
- const latestRun = deriveLatestThreadRun(serverProjection);
- const runtime = deriveThreadRuntime(serverProjection);
- const pendingRuntimeRequest =
- serverProjection.runtimeRequests.findLast((request) => request.status === "pending") ?? null;
- // Direct links can hydrate detail before the thread enters the shell index.
- return {
- ...presentThreadShell(environmentId, {
- ...serverProjection.thread,
- latestRunId: latestRun?.runId ?? null,
- activeRunId: runtime?.activeRunId ?? null,
- status: runtime?.status ?? "idle",
- pendingRuntimeRequest,
- latestVisibleMessage: null,
- latestUserMessageAt:
- serverProjection.messages.findLast((message) => message.role === "user")?.updatedAt ??
- null,
- hasActionableProposedPlan: serverProjection.plans.some(
- (plan) => plan.kind === "proposed_plan" && plan.status === "active",
- ),
- providerInstanceHistory: [
- ...new Set(
- serverProjection.providerThreads
- .filter((thread) => thread.appThreadId === threadId && thread.ownerNodeId === null)
- .map((thread) => thread.providerInstanceId),
- ),
- ],
- goal:
- serverProjection.providerThreads.find(
- (thread) => thread.id === serverProjection.thread.activeProviderThreadId,
- )?.goal ?? null,
- itemCount: serverProjection.turnItems.length,
- visibleItemCount: serverProjection.visibleTurnItems.length,
- updatedAt: serverProjection.updatedAt,
- }),
- latestRun,
- runtime,
- };
- })();
+ const serverThread =
+ serverThreadShell !== null || serverProjection === null
+ ? serverThreadShell
+ : presentThreadShellFromProjection(environmentId, threadId, serverProjection);
const promptRef = useRef("");
const localComposerRef = useRef<ChatComposerHandle | null>(null);
const composerRef = useComposerHandleContext() ?? localComposerRef;
@@ -1452,31 +1412,7 @@ export default function ChatView(props: ChatViewProps) {
activeThreadLiveTokenUsage,
activeProviderThread,
);
- const pendingBackgroundTasks = (() => {
- if (serverProjection === null || serverProjection === undefined) {
- return [];
- }
- const sessionError =
- serverProjection.providerSessions.findLast(
- (session) => session.providerInstanceId === serverProjection.thread.providerInstanceId,
- )?.lastError ?? null;
- const latestRun =
- usageLimitRunPresentedAsLatest(
- serverProjection.runs,
- serverProjection.turnItems,
- sessionError,
- ) ?? latestUnheldRun(serverProjection.runs);
- return [
- ...derivePendingBackgroundWork({
- latestRun,
- providerThreads: serverProjection.providerThreads,
- turnItems: serverProjection.turnItems,
- activeProviderThreadId: serverProjection.thread.activeProviderThreadId,
- runs: serverProjection.runs,
- pullRequests: serverProjection.thread.pullRequests,
- }),
- ];
- })();
+ const pendingBackgroundTasks = deriveThreadPendingBackgroundTasks(serverProjection);
const activeWorkStartedAt =
deriveActiveWorkStartedAt(activeActivityRun, activeRuntime, localDispatchStartedAt) ??
runlessWorkStartedAt;
@@ -1614,70 +1550,16 @@ export default function ChatView(props: ChatViewProps) {
panelAnimationDurationMs,
);
const turnDiffSummaries = useThreadCheckpointSummaries(routeThreadDetailRef);
- const availableRevertTurnCounts = (() => {
- const available = new Set<number>();
- const providerThread = serverProjection?.providerThreads.find(
- (candidate) => candidate.id === serverProjection.thread.activeProviderThreadId,
- );
- if (
- !providerThread?.providerSessionId ||
- providerThread.providerInstanceId !== serverProjection?.thread.modelSelection.instanceId
- )
- return available;
- available.add(0);
- const attemptsById = new Map(serverProjection.attempts.map((attempt) => [attempt.id, attempt]));
- const providerTurnsById = new Map<
- OrchestrationV2ProviderTurn["id"],
- OrchestrationV2ProviderTurn
- >();
- const latestTurnByAttempt = new Map<RunAttemptId, OrchestrationV2ProviderTurn>();
- for (const turn of serverProjection.providerTurns) {
- providerTurnsById.set(turn.id, turn);
- if (turn.runAttemptId === null) continue;
- const latest = latestTurnByAttempt.get(turn.runAttemptId);
- if (latest === undefined || turn.ordinal > latest.ordinal) {
- latestTurnByAttempt.set(turn.runAttemptId, turn);
- }
- }
- for (const run of serverProjection.runs) {
- if (run.activeAttemptId === null) continue;
- const attempt = attemptsById.get(run.activeAttemptId);
- const providerTurn =
- latestTurnByAttempt.get(run.activeAttemptId) ??
- (attempt?.providerTurnId == null
- ? undefined
- : providerTurnsById.get(attempt.providerTurnId));
- // A ready file checkpoint alone cannot restore the provider conversation.
- if (providerTurn?.providerThreadId === providerThread.id) available.add(run.ordinal);
- }
- return available;
- })();
+ const availableRevertTurnCounts = deriveAvailableRevertTurnCounts(serverProjection);
// A paged history window cannot identify the first loaded run as the thread start.
const threadStartCheckpointReady =
!serverThreadHistory.hasMoreHistory &&
findRevertCheckpoint(serverProjection?.checkpoints ?? [], 0) !== undefined;
- /**
- * File tools are explicit; completed commands are included because a shell
- * command can mutate the workspace without reporting the paths it touched.
- */
- const workspaceMutationId = (() => {
- let itemId: string | null = null;
- for (let index = serverVisibleTurnItems.length - 1; index >= 0; index -= 1) {
- const item = serverVisibleTurnItems[index]?.item;
- if (!item) continue;
- if (item.type !== "command_execution" && item.type !== "file_change") continue;
- if (item.status === "pending" || item.status === "running" || item.status === "waiting") {
- continue;
- }
- itemId = item.id;
- break;
- }
- const latestCheckpointCompletedAt = turnDiffSummaries.at(-1)?.completedAt ?? null;
- return itemId === null && latestCheckpointCompletedAt === null
- ? null
- : JSON.stringify([itemId, latestCheckpointCompletedAt]);
- })();
+ const workspaceMutationId = deriveWorkspaceMutationId(
+ serverVisibleTurnItems,
+ turnDiffSummaries.at(-1)?.completedAt ?? null,
+ );
const gitCwd = activeProject
? projectScriptCwd({