@fabricorg/experiments-assignment 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Fabric
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1,135 @@
1
+ 'use strict';
2
+
3
+ // src/hash.ts
4
+ function murmur3_x86_32(key, seed = 0) {
5
+ const data = utf8Bytes(key);
6
+ const len = data.length;
7
+ const nblocks = len >>> 2;
8
+ let h1 = seed >>> 0;
9
+ const c1 = 3432918353;
10
+ const c2 = 461845907;
11
+ for (let i = 0; i < nblocks; i++) {
12
+ const o = i * 4;
13
+ let k12 = data[o] & 255 | (data[o + 1] & 255) << 8 | (data[o + 2] & 255) << 16 | (data[o + 3] & 255) << 24;
14
+ k12 = mul32(k12, c1);
15
+ k12 = rotl32(k12, 15);
16
+ k12 = mul32(k12, c2);
17
+ h1 ^= k12;
18
+ h1 = rotl32(h1, 13);
19
+ h1 = mul32(h1, 5) + 3864292196 >>> 0;
20
+ }
21
+ let k1 = 0;
22
+ const tail = len & 3;
23
+ const tailStart = nblocks * 4;
24
+ if (tail >= 3) k1 ^= (data[tailStart + 2] & 255) << 16;
25
+ if (tail >= 2) k1 ^= (data[tailStart + 1] & 255) << 8;
26
+ if (tail >= 1) {
27
+ k1 ^= data[tailStart] & 255;
28
+ k1 = mul32(k1, c1);
29
+ k1 = rotl32(k1, 15);
30
+ k1 = mul32(k1, c2);
31
+ h1 ^= k1;
32
+ }
33
+ h1 ^= len;
34
+ h1 = fmix32(h1);
35
+ return h1 >>> 0;
36
+ }
37
+ function rotl32(x, r) {
38
+ return (x << r | x >>> 32 - r) >>> 0;
39
+ }
40
+ function mul32(a, b) {
41
+ const aHi = a >>> 16 & 65535;
42
+ const aLo = a & 65535;
43
+ const bHi = b >>> 16 & 65535;
44
+ const bLo = b & 65535;
45
+ return aLo * bLo + (aHi * bLo + aLo * bHi << 16 >>> 0 | 0) >>> 0 >>> 0;
46
+ }
47
+ function fmix32(h) {
48
+ let x = h >>> 0;
49
+ x ^= x >>> 16;
50
+ x = mul32(x, 2246822507);
51
+ x ^= x >>> 13;
52
+ x = mul32(x, 3266489909);
53
+ x ^= x >>> 16;
54
+ return x >>> 0;
55
+ }
56
+ function utf8Bytes(s) {
57
+ if (typeof TextEncoder !== "undefined") return new TextEncoder().encode(s);
58
+ const out = [];
59
+ for (let i = 0; i < s.length; i++) {
60
+ let c = s.charCodeAt(i);
61
+ if (c < 128) {
62
+ out.push(c);
63
+ } else if (c < 2048) {
64
+ out.push(192 | c >> 6, 128 | c & 63);
65
+ } else if (c >= 55296 && c <= 56319 && i + 1 < s.length) {
66
+ const c2 = s.charCodeAt(i + 1);
67
+ if (c2 >= 56320 && c2 <= 57343) {
68
+ c = 65536 + ((c & 1023) << 10 | c2 & 1023);
69
+ i++;
70
+ out.push(
71
+ 240 | c >> 18,
72
+ 128 | c >> 12 & 63,
73
+ 128 | c >> 6 & 63,
74
+ 128 | c & 63
75
+ );
76
+ }
77
+ } else {
78
+ out.push(224 | c >> 12, 128 | c >> 6 & 63, 128 | c & 63);
79
+ }
80
+ }
81
+ return new Uint8Array(out);
82
+ }
83
+
84
+ // src/bucket.ts
85
+ var BUCKET_PRECISION = 1e4;
86
+ function bucketFor(experimentId, salt, subjectId) {
87
+ const key = `${experimentId}:${salt}:${subjectId}`;
88
+ return murmur3_x86_32(key) % BUCKET_PRECISION;
89
+ }
90
+
91
+ // src/assign.ts
92
+ function assign(args) {
93
+ const bucket = bucketFor(args.experimentId, args.salt, args.subjectId);
94
+ if (args.forcedVariantKey && args.variants.some((v) => v.key === args.forcedVariantKey)) {
95
+ return { variantKey: args.forcedVariantKey, bucket, reason: "forced" };
96
+ }
97
+ if (args.stickyVariantKey && args.variants.some((v) => v.key === args.stickyVariantKey)) {
98
+ return { variantKey: args.stickyVariantKey, bucket, reason: "sticky" };
99
+ }
100
+ const sampleRate = args.sampleRate ?? 1;
101
+ if (sampleRate < 1) {
102
+ const sampleBucket = bucketFor(`${args.experimentId}:sample`, args.salt, args.subjectId) / BUCKET_PRECISION;
103
+ if (sampleBucket >= sampleRate) {
104
+ return { variantKey: null, bucket, reason: "ineligible" };
105
+ }
106
+ }
107
+ const totalDeclared = args.variants.reduce((s, v) => s + (v.weight ?? 0), 0);
108
+ if (totalDeclared > 0) {
109
+ if (totalDeclared !== BUCKET_PRECISION) {
110
+ throw new Error(
111
+ `experiment ${args.experimentId}: variant weights must sum to ${BUCKET_PRECISION}, got ${totalDeclared}`
112
+ );
113
+ }
114
+ let cumulative = 0;
115
+ for (const v of args.variants) {
116
+ cumulative += v.weight ?? 0;
117
+ if (bucket < cumulative) return { variantKey: v.key, bucket, reason: "eligible" };
118
+ }
119
+ return {
120
+ variantKey: args.variants[args.variants.length - 1].key,
121
+ bucket,
122
+ reason: "eligible"
123
+ };
124
+ }
125
+ const idx = Math.floor(bucket / BUCKET_PRECISION * args.variants.length);
126
+ const safeIdx = Math.min(idx, args.variants.length - 1);
127
+ return { variantKey: args.variants[safeIdx].key, bucket, reason: "eligible" };
128
+ }
129
+
130
+ exports.BUCKET_PRECISION = BUCKET_PRECISION;
131
+ exports.assign = assign;
132
+ exports.bucketFor = bucketFor;
133
+ exports.murmur3_x86_32 = murmur3_x86_32;
134
+ //# sourceMappingURL=index.cjs.map
135
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/hash.ts","../src/bucket.ts","../src/assign.ts"],"names":["k1"],"mappings":";;;AAOO,SAAS,cAAA,CAAe,GAAA,EAAa,IAAA,GAAO,CAAA,EAAW;AAC5D,EAAA,MAAM,IAAA,GAAO,UAAU,GAAG,CAAA;AAC1B,EAAA,MAAM,MAAM,IAAA,CAAK,MAAA;AACjB,EAAA,MAAM,UAAU,GAAA,KAAQ,CAAA;AACxB,EAAA,IAAI,KAAK,IAAA,KAAS,CAAA;AAClB,EAAA,MAAM,EAAA,GAAK,UAAA;AACX,EAAA,MAAM,EAAA,GAAK,SAAA;AAEX,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,OAAA,EAAS,CAAA,EAAA,EAAK;AAChC,IAAA,MAAM,IAAI,CAAA,GAAI,CAAA;AACd,IAAA,IAAIA,GAAAA,GACD,KAAK,CAAC,CAAA,GAAK,OACV,IAAA,CAAK,CAAA,GAAI,CAAC,CAAA,GAAK,GAAA,KAAS,KACxB,IAAA,CAAK,CAAA,GAAI,CAAC,CAAA,GAAK,GAAA,KAAS,MACxB,IAAA,CAAK,CAAA,GAAI,CAAC,CAAA,GAAK,GAAA,KAAS,EAAA;AAE5B,IAAAA,GAAAA,GAAK,KAAA,CAAMA,GAAAA,EAAI,EAAE,CAAA;AACjB,IAAAA,GAAAA,GAAK,MAAA,CAAOA,GAAAA,EAAI,EAAE,CAAA;AAClB,IAA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A;AACjB,IAAA,KAAA,MAAW,CAAA,IAAK,KAAK,QAAA,EAAU;AAC7B,MAAA,UAAA,IAAc,EAAE,MAAA,IAAU,CAAA;AAC1B,MAAA,IAAI,MAAA,GAAS,YAAY,OAAO,EAAE,YAAY,CAAA,CAAE,GAAA,EAAK,MAAA,EAAQ,MAAA,EAAQ,UAAA,EAAW;AAAA,IAClF;AAEA,IAAA,OAAO;AAAA,MACL,YAAY,IAAA,CAAK,QAAA,CAAS,KAAK,QAAA,CAAS,MAAA,GAAS,CAAC,CAAA,CAAG,GAAA;AAAA,MACrD,MAAA;AAAA,MACA,MAAA,EAAQ;AAAA,KACV;AAAA,EACF;AAGA,EAAA,MAAM,MAAM,IAAA,CAAK,KAAA,CAAO,SAAS,gBAAA,GAAoB,IAAA,CAAK,SAAS,MAAM,CAAA;AACzE,EAAA,MAAM,UAAU,IAAA,CAAK,GAAA,CAAI,KAAK,IAAA,CAAK,QAAA,CAAS,SAAS,CAAC,CAAA;AACtD,EAAA,OAAO,EAAE,YAAY,IAAA,CAAK,QAAA,CAAS,OAAO,CAAA,CAAG,GAAA,EAAK,MAAA,EAAQ,MAAA,EAAQ,UAAA,EAAW;AAC/E","file":"index.cjs","sourcesContent":["/**\n * MurmurHash3 x86 32-bit. The deterministic bucketing primitive for every\n * Fabric Experiments SDK. See docs/adr/0001-assignment-hash.md.\n *\n * Inputs are hashed as their UTF-8 byte representation.\n * Output is treated as uint32 (sign-fixed via `>>> 0`).\n */\nexport function murmur3_x86_32(key: string, seed = 0): number {\n const data = utf8Bytes(key);\n const len = data.length;\n const nblocks = len >>> 2;\n let h1 = seed >>> 0;\n const c1 = 0xcc9e2d51;\n const c2 = 0x1b873593;\n\n for (let i = 0; i < nblocks; i++) {\n const o = i * 4;\n let k1 =\n (data[o]! & 0xff) |\n ((data[o + 1]! & 0xff) << 8) |\n ((data[o + 2]! & 0xff) << 16) |\n ((data[o + 3]! & 0xff) << 24);\n\n k1 = mul32(k1, c1);\n k1 = rotl32(k1, 15);\n k1 = mul32(k1, c2);\n\n h1 ^= k1;\n h1 = rotl32(h1, 13);\n h1 = (mul32(h1, 5) + 0xe6546b64) >>> 0;\n }\n\n let k1 = 0;\n const tail = len & 3;\n const tailStart = nblocks * 4;\n if (tail >= 3) k1 ^= (data[tailStart + 2]! & 0xff) << 16;\n if (tail >= 2) k1 ^= (data[tailStart + 1]! & 0xff) << 8;\n if (tail >= 1) {\n k1 ^= data[tailStart]! & 0xff;\n k1 = mul32(k1, c1);\n k1 = rotl32(k1, 15);\n k1 = mul32(k1, c2);\n h1 ^= k1;\n }\n\n h1 ^= len;\n h1 = fmix32(h1);\n return h1 >>> 0;\n}\n\nfunction rotl32(x: number, r: number): number {\n return ((x << r) | (x >>> (32 - r))) >>> 0;\n}\n\nfunction mul32(a: number, b: number): number {\n const aHi = (a >>> 16) & 0xffff;\n const aLo = a & 0xffff;\n const bHi = (b >>> 16) & 0xffff;\n const bLo = b & 0xffff;\n return ((aLo * bLo + ((((aHi * bLo + aLo * bHi) << 16) >>> 0) | 0)) >>> 0) >>> 0;\n}\n\nfunction fmix32(h: number): number {\n let x = h >>> 0;\n x ^= x >>> 16;\n x = mul32(x, 0x85ebca6b);\n x ^= x >>> 13;\n x = mul32(x, 0xc2b2ae35);\n x ^= x >>> 16;\n return x >>> 0;\n}\n\nfunction utf8Bytes(s: string): Uint8Array {\n if (typeof TextEncoder !== 'undefined') return new TextEncoder().encode(s);\n // Fallback for environments without TextEncoder.\n const out: number[] = [];\n for (let i = 0; i < s.length; i++) {\n let c = s.charCodeAt(i);\n if (c < 0x80) {\n out.push(c);\n } else if (c < 0x800) {\n out.push(0xc0 | (c >> 6), 0x80 | (c & 0x3f));\n } else if (c >= 0xd800 && c <= 0xdbff && i + 1 < s.length) {\n const c2 = s.charCodeAt(i + 1);\n if (c2 >= 0xdc00 && c2 <= 0xdfff) {\n c = 0x10000 + (((c & 0x3ff) << 10) | (c2 & 0x3ff));\n i++;\n out.push(\n 0xf0 | (c >> 18),\n 0x80 | ((c >> 12) & 0x3f),\n 0x80 | ((c >> 6) & 0x3f),\n 0x80 | (c & 0x3f),\n );\n }\n } else {\n out.push(0xe0 | (c >> 12), 0x80 | ((c >> 6) & 0x3f), 0x80 | (c & 0x3f));\n }\n }\n return new Uint8Array(out);\n}\n","import { murmur3_x86_32 } from './hash.js';\n\nexport const BUCKET_PRECISION = 10000 as const;\n\n/**\n * Returns an integer in [0, BUCKET_PRECISION) deterministic in (experimentId, salt, subjectId).\n * Contract: see docs/adr/0001-assignment-hash.md.\n */\nexport function bucketFor(experimentId: string, salt: string, subjectId: string): number {\n const key = `${experimentId}:${salt}:${subjectId}`;\n return murmur3_x86_32(key) % BUCKET_PRECISION;\n}\n","import { BUCKET_PRECISION, bucketFor } from './bucket.js';\n\nexport interface ManifestVariant {\n key: string;\n /** Optional explicit weight; if any variant declares one, weights of all others are required. */\n weight?: number;\n}\n\nexport interface AssignArgs {\n experimentId: string;\n salt: string;\n subjectId: string;\n variants: readonly ManifestVariant[];\n /** Sample rate in [0, 1] of eligible traffic the experiment applies to. */\n sampleRate?: number;\n /** Pre-existing sticky assignment — if set and matches a current variant, returned as-is. */\n stickyVariantKey?: string;\n /** Forced override (preview / QA). Highest priority. */\n forcedVariantKey?: string;\n}\n\nexport type AssignmentReason = 'eligible' | 'sticky' | 'forced' | 'ineligible';\n\nexport interface AssignmentResult {\n variantKey: string | null;\n bucket: number;\n reason: AssignmentReason;\n}\n\n/**\n * Returns deterministic assignment for a subject. `null` variant + reason `ineligible`\n * means the subject was sampled out and the SDK must NOT log an exposure.\n */\nexport function assign(args: AssignArgs): AssignmentResult {\n const bucket = bucketFor(args.experimentId, args.salt, args.subjectId);\n\n if (args.forcedVariantKey && args.variants.some((v) => v.key === args.forcedVariantKey)) {\n return { variantKey: args.forcedVariantKey, bucket, reason: 'forced' };\n }\n\n if (args.stickyVariantKey && args.variants.some((v) => v.key === args.stickyVariantKey)) {\n return { variantKey: args.stickyVariantKey, bucket, reason: 'sticky' };\n }\n\n const sampleRate = args.sampleRate ?? 1;\n if (sampleRate < 1) {\n // Use a separate hash-derived value for sampling so it's independent of variant assignment.\n const sampleBucket =\n bucketFor(`${args.experimentId}:sample`, args.salt, args.subjectId) / BUCKET_PRECISION;\n if (sampleBucket >= sampleRate) {\n return { variantKey: null, bucket, reason: 'ineligible' };\n }\n }\n\n const totalDeclared = args.variants.reduce((s, v) => s + (v.weight ?? 0), 0);\n if (totalDeclared > 0) {\n if (totalDeclared !== BUCKET_PRECISION) {\n throw new Error(\n `experiment ${args.experimentId}: variant weights must sum to ${BUCKET_PRECISION}, got ${totalDeclared}`,\n );\n }\n let cumulative = 0;\n for (const v of args.variants) {\n cumulative += v.weight ?? 0;\n if (bucket < cumulative) return { variantKey: v.key, bucket, reason: 'eligible' };\n }\n // Numerical safety net.\n return {\n variantKey: args.variants[args.variants.length - 1]!.key,\n bucket,\n reason: 'eligible',\n };\n }\n\n // Even split.\n const idx = Math.floor((bucket / BUCKET_PRECISION) * args.variants.length);\n const safeIdx = Math.min(idx, args.variants.length - 1);\n return { variantKey: args.variants[safeIdx]!.key, bucket, reason: 'eligible' };\n}\n"]}
@@ -0,0 +1,46 @@
1
+ /**
2
+ * MurmurHash3 x86 32-bit. The deterministic bucketing primitive for every
3
+ * Fabric Experiments SDK. See docs/adr/0001-assignment-hash.md.
4
+ *
5
+ * Inputs are hashed as their UTF-8 byte representation.
6
+ * Output is treated as uint32 (sign-fixed via `>>> 0`).
7
+ */
8
+ declare function murmur3_x86_32(key: string, seed?: number): number;
9
+
10
+ declare const BUCKET_PRECISION: 10000;
11
+ /**
12
+ * Returns an integer in [0, BUCKET_PRECISION) deterministic in (experimentId, salt, subjectId).
13
+ * Contract: see docs/adr/0001-assignment-hash.md.
14
+ */
15
+ declare function bucketFor(experimentId: string, salt: string, subjectId: string): number;
16
+
17
+ interface ManifestVariant {
18
+ key: string;
19
+ /** Optional explicit weight; if any variant declares one, weights of all others are required. */
20
+ weight?: number;
21
+ }
22
+ interface AssignArgs {
23
+ experimentId: string;
24
+ salt: string;
25
+ subjectId: string;
26
+ variants: readonly ManifestVariant[];
27
+ /** Sample rate in [0, 1] of eligible traffic the experiment applies to. */
28
+ sampleRate?: number;
29
+ /** Pre-existing sticky assignment — if set and matches a current variant, returned as-is. */
30
+ stickyVariantKey?: string;
31
+ /** Forced override (preview / QA). Highest priority. */
32
+ forcedVariantKey?: string;
33
+ }
34
+ type AssignmentReason = 'eligible' | 'sticky' | 'forced' | 'ineligible';
35
+ interface AssignmentResult {
36
+ variantKey: string | null;
37
+ bucket: number;
38
+ reason: AssignmentReason;
39
+ }
40
+ /**
41
+ * Returns deterministic assignment for a subject. `null` variant + reason `ineligible`
42
+ * means the subject was sampled out and the SDK must NOT log an exposure.
43
+ */
44
+ declare function assign(args: AssignArgs): AssignmentResult;
45
+
46
+ export { type AssignArgs, type AssignmentResult, BUCKET_PRECISION, type ManifestVariant, assign, bucketFor, murmur3_x86_32 };
@@ -0,0 +1,46 @@
1
+ /**
2
+ * MurmurHash3 x86 32-bit. The deterministic bucketing primitive for every
3
+ * Fabric Experiments SDK. See docs/adr/0001-assignment-hash.md.
4
+ *
5
+ * Inputs are hashed as their UTF-8 byte representation.
6
+ * Output is treated as uint32 (sign-fixed via `>>> 0`).
7
+ */
8
+ declare function murmur3_x86_32(key: string, seed?: number): number;
9
+
10
+ declare const BUCKET_PRECISION: 10000;
11
+ /**
12
+ * Returns an integer in [0, BUCKET_PRECISION) deterministic in (experimentId, salt, subjectId).
13
+ * Contract: see docs/adr/0001-assignment-hash.md.
14
+ */
15
+ declare function bucketFor(experimentId: string, salt: string, subjectId: string): number;
16
+
17
+ interface ManifestVariant {
18
+ key: string;
19
+ /** Optional explicit weight; if any variant declares one, weights of all others are required. */
20
+ weight?: number;
21
+ }
22
+ interface AssignArgs {
23
+ experimentId: string;
24
+ salt: string;
25
+ subjectId: string;
26
+ variants: readonly ManifestVariant[];
27
+ /** Sample rate in [0, 1] of eligible traffic the experiment applies to. */
28
+ sampleRate?: number;
29
+ /** Pre-existing sticky assignment — if set and matches a current variant, returned as-is. */
30
+ stickyVariantKey?: string;
31
+ /** Forced override (preview / QA). Highest priority. */
32
+ forcedVariantKey?: string;
33
+ }
34
+ type AssignmentReason = 'eligible' | 'sticky' | 'forced' | 'ineligible';
35
+ interface AssignmentResult {
36
+ variantKey: string | null;
37
+ bucket: number;
38
+ reason: AssignmentReason;
39
+ }
40
+ /**
41
+ * Returns deterministic assignment for a subject. `null` variant + reason `ineligible`
42
+ * means the subject was sampled out and the SDK must NOT log an exposure.
43
+ */
44
+ declare function assign(args: AssignArgs): AssignmentResult;
45
+
46
+ export { type AssignArgs, type AssignmentResult, BUCKET_PRECISION, type ManifestVariant, assign, bucketFor, murmur3_x86_32 };
package/dist/index.js ADDED
@@ -0,0 +1,130 @@
1
+ // src/hash.ts
2
+ function murmur3_x86_32(key, seed = 0) {
3
+ const data = utf8Bytes(key);
4
+ const len = data.length;
5
+ const nblocks = len >>> 2;
6
+ let h1 = seed >>> 0;
7
+ const c1 = 3432918353;
8
+ const c2 = 461845907;
9
+ for (let i = 0; i < nblocks; i++) {
10
+ const o = i * 4;
11
+ let k12 = data[o] & 255 | (data[o + 1] & 255) << 8 | (data[o + 2] & 255) << 16 | (data[o + 3] & 255) << 24;
12
+ k12 = mul32(k12, c1);
13
+ k12 = rotl32(k12, 15);
14
+ k12 = mul32(k12, c2);
15
+ h1 ^= k12;
16
+ h1 = rotl32(h1, 13);
17
+ h1 = mul32(h1, 5) + 3864292196 >>> 0;
18
+ }
19
+ let k1 = 0;
20
+ const tail = len & 3;
21
+ const tailStart = nblocks * 4;
22
+ if (tail >= 3) k1 ^= (data[tailStart + 2] & 255) << 16;
23
+ if (tail >= 2) k1 ^= (data[tailStart + 1] & 255) << 8;
24
+ if (tail >= 1) {
25
+ k1 ^= data[tailStart] & 255;
26
+ k1 = mul32(k1, c1);
27
+ k1 = rotl32(k1, 15);
28
+ k1 = mul32(k1, c2);
29
+ h1 ^= k1;
30
+ }
31
+ h1 ^= len;
32
+ h1 = fmix32(h1);
33
+ return h1 >>> 0;
34
+ }
35
+ function rotl32(x, r) {
36
+ return (x << r | x >>> 32 - r) >>> 0;
37
+ }
38
+ function mul32(a, b) {
39
+ const aHi = a >>> 16 & 65535;
40
+ const aLo = a & 65535;
41
+ const bHi = b >>> 16 & 65535;
42
+ const bLo = b & 65535;
43
+ return aLo * bLo + (aHi * bLo + aLo * bHi << 16 >>> 0 | 0) >>> 0 >>> 0;
44
+ }
45
+ function fmix32(h) {
46
+ let x = h >>> 0;
47
+ x ^= x >>> 16;
48
+ x = mul32(x, 2246822507);
49
+ x ^= x >>> 13;
50
+ x = mul32(x, 3266489909);
51
+ x ^= x >>> 16;
52
+ return x >>> 0;
53
+ }
54
+ function utf8Bytes(s) {
55
+ if (typeof TextEncoder !== "undefined") return new TextEncoder().encode(s);
56
+ const out = [];
57
+ for (let i = 0; i < s.length; i++) {
58
+ let c = s.charCodeAt(i);
59
+ if (c < 128) {
60
+ out.push(c);
61
+ } else if (c < 2048) {
62
+ out.push(192 | c >> 6, 128 | c & 63);
63
+ } else if (c >= 55296 && c <= 56319 && i + 1 < s.length) {
64
+ const c2 = s.charCodeAt(i + 1);
65
+ if (c2 >= 56320 && c2 <= 57343) {
66
+ c = 65536 + ((c & 1023) << 10 | c2 & 1023);
67
+ i++;
68
+ out.push(
69
+ 240 | c >> 18,
70
+ 128 | c >> 12 & 63,
71
+ 128 | c >> 6 & 63,
72
+ 128 | c & 63
73
+ );
74
+ }
75
+ } else {
76
+ out.push(224 | c >> 12, 128 | c >> 6 & 63, 128 | c & 63);
77
+ }
78
+ }
79
+ return new Uint8Array(out);
80
+ }
81
+
82
+ // src/bucket.ts
83
+ var BUCKET_PRECISION = 1e4;
84
+ function bucketFor(experimentId, salt, subjectId) {
85
+ const key = `${experimentId}:${salt}:${subjectId}`;
86
+ return murmur3_x86_32(key) % BUCKET_PRECISION;
87
+ }
88
+
89
+ // src/assign.ts
90
+ function assign(args) {
91
+ const bucket = bucketFor(args.experimentId, args.salt, args.subjectId);
92
+ if (args.forcedVariantKey && args.variants.some((v) => v.key === args.forcedVariantKey)) {
93
+ return { variantKey: args.forcedVariantKey, bucket, reason: "forced" };
94
+ }
95
+ if (args.stickyVariantKey && args.variants.some((v) => v.key === args.stickyVariantKey)) {
96
+ return { variantKey: args.stickyVariantKey, bucket, reason: "sticky" };
97
+ }
98
+ const sampleRate = args.sampleRate ?? 1;
99
+ if (sampleRate < 1) {
100
+ const sampleBucket = bucketFor(`${args.experimentId}:sample`, args.salt, args.subjectId) / BUCKET_PRECISION;
101
+ if (sampleBucket >= sampleRate) {
102
+ return { variantKey: null, bucket, reason: "ineligible" };
103
+ }
104
+ }
105
+ const totalDeclared = args.variants.reduce((s, v) => s + (v.weight ?? 0), 0);
106
+ if (totalDeclared > 0) {
107
+ if (totalDeclared !== BUCKET_PRECISION) {
108
+ throw new Error(
109
+ `experiment ${args.experimentId}: variant weights must sum to ${BUCKET_PRECISION}, got ${totalDeclared}`
110
+ );
111
+ }
112
+ let cumulative = 0;
113
+ for (const v of args.variants) {
114
+ cumulative += v.weight ?? 0;
115
+ if (bucket < cumulative) return { variantKey: v.key, bucket, reason: "eligible" };
116
+ }
117
+ return {
118
+ variantKey: args.variants[args.variants.length - 1].key,
119
+ bucket,
120
+ reason: "eligible"
121
+ };
122
+ }
123
+ const idx = Math.floor(bucket / BUCKET_PRECISION * args.variants.length);
124
+ const safeIdx = Math.min(idx, args.variants.length - 1);
125
+ return { variantKey: args.variants[safeIdx].key, bucket, reason: "eligible" };
126
+ }
127
+
128
+ export { BUCKET_PRECISION, assign, bucketFor, murmur3_x86_32 };
129
+ //# sourceMappingURL=index.js.map
130
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/hash.ts","../src/bucket.ts","../src/assign.ts"],"names":["k1"],"mappings":";AAOO,SAAS,cAAA,CAAe,GAAA,EAAa,IAAA,GAAO,CAAA,EAAW;AAC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GAAoB,IAAA,CAAK,SAAS,MAAM,CAAA;AACzE,EAAA,MAAM,UAAU,IAAA,CAAK,GAAA,CAAI,KAAK,IAAA,CAAK,QAAA,CAAS,SAAS,CAAC,CAAA;AACtD,EAAA,OAAO,EAAE,YAAY,IAAA,CAAK,QAAA,CAAS,OAAO,CAAA,CAAG,GAAA,EAAK,MAAA,EAAQ,MAAA,EAAQ,UAAA,EAAW;AAC/E","file":"index.js","sourcesContent":["/**\n * MurmurHash3 x86 32-bit. The deterministic bucketing primitive for every\n * Fabric Experiments SDK. See docs/adr/0001-assignment-hash.md.\n *\n * Inputs are hashed as their UTF-8 byte representation.\n * Output is treated as uint32 (sign-fixed via `>>> 0`).\n */\nexport function murmur3_x86_32(key: string, seed = 0): number {\n const data = utf8Bytes(key);\n const len = data.length;\n const nblocks = len >>> 2;\n let h1 = seed >>> 0;\n const c1 = 0xcc9e2d51;\n const c2 = 0x1b873593;\n\n for (let i = 0; i < nblocks; i++) {\n const o = i * 4;\n let k1 =\n (data[o]! & 0xff) |\n ((data[o + 1]! & 0xff) << 8) |\n ((data[o + 2]! & 0xff) << 16) |\n ((data[o + 3]! & 0xff) << 24);\n\n k1 = mul32(k1, c1);\n k1 = rotl32(k1, 15);\n k1 = mul32(k1, c2);\n\n h1 ^= k1;\n h1 = rotl32(h1, 13);\n h1 = (mul32(h1, 5) + 0xe6546b64) >>> 0;\n }\n\n let k1 = 0;\n const tail = len & 3;\n const tailStart = nblocks * 4;\n if (tail >= 3) k1 ^= (data[tailStart + 2]! & 0xff) << 16;\n if (tail >= 2) k1 ^= (data[tailStart + 1]! & 0xff) << 8;\n if (tail >= 1) {\n k1 ^= data[tailStart]! & 0xff;\n k1 = mul32(k1, c1);\n k1 = rotl32(k1, 15);\n k1 = mul32(k1, c2);\n h1 ^= k1;\n }\n\n h1 ^= len;\n h1 = fmix32(h1);\n return h1 >>> 0;\n}\n\nfunction rotl32(x: number, r: number): number {\n return ((x << r) | (x >>> (32 - r))) >>> 0;\n}\n\nfunction mul32(a: number, b: number): number {\n const aHi = (a >>> 16) & 0xffff;\n const aLo = a & 0xffff;\n const bHi = (b >>> 16) & 0xffff;\n const bLo = b & 0xffff;\n return ((aLo * bLo + ((((aHi * bLo + aLo * bHi) << 16) >>> 0) | 0)) >>> 0) >>> 0;\n}\n\nfunction fmix32(h: number): number {\n let x = h >>> 0;\n x ^= x >>> 16;\n x = mul32(x, 0x85ebca6b);\n x ^= x >>> 13;\n x = mul32(x, 0xc2b2ae35);\n x ^= x >>> 16;\n return x >>> 0;\n}\n\nfunction utf8Bytes(s: string): Uint8Array {\n if (typeof TextEncoder !== 'undefined') return new TextEncoder().encode(s);\n // Fallback for environments without TextEncoder.\n const out: number[] = [];\n for (let i = 0; i < s.length; i++) {\n let c = s.charCodeAt(i);\n if (c < 0x80) {\n out.push(c);\n } else if (c < 0x800) {\n out.push(0xc0 | (c >> 6), 0x80 | (c & 0x3f));\n } else if (c >= 0xd800 && c <= 0xdbff && i + 1 < s.length) {\n const c2 = s.charCodeAt(i + 1);\n if (c2 >= 0xdc00 && c2 <= 0xdfff) {\n c = 0x10000 + (((c & 0x3ff) << 10) | (c2 & 0x3ff));\n i++;\n out.push(\n 0xf0 | (c >> 18),\n 0x80 | ((c >> 12) & 0x3f),\n 0x80 | ((c >> 6) & 0x3f),\n 0x80 | (c & 0x3f),\n );\n }\n } else {\n out.push(0xe0 | (c >> 12), 0x80 | ((c >> 6) & 0x3f), 0x80 | (c & 0x3f));\n }\n }\n return new Uint8Array(out);\n}\n","import { murmur3_x86_32 } from './hash.js';\n\nexport const BUCKET_PRECISION = 10000 as const;\n\n/**\n * Returns an integer in [0, BUCKET_PRECISION) deterministic in (experimentId, salt, subjectId).\n * Contract: see docs/adr/0001-assignment-hash.md.\n */\nexport function bucketFor(experimentId: string, salt: string, subjectId: string): number {\n const key = `${experimentId}:${salt}:${subjectId}`;\n return murmur3_x86_32(key) % BUCKET_PRECISION;\n}\n","import { BUCKET_PRECISION, bucketFor } from './bucket.js';\n\nexport interface ManifestVariant {\n key: string;\n /** Optional explicit weight; if any variant declares one, weights of all others are required. */\n weight?: number;\n}\n\nexport interface AssignArgs {\n experimentId: string;\n salt: string;\n subjectId: string;\n variants: readonly ManifestVariant[];\n /** Sample rate in [0, 1] of eligible traffic the experiment applies to. */\n sampleRate?: number;\n /** Pre-existing sticky assignment — if set and matches a current variant, returned as-is. */\n stickyVariantKey?: string;\n /** Forced override (preview / QA). Highest priority. */\n forcedVariantKey?: string;\n}\n\nexport type AssignmentReason = 'eligible' | 'sticky' | 'forced' | 'ineligible';\n\nexport interface AssignmentResult {\n variantKey: string | null;\n bucket: number;\n reason: AssignmentReason;\n}\n\n/**\n * Returns deterministic assignment for a subject. `null` variant + reason `ineligible`\n * means the subject was sampled out and the SDK must NOT log an exposure.\n */\nexport function assign(args: AssignArgs): AssignmentResult {\n const bucket = bucketFor(args.experimentId, args.salt, args.subjectId);\n\n if (args.forcedVariantKey && args.variants.some((v) => v.key === args.forcedVariantKey)) {\n return { variantKey: args.forcedVariantKey, bucket, reason: 'forced' };\n }\n\n if (args.stickyVariantKey && args.variants.some((v) => v.key === args.stickyVariantKey)) {\n return { variantKey: args.stickyVariantKey, bucket, reason: 'sticky' };\n }\n\n const sampleRate = args.sampleRate ?? 1;\n if (sampleRate < 1) {\n // Use a separate hash-derived value for sampling so it's independent of variant assignment.\n const sampleBucket =\n bucketFor(`${args.experimentId}:sample`, args.salt, args.subjectId) / BUCKET_PRECISION;\n if (sampleBucket >= sampleRate) {\n return { variantKey: null, bucket, reason: 'ineligible' };\n }\n }\n\n const totalDeclared = args.variants.reduce((s, v) => s + (v.weight ?? 0), 0);\n if (totalDeclared > 0) {\n if (totalDeclared !== BUCKET_PRECISION) {\n throw new Error(\n `experiment ${args.experimentId}: variant weights must sum to ${BUCKET_PRECISION}, got ${totalDeclared}`,\n );\n }\n let cumulative = 0;\n for (const v of args.variants) {\n cumulative += v.weight ?? 0;\n if (bucket < cumulative) return { variantKey: v.key, bucket, reason: 'eligible' };\n }\n // Numerical safety net.\n return {\n variantKey: args.variants[args.variants.length - 1]!.key,\n bucket,\n reason: 'eligible',\n };\n }\n\n // Even split.\n const idx = Math.floor((bucket / BUCKET_PRECISION) * args.variants.length);\n const safeIdx = Math.min(idx, args.variants.length - 1);\n return { variantKey: args.variants[safeIdx]!.key, bucket, reason: 'eligible' };\n}\n"]}
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@fabricorg/experiments-assignment",
3
+ "version": "0.0.1",
4
+ "description": "Deterministic, cross-language bucketing primitives for Fabric Experiments SDKs.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "sideEffects": false,
8
+ "main": "./dist/index.cjs",
9
+ "types": "./dist/index.d.ts",
10
+ "module": "./dist/index.js",
11
+ "exports": {
12
+ ".": {
13
+ "import": {
14
+ "types": "./dist/index.d.ts",
15
+ "default": "./dist/index.js"
16
+ },
17
+ "require": {
18
+ "types": "./dist/index.d.cts",
19
+ "default": "./dist/index.cjs"
20
+ }
21
+ }
22
+ },
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "devDependencies": {
27
+ "fast-check": "^3.23.2",
28
+ "tsup": "^8.3.5",
29
+ "typescript": "^5.8.3",
30
+ "vitest": "^2.1.8"
31
+ },
32
+ "scripts": {
33
+ "build": "tsup",
34
+ "type-check": "tsc --noEmit",
35
+ "test": "vitest run",
36
+ "clean": "rm -rf dist"
37
+ }
38
+ }