mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-20 15:13:47 +01:00
Rewrite of #2217. Required team mode wrote a bash check-gstack.sh and registered it as "$CLAUDE_PROJECT_DIR/.claude/hooks/check-gstack.sh", which only works when the hook runner is a POSIX shell. Under cmd.exe or PowerShell (Windows, Copilot CLI) the variable is not expanded and the .sh cannot run, so an intended allow or deny became a hook error. - The hook is now .claude/hooks/check-gstack.cjs: the same root search (GSTACK_ROOT, every host's global root, the migrated repo), with the home directory from HOME, then USERPROFILE, then os.homedir(). It prints {} when gstack resolves and the PreToolUse deny JSON with exit 2 when not. - The registered command is `node -e "require(require('path').resolve( process.env.CLAUDE_PROJECT_DIR || '.', '.claude', 'hooks', 'check-gstack.cjs'))"`: no `$` or `%`, so sh, cmd.exe and PowerShell all pass it through unchanged. - An existing check-gstack registration (matcher Skill or Skill|skill) is replaced in place; duplicates are dropped and unrelated hooks in the same entry kept. The legacy .sh is deleted only after the settings write succeeds (a tracked one joins the suggested git add), and is kept when bun is missing or the write fails, which now reports instead of claiming success. Tests: team-mode and the hook-schema test run the command as registered; test/team-init-hook-shell-neutral.test.ts runs it through the platform's default shell (cmd.exe on the Windows lane, which curates it in) and through PowerShell when installed (verified here in the pwsh 7 container). The routing-probe check for the bash `_GSTACK_ROOT` gate now pins the same contract in the .cjs: every host root probed, block only when none resolves. Co-authored-by: neallee2012 <1499497+neallee2012@users.noreply.github.com> Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
132 lines
5.5 KiB
TypeScript
132 lines
5.5 KiB
TypeScript
/**
|
|
* Routing probe + team-init install resolution — gate-tier tests (#2500).
|
|
*
|
|
* 1. The preamble's HAS_ROUTING probe must check AGENTS.md as well as
|
|
* CLAUDE.md. Non-Claude hosts (Codex, Cursor, generic harnesses) route
|
|
* skills via AGENTS.md — the cross-harness convention file. Before this
|
|
* fix, a repo with AGENTS.md routing but no CLAUDE.md reported
|
|
* HAS_ROUTING: no and got nagged to create CLAUDE.md.
|
|
*
|
|
* 2. gstack-team-init's required-mode enforcement (the CLAUDE.md
|
|
* verification snippet and the generated check-gstack.cjs hook) must
|
|
* resolve the install root across GSTACK_ROOT + every host's global
|
|
* install location, never hardcode ~/.claude/skills/gstack. The drift
|
|
* test pins the probe list against the hosts registry so a new host
|
|
* can't silently fall out of team-mode enforcement.
|
|
*
|
|
* Re-derived from community PR #2500 by @gamerey43.
|
|
*/
|
|
import { describe, test, expect } from 'bun:test';
|
|
import { execSync } from 'child_process';
|
|
import * as fs from 'fs';
|
|
import * as os from 'os';
|
|
import * as path from 'path';
|
|
import { HOST_PATHS } from '../scripts/resolvers/types';
|
|
import type { TemplateContext } from '../scripts/resolvers/types';
|
|
import { generatePreambleBash } from '../scripts/resolvers/preamble/generate-preamble-bash';
|
|
import { ALL_HOST_CONFIGS } from '../hosts/index';
|
|
|
|
const ROOT = path.join(import.meta.dir, '..');
|
|
|
|
function makeCtx(host: 'claude' | 'codex'): TemplateContext {
|
|
return {
|
|
skillName: 'test-skill',
|
|
tmplPath: 'test.tmpl',
|
|
host,
|
|
paths: HOST_PATHS[host],
|
|
preambleTier: 2,
|
|
};
|
|
}
|
|
|
|
// Token-reduction Phase 1: the probe bash moved from the rendered preamble
|
|
// into bin/gstack-skill-start (invoked by every host's preamble fence). The
|
|
// probe block under test is extracted from the LIVE script bytes.
|
|
const SKILL_START_SCRIPT = fs.readFileSync(
|
|
path.join(ROOT, 'bin', 'gstack-skill-start'),
|
|
'utf-8',
|
|
);
|
|
|
|
/** Extract the routing-probe block from the skill-start script. */
|
|
function extractRoutingProbe(scriptText: string): string {
|
|
const start = scriptText.indexOf('_HAS_ROUTING="no"');
|
|
expect(start).toBeGreaterThan(-1);
|
|
const end = scriptText.indexOf('done', start);
|
|
expect(end).toBeGreaterThan(start);
|
|
return scriptText.slice(start, end + 'done'.length);
|
|
}
|
|
|
|
describe('routing probe checks AGENTS.md too (#2500)', () => {
|
|
for (const host of ['claude', 'codex'] as const) {
|
|
test(`preamble reaches the CLAUDE.md AND AGENTS.md probe (${host})`, () => {
|
|
// The render must invoke the script that owns the probe...
|
|
const rendered = generatePreambleBash(makeCtx(host));
|
|
expect(rendered).toContain('gstack-skill-start');
|
|
// ...and the probe must cover both convention files.
|
|
const probe = extractRoutingProbe(SKILL_START_SCRIPT);
|
|
expect(probe).toContain('CLAUDE.md');
|
|
expect(probe).toContain('AGENTS.md');
|
|
});
|
|
}
|
|
|
|
test('live probe block: AGENTS.md-only repo reports HAS_ROUTING=yes', () => {
|
|
const probe = extractRoutingProbe(SKILL_START_SCRIPT);
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'routing-probe-'));
|
|
try {
|
|
fs.writeFileSync(
|
|
path.join(dir, 'AGENTS.md'),
|
|
'## Skill routing\n\n- Bugs → /investigate\n',
|
|
);
|
|
const out = execSync(
|
|
`bash -c '${probe.replace(/'/g, `'\\''`)}\necho "HAS_ROUTING: $_HAS_ROUTING"'`,
|
|
{ cwd: dir, encoding: 'utf-8', timeout: 30_000 },
|
|
);
|
|
expect(out).toContain('HAS_ROUTING: yes');
|
|
} finally {
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test('live probe block: repo with neither file reports HAS_ROUTING=no', () => {
|
|
const probe = extractRoutingProbe(SKILL_START_SCRIPT);
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'routing-probe-'));
|
|
try {
|
|
const out = execSync(
|
|
`bash -c '${probe.replace(/'/g, `'\\''`)}\necho "HAS_ROUTING: $_HAS_ROUTING"'`,
|
|
{ cwd: dir, encoding: 'utf-8', timeout: 30_000 },
|
|
);
|
|
expect(out).toContain('HAS_ROUTING: no');
|
|
} finally {
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('team-init resolves GSTACK_ROOT across every host (#2500)', () => {
|
|
const teamInit = fs.readFileSync(path.join(ROOT, 'bin', 'gstack-team-init'), 'utf-8');
|
|
|
|
test('probe list covers GSTACK_ROOT env + every registered host globalRoot + migrated repo', () => {
|
|
expect(teamInit).toContain('"${GSTACK_ROOT:-}"');
|
|
for (const config of ALL_HOST_CONFIGS) {
|
|
expect(teamInit).toContain(`$HOME/${config.globalRoot}`);
|
|
}
|
|
expect(teamInit).toContain('$HOME/.gstack/repos/gstack');
|
|
});
|
|
|
|
test('enforcement no longer hardcodes the Claude path as the only gate', () => {
|
|
expect(teamInit).not.toContain('test -d ~/.claude/skills/gstack/bin');
|
|
expect(teamInit).not.toContain('if [ ! -d "$HOME/.claude/skills/gstack/bin" ]');
|
|
});
|
|
|
|
test('generated hook blocks only when NO install root resolves', () => {
|
|
// The hook (CommonJS, so it runs under any hook shell) probes GSTACK_ROOT
|
|
// and every host's global root, and its block branch gates on none
|
|
// resolving, not on any single hardcoded directory.
|
|
const hook = teamInit.slice(teamInit.indexOf("cat > \"$HOOKS_DIR/check-gstack.cjs\""), teamInit.indexOf('\nHOOK_EOF\n'));
|
|
expect(hook).toContain('process.env.GSTACK_ROOT');
|
|
for (const config of ALL_HOST_CONFIGS) expect(hook).toContain(`'${config.globalRoot}'`);
|
|
expect(hook).toContain("'.gstack/repos/gstack'");
|
|
expect(hook).toContain('const gstackRoot = roots.find(');
|
|
expect(hook).toContain('if (gstackRoot) {');
|
|
});
|
|
});
|