Skip to content

Commit 224e7a5

Browse files
sahrizviclaude
andcommitted
fix(flag): close the read paths codex found: storage db, upgrade, scan census, provider nodes
Codex review of #1341 (gpt-5.6-sol). - `storage/db.ts` read `OPENCODE_DB` directly while core's `Flag.OPENCODE_DB` now aliases `ALTIMATE_CLI_DB`: one process could open two databases. Both read through `env` now - `cli/upgrade.ts` OR-ed the two names, so a documented `false` lost to a fallback `true` on the real update path; `project-scan`'s feature census read raw `OPENCODE_*` and under-reported documented names - Core's Effect `Config` flags used `Config.orElse`, which swallows a parse failure: a set-but-invalid documented value silently took the OPENCODE_ one. The documented value is read as a string and judged by `truthy`'s rule, so invalid is `false` like everywhere else - The provider wrap accepted a Record node (`fromEnv` answers a prefix path with one) as a documented value; only a set, non-empty scalar counts - `ALTIMATE_CLI_YOLO=""` counts as unset in both flag modules - Tests: `config.ts` really loads `ALTIMATE_CLI_CONFIG_CONTENT` (and it wins over the OPENCODE_ one); upgrade precedence; a non-OPENCODE Config key and a prefix-only documented name through the wrap; invalid/empty documented Effect flag; the DB alias; empty YOLO Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_012Q51zFUmPg1WwtS5CrGJE6
1 parent 6413dbe commit 224e7a5

9 files changed

Lines changed: 105 additions & 23 deletions

File tree

‎packages/core/src/flag/flag.ts‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Config } from "effect"
1+
import { Config, Option } from "effect"
22

33
// altimate_change start — every OPENCODE_* variable is documented under its ALTIMATE_CLI_*
44
// name (docs/docs/usage/cli.md), so the documented spelling is read first and the OPENCODE_
@@ -26,13 +26,22 @@ export function truthy(key: string) {
2626
}
2727

2828
/** An Effect `Config` boolean with the same documented-name-first rule, for the flags
29-
* that are resolved through the ambient ConfigProvider rather than `process.env`. */
29+
* that are resolved through the ambient ConfigProvider rather than `process.env`. The
30+
* documented value is read as a string and judged by `truthy`'s rule, so a set-but-invalid
31+
* documented value is `false` — not a fallback to the OPENCODE_ one, which `Config.orElse`
32+
* (and `Config.option`) would silently do, since both swallow parse failures. */
3033
function bool(key: string) {
3134
const alias = documentedAlias(key)
3235
const fallback = Config.boolean(key).pipe(Config.withDefault(false))
33-
return alias === undefined
34-
? fallback
35-
: Config.boolean(alias).pipe(Config.orElse(() => fallback))
36+
if (alias === undefined) return fallback
37+
return Config.all({ documented: Config.string(alias).pipe(Config.option), fallback }).pipe(
38+
Config.map(({ documented, fallback }) => {
39+
const value = Option.getOrUndefined(documented)
40+
if (value === undefined || value === "") return fallback
41+
const lower = value.toLowerCase()
42+
return lower === "true" || lower === "1"
43+
}),
44+
)
3645
}
3746
// altimate_change end
3847

@@ -178,8 +187,9 @@ export const Flag = {
178187
)
179188
},
180189
get ALTIMATE_CLI_YOLO() {
190+
// Empty counts as unset, as everywhere else in this file.
181191
const alt = process.env["ALTIMATE_CLI_YOLO"]
182-
if (alt !== undefined) {
192+
if (alt !== undefined && alt !== "") {
183193
const v = alt.toLowerCase()
184194
return v === "true" || v === "1"
185195
}

‎packages/opencode/src/altimate/tools/project-scan.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import { Telemetry } from "@/telemetry"
88
import * as OnboardingTelemetry from "@/altimate/telemetry/onboarding"
99
import { Config } from "@/config/config"
1010
import { Skill } from "../../skill"
11+
// altimate_change start — documented ALTIMATE_CLI_ name read first (see core `truthy`)
12+
import { truthy as FlagEnvTruthy } from "@opencode-ai/core/flag/flag"
13+
// altimate_change end
1114

1215
// --- Types ---
1316

@@ -869,10 +872,8 @@ export const ProjectScanTool = Tool.define("project_scan", {
869872
// Upstream's Effect-logging migration removed these keys from the Flag namespace
870873
// (and turned FILEWATCHER into an Effect Config<boolean>). For census we only need
871874
// "was the env var set", so read process.env directly.
872-
const flagSet = (key: string) => {
873-
const v = process.env[key]
874-
return v === "true" || v === "1"
875-
}
875+
// Through core's `truthy`, so the documented ALTIMATE_CLI_ name is counted too.
876+
const flagSet = (key: string) => FlagEnvTruthy(key)
876877
const enabledFlags: string[] = []
877878
if (flagSet("OPENCODE_EXPERIMENTAL")) enabledFlags.push("experimental")
878879
if (flagSet("OPENCODE_EXPERIMENTAL_PLAN_MODE")) enabledFlags.push("plan_mode")

‎packages/opencode/src/cli/upgrade.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { Flag } from "@opencode-ai/core/flag/flag"
44
import { Installation } from "@/installation"
55
import { InstallationVersion, InstallationChannel, isPublishableChannel } from "@opencode-ai/core/installation/version"
66
import { GlobalBus } from "@/bus/global"
7+
// altimate_change start — documented ALTIMATE_CLI_ name read first (see core `truthy`)
8+
import { truthy as FlagEnvTruthy } from "@opencode-ai/core/flag/flag"
9+
// altimate_change end
710
// altimate_change start — re-export the centralized channel guard so existing importers
811
// (cli/cmd/upgrade.ts, installation/upgrade.test.ts) keep resolving it from here.
912
export { isPublishableChannel }
@@ -85,13 +88,10 @@ export function isValidVersion(version: string): boolean {
8588
// altimate_change end
8689

8790
// altimate_change start — upstream_fix: honor both fork and upstream autoupdate-disable env vars
88-
function truthyEnv(name: string) {
89-
const value = process.env[name]?.toLowerCase()
90-
return value === "true" || value === "1"
91-
}
92-
9391
export function isAutoupdateDisabledByEnv() {
94-
return truthyEnv("ALTIMATE_CLI_DISABLE_AUTOUPDATE") || truthyEnv("OPENCODE_DISABLE_AUTOUPDATE")
92+
// The documented name wins outright when set (a documented `false` is not overridden
93+
// by a fallback `true`), same as every `Flag.*` read.
94+
return FlagEnvTruthy("OPENCODE_DISABLE_AUTOUPDATE")
9595
}
9696
// altimate_change end
9797

‎packages/opencode/src/effect/config-service.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,11 @@ export function aliasDocumentedNames(provider: ConfigProvider.ConfigProvider): C
8989
const head = path[0]
9090
const alias = typeof head === "string" ? documentedAlias(head) : undefined
9191
if (alias === undefined) return provider.load(path)
92+
// Only a set, non-empty scalar is a documented value. `fromEnv` also answers a
93+
// prefix path with a Record node (`ALTIMATE_CLI_CLIENT_CHILD=x` makes
94+
// `ALTIMATE_CLI_CLIENT` one), which is not a value of the flag.
9295
return Effect.flatMap(provider.load([alias, ...path.slice(1)]), (node) =>
93-
node && !(node._tag === "Value" && node.value === "") ? Effect.succeed(node) : provider.load(path),
96+
node && node._tag === "Value" && node.value !== "" ? Effect.succeed(node) : provider.load(path),
9497
)
9598
})
9699
}

‎packages/opencode/src/flag/flag.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ Object.defineProperty(Flag, "OPENCODE_CLIENT", {
240240
Object.defineProperty(Flag, "ALTIMATE_CLI_YOLO", {
241241
get() {
242242
const alt = process.env["ALTIMATE_CLI_YOLO"]
243-
if (alt !== undefined) {
243+
if (alt !== undefined && alt !== "") {
244244
const v = alt.toLowerCase()
245245
return v === "true" || v === "1"
246246
}

‎packages/opencode/src/storage/db.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import { Flag } from "../flag/flag"
1717
import { iife } from "@/util/iife"
1818
import { Effect } from "effect"
1919
import freshSchema from "@opencode-ai/core/database/schema.gen"
20+
// altimate_change start — documented ALTIMATE_CLI_ name read first (see core `env`)
21+
import { env as FlagEnv } from "@opencode-ai/core/flag/flag"
22+
// altimate_change end
2023

2124
declare const OPENCODE_MIGRATIONS: { sql: string; timestamp: number; name: string }[] | undefined
2225

@@ -33,7 +36,9 @@ export namespace Database {
3336
export const Path = iife(() => {
3437
// altimate_change upstream_fix — keep legacy storage on the same sqlite file
3538
// as core when callers override OPENCODE_DB.
36-
const overridden = process.env["OPENCODE_DB"]
39+
// Read through the documented-name rule like core's `Flag.OPENCODE_DB`: the two
40+
// must name the same file or one process splits across two databases.
41+
const overridden = FlagEnv("OPENCODE_DB")
3742
if (overridden) {
3843
if (overridden === ":memory:" || path.isAbsolute(overridden)) return overridden
3944
return path.join(Global.Path.data, overridden)

‎packages/opencode/test/cli/upgrade-decision.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ describe("isAutoupdateDisabledByEnv", () => {
186186
withAutoupdateEnv({ ALTIMATE_CLI_DISABLE_AUTOUPDATE: "0", OPENCODE_DISABLE_AUTOUPDATE: "false" }, () => {
187187
expect(isAutoupdateDisabledByEnv()).toBe(false)
188188
}))
189+
190+
test("a documented false is not overridden by a fallback true — same rule as Flag.* (#1329 class)", () =>
191+
withAutoupdateEnv({ ALTIMATE_CLI_DISABLE_AUTOUPDATE: "false", OPENCODE_DISABLE_AUTOUPDATE: "true" }, () => {
192+
expect(isAutoupdateDisabledByEnv()).toBe(false)
193+
}))
189194
})
190195
// altimate_change end
191196

‎packages/opencode/test/config/config.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2240,6 +2240,37 @@ describe("OPENCODE_PERMISSION env var", () => {
22402240
)
22412241
})
22422242

2243+
// altimate_change start — the documented ALTIMATE_CLI_CONFIG_CONTENT name reaches the
2244+
// same load path (#1329 class): this is the site that read `process.env` directly.
2245+
describe("ALTIMATE_CLI_CONFIG_CONTENT", () => {
2246+
it.instance("is loaded like OPENCODE_CONFIG_CONTENT", () =>
2247+
withProcessEnv(
2248+
"ALTIMATE_CLI_CONFIG_CONTENT",
2249+
JSON.stringify({ $schema: "https://opencode.ai/config.json", username: "documented-name" }),
2250+
Effect.gen(function* () {
2251+
const config = yield* Config.use.get()
2252+
expect(config.username).toBe("documented-name")
2253+
}),
2254+
),
2255+
)
2256+
2257+
it.instance("wins over OPENCODE_CONFIG_CONTENT when both are set", () =>
2258+
withProcessEnv(
2259+
"ALTIMATE_CLI_CONFIG_CONTENT",
2260+
JSON.stringify({ $schema: "https://opencode.ai/config.json", username: "documented-name" }),
2261+
withProcessEnv(
2262+
"OPENCODE_CONFIG_CONTENT",
2263+
JSON.stringify({ $schema: "https://opencode.ai/config.json", username: "fallback-name" }),
2264+
Effect.gen(function* () {
2265+
const config = yield* Config.use.get()
2266+
expect(config.username).toBe("documented-name")
2267+
}),
2268+
),
2269+
),
2270+
)
2271+
})
2272+
// altimate_change end
2273+
22432274
describe("OPENCODE_CONFIG_CONTENT token substitution", () => {
22442275
it.instance("substitutes {env:} tokens in OPENCODE_CONFIG_CONTENT", () =>
22452276
withProcessEnv(

‎packages/opencode/test/flag/external-skills-flag.test.ts‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, test } from "bun:test"
22
import path from "node:path"
3-
import { ConfigProvider, Effect, Layer, Option } from "effect"
3+
import { Config, ConfigProvider, Effect, Layer, Option } from "effect"
4+
import { ConfigService } from "../../src/effect/config-service"
45
import { RuntimeFlags } from "../../src/effect/runtime-flags"
56
import * as ServerAuth from "../../src/server/auth"
67

@@ -117,9 +118,27 @@ describe("the Effect Config-backed flags read the documented names too", () => {
117118
expect(empty.client).toBe("vscode")
118119
})
119120

120-
test("a numeric flag and a non-OPENCODE path are unaffected", async () => {
121+
test("a numeric flag works, and a non-OPENCODE path or a prefix-only documented name is left alone", async () => {
121122
expect((await runtimeFlags({ ALTIMATE_CLI_EXPERIMENTAL_OUTPUT_TOKEN_MAX: "4321" })).outputTokenMax).toBe(4321)
122123
expect((await runtimeFlags({ OPENCODE_EXPERIMENTAL_OUTPUT_TOKEN_MAX: "1234" })).outputTokenMax).toBe(1234)
124+
// A Config key with no OPENCODE_ prefix goes straight through the wrap.
125+
class Other extends ConfigService.Service<Other>()("@test/Other", {
126+
name: Config.string("OTHER_NAME").pipe(Config.withDefault("none")),
127+
}) {}
128+
const other = await Effect.runPromise(
129+
Effect.gen(function* () {
130+
return yield* Other
131+
}).pipe(
132+
Effect.provide(
133+
Other.defaultLayer.pipe(
134+
Layer.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: { OTHER_NAME: "x", ALTIMATE_CLI_NAME: "y" } }))),
135+
),
136+
),
137+
),
138+
)
139+
expect(other.name).toBe("x")
140+
// `fromEnv` answers a prefix path with a Record node: not a value of the flag.
141+
expect((await runtimeFlags({ ALTIMATE_CLI_CLIENT_CHILD: "x", OPENCODE_CLIENT: "vscode" })).client).toBe("vscode")
123142
})
124143

125144
test("server auth reads ALTIMATE_CLI_SERVER_PASSWORD / _USERNAME", async () => {
@@ -150,7 +169,7 @@ describe("the core Flag object reads the documented names", () => {
150169
return JSON.parse(out.trim())
151170
}
152171

153-
test("ALTIMATE_CLI_CONFIG reaches the Flag config.ts reads", async () => {
172+
test("ALTIMATE_CLI_CONFIG / _DIR / _CONTENT reach the core Flag values config.ts reads at import", async () => {
154173
expect(await coreFlag("OPENCODE_CONFIG", { ALTIMATE_CLI_CONFIG: "/documented.json" })).toBe("/documented.json")
155174
expect(await coreFlag("OPENCODE_CONFIG", { ALTIMATE_CLI_CONFIG: "", OPENCODE_CONFIG: "/fallback.json" })).toBe("/fallback.json")
156175
expect(await coreFlag("OPENCODE_CONFIG_DIR", { ALTIMATE_CLI_CONFIG_DIR: "/documented" })).toBe("/documented")
@@ -163,10 +182,18 @@ describe("the core Flag object reads the documented names", () => {
163182
expect(await flag("OPENCODE_DISABLE_AUTOUPDATE", { ALTIMATE_CLI_DISABLE_AUTOUPDATE: "false", OPENCODE_DISABLE_AUTOUPDATE: "true" })).toBe(false)
164183
})
165184

166-
test("the Effect Config flag it carries resolves the documented name", async () => {
185+
test("the Effect Config flag it carries resolves the documented name, and an invalid documented value does not fall back", async () => {
167186
expect(await coreFlag("OPENCODE_EXPERIMENTAL_FILEWATCHER", { ALTIMATE_CLI_EXPERIMENTAL_FILEWATCHER: "true" })).toBe(true)
168187
expect(await coreFlag("OPENCODE_EXPERIMENTAL_FILEWATCHER", { OPENCODE_EXPERIMENTAL_FILEWATCHER: "true" })).toBe(true)
169188
expect(await coreFlag("OPENCODE_EXPERIMENTAL_FILEWATCHER", {})).toBe(false)
189+
// Set-but-invalid is `false`, as `truthy` says — `Config.orElse`/`Config.option` would
190+
// have silently taken the OPENCODE_ value instead.
191+
expect(await coreFlag("OPENCODE_EXPERIMENTAL_FILEWATCHER", { ALTIMATE_CLI_EXPERIMENTAL_FILEWATCHER: "typo", OPENCODE_EXPERIMENTAL_FILEWATCHER: "true" })).toBe(false)
192+
expect(await coreFlag("OPENCODE_EXPERIMENTAL_FILEWATCHER", { ALTIMATE_CLI_EXPERIMENTAL_FILEWATCHER: "", OPENCODE_EXPERIMENTAL_FILEWATCHER: "true" })).toBe(true)
193+
// The legacy storage path and core read the same database override.
194+
expect(await coreFlag("OPENCODE_DB", { ALTIMATE_CLI_DB: "/tmp/x.db" })).toBe("/tmp/x.db")
195+
expect(await coreFlag("ALTIMATE_CLI_YOLO", { ALTIMATE_CLI_YOLO: "", OPENCODE_YOLO: "true" })).toBe(true)
196+
expect(await flag("ALTIMATE_CLI_YOLO", { ALTIMATE_CLI_YOLO: "", OPENCODE_YOLO: "true" })).toBe(true)
170197
})
171198
})
172199

0 commit comments

Comments
 (0)