DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(chat): compute continuation depths without recursion
Bingxi Zhao (Frank)5cff0b3e90fe
diff --git a/web/lib/message-branches.ts b/web/lib/message-branches.tsindex a9989d450..64aa44cb0 100644--- a/web/lib/message-branches.ts+++ b/web/lib/message-branches.ts@@ -101,21 +101,37 @@ function downstreamDepth<T extends BranchMessage>(if (cached !== undefined) return cachedif (activeParents.has(parentKey)) return 0+ // A long transcript must not consume the JavaScript call stack. Compute+ // depths in postorder while retaining the same cycle guard and memoization.+ const frame = (key: string) => ({ key, children: childrenByParent.get(key) ?? [], index: 0, depth: 0 })+ const stack = [frame(parentKey)]activeParents.add(parentKey)- let depth = 0- for (const child of childrenByParent.get(parentKey) ?? []) {+ while (stack.length) {+ const current = stack[stack.length - 1]+ const child = current.children[current.index++]+ if (!child) {+ stack.pop()+ activeParents.delete(current.key)+ depthByParent.set(current.key, current.depth)+ if (!stack.length) return current.depth+ const parent = stack[stack.length - 1]+ parent.depth = Math.max(parent.depth, 1 + current.depth)+ continue+ }if (child.id === undefined) {- depth = Math.max(depth, 1)+ current.depth = Math.max(current.depth, 1)continue}- depth = Math.max(- depth,- 1 + downstreamDepth(String(child.id), childrenByParent, depthByParent, activeParents)- )+ const key = String(child.id)+ const depth = depthByParent.get(key)+ if (depth !== undefined || activeParents.has(key)) {+ current.depth = Math.max(current.depth, 1 + (depth ?? 0))+ } else {+ activeParents.add(key)+ stack.push(frame(key))+ }}- activeParents.delete(parentKey)- depthByParent.set(parentKey, depth)- return depth+ return 0}export function buildVisiblePath<T extends BranchMessage>(diff --git a/web/tests/message-branches.test.ts b/web/tests/message-branches.test.tsindex c67068966..efed2e256 100644--- a/web/tests/message-branches.test.ts+++ b/web/tests/message-branches.test.ts@@ -59,3 +59,14 @@ test('an explicit branch selection still overrides the deepest path', () => {assert.equal(visible.messages.at(-1)?.content, 'accidental branch')assert.deepEqual(visible.siblingsByMessageId.get(9000)?.siblingIds, [250, 9000])})+++test("long continuations remain visible without overflowing the call stack", () => {+ const chain = Array.from({ length: 5000 }, (_, i) => ({ id: i + 1, parentMessageId: i === 0 ? null : i }))+ const newerShortBranch = { id: 6000, parentMessageId: null }+ const result = buildVisiblePath([...chain, newerShortBranch], undefined)+ assert.equal(result.messages.length, 5000)+ assert.equal(result.messages[0].id, 1)+ assert.equal(result.messages.at(-1)?.id, 5000)+ assert.deepEqual(buildVisiblePath([...chain, newerShortBranch], { null: 6000 }).messages, [newerShortBranch])+})