@operad-io/dev 0.0.0-stage → 0.0.1
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 +795 -2
- package/dev.schema.json +602 -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 +2897 -0
- package/src/git.ts +138 -0
- package/src/index.ts +226 -0
- package/src/integrate.ts +1201 -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 +1165 -0
- package/src/notcode.ts +266 -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 +185 -0
- package/src/refusals.ts +336 -0
- package/src/rerun.ts +378 -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 +1463 -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/flaky.ts
ADDED
|
@@ -0,0 +1,716 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Proposes a task when a test keeps passing only on its rerun. Each pass on a
|
|
3
|
+
* rerun is remembered per repository and test; when the count reaches
|
|
4
|
+
* `check.flakyTask` and no open task names the test, one task is proposed in
|
|
5
|
+
* the plan `check.flakyPlan` names, else the repository's own plan `flaky-tests`.
|
|
6
|
+
*
|
|
7
|
+
* The proposal runs under the repository lock and finds an earlier proposal by
|
|
8
|
+
* the test name in the open tasks, so a crash between the decision and the
|
|
9
|
+
* proposal leaves nothing half done: the next call decides again and proposes
|
|
10
|
+
* exactly one.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { appendFileSync, readdirSync, readFileSync, realpathSync } from "node:fs";
|
|
14
|
+
import { isAbsolute, join, relative } from "node:path";
|
|
15
|
+
import type { ObjectStore, StoredObject } from "@operad-io/engine";
|
|
16
|
+
import { isRecord, messageOf, type SecretStore } from "@operad-io/engine";
|
|
17
|
+
import type { MachineLoad } from "@operad-io/engine/platform";
|
|
18
|
+
import type { CheckLoad } from "./notcode.ts";
|
|
19
|
+
import { bareId, changeClock, isOpenTask, keyRepository, listAll, parseId, planData, planState, splitBrief, taskData, type PlanData, type RepositoryData } from "./model.ts";
|
|
20
|
+
import { anchoredPattern, shellQuote } from "./rerun.ts";
|
|
21
|
+
import type { FailedRerun } from "./settings.ts";
|
|
22
|
+
import { addConfirmedPlanLocked, addTaskLocked, withRepoLock } from "./service.ts";
|
|
23
|
+
|
|
24
|
+
const MASK = "***";
|
|
25
|
+
const SECRET_NAME = /(KEY|TOKEN|SECRET|PASS|PASSWORD|AUTH|CREDENTIAL)/i;
|
|
26
|
+
const SECRET_MIN_CHARS = 6;
|
|
27
|
+
|
|
28
|
+
/** The text with each known secret value, and each secret-named environment value, shown as `***`. */
|
|
29
|
+
export function maskSecrets(
|
|
30
|
+
text: string,
|
|
31
|
+
secrets: readonly string[],
|
|
32
|
+
env: Readonly<Record<string, string | undefined>> = process.env,
|
|
33
|
+
): string {
|
|
34
|
+
const values = new Set<string>();
|
|
35
|
+
for (const value of secrets) if (value.length > 0) values.add(value);
|
|
36
|
+
for (const [name, value] of Object.entries(env)) {
|
|
37
|
+
if (value !== undefined && value.length >= SECRET_MIN_CHARS && SECRET_NAME.test(name)) {
|
|
38
|
+
values.add(value);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
let out = text;
|
|
42
|
+
for (const value of [...values].sort((x, y) => y.length - x.length)) {
|
|
43
|
+
out = out.split(value).join(MASK);
|
|
44
|
+
}
|
|
45
|
+
return out;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const FLAKY_TYPE = "flaky-test";
|
|
49
|
+
const EXCERPT_LINES = 30;
|
|
50
|
+
const SHOWN_FAILURES = 2;
|
|
51
|
+
const SHOWN_TIMES = 20;
|
|
52
|
+
|
|
53
|
+
/** Test-only: set by a test to stop between the decision and the proposal. */
|
|
54
|
+
export const flakyTestHooks: {
|
|
55
|
+
beforePropose?: ((name: string) => Promise<void> | void) | undefined;
|
|
56
|
+
} = {};
|
|
57
|
+
|
|
58
|
+
export interface FlakyOccurrence {
|
|
59
|
+
readonly task: string;
|
|
60
|
+
readonly at: number;
|
|
61
|
+
readonly file: string | undefined;
|
|
62
|
+
/** The line the command's `failed.match` found and the lines before it; empty when the log was gone. */
|
|
63
|
+
readonly excerpt: string;
|
|
64
|
+
/** `failed.stress` of the command that found the failure. */
|
|
65
|
+
readonly stress?: string | undefined;
|
|
66
|
+
/** `failed.rule` of the command that found the failure. */
|
|
67
|
+
readonly rule?: string | undefined;
|
|
68
|
+
/**
|
|
69
|
+
* Set when the machine was busy when the test failed. Such a failure is
|
|
70
|
+
* recorded and never counts toward a task: a rerun on a busy machine says
|
|
71
|
+
* little about the test.
|
|
72
|
+
*/
|
|
73
|
+
readonly load?: MachineLoad | undefined;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The passes that count toward a task: the ones seen on an idle machine. */
|
|
77
|
+
function countedOf(record: FlakyRecord): readonly FlakyOccurrence[] {
|
|
78
|
+
return record.occurrences.filter((entry) => entry.load === undefined);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** `check.flakyLoadLimit`: the setting, else twice the number of cores. */
|
|
82
|
+
export function flakyLoadLimitOf(configured: number | undefined, load: MachineLoad): number {
|
|
83
|
+
return configured ?? load.cores * 2;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** True when a reading is above the limit. */
|
|
87
|
+
function overLimit(load: MachineLoad, configured: number | undefined): boolean {
|
|
88
|
+
return load.load1 > flakyLoadLimitOf(configured, load);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The load readings a check handed over: the start and the end of each run. */
|
|
92
|
+
interface LoadInput {
|
|
93
|
+
readonly load?: MachineLoad | undefined;
|
|
94
|
+
readonly startLoad?: MachineLoad | undefined;
|
|
95
|
+
readonly endLoad?: MachineLoad | undefined;
|
|
96
|
+
readonly loads?: readonly CheckLoad[] | undefined;
|
|
97
|
+
readonly loadLimit?: number | undefined;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** The first reading above the limit, or undefined when the check ran idle. */
|
|
101
|
+
function busyLoad(input: LoadInput): MachineLoad | undefined {
|
|
102
|
+
const readings: MachineLoad[] = [];
|
|
103
|
+
for (const run of input.loads ?? []) readings.push(run.start, run.end);
|
|
104
|
+
if (input.startLoad !== undefined) readings.push(input.startLoad);
|
|
105
|
+
if (input.endLoad !== undefined) readings.push(input.endLoad);
|
|
106
|
+
if (input.load !== undefined) readings.push(input.load);
|
|
107
|
+
return readings.find((reading) => overLimit(reading, input.loadLimit));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** The history and check-log line for a failure skipped as seen under load. */
|
|
111
|
+
function skippedNotice(name: string, load: MachineLoad): string {
|
|
112
|
+
return `flaky test "${name}" skipped: seen under load ${load.load1} on ${load.cores} cores`;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Adds the skip line to the check's own log; a log that cannot be written stays as it is. */
|
|
116
|
+
function noteCheckLog(path: string | undefined, text: string): void {
|
|
117
|
+
if (path === undefined) return;
|
|
118
|
+
try {
|
|
119
|
+
appendFileSync(path, `${text}\n`);
|
|
120
|
+
} catch {
|
|
121
|
+
// The history still carries the line.
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export interface FlakyRecord {
|
|
126
|
+
readonly [key: string]: unknown;
|
|
127
|
+
readonly repository: string;
|
|
128
|
+
readonly name: string;
|
|
129
|
+
readonly occurrences: readonly FlakyOccurrence[];
|
|
130
|
+
/** The task proposed for this test; the count restarts when it ended. */
|
|
131
|
+
readonly task?: string | undefined;
|
|
132
|
+
/** Set once a proposal failure was reported, so the notice is not repeated. */
|
|
133
|
+
readonly noticed?: number | undefined;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function recordKey(repository: string, name: string): string {
|
|
137
|
+
return `${repository}/${Buffer.from(name).toString("hex")}`;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function occurrenceOf(value: unknown): FlakyOccurrence | undefined {
|
|
141
|
+
if (!isRecord(value)) return undefined;
|
|
142
|
+
const { task, at, file, excerpt, load, stress, rule } = value;
|
|
143
|
+
if (typeof task !== "string" || typeof at !== "number") return undefined;
|
|
144
|
+
return {
|
|
145
|
+
...(isRecord(load) && typeof load.load1 === "number" && typeof load.cores === "number"
|
|
146
|
+
? { load: { load1: load.load1, cores: load.cores } }
|
|
147
|
+
: {}),
|
|
148
|
+
task,
|
|
149
|
+
at,
|
|
150
|
+
file: typeof file === "string" ? file : undefined,
|
|
151
|
+
excerpt: typeof excerpt === "string" ? excerpt : "",
|
|
152
|
+
...(typeof stress === "string" ? { stress } : {}),
|
|
153
|
+
...(typeof rule === "string" ? { rule } : {}),
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function recordOf(data: unknown): FlakyRecord | undefined {
|
|
158
|
+
if (!isRecord(data)) return undefined;
|
|
159
|
+
const { repository, name, occurrences, task, noticed } = data;
|
|
160
|
+
if (typeof repository !== "string" || typeof name !== "string") return undefined;
|
|
161
|
+
if (!Array.isArray(occurrences)) return undefined;
|
|
162
|
+
return {
|
|
163
|
+
repository,
|
|
164
|
+
name,
|
|
165
|
+
occurrences: occurrences
|
|
166
|
+
.map(occurrenceOf)
|
|
167
|
+
.filter((entry): entry is FlakyOccurrence => entry !== undefined),
|
|
168
|
+
...(typeof task === "string" ? { task } : {}),
|
|
169
|
+
...(typeof noticed === "number" ? { noticed } : {}),
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function writableOf(record: FlakyRecord): Record<string, unknown> {
|
|
174
|
+
return {
|
|
175
|
+
repository: record.repository,
|
|
176
|
+
name: record.name,
|
|
177
|
+
occurrences: record.occurrences.map((entry) => ({
|
|
178
|
+
task: entry.task,
|
|
179
|
+
at: entry.at,
|
|
180
|
+
excerpt: entry.excerpt,
|
|
181
|
+
...(entry.load === undefined ? {} : { load: { load1: entry.load.load1, cores: entry.load.cores } }),
|
|
182
|
+
...(entry.file === undefined ? {} : { file: entry.file }),
|
|
183
|
+
...(entry.stress === undefined ? {} : { stress: entry.stress }),
|
|
184
|
+
...(entry.rule === undefined ? {} : { rule: entry.rule }),
|
|
185
|
+
})),
|
|
186
|
+
...(record.task === undefined ? {} : { task: record.task }),
|
|
187
|
+
...(record.noticed === undefined ? {} : { noticed: record.noticed }),
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const FILE_HEADER = /^(\S+\.(?:test|spec)\.[cm]?[jt]sx?):$/;
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* The line `match` (the command's own `failed.match`) finds for the test, with
|
|
195
|
+
* up to 30 lines before it, and the test file whose header line (`<file>:`)
|
|
196
|
+
* precedes it. A runner may repeat every failure in a summary; the first match
|
|
197
|
+
* wins, which is the one in the file's own section, since a summary comes
|
|
198
|
+
* after the sections. A test whose log has no match has an empty excerpt.
|
|
199
|
+
*/
|
|
200
|
+
export function failureExcerpt(
|
|
201
|
+
log: string,
|
|
202
|
+
name: string,
|
|
203
|
+
match: string,
|
|
204
|
+
): { readonly excerpt: string; readonly file: string | undefined } {
|
|
205
|
+
const lines = log.replace(/\r\n/g, "\n").split("\n");
|
|
206
|
+
const pattern = new RegExp(match);
|
|
207
|
+
let file: string | undefined;
|
|
208
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
209
|
+
const line = lines[index] ?? "";
|
|
210
|
+
const header = FILE_HEADER.exec(line);
|
|
211
|
+
if (header !== null) file = header[1];
|
|
212
|
+
const found = pattern.exec(line);
|
|
213
|
+
if (found === null || found[1] !== name) continue;
|
|
214
|
+
return {
|
|
215
|
+
excerpt: lines.slice(Math.max(0, index - EXCERPT_LINES), index + 1).join("\n"),
|
|
216
|
+
file,
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
return { excerpt: "", file: undefined };
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const SEARCH_SKIP_DIRS = new Set([
|
|
223
|
+
"node_modules",
|
|
224
|
+
".git",
|
|
225
|
+
".operad-worktrees",
|
|
226
|
+
".radian",
|
|
227
|
+
]);
|
|
228
|
+
const TEST_FILE = /\.(?:test|spec)\.[cm]?[jt]sx?$/;
|
|
229
|
+
|
|
230
|
+
/** The test files under `dir`, without descending into the directories that hold none. */
|
|
231
|
+
function testFilesUnder(repositoryPath: string, dir: string, out: string[]): void {
|
|
232
|
+
let entries;
|
|
233
|
+
try {
|
|
234
|
+
entries = readdirSync(dir === "" ? repositoryPath : join(repositoryPath, dir), {
|
|
235
|
+
withFileTypes: true,
|
|
236
|
+
});
|
|
237
|
+
} catch {
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
for (const entry of entries) {
|
|
241
|
+
const path = dir === "" ? entry.name : `${dir}/${entry.name}`;
|
|
242
|
+
if (entry.isDirectory()) {
|
|
243
|
+
if (!SEARCH_SKIP_DIRS.has(entry.name)) testFilesUnder(repositoryPath, path, out);
|
|
244
|
+
} else if (entry.isFile() && TEST_FILE.test(entry.name)) {
|
|
245
|
+
out.push(path);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* The one test file of the repository that holds every part of the test's name
|
|
252
|
+
* (its describe names and its own), or undefined when none or several do.
|
|
253
|
+
*/
|
|
254
|
+
export function searchTestFile(repositoryPath: string, name: string): string | undefined {
|
|
255
|
+
const parts = name.split(" > ").filter((part) => part !== "");
|
|
256
|
+
if (parts.length === 0) return undefined;
|
|
257
|
+
const hits: string[] = [];
|
|
258
|
+
const files: string[] = [];
|
|
259
|
+
testFilesUnder(repositoryPath, "", files);
|
|
260
|
+
for (const path of files) {
|
|
261
|
+
let text: string;
|
|
262
|
+
try {
|
|
263
|
+
text = readFileSync(join(repositoryPath, path), "utf8");
|
|
264
|
+
} catch {
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
if (parts.every((part) => text.includes(part))) hits.push(path);
|
|
268
|
+
}
|
|
269
|
+
return hits.length === 1 ? hits[0] : undefined;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function slugOf(name: string): string {
|
|
273
|
+
const words = name
|
|
274
|
+
.toLowerCase()
|
|
275
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
276
|
+
.replace(/^-+|-+$/g, "");
|
|
277
|
+
const kept = `flaky-${words === "" ? "test" : words}`.slice(0, 48).replace(/-+$/, "");
|
|
278
|
+
return kept;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const BUILT_IN_RULE =
|
|
282
|
+
"A test that failed and then passed on rerun is a bug until shown otherwise. Do not raise a timeout or add a retry inside a test to make it pass.";
|
|
283
|
+
|
|
284
|
+
/** The text of the rule file `rule` names inside the repository, else dev's own sentence. */
|
|
285
|
+
function flakyRule(repositoryPath: string, rule: string | undefined): string {
|
|
286
|
+
if (rule === undefined) return BUILT_IN_RULE;
|
|
287
|
+
try {
|
|
288
|
+
const root = realpathSync(repositoryPath);
|
|
289
|
+
const path = realpathSync(join(root, rule));
|
|
290
|
+
const inside = relative(root, path);
|
|
291
|
+
if (inside === "" || inside.startsWith("..") || isAbsolute(inside)) return BUILT_IN_RULE;
|
|
292
|
+
const text = readFileSync(path, "utf8").replace(/\r\n/g, "\n").trim();
|
|
293
|
+
return text === "" ? BUILT_IN_RULE : text;
|
|
294
|
+
} catch {
|
|
295
|
+
return BUILT_IN_RULE;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** `stress` with {name}, {pattern} and {file} filled in; {file} is left out when the file is not known. */
|
|
300
|
+
function stressCommand(template: string, name: string, file: string | undefined): string {
|
|
301
|
+
return template
|
|
302
|
+
.replace(/\{(name|pattern)\}/g, (_, kind: string) => shellQuote(kind === "name" ? name : anchoredPattern(name)))
|
|
303
|
+
// An unknown file leaves nothing behind.
|
|
304
|
+
.replace(/\{file\}/g, file === undefined ? "" : shellQuote(file))
|
|
305
|
+
.replace(/[ \t]+$/gm, "");
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function stamp(at: number): string {
|
|
309
|
+
return new Date(at).toISOString();
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** The brief of the proposed task. */
|
|
313
|
+
export function flakyBrief(
|
|
314
|
+
repositoryPath: string,
|
|
315
|
+
whole: FlakyRecord,
|
|
316
|
+
): string {
|
|
317
|
+
const record: FlakyRecord = { ...whole, occurrences: countedOf(whole) };
|
|
318
|
+
const file = [...record.occurrences].reverse().find((entry) => entry.file !== undefined)?.file;
|
|
319
|
+
const where = file === undefined ? "a file that is not known (the logs do not name it and no single test file holds the name)" : `\`${file}\``;
|
|
320
|
+
const lines: string[] = [
|
|
321
|
+
`The test \`${record.name}\` in ${where} failed and then passed on its own rerun ${record.occurrences.length} times.`,
|
|
322
|
+
"",
|
|
323
|
+
"It happened in:",
|
|
324
|
+
...record.occurrences
|
|
325
|
+
.slice(-SHOWN_TIMES)
|
|
326
|
+
.map((entry) => `- ${entry.task} at ${stamp(entry.at)}`),
|
|
327
|
+
"",
|
|
328
|
+
"The last failures:",
|
|
329
|
+
];
|
|
330
|
+
const failures = record.occurrences
|
|
331
|
+
.filter((entry) => entry.excerpt !== "")
|
|
332
|
+
.slice(-SHOWN_FAILURES);
|
|
333
|
+
if (failures.length === 0) lines.push("", "(no log was left to quote)");
|
|
334
|
+
for (const entry of failures) {
|
|
335
|
+
const fence = entry.excerpt.includes("```") ? "~~~~" : "```";
|
|
336
|
+
lines.push("", `${entry.task} at ${stamp(entry.at)}:`, fence, entry.excerpt, fence);
|
|
337
|
+
}
|
|
338
|
+
const latest = [...record.occurrences].reverse();
|
|
339
|
+
// The newest pass is the one that reached the threshold: only its command's settings count.
|
|
340
|
+
const stress = latest[0]?.stress;
|
|
341
|
+
if (stress !== undefined) {
|
|
342
|
+
lines.push("", "Reproduce it:", "```sh", stressCommand(stress, record.name, file), "```", "", "Write the number of runs in place of `<N>`: it follows the rule quoted below.");
|
|
343
|
+
}
|
|
344
|
+
lines.push("", "The rule for flaky tests:", "", flakyRule(repositoryPath, latest[0]?.rule));
|
|
345
|
+
return lines.join("\n");
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
async function newestConfirmedPlan(
|
|
349
|
+
store: ObjectStore,
|
|
350
|
+
repository: string,
|
|
351
|
+
): Promise<StoredObject | undefined> {
|
|
352
|
+
let best: { readonly object: StoredObject; readonly number: number } | undefined;
|
|
353
|
+
for (const object of await listAll(store, "plan")) {
|
|
354
|
+
const plan = planData(object.data);
|
|
355
|
+
if (plan.repository !== repository || plan.status !== "confirmed") continue;
|
|
356
|
+
const number = parseId(bareId(object.id), "plan")?.number ?? -1;
|
|
357
|
+
if (best === undefined || number > best.number) best = { object, number };
|
|
358
|
+
}
|
|
359
|
+
return best?.object;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const FLAKY_PLAN_SLUG = "flaky-tests";
|
|
363
|
+
const FLAKY_PLAN_TITLE = "Flaky tests";
|
|
364
|
+
|
|
365
|
+
function isEnded(plan: PlanData): boolean {
|
|
366
|
+
const state = planState(plan);
|
|
367
|
+
return state === "finished" || state === "cancelled";
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* The plan flaky-test tasks go into: `check.flakyPlan` when it names a plan of
|
|
372
|
+
* the repository that is not finished or cancelled, else dev's own plan
|
|
373
|
+
* `flaky-tests`, made once (confirmed, with the auto level and priority of the
|
|
374
|
+
* repository's newest confirmed plan). Runs under the repository lock.
|
|
375
|
+
*/
|
|
376
|
+
async function flakyPlanFor(
|
|
377
|
+
store: ObjectStore,
|
|
378
|
+
repository: RepositoryData,
|
|
379
|
+
configured: string | undefined,
|
|
380
|
+
): Promise<{ readonly plan: StoredObject; readonly missing: string | undefined }> {
|
|
381
|
+
let missing: string | undefined;
|
|
382
|
+
if (configured !== undefined) {
|
|
383
|
+
const named = await store.get("plan", `${repository.name}/${bareId(configured)}`);
|
|
384
|
+
if (named !== undefined) {
|
|
385
|
+
const data = planData(named.data);
|
|
386
|
+
if (data.repository === repository.name && !isEnded(data)) {
|
|
387
|
+
return { plan: named, missing: undefined };
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
missing = configured;
|
|
391
|
+
}
|
|
392
|
+
const own = (await listAll(store, "plan"))
|
|
393
|
+
.filter((object) => {
|
|
394
|
+
const data = planData(object.data);
|
|
395
|
+
return (
|
|
396
|
+
data.repository === repository.name &&
|
|
397
|
+
parseId(bareId(object.id), "plan")?.slug === FLAKY_PLAN_SLUG &&
|
|
398
|
+
!isEnded(data)
|
|
399
|
+
);
|
|
400
|
+
})
|
|
401
|
+
.sort(
|
|
402
|
+
(x, y) =>
|
|
403
|
+
(parseId(bareId(y.id), "plan")?.number ?? -1) - (parseId(bareId(x.id), "plan")?.number ?? -1),
|
|
404
|
+
)[0];
|
|
405
|
+
if (own !== undefined) return { plan: own, missing };
|
|
406
|
+
const model = await newestConfirmedPlan(store, repository.name);
|
|
407
|
+
const data = model === undefined ? undefined : planData(model.data);
|
|
408
|
+
const plan = await addConfirmedPlanLocked(store, repository, {
|
|
409
|
+
slug: FLAKY_PLAN_SLUG,
|
|
410
|
+
title: FLAKY_PLAN_TITLE,
|
|
411
|
+
auto: data?.auto ?? "off",
|
|
412
|
+
priority: data?.priority ?? "P3",
|
|
413
|
+
});
|
|
414
|
+
return { plan, missing };
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** The title of the task dev proposes for a test. */
|
|
418
|
+
function titleFor(name: string): string {
|
|
419
|
+
return `Fix the flaky test: ${name}`;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Whether an open task is the test's task: its title is exactly the title dev
|
|
424
|
+
* gives a flaky task, or a line of its brief, outside fenced code and quoted
|
|
425
|
+
* text, is exactly the full test name. A task dev proposed for the test is
|
|
426
|
+
* linked on the test's record, not found here. Nothing else counts, so a
|
|
427
|
+
* failure excerpt in another brief never links a test.
|
|
428
|
+
*/
|
|
429
|
+
export function namesTest(title: string, brief: string, name: string): boolean {
|
|
430
|
+
if (title === titleFor(name)) return true;
|
|
431
|
+
let fenced = false;
|
|
432
|
+
for (const line of brief.replace(/\r\n/g, "\n").split("\n")) {
|
|
433
|
+
if (/^\s{0,3}(?:`{3,}|~{3,})/.test(line)) {
|
|
434
|
+
fenced = !fenced;
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
if (fenced || line.trimStart().startsWith(">")) continue;
|
|
438
|
+
if (line.trim() === name) return true;
|
|
439
|
+
}
|
|
440
|
+
return false;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
async function namingTask(
|
|
444
|
+
store: ObjectStore,
|
|
445
|
+
repository: string,
|
|
446
|
+
name: string,
|
|
447
|
+
): Promise<StoredObject | undefined> {
|
|
448
|
+
return (await listAll(store, "task")).find((object) => {
|
|
449
|
+
const task = taskData(object.data);
|
|
450
|
+
return (
|
|
451
|
+
task.repository === repository &&
|
|
452
|
+
isOpenTask(task) &&
|
|
453
|
+
namesTest(task.title, splitBrief(task.brief).body, name)
|
|
454
|
+
);
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
export interface FlakyInput {
|
|
459
|
+
readonly store: ObjectStore;
|
|
460
|
+
/** Secret values to hide in the failure excerpts. */
|
|
461
|
+
readonly secrets?: readonly string[] | undefined;
|
|
462
|
+
readonly env?: Readonly<Record<string, string | undefined>> | undefined;
|
|
463
|
+
readonly repository: RepositoryData;
|
|
464
|
+
/** The task whose check passed the tests on rerun. */
|
|
465
|
+
readonly taskId: string;
|
|
466
|
+
readonly names: readonly string[];
|
|
467
|
+
/** The `failed` settings of the command that found each test. */
|
|
468
|
+
readonly failedBy?: ReadonlyMap<string, FailedRerun> | undefined;
|
|
469
|
+
/** The check log, which still holds the first run's output. */
|
|
470
|
+
readonly log: string | undefined;
|
|
471
|
+
/** One load reading for the check, when the caller has only that. */
|
|
472
|
+
readonly load?: MachineLoad | undefined;
|
|
473
|
+
/** The load at the start and the end of the check, when it is one run. */
|
|
474
|
+
readonly startLoad?: MachineLoad | undefined;
|
|
475
|
+
readonly endLoad?: MachineLoad | undefined;
|
|
476
|
+
/** The load at the start and the end of each run of the check. */
|
|
477
|
+
readonly loads?: readonly CheckLoad[] | undefined;
|
|
478
|
+
/** `check.flakyLoadLimit`: the load above which a failure is not a flaky test. */
|
|
479
|
+
readonly loadLimit?: number | undefined;
|
|
480
|
+
/** `check.flakyTask`; 0 turns the proposal off. */
|
|
481
|
+
readonly threshold: number;
|
|
482
|
+
/** `check.flakyPlan`: the plan to propose in, by id. */
|
|
483
|
+
readonly flakyPlan?: string | undefined;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
function readLog(path: string | undefined): string {
|
|
487
|
+
if (path === undefined) return "";
|
|
488
|
+
try {
|
|
489
|
+
return readFileSync(path, "utf8");
|
|
490
|
+
} catch {
|
|
491
|
+
return "";
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
async function taskEnded(store: ObjectStore, repository: string, id: string): Promise<boolean> {
|
|
496
|
+
const object = await store.get("task", `${repository}/${id}`);
|
|
497
|
+
return object === undefined || !isOpenTask(taskData(object.data));
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* Remembers the tests that passed on rerun and proposes a task for each test
|
|
502
|
+
* that reached the threshold. Safe to call again at any time.
|
|
503
|
+
*/
|
|
504
|
+
export async function trackFlakyTests(input: FlakyInput): Promise<string | undefined> {
|
|
505
|
+
if (input.threshold <= 0 || input.names.length === 0) return undefined;
|
|
506
|
+
const notices: string[] = [];
|
|
507
|
+
const { store, repository } = input;
|
|
508
|
+
const log = readLog(input.log);
|
|
509
|
+
const at = changeClock.now();
|
|
510
|
+
const busy = busyLoad(input);
|
|
511
|
+
// A test seen under load this time is skipped, even when an earlier idle
|
|
512
|
+
// count already reached the threshold: that failure is not the trigger.
|
|
513
|
+
const skippedNames = busy === undefined ? new Set<string>() : new Set(input.names);
|
|
514
|
+
await withRepoLock(store, repository.name, async (fresh) => {
|
|
515
|
+
for (const name of input.names) {
|
|
516
|
+
const key = recordKey(fresh.name, name);
|
|
517
|
+
const stored = await store.get(FLAKY_TYPE, key);
|
|
518
|
+
let record: FlakyRecord = recordOf(stored?.data) ?? {
|
|
519
|
+
repository: fresh.name,
|
|
520
|
+
name,
|
|
521
|
+
occurrences: [],
|
|
522
|
+
};
|
|
523
|
+
if (record.task !== undefined && (await taskEnded(store, fresh.name, record.task))) {
|
|
524
|
+
record = { repository: fresh.name, name, occurrences: [] };
|
|
525
|
+
}
|
|
526
|
+
if (record.task === undefined) {
|
|
527
|
+
// An open task that names the test is its task from the first pass on:
|
|
528
|
+
// the count restarts when that task ends, however few passes came before.
|
|
529
|
+
const existing = await namingTask(store, fresh.name, name);
|
|
530
|
+
if (existing !== undefined) {
|
|
531
|
+
record = { ...record, task: bareId(existing.id) };
|
|
532
|
+
await store.put(FLAKY_TYPE, key, writableOf(record), "dev:flaky-task");
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
// While its task is open the count does not grow: the fix is on its way.
|
|
536
|
+
if (record.task !== undefined) {
|
|
537
|
+
if (busy !== undefined) notices.push(skippedNotice(name, busy));
|
|
538
|
+
continue;
|
|
539
|
+
}
|
|
540
|
+
const failed = input.failedBy?.get(name);
|
|
541
|
+
const found = failed === undefined ? { excerpt: "", file: undefined } : failureExcerpt(log, name, failed.match);
|
|
542
|
+
const file = found.file ?? searchTestFile(fresh.path, name);
|
|
543
|
+
const excerpt = maskSecrets(found.excerpt, input.secrets ?? [], input.env);
|
|
544
|
+
record = {
|
|
545
|
+
...record,
|
|
546
|
+
occurrences: [
|
|
547
|
+
...record.occurrences,
|
|
548
|
+
{ task: input.taskId, at, file, excerpt, ...(failed?.stress === undefined ? {} : { stress: failed.stress }), ...(failed?.rule === undefined ? {} : { rule: failed.rule }), ...(busy === undefined ? {} : { load: busy }) },
|
|
549
|
+
],
|
|
550
|
+
};
|
|
551
|
+
await store.put(FLAKY_TYPE, key, writableOf(record), "dev:flaky-count");
|
|
552
|
+
if (busy !== undefined) notices.push(skippedNotice(name, busy));
|
|
553
|
+
}
|
|
554
|
+
// Decide for every counted test of the repository: a crash after the count
|
|
555
|
+
// was written and before the task was proposed is finished here.
|
|
556
|
+
for (const object of await listAll(store, FLAKY_TYPE)) {
|
|
557
|
+
if (keyRepository(object.id) !== fresh.name) continue;
|
|
558
|
+
const record = recordOf(object.data);
|
|
559
|
+
if (record === undefined || record.task !== undefined) continue;
|
|
560
|
+
if (skippedNames.has(record.name)) continue;
|
|
561
|
+
if (countedOf(record).length < input.threshold) continue;
|
|
562
|
+
const notice = await propose(input, fresh, object.id, record);
|
|
563
|
+
if (notice !== undefined) notices.push(notice);
|
|
564
|
+
}
|
|
565
|
+
});
|
|
566
|
+
return notices.length === 0 ? undefined : notices.join("; ");
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
async function propose(
|
|
570
|
+
input: FlakyInput,
|
|
571
|
+
repository: RepositoryData,
|
|
572
|
+
key: string,
|
|
573
|
+
record: FlakyRecord,
|
|
574
|
+
): Promise<string | undefined> {
|
|
575
|
+
const { store } = input;
|
|
576
|
+
const existing = await namingTask(store, repository.name, record.name);
|
|
577
|
+
if (existing !== undefined) {
|
|
578
|
+
// Its task is the one that counts: the count restarts when that task ends.
|
|
579
|
+
await store.put(
|
|
580
|
+
FLAKY_TYPE,
|
|
581
|
+
key,
|
|
582
|
+
writableOf({ ...record, task: bareId(existing.id) }),
|
|
583
|
+
"dev:flaky-task",
|
|
584
|
+
);
|
|
585
|
+
return undefined;
|
|
586
|
+
}
|
|
587
|
+
const { plan, missing } = await flakyPlanFor(store, repository, input.flakyPlan);
|
|
588
|
+
await flakyTestHooks.beforePropose?.(record.name);
|
|
589
|
+
const fallback =
|
|
590
|
+
missing === undefined
|
|
591
|
+
? undefined
|
|
592
|
+
: `check.flakyPlan "${missing}" does not name an open plan of this repository; the task went into "${bareId(plan.id)}"`;
|
|
593
|
+
try {
|
|
594
|
+
const task = await addTaskLocked(
|
|
595
|
+
store,
|
|
596
|
+
{
|
|
597
|
+
plan: plan.id,
|
|
598
|
+
slug: slugOf(record.name),
|
|
599
|
+
title: titleFor(record.name),
|
|
600
|
+
brief: flakyBrief(repository.path, record),
|
|
601
|
+
openedBy: "dev",
|
|
602
|
+
},
|
|
603
|
+
repository,
|
|
604
|
+
);
|
|
605
|
+
await store.put(
|
|
606
|
+
FLAKY_TYPE,
|
|
607
|
+
key,
|
|
608
|
+
writableOf({ ...record, task: bareId(task.id) }),
|
|
609
|
+
"dev:flaky-task",
|
|
610
|
+
);
|
|
611
|
+
return fallback;
|
|
612
|
+
} catch (error) {
|
|
613
|
+
const count = countedOf(record).length;
|
|
614
|
+
if (record.noticed !== undefined) return undefined;
|
|
615
|
+
await store.put(
|
|
616
|
+
FLAKY_TYPE,
|
|
617
|
+
key,
|
|
618
|
+
writableOf({ ...record, noticed: count }),
|
|
619
|
+
"dev:flaky-task",
|
|
620
|
+
);
|
|
621
|
+
const notice = noticeFor(record, messageOf(error));
|
|
622
|
+
return fallback === undefined ? notice : `${fallback}; ${notice}`;
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
function noticeFor(record: FlakyRecord, reason: string): string {
|
|
627
|
+
return `flaky test "${record.name}" passed on rerun ${countedOf(record).length} times; no task was proposed: ${reason}`;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
/** The open task of each test name, across the home; the first one found wins. */
|
|
631
|
+
export async function flakyTasksByName(
|
|
632
|
+
store: ObjectStore,
|
|
633
|
+
): Promise<ReadonlyMap<string, string>> {
|
|
634
|
+
const tasks = new Map<string, string>();
|
|
635
|
+
for (const object of await listAll(store, FLAKY_TYPE)) {
|
|
636
|
+
const record = recordOf(object.data);
|
|
637
|
+
if (record?.task === undefined || tasks.has(record.name)) continue;
|
|
638
|
+
if (await taskEnded(store, record.repository, record.task)) continue;
|
|
639
|
+
tasks.set(record.name, record.task);
|
|
640
|
+
}
|
|
641
|
+
return tasks;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/** What a check step passes on after it recorded its passes on rerun. */
|
|
645
|
+
export interface FlakyReport {
|
|
646
|
+
readonly ctx: {
|
|
647
|
+
readonly store: ObjectStore;
|
|
648
|
+
readonly secrets: SecretStore;
|
|
649
|
+
readonly logger: { write(text: string): Promise<string> | string };
|
|
650
|
+
};
|
|
651
|
+
readonly repository: RepositoryData;
|
|
652
|
+
readonly taskId: string;
|
|
653
|
+
readonly threshold: number;
|
|
654
|
+
readonly flakyPlan?: string | undefined;
|
|
655
|
+
readonly load?: MachineLoad | undefined;
|
|
656
|
+
readonly startLoad?: MachineLoad | undefined;
|
|
657
|
+
readonly endLoad?: MachineLoad | undefined;
|
|
658
|
+
readonly loads?: readonly CheckLoad[] | undefined;
|
|
659
|
+
readonly loadLimit?: number | undefined;
|
|
660
|
+
readonly check: {
|
|
661
|
+
readonly passedOnRerun: readonly string[];
|
|
662
|
+
readonly failedBy?: ReadonlyMap<string, FailedRerun> | undefined;
|
|
663
|
+
readonly outcome: { readonly data?: Record<string, unknown> | undefined } | undefined;
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
async function secretValues(store: SecretStore): Promise<string[]> {
|
|
668
|
+
try {
|
|
669
|
+
const values: string[] = [];
|
|
670
|
+
for (const name of await store.names()) {
|
|
671
|
+
const secret = await store.get(name);
|
|
672
|
+
if (secret !== undefined) values.push(secret.reveal());
|
|
673
|
+
}
|
|
674
|
+
return values;
|
|
675
|
+
} catch {
|
|
676
|
+
return [];
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
/**
|
|
681
|
+
* Counts the passes on rerun of a finished check; a failure here never fails
|
|
682
|
+
* the check. Returns a notice for the check's history when no task could be
|
|
683
|
+
* proposed.
|
|
684
|
+
*/
|
|
685
|
+
export async function reportFlakyTests(report: FlakyReport): Promise<string | undefined> {
|
|
686
|
+
if (report.threshold <= 0 || report.check.passedOnRerun.length === 0) return undefined;
|
|
687
|
+
const log = report.check.outcome?.data?.log;
|
|
688
|
+
try {
|
|
689
|
+
const busy = busyLoad(report);
|
|
690
|
+
const notice = await trackFlakyTests({
|
|
691
|
+
store: report.ctx.store,
|
|
692
|
+
secrets: await secretValues(report.ctx.secrets),
|
|
693
|
+
repository: report.repository,
|
|
694
|
+
taskId: report.taskId,
|
|
695
|
+
names: report.check.passedOnRerun,
|
|
696
|
+
failedBy: report.check.failedBy,
|
|
697
|
+
log: typeof log === "string" ? log : undefined,
|
|
698
|
+
threshold: report.threshold,
|
|
699
|
+
flakyPlan: report.flakyPlan,
|
|
700
|
+
load: report.load,
|
|
701
|
+
startLoad: report.startLoad,
|
|
702
|
+
endLoad: report.endLoad,
|
|
703
|
+
loads: report.loads,
|
|
704
|
+
loadLimit: report.loadLimit,
|
|
705
|
+
});
|
|
706
|
+
// The check's log says why a failure was skipped, as its history does.
|
|
707
|
+
if (busy !== undefined && notice !== undefined) {
|
|
708
|
+
await report.ctx.logger.write(notice);
|
|
709
|
+
noteCheckLog(typeof log === "string" ? log : undefined, notice);
|
|
710
|
+
}
|
|
711
|
+
return notice;
|
|
712
|
+
} catch (error) {
|
|
713
|
+
await report.ctx.logger.write(`could not propose a task for a flaky test: ${messageOf(error)}`);
|
|
714
|
+
return undefined;
|
|
715
|
+
}
|
|
716
|
+
}
|