Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor(team): isolate locked turn admission checks
  • Loading branch information
SivanCola committed Nov 3, 2026
commit eca60de893be8b737aa13107dd86a6ce7099c12f
81 changes: 44 additions & 37 deletions desktop/turn_admission.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,43 +150,9 @@ func (a *App) beginRuntimeTurnWithModelChoice(tabID string, reclaim, detached bo
abort()
return nil, nil, err
}
running := ctrl.RuntimeStateSnapshot().Running
if err := checkRuntimeAdmission(ctrl, check); err != nil {
abort()
return nil, nil, err
}
if running {
if waiter, ok := ctrl.(turnFinishingWaiter); ok {
if done, finishing := waiter.TurnFinishingDone(); finishing {
// Re-resolve after waiting so close/switch cannot misroute retry.
abort()
<-done
continue
}
// Fan-out can end between runtime sampling and TurnFinishingDone.
// Re-check before reporting busy so that completed boundary retries
// instead of preserving the original false rejection window.
if !ctrl.RuntimeStateSnapshot().Running {
abort()
continue
}
}
abort()
return nil, nil, control.ErrTurnRunning
}
// A roster commit can win the mutation lock after the optimistic apply
// above. Recheck under the final admission fence, including directory
// bindings, before reserving a foreground or durable inbox turn.
pending, err := a.teamPresetPending(tab, ctrl)
if err != nil {
abort()
return nil, nil, err
}
if pending {
busy := rebuildControllerActiveWorkErrorFor(ctrl, "team preset")
abort()
if busy != nil {
return nil, nil, busy
if retry, err := a.checkRuntimeTurnBoundary(tab, ctrl, check, abort); retry || err != nil {
if err != nil {
return nil, nil, err
}
continue
}
Expand Down Expand Up @@ -235,6 +201,47 @@ func (a *App) beginRuntimeTurnWithModelChoice(tabID string, reclaim, detached bo
}
}

// checkRuntimeTurnBoundary runs with runtime admission and turnStartMu held.
// Success retains both locks; every retry or error releases them through abort.
func (a *App) checkRuntimeTurnBoundary(tab *WorkspaceTab, ctrl control.SessionAPI, check func(control.SessionAPI) error, abort func()) (bool, error) {
running := ctrl.RuntimeStateSnapshot().Running
if err := checkRuntimeAdmission(ctrl, check); err != nil {
abort()
return false, err
}
if running {
if waiter, ok := ctrl.(turnFinishingWaiter); ok {
if done, finishing := waiter.TurnFinishingDone(); finishing {
// Re-resolve after waiting so close/switch cannot misroute retry.
abort()
<-done
return true, nil
}
// Fan-out may finish between the two samples. Retry that boundary
// instead of reporting a stale busy result.
if !ctrl.RuntimeStateSnapshot().Running {
abort()
return true, nil
}
}
abort()
return false, control.ErrTurnRunning
}
// A roster commit can win after the optimistic apply. Recheck configuration
// and directory bindings inside the final foreground/inbox admission fence.
pending, err := a.teamPresetPending(tab, ctrl)
if err != nil {
abort()
return false, err
}
if pending {
busy := rebuildControllerActiveWorkErrorFor(ctrl, "team preset")
abort()
return busy == nil, busy
}
return false, nil
}

// Durable follow-ups are new runs even when a controller dispatches them on
// its own after TurnDone. Resolve the runtime owner again after detach/attach.
func (a *App) beforeInboxDispatch(ctrl *control.Controller) (func(), error) {
Expand Down
Loading