feat: side chat and fork surfaces for web and mobile #9437
Closedamanthanvi(GitHub profile, opens in a new tab)wants to merge 38 commits into
main fromrefs/pull/9437/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XXL
- apps/server/src/orchestration/Layers/ProviderCommandReactor.ts line 726Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -563,6 +566,11 @@563566 activeSession !== undefined &&564567 activeSession.providerInstanceId !== undefined565568 ? activeSession.providerInstanceId566- : thread.modelSelection.instanceId;569+ : (forkBinding?.providerInstanceId ?? thread.modelSelection.instanceId);567570 const desiredModelSelection = requestedModelSelection ?? thread.modelSelection;568571 const desiredInstanceId = desiredModelSelection.instanceId;572+ const inheritedForkInstanceId =573+ forkBinding?.providerInstanceId ??574+ thread.session?.providerInstanceId ??575+ thread.modelSelection.instanceId;576+ if (apps/server/src/orchestration/Layers/ProviderCommandReactor.ts line 1011Resolvedby amanthanvi(GitHub profile, opens in a new tab)@@ -782,0 +830,21 @@830+ method: "thread.turn.start",831+ detail: `Source thread '${latestTurnFork?.sourceThreadId}' advanced after fork '${threadId}' was created. Latest-turn providers can only fork from the source's current completed head.`,832+ });833+834+ if (!(yield* sourceStillAtRecordedForkBoundary())) {835+ return yield* latestTurnForkBoundaryError();836+ }837+838+ const startedSession = yield* startProviderSession(839+ thread.fork == null840+ ? undefined841+ : {842+ forkFrom: {843+ threadId: thread.fork.sourceThreadId,844+ ...(thread.fork.sourceTurnId !== null ? { turnId: thread.fork.sourceTurnId } : {}),845+ },846+ },847+ );848+ if (!(yield* sourceStillAtRecordedForkBoundary())) {849+ const boundaryError = latestTurnForkBoundaryError();850+ yield* providerService.stopSession({ threadId: startedSession.threadId }).pipe(apps/web/src/threadForking.logic.ts line 48Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +16,21 @@16+export function resolveLatestCompletedForkTarget(17+ latestTurn: OrchestrationLatestTurn | null | undefined,18+): ThreadForkTarget | null {19+ if (20+ latestTurn?.state !== "completed" ||21+ latestTurn.completedAt === null ||22+ latestTurn.assistantMessageId === null23+ ) {24+ return null;25+ }26+ return {27+ turnId: latestTurn.turnId,28+ messageId: latestTurn.assistantMessageId,29+ };30+}31+32+function resolveLatestCompletedMessageTarget(33+ messages: ReadonlyArray<Pick<OrchestrationMessage, "id" | "role" | "streaming" | "turnId">>,34+ excludedTurnId: TurnId,35+): ThreadForkTarget | null {36+ const message = messages.findLast(apps/web/src/hooks/useThreadActionMenu.ts line 249OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -222,3 +245,5 @@222245 }223246 return;224247 }248+ case "open-side-chat":249+ forkEntry?.onOpenSideChat();apps/web/src/components/Sidebar.tsx line 4069Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3204,3 +3223,5 @@32043223 if (preset) attemptSnooze(threadRef, preset);32053224 return;32063225 }3226+ if (clicked.value?.startsWith("open-existing-side-chat:")) {3227+ useRightPanelStorepackages/contracts/src/orchestration.ts line 1069Resolvedby amanthanvi(GitHub profile, opens in a new tab)@@ -802,3 +814,9 @@802814 createdAt: IsoDateTime,803815 });804816817+export const ThreadForkCommand = Schema.Struct({818+ type: Schema.Literal("thread.fork"),819+ commandId: CommandId,820+ threadId: ThreadId,821+ sourceThreadId: ThreadId,822+ sourceTurnId: Schema.optional(TurnId),
- apps/mobile/src/features/threads/ThreadRouteScreen.tsx lines 1061 to 1068OutdatedResolvedby amanthanvi(GitHub profile, opens in a new tab)@@ -808,3 +1056,13 @@8081056 onSelectUserInputOption={requests.onSelectUserInputOption}8091057 onChangeUserInputCustomAnswer={requests.onChangeUserInputCustomAnswer}8101058 onSubmitUserInput={requests.onSubmitUserInput}1059+ forkCapability={forkCapability}1060+ onForkAssistantMessage={handleForkAssistantMessage}1061+ {...(forkParentRef1062+ ? {1063+ forkOrigin: {1064+ title: forkParent?.title ?? "thread",1065+ onPress: openForkParent,1066+ },1067+ }1068+ : {})}apps/web/src/components/threadActionMenu.logic.ts lines 80 to 89Resolvedby amanthanvi(GitHub profile, opens in a new tab)@@ -62,3 +71,17 @@6271 },6372 ]6473 : []),74+ ...(state.forking75+ ? [76+ {77+ id: "open-side-chat" as const,78+ label: "Open side chat",79+ icon: "message-square-plus",80+ disabled: !state.forking.enabled,81+ },82+ {83+ id: "fork-thread" as const,84+ label: "Fork thread",85+ icon: "git-fork",86+ disabled: !state.forking.enabled,87+ },apps/server/src/provider/Layers/OpenCodeAdapter.ts lines 2452 to 2457OutdatedResolvedby amanthanvi(GitHub profile, opens in a new tab)@@ -2430,3 +2438,20 @@24302438 // a confirmed not-found (start fresh); transport/auth/server24312439 // errors propagate instead of masking as a new empty session.24322440 const resolved = yield* Effect.gen(function* () {2441+ if (forkSessionId) {2442+ const forkedSession = yield* runOpenCodeSdk("session.fork", () =>2443+ client.session.fork({ sessionID: forkSessionId, directory }),2444+ );2445+ const forked = forkedSession.data;2446+ if (!forked) {2447+ return yield* new OpenCodeRuntimeError({2448+ operation: "session.fork",2449+ detail: "OpenCode session.fork returned no session payload.",2450+ });2451+ }2452+ yield* runOpenCodeSdk("session.update", () =>2453+ client.session.update({2454+ sessionID: forked.id,2455+ permission: buildOpenCodePermissionRules(input.runtimeMode),2456+ }),2457+ );
- apps/mobile/src/features/threads/sideChats.logic.ts line 49OutdatedResolvedby amanthanvi(GitHub profile, opens in a new tab)@@ -0,0 +29,21 @@29+ ? latestTurn.turnId30+ : null;31+}32+33+export function canForkMobileAssistantMessage(input: {34+ readonly capability: ServerProviderSessionFork | undefined;35+ readonly completed: boolean;36+ readonly messageTurnId: TurnId | null;37+ readonly latestTurn:38+ | Pick<OrchestrationLatestTurn, "turnId" | "state" | "completedAt">39+ | null40+ | undefined;41+}): boolean {42+ if (!input.completed || input.messageTurnId === null) return false;43+ if (44+ input.latestTurn?.turnId === input.messageTurnId &&45+ resolveMobileLatestCompletedTurnId(input.latestTurn) !== input.messageTurnId46+ ) {47+ return false;48+ }49+ if (input.capability === "any-turn") return true;docs/user/side-chats-and-forks.md lines 39 to 40OutdatedResolvedby amanthanvi(GitHub profile, opens in a new tab)@@ -0,0 +20,21 @@20+**Claude** and **OpenCode** can fork only from the latest completed agent response.21+**Cursor**, **Grok**, and **Antigravity** do not currently support session forking.22+23+When a provider does not support forking, or the thread has no completed turn, the thread-menu and24+command-palette actions remain visible but unavailable. Earlier response actions are omitted for25+providers that can fork only from the latest turn.26+27+## Managing side chats28+29+Closing a side-chat tab only closes that local panel. The conversation still exists and can be30+reopened from **Side chats** in its parent thread's menu. On mobile, the same menu opens each side31+chat as a full-screen thread.32+33+Choose **Promote to thread** to move a side chat into the main thread list. Its conversation and34+link back to the parent are preserved.35+36+Deleting a side chat uses the normal thread delete action and permanently clears its conversation37+history. Closing and deleting are separate actions.38+39+Forked threads and promoted side chats show **Forked from _parent title_** at the top of the40+conversation. Select it to return to the source thread.apps/mobile/src/features/threads/ThreadRouteScreen.tsx line 635Resolvedby amanthanvi(GitHub profile, opens in a new tab)@@ -499,0 +574,21 @@574+ environmentId: sourceThread.environmentId,575+ input: {576+ threadId: nextThreadId,577+ sourceThreadId: sourceThread.id,578+ sourceTurnId: input.turnId,579+ sourceMessageId: input.messageId,580+ sideChat: input.sideChat,581+ createdAt: new Date().toISOString(),582+ },583+ });584+ if (result._tag === "Failure") {585+ if (!isAtomCommandInterrupted(result)) {586+ const error = squashAtomCommandFailure(result);587+ Alert.alert(588+ input.sideChat ? "Could not open side chat" : "Could not fork thread",589+ error instanceof Error ? error.message : "An error occurred.",590+ );591+ }592+ return;593+ }594+ navigationRef.current.navigate("Thread", {
- apps/mobile/src/features/threads/ThreadRouteScreen.tsx line 890Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -639,0 +842,21 @@842+ onPress: () => openSideChatThread(ThreadId.make(item.id.slice("side-chat:".length))),843+ })),844+ });845+ }846+ if (selectedThread?.sideChat === true) {847+ items.push({848+ type: "action",849+ label: "Delete",850+ destructive: true,851+ icon: { type: "sfSymbol", name: "trash" },852+ onPress: handleDeleteSideChat,853+ });854+ }855+ if (items.length === 0) return null;856+ return withNativeGlassHeaderItem({857+ type: "menu",858+ label: "",859+ accessibilityLabel: "Thread actions",860+ icon: { type: "sfSymbol", name: "ellipsis" },861+ menu: { title: "Thread", items },862+ });apps/mobile/src/features/threads/sideChats.logic.ts line 23Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,15 @@1+import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";2+import type {3+ OrchestrationCheckpointSummary,4+ OrchestrationLatestTurn,5+ ServerConfig,6+ ServerProviderSessionFork,7+ ThreadId,8+ TurnId,9+} from "@t3tools/contracts";10+11+export function visibleTopLevelThreads<T extends { readonly sideChat?: boolean }>(12+ threads: ReadonlyArray<T>,13+): T[] {14+ return threads.filter((thread) => thread.sideChat !== true);15+}
- apps/server/src/orchestration/Normalizer.ts line 188OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -131,0 +168,21 @@168+ ? activeSourceThread169+ : Option.fromNullishOr(170+ (yield* projectionSnapshotQuery.getArchivedShellSnapshot()).threads.find(171+ (thread) => thread.id === canonicalCommand.sourceThreadId,172+ ),173+ );174+ if (Option.isSome(sourceThread)) {175+ const providerService = yield* ProviderService;176+ const capabilities = yield* providerService.getCapabilities(177+ sourceThread.value.modelSelection.instanceId,178+ );179+ const latestTurn = sourceThread.value.latestTurn;180+ if (181+ capabilities.sessionFork === "latest-turn" &&182+ (latestTurn?.turnId !== canonicalCommand.sourceTurnId ||183+ latestTurn?.state !== "completed")184+ ) {185+ return yield* new OrchestrationCommandInvariantError({186+ commandType: canonicalCommand.type,187+ detail: `Turn '${canonicalCommand.sourceTurnId}' is not the latest completed turn of source thread '${canonicalCommand.sourceThreadId}'. Provider instance '${sourceThread.value.modelSelection.instanceId}' can only fork from the source's current completed head.`,188+ });apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts line 899OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -879,3 +896,4 @@879896 ON projects.project_id = threads.project_id880897 WHERE threads.deleted_at IS NULL881898 AND threads.archived_at IS NULL899+ AND threads.side_chat = 0apps/mobile/src/features/threads/sideChats.logic.ts line 68OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -0,0 +48,21 @@48+ );49+}50+51+export function canForkMobileAssistantMessage(input: {52+ readonly capability: ServerProviderSessionFork | undefined;53+ readonly completed: boolean;54+ readonly completedTurnIds: ReadonlySet<TurnId>;55+ readonly messageTurnId: TurnId | null;56+ readonly latestTurn:57+ | Pick<OrchestrationLatestTurn, "turnId" | "state" | "completedAt">58+ | null59+ | undefined;60+}): boolean {61+ if (!input.completed || input.messageTurnId === null) return false;62+ if (63+ input.latestTurn?.turnId === input.messageTurnId &&64+ resolveMobileLatestCompletedTurnId(input.latestTurn) !== input.messageTurnId65+ ) {66+ return false;67+ }68+ if (input.capability === "any-turn") return input.completedTurnIds.has(input.messageTurnId);
- apps/server/src/orchestration/Layers/ProviderCommandReactor.ts line 1008Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -777,6 +873,19 @@777873 });778874 yield* bindSessionToThread(restartedSession);779- return restartedSession.threadId;875+ return desiredModelSelection;780876 }781877782- const startedSession = yield* startProviderSession(undefined);878+ if (!(yield* sourceStillAtRecordedForkBoundary())) {879+ return yield* latestTurnForkBoundaryError();880+ }881+882+ const startedSession = yield* startProviderSession(883+ thread.fork == null884+ ? undefined885+ : {886+ forkFrom: {887+ threadId: thread.fork.sourceThreadId,888+ ...(thread.fork.sourceTurnId !== null ? { turnId: thread.fork.sourceTurnId } : {}),889+ },890+ },891+ );
- apps/server/src/orchestration/Layers/ProviderCommandReactor.ts line 913Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -711,3 +787,21 @@711787 });712788 });713789790+ let providerCapabilities: ProviderAdapterCapabilities | undefined;791+ let latestTurnFork: typeof thread.fork = null;792+ if (thread.fork != null && !forkHasOwnResumeCursor) {793+ providerCapabilities = yield* providerService.getCapabilities(desiredInstanceId);794+ if (providerCapabilities.sessionFork === "latest-turn") {795+ latestTurnFork = thread.fork;796+ }797+ }798+799+ const sourceStillAtRecordedForkBoundary = Effect.fnUntraced(function* () {800+ if (latestTurnFork === null) return true;801+ const source = yield* resolveThread(latestTurnFork.sourceThreadId);802+ const sourceLatestTurn = source?.latestTurn ?? null;803+ return latestTurnFork.sourceTurnId === null804+ ? sourceLatestTurn === null805+ : sourceLatestTurn?.turnId === latestTurnFork.sourceTurnId &&806+ sourceLatestTurn.state === "completed";807+ });apps/mobile/src/features/threads/sideChats.logic.ts line 39Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -0,0 +11,21 @@11+export function visibleTopLevelThreads<12+ T extends Pick<EnvironmentThreadShell, "fork" | "id" | "sideChat">,13+>(threads: ReadonlyArray<T>, knownThreadIds: ReadonlySet<ThreadId>): T[] {14+ return threads.filter((thread) => {15+ const sourceThreadId = thread.fork?.sourceThreadId;16+ return (17+ thread.sideChat !== true ||18+ sourceThreadId === undefined ||19+ !knownThreadIds.has(sourceThreadId)20+ );21+ });22+}23+24+export function resolveMobileThreadForkCapability(25+ thread: Pick<EnvironmentThreadShell, "modelSelection" | "session">,26+ serverConfig: ServerConfig | null,27+): ServerProviderSessionFork | undefined {28+ const instanceId = thread.session?.providerInstanceId ?? thread.modelSelection.instanceId;29+ return serverConfig?.providers.find((provider) => provider.instanceId === instanceId)30+ ?.sessionFork;31+}apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts line 1002OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -972,3 +999,4 @@972999 WHERE project_id = ${projectId}9731000 AND deleted_at IS NULL9741001 AND archived_at IS NULL1002+ AND side_chat = 0
Sign in to join this conversation.














macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed