🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix(conversation-flow): support optimistic update for activeBranchIndex (#10517)
Arvin Xu9b5b234571e4
diff --git a/packages/conversation-flow/src/transformation/BranchResolver.ts b/packages/conversation-flow/src/transformation/BranchResolver.tsindex 0c0210ba766..1c5cb6577b8 100644--- a/packages/conversation-flow/src/transformation/BranchResolver.ts+++ b/packages/conversation-flow/src/transformation/BranchResolver.ts@@ -11,16 +11,21 @@ import type { IdNode, Message } from '../types';export class BranchResolver {/*** Get active branch ID from IdNode structure (used in contextTree building)+ * Returns undefined for optimistic updates when the branch hasn't been created yet*/- getActiveBranchId(message: Message, idNode: IdNode): string {+ getActiveBranchId(message: Message, idNode: IdNode): string | undefined {// Priority 1: Try to get from metadata.activeBranchIndex (index-based)const activeBranchIndex = (message.metadata as any)?.activeBranchIndex;- if (- typeof activeBranchIndex === 'number' &&- activeBranchIndex >= 0 &&- activeBranchIndex < idNode.children.length- ) {- return idNode.children[activeBranchIndex].id;+ if (typeof activeBranchIndex === 'number' && activeBranchIndex >= 0) {+ // If index is within bounds, return the branch at that index+ if (activeBranchIndex < idNode.children.length) {+ return idNode.children[activeBranchIndex].id;+ }+ // Optimistic update: index === children.length means branch is being created+ if (activeBranchIndex === idNode.children.length) {+ return undefined;+ }+ // Invalid index (> children.length), ignore and continue to other strategies}// Priority 2: Infer from which branch has children@@ -36,20 +41,25 @@ export class BranchResolver {/*** Get active branch ID from flat list (used in flatList building)+ * Returns undefined for optimistic updates when the branch hasn't been created yet*/getActiveBranchIdFromMetadata(message: Message,childIds: string[],childrenMap: Map<string | null, string[]>,- ): string {+ ): string | undefined {// Priority 1: Try to get from metadata.activeBranchIndex (index-based)const activeBranchIndex = (message.metadata as any)?.activeBranchIndex;- if (- typeof activeBranchIndex === 'number' &&- activeBranchIndex >= 0 &&- activeBranchIndex < childIds.length- ) {- return childIds[activeBranchIndex];+ if (typeof activeBranchIndex === 'number' && activeBranchIndex >= 0) {+ // If index is within bounds, return the branch at that index+ if (activeBranchIndex < childIds.length) {+ return childIds[activeBranchIndex];+ }+ // Optimistic update: index === childIds.length means branch is being created+ if (activeBranchIndex === childIds.length) {+ return undefined;+ }+ // Invalid index (> childIds.length), ignore and continue to other strategies}// Priority 2: Infer from which child has descendantsdiff --git a/packages/conversation-flow/src/transformation/ContextTreeBuilder.ts b/packages/conversation-flow/src/transformation/ContextTreeBuilder.tsindex 92c5a735d9d..6143f1d239a 100644--- a/packages/conversation-flow/src/transformation/ContextTreeBuilder.ts+++ b/packages/conversation-flow/src/transformation/ContextTreeBuilder.ts@@ -185,7 +185,12 @@ export class ContextTreeBuilder {*/private createBranchNode(message: Message, idNode: IdNode): BranchNode {const activeBranchId = this.branchResolver.getActiveBranchId(message, idNode);- const activeBranchIndex = idNode.children.findIndex((child) => child.id === activeBranchId);++ // For optimistic update (activeBranchId is undefined), use children.length as the index+ // This indicates the branch is being created but doesn't exist yet+ const activeBranchIndex = activeBranchId+ ? idNode.children.findIndex((child) => child.id === activeBranchId)+ : idNode.children.length;// Each branch is a tree starting from that childconst branches = idNode.children.map((child) => {diff --git a/packages/conversation-flow/src/transformation/FlatListBuilder.ts b/packages/conversation-flow/src/transformation/FlatListBuilder.tsindex b07b1b33b30..23e207c2680 100644--- a/packages/conversation-flow/src/transformation/FlatListBuilder.ts+++ b/packages/conversation-flow/src/transformation/FlatListBuilder.ts@@ -165,6 +165,15 @@ export class FlatListBuilder {childMessages,this.childrenMap,);++ // Optimistic update: activeBranchId is undefined when branch is being created+ // In this case, just add user message without branch info and continue+ if (!activeBranchId) {+ flatList.push(message);+ processedIds.add(message.id);+ continue;+ }+const activeBranchIndex = childMessages.indexOf(activeBranchId);const userWithBranches = this.createUserMessageWithBranches(message,@@ -248,6 +257,12 @@ export class FlatListBuilder {flatList.push(message);processedIds.add(message.id);+ // Optimistic update: activeBranchId is undefined when branch is being created+ // In this case, just add assistant message and continue (no active branch yet)+ if (!activeBranchId) {+ continue;+ }+// Continue with active branch and process its messageconst activeBranchMsg = this.messageMap.get(activeBranchId);if (activeBranchMsg) {diff --git a/packages/conversation-flow/src/transformation/__tests__/BranchResolver.test.ts b/packages/conversation-flow/src/transformation/__tests__/BranchResolver.test.tsindex d02b8055440..c26e4f29ecf 100644--- a/packages/conversation-flow/src/transformation/__tests__/BranchResolver.test.ts+++ b/packages/conversation-flow/src/transformation/__tests__/BranchResolver.test.ts@@ -71,13 +71,13 @@ describe('BranchResolver', () => {expect(resolver.getActiveBranchId(message, idNode)).toBe('msg-2');});- it('should ignore invalid activeBranchIndex', () => {+ it('should return undefined for optimistic update (activeBranchIndex === children.length)', () => {const message: Message = {content: 'test',createdAt: 0,id: 'msg-1',meta: {},- metadata: { activeBranchIndex: 5 }, // out of bounds+ metadata: { activeBranchIndex: 2 }, // index = children.length (optimistic update)role: 'user',updatedAt: 0,};@@ -90,7 +90,31 @@ describe('BranchResolver', () => {id: 'msg-1',};- // Should default to first branch+ // When activeBranchIndex === children.length, it's an optimistic update+ // The branch hasn't been created yet, so return undefined+ expect(resolver.getActiveBranchId(message, idNode)).toBeUndefined();+ });++ it('should ignore activeBranchIndex when it exceeds optimistic update range', () => {+ const message: Message = {+ content: 'test',+ createdAt: 0,+ id: 'msg-1',+ meta: {},+ metadata: { activeBranchIndex: 5 }, // > children.length (invalid)+ role: 'user',+ updatedAt: 0,+ };++ const idNode: IdNode = {+ children: [+ { children: [], id: 'msg-2' },+ { children: [], id: 'msg-3' },+ ],+ id: 'msg-1',+ };++ // activeBranchIndex > children.length should be ignored, fallback to defaultexpect(resolver.getActiveBranchId(message, idNode)).toBe('msg-2');});});@@ -147,5 +171,44 @@ describe('BranchResolver', () => {expect(resolver.getActiveBranchIdFromMetadata(message, childIds, childrenMap)).toBe('msg-2');});++ it('should return undefined for optimistic update (activeBranchIndex === childIds.length)', () => {+ const message: Message = {+ content: 'test',+ createdAt: 0,+ id: 'msg-1',+ meta: {},+ metadata: { activeBranchIndex: 2 }, // index = childIds.length (optimistic update)+ role: 'user',+ updatedAt: 0,+ };++ const childIds = ['msg-2', 'msg-3'];+ const childrenMap = new Map<string | null, string[]>();++ // When activeBranchIndex === childIds.length, it's an optimistic update+ // The branch hasn't been created yet, so return undefined+ expect(+ resolver.getActiveBranchIdFromMetadata(message, childIds, childrenMap),+ ).toBeUndefined();+ });++ it('should ignore activeBranchIndex when it exceeds optimistic update range', () => {+ const message: Message = {+ content: 'test',+ createdAt: 0,+ id: 'msg-1',+ meta: {},+ metadata: { activeBranchIndex: 5 }, // > childIds.length (invalid)+ role: 'user',+ updatedAt: 0,+ };++ const childIds = ['msg-2', 'msg-3'];+ const childrenMap = new Map<string | null, string[]>();++ // activeBranchIndex > childIds.length should be ignored, fallback to default+ expect(resolver.getActiveBranchIdFromMetadata(message, childIds, childrenMap)).toBe('msg-2');+ });});});diff --git a/packages/conversation-flow/src/transformation/__tests__/ContextTreeBuilder.test.ts b/packages/conversation-flow/src/transformation/__tests__/ContextTreeBuilder.test.tsindex d8a906aaab4..5d170a27c68 100644--- a/packages/conversation-flow/src/transformation/__tests__/ContextTreeBuilder.test.ts+++ b/packages/conversation-flow/src/transformation/__tests__/ContextTreeBuilder.test.ts@@ -311,6 +311,70 @@ describe('ContextTreeBuilder', () => {expect(result).toHaveLength(0);});+ it('should set activeBranchIndex to children.length for optimistic update', () => {+ const messageMap = new Map<string, Message>([+ [+ 'msg-1',+ {+ content: 'Hello',+ createdAt: 0,+ id: 'msg-1',+ meta: {},+ // activeBranchIndex = 2 means optimistic update (pointing to not-yet-created branch)+ metadata: { activeBranchIndex: 2 },+ role: 'user',+ updatedAt: 0,+ },+ ],+ [+ 'msg-2',+ {+ content: 'Response 1',+ createdAt: 0,+ id: 'msg-2',+ meta: {},+ role: 'assistant',+ updatedAt: 0,+ },+ ],+ [+ 'msg-3',+ {+ content: 'Response 2',+ createdAt: 0,+ id: 'msg-3',+ meta: {},+ role: 'assistant',+ updatedAt: 0,+ },+ ],+ ]);++ const builder = createBuilder(messageMap);+ const idNodes: IdNode[] = [+ {+ children: [+ { children: [], id: 'msg-2' },+ { children: [], id: 'msg-3' },+ ],+ id: 'msg-1',+ },+ ];++ const result = builder.transformAll(idNodes);++ expect(result).toHaveLength(2);+ expect(result[0]).toEqual({ id: 'msg-1', type: 'message' });+ // When activeBranchIndex === children.length (optimistic update),+ // BranchResolver returns undefined, and ContextTreeBuilder uses children.length as index+ expect(result[1]).toMatchObject({+ activeBranchIndex: 2, // children.length = 2+ branches: [[{ id: 'msg-2', type: 'message' }], [{ id: 'msg-3', type: 'message' }]],+ parentMessageId: 'msg-1',+ type: 'branch',+ });+ });+it('should continue with active column children in compare mode', () => {const messageMap = new Map<string, Message>([[diff --git a/packages/conversation-flow/src/transformation/__tests__/FlatListBuilder.test.ts b/packages/conversation-flow/src/transformation/__tests__/FlatListBuilder.test.tsindex cc54a029123..37d70ba76a1 100644--- a/packages/conversation-flow/src/transformation/__tests__/FlatListBuilder.test.ts+++ b/packages/conversation-flow/src/transformation/__tests__/FlatListBuilder.test.ts@@ -507,5 +507,52 @@ describe('FlatListBuilder', () => {expect(result[1].role).toBe('assistantGroup');expect(result[2].id).toBe('msg-3');});++ it('should handle optimistic update for user message with branches', () => {+ // Scenario: User has sent a new message, activeBranchIndex points to a branch+ // that is being created but doesn't exist yet (optimistic update)+ const messages: Message[] = [+ {+ content: 'User',+ createdAt: 0,+ id: 'msg-1',+ meta: {},+ // activeBranchIndex = 2 means pointing to a not-yet-created branch (optimistic update)+ // when there are only 2 existing children (msg-2, msg-3)+ metadata: { activeBranchIndex: 2 },+ role: 'user',+ updatedAt: 0,+ },+ {+ content: 'Branch 1',+ createdAt: 0,+ id: 'msg-2',+ meta: {},+ parentId: 'msg-1',+ role: 'assistant',+ updatedAt: 0,+ },+ {+ content: 'Branch 2',+ createdAt: 0,+ id: 'msg-3',+ meta: {},+ parentId: 'msg-1',+ role: 'assistant',+ updatedAt: 0,+ },+ ];++ const builder = createBuilder(messages);+ const result = builder.flatten(messages);++ // When activeBranchIndex === children.length (optimistic update),+ // BranchResolver returns undefined, and FlatListBuilder just adds the user message+ // without branch info and doesn't continue to any branch+ expect(result).toHaveLength(1);+ expect(result[0].id).toBe('msg-1');+ // User message should not have branch info since we're in optimistic update mode+ expect((result[0] as any).siblingCount).toBeUndefined();+ });});});