Skip to content

Commit 78babdd

Browse files
fix: the opt-out probe rejects any source the config loader would reject
Each source is parsed with missing `{file:}` references as errors and validated by the loader's own schema step (`validateLoaded`); any failure makes the read unreadable. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01YNasdD5tQMNtzXsuMEy82X
1 parent 38c790d commit 78babdd

3 files changed

Lines changed: 47 additions & 24 deletions

File tree

‎packages/opencode/src/altimate/telemetry/config-state.ts‎

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { FSUtil } from "@opencode-ai/core/fs-util"
77
import { Global } from "../../global"
88
import { ConfigPaths } from "@/config/paths"
99
import { ConfigManaged } from "@/config/managed"
10+
import { validateLoaded } from "@/config/config"
1011

1112
/**
1213
* Whether the user config turns telemetry off, read without an Instance.
@@ -23,8 +24,9 @@ import { ConfigManaged } from "@/config/managed"
2324
*
2425
* - `disabled`: the last source that sets `telemetry.disabled` sets it to `true`.
2526
* - `enabled`: no source turns it off.
26-
* - `unreadable`: a source that could not be read or parsed comes after the last one that set
27-
* the value, so the answer is unknown. Callers fail closed for anything an opt-out forbids.
27+
* - `unreadable`: some source could not be read, parsed or validated as `Config` does (a missing
28+
* `{file:}` reference, a value its schema rejects). `Config` would fail to load at all, so the
29+
* answer is unknown. Callers fail closed for anything an opt-out forbids.
2830
*/
2931
export type State = "disabled" | "enabled" | "unreadable"
3032

@@ -45,17 +47,25 @@ function worktreeOf(directory: string): string | undefined {
4547
export async function disabledState(directory: string = process.cwd()): Promise<State> {
4648
try {
4749
let value: boolean | undefined
50+
// Any source that fails to read, parse or validate fails Config's whole load, so the answer
51+
// is unknown however the other sources set it.
4852
let unknown = false
49-
const apply = (data: unknown) => {
53+
const take = (data: unknown) => {
5054
const disabled = (data as { telemetry?: { disabled?: unknown } } | undefined)?.telemetry?.disabled
5155
if (disabled === undefined) return
52-
// Config's schema accepts only a boolean; anything else makes the whole config invalid there.
53-
if (typeof disabled !== "boolean") {
56+
if (typeof disabled !== "boolean") unknown = true
57+
else value = disabled
58+
}
59+
// Parse as the loader does (a missing `{file:}` reference is an error) and validate with the
60+
// loader's own schema step, so a source Config would reject cannot set the answer.
61+
const parse = async (text: string, input: string | { source: string; dir: string }) => {
62+
if (!text.trim()) return // the loader treats an empty file as `{}`
63+
const source = typeof input === "string" ? input : input.source
64+
try {
65+
take(validateLoaded(await ConfigPaths.parseText(text, input, "error", { track: false }), source))
66+
} catch {
5467
unknown = true
55-
return
5668
}
57-
value = disabled
58-
unknown = false
5969
}
6070
const readFile = async (file: string) => {
6171
let text: string
@@ -65,25 +75,15 @@ export async function disabledState(directory: string = process.cwd()): Promise<
6575
if ((err as NodeJS.ErrnoException)?.code !== "ENOENT") unknown = true
6676
return
6777
}
68-
try {
69-
apply(await ConfigPaths.parseText(text, file, "empty", { track: false }))
70-
} catch {
71-
unknown = true
72-
}
73-
}
74-
const readText = async (text: string, source: string, dir: string) => {
75-
try {
76-
apply(await ConfigPaths.parseText(text, { source, dir }, "empty", { track: false }))
77-
} catch {
78-
unknown = true
79-
}
78+
await parse(text, file)
8079
}
80+
const readText = (text: string, source: string, dir: string) => parse(text, { source, dir })
8181

8282
for (const file of GLOBAL_FILES) await readFile(path.join(Global.Path.config, file))
8383
// The legacy TOML global config, which Config merges after the JSON files (and migrates).
8484
const legacy = path.join(Global.Path.config, "config")
8585
try {
86-
apply(Bun.TOML.parse(await fs.readFile(legacy, "utf-8")))
86+
take(Bun.TOML.parse(await fs.readFile(legacy, "utf-8")))
8787
} catch (err) {
8888
if ((err as NodeJS.ErrnoException)?.code !== "ENOENT") unknown = true
8989
}

‎packages/opencode/src/config/config.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,13 @@ const LocalInfo = ConfigV1.Info
224224
type LocalInfo = ConfigV1.Info
225225
// altimate_change end
226226

227+
// altimate_change start — the loader's validation of one parsed source, side-effect free, for the
228+
// telemetry opt-out probe that reads the config before an Instance exists. Throws as loadConfig does.
229+
export function validateLoaded(parsed: unknown, source: string): LocalInfo {
230+
return ConfigParse.schema(LocalInfo, normalizeLoadedConfig(parsed), source)
231+
}
232+
// altimate_change end
233+
227234
// altimate_change start — opencode config includes local learn settings
228235
type Info = LocalInfo & {
229236
// altimate_change end

‎packages/opencode/test/altimate/telemetry/config-state.test.ts‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe("TelemetryConfig.disabledState (no Instance)", () => {
5454
const savedFlag = process.env.CONFIG_STATE_PROBE_FLAG
5555
try {
5656
process.env.CONFIG_STATE_PROBE_FLAG = "true"
57-
writeFileSync(globalFile(), '{ "telemetry": { "disabled": {env:CONFIG_STATE_PROBE_FLAG} }, "x": "${HOME}" }')
57+
writeFileSync(globalFile(), '{ "telemetry": { "disabled": {env:CONFIG_STATE_PROBE_FLAG} }, "model": "${HOME}" }')
5858
expect(await TelemetryConfig.disabledState(dir)).toBe("disabled")
5959
// The parser tracks its row behind `import("@/altimate/telemetry")`. That module is loaded, so
6060
// awaiting the same import settles after any such callback queued during the read.
@@ -68,6 +68,22 @@ describe("TelemetryConfig.disabledState (no Instance)", () => {
6868
}
6969
})
7070

71+
test("a later source the loader would reject cannot override an earlier opt-out", async () => {
72+
writeFileSync(globalFile(), JSON.stringify({ telemetry: { disabled: true } }))
73+
// Config's schema rejects a numeric model, so the real loader fails on this source.
74+
process.env.OPENCODE_CONFIG_CONTENT = JSON.stringify({ telemetry: { disabled: false }, model: 123 })
75+
expect(await TelemetryConfig.disabledState(dir)).toBe("unreadable")
76+
})
77+
78+
test("a missing {file:} reference makes the read unreadable", async () => {
79+
writeFileSync(globalFile(), JSON.stringify({ telemetry: { disabled: true } }))
80+
process.env.OPENCODE_CONFIG_CONTENT = JSON.stringify({
81+
telemetry: { disabled: false },
82+
model: `{file:${path.join(dir, "no-such-file")}}`,
83+
})
84+
expect(await TelemetryConfig.disabledState(dir)).toBe("unreadable")
85+
})
86+
7187
test("nothing sets it: enabled", async () => {
7288
expect(await TelemetryConfig.disabledState(dir)).toBe("enabled")
7389
})
@@ -107,9 +123,9 @@ describe("TelemetryConfig.disabledState (no Instance)", () => {
107123
expect(await TelemetryConfig.disabledState(dir)).toBe("unreadable")
108124
})
109125

110-
test("a later source that sets the value settles an earlier unreadable one", async () => {
126+
test("a source that fails anywhere makes the read unreadable, as it fails Config's whole load", async () => {
111127
writeFileSync(globalFile(), "{ not json")
112128
process.env.OPENCODE_CONFIG_CONTENT = JSON.stringify({ telemetry: { disabled: true } })
113-
expect(await TelemetryConfig.disabledState(dir)).toBe("disabled")
129+
expect(await TelemetryConfig.disabledState(dir)).toBe("unreadable")
114130
})
115131
})

0 commit comments

Comments
 (0)