Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,8 @@ jobs:
app/tests/plan-notices-layout.test.tsx
app/tests/question-answer-prompt-browser.test.tsx
app/tests/queue-send-now-layout.test.tsx
app/tests/queued-send-busy-browser.test.tsx
app/tests/queued-send-locked-browser.test.tsx
app/tests/reached-as-the-person-browser.test.tsx
app/tests/reconnecting-line-layout.test.tsx
app/tests/rejoin-fresh-token-browser.test.tsx
Expand Down
106 changes: 103 additions & 3 deletions app/src/components/channels/channel-chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ import {
stoppedReason,
TURN_CUT_OFF,
TURN_INTERRUPTED,
threadBusy,
} from "@/lib/copilot/stopped-turn";
import {
HISTORY_READ_CEILING_MS,
Expand Down Expand Up @@ -1955,6 +1956,27 @@ export function ChannelChat({
* does, and the box is left empty: handed back with Send ready, it only asked to be refused again.
*/
const turnRefused = useRef(false);
/**
* THE TURN'S FIRST RUN WAS REFUSED BECAUSE THE CONVERSATION STILL HAD A RUN GOING (`threadBusy`).
*
* Nothing ran, so nothing is said and nothing is kept: `deliver` takes the message off the screen
* and throws `StillWorkingError`, and the message waits as queued, faded, with Remove, until the
* conversation is free, then goes by itself. This is how Claude Code, GitHub Copilot's SDK, Codex
* and Open WebUI treat a message sent while a turn is still going.
*
* FOUND ON OPENBOT CLOUD, 2026-12-05. Send now stopped a turn and sent the queued message the
* moment the turn ended here, while a run on the server, carrying the stopped turn on, held the
* conversation for six seconds. The person read "Thread <id> is locked" under their message, the
* message stayed on screen as if sent, and a second copy waited as queued; removing that copy left
* the first looking sent.
*/
const lockRefused = useRef(false);
/**
* Ask the server whether a run is still going before the next turn is sent: after a Stop, and
* after a run was refused because one was. A run still going is waited for with the message
* queued, and the message is never drawn as sent in between.
*/
const askFreeFirst = useRef(false);
/**
* The run going now already said why it failed, in its RUN_ERROR. The runtime's client then fails
* the run as well (`onRunFailed`, carrying the same words), which for a turn still counted here
Expand Down Expand Up @@ -2451,6 +2473,7 @@ export function ChannelChat({
setRunError(null);
turnFailure.current = null;
turnRefused.current = false;
lockRefused.current = false;
stoppedHere.current = false;
// A Stop from here on is this turn's: it ends any run of the server's the turn follows.
const stops = stopEpoch.current;
Expand Down Expand Up @@ -2894,6 +2917,20 @@ export function ChannelChat({
watching.unsubscribe();
setRunsInFlight((count) => count - 1);
}
/*
* REFUSED BECAUSE A RUN WAS STILL GOING: nothing ran, so the message was never part of the
* conversation. It comes off the screen, and the throw parks it as queued to go again by itself
* once the conversation is free (`conversation-view.tsx`). See `lockRefused`.
*/
if (lockRefused.current) {
lockRefused.current = false;
awaitingReply.current = false;
askFreeFirst.current = true;
target.setMessages(
target.messages.filter((message) => existingMessageIds.has(message.id)),
);
throw new StillWorkingError();
}
if (!started && turnFailure.current === null && awaitingReply.current) {
awaitingReply.current = false;
// It never left this browser, so it is not part of the conversation: the words go back in the box.
Expand Down Expand Up @@ -2958,6 +2995,20 @@ export function ChannelChat({
.join("\n\n");
};

/**
* Whether the conversation is free for the next message: at once, unless a Stop or a refused run
* left a run that may still be going on the server, when the server is asked (`askFreeFirst`).
* A server that can't be asked counts as free, so a message is never held for want of an answer.
*/
const freeToSend = (): boolean | Promise<boolean> => {
if (!askFreeFirst.current) return true;
return askRunGoing(channel.id).then((going) => {
if (going.liveness === "live") return false;
askFreeFirst.current = false;
return true;
});
};

/**
* Send a user turn through the channel, including activity reporting and history repair.
*
Expand Down Expand Up @@ -2989,6 +3040,22 @@ export function ChannelChat({
// Its own kind, so a drained queue tries again once the Bot is free (`StillWorkingError`).
throw new StillWorkingError();
}
/*
* AFTER A STOP, OR A RUN REFUSED BECAUSE ONE WAS GOING, THE SERVER IS ASKED FIRST. A stopped run
* can go on for a while on the server before it lets go of the conversation, and a message sent
* into that is refused. Asked first, the message waits as queued and is never drawn as sent in
* between. A server that can't be asked is no reason to hold it: it goes.
*/
if (askFreeFirst.current) {
if (!(await freeToSend())) throw new StillWorkingError();
if (
turnsRef.current > 0 ||
agentRef.current.isRunning ||
turnsGoingAway(channel.id) > 0 ||
carriedByServerRef.current
)
throw new StillWorkingError();
}

const turn = {};
turnTokens.current.add(turn);
Expand Down Expand Up @@ -3105,6 +3172,11 @@ export function ChannelChat({
// Seen failing here is read, though the roster never hears of it (mark-read.ts).
runEndedIn(channel.id);
runErrorSaid.current = true;
// Refused because a run was still going: the turn's message waits and goes again (`lockRefused`).
if (threadBusy(event) && awaitingReply.current) {
lockRefused.current = true;
return;
}
fail(
stoppedReason(event?.message),
true,
Expand All @@ -3115,6 +3187,14 @@ export function ChannelChat({
);
},
onRunFailed: ({ error }) => {
// The turn's first run refused because a run was still going: said nowhere (`lockRefused`).
if (
threadBusy(error) &&
(awaitingReply.current || lockRefused.current)
) {
lockRefused.current = true;
return;
}
/*
* Not the Bot failing: the run's stream reached this browser out of shape, in any run of a
* turn going here. The turn reads it back from the thread once its runs are over (`deliver`).
Expand Down Expand Up @@ -3302,6 +3382,8 @@ export function ChannelChat({
awaitingReply.current = false;
stoppedHere.current = true;
stopEpoch.current++;
// The stopped run may hold the conversation on the server for a moment yet (`askFreeFirst`).
askFreeFirst.current = true;
copilotkit.stopAgent({ agent });
/*
* A TURN THE SERVER IS CARRYING ON IS STOPPED WHEREVER IT IS. Between one run and the next no run
Expand All @@ -3312,11 +3394,27 @@ export function ChannelChat({
const carried = channel.work?.find(
(entry) => isTurnWork(entry) && entry.key !== stoppedCarried,
);
if (!carried || !agent.threadId) return;
setStoppedCarried(carried.key);
const routed =
(agent as { runtimeAgentId?: string }).runtimeAgentId ?? agent.agentId;
if (!routed) return;
if (!routed || !agent.threadId) return;
/*
* AND A RUN GOING HERE IS STOPPED ON THE SERVER'S ROUTE TOO, not only on the socket. The server
* takes a person's turn to carry the moment its run answers a step itself, and a Stop that reached
* only the run let the server carry the turn on in a run of its own. FOUND ON OPENBOT CLOUD,
* 2026-12-05: the Bot went on to answer the message the person had just stopped, and that run
* held the conversation while their next message was refused. The route takes the turn off the
* server's queue. Nothing is said when it answers badly here: the run itself was stopped above.
*
* Only where the Stop above went over the socket: through the backend's event store, CopilotKit's
* agent hands its runs to an Intelligence agent of its own (`delegate`, `@copilotkit/core` 1.88)
* and stops it with `stop_run` alone. Over the plain event stream its Stop is this route already.
*/
if (!carried) {
if ((agent as { delegate?: unknown }).delegate)
void stopCarriedTurn(routed, agent.threadId).catch(() => undefined);
return;
}
setStoppedCarried(carried.key);
// The runtime's refusal is written for a program ("Thread access denied"): a person reads theirs.
stopCarriedTurn(routed, agent.threadId).catch(() => {
setStoppedCarried((stopped) =>
Expand Down Expand Up @@ -3475,6 +3573,8 @@ export function ChannelChat({
}
agents={toAgentOptions(agentProfiles, channel.agentIds)}
channelId={channel.id}
// What waits as queued goes once the conversation is free (`askFreeFirst`).
freeToSend={freeToSend}
// An unsent reply stays with this conversation when somebody looks at another one.
draftKey={`conversation:${channel.id}`}
composerOpening={composerOpenings}
Expand Down
Loading
Loading