@@ -62,8 +62,14 @@ export namespace Config {
6262 export const state = Instance . state ( async ( ) => {
6363 const auth = await Auth . all ( )
6464
65- // Load remote/well-known config first as the base layer (lowest precedence)
66- // This allows organizations to provide default configs that users can override
65+ // Config loading order (low -> high precedence): https://opencode.ai/docs/config#precedence-order
66+ // 1) Remote .well-known/opencode (org defaults)
67+ // 2) Global config (~/.config/opencode/opencode.json{,c})
68+ // 3) Custom config (OPENCODE_CONFIG)
69+ // 4) Project config (opencode.json{,c})
70+ // 5) .opencode directories (.opencode/agents/, .opencode/commands/, .opencode/plugins/, .opencode/opencode.json{,c})
71+ // 6) Inline config (OPENCODE_CONFIG_CONTENT)
72+ // Managed config directory is enterprise-only and always overrides everything above.
6773 let result : Info = { }
6874 for ( const [ key , value ] of Object . entries ( auth ) ) {
6975 if ( value . type === "wellknown" ) {
@@ -85,16 +91,16 @@ export namespace Config {
8591 }
8692 }
8793
88- // Global user config overrides remote config
94+ // Global user config overrides remote config.
8995 result = mergeConfigConcatArrays ( result , await global ( ) )
9096
91- // Custom config path overrides global
97+ // Custom config path overrides global config.
9298 if ( Flag . OPENCODE_CONFIG ) {
9399 result = mergeConfigConcatArrays ( result , await loadFile ( Flag . OPENCODE_CONFIG ) )
94100 log . debug ( "loaded custom config" , { path : Flag . OPENCODE_CONFIG } )
95101 }
96102
97- // Project config has highest precedence ( overrides global and remote)
103+ // Project config overrides global and remote config.
98104 if ( ! Flag . OPENCODE_DISABLE_PROJECT_CONFIG ) {
99105 for ( const file of [ "opencode.jsonc" , "opencode.json" ] ) {
100106 const found = await Filesystem . findUp ( file , Instance . directory , Instance . worktree )
@@ -104,12 +110,6 @@ export namespace Config {
104110 }
105111 }
106112
107- // Inline config content has highest precedence
108- if ( Flag . OPENCODE_CONFIG_CONTENT ) {
109- result = mergeConfigConcatArrays ( result , JSON . parse ( Flag . OPENCODE_CONFIG_CONTENT ) )
110- log . debug ( "loaded custom config from OPENCODE_CONFIG_CONTENT" )
111- }
112-
113113 result . agent = result . agent || { }
114114 result . mode = result . mode || { }
115115 result . plugin = result . plugin || [ ]
@@ -136,6 +136,7 @@ export namespace Config {
136136 ) ) ,
137137 ]
138138
139+ // .opencode directory config overrides (project and global) config sources.
139140 if ( Flag . OPENCODE_CONFIG_DIR ) {
140141 directories . push ( Flag . OPENCODE_CONFIG_DIR )
141142 log . debug ( "loading config from OPENCODE_CONFIG_DIR" , { path : Flag . OPENCODE_CONFIG_DIR } )
@@ -163,6 +164,12 @@ export namespace Config {
163164 result . plugin . push ( ...( await loadPlugin ( dir ) ) )
164165 }
165166
167+ // Inline config content overrides all non-managed config sources.
168+ if ( Flag . OPENCODE_CONFIG_CONTENT ) {
169+ result = mergeConfigConcatArrays ( result , JSON . parse ( Flag . OPENCODE_CONFIG_CONTENT ) )
170+ log . debug ( "loaded custom config from OPENCODE_CONFIG_CONTENT" )
171+ }
172+
166173 // Load managed config files last (highest priority) - enterprise admin-controlled
167174 // Kept separate from directories array to avoid write operations when installing plugins
168175 // which would fail on system directories requiring elevated permissions
0 commit comments