Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix: roll back failed bot fork sends in the panel

Zhehui Bifa6d31e701dc
diff --git a/src/state/fork-panel.test.ts b/src/state/fork-panel.test.ts
index c16621aa93..490535c8ce 100644
--- a/src/state/fork-panel.test.ts
+++ b/src/state/fork-panel.test.ts
@@ -1,6 +1,6 @@
import { expect, it } from "vitest";
import { addForkOptimistic, reduceForkPanel, removeForkOptimistic, type ForkPanelState } from "./fork-panel";
-import type { AppState, Message } from "./store";
+import { initialState, reducer, type AppState, type Message } from "./store";
const source: Message = { id: "source", role: "user", kind: "text", text: "Start", at: 1, parentId: null };
const panel = (): ForkPanelState => ({
@@ -30,6 +30,18 @@ it("restores the fork leaf after a failed optimistic send", () => {
expect(removed.forkPanel?.activeLeafId).toBe(source.id);
});
+it("removes a failed bot fork send before buffering background thread events", () => {
+ const start = {
+ ...initialState,
+ bots: [{ id: "bot", threadId: "main", messages: [], tasks: [{ threadId: "fork" }] }],
+ forkPanel: panel(),
+ } as unknown as AppState;
+ const sent = addForkOptimistic(start, { type: "send", botId: "bot", threadId: "fork", text: "Continue", sendId: "send-1" });
+ const removed = reducer(sent, { type: "optimisticMessageRemoved", threadId: "fork", sendId: "send-1" });
+ expect(removed.forkPanel?.messages).toEqual([source]);
+ expect(removed.forkPanel?.activeLeafId).toBe(source.id);
+});
+
it("ignores a stale load failure after retrying the same fork", () => {
const retry = reduceForkPanel(state(), { type: "openForkPanel", kind: "bot", ownerId: "bot", threadId: "fork", loadId: "load-2" });
expect(reduceForkPanel(retry, { type: "forkPanelLoadFailed", threadId: "fork", loadId: "load-1" })).toBe(retry);
diff --git a/src/state/store.tsx b/src/state/store.tsx
index f4a53a505e..3d04431226 100644
--- a/src/state/store.tsx
+++ b/src/state/store.tsx
@@ -896,6 +896,7 @@ function dismissOnboardingCard(state: AppState, botId: string): AppState {
export const CLOUD_LINK_SETTINGS = { type: "toggleAppSettings", open: true, section: "cloudAccount", cloudLink: true } as const satisfies Action;
export function reducer(state: AppState, action: Action): AppState {
+ if (action.type === "optimisticMessageRemoved") state = removeForkOptimistic(state, action);
if (action.type === "messageAdded" || action.type === "messagePatched" || action.type === "threadActive" || action.type === "optimisticMessageRemoved") {
const owner = state.bots.find((bot) => (bot.threadId !== action.threadId || bot.awaitingThreadSnapshot) && bot.tasks?.some((task) => task.threadId === action.threadId));
if (owner) {
@@ -1286,18 +1287,17 @@ export function reducer(state: AppState, action: Action): AppState {
return animated;
}
case "optimisticMessageRemoved": {
- const panelState = removeForkOptimistic(state, action);
const id = optimisticMessageId(action.sendId);
const remove = (messages: Message[], optimistic: Message) => messages
.filter((message) => message.id !== id)
.map((message) => message.optimisticThreadOrderAt === optimistic.at
? { ...message, optimisticThreadOrderAt: optimistic.optimisticThreadOrderAt }
: message);
- const bot = panelState.bots.find((candidate) => candidate.threadId === action.threadId);
+ const bot = state.bots.find((candidate) => candidate.threadId === action.threadId);
if (bot) {
const optimistic = bot.messages.find((message) => message.id === id);
- if (!optimistic) return panelState;
- const cleared = updateBot(panelState, bot.id, (current) => ({
+ if (!optimistic) return state;
+ const cleared = updateBot(state, bot.id, (current) => ({
...current,
messages: remove(current.messages, optimistic),
activeLeafId: current.activeLeafId === id
@@ -1308,12 +1308,12 @@ export function reducer(state: AppState, action: Action): AppState {
const kept = cleared.bots.find((candidate) => candidate.id === bot.id)?.messages ?? [];
return rewindThreadUpdatedAt(cleared, action.threadId, kept, task?.createdAt ?? 0, bot.hasMore ? optimistic.optimisticThreadOrderAt : undefined);
}
- const group = panelState.groups.find((candidate) => candidate.threadId === action.threadId);
+ const group = state.groups.find((candidate) => candidate.threadId === action.threadId);
const optimistic = group?.messages.find((message) => message.id === id);
- if (!group || !optimistic) return panelState;
+ if (!group || !optimistic) return state;
const cleared = {
- ...panelState,
- groups: panelState.groups.map((candidate) => candidate.id === group.id
+ ...state,
+ groups: state.groups.map((candidate) => candidate.id === group.id
? { ...candidate, messages: remove(candidate.messages, optimistic) }
: candidate),
};