fix(hooks): prefer installed CLI for stale index guidance

This commit is contained in:
AlphaComposite 2026-05-11 04:18:59 +03:00
parent d69eadfb7f
commit 1b15c9ad92
6 changed files with 17 additions and 6 deletions

View file

@ -298,11 +298,12 @@ function handlePostToolUse(input) {
// If HEAD matches last indexed commit, no reindex needed
if (currentHead && currentHead === lastCommit) return;
const analyzeCmd = `npx gitnexus analyze${hadEmbeddings ? ' --embeddings' : ''}`;
const analyzeCmd = `gitnexus analyze${hadEmbeddings ? ' --embeddings' : ''}`;
const npxAnalyzeCmd = `npx gitnexus analyze${hadEmbeddings ? ' --embeddings' : ''}`;
sendHookResponse(
'PostToolUse',
`GitNexus index is stale (last indexed: ${lastCommit ? lastCommit.slice(0, 7) : 'never'}). ` +
`Run \`${analyzeCmd}\` to update the knowledge graph.`,
`Run \`${analyzeCmd}\` to update the knowledge graph, or \`${npxAnalyzeCmd}\` if the CLI is not installed.`,
);
}

View file

@ -119,7 +119,7 @@ function generateGitNexusContent(
This project is indexed by GitNexus as **${projectName}**${noStats ? '' : ` (${stats.nodes || 0} symbols, ${stats.edges || 0} relationships, ${stats.processes || 0} execution flows)`}. Use the GitNexus MCP tools to understand code, assess impact, and navigate safely.
> If any GitNexus tool warns the index is stale, run \`npx gitnexus analyze\` in terminal first.
> If any GitNexus tool warns the index is stale, run \`gitnexus analyze\` in terminal first (or \`npx gitnexus analyze\` if the CLI is not installed).
## Always Do

View file

@ -340,7 +340,9 @@ async function getContextResource(backend: LocalBackend, repoName?: string): Pro
lines.push(' - cypher: Raw graph queries');
lines.push(' - list_repos: Discover all indexed repositories');
lines.push('');
lines.push('re_index: Run `npx gitnexus analyze` in terminal if data is stale');
lines.push(
're_index: Run `gitnexus analyze` in terminal if data is stale (or `npx gitnexus analyze` if the CLI is not installed)',
);
lines.push('');
lines.push('resources_available:');
lines.push(' - gitnexus://repos: All indexed repositories');
@ -581,7 +583,7 @@ async function getSetupResource(backend: LocalBackend): Promise<string> {
const repos = await backend.listRepos();
if (repos.length === 0) {
return '# GitNexus\n\nNo repositories indexed. Run: `npx gitnexus analyze` in a repository.';
return '# GitNexus\n\nNo repositories indexed. Run: `gitnexus analyze` in a repository (or `npx gitnexus analyze` if the CLI is not installed).';
}
const sections: string[] = [];

View file

@ -77,7 +77,10 @@ describe.each(HOOKS)('hooks e2e ($name)', ({ name, path: hookPath }) => {
const output = parseHookOutput(result.stdout);
expect(output).not.toBeNull();
expect(output!.additionalContext).toContain('stale');
expect(output!.additionalContext).toContain('npx gitnexus analyze');
expect(output!.additionalContext).toContain('Run `gitnexus analyze`');
expect(output!.additionalContext).toContain(
'`npx gitnexus analyze` if the CLI is not installed',
);
});
it('stays silent when meta.json lastCommit matches HEAD', () => {

View file

@ -57,6 +57,8 @@ describe('generateAIContextFiles', () => {
const content = await fs.readFile(path.join(tmpDir, 'CLAUDE.md'), 'utf-8');
expect(content).toContain('If any GitNexus tool warns the index is stale');
expect(content).toContain('run `gitnexus analyze`');
expect(content).toContain('`npx gitnexus analyze` if the CLI is not installed');
expect(content).toContain('## Always Do');
expect(content).toContain('## Never Do');
expect(content).toContain('## Resources');

View file

@ -188,6 +188,7 @@ describe('readResource', () => {
const backend = createMockBackend({ repos: [] });
const result = await readResource('gitnexus://repos', backend);
expect(result).toContain('No repositories indexed');
expect(result).toContain('Run: gitnexus analyze');
});
it('routes gitnexus://setup to setup resource', async () => {
@ -211,6 +212,8 @@ describe('readResource', () => {
const backend = createMockBackend({ repos: [] });
const result = await readResource('gitnexus://setup', backend);
expect(result).toContain('No repositories indexed');
expect(result).toContain('Run: `gitnexus analyze`');
expect(result).toContain('`npx gitnexus analyze` if the CLI is not installed');
});
it('routes group contracts resource through backend', async () => {