Skip to content

About

API definitions (Protocol Buffers and Connect) shared by Bugnet's router, gateway and edges.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

bugnet-proto

Protobuf and Connect API definitions, and shared test vectors, used by the Bugnet services.

Services

The router's files are in package bugnet.router.v1 under proto/bugnet/router/v1. Each of their services has one caller, which sends that service's own bearer token. The router refuses any other service's token, and admits no calls to a service whose token is empty.

Service File Caller Token variable
SandboxRouterService router.proto MCP gateway, bugnet-dev BUGNET_INTERNAL_TOKEN
EdgeService edge.proto bugnet-edge BUGNET_EDGE_TOKEN
SecretsRouteService edge.proto bugnet-secrets BUGNET_SECRETS_TOKEN
SshGatewayService edge.proto bugnet-ssh BUGNET_SSH_TOKEN
EgressService edge.proto bugnet-egress BUGNET_EGRESS_TOKEN
OperatorService operator.proto bugnet-dev, operators only BUGNET_OPERATOR_TOKEN
PaymentsService payments.proto Payments BUGNET_PAYMENTS_ROUTER_TOKEN
SessionPageService edge.proto bugnet-edge --role sessions BUGNET_SESSIONS_TOKEN

router.proto imports none of the other three files, so the gateway's generated client holds none of their services. Errors carry a google.rpc.ErrorInfo reason in domain bugnet.router.

BUGNET_PAYMENTS_ROUTER_TOKEN has the same name and value in Payments' environment and the router's. It is not BUGNET_PAYMENTS_TOKEN, which the gateway sends to Payments. PaymentsService has five calls. CheckPayer says whether the router would refuse a payer's order now. GetOrder says what became of an order, so Payments can refund one no event reached it for. The other three serve x402, below.

x402 calls

The x402 spec (section 6.2) adds seven calls across three services.

Service Call Does
SandboxRouterService CheckX402 Checks an x402 change without making it
SandboxRouterService EnableX402 Stores x402 services and applies them to the sandbox
SandboxRouterService ListX402 Lists the placement's x402 services, counts and refusals
EgressService AppendX402Events Stores egress's x402 rows for the session page
PaymentsService LookupPlacement Names the placement of a handle hash or a start order
PaymentsService AppendAllowanceEvent Stores an allowance event for the session page
PaymentsService AppendX402Resolved Stores Payments' final word on one x402 call
  • A refusal of CheckX402 or EnableX402 for one service carries ErrorInfo metadata service_id and host, which name the first failing add entry.
  • AllowanceEvent keeps the session page's numbers 1 to 5. The x402 spec adds kept_micros (6), the kept phase and AppendAllowanceEventRequest.start_order_id (3).
  • ResolveTunnelResponse.x402 (11) and ReportEgressUsageRequest.x402_unpaid (3) carry the caps and the unpaid use between the router and egress.

Session page calls

The session page spec (section 7.3) adds six calls across four services.

Service Call Does
SessionPageService GetSessionRecord Answers a record's state and page JSON for its key
SessionPageService DeleteSessionRecord Deletes a record's public page for an id and key
SessionPageService WatchSessionJob Streams one job's cleaned output while it runs
SandboxRouterService AppendSessionEvents Stores the gateway's tool calls for the page
SandboxRouterService RotateSessionLink Ends the named link and answers a new one
OperatorService GetSessionHold Answers a held record's page JSON to an operator
  • Placement.session_url (14) carries the page link on every answer until the record expires.
  • A wrong key and an unknown id both answer NotFound with reason SESSION_UNKNOWN.
  • AllowanceEvent, below, also feeds the page.

The x402 signer

proto/bugnet/payments/v1/x402.proto, in package bugnet.payments.v1, holds X402SignerService. Payments serves it on its mTLS listener, and bugnet-egress alone calls it, with its client certificate and no bearer token. Payments owns the file. The Go code is in gen/go/bugnet/payments/v1 (package paymentsv1).

Call Does
ReserveAndSign Checks a seller's 402, reserves the amount and signs the transfer
ReportOutcome Reports the seller's answer to a paid attempt, as hints only

Generating

make generate runs buf generate. It writes the Go packages to gen/go with protoc-gen-go and protoc-gen-connect-go from PATH, and JavaScript with TypeScript declarations to gen/ts with protoc-gen-es from node_modules, so run pnpm install first. Imports between the generated files end in .js, which Node's ESM loader needs. Run make test, make lint and make build for the Go code.

TypeScript

The ESM-only package @bugnet/proto exports four paths, and needs @bufbuild/protobuf 2.16 or a later 2.x beside it.

Path Holds For
@bugnet/proto/router/v1 router.proto, with SandboxRouterService The MCP gateway
@bugnet/proto/router/v1/payments payments.proto, with PaymentsService Payments
@bugnet/proto/payments/v1/x402 x402.proto, with X402SignerService Payments
@bugnet/proto/vectors/x402/<file> One x402 test vector file The MCP gateway

gen/ts also holds edge.proto and operator.proto, since buf generate runs every plugin on every file. The package exports neither, because only Go programs call those services. Use the package from a sibling checkout:

"dependencies": {
  "@bufbuild/protobuf": "2.16.0",
  "@bugnet/proto": "link:../bugnet-proto"
}
import { createClient } from "@connectrpc/connect";
import { SandboxRouterService } from "@bugnet/proto/router/v1";

const router = createClient(SandboxRouterService, transport);
import { PaymentsService } from "@bugnet/proto/router/v1/payments";

const payments = createClient(PaymentsService, transport);
import { X402SignerService } from "@bugnet/proto/payments/v1/x402";

Run pnpm install here once too. Node follows the link to this checkout, so the generated code loads its own @bufbuild/protobuf from this node_modules, unless Node runs with --preserve-symlinks.

Test vectors

vectors/x402 holds the x402 test cases that the router, bugnet-egress and the MCP gateway each run, so the three never drift. Go code reads them from vectors.X402, an embed.FS in package github.com/bugnet-sh/bugnet-proto/vectors. TypeScript imports them as JSON.

import paths from "@bugnet/proto/vectors/x402/paths.json" with { type: "json" };
File Cases Run by
paths.json paths of {path, ok}, queries of {query, ok} Router, egress, gateway
service-ids.json cases of {method, host, prefix, id} Router, gateway
shared-hosting-suffixes.json suffixes, a list of names Router, gateway
  • paths.json holds the path and query rules of x402 spec 6.7. ok says whether the rule passes the case. Egress runs both lists. The router and the gateway run only paths, since a service line has no query.
  • service-ids.json holds service ids of x402 spec 5.1. An id is xs_ and the first 16 characters of lower-case base32 of SHA-256 of method, host and prefix, joined by a zero byte.
  • shared-hosting-suffixes.json holds the 16 shared-hosting suffixes of x402 spec 6.16, in its order. The router's and the gateway's shared_hosting_suffixes defaults equal it.
  • Each file is one JSON object with v 1. No case appears twice.
  • The gateway team owns the content. A change needs both the gateway's and the router's owners.

go test ./vectors checks each file: the shape, with every key spelled exactly as above, every id against a reference implementation, every path and query case against a reference of the 6.7 rule, and the suffix list. Go's JSON decoder alone would accept Path for path.

About

API definitions (Protocol Buffers and Connect) shared by Bugnet's router, gateway and edges.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages