checker.ts
This file
Path
apps/sandbox/src/checker.ts
166 lines · 5.1 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/comments-ops into mainoscabrielbefore adoptione4c407e
lines 1–3why, one step back1// Each check job gets a Checker of its own (a unique id). A Checker named2// `snapshots:<project>` never starts a container: it only keeps that3// project's snapshot ids.Merge observability into mainoscabrielbefore adoption1985111
line 4why, one step back4import { CheckAt } from "@repo/checks/check-runner";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 5–6why, one step back5import type { CheckRun } from "@repo/checks/check-runner";6import { CheckExit, CheckWire } from "@repo/checks/check-runner/wire";Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 7why, one step back7import { keyOf } 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
lines 8–9why, one step back8import { VerifierBlocked } from "@repo/protocol/refusal";9import { DurableObject } from "cloudflare:workers";Merge observability into mainoscabrielbefore adoption1985111
line 10why, one step back10import { Effect, Exit, Layer, Option, Redacted, 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 11–17why, one step back1112import { SdkBox } from "./box/sdk.ts";13import { envPlanOf } from "./checker/env.ts";14import { WorkerCheckLogs } from "./checker/logs/worker.ts";15import { blocked, check } from "./checker/run.ts";16import { hasGateways, SdkChecker } from "./checker/sdk.ts";17import { answerJob } from "./job.ts";Merge observability into mainoscabrielbefore adoption1985111
lines 18–19why, one step back18import type { Job } from "./job.ts";19import { jobTelemetry } from "./telemetry.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 20–24why, one step back2021const DEFAULT_IMAGE = "checker:default";2223const CHECK_IMAGE = "check";24Merge build/comments-ops into mainoscabrielbefore adoptione4c407e
line 25why, one step back25/** oxlint's JS plugins reserve about 6 GiB of address space per thread; smaller sizes abort. */Start the Checker's containers on standard-3, so burrow's lint lane runs, and record the dev stage's live core looposcabrielbefore adoptionc7b68ff
lines 26–27why, one step back26export const checkInstance = "standard-3";27Merge observability into mainoscabrielbefore adoption1985111
lines 28–50why, one step back28export const checkJob: Job<29 CheckWire,30 typeof CheckWire.Encoded,31 CheckRun,32 VerifierBlocked,33 typeof CheckExit.Encoded34> = {35 // A defect may quote a sealed file's path: its span keeps its name alone,36 // and the platform's report of it its name and frames.37 defects: "name",38 exit: CheckExit,39 ids: ({ job }) => ({40 project: job.project,41 sha: CheckAt.match(job.at, {42 Commit: ({ commit }): string => commit,43 Tree: ({ tree }): string => tree,44 }),45 }),46 name: "Checker.check",47 tokens: ({ clone }) => [clone.token],48 wire: CheckWire,49};50Merge 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 51–52why, one step back51export class Checker extends DurableObject<Env> {52 check(wire: typeof CheckWire.Encoded): Promise<typeof CheckExit.Encoded> {Merge observability into mainoscabrielbefore adoption1985111
line 53why, one step back53 return answerJob(checkJob, (job) => this.#checked(job))(wire);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 54–91why, one step back54 }5556 snapshotOf(key: string): string | null {57 return this.ctx.storage.kv.get<string>(key) ?? null;58 }5960 keepSnapshot(key: string, id: string): void {61 this.ctx.storage.kv.put(key, id);62 }6364 forgetSnapshot(key: string): void {65 this.ctx.storage.kv.delete(key);66 }6768 #checked(wire: CheckWire): Effect.Effect<CheckRun, VerifierBlocked> {69 const { container, exports } = this.ctx;7071 if (container === undefined) {72 return blocked("the Checker has no container", { phase: "start" });73 }7475 if (!hasGateways(exports)) {76 return blocked("worker.ts does not export the outbound gateways", {77 phase: "start",78 });79 }8081 const image = container.images[CHECK_IMAGE];82 const wanted = wire.job.run.image ?? DEFAULT_IMAGE;8384 if (image === undefined || wanted !== DEFAULT_IMAGE) {85 return blocked("no image for this run block", {86 image: wanted,87 phase: "start",88 });89 }9091 const checkers: Checkers = this.env;Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
lines 92–95why, one step back9293 const index = checkers.CHECKER.getByName(94 `snapshots:${keyOf(wire.job.project)}`95 );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 96–99why, one step back9697 return check(wire).pipe(98 Effect.provide(99 Layer.mergeAll(Start the Checker's containers on standard-3, so burrow's lint lane runs, and record the dev stage's live core looposcabrielbefore adoptionc7b68ff
lines 100–104why, one step back100 SdkBox.layer(container, {101 enableInternet: false,102 image,103 instance: checkInstance,104 }),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 105–112why, one step back105 SdkChecker.layer(106 container,107 exports,108 {109 find: (key) => index.snapshotOf(key),110 forget: (key) => index.forgetSnapshot(key),111 keep: (key, id) => index.keepSnapshot(key, id),112 },Start the Checker's containers on standard-3, so burrow's lint lane runs, and record the dev stage's live core looposcabrielbefore adoptionc7b68ff
lines 113–121why, one step back113 {114 instance: checkInstance,115 // The hosts its env route may open, reserved ahead of the116 // catch-all intercept.117 proxied: Option.match(envPlanOf(wire.job).route, {118 onNone: () => [],119 onSome: ({ hosts }) => hosts,120 }),121 }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 122–129why, one step back122 ),123 WorkerCheckLogs.layer124 )125 )126 );127 }128}129Merge build/comments-ops into mainoscabrielbefore adoptione4c407e
line 130why, one step back130/** Written out so the Checker reads plain RPC promises and a test needs no stub types. */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 131–162why, one step back131export interface Checkers {132 readonly CHECKER: {133 readonly newUniqueId: () => DurableObjectId;134 readonly get: (id: DurableObjectId) => {135 readonly check: (136 wire: typeof CheckWire.Encoded137 ) => Promise<typeof CheckExit.Encoded>;138 };139 readonly getByName: (name: string) => {140 readonly snapshotOf: (key: string) => Promise<string | null>;141 readonly keepSnapshot: (key: string, id: string) => Promise<void>;142 readonly forgetSnapshot: (key: string) => Promise<void>;143 };144 };145}146147export const runCheck = (148 env: Checkers,149 wire: typeof CheckWire.Encoded150): Promise<typeof CheckExit.Encoded> =>151 Effect.runPromise(152 Effect.tryPromise(() =>153 env.CHECKER.get(env.CHECKER.newUniqueId()).check(wire)154 ).pipe(155 Effect.flatMap(Schema.decodeEffect(CheckExit)),156 Effect.tapError((failure) =>157 Effect.logError("the Checker could not be reached").pipe(158 Effect.annotateLogs({ reason: String(failure) })159 )160 ),161 Effect.orElseSucceed(() => Exit.fail(new VerifierBlocked({}))),162 Effect.flatMap(Schema.encodeEffect(CheckExit)),Merge observability into mainoscabrielbefore adoption1985111
lines 163–164why, one step back163 Effect.orDie,164 Effect.provide(jobTelemetry([Redacted.make(wire.clone.token)]))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 165–166why, one step back165 )166 );