- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/queue-driver.ts?version=37&format=json
150 linesFirst import 99% · infra-split 1%Last change infra-split in version 37, 2 versions changed it
You're reading version 37. The newest is 318, and 1 version since then changed this file.
1// The merge queue's checker loop (#39): one round takes a batch from the
2// front of the queue, runs the project's check on shared with the batch on
3// top, and lands it or finds the publish that turned it red. The check itself
4// is behind an interface (a CI runner on the server, a function in tests),
5// so this file holds only the rules agreed on #39:
6//
7// - A batch is everything waiting, in order, up to `cap` (8).
8// - Pass: every publish in the batch lands, each as its own version.
9// - Fail: halve prefixes to find the passing prefix and the first publish
10// that fails on top of it. That failing prefix is run once more; only a
11// second failure pulls the publish. The passing prefix lands at once.
12// - A timeout is a check failure. A box failure (never started, box lost,
13// no exit code) pulls nobody: the run is retried once, then the queue pauses.
14// - While shared is red, prefixes are tried one publish longer at a time,
15// and the first that passes lands: it holds the fix, with whatever came
16// before it. Nobody is pulled, since on a red base every prefix fails.
17import type { Blobs, Modes, PullReason } from "./project.ts";
19export interface Candidate {
20 readonly ids: number[];
21 readonly files: Record<string, string>;
22 readonly modes: Modes;
23 readonly blobs: Blobs;
24 readonly tree: string;
25}
27export type CheckResult =
28 | { readonly kind: "pass" }
29 /** The check failed or ran past its limit. `output` is its end, for the publisher. */
30 | { readonly kind: "fail"; readonly output?: string }
31 /** The box failed: the run never started, the box was lost, or there was no exit code. */
32 | { readonly kind: "box"; readonly reason: string };
34export interface Checker {
35 check(candidate: Candidate): Promise<CheckResult>;
36}
38/** The engine calls the loop needs: Project has them all. */
39export interface QueueEngine {
40 queue(): { items: { id: number }[] };
41 queueChecking(n: number): number[];
42 queueCandidate(n: number): Candidate;
43 queueLand(n: number, time: number): { versions: { id: number; version: number }[] };
44 queuePull(id: number, reason: Exclude<PullReason, "depends">, time: number, output?: string): { pulled: number[] };
45}
47export interface RoundOptions {
48 /** The most publishes in one batch. */
49 readonly cap?: number;
50 /** Whether shared's newest version fails the check now. */
51 readonly sharedRed?: boolean;
52 readonly now?: () => number;
53}
55export type RoundResult =
56 | { readonly kind: "idle" }
57 | { readonly kind: "landed"; readonly versions: { id: number; version: number }[]; readonly checks: number }
58 | {
59 readonly kind: "pulled";
60 readonly versions: { id: number; version: number }[];
61 readonly pulled: number[];
62 readonly output?: string;
63 readonly checks: number;
64 }
65 /** Shared is red and no prefix of the batch made it pass: nothing landed, nobody pulled. */
66 | { readonly kind: "held"; readonly checks: number; readonly output?: string }
67 /** The box failed twice in a row: nothing landed, nobody pulled. */
68 | { readonly kind: "paused"; readonly reason: string; readonly checks: number }
69 /**
70 * Shared changed while the box checked (a drop, or a push with plain git),
71 * so what it checked isn't what would land: nothing landed, nobody pulled.
72 * Run the round again.
73 */
74 | { readonly kind: "moved"; readonly checks: number };
76class Moved extends Error {}
78class Paused extends Error {
79 readonly reason: string;
80 constructor(reason: string) {
81 super(reason);
82 this.reason = reason;
83 }
84}
86/** Run one round of the queue. Call it again while it answers `landed` or `pulled` and the queue isn't empty. */
87export async function queueRound(
88 engine: QueueEngine,
89 checker: Checker,
90 options: RoundOptions = {},
91): Promise<RoundResult> {
92 const waiting = engine.queue().items.length;
93 if (waiting === 0) return { kind: "idle" };
94 const now = options.now ?? Date.now;
95 const n = Math.min(Math.max(1, options.cap ?? 8), waiting);
96 let checks = 0;
97 let output: string | undefined;
98 /** The tree each prefix was checked as: a land or a pull goes ahead only on that same tree. */
99 const checked = new Map<number, string>();
100 const same = (k: number): void => {
101 if (engine.queueCandidate(k).tree !== checked.get(k)) throw new Moved();
102 };
103 const land = (k: number) => {
104 same(k);
105 return engine.queueLand(k, now()).versions;
106 };
107 /** Whether shared with the first `k` queued on top passes; a box failure is retried once. */
108 const passes = async (k: number): Promise<boolean> => {
109 let last: CheckResult | undefined;
110 for (let tries = 0; tries < 2; tries++) {
111 checks++;
112 const candidate = engine.queueCandidate(k);
113 checked.set(k, candidate.tree);
114 last = await checker.check(candidate);
115 if (last.kind !== "box") break;
116 }
117 if (last!.kind === "box") throw new Paused(last!.reason);
118 if (last!.kind === "fail") output = last!.output;
119 return last!.kind === "pass";
120 };
121 engine.queueChecking(n);
122 try {
123 if (options.sharedRed) {
124 for (let k = 1; k <= n; k++) if (await passes(k)) return { kind: "landed", versions: land(k), checks };
125 return { kind: "held", checks, ...(output !== undefined ? { output } : {}) };
126 }
127 if (await passes(n)) return { kind: "landed", versions: land(n), checks };
128 // Shared alone passes; shared plus all n fails. Find the longest passing prefix.
129 let good = 0;
130 let bad = n;
131 while (bad - good > 1) {
132 const mid = Math.floor((good + bad) / 2);
133 if (await passes(mid)) good = mid;
134 else bad = mid;
135 }
136 // The failing prefix fails twice before anyone is pulled.
137 if (await passes(bad)) return { kind: "landed", versions: land(bad), checks };
138 const culprit = engine.queue().items[bad - 1]!.id;
139 same(bad);
140 const versions = good > 0 ? land(good) : [];
141 const { pulled } = engine.queuePull(culprit, "failed", now(), output);
142 return { kind: "pulled", versions, pulled, checks, ...(output !== undefined ? { output } : {}) };
143 } catch (e) {
144 if (e instanceof Paused) return { kind: "paused", reason: e.reason, checks };
145 if (e instanceof Moved) return { kind: "moved", checks };
146 throw e;
147 } finally {
148 engine.queueChecking(0);
149 }
150}