From d22bc1d1b6b57e6f9b30c242c03bf5eaa00e127f Mon Sep 17 00:00:00 2001 From: James Long Date: Tue, 10 Nov 2026 13:15:25 -0600 Subject: [PATCH] feat(core): speak opencode-pty protocol v8 (#62976) --- bun.lock | 24 +- packages/cli/package.json | 2 +- packages/core/package.json | 2 +- packages/core/src/persistent-pty/daemon.ts | 175 +++++++---- packages/core/src/persistent-pty/index.ts | 276 +++++++++++++++--- .../test/fixture/persistent-pty-daemon.ts | 236 +++++++++++---- .../core/test/persistent-pty-daemon.test.ts | 179 ++++++++---- .../core/test/persistent-pty-session.test.ts | 124 +++++++- packages/server/package.json | 2 +- .../server/src/handlers/persistent-pty.ts | 15 +- packages/server/test/persistent-pty.test.ts | 74 ++--- 11 files changed, 810 insertions(+), 299 deletions(-) diff --git a/bun.lock b/bun.lock index d79de285255..9219767ad1a 100644 --- a/bun.lock +++ b/bun.lock @@ -119,7 +119,7 @@ "@effect/platform-node": "catalog:", "@opencode/client": "workspace:*", "@opencode/plugin": "workspace:*", - "@opencode/pty": "0.4.0", + "@opencode/pty": "0.5.0", "@opencode/schema": "workspace:*", "@opencode/server": "workspace:*", "@opencode/tui": "workspace:*", @@ -228,7 +228,7 @@ "@opencode/codemode": "workspace:*", "@opencode/plugin": "workspace:*", "@opencode/plugin-browser": "workspace:*", - "@opencode/pty": "0.4.0", + "@opencode/pty": "0.5.0", "@opencode/schema": "workspace:*", "@opencode/util": "workspace:*", "@parcel/watcher": "2.5.1", @@ -623,7 +623,7 @@ "devDependencies": { "@opencode/client": "workspace:*", "@opencode/plugin": "workspace:*", - "@opencode/pty": "0.4.0", + "@opencode/pty": "0.5.0", "@tsconfig/bun": "catalog:", "@types/bun": "catalog:", "@typescript/native": "catalog:", @@ -1833,23 +1833,23 @@ "@opencode/protocol": ["@opencode/protocol@workspace:packages/protocol"], - "@opencode/pty": ["@opencode/pty@0.4.0", "", { "optionalDependencies": { "@opencode/pty-darwin-arm64": "0.4.0", "@opencode/pty-darwin-x64": "0.4.0", "@opencode/pty-linux-arm64-gnu": "0.4.0", "@opencode/pty-linux-arm64-musl": "0.4.0", "@opencode/pty-linux-x64-gnu": "0.4.0", "@opencode/pty-linux-x64-musl": "0.4.0", "@opencode/pty-win32-arm64": "0.4.0", "@opencode/pty-win32-x64": "0.4.0" }, "bin": { "opencode-pty": "bin/opencode-pty.js" } }, "sha512-v7cLT71yfoNM5FlyPGwMjeqIJbr0DneGq4NpoOY5iAKpsevzOIcsI2E0fiDWajuk5xAFRxwyv4CpGqow7cu79Q=="], + "@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-darwin-arm64": ["@opencode/pty-darwin-arm64@0.4.0", "", { "os": "darwin", "cpu": "arm64" }, "sha512-XzZZJcQeR42IRpxHbbPEo4AK7jdp9SpgPx234DZ6uLnl6yclXcixH70G2U88EhKtGAMyCPIXdBfvQf4OQTDvbQ=="], + "@opencode/pty-darwin-arm64": ["@opencode/pty-darwin-arm64@0.5.0", "", { "os": "darwin", "cpu": "arm64" }, "sha512-D+L/TnNUc35airWTBlLussnuDbZpKPQ2/uBHVkcycw6KrLYDb4hVoUJn8N5PXmLTnhmU64B4MUsdH9JoOZIi8w=="], - "@opencode/pty-darwin-x64": ["@opencode/pty-darwin-x64@0.4.0", "", { "os": "darwin", "cpu": "x64" }, "sha512-NvQfygGcsGmDCIUJ/VHLcS6SduC93Y/qP2hrHjtBpiL2sSQyeCDcifNhLvqY/wxGFNNZ4GeZwwZqe7rMEuBJbQ=="], + "@opencode/pty-darwin-x64": ["@opencode/pty-darwin-x64@0.5.0", "", { "os": "darwin", "cpu": "x64" }, "sha512-/JMtOIAY9WWszsauWnCwjl2Qr8PBihS94tLsxa8prZBVhbbpOzcFslQsqR8JnJ1gAVlYINxEVS79+QQPQ4NC9g=="], - "@opencode/pty-linux-arm64-gnu": ["@opencode/pty-linux-arm64-gnu@0.4.0", "", { "os": "linux", "cpu": "arm64", "libc": "glibc" }, "sha512-VZXkYD+CT+zdodmTnKn9/VlmswksLPBLJryeBsG4IXUFXTsATr7+PVygJk96FxCh5nq68H3a/UJgyiIUMl1XCQ=="], + "@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-musl": ["@opencode/pty-linux-arm64-musl@0.4.0", "", { "os": "linux", "cpu": "arm64", "libc": "musl" }, "sha512-hCx25Hn9sALdnkx2Y4aPGVw3glTbX61XRiJs+XETPPIO9QTaav4OS4k2JR2uHC6/u161xKdN82iJ+OYfwcGUSg=="], + "@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-x64-gnu": ["@opencode/pty-linux-x64-gnu@0.4.0", "", { "os": "linux", "cpu": "x64", "libc": "glibc" }, "sha512-Xal/d0+xp6XE4iCGUAw4JTQ4uf5l8V0EIbeKq+e03cHJewOMKZk4fooQjxpZvQIToBHRtnCJAmd+EEhVxXiDDw=="], + "@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-musl": ["@opencode/pty-linux-x64-musl@0.4.0", "", { "os": "linux", "cpu": "x64", "libc": "musl" }, "sha512-dBVtKZoVKAN6/E+65GrJksgRKzPkPHUjWZBzf3mRANXt2adpdbV+/M1AMfPCZSju2mN1j9rNu8MCrxwr+ugEig=="], + "@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-win32-arm64": ["@opencode/pty-win32-arm64@0.4.0", "", { "os": "win32", "cpu": "arm64" }, "sha512-IKJMUnS2aluWqRj13enMWomYtwbXq8rStcf/mUbxBoAGTDMtWR/ZrRmKb2ptR7KUqDjdSEa+8C8rczS7uGFl9g=="], + "@opencode/pty-win32-arm64": ["@opencode/pty-win32-arm64@0.5.0", "", { "os": "win32", "cpu": "arm64" }, "sha512-BYVSytysZvXmIQcP9VbbXbOV417RLW/BATiAK8MMbHBF6fzqQlpAMTi4cQmWJKOlHgrREKeMBw786qZ8BES3Vg=="], - "@opencode/pty-win32-x64": ["@opencode/pty-win32-x64@0.4.0", "", { "os": "win32", "cpu": "x64" }, "sha512-P1QJLUX918/y8he8VEBSYoiN5bYhUnJtEaEpiE5fxoYsDP9nqAHXqIrGxTNNnda5jOBcCalxxy0WhL8qgeKOqQ=="], + "@opencode/pty-win32-x64": ["@opencode/pty-win32-x64@0.5.0", "", { "os": "win32", "cpu": "x64" }, "sha512-42OEc6vvlL/gXAairdWf4DLX6juPrpOycES9sp3iE3UfQ5KsRsuQ6F3HFFmSBkiZ/yUhJtKoJdJbquBTNEL+Ow=="], "@opencode/schema": ["@opencode/schema@workspace:packages/schema"], diff --git a/packages/cli/package.json b/packages/cli/package.json index 2207532ff44..25b69405d63 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -32,7 +32,7 @@ "@effect/platform-node": "catalog:", "@opencode/client": "workspace:*", "@opencode/plugin": "workspace:*", - "@opencode/pty": "0.4.0", + "@opencode/pty": "0.5.0", "@opencode/schema": "workspace:*", "@opencode/server": "workspace:*", "@opencode/tui": "workspace:*", diff --git a/packages/core/package.json b/packages/core/package.json index 38b1c5dea6c..df233a19cb4 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -104,7 +104,7 @@ "@modelcontextprotocol/client": "2.4.0", "@modelcontextprotocol/core": "2.4.0", "@modelcontextprotocol/server": "2.4.0", - "@opencode/pty": "0.4.0", + "@opencode/pty": "0.5.0", "@opencode/ai": "workspace:*", "@opencode/codemode": "workspace:*", "@opencode/plugin": "workspace:*", diff --git a/packages/core/src/persistent-pty/daemon.ts b/packages/core/src/persistent-pty/daemon.ts index 526dc59c1a5..95382daf0a4 100644 --- a/packages/core/src/persistent-pty/daemon.ts +++ b/packages/core/src/persistent-pty/daemon.ts @@ -1,36 +1,59 @@ import { spawn } from "node:child_process" -import { mkdir, readFile, rename } from "node:fs/promises" +import { readFile } from "node:fs/promises" import net from "node:net" import path from "node:path" import { Cause, Data, Duration, Effect, Schema, Semaphore } from "effect" import type { Handoff } from "@opencode/schema/persistent-pty" -import { Session } from "@opencode/schema/session" -const ProtocolVersion = 7 +const ProtocolVersion = 8 const MaxFrameBytes = 8 * 1024 * 1024 const Lifecycle = Schema.Union([ Schema.Struct({ status: Schema.Literal("running") }), - Schema.Struct({ status: Schema.Literal("exited"), exit_code: Schema.NullOr(Schema.Number) }), + Schema.Struct({ + status: Schema.Literal("exited"), + exit_code: Schema.NullOr(Schema.Number), + signal: Schema.optional(Schema.NullOr(Schema.String)), + }), Schema.Struct({ status: Schema.Literal("failed"), message: Schema.String }), ]) -export const WireTerminal = Schema.Struct({ - id: Schema.Number, - pid: Schema.NullOr(Schema.Number), +const PtyInfo = Schema.Struct({ title: Schema.String, - user_title: Schema.optional(Schema.NullOr(Schema.String)), + user_title: Schema.NullOr(Schema.String), foreground_process: Schema.NullOr(Schema.String), - group_id: Session.ID, - command: Schema.Array(Schema.String), - cwd: Schema.String, cols: Schema.Number, rows: Schema.Number, +}) + +export const Metadata = Schema.Record(Schema.String, Schema.Unknown) +export type Metadata = typeof Metadata.Type + +const PipeInfo = Schema.Struct({ + output_path: Schema.String, + output_lines: Schema.Number, + output_error: Schema.optional(Schema.NullOr(Schema.String)), + metadata: Metadata, +}) + +export const ProcessInfo = Schema.Struct({ + id: Schema.Number, + mode: Schema.Literals(["pty", "pipe"]), + pid: Schema.NullOr(Schema.Number), + command: Schema.Array(Schema.String), + cwd: Schema.String, + group_id: Schema.String, lifecycle: Lifecycle, output_head: Schema.Number, output_tail: Schema.Number, + started_at: Schema.Number, + exited_at: Schema.optional(Schema.NullOr(Schema.Number)), + pty: Schema.optional(Schema.NullOr(PtyInfo)), + pipe: Schema.optional(Schema.NullOr(PipeInfo)), }) -export type WireTerminal = typeof WireTerminal.Type +export type ProcessInfo = typeof ProcessInfo.Type + +export type Signal = "SIGTERM" | "SIGINT" | "SIGKILL" const Registration = Schema.Struct({ instance_id: Schema.String, @@ -38,7 +61,7 @@ const Registration = Schema.Struct({ protocol: Schema.Number, socket: Schema.String, token: Schema.String, - features: Schema.optional(Schema.Array(Schema.String)), + features: Schema.Array(Schema.String), }) type Registration = typeof Registration.Type @@ -48,16 +71,17 @@ export const WireResponse = Schema.Union([ instance_id: Schema.String, pid: Schema.Number, protocol: Schema.Number, - features: Schema.optional(Schema.Array(Schema.String)), + features: Schema.Array(Schema.String), }), - Schema.Struct({ type: Schema.Literal("created"), terminal: WireTerminal }), - Schema.Struct({ type: Schema.Literal("terminals"), terminals: Schema.Array(WireTerminal) }), + Schema.Struct({ type: Schema.Literal("created"), process: ProcessInfo }), + Schema.Struct({ type: Schema.Literal("processes"), processes: Schema.Array(ProcessInfo) }), + Schema.Struct({ type: Schema.Literal("process"), process: ProcessInfo }), Schema.Struct({ type: Schema.Literal("ok") }), Schema.Struct({ type: Schema.Literal("owned") }), Schema.Struct({ type: Schema.Literal("handoff"), ticket: Schema.String, expires_at: Schema.Number }), Schema.Struct({ type: Schema.Literal("snapshot"), - terminal: WireTerminal, + process: ProcessInfo, text: Schema.String, checkpoint_base64: Schema.String, cursor_x: Schema.Number, @@ -65,14 +89,14 @@ export const WireResponse = Schema.Union([ }), Schema.Struct({ type: Schema.Literal("rows"), - terminal: WireTerminal, + process: ProcessInfo, lines: Schema.Array(Schema.String), cursor_x: Schema.Number, cursor_y: Schema.Number, }), Schema.Struct({ type: Schema.Literal("attached"), - terminal: WireTerminal, + process: ProcessInfo, role: Schema.Literals(["controller", "observer"]), generation: Schema.Number, requested_offset: Schema.Number, @@ -91,7 +115,12 @@ export const WireResponse = Schema.Union([ Schema.Struct({ type: Schema.Literal("exited"), exit_code: Schema.NullOr(Schema.Number), + signal: Schema.optional(Schema.NullOr(Schema.String)), final_offset: Schema.Number, + // Pipe processes only. + output_lines: Schema.optional(Schema.Number), + output_error: Schema.optional(Schema.NullOr(Schema.String)), + exited_at: Schema.Number, }), Schema.Struct({ type: Schema.Literal("controller_changed"), @@ -103,6 +132,7 @@ export const WireResponse = Schema.Union([ Schema.Struct({ type: Schema.Literal("error"), message: Schema.String }), ]) export type WireResponse = typeof WireResponse.Type +export type Exited = Extract export type Role = "controller" | "observer" @@ -130,7 +160,7 @@ export type SubscribeInput = { } export type DaemonAttachment = { - readonly terminal: WireTerminal + readonly terminal: ProcessInfo readonly role: Role readonly generation: number readonly replay: { @@ -144,8 +174,13 @@ export type DaemonAttachment = { readonly detach: () => void } +/** + * `connect`, `spawn`, `registration` and `protocol` mean the daemon could not be reached or started and the + * request never ran. `response` means the request was sent but its reply was lost. `request` is the daemon's + * own `error` reply. + */ export class DaemonError extends Data.TaggedError("PersistentPty.DaemonError")<{ - readonly kind: "connect" | "response" | "registration" | "protocol" | "spawn" + readonly kind: "connect" | "response" | "registration" | "protocol" | "spawn" | "request" readonly message: string readonly pid?: number }> {} @@ -153,13 +188,11 @@ export class DaemonError extends Data.TaggedError("PersistentPty.DaemonError")<{ export interface DaemonTransport { readonly request: (value: object, start?: boolean) => Effect.Effect readonly requestIfRunning: (value: object) => Effect.Effect - readonly supports: (feature: string) => Effect.Effect + /** Waits on its own connection, with no timeout, until the pipe process exits. Never starts the daemon. */ + readonly wait: (id: number) => Effect.Effect readonly shutdown: Effect.Effect readonly handoff: Effect.Effect - readonly subscribe: ( - id: number, - input: SubscribeInput, - ) => Effect.Effect + readonly subscribe: (id: number, input: SubscribeInput) => Effect.Effect } export const makeDaemonTransport = Effect.fn("PersistentPty.makeDaemonTransport")(function* ( @@ -174,6 +207,8 @@ export const makeDaemonTransport = Effect.fn("PersistentPty.makeDaemonTransport" let registration: Registration | undefined let owner: Awaited> | undefined let closed = false + // Set once a daemon we spawned turned out to speak another protocol. + let incompatible: DaemonError | undefined yield* Effect.addFinalizer(() => Effect.sync(() => { closed = true @@ -229,8 +264,7 @@ export const makeDaemonTransport = Effect.fn("PersistentPty.makeDaemonTransport" response.protocol !== ProtocolVersion ) return yield* Effect.fail(new DaemonError({ kind: "protocol", message: "opencode-pty registration mismatch" })) - // Daemons older than the features list omit it; trust the live daemon's answer over the file. - return { ...decoded, features: response.features ?? [] } + return decoded }) const spawnDaemon = Effect.fn("PersistentPty.daemon.spawn")(function* () { @@ -238,11 +272,11 @@ export const makeDaemonTransport = Effect.fn("PersistentPty.makeDaemonTransport" const child = yield* Effect.tryPromise({ try: () => new Promise>((resolve, reject) => { - const child = spawn( - executable, - ["daemon", "--runtime-dir", root, "--name", id], - { detached: true, windowsHide: true, stdio: "ignore" }, - ) + const child = spawn(executable, ["daemon", "--runtime-dir", root, "--name", id], { + detached: true, + windowsHide: true, + stdio: "ignore", + }) child.once("spawn", () => { child.unref() resolve(child) @@ -258,6 +292,11 @@ export const makeDaemonTransport = Effect.fn("PersistentPty.makeDaemonTransport" const found = yield* discover().pipe( Effect.map((value) => ({ value })), Effect.catch((error) => { + // The binary we just started speaks another protocol: waiting longer cannot help. + if (error.kind === "protocol" && error.pid !== undefined && error.pid === child.pid) { + incompatible = error + return Effect.fail(error) + } last = error return Effect.succeed(undefined) }), @@ -280,11 +319,13 @@ export const makeDaemonTransport = Effect.fn("PersistentPty.makeDaemonTransport" ) }) - // An unreachable daemon can still hold this directory's lock, so retry once in a fresh directory. + // An unreachable daemon, or one left by another release, can still hold this directory's lock, so retry once in a + // fresh directory. A binary that cannot be spawned, or that speaks another protocol, fails the same way again. const start = () => spawnDaemon().pipe( Effect.catch((error) => Effect.gen(function* () { + if (error.kind === "spawn" || error === incompatible) return yield* Effect.fail(error) yield* Effect.logWarning("Starting opencode-pty in a fresh runtime directory", { cause: error.message }) abandon() return yield* spawnDaemon() @@ -307,7 +348,8 @@ export const makeDaemonTransport = Effect.fn("PersistentPty.makeDaemonTransport" if (registration) return registration const found = yield* discover().pipe( Effect.catch((error) => { - if (shouldStart && error.kind === "connect") return start() + // Starting the same incompatible binary again would only fail again. + if (shouldStart && error.kind === "connect") return incompatible ? Effect.fail(incompatible) : start() return Effect.fail(error) }), ) @@ -351,9 +393,14 @@ export const makeDaemonTransport = Effect.fn("PersistentPty.makeDaemonTransport" Effect.catch((error) => (error.kind === "connect" ? Effect.succeed(undefined) : Effect.fail(error))), ) - const supports = Effect.fn("PersistentPty.daemon.supports")(function* (feature: string) { - const current = yield* connect(false) - return current.features?.includes(feature) ?? false + const wait = Effect.fn("PersistentPty.daemon.wait")(function* (processID: number) { + const attempt = withRegistration(false, (current) => + Effect.tryPromise({ + try: (signal) => waitPromise(current, processID, signal), + catch: (cause) => (cause instanceof DaemonError ? cause : failure("connect", cause)), + }), + ) + return yield* attempt.pipe(Effect.catch((error) => (error.kind === "registration" ? attempt : Effect.fail(error)))) }) const shutdown = Effect.gen(function* () { @@ -398,10 +445,7 @@ export const makeDaemonTransport = Effect.fn("PersistentPty.makeDaemonTransport" }), ) - const subscribe = Effect.fn("PersistentPty.daemon.subscribe")(function* ( - terminalID: number, - input: SubscribeInput, - ) { + const subscribe = Effect.fn("PersistentPty.daemon.subscribe")(function* (terminalID: number, input: SubscribeInput) { const attempt = withRegistration(false, (current) => Effect.tryPromise({ try: () => subscribePromise(current, terminalID, input), @@ -418,13 +462,10 @@ export const makeDaemonTransport = Effect.fn("PersistentPty.makeDaemonTransport" yield* Effect.gen(function* () { if (inherited.expiresAt <= Date.now()) return yield* Effect.fail(new DaemonError({ kind: "registration", message: "PTY restart handoff expired" })) - // Older releases put the daemon directory outside the root; its directory name is still the ID. id = path.basename(inherited.directory) found = yield* discover(inherited.directory) if (found.instance_id !== inherited.instanceID) return yield* Effect.fail(new DaemonError({ kind: "registration", message: "PTY restart daemon changed" })) - if (path.resolve(inherited.directory) !== path.resolve(root, id)) - yield* migrate(inherited.directory, path.join(root, id)) yield* claim(found, inherited.ticket) }).pipe( Effect.catchCause((cause) => @@ -440,20 +481,7 @@ export const makeDaemonTransport = Effect.fn("PersistentPty.makeDaemonTransport" ) } - return { request, requestIfRunning, supports, shutdown, handoff, subscribe } satisfies DaemonTransport -}) - -// Older releases kept registrations in purgeable temp directories. Renaming keeps the lock file's -// inode, so the still-running daemon keeps holding its lock in the new location. -const migrate = Effect.fn("PersistentPty.daemon.migrate")(function* (directory: string, target: string) { - yield* Effect.tryPromise({ - try: async () => { - await mkdir(target, { recursive: true, mode: 0o700 }) - await rename(path.join(directory, "service.lock"), path.join(target, "service.lock")) - await rename(path.join(directory, "service.json"), path.join(target, "service.json")) - }, - catch: (cause) => failure("registration", cause), - }) + return { request, requestIfRunning, wait, shutdown, handoff, subscribe } satisfies DaemonTransport }) async function openOwner(registration: Registration, ticket: string | undefined, signal: AbortSignal) { @@ -535,7 +563,7 @@ const oneShot = Effect.fn("PersistentPty.daemon.oneShot")(function* (registratio }) const response = yield* Effect.try({ try: () => decode(first), - catch: (cause) => failure("protocol", cause), + catch: (cause) => failure("response", cause), }) if (response.type === "error") return yield* Effect.fail(responseError(response.message)) return response @@ -632,7 +660,7 @@ async function subscribePromise( } let activated = false return { - terminal: response.terminal, + terminal: response.process, role: response.role, generation: response.generation, replay: { @@ -659,6 +687,29 @@ async function subscribePromise( } } +async function waitPromise(registration: Registration, id: number, signal: AbortSignal): Promise { + const socket = net.createConnection({ path: registration.socket }) + const abort = () => socket.destroy() + signal.addEventListener("abort", abort, { once: true }) + try { + await new Promise((resolve, reject) => { + socket.once("connect", resolve) + socket.once("error", reject) + }) + socket.write(encode({ token: registration.token, request: { op: "wait", id } })) + const frame = await decoder(socket).next() + // The connection was open, so a missing reply means the daemon went away while the process ran. + if (frame.done) throw failure("connect", "opencode-pty closed before the process exited") + const response = decode(frame.value) + if (response.type === "error") throw responseError(response.message) + if (response.type !== "exited") throw failure("response", `unexpected opencode-pty response: ${response.type}`) + return response + } finally { + signal.removeEventListener("abort", abort) + socket.destroy() + } +} + function encode(value: unknown) { const payload = Buffer.from(JSON.stringify(value)) if (payload.length > MaxFrameBytes) throw new Error("opencode-pty frame too large") @@ -691,7 +742,7 @@ function decode(payload: Uint8Array) { } function responseError(message: string) { - return new DaemonError({ kind: message === "authentication failed" ? "registration" : "protocol", message }) + return new DaemonError({ kind: message === "authentication failed" ? "registration" : "request", message }) } function failure(kind: DaemonError["kind"], cause: unknown) { diff --git a/packages/core/src/persistent-pty/index.ts b/packages/core/src/persistent-pty/index.ts index 1728996636d..99c99e4f3c7 100644 --- a/packages/core/src/persistent-pty/index.ts +++ b/packages/core/src/persistent-pty/index.ts @@ -22,15 +22,19 @@ import { ShellSelect } from "../shell/select.js" import { makeDaemonTransport, type DaemonAttachment, + type DaemonError, type DaemonTransport, + type Exited, + type Metadata, + type ProcessInfo, type Role, + type Signal, type SubscribeInput, type WireResponse, - type WireTerminal, } from "./daemon.js" import { resolveBinary } from "#persistent-pty-binary" -export type { Role, StreamEvent } from "./daemon.js" +export type { Metadata, Role, Signal, StreamEvent } from "./daemon.js" export { Handoff } from "@opencode/schema/persistent-pty" export { available } from "#persistent-pty-binary" @@ -54,22 +58,73 @@ export type Attachment = { readonly detach: () => void } +/** The daemon could not be reached or started; the request never ran. */ export class UnavailableError extends Schema.TaggedError()("PersistentPty.UnavailableError", { message: Schema.String, }) {} +/** The daemon rejected the request, or its reply was lost after it was sent, so it may have run. */ +export class RequestError extends Schema.TaggedError()("PersistentPty.RequestError", { + message: Schema.String, +}) {} + export class NotFoundError extends Schema.TaggedError()("PersistentPty.NotFoundError", { ptyID: Pty.ID, }) {} +export class ProcessNotFoundError extends Schema.TaggedError()( + "PersistentPty.ProcessNotFoundError", + { processID: Schema.Number }, +) {} + export class InvalidCwdError extends Schema.TaggedError()("PersistentPty.InvalidCwdError", { cwd: Schema.String, message: Schema.String, }) {} -export class UnsupportedError extends Schema.TaggedError()("PersistentPty.UnsupportedError", { - feature: Schema.String, -}) {} +/** A non-PTY process run by the daemon, its output written to `outputPath`. */ +export type PipeInfo = { + readonly id: number + readonly pid: number | undefined + readonly command: readonly string[] + readonly cwd: string + readonly groupID: string + readonly lifecycle: + | { readonly status: "running" } + | { readonly status: "exited"; readonly exitCode: number | undefined; readonly signal: string | undefined } + | { readonly status: "failed"; readonly message: string } + /** Bytes counted so far; the file can hold a little more. Pair only bytes up to here with `outputLines`. */ + readonly outputTail: number + readonly outputLines: number + readonly outputPath: string + readonly outputError: string | undefined + readonly metadata: Metadata + readonly startedAt: number + readonly exitedAt: number | undefined +} + +export type PipeExit = { + readonly exitCode: number | undefined + readonly signal: string | undefined + /** Runs past the file's size when `outputError` is set; use the file's real size instead. */ + readonly finalOffset: number + readonly outputLines: number + readonly outputError: string | undefined + readonly exitedAt: number +} + +export type CreatePipeInput = { + readonly program: string + readonly args: readonly string[] + /** Absolute. */ + readonly cwd: string + /** The child's whole environment; the daemon's own is not inherited. */ + readonly env: Readonly> + readonly groupID: string + /** Absolute; truncated on open, symlinks refused. */ + readonly outputPath: string + readonly metadata: Metadata +} export interface Interface { readonly list: (sessionID?: Session.ID) => Effect.Effect @@ -110,19 +165,27 @@ export interface Interface { rows: number, data: Uint8Array, ) => Effect.Effect - readonly rename: ( - id: Pty.ID, - title: string | null, - ) => Effect.Effect + readonly rename: (id: Pty.ID, title: string | null) => Effect.Effect readonly snapshot: (id: Pty.ID) => Effect.Effect readonly read: (sessionID: Session.ID, lines?: number) => Effect.Effect readonly remove: (id: Pty.ID) => Effect.Effect readonly shutdown: () => Effect.Effect readonly handoff: () => Effect.Effect - readonly attach: ( - id: Pty.ID, - input: SubscribeInput, - ) => Effect.Effect + readonly attach: (id: Pty.ID, input: SubscribeInput) => Effect.Effect + /** Starts the daemon if needed. Fall back to another spawner only on `UnavailableError`. */ + readonly createPipe: (input: CreatePipeInput) => Effect.Effect + /** Resolves when the process exits, on its own connection with no timeout; interrupting closes it. */ + readonly waitPipe: (id: number) => Effect.Effect + /** `ProcessNotFoundError` for an unknown id, a PTY, or when no daemon is running. */ + readonly getPipe: (id: number) => Effect.Effect + /** Signals the process group without forgetting the process; `waitPipe` then reports the signal. */ + readonly signalPipe: (id: number, signal: Signal) => Effect.Effect + /** Replaces the stored metadata. */ + readonly setPipeMetadata: (id: number, metadata: Metadata) => Effect.Effect + /** Kills the process if it still runs and forgets it; the output file is left alone. */ + readonly terminatePipe: (id: number) => Effect.Effect + /** Never starts the daemon; empty when none is running. */ + readonly listPipes: () => Effect.Effect } export class Service extends Context.Service()("@opencode/PersistentPty") {} @@ -151,18 +214,21 @@ const makeLayer = (options: Options = {}) => const current = new Map() const list = Effect.fn("PersistentPty.list")(function* (sessionID?: Session.ID) { - const response = yield* optionalRequest(daemon, { op: "list" }) + const response = yield* optionalRequest(daemon, { op: "list", mode: "pty" }) if (!response) return [] - if (response.type !== "terminals") return yield* unexpected(response) - return response.terminals + if (response.type !== "processes") return yield* unexpected(response) + return response.processes .map(toInfo) .filter((terminal) => sessionID === undefined || terminal.sessionID === sessionID) }) const get = Effect.fn("PersistentPty.get")(function* (id: Pty.ID) { - const found = (yield* list()).find((terminal) => terminal.id === id) - if (!found) return yield* new NotFoundError({ ptyID: id }) - return found + // Any ID that is not one of ours names no terminal; every other method looks the terminal up here first. + const parsed = parseID(id) + const found = + parsed === undefined ? undefined : yield* lookup(daemon, parsed).pipe(Effect.mapError(unavailable)) + if (found?.mode !== "pty") return yield* new NotFoundError({ ptyID: id }) + return toInfo(found) }) const create = Effect.fn("PersistentPty.create")(function* ( @@ -181,7 +247,7 @@ const makeLayer = (options: Options = {}) => const response = yield* request( daemon, { - op: "create", + op: "create_pty", program: input.command ?? ShellSelect.environment(global.bin), args: input.args, cwd, @@ -194,7 +260,7 @@ const makeLayer = (options: Options = {}) => true, ) if (response.type !== "created") return yield* unexpected(response) - const terminal = toInfo(response.terminal) + const terminal = toInfo(response.process) yield* bus.publish(Added, { sessionID, terminal }) return terminal }) @@ -217,8 +283,9 @@ const makeLayer = (options: Options = {}) => ), ) - const resize = Effect.fn("PersistentPty.resize")((id: Pty.ID, cols: number, rows: number, attachmentID?: string) => - command(id, "resize", { attachment_id: attachmentID ?? null, cols, rows }, true), + const resize = Effect.fn("PersistentPty.resize")( + (id: Pty.ID, cols: number, rows: number, attachmentID?: string) => + command(id, "resize", { attachment_id: attachmentID ?? null, cols, rows }, true), ) const control = Effect.fn("PersistentPty.control")( @@ -238,9 +305,6 @@ const makeLayer = (options: Options = {}) => const rename = Effect.fn("PersistentPty.rename")(function* (id: Pty.ID, title: string | null) { const terminal = yield* get(id) - // Daemons without the feature drop the connection on unknown requests. - if (!(yield* daemon.supports("rename").pipe(Effect.mapError(unavailable)))) - return yield* new UnsupportedError({ feature: "rename" }) const response = yield* request(daemon, { op: "rename", id: fromID(id), title }) if (response.type !== "ok") return yield* unexpected(response) const renamed = yield* get(id) @@ -253,7 +317,7 @@ const makeLayer = (options: Options = {}) => const response = yield* request(daemon, { op: "snapshot", id: fromID(id) }) if (response.type !== "snapshot") return yield* unexpected(response) return { - info: toInfo(response.terminal), + info: toInfo(response.process), text: response.text, checkpoint: Buffer.from(response.checkpoint_base64, "base64"), cursor: { x: response.cursor_x, y: response.cursor_y }, @@ -273,7 +337,7 @@ const makeLayer = (options: Options = {}) => // Let the daemon choose the live height in the same snapshot when lines is omitted. const response = yield* request(daemon, { op: "read_rows", id: fromID(id), rows: lines }) if (response.type !== "rows") return yield* unexpected(response) - const info = toInfo(response.terminal) + const info = toInfo(response.process) return { ptyID: info.id, title: info.title, @@ -367,9 +431,10 @@ const makeLayer = (options: Options = {}) => }, }) .pipe(Effect.mapError(unavailable)) - if (attachment.role === "controller") current.set(attachment.terminal.group_id, id) + const info = toInfo(attachment.terminal) + if (attachment.role === "controller") current.set(info.sessionID, id) return { - info: toInfo(attachment.terminal), + info, role: attachment.role, generation: attachment.generation, replay: attachment.replay, @@ -378,6 +443,61 @@ const makeLayer = (options: Options = {}) => } }) + const createPipe = Effect.fn("PersistentPty.createPipe")(function* (input: CreatePipeInput) { + const response = yield* daemon + .request( + { + op: "create_pipe", + program: input.program, + args: input.args, + cwd: input.cwd, + env: input.env, + group_id: input.groupID, + output_path: input.outputPath, + metadata: input.metadata, + }, + true, + ) + .pipe(Effect.mapError(pipeError)) + if (response.type !== "created") return yield* unexpectedPipe(response) + return toPipeInfo(response.process) + }) + + const waitPipe = Effect.fn("PersistentPty.waitPipe")(function* (id: number) { + return toPipeExit(yield* daemon.wait(id).pipe(Effect.mapError(pipeError))) + }) + + const getPipe = Effect.fn("PersistentPty.getPipe")(function* (id: number) { + const found = yield* lookup(daemon, id).pipe(Effect.mapError(unavailable)) + if (found?.mode !== "pipe") return yield* new ProcessNotFoundError({ processID: id }) + return toPipeInfo(found) + }) + + const pipeCommand = (value: object) => + Effect.gen(function* () { + const response = yield* daemon.request(value).pipe(Effect.mapError(pipeError)) + if (response.type !== "ok") return yield* unexpectedPipe(response) + }) + + const signalPipe = Effect.fn("PersistentPty.signalPipe")((id: number, signal: Signal) => + pipeCommand({ op: "signal", id, signal }), + ) + + const setPipeMetadata = Effect.fn("PersistentPty.setPipeMetadata")((id: number, metadata: Metadata) => + pipeCommand({ op: "set_metadata", id, metadata }), + ) + + const terminatePipe = Effect.fn("PersistentPty.terminatePipe")((id: number) => + pipeCommand({ op: "terminate", id }), + ) + + const listPipes = Effect.fn("PersistentPty.listPipes")(function* () { + const response = yield* daemon.requestIfRunning({ op: "list", mode: "pipe" }).pipe(Effect.mapError(pipeError)) + if (!response) return [] + if (response.type !== "processes") return yield* unexpectedPipe(response) + return response.processes.map(toPipeInfo) + }) + return Service.of({ list, get, @@ -393,11 +513,19 @@ const makeLayer = (options: Options = {}) => shutdown, handoff, attach, + createPipe, + waitPipe, + getPipe, + signalPipe, + setPipeMetadata, + terminatePipe, + listPipes, }) }), ) export const layer = makeLayer() +export const layerWith = (options: Options) => makeLayer(options) export const configured = (options?: Options) => makeGlobalNode({ service: Service, @@ -412,40 +540,108 @@ const request = (daemon: DaemonTransport, value: object, start = false) => const optionalRequest = (daemon: DaemonTransport, value: object) => daemon.requestIfRunning(value).pipe(Effect.mapError(unavailable)) +// `get` fails only for an unknown id, and no running daemon has no processes. +const lookup = (daemon: DaemonTransport, id: number) => + daemon.requestIfRunning({ op: "get", id }).pipe( + Effect.catchIf( + (error) => error.kind === "request", + () => Effect.succeed(undefined), + ), + Effect.flatMap((response) => { + if (!response) return Effect.succeed(undefined) + if (response.type !== "process") + return Effect.fail(new Error(`unexpected opencode-pty response: ${response.type}`)) + return Effect.succeed(response.process) + }), + ) + const unexpected = (response: WireResponse) => Effect.fail(new UnavailableError({ message: `unexpected opencode-pty response: ${response.type}` })) const unavailable = (error: unknown) => new UnavailableError({ message: error instanceof Error ? error.message : String(error) }) -function toInfo(value: WireTerminal): Info { +// Only a failure before the request was sent is safe to retry somewhere else. +const pipeError = (error: DaemonError) => + error.kind === "request" || error.kind === "response" + ? new RequestError({ message: error.message }) + : new UnavailableError({ message: error.message }) + +const unexpectedPipe = (response: WireResponse) => + Effect.fail(new RequestError({ message: `unexpected opencode-pty response: ${response.type}` })) + +function toInfo(value: ProcessInfo): Info { + if (!value.pty) throw new Error(`opencode-pty process ${value.id} is not a terminal`) const status = value.lifecycle.status return { ...Pty.Info.make({ id: toID(value.id), - title: value.title, + title: value.pty.title, command: value.command[0] || "", args: value.command.slice(1), cwd: value.cwd, status: status === "running" ? "running" : "exited", pid: value.pid ?? 0, - ...(status === "exited" ? { exitCode: value.lifecycle.exit_code ?? undefined } : {}), + ...(value.lifecycle.status === "exited" ? { exitCode: value.lifecycle.exit_code ?? undefined } : {}), }), - sessionID: value.group_id, - userTitle: value.user_title ?? null, - foregroundProcess: value.foreground_process, - size: { cols: value.cols, rows: value.rows }, + // Terminals are always created with a session ID as their group. + sessionID: Session.ID.make(value.group_id), + userTitle: value.pty.user_title, + foregroundProcess: value.pty.foreground_process, + size: { cols: value.pty.cols, rows: value.pty.rows }, output: { head: value.output_head, tail: value.output_tail }, } } +function toPipeInfo(value: ProcessInfo): PipeInfo { + if (!value.pipe) throw new Error(`opencode-pty process ${value.id} is not a pipe process`) + return { + id: value.id, + pid: value.pid ?? undefined, + command: value.command, + cwd: value.cwd, + groupID: value.group_id, + lifecycle: + value.lifecycle.status === "exited" + ? { + status: "exited", + exitCode: value.lifecycle.exit_code ?? undefined, + signal: value.lifecycle.signal ?? undefined, + } + : value.lifecycle, + outputTail: value.output_tail, + outputLines: value.pipe.output_lines, + outputPath: value.pipe.output_path, + outputError: value.pipe.output_error ?? undefined, + metadata: value.pipe.metadata, + startedAt: value.started_at, + exitedAt: value.exited_at ?? undefined, + } +} + +function toPipeExit(value: Exited): PipeExit { + return { + exitCode: value.exit_code ?? undefined, + signal: value.signal ?? undefined, + finalOffset: value.final_offset, + outputLines: value.output_lines ?? 0, + outputError: value.output_error ?? undefined, + exitedAt: value.exited_at, + } +} + function toID(value: number) { return Pty.ID.make(`pty_persistent_${value}`) } -function fromID(value: Pty.ID) { - if (!value.startsWith("pty_persistent_")) throw new Error(`invalid persistent PTY ID: ${value}`) +function parseID(value: Pty.ID) { + if (!value.startsWith("pty_persistent_")) return undefined const parsed = Number(value.slice("pty_persistent_".length)) - if (!Number.isSafeInteger(parsed) || parsed < 1) throw new Error(`invalid persistent PTY ID: ${value}`) + return Number.isSafeInteger(parsed) && parsed >= 1 ? parsed : undefined +} + +function fromID(value: Pty.ID) { + const parsed = parseID(value) + if (parsed === undefined) throw new Error(`invalid persistent PTY ID: ${value}`) return parsed } diff --git a/packages/core/test/fixture/persistent-pty-daemon.ts b/packages/core/test/fixture/persistent-pty-daemon.ts index c4a5e57794d..38413c9e4a1 100644 --- a/packages/core/test/fixture/persistent-pty-daemon.ts +++ b/packages/core/test/fixture/persistent-pty-daemon.ts @@ -1,10 +1,11 @@ +import { appendFileSync, writeFileSync } from "node:fs" import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises" import net from "node:net" import os from "node:os" import path from "node:path" import { Effect } from "effect" -export const pong = { type: "pong", instance_id: "test", pid: process.pid, protocol: 7 } +export const pong = { type: "pong", instance_id: "test", pid: process.pid, protocol: 8, features: [] } export function temporaryDirectory() { return Effect.acquireRelease( @@ -22,14 +23,20 @@ export function listen( const sockets = new Set() const server = net.createServer((socket) => { sockets.add(socket) + // A client may close its end (an interrupted wait) just before a reply is written. + socket.on("error", () => {}) socket.once("close", () => sockets.delete(socket)) - void readRequest(socket) - .then((envelope) => { - const response = - envelope.request.op === "own" ? { type: "owned" } : handle(socket, envelope.request, envelope.token) + // The ownership connection stays open for later requests, such as handoff preparation, like the real daemon's. + let owner = false + const serve = (): Promise => + readRequest(socket).then((envelope) => { + const owning = envelope.request.op === "own" + if (owning) owner = true + const response = owning ? { type: "owned" } : handle(socket, envelope.request, envelope.token) if (response) socket.write(frame(response)) + if (owner) return serve() }) - .catch(() => socket.destroy()) + void serve().catch(() => socket.destroy()) }) await new Promise((resolve, reject) => { server.once("error", reject) @@ -81,7 +88,7 @@ function readRequest(socket: net.Socket) { }) } -function frame(value: object) { +export function frame(value: object) { const payload = Buffer.from(JSON.stringify(value)) const output = Buffer.allocUnsafe(payload.length + 4) output.writeUInt32BE(payload.length) @@ -89,70 +96,164 @@ function frame(value: object) { return output } -export function writeRegistration( - directory: string, - socket: string, - instance = "test", - token = "test", - features?: string[], -) { +export function writeRegistration(directory: string, socket: string, instance = "test", token = "test") { return writeFile( path.join(directory, "service.json"), - JSON.stringify({ instance_id: instance, pid: process.pid, protocol: 7, socket, token, features }), + JSON.stringify({ instance_id: instance, pid: process.pid, protocol: 8, socket, token, features: [] }), ) } -// An in-process opencode-pty that a server adopts through a handoff, so no daemon is spawned. Without -// features it answers like a daemon that predates the features list. -export const fakeDaemon = Effect.fn(function* (features?: string[]) { +type Process = { + id: number + mode: "pty" | "pipe" + pid: number | null + command: string[] + cwd: string + group_id: string + lifecycle: { status: "running" | "exited"; exit_code?: number | null; signal?: string } + output_head: number + output_tail: number + started_at: number + exited_at: number | null + pty?: { title: string; user_title: string | null; foreground_process: string | null; cols: number; rows: number } + pipe?: { output_path: string; output_lines: number; output_error?: string; metadata: Record } +} + +// An in-process opencode-pty that a server adopts through a handoff, so no daemon is spawned. Pipe processes +// run nothing: they stay running until `exit` or a signal ends them (SIGTERM spares those in `trap`), and +// write only what `output` gives them. +export const fakeDaemon = Effect.fn(function* () { const base = yield* temporaryDirectory() const root = path.join(base, "root") const directory = path.join(root, "inherited") const socket = path.join(base, "daemon.sock") - const terminals: Array & { user_title?: string | null }> = [] + const processes: Process[] = [] + const waiters = new Map() const failTerminate = new Set() + const trap = new Set() + const failCreate = { message: undefined as string | undefined } const ops: string[] = [] + const requests: Record[] = [] let nextID = 1 + + const find = (id: unknown) => processes.find((item) => item.id === id) + const exit = (id: number, exitCode: number | null, signal?: string) => { + const found = find(id) + if (!found || found.lifecycle.status !== "running") return + found.lifecycle = { status: "exited", exit_code: exitCode, signal } + found.exited_at = Date.now() + const reply = exited(found) + waiters.get(id)?.forEach((connection) => connection.end(frame(reply))) + waiters.delete(id) + } + + // Appends to the output file and counts it, as the daemon's reader does; after `fail`, the file stops growing. + const output = (id: number, text: string) => { + const found = find(id) + if (!found?.pipe) return + if (found.pipe.output_error === undefined) { + appendFileSync(found.pipe.output_path, text) + found.pipe.output_lines += text.split("\n").length - 1 + } + found.output_tail += Buffer.byteLength(text) + } + // The daemon going away under a waiting server: the wait connection closes without a reply. + const drop = (id: number) => { + waiters.get(id)?.forEach((connection) => connection.destroy()) + waiters.delete(id) + } + const fail = (id: number, message: string) => { + const found = find(id) + if (found?.pipe) found.pipe.output_error = message + } + yield* listen(socket, (connection, request) => { ops.push(String(request.op)) - if (request.op === "ping") return features ? { ...pong, features } : pong - if (request.op === "list") return { type: "terminals", terminals } - if (request.op === "create") { - const base = terminal(nextID++, String(request.group_id)) - // A daemon that advertises features always serializes user_title. - const created = features ? { ...base, user_title: null } : base - terminals.push(created) - return { type: "created", terminal: created } + requests.push(request) + if (request.op === "ping") return pong + if (request.op === "list") + return { type: "processes", processes: processes.filter((item) => item.mode === request.mode) } + if (request.op === "get") { + const found = find(request.id) + if (!found) return { type: "error", message: `terminal ${String(request.id)} not found` } + return { type: "process", process: found } + } + if (request.op === "create_pty") { + const created = terminal(nextID++, String(request.group_id)) + processes.push(created) + return { type: "created", process: created } + } + if (request.op === "create_pipe") { + if (typeof request.cwd !== "string" || !path.isAbsolute(request.cwd)) + return { type: "error", message: "cwd must be absolute" } + if (failCreate.message !== undefined) return { type: "error", message: failCreate.message } + const created = pipe(nextID++, request) + writeFileSync(String(request.output_path), "") + processes.push(created) + return { type: "created", process: created } } if (request.op === "control") return { type: "ok" } - if (request.op === "terminate") { - if (failTerminate.has(`pty_persistent_${request.id}`)) return { type: "error", message: "terminate failed" } - const index = terminals.findIndex((item) => item.id === request.id) - if (index === -1) return { type: "error", message: `terminal not found: ${String(request.id)}` } - terminals.splice(index, 1) - return { type: "ok" } - } - if (request.op === "rename" && features?.includes("rename")) { - const found = terminals.find((item) => item.id === request.id) - if (!found) return { type: "error", message: "terminal not found" } - found.user_title = typeof request.title === "string" && request.title !== "" ? request.title : null - found.title = found.user_title ?? "test" - return { type: "ok" } - } - // Old daemons cannot parse unknown requests and drop the connection. + if (request.op === "prepare_handoff") return { type: "handoff", ticket: "next", expires_at: Date.now() + 60_000 } if (request.op === "rename") { - connection.destroy() + const found = find(request.id) + if (!found?.pty) return { type: "error", message: "terminal not found" } + found.pty.user_title = typeof request.title === "string" && request.title !== "" ? request.title : null + found.pty.title = found.pty.user_title ?? "test" + return { type: "ok" } + } + if (request.op === "wait") { + const found = find(request.id) + if (!found?.pipe) return { type: "error", message: `terminal ${String(request.id)} not found` } + if (found.lifecycle.status !== "running") return exited(found) + waiters.set(found.id, [...(waiters.get(found.id) ?? []), connection]) + connection.once("close", () => + waiters.set( + found.id, + (waiters.get(found.id) ?? []).filter((waiter) => waiter !== connection), + ), + ) return undefined } - return { type: "error", message: `unexpected request: ${String(request.op)}` } + if (request.op === "signal") { + const found = find(request.id) + if (!found?.pipe) return { type: "error", message: `terminal ${String(request.id)} not found` } + if (request.signal !== "SIGTERM" || !trap.has(found.id)) exit(found.id, null, String(request.signal)) + return { type: "ok" } + } + if (request.op === "set_metadata") { + const found = find(request.id) + if (!found?.pipe || !isRecord(request.metadata)) + return { type: "error", message: `terminal ${String(request.id)} not found` } + found.pipe.metadata = request.metadata + return { type: "ok" } + } + if (request.op === "terminate") { + if (failTerminate.has(`pty_persistent_${request.id}`)) return { type: "error", message: "terminate failed" } + const index = processes.findIndex((item) => item.id === request.id) + if (index === -1) return { type: "error", message: `terminal ${String(request.id)} not found` } + const [removed] = processes.splice(index, 1) + waiters.get(removed.id)?.forEach((waiter) => waiter.end(frame({ type: "error", message: "terminated" }))) + waiters.delete(removed.id) + return { type: "ok" } + } + return { type: "error", message: `unsupported request: ${String(request.op)}` } }) yield* Effect.promise(async () => { await mkdir(directory, { recursive: true }) - await writeRegistration(directory, socket, "test", "test", features) + await writeRegistration(directory, socket) }) return { failTerminate, + failCreate, + trap, ops, + requests, + exit, + output, + fail, + drop, + processes, + waiters: (id: number) => waiters.get(id)?.length ?? 0, options: { root, handoff: { directory, instanceID: "test", ticket: "ticket", expiresAt: Date.now() + 60_000 }, @@ -160,20 +261,53 @@ export const fakeDaemon = Effect.fn(function* (features?: string[]) { } }) -export function terminal(id: number, groupID = "ses_test") { +export function terminal(id: number, groupID = "ses_test"): Process { return { id, + mode: "pty", pid: null, - title: "test", - foreground_process: null, - group_id: groupID, command: ["test"], cwd: "/tmp", - cols: 80, - rows: 24, + group_id: groupID, lifecycle: { status: "running" }, output_head: 0, output_tail: 0, + started_at: Date.now(), + exited_at: null, + pty: { title: "test", user_title: null, foreground_process: null, cols: 80, rows: 24 }, + } +} + +function pipe(id: number, request: Record): Process { + return { + id, + mode: "pipe", + pid: 1_000 + id, + command: [String(request.program), ...(Array.isArray(request.args) ? request.args.map(String) : [])], + cwd: String(request.cwd), + group_id: String(request.group_id), + lifecycle: { status: "running" }, + output_head: 0, + output_tail: 0, + started_at: Date.now(), + exited_at: null, + pipe: { + output_path: String(request.output_path), + output_lines: 0, + metadata: isRecord(request.metadata) ? request.metadata : {}, + }, + } +} + +function exited(process: Process) { + return { + type: "exited", + exit_code: process.lifecycle.exit_code ?? null, + signal: process.lifecycle.signal, + final_offset: process.output_tail, + output_lines: process.pipe?.output_lines, + output_error: process.pipe?.output_error, + exited_at: process.exited_at ?? Date.now(), } } diff --git a/packages/core/test/persistent-pty-daemon.test.ts b/packages/core/test/persistent-pty-daemon.test.ts index 70eb640737c..b3f6aae4583 100644 --- a/packages/core/test/persistent-pty-daemon.test.ts +++ b/packages/core/test/persistent-pty-daemon.test.ts @@ -1,11 +1,11 @@ import { expect } from "bun:test" import { spawn } from "node:child_process" -import { existsSync } from "node:fs" -import { mkdir, stat, writeFile } from "node:fs/promises" +import { mkdir, writeFile } from "node:fs/promises" +import net from "node:net" import path from "node:path" -import { Effect } from "effect" +import { Effect, Fiber } from "effect" import { makeDaemonTransport } from "../src/persistent-pty/daemon" -import { listen, pong, temporaryDirectory, terminal, writeRegistration } from "./fixture/persistent-pty-daemon" +import { frame, listen, pong, temporaryDirectory, terminal, writeRegistration } from "./fixture/persistent-pty-daemon" import { it } from "./lib/effect" it.live("rediscovers a same-protocol daemon after its registration rotates", () => @@ -19,7 +19,7 @@ it.live("rediscovers a same-protocol daemon after its registration rotates", () if (receivedToken !== token) return { type: "error", message: "authentication failed" } if (request.op === "ping") return { ...pong, instance_id: instance } if (request.op === "create") creates++ - return request.op === "list" ? { type: "terminals", terminals: [] } : { type: "ok" } + return request.op === "list" ? { type: "processes", processes: [] } : { type: "ok" } }) yield* Effect.promise(() => writeRegistration(directory, socketPath, instance, token)) const daemon = yield* makeDaemonTransport(root, "daemon") @@ -30,7 +30,7 @@ it.live("rediscovers a same-protocol daemon after its registration rotates", () yield* Effect.promise(() => writeRegistration(directory, socketPath, instance, token)) const running = yield* daemon.requestIfRunning({ op: "list" }) - expect(running).toEqual({ type: "terminals", terminals: [] }) + expect(running).toEqual({ type: "processes", processes: [] }) token = "newest-token" instance = "newest-instance" @@ -50,11 +50,11 @@ it.live("rediscovers a rotated registration when acquiring a subscription", () = yield* listen(socketPath, (_socket, request, receivedToken) => { if (receivedToken !== token) return { type: "error", message: "authentication failed" } if (request.op === "ping") return { ...pong, instance_id: instance } - if (request.op !== "subscribe") return { type: "terminals", terminals: [] } + if (request.op !== "subscribe") return { type: "processes", processes: [] } subscriptions++ return { type: "attached", - terminal: terminal(1), + process: terminal(1), role: "observer", generation: 1, requested_offset: 0, @@ -85,30 +85,75 @@ it.live("rediscovers a rotated registration when acquiring a subscription", () = }), ) -it.live("refreshes the daemon's features when its registration rotates", () => +it.live("waits on its own connection until the process exits", () => Effect.gen(function* () { const { root, directory } = yield* temporaryRuntime() const socketPath = path.join(directory, "daemon.sock") - let token = "old-token" - let instance = "old-instance" - let features: string[] | undefined - yield* listen(socketPath, (_socket, request, receivedToken) => { - if (receivedToken !== token) return { type: "error", message: "authentication failed" } - if (request.op === "ping") return { ...pong, instance_id: instance, ...(features ? { features } : {}) } - return { type: "terminals", terminals: [] } + const waiting: net.Socket[] = [] + yield* listen(socketPath, (socket, request) => { + if (request.op === "ping") return pong + if (request.op === "wait" && request.id === 1) { + waiting.push(socket) + return undefined + } + return { type: "error", message: "terminal 2 not found" } }) - yield* Effect.promise(() => writeRegistration(directory, socketPath, instance, token)) + yield* Effect.promise(() => writeRegistration(directory, socketPath)) const daemon = yield* makeDaemonTransport(root, "daemon") - expect(yield* daemon.supports("rename")).toBeFalse() - token = "new-token" - instance = "new-instance" - features = ["rename"] - yield* Effect.promise(() => writeRegistration(directory, socketPath, instance, token, features)) - // The cached registration is replaced once a request is rejected, as rename's lookup does first. - yield* daemon.request({ op: "list" }) + const missing = yield* Effect.flip(daemon.wait(2)) + expect(missing).toMatchObject({ kind: "request", message: "terminal 2 not found" }) - expect(yield* daemon.supports("rename")).toBeTrue() + const fiber = yield* daemon.wait(1).pipe(Effect.forkScoped({ startImmediately: true })) + yield* poll(() => waiting.length === 1) + waiting[0].end( + frame({ type: "exited", exit_code: null, signal: "SIGTERM", final_offset: 3, output_lines: 1, exited_at: 7 }), + ) + expect(yield* Fiber.join(fiber)).toEqual({ + type: "exited", + exit_code: null, + signal: "SIGTERM", + final_offset: 3, + output_lines: 1, + exited_at: 7, + }) + }), +) + +it.live("closes the wait connection when the wait is interrupted", () => + Effect.gen(function* () { + const { root, directory } = yield* temporaryRuntime() + const socketPath = path.join(directory, "daemon.sock") + const waiting: net.Socket[] = [] + yield* listen(socketPath, (socket, request) => { + if (request.op === "ping") return pong + waiting.push(socket) + return undefined + }) + yield* Effect.promise(() => writeRegistration(directory, socketPath)) + const daemon = yield* makeDaemonTransport(root, "daemon") + + const fiber = yield* daemon.wait(1).pipe(Effect.forkScoped({ startImmediately: true })) + yield* poll(() => waiting.length === 1) + const closed = new Promise((resolve) => waiting[0].once("close", () => resolve())) + yield* Fiber.interrupt(fiber) + yield* Effect.promise(() => closed) + }), +) + +it.live("reports a daemon that goes away during a wait as unreachable", () => + Effect.gen(function* () { + const { root, directory } = yield* temporaryRuntime() + const socketPath = path.join(directory, "daemon.sock") + yield* listen(socketPath, (socket, request) => { + if (request.op === "ping") return pong + socket.destroy() + return undefined + }) + yield* Effect.promise(() => writeRegistration(directory, socketPath)) + const daemon = yield* makeDaemonTransport(root, "daemon") + + expect((yield* Effect.flip(daemon.wait(1))).kind).toBe("connect") }), ) @@ -119,7 +164,7 @@ it.live("retries a start-required request when connection fails before dispatch" const secondSocket = path.join(directory, "second.sock") const first = yield* listen(firstSocket, (_socket, request) => { if (request.op === "ping") return pong - return { type: "terminals", terminals: [] } + return { type: "processes", processes: [] } }) yield* Effect.promise(() => writeRegistration(directory, firstSocket)) const daemon = yield* makeDaemonTransport(root, "daemon") @@ -175,7 +220,14 @@ it.live("rejects incompatible daemons without replacing or killing them", () => yield* Effect.promise(() => writeFile( path.join(directory, "service.json"), - JSON.stringify({ instance_id: "old", pid: existing.pid, protocol: 6, socket: "/unused", token: "old" }), + JSON.stringify({ + instance_id: "old", + pid: existing.pid, + protocol: 7, + socket: "/unused", + token: "old", + features: [], + }), ), ) const daemon = yield* makeDaemonTransport(root, "daemon", () => Promise.resolve("/missing/opencode-pty")) @@ -183,7 +235,7 @@ it.live("rejects incompatible daemons without replacing or killing them", () => const optional = yield* Effect.flip(daemon.requestIfRunning({ op: "list" })) expect(optional).toMatchObject({ kind: "protocol", - message: "opencode-pty protocol mismatch: daemon=6, client=7", + message: "opencode-pty protocol mismatch: daemon=7, client=8", pid: existing.pid, }) @@ -195,36 +247,6 @@ it.live("rejects incompatible daemons without replacing or killing them", () => }), ) -it.live("moves an inherited legacy registration into the runtime root and keeps its lock file", () => - Effect.gen(function* () { - const base = yield* temporaryDirectory() - const legacy = path.join(base, "legacy", "inherited") - const socketPath = path.join(base, "daemon.sock") - yield* listen(socketPath, (_socket, request) => { - if (request.op === "ping") return pong - return { type: "terminals", terminals: [] } - }) - yield* Effect.promise(async () => { - await mkdir(legacy, { recursive: true }) - await writeFile(path.join(legacy, "service.lock"), "") - await writeRegistration(legacy, socketPath) - }) - const lock = yield* Effect.promise(() => stat(path.join(legacy, "service.lock"))) - const daemon = yield* makeDaemonTransport(path.join(base, "root"), "fresh", undefined, { - directory: legacy, - instanceID: "test", - ticket: "ticket", - expiresAt: Date.now() + 60_000, - }) - - const migrated = path.join(base, "root", "inherited") - expect(existsSync(path.join(legacy, "service.json"))).toBeFalse() - expect((yield* Effect.promise(() => stat(path.join(migrated, "service.lock")))).ino).toBe(lock.ino) - expect(existsSync(path.join(migrated, "service.json"))).toBeTrue() - expect(yield* daemon.request({ op: "list" })).toEqual({ type: "terminals", terminals: [] }) - }), -) - it.live("starts fresh instead of failing when an inherited registration is gone or expired", () => Effect.gen(function* () { const base = yield* temporaryDirectory() @@ -264,6 +286,13 @@ it.live("stops a discovered inherited daemon when adoption fails", () => }), ) +function poll(ready: () => boolean) { + return Effect.gen(function* () { + for (let attempt = 0; attempt < 200 && !ready(); attempt++) yield* Effect.sleep(10) + expect(ready()).toBeTrue() + }) +} + function temporaryRuntime() { return Effect.gen(function* () { const root = yield* temporaryDirectory() @@ -272,3 +301,37 @@ function temporaryRuntime() { return { root, directory } }) } + +if (process.platform !== "win32") + it.live("gives up at once on a daemon binary that speaks another protocol, and never starts it again", () => + Effect.gen(function* () { + const root = yield* temporaryDirectory() + const runtime = path.join(root, "runtime") + const spawns = path.join(root, "spawns") + const binary = path.join(root, "old-opencode-pty") + // Registers like an older release would, then idles. + yield* Effect.promise(() => + writeFile( + binary, + [ + "#!/bin/sh", + `echo spawn >> ${JSON.stringify(spawns)}`, + 'mkdir -p "$3/$5"', + `printf '{"instance_id":"old","pid":%s,"protocol":7,"socket":"none","token":"t","features":[]}' $$ > "$3/$5/service.json"`, + "exec sleep 30", + ].join("\n"), + { mode: 0o755 }, + ), + ) + const daemon = yield* makeDaemonTransport(runtime, "daemon", () => Promise.resolve(binary)) + const started = Date.now() + const first = yield* daemon.request({ op: "list", mode: "pipe" }, true).pipe(Effect.flip) + expect(first.kind).toBe("protocol") + const second = yield* daemon.request({ op: "list", mode: "pipe" }, true).pipe(Effect.flip) + expect(second.kind).toBe("protocol") + // Well under the 5 s spawn timeout: the daemon was not waited on again. + expect(Date.now() - started).toBeLessThan(4_000) + const count = yield* Effect.promise(() => Bun.file(spawns).text()) + expect(count.trim().split("\n")).toHaveLength(1) + }), + ) diff --git a/packages/core/test/persistent-pty-session.test.ts b/packages/core/test/persistent-pty-session.test.ts index e154ebe85b5..d199807f8c5 100644 --- a/packages/core/test/persistent-pty-session.test.ts +++ b/packages/core/test/persistent-pty-session.test.ts @@ -17,6 +17,7 @@ import { SessionStore } from "@opencode/core/session/store" import { SessionEnvironment } from "@opencode/core/session/environment" import { LocationServiceMap } from "@opencode/core/location-services" import { Removed, Updated } from "@opencode/schema/persistent-pty" +import { Pty } from "@opencode/schema/pty" import { fakeDaemon } from "./fixture/persistent-pty-daemon" import { it } from "./lib/effect" import { globalProjectNode } from "./lib/project" @@ -89,7 +90,7 @@ describe("PersistentPty session cleanup", () => { describe("PersistentPty rename", () => { it.live("renames a terminal, publishes the update and clears the user title", () => Effect.gen(function* () { - const daemon = yield* fakeDaemon(["rename"]) + const daemon = yield* fakeDaemon() yield* Effect.gen(function* () { const pty = yield* PersistentPty.Service const bus = yield* Bus.Service @@ -107,31 +108,116 @@ describe("PersistentPty rename", () => { }).pipe(Effect.provide(layer(daemon))) }), ) +}) - it.live("refuses to rename on a daemon without the rename feature and never sends it", () => +// 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" }, + } + + it.live("creates, inspects, signals and waits for a pipe process", () => + Effect.gen(function* () { + const daemon = yield* fakeDaemon() + yield* Effect.gen(function* () { + const pty = yield* PersistentPty.Service + 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" }, + group_id: "proj_test", + output_path: "/tmp/sh_test.out", + metadata: { id: "sh_test", command: "echo hi" }, + }) + expect(created).toMatchObject({ + groupID: "proj_test", + outputPath: "/tmp/sh_test.out", + lifecycle: { status: "running" }, + metadata: input.metadata, + exitedAt: undefined, + }) + + yield* pty.setPipeMetadata(created.id, { id: "sh_test", status: "killed" }) + expect((yield* pty.getPipe(created.id)).metadata).toEqual({ id: "sh_test", status: "killed" }) + expect((yield* pty.listPipes()).map((item) => item.id)).toEqual([created.id]) + + const waiting = yield* pty.waitPipe(created.id).pipe(Effect.forkScoped({ startImmediately: true })) + yield* pty.signalPipe(created.id, "SIGTERM") + expect(yield* Fiber.join(waiting)).toMatchObject({ exitCode: undefined, signal: "SIGTERM", outputLines: 0 }) + // A finished process answers a later wait at once and keeps its record until it is terminated. + expect((yield* pty.waitPipe(created.id)).signal).toBe("SIGTERM") + expect((yield* pty.getPipe(created.id)).lifecycle).toEqual({ + status: "exited", + exitCode: undefined, + signal: "SIGTERM", + }) + + yield* pty.terminatePipe(created.id) + expect(yield* Effect.flip(pty.getPipe(created.id))).toEqual( + new PersistentPty.ProcessNotFoundError({ processID: created.id }), + ) + expect(yield* pty.listPipes()).toEqual([]) + }).pipe(Effect.provide(layer(daemon))) + }), + ) + + it.live("keeps pipe processes and terminals apart", () => Effect.gen(function* () { const daemon = yield* fakeDaemon() yield* Effect.gen(function* () { const pty = yield* PersistentPty.Service const directory = yield* tmpdirScoped() - const terminal = yield* pty.create(Session.ID.make("ses_rename"), { + const terminal = yield* pty.create(Session.ID.make("ses_pipe"), { args: [], cwd: directory.path, title: "test", env: {}, }) - expect(terminal.userTitle).toBeNull() + const pipe = yield* pty.createPipe(input) - const error = yield* Effect.flip(pty.rename(terminal.id, "build")) + expect((yield* pty.list()).map((item) => item.id)).toEqual([terminal.id]) + expect((yield* pty.listPipes()).map((item) => item.id)).toEqual([pipe.id]) + const ptyID = Pty.ID.make(`pty_persistent_${pipe.id}`) + expect(yield* Effect.flip(pty.get(ptyID))).toEqual(new PersistentPty.NotFoundError({ ptyID })) + }).pipe(Effect.provide(layer(daemon))) + }), + ) - expect(error).toEqual(new PersistentPty.UnsupportedError({ feature: "rename" })) - expect(daemon.ops).not.toContain("rename") - expect((yield* pty.get(terminal.id)).title).toBe("test") + it.live("reports daemon rejections as request errors and a terminated wait as one", () => + Effect.gen(function* () { + const daemon = yield* fakeDaemon() + yield* Effect.gen(function* () { + const pty = yield* PersistentPty.Service + expect(yield* Effect.flip(pty.createPipe({ ...input, cwd: "relative" }))).toEqual( + new PersistentPty.RequestError({ message: "cwd must be absolute" }), + ) + const created = yield* pty.createPipe(input) + const waiting = yield* pty.waitPipe(created.id).pipe(Effect.forkScoped({ startImmediately: true })) + yield* poll(() => daemon.waiters(created.id) === 1) + yield* pty.terminatePipe(created.id) + expect(yield* Fiber.join(waiting).pipe(Effect.flip)).toEqual( + new PersistentPty.RequestError({ message: "terminated" }), + ) }).pipe(Effect.provide(layer(daemon))) }), ) }) +const poll = (ready: () => boolean) => + Effect.gen(function* () { + for (let attempt = 0; attempt < 200 && !ready(); attempt++) yield* Effect.sleep(10) + expect(ready()).toBeTrue() + }) + const nextUpdated = (bus: Bus.Interface) => bus .subscribe(Updated) @@ -163,3 +249,25 @@ const layer = (daemon: { readonly options: PersistentPty.Options }) => offlineModels, ], ) + +describe.skipIf(process.platform === "win32")("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: {}, + }) + expect(yield* pty.handoff()).toMatchObject({ ticket: "next" }) + expect(yield* pty.handoff()).toMatchObject({ ticket: "next" }) + }).pipe(Effect.provide(layer(daemon))) + }), + ) +}) diff --git a/packages/server/package.json b/packages/server/package.json index 8d589a0e706..88929f3ac5b 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -37,7 +37,7 @@ "effect": "catalog:" }, "devDependencies": { - "@opencode/pty": "0.4.0", + "@opencode/pty": "0.5.0", "@opencode/client": "workspace:*", "@opencode/plugin": "workspace:*", "@tsconfig/bun": "catalog:", diff --git a/packages/server/src/handlers/persistent-pty.ts b/packages/server/src/handlers/persistent-pty.ts index e861cfdecc2..d57340829e9 100644 --- a/packages/server/src/handlers/persistent-pty.ts +++ b/packages/server/src/handlers/persistent-pty.ts @@ -3,7 +3,6 @@ import { PtyTicket } from "@opencode/core/pty/ticket" import { ForbiddenError, InvalidRequestError, - PtyFeatureUnsupportedError, PtyNotFoundError, ServiceUnavailableError, } from "@opencode/protocol/errors" @@ -113,19 +112,7 @@ export const PersistentPtyHandler = HttpApiBuilder.group(Api, "server.experiment .resize(ctx.params.ptyID, ctx.payload.size.cols, ctx.payload.size.rows, ctx.payload.attachmentID) .pipe(mapTerminalError) if (ctx.payload.title === undefined) return { data: yield* pty.get(ctx.params.ptyID).pipe(mapTerminalError) } - return { - data: yield* pty.rename(ctx.params.ptyID, ctx.payload.title).pipe( - Effect.catchTags({ - "PersistentPty.NotFoundError": notFound, - "PersistentPty.UnsupportedError": (error) => - new PtyFeatureUnsupportedError({ - feature: error.feature, - message: "Terminal renaming will be available after OpenCode's terminal service restarts", - }), - "PersistentPty.UnavailableError": unavailable, - }), - ), - } + return { data: yield* pty.rename(ctx.params.ptyID, ctx.payload.title).pipe(mapTerminalError) } }), ) .handle( diff --git a/packages/server/test/persistent-pty.test.ts b/packages/server/test/persistent-pty.test.ts index 2be137bb61b..d4b72854334 100644 --- a/packages/server/test/persistent-pty.test.ts +++ b/packages/server/test/persistent-pty.test.ts @@ -208,7 +208,7 @@ smoke( 20_000, ) -it.live("reports renaming as unsupported on a daemon without the rename feature", () => +it.live("renames without a feature check and hides unknown terminals", () => Effect.gen(function* () { const fixture = yield* testDirectory() const daemon = yield* fakeDaemon() @@ -222,7 +222,7 @@ it.live("reports renaming as unsupported on a daemon without the rename feature" fs: { filewatcher: false }, }) const base = HttpServer.formatAddress(server.address) - const created = yield* request(base, "POST", "/api/experimental/session/ses_old_daemon/terminal", { + const created = yield* request(base, "POST", "/api/experimental/session/ses_v8_daemon/terminal", { args: [], cwd: fixture.root, title: "created", @@ -231,28 +231,19 @@ it.live("reports renaming as unsupported on a daemon without the rename feature" const terminal = Schema.decodeUnknownSync(PersistentPty.Info)(created.data) expect(terminal.userTitle).toBeNull() - const response = yield* Effect.promise(async () => { - const response = await fetch(new URL(`/api/experimental/persistent-pty/${terminal.id}`, base), { - method: "PUT", - headers: { authorization: `Basic ${btoa("opencode:secret")}`, "content-type": "application/json" }, - body: JSON.stringify({ title: "build" }), - }) - return { status: response.status, body: await response.json() } - }) + const renamed = yield* request(base, "PUT", `/api/experimental/persistent-pty/${terminal.id}`, { title: "build" }) + expect(renamed.data).toMatchObject({ title: "build", userTitle: "build" }) + expect(daemon.ops).toContain("rename") + expect(daemon.ops).not.toContain("list") - expect(response).toEqual({ - status: 501, - body: { - _tag: "PtyFeatureUnsupportedError", - feature: "rename", - message: "Terminal renaming will be available after OpenCode's terminal service restarts", - }, - }) - expect(daemon.ops).not.toContain("rename") - expect((yield* request(base, "GET", `/api/experimental/persistent-pty/${terminal.id}`)).data).toMatchObject({ - title: "test", - userTitle: null, + const missing = yield* Effect.promise(async () => { + const response = await fetch(new URL("/api/experimental/persistent-pty/pty_persistent_99", base), { + headers: { authorization: `Basic ${btoa("opencode:secret")}` }, + }) + await response.arrayBuffer() + return response.status }) + expect(missing).toBe(404) }), ) @@ -573,12 +564,11 @@ smoke( ) smoke( - "migrates a legacy runtime directory on restart and boots fresh when the registration is gone", + "boots fresh on restart when the handed-off registration is gone", () => Effect.gen(function* () { const fixture = yield* testDirectory() const scope = yield* Scope.Scope - const legacy = path.join(fixture.root, "legacy") const options = { hostname: "127.0.0.1", port: 0, @@ -586,50 +576,32 @@ smoke( app: { version: "test-version" }, database: { path: fixture.database }, fs: { filewatcher: false }, + pty: { root: fixture.directory }, } - const sessionID = Session.ID.make("ses_persistent_pty_migration") + const sessionID = Session.ID.make("ses_persistent_pty_gone") const originalScope = yield* Scope.fork(scope) - const original = yield* ServerProcess.start({ ...options, pty: { root: legacy } }).pipe( + const original = yield* ServerProcess.start(options).pipe( Effect.provideService(Scope.Scope, originalScope), ) const base = HttpServer.formatAddress(original.address) const first = Schema.decodeUnknownSync(PersistentPty.Info)( (yield* request(base, "POST", `/api/experimental/session/${sessionID}/terminal`, { command: "/bin/sh", - args: ["-c", "stty -echo; printf before-migration; exec cat"], + args: ["-c", "stty -echo; printf before-restart; exec cat"], cwd: process.cwd(), - title: "legacy", + title: "first", env: {}, })).data, ) - expect(yield* waitForText(base, first.id, "before-migration")).toContain("before-migration") - const legacyHandoff = Schema.decodeUnknownSync(PersistentPty.Handoff)( + expect(yield* waitForText(base, first.id, "before-restart")).toContain("before-restart") + const handoff = Schema.decodeUnknownSync(PersistentPty.Handoff)( (yield* request(base, "POST", "/api/experimental/persistent-pty/handoff")).handoff, ) yield* Scope.close(originalScope, Exit.void) - - const migratedScope = yield* Scope.fork(scope) - const migrated = yield* ServerProcess.start({ - ...options, - pty: { root: fixture.directory, handoff: legacyHandoff }, - }).pipe(Effect.provideService(Scope.Scope, migratedScope)) - const migratedBase = HttpServer.formatAddress(migrated.address) - const directory = path.join(fixture.directory, path.basename(legacyHandoff.directory)) - expect(existsSync(path.join(directory, "service.json"))).toBeTrue() - expect(existsSync(path.join(legacyHandoff.directory, "service.json"))).toBeFalse() - expect( - (yield* request(migratedBase, "GET", `/api/experimental/session/${sessionID}/terminal`)).data, - ).toMatchObject([{ id: first.id, pid: first.pid }]) - - const handoff = Schema.decodeUnknownSync(PersistentPty.Handoff)( - (yield* request(migratedBase, "POST", "/api/experimental/persistent-pty/handoff")).handoff, - ) - expect(handoff.directory).toBe(directory) - yield* Scope.close(migratedScope, Exit.void) // Simulate macOS purging the registration while the daemon waits for its successor. - yield* Effect.promise(() => fs.rm(path.join(directory, "service.json"))) + yield* Effect.promise(() => fs.rm(path.join(handoff.directory, "service.json"))) - const fresh = yield* ServerProcess.start({ ...options, pty: { root: fixture.directory, handoff } }) + const fresh = yield* ServerProcess.start({ ...options, pty: { ...options.pty, handoff } }) const freshBase = HttpServer.formatAddress(fresh.address) expect((yield* request(freshBase, "GET", `/api/experimental/session/${sessionID}/terminal`)).data).toEqual([]) const second = Schema.decodeUnknownSync(PersistentPty.Info)(