Skip to content

fix(providers): share the caller's own session with the model-provider port - #1410

Open
AmirF194 wants to merge 1 commit into
mozilla-ai:mainfrom
AmirF194:fix/1266-model-provider-port-second-session
Open

AmirF194 wants to merge 1 commit into
mozilla-ai:mainfrom
AmirF194:fix/1266-model-provider-port-second-session

Conversation

@AmirF194

@AmirF194 AmirF194 commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Description

Catalog, models, organization pricing, organization routing, and the playground chat route resolve ModelProviderPort through get_model_provider_port, which takes its session from get_db_if_needed. Since these routes take get_db for their own session, FastAPI treats the two as separate dependencies and opens a second, independent session for the port instead of sharing the caller's. Added get_model_provider_port_shared, which takes the session from get_db directly like the three sibling ports (get_growth_signal_port, get_identity_provider_port, get_telemetry_storage_port) already do, and switched those five routes to it.

Chat, messages, and responses keep the existing get_model_provider_port: they take get_db_if_needed themselves, and hybrid mode has no local database at all.

The core SelfHostedModelProviderAdapter discards the session it's given, so nothing observable changes for the OSS edition today. An overlay adapter that reads or writes through the port on one of these five routes is the one this fixes for.

How to test it locally

uv run pytest tests/unit/test_model_provider_port_management_route_session.py. It builds get_organization_pricing_service behind a bare route and checks the session ModelProviderPort gets built with is the same object as the route's own db; red without the change, green with it.

PR Type

  • Bug Fix

Relevant issues

Fixes #1266

Checklist

  • I understand the code I am submitting.
  • I have added or updated tests that cover my change (tests/unit, tests/integration).
  • I ran the Definition of Done checks locally (make lint, make typecheck, make test).
  • Documentation was updated where necessary.
  • If the API contract changed, I regenerated the OpenAPI spec (uv run python scripts/generate_openapi.py).
  • If this changes a rule in ARCHITECTURE.md or scripts/check_architecture.py, the description names the rule and says why.

make lint, make typecheck, the openapi/postman/architecture checks, and the unit suite all pass locally. Could not get test-integration running in my sandbox (its Postgres testcontainer needs a Docker socket my own container can't reach), so that leg is unverified here; nothing in the diff touches a route schema or a migration.

AI Usage

  • No AI was used.
  • AI was used for drafting/refactoring.
  • This is fully AI-generated.

AI Model/Tool used:

Any additional AI details you'd like to share:

Summary

  • Added a model-provider dependency that reuses the database session already used by the route.
  • Updated catalog, models, organization pricing, organization routing, and playground chat routes to use it.
  • Kept the existing dependency for chat, messages, and responses to support hybrid mode without a local database.
  • Added a regression test to verify that the provider receives the route’s session.

This avoids opening an extra database connection on the updated routes.

@AmirF194
AmirF194 deployed to integration-tests September 21, 2026 01:13 — with GitHub Actions Active
@AmirF194
AmirF194 deployed to integration-tests September 21, 2026 01:13 — with GitHub Actions Active
@AmirF194
AmirF194 deployed to integration-tests September 21, 2026 01:13 — with GitHub Actions Active
@AmirF194
AmirF194 deployed to integration-tests September 21, 2026 01:13 — with GitHub Actions Active
@coderabbitai

coderabbitai Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository: mozilla-ai/otari/.coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: 09ffb834-7313-48fd-913b-5275f4e24036

📥 Commits

Reviewing files that changed from the base of the PR and between d42d385 and d7101f8.

📒 Files selected for processing (6)
  • src/gateway/api/deps.py
  • src/gateway/api/routes/catalog.py
  • src/gateway/api/routes/models.py
  • src/gateway/api/routes/organization_pricing.py
  • src/gateway/api/routes/organization_routing.py
  • src/gateway/api/routes/playground.py

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 0 remain after this review.


Walkthrough

Changes

Management-plane routes now resolve the model-provider port with the route’s existing get_db session. The data-plane dependency remains unchanged. A regression test checks session reuse.

Shared model-provider session

Layer / File(s) Summary
Shared provider dependency
src/gateway/api/deps.py
Adds get_model_provider_port_shared and the ModelProviderPortSharedDep alias.
Management-plane route wiring
src/gateway/api/routes/catalog.py, src/gateway/api/routes/models.py, src/gateway/api/routes/organization_pricing.py, src/gateway/api/routes/organization_routing.py, src/gateway/api/routes/playground.py
Updates these routes to use the shared dependency alias.
Session reuse regression test
tests/unit/test_model_provider_port_management_route_session.py
Checks that provider resolution receives the organization pricing service’s existing session and occurs once.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: tbille

Merge Risk: ⚪ Minimal · up to d7101

The management routes now reuse their request database session, while data-plane routes retain their existing dependency. No merge-blocking risk was identified.

Security Architecture Review

Security architecture risk: 🔵 Low · up to d7101

Existing access checks remain in place, and the built-in provider does not use the session. The change could affect deployment-specific providers that read or write data, but their behavior has not been verified.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The affected entrypoints include catalog reads, tenant management writes, and signed-in playground completions. For the built-in adapter, changing its session argument does not change provider data or credentials; behavior of deployment-specific adapters remains unverified.

Trust Boundaries and Controls

  • observed — The inspected routes continue to derive identity or catalog-reader authority through their existing dependencies. The playground passes its resolved principal to completion handling, while the provider contract requires an explicit organization identifier for credential resolution.

Hardening Proposals

  • proposed — Before relying on session sharing in a deployment-specific adapter, exercise its tenant scoping and commit, rollback, and interruption behavior on the affected read, write, and streaming paths. Audit other get_db-backed provider consumers if uniform session ownership is the goal.
🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Title check ⚠️ Warning The title accurately describes the change and uses imperative mood, but it uses the scoped form fix(providers): instead of the required fix: prefix and exceeds the approximate 70-character limit a… Rename the title to a compliant form such as fix: share the caller's own session with the model-provider port. It uses the required prefix and stays under 70 characters.
Docstring Coverage ⚠️ Warning Docstring coverage is 64.71% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 17 functions across 7 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Description check ✅ Passed The description includes the required sections, explains the session-sharing bug, documents local testing, identifies the bug-fix type, links issue #1266, and records the integration-test limitation. …
Linked Issues check ✅ Passed The pull request satisfies the coding requirements in issue #1266. get_model_provider_port_shared receives get_db, and the catalog, models, organization pricing, organization routing, and playgrou…
Out of Scope Changes check ✅ Passed The changes stay within issue #1266. The dependency addition implements session sharing, the five route groups select that dependency, and the test verifies session identity. No unrelated change is id…
Full details: Title check

Explanation

The title accurately describes the change and uses imperative mood, but it uses the scoped form fix(providers): instead of the required fix: prefix and exceeds the approximate 70-character limit at 75 characters.

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
✨ Simplify code
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov-commenter

codecov-commenter commented Sep 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Flag Coverage Δ
integration 83.21% <100.00%> (?)
unit 73.36% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/gateway/api/deps.py 95.57% <100.00%> (ø)
src/gateway/api/routes/catalog.py 96.55% <ø> (ø)
src/gateway/api/routes/models.py 99.15% <ø> (ø)
src/gateway/api/routes/organization_pricing.py 92.68% <100.00%> (ø)
src/gateway/api/routes/organization_routing.py 90.74% <100.00%> (ø)
src/gateway/api/routes/playground.py 98.13% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AmirF194
AmirF194 force-pushed the fix/1266-model-provider-port-second-session branch from b83219b to d42d385 Compare September 22, 2026 15:21
@AmirF194
AmirF194 deployed to integration-tests September 22, 2026 15:21 — with GitHub Actions Active
@AmirF194
AmirF194 deployed to integration-tests September 22, 2026 15:21 — with GitHub Actions Active
@AmirF194
AmirF194 deployed to integration-tests September 22, 2026 15:21 — with GitHub Actions Active
@AmirF194
AmirF194 deployed to integration-tests September 22, 2026 15:21 — with GitHub Actions Active
…r port

Catalog, models, organization pricing, organization routing, and the
playground chat route take get_db for their session but resolved
ModelProviderPort through get_model_provider_port, which takes its
session from get_db_if_needed. FastAPI treats those as separate
dependencies and opens a second, independent session for the port
instead of sharing the caller's.

Added get_model_provider_port_shared, which takes the session from
get_db directly like the three sibling ports already do, and switched
those five routes to it. Chat, messages, and responses keep the
existing dependency: they take get_db_if_needed themselves, and
hybrid mode has no local database at all.

Fixes mozilla-ai#1266
@AmirF194
AmirF194 force-pushed the fix/1266-model-provider-port-second-session branch from d42d385 to d7101f8 Compare September 27, 2026 08:28
@AmirF194
AmirF194 deployed to integration-tests September 27, 2026 08:28 — with GitHub Actions Active
@AmirF194
AmirF194 deployed to integration-tests September 27, 2026 08:28 — with GitHub Actions Active
@AmirF194
AmirF194 deployed to integration-tests September 27, 2026 08:28 — with GitHub Actions Active
@AmirF194
AmirF194 deployed to integration-tests September 27, 2026 08:28 — with GitHub Actions Active
@AmirF194

Copy link
Copy Markdown
Contributor Author

CI is green here and it's been about a week with no review yet. Let me know if anything needs more context.

This branch was successfully deployed

1 active deployment
integration-tests — d7101f89 Deployed Sep 27, 2026 by AmirF194 via test-integration (3/4) #2878
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The model-provider port opens a second database session on routes that use get_db

2 participants