feat: lfx owns the v2 workflow router via a host-DI seam
Follow-up to #13783. This is the answer to Debojit's question on that PR: have lfx own the v2 workflow router so we only work on these APIs across the LF <> LFX seam, instead of keeping two copies of the handler.
Stacked on feat/lfx-serve-v2, so review/merge that one first.
What this does
lfx exports a WorkflowHost Protocol plus a create_workflow_router(host) factory. The router owns the env-neutral handler body (request parsing, stream-protocol validation, run dispatch, error -> HTTP mapping, the developer-api guard). The host supplies only the DB/tenant-bound pieces: resolve the caller, fetch-and-authorize a flow, the request session, and whether background runs exist.
Both runtimes mount the same router:
- bare
lfx servepasses a no-dbServeWorkflowHost(supports_background=False), which collapses the duplicated serve handler into the shared router. Its surface stays exactlyPOST /workflows. - langflow passes a
LangflowWorkflowHost(auth ->UserReadin its own session,get_flow_by_id_or_endpoint_name,ensure_flow_permission, readonly session, background submit).UserRead/FlowAction/JobServicestay confined to the langflow side.
What I had to change from the original idea
The plan assumed one shared SSE loop. It turns out langflow's streaming is genuinely richer than lfx's: it drives the v1 build-vertex loop with the agui side-channel, vertex-build persistence, and the job timeout. So I made run_sync/stream_response pluggable on the host instead of forcing langflow onto lfx's leaner loop. langflow keeps its own execution path, bare serve keeps lfx's defaults. The shared part is the routing skeleton, the auth/flow/authz wiring, the error mapping, and the request/response + SSE contract.
The durable routes (GET status, POST stop, GET events) stay LF-rich on an LF-owned background router, so there's one handler per method+path. The public workflow router is untouched here (separate follow-up).
Tests
- New no-mock cross-host contract test in lfx pins the request/response and SSE shape so the two hosts can't drift. lfx suite: 16 passed in an isolated lfx env (no langflow).
- The full v2 workflow backend suite (run, reconstruction, agui, public) stays green: 124 passed. Routes are byte-identical (FLOW_NOT_FOUND 404, stream-protocol 422, agui + reconstruction), no double-registration, bare serve still exactly
POST /workflows.
[!IMPORTANT]
Review skipped
Auto incremental reviews are disabled on this repository.
Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.βοΈ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID:
85e67bb4-b6d7-4748-b360-bab7df40808cYou can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
- [ ] π Trigger review
Walkthrough
This PR replaces separate v2 workflow endpoint implementations with a shared WorkflowHost protocol and create_workflow_router in lfx.workflow. Langflow backend and lfx serve CLI now implement host adapters (LangflowWorkflowHost, ServeWorkflowHost) delegating flow resolution, authorization, and sync/stream/background execution, with restructured error mapping and updated tests.
Changes
Shared Workflow Router
| Layer / File(s) | Summary |
|---|---|
Host contract and action enum src/lfx/src/lfx/workflow/actions.py, src/lfx/src/lfx/workflow/host.py |
Defines WorkflowAction enum, ResolvedFlow model, WorkflowHost Protocol, and WorkflowHostBase with default sync/stream/background behaviors. |
Shared router implementation src/lfx/src/lfx/workflow/router.py |
Implements create_workflow_router, bounded SSE event queue, developer API guard, field validation, sync/stream execution helpers, and background job route registration. |
Backend flow resolution and error mapping src/backend/base/langflow/api/v2/workflow.py |
Adds resolve_flow_for_execution, authorize_flow_action, execution gating, and normalized 404/503/500 error mapping for sync/stream/background paths. |
LangflowWorkflowHost and router wiring src/backend/base/langflow/api/v2/workflow_host.py, src/backend/base/langflow/api/v2/__init__.py, src/backend/base/langflow/api/router.py |
Implements LangflowWorkflowHost delegating to mapping helpers, updates v2 exports, and mounts the shared router plus durable background routes. |
lfx serve CLI host wiring src/lfx/src/lfx/cli/serve_app.py, src/lfx/src/lfx/cli/serve_workflow.py |
Replaces standalone endpoint logic with ServeWorkflowHost and mounts the shared router under /api/v2. |
Backend and lfx tests src/backend/tests/unit/api/v2/test_workflow_agui.py, src/lfx/tests/unit/workflow/test_router_contract.py, src/lfx/tests/unit/workflow/test_router_sync_isolation.py, src/lfx/tests/unit/cli/test_serve_identity.py, src/lfx/tests/unit/cli/test_serve_workflow.py |
Updates tests to exercise new helper functions directly and to target /api/v2/workflows routes and lfx.workflow.router patch points; adds new router contract and isolation tests. |
Secrets baseline .secrets.baseline |
Updates recorded line numbers and scan timestamp. |
Estimated code review effort: 4 (Complex) | ~75 minutes
Possibly related PRs
- langflow-ai/langflow#13307: Part of the same native v2 workflows endpoint/adapter-driven streaming and background workflow execution work.
- langflow-ai/langflow#13782: Builds on the same lfx.workflow adapters/converters plumbing extracted for the v2 workflow contract layer.
Suggested labels: refactor
Suggested reviewers: erichare, dkaushik94
π₯ Pre-merge checks | β 5 | β 4
β Failed checks (4 inconclusive)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Test Coverage For New Implementations | β Inconclusive | Repository clone failed, so this custom check could not run with code access. | Retry the review run. If this persists, inspect pre-merge custom-check logs for infrastructure or agent runtime failures. |
| Test Quality And Coverage | β Inconclusive | Repository clone failed, so this custom check could not run with code access. | Retry the review run. If this persists, inspect pre-merge custom-check logs for infrastructure or agent runtime failures. |
| Test File Naming And Structure | β Inconclusive | Repository clone failed, so this custom check could not run with code access. | Retry the review run. If this persists, inspect pre-merge custom-check logs for infrastructure or agent runtime failures. |
| Excessive Mock Usage Warning | β Inconclusive | Repository clone failed, so this custom check could not run with code access. | Retry the review run. If this persists, inspect pre-merge custom-check logs for infrastructure or agent runtime failures. |
β Passed checks (5 passed)
| Check name | Status | Explanation |
|---|---|---|
| Docstring Coverage | β Passed | No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. |
| Linked Issues check | β Passed | Check skipped because no linked issues were found for this pull request. |
| Out of Scope Changes check | β Passed | Check skipped because no linked issues were found for this pull request. |
| Description Check | β Passed | Check skipped - CodeRabbitβs high-level summary is enabled. |
| Title check | β Passed | The title is concise and accurately captures the main change: lfx now owns the v2 workflow router through a host-based DI seam. |
β¨ Finishing Touches
π§ͺ Generate unit tests (beta)
- [ ] Create PR with unit tests
- [ ] Commit unit tests in branch
feat/lfx-workflow-router-seam
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.
Comment @coderabbitai help to get the list of available commands.
β Test Coverage Advisor
No source changes detected without accompanying tests. Thanks for keeping coverage up! π
Advisory check only β never blocks merge.
Codecov Report
:x: Patch coverage is 80.71625% with 70 lines in your changes missing coverage. Please review.
:white_check_mark: Project coverage is 59.62%. Comparing base (55debb8) to head (2c897e8).
:warning: Report is 2 commits behind head on release-1.11.0.
Additional details and impacted files
@@ Coverage Diff @@
## release-1.11.0 #13816 +/- ##
==================================================
- Coverage 59.72% 59.62% -0.10%
==================================================
Files 2367 2371 +4
Lines 226686 226865 +179
Branches 34003 32032 -1971
==================================================
- Hits 135377 135279 -98
- Misses 89715 89979 +264
- Partials 1594 1607 +13
| Flag | Coverage Ξ | |
|---|---|---|
| backend | 65.46% <80.80%> (-0.66%) |
:arrow_down: |
| frontend | 58.68% <ΓΈ> (+0.01%) |
:arrow_up: |
| lfx | 57.38% <80.68%> (+0.06%) |
:arrow_up: |
Flags with carried forward coverage won't be shown. Click here to find out more.
| Files with missing lines | Coverage Ξ | |
|---|---|---|
| src/backend/base/langflow/api/router.py | 100.00% <100.00%> (ΓΈ) |
|
| src/backend/base/langflow/api/v2/workflow_host.py | 100.00% <100.00%> (ΓΈ) |
|
| src/lfx/src/lfx/cli/serve_app.py | 96.40% <100.00%> (+<0.01%) |
:arrow_up: |
| src/lfx/src/lfx/cli/serve_workflow.py | 100.00% <100.00%> (+19.51%) |
:arrow_up: |
| src/lfx/src/lfx/workflow/actions.py | 100.00% <100.00%> (ΓΈ) |
|
| src/lfx/src/lfx/workflow/host.py | 76.00% <76.00%> (ΓΈ) |
|
| src/backend/base/langflow/api/v2/workflow.py | 72.36% <68.85%> (-12.74%) |
:arrow_down: |
| src/lfx/src/lfx/workflow/router.py | 78.68% <78.68%> (ΓΈ) |
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
Frontend Unit Test Coverage Report
Coverage Summary
| Lines | Statements | Branches | Functions |
|---|---|---|---|
| 44.79% (60951/136059) | 69.59% (8343/11988) | 43.19% (1381/3197) |
Unit Test Results
| Tests | Skipped | Failures | Errors | Time |
|---|---|---|---|---|
| 5153 | 0 :zzz: | 0 :x: | 0 :fire: | 16m 39s :stopwatch: |
The changes look right, but I'd say move the endpoint to the same naming convention. I take it that we call it directly /workflows since that is the LFX convention for exposing an endpoint?
@ogabrielluiz
I think this comment might be addressed down the line since we might need to move things around. Considering LFX needs to be bare-bones ready, langflow imports need to be within langflow, and LFX needs to be encapsulated as much as possible within itself. That being said, the change here regresses error handling on the ensure_flow_permission error handler.
src/backend/base/langflow/api/v2/workflow.py β authorize_flow_action (the except HTTPException block)
Pre-seam, the flow fetch and ensure_flow_permission ran inside one try that mapped OperationalError β 503 DATABASE_ERROR and Exception β structured 500. In the seam, that try was split: resolve_flow_for_execution keeps the full mapping for the fetch, but authorize_flow_action catches only HTTPException.
ensure_flow_permission can raise a non-HTTPException: the enforce call itself is wrapped (fails closed to 403), but the surrounding await _audit.audit_decision(...) writes and the owner-override lookup (should_apply_owner_override()) touch the DB, and audit rows flow even in OSS. If one of those hits OperationalError (SQLite "database is locked" under the inline-run contention this code elsewhere explicitly designs around), it now escapes authorize_flow_action unmapped β FastAPI generic 500 with no structured body, where pre-seam it was 503 DATABASE_ERROR with flow_id.
Failure scenario: authenticated POST /api/v2/workflows, caller authorized-but-audited, transient DB lock during the RBAC audit write β client gets a bare 500 instead of the retryable 503 contract. Low frequency, but a real status + body regression on the auth path. Fix: give authorize_flow_action (or a wrapper around host.authorize in the router) the same OperationalError β 503 / Exception β structured 500 arms resolve_flow_for_execution has.
Rest of it looks good. Lemme know thoughts, and if we want to kick this stone down the road a couple of weeks. I'll LG the PR.
@dkaushik94's comments looked good, agreed on the general point that if any failure behaviors changed we should add some tests and sign off on each. Otherwise lgtm
Hey @dkaushik94, all three are addressed now:
- Sync no_env_fallback leak: run_workflow_sync wasn't activating the isolation contextvar, so a sync run under LFX_SERVE_NO_ENV_FALLBACK still read os.environ while stream was isolated. It now activates and resets around the run like the stream path, with a no-mock real-graph test that the flag is live mid-run and reset after.
- authorize_flow_action bare 500: it only caught HTTPException, so an OperationalError from the audit write leaked a 500. Added the same OperationalError to 503 / Exception to 500 arms the fetch path already has, with a test that a DB lock during authorize returns the 503 contract.
- Path parity: serve now mounts the shared router at /api/v2/workflows, so it's the same URL as the backend and you switch runtimes by changing the host, not the path. serve's other routes stay root-level.
@jordanrfrazier both failure-behavior changes have tests now, per your point. Commits f593334 and 284369f.