Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/src/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import SubPersistor from "./store/SubPersistor";
import { isTelemetryEnabled, setTelemetryEnabled, setTelemetryInstanceId } from "./telemetry/state";
import TrayIcon from "./TrayIcon";
import { UnleashClient } from "./unleash/client";
import { experimentCohortHash } from "./unleash/experimentCohort";
import { synchronizeFeatureFlags } from "./unleash/ipc";

/** test if the running version is a major downgrade (downgrading by a major or minor version,
Expand Down Expand Up @@ -522,6 +523,11 @@ class Application {
unleashClient.setContext({ userId: String(storedUserId) });
}

// Experiment cohorts. Needs Trust & Safety sign-off before release: see unleash/experimentCohort.ts.
if (this.mAppMetadata?.instanceId !== undefined) {
unleashClient.setExperimentCohortHash(experimentCohortHash(this.mAppMetadata.instanceId));
}

synchronizeFeatureFlags(unleashClient);

// Collect metadata before creating the main window — the renderer
Expand Down
27 changes: 27 additions & 0 deletions src/main/src/unleash/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,33 @@ describe("UnleashClient", () => {
const params = await fetchedParams("1.0.0");
expect(params.has("userId")).toBe(false);
});

it("omits the experiment-cohort hash when not set", async () => {
const params = await fetchedParams("1.0.0");
expect(params.has("properties[experimentCohortHash]")).toBe(false);
});

it("sends the experiment-cohort hash under properties once set", async () => {
fetchMocker.mockResponseOnce(toggleBody([]));
const client = new UnleashClient("1.0.0");
client.setExperimentCohortHash("abc123");
await client.fetchFeatureFlags();

const params = new URLSearchParams(new URL(fetchMocker.requests()[0]!.url).search);
expect(params.get("properties[experimentCohortHash]")).toBe("abc123");
});

it("keeps the experiment-cohort hash when the renderer sets the context", async () => {
fetchMocker.mockResponseOnce(toggleBody([]));
const client = new UnleashClient("1.0.0");
client.setExperimentCohortHash("abc123");
client.setContext({ userId: "42" });
await client.fetchFeatureFlags();

const params = new URLSearchParams(new URL(fetchMocker.requests()[0]!.url).search);
expect(params.get("userId")).toBe("42");
expect(params.get("properties[experimentCohortHash]")).toBe("abc123");
});
});

// ============================================================
Expand Down
12 changes: 11 additions & 1 deletion src/main/src/unleash/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type UnleashContext = {
appVersion: string;
os: ReturnType<typeof platform>;
channel: "beta" | "stable";
experimentCohortHash?: string;
};
};

Expand Down Expand Up @@ -61,6 +62,7 @@ export class UnleashClient {

#flags: FeatureFlag[] = [];
#context: FlagContext = {};
#experimentCohortHash: string | undefined;

constructor(appVersion: string, options?: UnleashClientOptions) {
this.#sessionId = randomUUID();
Expand Down Expand Up @@ -148,6 +150,11 @@ export class UnleashClient {
this.#context = context;
}

/** See experimentCohort.ts. */
setExperimentCohortHash(hash: string): void {
this.#experimentCohortHash = hash;
}

async postMetrics(bucket: FlagMetricsBucket): Promise<void> {
const result = await this.#apiClient.POST("/api/frontend/client/metrics", {
body: {
Expand Down Expand Up @@ -256,6 +263,9 @@ export class UnleashClient {
appVersion: this.#appVersion,
os: platform(),
channel: this.#channel,
...(this.#experimentCohortHash !== undefined && {
experimentCohortHash: this.#experimentCohortHash,
}),
},
};
}
Expand All @@ -279,7 +289,7 @@ function serializeContext(context: UnleashContext): string {
if (context.userId) params.set("userId", context.userId);

for (const [key, value] of Object.entries(context.properties)) {
params.set(`properties[${key}]`, value);
if (value !== undefined) params.set(`properties[${key}]`, value);
}

return params.toString();
Expand Down
23 changes: 23 additions & 0 deletions src/main/src/unleash/experimentCohort.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, expect, it } from "vitest";

import { experimentCohortHash } from "./experimentCohort";

const INSTANCE_ID = "3f2b8c1e-4d5a-4f6b-9c7d-8e9f0a1b2c3d";

describe("experimentCohortHash", () => {
it("gives the same install the same hash every time", () => {
expect(experimentCohortHash(INSTANCE_ID)).toBe(experimentCohortHash(INSTANCE_ID));
});

it("gives different installs different hashes", () => {
expect(experimentCohortHash(INSTANCE_ID)).not.toBe(
experimentCohortHash("00000000-0000-4000-8000-000000000000"),
);
});

it("never contains the instance ID itself", () => {
const hash = experimentCohortHash(INSTANCE_ID);
expect(hash).toMatch(/^[0-9a-f]{64}$/);
expect(hash).not.toContain(INSTANCE_ID);
});
});
14 changes: 14 additions & 0 deletions src/main/src/unleash/experimentCohort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { createHash } from "node:crypto";

// TRUST & SAFETY SIGN-OFF REQUIRED before release: a stable per-install ID sent to Unleash on every
// flag fetch, regardless of analytics consent. It keeps an install in one experiment cohort across
// launches; without it, logged-out installs fall back to a per-launch sessionId. Salted, so it
// can't be matched to the raw instance ID.

const SALT = "vortex-experiment-cohort:";

/** The install's experiment-cohort hash: hex SHA-256 of the salted instance ID. */
export const experimentCohortHash = (instanceId: string): string =>
createHash("sha256")
.update(SALT + instanceId)
.digest("hex");