fix(webui): reconcile turns after reconnect (#6211)
* fix(webui): reconcile turns after reconnect * perf(webui): bound reconnect recovery and index turn reconciliation * refactor(webui): simplify reconnect reconciliation * fix(webui): preserve turn authority during recovery * fix(webui): validate canonical completion identities
This commit is contained in:
1 parent
aeca1c0f1a
commit
fdeda8a468
18 files changed
+1741
-225
No files matched your search
@@ -70,6 +70,7 @@ function summarizeInboundWsPayload(ev: InboundEvent): unknown {
|
||||
type Unsubscribe = () => void;
|
||||
type EventHandler = (ev: InboundEvent) => void;
|
||||
type StatusHandler = (status: ConnectionStatus) => void;
|
||||
type RunCompletionHandler = (chatId: string) => void;
|
||||
type RuntimeModelHandler = (modelName: string | null, modelPreset?: string | null) => void;
|
||||
type SessionUpdateScope = "metadata" | "thread" | string;
|
||||
type SessionUpdateHandler = (
|
||||
@@ -171,6 +172,8 @@ export interface CanonicalRunSnapshot {
|
||||
hasPendingToolCalls: boolean;
|
||||
/** Exact active turn when supplied by a current gateway. */
|
||||
activeTurnId?: string | null;
|
||||
/** Supplied completion identities are authoritative for lifecycle turns. */
|
||||
completionIdsProvided?: boolean;
|
||||
}
|
||||
|
||||
type PendingMessageState = "queued" | "sent" | "unknown" | "accepted";
|
||||
@@ -198,6 +201,7 @@ export class NanobotClient {
|
||||
private sessionUpdateHandlers = new Set<SessionUpdateHandler>();
|
||||
private sidebarStateUpdateHandlers = new Set<SidebarStateUpdateHandler>();
|
||||
private runStatusHandlers = new Set<RunStatusHandler>();
|
||||
private runCompletionHandlers = new Set<RunCompletionHandler>();
|
||||
private errorHandlers = new Set<ErrorHandler>();
|
||||
// chat_id -> handlers listening on it
|
||||
private chatHandlers = new Map<string, Set<EventHandler>>();
|
||||
@@ -360,6 +364,20 @@ export class NanobotClient {
|
||||
return (this.unsettledRunTurnIdsByChatId.get(chatId)?.size ?? 0) > 0;
|
||||
}
|
||||
|
||||
/** Observe terminal wire facts without subscribing to or attaching a chat. */
|
||||
onRunCompletion(handler: RunCompletionHandler): Unsubscribe {
|
||||
this.runCompletionHandlers.add(handler);
|
||||
return () => this.runCompletionHandlers.delete(handler);
|
||||
}
|
||||
|
||||
/** Durable runs that need a canonical disposition after transport recovery. */
|
||||
getUnsettledRunChatIds(): string[] {
|
||||
return Array.from(new Set([
|
||||
...this.unsettledRunTurnIdsByChatId.keys(),
|
||||
...this.runStartedAtByChatId.keys(),
|
||||
])).filter((chatId) => !this.temporaryChatIds.has(chatId));
|
||||
}
|
||||
|
||||
private normalizeMaxFrameBytes(value: number | undefined): number | undefined {
|
||||
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) {
|
||||
return undefined;
|
||||
@@ -375,10 +393,11 @@ export class NanobotClient {
|
||||
snapshot?: CanonicalRunSnapshot,
|
||||
): boolean {
|
||||
if (completed.has(turnId)) return true;
|
||||
const pending = this.pendingMessageSends.get(this.runSendKey(chatId, turnId));
|
||||
if (snapshot?.completionIdsProvided && pending?.startsNewRun !== false) return false;
|
||||
if (!snapshot || snapshot.activeTurnId === turnId) return false;
|
||||
if (snapshot.hasPendingToolCalls) return false;
|
||||
if (observed.has(turnId)) return true;
|
||||
const pending = this.pendingMessageSends.get(this.runSendKey(chatId, turnId));
|
||||
return pending?.state === "unknown" || pending?.state === "accepted";
|
||||
}
|
||||
|
||||
@@ -770,6 +789,7 @@ export class NanobotClient {
|
||||
if (closesCurrentRun && this.runStartedAtByChatId.delete(chatId)) {
|
||||
this.emitRunStatus(chatId, null);
|
||||
}
|
||||
for (const handler of this.runCompletionHandlers) handler(chatId);
|
||||
}
|
||||
|
||||
private recordGoalStatusForRunStrip(chatId: string, ev: InboundEvent): void {
|
||||
@@ -1132,14 +1152,6 @@ export class NanobotClient {
|
||||
for (const handler of this.statusHandlers) handler(status);
|
||||
}
|
||||
|
||||
private clearRunStatusesForReconnect(): void {
|
||||
if (this.runStartedAtByChatId.size === 0) return;
|
||||
const chatIds = [...this.runStartedAtByChatId.keys()];
|
||||
this.runStartedAtByChatId.clear();
|
||||
this.runStartedAtByTurnKey.clear();
|
||||
for (const chatId of chatIds) this.emitRunStatus(chatId, null);
|
||||
}
|
||||
|
||||
private handleOpen(): void {
|
||||
this.setStatus("open");
|
||||
this.reconnectAttempts = 0;
|
||||
@@ -1530,7 +1542,6 @@ export class NanobotClient {
|
||||
}
|
||||
|
||||
private scheduleReconnect(): void {
|
||||
this.clearRunStatusesForReconnect();
|
||||
this.setStatus("reconnecting");
|
||||
const attempt = this.reconnectAttempts++;
|
||||
// Exponential backoff: 0.5s, 1s, 2s, 4s, capped.
|
||||
|
||||
Reference in new issue
Block a user