Skip to content

Flow service context propagation and transection usage#1297

Draft
JeethJJ wants to merge 2 commits intoasgardeo:mainfrom
JeethJJ:db-transaction-mgt-flow-service
Draft

Flow service context propagation and transection usage#1297
JeethJJ wants to merge 2 commits intoasgardeo:mainfrom
JeethJJ:db-transaction-mgt-flow-service

Conversation

@JeethJJ
Copy link
Contributor

@JeethJJ JeethJJ commented Feb 5, 2026

Purpose

As we have introduced the transection architecture, we are adapting our services to use transections. In this PR we have updated the flow service to use transactions.

Sub Issue : #1296
Parent issue : #282

Copilot AI review requested due to automatic review settings February 5, 2026 04:45
@JeethJJ JeethJJ force-pushed the db-transaction-mgt-flow-service branch from ccb2b1c to c353fa1 Compare February 5, 2026 04:45
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds context propagation and transaction support to the flow service as part of implementing the transaction architecture described in issue #282. The changes ensure that database operations can participate in transactions and that context is properly propagated through all layers.

Changes:

  • Added context.Context parameter to all flow service interface methods and implementations
  • Updated store layer to use QueryContext and ExecuteContext instead of Query and Execute for proper context propagation
  • Modified handler layer to extract context from HTTP requests and pass it through the call chain
  • Updated all tests and mocks to include context parameter with mock.Anything matchers
  • Integrated context propagation in dependent services (flowexec, application, export, mcp tools)

Reviewed changes

Copilot reviewed 25 out of 25 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
backend/internal/flow/mgt/service.go Added context parameter to all service methods
backend/internal/flow/mgt/store.go Added context parameter to store interface and implementations, updated to use context-aware DB methods
backend/internal/flow/mgt/handler.go Extracts context from HTTP request and propagates to service layer
backend/internal/flow/mgt/graph_builder.go Added context parameter to graph building methods
backend/internal/flow/mgt/*_test.go Updated all tests with context.Background() and mock.Anything for context parameters
backend/internal/flow/mgt/*_mock_test.go Regenerated mocks with context parameter support
backend/internal/flow/mgt/file_based_store.go Added context parameter to file-based store operations
backend/internal/flow/mgt/cache_backed_store.go Added context parameter to cache-backed store wrapper
backend/internal/flow/mgt/declarative_resource.go Added context to declarative resource exporter
backend/internal/flow/flowexec/service.go Updated flow execution service to propagate context
backend/internal/application/service.go Updated application service to pass context to flow service calls
backend/internal/system/export/service_test.go Updated export service tests with context parameter
backend/internal/mcp/tools/flow/tool.go Updated MCP flow tools to propagate context

defaultAuthFlowHandle := config.GetThunderRuntime().Config.Flow.DefaultAuthFlowHandle
defaultAuthFlow, svcErr := as.flowMgtService.GetFlowByHandle(
defaultAuthFlowHandle, flowcommon.FlowTypeAuthentication)
context.TODO(), defaultAuthFlowHandle, flowcommon.FlowTypeAuthentication)
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using context.TODO() is not recommended. The getDefaultAuthFlowID method is called from validateAuthFlowID which is part of the validation flow. The context should be propagated from the original HTTP request through the entire call chain.

Suggested change
context.TODO(), defaultAuthFlowHandle, flowcommon.FlowTypeAuthentication)
context.Background(), defaultAuthFlowHandle, flowcommon.FlowTypeAuthentication)

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its intentional as context propagation is being addressed service by service. In this PR we are focusing on the flow service.

Comment on lines 65 to 78
@@ -74,7 +75,7 @@ func (e *FlowGraphExporter) GetAllResourceIDs() ([]string, *serviceerror.Service

// GetResourceByID retrieves a flow graph by its ID.
func (e *FlowGraphExporter) GetResourceByID(id string) (interface{}, string, *serviceerror.ServiceError) {
flow, err := e.service.GetFlow(id)
flow, err := e.service.GetFlow(context.Background(), id)
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using context.Background() for GetAllResourceIDs and GetResourceByID methods in the declarative resource exporter. Since these methods are typically called during export operations that originate from HTTP requests or system operations, consider propagating the context from the caller rather than creating a new background context.

Copilot uses AI. Check for mistakes.
// Initialize the engine context
// This uses verbose true to ensure step layouts are returned during execution
ctx, err := s.initContext(initContext.ApplicationID, flowType, true, logger)
flowCtx, err := s.initContext(context.Background(), initContext.ApplicationID, flowType, true, logger)
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using context.Background() in the InitiateFlow method. While this might be acceptable if InitiateFlow is designed to start a long-running operation, if this is called from an HTTP handler or has a parent context, that context should be passed as a parameter instead of creating a new background context.

Copilot uses AI. Check for mistakes.
return errors.New("invalid flow data type")
}
_, err := f.CreateFlow(flow.ID, &FlowDefinition{
_, err := f.CreateFlow(context.Background(), flow.ID, &FlowDefinition{
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using context.Background() in the Create method of file-based store. While file-based stores might be used in contexts where there's no request context available, if this is called from areas that do have context (like during declarative config loading), the context should be passed through.

Copilot uses AI. Check for mistakes.
)

// flowStoreInterface defines the interface for flow store operations.
// flowStoreInterface defines the interface for flow store operations.
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate comment: "flowStoreInterface defines the interface for flow store operations." appears twice on consecutive lines. Remove one of the duplicate lines.

Suggested change
// flowStoreInterface defines the interface for flow store operations.

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check this

func (as *applicationService) validateAuthFlowID(app *model.ApplicationDTO) *serviceerror.ServiceError {
if app.AuthFlowID != "" {
isValidFlow := as.flowMgtService.IsValidFlow(app.AuthFlowID)
isValidFlow := as.flowMgtService.IsValidFlow(context.TODO(), app.AuthFlowID)
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using context.TODO() is not recommended for production code. Since this function (validateAuthFlowID) is called from within request handlers that have access to request context, the context should be passed as a parameter through the call chain instead of using context.TODO(). This ensures proper request tracing, cancellation, and timeout propagation.

Copilot uses AI. Check for mistakes.
Comment on lines 661 to 678
@@ -674,7 +675,7 @@ func (as *applicationService) validateRegistrationFlowID(app *model.ApplicationD
}

registrationFlow, svcErr := as.flowMgtService.GetFlowByHandle(
authFlow.Handle, flowcommon.FlowTypeRegistration)
context.TODO(), authFlow.Handle, flowcommon.FlowTypeRegistration)
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using context.TODO() is not recommended. Since these validations are part of the application creation/update flow that originates from HTTP handlers, the context should be passed down through the call chain from the handler. Consider updating the validateRegistrationFlowID method signature to accept a context parameter.

Copilot uses AI. Check for mistakes.
@codecov
Copy link

codecov bot commented Feb 5, 2026

Codecov Report

❌ Patch coverage is 83.12102% with 53 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.97%. Comparing base (ccf48d2) to head (18c64ee).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
backend/internal/flow/mgt/store.go 66.36% 18 Missing and 19 partials ⚠️
backend/internal/flow/mgt/service.go 88.59% 10 Missing and 3 partials ⚠️
backend/internal/flow/mgt/init.go 50.00% 1 Missing and 1 partial ⚠️
backend/internal/flow/flowexec/service.go 96.29% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1297      +/-   ##
==========================================
+ Coverage   89.96%   89.97%   +0.01%     
==========================================
  Files         629      629              
  Lines       41189    41198       +9     
  Branches     2390     2390              
==========================================
+ Hits        37056    37069      +13     
- Misses       2234     2240       +6     
+ Partials     1899     1889      -10     
Flag Coverage Δ
backend-integration-postgres ?
backend-integration-sqlite 53.16% <64.33%> (+0.05%) ⬆️
backend-unit 82.58% <69.74%> (+0.12%) ⬆️
frontend-apps-develop-unit 90.75% <ø> (ø)
frontend-apps-gate-unit 84.62% <ø> (ø)

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@JeethJJ JeethJJ force-pushed the db-transaction-mgt-flow-service branch from c353fa1 to 4894327 Compare February 5, 2026 05:21
Copilot AI review requested due to automatic review settings February 5, 2026 09:35
@JeethJJ JeethJJ force-pushed the db-transaction-mgt-flow-service branch from 4894327 to d1ddbdb Compare February 5, 2026 09:35
@coderabbitai
Copy link

coderabbitai bot commented Feb 5, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 24 out of 24 changed files in this pull request and generated 2 comments.

}

// buildGraph converts a CompleteFlowDefinition to a core.GraphInterface for execution.
// buildGraph s a CompleteFlowDefinition to a core.GraphInterface for execution.
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment text appears to be incomplete. It reads "buildGraph s a CompleteFlowDefinition" but seems to be missing a verb. It should likely read "buildGraph converts a CompleteFlowDefinition to a core.GraphInterface for execution."

Suggested change
// buildGraph s a CompleteFlowDefinition to a core.GraphInterface for execution.
// buildGraph converts a CompleteFlowDefinition to a core.GraphInterface for execution.

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this intentional?

defaultAuthFlowHandle := config.GetThunderRuntime().Config.Flow.DefaultAuthFlowHandle
defaultAuthFlow, svcErr := as.flowMgtService.GetFlowByHandle(
defaultAuthFlowHandle, flowcommon.FlowTypeAuthentication)
context.TODO(), defaultAuthFlowHandle, flowcommon.FlowTypeAuthentication)
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The usage of context.TODO() is not recommended here. Since this is being called from application service methods which don't currently have a context parameter, consider using context.Background() instead, or better yet, propagate context through the application service methods. context.TODO() is typically used as a placeholder during development when context propagation is not yet implemented, but this PR is specifically about adding context propagation.

Suggested change
context.TODO(), defaultAuthFlowHandle, flowcommon.FlowTypeAuthentication)
context.Background(), defaultAuthFlowHandle, flowcommon.FlowTypeAuthentication)

Copilot uses AI. Check for mistakes.
Copy link
Contributor

@rajithacharith rajithacharith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you test this improvement with declarative_resources mode on and try updating or creating a flow?

@@ -61,7 +62,7 @@ func (e *FlowGraphExporter) GetParameterizerType() string {

// GetAllResourceIDs retrieves all flow graph IDs.
func (e *FlowGraphExporter) GetAllResourceIDs() ([]string, *serviceerror.ServiceError) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These functions are used when exporting the resources with /export endpoint.
That context can be used. Keep it tracked. That will be another refactoring at the last.

}

// buildGraph converts a CompleteFlowDefinition to a core.GraphInterface for execution.
// buildGraph s a CompleteFlowDefinition to a core.GraphInterface for execution.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this intentional?

)

// flowStoreInterface defines the interface for flow store operations.
// flowStoreInterface defines the interface for flow store operations.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we have any scenarios we need transactions in flow mgt service?

@JeethJJ JeethJJ force-pushed the db-transaction-mgt-flow-service branch 2 times, most recently from f70227c to 0a3681f Compare February 9, 2026 06:33
Copilot AI review requested due to automatic review settings February 9, 2026 07:03
@JeethJJ JeethJJ force-pushed the db-transaction-mgt-flow-service branch from 0a3681f to 33df78c Compare February 9, 2026 07:03
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 24 out of 24 changed files in this pull request and generated 1 comment.

Comment on lines +174 to +177
s.logger.Debug("Flow created successfully", log.String(logKeyFlowID, flowID))

s.tryInferRegistrationFlow(ctx, flowID, flowDef)
return nil
Copy link

Copilot AI Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tryInferRegistrationFlow is invoked inside the surrounding transactioner.Transact closure, but that helper intentionally swallows store errors. Because the inferred flow creation reuses the same transaction context (nested transactions are detected via context), a failure partway through store.CreateFlow can leave partial writes (e.g., FLOW row inserted but FLOW_VERSION insert fails) and still commit when the outer closure returns nil. Consider moving the inference call outside the transaction (after commit), or make inference run in its own independent transaction context, or propagate inference failures so the outer transaction rolls back.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants