fix(webui): keep completed turns terminal across late events (#5991)
* fix(webui): keep completed turns terminal across late events * test(webui): align app layout client mock with completion API
This commit is contained in:
1 parent
450a85fe0e
commit
678cd8f4a7
5 files changed
+307
-27
No files matched your search
@@ -221,6 +221,8 @@ export class NanobotClient {
|
||||
private lastSocketMessageSendKey: string | null = null;
|
||||
/** Canonically completed turns whose delayed websocket frames must be ignored. */
|
||||
private canonicalCompletedTurnIdsByChatId = new Map<string, Set<string>>();
|
||||
/** Closed run identities; output/ACKs can still arrive after idle or local stop. */
|
||||
private closedRunTurnIdsByChatId = new Map<string, Set<string>>();
|
||||
private static readonly COMPLETED_TURN_FENCE_MAX = 256;
|
||||
/** Latest ``goal_state`` snapshot per ``chat_id`` (multi-session isolation). */
|
||||
private goalStateByChatId = new Map<string, GoalStateWsPayload>();
|
||||
@@ -328,6 +330,9 @@ export class NanobotClient {
|
||||
/** Clear the optimistic run state immediately after the user stops a turn. */
|
||||
finishRunLocally(chatId: string): void {
|
||||
const unsettled = [...(this.unsettledRunTurnIdsByChatId.get(chatId) ?? [])];
|
||||
const latestTurnId = this.latestRunTurnIdByChatId.get(chatId);
|
||||
if (latestTurnId) this.rememberRunCompletion(chatId, latestTurnId);
|
||||
for (const turnId of unsettled) this.rememberRunCompletion(chatId, turnId);
|
||||
for (const turnId of unsettled) this.settleRunTurn(chatId, turnId);
|
||||
this.latestRunTurnIdByChatId.delete(chatId);
|
||||
if (this.runStartedAtByChatId.delete(chatId)) {
|
||||
@@ -469,28 +474,8 @@ export class NanobotClient {
|
||||
completedTurnIds: readonly string[],
|
||||
snapshot?: CanonicalRunSnapshot,
|
||||
): boolean {
|
||||
this.fenceCanonicalCompletedTurns(chatId, completedTurnIds);
|
||||
const fences = this.canonicalCompletedTurnIdsByChatId.get(chatId) ?? new Set<string>();
|
||||
for (const turnId of completedTurnIds) {
|
||||
if (!turnId) continue;
|
||||
fences.add(turnId);
|
||||
}
|
||||
while (fences.size > NanobotClient.COMPLETED_TURN_FENCE_MAX) {
|
||||
const oldest = fences.values().next().value;
|
||||
if (typeof oldest !== "string") break;
|
||||
fences.delete(oldest);
|
||||
}
|
||||
if (fences.size > 0) this.canonicalCompletedTurnIdsByChatId.set(chatId, fences);
|
||||
const pendingInbound = this.pendingInboundByChat.get(chatId);
|
||||
if (pendingInbound) {
|
||||
const remaining = pendingInbound.filter((event) => {
|
||||
const turnId = "turn_id" in event && typeof event.turn_id === "string"
|
||||
? event.turn_id
|
||||
: null;
|
||||
return turnId === null || !fences.has(turnId);
|
||||
});
|
||||
if (remaining.length > 0) this.pendingInboundByChat.set(chatId, remaining);
|
||||
else this.pendingInboundByChat.delete(chatId);
|
||||
}
|
||||
|
||||
if (!this.canReconcileCanonicalCompletion(
|
||||
chatId,
|
||||
@@ -515,6 +500,7 @@ export class NanobotClient {
|
||||
observed,
|
||||
snapshot,
|
||||
)) continue;
|
||||
this.rememberRunCompletion(chatId, turnId);
|
||||
unsettledTurnIds.delete(turnId);
|
||||
this.clearPendingMessageSend(chatId, turnId);
|
||||
this.runStartedAtByTurnKey.delete(this.runSendKey(chatId, turnId));
|
||||
@@ -528,6 +514,32 @@ export class NanobotClient {
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Remember explicit terminal facts even when a history snapshot cannot replace live UI. */
|
||||
fenceCanonicalCompletedTurns(chatId: string, completedTurnIds: readonly string[]): void {
|
||||
const fences = this.canonicalCompletedTurnIdsByChatId.get(chatId) ?? new Set<string>();
|
||||
for (const turnId of completedTurnIds) {
|
||||
if (!turnId) continue;
|
||||
fences.add(turnId);
|
||||
}
|
||||
while (fences.size > NanobotClient.COMPLETED_TURN_FENCE_MAX) {
|
||||
const oldest = fences.values().next().value;
|
||||
if (typeof oldest !== "string") break;
|
||||
fences.delete(oldest);
|
||||
}
|
||||
if (fences.size > 0) this.canonicalCompletedTurnIdsByChatId.set(chatId, fences);
|
||||
const pendingInbound = this.pendingInboundByChat.get(chatId);
|
||||
if (pendingInbound) {
|
||||
const remaining = pendingInbound.filter((event) => {
|
||||
const turnId = "turn_id" in event && typeof event.turn_id === "string"
|
||||
? event.turn_id
|
||||
: null;
|
||||
return turnId === null || !fences.has(turnId);
|
||||
});
|
||||
if (remaining.length > 0) this.pendingInboundByChat.set(chatId, remaining);
|
||||
else this.pendingInboundByChat.delete(chatId);
|
||||
}
|
||||
}
|
||||
|
||||
/** Last ``goal_state`` payload for *chatId*, if any frame has arrived this connection. */
|
||||
getGoalState(chatId: string): GoalStateWsPayload | undefined {
|
||||
return this.goalStateByChatId.get(chatId);
|
||||
@@ -599,9 +611,22 @@ export class NanobotClient {
|
||||
|
||||
private recordCanonicalTurnOwnership(
|
||||
ev: Extract<InboundEvent, { event: "message_accepted" | "user_message" }>,
|
||||
): void {
|
||||
const activeTurnId = ev.active_turn_id;
|
||||
if (!activeTurnId) return;
|
||||
): Extract<InboundEvent, { event: "message_accepted" | "user_message" }> {
|
||||
const activeTurnId = ev.active_turn_id ?? (ev.starts_turn ? ev.turn_id : undefined);
|
||||
if (!activeTurnId) return ev;
|
||||
|
||||
if (this.isRunCompleted(ev.chat_id, activeTurnId)) {
|
||||
// The input is still valid, but its admission-time owner is already closed.
|
||||
// Undo a local new-run guess when this input was actually steering that owner.
|
||||
const pending = ev.turn_id
|
||||
? this.pendingMessageSends.get(this.runSendKey(ev.chat_id, ev.turn_id))
|
||||
: undefined;
|
||||
if (pending?.startsNewRun && ev.turn_id !== activeTurnId) {
|
||||
this.recordRunRejection(ev.chat_id, ev.turn_id);
|
||||
if (!this.hasUnsettledRun(ev.chat_id)) this.emitRunStatus(ev.chat_id, null);
|
||||
}
|
||||
return { ...ev, active_turn_id: undefined, started_at: undefined, starts_turn: false };
|
||||
}
|
||||
|
||||
// Two clients can optimistically submit while the chat still looks idle.
|
||||
// The gateway admits exactly one owner and classifies the other message as
|
||||
@@ -623,6 +648,7 @@ export class NanobotClient {
|
||||
this.runStartedAtByChatId.set(ev.chat_id, ev.started_at);
|
||||
if (previous !== ev.started_at) this.emitRunStatus(ev.chat_id, ev.started_at);
|
||||
}
|
||||
return ev;
|
||||
}
|
||||
|
||||
private recordRunRejection(chatId: string, turnId?: string): void {
|
||||
@@ -698,6 +724,22 @@ export class NanobotClient {
|
||||
);
|
||||
}
|
||||
|
||||
private isRunCompleted(chatId: string, turnId: string): boolean {
|
||||
return this.closedRunTurnIdsByChatId.get(chatId)?.has(turnId) === true
|
||||
|| this.canonicalCompletedTurnIdsByChatId.get(chatId)?.has(turnId) === true;
|
||||
}
|
||||
|
||||
private rememberRunCompletion(chatId: string, turnId: string): void {
|
||||
const closed = this.closedRunTurnIdsByChatId.get(chatId) ?? new Set<string>();
|
||||
closed.add(turnId);
|
||||
while (closed.size > NanobotClient.COMPLETED_TURN_FENCE_MAX) {
|
||||
const oldest = closed.values().next().value;
|
||||
if (typeof oldest !== "string") break;
|
||||
closed.delete(oldest);
|
||||
}
|
||||
this.closedRunTurnIdsByChatId.set(chatId, closed);
|
||||
}
|
||||
|
||||
private isSupersededRunCompletion(chatId: string, ev: InboundEvent): boolean {
|
||||
if (
|
||||
ev.event !== "turn_end"
|
||||
@@ -716,6 +758,7 @@ export class NanobotClient {
|
||||
}
|
||||
|
||||
private recordRunCompletion(chatId: string, turnId?: string): void {
|
||||
if (turnId) this.rememberRunCompletion(chatId, turnId);
|
||||
this.settleRunTurn(chatId, turnId);
|
||||
const latestRunTurnId = this.latestRunTurnIdByChatId.get(chatId);
|
||||
const closesCurrentRun = latestRunTurnId === undefined || turnId === latestRunTurnId;
|
||||
@@ -1136,8 +1179,17 @@ export class NanobotClient {
|
||||
const turnId = "turn_id" in parsed && typeof parsed.turn_id === "string"
|
||||
? parsed.turn_id
|
||||
: null;
|
||||
const correlatedChatId = (parsed as { chat_id?: string }).chat_id;
|
||||
// Fence before ownership, clocks, acceptance, or subscribers can mutate state.
|
||||
if (correlatedChatId && this.isCanonicalCompletedTurnEvent(correlatedChatId, parsed)) return;
|
||||
if (
|
||||
correlatedChatId && turnId && this.isRunCompleted(correlatedChatId, turnId)
|
||||
&& parsed.event !== "message_accepted" && parsed.event !== "user_message"
|
||||
&& parsed.event !== "message" && parsed.event !== "turn_end"
|
||||
&& !(parsed.event === "goal_status" && parsed.status === "idle")
|
||||
) return;
|
||||
if (parsed.event === "message_accepted" || parsed.event === "user_message") {
|
||||
this.recordCanonicalTurnOwnership(parsed);
|
||||
parsed = this.recordCanonicalTurnOwnership(parsed);
|
||||
}
|
||||
if (parsed.event === "message_accepted") {
|
||||
this.recordRunAcceptance(parsed.chat_id, parsed.turn_id);
|
||||
@@ -1158,7 +1210,6 @@ export class NanobotClient {
|
||||
return;
|
||||
}
|
||||
|
||||
const correlatedChatId = (parsed as { chat_id?: string }).chat_id;
|
||||
if (parsed.event === "error" && correlatedChatId && turnId) {
|
||||
this.recordRunRejection(correlatedChatId, turnId);
|
||||
if (parsed.detail !== "workspace_scope_rejected") {
|
||||
@@ -1249,7 +1300,6 @@ export class NanobotClient {
|
||||
|
||||
const chatId = (parsed as { chat_id?: string }).chat_id;
|
||||
if (chatId) {
|
||||
if (this.isCanonicalCompletedTurnEvent(chatId, parsed)) return;
|
||||
const supersededRunCompletion = this.isSupersededRunCompletion(chatId, parsed);
|
||||
this.recordGoalStatusForRunStrip(chatId, parsed);
|
||||
if (supersededRunCompletion) return;
|
||||
@@ -1485,6 +1535,7 @@ export class NanobotClient {
|
||||
this.latestRunTurnIdByChatId.delete(chatId);
|
||||
this.unsettledRunTurnIdsByChatId.delete(chatId);
|
||||
this.canonicalCompletedTurnIdsByChatId.delete(chatId);
|
||||
this.closedRunTurnIdsByChatId.delete(chatId);
|
||||
this.goalStateByChatId.delete(chatId);
|
||||
for (const key of [...this.runStartedAtByTurnKey.keys()]) {
|
||||
if (key.startsWith(`${chatId}\u0000`)) this.runStartedAtByTurnKey.delete(key);
|
||||
|
||||
Reference in new issue
Block a user