Files
alzeo/tests/test_install_agent_keeps.py
Hasan BasbunarandClaude Opus 5.5 532a94924d cockpit: the agent's relay on a LAN beside the tailnet, ::1, its own client key, and no login from another site
The Agent view's relay takes several addresses (AGENT_BIND=tailscale,192.168.1.10): the tailnet's and a LAN's, each with a
state of its own. The cockpit and the relay serve the loopback's IPv6 address where the box has one. The proxy asks this
box's cockpit on its own loopback, where it always answers, so a pair's tile speaks while Tailscale is down. Where the proxy
knows its clients by their keys, Smoke and Decide go with the key its file labels "cockpit", and say what to add when it has
none, instead of failing on a 401. A login posted by another site's page is refused before it counts as one of the five
tries a minute. Decision 1623.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 13:12:08 +01:00

328 lines
18 KiB
Python

#!/usr/bin/env python3
"""A re-run of install-agent.sh keeps the Agent tab as it was installed.
install.sh re-runs dashboard/install-agent.sh on every update. The script took
OPENCODE_PORT, AGENT_PORT and AGENT_BIND from the environment or its defaults (4096,
30091, and an address derived from the cockpit's), and handed them to
install-dashboard.sh, which writes them into the cockpit's unit: a box whose relay was
installed on its own port or address was put back on the defaults at the next update,
and without tailscale its Agent tab went dark. The service PATH was the caller's, so an
update from a shell with a shorter PATH took tools away from the agent (found in review,
2026-09-24). Each is read back from the installed units now; the environment still wins.
The script's own lines run here, up to the unit render, against installed units in a
temporary directory, with an opencode that only knows `serve --help` and a tailscale
that fails, both first on the PATH: the box's own tailscale is never asked.
"""
import pathlib
import subprocess
import tempfile
import unittest
REPO = pathlib.Path(__file__).resolve().parents[1]
TEXT = (REPO / "dashboard/install-agent.sh").read_text()
HEAD = TEXT[:TEXT.index("# ── render and install the unit")]
DASH_CUSTOM = ("[Service]\nEnvironment=COCKPIT_BIND=0.0.0.0\nEnvironment=COCKPIT_PORT=30090\n"
"Environment=COCKPIT_AGENT_PORT=30095\nEnvironment=COCKPIT_AGENT_BIND=192.168.1.10\n"
"Environment=COCKPIT_AGENT_UPSTREAM=http://127.0.0.1:4100\n")
OC_CUSTOM = ("[Service]\nEnvironment=PATH=/opt/agent-tools/bin:/usr/bin\n"
"ExecStart=/x/opencode serve --hostname 127.0.0.1 --port 4100 --print-logs --log-level INFO\n")
# a 1.x version: from 2.x on the script stops before the render (OpenCode 2, see below)
OPENCODE = "#!/bin/sh\ncase \"$*\" in *--help*) echo '--hostname --port';; *) echo 1.99.9;; esac\n"
def oc_unit(dirs):
"""An installed opencode-web unit whose PATH is `dirs`."""
return ("[Service]\nEnvironment=PATH=" + ":".join(dirs) + "\n"
"ExecStart=/x/opencode serve --hostname 127.0.0.1 --port 4100 --print-logs --log-level INFO\n")
def run_in(t, dash_unit, oc_unit=None, caller=(), **env):
"""((opencode port, relay port, relay bind, service PATH), stdout) of the script's lines,
with the directory t standing in for /etc/systemd/system. The PATH they run with is the
stubs' directory (t/bin, the same on every run in t), then `caller`, then /usr/bin:/bin."""
(t / "qwen38-dashboard.service").write_text(dash_unit)
if oc_unit is not None:
(t / "opencode-web.service").write_text(oc_unit)
fake = t / "bin"
if not fake.exists():
fake.mkdir()
(fake / "opencode").write_text(OPENCODE)
(fake / "tailscale").write_text("#!/bin/sh\nexit 1\n")
for f in fake.iterdir():
f.chmod(0o755)
script = (HEAD.replace("/etc/systemd/system", str(t))
+ 'echo "RESULT|$OPENCODE_PORT|$AGENT_PORT|$AGENT_BIND|$SVC_PATH"\n')
r = subprocess.run(["bash", "-c", script], capture_output=True, text=True, timeout=60,
cwd=REPO / "dashboard",
env={"PATH": ":".join([str(fake), *caller, "/usr/bin", "/bin"]), "HOME": str(t), **env})
line = next((ln for ln in r.stdout.splitlines() if ln.startswith("RESULT|")), None)
if line is None:
raise AssertionError(f"the script did not get to the render:\n{r.stdout}\n{r.stderr}")
return tuple(line.split("|")[1:]), r.stdout
def run(dash_unit, oc_unit=None, caller=(), **env):
with tempfile.TemporaryDirectory(prefix="agent-keeps-") as t:
return run_in(pathlib.Path(t), dash_unit, oc_unit, caller, **env)[0]
class AReRunKeepsTheInstalledTab(unittest.TestCase):
def test_the_installed_ports_and_address_are_kept(self):
oc, port, bind, path = run(DASH_CUSTOM, OC_CUSTOM)
self.assertEqual((oc, port, bind), ("4100", "30095", "192.168.1.10"))
def test_the_installed_service_path_is_not_lost(self):
_, _, _, path = run(DASH_CUSTOM, OC_CUSTOM)
self.assertIn("/opt/agent-tools/bin", path.split(":"))
self.assertIn("/usr/bin", path.split(":"))
def test_the_environment_still_wins(self):
oc, port, bind, path = run(DASH_CUSTOM, OC_CUSTOM, OPENCODE_PORT="4200", AGENT_PORT="30099",
AGENT_BIND="127.0.0.1", AGENT_PATH="/only/this")
self.assertEqual((oc, port, bind, path), ("4200", "30099", "127.0.0.1", "/only/this"))
def test_the_callers_order_comes_first(self):
# a newer tool first on the caller's PATH stays first for the agent: nvm's new node
# before the one the installed unit names
old, new = "/home/u/.nvm/versions/node/v24/bin", "/home/u/.nvm/versions/node/v26/bin"
path = run(DASH_CUSTOM, oc_unit([old, "/usr/bin"]), caller=[new])[3].split(":")
self.assertLess(path.index(new), path.index(old))
def test_a_first_install_takes_the_defaults(self):
oc, port, bind, _ = run("[Service]\nEnvironment=COCKPIT_BIND=0.0.0.0\nEnvironment=COCKPIT_AGENT_PORT=0\n")
self.assertEqual((oc, port, bind), ("4096", "30091", "tailscale"))
class TheRelayIsProvedWhereItAnswers(unittest.TestCase):
"""The proof at the end asked the configured address alone: with Tailscale down it proved nothing and named no
address, and with the relay on the box's loopback too (v1.59.1) one address that does not answer yet costs a note,
not the install. The script's own lines, with curl and sleep stubbed."""
TAIL = TEXT[TEXT.index("# ── the relay refuses a request without a session"):]
def proof(self, answers, bind="tailscale", ts="100.64.0.7", cockpit="0.0.0.0"):
script = ("set -euo pipefail\ndie(){ echo \"DIE: $*\"; exit 1; }\nnote(){ echo \"NOTE: $*\"; }\nsleep(){ :; }\n"
"curl(){ case \" $* \" in " + "".join(f"*http://{a}:*) printf 401 ;; " for a in answers) + "*) printf 000; return 7 ;; esac; }\n"
f"AGENT_BIND={bind}; TS_IP={ts}; AGENT_PORT=30091; COCKPIT_PORT=30090; COCKPIT_BIND={cockpit}; UNIT=opencode-web.service\n"
+ self.TAIL)
r = subprocess.run(["bash", "-c", script], capture_output=True, text=True, timeout=30, env={"PATH": "/usr/bin:/bin"})
return r.returncode, r.stdout + r.stderr
def test_both_addresses(self):
rc, out = self.proof(["127.0.0.1", "100.64.0.7"])
self.assertEqual(rc, 0, out)
self.assertIn("http://127.0.0.1:30091 refuses without a cockpit session (401)", out)
self.assertIn("http://100.64.0.7:30091 refuses without a cockpit session (401)", out)
self.assertIn("http://localhost:30090/#agent on the box's own screen", out)
self.assertIn("http://100.64.0.7:30090/#agent elsewhere", out)
self.assertIn("ssh -N -L 30090:127.0.0.1:30090 -L 30091:127.0.0.1:30091", out)
self.assertNotIn("NOTE", out)
def test_tailscale_down_the_box_has_its_agent(self):
rc, out = self.proof(["127.0.0.1"], ts="")
self.assertEqual(rc, 0, out)
self.assertIn("http://localhost:30090/#agent on the box's own screen", out)
self.assertIn("once tailscale is up", out)
def test_the_tailnet_and_a_lan(self):
# each address it was given is asked, and named where the cockpit answers on it too (decision 1623)
rc, out = self.proof(["127.0.0.1", "100.64.0.7", "192.168.1.10"], bind="tailscale,192.168.1.10")
self.assertEqual(rc, 0, out)
for a in ("127.0.0.1", "100.64.0.7", "192.168.1.10"):
self.assertIn(f"http://{a}:30091 refuses without a cockpit session (401)", out)
for a in ("100.64.0.7", "192.168.1.10"):
self.assertIn(f"http://{a}:30090/#agent elsewhere", out)
self.assertNotIn("NOTE", out)
rc, out = self.proof(["127.0.0.1", "192.168.1.10"], bind="tailscale,192.168.1.10", ts="")
self.assertEqual(rc, 0, out)
self.assertIn("once tailscale is up", out)
self.assertIn("http://192.168.1.10:30090/#agent elsewhere", out)
rc, out = self.proof(["127.0.0.1", "100.64.0.7"], bind="tailscale,192.168.1.10", cockpit="100.64.0.7")
self.assertEqual(rc, 0, out)
self.assertIn("NOTE: the relay does not answer on 192.168.1.10:30091 yet", out)
self.assertNotIn("http://192.168.1.10:30090/#agent", out, "the cockpit does not answer there")
def test_one_address_that_does_not_answer_is_a_note(self):
rc, out = self.proof(["127.0.0.1"])
self.assertEqual(rc, 0, out)
self.assertIn("NOTE: the relay does not answer on 100.64.0.7:30091 yet", out)
rc, out = self.proof(["100.64.0.7"])
self.assertEqual(rc, 0, out)
self.assertIn("NOTE: the relay does not answer on this box's 127.0.0.1:30091", out)
def test_none_is_a_failure(self):
rc, out = self.proof([])
self.assertEqual(rc, 1, out)
self.assertIn("DIE: the relay did not come up on any address", out)
def test_no_link_to_an_address_the_cockpit_does_not_answer_on(self):
rc, out = self.proof(["127.0.0.1", "100.64.0.7"], cockpit="192.168.1.10")
self.assertEqual(rc, 0, out)
self.assertNotIn("http://100.64.0.7:30090/#agent", out)
class OpenCode2IsServedOnlyByTheChosenBox(unittest.TestCase):
"""OpenCode 2 has a serve of its own that takes --hostname too (2.0.28, read 2026-10-22).
The tab speaks 2.x since the 2026-11-14 (relay on /api/info, ordered permissions, the
{file:...} answers scrubbed), but a stray 2.x on the PATH (Homebrew Core's formula is
one) must not hijack the unit of a box that never chose it: without OPENCODE_MAJOR=2 the
script stops before the render, so the installed unit is never touched (decision 455);
with the choice it renders."""
def run_with(self, version_line, **extra_env):
with tempfile.TemporaryDirectory(prefix="agent-oc2-") as d:
t = pathlib.Path(d)
(t / "qwen38-dashboard.service").write_text(DASH_CUSTOM)
(t / "opencode-web.service").write_text(OC_CUSTOM)
fake = t / "bin"
fake.mkdir()
(fake / "opencode").write_text("#!/bin/sh\ncase \"$*\" in *--help*) echo '--hostname --port';; "
f"*) echo '{version_line}';; esac\n")
(fake / "tailscale").write_text("#!/bin/sh\nexit 1\n")
for f in fake.iterdir():
f.chmod(0o755)
script = HEAD.replace("/etc/systemd/system", str(t)) + 'echo "RESULT|rendered"\n'
r = subprocess.run(["bash", "-c", script], capture_output=True, text=True, timeout=60,
cwd=REPO / "dashboard",
env={"PATH": f"{fake}:/usr/bin:/bin", "HOME": str(t), **extra_env})
return r.returncode, r.stdout + r.stderr, (t / "opencode-web.service").read_text()
def test_opencode_2_is_refused_before_the_render_without_the_choice(self):
for line in ("opencode v2.0.28", "2.1.3", "10.0.0"):
with self.subTest(line=line):
rc, out, unit = self.run_with(line)
self.assertNotEqual(rc, 0)
self.assertNotIn("RESULT|rendered", out)
self.assertIn("OpenCode 2, which this box did not choose", out)
self.assertIn("--opencode 2", out, "the message says how to ask for it")
self.assertEqual(unit, OC_CUSTOM, "the installed unit is left as it was")
def test_opencode_2_is_served_when_the_box_chose_it(self):
for line in ("opencode v2.3.0", "2.3.0"):
with self.subTest(line=line):
rc, out, _ = self.run_with(line, OPENCODE_MAJOR="2")
self.assertIn("RESULT|rendered", out, out)
def test_the_choice_does_not_falsely_upgrade_a_1x_binary(self):
# OPENCODE_MAJOR=2 alongside a 1.x binary is just the wrong choice, not a
# license to serve anything: it renders 1.x as before (the binary decides)
rc, out, _ = self.run_with("1.19.5", OPENCODE_MAJOR="2")
self.assertIn("RESULT|rendered", out, out)
def test_opencode_1_goes_on(self):
for line in ("1.19.4", "1.18.35", "opencode v1.99.0"):
with self.subTest(line=line):
rc, out, _ = self.run_with(line)
self.assertIn("RESULT|rendered", out, out)
H = "/home/someone"
# What an editor's session puts on its terminal's PATH, by the layouts the filter knows.
EDITOR_DIRS = [
f"{H}/.vscode-server/cli/servers/Stable-07f806f999/server/bin/remote-cli", # the reference box,
f"{H}/.vscode-server/data/User/globalStorage/github.copilot-chat/debugCommand", # 2026-10-03
f"{H}/.vscode-server/data/User/globalStorage/github.copilot-chat/copilotCli",
f"{H}/.vscode-server/bin/07f806f999/bin/remote-cli",
f"{H}/.vscode/cli/servers/Stable-07f806f999/server/bin/remote-cli",
f"{H}/.cursor-server/cli/servers/Stable-1a2b3c/server/bin/remote-cli",
f"{H}/.positron-server/bin/1a2b3c/bin/remote-cli",
"/usr/lib/code-server/lib/vscode/bin/remote-cli",
f"{H}/.config/Code/User/globalStorage/github.copilot-chat/debugCommand",
f"{H}/.vscode/extensions/ms-python.python-2026.6.0-linux-arm64/python_files/deactivate/bash",
f"{H}/.vscode-server/extensions/ms-python.debugpy-2026.6.0-linux-arm64/bundled/scripts/noConfigScripts",
]
# A desktop build's storage under a name with spaces: the script died on its PATH.
SPACED = f"{H}/.config/Code - OSS/User/globalStorage/github.copilot-chat/copilotCli"
# Each a character or a part away from one of those layouts, and none of them an editor's.
LOOK_ALIKES = [
f"{H}/.vscode-server-tools/bin",
f"{H}/xvscode-server/bin",
f"{H}/my.vscode-server/bin",
"/opt/x/bin/remote-cli-tools",
f"{H}/sbin/remote-cli",
f"{H}/MyUser/globalStorage/bin",
"/opt/extensions/my.tools/bin",
"/opt/extensions/tools-2/bin",
]
class AnEditorsSessionStaysOutOfTheServicePath(unittest.TestCase):
"""What an editor's session puts on its terminal's PATH reached the unit: an update run
from a VS Code terminal wrote VS Code's remote CLI (under a directory named after its
build) and two of its extensions' directories into opencode-web's PATH, so the unit
changed and the service restarted, and the remote CLI's directory goes at the next VS
Code update. The installed PATH kept them for good, since it only ever grew (the reference
box, 2026-10-03). They are known by their layout, left out of the caller's PATH and of
the installed one, and everything else is kept as before."""
def dirs(self, caller=(), installed=None, **env):
(_, _, _, path), out = self.run_once(caller, installed, **env)
return path.split(":"), out
def run_once(self, caller=(), installed=None, **env):
with tempfile.TemporaryDirectory(prefix="agent-editor-") as t:
return run_in(pathlib.Path(t), DASH_CUSTOM, None if installed is None else oc_unit(installed),
caller, **env)
def test_the_callers_editor_directories_are_left_out(self):
dirs, _ = self.dirs(EDITOR_DIRS + [SPACED, "/opt/tools/bin"])
self.assertEqual([d for d in EDITOR_DIRS + [SPACED] if d in dirs], [])
self.assertIn("/opt/tools/bin", dirs)
self.assertIn("/usr/bin", dirs)
def test_a_box_that_already_has_them_loses_them_and_is_told(self):
dirs, out = self.dirs(installed=["/opt/agent-tools/bin"] + EDITOR_DIRS + ["/usr/bin"])
self.assertEqual([d for d in EDITOR_DIRS if d in dirs], [])
self.assertIn("/opt/agent-tools/bin", dirs)
note = next(ln for ln in out.splitlines() if ln.startswith("NOTE: opencode-web's PATH leaves out"))
for d in EDITOR_DIRS:
self.assertIn(d, note)
def test_nothing_is_said_when_nothing_is_left_out(self):
_, out = self.dirs(EDITOR_DIRS, installed=["/opt/agent-tools/bin", "/usr/bin"])
self.assertNotIn("leaves out", out)
def test_a_name_that_only_looks_like_one_is_kept(self):
dirs, _ = self.dirs(LOOK_ALIKES)
self.assertEqual([d for d in LOOK_ALIKES if d not in dirs], [])
def test_relative_and_tilde_entries_stay(self):
# bash resolves both at each command, against its working directory and the home
kept = ["~/.local/bin", "./node_modules/.bin", "node_modules/.bin"]
dirs, _ = self.dirs(kept + ["/opt/tools/bin"], installed=["~/bin", "/usr/bin"])
self.assertEqual([d for d in kept + ["~/bin"] if d not in dirs], [])
def test_agent_path_is_taken_as_given_and_the_next_run_leaves_the_editor_out(self):
given = EDITOR_DIRS[0] + ":/usr/bin"
(_, _, _, path), _ = self.run_once(installed=["/usr/bin"], AGENT_PATH=given)
self.assertEqual(path, given)
dirs, out = self.dirs(installed=given.split(":"))
self.assertNotIn(EDITOR_DIRS[0], dirs)
self.assertIn(EDITOR_DIRS[0], out)
class EachDirectoryOnce(unittest.TestCase):
"""The caller's PATH and the installed one are merged on every update, the installed
one being what the run before wrote: a directory named twice in either, or in both,
is named once. Each run here reads the unit the run before wrote."""
def updates(self, *callers):
with tempfile.TemporaryDirectory(prefix="agent-once-") as td:
t, installed, seen = pathlib.Path(td), None, []
for caller in callers:
(_, _, _, path), _ = run_in(t, DASH_CUSTOM, installed, caller)
installed = oc_unit(path.split(":"))
seen.append(path)
return seen
def test_each_directory_is_named_once(self):
caller = ["/x/bin", "/x/bin", "/y/bin", "/x/bin"]
for path in self.updates(caller, caller + ["/y/bin"], caller):
dirs = path.split(":")
self.assertEqual(len(dirs), len(set(dirs)), dirs)
if __name__ == "__main__":
unittest.main(verbosity=2)