Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
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=299&format=json
148 linesFirst import 97% · infra-split 1% · onboard 1%Last change onboard in version 209, 3 versions changed it

You're reading version 299. The newest is 318, and no version since then changed this file. Go to the newestCompare with 318

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 (Christopher, Oct 14: no cap; it was 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;
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>;
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[] };
47export interface RoundOptions {
48 /** Whether shared's newest version fails the check now. */
49 readonly sharedRed?: boolean;
50 readonly now?: () => number;
53export type RoundResult =
54 | { readonly kind: "idle" }
55 | { readonly kind: "landed"; readonly versions: { id: number; version: number }[]; readonly checks: number }
56 | {
57 readonly kind: "pulled";
58 readonly versions: { id: number; version: number }[];
59 readonly pulled: number[];
60 readonly output?: string;
61 readonly checks: number;
62 }
63 /** Shared is red and no prefix of the batch made it pass: nothing landed, nobody pulled. */
64 | { readonly kind: "held"; readonly checks: number; readonly output?: string }
65 /** The box failed twice in a row: nothing landed, nobody pulled. */
66 | { readonly kind: "paused"; readonly reason: string; readonly checks: number }
67 /**
68 * Shared changed while the box checked (a drop, or a push with plain git),
69 * so what it checked isn't what would land: nothing landed, nobody pulled.
70 * Run the round again.
71 */
72 | { readonly kind: "moved"; readonly checks: number };
74class Moved extends Error {}
76class Paused extends Error {
77 readonly reason: string;
78 constructor(reason: string) {
79 super(reason);
80 this.reason = reason;
81 }
84/** Run one round of the queue. Call it again while it answers `landed` or `pulled` and the queue isn't empty. */
85export async function queueRound(
86 engine: QueueEngine,
87 checker: Checker,
88 options: RoundOptions = {},
89): Promise<RoundResult> {
90 const waiting = engine.queue().items.length;
91 if (waiting === 0) return { kind: "idle" };
92 const now = options.now ?? Date.now;
93 const n = waiting;
94 let checks = 0;
95 let output: string | undefined;
96 /** The tree each prefix was checked as: a land or a pull goes ahead only on that same tree. */
97 const checked = new Map<number, string>();
98 const same = (k: number): void => {
99 if (engine.queueCandidate(k).tree !== checked.get(k)) throw new Moved();
100 };
101 const land = (k: number) => {
102 same(k);
103 return engine.queueLand(k, now()).versions;
104 };
105 /** Whether shared with the first `k` queued on top passes; a box failure is retried once. */
106 const passes = async (k: number): Promise<boolean> => {
107 let last: CheckResult | undefined;
108 for (let tries = 0; tries < 2; tries++) {
109 checks++;
110 const candidate = engine.queueCandidate(k);
111 checked.set(k, candidate.tree);
112 last = await checker.check(candidate);
113 if (last.kind !== "box") break;
114 }
115 if (last!.kind === "box") throw new Paused(last!.reason);
116 if (last!.kind === "fail") output = last!.output;
117 return last!.kind === "pass";
118 };
119 engine.queueChecking(n);
120 try {
121 if (options.sharedRed) {
122 for (let k = 1; k <= n; k++) if (await passes(k)) return { kind: "landed", versions: land(k), checks };
123 return { kind: "held", checks, ...(output !== undefined ? { output } : {}) };
124 }
125 if (await passes(n)) return { kind: "landed", versions: land(n), checks };
126 // Shared alone passes; shared plus all n fails. Find the longest passing prefix.
127 let good = 0;
128 let bad = n;
129 while (bad - good > 1) {
130 const mid = Math.floor((good + bad) / 2);
131 if (await passes(mid)) good = mid;
132 else bad = mid;
133 }
134 // The failing prefix fails twice before anyone is pulled.
135 if (await passes(bad)) return { kind: "landed", versions: land(bad), checks };
136 const culprit = engine.queue().items[bad - 1]!.id;
137 same(bad);
138 const versions = good > 0 ? land(good) : [];
139 const { pulled } = engine.queuePull(culprit, "failed", now(), output);
140 return { kind: "pulled", versions, pulled, checks, ...(output !== undefined ? { output } : {}) };
141 } catch (e) {
142 if (e instanceof Paused) return { kind: "paused", reason: e.reason, checks };
143 if (e instanceof Moved) return { kind: "moved", checks };
144 throw e;
145 } finally {
146 engine.queueChecking(0);
147 }