Skip to content
Merged
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
9 changes: 9 additions & 0 deletions .changeset/browser-rule-traces.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@reflag/browser-sdk": minor
"@reflag/react-sdk": minor
"@reflag/react-native-sdk": minor
"@reflag/vue-sdk": minor
"@reflag/node-sdk": patch
---

Browser and framework SDKs ignore rule-by-rule evaluation traces. The deprecated `getFlagResults().<flag>.ruleResults` array is now empty, and browser check events no longer include `evalRuleResults`. Flag values, variant and source version IDs, and evaluation errors are unchanged. Browser and Node bootstrap snapshots still include an empty `ruleResults` array for older v2 readers. Node evaluation results and check events are unchanged.
1 change: 0 additions & 1 deletion packages/browser-sdk/src/bulkQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export type BulkEvent =
evalResult?: string;
evalErrors?: CheckEvent["evalErrors"];
evalContext?: Record<string, any>;
evalRuleResults?: boolean[];
evalMissingFields?: string[];
}
| {
Expand Down
13 changes: 7 additions & 6 deletions packages/browser-sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,11 @@ export class ReflagClient {
* @param flags The flags to update.
* @param triggerEvent Whether to trigger the `flagsUpdated` event.
*/
updateFlags(flags: RawFlags, triggerEvent = true, flagStateVersion?: number) {
updateFlags(
flags: BootstrappedState["flags"],
triggerEvent = true,
flagStateVersion?: number,
) {
this.flagsClient.setFetchedFlags(flags, triggerEvent, flagStateVersion);
}

Expand Down Expand Up @@ -1162,10 +1166,8 @@ export class ReflagClient {
variantKey: flag.variantKey,
sourceVersionId: flag.sourceVersionId,
}),
ruleResults: flag.ruleResults.map(({ ruleId, matched }) => ({
ruleId,
matched,
})),
// Released v2 readers require the array, even when it is empty.
ruleResults: [],
errors: flag.errors,
...(flag.optInEnabled === undefined
? {}
Expand Down Expand Up @@ -1403,7 +1405,6 @@ export class ReflagClient {
}
: {}),
flagStateVersion: this.flagsClient.getFlagStateVersion(),
evalRuleResults: result?.ruleResults.map((rule) => rule.matched),
evalMissingFields: result?.errors.flatMap((error) =>
error.code === "MISSING_CONTEXT_FIELD" && error.field
? [error.field]
Expand Down
21 changes: 1 addition & 20 deletions packages/browser-sdk/src/flag/flagCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,6 @@ export function isObject(item: unknown): item is Record<string, any> {
return item !== null && typeof item === "object" && !Array.isArray(item);
}

function isRuleEvaluationResult(result: unknown): boolean {
return (
isObject(result) &&
typeof result.ruleId === "string" &&
typeof result.matched === "boolean" &&
(result.allocationIndex === undefined ||
(Number.isInteger(result.allocationIndex) &&
result.allocationIndex >= 0)) &&
(result.destination === undefined ||
(isObject(result.destination) &&
(result.destination.type === "nextRule" ||
(result.destination.type === "variant" &&
typeof result.destination.variantKey === "string"))))
);
}

function isEvaluationError(error: unknown): boolean {
return (
isObject(error) &&
Expand Down Expand Up @@ -65,9 +49,6 @@ export function parseAPIFlagsResponse(
isFlagValue(flag.value) ||
(bootstrap && flag.resolved === false && flag.value === undefined)
) ||
(!(bootstrap && flag.ruleResults === undefined) &&
(!Array.isArray(flag.ruleResults) ||
!flag.ruleResults.every(isRuleEvaluationResult))) ||
!Array.isArray(flag.errors) ||
!flag.errors.every(isEvaluationError) ||
(flag.matchedRuleId !== undefined &&
Expand Down Expand Up @@ -112,7 +93,7 @@ export function parseAPIFlagsResponse(
sourceVersionId: flag.sourceVersionId,
matchedRuleId: flag.matchedRuleId,
allocationIndex: flag.allocationIndex,
ruleResults: flag.ruleResults ?? [],
ruleResults: [],
errors: flag.errors,
...(flag.optInEnabled === undefined
? {}
Expand Down
2 changes: 2 additions & 0 deletions packages/browser-sdk/src/flag/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export type RawFlag = {
sourceVersionId?: string;
matchedRuleId?: string;
allocationIndex?: number;
/** @deprecated Browser results no longer include a rule trace. This array is always empty. */
ruleResults: RuleEvaluationResult[];
errors: EvaluationError[];
/** Absent in Node bootstrap/older snapshots; null optIn means unavailable. */
Expand Down Expand Up @@ -168,6 +169,7 @@ export interface CheckEvent {
sourceVersionId?: string;
flagStateVersion?: number;
evalContext?: ReflagContext;
/** @deprecated Browser checks no longer include rule match results. */
evalRuleResults?: boolean[];
evalMissingFields?: string[];
evalErrors?: EvaluationError[];
Expand Down
17 changes: 9 additions & 8 deletions packages/browser-sdk/test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ describe("direct values and root checks", () => {
key: "flagA",
evalResult: "enhanced",
sourceVersionId: "bootstrap-version",
evalRuleResults: [],
});
expect(checks()[0]).not.toHaveProperty("evalRuleResults");
});

test("forwards v2 diagnostics without adding initialization errors to bootstrap reads", async () => {
Expand Down Expand Up @@ -161,7 +161,10 @@ describe("direct values and root checks", () => {
},
},
});
expect(client.getFlagResults().flagA.allocationIndex).toBe(0);
expect(client.getFlagResults().flagA).toMatchObject({
allocationIndex: 0,
ruleResults: [],
});
const state = client.getFlagsForBootstrap();
expect(state).toStrictEqual(JSON.parse(JSON.stringify(state)));
expect(state).not.toHaveProperty("flagStateVersion");
Expand All @@ -170,10 +173,7 @@ describe("direct values and root checks", () => {
resolved: true,
variantKey: "none",
sourceVersionId: "version-1",
ruleResults: [
{ ruleId: "skipped", matched: false },
{ ruleId: "split", matched: true },
],
ruleResults: [],
errors: [],
});
expect(state.flags.unresolved).not.toHaveProperty("value");
Expand All @@ -186,16 +186,16 @@ describe("direct values and root checks", () => {
key: "flagA",
evalResult: "none",
sourceVersionId: "version-1",
evalRuleResults: [false, true],
});
expect(checks()[0]).not.toHaveProperty("evalRuleResults");
});

test("registers an ignored root read immediately, but diagnostics/bootstrap register none; delivery stays buffered", async () => {
const client = create();
const check = vi.fn();
client.on("check", check);
await client.initialize();
client.getFlagResults();
expect(client.getFlagResults().flagA.ruleResults).toEqual([]);
client.getFlagsForBootstrap();
expect(check).not.toHaveBeenCalled();
client.getFlag("flagA");
Expand All @@ -217,6 +217,7 @@ describe("direct values and root checks", () => {
}),
]);
expect(checks()[0]).not.toHaveProperty("value");
expect(checks()[0]).not.toHaveProperty("evalRuleResults");
});

test("bare reads cannot return truthy objects, and mismatches do not leak payloads or fabricate selections", async () => {
Expand Down
54 changes: 9 additions & 45 deletions packages/browser-sdk/test/flagCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,12 @@ function setup() {
afterEach(() => vi.useRealTimers());

describe("v2 evaluated cache", () => {
const rule = { ruleId: "rule", matched: true };
const error = { code: "MISSING_CONTEXT_FIELD", message: "missing" };

test("accepts diagnostic optional fields and preserves their values", () => {
const flags = {
flagA: {
...selectedFlag(true),
ruleResults: [
rule,
{ ...rule, allocationIndex: 0, destination: { type: "nextRule" } },
{
...rule,
allocationIndex: 1,
destination: { type: "variant", variantKey: "" },
},
],
errors: [
error,
{ ...error, source: { type: "flag", key: "parent" } },
Expand All @@ -53,43 +43,17 @@ describe("v2 evaluated cache", () => {
expect(parseAPIFlagsResponse(flags)).toEqual(flags);
});

test("accepts omitted rule results only in bootstrap snapshots", () => {
const { ruleResults: _ruleResults, ...flagA } = selectedFlag(true);
expect(parseAPIFlagsResponse({ flagA }, true)).toEqual({
flagA: { ...flagA, ruleResults: [] },
});
expect(parseAPIFlagsResponse({ flagA })).toBeUndefined();
expect(
parseAPIFlagsResponse({ flagA: { ...flagA, ruleResults: null } }, true),
).toBeUndefined();
});

test.each([
null,
true,
{},
{ ...rule, ruleId: null },
{ ...rule, ruleId: 1 },
{ ...rule, matched: undefined },
{ ...rule, matched: 1 },
...[-1, 0.5, Infinity, NaN, "0", null].map((allocationIndex) => ({
...rule,
allocationIndex,
})),
...[
null,
[],
{ type: "unknown" },
{ type: "variant" },
{ type: "variant", variantKey: 1 },
].map((destination) => ({ ...rule, destination })),
])("rejects malformed rule diagnostics atomically: %j", (invalidRule) => {
const flags = {
good: selectedFlag(true),
bad: { ...selectedFlag(true), ruleResults: [invalidRule] },
};
expect(parseAPIFlagsResponse(flags)).toBeUndefined();
expect(parseAPIFlagsResponse(flags, true)).toBeUndefined();
{ ruleResults: [{ ruleId: "rule", matched: true }] },
{ ruleResults: null },
{ ruleResults: [true] },
])("ignores rule results in live and bootstrap snapshots: %j", (trace) => {
const expected = selectedFlag(true);
const { ruleResults: _ruleResults, ...value } = expected;
const flags = { flagA: { ...value, ...trace } };
expect(parseAPIFlagsResponse(flags)).toEqual({ flagA: expected });
expect(parseAPIFlagsResponse(flags, true)).toEqual({ flagA: expected });
});

test.each([
Expand Down
11 changes: 1 addition & 10 deletions packages/browser-sdk/test/flags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,14 @@ describe("v2 snapshots", () => {
});
const singular = client.getFlag("settings")!;
(singular.value as { nested: number[] }).nested.push(2);
singular.ruleResults[0].matched = false;
singular.ruleResults.push({ ruleId: "mutated", matched: true });
singular.errors.push({ code: "TEST", message: "mutated" });
expect(client.getFlag("settings")).toEqual(flag);

const batch = client.getFlags();
batch.settings.value = false;
batch.settings.ruleResults[0].matched = false;
const fetched = client.getFetchedFlags();
fetched.settings.value = false;
fetched.settings.ruleResults[0].matched = false;
expect(client.getFlags()).toEqual({ settings: flag });
expect(client.getFetchedFlags()).toEqual({ settings: flag });
expect(client.getFlag("settings")).toEqual(flag);
Expand Down Expand Up @@ -136,13 +134,6 @@ describe("v2 snapshots", () => {
flags: { bad: { ...selectedFlag(true), sourceVersionId: undefined } },
},
],
[
"malformed rule diagnostics",
{
...flagResponse,
flags: { bad: { ...selectedFlag(true), ruleResults: [true] } },
},
],
[
"malformed error diagnostics",
{
Expand Down
10 changes: 8 additions & 2 deletions packages/browser-sdk/test/mocks/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function selectedFlag(
variantKey,
sourceVersionId,
matchedRuleId: "default",
ruleResults: [{ ruleId: "default", matched: true }],
ruleResults: [],
errors: [],
};
}
Expand All @@ -37,7 +37,13 @@ export const flagResponse = {
protocolVersion: 2,
flagStateVersion: 1,
remoteContextUsed: false,
flags: flagsResult,
flags: {
...flagsResult,
flagA: {
...flagsResult.flagA,
ruleResults: [{ ruleId: "default", matched: true }],
},
},
};

function checkRequest(request: StrictRequest<DefaultBodyType>) {
Expand Down
7 changes: 2 additions & 5 deletions packages/node-sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,11 +532,8 @@ export class ReflagClient {
sourceVersionId: result.sourceVersionId,
}
: {}),
// Released v2 readers require the array and its rule IDs.
ruleResults: result.ruleResults.map(({ ruleId, matched }) => ({
ruleId,
matched,
})),
// Released v2 readers require the array, even when it is empty.
ruleResults: [],
// Dependency origins can name private flags or segments.
errors: result.errors.map(({ source: _source, ...error }) => error),
};
Expand Down
1 change: 1 addition & 0 deletions packages/node-sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export type BootstrappedFlags = {
string,
Pick<RawFlag, "resolved" | "variantKey" | "sourceVersionId"> & {
value: FlagValue;
/** Empty in generated snapshots; kept for compatibility with older v2 readers. */
ruleResults: Pick<RawFlag["ruleResults"][number], "ruleId" | "matched">[];
errors: (EvaluationError & { source?: never })[];
}
Expand Down
4 changes: 2 additions & 2 deletions packages/node-sdk/test/bootstrap-secrecy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe("bootstrap visibility boundary", () => {
resolved: true,
variantKey: "on",
sourceVersionId: "version:public",
ruleResults: [{ ruleId: "public-target", matched: true }],
ruleResults: [],
errors: [{ code: "MISSING_CONTEXT_FIELD", field: "user.plan" }],
},
},
Expand Down Expand Up @@ -179,7 +179,7 @@ describe("bootstrap visibility boundary", () => {
resolved: true,
variantKey: "default",
sourceVersionId: "version:rollout",
ruleResults: [{ ruleId: "rollout-rule", matched: true }],
ruleResults: [],
errors: [],
},
});
Expand Down
3 changes: 3 additions & 0 deletions packages/react-native-sdk/test/usage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,9 @@
}),
]),
);
for (const check of checks) {

Check warning on line 417 in packages/react-native-sdk/test/usage.test.tsx

View workflow job for this annotation

GitHub Actions / Build & Test

eslint(no-shadow)

'check' is already declared in the upper scope.

Check warning on line 417 in packages/react-native-sdk/test/usage.test.tsx

View workflow job for this annotation

GitHub Actions / Build & Test

eslint(no-shadow)

'check' is already declared in the upper scope.
expect(check).not.toHaveProperty("evalRuleResults");
}
});

test("rejects v1 bootstrap state instead of silently reading wrapper payloads", async () => {
Expand Down
1 change: 1 addition & 0 deletions packages/react-sdk/test/usage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ describe("direct-value hooks", () => {
sourceVersionId: "version-2",
flagStateVersion: 2,
});
expect(check.mock.calls[1][0]).not.toHaveProperty("evalRuleResults");
(first as { items: string[] }).items.push("mutated");
expect(result.current[1]).toEqual(value);
expect(client.getFlag("settings", null)).toEqual(value);
Expand Down
9 changes: 5 additions & 4 deletions packages/vue-sdk/test/usage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ describe("ReflagBootstrappedProvider", () => {
resolved: true,
variantKey: "true",
sourceVersionId: "version-1",
ruleResults: [],
ruleResults: [{ ruleId: "legacy-rule", matched: true }],
errors: [],
},
},
Expand All @@ -438,15 +438,16 @@ describe("ReflagBootstrappedProvider", () => {

const wrapper = mount(ReflagBootstrappedProvider, {
props: {
publishableKey: "key",
publishableKey: "bootstrapped-legacy-trace",
flags: bootstrappedFlags,
},
slots: { default: () => h(Child) },
});

await nextTick();
expect(wrapper.findComponent(Child).vm.client).toBeDefined();
expect(wrapper.findComponent(Child).vm.flag).toBe(true);
const { client, flag } = wrapper.findComponent(Child).vm;
expect(client.getFlagResults()["test-flag"].ruleResults).toEqual([]);
expect(flag).toBe(true);
});

test("enables live flag updates by default for bootstrapped providers", async () => {
Expand Down
Loading