mirror of
https://github.com/abhigyanpatwari/GitNexus.git
synced 2026-11-29 23:37:41 +00:00
* feat(mcp): add an opt-in refresh_index tool A local `gitnexus mcp` server started with GITNEXUS_MCP_ENABLE_REFRESH=1 lists refresh_index, which rebuilds one registered index from its working tree through the forked analyze worker auto-sync uses, with the same index-only options: no AGENTS.md, CLAUDE.md, skill, hook or repository file is written. The tool lives outside GITNEXUS_TOOLS, so the default catalog and its byte budget are unchanged. `gitnexus serve` never passes the option, so the web/Docker /api/mcp surface cannot list it; read-only mode hides and rejects it through the existing allowlist. A repository whose last index recorded more than GITNEXUS_MCP_REFRESH_MAX_FILES files (default 5000), or none, is refused. After GITNEXUS_MCP_REFRESH_TIMEOUT_MS the call stops waiting; the worker never interrupts a started analysis, so it finishes in the background and keeps the index lock. One refresh runs per repository at a time, and the slot is held until the worker is done. A collapsed graph write is reported as an error, as analyze and serve report it. dry_run reports staleness, the file count and whether .gitnexusrc asks for embeddings. Worker progress is relayed as MCP progress notifications when the client sends a progress token. Malformed limits fail startup. Refs #3524. * fix(mcp): keep worker log lines out of refresh progress The analyze worker sends its log lines as progress messages with percent -1. The launcher now drops them before onProgress, so the listener only sees real progress (the server relay already discarded them). The refresh tests now unset GITNEXUS_MCP_REFRESH_MAX_FILES and GITNEXUS_MCP_REFRESH_TIMEOUT_MS, so an inherited value cannot change the defaults they assert. The tool-arguments comment names the schema lookup it actually uses. * test(mcp): check ignored paths after a refresh, not only git status analyze excludes its own storage through .git/info/exclude, so a clean git status says nothing about ignored paths. The e2e now also lists them and allows none outside .gitnexus/. * fix(mcp): count the working tree against the refresh file limit refresh_index compared GITNEXUS_MCP_REFRESH_MAX_FILES with the file count of the last index. A stale index is exactly the case where that count can be far below the working tree, so a repository that grew from 100 to 50,000 files since its last index was admitted under the default limit of 5,000 and dry_run reported 100. countScanCandidates (filesystem-walker.ts) walks the same glob patterns and ignore rules as walkRepositoryPaths, without stat-ing, and stops one past the limit, so admission costs the same on a huge tree as on a small one. Large files, non-regular entries and declaration siblings are not subtracted, so the count is an upper bound on what analyze indexes. refreshIndex admits the rebuild from that count. dry_run reports it as estimatedFiles (null past the limit, where only "more than maxFiles" is known, and with no limit, where nothing is counted) and the registry count as lastIndexedFiles. An index without a recorded count is no longer refused, since the tree is counted. A tree that cannot be read is refused before the worker, like an unreadable .gitnexusrc. Tests: the walker count matches the scan's paths, stops at limit + 1, and counts a file the scan drops for its size. The backend refuses a tree over the limit whose last index was under it, without starting the worker, and does not count with no limit. The e2e test commits two files past a limit of 4 after an index of 3 and checks that dry_run and the refresh refuse, with the real enumeration, and that the index stays at its commit. Reading the registry count again fails the backend and e2e tests; dropping the early stop fails the walker test. * test(ingestion): ignore a directory only .gitignore excludes in the count test The fixture ignored generated/ through .gitignore, but the ignore service already skips a root generated/ directory on its own, so the test passed with .gitignore disabled. scratch/ is excluded by .gitignore alone. * test(mcp): cover refresh progress and readers * fix(mcp): retain timed-out refresh slots (#3769) Keep the repository busy until an abandoned worker exits, so retries cannot start another worker while the original refresh is still running. Cover late success and failure, and update the timeout documentation. --------- Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com> Co-authored-by: Gergő Magyar <gergomagyar@icloud.com>
320 lines
13 KiB
TypeScript
320 lines
13 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
// #3524: LocalBackend's refresh_index around a stubbed analyze worker. The real
|
|
// worker, registry, and git are covered by test/integration/mcp-refresh-index-e2e.
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
listRegisteredRepos: vi.fn(),
|
|
runAutoSyncAnalysis: vi.fn(),
|
|
checkStalenessAsync: vi.fn(),
|
|
embeddingsFromGitnexusRc: vi.fn(),
|
|
countScanCandidates: vi.fn(),
|
|
}));
|
|
|
|
vi.mock('../../src/storage/repo-manager.js', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('../../src/storage/repo-manager.js')>();
|
|
return { ...actual, listRegisteredRepos: mocks.listRegisteredRepos };
|
|
});
|
|
vi.mock('../../src/core/auto-sync/analysis-worker-launch.js', async (importOriginal) => {
|
|
const actual =
|
|
await importOriginal<typeof import('../../src/core/auto-sync/analysis-worker-launch.js')>();
|
|
return { ...actual, runAutoSyncAnalysis: mocks.runAutoSyncAnalysis };
|
|
});
|
|
vi.mock('../../src/core/git-staleness.js', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('../../src/core/git-staleness.js')>();
|
|
return { ...actual, checkStalenessAsync: mocks.checkStalenessAsync };
|
|
});
|
|
vi.mock('../../src/core/ingestion/filesystem-walker.js', async (importOriginal) => {
|
|
const actual =
|
|
await importOriginal<typeof import('../../src/core/ingestion/filesystem-walker.js')>();
|
|
return { ...actual, countScanCandidates: mocks.countScanCandidates };
|
|
});
|
|
vi.mock('../../src/core/gitnexus-rc-embeddings.js', () => ({
|
|
embeddingsFromGitnexusRc: mocks.embeddingsFromGitnexusRc,
|
|
}));
|
|
|
|
import path from 'node:path';
|
|
import { LocalBackend } from '../../src/mcp/local/local-backend.js';
|
|
|
|
const REPO = path.resolve('/work/shop');
|
|
|
|
function entry(overrides: Record<string, unknown> = {}) {
|
|
return {
|
|
name: 'shop',
|
|
path: REPO,
|
|
storagePath: `${REPO}/.gitnexus`,
|
|
indexedAt: '2026-10-01T00:00:00.000Z',
|
|
lastCommit: 'old',
|
|
stats: { files: 40, nodes: 300, edges: 900 },
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
const BEHIND = { isStale: true, commitsBehind: 3, status: 'behind', hint: 'behind' };
|
|
const CURRENT = { isStale: false, commitsBehind: 0, status: 'current' };
|
|
|
|
describe('LocalBackend refresh_index (#3524)', () => {
|
|
let backend: LocalBackend;
|
|
|
|
beforeEach(async () => {
|
|
vi.clearAllMocks();
|
|
vi.unstubAllEnvs();
|
|
// The assertions use the defaults, so an inherited setting must not leak in.
|
|
vi.stubEnv('GITNEXUS_MCP_REFRESH_MAX_FILES', undefined);
|
|
vi.stubEnv('GITNEXUS_MCP_REFRESH_TIMEOUT_MS', undefined);
|
|
mocks.listRegisteredRepos.mockResolvedValue([entry()]);
|
|
mocks.embeddingsFromGitnexusRc.mockResolvedValue({});
|
|
// The working tree has one file more than the last index recorded.
|
|
mocks.countScanCandidates.mockResolvedValue(41);
|
|
mocks.checkStalenessAsync.mockImplementation(async (_path: string, commit: string) =>
|
|
commit === 'new' ? CURRENT : BEHIND,
|
|
);
|
|
mocks.runAutoSyncAnalysis.mockImplementation(async () => {
|
|
mocks.listRegisteredRepos.mockResolvedValue([
|
|
entry({
|
|
lastCommit: 'new',
|
|
indexedAt: '2026-10-29T00:00:00.000Z',
|
|
stats: { files: 41, nodes: 310, edges: 930 },
|
|
}),
|
|
]);
|
|
return { stats: { files: 41 } };
|
|
});
|
|
backend = new LocalBackend();
|
|
await backend.init();
|
|
});
|
|
|
|
it('describes a dry run without starting the worker', async () => {
|
|
const result = await backend.callTool('refresh_index', { repo: 'shop', dry_run: true });
|
|
expect(result).toEqual({
|
|
status: 'dry_run',
|
|
repo: 'shop',
|
|
indexedAt: '2026-10-01T00:00:00.000Z',
|
|
lastCommit: 'old',
|
|
staleness: expect.objectContaining({ status: 'behind', commitsBehind: 3 }),
|
|
estimatedFiles: 41,
|
|
lastIndexedFiles: 40,
|
|
maxFiles: 5_000,
|
|
embeddings: false,
|
|
wouldRefresh: true,
|
|
});
|
|
expect(mocks.runAutoSyncAnalysis).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('reports and forwards embeddings that .gitnexusrc asks for', async () => {
|
|
mocks.embeddingsFromGitnexusRc.mockResolvedValue({ embeddings: true, embeddingsNodeLimit: 9 });
|
|
const dryRun = await backend.callTool('refresh_index', { repo: 'shop', dry_run: true });
|
|
expect(dryRun).toMatchObject({ embeddings: true, wouldRefresh: true });
|
|
|
|
await backend.callTool('refresh_index', { repo: 'shop' });
|
|
expect(mocks.runAutoSyncAnalysis.mock.calls[0][1]).toMatchObject({
|
|
embeddings: true,
|
|
embeddingsNodeLimit: 9,
|
|
});
|
|
});
|
|
|
|
it('refuses to start when .gitnexusrc cannot be read', async () => {
|
|
mocks.embeddingsFromGitnexusRc.mockRejectedValue(new Error('.gitnexusrc: invalid JSON'));
|
|
for (const dry_run of [true, false]) {
|
|
await expect(backend.callTool('refresh_index', { repo: 'shop', dry_run })).resolves.toEqual({
|
|
error: 'Cannot refresh shop: .gitnexusrc: invalid JSON.',
|
|
});
|
|
}
|
|
expect(mocks.runAutoSyncAnalysis).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('rebuilds index-only through the worker and reports before and after', async () => {
|
|
const onProgress = vi.fn();
|
|
const result = await backend.callTool('refresh_index', { repo: 'shop' }, { onProgress });
|
|
|
|
expect(mocks.runAutoSyncAnalysis).toHaveBeenCalledTimes(1);
|
|
const [repoPath, options, timeoutMs, runSignal, , , progress] =
|
|
mocks.runAutoSyncAnalysis.mock.calls[0];
|
|
expect(repoPath).toBe(REPO);
|
|
expect(options).toEqual({
|
|
skipAgentsMd: true,
|
|
skipSkills: true,
|
|
preserveExistingPdg: true,
|
|
atomicIncremental: true,
|
|
});
|
|
expect(timeoutMs).toBe(600_000);
|
|
// The worker never interrupts a started analysis, so no abort signal is passed.
|
|
expect(runSignal).toBeUndefined();
|
|
expect(progress).toBe(onProgress);
|
|
|
|
expect(result).toEqual({
|
|
status: 'refreshed',
|
|
repo: 'shop',
|
|
previous: {
|
|
indexedAt: '2026-10-01T00:00:00.000Z',
|
|
lastCommit: 'old',
|
|
staleness: expect.objectContaining({ status: 'behind' }),
|
|
},
|
|
indexedAt: '2026-10-29T00:00:00.000Z',
|
|
lastCommit: 'new',
|
|
staleness: { status: 'current' },
|
|
stats: { files: 41, nodes: 310, edges: 930 },
|
|
durationMs: expect.any(Number),
|
|
});
|
|
});
|
|
|
|
it('counts the working tree against the limit, not the last index', async () => {
|
|
// The last index recorded 40 files; the tree has since grown past the limit.
|
|
mocks.countScanCandidates.mockResolvedValue(5_001);
|
|
const dryRun = await backend.callTool('refresh_index', { repo: 'shop', dry_run: true });
|
|
expect(dryRun).toMatchObject({
|
|
estimatedFiles: null,
|
|
lastIndexedFiles: 40,
|
|
maxFiles: 5_000,
|
|
wouldRefresh: false,
|
|
refusal:
|
|
'shop has more than 5000 files in its working tree (40 at its last index), over ' +
|
|
"this server's refresh limit of 5000 (GITNEXUS_MCP_REFRESH_MAX_FILES). " +
|
|
'Run `gitnexus analyze` in the repository instead.',
|
|
});
|
|
await expect(backend.callTool('refresh_index', { repo: 'shop' })).resolves.toEqual({
|
|
error: dryRun.refusal,
|
|
});
|
|
expect(mocks.runAutoSyncAnalysis).not.toHaveBeenCalled();
|
|
// Enumeration stops one past the limit.
|
|
expect(mocks.countScanCandidates).toHaveBeenCalledWith(REPO, 5_000);
|
|
});
|
|
|
|
it('admits an index with no recorded count, and skips the count with no limit', async () => {
|
|
mocks.listRegisteredRepos.mockResolvedValue([entry({ stats: undefined })]);
|
|
await backend.init();
|
|
await expect(
|
|
backend.callTool('refresh_index', { repo: REPO, dry_run: true }),
|
|
).resolves.toMatchObject({ estimatedFiles: 41, lastIndexedFiles: null, wouldRefresh: true });
|
|
|
|
vi.stubEnv('GITNEXUS_MCP_REFRESH_MAX_FILES', '0');
|
|
mocks.countScanCandidates.mockClear();
|
|
await expect(
|
|
backend.callTool('refresh_index', { repo: REPO, dry_run: true }),
|
|
).resolves.toMatchObject({ estimatedFiles: null, maxFiles: 0, wouldRefresh: true });
|
|
await expect(backend.callTool('refresh_index', { repo: REPO })).resolves.toMatchObject({
|
|
status: 'refreshed',
|
|
});
|
|
expect(mocks.countScanCandidates).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('refuses to start when the working tree cannot be read', async () => {
|
|
mocks.countScanCandidates.mockRejectedValue(
|
|
new Error(`walkRepositoryPaths: path does not exist: ${REPO}`),
|
|
);
|
|
for (const dry_run of [true, false]) {
|
|
await expect(backend.callTool('refresh_index', { repo: 'shop', dry_run })).resolves.toEqual({
|
|
error: `Cannot refresh shop: walkRepositoryPaths: path does not exist: ${REPO}.`,
|
|
});
|
|
}
|
|
expect(mocks.runAutoSyncAnalysis).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('runs one refresh per repository at a time', async () => {
|
|
let finish!: () => void;
|
|
mocks.runAutoSyncAnalysis.mockImplementationOnce(
|
|
() => new Promise((resolve) => (finish = () => resolve({ stats: {} }))),
|
|
);
|
|
const first = backend.callTool('refresh_index', { repo: 'shop' });
|
|
await vi.waitFor(() => expect(mocks.runAutoSyncAnalysis).toHaveBeenCalled());
|
|
|
|
const second = await backend.callTool('refresh_index', { repo: 'shop' });
|
|
expect(second.error).toMatch(/already running/);
|
|
await expect(
|
|
backend.callTool('refresh_index', { repo: 'shop', dry_run: true }),
|
|
).resolves.toMatchObject({ wouldRefresh: false, inProgress: true });
|
|
|
|
finish();
|
|
await expect(first).resolves.toMatchObject({ status: 'refreshed' });
|
|
// The slot is released, including after a failure.
|
|
mocks.runAutoSyncAnalysis.mockRejectedValueOnce(new Error('Analysis timed out after 5ms.'));
|
|
const failed = await backend.callTool('refresh_index', { repo: 'shop' });
|
|
expect(failed.error).toMatch(/Refresh of shop failed: Analysis timed out after 5ms\./);
|
|
await expect(backend.callTool('refresh_index', { repo: 'shop' })).resolves.toMatchObject({
|
|
status: 'refreshed',
|
|
});
|
|
});
|
|
|
|
it('reports a collapsed graph write as a failure, like analyze and serve', async () => {
|
|
mocks.runAutoSyncAnalysis.mockImplementationOnce(async () => {
|
|
mocks.listRegisteredRepos.mockResolvedValue([entry({ lastCommit: 'new' })]);
|
|
return { stats: { files: 41 }, graphWriteCollapsed: { expected: 930, persisted: 12 } };
|
|
});
|
|
const result = await backend.callTool('refresh_index', { repo: 'shop' });
|
|
expect(result).toEqual({
|
|
error: expect.stringMatching(
|
|
/^shop was indexed INCOMPLETELY: only 12 of 930 expected relationships are readable.*`gitnexus analyze --force`/,
|
|
),
|
|
});
|
|
// The registry is still re-read, so later tools see what is on disk.
|
|
const list = await backend.callTool('list_repos', {});
|
|
expect(list.repositories[0].lastCommit).toBe('new');
|
|
});
|
|
|
|
it('keeps a timed-out refresh busy until its abandoned worker exits', async () => {
|
|
let finishWorker!: () => void;
|
|
const workerExited = new Promise<void>((resolve) => {
|
|
finishWorker = resolve;
|
|
});
|
|
mocks.runAutoSyncAnalysis.mockRejectedValueOnce(
|
|
Object.assign(new Error('Analysis timed out after 5ms. The analyze worker did not exit'), {
|
|
abandonedWorker: true,
|
|
workerExited,
|
|
}),
|
|
);
|
|
const result = await backend.callTool('refresh_index', { repo: 'shop' });
|
|
expect(result.error).toContain('Its worker keeps running until the analysis finishes');
|
|
await expect(backend.callTool('refresh_index', { repo: 'shop' })).resolves.toMatchObject({
|
|
error: expect.stringContaining('already running'),
|
|
});
|
|
await expect(
|
|
backend.callTool('refresh_index', { repo: 'shop', dry_run: true }),
|
|
).resolves.toMatchObject({ wouldRefresh: false, inProgress: true });
|
|
expect(mocks.runAutoSyncAnalysis).toHaveBeenCalledTimes(1);
|
|
|
|
finishWorker();
|
|
await workerExited;
|
|
await expect(backend.callTool('refresh_index', { repo: 'shop' })).resolves.toMatchObject({
|
|
status: 'refreshed',
|
|
});
|
|
expect(mocks.runAutoSyncAnalysis).toHaveBeenCalledTimes(2);
|
|
expect(result.error).toBe(
|
|
'Refresh of shop failed: Analysis timed out after 5ms. The analyze worker did not exit. ' +
|
|
'Its worker keeps running until the analysis finishes; another refresh is refused until it exits. ' +
|
|
'Run `gitnexus analyze` in the repository for the full log.',
|
|
);
|
|
});
|
|
|
|
it('reports unknown staleness when the check itself fails', async () => {
|
|
mocks.checkStalenessAsync.mockRejectedValue(new Error('git missing'));
|
|
const result = await backend.callTool('refresh_index', { repo: 'shop', dry_run: true });
|
|
expect(result.staleness).toEqual({ status: 'unknown' });
|
|
});
|
|
|
|
it('needs an explicit repo once several are indexed, dry runs included', async () => {
|
|
mocks.listRegisteredRepos.mockResolvedValue([
|
|
entry(),
|
|
entry({
|
|
name: 'blog',
|
|
path: path.resolve('/work/blog'),
|
|
storagePath: `${path.resolve('/work/blog')}/.gitnexus`,
|
|
}),
|
|
]);
|
|
vi.spyOn(process, 'cwd').mockReturnValue(REPO);
|
|
// The cwd would pick `shop` for a read tool; refresh_index does not use it.
|
|
await expect(
|
|
backend.selectToolRepository(undefined, undefined, { allowCwdDefault: true }),
|
|
).resolves.toMatchObject({ name: 'shop' });
|
|
for (const dry_run of [true, false]) {
|
|
await expect(backend.callTool('refresh_index', { dry_run })).rejects.toThrow(
|
|
/multiple repositories/i,
|
|
);
|
|
}
|
|
expect(mocks.runAutoSyncAnalysis).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('rejects a non-boolean dry_run', async () => {
|
|
const result = await backend.callTool('refresh_index', { repo: 'shop', dry_run: 'true' });
|
|
expect(result).toEqual({ error: 'Parameter "dry_run" must be a boolean.' });
|
|
expect(mocks.runAutoSyncAnalysis).not.toHaveBeenCalled();
|
|
});
|
|
});
|