Skip to content

fix: addSibling incorrectly rejects root message as sibling target #2386

Description

@nuthalapativarun

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions