|
1 | 1 | export * as Session from "./session.js" |
2 | 2 | export * from "./session/schema.js" |
3 | 3 |
|
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" |
5 | 5 | import { ListAnchor } from "@opencode-ai/schema/session" |
6 | 6 | import { and, asc, desc, eq, gt, isNull, like, lt, or, type SQL } from "drizzle-orm" |
7 | 7 | import { Project } from "./project.js" |
@@ -50,8 +50,7 @@ import { Job } from "./job.js" |
50 | 50 | import { Command } from "./command.js" |
51 | 51 | import { Shell } from "./shell.js" |
52 | 52 | 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" |
55 | 54 | import { fileURLToPath } from "url" |
56 | 55 | import { SessionEnvironment } from "./session/environment.js" |
57 | 56 | import { SessionHistory } from "./session/history.js" |
@@ -347,8 +346,6 @@ const layer = Layer.effect( |
347 | 346 | const jobs = yield* Job.Service |
348 | 347 | const environments = yield* SessionEnvironment.Service |
349 | 348 | const scope = yield* Scope.Scope |
350 | | - const activeShells = new Set<SessionSchema.ID>() |
351 | | - const shellLocks = KeyedMutex.makeUnsafe<SessionSchema.ID>() |
352 | 349 | const closeTransport = Effect.fn("Session.closeTransport")(function* (session: SessionSchema.Info) { |
353 | 350 | const location = Location.Ref.make({ |
354 | 351 | directory: session.location.directory, |
@@ -680,10 +677,7 @@ const layer = Layer.effect( |
680 | 677 | // retried payload, metadata, and delivery mode. |
681 | 678 | if (admitted.type !== "user" || admitted.sessionID !== input.sessionID) |
682 | 679 | 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) |
687 | 681 | return admitted |
688 | 682 | }), |
689 | 683 | ), |
@@ -717,60 +711,62 @@ const layer = Layer.effect( |
717 | 711 | }), |
718 | 712 | shell: Effect.fn("Session.shell")(function* (input) { |
719 | 713 | 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, |
744 | 740 | ) |
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), |
761 | 761 | sessionID: input.sessionID, |
762 | | - shell: completed.shell, |
763 | | - output: completed.output, |
| 762 | + resume: false, |
764 | 763 | }) |
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) |
774 | 770 | }), |
775 | 771 | skill: Effect.fn("Session.skill")(function* (input) { |
776 | 772 | const session = yield* result.get(input.sessionID) |
@@ -994,26 +990,6 @@ const layer = Layer.effect( |
994 | 990 | }), |
995 | 991 | ) |
996 | 992 |
|
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 | | - |
1017 | 993 | const preparePrompt = Effect.fn("Session.preparePrompt")(function* ( |
1018 | 994 | request: Parameters<Interface["prompt"]>[0], |
1019 | 995 | messageID: SessionMessage.ID, |
|
0 commit comments