[PR #4779] [MERGED] feat(mari): relevance-rank the available-names list (#4768 phase 3) #6495

Closed
opened 2026-10-23 04:10:57 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/4779
Author: @kolacheee
Created: 8/9/2026
Status: ✅ Merged
Merged: 8/9/2026
Merged by: @kolacheee

Base: staging ← Head: feat/4768-relevance-ranked-names


📝 Commits (6)

  • b9429ce feat(mari): rank the available-names list by conversation relevance (#4768)
  • b1c81a9 fix(mari): address adversarial review of the relevance-ranked list
  • 5c0779b Merge branch 'staging' into feat/4768-relevance-ranked-names
  • 06ee63b fix(mari): address CodeRabbit review of the #4768 semantic PR
  • 6ff68f4 Merge branch 'staging' into feat/4768-relevance-ranked-names
  • 670f2aa fix(mari): address CodeRabbit review of the relevance-ranked PR (#4768)

📊 Changes

4 files changed (+193 additions, -1 deletions)

View changed files

📝 package.json (+1 -1)
📝 packages/server/src/routes/generate.routes.ts (+12 -0)
📝 packages/server/src/routes/generate/professor-mari-prompt-context.ts (+0 -0)
➕ scripts/regressions/professor-mari-relevance-names.regression.ts (+180 -0)

📄 Description

Note

Stacks on #4778, which stacks on #4771. Until those merge, this PR's diff also shows their commits. Merge order: #4771 → #4778 → this. Draft until then and until the manual browser pass below.

Linked issue

Part of #4768 (phase 3 — makes the injected list carry its weight now that fetch does resolution).

Why this change

Phase 2 (#4778) gave Mari's [fetch:] full-library resolution (exact → substring → semantic), which made the injected <available_names> list mostly redundant for finding things — its remaining job is awareness. But it was still an alphabetical slice of up to 100/type, which is both the recurring token cost the issue flagged and an arbitrary ordering (the entities you're actually discussing are rarely the alphabetically-first ones).

What changed

When the conversation can be embedded, the <available_names> list is now ranked by semantic relevance to the current message and trimmed to a small top-K (default 12/type) — the entities that matter right now — instead of an alphabetical 100. Cheaper on a large library, and the names Mari sees are the relevant ones. Fetch still resolves the long tail, so nothing is lost.

Design points that keep it cheap and honest (all tightened by an adversarial review before this went up):

  • One embed per turn, never entities. Only the conversation query is embedded, and it's memoized across the turn's follow-up passes (the query is invariant, so the fetch follow-up loop doesn't re-embed it — this avoids up to two redundant embedder round-trips per fetch turn). Entity vectors come from the phase-2 persisted store; they are never embedded here. Cost is ~one small embed + cosine math per turn — the same order as the memory-recall embed the pipeline already does.
  • Lives on the volatile tail (from phase 1), so a per-turn-changing order never touches the cached system prefix.
  • Honest cold-start. Entities warm lazily via fetch. A type with no warmed embeddings falls back to the alphabetical list (exactly the pre-relevance behavior) rather than emitting an arbitrary creation-order slice; only a genuinely ranked block claims "ranked by relevance". So a brand-new library reads as it did before and warms into relevance ranking as items get fetched.
  • Full degrade to the alphabetical list when the vectorizer is unavailable, the query is empty (e.g. an autonomous turn), or ranking throws — the phase-1 behavior and its regression are preserved as the floor.

Known follow-up (noted, not blocking): the ranking re-scans the entity tables in-memory each pass (bounded, O(n), cheap next to the embed the memo removed); a lighter read-only projection could remove even that.

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

Manual verification notes

Automated (green), not a substitute for the human pass:

  • pnpm check green; full regression:issues + regression:prompt + professor-mari lanes green.
  • New professor-mari-relevance-names regression (real DB + deterministic stub embedder): relevance-first ordering, re-ranking as the conversation changes, cold-library → alphabetical (not creation-order), the alphabetical/empty-query degrade paths, and query-embedded-once-per-turn memoization. The phase-1 professor-mari-prompt-context regression still passes unchanged (the alphabetical path is the preserved floor).
  • A 3-lens adversarial review was run; the two majors it found (per-follow-up query re-embed, and the cold-start relevance mislabel) plus the overflow-count minors were fixed.

Human pass needed (needs a real embedder + LLM):

  • Manually verify in the Home Mari chat with a warmed, largish library: the names Mari surfaces track what the conversation is about (talk about vampires → vampire-y characters lead), and change as the topic shifts.
  • Manually verify it doesn't feel laggier than before on a fetch-heavy turn (the memoization is the guard here).
  • Manually verify a fresh library (nothing fetched yet) still shows a sensible alphabetical list and doesn't error.
  • Manually verify with no embedder configured: the list is alphabetical and nothing breaks.

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)

Prompt-internal behavior; no user docs reference the name-list ordering.

Summary by CodeRabbit

  • Bug Fixes

    • Improved Professor Mari’s relevance-based name ranking across follow-up interactions.
    • Added graceful alphabetical fallback when query text, relevance data, or vectorization is unavailable.
    • Reduced repeated relevance calculations during a single interaction for more consistent performance.
  • Tests

    • Added regression coverage for alphabetical fallback, relevance ranking, query re-ranking across turns, unavailable vectorization, and per-turn caching.

🔄 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/4779 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 8/9/2026 **Status:** ✅ Merged **Merged:** 8/9/2026 **Merged by:** [@kolacheee](https://github.com/kolacheee) **Base:** `staging` ← **Head:** `feat/4768-relevance-ranked-names` --- ### 📝 Commits (6) - [`b9429ce`](https://github.com/Pasta-Devs/Marinara-Engine/commit/b9429ce429789de100f6c30775023ebb14e8759e) feat(mari): rank the available-names list by conversation relevance (#4768) - [`b1c81a9`](https://github.com/Pasta-Devs/Marinara-Engine/commit/b1c81a958bdcc75d758a3a00b29948b933c002c3) fix(mari): address adversarial review of the relevance-ranked list - [`5c0779b`](https://github.com/Pasta-Devs/Marinara-Engine/commit/5c0779b3d5b4db260bb8026ebce9976d24507fd8) Merge branch 'staging' into feat/4768-relevance-ranked-names - [`06ee63b`](https://github.com/Pasta-Devs/Marinara-Engine/commit/06ee63b924cabbf0d5bc26647088966b382e6f66) fix(mari): address CodeRabbit review of the #4768 semantic PR - [`6ff68f4`](https://github.com/Pasta-Devs/Marinara-Engine/commit/6ff68f4f8e0986c2aecf161423b1d6f2e94cbd06) Merge branch 'staging' into feat/4768-relevance-ranked-names - [`670f2aa`](https://github.com/Pasta-Devs/Marinara-Engine/commit/670f2aaaaf68c22d0ad9d3ad48bd399bff640d70) fix(mari): address CodeRabbit review of the relevance-ranked PR (#4768) ### 📊 Changes **4 files changed** (+193 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `package.json` (+1 -1) 📝 `packages/server/src/routes/generate.routes.ts` (+12 -0) 📝 `packages/server/src/routes/generate/professor-mari-prompt-context.ts` (+0 -0) ➕ `scripts/regressions/professor-mari-relevance-names.regression.ts` (+180 -0) </details> ### 📄 Description > [!NOTE] > **Stacks on #4778, which stacks on #4771.** Until those merge, this PR's diff also shows their commits. **Merge order: #4771 → #4778 → this.** Draft until then and until the manual browser pass below. ## Linked issue Part of #4768 (phase 3 — makes the injected list carry its weight now that fetch does resolution). ## Why this change Phase 2 (#4778) gave Mari's `[fetch:]` full-library resolution (exact → substring → semantic), which made the injected `<available_names>` list mostly redundant for *finding* things — its remaining job is *awareness*. But it was still an **alphabetical slice of up to 100/type**, which is both the recurring token cost the issue flagged and an arbitrary ordering (the entities you're actually discussing are rarely the alphabetically-first ones). ## What changed When the conversation can be embedded, the `<available_names>` list is now **ranked by semantic relevance to the current message** and trimmed to a small top-K (default 12/type) — the entities that matter *right now* — instead of an alphabetical 100. Cheaper on a large library, and the names Mari sees are the relevant ones. Fetch still resolves the long tail, so nothing is lost. Design points that keep it cheap and honest (all tightened by an adversarial review before this went up): - **One embed per turn, never entities.** Only the conversation query is embedded, and it's **memoized across the turn's follow-up passes** (the query is invariant, so the fetch follow-up loop doesn't re-embed it — this avoids up to two redundant embedder round-trips per fetch turn). Entity vectors come from the phase-2 persisted store; they are *never* embedded here. Cost is ~one small embed + cosine math per turn — the same order as the memory-recall embed the pipeline already does. - **Lives on the volatile tail** (from phase 1), so a per-turn-changing order never touches the cached system prefix. - **Honest cold-start.** Entities warm lazily via fetch. A type with no warmed embeddings falls back to the **alphabetical** list (exactly the pre-relevance behavior) rather than emitting an arbitrary creation-order slice; only a genuinely ranked block claims "ranked by relevance". So a brand-new library reads as it did before and warms into relevance ranking as items get fetched. - **Full degrade** to the alphabetical list when the vectorizer is unavailable, the query is empty (e.g. an autonomous turn), or ranking throws — the phase-1 behavior and its regression are preserved as the floor. Known follow-up (noted, not blocking): the ranking re-scans the entity tables in-memory each pass (bounded, O(n), cheap next to the embed the memo removed); a lighter read-only projection could remove even that. ## Validation - [x] `pnpm check` passes locally - [x] Container (Docker / Podman) built and ran without issue - [x] Ran the app, clicked through the changes manually - [x] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [x] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes Automated (green), not a substitute for the human pass: - `pnpm check` green; full `regression:issues` + `regression:prompt` + `professor-mari` lanes green. - New `professor-mari-relevance-names` regression (real DB + deterministic stub embedder): relevance-first ordering, re-ranking as the conversation changes, cold-library → alphabetical (not creation-order), the alphabetical/empty-query degrade paths, and query-embedded-once-per-turn memoization. The phase-1 `professor-mari-prompt-context` regression still passes unchanged (the alphabetical path is the preserved floor). - A 3-lens adversarial review was run; the two majors it found (per-follow-up query re-embed, and the cold-start relevance mislabel) plus the overflow-count minors were fixed. Human pass needed (needs a real embedder + LLM): - **Manually verify** in the Home Mari chat with a warmed, largish library: the names Mari surfaces track what the conversation is about (talk about vampires → vampire-y characters lead), and change as the topic shifts. - **Manually verify** it doesn't feel laggier than before on a fetch-heavy turn (the memoization is the guard here). - **Manually verify** a fresh library (nothing fetched yet) still shows a sensible alphabetical list and doesn't error. - **Manually verify** with **no embedder configured**: the list is alphabetical and nothing breaks. ## Docs and release impact - [x] 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) Prompt-internal behavior; no user docs reference the name-list ordering. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved Professor Mari’s relevance-based name ranking across follow-up interactions. * Added graceful alphabetical fallback when query text, relevance data, or vectorization is unavailable. * Reduced repeated relevance calculations during a single interaction for more consistent performance. * **Tests** * Added regression coverage for alphabetical fallback, relevance ranking, query re-ranking across turns, unavailable vectorization, and per-turn caching. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-23 04:10:57 +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#6495
No description provided.