[GH-ISSUE #3014] [Issue]: save_lorebook_entry coerces any unrecognized mode value to the destructive 'replace' default #1586

Closed
opened 2026-10-21 05:40:05 +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/3014

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

normalizeLorebookWriteMode returns the value only if it is exactly "create", "append", or "replace"; any other value falls through to "replace". The manifest enum is ["create","replace","append"] and its description reads "Create or update an entry", which invites near-miss values like mode:"update", "add", or "new" from a model — especially non-native-tool-calling models whose tool calls are parsed from text and not enum-validated. Such a near-miss is silently mapped to replace (the destructive overwrite path) rather than rejected. A model intending to add to or create an entry can thus overwrite an existing same-named entry, losing its prior content, while the tool reports a successful save.

How to reproduce (try it yourself)

  1. With a text-parsed (non-native-function-calling) model, emit save_lorebook_entry({ name:"Aldric", content:"new partial note", mode:"update" }) where an entry named "Aldric" already exists.

You'll see: normalizeLorebookWriteMode("update") returns "replace"; the existing entry's content is overwritten (not appended/merged) and the prior content is lost; the tool returns {applied:true, action:"replaced"}.

Expected: an explicit-but-invalid mode is rejected with a correctable validation error, so the destructive overwrite is not triggered by a hallucinated value.

Where it is

packages/server/src/services/tools/tool-executor.ts:633-635 (normalizeLorebookWriteMode), used at :665 in saveLorebookEntry. The destructive overwrite path is in packages/server/src/services/generation/tool-resolution-runtime.ts:453-459,463.

What's happening

Unknown mode strings are silently coerced to the destructive default "replace" instead of being rejected or treated as non-destructive. Args are not enum-validated before reaching this function (a raw JSON.parse of the tool arguments passes the value straight through), so a near-miss mode from a text-parsed model reaches the coercion and triggers an overwrite.

Suggested fix

Validate mode in saveLorebookEntry so an explicit-but-invalid mode is rejected while an omitted mode still defaults to the documented "replace".

Replace the normalizer:

function normalizeLorebookWriteMode(value: unknown): "create" | "replace" | "append" | "invalid" {
  if (value === undefined || value === null || value === "") return "replace"; // omitted -> documented default
  return value === "create" || value === "append" || value === "replace" ? value : "invalid";
}

Then in saveLorebookEntry, before building the save payload:

const mode = normalizeLorebookWriteMode(args.mode);
if (mode === "invalid") {
  return { error: "save_lorebook_entry mode must be one of create|replace|append" };
}

and change the payload field at line 665 to use the validated mode.

Regression avoided: an OMITTED mode (undefined/null/empty) still maps to the documented "replace" default, so existing callers relying on the default are unaffected; valid values pass through unchanged. Doing the check in saveLorebookEntry (which can early-return { error }) rather than inside the normalizer keeps the established error-surfacing convention this function already uses, and explicitly treating undefined/null/"" as omission also catches non-string near-misses. Both downstream writers receive entry.mode already typed to the union, so nothing else is touched.


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/3014 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** `normalizeLorebookWriteMode` returns the value only if it is exactly `"create"`, `"append"`, or `"replace"`; any other value falls through to `"replace"`. The manifest enum is `["create","replace","append"]` and its description reads "Create or update an entry", which invites near-miss values like `mode:"update"`, `"add"`, or `"new"` from a model — especially non-native-tool-calling models whose tool calls are parsed from text and not enum-validated. Such a near-miss is silently mapped to `replace` (the destructive overwrite path) rather than rejected. A model intending to add to or create an entry can thus overwrite an existing same-named entry, losing its prior content, while the tool reports a successful save. **How to reproduce (try it yourself)** 1. With a text-parsed (non-native-function-calling) model, emit `save_lorebook_entry({ name:"Aldric", content:"new partial note", mode:"update" })` where an entry named "Aldric" already exists. You'll see: `normalizeLorebookWriteMode("update")` returns `"replace"`; the existing entry's content is overwritten (not appended/merged) and the prior content is lost; the tool returns `{applied:true, action:"replaced"}`. Expected: an explicit-but-invalid mode is rejected with a correctable validation error, so the destructive overwrite is not triggered by a hallucinated value. **Where it is** `packages/server/src/services/tools/tool-executor.ts:633-635` (`normalizeLorebookWriteMode`), used at `:665` in `saveLorebookEntry`. The destructive overwrite path is in `packages/server/src/services/generation/tool-resolution-runtime.ts:453-459,463`. **What's happening** Unknown mode strings are silently coerced to the destructive default `"replace"` instead of being rejected or treated as non-destructive. Args are not enum-validated before reaching this function (a raw `JSON.parse` of the tool arguments passes the value straight through), so a near-miss mode from a text-parsed model reaches the coercion and triggers an overwrite. **Suggested fix** Validate `mode` in `saveLorebookEntry` so an explicit-but-invalid mode is rejected while an omitted mode still defaults to the documented `"replace"`. Replace the normalizer: ```ts function normalizeLorebookWriteMode(value: unknown): "create" | "replace" | "append" | "invalid" { if (value === undefined || value === null || value === "") return "replace"; // omitted -> documented default return value === "create" || value === "append" || value === "replace" ? value : "invalid"; } ``` Then in `saveLorebookEntry`, before building the save payload: ```ts const mode = normalizeLorebookWriteMode(args.mode); if (mode === "invalid") { return { error: "save_lorebook_entry mode must be one of create|replace|append" }; } ``` and change the payload field at line 665 to use the validated `mode`. *Regression avoided:* an OMITTED mode (undefined/null/empty) still maps to the documented `"replace"` default, so existing callers relying on the default are unaffected; valid values pass through unchanged. Doing the check in `saveLorebookEntry` (which can early-return `{ error }`) rather than inside the normalizer keeps the established error-surfacing convention this function already uses, and explicitly treating undefined/null/"" as omission also catches non-string near-misses. Both downstream writers receive `entry.mode` already typed to the union, so nothing else is touched. --- *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:4833019303 --> @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#1586
No description provided.