Skip to content

Commit 909567c

Browse files
authored
Keep an open task card current under load, and send a parked message once the Bot is free (#1721)
* Send a parked message once the Bot is free, when the drain found it still working A message typed while the conversation read busy is parked and goes when the turn is over. The screen reads the turn over a render behind the agent: a drain started while the agent still had a run under way was refused before it asked the server anything, taken for a failed run and held until the person started a turn themselves. The message sat in the thread and never ran, with nothing said (seen once in a task's thread, a steer typed just after the task finished). The refusal is now its own error, and a drain refused for it goes again shortly until the Bot is free. A drain the server failed still waits for a turn the person starts. * Read only a conversation's own task cards when its work changes, and let a read under way finish Every work event, from any conversation, had every card on screen read its task again, and each read cancelled the one before. With five conversations working at once the events came every 100 to 300 ms and a read took longer, so a card left open never got an answer until the events paused: it read Done for up to 12 s after its task needed the person, showed no Stop, and a Stop click waiting for one timed out. The reads also kept the server busy enough that Stop answered in a second or more. Now only the cards of the conversation the event names read again (a task with no conversation that asked still reads on any), a read under way is let finish and is followed by one more, and the lists finish a read under way before reading again.
1 parent 7b4968a commit 909567c

6 files changed

Lines changed: 325 additions & 37 deletions

File tree

‎app/src/components/channels/channel-chat.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { ChannelWorkRows } from "@/components/channels/channel-work";
2828
import { attachmentModality } from "@/components/channels/chat-messages";
2929
import { toAgentOptions } from "@/components/channels/composer";
3030
import { ModelPicker } from "@/components/channels/composer/model-picker";
31+
import { StillWorkingError } from "@/components/channels/composer/queue";
3132
import { ConversationIntro } from "@/components/channels/conversation-intro";
3233
import { ConversationView } from "@/components/channels/conversation-view";
3334
import { ProgressRow } from "@/components/channels/progress-row";
@@ -1543,9 +1544,8 @@ export function ChannelChat({
15431544
agentRef.current.isRunning ||
15441545
turnsGoingAway(channel.id) > 0
15451546
) {
1546-
throw new Error(
1547-
"This Bot is still working on something. Wait for it, or stop it first.",
1548-
);
1547+
// Its own kind, so a drained queue tries again once the Bot is free (`StillWorkingError`).
1548+
throw new StillWorkingError();
15491549
}
15501550

15511551
const turn = {};

‎app/src/components/channels/composer/queue.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,3 +480,23 @@ function joinQueued(
480480
dropped,
481481
};
482482
}
483+
484+
/**
485+
* A send refused because the Bot already has the turn: nothing was asked of the server.
486+
*
487+
* Not a failed run, and the drain must not treat it as one. The conversation reads idle a render
488+
* behind the agent itself: a run can be under way on the agent (joined as the page opened, say)
489+
* while the screen still says nothing is. A drain in that gap was refused here, held back as if
490+
* the server had failed it, and waited for a turn the person would have to start: the message
491+
* they typed sat parked with nothing said, and never ran. FOUND LIVE, a steer typed into a task's
492+
* thread just after the task finished. Refused this way, what was parked goes back and is tried
493+
* again shortly, until the Bot is free and it goes; it costs no request while the Bot is busy.
494+
*/
495+
export class StillWorkingError extends Error {
496+
constructor() {
497+
super(
498+
"This Bot is still working on something. Wait for it, or stop it first.",
499+
);
500+
this.name = "StillWorkingError";
501+
}
502+
}

‎app/src/components/channels/conversation-view.tsx‎

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ import {
2424
type QueuedMessage,
2525
reduceQueue,
2626
} from "@/components/channels/composer";
27+
import { StillWorkingError } from "@/components/channels/composer/queue";
2728
import { NotifyPrompt } from "@/components/notifications/notify-prompt";
2829
import { newId } from "../../lib/new-id";
2930

31+
/** How soon a drain the Bot was still too busy for goes again. Nothing is sent while it waits. */
32+
export const STILL_WORKING_RETRY_MS = 500;
33+
3034
export function ConversationView({
3135
messages,
3236
threadId,
@@ -305,15 +309,26 @@ export function ConversationView({
305309
* The one answer to a failed send, used by both paths that can produce a run. See the drain's
306310
* `catch` for why restoring rather than releasing, and `heldBack` for why the hold.
307311
*/
312+
/**
313+
* A drain refused because the Bot still had the turn (`StillWorkingError`) asked nothing of the
314+
* server, so it is not held back: it goes again on this tick, until the Bot is free.
315+
*/
316+
const [busyRetry, setBusyRetry] = useState(0);
308317
const restoreFailedRun = useCallback(
309-
(messages: readonly QueuedMessage[]) => {
318+
(messages: readonly QueuedMessage[], error?: unknown) => {
310319
if (messages.length === 0) {
311320
return;
312321
}
322+
const stillWorking = error instanceof StillWorkingError;
313323
// Before the restore, not after: the effect reads both off refs, and the guard has to be
314324
// true by the moment the queue is non-empty again rather than one statement later.
315-
heldBack.current = true;
325+
heldBack.current = !stillWorking;
316326
apply({ messages, type: "restore" });
327+
if (stillWorking)
328+
setTimeout(
329+
() => setBusyRetry((tick) => tick + 1),
330+
STILL_WORKING_RETRY_MS,
331+
);
317332
},
318333
[apply],
319334
);
@@ -367,8 +382,8 @@ export function ConversationView({
367382
*/
368383
if (next.restoreIfRunFails.length > 0) {
369384
const carried = next.restoreIfRunFails;
370-
void started.catch(() => {
371-
restoreFailedRun(carried);
385+
void started.catch((error: unknown) => {
386+
restoreFailedRun(carried, error);
372387
});
373388
}
374389
return started;
@@ -397,8 +412,10 @@ export function ConversationView({
397412
* said is finished. The cost is that anything parked when that happens stays on screen unrun,
398413
* under a notice that explains why, which is the honest half of the trade.
399414
*
400-
* AND IT REFUSES ONCE A DRAIN HAS FAILED, until a turn starts. See `heldBack`.
415+
* AND IT REFUSES ONCE A DRAIN HAS FAILED, until a turn starts. See `heldBack`. A drain the Bot
416+
* was only too busy for is no failure: it goes again on `busyRetry`.
401417
*/
418+
// biome-ignore lint/correctness/useExhaustiveDependencies: `busyRetry` is the trigger to try a drain the Bot was too busy for again.
402419
useEffect(() => {
403420
if (inFlight) {
404421
/*
@@ -418,7 +435,7 @@ export function ConversationView({
418435
return;
419436
}
420437
const carried = next.restoreIfRunFails;
421-
void startRef.current(next.run).catch(() => {
438+
void startRef.current(next.run).catch((error: unknown) => {
422439
/*
423440
* Swallowed on purpose, and only here. A failed send from the composer throws so the composer
424441
* can put the words back in the box; the box for these is the queue they came out of, and the
@@ -450,9 +467,9 @@ export function ConversationView({
450467
* are back in the box, so the decision belongs to the call sites — this one, and the join in
451468
* `submit`, which answers the same question from the same list.
452469
*/
453-
restoreFailedRun(carried);
470+
restoreFailedRun(carried, error);
454471
});
455-
}, [apply, disabled, inFlight, restoreFailedRun]);
472+
}, [apply, disabled, inFlight, restoreFailedRun, busyRetry]);
456473

457474
return (
458475
<div className="flex flex-col flex-1 min-h-0">

‎app/src/lib/channels/use-channel-events.ts‎

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useRouter } from "@tanstack/react-router";
33
import { useEffect } from "react";
44
import { noticeChannelEvent } from "@/lib/notifications/reply-notice";
55
import { apiSocketPath, socketUrl as buildSocketUrl } from "@/lib/socket-url";
6-
import { taskKeys } from "@/lib/tasks/queries";
6+
import { type BotTask, taskKeys } from "@/lib/tasks/queries";
77
import { type BotLine, botSaidOffTheList } from "./said-off-the-list";
88
import {
99
type AgentChannel,
@@ -319,11 +319,63 @@ export function applyChannelWork(
319319
* marked stale, and read when they are next drawn.
320320
*/
321321
export function refreshTaskCards(
322-
queryClient: Pick<QueryClient, "invalidateQueries">,
322+
queryClient: QueryClient,
323323
activity: ChannelActivityEvent,
324324
): void {
325325
if (!activity.workAt) return;
326-
void queryClient.invalidateQueries({ queryKey: taskKeys.ones });
326+
readTasksOfConversation(queryClient, activity.channelId);
327+
}
328+
329+
/** Cards waiting on a read under way, to read once more when it lands. */
330+
const readAgainAfter = new Set<string>();
331+
332+
/**
333+
* THE CARDS OF ONE CONVERSATION READ THEIR TASKS AGAIN, AND A READ UNDER WAY IS NEVER THROWN AWAY.
334+
*
335+
* Every card on screen used to read its task again on any conversation's work changing, and each
336+
* read cancelled the one before it. Found under load, five conversations working at once: the
337+
* person's events came every 100 to 300 ms, a task's read took longer than that, so each card's
338+
* read was cancelled and started again until the events paused. A card left open read Done for 12 s
339+
* after its task needed the person, with no Stop on it, and every page's reads kept the server busy
340+
* enough that Stop itself answered in seconds. Now only the cards of the conversation the event is
341+
* about read again (the one that asked for a task, or the task's own), and a read already under way
342+
* is let finish, then read once more, since it may have begun before the change.
343+
*/
344+
function readTasksOfConversation(queryClient: QueryClient, channelId: string) {
345+
const cache = queryClient.getQueryCache();
346+
for (const query of cache.findAll({ queryKey: taskKeys.ones })) {
347+
const card = query.state.data as BotTask | undefined;
348+
// A task with no conversation that asked for it (a check-in's) is read on any change, as before.
349+
if (
350+
card?.originChannelId &&
351+
card.originChannelId !== channelId &&
352+
card.channelId !== channelId
353+
)
354+
continue;
355+
const hash = query.queryHash;
356+
if (query.state.fetchStatus !== "fetching") {
357+
void queryClient.invalidateQueries(
358+
{ queryKey: query.queryKey, exact: true },
359+
{ cancelRefetch: false },
360+
);
361+
continue;
362+
}
363+
if (readAgainAfter.has(hash)) continue;
364+
readAgainAfter.add(hash);
365+
const stop = cache.subscribe((event) => {
366+
if (
367+
event.query.queryHash !== hash ||
368+
event.query.state.fetchStatus === "fetching"
369+
)
370+
return;
371+
stop();
372+
readAgainAfter.delete(hash);
373+
void queryClient.invalidateQueries(
374+
{ queryKey: query.queryKey, exact: true },
375+
{ cancelRefetch: false },
376+
);
377+
});
378+
}
327379
}
328380

329381
/**
@@ -458,8 +510,17 @@ export function useChannelEvents() {
458510
* reported back, or the person told one something in its thread. Its task cards read
459511
* their tasks again, so a card open here says so at once, not on its next slow read.
460512
*/
461-
if (activity.work !== undefined && activity.busy === undefined)
462-
void queryClient.invalidateQueries({ queryKey: taskKeys.all });
513+
if (activity.work !== undefined && activity.busy === undefined) {
514+
readTasksOfConversation(queryClient, activity.channelId);
515+
// The lists (the Bots page, the sidebar's count) finish any read under way first.
516+
void queryClient.invalidateQueries(
517+
{
518+
queryKey: taskKeys.all,
519+
predicate: (query) => query.queryKey[1] !== "one",
520+
},
521+
{ cancelRefetch: false },
522+
);
523+
}
463524

464525
/*
465526
* A tab looking at the channel somebody just deleted in another tab.
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import { afterAll, afterEach, beforeAll, expect, test } from "bun:test";
2+
import { GlobalRegistrator } from "@happy-dom/global-registrator";
3+
import { cleanup, render, waitFor } from "@testing-library/react";
4+
import userEvent from "@testing-library/user-event";
5+
import type { ComposerDraft } from "@/components/channels/composer/draft";
6+
import { StillWorkingError } from "@/components/channels/composer/queue";
7+
import { ConversationView } from "@/components/channels/conversation-view";
8+
import { problem } from "./conversations-api";
9+
import { settleReactWork } from "./settle-react-work";
10+
11+
/**
12+
* A MESSAGE TYPED WHILE THE BOT HAS THE TURN IS SENT ONCE IT IS FREE, NEVER HELD WITH NOTHING SAID.
13+
*
14+
* Found live: a steer typed into a task's thread just after the task finished showed in the thread
15+
* and never ran, with no turn and no error. The conversation reads busy a render behind the agent:
16+
* the screen said the turn was over and drained what was parked, while the agent still had a run
17+
* under way, so the send was refused before it asked the server anything (`say` in
18+
* channel-chat.tsx). The drain took that refusal for a failed run and held the queue until the
19+
* person started a turn themselves, which nothing on screen asked them to do.
20+
*
21+
* `onSubmit` here refuses exactly as `say` does while the agent still runs, then sends once it is
22+
* free; everything above it is the real conversation view and composer.
23+
*/
24+
25+
const originalFetch = globalThis.fetch;
26+
beforeAll(() => {
27+
GlobalRegistrator.register({ url: "http://localhost/" });
28+
// Nothing here reaches the server: what the view reads on the side is answered Not found.
29+
globalThis.fetch = (async () =>
30+
problem(404, "Not found.")) as unknown as typeof fetch;
31+
});
32+
afterEach(cleanup);
33+
afterAll(async () => {
34+
await settleReactWork();
35+
globalThis.fetch = originalFetch;
36+
GlobalRegistrator.unregister();
37+
});
38+
39+
test("a parked message whose drain the Bot was still too busy for is sent once the Bot is free", async () => {
40+
const sent: string[] = [];
41+
/** The agent's own run, which the screen has not caught up with: busy for the first try. */
42+
let agentRunning = true;
43+
const onSubmit = async (draft: ComposerDraft) => {
44+
if (agentRunning) {
45+
// The run ends a moment later, as a joined run does.
46+
setTimeout(() => {
47+
agentRunning = false;
48+
}, 100);
49+
throw new StillWorkingError();
50+
}
51+
sent.push(draft.text);
52+
};
53+
const view = render(
54+
<ConversationView
55+
channelId="channel-1"
56+
messages={[]}
57+
onSubmit={onSubmit}
58+
// The task's last turn is still being read in, so what is typed now is parked.
59+
pending
60+
queueWhileBusy
61+
/>,
62+
);
63+
const user = userEvent.setup({ document: view.container.ownerDocument });
64+
await user.type(
65+
view.getByRole("textbox", { name: "Message" }),
66+
"Also add a line naming the worst month.",
67+
);
68+
await user.click(view.getByRole("button", { name: "Queue message" }));
69+
await waitFor(() =>
70+
expect(
71+
view.container.querySelector("[aria-label^='Remove queued message']"),
72+
).not.toBeNull(),
73+
);
74+
75+
// The screen reads the turn as over; the agent's run has a moment left.
76+
view.rerender(
77+
<ConversationView
78+
channelId="channel-1"
79+
messages={[]}
80+
onSubmit={onSubmit}
81+
queueWhileBusy
82+
/>,
83+
);
84+
85+
await waitFor(
86+
() => expect(sent).toEqual(["Also add a line naming the worst month."]),
87+
{ timeout: 3_000 },
88+
);
89+
expect(
90+
view.container.querySelector("[aria-label^='Remove queued message']"),
91+
).toBeNull();
92+
});
93+
94+
test("a drain that failed at the server still waits for a turn the person starts", async () => {
95+
const attempted: string[] = [];
96+
const onSubmit = async (draft: ComposerDraft) => {
97+
attempted.push(draft.text);
98+
throw new Error("the turn failed");
99+
};
100+
const view = render(
101+
<ConversationView
102+
channelId="channel-1"
103+
messages={[]}
104+
onSubmit={onSubmit}
105+
pending
106+
queueWhileBusy
107+
/>,
108+
);
109+
const user = userEvent.setup({ document: view.container.ownerDocument });
110+
await user.type(view.getByRole("textbox", { name: "Message" }), "Try this.");
111+
await user.click(view.getByRole("button", { name: "Queue message" }));
112+
view.rerender(
113+
<ConversationView
114+
channelId="channel-1"
115+
messages={[]}
116+
onSubmit={onSubmit}
117+
queueWhileBusy
118+
/>,
119+
);
120+
await waitFor(() => expect(attempted).toHaveLength(1));
121+
// Not sent again on its own: a server refusing every request is not retried in a loop.
122+
await new Promise((resolve) => setTimeout(resolve, 1_200));
123+
expect(attempted).toHaveLength(1);
124+
expect(
125+
view.container.querySelector("[aria-label^='Remove queued message']"),
126+
).not.toBeNull();
127+
});

0 commit comments

Comments
 (0)