[PR #2776] [MERGED] fix(storage): make lastContactByCharacter work on the file-storage backend #5724

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/2776
Author: @kolacheee
Created: 6/22/2026
Status: ✅ Merged
Merged: 6/22/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/issue-2775-last-contact-groupby


📝 Commits (1)

  • bc08067 fix(storage): aggregate lastContactByCharacter in JS for file-storage backend

📊 Changes

1 file changed (+15 additions, -5 deletions)

View changed files

📝 packages/server/src/services/storage/chats.storage.ts (+15 -5)

📄 Description

Linked issue

Closes #2775

Why this change

  • chats.lastContactByCharacter() built its query with .groupBy() plus a sql MAX() aggregate. The default file-storage backend's query-builder shim (file-backed-store.ts) implements select/from/where/orderBy but not groupBy (and does not evaluate sql MAX() aggregates), so the conversation route 500s with groupBy is not a function on that backend. It works on libsql, where real Drizzle supports groupBy.
  • Net effect: on the default backend, loading the conversation view fails and last-contact-per-character data is unavailable.

What changed

  • packages/server/src/services/storage/chats.storage.ts: replaced the SQL GROUP BY / MAX() aggregation with an in-JS reduction. The query now selects plain characterId + createdAt columns (using only select/from/where, which both backends support) and folds the rows into a { characterId: latestCreatedAt } map. created_at is a TEXT (ISO 8601) column, so lexicographic > is chronological.
  • Dropped the now-unused sql import from drizzle-orm.
  • .groupBy() is used in exactly this one place in the server, so dropping the aggregate here is smaller and safer than teaching the file-store shim SQL aggregation.
  • Behavior is unchanged on the libsql backend (identical result, computed in JS instead of SQL). No schema or API changes.

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

  • Already run: pnpm --filter @marinara-engine/server lint (tsc -b ../shared && tsc --noEmit) passes. Full pnpm check (TypeScript + ESLint across the workspace) has not yet been run.
  • Manually verify on the file-storage backend: open a chat / load the conversation view → confirm the route no longer 500s and last-contact data still populates. (The original groupBy is not a function 500 was reproduced from server logs before the change; in-app confirmation of the fix is pending a rebuild.)
  • Manually verify on the libsql backend: confirm last-contact-per-character data is unchanged (regression check, since this path was previously SQL-side).

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Version/release files updated (only if this PR includes a version bump)

Assessment: storage-internal refactor with no API/schema/behavior change and no version bump, so no docs or release-notes updates appear necessary — left unchecked for the contributor to confirm.

UI evidence (if applicable)

N/A — no UI changes (storage-internal fix).

Summary by CodeRabbit

  • Refactor
    • Optimized backend data retrieval for character contact history.

🔄 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/2776 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 6/22/2026 **Status:** ✅ Merged **Merged:** 6/22/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/issue-2775-last-contact-groupby` --- ### 📝 Commits (1) - [`bc08067`](https://github.com/Pasta-Devs/Marinara-Engine/commit/bc0806718c8e9ad5ba53e7635dee83a77416dcc5) fix(storage): aggregate lastContactByCharacter in JS for file-storage backend ### 📊 Changes **1 file changed** (+15 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/services/storage/chats.storage.ts` (+15 -5) </details> ### 📄 Description ## Linked issue Closes #2775 ## Why this change - `chats.lastContactByCharacter()` built its query with `.groupBy()` plus a `sql` `MAX()` aggregate. The default **file-storage** backend's query-builder shim (`file-backed-store.ts`) implements `select`/`from`/`where`/`orderBy` but **not** `groupBy` (and does not evaluate `sql` `MAX()` aggregates), so the conversation route **500s with `groupBy is not a function`** on that backend. It works on libsql, where real Drizzle supports `groupBy`. - Net effect: on the default backend, loading the conversation view fails and last-contact-per-character data is unavailable. ## What changed - `packages/server/src/services/storage/chats.storage.ts`: replaced the SQL `GROUP BY` / `MAX()` aggregation with an in-JS reduction. The query now selects plain `characterId` + `createdAt` columns (using only `select`/`from`/`where`, which both backends support) and folds the rows into a `{ characterId: latestCreatedAt }` map. `created_at` is a TEXT (ISO 8601) column, so lexicographic `>` is chronological. - Dropped the now-unused `sql` import from `drizzle-orm`. - `.groupBy()` is used in exactly this one place in the server, so dropping the aggregate here is smaller and safer than teaching the file-store shim SQL aggregation. - Behavior is unchanged on the libsql backend (identical result, computed in JS instead of SQL). No schema or API changes. ## 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 - **Already run:** `pnpm --filter @marinara-engine/server lint` (`tsc -b ../shared && tsc --noEmit`) passes. Full `pnpm check` (TypeScript + ESLint across the workspace) has not yet been run. - **Manually verify on the file-storage backend:** open a chat / load the conversation view → confirm the route no longer 500s and last-contact data still populates. (The original `groupBy is not a function` 500 was reproduced from server logs before the change; in-app confirmation of the fix is pending a rebuild.) - **Manually verify on the libsql backend:** confirm last-contact-per-character data is unchanged (regression check, since this path was previously SQL-side). ## Docs and release impact - [x] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Version/release files updated (only if this PR includes a version bump) _Assessment: storage-internal refactor with no API/schema/behavior change and no version bump, so no docs or release-notes updates appear necessary — left unchecked for the contributor to confirm._ ## UI evidence (if applicable) N/A — no UI changes (storage-internal fix). <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Optimized backend data retrieval for character contact history. <!-- 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-22 04:18:53 +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#5724
No description provided.