[GH-ISSUE #2962] [Issue]: Branching with an unknown cutoff message id silently copies the entire chat #1538

Closed
opened 2026-10-21 05:38:33 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Jun 28, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2962

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

The "branch up to here" cutoff is just a loop break on a matching message id, with no check that the id actually exists. If a non-existent id is supplied, the cutoff never fires and the entire chat is copied, with no error.

How to reproduce (try it yourself)

Deterministic (raw API): POST /chats/<id>/branch with body { "upToMessageId": "does-not-exist" }.
You'll see: a 200 with a full-length branch (the whole conversation copied), no error.
Expected: a 400/404, or the branch stops at the requested message.

(In the UI this is hard to hit — the branch button lives on the very message you'd branch at — so the realistic trigger is the raw API path, or a rare delete-races-branch window.)

Where it is

packages/server/src/routes/chats.routes.ts — the branch route destructures upToMessageId at line 2977 and uses it only as a loop break at 3062-3063 (if (upToMessageId && msg.id === upToMessageId) break;), with no presence validation before the copy loop (3024-3064). The copy happens before the break each iteration, so an unmatched id degrades to "copy everything."

What's happening

There's no msgs.some(m => m.id === upToMessageId) guard, so an unknown id silently means "no cutoff" rather than failing fast.

Suggested fix

When upToMessageId is provided, verify it exists in msgs before the loop and return 400/404 if not. Alternatively compute the slice index up front and iterate only that prefix, so an unknown id is detected explicitly.


AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.

Originally created by @kolacheee on GitHub (Jun 28, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2962 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** The "branch up to here" cutoff is just a loop break on a matching message id, with no check that the id actually exists. If a non-existent id is supplied, the cutoff never fires and the **entire** chat is copied, with no error. **How to reproduce (try it yourself)** Deterministic (raw API): `POST /chats/<id>/branch` with body `{ "upToMessageId": "does-not-exist" }`. **You'll see:** a `200` with a full-length branch (the whole conversation copied), no error. **Expected:** a `400`/`404`, or the branch stops at the requested message. (In the UI this is hard to hit — the branch button lives on the very message you'd branch at — so the realistic trigger is the raw API path, or a rare delete-races-branch window.) **Where it is** `packages/server/src/routes/chats.routes.ts` — the branch route destructures `upToMessageId` at line 2977 and uses it **only** as a loop break at 3062-3063 (`if (upToMessageId && msg.id === upToMessageId) break;`), with no presence validation before the copy loop (3024-3064). The copy happens before the break each iteration, so an unmatched id degrades to "copy everything." **What's happening** There's no `msgs.some(m => m.id === upToMessageId)` guard, so an unknown id silently means "no cutoff" rather than failing fast. **Suggested fix** When `upToMessageId` is provided, verify it exists in `msgs` before the loop and return `400`/`404` if not. Alternatively compute the slice index up front and iterate only that prefix, so an unknown id is detected explicitly. --- *AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.*
Author
Owner

@SpicyMarinara commented on GitHub (Jun 29, 2026):

Fixed in staging: branch creation now validates upToMessageId before creating anything and returns 404 when the cutoff message is not in the source chat. Validated with pnpm check and pnpm regression:prompt.

<!-- gh-comment-id:4830957323 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed in staging: branch creation now validates upToMessageId before creating anything and returns 404 when the cutoff message is not in the source chat. Validated with pnpm check and pnpm regression:prompt.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1538
No description provided.