fix(health): keep Health Stack command bytes, honor an empty section, detect from the working directory (review pass 1)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
This commit is contained in:
garrytanandcapy-ai[bot] committed 2026-10-31 12:56:57 +00:00
1 parent d7e3e601d7
commit ef52e5d690
3 files changed
+30 -7

No files matched your search

+1 -1
View File
@@ -48,7 +48,7 @@ async function main(argv: string[]): Promise<number> {
const mode = flag(args, '--for');
if (args.includes('--for')) {
if (verb !== 'detect' || mode !== 'health') { process.stderr.write(USAGE); return 64; }
const d = healthDimensions(repoRoot() ?? process.cwd());
const d = healthDimensions(process.cwd());
for (const c of d.checks) process.stdout.write(`DIMENSION ${c.dimension} ${c.command} (${c.source})\n`);
for (const m of d.missing) process.stdout.write(`DIMENSION ${m.dimension} none (${m.reason})\n`);
return 0;
+8 -6
View File
@@ -71,7 +71,7 @@ export function healthStack(claudeMd: string, keepTests = false): Check[] {
while (taken.has(name)) name = `${key}-${++n + 1}`;
seen.set(key, n);
taken.add(name);
checks.push({ name, command: m[2].replace(/`/g, ''), source: 'CLAUDE.md Health Stack' });
checks.push({ name, command: m[2].replace(/^`([^`]+)`$/, '$1'), source: 'CLAUDE.md Health Stack' });
}
return checks;
}
@@ -131,14 +131,15 @@ export const HEALTH_DIMENSIONS = ['typecheck', 'lint', 'test', 'deadcode', 'shel
export interface HealthDimensions { checks: Array<{ dimension: string; command: string; source: string }>; missing: Array<{ dimension: string; reason: string }> }
/**
* /health Step 1: a Health Stack section row for row (test rows kept, no auto-detection), else /health's own presence rules,
* /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 (stack.length) {
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: 'not in the CLAUDE.md Health Stack section' })) };
}
@@ -148,11 +149,12 @@ export function healthDimensions(root: string, env: NodeJS.ProcessEnv = process.
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 shellFiles = ['.', 'scripts', 'bin'].some(dir => { try { return fs.readdirSync(path.join(root, dir)).some(f => f.endsWith('.sh')); } catch { return false; } });
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'],
fs.readdirSync(root).some(f => /^(eslint\.config\..+|\.eslintrc(\..+)?)$/.test(f)) && ['lint', 'eslint .', 'eslint config'],
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 !== '' && ['test', testScript, 'package.json'],
!!pyproject && pyproject.includes('pytest') && ['test', 'pytest', 'pyproject.toml'],
@@ -161,7 +163,7 @@ export function healthDimensions(root: string, env: NodeJS.ProcessEnv = process.
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') && fs.existsSync(path.join(env.HOME ?? '', '.gbrain', 'config.json')) && ['gbrain', 'timeout 5s gbrain doctor --json', 'PATH, ~/.gbrain/config.json'],
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> = {
+21
View File
@@ -142,6 +142,27 @@ describe('/health detection: no check lost (GST-116 PR 2)', () => {
}
});
// Value: protects=a Health Stack keeps shell syntax, an empty section still turns auto-detection off, and detection runs
// where /health runs (a monorepo package); fails_when=backticks inside a command are stripped (a failing checker becomes
// a lookup), a section with no rows falls through to auto-detection, or the bin detects from the git top-level;
// why_new=review pass 1 outside voice; seam=the bin from a subdirectory
test('Health Stack bytes, an empty section, and the working directory', () => {
const tick = String.fromCharCode(96);
const subst = healthDimensions(repo('hs-subst', { 'CLAUDE.md': `## Health Stack\n\n- test: test "$(${tick}command -v false${tick})" = x\n- lint: ${tick}eslint .${tick}\n` }), { PATH: '', HOME: bareHome });
expect(subst.checks.map(c => c.command)).toEqual([`test "$(${tick}command -v false${tick})" = x`, 'eslint .']);
const empty = healthDimensions(repo('hs-empty', { 'tsconfig.json': '{}', 'CLAUDE.md': '## Health Stack\n\nNo health tools are configured.\n' }), { PATH: stubs, HOME: gbrainHome });
expect(empty.checks).toEqual([]);
expect(empty.missing.map(m => m.dimension)).toEqual([...HEALTH_DIMENSIONS]);
const mono = repo('monorepo', { 'tsconfig.json': '{}', 'packages/app/package.json': JSON.stringify({ scripts: { test: 'vitest run' } }) });
expect(spawnSync('git', ['init', '-q'], { cwd: mono, timeout: 30_000 }).status).toBe(0);
const env = { ...process.env, PATH: process.env.PATH, HOME: bareHome };
const pkg = detector(path.join(mono, 'packages', 'app'), env);
expect(pkg.found).toContain('test: vitest run');
expect(pkg.none).toContain('typecheck');
const noHome = repo('no-home', { '.gbrain/config.json': '{}' });
expect(healthDimensions(noHome, { PATH: stubs, HOME: '' }).checks.map(c => c.dimension)).not.toContain('gbrain');
});
// Value: protects=an unavailable category says why, so Step 2 records SKIPPED with a reason; fails_when=a none line
// loses its reason or shellcheck's two causes collapse; why_new=new output; seam=none
test('none lines name the reason', () => {