[PR #7413] [MERGED] fix(mari): page lorebook.entries with offset and nextOffset (#7407) #7721

Closed
opened 2026-10-24 04:06:50 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/7413
Author: @SpicyMarinara
Created: 10/11/2026
Status: ✅ Merged
Merged: 10/11/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/mari-lorebook-entries-offset


📝 Commits (4)

  • 41f1534 fix(mari): page lorebook.entries with offset and nextOffset (#7407)
  • 2d246d2 fix(mari): page mari lorebooks entries the same way
  • 546e281 fix(mari): keep every lorebook.entries page usable
  • 8f3fa01 fix(mari): flag shortened entry keys and list --offset in help

📊 Changes

7 files changed (+273 additions, -38 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 packages/server/src/services/mari-db/mari-db.service.ts (+67 -27)
📝 packages/server/src/services/professor-mari/workspace-agent.service.ts (+4 -3)
📝 scripts/regressions/mari/db-review-durability.regression.ts (+4 -4)
📝 scripts/regressions/mari/decision-authoring.regression.ts (+5 -3)
➕ scripts/regressions/mari/lorebook-entries-paging.regression.ts (+191 -0)
📝 scripts/regressions/open-issues.regression.ts (+1 -1)

📄 Description

Important

Contributions target staging. Only SpicyMarinara may promote this repository's staging branch or a same-repository hotfix/* branch to main.
Outside and first-time contributors also require an approving review from SpicyMarinara.

Linked issue

Closes #7407

Thanks to @bahaynes for the report and the repro script, which made this a quick fix.

Why this change

  • On a big lorebook, Professor Mari could not see, review or edit entries past the first page. app_data lorebook.entries read limit but never offset, and it returned a bare array with no total or nextOffset. So every request for "the rest" returned the same first page. That page was also more than twice the 24k read budget (52k characters for 100 entries), so even its tail was cut off before she saw it. instruction.list and skill.list were already paged for this reason. lorebook.entries was not.

What changed

  • lorebook.entries (in packages/server/src/services/mari-db/mari-db.service.ts) now pages like instruction.list. It honors offset (through normalizeOffset) and returns { items, total, offset, nextOffset }. The shell command mari lorebooks entries <id> had the same filter, sort and bug. It now shares the new pageLorebookEntries helper, accepts --offset and returns the same pages. Entries are added until the next one would push the pretty-printed page past MARI_READ_OUTPUT_BUDGET. The first entry is always kept, so paging always moves forward. limit still caps a page (default 100, max 2000). On the reporter's 190-entry lorebook this gives pages of 43, 43, 43, 43 and 18 entries, each under 24k characters.

  • Every field in the entry index row is now bounded: description is cut to 200 characters (as content already was), name to 200, tag to 100, and keys to at most 50 keys of 100 characters each. Without this, one entry with a long description or a huge key list fills the page by itself, and boundReadObject hides the whole items array, ids included. A row whose key list was cut carries keysTruncated: true, so Mari knows to read the entry before changing its keys (a patch.keys replaces the whole list). lorebook.getEntry still returns the full entry.

  • normalizeLimit now floors before its positivity check. A limit such as 0.5 falls back to the default instead of becoming 0. Before, it gave an empty page whose nextOffset pointed back at the same offset, so a pager would loop forever. I checked every caller in the file: all of them use a positive fallback, except the mari db list offset, whose fallback of 0 gives the same result as before.

  • Professor Mari's app_data tool description, its offset parameter and the "Lorebook reading" guidance in her system prompt now tell her to call again with offset: nextOffset until it is null. The guidance also tells her to read every page before deleting or reordering entries (that shifts later pages), and to call lorebook.getEntry before changing the keys of an entry marked keysTruncated: true. mari lorebooks help now lists --offset for entries.

  • Consumers of the old array shape: three regressions (open-issues, mari/db-review-durability, mari/decision-authoring) now read .items. Their assertions are unchanged. No client change was needed: listRecords/countMariListOutput in packages/client/src/lib/mari-referenced-resources.ts already read the first array in an object. The new regression pins that behaviour.

  • New regression scripts/regressions/mari/lorebook-entries-paging.regression.ts. It walks a 190-entry lorebook to the end and checks every entry once, in order, with distinct ids and every page under budget. It also checks:

    • the reporter's { limit: 50, offset: 150 } call, a small limit, a 0.5 limit, and an offset past the end;
    • that the shell command returns the same pages;
    • the chat work card's count and entry links on the new shape;
    • one entry with a huge description and one with 400 long keys. Both still fit a page with their ids, and lorebook.getEntry still returns key 400;
    • keysTruncated: true on the 400-key entry and on an entry with one 140-character key, and no flag on ordinary entries or on 50 keys of exactly 100 characters;
    • that mari lorebooks help lists --offset for entries.

    The regression fails on origin/staging, which returns a bare array. It also fails with each hardening reverted: without the field bounds, the heavy entry's page is elided; without the normalizeLimit change, a fractional limit returns no entries; without the keysTruncated flag or the help line, their assertions fail.

  • CHANGELOG.md: one entry under Unreleased.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Commands run: pnpm check; node ./scripts/run-regressions.mjs --filter scripts/regressions/mari/lorebook-entries-paging.regression.ts (also against origin/staging code, where it fails); --filter for scripts/regressions/open-issues.regression.ts, scripts/regressions/mari/db-review-durability.regression.ts, scripts/regressions/mari/decision-authoring.regression.ts, scripts/regressions/command-center.regression.ts, scripts/regressions/mari/instructions-memory.regression.ts; --filter scripts/regressions/mari/ (15 files); --filter scripts/regressions/professor-mari (16 files); pnpm regression:prompt; and the other 12 regressions that use the Mari DB service, because normalizeLimit is shared: package-table-registration, chat-diagnose, mari-completion-frames, agent-home-widgets, lorebook-test-scan, persona-api-decoded-reads, experience-state-imported-anchors, extension-security, mari-write-readback, lorebook-entry-message-provenance, lorebook-decision-activation, storage-writer-lock. All of these were rerun after the review fixes in 8f3fa0171, and the new regression fails with either the help line or the keysTruncated flag reverted.

Manual verification notes

  • Manually verify in the browser that, with a lorebook of 150+ entries, asking Professor Mari to "list every entry in " makes her call lorebook.entries again with offset and name entries from the end of the lorebook.
  • Manually verify that asking her to edit an entry near the end of a big lorebook (for example, entry 170 of 190) finds and updates that entry.
  • Manually verify that the work card for each lorebook.entries step reads "Read · · N entries", with N the entries on that page, and that entries she names in her answer still link to the lorebook editor.
  • Manually verify that a small lorebook (a few entries) still comes back in a single read with no second call.
  • Manually verify that asking her to add one key to an entry with more than 50 keys makes her read the entry with lorebook.getEntry first and keeps every existing key.
  • Manually verify that the Lorebooks panel and the lorebook editor are unchanged. This PR changes only Professor Mari's reads, so their entry lists and keys should look exactly as before.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened (see CONTRIBUTING.md § Translated documentation)
  • Version/release files updated (only if this PR includes a version bump)

No files under docs/ changed, so no [docs-i18n] follow-up is needed. CHANGELOG.md has one Unreleased entry.

UI evidence (if applicable)

Not applicable. This is a server-side read and prompt change with no UI change.

Local CodeRabbit review

I ran local CodeRabbit reviews (coderabbit review --agent --committed). Every finding was fixed and none was rejected.

  • Run 1, one finding: the shell mari lorebooks entries still ignored --offset and returned a bare array. Fixed in 2d246d227 with the shared pageLorebookEntries helper.
  • Run 2, two findings: a fractional limit produced an empty page whose nextOffset did not move, and an entry with a huge keys list could still overflow a page by itself. Both fixed in 546e281ff (normalizeLimit floors first, and every index field is bounded). Both are pinned by the regression.
  • Run 3: no findings.
  • Run 4, after the adversarial-review fixes in 8f3fa0171: no findings.

Adversarial review follow-up

All three findings were traced and fixed in 8f3fa0171; none was rejected.

  • Cut key lists had no marker. Confirmed: the index kept 50 keys of 100 characters with no sign anything was missing, and lorebook.updateEntry patch.keys replaces the whole list, so a keys edit built from the index could drop keys or save a cut regex. Fixed: such rows now carry keysTruncated: true, and the "Lorebook reading" guidance says to call lorebook.getEntry before changing those keys. Pinned by the regression.
  • Offset paging skips an entry when entries are deleted or reordered mid-walk. Confirmed by trace: each call re-reads and re-sorts the rows and slices by offset, so an applied lorebook.deleteEntry or an order change shifts later pages. instruction.list pages the same way, and staging could not reach later pages at all, so this is not a regression. Fixed with one guidance sentence telling Mari to read every page before deleting or reordering entries. A cursor (afterEntryId) would also break on reorders and adds a second paging scheme, so I kept the offset contract shared with instruction.list. This is prompt guidance only, with no dedicated test.
  • mari lorebooks help did not list --offset. Confirmed. Fixed in the help text, pinned by the regression.

🔄 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/7413 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 10/11/2026 **Status:** ✅ Merged **Merged:** 10/11/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/mari-lorebook-entries-offset` --- ### 📝 Commits (4) - [`41f1534`](https://github.com/Pasta-Devs/Marinara-Engine/commit/41f15345bddf7309721f78bcc52e41a166658041) fix(mari): page lorebook.entries with offset and nextOffset (#7407) - [`2d246d2`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2d246d22728dff7f71ecd82a68f41420a0f3850d) fix(mari): page `mari lorebooks entries` the same way - [`546e281`](https://github.com/Pasta-Devs/Marinara-Engine/commit/546e281ff643ab2171fa0b5bbe243bcbaad6cf39) fix(mari): keep every lorebook.entries page usable - [`8f3fa01`](https://github.com/Pasta-Devs/Marinara-Engine/commit/8f3fa0171d89a4f32949db342d11f78dc9bcffb7) fix(mari): flag shortened entry keys and list --offset in help ### 📊 Changes **7 files changed** (+273 additions, -38 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `packages/server/src/services/mari-db/mari-db.service.ts` (+67 -27) 📝 `packages/server/src/services/professor-mari/workspace-agent.service.ts` (+4 -3) 📝 `scripts/regressions/mari/db-review-durability.regression.ts` (+4 -4) 📝 `scripts/regressions/mari/decision-authoring.regression.ts` (+5 -3) ➕ `scripts/regressions/mari/lorebook-entries-paging.regression.ts` (+191 -0) 📝 `scripts/regressions/open-issues.regression.ts` (+1 -1) </details> ### 📄 Description > [!IMPORTANT] > Contributions target `staging`. Only `SpicyMarinara` may promote this repository's `staging` branch or a same-repository `hotfix/*` branch to `main`. > Outside and first-time contributors also require an approving review from `SpicyMarinara`. <!-- See CONTRIBUTING.md § Branches. --> ## Linked issue Closes #7407 Thanks to @bahaynes for the report and the repro script, which made this a quick fix. ## Why this change - On a big lorebook, Professor Mari could not see, review or edit entries past the first page. `app_data` `lorebook.entries` read `limit` but never `offset`, and it returned a bare array with no `total` or `nextOffset`. So every request for "the rest" returned the same first page. That page was also more than twice the 24k read budget (52k characters for 100 entries), so even its tail was cut off before she saw it. `instruction.list` and `skill.list` were already paged for this reason. `lorebook.entries` was not. ## What changed - `lorebook.entries` (in `packages/server/src/services/mari-db/mari-db.service.ts`) now pages like `instruction.list`. It honors `offset` (through `normalizeOffset`) and returns `{ items, total, offset, nextOffset }`. The shell command `mari lorebooks entries <id>` had the same filter, sort and bug. It now shares the new `pageLorebookEntries` helper, accepts `--offset` and returns the same pages. Entries are added until the next one would push the pretty-printed page past `MARI_READ_OUTPUT_BUDGET`. The first entry is always kept, so paging always moves forward. `limit` still caps a page (default 100, max 2000). On the reporter's 190-entry lorebook this gives pages of 43, 43, 43, 43 and 18 entries, each under 24k characters. - Every field in the entry index row is now bounded: `description` is cut to 200 characters (as `content` already was), `name` to 200, `tag` to 100, and `keys` to at most 50 keys of 100 characters each. Without this, one entry with a long description or a huge key list fills the page by itself, and `boundReadObject` hides the whole `items` array, ids included. A row whose key list was cut carries `keysTruncated: true`, so Mari knows to read the entry before changing its keys (a `patch.keys` replaces the whole list). `lorebook.getEntry` still returns the full entry. - `normalizeLimit` now floors before its positivity check. A limit such as `0.5` falls back to the default instead of becoming 0. Before, it gave an empty page whose `nextOffset` pointed back at the same offset, so a pager would loop forever. I checked every caller in the file: all of them use a positive fallback, except the `mari db list` offset, whose fallback of 0 gives the same result as before. - Professor Mari's `app_data` tool description, its `offset` parameter and the "Lorebook reading" guidance in her system prompt now tell her to call again with `offset: nextOffset` until it is null. The guidance also tells her to read every page before deleting or reordering entries (that shifts later pages), and to call `lorebook.getEntry` before changing the keys of an entry marked `keysTruncated: true`. `mari lorebooks help` now lists `--offset` for `entries`. - Consumers of the old array shape: three regressions (`open-issues`, `mari/db-review-durability`, `mari/decision-authoring`) now read `.items`. Their assertions are unchanged. No client change was needed: `listRecords`/`countMariListOutput` in `packages/client/src/lib/mari-referenced-resources.ts` already read the first array in an object. The new regression pins that behaviour. - New regression `scripts/regressions/mari/lorebook-entries-paging.regression.ts`. It walks a 190-entry lorebook to the end and checks every entry once, in order, with distinct ids and every page under budget. It also checks: - the reporter's `{ limit: 50, offset: 150 }` call, a small `limit`, a `0.5` limit, and an offset past the end; - that the shell command returns the same pages; - the chat work card's count and entry links on the new shape; - one entry with a huge description and one with 400 long keys. Both still fit a page with their ids, and `lorebook.getEntry` still returns key 400; - `keysTruncated: true` on the 400-key entry and on an entry with one 140-character key, and no flag on ordinary entries or on 50 keys of exactly 100 characters; - that `mari lorebooks help` lists `--offset` for `entries`. The regression fails on `origin/staging`, which returns a bare array. It also fails with each hardening reverted: without the field bounds, the heavy entry's page is elided; without the `normalizeLimit` change, a fractional limit returns no entries; without the `keysTruncated` flag or the help line, their assertions fail. - `CHANGELOG.md`: one entry under Unreleased. ## Validation - [ ] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [ ] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [ ] Above manual verification completed (describe below) - [ ] Read and followed `CONTRIBUTING.md` Commands run: `pnpm check`; `node ./scripts/run-regressions.mjs --filter scripts/regressions/mari/lorebook-entries-paging.regression.ts` (also against `origin/staging` code, where it fails); `--filter` for `scripts/regressions/open-issues.regression.ts`, `scripts/regressions/mari/db-review-durability.regression.ts`, `scripts/regressions/mari/decision-authoring.regression.ts`, `scripts/regressions/command-center.regression.ts`, `scripts/regressions/mari/instructions-memory.regression.ts`; `--filter scripts/regressions/mari/` (15 files); `--filter scripts/regressions/professor-mari` (16 files); `pnpm regression:prompt`; and the other 12 regressions that use the Mari DB service, because `normalizeLimit` is shared: `package-table-registration`, `chat-diagnose`, `mari-completion-frames`, `agent-home-widgets`, `lorebook-test-scan`, `persona-api-decoded-reads`, `experience-state-imported-anchors`, `extension-security`, `mari-write-readback`, `lorebook-entry-message-provenance`, `lorebook-decision-activation`, `storage-writer-lock`. All of these were rerun after the review fixes in 8f3fa0171, and the new regression fails with either the help line or the `keysTruncated` flag reverted. ### Manual verification notes - Manually verify in the browser that, with a lorebook of 150+ entries, asking Professor Mari to "list every entry in <lorebook>" makes her call `lorebook.entries` again with `offset` and name entries from the end of the lorebook. - Manually verify that asking her to edit an entry near the end of a big lorebook (for example, entry 170 of 190) finds and updates that entry. - Manually verify that the work card for each `lorebook.entries` step reads "Read · <lorebook> · N entries", with N the entries on that page, and that entries she names in her answer still link to the lorebook editor. - Manually verify that a small lorebook (a few entries) still comes back in a single read with no second call. - Manually verify that asking her to add one key to an entry with more than 50 keys makes her read the entry with `lorebook.getEntry` first and keeps every existing key. - Manually verify that the Lorebooks panel and the lorebook editor are unchanged. This PR changes only Professor Mari's reads, so their entry lists and keys should look exactly as before. ## Docs and release impact - [ ] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation) - [ ] Version/release files updated (only if this PR includes a version bump) No files under `docs/` changed, so no `[docs-i18n]` follow-up is needed. `CHANGELOG.md` has one Unreleased entry. ## UI evidence (if applicable) Not applicable. This is a server-side read and prompt change with no UI change. ## Local CodeRabbit review I ran local CodeRabbit reviews (`coderabbit review --agent --committed`). Every finding was fixed and none was rejected. - Run 1, one finding: the shell `mari lorebooks entries` still ignored `--offset` and returned a bare array. Fixed in 2d246d227 with the shared `pageLorebookEntries` helper. - Run 2, two findings: a fractional `limit` produced an empty page whose `nextOffset` did not move, and an entry with a huge `keys` list could still overflow a page by itself. Both fixed in 546e281ff (`normalizeLimit` floors first, and every index field is bounded). Both are pinned by the regression. - Run 3: no findings. - Run 4, after the adversarial-review fixes in 8f3fa0171: no findings. ## Adversarial review follow-up All three findings were traced and fixed in 8f3fa0171; none was rejected. - **Cut key lists had no marker.** Confirmed: the index kept 50 keys of 100 characters with no sign anything was missing, and `lorebook.updateEntry` `patch.keys` replaces the whole list, so a keys edit built from the index could drop keys or save a cut regex. Fixed: such rows now carry `keysTruncated: true`, and the "Lorebook reading" guidance says to call `lorebook.getEntry` before changing those keys. Pinned by the regression. - **Offset paging skips an entry when entries are deleted or reordered mid-walk.** Confirmed by trace: each call re-reads and re-sorts the rows and slices by `offset`, so an applied `lorebook.deleteEntry` or an `order` change shifts later pages. `instruction.list` pages the same way, and staging could not reach later pages at all, so this is not a regression. Fixed with one guidance sentence telling Mari to read every page before deleting or reordering entries. A cursor (`afterEntryId`) would also break on reorders and adds a second paging scheme, so I kept the offset contract shared with `instruction.list`. This is prompt guidance only, with no dedicated test. - **`mari lorebooks help` did not list `--offset`.** Confirmed. Fixed in the help text, pinned by the regression. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-24 04:06:50 +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#7721
No description provided.