Make Pipelines Docker-only with OpenCode and Pi
Install current verified harness releases inside Docker, preserve persistent accounts and settings, and provide separate interactive and pipeline model defaults with terminal onboarding and fuzzy pickers. Add Docker-aware authoring transport, Pi stage execution, bounded workflow controls, reliable final-result readback and usage settlement, and finite short-lived CLI pipe draining. Keep dated installation evidence and packaged authoring guidance consistent. Verified with a real Docker installation, both harness stages and interactive authors, the built-in plan/implement/review starter, independent host race/vet checks, installer tests, and packaged skill tests. Generated release receipts stay ignored in clean checkouts.
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
"""Compile-time Docker identity and immutable release pins for a source build."""
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
|
||||
|
||||
def module(name, file):
|
||||
spec = importlib.util.spec_from_file_location(name, Path(__file__).with_name(file))
|
||||
result = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(result)
|
||||
return result
|
||||
|
||||
|
||||
def release_args(releases):
|
||||
"""Build arguments for the resolved OpenCode digest/version and Pi version."""
|
||||
return ["--build-arg", "OPENCODE_VERSION=" + releases["opencode"]["version"],
|
||||
"--build-arg", "OPENCODE_DIGEST=" + releases["opencode"]["digest"],
|
||||
"--build-arg", "PI_VERSION=" + releases["pi"]["version"]]
|
||||
|
||||
|
||||
def docker_args(directory, runner=subprocess.run):
|
||||
def git(*args):
|
||||
result = runner(["git", *args], cwd=directory, capture_output=True, text=True)
|
||||
if result.returncode:
|
||||
raise ValueError("Cannot identify source checkout; build from Git or supply Docker build args explicitly")
|
||||
return result.stdout.strip()
|
||||
|
||||
commit = git("rev-parse", "HEAD")
|
||||
if not re.fullmatch(r"[0-9a-f]{40,64}", commit):
|
||||
raise ValueError("Invalid Git revision for Docker build")
|
||||
dirty = bool(git("status", "--porcelain", "--untracked-files=normal"))
|
||||
version = commit[:12] + ("-dirty" if dirty else "")
|
||||
return ["--build-arg", "PIPELINES_VERSION=" + version,
|
||||
"--build-arg", "PIPELINES_COMMIT=" + commit,
|
||||
"--build-arg", "PIPELINES_DIRTY=" + str(dirty).lower()]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
directory = Path(__file__).resolve().parent.parent
|
||||
try:
|
||||
# The runtime stage is FROM the immutable digest, so a standalone build
|
||||
# must resolve the current releases itself rather than fail or fall back.
|
||||
releases = module("resolve_releases", "resolve-releases.py").resolve()
|
||||
subprocess.run(["docker", "compose", "build", *docker_args(directory), *release_args(releases),
|
||||
*sys.argv[1:], "pipelines"], cwd=directory, check=True)
|
||||
except KeyboardInterrupt:
|
||||
sys.exit(130)
|
||||
except (ValueError, OSError, subprocess.CalledProcessError) as error:
|
||||
sys.exit(f"Docker build incomplete: {error}")
|
||||
@@ -0,0 +1,65 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Check installed Pi policy/RPC/idle lease cleanup without credentials or inference."""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import queue
|
||||
import subprocess
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
|
||||
|
||||
def main():
|
||||
with tempfile.TemporaryDirectory(prefix="pipelines-pi-rpc-") as directory:
|
||||
root = Path(directory)
|
||||
(root / "policy.json").write_text(json.dumps({"directory": directory, "tools": [], "paths": [], "edit": None, "shell": None}))
|
||||
(root / "lease").write_text("owned")
|
||||
(root / "policy.js").write_bytes((Path(__file__).resolve().parents[1] / "internal/stage/pi_policy.mjs").read_bytes())
|
||||
env = dict(os.environ, PI_OFFLINE="1", PI_CODING_AGENT_DIR=str(root / "config"),
|
||||
PIPELINES_PI_POLICY=str(root / "policy.json"), PIPELINES_PI_LEASE=str(root / "lease"),
|
||||
PIPELINES_PI_STOPPED=str(root / "quiescent"))
|
||||
process = subprocess.Popen(["pi", "--mode", "rpc", "--no-extensions", "--no-skills", "--no-context-files",
|
||||
"--no-prompt-templates", "--no-tools", "--extension", str(root / "policy.js")],
|
||||
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, env=env, cwd=root)
|
||||
output = queue.Queue()
|
||||
|
||||
def read():
|
||||
for line in process.stdout:
|
||||
output.put(line)
|
||||
|
||||
thread = threading.Thread(target=read, daemon=True)
|
||||
thread.start()
|
||||
try:
|
||||
for command in ["get_state", "set_auto_retry", "set_auto_compaction", "get_session_stats", "get_messages"]:
|
||||
process.stdin.write(json.dumps({"id": command, "type": command, "enabled": False}) + "\n")
|
||||
process.stdin.flush()
|
||||
reply = None
|
||||
deadline = time.monotonic() + 15
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
record = json.loads(output.get(timeout=.5))
|
||||
except queue.Empty:
|
||||
continue
|
||||
if record.get("id") == command:
|
||||
reply = record
|
||||
break
|
||||
assert reply and reply["success"], reply
|
||||
assert (root / "ready").read_text() == "ready"
|
||||
os.utime(root / "lease", (0, 0))
|
||||
process.wait(timeout=10)
|
||||
assert process.returncode == 0
|
||||
assert (root / "quiescent").read_text() == "quiescent"
|
||||
print("PASS: installed Pi policy readiness, RPC commands, idle lease expiry/exit. No prompt, credentials or inference. Active-tool cleanup not tested.")
|
||||
finally:
|
||||
if process.poll() is None:
|
||||
process.terminate()
|
||||
process.wait(timeout=10)
|
||||
thread.join(timeout=2)
|
||||
process.stdin.close()
|
||||
process.stdout.close()
|
||||
process.stderr.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -26,8 +26,18 @@ def host_timezone():
|
||||
raise ValueError("Cannot detect the host timezone; provide --timezone explicitly")
|
||||
|
||||
def host_workspaces(directory):
|
||||
home, repository = str(Path.home().resolve()), str(Path(directory).resolve())
|
||||
if any(any(char in value for char in "\r\n\x00$#'\"\\") or value in ("/", "/data", "/workspace", "/home/pipelines") for value in [home, repository]) or home == repository:
|
||||
raw_home = os.environ.get("HOME") or os.environ.get("USERPROFILE") or str(Path.home())
|
||||
home = str(Path.home().resolve())
|
||||
repository = str(Path(directory).resolve())
|
||||
# Reject both the raw and canonical resolved HOME so a platform's home alias
|
||||
# (/home/pipelines on Linux; /System/Volumes/Data/home/pipelines on macOS)
|
||||
# cannot bypass the reserved-path guard. Managed Docker volumes are separate
|
||||
# and are not weakened by these cheap path comparisons.
|
||||
candidates = {raw_home, home, str(Path(raw_home).resolve()), repository}
|
||||
for value in candidates:
|
||||
if any(char in value for char in "\r\n\x00$#'\"\\") or value in ("/", "/data", "/workspace", "/home/pipelines", "/System/Volumes/Data/home/pipelines"):
|
||||
raise ValueError("Host home/checkout cannot be mounted safely at its original path; configure a repository bind mount explicitly")
|
||||
if home == repository or str(Path(raw_home).resolve()) == repository:
|
||||
raise ValueError("Host home/checkout cannot be mounted safely at its original path; configure a repository bind mount explicitly")
|
||||
return {"PIPELINES_HOST_HOME": "'"+home+"'", "PIPELINES_HOST_REPOSITORY": "'"+repository+"'", "PIPELINES_UID": str(os.getuid()), "PIPELINES_GID": str(os.getgid())}
|
||||
|
||||
|
||||
@@ -78,4 +78,4 @@ def detect_lan(runner=subprocess.run, platform=sys.platform, route_probe=None):
|
||||
return address
|
||||
except OSError:
|
||||
pass
|
||||
raise ValueError("No LAN IPv4 address detected. Connect this computer to your LAN and rerun ./install.sh, or override with --ntfy-url and --bind-address.")
|
||||
raise ValueError("No LAN IPv4 address detected. Connect this computer to your LAN and rerun ./install, or override with --ntfy-url and --bind-address.")
|
||||
|
||||
@@ -0,0 +1,280 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Bounded resolution of current harness releases for the Docker image.
|
||||
|
||||
Network I/O is isolated behind an injectable ``fetch`` callable so unit tests
|
||||
never touch the network. The resolver:
|
||||
|
||||
* discovers supported V2 releases from one bounded GitHub request to
|
||||
``git/matching-refs/tags/v2``, which the official REST docs
|
||||
(https://docs.github.com/en/rest/git/refs#list-matching-references) document
|
||||
as a complete array with no pagination parameters. There is no page/per_page
|
||||
query, no ``Link`` cursor and no tags/list cross-check: an explicit
|
||||
``MAX_REFS`` count and ``MAX_BODY`` byte cap bound the single response;
|
||||
* treats the official V2 tag metadata (not the registry's insertion order) as
|
||||
the ordering, ignoring prerelease/build-suffixed tags and rejecting malformed
|
||||
ones; it never uses the moving ``releases/latest`` anchor;
|
||||
* resolves the selected tag's immutable ``sha256`` digest from GHCR and fails
|
||||
closed when it is missing or invalid, never falling back to an older version;
|
||||
* validates untrusted metadata, versions, digests and fixture values;
|
||||
* refuses non-registry hosts and unexpected redirects;
|
||||
* validates the Pi Node engine constraint fail-closed.
|
||||
|
||||
The selected versions/digests are data, not evergreen hardcoded answers.
|
||||
"""
|
||||
import json
|
||||
import re
|
||||
import urllib.error
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
|
||||
|
||||
class ReleaseError(ValueError):
|
||||
"""Resolution failed; callers must keep the last verified pins."""
|
||||
|
||||
|
||||
GHCR = "https://ghcr.io"
|
||||
GITHUB = "https://api.github.com"
|
||||
NPM = "https://registry.npmjs.org"
|
||||
OPENCODE_REPOSITORY = "anomalyco/opencode"
|
||||
OPENCODE_IMAGE = "ghcr.io/anomalyco/opencode"
|
||||
PI_PACKAGE = "@earendil-works/pi-coding-agent"
|
||||
MAX_REFS = 1000 # Explicit ceiling on the documented complete matching-refs array.
|
||||
MAX_BODY = 1 << 20 # Generous ceiling for tag lists and manifest metadata.
|
||||
SUPPORTED_MAJOR = 2
|
||||
HTTP_TIMEOUT = 5.0
|
||||
ALLOWED_HOSTS = frozenset({"ghcr.io", "api.github.com", "registry.npmjs.org"})
|
||||
GITHUB_HEADERS = {"Accept": "application/vnd.github+json", "User-Agent": "pipelines-setup"}
|
||||
MANIFEST_ACCEPT = ", ".join([
|
||||
"application/vnd.oci.image.index.v1+json",
|
||||
"application/vnd.docker.distribution.manifest.list.v2+json",
|
||||
"application/vnd.oci.image.manifest.v1+json",
|
||||
"application/vnd.docker.distribution.manifest.v2+json",
|
||||
])
|
||||
VERSION = re.compile(r"v?(\d+)\.(\d+)\.(\d+)")
|
||||
# A version-shaped tag carrying a prerelease/build suffix is ignored, not fatal.
|
||||
VERSION_SUFFIX = re.compile(r"v?(\d+)\.(\d+)\.(\d+)[-+].*")
|
||||
DIGEST = re.compile(r"sha256:[0-9a-f]{64}")
|
||||
TAG = re.compile(r"[A-Za-z0-9][A-Za-z0-9._/-]{0,127}")
|
||||
ENGINE = re.compile(r"(>=|<=|>|<|=)?\s*v?(\d+\.\d+\.\d+)")
|
||||
|
||||
|
||||
def registry_tag(tag):
|
||||
"""Normalize an official ``vX.Y.Z`` git tag to its registry tag name."""
|
||||
return tag[1:] if tag.startswith("v") else tag
|
||||
|
||||
|
||||
def parse_version(text):
|
||||
if not isinstance(text, str):
|
||||
return None
|
||||
match = VERSION.fullmatch(text.strip())
|
||||
if not match:
|
||||
return None
|
||||
return tuple(int(part) for part in match.groups())
|
||||
|
||||
|
||||
class _NoRedirect(urllib.request.HTTPRedirectHandler):
|
||||
"""Registry metadata lookups must never be silently redirected elsewhere."""
|
||||
|
||||
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
||||
raise ReleaseError(f"refusing unexpected redirect from {req.full_url!r} to {newurl!r}")
|
||||
|
||||
|
||||
_OPENER = urllib.request.build_opener(_NoRedirect)
|
||||
|
||||
|
||||
def _default_fetch(url, headers=None):
|
||||
parsed = urllib.parse.urlsplit(url)
|
||||
if parsed.scheme != "https" or parsed.hostname not in ALLOWED_HOSTS:
|
||||
raise ReleaseError(f"refusing non-registry host {url!r}")
|
||||
request = urllib.request.Request(url, headers=headers or {})
|
||||
try:
|
||||
with _OPENER.open(request, timeout=HTTP_TIMEOUT) as response:
|
||||
lowered = {key.lower(): value for key, value in response.headers.items()}
|
||||
return response.status, lowered, response.read(MAX_BODY + 1)
|
||||
except urllib.error.HTTPError as error:
|
||||
lowered = {key.lower(): value for key, value in error.headers.items()}
|
||||
return error.code, lowered, error.read(MAX_BODY + 1)
|
||||
|
||||
|
||||
def _response_json(fetch, url, headers=None):
|
||||
"""Fetch a bounded JSON body and return it with its lower-cased headers."""
|
||||
status, response_headers, body = fetch(url, headers or {})
|
||||
if status != 200:
|
||||
raise ReleaseError(f"HTTP {status} from {url}")
|
||||
if not isinstance(body, (bytes, bytearray)):
|
||||
raise ReleaseError(f"non-bytes response from {url}")
|
||||
if len(body) > MAX_BODY:
|
||||
raise ReleaseError(f"response body exceeds {MAX_BODY} bytes from {url}")
|
||||
try:
|
||||
data = json.loads(body)
|
||||
except (TypeError, ValueError):
|
||||
raise ReleaseError(f"invalid JSON from {url}") from None
|
||||
lowered = {key.lower(): value for key, value in (response_headers or {}).items()}
|
||||
return data, lowered
|
||||
|
||||
|
||||
def _json(fetch, url, headers=None):
|
||||
data, _ = _response_json(fetch, url, headers)
|
||||
return data
|
||||
|
||||
|
||||
def _github_v2_tags(fetch):
|
||||
"""One bounded request for the documented complete matching-refs array.
|
||||
|
||||
GitHub's "List matching references" endpoint documents a complete array and
|
||||
no pagination parameters, so a single ``MAX_BODY``/``MAX_REFS``-bounded GET
|
||||
is sufficient. Exact stable versions become candidates; version-shaped tags
|
||||
with a prerelease/build suffix are ignored; other malformed tags fail closed.
|
||||
"""
|
||||
url = f"{GITHUB}/repos/{OPENCODE_REPOSITORY}/git/matching-refs/tags/v{SUPPORTED_MAJOR}"
|
||||
data, _ = _response_json(fetch, url, GITHUB_HEADERS)
|
||||
if not isinstance(data, list):
|
||||
raise ReleaseError("GitHub matching-refs payload is not a list")
|
||||
if len(data) > MAX_REFS:
|
||||
raise ReleaseError(
|
||||
f"GitHub matching-refs returned {len(data)} refs, exceeding the {MAX_REFS}-ref cap; "
|
||||
"refusing to select a release without proof the tag list is complete")
|
||||
candidates = {}
|
||||
for entry in data:
|
||||
if not isinstance(entry, dict):
|
||||
raise ReleaseError("GitHub matching-refs entry is not an object")
|
||||
ref = entry.get("ref")
|
||||
if not isinstance(ref, str) or not ref.startswith("refs/tags/"):
|
||||
raise ReleaseError(f"GitHub matching-refs entry has an unexpected ref {ref!r}")
|
||||
tag = registry_tag(ref[len("refs/tags/"):])
|
||||
version = parse_version(tag)
|
||||
if version is None:
|
||||
if VERSION_SUFFIX.fullmatch(tag):
|
||||
# Prerelease/build tags never win; skip them instead of failing.
|
||||
continue
|
||||
raise ReleaseError(f"GitHub matching-refs tag {tag!r} is not an exact version")
|
||||
candidates[tag] = version
|
||||
return candidates
|
||||
|
||||
|
||||
def _ghcr_token(fetch):
|
||||
data = _json(fetch, f"{GHCR}/token?scope=repository:{OPENCODE_REPOSITORY}:pull")
|
||||
if not isinstance(data, dict):
|
||||
raise ReleaseError("registry token payload is not an object")
|
||||
token = data.get("token")
|
||||
if not isinstance(token, str) or not token:
|
||||
raise ReleaseError("registry did not issue a pull token")
|
||||
return token
|
||||
|
||||
|
||||
def _manifest_digest(fetch, token, tag):
|
||||
if not isinstance(tag, str) or not TAG.fullmatch(tag):
|
||||
raise ReleaseError(f"refusing to resolve an invalid registry tag {tag!r}")
|
||||
url = f"{GHCR}/v2/{OPENCODE_REPOSITORY}/manifests/{urllib.parse.quote(tag, safe='')}"
|
||||
status, headers, _ = fetch(url, {"Authorization": "Bearer " + token, "Accept": MANIFEST_ACCEPT})
|
||||
if status != 200:
|
||||
raise ReleaseError(f"HTTP {status} resolving digest for {tag}")
|
||||
digest = headers.get("docker-content-digest", "")
|
||||
if not DIGEST.fullmatch(digest):
|
||||
raise ReleaseError(f"registry returned no usable content digest for {tag}")
|
||||
return digest
|
||||
|
||||
|
||||
def resolve_opencode(fetch=_default_fetch):
|
||||
candidates = _github_v2_tags(fetch)
|
||||
supported = {tag: version for tag, version in candidates.items() if version[0] == SUPPORTED_MAJOR}
|
||||
if not supported:
|
||||
raise ReleaseError(f"no supported OpenCode V{SUPPORTED_MAJOR} release found in the official tag metadata")
|
||||
tag = max(supported, key=lambda name: supported[name])
|
||||
token = _ghcr_token(fetch)
|
||||
# The highest supported stable tag is authoritative; the manifest lookup
|
||||
# below proves publication and fails closed if its digest is missing, never
|
||||
# falling back to an older version.
|
||||
return {
|
||||
"image": OPENCODE_IMAGE,
|
||||
"version": tag,
|
||||
"digest": _manifest_digest(fetch, token, tag),
|
||||
"anchor": f"github:repos/{OPENCODE_REPOSITORY}/git/matching-refs/tags/v{SUPPORTED_MAJOR}",
|
||||
"candidates": sorted(supported),
|
||||
}
|
||||
|
||||
|
||||
def _validate_engine(engine):
|
||||
text = (engine or "").strip()
|
||||
if not text:
|
||||
return
|
||||
if not ENGINE.fullmatch(text):
|
||||
raise ReleaseError(f"unsupported Pi Node engine constraint {engine!r}; refusing to accept an unchecked runtime")
|
||||
|
||||
|
||||
def node_satisfies(engine, node_version):
|
||||
"""Fail-closed check for the simple constraints Pi publishes."""
|
||||
node = parse_version(node_version)
|
||||
if node is None:
|
||||
raise ReleaseError(f"could not parse Node version {node_version!r}")
|
||||
text = (engine or "").strip()
|
||||
if not text:
|
||||
return True
|
||||
match = ENGINE.fullmatch(text)
|
||||
if not match:
|
||||
raise ReleaseError(f"unsupported Pi Node engine constraint {engine!r}; refusing to accept an unchecked runtime")
|
||||
operator = match.group(1) or "="
|
||||
bound = parse_version(match.group(2))
|
||||
if operator == ">=":
|
||||
return node >= bound
|
||||
if operator == "<=":
|
||||
return node <= bound
|
||||
if operator == ">":
|
||||
return node > bound
|
||||
if operator == "<":
|
||||
return node < bound
|
||||
return node == bound
|
||||
|
||||
|
||||
def resolve_pi(fetch=_default_fetch, node_version=None):
|
||||
url = f"{NPM}/@earendil-works%2fpi-coding-agent/latest"
|
||||
data = _json(fetch, url)
|
||||
if not isinstance(data, dict):
|
||||
raise ReleaseError("npm Pi payload is not an object")
|
||||
version = data.get("version")
|
||||
if parse_version(version) is None:
|
||||
raise ReleaseError(f"npm returned a non-version Pi release {version!r}")
|
||||
engines = data.get("engines") or {}
|
||||
if not isinstance(engines, dict):
|
||||
raise ReleaseError("npm Pi engines metadata is malformed")
|
||||
engine = engines.get("node", "")
|
||||
if engine is not None and not isinstance(engine, str):
|
||||
raise ReleaseError("npm Pi node engine constraint is malformed")
|
||||
engine = engine or ""
|
||||
_validate_engine(engine)
|
||||
if node_version is not None and not node_satisfies(engine, node_version):
|
||||
raise ReleaseError(f"Pi {version} requires Node {engine} but the image provides {node_version}")
|
||||
return {"package": PI_PACKAGE, "version": version, "engine": engine}
|
||||
|
||||
|
||||
def resolve(fetch=_default_fetch, node_version=None):
|
||||
return {"opencode": resolve_opencode(fetch), "pi": resolve_pi(fetch, node_version)}
|
||||
|
||||
|
||||
def load_fixture(path):
|
||||
"""Test/offline seam: read an explicit resolution instead of the network."""
|
||||
with open(path, encoding="utf-8") as handle:
|
||||
data = json.load(handle)
|
||||
if not isinstance(data, dict):
|
||||
raise ReleaseError("release fixture must be an object")
|
||||
opencode, pi = data.get("opencode"), data.get("pi")
|
||||
if not isinstance(opencode, dict) or not isinstance(pi, dict):
|
||||
raise ReleaseError("release fixture must contain opencode and pi objects")
|
||||
if parse_version(opencode.get("version")) is None:
|
||||
raise ReleaseError("release fixture opencode version is not an exact version")
|
||||
if not DIGEST.fullmatch(str(opencode.get("digest", ""))):
|
||||
raise ReleaseError("release fixture opencode digest is not a sha256 digest")
|
||||
if opencode.get("image", OPENCODE_IMAGE) != OPENCODE_IMAGE:
|
||||
raise ReleaseError("release fixture opencode image is not the supported registry image")
|
||||
if parse_version(pi.get("version")) is None:
|
||||
raise ReleaseError("release fixture pi version is not an exact version")
|
||||
_validate_engine(pi.get("engine", ""))
|
||||
return data
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
try:
|
||||
print(json.dumps(resolve(), indent=2))
|
||||
except ReleaseError as error:
|
||||
sys.exit(f"Release resolution failed: {error}")
|
||||
+328
-17
@@ -23,13 +23,44 @@ def module(name, file):
|
||||
configure = module("configure_compose", "configure-compose.py")
|
||||
tokens = module("save_ntfy_token", "save-ntfy-token.py")
|
||||
lan = module("lan_address", "lan-address.py")
|
||||
build_metadata = module("build_metadata", "build_metadata.py")
|
||||
release_lookup = module("resolve_releases", "resolve-releases.py")
|
||||
|
||||
|
||||
# Exact executable version formats reported by the built image. Anything with a
|
||||
# banner, suffix or extra token is rejected instead of being partially parsed.
|
||||
# OpenCode's `--version` prints its executable name before the version token
|
||||
# ("opencode v2.0.26", verified probe output), so accept exactly that optional
|
||||
# lowercase prefix in addition to the bare/v-prefixed token; unrelated banners
|
||||
# such as "OpenCode v2.0.26" or trailing text are still rejected. The patch
|
||||
# component accepts arbitrary digits (valid/future versions could report e.g.
|
||||
# 260 or 100; these are illustrative examples, not verified actual releases);
|
||||
# verify_harness_versions still rejects a parsed 260 when the expected value is
|
||||
# 26 by exact string equality.
|
||||
HARNESS_VERSION_FORMATS = {
|
||||
"opencode": re.compile(r"(?:opencode )?v?(\d+\.\d+\.\d+)"),
|
||||
"pi": re.compile(r"(\d+\.\d+\.\d+)"),
|
||||
"node": re.compile(r"v?(\d+\.\d+\.\d+)"),
|
||||
}
|
||||
|
||||
|
||||
def parse_harness_version(name, value):
|
||||
"""Accept only the exact known version format for a harness executable."""
|
||||
pattern = HARNESS_VERSION_FORMATS.get(name)
|
||||
if pattern is None:
|
||||
raise ValueError(f"Unknown harness executable {name!r}; refusing to parse an unchecked version")
|
||||
match = pattern.fullmatch((value or "").strip())
|
||||
if not match:
|
||||
raise ValueError(f"Unexpected {name} version output {value!r}; refusing to continue on an unverified image")
|
||||
return match.group(1)
|
||||
|
||||
|
||||
class Setup:
|
||||
def __init__(self, directory, runner=subprocess.run, prompt=input):
|
||||
def __init__(self, directory, runner=subprocess.run, prompt=input, resolver=None):
|
||||
self.directory = Path(directory)
|
||||
self.runner = runner
|
||||
self.prompt = prompt
|
||||
self.resolver = resolver or release_lookup.resolve
|
||||
self.accounts = []
|
||||
|
||||
def compose(self, *args, capture=False, check=True, **kwargs):
|
||||
@@ -132,7 +163,30 @@ done
|
||||
'''
|
||||
result = self.compose("run", "--rm", "--no-deps", "-T", "--entrypoint", "sh", "pipelines", "-c", discovery, capture=True)
|
||||
return [line.strip() for line in result.stdout.splitlines()
|
||||
if re.fullmatch(r"[A-Za-z0-9_.-]+/[A-Za-z0-9_./:-]+", line.strip())]
|
||||
if re.fullmatch(r"[A-Za-z0-9_.-]+/[A-Za-z0-9_./:-]+", line.strip())]
|
||||
|
||||
def choose_model(self, models, title, purpose="", step="", current=""):
|
||||
# Keep terminal stdin free for the fuzzy picker; catalog/result use private
|
||||
# files. The picker always runs inside the configured Docker installation so
|
||||
# it never executes a host harness, even when host Go is available.
|
||||
with tempfile.TemporaryDirectory(prefix=".pipelines-picker-", dir=self.directory) as temporary:
|
||||
root = Path(temporary)
|
||||
catalog, result = root/"models.json", root/"selected.txt"
|
||||
catalog.write_text(json.dumps(models))
|
||||
catalog.chmod(0o600)
|
||||
options = ["--title", title]
|
||||
if purpose:
|
||||
options += ["--purpose", purpose]
|
||||
if step:
|
||||
options += ["--step", step]
|
||||
if current:
|
||||
options += ["--current", current]
|
||||
self.compose("run", "--rm", "--no-deps", "--entrypoint", "pipelines", "pipelines",
|
||||
"choose-model", "--options-file", str(catalog), "--output", str(result), *options)
|
||||
selected = result.read_text().strip()
|
||||
if selected not in models:
|
||||
raise ValueError("Model selection did not produce an available model; defaults unchanged")
|
||||
return selected
|
||||
|
||||
def validate_provider(self, args):
|
||||
models = self.discover_models()
|
||||
@@ -140,18 +194,31 @@ done
|
||||
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", "")
|
||||
harness_model = args.harness_model or saved.get("PIPELINES_HARNESS_MODEL", "")
|
||||
# Fresh setup selects the interactive model first and the new-pipeline
|
||||
# model second, matching `pipelines harness setup` (and reconfiguration).
|
||||
if (not model or not harness_model) and not available:
|
||||
connected = ", ".join(sorted(str(id) for id in ids)) or "none"
|
||||
visible = ", ".join(sorted({model.split('/', 1)[0] for model in models})) or "none"
|
||||
raise ValueError(f"OpenCode catalog discovery found no models for connected providers [{connected}] (visible model providers: [{visible}]). Your saved key was not replaced; this is NOT proof it is invalid. Rerun ./install; if it persists, share this message and the OpenCode version, not your key. No model was validated.")
|
||||
if not harness_model:
|
||||
harness_model = self.choose_model(
|
||||
available,
|
||||
"Choose the interactive OpenCode harness model",
|
||||
purpose="Runs your interactive OpenCode authoring and debugging sessions. It does not change new-pipeline-stage models.",
|
||||
step="Step 1 of 2 · interactive model",
|
||||
current=saved.get("PIPELINES_HARNESS_MODEL", ""))
|
||||
if not model:
|
||||
if not available:
|
||||
connected = ", ".join(sorted(str(id) for id in ids)) or "none"
|
||||
visible = ", ".join(sorted({model.split('/', 1)[0] for model in models})) or "none"
|
||||
raise ValueError(f"OpenCode catalog discovery found no models for connected providers [{connected}] (visible model providers: [{visible}]). Your saved key was not replaced; this is NOT proof it is invalid. Rerun ./install.sh; if it persists, share this message and the OpenCode version, not your key. 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
|
||||
model = self.choose_model(
|
||||
available,
|
||||
"Choose the default model for new pipelines",
|
||||
purpose="Runs model stages in new pipelines and starters. Stored separately from the interactive harness model.",
|
||||
step="Step 2 of 2 · new-pipeline-stage model",
|
||||
current=saved.get("PIPELINES_DEFAULT_MODEL", ""))
|
||||
if model not in models:
|
||||
raise ValueError("Selected model is not available in this OpenCode configuration. Choose a listed model with --model PROVIDER/MODEL.")
|
||||
if harness_model not in models:
|
||||
raise ValueError("Selected interactive harness model is unavailable; defaults unchanged")
|
||||
print(f"Validating {model} with one tool-free generation; the session is retained for debugging.")
|
||||
validation = '''set -eu
|
||||
opencode api get /api/integration >/dev/null
|
||||
@@ -174,13 +241,25 @@ exec pipelines provider-check --model "$1" --yes --json --timeout 90s
|
||||
if not any(line.startswith(key) for line in lines):
|
||||
updated = updated.rstrip("\n")+"\n"+key+model+"\n"
|
||||
path.write_text(updated)
|
||||
lines = path.read_text().splitlines(keepends=True)
|
||||
key = "PIPELINES_HARNESS_MODEL="
|
||||
updated = "".join(key+harness_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+harness_model+"\n"
|
||||
path.write_text(updated)
|
||||
setup_options = ["--model", harness_model, "--pipeline-model", model]
|
||||
versions = getattr(self, "harness_version_label", "")
|
||||
if versions:
|
||||
setup_options += ["--versions", versions]
|
||||
self.compose("run", "--rm", "--no-deps", "-T", "--entrypoint", "pipelines", "pipelines", "harness", "setup", "opencode", *setup_options)
|
||||
self.selected_harness_model, self.selected_pipeline_model = harness_model, model
|
||||
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"]):
|
||||
for command in (["go","install","-trimpath","./cmd/pipelines","./cmd/pipelines-tui"], ["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")
|
||||
@@ -225,6 +304,208 @@ exec pipelines provider-check --model "$1" --yes --json --timeout 90s
|
||||
"pipelines-workspace-check", directory, capture=True)
|
||||
print("Verified: Docker server can enter the host installation repository at " + directory)
|
||||
|
||||
def resolve_releases(self):
|
||||
fixture = os.environ.get("PIPELINES_RELEASE_FIXTURE")
|
||||
try:
|
||||
if fixture:
|
||||
return release_lookup.load_fixture(fixture)
|
||||
return self.resolver()
|
||||
except release_lookup.ReleaseError as error:
|
||||
raise ValueError(
|
||||
f"Could not resolve current harness releases ({error}); keeping the last verified Dockerfile pins. "
|
||||
"Rerun when the registry/npm is reachable; refusing to silently install stale copies.") from None
|
||||
|
||||
def reconfigure_harness(self, name, options):
|
||||
# Reuse the setup resolution/verification path without rebuilding or
|
||||
# restarting services: a stale image needs an explicit ./install upgrade.
|
||||
releases = self.resolve_releases()
|
||||
build_identity = self.build_identity()
|
||||
if self.needs_image_build(releases, build_identity):
|
||||
raise ValueError(
|
||||
f"The Docker image does not match the current resolved releases "
|
||||
f"(OpenCode {releases['opencode']['version']}, Pi {releases['pi']['version']}); "
|
||||
"run ./install to rebuild it, then rerun this reconfiguration")
|
||||
found = self.verify_harness_versions(releases) or {}
|
||||
label = self.harness_versions_label({
|
||||
"opencode": found.get("opencode", releases["opencode"]["version"]),
|
||||
"pi": found.get("pi", releases["pi"]["version"]),
|
||||
"node": found.get("node", ""),
|
||||
})
|
||||
self.compose("exec", "pipelines", "pipelines", "harness", "setup", name, "--login", "--versions", label, *options)
|
||||
|
||||
def link_host(self, name=None, config=None):
|
||||
# Shared by fresh setup and --opencode/--pi reconfiguration. The host
|
||||
# launcher is linked from the container profile; it is never executed
|
||||
# from a host harness runtime.
|
||||
if config is None:
|
||||
config = json.loads(self.compose(
|
||||
"exec", "-T", "pipelines", "pipelines", "harness", "list", "--json", capture=True).stdout)
|
||||
name = name or config["default"]
|
||||
profile = config["profiles"][name]
|
||||
if shutil.which("go"):
|
||||
executable = ["go", "run", "./cmd/pipelines"]
|
||||
elif shutil.which("pipelines"):
|
||||
executable = [shutil.which("pipelines")]
|
||||
else:
|
||||
print("Host harness not linked: rerun ./install with host Go available to build the Docker client.")
|
||||
return
|
||||
args = [*executable, "harness", "link", name, "--docker-directory", str(self.directory.resolve()),
|
||||
"--model", profile["model"], "--pipeline-model", profile["pipeline_model"]]
|
||||
if config.get("default") == name:
|
||||
args.append("--default")
|
||||
result = self.runner(args, cwd=self.directory)
|
||||
if result.returncode:
|
||||
raise ValueError("Host harness link failed; container configuration retained")
|
||||
|
||||
def release_receipt_path(self):
|
||||
return self.directory / ".pipelines-releases.json"
|
||||
|
||||
def read_release_receipt(self):
|
||||
path = self.release_receipt_path()
|
||||
if path.is_symlink():
|
||||
raise ValueError("Refusing to read a symlinked release receipt")
|
||||
if not path.exists():
|
||||
return {}
|
||||
try:
|
||||
data = json.loads(path.read_text())
|
||||
except ValueError:
|
||||
return {}
|
||||
return data if isinstance(data, dict) else {}
|
||||
|
||||
def record_release_receipt(self, releases, build_identity=None):
|
||||
path = self.release_receipt_path()
|
||||
if path.is_symlink():
|
||||
raise ValueError("Refusing to replace a symlinked release receipt")
|
||||
data = dict(releases)
|
||||
if build_identity:
|
||||
data["build"] = dict(build_identity)
|
||||
encoded = json.dumps(data, indent=2, sort_keys=True) + "\n"
|
||||
handle, temporary = tempfile.mkstemp(dir=str(self.directory), prefix=".pipelines-releases-")
|
||||
try:
|
||||
os.chmod(temporary, 0o600)
|
||||
with os.fdopen(handle, "w") as file:
|
||||
file.write(encoded)
|
||||
os.replace(temporary, path)
|
||||
finally:
|
||||
if os.path.exists(temporary):
|
||||
os.unlink(temporary)
|
||||
|
||||
def release_build_args(self, releases):
|
||||
return build_metadata.release_args(releases)
|
||||
|
||||
def build_identity(self):
|
||||
args = build_metadata.docker_args(self.directory, self.runner)
|
||||
return dict(arg.split("=", 1) for arg in args if arg != "--build-arg")
|
||||
|
||||
def image_identity(self, image_id):
|
||||
# Read the actual built image's labels rather than trusting the receipt
|
||||
# file, so an unrelated/stale image cannot satisfy the build decision.
|
||||
result = self.runner(["docker", "image", "inspect", image_id, "--format", "{{json .Config.Labels}}"],
|
||||
cwd=self.directory, capture_output=True, text=True)
|
||||
if result.returncode or not result.stdout.strip():
|
||||
return {}
|
||||
try:
|
||||
labels = json.loads(result.stdout)
|
||||
except ValueError:
|
||||
return {}
|
||||
return labels if isinstance(labels, dict) else {}
|
||||
|
||||
def expected_labels(self, releases, build_identity=None):
|
||||
labels = {
|
||||
"net.a-wan.pipelines.opencode-version": releases["opencode"]["version"],
|
||||
"net.a-wan.pipelines.opencode-digest": releases["opencode"]["digest"],
|
||||
"net.a-wan.pipelines.pi-version": releases["pi"]["version"],
|
||||
}
|
||||
if build_identity:
|
||||
labels["net.a-wan.pipelines.source-version"] = build_identity.get("PIPELINES_VERSION", "")
|
||||
labels["net.a-wan.pipelines.source-commit"] = build_identity.get("PIPELINES_COMMIT", "")
|
||||
labels["net.a-wan.pipelines.source-dirty"] = build_identity.get("PIPELINES_DIRTY", "")
|
||||
return labels
|
||||
|
||||
def receipt_matches(self, receipt, releases, build_identity=None):
|
||||
if not isinstance(receipt, dict):
|
||||
return False
|
||||
opencode, pi = receipt.get("opencode"), receipt.get("pi")
|
||||
if not isinstance(opencode, dict) or not isinstance(pi, dict):
|
||||
# A corrupted receipt must fail closed and rebuild, not raise.
|
||||
return False
|
||||
if (opencode.get("version") != releases["opencode"]["version"]
|
||||
or opencode.get("digest") != releases["opencode"]["digest"]
|
||||
or pi.get("version") != releases["pi"]["version"]):
|
||||
return False
|
||||
return not build_identity or receipt.get("build") == build_identity
|
||||
|
||||
def needs_image_build(self, releases, build_identity=None, force_dirty=False):
|
||||
# A dirty tree has no content identity: the same commit/version/dirty
|
||||
# strings can describe different bytes. During install, always invoke the
|
||||
# Docker build for a dirty source and let Docker layer caching decide. The
|
||||
# reconfiguration path (force_dirty=False) deliberately checks only the
|
||||
# verified release image so a model/login-only change is not a rebuild:
|
||||
# source identity is compared only for a full install.
|
||||
if force_dirty and build_identity and build_identity.get("PIPELINES_DIRTY") == "true":
|
||||
return True
|
||||
identity = build_identity if force_dirty else None
|
||||
images = self.compose("images", "-q", "pipelines", capture=True, check=False).stdout.strip().splitlines()
|
||||
if not images:
|
||||
return True
|
||||
if not self.receipt_matches(self.read_release_receipt(), releases, identity):
|
||||
return True
|
||||
labels = self.image_identity(images[0].strip())
|
||||
return not all(labels.get(key) == value
|
||||
for key, value in self.expected_labels(releases, identity).items())
|
||||
|
||||
def verify_harness_versions(self, releases):
|
||||
# One bounded in-image check per harness; fail closed on stale/missing.
|
||||
probe = '''set -eu
|
||||
printf 'opencode=%s\\n' "$(opencode --version)"
|
||||
printf 'pi=%s\\n' "$(pi --version)"
|
||||
printf 'node=%s\\n' "$(node --version)"
|
||||
'''
|
||||
result = self.compose("run", "--rm", "--no-deps", "-T", "--entrypoint", "sh", "pipelines", "-c", probe, capture=True)
|
||||
found = {}
|
||||
for line in result.stdout.splitlines():
|
||||
name, separator, value = line.strip().partition("=")
|
||||
if separator and name in HARNESS_VERSION_FORMATS:
|
||||
found[name] = parse_harness_version(name, value)
|
||||
expected_opencode = releases["opencode"]["version"]
|
||||
expected_opencode = expected_opencode[1:] if expected_opencode.startswith("v") else expected_opencode
|
||||
expected_pi = releases["pi"]["version"]
|
||||
expected_pi = expected_pi[1:] if expected_pi.startswith("v") else expected_pi
|
||||
if found.get("opencode") != expected_opencode or found.get("pi") != expected_pi:
|
||||
raise ValueError(
|
||||
f"Built image does not report the resolved harness versions (expected OpenCode {expected_opencode}, "
|
||||
f"Pi {expected_pi}); refusing to continue on a stale or mismatched image")
|
||||
engine = releases["pi"].get("engine", "")
|
||||
if engine:
|
||||
if "node" not in found:
|
||||
raise ValueError(f"Could not verify Node compatibility for Pi ({engine}); refusing to continue")
|
||||
if not release_lookup.node_satisfies(engine, found["node"]):
|
||||
raise ValueError(f"Built image Node {found['node']} does not satisfy Pi's required engine {engine}")
|
||||
print(f"Verified image harnesses: OpenCode {expected_opencode}, Pi {expected_pi}"
|
||||
+ (f", Node {found['node']} ({engine})" if engine else ""))
|
||||
return found
|
||||
|
||||
def harness_versions_label(self, found):
|
||||
# Rendered by the Go onboarding wizard; no extra probe or subprocess.
|
||||
parts = [f"OpenCode {found.get('opencode', '?')}", f"Pi {found.get('pi', '?')}"]
|
||||
if found.get("node"):
|
||||
parts.append(f"Node {found['node']}")
|
||||
return " · ".join(parts)
|
||||
|
||||
def verify_build(self, expected):
|
||||
result = self.compose("exec", "-T", "pipelines", "pipelines", "--url",
|
||||
"ws://127.0.0.1:8080/connect", "ping", "--json", capture=True)
|
||||
try:
|
||||
pong = json.loads(result.stdout)
|
||||
build = pong["build"]
|
||||
if (pong["type"] != "pong" or build["commit"] != expected["PIPELINES_COMMIT"]
|
||||
or build["version"] != expected["PIPELINES_VERSION"]
|
||||
or build["dirty"] is not (expected["PIPELINES_DIRTY"] == "true")):
|
||||
raise ValueError("build mismatch")
|
||||
except (ValueError, KeyError, TypeError):
|
||||
raise ValueError("Running server does not match the built source; setup is NOT complete. Check docker compose logs pipelines") from None
|
||||
print("Verified running server build: " + build["version"] + " (" + build["commit"] + ")")
|
||||
|
||||
def verify(self):
|
||||
# Read the token inside the container; never put it in argv or output.
|
||||
probe = '''set -eu
|
||||
@@ -251,7 +532,7 @@ printf 'header = "Authorization: Bearer %s"\\n' "$(cat "$PIPELINES_NTFY_TOKEN_FI
|
||||
self.verify_host_endpoint(endpoint)
|
||||
self.configure_client(endpoint)
|
||||
print(f"Host CLI/TUI connection: pipelines --url {endpoint} tui")
|
||||
print("Install host client binaries with ./install.sh --native if needed.")
|
||||
print("Run ./install with host Go available to install/update the Docker CLI/TUI clients.")
|
||||
|
||||
def verify_host_endpoint(self, endpoint):
|
||||
if shutil.which("go"):
|
||||
@@ -301,6 +582,12 @@ printf 'header = "Authorization: Bearer %s"\\n' "$(cat "$PIPELINES_NTFY_TOKEN_FI
|
||||
|
||||
def run(self, args):
|
||||
self.compose("version", capture=True)
|
||||
# Refuse an unidentifiable source archive before changing settings/services.
|
||||
build_args = build_metadata.docker_args(self.directory, self.runner)
|
||||
expected_build = dict(arg.split("=", 1) for arg in build_args if arg != "--build-arg")
|
||||
# Resolve current releases before touching settings/services; a failure
|
||||
# keeps the verified pins and stops rather than installing stale copies.
|
||||
releases = self.resolve_releases()
|
||||
self.settings(args)
|
||||
self.publish_address()
|
||||
self.configure_workspaces()
|
||||
@@ -309,16 +596,38 @@ printf 'header = "Authorization: Bearer %s"\\n' "$(cat "$PIPELINES_NTFY_TOKEN_FI
|
||||
self.compose("config", "--quiet", capture=True)
|
||||
self.compose("up", "-d", "--wait", "--wait-timeout", "60", "ntfy")
|
||||
self.bootstrap_ntfy()
|
||||
self.compose("build", "pipelines")
|
||||
if self.needs_image_build(releases, expected_build, force_dirty=True):
|
||||
self.compose("build", *build_args, *self.release_build_args(releases), "pipelines")
|
||||
found = self.verify_harness_versions(releases) or {}
|
||||
self.record_release_receipt(releases, expected_build)
|
||||
else:
|
||||
print("Keeping the existing image: resolved harness versions, digest and source identity are unchanged.")
|
||||
found = {}
|
||||
self.harness_version_label = self.harness_versions_label({
|
||||
"opencode": found.get("opencode", releases["opencode"]["version"]),
|
||||
"pi": found.get("pi", releases["pi"]["version"]),
|
||||
"node": found.get("node", ""),
|
||||
})
|
||||
self.compose("stop", "--timeout", "60", "pipelines")
|
||||
self.prepare_volume_ownership()
|
||||
self.compose("run", "--rm", "--no-deps", "-T", "--entrypoint", "pipelines", "pipelines", "skill", "install")
|
||||
self.authenticate(args.relogin)
|
||||
self.validate_provider(args)
|
||||
if args.harness == "opencode":
|
||||
self.authenticate(args.relogin)
|
||||
self.validate_provider(args)
|
||||
else:
|
||||
options = ["--default", "--login", "--versions", self.harness_version_label]
|
||||
if args.model:
|
||||
options += ["--pipeline-model", args.model]
|
||||
if args.harness_model:
|
||||
options += ["--model", args.harness_model]
|
||||
self.compose("run", "--rm", "--no-deps", "--entrypoint", "pipelines", "pipelines", "harness", "setup", "pi", *options)
|
||||
# Recreate to reload file secrets and environment, without deleting volumes.
|
||||
self.compose("up", "-d", "--force-recreate", "--wait", "--wait-timeout", "60", "pipelines")
|
||||
self.verify_build(expected_build)
|
||||
self.verify_workspace()
|
||||
self.verify()
|
||||
if shutil.which("go") or shutil.which("pipelines"):
|
||||
self.link_host()
|
||||
print("Docker setup complete. Use docker compose ps / logs to manage it; no OS user service was registered.")
|
||||
|
||||
|
||||
@@ -330,6 +639,8 @@ def arguments(argv=None):
|
||||
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)")
|
||||
parser.add_argument("--harness-model", help="Separate interactive OpenCode model default (catalog validation only)")
|
||||
parser.add_argument("--harness", choices=["opencode", "pi"], default="opencode", help="Harness for fresh Docker setup; Pi skips OpenCode login/inference validation")
|
||||
return parser.parse_args(argv)
|
||||
|
||||
|
||||
@@ -339,5 +650,5 @@ if __name__ == "__main__":
|
||||
except KeyboardInterrupt:
|
||||
print("\nSetup canceled; existing configuration, credentials and Docker volumes were retained.", file=sys.stderr)
|
||||
sys.exit(130)
|
||||
except (ValueError, OSError, EOFError, json.JSONDecodeError, configure.ZoneInfoNotFoundError) as error:
|
||||
except (ValueError, OSError, EOFError, json.JSONDecodeError, subprocess.CalledProcessError, configure.ZoneInfoNotFoundError) as error:
|
||||
sys.exit(f"Docker setup incomplete: {error}")
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Reconfigure one harness without reinstalling/restarting services or deleting login."""
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
|
||||
def module(name, file):
|
||||
spec = importlib.util.spec_from_file_location(name, Path(__file__).with_name(file))
|
||||
result = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(result)
|
||||
return result
|
||||
|
||||
|
||||
setup_docker = module("setup_docker", "setup-docker.py")
|
||||
|
||||
|
||||
def main():
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
if len(sys.argv) < 2 or sys.argv[1] not in ("opencode", "pi"):
|
||||
sys.exit("Usage: setup-harness.py opencode|pi [harness setup options]")
|
||||
name = sys.argv[1]
|
||||
settings = root / ".env"
|
||||
docker = settings.is_file() and "PIPELINES_HOST_REPOSITORY=" in settings.read_text()
|
||||
if not docker:
|
||||
sys.exit("Docker installation is not configured. Run ./install first; harness setup does not fall back to a host runtime.")
|
||||
installer = setup_docker.Setup(root)
|
||||
installer.reconfigure_harness(name, sys.argv[2:])
|
||||
installer.link_host(name)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
except KeyboardInterrupt:
|
||||
sys.exit(130)
|
||||
except (ValueError, KeyError, OSError, subprocess.CalledProcessError) as error:
|
||||
sys.exit(f"Harness setup incomplete; existing credentials retained: {error}")
|
||||
@@ -0,0 +1,107 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Developer-only source lifecycle check; not an alternative installer. No inference."""
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import socket
|
||||
import subprocess
|
||||
import tempfile
|
||||
import time
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--temp-dir", help="Parent directory for disposable test files")
|
||||
args = parser.parse_args()
|
||||
repo = Path(__file__).resolve().parent.parent
|
||||
with tempfile.TemporaryDirectory(prefix="pipelines-first-install-", dir=args.temp_dir) as temporary:
|
||||
root = Path(temporary)
|
||||
env = {k: v for k, v in os.environ.items() if not k.startswith("PIPELINES_")}
|
||||
env.update(GOBIN=str(root / "bin"), XDG_CONFIG_HOME=str(root / "config"),
|
||||
XDG_DATA_HOME=str(root / "data"), PIPELINES_DRAFT_DIR=str(root / "drafts"))
|
||||
subprocess.run(["go", "install", "-trimpath", "./cmd/..."], cwd=repo, env=env, check=True, timeout=180)
|
||||
subprocess.run([str(root / "bin/pipelines"), "skill", "install"], cwd=repo, env=env, check=True, timeout=30)
|
||||
cli = root / "bin/pipelines"
|
||||
assert (root / "bin/pipelines-tui").is_file(), "TUI binary missing"
|
||||
assert (root / "config/opencode/skills/pipelines/references/operations.md").is_file(), "Skill not installed"
|
||||
workspace = root / "workspace"
|
||||
workspace.mkdir()
|
||||
|
||||
def local(*command):
|
||||
return subprocess.run([str(cli), *command], cwd=workspace, env=env,
|
||||
capture_output=True, text=True, check=True, timeout=30).stdout
|
||||
|
||||
version = json.loads(local("version", "--json"))
|
||||
assert version == json.loads(local("--version", "--json"))
|
||||
assert "import [FILE.zip]" in local("help")
|
||||
with socket.socket() as sock:
|
||||
sock.bind(("127.0.0.1", 0))
|
||||
port = sock.getsockname()[1]
|
||||
endpoint = f"ws://127.0.0.1:{port}/connect"
|
||||
database = root / "runs.db"
|
||||
|
||||
def rpc(*command):
|
||||
return json.loads(local("--url", endpoint, *command, "--json"))
|
||||
|
||||
def start(log):
|
||||
process = subprocess.Popen([str(root / "bin/pipelines-server"), "-addr", f"127.0.0.1:{port}",
|
||||
"-db", str(database)], cwd=workspace, env=env, stdout=log, stderr=log)
|
||||
try:
|
||||
deadline = time.monotonic() + 15
|
||||
while time.monotonic() < deadline:
|
||||
if process.poll() is not None:
|
||||
raise RuntimeError("Test server exited; see " + str(root / "server.log"))
|
||||
try:
|
||||
with socket.create_connection(("127.0.0.1", port), timeout=.1):
|
||||
return process
|
||||
except OSError:
|
||||
time.sleep(.05)
|
||||
raise RuntimeError("Test server did not become ready")
|
||||
except BaseException:
|
||||
stop(process)
|
||||
raise
|
||||
|
||||
def stop(process):
|
||||
process.terminate()
|
||||
try:
|
||||
process.wait(timeout=10)
|
||||
except subprocess.TimeoutExpired:
|
||||
process.kill()
|
||||
process.wait()
|
||||
raise
|
||||
|
||||
with (root / "server.log").open("w") as log:
|
||||
process = start(log)
|
||||
try:
|
||||
pong = rpc("ping")
|
||||
assert pong["build"] == version, "Client/server build mismatch"
|
||||
assert not rpc("list").get("pipelines"), "Fresh database must be empty"
|
||||
assert not rpc("runs").get("runs"), "Fresh database must have no runs"
|
||||
assert len(rpc("templates")["templates"]) == 5
|
||||
draft = rpc("new", "First test", "Summarize input", "--template", "summary", "--no-open", "--yes")
|
||||
assert not rpc("list").get("pipelines"), "Draft creation must not publish"
|
||||
rpc("validate", draft["path"])
|
||||
rpc("submit", draft["path"], "--yes")
|
||||
published = rpc("show", "First test")
|
||||
assert published["version"] == 1
|
||||
archive = root / "definitions.zip"
|
||||
rpc("export", "First test", "--output", str(archive))
|
||||
result = rpc("import", str(archive), "--all", "--yes")
|
||||
assert not result["imported"], "Identical import must not republish"
|
||||
assert rpc("show", "First test")["version"] == 1
|
||||
assert Path(draft["path"]).is_file(), "Published draft must remain"
|
||||
finally:
|
||||
stop(process)
|
||||
process = start(log)
|
||||
try:
|
||||
assert rpc("show", "First test")["source"] == published["source"], "Definition lost on restart"
|
||||
assert rpc("ping")["build"] == version
|
||||
assert not rpc("runs").get("runs"), "Smoke check must not execute a workflow"
|
||||
finally:
|
||||
stop(process)
|
||||
print("PASS developer lifecycle fixture: isolated source binaries/skill, offline version, empty DB, five templates, draft/validate/publish, ZIP identical import, restart persistence. Not an installation option; no inference or notifications.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,65 @@
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from build_metadata import docker_args
|
||||
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
class BuildMetadataTests(unittest.TestCase):
|
||||
def test_clean_and_dirty_sources(self):
|
||||
for dirty in (False, True):
|
||||
def runner(command, **kwargs):
|
||||
out = "a" * 40 if command[1] == "rev-parse" else ("?? new.go" if dirty else "")
|
||||
return subprocess.CompletedProcess(command, 0, stdout=out)
|
||||
args = docker_args(".", runner)
|
||||
self.assertIn("PIPELINES_COMMIT=" + "a" * 40, args)
|
||||
self.assertIn("PIPELINES_VERSION=" + "a" * 12 + ("-dirty" if dirty else ""), args)
|
||||
self.assertIn("PIPELINES_DIRTY=" + str(dirty).lower(), args)
|
||||
|
||||
def test_missing_or_invalid_git_fails(self):
|
||||
for code, out in ((1, ""), (0, "bad revision")):
|
||||
def runner(command, **kwargs):
|
||||
return subprocess.CompletedProcess(command, code, stdout=out)
|
||||
with self.assertRaises(ValueError):
|
||||
docker_args(".", runner)
|
||||
|
||||
def test_generated_release_receipt_is_ignored_and_dirty_detection_intact(self):
|
||||
if shutil.which("git") is None:
|
||||
self.skipTest("git is required for the isolated source-hygiene fixture")
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
directory = Path(directory)
|
||||
shutil.copy(REPO_ROOT / ".gitignore", directory / ".gitignore")
|
||||
source = directory / "source.txt"
|
||||
source.write_text("tracked\n")
|
||||
|
||||
def git(*args):
|
||||
result = subprocess.run(["git", *args], cwd=directory, capture_output=True, text=True)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
return result.stdout
|
||||
|
||||
git("init", "-q")
|
||||
git("config", "user.name", "Fixture")
|
||||
git("config", "user.email", "fixture@example.invalid")
|
||||
git("config", "commit.gpgsign", "false")
|
||||
git("add", ".gitignore", "source.txt")
|
||||
git("commit", "-q", "-m", "fixture")
|
||||
|
||||
# Representative generated root artifacts the installer writes.
|
||||
(directory / ".pipelines-releases.json").write_text("{}\n")
|
||||
(directory / ".pipelines-releases-abc123").write_text("")
|
||||
|
||||
self.assertEqual(git("status", "--porcelain", "--untracked-files=normal"), "")
|
||||
clean = dict(arg.split("=", 1) for arg in docker_args(directory) if arg != "--build-arg")
|
||||
self.assertEqual(clean["PIPELINES_DIRTY"], "false")
|
||||
self.assertFalse(clean["PIPELINES_VERSION"].endswith("-dirty"))
|
||||
|
||||
source.write_text("modified\n")
|
||||
self.assertNotEqual(git("status", "--porcelain", "--untracked-files=normal"), "")
|
||||
dirty = dict(arg.split("=", 1) for arg in docker_args(directory) if arg != "--build-arg")
|
||||
self.assertEqual(dirty["PIPELINES_DIRTY"], "true")
|
||||
self.assertTrue(dirty["PIPELINES_VERSION"].endswith("-dirty"))
|
||||
@@ -1,7 +1,9 @@
|
||||
import importlib.util
|
||||
import os
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
spec = importlib.util.spec_from_file_location("configure", Path(__file__).with_name("configure-compose.py"))
|
||||
configure = importlib.util.module_from_spec(spec)
|
||||
@@ -9,7 +11,20 @@ spec.loader.exec_module(configure)
|
||||
|
||||
|
||||
class ConfigureTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# Container runs may have HOME=/home/pipelines, which the mount guard
|
||||
# rejects; inject a disposable HOME/USERPROFILE for the fixtures.
|
||||
home = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(home.cleanup)
|
||||
environment = patch.dict(os.environ, {"HOME": home.name, "USERPROFILE": home.name})
|
||||
environment.start()
|
||||
self.addCleanup(environment.stop)
|
||||
|
||||
def test_detect_timezone_and_preserve_existing_files(self):
|
||||
try:
|
||||
configure.host_timezone()
|
||||
except ValueError:
|
||||
self.skipTest("host timezone is unavailable in this environment")
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
zone = configure.configure(directory, "http://192.168.1.20:8090", "0.0.0.0")
|
||||
self.assertTrue(zone)
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
"""Developer-only PTY test for the compiled model picker.
|
||||
|
||||
Skipped by default. Set PIPELINES_HARNESS_PTY_DIR to build the CLI, drive the real
|
||||
picker under a PTY and retain raw ANSI bytes for inspection. This performs no
|
||||
inference, network call or server mutation.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import pty
|
||||
import select
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import time
|
||||
import unittest
|
||||
|
||||
|
||||
class HarnessPickerPTYTests(unittest.TestCase):
|
||||
directory = None
|
||||
binary = None
|
||||
# Longer than at least one cursor-blink interval so a blink cannot silently
|
||||
# reset the active row before the confirming key is sent.
|
||||
blink_wait = 1.5
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.directory = os.environ.get("PIPELINES_HARNESS_PTY_DIR")
|
||||
if not cls.directory:
|
||||
raise unittest.SkipTest("set PIPELINES_HARNESS_PTY_DIR to run compiled picker PTY tests")
|
||||
if not shutil.which("go"):
|
||||
raise unittest.SkipTest("host Go is required to build the picker")
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
build = Path(tempfile.mkdtemp(prefix="pipelines-pty-"))
|
||||
cls.binary = build / "pipelines"
|
||||
subprocess.run(["go", "build", "-o", str(cls.binary), "./cmd/pipelines"], cwd=root, check=True)
|
||||
Path(cls.directory).mkdir(parents=True, exist_ok=True)
|
||||
|
||||
def drive(self, name, models, nav=b"", current="", expect=None, confirm=True):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
catalog, result = Path(tmp) / "models.json", Path(tmp) / "selected.txt"
|
||||
catalog.write_text(json.dumps(models))
|
||||
command = [str(self.binary), "choose-model",
|
||||
"--options-file", str(catalog), "--output", str(result),
|
||||
"--title", "PTY picker", "--purpose", "Interactive model purpose",
|
||||
"--step", "Step 1 of 2"]
|
||||
if current:
|
||||
command += ["--current", current]
|
||||
master, slave = pty.openpty()
|
||||
process = subprocess.Popen(command, stdin=slave, stdout=slave, stderr=slave, close_fds=True)
|
||||
os.close(slave)
|
||||
output = b""
|
||||
sent = False
|
||||
confirmed = False
|
||||
sent_at = 0.0
|
||||
deadline = time.monotonic() + 25
|
||||
try:
|
||||
while time.monotonic() < deadline:
|
||||
ready, _, _ = select.select([master], [], [], 0.2)
|
||||
if ready:
|
||||
try:
|
||||
chunk = os.read(master, 4096)
|
||||
except OSError:
|
||||
break
|
||||
if not chunk:
|
||||
break
|
||||
output += chunk
|
||||
if not sent and b"Filter" in output:
|
||||
os.write(master, nav)
|
||||
sent = True
|
||||
sent_at = time.monotonic()
|
||||
# Wait through at least one full blink interval before
|
||||
# confirming so a blink-resets-index regression is caught.
|
||||
if sent and confirm and not confirmed and time.monotonic() - sent_at >= self.blink_wait:
|
||||
os.write(master, b"\r")
|
||||
confirmed = True
|
||||
if process.poll() is not None:
|
||||
break
|
||||
finally:
|
||||
if process.poll() is None:
|
||||
process.terminate()
|
||||
try:
|
||||
process.wait(timeout=5)
|
||||
except subprocess.TimeoutExpired:
|
||||
process.kill()
|
||||
os.close(master)
|
||||
(Path(self.directory) / (name + ".ansi")).write_bytes(output)
|
||||
self.assertIn(b"PTY picker", output, name)
|
||||
if confirm:
|
||||
self.assertTrue(result.exists(), name)
|
||||
written = result.read_text().strip()
|
||||
self.assertIn(written, models)
|
||||
self.assertEqual(written, expect, name)
|
||||
else:
|
||||
self.assertFalse(result.exists(), name)
|
||||
|
||||
def test_arrow_scrolled_model_survives_blinks(self):
|
||||
models = [f"provider/model-{i:02d}" for i in range(20)]
|
||||
# Move down past the visible window, then confirm the exact arrow row.
|
||||
self.drive("selection", models, nav=b"\x1b[B" * 15, current=models[0], expect=models[15])
|
||||
|
||||
def test_saved_model_stays_selected_across_blinks(self):
|
||||
models = [f"provider/model-{i:02d}" for i in range(5)]
|
||||
self.drive("saved", models, nav=b"", current=models[3], expect=models[3])
|
||||
|
||||
def test_fuzzy_filter_and_confirm(self):
|
||||
models = ["opencode-go/deepseek-v4.1-flash", "other/model"]
|
||||
self.drive("fuzzy", models, nav=b"dskfl", expect="opencode-go/deepseek-v4.1-flash")
|
||||
|
||||
def test_escape_cancels_without_writing_selection(self):
|
||||
models = ["opencode-go/deepseek-v4.1-flash"]
|
||||
self.drive("cancel", models, nav=b"\x1b", confirm=False)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -8,7 +8,7 @@ import manage
|
||||
|
||||
class InstallationTests(unittest.TestCase):
|
||||
def test_docker_installer_is_available_through_main_entrypoint(self):
|
||||
result=subprocess.run([str(Path(__file__).resolve().parents[1]/"install.sh"),"--docker","--help"],capture_output=True,text=True)
|
||||
result=subprocess.run([str(Path(__file__).resolve().parents[1]/"install"),"--help"],capture_output=True,text=True)
|
||||
self.assertEqual(result.returncode,0)
|
||||
self.assertIn("--ntfy-url",result.stdout)
|
||||
self.assertIn("--relogin",result.stdout)
|
||||
@@ -21,7 +21,7 @@ class InstallationTests(unittest.TestCase):
|
||||
executable.write_text('#!/bin/sh\nprintf "%s\\n" "$@" > "'+str(capture)+'"\n')
|
||||
executable.chmod(0o700)
|
||||
env=dict(os.environ,PATH=str(root)+os.pathsep+os.environ["PATH"])
|
||||
subprocess.run([str(Path(__file__).resolve().parents[1]/"install.sh")],env=env,stdin=subprocess.DEVNULL,check=True)
|
||||
subprocess.run([str(Path(__file__).resolve().parents[1]/"install")],env=env,stdin=subprocess.DEVNULL,check=True)
|
||||
self.assertEqual(capture.read_text().splitlines(),["scripts/setup-docker.py"])
|
||||
|
||||
def test_default_installer_forwards_docker_options(self):
|
||||
@@ -29,7 +29,7 @@ class InstallationTests(unittest.TestCase):
|
||||
self.assertEqual(result.returncode,0)
|
||||
self.assertIn("--ntfy-url",result.stdout)
|
||||
|
||||
def test_native_installer_needs_only_go_and_honors_gobin(self):
|
||||
def test_native_installer_is_rejected_without_running_go(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root=Path(directory)
|
||||
capture=root/"args"
|
||||
@@ -37,8 +37,10 @@ class InstallationTests(unittest.TestCase):
|
||||
executable.write_text('#!/bin/sh\nprintf "%s\\n" "$GOBIN" "$@" >> "'+str(capture)+'"\n')
|
||||
executable.chmod(0o700)
|
||||
env=dict(os.environ,PATH=str(root)+os.pathsep+os.environ["PATH"],GOBIN=str(root/"bin"))
|
||||
subprocess.run([str(Path(__file__).resolve().parents[1]/"install.sh"),"--native"],env=env,check=True)
|
||||
self.assertEqual(capture.read_text().splitlines(),[str(root/"bin"),"install","-trimpath","./cmd/...",str(root/"bin"),"run","./cmd/pipelines","skill","install"])
|
||||
result=subprocess.run([str(Path(__file__).resolve().parents[1]/"install"),"--native"],env=env,capture_output=True,text=True)
|
||||
self.assertEqual(result.returncode,2)
|
||||
self.assertIn("Run ./install",result.stderr)
|
||||
self.assertFalse(capture.exists())
|
||||
self.assertFalse((root/"bin").exists())
|
||||
|
||||
def test_standard_installer_rejects_legacy_provisioning_flags(self):
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { permit } from "../internal/stage/pi_policy.mjs";
|
||||
|
||||
test("explicit tools, scope, symlinks and command permissions", () => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), "pipelines-policy-"));
|
||||
const outside = fs.mkdtempSync(path.join(os.tmpdir(), "pipelines-policy-outside-"));
|
||||
try {
|
||||
fs.symlinkSync(outside, path.join(root, "escape"));
|
||||
fs.mkdirSync(path.join(root,"docs"));
|
||||
fs.writeFileSync(path.join(root,"other.txt"),"original");
|
||||
fs.symlinkSync(path.join(root,"other.txt"),path.join(root,"docs","alias.md"));
|
||||
const policy = { directory: root, tools: ["write", "read", "bash"], paths: [], edit: ["docs/*.md"], shell: ["go test ./..."] };
|
||||
assert.equal(permit(policy, "write", { path: "docs/new.md" }), null);
|
||||
assert.ok(permit(policy, "write", { path: "escape/file" }));
|
||||
assert.ok(permit(policy, "write", { path: "../escape" }));
|
||||
assert.ok(permit(policy, "write", { path: "other.txt" }));
|
||||
assert.ok(permit(policy,"write",{path:"docs/alias.md"}));
|
||||
assert.ok(permit(policy,"write",{path:"~/escape.txt"}));
|
||||
assert.ok(permit(policy, "read", { path: ".env.local" }));
|
||||
assert.equal(permit(policy, "bash", { command: "go test ./..." }), null);
|
||||
assert.ok(permit(policy, "bash", { command: "go test ./...; curl example.com" }));
|
||||
assert.ok(permit(policy, "edit", { path: "docs/new.md" }));
|
||||
assert.ok(permit({ ...policy, paths: ["docs/allowed.md"] }, "write", { path: "docs/new.md" }));
|
||||
} finally {
|
||||
fs.rmSync(root, { recursive: true }); fs.rmSync(outside, { recursive: true });
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,182 @@
|
||||
import importlib.util
|
||||
import json
|
||||
from pathlib import Path
|
||||
import unittest
|
||||
|
||||
spec = importlib.util.spec_from_file_location("resolve_releases", Path(__file__).with_name("resolve-releases.py"))
|
||||
resolve_releases = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(resolve_releases)
|
||||
|
||||
|
||||
class FakeRegistry:
|
||||
"""Deterministic loopback fixture; the resolver's fetch seam keeps tests offline.
|
||||
|
||||
A single complete matching-refs array is served; there is deliberately no
|
||||
``tags/list`` handler, so any registry cross-check would return 404 and fail.
|
||||
"""
|
||||
|
||||
def __init__(self, refs=None, pi=None, digest="sha256:" + "a" * 64,
|
||||
digests=None, refs_response=None):
|
||||
self.refs = refs if refs is not None else ["v1.18.35", "v2.0.25", "v2.0.26"]
|
||||
self.pi = pi if pi is not None else {"version": "1.1.0", "engines": {"node": ">=22.19.0"}}
|
||||
self.digest = digest
|
||||
self.digests = digests or {}
|
||||
self.refs_response = refs_response
|
||||
self.calls = []
|
||||
|
||||
def __call__(self, url, headers=None):
|
||||
self.calls.append(url)
|
||||
if "/git/matching-refs/" in url:
|
||||
if self.refs_response is not None:
|
||||
return 200, {}, json.dumps(self.refs_response).encode()
|
||||
body = [{"ref": "refs/tags/" + name} for name in self.refs]
|
||||
return 200, {}, json.dumps(body).encode()
|
||||
if "/token?" in url:
|
||||
return 200, {}, json.dumps({"token": "fixture"}).encode()
|
||||
if "/manifests/" in url:
|
||||
tag = url.rsplit("/", 1)[-1]
|
||||
if tag in self.digests:
|
||||
value = self.digests[tag]
|
||||
headers = {} if value is None else {"docker-content-digest": value}
|
||||
return 200, headers, b""
|
||||
return 200, {"docker-content-digest": self.digest}, b""
|
||||
if "registry.npmjs.org" in url:
|
||||
return 200, {}, json.dumps(self.pi).encode()
|
||||
return 404, {}, b""
|
||||
|
||||
|
||||
class ReleaseResolutionTests(unittest.TestCase):
|
||||
def test_selects_highest_v2_from_one_complete_matching_refs_request(self):
|
||||
registry = FakeRegistry()
|
||||
result = resolve_releases.resolve_opencode(registry)
|
||||
self.assertEqual(result["version"], "2.0.26")
|
||||
self.assertEqual(result["digest"], "sha256:" + "a" * 64)
|
||||
self.assertEqual(result["candidates"], ["2.0.25", "2.0.26"])
|
||||
self.assertNotIn("pages", result)
|
||||
calls = " ".join(registry.calls)
|
||||
self.assertNotIn("releases/latest", calls)
|
||||
self.assertNotIn("last=", calls)
|
||||
self.assertNotIn("/tags/list", calls, "redundant registry cross-check must be gone")
|
||||
self.assertIn("matching-refs", result["anchor"])
|
||||
ref_urls = [url for url in registry.calls if "/git/matching-refs/" in url]
|
||||
self.assertEqual(len(ref_urls), 1, "matching-refs must be a single bounded request")
|
||||
self.assertNotIn("per_page", ref_urls[0])
|
||||
self.assertNotIn("page=", ref_urls[0])
|
||||
self.assertNotIn("?", ref_urls[0])
|
||||
manifest_calls = [url for url in registry.calls if "/manifests/" in url]
|
||||
self.assertEqual(len(manifest_calls), 1)
|
||||
self.assertIn("2.0.26", manifest_calls[0])
|
||||
|
||||
def test_complete_100_plus_ref_array_resolves_in_one_request(self):
|
||||
refs = [f"v2.0.{i}" for i in range(120)] + ["v2.1.0", "v2.0.26"]
|
||||
registry = FakeRegistry(refs=refs)
|
||||
result = resolve_releases.resolve_opencode(registry)
|
||||
self.assertEqual(result["version"], "2.1.0")
|
||||
ref_urls = [url for url in registry.calls if "/git/matching-refs/" in url]
|
||||
self.assertEqual(len(ref_urls), 1)
|
||||
self.assertFalse(any("/tags/list" in url for url in registry.calls))
|
||||
|
||||
def test_over_cap_ref_array_fails_closed(self):
|
||||
registry = FakeRegistry(refs=[f"v2.0.{i}" for i in range(resolve_releases.MAX_REFS + 1)])
|
||||
with self.assertRaisesRegex(resolve_releases.ReleaseError, "cap"):
|
||||
resolve_releases.resolve_opencode(registry)
|
||||
self.assertFalse(any("/manifests/" in url for url in registry.calls))
|
||||
|
||||
def test_oversized_response_body_is_rejected(self):
|
||||
def fetch(url, headers=None):
|
||||
if "/git/matching-refs/" in url:
|
||||
return 200, {}, b"[" + b" " * (resolve_releases.MAX_BODY + 1)
|
||||
return 404, {}, b""
|
||||
|
||||
with self.assertRaisesRegex(resolve_releases.ReleaseError, "exceeds"):
|
||||
resolve_releases.resolve_opencode(fetch)
|
||||
|
||||
def test_newer_v1_tag_is_ignored(self):
|
||||
registry = FakeRegistry(refs=["v2.0.25", "v2.0.26", "v1.19.0"])
|
||||
result = resolve_releases.resolve_opencode(registry)
|
||||
self.assertEqual(result["version"], "2.0.26")
|
||||
self.assertNotIn("1.19.0", result["candidates"])
|
||||
self.assertEqual(result["candidates"], ["2.0.25", "2.0.26"])
|
||||
|
||||
def test_stable_v2_beats_newer_prerelease_and_build_suffix(self):
|
||||
registry = FakeRegistry(refs=["v2.0.26", "v2.0.27-rc.1", "v2.0.27+build.5"])
|
||||
result = resolve_releases.resolve_opencode(registry)
|
||||
self.assertEqual(result["version"], "2.0.26")
|
||||
self.assertEqual(result["candidates"], ["2.0.26"])
|
||||
|
||||
def test_missing_newest_digest_fails_closed_without_older_fallback(self):
|
||||
registry = FakeRegistry(refs=["v2.0.25", "v2.0.26"], digests={"2.0.26": None})
|
||||
with self.assertRaisesRegex(resolve_releases.ReleaseError, "digest"):
|
||||
resolve_releases.resolve_opencode(registry)
|
||||
manifest_calls = [url for url in registry.calls if "/manifests/" in url]
|
||||
self.assertEqual([url.rsplit("/", 1)[-1] for url in manifest_calls], ["2.0.26"])
|
||||
self.assertFalse(any("/manifests/2.0.25" in url for url in registry.calls))
|
||||
|
||||
def test_no_supported_v2_in_official_metadata_fails_closed(self):
|
||||
registry = FakeRegistry(refs=["v1.18.35", "v3.0.0"])
|
||||
with self.assertRaisesRegex(resolve_releases.ReleaseError, "no supported OpenCode V2"):
|
||||
resolve_releases.resolve_opencode(registry)
|
||||
|
||||
def test_malformed_github_metadata_is_rejected(self):
|
||||
cases = [
|
||||
[42],
|
||||
[{"ref": "refs/heads/main"}],
|
||||
[{"ref": "refs/tags/not-a-version"}],
|
||||
[{"ref": 123}],
|
||||
[{"noref": "refs/tags/v2.0.26"}],
|
||||
]
|
||||
for refs in cases:
|
||||
registry = FakeRegistry(refs_response=refs)
|
||||
with self.assertRaises(resolve_releases.ReleaseError):
|
||||
resolve_releases.resolve_opencode(registry)
|
||||
|
||||
def test_non_list_github_payload_is_rejected(self):
|
||||
def fetch(url, headers=None):
|
||||
if "/git/matching-refs/" in url:
|
||||
return 200, {}, json.dumps({"ref": "refs/tags/v2.0.26"}).encode()
|
||||
return 404, {}, b""
|
||||
|
||||
with self.assertRaisesRegex(resolve_releases.ReleaseError, "not a list"):
|
||||
resolve_releases.resolve_opencode(fetch)
|
||||
|
||||
def test_invalid_manifest_digest_is_rejected(self):
|
||||
with self.assertRaisesRegex(resolve_releases.ReleaseError, "digest"):
|
||||
resolve_releases.resolve_opencode(FakeRegistry(digest="not-a-digest"))
|
||||
|
||||
def test_non_registry_host_is_rejected(self):
|
||||
with self.assertRaisesRegex(resolve_releases.ReleaseError, "non-registry host"):
|
||||
resolve_releases._default_fetch("https://evil.example.com/tags/list")
|
||||
|
||||
def test_malformed_pi_metadata_is_rejected(self):
|
||||
for pi in ({"version": "not-semver"}, {"version": "1.1.0", "engines": "node"}, 42):
|
||||
with self.assertRaises(resolve_releases.ReleaseError):
|
||||
resolve_releases.resolve_pi(FakeRegistry(pi=pi))
|
||||
|
||||
def test_unsupported_node_engine_range_fails_closed(self):
|
||||
for engine in (">=22 <24", "^22.19.0", ">=22.0.0 || >=20"):
|
||||
with self.assertRaisesRegex(resolve_releases.ReleaseError, "unsupported"):
|
||||
resolve_releases.node_satisfies(engine, "22.19.0")
|
||||
|
||||
def test_resolve_pi_validates_node_engine(self):
|
||||
registry = FakeRegistry()
|
||||
result = resolve_releases.resolve_pi(registry, node_version="22.19.0")
|
||||
self.assertEqual(result["version"], "1.1.0")
|
||||
with self.assertRaisesRegex(resolve_releases.ReleaseError, "requires Node"):
|
||||
resolve_releases.resolve_pi(FakeRegistry(), node_version="20.0.0")
|
||||
|
||||
def test_combined_resolution_shape(self):
|
||||
result = resolve_releases.resolve(FakeRegistry(), node_version="22.19.0")
|
||||
self.assertEqual(set(result), {"opencode", "pi"})
|
||||
self.assertEqual(result["opencode"]["image"], resolve_releases.OPENCODE_IMAGE)
|
||||
|
||||
def test_load_fixture_rejects_incomplete_data(self):
|
||||
import tempfile
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
path = Path(directory, "fixture.json")
|
||||
path.write_text(json.dumps({"opencode": {}}))
|
||||
with self.assertRaises(resolve_releases.ReleaseError):
|
||||
resolve_releases.load_fixture(path)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -16,13 +16,268 @@ spec.loader.exec_module(setup)
|
||||
|
||||
|
||||
class DockerSetupTests(unittest.TestCase):
|
||||
def test_host_update_installs_clients_not_server(self):
|
||||
calls = []
|
||||
def runner(command, **kwargs):
|
||||
calls.append(command)
|
||||
return subprocess.CompletedProcess(command, 0)
|
||||
with patch.object(setup.shutil, "which", return_value="go"):
|
||||
setup.Setup(".", runner=runner).update_host_client()
|
||||
self.assertEqual(calls[0], ["go", "install", "-trimpath", "./cmd/pipelines", "./cmd/pipelines-tui"])
|
||||
self.assertFalse(any("./cmd/pipelines-server" in command or "./cmd/..." in command for command in calls))
|
||||
def test_fuzzy_model_picker_runs_in_container_with_private_files_and_terminal_input(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
calls = []
|
||||
def runner(command, **kwargs):
|
||||
calls.append(command)
|
||||
self.assertNotIn("-T", command, "interactive picker lost its terminal")
|
||||
self.assertFalse(kwargs.get("capture_output"), "picker output must not be captured")
|
||||
catalog = Path(command[command.index("--options-file")+1])
|
||||
self.assertEqual(setup.json.loads(catalog.read_text()), ["fixture/first", "fixture/second"])
|
||||
self.assertEqual(catalog.stat().st_mode & 0o777, 0o600)
|
||||
Path(command[command.index("--output")+1]).write_text("fixture/second\n")
|
||||
return subprocess.CompletedProcess(command, 0)
|
||||
installer = setup.Setup(directory, runner=runner)
|
||||
with patch.object(setup.shutil, "which", return_value="go"):
|
||||
self.assertEqual(installer.choose_model(["fixture/first", "fixture/second"], "Choose model"), "fixture/second")
|
||||
self.assertEqual(calls[-1][:5], ["docker", "compose", "run", "--rm", "--no-deps"])
|
||||
self.assertNotIn("go", calls[-1], "picker fell back to a host runtime")
|
||||
self.assertEqual(list(Path(directory).iterdir()), [])
|
||||
|
||||
def test_unidentifiable_source_fails_before_settings_or_startup(self):
|
||||
calls = []
|
||||
def runner(command, **kwargs):
|
||||
calls.append(command)
|
||||
return subprocess.CompletedProcess(command, 1 if command[0] == "git" else 0, stdout="", stderr="")
|
||||
installer = setup.Setup(".", runner=runner)
|
||||
with patch.object(installer, "settings", side_effect=AssertionError("must not modify settings")):
|
||||
with self.assertRaisesRegex(ValueError, "Cannot identify source"):
|
||||
installer.run(setup.arguments([]))
|
||||
self.assertFalse(any("up" in command or "build" in command for command in calls))
|
||||
|
||||
def test_running_build_must_match_compiled_source(self):
|
||||
expected = {"PIPELINES_VERSION": "a" * 12, "PIPELINES_COMMIT": "a" * 40, "PIPELINES_DIRTY": "false"}
|
||||
good = {"type": "pong", "build": {"version": "a" * 12, "commit": "a" * 40, "dirty": False}}
|
||||
for report, accepted in [(good, True), ({"type": "pong"}, False),
|
||||
({**good, "build": {**good["build"], "commit": "b" * 40}}, False),
|
||||
({**good, "build": {**good["build"], "dirty": None}}, False),
|
||||
({**good, "type": "error"}, False), ([], False)]:
|
||||
def runner(command, **kwargs):
|
||||
return subprocess.CompletedProcess(command, 0, stdout=setup.json.dumps(report), stderr="")
|
||||
installer = setup.Setup(".", runner=runner)
|
||||
if accepted:
|
||||
installer.verify_build(expected)
|
||||
else:
|
||||
with self.assertRaisesRegex(ValueError, "setup is NOT complete"):
|
||||
installer.verify_build(expected)
|
||||
|
||||
def setUp(self):
|
||||
directory = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(directory.cleanup)
|
||||
environment = patch.dict(os.environ, {"XDG_CONFIG_HOME": directory.name})
|
||||
home = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(home.cleanup)
|
||||
# Fixtures must never see the container default /home/pipelines; inject a
|
||||
# disposable HOME/USERPROFILE (host_workspaces still rejects unsafe paths).
|
||||
environment = patch.dict(os.environ, {"XDG_CONFIG_HOME": directory.name,
|
||||
"HOME": home.name, "USERPROFILE": home.name})
|
||||
environment.start()
|
||||
self.addCleanup(environment.stop)
|
||||
|
||||
def test_conditional_rebuild_uses_receipt_and_verified_image_identity(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
releases = {"opencode": {"version": "2.0.26", "digest": "sha256:" + "a" * 64}, "pi": {"version": "1.1.0"}}
|
||||
identity = {"PIPELINES_VERSION": "b" * 12, "PIPELINES_COMMIT": "b" * 40, "PIPELINES_DIRTY": "false"}
|
||||
image_labels = {}
|
||||
def runner(command, **kwargs):
|
||||
if command[2:4] == ["images", "-q"]:
|
||||
return subprocess.CompletedProcess(command, 0, stdout="imageid\n", stderr="")
|
||||
if command[:2] == ["docker", "image"]:
|
||||
return subprocess.CompletedProcess(command, 0, stdout=setup.json.dumps(image_labels), stderr="")
|
||||
return subprocess.CompletedProcess(command, 0, stdout="", stderr="")
|
||||
installer = setup.Setup(directory, runner=runner)
|
||||
self.assertTrue(installer.needs_image_build(releases, identity), "missing image must build")
|
||||
installer.record_release_receipt(releases, identity)
|
||||
image_labels.update(installer.expected_labels(releases, identity))
|
||||
self.assertFalse(installer.needs_image_build(releases, identity), "unchanged image/source rebuilt")
|
||||
changed_source = {**identity, "PIPELINES_VERSION": "b" * 12 + "-dirty", "PIPELINES_DIRTY": "true"}
|
||||
self.assertTrue(installer.needs_image_build(releases, changed_source, force_dirty=True), "changed source skipped install rebuild")
|
||||
changed = {"opencode": {"version": "2.0.27", "digest": "sha256:" + "c" * 64}, "pi": {"version": "1.1.0"}}
|
||||
self.assertTrue(installer.needs_image_build(changed, identity), "changed release not rebuilt")
|
||||
# The receipt now matches, but the actual image is unrelated: labels win.
|
||||
installer.record_release_receipt(changed, identity)
|
||||
self.assertTrue(installer.needs_image_build(changed, identity), "unrelated image accepted on receipt alone")
|
||||
receipt = setup.json.loads((Path(directory) / ".pipelines-releases.json").read_text())
|
||||
self.assertEqual(receipt["opencode"]["version"], "2.0.27")
|
||||
self.assertEqual(receipt["build"], identity)
|
||||
self.assertEqual((Path(directory) / ".pipelines-releases.json").stat().st_mode & 0o777, 0o600)
|
||||
build_args = installer.release_build_args(releases)
|
||||
self.assertIn("OPENCODE_VERSION=2.0.26", build_args)
|
||||
self.assertIn("OPENCODE_DIGEST=sha256:" + "a" * 64, build_args)
|
||||
|
||||
def test_dirty_source_forces_install_rebuild_but_not_reconfiguration(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
releases = {"opencode": {"version": "2.0.26", "digest": "sha256:" + "a" * 64}, "pi": {"version": "1.1.0"}}
|
||||
dirty = {"PIPELINES_VERSION": "b" * 12, "PIPELINES_COMMIT": "b" * 40, "PIPELINES_DIRTY": "true"}
|
||||
image_labels = {}
|
||||
def runner(command, **kwargs):
|
||||
if command[2:4] == ["images", "-q"]:
|
||||
return subprocess.CompletedProcess(command, 0, stdout="imageid\n", stderr="")
|
||||
if command[:2] == ["docker", "image"]:
|
||||
return subprocess.CompletedProcess(command, 0, stdout=setup.json.dumps(image_labels), stderr="")
|
||||
return subprocess.CompletedProcess(command, 0, stdout="", stderr="")
|
||||
installer = setup.Setup(directory, runner=runner)
|
||||
installer.record_release_receipt(releases, dirty)
|
||||
image_labels.update(installer.expected_labels(releases, dirty))
|
||||
# Identical dirty identity must not let changed source skip the build.
|
||||
self.assertTrue(installer.needs_image_build(releases, dirty, force_dirty=True))
|
||||
# Reconfiguration reuses the verified release image without a rebuild.
|
||||
self.assertFalse(installer.needs_image_build(releases, dirty))
|
||||
|
||||
def test_reconfiguration_ignores_source_identity_but_install_builds(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
releases = {"opencode": {"version": "2.0.26", "digest": "sha256:" + "a" * 64}, "pi": {"version": "1.1.0"}}
|
||||
built_with = {"PIPELINES_VERSION": "b" * 12, "PIPELINES_COMMIT": "b" * 40, "PIPELINES_DIRTY": "false"}
|
||||
other_source = {"PIPELINES_VERSION": "c" * 12, "PIPELINES_COMMIT": "c" * 40, "PIPELINES_DIRTY": "false"}
|
||||
image_labels = {}
|
||||
def runner(command, **kwargs):
|
||||
if command[2:4] == ["images", "-q"]:
|
||||
return subprocess.CompletedProcess(command, 0, stdout="imageid\n", stderr="")
|
||||
if command[:2] == ["docker", "image"]:
|
||||
return subprocess.CompletedProcess(command, 0, stdout=setup.json.dumps(image_labels), stderr="")
|
||||
return subprocess.CompletedProcess(command, 0, stdout="", stderr="")
|
||||
installer = setup.Setup(directory, runner=runner)
|
||||
installer.record_release_receipt(releases, other_source)
|
||||
image_labels.update(installer.expected_labels(releases, other_source))
|
||||
# Release identity matches; a model/login-only reconfigure must not rebuild.
|
||||
self.assertFalse(installer.needs_image_build(releases, built_with),
|
||||
"source identity forced a reconfiguration rebuild")
|
||||
# A full install with a different source identity must rebuild.
|
||||
self.assertTrue(installer.needs_image_build(releases, built_with, force_dirty=True),
|
||||
"different source identity skipped the install rebuild")
|
||||
|
||||
def test_corrupted_receipt_fails_closed_and_rebuilds(self):
|
||||
releases = {"opencode": {"version": "2.0.26", "digest": "sha256:" + "a" * 64}, "pi": {"version": "1.1.0"}}
|
||||
identity = {"PIPELINES_VERSION": "b" * 12}
|
||||
installer = setup.Setup(".")
|
||||
for receipt in ({"opencode": "x"}, {"pi": []}, {"opencode": {}, "pi": {}}, {"opencode": {"version": "2.0.26"}}, None, []):
|
||||
self.assertFalse(installer.receipt_matches(receipt, releases, identity), receipt)
|
||||
matching = {"opencode": {"version": "2.0.26", "digest": "sha256:" + "a" * 64},
|
||||
"pi": {"version": "1.1.0"}, "build": identity}
|
||||
self.assertTrue(installer.receipt_matches(matching, releases, identity))
|
||||
|
||||
def test_link_host_reuses_container_profile_and_falls_back_to_pipelines_client(self):
|
||||
calls = []
|
||||
def runner(command, **kwargs):
|
||||
if command[0] == "docker":
|
||||
return subprocess.CompletedProcess(command, 0,
|
||||
stdout='{"default":"pi","profiles":{"pi":{"model":"m","pipeline_model":"pm"}}}', stderr="")
|
||||
calls.append(command)
|
||||
return subprocess.CompletedProcess(command, 0)
|
||||
installer = setup.Setup(".", runner=runner)
|
||||
with patch.object(setup.shutil, "which", side_effect=lambda name: None if name == "go" else "/usr/bin/pipelines"):
|
||||
installer.link_host()
|
||||
self.assertEqual(calls, [["/usr/bin/pipelines", "harness", "link", "pi",
|
||||
"--docker-directory", str(Path(".").resolve()),
|
||||
"--model", "m", "--pipeline-model", "pm", "--default"]])
|
||||
|
||||
def test_reconfigure_reuses_resolution_and_refuses_stale_image(self):
|
||||
releases = {"opencode": {"version": "2.0.26", "digest": "sha256:" + "a" * 64},
|
||||
"pi": {"version": "1.1.0", "engine": ">=22.19.0"}}
|
||||
calls = []
|
||||
def runner(command, **kwargs):
|
||||
calls.append(command)
|
||||
return subprocess.CompletedProcess(command, 0, stdout="", stderr="")
|
||||
installer = setup.Setup(".", runner=runner)
|
||||
installer.resolve_releases = lambda: releases
|
||||
installer.build_identity = lambda: {"PIPELINES_VERSION": "b" * 12}
|
||||
installer.needs_image_build = lambda resolved, identity: True
|
||||
with self.assertRaisesRegex(ValueError, "run ./install"):
|
||||
installer.reconfigure_harness("pi", [])
|
||||
self.assertEqual(calls, [], "stale reconfigure must not touch Docker")
|
||||
|
||||
def test_reconfigure_verifies_and_configures_without_rebuilding(self):
|
||||
releases = {"opencode": {"version": "2.0.26", "digest": "sha256:" + "a" * 64},
|
||||
"pi": {"version": "1.1.0", "engine": ">=22.19.0"}}
|
||||
calls = []
|
||||
def runner(command, **kwargs):
|
||||
calls.append(command)
|
||||
return subprocess.CompletedProcess(command, 0, stdout="", stderr="")
|
||||
installer = setup.Setup(".", runner=runner)
|
||||
installer.resolve_releases = lambda: releases
|
||||
installer.build_identity = lambda: {"PIPELINES_VERSION": "b" * 12}
|
||||
installer.needs_image_build = lambda resolved, identity: False
|
||||
verified = []
|
||||
installer.verify_harness_versions = lambda resolved: verified.append(resolved)
|
||||
installer.reconfigure_harness("opencode", ["--model", "fixture/model"])
|
||||
self.assertEqual(verified, [releases])
|
||||
self.assertEqual(calls[0][:4], ["docker", "compose", "exec", "pipelines"])
|
||||
self.assertIn("harness", calls[0])
|
||||
self.assertFalse(any("build" in call for call in calls))
|
||||
|
||||
def test_stale_image_version_mismatch_fails_closed(self):
|
||||
def runner(command, **kwargs):
|
||||
return subprocess.CompletedProcess(command, 0, stdout="opencode=2.0.20\npi=0.87.1\nnode=v22.19.0\n", stderr="")
|
||||
installer = setup.Setup(".", runner=runner)
|
||||
with self.assertRaisesRegex(ValueError, "stale or mismatched"):
|
||||
installer.verify_harness_versions({"opencode": {"version": "2.0.26"}, "pi": {"version": "1.1.0", "engine": ">=22.19.0"}})
|
||||
|
||||
def test_exact_version_token_and_node_engine_are_enforced(self):
|
||||
releases = {"opencode": {"version": "2.0.26"}, "pi": {"version": "1.1.0", "engine": ">=22.19.0"}}
|
||||
rejected = ["opencode=v2.0.260\npi=1.1.0\nnode=v22.19.0\n", # parsed 260 != expected 26 (exact equality)
|
||||
"opencode=2.0.26\npi=1.1.0\n", # missing Node fails closed
|
||||
"opencode=2.0.26\npi=1.1.0\nnode=v20.0.0\n", # unsatisfied engine
|
||||
"opencode=v2.0.26-rc.1\npi=1.1.0\nnode=v22.19.0\n", # arbitrary suffix
|
||||
"opencode=OpenCode v2.0.26\npi=1.1.0\nnode=v22.19.0\n", # banner, not an exact token
|
||||
"opencode=v2.0.26 extra\npi=1.1.0\nnode=v22.19.0\n", # extra token after version
|
||||
"opencode=v2.0.26\npi=v1.1.0\nnode=v22.19.0\n", # Pi's format has no leading v
|
||||
"opencode=v2.0.26\npi=1.1.0\nnode=node v22.19.0\n"] # Node banner, not an exact token
|
||||
for output in rejected:
|
||||
def runner(command, **kwargs):
|
||||
return subprocess.CompletedProcess(command, 0, stdout=output, stderr="")
|
||||
with self.assertRaises(ValueError):
|
||||
setup.Setup(".", runner=runner).verify_harness_versions(releases)
|
||||
def good(command, **kwargs):
|
||||
return subprocess.CompletedProcess(command, 0, stdout="opencode=opencode v2.0.26\npi=1.1.0\nnode=v24.18.1\n", stderr="")
|
||||
setup.Setup(".", runner=good).verify_harness_versions(releases)
|
||||
|
||||
def test_three_digit_openCode_patch_is_compared_by_exact_equality(self):
|
||||
three = {"opencode": {"version": "2.0.100"}, "pi": {"version": "1.1.0"}}
|
||||
def matching(command, **kwargs):
|
||||
return subprocess.CompletedProcess(command, 0, stdout="opencode=opencode v2.0.100\npi=1.1.0\nnode=v24.18.1\n", stderr="")
|
||||
setup.Setup(".", runner=matching).verify_harness_versions(three)
|
||||
def different(command, **kwargs):
|
||||
return subprocess.CompletedProcess(command, 0, stdout="opencode=opencode v2.0.260\npi=1.1.0\nnode=v24.18.1\n", stderr="")
|
||||
with self.assertRaises(ValueError):
|
||||
setup.Setup(".", runner=different).verify_harness_versions(three)
|
||||
|
||||
def test_parse_harness_version_accepts_only_exact_known_formats(self):
|
||||
accepted = {("opencode", "v2.0.26"): "2.0.26",
|
||||
("opencode", "2.0.26"): "2.0.26",
|
||||
("opencode", "opencode v2.0.26"): "2.0.26",
|
||||
("opencode", "opencode 2.0.26"): "2.0.26",
|
||||
("opencode", "opencode v2.0.260"): "2.0.260",
|
||||
("opencode", "opencode v2.0.100"): "2.0.100",
|
||||
("opencode", "v2.0.260"): "2.0.260",
|
||||
("pi", "1.1.0"): "1.1.0",
|
||||
("node", "v24.18.1"): "24.18.1",
|
||||
("node", "24.18.1"): "24.18.1"}
|
||||
for (name, value), normalized in accepted.items():
|
||||
self.assertEqual(setup.parse_harness_version(name, value), normalized)
|
||||
for name, value in (("opencode", "v2.0.26-rc.1"), ("opencode", "OpenCode v2.0.26"),
|
||||
("opencode", "opencode v2.0.26 extra"), ("opencode", "opencode v2.0.26.1"),
|
||||
("pi", "v1.1.0"), ("pi", "1.1.0 extra"), ("node", "22.19.0.1"),
|
||||
("node", ""), ("unknown", "1.2.3")):
|
||||
with self.assertRaises(ValueError):
|
||||
setup.parse_harness_version(name, value)
|
||||
|
||||
def test_release_resolution_failure_keeps_verified_pins(self):
|
||||
installer = setup.Setup(".")
|
||||
def fail():
|
||||
raise setup.release_lookup.ReleaseError("registry unreachable")
|
||||
installer.resolver = fail
|
||||
with self.assertRaisesRegex(ValueError, "keeping the last verified Dockerfile pins"):
|
||||
installer.resolve_releases()
|
||||
|
||||
def test_verified_client_default_preserves_unrelated_settings_and_warns_on_override(self):
|
||||
path = Path(os.environ["XDG_CONFIG_HOME"])/"pipelines/client.json"
|
||||
path.parent.mkdir()
|
||||
@@ -63,6 +318,17 @@ class DockerSetupTests(unittest.TestCase):
|
||||
setup.Setup(directory).configure_workspaces()
|
||||
self.assertEqual(path.read_text(),content)
|
||||
|
||||
def test_host_workspaces_rejects_unsafe_home_and_equal_repository(self):
|
||||
with tempfile.TemporaryDirectory() as repository:
|
||||
for home in ["/", "/data", "/workspace", "/home/pipelines"]:
|
||||
with patch.dict(os.environ, {"HOME": home, "USERPROFILE": home}):
|
||||
with self.assertRaises(ValueError):
|
||||
setup.configure.host_workspaces(repository)
|
||||
# home == repository must also be rejected, not mounted at itself.
|
||||
with patch.dict(os.environ, {"HOME": repository, "USERPROFILE": repository}):
|
||||
with self.assertRaises(ValueError):
|
||||
setup.configure.host_workspaces(repository)
|
||||
|
||||
def test_ownership_migration_and_workspace_verification_are_scoped(self):
|
||||
calls=[]
|
||||
def runner(command,**kwargs):
|
||||
@@ -108,13 +374,15 @@ printf '%s' "$3" > "$FIXTURE/model"
|
||||
echo '{"healthy":true,"session_id":"ses_validation"}'
|
||||
''');pipelines.chmod(0o700)
|
||||
def runner(command,**kwargs):
|
||||
if "harness" in command:
|
||||
return subprocess.CompletedProcess(command,0,stdout="",stderr="")
|
||||
index=command.index("-c")
|
||||
# Each one-off container has a separate service/catalog.
|
||||
(root/"primed").unlink(missing_ok=True)
|
||||
return subprocess.run(["sh",*command[index:]],capture_output=True,text=True,
|
||||
env=dict(os.environ,PATH=str(root)+os.pathsep+os.environ["PATH"],FIXTURE=str(root)))
|
||||
installer=setup.Setup(directory,runner=runner)
|
||||
installer.validate_provider(setup.arguments(["--model","opencode-go/deepseek-v4.1-flash"]))
|
||||
installer.validate_provider(setup.arguments(["--model","opencode-go/deepseek-v4.1-flash","--harness-model","opencode-go/deepseek-v4.1-flash"]))
|
||||
self.assertEqual((root/"model").read_text(),"opencode-go/deepseek-v4.1-flash")
|
||||
self.assertIn("CUSTOM=keep\n",(root/".env").read_text())
|
||||
|
||||
@@ -205,7 +473,7 @@ fi
|
||||
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"]))
|
||||
installer.validate_provider(setup.arguments(["--model","fixture/new","--harness-model","fixture/new"]))
|
||||
self.assertEqual(before,path.read_bytes())
|
||||
self.assertTrue(any(any("provider-check" in arg for arg in command) for command in calls))
|
||||
|
||||
@@ -217,22 +485,29 @@ fi
|
||||
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"]))
|
||||
setup.Setup(directory,runner=runner).validate_provider(setup.arguments(["--model","fixture/unavailable","--harness-model","fixture/available"]))
|
||||
self.assertFalse(any(any("provider-check" in arg for arg 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()
|
||||
for name in ["setup-docker.py","lan-address.py","configure-compose.py","save-ntfy-token.py"]:
|
||||
for name in ["setup-docker.py","lan-address.py","configure-compose.py","save-ntfy-token.py","build_metadata.py","resolve-releases.py"]:
|
||||
shutil.copy2(Path(__file__).with_name(name),scripts/name)
|
||||
fixture=root/"releases.json"
|
||||
fixture.write_text(setup.json.dumps({"opencode":{"image":"ghcr.io/anomalyco/opencode","version":"2.0.26","digest":"sha256:"+"a"*64},"pi":{"version":"1.1.0","engine":">=22.19.0"}}))
|
||||
marker=root/"waiting"
|
||||
docker=root/"docker"
|
||||
docker.write_text('#!/bin/sh\ncase "$*" in *" up "*) touch "'+str(marker)+'"; exec sleep 30;; esac\n')
|
||||
docker.write_text('#!/bin/sh\ncase "$*" in *" up "*) touch "'+str(marker)+'"; exec sleep 30;; *"opencode --version"*) printf "opencode=2.0.26\\npi=1.1.0\\nnode=v22.19.0\\n";; esac\n')
|
||||
docker.chmod(0o700)
|
||||
go=root/"go";go.write_text('#!/bin/sh\nexit 0\n');go.chmod(0o700)
|
||||
git=root/"git";git.write_text('#!/bin/sh\nif [ "$1" = rev-parse ]; then echo '+"a"*40+'; fi\n');git.chmod(0o700)
|
||||
# The container's default home (/home/pipelines) is rejected by the
|
||||
# mount guard; give the subprocess its own disposable HOME.
|
||||
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"]),
|
||||
env=dict(os.environ,PATH=str(root)+os.pathsep+os.environ["PATH"],
|
||||
HOME=str(root/"home"),USERPROFILE=str(root/"home"),
|
||||
PIPELINES_RELEASE_FIXTURE=str(fixture)),
|
||||
stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True,start_new_session=True)
|
||||
try:
|
||||
deadline=time.monotonic()+5
|
||||
@@ -275,13 +550,16 @@ fi
|
||||
setup.Setup(directory).settings(setup.arguments([]))
|
||||
self.assertFalse(Path(directory,".env").exists())
|
||||
|
||||
def exercise(self, directory, existing=False, fail=None, confirm="yes"):
|
||||
def exercise(self, directory, existing=False, fail=None, confirm="yes", harness="opencode"):
|
||||
calls = []
|
||||
token = "tk_" + "a" * 29
|
||||
logged = existing
|
||||
|
||||
def runner(command, **kwargs):
|
||||
nonlocal logged
|
||||
if command[0] == "git":
|
||||
out = "a" * 40 if command[1] == "rev-parse" else ""
|
||||
return subprocess.CompletedProcess(command, 0, stdout=out, stderr="")
|
||||
args = command[2:]
|
||||
calls.append(args)
|
||||
code, out = 0, ""
|
||||
@@ -297,8 +575,14 @@ fi
|
||||
out = f"token {token} created\n"
|
||||
if args[-2:] == ["status", "--json"]:
|
||||
out = '{"ntfy":{"health":"healthy","url":"http://192.168.1.20:8090"},"topic":"pipelines"}'
|
||||
if args[-2:] == ["ping", "--json"]:
|
||||
out = setup.json.dumps({"type": "pong", "build": {"version": "a" * 12, "commit": "a" * 40, "dirty": False}})
|
||||
if args[-3:]==["harness","list","--json"]:
|
||||
out = setup.json.dumps({"default":harness,"profiles":{harness:{"model":"fixture/test-model","pipeline_model":"fixture/test-model"}}})
|
||||
if args[:1] == ["port"]:
|
||||
out = "127.0.0.1:8081\n"
|
||||
if "opencode --version" in args[-1]:
|
||||
out = "opencode=2.0.26\npi=1.1.0\nnode=v22.19.0\n"
|
||||
if args == ["config", "--format", "json"]:
|
||||
out = setup.json.dumps({"services":{"pipelines":{"volumes":[{"type":"volume","target":target} for target in ["/data","/home/pipelines","/workspace"]]}}})
|
||||
if fail and (fail in args or (fail=="login" and "exec opencode auth login" in args[-1]) or (fail=="provider-check" and any("provider-check" in arg for arg in args))):
|
||||
@@ -309,16 +593,54 @@ fi
|
||||
|
||||
args = setup.arguments([] if existing else ["--ntfy-url", "http://192.168.1.20:8090",
|
||||
"--bind-address", "192.168.1.20", "--timezone", "Europe/London"])
|
||||
args.harness=harness
|
||||
installer = setup.Setup(directory, runner=runner, prompt=lambda _: "1")
|
||||
installer.resolver = lambda: {"opencode": {"image": "ghcr.io/anomalyco/opencode", "version": "2.0.26", "digest": "sha256:" + "a" * 64},
|
||||
"pi": {"version": "1.1.0", "engine": ">=22.19.0"}}
|
||||
installer.choose_model=lambda models, title, purpose="", step="", current="": models[0]
|
||||
with patch.object(setup.lan,"detect_lan",return_value="192.168.1.20"):
|
||||
installer.run(args)
|
||||
return calls, token
|
||||
|
||||
def test_fresh_pi_setup_skips_opencode_login_and_generation(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
calls, _ = self.exercise(directory, harness="pi")
|
||||
setup_call = next(c for c in calls if "setup" in c and "pi" in c)
|
||||
self.assertIn("--login", setup_call)
|
||||
self.assertIn("--default", setup_call)
|
||||
self.assertIn("--versions", setup_call)
|
||||
self.assertIn("OpenCode 2.0.26", setup_call[setup_call.index("--versions") + 1])
|
||||
self.assertFalse(any("exec opencode auth login" in c[-1] for c in calls))
|
||||
self.assertFalse(any(any("provider-check" in arg for arg in c) for c in calls))
|
||||
|
||||
def test_reconfigure_passes_verified_versions_to_onboarding(self):
|
||||
releases = {"opencode": {"version": "2.0.26", "digest": "sha256:" + "a" * 64},
|
||||
"pi": {"version": "1.1.0", "engine": ">=22.19.0"}}
|
||||
calls = []
|
||||
def runner(command, **kwargs):
|
||||
calls.append(command)
|
||||
return subprocess.CompletedProcess(command, 0, stdout="", stderr="")
|
||||
installer = setup.Setup(".", runner=runner)
|
||||
installer.resolve_releases = lambda: releases
|
||||
installer.build_identity = lambda: {"PIPELINES_VERSION": "b" * 12}
|
||||
installer.needs_image_build = lambda resolved, identity: False
|
||||
installer.verify_harness_versions = lambda resolved: {"opencode": "2.0.26", "pi": "1.1.0", "node": "22.19.0"}
|
||||
installer.reconfigure_harness("pi", ["--model", "fixture/model"])
|
||||
setup_call = next(c for c in calls if "setup" in c)
|
||||
self.assertIn("--login", setup_call)
|
||||
self.assertIn("--versions", setup_call)
|
||||
label = setup_call[setup_call.index("--versions") + 1]
|
||||
for want in ("OpenCode 2.0.26", "Pi 1.1.0", "Node 22.19.0"):
|
||||
self.assertIn(want, label)
|
||||
|
||||
def test_fresh_setup_wires_notifications_and_runs_container_login(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
calls, token = self.exercise(directory)
|
||||
self.assertTrue(any("exec opencode auth login" in c[-1] for c in calls))
|
||||
self.assertTrue(any("--force-recreate" in c for c in calls))
|
||||
build = next(c for c in calls if "build" in c)
|
||||
self.assertIn("PIPELINES_COMMIT=" + "a" * 40, build)
|
||||
self.assertIn("PIPELINES_DIRTY=false", build)
|
||||
self.assertEqual(Path(directory, "secrets/ntfy-token.txt").read_text(), token + "\n")
|
||||
self.assertFalse(any(token in " ".join(c) for c in calls))
|
||||
self.assertLess(next(i for i,c in enumerate(calls) if "token" in c), next(i for i,c in enumerate(calls) if "build" in c))
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
import importlib.util
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
spec = importlib.util.spec_from_file_location("setup_harness", Path(__file__).with_name("setup-harness.py"))
|
||||
setup = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(setup)
|
||||
|
||||
|
||||
class HarnessSetupTests(unittest.TestCase):
|
||||
def test_missing_docker_installation_never_configures_host_harness(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
script = str(Path(directory, "scripts/setup-harness.py"))
|
||||
with patch.object(setup, "__file__", script), patch.object(sys, "argv", [script, "pi"]), patch.object(setup.subprocess, "run") as run:
|
||||
with self.assertRaisesRegex(SystemExit, "Run ./install first"):
|
||||
setup.main()
|
||||
run.assert_not_called()
|
||||
|
||||
def test_reconfiguration_reuses_setup_docker_helpers_and_links_host(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
(root / ".env").write_text("PIPELINES_HOST_REPOSITORY=/tmp/repo\n")
|
||||
script = str(root / "scripts/setup-harness.py")
|
||||
calls = {}
|
||||
|
||||
class Fake:
|
||||
def __init__(self, install_root):
|
||||
calls["root"] = install_root
|
||||
def reconfigure_harness(self, name, options):
|
||||
calls["name"] = name
|
||||
calls["options"] = tuple(options)
|
||||
def link_host(self, name, config=None):
|
||||
calls["linked"] = name
|
||||
def compose(self, *args, **kwargs):
|
||||
return subprocess.CompletedProcess(["docker", *args], 0,
|
||||
stdout='{"default":"pi","profiles":{"pi":{"model":"m","pipeline_model":"m"}}}', stderr="")
|
||||
|
||||
with patch.object(setup, "__file__", script), \
|
||||
patch.object(setup.setup_docker, "Setup", Fake), \
|
||||
patch.object(setup.sys, "argv", [script, "pi", "--model", "fixture/model"]):
|
||||
setup.main()
|
||||
# /var/folders and /private/var/folders are the same real directory on
|
||||
# macOS; compare canonically instead of by path spelling.
|
||||
self.assertTrue(os.path.samefile(calls["root"], root))
|
||||
self.assertEqual(calls["name"], "pi")
|
||||
self.assertEqual(calls["options"], ("--model", "fixture/model"))
|
||||
self.assertEqual(calls["linked"], "pi")
|
||||
|
||||
def test_stale_image_stops_without_rebuilding_services(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
(root / ".env").write_text("PIPELINES_HOST_REPOSITORY=/tmp/repo\n")
|
||||
script = str(root / "scripts/setup-harness.py")
|
||||
|
||||
class Fake:
|
||||
def __init__(self, install_root):
|
||||
pass
|
||||
def reconfigure_harness(self, name, options):
|
||||
raise ValueError("The Docker image does not match the current resolved releases; run ./install")
|
||||
|
||||
with patch.object(setup, "__file__", script), \
|
||||
patch.object(setup.setup_docker, "Setup", Fake), \
|
||||
patch.object(setup.sys, "argv", [script, "pi"]):
|
||||
with self.assertRaisesRegex(ValueError, "run ./install"):
|
||||
setup.main()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user