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)