@tayomi/deterministic-gates 0.0.0-stage → 0.4.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 +15 -0
- package/.claude-plugin/plugin.json +14 -0
- package/.codex-plugin/plugin.json +20 -0
- package/LICENSE +21 -0
- package/README.md +111 -2
- package/catalog/go.json +92 -0
- package/catalog/index.html +398 -0
- package/catalog/python.json +46 -0
- package/catalog/typescript.json +125 -0
- package/dist/src/cli.js +65 -0
- package/dist/src/core/check.js +157 -0
- package/dist/src/core/config.js +180 -0
- package/dist/src/core/json.js +32 -0
- package/dist/src/core/report-cache.js +35 -0
- package/dist/src/core/report.js +127 -0
- package/dist/src/core/run.js +172 -0
- package/dist/src/core/send-back.js +53 -0
- package/dist/src/core/snapshot.js +94 -0
- package/dist/src/core/turn-state.js +93 -0
- package/dist/src/core/waive-line.js +15 -0
- package/dist/src/helpers/new-clones.js +57 -0
- package/dist/src/helpers/new-suppressions.js +31 -0
- package/dist/src/helpers/related-tests.js +109 -0
- package/dist/src/helpers/test-reporter.js +57 -0
- package/dist/src/hook/display.js +22 -0
- package/dist/src/hook/hook.js +119 -0
- package/hooks/claude.json +17 -0
- package/hooks/hooks.json +38 -0
- package/hooks/register.ts +12 -0
- package/package.json +51 -4
- package/src/catalog/catalog.ts +168 -0
- package/src/cli.ts +61 -0
- package/src/core/check.ts +209 -0
- package/src/core/config.ts +225 -0
- package/src/core/json.ts +36 -0
- package/src/core/report-cache.ts +51 -0
- package/src/core/report.ts +159 -0
- package/src/core/run.ts +217 -0
- package/src/core/send-back.ts +87 -0
- package/src/core/snapshot.ts +105 -0
- package/src/core/turn-state.ts +153 -0
- package/src/core/waive-line.ts +19 -0
- package/src/helpers/new-clones.ts +58 -0
- package/src/helpers/new-suppressions.ts +44 -0
- package/src/helpers/related-tests.ts +119 -0
- package/src/helpers/test-reporter.ts +71 -0
- package/src/hook/display.ts +21 -0
- package/src/hook/hook.ts +144 -0
- package/src/testing.ts +39 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { relative } from "node:path";
|
|
2
|
+
// The frames of Node itself, and what it prints around an exception nothing caught.
|
|
3
|
+
const NOISE = /^(\s+at .*\((node:|<anonymous>)|\s+at node:|node:internal\/|\s+triggerUncaughtException\(| code: '|\}$|Node\.js v\d)/;
|
|
4
|
+
function cleaned(text) {
|
|
5
|
+
const lines = text.split("\n");
|
|
6
|
+
// The caret under triggerUncaughtException, and the blank line after it.
|
|
7
|
+
if (lines[0]?.startsWith("node:internal/"))
|
|
8
|
+
lines.splice(0, 4);
|
|
9
|
+
const cwd = process.cwd();
|
|
10
|
+
return lines
|
|
11
|
+
.filter((line) => !NOISE.test(line))
|
|
12
|
+
.map((line) => line.split(`file://${cwd}/`).join("").split(`${cwd}/`).join(""))
|
|
13
|
+
.join("\n")
|
|
14
|
+
.trimEnd();
|
|
15
|
+
}
|
|
16
|
+
function traceOf(error) {
|
|
17
|
+
const { cause } = error;
|
|
18
|
+
return cleaned(cause instanceof Error ? (cause.stack ?? cause.message) : String(cause ?? error.message));
|
|
19
|
+
}
|
|
20
|
+
/** Prints each distinct failure once, with the tests it hit, then the counts. */
|
|
21
|
+
export default async function* report(source) {
|
|
22
|
+
const stderr = new Map();
|
|
23
|
+
const names = new Map();
|
|
24
|
+
const hits = new Map();
|
|
25
|
+
let counts = "";
|
|
26
|
+
const hit = (trace, label) => hits.set(trace, [...(hits.get(trace) ?? []), label]);
|
|
27
|
+
for await (const { type, data } of source) {
|
|
28
|
+
const file = data.file === undefined ? data.name : relative(process.cwd(), data.file);
|
|
29
|
+
if (type === "test:stderr") {
|
|
30
|
+
stderr.set(file, (stderr.get(file) ?? "") + (data.message ?? ""));
|
|
31
|
+
}
|
|
32
|
+
else if (type === "test:start") {
|
|
33
|
+
const path = names.get(file) ?? [];
|
|
34
|
+
path.length = data.nesting;
|
|
35
|
+
path.push(data.name);
|
|
36
|
+
names.set(file, path);
|
|
37
|
+
}
|
|
38
|
+
else if (type === "test:fail") {
|
|
39
|
+
const error = data.details?.error;
|
|
40
|
+
if (error === undefined || error.failureType === "subtestsFailed")
|
|
41
|
+
continue;
|
|
42
|
+
// A file that could not run: its error went to stderr, with nothing in the failure.
|
|
43
|
+
if (data.nesting === 0 && data.name === file && !(error.cause instanceof Error)) {
|
|
44
|
+
hit(cleaned(stderr.get(file) ?? error.message), file);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
hit(traceOf(error), `${file}: ${(names.get(file) ?? []).slice(0, data.nesting + 1).join(" > ")}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
else if (type === "test:summary" && data.counts) {
|
|
51
|
+
counts = `${data.counts.tests} tests, ${data.counts.failed} failed`;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
for (const [trace, labels] of hits)
|
|
55
|
+
yield `✖ ${labels.join(", ")}\n${trace}\n\n`;
|
|
56
|
+
yield `${counts}\n`;
|
|
57
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Claude Code's MessageDisplay hook, run on each batch of lines of a reply as it streams: it shows the batch without
|
|
2
|
+
// its waive lines, which Claude Code would print raw. Display only: the stored message, the one the stop reads, keeps
|
|
3
|
+
// them. A batch with no waive line gets no answer, so the hook never takes the place of another display hook there.
|
|
4
|
+
import { readFileSync } from "node:fs";
|
|
5
|
+
import { isRecord, parseJson } from "../core/json.js";
|
|
6
|
+
import { hideWaives } from "../core/waive-line.js";
|
|
7
|
+
/** What the hook prints for a batch: the batch without its waive lines, or nothing when it holds none. */
|
|
8
|
+
export function displayOf(input) {
|
|
9
|
+
const payload = parseJson(input);
|
|
10
|
+
if (!isRecord(payload) || typeof payload.delta !== "string")
|
|
11
|
+
return undefined;
|
|
12
|
+
const shown = hideWaives(payload.delta);
|
|
13
|
+
if (shown === payload.delta)
|
|
14
|
+
return undefined;
|
|
15
|
+
// An empty displayContent is the protocol's own way to show nothing for the batch.
|
|
16
|
+
return JSON.stringify({ hookSpecificOutput: { hookEventName: "MessageDisplay", displayContent: shown } });
|
|
17
|
+
}
|
|
18
|
+
if (import.meta.main) {
|
|
19
|
+
const output = displayOf(readFileSync(0, "utf8"));
|
|
20
|
+
if (output)
|
|
21
|
+
console.log(output);
|
|
22
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { checkTurn, startTurn } from "../core/check.js";
|
|
2
|
+
import { configuredRoot, gatesOf } from "../core/config.js";
|
|
3
|
+
import { isRecord, messageOf, parseJson } from "../core/json.js";
|
|
4
|
+
import { broken, failures, summary, toolsMissing, toolsToInstall } from "../core/report.js";
|
|
5
|
+
import { judge, waivesOf } from "../core/send-back.js";
|
|
6
|
+
import { forgetTold, forgetTurn, readTurn, tellOnce, turnRoot, writeTurn } from "../core/turn-state.js";
|
|
7
|
+
export function parseHookInput(text) {
|
|
8
|
+
const input = parseJson(text);
|
|
9
|
+
if (!isRecord(input))
|
|
10
|
+
return {};
|
|
11
|
+
const { session_id, stop_hook_active, cwd, source, last_assistant_message } = input;
|
|
12
|
+
return {
|
|
13
|
+
...(typeof session_id === "string" ? { session_id } : {}),
|
|
14
|
+
...(typeof stop_hook_active === "boolean" ? { stop_hook_active } : {}),
|
|
15
|
+
...(typeof cwd === "string" ? { cwd } : {}),
|
|
16
|
+
...(typeof source === "string" ? { source } : {}),
|
|
17
|
+
...(typeof last_assistant_message === "string" ? { last_assistant_message } : {}),
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function sendBacks(count) {
|
|
21
|
+
return count === 1 ? "1 send-back" : `${count} send-backs`;
|
|
22
|
+
}
|
|
23
|
+
// How the stop ends, after the totals: sent back, stopped by the cap, or handed back once the agent was sent back.
|
|
24
|
+
function ending({ sendBack, count, max, open }) {
|
|
25
|
+
if (sendBack)
|
|
26
|
+
return `the agent is sent back to fix it (${count} of ${max})`;
|
|
27
|
+
if (open.length)
|
|
28
|
+
return max === 0 ? "send-backs are off" : `stopped after ${sendBacks(count)}`;
|
|
29
|
+
return count ? `after ${sendBacks(count)}` : "";
|
|
30
|
+
}
|
|
31
|
+
// What the agent is told besides the reports: where it stands, and what became of its waive lines.
|
|
32
|
+
function told({ count, max, waived, fallen, unknown, gates }) {
|
|
33
|
+
const names = (list) => list.join(", ");
|
|
34
|
+
return [
|
|
35
|
+
`This is send-back ${count} of ${max}.`,
|
|
36
|
+
...(waived.length ? [`Waived this turn, not to fix: ${names(waived.map(({ name }) => name))}.`] : []),
|
|
37
|
+
...(fallen.length
|
|
38
|
+
? [`Your waiver of ${names(fallen)} no longer holds: its report changed. Fix it, or waive it again.`]
|
|
39
|
+
: []),
|
|
40
|
+
...(unknown.length ? [`No gate is named ${names(unknown)}. The gates of this turn: ${names(gates)}.`] : []),
|
|
41
|
+
];
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* A red gate the agent did not waive sends it back with the reports, until the turn reaches its cap: a fix that
|
|
45
|
+
* breaks another gate is caught, and a gate the agent cannot satisfy never loops. The owner sees each stop, the
|
|
46
|
+
* waived gates with their reasons, and why the turn ended.
|
|
47
|
+
*/
|
|
48
|
+
export function stopOutput(turn, judged) {
|
|
49
|
+
const systemMessage = summary(turn, ending(judged), judged.waived);
|
|
50
|
+
if (!judged.sendBack)
|
|
51
|
+
return { systemMessage };
|
|
52
|
+
return { decision: "block", reason: failures({ ...turn, outcomes: judged.open }, told(judged)), systemMessage };
|
|
53
|
+
}
|
|
54
|
+
// When the gates cannot run, such as with a gates file that does not parse, only the owner is told: a crash would
|
|
55
|
+
// show them a stack trace at best, and the agent is not to blame.
|
|
56
|
+
function cannotRun(error) {
|
|
57
|
+
return { systemMessage: broken(messageOf(error)) };
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* A prompt ends the turn in progress; from a folder with no gates, or when the turn cannot start, no check will look
|
|
61
|
+
* for a new one. When the gates of the repo miss tools, the agent is asked once to install them.
|
|
62
|
+
*/
|
|
63
|
+
async function promptHook(session, cwd) {
|
|
64
|
+
try {
|
|
65
|
+
forgetTurn(session);
|
|
66
|
+
const root = configuredRoot(cwd);
|
|
67
|
+
if (root === undefined)
|
|
68
|
+
return undefined;
|
|
69
|
+
const missing = await startTurn(session, root);
|
|
70
|
+
if (!tellOnce(session, root, missing))
|
|
71
|
+
return undefined;
|
|
72
|
+
return {
|
|
73
|
+
systemMessage: toolsMissing(missing),
|
|
74
|
+
hookSpecificOutput: { hookEventName: "UserPromptSubmit", additionalContext: toolsToInstall(missing) },
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
return cannotRun(error);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* A stop checks the repo its turn started in, whatever folder the agent stops in, or that folder's repo otherwise,
|
|
83
|
+
* reads the gates the agent's last message waives, and records what it told the agent for the next stop of the turn.
|
|
84
|
+
*/
|
|
85
|
+
async function stopHook(session, cwd, retry, message) {
|
|
86
|
+
const root = turnRoot(session, retry) ?? configuredRoot(cwd);
|
|
87
|
+
if (root === undefined)
|
|
88
|
+
return undefined;
|
|
89
|
+
try {
|
|
90
|
+
const checked = await checkTurn(session, root);
|
|
91
|
+
const turn = readTurn(session, root);
|
|
92
|
+
if (turn === undefined)
|
|
93
|
+
throw new Error("the turn was not recorded after its check.");
|
|
94
|
+
const { gates, refused, maxSendBacks } = gatesOf(turn.config);
|
|
95
|
+
const names = [...gates.map(({ name }) => name), ...refused.map(({ name }) => name)];
|
|
96
|
+
const judged = judge(checked, waivesOf(message), names, maxSendBacks, turn.sendBacks);
|
|
97
|
+
writeTurn(session, root, { ...turn, sendBacks: judged.state });
|
|
98
|
+
return stopOutput(checked, judged);
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
return cannotRun(error);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Handles a hook event of a coding agent: Claude Code and Codex send the same input and read the same output. It does
|
|
106
|
+
* nothing outside git, or in a repo with no gates file.
|
|
107
|
+
*/
|
|
108
|
+
export async function handleHook(event, text) {
|
|
109
|
+
const { session_id: session, stop_hook_active: retry = false, cwd = process.cwd(), source, last_assistant_message: message = "", } = parseHookInput(text);
|
|
110
|
+
if (session === undefined)
|
|
111
|
+
return undefined;
|
|
112
|
+
// A session that starts, resumes or is cleared asks again for the missing tools; a compaction goes on as it was.
|
|
113
|
+
if (event === "session-start") {
|
|
114
|
+
if (source !== "compact")
|
|
115
|
+
forgetTold(session);
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
return event === "turn-start" ? promptHook(session, cwd) : stopHook(session, cwd, retry, message);
|
|
119
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"description": "Claude Code alone: hide the waive lines Claude Code would print raw, which Codex hides itself",
|
|
3
|
+
"modules": ["./register.ts"],
|
|
4
|
+
"hooks": {
|
|
5
|
+
"MessageDisplay": [
|
|
6
|
+
{
|
|
7
|
+
"hooks": [
|
|
8
|
+
{
|
|
9
|
+
"type": "command",
|
|
10
|
+
"command": "node \"${CLAUDE_PLUGIN_ROOT}/src/hook/display.ts\"",
|
|
11
|
+
"timeout": 5
|
|
12
|
+
}
|
|
13
|
+
]
|
|
14
|
+
}
|
|
15
|
+
]
|
|
16
|
+
}
|
|
17
|
+
}
|
package/hooks/hooks.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"description": "Run the deterministic gates on what each turn changed, and send the agent back until they pass",
|
|
3
|
+
"hooks": {
|
|
4
|
+
"SessionStart": [
|
|
5
|
+
{
|
|
6
|
+
"hooks": [
|
|
7
|
+
{
|
|
8
|
+
"type": "command",
|
|
9
|
+
"command": "node \"${CLAUDE_PLUGIN_ROOT}/src/cli.ts\" hook session-start",
|
|
10
|
+
"timeout": 10
|
|
11
|
+
}
|
|
12
|
+
]
|
|
13
|
+
}
|
|
14
|
+
],
|
|
15
|
+
"UserPromptSubmit": [
|
|
16
|
+
{
|
|
17
|
+
"hooks": [
|
|
18
|
+
{
|
|
19
|
+
"type": "command",
|
|
20
|
+
"command": "node \"${CLAUDE_PLUGIN_ROOT}/src/cli.ts\" hook turn-start",
|
|
21
|
+
"timeout": 60
|
|
22
|
+
}
|
|
23
|
+
]
|
|
24
|
+
}
|
|
25
|
+
],
|
|
26
|
+
"Stop": [
|
|
27
|
+
{
|
|
28
|
+
"hooks": [
|
|
29
|
+
{
|
|
30
|
+
"type": "command",
|
|
31
|
+
"command": "node \"${CLAUDE_PLUGIN_ROOT}/src/cli.ts\" hook check",
|
|
32
|
+
"timeout": 300
|
|
33
|
+
}
|
|
34
|
+
]
|
|
35
|
+
}
|
|
36
|
+
]
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// The Claude Code mod of the plugin: it draws each reply without its waive lines, a resumed session's included, which
|
|
2
|
+
// the MessageDisplay hook never sees again. The rest of the chain, a mod that draws views among it, gets the text
|
|
3
|
+
// already clean. Display only: the stored message, the one the stop reads, keeps them.
|
|
4
|
+
import type { Register } from "claude-code";
|
|
5
|
+
import { hideWaives } from "../src/core/waive-line.ts";
|
|
6
|
+
|
|
7
|
+
export const register: Register = (on) => {
|
|
8
|
+
on("ui.render", { component: "AssistantMessage" }, ($, e, next) => {
|
|
9
|
+
const text = hideWaives(e.props.text);
|
|
10
|
+
return text === e.props.text ? next(e) : next({ ...e, props: { ...e.props, text } });
|
|
11
|
+
});
|
|
12
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tayomi/deterministic-gates",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.4.1",
|
|
4
|
+
"description": "Deterministic backpressure for coding agents: gates that run on what each turn changed and send the agent back until they pass.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": {
|
|
8
|
+
"name": "Pierre Moati",
|
|
9
|
+
"url": "https://github.com/mopi1402"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/mopi1402/tayomi-deterministic-gates.git"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"coding-agents",
|
|
17
|
+
"quality-gates",
|
|
18
|
+
"backpressure",
|
|
19
|
+
"claude-code",
|
|
20
|
+
"codex",
|
|
21
|
+
"hooks"
|
|
22
|
+
],
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=24.2"
|
|
25
|
+
},
|
|
26
|
+
"packageManager": "pnpm@10.4.0",
|
|
27
|
+
"files": [
|
|
28
|
+
".claude-plugin",
|
|
29
|
+
".codex-plugin",
|
|
30
|
+
"catalog",
|
|
31
|
+
"dist",
|
|
32
|
+
"hooks",
|
|
33
|
+
"src",
|
|
34
|
+
"!src/**/*.test.ts"
|
|
35
|
+
],
|
|
36
|
+
"scripts": {
|
|
37
|
+
"catalog": "node src/catalog/catalog.ts",
|
|
38
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
39
|
+
"check": "tsc && prettier --check . && node --test \"src/**/*.test.ts\"",
|
|
40
|
+
"test": "node --test \"src/**/*.test.ts\"",
|
|
41
|
+
"integration": "node --test \"integration/*.test.ts\""
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/node": "24.19.2",
|
|
45
|
+
"jscpd": "5.4.2",
|
|
46
|
+
"knip": "6.44.1",
|
|
47
|
+
"prettier": "3.9.10",
|
|
48
|
+
"typescript": "6.0.3"
|
|
49
|
+
},
|
|
50
|
+
"bin": {
|
|
51
|
+
"deterministic-gates": "dist/src/cli.js"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { readdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { basename, join } from "node:path";
|
|
3
|
+
import { type Gate, isCompared, parseGates } from "../core/config.ts";
|
|
4
|
+
import { isRecord, messageOf } from "../core/json.ts";
|
|
5
|
+
|
|
6
|
+
// The catalog lists the gates worth copying into a repo, one file per technology. It never runs by itself: the engine
|
|
7
|
+
// reads only .gates.json, and a developer or an agent copies from here what a repo needs.
|
|
8
|
+
|
|
9
|
+
export const CATALOG_DIR = join(import.meta.dirname, "..", "..", "catalog");
|
|
10
|
+
export const PAGE = join(CATALOG_DIR, "index.html");
|
|
11
|
+
|
|
12
|
+
export interface Requirement {
|
|
13
|
+
tool: string;
|
|
14
|
+
install: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface Entry {
|
|
18
|
+
id: string;
|
|
19
|
+
description: string;
|
|
20
|
+
requires: Requirement[];
|
|
21
|
+
gate: Gate;
|
|
22
|
+
definition: Record<string, unknown>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface Catalog {
|
|
26
|
+
id: string;
|
|
27
|
+
title: string;
|
|
28
|
+
description: string;
|
|
29
|
+
entries: Entry[];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function isRequirement(value: unknown): value is Requirement {
|
|
33
|
+
return isRecord(value) && typeof value.tool === "string" && typeof value.install === "string";
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function parseEntry(catalogId: string, value: unknown, index: number): Entry {
|
|
37
|
+
const definition = isRecord(value) && isRecord(value.gate) ? value.gate : {};
|
|
38
|
+
const id = `${catalogId}/${typeof definition.name === "string" ? definition.name : `#${index + 1}`}`;
|
|
39
|
+
function refuse(problem: string): never {
|
|
40
|
+
throw new Error(`${id}: ${problem}.`);
|
|
41
|
+
}
|
|
42
|
+
if (!isRecord(value)) refuse("an entry is an object");
|
|
43
|
+
const { description, requires } = value;
|
|
44
|
+
if (typeof description !== "string" || !description) refuse("description says what the gate catches");
|
|
45
|
+
if (!Array.isArray(requires) || !requires.every(isRequirement))
|
|
46
|
+
refuse("requires lists the tools to install, as {tool, install}");
|
|
47
|
+
let gate: Gate | undefined;
|
|
48
|
+
try {
|
|
49
|
+
[gate] = parseGates(JSON.stringify({ gates: [definition] }));
|
|
50
|
+
} catch (error) {
|
|
51
|
+
refuse(messageOf(error).replace(/\.$/, ""));
|
|
52
|
+
}
|
|
53
|
+
if (gate === undefined) refuse('a catalog gate has severity "error"');
|
|
54
|
+
return { id, description, requires, gate, definition };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function parseCatalog(id: string, text: string): Catalog {
|
|
58
|
+
const parsed: unknown = JSON.parse(text);
|
|
59
|
+
if (!isRecord(parsed) || typeof parsed.title !== "string" || typeof parsed.description !== "string")
|
|
60
|
+
throw new Error(`${id}: a catalog has a title and a description.`);
|
|
61
|
+
if (!Array.isArray(parsed.gates)) throw new Error(`${id}: a catalog lists its gates.`);
|
|
62
|
+
const entries = parsed.gates.map((entry: unknown, index) => parseEntry(id, entry, index));
|
|
63
|
+
const seen = new Set<string>();
|
|
64
|
+
for (const entry of entries) {
|
|
65
|
+
if (seen.has(entry.id)) throw new Error(`${entry.id}: twice in the catalog.`);
|
|
66
|
+
seen.add(entry.id);
|
|
67
|
+
}
|
|
68
|
+
return { id, title: parsed.title, description: parsed.description, entries };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function readCatalogs(dir: string): Catalog[] {
|
|
72
|
+
return readdirSync(dir)
|
|
73
|
+
.filter((file) => file.endsWith(".json"))
|
|
74
|
+
.sort()
|
|
75
|
+
.map((file) => parseCatalog(basename(file, ".json"), readFileSync(join(dir, file), "utf8")));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function escape(text: string): string {
|
|
79
|
+
return text.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll('"', """);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function linked(text: string): string {
|
|
83
|
+
return escape(text).replace(/https:\/\/[^\s,]+/g, (url) => `<a href="${url}">${url}</a>`);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function entryHtml(entry: Entry): string {
|
|
87
|
+
const { id, description, requires, gate, definition } = entry;
|
|
88
|
+
const compare = isCompared(gate) ? "<dt>Fails on</dt><dd>only the output lines the turn added</dd>" : "";
|
|
89
|
+
const tools = requires.map(({ tool, install }) => `<li><code>${escape(tool)}</code>: ${linked(install)}</li>`);
|
|
90
|
+
return `<article id="${escape(id)}">
|
|
91
|
+
<h3><code>${escape(id)}</code></h3>
|
|
92
|
+
<p>${escape(description)}</p>
|
|
93
|
+
<dl>
|
|
94
|
+
<dt>Runs</dt><dd><code>${escape(gate.run.join(" "))}</code></dd>
|
|
95
|
+
<dt>When the turn changes</dt><dd><code>${escape(gate.files)}</code></dd>
|
|
96
|
+
${compare}<dt>Requires</dt><dd><ul>${tools.join("")}</ul></dd>
|
|
97
|
+
</dl>
|
|
98
|
+
<details><summary>Definition to copy into .gates.json</summary><pre><code>${escape(JSON.stringify(definition, null, 2))}</code></pre></details>
|
|
99
|
+
</article>`;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function catalogSection({ id, title, description, entries }: Catalog): string {
|
|
103
|
+
return `<section id="${escape(id)}">
|
|
104
|
+
<h2>${escape(title)}</h2>
|
|
105
|
+
<p>${escape(description)}</p>
|
|
106
|
+
${entries.map(entryHtml).join("\n")}
|
|
107
|
+
</section>`;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const STYLE = `
|
|
111
|
+
:root { --bg: #fafaf9; --panel: #ffffff; --text: #1c1917; --muted: #57534e; --line: #e7e5e4; --code: #f5f5f4;
|
|
112
|
+
--link: #1d4ed8; }
|
|
113
|
+
@media (prefers-color-scheme: dark) {
|
|
114
|
+
:root { --bg: #1c1917; --panel: #262321; --text: #f5f5f4; --muted: #a8a29e; --line: #3f3a36; --code: #2f2a27;
|
|
115
|
+
--link: #93c5fd; }
|
|
116
|
+
}
|
|
117
|
+
* { box-sizing: border-box; }
|
|
118
|
+
body { margin: 0; background: var(--bg); color: var(--text); font: 16px/1.55 system-ui, sans-serif; }
|
|
119
|
+
main { max-width: 860px; margin: 0 auto; padding: 32px 16px 64px; }
|
|
120
|
+
h1 { margin: 0 0 8px; font-size: 1.9rem; }
|
|
121
|
+
h2 { margin: 48px 0 4px; font-size: 1.4rem; }
|
|
122
|
+
h3 { margin: 0; font-size: 1rem; }
|
|
123
|
+
p, dd, li { color: var(--muted); }
|
|
124
|
+
a { color: var(--link); }
|
|
125
|
+
nav { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }
|
|
126
|
+
nav a { padding: 4px 12px; border: 1px solid var(--line); border-radius: 999px; text-decoration: none; }
|
|
127
|
+
article { margin: 16px 0; padding: 16px; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; }
|
|
128
|
+
dl { display: grid; grid-template-columns: max-content 1fr; gap: 4px 16px; margin: 12px 0; }
|
|
129
|
+
dt { font-weight: 600; }
|
|
130
|
+
dd { margin: 0; min-width: 0; }
|
|
131
|
+
dd ul { margin: 0; padding-left: 18px; }
|
|
132
|
+
code { font: 0.88em/1.4 ui-monospace, monospace; background: var(--code); padding: 1px 5px; border-radius: 4px;
|
|
133
|
+
overflow-wrap: anywhere; }
|
|
134
|
+
pre { overflow-x: auto; background: var(--code); padding: 12px; border-radius: 8px; }
|
|
135
|
+
pre code { padding: 0; }
|
|
136
|
+
summary { cursor: pointer; color: var(--muted); }
|
|
137
|
+
@media (max-width: 560px) { dl { grid-template-columns: 1fr; } dd { margin-bottom: 8px; } }
|
|
138
|
+
`;
|
|
139
|
+
|
|
140
|
+
/** The page a developer browses. It is generated from the catalog files, and a test fails when it falls behind them. */
|
|
141
|
+
export function catalogHtml(catalogs: Catalog[]): string {
|
|
142
|
+
const nav = catalogs.map(
|
|
143
|
+
({ id, title, entries }) => `<a href="#${escape(id)}">${escape(title)} (${entries.length})</a>`,
|
|
144
|
+
);
|
|
145
|
+
return `<!doctype html>
|
|
146
|
+
<html lang="en">
|
|
147
|
+
<head>
|
|
148
|
+
<meta charset="utf-8">
|
|
149
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
150
|
+
<title>Gate catalog</title>
|
|
151
|
+
<style>${STYLE}</style>
|
|
152
|
+
</head>
|
|
153
|
+
<body>
|
|
154
|
+
<main>
|
|
155
|
+
<header>
|
|
156
|
+
<h1>Gate catalog</h1>
|
|
157
|
+
<p>The gates worth copying into a repo. Nothing here runs by itself: the engine reads only the repo's <code>.gates.json</code>. Copy the definition of a gate there, install what it requires, and adapt its <code>files</code> to the layout of the repo.</p>
|
|
158
|
+
<p>Generated from <code>catalog/*.json</code> by <code>pnpm catalog</code>: edit those files, never this page.</p>
|
|
159
|
+
<nav>${nav.join("")}</nav>
|
|
160
|
+
</header>
|
|
161
|
+
${catalogs.map(catalogSection).join("\n")}
|
|
162
|
+
</main>
|
|
163
|
+
</body>
|
|
164
|
+
</html>
|
|
165
|
+
`;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (import.meta.main) writeFileSync(PAGE, catalogHtml(readCatalogs(CATALOG_DIR)));
|
package/src/cli.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { checkAll } from "./core/check.ts";
|
|
4
|
+
import { configuredRoot } from "./core/config.ts";
|
|
5
|
+
import { messageOf } from "./core/json.ts";
|
|
6
|
+
import { broken, reports, summary } from "./core/report.ts";
|
|
7
|
+
import { failed } from "./core/run.ts";
|
|
8
|
+
import { handleHook } from "./hook/hook.ts";
|
|
9
|
+
|
|
10
|
+
function readStdin(): string {
|
|
11
|
+
try {
|
|
12
|
+
return readFileSync(0, "utf8");
|
|
13
|
+
} catch {
|
|
14
|
+
return "";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** What the command prints when it is called with words it does not know. */
|
|
19
|
+
export const USAGE = "Usage: node src/cli.ts hook <session-start|turn-start|check>, or node src/cli.ts check";
|
|
20
|
+
|
|
21
|
+
// The helpers of the engine run only on import.meta.main, which came with Node 24.2: below, they are a silent green.
|
|
22
|
+
const [major = 0, minor = 0] = process.versions.node.split(".").map(Number);
|
|
23
|
+
const oldNode =
|
|
24
|
+
major < 24 || (major === 24 && minor < 2)
|
|
25
|
+
? `Node 24.2 or later is needed, this is ${process.versions.node}.`
|
|
26
|
+
: undefined;
|
|
27
|
+
|
|
28
|
+
async function main([command, event]: string[]): Promise<void> {
|
|
29
|
+
if (oldNode !== undefined) {
|
|
30
|
+
if (command === "hook") console.log(JSON.stringify({ systemMessage: broken(oldNode) }));
|
|
31
|
+
else {
|
|
32
|
+
console.error(oldNode);
|
|
33
|
+
process.exitCode = 2;
|
|
34
|
+
}
|
|
35
|
+
} else if (command === "hook" && (event === "session-start" || event === "turn-start" || event === "check")) {
|
|
36
|
+
const output = await handleHook(event, readStdin());
|
|
37
|
+
if (output) console.log(JSON.stringify(output));
|
|
38
|
+
} else if (command === "check" && event === undefined) {
|
|
39
|
+
const root = configuredRoot(process.cwd());
|
|
40
|
+
if (root === undefined) {
|
|
41
|
+
console.error("Not in a git repository, or no .gates.json in it.");
|
|
42
|
+
process.exitCode = 2;
|
|
43
|
+
} else {
|
|
44
|
+
try {
|
|
45
|
+
const all = await checkAll(root);
|
|
46
|
+
console.log([summary(all), ...reports(all.outcomes)].join("\n\n"));
|
|
47
|
+
if (all.outcomes.some(failed)) process.exitCode = 1;
|
|
48
|
+
} catch (error) {
|
|
49
|
+
console.error(`Could not run: ${messageOf(error)}`);
|
|
50
|
+
process.exitCode = 2;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
} else {
|
|
54
|
+
console.error(USAGE);
|
|
55
|
+
process.exitCode = 2;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Run as a program, never as a test imports it. Below Node 24.2, import.meta.main is undefined: the program runs then,
|
|
60
|
+
// to say that Node is too old.
|
|
61
|
+
if (import.meta.main !== false) await main(process.argv.slice(2));
|