diff --git a/docs/architecture-invariants.md b/docs/architecture-invariants.md index fdff9088..28f3a394 100644 --- a/docs/architecture-invariants.md +++ b/docs/architecture-invariants.md @@ -1445,7 +1445,7 @@ Tests that delete case trees go through `safeRmHomeTree()` (`test/mocks`), which **`install.sh`** (repo root) is the public entry point: `curl -fsSL | bash` installs Node/tmux/git/build tools if missing, clones to `~/.codeman/app`, builds, and offers a systemd/launchd service. Since installer v2 (2026-09-20) it is **look, ask, work, done**: `preflight_detect` prints what is on the machine, every human step runs BEFORE the build (one consent for all missing packages, ONE sudo prompt kept warm by `sudo_session_start`, the AI CLI menu, the Tailscale login/operator/HTTPS-toggle preflight), then the clone/`npm install`/build/service/serve run unattended behind `run_step` spinners (output in `~/.codeman/install.log`, tail shown on failure), and `print_done_screen` ends on the URL with a terminal QR code (the `qrcode` package Codeman already ships). Design + verification record: `docs/installer-v2-plan.md`. -The network-access question is 3-way: **Tailscale** (loopback bind + `tailscale serve`, curl-verified end-to-end), **LAN** (0.0.0.0 + password prompt), or **local-only**; it preserves the existing binding on re-runs via `read_existing_binding()`, which also reads back `CODEMAN_BASE_URL`/`CODEMAN_PORT` from the unit, and the flag/env PRESET paths keep an existing password too (`${CODEMAN_PASSWORD:-$EXISTING_PASSWORD}`: `--lan --service` on a password-protected unit used to rewrite it open with the unauthenticated ack, found in review 2026-09-21); `--password`/`--port` flip `RECONFIGURE` so they reach the unit instead of taking the quiet update path. +The network-access question is 3-way: **Tailscale** (loopback bind + `tailscale serve`, curl-verified end-to-end), **LAN** (0.0.0.0 + password prompt), or **local-only**; it preserves the existing binding on re-runs via `read_existing_binding()`, which also reads back `CODEMAN_BASE_URL`/`CODEMAN_PORT` and `CODEMAN_MULTIUSER` from the unit (both setup functions write a set `CODEMAN_MULTIUSER=1` back; systemd `Environment=` lines go through `read_systemd_environment()`, which unquotes the way systemd does, so a hand-written `KEY="value"` reads as `value`), and the flag/env PRESET paths keep an existing password too (`${CODEMAN_PASSWORD:-$EXISTING_PASSWORD}`: `--lan --service` on a password-protected unit used to rewrite it open with the unauthenticated ack, found in review 2026-09-21); `--password`/`--port` flip `RECONFIGURE` so they reach the unit instead of taking the quiet update path. ⚠️ The env a hand-started `codeman web` needs (host, password, ack, base URL, port) is composed in ONE place, `start_command_hint` for the done screen's Start line and `export_bind_env` for main()'s `exec` branch, so the two cannot disagree; `stop_background_helpers` runs right before that `exec`, because exec skips the EXIT trap and the sudo keepalive (keyed on `$$`, which becomes the server's pid) would otherwise refresh the sudo timestamp for the server's whole life. Ctrl+C in the HTTPS-toggle poll is trapped for the poll only and skips Tailscale for the run rather than killing the installer. diff --git a/install.sh b/install.sh index fbed753d..dc226e79 100755 --- a/install.sh +++ b/install.sh @@ -2341,20 +2341,31 @@ xml_unescape() { } # Read one assignment from the unit's Environment= directives. Each line can -# hold several assignments, quoted as whole words. Later assignments and empty -# directives override earlier values. The unit is read as data. +# hold several assignments. Quotes are removed wherever they sit in a word, as +# systemd does (KEY="a b" and "KEY=a b" are the same assignment); backslash +# escapes inside quotes are kept for systemd_env_unescape, and a line whose +# quote never closes assigns nothing. Later assignments and empty directives +# override earlier values. The unit is read as data. read_systemd_environment() { awk -v key="$2=" ' /^[[:space:]]*Environment[[:space:]]*=/ { - sub(/^[[:space:]]*Environment[[:space:]]*=[[:space:]]*/, "") - if ($0 == "") value = "" - while (match($0, /^("([^"\\]|\\.)*"|\047([^\047\\]|\\.)*\047|[^[:space:]"\047][^[:space:]]*)([[:space:]]|$)/)) { - word = substr($0, 1, RLENGTH) - $0 = substr($0, RLENGTH + 1) - sub(/^[[:space:]]+/, "") - sub(/[[:space:]]$/, "", word) - if (word ~ /^["\047]/) word = substr(word, 2, length(word) - 2) - if (index(word, key) == 1) value = substr(word, length(key) + 1) + line = $0 + sub(/^[[:space:]]*Environment[[:space:]]*=[[:space:]]*/, "", line) + if (line == "") { value = ""; next } + n = length(line); word = ""; inword = 0; q = "" + for (i = 1; i <= n + 1; i++) { + c = (i <= n) ? substr(line, i, 1) : " " + if (q != "") { + if (c == "\\" && i < n) { word = word c substr(line, i + 1, 1); i++; continue } + if (c == q) { q = ""; continue } + word = word c; continue + } + if (c == "\"" || c == "\047") { q = c; inword = 1; continue } + if (c ~ /[[:space:]]/) { + if (inword && index(word, key) == 1) value = substr(word, length(key) + 1) + word = ""; inword = 0; continue + } + word = word c; inword = 1 } } END { print value } @@ -4460,7 +4471,11 @@ print_done_screen() { fi if [[ "$EXISTING_MULTIUSER" != "1" ]]; then echo -e " ${BOLD}Users${NC} Create the first admin with ${CYAN}codeman users add --admin${NC} before enabling multi-user mode." - echo -e " To start manually, run ${CYAN}$(start_command_hint) --multiuser${NC}" + # Only where no service runs: next to a working service this line would start a + # second server on the same data dir and tmux socket. + if [[ "$launch" != "2" || "$service_ok" != "true" ]]; then + echo -e " To start manually, run ${CYAN}$(start_command_hint) --multiuser${NC}" + fi if [[ "$launch" == "2" ]]; then case "$svc" in systemd) diff --git a/test/install-sh-invariants.test.ts b/test/install-sh-invariants.test.ts index 7561d82c..ed3c0c01 100644 --- a/test/install-sh-invariants.test.ts +++ b/test/install-sh-invariants.test.ts @@ -994,6 +994,19 @@ describe('install.sh multi-user guidance and reconfiguration', () => { expect(output).toContain('[Service]'); expect(output).toContain('systemctl --user daemon-reload && systemctl --user restart codeman-web'); expect(output).not.toContain('EnvironmentVariables'); + // Next to a working service the manual start line would start a second server on the + // same data dir and tmux socket, so it is not printed there. + expect(output).not.toContain('codeman web --multiuser'); + }); + + it('keeps the manual start line when the service setup failed', () => { + const output = drive('SERVICE_TYPE=systemd; read_existing_binding; print_done_screen 2 false', { + path: unit, + content: '[Service]\n', + }); + expect(output).toContain('The service was set up but is not running yet'); + expect(output).toContain('codeman web --multiuser'); + expect(output).toContain('Environment=CODEMAN_MULTIUSER=1'); }); it('prints the exact LaunchAgent edit and unload/load sequence', () => { @@ -1006,6 +1019,7 @@ describe('install.sh multi-user guidance and reconfiguration', () => { expect(guidance.indexOf(unload)).toBeGreaterThan(-1); expect(guidance.indexOf(load)).toBeGreaterThan(guidance.indexOf(unload)); expect(guidance).not.toContain('daemon-reload'); + expect(output).not.toContain('codeman web --multiuser'); }); it.each([ @@ -1014,6 +1028,9 @@ describe('install.sh multi-user guidance and reconfiguration', () => { ['systemd', unit, ' Environment = CODEMAN_MULTIUSER=1 ', true], ['systemd', unit, 'Environment="NOTE=two words" CODEMAN_MULTIUSER=1 NODE_ENV=production', true], ['systemd', unit, "Environment='CODEMAN_MULTIUSER=1' NODE_ENV=production", true], + // systemd unquotes anywhere in a word, so a hand-quoted VALUE is the same setting. + ['systemd', unit, 'Environment=CODEMAN_MULTIUSER="1"', true], + ['systemd', unit, "Environment=CODEMAN_MULTIUSER='true'", true], ['systemd', unit, 'Environment=CODEMAN_MULTIUSER=1 CODEMAN_MULTIUSER=0', false], ['systemd', unit, 'Environment=CODEMAN_MULTIUSER=1\nEnvironment=CODEMAN_MULTIUSER=false', false], ['systemd', unit, 'Environment=CODEMAN_MULTIUSER=1\nEnvironment=', false], @@ -1072,6 +1089,31 @@ describe('install.sh multi-user guidance and reconfiguration', () => { expect(output).toContain('Environment="CODEMAN_PASSWORD=test password"'); expect(output).not.toContain('CODEMAN_MULTIUSER'); }); + + it.each([ + ['Environment=CODEMAN_PASSWORD="my pass"', 'Environment="CODEMAN_PASSWORD=my pass"'], + ['Environment=CODEMAN_PASSWORD="hunter2"', 'Environment="CODEMAN_PASSWORD=hunter2"'], + ['Environment="CODEMAN_PASSWORD=a\\"b\\\\c"', 'Environment="CODEMAN_PASSWORD=a\\"b\\\\c"'], + ])('keeps a hand-written password across a re-run: %s', (written, kept) => { + const output = drive( + ` + parse_flags --port 4000 --service + choose_network_binding >/dev/null + setup_systemd_service >/dev/null + cat "$HOME/${unit}" + `, + { path: unit, content: ['[Service]', 'Environment=CODEMAN_HOST=0.0.0.0', written].join('\n') } + ); + expect(output.split('\n')).toContain(kept); + }); + + it('assigns nothing from a line whose quote never closes', () => { + const output = drive('read_existing_binding; echo "multi=$EXISTING_MULTIUSER"', { + path: unit, + content: '[Service]\nEnvironment="CODEMAN_MULTIUSER=1\n', + }); + expect(output).toContain('multi=0'); + }); }); it('install.sh never contains the unauthenticated tunnel opt-in', () => {