Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions bin/codedb-bridge.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,45 @@ export const CODEDB_TOOLS = [
),
];

// Keys the bridge rejects with a dedicated message when set (handled
// rejection keys, #3352): `project` on every tool, plus the daemon-less
// modes per tool. These stay accepted by the allowlist below so their
// existing by-name rejection messages are unchanged; anything else
// outside the tool's schema is refused before it can be silently dropped
// (#4216).
const HANDLED_REJECTION_KEYS = {
codedb_tree: ['project'],
codedb_outline: ['project', 'compact'],
codedb_symbol: ['project'],
codedb_search: ['project', 'regex', 'scope', 'compact'],
codedb_word: ['project'],
codedb_hot: ['project'],
codedb_deps: ['project'],
};

// The accepted argument names for a tool: its advertised schema
// properties plus its handled rejection keys. Returns null for unknown
// tools (the switch in routeForTool still owns that error).
const acceptedArgsFor = (name) => {
const declared = CODEDB_TOOLS.find((t) => t.name === name);
if (!declared) return null;
return new Set([...Object.keys(declared.inputSchema.properties), ...(HANDLED_REJECTION_KEYS[name] ?? [])]);
};

// Map a tool call to the daemon route. Throws an Error naming the exact
// problem for unknown tools, missing required arguments, and arguments
// the daemon cannot honour — nothing is silently dropped (#3352).
export const routeForTool = (name, args = {}) => {
const accepted = acceptedArgsFor(name);
if (accepted !== null) {
for (const key of Object.keys(args)) {
if (!accepted.has(key)) {
throw new Error(
`argument "${key}": not a valid argument for ${name} (valid: ${[...accepted].sort().join(', ') || '(none)'}); it would be silently ignored by the codedb daemon route`
);
}
}
}
const rejectProject = () => {
if (args.project !== undefined) throw new Error(`argument "project": ${PROJECT_ARG_NOTE}`);
};
Expand Down
39 changes: 39 additions & 0 deletions bin/codedb-bridge.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,45 @@ test('tools/call forwards to the daemon and returns its payload', async () => {
assert.deepEqual(JSON.parse(hot.result.content[0].text).files, ['a']);
});

test('arguments in no tool schema are rejected by name, never silently dropped (#4216)', () => {
const neverNamed = {
codedb_tree: { bogus_tree_opt: 1 },
codedb_outline: { path: 'x', bogus_outline_opt: 1 },
codedb_symbol: { name: 'x', bogus_symbol_opt: 1 },
codedb_search: { query: 'x', bogus_search_opt: 1 },
codedb_word: { word: 'x', bogus_word_opt: 1 },
codedb_hot: { bogus_hot_opt: 1 },
codedb_deps: { path: 'x', bogus_deps_opt: 1 },
};
for (const t of CODEDB_TOOLS) {
const key = Object.keys(neverNamed[t.name]).find((k) => k.startsWith('bogus_'));
assert.throws(() => routeForTool(t.name, neverNamed[t.name]), new RegExp(`"${key}"`));
}
// The typo case from the issue: max_result (no s) is not max_results.
assert.throws(() => routeForTool('codedb_search', { query: 'x', max_result: 2 }), /"max_result"/);
});

test('a typoed max_result surfaces as an MCP error naming the argument, not the full result set (#4216)', async () => {
const handle = createHandler({
fetchFn: stubFetch({
'/explore/search?q=x': {
query: 'x',
results: [
{ path: 'a.rs', line: 1, text: 'a' },
{ path: 'b.rs', line: 2, text: 'b' },
{ path: 'c.rs', line: 3, text: 'c' },
{ path: 'd.rs', line: 4, text: 'd' },
{ path: 'e.rs', line: 5, text: 'e' },
],
},
}),
daemonUrl: 'http://127.0.0.1:7719',
});
const typo = await call(handle, 'codedb_search', { query: 'x', max_result: 2 });
assert.equal(typo.result.isError, true);
assert.match(typo.result.content[0].text, /"max_result"/);
});

test('a slow daemon call does not head-of-line block a later ping (PR #4171 Codex P2)', async () => {
const daemon = http.createServer((req, res) => {
const respond = () => {
Expand Down
Loading