Address PR review feedback (#3339)

Memoize live tRPC mount paths so the depth-N chain bench stays linear,
and render invalid group/MCP bounds without throwing.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Gergo Magyar 2026-09-21 07:51:03 +00:00
parent abee35b918
commit 4447f9b520
4 changed files with 47 additions and 15 deletions

View file

@ -463,7 +463,7 @@ function parseGroupQueryBound(
if (typeof value !== 'number' || !Number.isInteger(value) || value < 1 || value > max) {
return {
ok: false,
error: `Invalid "${field}": expected an integer in [1, ${max}], got ${JSON.stringify(value)}.`,
error: `Invalid "${field}": expected an integer in [1, ${max}], got ${describeValue(value)}.`,
};
}
return { ok: true, value };
@ -481,7 +481,7 @@ function parseGroupChainDepth(
) {
return {
ok: false,
error: `Invalid "chain_depth": expected an integer in [0, ${GROUP_CHAIN_MAX_DEPTH}], got ${JSON.stringify(value)}.`,
error: `Invalid "chain_depth": expected an integer in [0, ${GROUP_CHAIN_MAX_DEPTH}], got ${describeValue(value)}.`,
};
}
return { ok: true, value };

View file

@ -496,9 +496,12 @@ function buildMountPathLookup(
if (out.length > 0) result = out;
}
visiting.delete(binding);
// Nested cycle cuts return [] for this hop only. Memoizing that empty
// walk would drop a later live path (`a.b.list` after `a` ↔ `b`).
if (visiting.size === 0) memo.set(binding, result);
// Memoize live paths immediately so a depth-N chain is one walk, not one
// remount per procedure. Leave nested empty results uncached: a cycle cut
// returns [] for this hop only, and memoizing that would drop a later live
// path (`a.b.list` after `a` ↔ `b`). A finished top-level [] is real
// (unmounted / no root path) and is safe to cache.
if (result.length > 0 || visiting.size === 0) memo.set(binding, result);
return result;
};
}

View file

@ -1467,6 +1467,14 @@ export function parseListReposPagination(
* inputSchema — so the backend rejects out-of-range values the same way
* parseListReposPagination / pdg_query do (reject, not clamp).
*/
function describeBoundValue(raw: unknown): string {
try {
return JSON.stringify(raw) ?? String(raw);
} catch {
return typeof raw === 'bigint' ? `${raw}n` : Object.prototype.toString.call(raw);
}
}
function parseQueryPageBound(
value: unknown,
field: 'limit' | 'max_symbols',
@ -1477,7 +1485,7 @@ function parseQueryPageBound(
if (typeof value !== 'number' || !Number.isInteger(value) || value < 1 || value > max) {
return {
ok: false,
error: `Invalid "${field}": expected an integer in [1, ${max}], got ${JSON.stringify(value)}.`,
error: `Invalid "${field}": expected an integer in [1, ${max}], got ${describeBoundValue(value)}.`,
};
}
return { ok: true, value };
@ -1495,7 +1503,7 @@ function parseChainDepth(
) {
return {
ok: false,
error: `Invalid "chain_depth": expected an integer in [0, ${CONTEXT_CHAIN_MAX_DEPTH}], got ${JSON.stringify(value)}.`,
error: `Invalid "chain_depth": expected an integer in [0, ${CONTEXT_CHAIN_MAX_DEPTH}], got ${describeBoundValue(value)}.`,
};
}
return { ok: true, value };

View file

@ -341,25 +341,46 @@ describe('GroupService', () => {
const query = vi.fn(async () => ({ processes: [] }));
const svc = new GroupService(makePort({ query }));
const overLimit = await svc.groupQuery({
const infiniteLimit = await svc.groupQuery({
name: 'test-group',
query: 'auth flow',
limit: Infinity,
max_symbols: 9999,
});
expect(overLimit).toMatchObject({ error: expect.stringMatching(/Invalid "limit"/) });
expect(query).not.toHaveBeenCalled();
expect(infiniteLimit).toMatchObject({ error: expect.stringMatching(/Invalid "limit"/) });
const overSymbols = await svc.groupQuery({
const oversizedLimit = await svc.groupQuery({
name: 'test-group',
query: 'auth flow',
limit: 101,
});
expect(oversizedLimit).toMatchObject({ error: expect.stringMatching(/Invalid "limit"/) });
const infiniteSymbols = await svc.groupQuery({
name: 'test-group',
query: 'auth flow',
limit: 3,
max_symbols: Infinity,
});
expect(overSymbols).toMatchObject({
expect(infiniteSymbols).toMatchObject({
error: expect.stringMatching(/Invalid "max_symbols"/),
});
expect(query).not.toHaveBeenCalled();
const oversizedSymbols = await svc.groupQuery({
name: 'test-group',
query: 'auth flow',
max_symbols: 201,
});
expect(oversizedSymbols).toMatchObject({
error: expect.stringMatching(/Invalid "max_symbols"/),
});
const cyclic = { self: null as unknown };
cyclic.self = cyclic;
const cyclicLimit = await svc.groupQuery({
name: 'test-group',
query: 'auth flow',
limit: cyclic,
});
expect(cyclicLimit).toMatchObject({ error: expect.stringMatching(/Invalid "limit"/) });
const badDepth = await svc.groupQuery({
name: 'test-group',