-
Notifications
You must be signed in to change notification settings - Fork 0
feat!: anchored bucketing layout for traffic ramping (contract v12) #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
JosephSamirL
merged 5 commits into
feat/fullstack-v12
from
feat/anchored-bucketing-layout
Jul 6, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
437a51d
test(bucketing): import cross-SDK anchored golden vectors (v12 contract)
abbaseya 0ff796f
test(bucketing): anchored layout v12 — failing tests + stub (RED)
abbaseya 844c347
feat!: anchored bucketing layout for traffic ramping (contract v12)
abbaseya 979c019
test(bucketing): align anchored test doc comments with GREEN implemen…
abbaseya c387ad7
refactor(bucketing): simplify anchored active check and use resolved …
abbaseya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
Sources/ConvertSwiftSDKCore/Bucketing/AnchoredBucketing.swift
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| // Sources/ConvertSwiftSDKCore/Bucketing/AnchoredBucketing.swift | ||
| // ANCHORED bucketing layout (qs-01, cross-SDK bucketing contract v12) — Phase 2 / GREEN. | ||
| // | ||
| // Selected per experience by `BucketingManager.bucketVersionGated(...)`: `experience.version > 11` | ||
| // routes here; `version <= 11` / missing / non-numeric stays on the EXISTING packed walk in | ||
| // `BucketingManager.bucket(...)` (untouched, AC6). Spec of record (do NOT re-derive the algorithm | ||
| // here): `2026-06-09-convert-ios-sdk/qs-01-anchored-bucketing-layout.md` — mirrors the JS | ||
| // reference exactly (`data-manager.ts:591-610` `_buildVariationAllocations`, | ||
| // `bucketing-manager.ts:152-204` `getBucketRanges`/`selectBucketAnchored`): | ||
| // | ||
| // allocations = experience.variations (config order), entries with no `id` DROPPED (never | ||
| // counted toward totalWeight — matches `_buildVariationAllocations`'s `if (!variation?.id) | ||
| // return allocations`) → | ||
| // { id, allocation: isNaN(ta) || ta absent ? 100.0 : ta, | ||
| // active: (status == nil || status == RUNNING) && (ta > 0 || isNaN(ta) || ta absent) } | ||
| // totalWeight = sum of allocation over ALL remaining entries (active AND inactive) | ||
| // if totalWeight <= 0 → not bucketed | ||
| // cumWeight = 0 | ||
| // for each entry in order: | ||
| // anchor = (cumWeight / totalWeight) * 10000.0 | ||
| // width = entry.active ? entry.allocation * 100.0 : 0.0 | ||
| // if value >= anchor && value < anchor + width → return entry.id | ||
| // cumWeight += entry.allocation | ||
| // return nil | ||
| // | ||
| // All arithmetic is `Double` (IEEE754) throughout, matching JS `Number` semantics — no | ||
| // `Decimal`/`Float80`. `value` is the shared bucket-value projection (seed 9999, same | ||
| // MurmurHash3 + scaling as the packed path) — reused unchanged, never recomputed here. | ||
|
|
||
| import Foundation | ||
|
|
||
| /// Namespace for the ANCHORED bucketing pass (contract v12+). Stateless — a pure selector, mirroring | ||
| /// `BucketingManager.selectBucket` for the packed pass, so it stays trivially testable with no | ||
| /// collaborators (no `EventSink`/`Logger`; the caller owns mapping the result back onto a | ||
| /// `Variation` and the tracking enqueue). | ||
| internal enum AnchoredBucketing { | ||
| /// One variation's resolved allocation weight and active/inactive flag under the anchored | ||
| /// layout — a named struct (not a tuple) so `large_tuple` stays satisfied. | ||
| private struct Allocation { | ||
| let id: String | ||
| let allocation: Double | ||
| let active: Bool | ||
| } | ||
|
|
||
| /// Selects the variation id that `value` (a `0..<10000` bucket-unit, computed identically to | ||
| /// the packed path — hash/seed/scaling are untouched) falls into under the ANCHORED layout. | ||
| /// | ||
| /// - Parameters: | ||
| /// - variations: The experience's variations, in CONFIG ORDER, **unfiltered** — every entry | ||
| /// (active and inactive, with or without a `traffic_allocation`) must be passed through; | ||
| /// the anchored algorithm itself interprets active/inactive and NaN/absent allocation, | ||
| /// unlike the packed pass's pre-filtered `eligible` walk. | ||
| /// - value: The visitor's bucket value (`0..<10000`). | ||
| /// - Returns: The selected variation id, or `nil` when not bucketed. | ||
| static func selectBucket( | ||
| variations: [Components.Schemas.ExperienceVariationConfig], | ||
| value: Int | ||
| ) -> String? { | ||
| let allocations = buildAllocations(variations) | ||
| let totalWeight = allocations.reduce(0.0) { $0 + $1.allocation } | ||
| guard totalWeight > 0 else { | ||
| return nil | ||
| } | ||
|
|
||
| let doubleValue = Double(value) | ||
| var cumWeight = 0.0 | ||
| for entry in allocations { | ||
| let anchor = (cumWeight / totalWeight) * Double(Defaults.maxTraffic) | ||
| let width = entry.active ? entry.allocation * 100.0 : 0.0 | ||
| if doubleValue >= anchor && doubleValue < anchor + width { | ||
| return entry.id | ||
| } | ||
| cumWeight += entry.allocation | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| /// Builds the per-variation `{id, allocation, active}` triples (config order, entries with no | ||
| /// `id` dropped) that `selectBucket` walks — the direct mirror of the JS reference's | ||
| /// `_buildVariationAllocations`. | ||
| private static func buildAllocations( | ||
| _ variations: [Components.Schemas.ExperienceVariationConfig] | ||
| ) -> [Allocation] { | ||
| variations.compactMap { variation in | ||
| guard let id = variation.id else { | ||
| return nil | ||
| } | ||
| let allocation: Double | ||
| if let rawAllocation = variation.traffic_allocation, !rawAllocation.isNaN { | ||
| allocation = rawAllocation | ||
| } else { | ||
| allocation = 100.0 | ||
| } | ||
| let statusActive = variation.status == nil || variation.status == .running | ||
| let active = statusActive && allocation > 0 | ||
| return Allocation(id: id, allocation: allocation, active: active) | ||
| } | ||
| } | ||
| } |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.