[PR #4514] [MERGED] fix(generation): centralize connection admission and provider fallback #6406

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/4514
Author: @Gunterlie
Created: 8/3/2026
Status: ✅ Merged
Merged: 8/3/2026
Merged by: @Gunterlie

Base: staging ← Head: fix/connection-admission


📝 Commits (7)

  • d9d3af4 fix(generation): centralize connection admission and provider fallback
  • 7f4bb3d fix(generation): wire background admission and share one attempt across fallback
  • b63cf27 Merge branch 'staging' into fix/connection-admission
  • dd1443a fix(noodle): let connection admission failures defer automatic image work
  • 1b6ddb3 Merge remote-tracking branch 'origin/fix/connection-admission' into fix/connection-admission
  • 5d35faf fix(generation): settle a fallback chain on the chain's own outcome
  • 4d8fcb6 fix(generation): settle a fallback chain completed once usable output is delivered

📊 Changes

26 files changed (+991 additions, -44 deletions)

View changed files

📝 packages/server/src/routes/connections.routes.ts (+3 -0)
📝 packages/server/src/routes/encounter.routes.ts (+3 -0)
📝 packages/server/src/routes/generate.routes.ts (+8 -0)
📝 packages/server/src/routes/generate/dry-run-route.ts (+3 -1)
📝 packages/server/src/routes/lorebooks.routes.ts (+1 -0)
📝 packages/server/src/routes/noodle.routes.ts (+17 -1)
📝 packages/server/src/services/capability-packages/capability-language-model.service.ts (+4 -1)
➕ packages/server/src/services/generation/connection-admission.ts (+263 -0)
📝 packages/server/src/services/image/image-generation.ts (+59 -2)
📝 packages/server/src/services/llm/base-provider.ts (+20 -11)
📝 packages/server/src/services/llm/connection-fallback-provider.ts (+94 -21)
📝 packages/server/src/services/llm/provider-registry.ts (+5 -1)
📝 packages/server/src/services/memory-recall-embedding.ts (+1 -0)
📝 packages/server/src/services/noodle/noodle-autopost-scheduler.service.ts (+16 -5)
📝 packages/server/src/services/noodle/noodle-generated-activity.service.ts (+9 -0)
📝 packages/server/src/services/noodle/noodle-image-retry.ts (+5 -0)
📝 packages/server/src/services/noodle/noodle-noodler-generation.service.ts (+11 -0)
📝 packages/server/src/services/noodle/noodle-noodler-images.service.ts (+3 -0)
📝 packages/server/src/services/noodle/noodle-noodler-post.operation.ts (+3 -1)
📝 packages/server/src/services/noodle/noodle-public-generation.service.ts (+5 -0)

...and 6 more files

📄 Description

Why

Automatic work (NoodleR autopost, the timeline refresh scheduler) shares API connections with
whatever the user is generating right now. Nothing coordinated the two, so a scheduled run could
start on a connection mid-conversation and compete for the same rate limit, parallel-job budget,
or local model. This adds one admission layer that every generation path routes through:
foreground work always proceeds, background work yields to it and waits out a short idle window.

Fallback and cleanup came along because they are the same problem: an admitted slot that is never
released is worse than no admission at all, and a fallback that resolves a logical attempt must
report that attempt's real outcome.

What changed

  • Admission core (services/generation/connection-admission.ts): per-connection foreground
    count and background flag, a 30s post-foreground idle window, and a provider decorator that
    holds a slot for the duration of a chat, completion, or embed call.
  • Background producers: the NoodleR autopost scheduler and the automatic timeline refresh now
    run as background. A busy connection is a deferral, not a failure — the refresh route answers
    409 so the scheduler retries shortly instead of backing off as a configuration error.
  • Stream cleanup: the main generation route and the fallback provider close their generators
    on abort, disconnect, and token-write failure, so slots are always released.
  • One attempt across fallback: a background attempt is booked once on the primary and
    finalized once after the primary-plus-fallback chain settles, so a successful fallback is not
    recorded as a failed attempt. Admission rejections are rethrown rather than triggering fallback.
  • Image admission keys are built from the physical endpoint: RunPod endpoint ids stay
    separate, and the origin//v1 spellings of one OpenAI-compatible endpoint converge.

Linked issue

No issue is linked. One should be opened for this before merge.

Automated validation (run locally, passing)

  • pnpm check
  • pnpm regression:providers — admission rejection, fallback skip on admission failure, attempt
    outcomes across a successful and a failed fallback, stream abandonment releasing the slot,
    image admission key convergence and separation.
  • pnpm regression:noodle

Manual verification still needed

Automated coverage is unit/regression level only — no test exercises a real scheduler racing a
real user generation against a live provider. Please verify by hand:

  • Start a long streaming generation on connection A, then trigger the NoodleR autopost
    scheduler for a creator on connection A; confirm the automatic post is deferred (logged as
    [noodle-autopost] Deferred ...) rather than running concurrently.
  • Repeat with the automatic timeline refresh; confirm the scheduler retries in ~60s instead
    of recording a configuration failure and backing off for minutes.
  • Confirm a manual "Refresh NoodleR now" and a manual timeline refresh still run immediately
    while nothing else is generating (manual paths must stay foreground).
  • Abort a streaming generation mid-response, then start scheduled work on that connection
    after the idle window; confirm it is admitted (no stranded foreground slot).
  • Run a dry run against the local sidecar while a local-sidecar generation is active; confirm
    behavior is unchanged for the user.
  • Generate an image on an OpenAI-compatible connection configured with the bare origin, and
    again with the /v1 form; confirm both still generate successfully.

Known gaps

  • Only the Noodle schedulers produce background mode. Local-sidecar consumers (capability LLM,
    Professor Mari workspace, memory recall, lorebook vectorization, and three more) still run
    unadmitted; tracked as #4520. Follow-up adoption, not regressions.

Summary by CodeRabbit

  • New Features

    • Added connection-aware generation controls to LLM, embedding, and image requests.
    • Prevented overlapping foreground work and deferred background generation when connections are busy.
    • Added clear conflict responses when generation cannot start immediately.
    • Applied admission handling across Noodle generation, scheduled posts, refreshes, and image retries.
  • Bug Fixes

    • Improved cleanup of interrupted streaming responses.
    • Preserved original generation errors during stream cleanup.
    • Prevented connection-limit failures from triggering unnecessary fallback or retries.

🔄 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/4514 **Author:** [@Gunterlie](https://github.com/Gunterlie) **Created:** 8/3/2026 **Status:** ✅ Merged **Merged:** 8/3/2026 **Merged by:** [@Gunterlie](https://github.com/Gunterlie) **Base:** `staging` ← **Head:** `fix/connection-admission` --- ### 📝 Commits (7) - [`d9d3af4`](https://github.com/Pasta-Devs/Marinara-Engine/commit/d9d3af4b8e73a58a94b47afd5b1ca3da0ab0aa70) fix(generation): centralize connection admission and provider fallback - [`7f4bb3d`](https://github.com/Pasta-Devs/Marinara-Engine/commit/7f4bb3d706baab887e1531820e6eff65fa40eb3b) fix(generation): wire background admission and share one attempt across fallback - [`b63cf27`](https://github.com/Pasta-Devs/Marinara-Engine/commit/b63cf274eeda15807352dbd4ab8a7eb85837b24f) Merge branch 'staging' into fix/connection-admission - [`dd1443a`](https://github.com/Pasta-Devs/Marinara-Engine/commit/dd1443ae74ca656385bc4c6e663eb7b922133955) fix(noodle): let connection admission failures defer automatic image work - [`1b6ddb3`](https://github.com/Pasta-Devs/Marinara-Engine/commit/1b6ddb382c388db674ed9fa8e8270e0807f70dd8) Merge remote-tracking branch 'origin/fix/connection-admission' into fix/connection-admission - [`5d35faf`](https://github.com/Pasta-Devs/Marinara-Engine/commit/5d35faf874bb8d625ba5ca391729b0246ca712a2) fix(generation): settle a fallback chain on the chain's own outcome - [`4d8fcb6`](https://github.com/Pasta-Devs/Marinara-Engine/commit/4d8fcb6a02ed812426090aa7caa13d6b4b4a3c64) fix(generation): settle a fallback chain completed once usable output is delivered ### 📊 Changes **26 files changed** (+991 additions, -44 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/routes/connections.routes.ts` (+3 -0) 📝 `packages/server/src/routes/encounter.routes.ts` (+3 -0) 📝 `packages/server/src/routes/generate.routes.ts` (+8 -0) 📝 `packages/server/src/routes/generate/dry-run-route.ts` (+3 -1) 📝 `packages/server/src/routes/lorebooks.routes.ts` (+1 -0) 📝 `packages/server/src/routes/noodle.routes.ts` (+17 -1) 📝 `packages/server/src/services/capability-packages/capability-language-model.service.ts` (+4 -1) ➕ `packages/server/src/services/generation/connection-admission.ts` (+263 -0) 📝 `packages/server/src/services/image/image-generation.ts` (+59 -2) 📝 `packages/server/src/services/llm/base-provider.ts` (+20 -11) 📝 `packages/server/src/services/llm/connection-fallback-provider.ts` (+94 -21) 📝 `packages/server/src/services/llm/provider-registry.ts` (+5 -1) 📝 `packages/server/src/services/memory-recall-embedding.ts` (+1 -0) 📝 `packages/server/src/services/noodle/noodle-autopost-scheduler.service.ts` (+16 -5) 📝 `packages/server/src/services/noodle/noodle-generated-activity.service.ts` (+9 -0) 📝 `packages/server/src/services/noodle/noodle-image-retry.ts` (+5 -0) 📝 `packages/server/src/services/noodle/noodle-noodler-generation.service.ts` (+11 -0) 📝 `packages/server/src/services/noodle/noodle-noodler-images.service.ts` (+3 -0) 📝 `packages/server/src/services/noodle/noodle-noodler-post.operation.ts` (+3 -1) 📝 `packages/server/src/services/noodle/noodle-public-generation.service.ts` (+5 -0) _...and 6 more files_ </details> ### 📄 Description ## Why Automatic work (NoodleR autopost, the timeline refresh scheduler) shares API connections with whatever the user is generating right now. Nothing coordinated the two, so a scheduled run could start on a connection mid-conversation and compete for the same rate limit, parallel-job budget, or local model. This adds one admission layer that every generation path routes through: foreground work always proceeds, background work yields to it and waits out a short idle window. Fallback and cleanup came along because they are the same problem: an admitted slot that is never released is worse than no admission at all, and a fallback that resolves a logical attempt must report that attempt's real outcome. ## What changed - **Admission core** (`services/generation/connection-admission.ts`): per-connection foreground count and background flag, a 30s post-foreground idle window, and a provider decorator that holds a slot for the duration of a chat, completion, or embed call. - **Background producers**: the NoodleR autopost scheduler and the automatic timeline refresh now run as background. A busy connection is a deferral, not a failure — the refresh route answers 409 so the scheduler retries shortly instead of backing off as a configuration error. - **Stream cleanup**: the main generation route and the fallback provider close their generators on abort, disconnect, and token-write failure, so slots are always released. - **One attempt across fallback**: a background attempt is booked once on the primary and finalized once after the primary-plus-fallback chain settles, so a successful fallback is not recorded as a failed attempt. Admission rejections are rethrown rather than triggering fallback. - **Image admission keys** are built from the physical endpoint: RunPod endpoint ids stay separate, and the origin/`/v1` spellings of one OpenAI-compatible endpoint converge. ## Linked issue No issue is linked. One should be opened for this before merge. ## Automated validation (run locally, passing) - `pnpm check` - `pnpm regression:providers` — admission rejection, fallback skip on admission failure, attempt outcomes across a successful and a failed fallback, stream abandonment releasing the slot, image admission key convergence and separation. - `pnpm regression:noodle` ## Manual verification still needed Automated coverage is unit/regression level only — no test exercises a real scheduler racing a real user generation against a live provider. Please verify by hand: - [ ] Start a long streaming generation on connection A, then trigger the NoodleR autopost scheduler for a creator on connection A; confirm the automatic post is deferred (logged as `[noodle-autopost] Deferred ...`) rather than running concurrently. - [ ] Repeat with the automatic timeline refresh; confirm the scheduler retries in ~60s instead of recording a configuration failure and backing off for minutes. - [ ] Confirm a manual "Refresh NoodleR now" and a manual timeline refresh still run immediately while nothing else is generating (manual paths must stay foreground). - [ ] Abort a streaming generation mid-response, then start scheduled work on that connection after the idle window; confirm it is admitted (no stranded foreground slot). - [ ] Run a dry run against the local sidecar while a local-sidecar generation is active; confirm behavior is unchanged for the user. - [ ] Generate an image on an OpenAI-compatible connection configured with the bare origin, and again with the `/v1` form; confirm both still generate successfully. ## Known gaps - Only the Noodle schedulers produce background mode. Local-sidecar consumers (capability LLM, Professor Mari workspace, memory recall, lorebook vectorization, and three more) still run unadmitted; tracked as #4520. Follow-up adoption, not regressions. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Added connection-aware generation controls to LLM, embedding, and image requests. - Prevented overlapping foreground work and deferred background generation when connections are busy. - Added clear conflict responses when generation cannot start immediately. - Applied admission handling across Noodle generation, scheduled posts, refreshes, and image retries. - **Bug Fixes** - Improved cleanup of interrupted streaming responses. - Preserved original generation errors during stream cleanup. - Prevented connection-limit failures from triggering unnecessary fallback or retries. <!-- 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:09 +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#6406
No description provided.