Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

fix(agent-controller): the installer falls back to ~/.local, and takes --data-dir (#703)

Louis Amaudruz517cee911d7b
diff --git a/console/packages/agent-controller/README.md b/console/packages/agent-controller/README.md
index 431c98b0c..2163103cf 100644
--- a/console/packages/agent-controller/README.md
+++ b/console/packages/agent-controller/README.md
@@ -48,9 +48,15 @@ curl -fsSL https://raw.githubusercontent.com/sandbox-quantum/switch/main/console
| sh -s -- --server https://switch.example.com --code <code>
```
-Without `--server` and `--code`, `install.sh` only installs. It installs with
-`npm install --global`, so npm's global prefix must be writable by the user
-(`npm config set prefix ~/.local` otherwise). The package can also be installed
+Without `--server` and `--code`, `install.sh` only installs. `--data-dir <dir>`
+keeps the controller's state in that folder rather than the default one.
+
+It installs with `npm install --global`, into npm's global prefix. When the user
+cannot write there (Node from the system's packages installs into `/usr`), it
+installs into `~/.local` instead, without changing npm's settings, and says so
+if `~/.local/bin` is not on the PATH; the service runs the controller by its
+full path either way. `switch-agent-controller update` installs into the same
+prefix the running controller came from. The package can also be installed
directly: `npm install --global <the release's .tgz URL>`.
From a checkout, build the workspace packages
diff --git a/console/packages/agent-controller/install.sh b/console/packages/agent-controller/install.sh
index 7fdd98c38..854573bec 100755
--- a/console/packages/agent-controller/install.sh
+++ b/console/packages/agent-controller/install.sh
@@ -10,9 +10,14 @@
# --server <url> --code <code> Enroll with a one-time code from the Machines page.
# --name <name> The machine's name in Switch (default: the host name).
# --description <text> What the machine is for, shown on the Machines page.
+# --data-dir <dir> Keep the controller's state here rather than in the default folder.
# --no-service Enroll, but do not install the service.
# --version <x.y.z> Install this release rather than the newest.
#
+# It installs where npm installs global packages. When this user cannot write
+# there (Node from the system's packages installs into /usr), it installs into
+# ~/.local instead, without changing npm's settings.
+#
# SWITCH_CONTROLLER_RELEASES_REPOSITORY (owner/name) installs from a fork's releases.
set -eu
@@ -28,6 +33,7 @@ SERVER=""
CODE=""
NAME=""
DESCRIPTION=""
+DATA_DIR=""
SERVICE=1
VERSION=""
while [ $# -gt 0 ]; do
@@ -36,6 +42,7 @@ while [ $# -gt 0 ]; do
--code) [ $# -ge 2 ] || fail "--code needs a code."; CODE="$2"; shift 2 ;;
--name) [ $# -ge 2 ] || fail "--name needs a name."; NAME="$2"; shift 2 ;;
--description) [ $# -ge 2 ] || fail "--description needs a text."; DESCRIPTION="$2"; shift 2 ;;
+ --data-dir) [ $# -ge 2 ] || fail "--data-dir needs a folder."; DATA_DIR="$2"; shift 2 ;;
--no-service) SERVICE=0; shift ;;
--version) [ $# -ge 2 ] || fail "--version needs x.y.z."; VERSION="$2"; shift 2 ;;
*) fail "Unknown option '$1'." ;;
@@ -87,14 +94,28 @@ fi
PREFIX="$(npm config get prefix)"
if [ ! -w "$PREFIX" ] && [ ! -w "$PREFIX/lib/node_modules" ]; then
- fail "npm installs global packages into $PREFIX, which this user cannot write. Point npm at a folder of yours, then run this again:
- npm config set prefix \"\$HOME/.local\" (and make sure \$HOME/.local/bin is on your PATH)"
+ echo "npm installs global packages into $PREFIX, which this user cannot write; installing into $HOME/.local instead."
+ PREFIX="$HOME/.local"
+ mkdir -p "$PREFIX"
fi
echo "Installing $URL"
-npm install --global "$URL"
+npm install --global --prefix "$PREFIX" "$URL"
BIN="$PREFIX/bin/switch-agent-controller"
[ -x "$BIN" ] || fail "npm did not install $BIN."
+case ":$PATH:" in
+ *":$PREFIX/bin:"*) ON_PATH=1 ;;
+ *) ON_PATH=0 ;;
+esac
+
+# The service runs the controller by its full path; this is only for typing it.
+path_note() {
+ if [ "$ON_PATH" = 0 ]; then
+ echo
+ echo "$PREFIX/bin is not on your PATH, so the switch-agent-controller command is not found by name. Add it with:"
+ echo " export PATH=\"$PREFIX/bin:\$PATH\""
+ fi
+}
if [ -z "$SERVER" ]; then
echo
@@ -102,15 +123,18 @@ if [ -z "$SERVER" ]; then
echo " switch-agent-controller enroll --server <Switch API URL> --code <code>"
echo " switch-agent-controller install-service"
echo "Check this machine with: switch-agent-controller doctor"
+ path_note
exit 0
fi
set -- enroll --server "$SERVER" --code "$CODE"
if [ -n "$NAME" ]; then set -- "$@" --name "$NAME"; fi
if [ -n "$DESCRIPTION" ]; then set -- "$@" --description "$DESCRIPTION"; fi
+if [ -n "$DATA_DIR" ]; then set -- "$@" --data-dir "$DATA_DIR"; fi
"$BIN" "$@"
if [ "$SERVICE" = 1 ]; then
- "$BIN" install-service
+ if [ -n "$DATA_DIR" ]; then "$BIN" install-service --data-dir "$DATA_DIR"; else "$BIN" install-service; fi
fi
echo
-"$BIN" doctor
+if [ -n "$DATA_DIR" ]; then "$BIN" doctor --data-dir "$DATA_DIR"; else "$BIN" doctor; fi
+path_note
diff --git a/console/packages/agent-controller/src/main.ts b/console/packages/agent-controller/src/main.ts
index 25e3c02e5..6383a44cb 100644
--- a/console/packages/agent-controller/src/main.ts
+++ b/console/packages/agent-controller/src/main.ts
@@ -63,7 +63,7 @@ import {
} from './service';
import { contractPlatform, mapAgentProcess, PathProviderLocator } from './status';
import { ControllerStore } from './store';
-import { isNewer, latestRelease, releasesRepository } from './update';
+import { installPrefix, isNewer, latestRelease, releasesRepository } from './update';
export const VERSION: string = packageJson.version;
@@ -594,13 +594,20 @@ async function updateCommand(args: string[]): Promise<number> {
}
process.stdout.write(`Installing ${latest.version} from ${latest.packageUrl}\n`);
const code = await new Promise<number | null>((done, fail) => {
- const child = spawn('npm', ['install', '--global', latest.packageUrl], { stdio: 'inherit' });
+ const prefix = installPrefix(cliPath());
+ const npmArgs = [
+ 'install',
+ '--global',
+ ...(prefix ? ['--prefix', prefix] : []),
+ latest.packageUrl,
+ ];
+ const child = spawn('npm', npmArgs, { stdio: 'inherit' });
child.once('error', fail);
child.once('exit', done);
});
if (code !== 0)
throw new Error(
- `npm install exited with ${code}. If npm needs root to install globally, point it at a folder of yours (npm config set prefix ~/.local) and install again.`
+ `npm install exited with ${code}. If it could not write where the controller is installed, install it again with the installer, which falls back to ~/.local.`
);
const dataDir = resolveDataDir(values['data-dir']);
if ((await serviceState(dataDir, runSecretCommand)) === 'running') {
diff --git a/console/packages/agent-controller/src/update.test.ts b/console/packages/agent-controller/src/update.test.ts
index b408b22cf..85781aa68 100644
--- a/console/packages/agent-controller/src/update.test.ts
+++ b/console/packages/agent-controller/src/update.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
-import { isNewer, latestRelease, releasesRepository } from './update';
+import { installPrefix, isNewer, latestRelease, releasesRepository } from './update';
function release(tag: string, extra: Partial<{ draft: boolean; prerelease: boolean }> = {}) {
const version = tag.replace('switch-agent-controller-v', '');
@@ -66,3 +66,20 @@ describe('releasesRepository', () => {
).toThrow(/owner\/name/);
});
});
+
+describe('installPrefix', () => {
+ it('is the folder npm installed the controller under', () => {
+ expect(
+ installPrefix(
+ '/home/ada/.local/lib/node_modules/@switch-console/agent-controller/dist/cli.mjs'
+ )
+ ).toBe('/home/ada/.local');
+ expect(
+ installPrefix('/usr/lib/node_modules/@switch-console/agent-controller/dist/cli.mjs')
+ ).toBe('/usr');
+ });
+
+ it('is null outside an npm global install', () => {
+ expect(installPrefix('/work/switch/console/packages/agent-controller/dist/cli.mjs')).toBeNull();
+ });
+});
diff --git a/console/packages/agent-controller/src/update.ts b/console/packages/agent-controller/src/update.ts
index f4368d68f..703877443 100644
--- a/console/packages/agent-controller/src/update.ts
+++ b/console/packages/agent-controller/src/update.ts
@@ -15,6 +15,18 @@ export function releasesRepository(env: NodeJS.ProcessEnv): string {
return named;
}
+/**
+ * The npm prefix this controller was installed under, from the file it runs
+ * (`<prefix>/lib/node_modules/<package>/…`), so an update lands where the
+ * installer put it, `~/.local` included, whatever npm's own prefix is. Null
+ * when it does not run from an npm global install, a checkout say.
+ */
+export function installPrefix(cliFile: string): string | null {
+ const marker = '/lib/node_modules/';
+ const at = cliFile.lastIndexOf(marker);
+ return at > 0 ? cliFile.slice(0, at) : null;
+}
+
/** The file a release carries for `npm install -g`. */
export function packageAssetName(version: string): string {
return `switch-agent-controller-${version}.tgz`;