Skip to content

Commit 330d3e8

Browse files
committed
fix(web): keep timeline approvals answerable
1 parent a6efeb0 commit 330d3e8

5 files changed

Lines changed: 135 additions & 50 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { useStableCallback } from "../hooks/useStableCallback";
3838
import { useProjectScriptEditing } from "./chat/useProjectScriptEditing";
3939
import { RightPanelSurfaceContent } from "./chat/RightPanelSurfaceContent";
4040
import { usePendingThreadRequests } from "./chat/usePendingThreadRequests";
41+
import { useComposerTypingGuard } from "./chat/useComposerTypingGuard";
4142
import { isScratchProject } from "@t3tools/client-runtime/state/projects";
4243
import { visibleThreadPullRequests } from "@t3tools/shared/threadPullRequests";
4344
import {
@@ -210,6 +211,7 @@ import {
210211
} from "../sidebarPendingFileDropStore";
211212
import {
212213
composerDraftHasUserContent,
214+
composerTargetKey,
213215
type ComposerFileAttachment,
214216
type ComposerImageAttachment,
215217
type DraftThreadEnvMode,
@@ -1312,6 +1314,10 @@ export default function ChatView(props: ChatViewProps) {
13121314
composerRef,
13131315
setThreadError,
13141316
});
1317+
const typingGuard = useComposerTypingGuard(composerTargetKey(composerDraftTarget), [
1318+
...pendingApprovals.map((request) => request.requestId),
1319+
...pendingUserInputs.map((request) => request.requestId),
1320+
]);
13151321
const activeProposedPlan = (() => {
13161322
if (!latestRunSettled) {
13171323
return null;
@@ -3272,6 +3278,7 @@ export default function ChatView(props: ChatViewProps) {
32723278
<div className="relative flex min-h-0 flex-1 flex-col bg-background">
32733279
<ThreadTimeline
32743280
pendingApprovals={pendingApprovals}
3281+
heldApprovalRequestIds={typingGuard.heldRequestIds}
32753282
respondingRequestIds={respondingRequestIds}
32763283
onRespondToApproval={onRespondToApprovalStable}
32773284
canOperateThread={canOperateThread}
@@ -3524,6 +3531,7 @@ export default function ChatView(props: ChatViewProps) {
35243531
environmentUnavailable={activeEnvironmentUnavailableState}
35253532
activePendingApproval={activePendingApproval}
35263533
pendingApprovals={pendingApprovals}
3534+
typingGuard={typingGuard}
35273535
pendingUserInputs={pendingUserInputs}
35283536
activePendingProgress={activePendingProgress}
35293537
activePendingResolvedAnswers={activePendingResolvedAnswers}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useComposerTypingGuard } from "./useComposerTypingGuard";
1+
import type { useComposerTypingGuard } from "./useComposerTypingGuard";
22
import { insertAttachmentReferencesForDraft } from "./composerAttachmentReferences";
33
import { DESKTOP_PASTE_AS_TEXT_EVENT } from "../../lib/desktopPasteAsText";
44
import { runtimeModeConfig, runtimeModeOptions as runtimeModes } from "./runtimeModeConfig";
@@ -401,6 +401,7 @@ export interface ChatComposerProps {
401401

402402
activePendingApproval: PendingApproval | null;
403403
pendingApprovals: PendingApproval[];
404+
typingGuard: ReturnType<typeof useComposerTypingGuard>;
404405
pendingUserInputs: PendingUserInput[];
405406
activePendingProgress: {
406407
questionIndex: number;
@@ -549,6 +550,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
549550
environmentUnavailable,
550551
activePendingApproval: incomingPendingApproval,
551552
pendingApprovals,
553+
typingGuard,
552554
pendingUserInputs: incomingPendingUserInputs,
553555
activePendingProgress: incomingPendingProgress,
554556
activePendingResolvedAnswers,
@@ -627,10 +629,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
627629
onFocus: onTypingGuardFocus,
628630
onBlur: onTypingGuardBlur,
629631
onSend: onTypingGuardSend,
630-
} = useComposerTypingGuard(composerDraftTargetKey, [
631-
...pendingApprovals.map((request) => request.requestId),
632-
...incomingPendingUserInputs.map((request) => request.requestId),
633-
]);
632+
} = typingGuard;
634633
const activePendingApproval =
635634
incomingPendingApproval && !heldRequestIds.has(incomingPendingApproval.requestId)
636635
? incomingPendingApproval

‎apps/web/src/components/chat/MessagesTimeline.browser.test.tsx‎

Lines changed: 78 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,42 +1036,39 @@ it("keeps concurrent approvals actionable outside collapsed work and restores se
10361036
responseCapability: "live",
10371037
},
10381038
];
1039-
const entries = deriveTimelineEntriesFromVisibleTurnItems({
1040-
visibleTurnItems: [
1041-
source,
1042-
...approvals.map((approval, index) => ({
1043-
...source,
1044-
position: source.position + index + 1,
1045-
sourceItemId: TurnItemId.make(`approval-${index}`),
1046-
item: {
1047-
...sourceItem,
1048-
id: TurnItemId.make(`approval-${index}`),
1049-
type: "approval_request" as const,
1050-
status: "waiting" as const,
1051-
requestId: approval.requestId,
1052-
requestKind: approval.requestKind,
1053-
prompt: approval.detail,
1054-
},
1055-
})),
1056-
],
1057-
optimisticMessages: [],
1058-
});
1039+
const entries = (completed = false) =>
1040+
deriveTimelineEntriesFromVisibleTurnItems({
1041+
visibleTurnItems: [
1042+
{ ...source, item: { ...sourceItem, status: completed ? "completed" : sourceItem.status } },
1043+
...approvals.map((approval, index) => ({
1044+
...source,
1045+
position: source.position + index + 1,
1046+
sourceItemId: TurnItemId.make(`approval-${index}`),
1047+
item: {
1048+
...sourceItem,
1049+
id: TurnItemId.make(`approval-${index}`),
1050+
type: "approval_request" as const,
1051+
status: completed ? ("completed" as const) : ("waiting" as const),
1052+
requestId: approval.requestId,
1053+
requestKind: approval.requestKind,
1054+
prompt: approval.detail,
1055+
},
1056+
})),
1057+
],
1058+
optimisticMessages: [],
1059+
});
10591060
const props = baseProps();
10601061
const onRespondToApproval = vi.fn(async () => {});
1061-
const ui = (
1062-
pendingApprovals = approvals,
1063-
canOperateThread = true,
1064-
respondingRequestIds: RuntimeRequestId[] = [],
1065-
) => (
1062+
const ui = (overrides: Partial<TimelineProps> = {}) => (
10661063
<MessagesTimeline
10671064
{...props}
1068-
timelineEntries={entries}
1069-
pendingApprovals={pendingApprovals}
1070-
respondingRequestIds={respondingRequestIds}
1071-
canOperateThread={canOperateThread}
1065+
timelineEntries={entries()}
1066+
pendingApprovals={approvals}
1067+
canOperateThread
10721068
onRespondToApproval={onRespondToApproval}
10731069
isWorking
10741070
runningRunId={fixture.runId}
1071+
{...overrides}
10751072
/>
10761073
);
10771074
await using screen = await renderElement(ui());
@@ -1081,6 +1078,12 @@ it("keeps concurrent approvals actionable outside collapsed work and restores se
10811078
.toBeVisible();
10821079
await expect.element(screen.getByText(approvals[0]!.detail!, { exact: true })).toBeVisible();
10831080
await expect.element(screen.getByText("Needs permission to run the command")).toBeVisible();
1081+
expect(
1082+
getComputedStyle(screen.getByLabelText("Command", { exact: true }).element()).userSelect,
1083+
).toBe("text");
1084+
expect(
1085+
getComputedStyle(screen.getByText("Needs permission to run the command").element()).userSelect,
1086+
).toBe("text");
10841087
await expect.element(screen.getByText("Safari", { exact: true })).toBeVisible();
10851088
await click(screen.button("Why am I being asked this?"));
10861089
await expect.element(page.getByText(/an ask rule in the agent's own settings/)).toBeVisible();
@@ -1097,24 +1100,66 @@ it("keeps concurrent approvals actionable outside collapsed work and restores se
10971100
expect(onRespondToApproval).toHaveBeenLastCalledWith(commandRequest, "decline");
10981101
await click(screen.button("Allow once"));
10991102
expect(onRespondToApproval).toHaveBeenLastCalledWith(appRequest, "accept");
1100-
await screen.rerender(ui(approvals, false));
1103+
for (const timelineEntries of [entries(), []]) {
1104+
await screen.rerender(
1105+
ui({ timelineEntries, heldApprovalRequestIds: new Set([commandRequest]) }),
1106+
);
1107+
await expect
1108+
.element(screen.getByRole("group", { name: "Command approval" }))
1109+
.not.toBeInTheDocument();
1110+
expect(screen.buttons("Approve")).toHaveLength(0);
1111+
await expect
1112+
.element(screen.getByRole("button", { name: "Allow once", exact: true }))
1113+
.toBeVisible();
1114+
await screen.rerender(ui({ timelineEntries }));
1115+
await expect.element(screen.getByRole("group", { name: "Command approval" })).toBeVisible();
1116+
expect(screen.buttons("Approve")).toHaveLength(1);
1117+
}
1118+
await screen.rerender(ui({ timelineEntries: [], isWorking: false }));
1119+
await expect.element(screen.getByRole("group", { name: "Command approval" })).toBeVisible();
1120+
await click(screen.button("Approve"));
1121+
expect(onRespondToApproval).toHaveBeenLastCalledWith(commandRequest, "accept");
1122+
await click(screen.button("Deny"));
1123+
expect(onRespondToApproval).toHaveBeenLastCalledWith(appRequest, "decline");
1124+
await screen.rerender(
1125+
ui({
1126+
timelineEntries: [],
1127+
isWorking: false,
1128+
pendingApprovals: [{ ...approvals[0]!, detail: "" }],
1129+
}),
1130+
);
1131+
await expect
1132+
.element(screen.getByLabelText("Command", { exact: true }))
1133+
.toHaveTextContent("Command approval");
1134+
await screen.rerender(ui({ canOperateThread: false }));
11011135
await expect.element(screen.getByRole("button", { name: "Approve", exact: true })).toBeDisabled();
11021136
expect(
11031137
screen.buttons("More approval options").every((button) => button.hasAttribute("disabled")),
11041138
).toBe(true);
1105-
await screen.rerender(ui(approvals, true, [commandRequest]));
1139+
await screen.rerender(ui({ respondingRequestIds: [commandRequest] }));
11061140
await expect.element(screen.getByRole("button", { name: "Approve", exact: true })).toBeDisabled();
11071141
await expect
11081142
.element(screen.getByRole("button", { name: "Allow once", exact: true }))
11091143
.toBeEnabled();
1110-
await screen.rerender(ui([{ ...approvals[0]!, responseCapability: "not_resumable" }]));
1144+
await screen.rerender(
1145+
ui({ pendingApprovals: [{ ...approvals[0]!, responseCapability: "not_resumable" }] }),
1146+
);
11111147
await expect
11121148
.element(screen.getByText("Provider process is gone. Interrupt or restart the run to respond."))
11131149
.toBeVisible();
11141150
expect(screen.buttons("Approve")).toHaveLength(0);
1115-
await screen.rerender(ui([]));
1151+
await screen.rerender(
1152+
ui({ pendingApprovals: [], timelineEntries: entries(true), isWorking: false }),
1153+
);
11161154
await expect
11171155
.element(screen.getByRole("group", { name: "Command approval" }))
11181156
.not.toBeInTheDocument();
11191157
expect(screen.text()).not.toContain("Waiting for approval");
1158+
await screen.getByRole("button", { expanded: false }).click();
1159+
await expect
1160+
.element(screen.getByRole("button", { name: approvals[0]!.detail!, exact: true }))
1161+
.toBeVisible();
1162+
await expect
1163+
.element(screen.getByRole("button", { name: "Open the current tab", exact: true }))
1164+
.toBeVisible();
11201165
});

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

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ThreadFindTimelineContext } from "./ThreadFindProvider";
33
import { MarkdownFindContext } from "./markdownFindContext";
44
import { useChatCanvas } from "./ChatCanvasContext";
55
import { WorkLogBlock } from "./WorkLog";
6+
import { TimelinePendingApprovalRow } from "./TimelinePendingApprovalRow";
67
import type { WorktreeSetupSnapshot } from "@t3tools/contracts";
78
import { deriveTimelineMinimapItems } from "./timelineMinimapItems";
89
import {
@@ -201,6 +202,7 @@ export interface MessagesTimelineHistoryControls {
201202

202203
interface MessagesTimelineProps {
203204
pendingApprovals?: ReadonlyArray<PendingApproval>;
205+
heldApprovalRequestIds?: ReadonlySet<string> | undefined;
204206
respondingRequestIds?: TimelineRowSharedState["respondingRequestIds"];
205207
canOperateThread?: boolean;
206208
onRespondToApproval?: TimelineRowSharedState["onRespondToApproval"];
@@ -317,6 +319,7 @@ export function MessagesTimeline(props: MessagesTimelineProps) {
317319

318320
const ConversationTimeline = memo(function ConversationTimeline({
319321
pendingApprovals = EMPTY_PENDING_APPROVALS,
322+
heldApprovalRequestIds,
320323
respondingRequestIds = EMPTY_RESPONDING_REQUEST_IDS,
321324
canOperateThread = false,
322325
onRespondToApproval,
@@ -669,7 +672,9 @@ const ConversationTimeline = memo(function ConversationTimeline({
669672
: paintedExpandedAttemptIds;
670673

671674
const pendingApprovalsByRequestId = new Map(
672-
pendingApprovals.map((approval) => [approval.requestId, approval]),
675+
pendingApprovals
676+
.filter((approval) => !heldApprovalRequestIds?.has(approval.requestId))
677+
.map((approval) => [approval.requestId, approval]),
673678
);
674679
const pendingApprovalRequestIds = new Set(pendingApprovalsByRequestId.keys());
675680
const rawRows = useIncrementalTimelineRows(`${listIdentityKey}\0${workspaceRoot ?? ""}`, {
@@ -690,6 +695,17 @@ const ConversationTimeline = memo(function ConversationTimeline({
690695
worktreeSetup,
691696
});
692697
const rows = useStableRows(rawRows, listIdentityKey);
698+
const unmatchedPendingApprovals = new Map(pendingApprovalsByRequestId);
699+
if (unmatchedPendingApprovals.size > 0) {
700+
for (const row of rows) {
701+
const entries =
702+
row.kind === "work" ? row.groupedEntries : row.kind === "work-live" ? [row.entry] : [];
703+
for (const entry of entries) {
704+
const item = entry.projectedItem?.item;
705+
if (item?.type === "approval_request") unmatchedPendingApprovals.delete(item.requestId);
706+
}
707+
}
708+
}
693709
// Run status/timestamps change per stream event; keep them out of shared row context.
694710
const runs = useStableHandoffRuns(runsProp);
695711
const minimapItems = deriveTimelineMinimapItems(rows);
@@ -702,6 +718,7 @@ const ConversationTimeline = memo(function ConversationTimeline({
702718
restoringThreadPosition && restoreRowIndex >= 0 ? { indices: [restoreRowIndex] } : undefined;
703719
const showsEmptyState =
704720
rows.length === 0 &&
721+
unmatchedPendingApprovals.size === 0 &&
705722
!isWorking &&
706723
parentThreadLink === null &&
707724
historyControls === undefined &&
@@ -743,6 +760,7 @@ const ConversationTimeline = memo(function ConversationTimeline({
743760
listRef,
744761
rememberedPosition,
745762
restoreReady,
763+
viewportRef,
746764
]);
747765

748766
const [timelineViewportElement, setTimelineViewportElement] = useState<HTMLDivElement | null>(
@@ -828,15 +846,6 @@ const ConversationTimeline = memo(function ConversationTimeline({
828846
size: true,
829847
shouldRestorePosition: shouldRestoreVisibleContentPosition,
830848
};
831-
const timelineListFooter = (
832-
<TimelineListFooter
833-
composerInset={anchoredEndSpace ? 0 : contentInsetEndAdjustment}
834-
onGrow={onFooterGrow}
835-
>
836-
{footer}
837-
</TimelineListFooter>
838-
);
839-
840849
const measureContentOverflow = () =>
841850
timelineContentOverflowsViewport(listRef.current?.getState?.(), {
842851
composerInset: contentInsetEndAdjustment,
@@ -1259,6 +1268,30 @@ const ConversationTimeline = memo(function ConversationTimeline({
12591268
</TimelineRowCtx>
12601269
);
12611270
const listExtraData = { identity: listIdentityKey, rowCount: rows.length, sharedState };
1271+
const timelineListFooter = (
1272+
<TimelineListFooter
1273+
composerInset={anchoredEndSpace ? 0 : contentInsetEndAdjustment}
1274+
onGrow={onFooterGrow}
1275+
>
1276+
{footer}
1277+
{unmatchedPendingApprovals.size > 0 ? (
1278+
<TimelineRowCtx value={sharedState}>
1279+
<div className="messages-timeline-row-frame">
1280+
<div
1281+
className="@container/chat-lane chat-content-lane overflow-x-clip"
1282+
data-timeline-root="true"
1283+
>
1284+
<WorkLogBlock>
1285+
{[...unmatchedPendingApprovals.values()].map((approval) => (
1286+
<TimelinePendingApprovalRow key={approval.requestId} approval={approval} />
1287+
))}
1288+
</WorkLogBlock>
1289+
</div>
1290+
</div>
1291+
</TimelineRowCtx>
1292+
) : null}
1293+
</TimelineListFooter>
1294+
);
12621295

12631296
const forwardMinimapWheel = (event: WheelEvent) => {
12641297
const scrollNode = listRef.current?.getScrollableNode();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export const TimelinePendingApprovalRow = memo(function TimelinePendingApprovalR
5656
label={
5757
<code
5858
aria-label={presentation.detailLabel}
59-
className="block max-h-40 min-w-0 overflow-auto whitespace-pre-wrap font-mono text-xs leading-5 text-foreground wrap-anywhere [scrollbar-width:thin] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring/70"
59+
className="block max-h-40 min-w-0 overflow-auto whitespace-pre-wrap font-mono text-xs leading-5 text-foreground select-text! wrap-anywhere [scrollbar-width:thin] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring/70"
6060
data-approval-detail="complete"
6161
tabIndex={0}
6262
>
@@ -69,7 +69,7 @@ export const TimelinePendingApprovalRow = memo(function TimelinePendingApprovalR
6969
<span className="text-muted-foreground wrap-anywhere">{approval.appName}</span>
7070
) : null}
7171
{approval.reason ? (
72-
<span className="text-muted-foreground wrap-anywhere">{approval.reason}</span>
72+
<span className="text-muted-foreground select-text wrap-anywhere">{approval.reason}</span>
7373
) : null}
7474
<div className="flex flex-wrap items-center gap-x-2 gap-y-1">
7575
{canRespond ? (

0 commit comments

Comments
 (0)