mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 05:01:03 +03:00
feat(core): run local Windows shell commands in the process daemon (#63409)
This commit is contained in:
committed by
GitHub
Unverified
parent
464688ba68
commit
63dd3f1c6c
@@ -118,7 +118,7 @@
|
||||
"@effect/platform-node": "catalog:",
|
||||
"@opencode/client": "workspace:*",
|
||||
"@opencode/plugin": "workspace:*",
|
||||
"@opencode/pty": "0.5.0",
|
||||
"@opencode/pty": "0.5.2",
|
||||
"@opencode/schema": "workspace:*",
|
||||
"@opencode/server": "workspace:*",
|
||||
"@opencode/tui": "workspace:*",
|
||||
@@ -227,7 +227,7 @@
|
||||
"@opencode/codemode": "workspace:*",
|
||||
"@opencode/plugin": "workspace:*",
|
||||
"@opencode/plugin-browser": "workspace:*",
|
||||
"@opencode/pty": "0.5.0",
|
||||
"@opencode/pty": "0.5.2",
|
||||
"@opencode/schema": "workspace:*",
|
||||
"@opencode/util": "workspace:*",
|
||||
"@parcel/watcher": "2.5.1",
|
||||
@@ -666,7 +666,7 @@
|
||||
"devDependencies": {
|
||||
"@opencode/client": "workspace:*",
|
||||
"@opencode/plugin": "workspace:*",
|
||||
"@opencode/pty": "0.5.0",
|
||||
"@opencode/pty": "0.5.2",
|
||||
"@tsconfig/bun": "catalog:",
|
||||
"@types/bun": "catalog:",
|
||||
"@typescript/native": "catalog:",
|
||||
@@ -1831,23 +1831,23 @@
|
||||
|
||||
"@opencode/protocol": ["@opencode/protocol@workspace:packages/protocol"],
|
||||
|
||||
"@opencode/pty": ["@opencode/pty@0.5.0", "", { "optionalDependencies": { "@opencode/pty-darwin-arm64": "0.5.0", "@opencode/pty-darwin-x64": "0.5.0", "@opencode/pty-linux-arm64-gnu": "0.5.0", "@opencode/pty-linux-arm64-musl": "0.5.0", "@opencode/pty-linux-x64-gnu": "0.5.0", "@opencode/pty-linux-x64-musl": "0.5.0", "@opencode/pty-win32-arm64": "0.5.0", "@opencode/pty-win32-x64": "0.5.0" }, "bin": { "opencode-pty": "bin/opencode-pty.js" } }, "sha512-2U0RfT5U1lp1BcegXLoSBYfBx9sBNw3uJ/pao4UWdNV6UDfmDNGzdc8m4K1Q4ByBkXhbh8hexa0JS7HZ0vSsgg=="],
|
||||
"@opencode/pty": ["@opencode/pty@0.5.2", "", { "optionalDependencies": { "@opencode/pty-darwin-arm64": "0.5.2", "@opencode/pty-darwin-x64": "0.5.2", "@opencode/pty-linux-arm64-gnu": "0.5.2", "@opencode/pty-linux-arm64-musl": "0.5.2", "@opencode/pty-linux-x64-gnu": "0.5.2", "@opencode/pty-linux-x64-musl": "0.5.2", "@opencode/pty-win32-arm64": "0.5.2", "@opencode/pty-win32-x64": "0.5.2" }, "bin": { "opencode-pty": "bin/opencode-pty.js" } }, "sha512-SZrzOFZDQHvPndC+ZfDYB52qY/RFxeW0OmKeosSlm4I7tmdDxtvUYN/vy8yynh8678bL+1efbQGxhKa1nz0DCA=="],
|
||||
|
||||
"@opencode/pty-darwin-arm64": ["@opencode/pty-darwin-arm64@0.5.0", "", { "os": "darwin", "cpu": "arm64" }, "sha512-D+L/TnNUc35airWTBlLussnuDbZpKPQ2/uBHVkcycw6KrLYDb4hVoUJn8N5PXmLTnhmU64B4MUsdH9JoOZIi8w=="],
|
||||
"@opencode/pty-darwin-arm64": ["@opencode/pty-darwin-arm64@0.5.2", "", { "os": "darwin", "cpu": "arm64" }, "sha512-CxlLA/+ruVwt+QGOQEnBxiIImgpYU9EdW7bjvA0m+qSDOrFc0hAHOD13Suq3B2JJQxSsj7IM3XicQqmJfqe32Q=="],
|
||||
|
||||
"@opencode/pty-darwin-x64": ["@opencode/pty-darwin-x64@0.5.0", "", { "os": "darwin", "cpu": "x64" }, "sha512-/JMtOIAY9WWszsauWnCwjl2Qr8PBihS94tLsxa8prZBVhbbpOzcFslQsqR8JnJ1gAVlYINxEVS79+QQPQ4NC9g=="],
|
||||
"@opencode/pty-darwin-x64": ["@opencode/pty-darwin-x64@0.5.2", "", { "os": "darwin", "cpu": "x64" }, "sha512-xn4W7y8D+93WM+5+VAwTsPJjw/U9TTEZJQdtIQqFwcCgPCQUylMGu4/xhFbyMllzSycZyysJuhV6CHq/J798zw=="],
|
||||
|
||||
"@opencode/pty-linux-arm64-gnu": ["@opencode/pty-linux-arm64-gnu@0.5.0", "", { "os": "linux", "cpu": "arm64", "libc": "glibc" }, "sha512-vNiHKopfKrX6vj8OmDXYxZ+5JEZwf5djDzt3LOqeYuwStQRX2rxcl2/zjrvd5i8rAJw30RpJ+PTdS1t08vqtLA=="],
|
||||
"@opencode/pty-linux-arm64-gnu": ["@opencode/pty-linux-arm64-gnu@0.5.2", "", { "os": "linux", "cpu": "arm64", "libc": "glibc" }, "sha512-0Kg1477t9VYHLkaOStiDuoC8S3qHH92ZY/QEgNSOavgkCAAbSEU0NrkIBhEi3Gnw5+ElvfgVcLzEwcM5cfiYAA=="],
|
||||
|
||||
"@opencode/pty-linux-arm64-musl": ["@opencode/pty-linux-arm64-musl@0.5.0", "", { "os": "linux", "cpu": "arm64", "libc": "musl" }, "sha512-aW3lNGjit3FNAfpEskJgvTrtE2Lfnukpn8KmUVPUBN/EbRhT56h/N3ghzHYc5jIExETy06Xpy9i2xZxZQy0McQ=="],
|
||||
"@opencode/pty-linux-arm64-musl": ["@opencode/pty-linux-arm64-musl@0.5.2", "", { "os": "linux", "cpu": "arm64", "libc": "musl" }, "sha512-ZNk4nBooiVCvkB8cCfwn2nDV5OQHdFk/MfRQzBfwk28LakM1YC4lSwZPzY0McvFVjUILie9UEKntHRzgPaDBcA=="],
|
||||
|
||||
"@opencode/pty-linux-x64-gnu": ["@opencode/pty-linux-x64-gnu@0.5.0", "", { "os": "linux", "cpu": "x64", "libc": "glibc" }, "sha512-XaMnBSh1QvJmcYKSOtx1fVvldZ+x212zAHaq8ovyafufqp6/Xl3AuqPCXwPFnycW2PfbUDwfZ7dBCXThQtOt3Q=="],
|
||||
"@opencode/pty-linux-x64-gnu": ["@opencode/pty-linux-x64-gnu@0.5.2", "", { "os": "linux", "cpu": "x64", "libc": "glibc" }, "sha512-iLEq9nWOKfm9iWqDGlhqcx+SSLFqBz3toX83n/W+MSlxWg5048HzlGd/3RxY04sxmI6dlg/Ygz3KqP3bCgfusg=="],
|
||||
|
||||
"@opencode/pty-linux-x64-musl": ["@opencode/pty-linux-x64-musl@0.5.0", "", { "os": "linux", "cpu": "x64", "libc": "musl" }, "sha512-30LVc91GrJP8YD6chLic4+5nMsLz3hCYu2cxPQczSKyRWMyBjuwvCf040UzlRAQiGllp8KBb7nYa8SCtq2/49Q=="],
|
||||
"@opencode/pty-linux-x64-musl": ["@opencode/pty-linux-x64-musl@0.5.2", "", { "os": "linux", "cpu": "x64", "libc": "musl" }, "sha512-jBIw7oimeoTXoHH4yvtVFoPZGU1Rx+kdTVU8HVFGwCDNmeP4hI+R7QhF/pEsKZxzykTskR5x9u5ciiVJ5WEnMg=="],
|
||||
|
||||
"@opencode/pty-win32-arm64": ["@opencode/pty-win32-arm64@0.5.0", "", { "os": "win32", "cpu": "arm64" }, "sha512-BYVSytysZvXmIQcP9VbbXbOV417RLW/BATiAK8MMbHBF6fzqQlpAMTi4cQmWJKOlHgrREKeMBw786qZ8BES3Vg=="],
|
||||
"@opencode/pty-win32-arm64": ["@opencode/pty-win32-arm64@0.5.2", "", { "os": "win32", "cpu": "arm64" }, "sha512-giPMQUcSyoe2z6wQN6MKhRgu4rVv/xqscX8qOVt2lu7yfRNfviPFVY0qFPGOcu1L3e+DT5VrIo2QZeRBeV1Bdw=="],
|
||||
|
||||
"@opencode/pty-win32-x64": ["@opencode/pty-win32-x64@0.5.0", "", { "os": "win32", "cpu": "x64" }, "sha512-42OEc6vvlL/gXAairdWf4DLX6juPrpOycES9sp3iE3UfQ5KsRsuQ6F3HFFmSBkiZ/yUhJtKoJdJbquBTNEL+Ow=="],
|
||||
"@opencode/pty-win32-x64": ["@opencode/pty-win32-x64@0.5.2", "", { "os": "win32", "cpu": "x64" }, "sha512-hXdQWx+gHhQMRovDS3Fm0n4LuvSgLxRqEdicpVQzjjCTPYBMSBysWYRheQ2ydZlP0GCQ2Ymoi0dixgAHTYHO8A=="],
|
||||
|
||||
"@opencode/schema": ["@opencode/schema@workspace:packages/schema"],
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
"@effect/platform-node": "catalog:",
|
||||
"@opencode/client": "workspace:*",
|
||||
"@opencode/plugin": "workspace:*",
|
||||
"@opencode/pty": "0.5.0",
|
||||
"@opencode/pty": "0.5.2",
|
||||
"@opencode/schema": "workspace:*",
|
||||
"@opencode/server": "workspace:*",
|
||||
"@opencode/tui": "workspace:*",
|
||||
|
||||
@@ -104,7 +104,7 @@
|
||||
"@modelcontextprotocol/client": "2.4.0",
|
||||
"@modelcontextprotocol/core": "2.4.0",
|
||||
"@modelcontextprotocol/server": "2.4.0",
|
||||
"@opencode/pty": "0.5.0",
|
||||
"@opencode/pty": "0.5.2",
|
||||
"@opencode/ai": "workspace:*",
|
||||
"@opencode/codemode": "workspace:*",
|
||||
"@opencode/plugin": "workspace:*",
|
||||
|
||||
@@ -141,8 +141,8 @@ type Ended = {
|
||||
* here; callers (e.g. `ShellTool`) own that association and store the shell ID.
|
||||
*
|
||||
* Local commands run in the process daemon when it is available (`Daemon`), so they outlive a planned server
|
||||
* restart: the next server takes them back. Remote workspaces, Windows and an unavailable daemon keep spawning
|
||||
* them from the server.
|
||||
* restart: the next server takes them back. Remote workspaces and an unavailable daemon keep spawning them from
|
||||
* the server.
|
||||
*/
|
||||
export interface Interface {
|
||||
readonly create: <E = never, R = never>(
|
||||
@@ -615,12 +615,9 @@ const layer = () =>
|
||||
// Commands this server spawned for the Location; daemon commands live in `runs`, owned by the server.
|
||||
const commands = new Map<Shell.ID, Spawned>()
|
||||
const exitOrder: Shell.ID[] = []
|
||||
// Remote workspaces and Windows keep spawning commands from the server. Taken back before the service is
|
||||
// returned, so session restart recovery sees them.
|
||||
// Take local commands back before returning the service so session restart recovery sees them.
|
||||
const runs =
|
||||
location.workspaceID === undefined && process.platform !== "win32" && PersistentPty.available
|
||||
? yield* daemon.at(location)
|
||||
: undefined
|
||||
location.workspaceID === undefined && PersistentPty.available ? yield* daemon.at(location) : undefined
|
||||
|
||||
const outputDir = path.join(global.data, DIRECTORY, location.project.id)
|
||||
const { mkdir, unlink, stat } = yield* Effect.promise(() => import("fs/promises"))
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { describe, expect } from "bun:test"
|
||||
import path from "node:path"
|
||||
import { Effect, Fiber, Layer, Option, Stream } from "effect"
|
||||
import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder"
|
||||
import { LayerNode } from "@opencode/util/effect/layer-node"
|
||||
@@ -110,25 +111,24 @@ describe("PersistentPty rename", () => {
|
||||
)
|
||||
})
|
||||
|
||||
// Pipe mode is Unix-only: Windows daemons reject create_pipe, so OpenCode keeps spawning shell commands there.
|
||||
describe.skipIf(process.platform === "win32")("PersistentPty pipe processes", () => {
|
||||
const input = {
|
||||
program: "/bin/sh",
|
||||
args: ["-c", "echo hi"],
|
||||
cwd: "/tmp",
|
||||
env: { PATH: "/usr/bin" },
|
||||
groupID: "proj_test",
|
||||
outputPath: "/tmp/sh_test.out",
|
||||
metadata: { id: "sh_test", command: "echo hi" },
|
||||
}
|
||||
const pipeInput = (cwd: string) => ({
|
||||
program: process.execPath,
|
||||
args: ["--version"],
|
||||
cwd,
|
||||
env: { TEST: "1" },
|
||||
groupID: "proj_test",
|
||||
outputPath: path.join(cwd, "sh_test.out"),
|
||||
metadata: { id: "sh_test", command: "--version" },
|
||||
})
|
||||
|
||||
describe("PersistentPty pipe processes", () => {
|
||||
it.live("knows a handoff is prepared until a shutdown or until its ticket expires", () =>
|
||||
Effect.gen(function* () {
|
||||
const daemon = yield* fakeDaemon()
|
||||
yield* Effect.gen(function* () {
|
||||
const pty = yield* PersistentPty.Service
|
||||
expect(pty.handoffPrepared()).toBe(false)
|
||||
yield* pty.createPipe(input)
|
||||
yield* pty.createPipe(pipeInput(daemon.options.root))
|
||||
expect(yield* pty.handoff()).not.toBeNull()
|
||||
expect(pty.handoffPrepared()).toBe(true)
|
||||
// Abandoned by a shutdown.
|
||||
@@ -141,7 +141,7 @@ describe.skipIf(process.platform === "win32")("PersistentPty pipe processes", ()
|
||||
expiring.handoffTTL.ms = 50
|
||||
yield* Effect.gen(function* () {
|
||||
const pty = yield* PersistentPty.Service
|
||||
yield* pty.createPipe(input)
|
||||
yield* pty.createPipe(pipeInput(expiring.options.root))
|
||||
expect(yield* pty.handoff()).not.toBeNull()
|
||||
expect(pty.handoffPrepared()).toBe(true)
|
||||
yield* Effect.sleep(80)
|
||||
@@ -155,20 +155,21 @@ describe.skipIf(process.platform === "win32")("PersistentPty pipe processes", ()
|
||||
const daemon = yield* fakeDaemon()
|
||||
yield* Effect.gen(function* () {
|
||||
const pty = yield* PersistentPty.Service
|
||||
const input = pipeInput(daemon.options.root)
|
||||
const created = yield* pty.createPipe(input)
|
||||
expect(daemon.requests.at(-1)).toEqual({
|
||||
op: "create_pipe",
|
||||
program: "/bin/sh",
|
||||
args: ["-c", "echo hi"],
|
||||
cwd: "/tmp",
|
||||
env: { PATH: "/usr/bin" },
|
||||
program: input.program,
|
||||
args: input.args,
|
||||
cwd: input.cwd,
|
||||
env: input.env,
|
||||
group_id: "proj_test",
|
||||
output_path: "/tmp/sh_test.out",
|
||||
metadata: { id: "sh_test", command: "echo hi" },
|
||||
output_path: input.outputPath,
|
||||
metadata: input.metadata,
|
||||
})
|
||||
expect(created).toMatchObject({
|
||||
groupID: "proj_test",
|
||||
outputPath: "/tmp/sh_test.out",
|
||||
outputPath: input.outputPath,
|
||||
lifecycle: { status: "running" },
|
||||
metadata: input.metadata,
|
||||
exitedAt: undefined,
|
||||
@@ -210,7 +211,7 @@ describe.skipIf(process.platform === "win32")("PersistentPty pipe processes", ()
|
||||
title: "test",
|
||||
env: {},
|
||||
})
|
||||
const pipe = yield* pty.createPipe(input)
|
||||
const pipe = yield* pty.createPipe(pipeInput(directory.path))
|
||||
|
||||
expect((yield* pty.list()).map((item) => item.id)).toEqual([terminal.id])
|
||||
expect((yield* pty.listPipes()).map((item) => item.id)).toEqual([pipe.id])
|
||||
@@ -225,6 +226,7 @@ describe.skipIf(process.platform === "win32")("PersistentPty pipe processes", ()
|
||||
const daemon = yield* fakeDaemon()
|
||||
yield* Effect.gen(function* () {
|
||||
const pty = yield* PersistentPty.Service
|
||||
const input = pipeInput(daemon.options.root)
|
||||
expect(yield* Effect.flip(pty.createPipe({ ...input, cwd: "relative" }))).toEqual(
|
||||
new PersistentPty.RequestError({ message: "cwd must be absolute" }),
|
||||
)
|
||||
@@ -278,21 +280,13 @@ const layer = (daemon: { readonly options: PersistentPty.Options }) =>
|
||||
],
|
||||
)
|
||||
|
||||
describe.skipIf(process.platform === "win32")("PersistentPty handoff", () => {
|
||||
describe("PersistentPty handoff", () => {
|
||||
it.live("prepares a handoff more than once on the same owner connection", () =>
|
||||
Effect.gen(function* () {
|
||||
const daemon = yield* fakeDaemon()
|
||||
yield* Effect.gen(function* () {
|
||||
const pty = yield* PersistentPty.Service
|
||||
yield* pty.createPipe({
|
||||
program: "/bin/sh",
|
||||
args: ["-c", "echo hi"],
|
||||
cwd: "/tmp",
|
||||
env: {},
|
||||
groupID: "proj_test",
|
||||
outputPath: "/tmp/sh_handoff.out",
|
||||
metadata: {},
|
||||
})
|
||||
yield* pty.createPipe(pipeInput(daemon.options.root))
|
||||
expect(yield* pty.handoff()).toMatchObject({ ticket: "next" })
|
||||
expect(yield* pty.handoff()).toMatchObject({ ticket: "next" })
|
||||
}).pipe(Effect.provide(layer(daemon)))
|
||||
|
||||
@@ -438,9 +438,6 @@ describe("SessionExecution lifecycle", () => {
|
||||
)
|
||||
})
|
||||
|
||||
// Shell commands only run in the process daemon off Windows (pipe mode is Unix-only), so the daemon shell cases skip there.
|
||||
const daemonLive = process.platform === "win32" ? it.live.skip : it.live
|
||||
|
||||
describe("SessionRestart background recovery", () => {
|
||||
it.effect("keeps shell owners idle until a user prompt delivers recovered notices exactly once", () =>
|
||||
Effect.gen(function* () {
|
||||
@@ -530,7 +527,7 @@ describe("SessionRestart background recovery", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
daemonLive("counts a suspended parent's restart attempt before its shell-woken child can wake it", () =>
|
||||
it.live("counts a suspended parent's restart attempt before its shell-woken child can wake it", () =>
|
||||
Effect.gen(function* () {
|
||||
const db = yield* Database.Service
|
||||
const jobs = yield* Job.Service
|
||||
@@ -608,7 +605,7 @@ describe("SessionRestart background recovery", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
daemonLive("watches shell jobs whose commands survived the restart", () =>
|
||||
it.live("watches shell jobs whose commands survived the restart", () =>
|
||||
Effect.gen(function* () {
|
||||
const database = yield* Database.Service
|
||||
const store = yield* SessionStore.Service
|
||||
@@ -670,7 +667,7 @@ describe("SessionRestart background recovery", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
daemonLive("settles a shell call a killed server left running as moved to the background", () =>
|
||||
it.live("settles a shell call a killed server left running as moved to the background", () =>
|
||||
Effect.gen(function* () {
|
||||
const database = yield* Database.Service
|
||||
const store = yield* SessionStore.Service
|
||||
@@ -757,7 +754,7 @@ describe("SessionRestart background recovery", () => {
|
||||
)
|
||||
|
||||
for (const exited of [false, true])
|
||||
daemonLive(
|
||||
it.live(
|
||||
`reattaches once a ${exited ? "finished" : "running"} shell a killed server backgrounded without settling its call`,
|
||||
() =>
|
||||
Effect.gen(function* () {
|
||||
@@ -818,7 +815,7 @@ describe("SessionRestart background recovery", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
daemonLive("settles a recovered child's unsettled shell call before the child resumes", () =>
|
||||
it.live("settles a recovered child's unsettled shell call before the child resumes", () =>
|
||||
Effect.gen(function* () {
|
||||
const database = yield* Database.Service
|
||||
const store = yield* SessionStore.Service
|
||||
|
||||
@@ -23,15 +23,12 @@ import { tmpdirScoped } from "./fixture/tmpdir"
|
||||
import { binaryPath } from "@opencode/pty"
|
||||
import { it } from "./lib/effect"
|
||||
|
||||
const unix = process.platform !== "win32"
|
||||
const test = unix ? it.live : it.live.skip
|
||||
// On the test clock, for timers.
|
||||
const timed = unix ? it.effect : it.effect.skip
|
||||
const windows = process.platform === "win32"
|
||||
|
||||
const lines = (count: number) => Array.from({ length: count }, (_, index) => `line ${index + 1}\n`).join("")
|
||||
|
||||
describe("Shell on the process daemon", () => {
|
||||
test("runs a command with the server's environment, an absolute cwd and its metadata", () =>
|
||||
it.live("runs a command with the server's environment, an absolute cwd and its metadata", () =>
|
||||
Effect.gen(function* () {
|
||||
const env = yield* daemonShells()
|
||||
yield* Effect.promise(() => mkdir(path.join(env.directory, "sub")))
|
||||
@@ -103,9 +100,10 @@ describe("Shell on the process daemon", () => {
|
||||
expect(yield* shell.list()).toEqual([])
|
||||
}),
|
||||
)
|
||||
}))
|
||||
}),
|
||||
)
|
||||
|
||||
test("counts lines like the spawner and reads only what reached the file after a write error", () =>
|
||||
it.live("counts lines like the spawner and reads only what reached the file after a write error", () =>
|
||||
Effect.gen(function* () {
|
||||
const env = yield* daemonShells()
|
||||
yield* env.withShell(env.directory, (shell) =>
|
||||
@@ -139,9 +137,10 @@ describe("Shell on the process daemon", () => {
|
||||
)
|
||||
}),
|
||||
)
|
||||
}))
|
||||
}),
|
||||
)
|
||||
|
||||
test("bounds what it reads to number lines the daemon has not counted yet", () =>
|
||||
it.live("bounds what it reads to number lines the daemon has not counted yet", () =>
|
||||
Effect.gen(function* () {
|
||||
const env = yield* daemonShells()
|
||||
yield* env.withShell(env.directory, (shell) =>
|
||||
@@ -168,9 +167,10 @@ describe("Shell on the process daemon", () => {
|
||||
)
|
||||
}),
|
||||
)
|
||||
}))
|
||||
}),
|
||||
)
|
||||
|
||||
test("reports a command lost with the daemon as killed, with what it wrote", () =>
|
||||
it.live("reports a command lost with the daemon as killed, with what it wrote", () =>
|
||||
Effect.gen(function* () {
|
||||
const env = yield* daemonShells()
|
||||
yield* env.withShell(env.directory, (shell) =>
|
||||
@@ -185,9 +185,10 @@ describe("Shell on the process daemon", () => {
|
||||
})
|
||||
}),
|
||||
)
|
||||
}))
|
||||
}),
|
||||
)
|
||||
|
||||
test("stops what a failed command left in its group, and leaves a successful one's", () =>
|
||||
it.live("stops what a failed command left in its group, and leaves a successful one's", () =>
|
||||
Effect.gen(function* () {
|
||||
const env = yield* daemonShells()
|
||||
yield* env.withShell(env.directory, (shell) =>
|
||||
@@ -203,9 +204,10 @@ describe("Shell on the process daemon", () => {
|
||||
expect(env.signals(1)).toEqual([])
|
||||
}),
|
||||
)
|
||||
}))
|
||||
}),
|
||||
)
|
||||
|
||||
test("fails a command the daemon rejected instead of spawning it", () =>
|
||||
it.live("fails a command the daemon rejected instead of spawning it", () =>
|
||||
Effect.gen(function* () {
|
||||
const env = yield* daemonShells()
|
||||
env.daemon.failCreate.message = "output path is a symlink"
|
||||
@@ -217,10 +219,11 @@ describe("Shell on the process daemon", () => {
|
||||
expect(yield* shell.list()).toEqual([])
|
||||
}),
|
||||
)
|
||||
}))
|
||||
}),
|
||||
)
|
||||
|
||||
// On the test clock: the timeout and the grace period pass without waiting for them.
|
||||
timed("stops a timed-out command with SIGTERM, then SIGKILL after the grace period, and keeps it", () =>
|
||||
it.effect("stops a timed-out command with SIGTERM, then SIGKILL after the grace period, and keeps it", () =>
|
||||
Effect.gen(function* () {
|
||||
const env = yield* daemonShells()
|
||||
env.daemon.trap.add(1)
|
||||
@@ -250,7 +253,7 @@ describe("Shell on the process daemon", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
test("forgets a command in the daemon before deleting its file, on remove and on eviction", () =>
|
||||
it.live("forgets a command in the daemon before deleting its file, on remove and on eviction", () =>
|
||||
Effect.gen(function* () {
|
||||
const env = yield* daemonShells()
|
||||
yield* env.withShell(env.directory, (shell) =>
|
||||
@@ -280,9 +283,10 @@ describe("Shell on the process daemon", () => {
|
||||
expect(existsSync(finished[1]!.file)).toBe(true)
|
||||
}),
|
||||
)
|
||||
}))
|
||||
}),
|
||||
)
|
||||
|
||||
test("takes its commands back after a restart and leaves other Locations' alone", () =>
|
||||
it.live("takes its commands back after a restart and leaves other Locations' alone", () =>
|
||||
Effect.gen(function* () {
|
||||
const env = yield* daemonShells()
|
||||
env.daemon.trap.add(4)
|
||||
@@ -346,10 +350,11 @@ describe("Shell on the process daemon", () => {
|
||||
expect(yield* shell.wait(before.running.id)).toMatchObject({ status: "exited", exit: 0 })
|
||||
}),
|
||||
)
|
||||
}))
|
||||
}),
|
||||
)
|
||||
|
||||
// A reload or an eviction closes a Location while the server keeps running.
|
||||
timed("a Location closed while the server runs keeps enforcing its commands' timeouts", () =>
|
||||
it.effect("a Location closed while the server runs keeps enforcing its commands' timeouts", () =>
|
||||
Effect.gen(function* () {
|
||||
const env = yield* daemonShells()
|
||||
const closing = yield* env.openLocation(env.directory)
|
||||
@@ -363,7 +368,7 @@ describe("Shell on the process daemon", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
timed("a closed Location and its replacement see the same commands, with one timer and one report each", () =>
|
||||
it.effect("a closed Location and its replacement see the same commands, with one timer and one report each", () =>
|
||||
Effect.gen(function* () {
|
||||
const env = yield* daemonShells()
|
||||
const closing = yield* env.openLocation(env.directory)
|
||||
@@ -404,7 +409,7 @@ describe("Shell on the process daemon", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
test("finishes taking a Location's commands back even if the call that started it is interrupted", () =>
|
||||
it.live("finishes taking a Location's commands back even if the call that started it is interrupted", () =>
|
||||
Effect.gen(function* () {
|
||||
const env = yield* daemonShells()
|
||||
const created = yield* env.withShell(env.directory, (shell) =>
|
||||
@@ -435,9 +440,10 @@ describe("Shell on the process daemon", () => {
|
||||
const runs = yield* server.at(place)
|
||||
expect(runs.list().map((run) => run.info.id)).toEqual([created.id])
|
||||
expect(lists()).toBe(before + 1)
|
||||
}))
|
||||
}),
|
||||
)
|
||||
|
||||
test("the sweep keeps a running command's file, and deletes a finished one's file and forgets it", () =>
|
||||
it.live("the sweep keeps a running command's file, and deletes a finished one's file and forgets it", () =>
|
||||
Effect.gen(function* () {
|
||||
const env = yield* daemonShells()
|
||||
const ids = yield* env.withShell(env.directory, (shell) =>
|
||||
@@ -481,11 +487,12 @@ describe("Shell on the process daemon", () => {
|
||||
expect(env.daemon.processes.map((process) => process.id)).toEqual([1])
|
||||
// The old process's file is recent, so it stays.
|
||||
expect(existsSync(ids.old.file)).toBe(true)
|
||||
}))
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
// Against the real opencode-pty, like server/test/persistent-pty.test.ts.
|
||||
const binary = unix ? (process.env.OPENCODE_PTY_BIN ?? binaryPath) : undefined
|
||||
const binary = process.env.OPENCODE_PTY_BIN ?? binaryPath
|
||||
const real = binary !== undefined && existsSync(binary) ? it.live : it.live.skip
|
||||
|
||||
describe("Shell on a real process daemon", () => {
|
||||
@@ -502,12 +509,15 @@ describe("Shell on a real process daemon", () => {
|
||||
yield* Effect.addFinalizer(() => Effect.sync(() => delete process.env.OPENCODE_TEST_REMOVED))
|
||||
yield* Effect.gen(function* () {
|
||||
const shell = yield* Shell.Service
|
||||
const pty = yield* PersistentPty.Service
|
||||
yield* Shell.cleanup()
|
||||
expect(started()).toBe(false)
|
||||
const info = yield* shell.create(
|
||||
{
|
||||
shell: "/bin/sh",
|
||||
command: 'printf "%s|%s|%s\\n" "${OPENCODE_TEST_REMOVED:-unset}" "$TERM" "$(pwd)"; printf tail',
|
||||
shell: windows ? (Bun.which("pwsh") ?? "powershell.exe") : "/bin/sh",
|
||||
command: windows
|
||||
? "if ($env:OPENCODE_TEST_REMOVED) { exit 1 }; [Console]::Out.Write('unset|' + $env:TERM + '|' + $PWD.Path + [char]10 + 'tail')"
|
||||
: 'printf "%s|%s|%s\\n" "${OPENCODE_TEST_REMOVED:-unset}" "$TERM" "$(pwd)"; printf tail',
|
||||
},
|
||||
(invocation) =>
|
||||
Effect.sync(() => {
|
||||
@@ -515,6 +525,7 @@ describe("Shell on a real process daemon", () => {
|
||||
}),
|
||||
)
|
||||
expect(started()).toBe(true)
|
||||
expect(yield* pty.listPipes()).toMatchObject([{ pid: info.pid, metadata: { id: info.id } }])
|
||||
expect(yield* shell.result(info)).toMatchObject({
|
||||
info: { status: "exited", exit: 0 },
|
||||
capture: {
|
||||
|
||||
@@ -35,6 +35,7 @@ import { testEffect } from "./lib/effect"
|
||||
import { permissionLayer } from "./lib/permission"
|
||||
|
||||
const it = testEffect(tempGlobalLayer)
|
||||
const idleCommand = process.platform === "win32" ? "Start-Sleep -Seconds 60" : "sleep 60"
|
||||
|
||||
const model = SessionRunnerModel.node.replace(
|
||||
Layer.succeed(SessionRunnerModel.Service, {
|
||||
@@ -95,7 +96,7 @@ const world = Effect.gen(function* () {
|
||||
),
|
||||
scope,
|
||||
)
|
||||
const shellSpawns = () => spawns.filter((command) => JSON.stringify(command).includes("sleep 60"))
|
||||
const shellSpawns = () => spawns.filter((spawn) => JSON.stringify(spawn).includes(idleCommand))
|
||||
return { daemon, llm: Context.get(llm, TestLLM.Test), project, server, shellSpawns }
|
||||
})
|
||||
|
||||
@@ -115,8 +116,7 @@ const until = (check: () => boolean) =>
|
||||
while (!check()) yield* Effect.sleep(5)
|
||||
})
|
||||
|
||||
// Shell commands only run in the process daemon off Windows (pipe mode is Unix-only).
|
||||
describe.skipIf(process.platform === "win32")("shell commands across a graceful restart", () => {
|
||||
describe("shell commands across a graceful restart", () => {
|
||||
for (const finished of [false, true])
|
||||
it.live(
|
||||
`settles a foreground call ${finished ? "whose command finished during the restart" : "whose command still runs"} and resumes its session`,
|
||||
@@ -127,7 +127,7 @@ describe.skipIf(process.platform === "win32")("shell commands across a graceful
|
||||
let turns = 0
|
||||
yield* llm.serve((request) =>
|
||||
request.tools.some((tool) => tool.name === "shell") && turns++ === 0
|
||||
? TestLLM.toolCall("call-restart", "shell", { command: "sleep 60" })
|
||||
? TestLLM.toolCall("call-restart", "shell", { command: idleCommand })
|
||||
: TestLLM.text("Done", "done"),
|
||||
)
|
||||
|
||||
@@ -210,7 +210,7 @@ describe.skipIf(process.platform === "win32")("shell commands across a graceful
|
||||
let turns = 0
|
||||
yield* llm.serve((request) =>
|
||||
request.tools.some((tool) => tool.name === "shell") && turns++ === 0
|
||||
? TestLLM.toolCall("call-shutdown", "shell", { command: "sleep 60" })
|
||||
? TestLLM.toolCall("call-shutdown", "shell", { command: idleCommand })
|
||||
: TestLLM.text("Done", "done"),
|
||||
)
|
||||
let release = () => {}
|
||||
|
||||
@@ -231,8 +231,6 @@ const call = (input: typeof ShellTool.Input.Type, id = "call-shell") => ({
|
||||
})
|
||||
|
||||
const isWindows = process.platform === "win32"
|
||||
// Shell commands only run in the process daemon off Windows (pipe mode is Unix-only), so these cases have no daemon there.
|
||||
const daemonLive = isWindows ? fakeDaemonIt.live.skip : fakeDaemonIt.live
|
||||
|
||||
// The process daemon keeps a finished command's zombie until the command is forgotten.
|
||||
const running = (pid: number) => {
|
||||
@@ -1805,7 +1803,7 @@ describe("ShellTool", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
daemonLive("keeps a background daemon command's job across a Location reload and reports how it ended", () =>
|
||||
fakeDaemonIt.live("keeps a background daemon command's job across a Location reload and reports how it ended", () =>
|
||||
withTempDir((tmp) =>
|
||||
Effect.gen(function* () {
|
||||
reset()
|
||||
@@ -2224,44 +2222,47 @@ describe("ShellTool stopped", () => {
|
||||
return { shellID, exit, result: Exit.isSuccess(exit) ? exit.value : undefined }
|
||||
})
|
||||
|
||||
const test = isWindows ? parity.skip : parity.live
|
||||
parity.live(
|
||||
"stops a command the user stopped, keeps its file and fails the call with the tail, file and shell ID",
|
||||
() =>
|
||||
withTempDir((tmp) => {
|
||||
reset()
|
||||
return withSession(tmp.path, (registry) =>
|
||||
Effect.gen(function* () {
|
||||
const shell = yield* Shell.Service
|
||||
const jobs = yield* Job.Service
|
||||
const { shellID, result } = yield* stopCall(
|
||||
registry,
|
||||
isWindows
|
||||
? "[Console]::Out.Write('first' + [char]10 + 'second' + [char]10); Start-Sleep -Seconds 60"
|
||||
: "printf 'first\\nsecond\\n'; sleep 60",
|
||||
"call-stopped",
|
||||
"cancel",
|
||||
)
|
||||
const info = yield* shell.get(shellID)
|
||||
expect(info.status).toBe("killed")
|
||||
expect(yield* Effect.promise(() => fs.readFile(info.file, "utf8"))).toBe("first\nsecond\n")
|
||||
expect(result).toEqual({
|
||||
status: "error",
|
||||
error: {
|
||||
type: "tool.execution",
|
||||
message: `first\nsecond\n\n[full output saved to ${info.file}]\n\nCommand interrupted by the user (shell ID: ${shellID}).`,
|
||||
},
|
||||
})
|
||||
expect((yield* jobs.get(shellID))?.status).not.toBe("running")
|
||||
}),
|
||||
)
|
||||
}),
|
||||
)
|
||||
|
||||
test("stops a command the user stopped, keeps its file and fails the call with the tail, file and shell ID", () =>
|
||||
withTempDir((tmp) => {
|
||||
reset()
|
||||
return withSession(tmp.path, (registry) =>
|
||||
Effect.gen(function* () {
|
||||
const shell = yield* Shell.Service
|
||||
const jobs = yield* Job.Service
|
||||
const { shellID, result } = yield* stopCall(
|
||||
registry,
|
||||
"printf 'first\\nsecond\\n'; sleep 60",
|
||||
"call-stopped",
|
||||
"cancel",
|
||||
)
|
||||
const info = yield* shell.get(shellID)
|
||||
expect(info.status).toBe("killed")
|
||||
expect(yield* Effect.promise(() => fs.readFile(info.file, "utf8"))).toBe("first\nsecond\n")
|
||||
expect(result).toEqual({
|
||||
status: "error",
|
||||
error: {
|
||||
type: "tool.execution",
|
||||
message: `first\nsecond\n\n[full output saved to ${info.file}]\n\nCommand interrupted by the user (shell ID: ${shellID}).`,
|
||||
},
|
||||
})
|
||||
expect((yield* jobs.get(shellID))?.status).not.toBe("running")
|
||||
}),
|
||||
)
|
||||
}))
|
||||
|
||||
test("ends a call the user stopped before its command started, without starting it", () =>
|
||||
parity.live("ends a call the user stopped before its command started, without starting it", () =>
|
||||
withTempDir((tmp) => {
|
||||
reset()
|
||||
return withSession(tmp.path, (registry) =>
|
||||
Effect.gen(function* () {
|
||||
const shell = yield* Shell.Service
|
||||
const result = yield* executeTool(registry, {
|
||||
...call({ command: "printf started > started" }, "call-early-stop"),
|
||||
...call({ command: "echo started > started" }, "call-early-stop"),
|
||||
// Already stopped, as when the user stops the step while the call waits for permission.
|
||||
cancel: Effect.void,
|
||||
})
|
||||
@@ -2270,24 +2271,28 @@ describe("ShellTool stopped", () => {
|
||||
expect(yield* Effect.promise(() => Bun.file(path.join(tmp.path, "started")).exists())).toBe(false)
|
||||
}),
|
||||
)
|
||||
}))
|
||||
}),
|
||||
)
|
||||
|
||||
test("says once that a stopped command wrote nothing", () =>
|
||||
parity.live("says once that a stopped command wrote nothing", () =>
|
||||
withTempDir((tmp) => {
|
||||
reset()
|
||||
return withSession(tmp.path, (registry) =>
|
||||
Effect.gen(function* () {
|
||||
const shell = yield* Shell.Service
|
||||
const { shellID, result } = yield* stopCall(registry, "sleep 60", "call-silent", "cancel", false)
|
||||
const { shellID, result } = yield* stopCall(registry, idleCommand, "call-silent", "cancel", false)
|
||||
const info = yield* shell.get(shellID)
|
||||
expect(result?.error?.message).toBe(
|
||||
`[no output; output file: ${info.file}]\n\nCommand interrupted by the user (shell ID: ${shellID}).`,
|
||||
)
|
||||
}),
|
||||
)
|
||||
}))
|
||||
}),
|
||||
)
|
||||
|
||||
test(
|
||||
// Windows signals terminate the job immediately; a process cannot ignore SIGTERM there.
|
||||
const ignoresTerm = isWindows ? parity.skip : parity.live
|
||||
ignoresTerm(
|
||||
"settles a stopped call without waiting for a command that ignores SIGTERM",
|
||||
() =>
|
||||
withTempDir((tmp) => {
|
||||
@@ -2324,7 +2329,7 @@ describe("ShellTool stopped", () => {
|
||||
// The step's own gate: a stop goes through `cancel` only if the tool is awaiting it.
|
||||
const cancel = SessionStep.makeCancel()
|
||||
const fiber = yield* executeTool(registry, {
|
||||
...call({ command: "sleep 60" }, id),
|
||||
...call({ command: idleCommand }, id),
|
||||
cancel: cancel.effect,
|
||||
}).pipe(Effect.forkChild({ startImmediately: true }))
|
||||
while (!fake?.ops.includes("create_pipe")) yield* Effect.promise(() => Bun.sleep(2))
|
||||
@@ -2338,7 +2343,7 @@ describe("ShellTool stopped", () => {
|
||||
}
|
||||
})
|
||||
|
||||
daemonLive("stops a command the user stopped while it was being created", () =>
|
||||
fakeDaemonIt.live("stops a command the user stopped while it was being created", () =>
|
||||
withTempDir((tmp) => {
|
||||
reset()
|
||||
return withSession(tmp.path, (registry) =>
|
||||
@@ -2363,7 +2368,7 @@ describe("ShellTool stopped", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
daemonLive("stops a command the user stopped after its Location reloaded", () =>
|
||||
fakeDaemonIt.live("stops a command the user stopped after its Location reloaded", () =>
|
||||
withTempDir((tmp) =>
|
||||
Effect.gen(function* () {
|
||||
reset()
|
||||
@@ -2379,7 +2384,7 @@ describe("ShellTool stopped", () => {
|
||||
const cancel = SessionStep.makeCancel()
|
||||
const started = yield* Deferred.make<string>()
|
||||
const fiber = yield* executeTool(registry, {
|
||||
...call({ command: "sleep 60" }, "call-reloaded"),
|
||||
...call({ command: idleCommand }, "call-reloaded"),
|
||||
progress: (update) =>
|
||||
typeof update.shellID === "string"
|
||||
? Deferred.succeed(started, update.shellID).pipe(Effect.asVoid)
|
||||
@@ -2402,7 +2407,7 @@ describe("ShellTool stopped", () => {
|
||||
|
||||
for (const handoff of [false, true])
|
||||
for (const starting of [true, false])
|
||||
daemonLive(
|
||||
fakeDaemonIt.live(
|
||||
`${handoff ? "leaves a command running" : "stops a command"} when an interrupt ${handoff ? "during a handoff " : ""}cuts its call off while it ${starting ? "starts" : "runs"}`,
|
||||
() =>
|
||||
withTempDir((tmp) => {
|
||||
@@ -2447,12 +2452,12 @@ describe("ShellTool stopped", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
daemonLive("records which call waits on a daemon command, and keeps no timeout for a restart", () =>
|
||||
fakeDaemonIt.live("records which call waits on a daemon command, and keeps no timeout for a restart", () =>
|
||||
withTempDir((tmp) => {
|
||||
reset()
|
||||
return withSession(tmp.path, (registry) =>
|
||||
Effect.gen(function* () {
|
||||
const { shellID } = yield* stopCall(registry, "sleep 60", "call-recorded", "interrupt", false)
|
||||
const { shellID } = yield* stopCall(registry, idleCommand, "call-recorded", "interrupt", false)
|
||||
const request = fake?.requests.find((item) => item.op === "create_pipe")
|
||||
expect(request?.metadata).toMatchObject({
|
||||
id: shellID,
|
||||
@@ -2465,7 +2470,7 @@ describe("ShellTool stopped", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
test("lets an execute.after hook replace a stopped call's failure", () =>
|
||||
parity.live("lets an execute.after hook replace a stopped call's failure", () =>
|
||||
withTempDir((tmp) => {
|
||||
reset()
|
||||
return withSession(tmp.path, (registry) =>
|
||||
@@ -2479,7 +2484,7 @@ describe("ShellTool stopped", () => {
|
||||
event.error = new Tool.Error({ message: "rewritten by a plugin" })
|
||||
}),
|
||||
)
|
||||
const { shellID, result } = yield* stopCall(registry, "printf ready; sleep 60", "call-hooked", "cancel")
|
||||
const { shellID, result } = yield* stopCall(registry, startingCommand, "call-hooked", "cancel")
|
||||
expect(seen).toEqual([
|
||||
{
|
||||
interrupted: true,
|
||||
@@ -2491,20 +2496,22 @@ describe("ShellTool stopped", () => {
|
||||
expect(result?.error?.message).toBe("rewritten by a plugin")
|
||||
}),
|
||||
)
|
||||
}))
|
||||
}),
|
||||
)
|
||||
|
||||
test("stops a command whose call an interrupt cut off, with no handoff to take it over", () =>
|
||||
parity.live("stops a command whose call an interrupt cut off, with no handoff to take it over", () =>
|
||||
withTempDir((tmp) => {
|
||||
reset()
|
||||
return withSession(tmp.path, (registry) =>
|
||||
Effect.gen(function* () {
|
||||
const shell = yield* Shell.Service
|
||||
const jobs = yield* Job.Service
|
||||
const { shellID, exit } = yield* stopCall(registry, "printf ready; sleep 60", "call-teardown", "interrupt")
|
||||
const { shellID, exit } = yield* stopCall(registry, startingCommand, "call-teardown", "interrupt")
|
||||
expect(Exit.hasInterrupts(exit)).toBe(true)
|
||||
expect((yield* shell.wait(shellID)).status).toBe("killed")
|
||||
expect((yield* jobs.get(shellID))?.status).toBe("cancelled")
|
||||
}),
|
||||
)
|
||||
}))
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
@@ -37,7 +37,7 @@
|
||||
"effect": "catalog:"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@opencode/pty": "0.5.0",
|
||||
"@opencode/pty": "0.5.2",
|
||||
"@opencode/client": "workspace:*",
|
||||
"@opencode/plugin": "workspace:*",
|
||||
"@tsconfig/bun": "catalog:",
|
||||
|
||||
Reference in New Issue
Block a user