Skip to content

Commit 492e563

Browse files
tulsi-builderBasilBotclaude
committed
fix(agents): cancel a late transcript on scroll and when retargeted
A wheel or trackpad scroll before the lookup finishes now keeps the overview too, as a click or key press already did. A profile moved to another conversation while its request is pending drops the request instead of opening the new conversation's transcript later. Co-authored-by: BasilBot <399581b6165ab01343e3a1324774b1024d76942aea563468f93c975e5ee5193c@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: tulsi <[email protected]>
1 parent 82a4171 commit 492e563

3 files changed

Lines changed: 59 additions & 12 deletions

File tree

‎src/features/agents/OpenedConversation.test.tsx‎

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,15 @@ it("opens a transcript its lookup finds after the conversation was shown, once",
6666
expect(result.current.open).toBeUndefined();
6767
});
6868

69-
it("keeps the overview when the reader types or clicks before the lookup finishes", async () => {
70-
for (const input of ["keydown", "pointerdown"] as const) {
69+
it.each(["keydown", "pointerdown", "wheel"] as const)(
70+
"keeps the overview when the reader acts (%s) before the lookup finishes",
71+
async (input) => {
7172
let resolve: (row: Row) => void = () => {};
7273
const lookup = new Promise<Row>((done) => {
7374
resolve = done;
7475
});
7576
const done = vi.fn();
76-
const { result, unmount } = renderHook(() => {
77+
const { result } = renderHook(() => {
7778
const { session, searching } = useOpenedSession<Row>(
7879
[recent],
7980
conversation,
@@ -94,8 +95,47 @@ it("keeps the overview when the reader types or clicks before the lookup finishe
9495
}
9596
expect(result.current.open).toBeUndefined();
9697
expect(done).toHaveBeenCalledTimes(1);
97-
unmount();
98+
},
99+
);
100+
101+
it("drops a pending request when the profile moves to another conversation", async () => {
102+
const other = { channelId: "other", rootId: "root" };
103+
const lookups = new Map<string, (row: Row) => void>();
104+
const done = vi.fn();
105+
const { result, rerender } = renderHook(
106+
({ shown }: { shown: typeof conversation }) => {
107+
const { session, searching } = useOpenedSession<Row>(
108+
[recent],
109+
shown,
110+
(place) =>
111+
new Promise<Row>((resolve) => {
112+
lookups.set(place.channelId, resolve);
113+
}),
114+
);
115+
return useOpenTranscript<Row>(shown, (row) => row.key, {
116+
initial: session,
117+
searching,
118+
opening: 1,
119+
done,
120+
});
121+
},
122+
{ initialProps: { shown: conversation } },
123+
);
124+
try {
125+
rerender({ shown: other });
126+
expect(done).toHaveBeenCalledTimes(1);
127+
} finally {
128+
await act(async () => {
129+
lookups.get("channel")?.(found);
130+
lookups.get("other")?.({
131+
key: "other",
132+
channelId: "other",
133+
root: "root",
134+
});
135+
});
98136
}
137+
expect(result.current.open).toBeUndefined();
138+
expect(done).toHaveBeenCalledTimes(1);
99139
});
100140

101141
it("settles without opening when the lookup finds no session", async () => {

‎src/features/agents/OpenedConversation.tsx‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export function useOpenedSession<T extends Saved>(
103103
* conversation, and viewing it again brings its tab back in a tabbed host.
104104
* `request` asks for the conversation's transcript first. While its session
105105
* is still `searching`, the transcript opens once found, unless the reader
106-
* clicks or types first, so a late result never takes their focus;
106+
* clicks, types or scrolls first, so a late result never takes their place;
107107
* `request.done` reports that it was offered. A new `request.opening` asks
108108
* again, even for the conversation already shown. Closing that tab calls
109109
* `close`, since it was all that was asked for, unless the host is only
@@ -136,14 +136,18 @@ export function useOpenTranscript<T>(
136136
// The place whose asked-for session is still being looked up.
137137
const [waiting, setWaiting] = useState<string>();
138138
const moved = offered.place !== place;
139-
if (moved || (opening !== undefined && opening !== offered.opening)) {
139+
const fresh = opening !== undefined && opening !== offered.opening;
140+
if (moved || fresh) {
140141
setOffered({ place, ...(opening !== undefined && { opening }) });
141142
const asked =
142143
place && request?.initial !== undefined
143144
? { place, row: request.initial, initial: true as const }
144145
: undefined;
145146
if (moved || asked) setShown(asked);
146-
setWaiting(!asked && place && request?.searching ? place : undefined);
147+
// Only a new opening waits; moving to another conversation drops it.
148+
setWaiting(
149+
fresh && !asked && place && request?.searching ? place : undefined,
150+
);
147151
} else if (waiting !== undefined) {
148152
if (waiting !== place || !request) setWaiting(undefined);
149153
else if (request.initial !== undefined) {
@@ -152,12 +156,14 @@ export function useOpenTranscript<T>(
152156
} else if (!request.searching) setWaiting(undefined);
153157
}
154158
const pending = !!place && waiting === place;
155-
// The reader's own click or key press wins over a lookup still finishing.
159+
// The reader's own click, key press or scroll wins over a lookup still
160+
// finishing.
156161
useEffect(() => {
157162
if (!pending) return;
158163
const cancel = () => setWaiting(undefined);
159-
const events = ["pointerdown", "keydown"] as const;
160-
for (const type of events) document.addEventListener(type, cancel, true);
164+
const events = ["pointerdown", "keydown", "wheel"] as const;
165+
for (const type of events)
166+
document.addEventListener(type, cancel, { capture: true, passive: true });
161167
return () => {
162168
for (const type of events)
163169
document.removeEventListener(type, cancel, true);

‎src/features/agents/service.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,9 @@ export type AgentViewProps<Config = unknown> = {
258258
/** Where the agent was opened from, when it was opened from a conversation. */
259259
conversation?: AgentConversation;
260260
/** Present while the panel asks to show that conversation's transcript
261-
* first: an Activity tab does so, if it knows it then, and calls `done`
262-
* once it has, so the panel asks only once. */
261+
* first: an Activity tab does so if it knows it then, or once its lookup
262+
* finds it unless the reader acts first, and calls `done` once it has
263+
* decided, so the panel asks only once. */
263264
initialTranscript?: TranscriptRequest;
264265
/** Closes the panel showing the tab. */
265266
close?: () => void;

0 commit comments

Comments
 (0)