Skip to content
This repository was archived by the owner on Jul 6, 2026. It is now read-only.

Commit 86a4210

Browse files
feat(config): add configBoundary option for parent config traversal
Adds a configBoundary field to control how far up the filesystem OpenCode walks when searching for parent configs, opencode.json files, and instruction files like AGENTS.md. 'current' (default) — stops at git worktree root, backward compatible 'home' — walks to $HOME, enables client-level MCP/AGENTS.md 'root' — walks to / 'none' — stops immediately, disables parent config inheritance Use case: a ~/Projects/ClientA/opencode.jsonc with ASU-specific MCPs gets picked up by all projects under ClientA/ when configBoundary is 'home' in the global config. Also resolves relative instruction paths (./foo.md) from the config file directory rather than cwd, so client-level configs can reference local files. Closes anomalyco#10707 Co-authored-by: Sisyphus <[email protected]>
1 parent f5c3d35 commit 86a4210

3 files changed

Lines changed: 41 additions & 8 deletions

File tree

‎packages/core/src/flag/flag.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ export const Flag = {
108108
get OPENCODE_CONFIG_DIR() {
109109
return process.env["OPENCODE_CONFIG_DIR"]
110110
},
111+
get OPENCODE_CONFIG_BOUNDARY() {
112+
return process.env["OPENCODE_CONFIG_BOUNDARY"] as "current" | "home" | "root" | "none" | undefined
113+
},
111114
get OPENCODE_PURE() {
112115
return truthy("OPENCODE_PURE")
113116
},

‎packages/opencode/src/config/config.ts‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ export const Info = Schema.Struct({
203203
instructions: Schema.optional(Schema.mutable(Schema.Array(Schema.String))).annotate({
204204
description: "Additional instruction files or patterns to include",
205205
}),
206+
configBoundary: Schema.optional(Schema.Literals(["current", "home", "root", "none"])).annotate({
207+
description:
208+
"Controls how far up the filesystem OpenCode searches for parent configs and instruction files. " +
209+
"'current' (default) stops at the git worktree root. 'home' walks up to $HOME (useful for client-level MCP/AGENTS.md inheritance). " +
210+
"'root' walks to /. 'none' disables parent config search entirely.",
211+
}),
206212
layout: Schema.optional(ConfigLayout.Layout).annotate({ description: "@deprecated Always uses stretch layout." }),
207213
permission: Schema.optional(ConfigPermission.Info),
208214
tools: Schema.optional(Schema.Record(Schema.String, Schema.Boolean)),
@@ -281,6 +287,21 @@ export type Info = DeepMutable<Schema.Schema.Type<typeof Info>> & {
281287
plugin_origins?: ConfigPlugin.Origin[]
282288
}
283289

290+
export function getConfigStop(boundary?: Info["configBoundary"], worktree?: string): string {
291+
const effective = Flag.OPENCODE_CONFIG_BOUNDARY ?? boundary ?? "current"
292+
switch (effective) {
293+
case "none":
294+
return worktree ?? "/"
295+
case "home":
296+
return Global.Path.home
297+
case "root":
298+
return "/"
299+
case "current":
300+
default:
301+
return worktree ?? "/"
302+
}
303+
}
304+
284305
type State = {
285306
config: Info
286307
directories: string[]
@@ -372,6 +393,13 @@ export const layer = Layer.effect(
372393
if (!("path" in options)) return data
373394

374395
yield* Effect.promise(() => resolveLoadedPlugins(data, options.path))
396+
if (data.instructions) {
397+
const configDir = path.dirname(options.path)
398+
data.instructions = data.instructions.map((instruction) => {
399+
if (!instruction.startsWith("./") && !instruction.startsWith("../")) return instruction
400+
return path.resolve(configDir, instruction)
401+
})
402+
}
375403
if (!data.$schema) {
376404
data.$schema = "https://opencode.ai/config.json"
377405
const updated = text.replace(/^\s*\{/, '{\n "$schema": "https://opencode.ai/config.json",')
@@ -515,8 +543,10 @@ export const layer = Layer.effect(
515543
log.debug("loaded custom config", { path: Flag.OPENCODE_CONFIG })
516544
}
517545

546+
const stop = getConfigStop(result.configBoundary, ctx.worktree)
547+
518548
if (!Flag.OPENCODE_DISABLE_PROJECT_CONFIG) {
519-
for (const file of yield* ConfigPaths.files("opencode", ctx.directory, ctx.worktree).pipe(Effect.orDie)) {
549+
for (const file of yield* ConfigPaths.files("opencode", ctx.directory, stop).pipe(Effect.orDie)) {
520550
yield* merge(file, yield* loadFile(file), "local")
521551
}
522552
}
@@ -525,7 +555,7 @@ export const layer = Layer.effect(
525555
result.mode = result.mode || {}
526556
result.plugin = result.plugin || []
527557

528-
const directories = yield* ConfigPaths.directories(ctx.directory, ctx.worktree)
558+
const directories = yield* ConfigPaths.directories(ctx.directory, stop)
529559

530560
if (Flag.OPENCODE_CONFIG_DIR) {
531561
log.debug("loading config from OPENCODE_CONFIG_DIR", { path: Flag.OPENCODE_CONFIG_DIR })

‎packages/opencode/src/session/instruction.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from "path"
22
import { Effect, Layer, Context } from "effect"
33
import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/unstable/http"
4-
import { Config } from "@/config/config"
4+
import { Config, getConfigStop } from "@/config/config"
55
import { InstanceState } from "@/effect/instance-state"
66
import { Flag } from "@opencode-ai/core/flag/flag"
77
import { AppFileSystem } from "@opencode-ai/core/filesystem"
@@ -72,11 +72,11 @@ export const layer: Layer.Layer<
7272
),
7373
)
7474

75-
const relative = Effect.fnUntraced(function* (instruction: string) {
75+
const relative = Effect.fnUntraced(function* (instruction: string, stop: string) {
7676
const ctx = yield* InstanceState.context
7777
if (!Flag.OPENCODE_DISABLE_PROJECT_CONFIG) {
7878
return yield* fs
79-
.globUp(instruction, ctx.directory, ctx.worktree)
79+
.globUp(instruction, ctx.directory, stop)
8080
.pipe(Effect.catch(() => Effect.succeed([] as string[])))
8181
}
8282
return yield* fs
@@ -107,6 +107,7 @@ export const layer: Layer.Layer<
107107
const config = yield* cfg.get()
108108
const ctx = yield* InstanceState.context
109109
const paths = new Set<string>()
110+
const stop = getConfigStop(config.configBoundary, ctx.worktree)
110111

111112
for (const file of globalFiles) {
112113
if (yield* fs.existsSafe(file)) {
@@ -115,10 +116,9 @@ export const layer: Layer.Layer<
115116
}
116117
}
117118

118-
// The first project-level match wins so we don't stack AGENTS.md/CLAUDE.md from every ancestor.
119119
if (!Flag.OPENCODE_DISABLE_PROJECT_CONFIG) {
120120
for (const file of FILES) {
121-
const matches = yield* fs.findUp(file, ctx.directory, ctx.worktree)
121+
const matches = yield* fs.findUp(file, ctx.directory, stop)
122122
if (matches.length > 0) {
123123
matches.forEach((item) => paths.add(path.resolve(item)))
124124
break
@@ -137,7 +137,7 @@ export const layer: Layer.Layer<
137137
absolute: true,
138138
include: "file",
139139
})
140-
: relative(instruction)
140+
: relative(instruction, stop)
141141
).pipe(Effect.catch(() => Effect.succeed([] as string[])))
142142
matches.forEach((item) => paths.add(path.resolve(item)))
143143
}

0 commit comments

Comments
 (0)