@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.
- package/package.json +1 -1
- package/templates/apps/walgit/CONTEXT.md +37 -0
- package/templates/apps/walgit/README.md +193 -4
- package/templates/apps/walgit/e2e/harness.ts +2 -2
- package/templates/apps/walgit/e2e/scenarios.ts +302 -2
- package/templates/apps/walgit/package.json +5 -1
- package/templates/apps/walgit/registry.json +1 -1
- package/templates/apps/walgit/shared/browse.ts +733 -0
- package/templates/apps/walgit/shared/capabilities.ts +17 -0
- package/templates/apps/walgit/shared/container-env.ts +9 -0
- package/templates/apps/walgit/shared/landing.ts +8 -27
- package/templates/apps/walgit/shared/llms.ts +41 -0
- package/templates/apps/walgit/shared/mcp.ts +286 -0
- package/templates/apps/walgit/shared/policy.ts +21 -0
- package/templates/apps/walgit/shared/pooled.ts +31 -0
- package/templates/apps/walgit/shared/protocol.ts +140 -0
- package/templates/apps/walgit/shared/repo-list.ts +478 -0
- package/templates/apps/walgit/shared/store.ts +307 -0
- package/templates/apps/walgit/shared/telemetry.ts +79 -6
- package/templates/apps/walgit/shared/wal-index.ts +249 -0
- package/templates/apps/walgit/shared/web.ts +46 -0
- package/templates/apps/walgit/src/announce-main.ts +1 -1
- package/templates/apps/walgit/src/announce.ts +1 -1
- package/templates/apps/walgit/src/append-only.ts +5 -8
- package/templates/apps/walgit/src/browse.ts +230 -0
- package/templates/apps/walgit/src/cache.ts +4 -4
- package/templates/apps/walgit/src/compact.ts +6 -12
- package/templates/apps/walgit/src/delete-repo.ts +4 -3
- package/templates/apps/walgit/src/expire.ts +3 -3
- package/templates/apps/walgit/src/gc.ts +4 -3
- package/templates/apps/walgit/src/git.ts +127 -10
- package/templates/apps/walgit/src/hook-main.ts +1 -1
- package/templates/apps/walgit/src/http.ts +386 -3
- package/templates/apps/walgit/src/limits.ts +1 -1
- package/templates/apps/walgit/src/materialize.bench.ts +4 -10
- package/templates/apps/walgit/src/materialize.ts +11 -4
- package/templates/apps/walgit/src/orphans.ts +3 -3
- package/templates/apps/walgit/src/pending.ts +1 -1
- package/templates/apps/walgit/src/private.ts +1 -1
- package/templates/apps/walgit/src/proposals.ts +3 -3
- package/templates/apps/walgit/src/push-cert.ts +10 -1
- package/templates/apps/walgit/src/push.ts +11 -9
- package/templates/apps/walgit/src/reconcile.ts +4 -3
- package/templates/apps/walgit/src/server.ts +29 -2
- package/templates/apps/walgit/src/signers.ts +9 -3
- package/templates/apps/walgit/src/store-env.ts +8 -21
- package/templates/apps/walgit/src/store.ts +12 -241
- package/templates/apps/walgit/src/sync.ts +2 -2
- package/templates/apps/walgit/src/usage.ts +4 -18
- package/templates/apps/walgit/src/verify.ts +2 -2
- package/templates/apps/walgit/src/wal-index.ts +20 -239
- package/templates/apps/walgit/worker/index.ts +278 -1
- package/templates/apps/walgit/worker/mcp.ts +281 -0
- /package/templates/apps/walgit/{src → shared}/keys.ts +0 -0
- /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 {
|
|
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('&', '&')
|
|
936
914
|
.replaceAll('<', '<')
|
|
@@ -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
|
|
1053
|
-
*
|
|
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>'
|
|
@@ -29,8 +29,10 @@ import { CONTENT_SIGNAL } from './robots'
|
|
|
29
29
|
import {
|
|
30
30
|
CHALLENGE_PATH,
|
|
31
31
|
EVENTS_PATH,
|
|
32
|
+
MCP_PATH,
|
|
32
33
|
PROVENANCE_PATH,
|
|
33
34
|
READ_CHALLENGE_NAMESPACE,
|
|
35
|
+
REPOS_PATH,
|
|
34
36
|
SIGNERS_REF,
|
|
35
37
|
} from './protocol'
|
|
36
38
|
|
|
@@ -802,6 +804,45 @@ git clone https://${host}/$NAME.git
|
|
|
802
804
|
\`\`\`
|
|
803
805
|
|
|
804
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
|
|
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
|
+
}
|
|
832
|
+
## Ask this host directly
|
|
833
|
+
|
|
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:
|
|
835
|
+
|
|
836
|
+
\`\`\`sh
|
|
837
|
+
claude mcp add --transport http agentgit https://${host}${MCP_PATH}
|
|
838
|
+
\`\`\`
|
|
839
|
+
|
|
840
|
+
It carries what this host can answer without your disk: \`agentgit_status\` (does a name exist, is it claimed, is it private, what are its refs), \`agentgit_watch\` (block until a ref moves — the handoff primitive, capped at five minutes per call)${
|
|
841
|
+
// Named only where a signature can be made. On a deployment with no nonce
|
|
842
|
+
// seed the tool answers `signer: null` for every ref, which is the honest
|
|
843
|
+
// answer and a useless one to send an agent after.
|
|
844
|
+
caps.signedPushes ? ', `agentgit_provenance` (which key signed the push that moved a ref)' : ''
|
|
845
|
+
}, and this document as the resource \`agentgit://manual\`. Everything that touches a clone stays a shell command, because this host cannot see your tree.
|
|
805
846
|
${signing}${ownership}${privacy}${proposing}${events}
|
|
806
847
|
## If a push is refused
|
|
807
848
|
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Model Context Protocol surface, as a value.
|
|
3
|
+
*
|
|
4
|
+
* An agent finds a tool through the registry its harness already reads. The
|
|
5
|
+
* first version of this shipped as a stdio server an agent's harness spawned
|
|
6
|
+
* (`npx -y @zabaca/agentgit mcp`), and both ways it could fail to start were
|
|
7
|
+
* spawn problems rather than protocol ones: a cold npx cache racing the
|
|
8
|
+
* client's startup timeout, and a workspace checkout resolving the unlinked
|
|
9
|
+
* package. A URL has neither failure mode, so the server moved to the host
|
|
10
|
+
* that already answers every other question about a repository.
|
|
11
|
+
*
|
|
12
|
+
* **It carries only what the host can answer without the agent's disk.** A
|
|
13
|
+
* name's state, a ref moving, who pushed it, and the manual. Accepting a
|
|
14
|
+
* Proposal, configuring a credential helper and fetching into a clone are the
|
|
15
|
+
* skill's, as shell commands — they act on a tree this process cannot see, and
|
|
16
|
+
* a tool that pretended otherwise would be a tool that could only fail.
|
|
17
|
+
*
|
|
18
|
+
* Nothing here reaches anything. Every fact comes through `McpDeps`, whose
|
|
19
|
+
* implementations (`worker/mcp.ts`) are the container round trip and the event
|
|
20
|
+
* socket — which is what lets the whole surface be driven by a real MCP client
|
|
21
|
+
* in `src/mcp.test.ts` with no runtime at all. `shared/`'s one rule holds: this
|
|
22
|
+
* module imports no runtime, and both halves may compile it (docs/adr/0010).
|
|
23
|
+
*
|
|
24
|
+
* **The names are protocol, not an opinion.** `walgit may gain capabilities,
|
|
25
|
+
* never opinions` (CONTEXT.md), and `agentgit_*` plus `agentgit://manual` look
|
|
26
|
+
* like one deployment's brand in the mechanism. They are not configurable for
|
|
27
|
+
* the same reason `refs/walgit/signers` is not: a client TYPES them. A skill,
|
|
28
|
+
* a plugin and a harness config all name a tool, so a deployment that renamed
|
|
29
|
+
* its tools would be a deployment no existing client can call — the opposite
|
|
30
|
+
* of what instance configuration is for. What IS per-deployment — the host,
|
|
31
|
+
* the capabilities, the operator, every limit in the manual — arrives through
|
|
32
|
+
* `McpDeps`, and every refusal below is prefixed `walgit:` like the rest of
|
|
33
|
+
* this package, because the mechanism is what refused.
|
|
34
|
+
*
|
|
35
|
+
* The one rule it DOES own is the refusal: a read that the host refused is
|
|
36
|
+
* reported as a refusal and nothing else. `status` on a Private name a caller
|
|
37
|
+
* cannot prove a key for must not leak refs, existence or the lists alongside
|
|
38
|
+
* the 401 — that is the property `info/refs` has, and the reason both reading
|
|
39
|
+
* tools go through the host's own gated routes rather than a second copy of
|
|
40
|
+
* the gate.
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
|
|
44
|
+
import { z } from 'zod'
|
|
45
|
+
|
|
46
|
+
import type { Capabilities } from './capabilities'
|
|
47
|
+
import { renderLlms } from './llms'
|
|
48
|
+
import type { Operator } from './operator'
|
|
49
|
+
import { REF_NAME, REPO_ID } from './protocol'
|
|
50
|
+
|
|
51
|
+
/** The manual, as a resource a client can read without calling a tool. */
|
|
52
|
+
export const MANUAL_URI = 'agentgit://manual'
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The longest a single `agentgit_watch` holds a request open.
|
|
56
|
+
*
|
|
57
|
+
* A watch is a long poll over the ref-event stream, and the request it holds
|
|
58
|
+
* open is a Worker invocation — so the cap is the HOST's, not the caller's. A
|
|
59
|
+
* client that wants to wait longer calls again, and loses nothing by it:
|
|
60
|
+
* events are latest-state, so the next call's answer is current state rather
|
|
61
|
+
* than a replay it might have missed (docs/adr/0009).
|
|
62
|
+
*/
|
|
63
|
+
export const MAX_WATCH_MS = 5 * 60_000
|
|
64
|
+
|
|
65
|
+
/** What a read of a repository answered, or the refusal the host made. */
|
|
66
|
+
export type Read<T> = ({ ok: true } & T) | { ok: false; status: number; message: string }
|
|
67
|
+
|
|
68
|
+
/** One repository's state, as the host sees it. */
|
|
69
|
+
export interface StatusFacts {
|
|
70
|
+
/** Anything has ever been pushed to this name. */
|
|
71
|
+
exists: boolean
|
|
72
|
+
/** It holds a Signer List, so a stranger's push is refused (docs/adr/0012). */
|
|
73
|
+
claimed: boolean
|
|
74
|
+
/** It holds a Reader List, so a stranger's read is refused (docs/adr/0013). */
|
|
75
|
+
private: boolean
|
|
76
|
+
/** Ref name → sha, as the Index holds it. */
|
|
77
|
+
refs: Record<string, string>
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Who moved each ref, as the host recorded it (docs/adr/0011). */
|
|
81
|
+
export type ProvenanceFacts = Record<string, { signer: string; ts: string }>
|
|
82
|
+
|
|
83
|
+
/** What a watch saw: a ref that moved, or the deadline. */
|
|
84
|
+
export type WatchOutcome = { timedOut: false; ref: string; sha: string | null } | { timedOut: true }
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Everything the tools touch that this module cannot compute.
|
|
88
|
+
*
|
|
89
|
+
* `status` and `provenance` return a `Read`, never throw, because a refusal is
|
|
90
|
+
* an ANSWER the host made and has to survive the trip verbatim — turning a 401
|
|
91
|
+
* into an exception here is how a refusal becomes "something went wrong".
|
|
92
|
+
* `watch` may throw: an event stream that cannot be reached is not a verdict
|
|
93
|
+
* about the repository, and reporting it as one would be a lie.
|
|
94
|
+
*/
|
|
95
|
+
export interface McpDeps {
|
|
96
|
+
/** Reported in the handshake, so a client can name what it is talking to. */
|
|
97
|
+
version: string
|
|
98
|
+
/** This deployment's hostname, for the manual and for naming it in an error. */
|
|
99
|
+
host: string
|
|
100
|
+
/** What this deployment offers (`shared/capabilities.ts`). */
|
|
101
|
+
caps: Capabilities
|
|
102
|
+
/** Who runs it (`shared/operator.ts`), or nobody. */
|
|
103
|
+
operator: Operator | null
|
|
104
|
+
status(name: string): Promise<Read<StatusFacts>>
|
|
105
|
+
provenance(name: string): Promise<Read<{ repo: string; provenance: ProvenanceFacts }>>
|
|
106
|
+
watch(name: string, ref: string | null, timeoutMs: number): Promise<WatchOutcome>
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
interface ToolResult {
|
|
110
|
+
content: { type: 'text'; text: string }[]
|
|
111
|
+
isError?: boolean
|
|
112
|
+
[key: string]: unknown
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const json = (value: unknown): ToolResult => ({
|
|
116
|
+
content: [{ type: 'text', text: JSON.stringify(value, null, 2) }],
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
const refuse = (message: string): ToolResult => ({
|
|
120
|
+
content: [{ type: 'text', text: message }],
|
|
121
|
+
isError: true,
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The name grammar, applied before the host is asked.
|
|
126
|
+
*
|
|
127
|
+
* The same `REPO_ID` every other entry point applies (`src/repo.ts`,
|
|
128
|
+
* `shared/events.ts`): a name one surface accepts and another rejects is a
|
|
129
|
+
* repository half the service can see. Refused here rather than forwarded so a
|
|
130
|
+
* path-shaped name never becomes a request at all.
|
|
131
|
+
*/
|
|
132
|
+
function badName(name: string): string | null {
|
|
133
|
+
return REPO_ID.test(name)
|
|
134
|
+
? null
|
|
135
|
+
: `walgit: "${name}" is not a repository name here — one flat segment of letters, digits, dot, dash or underscore, starting with a letter or a digit.`
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* The MCP server this host serves, wired over `deps`.
|
|
140
|
+
*
|
|
141
|
+
* Returned unconnected: the transport is the caller's, which is what lets one
|
|
142
|
+
* function serve a Streamable HTTP request in the Worker and an in-memory pair
|
|
143
|
+
* in a test. The descriptions are written for the model that reads the tool
|
|
144
|
+
* list and nothing else — that list is the only documentation an MCP client
|
|
145
|
+
* sees before it calls something.
|
|
146
|
+
*/
|
|
147
|
+
export function createAgentgitMcp(deps: McpDeps): McpServer {
|
|
148
|
+
const server = new McpServer({ name: 'agentgit', version: deps.version })
|
|
149
|
+
|
|
150
|
+
const name = z
|
|
151
|
+
.string()
|
|
152
|
+
.describe('The repository name — the `<name>` in `https://host/<name>.git`.')
|
|
153
|
+
|
|
154
|
+
server.registerTool(
|
|
155
|
+
'agentgit_status',
|
|
156
|
+
{
|
|
157
|
+
description:
|
|
158
|
+
"What the host knows about a repository name: whether anything has been pushed to it, whether it holds a Signer List (claimed, so a stranger cannot push), whether it holds a Reader List (private, so a stranger cannot read), and every ref with its sha. Reads only; creates nothing. A free name answers exists: false rather than an error, so this is how you check before claiming one. signedPushes is the deployment's, not this name's: it says whether a push to anything here can be signed at all.",
|
|
159
|
+
inputSchema: { name },
|
|
160
|
+
},
|
|
161
|
+
async (input): Promise<ToolResult> => {
|
|
162
|
+
const bad = badName(input.name)
|
|
163
|
+
if (bad) return refuse(bad)
|
|
164
|
+
const read = await deps.status(input.name)
|
|
165
|
+
if (!read.ok) return refuse(read.message)
|
|
166
|
+
return json({
|
|
167
|
+
name: input.name,
|
|
168
|
+
exists: read.exists,
|
|
169
|
+
claimed: read.claimed,
|
|
170
|
+
private: read.private,
|
|
171
|
+
// An array rather than the map the Index holds: a tool result is read
|
|
172
|
+
// by a model, and a list of {ref, sha} survives truncation in a way an
|
|
173
|
+
// object whose KEYS are the interesting part does not.
|
|
174
|
+
refs: Object.entries(read.refs).map(([ref, sha]) => ({ ref, sha })),
|
|
175
|
+
signedPushes: deps.caps.signedPushes,
|
|
176
|
+
})
|
|
177
|
+
},
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
server.registerTool(
|
|
181
|
+
'agentgit_watch',
|
|
182
|
+
{
|
|
183
|
+
description:
|
|
184
|
+
'Block until a ref in a repository moves, then say what it is now. This is the handoff primitive: call it to wait for the other agent to push. Answers { ref, sha } when one moves, or { timedOut: true } when none did before the deadline — which is an answer, not a failure, so call it again to keep waiting. It touches no clone: fetching what it reports is yours to do.',
|
|
185
|
+
inputSchema: {
|
|
186
|
+
name,
|
|
187
|
+
ref: z
|
|
188
|
+
.string()
|
|
189
|
+
.optional()
|
|
190
|
+
.describe('A full ref name, e.g. refs/heads/main. Omit to watch every ref.'),
|
|
191
|
+
timeoutMs: z
|
|
192
|
+
.number()
|
|
193
|
+
.int()
|
|
194
|
+
// Positive, so a `0` or a negative does not become a watch that
|
|
195
|
+
// answers `{ timedOut: true }` before the socket is even open —
|
|
196
|
+
// which reads as "nothing moved" and is a claim about the repository
|
|
197
|
+
// rather than about the argument.
|
|
198
|
+
.positive()
|
|
199
|
+
.optional()
|
|
200
|
+
.describe(`How long to wait, capped at ${MAX_WATCH_MS}ms by the host.`),
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
async (input): Promise<ToolResult> => {
|
|
204
|
+
const bad = badName(input.name)
|
|
205
|
+
if (bad) return refuse(bad)
|
|
206
|
+
const ref = input.ref ?? null
|
|
207
|
+
if (ref !== null && !REF_NAME.test(ref)) {
|
|
208
|
+
return refuse(`walgit: "${ref}" is not a full ref name — refs/heads/main, not main.`)
|
|
209
|
+
}
|
|
210
|
+
// The host's cap, not the caller's (see `MAX_WATCH_MS`).
|
|
211
|
+
const timeoutMs = Math.min(input.timeoutMs ?? MAX_WATCH_MS, MAX_WATCH_MS)
|
|
212
|
+
let outcome: WatchOutcome
|
|
213
|
+
try {
|
|
214
|
+
outcome = await deps.watch(input.name, ref, timeoutMs)
|
|
215
|
+
} catch (error) {
|
|
216
|
+
// Named rather than hung, and the HOST is named: a watch that cannot
|
|
217
|
+
// reach the event stream is a fact about this deployment, and a client
|
|
218
|
+
// told only "failed" has nothing to retry against.
|
|
219
|
+
return refuse(
|
|
220
|
+
`walgit: could not watch ${input.name} on ${deps.host}: ${
|
|
221
|
+
error instanceof Error ? error.message : String(error)
|
|
222
|
+
}`,
|
|
223
|
+
)
|
|
224
|
+
}
|
|
225
|
+
return json({
|
|
226
|
+
timedOut: outcome.timedOut,
|
|
227
|
+
repo: input.name,
|
|
228
|
+
// Spelled out rather than left off, because shape alone is ambiguous: a
|
|
229
|
+
// deleted ref carries no sha, and `{ sha: null }` on its own reads as a
|
|
230
|
+
// handoff that landed.
|
|
231
|
+
ref: outcome.timedOut ? null : outcome.ref,
|
|
232
|
+
sha: outcome.timedOut ? null : outcome.sha,
|
|
233
|
+
})
|
|
234
|
+
},
|
|
235
|
+
)
|
|
236
|
+
|
|
237
|
+
server.registerTool(
|
|
238
|
+
'agentgit_provenance',
|
|
239
|
+
{
|
|
240
|
+
description:
|
|
241
|
+
'Who pushed a ref: the SSH key fingerprint that signed the push, and when it landed. A key, never a user — there are no accounts here. A ref nobody signed answers signer: null, which is the ordinary answer, because signing is opt-in.',
|
|
242
|
+
inputSchema: {
|
|
243
|
+
name,
|
|
244
|
+
ref: z.string().describe('A full ref name, e.g. refs/heads/main.'),
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
async (input): Promise<ToolResult> => {
|
|
248
|
+
const bad = badName(input.name)
|
|
249
|
+
if (bad) return refuse(bad)
|
|
250
|
+
const read = await deps.provenance(input.name)
|
|
251
|
+
if (!read.ok) return refuse(read.message)
|
|
252
|
+
const entry = read.provenance[input.ref]
|
|
253
|
+
return json({
|
|
254
|
+
repo: input.name,
|
|
255
|
+
ref: input.ref,
|
|
256
|
+
signer: entry?.signer ?? null,
|
|
257
|
+
ts: entry?.ts ?? null,
|
|
258
|
+
})
|
|
259
|
+
},
|
|
260
|
+
)
|
|
261
|
+
|
|
262
|
+
server.registerResource(
|
|
263
|
+
'manual',
|
|
264
|
+
MANUAL_URI,
|
|
265
|
+
{
|
|
266
|
+
title: 'agentgit manual',
|
|
267
|
+
description:
|
|
268
|
+
"This host's own /llms.txt — push-to-create, claiming a name, Reader Lists, Proposals and the event stream, with every limit this deployment actually enforces.",
|
|
269
|
+
mimeType: 'text/markdown',
|
|
270
|
+
},
|
|
271
|
+
async (uri) => ({
|
|
272
|
+
contents: [
|
|
273
|
+
{
|
|
274
|
+
uri: uri.href,
|
|
275
|
+
mimeType: 'text/markdown',
|
|
276
|
+
// The same renderer the `/llms.txt` route calls, over the same
|
|
277
|
+
// capabilities: one document, two ways to it, and no second copy to
|
|
278
|
+
// promise a limit this deployment does not have.
|
|
279
|
+
text: renderLlms(deps.host, deps.caps, deps.operator),
|
|
280
|
+
},
|
|
281
|
+
],
|
|
282
|
+
}),
|
|
283
|
+
)
|
|
284
|
+
|
|
285
|
+
return server
|
|
286
|
+
}
|
|
@@ -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
|
+
}
|