@tayomi/deterministic-gates 0.6.0 → 0.6.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/index.html +2 -2
- package/catalog/typescript.json +2 -2
- package/dist/src/core/check.js +4 -2
- package/dist/src/core/run.js +38 -22
- package/dist/src/core/watch.js +5 -5
- package/dist/src/helpers/angular-watch.js +46 -13
- package/package.json +1 -1
- package/src/core/check.ts +4 -2
- package/src/core/run.ts +55 -23
- package/src/core/watch.ts +5 -5
- package/src/helpers/angular-watch.ts +39 -13
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/claude-code-plugin-manifest.json",
|
|
3
3
|
"name": "deterministic-gates",
|
|
4
|
-
"version": "0.6.
|
|
4
|
+
"version": "0.6.1",
|
|
5
5
|
"description": "Deterministic backpressure for coding agents: gates that run on what each turn changed and send the agent back until they pass.",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Pierre Moati",
|
package/catalog/index.html
CHANGED
|
@@ -437,7 +437,7 @@ summary { cursor: pointer; color: var(--muted); }
|
|
|
437
437
|
</article>
|
|
438
438
|
<article id="typescript/no-new-strict-errors">
|
|
439
439
|
<h3><code>typescript/no-new-strict-errors</code></h3>
|
|
440
|
-
<p>Type-checks the project with strictNullChecks and strictPropertyInitialization on, and fails only on the errors the turn added: a project that turned them off, with more errors than it can fix at once, stops growing new ones. An error is told by its file, code and source line, since tsc prints the types of a message otherwise from one compilation to the next: one a change moves to another line is not new, nor one tsc reports in another file
|
|
440
|
+
<p>Type-checks the project with strictNullChecks and strictPropertyInitialization on, and fails only on the errors the turn added: a project that turned them off, with more errors than it can fix at once, stops growing new ones. An error is told by its file, code and source line, since tsc prints the types of a message otherwise from one compilation to the next: one a change moves to another line is not new, nor one tsc reports with the same message in another file the turn did not change, nor one on a line the turn changed, as long as its file has no more errors of its code than at the turn start. The agent still reads the message. tsc keeps watching between turns; without the watch, it runs at each prompt and stop instead. Refused when tsconfig.json only references others, as in an Angular project, since tsc would check no file: give it -p and a tsconfig. The whole check counts every error.</p>
|
|
441
441
|
<dl>
|
|
442
442
|
<dt>Runs</dt><dd><code>{engine}/src/helpers/type-errors.ts --strictNullChecks --strictPropertyInitialization</code></dd>
|
|
443
443
|
<dt>When the turn changes</dt><dd><code>(\.(ts|tsx)|^tsconfig.*\.json)$</code></dd>
|
|
@@ -453,7 +453,7 @@ summary { cursor: pointer; color: var(--muted); }
|
|
|
453
453
|
"--strictPropertyInitialization"
|
|
454
454
|
],
|
|
455
455
|
"compare": "turn-start",
|
|
456
|
-
"lineKey": "^(
|
|
456
|
+
"lineKey": "^((?<file>.+?): error TS\\d+) at [^\\t]*",
|
|
457
457
|
"watch": {
|
|
458
458
|
"run": [
|
|
459
459
|
"{engine}/src/helpers/type-errors.ts",
|
package/catalog/typescript.json
CHANGED
|
@@ -96,7 +96,7 @@
|
|
|
96
96
|
}
|
|
97
97
|
},
|
|
98
98
|
{
|
|
99
|
-
"description": "Type-checks the project with strictNullChecks and strictPropertyInitialization on, and fails only on the errors the turn added: a project that turned them off, with more errors than it can fix at once, stops growing new ones. An error is told by its file, code and source line, since tsc prints the types of a message otherwise from one compilation to the next: one a change moves to another line is not new, nor one tsc reports in another file
|
|
99
|
+
"description": "Type-checks the project with strictNullChecks and strictPropertyInitialization on, and fails only on the errors the turn added: a project that turned them off, with more errors than it can fix at once, stops growing new ones. An error is told by its file, code and source line, since tsc prints the types of a message otherwise from one compilation to the next: one a change moves to another line is not new, nor one tsc reports with the same message in another file the turn did not change, nor one on a line the turn changed, as long as its file has no more errors of its code than at the turn start. The agent still reads the message. tsc keeps watching between turns; without the watch, it runs at each prompt and stop instead. Refused when tsconfig.json only references others, as in an Angular project, since tsc would check no file: give it -p and a tsconfig. The whole check counts every error.",
|
|
100
100
|
"requires": [
|
|
101
101
|
{
|
|
102
102
|
"tool": "typescript",
|
|
@@ -109,7 +109,7 @@
|
|
|
109
109
|
"files": "(\\.(ts|tsx)|^tsconfig.*\\.json)$",
|
|
110
110
|
"run": ["{engine}/src/helpers/type-errors.ts", "--strictNullChecks", "--strictPropertyInitialization"],
|
|
111
111
|
"compare": "turn-start",
|
|
112
|
-
"lineKey": "^(
|
|
112
|
+
"lineKey": "^((?<file>.+?): error TS\\d+) at [^\\t]*",
|
|
113
113
|
"watch": {
|
|
114
114
|
"run": [
|
|
115
115
|
"{engine}/src/helpers/type-errors.ts",
|
package/dist/src/core/check.js
CHANGED
|
@@ -5,7 +5,7 @@ import { cachedReports, cacheReports } from "./report-cache.js";
|
|
|
5
5
|
import { check, filesOf, givenFiles, missingTool, wholeProject } from "./run.js";
|
|
6
6
|
import { changedSince, emptyCommit, hasCommit, lastCommit, snapshot, workingTree } from "./snapshot.js";
|
|
7
7
|
import { readTurn, writeTurn } from "./turn-state.js";
|
|
8
|
-
import { judgeWatch, keepWatch, startWatch, watchReport } from "./watch.js";
|
|
8
|
+
import { judgeWatch, keepWatch, startWatch, TURN_START_MS, watchReport } from "./watch.js";
|
|
9
9
|
function missingTools(root, gates) {
|
|
10
10
|
return gates.flatMap((gate) => {
|
|
11
11
|
const why = missingTool(root, gate);
|
|
@@ -43,13 +43,15 @@ export async function startTurn(session, root) {
|
|
|
43
43
|
return told;
|
|
44
44
|
}
|
|
45
45
|
const { commit: start, tree } = snapshot(root);
|
|
46
|
+
// The turn starts before its reports are ready: a hook stopped for lack of time leaves it, never the turn before.
|
|
47
|
+
writeTurn(session, root, { start, baselines: {}, config, checked: false });
|
|
46
48
|
const cached = cachedReports(root, tree);
|
|
47
49
|
const baselines = await Promise.all(compared.map(async (gate) => {
|
|
48
50
|
const cachedReport = cached(gate);
|
|
49
51
|
if (cachedReport !== undefined)
|
|
50
52
|
return { gate, ok: true, output: cachedReport };
|
|
51
53
|
if (!keepsWatch(gate))
|
|
52
|
-
return { gate, ...(await wholeProject(root, gate)) };
|
|
54
|
+
return { gate, ...(await wholeProject(root, gate, TURN_START_MS)) };
|
|
53
55
|
const output = await watchReport(session, root, gate);
|
|
54
56
|
return { gate, ok: output !== undefined, output };
|
|
55
57
|
}));
|
package/dist/src/core/run.js
CHANGED
|
@@ -12,8 +12,19 @@ const ENGINE = resolve(import.meta.dirname, "..", "..");
|
|
|
12
12
|
const manifest = readJsonFile(join(ENGINE, "package.json"));
|
|
13
13
|
/** The version of this engine: what its helpers print, so what a report holds, may change from one to the next. */
|
|
14
14
|
export const ENGINE_VERSION = isRecord(manifest) ? String(manifest.version) : "";
|
|
15
|
-
/**
|
|
16
|
-
|
|
15
|
+
/**
|
|
16
|
+
* What a tool of a repo runs with: no colour, which some tools print anyway, and the commands of its node_modules
|
|
17
|
+
* first on the PATH, as npx and the package managers run them, so that a tool finds the Node the repo pins.
|
|
18
|
+
*/
|
|
19
|
+
export function toolEnv(root) {
|
|
20
|
+
const path = [join(root, "node_modules", ".bin"), ...(process.env.PATH ? [process.env.PATH] : [])];
|
|
21
|
+
return { ...process.env, NO_COLOR: "1", FORCE_COLOR: "0", PATH: path.join(delimiter) };
|
|
22
|
+
}
|
|
23
|
+
/** The Node a script of the repo or of its packages runs on: the one the repo pins in its node_modules, as npx finds it, or the one of the engine. */
|
|
24
|
+
function repoNode(root) {
|
|
25
|
+
const pinned = join(root, "node_modules", ".bin", "node");
|
|
26
|
+
return executable(pinned) ? pinned : process.execPath;
|
|
27
|
+
}
|
|
17
28
|
const MAX_OUTPUT_MB = 16;
|
|
18
29
|
const ENGINE_PREFIX = "{engine}/";
|
|
19
30
|
/** A script of this engine: its .ts source, or the .js it is built to when the engine runs from its npm package. */
|
|
@@ -23,8 +34,8 @@ function engineScript(name) {
|
|
|
23
34
|
}
|
|
24
35
|
// A script of the repo runs on the interpreter of its extension.
|
|
25
36
|
const INTERPRETERS = [
|
|
26
|
-
[/\.(ts|js|mjs)$/,
|
|
27
|
-
[/\.py$/, "python3"],
|
|
37
|
+
[/\.(ts|js|mjs)$/, repoNode],
|
|
38
|
+
[/\.py$/, () => "python3"],
|
|
28
39
|
];
|
|
29
40
|
/** The script of a package of the repo's node_modules, or nothing when the repo does not have the package. */
|
|
30
41
|
export function packageScript(root, name) {
|
|
@@ -48,7 +59,7 @@ export function commandOf(root, name) {
|
|
|
48
59
|
if (name.startsWith(ENGINE_PREFIX)) {
|
|
49
60
|
return { file: process.execPath, args: [engineScript(name.slice(ENGINE_PREFIX.length))], missing: "" };
|
|
50
61
|
}
|
|
51
|
-
const interpreter = INTERPRETERS.find(([extension]) => extension.test(name))?.[1];
|
|
62
|
+
const interpreter = INTERPRETERS.find(([extension]) => extension.test(name))?.[1](root);
|
|
52
63
|
if (interpreter !== undefined) {
|
|
53
64
|
return {
|
|
54
65
|
file: interpreter,
|
|
@@ -58,7 +69,7 @@ export function commandOf(root, name) {
|
|
|
58
69
|
}
|
|
59
70
|
const script = packageScript(root, name);
|
|
60
71
|
if (script !== undefined)
|
|
61
|
-
return { file:
|
|
72
|
+
return { file: repoNode(root), args: [script], missing: "" };
|
|
62
73
|
if (name.includes("/"))
|
|
63
74
|
return { file: resolve(root, name), args: [], missing: `${name} does not exist.` };
|
|
64
75
|
return {
|
|
@@ -68,10 +79,8 @@ export function commandOf(root, name) {
|
|
|
68
79
|
};
|
|
69
80
|
}
|
|
70
81
|
// A program by its path, or by its name on the PATH, that the system would execute.
|
|
71
|
-
function executable(file) {
|
|
72
|
-
const candidates = isAbsolute(file)
|
|
73
|
-
? [file]
|
|
74
|
-
: (process.env.PATH ?? "").split(delimiter).map((dir) => join(dir, file));
|
|
82
|
+
function executable(file, path = process.env.PATH ?? "") {
|
|
83
|
+
const candidates = isAbsolute(file) ? [file] : path.split(delimiter).map((dir) => join(dir, file));
|
|
75
84
|
return candidates.some((candidate) => {
|
|
76
85
|
try {
|
|
77
86
|
accessSync(candidate, constants.X_OK);
|
|
@@ -90,7 +99,7 @@ function missingProgram(root, name) {
|
|
|
90
99
|
catch {
|
|
91
100
|
return undefined;
|
|
92
101
|
}
|
|
93
|
-
return command.missing && !executable(command.file) ? command.missing : undefined;
|
|
102
|
+
return command.missing && !executable(command.file, toolEnv(root).PATH) ? command.missing : undefined;
|
|
94
103
|
}
|
|
95
104
|
/**
|
|
96
105
|
* Why the program of a gate, or of its watch, cannot run on this machine, or nothing when they can: looked up as the
|
|
@@ -113,10 +122,11 @@ export function filesOf(gate, changed) {
|
|
|
113
122
|
/**
|
|
114
123
|
* The lines of output a turn added, less the ignored ones. With a key, a line is told by the part the key matches. A
|
|
115
124
|
* line of a new key is still an old one, each line of the turn start whose key went standing for one line only: first
|
|
116
|
-
* when it says the same as such a line, that problem reported elsewhere
|
|
117
|
-
*
|
|
125
|
+
* when it says the same as such a line, that problem reported elsewhere, unless the group of the key named file names
|
|
126
|
+
* a file the turn changed, where the agent may have written it; then when the first group of the key, its place, is
|
|
127
|
+
* the one of such a line, that problem changed where it is.
|
|
118
128
|
*/
|
|
119
|
-
export function newLines(before, after, ignore, key) {
|
|
129
|
+
export function newLines(before, after, ignore, key, changed = []) {
|
|
120
130
|
const ignored = ignore === undefined ? undefined : new RegExp(ignore);
|
|
121
131
|
const keyed = key === undefined ? undefined : new RegExp(key);
|
|
122
132
|
const lines = (text) => text
|
|
@@ -125,6 +135,7 @@ export function newLines(before, after, ignore, key) {
|
|
|
125
135
|
.filter(Boolean);
|
|
126
136
|
const keyOf = (line) => keyed?.exec(line)?.[0] ?? line;
|
|
127
137
|
const placeOf = (line) => keyed?.exec(line)?.[1];
|
|
138
|
+
const written = (line) => changed.includes(keyed?.exec(line)?.groups?.file ?? "");
|
|
128
139
|
const restOf = (line) => line.replace(keyOf(line), "").trim();
|
|
129
140
|
const old = lines(before);
|
|
130
141
|
const now = lines(after);
|
|
@@ -139,7 +150,7 @@ export function newLines(before, after, ignore, key) {
|
|
|
139
150
|
};
|
|
140
151
|
return now
|
|
141
152
|
.filter((line) => !oldKeys.has(keyOf(line)) && !ignored?.test(line))
|
|
142
|
-
.filter((line) => !restOf(line) || !stillOld((was) => restOf(was) === restOf(line)))
|
|
153
|
+
.filter((line) => !restOf(line) || written(line) || !stillOld((was) => restOf(was) === restOf(line)))
|
|
143
154
|
.filter((line) => placeOf(line) === undefined || !stillOld((was) => placeOf(was) === placeOf(line)));
|
|
144
155
|
}
|
|
145
156
|
async function execute(root, gate, args) {
|
|
@@ -158,7 +169,7 @@ async function execute(root, gate, args) {
|
|
|
158
169
|
cwd: root,
|
|
159
170
|
timeout: gate.timeoutSeconds * 1000,
|
|
160
171
|
maxBuffer: MAX_OUTPUT_MB * 1024 * 1024,
|
|
161
|
-
env:
|
|
172
|
+
env: toolEnv(root),
|
|
162
173
|
}, (error, stdout, stderr) => {
|
|
163
174
|
resolve({
|
|
164
175
|
ok: !error,
|
|
@@ -185,9 +196,14 @@ function outputOf(root, gate, command, error, printed) {
|
|
|
185
196
|
export function cleaned(root, printed) {
|
|
186
197
|
return stripVTControlCharacters(printed).split(`${root}${sep}`).join("").trim();
|
|
187
198
|
}
|
|
188
|
-
/**
|
|
189
|
-
|
|
190
|
-
|
|
199
|
+
/**
|
|
200
|
+
* Runs a compared gate on the whole project, as the turn starts, in the time the gate has or less: nothing when it
|
|
201
|
+
* runs out of it, since its lines would all be new at the stop.
|
|
202
|
+
*/
|
|
203
|
+
export async function wholeProject(root, gate, ms = gate.timeoutSeconds * 1000) {
|
|
204
|
+
const timeoutSeconds = Math.min(gate.timeoutSeconds, ms / 1000);
|
|
205
|
+
const { ok, output, timedOut } = await execute(root, { ...gate, timeoutSeconds }, gate.run.slice(1));
|
|
206
|
+
return timedOut ? { ok: false } : { ok, output };
|
|
191
207
|
}
|
|
192
208
|
/**
|
|
193
209
|
* Runs a gate on the turn. A compared gate fails on the lines of output the turn added, and on any exit code but 0:
|
|
@@ -200,15 +216,15 @@ export async function check(root, gate, files, turn, baseline) {
|
|
|
200
216
|
return { ...ran, ok, output, timedOut };
|
|
201
217
|
if (!isCompared(gate) || !ok)
|
|
202
218
|
return { ...ran, ok, output };
|
|
203
|
-
return { ...ran, ...addedLines(gate, output, baseline) };
|
|
219
|
+
return { ...ran, ...addedLines(gate, output, baseline, files) };
|
|
204
220
|
}
|
|
205
221
|
/**
|
|
206
222
|
* Judges the output of a compared gate that ran clean: it fails on the lines the turn added to the report of the turn
|
|
207
223
|
* start. Without that report, nothing counts but the exit code.
|
|
208
224
|
*/
|
|
209
|
-
export function addedLines(gate, output, baseline) {
|
|
225
|
+
export function addedLines(gate, output, baseline, changed) {
|
|
210
226
|
if (baseline === undefined)
|
|
211
227
|
return { ok: true, output: "" };
|
|
212
|
-
const added = newLines(baseline, output, gate.ignoreLines, gate.lineKey);
|
|
228
|
+
const added = newLines(baseline, output, gate.ignoreLines, gate.lineKey, changed);
|
|
213
229
|
return { ok: added.length === 0, output: added.join("\n") };
|
|
214
230
|
}
|
package/dist/src/core/watch.js
CHANGED
|
@@ -6,7 +6,7 @@ import { dirname, join } from "node:path";
|
|
|
6
6
|
import { createInterface } from "node:readline";
|
|
7
7
|
import { setTimeout as sleep } from "node:timers/promises";
|
|
8
8
|
import { isCompared } from "./config.js";
|
|
9
|
-
import { addedLines, cleaned, commandOf, ENGINE_VERSION,
|
|
9
|
+
import { addedLines, cleaned, commandOf, ENGINE_VERSION, toolEnv } from "./run.js";
|
|
10
10
|
// Once a cycle closes, the lines a watch prints after it, such as the errors of a failed build, come within this long.
|
|
11
11
|
const QUIET_MS = 1000;
|
|
12
12
|
// A change no cycle started after in this long is one the watch does not look at, such as a spec its build leaves out:
|
|
@@ -15,8 +15,8 @@ const UNSEEN_MS = 5000;
|
|
|
15
15
|
const POLL_MS = 100;
|
|
16
16
|
// What a watch that stopped leaves the agent: enough to see the error, not the whole history of the session.
|
|
17
17
|
const LAST_LINES = 20;
|
|
18
|
-
// A prompt waits this long at most for
|
|
19
|
-
const TURN_START_MS = 45_000;
|
|
18
|
+
// A prompt waits this long at most for the reports of its compared gates, under the 60 s its hook has.
|
|
19
|
+
export const TURN_START_MS = 45_000;
|
|
20
20
|
const PREFIX = "gates-watch-";
|
|
21
21
|
// A start that holds its lock longer than this died on the way: the next hook takes the lock over.
|
|
22
22
|
const STALE_START_MS = 10_000;
|
|
@@ -120,7 +120,7 @@ function launch(folder, root, watch) {
|
|
|
120
120
|
cwd: root,
|
|
121
121
|
detached: true,
|
|
122
122
|
stdio: ["ignore", log, log],
|
|
123
|
-
env:
|
|
123
|
+
env: toolEnv(root),
|
|
124
124
|
});
|
|
125
125
|
closeSync(log);
|
|
126
126
|
child.on("error", () => { });
|
|
@@ -211,7 +211,7 @@ export async function judgeWatch(session, root, gate, files, baseline) {
|
|
|
211
211
|
const { cycle } = settled;
|
|
212
212
|
const fails = new RegExp(gate.watch.failed ?? "(?!)");
|
|
213
213
|
const judged = isCompared(gate)
|
|
214
|
-
? addedLines(gate, cycle, baseline)
|
|
214
|
+
? addedLines(gate, cycle, baseline, files)
|
|
215
215
|
: { ok: !cycle.split("\n").some((line) => fails.test(line)), output: cycle };
|
|
216
216
|
return { name, ms, ...judged, report: judged.ok ? cycle : undefined };
|
|
217
217
|
}
|
|
@@ -4,25 +4,48 @@ import { createInterface } from "node:readline";
|
|
|
4
4
|
import { commandOf } from "../core/run.js";
|
|
5
5
|
import { COUNTS, HEADER } from "./vitest-state-reporter.js";
|
|
6
6
|
// The watch of an Angular project tested with Vitest: ng test in watch mode, with the reporter of the engine that
|
|
7
|
-
// prints the state of every spec at each run, and NG_TEST_LOG, which makes Angular say when a run ends.
|
|
8
|
-
//
|
|
9
|
-
//
|
|
7
|
+
// prints the state of every spec at each run, and NG_TEST_LOG, which makes Angular say when a run ends. It prints
|
|
8
|
+
// only what judges a run: the build also prints the files it built, a table longer than the report of the specs in a
|
|
9
|
+
// large project, whose names change at each build.
|
|
10
10
|
const NO_RERUN = /No test specifications to rerun/;
|
|
11
|
+
const KEPT = [/Changes detected\. Rebuilding/, /bundle generation failed/, NO_RERUN];
|
|
12
|
+
const ENDS = /Test run finished with success/;
|
|
13
|
+
// Angular says why a run ended, and says all tests passed of a run with failed specs.
|
|
14
|
+
const REASON = /\s*Reason: .*$/;
|
|
15
|
+
const ERROR = /^\S+ \[ERROR\] /;
|
|
16
|
+
// What a watch that stopped leaves, so that a crash still says why.
|
|
17
|
+
const LAST_LINES = 20;
|
|
11
18
|
/**
|
|
12
|
-
* What to print for each line of
|
|
13
|
-
*
|
|
19
|
+
* What to print for each line of ng test: the lines that open a run, the state of the specs, the errors of a build
|
|
20
|
+
* that failed, with the lines under each, and the line that ends a run, less its reason. A change that leaves the
|
|
21
|
+
* build as it was reruns no spec, so no state prints: the last one is printed again, so that the cycle still tells
|
|
22
|
+
* which specs are red.
|
|
14
23
|
*/
|
|
15
|
-
export function
|
|
24
|
+
export function reporting() {
|
|
16
25
|
let state = [];
|
|
17
26
|
let reading;
|
|
27
|
+
let inError = false;
|
|
18
28
|
return (line) => {
|
|
19
29
|
if (line === HEADER)
|
|
20
30
|
reading = [];
|
|
21
|
-
reading
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
31
|
+
if (reading !== undefined) {
|
|
32
|
+
reading.push(line);
|
|
33
|
+
if (line.startsWith(COUNTS)) {
|
|
34
|
+
state = reading;
|
|
35
|
+
reading = undefined;
|
|
36
|
+
}
|
|
37
|
+
return [line];
|
|
25
38
|
}
|
|
39
|
+
if (ERROR.test(line))
|
|
40
|
+
inError = true;
|
|
41
|
+
else if (line.trim() && !/^\s/.test(line))
|
|
42
|
+
inError = false;
|
|
43
|
+
if (inError)
|
|
44
|
+
return [line];
|
|
45
|
+
if (ENDS.test(line))
|
|
46
|
+
return [line.replace(REASON, "")];
|
|
47
|
+
if (!KEPT.some((kept) => kept.test(line)))
|
|
48
|
+
return [];
|
|
26
49
|
return NO_RERUN.test(line) ? [...state, line] : [line];
|
|
27
50
|
};
|
|
28
51
|
}
|
|
@@ -37,11 +60,21 @@ function main(root, args) {
|
|
|
37
60
|
env: { ...process.env, NG_TEST_LOG: "1" },
|
|
38
61
|
stdio: ["ignore", "pipe", "pipe"],
|
|
39
62
|
});
|
|
40
|
-
const
|
|
41
|
-
const
|
|
63
|
+
const report = reporting();
|
|
64
|
+
const last = [];
|
|
65
|
+
const forward = (line) => {
|
|
66
|
+
last.push(line);
|
|
67
|
+
if (last.length > LAST_LINES)
|
|
68
|
+
last.shift();
|
|
69
|
+
for (const shown of report(line))
|
|
70
|
+
console.log(shown);
|
|
71
|
+
};
|
|
42
72
|
createInterface({ input: watch.stdout }).on("line", forward);
|
|
43
73
|
createInterface({ input: watch.stderr }).on("line", forward);
|
|
44
|
-
watch.on("exit", (code) =>
|
|
74
|
+
watch.on("exit", (code) => {
|
|
75
|
+
console.log(["ng test stopped. Its last lines:", ...last].join("\n"));
|
|
76
|
+
process.exit(code ?? 1);
|
|
77
|
+
});
|
|
45
78
|
}
|
|
46
79
|
if (import.meta.main)
|
|
47
80
|
main(process.cwd(), process.argv.slice(2));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tayomi/deterministic-gates",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.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
|
@@ -5,7 +5,7 @@ import { cachedReports, cacheReports } from "./report-cache.ts";
|
|
|
5
5
|
import { check, filesOf, givenFiles, missingTool, type Outcome, wholeProject } from "./run.ts";
|
|
6
6
|
import { changedSince, emptyCommit, hasCommit, lastCommit, snapshot, workingTree } from "./snapshot.ts";
|
|
7
7
|
import { readTurn, type TurnState, writeTurn } from "./turn-state.ts";
|
|
8
|
-
import { judgeWatch, keepWatch, startWatch, watchReport } from "./watch.ts";
|
|
8
|
+
import { judgeWatch, keepWatch, startWatch, TURN_START_MS, watchReport } from "./watch.ts";
|
|
9
9
|
|
|
10
10
|
export interface CheckedTurn {
|
|
11
11
|
changed: string[];
|
|
@@ -70,12 +70,14 @@ export async function startTurn(session: string, root: string): Promise<string[]
|
|
|
70
70
|
return told;
|
|
71
71
|
}
|
|
72
72
|
const { commit: start, tree } = snapshot(root);
|
|
73
|
+
// The turn starts before its reports are ready: a hook stopped for lack of time leaves it, never the turn before.
|
|
74
|
+
writeTurn(session, root, { start, baselines: {}, config, checked: false });
|
|
73
75
|
const cached = cachedReports(root, tree);
|
|
74
76
|
const baselines = await Promise.all(
|
|
75
77
|
compared.map(async (gate): Promise<{ gate: Gate; ok: boolean; output?: string }> => {
|
|
76
78
|
const cachedReport = cached(gate);
|
|
77
79
|
if (cachedReport !== undefined) return { gate, ok: true, output: cachedReport };
|
|
78
|
-
if (!keepsWatch(gate)) return { gate, ...(await wholeProject(root, gate)) };
|
|
80
|
+
if (!keepsWatch(gate)) return { gate, ...(await wholeProject(root, gate, TURN_START_MS)) };
|
|
79
81
|
const output = await watchReport(session, root, gate);
|
|
80
82
|
return { gate, ok: output !== undefined, output };
|
|
81
83
|
}),
|
package/src/core/run.ts
CHANGED
|
@@ -24,8 +24,20 @@ const ENGINE = resolve(import.meta.dirname, "..", "..");
|
|
|
24
24
|
const manifest = readJsonFile(join(ENGINE, "package.json"));
|
|
25
25
|
/** The version of this engine: what its helpers print, so what a report holds, may change from one to the next. */
|
|
26
26
|
export const ENGINE_VERSION = isRecord(manifest) ? String(manifest.version) : "";
|
|
27
|
-
/**
|
|
28
|
-
|
|
27
|
+
/**
|
|
28
|
+
* What a tool of a repo runs with: no colour, which some tools print anyway, and the commands of its node_modules
|
|
29
|
+
* first on the PATH, as npx and the package managers run them, so that a tool finds the Node the repo pins.
|
|
30
|
+
*/
|
|
31
|
+
export function toolEnv(root: string): NodeJS.ProcessEnv {
|
|
32
|
+
const path = [join(root, "node_modules", ".bin"), ...(process.env.PATH ? [process.env.PATH] : [])];
|
|
33
|
+
return { ...process.env, NO_COLOR: "1", FORCE_COLOR: "0", PATH: path.join(delimiter) };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The Node a script of the repo or of its packages runs on: the one the repo pins in its node_modules, as npx finds it, or the one of the engine. */
|
|
37
|
+
function repoNode(root: string): string {
|
|
38
|
+
const pinned = join(root, "node_modules", ".bin", "node");
|
|
39
|
+
return executable(pinned) ? pinned : process.execPath;
|
|
40
|
+
}
|
|
29
41
|
const MAX_OUTPUT_MB = 16;
|
|
30
42
|
const ENGINE_PREFIX = "{engine}/";
|
|
31
43
|
|
|
@@ -43,9 +55,9 @@ export interface Command {
|
|
|
43
55
|
}
|
|
44
56
|
|
|
45
57
|
// A script of the repo runs on the interpreter of its extension.
|
|
46
|
-
const INTERPRETERS: [RegExp, string][] = [
|
|
47
|
-
[/\.(ts|js|mjs)$/,
|
|
48
|
-
[/\.py$/, "python3"],
|
|
58
|
+
const INTERPRETERS: [RegExp, (root: string) => string][] = [
|
|
59
|
+
[/\.(ts|js|mjs)$/, repoNode],
|
|
60
|
+
[/\.py$/, () => "python3"],
|
|
49
61
|
];
|
|
50
62
|
|
|
51
63
|
/** The script of a package of the repo's node_modules, or nothing when the repo does not have the package. */
|
|
@@ -69,7 +81,7 @@ export function commandOf(root: string, name: string): Command {
|
|
|
69
81
|
if (name.startsWith(ENGINE_PREFIX)) {
|
|
70
82
|
return { file: process.execPath, args: [engineScript(name.slice(ENGINE_PREFIX.length))], missing: "" };
|
|
71
83
|
}
|
|
72
|
-
const interpreter = INTERPRETERS.find(([extension]) => extension.test(name))?.[1];
|
|
84
|
+
const interpreter = INTERPRETERS.find(([extension]) => extension.test(name))?.[1](root);
|
|
73
85
|
if (interpreter !== undefined) {
|
|
74
86
|
return {
|
|
75
87
|
file: interpreter,
|
|
@@ -78,7 +90,7 @@ export function commandOf(root: string, name: string): Command {
|
|
|
78
90
|
};
|
|
79
91
|
}
|
|
80
92
|
const script = packageScript(root, name);
|
|
81
|
-
if (script !== undefined) return { file:
|
|
93
|
+
if (script !== undefined) return { file: repoNode(root), args: [script], missing: "" };
|
|
82
94
|
if (name.includes("/")) return { file: resolve(root, name), args: [], missing: `${name} does not exist.` };
|
|
83
95
|
return {
|
|
84
96
|
file: name,
|
|
@@ -88,10 +100,8 @@ export function commandOf(root: string, name: string): Command {
|
|
|
88
100
|
}
|
|
89
101
|
|
|
90
102
|
// A program by its path, or by its name on the PATH, that the system would execute.
|
|
91
|
-
function executable(file: string): boolean {
|
|
92
|
-
const candidates = isAbsolute(file)
|
|
93
|
-
? [file]
|
|
94
|
-
: (process.env.PATH ?? "").split(delimiter).map((dir) => join(dir, file));
|
|
103
|
+
function executable(file: string, path = process.env.PATH ?? ""): boolean {
|
|
104
|
+
const candidates = isAbsolute(file) ? [file] : path.split(delimiter).map((dir) => join(dir, file));
|
|
95
105
|
return candidates.some((candidate) => {
|
|
96
106
|
try {
|
|
97
107
|
accessSync(candidate, constants.X_OK);
|
|
@@ -109,7 +119,7 @@ function missingProgram(root: string, name: string): string | undefined {
|
|
|
109
119
|
} catch {
|
|
110
120
|
return undefined;
|
|
111
121
|
}
|
|
112
|
-
return command.missing && !executable(command.file) ? command.missing : undefined;
|
|
122
|
+
return command.missing && !executable(command.file, toolEnv(root).PATH) ? command.missing : undefined;
|
|
113
123
|
}
|
|
114
124
|
|
|
115
125
|
/**
|
|
@@ -137,10 +147,17 @@ export function filesOf(gate: Gate, changed: string[]): string[] {
|
|
|
137
147
|
/**
|
|
138
148
|
* The lines of output a turn added, less the ignored ones. With a key, a line is told by the part the key matches. A
|
|
139
149
|
* line of a new key is still an old one, each line of the turn start whose key went standing for one line only: first
|
|
140
|
-
* when it says the same as such a line, that problem reported elsewhere
|
|
141
|
-
*
|
|
150
|
+
* when it says the same as such a line, that problem reported elsewhere, unless the group of the key named file names
|
|
151
|
+
* a file the turn changed, where the agent may have written it; then when the first group of the key, its place, is
|
|
152
|
+
* the one of such a line, that problem changed where it is.
|
|
142
153
|
*/
|
|
143
|
-
export function newLines(
|
|
154
|
+
export function newLines(
|
|
155
|
+
before: string,
|
|
156
|
+
after: string,
|
|
157
|
+
ignore?: string,
|
|
158
|
+
key?: string,
|
|
159
|
+
changed: string[] = [],
|
|
160
|
+
): string[] {
|
|
144
161
|
const ignored = ignore === undefined ? undefined : new RegExp(ignore);
|
|
145
162
|
const keyed = key === undefined ? undefined : new RegExp(key);
|
|
146
163
|
const lines = (text: string) =>
|
|
@@ -150,6 +167,7 @@ export function newLines(before: string, after: string, ignore?: string, key?: s
|
|
|
150
167
|
.filter(Boolean);
|
|
151
168
|
const keyOf = (line: string) => keyed?.exec(line)?.[0] ?? line;
|
|
152
169
|
const placeOf = (line: string) => keyed?.exec(line)?.[1];
|
|
170
|
+
const written = (line: string) => changed.includes(keyed?.exec(line)?.groups?.file ?? "");
|
|
153
171
|
const restOf = (line: string) => line.replace(keyOf(line), "").trim();
|
|
154
172
|
const old = lines(before);
|
|
155
173
|
const now = lines(after);
|
|
@@ -163,7 +181,7 @@ export function newLines(before: string, after: string, ignore?: string, key?: s
|
|
|
163
181
|
};
|
|
164
182
|
return now
|
|
165
183
|
.filter((line) => !oldKeys.has(keyOf(line)) && !ignored?.test(line))
|
|
166
|
-
.filter((line) => !restOf(line) || !stillOld((was) => restOf(was) === restOf(line)))
|
|
184
|
+
.filter((line) => !restOf(line) || written(line) || !stillOld((was) => restOf(was) === restOf(line)))
|
|
167
185
|
.filter((line) => placeOf(line) === undefined || !stillOld((was) => placeOf(was) === placeOf(line)));
|
|
168
186
|
}
|
|
169
187
|
|
|
@@ -192,7 +210,7 @@ async function execute(root: string, gate: Gate, args: string[]): Promise<Execut
|
|
|
192
210
|
cwd: root,
|
|
193
211
|
timeout: gate.timeoutSeconds * 1000,
|
|
194
212
|
maxBuffer: MAX_OUTPUT_MB * 1024 * 1024,
|
|
195
|
-
env:
|
|
213
|
+
env: toolEnv(root),
|
|
196
214
|
},
|
|
197
215
|
(error, stdout, stderr) => {
|
|
198
216
|
resolve({
|
|
@@ -228,9 +246,18 @@ export function cleaned(root: string, printed: string): string {
|
|
|
228
246
|
return stripVTControlCharacters(printed).split(`${root}${sep}`).join("").trim();
|
|
229
247
|
}
|
|
230
248
|
|
|
231
|
-
/**
|
|
232
|
-
|
|
233
|
-
|
|
249
|
+
/**
|
|
250
|
+
* Runs a compared gate on the whole project, as the turn starts, in the time the gate has or less: nothing when it
|
|
251
|
+
* runs out of it, since its lines would all be new at the stop.
|
|
252
|
+
*/
|
|
253
|
+
export async function wholeProject(
|
|
254
|
+
root: string,
|
|
255
|
+
gate: Gate,
|
|
256
|
+
ms = gate.timeoutSeconds * 1000,
|
|
257
|
+
): Promise<{ ok: boolean; output?: string }> {
|
|
258
|
+
const timeoutSeconds = Math.min(gate.timeoutSeconds, ms / 1000);
|
|
259
|
+
const { ok, output, timedOut } = await execute(root, { ...gate, timeoutSeconds }, gate.run.slice(1));
|
|
260
|
+
return timedOut ? { ok: false } : { ok, output };
|
|
234
261
|
}
|
|
235
262
|
|
|
236
263
|
/**
|
|
@@ -248,15 +275,20 @@ export async function check(
|
|
|
248
275
|
const ran = { name: gate.name, ms, report: ok ? output : undefined };
|
|
249
276
|
if (timedOut) return { ...ran, ok, output, timedOut };
|
|
250
277
|
if (!isCompared(gate) || !ok) return { ...ran, ok, output };
|
|
251
|
-
return { ...ran, ...addedLines(gate, output, baseline) };
|
|
278
|
+
return { ...ran, ...addedLines(gate, output, baseline, files) };
|
|
252
279
|
}
|
|
253
280
|
|
|
254
281
|
/**
|
|
255
282
|
* Judges the output of a compared gate that ran clean: it fails on the lines the turn added to the report of the turn
|
|
256
283
|
* start. Without that report, nothing counts but the exit code.
|
|
257
284
|
*/
|
|
258
|
-
export function addedLines(
|
|
285
|
+
export function addedLines(
|
|
286
|
+
gate: Gate,
|
|
287
|
+
output: string,
|
|
288
|
+
baseline: string | undefined,
|
|
289
|
+
changed: string[],
|
|
290
|
+
): { ok: boolean; output: string } {
|
|
259
291
|
if (baseline === undefined) return { ok: true, output: "" };
|
|
260
|
-
const added = newLines(baseline, output, gate.ignoreLines, gate.lineKey);
|
|
292
|
+
const added = newLines(baseline, output, gate.ignoreLines, gate.lineKey, changed);
|
|
261
293
|
return { ok: added.length === 0, output: added.join("\n") };
|
|
262
294
|
}
|
package/src/core/watch.ts
CHANGED
|
@@ -16,7 +16,7 @@ import { dirname, join } from "node:path";
|
|
|
16
16
|
import { createInterface } from "node:readline";
|
|
17
17
|
import { setTimeout as sleep } from "node:timers/promises";
|
|
18
18
|
import { type Gate, isCompared, type Watch, type WatchedGate } from "./config.ts";
|
|
19
|
-
import { addedLines, cleaned, commandOf, ENGINE_VERSION, type Outcome,
|
|
19
|
+
import { addedLines, cleaned, commandOf, ENGINE_VERSION, type Outcome, toolEnv } from "./run.ts";
|
|
20
20
|
|
|
21
21
|
// Once a cycle closes, the lines a watch prints after it, such as the errors of a failed build, come within this long.
|
|
22
22
|
const QUIET_MS = 1000;
|
|
@@ -26,8 +26,8 @@ const UNSEEN_MS = 5000;
|
|
|
26
26
|
const POLL_MS = 100;
|
|
27
27
|
// What a watch that stopped leaves the agent: enough to see the error, not the whole history of the session.
|
|
28
28
|
const LAST_LINES = 20;
|
|
29
|
-
// A prompt waits this long at most for
|
|
30
|
-
const TURN_START_MS = 45_000;
|
|
29
|
+
// A prompt waits this long at most for the reports of its compared gates, under the 60 s its hook has.
|
|
30
|
+
export const TURN_START_MS = 45_000;
|
|
31
31
|
|
|
32
32
|
const PREFIX = "gates-watch-";
|
|
33
33
|
// A start that holds its lock longer than this died on the way: the next hook takes the lock over.
|
|
@@ -132,7 +132,7 @@ function launch(folder: string, root: string, watch: Watch): void {
|
|
|
132
132
|
cwd: root,
|
|
133
133
|
detached: true,
|
|
134
134
|
stdio: ["ignore", log, log],
|
|
135
|
-
env:
|
|
135
|
+
env: toolEnv(root),
|
|
136
136
|
});
|
|
137
137
|
closeSync(log);
|
|
138
138
|
child.on("error", () => {});
|
|
@@ -240,7 +240,7 @@ export async function judgeWatch(
|
|
|
240
240
|
const { cycle } = settled;
|
|
241
241
|
const fails = new RegExp(gate.watch.failed ?? "(?!)");
|
|
242
242
|
const judged = isCompared(gate)
|
|
243
|
-
? addedLines(gate, cycle, baseline)
|
|
243
|
+
? addedLines(gate, cycle, baseline, files)
|
|
244
244
|
: { ok: !cycle.split("\n").some((line) => fails.test(line)), output: cycle };
|
|
245
245
|
return { name, ms, ...judged, report: judged.ok ? cycle : undefined };
|
|
246
246
|
}
|
|
@@ -5,26 +5,44 @@ import { commandOf } from "../core/run.ts";
|
|
|
5
5
|
import { COUNTS, HEADER } from "./vitest-state-reporter.ts";
|
|
6
6
|
|
|
7
7
|
// The watch of an Angular project tested with Vitest: ng test in watch mode, with the reporter of the engine that
|
|
8
|
-
// prints the state of every spec at each run, and NG_TEST_LOG, which makes Angular say when a run ends.
|
|
9
|
-
//
|
|
10
|
-
//
|
|
8
|
+
// prints the state of every spec at each run, and NG_TEST_LOG, which makes Angular say when a run ends. It prints
|
|
9
|
+
// only what judges a run: the build also prints the files it built, a table longer than the report of the specs in a
|
|
10
|
+
// large project, whose names change at each build.
|
|
11
11
|
|
|
12
12
|
const NO_RERUN = /No test specifications to rerun/;
|
|
13
|
+
const KEPT = [/Changes detected\. Rebuilding/, /bundle generation failed/, NO_RERUN];
|
|
14
|
+
const ENDS = /Test run finished with success/;
|
|
15
|
+
// Angular says why a run ended, and says all tests passed of a run with failed specs.
|
|
16
|
+
const REASON = /\s*Reason: .*$/;
|
|
17
|
+
const ERROR = /^\S+ \[ERROR\] /;
|
|
18
|
+
// What a watch that stopped leaves, so that a crash still says why.
|
|
19
|
+
const LAST_LINES = 20;
|
|
13
20
|
|
|
14
21
|
/**
|
|
15
|
-
* What to print for each line of
|
|
16
|
-
*
|
|
22
|
+
* What to print for each line of ng test: the lines that open a run, the state of the specs, the errors of a build
|
|
23
|
+
* that failed, with the lines under each, and the line that ends a run, less its reason. A change that leaves the
|
|
24
|
+
* build as it was reruns no spec, so no state prints: the last one is printed again, so that the cycle still tells
|
|
25
|
+
* which specs are red.
|
|
17
26
|
*/
|
|
18
|
-
export function
|
|
27
|
+
export function reporting(): (line: string) => string[] {
|
|
19
28
|
let state: string[] = [];
|
|
20
29
|
let reading: string[] | undefined;
|
|
30
|
+
let inError = false;
|
|
21
31
|
return (line) => {
|
|
22
32
|
if (line === HEADER) reading = [];
|
|
23
|
-
reading
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
33
|
+
if (reading !== undefined) {
|
|
34
|
+
reading.push(line);
|
|
35
|
+
if (line.startsWith(COUNTS)) {
|
|
36
|
+
state = reading;
|
|
37
|
+
reading = undefined;
|
|
38
|
+
}
|
|
39
|
+
return [line];
|
|
27
40
|
}
|
|
41
|
+
if (ERROR.test(line)) inError = true;
|
|
42
|
+
else if (line.trim() && !/^\s/.test(line)) inError = false;
|
|
43
|
+
if (inError) return [line];
|
|
44
|
+
if (ENDS.test(line)) return [line.replace(REASON, "")];
|
|
45
|
+
if (!KEPT.some((kept) => kept.test(line))) return [];
|
|
28
46
|
return NO_RERUN.test(line) ? [...state, line] : [line];
|
|
29
47
|
};
|
|
30
48
|
}
|
|
@@ -40,11 +58,19 @@ function main(root: string, args: string[]): void {
|
|
|
40
58
|
env: { ...process.env, NG_TEST_LOG: "1" },
|
|
41
59
|
stdio: ["ignore", "pipe", "pipe"],
|
|
42
60
|
});
|
|
43
|
-
const
|
|
44
|
-
const
|
|
61
|
+
const report = reporting();
|
|
62
|
+
const last: string[] = [];
|
|
63
|
+
const forward = (line: string) => {
|
|
64
|
+
last.push(line);
|
|
65
|
+
if (last.length > LAST_LINES) last.shift();
|
|
66
|
+
for (const shown of report(line)) console.log(shown);
|
|
67
|
+
};
|
|
45
68
|
createInterface({ input: watch.stdout }).on("line", forward);
|
|
46
69
|
createInterface({ input: watch.stderr }).on("line", forward);
|
|
47
|
-
watch.on("exit", (code) =>
|
|
70
|
+
watch.on("exit", (code) => {
|
|
71
|
+
console.log(["ng test stopped. Its last lines:", ...last].join("\n"));
|
|
72
|
+
process.exit(code ?? 1);
|
|
73
|
+
});
|
|
48
74
|
}
|
|
49
75
|
|
|
50
76
|
if (import.meta.main) main(process.cwd(), process.argv.slice(2));
|