fix(webui): reconcile running turns on reconnect (#6715)

* fix(webui): reconcile running turns on reconnect

* test(webui): verify only the current turn-sync policy
This commit is contained in:
chengyongru authored and GitHub committed 2026-11-19 16:08:20 +08:00
1 parent b453de9cf6
commit 094bac0dc3
25 files changed
+455 -226

No files matched your search

+18 -1
View File
@@ -9,6 +9,7 @@ interface TurnRecord {
// Only openOrder marks unsettled ownership; a timestamp alone does not.
openOrder: number | null;
startedAt: number | null;
needsLiveCheck: boolean;
}
interface ChatRecord {
@@ -59,6 +60,7 @@ type Command =
| { kind: "transport-lost" }
| { kind: "local-stop" | "forget"; chatId: string }
| { kind: "fence" | "settle-completed-lifecycle"; chatId: string; completedTurnIds: readonly string[] }
| { kind: "reattach"; chatId: string; liveTurnIds: readonly string[] }
| { kind: "restore-pending"; chatId: string; generation: number; turnId: string | null };
export type RunEffect =
@@ -176,6 +178,7 @@ export class ClientRunLedger {
case "transport-lost":
for (const chat of this.chats.values()) {
for (const turn of chat.turns.values()) {
turn.needsLiveCheck = turn.openOrder !== null && (!turn.delivery || turn.delivery.phase === "accepted");
if (turn.delivery?.phase === "sent") {
turn.delivery = { phase: "unknown", role: turn.delivery.role };
}
@@ -207,6 +210,20 @@ export class ClientRunLedger {
chat.anonymousActive = command.turnId === null;
break;
}
case "reattach": {
const chat = this.chats.get(command.chatId);
if (!chat) break;
const live = new Set(command.liveTurnIds);
const retired: string[] = [];
for (const [turnId, turn] of chat.turns) {
if (turn.needsLiveCheck && !live.has(turnId)) retired.push(turnId);
turn.needsLiveCheck = false;
}
// Absence only retires previously observed owners, not new/uncertain sends.
// It does not report an outcome or claim that the transcript is complete.
if (retired.length > 0) this.completeTurns(command.chatId, retired, "lifecycle", effects);
break;
}
case "fence":
case "settle-completed-lifecycle":
this.completeTurns(command.chatId, command.completedTurnIds,
@@ -391,7 +408,7 @@ export class ClientRunLedger {
private ensureTurn(chat: ChatRecord, turnId: string): TurnRecord {
let turn = chat.turns.get(turnId);
if (!turn) {
turn = { delivery: null, openOrder: null, startedAt: null };
turn = { delivery: null, openOrder: null, startedAt: null, needsLiveCheck: false };
chat.turns.set(turnId, turn);
}
return turn;
+4
View File
@@ -1069,6 +1069,10 @@ export class NanobotClient {
} else {
this.knownChats.add(parsed.chat_id);
}
const live = parsed.live_turn_ids;
if (Array.isArray(live) && live.every((id) => typeof id === "string" && id.length > 0)) {
this.applyRunEffects(this.runs.transition({ kind: "reattach", chatId: parsed.chat_id, liveTurnIds: live }));
}
this.dispatch(parsed.chat_id, parsed);
return;
}
+1
View File
@@ -1565,6 +1565,7 @@ export type InboundEvent =
chat_id: string;
request_id?: string;
temporary?: boolean;
live_turn_ids?: string[];
recovery_state?: RecoveryState;
usage?: TurnUsage;
}
@@ -307,6 +307,50 @@ describe("reconnect across chat selection and transport generations", () => {
state: { active: false, unsettled: false }, unsettled: [] });
});
it.each(["failed", "fragment"] as const)("clears a completed run after reconnect with a %s history read", async (read) => {
const h = harness();
h.client.attach("alpha");
act(() => h.running("alpha", "turn-1", 100));
expect(h.client.getRunState("alpha").active).toBe(true);
await h.reopen();
await act(async () => {
const pending = h.reads.at(-1)!;
if (read === "failed") pending.fail(503);
else pending.resolve({ ...history("alpha", []), page: {
history_generation: "real-use", before_cursor: null, has_more_before: true,
record_range: { turn: 1, start: 1, end: 2 },
} });
});
expect(h.client.getRunState("alpha").active).toBe(true);
act(() => {
h.socket().receive({ event: "attached", chat_id: "alpha", live_turn_ids: [] });
h.running("alpha", "turn-1", 100); // An older running frame was still on the bus.
});
expect(h.client.getRunState("alpha").active).toBe(false);
h.close();
});
it("preserves a still-live owner and a newly submitted turn on reattach", async () => {
const h = harness();
h.client.attach("alpha");
act(() => {
h.running("alpha", "finished", 100);
h.running("alpha", "still-live", 200);
});
await h.reopen();
act(() => {
h.client.sendMessage("alpha", "new submission", undefined, { turnId: "new" });
h.socket().receive({ event: "attached", chat_id: "alpha", live_turn_ids: ["still-live"] });
});
expect(h.client.getRunState("alpha").latestTurnId).toBe("new");
expect(h.client.isRunCompleted("alpha", "new")).toBe(false);
act(() => h.socket().receive({ event: "turn_end", chat_id: "alpha", turn_id: "new" }));
expect(h.client.getRunState("alpha").active).toBe(true);
act(() => h.socket().receive({ event: "turn_end", chat_id: "alpha", turn_id: "still-live" }));
expect(h.client.getRunState("alpha").active).toBe(false);
h.close();
});
it("recovers six chats beyond the read-concurrency limit after a failed page and repeated drops", async () => {
const h = harness();
const chats = Array.from({ length: 6 }, (_, index) => `chat-${index}`);