File tree Expand file tree Collapse file tree
packages/opencode/src/cli/cmd Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -528,6 +528,45 @@ export const RunCommand = cmd({
528528 // Validate agent if specified
529529 const agent = await ( async ( ) => {
530530 if ( ! args . agent ) return undefined
531+
532+ // When attaching, validate against the running server instead of local Instance state.
533+ if ( args . attach ) {
534+ const modes = await sdk . app
535+ . agents ( )
536+ . then ( ( x ) => x . data ?? [ ] )
537+ . catch ( ( ) => undefined )
538+
539+ if ( ! modes ) {
540+ UI . println (
541+ UI . Style . TEXT_WARNING_BOLD + "!" ,
542+ UI . Style . TEXT_NORMAL ,
543+ `failed to list agents from ${ args . attach } . Falling back to default agent` ,
544+ )
545+ return undefined
546+ }
547+
548+ const agent = modes . find ( ( a ) => a . name === args . agent )
549+ if ( ! agent ) {
550+ UI . println (
551+ UI . Style . TEXT_WARNING_BOLD + "!" ,
552+ UI . Style . TEXT_NORMAL ,
553+ `agent "${ args . agent } " not found. Falling back to default agent` ,
554+ )
555+ return undefined
556+ }
557+
558+ if ( agent . mode === "subagent" ) {
559+ UI . println (
560+ UI . Style . TEXT_WARNING_BOLD + "!" ,
561+ UI . Style . TEXT_NORMAL ,
562+ `agent "${ args . agent } " is a subagent, not a primary agent. Falling back to default agent` ,
563+ )
564+ return undefined
565+ }
566+
567+ return args . agent
568+ }
569+
531570 const entry = await Agent . get ( args . agent )
532571 if ( ! entry ) {
533572 UI . println (
You can’t perform that action at this time.
0 commit comments