@tayomi/deterministic-gates 0.8.2 → 0.10.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.
- package/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/.codex-plugin/plugin.json +1 -1
- package/README.md +1 -1
- package/catalog/angular.json +2 -1
- package/catalog/index.html +29 -4
- package/catalog/typescript.json +14 -3
- package/dist/src/cli.js +8 -5
- package/dist/src/core/check.js +43 -25
- package/dist/src/core/config.js +76 -5
- package/dist/src/core/head.js +30 -0
- package/dist/src/core/report.js +119 -21
- package/dist/src/core/run.js +40 -11
- package/dist/src/core/snapshot.js +34 -0
- package/dist/src/core/turn-state.js +28 -8
- package/dist/src/core/watch.js +41 -16
- package/dist/src/helpers/bulk-suppressions.js +74 -0
- package/dist/src/helpers/new-clones.js +2 -2
- package/dist/src/helpers/new-suppressions.js +29 -16
- package/dist/src/hook/hook.js +14 -6
- package/package.json +1 -1
- package/src/cli.ts +8 -5
- package/src/core/check.ts +70 -24
- package/src/core/config.ts +107 -3
- package/src/core/head.ts +45 -0
- package/src/core/report.ts +139 -21
- package/src/core/run.ts +54 -9
- package/src/core/snapshot.ts +42 -0
- package/src/core/turn-state.ts +37 -11
- package/src/core/watch.ts +40 -13
- package/src/helpers/bulk-suppressions.ts +95 -0
- package/src/helpers/new-clones.ts +2 -2
- package/src/helpers/new-suppressions.ts +27 -16
- package/src/hook/hook.ts +23 -6
package/dist/src/core/report.js
CHANGED
|
@@ -3,19 +3,34 @@ import { WAIVE_TAG } from "./waive-line.js";
|
|
|
3
3
|
const LABEL = "● gates";
|
|
4
4
|
const SHOWN_PASSES = 6;
|
|
5
5
|
const REPORT_CHARS = 4000;
|
|
6
|
-
|
|
6
|
+
// The share of a cut report kept from its start.
|
|
7
|
+
const HEAD_SHARE = 0.25;
|
|
7
8
|
const TONES = { pass: "1;32", fail: "1;31", late: "1;33", warn: "1;38;5;208", dim: "2" };
|
|
8
9
|
// The warning sign as text, never as the emoji some terminals draw without the variation selector.
|
|
9
10
|
const WARNING = "\u26A0\uFE0E";
|
|
10
11
|
function paint(tone, text) {
|
|
11
12
|
return `\x1b[${TONES[tone]}m${text}\x1b[0m`;
|
|
12
13
|
}
|
|
13
|
-
/**
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Keeps the start and the end of a long report, in the characters given: tools put the problem at one or the other.
|
|
16
|
+
* Each cut moves to the end of a line when that keeps at least half of its side, so a list keeps whole entries, and
|
|
17
|
+
* stays inside a line longer than that, which would otherwise go whole.
|
|
18
|
+
*/
|
|
19
|
+
export function excerpt(report, max = REPORT_CHARS) {
|
|
20
|
+
if (report.length <= max)
|
|
16
21
|
return report;
|
|
17
|
-
const
|
|
18
|
-
|
|
22
|
+
const head = Math.floor(max * HEAD_SHARE);
|
|
23
|
+
const tail = max - head;
|
|
24
|
+
const before = report.lastIndexOf("\n", head - 1);
|
|
25
|
+
const after = report.indexOf("\n", report.length - tail);
|
|
26
|
+
const wholeHead = before >= head / 2;
|
|
27
|
+
const wholeTail = after !== -1 && after - (report.length - tail) <= tail / 2;
|
|
28
|
+
const end = wholeHead ? before : head;
|
|
29
|
+
const start = wholeTail ? after + 1 : report.length - tail;
|
|
30
|
+
const cut = wholeHead && wholeTail
|
|
31
|
+
? `${report.slice(end + 1, start - 1).split("\n").length} lines`
|
|
32
|
+
: `${start - end} characters`;
|
|
33
|
+
return `${report.slice(0, end)}\n[... ${cut} cut ...]\n${report.slice(start)}`;
|
|
19
34
|
}
|
|
20
35
|
export function duration(ms) {
|
|
21
36
|
return ms < 1000 ? `${Math.round(ms)} ms` : `${(ms / 1000).toFixed(1)} s`;
|
|
@@ -90,14 +105,61 @@ export function broken(reason) {
|
|
|
90
105
|
}
|
|
91
106
|
// The cheapest green is to silence the rule, and nothing in the engine judges a report. So the agent is told the one
|
|
92
107
|
// way out that is not a cheat: waive the gate, with a reason the owner reads and judges.
|
|
93
|
-
const
|
|
94
|
-
"Fix the code, never the check: a silenced rule (noqa, eslint-disable, nolint), a weakened gate or an edited",
|
|
108
|
+
const SEND_BACK = [
|
|
109
|
+
"Gates fail on what this turn changed. Fix every problem before you hand back.\n\nFix the code, never the check: a silenced rule (noqa, eslint-disable, nolint), a weakened gate or an edited",
|
|
95
110
|
".gates.json does not pass this turn. If you can fix a problem, fix it. Leave a gate red only when it cannot be",
|
|
96
111
|
"fixed (the report is wrong, or fixing it would loop until the last send-back), when fixing it goes against what",
|
|
97
|
-
"the owner asked, or when the owner has to settle a question first.
|
|
98
|
-
"blank line, one line per gate, its reason in one line:",
|
|
112
|
+
"the owner asked, or when the owner has to settle a question first.",
|
|
99
113
|
].join(" ");
|
|
100
114
|
const WAIVE_SYNTAX = `[${WAIVE_TAG}:<gate>]: waive "<reason>"`;
|
|
115
|
+
// Said whatever the repo says in place of the rest: without it, an agent could only loop on a gate it cannot fix.
|
|
116
|
+
const WAIVE = [
|
|
117
|
+
"To leave a gate red, waive it in your final message, after a blank line, one line per gate, its reason in one line:",
|
|
118
|
+
WAIVE_SYNTAX,
|
|
119
|
+
].join("\n");
|
|
120
|
+
// A gate that broke found no problem to fix: the agent looks for what stops it from running, in the code or around it.
|
|
121
|
+
const BROKEN_GATE = "This gate broke instead of reporting: repair what stops it from running (a missing dependency, a broken configuration, a file it cannot read), then fix what it reports.";
|
|
122
|
+
// What a session already told how to act reads at its next send-backs: what it has in its context is not said again.
|
|
123
|
+
const BRIEF = `Gates fail again: act as the first send-back of this session told you, or waive with ${WAIVE_SYNTAX}`;
|
|
124
|
+
/** What the engine tells the agent, with the prompt of the repo after it, or in its place. */
|
|
125
|
+
function worded(own, prompt, between) {
|
|
126
|
+
if (prompt === undefined)
|
|
127
|
+
return own;
|
|
128
|
+
return prompt.replace ? prompt.text : `${own}${between}${prompt.text}`;
|
|
129
|
+
}
|
|
130
|
+
// The report less the lines the gate omits, or whole when it would omit them all: a red gate always says something.
|
|
131
|
+
function sent(report, omitLines) {
|
|
132
|
+
if (omitLines === undefined)
|
|
133
|
+
return report;
|
|
134
|
+
const omitted = new RegExp(omitLines);
|
|
135
|
+
const kept = report.split("\n").filter((line) => !omitted.test(line));
|
|
136
|
+
return kept.some((line) => line.trim()) ? kept.join("\n") : report;
|
|
137
|
+
}
|
|
138
|
+
// A helper of the engine counts apart the errors of one source line, with `(2)` before the tab that ends their key,
|
|
139
|
+
// so that one more of them is new. The agent reads each of them once, with how many times it stands.
|
|
140
|
+
function saidOnce(report) {
|
|
141
|
+
const lines = report.split("\n").map((line) => line.replace(/ \(\d+\)\t/, "\t"));
|
|
142
|
+
const times = new Map();
|
|
143
|
+
for (const line of lines)
|
|
144
|
+
if (line.includes("\t"))
|
|
145
|
+
times.set(line, (times.get(line) ?? 0) + 1);
|
|
146
|
+
return lines
|
|
147
|
+
.filter((line, at) => !times.has(line) || lines.indexOf(line) === at)
|
|
148
|
+
.map((line) => ((times.get(line) ?? 1) > 1 ? `${line} (×${times.get(line)})` : line))
|
|
149
|
+
.join("\n");
|
|
150
|
+
}
|
|
151
|
+
// The characters each report gets of a total: what it needs when all fit, else the short ones whole and the rest of
|
|
152
|
+
// the total in equal shares for the long ones.
|
|
153
|
+
function shares(needs, total) {
|
|
154
|
+
const given = [...needs];
|
|
155
|
+
let left = total;
|
|
156
|
+
const shortestFirst = needs.map((_, at) => at).sort((a, b) => (needs[a] ?? 0) - (needs[b] ?? 0));
|
|
157
|
+
shortestFirst.forEach((at, done) => {
|
|
158
|
+
given[at] = Math.min(needs[at] ?? 0, Math.floor(left / (needs.length - done)));
|
|
159
|
+
left -= given[at];
|
|
160
|
+
});
|
|
161
|
+
return given;
|
|
162
|
+
}
|
|
101
163
|
/** What the owner sees when the gates of the repo miss tools on this machine, as the agent is asked to install them. */
|
|
102
164
|
export function toolsMissing(missing) {
|
|
103
165
|
const gates = missing.length === 1 ? "1 gate is off" : `${missing.length} gates are off`;
|
|
@@ -105,23 +167,59 @@ export function toolsMissing(missing) {
|
|
|
105
167
|
return [`${paint("fail", LABEL)} ${gates}, a tool is missing: the agent is asked to install it`, ...lines].join("\n");
|
|
106
168
|
}
|
|
107
169
|
/** What the agent is told when the gates of the repo miss tools: install them, or the gates that need them stay off. */
|
|
108
|
-
export function toolsToInstall(missing) {
|
|
170
|
+
export function toolsToInstall(missing, prompt) {
|
|
171
|
+
const install = [
|
|
172
|
+
"Install these tools first. If they cannot be installed, or the developer refuses, say so and go on with the",
|
|
173
|
+
"prompt: the gates that need them stay off until they are installed, and every other gate still runs.",
|
|
174
|
+
].join("\n");
|
|
109
175
|
return [
|
|
110
176
|
"Some gates of this repo cannot run on this machine, for a missing tool:",
|
|
111
177
|
...missing.map((tool) => `- ${tool}`),
|
|
112
|
-
|
|
113
|
-
"prompt: the gates that need them stay off until they are installed, and every other gate still runs.",
|
|
178
|
+
worded(install, prompt, "\n"),
|
|
114
179
|
].join("\n");
|
|
115
180
|
}
|
|
116
|
-
/**
|
|
117
|
-
|
|
118
|
-
|
|
181
|
+
/**
|
|
182
|
+
* The report of each failed gate, under its name, how to act on one the turn broke, and the prompt of the repo for it:
|
|
183
|
+
* each line a helper counts apart said once, less the lines the gate omits, in the characters it has, and in its share of a total when the reports have one.
|
|
184
|
+
*/
|
|
185
|
+
export function reports({ outcomes, wording = {} }) {
|
|
186
|
+
const { gates = [], prompts, maxTotalReportChars = Infinity } = wording;
|
|
187
|
+
const red = outcomes.filter(failed).map(({ name, output, broken }) => {
|
|
188
|
+
const gate = gates.find((known) => known.name === name);
|
|
189
|
+
const report = sent(saidOnce(output), gate?.omitLines);
|
|
190
|
+
const told = [
|
|
191
|
+
...(broken ? [worded(BROKEN_GATE, prompts?.brokenGate, "\n")] : []),
|
|
192
|
+
...(gate?.prompt ? [gate.prompt.text] : []),
|
|
193
|
+
];
|
|
194
|
+
return { name, told, report, needs: Math.min(report.length, gate?.maxReportChars ?? REPORT_CHARS) };
|
|
195
|
+
});
|
|
196
|
+
const given = shares(red.map(({ needs }) => needs), maxTotalReportChars);
|
|
197
|
+
return red.map(({ name, told, report }, at) => [`## ${name}`, ...told, excerpt(report, given[at])].join("\n"));
|
|
119
198
|
}
|
|
120
199
|
/**
|
|
121
|
-
* What the agent gets when it is sent back: how to act on a failure,
|
|
122
|
-
* check, each failed report.
|
|
200
|
+
* What the agent gets when it is sent back: how to act on a failure, in the words of the engine or of the repo, how
|
|
201
|
+
* to waive a gate, or one line for both once the session was told, what the stop has to tell it, the note of the check, each failed report.
|
|
123
202
|
*/
|
|
124
|
-
export function failures(
|
|
125
|
-
const head =
|
|
126
|
-
return [...head, ...told, ...(note ? [note] : []), ...reports(
|
|
203
|
+
export function failures(turn, told = [], brief = false) {
|
|
204
|
+
const head = brief ? [BRIEF] : [worded(SEND_BACK, turn.wording?.prompts?.sendBack, "\n\n"), WAIVE];
|
|
205
|
+
return [...head, ...told, ...(turn.note ? [turn.note] : []), ...reports(turn)].join("\n\n");
|
|
206
|
+
}
|
|
207
|
+
function gateResult({ name, ok, ms, output, timedOut, broken }) {
|
|
208
|
+
const durationMs = Math.round(ms);
|
|
209
|
+
if (ok)
|
|
210
|
+
return { name, status: "passed", durationMs };
|
|
211
|
+
if (timedOut)
|
|
212
|
+
return { name, status: "timed-out", durationMs };
|
|
213
|
+
return { name, status: broken ? "broken" : "failed", durationMs, report: output };
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* A check for a tool to read, such as a CI or a script, where the summary is for its owner and the send-back for the
|
|
217
|
+
* agent: whether it passed, each gate with its status and its whole report, and the notes of the check.
|
|
218
|
+
*/
|
|
219
|
+
export function results({ outcomes, skipped, ms, note }) {
|
|
220
|
+
const gates = [
|
|
221
|
+
...outcomes.map(gateResult),
|
|
222
|
+
...skipped.map((name) => ({ name, status: "skipped" })),
|
|
223
|
+
];
|
|
224
|
+
return { ok: !outcomes.some(failed), durationMs: Math.round(ms), gates, notes: note ? note.split("\n") : [] };
|
|
127
225
|
}
|
package/dist/src/core/run.js
CHANGED
|
@@ -4,7 +4,7 @@ import { delimiter, isAbsolute, join, resolve, sep } from "node:path";
|
|
|
4
4
|
import { stripVTControlCharacters } from "node:util";
|
|
5
5
|
import { FILES, isCompared, TURN } from "./config.js";
|
|
6
6
|
import { isRecord, messageOf, readJsonFile } from "./json.js";
|
|
7
|
-
/** A gate that judged the turn and found a problem: the only
|
|
7
|
+
/** A gate that judged the turn and found a problem, or that broke: the only kinds that send the agent back. */
|
|
8
8
|
export function failed(outcome) {
|
|
9
9
|
return !outcome.ok && !outcome.timedOut;
|
|
10
10
|
}
|
|
@@ -25,6 +25,12 @@ function repoNode(root) {
|
|
|
25
25
|
const pinned = join(root, "node_modules", ".bin", "node");
|
|
26
26
|
return executable(pinned) ? pinned : process.execPath;
|
|
27
27
|
}
|
|
28
|
+
/** Set for a gate whose prompt the repo replaces: its command leaves out the line it would tell the agent itself. */
|
|
29
|
+
const PROMPT_REPLACED = "GATES_PROMPT_REPLACED";
|
|
30
|
+
/** The report of an engine helper: its own line for the agent, unless the repo says another instead, then its list. */
|
|
31
|
+
export function headed(own, lines, env = process.env) {
|
|
32
|
+
return [...(env[PROMPT_REPLACED] ? [] : [own]), ...lines].join("\n");
|
|
33
|
+
}
|
|
28
34
|
const MAX_OUTPUT_MB = 16;
|
|
29
35
|
const ENGINE_PREFIX = "{engine}/";
|
|
30
36
|
/** A script of this engine: its .ts source, or the .js it is built to when the engine runs from its npm package. */
|
|
@@ -162,20 +168,28 @@ async function execute(root, gate, args) {
|
|
|
162
168
|
command = commandOf(root, name);
|
|
163
169
|
}
|
|
164
170
|
catch (error) {
|
|
165
|
-
return {
|
|
171
|
+
return {
|
|
172
|
+
ok: false,
|
|
173
|
+
output: messageOf(error),
|
|
174
|
+
ms: performance.now() - started,
|
|
175
|
+
timedOut: false,
|
|
176
|
+
cannotStart: true,
|
|
177
|
+
};
|
|
166
178
|
}
|
|
167
179
|
return new Promise((resolve) => {
|
|
168
180
|
execFile(command.file, [...command.args, ...args], {
|
|
169
181
|
cwd: root,
|
|
170
182
|
timeout: gate.timeoutSeconds * 1000,
|
|
171
183
|
maxBuffer: MAX_OUTPUT_MB * 1024 * 1024,
|
|
172
|
-
env: toolEnv(root),
|
|
184
|
+
env: { ...toolEnv(root), ...(gate.prompt?.replace ? { [PROMPT_REPLACED]: "1" } : {}) },
|
|
173
185
|
}, (error, stdout, stderr) => {
|
|
174
186
|
resolve({
|
|
175
187
|
ok: !error,
|
|
176
188
|
output: outputOf(root, gate, command, error, `${stdout}${stderr}`),
|
|
177
189
|
ms: performance.now() - started,
|
|
178
190
|
timedOut: error?.killed === true && error.code !== "ERR_CHILD_PROCESS_STDIO_MAXBUFFER",
|
|
191
|
+
cannotStart: error?.code === "ENOENT",
|
|
192
|
+
...(typeof error?.code === "number" ? { exitCode: error.code } : {}),
|
|
179
193
|
});
|
|
180
194
|
});
|
|
181
195
|
});
|
|
@@ -205,26 +219,41 @@ export async function wholeProject(root, gate, ms = gate.timeoutSeconds * 1000)
|
|
|
205
219
|
const { ok, output, timedOut } = await execute(root, { ...gate, timeoutSeconds }, gate.run.slice(1));
|
|
206
220
|
return timedOut ? { ok: false } : { ok, output };
|
|
207
221
|
}
|
|
222
|
+
/** The file a line of a compared gate names, by the group of its key named file. */
|
|
223
|
+
function fileOf(gate, line) {
|
|
224
|
+
return gate.lineKey === undefined ? undefined : new RegExp(gate.lineKey).exec(line)?.groups?.file;
|
|
225
|
+
}
|
|
226
|
+
/** Whether the lines of a compared gate name their file, so that they can be told from the ones of other files. */
|
|
227
|
+
export function namesFiles(gate) {
|
|
228
|
+
return /\(\?<file>/.test(gate.lineKey ?? "");
|
|
229
|
+
}
|
|
208
230
|
/**
|
|
209
|
-
* Runs a gate on the turn. A compared gate fails on the lines of output the turn added, and on any exit
|
|
210
|
-
* the same crash at the start and at the end of a turn prints no new line.
|
|
231
|
+
* Runs a gate on the turn. A compared gate fails on the lines of output the turn added, and is broken on any exit
|
|
232
|
+
* code but 0: the same crash at the start and at the end of a turn prints no new line. Another gate is broken on the
|
|
233
|
+
* exit codes it names as such. A gate out of time is neither.
|
|
211
234
|
*/
|
|
212
|
-
export async function check(root, gate, files, turn, baseline) {
|
|
213
|
-
const
|
|
235
|
+
export async function check(root, gate, files, turn, baseline, elsewhere = false) {
|
|
236
|
+
const executed = await execute(root, gate, argumentsOf(gate, files, turn));
|
|
237
|
+
const { ok, output, ms, timedOut, cannotStart, exitCode = 0 } = executed;
|
|
214
238
|
const ran = { name: gate.name, ms, report: ok ? output : undefined };
|
|
215
239
|
if (timedOut)
|
|
216
240
|
return { ...ran, ok, output, timedOut };
|
|
241
|
+
// A compared gate exits with 0 when it finds problems: any other code is its command that broke.
|
|
242
|
+
const broke = cannotStart || isCompared(gate) || gate.brokenExitCodes?.includes(exitCode);
|
|
243
|
+
if (!ok && broke)
|
|
244
|
+
return { ...ran, ok, output, broken: true };
|
|
217
245
|
if (!isCompared(gate) || !ok)
|
|
218
246
|
return { ...ran, ok, output };
|
|
219
|
-
return { ...ran, ...addedLines(gate, output, baseline, files) };
|
|
247
|
+
return { ...ran, ...addedLines(gate, output, baseline, files, elsewhere) };
|
|
220
248
|
}
|
|
221
249
|
/**
|
|
222
250
|
* Judges the output of a compared gate that ran clean: it fails on the lines the turn added to the report of the turn
|
|
223
|
-
* start. Without that report, nothing counts but the exit code.
|
|
251
|
+
* start. Without that report, nothing counts but the exit code. With a report of code HEAD has left since, elsewhere,
|
|
252
|
+
* only the lines that name a file the turn changed count: the others came with the code HEAD moved to.
|
|
224
253
|
*/
|
|
225
|
-
export function addedLines(gate, output, baseline, changed) {
|
|
254
|
+
export function addedLines(gate, output, baseline, changed, elsewhere = false) {
|
|
226
255
|
if (baseline === undefined)
|
|
227
256
|
return { ok: true, output: "" };
|
|
228
|
-
const added = newLines(baseline, output, gate.ignoreLines, gate.lineKey, changed);
|
|
257
|
+
const added = newLines(baseline, output, gate.ignoreLines, gate.lineKey, changed).filter((line) => !elsewhere || changed.includes(fileOf(gate, line) ?? ""));
|
|
229
258
|
return { ok: added.length === 0, output: added.join("\n") };
|
|
230
259
|
}
|
|
@@ -88,7 +88,41 @@ export function hasCommit(root, commit) {
|
|
|
88
88
|
return false;
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
|
+
/** A file as a commit holds it, or nothing when the commit does not have it. */
|
|
92
|
+
export function fileAt(root, commit, file) {
|
|
93
|
+
try {
|
|
94
|
+
// However long: past the default megabyte, git would read as a commit that does not have the file.
|
|
95
|
+
return execFileSync("git", ["show", `${commit}:${file}`], {
|
|
96
|
+
cwd: root,
|
|
97
|
+
encoding: "utf8",
|
|
98
|
+
stdio: "pipe",
|
|
99
|
+
maxBuffer: Infinity,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
91
106
|
/** The files that differ between two commits or trees, the deleted ones included. */
|
|
92
107
|
export function changedSince(root, start, end = workingTree(root)) {
|
|
93
108
|
return paths(git(root, ["diff", "-z", "--name-only", start, end])).sort();
|
|
94
109
|
}
|
|
110
|
+
/** The moves of HEAD its reflog holds, oldest first: none in a repo with no commit, or that keeps no reflog. */
|
|
111
|
+
export function headMoves(root) {
|
|
112
|
+
try {
|
|
113
|
+
const entries = git(root, ["reflog", "show", "--format=%H %gs", "HEAD"]).split("\n").filter(Boolean).reverse();
|
|
114
|
+
const reached = entries.map((entry) => entry.slice(0, entry.indexOf(" ")));
|
|
115
|
+
return entries.map((entry, at) => ({
|
|
116
|
+
from: reached[at - 1],
|
|
117
|
+
to: reached[at] ?? "",
|
|
118
|
+
how: entry.slice(entry.indexOf(" ") + 1),
|
|
119
|
+
}));
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
return [];
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/** The files a commit changed. For a merge, the ones it leaves unlike each of its parents: its resolved conflicts. */
|
|
126
|
+
export function commitFiles(root, commit) {
|
|
127
|
+
return paths(git(root, ["diff-tree", "--no-commit-id", "--name-only", "-r", "-c", "--root", "-z", commit]));
|
|
128
|
+
}
|
|
@@ -24,6 +24,8 @@ function isSendBacks(value) {
|
|
|
24
24
|
function isTurnState(value) {
|
|
25
25
|
return (isRecord(value) &&
|
|
26
26
|
typeof value.start === "string" &&
|
|
27
|
+
(value.head === undefined || typeof value.head === "string") &&
|
|
28
|
+
(value.moves === undefined || typeof value.moves === "number") &&
|
|
27
29
|
isStringRecord(value.baselines) &&
|
|
28
30
|
isGatesFiles(value.config) &&
|
|
29
31
|
typeof value.checked === "boolean" &&
|
|
@@ -34,7 +36,8 @@ function isSessionState(value) {
|
|
|
34
36
|
(value.current === undefined || typeof value.current === "string") &&
|
|
35
37
|
isRecord(value.turns) &&
|
|
36
38
|
Object.values(value.turns).every(isTurnState) &&
|
|
37
|
-
(value.told === undefined || (isRecord(value.told) && Object.values(value.told).every(isStringArray)))
|
|
39
|
+
(value.told === undefined || (isRecord(value.told) && Object.values(value.told).every(isStringArray))) &&
|
|
40
|
+
(value.conductTold === undefined || isStringArray(value.conductTold)));
|
|
38
41
|
}
|
|
39
42
|
function readSession(session) {
|
|
40
43
|
const state = readJsonFile(stateFile(session));
|
|
@@ -52,14 +55,17 @@ export function readTurn(session, root) {
|
|
|
52
55
|
* progress is read again, by its retry.
|
|
53
56
|
*/
|
|
54
57
|
export function writeTurn(session, root, turn) {
|
|
55
|
-
const { turns,
|
|
58
|
+
const { turns, ...state } = readSession(session);
|
|
56
59
|
const unchecked = Object.entries(turns).filter(([, kept]) => !kept.checked);
|
|
57
|
-
writeSession(session, {
|
|
60
|
+
writeSession(session, { ...state, current: root, turns: { ...Object.fromEntries(unchecked), [root]: turn } });
|
|
58
61
|
}
|
|
59
62
|
/** Ends the turn in progress without a check, as a prompt from a folder with no gates, or a failed start, does. */
|
|
60
63
|
export function forgetTurn(session) {
|
|
61
|
-
|
|
62
|
-
|
|
64
|
+
forget(session, "current");
|
|
65
|
+
}
|
|
66
|
+
function forget(session, key) {
|
|
67
|
+
const { [key]: had, ...state } = readSession(session);
|
|
68
|
+
if (had !== undefined)
|
|
63
69
|
writeSession(session, state);
|
|
64
70
|
}
|
|
65
71
|
/**
|
|
@@ -74,11 +80,25 @@ export function tellOnce(session, root, missing) {
|
|
|
74
80
|
writeSession(session, { ...state, told: { ...state.told, [root]: missing } });
|
|
75
81
|
return missing.length > 0;
|
|
76
82
|
}
|
|
83
|
+
/**
|
|
84
|
+
* Whether the agent is yet to be told how to act on a send-back of this repo, in this session: said once, it stays in
|
|
85
|
+
* its context, and saying it again at every send-back only costs it tokens. Each repo has its own words for it.
|
|
86
|
+
*/
|
|
87
|
+
export function tellConductOnce(session, root) {
|
|
88
|
+
const state = readSession(session);
|
|
89
|
+
const told = state.conductTold ?? [];
|
|
90
|
+
if (told.includes(root))
|
|
91
|
+
return false;
|
|
92
|
+
writeSession(session, { ...state, conductTold: [...told, root] });
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
/** Forgets that the agent was told how to act: a session that starts again, or compacts, may have lost it. */
|
|
96
|
+
export function forgetConduct(session) {
|
|
97
|
+
forget(session, "conductTold");
|
|
98
|
+
}
|
|
77
99
|
/** Forgets what the agent was told of missing tools, so a session that starts again, or resumes, is told again. */
|
|
78
100
|
export function forgetTold(session) {
|
|
79
|
-
|
|
80
|
-
if (told !== undefined)
|
|
81
|
-
writeSession(session, state);
|
|
101
|
+
forget(session, "told");
|
|
82
102
|
}
|
|
83
103
|
/**
|
|
84
104
|
* The repo of the turn a check is about, whatever folder the agent stops in. A check that follows its own turn start
|
package/dist/src/core/watch.js
CHANGED
|
@@ -43,21 +43,30 @@ export function keepWatch(root, gate) {
|
|
|
43
43
|
if (existsSync(folder))
|
|
44
44
|
touch(folder);
|
|
45
45
|
}
|
|
46
|
+
// What the folders of the watches of a gate of a repo start with, whatever version of the engine started them.
|
|
47
|
+
function watchesOf(root, gate) {
|
|
48
|
+
const key = createHash("sha256").update(`${root}\0${gate.name}`).digest("hex").slice(0, 16);
|
|
49
|
+
return `${PREFIX}${key}-`;
|
|
50
|
+
}
|
|
46
51
|
/**
|
|
47
52
|
* Where a watch keeps its process, its output and the sessions that use it, outside the repo: one per repo, gate and
|
|
48
53
|
* engine version, since a helper of the engine may print its cycles otherwise from one version to the next.
|
|
49
54
|
*/
|
|
50
55
|
function folderOf(root, gate) {
|
|
51
|
-
|
|
52
|
-
return join(tmpdir(), `${PREFIX}${key}`);
|
|
56
|
+
return join(tmpdir(), `${watchesOf(root, gate)}${ENGINE_VERSION}`);
|
|
53
57
|
}
|
|
54
58
|
// A file per session that uses the watch: two hooks at once each write their own, and neither loses the other's.
|
|
55
59
|
function sessionFile(folder, session) {
|
|
56
60
|
return join(folder, "sessions", session.replace(/[^\w-]/g, ""));
|
|
57
61
|
}
|
|
62
|
+
/** Records that the session uses the watch, and says whether it is the first time it does. */
|
|
58
63
|
function register(folder, session) {
|
|
64
|
+
const file = sessionFile(folder, session);
|
|
65
|
+
if (existsSync(file))
|
|
66
|
+
return false;
|
|
59
67
|
mkdirSync(join(folder, "sessions"), { recursive: true });
|
|
60
|
-
writeFileSync(
|
|
68
|
+
writeFileSync(file, "");
|
|
69
|
+
return true;
|
|
61
70
|
}
|
|
62
71
|
/** The process of the watch, as the engine recorded it when it started it, or nothing when it never did. */
|
|
63
72
|
function processOf(folder) {
|
|
@@ -111,7 +120,8 @@ export function startWatch(session, root, gate) {
|
|
|
111
120
|
mkdirSync(folder, { recursive: true });
|
|
112
121
|
touch(folder);
|
|
113
122
|
// The sessions of a watch that starts again still use it.
|
|
114
|
-
register(folder, session)
|
|
123
|
+
if (register(folder, session))
|
|
124
|
+
leaveOtherVersions(session, root, gate);
|
|
115
125
|
if (stateOf(folder) === "running" || !lockStart(folder))
|
|
116
126
|
return;
|
|
117
127
|
try {
|
|
@@ -123,6 +133,25 @@ export function startWatch(session, root, gate) {
|
|
|
123
133
|
unlockStart(folder);
|
|
124
134
|
}
|
|
125
135
|
}
|
|
136
|
+
/**
|
|
137
|
+
* The session leaves the watches other versions of the engine started for this gate of this repo, now that it uses
|
|
138
|
+
* the one of this version: after an update of the engine, the watch of the version before would otherwise run beside
|
|
139
|
+
* the new one, until the session ends or it stops for idleness. A session still on that version keeps its watch, and
|
|
140
|
+
* the watch of another repo, such as another worktree, is not one of them.
|
|
141
|
+
*/
|
|
142
|
+
function leaveOtherVersions(session, root, gate) {
|
|
143
|
+
const tmp = tmpdir();
|
|
144
|
+
const prefix = watchesOf(root, gate);
|
|
145
|
+
for (const entry of readDir(tmp))
|
|
146
|
+
if (entry.startsWith(prefix) && entry !== `${prefix}${ENGINE_VERSION}`)
|
|
147
|
+
leave(join(tmp, entry), session);
|
|
148
|
+
}
|
|
149
|
+
/** The session no longer uses the watch, which stops once no session does: the watch and every process it started. */
|
|
150
|
+
function leave(folder, session) {
|
|
151
|
+
rmSync(sessionFile(folder, session), { force: true });
|
|
152
|
+
if (!readDir(join(folder, "sessions")).length)
|
|
153
|
+
stop(folder);
|
|
154
|
+
}
|
|
126
155
|
/** The files of the repo the watch lists only as it starts, one per line. */
|
|
127
156
|
function frozenFiles(root, watch) {
|
|
128
157
|
if (watch.frozen === undefined)
|
|
@@ -332,9 +361,9 @@ function readDir(folder) {
|
|
|
332
361
|
* the gate: out of time, neither green nor red, never judged on an older cycle. The watch then owes a cycle for these
|
|
333
362
|
* files, which the next stop it answers asks for with its own: a change it missed would otherwise stay unseen until
|
|
334
363
|
* the file changes again. Only a stop pays that debt, never the report of a turn start, which would make old a
|
|
335
|
-
* failure no turn was judged on. A watch that stopped
|
|
364
|
+
* failure no turn was judged on. A watch that stopped is a broken gate, with the last lines it printed.
|
|
336
365
|
*/
|
|
337
|
-
export async function judgeWatch(session, root, gate, files, baseline) {
|
|
366
|
+
export async function judgeWatch(session, root, gate, files, baseline, elsewhere = false) {
|
|
338
367
|
const folder = folderOf(root, gate);
|
|
339
368
|
const owed = owedFiles(folder);
|
|
340
369
|
const asked = [...new Set([...files, ...owed.files])];
|
|
@@ -342,7 +371,7 @@ export async function judgeWatch(session, root, gate, files, baseline) {
|
|
|
342
371
|
const { name } = gate;
|
|
343
372
|
const { ms } = settled;
|
|
344
373
|
if ("stopped" in settled)
|
|
345
|
-
return { name, ok: false, ms, output: settled.stopped, report: undefined };
|
|
374
|
+
return { name, ok: false, ms, output: settled.stopped, report: undefined, broken: true };
|
|
346
375
|
if (!("cycle" in settled)) {
|
|
347
376
|
owe(folder, files);
|
|
348
377
|
const output = `The watch opened no cycle for the turn within ${gate.timeoutSeconds} s, so not judged. Make it faster, or move it to CI.`;
|
|
@@ -351,14 +380,14 @@ export async function judgeWatch(session, root, gate, files, baseline) {
|
|
|
351
380
|
// Paid, less what a stop out of time meanwhile added: only the lists this stop asked for.
|
|
352
381
|
for (const list of owed.lists)
|
|
353
382
|
rmSync(list, { force: true });
|
|
354
|
-
const judged = judgeCycle(gate, settled.cycle, baseline, asked);
|
|
383
|
+
const judged = judgeCycle(gate, settled.cycle, baseline, asked, elsewhere);
|
|
355
384
|
return { name, ms, ...judged, report: judged.ok ? settled.cycle : undefined };
|
|
356
385
|
}
|
|
357
386
|
/** The verdict of a cycle: its failed lines, or for a compared gate, the lines it added to the report of the turn start. */
|
|
358
|
-
function judgeCycle(gate, cycle, baseline, files) {
|
|
387
|
+
function judgeCycle(gate, cycle, baseline, files, elsewhere) {
|
|
359
388
|
const fails = new RegExp(gate.watch.failed ?? "(?!)");
|
|
360
389
|
return isCompared(gate)
|
|
361
|
-
? addedLines(gate, cycle, baseline, files)
|
|
390
|
+
? addedLines(gate, cycle, baseline, files, elsewhere)
|
|
362
391
|
: { ok: !cycle.split("\n").some((line) => fails.test(line)), output: cycle };
|
|
363
392
|
}
|
|
364
393
|
/**
|
|
@@ -375,12 +404,8 @@ export function endSession(session) {
|
|
|
375
404
|
if (!entry.startsWith(PREFIX))
|
|
376
405
|
continue;
|
|
377
406
|
const folder = join(tmpdir(), entry);
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
continue;
|
|
381
|
-
rmSync(file, { force: true });
|
|
382
|
-
if (!readdirSync(join(folder, "sessions")).length)
|
|
383
|
-
stop(folder);
|
|
407
|
+
if (existsSync(sessionFile(folder, session)))
|
|
408
|
+
leave(folder, session);
|
|
384
409
|
}
|
|
385
410
|
}
|
|
386
411
|
function stop(folder) {
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { isRecord, messageOf, parseJson } from "../core/json.js";
|
|
4
|
+
import { headed } from "../core/run.js";
|
|
5
|
+
import { fileAt } from "../core/snapshot.js";
|
|
6
|
+
function countsOf(file) {
|
|
7
|
+
const refuse = () => {
|
|
8
|
+
throw new Error("Not an ESLint suppressions file.");
|
|
9
|
+
};
|
|
10
|
+
if (!isRecord(file))
|
|
11
|
+
return refuse();
|
|
12
|
+
return new Map(Object.entries(file).map(([source, rules]) => {
|
|
13
|
+
if (!isRecord(rules))
|
|
14
|
+
return refuse();
|
|
15
|
+
const counted = Object.entries(rules).map(([rule, entry]) => {
|
|
16
|
+
if (!isRecord(entry) || typeof entry.count !== "number")
|
|
17
|
+
return refuse();
|
|
18
|
+
return [rule, entry.count];
|
|
19
|
+
});
|
|
20
|
+
return [source, new Map(counted)];
|
|
21
|
+
}));
|
|
22
|
+
}
|
|
23
|
+
function totals(counts) {
|
|
24
|
+
const total = new Map();
|
|
25
|
+
for (const rules of counts.values())
|
|
26
|
+
for (const [rule, count] of rules)
|
|
27
|
+
total.set(rule, (total.get(rule) ?? 0) + count);
|
|
28
|
+
return total;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The counts that rose, one line per file and rule. A file the turn moved takes its counts to another path: there, a
|
|
32
|
+
* count that rose is new only when the suppressions file holds more of its rule than as the turn started.
|
|
33
|
+
*/
|
|
34
|
+
export function newBulkSuppressions(before, after, moved = () => false) {
|
|
35
|
+
const was = countsOf(before ?? {});
|
|
36
|
+
const now = countsOf(after);
|
|
37
|
+
const [wasTotal, nowTotal] = [totals(was), totals(now)];
|
|
38
|
+
return [...now].flatMap(([source, rules]) => [...rules].flatMap(([rule, count]) => {
|
|
39
|
+
const old = was.get(source)?.get(rule) ?? 0;
|
|
40
|
+
if (count <= old)
|
|
41
|
+
return [];
|
|
42
|
+
// Asked last: it costs a look at the turn start.
|
|
43
|
+
const cameWithIt = (nowTotal.get(rule) ?? 0) <= (wasTotal.get(rule) ?? 0) && moved(source);
|
|
44
|
+
return cameWithIt ? [] : [`${source}: ${rule} ${old} -> ${count}`];
|
|
45
|
+
}));
|
|
46
|
+
}
|
|
47
|
+
/** The counts the turn raised in the suppressions files given, each under the name of its file. */
|
|
48
|
+
export function bulkSuppressions(root, turn, files) {
|
|
49
|
+
return files.flatMap((file) => {
|
|
50
|
+
const before = fileAt(root, turn, file);
|
|
51
|
+
// ESLint names a source from the folder it runs in: the one of its suppressions file, or the root of the repo
|
|
52
|
+
// when the repo keeps that file in a folder of its own. A source found in neither is never taken for a moved one.
|
|
53
|
+
const moved = (source) => [join(dirname(file), source), source].some((path) => existsSync(join(root, path)) && fileAt(root, turn, path) === undefined);
|
|
54
|
+
const rose = newBulkSuppressions(before === undefined ? undefined : parseJson(before), parseJson(readFileSync(join(root, file), "utf8")), moved);
|
|
55
|
+
return rose.map((line) => `${file}: ${line}`);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
function main(root, turn, files) {
|
|
59
|
+
const found = bulkSuppressions(root, turn, files);
|
|
60
|
+
if (!found.length)
|
|
61
|
+
return;
|
|
62
|
+
console.log(headed("New bulk suppressions: fix the violations, never the file that hides them.", found));
|
|
63
|
+
process.exitCode = 1;
|
|
64
|
+
}
|
|
65
|
+
if (import.meta.main) {
|
|
66
|
+
try {
|
|
67
|
+
main(process.cwd(), process.argv[2] ?? "HEAD", process.argv.slice(3));
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
// What is wrong with the file, without the stack of this script around it.
|
|
71
|
+
console.log(messageOf(error));
|
|
72
|
+
process.exitCode = 1;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -3,7 +3,7 @@ import { mkdtempSync, rmSync } from "node:fs";
|
|
|
3
3
|
import { tmpdir } from "node:os";
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import { isRecord, readJsonFile } from "../core/json.js";
|
|
6
|
-
import { packageScript } from "../core/run.js";
|
|
6
|
+
import { headed, packageScript } from "../core/run.js";
|
|
7
7
|
// jscpd compared with the tree the turn started from. Its own console report lists every clone of the repo, the old
|
|
8
8
|
// ones included, so this prints only the clones the turn added, one per line.
|
|
9
9
|
function refuse() {
|
|
@@ -46,7 +46,7 @@ function main(root, turn) {
|
|
|
46
46
|
const clones = newClones(readJsonFile(join(output, "jscpd-report.json")));
|
|
47
47
|
if (!clones.length)
|
|
48
48
|
return;
|
|
49
|
-
console.log(
|
|
49
|
+
console.log(headed("New clones: reuse the code already there, or factor it out.", clones));
|
|
50
50
|
process.exitCode = 1;
|
|
51
51
|
}
|
|
52
52
|
finally {
|