Skip to content

Commit e549946

Browse files
t3dotggclaude
andcommitted
fix: side chat review round 2
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 7e88d5b commit e549946

19 files changed

Lines changed: 177 additions & 90 deletions

‎apps/mobile/src/features/threads/ThreadSideChatsRow.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ export function ThreadSideChatsRow(props: {
3838
openLinkedThread({ environmentId: props.environmentId, threadId: sideChat.id })
3939
}
4040
>
41-
{sideChat.hasPendingApprovals || sideChat.hasPendingUserInput ? (
41+
{sideChat.hasPendingApprovals ||
42+
sideChat.hasPendingUserInput ||
43+
sideChat.pendingSubagentRequest ? (
4244
<View
4345
className={cn(
4446
"h-1.5 w-1.5 shrink-0 rounded-full",
45-
sideChat.hasPendingApprovals
47+
sideChat.hasPendingApprovals || sideChat.pendingSubagentRequest === "approval"
4648
? "bg-warning-foreground"
4749
: "bg-adaptive-indigo-600-300",
4850
)}

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
11561156
Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })),
11571157
);
11581158

1159+
/** Includes tombstones, which still reserve their id. */
1160+
const findThread = (threadId: ThreadId) =>
1161+
projectionStore.getThread(threadId).pipe(
1162+
Effect.catchTags({ ProjectionStoreThreadNotFoundError: () => Effect.succeed(null) }),
1163+
Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })),
1164+
);
1165+
11591166
/** Side chats live inside their parent, so deleting the parent deletes them too. */
11601167
const liveSideChatIds = Effect.fn("Orchestrator.liveSideChatIds")(
11611168
function* (threadId: ThreadId) {
@@ -4144,6 +4151,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
41444151
cause: `Thread ${command.sourceThreadId} was deleted.`,
41454152
});
41464153
}
4154+
if (command.sideChat === true && (yield* findThread(command.targetThreadId)) !== null) {
4155+
return yield* new OrchestratorThreadAlreadyExistsError({
4156+
commandId: command.commandId,
4157+
threadId: command.targetThreadId,
4158+
});
4159+
}
41474160

41484161
const sourceRun = runForSourcePoint(sourceProjection, command.sourcePoint);
41494162

@@ -12547,6 +12560,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1254712560

1254812561
const dispatchOnce = Effect.fn("orchestrationV2.dispatch.once")(function* (
1254912562
command: OrchestrationV2ServerCommand,
12563+
sideChatIds: ReadonlyArray<ThreadId>,
1255012564
): Effect.fn.Return<
1255112565
{
1255212566
readonly events: ReadonlyArray<OrchestrationV2DomainEvent>;
@@ -12659,7 +12673,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1265912673
// The dispatch wrapper holds each side chat's lock, so none can commit after this plan.
1266012674
const plans = [
1266112675
yield* planDeletion(command),
12662-
...(yield* Effect.forEach(yield* liveSideChatIds(command.threadId), (threadId) =>
12676+
...(yield* Effect.forEach(sideChatIds, (threadId) =>
1266312677
planDeletion({
1266412678
...command,
1266512679
commandId: CommandId.make(`${command.commandId}:side-chat:${threadId}`),
@@ -12955,8 +12969,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1295512969
};
1295612970
});
1295712971

12972+
/** `sideChatIds` are the live side chats a `thread.delete` cascades to, read under their locks. */
1295812973
const dispatchWithReceiptEffect = Effect.fn("orchestrationV2.dispatch.withReceipt")(function* (
1295912974
command: OrchestrationV2ServerCommand,
12975+
sideChatIds: ReadonlyArray<ThreadId> = [],
1296012976
): Effect.fn.Return<OrchestratorV2DispatchResult, OrchestratorV2Error> {
1296112977
yield* Effect.annotateCurrentSpan({
1296212978
"orchestration_v2.command_id": command.commandId,
@@ -13032,7 +13048,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1303213048
if (shell !== null) yield* refuseAboveDispatchModeLimit(command, threadId, shell);
1303313049
}
1303413050

13035-
const plan = yield* dispatchOnce(command).pipe(
13051+
const plan = yield* dispatchOnce(command, sideChatIds).pipe(
1303613052
Effect.filterOrFail(
1303713053
(planned) =>
1303813054
planned.events.length > 0 ||
@@ -13184,21 +13200,27 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1318413200
dispatchWithReceiptEffect(command),
1318513201
);
1318613202
// A parent's lock comes before its side chats' locks, so a delete cascade and a side chat
13187-
// fork or command never interleave.
13203+
// fork or command never interleave. A fork from a side chat or onto an existing thread would
13204+
// invert that order; dispatch rejects both, so they skip the source lock.
1318813205
if (
1318913206
command.type === "thread.fork" &&
1319013207
command.sideChat === true &&
1319113208
command.sourceThreadId !== command.targetThreadId
13192-
)
13193-
return yield* threadDispatch.withLock(command.sourceThreadId, dispatch);
13209+
) {
13210+
const source = yield* findThread(command.sourceThreadId);
13211+
const target = yield* findThread(command.targetThreadId);
13212+
return yield* source === null || source.lineage.sideChat === true || target !== null
13213+
? dispatch
13214+
: threadDispatch.withLock(command.sourceThreadId, dispatch);
13215+
}
1319413216
if (command.type === "thread.delete")
1319513217
return yield* threadDispatch.withLock(
1319613218
command.threadId,
1319713219
liveSideChatIds(command.threadId).pipe(
1319813220
Effect.flatMap((sideChatIds) =>
1319913221
sideChatIds.reduce(
1320013222
(locked, sideChatId) => threadDispatch.withLock(sideChatId, locked),
13201-
dispatchWithReceiptEffect(command),
13223+
dispatchWithReceiptEffect(command, sideChatIds),
1320213224
),
1320313225
),
1320413226
),

‎apps/server/src/orchestration-v2/ProviderTurnStartService.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,18 +344,19 @@ export const layer: Layer.Layer<
344344
(isCompactionCommandMessage(message) || parseGoalCommand(message) !== null),
345345
))),
346346
)));
347+
const isSideChatHandoff = (handoff: OrchestrationV2ContextHandoff) =>
348+
projection.thread.lineage.sideChat === true &&
349+
ContextHandoffService.isSideChatContextHandoff(handoff);
347350
const handoffs = projection.contextHandoffs.filter(
348351
(handoff) =>
349-
!ContextHandoffService.isSideChatContextHandoff(handoff) &&
352+
!isSideChatHandoff(handoff) &&
350353
(handoff.transferId == null || !subagentResultTransfers.has(handoff.transferId)) &&
351354
isPendingForRun(handoff),
352355
);
353356
// Carried forward like other handoffs until a provider turn accepts it.
354357
const sideChatHandoff = projection.contextHandoffs.find(
355358
(handoff) =>
356-
ContextHandoffService.isSideChatContextHandoff(handoff) &&
357-
handoff.delivery === undefined &&
358-
isPendingForRun(handoff),
359+
isSideChatHandoff(handoff) && handoff.delivery === undefined && isPendingForRun(handoff),
359360
);
360361
const nativeForkTransfer = projection.contextTransfers.find(
361362
(transfer) =>

‎apps/server/src/orchestration-v2/ThreadFork.execution.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,15 @@ it.effect("a side chat opened during the parent's first turn starts from a diges
881881
ThreadId.make("side-chat-busy-nested"),
882882
).pipe(Effect.flip);
883883
assert.equal(nested._tag, "OrchestratorDispatchError");
884+
// Either would take the side chat's lock before the parent's, against the delete cascade.
885+
const ontoParent = yield* fork("side-chat-busy-onto-parent", sideChatId, parentId).pipe(
886+
Effect.flip,
887+
);
888+
assert.equal(ontoParent._tag, "OrchestratorThreadAlreadyExistsError");
889+
const ontoExisting = yield* fork("side-chat-busy-onto-existing", parentId, sideChatId).pipe(
890+
Effect.flip,
891+
);
892+
assert.equal(ontoExisting._tag, "OrchestratorThreadAlreadyExistsError");
884893
const busyMergeBack = yield* orchestrator
885894
.dispatch({
886895
type: "thread.merge_back",

‎apps/server/src/orchestration-v2/ThreadStream.test.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import {
2+
ContextHandoffId,
3+
ContextTransferId,
24
EventId,
35
OrchestrationV2ThreadStreamItem,
6+
ProviderThreadId,
7+
RunId,
48
ThreadId,
59
TurnItemId,
10+
type OrchestrationV2ContextHandoff,
611
type OrchestrationV2ProjectedTurnItem,
712
type OrchestrationV2ThreadProjection,
813
type OrchestrationV2TurnItem,
@@ -149,11 +154,31 @@ describe("decideThreadResume", () => {
149154
});
150155

151156
it("builds socket fallback snapshots with a bounded timeline and history cursor", () => {
157+
const projection = timelineProjection(80);
158+
// A delivered side chat digest stays "ready", which bounded snapshots keep.
159+
const sideChatHandoff = {
160+
id: ContextHandoffId.make("side-chat-handoff"),
161+
transferId: ContextTransferId.make("side-chat-transfer"),
162+
threadId: THREAD_ID,
163+
targetRunId: RunId.make("side-chat-run"),
164+
fromProviderThreadIds: [],
165+
toProviderThreadId: ProviderThreadId.make("side-chat-provider-thread"),
166+
coveredRunOrdinals: { from: 1, to: 1 },
167+
strategy: "manual_context",
168+
status: "ready",
169+
summaryMessageId: null,
170+
summaryText: "x".repeat(8_000),
171+
delivery: { nativeThreadId: "native", status: "inline", itemIds: [] },
172+
createdByProviderInstanceId: null,
173+
createdAt: NOW,
174+
updatedAt: NOW,
175+
} satisfies OrchestrationV2ContextHandoff;
152176
const snapshot = buildBoundedThreadStreamSnapshot({
153177
snapshotSequence: 44,
154-
projection: timelineProjection(80),
178+
projection: { ...projection, contextHandoffs: [sideChatHandoff] },
155179
});
156180

181+
expect(snapshot.projection.contextHandoffs.map((handoff) => handoff.summaryText)).toEqual([""]);
157182
expect(snapshot.kind).toBe("snapshot");
158183
expect(snapshot.snapshotSequence).toBe(44);
159184
expect(snapshot.projection.visibleTurnItems).toHaveLength(75);

‎apps/web/src/browser/openFileInPreview.ts‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ import {
2424
setActivePreviewTab,
2525
updatePreviewServerSnapshot,
2626
} from "~/previewStateStore";
27-
import { selectSelectedRightPanelSurface, useRightPanelStore } from "~/rightPanelStore";
27+
import {
28+
rightPanelHostRef,
29+
selectSelectedRightPanelSurface,
30+
useRightPanelStore,
31+
} from "~/rightPanelStore";
2832

2933
import { resolveBrowserDefaults } from "./browserDefaults";
3034

@@ -63,39 +67,40 @@ export async function openUrlInPreview<E>(input: {
6367
if (defaults instanceof BrowserSettingsReadError) {
6468
return AsyncResult.failure(Cause.fail(defaults));
6569
}
66-
const runtime = previewRuntimeFor(input.threadRef.environmentId);
67-
const previousActiveTabId = readThreadPreviewState(input.threadRef).activeTabId;
70+
const threadRef = rightPanelHostRef(input.threadRef);
71+
const runtime = previewRuntimeFor(threadRef.environmentId);
72+
const previousActiveTabId = readThreadPreviewState(threadRef).activeTabId;
6873
// The server changes the active tab, not panel selection; selection changes come from the user.
6974
const selectedSurface = () =>
70-
selectSelectedRightPanelSurface(useRightPanelStore.getState().byThreadKey, input.threadRef)
71-
?.id ?? null;
75+
selectSelectedRightPanelSurface(useRightPanelStore.getState().byThreadKey, threadRef)?.id ??
76+
null;
7277
const surfaceBeforeOpen = selectedSurface();
7378
const result = await input.openPreview({
74-
environmentId: input.threadRef.environmentId,
79+
environmentId: threadRef.environmentId,
7580
input: {
76-
threadId: input.threadRef.threadId,
81+
threadId: threadRef.threadId,
7782
url: input.url,
7883
viewport: defaults.viewport,
7984
profileId: input.profileId ?? defaults.profileId,
8085
...(runtime === undefined ? {} : { runtime }),
8186
},
8287
});
8388
return mapAtomCommandResult(result, (snapshot) => {
84-
rememberPreviewUrl(input.threadRef, input.url);
89+
rememberPreviewUrl(threadRef, input.url);
8590
if (input.background) {
86-
updatePreviewServerSnapshot(input.threadRef, snapshot);
91+
updatePreviewServerSnapshot(threadRef, snapshot);
8792
// Restore focus unless the user picked a tab while opening this one.
8893
if (
8994
previousActiveTabId &&
90-
readThreadPreviewState(input.threadRef).activeTabId === snapshot.tabId &&
95+
readThreadPreviewState(threadRef).activeTabId === snapshot.tabId &&
9196
selectedSurface() === surfaceBeforeOpen
9297
) {
93-
setActivePreviewTab(input.threadRef, previousActiveTabId);
98+
setActivePreviewTab(threadRef, previousActiveTabId);
9499
}
95100
return;
96101
}
97-
applyPreviewServerSnapshot(input.threadRef, snapshot);
98-
useRightPanelStore.getState().openBrowser(input.threadRef, snapshot.tabId);
102+
applyPreviewServerSnapshot(threadRef, snapshot);
103+
useRightPanelStore.getState().openBrowser(threadRef, snapshot.tabId);
99104
});
100105
}
101106

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,9 @@ export default function ChatView(props: ChatViewProps) {
10901090
renderedRightPanelSurface,
10911091
);
10921092
const canMaximizeRightPanel = rightPanelOpen;
1093-
const rightPanelMaximized = canMaximizeRightPanel && rightPanelState.maximized === true;
1093+
// An embedded side chat has no panel; a stale maximized flag would hide its own column.
1094+
const rightPanelMaximized =
1095+
!embedded && canMaximizeRightPanel && rightPanelState.maximized === true;
10941096
const inlineRightPanelOwnsTitleBar = rightPanelOpen && !shouldUsePlanSidebarSheet;
10951097
const [threadPanelPopoverHandle] = useState(PopoverCreateHandle);
10961098
const [chatCanvasStore] = useState(createChatCanvasStore);
@@ -1499,10 +1501,6 @@ export default function ChatView(props: ChatViewProps) {
14991501
};
15001502
const downloadFileAttachmentStable = useStableCallback(downloadFileAttachment);
15011503
const openFileAttachment = (attachment: ChatFileAttachment) => {
1502-
if (parentPanel) {
1503-
parentPanel.openFileAttachment(attachment);
1504-
return;
1505-
}
15061504
if (activeThreadRef) {
15071505
useRightPanelStore.getState().openAttachment(activeThreadRef, attachment);
15081506
return;
@@ -2379,6 +2377,7 @@ export default function ChatView(props: ChatViewProps) {
23792377
pullRequestPanelTarget,
23802378
threadPanelPresentation,
23812379
isServerThread,
2380+
embedded,
23822381
isGitRepo,
23832382
browserAvailable,
23842383
canOperatePreview,
@@ -3176,10 +3175,7 @@ export default function ChatView(props: ChatViewProps) {
31763175
getShortcutContext={getShortcutContextStable}
31773176
onSendAnnotation={sendPreviewAnnotationStable}
31783177
onCloseSurface={closeRightPanelSurface}
3179-
sideChatParentPanel={{
3180-
openToolDiff: onOpenToolDiffStable,
3181-
openFileAttachment: openFileAttachmentStable,
3182-
}}
3178+
sideChatParentPanel={{ openToolDiff: onOpenToolDiffStable }}
31833179
terminal={{
31843180
launchContext: activeTerminalLaunchContext ?? null,
31853181
keybindings,

‎apps/web/src/components/ReopenClosedViewShortcut.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export function ReopenClosedViewShortcut() {
9191
environmentKnown: true,
9292
catalogReady: true,
9393
ownerExists: true,
94+
sideChatExists: true,
9495
shellLive: true,
9596
panel,
9697
};
@@ -99,6 +100,10 @@ export function ReopenClosedViewShortcut() {
99100
environmentKnown: catalog.entries.has(ref.environmentId),
100101
catalogReady: catalog.isReady,
101102
ownerExists: readThreadShell(ref) !== null || drafts.getDraftThreadByRef(ref) !== null,
103+
sideChatExists:
104+
entry.kind !== "panel-tab" ||
105+
entry.surface.kind !== "side-chat" ||
106+
readThreadShell(scopeThreadRef(ref.environmentId, entry.surface.threadId)) !== null,
102107
shellLive:
103108
appAtomRegistry.get(environmentShell.stateValueAtom(ref.environmentId)).status === "live",
104109
panel,

0 commit comments

Comments
 (0)