mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
Some checks failed
check / typecheck (push) Waiting to run
deploy-files / deploy (push) Waiting to run
deploy-www / deploy (push) Waiting to run
deploy / deploy (push) Waiting to run
nix-eval / nix-eval (push) Waiting to run
nix-hashes / compute-hash (blacksmith-4vcpu-ubuntu-2404, x86_64-linux) (push) Waiting to run
nix-hashes / compute-hash (blacksmith-4vcpu-ubuntu-2404-arm, aarch64-linux) (push) Waiting to run
nix-hashes / compute-hash (macos-latest, aarch64-darwin) (push) Waiting to run
nix-hashes / update-hashes (push) Blocked by required conditions
publish / build-electron (map[bun_install_flags:--os=darwin --cpu=x64 host:macos-26-intel platform_flag:--mac --x64 target:x86_64-apple-darwin]) (push) Blocked by required conditions
publish / build-electron (map[host:blacksmith-4vcpu-ubuntu-2404 platform_flag:--linux target:x86_64-unknown-linux-gnu]) (push) Blocked by required conditions
publish / build-electron (map[host:blacksmith-4vcpu-ubuntu-2404-arm platform_flag:--linux --arm64 target:aarch64-unknown-linux-gnu]) (push) Blocked by required conditions
publish / build-electron (map[host:blacksmith-4vcpu-windows-2025 platform_flag:--win target:x86_64-pc-windows-msvc]) (push) Blocked by required conditions
publish / publish (push) Blocked by required conditions
publish / version (push) Waiting to run
publish / build-cli (push) Blocked by required conditions
publish / sign-cli-macos (push) Blocked by required conditions
publish / build-node-app-archive (push) Blocked by required conditions
publish / build-node-cli (map[bun_install_flags:--cpu=* host:blacksmith-4vcpu-windows-2025 target:windows-arm64]) (push) Blocked by required conditions
publish / build-node-cli (map[host:blacksmith-4vcpu-ubuntu-2404 target:linux-x64]) (push) Blocked by required conditions
publish / build-node-cli (map[host:blacksmith-4vcpu-ubuntu-2404-arm target:linux-arm64]) (push) Blocked by required conditions
publish / build-node-cli (map[host:blacksmith-4vcpu-windows-2025 target:windows-x64]) (push) Blocked by required conditions
publish / build-node-cli (map[host:macos-26 target:darwin-arm64]) (push) Blocked by required conditions
publish / sign-cli-windows (push) Blocked by required conditions
publish / build-electron (map[bun_install_flags:--cpu=* host:windows-2025 platform_flag:--win --arm64 target:aarch64-pc-windows-msvc]) (push) Blocked by required conditions
publish / build-electron (map[bun_install_flags:--os=darwin --cpu=arm64 host:macos-26 platform_flag:--mac --arm64 target:aarch64-apple-darwin]) (push) Blocked by required conditions
publish / notify-discord-v2 (push) Blocked by required conditions
storybook / storybook build (push) Waiting to run
test / affected packages (push) Waiting to run
test / unit (linux) (push) Waiting to run
test / unit (windows) (push) Waiting to run
test / e2e (linux) (push) Blocked by required conditions
test / e2e (windows) (push) Blocked by required conditions
deploy-changelog / deploy (push) Has been cancelled
138 lines
5.2 KiB
TypeScript
138 lines
5.2 KiB
TypeScript
import { expect } from "bun:test"
|
|
import { Agent } from "@opencode/core/agent"
|
|
import { Permission } from "@opencode/core/permission"
|
|
import { Plugin } from "@opencode/core/plugin"
|
|
import { PluginHost } from "@opencode/core/plugin/host"
|
|
import { PluginPromise } from "@opencode/core/plugin/promise"
|
|
import { Tool } from "@opencode/core/tool"
|
|
import { Session } from "@opencode/schema/session"
|
|
import { SessionMessage } from "@opencode/schema/session-message"
|
|
import { Cause, Deferred, Effect, Exit, Fiber, Option } from "effect"
|
|
import { testEffect } from "../lib/effect"
|
|
import { PluginTestLayer } from "./fixture"
|
|
|
|
const it = testEffect(PluginTestLayer)
|
|
|
|
it.live("Promise tool executors receive interruption through their AbortSignal", () =>
|
|
Effect.gen(function* () {
|
|
const plugins = yield* Plugin.Service
|
|
const tools = yield* Tool.Service
|
|
const started = yield* Deferred.make<AbortSignal>()
|
|
yield* PluginPromise.fromPromise({
|
|
id: "cancel-tool",
|
|
async setup(context) {
|
|
await context.tool.transform((editor) =>
|
|
editor.add({
|
|
name: "wait",
|
|
description: "Wait until cancelled",
|
|
input: { type: "object", properties: {}, additionalProperties: false },
|
|
options: { codemode: false },
|
|
execute: (_input, context) =>
|
|
new Promise<never>((_resolve, reject) => {
|
|
context.signal.addEventListener("abort", () => reject(new Error("cancelled")), { once: true })
|
|
Effect.runSync(Deferred.succeed(started, context.signal))
|
|
}),
|
|
}),
|
|
)
|
|
},
|
|
}).effect(yield* PluginHost.make(plugins))
|
|
|
|
const snapshot = yield* tools.snapshot()
|
|
const fiber = yield* snapshot
|
|
.execute({
|
|
sessionID: Session.ID.make("ses_promise_tool_cancel"),
|
|
agent: Agent.ID.make("build"),
|
|
messageID: SessionMessage.ID.make("msg_promise_tool_cancel"),
|
|
call: { type: "tool-call", id: "call_promise_tool_cancel", name: "wait", input: {} },
|
|
})
|
|
.pipe(Effect.forkScoped)
|
|
const signal = yield* Deferred.await(started)
|
|
expect(signal.aborted).toBe(false)
|
|
yield* Fiber.interrupt(fiber)
|
|
const exit = yield* Fiber.await(fiber)
|
|
expect(signal.aborted).toBe(true)
|
|
expect(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause)).toBe(true)
|
|
}),
|
|
)
|
|
|
|
const execute = (snapshot: Tool.Snapshot, name: string) =>
|
|
snapshot
|
|
.execute({
|
|
sessionID: Session.ID.make("ses_promise_tool_error"),
|
|
agent: Agent.ID.make("build"),
|
|
messageID: SessionMessage.ID.make("msg_promise_tool_error"),
|
|
call: { type: "tool-call", id: `call_${name}`, name, input: {} },
|
|
})
|
|
.pipe(Effect.exit)
|
|
|
|
it.live("Promise tool throws become tool failures", () =>
|
|
Effect.gen(function* () {
|
|
const plugins = yield* Plugin.Service
|
|
const tools = yield* Tool.Service
|
|
yield* PluginPromise.fromPromise({
|
|
id: "throwing-tools",
|
|
async setup(context) {
|
|
await context.tool.transform((editor) => {
|
|
editor.add({
|
|
name: "plain",
|
|
description: "Throw an Error",
|
|
input: { type: "object", properties: {}, additionalProperties: false },
|
|
options: { codemode: false },
|
|
execute: async () => {
|
|
throw new Error("plain failure")
|
|
},
|
|
})
|
|
editor.add({
|
|
name: "typed",
|
|
description: "Throw a Tool.Error",
|
|
input: { type: "object", properties: {}, additionalProperties: false },
|
|
options: { codemode: false },
|
|
execute: async () => {
|
|
throw new Tool.Error({ message: "typed failure", metadata: { code: 7 } })
|
|
},
|
|
})
|
|
})
|
|
},
|
|
}).effect(yield* PluginHost.make(plugins))
|
|
|
|
const snapshot = yield* tools.snapshot()
|
|
const plain = yield* execute(snapshot, "plain")
|
|
const typed = yield* execute(snapshot, "typed")
|
|
expect(Option.getOrUndefined(Exit.findErrorOption(plain))).toMatchObject({ message: "plain failure" })
|
|
expect(Option.getOrUndefined(Exit.findErrorOption(typed))).toMatchObject({
|
|
message: "typed failure",
|
|
metadata: { code: 7 },
|
|
})
|
|
}),
|
|
)
|
|
|
|
it.live("Promise tools that rethrow a decline attach it to the tool failure", () =>
|
|
Effect.gen(function* () {
|
|
const plugins = yield* Plugin.Service
|
|
const tools = yield* Tool.Service
|
|
const declined = new Permission.DeclinedError()
|
|
yield* tools.transform((editor) =>
|
|
editor.add({
|
|
name: "guarded",
|
|
description: "Decline every call",
|
|
input: { type: "object", properties: {}, additionalProperties: false },
|
|
options: { codemode: false },
|
|
execute: () => Effect.die(declined),
|
|
}),
|
|
)
|
|
yield* PluginPromise.fromPromise({
|
|
id: "wrapping-tool",
|
|
async setup(context) {
|
|
await context.tool.transform((editor) =>
|
|
editor.update("guarded", (tool) => {
|
|
const original = tool.execute
|
|
tool.execute = (input, context) => original(input, context)
|
|
}),
|
|
)
|
|
},
|
|
}).effect(yield* PluginHost.make(plugins))
|
|
|
|
const exit = yield* execute(yield* tools.snapshot(), "guarded")
|
|
expect(Option.getOrUndefined(Exit.findErrorOption(exit))?.error).toBe(declined)
|
|
}),
|
|
)
|