[PR #1637] [MERGED] Normalize lorebook-entry fields at the generic storage boundary #5169

Closed
opened 2026-10-22 04:13:54 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/1637
Author: @Romuromylus
Created: 5/30/2026
Status: ✅ Merged
Merged: 5/30/2026
Merged by: @Romuromylus

Base: refactor ← Head: fix/normalize-lorebook-entry-storage


📝 Commits (3)

  • af430a3 Normalize lorebook-entry fields at the generic storage boundary
  • 089f776 Allow null relationships/dynamicState on lorebook-entry round-trip
  • d7edbc8 Add negative-control test for unparseable lorebook-entry object field

📊 Changes

1 file changed (+133 additions, -1 deletions)

View changed files

📝 src-tauri/src/commands/storage/shared.rs (+133 -1)

📄 Description

Why this change

Follow-up to #1571 (related to but not a duplicate of #1513). #1513 fixed legacy lorebook-entry import normalization, but the generic storage write boundary stayed loosely typed: the lorebook-entries arm of normalize_typed_json_fields only normalized keys and secondaryKeys. Any caller that bypassed the legacy import path (the editor, bulk create, copy/move/reorder, tool-generated entries, remote runtime) could still hand enabled: "false", constant: "true", keys: "[\"moon\"]", or a JSON-string object across the boundary and rely on downstream UI/generation to guess the intended shape.

normalize_typed_json_fields is the single contract both write paths cross: storage_create through with_entity_defaults, and storage_update through normalize_update_patch. Strengthening that one arm closes the gap for every generic caller at once.

What changed

  • src-tauri/src/commands/storage/shared.rs: extend the lorebook-entries arm to normalize the full native record shape:
    • the nine boolean flags (enabled, constant, selective, matchWholeWords, caseSensitive, useRegex, preventRecursion, locked, excludeFromVectorization) via the existing normalize_boolish_fields,
    • the remaining array fields (characterFilterIds, characterTagFilters, generationTriggerFilters, additionalMatchingSources, activationConditions) alongside keys/secondaryKeys,
    • the relationships/dynamicState objects and the nullable schedule object.
    • Values that cannot be coerced are rejected rather than stored, matching the existing array-field contract.
  • Added three tests: legacy-string coercion (positive), native row passes through unchanged (negative control), and an unparseable array field is rejected.

Verification

  • cargo clippy --workspace --all-targets -- -D warnings clean.
  • New tests plus the existing lorebook (19) and normalization (31) suites pass; the legacy import path (which also calls this function) is unaffected because its rows are already coerced before reaching it and the new object/schedule fields are absent on legacy rows.

The end-user write paths reach this boundary through generic storage_create/storage_update, fully covered by the new unit tests against the real normalization function.

Known caveat: numeric fields

Numeric lorebook-entry fields (probability, scanDepth, position, depth, order, sticky, cooldown, delay, ephemeral, groupWeight) are intentionally left untouched. There is no numeric-coercion primitive at this boundary today and the legacy contract never covered them, so adding one is out of scope for this fix. If a malformed numeric caller ever appears it warrants a dedicated follow-up with its own primitive.

Linked issue

Closes #1571.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/1637 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 5/30/2026 **Status:** ✅ Merged **Merged:** 5/30/2026 **Merged by:** [@Romuromylus](https://github.com/Romuromylus) **Base:** `refactor` ← **Head:** `fix/normalize-lorebook-entry-storage` --- ### 📝 Commits (3) - [`af430a3`](https://github.com/Pasta-Devs/Marinara-Engine/commit/af430a324d5762f8462459f51c3f6d5aeeeff46b) Normalize lorebook-entry fields at the generic storage boundary - [`089f776`](https://github.com/Pasta-Devs/Marinara-Engine/commit/089f7761bffaad1d87ca046616b2d8e6cf0910d1) Allow null relationships/dynamicState on lorebook-entry round-trip - [`d7edbc8`](https://github.com/Pasta-Devs/Marinara-Engine/commit/d7edbc8ed25ebfc95e68cea0524f6c3f75b082e8) Add negative-control test for unparseable lorebook-entry object field ### 📊 Changes **1 file changed** (+133 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `src-tauri/src/commands/storage/shared.rs` (+133 -1) </details> ### 📄 Description ## Why this change Follow-up to #1571 (related to but not a duplicate of #1513). #1513 fixed legacy lorebook-entry **import** normalization, but the generic storage write boundary stayed loosely typed: the `lorebook-entries` arm of `normalize_typed_json_fields` only normalized `keys` and `secondaryKeys`. Any caller that bypassed the legacy import path (the editor, bulk create, copy/move/reorder, tool-generated entries, remote runtime) could still hand `enabled: "false"`, `constant: "true"`, `keys: "[\"moon\"]"`, or a JSON-string object across the boundary and rely on downstream UI/generation to guess the intended shape. `normalize_typed_json_fields` is the single contract both write paths cross: `storage_create` through `with_entity_defaults`, and `storage_update` through `normalize_update_patch`. Strengthening that one arm closes the gap for every generic caller at once. ## What changed - `src-tauri/src/commands/storage/shared.rs`: extend the `lorebook-entries` arm to normalize the full native record shape: - the nine boolean flags (`enabled`, `constant`, `selective`, `matchWholeWords`, `caseSensitive`, `useRegex`, `preventRecursion`, `locked`, `excludeFromVectorization`) via the existing `normalize_boolish_fields`, - the remaining array fields (`characterFilterIds`, `characterTagFilters`, `generationTriggerFilters`, `additionalMatchingSources`, `activationConditions`) alongside `keys`/`secondaryKeys`, - the `relationships`/`dynamicState` objects and the nullable `schedule` object. - Values that cannot be coerced are rejected rather than stored, matching the existing array-field contract. - Added three tests: legacy-string coercion (positive), native row passes through unchanged (negative control), and an unparseable array field is rejected. ## Verification - `cargo clippy --workspace --all-targets -- -D warnings` clean. - New tests plus the existing lorebook (19) and normalization (31) suites pass; the legacy import path (which also calls this function) is unaffected because its rows are already coerced before reaching it and the new object/schedule fields are absent on legacy rows. The end-user write paths reach this boundary through generic `storage_create`/`storage_update`, fully covered by the new unit tests against the real normalization function. ## Known caveat: numeric fields Numeric lorebook-entry fields (`probability`, `scanDepth`, `position`, `depth`, `order`, `sticky`, `cooldown`, `delay`, `ephemeral`, `groupWeight`) are intentionally left untouched. There is no numeric-coercion primitive at this boundary today and the legacy contract never covered them, so adding one is out of scope for this fix. If a malformed numeric caller ever appears it warrants a dedicated follow-up with its own primitive. ## Linked issue Closes #1571. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:13:54 +07:00
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#5169
No description provided.