[GH-ISSUE #3006] [Issue]: edit_chat_message silently truncates replacement content at 16 KB (wrong byte cap, no truncated flag) #1578

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

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

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

editChatMessage caps the full replacement message body to MAX_APPEND_BYTES (16 KB) using trimToUtf8Bytes, then persists the truncated text with no indication that truncation occurred. MAX_APPEND_BYTES is the per-append cap for the rolling chat-summary feature, not a message-size limit — it appears to have been copy-pasted into the message-edit path. Chat messages have no length limit in storage, and long roleplay/assistant messages routinely exceed 16 KB. When an agent with the edit_messages capability calls edit_chat_message with a replacement longer than 16 KB, the stored message is silently chopped and the tail is permanently lost. Unlike write_chat_variable (which returns truncated/bytes), edit_chat_message returns only {applied, messageId, role, reason}, so neither the model nor the user is told the edit was cut.

How to reproduce (try it yourself)

  1. Configure an agent with the edit_messages capability so edit_chat_message is enabled and replaceChatMessageContent is wired.
  2. Have the agent call edit_chat_message({ messageId: "<a real message id>", content: "<a string longer than 16384 UTF-8 bytes>" }).
  3. Inspect the persisted message.

You'll see: the message is truncated to 16 KB; the tool reports applied:true with no truncation indicator; content past 16 KB is gone permanently.

Expected: the full replacement content is stored (messages have no storage cap), consistent with every other message-writing path.

Where it is

packages/server/src/services/tools/tool-executor.ts:685 (the trimToUtf8Bytes(args.content.trim(), MAX_APPEND_BYTES) call in editChatMessage); the constant MAX_APPEND_BYTES = 16 * 1024 at line 68. The writer that persists with no truncated flag is replaceChatMessageContent in packages/server/src/services/generation/tool-resolution-runtime.ts:643-649. The manifest schema has no maxLength at packages/shared/src/features/function-calls/tools/edit-chat-message/manifest.ts:14-17.

What's happening

editChatMessage reuses MAX_APPEND_BYTES (named/intended for chat-summary appends) as the cap for a full message replacement. The cap is far smaller than realistic message sizes (lorebook content uses a 64 KB cap), and unlike other writers no truncated/bytes flag is returned, so silent data loss occurs. updateMessageContent (chats.storage.ts:843-848) does a plain db.update(messages).set({ content }) with no length limit.

Suggested fix

In tool-executor.ts editChatMessage, stop applying the chat-summary append cap to full message replacements — pass the trimmed content through unchanged:

return replaceFn({
  messageId: args.messageId.trim(),
  content: args.content.trim(),
  reason: typeof args.reason === "string" ? args.reason.trim().slice(0, 240) : undefined,
});

i.e. replace trimToUtf8Bytes(args.content.trim(), MAX_APPEND_BYTES) with args.content.trim(). This matches every other message-writing path (createMessage, updateMessageContent, continuation, prose-guardian rewrites), all of which persist uncapped content. MAX_APPEND_BYTES stays in place for its legitimate use at line 528 (append_chat_summary). No truncated/bytes flag is needed because no truncation occurs. Existing validation (non-empty messageId/content, role restriction) is preserved. (Alternative, also acceptable: introduce a dedicated MAX_CHAT_MESSAGE_BYTES, e.g. 64 KB, and surface truncated/bytes when truncation actually occurs — but since storage caps nothing, dropping the cap is cleaner and more consistent.) No new oversize/DoS exposure: the model already produces and persists arbitrarily long messages through the other paths every turn.


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

Originally created by @kolacheee on GitHub (Jun 29, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3006 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** `editChatMessage` caps the full replacement message body to `MAX_APPEND_BYTES` (16 KB) using `trimToUtf8Bytes`, then persists the truncated text with no indication that truncation occurred. `MAX_APPEND_BYTES` is the per-append cap for the rolling chat-summary feature, not a message-size limit — it appears to have been copy-pasted into the message-edit path. Chat messages have no length limit in storage, and long roleplay/assistant messages routinely exceed 16 KB. When an agent with the `edit_messages` capability calls `edit_chat_message` with a replacement longer than 16 KB, the stored message is silently chopped and the tail is permanently lost. Unlike `write_chat_variable` (which returns `truncated`/`bytes`), `edit_chat_message` returns only `{applied, messageId, role, reason}`, so neither the model nor the user is told the edit was cut. **How to reproduce (try it yourself)** 1. Configure an agent with the `edit_messages` capability so `edit_chat_message` is enabled and `replaceChatMessageContent` is wired. 2. Have the agent call `edit_chat_message({ messageId: "<a real message id>", content: "<a string longer than 16384 UTF-8 bytes>" })`. 3. Inspect the persisted message. You'll see: the message is truncated to 16 KB; the tool reports `applied:true` with no truncation indicator; content past 16 KB is gone permanently. Expected: the full replacement content is stored (messages have no storage cap), consistent with every other message-writing path. **Where it is** `packages/server/src/services/tools/tool-executor.ts:685` (the `trimToUtf8Bytes(args.content.trim(), MAX_APPEND_BYTES)` call in `editChatMessage`); the constant `MAX_APPEND_BYTES = 16 * 1024` at line 68. The writer that persists with no truncated flag is `replaceChatMessageContent` in `packages/server/src/services/generation/tool-resolution-runtime.ts:643-649`. The manifest schema has no `maxLength` at `packages/shared/src/features/function-calls/tools/edit-chat-message/manifest.ts:14-17`. **What's happening** `editChatMessage` reuses `MAX_APPEND_BYTES` (named/intended for chat-summary appends) as the cap for a full message replacement. The cap is far smaller than realistic message sizes (lorebook content uses a 64 KB cap), and unlike other writers no `truncated`/`bytes` flag is returned, so silent data loss occurs. `updateMessageContent` (chats.storage.ts:843-848) does a plain `db.update(messages).set({ content })` with no length limit. **Suggested fix** In `tool-executor.ts` `editChatMessage`, stop applying the chat-summary append cap to full message replacements — pass the trimmed content through unchanged: ```ts return replaceFn({ messageId: args.messageId.trim(), content: args.content.trim(), reason: typeof args.reason === "string" ? args.reason.trim().slice(0, 240) : undefined, }); ``` i.e. replace `trimToUtf8Bytes(args.content.trim(), MAX_APPEND_BYTES)` with `args.content.trim()`. This matches every other message-writing path (createMessage, updateMessageContent, continuation, prose-guardian rewrites), all of which persist uncapped content. `MAX_APPEND_BYTES` stays in place for its legitimate use at line 528 (`append_chat_summary`). No `truncated`/`bytes` flag is needed because no truncation occurs. Existing validation (non-empty `messageId`/`content`, role restriction) is preserved. (Alternative, also acceptable: introduce a dedicated `MAX_CHAT_MESSAGE_BYTES`, e.g. 64 KB, and surface `truncated`/`bytes` when truncation actually occurs — but since storage caps nothing, dropping the cap is cleaner and more consistent.) No new oversize/DoS exposure: the model already produces and persists arbitrarily long messages through the other paths every turn. --- *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 merged staging PR #3018.

<!-- gh-comment-id:4833016226 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed in merged staging PR #3018.
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#1578
No description provided.