@tayomi/deterministic-gates 0.7.0 → 0.7.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/angular.json +3 -3
- package/catalog/index.html +3 -3
- package/dist/src/core/watch.js +108 -70
- package/package.json +1 -1
- package/src/core/watch.ts +108 -75
|
@@ -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.7.
|
|
4
|
+
"version": "0.7.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/angular.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
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
4
|
"gates": [
|
|
5
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. The watch is asked for a cycle through tsconfig.spec.json, and starts again when a spec is created or deleted, since ng test lists its specs once.",
|
|
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. The watch is asked for a cycle through tsconfig.spec.json, and starts again when a spec is created or deleted, since ng test lists its specs once. Angular 21 does not rebuild for a tsconfig: give its watch.sync a spec.",
|
|
7
7
|
"requires": [
|
|
8
8
|
{
|
|
9
9
|
"tool": "@angular/cli",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
}
|
|
35
35
|
},
|
|
36
36
|
{
|
|
37
|
-
"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. The watch is asked for a cycle through
|
|
37
|
+
"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. The watch is asked for a cycle through src/main.ts.",
|
|
38
38
|
"requires": [
|
|
39
39
|
{
|
|
40
40
|
"tool": "@angular/cli",
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
"cycleStart": "Changes detected\\. Rebuilding",
|
|
62
62
|
"cycleEnd": "Application bundle generation (complete|failed)",
|
|
63
63
|
"failed": "Application bundle generation failed",
|
|
64
|
-
"sync": "
|
|
64
|
+
"sync": "src/main.ts"
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
67
|
},
|
package/catalog/index.html
CHANGED
|
@@ -47,7 +47,7 @@ summary { cursor: pointer; color: var(--muted); }
|
|
|
47
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
48
|
<article id="angular/tests">
|
|
49
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. The watch is asked for a cycle through tsconfig.spec.json, and starts again when a spec is created or deleted, since ng test lists its specs once.</p>
|
|
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. The watch is asked for a cycle through tsconfig.spec.json, and starts again when a spec is created or deleted, since ng test lists its specs once. Angular 21 does not rebuild for a tsconfig: give its watch.sync a spec.</p>
|
|
51
51
|
<dl>
|
|
52
52
|
<dt>Runs</dt><dd><code>@angular/cli test --watch=false</code></dd>
|
|
53
53
|
<dt>When the turn changes</dt><dd><code>\.(ts|html|css)$</code></dd>
|
|
@@ -76,7 +76,7 @@ summary { cursor: pointer; color: var(--muted); }
|
|
|
76
76
|
</article>
|
|
77
77
|
<article id="angular/build">
|
|
78
78
|
<h3><code>angular/build</code></h3>
|
|
79
|
-
<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. The watch is asked for a cycle through
|
|
79
|
+
<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. The watch is asked for a cycle through src/main.ts.</p>
|
|
80
80
|
<dl>
|
|
81
81
|
<dt>Runs</dt><dd><code>@angular/cli build --optimization=false --output-path=node_modules/.cache/gates/build</code></dd>
|
|
82
82
|
<dt>When the turn changes</dt><dd><code>\.(ts|html|css|scss|sass|less)$</code></dd>
|
|
@@ -103,7 +103,7 @@ summary { cursor: pointer; color: var(--muted); }
|
|
|
103
103
|
"cycleStart": "Changes detected\\. Rebuilding",
|
|
104
104
|
"cycleEnd": "Application bundle generation (complete|failed)",
|
|
105
105
|
"failed": "Application bundle generation failed",
|
|
106
|
-
"sync": "
|
|
106
|
+
"sync": "src/main.ts"
|
|
107
107
|
}
|
|
108
108
|
}</code></pre></details>
|
|
109
109
|
</article>
|
package/dist/src/core/watch.js
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
2
|
import { createHash, randomUUID } from "node:crypto";
|
|
3
|
-
import { closeSync, existsSync, mkdirSync, openSync, readdirSync, readFileSync, rmSync, statSync, utimesSync, writeFileSync, } from "node:fs";
|
|
3
|
+
import { closeSync, existsSync, mkdirSync, openSync, readdirSync, readFileSync, renameSync, rmSync, statSync, utimesSync, writeFileSync, } from "node:fs";
|
|
4
4
|
import { tmpdir } from "node:os";
|
|
5
|
-
import {
|
|
5
|
+
import { 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
9
|
import { addedLines, cleaned, commandOf, ENGINE_VERSION, toolEnv } from "./run.js";
|
|
10
10
|
import { listFiles } from "./snapshot.js";
|
|
11
11
|
// A watch that has not opened a cycle this long after the engine touched its sync file is touched again: it may look
|
|
12
|
-
// for changes only once its first cycle is over, as Angular and tsc do, and never see a touch made before.
|
|
13
|
-
//
|
|
12
|
+
// for changes only once its first cycle is over, as Angular and tsc do, and never see a touch made before. Each wait
|
|
13
|
+
// is twice the one before, since a watch on a busy machine takes seconds to say it opens a cycle, and a touch too
|
|
14
|
+
// many is a cycle too many. The wait changes no verdict, which only ever comes from a cycle the watch opened after a
|
|
15
|
+
// touch. The tests of the engine shorten it.
|
|
14
16
|
const RESYNC_MS = 1000;
|
|
17
|
+
// A watch that opens no cycle after this many touches, 31 s after the first, does not look at its sync file.
|
|
18
|
+
const SYNC_TOUCHES = 5;
|
|
15
19
|
const POLL_MS = 100;
|
|
16
20
|
// What a watch that stopped leaves the agent: enough to see the error, not the whole history of the session.
|
|
17
21
|
const LAST_LINES = 20;
|
|
@@ -26,6 +30,9 @@ const IDLE_MS = 30 * 60 * 1000;
|
|
|
26
30
|
function idleMs() {
|
|
27
31
|
return Number(process.env.GATES_WATCH_IDLE_MS) || IDLE_MS;
|
|
28
32
|
}
|
|
33
|
+
function resyncMs() {
|
|
34
|
+
return Number(process.env.GATES_WATCH_RESYNC_MS) || RESYNC_MS;
|
|
35
|
+
}
|
|
29
36
|
/** Records a prompt or a stop on the repo of a watch, which keeps it from stopping for idleness. */
|
|
30
37
|
function touch(folder) {
|
|
31
38
|
writeFileSync(join(folder, "activity"), "");
|
|
@@ -129,10 +136,14 @@ function launch(folder, root, watch) {
|
|
|
129
136
|
rmSync(join(folder, "idle"), { force: true });
|
|
130
137
|
// The first cycle of a watch reads every file there is: it owes a cycle for none.
|
|
131
138
|
rmSync(join(folder, "owed"), { recursive: true, force: true });
|
|
139
|
+
// What the watch before this one left of its cycles.
|
|
140
|
+
for (const left of ["cycles", "cycle", "last"])
|
|
141
|
+
rmSync(join(folder, left), { force: true });
|
|
132
142
|
const [name = "", ...args] = watch.run;
|
|
133
143
|
const command = commandOf(root, name);
|
|
134
144
|
const supervisor = commandOf(root, "{engine}/src/core/watch.ts");
|
|
135
|
-
const
|
|
145
|
+
const cycle = [watch.cycleStart, watch.cycleEnd];
|
|
146
|
+
const watched = [folder, String(idleMs()), ...cycle, command.file, ...command.args, ...args];
|
|
136
147
|
// Listed before the watch starts: a file created in between counts as one it may not know, never the other way.
|
|
137
148
|
writeFileSync(join(folder, "frozen"), frozenFiles(root, watch));
|
|
138
149
|
const log = openSync(join(folder, "output.log"), "w");
|
|
@@ -147,32 +158,10 @@ function launch(folder, root, watch) {
|
|
|
147
158
|
child.unref();
|
|
148
159
|
writeFileSync(join(folder, "pid"), String(child.pid ?? ""));
|
|
149
160
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
const opens = new RegExp(watch.cycleStart);
|
|
155
|
-
const closes = new RegExp(watch.cycleEnd);
|
|
156
|
-
// The start of the watch opens the first cycle, and lines after the one that closes a cycle still belong to it. A
|
|
157
|
-
// line that opens a cycle while one is open belongs to that one: Angular says it rebuilds as it starts to, and
|
|
158
|
-
// again once it has, and counting both would end a cycle before its first line.
|
|
159
|
-
const starts = [];
|
|
160
|
-
let open = true;
|
|
161
|
-
lines.forEach((line, at) => {
|
|
162
|
-
if (closes.test(line))
|
|
163
|
-
open = false;
|
|
164
|
-
else if (!open && opens.test(line)) {
|
|
165
|
-
starts.push(at);
|
|
166
|
-
open = true;
|
|
167
|
-
}
|
|
168
|
-
});
|
|
169
|
-
const last = starts.at(-1);
|
|
170
|
-
return {
|
|
171
|
-
opened: starts.length,
|
|
172
|
-
open,
|
|
173
|
-
// Less the lines that say it opened, which tell the agent nothing.
|
|
174
|
-
complete: last === undefined ? [] : lines.slice((starts.at(-2) ?? -1) + 1, last).filter((line) => !opens.test(line)),
|
|
175
|
-
};
|
|
161
|
+
// A watch whose supervisor has written nothing yet is in the cycle its start opens.
|
|
162
|
+
function cyclesOf(folder) {
|
|
163
|
+
const [opened, state] = readText(join(folder, "cycles")).split(" ");
|
|
164
|
+
return { opened: Number(opened), open: state !== "closed" };
|
|
176
165
|
}
|
|
177
166
|
/** Gives the files their date again, as a change of nothing else, for a watch that looks at their date. */
|
|
178
167
|
function touchAgain(paths) {
|
|
@@ -192,14 +181,19 @@ function changedAt(path) {
|
|
|
192
181
|
return 0;
|
|
193
182
|
}
|
|
194
183
|
}
|
|
195
|
-
function
|
|
184
|
+
function readText(path) {
|
|
196
185
|
try {
|
|
197
|
-
return readFileSync(
|
|
186
|
+
return readFileSync(path, "utf8");
|
|
198
187
|
}
|
|
199
188
|
catch {
|
|
200
189
|
return "";
|
|
201
190
|
}
|
|
202
191
|
}
|
|
192
|
+
/** Writes a file at once, so that a hook reading it never finds half of it. */
|
|
193
|
+
function writeWhole(path, text) {
|
|
194
|
+
writeFileSync(`${path}.new`, text);
|
|
195
|
+
renameSync(`${path}.new`, path);
|
|
196
|
+
}
|
|
203
197
|
/**
|
|
204
198
|
* Starts the watch again when a file it lists only as it starts was created or deleted since: its first cycle then
|
|
205
199
|
* reads every file there is. Only one hook does, and only the watch it looked at: another session may be here too.
|
|
@@ -207,10 +201,10 @@ function readLog(log) {
|
|
|
207
201
|
async function restartOnFrozenChange(session, root, gate, folder) {
|
|
208
202
|
if (gate.watch.frozen === undefined || stateOf(folder) !== "running")
|
|
209
203
|
return;
|
|
210
|
-
if (
|
|
204
|
+
if (readText(join(folder, "frozen")) === frozenFiles(root, gate.watch) || !lockStart(folder))
|
|
211
205
|
return;
|
|
212
206
|
try {
|
|
213
|
-
if (stateOf(folder) !== "running" ||
|
|
207
|
+
if (stateOf(folder) !== "running" || readText(join(folder, "frozen")) === frozenFiles(root, gate.watch))
|
|
214
208
|
return;
|
|
215
209
|
// Stopped by the engine, so that another session waiting on the watch does not take it for one that broke.
|
|
216
210
|
writeFileSync(join(folder, "idle"), "");
|
|
@@ -229,20 +223,20 @@ async function restartOnFrozenChange(session, root, gate, folder) {
|
|
|
229
223
|
* Asks the watch of a gate for a cycle that read the files of the turn, and waits for it, with no guess on how long a
|
|
230
224
|
* watch takes to see a change. Each round touches files, then waits for the watch to open a cycle after the touch,
|
|
231
225
|
* which only a watch that saw the touch does:
|
|
232
|
-
* 1. the
|
|
233
|
-
*
|
|
234
|
-
*
|
|
226
|
+
* 1. the files of the turn, so that a change it missed reaches it, and the sync file. A watch that does not look for
|
|
227
|
+
* changes yet sees neither, and both are touched again;
|
|
228
|
+
* 2. the sync file, once that cycle has closed: the cycle of round 1 may have been on its way before the touch, and
|
|
235
229
|
* this one opens after every change the watch then had waiting;
|
|
236
|
-
*
|
|
230
|
+
* 3. the sync file once more, since the cycle it opens ends the one of round 2, the one that judges, whose last lines
|
|
237
231
|
* may come after the line that closes it.
|
|
238
|
-
* A watch no
|
|
232
|
+
* A watch that opens no cycle however often it is touched does not look at its sync file, and is said so. A watch no
|
|
233
|
+
* hook started, as when the hooks were installed mid-session, starts now, and so does one the engine
|
|
239
234
|
* stopped, for idleness or to start it again: it is not to blame. One another session starts again meanwhile is asked
|
|
240
235
|
* again from the first round.
|
|
241
236
|
*/
|
|
242
237
|
async function settle(session, root, gate, files, limitMs) {
|
|
243
238
|
const started = performance.now();
|
|
244
239
|
const folder = folderOf(root, gate);
|
|
245
|
-
const log = join(folder, "output.log");
|
|
246
240
|
const sync = join(root, gate.watch.sync);
|
|
247
241
|
if (!existsSync(sync)) {
|
|
248
242
|
const stopped = `The watch cannot be asked for a cycle: its watch.sync, ${gate.watch.sync}, does not exist.`;
|
|
@@ -250,10 +244,11 @@ async function settle(session, root, gate, files, limitMs) {
|
|
|
250
244
|
}
|
|
251
245
|
await restartOnFrozenChange(session, root, gate, folder);
|
|
252
246
|
const turn = files.map((file) => join(root, file)).filter((path) => existsSync(path));
|
|
253
|
-
const rounds = [[
|
|
247
|
+
const rounds = [[...turn, sync], [sync], [sync]];
|
|
254
248
|
let asked;
|
|
255
249
|
let round = 0;
|
|
256
|
-
// The cycles the watch had opened when the round touched its files,
|
|
250
|
+
// The cycles the watch had opened when the round touched its files, when it last did, and how many times: none
|
|
251
|
+
// before its first touch.
|
|
257
252
|
let touched;
|
|
258
253
|
for (;;) {
|
|
259
254
|
const state = stateOf(folder);
|
|
@@ -261,14 +256,12 @@ async function settle(session, root, gate, files, limitMs) {
|
|
|
261
256
|
startWatch(session, root, gate);
|
|
262
257
|
// Waiting on the watch is using it: it does not stop for idleness under a stop that waits.
|
|
263
258
|
touch(folder);
|
|
264
|
-
// Read once the watch is known stopped, so its last lines are in.
|
|
265
|
-
const printed = readLog(log);
|
|
266
259
|
const ms = performance.now() - started;
|
|
267
260
|
if (state === "stopped") {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
261
|
+
// Read once the watch is known stopped, so its last lines are in: the ones its supervisor kept, or what the
|
|
262
|
+
// supervisor itself printed when it broke.
|
|
263
|
+
const printed = readText(join(folder, "last")) || readText(join(folder, "output.log"));
|
|
264
|
+
const last = cleaned(root, printed.trimEnd().split("\n").slice(-LAST_LINES).join("\n"));
|
|
272
265
|
return {
|
|
273
266
|
ms,
|
|
274
267
|
stopped: last ? `The watch stopped. Its last lines:\n${last}` : "The watch stopped, with no output.",
|
|
@@ -280,24 +273,29 @@ async function settle(session, root, gate, files, limitMs) {
|
|
|
280
273
|
round = 0;
|
|
281
274
|
touched = undefined;
|
|
282
275
|
}
|
|
283
|
-
const cycles = cyclesOf(
|
|
276
|
+
const cycles = cyclesOf(folder);
|
|
284
277
|
if (state === "running" && touched === undefined) {
|
|
285
278
|
// A round starts once the cycle before it has closed.
|
|
286
279
|
if (!cycles.open) {
|
|
287
280
|
touchAgain(rounds[round] ?? []);
|
|
288
|
-
touched = { opened: cycles.opened, at: Date.now() };
|
|
281
|
+
touched = { opened: cycles.opened, at: Date.now(), times: 1 };
|
|
289
282
|
}
|
|
290
283
|
}
|
|
291
284
|
else if (touched !== undefined && cycles.opened > touched.opened) {
|
|
285
|
+
// Written before the count of the cycle that ended it: the cycle read is that one, or a later one.
|
|
292
286
|
if (round === rounds.length - 1)
|
|
293
|
-
return { ms, cycle: cleaned(root,
|
|
287
|
+
return { ms, cycle: cleaned(root, readText(join(folder, "cycle"))) };
|
|
294
288
|
round++;
|
|
295
289
|
touched = undefined;
|
|
296
290
|
continue;
|
|
297
291
|
}
|
|
298
|
-
else if (touched !== undefined && Date.now() - touched.at >=
|
|
292
|
+
else if (touched !== undefined && Date.now() - touched.at >= resyncMs() * 2 ** (touched.times - 1)) {
|
|
293
|
+
if (touched.times === SYNC_TOUCHES) {
|
|
294
|
+
const stopped = `The watch opens no cycle when the date of its watch.sync, ${gate.watch.sync}, changes: give it a file the watch rebuilds for, such as a source file it reads.`;
|
|
295
|
+
return { ms, stopped };
|
|
296
|
+
}
|
|
299
297
|
touchAgain(rounds[round] ?? []);
|
|
300
|
-
touched
|
|
298
|
+
touched = { ...touched, at: Date.now(), times: touched.times + 1 };
|
|
301
299
|
}
|
|
302
300
|
if (ms >= limitMs)
|
|
303
301
|
return { ms, timedOut: true };
|
|
@@ -310,7 +308,7 @@ async function settle(session, root, gate, files, limitMs) {
|
|
|
310
308
|
*/
|
|
311
309
|
function owedFiles(folder) {
|
|
312
310
|
const lists = readDir(join(folder, "owed")).map((name) => join(folder, "owed", name));
|
|
313
|
-
const files = lists.flatMap((list) =>
|
|
311
|
+
const files = lists.flatMap((list) => readText(list).split("\0").filter(Boolean));
|
|
314
312
|
return { lists, files: [...new Set(files)] };
|
|
315
313
|
}
|
|
316
314
|
function owe(folder, files) {
|
|
@@ -391,29 +389,69 @@ function stop(folder) {
|
|
|
391
389
|
catch { }
|
|
392
390
|
}
|
|
393
391
|
/**
|
|
394
|
-
* The supervisor of a watch, which this module runs as when the engine starts one: it runs the watch
|
|
395
|
-
*
|
|
396
|
-
*
|
|
397
|
-
* the
|
|
392
|
+
* The supervisor of a watch, which this module runs as when the engine starts one: it runs the watch and stops it
|
|
393
|
+
* with every process it started once its repo has had no prompt nor stop for the idle delay. Started detached, it
|
|
394
|
+
* leads a process group of its own, so stopping the group stops the watch and the tools under it. It counts the
|
|
395
|
+
* cycles of the watch as it prints them, and keeps only the lines a hook reads: a watch prints thousands of lines per
|
|
396
|
+
* cycle for as long as a session lasts, which no hook could read again at each look.
|
|
398
397
|
*/
|
|
399
|
-
function supervise(
|
|
398
|
+
function supervise(folder, idle, cycle, command) {
|
|
399
|
+
const [file = "", ...args] = command;
|
|
400
|
+
const opens = new RegExp(cycle.start);
|
|
401
|
+
const closes = new RegExp(cycle.end);
|
|
400
402
|
const watch = spawn(file, args, { stdio: ["ignore", "pipe", "pipe"] });
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
403
|
+
// The start of the watch opens the first cycle, and lines after the one that closes a cycle still belong to it. A
|
|
404
|
+
// line that opens a cycle while one is open belongs to that one: Angular says it rebuilds as it starts to, and
|
|
405
|
+
// again once it has, and counting both would end a cycle before its first line.
|
|
406
|
+
let opened = 0;
|
|
407
|
+
let open = true;
|
|
408
|
+
// The lines of the cycle in progress, less the ones that say it opened, which tell the agent nothing.
|
|
409
|
+
let lines = [];
|
|
410
|
+
// What a watch that stopped leaves the agent.
|
|
411
|
+
const last = [];
|
|
412
|
+
const count = () => writeWhole(join(folder, "cycles"), `${opened} ${open ? "open" : "closed"}`);
|
|
413
|
+
const read = (line) => {
|
|
414
|
+
last.push(line);
|
|
415
|
+
if (last.length > LAST_LINES)
|
|
416
|
+
last.shift();
|
|
417
|
+
if (closes.test(line)) {
|
|
418
|
+
if (open) {
|
|
419
|
+
open = false;
|
|
420
|
+
count();
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
else if (!open && opens.test(line)) {
|
|
424
|
+
// Nothing is printed in a cycle any more once the next one has opened: its lines are whole, and written before
|
|
425
|
+
// the count that tells a hook of them.
|
|
426
|
+
writeWhole(join(folder, "cycle"), lines.join("\n"));
|
|
427
|
+
lines = [];
|
|
428
|
+
opened++;
|
|
429
|
+
open = true;
|
|
430
|
+
count();
|
|
431
|
+
}
|
|
432
|
+
if (!opens.test(line))
|
|
433
|
+
lines.push(line);
|
|
434
|
+
};
|
|
435
|
+
const end = (code) => {
|
|
436
|
+
writeWhole(join(folder, "last"), last.join("\n"));
|
|
437
|
+
process.exit(code);
|
|
438
|
+
};
|
|
439
|
+
count();
|
|
440
|
+
createInterface({ input: watch.stdout }).on("line", read);
|
|
441
|
+
createInterface({ input: watch.stderr }).on("line", read);
|
|
404
442
|
watch.on("error", (error) => {
|
|
405
|
-
|
|
406
|
-
|
|
443
|
+
last.push(error.message);
|
|
444
|
+
end(1);
|
|
407
445
|
});
|
|
408
|
-
watch.on("close", (code) =>
|
|
446
|
+
watch.on("close", (code) => end(code ?? 1));
|
|
409
447
|
setInterval(() => {
|
|
410
|
-
if (Date.now() - changedAt(activity) < idle)
|
|
448
|
+
if (Date.now() - changedAt(join(folder, "activity")) < idle)
|
|
411
449
|
return;
|
|
412
|
-
writeFileSync(join(
|
|
450
|
+
writeFileSync(join(folder, "idle"), "");
|
|
413
451
|
process.kill(-process.pid, "SIGTERM");
|
|
414
452
|
}, Math.min(idle / 4, 10_000));
|
|
415
453
|
}
|
|
416
454
|
if (import.meta.main) {
|
|
417
|
-
const [
|
|
418
|
-
supervise(
|
|
455
|
+
const [folder = "", idle = "", start = "", end = "", ...command] = process.argv.slice(2);
|
|
456
|
+
supervise(folder, Number(idle), { start, end }, command);
|
|
419
457
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tayomi/deterministic-gates",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.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/watch.ts
CHANGED
|
@@ -7,13 +7,14 @@ import {
|
|
|
7
7
|
openSync,
|
|
8
8
|
readdirSync,
|
|
9
9
|
readFileSync,
|
|
10
|
+
renameSync,
|
|
10
11
|
rmSync,
|
|
11
12
|
statSync,
|
|
12
13
|
utimesSync,
|
|
13
14
|
writeFileSync,
|
|
14
15
|
} from "node:fs";
|
|
15
16
|
import { tmpdir } from "node:os";
|
|
16
|
-
import {
|
|
17
|
+
import { join } from "node:path";
|
|
17
18
|
import { createInterface } from "node:readline";
|
|
18
19
|
import { setTimeout as sleep } from "node:timers/promises";
|
|
19
20
|
import { type Gate, isCompared, type Watch, type WatchedGate } from "./config.ts";
|
|
@@ -21,9 +22,13 @@ import { addedLines, cleaned, commandOf, ENGINE_VERSION, type Outcome, toolEnv }
|
|
|
21
22
|
import { listFiles } from "./snapshot.ts";
|
|
22
23
|
|
|
23
24
|
// A watch that has not opened a cycle this long after the engine touched its sync file is touched again: it may look
|
|
24
|
-
// for changes only once its first cycle is over, as Angular and tsc do, and never see a touch made before.
|
|
25
|
-
//
|
|
25
|
+
// for changes only once its first cycle is over, as Angular and tsc do, and never see a touch made before. Each wait
|
|
26
|
+
// is twice the one before, since a watch on a busy machine takes seconds to say it opens a cycle, and a touch too
|
|
27
|
+
// many is a cycle too many. The wait changes no verdict, which only ever comes from a cycle the watch opened after a
|
|
28
|
+
// touch. The tests of the engine shorten it.
|
|
26
29
|
const RESYNC_MS = 1000;
|
|
30
|
+
// A watch that opens no cycle after this many touches, 31 s after the first, does not look at its sync file.
|
|
31
|
+
const SYNC_TOUCHES = 5;
|
|
27
32
|
const POLL_MS = 100;
|
|
28
33
|
// What a watch that stopped leaves the agent: enough to see the error, not the whole history of the session.
|
|
29
34
|
const LAST_LINES = 20;
|
|
@@ -41,6 +46,10 @@ function idleMs(): number {
|
|
|
41
46
|
return Number(process.env.GATES_WATCH_IDLE_MS) || IDLE_MS;
|
|
42
47
|
}
|
|
43
48
|
|
|
49
|
+
function resyncMs(): number {
|
|
50
|
+
return Number(process.env.GATES_WATCH_RESYNC_MS) || RESYNC_MS;
|
|
51
|
+
}
|
|
52
|
+
|
|
44
53
|
/** Records a prompt or a stop on the repo of a watch, which keeps it from stopping for idleness. */
|
|
45
54
|
function touch(folder: string): void {
|
|
46
55
|
writeFileSync(join(folder, "activity"), "");
|
|
@@ -144,10 +153,13 @@ function launch(folder: string, root: string, watch: Watch): void {
|
|
|
144
153
|
rmSync(join(folder, "idle"), { force: true });
|
|
145
154
|
// The first cycle of a watch reads every file there is: it owes a cycle for none.
|
|
146
155
|
rmSync(join(folder, "owed"), { recursive: true, force: true });
|
|
156
|
+
// What the watch before this one left of its cycles.
|
|
157
|
+
for (const left of ["cycles", "cycle", "last"]) rmSync(join(folder, left), { force: true });
|
|
147
158
|
const [name = "", ...args] = watch.run;
|
|
148
159
|
const command = commandOf(root, name);
|
|
149
160
|
const supervisor = commandOf(root, "{engine}/src/core/watch.ts");
|
|
150
|
-
const
|
|
161
|
+
const cycle = [watch.cycleStart, watch.cycleEnd];
|
|
162
|
+
const watched = [folder, String(idleMs()), ...cycle, command.file, ...command.args, ...args];
|
|
151
163
|
// Listed before the watch starts: a file created in between counts as one it may not know, never the other way.
|
|
152
164
|
writeFileSync(join(folder, "frozen"), frozenFiles(root, watch));
|
|
153
165
|
const log = openSync(join(folder, "output.log"), "w");
|
|
@@ -163,41 +175,18 @@ function launch(folder: string, root: string, watch: Watch): void {
|
|
|
163
175
|
writeFileSync(join(folder, "pid"), String(child.pid ?? ""));
|
|
164
176
|
}
|
|
165
177
|
|
|
166
|
-
/** The cycles of a watch
|
|
178
|
+
/** The cycles of a watch since it started, as its supervisor counts them. */
|
|
167
179
|
interface Cycles {
|
|
168
180
|
/** How many cycles the watch opened, the one its start opens left out. */
|
|
169
181
|
opened: number;
|
|
170
182
|
/** Whether the last cycle is still open. */
|
|
171
183
|
open: boolean;
|
|
172
|
-
/** The lines of the cycle before the last: nothing is printed in it any more, since the next one has opened. */
|
|
173
|
-
complete: string[];
|
|
174
184
|
}
|
|
175
185
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
const closes = new RegExp(watch.cycleEnd);
|
|
181
|
-
// The start of the watch opens the first cycle, and lines after the one that closes a cycle still belong to it. A
|
|
182
|
-
// line that opens a cycle while one is open belongs to that one: Angular says it rebuilds as it starts to, and
|
|
183
|
-
// again once it has, and counting both would end a cycle before its first line.
|
|
184
|
-
const starts: number[] = [];
|
|
185
|
-
let open = true;
|
|
186
|
-
lines.forEach((line, at) => {
|
|
187
|
-
if (closes.test(line)) open = false;
|
|
188
|
-
else if (!open && opens.test(line)) {
|
|
189
|
-
starts.push(at);
|
|
190
|
-
open = true;
|
|
191
|
-
}
|
|
192
|
-
});
|
|
193
|
-
const last = starts.at(-1);
|
|
194
|
-
return {
|
|
195
|
-
opened: starts.length,
|
|
196
|
-
open,
|
|
197
|
-
// Less the lines that say it opened, which tell the agent nothing.
|
|
198
|
-
complete:
|
|
199
|
-
last === undefined ? [] : lines.slice((starts.at(-2) ?? -1) + 1, last).filter((line) => !opens.test(line)),
|
|
200
|
-
};
|
|
186
|
+
// A watch whose supervisor has written nothing yet is in the cycle its start opens.
|
|
187
|
+
function cyclesOf(folder: string): Cycles {
|
|
188
|
+
const [opened, state] = readText(join(folder, "cycles")).split(" ");
|
|
189
|
+
return { opened: Number(opened), open: state !== "closed" };
|
|
201
190
|
}
|
|
202
191
|
|
|
203
192
|
/** Gives the files their date again, as a change of nothing else, for a watch that looks at their date. */
|
|
@@ -218,14 +207,20 @@ function changedAt(path: string): number {
|
|
|
218
207
|
}
|
|
219
208
|
}
|
|
220
209
|
|
|
221
|
-
function
|
|
210
|
+
function readText(path: string): string {
|
|
222
211
|
try {
|
|
223
|
-
return readFileSync(
|
|
212
|
+
return readFileSync(path, "utf8");
|
|
224
213
|
} catch {
|
|
225
214
|
return "";
|
|
226
215
|
}
|
|
227
216
|
}
|
|
228
217
|
|
|
218
|
+
/** Writes a file at once, so that a hook reading it never finds half of it. */
|
|
219
|
+
function writeWhole(path: string, text: string): void {
|
|
220
|
+
writeFileSync(`${path}.new`, text);
|
|
221
|
+
renameSync(`${path}.new`, path);
|
|
222
|
+
}
|
|
223
|
+
|
|
229
224
|
/** Where a watch is once it has answered, with the lines of the cycle that judges, or once it no longer can. */
|
|
230
225
|
type Settled = { ms: number } & ({ cycle: string } | { stopped: string } | { timedOut: true });
|
|
231
226
|
|
|
@@ -235,9 +230,9 @@ type Settled = { ms: number } & ({ cycle: string } | { stopped: string } | { tim
|
|
|
235
230
|
*/
|
|
236
231
|
async function restartOnFrozenChange(session: string, root: string, gate: WatchedGate, folder: string): Promise<void> {
|
|
237
232
|
if (gate.watch.frozen === undefined || stateOf(folder) !== "running") return;
|
|
238
|
-
if (
|
|
233
|
+
if (readText(join(folder, "frozen")) === frozenFiles(root, gate.watch) || !lockStart(folder)) return;
|
|
239
234
|
try {
|
|
240
|
-
if (stateOf(folder) !== "running" ||
|
|
235
|
+
if (stateOf(folder) !== "running" || readText(join(folder, "frozen")) === frozenFiles(root, gate.watch)) return;
|
|
241
236
|
// Stopped by the engine, so that another session waiting on the watch does not take it for one that broke.
|
|
242
237
|
writeFileSync(join(folder, "idle"), "");
|
|
243
238
|
stop(folder);
|
|
@@ -254,13 +249,14 @@ async function restartOnFrozenChange(session: string, root: string, gate: Watche
|
|
|
254
249
|
* Asks the watch of a gate for a cycle that read the files of the turn, and waits for it, with no guess on how long a
|
|
255
250
|
* watch takes to see a change. Each round touches files, then waits for the watch to open a cycle after the touch,
|
|
256
251
|
* which only a watch that saw the touch does:
|
|
257
|
-
* 1. the
|
|
258
|
-
*
|
|
259
|
-
*
|
|
252
|
+
* 1. the files of the turn, so that a change it missed reaches it, and the sync file. A watch that does not look for
|
|
253
|
+
* changes yet sees neither, and both are touched again;
|
|
254
|
+
* 2. the sync file, once that cycle has closed: the cycle of round 1 may have been on its way before the touch, and
|
|
260
255
|
* this one opens after every change the watch then had waiting;
|
|
261
|
-
*
|
|
256
|
+
* 3. the sync file once more, since the cycle it opens ends the one of round 2, the one that judges, whose last lines
|
|
262
257
|
* may come after the line that closes it.
|
|
263
|
-
* A watch no
|
|
258
|
+
* A watch that opens no cycle however often it is touched does not look at its sync file, and is said so. A watch no
|
|
259
|
+
* hook started, as when the hooks were installed mid-session, starts now, and so does one the engine
|
|
264
260
|
* stopped, for idleness or to start it again: it is not to blame. One another session starts again meanwhile is asked
|
|
265
261
|
* again from the first round.
|
|
266
262
|
*/
|
|
@@ -273,7 +269,6 @@ async function settle(
|
|
|
273
269
|
): Promise<Settled> {
|
|
274
270
|
const started = performance.now();
|
|
275
271
|
const folder = folderOf(root, gate);
|
|
276
|
-
const log = join(folder, "output.log");
|
|
277
272
|
const sync = join(root, gate.watch.sync);
|
|
278
273
|
if (!existsSync(sync)) {
|
|
279
274
|
const stopped = `The watch cannot be asked for a cycle: its watch.sync, ${gate.watch.sync}, does not exist.`;
|
|
@@ -281,27 +276,23 @@ async function settle(
|
|
|
281
276
|
}
|
|
282
277
|
await restartOnFrozenChange(session, root, gate, folder);
|
|
283
278
|
const turn = files.map((file) => join(root, file)).filter((path) => existsSync(path));
|
|
284
|
-
const rounds = [[
|
|
279
|
+
const rounds = [[...turn, sync], [sync], [sync]];
|
|
285
280
|
let asked: string | undefined;
|
|
286
281
|
let round = 0;
|
|
287
|
-
// The cycles the watch had opened when the round touched its files,
|
|
288
|
-
|
|
282
|
+
// The cycles the watch had opened when the round touched its files, when it last did, and how many times: none
|
|
283
|
+
// before its first touch.
|
|
284
|
+
let touched: { opened: number; at: number; times: number } | undefined;
|
|
289
285
|
for (;;) {
|
|
290
286
|
const state = stateOf(folder);
|
|
291
287
|
if (state === "absent" || state === "idle") startWatch(session, root, gate);
|
|
292
288
|
// Waiting on the watch is using it: it does not stop for idleness under a stop that waits.
|
|
293
289
|
touch(folder);
|
|
294
|
-
// Read once the watch is known stopped, so its last lines are in.
|
|
295
|
-
const printed = readLog(log);
|
|
296
290
|
const ms = performance.now() - started;
|
|
297
291
|
if (state === "stopped") {
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
.slice(-LAST_LINES - 1)
|
|
303
|
-
.join("\n"),
|
|
304
|
-
);
|
|
292
|
+
// Read once the watch is known stopped, so its last lines are in: the ones its supervisor kept, or what the
|
|
293
|
+
// supervisor itself printed when it broke.
|
|
294
|
+
const printed = readText(join(folder, "last")) || readText(join(folder, "output.log"));
|
|
295
|
+
const last = cleaned(root, printed.trimEnd().split("\n").slice(-LAST_LINES).join("\n"));
|
|
305
296
|
return {
|
|
306
297
|
ms,
|
|
307
298
|
stopped: last ? `The watch stopped. Its last lines:\n${last}` : "The watch stopped, with no output.",
|
|
@@ -313,21 +304,26 @@ async function settle(
|
|
|
313
304
|
round = 0;
|
|
314
305
|
touched = undefined;
|
|
315
306
|
}
|
|
316
|
-
const cycles = cyclesOf(
|
|
307
|
+
const cycles = cyclesOf(folder);
|
|
317
308
|
if (state === "running" && touched === undefined) {
|
|
318
309
|
// A round starts once the cycle before it has closed.
|
|
319
310
|
if (!cycles.open) {
|
|
320
311
|
touchAgain(rounds[round] ?? []);
|
|
321
|
-
touched = { opened: cycles.opened, at: Date.now() };
|
|
312
|
+
touched = { opened: cycles.opened, at: Date.now(), times: 1 };
|
|
322
313
|
}
|
|
323
314
|
} else if (touched !== undefined && cycles.opened > touched.opened) {
|
|
324
|
-
|
|
315
|
+
// Written before the count of the cycle that ended it: the cycle read is that one, or a later one.
|
|
316
|
+
if (round === rounds.length - 1) return { ms, cycle: cleaned(root, readText(join(folder, "cycle"))) };
|
|
325
317
|
round++;
|
|
326
318
|
touched = undefined;
|
|
327
319
|
continue;
|
|
328
|
-
} else if (touched !== undefined && Date.now() - touched.at >=
|
|
320
|
+
} else if (touched !== undefined && Date.now() - touched.at >= resyncMs() * 2 ** (touched.times - 1)) {
|
|
321
|
+
if (touched.times === SYNC_TOUCHES) {
|
|
322
|
+
const stopped = `The watch opens no cycle when the date of its watch.sync, ${gate.watch.sync}, changes: give it a file the watch rebuilds for, such as a source file it reads.`;
|
|
323
|
+
return { ms, stopped };
|
|
324
|
+
}
|
|
329
325
|
touchAgain(rounds[round] ?? []);
|
|
330
|
-
touched
|
|
326
|
+
touched = { ...touched, at: Date.now(), times: touched.times + 1 };
|
|
331
327
|
}
|
|
332
328
|
if (ms >= limitMs) return { ms, timedOut: true };
|
|
333
329
|
await sleep(POLL_MS);
|
|
@@ -340,7 +336,7 @@ async function settle(
|
|
|
340
336
|
*/
|
|
341
337
|
function owedFiles(folder: string): { lists: string[]; files: string[] } {
|
|
342
338
|
const lists = readDir(join(folder, "owed")).map((name) => join(folder, "owed", name));
|
|
343
|
-
const files = lists.flatMap((list) =>
|
|
339
|
+
const files = lists.flatMap((list) => readText(list).split("\0").filter(Boolean));
|
|
344
340
|
return { lists, files: [...new Set(files)] };
|
|
345
341
|
}
|
|
346
342
|
|
|
@@ -432,25 +428,62 @@ function stop(folder: string): void {
|
|
|
432
428
|
}
|
|
433
429
|
|
|
434
430
|
/**
|
|
435
|
-
* The supervisor of a watch, which this module runs as when the engine starts one: it runs the watch
|
|
436
|
-
*
|
|
437
|
-
*
|
|
438
|
-
* the
|
|
431
|
+
* The supervisor of a watch, which this module runs as when the engine starts one: it runs the watch and stops it
|
|
432
|
+
* with every process it started once its repo has had no prompt nor stop for the idle delay. Started detached, it
|
|
433
|
+
* leads a process group of its own, so stopping the group stops the watch and the tools under it. It counts the
|
|
434
|
+
* cycles of the watch as it prints them, and keeps only the lines a hook reads: a watch prints thousands of lines per
|
|
435
|
+
* cycle for as long as a session lasts, which no hook could read again at each look.
|
|
439
436
|
*/
|
|
440
|
-
function supervise(
|
|
437
|
+
function supervise(folder: string, idle: number, cycle: { start: string; end: string }, command: string[]): void {
|
|
438
|
+
const [file = "", ...args] = command;
|
|
439
|
+
const opens = new RegExp(cycle.start);
|
|
440
|
+
const closes = new RegExp(cycle.end);
|
|
441
441
|
const watch = spawn(file, args, { stdio: ["ignore", "pipe", "pipe"] });
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
442
|
+
// The start of the watch opens the first cycle, and lines after the one that closes a cycle still belong to it. A
|
|
443
|
+
// line that opens a cycle while one is open belongs to that one: Angular says it rebuilds as it starts to, and
|
|
444
|
+
// again once it has, and counting both would end a cycle before its first line.
|
|
445
|
+
let opened = 0;
|
|
446
|
+
let open = true;
|
|
447
|
+
// The lines of the cycle in progress, less the ones that say it opened, which tell the agent nothing.
|
|
448
|
+
let lines: string[] = [];
|
|
449
|
+
// What a watch that stopped leaves the agent.
|
|
450
|
+
const last: string[] = [];
|
|
451
|
+
const count = () => writeWhole(join(folder, "cycles"), `${opened} ${open ? "open" : "closed"}`);
|
|
452
|
+
const read = (line: string) => {
|
|
453
|
+
last.push(line);
|
|
454
|
+
if (last.length > LAST_LINES) last.shift();
|
|
455
|
+
if (closes.test(line)) {
|
|
456
|
+
if (open) {
|
|
457
|
+
open = false;
|
|
458
|
+
count();
|
|
459
|
+
}
|
|
460
|
+
} else if (!open && opens.test(line)) {
|
|
461
|
+
// Nothing is printed in a cycle any more once the next one has opened: its lines are whole, and written before
|
|
462
|
+
// the count that tells a hook of them.
|
|
463
|
+
writeWhole(join(folder, "cycle"), lines.join("\n"));
|
|
464
|
+
lines = [];
|
|
465
|
+
opened++;
|
|
466
|
+
open = true;
|
|
467
|
+
count();
|
|
468
|
+
}
|
|
469
|
+
if (!opens.test(line)) lines.push(line);
|
|
470
|
+
};
|
|
471
|
+
const end = (code: number) => {
|
|
472
|
+
writeWhole(join(folder, "last"), last.join("\n"));
|
|
473
|
+
process.exit(code);
|
|
474
|
+
};
|
|
475
|
+
count();
|
|
476
|
+
createInterface({ input: watch.stdout }).on("line", read);
|
|
477
|
+
createInterface({ input: watch.stderr }).on("line", read);
|
|
445
478
|
watch.on("error", (error) => {
|
|
446
|
-
|
|
447
|
-
|
|
479
|
+
last.push(error.message);
|
|
480
|
+
end(1);
|
|
448
481
|
});
|
|
449
|
-
watch.on("close", (code) =>
|
|
482
|
+
watch.on("close", (code) => end(code ?? 1));
|
|
450
483
|
setInterval(
|
|
451
484
|
() => {
|
|
452
|
-
if (Date.now() - changedAt(activity) < idle) return;
|
|
453
|
-
writeFileSync(join(
|
|
485
|
+
if (Date.now() - changedAt(join(folder, "activity")) < idle) return;
|
|
486
|
+
writeFileSync(join(folder, "idle"), "");
|
|
454
487
|
process.kill(-process.pid, "SIGTERM");
|
|
455
488
|
},
|
|
456
489
|
Math.min(idle / 4, 10_000),
|
|
@@ -458,6 +491,6 @@ function supervise(activity: string, idle: number, file: string, args: string[])
|
|
|
458
491
|
}
|
|
459
492
|
|
|
460
493
|
if (import.meta.main) {
|
|
461
|
-
const [
|
|
462
|
-
supervise(
|
|
494
|
+
const [folder = "", idle = "", start = "", end = "", ...command] = process.argv.slice(2);
|
|
495
|
+
supervise(folder, Number(idle), { start, end }, command);
|
|
463
496
|
}
|