@lp-labs/experiment-assignment
v0.1.0
Published
Bounded deterministic experiment-assignment utilities with explicit validation and zero runtime dependencies.
Readme
@lp-labs/experiment-assignment
Deterministic versioned experiment buckets, explicit exclusions, bounded allocation and variant weights. Original independent implementation, zero runtime/peer dependencies. Node >=18, version 0.1.0. Assignment does not provide statistical or causal guarantees.
npm install @lp-labs/experiment-assignmentRunnable example
import { createExperimentAssigner, assignmentBucket } from '@lp-labs/experiment-assignment';
const assigner = createExperimentAssigner({
key: 'checkout',
version: '1',
allocation: 10000,
variants: [
{ key: 'control', weight: 5000 },
{ key: 'treatment', weight: 5000 },
],
excludedSubjects: ['staff'],
});
const assignment = assigner.assign('customer-1');
if (assignment.reason !== 'assigned' || assigner.assign('staff').variant !== null)
throw new Error('Unexpected assignment');
console.log(assignment, assignmentBucket('customer-1', 'checkout', '1'));CommonJS: require('@lp-labs/experiment-assignment'). ESM/CommonJS runtime exports
are identical, with separate matching declarations.
API and types
VariantAllocation:{ key: string; weight: number }, positive integer basis points.ExperimentDefinition:key, explicitversion,allocation(integer basis points 0–10000),variants(ordered array, weights sum exactly 10000), optionalexcludedSubjects(string array, duplicate exclusions are harmless).Assignment:experiment,version,bucket(integer 0–9999),variant(variant key or null),reason: 'assigned' | 'excluded' | 'unallocated'.createExperimentAssigner(definition: ExperimentDefinition): ExperimentAssigner: validate/copy a fixed configuration, no retained request or random state.ExperimentAssigner.assign(subject: string): Assignment: excludes configured subjects first, then requiresbucket < allocation; variant weights partition the allocated interval in input order. First cumulative weight satisfyingbucket * 10000 < cumulativeWeight * allocationwins. Integer arithmetic avoids rounding boundaries; small allocations may leave a variant with zero reachable buckets. Returns a fresh result on each call, regardless of request ordering.assignmentBucket(subject, experiment, version): number: FNV-1a uint32 over UTF-16 code units of length-prefixed['assignment-v1', experiment, version, subject]; concatenate${part.length}:${part}, initial 2166136261, multiplier 16777619, modulo 10000. The algorithm marker is fixed independently of your explicit version.
Four named interfaces (VariantAllocation, ExperimentDefinition, Assignment,
ExperimentAssigner) are exported types; the factory and hash are runtime exports.
Errors, customization and limits
TypeError: empty/nonstrings/over-128-UTF-16-unit identifiers (including subject/version),
duplicate variant keys. RangeError: noninteger/out-of-range allocation or weights,
weight sum !=10000, zero or >100 variants, nonarray variants/exclusions, >100000
exclusions. Configuration is validated before any assigner is returned; no partial
state or external input mutation. Ordinary trusted config data is required;
iterator/getter failures propagate.
Customize version, experiment namespace, variant order/weights, allocation, exclusions, and subject normalization upstream. Increasing allocation preserves admission, but can change variants, since variant intervals scale with allocation. Changing weights, order, version, or key can also reassign subjects. Fixed configuration gives stable assignment; there is no persisted sticky state or cross-experiment mutual exclusion.
Modulo bias and hash collisions exist; hash is not cryptographic and client-chosen subjects are manipulable. No authentication/authorization boundary, privacy protection, exposure logging, statistical significance, power analysis, population balance, causal inference, or experiment-outcome guarantee. Exclusion lists are explicit inputs, not verified eligibility.
Factory cost/storage O(V + X); assignment O(subject length + V), with O(1) exclusion lookup on average. V <=100, X <=100000. Output does not expose exclusion lists. Related design context: virtual-time determinism, performance-budgets honest limitations, incremental-aggregation snapshots, unit-system explicit numeric semantics. No imports shared.
Development
npm run build, npm run typecheck, npm test, npm run format, npm run format:check.
TypeScript ^5.9.3 and Prettier ^3.6.2 dev-only; strict ESM/CommonJS declarations.
Tests cover seeded independent bucket oracles, integer allocation boundaries,
exclusions, reordered requests, mutation races and full interval partition checks.
Support
If this library is useful to you, consider supporting its maintenance with a donation via PayPal.
Author
Created by Libin Prasanth. Connect on LinkedIn for updates and discussion.
