@zabaca/zbc 0.21.0 → 0.21.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 (51) hide show
  1. package/package.json +1 -1
  2. package/templates/apps/walgit/CONTEXT.md +25 -0
  3. package/templates/apps/walgit/README.md +152 -4
  4. package/templates/apps/walgit/e2e/harness.ts +2 -2
  5. package/templates/apps/walgit/e2e/scenarios.ts +302 -2
  6. package/templates/apps/walgit/shared/browse.ts +733 -0
  7. package/templates/apps/walgit/shared/capabilities.ts +17 -0
  8. package/templates/apps/walgit/shared/container-env.ts +9 -0
  9. package/templates/apps/walgit/shared/landing.ts +8 -27
  10. package/templates/apps/walgit/shared/llms.ts +25 -0
  11. package/templates/apps/walgit/shared/policy.ts +21 -0
  12. package/templates/apps/walgit/shared/pooled.ts +31 -0
  13. package/templates/apps/walgit/shared/protocol.ts +120 -0
  14. package/templates/apps/walgit/shared/repo-list.ts +478 -0
  15. package/templates/apps/walgit/shared/store.ts +307 -0
  16. package/templates/apps/walgit/shared/telemetry.ts +68 -6
  17. package/templates/apps/walgit/shared/wal-index.ts +249 -0
  18. package/templates/apps/walgit/shared/web.ts +46 -0
  19. package/templates/apps/walgit/src/announce-main.ts +1 -1
  20. package/templates/apps/walgit/src/announce.ts +1 -1
  21. package/templates/apps/walgit/src/append-only.ts +5 -8
  22. package/templates/apps/walgit/src/browse.ts +230 -0
  23. package/templates/apps/walgit/src/cache.ts +4 -4
  24. package/templates/apps/walgit/src/compact.ts +6 -12
  25. package/templates/apps/walgit/src/delete-repo.ts +4 -3
  26. package/templates/apps/walgit/src/expire.ts +3 -3
  27. package/templates/apps/walgit/src/gc.ts +4 -3
  28. package/templates/apps/walgit/src/git.ts +127 -10
  29. package/templates/apps/walgit/src/hook-main.ts +1 -1
  30. package/templates/apps/walgit/src/http.ts +386 -3
  31. package/templates/apps/walgit/src/limits.ts +1 -1
  32. package/templates/apps/walgit/src/materialize.bench.ts +4 -10
  33. package/templates/apps/walgit/src/materialize.ts +11 -4
  34. package/templates/apps/walgit/src/orphans.ts +3 -3
  35. package/templates/apps/walgit/src/pending.ts +1 -1
  36. package/templates/apps/walgit/src/private.ts +1 -1
  37. package/templates/apps/walgit/src/proposals.ts +3 -3
  38. package/templates/apps/walgit/src/push-cert.ts +10 -1
  39. package/templates/apps/walgit/src/push.ts +11 -9
  40. package/templates/apps/walgit/src/reconcile.ts +4 -3
  41. package/templates/apps/walgit/src/server.ts +29 -2
  42. package/templates/apps/walgit/src/signers.ts +9 -3
  43. package/templates/apps/walgit/src/store-env.ts +8 -21
  44. package/templates/apps/walgit/src/store.ts +12 -241
  45. package/templates/apps/walgit/src/sync.ts +2 -2
  46. package/templates/apps/walgit/src/usage.ts +4 -18
  47. package/templates/apps/walgit/src/verify.ts +2 -2
  48. package/templates/apps/walgit/src/wal-index.ts +20 -239
  49. package/templates/apps/walgit/worker/index.ts +232 -1
  50. /package/templates/apps/walgit/{src → shared}/keys.ts +0 -0
  51. /package/templates/apps/walgit/{src → shared}/ulid.ts +0 -0
@@ -63,6 +63,7 @@ type CapabilityVar = Extract<
63
63
  | 'WALGIT_SIGNER_LISTS'
64
64
  | 'WALGIT_PRIVATE_REPOS'
65
65
  | 'WALGIT_PROPOSALS'
66
+ | 'WALGIT_WEB'
66
67
  | 'WALGIT_RATE_WINDOW_SECONDS'
67
68
  | 'WALGIT_MAX_NEW_REPOS_PER_SOURCE'
68
69
  | 'WALGIT_MAX_PUSHES_PER_SOURCE'
@@ -180,6 +181,21 @@ export type Capabilities = {
180
181
  * gave a token to.
181
182
  */
182
183
  proposals: boolean
184
+ /**
185
+ * This deployment serves a browse of the repositories it holds — the list at
186
+ * `/repos` (`shared/repo-list.ts`), and the repository pages under it
187
+ * (`shared/browse.ts`).
188
+ *
189
+ * A plain flag, read on BOTH sides: at the edge to claim the routes, and in
190
+ * the container to decide whether `/_walgit/browse` exists — the list is
191
+ * answered off the log and never wakes the container, but a tree is git
192
+ * objects and only the Cache holds those. Default off, because a
193
+ * deployment's repository names are the one
194
+ * thing a credentialed instance has not already been asked to publish — the
195
+ * credential still gates the route, but enumerating names is a surface an
196
+ * operator opts into rather than acquires on upgrade.
197
+ */
198
+ web: boolean
183
199
  /** A repository is collected this many hours after its last push. */
184
200
  retentionHours: number | null
185
201
  /** Largest single push, in bytes. */
@@ -275,6 +291,7 @@ export function capabilitiesFrom(
275
291
  publicAccess &&
276
292
  seedValue(env.WALGIT_PRIVATE_REPOS) !== null,
277
293
  proposals: namesCanRefuse && signedPushes && flagEnabled(env.WALGIT_PROPOSALS),
294
+ web: flagEnabled(env.WALGIT_WEB),
278
295
  retentionHours: positiveNumber(env.WALGIT_RETENTION_HOURS),
279
296
  maxPushBytes: positiveNumber(env.WALGIT_MAX_PUSH_BYTES),
280
297
  maxRepoBytes: positiveNumber(env.WALGIT_MAX_REPO_BYTES),
@@ -82,6 +82,15 @@ export const CONTAINER_ENV = [
82
82
  // nonce and mints nothing — it only widens what `pre-receive` accepts on a
83
83
  // claimed name, and the hook that widens runs in the container.
84
84
  'WALGIT_PROPOSALS',
85
+ // The web view (`shared/repo-list.ts`). The only variable on this list that
86
+ // nothing inside the container reads: the list is answered at the edge, off
87
+ // the log, precisely so a browse does not wake the container. It is here
88
+ // because it is an Advertised capability, and `CapabilityVar` narrows through
89
+ // this list — a capability the container is never told about would be one the
90
+ // edge advertises and no half could ever enforce. The cost is the one this
91
+ // list buys everywhere else: the deploy that first sets it changes the
92
+ // fingerprint and replaces the running container once.
93
+ 'WALGIT_WEB',
85
94
  // Per-source rate limits (src/rate-limit.ts). The verdict is reached in the
86
95
  // container's own HTTP handler — the only place that sees both the source the
87
96
  // edge attributed the request to and the repository it names — so the numbers
@@ -53,7 +53,7 @@ import type { Capabilities } from './capabilities'
53
53
  import { analyticsScript, type Analytics } from './analytics'
54
54
  import { contactHref, type Operator } from './operator'
55
55
  import { OG_IMAGE_HEIGHT, OG_IMAGE_PATH, OG_IMAGE_WIDTH } from './og-image'
56
- import { describeBytes, describeWindow } from './policy'
56
+ import { describeWindow, shortBytes } from './policy'
57
57
  import { EVENTS_PATH, SIGNERS_REF } from './protocol'
58
58
  import { CONTENT_SIGNAL } from './robots'
59
59
 
@@ -71,33 +71,11 @@ export function wantsLanding(method: string, pathname: string, accept: string):
71
71
  return accept.toLowerCase().includes('text/html')
72
72
  }
73
73
 
74
- function describeHours(hours: number): string {
74
+ export function describeHours(hours: number): string {
75
75
  if (hours === 1) return '1 hour'
76
76
  return `${hours} hours`
77
77
  }
78
78
 
79
- /**
80
- * A cap without its exact byte count.
81
- *
82
- * `describeBytes` renders `99 MiB (103809024 bytes)` because the refusal it was
83
- * written for is read by a client comparing a number to its own, and a
84
- * parenthetical there is the difference between a machine acting on the message
85
- * and a machine guessing at a rounded figure. On a page somebody is deciding
86
- * whether to use this host at all, the same nine digits are noise in the middle
87
- * of a sentence — and `The rules.` is where every term is being cut to what
88
- * scans.
89
- *
90
- * DERIVED from `describeBytes` rather than formatted again, which is the whole
91
- * care here: the invariant this file keeps is that the cap the page prints and
92
- * the cap `pre-receive` refuses on cannot look like two different numbers, and
93
- * a second formatter is exactly how they would. Stripping a suffix off the one
94
- * rendering cannot change the figure in front of it, so the page still cannot
95
- * disagree with the hook — it only says less.
96
- */
97
- function shortBytes(bytes: number): string {
98
- return describeBytes(bytes).replace(/ \(\d+ bytes\)$/, '')
99
- }
100
-
101
79
  const claim = (key: string, value: string) =>
102
80
  ` <li><span class="k">${key}</span><span class="v">${value}</span></li>`
103
81
 
@@ -930,7 +908,7 @@ function wireScript(): string {
930
908
  * put in a deployment variable and which land in markup. So they are escaped
931
909
  * here, once, at the only place they enter the document.
932
910
  */
933
- function escapeHtml(raw: string): string {
911
+ export function escapeHtml(raw: string): string {
934
912
  return raw
935
913
  .replaceAll('&', '&amp;')
936
914
  .replaceAll('<', '&lt;')
@@ -1049,12 +1027,15 @@ function closeSection(host: string, caps: Capabilities): string {
1049
1027
  * one of them an in-page skip link, so anybody who wanted the repository, the
1050
1028
  * client or the manual had to retype a path from memory.
1051
1029
  *
1052
- * The client's entry is gated like every other mention of it. The source and
1053
- * the manual are not: walgit is open source wherever it is deployed, and
1030
+ * The client's entry is gated like every other mention of it, and so is the
1031
+ * repository list: `/repos` exists only where `WALGIT_WEB` is set, and a footer
1032
+ * link is the easiest place on a page to promise a route the Worker does not
1033
+ * claim. The source and the manual are not: walgit is open source wherever it is deployed, and
1054
1034
  * `/llms.txt` is served by every deployment.
1055
1035
  */
1056
1036
  function footerLinks(caps: Capabilities): string {
1057
1037
  const rows = [
1038
+ caps.web ? ' <a href="/repos">Repositories</a>' : '',
1058
1039
  ' <a href="https://github.com/Zabaca/zbc">Open source</a>',
1059
1040
  caps.events
1060
1041
  ? ' <a href="https://www.npmjs.com/package/@zabaca/agentgit">@zabaca/agentgit</a>'
@@ -32,6 +32,7 @@ import {
32
32
  MCP_PATH,
33
33
  PROVENANCE_PATH,
34
34
  READ_CHALLENGE_NAMESPACE,
35
+ REPOS_PATH,
35
36
  SIGNERS_REF,
36
37
  } from './protocol'
37
38
 
@@ -803,7 +804,31 @@ git clone https://${host}/$NAME.git
803
804
  \`\`\`
804
805
 
805
806
  Handing work to another agent is the URL and nothing else. There is no owner to ask, no invitation to send and no review to pass.
807
+ ${
808
+ // The web view (`shared/repo-list.ts`), named only where it is served.
809
+ // One line, because an agent wants the URL and its shape rather than a
810
+ // tour: the JSON is what a tool reads, and the page is for the person it
811
+ // hands the link to.
812
+ caps.web
813
+ ? `
814
+ ## See what this host holds
806
815
 
816
+ \`\`\`sh
817
+ curl https://${host}${REPOS_PATH}${caps.publicAccess ? '' : ' -u walgit:$TOKEN'}
818
+ \`\`\`
819
+
820
+ Every name this host holds, newest push first, a hundred per page (\`?page=2\`) — as JSON here, and as a page in a browser. It names what each one is: last push, refs, size, whether it is claimed${caps.namesCanBePrivate ? ', whether it is Private' : ''} and whether it is being removed.
821
+
822
+ \`\`\`sh
823
+ curl -H 'accept: application/json' https://${host}/$NAME${caps.publicAccess ? '' : ' -u walgit:$TOKEN'}
824
+ \`\`\`
825
+
826
+ One repository: its refs, its default branch and one level of the default branch's tree. \`/$NAME/tree/<ref>/<path>\` is any directory at any ref — a branch name containing slashes resolves, because the host splits the ref from the path against its own Index. Same gate as a clone${caps.namesCanBePrivate ? ', so a Private name is refused here exactly as `info/refs` is' : ''}.
827
+
828
+ Three more reads on the same split: \`/$NAME/blob/<ref>/<path>\` is one file (up to 1 MiB, otherwise its size alone), \`/$NAME/raw/<ref>/<path>\` is its bytes — always \`text/plain\` or an \`application/octet-stream\` attachment, chosen by content and never by extension — and \`/$NAME/commits/<ref>\` is fifty commits per page with \`?before=<full oid>\` as the cursor.
829
+ `
830
+ : ''
831
+ }
807
832
  ## Ask this host directly
808
833
 
809
834
  This host speaks the Model Context Protocol at \`https://${host}${MCP_PATH}\` (Streamable HTTP). There is nothing to install and nothing to spawn — point a client at the URL:
@@ -45,6 +45,27 @@ export function describeBytes(bytes: number): string {
45
45
 
46
46
  const round = (n: number) => String(Math.round(n * 100) / 100)
47
47
 
48
+ /**
49
+ * A cap without its exact byte count.
50
+ *
51
+ * `describeBytes` renders `99 MiB (103809024 bytes)` because the refusal it was
52
+ * written for is read by a client comparing a number to its own, and a
53
+ * parenthetical there is the difference between a machine acting on the message
54
+ * and a machine guessing at a rounded figure. On a page somebody is reading —
55
+ * the landing page's fine print, a row in the repository list — the same nine
56
+ * digits are noise in the middle of a sentence.
57
+ *
58
+ * DERIVED from `describeBytes` rather than formatted again, which is the whole
59
+ * care here: the invariant is that the cap a document prints and the cap
60
+ * `pre-receive` refuses on cannot look like two different numbers, and a second
61
+ * formatter is exactly how they would. Stripping a suffix off the one rendering
62
+ * cannot change the figure in front of it, so a document still cannot disagree
63
+ * with the hook — it only says less.
64
+ */
65
+ export function shortBytes(bytes: number): string {
66
+ return describeBytes(bytes).replace(/ \(\d+ bytes\)$/, '')
67
+ }
68
+
48
69
  /**
49
70
  * Is a boolean-ish walgit variable on?
50
71
  *
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Run a task over a list with a bounded number in flight.
3
+ *
4
+ * It lives in the Shared Kernel because both halves fan out over the same
5
+ * thing — one `index.json` per repository — and neither may own the pool:
6
+ * `src/usage.ts` reads every Index to report what the bucket holds, and
7
+ * `shared/repo-list.ts` reads a page of them at the edge to render the list
8
+ * (docs/adr/0010). It was written twice, docstring included, which is exactly
9
+ * the drift that ADR keeps naming: nothing here touches a runtime, so there is
10
+ * no reason for a second copy.
11
+ *
12
+ * Bounded rather than `Promise.all` over everything: a thousand simultaneous
13
+ * GETs against the object store is a burst that gets a deployment rate-limited
14
+ * for a read-only page.
15
+ *
16
+ * Order is the INPUT's, not completion order — every caller here pairs results
17
+ * back up with the names it asked about.
18
+ */
19
+ export async function pooled<T, R>(
20
+ items: readonly T[],
21
+ limit: number,
22
+ task: (item: T) => Promise<R>,
23
+ ): Promise<R[]> {
24
+ const results: R[] = Array.from({ length: items.length })
25
+ let next = 0
26
+ const workers = Array.from({ length: Math.min(limit, items.length) }, async () => {
27
+ for (let i = next++; i < items.length; i = next++) results[i] = await task(items[i]!)
28
+ })
29
+ await Promise.all(workers)
30
+ return results
31
+ }
@@ -175,6 +175,121 @@ export const MCP_PATH = '/_walgit/mcp'
175
175
  */
176
176
  export const ANNOUNCE_PATH = '/_walgit/announce'
177
177
 
178
+ /**
179
+ * The repository list — the web view's one page that is about the deployment
180
+ * rather than about a name (`shared/repo-list.ts`).
181
+ *
182
+ * Not under `/_walgit/`, unlike every path above it: this one is TYPED by a
183
+ * person into a browser, and the reserved prefix exists for endpoints a client
184
+ * speaks rather than for pages someone reads. It lives here rather than in
185
+ * `shared/repo-list.ts` so `classifyRequest` can name the route without
186
+ * importing the renderer.
187
+ *
188
+ * Same collision argument as `/llms.txt` and its neighbours: a repository is
189
+ * reached at `/<name>.git/…`, so a repository actually called `repos` keeps its
190
+ * clone URL and only loses its bare browse URL (README).
191
+ */
192
+ export const REPOS_PATH = '/repos'
193
+
194
+ /**
195
+ * One repository's refs and one level of its tree, read on the Cache.
196
+ *
197
+ * `?repo=&op=refs|tree&ref=&path=`, and under `/_walgit/` for the reason every
198
+ * endpoint above is: the prefix is reserved and a repository is reached at
199
+ * `/<name>.git/…`, so it cannot shadow one.
200
+ *
201
+ * It is for the EDGE rather than for a client — the two browse pages are
202
+ * rendered from its answer (`shared/browse.ts`) — but it is gated as the
203
+ * Provenance Read beside it is, by the deployment credential and then the Read
204
+ * Challenge, rather than by `INTERNAL_HEADER`: it answers a question about one
205
+ * repository, and the credential a clone of that repository needs is exactly
206
+ * the credential a browse of it needs.
207
+ *
208
+ * Unlike every other read here it has to touch the Cache, because the Index
209
+ * holds refs and no objects — which is why it is the container that answers it
210
+ * at all, where the repository list is answered at the edge off the log.
211
+ */
212
+ export const BROWSE_PATH = '/_walgit/browse'
213
+
214
+ /**
215
+ * The browse URLs a person types: `/<name>`, and `/<name>/<kind>/<rest>` for
216
+ * each of the four GitHub-shaped reads.
217
+ *
218
+ * Group 1 is the repository; group 2 is the kind; group 3 is everything after
219
+ * it, which is a ref and a path RUN TOGETHER — a branch name may contain
220
+ * slashes, so where one ends is a question only the Index can answer
221
+ * (`shared/browse.ts` splits it by longest ref prefix). The regex therefore
222
+ * takes the remainder whole rather than pretending the URL says where the
223
+ * boundary is.
224
+ *
225
+ * The kinds are spelled out rather than matched as `[^/]+` so a URL naming one
226
+ * walgit does not serve — `/<name>/blame/…` — is not a page at all rather than
227
+ * a browse that 404s from deeper in; and so a repository page cannot be
228
+ * confused with a directory inside one.
229
+ *
230
+ * Not under `/_walgit/`, like `REPOS_PATH` and for the same reason: these are
231
+ * pages someone reads rather than endpoints a client speaks. Same collision
232
+ * argument too — a repository is cloned at `/<name>.git/…`, so a repository
233
+ * called `repos` keeps its clone URL and only loses its bare browse URL.
234
+ */
235
+ export const BROWSE_HTTP = /^\/([^/]+)(?:\/(tree|blob|raw|commits)\/(.+))?$/
236
+
237
+ /**
238
+ * Which of the four reads a browse URL names.
239
+ *
240
+ * `tree` is a directory (and the bare `/<name>`), `blob` a file's page, `raw`
241
+ * its bytes, `commits` the history. One vocabulary for the URL, the container's
242
+ * `op` and the page renderer, so a route the edge claims and the container does
243
+ * not answer is a type error rather than a 404 someone finds later.
244
+ */
245
+ export type BrowseKind = 'tree' | 'blob' | 'raw' | 'commits'
246
+
247
+ /** A browse URL, as the page routes spell one. */
248
+ export interface BrowseRoute {
249
+ repo: string
250
+ kind: BrowseKind
251
+ /** Everything after the kind: a ref and a path run together, or `''`. */
252
+ rest: string
253
+ }
254
+
255
+ /**
256
+ * Is this one of the two browse pages, and which repository?
257
+ *
258
+ * Only GET and HEAD, and only a name walgit would actually serve — the path is
259
+ * attacker-controlled, and a name this accepted and `resolveRepo` refused would
260
+ * be a page about a repository half the service can see.
261
+ *
262
+ * Here beside the grammar rather than in `shared/browse.ts` for the reason
263
+ * `SMART_HTTP` is here: the edge ROUTES on it and `classifyRequest` COUNTS on
264
+ * it, and two readings of the grammar would be a metric describing traffic
265
+ * that never happened. The classifier makes one deliberate narrowing on top of
266
+ * it — a path whose SHAPE already has a bucket (`/wp-login.php`, `/.env`) keeps
267
+ * that bucket rather than becoming a repository page, so the scanner baseline
268
+ * those buckets exist to measure stays measurable. It is stated there, once.
269
+ */
270
+ export function wantsBrowse(method: string, pathname: string): BrowseRoute | null {
271
+ if (method !== 'GET' && method !== 'HEAD') return null
272
+ const route = BROWSE_HTTP.exec(pathname)
273
+ if (!route) return null
274
+ const repo = route[1]!
275
+ if (!REPO_ID.test(repo)) return null
276
+ // A bare `/<name>` is the tree at the default branch: the kind is absent
277
+ // from the URL because there is nothing yet to disambiguate.
278
+ return { repo, kind: (route[2] as BrowseKind | undefined) ?? 'tree', rest: route[3] ?? '' }
279
+ }
280
+
281
+ /**
282
+ * The challenge every walgit refusal of a read carries.
283
+ *
284
+ * One string, because it is answered by two very different clients and refused
285
+ * in four places: git prompts for a credential only when challenged in this
286
+ * scheme (which is what makes `git clone https://…` work interactively at all),
287
+ * and a browser prompts for the same reason on the web view. The realm is what
288
+ * ties them together — one credential answers both — so it is stated here with
289
+ * the rest of the wire vocabulary rather than typed out at each refusal.
290
+ */
291
+ export const BASIC_CHALLENGE = 'Basic realm="walgit"'
292
+
178
293
  // ── Grammar ─────────────────────────────────────────────────────────────────
179
294
 
180
295
  /**
@@ -258,6 +373,9 @@ export const SIGNERS_REF = 'refs/walgit/signers'
258
373
  * a misconfigured client
259
374
  * - `collision` a name already taken — a product signal about naming
260
375
  * - `unauthorized` a bad or missing credential
376
+ * - `invalid` a request walgit could not read at all — a malformed
377
+ * history cursor, say. Not a thing that might have existed,
378
+ * which is what separates it from `not-found`
261
379
  * - `edge` walgit did not refuse; something in front of it did. This
262
380
  * one is a BUG SIGNAL: every refusal walgit means to make it
263
381
  * should make itself, with an explanation. Absorbing it into
@@ -267,6 +385,7 @@ export type RejectKind =
267
385
  | 'size-cap'
268
386
  | 'collision'
269
387
  | 'unauthorized'
388
+ | 'invalid'
270
389
  | 'not-found'
271
390
  | 'unavailable'
272
391
  | 'edge'
@@ -289,6 +408,7 @@ const KINDS = new Set<RejectKind>([
289
408
  'size-cap',
290
409
  'collision',
291
410
  'unauthorized',
411
+ 'invalid',
292
412
  'not-found',
293
413
  'unavailable',
294
414
  'edge',