Skip to content

Wip/precomputed segments for flags#3198

Open
danoswaltCL wants to merge 5 commits into
release/6.5from
wip/precomputed-segments-for-flags
Open

Wip/precomputed segments for flags#3198
danoswaltCL wants to merge 5 commits into
release/6.5from
wip/precomputed-segments-for-flags

Conversation

@danoswaltCL

@danoswaltCL danoswaltCL commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

requirements largely found here: #3191

note that this is just for the flags at this moment, as its somewhat more straightforward, the main mechanics will be the same for both.

This creates a new table for precomputed segments, which will get updated on segment list changes.
getKeys() will now do very little to snag flags.

I have this going to the old flow still in the event of some error finding the precomputed segment, which hopefully would be really hard to do.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Implements “precomputed segment lists” for feature flags to avoid recursive segment resolution on the /featureflag read path by persisting flattened inclusion/exclusion member IDs and doing in-memory lookups.

Changes:

  • Added precomputed_segment storage (migration + entity + repository) and a PrecomputedSegmentService to compute/cache flattened segment member IDs.
  • Wired recompute/backfill triggers into segment + feature flag write paths, and added startup backfill.
  • Updated FeatureFlagService.getKeys to use a fast-path that pulls precomputed rows and filters via Set.has() checks (with fallback to legacy on-the-fly resolution when a row is missing).

Reviewed changes

Copilot reviewed 21 out of 22 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
packages/types/src/Experiment/enums.ts Adds cache prefix for precomputed segment caching.
packages/backend/src/database/migrations/1779500000000-precomputedSegment.ts Creates precomputed_segment table keyed by feature flag id.
packages/backend/src/api/models/PrecomputedSegment.ts New TypeORM entity for precomputed flag segment members.
packages/backend/src/api/repositories/PrecomputedSegmentRepository.ts Upsert + batch fetch helpers for precomputed rows.
packages/backend/src/api/services/PrecomputedSegmentService.ts Flattens segment graphs into ID arrays; caching; backfill; recompute scheduling.
packages/backend/src/api/services/FeatureFlagService.ts Fast-path filtering using precomputed rows; recompute/seed triggers.
packages/backend/src/api/services/SegmentService.ts Schedules recomputes on segment mutations; recompute-after-delete logic.
packages/backend/src/api/repositories/SegmentRepository.ts Adds parent-segment lookup for ancestor recompute walks.
packages/backend/src/api/repositories/FeatureFlagRepository.ts Adds minimal projection query for /featureflag keys path.
packages/backend/src/init/seed/backfillPrecomputedSegments.ts Startup backfill for missing precomputed flag rows.
packages/backend/src/app.ts Wires startup backfill into boot sequence.
packages/backend/test/unit/services/*.test.ts Adds/updates unit tests for precomputed behavior + triggers.
packages/backend/CLAUDE.md Documents the precomputed flag segment design + triggers.
clientlibs/js/src/**/*.spec.ts Formatting-only test changes.
.gitignore Ignores .claude*.
.claude/plans/precomputed-segments-experiments.md Planning doc for future experiment parity.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +946 to +950
// Individual exclusion always wins
if (exclusionSet.has(experimentUser.id)) return false;

// Individual inclusion bypasses group checks
if (inclusionSet.has(experimentUser.id)) return true;
Comment on lines +536 to +538
if (flagId) {
await this.precomputedSegmentService.recomputeForFlag(flagId, logger);
}
Comment on lines +698 to +702
// Recompute precomputed sets for each affected flag after the transaction commits
const affectedFlagIds = [...new Set(listsInput.map((l) => l.id))];
await Promise.all(
affectedFlagIds.map((flagId) => this.precomputedSegmentService.recomputeForFlag(flagId, logger))
);
Comment on lines +856 to +857
await this.precomputedSegmentService.recomputeForFlag(listInput.id, logger);

Comment on lines +1175 to +1177
// The outer transaction has committed — recompute now (addList skipped it because it ran
// inside the transaction) so the imported enabled lists are reflected in precomputed_segment.
await this.precomputedSegmentService.recomputeForFlag(createdFlag.id, logger);
Comment on lines +1371 to +1373
// The outer transaction has committed — recompute now (addList skipped it because it ran
// inside the transaction) so the imported lists are reflected in precomputed_segment.
await this.precomputedSegmentService.recomputeForFlag(featureFlagId, logger);
Comment on lines +490 to +493
// Recompute after the delete transaction has committed so the recompute reads the
// post-delete state and stale member IDs are removed (fire-and-forget).
affectedFlagIds.forEach((flagId) => this.precomputedSegmentService.recomputeForFlag(flagId, logger));

Comment on lines +1037 to +1043
// Recompute precomputed sets for all flags that reference this segment (fire-and-forget).
// Skip when the caller already owns an explicit recomputeForFlag after the transaction —
// firing this from inside a transaction risks a stale-read race where the fire-and-forget
// reads the old enabled value and its upsert overwrites the correct post-commit result.
if (!skipScheduleRecompute) {
this.precomputedSegmentService.scheduleRecomputeForSegment(segmentDoc.id, logger);
}
Comment on lines +16 to +20
public async findByFlagIds(flagIds: string[]): Promise<(PrecomputedSegment | null)[]> {
if (!flagIds.length) return [];
const rows = await this.createQueryBuilder('ps').where('ps.featureFlagId IN (:...ids)', { ids: flagIds }).getMany();
return flagIds.map((id) => rows.find((r) => r.featureFlagId === id) ?? null);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants