mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
refactor(core): keep daemon shell commands in one server-wide registry
This commit is contained in:
parent
0475da6918
commit
71005e765c
7 changed files with 527 additions and 401 deletions
|
|
@ -169,6 +169,7 @@ const services = [
|
|||
SubagentJob.Service,
|
||||
SharedCache.Service,
|
||||
Shell.Service,
|
||||
Shell.Daemon,
|
||||
ShellSelect.Service,
|
||||
Snapshot.Service,
|
||||
Skill.Service,
|
||||
|
|
@ -213,6 +214,7 @@ export const requirements = LayerNode.group([
|
|||
Detaching.node,
|
||||
Job.node,
|
||||
LocationLifecycle.node,
|
||||
Shell.daemonNode,
|
||||
KV.node,
|
||||
llmClient,
|
||||
Location.node,
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -252,6 +252,7 @@ export const Plugin = {
|
|||
const environment = yield* Environment.Service
|
||||
const access = yield* FileAccess.Service
|
||||
const shell = yield* Shell.Service
|
||||
const daemon = yield* Shell.Daemon
|
||||
const shellSelect = yield* ShellSelect.Service
|
||||
const compatibleShell = shellSelect.resolve({ priority: "compat" })
|
||||
const permission = yield* Permission.Service
|
||||
|
|
@ -294,11 +295,10 @@ export const Plugin = {
|
|||
})
|
||||
|
||||
const notifyWhenDone = Effect.fn("ShellTool.notifyWhenDone")(
|
||||
(sessionID: SessionSchema.ID, id: string, shellID: string, command: string, settled: Deferred.Deferred<Output>) =>
|
||||
notify(sessions, jobs, { sessionID, id, shellID, command, settled }),
|
||||
// Not in this Location's scope: a daemon command outlives a Location that closes while the server runs, and
|
||||
// its session must still hear how it ended. The watch ends with the job, or when the job registry closes.
|
||||
Effect.forkDetach({ startImmediately: true }),
|
||||
// its session must still hear how it ended.
|
||||
(sessionID: SessionSchema.ID, id: string, shellID: string, command: string, settled: Deferred.Deferred<Output>) =>
|
||||
daemon.watch(notify(sessions, jobs, { sessionID, id, shellID, command, settled })),
|
||||
)
|
||||
|
||||
// Foreground commands this tool is blocked on. A planned shutdown moves them to the background, so it records
|
||||
|
|
|
|||
|
|
@ -40,8 +40,10 @@ export const daemonShells = Effect.fn(function* () {
|
|||
})
|
||||
}),
|
||||
).pipe(Layer.provide(hostEnvironmentLayer))
|
||||
const shellLayer = (directory: string) =>
|
||||
// `server` shares one server's daemon commands between Locations; without it the Location gets a server of its own.
|
||||
const shellLayer = (directory: string, server?: Shell.DaemonInterface) =>
|
||||
AppNodeBuilder.build(LayerNode.group([Shell.node, LocationLifecycle.node, Bus.node, Detaching.node]), [
|
||||
...(server ? [Shell.daemonNode.replace(Layer.succeed(Shell.Daemon, server))] : []),
|
||||
Location.node.replace(
|
||||
Layer.succeed(
|
||||
Location.Service,
|
||||
|
|
@ -53,7 +55,15 @@ export const daemonShells = Effect.fn(function* () {
|
|||
Environment.node.replace(environment),
|
||||
PersistentPty.node.replace(Layer.succeed(PersistentPty.Service, pty)),
|
||||
])
|
||||
// A Location's Shell that stays open for the rest of the test.
|
||||
// The server that `openLocation` Locations share, as long as the test runs.
|
||||
const serverContext = yield* Layer.build(
|
||||
AppNodeBuilder.build(LayerNode.group([Shell.daemonNode, Bus.node]), [
|
||||
Global.node.replace(global),
|
||||
PersistentPty.node.replace(Layer.succeed(PersistentPty.Service, pty)),
|
||||
]),
|
||||
)
|
||||
const server = Context.get(serverContext, Shell.Daemon)
|
||||
// A Location's Shell, on a server of its own, that stays open for the rest of the test.
|
||||
const openShell = (directory: string) =>
|
||||
Effect.gen(function* () {
|
||||
const context = yield* Layer.build(shellLayer(directory))
|
||||
|
|
@ -63,7 +73,7 @@ export const daemonShells = Effect.fn(function* () {
|
|||
const openLocation = (directory: string) =>
|
||||
Effect.gen(function* () {
|
||||
const scope = yield* Scope.make()
|
||||
const context = yield* Layer.buildWithScope(shellLayer(directory), scope)
|
||||
const context = yield* Layer.buildWithScope(shellLayer(directory, server), scope)
|
||||
return {
|
||||
shell: Context.get(context, Shell.Service),
|
||||
bus: Context.get(context, Bus.Service),
|
||||
|
|
@ -96,6 +106,8 @@ export const daemonShells = Effect.fn(function* () {
|
|||
withShell,
|
||||
openShell,
|
||||
openLocation,
|
||||
// Where the server's daemon commands report.
|
||||
serverBus: Context.get(serverContext, Bus.Service),
|
||||
spawned: () => spawned,
|
||||
created: () => daemon.requests.filter((request) => request.op === "create_pipe"),
|
||||
signals: (id: number) =>
|
||||
|
|
|
|||
|
|
@ -306,42 +306,43 @@ describe("Shell on the process daemon", () => {
|
|||
}),
|
||||
)
|
||||
|
||||
timed("a closed Location and its replacement share a command's timeout and stop through the daemon", () =>
|
||||
timed("a closed Location and its replacement see the same commands, with one timer and one report each", () =>
|
||||
Effect.gen(function* () {
|
||||
const env = yield* daemonShells()
|
||||
const closing = yield* env.openLocation(env.directory)
|
||||
const backgrounded = yield* closing.shell.create({ shell: "/bin/sh", command: "sleep 60", timeout: 100 })
|
||||
const stopped = yield* closing.shell.create({ shell: "/bin/sh", command: "sleep 60" })
|
||||
const removed = yield* closing.shell.create({ shell: "/bin/sh", command: "sleep 60" })
|
||||
yield* closing.close
|
||||
const exits = { closed: 0, replacement: 0 }
|
||||
yield* closing.bus.listen((event) =>
|
||||
let exits = 0
|
||||
yield* env.serverBus.listen((event) =>
|
||||
Effect.sync(() => {
|
||||
if (event.type === "shell.exited") exits.closed++
|
||||
if (event.type === "shell.exited") exits++
|
||||
}),
|
||||
)
|
||||
const replacement = yield* env.openLocation(env.directory)
|
||||
yield* replacement.bus.listen((event) =>
|
||||
Effect.sync(() => {
|
||||
if (event.type === "shell.exited") exits.replacement++
|
||||
}),
|
||||
)
|
||||
expect((yield* replacement.shell.list()).map((item) => item.id)).toEqual([backgrounded.id, stopped.id])
|
||||
expect((yield* replacement.shell.list()).map((item) => item.id)).toEqual([
|
||||
backgrounded.id,
|
||||
stopped.id,
|
||||
removed.id,
|
||||
])
|
||||
|
||||
// The call that started it, still in the closed Location, moves it to the background and clears its timeout.
|
||||
yield* closing.shell.timeout(backgrounded.id, 0)
|
||||
const gets = () => env.daemon.ops.filter((op) => op === "get").length
|
||||
const before = gets()
|
||||
yield* TestClock.adjust("100 millis")
|
||||
// The replacement's timer checked the daemon and left the command alone.
|
||||
yield* until(() => gets() > before)
|
||||
expect(env.signals(1)).toEqual([])
|
||||
|
||||
// Cancelling its job in the closed Location stops it; the replacement reports why it ended.
|
||||
// Cancelling its job in the closed Location stops it; both see why it ended, and it is reported once.
|
||||
yield* closing.shell.stop(stopped.id, "killed")
|
||||
expect(yield* replacement.shell.wait(stopped.id)).toMatchObject({ status: "killed" })
|
||||
expect(yield* closing.shell.wait(stopped.id)).toMatchObject({ status: "killed" })
|
||||
yield* until(() => exits.replacement === 1)
|
||||
expect(exits.closed).toBe(0)
|
||||
yield* until(() => exits === 1)
|
||||
|
||||
// Removed through one, it is gone from both.
|
||||
yield* replacement.shell.remove(removed.id)
|
||||
expect(yield* closing.shell.get(removed.id).pipe(Effect.flip)).toBeInstanceOf(Shell.NotFoundError)
|
||||
// The server watched them all along: the replacement took nothing back and waited on nothing more.
|
||||
expect(env.daemon.ops.filter((op) => op === "wait")).toHaveLength(3)
|
||||
yield* replacement.close
|
||||
}),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -144,6 +144,7 @@ const shellPluginSupervisor = makeLocationNode({
|
|||
Detaching.node,
|
||||
Job.node,
|
||||
Shell.node,
|
||||
Shell.daemonNode,
|
||||
ShellSelect.node,
|
||||
Tool.node,
|
||||
],
|
||||
|
|
@ -151,6 +152,7 @@ const shellPluginSupervisor = makeLocationNode({
|
|||
|
||||
const nodes = LayerNode.group([
|
||||
PersistentPty.node,
|
||||
Shell.daemonNode,
|
||||
Database.node,
|
||||
Bus.node,
|
||||
Detaching.node,
|
||||
|
|
@ -239,6 +241,7 @@ const detachableShellSupervisor = makeLocationNode({
|
|||
Detaching.node,
|
||||
Job.node,
|
||||
Shell.node,
|
||||
Shell.daemonNode,
|
||||
ShellSelect.node,
|
||||
Tool.node,
|
||||
],
|
||||
|
|
@ -1261,6 +1264,7 @@ describe("ShellTool", () => {
|
|||
Detaching.node,
|
||||
Job.node,
|
||||
Shell.node,
|
||||
Shell.daemonNode,
|
||||
ShellSelect.node,
|
||||
Tool.node,
|
||||
],
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import { Instance } from "@opencode/core/instance/service"
|
|||
import { SessionTransfer } from "@opencode/core/session/transfer"
|
||||
import { InstructionEntry } from "@opencode/core/session/instruction-entry"
|
||||
import { ShellSelect } from "@opencode/core/shell/select"
|
||||
import { Shell } from "@opencode/core/shell"
|
||||
import { Detaching } from "@opencode/core/detaching"
|
||||
import { Job } from "@opencode/core/job"
|
||||
import { KV } from "@opencode/core/kv"
|
||||
|
|
@ -77,6 +78,8 @@ const applicationServiceNodes = [
|
|||
PermissionSaved.node,
|
||||
PtyTicket.node,
|
||||
PersistentPty.node,
|
||||
// Daemon-run shell commands belong to the server, so a Location closing never takes them down.
|
||||
Shell.daemonNode,
|
||||
Credential.node,
|
||||
WellKnown.node,
|
||||
ServerPairing.node,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue