// A drill's write lane: the app's actions an anonymous caller may POST, each request carrying a // fresh input drawn as the exercise pass draws one (the seeded draw, then the app's `exercise.ts` // override). It is how a drill's load reaches the queue — a job enqueued over HTTP is what // `worker-sigkill` and `worker-sigterm` need on the table to have anything to reclaim. import { bindDrawn } from '@ultimat3/core'; import type { StandardSchemaV1 } from '@ultimat3/schema'; import type { ExerciseOverrides } from './define-exercise'; import { overrideFor } from './define-exercise'; import type { DrillTarget } from './drill-load'; import type { Sampler } from './exercise-input'; import { loadSampler } from './exercise-input'; import { isAsyncInput } from './exercise-override-input'; /** The seed every drill draws under: one run's inputs are the same on every machine. */ export const DRILL_DRAW_SEED = 40; /** An action as the served route table states it. */ export interface DrillWriteCandidate { readonly name: string; readonly method: string; readonly path: string; /** The policy admits a caller with no session (`auth: 'public'` on its route). */ readonly anonymous: boolean; readonly input: StandardSchemaV1; } export interface DrillWritesOptions { readonly actions: readonly DrillWriteCandidate[]; readonly overrides?: ExerciseOverrides | undefined; /** The exercise pass's own sampler, loaded on demand when not handed in. */ readonly sampler?: Pick | undefined; } export interface DrillWrites { readonly targets: readonly DrillTarget[]; /** Every action left out, and why: printed, so an empty lane is never a silent one. */ readonly skipped: readonly { readonly name: string; readonly reason: string }[]; } const ASYNC_INPUT = 'exercise.ts computes its input from the pass (an async input); a load has none'; type Verdict = | { readonly reason: string } | { readonly bind: ((drawn: unknown) => unknown) | undefined }; /** Why `candidate` cannot ride an anonymous load, or the override that binds its input. */ function verdictOf( candidate: DrillWriteCandidate, overrides: ExerciseOverrides | undefined, ): Verdict { if (candidate.method !== 'POST') return { reason: `answers ${candidate.method}; the load posts` }; if (/[:*]/.test(candidate.path)) return { reason: 'its path takes a parameter the load cannot bind' }; if (!candidate.anonymous) return { reason: 'its policy refuses an anonymous caller' }; // Kind-aware, as the pass: an `'action:'` entry wins over a bare one a job may share. const override = overrideFor(overrides, 'action', candidate.name); if (override === undefined) return { bind: undefined }; if ('off' in override) return { reason: `exercise.ts turns it off: ${override.off.trim()}` }; if (override.actor !== null) return { reason: 'exercise.ts runs it as a signed-in actor' }; if (isAsyncInput(override)) { return { reason: 'exercise.ts computes its input from the pass (an async input); a load has none', }; } return { bind: bindDrawn(override.input) }; } const isThenable = (value: unknown): value is PromiseLike => (typeof value === 'object' || typeof value === 'function') && value !== null && typeof (value as { then?: unknown }).then === 'function'; /** * The first request's input, drawn ahead of the load. A thenable's rejection is handled: its * outcome is discarded — the action is skipped either way — and unhandled it would end the drill. * A throw answers `undefined`: the load's own draw throws it again on every request, as before. */ function probe( bind: (drawn: unknown) => unknown, sampler: Pick, candidate: DrillWriteCandidate, ): unknown { try { const first = bind(sampler.arbitrary(candidate.input, { seed: DRILL_DRAW_SEED, run: 1 })); if (isThenable(first)) Promise.resolve(first).catch(() => undefined); return first; } catch { return undefined; } } /** The write targets of an anonymous load, and every action it leaves out. */ export async function drillWriteTargets(options: DrillWritesOptions): Promise { const sampler = options.sampler ?? (await loadSampler()); const targets: DrillTarget[] = []; const skipped: { name: string; reason: string }[] = []; for (const candidate of options.actions) { const verdict = verdictOf(candidate, options.overrides); if ('reason' in verdict) { skipped.push({ name: candidate.name, reason: verdict.reason }); continue; } const { bind } = verdict; // `isAsyncInput` reads the function's tag, and a plain function returning a promise has none: // drawn, its body was `JSON.stringify(promise)` — `{}`. So the first request's input is drawn // here, and one that is a thenable is the same skip. if (bind !== undefined && isThenable(probe(bind, sampler, candidate))) { skipped.push({ name: candidate.name, reason: ASYNC_INPUT }); continue; } targets.push({ method: 'POST', path: candidate.path, // Run n of the seeded draw: a distinct input per request, so a job keyed by its input is a // distinct job — the same body every time is one job deduped, and nothing to reclaim. draw: (run) => { const drawn = sampler.arbitrary(candidate.input, { seed: DRILL_DRAW_SEED, run }); return bind === undefined ? drawn : bind(drawn); }, }); } return { targets, skipped }; }