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
24 changes: 12 additions & 12 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:*",
Expand Down
11 changes: 4 additions & 7 deletions packages/core/src/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>(
Expand Down Expand Up @@ -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"))
Expand Down
56 changes: 25 additions & 31 deletions packages/core/test/persistent-pty-session.test.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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])
Expand All @@ -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" }),
)
Expand Down Expand Up @@ -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)))
Expand Down
13 changes: 5 additions & 8 deletions packages/core/test/session-execution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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* () {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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* () {
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading