[PR #2669] [MERGED] fix(llm): harden connection auth, clamp Anthropic temperature, wire stop sequences (#2656, #2657, #2658) #5712

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

📋 Pull Request Information

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

Base: staging ← Head: fix/connection-preset-send-tier1


📝 Commits (2)

  • 70a34c6 fix(llm): harden connection auth, clamp Anthropic temperature, wire stop sequences
  • bef00a9 fix(llm): address review — trim/filter stop sequences and whitespace-only keys

📊 Changes

5 files changed (+70 additions, -9 deletions)

View changed files

📝 packages/server/src/routes/generate.routes.ts (+10 -0)
📝 packages/server/src/services/llm/providers/anthropic.provider.ts (+16 -4)
📝 packages/server/src/services/llm/providers/google.provider.ts (+8 -2)
📝 packages/server/src/services/llm/providers/openai.provider.ts (+5 -1)
📝 packages/server/src/utils/crypto.ts (+31 -2)

📄 Description

Linked issue

Closes #2656
Closes #2657
Closes #2658

Why this change

All three bugs sit on the hot path that defines the product — turning a user's connection settings + preset into an actual API request — and each one silently breaks core usage:

  • #2656 — when a connection's API key can't be decrypted (encryption key changed after a reinstall / moved data dir / ENCRYPTION_KEY change) or an endpoint is intentionally keyless, providers sent a blank Bearer /x-api-key: header → opaque 401 on every generation, with no hint that the real cause is "re-enter your key." A wrong-length ENCRYPTION_KEY was worse still: an uncaught Invalid key length 500 on save and a silently-blanked key on read.
  • #2657 — a preset temperature > 1 (valid on OpenAI, common for SillyTavern migrants, and even suggested by our own tuning docs) was sent unclamped to Anthropic, which only accepts 0–1, so every Anthropic generation 400'd.
  • #2658 — stop sequences were parsed and saved but never attached to the outgoing request for any provider, so a core roleplay control (stopping the model before it writes the user's lines) silently did nothing.

What changed

  • Auth (#2656): the OpenAI / Anthropic / Google providers now omit the auth header entirely when the key is empty, instead of sending a blank credential. getEncryptionKey() validates the resolved key is exactly 32 bytes and fails with a clear, actionable message instead of a cryptic Invalid key length / silent blank. (The wrong-length-key crash was an adjacent bug found while reviewing this code and folded in.)
  • Anthropic temperature (#2657): new clampAnthropicTemperature() clamps to [0, 1] at serialization time only — the stored preset is never mutated, so the same preset still sends e.g. 1.6 to OpenAI-compatible backends that accept 0–2.
  • Stop sequences (#2658): wired stopSequences (preset → connection → chat precedence) into ChatOptions.stop, and added the provider mappings — Anthropic stop_sequences, Google generationConfig.stopSequences (OpenAI already consumed options.stop).

5 files, +64 / −9. No schema, client, or docs changes.

Validation

ℹ️ Boxes are intentionally left unchecked — per CONTRIBUTING.md / CLAUDE.md, these are a human to-do list, not proof of work done by an agent.

Automated (done by the agent): pnpm check passed locally — full TypeScript + ESLint + build; the server tsc build exited 0.

Not done (needs a human): the app/container was not run and the providers were not exercised against live APIs. Please complete the manual checks below before marking this ready for review.

  • 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

Suggested checks, each mapped to a fix:

  1. Anthropic temp clamp (#2657): On a Claude-direct connection, set a preset temperature: 1.5 and send a message → should succeed (outgoing body carries temperature: 1.0), and the preset should still read 1.5. On an OpenAI connection the same preset should still send 1.5.
  2. Stop sequences (#2658): Set a stop sequence (e.g. \nUser:) and confirm generation halts at it on OpenAI, Anthropic, and Gemini connections (inspect the outgoing request body / logPromptSentToModel).
  3. Empty-key auth (#2656): Point a connection at a keyless local OpenAI-compatible endpoint → request should carry no Authorization header. Simulate a decrypt failure (change ENCRYPTION_KEY after saving a key) → generation should fail cleanly rather than sending Bearer .
  4. Key validation (#2656): Set ENCRYPTION_KEY=abc123 (wrong length), restart, save a connection key → expect a clear "must be 64 hexadecimal characters" error, not an Invalid key length 500.

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)

No docs/version changes are included. A follow-up note in docs/GENERATION_PARAMETERS.md (Anthropic now clamps temperature > 1 rather than erroring) would be a nice addition but isn't required for this fix.

UI evidence (if applicable)

N/A — server-only change.

Summary by CodeRabbit

Release Notes

  • New Features

    • Added support for configurable stop sequences in LLM generation requests and advanced parameters.
  • Bug Fixes

    • Improved encryption key validation with clearer error messages for invalid keys.
    • Fixed API authentication header handling to prevent invalid token submissions across providers.
    • Enhanced temperature parameter handling to respect provider constraints.

🔄 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/2669 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 6/20/2026 **Status:** ✅ Merged **Merged:** 6/21/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/connection-preset-send-tier1` --- ### 📝 Commits (2) - [`70a34c6`](https://github.com/Pasta-Devs/Marinara-Engine/commit/70a34c6507fa299e3773b2cd7d5b3f3465b33ceb) fix(llm): harden connection auth, clamp Anthropic temperature, wire stop sequences - [`bef00a9`](https://github.com/Pasta-Devs/Marinara-Engine/commit/bef00a9043fd4cf016d402c9532bf93d6926837f) fix(llm): address review — trim/filter stop sequences and whitespace-only keys ### 📊 Changes **5 files changed** (+70 additions, -9 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/routes/generate.routes.ts` (+10 -0) 📝 `packages/server/src/services/llm/providers/anthropic.provider.ts` (+16 -4) 📝 `packages/server/src/services/llm/providers/google.provider.ts` (+8 -2) 📝 `packages/server/src/services/llm/providers/openai.provider.ts` (+5 -1) 📝 `packages/server/src/utils/crypto.ts` (+31 -2) </details> ### 📄 Description <!-- Target branch: `staging` --> ## Linked issue Closes #2656 Closes #2657 Closes #2658 ## Why this change All three bugs sit on the hot path that defines the product — turning a user's connection settings + preset into an actual API request — and each one silently breaks core usage: - **#2656** — when a connection's API key can't be decrypted (encryption key changed after a reinstall / moved data dir / `ENCRYPTION_KEY` change) or an endpoint is intentionally keyless, providers sent a **blank** `Bearer `/`x-api-key:` header → opaque 401 on every generation, with no hint that the real cause is "re-enter your key." A wrong-length `ENCRYPTION_KEY` was worse still: an uncaught `Invalid key length` 500 on save and a silently-blanked key on read. - **#2657** — a preset `temperature > 1` (valid on OpenAI, common for SillyTavern migrants, and even suggested by our own tuning docs) was sent unclamped to Anthropic, which only accepts `0–1`, so **every** Anthropic generation 400'd. - **#2658** — stop sequences were parsed and saved but never attached to the outgoing request for **any** provider, so a core roleplay control (stopping the model before it writes the user's lines) silently did nothing. ## What changed - **Auth (#2656):** the OpenAI / Anthropic / Google providers now omit the auth header entirely when the key is empty, instead of sending a blank credential. `getEncryptionKey()` validates the resolved key is exactly 32 bytes and fails with a clear, actionable message instead of a cryptic `Invalid key length` / silent blank. *(The wrong-length-key crash was an adjacent bug found while reviewing this code and folded in.)* - **Anthropic temperature (#2657):** new `clampAnthropicTemperature()` clamps to `[0, 1]` **at serialization time only** — the stored preset is never mutated, so the same preset still sends e.g. `1.6` to OpenAI-compatible backends that accept `0–2`. - **Stop sequences (#2658):** wired `stopSequences` (preset → connection → chat precedence) into `ChatOptions.stop`, and added the provider mappings — Anthropic `stop_sequences`, Google `generationConfig.stopSequences` (OpenAI already consumed `options.stop`). 5 files, +64 / −9. No schema, client, or docs changes. ## Validation > ℹ️ Boxes are intentionally left unchecked — per `CONTRIBUTING.md` / `CLAUDE.md`, these are a human to-do list, not proof of work done by an agent. **Automated (done by the agent):** `pnpm check` passed locally — full TypeScript + ESLint + build; the server `tsc` build exited `0`. **Not done (needs a human):** the app/container was not run and the providers were not exercised against live APIs. Please complete the manual checks below before marking this ready for review. - [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 Suggested checks, each mapped to a fix: 1. **Anthropic temp clamp (#2657):** On a Claude-direct connection, set a preset `temperature: 1.5` and send a message → should succeed (outgoing body carries `temperature: 1.0`), and the preset should still read `1.5`. On an OpenAI connection the same preset should still send `1.5`. 2. **Stop sequences (#2658):** Set a stop sequence (e.g. `\nUser:`) and confirm generation halts at it on OpenAI, Anthropic, and Gemini connections (inspect the outgoing request body / `logPromptSentToModel`). 3. **Empty-key auth (#2656):** Point a connection at a keyless local OpenAI-compatible endpoint → request should carry no `Authorization` header. Simulate a decrypt failure (change `ENCRYPTION_KEY` after saving a key) → generation should fail cleanly rather than sending `Bearer `. 4. **Key validation (#2656):** Set `ENCRYPTION_KEY=abc123` (wrong length), restart, save a connection key → expect a clear "must be 64 hexadecimal characters" error, not an `Invalid key length` 500. ## 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) No docs/version changes are included. A follow-up note in `docs/GENERATION_PARAMETERS.md` (Anthropic now clamps `temperature > 1` rather than erroring) would be a nice addition but isn't required for this fix. ## UI evidence (if applicable) N/A — server-only change. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes * **New Features** * Added support for configurable stop sequences in LLM generation requests and advanced parameters. * **Bug Fixes** * Improved encryption key validation with clearer error messages for invalid keys. * Fixed API authentication header handling to prevent invalid token submissions across providers. * Enhanced temperature parameter handling to respect provider constraints. <!-- 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:45 +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#5712
No description provided.