outbox.ts
This file
Path
packages/project/src/outbox.ts
288 lines · 8.3 KiB · TypeScript
Last changed by
Merge observability into mainoscabrielbefore adoption1985111
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289import { RefName, RepoHost } from "@repo/artifacts/repo-host";import { Verdict } from "@repo/protocol/domain/answer";import type { ProjectEvent } from "@repo/protocol/domain/event";import { CommitSha, Handle, LandedSha, RepoName, RequestId, TreeSha,} from "@repo/protocol/ids";import type { ProjectName } from "@repo/protocol/ids";import { Array, DateTime, Effect, Option, Order, Schema, Struct } from "effect";
import { AnswerRuns } from "./answer-runs.ts";import type { AnswerQueue } from "./answer-runs.ts";import { DraftParams, DraftRuns } from "./draft-runs.ts";import { Lander, Rebuild } from "./lander.ts";import { backoff } from "./landing.ts";import { MetricPoint, Metrics } from "./metrics.ts";import type { OutboxRow } from "./project-store/rows.ts";import { Projection, ProjectedEvent } from "./projection.ts";import { ProjectVisibility, VisibilityEffect } from "./visibility-port.ts";
const mainRef = RefName.make("refs/heads/main");
export const LandAgain = Schema.Struct({ author: Handle, base: CommitSha, fork: RepoName, head: CommitSha, main: LandedSha, message: Schema.String, repo: RepoName, requestId: RequestId, tree: TreeSha, verdict: Schema.optionalKey(Verdict),});
export type LandAgain = typeof LandAgain.Type;
export const OutboxBody = Schema.Union([ ProjectedEvent, Schema.TaggedStruct("MakeFork", { from: RepoName, name: RepoName }), Schema.TaggedStruct("StartAnswer", { requestId: RequestId }), Schema.TaggedStruct("Bookkeeping", { job: LandAgain }), Schema.TaggedStruct("Visibility", { effect: VisibilityEffect }), Schema.TaggedStruct("StartDraft", DraftParams.fields), Schema.TaggedStruct("Measured", { points: Schema.NonEmptyArray(MetricPoint), }),]).pipe(Schema.toTaggedUnion("_tag"));
export type OutboxBody = typeof OutboxBody.Type;
const BodyJson = Schema.toCodecJson(OutboxBody);
const encodeBody = Schema.encodeEffect(BodyJson);
const decodeBody = Schema.decodeUnknownEffect(BodyJson);
export const outboxRow = ( id: string, body: OutboxBody, notBefore: DateTime.Utc): Effect.Effect<OutboxRow> => Effect.map(Effect.orDie(encodeBody(body)), (encoded) => ({ attempts: 0, body: encoded, id, kind: body._tag, notBefore, }));
export const projectedRows = ( project: ProjectName, events: readonly ProjectEvent[], now: DateTime.Utc): Effect.Effect<readonly OutboxRow[]> => Effect.forEach(events, (event) => outboxRow( `projected:${event.seq}`, ProjectedEvent.make({ event, project }), now ) );
export const startRow = (requestId: RequestId, at: DateTime.Utc) => outboxRow( `start:${requestId}:${DateTime.toEpochMillis(at)}`, OutboxBody.cases.StartAnswer.make({ requestId }), at );
export const draftRow = (params: DraftParams, at: DateTime.Utc) => outboxRow( `draft:${params.proposal}:r${params.round}`, OutboxBody.cases.StartDraft.make(params), at );
/** * Written in the commit that holds the fact, under an id the fact alone * names, so a replay that changes nothing writes no row and the same fact * kept twice is one row. */export const measuredRow = ( fact: string, points: readonly MetricPoint[], at: DateTime.Utc): Effect.Effect<readonly OutboxRow[]> => Array.isReadonlyArrayNonEmpty(points) ? Effect.map( outboxRow( `measured:${fact}`, OutboxBody.cases.Measured.make({ points }), at ), Array.of ) : Effect.succeed([]);
export const isDue = (row: OutboxRow, now: DateTime.Utc) => DateTime.isLessThanOrEqualTo(row.notBefore, now);
export interface OutboxRun { readonly done: readonly string[]; readonly retry: readonly OutboxRow[];}
const later = (row: OutboxRow, now: DateTime.Utc): OutboxRow => ({ ...row, attempts: row.attempts + 1, notBefore: DateTime.addDuration(now, backoff(row.attempts)),});
interface Decoded { readonly body: OutboxBody; readonly row: OutboxRow;}
const ran = (row: OutboxRow, ok: boolean, now: DateTime.Utc): OutboxRun => ok ? { done: [row.id], retry: [] } : { done: [], retry: [later(row, now)] };
const all = (runs: readonly OutboxRun[]): OutboxRun => ({ done: runs.flatMap(({ done }) => done), retry: runs.flatMap(({ retry }) => retry),});
// Artifacts may answer a fork that worked with an error: a fork that exists// and has `main` is made.const fork = (from: RepoName, name: RepoName) => RepoHost.use((host) => host.fork(from, name).pipe( Effect.as(true), Effect.catchTag("RepoExists", () => host.refs(name).pipe( Effect.map((refs) => refs.has(mainRef)), Effect.orElseSucceed(() => false) ) ), Effect.orElseSucceed(() => false) ) );
const landAgain = (job: LandAgain) => Lander.use((lander) => lander.land({ author: job.author, main: job.main, message: job.message, rebuild: Rebuild.Merge({ base: job.base, fork: job.fork, head: job.head, }), repo: job.repo, requestId: job.requestId, tree: job.tree, verdict: Option.fromUndefinedOr(job.verdict), }) ).pipe( Effect.map(({ pending }) => pending.length === 0), Effect.orElseSucceed(() => false) );
const bySeq = Order.mapInput(Order.Number, ({ body }: Decoded) => OutboxBody.guards.Projected(body) ? body.event.seq : 0);
// Answer runs go last: they may run long, and settle their own rows.export const runOutbox = ( rows: readonly OutboxRow[], queue: AnswerQueue, now: DateTime.Utc, // Takes rows out of the store before they run, naming those it still held. claim: (ids: readonly string[]) => Effect.Effect<readonly string[]>): Effect.Effect< OutboxRun, never, AnswerRuns | Lander | Projection | RepoHost> => Effect.gen(function* () { const decoded = yield* Effect.forEach(rows, (row) => Effect.map(Effect.orDie(decodeBody(row.body)), (body) => ({ body, row })) );
const of = <Tag extends OutboxBody["_tag"]>(tag: Tag) => decoded.filter( ( each ): each is Decoded & { readonly body: Extract<OutboxBody, { readonly _tag: Tag }>; } => OutboxBody.guards[tag](each.body) );
const projected = Array.sort(of("Projected"), bySeq);
const delivered = Array.isReadonlyArrayNonEmpty(projected) ? yield* Projection.use((projection) => projection.apply(projected.map(({ row }) => row)) ).pipe( Effect.as(true), Effect.catchTag("ProjectionFailed", (failed) => Effect.as( Effect.logWarning("the projection refused rows", failed), false ) ) ) : true;
const forks = yield* Effect.forEach(of("MakeFork"), ({ body, row }) => Effect.map(fork(body.from, body.name), (ok) => ran(row, ok, now)) );
const kept = yield* Effect.forEach(of("Bookkeeping"), ({ body, row }) => Effect.map(landAgain(body.job), (ok) => ran(row, ok, now)) );
const visibility = yield* ProjectVisibility;
const planned = yield* Effect.forEach(of("Visibility"), ({ body, row }) => Effect.map(visibility.settle(body.effect), (ok) => ran(row, ok, now)) );
const draftRuns = yield* DraftRuns;
const drafts = yield* Effect.forEach(of("StartDraft"), ({ body, row }) => Effect.as( draftRuns.start(Struct.omit(body, ["_tag"])), ran(row, true, now) ) );
const metrics = yield* Metrics;
// A point is written at most once, and never retried: the dataset adds // every point it takes. Its row leaves the store first, so of a call's // settle and the alarm's, both holding it, only the one that took it // writes its points. const taken = new Set( yield* claim(of("Measured").map(({ row }) => row.id)) );
const measured = yield* Effect.forEach( of("Measured").filter(({ row }) => taken.has(row.id)), ({ body, row }) => Effect.as(metrics.write(body.points), ran(row, true, now)) );
const runs = yield* Effect.forEach(of("StartAnswer"), ({ body, row }) => Effect.as( AnswerRuns.use((answers) => answers.start(body.requestId, queue)), ran(row, true, now) ) );
return all([ ...projected.map(({ row }) => ran(row, delivered, now)), ...forks, ...kept, ...planned, ...drafts, ...measured, ...runs, ]); });