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

Commit 2a79f91

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 1772e8e commit 2a79f91

4 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
@@ -62,6 +62,9 @@ export const Flag = {
6262
get OPENCODE_CONFIG_DIR() {
6363
return process.env["OPENCODE_CONFIG_DIR"]
6464
},
65+
get OPENCODE_CONFIG_BOUNDARY() {
66+
return process.env["OPENCODE_CONFIG_BOUNDARY"] as "current" | "home" | "root" | "none" | undefined
67+
},
6568
get OPENCODE_PURE() {
6669
return truthy("OPENCODE_PURE")
6770
},

‎packages/core/src/v1/config/config.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ export const Info = Schema.Struct({
118118
instructions: Schema.optional(Schema.mutable(Schema.Array(Schema.String))).annotate({
119119
description: "Additional instruction files or patterns to include",
120120
}),
121+
configBoundary: Schema.optional(Schema.Literals(["current", "home", "root", "none"])).annotate({
122+
description:
123+
"Controls how far up the filesystem OpenCode searches for parent configs and instruction files. " +
124+
"'current' (default) stops at the git worktree root. 'home' walks up to $HOME (useful for client-level MCP/AGENTS.md inheritance). " +
125+
"'root' walks to /. 'none' disables parent config search entirely.",
126+
}),
121127
layout: Schema.optional(ConfigLayoutV1.Layout).annotate({ description: "@deprecated Always uses stretch layout." }),
122128
permission: Schema.optional(ConfigPermissionV1.Info),
123129
tools: Schema.optional(Schema.Record(Schema.String, Schema.Boolean)),

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,21 @@ type Info = ConfigV1.Info & {
115115
plugin_origins?: ConfigPlugin.Origin[]
116116
}
117117

118+
export function getConfigStop(boundary?: Info["configBoundary"], worktree?: string): string {
119+
const effective = Flag.OPENCODE_CONFIG_BOUNDARY ?? boundary ?? "current"
120+
switch (effective) {
121+
case "none":
122+
return worktree ?? "/"
123+
case "home":
124+
return Global.Path.home
125+
case "root":
126+
return "/"
127+
case "current":
128+
default:
129+
return worktree ?? "/"
130+
}
131+
}
132+
118133
type State = {
119134
config: Info
120135
directories: string[]
@@ -220,6 +235,13 @@ export const layer = Layer.effect(
220235
if (!("path" in options)) return data
221236

222237
yield* Effect.promise(() => resolveLoadedPlugins(data, options.path))
238+
if (data.instructions) {
239+
const configDir = path.dirname(options.path)
240+
data.instructions = data.instructions.map((instruction) => {
241+
if (!instruction.startsWith("./") && !instruction.startsWith("../")) return instruction
242+
return path.resolve(configDir, instruction)
243+
})
244+
}
223245
if (!data.$schema) {
224246
data.$schema = "https://opencode.ai/config.json"
225247
const updated = text.replace(/^\s*\{/, '{\n "$schema": "https://opencode.ai/config.json",')
@@ -394,8 +416,10 @@ export const layer = Layer.effect(
394416
log.debug("loaded custom config", { path: Flag.OPENCODE_CONFIG })
395417
}
396418

419+
const stop = getConfigStop(result.configBoundary, ctx.worktree)
420+
397421
if (!Flag.OPENCODE_DISABLE_PROJECT_CONFIG) {
398-
for (const file of yield* ConfigPaths.files("opencode", ctx.directory, ctx.worktree).pipe(Effect.orDie)) {
422+
for (const file of yield* ConfigPaths.files("opencode", ctx.directory, stop).pipe(Effect.orDie)) {
399423
yield* merge(file, yield* loadFile(file, authEnv), "local")
400424
}
401425
}
@@ -404,7 +428,7 @@ export const layer = Layer.effect(
404428
result.mode = result.mode || {}
405429
result.plugin = result.plugin || []
406430

407-
const directories = yield* ConfigPaths.directories(ctx.directory, ctx.worktree)
431+
const directories = yield* ConfigPaths.directories(ctx.directory, stop)
408432

409433
if (Flag.OPENCODE_CONFIG_DIR) {
410434
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
@@ -2,7 +2,7 @@ import path from "path"
22
import { SessionV1 } from "@opencode-ai/core/v1/session"
33
import { Effect, Layer, Context } from "effect"
44
import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/unstable/http"
5-
import { Config } from "@/config/config"
5+
import { Config, getConfigStop } from "@/config/config"
66
import { InstanceState } from "@/effect/instance-state"
77
import { RuntimeFlags } from "@/effect/runtime-flags"
88
import { Flag } from "@opencode-ai/core/flag/flag"
@@ -74,11 +74,11 @@ export const layer: Layer.Layer<
7474
),
7575
)
7676

77-
const relative = Effect.fnUntraced(function* (instruction: string) {
77+
const relative = Effect.fnUntraced(function* (instruction: string, stop: string) {
7878
const ctx = yield* InstanceState.context
7979
if (!Flag.OPENCODE_DISABLE_PROJECT_CONFIG) {
8080
return yield* fs
81-
.globUp(instruction, ctx.directory, ctx.worktree)
81+
.globUp(instruction, ctx.directory, stop)
8282
.pipe(Effect.catch(() => Effect.succeed([] as string[])))
8383
}
8484
return yield* fs
@@ -109,6 +109,7 @@ export const layer: Layer.Layer<
109109
const config = yield* cfg.get()
110110
const ctx = yield* InstanceState.context
111111
const paths = new Set<string>()
112+
const stop = getConfigStop(config.configBoundary, ctx.worktree)
112113

113114
for (const file of globalFiles) {
114115
if (yield* fs.existsSafe(file)) {
@@ -117,11 +118,10 @@ export const layer: Layer.Layer<
117118
}
118119
}
119120

120-
// The first project-level match wins so we don't stack AGENTS.md/CLAUDE.md from every ancestor.
121121
if (!Flag.OPENCODE_DISABLE_PROJECT_CONFIG) {
122122
for (const file of instructionFiles) {
123123
const matches = yield* fs
124-
.findUp(file, ctx.directory, ctx.worktree)
124+
.findUp(file, ctx.directory, stop)
125125
.pipe(Effect.catch(() => Effect.succeed([])))
126126
if (matches.length > 0) {
127127
matches.forEach((item) => paths.add(path.resolve(item)))
@@ -141,7 +141,7 @@ export const layer: Layer.Layer<
141141
absolute: true,
142142
include: "file",
143143
})
144-
: relative(instruction)
144+
: relative(instruction, stop)
145145
).pipe(Effect.catch(() => Effect.succeed([] as string[])))
146146
matches.forEach((item) => paths.add(path.resolve(item)))
147147
}

0 commit comments

Comments
 (0)