refusal.ts
This file
Path
packages/protocol/src/refusal.ts
469 lines · 12.2 KiB · TypeScript
Last changed by
Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
History of this fileTake it
1import { Brand } from "@repo/brand";2import { Effect, Schema } from "effect";34import { At } from "./domain/common.ts";5import {6 AskId,7 CommitSha,8 DecisionId,9 EnvKey,10 Handle,11 LandedSha,12 ProjectName,13 ProposalId,14 RepoPath,15 shortNameOf,16} from "./ids.ts";17import { Posture } from "./posture.ts";1819export { At } from "./domain/common.ts";2021const cli = (...args: readonly string[]) => [Brand.cli, ...args].join(" ");2223const Label = Schema.NonEmptyString;2425const Paths = Schema.NonEmptyArray(RepoPath);2627// Every refusal carries `retryable`, fixed per tag, and `next`, the command to28// run next. Both are fields, not statics: Effect's MCP server sends only what29// the failure schema encodes. No refusal has a `message`, so MCP sends JSON.30const Retryable = {31 at: Schema.optionalKey(At),32 next: Schema.NonEmptyString,33 retryable: Schema.tag(true),34};3536const Final = { ...Retryable, retryable: Schema.tag(false) };3738const nextIs = (command: string) => ({39 next: Schema.NonEmptyString.pipe(40 Schema.withConstructorDefault(Effect.succeed(command))41 ),42});4344export class NotClaimed extends Schema.TaggedError<NotClaimed>()("NotClaimed", {45 ...Retryable,46 ask: AskId,47}) {48 static readonly of = (ask: AskId) =>49 new NotClaimed({ ask, next: cli("claim", ask) });50}5152export class ClaimLapsed extends Schema.TaggedError<ClaimLapsed>()(53 "ClaimLapsed",54 {55 ...Retryable,56 ask: AskId,57 }58) {59 static readonly of = (ask: AskId) =>60 new ClaimLapsed({ ask, next: cli("claim", ask) });61}6263export class NotVouched extends Schema.TaggedError<NotVouched>()("NotVouched", {64 ...Final,65 ...nextIs(cli("status")),66}) {}6768export class CapReached extends Schema.TaggedError<CapReached>()("CapReached", {69 ...Retryable,70 ...nextIs(cli("status")),71 cap: Schema.Int,72 /** Which limit: claims held (also when absent), proposals, or drafts. */73 limit: Schema.optionalKey(Schema.Literals(["claims", "proposals", "drafts"])),74 /** For a rate limit: seconds until one more is let through. */75 retryAfter: Schema.optionalKey(76 Schema.Int.check(Schema.isGreaterThanOrEqualTo(1))77 ),78}) {}7980export class AskFull extends Schema.TaggedError<AskFull>()("AskFull", {81 ...Retryable,82 ...nextIs(cli("asks")),83 ask: AskId,84}) {}8586export class NoAsk extends Schema.TaggedError<NoAsk>()("NoAsk", {87 ...Final,88 ...nextIs(cli("asks")),89}) {}9091export class RepoBusy extends Schema.TaggedError<RepoBusy>()("RepoBusy", {92 ...Retryable,93 ...nextIs("git push"),94}) {}9596export class DirectPush extends Schema.TaggedError<DirectPush>()("DirectPush", {97 ...Final,98 ...nextIs(cli("asks")),99 ref: Label,100}) {}101102export class AskBlocked extends Schema.TaggedError<AskBlocked>()("AskBlocked", {103 ...Retryable,104 ...nextIs(cli("asks")),105 ask: AskId,106 blockedBy: Schema.NonEmptyArray(AskId),107}) {108 static readonly of = (109 ask: AskId,110 blockedBy: readonly [AskId, ...(readonly AskId[])]111 ) => new AskBlocked({ ask, blockedBy });112}113114export class Denounced extends Schema.TaggedError<Denounced>()("Denounced", {115 ...Final,116 ...nextIs(cli("status")),117}) {}118119export class OutOfScope extends Schema.TaggedError<OutOfScope>()("OutOfScope", {120 ...Final,121 ...nextIs(cli("check")),122 paths: Paths,123}) {}124125export class TouchedJudge extends Schema.TaggedError<TouchedJudge>()(126 "TouchedJudge",127 {128 ...Final,129 ...nextIs(cli("check")),130 paths: Paths,131 }132) {}133134export class Weakened extends Schema.TaggedError<Weakened>()("Weakened", {135 ...Final,136 ...nextIs(cli("check")),137 weakening: Schema.Union([138 Schema.Struct({ marker: Label, reason: Schema.optionalKey(Label) }),139 Schema.Struct({140 after: Schema.Int,141 before: Schema.Int,142 ratchet: Label,143 territory: Label,144 }),145 ]),146}) {}147148export class PrivatePathInPublicTree extends Schema.TaggedError<PrivatePathInPublicTree>()(149 "PrivatePathInPublicTree",150 { ...Final, ...nextIs(cli("check")), paths: Paths }151) {}152153export class ReachedInternals extends Schema.TaggedError<ReachedInternals>()(154 "ReachedInternals",155 { ...Final, ...nextIs(cli("check")), from: RepoPath, to: Label }156) {}157158export const MovedBy = Schema.Struct({159 ask: Schema.optionalKey(AskId),160 author: Handle,161 landing: LandedSha,162 path: RepoPath,163});164165export type MovedBy = typeof MovedBy.Type;166167export class StaleBase extends Schema.TaggedError<StaleBase>()("StaleBase", {168 ...Retryable,169 ...nextIs("git pull --rebase"),170 base: CommitSha,171 conflicts: Schema.Array(RepoPath),172 main: CommitSha,173 movedBy: Schema.optionalKey(Schema.NonEmptyArray(MovedBy)),174}) {}175176export class StaleLayer extends Schema.TaggedError<StaleLayer>()("StaleLayer", {177 ...Retryable,178 ...nextIs(cli("status")),179 layer: Label,180}) {}181182const LOCAL = "local:";183184export class ChecksFailed extends Schema.TaggedError<ChecksFailed>()(185 "ChecksFailed",186 {187 ...Final,188 lane: Label,189 logRef: Label,190 }191) {192 /** A lane whose log held a stored write-only value: the log was never kept. */193 static readonly withheld = "withheld";194195 static readonly localRef = (lane: string) => `${LOCAL}${lane}`;196197 static readonly isLocal = (logRef: string) => logRef.startsWith(LOCAL);198}199200/** A held-back check failed; only its area and suite are disclosed. */201export class HeldBackFailed extends Schema.TaggedError<HeldBackFailed>()(202 "HeldBackFailed",203 { ...Final, ...nextIs(cli("status")), area: Label, suite: Label }204) {}205206export class AttemptsExhausted extends Schema.TaggedError<AttemptsExhausted>()(207 "AttemptsExhausted",208 {209 ...Retryable,210 ...nextIs(cli("status")),211 area: Label,212 resetsAt: Schema.DateTimeUtcFromString,213 }214) {}215216export class Flake extends Schema.TaggedError<Flake>()("Flake", {217 ...Retryable,218 ...nextIs(cli("status")),219 lane: Label,220}) {}221222export class VerifierBlocked extends Schema.TaggedError<VerifierBlocked>()(223 "VerifierBlocked",224 { ...Retryable, ...nextIs(cli("status")) }225) {}226227export class NotRedFirst extends Schema.TaggedError<NotRedFirst>()(228 "NotRedFirst",229 {230 ...Final,231 ...nextIs(cli("check")),232 }233) {}234235export class WeakCheck extends Schema.TaggedError<WeakCheck>()("WeakCheck", {236 ...Final,237 ...nextIs(cli("check")),238 weakness: Label,239}) {}240241export class IncompleteBrief extends Schema.TaggedError<IncompleteBrief>()(242 "IncompleteBrief",243 { ...Final, ...nextIs(cli("check")), missing: Schema.NonEmptyArray(Label) }244) {}245246export class LayerContentInPublicTree extends Schema.TaggedError<LayerContentInPublicTree>()(247 "LayerContentInPublicTree",248 { ...Final, ...nextIs(cli("check")), paths: Paths }249) {}250251export class SecretInOutput extends Schema.TaggedError<SecretInOutput>()(252 "SecretInOutput",253 {254 ...Final,255 ...nextIs(cli("check")),256 item: Label,257 where: Schema.Literals(["output", "tree"]),258 }259) {}260261export class UnstatedRequirement extends Schema.TaggedError<UnstatedRequirement>()(262 "UnstatedRequirement",263 { ...Final, ...nextIs(cli("check")), area: Label }264) {}265266export class PostureChange extends Schema.TaggedError<PostureChange>()(267 "PostureChange",268 { ...Final, ...nextIs(cli("today")), from: Posture, to: Posture }269) {}270271export class NotDecider extends Schema.TaggedError<NotDecider>()("NotDecider", {272 ...Final,273 ...nextIs(cli("status")),274}) {}275276export class DecisionClosed extends Schema.TaggedError<DecisionClosed>()(277 "DecisionClosed",278 { ...Final, ...nextIs(cli("today")), decision: DecisionId }279) {}280281export class ProposalClosed extends Schema.TaggedError<ProposalClosed>()(282 "ProposalClosed",283 { ...Final, ...nextIs(cli("today")), proposal: ProposalId }284) {}285286export class DraftInProgress extends Schema.TaggedError<DraftInProgress>()(287 "DraftInProgress",288 { ...Retryable, proposal: ProposalId }289) {290 static readonly of = (proposal: ProposalId) =>291 new DraftInProgress({ next: cli("proposal", proposal), proposal });292}293294export class UnknownProject extends Schema.TaggedError<UnknownProject>()(295 "UnknownProject",296 { ...Final, ...nextIs(cli("asks")), project: ProjectName }297) {}298299export class UnknownAsk extends Schema.TaggedError<UnknownAsk>()("UnknownAsk", {300 ...Final,301 ...nextIs(cli("asks")),302 ask: AskId,303}) {}304305export class UnknownCommit extends Schema.TaggedError<UnknownCommit>()(306 "UnknownCommit",307 { ...Final, ...nextIs("git fetch"), sha: CommitSha }308) {}309310export class UnknownDecision extends Schema.TaggedError<UnknownDecision>()(311 "UnknownDecision",312 { ...Final, ...nextIs(cli("today")), decision: DecisionId }313) {}314315export class UnknownPath extends Schema.TaggedError<UnknownPath>()(316 "UnknownPath",317 { ...Final, ...nextIs("git ls-tree"), commit: CommitSha, path: RepoPath }318) {}319320export class UnknownRevision extends Schema.TaggedError<UnknownRevision>()(321 "UnknownRevision",322 { ...Final, ...nextIs("git ls-remote"), rev: Schema.String }323) {}324325export class UnknownProposal extends Schema.TaggedError<UnknownProposal>()(326 "UnknownProposal",327 { ...Final, ...nextIs(cli("today")), proposal: ProposalId }328) {}329330export class ProjectExists extends Schema.TaggedError<ProjectExists>()(331 "ProjectExists",332 { ...Final, ...nextIs(cli("asks")), project: ProjectName }333) {}334335export class NotYourNamespace extends Schema.TaggedError<NotYourNamespace>()(336 "NotYourNamespace",337 { ...Final, handle: Handle, project: ProjectName }338) {339 static readonly of = (project: ProjectName, handle: Handle) =>340 new NotYourNamespace({341 handle,342 next: cli(343 "init",344 "--project",345 `${handle.toLowerCase()}/${shortNameOf(project)}`346 ),347 project,348 });349}350351export class ImportFailed extends Schema.TaggedError<ImportFailed>()(352 "ImportFailed",353 { ...Retryable, ...nextIs(cli("init")), reason: Label }354) {}355356export class UnknownEnvItem extends Schema.TaggedError<UnknownEnvItem>()(357 "UnknownEnvItem",358 { ...Final, ...nextIs(cli("env", "shape")), key: EnvKey }359) {}360361export class NotSensitive extends Schema.TaggedError<NotSensitive>()(362 "NotSensitive",363 { ...Final, ...nextIs(cli("env", "shape")), key: EnvKey }364) {}365366/** Names the minimum only: never the value, nor its length. */367export class ValueTooShort extends Schema.TaggedError<ValueTooShort>()(368 "ValueTooShort",369 { ...Final, key: EnvKey, minimum: Schema.Int }370) {371 static readonly of = (key: EnvKey, minimum: number) =>372 new ValueTooShort({373 key,374 minimum,375 next: cli("env", "set", key, "--acknowledge-short"),376 });377378 static readonly belowFloor = (key: EnvKey, minimum: number) =>379 new ValueTooShort({ key, minimum, next: cli("env", "set", key) });380}381382const pushAndClaim = [383 NotClaimed,384 ClaimLapsed,385 NotVouched,386 CapReached,387 AskFull,388 NoAsk,389 RepoBusy,390 DirectPush,391 AskBlocked,392 Denounced,393] as const;394395// These accumulate, so one push hears about every one at once.396const accumulating = [397 OutOfScope,398 TouchedJudge,399 Weakened,400 PrivatePathInPublicTree,401 ReachedInternals,402] as const;403404const merge = [StaleBase, StaleLayer] as const;405406const checks = [ChecksFailed, HeldBackFailed, AttemptsExhausted] as const;407408const neverCounted = [Flake, VerifierBlocked] as const;409410const atAskOpen = [NotRedFirst, WeakCheck, IncompleteBrief] as const;411412const maintainersOnly = [413 LayerContentInPublicTree,414 SecretInOutput,415 UnstatedRequirement,416] as const;417418const decision = [419 PostureChange,420 NotDecider,421 DecisionClosed,422 ProposalClosed,423 DraftInProgress,424] as const;425426const lookup = [427 UnknownProject,428 UnknownAsk,429 UnknownCommit,430 UnknownDecision,431 UnknownPath,432 UnknownRevision,433 UnknownProposal,434] as const;435436const setup = [ProjectExists, NotYourNamespace, ImportFailed] as const;437438const env = [UnknownEnvItem, NotSensitive, ValueTooShort] as const;439440export const groups = [441 { members: pushAndClaim, name: "Push and claim" },442 { members: accumulating, name: "Shape" },443 { members: merge, name: "Merge" },444 { members: checks, name: "Checks" },445 { members: neverCounted, name: "Never counted against the answerer" },446 { members: atAskOpen, name: "At ask open" },447 { members: maintainersOnly, name: "Maintainers only" },448 { members: decision, name: "Decision" },449 { members: lookup, name: "Lookup" },450 { members: setup, name: "Setup" },451 { members: env, name: "Env" },452] as const;453454/** The closed union. Each member's tag is public: rename one only with an alias. */455export const Refusal = Schema.Union([456 ...pushAndClaim,457 ...accumulating,458 ...merge,459 ...checks,460 ...neverCounted,461 ...atAskOpen,462 ...maintainersOnly,463 ...decision,464 ...lookup,465 ...setup,466 ...env,467]);468469export type Refusal = typeof Refusal.Type;