@zabaca/zbc 0.20.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 (55) hide show
  1. package/package.json +1 -1
  2. package/templates/apps/walgit/CONTEXT.md +37 -0
  3. package/templates/apps/walgit/README.md +193 -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/package.json +5 -1
  7. package/templates/apps/walgit/registry.json +1 -1
  8. package/templates/apps/walgit/shared/browse.ts +733 -0
  9. package/templates/apps/walgit/shared/capabilities.ts +17 -0
  10. package/templates/apps/walgit/shared/container-env.ts +9 -0
  11. package/templates/apps/walgit/shared/landing.ts +8 -27
  12. package/templates/apps/walgit/shared/llms.ts +41 -0
  13. package/templates/apps/walgit/shared/mcp.ts +286 -0
  14. package/templates/apps/walgit/shared/policy.ts +21 -0
  15. package/templates/apps/walgit/shared/pooled.ts +31 -0
  16. package/templates/apps/walgit/shared/protocol.ts +140 -0
  17. package/templates/apps/walgit/shared/repo-list.ts +478 -0
  18. package/templates/apps/walgit/shared/store.ts +307 -0
  19. package/templates/apps/walgit/shared/telemetry.ts +79 -6
  20. package/templates/apps/walgit/shared/wal-index.ts +249 -0
  21. package/templates/apps/walgit/shared/web.ts +46 -0
  22. package/templates/apps/walgit/src/announce-main.ts +1 -1
  23. package/templates/apps/walgit/src/announce.ts +1 -1
  24. package/templates/apps/walgit/src/append-only.ts +5 -8
  25. package/templates/apps/walgit/src/browse.ts +230 -0
  26. package/templates/apps/walgit/src/cache.ts +4 -4
  27. package/templates/apps/walgit/src/compact.ts +6 -12
  28. package/templates/apps/walgit/src/delete-repo.ts +4 -3
  29. package/templates/apps/walgit/src/expire.ts +3 -3
  30. package/templates/apps/walgit/src/gc.ts +4 -3
  31. package/templates/apps/walgit/src/git.ts +127 -10
  32. package/templates/apps/walgit/src/hook-main.ts +1 -1
  33. package/templates/apps/walgit/src/http.ts +386 -3
  34. package/templates/apps/walgit/src/limits.ts +1 -1
  35. package/templates/apps/walgit/src/materialize.bench.ts +4 -10
  36. package/templates/apps/walgit/src/materialize.ts +11 -4
  37. package/templates/apps/walgit/src/orphans.ts +3 -3
  38. package/templates/apps/walgit/src/pending.ts +1 -1
  39. package/templates/apps/walgit/src/private.ts +1 -1
  40. package/templates/apps/walgit/src/proposals.ts +3 -3
  41. package/templates/apps/walgit/src/push-cert.ts +10 -1
  42. package/templates/apps/walgit/src/push.ts +11 -9
  43. package/templates/apps/walgit/src/reconcile.ts +4 -3
  44. package/templates/apps/walgit/src/server.ts +29 -2
  45. package/templates/apps/walgit/src/signers.ts +9 -3
  46. package/templates/apps/walgit/src/store-env.ts +8 -21
  47. package/templates/apps/walgit/src/store.ts +12 -241
  48. package/templates/apps/walgit/src/sync.ts +2 -2
  49. package/templates/apps/walgit/src/usage.ts +4 -18
  50. package/templates/apps/walgit/src/verify.ts +2 -2
  51. package/templates/apps/walgit/src/wal-index.ts +20 -239
  52. package/templates/apps/walgit/worker/index.ts +278 -1
  53. package/templates/apps/walgit/worker/mcp.ts +281 -0
  54. /package/templates/apps/walgit/{src → shared}/keys.ts +0 -0
  55. /package/templates/apps/walgit/{src → shared}/ulid.ts +0 -0
@@ -0,0 +1,281 @@
1
+ /**
2
+ * The MCP endpoint's Workers half: a transport, and the three reads.
3
+ *
4
+ * Everything an agent can ASK is `shared/mcp.ts` — the tools, their schemas,
5
+ * their descriptions and the one rule about refusals — and it is tested there
6
+ * against a real MCP client with no runtime. What is left here is exactly the
7
+ * part that needs one: a Streamable HTTP transport, a container round trip, and
8
+ * a socket to the ref-event Durable Object.
9
+ *
10
+ * Two properties this file is responsible for keeping:
11
+ *
12
+ * - **The gate is the host's own, not a second copy.** `status` and
13
+ * `provenance` both go through the container's `PROVENANCE_PATH`, which is
14
+ * already placed behind exactly the credential a clone needs and already
15
+ * applies the Read Challenge for a Private name (`src/http.ts`,
16
+ * docs/adr/0013). A refusal it makes is forwarded verbatim; the internal
17
+ * `REFS_PATH` read that fills in the refs happens ONLY after that refusal
18
+ * did not come, because it is `INTERNAL_HEADER`-gated and gates nothing
19
+ * itself.
20
+ * - **The request is served at the edge**, in the sense that matters: the MCP
21
+ * protocol itself is answered here and the container never routes
22
+ * `/_walgit/mcp`. `watch` subscribes to the Durable Object the way a
23
+ * WebSocket client does, and the manual is rendered from the same
24
+ * capabilities the `/llms.txt` route uses, so neither touches the container
25
+ * at all.
26
+ *
27
+ * `status` and `provenance` DO wake it, and `status` twice — once for the gated
28
+ * provenance read and once for `REFS_PATH`. That is the price of the first
29
+ * property rather than an oversight: refs are authoritative in the Index
30
+ * (docs/adr/0007), the Index is reachable only with the container's object-store
31
+ * credentials (`shared/container-env.ts`), and the gate that decides whether
32
+ * this caller may see them is the container's too. An edge answer would need
33
+ * both a second verifier and a second reader — the two things ADR-0013 and
34
+ * ADR-0007 each refuse. Folding the refs into the provenance response would
35
+ * save the second trip and is a container-route change this one did not make.
36
+ */
37
+
38
+ import { StreamableHTTPTransport } from '@hono/mcp'
39
+ import { getContainer } from '@cloudflare/containers'
40
+ import { Hono } from 'hono'
41
+
42
+ import type { Capabilities } from '../shared/capabilities'
43
+ import { parseTokens } from '../shared/credentials'
44
+ import { authorizeSubscribe } from '../shared/events'
45
+ import {
46
+ type McpDeps,
47
+ type ProvenanceFacts,
48
+ type Read,
49
+ type StatusFacts,
50
+ type WatchOutcome,
51
+ createAgentgitMcp,
52
+ } from '../shared/mcp'
53
+ import type { Operator } from '../shared/operator'
54
+ import {
55
+ EVENTS_PATH,
56
+ INTERNAL_HEADER,
57
+ MCP_PATH,
58
+ PROVENANCE_PATH,
59
+ REFS_PATH,
60
+ } from '../shared/protocol'
61
+ import { EVENTS_OBJECT_NAME } from './events-do'
62
+ import type { Env } from './index'
63
+
64
+ /** What a claimed name's provenance read carries about its lists. */
65
+ interface Claim {
66
+ signers: string[]
67
+ readers?: string[]
68
+ ts: string
69
+ }
70
+
71
+ /**
72
+ * Answer one MCP request.
73
+ *
74
+ * A fresh `McpServer` per request, like the inbox's endpoint and for the same
75
+ * reason: a Workers isolate is per-request and holds no session, so there is no
76
+ * long-lived server to keep. `app.all` because Streamable HTTP puts client
77
+ * messages on POST and the server-to-client stream on GET, and a transport
78
+ * mounted for one of them is a client that connects and then hears nothing.
79
+ */
80
+ export async function handleMcp(
81
+ request: Request,
82
+ env: Env,
83
+ caps: Capabilities,
84
+ operator: Operator | null,
85
+ host: string,
86
+ ): Promise<Response> {
87
+ const server = createAgentgitMcp(deps(request, env, caps, operator, host))
88
+ const transport = new StreamableHTTPTransport()
89
+ await server.connect(transport)
90
+ const app = new Hono()
91
+ app.all(MCP_PATH, (c) => transport.handleRequest(c))
92
+ return app.fetch(request)
93
+ }
94
+
95
+ function deps(
96
+ request: Request,
97
+ env: Env,
98
+ caps: Capabilities,
99
+ operator: Operator | null,
100
+ host: string,
101
+ ): McpDeps {
102
+ // The caller's own credential, forwarded unchanged to every gated read. An
103
+ // MCP client sends it as an ordinary Authorization header, which is the same
104
+ // header a clone of a Private repository presents.
105
+ const authorization = request.headers.get('authorization')
106
+
107
+ return {
108
+ version: env.WALGIT_BUILD_ID ?? '0.0.0',
109
+ host,
110
+ caps,
111
+ operator,
112
+
113
+ async status(name): Promise<Read<StatusFacts>> {
114
+ const read = await provenanceRead(env, authorization, name)
115
+ if (!read.ok) return read
116
+ // Only now: `REFS_PATH` is internal and judges nothing, so reading it
117
+ // before the gate above answered would be the second copy of the gate
118
+ // this file exists not to have.
119
+ const refs = await readRefs(env, name)
120
+ const claim = read.claim
121
+ return {
122
+ ok: true,
123
+ // The same judgment `src/http.ts` makes of a name nobody has pushed to:
124
+ // a repository with no refs is one that does not exist yet.
125
+ exists: Object.keys(refs).length > 0,
126
+ claimed: claim !== undefined,
127
+ // Presence is the switch, and `[]` is a value — an empty Reader List
128
+ // means the Signer List reads this and nobody else (docs/adr/0013).
129
+ private: claim?.readers !== undefined,
130
+ refs,
131
+ }
132
+ },
133
+
134
+ async provenance(name): Promise<Read<{ repo: string; provenance: ProvenanceFacts }>> {
135
+ const read = await provenanceRead(env, authorization, name)
136
+ if (!read.ok) return read
137
+ return { ok: true, repo: name, provenance: read.provenance }
138
+ },
139
+
140
+ watch: (name, ref, timeoutMs) => watchRef(env, caps, authorization, name, ref, timeoutMs),
141
+ }
142
+ }
143
+
144
+ /**
145
+ * One repository's provenance and Claim, through the container's own gate.
146
+ *
147
+ * The status code is carried out with the body, because the body is the
148
+ * refusal the host wrote and `shared/mcp.ts` forwards it verbatim — a 401 for a
149
+ * Private name an unproven reader asked about has to reach the agent saying
150
+ * what `info/refs` would say, and nothing more.
151
+ */
152
+ async function provenanceRead(
153
+ env: Env,
154
+ authorization: string | null,
155
+ name: string,
156
+ ): Promise<Read<{ provenance: ProvenanceFacts; claim?: Claim }>> {
157
+ const headers = new Headers()
158
+ if (authorization) headers.set('authorization', authorization)
159
+ const response = await getContainer(env.WALGIT_CONTAINER).fetch(
160
+ new Request(`https://walgit.internal${PROVENANCE_PATH}?repo=${encodeURIComponent(name)}`, {
161
+ headers,
162
+ }),
163
+ )
164
+ if (!response.ok) {
165
+ const message = (await response.text()).trim()
166
+ return {
167
+ ok: false,
168
+ status: response.status,
169
+ message: message === '' ? `walgit: the host answered ${response.status}` : message,
170
+ }
171
+ }
172
+ const body = (await response.json()) as { provenance?: ProvenanceFacts; claim?: Claim }
173
+ return { ok: true, provenance: body.provenance ?? {}, claim: body.claim }
174
+ }
175
+
176
+ /** One repository's ref state, read from the Index through the container. */
177
+ async function readRefs(env: Env, name: string): Promise<Record<string, string>> {
178
+ const response = await getContainer(env.WALGIT_CONTAINER).fetch(
179
+ new Request(`https://walgit.internal${REFS_PATH}?repo=${encodeURIComponent(name)}`, {
180
+ headers: { [INTERNAL_HEADER]: '1' },
181
+ }),
182
+ )
183
+ // Empty rather than thrown: the gate above already said this name may be
184
+ // read, and a refs lookup that did not answer is a repository with nothing
185
+ // known about it — which is what `exists: false` says.
186
+ if (!response.ok) return {}
187
+ const body = (await response.json()) as { refs?: Record<string, string> }
188
+ return body.refs ?? {}
189
+ }
190
+
191
+ /**
192
+ * Wait for one ref to move, over the same socket a `watch` client uses.
193
+ *
194
+ * The gate is the event stream's own, in both halves: the deployment
195
+ * credential is checked here exactly as `events()` checks it in `index.ts`, and
196
+ * the Reader List is checked by the Durable Object against the credential this
197
+ * request forwards (`worker/events-do.ts`). Neither is re-decided.
198
+ *
199
+ * Throws rather than returning a verdict when the stream cannot be reached or
200
+ * refuses: an answer of `{ timedOut: true }` would tell an agent that nothing
201
+ * happened, which is a different and false statement.
202
+ */
203
+ async function watchRef(
204
+ env: Env,
205
+ caps: Capabilities,
206
+ authorization: string | null,
207
+ repo: string,
208
+ ref: string | null,
209
+ timeoutMs: number,
210
+ ): Promise<WatchOutcome> {
211
+ if (!caps.events) {
212
+ throw new Error('this deployment does not serve the ref-event stream')
213
+ }
214
+ if (
215
+ !authorizeSubscribe({
216
+ authorization,
217
+ tokens: parseTokens(env.WALGIT_HTTP_TOKENS),
218
+ isPublic: caps.publicAccess,
219
+ })
220
+ ) {
221
+ throw new Error('unauthorized')
222
+ }
223
+
224
+ const headers = new Headers({ upgrade: 'websocket' })
225
+ if (authorization) headers.set('authorization', authorization)
226
+ const stub = env.WALGIT_EVENTS.get(env.WALGIT_EVENTS.idFromName(EVENTS_OBJECT_NAME))
227
+ const response = await stub.fetch(
228
+ new Request(`https://walgit.internal${EVENTS_PATH}`, { headers }),
229
+ )
230
+ const socket = response.webSocket
231
+ if (!socket) throw new Error(`the event stream answered ${response.status}`)
232
+ socket.accept()
233
+
234
+ return new Promise<WatchOutcome>((resolve, reject) => {
235
+ let settled = false
236
+ const finish = (run: () => void) => {
237
+ if (settled) return
238
+ settled = true
239
+ clearTimeout(timer)
240
+ try {
241
+ socket.close(1000, 'walgit: done')
242
+ } catch {
243
+ // Already gone.
244
+ }
245
+ run()
246
+ }
247
+ const timer = setTimeout(() => finish(() => resolve({ timedOut: true })), timeoutMs)
248
+
249
+ socket.addEventListener('message', (event) => {
250
+ let message: Record<string, unknown>
251
+ try {
252
+ const raw = typeof event.data === 'string' ? event.data : ''
253
+ message = JSON.parse(raw) as Record<string, unknown>
254
+ } catch {
255
+ return
256
+ }
257
+ if (typeof message.error === 'string') {
258
+ const error = message.error
259
+ finish(() => reject(new Error(error)))
260
+ return
261
+ }
262
+ // The handshake is current state for what was named, not a ref that
263
+ // moved — resolving on it would make every call return immediately.
264
+ if (message.ok === true) return
265
+ if (typeof message.ref !== 'string') return
266
+ const moved = message.ref
267
+ const sha = typeof message.sha === 'string' ? message.sha : null
268
+ finish(() => resolve({ timedOut: false, ref: moved, sha }))
269
+ })
270
+ socket.addEventListener('close', () =>
271
+ finish(() => reject(new Error('the event stream closed the subscription'))),
272
+ )
273
+ socket.addEventListener('error', () =>
274
+ finish(() => reject(new Error('the event stream failed'))),
275
+ )
276
+
277
+ // Omitting `refs` watches every ref in the repository, which is what a
278
+ // caller that named none asked for.
279
+ socket.send(JSON.stringify({ watch: [{ repo, ...(ref === null ? {} : { refs: [ref] }) }] }))
280
+ })
281
+ }
File without changes
File without changes