@maniumek/suwgate 0.16.0 → 0.18.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/AGENTS.shared.md +13 -2
- package/README.md +98 -9
- package/dist/cli.js +217 -13
- package/dist/gate/breakdown.d.ts +40 -0
- package/dist/gate/breakdown.js +111 -0
- package/dist/gate/check.d.ts +4 -0
- package/dist/gate/check.js +38 -1
- package/dist/gate/ciHistory.d.ts +21 -0
- package/dist/gate/ciHistory.js +195 -0
- package/dist/gate/config.d.ts +5 -1
- package/dist/gate/config.js +56 -2
- package/dist/gate/context.d.ts +15 -0
- package/dist/gate/context.js +98 -0
- package/dist/gate/contract.d.ts +7 -0
- package/dist/gate/contract.js +1 -0
- package/dist/gate/dashboard.d.ts +112 -0
- package/dist/gate/dashboard.js +148 -0
- package/dist/gate/dashboardFile.d.ts +33 -0
- package/dist/gate/dashboardFile.js +185 -0
- package/dist/gate/dashboardJson.d.ts +2 -0
- package/dist/gate/dashboardJson.js +26 -0
- package/dist/gate/dashboardPage.d.ts +3 -0
- package/dist/gate/dashboardPage.js +307 -0
- package/dist/gate/dashboardServer.d.ts +11 -0
- package/dist/gate/dashboardServer.js +70 -0
- package/dist/gate/dashboardTesting.d.ts +12 -0
- package/dist/gate/dashboardTesting.js +99 -0
- package/dist/gate/guardsStep.d.ts +6 -0
- package/dist/gate/guardsStep.js +8 -1
- package/dist/gate/history.d.ts +31 -0
- package/dist/gate/history.js +105 -0
- package/dist/gate/plan.d.ts +3 -1
- package/dist/gate/plan.js +6 -3
- package/dist/gate/repository.d.ts +1 -0
- package/dist/gate/repository.js +1 -1
- package/dist/gate/runner.d.ts +3 -0
- package/dist/gate/runner.js +4 -1
- package/dist/gate/stats.d.ts +27 -0
- package/dist/gate/stats.js +84 -0
- package/dist/guards/comments/eslint.js +1 -0
- package/dist/guards/comments/scan.js +1 -0
- package/dist/guards/comments/switches.js +1 -0
- package/dist/guards/database/connectionHost.js +1 -0
- package/dist/guards/dotnet/appsettingsBound.js +1 -0
- package/dist/guards/dotnet/bannedNugetPackages.js +1 -0
- package/dist/guards/dotnet/openApiVersions.js +1 -0
- package/dist/guards/dotnet/suwbackkitWiring.js +1 -0
- package/dist/guards/dotnet/unusedNugetVersions.js +1 -0
- package/dist/guards/repository/bannedNpmPackages.js +1 -0
- package/dist/guards/repository/composePorts.js +1 -0
- package/dist/guards/repository/dependencyDirection.js +1 -0
- package/dist/guards/repository/forbiddenNames.js +1 -0
- package/dist/guards/repository/humanDocs.js +1 -0
- package/dist/guards/repository/imagePins.js +1 -0
- package/dist/guards/repository/justModules.js +1 -0
- package/dist/guards/repository/noRawColors.js +1 -0
- package/dist/guards/repository/repositoryLayout.js +1 -0
- package/dist/guards/repository/scriptsCalled.js +1 -0
- package/dist/guards/repository/scriptsHelp.js +1 -0
- package/dist/guards/repository/suwfrontkitWiring.js +1 -0
- package/dist/guards/repository/vsCodeSettings.js +1 -0
- package/dist/profiles/holdout.d.ts +2 -0
- package/dist/profiles/holdout.js +38 -0
- package/dist/profiles/index.d.ts +1 -0
- package/dist/profiles/index.js +1 -0
- package/dist/profiles/reports.d.ts +6 -0
- package/dist/profiles/reports.js +14 -0
- package/dist/profiles/verdict.js +41 -2
- package/dist/session/doctor.d.ts +26 -0
- package/dist/session/doctor.js +265 -0
- package/dist/session/gardenRecord.d.ts +12 -0
- package/dist/session/gardenRecord.js +58 -0
- package/dist/session/guardPr.js +2 -2
- package/dist/session/prePush.js +1 -1
- package/dist/session/suwagentInfo.d.ts +28 -0
- package/dist/session/suwagentInfo.js +103 -0
- package/package.json +1 -1
- package/schema/suwgate.schema.json +26 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { causeOf, runsInPeriod, stoppers } from "./breakdown.js";
|
|
4
|
+
import { loadConfig } from "./config.js";
|
|
5
|
+
import { attentionOf, hookRows } from "./dashboard.js";
|
|
6
|
+
import { allRuns } from "./ciHistory.js";
|
|
7
|
+
import { branchOf } from "./history.js";
|
|
8
|
+
import { git, repositoryRoot } from "./repository.js";
|
|
9
|
+
const MAX_CHARACTERS = 1800;
|
|
10
|
+
const PERIOD_DAYS = 30;
|
|
11
|
+
const RECENT_RUNS = 10;
|
|
12
|
+
const LISTED = 3;
|
|
13
|
+
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
|
14
|
+
const HOW_WORK_REACHES_A_PULL_REQUEST = [
|
|
15
|
+
"suwgate, how work reaches a pull request here:",
|
|
16
|
+
"- The gate runs quick after every turn and before every commit: fix what it reports.",
|
|
17
|
+
"- Never run the full gate (`suwgate check` without `--quick`): CI does. Never `--no-verify`, never edit the gate.",
|
|
18
|
+
"- A finished change goes to a pull request only through /suwgate:ship: a small change gets the gate only, a normal one also simplify and review, a careful one cannot drop the review.",
|
|
19
|
+
"- The owner reads the PR proof and decides the merge.",
|
|
20
|
+
];
|
|
21
|
+
const WHERE_TO_LOOK = [
|
|
22
|
+
"Where to look:",
|
|
23
|
+
"- `suwgate dashboard --json` and the insights skill answer questions about the state; /suwgate:dashboard-open shows the owner the page.",
|
|
24
|
+
];
|
|
25
|
+
function clip(text, length) {
|
|
26
|
+
return text.length <= length ? text : `${text.slice(0, length - 3)}...`;
|
|
27
|
+
}
|
|
28
|
+
function stamp(iso) {
|
|
29
|
+
const date = new Date(iso);
|
|
30
|
+
return `${date.getUTCDate()} ${MONTHS[date.getUTCMonth()] ?? ""} ${iso.slice(11, 16)} UTC`;
|
|
31
|
+
}
|
|
32
|
+
function lastRun(data) {
|
|
33
|
+
const recent = data.branchRuns.toSorted((a, b) => Date.parse(b.at) - Date.parse(a.at)).slice(0, RECENT_RUNS);
|
|
34
|
+
const [last] = recent;
|
|
35
|
+
if (last === undefined)
|
|
36
|
+
return `- Branch ${clip(data.branch, 60)}: no run recorded here yet.`;
|
|
37
|
+
const red = recent.filter((run) => run.verdict === "red").length;
|
|
38
|
+
const verdict = last.verdict === "red" ? `red, stopped by ${clip(causeOf(last), 80)}` : "green";
|
|
39
|
+
return `- Branch ${clip(data.branch, 60)}: last run ${stamp(last.at)}, ${verdict}; red runs among the last ${recent.length} here: ${red}.`;
|
|
40
|
+
}
|
|
41
|
+
export function renderContext(data) {
|
|
42
|
+
const sets = data.guardSets === undefined
|
|
43
|
+
? "all guard sets"
|
|
44
|
+
: data.guardSets.length === 0
|
|
45
|
+
? "no guard set"
|
|
46
|
+
: `guard sets ${data.guardSets.join(", ")}`;
|
|
47
|
+
const hooks = data.unwiredHooks.length === 0 ? "all five hooks wired" : `hooks not wired here: ${data.unwiredHooks.join(", ")}`;
|
|
48
|
+
const needs = data.attention === 0
|
|
49
|
+
? "nothing from the runs or the hooks needs the owner"
|
|
50
|
+
: `${data.attention} thing${data.attention === 1 ? "" : "s"} from the runs and the hooks need the owner${data.stoppedIds.length === 0 ? "" : ` (stopped the gate: ${data.stoppedIds.slice(0, LISTED).join(", ")})`}`;
|
|
51
|
+
const text = [
|
|
52
|
+
...HOW_WORK_REACHES_A_PULL_REQUEST,
|
|
53
|
+
"This repository now:",
|
|
54
|
+
`- suwgate ${data.version}, call it as ${data.binary}; profiles ${data.profiles.join(", ")}; ${sets}; ${hooks}.`,
|
|
55
|
+
lastRun(data),
|
|
56
|
+
`- ${needs}; the wall needs the network: run /suwgate:doctor.`,
|
|
57
|
+
...WHERE_TO_LOOK,
|
|
58
|
+
].join("\n");
|
|
59
|
+
return clip(text, MAX_CHARACTERS);
|
|
60
|
+
}
|
|
61
|
+
function binaryOf(root, entry) {
|
|
62
|
+
const relative = path.relative(root, entry);
|
|
63
|
+
return relative.startsWith("..") || relative.includes("node_modules")
|
|
64
|
+
? "the repository's own `suwgate` (node_modules)"
|
|
65
|
+
: `\`node ${relative}\``;
|
|
66
|
+
}
|
|
67
|
+
export function readContext(request, version, entry) {
|
|
68
|
+
const root = repositoryRoot(request.cwd);
|
|
69
|
+
if (root === null)
|
|
70
|
+
return "";
|
|
71
|
+
const loaded = loadConfig(root, request.catalog);
|
|
72
|
+
if ("problems" in loaded)
|
|
73
|
+
return "";
|
|
74
|
+
const read = (file) => {
|
|
75
|
+
try {
|
|
76
|
+
return readFileSync(path.join(root, file), "utf8");
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
const now = request.now();
|
|
83
|
+
const runs = allRuns(root);
|
|
84
|
+
const branch = branchOf(root);
|
|
85
|
+
const hooks = hookRows(read, git(root, ["config", "--get", "core.hooksPath"])?.trim() ?? "");
|
|
86
|
+
const stopped = stoppers(runs, PERIOD_DAYS, now);
|
|
87
|
+
return `${renderContext({
|
|
88
|
+
version,
|
|
89
|
+
binary: binaryOf(root, entry),
|
|
90
|
+
profiles: loaded.config.profiles,
|
|
91
|
+
guardSets: loaded.config.guardSets,
|
|
92
|
+
unwiredHooks: hooks.filter((hook) => !hook.wired).map((hook) => hook.id),
|
|
93
|
+
branch,
|
|
94
|
+
branchRuns: runsInPeriod(runs, PERIOD_DAYS, now).filter((run) => run.branch === branch),
|
|
95
|
+
stoppedIds: stopped.map((stopper) => stopper.id),
|
|
96
|
+
attention: attentionOf([], stopped, hooks).length,
|
|
97
|
+
})}\n`;
|
|
98
|
+
}
|
package/dist/gate/contract.d.ts
CHANGED
|
@@ -30,6 +30,7 @@ export interface Guard {
|
|
|
30
30
|
id: string;
|
|
31
31
|
description: string;
|
|
32
32
|
profiles: readonly Profile[] | "every";
|
|
33
|
+
set?: string;
|
|
33
34
|
quick?: boolean;
|
|
34
35
|
settingsSchema?: object;
|
|
35
36
|
check(context: GuardContext): readonly Finding[] | Promise<readonly Finding[]>;
|
|
@@ -45,6 +46,12 @@ export interface StepContext {
|
|
|
45
46
|
settings: Readonly<Record<string, unknown>>;
|
|
46
47
|
changed(): readonly string[];
|
|
47
48
|
}
|
|
49
|
+
export declare const HOLDOUT_STEP = "holdout";
|
|
50
|
+
export interface Holdout {
|
|
51
|
+
run: string;
|
|
52
|
+
after?: string[];
|
|
53
|
+
limitSeconds?: number;
|
|
54
|
+
}
|
|
48
55
|
export interface Skip {
|
|
49
56
|
skip: string;
|
|
50
57
|
}
|
package/dist/gate/contract.js
CHANGED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { type BranchSummary, type DayCount, type Stopper, type Trends } from "./breakdown.ts";
|
|
2
|
+
import { type Catalog, type Config } from "./config.ts";
|
|
3
|
+
import type { CiView } from "./ciHistory.ts";
|
|
4
|
+
import type { RunRecord } from "./history.ts";
|
|
5
|
+
import { type ListedStep } from "./plan.ts";
|
|
6
|
+
import { type Stats } from "./stats.ts";
|
|
7
|
+
export interface WallCheck {
|
|
8
|
+
name: string;
|
|
9
|
+
state: "holds" | "broken" | "unchecked";
|
|
10
|
+
detail: string;
|
|
11
|
+
}
|
|
12
|
+
export interface PluginView {
|
|
13
|
+
version: string;
|
|
14
|
+
skills: {
|
|
15
|
+
name: string;
|
|
16
|
+
summary: string;
|
|
17
|
+
onlyWhenAsked: boolean;
|
|
18
|
+
}[];
|
|
19
|
+
hookDescription: string;
|
|
20
|
+
hooks: {
|
|
21
|
+
event: string;
|
|
22
|
+
matcher: string;
|
|
23
|
+
script: string;
|
|
24
|
+
}[];
|
|
25
|
+
agents: {
|
|
26
|
+
name: string;
|
|
27
|
+
description: string;
|
|
28
|
+
tools: string;
|
|
29
|
+
}[];
|
|
30
|
+
}
|
|
31
|
+
export interface GardenView {
|
|
32
|
+
at: string;
|
|
33
|
+
branch: string;
|
|
34
|
+
found: Record<string, number>;
|
|
35
|
+
}
|
|
36
|
+
export interface DashboardInput {
|
|
37
|
+
name: string;
|
|
38
|
+
days: number;
|
|
39
|
+
now: Date;
|
|
40
|
+
runs: readonly RunRecord[];
|
|
41
|
+
ci: CiView;
|
|
42
|
+
config: Config;
|
|
43
|
+
catalog: Catalog;
|
|
44
|
+
wall: readonly WallCheck[];
|
|
45
|
+
wallCheckedAt: Date;
|
|
46
|
+
live: boolean;
|
|
47
|
+
files: readonly string[];
|
|
48
|
+
read: (file: string) => string | null;
|
|
49
|
+
hooksPath: string;
|
|
50
|
+
criterionIds: (spec: string) => string[];
|
|
51
|
+
plugin: PluginView | null;
|
|
52
|
+
garden: GardenView | null;
|
|
53
|
+
}
|
|
54
|
+
interface AttentionItem {
|
|
55
|
+
kind: "wall" | "broken" | "have";
|
|
56
|
+
text: string;
|
|
57
|
+
detail: string;
|
|
58
|
+
}
|
|
59
|
+
interface GuardRow {
|
|
60
|
+
id: string;
|
|
61
|
+
set: string;
|
|
62
|
+
state: "active" | "set not taken" | "skipped";
|
|
63
|
+
skipReason: string;
|
|
64
|
+
}
|
|
65
|
+
interface ExceptionRow {
|
|
66
|
+
guard: string;
|
|
67
|
+
target: string;
|
|
68
|
+
reason: string;
|
|
69
|
+
covered: number | null;
|
|
70
|
+
sharedBy: number;
|
|
71
|
+
}
|
|
72
|
+
interface HookRow {
|
|
73
|
+
id: string;
|
|
74
|
+
where: string;
|
|
75
|
+
wired: boolean;
|
|
76
|
+
}
|
|
77
|
+
interface CriterionRow {
|
|
78
|
+
id: string;
|
|
79
|
+
spec: string;
|
|
80
|
+
tests: {
|
|
81
|
+
file: string;
|
|
82
|
+
kind: string;
|
|
83
|
+
}[];
|
|
84
|
+
}
|
|
85
|
+
export interface DashboardData {
|
|
86
|
+
name: string;
|
|
87
|
+
days: number;
|
|
88
|
+
generated: Date;
|
|
89
|
+
attention: AttentionItem[];
|
|
90
|
+
wall: readonly WallCheck[];
|
|
91
|
+
wallCheckedAt: Date;
|
|
92
|
+
live: boolean;
|
|
93
|
+
ci: CiView & {
|
|
94
|
+
runs: number;
|
|
95
|
+
};
|
|
96
|
+
stats: Stats;
|
|
97
|
+
trends: Trends;
|
|
98
|
+
perDay: DayCount[];
|
|
99
|
+
stopped: Stopper[];
|
|
100
|
+
branches: BranchSummary[];
|
|
101
|
+
steps: ListedStep[];
|
|
102
|
+
guards: GuardRow[];
|
|
103
|
+
exceptions: ExceptionRow[];
|
|
104
|
+
hooks: HookRow[];
|
|
105
|
+
criteria: CriterionRow[] | null;
|
|
106
|
+
plugin: PluginView | null;
|
|
107
|
+
garden: GardenView | null;
|
|
108
|
+
}
|
|
109
|
+
export declare function hookRows(read: (file: string) => string | null, hooksPath: string): HookRow[];
|
|
110
|
+
export declare function attentionOf(wall: readonly WallCheck[], stopped: readonly Stopper[], hooks: readonly HookRow[]): AttentionItem[];
|
|
111
|
+
export declare function collectDashboard(input: DashboardInput): DashboardData;
|
|
112
|
+
export {};
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { branches, runsInPeriod, runsPerDay, stoppers, trends, } from "./breakdown.js";
|
|
3
|
+
import { takesGuard } from "./config.js";
|
|
4
|
+
import { listSteps } from "./plan.js";
|
|
5
|
+
import { summarize } from "./stats.js";
|
|
6
|
+
const DEFAULT_SPECS_FOLDER = "docs/visual/spec";
|
|
7
|
+
const HOOKS = [
|
|
8
|
+
{ id: "guard-edit", where: ".claude/settings.json" },
|
|
9
|
+
{ id: "guard-pr", where: ".claude/settings.json" },
|
|
10
|
+
{ id: "stop", where: ".claude/settings.json" },
|
|
11
|
+
{ id: "pre-commit", where: "core.hooksPath" },
|
|
12
|
+
{ id: "pre-push", where: "core.hooksPath" },
|
|
13
|
+
];
|
|
14
|
+
const TEST_FILE = /\.(?:test|spec|e2e)\.[cm]?[jt]sx?$|Tests?\.cs$|(?:^|\/)test_[^/]*\.py$|_test\.py$/;
|
|
15
|
+
function testKind(file) {
|
|
16
|
+
if (file.includes(".e2e."))
|
|
17
|
+
return "e2e";
|
|
18
|
+
if (/\.(?:test|spec)\.[cm]?tsx?$/.test(file))
|
|
19
|
+
return "unit";
|
|
20
|
+
if (file.endsWith(".cs"))
|
|
21
|
+
return ".NET";
|
|
22
|
+
if (file.endsWith(".py"))
|
|
23
|
+
return "Python";
|
|
24
|
+
return "other";
|
|
25
|
+
}
|
|
26
|
+
function namesCriterion(text, id) {
|
|
27
|
+
const escaped = id.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
28
|
+
return (['"', "'", "`"].some((quote) => text.includes(`${quote}${id}:`)) ||
|
|
29
|
+
new RegExp(`Trait\\(\\s*"criterion"\\s*,\\s*"${escaped}"\\s*\\)`).test(text));
|
|
30
|
+
}
|
|
31
|
+
function criteriaSettings(read) {
|
|
32
|
+
try {
|
|
33
|
+
const parsed = JSON.parse(read("criteria.json") ?? "{}");
|
|
34
|
+
const kinds = Array.isArray(parsed.kinds) ? parsed.kinds : [];
|
|
35
|
+
return {
|
|
36
|
+
specs: typeof parsed.specs === "string" ? parsed.specs : DEFAULT_SPECS_FOLDER,
|
|
37
|
+
kinds: kinds.flatMap((kind) => typeof kind.kind === "string" && Array.isArray(kind.paths)
|
|
38
|
+
? [{ kind: kind.kind, paths: kind.paths.filter((entry) => typeof entry === "string") }]
|
|
39
|
+
: []),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return { specs: DEFAULT_SPECS_FOLDER, kinds: [] };
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function namesAsWord(text, id) {
|
|
47
|
+
const escaped = id.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
48
|
+
return new RegExp(`(?<![A-Za-z0-9-])${escaped}(?![A-Za-z0-9-])`).test(text);
|
|
49
|
+
}
|
|
50
|
+
function criteriaOf(input) {
|
|
51
|
+
const settings = criteriaSettings(input.read);
|
|
52
|
+
const folder = settings.specs;
|
|
53
|
+
const specs = input.files.filter((file) => file.startsWith(`${folder}/`) && file.endsWith(".html") && !file.slice(folder.length + 1).includes("/"));
|
|
54
|
+
if (specs.length === 0)
|
|
55
|
+
return null;
|
|
56
|
+
const tests = input.files
|
|
57
|
+
.filter((file) => TEST_FILE.test(file))
|
|
58
|
+
.flatMap((file) => {
|
|
59
|
+
const text = input.read(file);
|
|
60
|
+
return text === null ? [] : [{ file, text }];
|
|
61
|
+
});
|
|
62
|
+
const own = settings.kinds.flatMap((kind) => input.files
|
|
63
|
+
.filter((file) => !TEST_FILE.test(file) && kind.paths.some((pattern) => path.matchesGlob(file, pattern)))
|
|
64
|
+
.flatMap((file) => {
|
|
65
|
+
const text = input.read(file);
|
|
66
|
+
return text === null ? [] : [{ file, text, kind: kind.kind }];
|
|
67
|
+
}));
|
|
68
|
+
return specs.flatMap((spec) => input.criterionIds(input.read(spec) ?? "").map((id) => ({
|
|
69
|
+
id,
|
|
70
|
+
spec,
|
|
71
|
+
tests: [
|
|
72
|
+
...tests.filter((test) => namesCriterion(test.text, id)).map((test) => ({ file: test.file, kind: testKind(test.file) })),
|
|
73
|
+
...own.filter((test) => namesAsWord(test.text, id)).map((test) => ({ file: test.file, kind: test.kind })),
|
|
74
|
+
],
|
|
75
|
+
})));
|
|
76
|
+
}
|
|
77
|
+
function guardRows(input) {
|
|
78
|
+
return input.catalog.guards
|
|
79
|
+
.filter((guard) => guard.profiles === "every" || guard.profiles.some((profile) => input.config.profiles.includes(profile)))
|
|
80
|
+
.map((guard) => {
|
|
81
|
+
const skipped = input.config.skippedGuards.find((entry) => entry.guard === guard.id);
|
|
82
|
+
const state = skipped !== undefined ? "skipped" : takesGuard(input.config, guard) ? "active" : "set not taken";
|
|
83
|
+
return { id: guard.id, set: guard.set ?? "core", state, skipReason: skipped?.reason ?? "" };
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
function latestRunOf(guard, runs) {
|
|
87
|
+
return runs.filter((run) => run.ran.includes(guard)).toSorted((a, b) => Date.parse(b.at) - Date.parse(a.at))[0];
|
|
88
|
+
}
|
|
89
|
+
function exceptionRows(input) {
|
|
90
|
+
return input.config.exceptions.map((exception) => {
|
|
91
|
+
const latest = latestRunOf(exception.guard, input.runs);
|
|
92
|
+
return {
|
|
93
|
+
guard: exception.guard,
|
|
94
|
+
target: exception.target,
|
|
95
|
+
reason: exception.reason,
|
|
96
|
+
covered: latest === undefined ? null : (latest.excepted[exception.guard] ?? 0),
|
|
97
|
+
sharedBy: input.config.exceptions.filter((other) => other.guard === exception.guard).length,
|
|
98
|
+
};
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
export function hookRows(read, hooksPath) {
|
|
102
|
+
const settings = read(".claude/settings.json") ?? "";
|
|
103
|
+
return HOOKS.map(({ id, where }) => {
|
|
104
|
+
const file = hooksPath === "" ? null : read(`${hooksPath}/${id}`);
|
|
105
|
+
const wired = where === "core.hooksPath" ? file?.includes(`hook ${id}`) === true : settings.includes(`hook ${id}`);
|
|
106
|
+
return { id, where, wired };
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
export function attentionOf(wall, stopped, hooks) {
|
|
110
|
+
return [
|
|
111
|
+
...wall
|
|
112
|
+
.filter((check) => check.state === "broken")
|
|
113
|
+
.map((check) => ({ kind: "wall", text: check.name, detail: "is not in place" })),
|
|
114
|
+
...stopped.map((stopper) => ({
|
|
115
|
+
kind: "broken",
|
|
116
|
+
text: `${stopper.id} stopped the gate ${stopper.runs} time${stopper.runs === 1 ? "" : "s"}`,
|
|
117
|
+
detail: `on ${stopper.branches.join(", ")}`,
|
|
118
|
+
})),
|
|
119
|
+
...hooks
|
|
120
|
+
.filter((hook) => !hook.wired)
|
|
121
|
+
.map((hook) => ({ kind: "have", text: `the ${hook.id} hook is not wired`, detail: `in ${hook.where}` })),
|
|
122
|
+
];
|
|
123
|
+
}
|
|
124
|
+
export function collectDashboard(input) {
|
|
125
|
+
const stopped = stoppers(input.runs, input.days, input.now);
|
|
126
|
+
return {
|
|
127
|
+
name: input.name,
|
|
128
|
+
days: input.days,
|
|
129
|
+
generated: input.now,
|
|
130
|
+
attention: attentionOf(input.wall, stopped, hookRows(input.read, input.hooksPath)),
|
|
131
|
+
wall: input.wall,
|
|
132
|
+
wallCheckedAt: input.wallCheckedAt,
|
|
133
|
+
live: input.live,
|
|
134
|
+
ci: { ...input.ci, runs: runsInPeriod(input.runs, input.days, input.now).filter((run) => run.ci).length },
|
|
135
|
+
stats: summarize(input.runs, input.days, input.now),
|
|
136
|
+
trends: trends(input.runs, input.days, input.now),
|
|
137
|
+
perDay: runsPerDay(input.runs, input.days, input.now),
|
|
138
|
+
stopped,
|
|
139
|
+
branches: branches(input.runs, input.days, input.now),
|
|
140
|
+
steps: listSteps({ root: "", quick: false, config: input.config, catalog: input.catalog, files: [], changed: () => [] }),
|
|
141
|
+
guards: guardRows(input),
|
|
142
|
+
exceptions: exceptionRows(input),
|
|
143
|
+
hooks: hookRows(input.read, input.hooksPath),
|
|
144
|
+
criteria: criteriaOf(input),
|
|
145
|
+
plugin: input.plugin,
|
|
146
|
+
garden: input.garden,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type CiView } from "./ciHistory.ts";
|
|
2
|
+
import { type Catalog } from "./config.ts";
|
|
3
|
+
import { type GardenView, type PluginView, type WallCheck } from "./dashboard.ts";
|
|
4
|
+
export declare const GARDEN_FILE = "suwgate-garden.jsonl";
|
|
5
|
+
export interface DashboardRequest {
|
|
6
|
+
cwd: string;
|
|
7
|
+
days: number;
|
|
8
|
+
out: string | undefined;
|
|
9
|
+
now: () => Date;
|
|
10
|
+
catalog: Catalog;
|
|
11
|
+
extraFiles: readonly string[];
|
|
12
|
+
wall: (root: string) => Promise<readonly WallCheck[]>;
|
|
13
|
+
ci: (root: string, now: Date) => Promise<CiView>;
|
|
14
|
+
criterionIds: (spec: string) => string[];
|
|
15
|
+
plugin: (root: string) => PluginView | null;
|
|
16
|
+
garden: (file: string | null) => GardenView | null;
|
|
17
|
+
}
|
|
18
|
+
export interface LiveSources {
|
|
19
|
+
page: () => Promise<string>;
|
|
20
|
+
fingerprint: () => string;
|
|
21
|
+
}
|
|
22
|
+
export declare function dashboardAsJson(request: DashboardRequest): Promise<{
|
|
23
|
+
exitCode: 0 | 2;
|
|
24
|
+
text: string;
|
|
25
|
+
}>;
|
|
26
|
+
export declare function dashboard(request: DashboardRequest): Promise<{
|
|
27
|
+
exitCode: 0 | 2;
|
|
28
|
+
text: string;
|
|
29
|
+
}>;
|
|
30
|
+
export declare function liveSources(request: DashboardRequest): LiveSources | {
|
|
31
|
+
exitCode: 2;
|
|
32
|
+
text: string;
|
|
33
|
+
};
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { readdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { allRuns, CI_HISTORY_FILE, syncedTo } from "./ciHistory.js";
|
|
5
|
+
import { CONFIG_FILE, loadConfig } from "./config.js";
|
|
6
|
+
import { collectDashboard } from "./dashboard.js";
|
|
7
|
+
import { dashboardJson } from "./dashboardJson.js";
|
|
8
|
+
import { renderDashboard } from "./dashboardPage.js";
|
|
9
|
+
import { HISTORY_FILE, inCommonDir, readRuns } from "./history.js";
|
|
10
|
+
import { git, repositoryFiles, repositoryRoot } from "./repository.js";
|
|
11
|
+
const DASHBOARD_FILE = "suwgate-dashboard.html";
|
|
12
|
+
const SPECS_FOLDER = "docs/visual/spec";
|
|
13
|
+
const WALL_TTL_MS = 5 * 60 * 1000;
|
|
14
|
+
export const GARDEN_FILE = "suwgate-garden.jsonl";
|
|
15
|
+
function repositoryName(root) {
|
|
16
|
+
const remote = git(root, ["remote", "get-url", "origin"])?.trim() ?? "";
|
|
17
|
+
const slug = /[:/]([^/:]+\/[^/:]+?)(?:\.git)?$/.exec(remote)?.[1];
|
|
18
|
+
return slug ?? path.basename(root);
|
|
19
|
+
}
|
|
20
|
+
function readOptional(root, file) {
|
|
21
|
+
try {
|
|
22
|
+
return readFileSync(path.join(root, file), "utf8");
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function prepare(request) {
|
|
29
|
+
const root = repositoryRoot(request.cwd);
|
|
30
|
+
if (root === null) {
|
|
31
|
+
return { exitCode: 2, text: `suwgate dashboard: ${request.cwd} is not inside a git repository: run it in a checkout` };
|
|
32
|
+
}
|
|
33
|
+
const loaded = loadConfig(root, request.catalog);
|
|
34
|
+
if ("problems" in loaded) {
|
|
35
|
+
return {
|
|
36
|
+
exitCode: 2,
|
|
37
|
+
text: ["suwgate dashboard: suwgate.yml is not valid:", ...loaded.problems.map((line) => ` ${line}`)].join("\n"),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
return { root, config: loaded.config };
|
|
41
|
+
}
|
|
42
|
+
function collect(request, root, config, wall, ci, live) {
|
|
43
|
+
return collectDashboard({
|
|
44
|
+
name: repositoryName(root),
|
|
45
|
+
days: request.days,
|
|
46
|
+
now: request.now(),
|
|
47
|
+
runs: ci.state === "off" ? readRuns(root) : allRuns(root),
|
|
48
|
+
ci,
|
|
49
|
+
config,
|
|
50
|
+
catalog: request.catalog,
|
|
51
|
+
wall: wall.checks,
|
|
52
|
+
wallCheckedAt: wall.checkedAt,
|
|
53
|
+
live,
|
|
54
|
+
files: repositoryFiles(root),
|
|
55
|
+
read: (file) => readOptional(root, file),
|
|
56
|
+
hooksPath: git(root, ["config", "--get", "core.hooksPath"])?.trim() ?? "",
|
|
57
|
+
criterionIds: request.criterionIds,
|
|
58
|
+
plugin: request.plugin(root),
|
|
59
|
+
garden: request.garden(inCommonDir(root, GARDEN_FILE)),
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
export async function dashboardAsJson(request) {
|
|
63
|
+
const prepared = prepare(request);
|
|
64
|
+
if ("exitCode" in prepared)
|
|
65
|
+
return prepared;
|
|
66
|
+
const wall = { checks: await request.wall(prepared.root), checkedAt: request.now() };
|
|
67
|
+
const ci = await request.ci(prepared.root, request.now());
|
|
68
|
+
return { exitCode: 0, text: JSON.stringify(dashboardJson(collect(request, prepared.root, prepared.config, wall, ci, false))) };
|
|
69
|
+
}
|
|
70
|
+
export async function dashboard(request) {
|
|
71
|
+
const prepared = prepare(request);
|
|
72
|
+
if ("exitCode" in prepared)
|
|
73
|
+
return prepared;
|
|
74
|
+
const { root, config } = prepared;
|
|
75
|
+
const wall = { checks: await request.wall(root), checkedAt: request.now() };
|
|
76
|
+
const html = renderDashboard(collect(request, root, config, wall, await request.ci(root, request.now()), false));
|
|
77
|
+
const target = request.out === undefined ? inCommonDir(root, DASHBOARD_FILE) : path.resolve(request.cwd, request.out);
|
|
78
|
+
if (target === null)
|
|
79
|
+
return { exitCode: 2, text: "suwgate dashboard: no place to write the page: pass --out <path>" };
|
|
80
|
+
writeFileSync(target, html);
|
|
81
|
+
return { exitCode: 0, text: target };
|
|
82
|
+
}
|
|
83
|
+
function cachedWall(read, now) {
|
|
84
|
+
let current = null;
|
|
85
|
+
let pending = null;
|
|
86
|
+
const expired = () => current === null || now().getTime() - current.checkedAt.getTime() >= WALL_TTL_MS;
|
|
87
|
+
return {
|
|
88
|
+
stamp: () => (expired() ? "due" : String(current?.checkedAt.getTime())),
|
|
89
|
+
get: async () => {
|
|
90
|
+
if (current !== null && !expired())
|
|
91
|
+
return current;
|
|
92
|
+
pending ??= read()
|
|
93
|
+
.then((checks) => {
|
|
94
|
+
current = { checks, checkedAt: now() };
|
|
95
|
+
return current;
|
|
96
|
+
})
|
|
97
|
+
.finally(() => {
|
|
98
|
+
pending = null;
|
|
99
|
+
});
|
|
100
|
+
return await pending;
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function backgroundCi(read, before, now) {
|
|
105
|
+
let current = { state: "syncing", syncedTo: before, reason: "" };
|
|
106
|
+
let finishedAt = 0;
|
|
107
|
+
let pending = false;
|
|
108
|
+
return {
|
|
109
|
+
get: () => {
|
|
110
|
+
if (!pending && now().getTime() - finishedAt >= WALL_TTL_MS) {
|
|
111
|
+
pending = true;
|
|
112
|
+
read()
|
|
113
|
+
.then((view) => {
|
|
114
|
+
current = view;
|
|
115
|
+
})
|
|
116
|
+
.catch((error) => {
|
|
117
|
+
current = {
|
|
118
|
+
state: "unavailable",
|
|
119
|
+
syncedTo: current.syncedTo,
|
|
120
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
121
|
+
};
|
|
122
|
+
})
|
|
123
|
+
.finally(() => {
|
|
124
|
+
finishedAt = now().getTime();
|
|
125
|
+
pending = false;
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
return current;
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
function fileStamp(file) {
|
|
133
|
+
try {
|
|
134
|
+
const found = statSync(file);
|
|
135
|
+
return `${file}:${found.size}:${found.mtimeMs}`;
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
return `${file}:-`;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
function folderStamps(folder) {
|
|
142
|
+
try {
|
|
143
|
+
return readdirSync(folder)
|
|
144
|
+
.toSorted()
|
|
145
|
+
.map((name) => fileStamp(path.join(folder, name)));
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
return [];
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
export function liveSources(request) {
|
|
152
|
+
const prepared = prepare(request);
|
|
153
|
+
if ("exitCode" in prepared)
|
|
154
|
+
return prepared;
|
|
155
|
+
const { root } = prepared;
|
|
156
|
+
const wall = cachedWall(() => request.wall(root), request.now);
|
|
157
|
+
const ci = backgroundCi(() => request.ci(root, request.now()), syncedTo(root), request.now);
|
|
158
|
+
const watched = [
|
|
159
|
+
inCommonDir(root, HISTORY_FILE),
|
|
160
|
+
inCommonDir(root, CI_HISTORY_FILE),
|
|
161
|
+
inCommonDir(root, GARDEN_FILE),
|
|
162
|
+
path.join(root, CONFIG_FILE),
|
|
163
|
+
path.join(root, ".claude", "settings.json"),
|
|
164
|
+
path.join(root, "criteria.json"),
|
|
165
|
+
...request.extraFiles,
|
|
166
|
+
];
|
|
167
|
+
return {
|
|
168
|
+
page: async () => {
|
|
169
|
+
const current = prepare(request);
|
|
170
|
+
if ("exitCode" in current)
|
|
171
|
+
throw new Error(current.text);
|
|
172
|
+
return renderDashboard(collect(request, root, current.config, await wall.get(), ci.get(), true));
|
|
173
|
+
},
|
|
174
|
+
fingerprint: () => createHash("sha1")
|
|
175
|
+
.update([
|
|
176
|
+
...watched.map((file) => (file === null ? "-" : fileStamp(file))),
|
|
177
|
+
...folderStamps(path.join(root, SPECS_FOLDER)),
|
|
178
|
+
wall.stamp(),
|
|
179
|
+
JSON.stringify(ci.get()),
|
|
180
|
+
request.now().toISOString().slice(0, 10),
|
|
181
|
+
].join("\n"))
|
|
182
|
+
.digest("hex")
|
|
183
|
+
.slice(0, 16),
|
|
184
|
+
};
|
|
185
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ABOUT } from "./dashboardPage.js";
|
|
2
|
+
const SCHEMA_VERSION = 1;
|
|
3
|
+
export function dashboardJson(data) {
|
|
4
|
+
const { quiet, ...stats } = data.stats;
|
|
5
|
+
return {
|
|
6
|
+
schemaVersion: SCHEMA_VERSION,
|
|
7
|
+
repository: data.name,
|
|
8
|
+
days: data.days,
|
|
9
|
+
generated: data.generated.toISOString(),
|
|
10
|
+
attention: data.attention.map((item) => ({ kind: item.kind, text: item.text, detail: item.detail, part: item.kind })),
|
|
11
|
+
ci: data.ci,
|
|
12
|
+
wall: { checkedAt: data.wallCheckedAt.toISOString(), checks: data.wall },
|
|
13
|
+
runs: { ...stats, trends: data.trends, perDay: data.perDay },
|
|
14
|
+
stopped: data.stopped,
|
|
15
|
+
branches: data.branches,
|
|
16
|
+
steps: data.steps,
|
|
17
|
+
guards: data.guards,
|
|
18
|
+
exceptions: data.exceptions,
|
|
19
|
+
hooks: data.hooks,
|
|
20
|
+
quietGuards: quiet,
|
|
21
|
+
criteria: data.criteria,
|
|
22
|
+
plugin: data.plugin,
|
|
23
|
+
garden: data.garden,
|
|
24
|
+
about: ABOUT,
|
|
25
|
+
};
|
|
26
|
+
}
|