@tayomi/deterministic-gates 0.8.1 → 0.9.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/index.html +23 -4
- package/catalog/typescript.json +12 -2
- package/dist/src/cli.js +8 -5
- package/dist/src/core/check.js +11 -8
- package/dist/src/core/config.js +69 -5
- package/dist/src/core/report.js +106 -21
- package/dist/src/core/run.js +22 -6
- package/dist/src/core/snapshot.js +15 -0
- package/dist/src/core/turn-state.js +26 -8
- package/dist/src/core/watch.js +37 -12
- package/dist/src/helpers/angular-errors.js +28 -3
- package/dist/src/helpers/bulk-suppressions.js +73 -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 +30 -8
- package/src/core/config.ts +96 -3
- package/src/core/report.ts +127 -21
- package/src/core/run.ts +30 -6
- package/src/core/snapshot.ts +15 -0
- package/src/core/turn-state.ts +30 -7
- package/src/core/watch.ts +36 -11
- package/src/helpers/angular-errors.ts +25 -5
- package/src/helpers/bulk-suppressions.ts +90 -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/src/core/watch.ts
CHANGED
|
@@ -61,13 +61,18 @@ export function keepWatch(root: string, gate: Gate): void {
|
|
|
61
61
|
if (existsSync(folder)) touch(folder);
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
+
// What the folders of the watches of a gate of a repo start with, whatever version of the engine started them.
|
|
65
|
+
function watchesOf(root: string, gate: Gate): string {
|
|
66
|
+
const key = createHash("sha256").update(`${root}\0${gate.name}`).digest("hex").slice(0, 16);
|
|
67
|
+
return `${PREFIX}${key}-`;
|
|
68
|
+
}
|
|
69
|
+
|
|
64
70
|
/**
|
|
65
71
|
* Where a watch keeps its process, its output and the sessions that use it, outside the repo: one per repo, gate and
|
|
66
72
|
* engine version, since a helper of the engine may print its cycles otherwise from one version to the next.
|
|
67
73
|
*/
|
|
68
74
|
function folderOf(root: string, gate: Gate): string {
|
|
69
|
-
|
|
70
|
-
return join(tmpdir(), `${PREFIX}${key}`);
|
|
75
|
+
return join(tmpdir(), `${watchesOf(root, gate)}${ENGINE_VERSION}`);
|
|
71
76
|
}
|
|
72
77
|
|
|
73
78
|
// A file per session that uses the watch: two hooks at once each write their own, and neither loses the other's.
|
|
@@ -75,9 +80,13 @@ function sessionFile(folder: string, session: string): string {
|
|
|
75
80
|
return join(folder, "sessions", session.replace(/[^\w-]/g, ""));
|
|
76
81
|
}
|
|
77
82
|
|
|
78
|
-
|
|
83
|
+
/** Records that the session uses the watch, and says whether it is the first time it does. */
|
|
84
|
+
function register(folder: string, session: string): boolean {
|
|
85
|
+
const file = sessionFile(folder, session);
|
|
86
|
+
if (existsSync(file)) return false;
|
|
79
87
|
mkdirSync(join(folder, "sessions"), { recursive: true });
|
|
80
|
-
writeFileSync(
|
|
88
|
+
writeFileSync(file, "");
|
|
89
|
+
return true;
|
|
81
90
|
}
|
|
82
91
|
|
|
83
92
|
/** The process of the watch, as the engine recorded it when it started it, or nothing when it never did. */
|
|
@@ -130,7 +139,7 @@ export function startWatch(session: string, root: string, gate: WatchedGate): vo
|
|
|
130
139
|
mkdirSync(folder, { recursive: true });
|
|
131
140
|
touch(folder);
|
|
132
141
|
// The sessions of a watch that starts again still use it.
|
|
133
|
-
register(folder, session);
|
|
142
|
+
if (register(folder, session)) leaveOtherVersions(session, root, gate);
|
|
134
143
|
if (stateOf(folder) === "running" || !lockStart(folder)) return;
|
|
135
144
|
try {
|
|
136
145
|
// Another hook may have started it between the look above and the lock.
|
|
@@ -140,6 +149,25 @@ export function startWatch(session: string, root: string, gate: WatchedGate): vo
|
|
|
140
149
|
}
|
|
141
150
|
}
|
|
142
151
|
|
|
152
|
+
/**
|
|
153
|
+
* The session leaves the watches other versions of the engine started for this gate of this repo, now that it uses
|
|
154
|
+
* the one of this version: after an update of the engine, the watch of the version before would otherwise run beside
|
|
155
|
+
* the new one, until the session ends or it stops for idleness. A session still on that version keeps its watch, and
|
|
156
|
+
* the watch of another repo, such as another worktree, is not one of them.
|
|
157
|
+
*/
|
|
158
|
+
function leaveOtherVersions(session: string, root: string, gate: Gate): void {
|
|
159
|
+
const tmp = tmpdir();
|
|
160
|
+
const prefix = watchesOf(root, gate);
|
|
161
|
+
for (const entry of readDir(tmp))
|
|
162
|
+
if (entry.startsWith(prefix) && entry !== `${prefix}${ENGINE_VERSION}`) leave(join(tmp, entry), session);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** The session no longer uses the watch, which stops once no session does: the watch and every process it started. */
|
|
166
|
+
function leave(folder: string, session: string): void {
|
|
167
|
+
rmSync(sessionFile(folder, session), { force: true });
|
|
168
|
+
if (!readDir(join(folder, "sessions")).length) stop(folder);
|
|
169
|
+
}
|
|
170
|
+
|
|
143
171
|
/** The files of the repo the watch lists only as it starts, one per line. */
|
|
144
172
|
function frozenFiles(root: string, watch: Watch): string {
|
|
145
173
|
if (watch.frozen === undefined) return "";
|
|
@@ -362,7 +390,7 @@ function readDir(folder: string): string[] {
|
|
|
362
390
|
* the gate: out of time, neither green nor red, never judged on an older cycle. The watch then owes a cycle for these
|
|
363
391
|
* files, which the next stop it answers asks for with its own: a change it missed would otherwise stay unseen until
|
|
364
392
|
* the file changes again. Only a stop pays that debt, never the report of a turn start, which would make old a
|
|
365
|
-
* failure no turn was judged on. A watch that stopped
|
|
393
|
+
* failure no turn was judged on. A watch that stopped is a broken gate, with the last lines it printed.
|
|
366
394
|
*/
|
|
367
395
|
export async function judgeWatch(
|
|
368
396
|
session: string,
|
|
@@ -377,7 +405,7 @@ export async function judgeWatch(
|
|
|
377
405
|
const settled = await settle(session, root, gate, asked, gate.timeoutSeconds * 1000);
|
|
378
406
|
const { name } = gate;
|
|
379
407
|
const { ms } = settled;
|
|
380
|
-
if ("stopped" in settled) return { name, ok: false, ms, output: settled.stopped, report: undefined };
|
|
408
|
+
if ("stopped" in settled) return { name, ok: false, ms, output: settled.stopped, report: undefined, broken: true };
|
|
381
409
|
if (!("cycle" in settled)) {
|
|
382
410
|
owe(folder, files);
|
|
383
411
|
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.`;
|
|
@@ -416,10 +444,7 @@ export function endSession(session: string): void {
|
|
|
416
444
|
for (const entry of readdirSync(tmpdir())) {
|
|
417
445
|
if (!entry.startsWith(PREFIX)) continue;
|
|
418
446
|
const folder = join(tmpdir(), entry);
|
|
419
|
-
|
|
420
|
-
if (!existsSync(file)) continue;
|
|
421
|
-
rmSync(file, { force: true });
|
|
422
|
-
if (!readdirSync(join(folder, "sessions")).length) stop(folder);
|
|
447
|
+
if (existsSync(sessionFile(folder, session))) leave(folder, session);
|
|
423
448
|
}
|
|
424
449
|
}
|
|
425
450
|
|
|
@@ -15,7 +15,8 @@ import { strictErrorLines } from "./type-errors.ts";
|
|
|
15
15
|
//
|
|
16
16
|
// With --watch, it stays, and checks again each time the date of its tsconfig changes, which is how the engine asks
|
|
17
17
|
// a watch for a cycle. It follows no other file: a check reads the files as they are when it starts, the ones whose
|
|
18
|
-
// date changed since the check before it, so it never misses a change, and it reuses the program of that check.
|
|
18
|
+
// date changed since the check before it, so it never misses a change, and it reuses the program of that check. When
|
|
19
|
+
// no file that check read has changed, it says what that check said, without checking again.
|
|
19
20
|
|
|
20
21
|
/** A diagnostic of the Angular compiler, as its API gives it. */
|
|
21
22
|
export interface Diagnostic {
|
|
@@ -110,7 +111,14 @@ export async function compilerOf(root: string, args: string[]): Promise<{ compil
|
|
|
110
111
|
// is how the compiler knows it need not check it again.
|
|
111
112
|
const sources = new Map<string, { at: number; source: unknown }>();
|
|
112
113
|
const resources = new Map<string, number>();
|
|
113
|
-
let last:
|
|
114
|
+
let last:
|
|
115
|
+
{ program: unknown; options: string; roots: string; reused: boolean; checked: readonly Diagnostic[] } | undefined;
|
|
116
|
+
// No file the check before this one read has another date: it would say the same.
|
|
117
|
+
const unchanged = () => {
|
|
118
|
+
for (const [file, { at }] of sources) if (changedAt(file) !== at) return false;
|
|
119
|
+
for (const [file, at] of resources) if (changedAt(file) !== at) return false;
|
|
120
|
+
return true;
|
|
121
|
+
};
|
|
114
122
|
return {
|
|
115
123
|
compiler: {
|
|
116
124
|
async diagnostics() {
|
|
@@ -122,10 +130,18 @@ export async function compilerOf(root: string, args: string[]): Promise<{ compil
|
|
|
122
130
|
sources.clear();
|
|
123
131
|
resources.clear();
|
|
124
132
|
}
|
|
133
|
+
// The check before this one stands when nothing it read has changed, which saves checking every file again.
|
|
134
|
+
// Never the first check of a compiler: TypeScript reports some errors once per type, in the first file it
|
|
135
|
+
// checks, and that order is another in a program that reuses one, so a first check served again would be
|
|
136
|
+
// compared with the checks after it, and an error would look new for having moved.
|
|
137
|
+
const roots = JSON.stringify(config.rootNames);
|
|
138
|
+
if (last?.reused && last.roots === roots && unchanged()) return [...config.errors, ...last.checked];
|
|
125
139
|
const base = ng.createCompilerHost({ options });
|
|
140
|
+
const asked = new Set<string>();
|
|
126
141
|
const host = {
|
|
127
142
|
...base,
|
|
128
143
|
getSourceFile(file: string, ...rest: unknown[]) {
|
|
144
|
+
asked.add(file);
|
|
129
145
|
const at = changedAt(file);
|
|
130
146
|
const kept = sources.get(file);
|
|
131
147
|
if (kept?.at === at) return kept.source;
|
|
@@ -150,15 +166,19 @@ export async function compilerOf(root: string, args: string[]): Promise<{ compil
|
|
|
150
166
|
return modified;
|
|
151
167
|
},
|
|
152
168
|
};
|
|
169
|
+
const reused = last !== undefined;
|
|
153
170
|
const program = new ng.NgtscProgram(config.rootNames, options, host, last?.program);
|
|
154
171
|
await program.loadNgStructureAsync();
|
|
155
|
-
|
|
156
|
-
return [
|
|
157
|
-
...config.errors,
|
|
172
|
+
const checked: readonly Diagnostic[] = [
|
|
158
173
|
...program.getTsSyntacticDiagnostics(),
|
|
159
174
|
...program.getTsSemanticDiagnostics(),
|
|
160
175
|
...program.getNgSemanticDiagnostics(),
|
|
161
176
|
];
|
|
177
|
+
// A file the program no longer reads is forgotten once it changes: kept with its old date, it would make
|
|
178
|
+
// every check to come a whole one.
|
|
179
|
+
for (const [file, { at }] of sources) if (!asked.has(file) && changedAt(file) !== at) sources.delete(file);
|
|
180
|
+
last = { program, options: JSON.stringify(options), roots, reused, checked };
|
|
181
|
+
return [...config.errors, ...checked];
|
|
162
182
|
},
|
|
163
183
|
},
|
|
164
184
|
};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { isRecord, messageOf, parseJson } from "../core/json.ts";
|
|
4
|
+
import { headed } from "../core/run.ts";
|
|
5
|
+
import { fileAt } from "../core/snapshot.ts";
|
|
6
|
+
|
|
7
|
+
// ESLint keeps the violations a repo chose to live with in a suppressions file, a count per file and rule, and
|
|
8
|
+
// `eslint --suppress-all` writes every violation of now into it: the lint turns green with no comment in the code.
|
|
9
|
+
// This prints the counts the turn raised in the suppressions files it changed, compared with the turn start.
|
|
10
|
+
|
|
11
|
+
/** The count of each rule in each file, as ESLint writes them: { "src/a.ts": { "no-console": { "count": 2 } } }. */
|
|
12
|
+
type Counts = Map<string, Map<string, number>>;
|
|
13
|
+
|
|
14
|
+
function countsOf(file: unknown): Counts {
|
|
15
|
+
const refuse = (): never => {
|
|
16
|
+
throw new Error("Not an ESLint suppressions file.");
|
|
17
|
+
};
|
|
18
|
+
if (!isRecord(file)) return refuse();
|
|
19
|
+
return new Map(
|
|
20
|
+
Object.entries(file).map(([source, rules]) => {
|
|
21
|
+
if (!isRecord(rules)) return refuse();
|
|
22
|
+
const counted = Object.entries(rules).map(([rule, entry]): [string, number] => {
|
|
23
|
+
if (!isRecord(entry) || typeof entry.count !== "number") return refuse();
|
|
24
|
+
return [rule, entry.count];
|
|
25
|
+
});
|
|
26
|
+
return [source, new Map(counted)];
|
|
27
|
+
}),
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function totals(counts: Counts): Map<string, number> {
|
|
32
|
+
const total = new Map<string, number>();
|
|
33
|
+
for (const rules of counts.values())
|
|
34
|
+
for (const [rule, count] of rules) total.set(rule, (total.get(rule) ?? 0) + count);
|
|
35
|
+
return total;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The counts that rose, one line per file and rule. A file the turn moved takes its counts to another path: there, a
|
|
40
|
+
* count that rose is new only when the suppressions file holds more of its rule than as the turn started.
|
|
41
|
+
*/
|
|
42
|
+
export function newBulkSuppressions(
|
|
43
|
+
before: unknown,
|
|
44
|
+
after: unknown,
|
|
45
|
+
moved: (source: string) => boolean = () => false,
|
|
46
|
+
): string[] {
|
|
47
|
+
const was = countsOf(before ?? {});
|
|
48
|
+
const now = countsOf(after);
|
|
49
|
+
const [wasTotal, nowTotal] = [totals(was), totals(now)];
|
|
50
|
+
return [...now].flatMap(([source, rules]) =>
|
|
51
|
+
[...rules].flatMap(([rule, count]) => {
|
|
52
|
+
const old = was.get(source)?.get(rule) ?? 0;
|
|
53
|
+
if (count <= old) return [];
|
|
54
|
+
// Asked last: it costs a look at the turn start.
|
|
55
|
+
const cameWithIt = (nowTotal.get(rule) ?? 0) <= (wasTotal.get(rule) ?? 0) && moved(source);
|
|
56
|
+
return cameWithIt ? [] : [`${source}: ${rule} ${old} -> ${count}`];
|
|
57
|
+
}),
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function main(root: string, turn: string, files: string[]): void {
|
|
62
|
+
const found = files.flatMap((file) => {
|
|
63
|
+
const before = fileAt(root, turn, file);
|
|
64
|
+
// ESLint names a source from the folder it runs in, which holds its suppressions file unless the repo moved it:
|
|
65
|
+
// a source not found there is never taken for a moved one.
|
|
66
|
+
const moved = (source: string) => {
|
|
67
|
+
const path = join(dirname(file), source);
|
|
68
|
+
return existsSync(join(root, path)) && fileAt(root, turn, path) === undefined;
|
|
69
|
+
};
|
|
70
|
+
const rose = newBulkSuppressions(
|
|
71
|
+
before === undefined ? undefined : parseJson(before),
|
|
72
|
+
parseJson(readFileSync(file, "utf8")),
|
|
73
|
+
moved,
|
|
74
|
+
);
|
|
75
|
+
return rose.map((line) => `${file}: ${line}`);
|
|
76
|
+
});
|
|
77
|
+
if (!found.length) return;
|
|
78
|
+
console.log(headed("New bulk suppressions: fix the violations, never the file that hides them.", found));
|
|
79
|
+
process.exitCode = 1;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (import.meta.main) {
|
|
83
|
+
try {
|
|
84
|
+
main(process.cwd(), process.argv[2] ?? "HEAD", process.argv.slice(3));
|
|
85
|
+
} catch (error) {
|
|
86
|
+
// What is wrong with the file, without the stack of this script around it.
|
|
87
|
+
console.log(messageOf(error));
|
|
88
|
+
process.exitCode = 1;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -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.ts";
|
|
6
|
-
import { packageScript } from "../core/run.ts";
|
|
6
|
+
import { headed, packageScript } from "../core/run.ts";
|
|
7
7
|
|
|
8
8
|
// jscpd compared with the tree the turn started from. Its own console report lists every clone of the repo, the old
|
|
9
9
|
// ones included, so this prints only the clones the turn added, one per line.
|
|
@@ -48,7 +48,7 @@ function main(root: string, turn: string): void {
|
|
|
48
48
|
}
|
|
49
49
|
const clones = newClones(readJsonFile(join(output, "jscpd-report.json")));
|
|
50
50
|
if (!clones.length) return;
|
|
51
|
-
console.log(
|
|
51
|
+
console.log(headed("New clones: reuse the code already there, or factor it out.", clones));
|
|
52
52
|
process.exitCode = 1;
|
|
53
53
|
} finally {
|
|
54
54
|
rmSync(output, { recursive: true, force: true });
|
|
@@ -1,43 +1,54 @@
|
|
|
1
|
-
import { execFileSync } from "node:child_process";
|
|
2
1
|
import { readFileSync } from "node:fs";
|
|
2
|
+
import { headed } from "../core/run.ts";
|
|
3
|
+
import { fileAt } from "../core/snapshot.ts";
|
|
3
4
|
|
|
4
5
|
// The cheapest green is to silence the rule. This prints the rule suppressions the turn added to the changed files,
|
|
5
6
|
// one per line, compared with the file as the turn started: the ones already there are the owner's business.
|
|
6
7
|
|
|
7
8
|
/** The ways to silence a rule in the languages of the catalog: linters, type checkers, formatters, scanners. */
|
|
8
9
|
const SUPPRESSIONS =
|
|
9
|
-
/eslint-disable|@ts-(ignore|expect-error|nocheck)|prettier-ignore|biome-ignore|\bnoqa\b|type:\s*ignore|pylint:\s*disable|fmt:\s*(off|skip)|\bnosec\b|\bnolint\b|go:nolint|coverage:\s*ignore|pragma:\s*no cover/;
|
|
10
|
+
/eslint-disable|stylelint-disable|@ts-(ignore|expect-error|nocheck)|prettier-ignore|biome-ignore|\bnoqa\b|type:\s*ignore|pylint:\s*disable|fmt:\s*(off|skip)|\bnosec\b|\bnolint\b|go:nolint|coverage:\s*ignore|pragma:\s*no cover/;
|
|
10
11
|
|
|
11
12
|
export interface Suppression {
|
|
12
13
|
line: number;
|
|
13
14
|
text: string;
|
|
14
15
|
}
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
return line && !seen.has(line) && SUPPRESSIONS.test(line) ? [{ line: index + 1, text: line }] : [];
|
|
22
|
-
});
|
|
17
|
+
// A suppression from where it starts to the end of its line, without the code before it: a change of that code does
|
|
18
|
+
// not make it new.
|
|
19
|
+
function suppressionOf(line: string): string | undefined {
|
|
20
|
+
const found = SUPPRESSIONS.exec(line);
|
|
21
|
+
return found ? line.slice(found.index).trim() : undefined;
|
|
23
22
|
}
|
|
24
23
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
/**
|
|
25
|
+
* The suppressions the file holds now and did not hold as the turn started. Each one of the turn start stands for one
|
|
26
|
+
* of now that says the same, so a second one beside it is new.
|
|
27
|
+
*/
|
|
28
|
+
export function newSuppressions(before: string | undefined, after: string): Suppression[] {
|
|
29
|
+
const held = new Map<string, number>();
|
|
30
|
+
for (const line of (before ?? "").split("\n")) {
|
|
31
|
+
const suppression = suppressionOf(line);
|
|
32
|
+
if (suppression !== undefined) held.set(suppression, (held.get(suppression) ?? 0) + 1);
|
|
30
33
|
}
|
|
34
|
+
return after.split("\n").flatMap((text, index) => {
|
|
35
|
+
const suppression = suppressionOf(text);
|
|
36
|
+
if (suppression === undefined) return [];
|
|
37
|
+
const left = held.get(suppression) ?? 0;
|
|
38
|
+
if (left === 0) return [{ line: index + 1, text: text.trim() }];
|
|
39
|
+
held.set(suppression, left - 1);
|
|
40
|
+
return [];
|
|
41
|
+
});
|
|
31
42
|
}
|
|
32
43
|
|
|
33
44
|
function main(root: string, turn: string, files: string[]): void {
|
|
34
45
|
const found = files.flatMap((file) =>
|
|
35
|
-
newSuppressions(
|
|
46
|
+
newSuppressions(fileAt(root, turn, file), readFileSync(file, "utf8")).map(
|
|
36
47
|
({ line, text }) => `${file}:${line}: ${text}`,
|
|
37
48
|
),
|
|
38
49
|
);
|
|
39
50
|
if (!found.length) return;
|
|
40
|
-
console.log(
|
|
51
|
+
console.log(headed("New rule suppressions: fix the code, never the check.", found));
|
|
41
52
|
process.exitCode = 1;
|
|
42
53
|
}
|
|
43
54
|
|
package/src/hook/hook.ts
CHANGED
|
@@ -1,10 +1,19 @@
|
|
|
1
1
|
import { type CheckedTurn, checkTurn, startSession, startTurn } from "../core/check.ts";
|
|
2
|
-
import { configuredRoot, gatesOf } from "../core/config.ts";
|
|
2
|
+
import { configuredRoot, gatesOf, readGatesFiles } from "../core/config.ts";
|
|
3
3
|
import { isRecord, messageOf, parseJson } from "../core/json.ts";
|
|
4
4
|
import { broken, failures, summary, toolsMissing, toolsToInstall } from "../core/report.ts";
|
|
5
5
|
import { type Judged, judge, waivesOf } from "../core/send-back.ts";
|
|
6
6
|
import { endSession } from "../core/watch.ts";
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
forgetConduct,
|
|
9
|
+
forgetTold,
|
|
10
|
+
forgetTurn,
|
|
11
|
+
readTurn,
|
|
12
|
+
tellConductOnce,
|
|
13
|
+
tellOnce,
|
|
14
|
+
turnRoot,
|
|
15
|
+
writeTurn,
|
|
16
|
+
} from "../core/turn-state.ts";
|
|
8
17
|
|
|
9
18
|
export interface HookInput {
|
|
10
19
|
session_id?: string;
|
|
@@ -72,10 +81,11 @@ function told({ count, max, waived, fallen, unknown, gates }: Judged): string[]
|
|
|
72
81
|
* breaks another gate is caught, and a gate the agent cannot satisfy never loops. The owner sees each stop, the
|
|
73
82
|
* waived gates with their reasons, and why the turn ended.
|
|
74
83
|
*/
|
|
75
|
-
export function stopOutput(turn: CheckedTurn, judged: Judged): HookOutput {
|
|
84
|
+
export function stopOutput(turn: CheckedTurn, judged: Judged, brief = false): HookOutput {
|
|
76
85
|
const systemMessage = summary(turn, ending(judged), judged.waived);
|
|
77
86
|
if (!judged.sendBack) return { systemMessage };
|
|
78
|
-
|
|
87
|
+
const reason = failures({ ...turn, outcomes: judged.open }, told(judged), brief);
|
|
88
|
+
return { decision: "block", reason, systemMessage };
|
|
79
89
|
}
|
|
80
90
|
|
|
81
91
|
// When the gates cannot run, such as with a gates file that does not parse, only the owner is told: a crash would
|
|
@@ -106,9 +116,13 @@ async function promptHook(session: string, cwd: string): Promise<HookOutput | un
|
|
|
106
116
|
if (root === undefined) return undefined;
|
|
107
117
|
const missing = await startTurn(session, root);
|
|
108
118
|
if (!tellOnce(session, root, missing)) return undefined;
|
|
119
|
+
const { prompts } = gatesOf(readGatesFiles(root));
|
|
109
120
|
return {
|
|
110
121
|
systemMessage: toolsMissing(missing),
|
|
111
|
-
hookSpecificOutput: {
|
|
122
|
+
hookSpecificOutput: {
|
|
123
|
+
hookEventName: "UserPromptSubmit",
|
|
124
|
+
additionalContext: toolsToInstall(missing, prompts.missingTools),
|
|
125
|
+
},
|
|
112
126
|
};
|
|
113
127
|
} catch (error) {
|
|
114
128
|
return cannotRun(error);
|
|
@@ -135,7 +149,8 @@ async function stopHook(
|
|
|
135
149
|
const names = [...gates.map(({ name }) => name), ...refused.map(({ name }) => name)];
|
|
136
150
|
const judged = judge(checked, waivesOf(message), names, maxSendBacks, turn.sendBacks);
|
|
137
151
|
writeTurn(session, root, { ...turn, sendBacks: judged.state });
|
|
138
|
-
|
|
152
|
+
const brief = judged.sendBack && !tellConductOnce(session, root);
|
|
153
|
+
return stopOutput(checked, judged, brief);
|
|
139
154
|
} catch (error) {
|
|
140
155
|
return cannotRun(error);
|
|
141
156
|
}
|
|
@@ -157,6 +172,8 @@ export async function handleHook(event: HookEvent, text: string): Promise<HookOu
|
|
|
157
172
|
// A session that starts, resumes or is cleared asks again for the missing tools; a compaction goes on as it was.
|
|
158
173
|
if (event === "session-start") {
|
|
159
174
|
if (source !== "compact") forgetTold(session);
|
|
175
|
+
// A compaction may have summed up how to act on a send-back: the next one says it whole again.
|
|
176
|
+
forgetConduct(session);
|
|
160
177
|
sessionHook(session, cwd);
|
|
161
178
|
return undefined;
|
|
162
179
|
}
|