Skip to content

Commit 81cab6d

Browse files
fix: read the sign-in opt-out from the linked project's config
`buildCliContext` takes the project directory, and the workspace hand-off passes the linked project's path. A project-level opt-out then holds when the command starts elsewhere. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01YNasdD5tQMNtzXsuMEy82X
1 parent 0908868 commit 81cab6d

4 files changed

Lines changed: 77 additions & 4 deletions

File tree

‎packages/opencode/src/altimate/plugin/altimate.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ const log = Log.create({ service: "altimate-plugin" })
7777
// and `cli_version` are strings, treat `cli_version: "local"` as a valid dev
7878
// build, and treat the payload as untrusted (anyone can craft a URL). An absent
7979
// `machine_id` means "do not attribute" — it must never be aliased on.
80-
export async function buildCliContext(machineIdPath?: string): Promise<string> {
80+
/** `directory`: the project the command acts on, whose config may opt out; the process's working
81+
* directory when the command has none (`auth login`). */
82+
export async function buildCliContext(machineIdPath?: string, directory?: string): Promise<string> {
8183
// altimate_change start — honour both telemetry opt-out gates, mirroring
8284
// telemetry/index.ts::doInit:
8385
// 1. ALTIMATE_TELEMETRY_DISABLED / OPENCODE_DISABLE_TELEMETRY env vars
@@ -99,7 +101,7 @@ export async function buildCliContext(machineIdPath?: string): Promise<string> {
99101
// read (an opt-out may be behind them), omit the id. Only a config that
100102
// readably leaves telemetry on defers to the telemetry module's own state
101103
// (opted out by env, no sink, test runner).
102-
const state = await TelemetryConfig.disabledState()
104+
const state = await TelemetryConfig.disabledState(directory)
103105
if (state === "enabled") {
104106
await Telemetry.init().catch(() => {})
105107
disabled = !Telemetry.isEnabled()

‎packages/opencode/src/altimate/workspace/browser-handoff.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,8 @@ export async function runHandoffWithOpener(
556556
// Import buildCliContext lazily so this module doesn't pull altimate.ts
557557
// into every consumer's import graph at load time.
558558
const { buildCliContext } = await import("../plugin/altimate")
559-
const cliContext = await buildCliContext().catch((err) => {
559+
// The linked project's config decides the opt-out, not the directory the command started in.
560+
const cliContext = await buildCliContext(undefined, input.identifier.projectPath).catch((err) => {
560561
log.warn("buildCliContext failed; proceeding without", { err: String(err) })
561562
return ""
562563
})

‎packages/opencode/test/altimate/altimate-plugin.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,56 @@ describe("buildCliContext", () => {
216216
})
217217
})
218218

219+
test("Config.get() throws: the opt-out of the project the command acts on is read, not the cwd's", async () => {
220+
delete process.env.ALTIMATE_TELEMETRY_DISABLED
221+
cfg.mockImplementation((async () => {
222+
throw new Error("InstanceRef not provided")
223+
}) as never)
224+
await using dir = await tmpdir()
225+
await using project = await tmpdir()
226+
fs.mkdirSync(path.join(project.path, ".altimate-code"))
227+
fs.writeFileSync(
228+
path.join(project.path, ".altimate-code", "altimate-code.json"),
229+
JSON.stringify({ telemetry: { disabled: true } }),
230+
)
231+
const idPath = path.join(dir.path, "machine-id")
232+
fs.writeFileSync(idPath, VALID_UUID, "utf8")
233+
const saved = {
234+
cs: process.env.APPLICATIONINSIGHTS_CONNECTION_STRING,
235+
opencodeOptOut: process.env.OPENCODE_DISABLE_TELEMETRY,
236+
content: process.env.OPENCODE_CONFIG_CONTENT,
237+
}
238+
delete process.env.OPENCODE_DISABLE_TELEMETRY
239+
delete process.env.OPENCODE_CONFIG_CONTENT
240+
process.env.APPLICATIONINSIGHTS_CONNECTION_STRING =
241+
"InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://ingest.example.invalid/"
242+
const home = spyOn(os, "homedir").mockImplementation(() => dir.path)
243+
const net = spyOn(globalThis, "fetch").mockImplementation((async () => new Response("", { status: 200 })) as any)
244+
const decode = (encoded: string) =>
245+
JSON.parse(Buffer.from(encoded, "base64url").toString("utf8")) as Record<string, unknown>
246+
try {
247+
await Telemetry.shutdown()
248+
await Telemetry.init()
249+
expect(Telemetry.isEnabled()).toBe(true)
250+
// The opted-out project: no id.
251+
expect("machine_id" in decode(await buildCliContext(idPath, project.path))).toBe(false)
252+
// The same process from a directory with no opt-out: the id is sent.
253+
expect(decode(await buildCliContext(idPath, dir.path))["machine_id"]).toBe(VALID_UUID)
254+
} finally {
255+
await Telemetry.shutdown()
256+
net.mockRestore()
257+
home.mockRestore()
258+
for (const [key, value] of [
259+
["APPLICATIONINSIGHTS_CONNECTION_STRING", saved.cs],
260+
["OPENCODE_DISABLE_TELEMETRY", saved.opencodeOptOut],
261+
["OPENCODE_CONFIG_CONTENT", saved.content],
262+
] as const) {
263+
if (value === undefined) delete process.env[key]
264+
else process.env[key] = value
265+
}
266+
}
267+
})
268+
219269
test("includes machine_id when telemetry is enabled", async () => {
220270
delete process.env.ALTIMATE_TELEMETRY_DISABLED
221271
await using dir = await tmpdir()

‎packages/opencode/test/altimate/workspace/browser-handoff.test.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
// launching a real browser. The listener itself binds to 127.0.0.1, walks
88
// 7317..7325, and processes real HTTP requests — this is genuine end-to-end
99
// coverage for the callback validation path.
10-
import { afterEach, beforeEach, describe, expect, test } from "bun:test"
10+
import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test"
1111
import { createServer, connect } from "node:net"
1212

1313
import { AltimateApi } from "../../../src/altimate/api/client"
14+
import * as AltimatePlugin from "../../../src/altimate/plugin/altimate"
1415
import {
1516
buildManageUrl,
1617
openWorkspaceBrowserHandoff,
@@ -344,6 +345,25 @@ describe("runHandoffWithOpener end-to-end", () => {
344345
}
345346
})
346347

348+
test("cli_context is built for the linked project's directory, so its config opt-out applies", async () => {
349+
const spy = spyOn(AltimatePlugin, "buildCliContext").mockImplementation(async () => "ctx")
350+
try {
351+
let observed = ""
352+
await runHandoffWithOpener(
353+
{ identifier: { repoRemote: "[email protected]:acme/foo.git", projectPath: "/w/foo" }, projectName: "foo" },
354+
async (url) => {
355+
observed = url
356+
const { state, redirect } = parseHandoffUrl(url)
357+
await fireCallback(redirect, { workspace_id: "1", state, tenant: "acme" })
358+
},
359+
)
360+
expect(spy).toHaveBeenCalledWith(undefined, "/w/foo")
361+
expect(new URLSearchParams(new URL(observed).hash.replace(/^#/, "")).get("cli_context")).toBe("ctx")
362+
} finally {
363+
spy.mockRestore()
364+
}
365+
})
366+
347367
test("URL keeps project_name, project_remote, project_path out of the query", async () => {
348368
// Per m6 in the consensus review: project_path + project_remote MUST NOT
349369
// be sent as query params (they'd land in browser history, SaaS/CDN/WAF

0 commit comments

Comments
 (0)