Wip/precomputed segments for flags#3198
Open
danoswaltCL wants to merge 5 commits into
Open
Conversation
…flag include lists
…d-segments-for-flags
Contributor
There was a problem hiding this comment.
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_segmentstorage (migration + entity + repository) and aPrecomputedSegmentServiceto compute/cache flattened segment member IDs. - Wired recompute/backfill triggers into segment + feature flag write paths, and added startup backfill.
- Updated
FeatureFlagService.getKeysto use a fast-path that pulls precomputed rows and filters viaSet.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); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.