Validate providers and retain complete run debugging context

This commit is contained in:
2026-10-09 23:21:01 +01:00
parent f3d829bea5
commit 08b8647d5b
51 changed files with 1036 additions and 159 deletions
+70 -2
View File
@@ -3,6 +3,8 @@
import argparse
import importlib.util
import ipaddress
import re
import shutil
import json
from pathlib import Path
import subprocess
@@ -22,9 +24,11 @@ lan = module("lan_address", "lan-address.py")
class Setup:
def __init__(self, directory, runner=subprocess.run):
def __init__(self, directory, runner=subprocess.run, prompt=input):
self.directory = Path(directory)
self.runner = runner
self.prompt = prompt
self.accounts = []
def compose(self, *args, capture=False, check=True, **kwargs):
result = self.runner(["docker", "compose", *args], cwd=self.directory,
@@ -91,6 +95,7 @@ class Setup:
accounts = json.loads(result.stdout)
if not isinstance(accounts, list):
raise ValueError("Unexpected OpenCode account-list format; authentication not verified")
self.accounts = accounts
return any(isinstance(account, dict) and account.get("connections") for account in accounts)
if connected() and not relogin:
@@ -101,6 +106,65 @@ class Setup:
if not connected():
raise ValueError("Provider setup incomplete; rerun the installer to continue (credentials/volumes retained)")
def validate_provider(self, args):
result = self.compose("run", "--rm", "--no-deps", "-T", "--entrypoint", "opencode", "pipelines", "models", capture=True)
models = [line.strip() for line in result.stdout.splitlines()
if re.fullmatch(r"[A-Za-z0-9_.-]+/[A-Za-z0-9_./:-]+", line.strip())]
ids = {account.get("id") for account in self.accounts if isinstance(account,dict) and account.get("connections")}
available = [model for model in models if model.split("/",1)[0] in ids]
saved = dict(line.split("=",1) for line in (self.directory/".env").read_text().splitlines() if "=" in line)
model = args.model or saved.get("PIPELINES_DEFAULT_MODEL", "")
if not model:
if not available:
raise ValueError("No models found for the connected provider. Reconnect with ./install.sh --relogin; no model was validated.")
print("Choose the model to validate (one small tool-free test; provider charges may apply):")
for i, item in enumerate(available[:25],1):
print(f" {i}. {item}")
choice = self.prompt("Model number or full provider/model: ").strip()
model = available[int(choice)-1] if choice.isdigit() and 1 <= int(choice) <= len(available) else choice
if model not in models:
raise ValueError("Selected model is not available in this OpenCode configuration. Choose a listed model with --model PROVIDER/MODEL.")
print(f"Validating {model} with one tool-free generation; the session is retained for debugging.")
result = self.compose("run", "--rm", "--no-deps", "-T", "--entrypoint", "pipelines", "pipelines",
"provider-check", "--model", model, "--yes", "--json", "--timeout", "90s", capture=True, check=False)
try:
report = json.loads(result.stdout)
except ValueError:
raise ValueError("Provider validation could not run. Rebuild the image and rerun; setup is NOT complete.")
if not isinstance(report,dict) or result.returncode or report.get("healthy") is not True or not report.get("session_id"):
print(json.dumps(report, indent=2), file=sys.stderr)
raise ValueError("Provider validation FAILED. Fix the reported authentication/model/quota/service error, then rerun --relogin or --model PROVIDER/MODEL. Setup is NOT complete.")
path = self.directory/".env"
text = path.read_text()
key = "PIPELINES_DEFAULT_MODEL="
lines = text.splitlines(keepends=True)
updated = "".join(key+model+"\n" if line.startswith(key) else line for line in lines)
if not any(line.startswith(key) for line in lines):
updated = updated.rstrip("\n")+"\n"+key+model+"\n"
path.write_text(updated)
print(f"Provider/model validated: {model} · session {report.get('session_id','unknown')}")
def update_host_client(self):
if not shutil.which("go"):
print("Host Go is unavailable: Docker binaries are updated, but an existing host CLI/TUI is NOT updated. Install/update that client before expecting new UI behavior.")
return
for command in (["go","install","-trimpath","./cmd/..."], ["go","run","./cmd/pipelines","skill","install"]):
result = self.runner(command, cwd=self.directory)
if result.returncode:
raise ValueError("Host client update failed; Docker setup is incomplete")
print("Updated host CLI/TUI using go install. Restart the running TUI; an already-running process keeps its old code.")
def publish_address(self):
address = lan.detect_lan()
path = self.directory/".env"
text = path.read_text()
key = "PIPELINES_HOST_IP="
lines = text.splitlines(keepends=True)
updated = "".join(key+address+"\n" if line.startswith(key) else line for line in lines)
if not any(line.startswith(key) for line in lines):
updated = updated.rstrip("\n")+"\n"+key+address+"\n"
path.write_text(updated)
def verify(self):
# Read the token inside the container; never put it in argv or output.
probe = '''set -eu
@@ -118,7 +182,7 @@ printf 'header = "Authorization: Bearer %s"\\n' "$(cat "$PIPELINES_NTFY_TOKEN_FI
raise ValueError("Pipelines did not report healthy ntfy; check docker compose logs pipelines ntfy")
print("Verified: Pipelines reports healthy ntfy and authenticated publishing succeeded.")
print(f"On your phone: add {status['ntfy']['url']}, log in as phone-reader, subscribe to pipelines.")
print("Phone delivery and model inference are not verified by this setup check.")
print("Phone reception is not verified by this setup check.")
address = self.compose("port", "pipelines", "8080", capture=True).stdout.strip()
port = address.rsplit(":", 1)[-1]
if not port.isdigit():
@@ -129,6 +193,8 @@ printf 'header = "Authorization: Bearer %s"\\n' "$(cat "$PIPELINES_NTFY_TOKEN_FI
def run(self, args):
self.compose("version", capture=True)
self.settings(args)
self.publish_address()
self.update_host_client()
# Validate the actual resolved Compose configuration before starting anything.
self.compose("config", "--quiet", capture=True)
self.compose("up", "-d", "--wait", "--wait-timeout", "60", "ntfy")
@@ -136,6 +202,7 @@ printf 'header = "Authorization: Bearer %s"\\n' "$(cat "$PIPELINES_NTFY_TOKEN_FI
self.compose("build", "pipelines")
self.compose("run", "--rm", "--no-deps", "-T", "--entrypoint", "pipelines", "pipelines", "skill", "install")
self.authenticate(args.relogin)
self.validate_provider(args)
# Recreate to reload file secrets and environment, without deleting volumes.
self.compose("up", "-d", "--force-recreate", "--wait", "--wait-timeout", "60", "pipelines")
self.verify()
@@ -149,6 +216,7 @@ def arguments(argv=None):
parser.add_argument("--timezone", help="Override the detected timezone (fallback: UTC)")
parser.add_argument("--port", type=int)
parser.add_argument("--relogin", action="store_true", help="open OpenCode provider login again without deleting accounts")
parser.add_argument("--model", help="Provider/model for actual generation validation (saved after success)")
return parser.parse_args(argv)
+39 -4
View File
@@ -16,6 +16,35 @@ spec.loader.exec_module(setup)
class DockerSetupTests(unittest.TestCase):
def test_provider_validation_failure_preserves_saved_model(self):
for report in ['{"healthy":false,"diagnostic":"quota exhausted","session_id":"ses_failed"}',
'{"healthy":true}', '[]', 'not-json']:
with tempfile.TemporaryDirectory() as directory:
path=Path(directory,".env")
path.write_text("PIPELINES_DEFAULT_MODEL=fixture/previous\nCUSTOM=keep\n")
before=path.read_bytes()
calls=[]
def runner(command,**kwargs):
calls.append(command)
out="fixture/new\n" if command[-1:]==["models"] else report
return subprocess.CompletedProcess(command,0,stdout=out,stderr="")
installer=setup.Setup(directory,runner=runner)
with patch("sys.stderr"),self.assertRaises(ValueError):
installer.validate_provider(setup.arguments(["--model","fixture/new"]))
self.assertEqual(before,path.read_bytes())
self.assertTrue(any("provider-check" in command for command in calls))
def test_unavailable_model_does_not_attempt_generation(self):
with tempfile.TemporaryDirectory() as directory:
Path(directory,".env").write_text("CUSTOM=keep\n")
calls=[]
def runner(command,**kwargs):
calls.append(command)
return subprocess.CompletedProcess(command,0,stdout="fixture/available\n",stderr="")
with self.assertRaises(ValueError):
setup.Setup(directory,runner=runner).validate_provider(setup.arguments(["--model","fixture/unavailable"]))
self.assertFalse(any("provider-check" in command for command in calls))
def test_ctrl_c_exits_cleanly_and_retains_configuration(self):
with tempfile.TemporaryDirectory() as directory:
root=Path(directory);scripts=root/"scripts";scripts.mkdir()
@@ -25,6 +54,7 @@ class DockerSetupTests(unittest.TestCase):
docker=root/"docker"
docker.write_text('#!/bin/sh\ncase "$*" in *" up "*) touch "'+str(marker)+'"; exec sleep 30;; esac\n')
docker.chmod(0o700)
go=root/"go";go.write_text('#!/bin/sh\nexit 0\n');go.chmod(0o700)
process=subprocess.Popen([sys.executable,str(scripts/"setup-docker.py"),
"--ntfy-url","http://192.168.1.25:8090","--bind-address","192.168.1.25","--timezone","UTC"],
env=dict(os.environ,PATH=str(root)+os.pathsep+os.environ["PATH"]),
@@ -81,7 +111,11 @@ class DockerSetupTests(unittest.TestCase):
calls.append(args)
code, out = 0, ""
if args[-4:] == ["auth", "list", "--format", "json"]:
out = '[{"connections":[{"id":"fixture"}]}]' if logged else '[]'
out = '[{"id":"fixture","connections":[{"id":"fixture"}]}]' if logged else '[]'
if args[-1:] == ["models"]:
out = "fixture/test-model\n"
if "provider-check" in args:
out = '{"healthy":true,"session_id":"ses_fixture"}'
if args[-2:] == ["auth", "login"] and confirm == "yes":
logged = True
if "token" in args:
@@ -96,8 +130,9 @@ class DockerSetupTests(unittest.TestCase):
args = setup.arguments([] if existing else ["--ntfy-url", "http://192.168.1.20:8090",
"--bind-address", "192.168.1.20", "--timezone", "Europe/London"])
installer = setup.Setup(directory, runner=runner)
installer.run(args)
installer = setup.Setup(directory, runner=runner, prompt=lambda _: "1")
with patch.object(setup.lan,"detect_lan",return_value="192.168.1.20"):
installer.run(args)
return calls, token
def test_fresh_setup_wires_notifications_and_runs_container_login(self):
@@ -133,7 +168,7 @@ class DockerSetupTests(unittest.TestCase):
self.assertTrue(Path(directory, "secrets/ntfy-token.txt").stat().st_size)
def test_status_and_publication_failures_are_not_success(self):
for failure in ["status", "sh"]:
for failure in ["status", "sh", "provider-check"]:
with tempfile.TemporaryDirectory() as directory, self.assertRaises(ValueError):
self.exercise(directory, fail=failure)