Skip to content

Commit ba936f3

Browse files
committed
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 anomalyco#51051
1 parent 567c1d0 commit ba936f3

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

‎packages/server/src/handlers/persistent-pty.ts‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { Instance } from "@opencode/core/instance/service"
12
import { PersistentPty } from "@opencode/core/persistent-pty"
23
import { PtyTicket } from "@opencode/core/pty/ticket"
4+
import { Session } from "@opencode/core/session"
35
import { ShellSelect } from "@opencode/core/shell/select"
46
import {
57
ForbiddenError,
@@ -13,12 +15,13 @@ import {
1315
PTY_CONNECT_TOKEN_HEADER,
1416
PTY_CONNECT_TOKEN_HEADER_VALUE,
1517
} from "@opencode/protocol/groups/persistent-pty"
16-
import { Effect, Queue } from "effect"
18+
import { Effect, Option, Queue } from "effect"
1719
import { HttpServerRequest, HttpServerResponse } from "effect/http"
1820
import { HttpApiBuilder, HttpApiSchema } from "effect/http-api"
1921
import { Socket } from "effect/socket"
2022
import { Api } from "../api"
2123
import { CorsConfig, isAllowedRequestOrigin } from "../cors"
24+
import { awaitPlugins, sessionInfo } from "../location"
2225
import { makeArtifact } from "./artifact"
2326
import { makeMediaGet } from "./media"
2427
import { type Outbound, runPtySocket } from "./pty-socket"
@@ -33,6 +36,30 @@ export const PersistentPtyHandler = HttpApiBuilder.group(Api, "server.experiment
3336
const mediaGet = yield* makeMediaGet
3437
const schedule = yield* makeSchedule
3538

39+
// Resolves the configured shell in the terminal's session location. The experimental endpoints carry no
40+
// location middleware, so the session's instance services are provided here, the same way
41+
// middleware/session-location.ts provides them. Resolution is best effort: terminals whose session or
42+
// location cannot be resolved keep the process-global environment shell, as before this change.
43+
const configuredShell = Effect.fn("PersistentPty.configuredShell")(function* (sessionID: Session.ID) {
44+
const sessions = yield* Session.Service
45+
const instances = yield* Instance.Service
46+
const session = yield* sessionInfo(sessions, sessionID).pipe(Effect.option)
47+
if (Option.isNone(session)) return undefined
48+
const shell = yield* instances
49+
.provide(session.value)(
50+
awaitPlugins.pipe(
51+
Effect.andThen(
52+
Effect.gen(function* () {
53+
const shellSelect = yield* ShellSelect.Service
54+
return yield* shellSelect.resolve({ priority: "config" })
55+
}),
56+
),
57+
),
58+
)
59+
.pipe(Effect.option)
60+
return Option.isSome(shell) ? shell.value : undefined
61+
})
62+
3663
return handlers
3764
.handleRaw("media.get", (ctx) => mediaGet(ctx.params.hash, ctx.request))
3865
.handle("artifacts.providers", (ctx) => artifact.providers(ctx.query, ctx.request))
@@ -63,14 +90,14 @@ export const PersistentPtyHandler = HttpApiBuilder.group(Api, "server.experiment
6390
.handle(
6491
"persistentPty.create",
6592
Effect.fn(function* (ctx) {
66-
const shell = yield* ShellSelect.Service
6793
return {
6894
data: yield* pty
6995
.create(ctx.params.sessionID, {
7096
// PersistentPty is process-global and cannot access the
7197
// Location-scoped shell service, so resolve the configured
72-
// shell here for terminals created without a command.
73-
command: ctx.payload.command ?? (yield* shell.resolve({ priority: "config" })),
98+
// shell in the session's location for terminals created
99+
// without an explicit command.
100+
command: ctx.payload.command ?? (yield* configuredShell(ctx.params.sessionID)),
74101
args: ctx.payload.args,
75102
cwd: ctx.payload.cwd,
76103
title: ctx.payload.title,

‎packages/server/test/persistent-pty.test.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,8 @@ smoke(
506506
yield* Effect.promise(async () => {
507507
await fs.writeFile(configured, "#!/bin/sh\nprintf configured-shell; exec cat\n")
508508
await fs.chmod(configured, 0o755)
509+
await fs.writeFile(path.join(fixture.root, "opencode.json"), JSON.stringify({ shell: configured }))
510+
await fs.writeFile(path.join(fixture.root, "catalog.json"), JSON.stringify({}))
509511
})
510512
const server = yield* ServerProcess.start<never, never>({
511513
hostname: "127.0.0.1",
@@ -514,10 +516,15 @@ smoke(
514516
app: { version: "test-version" },
515517
database: { path: fixture.database },
516518
fs: { filewatcher: false },
517-
config: { content: JSON.stringify({ shell: configured }) },
519+
models: { fetch: false, file: path.join(fixture.root, "catalog.json") },
518520
})
519521
const base = HttpServer.formatAddress(server.address)
520-
const sessionID = Session.ID.make("ses_persistent_pty_configured_shell")
522+
// Shell resolution goes through the terminal's session location, so create a session rooted at the fixture.
523+
const sessionID = Schema.decodeUnknownSync(Session.ID)(
524+
((yield* request(base, "POST", "/api/session", { location: { directory: fixture.root } })) as {
525+
data: { id: unknown }
526+
}).data.id,
527+
)
521528
expect((yield* request(base, "GET", `/api/experimental/session/${sessionID}/terminal`)).data).toEqual([])
522529
const terminal = Schema.decodeUnknownSync(PersistentPty.Info)(
523530
(yield* request(base, "POST", `/api/experimental/session/${sessionID}/terminal`, {

0 commit comments

Comments
 (0)