@zabaca/zbc 0.19.1 → 0.21.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/LICENSE +21 -0
- package/package.json +2 -1
- package/src/utils/copy-template.ts +37 -0
- package/templates/apps/inbox/package.json +1 -0
- package/templates/apps/secret-relay/package.json +1 -0
- package/templates/apps/walgit/CONTEXT.md +12 -0
- package/templates/apps/walgit/README.md +73 -2
- package/templates/apps/walgit/assets/agentgit-mark.svg +1 -0
- package/templates/apps/walgit/assets/agentgit-og.png +0 -0
- package/templates/apps/walgit/package.json +6 -1
- package/templates/apps/walgit/registry.json +1 -1
- package/templates/apps/walgit/shared/analytics.ts +100 -0
- package/templates/apps/walgit/shared/favicon.ts +72 -0
- package/templates/apps/walgit/shared/landing.ts +82 -9
- package/templates/apps/walgit/shared/llms.ts +16 -0
- package/templates/apps/walgit/shared/mcp.ts +286 -0
- package/templates/apps/walgit/shared/og-image.ts +51 -0
- package/templates/apps/walgit/shared/protocol.ts +20 -0
- package/templates/apps/walgit/shared/telemetry.ts +86 -2
- package/templates/apps/walgit/worker/assets.d.ts +13 -0
- package/templates/apps/walgit/worker/index.ts +173 -3
- package/templates/apps/walgit/worker/mcp.ts +281 -0
- package/templates/apps/walgit/wrangler.jsonc +10 -0
- package/templates/apps/warehouse/package.json +1 -0
|
@@ -33,6 +33,16 @@ import { parseTokens } from '../shared/credentials'
|
|
|
33
33
|
import { authorizeAnnounce, authorizeSubscribe } from '../shared/events'
|
|
34
34
|
import { renderLanding, wantsLanding } from '../shared/landing'
|
|
35
35
|
import { renderLlms, wantsLlms } from '../shared/llms'
|
|
36
|
+
import {
|
|
37
|
+
FAVICON_BODY,
|
|
38
|
+
FAVICON_CONTENT_TYPE,
|
|
39
|
+
ICON_CACHE_CONTROL,
|
|
40
|
+
TOUCH_ICON_STATUS,
|
|
41
|
+
wantsFavicon,
|
|
42
|
+
wantsTouchIcon,
|
|
43
|
+
} from '../shared/favicon'
|
|
44
|
+
import { OG_IMAGE_CACHE_CONTROL, OG_IMAGE_CONTENT_TYPE, wantsOgImage } from '../shared/og-image'
|
|
45
|
+
import { analyticsFrom } from '../shared/analytics'
|
|
36
46
|
import { operatorFrom } from '../shared/operator'
|
|
37
47
|
import { renderRobots, wantsRobots } from '../shared/robots'
|
|
38
48
|
import {
|
|
@@ -42,6 +52,7 @@ import {
|
|
|
42
52
|
EXPIRE_PATH,
|
|
43
53
|
INTERNAL_HEADER,
|
|
44
54
|
INTERNAL_HEADERS,
|
|
55
|
+
MCP_PATH,
|
|
45
56
|
SERVED_HEADER,
|
|
46
57
|
} from '../shared/protocol'
|
|
47
58
|
import {
|
|
@@ -51,6 +62,11 @@ import {
|
|
|
51
62
|
type RequestMetric,
|
|
52
63
|
} from '../shared/telemetry'
|
|
53
64
|
import { BROADCAST_PATH, EVENTS_OBJECT_NAME, WalgitEvents } from './events-do'
|
|
65
|
+
import { handleMcp } from './mcp'
|
|
66
|
+
// The card's picture, as bytes in the bundle (wrangler.jsonc's `Data` rule).
|
|
67
|
+
// Rendered once by `scripts/render-og-image.ts` in the zbc repository and
|
|
68
|
+
// committed — never drawn per request.
|
|
69
|
+
import OG_IMAGE_BYTES from '../assets/agentgit-og.png'
|
|
54
70
|
|
|
55
71
|
export interface Env {
|
|
56
72
|
WALGIT_CONTAINER: DurableObjectNamespace<WalgitContainer>
|
|
@@ -149,6 +165,14 @@ export interface Env {
|
|
|
149
165
|
*/
|
|
150
166
|
WALGIT_OPERATOR?: string
|
|
151
167
|
WALGIT_CONTACT?: string
|
|
168
|
+
/**
|
|
169
|
+
* Browser analytics on the landing page (`shared/analytics.ts`). Edge-only,
|
|
170
|
+
* like the two above and for the same reason. Read ONLY through
|
|
171
|
+
* `analyticsFrom`.
|
|
172
|
+
*/
|
|
173
|
+
WALGIT_POSTHOG_KEY?: string
|
|
174
|
+
WALGIT_POSTHOG_HOST?: string
|
|
175
|
+
WALGIT_POSTHOG_UI_HOST?: string
|
|
152
176
|
WALGIT_EVENTS: DurableObjectNamespace<WalgitEvents>
|
|
153
177
|
}
|
|
154
178
|
|
|
@@ -285,6 +309,9 @@ export default {
|
|
|
285
309
|
// beside the capabilities and passed to both documents, so the page and
|
|
286
310
|
// the manual cannot name two different operators.
|
|
287
311
|
const operator = operatorFrom(env)
|
|
312
|
+
// Browser analytics for the page (`shared/analytics.ts`), off unless the
|
|
313
|
+
// instance set a key. Read beside the two reads it mirrors.
|
|
314
|
+
const analytics = analyticsFrom(env)
|
|
288
315
|
|
|
289
316
|
// The browser half of `/`, answered at the edge (shared/landing.ts). Placed
|
|
290
317
|
// before every other decision on purpose: a link on an aggregator points at
|
|
@@ -297,7 +324,7 @@ export default {
|
|
|
297
324
|
// them: this document and `/llms.txt` want a bare hostname they prefix
|
|
298
325
|
// with `https://`/`wss://` themselves, while `GET /` needs a full origin
|
|
299
326
|
// including the scheme, and one field could not serve both.
|
|
300
|
-
const page = renderLanding(url.host, caps, operator)
|
|
327
|
+
const page = renderLanding(url.host, caps, operator, analytics)
|
|
301
328
|
const bytes = new TextEncoder().encode(page)
|
|
302
329
|
record(env, ctx, {
|
|
303
330
|
kind: 'landing',
|
|
@@ -397,6 +424,102 @@ export default {
|
|
|
397
424
|
})
|
|
398
425
|
}
|
|
399
426
|
|
|
427
|
+
// `/agentgit-og.png` — the picture the card the head advertises is made
|
|
428
|
+
// of (shared/og-image.ts). At the edge for the reason the page is, and
|
|
429
|
+
// more so: the traffic is crawlers, arriving in a burst when a link is
|
|
430
|
+
// posted, and not one of them should wake the container for a picture
|
|
431
|
+
// that changes only on deploy. The bytes are in the bundle, so nothing is
|
|
432
|
+
// fetched to serve it. Same collision argument as `/llms.txt` and
|
|
433
|
+
// `/robots.txt`: a repository called `agentgit-og.png` is reached at
|
|
434
|
+
// `/agentgit-og.png.git/…`, so this route cannot shadow one.
|
|
435
|
+
if (wantsOgImage(request.method, url.pathname)) {
|
|
436
|
+
record(env, ctx, {
|
|
437
|
+
kind: 'og-image',
|
|
438
|
+
repo: '',
|
|
439
|
+
outcome: 'ok',
|
|
440
|
+
reject: '',
|
|
441
|
+
status: 200,
|
|
442
|
+
served: false,
|
|
443
|
+
cold: false,
|
|
444
|
+
ttfbMs: Date.now() - startedAt,
|
|
445
|
+
totalMs: Date.now() - startedAt,
|
|
446
|
+
// A HEAD is answered with headers only, so it served no bytes.
|
|
447
|
+
bytesServed: request.method === 'HEAD' ? 0 : OG_IMAGE_BYTES.byteLength,
|
|
448
|
+
bytesReceived: 0,
|
|
449
|
+
})
|
|
450
|
+
return new Response(request.method === 'HEAD' ? null : OG_IMAGE_BYTES, {
|
|
451
|
+
status: 200,
|
|
452
|
+
headers: {
|
|
453
|
+
'content-type': OG_IMAGE_CONTENT_TYPE,
|
|
454
|
+
// A day, where the page and the manual get a minute: the picture
|
|
455
|
+
// states no capability and no limit, so nothing in it can outlive a
|
|
456
|
+
// config change.
|
|
457
|
+
'cache-control': OG_IMAGE_CACHE_CONTROL,
|
|
458
|
+
// A HEAD must answer the size the GET would send; a Response built
|
|
459
|
+
// from a null body would otherwise report none.
|
|
460
|
+
'content-length': String(OG_IMAGE_BYTES.byteLength),
|
|
461
|
+
},
|
|
462
|
+
})
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
// `/favicon.ico` — the request a browser makes on every first view of the
|
|
466
|
+
// page whatever the head declares, and the one the container was answering
|
|
467
|
+
// 404 to ~282 times in the two hours after the launch link went up. The
|
|
468
|
+
// bytes are the mark the head already inlines, read from one constant
|
|
469
|
+
// (shared/favicon.ts → shared/landing.ts), so the tab and the masthead
|
|
470
|
+
// cannot drift. Same collision argument as `/llms.txt`, `/robots.txt` and
|
|
471
|
+
// `/agentgit-og.png`: a repository called `favicon.ico` is reached at
|
|
472
|
+
// `/favicon.ico.git/…`, so this route cannot shadow one.
|
|
473
|
+
if (wantsFavicon(request.method, url.pathname)) {
|
|
474
|
+
const bytes = new TextEncoder().encode(FAVICON_BODY)
|
|
475
|
+
record(env, ctx, {
|
|
476
|
+
kind: 'favicon',
|
|
477
|
+
repo: '',
|
|
478
|
+
outcome: 'ok',
|
|
479
|
+
reject: '',
|
|
480
|
+
status: 200,
|
|
481
|
+
served: false,
|
|
482
|
+
cold: false,
|
|
483
|
+
ttfbMs: Date.now() - startedAt,
|
|
484
|
+
totalMs: Date.now() - startedAt,
|
|
485
|
+
bytesServed: request.method === 'HEAD' ? 0 : bytes.byteLength,
|
|
486
|
+
bytesReceived: 0,
|
|
487
|
+
})
|
|
488
|
+
return new Response(request.method === 'HEAD' ? null : bytes, {
|
|
489
|
+
status: 200,
|
|
490
|
+
headers: {
|
|
491
|
+
'content-type': FAVICON_CONTENT_TYPE,
|
|
492
|
+
'cache-control': ICON_CACHE_CONTROL,
|
|
493
|
+
// A HEAD must answer the size the GET would send.
|
|
494
|
+
'content-length': String(bytes.byteLength),
|
|
495
|
+
},
|
|
496
|
+
})
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
// The touch icons iOS asks for when a page is bookmarked, which the head
|
|
500
|
+
// does not declare and no raster exists for. 204 rather than 404 because a
|
|
501
|
+
// 404 is what a client retries; with the day-long cache header above, this
|
|
502
|
+
// is asked once a day instead of once a page. Same collision argument.
|
|
503
|
+
if (wantsTouchIcon(request.method, url.pathname)) {
|
|
504
|
+
record(env, ctx, {
|
|
505
|
+
kind: 'favicon',
|
|
506
|
+
repo: '',
|
|
507
|
+
outcome: 'ok',
|
|
508
|
+
reject: '',
|
|
509
|
+
status: TOUCH_ICON_STATUS,
|
|
510
|
+
served: false,
|
|
511
|
+
cold: false,
|
|
512
|
+
ttfbMs: Date.now() - startedAt,
|
|
513
|
+
totalMs: Date.now() - startedAt,
|
|
514
|
+
bytesServed: 0,
|
|
515
|
+
bytesReceived: 0,
|
|
516
|
+
})
|
|
517
|
+
return new Response(null, {
|
|
518
|
+
status: TOUCH_ICON_STATUS,
|
|
519
|
+
headers: { 'cache-control': ICON_CACHE_CONTROL },
|
|
520
|
+
})
|
|
521
|
+
}
|
|
522
|
+
|
|
400
523
|
// The ref-event stream, answered at the edge for the same reason the
|
|
401
524
|
// landing page is: a subscription is a socket the container has no reason
|
|
402
525
|
// to hold, and holding one would keep the single container awake for as
|
|
@@ -414,6 +537,50 @@ export default {
|
|
|
414
537
|
return events(request, url, env, caps)
|
|
415
538
|
}
|
|
416
539
|
|
|
540
|
+
// The MCP endpoint (`shared/mcp.ts`), answered at the edge like the two
|
|
541
|
+
// documents above: a tool call is a question about a name, and the manual
|
|
542
|
+
// it also serves is rendered from these same capabilities rather than
|
|
543
|
+
// fetched. Unconditionally routed, unlike the socket above — the endpoint
|
|
544
|
+
// is a read surface that exists on every deployment, and the one tool that
|
|
545
|
+
// needs the stream (`agentgit_watch`) says so itself where it is off.
|
|
546
|
+
//
|
|
547
|
+
// Same collision argument as `/llms.txt` and its neighbours, and stronger:
|
|
548
|
+
// `/_walgit/` is reserved, and a repository called `mcp` is reached at
|
|
549
|
+
// `/mcp.git/…`, so this route cannot shadow one.
|
|
550
|
+
if (url.pathname === MCP_PATH) {
|
|
551
|
+
const response = await handleMcp(request, env, caps, operator, url.host)
|
|
552
|
+
record(env, ctx, {
|
|
553
|
+
// Classified rather than hand-labelled, so the kind this row carries
|
|
554
|
+
// and the kind `shared/telemetry.ts` names for this path cannot become
|
|
555
|
+
// two facts.
|
|
556
|
+
...classifyRequest(request.method, url.pathname, url.search),
|
|
557
|
+
// From the HTTP status, and deliberately not from the JSON-RPC body:
|
|
558
|
+
// a tool that REFUSED — a Private name an unproven reader asked about —
|
|
559
|
+
// answers 200 carrying `isError`, and is counted `ok` here. That is the
|
|
560
|
+
// honest reading of this column (the request was served) and the only
|
|
561
|
+
// affordable one: seeing the refusal would mean buffering and parsing
|
|
562
|
+
// every response body to label a row. A refusal the endpoint makes at
|
|
563
|
+
// the HTTP level, a malformed JSON-RPC body's 400, is counted.
|
|
564
|
+
outcome: response.ok ? 'ok' : 'reject',
|
|
565
|
+
reject: response.ok ? '' : 'other',
|
|
566
|
+
status: response.status,
|
|
567
|
+
// The container answers `agentgit_status` and `agentgit_provenance`
|
|
568
|
+
// behind this, but the REQUEST was answered here — `served` is about
|
|
569
|
+
// which layer produced the response, and saying otherwise would make
|
|
570
|
+
// the `edge` refusal signal unreadable.
|
|
571
|
+
served: false,
|
|
572
|
+
cold: false,
|
|
573
|
+
ttfbMs: Date.now() - startedAt,
|
|
574
|
+
totalMs: Date.now() - startedAt,
|
|
575
|
+
// Not counted: an MCP body is small and may be a stream held open for
|
|
576
|
+
// five minutes by `agentgit_watch`, and wrapping it to weigh it would
|
|
577
|
+
// hold the datapoint for the length of the wait.
|
|
578
|
+
bytesServed: 0,
|
|
579
|
+
bytesReceived: Number(request.headers.get('content-length') ?? '0') || 0,
|
|
580
|
+
})
|
|
581
|
+
return response
|
|
582
|
+
}
|
|
583
|
+
|
|
417
584
|
const facts = classifyRequest(request.method, url.pathname, url.search)
|
|
418
585
|
|
|
419
586
|
// The container's expiry endpoint trusts INTERNAL_HEADER to mean "the
|
|
@@ -618,13 +785,16 @@ function stripInternal(request: Request): Request {
|
|
|
618
785
|
|
|
619
786
|
/** The facts known before the container answers. */
|
|
620
787
|
function base(
|
|
621
|
-
facts: { kind: RequestMetric['kind']; repo: string },
|
|
788
|
+
facts: { kind: RequestMetric['kind']; repo: string; bucket?: RequestMetric['bucket'] },
|
|
622
789
|
request: Request,
|
|
623
|
-
): Pick<RequestMetric, 'kind' | 'repo' | 'bytesReceived'> {
|
|
790
|
+
): Pick<RequestMetric, 'kind' | 'repo' | 'bucket' | 'bytesReceived'> {
|
|
624
791
|
const declared = Number(request.headers.get('content-length') ?? '0')
|
|
625
792
|
return {
|
|
626
793
|
kind: facts.kind,
|
|
627
794
|
repo: facts.repo,
|
|
795
|
+
// Only `other` carries one, and only `other` writes it to the datapoint —
|
|
796
|
+
// which is the one kind that reaches this helper without a route to name.
|
|
797
|
+
bucket: facts.bucket,
|
|
628
798
|
bytesReceived: Number.isFinite(declared) ? declared : 0,
|
|
629
799
|
}
|
|
630
800
|
}
|
|
@@ -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
|
+
}
|
|
@@ -14,6 +14,16 @@
|
|
|
14
14
|
"compatibility_date": "2026-07-01",
|
|
15
15
|
"compatibility_flags": ["nodejs_compat"],
|
|
16
16
|
"observability": { "enabled": true },
|
|
17
|
+
// The link-preview card (assets/agentgit-og.png, shared/og-image.ts) travels
|
|
18
|
+
// in the Worker bundle as bytes: a crawler fetching the picture must not
|
|
19
|
+
// wake the container any more than a reader fetching the page does, and a
|
|
20
|
+
// 40 KB flat-colour PNG is cheaper to carry than a second service to fetch
|
|
21
|
+
// it from. `Data` is the loader that makes `import png from './x.png'` the
|
|
22
|
+
// file's ArrayBuffer (worker/assets.d.ts declares that for TypeScript).
|
|
23
|
+
// `fallthrough` so wrangler's own default Data rule (`**/*.bin`) still
|
|
24
|
+
// applies after this one; without it wrangler warns that this rule silently
|
|
25
|
+
// replaces the built-in.
|
|
26
|
+
"rules": [{ "type": "Data", "globs": ["**/*.png"], "fallthrough": true }],
|
|
17
27
|
"containers": [
|
|
18
28
|
{
|
|
19
29
|
"class_name": "WalgitContainer",
|