Bug Description
addSibling in src/lib/utils/tree/addSibling.ts throws an error when called with the root message's ID as the sibling target:
Error: The sibling message is the root message, therefore we can't add a sibling
This is an unnecessary restriction. A sibling of the root message is a valid operation: the new message should be inserted with an empty ancestors array (same as the root), and since there is no parent to update, no parent's children list needs to change.
Root Cause
The guard clause at lines 18–20 of addSibling.ts explicitly throws when sibling.ancestors is empty or undefined:
if (!sibling.ancestors || sibling.ancestors?.length === 0) {
throw new Error("The sibling message is the root message, therefore we can't add a sibling");
}
However, if this block is removed, the existing logic already handles the root case correctly:
sibling.ancestors is [], so the new message is pushed with ancestors: []
nearestAncestorId resolves to undefined (last element of empty array)
nearestAncestor is not found, so the children update block is skipped
Expected Behavior
addSibling(conv, newMessage, conv.rootMessageId) should succeed and return the ID of the newly created message, which has ancestors: [].
Files Involved
src/lib/utils/tree/addSibling.ts — contains the incorrect guard clause
src/lib/utils/tree/addSibling.spec.ts — has a TODO comment acknowledging this should be fixed (line 50)
Bug Description
addSiblinginsrc/lib/utils/tree/addSibling.tsthrows an error when called with the root message's ID as the sibling target:This is an unnecessary restriction. A sibling of the root message is a valid operation: the new message should be inserted with an empty
ancestorsarray (same as the root), and since there is no parent to update, no parent'schildrenlist needs to change.Root Cause
The guard clause at lines 18–20 of
addSibling.tsexplicitly throws whensibling.ancestorsis empty or undefined:However, if this block is removed, the existing logic already handles the root case correctly:
sibling.ancestorsis[], so the new message is pushed withancestors: []nearestAncestorIdresolves toundefined(last element of empty array)nearestAncestoris not found, so thechildrenupdate block is skippedExpected Behavior
addSibling(conv, newMessage, conv.rootMessageId)should succeed and return the ID of the newly created message, which hasancestors: [].Files Involved
src/lib/utils/tree/addSibling.ts— contains the incorrect guard clausesrc/lib/utils/tree/addSibling.spec.ts— has a TODO comment acknowledging this should be fixed (line 50)