@tayomi/deterministic-gates 0.7.0 → 0.8.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 +26 -7
- package/catalog/index.html +25 -11
- package/dist/src/core/watch.js +107 -66
- package/dist/src/helpers/angular-errors.js +183 -0
- package/package.json +1 -1
- package/src/core/watch.ts +107 -71
- package/src/helpers/angular-errors.ts +200 -0
- package/dist/src/helpers/template-errors.js +0 -85
- package/src/helpers/template-errors.ts +0 -101
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
9
|
+
import { statSync, watchFile } from "node:fs";
|
|
10
|
+
import { createRequire } from "node:module";
|
|
11
|
+
import { join, relative } from "node:path";
|
|
12
|
+
import { pathToFileURL } from "node:url";
|
|
13
|
+
import { configOf, nothingChecked } from "./tsc.js";
|
|
14
|
+
import { strictErrorLines } from "./type-errors.js";
|
|
15
|
+
/** A code as Angular prints it: its own codes are negative, -99 then their number, as -998001 for NG8001. */
|
|
16
|
+
function codeOf(code) {
|
|
17
|
+
return code < 0 ? `NG${String(-code).slice(2)}` : `TS${code}`;
|
|
18
|
+
}
|
|
19
|
+
/** The errors of the project, each on one line, as the gate compares them. */
|
|
20
|
+
export function angularErrors(root, diagnostics) {
|
|
21
|
+
return strictErrorLines(diagnostics.map(({ file, start = 0, code, messageText }) => {
|
|
22
|
+
const message = typeof messageText === "string" ? messageText : messageText.messageText;
|
|
23
|
+
if (file === undefined)
|
|
24
|
+
return message;
|
|
25
|
+
const line = file.text.slice(0, start).split("\n").length - 1;
|
|
26
|
+
return {
|
|
27
|
+
file: relative(root, file.fileName),
|
|
28
|
+
code: codeOf(code),
|
|
29
|
+
source: file.text.split("\n")[line] ?? "",
|
|
30
|
+
message: message.split("\n")[0] ?? "",
|
|
31
|
+
};
|
|
32
|
+
}));
|
|
33
|
+
}
|
|
34
|
+
/** The arguments that are options of the compiler: not the tsconfig, nor --watch, which are the helper's own. */
|
|
35
|
+
function compilerArgs(args) {
|
|
36
|
+
const own = (arg) => arg === "-p" || arg === "--project";
|
|
37
|
+
return args.filter((arg, at) => arg !== "--watch" && !own(arg) && !own(args[at - 1]));
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The options forced on top of the project's: strict templates, and each option of TypeScript the gate gives, as tsc
|
|
41
|
+
* reads them. One TypeScript does not know is refused, since a misspelt option would otherwise check nothing more and
|
|
42
|
+
* leave the gate green.
|
|
43
|
+
*/
|
|
44
|
+
function forced(ts, args) {
|
|
45
|
+
const { options, errors, fileNames } = ts.parseCommandLine(compilerArgs(args));
|
|
46
|
+
const [error] = errors;
|
|
47
|
+
if (error !== undefined)
|
|
48
|
+
return { refused: ts.flattenDiagnosticMessageText(error.messageText, "\n") };
|
|
49
|
+
const [stray] = fileNames;
|
|
50
|
+
if (stray !== undefined)
|
|
51
|
+
return { refused: `${stray} is not an option of the compiler.` };
|
|
52
|
+
return { options: { strictTemplates: true, ...options } };
|
|
53
|
+
}
|
|
54
|
+
function changedAt(file) {
|
|
55
|
+
return statSync(file, { throwIfNoEntry: false })?.mtimeMs ?? 0;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The Angular compiler of the repo, once it is known to check files with the API this helper calls: otherwise, why
|
|
59
|
+
* not. That API is not a public one of Angular, so a version that changed it refuses the gate, never passes it green.
|
|
60
|
+
*/
|
|
61
|
+
export async function compilerOf(root, args) {
|
|
62
|
+
const nothing = nothingChecked(root, args);
|
|
63
|
+
if (nothing)
|
|
64
|
+
return { refused: nothing };
|
|
65
|
+
const required = createRequire(join(root, "package.json"));
|
|
66
|
+
const given = forced(required("typescript"), args);
|
|
67
|
+
if ("refused" in given)
|
|
68
|
+
return given;
|
|
69
|
+
const ng = await import(__rewriteRelativeImportExtension(pathToFileURL(required.resolve("@angular/compiler-cli")).href));
|
|
70
|
+
const { version } = required("@angular/compiler-cli/package.json");
|
|
71
|
+
const api = ng.NgtscProgram?.prototype;
|
|
72
|
+
const missing = [
|
|
73
|
+
["NgtscProgram", ng.NgtscProgram],
|
|
74
|
+
["NgtscProgram.loadNgStructureAsync", api?.loadNgStructureAsync],
|
|
75
|
+
["NgtscProgram.getTsSyntacticDiagnostics", api?.getTsSyntacticDiagnostics],
|
|
76
|
+
["NgtscProgram.getTsSemanticDiagnostics", api?.getTsSemanticDiagnostics],
|
|
77
|
+
["NgtscProgram.getNgSemanticDiagnostics", api?.getNgSemanticDiagnostics],
|
|
78
|
+
["readConfiguration", ng.readConfiguration],
|
|
79
|
+
["createCompilerHost", ng.createCompilerHost],
|
|
80
|
+
].find(([, found]) => typeof found !== "function")?.[0];
|
|
81
|
+
if (missing !== undefined)
|
|
82
|
+
return {
|
|
83
|
+
refused: `The API of @angular/compiler-cli ${version} is not the one this gate knows: it has no ${missing}. Update deterministic-gates.`,
|
|
84
|
+
};
|
|
85
|
+
// What the check before this one read, by the date it had: a file whose date is the same is given as it was, which
|
|
86
|
+
// is how the compiler knows it need not check it again.
|
|
87
|
+
const sources = new Map();
|
|
88
|
+
const resources = new Map();
|
|
89
|
+
let last;
|
|
90
|
+
return {
|
|
91
|
+
compiler: {
|
|
92
|
+
async diagnostics() {
|
|
93
|
+
const config = ng.readConfiguration(configOf(root, args));
|
|
94
|
+
const options = { ...config.options, ...given.options };
|
|
95
|
+
// A program checked with other options is no help to this one.
|
|
96
|
+
if (last?.options !== JSON.stringify(options)) {
|
|
97
|
+
last = undefined;
|
|
98
|
+
sources.clear();
|
|
99
|
+
resources.clear();
|
|
100
|
+
}
|
|
101
|
+
const base = ng.createCompilerHost({ options });
|
|
102
|
+
const host = {
|
|
103
|
+
...base,
|
|
104
|
+
getSourceFile(file, ...rest) {
|
|
105
|
+
const at = changedAt(file);
|
|
106
|
+
const kept = sources.get(file);
|
|
107
|
+
if (kept?.at === at)
|
|
108
|
+
return kept.source;
|
|
109
|
+
const source = base.getSourceFile(file, ...rest);
|
|
110
|
+
if (source === undefined)
|
|
111
|
+
sources.delete(file);
|
|
112
|
+
else
|
|
113
|
+
sources.set(file, { at, source });
|
|
114
|
+
return source;
|
|
115
|
+
},
|
|
116
|
+
// A template or a style, which the compiler reads as text.
|
|
117
|
+
readFile(file) {
|
|
118
|
+
if (!/\.(ts|json)$/.test(file) && !resources.has(file))
|
|
119
|
+
resources.set(file, changedAt(file));
|
|
120
|
+
return base.readFile(file);
|
|
121
|
+
},
|
|
122
|
+
getModifiedResourceFiles() {
|
|
123
|
+
const modified = new Set();
|
|
124
|
+
for (const [file, at] of resources) {
|
|
125
|
+
const now = changedAt(file);
|
|
126
|
+
if (now === at)
|
|
127
|
+
continue;
|
|
128
|
+
modified.add(file);
|
|
129
|
+
resources.set(file, now);
|
|
130
|
+
}
|
|
131
|
+
return modified;
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
const program = new ng.NgtscProgram(config.rootNames, options, host, last?.program);
|
|
135
|
+
await program.loadNgStructureAsync();
|
|
136
|
+
last = { program, options: JSON.stringify(options) };
|
|
137
|
+
return [
|
|
138
|
+
...config.errors,
|
|
139
|
+
...program.getTsSyntacticDiagnostics(),
|
|
140
|
+
...program.getTsSemanticDiagnostics(),
|
|
141
|
+
...program.getNgSemanticDiagnostics(),
|
|
142
|
+
];
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
/** Checks now, then each time the date of the tsconfig changes, saying when a check starts and when it is done. */
|
|
148
|
+
async function watch(root, args, compiler) {
|
|
149
|
+
let asked = false;
|
|
150
|
+
let checking = false;
|
|
151
|
+
const check = async () => {
|
|
152
|
+
asked = true;
|
|
153
|
+
if (checking)
|
|
154
|
+
return;
|
|
155
|
+
checking = true;
|
|
156
|
+
// Asked again while it checked: the files may have changed since it read them.
|
|
157
|
+
while (asked) {
|
|
158
|
+
asked = false;
|
|
159
|
+
console.log("Type check started");
|
|
160
|
+
console.log([...angularErrors(root, await compiler.diagnostics()), "Type check done"].join("\n"));
|
|
161
|
+
}
|
|
162
|
+
checking = false;
|
|
163
|
+
};
|
|
164
|
+
// Looked at rather than watched: a date read ten times a second misses no change, whatever the file system says.
|
|
165
|
+
watchFile(configOf(root, args), { interval: 100 }, (now, before) => {
|
|
166
|
+
if (now.mtimeMs !== before.mtimeMs)
|
|
167
|
+
void check();
|
|
168
|
+
});
|
|
169
|
+
await check();
|
|
170
|
+
}
|
|
171
|
+
async function main(root, args) {
|
|
172
|
+
const found = await compilerOf(root, args);
|
|
173
|
+
if ("refused" in found) {
|
|
174
|
+
console.log(found.refused);
|
|
175
|
+
process.exit(1);
|
|
176
|
+
}
|
|
177
|
+
if (args.includes("--watch"))
|
|
178
|
+
await watch(root, args, found.compiler);
|
|
179
|
+
else
|
|
180
|
+
console.log(angularErrors(root, await found.compiler.diagnostics()).join("\n"));
|
|
181
|
+
}
|
|
182
|
+
if (import.meta.main)
|
|
183
|
+
await main(process.cwd(), process.argv.slice(2));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tayomi/deterministic-gates",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.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,17 @@ 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 sync file
|
|
252
|
+
* 1. the sync file alone. The cycle that follows proves the watch now looks for changes, whoever made it open: a
|
|
253
|
+
* watch may look for them only once its first cycle is over, as Angular and tsc do, and a touch made before is
|
|
254
|
+
* lost. Touched with the sync file in this round, the files of the turn could be lost with it, and a cycle the
|
|
255
|
+
* watch then opens for a change of someone else, another gate or another session, would answer for them;
|
|
258
256
|
* 2. the files of the turn, so that a change it missed reaches it, then the sync file;
|
|
259
257
|
* 3. the sync file, once that cycle has closed: the cycle of round 2 may have been on its way before the touch, and
|
|
260
258
|
* this one opens after every change the watch then had waiting;
|
|
261
259
|
* 4. the sync file once more, since the cycle it opens ends the one of round 3, the one that judges, whose last lines
|
|
262
260
|
* may come after the line that closes it.
|
|
263
|
-
* A watch no
|
|
261
|
+
* 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
|
|
262
|
+
* hook started, as when the hooks were installed mid-session, starts now, and so does one the engine
|
|
264
263
|
* stopped, for idleness or to start it again: it is not to blame. One another session starts again meanwhile is asked
|
|
265
264
|
* again from the first round.
|
|
266
265
|
*/
|
|
@@ -273,7 +272,6 @@ async function settle(
|
|
|
273
272
|
): Promise<Settled> {
|
|
274
273
|
const started = performance.now();
|
|
275
274
|
const folder = folderOf(root, gate);
|
|
276
|
-
const log = join(folder, "output.log");
|
|
277
275
|
const sync = join(root, gate.watch.sync);
|
|
278
276
|
if (!existsSync(sync)) {
|
|
279
277
|
const stopped = `The watch cannot be asked for a cycle: its watch.sync, ${gate.watch.sync}, does not exist.`;
|
|
@@ -284,24 +282,20 @@ async function settle(
|
|
|
284
282
|
const rounds = [[sync], ...(turn.length ? [[...turn, sync]] : []), [sync], [sync]];
|
|
285
283
|
let asked: string | undefined;
|
|
286
284
|
let round = 0;
|
|
287
|
-
// The cycles the watch had opened when the round touched its files,
|
|
288
|
-
|
|
285
|
+
// The cycles the watch had opened when the round touched its files, when it last did, and how many times: none
|
|
286
|
+
// before its first touch.
|
|
287
|
+
let touched: { opened: number; at: number; times: number } | undefined;
|
|
289
288
|
for (;;) {
|
|
290
289
|
const state = stateOf(folder);
|
|
291
290
|
if (state === "absent" || state === "idle") startWatch(session, root, gate);
|
|
292
291
|
// Waiting on the watch is using it: it does not stop for idleness under a stop that waits.
|
|
293
292
|
touch(folder);
|
|
294
|
-
// Read once the watch is known stopped, so its last lines are in.
|
|
295
|
-
const printed = readLog(log);
|
|
296
293
|
const ms = performance.now() - started;
|
|
297
294
|
if (state === "stopped") {
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
.slice(-LAST_LINES - 1)
|
|
303
|
-
.join("\n"),
|
|
304
|
-
);
|
|
295
|
+
// Read once the watch is known stopped, so its last lines are in: the ones its supervisor kept, or what the
|
|
296
|
+
// supervisor itself printed when it broke.
|
|
297
|
+
const printed = readText(join(folder, "last")) || readText(join(folder, "output.log"));
|
|
298
|
+
const last = cleaned(root, printed.trimEnd().split("\n").slice(-LAST_LINES).join("\n"));
|
|
305
299
|
return {
|
|
306
300
|
ms,
|
|
307
301
|
stopped: last ? `The watch stopped. Its last lines:\n${last}` : "The watch stopped, with no output.",
|
|
@@ -313,21 +307,26 @@ async function settle(
|
|
|
313
307
|
round = 0;
|
|
314
308
|
touched = undefined;
|
|
315
309
|
}
|
|
316
|
-
const cycles = cyclesOf(
|
|
310
|
+
const cycles = cyclesOf(folder);
|
|
317
311
|
if (state === "running" && touched === undefined) {
|
|
318
312
|
// A round starts once the cycle before it has closed.
|
|
319
313
|
if (!cycles.open) {
|
|
320
314
|
touchAgain(rounds[round] ?? []);
|
|
321
|
-
touched = { opened: cycles.opened, at: Date.now() };
|
|
315
|
+
touched = { opened: cycles.opened, at: Date.now(), times: 1 };
|
|
322
316
|
}
|
|
323
317
|
} else if (touched !== undefined && cycles.opened > touched.opened) {
|
|
324
|
-
|
|
318
|
+
// Written before the count of the cycle that ended it: the cycle read is that one, or a later one.
|
|
319
|
+
if (round === rounds.length - 1) return { ms, cycle: cleaned(root, readText(join(folder, "cycle"))) };
|
|
325
320
|
round++;
|
|
326
321
|
touched = undefined;
|
|
327
322
|
continue;
|
|
328
|
-
} else if (touched !== undefined && Date.now() - touched.at >=
|
|
323
|
+
} else if (touched !== undefined && Date.now() - touched.at >= resyncMs() * 2 ** (touched.times - 1)) {
|
|
324
|
+
if (touched.times === SYNC_TOUCHES) {
|
|
325
|
+
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.`;
|
|
326
|
+
return { ms, stopped };
|
|
327
|
+
}
|
|
329
328
|
touchAgain(rounds[round] ?? []);
|
|
330
|
-
touched
|
|
329
|
+
touched = { ...touched, at: Date.now(), times: touched.times + 1 };
|
|
331
330
|
}
|
|
332
331
|
if (ms >= limitMs) return { ms, timedOut: true };
|
|
333
332
|
await sleep(POLL_MS);
|
|
@@ -340,7 +339,7 @@ async function settle(
|
|
|
340
339
|
*/
|
|
341
340
|
function owedFiles(folder: string): { lists: string[]; files: string[] } {
|
|
342
341
|
const lists = readDir(join(folder, "owed")).map((name) => join(folder, "owed", name));
|
|
343
|
-
const files = lists.flatMap((list) =>
|
|
342
|
+
const files = lists.flatMap((list) => readText(list).split("\0").filter(Boolean));
|
|
344
343
|
return { lists, files: [...new Set(files)] };
|
|
345
344
|
}
|
|
346
345
|
|
|
@@ -432,25 +431,62 @@ function stop(folder: string): void {
|
|
|
432
431
|
}
|
|
433
432
|
|
|
434
433
|
/**
|
|
435
|
-
* The supervisor of a watch, which this module runs as when the engine starts one: it runs the watch
|
|
436
|
-
*
|
|
437
|
-
*
|
|
438
|
-
* the
|
|
434
|
+
* The supervisor of a watch, which this module runs as when the engine starts one: it runs the watch and stops it
|
|
435
|
+
* with every process it started once its repo has had no prompt nor stop for the idle delay. Started detached, it
|
|
436
|
+
* leads a process group of its own, so stopping the group stops the watch and the tools under it. It counts the
|
|
437
|
+
* cycles of the watch as it prints them, and keeps only the lines a hook reads: a watch prints thousands of lines per
|
|
438
|
+
* cycle for as long as a session lasts, which no hook could read again at each look.
|
|
439
439
|
*/
|
|
440
|
-
function supervise(
|
|
440
|
+
function supervise(folder: string, idle: number, cycle: { start: string; end: string }, command: string[]): void {
|
|
441
|
+
const [file = "", ...args] = command;
|
|
442
|
+
const opens = new RegExp(cycle.start);
|
|
443
|
+
const closes = new RegExp(cycle.end);
|
|
441
444
|
const watch = spawn(file, args, { stdio: ["ignore", "pipe", "pipe"] });
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
+
// The start of the watch opens the first cycle, and lines after the one that closes a cycle still belong to it. A
|
|
446
|
+
// line that opens a cycle while one is open belongs to that one: Angular says it rebuilds as it starts to, and
|
|
447
|
+
// again once it has, and counting both would end a cycle before its first line.
|
|
448
|
+
let opened = 0;
|
|
449
|
+
let open = true;
|
|
450
|
+
// The lines of the cycle in progress, less the ones that say it opened, which tell the agent nothing.
|
|
451
|
+
let lines: string[] = [];
|
|
452
|
+
// What a watch that stopped leaves the agent.
|
|
453
|
+
const last: string[] = [];
|
|
454
|
+
const count = () => writeWhole(join(folder, "cycles"), `${opened} ${open ? "open" : "closed"}`);
|
|
455
|
+
const read = (line: string) => {
|
|
456
|
+
last.push(line);
|
|
457
|
+
if (last.length > LAST_LINES) last.shift();
|
|
458
|
+
if (closes.test(line)) {
|
|
459
|
+
if (open) {
|
|
460
|
+
open = false;
|
|
461
|
+
count();
|
|
462
|
+
}
|
|
463
|
+
} else if (!open && opens.test(line)) {
|
|
464
|
+
// Nothing is printed in a cycle any more once the next one has opened: its lines are whole, and written before
|
|
465
|
+
// the count that tells a hook of them.
|
|
466
|
+
writeWhole(join(folder, "cycle"), lines.join("\n"));
|
|
467
|
+
lines = [];
|
|
468
|
+
opened++;
|
|
469
|
+
open = true;
|
|
470
|
+
count();
|
|
471
|
+
}
|
|
472
|
+
if (!opens.test(line)) lines.push(line);
|
|
473
|
+
};
|
|
474
|
+
const end = (code: number) => {
|
|
475
|
+
writeWhole(join(folder, "last"), last.join("\n"));
|
|
476
|
+
process.exit(code);
|
|
477
|
+
};
|
|
478
|
+
count();
|
|
479
|
+
createInterface({ input: watch.stdout }).on("line", read);
|
|
480
|
+
createInterface({ input: watch.stderr }).on("line", read);
|
|
445
481
|
watch.on("error", (error) => {
|
|
446
|
-
|
|
447
|
-
|
|
482
|
+
last.push(error.message);
|
|
483
|
+
end(1);
|
|
448
484
|
});
|
|
449
|
-
watch.on("close", (code) =>
|
|
485
|
+
watch.on("close", (code) => end(code ?? 1));
|
|
450
486
|
setInterval(
|
|
451
487
|
() => {
|
|
452
|
-
if (Date.now() - changedAt(activity) < idle) return;
|
|
453
|
-
writeFileSync(join(
|
|
488
|
+
if (Date.now() - changedAt(join(folder, "activity")) < idle) return;
|
|
489
|
+
writeFileSync(join(folder, "idle"), "");
|
|
454
490
|
process.kill(-process.pid, "SIGTERM");
|
|
455
491
|
},
|
|
456
492
|
Math.min(idle / 4, 10_000),
|
|
@@ -458,6 +494,6 @@ function supervise(activity: string, idle: number, file: string, args: string[])
|
|
|
458
494
|
}
|
|
459
495
|
|
|
460
496
|
if (import.meta.main) {
|
|
461
|
-
const [
|
|
462
|
-
supervise(
|
|
497
|
+
const [folder = "", idle = "", start = "", end = "", ...command] = process.argv.slice(2);
|
|
498
|
+
supervise(folder, Number(idle), { start, end }, command);
|
|
463
499
|
}
|