opencode/packages/core/test/plugin/tool-input-repair-integration.test.ts

345 lines
12 KiB
TypeScript

import { expect } from "bun:test"
import { Effect, Schema } from "effect"
import { Agent } from "@opencode/core/agent"
import { Plugin } from "@opencode/core/plugin"
import { ToolInputRepairPlugin } from "@opencode/core/plugin/tool-input-repair"
import { Session } from "@opencode/core/session"
import { SessionMessage } from "@opencode/core/session/message"
import { Tool } from "@opencode/core/tool"
import { testEffect } from "../lib/effect"
import { PluginTestLayer } from "./fixture"
const it = testEffect(PluginTestLayer)
const identity = {
sessionID: Session.ID.make("ses_repair"),
agent: Agent.ID.make("build"),
messageID: SessionMessage.ID.make("msg_repair"),
}
it.effect("repairs tool input before validating its original schema", () =>
Effect.gen(function* () {
const plugins = yield* Plugin.Service
const registry = yield* Tool.Service
const executed: unknown[] = []
yield* plugins.activate([
{ ...ToolInputRepairPlugin.Plugin, revision: "1" },
{
id: "repairable-tool",
revision: "1",
effect: (ctx) =>
ctx.tool.transform((draft) =>
draft.add({
name: "repairable",
options: { codemode: false },
description: "Repairable",
input: Schema.Struct({ count: Schema.Int, enabled: Schema.Boolean }),
execute: (input) => Effect.sync(() => executed.push(input)).pipe(Effect.as({ content: "ok" })),
}),
),
},
])
const snapshot = yield* registry.snapshot()
yield* snapshot.execute({
...identity,
call: {
type: "tool-call",
id: "call-repair",
name: "repairable",
input: '{"count":"2","enabled":"true","extra":true}',
},
})
expect(executed).toEqual([{ count: 2, enabled: true }])
yield* registry.transform((draft) => {
draft.update("repairable", (tool) => {
tool.input = Schema.Struct({ count: Schema.Boolean, enabled: Schema.Boolean })
})
})
const updated = yield* registry.snapshot()
yield* updated.execute({
...identity,
call: {
type: "tool-call",
id: "call-updated",
name: "repairable",
input: { count: "false", enabled: "true" },
},
})
expect(executed).toEqual([
{ count: 2, enabled: true },
{ count: false, enabled: true },
])
yield* registry.transform((draft) => draft.remove("repairable"))
const removed = yield* registry.snapshot()
expect(
(yield* removed
.execute({
...identity,
call: { type: "tool-call", id: "call-removed", name: "repairable", input: {} },
})
.pipe(Effect.flip)).message,
).toBe('No tool named "repairable" is currently available. Please use a tool from the available tool list.')
}),
)
it.effect("runs a call to another harness's tool name as the matching Code Mode tool", () =>
Effect.gen(function* () {
const plugins = yield* Plugin.Service
const registry = yield* Tool.Service
const executed: unknown[] = []
yield* plugins.activate([
{ ...ToolInputRepairPlugin.Plugin, revision: "1" },
{
id: "shell-tool",
revision: "1",
effect: (ctx) =>
ctx.tool.transform((draft) =>
draft.add({
name: "shell",
description: "Shell",
input: Schema.Struct({ command: Schema.String, timeout: Schema.optionalKey(Schema.Int) }),
execute: (input) => Effect.sync(() => executed.push(input)).pipe(Effect.as({ content: "done" })),
}),
),
},
])
// shell is only offered inside Code Mode, so the renamed call runs as the program that calls it.
const snapshot = yield* registry.snapshot()
const result = yield* snapshot.execute({
...identity,
call: { type: "tool-call", id: "call-bash", name: "bash", input: { command: "ls", timeout: "5000" } },
})
expect(executed).toEqual([{ command: "ls", timeout: 5000 }])
expect(result.content[0]).toEqual({
type: "text",
text: '<tool-repair>\nThere is no "bash" tool, so this ran inside execute as tools.shell(input). Call it there next time.\n</tool-repair>',
})
}),
)
it.effect("runs a call to another harness's tool name as the matching tool and says so", () =>
Effect.gen(function* () {
const plugins = yield* Plugin.Service
const registry = yield* Tool.Service
const executed: unknown[] = []
yield* plugins.activate([
{ ...ToolInputRepairPlugin.Plugin, revision: "1" },
{
id: "shell-tool",
revision: "1",
effect: (ctx) =>
ctx.tool.transform((draft) =>
draft.add({
name: "shell",
options: { codemode: false },
description: "Shell",
input: Schema.Struct({ command: Schema.String, timeout: Schema.optionalKey(Schema.Int) }),
execute: (input) => Effect.sync(() => executed.push(input)).pipe(Effect.as({ content: "done" })),
}),
),
},
])
const snapshot = yield* registry.snapshot()
const result = yield* snapshot.execute({
...identity,
call: { type: "tool-call", id: "call-bash", name: "bash", input: { command: "ls", timeout: "5000" } },
})
expect(executed).toEqual([{ command: "ls", timeout: 5000 }])
expect(result.content).toEqual([
{
type: "text",
text: '<tool-repair>\nThere is no "bash" tool, so this ran as "shell". Call "shell" next time.\n</tool-repair>',
},
{ type: "text", text: "done" },
])
const direct = yield* snapshot.execute({
...identity,
call: { type: "tool-call", id: "call-shell", name: "shell", input: { command: "pwd" } },
})
expect(direct.content).toEqual([{ type: "text", text: "done" }])
// A call rejected before it runs leaves no note behind for a later call with the same ID.
yield* snapshot
.execute({
...identity,
call: { type: "tool-call", id: "call-reused", name: "bash", input: { command: "ls" } },
definitions: new Map(),
})
.pipe(Effect.flip)
const reused = yield* snapshot.execute({
...identity,
call: { type: "tool-call", id: "call-reused", name: "shell", input: { command: "pwd" } },
})
expect(reused.content).toEqual([{ type: "text", text: "done" }])
}),
)
it.effect("runs a top-level call to a Code Mode tool and says to call it inside execute", () =>
Effect.gen(function* () {
const plugins = yield* Plugin.Service
const registry = yield* Tool.Service
yield* plugins.activate([
{ ...ToolInputRepairPlugin.Plugin, revision: "1" },
{
id: "acme-tools",
revision: "1",
effect: (ctx) =>
ctx.tool.transform((draft) => {
draft.add({
name: "echo",
options: { namespace: "acme" },
description: "Echo",
input: Schema.Struct({ text: Schema.String }),
execute: ({ text }) => Effect.succeed({ content: text }),
})
draft.add({
name: "fails",
options: { namespace: "acme" },
description: "Fails",
input: Schema.Struct({}),
execute: () => Effect.fail(new Tool.Error({ message: "boom" })),
})
}),
},
])
const snapshot = yield* registry.snapshot()
const note = (path: string) =>
`<tool-repair>\nacme.${path} is a Code Mode tool, so this ran inside execute as tools.acme.${path}(input). Call it there next time.\n</tool-repair>`
const result = yield* snapshot.execute({
...identity,
call: { type: "tool-call", id: "call-echo", name: "acme.echo", input: { text: "hi" } },
})
expect(result.content[0]).toEqual({ type: "text", text: note("echo") })
// A failing program fails the call, with the note first.
const failed = yield* snapshot
.execute({ ...identity, call: { type: "tool-call", id: "call-fails", name: "acme.fails", input: {} } })
.pipe(Effect.flip)
expect(failed.message.startsWith(note("fails"))).toBe(true)
expect(failed.message).toContain("boom")
}),
)
it.effect("repairs namespaced inner tool input called from Code Mode", () =>
Effect.gen(function* () {
const plugins = yield* Plugin.Service
const registry = yield* Tool.Service
const executed: unknown[] = []
yield* plugins.activate([{ ...ToolInputRepairPlugin.Plugin, revision: "1" }])
yield* registry.transform((draft) =>
draft.add({
name: "count",
options: { namespace: "example" },
description: "Record a count",
input: Schema.Struct({ count: Schema.Int }),
execute: (input) => Effect.sync(() => executed.push(input)).pipe(Effect.as({ content: "ok" })),
}),
)
const snapshot = yield* registry.snapshot()
yield* snapshot.execute({
...identity,
call: {
type: "tool-call",
id: "call-codemode-repair",
name: "execute",
input: { code: 'return await tools.example.count({ count: "3" })' },
},
})
expect(executed).toEqual([{ count: 3 }])
}),
)
it.effect("repairs nested Code Mode calls against the Code Mode input schema", () =>
Effect.gen(function* () {
const plugins = yield* Plugin.Service
const registry = yield* Tool.Service
const executed: unknown[] = []
yield* plugins.activate([{ ...ToolInputRepairPlugin.Plugin, revision: "1" }])
yield* registry.transform((draft) =>
draft.add({
name: "inspect",
description: "Inspect",
input: Schema.Struct({ count: Schema.Int }),
options: {
codemode: {
input: Schema.Struct({ count: Schema.Int, detail: Schema.optionalKey(Schema.Boolean) }),
},
},
execute: (input) => Effect.sync(() => executed.push(input)).pipe(Effect.as({ content: "ok" })),
}),
)
const inCodeMode = yield* registry.snapshot()
yield* inCodeMode.execute({
...identity,
call: {
type: "tool-call",
id: "call-codemode-dynamic",
name: "execute",
input: { code: 'return await tools.inspect({ count: "3", detail: "true", extra: true })' },
},
})
const direct = yield* registry.snapshot(undefined, { tools: { direct: new Set(["inspect"]) } })
yield* direct.execute({
...identity,
call: {
type: "tool-call",
id: "call-direct-dynamic",
name: "inspect",
input: { count: "4", detail: true },
},
})
expect(executed).toEqual([{ count: 3, detail: true }, { count: 4 }])
}),
)
it.effect("fails a Code Mode shell call with keys its input doesn't take instead of dropping them", () =>
Effect.gen(function* () {
const plugins = yield* Plugin.Service
const registry = yield* Tool.Service
const executed: unknown[] = []
yield* plugins.activate([{ ...ToolInputRepairPlugin.Plugin, revision: "1" }])
yield* registry.transform((draft) =>
draft.add({
name: "shell",
description: "Shell",
input: Schema.Struct({ command: Schema.String, background: Schema.optionalKey(Schema.Boolean) }),
options: { codemode: { input: Schema.Struct({ command: Schema.String }) } },
execute: (input) => Effect.sync(() => executed.push(input)).pipe(Effect.as({ content: "ok" })),
}),
)
const snapshot = yield* registry.snapshot()
const result = yield* snapshot.execute({
...identity,
call: {
type: "tool-call",
id: "call-strict-shell",
name: "execute",
input: {
code: 'try { await tools.shell({ command: "ls", background: true, nohup: 1, description: "List" }) } catch (e) { return String(e.message ?? e) }',
},
},
})
expect(executed).toEqual([])
const text = result.content.flatMap((part) => (part.type === "text" ? [part.text] : [])).join("\n")
expect(text).toContain("<tool-repair>")
expect(text).toContain(
"tools.shell takes only `command`, `workdir`, `timeout`, `yield_ms` in Code Mode, so this call with `background`, `nohup` did not run.",
)
expect(text).toContain("execute's `background` option")
// Declared keys still run, and a key that changes nothing about the command is dropped.
yield* snapshot.execute({
...identity,
call: {
type: "tool-call",
id: "call-plain-shell",
name: "execute",
input: { code: 'return await tools.shell({ command: "ls", description: "List" })' },
},
})
expect(executed).toEqual([{ command: "ls" }])
}),
)