From 567c1d0ccf4266dd2cf3a68657fae68b8cc52eae Mon Sep 17 00:00:00 2001 From: Wu Junkai Date: Tue, 3 Nov 2026 15:26:59 +0800 Subject: [PATCH 1/3] fix(server): resolve the configured shell for session terminals Session terminals resolved their shell via ShellSelect.environment(), which only reads process.env.SHELL, so the shell config option was ignored (always PowerShell on Windows). Resolve through the Location-scoped ShellSelect.Service in the persistentPty.create handler instead, matching the agent shell tool and pty.ts. Fixes #51051 --- .../server/src/handlers/persistent-pty.ts | 7 +++- packages/server/test/persistent-pty.test.ts | 37 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/packages/server/src/handlers/persistent-pty.ts b/packages/server/src/handlers/persistent-pty.ts index f5297bb47207..95c10047ac17 100644 --- a/packages/server/src/handlers/persistent-pty.ts +++ b/packages/server/src/handlers/persistent-pty.ts @@ -1,5 +1,6 @@ import { PersistentPty } from "@opencode/core/persistent-pty" import { PtyTicket } from "@opencode/core/pty/ticket" +import { ShellSelect } from "@opencode/core/shell/select" import { ForbiddenError, InvalidRequestError, @@ -62,10 +63,14 @@ export const PersistentPtyHandler = HttpApiBuilder.group(Api, "server.experiment .handle( "persistentPty.create", Effect.fn(function* (ctx) { + const shell = yield* ShellSelect.Service return { data: yield* pty .create(ctx.params.sessionID, { - command: ctx.payload.command, + // PersistentPty is process-global and cannot access the + // Location-scoped shell service, so resolve the configured + // shell here for terminals created without a command. + command: ctx.payload.command ?? (yield* shell.resolve({ priority: "config" })), args: ctx.payload.args, cwd: ctx.payload.cwd, title: ctx.payload.title, diff --git a/packages/server/test/persistent-pty.test.ts b/packages/server/test/persistent-pty.test.ts index 2be137bb61bf..da66c7b1afa2 100644 --- a/packages/server/test/persistent-pty.test.ts +++ b/packages/server/test/persistent-pty.test.ts @@ -497,6 +497,43 @@ smoke( 20_000, ) +smoke( + "uses the configured shell for terminals created without a command", + () => + Effect.gen(function* () { + const fixture = yield* testDirectory() + const configured = path.join(fixture.root, "configured-shell") + yield* Effect.promise(async () => { + await fs.writeFile(configured, "#!/bin/sh\nprintf configured-shell; exec cat\n") + await fs.chmod(configured, 0o755) + }) + const server = yield* ServerProcess.start({ + hostname: "127.0.0.1", + port: 0, + password: "secret", + app: { version: "test-version" }, + database: { path: fixture.database }, + fs: { filewatcher: false }, + config: { content: JSON.stringify({ shell: configured }) }, + }) + const base = HttpServer.formatAddress(server.address) + const sessionID = Session.ID.make("ses_persistent_pty_configured_shell") + expect((yield* request(base, "GET", `/api/experimental/session/${sessionID}/terminal`)).data).toEqual([]) + const terminal = Schema.decodeUnknownSync(PersistentPty.Info)( + (yield* request(base, "POST", `/api/experimental/session/${sessionID}/terminal`, { + args: [], + cwd: fixture.root, + title: "configured shell", + env: {}, + })).data, + ) + expect(terminal.command).toBe(configured) + expect(yield* waitForText(base, terminal.id, "configured-shell")).toContain("configured-shell") + yield* request(base, "DELETE", `/api/experimental/persistent-pty/${terminal.id}`) + }), + 20_000, +) + smoke( "isolates servers sharing a database and preserves terminals only through explicit restart handoff", () => From ba936f3e566040340d6f19ac0e0e70bf80ffd363 Mon Sep 17 00:00:00 2001 From: Wu Junkai Date: Tue, 3 Nov 2026 19:07:41 +0800 Subject: [PATCH 2/3] fix(server): resolve the configured shell in the session location Review on the first revision showed ShellSelect.Service is not provided to the persistentPty handlers: the experimental endpoints carry no location middleware, so the yield failed every terminal create and leaked into the routes layer. Resolve in the session's location instead, the same way middleware/session-location.ts does: sessionInfo, then instances.provide, then ShellSelect.Service, awaiting plugin activation so the config-to-shell state is populated. Resolution is lazy (explicit commands never enter it) and best effort: terminals whose session cannot be resolved keep the environment shell, which the existing tests depend on. Fixes #51051 --- .../server/src/handlers/persistent-pty.ts | 35 ++++++++++++++++--- packages/server/test/persistent-pty.test.ts | 11 ++++-- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/packages/server/src/handlers/persistent-pty.ts b/packages/server/src/handlers/persistent-pty.ts index 95c10047ac17..658eb829bff5 100644 --- a/packages/server/src/handlers/persistent-pty.ts +++ b/packages/server/src/handlers/persistent-pty.ts @@ -1,5 +1,7 @@ +import { Instance } from "@opencode/core/instance/service" import { PersistentPty } from "@opencode/core/persistent-pty" import { PtyTicket } from "@opencode/core/pty/ticket" +import { Session } from "@opencode/core/session" import { ShellSelect } from "@opencode/core/shell/select" import { ForbiddenError, @@ -13,12 +15,13 @@ import { PTY_CONNECT_TOKEN_HEADER, PTY_CONNECT_TOKEN_HEADER_VALUE, } from "@opencode/protocol/groups/persistent-pty" -import { Effect, Queue } from "effect" +import { Effect, Option, Queue } from "effect" import { HttpServerRequest, HttpServerResponse } from "effect/http" import { HttpApiBuilder, HttpApiSchema } from "effect/http-api" import { Socket } from "effect/socket" import { Api } from "../api" import { CorsConfig, isAllowedRequestOrigin } from "../cors" +import { awaitPlugins, sessionInfo } from "../location" import { makeArtifact } from "./artifact" import { makeMediaGet } from "./media" import { type Outbound, runPtySocket } from "./pty-socket" @@ -33,6 +36,30 @@ export const PersistentPtyHandler = HttpApiBuilder.group(Api, "server.experiment const mediaGet = yield* makeMediaGet const schedule = yield* makeSchedule + // Resolves the configured shell in the terminal's session location. The experimental endpoints carry no + // location middleware, so the session's instance services are provided here, the same way + // middleware/session-location.ts provides them. Resolution is best effort: terminals whose session or + // location cannot be resolved keep the process-global environment shell, as before this change. + const configuredShell = Effect.fn("PersistentPty.configuredShell")(function* (sessionID: Session.ID) { + const sessions = yield* Session.Service + const instances = yield* Instance.Service + const session = yield* sessionInfo(sessions, sessionID).pipe(Effect.option) + if (Option.isNone(session)) return undefined + const shell = yield* instances + .provide(session.value)( + awaitPlugins.pipe( + Effect.andThen( + Effect.gen(function* () { + const shellSelect = yield* ShellSelect.Service + return yield* shellSelect.resolve({ priority: "config" }) + }), + ), + ), + ) + .pipe(Effect.option) + return Option.isSome(shell) ? shell.value : undefined + }) + return handlers .handleRaw("media.get", (ctx) => mediaGet(ctx.params.hash, ctx.request)) .handle("artifacts.providers", (ctx) => artifact.providers(ctx.query, ctx.request)) @@ -63,14 +90,14 @@ export const PersistentPtyHandler = HttpApiBuilder.group(Api, "server.experiment .handle( "persistentPty.create", Effect.fn(function* (ctx) { - const shell = yield* ShellSelect.Service return { data: yield* pty .create(ctx.params.sessionID, { // PersistentPty is process-global and cannot access the // Location-scoped shell service, so resolve the configured - // shell here for terminals created without a command. - command: ctx.payload.command ?? (yield* shell.resolve({ priority: "config" })), + // shell in the session's location for terminals created + // without an explicit command. + command: ctx.payload.command ?? (yield* configuredShell(ctx.params.sessionID)), args: ctx.payload.args, cwd: ctx.payload.cwd, title: ctx.payload.title, diff --git a/packages/server/test/persistent-pty.test.ts b/packages/server/test/persistent-pty.test.ts index da66c7b1afa2..68ddff4c6775 100644 --- a/packages/server/test/persistent-pty.test.ts +++ b/packages/server/test/persistent-pty.test.ts @@ -506,6 +506,8 @@ smoke( yield* Effect.promise(async () => { await fs.writeFile(configured, "#!/bin/sh\nprintf configured-shell; exec cat\n") await fs.chmod(configured, 0o755) + await fs.writeFile(path.join(fixture.root, "opencode.json"), JSON.stringify({ shell: configured })) + await fs.writeFile(path.join(fixture.root, "catalog.json"), JSON.stringify({})) }) const server = yield* ServerProcess.start({ hostname: "127.0.0.1", @@ -514,10 +516,15 @@ smoke( app: { version: "test-version" }, database: { path: fixture.database }, fs: { filewatcher: false }, - config: { content: JSON.stringify({ shell: configured }) }, + models: { fetch: false, file: path.join(fixture.root, "catalog.json") }, }) const base = HttpServer.formatAddress(server.address) - const sessionID = Session.ID.make("ses_persistent_pty_configured_shell") + // Shell resolution goes through the terminal's session location, so create a session rooted at the fixture. + const sessionID = Schema.decodeUnknownSync(Session.ID)( + ((yield* request(base, "POST", "/api/session", { location: { directory: fixture.root } })) as { + data: { id: unknown } + }).data.id, + ) expect((yield* request(base, "GET", `/api/experimental/session/${sessionID}/terminal`)).data).toEqual([]) const terminal = Schema.decodeUnknownSync(PersistentPty.Info)( (yield* request(base, "POST", `/api/experimental/session/${sessionID}/terminal`, { From cbaefe47d40d57606a7ef5f450473d44c0fd57d3 Mon Sep 17 00:00:00 2001 From: Wu Junkai Date: Tue, 3 Nov 2026 19:19:10 +0800 Subject: [PATCH 3/3] fix(server): close over session services instead of yielding per request Yielding Session.Service and Instance.Service inside the per-request resolution added them as requirements of the routes layer (89 tsc -b errors in the server tests on an otherwise clean base). Yield them once in the group's setup next to PtyTicket.Service and close over them instead. Server typecheck:tsc is clean end to end now, and the persistent-pty suite stays at 8/8 on Linux. --- packages/server/src/handlers/persistent-pty.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/server/src/handlers/persistent-pty.ts b/packages/server/src/handlers/persistent-pty.ts index 658eb829bff5..ce37f954a470 100644 --- a/packages/server/src/handlers/persistent-pty.ts +++ b/packages/server/src/handlers/persistent-pty.ts @@ -30,6 +30,8 @@ import { makeSchedule } from "./schedule" export const PersistentPtyHandler = HttpApiBuilder.group(Api, "server.experimental", (handlers) => Effect.gen(function* () { const tickets = yield* PtyTicket.Service + const sessions = yield* Session.Service + const instances = yield* Instance.Service const cors = yield* CorsConfig const pty = yield* PersistentPty.Service const artifact = yield* makeArtifact @@ -41,8 +43,6 @@ export const PersistentPtyHandler = HttpApiBuilder.group(Api, "server.experiment // middleware/session-location.ts provides them. Resolution is best effort: terminals whose session or // location cannot be resolved keep the process-global environment shell, as before this change. const configuredShell = Effect.fn("PersistentPty.configuredShell")(function* (sessionID: Session.ID) { - const sessions = yield* Session.Service - const instances = yield* Instance.Service const session = yield* sessionInfo(sessions, sessionID).pipe(Effect.option) if (Option.isNone(session)) return undefined const shell = yield* instances