@tayomi/deterministic-gates 0.5.0 → 0.5.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 +5 -4
- package/catalog/typescript.json +4 -3
- package/dist/src/core/check.js +5 -2
- package/dist/src/core/report-cache.js +7 -2
- package/dist/src/core/run.js +3 -0
- package/dist/src/core/watch.js +6 -3
- package/dist/src/helpers/tsc.js +57 -0
- package/dist/src/helpers/type-errors.js +85 -13
- package/package.json +1 -1
- package/src/core/check.ts +7 -2
- package/src/core/report-cache.ts +12 -3
- package/src/core/run.ts +3 -0
- package/src/core/watch.ts +6 -3
- package/src/helpers/tsc.ts +60 -0
- package/src/helpers/type-errors.ts +83 -11
|
@@ -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.5.
|
|
4
|
+
"version": "0.5.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
|
@@ -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 message, so one a change moves to another line is not new. tsc keeps watching between turns; without the watch, it runs at each prompt and stop instead.
|
|
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 message, so one a change moves to another line is not new. 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>
|
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 message, so one a change moves to another line is not new. tsc keeps watching between turns; without the watch, it runs at each prompt and stop instead.
|
|
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 message, so one a change moves to another line is not new. 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",
|
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)
|
|
@@ -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;
|
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,24 +1,93 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
2
|
import { createInterface } from "node:readline";
|
|
3
|
-
import {
|
|
3
|
+
import { checkingTsc } from "./tsc.js";
|
|
4
4
|
// tsc, with the options it is given, such as strict ones a project turned off, printing its errors so that a gate can
|
|
5
5
|
// compare them with the ones of the turn start: one line per error, with its file and none of its line and column, so
|
|
6
6
|
// an error a change moves down is not new, and a count after an error its file already has, so one more of it is.
|
|
7
|
+
// tsc orders the members of a union as it happened to create them, which differs between the first compilation of a
|
|
8
|
+
// watch and the next ones: each union prints sorted, untruncated, and without the lines under the error, which name its
|
|
9
|
+
// first failing member, so the same error always prints the same.
|
|
7
10
|
// With --watch, tsc keeps watching, and each compilation prints between a line that starts it and one that ends it.
|
|
8
11
|
const STARTS = /Starting (compilation in watch mode|incremental compilation)/;
|
|
9
12
|
const ENDS = /Found \d+ errors?\. Watching for file changes/;
|
|
10
13
|
const POSITION = /^(.+)\(\d+,\d+\): (error TS\d+: .*)$/;
|
|
14
|
+
const OPENS = { "(": ")", "[": "]", "{": "}", "<": ">" };
|
|
15
|
+
// A property, parameter or index signature before its type, in an object, a tuple or a parameter list.
|
|
16
|
+
const LABEL = /^((?:\.\.\.)?(?:[\w$]+|"[^"]*"|\[[^\]]*\])\??: )(.*)$/s;
|
|
17
|
+
/** Where the bracket or the string that starts at index ends, or the index itself for any other character. */
|
|
18
|
+
function skipped(text, index) {
|
|
19
|
+
const char = text[index] ?? "";
|
|
20
|
+
if (char === '"' || char === "`") {
|
|
21
|
+
let end = index + 1;
|
|
22
|
+
while (end < text.length && text[end] !== char)
|
|
23
|
+
end += text[end] === "\\" ? 2 : 1;
|
|
24
|
+
return end;
|
|
25
|
+
}
|
|
26
|
+
const close = OPENS[char];
|
|
27
|
+
if (close === undefined)
|
|
28
|
+
return index;
|
|
29
|
+
let end = index + 1;
|
|
30
|
+
// The arrow of a function type closes no bracket.
|
|
31
|
+
while (end < text.length && text[end] !== close)
|
|
32
|
+
end = text.startsWith("=>", end) ? end + 2 : skipped(text, end) + 1;
|
|
33
|
+
return end;
|
|
34
|
+
}
|
|
35
|
+
/** The parts of a text between a separator that no bracket nor string encloses. */
|
|
36
|
+
function split(text, separator) {
|
|
37
|
+
const parts = [];
|
|
38
|
+
let start = 0;
|
|
39
|
+
for (let index = 0; index < text.length; index = skipped(text, index) + 1) {
|
|
40
|
+
if (text.startsWith("=>", index))
|
|
41
|
+
index++;
|
|
42
|
+
else if (text.startsWith(separator, index)) {
|
|
43
|
+
parts.push(text.slice(start, index));
|
|
44
|
+
start = index + separator.length;
|
|
45
|
+
index += separator.length - 1;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return [...parts, text.slice(start)];
|
|
49
|
+
}
|
|
50
|
+
/** A type as tsc prints it, with the members of each union and intersection sorted, at every depth. */
|
|
51
|
+
function sortedType(type) {
|
|
52
|
+
// A function type outside any bracket returns all the rest: a union of function types puts each in parentheses.
|
|
53
|
+
const [parameters = "", ...returned] = split(type, " => ");
|
|
54
|
+
if (returned.length)
|
|
55
|
+
return `${sortedInside(parameters)} => ${sortedType(returned.join(" => "))}`;
|
|
56
|
+
const sorted = (members) => (members.length > 1 ? members.sort() : members);
|
|
57
|
+
const union = split(type, " | ").map((member) => sorted(split(member, " & ").map(sortedInside)).join(" & "));
|
|
58
|
+
return sorted(union).join(" | ");
|
|
59
|
+
}
|
|
60
|
+
/** A type with the content of each of its brackets sorted. */
|
|
61
|
+
function sortedInside(type) {
|
|
62
|
+
let out = "";
|
|
63
|
+
for (let index = 0; index < type.length;) {
|
|
64
|
+
const end = skipped(type, index);
|
|
65
|
+
const char = type[index] ?? "";
|
|
66
|
+
if (OPENS[char] !== undefined) {
|
|
67
|
+
const separator = char === "{" ? ";" : ",";
|
|
68
|
+
const inner = split(type.slice(index + 1, end), separator).map((element) => {
|
|
69
|
+
const [, before = "", trimmed = "", after = ""] = /^(\s*)(.*?)(\s*)$/s.exec(element) ?? [];
|
|
70
|
+
const label = LABEL.exec(trimmed);
|
|
71
|
+
return before + (label ? `${label[1]}${sortedType(label[2] ?? "")}` : sortedType(trimmed)) + after;
|
|
72
|
+
});
|
|
73
|
+
out += `${char}${inner.join(separator)}${type[end] ?? ""}`;
|
|
74
|
+
}
|
|
75
|
+
else
|
|
76
|
+
out += type.slice(index, end + 1);
|
|
77
|
+
index = end + 1;
|
|
78
|
+
}
|
|
79
|
+
return out;
|
|
80
|
+
}
|
|
11
81
|
/** The errors tsc printed, each on one line, as the gate compares them. */
|
|
12
82
|
export function typeErrors(printed) {
|
|
13
83
|
const errors = [];
|
|
14
84
|
for (const line of printed) {
|
|
15
|
-
|
|
85
|
+
// The lines under an error explain it, and name its first failing member.
|
|
86
|
+
if (!line.trim() || /^\s/.test(line))
|
|
16
87
|
continue;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
else
|
|
21
|
-
errors.push(line.replace(POSITION, "$1: $2"));
|
|
88
|
+
const located = line.replace(POSITION, "$1: $2");
|
|
89
|
+
// tsc quotes each type it names.
|
|
90
|
+
errors.push(located.replace(/'([^']*)'/g, (_quoted, type) => `'${sortedType(type)}'`));
|
|
22
91
|
}
|
|
23
92
|
const seen = new Map();
|
|
24
93
|
return errors.map((error) => {
|
|
@@ -42,13 +111,16 @@ export function watching() {
|
|
|
42
111
|
};
|
|
43
112
|
}
|
|
44
113
|
function main(root, args) {
|
|
45
|
-
const tsc =
|
|
46
|
-
if (tsc.missing) {
|
|
47
|
-
console.log(tsc.missing);
|
|
48
|
-
process.exit(1);
|
|
49
|
-
}
|
|
114
|
+
const tsc = checkingTsc(root, args);
|
|
50
115
|
const watch = args.includes("--watch");
|
|
51
|
-
const options = [
|
|
116
|
+
const options = [
|
|
117
|
+
"--noEmit",
|
|
118
|
+
"--pretty",
|
|
119
|
+
"false",
|
|
120
|
+
"--noErrorTruncation",
|
|
121
|
+
...(watch ? ["--preserveWatchOutput"] : []),
|
|
122
|
+
...args,
|
|
123
|
+
];
|
|
52
124
|
const child = spawn(tsc.file, [...tsc.args, ...options], { stdio: ["ignore", "pipe", "inherit"] });
|
|
53
125
|
const printed = [];
|
|
54
126
|
const print = watching();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tayomi/deterministic-gates",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.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
|
@@ -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/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;
|
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,24 +1,93 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
2
|
import { createInterface } from "node:readline";
|
|
3
|
-
import {
|
|
3
|
+
import { checkingTsc } from "./tsc.ts";
|
|
4
4
|
|
|
5
5
|
// tsc, with the options it is given, such as strict ones a project turned off, printing its errors so that a gate can
|
|
6
6
|
// compare them with the ones of the turn start: one line per error, with its file and none of its line and column, so
|
|
7
7
|
// an error a change moves down is not new, and a count after an error its file already has, so one more of it is.
|
|
8
|
+
// tsc orders the members of a union as it happened to create them, which differs between the first compilation of a
|
|
9
|
+
// watch and the next ones: each union prints sorted, untruncated, and without the lines under the error, which name its
|
|
10
|
+
// first failing member, so the same error always prints the same.
|
|
8
11
|
// With --watch, tsc keeps watching, and each compilation prints between a line that starts it and one that ends it.
|
|
9
12
|
|
|
10
13
|
const STARTS = /Starting (compilation in watch mode|incremental compilation)/;
|
|
11
14
|
const ENDS = /Found \d+ errors?\. Watching for file changes/;
|
|
12
15
|
const POSITION = /^(.+)\(\d+,\d+\): (error TS\d+: .*)$/;
|
|
13
16
|
|
|
17
|
+
const OPENS: Record<string, string> = { "(": ")", "[": "]", "{": "}", "<": ">" };
|
|
18
|
+
// A property, parameter or index signature before its type, in an object, a tuple or a parameter list.
|
|
19
|
+
const LABEL = /^((?:\.\.\.)?(?:[\w$]+|"[^"]*"|\[[^\]]*\])\??: )(.*)$/s;
|
|
20
|
+
|
|
21
|
+
/** Where the bracket or the string that starts at index ends, or the index itself for any other character. */
|
|
22
|
+
function skipped(text: string, index: number): number {
|
|
23
|
+
const char = text[index] ?? "";
|
|
24
|
+
if (char === '"' || char === "`") {
|
|
25
|
+
let end = index + 1;
|
|
26
|
+
while (end < text.length && text[end] !== char) end += text[end] === "\\" ? 2 : 1;
|
|
27
|
+
return end;
|
|
28
|
+
}
|
|
29
|
+
const close = OPENS[char];
|
|
30
|
+
if (close === undefined) return index;
|
|
31
|
+
let end = index + 1;
|
|
32
|
+
// The arrow of a function type closes no bracket.
|
|
33
|
+
while (end < text.length && text[end] !== close) end = text.startsWith("=>", end) ? end + 2 : skipped(text, end) + 1;
|
|
34
|
+
return end;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The parts of a text between a separator that no bracket nor string encloses. */
|
|
38
|
+
function split(text: string, separator: string): string[] {
|
|
39
|
+
const parts: string[] = [];
|
|
40
|
+
let start = 0;
|
|
41
|
+
for (let index = 0; index < text.length; index = skipped(text, index) + 1) {
|
|
42
|
+
if (text.startsWith("=>", index)) index++;
|
|
43
|
+
else if (text.startsWith(separator, index)) {
|
|
44
|
+
parts.push(text.slice(start, index));
|
|
45
|
+
start = index + separator.length;
|
|
46
|
+
index += separator.length - 1;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return [...parts, text.slice(start)];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** A type as tsc prints it, with the members of each union and intersection sorted, at every depth. */
|
|
53
|
+
function sortedType(type: string): string {
|
|
54
|
+
// A function type outside any bracket returns all the rest: a union of function types puts each in parentheses.
|
|
55
|
+
const [parameters = "", ...returned] = split(type, " => ");
|
|
56
|
+
if (returned.length) return `${sortedInside(parameters)} => ${sortedType(returned.join(" => "))}`;
|
|
57
|
+
const sorted = (members: string[]) => (members.length > 1 ? members.sort() : members);
|
|
58
|
+
const union = split(type, " | ").map((member) => sorted(split(member, " & ").map(sortedInside)).join(" & "));
|
|
59
|
+
return sorted(union).join(" | ");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** A type with the content of each of its brackets sorted. */
|
|
63
|
+
function sortedInside(type: string): string {
|
|
64
|
+
let out = "";
|
|
65
|
+
for (let index = 0; index < type.length;) {
|
|
66
|
+
const end = skipped(type, index);
|
|
67
|
+
const char = type[index] ?? "";
|
|
68
|
+
if (OPENS[char] !== undefined) {
|
|
69
|
+
const separator = char === "{" ? ";" : ",";
|
|
70
|
+
const inner = split(type.slice(index + 1, end), separator).map((element) => {
|
|
71
|
+
const [, before = "", trimmed = "", after = ""] = /^(\s*)(.*?)(\s*)$/s.exec(element) ?? [];
|
|
72
|
+
const label = LABEL.exec(trimmed);
|
|
73
|
+
return before + (label ? `${label[1]}${sortedType(label[2] ?? "")}` : sortedType(trimmed)) + after;
|
|
74
|
+
});
|
|
75
|
+
out += `${char}${inner.join(separator)}${type[end] ?? ""}`;
|
|
76
|
+
} else out += type.slice(index, end + 1);
|
|
77
|
+
index = end + 1;
|
|
78
|
+
}
|
|
79
|
+
return out;
|
|
80
|
+
}
|
|
81
|
+
|
|
14
82
|
/** The errors tsc printed, each on one line, as the gate compares them. */
|
|
15
83
|
export function typeErrors(printed: string[]): string[] {
|
|
16
84
|
const errors: string[] = [];
|
|
17
85
|
for (const line of printed) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
86
|
+
// The lines under an error explain it, and name its first failing member.
|
|
87
|
+
if (!line.trim() || /^\s/.test(line)) continue;
|
|
88
|
+
const located = line.replace(POSITION, "$1: $2");
|
|
89
|
+
// tsc quotes each type it names.
|
|
90
|
+
errors.push(located.replace(/'([^']*)'/g, (_quoted, type: string) => `'${sortedType(type)}'`));
|
|
22
91
|
}
|
|
23
92
|
const seen = new Map<string, number>();
|
|
24
93
|
return errors.map((error) => {
|
|
@@ -43,13 +112,16 @@ export function watching(): (line: string) => string[] {
|
|
|
43
112
|
}
|
|
44
113
|
|
|
45
114
|
function main(root: string, args: string[]): void {
|
|
46
|
-
const tsc =
|
|
47
|
-
if (tsc.missing) {
|
|
48
|
-
console.log(tsc.missing);
|
|
49
|
-
process.exit(1);
|
|
50
|
-
}
|
|
115
|
+
const tsc = checkingTsc(root, args);
|
|
51
116
|
const watch = args.includes("--watch");
|
|
52
|
-
const options = [
|
|
117
|
+
const options = [
|
|
118
|
+
"--noEmit",
|
|
119
|
+
"--pretty",
|
|
120
|
+
"false",
|
|
121
|
+
"--noErrorTruncation",
|
|
122
|
+
...(watch ? ["--preserveWatchOutput"] : []),
|
|
123
|
+
...args,
|
|
124
|
+
];
|
|
53
125
|
const child = spawn(tsc.file, [...tsc.args, ...options], { stdio: ["ignore", "pipe", "inherit"] });
|
|
54
126
|
const printed: string[] = [];
|
|
55
127
|
const print = watching();
|