Skip to content

Commit 7ae54e4

Browse files
Merge pull request #1053 from Dhruvkumar-Microsoft/feature/TAS27
fix: resolved the team switching issue it is taking the caching RFP team for the first time
2 parents f4c2aeb + 6e1115f commit 7ae54e4

2 files changed

Lines changed: 43 additions & 6 deletions

File tree

src/backend/api/router.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,19 +341,31 @@ async def process_request(
341341

342342
# Ensure the workflow is valid (rebuild if terminated or stuck from a prior run)
343343
current_workflow = orchestration_config.get_current_orchestration(user_id)
344+
345+
cached_team_id = getattr(current_workflow, "_team_id", None)
346+
team_mismatch = (
347+
current_workflow is not None and cached_team_id != team_id
348+
)
349+
344350
workflow_unusable = (
345351
current_workflow is None
346352
or getattr(current_workflow, "_terminated", False)
347353
or getattr(current_workflow, "_is_running", False)
354+
or team_mismatch
348355
)
349356
if workflow_unusable:
350357
logger.info(
351-
"Workflow unusable for user '%s' (None=%s, terminated=%s, is_running=%s) — rebuilding",
358+
"Workflow unusable for user '%s' (None=%s, terminated=%s, is_running=%s, "
359+
"team_mismatch=%s cached_team=%s selected_team=%s) — rebuilding",
352360
user_id,
353361
current_workflow is None,
354362
getattr(current_workflow, "_terminated", False),
355363
getattr(current_workflow, "_is_running", False),
364+
team_mismatch,
365+
cached_team_id,
366+
team_id,
356367
)
368+
357369
# Force-clear the running flag so get_current_or_new_orchestration
358370
# sees it as terminated and takes the lightweight reset path.
359371
if current_workflow is not None and getattr(current_workflow, "_is_running", False):
@@ -1454,4 +1466,4 @@ async def get_generated_image(blob_name: str):
14541466
return Response(content=data, media_type="image/png")
14551467
except Exception as exc:
14561468
logging.error(f"Error retrieving image '{blob_name}': {exc}")
1457-
raise HTTPException(status_code=404, detail="Image not found")
1469+
raise HTTPException(status_code=404, detail="Image not found")

src/backend/orchestration/orchestration_manager.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,35 @@ async def get_current_or_new_orchestration(
202202
current = orchestration_config.get_current_orchestration(user_id)
203203
workflow_terminated = getattr(current, "_terminated", False)
204204

205-
# Full rebuild: no workflow exists or team explicitly changed
206-
needs_full_rebuild = current is None or team_switched
205+
# Detect a stale cached orchestration: it was built for a different team
206+
# than the one now selected. Without this, /select_team leaves the prior
207+
# team's workflow cached and the next run executes the wrong agents until
208+
# a page refresh rebuilds it. The team_id tag is set on every workflow we
209+
# build/reset below.
210+
current_team_id = getattr(current, "_team_id", None)
211+
team_changed = (
212+
current is not None and current_team_id != team_config.team_id
213+
)
214+
215+
216+
cls.logger.info(
217+
"get_current_or_new_orchestration: user='%s' selected_team='%s' "
218+
"cached_team='%s' team_switched=%s team_changed=%s current_is_none=%s",
219+
user_id, team_config.team_id, current_team_id,
220+
team_switched, team_changed, current is None,
221+
)
222+
223+
224+
# Full rebuild: no workflow exists, team explicitly switched, or the
225+
# cached workflow belongs to a different team than the selected one.
226+
needs_full_rebuild = current is None or team_switched or team_changed
227+
228+
229+
# Lightweight reset: workflow finished but agents are still valid for the
230+
# same team (a team change always routes to full rebuild above so we
231+
# never reuse the previous team's agents here).
232+
207233

208-
# Lightweight reset: workflow finished but agents are still valid
209234
needs_workflow_reset = not needs_full_rebuild and workflow_terminated
210235

211236
if needs_full_rebuild:
@@ -808,4 +833,4 @@ async def _process_event_stream(
808833
if tool_approvals:
809834
result["tool_approvals"] = tool_approvals
810835
return result
811-
return None
836+
return None

0 commit comments

Comments
 (0)