Skip to content

Commit 5f2733b

Browse files
fix: the config opt-out probe emits no telemetry of its own
The probe parses with `track: false`, so reading a config that uses env interpolation queues no `config_env_interpolation` row. Its tests set aside every global config source and both inline-config variables. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01YNasdD5tQMNtzXsuMEy82X (cherry picked from commit 12b9f6e)
1 parent 81cab6d commit 5f2733b

3 files changed

Lines changed: 57 additions & 20 deletions

File tree

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ import { ConfigManaged } from "@/config/managed"
1515
* before one exists, so there it throws. Treating that as "enabled" let an opted-out user's main
1616
* thread clear the opt-out marker and resolve their identity. This reads the same files `Config`
1717
* merges, in the same order (a later source overrides an earlier one), straight from disk:
18-
* the global config files (the legacy TOML `config` file included), `OPENCODE_CONFIG`, the project files and `.altimate-code`/`.opencode`
19-
* directories found from `directory`, the inline `OPENCODE_CONFIG_CONTENT`, the managed config
20-
* directory and macOS managed preferences. Remote and account configs are not read (they need
21-
* the network).
18+
* the global config files (the legacy TOML `config` file included), `OPENCODE_CONFIG`, the project
19+
* files and `.altimate-code`/`.opencode` directories found from `directory`, the inline
20+
* `OPENCODE_CONFIG_CONTENT`, the managed config directory and macOS managed preferences. Remote and
21+
* account configs are not read (they need the network). The read emits no telemetry of its own: it
22+
* runs before telemetry knows whether it may.
2223
*
23-
* - `disabled`: the last source that sets `telemetry.disabled` sets it truthy.
24+
* - `disabled`: the last source that sets `telemetry.disabled` sets it to `true`.
2425
* - `enabled`: no source turns it off.
2526
* - `unreadable`: a source that could not be read or parsed comes after the last one that set
2627
* the value, so the answer is unknown. Callers fail closed for anything an opt-out forbids.
@@ -65,14 +66,14 @@ export async function disabledState(directory: string = process.cwd()): Promise<
6566
return
6667
}
6768
try {
68-
apply(await ConfigPaths.parseText(text, file, "empty"))
69+
apply(await ConfigPaths.parseText(text, file, "empty", { track: false }))
6970
} catch {
7071
unknown = true
7172
}
7273
}
7374
const readText = async (text: string, source: string, dir: string) => {
7475
try {
75-
apply(await ConfigPaths.parseText(text, { source, dir }, "empty"))
76+
apply(await ConfigPaths.parseText(text, { source, dir }, "empty", { track: false }))
7677
} catch {
7778
unknown = true
7879
}

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ function dir(input: ParseSource) {
142142
}
143143

144144
/** Apply ${VAR}/${VAR:-default}/$${VAR}/{env:VAR} and {file:path} substitutions to config text. */
145-
async function substitute(text: string, input: ParseSource, missing: "error" | "empty" = "error") {
145+
async function substitute(text: string, input: ParseSource, missing: "error" | "empty" = "error", track = true) {
146146
// Single-pass substitution against the ORIGINAL text prevents output of one
147147
// pattern being re-matched by another (e.g. {env:A}="${B}" expanding B).
148148
// Uses the shared ENV_VAR_PATTERN grammar, adding JSON-escaping for values
@@ -186,7 +186,7 @@ async function substitute(text: string, input: ParseSource, missing: "error" | "
186186
// the telemetry schema). Dynamic import avoids a circular dep with @/altimate/telemetry (which
187187
// imports @/config/config). Matches main's single emit site in substitute().
188188
const { dollarRefs, dollarUnresolved, dollarDefaulted, dollarEscaped, legacyBraceRefs, legacyBraceUnresolved } = stats
189-
if (dollarRefs > 0 || legacyBraceRefs > 0 || dollarEscaped > 0) {
189+
if (track && (dollarRefs > 0 || legacyBraceRefs > 0 || dollarEscaped > 0)) {
190190
import("@/altimate/telemetry")
191191
.then(({ Telemetry }) => {
192192
Telemetry.track({
@@ -258,10 +258,16 @@ async function substitute(text: string, input: ParseSource, missing: "error" | "
258258
return out
259259
}
260260

261-
/** Substitute and parse JSONC text, throwing JsonError on syntax errors. */
262-
export async function parseText(text: string, input: ParseSource, missing: "error" | "empty" = "error"): Promise<any> {
261+
/** Substitute and parse JSONC text, throwing JsonError on syntax errors. `track: false` skips the
262+
* interpolation telemetry row, for a read that must not emit telemetry (the opt-out probe). */
263+
export async function parseText(
264+
text: string,
265+
input: ParseSource,
266+
missing: "error" | "empty" = "error",
267+
opts: { track?: boolean } = {},
268+
): Promise<any> {
263269
const configSource = source(input)
264-
text = await substitute(text, input, missing)
270+
text = await substitute(text, input, missing, opts.track !== false)
265271

266272
const errors: JsoncParseError[] = []
267273
const data = parseJsonc(text, errors, { allowTrailingComma: true })

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

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,68 @@
1-
import { afterEach, beforeEach, describe, expect, test } from "bun:test"
1+
import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test"
22
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "fs"
33
import os from "os"
44
import path from "path"
55
import { Global } from "../../../src/global"
66
import { TelemetryConfig } from "../../../src/altimate/telemetry/config-state"
7+
import { Telemetry } from "../../../src/altimate/telemetry"
78

89
describe("TelemetryConfig.disabledState (no Instance)", () => {
910
let dir: string
10-
let savedContent: string | undefined
1111
// The preload points the global config at a per-run temp directory; other suites may have
12-
// seeded these files there, so each test puts back what it found.
12+
// seeded files there, so every global source the reader takes is set aside and put back.
13+
const GLOBAL_SOURCES = [
14+
"config.json",
15+
"opencode.json",
16+
"opencode.jsonc",
17+
"altimate-code.json",
18+
"altimate-code.jsonc",
19+
"config",
20+
]
21+
// Both names of the inline config (the documented alias is read first).
22+
const INLINE_ENV = ["ALTIMATE_CLI_CONFIG_CONTENT", "OPENCODE_CONFIG_CONTENT"]
1323
const globalFile = () => path.join(Global.Path.config, "altimate-code.json")
1424
const legacyFile = () => path.join(Global.Path.config, "config")
1525
const savedFiles = new Map<string, string | undefined>()
26+
const savedEnv = new Map<string, string | undefined>()
1627

1728
beforeEach(() => {
1829
dir = mkdtempSync(path.join(os.tmpdir(), "telemetry-config-state-"))
19-
savedContent = process.env.OPENCODE_CONFIG_CONTENT
20-
delete process.env.OPENCODE_CONFIG_CONTENT
21-
for (const file of [globalFile(), legacyFile()]) {
30+
for (const key of INLINE_ENV) {
31+
savedEnv.set(key, process.env[key])
32+
delete process.env[key]
33+
}
34+
for (const name of GLOBAL_SOURCES) {
35+
const file = path.join(Global.Path.config, name)
2236
savedFiles.set(file, existsSync(file) ? readFileSync(file, "utf-8") : undefined)
2337
rmSync(file, { force: true })
2438
}
2539
})
2640
afterEach(() => {
27-
if (savedContent === undefined) delete process.env.OPENCODE_CONFIG_CONTENT
28-
else process.env.OPENCODE_CONFIG_CONTENT = savedContent
41+
for (const [key, value] of savedEnv) {
42+
if (value === undefined) delete process.env[key]
43+
else process.env[key] = value
44+
}
2945
for (const [file, text] of savedFiles) {
3046
if (text === undefined) rmSync(file, { force: true })
3147
else writeFileSync(file, text)
3248
}
3349
rmSync(dir, { recursive: true, force: true })
3450
})
3551

52+
test("reading a config that uses env interpolation emits no telemetry", async () => {
53+
const track = spyOn(Telemetry, "track")
54+
try {
55+
process.env.CONFIG_STATE_PROBE_FLAG = "true"
56+
writeFileSync(globalFile(), '{ "telemetry": { "disabled": {env:CONFIG_STATE_PROBE_FLAG} }, "x": "${HOME}" }')
57+
expect(await TelemetryConfig.disabledState(dir)).toBe("disabled")
58+
await new Promise((r) => setTimeout(r, 50)) // the parser's row would arrive after a dynamic import
59+
expect(track.mock.calls.filter(([event]) => event.type === "config_env_interpolation")).toHaveLength(0)
60+
} finally {
61+
delete process.env.CONFIG_STATE_PROBE_FLAG
62+
track.mockRestore()
63+
}
64+
})
65+
3666
test("nothing sets it: enabled", async () => {
3767
expect(await TelemetryConfig.disabledState(dir)).toBe("enabled")
3868
})

0 commit comments

Comments
 (0)