GitNexus/gitnexus/test/unit/mcp-refresh-index.test.ts
samuel mbabhazi 02101fdfed
feat(mcp): add an opt-in, index-only refresh_index tool (#3769)
* 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>
2026-10-31 13:57:02 +00:00

380 lines
14 KiB
TypeScript

import type http from 'node:http';
import net, { type AddressInfo } from 'node:net';
import express from 'express';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js';
import { createMCPServer } from '../../src/mcp/server.js';
import { startMcpHttpServer } from '../../src/mcp/http-transport.js';
import type { LocalBackend } from '../../src/mcp/local/local-backend.js';
import { createMcpRepositoryPolicy } from '../../src/mcp/repository-policy.js';
import { mountMCPEndpoints } from '../../src/server/mcp-http.js';
import {
MCP_REFRESH_DEFAULT_MAX_FILES,
MCP_REFRESH_DEFAULT_TIMEOUT_MS,
resolveMcpRefreshLimits,
resolveMcpRefreshMode,
} from '../../src/mcp/refresh-policy.js';
const REPOS = [
{ name: 'Alpha', path: '/repos/alpha', indexedAt: '2026-01-01', lastCommit: 'a'.repeat(40) },
{ name: 'Beta', path: '/repos/beta', indexedAt: '2026-01-02', lastCommit: 'b'.repeat(40) },
];
function createMockBackend() {
return {
callTool: vi.fn().mockResolvedValue({ status: 'refreshed' }),
listRepos: vi.fn().mockResolvedValue(REPOS.map((repo) => ({ ...repo }))),
countRepos: vi.fn().mockResolvedValue(REPOS.length),
cachedRepoCount: vi.fn().mockReturnValue(REPOS.length),
resolveRepo: vi
.fn()
.mockResolvedValue({ name: 'test', repoPath: '/tmp/test', lastCommit: 'abc' }),
getContext: vi.fn().mockReturnValue(null),
disconnect: vi.fn().mockResolvedValue(undefined),
};
}
async function connect(
options: Parameters<typeof createMCPServer>[1] = {},
backend = createMockBackend(),
) {
const server = createMCPServer(backend as unknown as LocalBackend, options);
const client = new Client({ name: 'refresh-test-client', version: '0.0.0' });
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
await Promise.all([server.connect(serverTransport), client.connect(clientTransport)]);
return {
backend,
client,
serverTransport,
close: async () => {
await client.close();
await server.close();
},
};
}
afterEach(() => {
vi.unstubAllEnvs();
});
describe('refresh_index MCP gate (#3524)', () => {
it('is absent and rejected by default, even when the environment asks for it', async () => {
// `gitnexus serve` creates its MCP servers without the option, so its
// environment alone must not be able to expose the tool.
vi.stubEnv('GITNEXUS_MCP_ENABLE_REFRESH', '1');
const session = await connect();
try {
const { tools } = await session.client.listTools();
expect(tools.map((tool) => tool.name)).not.toContain('refresh_index');
const response = await session.client.callTool({
name: 'refresh_index',
arguments: { repo: 'test' },
});
expect(response.isError).toBe(true);
expect(JSON.stringify(response.content)).toContain('GITNEXUS_MCP_ENABLE_REFRESH=1');
expect(session.backend.callTool).not.toHaveBeenCalled();
} finally {
await session.close();
}
});
it('is listed as a non-destructive write when the local server enables it', async () => {
const session = await connect({ refreshIndex: true });
try {
const { tools } = await session.client.listTools();
const refresh = tools.find((tool) => tool.name === 'refresh_index');
expect(refresh?.annotations).toEqual({
readOnlyHint: false,
destructiveHint: false,
idempotentHint: true,
openWorldHint: false,
});
expect(Object.keys(refresh?.inputSchema.properties ?? {}).sort()).toEqual([
'dry_run',
'repo',
]);
session.backend.callTool.mockResolvedValueOnce({ status: 'dry_run', wouldRefresh: true });
const response = await session.client.callTool({
name: 'refresh_index',
arguments: { repo: 'test', dry_run: true },
});
expect(response.isError).not.toBe(true);
// A dry run changes nothing, so it gets no next step.
expect(JSON.stringify(response.content)).not.toContain('**Next:**');
// No progress token, so nothing beyond the arguments reaches the backend.
expect(session.backend.callTool).toHaveBeenCalledWith('refresh_index', {
repo: 'test',
dry_run: true,
});
} finally {
await session.close();
}
});
it('rejects a branch pin before dispatch', async () => {
const session = await connect({ refreshIndex: true });
try {
const response = await session.client.callTool({
name: 'refresh_index',
arguments: { repo: 'test', branch: 'main' },
});
expect(response.isError).toBe(true);
expect(session.backend.callTool).not.toHaveBeenCalled();
} finally {
await session.close();
}
});
it('stays hidden and rejected in read-only mode even when enabled', async () => {
vi.stubEnv('GITNEXUS_MCP_READ_ONLY', '1');
const session = await connect({ refreshIndex: true });
try {
const { tools } = await session.client.listTools();
expect(tools.map((tool) => tool.name)).not.toContain('refresh_index');
const response = await session.client.callTool({
name: 'refresh_index',
arguments: { repo: 'test', dry_run: true },
});
expect(response.isError).toBe(true);
expect(JSON.stringify(response.content)).toContain('read-only mode');
expect(session.backend.callTool).not.toHaveBeenCalled();
} finally {
await session.close();
}
});
it('forwards strictly increasing progress and clamps it to the advertised total', async () => {
const backend = createMockBackend();
backend.callTool.mockImplementation(
async (
_method: string,
_args: unknown,
context?: { onProgress?: (progress: number, message: string) => Promise<void> },
) => {
await context?.onProgress?.(0, 'starting');
await context?.onProgress?.(10, 'parsing');
await context?.onProgress?.(10, 'repeated');
await context?.onProgress?.(5, 'decreasing');
await context?.onProgress?.(90, 'communities');
await context?.onProgress?.(105, 'complete');
await context?.onProgress?.(100, 'repeated completion');
await context?.onProgress?.(150, 'above total again');
return { status: 'refreshed' };
},
);
const session = await connect({ refreshIndex: true }, backend);
try {
const updates: Array<{ progress: number; total?: number; message?: string }> = [];
const response = await session.client.callTool(
{ name: 'refresh_index', arguments: { repo: 'test' } },
undefined,
{ onprogress: (update) => updates.push(update), resetTimeoutOnProgress: true },
);
expect(response.isError).not.toBe(true);
expect(JSON.stringify(response.content)).toContain('it now reads the refreshed one');
expect(updates).toEqual([
{ progress: 0, total: 100, message: 'starting' },
{ progress: 10, total: 100, message: 'parsing' },
{ progress: 90, total: 100, message: 'communities' },
{ progress: 100, total: 100, message: 'complete' },
]);
} finally {
await session.close();
}
});
it('finishes the refresh and sends later progress when a notification send rejects', async () => {
const backend = createMockBackend();
backend.callTool.mockImplementation(
async (
_method: string,
_args: unknown,
context?: { onProgress?: (progress: number, message: string) => Promise<void> },
) => {
await context?.onProgress?.(10, 'parsing');
await context?.onProgress?.(90, 'communities');
return { status: 'refreshed' };
},
);
const session = await connect({ refreshIndex: true }, backend);
const send = session.serverTransport.send.bind(session.serverTransport);
const rejected = vi.fn();
const unhandled = vi.fn();
process.on('unhandledRejection', unhandled);
const spy = vi
.spyOn(session.serverTransport, 'send')
.mockImplementation(async (message, options) => {
if (
'method' in message &&
message.method === 'notifications/progress' &&
message.params?.progress === 10
) {
rejected();
throw new Error('notification transport unavailable');
}
await send(message, options);
});
try {
const updates: Array<{ progress: number; total?: number; message?: string }> = [];
const response = await session.client.callTool(
{ name: 'refresh_index', arguments: { repo: 'test' } },
undefined,
{ onprogress: (update) => updates.push(update) },
);
expect(rejected).toHaveBeenCalledOnce();
expect(response.isError).not.toBe(true);
expect(JSON.stringify(response.content)).toContain('refreshed');
expect(updates).toEqual([{ progress: 90, total: 100, message: 'communities' }]);
// The suite tolerates native teardown errors globally, so assert this
// rejection explicitly after Node has had a turn to report it.
await new Promise<void>((resolve) => setImmediate(resolve));
expect(unhandled).not.toHaveBeenCalled();
} finally {
process.off('unhandledRejection', unhandled);
spy.mockRestore();
await session.close();
}
});
});
describe('refresh_index repository policy (#3524)', () => {
it('keeps a refresh inside the MCP repository allowlist', async () => {
const backend = createMockBackend();
const repositoryPolicy = await createMcpRepositoryPolicy(backend as unknown as LocalBackend, {
GITNEXUS_MCP_ALLOWED_REPOS: 'Alpha',
});
const session = await connect({ refreshIndex: true, repositoryPolicy }, backend);
try {
const denied = await session.client.callTool({
name: 'refresh_index',
arguments: { repo: '/repos/beta' },
});
expect(denied.isError).toBe(true);
expect(backend.callTool).not.toHaveBeenCalled();
await session.client.callTool({ name: 'refresh_index', arguments: {} });
expect(backend.callTool).toHaveBeenCalledWith('refresh_index', { repo: '/repos/alpha' });
} finally {
await session.close();
}
});
});
describe('refresh_index over HTTP (#3524)', () => {
const servers: http.Server[] = [];
afterEach(async () => {
for (const server of servers.splice(0)) {
await new Promise<void>((resolve) => server.close(() => resolve()));
}
});
// The DNS-rebinding guard allows the configured port, so port 0 cannot be used here.
async function freePort(): Promise<number> {
const probe = net.createServer();
await new Promise<void>((resolve) => probe.listen(0, '127.0.0.1', resolve));
const { port } = probe.address() as AddressInfo;
await new Promise<void>((resolve) => probe.close(() => resolve()));
return port;
}
async function toolNames(transport: StreamableHTTPClientTransport | SSEClientTransport) {
const client = new Client({ name: 'refresh-http-client', version: '0.0.0' });
await client.connect(transport);
try {
return (await client.listTools()).tools.map((tool) => tool.name);
} finally {
await client.close();
}
}
it.each([
[true, true],
[undefined, false],
])('gitnexus mcp --http with refreshIndex=%s lists it: %s', async (refreshIndex, listed) => {
const port = await freePort();
const server = await startMcpHttpServer(createMockBackend() as unknown as LocalBackend, {
port,
host: '127.0.0.1',
refreshIndex,
});
servers.push(server);
const base = `http://127.0.0.1:${port}`;
for (const transport of [
new StreamableHTTPClientTransport(new URL(`${base}/mcp`)),
new SSEClientTransport(new URL(`${base}/sse`)),
]) {
expect((await toolNames(transport)).includes('refresh_index')).toBe(listed);
}
});
it('is never listed on the serve /api/mcp surface, even with the variable set', async () => {
vi.stubEnv('GITNEXUS_MCP_ENABLE_REFRESH', '1');
const app = express();
app.use(express.json());
const cleanup = await mountMCPEndpoints(app, createMockBackend() as unknown as LocalBackend);
const server = await new Promise<http.Server>((resolve) => {
const listening = app.listen(0, '127.0.0.1', () => resolve(listening));
});
servers.push(server);
try {
const port = (server.address() as AddressInfo).port;
const names = await toolNames(
new StreamableHTTPClientTransport(new URL(`http://127.0.0.1:${port}/api/mcp`)),
);
expect(names).toContain('list_repos');
expect(names).not.toContain('refresh_index');
} finally {
await cleanup();
}
});
});
describe('refresh policy environment', () => {
it.each([
[undefined, false],
['', false],
['0', false],
['1', true],
])('reads GITNEXUS_MCP_ENABLE_REFRESH=%s as %s', (value, expected) => {
expect(resolveMcpRefreshMode({ GITNEXUS_MCP_ENABLE_REFRESH: value })).toBe(expected);
});
it.each(['true', 'yes', '2'])('fails startup for malformed enable value %s', (value) => {
expect(() => resolveMcpRefreshMode({ GITNEXUS_MCP_ENABLE_REFRESH: value })).toThrow(
/GITNEXUS_MCP_ENABLE_REFRESH must be 0 or 1/,
);
});
it('defaults the size and time limits, and accepts 0 files as no limit', () => {
expect(resolveMcpRefreshLimits({})).toEqual({
maxFiles: MCP_REFRESH_DEFAULT_MAX_FILES,
timeoutMs: MCP_REFRESH_DEFAULT_TIMEOUT_MS,
});
expect(
resolveMcpRefreshLimits({
GITNEXUS_MCP_REFRESH_MAX_FILES: '0',
GITNEXUS_MCP_REFRESH_TIMEOUT_MS: '30000',
}),
).toEqual({ maxFiles: 0, timeoutMs: 30_000 });
expect(
resolveMcpRefreshLimits({ GITNEXUS_MCP_REFRESH_TIMEOUT_MS: '2147483647' }).timeoutMs,
).toBe(2_147_483_647);
});
it.each([
['GITNEXUS_MCP_REFRESH_MAX_FILES', '-1'],
['GITNEXUS_MCP_REFRESH_MAX_FILES', '1e3'],
['GITNEXUS_MCP_REFRESH_TIMEOUT_MS', '0'],
['GITNEXUS_MCP_REFRESH_TIMEOUT_MS', 'ten'],
// Node would fire a longer timer after 1 ms and cancel every refresh.
['GITNEXUS_MCP_REFRESH_TIMEOUT_MS', '2147483648'],
])('rejects %s=%s', (name, value) => {
expect(() => resolveMcpRefreshLimits({ [name]: value })).toThrow(new RegExp(name));
});
});