Skip to content

Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.

Let start.sh and stop.sh see their own processes on Windows (#682)

Omkar Chebale70da1f4ed022
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 61eb6651..af774e5b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,19 @@ Newest first. `Unreleased` is what is on `main` and not yet tagged.
## Unreleased
+### `start.sh` and `stop.sh` see their own processes on Windows
+
+In Git Bash on Windows, which has no `lsof`, `pgrep` or `pkill`, every process and port lookup in
+the two scripts came back empty.
+- `bash scripts/stop.sh` reported the app, the routine worker and the API server as not running,
+ and left all three up.
+- `start.sh` could not see a port held by another process.
+- `start.sh` started another routine worker on every rerun, then reported that the one it had just
+ started "did not stay up".
+
+Where those tools are missing on Windows, the scripts now ask PowerShell, which ships with Windows.
+Everywhere the tools exist they are used exactly as before.
+
### A clone on Windows builds an image that starts
On Windows, where Git converts line endings by default, a clone checked every text file out with
diff --git a/scripts/processes.sh b/scripts/processes.sh
new file mode 100644
index 00000000..e4e9a41e
--- /dev/null
+++ b/scripts/processes.sh
@@ -0,0 +1,103 @@
+# Find and stop this stack's own processes, for `start.sh` and `stop.sh`.
+#
+# Sourced by both, after `require-bash.sh`. Not executable and not a script anybody runs.
+#
+# Both scripts ask two questions. What is listening on a port: `lsof`. Is one of our background
+# processes running: `pgrep -f`, and stop it with `pkill -f`. Git Bash on Windows has none of the
+# three, and its `ps` cannot see a native process's arguments, so there every answer came back empty
+# and every failure was silent. `stop.sh` said the app and server were "not running" and left them
+# up. `start.sh` could not see a port held by something else. It also started another routine worker
+# on every rerun, and then reported that the one it had just started "did not stay up".
+#
+# WHERE THE TOOL EXISTS, IT IS CALLED EXACTLY AS BEFORE. Only where it is missing on Windows does the
+# question go to PowerShell, which ships with Windows. Its answers are Windows process ids, which Git
+# Bash's `kill` does not accept, so stopping those goes through PowerShell as well.
+#
+# The port, pattern and ids reach PowerShell through the environment rather than on its command
+# line, so a search for a pattern never finds the PowerShell process doing the searching, the way
+# `pgrep -f` leaves itself out.
+
+# `type -P` rather than `command -v`: only a program on PATH counts, not a shell function or alias of
+# the same name that happens to be defined.
+openbot_windows_without() {
+ [ "${OS:-}" = "Windows_NT" ] && ! type -P "$1" >/dev/null 2>&1
+}
+
+# PowerShell writes CRLF. A trailing `\r` would make an empty answer non-empty and a pid unusable.
+openbot_powershell() {
+ powershell.exe -NoProfile -NonInteractive -Command "$1" 2>/dev/null | tr -d '\r' || true
+}
+
+# What is listening on a TCP port, as "name (address)", or nothing when the port is free.
+holder() {
+ if openbot_windows_without lsof; then
+ OPENBOT_PORT="$1" openbot_powershell '
+ Get-NetTCPConnection -State Listen -LocalPort $env:OPENBOT_PORT -ErrorAction SilentlyContinue |
+ Select-Object -First 1 |
+ ForEach-Object { "{0} ({1}:{2})" -f (Get-Process -Id $_.OwningProcess).ProcessName, $_.LocalAddress, $_.LocalPort }'
+ return 0
+ fi
+ lsof -nP -iTCP:"$1" -sTCP:LISTEN -Fcn 2>/dev/null | awk '/^c/{c=substr($0,2)} /^n/{print c" ("substr($0,2)")"; exit}' || true
+}
+
+# The ids of whatever is listening on a TCP port, one per line.
+port_pids() {
+ if openbot_windows_without lsof; then
+ OPENBOT_PORT="$1" openbot_powershell '
+ Get-NetTCPConnection -State Listen -LocalPort $env:OPENBOT_PORT -ErrorAction SilentlyContinue |
+ Select-Object -ExpandProperty OwningProcess -Unique'
+ return 0
+ fi
+ lsof -t -nP -iTCP:"$1" -sTCP:LISTEN 2>/dev/null || true
+}
+
+# `kill [-9] <pids>`, for ids from `port_pids`. Windows has no SIGTERM to send a console process, so
+# there both forms stop it outright. A stopped process takes a moment to go, so this waits for it,
+# up to five seconds, rather than let the next check see it still there.
+kill_pids() {
+ if openbot_windows_without lsof; then
+ OPENBOT_PIDS="$*" openbot_powershell '
+ $ids = @($env:OPENBOT_PIDS -split " " | Where-Object { $_ -match "^[0-9]+$" })
+ if ($ids) {
+ Stop-Process -Force -Id $ids -ErrorAction SilentlyContinue
+ Wait-Process -Id $ids -Timeout 5 -ErrorAction SilentlyContinue
+ }' >/dev/null
+ return 0
+ fi
+ kill "$@" 2>/dev/null || true
+}
+
+# The ids of processes whose command line is the pattern, such as `bun worker/src/index.ts`, and when
+# asked, stopped and waited for as `kill_pids` does. Windows records the executable as a path,
+# `C:\...\bun.exe` and sometimes quoted, so the first word is matched against the process name and
+# the rest against its command line.
+openbot_windows_matching() {
+ OPENBOT_PATTERN="$1" OPENBOT_STOP="${2:-}" openbot_powershell '
+ $name, $rest = $env:OPENBOT_PATTERN -split " ", 2
+ $ids = @(Get-CimInstance Win32_Process |
+ Where-Object { $_.Name -eq "$name.exe" -and $_.CommandLine -and $_.CommandLine.Contains(" $rest") } |
+ ForEach-Object { $_.ProcessId })
+ if ($ids -and $env:OPENBOT_STOP) {
+ Stop-Process -Force -Id $ids -ErrorAction SilentlyContinue
+ Wait-Process -Id $ids -Timeout 5 -ErrorAction SilentlyContinue
+ }
+ $ids'
+}
+
+# `pgrep -f <pattern>`: true when such a process is running.
+running() {
+ if openbot_windows_without pgrep; then
+ [ -n "$(openbot_windows_matching "$1")" ]
+ return
+ fi
+ pgrep -f "$1" >/dev/null 2>&1
+}
+
+# `pkill -f <pattern>`.
+stop_matching() {
+ if openbot_windows_without pkill; then
+ openbot_windows_matching "$1" stop >/dev/null
+ return 0
+ fi
+ pkill -f "$1" >/dev/null 2>&1 || true
+}
diff --git a/scripts/processes.test.ts b/scripts/processes.test.ts
new file mode 100644
index 00000000..cc95dffc
--- /dev/null
+++ b/scripts/processes.test.ts
@@ -0,0 +1,133 @@
+import { afterEach, beforeEach, describe, expect, test } from "bun:test";
+import { chmod, mkdtemp, rm, writeFile } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join, resolve } from "node:path";
+
+/**
+ * `scripts/processes.sh` on a Windows host that has no `lsof`, `pgrep` or `pkill`, run anywhere.
+ *
+ * PATH holds only the fakes below and `tr`, so a Linux machine with the real tools installed still
+ * takes the Windows path, and the fake `powershell.exe` records what it was asked. It answers with
+ * CRLF line endings, as the real one does. The fakes are `#!/bin/sh` because a `#!` line cannot hold
+ * the space in Windows' `C:\Program Files\Git\...\bash.exe`.
+ */
+
+const processesScript = resolve("scripts/processes.sh");
+const bash = Bun.which("bash") as string;
+const realTr = Bun.which("tr") as string;
+
+let directory: string;
+let bin: string;
+let log: string;
+
+async function writeExecutable(path: string, contents: string) {
+ await writeFile(path, contents);
+ await chmod(path, 0o755);
+}
+
+beforeEach(async () => {
+ directory = await mkdtemp(join(tmpdir(), "openbot-processes-"));
+ bin = join(directory, "bin");
+ log = join(directory, "calls.log");
+ await Bun.$`mkdir -p ${bin}`;
+ await writeExecutable(join(bin, "tr"), `#!/bin/sh\nexec "${realTr}" "$@"\n`);
+ await writeExecutable(
+ join(bin, "powershell.exe"),
+ `#!/bin/sh
+printf 'argv=%s|port=%s|pattern=%s|stop=%s|pids=%s\\n' "$*" "\${OPENBOT_PORT:-}" "\${OPENBOT_PATTERN:-}" "\${OPENBOT_STOP:-}" "\${OPENBOT_PIDS:-}" >> "$CALLS"
+case "$*" in
+ *ProcessName*) printf 'bun (127.0.0.1:%s)\\r\\n' "$OPENBOT_PORT" ;;
+ *-Unique*|*Win32_Process*) printf '4242\\r\\n' ;;
+esac
+`,
+ );
+});
+
+afterEach(async () => {
+ await rm(directory, { recursive: true, force: true });
+});
+
+// `OS` is always passed: Git Bash restores `OS=Windows_NT` from Windows when it is merely left out.
+async function run(expression: string, os = "Windows_NT") {
+ const child = Bun.spawn({
+ cmd: [bash, "-c", `. "${processesScript}"; ${expression}`],
+ env: { PATH: bin, CALLS: log, OS: os },
+ stdout: "pipe",
+ stderr: "pipe",
+ });
+ const [exitCode, stdout] = await Promise.all([
+ child.exited,
+ new Response(child.stdout).text(),
+ ]);
+ const calls = await Bun.file(log)
+ .text()
+ .catch(() => "");
+ return { exitCode, stdout, calls };
+}
+
+describe("processes.sh on Windows without lsof, pgrep or pkill", () => {
+ test("holder names the listener, with the port in the environment and no CR", async () => {
+ const result = await run('printf "[%s]" "$(holder 3001)"');
+
+ expect(result.stdout).toBe("[bun (127.0.0.1:3001)]");
+ expect(result.calls).toContain("|port=3001|");
+ expect(result.calls).not.toContain("argv=3001");
+ });
+
+ test("port_pids and kill_pids hand Windows ids to PowerShell", async () => {
+ const result = await run('kill_pids -9 $(port_pids 3001); echo "exit=$?"');
+
+ expect(result.stdout).toBe("exit=0\n");
+ expect(result.calls).toContain("|pids=-9 4242");
+ });
+
+ test("running finds the worker without its pattern on PowerShell's command line", async () => {
+ const result = await run(
+ 'running "bun worker/src/index.ts" && echo yes || echo no',
+ );
+
+ expect(result.stdout).toBe("yes\n");
+ expect(result.calls).toContain("|pattern=bun worker/src/index.ts|stop=|");
+ expect(result.calls.split("|port=")[0]).not.toContain(
+ "worker/src/index.ts",
+ );
+ });
+
+ test("stop_matching asks PowerShell to stop what matches", async () => {
+ const result = await run(
+ 'stop_matching "bun --env-file=../.env src/production-entry.ts"; echo "exit=$?"',
+ );
+
+ expect(result.stdout).toBe("exit=0\n");
+ expect(result.calls).toContain(
+ "|pattern=bun --env-file=../.env src/production-entry.ts|stop=stop|",
+ );
+ });
+});
+
+describe("processes.sh where the POSIX tools are the answer", () => {
+ test("a Windows host that has lsof, pgrep and pkill uses them, not PowerShell", async () => {
+ for (const tool of ["lsof", "pgrep", "pkill"]) {
+ await writeExecutable(
+ join(bin, tool),
+ `#!/bin/sh\nprintf '%s %s\\n' "${tool}" "$*" >> "$CALLS"\n`,
+ );
+ }
+
+ const result = await run(
+ 'holder 3001; running "bun worker/src/index.ts"; stop_matching "bun worker/src/index.ts"',
+ );
+
+ expect(result.calls).not.toContain("argv=");
+ expect(result.calls).toContain("lsof -nP -iTCP:3001 -sTCP:LISTEN -Fcn");
+ expect(result.calls).toContain("pgrep -f bun worker/src/index.ts");
+ expect(result.calls).toContain("pkill -f bun worker/src/index.ts");
+ });
+
+ test("off Windows, a missing lsof is still an empty answer, as before", async () => {
+ const result = await run('printf "[%s]" "$(holder 3001)"', "");
+
+ expect(result.stdout).toBe("[]");
+ expect(result.calls).toBe("");
+ });
+});
diff --git a/scripts/start-restart-guard.test.ts b/scripts/start-restart-guard.test.ts
index f2893f86..5d2638a1 100644
--- a/scripts/start-restart-guard.test.ts
+++ b/scripts/start-restart-guard.test.ts
@@ -83,6 +83,10 @@ async function runStartWithStaleServerProbe(
join(scripts, "require-bash.sh"),
await readFile("scripts/require-bash.sh"),
);
+ await writeFile(
+ join(scripts, "processes.sh"),
+ await readFile("scripts/processes.sh"),
+ );
await writeExecutable(
join(fakeBin, "lsof"),
diff --git a/scripts/start.sh b/scripts/start.sh
index 88af586a..237e8d61 100755
--- a/scripts/start.sh
+++ b/scripts/start.sh
@@ -6,6 +6,8 @@
# Before anything else, and before the `set` line below, which is itself bash-only: this file is
# bash, and being read by `sh` used to end it with exit 1 and no output at all. See that file.
. "$(dirname "$0")/require-bash.sh"
+# `holder`, `running` and `stop_matching`, which also work in Git Bash on Windows. See that file.
+. "$(dirname "$0")/processes.sh"
set -euo pipefail
@@ -137,10 +139,6 @@ green() { printf '\033[32m%s\033[0m\n' "$1"; }
red() { printf '\033[31m%s\033[0m\n' "$1"; }
info() { printf '\033[2m%s\033[0m\n' "$1"; }
-holder() {
- lsof -nP -iTCP:"$1" -sTCP:LISTEN -Fcn 2>/dev/null | awk '/^c/{c=substr($0,2)} /^n/{print c" ("substr($0,2)")"; exit}' || true
-}
-
# Does whatever holds this port answer as OpenBot, rather than merely answer?
#
# `curl -f` proves something is listening and returned 2xx. That is not the same claim, and the gap
@@ -214,8 +212,8 @@ wait_for() {
}
stop_server_processes_for_restart() {
- pkill -f "bun --env-file=../.env src/production-entry.ts" >/dev/null 2>&1 || true
- pkill -f "bun --env-file=../.env src/index.ts" >/dev/null 2>&1 || true
+ stop_matching "bun --env-file=../.env src/production-entry.ts"
+ stop_matching "bun --env-file=../.env src/index.ts"
}
echo
@@ -373,7 +371,7 @@ wait_for_openbot "$SERVER_PORT" server
# The old pattern matched those containers, the guard false-positived, and the worker silently never
# started. `bun worker/src/index.ts` matches nothing else in the repo. Running from `$ROOT` is safe:
# relative imports resolve from the importing file, not from the process's cwd.
-if ! pgrep -f "bun worker/src/index.ts" >/dev/null 2>&1; then
+if ! running "bun worker/src/index.ts"; then
WORKER_DATABASE_URL="$(setting DATABASE_URL postgres://openbot:openbot@localhost:5432/openbot)"
(cd "$ROOT" && \
DATABASE_URL="$WORKER_DATABASE_URL" \
@@ -382,7 +380,7 @@ if ! pgrep -f "bun worker/src/index.ts" >/dev/null 2>&1; then
bun worker/src/index.ts >"$LOGS/worker.log" 2>&1 &)
info " worker: started (routine sweep loop)"
sleep 1
- if ! pgrep -f "bun worker/src/index.ts" >/dev/null 2>&1; then
+ if ! running "bun worker/src/index.ts"; then
red " worker: did not stay up, check $LOGS/worker.log"
fi
else
diff --git a/scripts/stop.sh b/scripts/stop.sh
index d7833d6f..e7eeed1b 100755
--- a/scripts/stop.sh
+++ b/scripts/stop.sh
@@ -11,6 +11,9 @@
# Before anything else, and before the `set` line below, which is itself bash-only: this file is
# bash, and being read by `sh` used to end it with exit 1 and no output at all. See that file.
. "$(dirname "$0")/require-bash.sh"
+# `holder`, `port_pids`, `kill_pids`, `running` and `stop_matching`, which also work in Git Bash on
+# Windows. See that file.
+. "$(dirname "$0")/processes.sh"
set -euo pipefail
@@ -62,10 +65,6 @@ green() { printf '\033[32m%s\033[0m\n' "$1"; }
red() { printf '\033[31m%s\033[0m\n' "$1"; }
info() { printf '\033[2m%s\033[0m\n' "$1"; }
-holder() {
- lsof -nP -iTCP:"$1" -sTCP:LISTEN -Fcn 2>/dev/null | awk '/^c/{c=substr($0,2)} /^n/{print c" ("substr($0,2)")"; exit}' || true
-}
-
# Does whatever holds this port answer as OpenBot, rather than merely answer?
#
# The same question start.sh asks before starting, asked here for the opposite reason. Starting on
@@ -103,15 +102,15 @@ stop_port() {
red " Left it alone. Stop it yourself if it is in the way."
return 0
fi
- pids="$(lsof -t -nP -iTCP:"$port" -sTCP:LISTEN 2>/dev/null || true)"
+ pids="$(port_pids "$port")"
[ -z "$pids" ] && { info " $name: not running on $port"; return 0; }
# shellcheck disable=SC2086
- kill $pids 2>/dev/null || true
+ kill_pids $pids
sleep 2
- pids="$(lsof -t -nP -iTCP:"$port" -sTCP:LISTEN 2>/dev/null || true)"
+ pids="$(port_pids "$port")"
if [ -n "$pids" ]; then
# shellcheck disable=SC2086
- kill -9 $pids 2>/dev/null || true
+ kill_pids -9 $pids
fi
green " $name: stopped ($who)"
}
@@ -126,8 +125,8 @@ stop_port "$APP_PORT" app
info "2/4 Routine worker"
# The same pattern start.sh starts it with, and it has to stay that specific: a bare `bun
# src/index.ts` matches the server, computer and supervisor containers on a Linux host too.
-if pgrep -f "bun worker/src/index.ts" >/dev/null 2>&1; then
- pkill -f "bun worker/src/index.ts" || true
+if running "bun worker/src/index.ts"; then
+ stop_matching "bun worker/src/index.ts"
green " worker: stopped"
else
info " worker: not running"