Skip to content

Commit 6dc4d4e

Browse files
authored
fix(core): give managed configuration highest precedence (#55267)
1 parent 6a7cedc commit 6dc4d4e

5 files changed

Lines changed: 72 additions & 32 deletions

File tree

‎packages/core/src/config.ts‎

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { ConfigNormalize } from "./config/normalize.js"
1818
import { ConfigDiscovery } from "./config/discovery.js"
1919
import { ConfigWatch } from "./config/watch.js"
2020
import { WellKnown } from "./wellknown.js"
21+
import { ConfigExperimental } from "@opencode/schema/config/experimental"
2122
import { AppProcess } from "@opencode/util/process"
2223
import { ChildProcess } from "effect/process"
2324

@@ -241,16 +242,41 @@ export const layer = (options?: Options) =>
241242
)
242243
: []
243244

244-
// System entries sit at lowest priority so users and projects can
245-
// override ordinary defaults while reversed policy evaluation gives
246-
// system policies highest file-based authority. Global entries sit
247-
// below explicit and direct files; project directories rank above them.
248-
// MDM preferences rank below the system directory so their policies
249-
// carry the most file-based authority, matching V1 where they won.
250-
const systemSupplementary = [
251-
...(sources.preferences ? yield* loadPreferences(sources.preferences).pipe(Effect.orDie) : []),
245+
// Managed settings load last so they override everything, and MDM
246+
// preferences outrank the system directory as in V1. Policies evaluate
247+
// in reverse load order, so managed policies split off and load first
248+
// to keep final file-based authority.
249+
const managed = [
252250
...(sources.system?.present ? yield* loadDirectory(sources.system.path).pipe(Effect.orDie) : []),
251+
...(sources.preferences ? yield* loadPreferences(sources.preferences).pipe(Effect.orDie) : []),
253252
]
253+
const managedPolicies = managed
254+
.flatMap((entry) =>
255+
entry.type === "document" && entry.info.experimental?.policies
256+
? [
257+
new Document({
258+
type: "document",
259+
path: entry.path,
260+
info: new Info({
261+
experimental: new ConfigExperimental.Info({ policies: entry.info.experimental.policies }),
262+
}),
263+
}),
264+
]
265+
: [],
266+
)
267+
.toReversed()
268+
const managedSettings = managed.map((entry) =>
269+
entry.type === "document" && entry.info.experimental?.policies
270+
? new Document({
271+
...entry,
272+
info: new Info({
273+
...entry.info,
274+
experimental: new ConfigExperimental.Info({ ...entry.info.experimental, policies: undefined }),
275+
}),
276+
})
277+
: entry,
278+
)
279+
// Global entries sit below explicit and direct files; project directories rank above them.
254280
const globalSupplementary = sources.global ? yield* loadDirectory(sources.global).pipe(Effect.orDie) : []
255281
const projectSupplementary = yield* Effect.forEach(
256282
sources.project.filter((root) => root.present),
@@ -260,13 +286,14 @@ export const layer = (options?: Options) =>
260286
Effect.map((entries) => entries.flat()),
261287
)
262288
return [
263-
...systemSupplementary,
289+
...managedPolicies,
264290
...(yield* loadWellknown().pipe(Effect.orDie)),
265291
...globalSupplementary,
266292
...explicit,
267293
...direct,
268294
...projectSupplementary,
269295
...content,
296+
...managedSettings,
270297
]
271298
})
272299

‎packages/core/test/config/config.test.ts‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { Provider } from "@opencode/core/provider"
2323
import { AbsolutePath } from "@opencode/core/schema"
2424
import { WellKnown } from "@opencode/core/wellknown"
2525
import { KV } from "@opencode/core/kv"
26+
import { ManagedPolicy } from "@opencode/core/managed-policy"
2627
import { Integration } from "@opencode/schema/integration"
2728
import { emptyCredentialNode, emptyWellknownNode } from "../fixture/config-nodes"
2829
import { location } from "../fixture/location"
@@ -77,6 +78,8 @@ function testLayer(
7778
return Layer.mergeAll(built, watcher)
7879
}
7980

81+
const tool = (effect: "allow" | "deny") => ({ action: "tool.use", resource: "shell:*", effect })
82+
8083
const provider = {
8184
package: "native",
8285
settings: {},
@@ -202,7 +205,7 @@ describe("Config", () => {
202205
),
203206
)
204207

205-
it.live("loads system configuration at lowest priority", () =>
208+
it.live("loads system configuration at highest priority", () =>
206209
Effect.acquireDisposable(Effect.promise(() => tmpdir())).pipe(
207210
Effect.flatMap((tmp) => {
208211
const system = path.join(tmp.path, "system")
@@ -214,24 +217,30 @@ describe("Config", () => {
214217
await fs.mkdir(project, { recursive: true })
215218
await fs.writeFile(
216219
path.join(system, "opencode.json"),
217-
JSON.stringify({ shell: "system", username: "system-user" }),
220+
JSON.stringify({ shell: "system", username: "system-user", experimental: { policies: [tool("deny")] } }),
218221
)
219222
await fs.writeFile(path.join(global, "opencode.json"), JSON.stringify({ shell: "global" }))
220-
await fs.writeFile(path.join(project, "opencode.json"), JSON.stringify({ shell: "project" }))
223+
await fs.writeFile(
224+
path.join(project, "opencode.json"),
225+
JSON.stringify({ shell: "project", experimental: { policies: [tool("allow")] } }),
226+
)
221227
}).pipe(
222228
Effect.andThen(
223229
Effect.gen(function* () {
224230
const config = yield* Config.Service
225231
const entries = yield* config.entries()
226232
expect(
227233
entries.flatMap((entry) => (entry.type === "document" && entry.info.shell ? [entry.info.shell] : [])),
228-
).toEqual(["system", "global", "project"])
234+
).toEqual(["global", "project", "system"])
229235
expect(entries.flatMap((entry) => (entry.type === "directory" ? [entry.path] : []))).toEqual([
230-
AbsolutePath.make(system),
231236
AbsolutePath.make(global),
237+
AbsolutePath.make(system),
232238
])
233-
expect(Config.latest(entries, "shell")).toBe("project")
239+
expect(Config.latest(entries, "shell")).toBe("system")
234240
expect(Config.latest(entries, "username")).toBe("system-user")
241+
expect(
242+
ManagedPolicy.decision(ManagedPolicy.statements(entries, { statements: [] }), "tool.use", "shell:ls"),
243+
).toBe("deny")
235244
}).pipe(
236245
Effect.provide(
237246
testLayer(project, global, project, undefined, undefined, emptyCredentialNode, emptyWellknownNode, {

‎services/www/src/docs/content/config.mdx‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ OpenCode supports JSON and JSONC. Use JSONC when you want comments or trailing c
2727

2828
## Locations
2929

30-
Put machine-wide defaults in the system configuration directory:
30+
Put managed, machine-wide settings in the system configuration directory:
3131

3232
| Platform | Directory |
3333
| -------- | --------------------------------------- |
@@ -56,9 +56,11 @@ machine-wide one:
5656
/Library/Managed Preferences/ai.opencode.managed.plist
5757
```
5858

59-
Managed preferences load just below the system directory, so their
60-
[policies](/policies#precedence) have the most authority of any file. OpenCode
61-
reads them at startup and whenever another config change triggers a reload.
59+
Managed settings have the highest precedence, so users and projects cannot
60+
override the values they set, and managed preferences override the system
61+
directory. Settings alone cannot stop users from adding providers, MCP servers,
62+
agents, or plugins; use [policies](/policies#precedence) in managed
63+
configuration to enforce restrictions.
6264

6365
Put user settings for every project in the global configuration:
6466

@@ -95,15 +97,16 @@ For example, start OpenCode from `/home/user/projects/acme/packages/web`:
9597

9698
OpenCode applies these files from lowest to highest precedence:
9799

98-
1. `/etc/opencode/opencode.json`
99-
2. `~/.config/opencode/opencode.json`
100-
3. `/home/user/projects/acme/opencode.json`
101-
4. `/home/user/projects/acme/packages/web/opencode.json`
100+
1. `~/.config/opencode/opencode.json`
101+
2. `/home/user/projects/acme/opencode.json`
102+
3. `/home/user/projects/acme/packages/web/opencode.json`
103+
4. `/etc/opencode/opencode.json`
102104

103105
The package config overrides matching settings from the repository config,
104-
which overrides the global and system configs. Settings that do not conflict
105-
are preserved, and [policies](/policies#precedence) evaluate in the reverse
106-
order so system policies outrank global and project policies.
106+
which overrides the global config. The system config overrides all of them.
107+
Settings that do not conflict are preserved. [Policies](/policies#precedence)
108+
from global and project files evaluate in the reverse order, and system
109+
policies outrank all of them.
107110

108111
## Schema
109112

‎services/www/src/docs/content/migrate-v1.mdx‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ V2 reads these same locations. It normalizes supported V1 and native V2 fields i
6565
file. Existing supported V1 configuration is intended to keep working, so you do not need to convert it to try or adopt
6666
V2.
6767

68-
In V2, the system configuration directory loads at the lowest precedence for ordinary settings so it provides
69-
machine-wide defaults, while [policies](/policies#precedence) evaluate in the reverse order so system
70-
`experimental.policies` (including migrated `enabled_providers` and `disabled_providers`) cannot be overridden by global
71-
or project files. Express admin-enforced restrictions as `experimental.policies` rather than ordinary settings such as
72-
`permissions`.
68+
As in V1, system configuration and macOS managed preferences have the highest precedence, so their settings override
69+
global and project files. Their [policies](/policies#precedence), including migrated `enabled_providers` and
70+
`disabled_providers`, also outrank global and project policies. Overriding a setting cannot stop users from adding
71+
providers, MCP servers, agents, or plugins, so express admin-enforced restrictions as `experimental.policies` rather
72+
than ordinary settings such as `permissions`.
7373

7474
### Ask OpenCode to migrate
7575

‎services/www/src/docs/content/policies.mdx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ it was configured.
181181
## Precedence
182182

183183
Ordinary settings let the nearest configuration win. Policies reverse that: statements
184-
from broader configuration are evaluated later, so they override narrower ones.
184+
from broader configuration are evaluated later, so they override narrower ones. Managed
185+
configuration and the connected Console always have the final say.
185186

186187
| Authority | Source |
187188
| ----------- | -------------------------------------------------------------------------------------------------------------- |

0 commit comments

Comments
 (0)