mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-27 10:24:00 +01:00
train S2: merge #3691 at c4f7a21885 v1.92.68.0
This commit is contained in:
commit
06650bfe70
12 files changed
+312
-73
No files matched your search
+53
-3
@@ -53,8 +53,8 @@ export function declaredVerify(claudeMd: string): string | null {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** The '## Health Stack' section (/health's own format): '- key: command' rows. 'test' and 'tests' are left to pregate; a repeated key gets -2, -3 so every check has its own name. */
|
||||
export function healthStack(claudeMd: string): Check[] {
|
||||
/** The '## Health Stack' section (/health's own format): '- key: command' rows. 'test' and 'tests' are left to pregate unless keepTests (/health scores them); a repeated key gets -2, -3 so every check has its own name. */
|
||||
export function healthStack(claudeMd: string, keepTests = false): Check[] {
|
||||
const lines = claudeMd.split('\n');
|
||||
const start = lines.findIndex(l => /^##\s+Health Stack\s*$/.test(l));
|
||||
if (start < 0) return [];
|
||||
@@ -64,7 +64,7 @@ export function healthStack(claudeMd: string): Check[] {
|
||||
for (const line of lines.slice(start + 1)) {
|
||||
if (/^#{1,6}\s/.test(line)) break;
|
||||
const m = /^\s*[-*]\s*([A-Za-z0-9_-]+):\s*(.+?)\s*$/.exec(line);
|
||||
if (!m || ['test', TESTS].includes(m[1].toLowerCase())) continue;
|
||||
if (!m || (!keepTests && ['test', TESTS].includes(m[1].toLowerCase()))) continue;
|
||||
const key = m[1].toLowerCase();
|
||||
let n = seen.get(key) ?? 0;
|
||||
let name = key;
|
||||
@@ -127,6 +127,56 @@ export function detectChecks(root: string): Check[] {
|
||||
});
|
||||
}
|
||||
|
||||
export const HEALTH_DIMENSIONS = ['typecheck', 'lint', 'test', 'deadcode', 'shell', 'gbrain'] as const;
|
||||
export interface HealthDimensions { checks: Array<{ dimension: string; command: string; source: string }>; missing: Array<{ dimension: string; reason: string }> }
|
||||
|
||||
/**
|
||||
* /health Step 1, in the directory /health runs from: a Health Stack section row for row (test rows kept; a section with no
|
||||
* rows still turns auto-detection off), else /health's own presence rules,
|
||||
* which may use a tool found on PATH (unlike /verify's declared-only manifestChecks). Reads files and PATH; runs nothing.
|
||||
* test/health-detect-no-loss.test.ts holds these rules to the shell block they replaced.
|
||||
*/
|
||||
export function healthDimensions(root: string, env: NodeJS.ProcessEnv = process.env): HealthDimensions {
|
||||
const claude = read(path.join(root, 'CLAUDE.md')) ?? '';
|
||||
const stack = healthStack(claude, true);
|
||||
if (/^##\s+Health Stack\s*$/m.test(claude)) {
|
||||
const keys = new Set(stack.map(c => c.name.replace(/-\d+$/, '').replace(/^tests$/, 'test')));
|
||||
return { checks: stack.map(c => ({ dimension: c.name, command: c.command, source: c.source })), missing: HEALTH_DIMENSIONS.filter(d => !keys.has(d)).map(dimension => ({ dimension, reason: stack.length ? 'not in the CLAUDE.md Health Stack section' : 'the CLAUDE.md Health Stack section has no "- key: command" rows' })) };
|
||||
}
|
||||
const has = (file: string) => fs.existsSync(path.join(root, file));
|
||||
const which = (bin: string) => Bun.which(bin, { PATH: env.PATH ?? '' }) !== null;
|
||||
const pkgText = read(path.join(root, 'package.json'));
|
||||
const pyproject = read(path.join(root, 'pyproject.toml'));
|
||||
let testScript: unknown;
|
||||
try { testScript = pkgText?.includes('"test"') ? JSON.parse(pkgText).scripts?.test : undefined; } catch { /* an unreadable package.json declares nothing */ }
|
||||
const listDir = (dir: string) => { try { return fs.readdirSync(dir); } catch { return []; } };
|
||||
const shellFiles = ['.', 'scripts', 'bin'].some(dir => listDir(path.join(root, dir)).some(f => f.endsWith('.sh')));
|
||||
const found: Array<[string, string, string] | false> = [
|
||||
has('tsconfig.json') && ['typecheck', 'tsc --noEmit', 'tsconfig.json'],
|
||||
(has('biome.json') || has('biome.jsonc')) && ['lint', 'biome check .', 'biome.json'],
|
||||
listDir(root).some(f => /^(eslint\.config\..+|\.eslintrc(\..+)?)$/.test(f)) && ['lint', 'eslint .', 'eslint config'],
|
||||
!!pyproject && /pylint|ruff/.test(pyproject) && ['lint', 'ruff check .', 'pyproject.toml'],
|
||||
typeof testScript === 'string' && testScript !== '' && !/[\r\n]/.test(testScript) && ['test', testScript, 'package.json'],
|
||||
!!pyproject && pyproject.includes('pytest') && ['test', 'pytest', 'pyproject.toml'],
|
||||
has('Cargo.toml') && ['test', 'cargo test', 'Cargo.toml'],
|
||||
has('go.mod') && ['test', 'go test ./...', 'go.mod'],
|
||||
which('knip') && ['deadcode', 'knip', 'PATH'],
|
||||
!!pkgText?.includes('"knip"') && ['deadcode', 'npx knip', 'package.json'],
|
||||
which('shellcheck') && shellFiles && ['shell', 'shellcheck', 'PATH, *.sh in the root, scripts/ or bin/'],
|
||||
which('gbrain') && !!env.HOME && fs.existsSync(path.join(env.HOME, '.gbrain', 'config.json')) && ['gbrain', 'timeout 5s gbrain doctor --json', 'PATH, ~/.gbrain/config.json'],
|
||||
];
|
||||
const checks = found.filter((c): c is [string, string, string] => !!c).map(([dimension, command, source]) => ({ dimension, command, source }));
|
||||
const reasons: Record<string, string> = {
|
||||
typecheck: 'no tsconfig.json',
|
||||
lint: 'no biome.json, eslint config, or pyproject.toml naming ruff or pylint',
|
||||
test: typeof testScript === 'string' && /[\r\n]/.test(testScript) ? 'the package.json test script spans lines; list it in a CLAUDE.md Health Stack section' : 'no package.json test script, pytest in pyproject.toml, Cargo.toml or go.mod',
|
||||
deadcode: 'knip is not on PATH or in package.json',
|
||||
shell: which('shellcheck') ? 'no *.sh file in the root, scripts/ or bin/' : 'shellcheck is not on PATH',
|
||||
gbrain: 'gbrain is not on PATH or ~/.gbrain/config.json is absent',
|
||||
};
|
||||
return { checks, missing: HEALTH_DIMENSIONS.filter(d => !checks.some(c => c.dimension === d)).map(dimension => ({ dimension, reason: reasons[dimension] })) };
|
||||
}
|
||||
|
||||
/** Every command a run could execute, read without executing any: the checks, the pregate runner and each selection manifest command. */
|
||||
export function commandSet(root: string, checks: readonly Check[], config: Pick<PregateConfig, 'runner' | 'selection'>): string {
|
||||
return sha256(JSON.stringify({ checks: checks.map(c => [c.name, c.command, c.body ?? '']), scripts: packageScripts(root), runner: config.runner ?? detectedRunnerInputs(root), selection: Object.entries(config.selection ?? {}).sort(([a], [b]) => a.localeCompare(b)) }));
|
||||
|
||||
Reference in new issue
Block a user