@tayomi/deterministic-gates 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/.codex-plugin/plugin.json +1 -1
- package/catalog/index.html +6 -4
- package/catalog/typescript.json +5 -3
- package/dist/src/core/check.js +5 -2
- package/dist/src/core/config.js +17 -2
- package/dist/src/core/report-cache.js +7 -2
- package/dist/src/core/run.js +32 -5
- package/dist/src/core/watch.js +6 -3
- package/dist/src/helpers/tsc.js +57 -0
- package/dist/src/helpers/type-errors.js +45 -26
- package/package.json +1 -1
- package/src/core/check.ts +7 -2
- package/src/core/config.ts +28 -2
- package/src/core/report-cache.ts +12 -3
- package/src/core/run.ts +34 -7
- package/src/core/watch.ts +6 -3
- package/src/helpers/tsc.ts +60 -0
- package/src/helpers/type-errors.ts +48 -24
|
@@ -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.
|
|
4
|
+
"version": "0.6.0",
|
|
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
|
@@ -334,9 +334,9 @@ summary { cursor: pointer; color: var(--muted); }
|
|
|
334
334
|
</article>
|
|
335
335
|
<article id="typescript/typecheck">
|
|
336
336
|
<h3><code>typescript/typecheck</code></h3>
|
|
337
|
-
<p>Type-checks the project with tsc, since a change in one file can break the types of another. Incremental: tsc keeps what it learnt under node_modules/.cache and re-checks only what the changed files reach.</p>
|
|
337
|
+
<p>Type-checks the project with tsc, since a change in one file can break the types of another, and writes no JavaScript. Incremental: tsc keeps what it learnt under node_modules/.cache and re-checks only what the changed files reach. Refused when tsconfig.json only references others, as in an Angular project, since tsc would check no file: give it -p and a tsconfig.</p>
|
|
338
338
|
<dl>
|
|
339
|
-
<dt>Runs</dt><dd><code>
|
|
339
|
+
<dt>Runs</dt><dd><code>{engine}/src/helpers/tsc.ts --pretty false --noEmit --incremental --tsBuildInfoFile node_modules/.cache/gates/tsbuildinfo</code></dd>
|
|
340
340
|
<dt>When the turn changes</dt><dd><code>(\.(ts|tsx)|^tsconfig\.json)$</code></dd>
|
|
341
341
|
<dt>Requires</dt><dd><ul><li><code>typescript</code>: npm install --save-dev typescript</li></ul></dd>
|
|
342
342
|
</dl>
|
|
@@ -345,9 +345,10 @@ summary { cursor: pointer; color: var(--muted); }
|
|
|
345
345
|
"severity": "error",
|
|
346
346
|
"files": "(\\.(ts|tsx)|^tsconfig\\.json)$",
|
|
347
347
|
"run": [
|
|
348
|
-
"
|
|
348
|
+
"{engine}/src/helpers/tsc.ts",
|
|
349
349
|
"--pretty",
|
|
350
350
|
"false",
|
|
351
|
+
"--noEmit",
|
|
351
352
|
"--incremental",
|
|
352
353
|
"--tsBuildInfoFile",
|
|
353
354
|
"node_modules/.cache/gates/tsbuildinfo"
|
|
@@ -436,7 +437,7 @@ summary { cursor: pointer; color: var(--muted); }
|
|
|
436
437
|
</article>
|
|
437
438
|
<article id="typescript/no-new-strict-errors">
|
|
438
439
|
<h3><code>typescript/no-new-strict-errors</code></h3>
|
|
439
|
-
<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
|
|
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 with the same message, 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>
|
|
440
441
|
<dl>
|
|
441
442
|
<dt>Runs</dt><dd><code>{engine}/src/helpers/type-errors.ts --strictNullChecks --strictPropertyInitialization</code></dd>
|
|
442
443
|
<dt>When the turn changes</dt><dd><code>(\.(ts|tsx)|^tsconfig.*\.json)$</code></dd>
|
|
@@ -452,6 +453,7 @@ summary { cursor: pointer; color: var(--muted); }
|
|
|
452
453
|
"--strictPropertyInitialization"
|
|
453
454
|
],
|
|
454
455
|
"compare": "turn-start",
|
|
456
|
+
"lineKey": "^(.+?: error TS\\d+) at [^\\t]*",
|
|
455
457
|
"watch": {
|
|
456
458
|
"run": [
|
|
457
459
|
"{engine}/src/helpers/type-errors.ts",
|
package/catalog/typescript.json
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
}
|
|
19
19
|
},
|
|
20
20
|
{
|
|
21
|
-
"description": "Type-checks the project with tsc, since a change in one file can break the types of another. Incremental: tsc keeps what it learnt under node_modules/.cache and re-checks only what the changed files reach.",
|
|
21
|
+
"description": "Type-checks the project with tsc, since a change in one file can break the types of another, and writes no JavaScript. Incremental: tsc keeps what it learnt under node_modules/.cache and re-checks only what the changed files reach. Refused when tsconfig.json only references others, as in an Angular project, since tsc would check no file: give it -p and a tsconfig.",
|
|
22
22
|
"requires": [
|
|
23
23
|
{
|
|
24
24
|
"tool": "typescript",
|
|
@@ -30,9 +30,10 @@
|
|
|
30
30
|
"severity": "error",
|
|
31
31
|
"files": "(\\.(ts|tsx)|^tsconfig\\.json)$",
|
|
32
32
|
"run": [
|
|
33
|
-
"
|
|
33
|
+
"{engine}/src/helpers/tsc.ts",
|
|
34
34
|
"--pretty",
|
|
35
35
|
"false",
|
|
36
|
+
"--noEmit",
|
|
36
37
|
"--incremental",
|
|
37
38
|
"--tsBuildInfoFile",
|
|
38
39
|
"node_modules/.cache/gates/tsbuildinfo"
|
|
@@ -95,7 +96,7 @@
|
|
|
95
96
|
}
|
|
96
97
|
},
|
|
97
98
|
{
|
|
98
|
-
"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
|
|
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 with the same message, 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.",
|
|
99
100
|
"requires": [
|
|
100
101
|
{
|
|
101
102
|
"tool": "typescript",
|
|
@@ -108,6 +109,7 @@
|
|
|
108
109
|
"files": "(\\.(ts|tsx)|^tsconfig.*\\.json)$",
|
|
109
110
|
"run": ["{engine}/src/helpers/type-errors.ts", "--strictNullChecks", "--strictPropertyInitialization"],
|
|
110
111
|
"compare": "turn-start",
|
|
112
|
+
"lineKey": "^(.+?: error TS\\d+) at [^\\t]*",
|
|
111
113
|
"watch": {
|
|
112
114
|
"run": [
|
|
113
115
|
"{engine}/src/helpers/type-errors.ts",
|
package/dist/src/core/check.js
CHANGED
|
@@ -150,10 +150,13 @@ export async function checkTurn(session, root) {
|
|
|
150
150
|
const results = await runGates(root, runnable(gates, missing), changed, turn.start, baselines, session);
|
|
151
151
|
cacheReports(root, end, endReports(results, baselines));
|
|
152
152
|
writeTurn(session, root, { ...turn, checked: true });
|
|
153
|
-
// A compared gate with no report from the turn start can only count its exit code
|
|
153
|
+
// A compared gate with no report from the turn start can only count its exit code, and a watch has none: a green it
|
|
154
|
+
// did not earn is said.
|
|
154
155
|
const uncompared = results
|
|
155
156
|
.filter(({ gate, outcome }) => outcome && isCompared(gate) && baselines(gate) === undefined)
|
|
156
|
-
.map(({ gate }) =>
|
|
157
|
+
.map(({ gate }) => keepsWatch(gate)
|
|
158
|
+
? `${gate.name} not judged: no report from the turn start to compare its cycle with`
|
|
159
|
+
: `${gate.name} not compared: no report from the turn start, only its exit code counted`);
|
|
157
160
|
// Only a gate the turn concerned is named: one that would not have run anyway costs nothing to miss.
|
|
158
161
|
const notRun = missing
|
|
159
162
|
.filter(({ gate }) => filesOf(gate, changed).length)
|
package/dist/src/core/config.js
CHANGED
|
@@ -8,7 +8,17 @@ const LOCAL_GATES_FILE = ".gates.local.json";
|
|
|
8
8
|
const DEFAULT_MAX_TIMEOUT_SECONDS = 45;
|
|
9
9
|
// The times a turn sends the agent back: enough for a fix that breaks another gate, never a loop.
|
|
10
10
|
const DEFAULT_MAX_SEND_BACKS = 5;
|
|
11
|
-
const FIELDS = new Set([
|
|
11
|
+
const FIELDS = new Set([
|
|
12
|
+
"name",
|
|
13
|
+
"severity",
|
|
14
|
+
"files",
|
|
15
|
+
"run",
|
|
16
|
+
"compare",
|
|
17
|
+
"ignoreLines",
|
|
18
|
+
"lineKey",
|
|
19
|
+
"timeoutSeconds",
|
|
20
|
+
"watch",
|
|
21
|
+
]);
|
|
12
22
|
const WATCH_FIELDS = new Set(["run", "cycleStart", "cycleEnd", "failed"]);
|
|
13
23
|
/** In the arguments of a gate: the changed files it looks at, and the commit of the turn start. */
|
|
14
24
|
export const FILES = "{files}";
|
|
@@ -104,7 +114,7 @@ function toWatch(watch, compared) {
|
|
|
104
114
|
};
|
|
105
115
|
}
|
|
106
116
|
function toGate(entry, maxTimeoutSeconds) {
|
|
107
|
-
const { name, severity, files, run, compare, ignoreLines, watch, timeoutSeconds = maxTimeoutSeconds } = entry;
|
|
117
|
+
const { name, severity, files, run, compare, ignoreLines, lineKey, watch, timeoutSeconds = maxTimeoutSeconds, } = entry;
|
|
108
118
|
const unknown = Object.keys(entry).find((key) => !FIELDS.has(key));
|
|
109
119
|
if (unknown !== undefined)
|
|
110
120
|
refuse(`unknown field ${unknown}`);
|
|
@@ -122,6 +132,10 @@ function toGate(entry, maxTimeoutSeconds) {
|
|
|
122
132
|
refuse(`a compared gate reads the whole project: it takes no ${FILES}`);
|
|
123
133
|
if (ignoreLines !== undefined && !isPattern(ignoreLines))
|
|
124
134
|
refuse("ignoreLines is a regular expression");
|
|
135
|
+
if (lineKey !== undefined && !isPattern(lineKey))
|
|
136
|
+
refuse("lineKey is a regular expression");
|
|
137
|
+
if (lineKey !== undefined && compare === undefined)
|
|
138
|
+
refuse("lineKey tells apart the lines of a compared gate: it takes compare");
|
|
125
139
|
if (typeof timeoutSeconds !== "number" || timeoutSeconds <= 0)
|
|
126
140
|
refuse("timeoutSeconds is a positive number");
|
|
127
141
|
return {
|
|
@@ -133,6 +147,7 @@ function toGate(entry, maxTimeoutSeconds) {
|
|
|
133
147
|
timeoutSeconds: Math.min(timeoutSeconds, maxTimeoutSeconds),
|
|
134
148
|
...(compare === undefined ? {} : { compare }),
|
|
135
149
|
...(ignoreLines === undefined ? {} : { ignoreLines }),
|
|
150
|
+
...(lineKey === undefined ? {} : { lineKey }),
|
|
136
151
|
...(watch === undefined ? {} : { watch: toWatch(watch, compare !== undefined) }),
|
|
137
152
|
};
|
|
138
153
|
}
|
|
@@ -3,8 +3,12 @@ import { writeFileSync } from "node:fs";
|
|
|
3
3
|
import { tmpdir } from "node:os";
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import { isRecord, isStringRecord, readJsonFile } from "./json.js";
|
|
6
|
+
import { ENGINE_VERSION } from "./run.js";
|
|
6
7
|
function isCache(value) {
|
|
7
|
-
return isRecord(value) &&
|
|
8
|
+
return (isRecord(value) &&
|
|
9
|
+
typeof value.engine === "string" &&
|
|
10
|
+
typeof value.tree === "string" &&
|
|
11
|
+
isStringRecord(value.reports));
|
|
8
12
|
}
|
|
9
13
|
function cacheFile(root) {
|
|
10
14
|
const repo = createHash("sha256").update(root).digest("hex").slice(0, 16);
|
|
@@ -15,7 +19,7 @@ function commandOf(gate) {
|
|
|
15
19
|
}
|
|
16
20
|
function readCache(root) {
|
|
17
21
|
const cache = readJsonFile(cacheFile(root));
|
|
18
|
-
return isCache(cache) ? cache : undefined;
|
|
22
|
+
return isCache(cache) && cache.engine === ENGINE_VERSION ? cache : undefined;
|
|
19
23
|
}
|
|
20
24
|
export function cachedReports(root, tree) {
|
|
21
25
|
const cache = readCache(root);
|
|
@@ -25,6 +29,7 @@ export function cachedReports(root, tree) {
|
|
|
25
29
|
export function cacheReports(root, tree, reports) {
|
|
26
30
|
const kept = readCache(root);
|
|
27
31
|
const cache = {
|
|
32
|
+
engine: ENGINE_VERSION,
|
|
28
33
|
tree,
|
|
29
34
|
reports: {
|
|
30
35
|
...(kept?.tree === tree ? kept.reports : {}),
|
package/dist/src/core/run.js
CHANGED
|
@@ -9,6 +9,9 @@ export function failed(outcome) {
|
|
|
9
9
|
return !outcome.ok && !outcome.timedOut;
|
|
10
10
|
}
|
|
11
11
|
const ENGINE = resolve(import.meta.dirname, "..", "..");
|
|
12
|
+
const manifest = readJsonFile(join(ENGINE, "package.json"));
|
|
13
|
+
/** The version of this engine: what its helpers print, so what a report holds, may change from one to the next. */
|
|
14
|
+
export const ENGINE_VERSION = isRecord(manifest) ? String(manifest.version) : "";
|
|
12
15
|
/** What a tool runs with: no colour, which some tools print anyway. */
|
|
13
16
|
export const TOOL_ENV = { ...process.env, NO_COLOR: "1", FORCE_COLOR: "0" };
|
|
14
17
|
const MAX_OUTPUT_MB = 16;
|
|
@@ -107,13 +110,37 @@ export function filesOf(gate, changed) {
|
|
|
107
110
|
const pattern = new RegExp(gate.files);
|
|
108
111
|
return changed.filter((file) => pattern.test(file));
|
|
109
112
|
}
|
|
110
|
-
|
|
113
|
+
/**
|
|
114
|
+
* 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
|
+
* 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; then when the first group of the key, its
|
|
117
|
+
* place, is the one of such a line, that problem changed where it is.
|
|
118
|
+
*/
|
|
119
|
+
export function newLines(before, after, ignore, key) {
|
|
111
120
|
const ignored = ignore === undefined ? undefined : new RegExp(ignore);
|
|
112
|
-
const
|
|
113
|
-
|
|
121
|
+
const keyed = key === undefined ? undefined : new RegExp(key);
|
|
122
|
+
const lines = (text) => text
|
|
114
123
|
.split("\n")
|
|
115
124
|
.map((line) => line.trim())
|
|
116
|
-
.filter(
|
|
125
|
+
.filter(Boolean);
|
|
126
|
+
const keyOf = (line) => keyed?.exec(line)?.[0] ?? line;
|
|
127
|
+
const placeOf = (line) => keyed?.exec(line)?.[1];
|
|
128
|
+
const restOf = (line) => line.replace(keyOf(line), "").trim();
|
|
129
|
+
const old = lines(before);
|
|
130
|
+
const now = lines(after);
|
|
131
|
+
const oldKeys = new Set(old.map(keyOf));
|
|
132
|
+
const nowKeys = new Set(now.map(keyOf));
|
|
133
|
+
const gone = old.filter((line) => !nowKeys.has(keyOf(line)));
|
|
134
|
+
const stillOld = (same) => {
|
|
135
|
+
const at = gone.findIndex(same);
|
|
136
|
+
if (at !== -1)
|
|
137
|
+
gone.splice(at, 1);
|
|
138
|
+
return at !== -1;
|
|
139
|
+
};
|
|
140
|
+
return now
|
|
141
|
+
.filter((line) => !oldKeys.has(keyOf(line)) && !ignored?.test(line))
|
|
142
|
+
.filter((line) => !restOf(line) || !stillOld((was) => restOf(was) === restOf(line)))
|
|
143
|
+
.filter((line) => placeOf(line) === undefined || !stillOld((was) => placeOf(was) === placeOf(line)));
|
|
117
144
|
}
|
|
118
145
|
async function execute(root, gate, args) {
|
|
119
146
|
const started = performance.now();
|
|
@@ -182,6 +209,6 @@ export async function check(root, gate, files, turn, baseline) {
|
|
|
182
209
|
export function addedLines(gate, output, baseline) {
|
|
183
210
|
if (baseline === undefined)
|
|
184
211
|
return { ok: true, output: "" };
|
|
185
|
-
const added = newLines(baseline, output, gate.ignoreLines);
|
|
212
|
+
const added = newLines(baseline, output, gate.ignoreLines, gate.lineKey);
|
|
186
213
|
return { ok: added.length === 0, output: added.join("\n") };
|
|
187
214
|
}
|
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, TOOL_ENV } from "./run.js";
|
|
9
|
+
import { addedLines, cleaned, commandOf, ENGINE_VERSION, TOOL_ENV } 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:
|
|
@@ -36,9 +36,12 @@ export function keepWatch(root, gate) {
|
|
|
36
36
|
if (existsSync(folder))
|
|
37
37
|
touch(folder);
|
|
38
38
|
}
|
|
39
|
-
/**
|
|
39
|
+
/**
|
|
40
|
+
* Where a watch keeps its process, its output and the sessions that use it, outside the repo: one per repo, gate and
|
|
41
|
+
* engine version, since a helper of the engine may print its cycles otherwise from one version to the next.
|
|
42
|
+
*/
|
|
40
43
|
function folderOf(root, gate) {
|
|
41
|
-
const key = createHash("sha256").update(`${root}\0${gate.name}`).digest("hex").slice(0, 16);
|
|
44
|
+
const key = createHash("sha256").update(`${root}\0${gate.name}\0${ENGINE_VERSION}`).digest("hex").slice(0, 16);
|
|
42
45
|
return join(tmpdir(), `${PREFIX}${key}`);
|
|
43
46
|
}
|
|
44
47
|
// A file per session that uses the watch: two hooks at once each write their own, and neither loses the other's.
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { statSync } from "node:fs";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import { dirname, join, relative, resolve } from "node:path";
|
|
5
|
+
import { commandOf } from "../core/run.js";
|
|
6
|
+
// tsc, with the options it is given. A tsconfig that only references others, as the tsconfig.json of an Angular
|
|
7
|
+
// project, checks no file, and tsc says nothing of it: the gate would be green without checking anything, so it is
|
|
8
|
+
// refused, with the tsconfigs it references.
|
|
9
|
+
/** The tsconfig tsc reads: the one of -p or --project, a folder meaning its tsconfig.json, or the one of the root. */
|
|
10
|
+
function configOf(root, args) {
|
|
11
|
+
const at = args.findIndex((arg) => arg === "-p" || arg === "--project");
|
|
12
|
+
const given = at === -1 ? undefined : args[at + 1];
|
|
13
|
+
if (given === undefined)
|
|
14
|
+
return join(root, "tsconfig.json");
|
|
15
|
+
const path = resolve(root, given);
|
|
16
|
+
return statSync(path, { throwIfNoEntry: false })?.isDirectory() ? join(path, "tsconfig.json") : path;
|
|
17
|
+
}
|
|
18
|
+
function listed(names) {
|
|
19
|
+
return names.length > 1 ? `${names.slice(0, -1).join(", ")} and ${names.at(-1)}` : (names[0] ?? "");
|
|
20
|
+
}
|
|
21
|
+
/** Why tsc, with these options, would check no file, or nothing when it checks some, or will say itself what is wrong. */
|
|
22
|
+
export function nothingChecked(root, args) {
|
|
23
|
+
let ts;
|
|
24
|
+
try {
|
|
25
|
+
ts = createRequire(join(root, "package.json"))("typescript");
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
const file = configOf(root, args);
|
|
31
|
+
const read = ts.readConfigFile(file, (path) => ts.sys.readFile(path));
|
|
32
|
+
if (read.error !== undefined)
|
|
33
|
+
return undefined;
|
|
34
|
+
const parsed = ts.parseJsonConfigFileContent(read.config, ts.sys, dirname(file), undefined, file);
|
|
35
|
+
const references = parsed.projectReferences ?? [];
|
|
36
|
+
if (parsed.fileNames.length || !references.length)
|
|
37
|
+
return undefined;
|
|
38
|
+
const names = references.map((reference) => relative(root, ts.resolveProjectReferencePath(reference)));
|
|
39
|
+
return `${relative(root, file)} checks no file: it only references ${listed(names)}. Add -p and the one to check to the gate's run.`;
|
|
40
|
+
}
|
|
41
|
+
/** The tsc of the repo, once it is known to check files with these options: otherwise, it says why not and exits. */
|
|
42
|
+
export function checkingTsc(root, args) {
|
|
43
|
+
const tsc = commandOf(root, "typescript");
|
|
44
|
+
const refused = tsc.missing || nothingChecked(root, args);
|
|
45
|
+
if (refused) {
|
|
46
|
+
console.log(refused);
|
|
47
|
+
process.exit(1);
|
|
48
|
+
}
|
|
49
|
+
return tsc;
|
|
50
|
+
}
|
|
51
|
+
function main(root, args) {
|
|
52
|
+
const tsc = checkingTsc(root, args);
|
|
53
|
+
const child = spawn(tsc.file, [...tsc.args, ...args], { stdio: "inherit" });
|
|
54
|
+
child.on("close", (code) => process.exit(code ?? 1));
|
|
55
|
+
}
|
|
56
|
+
if (import.meta.main)
|
|
57
|
+
main(process.cwd(), process.argv.slice(2));
|
|
@@ -1,34 +1,49 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { resolve } from "node:path";
|
|
2
4
|
import { createInterface } from "node:readline";
|
|
3
|
-
import {
|
|
5
|
+
import { checkingTsc } from "./tsc.js";
|
|
4
6
|
// tsc, with the options it is given, such as strict ones a project turned off, printing its errors so that a gate can
|
|
5
|
-
// compare them with the ones of the turn start
|
|
6
|
-
//
|
|
7
|
+
// compare them with the ones of the turn start. tsc prints the same error otherwise from one compilation to the next:
|
|
8
|
+
// a watch checks again only the files a change reaches, with types created in another order, so the members of a
|
|
9
|
+
// union, the properties of an object, and the file of an error tsc reports once per type, all change. Each error is
|
|
10
|
+
// then one line: a key the gate compares, its file, code and source line, so an error a change moves down is not new,
|
|
11
|
+
// with a count after one its file already has on the same source, so one more of it is; then, after a tab, the first
|
|
12
|
+
// line of its message, for the agent alone.
|
|
7
13
|
// With --watch, tsc keeps watching, and each compilation prints between a line that starts it and one that ends it.
|
|
8
14
|
const STARTS = /Starting (compilation in watch mode|incremental compilation)/;
|
|
9
15
|
const ENDS = /Found \d+ errors?\. Watching for file changes/;
|
|
10
|
-
const POSITION = /^(.+)\(\d
|
|
16
|
+
const POSITION = /^(.+)\((\d+),\d+\): error (TS\d+): (.*)$/;
|
|
17
|
+
const MESSAGE_CHARS = 300;
|
|
18
|
+
const SOURCE_CHARS = 200;
|
|
19
|
+
function cut(text, chars) {
|
|
20
|
+
return text.length > chars ? `${text.slice(0, chars)}…` : text;
|
|
21
|
+
}
|
|
11
22
|
/** The errors tsc printed, each on one line, as the gate compares them. */
|
|
12
|
-
export function typeErrors(printed) {
|
|
23
|
+
export function typeErrors(printed, sources) {
|
|
24
|
+
const lines = new Map();
|
|
25
|
+
const sourceLine = (file, line) => {
|
|
26
|
+
if (!lines.has(file))
|
|
27
|
+
lines.set(file, (sources(file) ?? "").split("\n"));
|
|
28
|
+
return (lines.get(file)?.[line - 1] ?? "").replaceAll("\t", " ").trim();
|
|
29
|
+
};
|
|
30
|
+
const seen = new Map();
|
|
13
31
|
const errors = [];
|
|
14
32
|
for (const line of printed) {
|
|
15
|
-
|
|
33
|
+
// The lines under an error explain it, and name its first failing member.
|
|
34
|
+
if (!line.trim() || /^\s/.test(line))
|
|
16
35
|
continue;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
36
|
+
const [, file, at, code, message = ""] = POSITION.exec(line) ?? [];
|
|
37
|
+
const found = file === undefined ? line : `${file}: error ${code} at \`${cut(sourceLine(file, Number(at)), SOURCE_CHARS)}\``;
|
|
38
|
+
const count = (seen.get(found) ?? 0) + 1;
|
|
39
|
+
seen.set(found, count);
|
|
40
|
+
const key = count === 1 ? found : `${found} (${count})`;
|
|
41
|
+
errors.push(file === undefined ? key : `${key}\t${cut(message, MESSAGE_CHARS)}`);
|
|
22
42
|
}
|
|
23
|
-
|
|
24
|
-
return errors.map((error) => {
|
|
25
|
-
const count = (seen.get(error) ?? 0) + 1;
|
|
26
|
-
seen.set(error, count);
|
|
27
|
-
return count === 1 ? error : `${error} (${count})`;
|
|
28
|
-
});
|
|
43
|
+
return errors;
|
|
29
44
|
}
|
|
30
45
|
/** What to print for each line of tsc in watch mode: nothing until a compilation ends, then its errors. */
|
|
31
|
-
export function watching() {
|
|
46
|
+
export function watching(sources) {
|
|
32
47
|
let compiled = [];
|
|
33
48
|
return (line) => {
|
|
34
49
|
if (STARTS.test(line)) {
|
|
@@ -36,22 +51,26 @@ export function watching() {
|
|
|
36
51
|
return ["Type check started"];
|
|
37
52
|
}
|
|
38
53
|
if (ENDS.test(line))
|
|
39
|
-
return [...typeErrors(compiled), "Type check done"];
|
|
54
|
+
return [...typeErrors(compiled, sources), "Type check done"];
|
|
40
55
|
compiled.push(line);
|
|
41
56
|
return [];
|
|
42
57
|
};
|
|
43
58
|
}
|
|
44
59
|
function main(root, args) {
|
|
45
|
-
const tsc =
|
|
46
|
-
if (tsc.missing) {
|
|
47
|
-
console.log(tsc.missing);
|
|
48
|
-
process.exit(1);
|
|
49
|
-
}
|
|
60
|
+
const tsc = checkingTsc(root, args);
|
|
50
61
|
const watch = args.includes("--watch");
|
|
51
62
|
const options = ["--noEmit", "--pretty", "false", ...(watch ? ["--preserveWatchOutput"] : []), ...args];
|
|
52
63
|
const child = spawn(tsc.file, [...tsc.args, ...options], { stdio: ["ignore", "pipe", "inherit"] });
|
|
53
64
|
const printed = [];
|
|
54
|
-
const
|
|
65
|
+
const sources = (file) => {
|
|
66
|
+
try {
|
|
67
|
+
return readFileSync(resolve(root, file), "utf8");
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
const print = watching(sources);
|
|
55
74
|
createInterface({ input: child.stdout }).on("line", (line) => {
|
|
56
75
|
if (watch)
|
|
57
76
|
for (const shown of print(line))
|
|
@@ -65,7 +84,7 @@ function main(root, args) {
|
|
|
65
84
|
console.log(printed.join("\n"));
|
|
66
85
|
process.exit(code ?? 1);
|
|
67
86
|
}
|
|
68
|
-
console.log(typeErrors(printed).join("\n"));
|
|
87
|
+
console.log(typeErrors(printed, sources).join("\n"));
|
|
69
88
|
});
|
|
70
89
|
}
|
|
71
90
|
if (import.meta.main)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tayomi/deterministic-gates",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
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
|
@@ -200,10 +200,15 @@ export async function checkTurn(session: string, root: string): Promise<CheckedT
|
|
|
200
200
|
const results = await runGates(root, runnable(gates, missing), changed, turn.start, baselines, session);
|
|
201
201
|
cacheReports(root, end, endReports(results, baselines));
|
|
202
202
|
writeTurn(session, root, { ...turn, checked: true });
|
|
203
|
-
// A compared gate with no report from the turn start can only count its exit code
|
|
203
|
+
// A compared gate with no report from the turn start can only count its exit code, and a watch has none: a green it
|
|
204
|
+
// did not earn is said.
|
|
204
205
|
const uncompared = results
|
|
205
206
|
.filter(({ gate, outcome }) => outcome && isCompared(gate) && baselines(gate) === undefined)
|
|
206
|
-
.map(({ gate }) =>
|
|
207
|
+
.map(({ gate }) =>
|
|
208
|
+
keepsWatch(gate)
|
|
209
|
+
? `${gate.name} not judged: no report from the turn start to compare its cycle with`
|
|
210
|
+
: `${gate.name} not compared: no report from the turn start, only its exit code counted`,
|
|
211
|
+
);
|
|
207
212
|
// Only a gate the turn concerned is named: one that would not have run anyway costs nothing to miss.
|
|
208
213
|
const notRun = missing
|
|
209
214
|
.filter(({ gate }) => filesOf(gate, changed).length)
|
package/src/core/config.ts
CHANGED
|
@@ -10,7 +10,17 @@ const LOCAL_GATES_FILE = ".gates.local.json";
|
|
|
10
10
|
const DEFAULT_MAX_TIMEOUT_SECONDS = 45;
|
|
11
11
|
// The times a turn sends the agent back: enough for a fix that breaks another gate, never a loop.
|
|
12
12
|
const DEFAULT_MAX_SEND_BACKS = 5;
|
|
13
|
-
const FIELDS = new Set([
|
|
13
|
+
const FIELDS = new Set([
|
|
14
|
+
"name",
|
|
15
|
+
"severity",
|
|
16
|
+
"files",
|
|
17
|
+
"run",
|
|
18
|
+
"compare",
|
|
19
|
+
"ignoreLines",
|
|
20
|
+
"lineKey",
|
|
21
|
+
"timeoutSeconds",
|
|
22
|
+
"watch",
|
|
23
|
+
]);
|
|
14
24
|
const WATCH_FIELDS = new Set(["run", "cycleStart", "cycleEnd", "failed"]);
|
|
15
25
|
|
|
16
26
|
/** In the arguments of a gate: the changed files it looks at, and the commit of the turn start. */
|
|
@@ -24,6 +34,8 @@ export interface Gate {
|
|
|
24
34
|
run: string[];
|
|
25
35
|
compare?: "turn-start";
|
|
26
36
|
ignoreLines?: string;
|
|
37
|
+
/** The part of a line of a compared gate that tells it apart, when the rest may print otherwise from run to run. */
|
|
38
|
+
lineKey?: string;
|
|
27
39
|
timeoutSeconds: number;
|
|
28
40
|
watch?: Watch;
|
|
29
41
|
}
|
|
@@ -154,7 +166,17 @@ function toWatch(watch: unknown, compared: boolean): Watch {
|
|
|
154
166
|
}
|
|
155
167
|
|
|
156
168
|
function toGate(entry: Entry, maxTimeoutSeconds: number): Gate {
|
|
157
|
-
const {
|
|
169
|
+
const {
|
|
170
|
+
name,
|
|
171
|
+
severity,
|
|
172
|
+
files,
|
|
173
|
+
run,
|
|
174
|
+
compare,
|
|
175
|
+
ignoreLines,
|
|
176
|
+
lineKey,
|
|
177
|
+
watch,
|
|
178
|
+
timeoutSeconds = maxTimeoutSeconds,
|
|
179
|
+
} = entry;
|
|
158
180
|
const unknown = Object.keys(entry).find((key) => !FIELDS.has(key));
|
|
159
181
|
if (unknown !== undefined) refuse(`unknown field ${unknown}`);
|
|
160
182
|
if (!/^[a-z][a-z0-9-]*$/.test(name)) refuse("a name is lowercase words joined by hyphens");
|
|
@@ -166,6 +188,9 @@ function toGate(entry: Entry, maxTimeoutSeconds: number): Gate {
|
|
|
166
188
|
if (compare !== undefined && run.includes(FILES))
|
|
167
189
|
refuse(`a compared gate reads the whole project: it takes no ${FILES}`);
|
|
168
190
|
if (ignoreLines !== undefined && !isPattern(ignoreLines)) refuse("ignoreLines is a regular expression");
|
|
191
|
+
if (lineKey !== undefined && !isPattern(lineKey)) refuse("lineKey is a regular expression");
|
|
192
|
+
if (lineKey !== undefined && compare === undefined)
|
|
193
|
+
refuse("lineKey tells apart the lines of a compared gate: it takes compare");
|
|
169
194
|
if (typeof timeoutSeconds !== "number" || timeoutSeconds <= 0) refuse("timeoutSeconds is a positive number");
|
|
170
195
|
return {
|
|
171
196
|
name,
|
|
@@ -176,6 +201,7 @@ function toGate(entry: Entry, maxTimeoutSeconds: number): Gate {
|
|
|
176
201
|
timeoutSeconds: Math.min(timeoutSeconds, maxTimeoutSeconds),
|
|
177
202
|
...(compare === undefined ? {} : { compare }),
|
|
178
203
|
...(ignoreLines === undefined ? {} : { ignoreLines }),
|
|
204
|
+
...(lineKey === undefined ? {} : { lineKey }),
|
|
179
205
|
...(watch === undefined ? {} : { watch: toWatch(watch, compare !== undefined) }),
|
|
180
206
|
};
|
|
181
207
|
}
|
package/src/core/report-cache.ts
CHANGED
|
@@ -4,18 +4,26 @@ import { tmpdir } from "node:os";
|
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import type { Gate } from "./config.ts";
|
|
6
6
|
import { isRecord, isStringRecord, readJsonFile } from "./json.ts";
|
|
7
|
+
import { ENGINE_VERSION } from "./run.ts";
|
|
7
8
|
|
|
8
9
|
// A turn almost always starts from the tree the previous turn ended on, so it reuses the reports of that tree
|
|
9
10
|
// instead of running the compared gates again while the prompt waits. A report is the output of one command on one
|
|
10
|
-
// tree, so the reports of a tree are kept by command, whatever the gate is called.
|
|
11
|
+
// tree, so the reports of a tree are kept by command, whatever the gate is called. A helper of the engine may print
|
|
12
|
+
// its report otherwise from one version to the next: the reports of another version are not reused.
|
|
11
13
|
|
|
12
14
|
interface Cache {
|
|
15
|
+
engine: string;
|
|
13
16
|
tree: string;
|
|
14
17
|
reports: Record<string, string>;
|
|
15
18
|
}
|
|
16
19
|
|
|
17
20
|
function isCache(value: unknown): value is Cache {
|
|
18
|
-
return
|
|
21
|
+
return (
|
|
22
|
+
isRecord(value) &&
|
|
23
|
+
typeof value.engine === "string" &&
|
|
24
|
+
typeof value.tree === "string" &&
|
|
25
|
+
isStringRecord(value.reports)
|
|
26
|
+
);
|
|
19
27
|
}
|
|
20
28
|
|
|
21
29
|
function cacheFile(root: string): string {
|
|
@@ -29,7 +37,7 @@ function commandOf(gate: Gate): string {
|
|
|
29
37
|
|
|
30
38
|
function readCache(root: string): Cache | undefined {
|
|
31
39
|
const cache = readJsonFile(cacheFile(root));
|
|
32
|
-
return isCache(cache) ? cache : undefined;
|
|
40
|
+
return isCache(cache) && cache.engine === ENGINE_VERSION ? cache : undefined;
|
|
33
41
|
}
|
|
34
42
|
|
|
35
43
|
export function cachedReports(root: string, tree: string): (gate: Gate) => string | undefined {
|
|
@@ -41,6 +49,7 @@ export function cachedReports(root: string, tree: string): (gate: Gate) => strin
|
|
|
41
49
|
export function cacheReports(root: string, tree: string, reports: (readonly [Gate, string])[]): void {
|
|
42
50
|
const kept = readCache(root);
|
|
43
51
|
const cache: Cache = {
|
|
52
|
+
engine: ENGINE_VERSION,
|
|
44
53
|
tree,
|
|
45
54
|
reports: {
|
|
46
55
|
...(kept?.tree === tree ? kept.reports : {}),
|
package/src/core/run.ts
CHANGED
|
@@ -21,6 +21,9 @@ export function failed(outcome: Outcome): boolean {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
const ENGINE = resolve(import.meta.dirname, "..", "..");
|
|
24
|
+
const manifest = readJsonFile(join(ENGINE, "package.json"));
|
|
25
|
+
/** The version of this engine: what its helpers print, so what a report holds, may change from one to the next. */
|
|
26
|
+
export const ENGINE_VERSION = isRecord(manifest) ? String(manifest.version) : "";
|
|
24
27
|
/** What a tool runs with: no colour, which some tools print anyway. */
|
|
25
28
|
export const TOOL_ENV = { ...process.env, NO_COLOR: "1", FORCE_COLOR: "0" };
|
|
26
29
|
const MAX_OUTPUT_MB = 16;
|
|
@@ -131,13 +134,37 @@ export function filesOf(gate: Gate, changed: string[]): string[] {
|
|
|
131
134
|
return changed.filter((file) => pattern.test(file));
|
|
132
135
|
}
|
|
133
136
|
|
|
134
|
-
|
|
137
|
+
/**
|
|
138
|
+
* 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
|
+
* 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; then when the first group of the key, its
|
|
141
|
+
* place, is the one of such a line, that problem changed where it is.
|
|
142
|
+
*/
|
|
143
|
+
export function newLines(before: string, after: string, ignore?: string, key?: string): string[] {
|
|
135
144
|
const ignored = ignore === undefined ? undefined : new RegExp(ignore);
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
145
|
+
const keyed = key === undefined ? undefined : new RegExp(key);
|
|
146
|
+
const lines = (text: string) =>
|
|
147
|
+
text
|
|
148
|
+
.split("\n")
|
|
149
|
+
.map((line) => line.trim())
|
|
150
|
+
.filter(Boolean);
|
|
151
|
+
const keyOf = (line: string) => keyed?.exec(line)?.[0] ?? line;
|
|
152
|
+
const placeOf = (line: string) => keyed?.exec(line)?.[1];
|
|
153
|
+
const restOf = (line: string) => line.replace(keyOf(line), "").trim();
|
|
154
|
+
const old = lines(before);
|
|
155
|
+
const now = lines(after);
|
|
156
|
+
const oldKeys = new Set(old.map(keyOf));
|
|
157
|
+
const nowKeys = new Set(now.map(keyOf));
|
|
158
|
+
const gone = old.filter((line) => !nowKeys.has(keyOf(line)));
|
|
159
|
+
const stillOld = (same: (was: string) => boolean) => {
|
|
160
|
+
const at = gone.findIndex(same);
|
|
161
|
+
if (at !== -1) gone.splice(at, 1);
|
|
162
|
+
return at !== -1;
|
|
163
|
+
};
|
|
164
|
+
return now
|
|
165
|
+
.filter((line) => !oldKeys.has(keyOf(line)) && !ignored?.test(line))
|
|
166
|
+
.filter((line) => !restOf(line) || !stillOld((was) => restOf(was) === restOf(line)))
|
|
167
|
+
.filter((line) => placeOf(line) === undefined || !stillOld((was) => placeOf(was) === placeOf(line)));
|
|
141
168
|
}
|
|
142
169
|
|
|
143
170
|
interface Executed {
|
|
@@ -230,6 +257,6 @@ export async function check(
|
|
|
230
257
|
*/
|
|
231
258
|
export function addedLines(gate: Gate, output: string, baseline: string | undefined): { ok: boolean; output: string } {
|
|
232
259
|
if (baseline === undefined) return { ok: true, output: "" };
|
|
233
|
-
const added = newLines(baseline, output, gate.ignoreLines);
|
|
260
|
+
const added = newLines(baseline, output, gate.ignoreLines, gate.lineKey);
|
|
234
261
|
return { ok: added.length === 0, output: added.join("\n") };
|
|
235
262
|
}
|
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, type Outcome, TOOL_ENV } from "./run.ts";
|
|
19
|
+
import { addedLines, cleaned, commandOf, ENGINE_VERSION, type Outcome, TOOL_ENV } 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;
|
|
@@ -51,9 +51,12 @@ export function keepWatch(root: string, gate: Gate): void {
|
|
|
51
51
|
if (existsSync(folder)) touch(folder);
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
/**
|
|
54
|
+
/**
|
|
55
|
+
* Where a watch keeps its process, its output and the sessions that use it, outside the repo: one per repo, gate and
|
|
56
|
+
* engine version, since a helper of the engine may print its cycles otherwise from one version to the next.
|
|
57
|
+
*/
|
|
55
58
|
function folderOf(root: string, gate: Gate): string {
|
|
56
|
-
const key = createHash("sha256").update(`${root}\0${gate.name}`).digest("hex").slice(0, 16);
|
|
59
|
+
const key = createHash("sha256").update(`${root}\0${gate.name}\0${ENGINE_VERSION}`).digest("hex").slice(0, 16);
|
|
57
60
|
return join(tmpdir(), `${PREFIX}${key}`);
|
|
58
61
|
}
|
|
59
62
|
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { statSync } from "node:fs";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import { dirname, join, relative, resolve } from "node:path";
|
|
5
|
+
import type * as Ts from "typescript";
|
|
6
|
+
import { type Command, commandOf } from "../core/run.ts";
|
|
7
|
+
|
|
8
|
+
// tsc, with the options it is given. A tsconfig that only references others, as the tsconfig.json of an Angular
|
|
9
|
+
// project, checks no file, and tsc says nothing of it: the gate would be green without checking anything, so it is
|
|
10
|
+
// refused, with the tsconfigs it references.
|
|
11
|
+
|
|
12
|
+
/** The tsconfig tsc reads: the one of -p or --project, a folder meaning its tsconfig.json, or the one of the root. */
|
|
13
|
+
function configOf(root: string, args: string[]): string {
|
|
14
|
+
const at = args.findIndex((arg) => arg === "-p" || arg === "--project");
|
|
15
|
+
const given = at === -1 ? undefined : args[at + 1];
|
|
16
|
+
if (given === undefined) return join(root, "tsconfig.json");
|
|
17
|
+
const path = resolve(root, given);
|
|
18
|
+
return statSync(path, { throwIfNoEntry: false })?.isDirectory() ? join(path, "tsconfig.json") : path;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function listed(names: string[]): string {
|
|
22
|
+
return names.length > 1 ? `${names.slice(0, -1).join(", ")} and ${names.at(-1)}` : (names[0] ?? "");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Why tsc, with these options, would check no file, or nothing when it checks some, or will say itself what is wrong. */
|
|
26
|
+
export function nothingChecked(root: string, args: string[]): string | undefined {
|
|
27
|
+
let ts: typeof Ts;
|
|
28
|
+
try {
|
|
29
|
+
ts = createRequire(join(root, "package.json"))("typescript") as typeof Ts;
|
|
30
|
+
} catch {
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
const file = configOf(root, args);
|
|
34
|
+
const read = ts.readConfigFile(file, (path) => ts.sys.readFile(path));
|
|
35
|
+
if (read.error !== undefined) return undefined;
|
|
36
|
+
const parsed = ts.parseJsonConfigFileContent(read.config, ts.sys, dirname(file), undefined, file);
|
|
37
|
+
const references = parsed.projectReferences ?? [];
|
|
38
|
+
if (parsed.fileNames.length || !references.length) return undefined;
|
|
39
|
+
const names = references.map((reference) => relative(root, ts.resolveProjectReferencePath(reference)));
|
|
40
|
+
return `${relative(root, file)} checks no file: it only references ${listed(names)}. Add -p and the one to check to the gate's run.`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The tsc of the repo, once it is known to check files with these options: otherwise, it says why not and exits. */
|
|
44
|
+
export function checkingTsc(root: string, args: string[]): Command {
|
|
45
|
+
const tsc = commandOf(root, "typescript");
|
|
46
|
+
const refused = tsc.missing || nothingChecked(root, args);
|
|
47
|
+
if (refused) {
|
|
48
|
+
console.log(refused);
|
|
49
|
+
process.exit(1);
|
|
50
|
+
}
|
|
51
|
+
return tsc;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function main(root: string, args: string[]): void {
|
|
55
|
+
const tsc = checkingTsc(root, args);
|
|
56
|
+
const child = spawn(tsc.file, [...tsc.args, ...args], { stdio: "inherit" });
|
|
57
|
+
child.on("close", (code) => process.exit(code ?? 1));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (import.meta.main) main(process.cwd(), process.argv.slice(2));
|
|
@@ -1,58 +1,82 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { resolve } from "node:path";
|
|
2
4
|
import { createInterface } from "node:readline";
|
|
3
|
-
import {
|
|
5
|
+
import { checkingTsc } from "./tsc.ts";
|
|
4
6
|
|
|
5
7
|
// tsc, with the options it is given, such as strict ones a project turned off, printing its errors so that a gate can
|
|
6
|
-
// compare them with the ones of the turn start
|
|
7
|
-
//
|
|
8
|
+
// compare them with the ones of the turn start. tsc prints the same error otherwise from one compilation to the next:
|
|
9
|
+
// a watch checks again only the files a change reaches, with types created in another order, so the members of a
|
|
10
|
+
// union, the properties of an object, and the file of an error tsc reports once per type, all change. Each error is
|
|
11
|
+
// then one line: a key the gate compares, its file, code and source line, so an error a change moves down is not new,
|
|
12
|
+
// with a count after one its file already has on the same source, so one more of it is; then, after a tab, the first
|
|
13
|
+
// line of its message, for the agent alone.
|
|
8
14
|
// With --watch, tsc keeps watching, and each compilation prints between a line that starts it and one that ends it.
|
|
9
15
|
|
|
10
16
|
const STARTS = /Starting (compilation in watch mode|incremental compilation)/;
|
|
11
17
|
const ENDS = /Found \d+ errors?\. Watching for file changes/;
|
|
12
|
-
const POSITION = /^(.+)\(\d
|
|
18
|
+
const POSITION = /^(.+)\((\d+),\d+\): error (TS\d+): (.*)$/;
|
|
19
|
+
const MESSAGE_CHARS = 300;
|
|
20
|
+
const SOURCE_CHARS = 200;
|
|
21
|
+
|
|
22
|
+
/** The text of a file of the project, or nothing when it cannot be read. */
|
|
23
|
+
export type Sources = (file: string) => string | undefined;
|
|
24
|
+
|
|
25
|
+
function cut(text: string, chars: number): string {
|
|
26
|
+
return text.length > chars ? `${text.slice(0, chars)}…` : text;
|
|
27
|
+
}
|
|
13
28
|
|
|
14
29
|
/** The errors tsc printed, each on one line, as the gate compares them. */
|
|
15
|
-
export function typeErrors(printed: string[]): string[] {
|
|
30
|
+
export function typeErrors(printed: string[], sources: Sources): string[] {
|
|
31
|
+
const lines = new Map<string, string[]>();
|
|
32
|
+
const sourceLine = (file: string, line: number) => {
|
|
33
|
+
if (!lines.has(file)) lines.set(file, (sources(file) ?? "").split("\n"));
|
|
34
|
+
return (lines.get(file)?.[line - 1] ?? "").replaceAll("\t", " ").trim();
|
|
35
|
+
};
|
|
36
|
+
const seen = new Map<string, number>();
|
|
16
37
|
const errors: string[] = [];
|
|
17
38
|
for (const line of printed) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
39
|
+
// The lines under an error explain it, and name its first failing member.
|
|
40
|
+
if (!line.trim() || /^\s/.test(line)) continue;
|
|
41
|
+
const [, file, at, code, message = ""] = POSITION.exec(line) ?? [];
|
|
42
|
+
const found =
|
|
43
|
+
file === undefined ? line : `${file}: error ${code} at \`${cut(sourceLine(file, Number(at)), SOURCE_CHARS)}\``;
|
|
44
|
+
const count = (seen.get(found) ?? 0) + 1;
|
|
45
|
+
seen.set(found, count);
|
|
46
|
+
const key = count === 1 ? found : `${found} (${count})`;
|
|
47
|
+
errors.push(file === undefined ? key : `${key}\t${cut(message, MESSAGE_CHARS)}`);
|
|
22
48
|
}
|
|
23
|
-
|
|
24
|
-
return errors.map((error) => {
|
|
25
|
-
const count = (seen.get(error) ?? 0) + 1;
|
|
26
|
-
seen.set(error, count);
|
|
27
|
-
return count === 1 ? error : `${error} (${count})`;
|
|
28
|
-
});
|
|
49
|
+
return errors;
|
|
29
50
|
}
|
|
30
51
|
|
|
31
52
|
/** What to print for each line of tsc in watch mode: nothing until a compilation ends, then its errors. */
|
|
32
|
-
export function watching(): (line: string) => string[] {
|
|
53
|
+
export function watching(sources: Sources): (line: string) => string[] {
|
|
33
54
|
let compiled: string[] = [];
|
|
34
55
|
return (line) => {
|
|
35
56
|
if (STARTS.test(line)) {
|
|
36
57
|
compiled = [];
|
|
37
58
|
return ["Type check started"];
|
|
38
59
|
}
|
|
39
|
-
if (ENDS.test(line)) return [...typeErrors(compiled), "Type check done"];
|
|
60
|
+
if (ENDS.test(line)) return [...typeErrors(compiled, sources), "Type check done"];
|
|
40
61
|
compiled.push(line);
|
|
41
62
|
return [];
|
|
42
63
|
};
|
|
43
64
|
}
|
|
44
65
|
|
|
45
66
|
function main(root: string, args: string[]): void {
|
|
46
|
-
const tsc =
|
|
47
|
-
if (tsc.missing) {
|
|
48
|
-
console.log(tsc.missing);
|
|
49
|
-
process.exit(1);
|
|
50
|
-
}
|
|
67
|
+
const tsc = checkingTsc(root, args);
|
|
51
68
|
const watch = args.includes("--watch");
|
|
52
69
|
const options = ["--noEmit", "--pretty", "false", ...(watch ? ["--preserveWatchOutput"] : []), ...args];
|
|
53
70
|
const child = spawn(tsc.file, [...tsc.args, ...options], { stdio: ["ignore", "pipe", "inherit"] });
|
|
54
71
|
const printed: string[] = [];
|
|
55
|
-
const
|
|
72
|
+
const sources: Sources = (file) => {
|
|
73
|
+
try {
|
|
74
|
+
return readFileSync(resolve(root, file), "utf8");
|
|
75
|
+
} catch {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
const print = watching(sources);
|
|
56
80
|
createInterface({ input: child.stdout }).on("line", (line) => {
|
|
57
81
|
if (watch) for (const shown of print(line)) console.log(shown);
|
|
58
82
|
else printed.push(line);
|
|
@@ -63,7 +87,7 @@ function main(root: string, args: string[]): void {
|
|
|
63
87
|
console.log(printed.join("\n"));
|
|
64
88
|
process.exit(code ?? 1);
|
|
65
89
|
}
|
|
66
|
-
console.log(typeErrors(printed).join("\n"));
|
|
90
|
+
console.log(typeErrors(printed, sources).join("\n"));
|
|
67
91
|
});
|
|
68
92
|
}
|
|
69
93
|
|