caelum0x f4486bda26 feat(flags): targeting by property, cohort or segment and % rollouts on a stable hash (QY1.3, I10, A19)
- packages/openfeature-railyard/src/targeting.mjs: one dependency-free
  evaluator (sync SHA-1, rollout hash = parseInt(sha1("<flag>.<id>")[0:15])
  / 0xfffffffffffffff, inside when <= rollout/100; ordered rules, all
  conditions of a rule must match, first match wins with its own value).
  Property operators follow the reference matcher of posthog-node (MIT):
  exact/is_not (lists, true/false by truthiness), icontains, regex, gt/gte/
  lt/lte (numeric, else text), is_set/is_not_set. Segments are named sets of
  property conditions per environment (Flagsmith segment shape, BSD-3);
  cohorts are product analytics cohort ids. Byte copies with .d.mts types in
  apps/console/lib/flags and apps/product-analytics/src/flags (drift test).
- Console: rules and segments live in Railyard's store for both backends
  (migration 0009: railyard.flag_targeting, railyard.flag_segments, bounded
  checks, cascade, RLS, no anon), so the table fallback and the Flagsmith
  backend have the same rule features. setFlag validates rules and segment
  references; a used segment cannot be removed; branches copy rules and
  segments. The read endpoint takes ?id, ?props (JSON, 2 KB) and ?cohorts,
  answers each flag for that person and ships {on, value, rules} plus the
  segments used, so SDKs evaluate locally; without a person a targeted flag
  reads off (old clients stay safe). Flags screen: rules per flag (everyone,
  a property, a segment; cohorts only with RAILYARD_REPLAY_LIVE) with a
  share, and segments. POST /api/flags/segments.
- Product analytics /flags: evaluates targeted flags with the person's
  stored properties (request person_properties override) and cohort
  membership (through person overrides).
- OpenFeature providers and the client: evaluation context targetingKey +
  attributes (+ cohorts) resolve per person; cached starts keep rules.
- Contract fixture: flags items carry targeting (additive, version 2).

Tests
- openfeature 16/16: SHA-1 vs node crypto, rollout shares within bounds
  over 10 000 ids and monotonic growth, operators, rules, validation, read
  answer, client and both providers vs the evaluator, cached start, drift.
- console 142/142 incl. both backends with the same 20 rule sets, segments,
  isolation, read-endpoint context, 0009 migration on a scratch PG, and
  10 000 ids x 20 rule sets: provider local == read endpoint (0 mismatches).
- product analytics test/qy 10/10 incl. 10 000 ids x 20 rule sets: provider
  local == /flags from stored people and cohorts (0 mismatches), and the
  unmodified posthog-node local evaluation agreeing on all 18 non-cohort
  sets for 10 000 ids (0 mismatches, 0 undecided); a3 experiments unchanged.
- contract 6/6; console and PA tsc clean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 13:09:54 +03:00

83 lines
5.2 KiB
JavaScript

// QY1.3: twenty rule sets and a deterministic population, shared by the
// agreement tests (the SDK providers' local evaluation against the console's
// read endpoint and product analytics /flags, 10 000 ids each) and by the
// comparison with the reference SDK's own local evaluation.
export const COHORT_A = "0b8f6a3e-1c2d-4e5f-8a9b-0c1d2e3f4a5b";
export const COHORT_B = "7e6d5c4b-3a29-4817-a6b5-c4d3e2f1a0b9";
export const SEGMENTS = {
paying: { key: "paying", name: "Paying", conditions: [{ type: "property", key: "plan", operator: "exact", value: ["pro", "team", "enterprise"] }] },
eu_adults: {
key: "eu_adults",
name: "EU adults",
conditions: [
{ type: "property", key: "country", operator: "exact", value: ["DE", "FR", "NL"] },
{ type: "property", key: "age", operator: "gte", value: 18 },
],
},
};
const p = (key, operator, value) => (value === undefined ? { type: "property", key, operator } : { type: "property", key, operator, value });
/** Flag key -> {enabled, value, targeting}. `cohort` marks sets the reference SDK cannot evaluate locally. */
export const RULE_SETS = {
r01_half: { enabled: true, value: null, targeting: { rules: [{ conditions: [], rollout: 50 }] } },
r02_ten: { enabled: true, value: null, targeting: { rules: [{ conditions: [], rollout: 10 }] } },
r03_third: { enabled: true, value: "blue", targeting: { rules: [{ conditions: [], rollout: 33.3 }] } },
r04_pro: { enabled: true, value: null, targeting: { rules: [{ conditions: [p("plan", "exact", "pro")], rollout: 100 }] } },
r05_paid_list: { enabled: true, value: null, targeting: { rules: [{ conditions: [p("plan", "exact", ["pro", "team"])], rollout: 60 }] } },
r06_not_free: { enabled: true, value: null, targeting: { rules: [{ conditions: [p("plan", "is_not", "free")], rollout: 100 }] } },
r07_company_mail: { enabled: true, value: null, targeting: { rules: [{ conditions: [p("email", "icontains", "@ACME.example")], rollout: 100 }] } },
r08_not_test_mail: { enabled: true, value: null, targeting: { rules: [{ conditions: [p("email", "not_icontains", "test")], rollout: 40 }] } },
r09_regex: { enabled: true, value: null, targeting: { rules: [{ conditions: [p("email", "regex", "^user_[0-9]*7@")], rollout: 100 }] } },
r10_not_regex: { enabled: true, value: null, targeting: { rules: [{ conditions: [p("country", "not_regex", "^(US|CA)$")], rollout: 75 }] } },
r11_age_gt: { enabled: true, value: null, targeting: { rules: [{ conditions: [p("age", "gt", 30)], rollout: 100 }] } },
r12_age_lte_text: { enabled: true, value: null, targeting: { rules: [{ conditions: [p("age", "lte", "25")], rollout: 100 }] } },
r13_beta_truthy: { enabled: true, value: null, targeting: { rules: [{ conditions: [p("beta", "exact", "true")], rollout: 100 }] } },
r14_set: { enabled: true, value: null, targeting: { rules: [{ conditions: [p("company", "is_set")], rollout: 80 }] } },
r15_segment: { enabled: true, value: null, targeting: { rules: [{ conditions: [{ type: "segment", key: "paying" }], rollout: 50 }] } },
r16_two_rules: {
enabled: true,
value: "default",
targeting: {
rules: [
{ conditions: [p("plan", "exact", "enterprise")], rollout: 100, value: "enterprise" },
{ conditions: [{ type: "segment", key: "eu_adults" }], rollout: 30, value: "eu" },
{ conditions: [], rollout: 5 },
],
},
},
r17_and: { enabled: true, value: null, targeting: { rules: [{ conditions: [p("plan", "is_not", "free"), p("age", "gte", 21), p("country", "exact", "DE")], rollout: 90 }] } },
r18_off: { enabled: false, value: null, targeting: { rules: [{ conditions: [], rollout: 100 }] } },
r19_cohort: { enabled: true, value: null, cohort: true, targeting: { rules: [{ conditions: [{ type: "cohort", id: COHORT_A }], rollout: 100 }, { conditions: [{ type: "cohort", id: COHORT_B }], rollout: 20 }] } },
r20_cohort_and_prop: { enabled: true, value: null, cohort: true, targeting: { rules: [{ conditions: [{ type: "cohort", id: COHORT_A }, p("plan", "is_not", "free")], rollout: 70 }] } },
};
const PLANS = ["free", "pro", "team", "enterprise", "free"];
const COUNTRIES = ["DE", "FR", "US", "CA", "NL", "TR", "BR"];
const DOMAINS = ["acme.example", "shop.example", "test.example", "mail.example"];
/** The id of person i (several id shapes, as apps send them). */
export const idOf = (i) => (i % 3 === 0 ? `user_${i}` : i % 3 === 1 ? `${i}@shop.example` : `anon-${(i * 2654435761) % 4294967296}`);
/**
* Person i's properties. `complete` gives every property (the reference SDK
* asks its server when one is missing); otherwise some are left out.
*/
export function propsOf(i, complete = false) {
const props = {
plan: PLANS[i % PLANS.length],
email: `user_${i}@${DOMAINS[(i >> 2) % DOMAINS.length]}`,
country: COUNTRIES[(i * 7) % COUNTRIES.length],
age: i % 11 === 0 ? String(16 + (i % 50)) : 16 + ((i * 13) % 50),
beta: i % 4 === 0 ? true : i % 4 === 1 ? "TRUE" : i % 4 === 2 ? false : "false",
};
if (complete || i % 6 !== 0) props.company = `co_${i % 17}`;
if (!complete && i % 9 === 0) delete props.country;
return props;
}
/** The cohorts person i is in. */
export const cohortsOf = (i) => [...(i % 5 === 1 ? [COHORT_A] : []), ...(i % 3 === 0 ? [COHORT_B] : [])];