DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
Merge dev into fix/switch-branch-optimistic-id
Bingxi Zhao (Frank)0e351970b854
diff --cc web/context/UnifiedChatContext.tsxindex c6f318723,c5be30376..f3f1d3f24--- a/web/context/UnifiedChatContext.tsx+++ b/web/context/UnifiedChatContext.tsx@@@ -31,7 -31,7 +31,8 @@@ import { normalizeMarkdownForDisplay }import { normalizeMessageContent } from "@/lib/message-content";import {buildVisiblePath,+ persistedBranchSelections,+ selectChildBranch,tipMessageId,} from "@/lib/message-branches";import { nextOptimisticId, resolvePersistedMessage } from "@/lib/optimistic-id";diff --cc web/lib/message-branches.tsindex 742b41a5f,b217cf0ee..c158930e1--- a/web/lib/message-branches.ts+++ b/web/lib/message-branches.ts@@@ -155,17 -155,23 +155,34 @@@ export function buildVisiblePathreturn { 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+ * only one visible child (notably message edits). Persisting that choice with+ * the optimistic id also lets reconcileTurnIds remap it to the server id.+ */+ export function selectChildBranch(+ selectedBranches: Record<string, number>,+ parentId: number | null,+ childId: number,+ ): Record<string, number> {+ return {+ ...selectedBranches,+ [parentKey(parentId)]: childId,+ };+ }+/*** 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 --cc web/tests/optimistic-id.test.tsindex abe218a03,236f44f35..14ee8b582--- a/web/tests/optimistic-id.test.ts+++ b/web/tests/optimistic-id.test.ts@@@ -7,7 -7,7 +7,8 @@@ importimport { reconcileTurnIds } from "../lib/turn-reconcile";import {buildVisiblePath,+ persistedBranchSelections,+ selectChildBranch,tipMessageId,} from "../lib/message-branches";import type { MessageItem } from "../context/UnifiedChatContext";@@@ -108,9 -131,86 +132,93 @@@ test("the previous reply stays visible);});+ test("a freshly edited sibling overrides the previously selected branch", () => {+ const messages = [+ { id: 10, role: "user" as const, content: "q1", parentMessageId: null },+ {+ id: 11,+ role: "assistant" as const,+ content: "a1",+ parentMessageId: 10,+ },+ {+ id: 12,+ role: "user" as const,+ content: "original q2",+ parentMessageId: 11,+ },+ {+ id: -20,+ role: "user" as const,+ content: "edited q2",+ parentMessageId: 11,+ },+ ];++ const selectedBranches = selectChildBranch({ "11": 12 }, 11, -20);+ const visible = buildVisiblePath(+ messages as unknown as MessageItem[],+ selectedBranches,+ );++ assert.deepEqual(+ visible.messages.map((message) => message.content),+ ["q1", "a1", "edited q2"],+ );+ });++ test("the edited branch and its reply survive optimistic id reconciliation", () => {+ const editedId = nextOptimisticId();+ const replyId = nextOptimisticId();+ const messages = [+ { id: 10, role: "user" as const, content: "q1", parentMessageId: null },+ {+ id: 11,+ role: "assistant" as const,+ content: "a1",+ parentMessageId: 10,+ },+ {+ id: 12,+ role: "user" as const,+ content: "original q2",+ parentMessageId: 11,+ },+ {+ id: editedId,+ role: "user" as const,+ content: "edited q2",+ parentMessageId: 11,+ },+ {+ id: replyId,+ role: "assistant" as const,+ content: "edited a2",+ parentMessageId: editedId,+ events: [{ turn_id: "turn_2" }],+ },+ ];++ const reconciled = reconcileTurnIds(+ messages as unknown as MessageItem[],+ selectChildBranch({}, 11, editedId),+ { turnId: "turn_2", userMessageId: 30, assistantMessageId: 31 },+ );+ const visible = buildVisiblePath(+ reconciled.messages,+ reconciled.selectedBranches,+ );++ assert.equal(reconciled.selectedBranches["11"], 30);+ assert.deepEqual(+ visible.messages.map((message) => message.content),+ ["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 },+ );+});