@comforge/mcp 0.1.0 → 0.3.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/README.md +114 -23
- package/dist/host.d.ts +44 -8
- package/dist/host.js +54 -0
- package/dist/hostServices.d.ts +133 -0
- package/dist/hostServices.js +240 -0
- package/dist/http.d.ts +8 -1
- package/dist/http.js +4 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/results.d.ts +4 -0
- package/dist/results.js +8 -1
- package/dist/server.d.ts +12 -2
- package/dist/server.js +27 -10
- package/dist/tools.d.ts +16 -2
- package/dist/tools.js +146 -26
- package/llms.txt +11 -6
- package/package.json +8 -3
package/README.md
CHANGED
|
@@ -5,9 +5,9 @@ over the [Model Context Protocol](https://modelcontextprotocol.io). It calls the
|
|
|
5
5
|
lifecycle services and `@comforge/github`'s reads in process. It never shells out to `comforge`,
|
|
6
6
|
parses its output, or keeps a second copy of its governance, state or authorization.
|
|
7
7
|
|
|
8
|
-
This README describes the source in this repository
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
This README describes the source in this repository, which `package.json` versions as 0.2.0
|
|
9
|
+
(Node >= 22.18 or Bun >= 1.4, and git). Anything marked "unreleased" is in the source but not in
|
|
10
|
+
a published release; the CHANGELOG's "Unreleased" entry lists it.
|
|
11
11
|
|
|
12
12
|
It never merges, deploys, approves, requests a review, closes an issue or runs an arbitrary command.
|
|
13
13
|
|
|
@@ -21,20 +21,20 @@ It never merges, deploys, approves, requests a review, closes an issue or runs a
|
|
|
21
21
|
| `comforge_evidence` | all | one section of an initiative's record (validations, publications, completions, revisions, operations), newest first |
|
|
22
22
|
| `comforge_issue` | all | an issue and whether its specification still verifies, or (without `number`) a page of issues; text is untrusted |
|
|
23
23
|
| `comforge_pr` | all | a pull request, optionally its checks, changed files and native GitHub reviews |
|
|
24
|
-
| `comforge_prs`
|
|
25
|
-
| `comforge_comments`
|
|
26
|
-
| `comforge_reviews`
|
|
27
|
-
| `comforge_artifact`
|
|
24
|
+
| `comforge_prs` | all | a page of the repository's pull requests (`state` open, closed or all) |
|
|
25
|
+
| `comforge_comments` | all | a page of an issue's or pull request's comments; text is untrusted |
|
|
26
|
+
| `comforge_reviews` | all | a pull request's advisory reviews, with integrity and provenance |
|
|
27
|
+
| `comforge_artifact` | all | one file at an exact commit, only under the repository's trusted `artifacts.readable` paths |
|
|
28
28
|
| `comforge_start` | executor | verifies the specification, creates the isolated checkout |
|
|
29
29
|
| `comforge_revise` | executor | reuses or re-creates a pull request's checkout and syncs it |
|
|
30
30
|
| `comforge_verify` | executor | trusted validation of an exact commit in a clean snapshot |
|
|
31
31
|
| `comforge_deliver` | publisher | push, one pull request, one completion record, read back; `execute:false` plans |
|
|
32
32
|
| `comforge_recover` | publisher | settles uncertain writes from GitHub; `execute:false` reports |
|
|
33
|
-
| `comforge_cleanup` | publisher | plans a cleanup; executing needs operator mode or a cleanup-scoped command grant (
|
|
34
|
-
| `comforge_publish_review`
|
|
35
|
-
| `comforge_publish_specification`
|
|
33
|
+
| `comforge_cleanup` | publisher | plans a cleanup; executing needs operator mode or a cleanup-scoped command grant (see Signed command scope) |
|
|
34
|
+
| `comforge_publish_review` | reviewer | publishes one advisory review comment bound to the reviewed head; `execute:false` plans |
|
|
35
|
+
| `comforge_publish_specification` | publisher | publishes a specification issue (idempotent, read back); `execute:false` plans |
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
There are 18 tools. Advisory reviews are marked comments, never native
|
|
38
38
|
reviews or approvals.
|
|
39
39
|
The four profiles are `read-only`, `reviewer` (adds patches and full review bodies to reads),
|
|
40
40
|
`executor` and `publisher`. A principal can hold several. A principal discovers only the tools its
|
|
@@ -64,10 +64,10 @@ and secret-like text are redacted. Over HTTP, host paths are replaced by `<host>
|
|
|
64
64
|
`security.authorization`: owner grants or operator mode. A caller passes an owner-signed grant
|
|
65
65
|
envelope in `grant`, and the CLI verifies it against the trusted keys, ticket, specification,
|
|
66
66
|
head and branch. The server cannot authorize anything itself.
|
|
67
|
-
- **Cleanup needs its own authority
|
|
67
|
+
- **Cleanup needs its own authority.** `comforge_cleanup` with `execute:true` runs
|
|
68
68
|
under operator mode, or under an owner grant that scopes the command to cleanup of the merged
|
|
69
69
|
pull request (`authorizeCommand` in [`@comforge/core`](https://www.npmjs.com/package/@comforge/core)). A grant signed
|
|
70
|
-
to publish is never valid for cleanup. With
|
|
70
|
+
to publish is never valid for cleanup. With an `@comforge/core` 0.1.x grant format,
|
|
71
71
|
which cannot name a command, `execute:true` is refused with `command-scope-unsupported`; run
|
|
72
72
|
cleanup through the `comforge` CLI.
|
|
73
73
|
- **Repository isolation.** Each principal has an allowlist. An unknown or foreign id gets the same
|
|
@@ -91,11 +91,11 @@ and secret-like text are redacted. Over HTTP, host paths are replaced by `<host>
|
|
|
91
91
|
host passes an `IsolationProvider`. That is the host's attestation that its runtime runs every
|
|
92
92
|
command inside an isolation boundary it operates. A flag cannot stand in for it. This package
|
|
93
93
|
implements no sandbox and claims none.
|
|
94
|
-
- **Remote publication is not remote execution
|
|
94
|
+
- **Remote publication is not remote execution.** The reads, `comforge_publish_review`
|
|
95
95
|
and `comforge_publish_specification` run no repository command, so over HTTP they are available
|
|
96
96
|
without an `IsolationProvider`. They still need the principal's profile and the repository's
|
|
97
97
|
trusted authorization. Only the executing tools need the provider.
|
|
98
|
-
- **Host policy
|
|
98
|
+
- **Host policy.** `HostServices.hostPolicy(binding)` hands the CLI's `HostPolicy`
|
|
99
99
|
(checkout parent, worker preparation and removal, validation) to the lifecycle for one
|
|
100
100
|
repository. It is host code; no tool input reaches it.
|
|
101
101
|
- **Cancellation.** It is checked between the CLI's stages, never in the middle of a write. An
|
|
@@ -121,7 +121,7 @@ and secret-like text are redacted. Over HTTP, host paths are replaced by `<host>
|
|
|
121
121
|
- `compactDiscovery: true` (or `--compact-discovery`) leaves the output schemas out of
|
|
122
122
|
`tools/list`. Measured: 39-45% fewer discovery tokens (17 tools: 11,019 to 6,759). The results
|
|
123
123
|
keep the same versioned envelope.
|
|
124
|
-
- `textContent: "summary"` (
|
|
124
|
+
- `textContent: "summary"` (`--summary-text`; default `"full"`) makes each result's text
|
|
125
125
|
content one summary line and leaves `structuredContent` unchanged. Measured: a model that is
|
|
126
126
|
given `structuredContent` reads the same tokens; the wire shrinks to about the structured size.
|
|
127
127
|
A client that reads only text sees just the summary line, so it is opt-in.
|
|
@@ -132,17 +132,106 @@ is the smallest equivalent form (pull requests 1,191 vs CLI 1,617 vs raw GitHub
|
|
|
132
132
|
is the largest fixed cost of an MCP session. These are per-call counts; no end-to-end saving is
|
|
133
133
|
claimed.
|
|
134
134
|
|
|
135
|
-
##
|
|
135
|
+
## Repositories without a checkout
|
|
136
|
+
|
|
137
|
+
A host that has no checkout (a web app that answers MCP over HTTP) can still serve the GitHub
|
|
138
|
+
evidence reads. `createComforgeMcpServer` accepts `remoteRepositories` and `remote` since 0.2.0;
|
|
139
|
+
**unreleased:** `createHttpHandler` forwards them too. The reads are: `comforge_capabilities`, `comforge_issue`, `comforge_pr`, `comforge_prs`,
|
|
140
|
+
`comforge_comments`, `comforge_reviews` and `comforge_artifact` (`REMOTE_READ_TOOLS`).
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
createHttpHandler({
|
|
144
|
+
repositories: [],
|
|
145
|
+
remoteRepositories: [{ id: "widgets", owner: "acme", name: "widgets", configSha256: "<optional pin>" }],
|
|
146
|
+
remote: {
|
|
147
|
+
trustedConfig: async (b) => ({ revision, text, observedAt }), // host code: the base head and its comforge.config.json
|
|
148
|
+
credentials: (b, principal) => serverOwnedToken,
|
|
149
|
+
configMaxAgeMs: 600_000,
|
|
150
|
+
},
|
|
151
|
+
host: { runtime: () => { throw new Error("no runtime"); } },
|
|
152
|
+
// authenticate, allowedHosts, ... as above
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
- The host supplies the trusted configuration; a caller never can. The server refuses a
|
|
157
|
+
snapshot whose revision is not a full SHA (`config-invalid`), that is older than
|
|
158
|
+
`configMaxAgeMs` or from the future (`config-stale`, retryable), that differs from the pin or
|
|
159
|
+
from GitHub's bytes at that revision read with the server's credential (`config-mismatch`), or
|
|
160
|
+
that names another repository (`repository-mismatch`). A source that throws is `config-missing`.
|
|
161
|
+
- Whether the revision is the base branch head is the host's statement; the server verifies the
|
|
162
|
+
bytes at that revision, not branch membership.
|
|
163
|
+
- Every other tool is refused for such a repository with `checkout-required`, with or without an
|
|
164
|
+
`IsolationProvider`: loading a configuration never enables execution or writes. A server with
|
|
165
|
+
only checkout-free repositories does not list those tools at all.
|
|
166
|
+
|
|
167
|
+
Over HTTP the handler keeps every check above for these repositories: authentication (401 and
|
|
168
|
+
the challenge), the `Host` and `Origin` allowlists, profile checks on every call, per-principal
|
|
169
|
+
sessions (a changed principal ends the session) and a refusal of executing tools. Constructing the
|
|
170
|
+
handler with `remoteRepositories` and no `remote` throws.
|
|
171
|
+
|
|
172
|
+
## Host services
|
|
173
|
+
|
|
174
|
+
`@comforge/mcp/host` is for an application that serves its own tools, sessions and storage and
|
|
175
|
+
needs the same engineering services in its own process, without a subprocess or a loopback HTTP
|
|
176
|
+
call. The MCP server of this package uses the same services for its evidence reads.
|
|
177
|
+
|
|
178
|
+
```ts
|
|
179
|
+
import { createEngineeringHost, sqliteOperationStore } from "@comforge/mcp/host";
|
|
180
|
+
|
|
181
|
+
const host = createEngineeringHost({
|
|
182
|
+
repository: { owner: "acme", name: "widgets" },
|
|
183
|
+
transport: myReadTransport, // { get(path, query) }: your client, credential and limits
|
|
184
|
+
});
|
|
185
|
+
await host.evidence.getPullRequest(42); // exact-commit, complete-or-refuse reads
|
|
186
|
+
await host.advisoryReviews(reviewProtocol, { number: 42, headSha }, { trustedAuthors });
|
|
187
|
+
await host.trustedConfig({ branch: "main" }); // bytes at one immutable revision
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
| You inject | The services never take it from a caller |
|
|
191
|
+
|---|---|
|
|
192
|
+
| `repository` | one `owner/name`; every request stays inside it |
|
|
193
|
+
| `transport` | the read path: credential, limits, error mapping |
|
|
194
|
+
| `writes` (optional) | `token`, `capabilities`, `authorize(descriptor)`, `operations` store, `clock` |
|
|
195
|
+
|
|
196
|
+
There are two profiles. Without `writes` the host is `read-only`: `writes.prepare`, `execute`,
|
|
197
|
+
`reconcile` and `abandon` refuse with `capability-denied` before a request exists. This is the
|
|
198
|
+
profile of a checkout-free network host. With `writes` the host is `governed-writes`, for an
|
|
199
|
+
embedded trusted host: each write is sent once per operation id, only within `capabilities`, only
|
|
200
|
+
after your `authorize` allows that exact descriptor, and an uncertain write is settled by
|
|
201
|
+
`reconcile` against GitHub, never sent again. Your application keeps OAuth, principals, sessions,
|
|
202
|
+
admission and the decision to write at all. The services add no tool, no remote execution and no
|
|
203
|
+
lifecycle write.
|
|
204
|
+
|
|
205
|
+
`sqliteOperationStore({ database, repository, table?, repositoryColumn?, kindAndStateColumns? })`
|
|
206
|
+
is the durable operation store over your own SQLite handle (`prepare(sql)`): insert-if-absent,
|
|
207
|
+
version-checked replace, a finished record stored with a digest in place of its body, and a
|
|
208
|
+
malformed, foreign or state-inconsistent record refused. It adopts an existing table as it is;
|
|
209
|
+
`inspect()` and `compact({ before, limit })` are the only retention operations and never remove
|
|
210
|
+
an identity. Every refusal is a `HostServiceError` with a stable `code`.
|
|
211
|
+
|
|
212
|
+
## Signed command scope
|
|
136
213
|
|
|
137
214
|
`@comforge/core` adds an optional signed `commands` list to a grant, and `@comforge/cli` checks it:
|
|
138
215
|
- a grant **without** the field keeps its meaning for publish, complete, deliver and recover;
|
|
139
216
|
- such a grant is **never** valid for cleanup;
|
|
140
217
|
- a grant **with** the field is valid only for the commands it lists.
|
|
141
218
|
|
|
142
|
-
`comforge_cleanup` executes under operator mode or such a scoped grant
|
|
143
|
-
released and pinned. The grant format is documented in the
|
|
219
|
+
`comforge_cleanup` executes under operator mode or such a scoped grant. The grant format is documented in the
|
|
144
220
|
[`@comforge/core` README](https://www.npmjs.com/package/@comforge/core).
|
|
145
221
|
|
|
222
|
+
## Quick start
|
|
223
|
+
|
|
224
|
+
Install one interface and pin it exactly: `npm install --save-exact @comforge/mcp@0.2.0`. It
|
|
225
|
+
brings `@comforge/cli`, `@comforge/core`, `@comforge/github` and `@comforge/ledger`; do not install
|
|
226
|
+
them separately for this. Use either `npx comforge-mcp` (stdio, below) or embed `createHttpHandler`, not both.
|
|
227
|
+
You need a repository whose base branch carries `comforge.config.json`, a GitHub credential from
|
|
228
|
+
your host or the repository's `security.credentials` source (never a tool input) and, for any
|
|
229
|
+
write, the trusted `security.authorization` (an owner grant envelope or operator mode).
|
|
230
|
+
|
|
231
|
+
Measured on one machine (the source repository's onboarding evidence): the MCP example installed in 0.8 s and ran its first verified
|
|
232
|
+
workflow in 0.8 s on Node and 0.5 s on Bun; the CLI example installed in 1.5 s and ran in 2.6 s
|
|
233
|
+
on Node and 1.2 s on Bun. Human setup time (configuration, grants, credentials) was not measured.
|
|
234
|
+
|
|
146
235
|
## Run locally
|
|
147
236
|
|
|
148
237
|
```sh
|
|
@@ -174,9 +263,11 @@ const handler = createHttpHandler({
|
|
|
174
263
|
- **Repository commands block their process.** They run synchronously through the CLI's runtime,
|
|
175
264
|
so a long validation holds the server for its duration. That is one more reason remote
|
|
176
265
|
execution needs a host-operated worker.
|
|
177
|
-
- **Advisory reviews need
|
|
178
|
-
|
|
179
|
-
only and says so.
|
|
180
|
-
|
|
266
|
+
- **Advisory reviews need configuration.** `comforge_reviews` and `comforge_publish_review` need
|
|
267
|
+
the repository's trusted `review` configuration section; without it `comforge_pr` returns native
|
|
268
|
+
GitHub reviews only and says so.
|
|
269
|
+
- **Recovery and concurrency.** Initiative state is a file ledger with compare-and-swap, so a
|
|
270
|
+
concurrent writer on the same checkout is refused rather than merged. An interrupted delivery
|
|
271
|
+
is settled by `comforge_recover`, which reads GitHub and never replays a write blindly.
|
|
181
272
|
- **Limits are per process.** Rate and session limits live in one process, so run one instance
|
|
182
273
|
per limit domain.
|
package/dist/host.d.ts
CHANGED
|
@@ -1,11 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* What the host gives the server, and the repository allowlist.
|
|
3
|
-
*
|
|
4
|
-
* Trust boundary: the host owns credentials, the runtime that runs repository commands, the
|
|
5
|
-
* state store and the principal. A caller supplies none of them: it names an allowlisted
|
|
6
|
-
* repository id, and the server resolves everything else from the host and from the
|
|
7
|
-
* repository's trusted configuration (its base branch, never the working tree).
|
|
8
|
-
*/
|
|
9
1
|
import { type ComforgeConfig, type CredentialProvider, type HostPolicy, type Runtime, type StateStore } from "@comforge/cli";
|
|
10
2
|
import type { Principal } from "./contracts.ts";
|
|
11
3
|
export interface RepositoryBinding {
|
|
@@ -66,3 +58,47 @@ export declare const repositoryOf: (b: RepositoryBinding) => {
|
|
|
66
58
|
* The working tree's copy is ignored. It must name the binding's repository.
|
|
67
59
|
*/
|
|
68
60
|
export declare function trustedConfig(rt: Runtime, binding: RepositoryBinding): ComforgeConfig;
|
|
61
|
+
/**
|
|
62
|
+
* An allowlisted repository the host serves without a local checkout: GitHub evidence reads
|
|
63
|
+
* only (issue, pull request, pull requests, comments, advisory reviews, artifacts). Lifecycle,
|
|
64
|
+
* state and executing tools are refused for it (`checkout-required`): they need a checkout and,
|
|
65
|
+
* remotely, isolation, which loading a configuration never provides.
|
|
66
|
+
*/
|
|
67
|
+
export interface RemoteRepositoryBinding {
|
|
68
|
+
id: string;
|
|
69
|
+
owner: string;
|
|
70
|
+
name: string;
|
|
71
|
+
/**
|
|
72
|
+
* sha256 (hex) of the trusted `comforge.config.json` bytes the host pins. When set, any other
|
|
73
|
+
* configuration is refused (`config-mismatch`), even one committed at the base.
|
|
74
|
+
*/
|
|
75
|
+
configSha256?: string;
|
|
76
|
+
}
|
|
77
|
+
/** The trusted configuration as the host observed it: bytes at one immutable base revision. */
|
|
78
|
+
export interface TrustedConfigSnapshot {
|
|
79
|
+
/** Full 40-hex commit of the base branch the host trusts. */
|
|
80
|
+
revision: string;
|
|
81
|
+
/** The exact `comforge.config.json` text at `revision`. */
|
|
82
|
+
text: string;
|
|
83
|
+
/** When the host observed `revision` as the base head (ms since epoch). */
|
|
84
|
+
observedAt: number;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Host code for checkout-free repositories. The server never takes a configuration, revision,
|
|
88
|
+
* URL or credential from a caller: the host supplies them, and the server checks the snapshot
|
|
89
|
+
* against GitHub (the same bytes at that revision), its pin, its age and its repository.
|
|
90
|
+
*/
|
|
91
|
+
export interface RemoteHost {
|
|
92
|
+
trustedConfig(binding: RemoteRepositoryBinding): TrustedConfigSnapshot | Promise<TrustedConfigSnapshot>;
|
|
93
|
+
/** Server-owned GitHub token for the binding; never returned to a caller. */
|
|
94
|
+
credentials(binding: RemoteRepositoryBinding, principal: Principal): string | undefined;
|
|
95
|
+
/** Oldest snapshot accepted; default 10 minutes. */
|
|
96
|
+
configMaxAgeMs?: number;
|
|
97
|
+
fetchImpl?: typeof fetch;
|
|
98
|
+
now?(): number;
|
|
99
|
+
}
|
|
100
|
+
/** Refuses malformed remote bindings, and an id used by a local and a remote binding alike. */
|
|
101
|
+
export declare function assertRemoteBindings(remote: readonly RemoteRepositoryBinding[], local: readonly RepositoryBinding[]): void;
|
|
102
|
+
export declare function resolveRemoteBinding(remote: readonly RemoteRepositoryBinding[], principal: Principal, id: string): RemoteRepositoryBinding | null;
|
|
103
|
+
/** The snapshot's own checks: revision shape, age, pin, parse and repository identity. */
|
|
104
|
+
export declare function checkSnapshot(binding: RemoteRepositoryBinding, snapshot: TrustedConfigSnapshot, now: number, maxAgeMs: number): ComforgeConfig;
|
package/dist/host.js
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* repository id, and the server resolves everything else from the host and from the
|
|
7
7
|
* repository's trusted configuration (its base branch, never the working tree).
|
|
8
8
|
*/
|
|
9
|
+
import { createHash } from "node:crypto";
|
|
9
10
|
import { parseConfig, } from "@comforge/cli";
|
|
10
11
|
import { ToolRefusal } from "./results.js";
|
|
11
12
|
const ID = /^[a-z0-9][a-z0-9._-]{0,63}$/;
|
|
@@ -76,3 +77,56 @@ export function trustedConfig(rt, binding) {
|
|
|
76
77
|
});
|
|
77
78
|
return config;
|
|
78
79
|
}
|
|
80
|
+
const SHA40 = /^[0-9a-f]{40}$/;
|
|
81
|
+
const HEX64 = /^[0-9a-f]{64}$/;
|
|
82
|
+
/** Refuses malformed remote bindings, and an id used by a local and a remote binding alike. */
|
|
83
|
+
export function assertRemoteBindings(remote, local) {
|
|
84
|
+
const seen = new Set(local.map((b) => b.id));
|
|
85
|
+
for (const b of remote) {
|
|
86
|
+
if (!ID.test(b.id))
|
|
87
|
+
throw new Error(`repository id ${JSON.stringify(b.id)} is malformed`);
|
|
88
|
+
if (seen.has(b.id))
|
|
89
|
+
throw new Error(`repository id ${b.id} is listed twice`);
|
|
90
|
+
seen.add(b.id);
|
|
91
|
+
if (!/^[A-Za-z0-9_.-]{1,100}$/.test(b.owner) || !/^[A-Za-z0-9_.-]{1,100}$/.test(b.name))
|
|
92
|
+
throw new Error(`repository ${b.id}: owner and name must be GitHub names`);
|
|
93
|
+
if (b.configSha256 !== undefined && !HEX64.test(b.configSha256))
|
|
94
|
+
throw new Error(`repository ${b.id}: configSha256 must be 64 lowercase hex characters`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
export function resolveRemoteBinding(remote, principal, id) {
|
|
98
|
+
const binding = remote.find((b) => b.id === id);
|
|
99
|
+
const allowed = principal.repositories === "*" || principal.repositories.includes(id);
|
|
100
|
+
return binding && allowed ? binding : null;
|
|
101
|
+
}
|
|
102
|
+
/** The snapshot's own checks: revision shape, age, pin, parse and repository identity. */
|
|
103
|
+
export function checkSnapshot(binding, snapshot, now, maxAgeMs) {
|
|
104
|
+
const detail = { repository: binding.id };
|
|
105
|
+
if (!snapshot || typeof snapshot.text !== "string" || typeof snapshot.revision !== "string")
|
|
106
|
+
throw new ToolRefusal("config-missing", "the host supplied no trusted configuration", 2, detail);
|
|
107
|
+
if (!SHA40.test(snapshot.revision))
|
|
108
|
+
throw new ToolRefusal("config-invalid", "the trusted revision is not a full commit SHA", 2, detail);
|
|
109
|
+
if (!Number.isFinite(snapshot.observedAt) ||
|
|
110
|
+
snapshot.observedAt > now + 60_000 ||
|
|
111
|
+
now - snapshot.observedAt > maxAgeMs)
|
|
112
|
+
throw new ToolRefusal("config-stale", "the trusted configuration snapshot is too old", 3, {
|
|
113
|
+
...detail,
|
|
114
|
+
maxAgeMs,
|
|
115
|
+
});
|
|
116
|
+
if (binding.configSha256 !== undefined && sha256(snapshot.text) !== binding.configSha256)
|
|
117
|
+
throw new ToolRefusal("config-mismatch", "the trusted configuration is not the pinned one", 2, detail);
|
|
118
|
+
let config;
|
|
119
|
+
try {
|
|
120
|
+
config = parseConfig(snapshot.text, "comforge.config.json");
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
throw new ToolRefusal("config-invalid", "the trusted comforge.config.json is invalid", 2, {
|
|
124
|
+
...detail,
|
|
125
|
+
reason: error instanceof Error ? error.message.slice(0, 500) : "invalid",
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
if (config.repository.owner !== binding.owner || config.repository.name !== binding.name)
|
|
129
|
+
throw new ToolRefusal("repository-mismatch", "the trusted configuration names another repository", 1, detail);
|
|
130
|
+
return config;
|
|
131
|
+
}
|
|
132
|
+
const sha256 = (text) => createHash("sha256").update(text, "utf8").digest("hex");
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { type AdvisoryReview, type AuthorizationDecision, type EvidenceLimits, type FetchTransportOptions, type FileBytesEvidence, GitHubAdapterError, type GitHubEvidence, type GitHubTransport, type IssueCommentEvidence, listAdvisoryReviews, type OperationDescriptor, type OperationLedger, type OperationRecord, type UntrustedText, type WriteCapability, type WriteOperation, type WriteOutcome, type WrittenResource } from "@comforge/github";
|
|
2
|
+
import { type PreparedDatabase, type RetentionReport } from "@comforge/ledger/sqlite";
|
|
3
|
+
import type { TrustedConfigSnapshot } from "./host.ts";
|
|
4
|
+
export declare const HOST_SERVICES_SCHEMA: "comforge-host-services/v1";
|
|
5
|
+
/** One GitHub read: `get(path, query)`. The host's own client, or `fetchTransport`. */
|
|
6
|
+
export type HostTransport = GitHubTransport;
|
|
7
|
+
/** Exact-commit, complete-or-refuse reads of one repository. */
|
|
8
|
+
export type HostEvidence = GitHubEvidence;
|
|
9
|
+
/** The host's SQLite handle: `prepare(sql)` returning a statement with `run`, `get` and `all`. */
|
|
10
|
+
export type HostDatabase = PreparedDatabase;
|
|
11
|
+
/** Durable operation records with insert-if-absent and version-checked replace. */
|
|
12
|
+
export type OperationStore = OperationLedger;
|
|
13
|
+
/**
|
|
14
|
+
* Every refusal of these services: `code` is stable, `detail` carries no credential or body.
|
|
15
|
+
* One class for reads, writes and stores, so a host maps it once.
|
|
16
|
+
*/
|
|
17
|
+
export declare const HostServiceError: typeof GitHubAdapterError;
|
|
18
|
+
export type HostServiceError = GitHubAdapterError;
|
|
19
|
+
export type { AdvisoryReview, AuthorizationDecision, FileBytesEvidence, IssueCommentEvidence, OperationDescriptor, OperationRecord, TrustedConfigSnapshot, UntrustedText, WriteCapability, WriteOperation, WriteOutcome, WrittenResource, };
|
|
20
|
+
/** A transport over `fetch` with a server-owned token; GitHub's API origin unless overridden. */
|
|
21
|
+
export declare function fetchTransport(options: FetchTransportOptions): HostTransport;
|
|
22
|
+
export interface HostWriteOptions {
|
|
23
|
+
/** Server-owned. Never stored, logged or returned. */
|
|
24
|
+
token: string | (() => string | Promise<string>);
|
|
25
|
+
/** The only writes performed, whatever the token permits. */
|
|
26
|
+
capabilities: readonly WriteCapability[];
|
|
27
|
+
/** The host's decision for one exact descriptor. Policy stays with the host. */
|
|
28
|
+
authorize: (descriptor: OperationDescriptor) => AuthorizationDecision | Promise<AuthorizationDecision>;
|
|
29
|
+
operations: OperationStore;
|
|
30
|
+
/** Milliseconds since the epoch. */
|
|
31
|
+
clock: () => number;
|
|
32
|
+
markers?: {
|
|
33
|
+
namespace?: string;
|
|
34
|
+
} | false;
|
|
35
|
+
/** The login the token writes as; reconciliation trusts only its markers. */
|
|
36
|
+
expectedAuthor?: string;
|
|
37
|
+
fetchImpl?: typeof fetch;
|
|
38
|
+
apiOrigin?: string;
|
|
39
|
+
timeoutMs?: number;
|
|
40
|
+
maxBodyBytes?: number;
|
|
41
|
+
userAgent?: string;
|
|
42
|
+
}
|
|
43
|
+
export interface EngineeringHostOptions {
|
|
44
|
+
repository: {
|
|
45
|
+
owner: string;
|
|
46
|
+
name: string;
|
|
47
|
+
};
|
|
48
|
+
transport: HostTransport;
|
|
49
|
+
/** Paging and text bounds of the evidence reads; the package defaults otherwise. */
|
|
50
|
+
evidenceLimits?: Partial<EvidenceLimits>;
|
|
51
|
+
writes?: HostWriteOptions;
|
|
52
|
+
}
|
|
53
|
+
export interface HostWrites {
|
|
54
|
+
/** The exact descriptor `operation` would send; sends nothing. */
|
|
55
|
+
prepare(operation: WriteOperation): Promise<OperationDescriptor>;
|
|
56
|
+
/** Sends once per operation id; a repeat is answered from the operation store. */
|
|
57
|
+
execute(operation: WriteOperation): Promise<WriteOutcome>;
|
|
58
|
+
/** Settles an uncertain write against GitHub without sending it again. */
|
|
59
|
+
reconcile(operationId: string): Promise<WriteOutcome>;
|
|
60
|
+
abandon(operationId: string, reason: string): Promise<void>;
|
|
61
|
+
}
|
|
62
|
+
export interface EngineeringHost {
|
|
63
|
+
readonly schema: typeof HOST_SERVICES_SCHEMA;
|
|
64
|
+
/** `owner/name`. */
|
|
65
|
+
readonly repository: string;
|
|
66
|
+
readonly profile: "read-only" | "governed-writes";
|
|
67
|
+
readonly evidence: HostEvidence;
|
|
68
|
+
/**
|
|
69
|
+
* Advisory reviews on a pull request, decoded under the host's review protocol. Author trust
|
|
70
|
+
* comes from the forge's authenticated comment author and `trustedAuthors`, never from text.
|
|
71
|
+
*/
|
|
72
|
+
advisoryReviews(protocol: Parameters<typeof listAdvisoryReviews>[1], pullRequest: {
|
|
73
|
+
number: number;
|
|
74
|
+
headSha: string;
|
|
75
|
+
}, options: {
|
|
76
|
+
trustedAuthors: readonly string[];
|
|
77
|
+
}): Promise<AdvisoryReview[]>;
|
|
78
|
+
/** `comforge.config.json` at the head of `branch`, as bytes at one immutable revision. */
|
|
79
|
+
trustedConfig(options: {
|
|
80
|
+
branch: string;
|
|
81
|
+
now?: () => number;
|
|
82
|
+
maxBytes?: number;
|
|
83
|
+
}): Promise<TrustedConfigSnapshot>;
|
|
84
|
+
/** Whether GitHub holds exactly `snapshot.text` at `snapshot.revision`. */
|
|
85
|
+
confirmTrustedConfig(snapshot: Pick<TrustedConfigSnapshot, "revision" | "text">): Promise<"match" | "mismatch" | "absent">;
|
|
86
|
+
readonly writes: HostWrites;
|
|
87
|
+
}
|
|
88
|
+
export declare function createEngineeringHost(options: EngineeringHostOptions): EngineeringHost;
|
|
89
|
+
/** An operation that can never be sent again: succeeded, abandoned, or failed without retry. */
|
|
90
|
+
export declare function isFinished(record: OperationRecord): boolean;
|
|
91
|
+
/**
|
|
92
|
+
* The stored form of a record: a finished one keeps a digest of its body instead of the body.
|
|
93
|
+
* Identity, version, fingerprint, descriptor digest, outcome and failure are untouched.
|
|
94
|
+
*/
|
|
95
|
+
export declare function minimizeFinished(record: OperationRecord): OperationRecord;
|
|
96
|
+
export type RetentionClass = "retryable" | "unresolved" | "finished-compact" | "finished-compactable";
|
|
97
|
+
export declare function retentionClass(record: OperationRecord): RetentionClass;
|
|
98
|
+
export interface SqliteOperationStoreOptions {
|
|
99
|
+
/** The host's database. `prepare` is called on first use, never at construction. */
|
|
100
|
+
database: HostDatabase;
|
|
101
|
+
/** The repository (`owner/name`) whose records this store holds; others are refused. */
|
|
102
|
+
repository: string;
|
|
103
|
+
/** Default `comforge_ledger`. An existing table keeps its name, columns and rows. */
|
|
104
|
+
table?: string;
|
|
105
|
+
/** The column holding the repository. Default `namespace`. */
|
|
106
|
+
repositoryColumn?: string;
|
|
107
|
+
/** Write `kind` and `state` beside each record (`kind` can never change). Default false. */
|
|
108
|
+
kindAndStateColumns?: boolean;
|
|
109
|
+
clock?: () => Date;
|
|
110
|
+
}
|
|
111
|
+
export interface SqliteOperationStore extends OperationStore {
|
|
112
|
+
/** Read-only: how many of this repository's records are in each retention class. */
|
|
113
|
+
inspect(): Promise<{
|
|
114
|
+
operations: number;
|
|
115
|
+
classes: Record<string, number>;
|
|
116
|
+
unreadable: number;
|
|
117
|
+
}>;
|
|
118
|
+
/**
|
|
119
|
+
* Identity-preserving: finished records last written before `before` are rewritten in their
|
|
120
|
+
* minimized form, at most `limit` per call. Unresolved and retryable records are never touched.
|
|
121
|
+
*/
|
|
122
|
+
compact(options: {
|
|
123
|
+
before: Date;
|
|
124
|
+
limit: number;
|
|
125
|
+
}): Promise<RetentionReport>;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The operation store over the host's SQLite database. Atomic across processes and restarts
|
|
129
|
+
* (insert-if-absent, version-checked replace). A record never holds a credential, a finished
|
|
130
|
+
* one is stored minimized, and one that is malformed, of another repository or inconsistent
|
|
131
|
+
* with its state is refused, so the writer reports a store failure and sends nothing.
|
|
132
|
+
*/
|
|
133
|
+
export declare function sqliteOperationStore(options: SqliteOperationStoreOptions): SqliteOperationStore;
|