Skip to content

refactor(models): remove implicit sessions from workflow_run and credential accessors - #45168

Open
xiaoyuCR7 wants to merge 2 commits into
langgenius:mainfrom
xiaoyuCR7:refactor/models-workflow-run-explicit-session
Open

xiaoyuCR7 wants to merge 2 commits into
langgenius:mainfrom
xiaoyuCR7:refactor/models-workflow-run-explicit-session

Conversation

@xiaoyuCR7

@xiaoyuCR7 xiaoyuCR7 commented Oct 30, 2026 •

Copy link
Copy Markdown

Fixes #45166. _resolve_workflow_app_tenant_id is left to #43112, which already
handles it and has auto-merge enabled, so this PR does not touch it.

Summary

Message.workflow_run and WorkflowAppLog.workflow_run each build their own
sessionmaker from the global engine. I found no callers, so this PR removes
them instead of adding a session parameter.

  • The console and service API app log endpoints serialize the DTOs built by
    WorkflowAppLogQueryRepository, not these ORM properties.
  • The only log.workflow_run access I found, in the e2e tests, reads that DTO.
  • No response schema reads workflow_run from a Message.
  • The db import in models/model.py became unused, so it is removed, along
    with the models.model.db patch in test_human_input_form.py.

Provider credentials

Provider.credential and ProviderModel.credential were cached_property
accessors that opened their own session. ProviderManager already preloads these
rows in batches, so that lookup was only a hidden fallback.

  • Provider.credential now reads a value attached by the caller through
    attach_credential and never queries. It raises RuntimeError if the credential
    was not attached, so a missing load cannot look like an unset credential.
  • ProviderModel.credential, credential_name and encrypted_config had no
    readers and are removed. The loaded row is passed to
    _ProviderModelCacheEntry.from_record directly.
  • _init_trial_provider_records could return an existing record from its
    IntegrityError branch without the batch load, so that branch attaches the
    credential too. This keeps the previous behavior for such a record.

Behavior change to review: reading a credential on a Provider loaded outside
ProviderManager without attaching now raises instead of querying. I found no
such reader in production code.

@GYJ99 I saw your comment on the same two accessors after preparing this
change. I'm opening it because the audit suggests they can be deleted. Happy to coordinate if you already have work in progress.

Testing

Run locally on Windows with Python 3.13, without the vdb-all dependency group:

  • pytest on tests/unit_tests/models, core/entities, core/ops, core/app/llm,
    core/test_provider_manager.py, and the human input and workflow log tests:
    1061 passed, 1 error. The error is a Windows temp file PermissionError in
    test_trace_entity.py, and it also occurs on the unmodified base commit.
  • Removing the new attach call from the IntegrityError branch makes its
    regression test fail with the expected RuntimeError.
  • pyrefly check on the four changed source files: 0 diagnostics.
  • lint-imports: 76 contracts kept, 0 broken.
  • ruff format and ruff check pass on the changed files.

Not verified:

  • The full unit test suite and make lint && make type-check were not run.
  • Local runs used --no-cov and a stub for python-magic, because importing
    libmagic segfaults on this machine. CI is awaiting maintainer approval.

Screenshots

N/A, no UI change.

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've verified the change and added or updated tests where meaningful regression risk justifies coverage.
  • I've updated the documentation accordingly.
  • I ran make lint && make type-check (backend) and vp staged (frontend) to appease the lint gods

From Claude Code

🤖 Generated with Claude Code

Message.workflow_run and WorkflowAppLog.workflow_run built their own
sessionmaker from the global engine, but nothing reads them: the app log
endpoints serialize DTOs from WorkflowAppLogQueryRepository instead.

Remove both, the db import in models/model.py they left unused, and the
test patch of that import.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Oct 30, 2026
@xiaoyuCR7
xiaoyuCR7 marked this pull request as ready for review October 30, 2026 15:04
Provider.credential and ProviderModel.credential were cached properties
that opened their own session. ProviderManager already preloads these rows
in batches, so the lookup only served as a hidden fallback.

Provider now reads a credential attached by the caller and raises when it
was not attached. ProviderModel's accessors had no readers and are removed;
the loaded credential is passed to the cache entry directly.

The IntegrityError branch of _init_trial_provider_records returned records
that bypassed the batch load, so it now attaches their credential too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Oct 30, 2026
@xiaoyuCR7 xiaoyuCR7 changed the title refactor(models): remove unused workflow_run accessors refactor(models): remove implicit sessions from workflow_run and credential accessors Oct 30, 2026

GYJ99 commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Thanks for the heads-up, @xiaoyuCR7. My earlier comment on #45166 was a proposed scope and test plan; I have not started a competing implementation. Your call-site audit covers the same Message.workflow_run and WorkflowAppLog.workflow_run accessors, so I won't open a duplicate PR. Removing truly unused properties looks preferable to adding a session parameter solely to preserve them. I appreciate you documenting the behavior change for provider credentials and the actual test limitations. I'll follow the review and help with focused verification if there's an uncovered case. Thanks for coordinating!

@Infernoooooo7

Copy link
Copy Markdown

Hey! I tested your changes locally and added two additional regression tests in test_provider_manager.py.

They cover:

  • Handling deleted/missing provider credentials through _get_all_providers().
  • Resolving active, dangling, and absent credentials in _load_provider_model_cache_entries().

The full provider manager test file passes locally (57 passed).

Would these additional tests be useful for your PR? I'd be happy to contribute them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rewrite property to accept session: Session as parameter

3 participants