@vllnt/convex-experiments 0.1.0-canary.207ab91

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.
Files changed (65) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +132 -0
  3. package/dist/client/index.d.ts +117 -0
  4. package/dist/client/index.d.ts.map +1 -0
  5. package/dist/client/index.js +105 -0
  6. package/dist/client/index.js.map +1 -0
  7. package/dist/client/types.d.ts +70 -0
  8. package/dist/client/types.d.ts.map +1 -0
  9. package/dist/client/types.js +3 -0
  10. package/dist/client/types.js.map +1 -0
  11. package/dist/component/_generated/api.d.ts +38 -0
  12. package/dist/component/_generated/api.d.ts.map +1 -0
  13. package/dist/component/_generated/api.js +31 -0
  14. package/dist/component/_generated/api.js.map +1 -0
  15. package/dist/component/_generated/component.d.ts +93 -0
  16. package/dist/component/_generated/component.d.ts.map +1 -0
  17. package/dist/component/_generated/component.js +11 -0
  18. package/dist/component/_generated/component.js.map +1 -0
  19. package/dist/component/_generated/dataModel.d.ts +46 -0
  20. package/dist/component/_generated/dataModel.d.ts.map +1 -0
  21. package/dist/component/_generated/dataModel.js +11 -0
  22. package/dist/component/_generated/dataModel.js.map +1 -0
  23. package/dist/component/_generated/server.d.ts +121 -0
  24. package/dist/component/_generated/server.d.ts.map +1 -0
  25. package/dist/component/_generated/server.js +78 -0
  26. package/dist/component/_generated/server.js.map +1 -0
  27. package/dist/component/convex.config.d.ts +3 -0
  28. package/dist/component/convex.config.d.ts.map +1 -0
  29. package/dist/component/convex.config.js +7 -0
  30. package/dist/component/convex.config.js.map +1 -0
  31. package/dist/component/mutations.d.ts +67 -0
  32. package/dist/component/mutations.d.ts.map +1 -0
  33. package/dist/component/mutations.js +196 -0
  34. package/dist/component/mutations.js.map +1 -0
  35. package/dist/component/queries.d.ts +39 -0
  36. package/dist/component/queries.d.ts.map +1 -0
  37. package/dist/component/queries.js +72 -0
  38. package/dist/component/queries.js.map +1 -0
  39. package/dist/component/schema.d.ts +77 -0
  40. package/dist/component/schema.d.ts.map +1 -0
  41. package/dist/component/schema.js +49 -0
  42. package/dist/component/schema.js.map +1 -0
  43. package/dist/component/validators.d.ts +96 -0
  44. package/dist/component/validators.d.ts.map +1 -0
  45. package/dist/component/validators.js +38 -0
  46. package/dist/component/validators.js.map +1 -0
  47. package/dist/shared.d.ts +30 -0
  48. package/dist/shared.d.ts.map +1 -0
  49. package/dist/shared.js +50 -0
  50. package/dist/shared.js.map +1 -0
  51. package/package.json +98 -0
  52. package/src/client/index.ts +249 -0
  53. package/src/client/types.ts +73 -0
  54. package/src/component/_generated/api.ts +54 -0
  55. package/src/component/_generated/component.ts +92 -0
  56. package/src/component/_generated/dataModel.ts +60 -0
  57. package/src/component/_generated/server.ts +156 -0
  58. package/src/component/convex.config.ts +9 -0
  59. package/src/component/mutations.ts +224 -0
  60. package/src/component/queries.ts +84 -0
  61. package/src/component/schema.ts +55 -0
  62. package/src/component/validators.ts +54 -0
  63. package/src/shared.ts +66 -0
  64. package/src/test.ts +12 -0
  65. package/src/unit.test.ts +84 -0
@@ -0,0 +1,72 @@
1
+ import { v } from "convex/values";
2
+ import { query } from "./_generated/server";
3
+ import { assignmentProjection, experimentDef, variantResult, } from "./validators";
4
+ /** The experiment definition for `(scope, key)`, or `null` if none exists. */
5
+ export const getExperiment = query({
6
+ args: { scope: v.string(), key: v.string() },
7
+ returns: v.union(v.null(), experimentDef),
8
+ handler: async (ctx, args) => {
9
+ const row = await ctx.db
10
+ .query("experiments")
11
+ .withIndex("by_scope_key", (q) => q.eq("scope", args.scope).eq("key", args.key))
12
+ .unique();
13
+ if (row === null) {
14
+ return null;
15
+ }
16
+ return {
17
+ key: row.key,
18
+ scope: row.scope,
19
+ status: row.status,
20
+ variants: row.variants,
21
+ salt: row.salt,
22
+ createdAt: row.createdAt,
23
+ };
24
+ },
25
+ });
26
+ /** A subject's sticky assignment for an experiment, or `null` if not enrolled. */
27
+ export const getAssignment = query({
28
+ args: { scope: v.string(), key: v.string(), subjectRef: v.string() },
29
+ returns: v.union(v.null(), assignmentProjection),
30
+ handler: async (ctx, args) => {
31
+ const row = await ctx.db
32
+ .query("assignments")
33
+ .withIndex("by_scope_experiment_subject", (q) => q
34
+ .eq("scope", args.scope)
35
+ .eq("experimentKey", args.key)
36
+ .eq("subjectRef", args.subjectRef))
37
+ .unique();
38
+ if (row === null) {
39
+ return null;
40
+ }
41
+ return { variant: row.variant, assignedAt: row.assignedAt };
42
+ },
43
+ });
44
+ /**
45
+ * Per-variant exposure tallies for an experiment: distinct `subjects` exposed and
46
+ * total `exposures`. Reads the deduped exposure rows via the `by_scope_experiment`
47
+ * index and groups them in memory (one indexed scan, no per-row queries). An
48
+ * experiment with no exposures returns an empty array.
49
+ */
50
+ export const results = query({
51
+ args: { scope: v.string(), key: v.string() },
52
+ returns: v.array(variantResult),
53
+ handler: async (ctx, args) => {
54
+ const rows = await ctx.db
55
+ .query("exposures")
56
+ .withIndex("by_scope_experiment", (q) => q.eq("scope", args.scope).eq("experimentKey", args.key))
57
+ .collect();
58
+ const byVariant = new Map();
59
+ for (const row of rows) {
60
+ const agg = byVariant.get(row.variant) ?? { subjects: 0, exposures: 0 };
61
+ agg.subjects += 1;
62
+ agg.exposures += row.count;
63
+ byVariant.set(row.variant, agg);
64
+ }
65
+ return Array.from(byVariant, ([variantKey, agg]) => ({
66
+ variant: variantKey,
67
+ subjects: agg.subjects,
68
+ exposures: agg.exposures,
69
+ }));
70
+ },
71
+ });
72
+ //# sourceMappingURL=queries.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queries.js","sourceRoot":"","sources":["../../src/component/queries.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAClC,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAC5C,OAAO,EACL,oBAAoB,EACpB,aAAa,EACb,aAAa,GACd,MAAM,cAAc,CAAC;AAEtB,8EAA8E;AAC9E,MAAM,CAAC,M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IAAI,IAAI,EAAE,CAAC;YACvB,MAAM,GAAG,GAAG,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,QAAQ,EAAE,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,CAAC;YACxE,GAAG,CAAC,QAAQ,IAAI,CAAC,CAAC;YAClB,GAAG,CAAC,SAAS,IAAI,GAAG,CAAC,KAAK,CAAC;YAC3B,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;QAClC,CAAC;QACD,OAAO,KAAK,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC,CAAC,UAAU,EAAE,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC;YACnD,OAAO,EAAE,UAAU;YACnB,QAAQ,EAAE,GAAG,CAAC,QAAQ;YACtB,SAAS,EAAE,GAAG,CAAC,SAAS;SACzB,CAAC,CAAC,CAAC;IACN,CAAC;CACF,CAAC,CAAC"}
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Sandboxed tables — the experimentation ledger's own concern. No host tables are
3
+ * touched. An experiment is unique within a `scope`; `subjectRef`, variant keys,
4
+ * and the scope are all opaque host-owned strings.
5
+ *
6
+ * - `experiments` — the experiment definition (variants, weights, lifecycle, salt).
7
+ * - `assignments` — a subject's sticky variant choice, persisted on first `assign`.
8
+ * - `exposures` — a deduped per-subject exposure tally (the measurable surface).
9
+ */
10
+ declare const _default: import("convex/server").SchemaDefinition<{
11
+ experiments: import("convex/server").TableDefinition<import("convex/values").VObject<{
12
+ scope: string;
13
+ key: string;
14
+ variants: {
15
+ key: string;
16
+ weight: number;
17
+ }[];
18
+ salt: string;
19
+ status: "running" | "draft" | "stopped";
20
+ createdAt: number;
21
+ }, {
22
+ key: import("convex/values").VString<string, "required">;
23
+ scope: import("convex/values").VString<string, "required">;
24
+ status: import("convex/values").VUnion<"running" | "draft" | "stopped", [import("convex/values").VLiteral<"draft", "required">, import("convex/values").VLiteral<"running", "required">, import("convex/values").VLiteral<"stopped", "required">], "required", never>;
25
+ variants: import("convex/values").VArray<{
26
+ key: string;
27
+ weight: number;
28
+ }[], import("convex/values").VObject<{
29
+ key: string;
30
+ weight: number;
31
+ }, {
32
+ key: import("convex/values").VString<string, "required">;
33
+ weight: import("convex/values").VFloat64<number, "required">;
34
+ }, "required", "key" | "weight">, "required">;
35
+ salt: import("convex/values").VString<string, "required">;
36
+ createdAt: import("convex/values").VFloat64<number, "required">;
37
+ }, "required", "scope" | "key" | "variants" | "salt" | "status" | "createdAt">, {
38
+ by_scope_key: ["scope", "key", "_creationTime"];
39
+ }, {}, {}>;
40
+ assignments: import("convex/server").TableDefinition<import("convex/values").VObject<{
41
+ scope: string;
42
+ subjectRef: string;
43
+ variant: string;
44
+ assignedAt: number;
45
+ experimentKey: string;
46
+ }, {
47
+ scope: import("convex/values").VString<string, "required">;
48
+ experimentKey: import("convex/values").VString<string, "required">;
49
+ subjectRef: import("convex/values").VString<string, "required">;
50
+ variant: import("convex/values").VString<string, "required">;
51
+ assignedAt: import("convex/values").VFloat64<number, "required">;
52
+ }, "required", "scope" | "subjectRef" | "variant" | "assignedAt" | "experimentKey">, {
53
+ by_scope_experiment_subject: ["scope", "experimentKey", "subjectRef", "_creationTime"];
54
+ }, {}, {}>;
55
+ exposures: import("convex/server").TableDefinition<import("convex/values").VObject<{
56
+ scope: string;
57
+ subjectRef: string;
58
+ variant: string;
59
+ experimentKey: string;
60
+ firstExposedAt: number;
61
+ lastExposedAt: number;
62
+ count: number;
63
+ }, {
64
+ scope: import("convex/values").VString<string, "required">;
65
+ experimentKey: import("convex/values").VString<string, "required">;
66
+ subjectRef: import("convex/values").VString<string, "required">;
67
+ variant: import("convex/values").VString<string, "required">;
68
+ firstExposedAt: import("convex/values").VFloat64<number, "required">;
69
+ lastExposedAt: import("convex/values").VFloat64<number, "required">;
70
+ count: import("convex/values").VFloat64<number, "required">;
71
+ }, "required", "scope" | "subjectRef" | "variant" | "experimentKey" | "firstExposedAt" | "lastExposedAt" | "count">, {
72
+ by_scope_experiment_subject: ["scope", "experimentKey", "subjectRef", "_creationTime"];
73
+ by_scope_experiment: ["scope", "experimentKey", "_creationTime"];
74
+ }, {}, {}>;
75
+ }, true>;
76
+ export default _default;
77
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/component/schema.ts"],"names":[],"mappings":"AAIA;;;;;;;;GAQG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACH,wBAyCG"}
@@ -0,0 +1,49 @@
1
+ import { defineSchema, defineTable } from "convex/server";
2
+ import { v } from "convex/values";
3
+ import { variant } from "./validators";
4
+ /**
5
+ * Sandboxed tables — the experimentation ledger's own concern. No host tables are
6
+ * touched. An experiment is unique within a `scope`; `subjectRef`, variant keys,
7
+ * and the scope are all opaque host-owned strings.
8
+ *
9
+ * - `experiments` — the experiment definition (variants, weights, lifecycle, salt).
10
+ * - `assignments` — a subject's sticky variant choice, persisted on first `assign`.
11
+ * - `exposures` — a deduped per-subject exposure tally (the measurable surface).
12
+ */
13
+ export default defineSchema({
14
+ experiments: defineTable({
15
+ key: v.string(),
16
+ scope: v.string(),
17
+ status: v.union(v.literal("draft"), v.literal("running"), v.literal("stopped")),
18
+ variants: v.array(variant),
19
+ salt: v.string(),
20
+ createdAt: v.number(),
21
+ }).index("by_scope_key", ["scope", "key"]),
22
+ assignments: defineTable({
23
+ scope: v.string(),
24
+ experimentKey: v.string(),
25
+ subjectRef: v.string(),
26
+ variant: v.string(),
27
+ assignedAt: v.number(),
28
+ }).index("by_scope_experiment_subject", [
29
+ "scope",
30
+ "experimentKey",
31
+ "subjectRef",
32
+ ]),
33
+ exposures: defineTable({
34
+ scope: v.string(),
35
+ experimentKey: v.string(),
36
+ subjectRef: v.string(),
37
+ variant: v.string(),
38
+ firstExposedAt: v.number(),
39
+ lastExposedAt: v.number(),
40
+ count: v.number(),
41
+ })
42
+ .index("by_scope_experiment_subject", [
43
+ "scope",
44
+ "experimentKey",
45
+ "subjectRef",
46
+ ])
47
+ .index("by_scope_experiment", ["scope", "experimentKey"]),
48
+ });
49
+ //# sourceMappingURL=schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/component/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAClC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC;;;;;;;;GAQG;AACH,eAAe,YAAY,CAAC;IAC1B,WAAW,EAAE,WAAW,CAAC;QACvB,GAAG,EAAE,CAAC,CAAC,MAAM,EAAE;QACf,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;QACjB,MAAM,EAAE,CAAC,CAAC,KAAK,CACb,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,EAClB,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,EACpB,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,CACrB;QACD,QAAQ,EAAE,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC;QAC1B,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE;QAChB,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;KACtB,CAAC,CAAC,KAAK,CAAC,cAAc,EAAE,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;IAE1C,WAAW,EAAE,WAAW,CAAC;QACvB,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;QACjB,aAAa,EAAE,CAAC,CAAC,MAAM,EAAE;QACzB,UAAU,EAAE,CAAC,CAAC,MAAM,EAAE;QACtB,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE;QACnB,UAAU,EAAE,CAAC,CAAC,MAAM,EAAE;KACvB,CAAC,CAAC,KAAK,CAAC,6BAA6B,EAAE;QACtC,OAAO;QACP,eAAe;QACf,YAAY;KACb,CAAC;IAEF,SAAS,EAAE,WAAW,CAAC;QACrB,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;QACjB,aAAa,EAAE,CAAC,CAAC,MAAM,EAAE;QACzB,UAAU,EAAE,CAAC,CAAC,MAAM,EAAE;QACtB,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE;QACnB,cAAc,EAAE,CAAC,CAAC,MAAM,EAAE;QAC1B,aAAa,EAAE,CAAC,CAAC,MAAM,EAAE;QACzB,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;KAClB,CAAC;SACC,KAAK,CAAC,6BAA6B,EAAE;QACpC,OAAO;QACP,eAAe;QACf,YAAY;KACb,CAAC;SACD,KAAK,CAAC,qBAAqB,EAAE,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC;CAC5D,CAAC,CAAC"}
@@ -0,0 +1,96 @@
1
+ /** A weighted variant — an opaque variant key and its relative selection weight. */
2
+ export declare const variant: import("convex/values").VObject<{
3
+ key: string;
4
+ weight: number;
5
+ }, {
6
+ key: import("convex/values").VString<string, "required">;
7
+ weight: import("convex/values").VFloat64<number, "required">;
8
+ }, "required", "key" | "weight">;
9
+ /** Experiment lifecycle status. `running` is the only status that enrolls subjects. */
10
+ export declare const experimentStatus: import("convex/values").VUnion<"running" | "draft" | "stopped", [import("convex/values").VLiteral<"draft", "required">, import("convex/values").VLiteral<"running", "required">, import("convex/values").VLiteral<"stopped", "required">], "required", never>;
11
+ /**
12
+ * Result of `assign`. `{ variant: null }` — the experiment is absent or not
13
+ * `running`, so the subject is not enrolled (the host treats this as control).
14
+ * `{ variant, isNew }` — the enrolled variant key, with `isNew` true only on the
15
+ * call that first persisted the assignment.
16
+ */
17
+ export declare const assignResult: import("convex/values").VUnion<{
18
+ variant: null;
19
+ } | {
20
+ variant: string;
21
+ isNew: boolean;
22
+ }, [import("convex/values").VObject<{
23
+ variant: null;
24
+ }, {
25
+ variant: import("convex/values").VNull<null, "required">;
26
+ }, "required", "variant">, import("convex/values").VObject<{
27
+ variant: string;
28
+ isNew: boolean;
29
+ }, {
30
+ variant: import("convex/values").VString<string, "required">;
31
+ isNew: import("convex/values").VBoolean<boolean, "required">;
32
+ }, "required", "variant" | "isNew">], "required", "variant" | "isNew">;
33
+ /** Result of `logExposure`: the enrolled variant, or `null` when not enrolled. */
34
+ export declare const exposureResult: import("convex/values").VUnion<{
35
+ variant: null;
36
+ } | {
37
+ variant: string;
38
+ }, [import("convex/values").VObject<{
39
+ variant: null;
40
+ }, {
41
+ variant: import("convex/values").VNull<null, "required">;
42
+ }, "required", "variant">, import("convex/values").VObject<{
43
+ variant: string;
44
+ }, {
45
+ variant: import("convex/values").VString<string, "required">;
46
+ }, "required", "variant">], "required", "variant">;
47
+ /** Projection of an experiment definition returned by `getExperiment`. */
48
+ export declare const experimentDef: import("convex/values").VObject<{
49
+ scope: string;
50
+ key: string;
51
+ variants: {
52
+ key: string;
53
+ weight: number;
54
+ }[];
55
+ salt: string;
56
+ status: "running" | "draft" | "stopped";
57
+ createdAt: number;
58
+ }, {
59
+ key: import("convex/values").VString<string, "required">;
60
+ scope: import("convex/values").VString<string, "required">;
61
+ status: import("convex/values").VUnion<"running" | "draft" | "stopped", [import("convex/values").VLiteral<"draft", "required">, import("convex/values").VLiteral<"running", "required">, import("convex/values").VLiteral<"stopped", "required">], "required", never>;
62
+ variants: import("convex/values").VArray<{
63
+ key: string;
64
+ weight: number;
65
+ }[], import("convex/values").VObject<{
66
+ key: string;
67
+ weight: number;
68
+ }, {
69
+ key: import("convex/values").VString<string, "required">;
70
+ weight: import("convex/values").VFloat64<number, "required">;
71
+ }, "required", "key" | "weight">, "required">;
72
+ salt: import("convex/values").VString<string, "required">;
73
+ createdAt: import("convex/values").VFloat64<number, "required">;
74
+ }, "required", "scope" | "key" | "variants" | "salt" | "status" | "createdAt">;
75
+ /** Projection of a subject's sticky assignment returned by `getAssignment`. */
76
+ export declare const assignmentProjection: import("convex/values").VObject<{
77
+ variant: string;
78
+ assignedAt: number;
79
+ }, {
80
+ variant: import("convex/values").VString<string, "required">;
81
+ assignedAt: import("convex/values").VFloat64<number, "required">;
82
+ }, "required", "variant" | "assignedAt">;
83
+ /** Per-variant exposure tally returned by `results`. */
84
+ export declare const variantResult: import("convex/values").VObject<{
85
+ variant: string;
86
+ subjects: number;
87
+ exposures: number;
88
+ }, {
89
+ /** The variant key. */
90
+ variant: import("convex/values").VString<string, "required">;
91
+ /** Distinct subjects exposed to this variant (the funnel denominator). */
92
+ subjects: import("convex/values").VFloat64<number, "required">;
93
+ /** Total exposure events recorded for this variant. */
94
+ exposures: import("convex/values").VFloat64<number, "required">;
95
+ }, "required", "variant" | "subjects" | "exposures">;
96
+ //# sourceMappingURL=validators.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validators.d.ts","sourceRoot":"","sources":["../../src/component/validators.ts"],"names":[],"mappings":"AAEA,oFAAoF;AACpF,eAAO,MAAM,OAAO;;;;;;gCAAoD,CAAC;AAEzE,uFAAuF;AACvF,eAAO,MAAM,gBAAgB,+PAI5B,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,YAAY;;;;;;;;;;;;;;;sEAGxB,CAAC;AAEF,kFAAkF;AAClF,eAAO,MAAM,cAAc;;;;;;;;;;;;kDAG1B,CAAC;AAEF,0EAA0E;AAC1E,eAAO,MAAM,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;8EAOxB,CAAC;AAEH,+EAA+E;AAC/E,eAAO,MAAM,oBAAoB;;;;;;wCAG/B,CAAC;AAEH,wDAAwD;AACxD,eAAO,MAAM,aAAa;;;;;IACxB,uBAAuB;;IAEvB,0EAA0E;;IAE1E,uDAAuD;;oDAEvD,CAAC"}
@@ -0,0 +1,38 @@
1
+ import { v } from "convex/values";
2
+ /** A weighted variant — an opaque variant key and its relative selection weight. */
3
+ export const variant = v.object({ key: v.string(), weight: v.number() });
4
+ /** Experiment lifecycle status. `running` is the only status that enrolls subjects. */
5
+ export const experimentStatus = v.union(v.literal("draft"), v.literal("running"), v.literal("stopped"));
6
+ /**
7
+ * Result of `assign`. `{ variant: null }` — the experiment is absent or not
8
+ * `running`, so the subject is not enrolled (the host treats this as control).
9
+ * `{ variant, isNew }` — the enrolled variant key, with `isNew` true only on the
10
+ * call that first persisted the assignment.
11
+ */
12
+ export const assignResult = v.union(v.object({ variant: v.null() }), v.object({ variant: v.string(), isNew: v.boolean() }));
13
+ /** Result of `logExposure`: the enrolled variant, or `null` when not enrolled. */
14
+ export const exposureResult = v.union(v.object({ variant: v.null() }), v.object({ variant: v.string() }));
15
+ /** Projection of an experiment definition returned by `getExperiment`. */
16
+ export const experimentDef = v.object({
17
+ key: v.string(),
18
+ scope: v.string(),
19
+ status: experimentStatus,
20
+ variants: v.array(variant),
21
+ salt: v.string(),
22
+ createdAt: v.number(),
23
+ });
24
+ /** Projection of a subject's sticky assignment returned by `getAssignment`. */
25
+ export const assignmentProjection = v.object({
26
+ variant: v.string(),
27
+ assignedAt: v.number(),
28
+ });
29
+ /** Per-variant exposure tally returned by `results`. */
30
+ export const variantResult = v.object({
31
+ /** The variant key. */
32
+ variant: v.string(),
33
+ /** Distinct subjects exposed to this variant (the funnel denominator). */
34
+ subjects: v.number(),
35
+ /** Total exposure events recorded for this variant. */
36
+ exposures: v.number(),
37
+ });
38
+ //# sourceMappingURL=validators.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validators.js","sourceRoot":"","sources":["../../src/component/validators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAElC,oFAAoF;AACpF,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,CAAC,MAAM,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,MAAM,EAAE,EAAE,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;AAEzE,uFAAuF;AACvF,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,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D;AACxD,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,CAAC,MAAM,CAAC;IACpC,uBAAuB;IACvB,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE;IACnB,0EAA0E;IAC1E,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE;IACpB,uDAAuD;IACvD,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;CACtB,CAAC,CAAC"}
@@ -0,0 +1,30 @@
1
+ /** Shared constants + pure deterministic assignment used by both `client/` and `component/`. */
2
+ export declare const COMPONENT_NAME = "experiments";
3
+ /** Default namespace when the host does not scope an experiment. */
4
+ export declare const DEFAULT_SCOPE = "global";
5
+ /** Lifecycle status the client applies to `define` when the caller omits one. */
6
+ export declare const DEFAULT_STATUS = "running";
7
+ /** A weighted variant: an opaque variant key and its relative selection weight. */
8
+ export interface Variant {
9
+ /** The host's opaque variant key (e.g. `"control"`, `"treatment"`). */
10
+ key: string;
11
+ /** Relative selection weight; the bucket width is `weight / sum(weights)`. */
12
+ weight: number;
13
+ }
14
+ /**
15
+ * FNV-1a 32-bit hash of `input`, returned as an unsigned integer. Deterministic,
16
+ * synchronous, and dependency-free — the component runs in a V8 isolate, so a
17
+ * pure string hash (rather than async `crypto.subtle`) is what keeps variant
18
+ * assignment a plain function callable inside a mutation.
19
+ */
20
+ export declare function fnv1a(input: string): number;
21
+ /**
22
+ * Deterministically pick a variant for `subjectRef` from `variants`, weighted by
23
+ * each variant's `weight`. The same `(salt, subjectRef)` always maps to the same
24
+ * variant — sticky assignment with no storage — and the distribution matches the
25
+ * weights. `salt` lets a host re-randomize an experiment (a fresh re-run) without
26
+ * changing subject ids. Assumes a non-empty `variants` with positive weights; the
27
+ * caller validates that upstream.
28
+ */
29
+ export declare function pickVariant(variants: readonly Variant[], salt: string, subjectRef: string): string;
30
+ //# sourceMappingURL=shared.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"AAAA,gGAAgG;AAEhG,eAAO,MAAM,cAAc,gBAAgB,CAAC;AAE5C,oEAAoE;AACpE,eAAO,MAAM,aAAa,WAAW,CAAC;AAEtC,iFAAiF;AACjF,eAAO,MAAM,cAAc,YAAY,CAAC;AAExC,mFAAmF;AACnF,MAAM,WAAW,OAAO;IACtB,uEAAuE;IACvE,GAAG,EAAE,MAAM,CAAC;IACZ,8EAA8E;IAC9E,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;;;;GAKG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAO3C;AAED;;;;;;;GAOG;AACH,wBAAgB,WAAW,CACzB,QAAQ,EAAE,SAAS,OAAO,EAAE,EAC5B,IAAI,EAAE,MAAM,EACZ,UAAU,EAAE,MAAM,GACjB,MAAM,CAoBR"}
package/dist/shared.js ADDED
@@ -0,0 +1,50 @@
1
+ /** Shared constants + pure deterministic assignment used by both `client/` and `component/`. */
2
+ export const COMPONENT_NAME = "experiments";
3
+ /** Default namespace when the host does not scope an experiment. */
4
+ export const DEFAULT_SCOPE = "global";
5
+ /** Lifecycle status the client applies to `define` when the caller omits one. */
6
+ export const DEFAULT_STATUS = "running";
7
+ /**
8
+ * FNV-1a 32-bit hash of `input`, returned as an unsigned integer. Deterministic,
9
+ * synchronous, and dependency-free — the component runs in a V8 isolate, so a
10
+ * pure string hash (rather than async `crypto.subtle`) is what keeps variant
11
+ * assignment a plain function callable inside a mutation.
12
+ */
13
+ export function fnv1a(input) {
14
+ let hash = 0x811c9dc5;
15
+ for (let i = 0; i < input.length; i++) {
16
+ hash ^= input.charCodeAt(i);
17
+ hash = Math.imul(hash, 0x01000193);
18
+ }
19
+ return hash >>> 0;
20
+ }
21
+ /**
22
+ * Deterministically pick a variant for `subjectRef` from `variants`, weighted by
23
+ * each variant's `weight`. The same `(salt, subjectRef)` always maps to the same
24
+ * variant — sticky assignment with no storage — and the distribution matches the
25
+ * weights. `salt` lets a host re-randomize an experiment (a fresh re-run) without
26
+ * changing subject ids. Assumes a non-empty `variants` with positive weights; the
27
+ * caller validates that upstream.
28
+ */
29
+ export function pickVariant(variants, salt, subjectRef) {
30
+ const lastVariant = variants[variants.length - 1];
31
+ if (lastVariant === undefined) {
32
+ throw new Error("pickVariant requires at least one variant");
33
+ }
34
+ let total = 0;
35
+ for (const variant of variants) {
36
+ total += variant.weight;
37
+ }
38
+ // Map the hash into [0, total): a point on the cumulative-weight line.
39
+ let target = (fnv1a(`${salt}:${subjectRef}`) / 0x100000000) * total;
40
+ // Walk every variant but the last, subtracting its bucket width.
41
+ for (const variant of variants.slice(0, -1)) {
42
+ target -= variant.weight;
43
+ if (target < 0) {
44
+ return variant.key;
45
+ }
46
+ }
47
+ // The point fell in the final variant's bucket (also the single-variant case).
48
+ return lastVariant.key;
49
+ }
50
+ //# sourceMappingURL=shared.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared.js","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"AAAA,gGAAgG;AAEhG,MAAM,CAAC,MAAM,cAAc,GAAG,aAAa,CAAC;AAE5C,oEAAoE;AACpE,MAAM,CAAC,MAAM,aAAa,GAAG,QAAQ,CAAC;AAEtC,iFAAiF;AACjF,MAAM,CAAC,MAAM,cAAc,GAAG,SAAS,CAAC;AAUxC;;;;;GAKG;AACH,MAAM,UAAU,KAAK,CAAC,KAAa;IACjC,IAAI,IAAI,GAAG,UAAU,CAAC;IACtB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACtC,IAAI,IAAI,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;QAC5B,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;IACrC,CAAC;IACD,OAAO,IAAI,KAAK,CAAC,CAAC;AACpB,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,WAAW,CACzB,QAA4B,EAC5B,IAAY,EACZ,UAAkB;IAElB,MAAM,WAAW,GAAG,QAAQ,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IAClD,IAAI,WAAW,KAAK,SAAS,EAAE,CAAC;QAC9B,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;IAC/D,CAAC;IACD,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;QAC/B,KAAK,IAAI,OAAO,CAAC,MAAM,CAAC;IAC1B,CAAC;IACD,uEAAuE;IACvE,IAAI,MAAM,GAAG,CAAC,KAAK,CAAC,GAAG,IAAI,IAAI,UAAU,EAAE,CAAC,GAAG,WAAW,CAAC,GAAG,KAAK,CAAC;IACpE,iEAAiE;IACjE,KAAK,MAAM,OAAO,IAAI,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC5C,MAAM,IAAI,OAAO,CAAC,MAAM,CAAC;QACzB,IAAI,MAAM,GAAG,CAAC,EAAE,CAAC;YACf,OAAO,OAAO,CAAC,GAAG,CAAC;QACrB,CAAC;IACH,CAAC;IACD,+EAA+E;IAC/E,OAAO,WAAW,CAAC,GAAG,CAAC;AACzB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,98 @@
1
+ {
2
+ "name": "@vllnt/convex-experiments",
3
+ "version": "0.1.0-canary.207ab91",
4
+ "description": "Deterministic A/B experimentation — sticky variant assignment and deduped exposure tracking, as a Convex component",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "packageManager": "pnpm@9.15.4",
8
+ "files": [
9
+ "dist",
10
+ "src"
11
+ ],
12
+ "exports": {
13
+ "./package.json": "./package.json",
14
+ ".": {
15
+ "types": "./dist/client/index.d.ts",
16
+ "default": "./dist/client/index.js"
17
+ },
18
+ "./test": "./src/test.ts",
19
+ "./_generated/component.js": {
20
+ "types": "./dist/component/_generated/component.d.ts"
21
+ },
22
+ "./_generated/component": {
23
+ "types": "./dist/component/_generated/component.d.ts"
24
+ },
25
+ "./convex.config": {
26
+ "types": "./dist/component/convex.config.d.ts",
27
+ "default": "./dist/component/convex.config.js"
28
+ },
29
+ "./convex.config.js": {
30
+ "types": "./dist/component/convex.config.d.ts",
31
+ "default": "./dist/component/convex.config.js"
32
+ }
33
+ },
34
+ "types": "./dist/client/index.d.ts",
35
+ "module": "./dist/client/index.js",
36
+ "scripts": {
37
+ "build": "tsc --project ./tsconfig.build.json",
38
+ "build:codegen": "pnpm convex codegen --component-dir ./src/component && pnpm build",
39
+ "build:clean": "rm -rf dist *.tsbuildinfo && pnpm build:codegen",
40
+ "typecheck": "tsc --noEmit",
41
+ "typecheck:ci": "tsc --noEmit --project tsconfig.ci.json",
42
+ "lint": "eslint .",
43
+ "test": "vitest run --passWithNoTests",
44
+ "test:watch": "vitest --typecheck --clearScreen false",
45
+ "test:coverage": "vitest run --coverage --coverage.reporter=text",
46
+ "generate:llms": "node scripts/generate-llms.mjs",
47
+ "preversion": "pnpm install --frozen-lockfile && pnpm build && pnpm test:coverage && pnpm typecheck && pnpm generate:llms",
48
+ "prepublishOnly": "npm whoami || npm login",
49
+ "alpha": "npm version prerelease --preid alpha && npm publish --tag alpha && git push --follow-tags",
50
+ "release": "npm version patch && npm publish && git push --follow-tags"
51
+ },
52
+ "dependencies": {},
53
+ "peerDependencies": {
54
+ "convex": "^1.41.0"
55
+ },
56
+ "devDependencies": {
57
+ "@edge-runtime/vm": "^5.0.0",
58
+ "@vitest/coverage-v8": "^4.1.8",
59
+ "@vllnt/eslint-config": "^1.0.0",
60
+ "@vllnt/typescript": "^1.0.0",
61
+ "convex": "^1.41.0",
62
+ "convex-test": "^0.0.53",
63
+ "eslint": "^9.0.0",
64
+ "prettier": "^3.4.0",
65
+ "typescript": "^5.7.0",
66
+ "vitest": "^4.1.8"
67
+ },
68
+ "homepage": "https://github.com/vllnt/convex-experiments#readme",
69
+ "bugs": {
70
+ "url": "https://github.com/vllnt/convex-experiments/issues"
71
+ },
72
+ "author": {
73
+ "name": "bntvllnt",
74
+ "url": "https://bntvllnt.com"
75
+ },
76
+ "funding": {
77
+ "type": "github",
78
+ "url": "https://github.com/sponsors/bntvllnt"
79
+ },
80
+ "repository": {
81
+ "type": "git",
82
+ "url": "https://github.com/vllnt/convex-experiments"
83
+ },
84
+ "engines": {
85
+ "node": ">=18"
86
+ },
87
+ "publishConfig": {
88
+ "access": "public"
89
+ },
90
+ "sideEffects": false,
91
+ "keywords": [
92
+ "convex",
93
+ "convex-component",
94
+ "experiments",
95
+ "ab-testing",
96
+ "feature-experimentation"
97
+ ]
98
+ }