Merge pull request #1049 from ZQR1101/fix/switch-branch-optimistic-id

This commit is contained in:
Bingxi Zhao
2026-08-29 11:53:57 +08:00
committed by GitHub
3 changed files with 24 additions and 3 deletions
+5 -3
View File
@@ -31,6 +31,7 @@ import { normalizeMarkdownForDisplay } from "@/lib/markdown-display";
import { normalizeMessageContent } from "@/lib/message-content";
import {
buildVisiblePath,
persistedBranchSelections,
selectChildBranch,
tipMessageId,
} from "@/lib/message-branches";
@@ -1988,11 +1989,12 @@ export function UnifiedChatProvider({
childId,
});
const sessionId = session.sessionId;
if (!sessionId) return;
const nextSelections = {
if (!sessionId || childId < 0) return;
const nextSelections = persistedBranchSelections({
...session.selectedBranches,
[parentKey]: childId,
};
});
if (Object.keys(nextSelections).length === 0) return;
// Fire-and-forget — local state is the source of truth for the UI;
// the server copy only matters for reload-time hydration.
updateBranchSelection(sessionId, nextSelections).catch((err) => {
+11
View File
@@ -155,6 +155,17 @@ export function buildVisiblePath(
return { messages: visible, siblingsByMessageId };
}
/** Branch picks safe to persist — optimistic (negative) ids are client-only. */
export function persistedBranchSelections(
selections: Record<string, number>,
): Record<string, number> {
const result: Record<string, number> = {};
for (const [key, id] of Object.entries(selections)) {
if (Number.isInteger(id) && id > 0) result[key] = id;
}
return result;
}
/** Select a freshly-created child at a branch point.
*
* Sending a message can create a sibling even when the parent previously had
+8
View File
@@ -7,6 +7,7 @@ import {
import { reconcileTurnIds } from "../lib/turn-reconcile";
import {
buildVisiblePath,
persistedBranchSelections,
selectChildBranch,
tipMessageId,
} from "../lib/message-branches";
@@ -214,3 +215,10 @@ test("the edited branch and its reply survive optimistic id reconciliation", ()
["q1", "a1", "edited q2", "edited a2"],
);
});
test("persisted branch selections drop optimistic ids", () => {
assert.deepEqual(
persistedBranchSelections({ null: -42, "10": 11, "11": -99, "12": 13 }),
{ "10": 11, "12": 13 },
);
});