@tayomi/deterministic-gates 0.10.0 → 0.10.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/dist/src/core/check.js +32 -16
- package/dist/src/core/head.js +76 -22
- package/dist/src/core/report.js +16 -9
- package/dist/src/core/snapshot.js +72 -15
- package/dist/src/core/turn-state.js +14 -2
- package/dist/src/helpers/new-clones.js +90 -16
- package/package.json +1 -1
- package/src/core/check.ts +30 -24
- package/src/core/head.ts +99 -30
- package/src/core/report.ts +15 -8
- package/src/core/snapshot.ts +87 -20
- package/src/core/turn-state.ts +20 -3
- package/src/helpers/new-clones.ts +105 -15
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/claude-code-plugin-manifest.json",
|
|
3
3
|
"name": "deterministic-gates",
|
|
4
|
-
"version": "0.10.
|
|
4
|
+
"version": "0.10.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",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "deterministic-gates",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.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
|
"author": {
|
|
6
6
|
"name": "Pierre Moati",
|
package/dist/src/core/check.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { existsSync } from "node:fs";
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
import { changedGatesFiles, gatesOf, isCompared, keepsWatch, readGatesFiles, } from "./config.js";
|
|
4
|
-
import { headOf, movedSince } from "./head.js";
|
|
4
|
+
import { headOf, movedSince, observed, thereOf } from "./head.js";
|
|
5
5
|
import { cachedReports, cacheReports } from "./report-cache.js";
|
|
6
6
|
import { check, filesOf, givenFiles, missingTool, namesFiles, wholeProject } from "./run.js";
|
|
7
|
-
import { changedSince, emptyCommit, hasCommit, lastCommit, snapshot, workingTree } from "./snapshot.js";
|
|
8
|
-
import { readTurn, writeTurn } from "./turn-state.js";
|
|
7
|
+
import { changedSince, emptyCommit, hasCommit, lastCommit, sharedDir, snapshot, workingTree } from "./snapshot.js";
|
|
8
|
+
import { readThere, readTurn, writeThere, writeTurn } from "./turn-state.js";
|
|
9
9
|
import { judgeWatch, keepWatch, startWatch, TURN_START_MS, watchReport } from "./watch.js";
|
|
10
10
|
function missingTools(root, gates) {
|
|
11
11
|
return gates.flatMap((gate) => {
|
|
@@ -39,10 +39,17 @@ export async function startTurn(session, root) {
|
|
|
39
39
|
const compared = runnable(gates, missing).filter(isCompared);
|
|
40
40
|
startWatches(session, root, runnable(gates, missing));
|
|
41
41
|
const interrupted = readTurn(session, root);
|
|
42
|
+
// The worktrees of a repo share its commits: what the session knows of them is of the repo, not of the folder.
|
|
43
|
+
const shared = sharedDir(root);
|
|
44
|
+
const there = readThere(session, shared);
|
|
42
45
|
if (interrupted?.checked === false) {
|
|
43
|
-
|
|
46
|
+
// What it committed so far stays the session's, and a commit older than this prompt is one a fetch may bring.
|
|
47
|
+
if (there)
|
|
48
|
+
writeThere(session, shared, observed(root, there, interrupted.at ?? Date.now()));
|
|
49
|
+
writeTurn(session, root, { ...interrupted, at: Date.now() });
|
|
44
50
|
return told;
|
|
45
51
|
}
|
|
52
|
+
writeThere(session, shared, thereOf(root, there));
|
|
46
53
|
const head = headOf(root);
|
|
47
54
|
const { commit: start, tree } = snapshot(root);
|
|
48
55
|
// The turn starts before its reports are ready: a hook stopped for lack of time leaves it, never the turn before.
|
|
@@ -128,8 +135,7 @@ function endReports(results, baselines, elsewhere) {
|
|
|
128
135
|
* them mid-turn changes the next turn, never its own. Without a recorded start, the turn runs from the last commit
|
|
129
136
|
* with the gates of now; with a start git no longer has, from the last commit with the gates and the reports it
|
|
130
137
|
* started with: checking too much beats leaving a hole. When HEAD moved during the turn to code it did not write, as
|
|
131
|
-
* a checkout or a pull does, the turn is only
|
|
132
|
-
* turn committed before: the rest came with that commit.
|
|
138
|
+
* a checkout or a pull does, the turn is only the files unlike the ones that code holds: the rest came with it.
|
|
133
139
|
*/
|
|
134
140
|
export async function checkTurn(session, root) {
|
|
135
141
|
const recorded = readTurn(session, root);
|
|
@@ -145,28 +151,38 @@ export async function checkTurn(session, root) {
|
|
|
145
151
|
baselines: recorded?.baselines ?? {},
|
|
146
152
|
config: recorded?.config ?? now,
|
|
147
153
|
checked: false,
|
|
148
|
-
...(kept ? { head: kept.head,
|
|
154
|
+
...(kept ? { head: kept.head, at: kept.at } : {}),
|
|
149
155
|
// What the earlier stops of the turn told the agent goes on, so its send-backs and waivers add up.
|
|
150
156
|
...(recorded?.sendBacks ? { sendBacks: recorded.sendBacks } : {}),
|
|
151
157
|
};
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
const
|
|
158
|
+
const shared = sharedDir(root);
|
|
159
|
+
const there = readThere(session, shared);
|
|
160
|
+
const moved = kept && there && movedSince(root, kept, there);
|
|
155
161
|
const started = performance.now();
|
|
156
162
|
const end = workingTree(root);
|
|
157
163
|
const since = changedSince(root, turn.start, end);
|
|
158
|
-
|
|
159
|
-
const changed =
|
|
164
|
+
// A file like the one a commit under HEAD holds came with that commit: the turn did not write it.
|
|
165
|
+
const changed = (moved?.under ?? []).reduce((files, commit) => {
|
|
166
|
+
const unlike = new Set(changedSince(root, commit, end));
|
|
167
|
+
return files.filter((file) => unlike.has(file));
|
|
168
|
+
}, since);
|
|
169
|
+
// Code came with a commit, or left with one: the tree the turn started on no longer tells what the turn wrote.
|
|
170
|
+
const elsewhere = moved !== undefined && (moved.arrived || changed.length < since.length);
|
|
171
|
+
// Where a gate given the turn starts from: then, the commit that brought the code.
|
|
172
|
+
const from = (elsewhere ? moved?.to : undefined) ?? turn.start;
|
|
173
|
+
// A report of code HEAD has left serves a gate whose lines name their file, to count the ones of the turn's files.
|
|
174
|
+
const baselines = (gate) => (elsewhere && !namesFiles(gate) ? undefined : turn.baselines[gate.name]);
|
|
160
175
|
const config = gatesOf(turn.config);
|
|
161
176
|
const { gates, refused } = config;
|
|
162
177
|
const missing = missingTools(root, gates);
|
|
163
178
|
for (const gate of gates)
|
|
164
179
|
if (keepsWatch(gate))
|
|
165
180
|
keepWatch(root, gate);
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
cacheReports(root, end, endReports(results, baselines, moved !== undefined));
|
|
181
|
+
const results = await runGates(root, runnable(gates, missing), changed, from, baselines, session, elsewhere);
|
|
182
|
+
cacheReports(root, end, endReports(results, baselines, elsewhere));
|
|
169
183
|
writeTurn(session, root, { ...turn, checked: true });
|
|
184
|
+
if (there)
|
|
185
|
+
writeThere(session, shared, observed(root, there, kept?.at ?? Date.now()));
|
|
170
186
|
// A compared gate with no report from the turn start can only count its exit code, and a watch has none: a green it
|
|
171
187
|
// did not earn is said.
|
|
172
188
|
const uncompared = results
|
|
@@ -182,7 +198,7 @@ export async function checkTurn(session, root) {
|
|
|
182
198
|
.map(({ gate, why }) => `${gate.name} not run: ${why}`);
|
|
183
199
|
const notes = [
|
|
184
200
|
...(startNote ? [startNote] : []),
|
|
185
|
-
...(
|
|
201
|
+
...(elsewhere ? ["HEAD moved during the turn to code it did not write: only what the turn wrote is checked"] : []),
|
|
186
202
|
...configNote(changedGatesFiles(turn.config, now)),
|
|
187
203
|
...uncompared,
|
|
188
204
|
...refusedNotes(refused),
|
package/dist/src/core/head.js
CHANGED
|
@@ -1,30 +1,84 @@
|
|
|
1
|
-
import {
|
|
2
|
-
// What takes HEAD to a commit the turn did not write, as the reflog names it. Any other move is a commit of the turn,
|
|
3
|
-
// as an amend or a cherry-pick: checking too much beats leaving a hole.
|
|
4
|
-
const NOT_ITS_COMMIT = /^(checkout|pull|merge|rebase|reset)\b/;
|
|
5
|
-
// A merge the turn commits brings the code of a branch too: of its files, the turn wrote the conflicts it resolved.
|
|
6
|
-
const ELSEWHERE = new RegExp(`${NOT_ITS_COMMIT.source}|^commit \\(merge\\)`);
|
|
1
|
+
import { branchTips, commitsPast, hasCommit, isIn } from "./snapshot.js";
|
|
7
2
|
export function headOf(root) {
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
return { moves: moves.length, ...(head === undefined ? {} : { head }) };
|
|
3
|
+
const { head } = branchTips(root);
|
|
4
|
+
return { ...(head === undefined ? {} : { head }), at: Date.now() };
|
|
11
5
|
}
|
|
12
6
|
/**
|
|
13
|
-
*
|
|
14
|
-
* a
|
|
7
|
+
* What a session knows of a repo as a turn starts there, from what it knew: the commit HEAD is on was there, since
|
|
8
|
+
* a turn that made it has been judged on it. Before its first turn, the tips of the branches were.
|
|
15
9
|
*/
|
|
16
|
-
export function
|
|
17
|
-
|
|
10
|
+
export function thereOf(root, before) {
|
|
11
|
+
const { head, tips } = branchTips(root);
|
|
12
|
+
if (before === undefined)
|
|
13
|
+
return { known: tips, made: [] };
|
|
14
|
+
return { known: [...new Set([...before.known, ...(head === undefined ? [] : [head])])], made: before.made };
|
|
15
|
+
}
|
|
16
|
+
/** The commits the session made, among the ones past what it knew: the ones it saw as its own, and the ones of the turn. */
|
|
17
|
+
function madeOf(past, { made }, at) {
|
|
18
|
+
const seen = new Set(made);
|
|
19
|
+
const started = Math.floor(at / 1000);
|
|
20
|
+
return past.filter((commit) => seen.has(commit.hash) || commit.at >= started);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* What the session knows once a turn that started then stops: with the commits the turn made, on whatever branch, so
|
|
24
|
+
* that they stay its own in the turns after. Less the known commits git no longer has, as after a deleted branch.
|
|
25
|
+
*/
|
|
26
|
+
export function observed(root, there, at) {
|
|
27
|
+
const seen = (known) => {
|
|
28
|
+
const mine = madeOf(commitsPast(root, branchTips(root).tips, known), there, at);
|
|
29
|
+
return { known, made: [...new Set([...there.made, ...mine.map(({ hash }) => hash)])] };
|
|
30
|
+
};
|
|
31
|
+
try {
|
|
32
|
+
return seen(there.known);
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
const kept = there.known.filter((commit) => hasCommit(root, commit));
|
|
36
|
+
try {
|
|
37
|
+
return seen(kept);
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return { known: kept, made: there.made };
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/** The commits that were there, nearest under a commit: itself, or the ones under the commits the session made. */
|
|
45
|
+
function under(tip, made) {
|
|
46
|
+
const there = new Set();
|
|
47
|
+
const seen = new Set();
|
|
48
|
+
const left = [tip];
|
|
49
|
+
for (let commit = left.pop(); commit !== undefined; commit = left.pop()) {
|
|
50
|
+
if (seen.has(commit))
|
|
51
|
+
continue;
|
|
52
|
+
seen.add(commit);
|
|
53
|
+
const parents = made.get(commit);
|
|
54
|
+
if (parents === undefined)
|
|
55
|
+
there.add(commit);
|
|
56
|
+
else
|
|
57
|
+
left.push(...parents);
|
|
58
|
+
}
|
|
59
|
+
return [...there];
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Whether the code under HEAD that the session did not write changed since the turn started, as after a checkout, a
|
|
63
|
+
* pull, a merge, a rebase or a reset. Nothing when it did not, as after a commit of the turn, or a branch it left and
|
|
64
|
+
* came back to; nor when nothing tells any more, where all the turn changed is checked.
|
|
65
|
+
*/
|
|
66
|
+
export function movedSince(root, { head, at }, there) {
|
|
67
|
+
const now = branchTips(root).head;
|
|
68
|
+
if (head === undefined || at === undefined || now === undefined || now === head)
|
|
18
69
|
return undefined;
|
|
19
|
-
|
|
20
|
-
|
|
70
|
+
let past;
|
|
71
|
+
try {
|
|
72
|
+
past = commitsPast(root, [now, head], there.known);
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
21
75
|
return undefined;
|
|
22
|
-
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
if (move === undefined)
|
|
76
|
+
}
|
|
77
|
+
const made = new Map(madeOf(past, there, at).map(({ hash, parents }) => [hash, parents]));
|
|
78
|
+
const [before, after] = [under(head, made), under(now, made)];
|
|
79
|
+
if (after.length === before.length && after.every((commit) => before.includes(commit)))
|
|
27
80
|
return undefined;
|
|
28
|
-
const
|
|
29
|
-
|
|
81
|
+
const brought = after.find((commit) => !before.some((had) => isIn(root, commit, had)));
|
|
82
|
+
const to = brought ?? after.find((commit) => !before.includes(commit));
|
|
83
|
+
return { under: after, arrived: brought !== undefined, ...(to === undefined ? {} : { to }) };
|
|
30
84
|
}
|
package/dist/src/core/report.js
CHANGED
|
@@ -135,16 +135,23 @@ function sent(report, omitLines) {
|
|
|
135
135
|
const kept = report.split("\n").filter((line) => !omitted.test(line));
|
|
136
136
|
return kept.some((line) => line.trim()) ? kept.join("\n") : report;
|
|
137
137
|
}
|
|
138
|
-
// A helper of the engine counts apart the errors of one source line, with `(2)`
|
|
139
|
-
// so that one more of them is new. The agent reads each of them once, with how many
|
|
138
|
+
// A helper of the engine counts apart the errors of one source line, with `(2)` after the source it quotes and before
|
|
139
|
+
// the tab that ends their key, so that one more of them is new. The agent reads each of them once, with how many
|
|
140
|
+
// times it stands. A line of another tool is left as it is.
|
|
141
|
+
const COUNTED_APART = /` \(\d+\)\t/;
|
|
140
142
|
function saidOnce(report) {
|
|
141
|
-
|
|
143
|
+
if (!COUNTED_APART.test(report))
|
|
144
|
+
return report;
|
|
145
|
+
const lines = report.split("\n");
|
|
146
|
+
const keyed = lines.map((line) => line.replace(COUNTED_APART, "`\t"));
|
|
147
|
+
const counted = new Set(keyed.filter((line, at) => line !== lines[at]));
|
|
142
148
|
const times = new Map();
|
|
143
|
-
for (const line of
|
|
144
|
-
if (
|
|
149
|
+
for (const line of keyed)
|
|
150
|
+
if (counted.has(line))
|
|
145
151
|
times.set(line, (times.get(line) ?? 0) + 1);
|
|
146
|
-
|
|
147
|
-
|
|
152
|
+
const said = new Set();
|
|
153
|
+
return keyed
|
|
154
|
+
.filter((line) => !times.has(line) || (!said.has(line) && said.add(line)))
|
|
148
155
|
.map((line) => ((times.get(line) ?? 1) > 1 ? `${line} (×${times.get(line)})` : line))
|
|
149
156
|
.join("\n");
|
|
150
157
|
}
|
|
@@ -180,13 +187,13 @@ export function toolsToInstall(missing, prompt) {
|
|
|
180
187
|
}
|
|
181
188
|
/**
|
|
182
189
|
* 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,
|
|
190
|
+
* less the lines the gate omits, each line a helper counts apart said once, in the characters it has, and in its share of a total when the reports have one.
|
|
184
191
|
*/
|
|
185
192
|
export function reports({ outcomes, wording = {} }) {
|
|
186
193
|
const { gates = [], prompts, maxTotalReportChars = Infinity } = wording;
|
|
187
194
|
const red = outcomes.filter(failed).map(({ name, output, broken }) => {
|
|
188
195
|
const gate = gates.find((known) => known.name === name);
|
|
189
|
-
const report = sent(
|
|
196
|
+
const report = saidOnce(sent(output, gate?.omitLines));
|
|
190
197
|
const told = [
|
|
191
198
|
...(broken ? [worded(BROKEN_GATE, prompts?.brokenGate, "\n")] : []),
|
|
192
199
|
...(gate?.prompt ? [gate.prompt.text] : []),
|
|
@@ -2,9 +2,20 @@ import { execFileSync } from "node:child_process";
|
|
|
2
2
|
import { cpSync, existsSync, mkdtempSync, rmSync } from "node:fs";
|
|
3
3
|
import { tmpdir } from "node:os";
|
|
4
4
|
import { join, resolve } from "node:path";
|
|
5
|
-
/**
|
|
5
|
+
/**
|
|
6
|
+
* Runs git in the repo, and gives what it printed, without the line break that ends it, however long. Nothing else is
|
|
7
|
+
* trimmed: the name of a file may start with a space.
|
|
8
|
+
*/
|
|
6
9
|
function git(root, args, { env = process.env, input } = {}) {
|
|
7
|
-
|
|
10
|
+
const printed = execFileSync("git", args, {
|
|
11
|
+
cwd: root,
|
|
12
|
+
encoding: "utf8",
|
|
13
|
+
env,
|
|
14
|
+
input,
|
|
15
|
+
stdio: "pipe",
|
|
16
|
+
maxBuffer: Infinity,
|
|
17
|
+
});
|
|
18
|
+
return printed.replace(/\n$/, "");
|
|
8
19
|
}
|
|
9
20
|
// One NUL after each path, which git otherwise quotes and escapes when it holds an accent.
|
|
10
21
|
function paths(printed) {
|
|
@@ -107,22 +118,68 @@ export function fileAt(root, commit, file) {
|
|
|
107
118
|
export function changedSince(root, start, end = workingTree(root)) {
|
|
108
119
|
return paths(git(root, ["diff", "-z", "--name-only", start, end])).sort();
|
|
109
120
|
}
|
|
110
|
-
/** The
|
|
111
|
-
export function
|
|
121
|
+
/** The commit HEAD is on, and the tips of the branches: nothing in a repo with no commit. */
|
|
122
|
+
export function branchTips(root) {
|
|
123
|
+
try {
|
|
124
|
+
const head = git(root, ["rev-parse", "--verify", "--quiet", "HEAD"]);
|
|
125
|
+
const tips = git(root, ["for-each-ref", "--format=%(objectname)", "refs/heads"]).split("\n");
|
|
126
|
+
return { head, tips: [...new Set([head, ...tips.filter(Boolean)])] };
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
return { tips: [] };
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/** The folder of git that the worktrees of a repo share: what tells that two of them are of the same repo. */
|
|
133
|
+
export function sharedDir(root) {
|
|
112
134
|
try {
|
|
113
|
-
|
|
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
|
-
}));
|
|
135
|
+
return git(root, ["rev-parse", "--path-format=absolute", "--git-common-dir"]);
|
|
120
136
|
}
|
|
121
137
|
catch {
|
|
122
|
-
return
|
|
138
|
+
return root;
|
|
123
139
|
}
|
|
124
140
|
}
|
|
125
|
-
/**
|
|
126
|
-
export function
|
|
127
|
-
|
|
141
|
+
/** Whether a commit is another one, or one it holds. */
|
|
142
|
+
export function isIn(root, commit, other) {
|
|
143
|
+
try {
|
|
144
|
+
git(root, ["merge-base", "--is-ancestor", commit, other]);
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/** The commits these tips hold that none of the known commits does. It throws when git no longer has one of them. */
|
|
152
|
+
export function commitsPast(root, tips, known) {
|
|
153
|
+
const input = known.map((commit) => `^${commit}\n`).join("");
|
|
154
|
+
// Two lines a commit: "commit", its hash and its parents, then its time.
|
|
155
|
+
const lines = git(root, ["rev-list", "--parents", "--format=%ct", "--stdin", ...tips], { input }).split("\n");
|
|
156
|
+
return lines.filter(Boolean).flatMap((line, at, all) => {
|
|
157
|
+
if (at % 2)
|
|
158
|
+
return [];
|
|
159
|
+
const [, hash = "", ...parents] = line.split(" ");
|
|
160
|
+
return [{ hash, parents, at: Number(all[at + 1]) }];
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* The lines of a file the working tree wrote since a commit that has the file. The file is compared with the one of
|
|
165
|
+
* the commit, not through the index, which does not know a file nobody added; and with the diff of git itself, plain,
|
|
166
|
+
* whatever colours or program the owner set for it.
|
|
167
|
+
*/
|
|
168
|
+
export function writtenSince(root, commit, file) {
|
|
169
|
+
const plain = ["--no-color", "--no-ext-diff", "--no-textconv", "--unified=0", "--inter-hunk-context=0"];
|
|
170
|
+
// The name as it is: git would otherwise read the brackets of a file such as [slug].ts as a pattern.
|
|
171
|
+
const diff = git(root, ["--literal-pathspecs", "diff", ...plain, `${commit}:${file}`, "--", file]);
|
|
172
|
+
const hunks = diff.matchAll(/^@@ -\S+ \+(\d+)(?:,(\d+))? @@/gm);
|
|
173
|
+
return [...hunks].map(([, at, lines = "1"]) => ({ at: Number(at), lines: Number(lines) }));
|
|
174
|
+
}
|
|
175
|
+
// Fewer names at once than a command line holds.
|
|
176
|
+
const NAMES_AT_ONCE = 500;
|
|
177
|
+
/** The ones of these files a commit has. */
|
|
178
|
+
export function filesAt(root, commit, files) {
|
|
179
|
+
const found = [];
|
|
180
|
+
for (let at = 0; at < files.length; at += NAMES_AT_ONCE) {
|
|
181
|
+
const names = files.slice(at, at + NAMES_AT_ONCE);
|
|
182
|
+
found.push(...paths(git(root, ["--literal-pathspecs", "ls-tree", "-r", "--name-only", "-z", commit, "--", ...names])));
|
|
183
|
+
}
|
|
184
|
+
return found;
|
|
128
185
|
}
|
|
@@ -25,19 +25,23 @@ function isTurnState(value) {
|
|
|
25
25
|
return (isRecord(value) &&
|
|
26
26
|
typeof value.start === "string" &&
|
|
27
27
|
(value.head === undefined || typeof value.head === "string") &&
|
|
28
|
-
(value.
|
|
28
|
+
(value.at === undefined || typeof value.at === "number") &&
|
|
29
29
|
isStringRecord(value.baselines) &&
|
|
30
30
|
isGatesFiles(value.config) &&
|
|
31
31
|
typeof value.checked === "boolean" &&
|
|
32
32
|
(value.sendBacks === undefined || isSendBacks(value.sendBacks)));
|
|
33
33
|
}
|
|
34
|
+
function isThere(value) {
|
|
35
|
+
return isRecord(value) && isStringArray(value.known) && isStringArray(value.made);
|
|
36
|
+
}
|
|
34
37
|
function isSessionState(value) {
|
|
35
38
|
return (isRecord(value) &&
|
|
36
39
|
(value.current === undefined || typeof value.current === "string") &&
|
|
37
40
|
isRecord(value.turns) &&
|
|
38
41
|
Object.values(value.turns).every(isTurnState) &&
|
|
39
42
|
(value.told === undefined || (isRecord(value.told) && Object.values(value.told).every(isStringArray))) &&
|
|
40
|
-
(value.conductTold === undefined || isStringArray(value.conductTold))
|
|
43
|
+
(value.conductTold === undefined || isStringArray(value.conductTold)) &&
|
|
44
|
+
(value.there === undefined || (isRecord(value.there) && Object.values(value.there).every(isThere))));
|
|
41
45
|
}
|
|
42
46
|
function readSession(session) {
|
|
43
47
|
const state = readJsonFile(stateFile(session));
|
|
@@ -59,6 +63,14 @@ export function writeTurn(session, root, turn) {
|
|
|
59
63
|
const unchecked = Object.entries(turns).filter(([, kept]) => !kept.checked);
|
|
60
64
|
writeSession(session, { ...state, current: root, turns: { ...Object.fromEntries(unchecked), [root]: turn } });
|
|
61
65
|
}
|
|
66
|
+
/** What the session knows of the commits of a repo, or nothing before its first turn there. */
|
|
67
|
+
export function readThere(session, root) {
|
|
68
|
+
return readSession(session).there?.[root];
|
|
69
|
+
}
|
|
70
|
+
export function writeThere(session, root, there) {
|
|
71
|
+
const state = readSession(session);
|
|
72
|
+
writeSession(session, { ...state, there: { ...state.there, [root]: there } });
|
|
73
|
+
}
|
|
62
74
|
/** Ends the turn in progress without a check, as a prompt from a folder with no gates, or a failed start, does. */
|
|
63
75
|
export function forgetTurn(session) {
|
|
64
76
|
forget(session, "current");
|
|
@@ -1,33 +1,95 @@
|
|
|
1
1
|
import { execFileSync } from "node:child_process";
|
|
2
|
-
import { mkdtempSync, rmSync } from "node:fs";
|
|
2
|
+
import { mkdtempSync, readFileSync, 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
6
|
import { headed, packageScript } from "../core/run.js";
|
|
7
|
+
import { filesAt, writtenSince } from "../core/snapshot.js";
|
|
7
8
|
// jscpd compared with the tree the turn started from. Its own console report lists every clone of the repo, the old
|
|
8
9
|
// ones included, so this prints only the clones the turn added, one per line.
|
|
10
|
+
//
|
|
11
|
+
// jscpd tells a clone by its two files and where it stands in them, so it also calls new an old clone the turn only
|
|
12
|
+
// disturbed: two old files it pairs once the turn takes their copy out of a third, or a clone the turn cut in two by a
|
|
13
|
+
// line written in its middle. A clone is the turn's only when the turn wrote in one of its sides.
|
|
9
14
|
function refuse() {
|
|
10
15
|
throw new Error("Not a jscpd report.");
|
|
11
16
|
}
|
|
12
|
-
function
|
|
17
|
+
function placeOf(value, line, column) {
|
|
18
|
+
if (isRecord(value) && typeof value.line === "number" && typeof value.column === "number")
|
|
19
|
+
return { line: value.line, column: value.column };
|
|
20
|
+
return { line, column };
|
|
21
|
+
}
|
|
22
|
+
// Without the columns of a clone, as from another jscpd, its first and last lines count as whole.
|
|
23
|
+
function sideOf(value) {
|
|
13
24
|
if (!isRecord(value))
|
|
14
25
|
refuse();
|
|
15
|
-
const { name, start, end } = value;
|
|
26
|
+
const { name, start, end, startLoc, endLoc } = value;
|
|
16
27
|
if (typeof name !== "string" || typeof start !== "number" || typeof end !== "number")
|
|
17
28
|
refuse();
|
|
18
|
-
return
|
|
29
|
+
return { name, start: placeOf(startLoc, start, 0), end: placeOf(endLoc, end, Infinity) };
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Whether the turn wrote in a side of a clone: a line it wrote stands whole in it, or it cut lines out of its middle.
|
|
33
|
+
* A clone starts and ends between two tokens, often inside a line: a line it only starts or ends in is not in it, nor
|
|
34
|
+
* an empty one, which jscpd does not read.
|
|
35
|
+
*/
|
|
36
|
+
export function wroteIn({ start, end }, text, written) {
|
|
37
|
+
const lines = text.split("\n");
|
|
38
|
+
const whole = (at) => {
|
|
39
|
+
const line = lines[at - 1] ?? "";
|
|
40
|
+
if (!line.trim())
|
|
41
|
+
return false;
|
|
42
|
+
const starts = at > start.line || (at === start.line && start.column <= line.length - line.trimStart().length);
|
|
43
|
+
const ends = at < end.line || (at === end.line && end.column >= line.trimEnd().length);
|
|
44
|
+
return starts && ends;
|
|
45
|
+
};
|
|
46
|
+
return written.some(({ at, lines: count }) => count === 0 ? start.line <= at && at < end.line : Array.from({ length: count }, (_, more) => at + more).some(whole));
|
|
19
47
|
}
|
|
20
|
-
|
|
48
|
+
/** The clones jscpd calls new, each as its two sides. */
|
|
49
|
+
function newPairs(report) {
|
|
21
50
|
if (!isRecord(report) || !Array.isArray(report.duplicates))
|
|
22
51
|
refuse();
|
|
23
52
|
return report.duplicates.flatMap((clone) => {
|
|
24
53
|
if (!isRecord(clone))
|
|
25
54
|
refuse();
|
|
26
|
-
const
|
|
27
|
-
return clone.isNew === true ? [
|
|
55
|
+
const sides = [sideOf(clone.firstFile), sideOf(clone.secondFile)];
|
|
56
|
+
return clone.isNew === true ? [sides] : [];
|
|
28
57
|
});
|
|
29
58
|
}
|
|
30
|
-
|
|
59
|
+
/** The clones jscpd calls new, one per line, less the ones the turn wrote in neither side of. */
|
|
60
|
+
export function newClones(report, wrote = () => true) {
|
|
61
|
+
return newPairs(report)
|
|
62
|
+
.filter((sides) => sides.some(wrote))
|
|
63
|
+
.map((sides) => sides.map(({ name, start, end }) => `${name}:${start.line}-${end.line}`).join(" ~ "));
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Whether the turn wrote in a side of a clone, for the clones of a report. Each file is read once, however many
|
|
67
|
+
* clones it has: the whole check finds every clone of the repo new.
|
|
68
|
+
*/
|
|
69
|
+
function turnWrote(root, turn, report) {
|
|
70
|
+
const names = [...new Set(newPairs(report).flatMap((sides) => sides.map(({ name }) => name)))];
|
|
71
|
+
const old = new Set(filesAt(root, turn, names));
|
|
72
|
+
const files = new Map();
|
|
73
|
+
return (side) => {
|
|
74
|
+
// A file the turn created is all its own, and one jscpd names from another folder is unknown here: checking too
|
|
75
|
+
// much beats leaving a hole.
|
|
76
|
+
if (!old.has(side.name))
|
|
77
|
+
return true;
|
|
78
|
+
try {
|
|
79
|
+
const file = files.get(side.name) ?? {
|
|
80
|
+
text: readFileSync(join(root, side.name), "utf8"),
|
|
81
|
+
written: writtenSince(root, turn, side.name),
|
|
82
|
+
};
|
|
83
|
+
files.set(side.name, file);
|
|
84
|
+
return wroteIn(side, file.text, file.written);
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/** The clones the turn added to the repo, as jscpd finds them. It throws what jscpd said when jscpd fails. */
|
|
92
|
+
export function turnClones(root, turn) {
|
|
31
93
|
const jscpd = packageScript(root, "jscpd");
|
|
32
94
|
if (jscpd === undefined)
|
|
33
95
|
throw new Error("jscpd is not installed in node_modules: install the dependencies of the repo.");
|
|
@@ -39,19 +101,31 @@ function main(root, turn) {
|
|
|
39
101
|
catch (error) {
|
|
40
102
|
// What jscpd said, without the stack of this script around it.
|
|
41
103
|
const said = isRecord(error) && typeof error.stderr === "string" ? error.stderr.trim() : String(error);
|
|
42
|
-
|
|
43
|
-
process.exitCode = 1;
|
|
44
|
-
return;
|
|
104
|
+
throw new Error(`jscpd failed: ${said}`, { cause: error });
|
|
45
105
|
}
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
return;
|
|
49
|
-
console.log(headed("New clones: reuse the code already there, or factor it out.", clones));
|
|
50
|
-
process.exitCode = 1;
|
|
106
|
+
const report = readJsonFile(join(output, "jscpd-report.json"));
|
|
107
|
+
return newClones(report, turnWrote(root, turn, report));
|
|
51
108
|
}
|
|
52
109
|
finally {
|
|
53
110
|
rmSync(output, { recursive: true, force: true });
|
|
54
111
|
}
|
|
55
112
|
}
|
|
113
|
+
function main(root, turn) {
|
|
114
|
+
let clones;
|
|
115
|
+
try {
|
|
116
|
+
clones = turnClones(root, turn);
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
if (!(error instanceof Error) || !error.message.startsWith("jscpd failed: "))
|
|
120
|
+
throw error;
|
|
121
|
+
console.log(error.message);
|
|
122
|
+
process.exitCode = 1;
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (!clones.length)
|
|
126
|
+
return;
|
|
127
|
+
console.log(headed("New clones: reuse the code already there, or factor it out.", clones));
|
|
128
|
+
process.exitCode = 1;
|
|
129
|
+
}
|
|
56
130
|
if (import.meta.main)
|
|
57
131
|
main(process.cwd(), process.argv[2] ?? "HEAD");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tayomi/deterministic-gates",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.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/check.ts
CHANGED
|
@@ -10,11 +10,11 @@ import {
|
|
|
10
10
|
type Prompts,
|
|
11
11
|
readGatesFiles,
|
|
12
12
|
} from "./config.ts";
|
|
13
|
-
import { headOf, movedSince } from "./head.ts";
|
|
13
|
+
import { headOf, movedSince, observed, thereOf } from "./head.ts";
|
|
14
14
|
import { cachedReports, cacheReports } from "./report-cache.ts";
|
|
15
15
|
import { check, filesOf, givenFiles, missingTool, namesFiles, type Outcome, wholeProject } from "./run.ts";
|
|
16
|
-
import { changedSince, emptyCommit, hasCommit, lastCommit, snapshot, workingTree } from "./snapshot.ts";
|
|
17
|
-
import { readTurn, type TurnState, writeTurn } from "./turn-state.ts";
|
|
16
|
+
import { changedSince, emptyCommit, hasCommit, lastCommit, sharedDir, snapshot, workingTree } from "./snapshot.ts";
|
|
17
|
+
import { readThere, readTurn, type TurnState, writeThere, writeTurn } from "./turn-state.ts";
|
|
18
18
|
import { judgeWatch, keepWatch, startWatch, TURN_START_MS, watchReport } from "./watch.ts";
|
|
19
19
|
|
|
20
20
|
/** What the repo says of what the agent is sent: the prompts of the whole check, and its gates, for their own. */
|
|
@@ -85,10 +85,16 @@ export async function startTurn(session: string, root: string): Promise<string[]
|
|
|
85
85
|
const compared = runnable(gates, missing).filter(isCompared);
|
|
86
86
|
startWatches(session, root, runnable(gates, missing));
|
|
87
87
|
const interrupted = readTurn(session, root);
|
|
88
|
+
// The worktrees of a repo share its commits: what the session knows of them is of the repo, not of the folder.
|
|
89
|
+
const shared = sharedDir(root);
|
|
90
|
+
const there = readThere(session, shared);
|
|
88
91
|
if (interrupted?.checked === false) {
|
|
89
|
-
|
|
92
|
+
// What it committed so far stays the session's, and a commit older than this prompt is one a fetch may bring.
|
|
93
|
+
if (there) writeThere(session, shared, observed(root, there, interrupted.at ?? Date.now()));
|
|
94
|
+
writeTurn(session, root, { ...interrupted, at: Date.now() });
|
|
90
95
|
return told;
|
|
91
96
|
}
|
|
97
|
+
writeThere(session, shared, thereOf(root, there));
|
|
92
98
|
const head = headOf(root);
|
|
93
99
|
const { commit: start, tree } = snapshot(root);
|
|
94
100
|
// The turn starts before its reports are ready: a hook stopped for lack of time leaves it, never the turn before.
|
|
@@ -199,8 +205,7 @@ function endReports(results: Result[], baselines: Baselines, elsewhere: boolean)
|
|
|
199
205
|
* them mid-turn changes the next turn, never its own. Without a recorded start, the turn runs from the last commit
|
|
200
206
|
* with the gates of now; with a start git no longer has, from the last commit with the gates and the reports it
|
|
201
207
|
* started with: checking too much beats leaving a hole. When HEAD moved during the turn to code it did not write, as
|
|
202
|
-
* a checkout or a pull does, the turn is only
|
|
203
|
-
* turn committed before: the rest came with that commit.
|
|
208
|
+
* a checkout or a pull does, the turn is only the files unlike the ones that code holds: the rest came with it.
|
|
204
209
|
*/
|
|
205
210
|
export async function checkTurn(session: string, root: string): Promise<CheckedTurn> {
|
|
206
211
|
const recorded = readTurn(session, root);
|
|
@@ -217,34 +222,35 @@ export async function checkTurn(session: string, root: string): Promise<CheckedT
|
|
|
217
222
|
baselines: recorded?.baselines ?? {},
|
|
218
223
|
config: recorded?.config ?? now,
|
|
219
224
|
checked: false,
|
|
220
|
-
...(kept ? { head: kept.head,
|
|
225
|
+
...(kept ? { head: kept.head, at: kept.at } : {}),
|
|
221
226
|
// What the earlier stops of the turn told the agent goes on, so its send-backs and waivers add up.
|
|
222
227
|
...(recorded?.sendBacks ? { sendBacks: recorded.sendBacks } : {}),
|
|
223
228
|
};
|
|
224
|
-
const
|
|
225
|
-
|
|
226
|
-
const
|
|
229
|
+
const shared = sharedDir(root);
|
|
230
|
+
const there = readThere(session, shared);
|
|
231
|
+
const moved = kept && there && movedSince(root, kept, there);
|
|
227
232
|
const started = performance.now();
|
|
228
233
|
const end = workingTree(root);
|
|
229
234
|
const since = changedSince(root, turn.start, end);
|
|
230
|
-
|
|
231
|
-
const changed =
|
|
235
|
+
// A file like the one a commit under HEAD holds came with that commit: the turn did not write it.
|
|
236
|
+
const changed = (moved?.under ?? []).reduce((files, commit) => {
|
|
237
|
+
const unlike = new Set(changedSince(root, commit, end));
|
|
238
|
+
return files.filter((file) => unlike.has(file));
|
|
239
|
+
}, since);
|
|
240
|
+
// Code came with a commit, or left with one: the tree the turn started on no longer tells what the turn wrote.
|
|
241
|
+
const elsewhere = moved !== undefined && (moved.arrived || changed.length < since.length);
|
|
242
|
+
// Where a gate given the turn starts from: then, the commit that brought the code.
|
|
243
|
+
const from = (elsewhere ? moved?.to : undefined) ?? turn.start;
|
|
244
|
+
// A report of code HEAD has left serves a gate whose lines name their file, to count the ones of the turn's files.
|
|
245
|
+
const baselines: Baselines = (gate) => (elsewhere && !namesFiles(gate) ? undefined : turn.baselines[gate.name]);
|
|
232
246
|
const config = gatesOf(turn.config);
|
|
233
247
|
const { gates, refused } = config;
|
|
234
248
|
const missing = missingTools(root, gates);
|
|
235
249
|
for (const gate of gates) if (keepsWatch(gate)) keepWatch(root, gate);
|
|
236
|
-
const
|
|
237
|
-
|
|
238
|
-
root,
|
|
239
|
-
runnable(gates, missing),
|
|
240
|
-
changed,
|
|
241
|
-
from,
|
|
242
|
-
baselines,
|
|
243
|
-
session,
|
|
244
|
-
moved !== undefined,
|
|
245
|
-
);
|
|
246
|
-
cacheReports(root, end, endReports(results, baselines, moved !== undefined));
|
|
250
|
+
const results = await runGates(root, runnable(gates, missing), changed, from, baselines, session, elsewhere);
|
|
251
|
+
cacheReports(root, end, endReports(results, baselines, elsewhere));
|
|
247
252
|
writeTurn(session, root, { ...turn, checked: true });
|
|
253
|
+
if (there) writeThere(session, shared, observed(root, there, kept?.at ?? Date.now()));
|
|
248
254
|
// A compared gate with no report from the turn start can only count its exit code, and a watch has none: a green it
|
|
249
255
|
// did not earn is said.
|
|
250
256
|
const uncompared = results
|
|
@@ -262,7 +268,7 @@ export async function checkTurn(session: string, root: string): Promise<CheckedT
|
|
|
262
268
|
.map(({ gate, why }) => `${gate.name} not run: ${why}`);
|
|
263
269
|
const notes = [
|
|
264
270
|
...(startNote ? [startNote] : []),
|
|
265
|
-
...(
|
|
271
|
+
...(elsewhere ? ["HEAD moved during the turn to code it did not write: only what the turn wrote is checked"] : []),
|
|
266
272
|
...configNote(changedGatesFiles(turn.config, now)),
|
|
267
273
|
...uncompared,
|
|
268
274
|
...refusedNotes(refused),
|
package/src/core/head.ts
CHANGED
|
@@ -1,45 +1,114 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { branchTips, type Commit, commitsPast, hasCommit, isIn } from "./snapshot.ts";
|
|
2
2
|
|
|
3
|
-
// What
|
|
4
|
-
//
|
|
5
|
-
|
|
6
|
-
// A
|
|
7
|
-
|
|
3
|
+
// What tells the code a turn wrote from the code that came with a branch it checked out, pulled, merged or rebased on.
|
|
4
|
+
// Nothing here reads how HEAD moved, which git tells in too many ways: only which commits were there before.
|
|
5
|
+
//
|
|
6
|
+
// A commit was there when a branch held it as the session first started a turn in the repo, when HEAD was on it as a
|
|
7
|
+
// turn of the session started, since the turns before have judged it, or when it was committed before the turn, as
|
|
8
|
+
// one a fetch brought, unless a stop of the session saw it as its own already. Any other commit is one the session
|
|
9
|
+
// made, by whatever command, in whatever worktree: checking too much beats leaving a hole.
|
|
8
10
|
|
|
9
|
-
/** HEAD as
|
|
11
|
+
/** What a turn records of HEAD as it starts: its commit, when the repo has one, and when, in milliseconds. */
|
|
10
12
|
export interface Head {
|
|
11
13
|
head?: string;
|
|
12
|
-
|
|
14
|
+
at?: number;
|
|
13
15
|
}
|
|
14
16
|
|
|
15
|
-
/**
|
|
17
|
+
/**
|
|
18
|
+
* What a session knows of the commits of a repo: the tips of its branches as its first turn there started, with the
|
|
19
|
+
* commit each of its turns started on, and the commits it made since, as its stops saw them.
|
|
20
|
+
*/
|
|
21
|
+
export interface There {
|
|
22
|
+
known: string[];
|
|
23
|
+
made: string[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The code under HEAD that the session did not write, when it is not the one the turn started on. */
|
|
16
27
|
export interface Moved {
|
|
17
|
-
/** The
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
28
|
+
/** The commits that were there, nearest under HEAD: a file like the one any of them holds is not the turn's. */
|
|
29
|
+
under: string[];
|
|
30
|
+
/** The one of them the turn did not start on, when there is one: where a gate given the turn starts from. */
|
|
31
|
+
to?: string;
|
|
32
|
+
/** Whether one of them holds code the turn did not start on, where the others only hold less of it. */
|
|
33
|
+
arrived: boolean;
|
|
22
34
|
}
|
|
23
35
|
|
|
24
36
|
export function headOf(root: string): Head {
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
37
|
+
const { head } = branchTips(root);
|
|
38
|
+
return { ...(head === undefined ? {} : { head }), at: Date.now() };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* What a session knows of a repo as a turn starts there, from what it knew: the commit HEAD is on was there, since
|
|
43
|
+
* a turn that made it has been judged on it. Before its first turn, the tips of the branches were.
|
|
44
|
+
*/
|
|
45
|
+
export function thereOf(root: string, before?: There): There {
|
|
46
|
+
const { head, tips } = branchTips(root);
|
|
47
|
+
if (before === undefined) return { known: tips, made: [] };
|
|
48
|
+
return { known: [...new Set([...before.known, ...(head === undefined ? [] : [head])])], made: before.made };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The commits the session made, among the ones past what it knew: the ones it saw as its own, and the ones of the turn. */
|
|
52
|
+
function madeOf(past: Commit[], { made }: There, at: number): Commit[] {
|
|
53
|
+
const seen = new Set(made);
|
|
54
|
+
const started = Math.floor(at / 1000);
|
|
55
|
+
return past.filter((commit) => seen.has(commit.hash) || commit.at >= started);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* What the session knows once a turn that started then stops: with the commits the turn made, on whatever branch, so
|
|
60
|
+
* that they stay its own in the turns after. Less the known commits git no longer has, as after a deleted branch.
|
|
61
|
+
*/
|
|
62
|
+
export function observed(root: string, there: There, at: number): There {
|
|
63
|
+
const seen = (known: string[]): There => {
|
|
64
|
+
const mine = madeOf(commitsPast(root, branchTips(root).tips, known), there, at);
|
|
65
|
+
return { known, made: [...new Set([...there.made, ...mine.map(({ hash }) => hash)])] };
|
|
66
|
+
};
|
|
67
|
+
try {
|
|
68
|
+
return seen(there.known);
|
|
69
|
+
} catch {
|
|
70
|
+
const kept = there.known.filter((commit) => hasCommit(root, commit));
|
|
71
|
+
try {
|
|
72
|
+
return seen(kept);
|
|
73
|
+
} catch {
|
|
74
|
+
return { known: kept, made: there.made };
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The commits that were there, nearest under a commit: itself, or the ones under the commits the session made. */
|
|
80
|
+
function under(tip: string, made: Map<string, string[]>): string[] {
|
|
81
|
+
const there = new Set<string>();
|
|
82
|
+
const seen = new Set<string>();
|
|
83
|
+
const left = [tip];
|
|
84
|
+
for (let commit = left.pop(); commit !== undefined; commit = left.pop()) {
|
|
85
|
+
if (seen.has(commit)) continue;
|
|
86
|
+
seen.add(commit);
|
|
87
|
+
const parents = made.get(commit);
|
|
88
|
+
if (parents === undefined) there.add(commit);
|
|
89
|
+
else left.push(...parents);
|
|
90
|
+
}
|
|
91
|
+
return [...there];
|
|
28
92
|
}
|
|
29
93
|
|
|
30
94
|
/**
|
|
31
|
-
* Whether
|
|
32
|
-
* a reset. Nothing when it did not,
|
|
95
|
+
* Whether the code under HEAD that the session did not write changed since the turn started, as after a checkout, a
|
|
96
|
+
* pull, a merge, a rebase or a reset. Nothing when it did not, as after a commit of the turn, or a branch it left and
|
|
97
|
+
* came back to; nor when nothing tells any more, where all the turn changed is checked.
|
|
33
98
|
*/
|
|
34
|
-
export function movedSince(root: string, { head,
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
const
|
|
44
|
-
|
|
99
|
+
export function movedSince(root: string, { head, at }: Head, there: There): Moved | undefined {
|
|
100
|
+
const now = branchTips(root).head;
|
|
101
|
+
if (head === undefined || at === undefined || now === undefined || now === head) return undefined;
|
|
102
|
+
let past: Commit[];
|
|
103
|
+
try {
|
|
104
|
+
past = commitsPast(root, [now, head], there.known);
|
|
105
|
+
} catch {
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
const made = new Map(madeOf(past, there, at).map(({ hash, parents }) => [hash, parents]));
|
|
109
|
+
const [before, after] = [under(head, made), under(now, made)];
|
|
110
|
+
if (after.length === before.length && after.every((commit) => before.includes(commit))) return undefined;
|
|
111
|
+
const brought = after.find((commit) => !before.some((had) => isIn(root, commit, had)));
|
|
112
|
+
const to = brought ?? after.find((commit) => !before.includes(commit));
|
|
113
|
+
return { under: after, arrived: brought !== undefined, ...(to === undefined ? {} : { to }) };
|
|
45
114
|
}
|
package/src/core/report.ts
CHANGED
|
@@ -168,14 +168,21 @@ function sent(report: string, omitLines: string | undefined): string {
|
|
|
168
168
|
return kept.some((line) => line.trim()) ? kept.join("\n") : report;
|
|
169
169
|
}
|
|
170
170
|
|
|
171
|
-
// A helper of the engine counts apart the errors of one source line, with `(2)`
|
|
172
|
-
// so that one more of them is new. The agent reads each of them once, with how many
|
|
171
|
+
// A helper of the engine counts apart the errors of one source line, with `(2)` after the source it quotes and before
|
|
172
|
+
// the tab that ends their key, so that one more of them is new. The agent reads each of them once, with how many
|
|
173
|
+
// times it stands. A line of another tool is left as it is.
|
|
174
|
+
const COUNTED_APART = /` \(\d+\)\t/;
|
|
175
|
+
|
|
173
176
|
function saidOnce(report: string): string {
|
|
174
|
-
|
|
177
|
+
if (!COUNTED_APART.test(report)) return report;
|
|
178
|
+
const lines = report.split("\n");
|
|
179
|
+
const keyed = lines.map((line) => line.replace(COUNTED_APART, "`\t"));
|
|
180
|
+
const counted = new Set(keyed.filter((line, at) => line !== lines[at]));
|
|
175
181
|
const times = new Map<string, number>();
|
|
176
|
-
for (const line of
|
|
177
|
-
|
|
178
|
-
|
|
182
|
+
for (const line of keyed) if (counted.has(line)) times.set(line, (times.get(line) ?? 0) + 1);
|
|
183
|
+
const said = new Set<string>();
|
|
184
|
+
return keyed
|
|
185
|
+
.filter((line) => !times.has(line) || (!said.has(line) && said.add(line)))
|
|
179
186
|
.map((line) => ((times.get(line) ?? 1) > 1 ? `${line} (×${times.get(line)})` : line))
|
|
180
187
|
.join("\n");
|
|
181
188
|
}
|
|
@@ -217,13 +224,13 @@ export function toolsToInstall(missing: string[], prompt?: Prompt): string {
|
|
|
217
224
|
|
|
218
225
|
/**
|
|
219
226
|
* 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:
|
|
220
|
-
* each line a helper counts apart said once,
|
|
227
|
+
* less the lines the gate omits, each line a helper counts apart said once, in the characters it has, and in its share of a total when the reports have one.
|
|
221
228
|
*/
|
|
222
229
|
export function reports({ outcomes, wording = {} }: Pick<CheckedTurn, "outcomes" | "wording">): string[] {
|
|
223
230
|
const { gates = [], prompts, maxTotalReportChars = Infinity } = wording;
|
|
224
231
|
const red = outcomes.filter(failed).map(({ name, output, broken }) => {
|
|
225
232
|
const gate = gates.find((known) => known.name === name);
|
|
226
|
-
const report = sent(
|
|
233
|
+
const report = saidOnce(sent(output, gate?.omitLines));
|
|
227
234
|
const told = [
|
|
228
235
|
...(broken ? [worded(BROKEN_GATE, prompts?.brokenGate, "\n")] : []),
|
|
229
236
|
...(gate?.prompt ? [gate.prompt.text] : []),
|
package/src/core/snapshot.ts
CHANGED
|
@@ -8,9 +8,20 @@ interface GitOptions {
|
|
|
8
8
|
input?: string;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
/**
|
|
11
|
+
/**
|
|
12
|
+
* Runs git in the repo, and gives what it printed, without the line break that ends it, however long. Nothing else is
|
|
13
|
+
* trimmed: the name of a file may start with a space.
|
|
14
|
+
*/
|
|
12
15
|
function git(root: string, args: string[], { env = process.env, input }: GitOptions = {}): string {
|
|
13
|
-
|
|
16
|
+
const printed = execFileSync("git", args, {
|
|
17
|
+
cwd: root,
|
|
18
|
+
encoding: "utf8",
|
|
19
|
+
env,
|
|
20
|
+
input,
|
|
21
|
+
stdio: "pipe",
|
|
22
|
+
maxBuffer: Infinity,
|
|
23
|
+
});
|
|
24
|
+
return printed.replace(/\n$/, "");
|
|
14
25
|
}
|
|
15
26
|
|
|
16
27
|
// One NUL after each path, which git otherwise quotes and escapes when it holds an accent.
|
|
@@ -119,29 +130,85 @@ export function changedSince(root: string, start: string, end = workingTree(root
|
|
|
119
130
|
return paths(git(root, ["diff", "-z", "--name-only", start, end])).sort();
|
|
120
131
|
}
|
|
121
132
|
|
|
122
|
-
/** A
|
|
123
|
-
export interface
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
133
|
+
/** A commit, with its parents, and when it was committed, in seconds. */
|
|
134
|
+
export interface Commit {
|
|
135
|
+
hash: string;
|
|
136
|
+
parents: string[];
|
|
137
|
+
at: number;
|
|
127
138
|
}
|
|
128
139
|
|
|
129
|
-
/** The
|
|
130
|
-
export function
|
|
140
|
+
/** The commit HEAD is on, and the tips of the branches: nothing in a repo with no commit. */
|
|
141
|
+
export function branchTips(root: string): { head?: string; tips: string[] } {
|
|
131
142
|
try {
|
|
132
|
-
const
|
|
133
|
-
const
|
|
134
|
-
return
|
|
135
|
-
from: reached[at - 1],
|
|
136
|
-
to: reached[at] ?? "",
|
|
137
|
-
how: entry.slice(entry.indexOf(" ") + 1),
|
|
138
|
-
}));
|
|
143
|
+
const head = git(root, ["rev-parse", "--verify", "--quiet", "HEAD"]);
|
|
144
|
+
const tips = git(root, ["for-each-ref", "--format=%(objectname)", "refs/heads"]).split("\n");
|
|
145
|
+
return { head, tips: [...new Set([head, ...tips.filter(Boolean)])] };
|
|
139
146
|
} catch {
|
|
140
|
-
return [];
|
|
147
|
+
return { tips: [] };
|
|
141
148
|
}
|
|
142
149
|
}
|
|
143
150
|
|
|
144
|
-
/** The
|
|
145
|
-
export function
|
|
146
|
-
|
|
151
|
+
/** The folder of git that the worktrees of a repo share: what tells that two of them are of the same repo. */
|
|
152
|
+
export function sharedDir(root: string): string {
|
|
153
|
+
try {
|
|
154
|
+
return git(root, ["rev-parse", "--path-format=absolute", "--git-common-dir"]);
|
|
155
|
+
} catch {
|
|
156
|
+
return root;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Whether a commit is another one, or one it holds. */
|
|
161
|
+
export function isIn(root: string, commit: string, other: string): boolean {
|
|
162
|
+
try {
|
|
163
|
+
git(root, ["merge-base", "--is-ancestor", commit, other]);
|
|
164
|
+
return true;
|
|
165
|
+
} catch {
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** The commits these tips hold that none of the known commits does. It throws when git no longer has one of them. */
|
|
171
|
+
export function commitsPast(root: string, tips: string[], known: string[]): Commit[] {
|
|
172
|
+
const input = known.map((commit) => `^${commit}\n`).join("");
|
|
173
|
+
// Two lines a commit: "commit", its hash and its parents, then its time.
|
|
174
|
+
const lines = git(root, ["rev-list", "--parents", "--format=%ct", "--stdin", ...tips], { input }).split("\n");
|
|
175
|
+
return lines.filter(Boolean).flatMap((line, at, all) => {
|
|
176
|
+
if (at % 2) return [];
|
|
177
|
+
const [, hash = "", ...parents] = line.split(" ");
|
|
178
|
+
return [{ hash, parents, at: Number(all[at + 1]) }];
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** Lines a change wrote in a file: where they start, and how many. None of them, where it only cut lines out after that one. */
|
|
183
|
+
export interface WrittenLines {
|
|
184
|
+
at: number;
|
|
185
|
+
lines: number;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* The lines of a file the working tree wrote since a commit that has the file. The file is compared with the one of
|
|
190
|
+
* the commit, not through the index, which does not know a file nobody added; and with the diff of git itself, plain,
|
|
191
|
+
* whatever colours or program the owner set for it.
|
|
192
|
+
*/
|
|
193
|
+
export function writtenSince(root: string, commit: string, file: string): WrittenLines[] {
|
|
194
|
+
const plain = ["--no-color", "--no-ext-diff", "--no-textconv", "--unified=0", "--inter-hunk-context=0"];
|
|
195
|
+
// The name as it is: git would otherwise read the brackets of a file such as [slug].ts as a pattern.
|
|
196
|
+
const diff = git(root, ["--literal-pathspecs", "diff", ...plain, `${commit}:${file}`, "--", file]);
|
|
197
|
+
const hunks = diff.matchAll(/^@@ -\S+ \+(\d+)(?:,(\d+))? @@/gm);
|
|
198
|
+
return [...hunks].map(([, at, lines = "1"]) => ({ at: Number(at), lines: Number(lines) }));
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Fewer names at once than a command line holds.
|
|
202
|
+
const NAMES_AT_ONCE = 500;
|
|
203
|
+
|
|
204
|
+
/** The ones of these files a commit has. */
|
|
205
|
+
export function filesAt(root: string, commit: string, files: string[]): string[] {
|
|
206
|
+
const found: string[] = [];
|
|
207
|
+
for (let at = 0; at < files.length; at += NAMES_AT_ONCE) {
|
|
208
|
+
const names = files.slice(at, at + NAMES_AT_ONCE);
|
|
209
|
+
found.push(
|
|
210
|
+
...paths(git(root, ["--literal-pathspecs", "ls-tree", "-r", "--name-only", "-z", commit, "--", ...names])),
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
return found;
|
|
147
214
|
}
|
package/src/core/turn-state.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { writeFileSync } from "node:fs";
|
|
|
2
2
|
import { tmpdir } from "node:os";
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import type { GatesFiles } from "./config.ts";
|
|
5
|
-
import type { Head } from "./head.ts";
|
|
5
|
+
import type { Head, There } from "./head.ts";
|
|
6
6
|
import { isRecord, isStringArray, isStringRecord, readJsonFile } from "./json.ts";
|
|
7
7
|
|
|
8
8
|
/** A red gate the agent chose to leave, with why, bound to the fingerprint of the report it saw. */
|
|
@@ -53,6 +53,8 @@ interface SessionState {
|
|
|
53
53
|
told?: Record<string, string[]>;
|
|
54
54
|
/** The repos a send-back of the session told the agent how to act in: the next ones there say it in one line. */
|
|
55
55
|
conductTold?: string[];
|
|
56
|
+
/** What the session knows of the commits of each repo, for as long as it lasts, whatever repo its turns are in. */
|
|
57
|
+
there?: Record<string, There>;
|
|
56
58
|
}
|
|
57
59
|
|
|
58
60
|
const EMPTY: SessionState = { turns: {} };
|
|
@@ -80,7 +82,7 @@ function isTurnState(value: unknown): value is TurnState {
|
|
|
80
82
|
isRecord(value) &&
|
|
81
83
|
typeof value.start === "string" &&
|
|
82
84
|
(value.head === undefined || typeof value.head === "string") &&
|
|
83
|
-
(value.
|
|
85
|
+
(value.at === undefined || typeof value.at === "number") &&
|
|
84
86
|
isStringRecord(value.baselines) &&
|
|
85
87
|
isGatesFiles(value.config) &&
|
|
86
88
|
typeof value.checked === "boolean" &&
|
|
@@ -88,6 +90,10 @@ function isTurnState(value: unknown): value is TurnState {
|
|
|
88
90
|
);
|
|
89
91
|
}
|
|
90
92
|
|
|
93
|
+
function isThere(value: unknown): value is There {
|
|
94
|
+
return isRecord(value) && isStringArray(value.known) && isStringArray(value.made);
|
|
95
|
+
}
|
|
96
|
+
|
|
91
97
|
function isSessionState(value: unknown): value is SessionState {
|
|
92
98
|
return (
|
|
93
99
|
isRecord(value) &&
|
|
@@ -95,7 +101,8 @@ function isSessionState(value: unknown): value is SessionState {
|
|
|
95
101
|
isRecord(value.turns) &&
|
|
96
102
|
Object.values(value.turns).every(isTurnState) &&
|
|
97
103
|
(value.told === undefined || (isRecord(value.told) && Object.values(value.told).every(isStringArray))) &&
|
|
98
|
-
(value.conductTold === undefined || isStringArray(value.conductTold))
|
|
104
|
+
(value.conductTold === undefined || isStringArray(value.conductTold)) &&
|
|
105
|
+
(value.there === undefined || (isRecord(value.there) && Object.values(value.there).every(isThere)))
|
|
99
106
|
);
|
|
100
107
|
}
|
|
101
108
|
|
|
@@ -123,6 +130,16 @@ export function writeTurn(session: string, root: string, turn: TurnState): void
|
|
|
123
130
|
writeSession(session, { ...state, current: root, turns: { ...Object.fromEntries(unchecked), [root]: turn } });
|
|
124
131
|
}
|
|
125
132
|
|
|
133
|
+
/** What the session knows of the commits of a repo, or nothing before its first turn there. */
|
|
134
|
+
export function readThere(session: string, root: string): There | undefined {
|
|
135
|
+
return readSession(session).there?.[root];
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function writeThere(session: string, root: string, there: There): void {
|
|
139
|
+
const state = readSession(session);
|
|
140
|
+
writeSession(session, { ...state, there: { ...state.there, [root]: there } });
|
|
141
|
+
}
|
|
142
|
+
|
|
126
143
|
/** Ends the turn in progress without a check, as a prompt from a folder with no gates, or a failed start, does. */
|
|
127
144
|
export function forgetTurn(session: string): void {
|
|
128
145
|
forget(session, "current");
|
|
@@ -1,33 +1,112 @@
|
|
|
1
1
|
import { execFileSync } from "node:child_process";
|
|
2
|
-
import { mkdtempSync, rmSync } from "node:fs";
|
|
2
|
+
import { mkdtempSync, readFileSync, 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.ts";
|
|
6
6
|
import { headed, packageScript } from "../core/run.ts";
|
|
7
|
+
import { filesAt, type WrittenLines, writtenSince } from "../core/snapshot.ts";
|
|
7
8
|
|
|
8
9
|
// jscpd compared with the tree the turn started from. Its own console report lists every clone of the repo, the old
|
|
9
10
|
// ones included, so this prints only the clones the turn added, one per line.
|
|
11
|
+
//
|
|
12
|
+
// jscpd tells a clone by its two files and where it stands in them, so it also calls new an old clone the turn only
|
|
13
|
+
// disturbed: two old files it pairs once the turn takes their copy out of a third, or a clone the turn cut in two by a
|
|
14
|
+
// line written in its middle. A clone is the turn's only when the turn wrote in one of its sides.
|
|
10
15
|
|
|
11
16
|
function refuse(): never {
|
|
12
17
|
throw new Error("Not a jscpd report.");
|
|
13
18
|
}
|
|
14
|
-
|
|
19
|
+
|
|
20
|
+
/** A place in a file, as jscpd counts: lines from 1, columns from 0. */
|
|
21
|
+
interface Place {
|
|
22
|
+
line: number;
|
|
23
|
+
column: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** One of the two sides of a clone: its file, and where it starts and ends, between two tokens. */
|
|
27
|
+
interface Side {
|
|
28
|
+
name: string;
|
|
29
|
+
start: Place;
|
|
30
|
+
end: Place;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function placeOf(value: unknown, line: number, column: number): Place {
|
|
34
|
+
if (isRecord(value) && typeof value.line === "number" && typeof value.column === "number")
|
|
35
|
+
return { line: value.line, column: value.column };
|
|
36
|
+
return { line, column };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Without the columns of a clone, as from another jscpd, its first and last lines count as whole.
|
|
40
|
+
function sideOf(value: unknown): Side {
|
|
15
41
|
if (!isRecord(value)) refuse();
|
|
16
|
-
const { name, start, end } = value;
|
|
42
|
+
const { name, start, end, startLoc, endLoc } = value;
|
|
17
43
|
if (typeof name !== "string" || typeof start !== "number" || typeof end !== "number") refuse();
|
|
18
|
-
return
|
|
44
|
+
return { name, start: placeOf(startLoc, start, 0), end: placeOf(endLoc, end, Infinity) };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Whether the turn wrote in a side of a clone: a line it wrote stands whole in it, or it cut lines out of its middle.
|
|
49
|
+
* A clone starts and ends between two tokens, often inside a line: a line it only starts or ends in is not in it, nor
|
|
50
|
+
* an empty one, which jscpd does not read.
|
|
51
|
+
*/
|
|
52
|
+
export function wroteIn({ start, end }: Pick<Side, "start" | "end">, text: string, written: WrittenLines[]): boolean {
|
|
53
|
+
const lines = text.split("\n");
|
|
54
|
+
const whole = (at: number) => {
|
|
55
|
+
const line = lines[at - 1] ?? "";
|
|
56
|
+
if (!line.trim()) return false;
|
|
57
|
+
const starts = at > start.line || (at === start.line && start.column <= line.length - line.trimStart().length);
|
|
58
|
+
const ends = at < end.line || (at === end.line && end.column >= line.trimEnd().length);
|
|
59
|
+
return starts && ends;
|
|
60
|
+
};
|
|
61
|
+
return written.some(({ at, lines: count }) =>
|
|
62
|
+
count === 0 ? start.line <= at && at < end.line : Array.from({ length: count }, (_, more) => at + more).some(whole),
|
|
63
|
+
);
|
|
19
64
|
}
|
|
20
65
|
|
|
21
|
-
|
|
66
|
+
/** The clones jscpd calls new, each as its two sides. */
|
|
67
|
+
function newPairs(report: unknown): Side[][] {
|
|
22
68
|
if (!isRecord(report) || !Array.isArray(report.duplicates)) refuse();
|
|
23
69
|
return report.duplicates.flatMap((clone: unknown) => {
|
|
24
70
|
if (!isRecord(clone)) refuse();
|
|
25
|
-
const
|
|
26
|
-
return clone.isNew === true ? [
|
|
71
|
+
const sides = [sideOf(clone.firstFile), sideOf(clone.secondFile)];
|
|
72
|
+
return clone.isNew === true ? [sides] : [];
|
|
27
73
|
});
|
|
28
74
|
}
|
|
29
75
|
|
|
30
|
-
|
|
76
|
+
/** The clones jscpd calls new, one per line, less the ones the turn wrote in neither side of. */
|
|
77
|
+
export function newClones(report: unknown, wrote: (side: Side) => boolean = () => true): string[] {
|
|
78
|
+
return newPairs(report)
|
|
79
|
+
.filter((sides) => sides.some(wrote))
|
|
80
|
+
.map((sides) => sides.map(({ name, start, end }) => `${name}:${start.line}-${end.line}`).join(" ~ "));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Whether the turn wrote in a side of a clone, for the clones of a report. Each file is read once, however many
|
|
85
|
+
* clones it has: the whole check finds every clone of the repo new.
|
|
86
|
+
*/
|
|
87
|
+
function turnWrote(root: string, turn: string, report: unknown): (side: Side) => boolean {
|
|
88
|
+
const names = [...new Set(newPairs(report).flatMap((sides) => sides.map(({ name }) => name)))];
|
|
89
|
+
const old = new Set(filesAt(root, turn, names));
|
|
90
|
+
const files = new Map<string, { text: string; written: WrittenLines[] }>();
|
|
91
|
+
return (side) => {
|
|
92
|
+
// A file the turn created is all its own, and one jscpd names from another folder is unknown here: checking too
|
|
93
|
+
// much beats leaving a hole.
|
|
94
|
+
if (!old.has(side.name)) return true;
|
|
95
|
+
try {
|
|
96
|
+
const file = files.get(side.name) ?? {
|
|
97
|
+
text: readFileSync(join(root, side.name), "utf8"),
|
|
98
|
+
written: writtenSince(root, turn, side.name),
|
|
99
|
+
};
|
|
100
|
+
files.set(side.name, file);
|
|
101
|
+
return wroteIn(side, file.text, file.written);
|
|
102
|
+
} catch {
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The clones the turn added to the repo, as jscpd finds them. It throws what jscpd said when jscpd fails. */
|
|
109
|
+
export function turnClones(root: string, turn: string): string[] {
|
|
31
110
|
const jscpd = packageScript(root, "jscpd");
|
|
32
111
|
if (jscpd === undefined)
|
|
33
112
|
throw new Error("jscpd is not installed in node_modules: install the dependencies of the repo.");
|
|
@@ -42,17 +121,28 @@ function main(root: string, turn: string): void {
|
|
|
42
121
|
} catch (error) {
|
|
43
122
|
// What jscpd said, without the stack of this script around it.
|
|
44
123
|
const said = isRecord(error) && typeof error.stderr === "string" ? error.stderr.trim() : String(error);
|
|
45
|
-
|
|
46
|
-
process.exitCode = 1;
|
|
47
|
-
return;
|
|
124
|
+
throw new Error(`jscpd failed: ${said}`, { cause: error });
|
|
48
125
|
}
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
console.log(headed("New clones: reuse the code already there, or factor it out.", clones));
|
|
52
|
-
process.exitCode = 1;
|
|
126
|
+
const report = readJsonFile(join(output, "jscpd-report.json"));
|
|
127
|
+
return newClones(report, turnWrote(root, turn, report));
|
|
53
128
|
} finally {
|
|
54
129
|
rmSync(output, { recursive: true, force: true });
|
|
55
130
|
}
|
|
56
131
|
}
|
|
57
132
|
|
|
133
|
+
function main(root: string, turn: string): void {
|
|
134
|
+
let clones: string[];
|
|
135
|
+
try {
|
|
136
|
+
clones = turnClones(root, turn);
|
|
137
|
+
} catch (error) {
|
|
138
|
+
if (!(error instanceof Error) || !error.message.startsWith("jscpd failed: ")) throw error;
|
|
139
|
+
console.log(error.message);
|
|
140
|
+
process.exitCode = 1;
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (!clones.length) return;
|
|
144
|
+
console.log(headed("New clones: reuse the code already there, or factor it out.", clones));
|
|
145
|
+
process.exitCode = 1;
|
|
146
|
+
}
|
|
147
|
+
|
|
58
148
|
if (import.meta.main) main(process.cwd(), process.argv[2] ?? "HEAD");
|