fix(webui): settle completed turns when fencing canonical history
This commit is contained in:
1 parent
966ed5e0bd
commit
9e499ad539
3 files changed
+92
No files matched your search
@@ -486,6 +486,11 @@ export class ClientRunLedger {
|
||||
if (!turnId) continue;
|
||||
chat ??= this.ensureChat(chatId);
|
||||
chat.fenced.add(turnId);
|
||||
// Explicit completion settles ownership even when the full page cannot be adopted.
|
||||
this.rememberClosed(chat, turnId);
|
||||
this.settle(chatId, turnId, effects);
|
||||
// Anonymous recovery activity is independent of this named turn.
|
||||
if (chat.latestTurnId === turnId) chat.effectiveStartedAt = null;
|
||||
}
|
||||
if (chat) this.trimTerminalSet(chat.fenced);
|
||||
effects.push({ kind: "prune-inbound", chatId, turnIds: Array.from(chat?.fenced ?? []) });
|
||||
|
||||
@@ -342,6 +342,8 @@ describe("NanobotClient", () => {
|
||||
? { event, chat_id: "chat", ...state }
|
||||
: { event, chat_id: "chat", recovery_state: state });
|
||||
expect(client.getRunState("chat")).toMatchObject({ active: true, unsettled: false, latestTurnId: "recovery:old-recovery" });
|
||||
client.fenceCanonicalCompletedTurns("chat", ["recovery:old-recovery"]);
|
||||
expect(client.getRunState("chat")).toMatchObject({ active: true, unsettled: false, latestTurnId: "recovery:old-recovery" });
|
||||
socket.fakeMessage({ event: "goal_status", chat_id: "chat", turn_id: "recovery:new-recovery", status: "running", started_at: 200 });
|
||||
expect(client.getRunState("chat")).toMatchObject({ active: true, unsettled: true,
|
||||
latestTurnId: "recovery:new-recovery", startedAt: 200 });
|
||||
@@ -991,6 +993,55 @@ describe("NanobotClient", () => {
|
||||
expect(client.getRunState("chat-race").startedAt).toBe(12_345);
|
||||
});
|
||||
|
||||
it.each(["canonical", "fence"] as const)("settles an older completed turn without stopping its newer owner via %s", (source) => {
|
||||
const client = new NanobotClient({ url: "ws://test", reconnect: false,
|
||||
socketFactory: (url) => new FakeSocket(url) as unknown as WebSocket });
|
||||
client.connect();
|
||||
const socket = lastSocket();
|
||||
socket.fakeOpen();
|
||||
socket.fakeMessage({ event: "goal_status", chat_id: "chat-race", turn_id: "old", status: "running", started_at: 100 });
|
||||
const generation = client.getRunGeneration("chat-race");
|
||||
socket.fakeMessage({ event: "goal_status", chat_id: "chat-race", turn_id: "new", status: "running", started_at: 200 });
|
||||
const newerRun = client.getRunState("chat-race");
|
||||
const received = vi.fn();
|
||||
client.onChat("chat-race", received);
|
||||
|
||||
if (source === "canonical") {
|
||||
expect(client.reconcileCanonicalCompletion("chat-race", generation, ["old"], {
|
||||
observedTurnIds: ["old"], hasPendingToolCalls: false, activeTurnId: null, completionIdsProvided: true,
|
||||
})).toBe(false);
|
||||
} else client.fenceCanonicalCompletedTurns("chat-race", ["old"]);
|
||||
expect(client.isRunCompleted("chat-race", "old")).toBe(true);
|
||||
expect(client.getRunState("chat-race")).toEqual(newerRun);
|
||||
socket.fakeMessage({ event: "turn_end", chat_id: "chat-race", turn_id: "old" });
|
||||
expect(client.getRunState("chat-race")).toEqual(newerRun);
|
||||
const live = { event: "delta", chat_id: "chat-race", turn_id: "new", text: "New answer" };
|
||||
socket.fakeMessage(live);
|
||||
expect(received).toHaveBeenCalledWith(live);
|
||||
socket.fakeMessage({ event: "turn_end", chat_id: "chat-race", turn_id: "new" });
|
||||
socket.fakeMessage({ event: "goal_status", chat_id: "chat-race", turn_id: "new", status: "idle" });
|
||||
expect(client.getRunState("chat-race")).toMatchObject({ active: false, unsettled: false, startedAt: null });
|
||||
expect(client.getUnsettledRunChatIds()).toEqual([]);
|
||||
client.close();
|
||||
});
|
||||
|
||||
it("notifies subscribers when a completion fence settles the latest run without its terminal frame", () => {
|
||||
const client = new NanobotClient({ url: "ws://test", reconnect: false,
|
||||
socketFactory: (url) => new FakeSocket(url) as unknown as WebSocket });
|
||||
client.connect();
|
||||
const socket = lastSocket();
|
||||
socket.fakeOpen();
|
||||
socket.fakeMessage({ event: "goal_status", chat_id: "chat", turn_id: "done", status: "running", started_at: 100 });
|
||||
const handler = vi.fn();
|
||||
client.onRunStatus(handler);
|
||||
client.fenceCanonicalCompletedTurns("chat", ["done"]);
|
||||
expect(client.getRunState("chat")).toMatchObject({ active: false, unsettled: false, latestTurnId: "done", startedAt: null });
|
||||
expect(handler).toHaveBeenLastCalledWith("chat", expect.objectContaining({ active: false, unsettled: false, startedAt: null }));
|
||||
socket.fakeMessage({ event: "goal_status", chat_id: "chat", turn_id: "done", status: "running", started_at: 999 });
|
||||
expect(client.getRunState("chat").active).toBe(false);
|
||||
client.close();
|
||||
});
|
||||
|
||||
it("rejects a user-only snapshot for a submitted turn that has not completed", () => {
|
||||
const client = new NanobotClient({
|
||||
url: "ws://test",
|
||||
|
||||
@@ -80,6 +80,42 @@ afterEach(() => {
|
||||
});
|
||||
|
||||
describe("durable run recovery", () => {
|
||||
it("settles a missed old completion from mounted history while a newer turn remains active", async () => {
|
||||
let finishRead!: (value: Response) => void;
|
||||
vi.stubGlobal("fetch", vi.fn((url: string) => String(url).includes("/webui-thread")
|
||||
? new Promise<Response>((resolve) => { finishRead = resolve; })
|
||||
: Promise.resolve(new Response("{}", { status: 404 }))));
|
||||
const { client, sockets, running, reopen, close } = setup();
|
||||
running();
|
||||
await reopen();
|
||||
act(() => running("inactive", "turn-new", 456));
|
||||
const wrapper = ({ children }: { children: ReactNode }) => (
|
||||
<ClientProvider client={client} token="renewed-token">{children}</ClientProvider>
|
||||
);
|
||||
const mounted = renderHook(() => useNanobotStream("inactive", [], false,
|
||||
undefined, undefined, [], { key: "websocket:inactive" }), { wrapper });
|
||||
const body: WebuiThreadPersistedPayload = {
|
||||
...completedThread(), schemaVersion: 3,
|
||||
completed_turn_ids: ["turn-old"], has_pending_tool_calls: true, active_turn_id: "turn-new",
|
||||
};
|
||||
body.events.push({ event: "user_message", chat_id: "inactive", turn_id: "turn-new", text: "Next task", starts_turn: true });
|
||||
await act(async () => { finishRead(response(body)); });
|
||||
expect(mounted.result.current.messages.map((message) => message.content)).toContain("The complete answer");
|
||||
expect(client.isRunCompleted("inactive", "turn-old")).toBe(true);
|
||||
expect(client.getRunState("inactive")).toMatchObject({ active: true, latestTurnId: "turn-new", startedAt: 456 });
|
||||
act(() => sockets.at(-1)!.receive({ event: "turn_end", chat_id: "inactive", turn_id: "turn-new" }));
|
||||
expect(client.isRunCompleted("inactive", "turn-new")).toBe(true);
|
||||
expect(client.getRunState("inactive")).toMatchObject({ active: false, unsettled: false, startedAt: null });
|
||||
expect(mounted.result.current.isStreaming).toBe(false);
|
||||
act(() => {
|
||||
sockets.at(-1)!.receive({ event: "turn_end", chat_id: "inactive", turn_id: "turn-old" });
|
||||
running("inactive", "turn-old", 999);
|
||||
});
|
||||
expect(mounted.result.current.isStreaming).toBe(false);
|
||||
mounted.unmount();
|
||||
close();
|
||||
});
|
||||
|
||||
it("settles an unmounted run without losing buffered or late output absent from its completed latest fragment", async () => {
|
||||
const chatId = "inactive";
|
||||
const turnId = "turn-old";
|
||||
|
||||
Reference in new issue
Block a user