@@ -6,14 +6,10 @@ import type {
66 ManagedAgent ,
77 UpdatePersonaInput ,
88} from "@/shared/api/types" ;
9- import { Switch } from "@/shared/ui/switch" ;
109import type { BackendIntent } from "../lib/instanceInputForDefinition" ;
11- import {
12- definitionCreateDialogState ,
13- intentForStartToggle ,
14- type AgentCreateIntent ,
15- } from "./agentCreateIntent" ;
10+ import type { AgentCreateIntent } from "./agentCreateIntent" ;
1611import type { EditAgentFocusTarget } from "@/features/agents/openEditAgentEvent" ;
12+ import { useMeshAvailability } from "@/features/mesh-compute/hooks/useMeshAvailability" ;
1713import { AgentInstanceEditDialog } from "./AgentInstanceEditDialog" ;
1814import { createPersonaDialogState } from "./personaDialogState" ;
1915import { AgentDefinitionDialog } from "./AgentDefinitionDialog" ;
@@ -79,10 +75,11 @@ type AgentDialogProps =
7975/**
8076 * Unified entry point (Phase 1B.2/1B.3b/1B.3c): routes an intent to the form
8177 * that owns it. The definition family renders AgentDefinitionDialog — create
82- * mode adds a "start after create" toggle, definition-edit passes the caller's
83- * PersonaDialogState-derived props through unchanged (edit/duplicate/import).
84- * instance-edit renders AgentInstanceEditDialog (persistent mount + `open`
85- * toggle — its reset lifecycle is keyed on [open, agent.pubkey]).
78+ * mode always starts the agent and includes a WhereToRunSection;
79+ * definition-edit passes the caller's PersonaDialogState-derived props
80+ * through unchanged (edit/duplicate/import). instance-edit renders
81+ * AgentInstanceEditDialog (persistent mount + `open` toggle — its reset
82+ * lifecycle is keyed on [open, agent.pubkey]).
8683 */
8784export function AgentDialog ( props : AgentDialogProps ) {
8885 if ( props . mode === "instance-edit" ) {
@@ -112,47 +109,44 @@ function AgentCreateDialogRouter({
112109 runtimesLoading,
113110 onSubmitDefinition,
114111} : AgentDialogCreateProps ) {
115- const [ startAfterCreate , setStartAfterCreate ] = React . useState ( true ) ;
116112 const [ runDraft , setRunDraft ] = React . useState ( emptyWhereToRunDraft ) ;
117- // Stable identity across toggle flips — AgentDefinitionDialog re-initializes its
118- // fields whenever `initialValues` changes.
113+ const { availability : meshAvailability } = useMeshAvailability ( ) ;
114+ const meshUnavailable =
115+ meshAvailability != null && ! meshAvailability . available ;
116+ // The cleanup effect below resets the persisted draft after this render. The
117+ // submit path must not wait for that effect: it uses the local fallback
118+ // immediately when a selected mesh target disappears.
119+ const effectiveRunDraft =
120+ meshUnavailable && runDraft . runOn === "mesh"
121+ ? emptyWhereToRunDraft
122+ : runDraft ;
123+ // Persist the fallback after the synchronous guard has made the render safe.
124+ // This cleanup keeps a future availability recovery from restoring an invalid
125+ // mesh selection.
126+ React . useEffect ( ( ) => {
127+ if ( meshUnavailable && runDraft . runOn === "mesh" ) {
128+ setRunDraft ( emptyWhereToRunDraft ) ;
129+ }
130+ } , [ meshUnavailable , runDraft . runOn ] ) ;
119131 const initialValues = React . useMemo (
120132 ( ) => createPersonaDialogState ( ) . initialValues ,
121133 [ ] ,
122134 ) ;
123135
124- const copy = definitionCreateDialogState ( startAfterCreate ) ;
136+ const copy = createPersonaDialogState ( ) ;
125137
126138 return (
127139 < AgentDefinitionDialog
128- createFooterSlot = {
129- < label
130- className = "flex cursor-pointer items-center gap-2 text-sm text-muted-foreground"
131- htmlFor = "agent-dialog-start-toggle"
132- >
133- < Switch
134- checked = { startAfterCreate }
135- data-testid = "agent-dialog-start-toggle"
136- disabled = { isDefinitionPending }
137- id = "agent-dialog-start-toggle"
138- onCheckedChange = { setStartAfterCreate }
139- />
140- Start agent after creation
141- </ label >
142- }
143140 createRunSection = {
144- // "Where to run" is instance state: with the start toggle off no
145- // instance exists, so the section disappears instead of dangling.
146- startAfterCreate ? (
147- < WhereToRunSection
148- draft = { runDraft }
149- isPending = { isDefinitionPending }
150- onDraftChange = { setRunDraft }
151- />
152- ) : null
141+ < WhereToRunSection
142+ draft = { runDraft }
143+ isPending = { isDefinitionPending }
144+ meshAvailability = { meshAvailability }
145+ onDraftChange = { setRunDraft }
146+ />
153147 }
154- createSubmitBlocked = { ! canSubmitWhereToRun ( runDraft , startAfterCreate ) }
155- createRunOnMesh = { startAfterCreate && runDraft . runOn === "mesh" }
148+ createSubmitBlocked = { ! canSubmitWhereToRun ( effectiveRunDraft ) }
149+ createRunOnMesh = { effectiveRunDraft . runOn === "mesh" }
156150 description = { copy . description }
157151 error = { definitionError }
158152 initialValues = { initialValues }
@@ -161,8 +155,8 @@ function AgentCreateDialogRouter({
161155 onSubmit = { async ( input ) => {
162156 const submitted = await onSubmitDefinition (
163157 input ,
164- intentForStartToggle ( startAfterCreate ) ,
165- resolveBackendIntent ( runDraft , startAfterCreate ) ,
158+ "definition_start" ,
159+ resolveBackendIntent ( effectiveRunDraft ) ,
166160 ) ;
167161 if ( submitted ) {
168162 onOpenChange ( false ) ;
0 commit comments