Skip to content

Commit ad9117b

Browse files
authored
fix(session): run user shells immediately in the background
Run user shell commands concurrently with model execution, retain their output in one shell entry, and admit non-waking completion messages. Share result capture and notification formatting across shell callers.
1 parent 6b7a1d4 commit ad9117b

24 files changed

Lines changed: 1018 additions & 214 deletions

‎packages/client/src/solid/data.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,8 @@ export function createData(config: CreateDataInput) {
750750
command: event.data.shell.command,
751751
status: event.data.shell.status,
752752
exit: event.data.shell.exit,
753-
metadata: event.metadata,
753+
metadata:
754+
event.data.shell.metadata.background === true ? { ...event.metadata, background: true } : event.metadata,
754755
time: { created: event.created },
755756
})
756757
})

‎packages/client/test/solid-data.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,36 @@ test("ignores activity snapshots from an older connection", async () => {
666666
}
667667
})
668668

669+
test("projects background user shell metadata from durable shell data", () => {
670+
const setup = activityFixture(() => Response.json({ data: {} }))
671+
try {
672+
setup.emit({
673+
id: "evt_user_shell",
674+
created: 1,
675+
type: "session.shell.started",
676+
durable: { aggregateID: "ses_refresh", seq: 1, version: 1 },
677+
data: {
678+
sessionID: "ses_refresh",
679+
shell: {
680+
id: "sh_user",
681+
status: "running",
682+
command: "pwd",
683+
cwd: "/project",
684+
shell: "/bin/sh",
685+
file: "/project/shell.out",
686+
metadata: { sessionID: "ses_refresh", background: true },
687+
time: { started: 1 },
688+
},
689+
},
690+
})
691+
expect(setup.data.session.message.list("ses_refresh")).toMatchObject([
692+
{ type: "shell", shellID: "sh_user", status: "running", metadata: { background: true } },
693+
])
694+
} finally {
695+
setup.dispose()
696+
}
697+
})
698+
669699
function activityFixture(read: () => Response | Promise<Response>) {
670700
const listeners = new Set<Parameters<CreateDataInput["event"]["listen"]>[0]>()
671701
const api = OpenCode.make({

‎packages/core/src/session.ts‎

Lines changed: 56 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export * as Session from "./session.js"
22
export * from "./session/schema.js"
33

4-
import { Cause, Effect, Layer, Schema, Context, RcMap, Stream, Scope } from "effect"
4+
import { Cause, Effect, Fiber, Layer, Schema, Context, RcMap, Stream, Scope } from "effect"
55
import { ListAnchor } from "@opencode-ai/schema/session"
66
import { and, asc, desc, eq, gt, isNull, like, lt, or, type SQL } from "drizzle-orm"
77
import { Project } from "./project.js"
@@ -50,8 +50,7 @@ import { Job } from "./job.js"
5050
import { Command } from "./command.js"
5151
import { Shell } from "./shell.js"
5252
import { Global } from "@opencode-ai/util/global"
53-
import { Shell as ShellSchema } from "@opencode-ai/schema/shell"
54-
import { KeyedMutex } from "./effect/keyed-mutex.js"
53+
import { ShellResult } from "./shell/result.js"
5554
import { fileURLToPath } from "url"
5655
import { SessionEnvironment } from "./session/environment.js"
5756
import { SessionHistory } from "./session/history.js"
@@ -347,8 +346,6 @@ const layer = Layer.effect(
347346
const jobs = yield* Job.Service
348347
const environments = yield* SessionEnvironment.Service
349348
const scope = yield* Scope.Scope
350-
const activeShells = new Set<SessionSchema.ID>()
351-
const shellLocks = KeyedMutex.makeUnsafe<SessionSchema.ID>()
352349
const closeTransport = Effect.fn("Session.closeTransport")(function* (session: SessionSchema.Info) {
353350
const location = Location.Ref.make({
354351
directory: session.location.directory,
@@ -680,10 +677,7 @@ const layer = Layer.effect(
680677
// retried payload, metadata, and delivery mode.
681678
if (admitted.type !== "user" || admitted.sessionID !== input.sessionID)
682679
return yield* new PromptConflictError({ sessionID: input.sessionID, messageID })
683-
if (input.resume !== false) {
684-
if (activeShells.has(admitted.sessionID)) return admitted
685-
yield* execution.wake(admitted.sessionID)
686-
}
680+
if (input.resume !== false) yield* execution.wake(admitted.sessionID)
687681
return admitted
688682
}),
689683
),
@@ -717,60 +711,62 @@ const layer = Layer.effect(
717711
}),
718712
shell: Effect.fn("Session.shell")(function* (input) {
719713
const session = yield* result.get(input.sessionID)
720-
yield* shellLocks.withLock(input.sessionID)(
721-
Effect.gen(function* () {
722-
activeShells.add(input.sessionID)
723-
yield* execution.awaitIdle(input.sessionID)
724-
const started = yield* Effect.gen(function* () {
725-
const plugins = yield* PluginSupervisor.Service
726-
yield* plugins.flush
727-
const shell = yield* Shell.Service
728-
return yield* shell
729-
.create({
730-
command: input.command,
731-
cwd: session.location.directory,
732-
timeout: 0,
733-
metadata: { sessionID: input.sessionID },
734-
})
735-
.pipe(Effect.orDie)
736-
}).pipe(Effect.provide(locations.get(session.location)))
737-
yield* bus.publish(
738-
SessionEvent.Shell.Started,
739-
{
740-
sessionID: input.sessionID,
741-
shell: started,
742-
},
743-
{ id: input.id },
714+
// The server owns completion recording even if the submitting client disconnects.
715+
const running = yield* Effect.gen(function* () {
716+
// Resolve shell services here without pinning Session events to this Location after a move.
717+
const shell = yield* Effect.gen(function* () {
718+
const plugins = yield* PluginSupervisor.Service
719+
yield* plugins.flush
720+
return yield* Shell.Service
721+
}).pipe(Effect.provide(locations.get(session.location)))
722+
const started = yield* shell
723+
.create({
724+
command: input.command,
725+
cwd: session.location.directory,
726+
timeout: 0,
727+
metadata: { sessionID: input.sessionID, background: true },
728+
})
729+
.pipe(
730+
Effect.tapError((error) =>
731+
result.synthetic({
732+
sessionID: input.sessionID,
733+
text: `User shell command failed to start:\n${input.command}\n\n${error.message}`,
734+
description: input.command,
735+
metadata: { source: "shell", state: "error" },
736+
resume: false,
737+
}),
738+
),
739+
Effect.orDie,
744740
)
745-
const completed = yield* Effect.gen(function* () {
746-
const shell = yield* Shell.Service
747-
const terminal = yield* shell.wait(started.id).pipe(
748-
Effect.map((info) => ({ info, retained: true as const })),
749-
Effect.catchTag("Shell.NotFoundError", () =>
750-
Effect.succeed({ info: synthesizeTerminalShellInfo(started), retained: false as const }),
751-
),
752-
)
753-
const output = terminal.retained
754-
? yield* shell
755-
.output(started.id, { limit: SHELL_MAX_CAPTURE_BYTES })
756-
.pipe(Effect.catchTag("Shell.NotFoundError", () => Effect.succeed(missingShellOutput())))
757-
: missingShellOutput()
758-
return { shell: terminal.info, output }
759-
}).pipe(Effect.provide(locations.get(session.location)))
760-
yield* bus.publish(SessionEvent.Shell.Ended, {
741+
yield* bus.publish(
742+
SessionEvent.Shell.Started,
743+
{
744+
sessionID: input.sessionID,
745+
shell: started,
746+
},
747+
{ id: input.id },
748+
)
749+
const terminal = yield* shell.result(started)
750+
const preview = yield* shell
751+
.output(started.id, { limit: SHELL_MAX_CAPTURE_BYTES })
752+
.pipe(Effect.catchTag("Shell.NotFoundError", () => Effect.succeed(ShellResult.unavailable)))
753+
yield* bus.publish(SessionEvent.Shell.Ended, {
754+
sessionID: input.sessionID,
755+
shell: terminal.info,
756+
output: preview,
757+
})
758+
yield* result
759+
.synthetic({
760+
...ShellResult.userNotification(terminal),
761761
sessionID: input.sessionID,
762-
shell: completed.shell,
763-
output: completed.output,
762+
resume: false,
764763
})
765-
}).pipe(
766-
Effect.ensuring(
767-
Effect.gen(function* () {
768-
activeShells.delete(input.sessionID)
769-
yield* execution.wake(input.sessionID)
770-
}),
771-
),
772-
),
773-
)
764+
.pipe(
765+
Effect.catchTag("Session.NotFoundError", () => Effect.void),
766+
Effect.orDie,
767+
)
768+
}).pipe(Effect.forkIn(scope, { startImmediately: true }))
769+
yield* Fiber.join(running)
774770
}),
775771
skill: Effect.fn("Session.skill")(function* (input) {
776772
const session = yield* result.get(input.sessionID)
@@ -994,26 +990,6 @@ const layer = Layer.effect(
994990
}),
995991
)
996992

997-
function missingShellOutput() {
998-
const output = "Shell command output is no longer available."
999-
return {
1000-
output,
1001-
cursor: Buffer.byteLength(output),
1002-
size: Buffer.byteLength(output),
1003-
truncated: false,
1004-
}
1005-
}
1006-
1007-
function synthesizeTerminalShellInfo(started: ShellSchema.Info): ShellSchema.Info {
1008-
return {
1009-
...started,
1010-
// The Shell record was removed before waiters could observe it; publish a terminal
1011-
// boundary instead of leaving the Session shell message permanently running.
1012-
status: "killed",
1013-
time: { ...started.time, completed: Date.now() },
1014-
}
1015-
}
1016-
1017993
const preparePrompt = Effect.fn("Session.preparePrompt")(function* (
1018994
request: Parameters<Interface["prompt"]>[0],
1019995
messageID: SessionMessage.ID,

‎packages/core/src/session/compaction.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,9 @@ const serialize = (message: SessionMessage.Info) => {
164164
if (message.type === "synthetic") return `[Synthetic context]: ${message.text}`
165165
if (message.type === "skill") return `[Skill activated: ${message.name}]\n${message.text}`
166166
if (message.type === "shell")
167-
return `[Shell]: ${message.command}\n${truncateToolOutput(message.output?.output ?? "")}`
167+
return message.metadata?.background === true
168+
? ""
169+
: `[Shell]: ${message.command}\n${truncateToolOutput(message.output?.output ?? "")}`
168170
return ""
169171
}
170172

‎packages/core/src/session/execution/restart.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { SessionEvent } from "../event.js"
99
import { SessionExecution } from "../execution.js"
1010
import { SessionSchema } from "../schema.js"
1111
import { SessionStore } from "../store.js"
12+
import { ShellResult } from "../../shell/result.js"
1213
import { SubagentCompletion } from "../subagent-completion.js"
1314

1415
const CONTINUE_AFTER_SERVER_RESTART =
@@ -116,13 +117,13 @@ export const layer = (options?: Options) =>
116117
id: background.notificationID,
117118
sessionID: recovery.sessionID,
118119
description: recovery.command,
119-
text: `<shell id="${background.id}" state="${state}" command="${recovery.command}">\n${text}\n</shell>`,
120-
metadata: {
121-
source: "shell",
120+
...ShellResult.notification({
122121
jobID: background.id,
123122
shellID: recovery.shellID,
123+
command: recovery.command,
124124
state,
125-
},
125+
text,
126+
}),
126127
...(suspended.has(recovery.sessionID) ? { resume: false } : {}),
127128
})
128129
.pipe(

‎packages/core/src/session/message-updater.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ export function update(adapter: Adapter, event: SessionEvent.DurableEvent) {
169169
SessionMessage.Shell.make({
170170
id: SessionMessage.ID.fromEvent(event.id),
171171
type: "shell",
172-
metadata: event.metadata,
172+
metadata:
173+
event.data.shell.metadata.background === true ? { ...event.metadata, background: true } : event.metadata,
173174
shellID: event.data.shell.id,
174175
command: event.data.shell.command,
175176
status: event.data.shell.status,

‎packages/core/src/session/runner/to-llm-message.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ function toLLMMessage(message: SessionMessage.Info, model: Model.Ref, providerMe
260260
case "system":
261261
return [Message.system(message.text)]
262262
case "shell":
263+
// Background shell results enter context once, through their completion inbox item.
264+
if (message.metadata?.background === true) return []
263265
return [
264266
Message.make({
265267
id: message.id,

‎packages/core/src/shell.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import type { ShellCreateBefore } from "@opencode-ai/plugin/effect/shell"
1818
import { PluginHooks } from "./plugin/hooks.js"
1919
import { SessionEnvironment } from "./session/environment.js"
2020
import { SessionSchema } from "./session/schema.js"
21+
import { Config } from "./config.js"
22+
import { ToolOutput } from "./tool-output.js"
23+
import { ShellResult } from "./shell/result.js"
2124

2225
export class NotFoundError extends Schema.TaggedError<NotFoundError>()("Shell.NotFoundError", {
2326
id: Shell.ID,
@@ -65,6 +68,8 @@ export interface Interface {
6568
// Resolves once the command reaches a terminal status, returning its final Info. Fails with
6669
// NotFoundError if the command is unknown or is removed before it terminates.
6770
readonly wait: (id: Shell.ID) => Effect.Effect<Shell.Info, NotFoundError>
71+
// A known shell's terminal state and bounded tail. Missing capture remains distinct from its exit status.
72+
readonly result: (started: Shell.Info) => Effect.Effect<ShellResult.Result>
6873
// Replaces the running command's timeout from now; zero clears it.
6974
readonly timeout: (id: Shell.ID, duration: number) => Effect.Effect<Shell.Info, NotFoundError>
7075
readonly output: (id: Shell.ID, input?: Shell.OutputInput) => Effect.Effect<Shell.Output, NotFoundError>
@@ -120,6 +125,7 @@ const layer = () =>
120125
const environment = yield* Environment.Service
121126
const hooks = yield* PluginHooks.Service
122127
const environments = yield* SessionEnvironment.Service
128+
const config = yield* Config.Service
123129
const context = yield* Effect.context()
124130
const runFork = Effect.runForkWith(context)
125131
const commands = new Map<Shell.ID, Active>()
@@ -217,6 +223,28 @@ const layer = () =>
217223
}
218224
})
219225

226+
const result = Effect.fn("Shell.result")(function* (started: Shell.Info) {
227+
const info = yield* wait(started.id).pipe(
228+
Effect.catchTag("Shell.NotFoundError", () =>
229+
Effect.succeed({ ...started, status: "killed" as const, time: { ...started.time, completed: Date.now() } }),
230+
),
231+
)
232+
const capture = yield* Effect.gen(function* () {
233+
const limits = Config.latest(yield* config.entries(), "tool_output")
234+
const maxLines = limits?.max_lines ?? ToolOutput.MAX_LINES
235+
const maxBytes = limits?.max_bytes ?? ToolOutput.MAX_BYTES
236+
const latest = yield* output(info.id, { cursor: Number.MAX_SAFE_INTEGER })
237+
const page = yield* output(info.id, { cursor: Math.max(0, latest.size - maxBytes), limit: maxBytes })
238+
const lines = page.output.split("\n")
239+
if (page.output.endsWith("\n")) lines.pop()
240+
const truncated = latest.size > maxBytes || lines.length > maxLines
241+
const text = lines.length > maxLines ? lines.slice(-maxLines).join("\n") : page.output
242+
const notice = truncated ? `\n\n[output truncated; full output saved to: ${info.file}]` : ""
243+
return { output: `${text || "(no output)"}${notice}`, truncated }
244+
}).pipe(Effect.catchTag("Shell.NotFoundError", () => Effect.succeed(undefined)))
245+
return { info, capture }
246+
})
247+
220248
const create = Effect.fn("Shell.create")(function* <E = never, R = never>(
221249
input: CreateInput,
222250
before?: (input: ShellCreateBefore) => Effect.Effect<void, E, R>,
@@ -382,7 +410,7 @@ const layer = () =>
382410
return command.info
383411
})
384412

385-
return Service.of({ create, list, get, wait, timeout, output, remove })
413+
return Service.of({ create, list, get, wait, result, timeout, output, remove })
386414
}),
387415
)
388416

@@ -397,6 +425,7 @@ export const node = makeLocationNode({
397425
Environment.node,
398426
PluginHooks.node,
399427
SessionEnvironment.node,
428+
Config.node,
400429
cleanupNode,
401430
],
402431
})

0 commit comments

Comments
 (0)