Skip to content

Commit 61662b0

Browse files
fatihozdilclaude
andcommitted
fix(web): address review on code block runs
- Key each run by its fence position so identical commands do not share output. - Send the output to the thread and modes that started the command. - Finish a run when its shell exits without an end marker. - Never reuse a terminal that is still running a process. Co-Authored-By: Claude Haiku 5.5 <[email protected]>
1 parent b18f95e commit 61662b0

5 files changed

Lines changed: 33 additions & 19 deletions

File tree

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1636,7 +1636,12 @@ const CHAT_MARKDOWN_COMPONENTS = {
16361636
? onRunShellCommand
16371637
: undefined
16381638
}
1639-
shellRunScope={shellRunScope}
1639+
// The source offset tells two fences with the same command apart.
1640+
shellRunScope={
1641+
shellRunScope === undefined
1642+
? undefined
1643+
: `${shellRunScope}@${node?.position?.start.offset ?? 0}`
1644+
}
16401645
isStreaming={isStreaming}
16411646
>
16421647
{highlightedCode}

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2784,25 +2784,25 @@ export default function ChatView(props: ChatViewProps) {
27842784
});
27852785
const runCodeBlockCommand: ChatShellCommandRunner = (command, onStarted, terminalId) => {
27862786
const threadId = activeThreadId;
2787+
// Captured now: the output must go to the thread and mode that started the command.
2788+
const sendToOrigin = sendThreadMessage;
27872789
const runInTerminal = parentPanel?.runShellCommand ?? runShellCommandStable;
27882790
runInTerminal(
27892791
command,
27902792
(run) => {
27912793
onStarted(run);
27922794
if (threadId === null) return;
27932795
watchChatShellRun(run, (output) => {
2794-
void sendThreadMessageStable(threadId, chatShellRunResultMessage(command, output)).catch(
2795-
(cause) => {
2796-
toastManager.add(
2797-
stackedThreadToast({
2798-
type: "error",
2799-
title: "Couldn't send the output to Claude",
2800-
description:
2801-
cause instanceof Error ? cause.message : "An unexpected error occurred.",
2802-
}),
2803-
);
2804-
},
2805-
);
2796+
void sendToOrigin(threadId, chatShellRunResultMessage(command, output)).catch((cause) => {
2797+
toastManager.add(
2798+
stackedThreadToast({
2799+
type: "error",
2800+
title: "Couldn't send the output to Claude",
2801+
description:
2802+
cause instanceof Error ? cause.message : "An unexpected error occurred.",
2803+
}),
2804+
);
2805+
});
28062806
});
28072807
},
28082808
terminalId,

‎apps/web/src/components/chat/ChatCodeBlockRunOutput.tsx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ export function ChatCodeBlockRunOutput({ run }: { readonly run: ChatShellRun })
2121
? `Couldn't read output: ${session.error}`
2222
: exitCode !== null
2323
? `Exited with code ${exitCode}`
24-
: session.status === "closed"
25-
? "Terminal closed"
26-
: "Running…";
24+
: session.status === "exited"
25+
? "Ended without an exit status"
26+
: session.status === "closed"
27+
? "Terminal closed"
28+
: "Running…";
2729

2830
return (
2931
<div

‎apps/web/src/components/chat/chatShellRunStore.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,12 @@ export function watchChatShellRun(
4444
session.output.chunks.map((chunk) => chunk.data).join(""),
4545
run.id,
4646
);
47-
if (output.exitCode === null && session.status !== "closed" && session.error === null) return;
47+
// A shell that exited without printing the end marker is finished too, so the block stops at "Running…".
48+
const ended =
49+
session.status === "exited" || (session.status === "closed" && session.version > 0);
50+
if (output.exitCode === null && !ended && session.error === null) return;
4851
unsubscribe();
49-
if (output.exitCode === null) return;
52+
if (output.exitCode === null && session.error !== null) return;
5053
finishedRunIds.add(run.id);
5154
onFinished(output);
5255
});

‎apps/web/src/components/chat/useChatTerminalActions.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,12 @@ export function useChatTerminalActions(input: ChatTerminalActionsInput) {
231231
});
232232
}
233233
const targetCwd = options?.cwd ?? gitCwd ?? activeProject.workspaceRoot;
234+
// A busy terminal is running something else, so the run needs its own terminal.
234235
const reusableTerminalId = options?.chatShellRun
235-
? (options.chatShellRun.terminalId ?? null)
236+
? options.chatShellRun.terminalId &&
237+
!runningTerminalIds.includes(options.chatShellRun.terminalId)
238+
? options.chatShellRun.terminalId
239+
: null
236240
: options?.preferNewTerminal ||
237241
!canReuseTerminal ||
238242
!readEnvironmentScope(environmentId, AuthTerminalReadScope)

0 commit comments

Comments
 (0)