@@ -25,41 +25,32 @@ import {
2525 type OnboardingTransitionDirection ,
2626 OnboardingSlideTransition ,
2727} from "./OnboardingSlideTransition" ;
28- import { ONBOARDING_RUNTIME_ORDER } from "./onboardingRuntimeSelection" ;
28+ import {
29+ getReadyOnboardingRuntimes ,
30+ getVisibleOnboardingRuntimes ,
31+ } from "./onboardingRuntimeSelection" ;
2932import type { DefaultConfigStepActions } from "./types" ;
3033
3134type DefaultConfigStepProps = {
3235 actions : DefaultConfigStepActions ;
3336 direction : OnboardingTransitionDirection ;
34- selectedRuntimeIds : readonly string [ ] ;
37+ readyRuntimeIds : readonly string [ ] ;
3538} ;
3639
3740function formatHarnessLabel ( runtime : AcpRuntimeCatalogEntry | undefined ) {
3841 if ( ! runtime ) return "Select a harness" ;
3942 return runtime . id === "buzz-agent" ? "Buzz" : runtime . label ;
4043}
4144
42- function sortSelectedRuntimes (
43- runtimes : readonly AcpRuntimeCatalogEntry [ ] ,
44- selectedRuntimeIds : readonly string [ ] ,
45- ) {
46- const selectedRuntimeIdSet = new Set ( selectedRuntimeIds ) ;
47- return runtimes
48- . filter ( ( runtime ) => selectedRuntimeIdSet . has ( runtime . id ) )
49- . sort ( ( left , right ) => {
50- const leftIndex = ONBOARDING_RUNTIME_ORDER . indexOf ( left . id ) ;
51- const rightIndex = ONBOARDING_RUNTIME_ORDER . indexOf ( right . id ) ;
52- return (
53- ( leftIndex === - 1 ? ONBOARDING_RUNTIME_ORDER . length : leftIndex ) -
54- ( rightIndex === - 1 ? ONBOARDING_RUNTIME_ORDER . length : rightIndex )
55- ) ;
56- } ) ;
57- }
58-
5945function AgentDefaultsSection ( {
60- selectedRuntimeIds,
46+ onPersistenceStateChange,
47+ readyRuntimeIds,
6148} : {
62- selectedRuntimeIds : readonly string [ ] ;
49+ onPersistenceStateChange : ( state : {
50+ canComplete : boolean ;
51+ flush : ( ) => Promise < void > ;
52+ } ) => void ;
53+ readyRuntimeIds : readonly string [ ] ;
6354} ) {
6455 const runtimesQuery = useAcpRuntimesQuery ( ) ;
6556 const [ config , setConfig ] =
@@ -70,8 +61,10 @@ function AgentDefaultsSection({
7061 const [ bakedEnv , setBakedEnv ] = React . useState < BakedEnvEntry [ ] > ( [ ] ) ;
7162 const coalescerRef = React . useRef < {
7263 enqueue : ( value : GlobalAgentConfig ) => void ;
64+ flush : ( ) => Promise < void > ;
7365 cancel : ( ) => void ;
7466 } | null > ( null ) ;
67+ const [ isSaving , setIsSaving ] = React . useState ( false ) ;
7568
7669 React . useEffect ( ( ) => {
7770 let unmounted = false ;
@@ -102,7 +95,9 @@ function AgentDefaultsSection({
10295 // set_global_agent_config returns a save result (config + restart
10396 // counts); the coalescer round-trips the persisted config only.
10497 async ( next ) => ( await setGlobalAgentConfig ( next ) ) . config ,
105- ( ) => undefined , // saving state not surfaced in this autosave UX
98+ ( saving ) => {
99+ if ( ! unmounted ) setIsSaving ( saving ) ;
100+ } ,
106101 ( saved ) => {
107102 if ( ! unmounted ) setConfig ( saved ) ;
108103 } ,
@@ -115,31 +110,48 @@ function AgentDefaultsSection({
115110 } ;
116111 } , [ ] ) ;
117112
118- const selectedRuntimes = React . useMemo (
119- ( ) => sortSelectedRuntimes ( runtimesQuery . data ?? [ ] , selectedRuntimeIds ) ,
120- [ runtimesQuery . data , selectedRuntimeIds ] ,
113+ const effectiveReadyRuntimeIds = React . useMemo (
114+ ( ) =>
115+ readyRuntimeIds . length > 0
116+ ? readyRuntimeIds
117+ : getReadyOnboardingRuntimes ( runtimesQuery . data ?? [ ] ) . map (
118+ ( runtime ) => runtime . id ,
119+ ) ,
120+ [ readyRuntimeIds , runtimesQuery . data ] ,
121121 ) ;
122- const selectedRuntime = React . useMemo ( ( ) => {
123- const preferredRuntime = selectedRuntimes . find (
124- ( runtime ) => runtime . id === config . preferred_runtime ,
125- ) ;
126- return preferredRuntime ?? selectedRuntimes [ 0 ] ;
127- } , [ config . preferred_runtime , selectedRuntimes ] ) ;
128- const selectedRuntimeId =
129- selectedRuntime ?. id ?? config . preferred_runtime ?? "" ;
122+ const readyRuntimeIdSet = React . useMemo (
123+ ( ) => new Set ( effectiveReadyRuntimeIds ) ,
124+ [ effectiveReadyRuntimeIds ] ,
125+ ) ;
126+ // Setup already confirmed readiness. Re-filter only for onboarding
127+ // visibility here; a transient auth recheck must not invalidate that handoff.
128+ const readyRuntimes = React . useMemo (
129+ ( ) =>
130+ getVisibleOnboardingRuntimes ( runtimesQuery . data ?? [ ] ) . filter ( ( runtime ) =>
131+ readyRuntimeIdSet . has ( runtime . id ) ,
132+ ) ,
133+ [ readyRuntimeIdSet , runtimesQuery . data ] ,
134+ ) ;
135+ const selectedRuntime = React . useMemo (
136+ ( ) =>
137+ readyRuntimes . find ( ( runtime ) => runtime . id === config . preferred_runtime ) ,
138+ [ config . preferred_runtime , readyRuntimes ] ,
139+ ) ;
140+ const selectedRuntimeId = selectedRuntime ?. id ?? "" ;
130141 const configSurfaceLoading = isLoading || runtimesQuery . isLoading ;
142+
131143 const configSurfaceError =
132144 runtimesQuery . isError ||
133145 ( ! configSurfaceLoading &&
134- selectedRuntimeIds . length > 0 &&
135- ! selectedRuntime ) ;
146+ effectiveReadyRuntimeIds . length > 0 &&
147+ readyRuntimes . length === 0 ) ;
136148 const harnessOptions = React . useMemo (
137149 ( ) =>
138- selectedRuntimes . map ( ( runtime ) => ( {
150+ readyRuntimes . map ( ( runtime ) => ( {
139151 label : formatHarnessLabel ( runtime ) ,
140152 value : runtime . id ,
141153 } ) ) ,
142- [ selectedRuntimes ] ,
154+ [ readyRuntimes ] ,
143155 ) ;
144156
145157 const handleHarnessChange = React . useCallback (
@@ -154,21 +166,27 @@ function AgentDefaultsSection({
154166 ) ;
155167
156168 React . useEffect ( ( ) => {
157- if ( isLoading || ! selectedRuntimeId ) return ;
158- if ( config . preferred_runtime === selectedRuntimeId ) return ;
159-
160- // The user can go Back, change which harnesses are selected, then return to
161- // this page without using this page's own harness dropdown. Reconcile that
162- // effective harness change through the same reset path so a Codex model
163- // never survives into Claude Code as a custom model (or vice versa).
164- handleHarnessChange ( selectedRuntimeId ) ;
169+ if ( configSurfaceLoading || selectedRuntimeId ) return ;
170+ if ( readyRuntimes . length !== 1 ) return ;
171+ handleHarnessChange ( readyRuntimes [ 0 ] . id ) ;
165172 } , [
166- config . preferred_runtime ,
173+ configSurfaceLoading ,
167174 handleHarnessChange ,
168- isLoading ,
175+ readyRuntimes ,
169176 selectedRuntimeId ,
170177 ] ) ;
171178
179+ const flushPersistence = React . useCallback (
180+ ( ) => coalescerRef . current ?. flush ( ) ?? Promise . resolve ( ) ,
181+ [ ] ,
182+ ) ;
183+ React . useEffect ( ( ) => {
184+ onPersistenceStateChange ( {
185+ canComplete : selectedRuntimeId . length > 0 && ! isSaving ,
186+ flush : flushPersistence ,
187+ } ) ;
188+ } , [ flushPersistence , isSaving , onPersistenceStateChange , selectedRuntimeId ] ) ;
189+
172190 return (
173191 < section className = "w-full space-y-4 text-left text-sm" >
174192 { configSurfaceLoading ? (
@@ -236,8 +254,29 @@ function AgentDefaultsSection({
236254export function DefaultConfigStep ( {
237255 actions,
238256 direction,
239- selectedRuntimeIds ,
257+ readyRuntimeIds ,
240258} : DefaultConfigStepProps ) {
259+ const [ persistenceState , setPersistenceState ] = React . useState < {
260+ canComplete : boolean ;
261+ flush : ( ) => Promise < void > ;
262+ } > ( { canComplete : false , flush : ( ) => Promise . resolve ( ) } ) ;
263+ const [ completionError , setCompletionError ] = React . useState < string | null > (
264+ null ,
265+ ) ;
266+ const [ isCompleting , setIsCompleting ] = React . useState ( false ) ;
267+
268+ const handleComplete = React . useCallback ( async ( ) => {
269+ setIsCompleting ( true ) ;
270+ setCompletionError ( null ) ;
271+ try {
272+ await persistenceState . flush ( ) ;
273+ actions . complete ( ) ;
274+ } catch {
275+ setCompletionError ( "Couldn't save your default harness. Try again." ) ;
276+ setIsCompleting ( false ) ;
277+ }
278+ } , [ actions , persistenceState ] ) ;
279+
241280 return (
242281 < OnboardingSlideTransition
243282 className = "flex min-h-full w-full flex-col items-center"
@@ -258,15 +297,27 @@ export function DefaultConfigStep({
258297
259298 < div className = "flex w-full flex-1 items-center justify-center py-10" >
260299 < div className = "w-full max-w-[328px]" >
261- < AgentDefaultsSection selectedRuntimeIds = { selectedRuntimeIds } />
300+ < AgentDefaultsSection
301+ onPersistenceStateChange = { setPersistenceState }
302+ readyRuntimeIds = { readyRuntimeIds }
303+ />
304+ { completionError ? (
305+ < p
306+ className = "mt-3 text-center text-xs text-destructive"
307+ role = "alert"
308+ >
309+ { completionError }
310+ </ p >
311+ ) : null }
262312 </ div >
263313 </ div >
264314
265315 < OnboardingFooter >
266316 < Button
267317 className = { `${ ONBOARDING_PRIMARY_CTA_CLASS } text-sm` }
268318 data-testid = "onboarding-finish"
269- onClick = { actions . complete }
319+ disabled = { ! persistenceState . canComplete || isCompleting }
320+ onClick = { ( ) => void handleComplete ( ) }
270321 type = "button"
271322 >
272323 Next
0 commit comments