diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5fdc275160..c69c9d674a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -250,7 +250,7 @@ jobs: - "packages/cli/src/ui/**" - "packages/cli/src/{background-removal,registry,telemetry}/**" - "packages/cli/src/capture/{captureCompositionFrame,captureTimeout,navigateForCapture,readBoundedResponse}.ts" - - "packages/cli/src/media-use/lib/config-lock.mjs" + - "packages/cli/src/media-use/lib/{config-lock,telemetry-opt-out}.mjs" - "packages/cli/src/auth/**" - "packages/cli/scripts/**" - "packages/core/stubs/**" diff --git a/packages/cli/src/media-use/lib/telemetry-opt-out.d.mts b/packages/cli/src/media-use/lib/telemetry-opt-out.d.mts new file mode 100644 index 0000000000..be732cc8c5 --- /dev/null +++ b/packages/cli/src/media-use/lib/telemetry-opt-out.d.mts @@ -0,0 +1,6 @@ +export type TelemetryOptOutVariable = "HYPERFRAMES_NO_TELEMETRY" | "DO_NOT_TRACK"; +export const TELEMETRY_OPT_OUT_VARIABLES: readonly TelemetryOptOutVariable[]; +export const TELEMETRY_OPT_OUT_VALUES: readonly string[]; +export function telemetryOptOutVariable( + variables: Readonly>, +): TelemetryOptOutVariable | null; diff --git a/packages/cli/src/media-use/lib/telemetry-opt-out.mjs b/packages/cli/src/media-use/lib/telemetry-opt-out.mjs new file mode 100644 index 0000000000..31cf534c9b --- /dev/null +++ b/packages/cli/src/media-use/lib/telemetry-opt-out.mjs @@ -0,0 +1,14 @@ +// The one rule for "telemetry is off" in a process's environment. It has no imports, so the CLI, the media-use +// skill scripts (published as standalone files) and Studio's build configs can all read it. +export const TELEMETRY_OPT_OUT_VARIABLES = ["HYPERFRAMES_NO_TELEMETRY", "DO_NOT_TRACK"]; +export const TELEMETRY_OPT_OUT_VALUES = ["1", "true", "yes", "on"]; + +/** The variable in `variables` that turns telemetry off, or null when none does. */ +export function telemetryOptOutVariable(variables) { + for (const name of TELEMETRY_OPT_OUT_VARIABLES) { + const value = variables[name]; + if (typeof value === "string" && TELEMETRY_OPT_OUT_VALUES.includes(value.trim().toLowerCase())) + return name; + } + return null; +} diff --git a/packages/cli/src/media-use/lib/telemetry-opt-out.test.mjs b/packages/cli/src/media-use/lib/telemetry-opt-out.test.mjs new file mode 100644 index 0000000000..688fc6c0c6 --- /dev/null +++ b/packages/cli/src/media-use/lib/telemetry-opt-out.test.mjs @@ -0,0 +1,23 @@ +import { strict as assert } from "node:assert"; +import { test } from "node:test"; +import { telemetryOptOutVariable } from "./telemetry-opt-out.mjs"; + +test("names the variable that turns telemetry off, the HyperFrames one first", () => { + assert.equal(telemetryOptOutVariable({ DO_NOT_TRACK: "1" }), "DO_NOT_TRACK"); + assert.equal( + telemetryOptOutVariable({ HYPERFRAMES_NO_TELEMETRY: "yes" }), + "HYPERFRAMES_NO_TELEMETRY", + ); + assert.equal( + telemetryOptOutVariable({ DO_NOT_TRACK: "1", HYPERFRAMES_NO_TELEMETRY: "on" }), + "HYPERFRAMES_NO_TELEMETRY", + ); +}); + +test("reads every affirmative spelling, and nothing else, as an opt-out", () => { + for (const value of ["1", "true", "YES", " on "]) + assert.equal(telemetryOptOutVariable({ DO_NOT_TRACK: value }), "DO_NOT_TRACK", value); + for (const value of ["", "0", "false", "no", "off"]) + assert.equal(telemetryOptOutVariable({ DO_NOT_TRACK: value }), null, value); + assert.equal(telemetryOptOutVariable({}), null); +}); diff --git a/packages/cli/src/media-use/lib/telemetry.mjs b/packages/cli/src/media-use/lib/telemetry.mjs index ed74b77066..40e9826ced 100644 --- a/packages/cli/src/media-use/lib/telemetry.mjs +++ b/packages/cli/src/media-use/lib/telemetry.mjs @@ -8,6 +8,7 @@ import { homedir } from "node:os"; import { dirname, join } from "node:path"; import { withFileLock } from "./config-lock.mjs"; import { globalMediaDir } from "./media-home.mjs"; +import { telemetryOptOutVariable } from "./telemetry-opt-out.mjs"; const POSTHOG_API_KEY = "phc_zjjbX0PnWxERXrMHhkEJWj9A9BhGVLRReICgsfTMmpx"; const POSTHOG_HOST = "https://us.i.posthog.com"; @@ -41,8 +42,7 @@ export function optedOut() { // `hyperframes telemetry disable` writes telemetryEnabled: false to the // shared config; one opt-out must cover the CLI and the media-use scripts. readSharedConfig().telemetryEnabled === false || - process.env.HYPERFRAMES_NO_TELEMETRY === "1" || - process.env.DO_NOT_TRACK === "1" || + telemetryOptOutVariable(process.env) !== null || process.env.CI === "true" || process.env.CI === "1" || process.env.NODE_ENV === "development" diff --git a/packages/cli/src/media-use/lib/telemetry.test.mjs b/packages/cli/src/media-use/lib/telemetry.test.mjs index b3ebfafc99..de443bf9b3 100644 --- a/packages/cli/src/media-use/lib/telemetry.test.mjs +++ b/packages/cli/src/media-use/lib/telemetry.test.mjs @@ -59,6 +59,25 @@ test("optedOut respects DO_NOT_TRACK / HYPERFRAMES_NO_TELEMETRY / CI", () => { } }); +// fails without the fix: the media-use scripts took only "1", so DO_NOT_TRACK=true silenced the CLI and not them. +for (const [name, value] of [ + ["DO_NOT_TRACK", "true"], + ["DO_NOT_TRACK", "yes"], + ["DO_NOT_TRACK", " ON "], + ["HYPERFRAMES_NO_TELEMETRY", "TRUE"], +]) { + test(`optedOut respects ${name}=${JSON.stringify(value)}, as the CLI does`, () => { + const saved = { ...process.env }; + try { + withoutTelemetryOptOut(); + process.env[name] = value; + assert.equal(optedOut(), true); + } finally { + restoreEnv(saved); + } + }); +} + test("optedOut honors telemetryEnabled: false from the shared config", () => { const savedEnv = { ...process.env }; const { root, home } = sandbox(); diff --git a/packages/cli/src/telemetry/policy.ts b/packages/cli/src/telemetry/policy.ts index 8daa7cfefb..3b68962e71 100644 --- a/packages/cli/src/telemetry/policy.ts +++ b/packages/cli/src/telemetry/policy.ts @@ -1,3 +1,4 @@ +import { telemetryOptOutVariable } from "../media-use/lib/telemetry-opt-out.mjs"; import { isDevMode } from "../utils/env.js"; import { POSTHOG_API_KEY } from "./posthogKey.js"; @@ -13,28 +14,13 @@ export type TelemetryStatus = { source: TelemetryStatusSource; }; -const ENV_OPT_OUT_VALUES = new Set(["1", "true", "yes", "on"]); - -/** - * Parse privacy-control environment variables consistently at every CLI - * surface. Be liberal about common affirmative spellings so an explicit - * opt-out never silently becomes an opt-in. - */ -function isEnvOptOutValue(value: string | undefined): boolean { - return value !== undefined && ENV_OPT_OUT_VALUES.has(value.trim().toLowerCase()); -} - /** * Return the runtime/build override that suppresses telemetry before the * persisted preference is considered. */ export function telemetryRuntimeOverride(): Exclude | null { - if (isEnvOptOutValue(process.env["HYPERFRAMES_NO_TELEMETRY"])) { - return "HYPERFRAMES_NO_TELEMETRY"; - } - if (isEnvOptOutValue(process.env["DO_NOT_TRACK"])) { - return "DO_NOT_TRACK"; - } + const envOptOut = telemetryOptOutVariable(process.env); + if (envOptOut) return envOptOut; if (isDevMode()) { return "dev_mode"; } diff --git a/packages/studio/src/telemetry/policy.ts b/packages/studio/src/telemetry/policy.ts index f17f69ab7a..2c79fc9d13 100644 --- a/packages/studio/src/telemetry/policy.ts +++ b/packages/studio/src/telemetry/policy.ts @@ -43,15 +43,17 @@ function isApiKeyConfigured(): boolean { return POSTHOG_API_KEY.startsWith("phc_"); } -// VITE_HYPERFRAMES_NO_TELEMETRY mirrors the CLI's HYPERFRAMES_NO_TELEMETRY=1 -// opt-out so HeyGen's own dev/CI builds can suppress telemetry from the studio -// bundle the same way. Vite injects it at build time. Match the CLI's -// affirmative privacy-control spellings. +declare const __HF_TELEMETRY_OPT_OUT_VALUES__: readonly string[] | undefined; + +// VITE_HYPERFRAMES_NO_TELEMETRY opts a build out, in the spellings Studio's build injects from the CLI's +// shared check; a bundle built without them treats any value as an opt-out. // `import.meta.env` may be undefined in non-Vite bundlers (Next.js Turbopack). function isBuildTimeOptOut(): boolean { try { - const v = import.meta.env.VITE_HYPERFRAMES_NO_TELEMETRY as string | undefined; - return v !== undefined && ["1", "true", "yes", "on"].includes(v.trim().toLowerCase()); + const value = (import.meta.env.VITE_HYPERFRAMES_NO_TELEMETRY as string | undefined)?.trim(); + if (!value) return false; + if (typeof __HF_TELEMETRY_OPT_OUT_VALUES__ === "undefined") return true; + return __HF_TELEMETRY_OPT_OUT_VALUES__.includes(value.toLowerCase()); } catch { return false; } diff --git a/packages/studio/tsup.config.ts b/packages/studio/tsup.config.ts index 98920fab21..8eb8f18219 100644 --- a/packages/studio/tsup.config.ts +++ b/packages/studio/tsup.config.ts @@ -1,4 +1,5 @@ import { defineConfig } from "tsup"; +import { TELEMETRY_OPT_OUT_DEFINE } from "./vite.telemetry"; export default defineConfig({ tsconfig: "tsconfig.lib.json", @@ -13,6 +14,7 @@ export default defineConfig({ dts: true, clean: false, splitting: true, + define: TELEMETRY_OPT_OUT_DEFINE, external: [ "@codemirror/autocomplete", "@codemirror/commands", diff --git a/packages/studio/vite.config.ts b/packages/studio/vite.config.ts index 5b616f1844..e781803d33 100644 --- a/packages/studio/vite.config.ts +++ b/packages/studio/vite.config.ts @@ -25,6 +25,7 @@ import { PREVIEW_ALLOWED_HOSTS_ENV, previewAllowedHostsFromEnv, } from "../studio-server/src/helpers/requestIdentity"; +import { TELEMETRY_OPT_OUT_DEFINE } from "./vite.telemetry"; async function loadRuntimeSourceForDev( server: import("vite").ViteDevServer, @@ -366,6 +367,7 @@ export default defineConfig({ plugins: [react(), tailwindcss(), devProjectApi()], define: { __STUDIO_VERSION__: JSON.stringify(studioPkg.version), + ...TELEMETRY_OPT_OUT_DEFINE, }, resolve: { alias: { diff --git a/packages/studio/vite.telemetry.ts b/packages/studio/vite.telemetry.ts new file mode 100644 index 0000000000..8024534cc3 --- /dev/null +++ b/packages/studio/vite.telemetry.ts @@ -0,0 +1,6 @@ +import { TELEMETRY_OPT_OUT_VALUES } from "../cli/src/media-use/lib/telemetry-opt-out.mjs"; + +/** The opt-out spellings Studio's browser code compares its build flag against. */ +export const TELEMETRY_OPT_OUT_DEFINE = { + __HF_TELEMETRY_OPT_OUT_VALUES__: JSON.stringify(TELEMETRY_OPT_OUT_VALUES), +}; diff --git a/scripts/check-media-use-copy-parity.test.mjs b/scripts/check-media-use-copy-parity.test.mjs index a154a91c77..beb8ccbd9b 100644 --- a/scripts/check-media-use-copy-parity.test.mjs +++ b/scripts/check-media-use-copy-parity.test.mjs @@ -32,6 +32,7 @@ export const MEDIA_USE_COPY_NAMES = [ "received-env.mjs", "recipe-store.mjs", "telemetry.mjs", + "telemetry-opt-out.mjs", "terminal-text.mjs", "transcriptCutFade.mjs", "words.mjs", diff --git a/scripts/generate-skill-module-copies.mjs b/scripts/generate-skill-module-copies.mjs index 61409ac72a..f33cbe4f85 100644 --- a/scripts/generate-skill-module-copies.mjs +++ b/scripts/generate-skill-module-copies.mjs @@ -16,6 +16,11 @@ export const skillModuleCopies = [ "packages/cli/src/media-use/lib/received-env.mjs", ["skills/media-use/scripts/lib/received-env.mjs"], ], + ["packages/cli/src/media-use/lib/telemetry.mjs", ["skills/media-use/scripts/lib/telemetry.mjs"]], + [ + "packages/cli/src/media-use/lib/telemetry-opt-out.mjs", + ["skills/media-use/scripts/lib/telemetry-opt-out.mjs"], + ], [ "packages/cli/src/media-use/lib/openverse.mjs", ["skills/media-use/audio/scripts/lib/openverse.mjs"], diff --git a/skills-manifest.json b/skills-manifest.json index 04a573d5a9..dc61115497 100644 --- a/skills-manifest.json +++ b/skills-manifest.json @@ -18,8 +18,8 @@ "files": 13 }, "media-use": { - "hash": "7aa1e3701f3a10c0", - "files": 134 + "hash": "ff2fa3c4eb8a7bc1", + "files": 135 }, "remotion-to-hyperframes": { "hash": "3a1dd4a513701ef6", diff --git a/skills/media-use/scripts/lib/telemetry-opt-out.mjs b/skills/media-use/scripts/lib/telemetry-opt-out.mjs new file mode 100644 index 0000000000..31cf534c9b --- /dev/null +++ b/skills/media-use/scripts/lib/telemetry-opt-out.mjs @@ -0,0 +1,14 @@ +// The one rule for "telemetry is off" in a process's environment. It has no imports, so the CLI, the media-use +// skill scripts (published as standalone files) and Studio's build configs can all read it. +export const TELEMETRY_OPT_OUT_VARIABLES = ["HYPERFRAMES_NO_TELEMETRY", "DO_NOT_TRACK"]; +export const TELEMETRY_OPT_OUT_VALUES = ["1", "true", "yes", "on"]; + +/** The variable in `variables` that turns telemetry off, or null when none does. */ +export function telemetryOptOutVariable(variables) { + for (const name of TELEMETRY_OPT_OUT_VARIABLES) { + const value = variables[name]; + if (typeof value === "string" && TELEMETRY_OPT_OUT_VALUES.includes(value.trim().toLowerCase())) + return name; + } + return null; +} diff --git a/skills/media-use/scripts/lib/telemetry.mjs b/skills/media-use/scripts/lib/telemetry.mjs index ed74b77066..40e9826ced 100644 --- a/skills/media-use/scripts/lib/telemetry.mjs +++ b/skills/media-use/scripts/lib/telemetry.mjs @@ -8,6 +8,7 @@ import { homedir } from "node:os"; import { dirname, join } from "node:path"; import { withFileLock } from "./config-lock.mjs"; import { globalMediaDir } from "./media-home.mjs"; +import { telemetryOptOutVariable } from "./telemetry-opt-out.mjs"; const POSTHOG_API_KEY = "phc_zjjbX0PnWxERXrMHhkEJWj9A9BhGVLRReICgsfTMmpx"; const POSTHOG_HOST = "https://us.i.posthog.com"; @@ -41,8 +42,7 @@ export function optedOut() { // `hyperframes telemetry disable` writes telemetryEnabled: false to the // shared config; one opt-out must cover the CLI and the media-use scripts. readSharedConfig().telemetryEnabled === false || - process.env.HYPERFRAMES_NO_TELEMETRY === "1" || - process.env.DO_NOT_TRACK === "1" || + telemetryOptOutVariable(process.env) !== null || process.env.CI === "true" || process.env.CI === "1" || process.env.NODE_ENV === "development" diff --git a/skills/media-use/scripts/lib/telemetry.test.mjs b/skills/media-use/scripts/lib/telemetry.test.mjs index d2d39f9849..538caa5522 100644 --- a/skills/media-use/scripts/lib/telemetry.test.mjs +++ b/skills/media-use/scripts/lib/telemetry.test.mjs @@ -73,6 +73,24 @@ test("optedOut still honors the env opt-outs", () => { } }); +// fails without the fix: the skill took only "1", so DO_NOT_TRACK=true silenced the CLI and not it. +for (const [name, value] of [ + ["DO_NOT_TRACK", "true"], + ["DO_NOT_TRACK", "yes"], + ["DO_NOT_TRACK", " ON "], + ["HYPERFRAMES_NO_TELEMETRY", "TRUE"], +]) { + test(`optedOut respects ${name}=${JSON.stringify(value)}, as the CLI does`, () => { + writeConfig(null); + process.env[name] = value; + try { + assert.equal(optedOut(), true); + } finally { + delete process.env[name]; + } + }); +} + test("optedOut reads the shared config from HYPERFRAMES_CONFIG_DIR when it is set", () => { writeConfig(null); const configDir = join(home, "elsewhere");