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

61 lines
2.9 KiB
TypeScript

import { FlagRules, FlagSegments, type RuleView } from "../../../components/FlagRules.tsx";
import { FlagsPanel } from "../../../components/FlagsPanel.tsx";
import { currentSession } from "../../../lib/current.ts";
import { deps } from "../../../lib/deps.ts";
import { listFlags, listSegments } from "../../../lib/flags/service.ts";
// Flags per app environment (Wave P PY2.1, RYP-E2). A flip reaches apps on
// their next read (the SDK reads every 30 s; reads are cached 15 s). Rules
// and % rollouts per flag, and the environment's segments (QY1.3).
export default async function FlagsPage() {
const { repo, flags, cfg } = deps();
const s = (await currentSession())!;
const [envs, apps] = await Promise.all([flags.store.listEnvs(s.org), repo.listApps(s.org)]);
const withFlags = await Promise.all(
envs.map(async (env) => ({ env, flags: await listFlags(flags, env).catch(() => null), segments: await listSegments(flags, env).catch(() => []) })),
);
const unset = apps.filter((a) => !envs.some((e) => e.app === a.name));
return (
<>
<h1>Flags</h1>
{withFlags.length === 0 && unset.length === 0 ? (
<section className="card">
<p className="muted">No apps yet. Add an app first, or run the add command in a Scribase project folder.</p>
</section>
) : null}
{withFlags.map(({ env, flags: list, segments }) => (
<section className="card" key={env.id}>
<h2>
{env.app} <span className="muted">{env.env}</span>
</h2>
<p className="muted">
Flags key for this environment: <code>{env.client_key}</code>
{env.first_read_at ? ` · first read by the app ${env.first_read_at.slice(0, 16).replace("T", " ")} UTC` : " · not read by the app yet"}
</p>
{list ? <FlagsPanel app={env.app} env={env.env} flags={list.map((f) => ({ key: f.key, enabled: f.enabled, value: f.value }))} /> : <p className="error">The flags service did not answer. Try again shortly.</p>}
{list?.length ? (
<details>
<summary>Who gets each flag</summary>
{list.map((f) => (
<div key={f.key}>
<h3>{f.key}</h3>
<FlagRules app={env.app} env={env.env} flag={{ key: f.key, enabled: f.enabled, rules: (f.targeting?.rules as RuleView[] | undefined) ?? null }} segments={segments.map((s) => s.key)} groups={cfg.replayLive} />
</div>
))}
<FlagSegments app={env.app} env={env.env} segments={segments.map((s) => ({ key: s.key, name: s.name, conditions: s.conditions as RuleView["conditions"] }))} />
</details>
) : null}
</section>
))}
{unset.map((a) => (
<section className="card" key={a.id}>
<h2>
{a.name} <span className="muted">production</span>
</h2>
<FlagsPanel app={a.name} env="production" flags={[]} />
</section>
))}
</>
);
}