diff --git a/apps/backend/internal/agent/runtime/lifecycle/session_test.go b/apps/backend/internal/agent/runtime/lifecycle/session_test.go index 94da42e77d..a19f57ec50 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/session_test.go +++ b/apps/backend/internal/agent/runtime/lifecycle/session_test.go @@ -704,6 +704,7 @@ func TestInitializeAndPrompt_AppliesProfileConfigOptions(t *testing.T) { } execution.SetModelState(&CachedModelState{ CurrentModelID: "default-model", + Models: []streams.SessionModelInfo{{ModelID: "sonnet"}}, ConfigOptions: []streams.ConfigOption{ { ID: "model", Category: "model", CurrentValue: "default-model", diff --git a/apps/backend/internal/orchestrator/event_handlers_workflow.go b/apps/backend/internal/orchestrator/event_handlers_workflow.go index 82a73efd45..af77dc46b8 100644 --- a/apps/backend/internal/orchestrator/event_handlers_workflow.go +++ b/apps/backend/internal/orchestrator/event_handlers_workflow.go @@ -2854,9 +2854,10 @@ func (s *Service) exactModelWorkflowStartPolicy( // workflowEntryRequiresFreshExactModelSession prevents a workflow lane from // resuming a parked session whose persisted provider model disagrees with the -// same exact profile's configured model. Explicit session overrides remain -// valid within a lane; this boundary applies only while entering a different -// workflow step and only to profiles that did not authorize a fallback. +// profile's configured model policy. Explicit session overrides remain valid +// within a lane; this boundary applies only while entering a different workflow +// step. Auto-fallback profiles intentionally allow provider-default reuse, but +// explicit fallback profiles may reuse only the configured model or fallback. func (s *Service) workflowEntryRequiresFreshExactModelSession( ctx context.Context, session *models.TaskSession, @@ -2873,13 +2874,16 @@ func (s *Service) workflowEntryRequiresFreshExactModelSession( if profile == nil { return false, fmt.Errorf("resolve exact workflow profile %q: profile is unavailable", profileID) } - if profile.Model == "" || profile.AutoFallback || profile.FallbackModel != "" { + if profile.Model == "" || profile.AutoFallback { return false, nil } effective, ok := models.LoadEffectiveSessionRuntimeConfig(session) if !ok || effective.Model == "" || effective.Model == profile.Model { return false, nil } + if profile.FallbackModel != "" && effective.Model == profile.FallbackModel { + return false, nil + } s.logger.Info("creating fresh workflow session for exact model identity", zap.String("session_id", session.ID), zap.String("profile_id", profileID), diff --git a/apps/backend/internal/orchestrator/event_handlers_workflow_profile_session_policy_test.go b/apps/backend/internal/orchestrator/event_handlers_workflow_profile_session_policy_test.go index f3242e66d0..d80b092113 100644 --- a/apps/backend/internal/orchestrator/event_handlers_workflow_profile_session_policy_test.go +++ b/apps/backend/internal/orchestrator/event_handlers_workflow_profile_session_policy_test.go @@ -786,6 +786,45 @@ func TestPrepareWorkflowStepSession_HumanQAToWorkKeepsAuthorizedFallbackSession( require.Equal(t, fixture.current.ID, workSession.ID) } +func TestPrepareWorkflowStepSession_FallbackProfileDoesNotReuseUnauthorizedRuntime(t *testing.T) { + ctx := context.Background() + fixture := newProfileSwitchFixture(t, models.WorkflowProfileSessionStartPolicyReuse, models.WorkflowProfileSessionEndPolicyPark) + fixture.agentMgr.resolveProfileInfo = &executor.AgentProfileInfo{ + Model: "gpt-5.6-terra", FallbackModel: "gpt-5.6-luna", + } + + parked := &models.TaskSession{ + ID: "session-parked-terra", TaskID: "t1", AgentProfileID: "terra-with-luna-fallback", + ExecutorID: "exec-local", ExecutorProfileID: "ep1", TaskEnvironmentID: "env-1", + State: models.TaskSessionStateWaitingForInput, StartedAt: time.Now().UTC(), UpdatedAt: time.Now().UTC(), + Metadata: map[string]interface{}{ + models.SessionMetaKeyRuntimeConfig: models.SessionRuntimeConfig{Model: "gpt-5.6-sol"}, + }, + } + require.NoError(t, fixture.repo.CreateTaskSession(ctx, parked)) + seedExecutorRunning(t, fixture.repo, parked.ID, parked.TaskID, "execution-parked-terra") + + workStep := &wfmodels.WorkflowStep{ + ID: "step-work", WorkflowID: "wf1", AgentProfileID: "terra-with-luna-fallback", + ProfileSessionStartPolicy: models.WorkflowProfileSessionStartPolicyReuse, + } + qaStep := &wfmodels.WorkflowStep{ + ID: "step-qa", WorkflowID: "wf1", AgentProfileID: "qa-profile", + ProfileSessionEndPolicy: models.WorkflowProfileSessionEndPolicyPark, + } + + workSession, switched, err := fixture.svc.prepareWorkflowStepSession(ctx, "t1", fixture.current, workStep, qaStep) + require.NoError(t, err) + require.True(t, switched) + require.NotNil(t, workSession) + require.NotEqual(t, parked.ID, workSession.ID, "a parked ACP session with an unauthorized runtime model must not be promoted") + require.Equal(t, "terra-with-luna-fallback", workSession.AgentProfileID) + + storedParked, err := fixture.repo.GetTaskSession(ctx, parked.ID) + require.NoError(t, err) + require.False(t, storedParked.IsPrimary, "the stale parked session must remain unpromoted") +} + func TestSwitchSessionForStep_NewOnStartParkOnEndRoundTrip(t *testing.T) { ctx := context.Background() fixture := newProfileSwitchFixture(t, models.WorkflowProfileSessionStartPolicyNew, models.WorkflowProfileSessionEndPolicyPark)