@tayomi/deterministic-gates 0.4.2 → 0.5.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.
@@ -9,7 +9,7 @@
9
9
  "name": "deterministic-gates",
10
10
  "source": "./",
11
11
  "description": "Gates that run on what each turn changed and send the agent back until they pass.",
12
- "version": "0.4.2"
12
+ "version": "0.5.0"
13
13
  }
14
14
  ]
15
15
  }
@@ -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.2",
4
+ "version": "0.5.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",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "deterministic-gates",
3
- "version": "0.4.2",
3
+ "version": "0.5.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
  "author": {
6
6
  "name": "Pierre Moati",
package/README.md CHANGED
@@ -37,7 +37,7 @@ codex plugin marketplace add mopi1402/tayomi-deterministic-gates
37
37
  codex plugin add deterministic-gates@tayomi-deterministic-gates
38
38
  ```
39
39
 
40
- Codex runs no hook you have not reviewed: in a session, open `/hooks` and trust the three hooks of the plugin. A plugin update that changes a script asks for a new review.
40
+ Codex runs no hook you have not reviewed: in a session, open `/hooks` and trust the four hooks of the plugin. A plugin update that changes a script asks for a new review.
41
41
 
42
42
  ## Configure the gates
43
43
 
@@ -0,0 +1,85 @@
1
+ {
2
+ "title": "Angular",
3
+ "description": "Gates for an Angular project tested with Vitest through the Angular builder, ng test. Each tool comes from the repo's node_modules, so its version is the one the lockfile pins.",
4
+ "gates": [
5
+ {
6
+ "description": "Runs the unit tests in a watch kept warm between turns, since ng test builds the whole application before a single test runs. At the stop, it waits for the watch to finish with the turn's changes, then fails on a build that does not compile, and on every red spec, the ones the change did not rerun included. The whole check runs ng test once, without a watch.",
7
+ "requires": [
8
+ {
9
+ "tool": "@angular/cli",
10
+ "install": "npm install --save-dev @angular/cli"
11
+ },
12
+ {
13
+ "tool": "@angular/build",
14
+ "install": "npm install --save-dev @angular/build"
15
+ },
16
+ {
17
+ "tool": "vitest",
18
+ "install": "npm install --save-dev vitest"
19
+ }
20
+ ],
21
+ "gate": {
22
+ "name": "tests",
23
+ "severity": "error",
24
+ "files": "\\.(ts|html|css)$",
25
+ "run": ["@angular/cli", "test", "--watch=false"],
26
+ "watch": {
27
+ "run": ["{engine}/src/helpers/angular-watch.ts"],
28
+ "cycleStart": "Changes detected\\. Rebuilding",
29
+ "cycleEnd": "Test run finished with success|bundle generation failed",
30
+ "failed": "^FAIL |bundle generation failed"
31
+ }
32
+ }
33
+ },
34
+ {
35
+ "description": "Builds the application in a watch kept warm between turns, which type-checks every template, the ones of a component no spec reaches included: tsc alone never reads a template. At the stop, it fails on a build that does not compile, with the file and line of each error. Built without optimization into node_modules/.cache, so the dist of the project stays as it is. The whole check builds once, without a watch.",
36
+ "requires": [
37
+ {
38
+ "tool": "@angular/cli",
39
+ "install": "npm install --save-dev @angular/cli"
40
+ },
41
+ {
42
+ "tool": "@angular/build",
43
+ "install": "npm install --save-dev @angular/build"
44
+ }
45
+ ],
46
+ "gate": {
47
+ "name": "build",
48
+ "severity": "error",
49
+ "files": "\\.(ts|html|css|scss|sass|less)$",
50
+ "run": ["@angular/cli", "build", "--optimization=false", "--output-path=node_modules/.cache/gates/build"],
51
+ "watch": {
52
+ "run": [
53
+ "@angular/cli",
54
+ "build",
55
+ "--watch",
56
+ "--optimization=false",
57
+ "--output-path=node_modules/.cache/gates/build"
58
+ ],
59
+ "cycleStart": "Changes detected\\. Rebuilding",
60
+ "cycleEnd": "Application bundle generation (complete|failed)",
61
+ "failed": "Application bundle generation failed"
62
+ }
63
+ }
64
+ },
65
+ {
66
+ "description": "Lints the changed templates with angular-eslint, warnings included: the lint of the TypeScript catalog reads no .html file. It needs an ESLint config that gives .html files to angular-eslint, such as its templateRecommended and templateAccessibility configs.",
67
+ "requires": [
68
+ {
69
+ "tool": "eslint",
70
+ "install": "npm install --save-dev eslint"
71
+ },
72
+ {
73
+ "tool": "angular-eslint",
74
+ "install": "npm install --save-dev angular-eslint"
75
+ }
76
+ ],
77
+ "gate": {
78
+ "name": "template-lint",
79
+ "severity": "error",
80
+ "files": "\\.html$",
81
+ "run": ["eslint", "--max-warnings", "0", "--no-warn-ignored", "{files}"]
82
+ }
83
+ }
84
+ ]
85
+ }
@@ -40,8 +40,92 @@ summary { cursor: pointer; color: var(--muted); }
40
40
  <h1>Gate catalog</h1>
41
41
  <p>The gates worth copying into a repo. Nothing here runs by itself: the engine reads only the repo's <code>.gates.json</code>. Copy the definition of a gate there, install what it requires, and adapt its <code>files</code> to the layout of the repo.</p>
42
42
  <p>Generated from <code>catalog/*.json</code> by <code>pnpm catalog</code>: edit those files, never this page.</p>
43
- <nav><a href="#go">Go (6)</a><a href="#python">Python (3)</a><a href="#typescript">TypeScript (8)</a></nav>
43
+ <nav><a href="#angular">Angular (3)</a><a href="#go">Go (6)</a><a href="#python">Python (3)</a><a href="#typescript">TypeScript (9)</a></nav>
44
44
  </header>
45
+ <section id="angular">
46
+ <h2>Angular</h2>
47
+ <p>Gates for an Angular project tested with Vitest through the Angular builder, ng test. Each tool comes from the repo's node_modules, so its version is the one the lockfile pins.</p>
48
+ <article id="angular/tests">
49
+ <h3><code>angular/tests</code></h3>
50
+ <p>Runs the unit tests in a watch kept warm between turns, since ng test builds the whole application before a single test runs. At the stop, it waits for the watch to finish with the turn's changes, then fails on a build that does not compile, and on every red spec, the ones the change did not rerun included. The whole check runs ng test once, without a watch.</p>
51
+ <dl>
52
+ <dt>Runs</dt><dd><code>@angular/cli test --watch=false</code></dd>
53
+ <dt>When the turn changes</dt><dd><code>\.(ts|html|css)$</code></dd>
54
+ <dt>Requires</dt><dd><ul><li><code>@angular/cli</code>: npm install --save-dev @angular/cli</li><li><code>@angular/build</code>: npm install --save-dev @angular/build</li><li><code>vitest</code>: npm install --save-dev vitest</li></ul></dd>
55
+ </dl>
56
+ <details><summary>Definition to copy into .gates.json</summary><pre><code>{
57
+ &quot;name&quot;: &quot;tests&quot;,
58
+ &quot;severity&quot;: &quot;error&quot;,
59
+ &quot;files&quot;: &quot;\\.(ts|html|css)$&quot;,
60
+ &quot;run&quot;: [
61
+ &quot;@angular/cli&quot;,
62
+ &quot;test&quot;,
63
+ &quot;--watch=false&quot;
64
+ ],
65
+ &quot;watch&quot;: {
66
+ &quot;run&quot;: [
67
+ &quot;{engine}/src/helpers/angular-watch.ts&quot;
68
+ ],
69
+ &quot;cycleStart&quot;: &quot;Changes detected\\. Rebuilding&quot;,
70
+ &quot;cycleEnd&quot;: &quot;Test run finished with success|bundle generation failed&quot;,
71
+ &quot;failed&quot;: &quot;^FAIL |bundle generation failed&quot;
72
+ }
73
+ }</code></pre></details>
74
+ </article>
75
+ <article id="angular/build">
76
+ <h3><code>angular/build</code></h3>
77
+ <p>Builds the application in a watch kept warm between turns, which type-checks every template, the ones of a component no spec reaches included: tsc alone never reads a template. At the stop, it fails on a build that does not compile, with the file and line of each error. Built without optimization into node_modules/.cache, so the dist of the project stays as it is. The whole check builds once, without a watch.</p>
78
+ <dl>
79
+ <dt>Runs</dt><dd><code>@angular/cli build --optimization=false --output-path=node_modules/.cache/gates/build</code></dd>
80
+ <dt>When the turn changes</dt><dd><code>\.(ts|html|css|scss|sass|less)$</code></dd>
81
+ <dt>Requires</dt><dd><ul><li><code>@angular/cli</code>: npm install --save-dev @angular/cli</li><li><code>@angular/build</code>: npm install --save-dev @angular/build</li></ul></dd>
82
+ </dl>
83
+ <details><summary>Definition to copy into .gates.json</summary><pre><code>{
84
+ &quot;name&quot;: &quot;build&quot;,
85
+ &quot;severity&quot;: &quot;error&quot;,
86
+ &quot;files&quot;: &quot;\\.(ts|html|css|scss|sass|less)$&quot;,
87
+ &quot;run&quot;: [
88
+ &quot;@angular/cli&quot;,
89
+ &quot;build&quot;,
90
+ &quot;--optimization=false&quot;,
91
+ &quot;--output-path=node_modules/.cache/gates/build&quot;
92
+ ],
93
+ &quot;watch&quot;: {
94
+ &quot;run&quot;: [
95
+ &quot;@angular/cli&quot;,
96
+ &quot;build&quot;,
97
+ &quot;--watch&quot;,
98
+ &quot;--optimization=false&quot;,
99
+ &quot;--output-path=node_modules/.cache/gates/build&quot;
100
+ ],
101
+ &quot;cycleStart&quot;: &quot;Changes detected\\. Rebuilding&quot;,
102
+ &quot;cycleEnd&quot;: &quot;Application bundle generation (complete|failed)&quot;,
103
+ &quot;failed&quot;: &quot;Application bundle generation failed&quot;
104
+ }
105
+ }</code></pre></details>
106
+ </article>
107
+ <article id="angular/template-lint">
108
+ <h3><code>angular/template-lint</code></h3>
109
+ <p>Lints the changed templates with angular-eslint, warnings included: the lint of the TypeScript catalog reads no .html file. It needs an ESLint config that gives .html files to angular-eslint, such as its templateRecommended and templateAccessibility configs.</p>
110
+ <dl>
111
+ <dt>Runs</dt><dd><code>eslint --max-warnings 0 --no-warn-ignored {files}</code></dd>
112
+ <dt>When the turn changes</dt><dd><code>\.html$</code></dd>
113
+ <dt>Requires</dt><dd><ul><li><code>eslint</code>: npm install --save-dev eslint</li><li><code>angular-eslint</code>: npm install --save-dev angular-eslint</li></ul></dd>
114
+ </dl>
115
+ <details><summary>Definition to copy into .gates.json</summary><pre><code>{
116
+ &quot;name&quot;: &quot;template-lint&quot;,
117
+ &quot;severity&quot;: &quot;error&quot;,
118
+ &quot;files&quot;: &quot;\\.html$&quot;,
119
+ &quot;run&quot;: [
120
+ &quot;eslint&quot;,
121
+ &quot;--max-warnings&quot;,
122
+ &quot;0&quot;,
123
+ &quot;--no-warn-ignored&quot;,
124
+ &quot;{files}&quot;
125
+ ]
126
+ }</code></pre></details>
127
+ </article>
128
+ </section>
45
129
  <section id="go">
46
130
  <h2>Go</h2>
47
131
  <p>Gates for a Go module. The toolchain comes from the PATH. The first build of a cold cache is slow, which is why these gates allow 60 seconds.</p>
@@ -350,6 +434,36 @@ summary { cursor: pointer; color: var(--muted); }
350
434
  ]
351
435
  }</code></pre></details>
352
436
  </article>
437
+ <article id="typescript/no-new-strict-errors">
438
+ <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. Give -p and a tsconfig when tsconfig.json only references others, as in an Angular project. The whole check counts every error.</p>
440
+ <dl>
441
+ <dt>Runs</dt><dd><code>{engine}/src/helpers/type-errors.ts --strictNullChecks --strictPropertyInitialization</code></dd>
442
+ <dt>When the turn changes</dt><dd><code>(\.(ts|tsx)|^tsconfig.*\.json)$</code></dd>
443
+ <dt>Fails on</dt><dd>only the output lines the turn added</dd><dt>Requires</dt><dd><ul><li><code>typescript</code>: npm install --save-dev typescript</li></ul></dd>
444
+ </dl>
445
+ <details><summary>Definition to copy into .gates.json</summary><pre><code>{
446
+ &quot;name&quot;: &quot;no-new-strict-errors&quot;,
447
+ &quot;severity&quot;: &quot;error&quot;,
448
+ &quot;files&quot;: &quot;(\\.(ts|tsx)|^tsconfig.*\\.json)$&quot;,
449
+ &quot;run&quot;: [
450
+ &quot;{engine}/src/helpers/type-errors.ts&quot;,
451
+ &quot;--strictNullChecks&quot;,
452
+ &quot;--strictPropertyInitialization&quot;
453
+ ],
454
+ &quot;compare&quot;: &quot;turn-start&quot;,
455
+ &quot;watch&quot;: {
456
+ &quot;run&quot;: [
457
+ &quot;{engine}/src/helpers/type-errors.ts&quot;,
458
+ &quot;--watch&quot;,
459
+ &quot;--strictNullChecks&quot;,
460
+ &quot;--strictPropertyInitialization&quot;
461
+ ],
462
+ &quot;cycleStart&quot;: &quot;^Type check started$&quot;,
463
+ &quot;cycleEnd&quot;: &quot;^Type check done$&quot;
464
+ }
465
+ }</code></pre></details>
466
+ </article>
353
467
  <article id="typescript/no-new-dead-code">
354
468
  <h3><code>typescript/no-new-dead-code</code></h3>
355
469
  <p>Fails on the unused files, exports and dependencies the turn added, never on the ones already there, so it works on a repo that already has some.</p>
@@ -94,6 +94,32 @@
94
94
  "run": ["{engine}/src/helpers/new-clones.ts", "{turn}"]
95
95
  }
96
96
  },
97
+ {
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. Give -p and a tsconfig when tsconfig.json only references others, as in an Angular project. The whole check counts every error.",
99
+ "requires": [
100
+ {
101
+ "tool": "typescript",
102
+ "install": "npm install --save-dev typescript"
103
+ }
104
+ ],
105
+ "gate": {
106
+ "name": "no-new-strict-errors",
107
+ "severity": "error",
108
+ "files": "(\\.(ts|tsx)|^tsconfig.*\\.json)$",
109
+ "run": ["{engine}/src/helpers/type-errors.ts", "--strictNullChecks", "--strictPropertyInitialization"],
110
+ "compare": "turn-start",
111
+ "watch": {
112
+ "run": [
113
+ "{engine}/src/helpers/type-errors.ts",
114
+ "--watch",
115
+ "--strictNullChecks",
116
+ "--strictPropertyInitialization"
117
+ ],
118
+ "cycleStart": "^Type check started$",
119
+ "cycleEnd": "^Type check done$"
120
+ }
121
+ }
122
+ },
97
123
  {
98
124
  "description": "Fails on the unused files, exports and dependencies the turn added, never on the ones already there, so it works on a repo that already has some.",
99
125
  "requires": [
package/dist/src/cli.js CHANGED
@@ -5,7 +5,7 @@ import { configuredRoot } from "./core/config.js";
5
5
  import { messageOf } from "./core/json.js";
6
6
  import { broken, reports, summary } from "./core/report.js";
7
7
  import { failed } from "./core/run.js";
8
- import { handleHook } from "./hook/hook.js";
8
+ import { HOOK_EVENTS, handleHook, isHookEvent } from "./hook/hook.js";
9
9
  function readStdin() {
10
10
  try {
11
11
  return readFileSync(0, "utf8");
@@ -15,7 +15,7 @@ function readStdin() {
15
15
  }
16
16
  }
17
17
  /** What the command prints when it is called with words it does not know, named by the bin of the npm package. */
18
- export const USAGE = "Usage: deterministic-gates check, or deterministic-gates hook <session-start|turn-start|check>";
18
+ export const USAGE = `Usage: deterministic-gates check, or deterministic-gates hook <${HOOK_EVENTS.join("|")}>`;
19
19
  // The helpers of the engine run only on import.meta.main, which came with Node 24.2: below, they are a silent green.
20
20
  const [major = 0, minor = 0] = process.versions.node.split(".").map(Number);
21
21
  const oldNode = major < 24 || (major === 24 && minor < 2)
@@ -30,7 +30,7 @@ async function main([command, event]) {
30
30
  process.exitCode = 2;
31
31
  }
32
32
  }
33
- else if (command === "hook" && (event === "session-start" || event === "turn-start" || event === "check")) {
33
+ else if (command === "hook" && isHookEvent(event)) {
34
34
  const output = await handleHook(event, readStdin());
35
35
  if (output)
36
36
  console.log(JSON.stringify(output));
@@ -1,10 +1,11 @@
1
1
  import { existsSync } from "node:fs";
2
2
  import { join } from "node:path";
3
- import { changedGatesFiles, gatesOf, isCompared, readGatesFiles } from "./config.js";
3
+ import { changedGatesFiles, gatesOf, isCompared, keepsWatch, readGatesFiles } from "./config.js";
4
4
  import { cachedReports, cacheReports } from "./report-cache.js";
5
5
  import { check, filesOf, givenFiles, missingTool, wholeProject } from "./run.js";
6
6
  import { changedSince, emptyCommit, hasCommit, lastCommit, snapshot, workingTree } from "./snapshot.js";
7
7
  import { readTurn, writeTurn } from "./turn-state.js";
8
+ import { judgeWatch, keepWatch, startWatch, watchReport } from "./watch.js";
8
9
  function missingTools(root, gates) {
9
10
  return gates.flatMap((gate) => {
10
11
  const why = missingTool(root, gate);
@@ -14,6 +15,16 @@ function missingTools(root, gates) {
14
15
  function runnable(gates, missing) {
15
16
  return gates.filter((gate) => !missing.some((tool) => tool.gate === gate));
16
17
  }
18
+ function startWatches(session, root, gates) {
19
+ for (const gate of gates)
20
+ if (keepsWatch(gate))
21
+ startWatch(session, root, gate);
22
+ }
23
+ /** Starts the watches of the gates of a repo that are not running yet, so their first build runs before a turn. */
24
+ export function startSession(session, root) {
25
+ const { gates } = gatesOf(readGatesFiles(root));
26
+ startWatches(session, root, runnable(gates, missingTools(root, gates)));
27
+ }
17
28
  /**
18
29
  * Records the commit a turn starts from, and the reports its compared gates will compare with. After an interrupted
19
30
  * turn, the next one of the repo starts where it did, so its check also covers what the interrupted one changed.
@@ -25,6 +36,7 @@ export async function startTurn(session, root) {
25
36
  const missing = missingTools(root, gates);
26
37
  const told = missing.map(({ gate, why }) => `${gate.name}: ${why}`);
27
38
  const compared = runnable(gates, missing).filter(isCompared);
39
+ startWatches(session, root, runnable(gates, missing));
28
40
  const interrupted = readTurn(session, root);
29
41
  if (interrupted?.checked === false) {
30
42
  writeTurn(session, root, interrupted);
@@ -34,17 +46,19 @@ export async function startTurn(session, root) {
34
46
  const cached = cachedReports(root, tree);
35
47
  const baselines = await Promise.all(compared.map(async (gate) => {
36
48
  const cachedReport = cached(gate);
37
- return {
38
- gate,
39
- ...(cachedReport === undefined ? await wholeProject(root, gate) : { ok: true, output: cachedReport }),
40
- };
49
+ if (cachedReport !== undefined)
50
+ return { gate, ok: true, output: cachedReport };
51
+ if (!keepsWatch(gate))
52
+ return { gate, ...(await wholeProject(root, gate)) };
53
+ const output = await watchReport(session, root, gate);
54
+ return { gate, ok: output !== undefined, output };
41
55
  }));
42
56
  // A report from a command that broke still serves this turn, since checking too much beats leaving a hole, but it
43
57
  // is never kept for the next one.
44
- cacheReports(root, tree, baselines.filter(({ ok }) => ok).map(({ gate, output }) => [gate, output]));
58
+ cacheReports(root, tree, baselines.flatMap(({ gate, ok, output }) => (ok && output !== undefined ? [[gate, output]] : [])));
45
59
  writeTurn(session, root, {
46
60
  start,
47
- baselines: Object.fromEntries(baselines.map(({ gate, output }) => [gate.name, output])),
61
+ baselines: Object.fromEntries(baselines.flatMap(({ gate, output }) => (output === undefined ? [] : [[gate.name, output]]))),
48
62
  config,
49
63
  checked: false,
50
64
  });
@@ -53,14 +67,17 @@ export async function startTurn(session, root) {
53
67
  /**
54
68
  * Runs, in parallel, each gate that looks at a changed file. A deleted file counts as changed, since a gate that
55
69
  * reads the whole project may break on it, but is never named: a gate given {files} looks at the files left, and
56
- * is skipped when none is.
70
+ * is skipped when none is. At the end of a turn of a session, a gate that keeps a watch is judged on it instead of
71
+ * running.
57
72
  */
58
- function runGates(root, gates, changed, turn, baselines) {
73
+ function runGates(root, gates, changed, turn, baselines, session) {
59
74
  return Promise.all(gates.map(async (gate) => {
60
75
  const matching = filesOf(gate, changed);
61
76
  const files = givenFiles(gate) ? matching.filter((file) => existsSync(join(root, file))) : matching;
62
77
  if (!files.length)
63
78
  return { gate };
79
+ if (session !== undefined && keepsWatch(gate))
80
+ return { gate, outcome: await judgeWatch(session, root, gate, matching, baselines(gate)) };
64
81
  return { gate, outcome: await check(root, gate, files, turn, baselines(gate)) };
65
82
  }));
66
83
  }
@@ -127,7 +144,10 @@ export async function checkTurn(session, root) {
127
144
  const changed = changedSince(root, turn.start, end);
128
145
  const { gates, refused, showAllPasses } = gatesOf(turn.config);
129
146
  const missing = missingTools(root, gates);
130
- const results = await runGates(root, runnable(gates, missing), changed, turn.start, baselines);
147
+ for (const gate of gates)
148
+ if (keepsWatch(gate))
149
+ keepWatch(root, gate);
150
+ const results = await runGates(root, runnable(gates, missing), changed, turn.start, baselines, session);
131
151
  cacheReports(root, end, endReports(results, baselines));
132
152
  writeTurn(session, root, { ...turn, checked: true });
133
153
  // A compared gate with no report from the turn start can only count its exit code: a green it did not earn is said.
@@ -8,10 +8,14 @@ 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(["name", "severity", "files", "run", "compare", "ignoreLines", "timeoutSeconds"]);
11
+ const FIELDS = new Set(["name", "severity", "files", "run", "compare", "ignoreLines", "timeoutSeconds", "watch"]);
12
+ const WATCH_FIELDS = new Set(["run", "cycleStart", "cycleEnd", "failed"]);
12
13
  /** In the arguments of a gate: the changed files it looks at, and the commit of the turn start. */
13
14
  export const FILES = "{files}";
14
15
  export const TURN = "{turn}";
16
+ export function keepsWatch(gate) {
17
+ return gate.watch !== undefined;
18
+ }
15
19
  /** A gate that fails on the lines of output the turn added, with the report of the turn start to compare with. */
16
20
  export function isCompared(gate) {
17
21
  return gate.compare === "turn-start";
@@ -78,8 +82,29 @@ function isPattern(value) {
78
82
  return false;
79
83
  }
80
84
  }
85
+ function toWatch(watch, compared) {
86
+ if (!isRecord(watch))
87
+ return refuse("watch holds run, cycleStart, cycleEnd and failed");
88
+ const unknown = Object.keys(watch).find((key) => !WATCH_FIELDS.has(key));
89
+ if (unknown !== undefined)
90
+ refuse(`unknown field watch.${unknown}`);
91
+ const { run, cycleStart, cycleEnd, failed } = watch;
92
+ if (!isStringArray(run) || !run.length)
93
+ refuse("watch.run is a non-empty list of strings");
94
+ if (run.some((argument) => argument.includes(FILES) || argument.includes(TURN)))
95
+ refuse(`a watch outlives the turn: its run takes neither ${FILES} nor ${TURN}`);
96
+ const pattern = (field, value) => isPattern(value) ? value : refuse(`watch.${field} is a regular expression on a line of its output`);
97
+ if (compared && failed !== undefined)
98
+ refuse("a compared watch fails on the lines its cycle added: no watch.failed");
99
+ return {
100
+ run,
101
+ cycleStart: pattern("cycleStart", cycleStart),
102
+ cycleEnd: pattern("cycleEnd", cycleEnd),
103
+ ...(compared ? {} : { failed: pattern("failed", failed) }),
104
+ };
105
+ }
81
106
  function toGate(entry, maxTimeoutSeconds) {
82
- const { name, severity, files, run, compare, ignoreLines, timeoutSeconds = maxTimeoutSeconds } = entry;
107
+ const { name, severity, files, run, compare, ignoreLines, watch, timeoutSeconds = maxTimeoutSeconds } = entry;
83
108
  const unknown = Object.keys(entry).find((key) => !FIELDS.has(key));
84
109
  if (unknown !== undefined)
85
110
  refuse(`unknown field ${unknown}`);
@@ -108,6 +133,7 @@ function toGate(entry, maxTimeoutSeconds) {
108
133
  timeoutSeconds: Math.min(timeoutSeconds, maxTimeoutSeconds),
109
134
  ...(compare === undefined ? {} : { compare }),
110
135
  ...(ignoreLines === undefined ? {} : { ignoreLines }),
136
+ ...(watch === undefined ? {} : { watch: toWatch(watch, compare !== undefined) }),
111
137
  };
112
138
  }
113
139
  /**
@@ -9,6 +9,8 @@ export function failed(outcome) {
9
9
  return !outcome.ok && !outcome.timedOut;
10
10
  }
11
11
  const ENGINE = resolve(import.meta.dirname, "..", "..");
12
+ /** What a tool runs with: no colour, which some tools print anyway. */
13
+ export const TOOL_ENV = { ...process.env, NO_COLOR: "1", FORCE_COLOR: "0" };
12
14
  const MAX_OUTPUT_MB = 16;
13
15
  const ENGINE_PREFIX = "{engine}/";
14
16
  /** A script of this engine: its .ts source, or the .js it is built to when the engine runs from its npm package. */
@@ -77,20 +79,23 @@ function executable(file) {
77
79
  }
78
80
  });
79
81
  }
80
- /**
81
- * Why the program of a gate cannot run on this machine, or nothing when it can: looked up as the gate would start it,
82
- * without starting it. A command the engine cannot resolve is left to the run, which reports it.
83
- */
84
- export function missingTool(root, gate) {
82
+ function missingProgram(root, name) {
85
83
  let command;
86
84
  try {
87
- command = commandOf(root, gate.run[0] ?? "");
85
+ command = commandOf(root, name);
88
86
  }
89
87
  catch {
90
88
  return undefined;
91
89
  }
92
90
  return command.missing && !executable(command.file) ? command.missing : undefined;
93
91
  }
92
+ /**
93
+ * Why the program of a gate, or of its watch, cannot run on this machine, or nothing when they can: looked up as the
94
+ * gate would start it, without starting it. A command the engine cannot resolve is left to the run, which reports it.
95
+ */
96
+ export function missingTool(root, gate) {
97
+ return missingProgram(root, gate.run[0] ?? "") ?? (gate.watch && missingProgram(root, gate.watch.run[0] ?? ""));
98
+ }
94
99
  export function argumentsOf(gate, files, turn) {
95
100
  return gate.run.slice(1).flatMap((argument) => (argument === FILES ? files : [argument.replaceAll(TURN, turn)]));
96
101
  }
@@ -126,7 +131,7 @@ async function execute(root, gate, args) {
126
131
  cwd: root,
127
132
  timeout: gate.timeoutSeconds * 1000,
128
133
  maxBuffer: MAX_OUTPUT_MB * 1024 * 1024,
129
- env: { ...process.env, NO_COLOR: "1", FORCE_COLOR: "0" },
134
+ env: TOOL_ENV,
130
135
  }, (error, stdout, stderr) => {
131
136
  resolve({
132
137
  ok: !error,
@@ -144,12 +149,15 @@ function outputOf(root, gate, command, error, printed) {
144
149
  return `Out of time after ${gate.timeoutSeconds} s, so not judged. Make it faster, or move it to CI.`;
145
150
  if (error?.code === "ENOENT")
146
151
  return command.missing;
147
- // Some tools colour despite NO_COLOR, and absolute paths only cost the agent tokens.
148
- const said = stripVTControlCharacters(printed).split(`${root}${sep}`).join("").trim();
152
+ const said = cleaned(root, printed);
149
153
  if (said || !error)
150
154
  return said;
151
155
  return `exit code ${String(error.code)}, no output`;
152
156
  }
157
+ /** What a tool printed, for the agent: some tools colour despite NO_COLOR, and absolute paths only cost it tokens. */
158
+ export function cleaned(root, printed) {
159
+ return stripVTControlCharacters(printed).split(`${root}${sep}`).join("").trim();
160
+ }
153
161
  /** Runs a compared gate on the whole project, as the turn starts. */
154
162
  export function wholeProject(root, gate) {
155
163
  return execute(root, gate, gate.run.slice(1));
@@ -165,8 +173,15 @@ export async function check(root, gate, files, turn, baseline) {
165
173
  return { ...ran, ok, output, timedOut };
166
174
  if (!isCompared(gate) || !ok)
167
175
  return { ...ran, ok, output };
176
+ return { ...ran, ...addedLines(gate, output, baseline) };
177
+ }
178
+ /**
179
+ * Judges the output of a compared gate that ran clean: it fails on the lines the turn added to the report of the turn
180
+ * start. Without that report, nothing counts but the exit code.
181
+ */
182
+ export function addedLines(gate, output, baseline) {
168
183
  if (baseline === undefined)
169
- return { ...ran, ok: true, output: "" };
184
+ return { ok: true, output: "" };
170
185
  const added = newLines(baseline, output, gate.ignoreLines);
171
- return { ...ran, ok: added.length === 0, output: added.join("\n") };
186
+ return { ok: added.length === 0, output: added.join("\n") };
172
187
  }