Skip to content

Commit e42d622

Browse files
authored
fix(core): make build metadata a required service so flags can't silently fall back to release defaults (#62686)
1 parent 61045f1 commit e42d622

39 files changed

Lines changed: 487 additions & 209 deletions

‎packages/core/src/app.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ export interface Info {
99
readonly channel: string
1010
}
1111

12-
export const Metadata = Context.Reference<Info>("@opencode/App", {
13-
defaultValue: () => make(),
14-
})
12+
export class Service extends Context.Service<Service, Info>()("@opencode/App") {}
1513

1614
export function make(input: Partial<Info> = {}): Info {
1715
return {
@@ -25,9 +23,9 @@ export function useragent(app: Info) {
2523
return `opencode/${app.channel}/${app.version}/${app.name}`
2624
}
2725

28-
export const layer = (input?: Partial<Info>) => Layer.succeed(Metadata, make(input))
26+
export const layer = (input?: Partial<Info>) => Layer.succeed(Service, make(input))
2927

3028
export const configured = (input?: Partial<Info>) =>
31-
makeGlobalNode({ service: Metadata, layer: layer(input), deps: [] })
29+
makeGlobalNode({ service: Service, layer: layer(input), deps: [] })
3230

3331
export const node = configured()

‎packages/core/src/artifact.ts‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ import type { SessionID } from "@opencode/schema/session-id"
1919
import { Global } from "@opencode/util/global"
2020
import { makeLocationNode } from "@opencode/util/effect/app-node"
2121
import { Clock, Context, Effect, Layer, Option, Schema } from "effect"
22-
import { App } from "./app.js"
2322
import { Bus } from "./bus.js"
24-
import { Config } from "./config.js"
2523
import { Flag } from "./flag.js"
2624
import { KV } from "./kv.js"
2725
import { Location } from "./location.js"
@@ -206,12 +204,11 @@ const layer = Layer.effect(
206204
Service,
207205
Effect.gen(function* () {
208206
const bus = yield* Bus.Service
209-
const config = yield* Config.Service
207+
const flags = yield* Flag.Service
210208
const global = yield* Global.Service
211209
const kv = yield* KV.Service
212210
const location = yield* Location.Service
213211
const sessions = yield* Session.Service
214-
const flags = yield* Flag.resolver
215212

216213
const rootDir = path.join(global.data, "artifacts")
217214
const itemDir = (id: ID) => path.join(rootDir, id)
@@ -266,10 +263,7 @@ const layer = Layer.effect(
266263
notify: State.changes(snapshotProviders, () => bus.publish(Event.Updated, {}).pipe(Effect.asVoid)),
267264
})
268265

269-
const isEnabled = Effect.fn("Artifact.enabled")(function* () {
270-
const entries = yield* config.entries()
271-
return flags(entries).artifacts.enabled
272-
})
266+
const isEnabled = Effect.fn("Artifact.enabled")(() => flags.enabled("artifacts"))
273267

274268
const requireEnabled = () =>
275269
isEnabled().pipe(Effect.flatMap((on) => (on ? Effect.void : Effect.fail(new DisabledError()))))
@@ -458,7 +452,7 @@ const layer = Layer.effect(
458452
export const node = makeLocationNode({
459453
service: Service,
460454
layer,
461-
deps: [App.node, Bus.node, Config.node, Global.node, KV.node, Location.node, Session.node],
455+
deps: [Bus.node, Flag.node, Global.node, KV.node, Location.node, Session.node],
462456
})
463457

464458
function encodeStringFiles(files: Readonly<Record<string, string>>) {

‎packages/core/src/codemode/tool.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export const create = (
8686
inventory: Inventory,
8787
executeTool: (name: string, tool: Info, input: unknown, context: ToolContext) => Effect.Effect<Result, Error>,
8888
permission: Permission.Interface,
89+
flag: Flag.Interface,
8990
) => {
9091
return {
9192
name: "execute",
@@ -119,7 +120,7 @@ export const create = (
119120
yield* Effect.sleep(wait).pipe(Effect.andThen(lock.withPermit(publish)), Effect.forkIn(scope))
120121
}),
121122
)
122-
const result = yield* runtime(inventory, (yield* Flag.enabled("codemode_vm")) ? "vm" : "legacy")
123+
const result = yield* runtime(inventory, (yield* flag.enabled("codemode_vm")) ? "vm" : "legacy")
123124
.execute(code)
124125
.pipe(
125126
Effect.provideService(Run, {
@@ -295,8 +296,9 @@ export const check = (
295296
bindings: Readonly<Record<string, CodeMode.DataValue>>,
296297
limits: CodeMode.ExecutionLimits,
297298
executeTool: (name: string, tool: Info, input: unknown) => Effect.Effect<Result, Error>,
299+
flag: Flag.Interface,
298300
) =>
299-
Flag.enabled("codemode_vm").pipe(
301+
flag.enabled("codemode_vm").pipe(
300302
Effect.flatMap((vm) =>
301303
CodeMode.make({
302304
tools: inventoryTools(inventory),

‎packages/core/src/config/plugin/compaction.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ export const Plugin = define({
1212
effect: Effect.fn(function* (ctx) {
1313
const config = yield* Config.Service
1414
const compaction = yield* SessionCompaction.Service
15-
const flags = yield* Flag.resolver
15+
const flags = yield* Flag.Service
1616
const loaded = yield* ConfigEntryObserver.observe(config, ctx.event, compaction.reload())
1717
yield* compaction.transform((editor) => {
18-
editor.configure({ precompute: flags(loaded.entries).background_compaction.enabled })
18+
editor.configure({ precompute: flags.resolve(loaded.entries).background_compaction.enabled })
1919
for (const entry of loaded.entries) {
2020
if (entry.type !== "document" || !entry.info.compaction) continue
2121
editor.configure({

‎packages/core/src/flag.ts‎

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * as Flag from "./flag.js"
22

3-
import { Context, Effect, Option, Record } from "effect"
3+
import { Context, Effect, Layer, Record } from "effect"
4+
import { makeLocationNode } from "@opencode/util/effect/app-node"
45
import { ConfigFlag } from "@opencode/schema/config/flag"
56
import type { Document, Entry } from "@opencode/schema/config"
67
import { App } from "./app.js"
@@ -14,6 +15,14 @@ export interface State {
1415
readonly source: "env" | "config" | "default"
1516
}
1617

18+
export interface Interface {
19+
readonly resolve: (entries: readonly Entry[]) => Record<Key, State>
20+
readonly all: Effect.Effect<Record<Key, State>>
21+
readonly enabled: (key: Key) => Effect.Effect<boolean>
22+
}
23+
24+
export class Service extends Context.Service<Service, Interface>()("@opencode/Flag") {}
25+
1726
/**
1827
* `OPENCODE_FLAGS`, a comma-separated list such as `schedules,-media_blob`; a leading `-` turns a flag off, and
1928
* `all` turns every flag on unless the same list opts one out, as in `all,-media_blob`.
@@ -63,19 +72,45 @@ export function resolve(input: {
6372
})
6473
}
6574

66-
/** {@link resolve} for this process's channel and `OPENCODE_FLAGS`, for synchronous readers of config entries. */
67-
export const resolver = Effect.gen(function* () {
68-
const app = yield* App.Metadata
69-
const overrides = yield* Overrides
70-
return (entries: readonly Entry[]) => resolve({ entries, channel: app.channel, overrides })
71-
})
75+
export const layer = Layer.effect(
76+
Service,
77+
Effect.gen(function* () {
78+
const app = yield* App.Service
79+
const config = yield* Config.Service
80+
const overrides = yield* Overrides
81+
const resolveEntries = (entries: readonly Entry[]) => resolve({ entries, channel: app.channel, overrides })
82+
const all = Effect.gen(function* () {
83+
return resolveEntries(yield* config.entries())
84+
})
85+
return Service.of({
86+
resolve: resolveEntries,
87+
all,
88+
enabled: (key) => Effect.map(all, (flags) => flags[key].enabled),
89+
})
90+
}),
91+
)
7292

73-
/** Every flag with its value and where it came from, read from the Config in scope on each call. */
74-
export const all = Effect.gen(function* () {
75-
const config = yield* Effect.serviceOption(Config.Service)
76-
const entries = Option.isSome(config) ? yield* config.value.entries() : []
77-
const read = yield* resolver
78-
return read(entries)
93+
export const testLayer = (
94+
entries: Entry[] = [],
95+
options?: { readonly app?: Partial<App.Info>; readonly overrides?: string },
96+
) =>
97+
Layer.fresh(layer).pipe(
98+
Layer.provideMerge(
99+
Layer.mergeAll(
100+
App.layer(options?.app),
101+
Config.testLayer(entries),
102+
Layer.succeed(Overrides, options?.overrides),
103+
),
104+
),
105+
)
106+
107+
export const node = makeLocationNode({
108+
service: Service,
109+
layer,
110+
deps: [App.node, Config.node],
79111
})
80112

81-
export const enabled = (key: Key) => all.pipe(Effect.map((flags) => flags[key].enabled))
113+
/** Every flag with its value and where it came from, read from the location's Config on each call. */
114+
export const all = Service.use((flags) => flags.all)
115+
116+
export const enabled = (key: Key) => Service.use((flags) => flags.enabled(key))

‎packages/core/src/instance.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { Context, Effect, Layer } from "effect"
22
import { Agent } from "./agent.js"
3+
import { App } from "./app.js"
34
import { Artifact } from "./artifact.js"
45
import { AISDK } from "./aisdk.js"
56
import { Model } from "./model.js"
67
import { Provider } from "./provider.js"
78
import { Command } from "./command.js"
89
import { Config } from "./config.js"
10+
import { Flag } from "./flag.js"
911
import { LayerNode } from "@opencode/util/effect/layer-node"
1012
import { Node } from "@opencode/util/effect/app-node"
1113
import { FileMutation } from "./file-mutation.js"
@@ -60,10 +62,12 @@ export * as Instance from "./instance.js"
6062
export { Service, node, type Interface } from "./instance/service.js"
6163

6264
const nodes = [
65+
App.node,
6366
Location.node,
6467
LocationLifecycle.node,
6568
Environment.node,
6669
Config.node,
70+
Flag.node,
6771
Agent.node,
6872
Artifact.node,
6973
Command.node,

‎packages/core/src/models-dev.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ export const layer = (options?: Options) =>
533533
Effect.gen(function* () {
534534
const fs = yield* FSUtil.Service
535535
const bus = yield* Bus.Service
536-
const app = yield* App.Metadata
536+
const app = yield* App.Service
537537
const kv = yield* KV.Service
538538
const http = (yield* HttpClient.HttpClient).pipe(
539539
HttpClient.retryTransient({

‎packages/core/src/plugin/host.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const isRpcEvent = (event: Event.Payload): event is RpcEvent => isRpcType(event.
5151
const isPublicEvent = (event: Event.Payload): event is EventManifest.ServerEvent | RpcEvent =>
5252
EventManifest.isServer(event) || isRpcEvent(event)
5353
export const make = Effect.fn("PluginHost.make")(function* (plugin: Pick<Interface, "list">, pluginID: string) {
54-
const app = yield* App.Metadata
54+
const app = yield* App.Service
5555
const agents = yield* Agent.Service
5656
const artifacts = yield* Artifact.Service
5757
const aisdk = yield* AISDK.Service

‎packages/core/src/plugin/internal.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { Environment } from "../environment/index.js"
4242
import { FileAccess } from "../file-access.js"
4343
import { FileMutation } from "../file-mutation.js"
4444
import { FileWrite } from "../file-write.js"
45+
import { Flag } from "../flag.js"
4546
import { Formatter } from "../formatter.js"
4647
import { Generate } from "../generate.js"
4748
import { Form } from "../form.js"
@@ -120,7 +121,7 @@ import { WellKnownPlugin } from "../wellknown/plugin.js"
120121
const services = [
121122
Agent.Service,
122123
// Activation clears the ambient context, so built-ins see the configured build channel only through this.
123-
App.Metadata,
124+
App.Service,
124125
FetchHttpClient.Fetch,
125126
Artifact.Service,
126127
AppProcess.Service,
@@ -134,6 +135,7 @@ const services = [
134135
FileAccess.Service,
135136
FileMutation.Service,
136137
FileWrite.Service,
138+
Flag.Service,
137139
Formatter.Service,
138140
Generate.Service,
139141
LocationWatcherPolicy.Service,
@@ -193,6 +195,7 @@ export const requirements = LayerNode.group([
193195
FileAccess.node,
194196
FileMutation.node,
195197
FileWrite.node,
198+
Flag.node,
196199
Formatter.node,
197200
Generate.node,
198201
LocationWatcherPolicy.node,

‎packages/core/src/schedule.ts‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
toCreateInput,
2121
When,
2222
} from "@opencode/schema/schedule"
23-
import { App } from "./app.js"
2423
import { Config } from "./config.js"
2524
import { Database } from "./database/database.js"
2625
import { FileSystem } from "./filesystem.js"
@@ -214,16 +213,15 @@ const layer = Layer.effect(
214213
const instances = yield* Instance.Service
215214
const execution = yield* SessionExecution.Service
216215
const doorbell = yield* Latch.make()
217-
const flags = yield* Flag.resolver
218216

219217
// Config is per location, so a schedule follows the config where its Session lives.
220218
const settings = (session: SessionSchema.Info) =>
221219
Effect.gen(function* () {
222-
const config = yield* Effect.serviceOption(Config.Service)
223-
if (Option.isNone(config)) return { enabled: false, timezone: undefined }
224-
const entries = yield* config.value.entries()
220+
const config = yield* Config.Service
221+
const flags = yield* Flag.Service
222+
const entries = yield* config.entries()
225223
return {
226-
enabled: flags(entries).schedules.enabled,
224+
enabled: flags.resolve(entries).schedules.enabled,
227225
timezone: Config.latest(entries, "timezone"),
228226
}
229227
}).pipe(instances.provide(session))
@@ -830,5 +828,5 @@ const layer = Layer.effect(
830828
export const node: LayerNode.Provider<Service, never, typeof Node.tags.values.global> = makeGlobalNode({
831829
service: Service,
832830
layer,
833-
deps: [App.node, Database.node, Session.node, SessionExecution.node, Instance.node],
831+
deps: [Database.node, Session.node, SessionExecution.node, Instance.node],
834832
})

0 commit comments

Comments
 (0)