@tayomi/deterministic-gates 0.4.2 → 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/README.md +1 -1
- package/catalog/angular.json +85 -0
- package/catalog/index.html +119 -4
- package/catalog/typescript.json +29 -2
- package/dist/src/cli.js +3 -3
- package/dist/src/core/check.js +35 -12
- package/dist/src/core/config.js +28 -2
- package/dist/src/core/report-cache.js +7 -2
- package/dist/src/core/run.js +29 -11
- package/dist/src/core/watch.js +283 -0
- package/dist/src/helpers/angular-watch.js +47 -0
- package/dist/src/helpers/tsc.js +57 -0
- package/dist/src/helpers/type-errors.js +144 -0
- package/dist/src/helpers/vitest-state-reporter.js +26 -0
- package/dist/src/hook/hook.js +25 -1
- package/hooks/hooks.json +11 -0
- package/package.json +1 -1
- package/src/cli.ts +3 -3
- package/src/core/check.ts +36 -12
- package/src/core/config.ts +43 -2
- package/src/core/report-cache.ts +12 -3
- package/src/core/run.ts +32 -11
- package/src/core/watch.ts +313 -0
- package/src/helpers/angular-watch.ts +50 -0
- package/src/helpers/tsc.ts +60 -0
- package/src/helpers/type-errors.ts +142 -0
- package/src/helpers/vitest-state-reporter.ts +48 -0
- package/src/hook/hook.ts +27 -2
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { extname, join } from "node:path";
|
|
3
|
+
import { createInterface } from "node:readline";
|
|
4
|
+
import { commandOf } from "../core/run.ts";
|
|
5
|
+
import { COUNTS, HEADER } from "./vitest-state-reporter.ts";
|
|
6
|
+
|
|
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. A change that
|
|
9
|
+
// leaves the build as it was reruns no spec, so no state prints: the last one is printed again, so that the cycle
|
|
10
|
+
// still tells which specs are red.
|
|
11
|
+
|
|
12
|
+
const NO_RERUN = /No test specifications to rerun/;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* What to print for each line of the watch: the line, and before a run that reruns no spec, the last state of the
|
|
16
|
+
* specs, so that the cycle still tells which specs are red.
|
|
17
|
+
*/
|
|
18
|
+
export function replaying(): (line: string) => string[] {
|
|
19
|
+
let state: string[] = [];
|
|
20
|
+
let reading: string[] | undefined;
|
|
21
|
+
return (line) => {
|
|
22
|
+
if (line === HEADER) reading = [];
|
|
23
|
+
reading?.push(line);
|
|
24
|
+
if (reading !== undefined && line.startsWith(COUNTS)) {
|
|
25
|
+
state = reading;
|
|
26
|
+
reading = undefined;
|
|
27
|
+
}
|
|
28
|
+
return NO_RERUN.test(line) ? [...state, line] : [line];
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function main(root: string, args: string[]): void {
|
|
33
|
+
const ng = commandOf(root, "@angular/cli");
|
|
34
|
+
if (ng.missing) {
|
|
35
|
+
console.log(ng.missing);
|
|
36
|
+
process.exit(1);
|
|
37
|
+
}
|
|
38
|
+
const reporter = join(import.meta.dirname, `vitest-state-reporter${extname(import.meta.filename)}`);
|
|
39
|
+
const watch = spawn(ng.file, [...ng.args, "test", "--watch", `--reporters=${reporter}`, ...args], {
|
|
40
|
+
env: { ...process.env, NG_TEST_LOG: "1" },
|
|
41
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
42
|
+
});
|
|
43
|
+
const replay = replaying();
|
|
44
|
+
const forward = (line: string) => console.log(replay(line).join("\n"));
|
|
45
|
+
createInterface({ input: watch.stdout }).on("line", forward);
|
|
46
|
+
createInterface({ input: watch.stderr }).on("line", forward);
|
|
47
|
+
watch.on("exit", (code) => process.exit(code ?? 1));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (import.meta.main) main(process.cwd(), process.argv.slice(2));
|
|
@@ -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));
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { createInterface } from "node:readline";
|
|
3
|
+
import { checkingTsc } from "./tsc.ts";
|
|
4
|
+
|
|
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
|
+
// 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
|
+
// 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.
|
|
11
|
+
// With --watch, tsc keeps watching, and each compilation prints between a line that starts it and one that ends it.
|
|
12
|
+
|
|
13
|
+
const STARTS = /Starting (compilation in watch mode|incremental compilation)/;
|
|
14
|
+
const ENDS = /Found \d+ errors?\. Watching for file changes/;
|
|
15
|
+
const POSITION = /^(.+)\(\d+,\d+\): (error TS\d+: .*)$/;
|
|
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
|
+
|
|
82
|
+
/** The errors tsc printed, each on one line, as the gate compares them. */
|
|
83
|
+
export function typeErrors(printed: string[]): string[] {
|
|
84
|
+
const errors: string[] = [];
|
|
85
|
+
for (const line of printed) {
|
|
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)}'`));
|
|
91
|
+
}
|
|
92
|
+
const seen = new Map<string, number>();
|
|
93
|
+
return errors.map((error) => {
|
|
94
|
+
const count = (seen.get(error) ?? 0) + 1;
|
|
95
|
+
seen.set(error, count);
|
|
96
|
+
return count === 1 ? error : `${error} (${count})`;
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** What to print for each line of tsc in watch mode: nothing until a compilation ends, then its errors. */
|
|
101
|
+
export function watching(): (line: string) => string[] {
|
|
102
|
+
let compiled: string[] = [];
|
|
103
|
+
return (line) => {
|
|
104
|
+
if (STARTS.test(line)) {
|
|
105
|
+
compiled = [];
|
|
106
|
+
return ["Type check started"];
|
|
107
|
+
}
|
|
108
|
+
if (ENDS.test(line)) return [...typeErrors(compiled), "Type check done"];
|
|
109
|
+
compiled.push(line);
|
|
110
|
+
return [];
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function main(root: string, args: string[]): void {
|
|
115
|
+
const tsc = checkingTsc(root, args);
|
|
116
|
+
const watch = args.includes("--watch");
|
|
117
|
+
const options = [
|
|
118
|
+
"--noEmit",
|
|
119
|
+
"--pretty",
|
|
120
|
+
"false",
|
|
121
|
+
"--noErrorTruncation",
|
|
122
|
+
...(watch ? ["--preserveWatchOutput"] : []),
|
|
123
|
+
...args,
|
|
124
|
+
];
|
|
125
|
+
const child = spawn(tsc.file, [...tsc.args, ...options], { stdio: ["ignore", "pipe", "inherit"] });
|
|
126
|
+
const printed: string[] = [];
|
|
127
|
+
const print = watching();
|
|
128
|
+
createInterface({ input: child.stdout }).on("line", (line) => {
|
|
129
|
+
if (watch) for (const shown of print(line)) console.log(shown);
|
|
130
|
+
else printed.push(line);
|
|
131
|
+
});
|
|
132
|
+
child.on("close", (code) => {
|
|
133
|
+
// 1 and 2 are tsc saying it found errors: the gate counts them, so only a tsc that broke fails.
|
|
134
|
+
if (watch || code === null || code > 2) {
|
|
135
|
+
console.log(printed.join("\n"));
|
|
136
|
+
process.exit(code ?? 1);
|
|
137
|
+
}
|
|
138
|
+
console.log(typeErrors(printed).join("\n"));
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (import.meta.main) main(process.cwd(), process.argv.slice(2));
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { relative } from "node:path";
|
|
2
|
+
|
|
3
|
+
// A Vitest reporter for a watch: at the end of each run, the state of every spec Vitest has run so far, not only the
|
|
4
|
+
// ones it reran. A rerun of one spec then never hides another that is still red: the last run alone tells the state.
|
|
5
|
+
// It prints a header line, one FAIL line per failed test with its error under it, then the counts.
|
|
6
|
+
|
|
7
|
+
interface TestError {
|
|
8
|
+
message?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface TestCase {
|
|
12
|
+
fullName: string;
|
|
13
|
+
result(): { state: string; errors?: readonly TestError[] };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface TestModule {
|
|
17
|
+
moduleId: string;
|
|
18
|
+
errors(): readonly TestError[];
|
|
19
|
+
children: { allTests(): Iterable<TestCase> };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The header of the state, which the watch helper of the engine looks for. */
|
|
23
|
+
export const HEADER = "Spec states:";
|
|
24
|
+
export const COUNTS = "Specs:";
|
|
25
|
+
|
|
26
|
+
function indented(error: TestError): string[] {
|
|
27
|
+
return (error.message ?? "").split("\n").map((line) => ` ${line}`);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function failuresOf(module: TestModule): string[] {
|
|
31
|
+
const spec = relative(process.cwd(), module.moduleId);
|
|
32
|
+
const lines = module.errors().flatMap((error) => [`FAIL ${spec}`, ...indented(error)]);
|
|
33
|
+
for (const test of module.children.allTests()) {
|
|
34
|
+
const { state, errors = [] } = test.result();
|
|
35
|
+
if (state === "failed") lines.push(`FAIL ${spec} > ${test.fullName}`, ...errors.flatMap(indented));
|
|
36
|
+
}
|
|
37
|
+
return lines;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default class StateReporter {
|
|
41
|
+
private readonly failures = new Map<string, string[]>();
|
|
42
|
+
|
|
43
|
+
onTestRunEnd(modules: readonly TestModule[]): void {
|
|
44
|
+
for (const module of modules) this.failures.set(module.moduleId, failuresOf(module));
|
|
45
|
+
const red = [...this.failures.values()].filter((lines) => lines.length);
|
|
46
|
+
console.log([HEADER, ...red.flat(), `${COUNTS} ${red.length} red of ${this.failures.size}`].join("\n"));
|
|
47
|
+
}
|
|
48
|
+
}
|
package/src/hook/hook.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { type CheckedTurn, checkTurn, startTurn } from "../core/check.ts";
|
|
1
|
+
import { type CheckedTurn, checkTurn, startSession, startTurn } from "../core/check.ts";
|
|
2
2
|
import { configuredRoot, gatesOf } from "../core/config.ts";
|
|
3
3
|
import { isRecord, messageOf, parseJson } from "../core/json.ts";
|
|
4
4
|
import { broken, failures, summary, toolsMissing, toolsToInstall } from "../core/report.ts";
|
|
5
5
|
import { type Judged, judge, waivesOf } from "../core/send-back.ts";
|
|
6
|
+
import { endSession } from "../core/watch.ts";
|
|
6
7
|
import { forgetTold, forgetTurn, readTurn, tellOnce, turnRoot, writeTurn } from "../core/turn-state.ts";
|
|
7
8
|
|
|
8
9
|
export interface HookInput {
|
|
@@ -20,7 +21,14 @@ export interface HookOutput {
|
|
|
20
21
|
hookSpecificOutput?: { hookEventName: "UserPromptSubmit"; additionalContext: string };
|
|
21
22
|
}
|
|
22
23
|
|
|
23
|
-
|
|
24
|
+
/** The events the plugin hooks, each named as the CLI takes it. */
|
|
25
|
+
export const HOOK_EVENTS = ["session-start", "turn-start", "check", "session-end"] as const;
|
|
26
|
+
|
|
27
|
+
export type HookEvent = (typeof HOOK_EVENTS)[number];
|
|
28
|
+
|
|
29
|
+
export function isHookEvent(word: string | undefined): word is HookEvent {
|
|
30
|
+
return HOOK_EVENTS.some((event) => event === word);
|
|
31
|
+
}
|
|
24
32
|
|
|
25
33
|
export function parseHookInput(text: string): HookInput {
|
|
26
34
|
const input = parseJson(text);
|
|
@@ -76,6 +84,17 @@ function cannotRun(error: unknown): HookOutput {
|
|
|
76
84
|
return { systemMessage: broken(messageOf(error)) };
|
|
77
85
|
}
|
|
78
86
|
|
|
87
|
+
/**
|
|
88
|
+
* A session that starts starts the watches of its repo. Whatever stops them is told at the prompt, which reads the
|
|
89
|
+
* same gates.
|
|
90
|
+
*/
|
|
91
|
+
function sessionHook(session: string, cwd: string): void {
|
|
92
|
+
try {
|
|
93
|
+
const root = configuredRoot(cwd);
|
|
94
|
+
if (root !== undefined) startSession(session, root);
|
|
95
|
+
} catch {}
|
|
96
|
+
}
|
|
97
|
+
|
|
79
98
|
/**
|
|
80
99
|
* A prompt ends the turn in progress; from a folder with no gates, or when the turn cannot start, no check will look
|
|
81
100
|
* for a new one. When the gates of the repo miss tools, the agent is asked once to install them.
|
|
@@ -138,6 +157,12 @@ export async function handleHook(event: HookEvent, text: string): Promise<HookOu
|
|
|
138
157
|
// A session that starts, resumes or is cleared asks again for the missing tools; a compaction goes on as it was.
|
|
139
158
|
if (event === "session-start") {
|
|
140
159
|
if (source !== "compact") forgetTold(session);
|
|
160
|
+
sessionHook(session, cwd);
|
|
161
|
+
return undefined;
|
|
162
|
+
}
|
|
163
|
+
// Codex gives a session that ends 3 s at most: the hook only stops the watches no other session uses.
|
|
164
|
+
if (event === "session-end") {
|
|
165
|
+
endSession(session);
|
|
141
166
|
return undefined;
|
|
142
167
|
}
|
|
143
168
|
return event === "turn-start" ? promptHook(session, cwd) : stopHook(session, cwd, retry, message);
|