@tayomi/deterministic-gates 0.9.0 → 0.10.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/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/.codex-plugin/plugin.json +1 -1
- package/catalog/angular.json +2 -1
- package/catalog/index.html +6 -0
- package/catalog/typescript.json +2 -1
- package/dist/src/core/check.js +51 -20
- package/dist/src/core/config.js +8 -1
- package/dist/src/core/head.js +84 -0
- package/dist/src/core/report.js +22 -2
- package/dist/src/core/run.js +21 -8
- package/dist/src/core/snapshot.js +78 -2
- package/dist/src/core/turn-state.js +15 -1
- package/dist/src/core/watch.js +4 -4
- package/dist/src/helpers/bulk-suppressions.js +10 -9
- package/dist/src/helpers/new-clones.js +90 -16
- package/package.json +1 -1
- package/src/core/check.ts +49 -19
- package/src/core/config.ts +11 -0
- package/src/core/head.ts +114 -0
- package/src/core/report.ts +21 -2
- package/src/core/run.ts +27 -6
- package/src/core/snapshot.ts +96 -2
- package/src/core/turn-state.ts +25 -5
- package/src/core/watch.ts +4 -2
- package/src/helpers/bulk-suppressions.ts +14 -9
- package/src/helpers/new-clones.ts +105 -15
|
@@ -44,18 +44,19 @@ export function newBulkSuppressions(before, after, moved = () => false) {
|
|
|
44
44
|
return cameWithIt ? [] : [`${source}: ${rule} ${old} -> ${count}`];
|
|
45
45
|
}));
|
|
46
46
|
}
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
/** The counts the turn raised in the suppressions files given, each under the name of its file. */
|
|
48
|
+
export function bulkSuppressions(root, turn, files) {
|
|
49
|
+
return files.flatMap((file) => {
|
|
49
50
|
const before = fileAt(root, turn, file);
|
|
50
|
-
// ESLint names a source from the folder it runs in
|
|
51
|
-
// a source
|
|
52
|
-
const moved = (source) =>
|
|
53
|
-
|
|
54
|
-
return existsSync(join(root, path)) && fileAt(root, turn, path) === undefined;
|
|
55
|
-
};
|
|
56
|
-
const rose = newBulkSuppressions(before === undefined ? undefined : parseJson(before), parseJson(readFileSync(file, "utf8")), moved);
|
|
51
|
+
// ESLint names a source from the folder it runs in: the one of its suppressions file, or the root of the repo
|
|
52
|
+
// when the repo keeps that file in a folder of its own. A source found in neither is never taken for a moved one.
|
|
53
|
+
const moved = (source) => [join(dirname(file), source), source].some((path) => existsSync(join(root, path)) && fileAt(root, turn, path) === undefined);
|
|
54
|
+
const rose = newBulkSuppressions(before === undefined ? undefined : parseJson(before), parseJson(readFileSync(join(root, file), "utf8")), moved);
|
|
57
55
|
return rose.map((line) => `${file}: ${line}`);
|
|
58
56
|
});
|
|
57
|
+
}
|
|
58
|
+
function main(root, turn, files) {
|
|
59
|
+
const found = bulkSuppressions(root, turn, files);
|
|
59
60
|
if (!found.length)
|
|
60
61
|
return;
|
|
61
62
|
console.log(headed("New bulk suppressions: fix the violations, never the file that hides them.", found));
|
|
@@ -1,33 +1,95 @@
|
|
|
1
1
|
import { execFileSync } from "node:child_process";
|
|
2
|
-
import { mkdtempSync, rmSync } from "node:fs";
|
|
2
|
+
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
|
|
3
3
|
import { tmpdir } from "node:os";
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import { isRecord, readJsonFile } from "../core/json.js";
|
|
6
6
|
import { headed, packageScript } from "../core/run.js";
|
|
7
|
+
import { filesAt, writtenSince } from "../core/snapshot.js";
|
|
7
8
|
// jscpd compared with the tree the turn started from. Its own console report lists every clone of the repo, the old
|
|
8
9
|
// ones included, so this prints only the clones the turn added, one per line.
|
|
10
|
+
//
|
|
11
|
+
// jscpd tells a clone by its two files and where it stands in them, so it also calls new an old clone the turn only
|
|
12
|
+
// disturbed: two old files it pairs once the turn takes their copy out of a third, or a clone the turn cut in two by a
|
|
13
|
+
// line written in its middle. A clone is the turn's only when the turn wrote in one of its sides.
|
|
9
14
|
function refuse() {
|
|
10
15
|
throw new Error("Not a jscpd report.");
|
|
11
16
|
}
|
|
12
|
-
function
|
|
17
|
+
function placeOf(value, line, column) {
|
|
18
|
+
if (isRecord(value) && typeof value.line === "number" && typeof value.column === "number")
|
|
19
|
+
return { line: value.line, column: value.column };
|
|
20
|
+
return { line, column };
|
|
21
|
+
}
|
|
22
|
+
// Without the columns of a clone, as from another jscpd, its first and last lines count as whole.
|
|
23
|
+
function sideOf(value) {
|
|
13
24
|
if (!isRecord(value))
|
|
14
25
|
refuse();
|
|
15
|
-
const { name, start, end } = value;
|
|
26
|
+
const { name, start, end, startLoc, endLoc } = value;
|
|
16
27
|
if (typeof name !== "string" || typeof start !== "number" || typeof end !== "number")
|
|
17
28
|
refuse();
|
|
18
|
-
return
|
|
29
|
+
return { name, start: placeOf(startLoc, start, 0), end: placeOf(endLoc, end, Infinity) };
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Whether the turn wrote in a side of a clone: a line it wrote stands whole in it, or it cut lines out of its middle.
|
|
33
|
+
* A clone starts and ends between two tokens, often inside a line: a line it only starts or ends in is not in it, nor
|
|
34
|
+
* an empty one, which jscpd does not read.
|
|
35
|
+
*/
|
|
36
|
+
export function wroteIn({ start, end }, text, written) {
|
|
37
|
+
const lines = text.split("\n");
|
|
38
|
+
const whole = (at) => {
|
|
39
|
+
const line = lines[at - 1] ?? "";
|
|
40
|
+
if (!line.trim())
|
|
41
|
+
return false;
|
|
42
|
+
const starts = at > start.line || (at === start.line && start.column <= line.length - line.trimStart().length);
|
|
43
|
+
const ends = at < end.line || (at === end.line && end.column >= line.trimEnd().length);
|
|
44
|
+
return starts && ends;
|
|
45
|
+
};
|
|
46
|
+
return written.some(({ at, lines: count }) => count === 0 ? start.line <= at && at < end.line : Array.from({ length: count }, (_, more) => at + more).some(whole));
|
|
19
47
|
}
|
|
20
|
-
|
|
48
|
+
/** The clones jscpd calls new, each as its two sides. */
|
|
49
|
+
function newPairs(report) {
|
|
21
50
|
if (!isRecord(report) || !Array.isArray(report.duplicates))
|
|
22
51
|
refuse();
|
|
23
52
|
return report.duplicates.flatMap((clone) => {
|
|
24
53
|
if (!isRecord(clone))
|
|
25
54
|
refuse();
|
|
26
|
-
const
|
|
27
|
-
return clone.isNew === true ? [
|
|
55
|
+
const sides = [sideOf(clone.firstFile), sideOf(clone.secondFile)];
|
|
56
|
+
return clone.isNew === true ? [sides] : [];
|
|
28
57
|
});
|
|
29
58
|
}
|
|
30
|
-
|
|
59
|
+
/** The clones jscpd calls new, one per line, less the ones the turn wrote in neither side of. */
|
|
60
|
+
export function newClones(report, wrote = () => true) {
|
|
61
|
+
return newPairs(report)
|
|
62
|
+
.filter((sides) => sides.some(wrote))
|
|
63
|
+
.map((sides) => sides.map(({ name, start, end }) => `${name}:${start.line}-${end.line}`).join(" ~ "));
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Whether the turn wrote in a side of a clone, for the clones of a report. Each file is read once, however many
|
|
67
|
+
* clones it has: the whole check finds every clone of the repo new.
|
|
68
|
+
*/
|
|
69
|
+
function turnWrote(root, turn, report) {
|
|
70
|
+
const names = [...new Set(newPairs(report).flatMap((sides) => sides.map(({ name }) => name)))];
|
|
71
|
+
const old = new Set(filesAt(root, turn, names));
|
|
72
|
+
const files = new Map();
|
|
73
|
+
return (side) => {
|
|
74
|
+
// A file the turn created is all its own, and one jscpd names from another folder is unknown here: checking too
|
|
75
|
+
// much beats leaving a hole.
|
|
76
|
+
if (!old.has(side.name))
|
|
77
|
+
return true;
|
|
78
|
+
try {
|
|
79
|
+
const file = files.get(side.name) ?? {
|
|
80
|
+
text: readFileSync(join(root, side.name), "utf8"),
|
|
81
|
+
written: writtenSince(root, turn, side.name),
|
|
82
|
+
};
|
|
83
|
+
files.set(side.name, file);
|
|
84
|
+
return wroteIn(side, file.text, file.written);
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/** The clones the turn added to the repo, as jscpd finds them. It throws what jscpd said when jscpd fails. */
|
|
92
|
+
export function turnClones(root, turn) {
|
|
31
93
|
const jscpd = packageScript(root, "jscpd");
|
|
32
94
|
if (jscpd === undefined)
|
|
33
95
|
throw new Error("jscpd is not installed in node_modules: install the dependencies of the repo.");
|
|
@@ -39,19 +101,31 @@ function main(root, turn) {
|
|
|
39
101
|
catch (error) {
|
|
40
102
|
// What jscpd said, without the stack of this script around it.
|
|
41
103
|
const said = isRecord(error) && typeof error.stderr === "string" ? error.stderr.trim() : String(error);
|
|
42
|
-
|
|
43
|
-
process.exitCode = 1;
|
|
44
|
-
return;
|
|
104
|
+
throw new Error(`jscpd failed: ${said}`, { cause: error });
|
|
45
105
|
}
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
return;
|
|
49
|
-
console.log(headed("New clones: reuse the code already there, or factor it out.", clones));
|
|
50
|
-
process.exitCode = 1;
|
|
106
|
+
const report = readJsonFile(join(output, "jscpd-report.json"));
|
|
107
|
+
return newClones(report, turnWrote(root, turn, report));
|
|
51
108
|
}
|
|
52
109
|
finally {
|
|
53
110
|
rmSync(output, { recursive: true, force: true });
|
|
54
111
|
}
|
|
55
112
|
}
|
|
113
|
+
function main(root, turn) {
|
|
114
|
+
let clones;
|
|
115
|
+
try {
|
|
116
|
+
clones = turnClones(root, turn);
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
if (!(error instanceof Error) || !error.message.startsWith("jscpd failed: "))
|
|
120
|
+
throw error;
|
|
121
|
+
console.log(error.message);
|
|
122
|
+
process.exitCode = 1;
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (!clones.length)
|
|
126
|
+
return;
|
|
127
|
+
console.log(headed("New clones: reuse the code already there, or factor it out.", clones));
|
|
128
|
+
process.exitCode = 1;
|
|
129
|
+
}
|
|
56
130
|
if (import.meta.main)
|
|
57
131
|
main(process.cwd(), process.argv[2] ?? "HEAD");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tayomi/deterministic-gates",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.1",
|
|
4
4
|
"description": "Deterministic backpressure for coding agents: gates that run on what each turn changed and send the agent back until they pass.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
package/src/core/check.ts
CHANGED
|
@@ -10,10 +10,11 @@ import {
|
|
|
10
10
|
type Prompts,
|
|
11
11
|
readGatesFiles,
|
|
12
12
|
} from "./config.ts";
|
|
13
|
+
import { headOf, movedSince, observed, thereOf } from "./head.ts";
|
|
13
14
|
import { cachedReports, cacheReports } from "./report-cache.ts";
|
|
14
|
-
import { check, filesOf, givenFiles, missingTool, type Outcome, wholeProject } from "./run.ts";
|
|
15
|
-
import { changedSince, emptyCommit, hasCommit, lastCommit, snapshot, workingTree } from "./snapshot.ts";
|
|
16
|
-
import { readTurn, type TurnState, writeTurn } from "./turn-state.ts";
|
|
15
|
+
import { check, filesOf, givenFiles, missingTool, namesFiles, type Outcome, wholeProject } from "./run.ts";
|
|
16
|
+
import { changedSince, emptyCommit, hasCommit, lastCommit, sharedDir, snapshot, workingTree } from "./snapshot.ts";
|
|
17
|
+
import { readThere, readTurn, type TurnState, writeThere, writeTurn } from "./turn-state.ts";
|
|
17
18
|
import { judgeWatch, keepWatch, startWatch, TURN_START_MS, watchReport } from "./watch.ts";
|
|
18
19
|
|
|
19
20
|
/** What the repo says of what the agent is sent: the prompts of the whole check, and its gates, for their own. */
|
|
@@ -84,13 +85,20 @@ export async function startTurn(session: string, root: string): Promise<string[]
|
|
|
84
85
|
const compared = runnable(gates, missing).filter(isCompared);
|
|
85
86
|
startWatches(session, root, runnable(gates, missing));
|
|
86
87
|
const interrupted = readTurn(session, root);
|
|
88
|
+
// The worktrees of a repo share its commits: what the session knows of them is of the repo, not of the folder.
|
|
89
|
+
const shared = sharedDir(root);
|
|
90
|
+
const there = readThere(session, shared);
|
|
87
91
|
if (interrupted?.checked === false) {
|
|
88
|
-
|
|
92
|
+
// What it committed so far stays the session's, and a commit older than this prompt is one a fetch may bring.
|
|
93
|
+
if (there) writeThere(session, shared, observed(root, there, interrupted.at ?? Date.now()));
|
|
94
|
+
writeTurn(session, root, { ...interrupted, at: Date.now() });
|
|
89
95
|
return told;
|
|
90
96
|
}
|
|
97
|
+
writeThere(session, shared, thereOf(root, there));
|
|
98
|
+
const head = headOf(root);
|
|
91
99
|
const { commit: start, tree } = snapshot(root);
|
|
92
100
|
// The turn starts before its reports are ready: a hook stopped for lack of time leaves it, never the turn before.
|
|
93
|
-
writeTurn(session, root, { start, baselines: {}, config, checked: false });
|
|
101
|
+
writeTurn(session, root, { start, ...head, baselines: {}, config, checked: false });
|
|
94
102
|
const cached = cachedReports(root, tree);
|
|
95
103
|
const baselines = await Promise.all(
|
|
96
104
|
compared.map(async (gate): Promise<{ gate: Gate; ok: boolean; output?: string }> => {
|
|
@@ -110,6 +118,7 @@ export async function startTurn(session: string, root: string): Promise<string[]
|
|
|
110
118
|
);
|
|
111
119
|
writeTurn(session, root, {
|
|
112
120
|
start,
|
|
121
|
+
...head,
|
|
113
122
|
baselines: Object.fromEntries(
|
|
114
123
|
baselines.flatMap(({ gate, output }) => (output === undefined ? [] : [[gate.name, output]])),
|
|
115
124
|
),
|
|
@@ -123,7 +132,7 @@ export async function startTurn(session: string, root: string): Promise<string[]
|
|
|
123
132
|
* Runs, in parallel, each gate that looks at a changed file. A deleted file counts as changed, since a gate that
|
|
124
133
|
* reads the whole project may break on it, but is never named: a gate given {files} looks at the files left, and
|
|
125
134
|
* is skipped when none is. At the end of a turn of a session, a gate that keeps a watch is judged on it instead of
|
|
126
|
-
* running.
|
|
135
|
+
* running. Elsewhere, the reports of the turn start are of code HEAD has left since.
|
|
127
136
|
*/
|
|
128
137
|
function runGates(
|
|
129
138
|
root: string,
|
|
@@ -132,6 +141,7 @@ function runGates(
|
|
|
132
141
|
turn: string,
|
|
133
142
|
baselines: Baselines,
|
|
134
143
|
session?: string,
|
|
144
|
+
elsewhere = false,
|
|
135
145
|
): Promise<Result[]> {
|
|
136
146
|
return Promise.all(
|
|
137
147
|
gates.map(async (gate) => {
|
|
@@ -139,8 +149,8 @@ function runGates(
|
|
|
139
149
|
const files = givenFiles(gate) ? matching.filter((file) => existsSync(join(root, file))) : matching;
|
|
140
150
|
if (!files.length) return { gate };
|
|
141
151
|
if (session !== undefined && keepsWatch(gate))
|
|
142
|
-
return { gate, outcome: await judgeWatch(session, root, gate, matching, baselines(gate)) };
|
|
143
|
-
return { gate, outcome: await check(root, gate, files, turn, baselines(gate)) };
|
|
152
|
+
return { gate, outcome: await judgeWatch(session, root, gate, matching, baselines(gate), elsewhere) };
|
|
153
|
+
return { gate, outcome: await check(root, gate, files, turn, baselines(gate), elsewhere) };
|
|
144
154
|
}),
|
|
145
155
|
);
|
|
146
156
|
}
|
|
@@ -179,12 +189,13 @@ export async function checkAll(root: string): Promise<CheckedTurn> {
|
|
|
179
189
|
|
|
180
190
|
// A report holds for the tree a turn ends on only if the gate ran clean on the whole project. A gate that failed
|
|
181
191
|
// keeps the report it started from, so what the turn added stays new until it is fixed, and a gate that did not run
|
|
182
|
-
// keeps none: it may still read a changed file its pattern misses, as knip reads tsconfig.json.
|
|
183
|
-
|
|
192
|
+
// keeps none: it may still read a changed file its pattern misses, as knip reads tsconfig.json. Elsewhere, the report
|
|
193
|
+
// a failed gate started from is of code HEAD has left, and is not kept either.
|
|
194
|
+
function endReports(results: Result[], baselines: Baselines, elsewhere: boolean) {
|
|
184
195
|
return results.flatMap(({ gate, outcome }) => {
|
|
185
196
|
// A gate that could not run says nothing of the tree: the report it started from was a crash, or may be stale.
|
|
186
197
|
if (!isCompared(gate) || !outcome || outcome.broken) return [];
|
|
187
|
-
const report = outcome.ok ? outcome.report : baselines(gate);
|
|
198
|
+
const report = outcome.ok ? outcome.report : elsewhere ? undefined : baselines(gate);
|
|
188
199
|
return report === undefined ? [] : [[gate, report] as const];
|
|
189
200
|
});
|
|
190
201
|
}
|
|
@@ -193,7 +204,8 @@ function endReports(results: Result[], baselines: Baselines) {
|
|
|
193
204
|
* Runs each gate that looks at a file the turn changed, with the gates as the turn started: an agent that weakens
|
|
194
205
|
* them mid-turn changes the next turn, never its own. Without a recorded start, the turn runs from the last commit
|
|
195
206
|
* with the gates of now; with a start git no longer has, from the last commit with the gates and the reports it
|
|
196
|
-
* started with: checking too much beats leaving a hole.
|
|
207
|
+
* started with: checking too much beats leaving a hole. When HEAD moved during the turn to code it did not write, as
|
|
208
|
+
* a checkout or a pull does, the turn is only the files unlike the ones that code holds: the rest came with it.
|
|
197
209
|
*/
|
|
198
210
|
export async function checkTurn(session: string, root: string): Promise<CheckedTurn> {
|
|
199
211
|
const recorded = readTurn(session, root);
|
|
@@ -210,28 +222,45 @@ export async function checkTurn(session: string, root: string): Promise<CheckedT
|
|
|
210
222
|
baselines: recorded?.baselines ?? {},
|
|
211
223
|
config: recorded?.config ?? now,
|
|
212
224
|
checked: false,
|
|
225
|
+
...(kept ? { head: kept.head, at: kept.at } : {}),
|
|
213
226
|
// What the earlier stops of the turn told the agent goes on, so its send-backs and waivers add up.
|
|
214
227
|
...(recorded?.sendBacks ? { sendBacks: recorded.sendBacks } : {}),
|
|
215
228
|
};
|
|
216
|
-
const
|
|
229
|
+
const shared = sharedDir(root);
|
|
230
|
+
const there = readThere(session, shared);
|
|
231
|
+
const moved = kept && there && movedSince(root, kept, there);
|
|
217
232
|
const started = performance.now();
|
|
218
233
|
const end = workingTree(root);
|
|
219
|
-
const
|
|
234
|
+
const since = changedSince(root, turn.start, end);
|
|
235
|
+
// A file like the one a commit under HEAD holds came with that commit: the turn did not write it.
|
|
236
|
+
const changed = (moved?.under ?? []).reduce((files, commit) => {
|
|
237
|
+
const unlike = new Set(changedSince(root, commit, end));
|
|
238
|
+
return files.filter((file) => unlike.has(file));
|
|
239
|
+
}, since);
|
|
240
|
+
// Code came with a commit, or left with one: the tree the turn started on no longer tells what the turn wrote.
|
|
241
|
+
const elsewhere = moved !== undefined && (moved.arrived || changed.length < since.length);
|
|
242
|
+
// Where a gate given the turn starts from: then, the commit that brought the code.
|
|
243
|
+
const from = (elsewhere ? moved?.to : undefined) ?? turn.start;
|
|
244
|
+
// A report of code HEAD has left serves a gate whose lines name their file, to count the ones of the turn's files.
|
|
245
|
+
const baselines: Baselines = (gate) => (elsewhere && !namesFiles(gate) ? undefined : turn.baselines[gate.name]);
|
|
220
246
|
const config = gatesOf(turn.config);
|
|
221
247
|
const { gates, refused } = config;
|
|
222
248
|
const missing = missingTools(root, gates);
|
|
223
249
|
for (const gate of gates) if (keepsWatch(gate)) keepWatch(root, gate);
|
|
224
|
-
const results = await runGates(root, runnable(gates, missing), changed,
|
|
225
|
-
cacheReports(root, end, endReports(results, baselines));
|
|
250
|
+
const results = await runGates(root, runnable(gates, missing), changed, from, baselines, session, elsewhere);
|
|
251
|
+
cacheReports(root, end, endReports(results, baselines, elsewhere));
|
|
226
252
|
writeTurn(session, root, { ...turn, checked: true });
|
|
253
|
+
if (there) writeThere(session, shared, observed(root, there, kept?.at ?? Date.now()));
|
|
227
254
|
// A compared gate with no report from the turn start can only count its exit code, and a watch has none: a green it
|
|
228
255
|
// did not earn is said.
|
|
229
256
|
const uncompared = results
|
|
230
257
|
.filter(({ gate, outcome }) => outcome && isCompared(gate) && baselines(gate) === undefined)
|
|
231
258
|
.map(({ gate }) =>
|
|
232
|
-
|
|
233
|
-
? `${gate.name} not
|
|
234
|
-
:
|
|
259
|
+
turn.baselines[gate.name] !== undefined
|
|
260
|
+
? `${gate.name} not compared: its report of the turn start is of the code HEAD left, and its lines name no file`
|
|
261
|
+
: keepsWatch(gate)
|
|
262
|
+
? `${gate.name} not judged: no report from the turn start to compare its cycle with`
|
|
263
|
+
: `${gate.name} not compared: no report from the turn start, only its exit code counted`,
|
|
235
264
|
);
|
|
236
265
|
// Only a gate the turn concerned is named: one that would not have run anyway costs nothing to miss.
|
|
237
266
|
const notRun = missing
|
|
@@ -239,6 +268,7 @@ export async function checkTurn(session: string, root: string): Promise<CheckedT
|
|
|
239
268
|
.map(({ gate, why }) => `${gate.name} not run: ${why}`);
|
|
240
269
|
const notes = [
|
|
241
270
|
...(startNote ? [startNote] : []),
|
|
271
|
+
...(elsewhere ? ["HEAD moved during the turn to code it did not write: only what the turn wrote is checked"] : []),
|
|
242
272
|
...configNote(changedGatesFiles(turn.config, now)),
|
|
243
273
|
...uncompared,
|
|
244
274
|
...refusedNotes(refused),
|
package/src/core/config.ts
CHANGED
|
@@ -26,6 +26,7 @@ const FIELDS = new Set([
|
|
|
26
26
|
"prompt",
|
|
27
27
|
"maxReportChars",
|
|
28
28
|
"omitLines",
|
|
29
|
+
"brokenExitCodes",
|
|
29
30
|
]);
|
|
30
31
|
const WATCH_FIELDS = new Set(["run", "cycleStart", "cycleEnd", "failed", "sync", "frozen"]);
|
|
31
32
|
|
|
@@ -73,6 +74,8 @@ export interface Gate {
|
|
|
73
74
|
omitLines?: string;
|
|
74
75
|
/** What the agent is told with the report of the gate. Replaced, an engine helper leaves its own line out. */
|
|
75
76
|
prompt?: Prompt;
|
|
77
|
+
/** The exit codes of its command that mean it broke, where any other than 0 means it found a problem. */
|
|
78
|
+
brokenExitCodes?: number[];
|
|
76
79
|
}
|
|
77
80
|
|
|
78
81
|
/**
|
|
@@ -176,6 +179,10 @@ function isReportChars(value: unknown): value is number {
|
|
|
176
179
|
return typeof value === "number" && Number.isInteger(value) && value > 0;
|
|
177
180
|
}
|
|
178
181
|
|
|
182
|
+
function isExitCodes(value: unknown): value is number[] {
|
|
183
|
+
return Array.isArray(value) && value.every((code) => Number.isInteger(code) && code >= 1 && code <= 255);
|
|
184
|
+
}
|
|
185
|
+
|
|
179
186
|
// Nothing unless a file says: the report then keeps the characters the engine gives every gate.
|
|
180
187
|
function maxReportCharsOf(shared: GatesFile, local: GatesFile): number | undefined {
|
|
181
188
|
const max = local.maxReportChars ?? shared.maxReportChars;
|
|
@@ -265,6 +272,7 @@ function toGate(entry: Entry, maxTimeoutSeconds: number, reportChars: number | u
|
|
|
265
272
|
watch,
|
|
266
273
|
prompt,
|
|
267
274
|
omitLines,
|
|
275
|
+
brokenExitCodes,
|
|
268
276
|
timeoutSeconds = maxTimeoutSeconds,
|
|
269
277
|
maxReportChars = reportChars,
|
|
270
278
|
} = entry;
|
|
@@ -285,6 +293,8 @@ function toGate(entry: Entry, maxTimeoutSeconds: number, reportChars: number | u
|
|
|
285
293
|
if (typeof timeoutSeconds !== "number" || timeoutSeconds <= 0) refuse("timeoutSeconds is a positive number");
|
|
286
294
|
if (maxReportChars !== undefined && !isReportChars(maxReportChars)) refuse(REPORT_CHARS_SHAPE);
|
|
287
295
|
if (omitLines !== undefined && !isPattern(omitLines)) refuse("omitLines is a regular expression");
|
|
296
|
+
if (brokenExitCodes !== undefined && !isExitCodes(brokenExitCodes))
|
|
297
|
+
refuse("brokenExitCodes is a list of exit codes, each a whole number from 1 to 255");
|
|
288
298
|
const told = prompt === undefined ? undefined : (toPrompt(prompt) ?? refuse(`prompt is ${PROMPT_SHAPE}`));
|
|
289
299
|
return {
|
|
290
300
|
name,
|
|
@@ -300,6 +310,7 @@ function toGate(entry: Entry, maxTimeoutSeconds: number, reportChars: number | u
|
|
|
300
310
|
...(told === undefined ? {} : { prompt: told }),
|
|
301
311
|
...(maxReportChars === undefined ? {} : { maxReportChars }),
|
|
302
312
|
...(omitLines === undefined ? {} : { omitLines }),
|
|
313
|
+
...(brokenExitCodes === undefined ? {} : { brokenExitCodes }),
|
|
303
314
|
};
|
|
304
315
|
}
|
|
305
316
|
|
package/src/core/head.ts
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { branchTips, type Commit, commitsPast, hasCommit, isIn } from "./snapshot.ts";
|
|
2
|
+
|
|
3
|
+
// What tells the code a turn wrote from the code that came with a branch it checked out, pulled, merged or rebased on.
|
|
4
|
+
// Nothing here reads how HEAD moved, which git tells in too many ways: only which commits were there before.
|
|
5
|
+
//
|
|
6
|
+
// A commit was there when a branch held it as the session first started a turn in the repo, when HEAD was on it as a
|
|
7
|
+
// turn of the session started, since the turns before have judged it, or when it was committed before the turn, as
|
|
8
|
+
// one a fetch brought, unless a stop of the session saw it as its own already. Any other commit is one the session
|
|
9
|
+
// made, by whatever command, in whatever worktree: checking too much beats leaving a hole.
|
|
10
|
+
|
|
11
|
+
/** What a turn records of HEAD as it starts: its commit, when the repo has one, and when, in milliseconds. */
|
|
12
|
+
export interface Head {
|
|
13
|
+
head?: string;
|
|
14
|
+
at?: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* What a session knows of the commits of a repo: the tips of its branches as its first turn there started, with the
|
|
19
|
+
* commit each of its turns started on, and the commits it made since, as its stops saw them.
|
|
20
|
+
*/
|
|
21
|
+
export interface There {
|
|
22
|
+
known: string[];
|
|
23
|
+
made: string[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The code under HEAD that the session did not write, when it is not the one the turn started on. */
|
|
27
|
+
export interface Moved {
|
|
28
|
+
/** The commits that were there, nearest under HEAD: a file like the one any of them holds is not the turn's. */
|
|
29
|
+
under: string[];
|
|
30
|
+
/** The one of them the turn did not start on, when there is one: where a gate given the turn starts from. */
|
|
31
|
+
to?: string;
|
|
32
|
+
/** Whether one of them holds code the turn did not start on, where the others only hold less of it. */
|
|
33
|
+
arrived: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function headOf(root: string): Head {
|
|
37
|
+
const { head } = branchTips(root);
|
|
38
|
+
return { ...(head === undefined ? {} : { head }), at: Date.now() };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* What a session knows of a repo as a turn starts there, from what it knew: the commit HEAD is on was there, since
|
|
43
|
+
* a turn that made it has been judged on it. Before its first turn, the tips of the branches were.
|
|
44
|
+
*/
|
|
45
|
+
export function thereOf(root: string, before?: There): There {
|
|
46
|
+
const { head, tips } = branchTips(root);
|
|
47
|
+
if (before === undefined) return { known: tips, made: [] };
|
|
48
|
+
return { known: [...new Set([...before.known, ...(head === undefined ? [] : [head])])], made: before.made };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The commits the session made, among the ones past what it knew: the ones it saw as its own, and the ones of the turn. */
|
|
52
|
+
function madeOf(past: Commit[], { made }: There, at: number): Commit[] {
|
|
53
|
+
const seen = new Set(made);
|
|
54
|
+
const started = Math.floor(at / 1000);
|
|
55
|
+
return past.filter((commit) => seen.has(commit.hash) || commit.at >= started);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* What the session knows once a turn that started then stops: with the commits the turn made, on whatever branch, so
|
|
60
|
+
* that they stay its own in the turns after. Less the known commits git no longer has, as after a deleted branch.
|
|
61
|
+
*/
|
|
62
|
+
export function observed(root: string, there: There, at: number): There {
|
|
63
|
+
const seen = (known: string[]): There => {
|
|
64
|
+
const mine = madeOf(commitsPast(root, branchTips(root).tips, known), there, at);
|
|
65
|
+
return { known, made: [...new Set([...there.made, ...mine.map(({ hash }) => hash)])] };
|
|
66
|
+
};
|
|
67
|
+
try {
|
|
68
|
+
return seen(there.known);
|
|
69
|
+
} catch {
|
|
70
|
+
const kept = there.known.filter((commit) => hasCommit(root, commit));
|
|
71
|
+
try {
|
|
72
|
+
return seen(kept);
|
|
73
|
+
} catch {
|
|
74
|
+
return { known: kept, made: there.made };
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The commits that were there, nearest under a commit: itself, or the ones under the commits the session made. */
|
|
80
|
+
function under(tip: string, made: Map<string, string[]>): string[] {
|
|
81
|
+
const there = new Set<string>();
|
|
82
|
+
const seen = new Set<string>();
|
|
83
|
+
const left = [tip];
|
|
84
|
+
for (let commit = left.pop(); commit !== undefined; commit = left.pop()) {
|
|
85
|
+
if (seen.has(commit)) continue;
|
|
86
|
+
seen.add(commit);
|
|
87
|
+
const parents = made.get(commit);
|
|
88
|
+
if (parents === undefined) there.add(commit);
|
|
89
|
+
else left.push(...parents);
|
|
90
|
+
}
|
|
91
|
+
return [...there];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Whether the code under HEAD that the session did not write changed since the turn started, as after a checkout, a
|
|
96
|
+
* pull, a merge, a rebase or a reset. Nothing when it did not, as after a commit of the turn, or a branch it left and
|
|
97
|
+
* came back to; nor when nothing tells any more, where all the turn changed is checked.
|
|
98
|
+
*/
|
|
99
|
+
export function movedSince(root: string, { head, at }: Head, there: There): Moved | undefined {
|
|
100
|
+
const now = branchTips(root).head;
|
|
101
|
+
if (head === undefined || at === undefined || now === undefined || now === head) return undefined;
|
|
102
|
+
let past: Commit[];
|
|
103
|
+
try {
|
|
104
|
+
past = commitsPast(root, [now, head], there.known);
|
|
105
|
+
} catch {
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
const made = new Map(madeOf(past, there, at).map(({ hash, parents }) => [hash, parents]));
|
|
109
|
+
const [before, after] = [under(head, made), under(now, made)];
|
|
110
|
+
if (after.length === before.length && after.every((commit) => before.includes(commit))) return undefined;
|
|
111
|
+
const brought = after.find((commit) => !before.some((had) => isIn(root, commit, had)));
|
|
112
|
+
const to = brought ?? after.find((commit) => !before.includes(commit));
|
|
113
|
+
return { under: after, arrived: brought !== undefined, ...(to === undefined ? {} : { to }) };
|
|
114
|
+
}
|
package/src/core/report.ts
CHANGED
|
@@ -168,6 +168,25 @@ function sent(report: string, omitLines: string | undefined): string {
|
|
|
168
168
|
return kept.some((line) => line.trim()) ? kept.join("\n") : report;
|
|
169
169
|
}
|
|
170
170
|
|
|
171
|
+
// A helper of the engine counts apart the errors of one source line, with `(2)` after the source it quotes and before
|
|
172
|
+
// the tab that ends their key, so that one more of them is new. The agent reads each of them once, with how many
|
|
173
|
+
// times it stands. A line of another tool is left as it is.
|
|
174
|
+
const COUNTED_APART = /` \(\d+\)\t/;
|
|
175
|
+
|
|
176
|
+
function saidOnce(report: string): string {
|
|
177
|
+
if (!COUNTED_APART.test(report)) return report;
|
|
178
|
+
const lines = report.split("\n");
|
|
179
|
+
const keyed = lines.map((line) => line.replace(COUNTED_APART, "`\t"));
|
|
180
|
+
const counted = new Set(keyed.filter((line, at) => line !== lines[at]));
|
|
181
|
+
const times = new Map<string, number>();
|
|
182
|
+
for (const line of keyed) if (counted.has(line)) times.set(line, (times.get(line) ?? 0) + 1);
|
|
183
|
+
const said = new Set<string>();
|
|
184
|
+
return keyed
|
|
185
|
+
.filter((line) => !times.has(line) || (!said.has(line) && said.add(line)))
|
|
186
|
+
.map((line) => ((times.get(line) ?? 1) > 1 ? `${line} (×${times.get(line)})` : line))
|
|
187
|
+
.join("\n");
|
|
188
|
+
}
|
|
189
|
+
|
|
171
190
|
// The characters each report gets of a total: what it needs when all fit, else the short ones whole and the rest of
|
|
172
191
|
// the total in equal shares for the long ones.
|
|
173
192
|
function shares(needs: number[], total: number): number[] {
|
|
@@ -205,13 +224,13 @@ export function toolsToInstall(missing: string[], prompt?: Prompt): string {
|
|
|
205
224
|
|
|
206
225
|
/**
|
|
207
226
|
* The report of each failed gate, under its name, how to act on one the turn broke, and the prompt of the repo for it:
|
|
208
|
-
* less the lines the gate omits, in the characters it has, and in its share of a total when the reports have one.
|
|
227
|
+
* less the lines the gate omits, each line a helper counts apart said once, in the characters it has, and in its share of a total when the reports have one.
|
|
209
228
|
*/
|
|
210
229
|
export function reports({ outcomes, wording = {} }: Pick<CheckedTurn, "outcomes" | "wording">): string[] {
|
|
211
230
|
const { gates = [], prompts, maxTotalReportChars = Infinity } = wording;
|
|
212
231
|
const red = outcomes.filter(failed).map(({ name, output, broken }) => {
|
|
213
232
|
const gate = gates.find((known) => known.name === name);
|
|
214
|
-
const report = sent(output, gate?.omitLines);
|
|
233
|
+
const report = saidOnce(sent(output, gate?.omitLines));
|
|
215
234
|
const told = [
|
|
216
235
|
...(broken ? [worded(BROKEN_GATE, prompts?.brokenGate, "\n")] : []),
|
|
217
236
|
...(gate?.prompt ? [gate.prompt.text] : []),
|
package/src/core/run.ts
CHANGED
|
@@ -205,6 +205,8 @@ interface Executed {
|
|
|
205
205
|
timedOut: boolean;
|
|
206
206
|
/** The command was never started: it is missing, or the engine cannot resolve it. */
|
|
207
207
|
cannotStart: boolean;
|
|
208
|
+
/** The code the command exited with, when it ran to its end. */
|
|
209
|
+
exitCode?: number;
|
|
208
210
|
}
|
|
209
211
|
|
|
210
212
|
async function execute(root: string, gate: Gate, args: string[]): Promise<Executed> {
|
|
@@ -240,6 +242,7 @@ async function execute(root: string, gate: Gate, args: string[]): Promise<Execut
|
|
|
240
242
|
ms: performance.now() - started,
|
|
241
243
|
timedOut: error?.killed === true && error.code !== "ERR_CHILD_PROCESS_STDIO_MAXBUFFER",
|
|
242
244
|
cannotStart: error?.code === "ENOENT",
|
|
245
|
+
...(typeof error?.code === "number" ? { exitCode: error.code } : {}),
|
|
243
246
|
});
|
|
244
247
|
},
|
|
245
248
|
);
|
|
@@ -282,9 +285,20 @@ export async function wholeProject(
|
|
|
282
285
|
return timedOut ? { ok: false } : { ok, output };
|
|
283
286
|
}
|
|
284
287
|
|
|
288
|
+
/** The file a line of a compared gate names, by the group of its key named file. */
|
|
289
|
+
function fileOf(gate: Gate, line: string): string | undefined {
|
|
290
|
+
return gate.lineKey === undefined ? undefined : new RegExp(gate.lineKey).exec(line)?.groups?.file;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** Whether the lines of a compared gate name their file, so that they can be told from the ones of other files. */
|
|
294
|
+
export function namesFiles(gate: Gate): boolean {
|
|
295
|
+
return /\(\?<file>/.test(gate.lineKey ?? "");
|
|
296
|
+
}
|
|
297
|
+
|
|
285
298
|
/**
|
|
286
299
|
* Runs a gate on the turn. A compared gate fails on the lines of output the turn added, and is broken on any exit
|
|
287
|
-
* code but 0: the same crash at the start and at the end of a turn prints no new line.
|
|
300
|
+
* code but 0: the same crash at the start and at the end of a turn prints no new line. Another gate is broken on the
|
|
301
|
+
* exit codes it names as such. A gate out of time is neither.
|
|
288
302
|
*/
|
|
289
303
|
export async function check(
|
|
290
304
|
root: string,
|
|
@@ -292,27 +306,34 @@ export async function check(
|
|
|
292
306
|
files: string[],
|
|
293
307
|
turn: string,
|
|
294
308
|
baseline: string | undefined,
|
|
309
|
+
elsewhere = false,
|
|
295
310
|
): Promise<Outcome> {
|
|
296
|
-
const
|
|
311
|
+
const executed = await execute(root, gate, argumentsOf(gate, files, turn));
|
|
312
|
+
const { ok, output, ms, timedOut, cannotStart, exitCode = 0 } = executed;
|
|
297
313
|
const ran = { name: gate.name, ms, report: ok ? output : undefined };
|
|
298
314
|
if (timedOut) return { ...ran, ok, output, timedOut };
|
|
299
315
|
// A compared gate exits with 0 when it finds problems: any other code is its command that broke.
|
|
300
|
-
|
|
316
|
+
const broke = cannotStart || isCompared(gate) || gate.brokenExitCodes?.includes(exitCode);
|
|
317
|
+
if (!ok && broke) return { ...ran, ok, output, broken: true };
|
|
301
318
|
if (!isCompared(gate) || !ok) return { ...ran, ok, output };
|
|
302
|
-
return { ...ran, ...addedLines(gate, output, baseline, files) };
|
|
319
|
+
return { ...ran, ...addedLines(gate, output, baseline, files, elsewhere) };
|
|
303
320
|
}
|
|
304
321
|
|
|
305
322
|
/**
|
|
306
323
|
* Judges the output of a compared gate that ran clean: it fails on the lines the turn added to the report of the turn
|
|
307
|
-
* start. Without that report, nothing counts but the exit code.
|
|
324
|
+
* start. Without that report, nothing counts but the exit code. With a report of code HEAD has left since, elsewhere,
|
|
325
|
+
* only the lines that name a file the turn changed count: the others came with the code HEAD moved to.
|
|
308
326
|
*/
|
|
309
327
|
export function addedLines(
|
|
310
328
|
gate: Gate,
|
|
311
329
|
output: string,
|
|
312
330
|
baseline: string | undefined,
|
|
313
331
|
changed: string[],
|
|
332
|
+
elsewhere = false,
|
|
314
333
|
): { ok: boolean; output: string } {
|
|
315
334
|
if (baseline === undefined) return { ok: true, output: "" };
|
|
316
|
-
const added = newLines(baseline, output, gate.ignoreLines, gate.lineKey, changed)
|
|
335
|
+
const added = newLines(baseline, output, gate.ignoreLines, gate.lineKey, changed).filter(
|
|
336
|
+
(line) => !elsewhere || changed.includes(fileOf(gate, line) ?? ""),
|
|
337
|
+
);
|
|
317
338
|
return { ok: added.length === 0, output: added.join("\n") };
|
|
318
339
|
}
|