@@ -9,7 +9,6 @@ import { Glob } from "@opencode-ai/core/util/glob"
99import { configEntryNameFromPath } from "./entry-name"
1010import * as ConfigMarkdown from "./markdown"
1111import { ConfigModelID } from "./model-id"
12- import { ConfigParse } from "./parse"
1312import { ConfigPermission } from "./permission"
1413
1514const log = Log . create ( { service : "config" } )
@@ -104,6 +103,18 @@ export const Info = AgentSchema.pipe(
104103) . annotate ( { identifier : "AgentConfig" } )
105104export type Info = Schema . Schema . Type < typeof Info >
106105
106+ // Surface a config-loading failure to the user and log it, so an invalid file is
107+ // reported instead of crashing the whole load (load) or vanishing silently (loadMode).
108+ // Publishing requires an instance context that isn't always present (e.g. tests), so a
109+ // publish failure is swallowed — the log line is the durable record, the event is best-effort.
110+ async function report ( label : string , item : string , message : string , err : unknown ) {
111+ log . error ( `failed to load ${ label } ` , { [ label ] : item , err } )
112+ try {
113+ const { Session } = await import ( "@/session/session" )
114+ await Bus . publish ( Session . Event . Error , { error : new NamedError . Unknown ( { message } ) . toObject ( ) } )
115+ } catch { }
116+ }
117+
107118export async function load ( dir : string ) {
108119 const result : Record < string , Info > = { }
109120 for ( const item of await Glob . scan ( "{agent,agents}/**/*.md" , {
@@ -113,25 +124,24 @@ export async function load(dir: string) {
113124 symlink : true ,
114125 } ) ) {
115126 const md = await ConfigMarkdown . parse ( item ) . catch ( async ( err ) => {
116- const message = ConfigMarkdown . FrontmatterError . isInstance ( err )
117- ? err . data . message
118- : `Failed to parse agent ${ item } `
119- const { Session } = await import ( "@/session/session" )
120- void Bus . publish ( Session . Event . Error , { error : new NamedError . Unknown ( { message } ) . toObject ( ) } )
121- log . error ( "failed to load agent" , { agent : item , err } )
127+ const message = ConfigMarkdown . FrontmatterError . isInstance ( err ) ? err . data . message : `Failed to parse agent ${ item } `
128+ await report ( "agent" , item , message , err )
122129 return undefined
123130 } )
124131 if ( ! md ) continue
125132
126133 const patterns = [ "/.opencode/agent/" , "/.opencode/agents/" , "/agent/" , "/agents/" ]
127- const name = configEntryNameFromPath ( item , patterns )
128-
129134 const config = {
130- name,
135+ name : configEntryNameFromPath ( item , patterns ) ,
131136 ...md . data ,
132137 prompt : md . content . trim ( ) ,
133138 }
134- result [ config . name ] = ConfigParse . schema ( Info , config , item )
139+ const parsed = Schema . decodeUnknownExit ( Info ) ( config , { errors : "all" , propertyOrder : "original" } )
140+ if ( Exit . isFailure ( parsed ) ) {
141+ await report ( "agent" , item , `Failed to parse agent ${ item } ` , parsed . cause )
142+ continue
143+ }
144+ result [ config . name ] = parsed . value
135145 }
136146 return result
137147}
@@ -145,12 +155,8 @@ export async function loadMode(dir: string) {
145155 symlink : true ,
146156 } ) ) {
147157 const md = await ConfigMarkdown . parse ( item ) . catch ( async ( err ) => {
148- const message = ConfigMarkdown . FrontmatterError . isInstance ( err )
149- ? err . data . message
150- : `Failed to parse mode ${ item } `
151- const { Session } = await import ( "@/session/session" )
152- void Bus . publish ( Session . Event . Error , { error : new NamedError . Unknown ( { message } ) . toObject ( ) } )
153- log . error ( "failed to load mode" , { mode : item , err } )
158+ const message = ConfigMarkdown . FrontmatterError . isInstance ( err ) ? err . data . message : `Failed to parse mode ${ item } `
159+ await report ( "mode" , item , message , err )
154160 return undefined
155161 } )
156162 if ( ! md ) continue
@@ -161,11 +167,13 @@ export async function loadMode(dir: string) {
161167 prompt : md . content . trim ( ) ,
162168 }
163169 const parsed = Schema . decodeUnknownExit ( Info ) ( config , { errors : "all" , propertyOrder : "original" } )
164- if ( Exit . isSuccess ( parsed ) ) {
165- result [ config . name ] = {
166- ...parsed . value ,
167- mode : "primary" as const ,
168- }
170+ if ( Exit . isFailure ( parsed ) ) {
171+ await report ( "mode" , item , `Failed to parse mode ${ item } ` , parsed . cause )
172+ continue
173+ }
174+ result [ config . name ] = {
175+ ...parsed . value ,
176+ mode : "primary" as const ,
169177 }
170178 }
171179 return result
0 commit comments