[GH-ISSUE #2468] [Issue]: forkRoleplayScene drops three guards main enforces (upToMessageId validation, convert+limit rejection, chronological sort) #1211

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

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

Originally assigned to: @SpicyMarinara on GitHub.

Summary

Severity: Medium — correctness / data-loss regression. forkRoleplayScene dropped three guards main enforces: it does not validate upToMessageId, does not reject convert + upToMessageId, and does not sort messages chronologically before slicing. The first silently clones the whole scene on a stale id; the second can permanently delete scene messages.

Expected behavior

Mirror main's scene fork route: reject an upToMessageId that is not in the scene (400), reject convert combined with upToMessageId, and sort messages by createdAt before applying the cut.

Actual behavior

// src/engine/modes/roleplay/scene/scene-service.ts:280-355 (forkRoleplayScene)
// (a) only mode is validated (281-283); no check that upToMessageId exists in the scene
for (const message of await messagesForChat(storage, input.sceneChatId)) {  // 315 — raw storage order, no createdAt sort
  const stopAfterThis = input.upToMessageId && message.id === input.upToMessageId;
  // ...copy...
  if (stopAfterThis) break;
}
// (b) convert deletes the entire source regardless of upToMessageId:
if (input.mode === "convert") { cleanOriginScenePointers(...); await deleteChatWithMessages(storage, input.sceneChatId); }  // 349-352
  • (a) A bogus/stale upToMessageId never matches, so stopAfterThis is never true and the entire scene is copied — "clone from here" silently becomes "clone everything".
  • (b) convert + a mid-scene upToMessageId copies only a prefix and then deletes the whole source chat — every message after the cut is permanently lost. The contract docstring (scene.ts:124) even says upToMessageId is "Clone only", but the impl never enforces it.
  • (c) The copy iterates listChatMessages in raw storage order with no createdAt sort, so if storage order ever diverges from chronological order the wrong prefix is copied.

Steps to reproduce

  1. (a) Clone-from-here on a message that was just deleted/swiped away → the whole scene is cloned instead of the prefix.
  2. (b) Call forkRoleplayScene with { mode: "convert", upToMessageId: <mid-scene id> } → only a prefix survives, the rest of the scene is deleted.

Impact area

  • Roleplay mode
  • React-free engine behavior, contracts, agents, or capability ports

Environment

  • Branch or commit: refactor @ 1fae2c42
  • Run mode: Tauri desktop / web shell
  • OS + version: found via static source audit (not runtime-reproduced)
  • Node + pnpm versions: n/a (static audit)
  • Rust version, if relevant: n/a (static audit)
  • Remote Runtime URL configured? not relevant
  • Provider, model, or integration, if relevant: n/a

Logs, screenshots, or video

Static trace: scene-service.ts:280-355, messagesForChat at :674-677, contract note scene.ts:124.

Regression notes

Regression vs main. packages/server/src/routes/scene.routes.ts guards all three: rejects an unknown upToMessageId ("Message is not part of this scene", ~562), rejects convert + limit ("Convert cannot be limited to a message", ~535), and sorts by createdAt before slicing (~556-560, with an explanatory comment). The refactor dropped all three.

Additional context

Note (b) is a latent guard-drop: no shipped call site currently passes convert + upToMessageId together (the convert UI prop carries no upToMessageId), but the service signature accepts the combination, so it is a real data-loss hazard for any future caller. Fix: add the three guards at the top of forkRoleplayScene and materialize+sort messagesForChat once before the loop.

Template check

Please uncheck the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @Romuromylus on GitHub (Jun 6, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2468 Originally assigned to: @SpicyMarinara on GitHub. ## Summary **Severity: Medium — correctness / data-loss regression.** `forkRoleplayScene` dropped three guards main enforces: it does not validate `upToMessageId`, does not reject `convert` + `upToMessageId`, and does not sort messages chronologically before slicing. The first silently clones the whole scene on a stale id; the second can permanently delete scene messages. ## Expected behavior Mirror main's scene fork route: reject an `upToMessageId` that is not in the scene (400), reject `convert` combined with `upToMessageId`, and sort messages by `createdAt` before applying the cut. ## Actual behavior ```ts // src/engine/modes/roleplay/scene/scene-service.ts:280-355 (forkRoleplayScene) // (a) only mode is validated (281-283); no check that upToMessageId exists in the scene for (const message of await messagesForChat(storage, input.sceneChatId)) { // 315 — raw storage order, no createdAt sort const stopAfterThis = input.upToMessageId && message.id === input.upToMessageId; // ...copy... if (stopAfterThis) break; } // (b) convert deletes the entire source regardless of upToMessageId: if (input.mode === "convert") { cleanOriginScenePointers(...); await deleteChatWithMessages(storage, input.sceneChatId); } // 349-352 ``` - **(a)** A bogus/stale `upToMessageId` never matches, so `stopAfterThis` is never true and the *entire* scene is copied — "clone from here" silently becomes "clone everything". - **(b)** `convert` + a mid-scene `upToMessageId` copies only a prefix and then deletes the whole source chat — every message after the cut is permanently lost. The contract docstring (`scene.ts:124`) even says `upToMessageId` is "Clone only", but the impl never enforces it. - **(c)** The copy iterates `listChatMessages` in raw storage order with no `createdAt` sort, so if storage order ever diverges from chronological order the wrong prefix is copied. ## Steps to reproduce 1. (a) Clone-from-here on a message that was just deleted/swiped away → the whole scene is cloned instead of the prefix. 2. (b) Call `forkRoleplayScene` with `{ mode: "convert", upToMessageId: <mid-scene id> }` → only a prefix survives, the rest of the scene is deleted. ## Impact area - [x] Roleplay mode - [x] React-free engine behavior, contracts, agents, or capability ports ## Environment - Branch or commit: refactor @ 1fae2c42 - Run mode: Tauri desktop / web shell - OS + version: found via static source audit (not runtime-reproduced) - Node + pnpm versions: n/a (static audit) - Rust version, if relevant: n/a (static audit) - Remote Runtime URL configured? not relevant - Provider, model, or integration, if relevant: n/a ## Logs, screenshots, or video Static trace: `scene-service.ts:280-355`, `messagesForChat` at `:674-677`, contract note `scene.ts:124`. ## Regression notes Regression vs main. `packages/server/src/routes/scene.routes.ts` guards all three: rejects an unknown `upToMessageId` ("Message is not part of this scene", ~562), rejects `convert` + limit ("Convert cannot be limited to a message", ~535), and sorts by `createdAt` before slicing (~556-560, with an explanatory comment). The refactor dropped all three. ## Additional context Note (b) is a latent guard-drop: no shipped call site currently passes `convert` + `upToMessageId` together (the convert UI prop carries no `upToMessageId`), but the service signature accepts the combination, so it is a real data-loss hazard for any future caller. Fix: add the three guards at the top of `forkRoleplayScene` and materialize+sort `messagesForChat` once before the loop. ## Template check Please uncheck the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:29:06 +07:00
Author
Owner

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

Fixed on refactor in 038b7220.

forkRoleplayScene now rejects convert plus upToMessageId, validates the cutoff message, and processes messages in chronological order.

Verification: pnpm typecheck; pnpm build; pnpm check:architecture; cargo check --manifest-path src-tauri/Cargo.toml --workspace; focused Rust backup/profile tests; git diff --check; no .test.ts/.test.tsx files present.

<!-- gh-comment-id:4638721082 --> @SpicyMarinara commented on GitHub (Jun 6, 2026): Fixed on refactor in 038b7220. forkRoleplayScene now rejects convert plus upToMessageId, validates the cutoff message, and processes messages in chronological order. Verification: pnpm typecheck; pnpm build; pnpm check:architecture; cargo check --manifest-path src-tauri/Cargo.toml --workspace; focused Rust backup/profile tests; git diff --check; no .test.ts/.test.tsx files present.
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#1211
No description provided.