guards.ts
This file
Path
apps/platform/src/workflows/answer/guards.ts
152 lines · 4.2 KiB · TypeScript
Last changed by
Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8
lines 1–2why, one step back1// Layer content is compared only for layers the answerer or a finisher reads: for2// anyone else the guard would be an oracle on a layer's content.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 3–103why, one step back3import { ObjectId, RepoHost } from "@repo/artifacts/repo-host";4import type { Change } from "@repo/project/landing";5import { LandingState } from "@repo/project/landing";6import { Projects } from "@repo/project/projects";7import { AskKind } from "@repo/protocol/domain/kind";8import { EnvKey } from "@repo/protocol/ids";9import type {10 Handle,11 ProjectName,12 RequestId,13 TreeSha,14} from "@repo/protocol/ids";15import { Refusal } from "@repo/protocol/refusal";16import type {17 LayerContentInPublicTree,18 PrivatePathInPublicTree,19} from "@repo/protocol/refusal";20import { Array, Context, Effect, Option, Schema } from "effect";2122import { StepRunner } from "../step.ts";2324export interface GuardInput {25 readonly project: ProjectName;26 readonly answerer: Handle;27 readonly finishers: readonly Handle[];28 readonly changes: readonly Change[];29}3031export interface LayerGuardsPort {32 readonly check: (33 input: GuardInput34 ) => Effect.Effect<35 readonly (PrivatePathInPublicTree | LayerContentInPublicTree)[]36 >;37}3839export const LandingGuards = Context.Reference<Option.Option<LayerGuardsPort>>(40 "platform/LandingGuards",41 { defaultValue: () => Option.none() }42);4344export const GuardRun = Schema.TaggedUnion({45 Blocked: { attempt: Schema.Int },46 Guarded: {47 finish: AskKind.fields.finish,48 quarantined: Schema.Array(EnvKey),49 refusals: Schema.Array(Refusal),50 },51 Moved: {},52});5354export type GuardRun = typeof GuardRun.Type;5556export const passes = (run: GuardRun): boolean =>57 GuardRun.guards.Guarded(run) &&58 run.refusals.length === 0 &&59 run.quarantined.length === 0;6061const written = (changes: readonly Change[]) =>62 Array.dedupe(63 changes.flatMap(({ after, status }) =>64 status === "deleted" || after === undefined ? [] : [after]65 )66 );6768const guarded = (69 params: { readonly project: ProjectName; readonly requestId: RequestId },70 tree: TreeSha,71 port: LayerGuardsPort72): Effect.Effect<GuardRun, never, Projects | RepoHost> =>73 Effect.gen(function* () {74 const api = yield* Projects.use((projects) =>75 projects.get(params.project)76 ).pipe(Effect.orDie);7778 const run = yield* api.answer(params.requestId);7980 const found = Option.flatMap(run, (each) =>81 LandingState.guards.Checking(each.state) && each.state.tree === tree82 ? Option.some({ finish: each.finish, state: each.state })83 : Option.none()84 );8586 if (Option.isNone(found)) {87 return GuardRun.cases.Moved.make({});88 }8990 const { finish, state } = found.value;91 const { request } = state;9293 const refusals = yield* port.check({94 answerer: request.answerer,95 changes: state.changes,96 finishers: request.credits97 .filter(({ role }) => role === "Finished-by")98 .map(({ handle }) => handle),99 project: params.project,100 });101102 const host = yield* RepoHost;103Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8
lines 104–105why, one step back104 // A fork the platform cannot read leaves its files unscanned, and the tree waits. An105 // id no blob in the fork has is a submodule's pointer, with no content to scan.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 106–152why, one step back106 const read = yield* Effect.option(107 Effect.forEach(written(state.changes), (blob) =>108 host.readBlob(request.fork, ObjectId.make(blob))109 )110 );111112 if (Option.isNone(read)) {113 return GuardRun.cases.Blocked.make({ attempt: state.attempt });114 }115116 const held = yield* Effect.forEach(Array.getSomes(read.value), (blob) =>117 api.scanForValues({ blob })118 );119120 return GuardRun.cases.Guarded.make({121 finish,122 quarantined: Array.dedupe(Array.getSomes(held)),123 refusals,124 });125 });126127export const guards = (128 params: { readonly project: ProjectName; readonly requestId: RequestId },129 tree: TreeSha,130 attempt: number131): Effect.Effect<132 Option.Option<GuardRun>,133 never,134 Projects | RepoHost | StepRunner135> =>136 Effect.gen(function* () {137 const plugged = yield* LandingGuards;138139 if (Option.isNone(plugged)) {140 return Option.none();141 }142143 const { step } = yield* StepRunner;144145 return Option.some(146 yield* step(147 `guards#${attempt}`,148 GuardRun,149 guarded(params, tree, plugged.value)150 )151 );152 });