@zabaca/zbc 0.11.0 → 0.13.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 +63 -0
- package/package.json +2 -1
- package/templates/apps/inbox/package.json +1 -1
- package/templates/apps/walgit/CONTEXT.md +135 -0
- package/templates/apps/walgit/Dockerfile +40 -18
- package/templates/apps/walgit/README.md +518 -55
- package/templates/apps/walgit/e2e/README.md +100 -6
- package/templates/apps/walgit/e2e/harness.ts +262 -1
- package/templates/apps/walgit/e2e/live.ts +588 -0
- package/templates/apps/walgit/e2e/scenarios.ts +367 -5
- package/templates/apps/walgit/e2e/suite.ts +5 -5
- package/templates/apps/walgit/examples/watch.ts +209 -0
- package/templates/apps/walgit/package.json +12 -3
- package/templates/apps/walgit/registry.json +6 -11
- package/templates/apps/walgit/shared/capabilities.ts +189 -0
- package/templates/apps/walgit/shared/container-env.ts +114 -0
- package/templates/apps/walgit/shared/credentials.ts +81 -0
- package/templates/apps/walgit/shared/events.ts +277 -0
- package/templates/apps/walgit/shared/landing.ts +1241 -0
- package/templates/apps/walgit/shared/llms.ts +463 -0
- package/templates/apps/walgit/shared/outbox.ts +266 -0
- package/templates/apps/walgit/shared/policy.ts +59 -0
- package/templates/apps/walgit/shared/protocol.ts +197 -0
- package/templates/apps/walgit/shared/provenance.ts +154 -0
- package/templates/apps/walgit/shared/ref-cache.ts +181 -0
- package/templates/apps/walgit/shared/telemetry.ts +219 -0
- package/templates/apps/walgit/src/announce-main.ts +37 -0
- package/templates/apps/walgit/src/announce.ts +98 -0
- package/templates/apps/walgit/src/append-only.ts +112 -0
- package/templates/apps/walgit/src/cache.ts +172 -0
- package/templates/apps/walgit/src/cli.ts +493 -0
- package/templates/apps/walgit/src/compact.ts +25 -23
- package/templates/apps/walgit/src/delete-repo.ts +237 -0
- package/templates/apps/walgit/src/expire.ts +248 -0
- package/templates/apps/walgit/src/gc.ts +8 -16
- package/templates/apps/walgit/src/git-backend.ts +29 -0
- package/templates/apps/walgit/src/git.ts +78 -0
- package/templates/apps/walgit/src/hook-main.ts +259 -17
- package/templates/apps/walgit/src/hooks.ts +4 -3
- package/templates/apps/walgit/src/http.ts +237 -45
- package/templates/apps/walgit/src/instructions.ts +240 -0
- package/templates/apps/walgit/src/keys.ts +126 -0
- package/templates/apps/walgit/src/limits.ts +158 -0
- package/templates/apps/walgit/src/materialize.bench.ts +10 -10
- package/templates/apps/walgit/src/materialize.ts +48 -40
- package/templates/apps/walgit/src/mkdir-lock.ts +66 -0
- package/templates/apps/walgit/src/orphans.ts +2 -5
- package/templates/apps/walgit/src/pending.ts +201 -0
- package/templates/apps/walgit/src/push-cert.ts +254 -0
- package/templates/apps/walgit/src/push.ts +233 -46
- package/templates/apps/walgit/src/reconcile.ts +5 -8
- package/templates/apps/walgit/src/repo.ts +22 -77
- package/templates/apps/walgit/src/server.ts +148 -26
- package/templates/apps/walgit/src/signers.ts +577 -0
- package/templates/apps/walgit/src/store-env.ts +5 -4
- package/templates/apps/walgit/src/store.ts +47 -24
- package/templates/apps/walgit/src/usage.ts +404 -0
- package/templates/apps/walgit/src/verify.ts +171 -0
- package/templates/apps/walgit/src/wal-index.ts +189 -11
- package/templates/apps/walgit/tsconfig.json +8 -2
- package/templates/apps/walgit/tsconfig.worker.json +24 -0
- package/templates/apps/walgit/worker/events-do.ts +259 -0
- package/templates/apps/walgit/worker/index.ts +572 -0
- package/templates/apps/walgit/wrangler.jsonc +95 -0
- package/templates/apps/warehouse/package.json +1 -1
- package/templates/infra/modules/cloudflare/index.ts +77 -6
- package/templates/infra/modules/cloudflare-access/index.ts +85 -14
- package/templates/infra/modules/fly/index.ts +150 -10
- package/templates/infra/modules/turso/index.ts +43 -3
- package/templates/infra/package.json +1 -1
- package/templates/apps/walgit/entrypoint.sh +0 -59
- package/templates/apps/walgit/fly.toml +0 -52
- package/templates/apps/walgit/src/ssh-shell.ts +0 -36
- package/templates/apps/walgit/sshd_config +0 -38
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the fan-out remembers about refs.
|
|
3
|
+
*
|
|
4
|
+
* A handshake needs the current sha of everything a subscriber watches, and in
|
|
5
|
+
* the first cut that always asked the container — so a connect could wake a
|
|
6
|
+
* sleeping container to be told what the fan-out had just finished announcing.
|
|
7
|
+
* This is the copy that removes that: ask once per repository, then answer from
|
|
8
|
+
* memory and keep it current from the announcements already flowing through.
|
|
9
|
+
*
|
|
10
|
+
* The copy is DERIVED and never authoritative. `index.json` remains the source
|
|
11
|
+
* of truth for refs (docs/adr/0007) and gains no field; on a miss the answer
|
|
12
|
+
* still comes from it, through the container. Nothing here is written to
|
|
13
|
+
* Durable Object storage, deliberately: state that survives in the object would
|
|
14
|
+
* be a database beside the log, and would cost the two properties the log buys
|
|
15
|
+
* — a repository restorable from the bucket alone, and an operator CLI that
|
|
16
|
+
* reads the bucket from outside Cloudflare entirely. Losing this cache costs a
|
|
17
|
+
* container round-trip, which is the correct price for a cache.
|
|
18
|
+
*
|
|
19
|
+
* Pure: a Map, no I/O, no runtime. That is why it lives in `shared/` and is a
|
|
20
|
+
* module of its own rather than fields on the Durable Object, and why its
|
|
21
|
+
* behaviour is tested from `src/` with the rest of the suite.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import type { RefEvent } from './events'
|
|
25
|
+
|
|
26
|
+
/** A repository's full ref state, exactly as the Index reports it. */
|
|
27
|
+
export type Refs = Record<string, string>
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* How many repositories one fan-out remembers.
|
|
31
|
+
*
|
|
32
|
+
* A bound rather than a growing map, because the object is long-lived and the
|
|
33
|
+
* set of repositories it is asked about is not bounded by anything else. The
|
|
34
|
+
* oldest-filled entry goes first; evicting one costs a container round-trip on
|
|
35
|
+
* the next connect for it, and nothing more.
|
|
36
|
+
*/
|
|
37
|
+
export const MAX_CACHED_REPOS = 512
|
|
38
|
+
|
|
39
|
+
export class RefCache {
|
|
40
|
+
/** Insertion-ordered, so the oldest fill is the first eviction candidate. */
|
|
41
|
+
private readonly refsByRepo = new Map<string, Refs>()
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Events that landed while a repository was being read from the Index.
|
|
45
|
+
*
|
|
46
|
+
* Reading one is a round-trip to the container, and a push can land in the
|
|
47
|
+
* middle of it — announced from a ref state NEWER than the snapshot already
|
|
48
|
+
* in flight. Without this, `apply` would drop that event (the repository has
|
|
49
|
+
* no entry yet, correctly) and the fill that follows would then install the
|
|
50
|
+
* older snapshot, leaving the cache — and the handshake reading it — behind
|
|
51
|
+
* by one push until the next one happened to arrive.
|
|
52
|
+
*
|
|
53
|
+
* So a fill is bracketed: `beginFill` says a read is in flight, `apply`
|
|
54
|
+
* buffers anything for that repository, and `endFill` replays the buffer over
|
|
55
|
+
* the snapshot. The buffer is per-repository and lives only for the length of
|
|
56
|
+
* one read.
|
|
57
|
+
*/
|
|
58
|
+
private readonly inFlight = new Map<string, { readers: number; buffered: RefEvent[] }>()
|
|
59
|
+
|
|
60
|
+
constructor(private readonly maxRepos: number = MAX_CACHED_REPOS) {}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* A read of this repository's ref state has started.
|
|
64
|
+
*
|
|
65
|
+
* Counted rather than flagged: two subscribers can connect for the same
|
|
66
|
+
* unknown repository at once, and the second one's `endFill` must not throw
|
|
67
|
+
* away a buffer the first one is still relying on.
|
|
68
|
+
*/
|
|
69
|
+
beginFill(repo: string): void {
|
|
70
|
+
const existing = this.inFlight.get(repo)
|
|
71
|
+
if (existing) {
|
|
72
|
+
existing.readers += 1
|
|
73
|
+
return
|
|
74
|
+
}
|
|
75
|
+
this.inFlight.set(repo, { readers: 1, buffered: [] })
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* A read finished: install the snapshot, then replay anything announced while
|
|
80
|
+
* it was in flight, so the result is never older than an event already sent.
|
|
81
|
+
*/
|
|
82
|
+
endFill(repo: string, refs: Refs): void {
|
|
83
|
+
const flight = this.inFlight.get(repo)
|
|
84
|
+
this.fill(repo, refs)
|
|
85
|
+
if (!flight) return
|
|
86
|
+
// Detached and the slot emptied BEFORE the replay: `apply` buffers whatever
|
|
87
|
+
// is still in flight, so replaying the very array it appends to would never
|
|
88
|
+
// terminate.
|
|
89
|
+
const buffered = flight.buffered
|
|
90
|
+
flight.buffered = []
|
|
91
|
+
this.release(repo, flight)
|
|
92
|
+
if (buffered.length > 0) this.apply(buffered)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* A read failed: leave the repository unknown.
|
|
97
|
+
*
|
|
98
|
+
* Emphatically NOT a fill with an empty ref state — that would be a cache hit
|
|
99
|
+
* claiming the repository has no refs, and a later handshake would answer it
|
|
100
|
+
* confidently. A miss costs another round-trip; a wrong hit costs a
|
|
101
|
+
* subscriber that never learns it is behind.
|
|
102
|
+
*/
|
|
103
|
+
abortFill(repo: string): void {
|
|
104
|
+
const flight = this.inFlight.get(repo)
|
|
105
|
+
if (!flight) return
|
|
106
|
+
this.release(repo, flight)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
private release(repo: string, flight: { readers: number; buffered: RefEvent[] }): void {
|
|
110
|
+
flight.readers -= 1
|
|
111
|
+
if (flight.readers <= 0) this.inFlight.delete(repo)
|
|
112
|
+
else flight.buffered = []
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Is this repository's ref state known here? */
|
|
116
|
+
has(repo: string): boolean {
|
|
117
|
+
return this.refsByRepo.has(repo)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** The repositories out of these that still have to be read from the Index. */
|
|
121
|
+
missing(repos: readonly string[]): string[] {
|
|
122
|
+
return repos.filter((repo) => !this.refsByRepo.has(repo))
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Record a repository's full ref state, as read from the Index.
|
|
127
|
+
*
|
|
128
|
+
* Whole-repository only: a partial fill would be indistinguishable from a
|
|
129
|
+
* complete one at read time, and a whole-repository handshake would then
|
|
130
|
+
* answer with a subset and look authoritative doing it.
|
|
131
|
+
*/
|
|
132
|
+
fill(repo: string, refs: Refs): void {
|
|
133
|
+
this.refsByRepo.delete(repo)
|
|
134
|
+
this.refsByRepo.set(repo, { ...refs })
|
|
135
|
+
while (this.refsByRepo.size > this.maxRepos) {
|
|
136
|
+
const oldest = this.refsByRepo.keys().next()
|
|
137
|
+
if (oldest.done) break
|
|
138
|
+
this.refsByRepo.delete(oldest.value)
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Everything known for these repositories, in the shape `handshake` takes. */
|
|
143
|
+
read(repos: readonly string[]): Record<string, Refs> {
|
|
144
|
+
const byRepo: Record<string, Refs> = {}
|
|
145
|
+
for (const repo of repos) {
|
|
146
|
+
const refs = this.refsByRepo.get(repo)
|
|
147
|
+
if (refs) byRepo[repo] = refs
|
|
148
|
+
}
|
|
149
|
+
return byRepo
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Fold announced events into what is known.
|
|
154
|
+
*
|
|
155
|
+
* An event for a repository this cache has never filled is DROPPED rather
|
|
156
|
+
* than used to start an entry: one ref is not a repository's ref state, and
|
|
157
|
+
* an entry built from events alone would answer a later whole-repository
|
|
158
|
+
* watch with whatever happened to have moved since. A miss is cheap; a
|
|
159
|
+
* confidently wrong hit is not.
|
|
160
|
+
*
|
|
161
|
+
* A deletion (`sha: null`) removes the ref, so the cache says what the Index
|
|
162
|
+
* says rather than remembering a ref that is gone.
|
|
163
|
+
*/
|
|
164
|
+
apply(events: readonly RefEvent[]): void {
|
|
165
|
+
for (const event of events) {
|
|
166
|
+
// Buffered rather than dropped when a read of this repository is in
|
|
167
|
+
// flight: the fill about to land is a snapshot taken BEFORE this event.
|
|
168
|
+
const flight = this.inFlight.get(event.repo)
|
|
169
|
+
if (flight) flight.buffered.push(event)
|
|
170
|
+
const refs = this.refsByRepo.get(event.repo)
|
|
171
|
+
if (!refs) continue
|
|
172
|
+
if (event.sha === null) delete refs[event.ref]
|
|
173
|
+
else refs[event.ref] = event.sha
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** How many repositories are remembered. For tests and for the bound. */
|
|
178
|
+
get size(): number {
|
|
179
|
+
return this.refsByRepo.size
|
|
180
|
+
}
|
|
181
|
+
}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the write-ahead log cannot see.
|
|
3
|
+
*
|
|
4
|
+
* `src/usage.ts` derives storage, repository count and push volume from the log
|
|
5
|
+
* itself, with no instrumentation to keep in sync. That view has one hole, and
|
|
6
|
+
* it is structural rather than an oversight: a clone writes nothing to the log.
|
|
7
|
+
* Read volume, bytes served, how long a request took, whether it paid for a
|
|
8
|
+
* cold container and every refusal that never became a WAL entry are invisible
|
|
9
|
+
* there and can only be counted where the request is — the Worker in front of
|
|
10
|
+
* the container, which sees even the requests the container never receives.
|
|
11
|
+
*
|
|
12
|
+
* So this file counts exactly that hole and nothing else. Storage, repository
|
|
13
|
+
* count and push bytes are deliberately NOT recorded here: two records of one
|
|
14
|
+
* fact disagree the first time a write fails between them, and the log's copy
|
|
15
|
+
* is the one that cannot drift.
|
|
16
|
+
*
|
|
17
|
+
* Refusals are counted BY KIND, never as an aggregate error rate — the kinds
|
|
18
|
+
* and what each one means are `shared/protocol.ts`'s, because the container is
|
|
19
|
+
* the layer that names most of them and this is the layer that counts them.
|
|
20
|
+
*
|
|
21
|
+
* Pure on purpose — no Workers types, no bindings, no clock. `worker/index.ts`
|
|
22
|
+
* owns the one side effect (a single `writeDataPoint`), and `src/telemetry.test.ts`
|
|
23
|
+
* exercises everything here without a runtime.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import {
|
|
27
|
+
HEALTH_PATH,
|
|
28
|
+
PROVENANCE_PATH,
|
|
29
|
+
REJECT_HEADER,
|
|
30
|
+
REPO_ID,
|
|
31
|
+
SERVED_HEADER,
|
|
32
|
+
SMART_HTTP,
|
|
33
|
+
normalizeReject,
|
|
34
|
+
type RejectKind,
|
|
35
|
+
} from './protocol'
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* What the request was asking for.
|
|
39
|
+
*
|
|
40
|
+
* Split by protocol endpoint rather than by HTTP verb, because "how many
|
|
41
|
+
* clones?" is the question an operator actually has, and a clone is two
|
|
42
|
+
* requests: the ref advertisement, then the pack. Counting `clone` alone gives
|
|
43
|
+
* the number of repositories actually read; counting `clone-advertise` alone
|
|
44
|
+
* gives the number of clients that looked.
|
|
45
|
+
*/
|
|
46
|
+
export type RequestKind =
|
|
47
|
+
| 'clone-advertise'
|
|
48
|
+
| 'clone'
|
|
49
|
+
| 'push-advertise'
|
|
50
|
+
| 'push'
|
|
51
|
+
| 'instructions'
|
|
52
|
+
// The browser's `/`, answered at the edge and never proxied — so it is its
|
|
53
|
+
// own kind rather than folded into `instructions`. Counting them together
|
|
54
|
+
// would hide the one number a launch actually turns on: how many people read
|
|
55
|
+
// the page versus how many clients read the protocol.
|
|
56
|
+
| 'landing'
|
|
57
|
+
| 'health'
|
|
58
|
+
// Someone asking who pushed (docs/adr/0011). Its own kind rather than
|
|
59
|
+
// `other`, because `other` is the unroutable bucket and a provenance read is
|
|
60
|
+
// a request walgit answers — folding the two together would hide both the
|
|
61
|
+
// demand for the feature and any refusal it produces, and would lose the
|
|
62
|
+
// repository name with them.
|
|
63
|
+
| 'provenance'
|
|
64
|
+
| 'other'
|
|
65
|
+
|
|
66
|
+
export type Outcome = 'ok' | 'reject'
|
|
67
|
+
|
|
68
|
+
export interface RequestFacts {
|
|
69
|
+
kind: RequestKind
|
|
70
|
+
/** The repository the path names, or `''` for a request that names none. */
|
|
71
|
+
repo: string
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Which endpoint, and which repository.
|
|
76
|
+
*
|
|
77
|
+
* The repository NAME is recorded. Every repository on a public walgit is
|
|
78
|
+
* world-readable by construction, so a name is not a secret to leak — and
|
|
79
|
+
* without it an operator watching a traffic spike cannot tell one busy
|
|
80
|
+
* repository from a hundred. Nothing else about the caller is recorded: no IP,
|
|
81
|
+
* no user agent, no credential, no bytes of anyone's repository content.
|
|
82
|
+
*/
|
|
83
|
+
export function classifyRequest(method: string, pathname: string, search: string): RequestFacts {
|
|
84
|
+
if (pathname === HEALTH_PATH) return { kind: 'health', repo: '' }
|
|
85
|
+
if (pathname === '/' && (method === 'GET' || method === 'HEAD')) {
|
|
86
|
+
return { kind: 'instructions', repo: '' }
|
|
87
|
+
}
|
|
88
|
+
// The repository is in the query string here, not the path — the one endpoint
|
|
89
|
+
// that names one without going through `SMART_HTTP`. Read anyway, because a
|
|
90
|
+
// provenance read with no repository attached is a row an operator cannot act
|
|
91
|
+
// on, and the name is already recorded for every clone of the same repo. Only
|
|
92
|
+
// a name walgit would actually serve is kept: the query string is
|
|
93
|
+
// attacker-controlled and unbounded, and a datapoint is not the place to find
|
|
94
|
+
// that out.
|
|
95
|
+
if (pathname === PROVENANCE_PATH) {
|
|
96
|
+
const requested = new URLSearchParams(search).get('repo') ?? ''
|
|
97
|
+
return { kind: 'provenance', repo: REPO_ID.test(requested) ? requested : '' }
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const route = SMART_HTTP.exec(pathname)
|
|
101
|
+
if (!route) return { kind: 'other', repo: '' }
|
|
102
|
+
const repo = route[1]!
|
|
103
|
+
|
|
104
|
+
if (route[2] === 'git-upload-pack') return { kind: 'clone', repo }
|
|
105
|
+
if (route[2] === 'git-receive-pack') return { kind: 'push', repo }
|
|
106
|
+
|
|
107
|
+
// The advertisement is the same URL for both directions; only `service` says
|
|
108
|
+
// which, and a request without it is dumb-HTTP, which walgit does not serve.
|
|
109
|
+
const service = new URLSearchParams(search).get('service')
|
|
110
|
+
if (service === 'git-receive-pack') return { kind: 'push-advertise', repo }
|
|
111
|
+
if (service === 'git-upload-pack') return { kind: 'clone-advertise', repo }
|
|
112
|
+
return { kind: 'other', repo }
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Was this a refusal, and of what kind?
|
|
117
|
+
*
|
|
118
|
+
* The kind comes from the container when the container refused — it is the only
|
|
119
|
+
* layer that knows the difference between a size cap and a name collision, and
|
|
120
|
+
* naming it in a header beats re-deriving it from a status code that several
|
|
121
|
+
* refusals share. Status is the fallback for a refusal that arrived with no
|
|
122
|
+
* header, and the absence of the "served" stamp is what makes `edge` detectable
|
|
123
|
+
* at all: a refusal walgit never produced is one something in front of it made
|
|
124
|
+
* on walgit's behalf.
|
|
125
|
+
*/
|
|
126
|
+
export function classifyOutcome(
|
|
127
|
+
status: number,
|
|
128
|
+
headers: { get(name: string): string | null },
|
|
129
|
+
): { outcome: Outcome; reject: RejectKind | '' } {
|
|
130
|
+
const declared = headers.get(REJECT_HEADER)
|
|
131
|
+
if (declared) return { outcome: 'reject', reject: normalizeReject(declared) }
|
|
132
|
+
if (status < 400) return { outcome: 'ok', reject: '' }
|
|
133
|
+
if (headers.get(SERVED_HEADER) === null) return { outcome: 'reject', reject: 'edge' }
|
|
134
|
+
return { outcome: 'reject', reject: fromStatus(status) }
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function fromStatus(status: number): RejectKind {
|
|
138
|
+
if (status === 401 || status === 403) return 'unauthorized'
|
|
139
|
+
if (status === 404) return 'not-found'
|
|
140
|
+
if (status === 409) return 'collision'
|
|
141
|
+
if (status === 413) return 'size-cap'
|
|
142
|
+
if (status === 503) return 'unavailable'
|
|
143
|
+
return 'other'
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export interface RequestMetric {
|
|
147
|
+
kind: RequestKind
|
|
148
|
+
repo: string
|
|
149
|
+
outcome: Outcome
|
|
150
|
+
reject: RejectKind | ''
|
|
151
|
+
status: number
|
|
152
|
+
/** The container answered this one (as opposed to something in front of it). */
|
|
153
|
+
served: boolean
|
|
154
|
+
/** The container had just started when it answered — the cold path. */
|
|
155
|
+
cold: boolean
|
|
156
|
+
/** Time to the response headers: what a client waits before anything moves. */
|
|
157
|
+
ttfbMs: number
|
|
158
|
+
/** Time until the last byte was written: what a clone actually costs. */
|
|
159
|
+
totalMs: number
|
|
160
|
+
/** Response body bytes. The read-volume number the log cannot produce. */
|
|
161
|
+
bytesServed: number
|
|
162
|
+
/** Declared request body bytes, or 0 when the client did not declare one. */
|
|
163
|
+
bytesReceived: number
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** The Analytics Engine datapoint shape, kept here so a test can pin it. */
|
|
167
|
+
export interface DataPoint {
|
|
168
|
+
indexes: string[]
|
|
169
|
+
blobs: string[]
|
|
170
|
+
doubles: number[]
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* One datapoint per request.
|
|
175
|
+
*
|
|
176
|
+
* `indexes` takes the request kind: Analytics Engine samples per index, and
|
|
177
|
+
* sampling clones (the loud thing) independently of refusals (the rare thing
|
|
178
|
+
* an operator is actually hunting) is what keeps a handful of refusals visible
|
|
179
|
+
* under load.
|
|
180
|
+
*/
|
|
181
|
+
export function toDataPoint(metric: RequestMetric): DataPoint {
|
|
182
|
+
return {
|
|
183
|
+
indexes: [metric.kind],
|
|
184
|
+
blobs: [
|
|
185
|
+
metric.kind,
|
|
186
|
+
metric.outcome,
|
|
187
|
+
metric.reject,
|
|
188
|
+
metric.repo,
|
|
189
|
+
metric.cold ? 'cold' : 'warm',
|
|
190
|
+
metric.served ? 'container' : 'edge',
|
|
191
|
+
],
|
|
192
|
+
doubles: [
|
|
193
|
+
metric.status,
|
|
194
|
+
metric.ttfbMs,
|
|
195
|
+
metric.totalMs,
|
|
196
|
+
metric.bytesServed,
|
|
197
|
+
metric.bytesReceived,
|
|
198
|
+
metric.cold ? 1 : 0,
|
|
199
|
+
],
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Column names for the datapoint above — the legend for a SQL API query. */
|
|
204
|
+
export const BLOB_COLUMNS = [
|
|
205
|
+
'kind',
|
|
206
|
+
'outcome',
|
|
207
|
+
'reject',
|
|
208
|
+
'repo',
|
|
209
|
+
'temperature',
|
|
210
|
+
'answered',
|
|
211
|
+
] as const
|
|
212
|
+
export const DOUBLE_COLUMNS = [
|
|
213
|
+
'status',
|
|
214
|
+
'ttfb_ms',
|
|
215
|
+
'total_ms',
|
|
216
|
+
'bytes_served',
|
|
217
|
+
'bytes_received',
|
|
218
|
+
'cold',
|
|
219
|
+
] as const
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
/**
|
|
3
|
+
* The detached ref-event announcer.
|
|
4
|
+
*
|
|
5
|
+
* A separate process, spawned and disowned by `post-receive`, for the same
|
|
6
|
+
* reason `compact-main.ts` is: `post-receive` holds the client's connection
|
|
7
|
+
* open until it exits, so anything awaited in there lands as push latency. An
|
|
8
|
+
* announcement is a network call to the fan-out, and a fan-out that is slow or
|
|
9
|
+
* unreachable would otherwise cost the pusher up to the announce timeout — on
|
|
10
|
+
* the one path this project promises to keep fast, for a feature whose entire
|
|
11
|
+
* purpose is to stop clients spending time on staleness.
|
|
12
|
+
*
|
|
13
|
+
* Failures are logged and swallowed, and there is nothing to retry: the push is
|
|
14
|
+
* already durable and `index.json` already carries the refs this would have
|
|
15
|
+
* announced, so a subscriber's next handshake reads the truth from there
|
|
16
|
+
* regardless. A dropped announcement costs one subscriber one push of
|
|
17
|
+
* freshness, never correctness.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { announce, announceConfigFromEnv } from './announce'
|
|
21
|
+
import type { RefChange } from './wal-index'
|
|
22
|
+
|
|
23
|
+
const repoId = process.argv[2]
|
|
24
|
+
const payload = process.argv[3]
|
|
25
|
+
|
|
26
|
+
async function main(): Promise<void> {
|
|
27
|
+
if (!repoId || !payload) throw new Error('usage: announce-main <repo-id> <changes-json>')
|
|
28
|
+
const config = announceConfigFromEnv()
|
|
29
|
+
// Not an error: the environment can change between the spawn and the start,
|
|
30
|
+
// and a deployment with no stream configured simply has nothing to tell.
|
|
31
|
+
if (!config) return
|
|
32
|
+
await announce(config, repoId, JSON.parse(payload) as RefChange[])
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
main().catch((err) => {
|
|
36
|
+
process.stderr.write(`walgit: ref-event announce failed: ${(err as Error).message}\n`)
|
|
37
|
+
})
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Telling subscribers that a push landed.
|
|
3
|
+
*
|
|
4
|
+
* The rule this file exists to keep: an announcement is published only after
|
|
5
|
+
* the push is durable. `reference-transaction prepared` is where the
|
|
6
|
+
* compare-and-swap on `index.json` happens, and a push that loses it is
|
|
7
|
+
* rejected — git never runs `post-receive` for it — so announcing from
|
|
8
|
+
* `post-receive` means a client can never be told about a push that then lost.
|
|
9
|
+
* That is the ordering docs/adr/0007 makes load-bearing, reused rather than
|
|
10
|
+
* re-derived.
|
|
11
|
+
*
|
|
12
|
+
* The other rule: this must never be able to fail a push. `post-receive` runs
|
|
13
|
+
* after the ref transaction has committed, so nothing it does can unmake the
|
|
14
|
+
* push — and every failure here is caught, logged and swallowed. A subscriber
|
|
15
|
+
* that misses an announcement is a subscriber that learns the state on its next
|
|
16
|
+
* handshake; a push that failed because a notification could not be delivered
|
|
17
|
+
* would be a git host that stops working when a side channel does.
|
|
18
|
+
*
|
|
19
|
+
* The wire types and the change-to-event mapping come from `shared/events.ts`,
|
|
20
|
+
* which is where the protocol lives — one definition, so the two ends cannot
|
|
21
|
+
* disagree about what a deletion looks like.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { eventsFromChanges } from '../shared/events'
|
|
25
|
+
import { ANNOUNCE_PATH } from '../shared/protocol'
|
|
26
|
+
import type { RefChange } from './wal-index'
|
|
27
|
+
|
|
28
|
+
export interface AnnounceConfig {
|
|
29
|
+
/** The deployment's own public origin — the Worker in front of this container. */
|
|
30
|
+
url: string
|
|
31
|
+
/** The shared secret proving this is walgit's push path (`WALGIT_EVENTS_TOKEN`). */
|
|
32
|
+
token: string
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* How long the push path will wait for the announcement to be accepted.
|
|
37
|
+
*
|
|
38
|
+
* Bounded because `post-receive` holds the client's connection open until it
|
|
39
|
+
* exits: an unreachable endpoint must cost the pusher a couple of seconds at
|
|
40
|
+
* the very worst, not a hung push. Short enough to be invisible next to the
|
|
41
|
+
* pack upload that preceded it.
|
|
42
|
+
*/
|
|
43
|
+
const ANNOUNCE_TIMEOUT_MS = 2_000
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The stream's configuration, or null when this deployment has none.
|
|
47
|
+
*
|
|
48
|
+
* Both halves are required: a URL without a secret would be rejected at the
|
|
49
|
+
* door, and a secret without a URL has nowhere to go. Off unless configured,
|
|
50
|
+
* like every other optional policy here.
|
|
51
|
+
*/
|
|
52
|
+
export function announceConfigFromEnv(
|
|
53
|
+
env: Record<string, string | undefined> = process.env,
|
|
54
|
+
): AnnounceConfig | null {
|
|
55
|
+
const url = (env.WALGIT_EVENTS_URL ?? '').trim()
|
|
56
|
+
const token = (env.WALGIT_EVENTS_TOKEN ?? '').trim()
|
|
57
|
+
if (!url || !token) return null
|
|
58
|
+
return { url: url.replace(/\/+$/, ''), token }
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Publish one push's ref changes. Never throws.
|
|
63
|
+
*
|
|
64
|
+
* `fetchImpl` is injected for the same reason `runBackend` is in src/http.ts:
|
|
65
|
+
* the decision to announce, and the shape of what is announced, are testable
|
|
66
|
+
* without a network.
|
|
67
|
+
*/
|
|
68
|
+
export async function announce(
|
|
69
|
+
config: AnnounceConfig,
|
|
70
|
+
repoId: string,
|
|
71
|
+
changes: readonly RefChange[],
|
|
72
|
+
fetchImpl: typeof fetch = fetch,
|
|
73
|
+
): Promise<boolean> {
|
|
74
|
+
const events = eventsFromChanges(repoId, changes)
|
|
75
|
+
if (events.length === 0) return false
|
|
76
|
+
try {
|
|
77
|
+
const response = await fetchImpl(`${config.url}${ANNOUNCE_PATH}`, {
|
|
78
|
+
method: 'POST',
|
|
79
|
+
headers: {
|
|
80
|
+
'content-type': 'application/json',
|
|
81
|
+
authorization: `Bearer ${config.token}`,
|
|
82
|
+
},
|
|
83
|
+
body: JSON.stringify({ events }),
|
|
84
|
+
signal: AbortSignal.timeout(ANNOUNCE_TIMEOUT_MS),
|
|
85
|
+
})
|
|
86
|
+
if (!response.ok) {
|
|
87
|
+
// Logged rather than retried: the push is already acknowledged, and the
|
|
88
|
+
// state the announcement carries is in `index.json` regardless — a
|
|
89
|
+
// subscriber's next handshake reads it there.
|
|
90
|
+
process.stderr.write(`walgit: ref-event announce refused (${response.status})\n`)
|
|
91
|
+
return false
|
|
92
|
+
}
|
|
93
|
+
return true
|
|
94
|
+
} catch (err) {
|
|
95
|
+
process.stderr.write(`walgit: ref-event announce failed: ${(err as Error).message}\n`)
|
|
96
|
+
return false
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Append-only refs: a stranger may add to a repository, never destroy it.
|
|
3
|
+
*
|
|
4
|
+
* This is the property a public, credential-free git host is safe on. It is
|
|
5
|
+
* enforced twice, and the two layers are not redundant:
|
|
6
|
+
*
|
|
7
|
+
* - git's own `receive.denyNonFastForwards` / `receive.denyDeletes` are the
|
|
8
|
+
* backstop. They cannot be bypassed by a bug in this file, and they hold
|
|
9
|
+
* even for a ref update that never passes through `pre-receive`.
|
|
10
|
+
* - the check below runs in `pre-receive`, which is EARLIER than git's own
|
|
11
|
+
* refusal, and that earliness buys two things git cannot give. The wording
|
|
12
|
+
* — `denying non-fast-forward refs/heads/main` tells an agent nothing about
|
|
13
|
+
* what walgit is or what to do instead — and the object-store write: git
|
|
14
|
+
* refuses after `pre-receive`, by which point walgit has already uploaded
|
|
15
|
+
* the pack for a push that was never going to land, leaving an orphan for
|
|
16
|
+
* `findOrphans` to reclaim.
|
|
17
|
+
*
|
|
18
|
+
* Off unless the instance turns it on. A private instance may want force-push;
|
|
19
|
+
* this is instance configuration, not a template default.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import * as crypto from 'node:crypto'
|
|
23
|
+
|
|
24
|
+
import { git } from './git'
|
|
25
|
+
import { flagEnabled } from '../shared/policy'
|
|
26
|
+
import { ZERO_OID } from '../shared/protocol'
|
|
27
|
+
import type { RefChange } from './wal-index'
|
|
28
|
+
|
|
29
|
+
/** The env flag an instance sets to make its repositories append-only. */
|
|
30
|
+
export function appendOnlyEnabled(env: Record<string, string | undefined> = process.env): boolean {
|
|
31
|
+
return flagEnabled(env.WALGIT_APPEND_ONLY)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A free name to push to instead. Random rather than incrementing, because a
|
|
36
|
+
* wave of agents running near-identical prompts all reach for `test` at once
|
|
37
|
+
* and a counter would hand several of them the same "free" name.
|
|
38
|
+
*/
|
|
39
|
+
export function suggestName(repoId: string): string {
|
|
40
|
+
return `${repoId}-${crypto.randomBytes(4).toString('hex')}`
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type RefVerdict =
|
|
44
|
+
| { allowed: true }
|
|
45
|
+
| { allowed: false; kind: 'delete' | 'rewrite'; ref: string }
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Is this one ref change append-only?
|
|
49
|
+
*
|
|
50
|
+
* Creating a ref is always allowed; deleting one never is. Updating one is
|
|
51
|
+
* allowed exactly when the old tip is an ancestor of the new tip — which is
|
|
52
|
+
* git's own fast-forward test, asked of the objects sitting in the quarantine
|
|
53
|
+
* where `pre-receive` can already see them. Unrelated history fails it, which
|
|
54
|
+
* is the intended answer: it would drop every commit the ref names today.
|
|
55
|
+
*/
|
|
56
|
+
export function judgeRefChange(gitDir: string, change: RefChange): RefVerdict {
|
|
57
|
+
if (change.oldOid === ZERO_OID) return { allowed: true }
|
|
58
|
+
if (change.newOid === ZERO_OID) return { allowed: false, kind: 'delete', ref: change.ref }
|
|
59
|
+
const res = git([
|
|
60
|
+
'--git-dir',
|
|
61
|
+
gitDir,
|
|
62
|
+
'merge-base',
|
|
63
|
+
'--is-ancestor',
|
|
64
|
+
change.oldOid,
|
|
65
|
+
change.newOid,
|
|
66
|
+
])
|
|
67
|
+
if (res.status === 0) return { allowed: true }
|
|
68
|
+
return { allowed: false, kind: 'rewrite', ref: change.ref }
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The message a rejected push reads. This is product copy, not a debug string:
|
|
73
|
+
* for most agents it is the first thing walgit ever says to them, so it states
|
|
74
|
+
* the rule, why the push was refused, and one concrete thing to do next.
|
|
75
|
+
*/
|
|
76
|
+
export function rejectionMessage(repoId: string, verdict: RefVerdict & { allowed: false }): string {
|
|
77
|
+
const what =
|
|
78
|
+
verdict.kind === 'delete'
|
|
79
|
+
? `Deleting ${verdict.ref} would destroy history other people can still reach.`
|
|
80
|
+
: `This push would rewrite ${verdict.ref}: its current commits are not in what you pushed.`
|
|
81
|
+
return [
|
|
82
|
+
`walgit: refused — ${repoId} is append-only.`,
|
|
83
|
+
'',
|
|
84
|
+
what,
|
|
85
|
+
'Anyone can push here, so nothing that has landed can be removed or replaced.',
|
|
86
|
+
'',
|
|
87
|
+
'What you can do instead:',
|
|
88
|
+
` - push to a new branch: git push origin HEAD:refs/heads/<new-branch>`,
|
|
89
|
+
` - or use a fresh repository: git remote set-url origin <same-host>/${suggestName(repoId)}.git`,
|
|
90
|
+
'',
|
|
91
|
+
'Nothing was uploaded; the repository is unchanged.',
|
|
92
|
+
].join('\n')
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export type AppendOnlyResult = { ok: true } | { ok: false; message: string }
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Judge a whole push. The first offending ref decides it: a push is all or
|
|
99
|
+
* nothing to git, so reporting them one at a time is enough and keeps the
|
|
100
|
+
* message short enough to be read.
|
|
101
|
+
*/
|
|
102
|
+
export function checkAppendOnly(
|
|
103
|
+
gitDir: string,
|
|
104
|
+
repoId: string,
|
|
105
|
+
changes: readonly RefChange[],
|
|
106
|
+
): AppendOnlyResult {
|
|
107
|
+
for (const change of changes) {
|
|
108
|
+
const verdict = judgeRefChange(gitDir, change)
|
|
109
|
+
if (!verdict.allowed) return { ok: false, message: rejectionMessage(repoId, verdict) }
|
|
110
|
+
}
|
|
111
|
+
return { ok: true }
|
|
112
|
+
}
|