refactor: use sessionmaker().begin() in console workspace and misc co…#34284
Merged
asukaminato0721 merged 2 commits intolanggenius:mainfrom Mar 31, 2026
Merged
Conversation
Contributor
Pyrefly DiffNo changes detected. |
Contributor
There was a problem hiding this comment.
Pull request overview
Refactors several console controllers to use sessionmaker(...).begin() context blocks for transaction framing, aligning controller DB session handling with SQLAlchemy 2.x session best practices.
Changes:
- Replaced
with Session(db.engine) as session:(and explicitcommit()/session.begin()) withwith sessionmaker(db.engine).begin() as session:in targeted endpoints. - Removed now-redundant explicit
session.commit()calls wherebegin()handles commit/rollback automatically. - Updated imports from
Sessiontosessionmakeracross the affected modules.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| api/controllers/console/apikey.py | Uses sessionmaker(...).begin() for _get_resource() session scope. |
| api/controllers/console/explore/conversation.py | Uses sessionmaker(...).begin() for conversation pagination query. |
| api/controllers/console/workspace/init.py | Uses sessionmaker(...).begin() for plugin permission lookup in decorator. |
| api/controllers/console/workspace/account.py | Uses sessionmaker(...).begin() for account lookup during change-email flow. |
| api/controllers/console/workspace/trigger_providers.py | Uses sessionmaker(...).begin() for deletion transaction and removes explicit commit. |
| api/controllers/console/workspace/tool_providers.py | Uses sessionmaker(...).begin() across MCP provider CRUD/auth flows. |
Comments suppressed due to low confidence (2)
api/controllers/console/workspace/account.py:570
AccountService.get_account_by_email_with_case_fallback()returns an ORMAccount. Wrapping this read insessionmaker(...).begin()will commit on exit and (with SQLAlchemy defaults) expire the instance; accessingaccount.emailafter the context can raiseDetachedInstanceError. Consider either (a) using a non-committing session context for this read, or (b) extracting the needed scalar fields (email) inside the session before leaving, or (c) configuring the sessionmaker withexpire_on_commit=Falsefor this block.
with sessionmaker(db.engine).begin() as session:
account = AccountService.get_account_by_email_with_case_fallback(args.email, session=session)
if account is None:
raise AccountNotFound()
email_for_sending = account.email
user_email = account.email
api/controllers/console/workspace/init.py:39
- The early
return view(*args, **kwargs)runs while insidesessionmaker(...).begin(), so the entire view executes inside a DB transaction and the commit happens only after the view returns. This can unintentionally hold locks/transactions open for the full request. Consider limiting the session/transaction scope to just the permission query (compute an allow/deny decision, exit the context, then callview).
with sessionmaker(db.engine).begin() as session:
permission = (
session.query(TenantPluginPermission)
.where(
TenantPluginPermission.tenant_id == tenant_id,
)
.first()
)
if not permission:
# no permission set, allow access for everyone
return view(*args, **kwargs)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Contributor
Pyrefly DiffNo changes detected. |
1 similar comment
Contributor
Pyrefly DiffNo changes detected. |
8fcdaad to
3744caa
Compare
Contributor
Pyrefly DiffNo changes detected. |
asukaminato0721
approved these changes
Mar 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Part of #24245.
Refactor remaining console controllers (workspace, apikey, explore) to use
sessionmaker().begin()instead ofSession()with explicitcommit()orsession.begin(), following SQLAlchemy best practices.Files changed
api/controllers/console/apikey.pyapi/controllers/console/explore/conversation.pyapi/controllers/console/workspace/__init__.pyapi/controllers/console/workspace/account.pyapi/controllers/console/workspace/trigger_providers.pyapi/controllers/console/workspace/tool_providers.py