GeezerVison is showing github.com — feat(core): stabilize top-level policies config (#55297) · matglas/opencode@f576da5
File treeExpand file treeCollapse file tree Open diff view settings client/src/promise/generated
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/client/src/promise/generated/types.ts +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2154,6 +2154,11 @@ export type ConfigEntry = | 2154 | 2154 | enterprise?: { url?: string } | 2155 | 2155 | username?: string | 2156 | 2156 | permissions?: PermissionRuleset | | 2157 | + policies?: Array<{ | | 2158 | + action: "provider.use" | "tool.use" | "integration.use" | | 2159 | + resource: string | | 2160 | + effect: "allow" | "deny" | | 2161 | + }> | 2157 | 2162 | agents?: { | 2158 | 2163 | [x: string]: { | 2159 | 2164 | model?: string | { providerID: string; model: string; variant?: string } | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/config.ts +14-14Lines changed: 14 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,7 +18,6 @@ import { ConfigNormalize } from "./config/normalize.js" | 18 | 18 | import { ConfigDiscovery } from "./config/discovery.js" | 19 | 19 | import { ConfigWatch } from "./config/watch.js" | 20 | 20 | import { WellKnown } from "./wellknown.js" | 21 | | -import { ConfigExperimental } from "@opencode/schema/config/experimental" | 22 | 21 | import { AppProcess } from "@opencode/util/process" | 23 | 22 | import { ChildProcess } from "effect/process" | 24 | 23 |
| @@ -252,28 +251,20 @@ export const layer = (options?: Options) => | 252 | 251 | ] | 253 | 252 | const managedPolicies = managed | 254 | 253 | .flatMap((entry) => | 255 | | - entry.type === "document" && entry.info.experimental?.policies | | 254 | + entry.type === "document" && entry.info.policies | 256 | 255 | ? [ | 257 | 256 | new Document({ | 258 | 257 | type: "document", | 259 | 258 | path: entry.path, | 260 | | - info: new Info({ | 261 | | - experimental: new ConfigExperimental.Info({ policies: entry.info.experimental.policies }), | 262 | | - }), | | 259 | + info: new Info({ policies: entry.info.policies }), | 263 | 260 | }), | 264 | 261 | ] | 265 | 262 | : [], | 266 | 263 | ) | 267 | 264 | .toReversed() | 268 | 265 | 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 | | - }) | | 266 | + entry.type === "document" && entry.info.policies | | 267 | + ? new Document({ ...entry, info: new Info({ ...entry.info, policies: undefined }) }) | 277 | 268 | : entry, | 278 | 269 | ) | 279 | 270 | // Global entries sit below explicit and direct files; project directories rank above them. | @@ -428,7 +419,16 @@ export function configured(options?: Options) { | 428 | 419 | return makeLocationNode({ | 429 | 420 | service: Service, | 430 | 421 | layer: layer(options), | 431 | | - deps: [Watcher.node, Bus.node, FSUtil.node, Global.node, Location.node, Credential.node, WellKnown.node, AppProcess.node], | | 422 | + deps: [ | | 423 | + Watcher.node, | | 424 | + Bus.node, | | 425 | + FSUtil.node, | | 426 | + Global.node, | | 427 | + Location.node, | | 428 | + Credential.node, | | 429 | + WellKnown.node, | | 430 | + AppProcess.node, | | 431 | + ], | 432 | 432 | }) | 433 | 433 | } | 434 | 434 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/config/normalize.ts +30-14Lines changed: 30 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -192,6 +192,7 @@ export function normalize(input: unknown): Result { | 192 | 192 | normalizeSkills(input, encoded, diagnostics) | 193 | 193 | normalizeMcp(input, encoded, diagnostics) | 194 | 194 | normalizeCompaction(input, encoded, diagnostics) | | 195 | + normalizePolicies(input, encoded, diagnostics) | 195 | 196 | normalizeExperimental(input, encoded, diagnostics) | 196 | 197 | normalizeWatcher(input, encoded, diagnostics) | 197 | 198 | normalizeFormatter(input, encoded, diagnostics) | @@ -378,12 +379,13 @@ function normalizeCompaction( | 378 | 379 | if (Object.keys(result).length || !Object.keys(input.compaction).length) encoded.compaction = result | 379 | 380 | } | 380 | 381 |
| 381 | | -function normalizeExperimental( | | 382 | +// V1 provider lists and `experimental.policies` stay supported and merge into | | 383 | +// top-level `policies`; within a document, later sources win ties. | | 384 | +function normalizePolicies( | 382 | 385 | input: Record<string, unknown>, | 383 | 386 | encoded: Record<string, unknown>, | 384 | 387 | diagnostics: Diagnostic[], | 385 | 388 | ) { | 386 | | - const result: Record<string, unknown> = {} | 387 | 389 | const generated: unknown[] = [] | 388 | 390 | const enabled = decodeProviderList(input, "enabled_providers", diagnostics) | 389 | 391 | if (enabled.present && (!enabled.nonEmpty || enabled.values.length)) { | @@ -404,7 +406,32 @@ function normalizeExperimental( | 404 | 406 | effect: "deny", | 405 | 407 | })), | 406 | 408 | ) | 407 | | - const native: unknown[] = [] | | 409 | + const experimental = isRecord(input.experimental) | | 410 | + ? decodeList( | | 411 | + input.experimental.policies, | | 412 | + ConfigPolicy.Info, | | 413 | + ["experimental", "policies"], | | 414 | + diagnostics, | | 415 | + decodeEncoded, | | 416 | + ) | | 417 | + : [] | | 418 | + const native = decodeList(input.policies, ConfigPolicy.Info, ["policies"], diagnostics, decodeEncoded) | | 419 | + if ( | | 420 | + generated.length || | | 421 | + experimental.length || | | 422 | + native.length || | | 423 | + Array.isArray(input.policies) || | | 424 | + (isRecord(input.experimental) && Array.isArray(input.experimental.policies)) | | 425 | + ) | | 426 | + encoded.policies = [...generated, ...experimental, ...native] | | 427 | +} | | 428 | + | | 429 | +function normalizeExperimental( | | 430 | + input: Record<string, unknown>, | | 431 | + encoded: Record<string, unknown>, | | 432 | + diagnostics: Diagnostic[], | | 433 | +) { | | 434 | + const result: Record<string, unknown> = {} | 408 | 435 | if (own(input, "experimental")) { | 409 | 436 | if (!isRecord(input.experimental)) invalid(["experimental"], diagnostics) | 410 | 437 | if (isRecord(input.experimental)) { | @@ -430,19 +457,8 @@ function normalizeExperimental( | 430 | 457 | ) | 431 | 458 | if (value !== undefined) result.subagent_depth = value | 432 | 459 | } | 433 | | - native.push( | 434 | | - ...decodeList( | 435 | | - experimental.policies, | 436 | | - ConfigPolicy.Info, | 437 | | - ["experimental", "policies"], | 438 | | - diagnostics, | 439 | | - decodeEncoded, | 440 | | - ), | 441 | | - ) | 442 | 460 | } | 443 | 461 | } | 444 | | - if (generated.length || native.length || (isRecord(input.experimental) && Array.isArray(input.experimental.policies))) | 445 | | - result.policies = [...generated, ...native] | 446 | 462 | if (Object.keys(result).length || (isRecord(input.experimental) && !Object.keys(input.experimental).length)) | 447 | 463 | encoded.experimental = result | 448 | 464 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/managed-policy.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -52,7 +52,7 @@ export function statements(entries: readonly Entry[], organization: State) { | 52 | 52 | ...entries | 53 | 53 | .filter((entry): entry is Document => entry.type === "document") | 54 | 54 | .toReversed() | 55 | | - .flatMap((entry) => entry.info.experimental?.policies ?? []) | | 55 | + .flatMap((entry) => entry.info.policies ?? []) | 56 | 56 | .map((policy) => ({ ...policy, message: "Blocked by configuration policy" })), | 57 | 57 | ...organization.statements.map((policy) => ({ | 58 | 58 | ...policy, | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/plugin/provider/opencode.ts +24-17Lines changed: 24 additions & 17 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -20,6 +20,23 @@ import { Money } from "@opencode/schema/money" | 20 | 20 | const defaultServer = "https://opencode.ai/console" | 21 | 21 | const clientID = "opencode-cli" | 22 | 22 | const methodID = Integration.MethodID.make("device") | | 23 | +// Drop statements whose action or effect is a value this version does not know, so a newer Console can | | 24 | +// extend either enum. Malformed statements still fail validation. | | 25 | +const RemotePolicies = Schema.Array(Schema.Unknown).pipe( | | 26 | + Schema.decodeTo(Schema.Unknown, { | | 27 | + decode: SchemaGetter.transform((policies) => | | 28 | + policies.filter((policy) => { | | 29 | + const action = Schema.decodeUnknownOption(Schema.Struct({ action: Schema.String }))(policy) | | 30 | + if (Option.isSome(action) && !Schema.is(ConfigPolicy.Info.fields.action)(action.value.action)) return false | | 31 | + const effect = Schema.decodeUnknownOption(Schema.Struct({ effect: Schema.String }))(policy) | | 32 | + return Option.isNone(effect) || Schema.is(ConfigPolicy.Effect)(effect.value.effect) | | 33 | + }), | | 34 | + ), | | 35 | + encode: SchemaGetter.passthrough({ strict: false }), | | 36 | + }), | | 37 | + Schema.decodeTo(Schema.Array(ConfigPolicy.Info)), | | 38 | + Schema.optional, | | 39 | +) | 23 | 40 | const RemoteResponse = Schema.Struct({ | 24 | 41 | providers: Schema.Record(Schema.String, ConfigProvider.Info), | 25 | 42 | websearch: Schema.Struct({ | @@ -35,22 +52,9 @@ const RemoteResponse = Schema.Struct({ | 35 | 52 | ), | 36 | 53 | }).pipe(Schema.optional), | 37 | 54 | // Organization policy compiled for the authenticated caller; omitted when there is none. | 38 | | - experimental: Schema.Struct({ | 39 | | - policies: Schema.Array(Schema.Unknown).pipe( | 40 | | - Schema.decodeTo(Schema.Unknown, { | 41 | | - // Filter only unsupported actions. Malformed supported statements still fail validation. | 42 | | - decode: SchemaGetter.transform((policies) => | 43 | | - policies.filter((policy) => { | 44 | | - const action = Schema.decodeUnknownOption(Schema.Struct({ action: Schema.String }))(policy) | 45 | | - return Option.isNone(action) || Schema.is(ConfigPolicy.Info.fields.action)(action.value.action) | 46 | | - }), | 47 | | - ), | 48 | | - encode: SchemaGetter.passthrough({ strict: false }), | 49 | | - }), | 50 | | - Schema.decodeTo(Schema.Array(ConfigPolicy.Info)), | 51 | | - Schema.optional, | 52 | | - ), | 53 | | - }).pipe(Schema.optional), | | 55 | + // `experimental.policies` remains accepted alongside top-level `policies`. | | 56 | + policies: RemotePolicies, | | 57 | + experimental: Schema.Struct({ policies: RemotePolicies }).pipe(Schema.optional), | 54 | 58 | }) | 55 | 59 | const Device = Schema.Struct({ | 56 | 60 | device_code: Schema.String, | @@ -243,7 +247,10 @@ export const OpencodePlugin = define<HttpClient.HttpClient | Bus.Service | Manag | 243 | 247 | }) | 244 | 248 | // Statements ride on the snapshot, so a credential switch, disconnect, or 404 replaces them too. | 245 | 249 | const publish = (next: typeof snapshot) => | 246 | | - managed.set({ statements: next.config?.experimental?.policies ?? [], organization: next.organization }) | | 250 | + managed.set({ | | 251 | + statements: [...(next.config?.experimental?.policies ?? []), ...(next.config?.policies ?? [])], | | 252 | + organization: next.organization, | | 253 | + }) | 247 | 254 |
| 248 | 255 | yield* ctx.integration.transform((editor) => { | 249 | 256 | editor.update("opencode", (integration) => { | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/test/config/config.test.ts +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -709,7 +709,7 @@ describe("Config", () => { | 709 | 709 | const project = path.join(tmp.path, "project") | 710 | 710 | const remote = (entries: readonly Entry[]) => ({ | 711 | 711 | provider: Config.latest(entries, "providers")?.remote?.package, | 712 | | - policies: Config.latest(entries, "experimental")?.policies, | | 712 | + policies: Config.latest(entries, "policies"), | 713 | 713 | }) | 714 | 714 | const loaded = { | 715 | 715 | provider: "aisdk:@ai-sdk/openai-compatible", | @@ -966,14 +966,14 @@ describe("Config", () => { | 966 | 966 | migrateV1({ | 967 | 967 | enabled_providers: ["anthropic", "openai"], | 968 | 968 | disabled_providers: ["openai"], | 969 | | - }).experimental?.policies, | | 969 | + }).policies, | 970 | 970 | ).toEqual([ | 971 | 971 | { action: "provider.use", resource: "*", effect: "deny" }, | 972 | 972 | { action: "provider.use", resource: "anthropic", effect: "allow" }, | 973 | 973 | { action: "provider.use", resource: "openai", effect: "allow" }, | 974 | 974 | { action: "provider.use", resource: "openai", effect: "deny" }, | 975 | 975 | ]) | 976 | | - expect(migrateV1({ enabled_providers: [] }).experimental?.policies).toEqual([ | | 976 | + expect(migrateV1({ enabled_providers: [] }).policies).toEqual([ | 977 | 977 | { action: "provider.use", resource: "*", effect: "deny" }, | 978 | 978 | ]) | 979 | 979 | }) | @@ -1031,7 +1031,7 @@ describe("Config", () => { | 1031 | 1031 | expect(migrated.model).toEqual({ providerID: "azure", model: "deployment" }) | 1032 | 1032 | expect(migrated.agents?.reviewer?.model).toEqual({ providerID: "google-vertex", model: "claude-sonnet" }) | 1033 | 1033 | expect(migrated.commands?.review?.model).toEqual({ providerID: "azure", model: "deployment" }) | 1034 | | - expect(migrated.experimental?.policies).toEqual([ | | 1034 | + expect(migrated.policies).toEqual([ | 1035 | 1035 | { action: "provider.use", resource: "*", effect: "deny" }, | 1036 | 1036 | { action: "provider.use", resource: "google-vertex", effect: "allow" }, | 1037 | 1037 | { action: "provider.use", resource: "azure", effect: "deny" }, | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/test/config/normalization.test.ts +32-18Lines changed: 32 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -44,6 +44,15 @@ function withoutEmptyCompatibilityContainers(input: Record<string, unknown>) { | 44 | 44 | delete compaction.keep | 45 | 45 | if (!originallyEmpty && !Object.keys(compaction).length) delete result.compaction | 46 | 46 | } | | 47 | + // Deprecated experimental policies normalize into top-level policies. | | 48 | + if (typeof result.experimental === "object" && result.experimental !== null && !Array.isArray(result.experimental)) { | | 49 | + const experimental = result.experimental as Record<string, unknown> | | 50 | + if (Array.isArray(experimental.policies)) { | | 51 | + result.policies = [...experimental.policies, ...(Array.isArray(result.policies) ? result.policies : [])] | | 52 | + delete experimental.policies | | 53 | + if (!Object.keys(experimental).length) delete result.experimental | | 54 | + } | | 55 | + } | 47 | 56 | return result | 48 | 57 | } | 49 | 58 |
| @@ -56,7 +65,7 @@ describe("ConfigNormalize", () => { | 56 | 65 | ] as const | 57 | 66 | const result = normalized({ experimental: { policies } }) | 58 | 67 | expect(result.diagnostics).toEqual([]) | 59 | | - expect(Schema.decodeUnknownSync(Info)(result.encoded).experimental?.policies).toEqual(policies) | | 68 | + expect(Schema.decodeUnknownSync(Info)(result.encoded).policies).toEqual(policies) | 60 | 69 | }) | 61 | 70 |
| 62 | 71 | test("drops unsupported policy actions without losing supported policies", () => { | @@ -70,7 +79,7 @@ describe("ConfigNormalize", () => { | 70 | 79 | ], | 71 | 80 | }, | 72 | 81 | }) | 73 | | - expect(Schema.decodeUnknownSync(Info)(result.encoded).experimental?.policies).toEqual([supported]) | | 82 | + expect(Schema.decodeUnknownSync(Info)(result.encoded).policies).toEqual([supported]) | 74 | 83 | expect(result.diagnostics.map((diagnostic) => diagnostic.path)).toEqual([ | 75 | 84 | ["experimental", "policies", "0"], | 76 | 85 | ["experimental", "policies", "2"], | @@ -467,42 +476,47 @@ describe("ConfigNormalize", () => { | 467 | 476 | }) | 468 | 477 |
| 469 | 478 | test("distinguishes empty, mixed, and wholly malformed enabled provider lists", () => { | 470 | | - expect(normalized({ enabled_providers: [] }).encoded.experimental).toEqual({ | 471 | | - policies: [{ action: "provider.use", resource: "*", effect: "deny" }], | 472 | | - }) | 473 | | - expect(normalized({ enabled_providers: [1, "anthropic", false] }).encoded.experimental).toEqual({ | 474 | | - policies: [ | 475 | | - { action: "provider.use", resource: "*", effect: "deny" }, | 476 | | - { action: "provider.use", resource: "anthropic", effect: "allow" }, | 477 | | - ], | 478 | | - }) | 479 | | - expect(normalized({ enabled_providers: [1, false] }).encoded.experimental).toBeUndefined() | 480 | | - expect(normalized({ enabled_providers: "anthropic" }).encoded.experimental).toBeUndefined() | | 479 | + expect(normalized({ enabled_providers: [] }).encoded.policies).toEqual([ | | 480 | + { action: "provider.use", resource: "*", effect: "deny" }, | | 481 | + ]) | | 482 | + expect(normalized({ enabled_providers: [1, "anthropic", false] }).encoded.policies).toEqual([ | | 483 | + { action: "provider.use", resource: "*", effect: "deny" }, | | 484 | + { action: "provider.use", resource: "anthropic", effect: "allow" }, | | 485 | + ]) | | 486 | + expect(normalized({ enabled_providers: [1, false] }).encoded.policies).toBeUndefined() | | 487 | + expect(normalized({ enabled_providers: "anthropic" }).encoded.policies).toBeUndefined() | 481 | 488 | }) | 482 | 489 |
| 483 | | - test("appends native policies after migrated provider policies", () => { | | 490 | + test("merges migrated, experimental, and native policies in order", () => { | 484 | 491 | expect( | 485 | 492 | normalized({ | 486 | 493 | enabled_providers: ["anthropic"], | 487 | 494 | disabled_providers: ["openai"], | | 495 | + policies: [{ action: "provider.use", resource: "native", effect: "deny" }], | 488 | 496 | experimental: { | 489 | 497 | portable_shell_scanner: true, | 490 | 498 | subagent_depth: 0, | 491 | 499 | policies: [{ action: "provider.use", resource: "custom", effect: "allow" }], | 492 | 500 | }, | 493 | | - }).encoded.experimental, | 494 | | - ).toEqual({ | 495 | | - portable_shell_scanner: true, | 496 | | - subagent_depth: 0, | | 501 | + }).encoded, | | 502 | + ).toMatchObject({ | | 503 | + experimental: { portable_shell_scanner: true, subagent_depth: 0 }, | 497 | 504 | policies: [ | 498 | 505 | { action: "provider.use", resource: "*", effect: "deny" }, | 499 | 506 | { action: "provider.use", resource: "anthropic", effect: "allow" }, | 500 | 507 | { action: "provider.use", resource: "openai", effect: "deny" }, | 501 | 508 | { action: "provider.use", resource: "custom", effect: "allow" }, | | 509 | + { action: "provider.use", resource: "native", effect: "deny" }, | 502 | 510 | ], | 503 | 511 | }) | 504 | 512 | }) | 505 | 513 |
| | 514 | + test("never emits experimental policies", () => { | | 515 | + expect( | | 516 | + normalized({ experimental: { policies: [{ action: "provider.use", resource: "*", effect: "deny" }] } }).encoded, | | 517 | + ).toEqual({ policies: [{ action: "provider.use", resource: "*", effect: "deny" }] }) | | 518 | + }) | | 519 | + | 506 | 520 | test("reports unsupported legacy settings without including their values", () => { | 507 | 521 | const secret = "do-not-log-this-value" | 508 | 522 | const result = normalized({ | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/test/config/policy.test.ts +1-2Lines changed: 1 addition & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,8 +18,7 @@ import { PluginTestLayer } from "../plugin/fixture" | 18 | 18 | const it = testEffect(PluginTestLayer) | 19 | 19 | const decode = Schema.decodeUnknownSync(Info) | 20 | 20 |
| 21 | | -const document = (...policies: ConfigPolicy.Info[]) => | 22 | | - new Document({ type: "document", info: decode({ experimental: { policies } }) }) | | 21 | +const document = (...policies: ConfigPolicy.Info[]) => new Document({ type: "document", info: decode({ policies }) }) | 23 | 22 | const provider = (effect: ConfigPolicy.Effect, resource: string): ConfigPolicy.Info => ({ | 24 | 23 | action: "provider.use", | 25 | 24 | resource, | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/test/mcp.test.ts +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -311,7 +311,7 @@ function resourceMcpLayer( | 311 | 311 | new Document({ | 312 | 312 | type: "document", | 313 | 313 | info: new Info({ | 314 | | - experimental: { policies: overrides?.policies ?? [] }, | | 314 | + policies: overrides?.policies ?? [], | 315 | 315 | mcp: new ConfigMCP.Info({ | 316 | 316 | servers: { | 317 | 317 | resources: | @@ -2635,7 +2635,7 @@ for (const source of ["config", "organization"] as const) { | 2635 | 2635 | mcp: new ConfigMCP.Info({ | 2636 | 2636 | servers: { resources: new ConfigMCP.Remote({ type: "remote", url: server.url, oauth: false }) }, | 2637 | 2637 | }), | 2638 | | - experimental: { policies: [{ action: "integration.use", resource: "mcp:*", effect }] }, | | 2638 | + policies: [{ action: "integration.use", resource: "mcp:*", effect }], | 2639 | 2639 | }), | 2640 | 2640 | }) | 2641 | 2641 | yield* test.setEntries([document("allow")]) | |
|