next-evolve 0.0.0-stage → 0.3.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.
Files changed (124) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +136 -2
  3. package/dist/_virtual/_rolldown/runtime.js +12 -0
  4. package/dist/aggregate/attribution.d.ts +40 -0
  5. package/dist/aggregate/attribution.js +115 -0
  6. package/dist/aggregate/run.d.ts +21 -0
  7. package/dist/aggregate/run.js +139 -0
  8. package/dist/cli/aggregate.d.ts +9 -0
  9. package/dist/cli/aggregate.js +104 -0
  10. package/dist/cli/args.d.ts +12 -0
  11. package/dist/cli/args.js +23 -0
  12. package/dist/cli/cloud.d.ts +41 -0
  13. package/dist/cli/cloud.js +74 -0
  14. package/dist/cli/connect.d.ts +8 -0
  15. package/dist/cli/connect.js +46 -0
  16. package/dist/cli/detect.d.ts +26 -0
  17. package/dist/cli/detect.js +97 -0
  18. package/dist/cli/doctor.d.ts +34 -0
  19. package/dist/cli/doctor.js +230 -0
  20. package/dist/cli/env-file.d.ts +4 -0
  21. package/dist/cli/env-file.js +15 -0
  22. package/dist/cli/files.d.ts +25 -0
  23. package/dist/cli/files.js +173 -0
  24. package/dist/cli/index.d.ts +1 -0
  25. package/dist/cli/index.js +81 -0
  26. package/dist/cli/init.d.ts +26 -0
  27. package/dist/cli/init.js +240 -0
  28. package/dist/cli/load-app.d.ts +19 -0
  29. package/dist/cli/load-app.js +67 -0
  30. package/dist/cli/migrate.d.ts +7 -0
  31. package/dist/cli/migrate.js +30 -0
  32. package/dist/cli/pages.d.ts +17 -0
  33. package/dist/cli/pages.js +304 -0
  34. package/dist/cli/plan.d.ts +27 -0
  35. package/dist/cli/plan.js +129 -0
  36. package/dist/cli/pull.d.ts +13 -0
  37. package/dist/cli/pull.js +62 -0
  38. package/dist/cli/push.d.ts +14 -0
  39. package/dist/cli/push.js +33 -0
  40. package/dist/client/beacon.d.ts +11 -0
  41. package/dist/client/beacon.js +51 -0
  42. package/dist/client/evolve-script.d.ts +6 -0
  43. package/dist/client/evolve-script.js +60 -0
  44. package/dist/client/index.d.ts +3 -0
  45. package/dist/client/index.js +3 -0
  46. package/dist/cloud/manifest.d.ts +14 -0
  47. package/dist/cloud/manifest.js +46 -0
  48. package/dist/cloud/push.d.ts +21 -0
  49. package/dist/cloud/push.js +56 -0
  50. package/dist/cloud/regions.d.ts +8 -0
  51. package/dist/cloud/regions.js +25 -0
  52. package/dist/config/config.d.ts +51 -0
  53. package/dist/config/config.js +47 -0
  54. package/dist/config/goals.d.ts +50 -0
  55. package/dist/config/goals.js +85 -0
  56. package/dist/config/index.d.ts +3 -0
  57. package/dist/config/index.js +3 -0
  58. package/dist/core/assign.d.ts +35 -0
  59. package/dist/core/assign.js +104 -0
  60. package/dist/core/bots.d.ts +5 -0
  61. package/dist/core/bots.js +25 -0
  62. package/dist/core/constants.d.ts +3 -0
  63. package/dist/core/constants.js +5 -0
  64. package/dist/core/cookies.d.ts +12 -0
  65. package/dist/core/cookies.js +25 -0
  66. package/dist/core/keys.d.ts +14 -0
  67. package/dist/core/keys.js +38 -0
  68. package/dist/core/log.d.ts +2 -0
  69. package/dist/core/log.js +10 -0
  70. package/dist/db/client.d.ts +22 -0
  71. package/dist/db/client.js +50 -0
  72. package/dist/db/migrate.d.ts +18 -0
  73. package/dist/db/migrate.js +43 -0
  74. package/dist/db/writer.d.ts +53 -0
  75. package/dist/db/writer.js +182 -0
  76. package/dist/index.d.ts +5 -0
  77. package/dist/index.js +5 -0
  78. package/dist/instrumentation/index.d.ts +24 -0
  79. package/dist/instrumentation/index.js +54 -0
  80. package/dist/node_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/typescript.d.ts +11169 -0
  81. package/dist/proxy/index.d.ts +2 -0
  82. package/dist/proxy/index.js +1 -0
  83. package/dist/proxy/proxy.d.ts +24 -0
  84. package/dist/proxy/proxy.js +120 -0
  85. package/dist/react/context.d.ts +6 -0
  86. package/dist/react/context.js +8 -0
  87. package/dist/react/optimizable.d.ts +37 -0
  88. package/dist/react/optimizable.js +38 -0
  89. package/dist/react/region.d.ts +17 -0
  90. package/dist/react/region.js +127 -0
  91. package/dist/react/render-bundle.d.ts +15 -0
  92. package/dist/react/render-bundle.js +43 -0
  93. package/dist/react/with-evolve.d.ts +29 -0
  94. package/dist/react/with-evolve.js +57 -0
  95. package/dist/server/aggregate-route.d.ts +16 -0
  96. package/dist/server/aggregate-route.js +68 -0
  97. package/dist/server/collect.d.ts +62 -0
  98. package/dist/server/collect.js +157 -0
  99. package/dist/server/events.d.ts +18 -0
  100. package/dist/server/events.js +50 -0
  101. package/dist/server/evolve.d.ts +20 -0
  102. package/dist/server/evolve.js +57 -0
  103. package/dist/server/handler.d.ts +17 -0
  104. package/dist/server/handler.js +105 -0
  105. package/dist/server/index.d.ts +4 -0
  106. package/dist/server/index.js +4 -0
  107. package/dist/server/runtime.d.ts +26 -0
  108. package/dist/server/runtime.js +45 -0
  109. package/dist/server/sync-route.d.ts +28 -0
  110. package/dist/server/sync-route.js +100 -0
  111. package/dist/server/track-route.d.ts +25 -0
  112. package/dist/server/track-route.js +49 -0
  113. package/dist/store/disk.d.ts +6 -0
  114. package/dist/store/disk.js +23 -0
  115. package/dist/store/fetch.d.ts +26 -0
  116. package/dist/store/fetch.js +74 -0
  117. package/dist/store/global-config.d.ts +39 -0
  118. package/dist/store/global-config.js +122 -0
  119. package/dist/store/memory.d.ts +39 -0
  120. package/dist/store/memory.js +72 -0
  121. package/dist/store/runtime.d.ts +44 -0
  122. package/dist/store/runtime.js +101 -0
  123. package/migrations/0001_init.sql +56 -0
  124. package/package.json +73 -3
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Velocinator
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 all
13
+ 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 THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,137 @@
1
- # Temporary Holding Version
1
+ # next-evolve
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Self-improving Next.js sites: server-side variants with no flicker, measured in your own Postgres. Needs an Evolve Cloud account.
4
+
5
+ > Alpha.
6
+
7
+ ## Setup
8
+
9
+ ```sh
10
+ npx next-evolve init --routes "/ /pricing" # add --dry-run to see the diff first
11
+ npx next-evolve doctor # checks env, key, DB, bundle, proxy, pages, cloud → app
12
+ ```
13
+
14
+ `init` installs the package, connects to your site (secret key and webhook secret from the dashboard), sets up the database, and writes the config, route handler, proxy, layout script, instrumentation and `prebuild` hook. Static-mode pages move under `app/[evolve]/` and get wrapped with `withEvolve()`; when that isn't safe (a top-level `[slug]`, `force-dynamic`, a `"use client"` page…) the route stays in dynamic mode and `init` says why. It never overwrites your `.env.local` lines, and a re-run changes nothing. `--yes` skips every prompt.
15
+
16
+ Other commands: `migrate`, `pull`, `push`, `connect --test [--app-url]`, `aggregate`. The sections below describe what `init` writes.
17
+
18
+ ## Database
19
+
20
+ Raw events (exposures, goal events, identities) stay in **your** Postgres (≥ 14), in a separate `evolve` schema. Evolve Cloud only ever receives aggregate counts.
21
+
22
+ ```sh
23
+ EVOLVE_DATABASE_URL=postgres://… npx next-evolve migrate
24
+ ```
25
+
26
+ Use the pooled URL on serverless hosts (e.g. Neon's `-pooler` host).
27
+
28
+ ## Goals
29
+
30
+ ```ts
31
+ // evolve.goals.ts
32
+ import { defineGoals } from "next-evolve/config";
33
+
34
+ export default defineGoals({
35
+ cta_click: { event: "cta_click", window: "1d" },
36
+ signup_page: { pageview: "/signup", window: "1d" },
37
+ paid: {
38
+ event: "subscription_started",
39
+ source: "stripe",
40
+ window: "30d",
41
+ value: (e) => e.props.amount as number,
42
+ },
43
+ });
44
+ ```
45
+
46
+ Function values run in your app when the event is written; only `{ kind: "local-fn" }` is sent to the cloud.
47
+
48
+ ## Routes and beacon
49
+
50
+ ```ts
51
+ // app/api/evolve/[...evolve]/route.ts
52
+ import { createEvolveHandler } from "next-evolve/server";
53
+ import evolveConfig from "@/evolve.config";
54
+ import goals from "@/evolve.goals";
55
+
56
+ export const { GET, POST } = createEvolveHandler({ config: evolveConfig, goals });
57
+ export const maxDuration = 60;
58
+ ```
59
+
60
+ Put `<EvolveScript />` (from `next-evolve/client`) in the root layout. It sends page views, exposures and clicks on `[data-evolve-goal]` elements (forms: on submit) to `/api/evolve/collect` on your own origin.
61
+
62
+ ## Tracking
63
+
64
+ ```ts
65
+ import { evolve } from "next-evolve/server";
66
+
67
+ await evolve.track("project_created", { userId: user.id });
68
+ await evolve.identify(user.id); // links the visitor's evolve_id cookie; first link wins
69
+ ```
70
+
71
+ In the browser: `import { track, identify } from "next-evolve/client"`.
72
+
73
+ ### Server-to-server events
74
+
75
+ `POST /api/evolve/track` with `Authorization: Bearer $EVOLVE_TRACK_SECRET` and `{ "events": [{ "name", "userId"?, "anonId"?, "value"?, "props"?, "source"?, "ts"?, "dedupeKey"? }] }` (≤ 500 events). Unset secret → 404; wrong bearer → 401.
76
+
77
+ ### Stripe recipe
78
+
79
+ Call `evolve.track` from your own Stripe webhook handler, after you verify the signature:
80
+
81
+ ```ts
82
+ // app/api/stripe/route.ts
83
+ import { evolve } from "next-evolve/server";
84
+
85
+ if (event.type === "invoice.paid") {
86
+ const invoice = event.data.object;
87
+ await evolve.track("subscription_started", {
88
+ userId: invoice.metadata.userId,
89
+ source: "stripe",
90
+ props: { amount: invoice.amount_paid / 100 },
91
+ dedupeKey: event.id, // Stripe retries: stored once
92
+ });
93
+ }
94
+ ```
95
+
96
+ ## Aggregation
97
+
98
+ Evolve Cloud never reads your database. It sends a signed request to `POST /api/evolve/aggregate` (verified with `EVOLVE_WEBHOOK_SECRET`), and your app answers with counts and sums per experiment × variant × goal × day, signed with the same secret. No visitor or user ids leave your app. Set `EVOLVE_SECRET_KEY`, `EVOLVE_WEBHOOK_SECRET` and `EVOLVE_DATABASE_URL`; the route needs `maxDuration = 60`.
99
+
100
+ In local development the cloud can't reach `localhost`. Run the same request yourself:
101
+
102
+ ```sh
103
+ npx next-evolve aggregate # print the statistics
104
+ npx next-evolve aggregate --upload # and send them to Evolve Cloud
105
+ ```
106
+
107
+ ## Sync
108
+
109
+ Your app keeps a signed copy of its configuration (the bundle) in memory and serves from it with no network call on the request path. It gets new bundles three ways:
110
+
111
+ - **Polling:** every 60 s (5 s in dev), in the background.
112
+ - **Webhook:** when you change an experiment, Evolve Cloud calls `POST /api/evolve/sync` (signed with `EVOLVE_WEBHOOK_SECRET`). The receiving instance pulls the new bundle within seconds. The webhook carries no configuration, so a forged call can at most cause one extra fetch.
113
+ - **Build time:** add `"prebuild": "next-evolve pull"` to `package.json` and pass the result to the proxy, so a fresh instance serves the last bundle before its first fetch:
114
+
115
+ ```ts
116
+ // proxy.ts
117
+ import { createEvolveProxy } from "next-evolve/proxy";
118
+ import buildBundle from "./.evolve/bundle.generated";
119
+ import evolveConfig from "./evolve.config";
120
+
121
+ export const proxy = createEvolveProxy(evolveConfig, { buildBundle });
122
+ ```
123
+
124
+ Add `.evolve/` to `.gitignore`. `pull` never fails the build: without a key or a reachable cloud it keeps the previous bundle or writes an empty one.
125
+
126
+ **Long-running Node (`next start`, Docker):** call `registerEvolve()` from `instrumentation.ts`. It polls in the background and keeps the last bundle on disk (`os.tmpdir()`, or `EVOLVE_CACHE_DIR`), so a restart serves it even when the cloud is unreachable.
127
+
128
+ **Vercel + Global Config (recommended on Vercel):** without it, a webhook reaches only the instance that receives it and the others catch up within 60 s. With a [Global Config](https://vercel.com/docs/edge-config) store, the sync route writes the bundle there and every instance picks it up within ~10 s. Set:
129
+
130
+ | Var | Purpose |
131
+ | ------------------------- | --------------------------------------- |
132
+ | `EVOLVE_GLOBAL_CONFIG` | The store's connection string (read) |
133
+ | `EVOLVE_GLOBAL_CONFIG_ID` | The store id (`ecfg_…`), for writes |
134
+ | `EVOLVE_VERCEL_API_TOKEN` | A Vercel token that can write the store |
135
+ | `EVOLVE_VERCEL_TEAM_ID` | Optional, for team-owned stores |
136
+
137
+ Bundles read from Global Config are verified like any other. With an existing proxy that calls `applyEvolve`, `await evolveReady(evolveConfig)` first (a fresh instance waits ≤ 20 ms for one read).
@@ -0,0 +1,12 @@
1
+ // \0rolldown/runtime.js
2
+ var __defProp = Object.defineProperty;
3
+ var __exportAll = (all, no_symbols) => {
4
+ let target = {};
5
+ for (var name in all) __defProp(target, name, {
6
+ get: all[name],
7
+ enumerable: true
8
+ });
9
+ if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
10
+ return target;
11
+ };
12
+ export { __exportAll };
@@ -0,0 +1,40 @@
1
+ import { Sql } from "../db/client.js";
2
+ import { AggregateRow, SerializedGoal } from "@next-evolve/protocol";
3
+ export type AttributionInput = {
4
+ experimentId: string;
5
+ /** Only exposures to these variants count. */
6
+ variants: readonly string[];
7
+ goalKey: string;
8
+ goal: SerializedGoal;
9
+ /** Upper bound for exposures and events (exclusive). */
10
+ asOf: Date;
11
+ /** Only units whose cohort day (UTC day of first exposure) is on/after this `YYYY-MM-DD`. */
12
+ cohortsFrom: string;
13
+ };
14
+ type RawRow = {
15
+ variant_id: string;
16
+ seg: string;
17
+ day: string;
18
+ n: number;
19
+ c: number;
20
+ s1: number;
21
+ s2: number;
22
+ mn: number;
23
+ mc: number;
24
+ ms1: number;
25
+ ms2: number;
26
+ };
27
+ /**
28
+ * The attribution query for one experiment × goal. An event counts for a unit
29
+ * when it falls in `[first exposure, first exposure + window)`, capped at
30
+ * `asOf`. A unit is mature when its whole window ended before `asOf`.
31
+ *
32
+ * - visitor unit: one unit per exposed anon id; events match on the anon id,
33
+ * or on the linked user id (cross-device and server events).
34
+ * - user unit: identified exposures only, one unit per user at their earliest
35
+ * exposure; events match on the user id or any of the user's anon ids.
36
+ */
37
+ export declare function attributionQuery(sql: Sql, input: AttributionInput): import("postgres").PendingQuery<RawRow[]>;
38
+ /** Runs the attribution query and returns protocol rows. */
39
+ export declare function runAttribution(sql: Sql, input: AttributionInput): Promise<AggregateRow[]>;
40
+ export {};
@@ -0,0 +1,115 @@
1
+ import { parseWindowSeconds } from "@next-evolve/protocol";
2
+ // src/aggregate/attribution.ts
3
+ const NUMERIC_RE = "^\\s*-?(\\d{1,30}(\\.\\d{0,30})?|\\.\\d{1,30})([eE][-+]?\\d{1,2})?\\s*$";
4
+ function numericText(sql, text) {
5
+ return sql`(case when ${text} ~ ${NUMERIC_RE} then (${text})::float8 end)`;
6
+ }
7
+ /** The event's value for this goal (null → conversion only). */
8
+ function valueExpr(sql, key, goal) {
9
+ if (goal.kind === "pageview" || goal.value === null) return sql`e.value`;
10
+ if (goal.value.kind === "local-fn") return numericText(sql, sql`e.goal_values ->> ${key}::text`);
11
+ return numericText(sql, sql`e.props #>> string_to_array(${goal.value.path}::text, '.')`);
12
+ }
13
+ /** Which events count for this goal (window and unit join come from the caller). */
14
+ function matchExpr(sql, goal) {
15
+ if (goal.kind === "pageview") return sql`e.name = '$pageview' and ${goal.path.endsWith("*") ? sql`starts_with(e.path, ${goal.path.slice(0, -1)}::text)` : sql`e.path = ${goal.path}::text`}`;
16
+ const source = goal.source === null ? sql`` : sql`and e.source = ${goal.source}::text`;
17
+ const where = goal.where === null ? sql`` : sql`and e.props @> (${JSON.stringify(goal.where)}::text)::jsonb`;
18
+ return sql`e.name = ${goal.event}::text ${source} ${where}`;
19
+ }
20
+ /**
21
+ * The attribution query for one experiment × goal. An event counts for a unit
22
+ * when it falls in `[first exposure, first exposure + window)`, capped at
23
+ * `asOf`. A unit is mature when its whole window ended before `asOf`.
24
+ *
25
+ * - visitor unit: one unit per exposed anon id; events match on the anon id,
26
+ * or on the linked user id (cross-device and server events).
27
+ * - user unit: identified exposures only, one unit per user at their earliest
28
+ * exposure; events match on the user id or any of the user's anon ids.
29
+ */
30
+ function attributionQuery(sql, input) {
31
+ const windowSeconds = parseWindowSeconds(input.goal.window);
32
+ if (windowSeconds === null) throw new Error(`invalid goal window ${input.goal.window}`);
33
+ const asOf = input.asOf.toISOString();
34
+ const from = `${input.cohortsFrom}T00:00:00Z`;
35
+ const win = sql`make_interval(secs => ${windowSeconds}::int)`;
36
+ const asOfTs = sql`${asOf}::timestamptz`;
37
+ const val = valueExpr(sql, input.goalKey, input.goal);
38
+ const match = matchExpr(sql, input.goal);
39
+ const inWindow = sql`e.ts >= u.first_at and e.ts < least(u.first_at + ${win}, ${asOfTs})`;
40
+ const variants = sql`string_to_array(${input.variants.join(",")}::text, ',')`;
41
+ return sql`
42
+ with ${input.goal.unit === "visitor" ? sql`
43
+ u as (
44
+ select x.anon_id as unit, x.variant_id, x.first_at, coalesce(x.segment, 'main') as seg,
45
+ i.user_id, (x.first_at + ${win} <= ${asOfTs}) as mature
46
+ from evolve.exposures x
47
+ left join evolve.identities i on i.anon_id = x.anon_id
48
+ where x.experiment_id = ${input.experimentId}::text
49
+ and x.variant_id = any(${variants})
50
+ and x.first_at >= ${from}::timestamptz and x.first_at < ${asOfTs}),
51
+ hits as (
52
+ select u.unit, ${val} as v from u
53
+ join evolve.events e on e.anon_id = u.unit
54
+ where ${match} and ${inWindow}
55
+ union all
56
+ select u.unit, ${val} from u
57
+ join evolve.events e on e.user_id = u.user_id
58
+ where u.user_id is not null and (e.anon_id is null or e.anon_id <> u.unit)
59
+ and ${match} and ${inWindow})` : sql`
60
+ firsts as (
61
+ -- Earliest exposure per user over all cohorts, so a user exposed again on
62
+ -- another device never becomes a second unit in a later cohort.
63
+ select distinct on (i.user_id) i.user_id as unit, x.variant_id, x.first_at,
64
+ coalesce(x.segment, 'main') as seg
65
+ from evolve.exposures x
66
+ join evolve.identities i on i.anon_id = x.anon_id
67
+ where x.experiment_id = ${input.experimentId}::text and x.first_at < ${asOfTs}
68
+ order by i.user_id, x.first_at, x.anon_id),
69
+ u as (
70
+ select unit, variant_id, first_at, seg, (first_at + ${win} <= ${asOfTs}) as mature
71
+ from firsts
72
+ where variant_id = any(${variants}) and first_at >= ${from}::timestamptz),
73
+ hits as (
74
+ select u.unit, ${val} as v from u
75
+ join evolve.events e on e.user_id = u.unit
76
+ where ${match} and ${inWindow}
77
+ union all
78
+ select u.unit, ${val} from u
79
+ join evolve.identities ia on ia.user_id = u.unit
80
+ join evolve.events e on e.anon_id = ia.anon_id
81
+ where (e.user_id is null or e.user_id <> u.unit) and ${match} and ${inWindow})`},
82
+ per_unit as (select unit, sum(coalesce(v, 0)) as v from hits group by unit)
83
+ select u.variant_id, u.seg,
84
+ to_char((u.first_at at time zone 'UTC')::date, 'YYYY-MM-DD') as day,
85
+ count(*)::int as n,
86
+ count(p.unit)::int as c,
87
+ coalesce(sum(p.v), 0)::float8 as s1,
88
+ coalesce(sum(p.v * p.v), 0)::float8 as s2,
89
+ (count(*) filter (where u.mature))::int as mn,
90
+ (count(p.unit) filter (where u.mature))::int as mc,
91
+ coalesce(sum(p.v) filter (where u.mature), 0)::float8 as ms1,
92
+ coalesce(sum(p.v * p.v) filter (where u.mature), 0)::float8 as ms2
93
+ from u left join per_unit p on p.unit = u.unit
94
+ group by 1, 2, 3
95
+ order by 3, 1, 2`;
96
+ }
97
+ /** Runs the attribution query and returns protocol rows. */
98
+ async function runAttribution(sql, input) {
99
+ return (await attributionQuery(sql, input)).map((r) => ({
100
+ exp: input.experimentId,
101
+ var: r.variant_id,
102
+ goal: input.goalKey,
103
+ seg: r.seg,
104
+ day: r.day,
105
+ n: r.n,
106
+ c: r.c,
107
+ s1: r.s1,
108
+ s2: r.s2,
109
+ mn: r.mn,
110
+ mc: r.mc,
111
+ ms1: r.ms1,
112
+ ms2: r.ms2
113
+ }));
114
+ }
115
+ export { attributionQuery, runAttribution };
@@ -0,0 +1,21 @@
1
+ import { Sql } from "../db/client.js";
2
+ import { AggregateRequest, AggregateResponse } from "@next-evolve/protocol";
3
+ export type RunAggregateOptions = {
4
+ /** Whole-request budget (spec: 25 s). */
5
+ maxMs: number;
6
+ /** Per-query cap (spec: 20 s). */
7
+ statementTimeoutMs: number;
8
+ sdkVersion: string;
9
+ now?: () => number;
10
+ };
11
+ export type RunAggregateResult = {
12
+ kind: "ok";
13
+ response: AggregateResponse;
14
+ cached: boolean;
15
+ } | {
16
+ kind: "busy";
17
+ retryAfter: number;
18
+ };
19
+ /** Answer `429 { busy, retryAfter }` while another aggregate holds the lock. */
20
+ export declare const BUSY_RETRY_AFTER_SECONDS = 30;
21
+ export declare function runAggregate(sql: Sql, req: AggregateRequest, opts: RunAggregateOptions): Promise<RunAggregateResult>;
@@ -0,0 +1,139 @@
1
+ import { runAttribution } from "./attribution.js";
2
+ import { MAX_RESPONSE_ROWS } from "@next-evolve/protocol";
3
+ import { createHash } from "node:crypto";
4
+ // src/aggregate/run.ts
5
+ /** Answer `429 { busy, retryAfter }` while another aggregate holds the lock. */
6
+ const BUSY_RETRY_AFTER_SECONDS = 30;
7
+ /** Below this much budget left, don't start another query. */
8
+ const MIN_QUERY_MS = 500;
9
+ const QUERY_CANCELED = "57014";
10
+ /** Stored responses are replayed for this long (spec §9.3). */
11
+ const RUN_TTL = "24 hours";
12
+ async function storedResponse(sql, requestId) {
13
+ const [hit] = await sql`
14
+ select response from evolve.aggregate_runs
15
+ where request_id = ${requestId} and status = 'done'
16
+ and received_at > now() - ${RUN_TTL}::interval`;
17
+ return hit?.response ?? null;
18
+ }
19
+ async function runAggregate(sql, req, opts) {
20
+ const now = opts.now ?? Date.now;
21
+ const started = now();
22
+ const cached = await storedResponse(sql, req.requestId);
23
+ if (cached) return {
24
+ kind: "ok",
25
+ response: cached,
26
+ cached: true
27
+ };
28
+ const conn = await sql.reserve();
29
+ let locked = false;
30
+ try {
31
+ const [l] = await conn`
32
+ select pg_try_advisory_lock(hashtext('evolve.aggregate')) as ok`;
33
+ locked = Boolean(l?.ok);
34
+ if (!locked) return {
35
+ kind: "busy",
36
+ retryAfter: 30
37
+ };
38
+ const raced = await storedResponse(conn, req.requestId);
39
+ if (raced) return {
40
+ kind: "ok",
41
+ response: raced,
42
+ cached: true
43
+ };
44
+ const deadline = started + opts.maxMs;
45
+ const asOf = new Date(req.asOf);
46
+ const rows = [];
47
+ const errors = [];
48
+ const remaining = [];
49
+ for (const exp of req.experiments) {
50
+ if (remaining.length) {
51
+ remaining.push(exp.id);
52
+ continue;
53
+ }
54
+ const expRows = [];
55
+ const expErrors = [];
56
+ let outOfBudget = false;
57
+ for (const g of exp.goals) {
58
+ const left = deadline - now();
59
+ if (left < MIN_QUERY_MS) {
60
+ outOfBudget = true;
61
+ break;
62
+ }
63
+ const timeout = Math.floor(Math.min(opts.statementTimeoutMs, left));
64
+ await conn.unsafe(`set statement_timeout = ${timeout}`);
65
+ try {
66
+ expRows.push(...await runAttribution(conn, {
67
+ experimentId: exp.id,
68
+ variants: exp.variants,
69
+ goalKey: g.key,
70
+ goal: g.def,
71
+ asOf,
72
+ cohortsFrom: exp.cohortsFrom
73
+ }));
74
+ } catch (e) {
75
+ const code = e.code;
76
+ if (code === QUERY_CANCELED && now() >= deadline - MIN_QUERY_MS) {
77
+ outOfBudget = true;
78
+ break;
79
+ }
80
+ expErrors.push({
81
+ exp: exp.id,
82
+ goal: g.key,
83
+ message: code === QUERY_CANCELED ? "statement timeout" : errorMessage(e)
84
+ });
85
+ }
86
+ }
87
+ if (outOfBudget || rows.length + expRows.length > MAX_RESPONSE_ROWS) {
88
+ remaining.push(exp.id);
89
+ continue;
90
+ }
91
+ rows.push(...expRows);
92
+ errors.push(...expErrors);
93
+ }
94
+ await conn.unsafe("set statement_timeout = 0");
95
+ const usage = req.usageFrom ? (await conn`
96
+ select to_char(day, 'YYYY-MM-DD') as day, tested_pageviews::text as n
97
+ from evolve.usage_daily where day >= ${req.usageFrom}::date order by day`).map((u) => ({
98
+ day: u.day,
99
+ testedPageviews: Number(u.n)
100
+ })) : [];
101
+ const response = {
102
+ pv: req.pv,
103
+ requestId: req.requestId,
104
+ site: req.site,
105
+ asOf: req.asOf,
106
+ sdk: opts.sdkVersion,
107
+ mode: req.mode,
108
+ durationMs: Math.max(0, Math.round(now() - started)),
109
+ partial: remaining.length > 0,
110
+ remaining,
111
+ rows,
112
+ usage,
113
+ errors
114
+ };
115
+ const body = JSON.stringify(response);
116
+ await conn`
117
+ insert into evolve.aggregate_runs
118
+ (request_id, received_at, mode, status, duration_ms, response_hash, response)
119
+ values (${req.requestId}, ${new Date(started)}, ${req.mode}, 'done', ${response.durationMs},
120
+ ${createHash("sha256").update(body).digest("hex")}, (${body}::text)::jsonb)
121
+ on conflict (request_id) do nothing`;
122
+ await conn`delete from evolve.aggregate_runs where received_at < now() - ${RUN_TTL}::interval`;
123
+ return {
124
+ kind: "ok",
125
+ response,
126
+ cached: false
127
+ };
128
+ } finally {
129
+ if (locked) {
130
+ await conn.unsafe("set statement_timeout = 0").catch(() => {});
131
+ await conn`select pg_advisory_unlock(hashtext('evolve.aggregate'))`.catch(() => {});
132
+ }
133
+ conn.release();
134
+ }
135
+ }
136
+ function errorMessage(e) {
137
+ return String(e instanceof Error ? e.message : e).slice(0, 500);
138
+ }
139
+ export { BUSY_RETRY_AFTER_SECONDS, runAggregate };
@@ -0,0 +1,9 @@
1
+ type Env = Record<string, string | undefined>;
2
+ export type AggregateCommandDeps = {
3
+ env: Env;
4
+ fetch?: typeof fetch;
5
+ out?: (s: string) => void;
6
+ err?: (s: string) => void;
7
+ };
8
+ export declare function aggregateCommand(args: string[], deps: AggregateCommandDeps): Promise<number>;
9
+ export {};
@@ -0,0 +1,104 @@
1
+ import { runAggregate } from "../aggregate/run.js";
2
+ import { sdkVersion } from "../cloud/manifest.js";
3
+ import { createSql } from "../db/client.js";
4
+ import "../store/runtime.js";
5
+ import { SIGNATURE_HEADER, aggregateRequestSchema, isSupportedProtocol, parseSecretKey, signBody } from "@next-evolve/protocol";
6
+ // src/cli/aggregate.ts
7
+ async function aggregateCommand(args, deps) {
8
+ const { env } = deps;
9
+ const out = deps.out ?? ((s) => process.stdout.write(`${s}\n`));
10
+ const err = deps.err ?? ((s) => console.error(s));
11
+ const doFetch = deps.fetch ?? fetch;
12
+ const upload = args.includes("--upload");
13
+ const key = env.EVOLVE_SECRET_KEY;
14
+ const secret = env.EVOLVE_WEBHOOK_SECRET;
15
+ const dbUrl = env.EVOLVE_DATABASE_URL;
16
+ const site = parseSecretKey(key ?? "")?.siteId;
17
+ const missing = [
18
+ !site && "EVOLVE_SECRET_KEY",
19
+ !dbUrl && "EVOLVE_DATABASE_URL",
20
+ upload && !secret && "EVOLVE_WEBHOOK_SECRET"
21
+ ].filter(Boolean);
22
+ if (missing.length) {
23
+ err(`next-evolve aggregate: set ${missing.join(", ")}`);
24
+ return 1;
25
+ }
26
+ const cloud = env.EVOLVE_CLOUD_URL ?? "https://api.evolveab.com";
27
+ const auth = { authorization: `Bearer ${key}` };
28
+ let request;
29
+ try {
30
+ const res = await doFetch(new URL("/v1/aggregates/request", cloud), {
31
+ method: "POST",
32
+ headers: auth
33
+ });
34
+ if (res.status === 404) {
35
+ out("Nothing to aggregate: no running experiments.");
36
+ return 0;
37
+ }
38
+ if (!res.ok) {
39
+ err(`next-evolve aggregate: the cloud answered ${res.status} for the request`);
40
+ return 1;
41
+ }
42
+ const body = await res.json();
43
+ if (!isSupportedProtocol(body.request?.pv)) {
44
+ err("next-evolve aggregate: unsupported protocol version; upgrade next-evolve");
45
+ return 1;
46
+ }
47
+ const parsed = aggregateRequestSchema.safeParse(body.request);
48
+ if (!parsed.success || parsed.data.site !== site) {
49
+ err("next-evolve aggregate: the cloud sent an invalid request");
50
+ return 1;
51
+ }
52
+ request = parsed.data;
53
+ } catch (e) {
54
+ err(`next-evolve aggregate: can't reach ${cloud} (${e instanceof Error ? e.message : e})`);
55
+ return 1;
56
+ }
57
+ const sql = createSql(dbUrl, { max: 1 });
58
+ let result;
59
+ try {
60
+ result = await runAggregate(sql, request, {
61
+ maxMs: 12e4,
62
+ statementTimeoutMs: 6e4,
63
+ sdkVersion: sdkVersion()
64
+ });
65
+ } catch (e) {
66
+ err(`next-evolve aggregate: database error (${e instanceof Error ? e.message : e})`);
67
+ return 1;
68
+ } finally {
69
+ await sql.end({ timeout: 5 });
70
+ }
71
+ if (result.kind === "busy") {
72
+ err("next-evolve aggregate: another aggregation is running; try again in 30 s");
73
+ return 1;
74
+ }
75
+ const r = result.response;
76
+ const summary = `${r.rows.length} rows for ${request.experiments.length} experiment(s) in ${r.durationMs} ms${r.partial ? `, partial (remaining: ${r.remaining.join(", ")})` : ""}${r.errors.length ? `, ${r.errors.length} error(s)` : ""}`;
77
+ if (!upload) {
78
+ out(JSON.stringify(r, null, 2));
79
+ err(summary);
80
+ return 0;
81
+ }
82
+ const body = JSON.stringify(r);
83
+ try {
84
+ const res = await doFetch(new URL("/v1/aggregates", cloud), {
85
+ method: "POST",
86
+ headers: {
87
+ ...auth,
88
+ "content-type": "application/json",
89
+ [SIGNATURE_HEADER]: signBody(secret, body)
90
+ },
91
+ body
92
+ });
93
+ if (!res.ok) {
94
+ err(`next-evolve aggregate: upload failed (${res.status} ${await res.text()})`);
95
+ return 1;
96
+ }
97
+ } catch (e) {
98
+ err(`next-evolve aggregate: upload failed (${e instanceof Error ? e.message : e})`);
99
+ return 1;
100
+ }
101
+ out(`Uploaded ${summary}.`);
102
+ return 0;
103
+ }
104
+ export { aggregateCommand };
@@ -0,0 +1,12 @@
1
+ export type FlagSpec = Record<string, {
2
+ type: "string" | "boolean";
3
+ short?: string;
4
+ }>;
5
+ /** Parses `--flag value` / `--flag`; unknown flags are an error (returned, not thrown). */
6
+ export declare function parseFlags<S extends FlagSpec>(args: string[], spec: S): {
7
+ ok: true;
8
+ flags: { [K in keyof S]?: S[K]["type"] extends "boolean" ? boolean : string; };
9
+ } | {
10
+ ok: false;
11
+ error: string;
12
+ };
@@ -0,0 +1,23 @@
1
+ import { parseArgs } from "node:util";
2
+ // src/cli/args.ts
3
+ /** Parses `--flag value` / `--flag`; unknown flags are an error (returned, not thrown). */
4
+ function parseFlags(args, spec) {
5
+ try {
6
+ const { values } = parseArgs({
7
+ args,
8
+ options: spec,
9
+ strict: true,
10
+ allowPositionals: false
11
+ });
12
+ return {
13
+ ok: true,
14
+ flags: values
15
+ };
16
+ } catch (e) {
17
+ return {
18
+ ok: false,
19
+ error: e instanceof Error ? e.message : String(e)
20
+ };
21
+ }
22
+ }
23
+ export { parseFlags };