summaryrefslogtreecommitdiffhomepage
path: root/packages/exec-backend/src
diff options
context:
space:
mode:
authorAdam Malczewski <[email protected]>2026-06-25 12:22:41 +0900
committerAdam Malczewski <[email protected]>2026-06-25 12:22:41 +0900
commit54db4583e66134010375a1fa94256f36034ffdff (patch)
treeec0bcd395d365741ed18e160f9b5842233051ba2 /packages/exec-backend/src
parent0b154bdad4f75a091db3ca46424abd17fbbc23ff (diff)
downloaddispatch-54db4583e66134010375a1fa94256f36034ffdff.tar.gz
dispatch-54db4583e66134010375a1fa94256f36034ffdff.zip
feat(ssh): wave 1 — ExecBackend + computer data model + runtime threading
Wave 1 of transparent SSH support (parallel owner-agents on disjoint packages, plus the orchestrator-authored kernel contract seam from wave 0): - packages/wire: + Computer/ComputerEntry (read-only view over ~/.ssh/config Host aliases) + Workspace.defaultComputerId (string|null, null=local). Types only; 3 conformance tests. - packages/exec-backend (NEW core extension): the ExecBackend abstraction (spawn + minimal fs surface) the bundled tools will program against instead of node:fs/child_process. LocalExecBackend wraps today's node calls (behavior-identical; node:fs-style .code errors). execBackendHandle + ExecBackendResolver (sync; computerId undefined -> local; set -> throws until the ssh package wires remote resolution in wave 5). 20 tests. - packages/kernel (runtime only): thread computerId through dispatch.ts + run-turn.ts exactly as cwd is threaded (opaque, forwarded to ToolExecuteContext; absent = local = byte-identical to today). +2 tests. - packages/conversation-store: computer (SSH alias) assignment + resolution mirroring cwd — WorkspaceRow.defaultComputerId + setWorkspaceDefaultComputerId + getComputerId/setComputerId/clearComputerId + getEffectiveComputer (override -> per-conv -> workspace default -> null/local). Fixes the 3 Workspace literal sites the new required wire field broke. +18 tests. - orchestrator: root tsconfig.json ref for exec-backend + bun install. Verified: tsc -b EXIT 0, biome clean, 1592 vitest pass (was 1549, +43). Refs: notes/ssh-support-plan.md (decisions §0.5/§13). No merge or push.
Diffstat (limited to 'packages/exec-backend/src')
-rw-r--r--packages/exec-backend/src/backend.test.ts63
-rw-r--r--packages/exec-backend/src/backend.ts78
-rw-r--r--packages/exec-backend/src/extension.ts48
-rw-r--r--packages/exec-backend/src/index.ts5
-rw-r--r--packages/exec-backend/src/local.test.ts199
-rw-r--r--packages/exec-backend/src/local.ts146
-rw-r--r--packages/exec-backend/src/service.ts27
7 files changed, 566 insertions, 0 deletions
diff --git a/packages/exec-backend/src/backend.test.ts b/packages/exec-backend/src/backend.test.ts
new file mode 100644
index 0000000..30458e7
--- /dev/null
+++ b/packages/exec-backend/src/backend.test.ts
@@ -0,0 +1,63 @@
+import { describe, expect, it } from "vitest";
+import type { DirEntry, ExecBackend, ExecResult, SpawnParams, StatResult } from "./backend.js";
+
+/**
+ * ExecBackend type conformance — a fake backend satisfies the interface.
+ * (Pure compile-time + runtime check; zero internal mocks.)
+ */
+describe("ExecBackend type conformance", () => {
+ it("a minimal fake satisfies the ExecBackend interface", () => {
+ const fake: ExecBackend = {
+ spawn: async (_params: SpawnParams): Promise<ExecResult> => ({
+ exitCode: 0,
+ timedOut: false,
+ aborted: false,
+ }),
+ readFile: async (_path: string): Promise<string> => "",
+ writeFile: async (_path: string, _content: string): Promise<void> => {},
+ stat: async (_path: string): Promise<StatResult> => ({ isFile: true, isDirectory: false }),
+ readdir: async (_path: string): Promise<readonly DirEntry[]> => [],
+ exists: async (_path: string): Promise<boolean> => true,
+ };
+
+ // Runtime sanity: every method is present and callable.
+ expect(typeof fake.spawn).toBe("function");
+ expect(typeof fake.readFile).toBe("function");
+ expect(typeof fake.writeFile).toBe("function");
+ expect(typeof fake.stat).toBe("function");
+ expect(typeof fake.readdir).toBe("function");
+ expect(typeof fake.exists).toBe("function");
+ });
+
+ it("ExecResult is { exitCode, timedOut, aborted }", () => {
+ const result: ExecResult = { exitCode: null, timedOut: true, aborted: false };
+ expect(result.exitCode).toBeNull();
+ expect(result.timedOut).toBe(true);
+ expect(result.aborted).toBe(false);
+ });
+
+ it("SpawnParams carries the shell-tool seam fields", () => {
+ const params: SpawnParams = {
+ command: "echo",
+ cwd: "/tmp",
+ signal: new AbortController().signal,
+ timeout: 1000,
+ onOutput: () => {},
+ };
+ expect(params.command).toBe("echo");
+ expect(params.timeout).toBe(1000);
+ });
+
+ it("StatResult distinguishes file vs directory", () => {
+ const fileStat: StatResult = { isFile: true, isDirectory: false };
+ const dirStat: StatResult = { isFile: false, isDirectory: true };
+ expect(fileStat.isFile && !fileStat.isDirectory).toBe(true);
+ expect(!dirStat.isFile && dirStat.isDirectory).toBe(true);
+ });
+
+ it("DirEntry carries name + isDirectory", () => {
+ const entry: DirEntry = { name: "sub", isDirectory: true };
+ expect(entry.name).toBe("sub");
+ expect(entry.isDirectory).toBe(true);
+ });
+});
diff --git a/packages/exec-backend/src/backend.ts b/packages/exec-backend/src/backend.ts
new file mode 100644
index 0000000..f6a807f
--- /dev/null
+++ b/packages/exec-backend/src/backend.ts
@@ -0,0 +1,78 @@
+/**
+ * ExecBackend — the transport-agnostic spawn + minimal filesystem surface.
+ *
+ * Tools (tool-shell, tool-read-file, tool-write-file, tool-edit-file) program
+ * against THIS abstraction instead of `node:fs` / `node:child_process` directly.
+ * Two implementations exist:
+ *
+ * - `LocalExecBackend` — wraps today's node calls (behavior-identical).
+ * - `SshExecBackend` — wraps ssh2 `exec` + `sftp` (added later by the `ssh`
+ * package; not this package's concern — but THIS interface is the seam it
+ * implements).
+ *
+ * The surface is deliberately SMALL (only what the bundled tools use) so a
+ * remote implementation is tractable. New operations are added here, not ad hoc.
+ *
+ * Resolved per-call from `ToolExecuteContext.computerId` via the injected
+ * `ExecBackendResolver` (see `./service.js`). `computerId` undefined → local.
+ *
+ * Error contract: `readFile`/`stat`/`readdir`/`writeFile` throw node:fs-style
+ * errors carrying a `.code` property (e.g. `"ENOENT"`) so the tools' existing
+ * error branches work unchanged. `exists` never throws (returns `false` on
+ * missing). The SshExecBackend maps ssh2 errors onto these same shapes.
+ */
+
+/** A spawned process's result. Mirrors tool-shell's `SpawnResult` exactly. */
+export interface ExecResult {
+ readonly exitCode: number | null;
+ readonly timedOut: boolean;
+ readonly aborted: boolean;
+}
+
+/** Parameters for spawning a shell command. Mirrors tool-shell's `SpawnShell` params. */
+export interface SpawnParams {
+ readonly command: string;
+ readonly cwd: string;
+ readonly signal: AbortSignal;
+ readonly timeout: number;
+ readonly onOutput: (data: string, stream: "stdout" | "stderr") => void;
+}
+
+/** Stat result — the subset read_file / write_file / edit_file need. */
+export interface StatResult {
+ readonly isFile: boolean;
+ readonly isDirectory: boolean;
+}
+
+/** A directory entry — the subset read_file lists. */
+export interface DirEntry {
+ readonly name: string;
+ readonly isDirectory: boolean;
+}
+
+/**
+ * The execution backend: spawn + a minimal filesystem surface.
+ * Tools program against THIS, never against `node:fs`. Resolved per-call from
+ * `ToolExecuteContext.computerId` via the injected resolver.
+ */
+export interface ExecBackend {
+ /** Run a shell command, streaming stdout/stderr. The shell-tool seam. */
+ readonly spawn: (params: SpawnParams) => Promise<ExecResult>;
+
+ // --- filesystem (the read_file / write_file / edit_file surface) ---
+
+ /** Read a file as utf8 text. Throws node:fs-style errors with `.code`. */
+ readonly readFile: (path: string) => Promise<string>;
+
+ /** Write utf8 text to a file. Throws on failure (e.g. missing parent dir). */
+ readonly writeFile: (path: string, content: string) => Promise<void>;
+
+ /** Stat a path. Throws node:fs-style errors with `.code` (e.g. `"ENOENT"`). */
+ readonly stat: (path: string) => Promise<StatResult>;
+
+ /** List directory entries. Throws node:fs-style errors with `.code`. */
+ readonly readdir: (path: string) => Promise<readonly DirEntry[]>;
+
+ /** Check existence without throwing (returns `false` when the path is missing). */
+ readonly exists: (path: string) => Promise<boolean>;
+}
diff --git a/packages/exec-backend/src/extension.ts b/packages/exec-backend/src/extension.ts
new file mode 100644
index 0000000..c07b7a8
--- /dev/null
+++ b/packages/exec-backend/src/extension.ts
@@ -0,0 +1,48 @@
+import type { Extension, Manifest } from "@dispatch/kernel";
+import type { ExecBackend } from "./backend.js";
+import { localExecBackend } from "./local.js";
+import type { ExecBackendResolver } from "./service.js";
+import { execBackendHandle } from "./service.js";
+
+export const manifest: Manifest = {
+ id: "exec-backend",
+ name: "Exec Backend",
+ version: "0.0.0",
+ apiVersion: "^0.1.0",
+ trust: "bundled",
+ activation: "eager",
+ contributes: { services: ["exec-backend/resolver"] },
+};
+
+/**
+ * The resolver provided by this extension.
+ *
+ * - `computerId` undefined → `LocalExecBackend` (today's local behavior).
+ * - `computerId` set → throws. Remote execution is wired by `host-bin` + the
+ * `ssh` package in a later wave (`SshExecBackend` implements the same
+ * `ExecBackend` interface). For now only the local path exists — failing
+ * loudly here is safer than silently running locally when remote was requested.
+ */
+function resolveBackend(computerId?: string): ExecBackend {
+ if (computerId === undefined) return localExecBackend;
+ throw new Error(
+ `Remote execution (computerId="${computerId}") is not yet configured. ` +
+ "The SSH backend will be wired by the ssh package.",
+ );
+}
+
+/**
+ * Factory: create the `exec-backend` core extension.
+ *
+ * `activate` provides the local-only `ExecBackendResolver` via the typed
+ * service handle. Remote resolution is added in a later wave.
+ */
+export function createExecBackendExtension(): Extension {
+ return {
+ manifest,
+ activate(host) {
+ const resolver: ExecBackendResolver = resolveBackend;
+ host.provideService(execBackendHandle, resolver);
+ },
+ };
+}
diff --git a/packages/exec-backend/src/index.ts b/packages/exec-backend/src/index.ts
new file mode 100644
index 0000000..3135f79
--- /dev/null
+++ b/packages/exec-backend/src/index.ts
@@ -0,0 +1,5 @@
+export type { DirEntry, ExecBackend, ExecResult, SpawnParams, StatResult } from "./backend.js";
+export { createExecBackendExtension, manifest } from "./extension.js";
+export { createLocalExecBackend, localExecBackend } from "./local.js";
+export type { ExecBackendResolver } from "./service.js";
+export { execBackendHandle } from "./service.js";
diff --git a/packages/exec-backend/src/local.test.ts b/packages/exec-backend/src/local.test.ts
new file mode 100644
index 0000000..5357d6f
--- /dev/null
+++ b/packages/exec-backend/src/local.test.ts
@@ -0,0 +1,199 @@
+import { writeFile as fsWriteFile, mkdir, mkdtemp, rm } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { afterEach, beforeEach, describe, expect, it } from "vitest";
+import type { ExecBackend } from "./backend.js";
+import { createLocalExecBackend, localExecBackend } from "./local.js";
+
+/**
+ * LocalExecBackend — integration tests against the OUTERMOST real edge
+ * (real fs/spawn). Zero internal mocks; no mocking of @dispatch/*.
+ */
+describe("LocalExecBackend", () => {
+ const backend: ExecBackend = createLocalExecBackend();
+ let tmpDir: string;
+
+ beforeEach(async () => {
+ tmpDir = await mkdtemp(join(tmpdir(), "exec-backend-test-"));
+ });
+
+ afterEach(async () => {
+ await rm(tmpDir, { recursive: true, force: true });
+ });
+
+ describe("spawn", () => {
+ it("runs a real `sh -c 'echo hi'` and returns exitCode 0 + captured stdout", async () => {
+ let output = "";
+ const result = await backend.spawn({
+ command: "echo hi",
+ cwd: tmpDir,
+ signal: AbortSignal.timeout(5000),
+ timeout: 5000,
+ onOutput: (data) => {
+ output += data;
+ },
+ });
+ expect(result.exitCode).toBe(0);
+ expect(result.timedOut).toBe(false);
+ expect(result.aborted).toBe(false);
+ expect(output).toContain("hi");
+ });
+
+ it("returns a non-zero exit code for a failing command", async () => {
+ const result = await backend.spawn({
+ command: "false",
+ cwd: tmpDir,
+ signal: AbortSignal.timeout(5000),
+ timeout: 5000,
+ onOutput: () => {},
+ });
+ expect(result.exitCode).toBe(1);
+ expect(result.aborted).toBe(false);
+ expect(result.timedOut).toBe(false);
+ });
+
+ it("streams stderr separately from stdout", async () => {
+ const streams: Array<{ data: string; stream: "stdout" | "stderr" }> = [];
+ const result = await backend.spawn({
+ command: "echo out; echo err 1>&2",
+ cwd: tmpDir,
+ signal: AbortSignal.timeout(5000),
+ timeout: 5000,
+ onOutput: (data, stream) => streams.push({ data, stream }),
+ });
+ expect(result.exitCode).toBe(0);
+ expect(streams.some((s) => s.stream === "stdout" && s.data.includes("out"))).toBe(true);
+ expect(streams.some((s) => s.stream === "stderr" && s.data.includes("err"))).toBe(true);
+ });
+
+ it("resolves with aborted: true when the signal fires", async () => {
+ const controller = new AbortController();
+ const promise = backend.spawn({
+ command: "sleep 30",
+ cwd: tmpDir,
+ signal: controller.signal,
+ timeout: 60_000,
+ onOutput: () => {},
+ });
+ // Let the sleep actually start.
+ await new Promise((r) => setTimeout(r, 300));
+ controller.abort();
+ const result = await promise;
+ expect(result.aborted).toBe(true);
+ expect(result.timedOut).toBe(false);
+ });
+
+ it("resolves with timedOut: true when the timeout elapses", async () => {
+ const start = Date.now();
+ const result = await backend.spawn({
+ command: "sleep 30",
+ cwd: tmpDir,
+ signal: AbortSignal.timeout(60_000),
+ timeout: 300,
+ onOutput: () => {},
+ });
+ const elapsed = Date.now() - start;
+ expect(result.timedOut).toBe(true);
+ expect(result.aborted).toBe(false);
+ // Should resolve shortly after the 300ms timeout, well under 30s.
+ expect(elapsed).toBeLessThan(10_000);
+ });
+ });
+
+ describe("stat", () => {
+ it("distinguishes file vs directory", async () => {
+ await fsWriteFile(join(tmpDir, "file.txt"), "hello");
+ await mkdir(join(tmpDir, "subdir"));
+
+ const fileStat = await backend.stat(join(tmpDir, "file.txt"));
+ expect(fileStat.isFile).toBe(true);
+ expect(fileStat.isDirectory).toBe(false);
+
+ const dirStat = await backend.stat(join(tmpDir, "subdir"));
+ expect(dirStat.isFile).toBe(false);
+ expect(dirStat.isDirectory).toBe(true);
+ });
+
+ it("throws ENOENT with .code for a missing path", async () => {
+ try {
+ await backend.stat(join(tmpDir, "nope"));
+ expect.fail("stat should have thrown for a missing path");
+ } catch (err: unknown) {
+ expect((err as NodeJS.ErrnoException).code).toBe("ENOENT");
+ }
+ });
+ });
+
+ describe("readFile / writeFile / readdir / exists round-trip", () => {
+ it("writes then reads a file (utf8 round-trip)", async () => {
+ const filePath = join(tmpDir, "round.txt");
+ await backend.writeFile(filePath, "round-trip content");
+ const content = await backend.readFile(filePath);
+ expect(content).toBe("round-trip content");
+ });
+
+ it("readdir lists entries with correct isDirectory flags", async () => {
+ await fsWriteFile(join(tmpDir, "a.txt"), "a");
+ await mkdir(join(tmpDir, "sub"));
+
+ const entries = await backend.readdir(tmpDir);
+ const names = entries.map((e) => e.name).sort();
+ expect(names).toEqual(["a.txt", "sub"]);
+
+ const sub = entries.find((e) => e.name === "sub");
+ expect(sub?.isDirectory).toBe(true);
+
+ const file = entries.find((e) => e.name === "a.txt");
+ expect(file?.isDirectory).toBe(false);
+ });
+
+ it("exists returns true for an existing file, false for a missing one", async () => {
+ const filePath = join(tmpDir, "exists.txt");
+ await fsWriteFile(filePath, "x");
+ expect(await backend.exists(filePath)).toBe(true);
+ expect(await backend.exists(join(tmpDir, "missing"))).toBe(false);
+ });
+
+ it("exists returns true for an existing directory", async () => {
+ await mkdir(join(tmpDir, "adir"));
+ expect(await backend.exists(join(tmpDir, "adir"))).toBe(true);
+ });
+
+ it("readFile throws ENOENT with .code for a missing file", async () => {
+ try {
+ await backend.readFile(join(tmpDir, "missing.txt"));
+ expect.fail("readFile should have thrown for a missing file");
+ } catch (err: unknown) {
+ expect((err as NodeJS.ErrnoException).code).toBe("ENOENT");
+ }
+ });
+
+ it("readdir throws ENOENT with .code for a missing directory", async () => {
+ try {
+ await backend.readdir(join(tmpDir, "missingdir"));
+ expect.fail("readdir should have thrown for a missing directory");
+ } catch (err: unknown) {
+ expect((err as NodeJS.ErrnoException).code).toBe("ENOENT");
+ }
+ });
+
+ it("writeFile throws an error with .code when the parent dir is missing", async () => {
+ try {
+ await backend.writeFile(join(tmpDir, "missing-parent", "child.txt"), "x");
+ expect.fail("writeFile should have thrown for a missing parent dir");
+ } catch (err: unknown) {
+ expect((err as NodeJS.ErrnoException).code).toBe("ENOENT");
+ }
+ });
+ });
+
+ describe("singleton", () => {
+ it("localExecBackend singleton satisfies ExecBackend and behaves identically", async () => {
+ expect(typeof localExecBackend.spawn).toBe("function");
+ expect(typeof localExecBackend.readFile).toBe("function");
+ const filePath = join(tmpDir, "singleton.txt");
+ await localExecBackend.writeFile(filePath, "singleton");
+ expect(await localExecBackend.readFile(filePath)).toBe("singleton");
+ });
+ });
+});
diff --git a/packages/exec-backend/src/local.ts b/packages/exec-backend/src/local.ts
new file mode 100644
index 0000000..ca88a11
--- /dev/null
+++ b/packages/exec-backend/src/local.ts
@@ -0,0 +1,146 @@
+import { spawn as nodeSpawn } from "node:child_process";
+import { access, readdir, readFile, stat, writeFile } from "node:fs/promises";
+import type { DirEntry, ExecBackend, ExecResult, SpawnParams, StatResult } from "./backend.js";
+
+/**
+ * LocalExecBackend — wraps `node:fs/promises` + `node:child_process`.
+ *
+ * Behavior is IDENTICAL to today's local tools:
+ * - `spawn` mirrors `realSpawn` in `packages/tool-shell/src/spawn.ts` — same
+ * `sh -c` invocation, detached process-group kill on abort/timeout,
+ * close-based resolution, and spawn-error → `{ exitCode: 1 }`.
+ * - `readFile`/`writeFile`/`stat`/`readdir` use the same `node:fs/promises`
+ * calls (utf8, `withFileTypes`) the tools make inline today, and throw the
+ * same node errors (carrying `.code`) so the tools' existing error branches
+ * work unchanged.
+ * - `exists` swallows all errors and returns `false` (an existence check).
+ *
+ * This factors the inline node calls out behind the `ExecBackend` interface so
+ * a remote (SshExecBackend) can swap in transparently. Stateless — safe to
+ * share as a singleton.
+ */
+export function createLocalExecBackend(): ExecBackend {
+ return {
+ spawn: localSpawn,
+
+ readFile: (path) => readFile(path, "utf8"),
+
+ writeFile: (path, content) => writeFile(path, content, "utf8"),
+
+ stat: async (path): Promise<StatResult> => {
+ const s = await stat(path);
+ return { isFile: s.isFile(), isDirectory: s.isDirectory() };
+ },
+
+ readdir: async (path): Promise<readonly DirEntry[]> => {
+ const entries = await readdir(path, { encoding: "utf8", withFileTypes: true });
+ return entries.map((e): DirEntry => ({ name: e.name, isDirectory: e.isDirectory() }));
+ },
+
+ exists: async (path): Promise<boolean> => {
+ try {
+ await access(path);
+ return true;
+ } catch {
+ return false;
+ }
+ },
+ };
+}
+
+/** Default singleton — stateless, safe to share across calls. */
+export const localExecBackend: ExecBackend = createLocalExecBackend();
+
+/**
+ * Run a shell command locally via `node:child_process`.
+ *
+ * Ported verbatim from `packages/tool-shell/src/spawn.ts` (`realSpawn`) so
+ * behavior is byte-identical: `sh -c <command>`, `detached: true` (own process
+ * group), process-group `SIGKILL` on abort/timeout so a backgrounded grandchild
+ * cannot hold the stdio pipes open, and resolve-once-with-cleanup to avoid
+ * listener/timer leaks.
+ */
+function localSpawn(params: SpawnParams): Promise<ExecResult> {
+ return new Promise<ExecResult>((resolve) => {
+ // detached: true puts the child in its own process group (pgid = child.pid).
+ // This lets us kill the entire group (child + any grandchildren that inherit
+ // the pipes) via process.kill(-pgid, "SIGKILL") on abort/timeout, so a
+ // backgrounded grandchild can't keep the stdio pipes open and stall the
+ // promise on child.on("close").
+ const child = nodeSpawn("sh", ["-c", params.command], {
+ cwd: params.cwd,
+ stdio: ["ignore", "pipe", "pipe"],
+ detached: true,
+ });
+
+ let settled = false;
+ let timedOut = false;
+ let timer: ReturnType<typeof setTimeout> | undefined;
+
+ /** Kill the entire child process group (best-effort — group may be gone). */
+ const killGroup = () => {
+ if (child.pid !== undefined) {
+ try {
+ process.kill(-child.pid, "SIGKILL");
+ } catch {
+ // Process group may already be gone — ignore.
+ }
+ }
+ };
+
+ /** Remove the abort listener and clear the timeout timer (no leaks). */
+ const cleanup = () => {
+ if (timer !== undefined) {
+ clearTimeout(timer);
+ timer = undefined;
+ }
+ params.signal.removeEventListener("abort", onAbort);
+ };
+
+ /** Resolve once, then clean up so listeners/timers never leak. */
+ const settle = (result: ExecResult) => {
+ if (settled) return;
+ settled = true;
+ cleanup();
+ resolve(result);
+ };
+
+ const onAbort = () => {
+ if (settled) return;
+ killGroup();
+ // Resolve immediately — do NOT wait for child.on("close"), which may
+ // never fire if a grandchild holds the pipes open.
+ settle({ exitCode: null, timedOut: false, aborted: true });
+ };
+ params.signal.addEventListener("abort", onAbort, { once: true });
+
+ timer = setTimeout(() => {
+ if (settled) return;
+ timedOut = true;
+ killGroup();
+ // Resolve immediately — same reasoning as abort.
+ settle({ exitCode: null, timedOut: true, aborted: false });
+ }, params.timeout);
+
+ child.stdout.on("data", (chunk: Buffer) => {
+ params.onOutput(chunk.toString(), "stdout");
+ });
+
+ child.stderr.on("data", (chunk: Buffer) => {
+ params.onOutput(chunk.toString(), "stderr");
+ });
+
+ // Normal-completion path: wait for "close" so all stdout/stderr is captured.
+ // If abort/timeout already settled, this is a no-op (settled === true).
+ child.on("close", (code) => {
+ settle({ exitCode: code, timedOut, aborted: false });
+ });
+
+ // Spawn error (e.g. bad cwd, sh not found). Kill the group just in case
+ // and resolve — never leave the promise pending.
+ child.on("error", () => {
+ killGroup();
+ settle({ exitCode: 1, timedOut: false, aborted: false });
+ });
+ });
+}
diff --git a/packages/exec-backend/src/service.ts b/packages/exec-backend/src/service.ts
new file mode 100644
index 0000000..81ea5fa
--- /dev/null
+++ b/packages/exec-backend/src/service.ts
@@ -0,0 +1,27 @@
+import { defineService } from "@dispatch/kernel";
+import type { ExecBackend } from "./backend.js";
+
+/**
+ * Resolve an `ExecBackend` for a given computer.
+ *
+ * - `computerId` **undefined** → local (today's behavior; `LocalExecBackend`).
+ * - `computerId` **set** → remote (SSH; wired by `host-bin` + the `ssh` package
+ * in a later wave — the `SshExecBackend` implements the same `ExecBackend`
+ * interface).
+ *
+ * The resolver is SYNCHRONOUS by design: it returns a backend whose methods are
+ * async, so any remote connection acquisition happens lazily inside the first
+ * backend method call, not at resolver-call time. This keeps the resolver
+ * side-effect-free — merely resolving a backend never opens a connection; only
+ * when a tool actually executes does the (remote) backend connect.
+ */
+export type ExecBackendResolver = (computerId?: string) => ExecBackend;
+
+/**
+ * Typed service handle for the `ExecBackend` resolver.
+ *
+ * The `exec-backend` extension provides this via `host.provideService`.
+ * Tool extensions resolve their per-call backend from it (injected at
+ * activation by `host-bin`).
+ */
+export const execBackendHandle = defineService<ExecBackendResolver>("exec-backend/resolver");