Skip to content

Commit 6b14216

Browse files
committed
fix(llm): reject inherited tool names
1 parent 5a4baca commit 6b14216

2 files changed

Lines changed: 99 additions & 2 deletions

File tree

‎packages/opencode/src/session/llm.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,10 @@ export namespace LLM {
266266

267267
// altimate_change start — finalization must have no executable catalog, even for unsolicited tool calls
268268
const runBudget = await sessionRunBudget(input.sessionID)
269-
const tools: Record<string, Tool> = runBudget?.toolsDisabled ? {} : await resolveTools(input)
269+
const resolvedTools: Record<string, Tool> = runBudget?.toolsDisabled ? {} : await resolveTools(input)
270+
// The AI SDK checks tool names by property lookup. A null-prototype catalog prevents
271+
// inherited Object properties (for example, "constructor") from masquerading as tools.
272+
const tools: Record<string, Tool> = Object.assign(Object.create(null), resolvedTools)
270273
const toolChoice = runBudget?.toolsDisabled ? "none" : input.toolChoice
271274
// altimate_change end
272275

@@ -386,7 +389,7 @@ export namespace LLM {
386389
// altimate_change end
387390
async experimental_repairToolCall(failed) {
388391
const lower = failed.toolCall.toolName.toLowerCase()
389-
if (lower !== failed.toolCall.toolName && tools[lower]) {
392+
if (lower !== failed.toolCall.toolName && Object.prototype.hasOwnProperty.call(tools, lower)) {
390393
l.info("repairing tool call", {
391394
tool: failed.toolCall.toolName,
392395
repaired: lower,

‎packages/opencode/test/session/llm.test.ts‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,100 @@ describe("session.llm.stream", () => {
398398
})
399399
})
400400

401+
test("inherited object property names report through the hidden fallback", async () => {
402+
const providerID = "alibaba"
403+
const { model } = await loadFixture(providerID, "qwen-plus")
404+
const request = waitRequest(
405+
"/chat/completions",
406+
createEventResponse(
407+
[
408+
{
409+
id: "chatcmpl-inherited",
410+
object: "chat.completion.chunk",
411+
choices: [
412+
{
413+
delta: {
414+
role: "assistant",
415+
tool_calls: [
416+
{
417+
index: 0,
418+
id: "call-inherited",
419+
type: "function",
420+
function: { name: "constructor", arguments: "{}" },
421+
},
422+
],
423+
},
424+
},
425+
],
426+
},
427+
{
428+
id: "chatcmpl-inherited",
429+
object: "chat.completion.chunk",
430+
choices: [{ delta: {}, finish_reason: "tool_calls" }],
431+
},
432+
],
433+
true,
434+
),
435+
)
436+
await using tmp = await tmpdir({
437+
config: {
438+
enabled_providers: [providerID],
439+
provider: { [providerID]: { options: { apiKey: "test-key", baseURL: `${state.server!.url.origin}/v1` } } },
440+
},
441+
})
442+
await Instance.provide({
443+
directory: tmp.path,
444+
fn: async () => {
445+
const resolved = await Provider.getModel(ProviderID.make(providerID), ModelID.make(model.id))
446+
const sessionID = SessionID.make("session-inherited-tool")
447+
const agent = {
448+
name: "custom-builder",
449+
mode: "primary",
450+
options: {},
451+
permission: PermissionNext.fromConfig({ "*": "allow" }),
452+
} satisfies Agent.Info
453+
const stream = await LLM.stream({
454+
user: {
455+
id: MessageID.make("msg_inherited-tool"),
456+
sessionID,
457+
role: "user",
458+
time: { created: Date.now() },
459+
agent: agent.name,
460+
model: { providerID: ProviderID.make(providerID), modelID: resolved.id },
461+
},
462+
sessionID,
463+
model: resolved,
464+
agent,
465+
system: [],
466+
abort: new AbortController().signal,
467+
messages: [{ role: "user", content: "Call the requested tool" }],
468+
tools: {
469+
read: tool({ inputSchema: jsonSchema({ type: "object", properties: {} }), execute: async () => "read" }),
470+
invalid: tool({
471+
inputSchema: jsonSchema<{ tool: string; error: string }>({
472+
type: "object",
473+
properties: { tool: { type: "string" }, error: { type: "string" } },
474+
required: ["tool", "error"],
475+
}),
476+
execute: async (input) => input,
477+
}),
478+
},
479+
})
480+
const events = []
481+
for await (const event of stream.fullStream) events.push(event)
482+
expect(events.find((event) => event.type === "tool-result")).toMatchObject({
483+
toolName: "invalid",
484+
output: { tool: "constructor", error: expect.stringContaining("constructor") },
485+
})
486+
expect(events.some((event) => event.type === "tool-error" || event.type === "error")).toBe(false)
487+
const capture = await request
488+
expect((capture.body.tools as { function: { name: string } }[]).map((item) => item.function.name)).toEqual([
489+
"read",
490+
])
491+
},
492+
})
493+
})
494+
401495
test.skip("sends temperature, tokens, and reasoning options for openai-compatible models", async () => {
402496
const server = state.server
403497
if (!server) {

0 commit comments

Comments
 (0)