versimo 0.0.0-stage → 0.3.0
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/CHANGELOG.md +34 -0
- package/LICENSE +21 -0
- package/README.md +110 -2
- package/dist/bucket.d.ts +13 -0
- package/dist/bucket.js +60 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +59 -0
- package/dist/client.d.ts +31 -0
- package/dist/client.js +102 -0
- package/dist/deploy.d.ts +29 -0
- package/dist/deploy.js +29 -0
- package/dist/github/app-auth.d.ts +14 -0
- package/dist/github/app-auth.js +63 -0
- package/dist/github/client.d.ts +64 -0
- package/dist/github/client.js +127 -0
- package/dist/github/index.d.ts +4 -0
- package/dist/github/index.js +4 -0
- package/dist/github/tags.d.ts +20 -0
- package/dist/github/tags.js +56 -0
- package/dist/github/webhook.d.ts +5 -0
- package/dist/github/webhook.js +23 -0
- package/dist/health.d.ts +50 -0
- package/dist/health.js +68 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +12 -0
- package/dist/monitor.d.ts +67 -0
- package/dist/monitor.js +148 -0
- package/dist/posthog.d.ts +42 -0
- package/dist/posthog.js +96 -0
- package/dist/provider.d.ts +28 -0
- package/dist/provider.js +51 -0
- package/dist/resolve.d.ts +5 -0
- package/dist/resolve.js +22 -0
- package/dist/semver.d.ts +11 -0
- package/dist/semver.js +56 -0
- package/dist/transitions.d.ts +14 -0
- package/dist/transitions.js +51 -0
- package/dist/types.d.ts +51 -0
- package/dist/types.js +8 -0
- package/dist/validate.d.ts +6 -0
- package/dist/validate.js +79 -0
- package/docs/api.md +214 -0
- package/docs/concepts.md +65 -0
- package/docs/github-app.md +90 -0
- package/docs/health.md +76 -0
- package/docs/porting.md +130 -0
- package/docs/record-format.md +62 -0
- package/docs/usage.md +236 -0
- package/github-app/manifest.json +19 -0
- package/package.json +63 -4
- package/schema/examples/billing.json +15 -0
- package/schema/module-record.schema.json +50 -0
package/docs/api.md
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
# API reference
|
|
2
|
+
|
|
3
|
+
Everything is a named export of `versimo`.
|
|
4
|
+
|
|
5
|
+
## Types
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
type Action = 'draft' | 'promote' | 'rollback' | 'rollout' | 'patch';
|
|
9
|
+
|
|
10
|
+
interface HistoryEntry {
|
|
11
|
+
at: string;
|
|
12
|
+
action: Action;
|
|
13
|
+
from: string | null;
|
|
14
|
+
to: string | null;
|
|
15
|
+
by?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface ModuleRecord {
|
|
19
|
+
$schema?: string;
|
|
20
|
+
module: string;
|
|
21
|
+
last: string | null;
|
|
22
|
+
current: string;
|
|
23
|
+
next: string | null;
|
|
24
|
+
rollout: number;
|
|
25
|
+
healthy: boolean;
|
|
26
|
+
updatedAt: string;
|
|
27
|
+
history?: HistoryEntry[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface Context { userId?: string }
|
|
31
|
+
type Channel = 'next' | 'current' | 'last';
|
|
32
|
+
type ResolutionReason = 'ROLLOUT' | 'DEFAULT' | 'NO_USER' | 'UNHEALTHY';
|
|
33
|
+
|
|
34
|
+
interface Resolution {
|
|
35
|
+
version: string;
|
|
36
|
+
channel: Channel;
|
|
37
|
+
reason: ResolutionReason;
|
|
38
|
+
bucket: number | null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface TransitionOptions {
|
|
42
|
+
by?: string; // recorded in history
|
|
43
|
+
now?: Date | string | number; // defaults to now
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Resolving
|
|
48
|
+
|
|
49
|
+
### `resolveVersion(record, ctx?)`
|
|
50
|
+
|
|
51
|
+
Returns the version string this user should get.
|
|
52
|
+
|
|
53
|
+
1. If `record.healthy` is false, return `last`, or `current` when there is no last.
|
|
54
|
+
2. If there is no `ctx.userId`, return `current`.
|
|
55
|
+
3. If `next` is set and the user's bucket is below `rollout`, return `next`.
|
|
56
|
+
4. Otherwise return `current`.
|
|
57
|
+
|
|
58
|
+
### `resolve(record, ctx?)`
|
|
59
|
+
|
|
60
|
+
Same rules, but returns a `Resolution` with the channel, the reason and the bucket.
|
|
61
|
+
|
|
62
|
+
### `bucketFor(module, userId)`
|
|
63
|
+
|
|
64
|
+
Returns the user's bucket, an integer from 0 to 99. Stable for a given module and user id.
|
|
65
|
+
|
|
66
|
+
## Changing a record
|
|
67
|
+
|
|
68
|
+
All four return a new record and never change the one you pass in. Each sets `updatedAt` and appends a history entry. They throw a `VersimoError` with code `INVALID_INPUT` when the change makes no sense.
|
|
69
|
+
|
|
70
|
+
### `draftNext(record, version, opts?)`
|
|
71
|
+
|
|
72
|
+
Sets `next` to `version` and `rollout` to 0. Replaces any next already there. `version` must be valid semver and greater than `current`. Pass `{ patch: true }` to log the action as `patch` instead of `draft`.
|
|
73
|
+
|
|
74
|
+
### `promote(record, opts?)`
|
|
75
|
+
|
|
76
|
+
Moves `next` to `current` and `current` to `last`. Empties `next`, sets `rollout` to 0 and `healthy` to true. Throws when `next` is null.
|
|
77
|
+
|
|
78
|
+
### `rollback(record, opts?)`
|
|
79
|
+
|
|
80
|
+
Sets `current` to `last`. Sets `last` and `next` to null, `rollout` to 0 and `healthy` to true. Throws when `last` is null.
|
|
81
|
+
|
|
82
|
+
All four take `opts.by` and `opts.reason`, which are written to the history entry.
|
|
83
|
+
|
|
84
|
+
### `setRollout(record, n, opts?)`
|
|
85
|
+
|
|
86
|
+
Sets `rollout` to `n`, an integer from 0 to 100. Throws when `n` is above 0 and `next` is null.
|
|
87
|
+
|
|
88
|
+
## Health
|
|
89
|
+
|
|
90
|
+
### `evaluateHealth(checks, observation)`
|
|
91
|
+
|
|
92
|
+
Judges one observation against the checks and returns `{ healthy, failures }`. Each failure is `{ check, value, limit }`, with `check` one of `errorRate`, `latency` or `url`, in that order. Throws `INVALID_INPUT` for a bad limit or a bad measurement.
|
|
93
|
+
|
|
94
|
+
### `checkHealth(record, checks, observation, opts?)`
|
|
95
|
+
|
|
96
|
+
Runs `evaluateHealth`. When current fails and there is a last, it returns the rolled back record with `rolledBack: true`. The history entry has `by` (default `versimo-health`) and `reason`, for example `health: errorRate, latency`. With no last the record comes back unchanged. Returns `{ record, verdict, rolledBack }`.
|
|
97
|
+
|
|
98
|
+
### `createHealthMonitor(options)`
|
|
99
|
+
|
|
100
|
+
Runs the checks on its own: reads the record from your store, measures, rolls back, writes the record and alerts. `check(module)` runs once. `start(modules, everyMs?)` runs on a timer and returns a stop function. See [Health checks](health.md) for the options.
|
|
101
|
+
|
|
102
|
+
## Validating
|
|
103
|
+
|
|
104
|
+
### `validateRecord(record)`
|
|
105
|
+
|
|
106
|
+
Returns `{ ok: boolean, errors: string[] }`. It never throws. It checks every field, rejects unknown fields, and checks that `next` is above `current`, `last` is below `current`, and `rollout` is 0 when there is no next.
|
|
107
|
+
|
|
108
|
+
## Client
|
|
109
|
+
|
|
110
|
+
### `createClient(options)`
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
interface ClientOptions {
|
|
114
|
+
source: ((module: string) => ModuleRecord | null | undefined | Promise<ModuleRecord | null | undefined>) | string;
|
|
115
|
+
ttlMs?: number; // default 30000
|
|
116
|
+
now?: () => number; // clock for the cache, for tests
|
|
117
|
+
fetch?: typeof fetch; // for URL sources
|
|
118
|
+
rollout?: (module: string) => number | null | undefined | Promise<number | null | undefined>;
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
`rollout` overrides the record's share on next, for example with `postHogRollout`. Only the share changes. When it returns null, a value outside 0 to 100, or throws, the record's own rollout is used.
|
|
123
|
+
|
|
124
|
+
Returns a `VersimoClient`:
|
|
125
|
+
|
|
126
|
+
- `record(module)` returns the cached or freshly loaded, validated record, with any `rollout` override applied.
|
|
127
|
+
- `resolve(module, ctx?)` returns a `Resolution`.
|
|
128
|
+
- `version(module, ctx?)` returns the version string.
|
|
129
|
+
- `clear(module?)` drops one cached record, or all.
|
|
130
|
+
|
|
131
|
+
Concurrent loads of one module share a single request. If a refresh fails with a source error and a stale record exists, the stale record is returned.
|
|
132
|
+
|
|
133
|
+
Module slugs must match `^[a-z0-9]+(-[a-z0-9]+)*$`.
|
|
134
|
+
|
|
135
|
+
## PostHog
|
|
136
|
+
|
|
137
|
+
### `postHogRollout(options)`
|
|
138
|
+
|
|
139
|
+
```ts
|
|
140
|
+
interface PostHogRolloutOptions {
|
|
141
|
+
apiKey: string; // PostHog project key, phc_...
|
|
142
|
+
host?: string; // default https://eu.i.posthog.com
|
|
143
|
+
ttlMs?: number; // default 30000
|
|
144
|
+
now?: () => number;
|
|
145
|
+
fetch?: typeof fetch;
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
Returns a rollout function for `createClient`. It reads the flag `versimo-<module>` from one `/flags?v=2` request per TTL, sent with the distinct id `versimo` and no user data. See [PostHog flags drive next](usage.md#posthog-flags-drive-next).
|
|
150
|
+
|
|
151
|
+
### `rolloutFromFlag(flag)`
|
|
152
|
+
|
|
153
|
+
The share on next from one flag of a PostHog `/flags?v=2` answer, or null to keep the record's rollout. Flag off gives 0. Flag on with a payload whose `rollout` is a whole number from 0 to 100 gives that number. The payload may be a JSON string or an object.
|
|
154
|
+
|
|
155
|
+
### `postHogFlagKey(module)`
|
|
156
|
+
|
|
157
|
+
`versimo-<module>`.
|
|
158
|
+
|
|
159
|
+
## OpenFeature provider
|
|
160
|
+
|
|
161
|
+
### `new VersimoProvider(client)`
|
|
162
|
+
|
|
163
|
+
Has `metadata`, `resolveStringEvaluation`, and the boolean, number and object methods. See [usage](usage.md#openfeature).
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
resolveStringEvaluation(flagKey: string, defaultValue: string, context?: EvaluationContext):
|
|
167
|
+
Promise<{ value: string; variant?: string; reason?: string; errorCode?: string; errorMessage?: string }>
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
`context.targetingKey` is used as the user id.
|
|
171
|
+
|
|
172
|
+
## Semver helpers
|
|
173
|
+
|
|
174
|
+
- `isSemver(value)` returns true for a valid semver string.
|
|
175
|
+
- `parseSemver(value)` returns `{ major, minor, patch, prerelease }` or throws a `TypeError`.
|
|
176
|
+
- `compareSemver(a, b)` returns -1, 0 or 1. Prerelease rules follow the spec. Build metadata is ignored.
|
|
177
|
+
- `SEMVER_PATTERN` is the regular expression source, the same one the JSON Schema uses.
|
|
178
|
+
|
|
179
|
+
## Errors
|
|
180
|
+
|
|
181
|
+
### `VersimoError`
|
|
182
|
+
|
|
183
|
+
An `Error` with a `code`.
|
|
184
|
+
|
|
185
|
+
| Code | When |
|
|
186
|
+
| --- | --- |
|
|
187
|
+
| `INVALID_INPUT` | A bad argument, such as a bad version, a bad rollout or a bad module slug |
|
|
188
|
+
| `INVALID_RECORD` | A fetched record failed `validateRecord` |
|
|
189
|
+
| `NOT_FOUND` | The source has no record for the module |
|
|
190
|
+
| `SOURCE_ERROR` | The source threw, or the HTTP request failed |
|
|
191
|
+
|
|
192
|
+
## GitHub
|
|
193
|
+
|
|
194
|
+
`versimo/github` holds what the Versimo GitHub app needs. It is a separate entry point, so the main package stays small. See [github-app.md](github-app.md) for the full picture.
|
|
195
|
+
|
|
196
|
+
```ts
|
|
197
|
+
parseModuleTag(tag: string): { module: string; version: string } | null
|
|
198
|
+
modulesFromTags(tags: Iterable<string>): Record<string, string[]>
|
|
199
|
+
recordFromTags(module: string, versions: readonly string[], now?: Date | string | number): ModuleRecord | null
|
|
200
|
+
|
|
201
|
+
createAppJwt(opts: { appId: string | number; privateKey: string; now?: Date | string | number }): Promise<string>
|
|
202
|
+
getInstallationToken(opts: { jwt: string; installationId: string | number; fetch?; apiUrl? }): Promise<{ token: string; expiresAt: string }>
|
|
203
|
+
verifyWebhookSignature(secret: string, body: string | Uint8Array, signature: string | null | undefined): Promise<boolean>
|
|
204
|
+
|
|
205
|
+
createGitHub(opts: { token: string; fetch?: typeof fetch; apiUrl?: string }): GitHubClient
|
|
206
|
+
|
|
207
|
+
interface GitHubClient {
|
|
208
|
+
listTags(owner: string, repo: string): Promise<string[]>;
|
|
209
|
+
moduleVersions(owner: string, repo: string): Promise<Record<string, string[]>>;
|
|
210
|
+
openPullRequest(input: PullRequestInput): Promise<PullRequestResult>;
|
|
211
|
+
}
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
`openPullRequest` creates a new branch, commits the files to it, opens the pull request and turns on auto merge (squash by default, `autoMerge: false` to skip). It refuses a head branch named `main`, `master`, the default branch or the base, and fails if the branch already exists. It never moves an existing branch. GitHub errors throw `VersimoError` with `NOT_FOUND` or `SOURCE_ERROR`, bad input throws `INVALID_INPUT`.
|
package/docs/concepts.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# Concepts
|
|
2
|
+
|
|
3
|
+
## No staging
|
|
4
|
+
|
|
5
|
+
A staging environment is a copy of production that is never quite the same. It has other data, other traffic and other load. Bugs hide there and show up on live.
|
|
6
|
+
|
|
7
|
+
Versimo skips it. You test on live, with real users, in small steps. The price of a mistake is kept small by three rules.
|
|
8
|
+
|
|
9
|
+
## Three versions
|
|
10
|
+
|
|
11
|
+
Each module has three versions at any time.
|
|
12
|
+
|
|
13
|
+
- **Last** is the version that was current before. It is the way back.
|
|
14
|
+
- **Current** is the version most users get.
|
|
15
|
+
- **Next** is the version under test on live. It may be empty.
|
|
16
|
+
|
|
17
|
+
A module is one deployable piece of your product, such as billing, sign-in or search. Each has its own record and its own versions.
|
|
18
|
+
|
|
19
|
+
## Rollout
|
|
20
|
+
|
|
21
|
+
A rollout is a whole number from 0 to 100. It is the share of users who get next. At 0, nobody does. At 100, everybody does.
|
|
22
|
+
|
|
23
|
+
Every user has a bucket from 0 to 99 for each module. The bucket comes from a stable hash of the module and the user id. A user is on next when their bucket is below the rollout.
|
|
24
|
+
|
|
25
|
+
Two things follow from this.
|
|
26
|
+
|
|
27
|
+
- The same user always lands in the same bucket, so they do not flip between versions on each visit.
|
|
28
|
+
- Raising the rollout only adds users. Nobody who was on next is moved back to current.
|
|
29
|
+
|
|
30
|
+
Buckets differ per module, so the same users are not always the first to see every new version.
|
|
31
|
+
|
|
32
|
+
The hash is FNV-1a over the UTF-8 bytes of the module, a zero byte and the user id. A murmur3 style finaliser mixes the bits, and the result is taken modulo 100. Another language can port it in a few lines.
|
|
33
|
+
|
|
34
|
+
## Promote
|
|
35
|
+
|
|
36
|
+
Promote is how next becomes current.
|
|
37
|
+
|
|
38
|
+
- Current becomes last.
|
|
39
|
+
- Next becomes current.
|
|
40
|
+
- Next is emptied and the rollout goes back to 0.
|
|
41
|
+
|
|
42
|
+
Promote when next has run on live for long enough and looks good.
|
|
43
|
+
|
|
44
|
+
## Health and rollback
|
|
45
|
+
|
|
46
|
+
Each module can have health checks: an error rate, a latency and a custom URL. When current fails one, Versimo rolls back to last on its own, records the failed checks in the history, and sends an alert. See [Health checks](health.md).
|
|
47
|
+
|
|
48
|
+
You can also set `healthy` to `false` on the record from your own tooling.
|
|
49
|
+
|
|
50
|
+
While `healthy` is `false`, every user gets last. This is the automatic step back and it needs no deploy. If there is no last, users stay on current, because there is nothing better to serve.
|
|
51
|
+
|
|
52
|
+
`rollback` makes the step back permanent in the record. Last becomes current. The failed version and any drafted next are dropped, so there is nothing to roll back to until the next promote.
|
|
53
|
+
|
|
54
|
+
## Patch
|
|
55
|
+
|
|
56
|
+
The fix for a failed version ships as a semver patch. After a rollback to `2.3.1`, a fix for `2.4.0` is drafted as `2.4.1`. It goes through the same path as any next: draft, rollout, promote. Pass `{ patch: true }` to `draftNext` to record it as a patch in the history.
|
|
57
|
+
|
|
58
|
+
## The whole cycle
|
|
59
|
+
|
|
60
|
+
1. `draftNext(record, '2.5.0')` puts 2.5.0 in next at 0%.
|
|
61
|
+
2. `setRollout(record, 5)`, then 25, then 100 as confidence grows.
|
|
62
|
+
3. `promote(record)` makes 2.5.0 current.
|
|
63
|
+
4. If it fails, `healthy` goes false and users get last at once.
|
|
64
|
+
5. `rollback(record)` records the step back.
|
|
65
|
+
6. `draftNext(record, '2.5.1', { patch: true })` starts the fix.
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# GitHub app
|
|
2
|
+
|
|
3
|
+
The Versimo GitHub app connects a repository to a Versimo project. It does two things and no more:
|
|
4
|
+
|
|
5
|
+
1. It reads the repository's tags to find your modules and their versions.
|
|
6
|
+
2. It opens pull requests for drafts and patches, with auto merge turned on, so they merge once your checks are green.
|
|
7
|
+
|
|
8
|
+
It never pushes to `main`. Every change arrives as a normal pull request that your branch rules and reviews apply to.
|
|
9
|
+
|
|
10
|
+
## Modules and versions come from tags
|
|
11
|
+
|
|
12
|
+
Tag each release of a module as `module/vX.Y.Z`:
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
git tag -s billing/v2.4.0 -m "billing 2.4.0"
|
|
16
|
+
git push origin billing/v2.4.0
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
The module is a lower-case slug, the same rule as the record's `module` field. Other tags, such as `v1.0.0` or `packages/go/v0.2.0`, are ignored.
|
|
20
|
+
|
|
21
|
+
For a module with no record yet, Versimo starts one from its tags: current is the newest stable version, last is the one before it, next is empty and rollout is 0. Pre-release tags such as `billing/v2.5.0-rc.1` are listed but never become current on their own. You choose when to draft next.
|
|
22
|
+
|
|
23
|
+
## Permissions
|
|
24
|
+
|
|
25
|
+
| Permission | Level | Why |
|
|
26
|
+
| --- | --- | --- |
|
|
27
|
+
| Metadata | Read | Required by GitHub for every app |
|
|
28
|
+
| Contents | Write | Read code and tags, and create the branch a pull request comes from |
|
|
29
|
+
| Pull requests | Write | Open pull requests and turn on auto merge |
|
|
30
|
+
|
|
31
|
+
Events: `create` (a new tag adds its module) and `pull_request` (a draft or patch merged or closed). GitHub also sends `installation` events, so uninstalling the app disconnects its projects.
|
|
32
|
+
|
|
33
|
+
GitHub has no permission for "create branches but not push to main". Contents write is the narrowest level that lets an app open a pull request from its own branch. Versimo keeps the promise in two ways:
|
|
34
|
+
|
|
35
|
+
- **In the code.** The client in `versimo/github` has exactly one write: it creates a new branch from the base, commits to it and opens a pull request. It creates refs and never updates or deletes one, and it refuses a head branch named `main`, `master`, the default branch or the base. There is no code path that moves an existing branch.
|
|
36
|
+
- **In your repository.** Add a branch ruleset on your default branch that requires a pull request before merging. Then no app, Versimo included, can push to it directly.
|
|
37
|
+
|
|
38
|
+
Auto merge needs "Allow auto-merge" on in the repository's settings. When it is off, the pull request still opens and Versimo tells you auto merge could not be turned on.
|
|
39
|
+
|
|
40
|
+
## Using it from code
|
|
41
|
+
|
|
42
|
+
The pieces the Versimo app uses are in the library, under `versimo/github`. They use `fetch` and WebCrypto only, so they run on Node 20, Cloudflare Workers and Deno.
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import { createAppJwt, getInstallationToken, createGitHub, recordFromTags } from 'versimo/github';
|
|
46
|
+
|
|
47
|
+
const jwt = await createAppJwt({ appId: env.GITHUB_APP_ID, privateKey: env.GITHUB_APP_PRIVATE_KEY });
|
|
48
|
+
const { token } = await getInstallationToken({ jwt, installationId });
|
|
49
|
+
const github = createGitHub({ token });
|
|
50
|
+
|
|
51
|
+
const modules = await github.moduleVersions('acme', 'shop');
|
|
52
|
+
// { billing: ['2.4.0', '2.3.1'], search: ['1.0.0'] }
|
|
53
|
+
const record = recordFromTags('billing', modules.billing!);
|
|
54
|
+
|
|
55
|
+
const pr = await github.openPullRequest({
|
|
56
|
+
owner: 'acme',
|
|
57
|
+
repo: 'shop',
|
|
58
|
+
head: 'versimo/billing-v2.4.1',
|
|
59
|
+
title: 'billing 2.4.1',
|
|
60
|
+
files: [{ path: 'src/billing/fix.ts', content: '...' }],
|
|
61
|
+
});
|
|
62
|
+
// { number: 7, url: '...', autoMerge: true }
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Check webhooks before you trust them:
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import { verifyWebhookSignature } from 'versimo/github';
|
|
69
|
+
|
|
70
|
+
const body = await request.text();
|
|
71
|
+
if (!(await verifyWebhookSignature(env.GITHUB_WEBHOOK_SECRET, body, request.headers.get('x-hub-signature-256')))) {
|
|
72
|
+
return new Response('bad signature', { status: 401 });
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Registering the app
|
|
77
|
+
|
|
78
|
+
This is done once, by the owner of the `luca-sia` organisation. The manifest is in [`github-app/manifest.json`](../github-app/manifest.json).
|
|
79
|
+
|
|
80
|
+
1. Open `https://github.com/organizations/luca-sia/settings/apps/new` and register the app from the manifest (GitHub's [manifest flow](https://docs.github.com/en/apps/sharing-github-apps/registering-a-github-app-from-a-manifest)), or fill in the same values by hand.
|
|
81
|
+
2. Generate a private key and a webhook secret.
|
|
82
|
+
Keep "Request user authorization (OAuth) during installation" on, as the manifest sets it. The API checks with the installer's own token that they can see the installation before it connects a repo.
|
|
83
|
+
3. Generate a client secret too. Save the app id, client id, client secret, private key and webhook secret in 1Password, never in the repository.
|
|
84
|
+
4. Give them to the API Worker (`api/`): `GITHUB_APP_ID` and `GITHUB_CLIENT_ID` as vars, `GITHUB_CLIENT_SECRET`, `GITHUB_APP_PRIVATE_KEY` and `GITHUB_WEBHOOK_SECRET` as secrets. See [api/README.md](../api/README.md).
|
|
85
|
+
|
|
86
|
+
The webhook and callback URLs in the manifest point at the API Worker on `versimo.dev`: `/api/github/webhook` and `/app/github/callback`.
|
|
87
|
+
|
|
88
|
+
## Connecting a repo in the app
|
|
89
|
+
|
|
90
|
+
On a project, Connect GitHub sends you to `GET /api/projects/:id/github/install`, which goes on to GitHub. Pick the repos the app may see. Back in the app, a single repo connects at once. With several, you pick one. Every module with a stable tag is added to the project, and later a new module's first stable tag adds it. Tags never change a module that exists.
|
package/docs/health.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# Health checks
|
|
2
|
+
|
|
3
|
+
If current fails, traffic goes back to last on its own and the fix ships as a semver patch. This page is how Versimo does the first half.
|
|
4
|
+
|
|
5
|
+
## Checks
|
|
6
|
+
|
|
7
|
+
Each module gets its own limits. Leave one out to turn it off.
|
|
8
|
+
|
|
9
|
+
| Check | Field | Fails when |
|
|
10
|
+
| --- | --- | --- |
|
|
11
|
+
| Error rate | `maxErrorRate` (0 to 1) | `errors / requests` is above it, once there are at least `minRequests` requests (default 1) |
|
|
12
|
+
| Latency | `maxLatencyMs` | the measured latency (usually p95) is above it |
|
|
13
|
+
| Custom URL | `url` | the URL does not answer with a 2xx status, or cannot be reached |
|
|
14
|
+
|
|
15
|
+
A value equal to the limit passes.
|
|
16
|
+
|
|
17
|
+
## What happens on a failure
|
|
18
|
+
|
|
19
|
+
1. Current steps back to last. This is `rollback`: last becomes current, next and the rollout are cleared.
|
|
20
|
+
2. The history entry says why, for example `{ "action": "rollback", "by": "versimo-health", "reason": "health: errorRate" }`.
|
|
21
|
+
3. An alert is sent.
|
|
22
|
+
|
|
23
|
+
If there is no last, nothing changes and only the alert goes out, once per failing version.
|
|
24
|
+
|
|
25
|
+
Then draft the fix as a patch, `draftNext(record, '2.4.1', { patch: true })`, and roll it out as usual.
|
|
26
|
+
|
|
27
|
+
## Running it
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { createHealthMonitor } from 'versimo';
|
|
31
|
+
|
|
32
|
+
const monitor = createHealthMonitor({
|
|
33
|
+
store: {
|
|
34
|
+
read: (module) => loadRecord(module), // your record store
|
|
35
|
+
write: (record) => saveRecord(record),
|
|
36
|
+
},
|
|
37
|
+
checks: {
|
|
38
|
+
billing: { maxErrorRate: 0.02, maxLatencyMs: 800, url: 'https://billing.example.com/health', minRequests: 100 },
|
|
39
|
+
},
|
|
40
|
+
posthog: {
|
|
41
|
+
host: 'https://eu.posthog.com',
|
|
42
|
+
projectId: process.env.POSTHOG_PROJECT_ID!,
|
|
43
|
+
apiKey: process.env.POSTHOG_PERSONAL_API_KEY!,
|
|
44
|
+
},
|
|
45
|
+
observe: async (module) => ({ latencyMs: await p95(module) }), // optional, your own numbers
|
|
46
|
+
alert: process.env.SLACK_WEBHOOK_URL, // or a function
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const stop = monitor.start(['billing', 'sign-in'], 60_000);
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
`monitor.check('billing')` runs once, which suits a cron job or a scheduled Worker.
|
|
53
|
+
|
|
54
|
+
The numbers come from three places, and later ones win: PostHog, the URL check, then your `observe` function.
|
|
55
|
+
|
|
56
|
+
## PostHog error tracking
|
|
57
|
+
|
|
58
|
+
When `posthog` is set and a module has `maxErrorRate`, the monitor asks PostHog how many events and how many `$exception` events there were for that module and version in the last `windowMinutes` (default 5).
|
|
59
|
+
|
|
60
|
+
For this to work, tag your events with the module and the version the user got:
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
posthog.register({ versimo_module: 'billing', versimo_version: version });
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
The property names can be changed with `moduleProperty` and `versionProperty`. The API key needs query read access. Keep it in your secret store, never in the repo.
|
|
67
|
+
|
|
68
|
+
## Alerts
|
|
69
|
+
|
|
70
|
+
A webhook URL gets a JSON POST with `module`, `at`, `version`, `rolledBack`, `current`, `failures` and `text`. The `text` field makes it work with a Slack incoming webhook as it is:
|
|
71
|
+
|
|
72
|
+
> Versimo: billing 2.4.0 failed its health checks (errorRate). Rolled back to 2.3.1.
|
|
73
|
+
|
|
74
|
+
## Other languages
|
|
75
|
+
|
|
76
|
+
`evaluateHealth` and `checkHealth` are in every port and give the same result, checked by the `health` cases in [spec/vectors.json](../spec/vectors.json). The monitor is TypeScript only for now. In another language, measure, call `checkHealth`, and save the record when `rolledBack` is true.
|
package/docs/porting.md
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# Porting Versimo
|
|
2
|
+
|
|
3
|
+
Every language port must give the same answer as Versimo for TypeScript for the same record and user. A user must land on the same version whichever language the server or the app is written in. This page says what to copy and how to check it.
|
|
4
|
+
|
|
5
|
+
The reference is the TypeScript source in `src/`. The shared test data is [spec/vectors.json](../spec/vectors.json).
|
|
6
|
+
|
|
7
|
+
## What a port contains
|
|
8
|
+
|
|
9
|
+
- `bucketFor`
|
|
10
|
+
- `resolve` and `resolveVersion`
|
|
11
|
+
- `draftNext`, `promote`, `rollback` and `setRollout`
|
|
12
|
+
- `validateRecord`
|
|
13
|
+
- Semver check and compare
|
|
14
|
+
- `evaluateHealth` and `checkHealth`
|
|
15
|
+
- A client with a source (a function, or a URL), a TTL cache and stale-on-error
|
|
16
|
+
|
|
17
|
+
A PostHog rollout source is optional. If a port has one, `rolloutFromFlag` must pass the `posthog` vectors, and the client applies the share to the record before resolving. PostHog only sets the share. The bucket always decides who is on next.
|
|
18
|
+
|
|
19
|
+
An OpenFeature provider is optional. Add it only when the language has a way to do it without a dependency.
|
|
20
|
+
|
|
21
|
+
## The bucket
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
bytes = UTF-8(module + "\0" + userId)
|
|
25
|
+
h = 0x811c9dc5
|
|
26
|
+
for each byte:
|
|
27
|
+
h = h XOR byte
|
|
28
|
+
h = (h * 0x01000193) mod 2^32
|
|
29
|
+
h = h XOR (h >> 16)
|
|
30
|
+
h = (h * 0x85ebca6b) mod 2^32
|
|
31
|
+
h = h XOR (h >> 13)
|
|
32
|
+
h = (h * 0xc2b2ae35) mod 2^32
|
|
33
|
+
h = h XOR (h >> 16)
|
|
34
|
+
bucket = h mod 100
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
All arithmetic is on unsigned 32-bit integers. The first part is FNV-1a. The rest is the murmur3 finaliser, which spreads the low bits so that `mod 100` is even.
|
|
38
|
+
|
|
39
|
+
Hash the bytes of the string as given. Do not normalise Unicode. Input must be valid Unicode, so lone surrogates are out of scope.
|
|
40
|
+
|
|
41
|
+
## Resolving
|
|
42
|
+
|
|
43
|
+
1. If `healthy` is false, return `last`, or `current` when there is no last. Reason `UNHEALTHY`, no bucket.
|
|
44
|
+
2. If the user id is missing or empty, return `current`. Reason `NO_USER`, no bucket.
|
|
45
|
+
3. Compute the bucket. If `next` is set and `bucket < rollout`, return `next` with reason `ROLLOUT`.
|
|
46
|
+
4. Otherwise return `current` with reason `DEFAULT`.
|
|
47
|
+
|
|
48
|
+
The channel is `next`, `current` or `last` to match the version returned.
|
|
49
|
+
|
|
50
|
+
## Transitions
|
|
51
|
+
|
|
52
|
+
Each transition returns a new record, sets `updatedAt` to the time of the change, and appends a history entry `{ at, action, from, to, by? }`. The time is written as UTC with milliseconds, for example `2026-04-01T10:00:00.000Z`. The input is never changed. Errors use the code `INVALID_INPUT`.
|
|
53
|
+
|
|
54
|
+
| Function | Changes | History action, from and to |
|
|
55
|
+
| --- | --- | --- |
|
|
56
|
+
| `draftNext(version)` | `next = version`, `rollout = 0` | `draft` (or `patch`), `next` or else `current`, version |
|
|
57
|
+
| `promote` | `last = current`, `current = next`, `next = null`, `rollout = 0`, `healthy = true` | `promote`, current, next |
|
|
58
|
+
| `rollback` | `last = null`, `current = last`, `next = null`, `rollout = 0`, `healthy = true` | `rollback`, current, last |
|
|
59
|
+
| `setRollout(n)` | `rollout = n` | `rollout`, old rollout as text, new rollout as text |
|
|
60
|
+
|
|
61
|
+
Every transition also takes an optional `reason`. When given, the history entry gets a `reason` key after `by`.
|
|
62
|
+
|
|
63
|
+
Rules that raise an error:
|
|
64
|
+
|
|
65
|
+
- `draftNext`: the version is not semver, or is not greater than `current`.
|
|
66
|
+
- `promote`: `next` is null.
|
|
67
|
+
- `rollback`: `last` is null.
|
|
68
|
+
- `setRollout`: `n` is not an integer from 0 to 100, or `n > 0` while `next` is null.
|
|
69
|
+
|
|
70
|
+
## Health
|
|
71
|
+
|
|
72
|
+
Checks: `maxErrorRate` (0 to 1), `maxLatencyMs` (0 or more), `url` (non-empty), `minRequests` (integer, default 1). Observation: `requests`, `errors` (integers), `latencyMs` (0 or more), `urlStatus` (integer, 0 for unreachable). A missing key turns the check off or skips it.
|
|
73
|
+
|
|
74
|
+
1. Reject bad input with `INVALID_INPUT`: a limit out of range, a negative or fractional count, `errors` above `requests`, NaN or infinity.
|
|
75
|
+
2. Error rate: when `maxErrorRate`, `requests` and `errors` are all set and `requests >= max(1, minRequests)`, compute `errors / requests` as a double. It fails when the rate is above the limit.
|
|
76
|
+
3. Latency: fails when `latencyMs` is above `maxLatencyMs`.
|
|
77
|
+
4. URL: fails when `url` is set and `urlStatus` is set and outside 200 to 299. The limit is null.
|
|
78
|
+
5. Failures come in that order, as `{ check, value, limit }`.
|
|
79
|
+
6. `checkHealth`: when the verdict fails and `last` is set, call `rollback` with `by` defaulting to `versimo-health` and `reason` set to `health: ` and the failed check names joined by `, `. Otherwise return the record unchanged with `rolledBack` false.
|
|
80
|
+
|
|
81
|
+
Limits are inclusive: a value equal to the limit passes. The runner that measures and alerts (`createHealthMonitor`) is TypeScript only for now. Ports need the pure part.
|
|
82
|
+
|
|
83
|
+
## Validation
|
|
84
|
+
|
|
85
|
+
`validateRecord` takes any JSON value and never fails. It returns `ok` and a list of error messages. Each message starts with the name of the field, such as `rollout must be an integer from 0 to 100` or `history[1].at must be an ISO 8601 date-time`. Copy the messages from `src/validate.ts`. The vectors compare only the field name at the start of each message, and their order.
|
|
86
|
+
|
|
87
|
+
Things that are easy to get wrong:
|
|
88
|
+
|
|
89
|
+
- A missing `last` or `next` is an error. An explicit `null` is fine.
|
|
90
|
+
- A missing history entry `from` or `to` is an error. `null` is fine.
|
|
91
|
+
- The ordering checks (`next` above `current`, `last` below `current`, `rollout` 0 without a next) run only when no other error was found.
|
|
92
|
+
- A boolean is not an integer. A number such as `10.0` is.
|
|
93
|
+
- Unknown keys are errors, in the order they appear in the record. Languages with unordered maps may sort them.
|
|
94
|
+
- Slugs, semver and date-times must match the whole string. In many regex engines `$` also matches before a trailing newline, so use an end of text anchor or check the match length. The vectors include these cases.
|
|
95
|
+
- Use `[0-9]` for digits, not `\d`, which matches other scripts in some engines.
|
|
96
|
+
- The date-time check follows the reference, which is lenient. It accepts any day from 1 to 31 in any month, and `24:00:00`. It rejects month 13, hour 25, minute or second 60, and offsets of 24 hours or more.
|
|
97
|
+
|
|
98
|
+
## Semver
|
|
99
|
+
|
|
100
|
+
The pattern is the one in `schema/module-record.schema.json`. Compare as the semver spec says and ignore build metadata. Compare number parts as digit strings, by length and then by text, so large numbers cannot overflow. A prerelease sorts below its release. Numeric prerelease identifiers sort below text ones.
|
|
101
|
+
|
|
102
|
+
## The client
|
|
103
|
+
|
|
104
|
+
- The source is a function from module slug to record or nothing, or a URL.
|
|
105
|
+
- A URL containing `{module}` has it replaced. Any other URL is a base, and the client fetches `<base>/<module>.json` with trailing slashes on the base removed.
|
|
106
|
+
- A 404 means no record. Any other failure or non-2xx status is `SOURCE_ERROR`. A body that is not JSON is `SOURCE_ERROR`.
|
|
107
|
+
- Check the module slug first. A bad slug is `INVALID_INPUT` and never reaches the source.
|
|
108
|
+
- No record is `NOT_FOUND`. A record that fails validation is `INVALID_RECORD`.
|
|
109
|
+
- Cache each validated record for the TTL, 30 seconds by default.
|
|
110
|
+
- Concurrent loads of one module share a single request.
|
|
111
|
+
- When a refresh fails with `SOURCE_ERROR` and an old record exists, return the old record and restart its TTL. Do not hide `NOT_FOUND` or `INVALID_RECORD`.
|
|
112
|
+
- Provide a way to clear one record or all of them, and a clock you can replace in tests.
|
|
113
|
+
|
|
114
|
+
## The vectors
|
|
115
|
+
|
|
116
|
+
`spec/vectors.json` is generated from the TypeScript code by `npm run spec:generate`. Do not edit it by hand. The TypeScript tests fail if the file is out of date. It has these parts:
|
|
117
|
+
|
|
118
|
+
| Key | Each case has | The port must |
|
|
119
|
+
| --- | --- | --- |
|
|
120
|
+
| `bucket` | `module`, `userId`, `bucket` | Return that bucket |
|
|
121
|
+
| `resolve` | `name`, `record`, `userId` (null for none), `expected` | Return the same version, channel, reason and bucket |
|
|
122
|
+
| `transitions` | `name`, `op`, `args`, `record`, `now`, `by`, then `expected` or `error` | Return the expected record, or fail with that error code |
|
|
123
|
+
| `validation` | `name`, `record`, `ok`, `errors` | Agree on `ok` and on the field names, in order |
|
|
124
|
+
| `semver.compare` | `a`, `b`, `expected` | Return -1, 0 or 1 |
|
|
125
|
+
| `semver.valid` | `value`, `valid` | Agree on validity |
|
|
126
|
+
| `health` | `name`, `record`, `checks`, `observation`, `now`, `by`, then `expected` or `error` | Return the same record, verdict and `rolledBack`, or fail with that error code |
|
|
127
|
+
|
|
128
|
+
For transitions, `op` is `promote`, `rollback`, `setRollout` (with `args.n`) or `draftNext` (with `args.version` and an optional `args.patch`). `now` is a fixed time in UTC with milliseconds. A typed port can skip a case that its types rule out, such as a fractional rollout, as long as the type system rejects it.
|
|
129
|
+
|
|
130
|
+
A new port reads the file from the repository, runs every case, and adds its own tests for the client. Then add a job to `.github/workflows/ci.yml` and a row to the table in the README.
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# Record format
|
|
2
|
+
|
|
3
|
+
A record holds the release state of one module. It is a JSON document. The schema is [schema/module-record.schema.json](../schema/module-record.schema.json) (JSON Schema 2020-12). An example is [schema/examples/billing.json](../schema/examples/billing.json).
|
|
4
|
+
|
|
5
|
+
```json
|
|
6
|
+
{
|
|
7
|
+
"$schema": "https://versimo.dev/schema/module-record.schema.json",
|
|
8
|
+
"module": "billing",
|
|
9
|
+
"last": "2.3.1",
|
|
10
|
+
"current": "2.4.0",
|
|
11
|
+
"next": "2.5.0",
|
|
12
|
+
"rollout": 10,
|
|
13
|
+
"healthy": true,
|
|
14
|
+
"updatedAt": "2026-03-12T09:30:00Z",
|
|
15
|
+
"history": [
|
|
16
|
+
{ "at": "2026-03-12T09:30:00Z", "action": "rollout", "from": "0", "to": "10", "by": "alex" }
|
|
17
|
+
]
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Fields
|
|
22
|
+
|
|
23
|
+
| Field | Type | Required | Meaning |
|
|
24
|
+
| --- | --- | --- | --- |
|
|
25
|
+
| `$schema` | string | no | Link to the schema, for editors |
|
|
26
|
+
| `module` | string | yes | Lower-case slug such as `billing` or `sign-in` |
|
|
27
|
+
| `last` | semver or null | yes | The version before current. The rollback target |
|
|
28
|
+
| `current` | semver | yes | The version most users get |
|
|
29
|
+
| `next` | semver or null | yes | The version under test on live |
|
|
30
|
+
| `rollout` | integer 0 to 100 | yes | Share of users on next |
|
|
31
|
+
| `healthy` | boolean | yes | Health of current. When false, users get last |
|
|
32
|
+
| `updatedAt` | ISO 8601 date-time | yes | Time of the last change |
|
|
33
|
+
| `history` | array | no | Log of changes, oldest first |
|
|
34
|
+
|
|
35
|
+
No other fields are allowed.
|
|
36
|
+
|
|
37
|
+
## History entries
|
|
38
|
+
|
|
39
|
+
| Field | Type | Required | Meaning |
|
|
40
|
+
| --- | --- | --- | --- |
|
|
41
|
+
| `at` | ISO 8601 date-time | yes | When it happened |
|
|
42
|
+
| `action` | string | yes | `draft`, `promote`, `rollback`, `rollout` or `patch` |
|
|
43
|
+
| `from` | string or null | yes | Value before the change |
|
|
44
|
+
| `to` | string or null | yes | Value after the change |
|
|
45
|
+
| `by` | string | no | Who made the change |
|
|
46
|
+
| `reason` | string | no | Why. An automatic rollback writes the failed checks, for example `health: errorRate` |
|
|
47
|
+
|
|
48
|
+
For `draft`, `promote`, `rollback` and `patch`, `from` and `to` are versions. For `rollout` they are the old and new percentages as strings, for example `"10"` and `"25"`.
|
|
49
|
+
|
|
50
|
+
## Extra rules
|
|
51
|
+
|
|
52
|
+
The schema checks shapes. `validateRecord` also checks these rules, which a schema cannot express.
|
|
53
|
+
|
|
54
|
+
- `next` must be greater than `current`.
|
|
55
|
+
- `last` must be lower than `current`.
|
|
56
|
+
- `rollout` must be 0 when `next` is null.
|
|
57
|
+
|
|
58
|
+
Versions follow [semver 2.0](https://semver.org). A leading `v` is not allowed.
|
|
59
|
+
|
|
60
|
+
## Where records live
|
|
61
|
+
|
|
62
|
+
Anywhere you can read JSON. A file on a CDN, a row in a database, a config service or a feature flag payload. See [usage](usage.md) for examples.
|