surfaces.ts
This file
Path
apps/platform/src/surfaces.ts
388 lines · 11.4 KiB · TypeScript
Last changed by
Merge observability into mainoscabrielbefore adoption1985111
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
line 1why, one step back1import { commandDocs } from "@repo/agent-kit/command-doc";Merge build/1007oscabrielbefore adoption77fa97a
line 2why, one step back2import { platformSkillParts } from "@repo/agent-kit/platform";Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
line 3why, one step back3import { RepoHost } from "@repo/artifacts/repo-host";Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 4–5why, one step back4import { Brand } from "@repo/brand";5import { Upstream } from "@repo/git/upstream";Name the ask an opening opened, say why a policy has not evaluated, and give code history a durable storeoscabrielbefore adoption51f046c
line 6why, one step back6import { CodeStore } from "@repo/project/code-store";Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 7why, one step back7import { CodeCache } from "@repo/project/code/cache";Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
line 8why, one step back8import { FrontPage } from "@repo/project/front-page";Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
line 9why, one step back9import { Projects } from "@repo/project/projects";Join W12 group 1: kept postures on the platform's /, and vellum's standing in the dev worldoscabrielbefore adoptione817d6c
line 10why, one step back10import { StandingPostures } from "@repo/project/standing-posture";Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 11–12why, one step back11import { authenticated, bearerToken } from "@repo/protocol/authenticate";12import { Client, Contracts, surfaced } from "@repo/protocol/contracts";Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
line 13why, one step back13import { ProjectName } from "@repo/protocol/ids";Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
line 14why, one step back14import { toToolkit } from "@repo/protocol/mcp";Merge build/1007oscabrielbefore adoption77fa97a
line 15why, one step back15import { briefResources, skillPrompt } from "@repo/protocol/mcp-extras";Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 16–17why, one step back16import { Authenticated } from "@repo/protocol/principal";17import type { Person } from "@repo/protocol/principal";Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
line 18why, one step back18import { Context, Effect, Layer, Option, Schema } from "effect";Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 19–35why, one step back19import { McpProtocol, McpServer } from "effect/ai";20import {21 Headers,22 HttpRouter,23 HttpServerRequest,24 HttpServerResponse,25} from "effect/http";26import type { Rpc, RpcGroup } from "effect/rpc";27import {28 RpcMiddleware,29 RpcSerialization,30 RpcServer,31 RpcTest,32} from "effect/rpc";3334import { serveGit } from "./git.ts";35import type { GitServices } from "./git.ts";Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
lines 36–37why, one step back36import { homeResponse } from "./home.ts";37import { HostInfo } from "./host-info.ts";Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
line 38why, one step back38import { LayerGate } from "./layer-gate.ts";Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
line 39why, one step back39import { rawResponse } from "./raw.ts";Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 40–41why, one step back40import { releaseRoutes } from "./releases.ts";41import type { ReleaseStore } from "./releases.ts";Merge build/1007oscabrielbefore adoption77fa97a
line 42why, one step back42import { buildHandler } from "./router-build.ts";Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 43–46why, one step back43import { Sessions } from "./sessions.ts";44import { isFramed } from "./watch.ts";45import { agentSkillRoutes } from "./well-known.ts";46Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
lines 47–48why, one step back47export { HostInfo } from "./host-info.ts";48Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 49–59why, one step back49export class Stage extends Context.Service<Stage, { readonly name: string }>()(50 "platform/Stage"51) {}5253export type Services =54 | Rpc.ToHandler<RpcGroup.Rpcs<typeof Contracts>>55 | Sessions56 | GitServices57 | ReleaseStore58 | FrontPage59 | RepoHostAdd the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
lines 60–61why, one step back60 | Stage61 | HostInfo;Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 62–63why, one step back6263const GIT_PATH =Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 64why, one step back64 /^\/[A-Za-z0-9._-]+(?:\/[A-Za-z0-9._-]+){0,2}\/(?:info\/refs|git-upload-pack|git-receive-pack)$/u;Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 65–70why, one step back6566const DISCOVERY_PATH =67 /^\/\.well-known\/oauth-(?:protected-resource|authorization-server)(?:\/.*)?$/u;6869const SIGN_IN_PATH = /^\/(?:authorize|signup|token|oauth(?:\/.*)?)$/u;70Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8
line 71why, one step back71// No git path, whose segment after the name is `info` or a git service, ever matches.Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 72why, one step back72const RAW_PATH = /^\/[a-z0-9._-]+(?:\/[a-z0-9._-]+){0,2}\/raw\/.+$/u;Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
line 73why, one step back73Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 74–78why, one step back74const RELEASE_PATH =75 /^\/(?:install\.sh|releases\/(?:manifest\.json|[^/]+\/[^/]+))$/u;7677const SKILLS_PATH = /^\/\.well-known\/agent-skills\/.+$/u;78Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8
lines 79–81why, one step back79// No credential is asked for or checked on these, so none answers 401: git's gateway80// answers an unknown credential itself, and the rest may carry a stale token or an81// OAuth client's own credential.Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
line 82why, one step back82const isExempt = (path: string) =>Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
line 83why, one step back83 path === "/" ||Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
line 84why, one step back84 path === "/health" ||Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
line 85why, one step back85 RAW_PATH.test(path) ||Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 86–106why, one step back86 GIT_PATH.test(path) ||87 DISCOVERY_PATH.test(path) ||88 SIGN_IN_PATH.test(path) ||89 RELEASE_PATH.test(path) ||90 SKILLS_PATH.test(path);9192const PROTECTED_RESOURCE_PATH = "/.well-known/oauth-protected-resource";9394const originOf = (request: HttpServerRequest.HttpServerRequest) =>95 URL.parse(request.originalUrl)?.origin ??96 Option.match(HttpServerRequest.toURL(request), {97 onNone: () => "http://localhost",98 onSome: ({ origin }) => origin,99 });100101export const protectedResourceMetadata = (origin: string) => ({102 authorization_servers: [origin] as const,103 bearer_methods_supported: ["header"] as const,104 resource: origin,105});106Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8
lines 107–108why, one step back107// RFC 9728 §5.1, which MCP's authorization requires. `invalid_token` only for a108// bearer token the platform does not know (RFC 6750 §3.1).Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 109–175why, one step back109const unauthorized = (110 request: HttpServerRequest.HttpServerRequest,111 error?: "invalid_token"112) => {113 const params = [114 ...(error === undefined ? [] : [`error="${error}"`]),115 `resource_metadata="${originOf(request)}${PROTECTED_RESOURCE_PATH}"`,116 ];117118 return HttpServerResponse.empty({119 headers: { "www-authenticate": `Bearer ${params.join(", ")}` },120 status: 401,121 });122};123124const Caller = Context.Reference<125 Option.Option<{ readonly person: Person; readonly token: string }>126>("platform/Caller", { defaultValue: Option.none });127128const identifyCaller = HttpRouter.middleware(129 Effect.gen(function* () {130 const sessions = yield* Sessions;131132 return (httpEffect) =>133 Effect.gen(function* () {134 const request = yield* HttpServerRequest.HttpServerRequest;135136 const [path = request.url] = request.url.split("?");137138 const { authorization } = request.headers;139140 if (authorization === undefined || isExempt(path)) {141 return yield* httpEffect;142 }143144 const token = bearerToken(authorization);145146 if (Option.isNone(token)) {147 return unauthorized(request);148 }149150 const person = yield* sessions.verify(token.value);151152 if (Option.isNone(person)) {153 return unauthorized(request, "invalid_token");154 }155156 return yield* Effect.provideService(157 httpEffect,158 Caller,159 Option.some({ person: person.value, token: token.value })160 );161 });162 }),163 { global: true }164);165166const signedInOnly = HttpRouter.middleware((httpEffect) =>167 Effect.gen(function* () {168 if (Option.isSome(yield* Caller)) {169 return yield* httpEffect;170 }171172 return unauthorized(yield* HttpServerRequest.HttpServerRequest);173 })174).layer;175Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8
lines 176–177why, one step back176// A call naming a credential other than the one its request was let in with (one an177// RPC message adds to its own headers) runs as nobody.Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 178–212why, one step back178const asCaller = authenticated((credential) =>179 Caller.useSync(180 Option.flatMap(({ person, token }) =>181 token === credential ? Option.some(person) : Option.none()182 )183 )184);185186const forwardCredential = RpcMiddleware.layerClient(187 Authenticated,188 ({ next, request }) =>189 Effect.gen(function* () {190 const incoming = yield* Effect.serviceOption(191 HttpServerRequest.HttpServerRequest192 );193194 const authorization = Option.flatMapNullishOr(195 incoming,196 ({ headers }) => headers.authorization197 );198199 return yield* next(200 Option.match(authorization, {201 onNone: () => request,202 onSome: (value) => ({203 ...request,204 headers: Headers.set(request.headers, "authorization", value),205 }),206 })207 );208 })209);210211const projection = toToolkit(surfaced("mcp"), Effect.service(Client));212Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8
lines 213–214why, one step back213// Made from the skill's parts, not its `SKILL.md`, whose hash is a promise the214// router's build may not await.Merge build/1007oscabrielbefore adoption77fa97a
lines 215–216why, one step back215const contribute = skillPrompt(216 platformSkillParts({ commands: commandDocs(Contracts) })Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 217–263why, one step back217);218219const mcp = Layer.mergeAll(220 McpServer.toolkit(projection.toolkit).pipe(221 Layer.provideMerge(projection.layer)222 ),223 briefResources,224 contribute225).pipe(226 Layer.provide(Layer.effect(Client, RpcTest.makeClient(Contracts))),227 Layer.provide([asCaller, forwardCredential]),228 Layer.provide(229 McpServer.layerHttp({230 name: Brand.name,231 path: "/mcp",232 protocols: [McpProtocol.v2026_07_28],233 version: "0.0.0",234 }).pipe(Layer.provide(signedInOnly))235 )236);237238const rpcIn = (serialization: Layer.Layer<RpcSerialization.RpcSerialization>) =>239 RpcServer.toHttpEffect(Contracts).pipe(Effect.provide(serialization));240241const rpc = HttpRouter.use((router) =>242 Effect.gen(function* () {243 const whole = yield* rpcIn(RpcSerialization.layerJson);244 const framed = yield* rpcIn(RpcSerialization.layerNdjson);245246 yield* router.add("POST", "/rpc", (request) =>247 isFramed(request.headers) ? framed : whole248 );249 })250).pipe(Layer.provide(asCaller));251252const health = HttpRouter.use((router) =>253 Effect.gen(function* () {254 const { name } = yield* Stage;255256 yield* router.add(257 "GET",258 "/health",259 HttpServerResponse.jsonUnsafe({ ok: true, stage: name })260 );261 })262);263Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
lines 264–275why, one step back264const fromWeb =265 <R>(respond: (web: Request) => Effect.Effect<Response, never, R>) =>266 (request: HttpServerRequest.HttpServerRequest) =>267 HttpServerRequest.toWeb(request).pipe(268 Effect.flatMap(respond),269 Effect.map(HttpServerResponse.fromWeb),270 Effect.orElseSucceed(() => HttpServerResponse.empty({ status: 400 }))271 );272273const home = HttpRouter.use((router) =>274 Effect.gen(function* () {275 const services = Context.make(FrontPage, yield* FrontPage).pipe(Merge build/1102 into mainoscabrielbefore adoption084383d
line 276why, one step back276 Context.add(HostInfo, yield* HostInfo),Join W12 group 1: kept postures on the platform's /, and vellum's standing in the dev worldoscabrielbefore adoptione817d6c
lines 277–278why, one step back277 Context.add(Projects, yield* Projects),278 Context.add(StandingPostures, yield* StandingPostures)Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
lines 279–290why, one step back279 );280281 yield* router.add(282 "GET",283 "/",284 fromWeb((web) => Effect.provide(homeResponse(web), services))285 );286 })287);288289const decodeProject = Schema.decodeUnknownOption(ProjectName);290Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
lines 291–305why, one step back291const ADDRESSES = ["/:a", "/:a/:b", "/:a/:b/:c"] as const;292293const addressOf = (params: Readonly<Record<string, string | undefined>>) =>294 [params.a, params.b, params.c]295 .filter((segment) => segment !== undefined)296 .join("/");297298const RAW_ROUTES = ADDRESSES.map((address) => `${address}/raw/*` as const);299300const GIT_ROUTES = ADDRESSES.flatMap((address) => [301 `${address}/info/refs` as const,302 `${address}/git-upload-pack` as const,303 `${address}/git-receive-pack` as const,304]);305Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
lines 306–308why, one step back306const raw = HttpRouter.use((router) =>307 Effect.gen(function* () {308 const services = Context.make(RepoHost, yield* RepoHost).pipe(Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 309why, one step back309 Context.add(Projects, yield* Projects),Name the ask an opening opened, say why a policy has not evaluated, and give code history a durable storeoscabrielbefore adoption51f046c
lines 310–311why, one step back310 Context.add(CodeCache, yield* CodeCache),311 Context.add(CodeStore, yield* CodeStore)Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
lines 312–313why, one step back312 );313Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
lines 314–317why, one step back314 for (const path of RAW_ROUTES) {315 yield* router.add("GET", path, (request) =>316 Effect.gen(function* () {317 const params = yield* HttpRouter.params;Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
line 318why, one step back318Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
lines 319–329why, one step back319 return yield* Option.match(decodeProject(addressOf(params)), {320 onNone: () =>321 Effect.succeed(HttpServerResponse.empty({ status: 404 })),322 onSome: (project) =>323 fromWeb((web) =>324 Effect.provide(rawResponse(web, project), services)325 )(request),326 });327 })328 );329 }Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
lines 330–332why, one step back330 })331);332Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 333–369why, one step back333const discovery = HttpRouter.use((router) =>334 router.add("GET", PROTECTED_RESOURCE_PATH, (request) =>335 Effect.succeed(336 HttpServerResponse.jsonUnsafe(337 protectedResourceMetadata(originOf(request))338 )339 )340 )341);342343const toGateway = (request: HttpServerRequest.HttpServerRequest) =>344 HttpServerRequest.toWeb(request).pipe(345 Effect.flatMap((web) =>346 Option.match(serveGit(web), {347 onNone: () => Effect.succeed(HttpServerResponse.empty({ status: 404 })),348 onSome: Effect.map(HttpServerResponse.fromWeb),349 })350 ),351 Effect.orElseSucceed(() => HttpServerResponse.empty({ status: 400 }))352 );353354const git = HttpRouter.use((router) =>355 Effect.gen(function* () {356 const services = Context.make(Sessions, yield* Sessions).pipe(357 Context.add(Projects, yield* Projects),358 Context.add(Upstream, yield* Upstream),359 Context.add(LayerGate, yield* LayerGate)360 );361362 for (const path of GIT_ROUTES) {363 yield* router.add("*", path, (request) =>364 Effect.provide(toGateway(request), services)365 );366 }367 })368);369Merge observability into mainoscabrielbefore adoption1985111
lines 370–373why, one step back370export const serve = <E, TE = never>(371 services: Layer.Layer<Services, E>,372 telemetry?: Layer.Layer<never, TE>373) =>Merge build/1007oscabrielbefore adoption77fa97a
line 374why, one step back374 buildHandler(Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
line 375why, one step back375 Layer.mergeAll(Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
line 376why, one step back376 home,Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 377–378why, one step back377 health,378 discovery,Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
line 379why, one step back379 raw,Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 380–385why, one step back380 git,381 mcp,382 rpc,383 releaseRoutes,384 agentSkillRoutes,385 identifyCallerMerge observability into mainoscabrielbefore adoption1985111
lines 386–387why, one step back386 ).pipe(Layer.provide(services)),387 telemetryMerge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
line 388why, one step back388 );