From e4d1d95d8e7602e6b375534403141dff249b4e85 Mon Sep 17 00:00:00 2001 From: Aly Raffauf Date: Mon, 16 Nov 2026 19:24:21 -0500 Subject: [PATCH] refactor(sdk): run local commands through the host's runner sdk/local_command found programs on PATH and spawned them itself. Keep the SDK half to the contract: the Command a caller asks to run, its Outcome, the Runner type, and collect, which reads a started command's output up to a cap and kills it at its deadline. Core's harness/local_run finds the program and starts it with the usage feed's environment allowlist, then collects through the SDK. location, vcs and worktree run their commands through it. Behaviour is unchanged. --- robot-docs/extensions.md | 10 +++- src/albedo/harness/local_run.gleam | 36 +++++++++++ src/albedo/harness/location.gleam | 13 ++-- src/albedo/harness/sdk/local_command.gleam | 69 ++++++++++------------ src/albedo/harness/vcs.gleam | 13 ++-- src/albedo/harness/worktree.gleam | 13 ++-- 6 files changed, 95 insertions(+), 59 deletions(-) create mode 100644 src/albedo/harness/local_run.gleam diff --git a/robot-docs/extensions.md b/robot-docs/extensions.md index e3bb24bfe..0ede8a8ac 100644 --- a/robot-docs/extensions.md +++ b/robot-docs/extensions.md @@ -270,9 +270,13 @@ The SDK also provides these helpers. scanner, frontmatter policy, and prompt mentions. `scan_at` reads project skills from the directory its `readable` argument answers. See [skills](skills.md). -- [`sdk/file_fingerprint`](../src/albedo/harness/sdk/file_fingerprint.gleam) and - [`sdk/local_command`](../src/albedo/harness/sdk/local_command.gleam): A file's - content fingerprint, and bounded local commands. +- [`sdk/file_fingerprint`](../src/albedo/harness/sdk/file_fingerprint.gleam): A + file's content fingerprint. +- [`sdk/local_command`](../src/albedo/harness/sdk/local_command.gleam): A + bounded local command, how it ended, and how its output is collected. The host + runs it: core's [`local_run`](../src/albedo/harness/local_run.gleam) finds the + program on `PATH` and starts it with the usage feed's environment allowlist. + The SDK starts no process itself. - [`sdk/helper`](../src/albedo/harness/sdk/helper.gleam): Python helper subprocess execution with explicit outcomes. - [`sdk/renderer`](../src/albedo/harness/sdk/renderer.gleam): Runs of the diff --git a/src/albedo/harness/local_run.gleam b/src/albedo/harness/local_run.gleam new file mode 100644 index 000000000..779344197 --- /dev/null +++ b/src/albedo/harness/local_run.gleam @@ -0,0 +1,36 @@ +//// The host's runner for short local commands (`sdk/local_command`): it +//// finds the program on `PATH` and starts it in the command's directory +//// with the usage feed's environment allowlist, then collects its output +//// with the SDK's `collect`. Core callers run commands through `run`, and +//// the worktree service hands `run` to the worktree providers it calls. + +import albedo/harness/sdk/local_command.{type Command, type Outcome} +import albedo_ports +import albedo_ports/subprocess + +/// Run `command`. `Error(Nil)` when its program is not on `PATH`. +pub fn run(command: Command) -> Result(Outcome, Nil) { + case albedo_ports.find_executable(command.program) { + Error(Nil) -> Error(Nil) + Ok(executable) -> { + let merge = case command.merge_stderr { + True -> [albedo_ports.StderrToStdout] + False -> [] + } + let settings = [ + albedo_ports.Args(command.args), + albedo_ports.Cd(command.cwd), + albedo_ports.ExitStatus, + albedo_ports.Binary, + albedo_ports.Hide, + albedo_ports.Env(subprocess.command_environment()), + ..merge + ] + case albedo_ports.start(executable, settings) { + Error(reason) -> Ok(local_command.Failed(reason)) + Ok(port) -> + Ok(local_command.collect(port, command.cap, command.timeout_ms)) + } + } + } +} diff --git a/src/albedo/harness/location.gleam b/src/albedo/harness/location.gleam index 56e78f402..9e9fd1f4b 100644 --- a/src/albedo/harness/location.gleam +++ b/src/albedo/harness/location.gleam @@ -5,6 +5,7 @@ //// were. A remote one is `[user@]host:/abs/path`; its path must be absolute, //// so one remote directory never gets two keys. +import albedo/harness/local_run import albedo/harness/sdk/local_command import albedo/harness/ssh import gleam/dynamic/decode @@ -249,14 +250,14 @@ fn ssh_user(host: String) -> Result(String, Nil) { fn configured_user(host: String) -> Result(String, Nil) { case - local_command.run( - "ssh", - ["-G", host], - "/", - 2000, + local_run.run(local_command.Command( + program: "ssh", + args: ["-G", host], + cwd: "/", + timeout_ms: 2000, merge_stderr: False, cap: 8_388_608, - ) + )) { Ok(local_command.Exited(0, output)) -> output diff --git a/src/albedo/harness/sdk/local_command.gleam b/src/albedo/harness/sdk/local_command.gleam index 01b8b69b1..4ffd2b64d 100644 --- a/src/albedo/harness/sdk/local_command.gleam +++ b/src/albedo/harness/sdk/local_command.gleam @@ -1,14 +1,31 @@ -//// Short local commands such as git, jj and `ssh -G`, run with the usage -//// feed's environment allowlist, a deadline, and a stdout cap, in the -//// directory they are about. Callers decide what each outcome means. +//// Short local commands such as git, jj and `ssh -G`: what a caller asks to +//// run, how the run ended, and how a run's output is collected. Running one +//// is the host's: its runner (`albedo/harness/local_run`) finds the program +//// on `PATH`, starts it with the usage feed's environment allowlist in the +//// directory it is about, and collects its output with `collect`. Code that +//// runs commands receives the host's `Runner`. Callers decide what each +//// outcome means. import albedo_clock import albedo_ports -import albedo_ports/subprocess import gleam/bit_array import gleam/erlang/port.{type Port} import gleam/list +/// A command to run: `program` from `PATH` with `args`, in `cwd`, killed at +/// `timeout_ms`. With `merge_stderr` its stderr joins the output. Output +/// past `cap` bytes is discarded; the chunk that crosses it is kept. +pub type Command { + Command( + program: String, + args: List(String), + cwd: String, + timeout_ms: Int, + merge_stderr: Bool, + cap: Int, + ) +} + /// How a command that was found on `PATH` ended. pub type Outcome { /// It exited with `status`, having written `output` (possibly capped). @@ -19,40 +36,16 @@ pub type Outcome { Failed(reason: String) } -/// Run `program` from `PATH` in `cwd`. `Error(Nil)` when it is not on -/// `PATH`. With `merge_stderr` its stderr joins the output. Output past -/// `cap` bytes is discarded; the chunk that crosses it is kept. -pub fn run( - program: String, - args: List(String), - cwd: String, - timeout_ms: Int, - merge_stderr merge_stderr: Bool, - cap cap: Int, -) -> Result(Outcome, Nil) { - case albedo_ports.find_executable(program) { - Error(Nil) -> Error(Nil) - Ok(executable) -> { - let merge = case merge_stderr { - True -> [albedo_ports.StderrToStdout] - False -> [] - } - let settings = [ - albedo_ports.Args(args), - albedo_ports.Cd(cwd), - albedo_ports.ExitStatus, - albedo_ports.Binary, - albedo_ports.Hide, - albedo_ports.Env(subprocess.command_environment()), - ..merge - ] - case albedo_ports.start(executable, settings) { - Error(reason) -> Ok(Failed(reason)) - Ok(port) -> - Ok(drain(port, [], 0, cap, albedo_clock.monotonic_ms() + timeout_ms)) - } - } - } +/// Runs a command, or answers `Error(Nil)` when its program is not on +/// `PATH`. The host supplies it. +pub type Runner = + fn(Command) -> Result(Outcome, Nil) + +/// The outcome of the command `port` runs, opened with `exit_status` and +/// `binary`: its output up to `cap` bytes once it exits, or `TimedOut` after +/// killing it when it is still running `timeout_ms` from now. +pub fn collect(port: Port, cap: Int, timeout_ms: Int) -> Outcome { + drain(port, [], 0, cap, albedo_clock.monotonic_ms() + timeout_ms) } fn drain( diff --git a/src/albedo/harness/vcs.gleam b/src/albedo/harness/vcs.gleam index 28efbfabf..d42fbe755 100644 --- a/src/albedo/harness/vcs.gleam +++ b/src/albedo/harness/vcs.gleam @@ -10,6 +10,7 @@ //// over ssh collected, running the commands `plan` names. Either way the //// answers are read by the same code below. +import albedo/harness/local_run import albedo/harness/sdk/local_command import gleam/int import gleam/list @@ -433,14 +434,14 @@ fn run( timeout_ms: Int, ) -> Result(String, Nil) { case - local_command.run( - program, - args, - cwd, - timeout_ms, + local_run.run(local_command.Command( + program:, + args:, + cwd:, + timeout_ms:, merge_stderr: False, cap: stdout_cap, - ) + )) { Ok(local_command.Exited(0, output)) -> Ok(output) _ -> Error(Nil) diff --git a/src/albedo/harness/worktree.gleam b/src/albedo/harness/worktree.gleam index 5f18f5ed7..5d1008555 100644 --- a/src/albedo/harness/worktree.gleam +++ b/src/albedo/harness/worktree.gleam @@ -1,4 +1,5 @@ import albedo/daemon/bus +import albedo/harness/local_run import albedo/harness/location import albedo/harness/sdk/follow import albedo/harness/sdk/local_command @@ -853,14 +854,14 @@ pub fn exec( timeout_ms: Int, ) -> Result(String, String) { case - local_command.run( - program, - args, - cwd, - timeout_ms, + local_run.run(local_command.Command( + program:, + args:, + cwd:, + timeout_ms:, merge_stderr: True, cap: output_cap, - ) + )) { Error(Nil) -> Error(program <> " is not installed or not on PATH") Ok(local_command.Exited(0, output)) -> Ok(output) -- knotmirror 0.0.1