@operad-io/dev 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +12 -2
- package/dev.schema.json +631 -0
- package/flows/chat-worktree.flow.ts +18 -0
- package/flows/dev.flow.ts +7 -0
- package/package.json +29 -4
- package/src/actions.ts +591 -0
- package/src/announce.ts +225 -0
- package/src/chat.ts +234 -0
- package/src/check.ts +80 -0
- package/src/commands.ts +417 -0
- package/src/editor.ts +55 -0
- package/src/flaky.ts +716 -0
- package/src/flow-compiler.ts +858 -0
- package/src/followups.ts +223 -0
- package/src/gate.ts +2950 -0
- package/src/git.ts +138 -0
- package/src/index.ts +227 -0
- package/src/integrate.ts +1213 -0
- package/src/integration-queue.ts +393 -0
- package/src/lock.ts +36 -0
- package/src/mainclone.ts +102 -0
- package/src/marks.ts +126 -0
- package/src/model.ts +1171 -0
- package/src/notcode.ts +305 -0
- package/src/place-order.ts +161 -0
- package/src/place-wait.ts +64 -0
- package/src/places.ts +368 -0
- package/src/prepare.ts +106 -0
- package/src/progress.ts +115 -0
- package/src/project-flows.ts +160 -0
- package/src/project-settings.ts +192 -0
- package/src/refusals.ts +336 -0
- package/src/rerun.ts +390 -0
- package/src/reruns.ts +62 -0
- package/src/retry.ts +212 -0
- package/src/rework.ts +366 -0
- package/src/schedule.ts +1052 -0
- package/src/service.ts +2160 -0
- package/src/settings.ts +1495 -0
- package/src/standing.ts +87 -0
- package/src/stdin.ts +105 -0
- package/src/steps.ts +1775 -0
- package/src/stops.ts +103 -0
- package/src/throughput.ts +347 -0
- package/src/timeouts.ts +60 -0
- package/src/views.ts +1167 -0
- package/src/waits.ts +105 -0
package/src/notcode.ts
ADDED
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tells a check result that says nothing about the code from one that does,
|
|
3
|
+
* and runs a check so that the first kind is handled without the worker.
|
|
4
|
+
*
|
|
5
|
+
* A check result is "not the code" when any of these holds (the rules live
|
|
6
|
+
* here and nowhere else):
|
|
7
|
+
*
|
|
8
|
+
* 1. `limit`: the command was stopped at the gate's command time limit
|
|
9
|
+
* (the shell step's reason `timeout`).
|
|
10
|
+
* 2. `signal`: the command ended by a signal nobody in the task sent: the
|
|
11
|
+
* shell step's reason `signal`, or an exit status of 128+n (129 to 192), as
|
|
12
|
+
* a shell reports a child that a signal killed. A cancel is a different
|
|
13
|
+
* reason (`cancelled`) and never lands here.
|
|
14
|
+
* 3. `test-timeout`: the log names at least one failed test, and every one of
|
|
15
|
+
* them failed at the test timeout or later, within 5% below it. The test
|
|
16
|
+
* timeout is the `--timeout` of the failing command line, else 30 s. One
|
|
17
|
+
* failure that took less than that, or no failed test line at all, is an
|
|
18
|
+
* assertion (or some other failure): the worker's business.
|
|
19
|
+
* 4. `rerun-passed`: every test that failed in the first run passed on its
|
|
20
|
+
* rerun. The check then passes; the kind tells the flaky-test count whether
|
|
21
|
+
* the machine was to blame (see `isLoaded`).
|
|
22
|
+
*
|
|
23
|
+
* A `test-timeout` is code, not the machine, when the failed test was rerun
|
|
24
|
+
* alone, timed out again, and the load during that rerun stayed below the
|
|
25
|
+
* flaky load limit (`check.flakyLoadLimit`, default twice the cores): the
|
|
26
|
+
* result goes to the worker as a failure with a rework note.
|
|
27
|
+
*
|
|
28
|
+
* For a result of kind 1 to 3, `runCheckSteadily` waits until the machine's
|
|
29
|
+
* 1-minute load average is below the limit (`check.loadLimit`, default the
|
|
30
|
+
* number of cores), runs the check once more by itself, and reports a second
|
|
31
|
+
* result of that kind as `notCode`, which the steps turn into a wait for a
|
|
32
|
+
* person.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
import { machineLoad, type MachineLoad } from "@operad-io/engine/platform";
|
|
36
|
+
import { waitFor } from "./gate.ts";
|
|
37
|
+
import { flakyLoadLimitOf } from "./flaky.ts";
|
|
38
|
+
import { readSince, type CheckRun } from "./rerun.ts";
|
|
39
|
+
import { isRecord } from "@operad-io/engine";
|
|
40
|
+
|
|
41
|
+
/** The kind of a "not the code" result. */
|
|
42
|
+
export type NotCodeKind = "limit" | "signal" | "test-timeout" | "rerun-passed";
|
|
43
|
+
|
|
44
|
+
export interface NotCode {
|
|
45
|
+
readonly kind: NotCodeKind;
|
|
46
|
+
/** What happened, as a waiting line says it ("check timed out"). */
|
|
47
|
+
readonly what: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The test timeout when the command line names none (the repository's bunfig). */
|
|
51
|
+
export const DEFAULT_TEST_TIMEOUT_MS = 30_000;
|
|
52
|
+
|
|
53
|
+
/** How far below the timeout a failure may end and still count as a timeout. */
|
|
54
|
+
export const TEST_TIMEOUT_MARGIN = 0.05;
|
|
55
|
+
|
|
56
|
+
/** How often the wait for a quiet machine looks at the load again. */
|
|
57
|
+
export const LOAD_POLL_MS = 30_000;
|
|
58
|
+
|
|
59
|
+
/** Test-only: a load reader and a wait in place of the real ones. */
|
|
60
|
+
export const notCodeTestHooks: {
|
|
61
|
+
load?: (() => MachineLoad) | undefined;
|
|
62
|
+
wait?: ((ms: number, signal: AbortSignal | undefined) => Promise<boolean>) | undefined;
|
|
63
|
+
} = {};
|
|
64
|
+
|
|
65
|
+
export function currentLoad(): MachineLoad {
|
|
66
|
+
return (notCodeTestHooks.load ?? machineLoad)();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The load limit: the setting, else the number of cores. */
|
|
70
|
+
export function loadLimitOf(configured: number | undefined, load: MachineLoad): number {
|
|
71
|
+
return configured ?? load.cores;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** True when the machine is too busy to trust a check: load at or above the limit. */
|
|
75
|
+
export function isLoaded(load: MachineLoad, configured: number | undefined): boolean {
|
|
76
|
+
return load.load1 >= loadLimitOf(configured, load);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface FailedTest {
|
|
80
|
+
readonly name: string;
|
|
81
|
+
readonly ms: number;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const FAIL_LINE = /^\(fail\) (.+?) \[(\d+(?:\.\d+)?)(ms|s)\]\s*$/;
|
|
85
|
+
|
|
86
|
+
/** The failed tests a log lists as `(fail) name [time]` lines, each once, with how long it ran. */
|
|
87
|
+
export function failedTests(log: string): FailedTest[] {
|
|
88
|
+
const seen = new Map<string, number>();
|
|
89
|
+
for (const line of log.replace(/\r\n/g, "\n").split("\n")) {
|
|
90
|
+
const found = FAIL_LINE.exec(line);
|
|
91
|
+
if (found === null) continue;
|
|
92
|
+
const name = found[1] ?? "";
|
|
93
|
+
const amount = Number(found[2]);
|
|
94
|
+
const ms = found[3] === "s" ? amount * 1000 : amount;
|
|
95
|
+
if (!seen.has(name)) seen.set(name, ms);
|
|
96
|
+
}
|
|
97
|
+
return [...seen].map(([name, ms]) => ({ name, ms }));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** The `--timeout` of a command line in milliseconds; the default when it names none. */
|
|
101
|
+
export function testTimeoutOf(line: string | undefined): number {
|
|
102
|
+
const found = /--timeout(?:=|\s+)(\d+)/.exec(line ?? "");
|
|
103
|
+
const ms = found === null ? NaN : Number(found[1]);
|
|
104
|
+
return Number.isFinite(ms) && ms > 0 ? ms : DEFAULT_TEST_TIMEOUT_MS;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** What the classifier is given about one finished check. */
|
|
108
|
+
export interface CheckFacts {
|
|
109
|
+
/** The shell step's result: `pass`, `fail` or another. */
|
|
110
|
+
readonly result: string | undefined;
|
|
111
|
+
/** The shell step's reason for a failure: `exit`, `signal`, `timeout`, `cancelled`. */
|
|
112
|
+
readonly reason?: string | undefined;
|
|
113
|
+
readonly exitCode?: number | null | undefined;
|
|
114
|
+
readonly signal?: string | null | undefined;
|
|
115
|
+
/** The failing command line. */
|
|
116
|
+
readonly line?: string | undefined;
|
|
117
|
+
/** The check's own output, from where its last run began. */
|
|
118
|
+
readonly log: string;
|
|
119
|
+
/** The check failed once and passed when it, or its failed tests, ran again. */
|
|
120
|
+
readonly rerunPassed?: boolean | undefined;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Whether a check result says nothing about the code, and what it says instead. */
|
|
124
|
+
export function classifyCheck(facts: CheckFacts): NotCode | undefined {
|
|
125
|
+
if (facts.result === "pass") {
|
|
126
|
+
return facts.rerunPassed === true
|
|
127
|
+
? { kind: "rerun-passed", what: "a test passed on rerun" }
|
|
128
|
+
: undefined;
|
|
129
|
+
}
|
|
130
|
+
if (facts.result !== "fail") return undefined;
|
|
131
|
+
if (facts.reason === "timeout") return { kind: "limit", what: "check timed out" };
|
|
132
|
+
const code = facts.exitCode;
|
|
133
|
+
if (
|
|
134
|
+
facts.reason === "signal" ||
|
|
135
|
+
(facts.reason === "exit" && typeof code === "number" && code > 128 && code <= 192)
|
|
136
|
+
) {
|
|
137
|
+
const number = typeof code === "number" && code > 128 ? `signal ${code - 128}` : undefined;
|
|
138
|
+
const name = facts.signal ?? number ?? "a signal";
|
|
139
|
+
return { kind: "signal", what: `check was killed by ${name}` };
|
|
140
|
+
}
|
|
141
|
+
if (facts.reason !== "exit") return undefined;
|
|
142
|
+
const failed = failedTests(facts.log);
|
|
143
|
+
if (failed.length === 0) return undefined;
|
|
144
|
+
const timeout = testTimeoutOf(facts.line);
|
|
145
|
+
const floor = timeout * (1 - TEST_TIMEOUT_MARGIN);
|
|
146
|
+
if (failed.every((test) => test.ms >= floor)) {
|
|
147
|
+
return { kind: "test-timeout", what: "tests timed out" };
|
|
148
|
+
}
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** The facts of a finished check, read from its outcome and its log. */
|
|
153
|
+
export function factsOf(checked: CheckRun): CheckFacts {
|
|
154
|
+
const outcome = checked.outcome;
|
|
155
|
+
const data = outcome !== undefined && isRecord(outcome.data) ? outcome.data : {};
|
|
156
|
+
const log = typeof data.log === "string" ? data.log : undefined;
|
|
157
|
+
const start = typeof data.logStart === "number" ? data.logStart : 0;
|
|
158
|
+
return {
|
|
159
|
+
result: checked.hostError === undefined ? outcome?.result : "host-error",
|
|
160
|
+
reason: typeof data.reason === "string" ? data.reason : undefined,
|
|
161
|
+
exitCode: typeof data.exitCode === "number" ? data.exitCode : null,
|
|
162
|
+
signal: typeof data.signal === "string" ? data.signal : null,
|
|
163
|
+
line: typeof data.line === "string" ? data.line : undefined,
|
|
164
|
+
log: log === undefined ? "" : readSince(log, start),
|
|
165
|
+
rerunPassed: checked.hostError === undefined && checked.rerun && outcome?.result === "pass",
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** The kind of a finished check, or undefined when it says something about the code. */
|
|
170
|
+
export function classifyRun(checked: CheckRun): NotCode | undefined {
|
|
171
|
+
return classifyCheck(factsOf(checked));
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** True for the kinds that a quiet machine and another run may undo. */
|
|
175
|
+
function worthAnotherRun(found: NotCode | undefined): found is NotCode {
|
|
176
|
+
return found !== undefined && found.kind !== "rerun-passed";
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** The waiting line for a check that came out the same way twice. */
|
|
180
|
+
export function notCodeCause(found: NotCode, load: MachineLoad): string {
|
|
181
|
+
return `${found.what} twice under load ${Math.round(load.load1)} on ${load.cores} cores`;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** The load when one run of a command started and when it ended. */
|
|
185
|
+
export interface CheckLoad {
|
|
186
|
+
readonly start: MachineLoad;
|
|
187
|
+
readonly end: MachineLoad;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* The checked result with a note when it is a test that timed out when run
|
|
192
|
+
* alone under a load below the flaky limit; otherwise undefined.
|
|
193
|
+
*/
|
|
194
|
+
function timeoutAlone(
|
|
195
|
+
checked: CheckRun,
|
|
196
|
+
load: CheckLoad,
|
|
197
|
+
flakyLimit: number | undefined,
|
|
198
|
+
): CheckRun | undefined {
|
|
199
|
+
if (checked.aloneRerun !== true) return undefined;
|
|
200
|
+
const facts = factsOf(checked);
|
|
201
|
+
if (classifyCheck(facts)?.kind !== "test-timeout") return undefined;
|
|
202
|
+
const limit = flakyLoadLimitOf(flakyLimit, load.end);
|
|
203
|
+
// Busy means above the limit, as `flaky.ts` reads it. The reading spans the whole
|
|
204
|
+
// check run (full suite and alone rerun), so a quiet reading is a strict test.
|
|
205
|
+
if (load.start.load1 > limit || load.end.load1 > limit) return undefined;
|
|
206
|
+
const outcome = checked.outcome;
|
|
207
|
+
if (outcome === undefined) return undefined;
|
|
208
|
+
const data = isRecord(outcome.data) ? outcome.data : {};
|
|
209
|
+
const names = failedTests(facts.log).map((test) => test.name).join("; ");
|
|
210
|
+
const seconds = testTimeoutOf(facts.line) / 1000;
|
|
211
|
+
const peak = Math.max(load.start.load1, load.end.load1);
|
|
212
|
+
const log = typeof data.log === "string" ? `; log: ${data.log}` : "";
|
|
213
|
+
const message = `the failed test "${names}" timed out at ${seconds} s when run alone under load ${Number(peak.toFixed(1))} on ${load.end.cores} cores${log}`;
|
|
214
|
+
return { ...checked, outcome: { ...outcome, data: { ...data, message } } };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export interface SteadyCheck {
|
|
218
|
+
readonly checked: CheckRun;
|
|
219
|
+
/** Set when the check ended "not the code" twice: a person decides. */
|
|
220
|
+
readonly notCode?: { readonly cause: string; readonly kind: NotCodeKind } | undefined;
|
|
221
|
+
/** How many times the check ran (1 or 2). */
|
|
222
|
+
readonly runs: number;
|
|
223
|
+
/** The busiest the machine was when the check started and when each run ended. */
|
|
224
|
+
readonly load: MachineLoad;
|
|
225
|
+
/** The load at the start and the end of each run of the command. */
|
|
226
|
+
readonly loads: readonly CheckLoad[];
|
|
227
|
+
/** Set when the run was cancelled while it waited for the load. */
|
|
228
|
+
readonly cancelled?: boolean | undefined;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Waits until the load is below the limit; false when cancelled. */
|
|
232
|
+
async function waitForQuiet(
|
|
233
|
+
limit: number | undefined,
|
|
234
|
+
signal: AbortSignal | undefined,
|
|
235
|
+
onLog: (text: string) => Promise<unknown> | unknown,
|
|
236
|
+
): Promise<boolean> {
|
|
237
|
+
const wait = notCodeTestHooks.wait ?? waitFor;
|
|
238
|
+
let said = false;
|
|
239
|
+
for (;;) {
|
|
240
|
+
if (signal?.aborted === true) return false;
|
|
241
|
+
const load = currentLoad();
|
|
242
|
+
if (!isLoaded(load, limit)) return true;
|
|
243
|
+
if (!said) {
|
|
244
|
+
said = true;
|
|
245
|
+
try {
|
|
246
|
+
await onLog(
|
|
247
|
+
`check was not the code; waiting for the load (${load.load1}) to fall below ${loadLimitOf(limit, load)} on ${load.cores} cores`,
|
|
248
|
+
);
|
|
249
|
+
} catch {
|
|
250
|
+
// A note that could not be written does not change the wait.
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
if (!(await wait(LOAD_POLL_MS, signal))) return false;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Runs a check. A result that is not the code waits for a quiet machine and
|
|
259
|
+
* runs once more; the second run's result stands, and a second result that is
|
|
260
|
+
* not the code is reported in `notCode`.
|
|
261
|
+
*/
|
|
262
|
+
export async function runCheckSteadily(input: {
|
|
263
|
+
readonly run: () => Promise<CheckRun>;
|
|
264
|
+
readonly limit: number | undefined;
|
|
265
|
+
/** `check.flakyLoadLimit`: a timeout alone below it is the code's. */
|
|
266
|
+
readonly flakyLimit?: number | undefined;
|
|
267
|
+
readonly signal?: AbortSignal | undefined;
|
|
268
|
+
readonly log?: ((text: string) => Promise<unknown> | unknown) | undefined;
|
|
269
|
+
}): Promise<SteadyCheck> {
|
|
270
|
+
const firstStart = currentLoad();
|
|
271
|
+
let peak = firstStart;
|
|
272
|
+
const sample = (): MachineLoad => {
|
|
273
|
+
const now = currentLoad();
|
|
274
|
+
if (now.load1 > peak.load1) peak = now;
|
|
275
|
+
return now;
|
|
276
|
+
};
|
|
277
|
+
const first = await input.run();
|
|
278
|
+
const firstEnd = sample();
|
|
279
|
+
const loads: CheckLoad[] = [{ start: firstStart, end: firstEnd }];
|
|
280
|
+
const firstCode = timeoutAlone(first, { start: firstStart, end: firstEnd }, input.flakyLimit);
|
|
281
|
+
if (firstCode !== undefined) return { checked: firstCode, runs: 1, load: peak, loads };
|
|
282
|
+
if (!worthAnotherRun(classifyRun(first))) {
|
|
283
|
+
return { checked: first, runs: 1, load: peak, loads };
|
|
284
|
+
}
|
|
285
|
+
if (!(await waitForQuiet(input.limit, input.signal, input.log ?? (() => {})))) {
|
|
286
|
+
return { checked: first, runs: 1, load: peak, loads, cancelled: true };
|
|
287
|
+
}
|
|
288
|
+
const secondStart = sample();
|
|
289
|
+
const second = await input.run();
|
|
290
|
+
const after = sample();
|
|
291
|
+
loads.push({ start: secondStart, end: after });
|
|
292
|
+
const secondCode = timeoutAlone(second, { start: secondStart, end: after }, input.flakyLimit);
|
|
293
|
+
if (secondCode !== undefined) return { checked: secondCode, runs: 2, load: peak, loads };
|
|
294
|
+
const found = classifyRun(second);
|
|
295
|
+
if (!worthAnotherRun(found)) {
|
|
296
|
+
return { checked: second, runs: 2, load: peak, loads };
|
|
297
|
+
}
|
|
298
|
+
return {
|
|
299
|
+
checked: second,
|
|
300
|
+
runs: 2,
|
|
301
|
+
load: peak,
|
|
302
|
+
loads,
|
|
303
|
+
notCode: { cause: notCodeCause(found, after), kind: found.kind },
|
|
304
|
+
};
|
|
305
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The order in which waiting checks take places. Integration checks go before
|
|
3
|
+
* verify checks, so a verified task can reach main without waiting behind every
|
|
4
|
+
* task check on the machine; among each kind the order is first come, first
|
|
5
|
+
* served. A verify that has waited longer than `VERIFY_STARVATION_MS` goes
|
|
6
|
+
* before the integration checks, so no verify waits forever.
|
|
7
|
+
*
|
|
8
|
+
* Each waiting check keeps one small file per pool next to the place lock files,
|
|
9
|
+
* named by the pool, the time it began to wait, its pid and its kind, so
|
|
10
|
+
* separate processes agree on the order. A file whose process is dead is
|
|
11
|
+
* removed by whoever reads it, the same dead-pid rule the place locks use.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { mkdirSync, readdirSync, unlinkSync, writeFileSync } from "node:fs";
|
|
15
|
+
import { join } from "node:path";
|
|
16
|
+
import { isAlive } from "@operad-io/engine/platform";
|
|
17
|
+
|
|
18
|
+
/** What a check is for: an integration lands a verified task, a verify checks one. */
|
|
19
|
+
export type CheckKind = "integration" | "verify";
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* How long a verify may wait before it goes before the integration checks. It
|
|
23
|
+
* is longer than one integration check usually takes, so the integrations of a
|
|
24
|
+
* queue still drain first, and short enough that a verify cannot be starved for
|
|
25
|
+
* an afternoon by a stream of integrations.
|
|
26
|
+
*/
|
|
27
|
+
export const VERIFY_STARVATION_MS = 30 * 60 * 1000;
|
|
28
|
+
|
|
29
|
+
interface Entry {
|
|
30
|
+
readonly file: string;
|
|
31
|
+
readonly pool: string;
|
|
32
|
+
readonly kind: CheckKind;
|
|
33
|
+
readonly since: number;
|
|
34
|
+
readonly pid: number;
|
|
35
|
+
readonly nonce: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
let counter = 0;
|
|
39
|
+
const PREFIX = "queue-";
|
|
40
|
+
const SUFFIX = ".wait";
|
|
41
|
+
|
|
42
|
+
function parse(file: string): Entry | undefined {
|
|
43
|
+
if (!file.startsWith(PREFIX) || !file.endsWith(SUFFIX)) return undefined;
|
|
44
|
+
const parts = file.slice(PREFIX.length, -SUFFIX.length).split("-");
|
|
45
|
+
// New entries end in their kind; an entry written before kinds existed has
|
|
46
|
+
// none, and is a verify.
|
|
47
|
+
let kind: CheckKind = "verify";
|
|
48
|
+
const last = parts.at(-1);
|
|
49
|
+
if (last === "integration" || last === "verify") {
|
|
50
|
+
kind = last;
|
|
51
|
+
parts.pop();
|
|
52
|
+
}
|
|
53
|
+
const nonce = parts.pop();
|
|
54
|
+
const pid = Number(parts.pop());
|
|
55
|
+
const since = Number(parts.pop());
|
|
56
|
+
const pool = parts.join("-");
|
|
57
|
+
if (nonce === undefined || pool === "" || !Number.isInteger(pid) || !Number.isFinite(since)) {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
return { file, pool, kind, since, pid, nonce };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function starved(entry: Entry, now: number): boolean {
|
|
64
|
+
return entry.kind === "verify" && now - entry.since > VERIFY_STARVATION_MS;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function before(left: Entry, right: Entry, now: number): boolean {
|
|
68
|
+
const leftStarved = starved(left, now);
|
|
69
|
+
const rightStarved = starved(right, now);
|
|
70
|
+
if (leftStarved !== rightStarved) return leftStarved;
|
|
71
|
+
if (!leftStarved && left.kind !== right.kind) {
|
|
72
|
+
return left.kind === "integration";
|
|
73
|
+
}
|
|
74
|
+
if (left.since !== right.since) return left.since < right.since;
|
|
75
|
+
if (left.pid !== right.pid) return left.pid < right.pid;
|
|
76
|
+
return left.nonce < right.nonce;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The live entries of one pool; dead ones are removed. */
|
|
80
|
+
function live(directory: string, pool: string): Entry[] {
|
|
81
|
+
let files: string[];
|
|
82
|
+
try {
|
|
83
|
+
files = readdirSync(directory);
|
|
84
|
+
} catch {
|
|
85
|
+
return [];
|
|
86
|
+
}
|
|
87
|
+
const entries: Entry[] = [];
|
|
88
|
+
for (const file of files) {
|
|
89
|
+
const entry = parse(file);
|
|
90
|
+
if (entry === undefined || entry.pool !== pool) continue;
|
|
91
|
+
if (!isAlive(entry.pid)) {
|
|
92
|
+
try {
|
|
93
|
+
unlinkSync(join(directory, file));
|
|
94
|
+
} catch {
|
|
95
|
+
// Another reader removed it first.
|
|
96
|
+
}
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
entries.push(entry);
|
|
100
|
+
}
|
|
101
|
+
return entries;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** One check's place in the order of one pool. */
|
|
105
|
+
export class WaitEntry {
|
|
106
|
+
private readonly file: string;
|
|
107
|
+
private removed = false;
|
|
108
|
+
|
|
109
|
+
private constructor(
|
|
110
|
+
private readonly directory: string,
|
|
111
|
+
private readonly pool: string,
|
|
112
|
+
private readonly kind: CheckKind,
|
|
113
|
+
readonly since: number,
|
|
114
|
+
private readonly nonce: string,
|
|
115
|
+
) {
|
|
116
|
+
this.file = `${PREFIX}${pool}-${String(since).padStart(16, "0")}-${process.pid}-${nonce}-${kind}${SUFFIX}`;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Joins the order of `pool` as having begun to wait at `since`. */
|
|
120
|
+
static join(
|
|
121
|
+
directory: string,
|
|
122
|
+
pool: string,
|
|
123
|
+
since: number,
|
|
124
|
+
kind: CheckKind = "verify",
|
|
125
|
+
): WaitEntry {
|
|
126
|
+
mkdirSync(directory, { recursive: true });
|
|
127
|
+
// Checks of one process that began in the same millisecond keep their call order.
|
|
128
|
+
counter += 1;
|
|
129
|
+
const nonce = String(counter).padStart(10, "0");
|
|
130
|
+
const entry = new WaitEntry(directory, pool, kind, since, nonce);
|
|
131
|
+
writeFileSync(join(directory, entry.file), "");
|
|
132
|
+
return entry;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** How many live checks go before this one for the pool. */
|
|
136
|
+
ahead(now: number = Date.now()): number {
|
|
137
|
+
const mine = parse(this.file);
|
|
138
|
+
if (mine === undefined) return 0;
|
|
139
|
+
return live(this.directory, this.pool).filter((other) => before(other, mine, now)).length;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** How many live integration checks go before this one. */
|
|
143
|
+
integrationsAhead(now: number = Date.now()): number {
|
|
144
|
+
const mine = parse(this.file);
|
|
145
|
+
if (mine === undefined) return 0;
|
|
146
|
+
return live(this.directory, this.pool).filter(
|
|
147
|
+
(other) => other.kind === "integration" && before(other, mine, now),
|
|
148
|
+
).length;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Leaves the order; safe to call again. */
|
|
152
|
+
leave(): void {
|
|
153
|
+
if (this.removed) return;
|
|
154
|
+
this.removed = true;
|
|
155
|
+
try {
|
|
156
|
+
unlinkSync(join(this.directory, this.file));
|
|
157
|
+
} catch {
|
|
158
|
+
// Already removed, for example by a reader that saw this process dead.
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The task's own word while its step waits for a check place: the attempt
|
|
3
|
+
* records which place group it waits for and how many waiters are ahead, and
|
|
4
|
+
* the row's note and mark read it. The step clears it when the place is taken.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { StepContext } from "@operad-io/engine";
|
|
8
|
+
import { taskData, type TaskData } from "./model.ts";
|
|
9
|
+
import { saveTaskGuarded } from "./steps.ts";
|
|
10
|
+
|
|
11
|
+
/** A task's wait for a check place, as the attempt records it. */
|
|
12
|
+
export interface PlaceWait {
|
|
13
|
+
readonly key: string;
|
|
14
|
+
/** When this wait began; waiters of one group are served in this order. */
|
|
15
|
+
readonly since: number;
|
|
16
|
+
readonly ahead: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** What the row says while a step waits for a check place. */
|
|
20
|
+
export function placeWaitNote(task: TaskData): string | undefined {
|
|
21
|
+
const wait = task.attempt?.placeWait;
|
|
22
|
+
if (wait === undefined || wait === null) return undefined;
|
|
23
|
+
return `waiting for a ${wait.key}${wait.ahead > 0 ? `, ${wait.ahead} ahead` : ""}`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Records on the task that its step waits for `key` (with the checks ahead of
|
|
28
|
+
* it in the order of the place files), or clears the record when `key` is null.
|
|
29
|
+
* Writes only when the record changes, and only while the step still owns its
|
|
30
|
+
* claim.
|
|
31
|
+
*/
|
|
32
|
+
export async function taskPlaceWait(
|
|
33
|
+
ctx: StepContext,
|
|
34
|
+
taskKey: string,
|
|
35
|
+
key: string | null,
|
|
36
|
+
order?: { readonly since: number; readonly ahead: number },
|
|
37
|
+
): Promise<void> {
|
|
38
|
+
const object = await ctx.store.get("task", taskKey);
|
|
39
|
+
if (object === undefined) return;
|
|
40
|
+
const task = taskData(object.data);
|
|
41
|
+
const attempt = task.attempt;
|
|
42
|
+
// A step with no attempt has no row to mark.
|
|
43
|
+
if (attempt === undefined) return;
|
|
44
|
+
const current = attempt.placeWait ?? null;
|
|
45
|
+
if (key === null) {
|
|
46
|
+
if (current === null) return;
|
|
47
|
+
await saveTaskGuarded(
|
|
48
|
+
ctx,
|
|
49
|
+
taskKey,
|
|
50
|
+
{ ...task, attempt: { ...attempt, placeWait: null } },
|
|
51
|
+
"dev:place-wait",
|
|
52
|
+
);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const since = order?.since ?? (current?.key === key ? current.since : Date.now());
|
|
56
|
+
const ahead = order?.ahead ?? 0;
|
|
57
|
+
if (current?.key === key && current.ahead === ahead) return;
|
|
58
|
+
await saveTaskGuarded(
|
|
59
|
+
ctx,
|
|
60
|
+
taskKey,
|
|
61
|
+
{ ...task, attempt: { ...attempt, placeWait: { key, since, ahead } } },
|
|
62
|
+
"dev:place-wait",
|
|
63
|
+
);
|
|
64
|
+
}
|