export * as Config from "./config.js" import { makeLocationNode } from "@opencode/util/effect/app-node" import path from "path" import { isDeepStrictEqual } from "node:util" import { applyEdits, modify, type ParseError, parse } from "jsonc-parser" import { Context, Effect, FiberMap, Layer, Option, PubSub, Ref, Schema, Semaphore, Stream } from "effect" import { Directory, Document, Info, type Patch, type Entry, Event } from "@opencode/schema/config" import { Credential } from "./credential.js" import { Bus } from "./bus.js" import { Watcher } from "./filesystem/watcher.js" import { FSUtil } from "@opencode/util/fs-util" import { Global } from "@opencode/util/global" import { Location } from "./location.js" import { AbsolutePath } from "./schema.js" import { ConfigVariable } from "./config/variable.js" import { ConfigNormalize } from "./config/normalize.js" import { ConfigDiscovery } from "./config/discovery.js" import { ConfigWatch } from "./config/watch.js" import { ConfigIntegration } from "./config/integration.js" import { ConfigManaged } from "./config/managed.js" import { ManagedPolicy } from "./managed-policy.js" import { Connection } from "@opencode/schema/connection" import { WellKnown } from "./wellknown.js" import { AppProcess } from "@opencode/util/process" import { ChildProcess } from "effect/process" export function latest(entries: readonly Entry[], key: K): Info[K] | undefined { return entries.findLast((entry): entry is Document => entry.type === "document" && entry.info[key] !== undefined) ?.info[key] } export interface Interface { /** Returns location config documents and discovery sources from lowest to highest priority. */ readonly entries: () => Effect.Effect /** Compatibility roots consumed by internal compatibility plugins. */ readonly compatibility?: () => Effect.Effect<{ readonly claude: readonly AbsolutePath[] readonly agents: readonly AbsolutePath[] }> /** * Streams raw filesystem updates under config roots. Config owns root * topology and watch reconciliation; domain owners filter this feed for the * source files they parse and rebuild their own state. */ readonly changes: () => Stream.Stream /** Updates supported global config fields while preserving unrelated JSONC content. */ readonly update?: (patch: Patch) => Effect.Effect /** * Replaces, or clears with `undefined`, the document an integration connection contributes. It loads right after * managed policies: its settings layer below remote, user, and project config, while its policies outrank theirs. * Changing it reloads config like any other source. */ readonly integration?: ( integrationID: ConfigIntegration.Document["integrationID"], document: ConfigIntegration.Document | undefined, ) => Effect.Effect } export const Options = Schema.Struct({ project: Schema.optional(Schema.Boolean), // false skips the system and global config dirs, ~/.claude, and ~/.agents; // wellknown, file, and content entries still load. global: Schema.optional(Schema.Boolean), system: Schema.optional(Schema.Union([Schema.Boolean, Schema.String])), // false skips ~/.claude and project .claude dirs. claude: Schema.optional(Schema.Boolean), file: Schema.optional(Schema.String), content: Schema.optional(Schema.String), }) export type Options = typeof Options.Type export class Service extends Context.Service()("@opencode/Config") {} export interface TestInterface extends Interface { /** Replaces the entries returned by subsequent entries() calls. */ readonly setEntries: (entries: Entry[]) => Effect.Effect /** Emits one filesystem update to every changes() subscriber. */ readonly emitChange: (update: Watcher.Update) => Effect.Effect } export class Test extends Context.Service()("@opencode/Config/Test") {} /** In-memory config for tests: static entries with replaceable state and a test-driven change feed. */ export const testLayer = ( initial: Entry[] = [], compatibility: { readonly claude: readonly AbsolutePath[]; readonly agents: readonly AbsolutePath[] } = { claude: [], agents: [], }, ) => Layer.effectContext( Effect.gen(function* () { const entries = yield* Ref.make(initial) const integrations = yield* Ref.make(new Map()) const updates = yield* PubSub.unbounded() const service = Test.of({ entries: () => Effect.all([Ref.get(integrations), Ref.get(entries)]).pipe( Effect.map(([documents, entries]) => [...documents.values(), ...entries]), ), compatibility: () => Effect.succeed(compatibility), changes: () => Stream.fromPubSub(updates), setEntries: (next) => Ref.set(entries, next), emitChange: (update) => PubSub.publish(updates, update).pipe(Effect.asVoid), integration: (integrationID, document) => Ref.update(integrations, (current) => { // Like config, keeps the document it holds when handed a deep-equal one. if (isDeepStrictEqual(current.get(integrationID), document)) return current const next = new Map(current) next.delete(integrationID) return document ? next.set(integrationID, document) : next }), }) return Context.empty().pipe(Context.add(Service, service), Context.add(Test, service)) }), ) export const layer = (options?: Options) => Layer.effect( Service, Effect.gen(function* () { const fs = yield* FSUtil.Service const location = yield* Location.Service const watcher = yield* Watcher.Service const bus = yield* Bus.Service const credentials = yield* Credential.Service const globalService = yield* Global.Service const wellknown = yield* WellKnown.Service const processes = yield* AppProcess.Service const integrations = new Map() // Integrations set their documents from outside this layer, so their reloads run with its services. const context = yield* Effect.context() const reloadLock = Semaphore.makeUnsafe(1) const updateLock = Semaphore.makeUnsafe(1) const decodeOptions = { errors: "all", onExcessProperty: "ignore" } as const const decodeInfo = Schema.decodeUnknownOption(Info, decodeOptions) const parseInfo = Effect.fn("Config.parseInfo")(function* (text: string, source: string) { const errors: ParseError[] = [] const input: unknown = parse(text, errors, { allowTrailingComma: true }) if (errors.length) { yield* Effect.logWarning("configuration normalization diagnostic", { source, path: "$", kind: "invalid", action: "rejected malformed JSON or JSONC document", }) return } const result = ConfigNormalize.normalize(input) // Every reload repeats the routine notes on a config that loads. A rejected config or a dropped malformed // setting is the user's to fix, so those stay warnings. yield* Effect.forEach(result.diagnostics, (diagnostic) => { const log = result.type === "rejected" || diagnostic.kind === "invalid" ? Effect.logWarning : Effect.logDebug return log("configuration normalization diagnostic", { source, path: diagnostic.path[0] === "$" ? "$" : `$.${diagnostic.path.join(".")}`, kind: diagnostic.kind, action: diagnostic.message, }) }) if (result.type === "rejected") return const info = Option.getOrUndefined(decodeInfo(result.encoded)) if (info) return info yield* Effect.logWarning("configuration normalization diagnostic", { source, path: "$", kind: "invalid", action: "rejected canonical configuration after final validation", }) }) const loadFile = Effect.fnUntraced(function* (filepath: string, references: Set) { const text = yield* fs.readFileStringSafe(filepath) if (text === undefined) return references.add(filepath) const substituted = yield* ConfigVariable.substitute({ type: "path", path: filepath, text, references }) const info = yield* parseInfo(substituted, filepath) if (!info) return return new Document({ type: "document", path: AbsolutePath.make(filepath), info }) }) const loadWellknownEntry = Effect.fnUntraced(function* (entry: WellKnown.Entry, references: Set) { const auth = entry.manifest.auth const credential = (yield* credentials.list(entry.integrationID)).at(-1) const variables = auth && credential?.value.type === "key" ? { [auth.env]: credential.value.key } : undefined // Logging out drops the saved remote config, so the source supplies nothing until a fresh fetch succeeds. if (!variables) return yield* wellknown.clearCached(entry.origin).pipe(Effect.as([])) // A failed refresh keeps the last resolved config so remote providers and allowlists do not disappear. const configs = yield* wellknown.resolve(entry, variables).pipe( Effect.catch((error) => Effect.logWarning("failed to load wellknown config", { source: entry.origin, error }).pipe( Effect.andThen(wellknown.cached(entry.origin)), ), ), ) return yield* Effect.forEach(configs, (config) => ConfigVariable.substitute({ type: "virtual", source: entry.origin, dir: entry.origin, text: JSON.stringify(config), env: variables, references, }).pipe( Effect.flatMap((text) => parseInfo(text, entry.origin)), Effect.map((info) => info ? new ConfigIntegration.WellKnownDocument({ type: "document", info, integrationID: entry.integrationID, }) : undefined, ), ), ).pipe(Effect.map((documents) => documents.filter((document) => document !== undefined))) }) const loadWellknown = Effect.fn("Config.loadWellknown")(function* (references: Set) { const entries = yield* wellknown .entries() .pipe( Effect.catch((error) => Effect.logWarning("failed to discover wellknown config", { error }).pipe(Effect.as([] as const)), ), ) return yield* Effect.forEach(entries, (entry) => loadWellknownEntry(entry, references).pipe( Effect.map((documents) => ({ integrationID: entry.integrationID, documents })), ), ) }) // `integration.use` decides whether an integration may supply anything. Only managed policies can withhold the // documents an integration delivers, so a project file can never strip the Console document carrying org policy. // A withheld document keeps its policies, so denying an integration never lifts the restrictions it carries. const supplies = Effect.fnUntraced(function* ( lockdown: ReturnType, integrationID: string, connection?: Connection.Info, ) { const stored = connection?.type === "credential" ? yield* credentials.get(Credential.ID.make(connection.id)) : undefined const resource = ManagedPolicy.integrationResource( integrationID, stored ? ManagedPolicy.account(stored.value) : undefined, ) return ManagedPolicy.decision(lockdown, "integration.use", resource) !== "deny" }) const loadPreferences = Effect.fn("Config.loadPreferences")(function* ( plist: AbsolutePath, references: Set, ) { const result = yield* processes .run(ChildProcess.make("plutil", ["-convert", "json", "-o", "-", plist], { stdin: "ignore" }), { timeout: "10 seconds", }) .pipe(Effect.flatMap(AppProcess.requireSuccess), Effect.option) if (Option.isNone(result)) { yield* Effect.logWarning("failed to convert managed preferences", { source: plist }) return [] } references.add(plist) const text = yield* ConfigVariable.substitute({ type: "virtual", source: plist, dir: path.dirname(plist), text: ConfigDiscovery.stripPreferenceMetadata(result.value.stdout.toString()), references, }) const info = yield* parseInfo(text, plist) return info ? [new Document({ type: "document", path: plist, info })] : [] }) const loadDirectory = Effect.fnUntraced(function* (directory: AbsolutePath, references: Set) { return [ ...(yield* Effect.forEach(ConfigDiscovery.names, (file) => loadFile(path.join(directory, file), references), ).pipe(Effect.map((configs) => configs.filter((config): config is Document => config !== undefined)))), new Directory({ type: "directory", path: directory }), ] }) const load = Effect.fn("Config.load")(function* (sources: ConfigDiscovery.Sources) { const references = new Set() const direct = yield* Effect.forEach(sources.direct, (filepath) => loadFile(filepath, references)).pipe( Effect.orDie, Effect.map((entries) => entries.filter((entry): entry is Document => entry !== undefined)), ) const explicit = sources.explicit ? yield* loadFile(sources.explicit, references).pipe( Effect.map((config) => (config ? [config] : [])), Effect.orDie, ) : [] const content = options?.content !== undefined ? yield* ConfigVariable.substitute({ type: "virtual", source: "OPENCODE_CONFIG_CONTENT", dir: location.directory, text: options.content, references, }).pipe( Effect.flatMap((text) => parseInfo(text, "OPENCODE_CONFIG_CONTENT")), Effect.map((info) => (info ? [new Document({ type: "document", info })] : [])), Effect.orDie, ) : [] // Managed settings load last so they override everything, and MDM // preferences outrank the system directory as in V1. Policies evaluate // in reverse load order, so managed policies split off and load first // to keep final file-based authority. const managed = [ ...(sources.system?.present ? yield* loadDirectory(sources.system.path, references).pipe(Effect.orDie) : []), ...(sources.preferences ? yield* loadPreferences(sources.preferences, references).pipe(Effect.orDie) : []), ] const managedPolicies = managed .flatMap((entry) => entry.type === "document" && entry.info.policies ? [ new ConfigManaged.Document({ type: "document", path: entry.path, info: new Info({ policies: entry.info.policies }), }), ] : [], ) .toReversed() const managedSettings = managed.map((entry) => entry.type === "document" ? new ConfigManaged.Document({ ...entry, info: new Info({ ...entry.info, policies: undefined }) }) : entry, ) // Integration documents load right after managed policies: every remote, user, and project layer overrides // their settings, and since policies evaluate earliest-loaded first, only managed policies outrank theirs. // Global entries sit below explicit and direct files; project directories rank above them. const globalSupplementary = sources.global ? yield* loadDirectory(sources.global, references).pipe(Effect.orDie) : [] const projectSupplementary = yield* Effect.forEach( sources.project.filter((root) => root.present), (root) => loadDirectory(root.path, references), ).pipe( Effect.orDie, Effect.map((entries) => entries.flat()), ) const lockdown = ManagedPolicy.statements(managedPolicies) const integrated = yield* Effect.forEach(integrations.values(), (document) => supplies(lockdown, document.integrationID, document.connection).pipe( Effect.map((allowed) => allowed ? document : new ConfigIntegration.Document({ ...document, info: new Info({ policies: document.info.policies }) }), ), ), ) const wellknownDocuments = yield* Effect.forEach(yield* loadWellknown(references).pipe(Effect.orDie), (entry) => supplies(lockdown, entry.integrationID).pipe( Effect.map((allowed) => allowed ? entry.documents : entry.documents.map( (document) => new ConfigIntegration.WellKnownDocument({ ...document, info: new Info({ policies: document.info.policies }), }), ), ), ), ) return { configs: [ ...managedPolicies, ...integrated, ...wellknownDocuments.flat(), ...globalSupplementary, ...explicit, ...direct, ...projectSupplementary, ...content, ...managedSettings, ], isInput: yield* ConfigWatch.inputs(sources, references), } }) const initial = yield* ConfigDiscovery.discover(options) let sources = initial let { configs, isInput } = yield* load(initial) const updates = yield* PubSub.unbounded() const reloads = yield* PubSub.sliding(1) // Readiness rescans recover writes made before a watch attached. const requestReload = PubSub.publish(reloads, undefined).pipe(Effect.asVoid) const watched = yield* FiberMap.make() const reconcile = Effect.fn("Config.reconcileWatches")(function* (sources: ConfigDiscovery.Sources) { const plan = ConfigWatch.plan(sources) for (const key of Array.from(watched, ([key]) => key)) { if (!plan.has(key)) yield* FiberMap.remove(watched, key) } for (const [key, target] of plan) { let real = target.path const ready = Effect.gen(function* () { real = yield* fs.resolve(target.path) yield* requestReload }) yield* watcher.subscribe(target, ready).pipe( Effect.flatMap( Stream.runForEach((update) => Effect.gen(function* () { yield* PubSub.publish(updates, update) if (isInput(update.path)) return yield* requestReload // Native watches can report the canonical path even after the root was deleted. if (real === target.path || !FSUtil.contains(real, update.path)) return if (isInput(path.join(target.path, path.relative(real, update.path)))) yield* requestReload }), ), ), FiberMap.run(watched, key, { onlyIfMissing: true, startImmediately: true }), ) } }) const reload = Effect.fn("Config.reload")( function* () { const discovered = yield* ConfigDiscovery.discover(options) const next = yield* load(discovered) isInput = next.isInput yield* reconcile(discovered) const compatibilityChanged = !isDeepStrictEqual(sources.claude, discovered.claude) || !isDeepStrictEqual(sources.agents, discovered.agents) if (isDeepStrictEqual(configs, next.configs) && !compatibilityChanged) return sources = discovered configs = next.configs yield* bus.publish(Event.Updated, {}) }, (effect) => reloadLock.withPermit(effect), ) // Subscribe eagerly so synchronous watch readiness isn't dropped. const pendingReloads = yield* PubSub.subscribe(reloads) yield* Stream.fromSubscription(pendingReloads).pipe( Stream.debounce("100 millis"), Stream.runForEach(() => reload().pipe(Effect.catchCause((cause) => Effect.logError("failed to reload config", { cause }))), ), Effect.forkScoped({ startImmediately: true }), ) yield* bus.subscribe(Credential.Event.Switched).pipe( Stream.filterEffect((event) => wellknown.entries().pipe( Effect.map((entries) => entries.some((entry) => entry.integrationID === event.data.integrationID)), Effect.orElseSucceed(() => false), ), ), Stream.runForEach(() => reload().pipe(Effect.catchCause((cause) => Effect.logError("failed to reload wellknown config", { cause }))), ), Effect.forkScoped({ startImmediately: true }), ) yield* bus.subscribe(WellKnown.Event.Updated).pipe( Stream.runForEach(() => reload().pipe(Effect.catchCause((cause) => Effect.logError("failed to reload wellknown sources", { cause }))), ), Effect.forkScoped({ startImmediately: true }), ) yield* Effect.sleep("10 minutes").pipe( Effect.andThen( Effect.suspend(() => { if (!wellknown.snapshot().length) return Effect.void return Effect.gen(function* () { const changed = yield* wellknown .refresh() .pipe( Effect.catch((error) => Effect.logWarning("failed to refresh wellknown manifests", { error }).pipe(Effect.as(false)), ), ) if (!changed) yield* reload() }).pipe(Effect.catchCause((cause) => Effect.logWarning("failed to refresh wellknown config", { cause }))) }), ), Effect.forever, Effect.forkScoped({ startImmediately: true }), ) yield* reloadLock.withPermit(reconcile(initial)) const update = Effect.fn("Config.update")( function* (patch: Patch) { const directory = initial.global ?? AbsolutePath.make(globalService.config) const candidates = ConfigDiscovery.names.map((name) => path.join(directory, name)) const filepath = (yield* Effect.filter(candidates, fs.isFile)).at(-1) ?? path.join(directory, "opencode.jsonc") const text = (yield* fs.readFileStringSafe(filepath)) ?? "{}\n" const updated = yield* Effect.try({ try: () => applyEdits( text, modify(text, ["shell"], patch.shell ?? undefined, { formattingOptions: { tabSize: 2, insertSpaces: true }, }), ), catch: (cause) => new FSUtil.FileSystemError({ method: "config.update", cause }), }) yield* fs.writeWithDirs(filepath, updated.endsWith("\n") ? updated : `${updated}\n`) yield* requestReload }, (effect) => updateLock.withPermit(effect), ) return Service.of({ entries: Effect.fnUntraced(function* () { return configs }), compatibility: () => Effect.all({ claude: Effect.filter(sources.claude, fs.isDir), agents: Effect.filter(sources.agents, fs.isDir), }), changes: () => Stream.fromPubSub(updates), update, integration: Effect.fn("Config.integration")(function* (integrationID, document) { if (isDeepStrictEqual(integrations.get(integrationID), document)) return integrations.delete(integrationID) if (document) integrations.set(integrationID, document) yield* reload().pipe( Effect.provide(context), Effect.catchCause((cause) => Effect.logError("failed to reload config", { cause })), ) }), }) }), ) export function configured(options?: Options) { return makeLocationNode({ service: Service, layer: layer(options), deps: [ Watcher.node, Bus.node, FSUtil.node, Global.node, Location.node, Credential.node, WellKnown.node, AppProcess.node, ], }) } export const node = configured()