Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(web): skip persisting optimistic branch selections

ZQR11017b3c9c0cff9a
diff --git a/web/context/UnifiedChatContext.tsx b/web/context/UnifiedChatContext.tsx
index d459a9a9b..c6f318723 100644
--- a/web/context/UnifiedChatContext.tsx
+++ b/web/context/UnifiedChatContext.tsx
@@ -29,7 +29,11 @@ import {
} from "@/lib/session-api";
import { normalizeMarkdownForDisplay } from "@/lib/markdown-display";
import { normalizeMessageContent } from "@/lib/message-content";
-import { buildVisiblePath, tipMessageId } from "@/lib/message-branches";
+import {
+ buildVisiblePath,
+ persistedBranchSelections,
+ tipMessageId,
+} from "@/lib/message-branches";
import { nextOptimisticId, resolvePersistedMessage } from "@/lib/optimistic-id";
import { reconcileTurnIds } from "@/lib/turn-reconcile";
import {
@@ -1977,11 +1981,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) => {
diff --git a/web/lib/message-branches.ts b/web/lib/message-branches.ts
index 713700691..742b41a5f 100644
--- a/web/lib/message-branches.ts
+++ b/web/lib/message-branches.ts
@@ -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;
+}
+
/**
* Find the most recent child id under ``parentId`` from a flat message
* list. Used after an edit to auto-select the freshly persisted sibling.
diff --git a/web/tests/optimistic-id.test.ts b/web/tests/optimistic-id.test.ts
index 6c2b105dd..abe218a03 100644
--- a/web/tests/optimistic-id.test.ts
+++ b/web/tests/optimistic-id.test.ts
@@ -5,7 +5,11 @@ import {
resolvePersistedMessage,
} from "../lib/optimistic-id";
import { reconcileTurnIds } from "../lib/turn-reconcile";
-import { buildVisiblePath, tipMessageId } from "../lib/message-branches";
+import {
+ buildVisiblePath,
+ persistedBranchSelections,
+ tipMessageId,
+} from "../lib/message-branches";
import type { MessageItem } from "../context/UnifiedChatContext";
test("optimistic ids are negative and strictly decreasing", () => {
@@ -103,3 +107,10 @@ test("the previous reply stays visible once the next turn is queued", () => {
["q1", "a1", "q2"],
);
});
+
+test("persisted branch selections drop optimistic ids", () => {
+ assert.deepEqual(
+ persistedBranchSelections({ null: -42, "10": 11, "11": -99, "12": 13 }),
+ { "10": 11, "12": 13 },
+ );
+});