Resolve configured host models during native installation

Read OpenCode V2's resolved default Model.Info through its authenticated CLI API from HOME when saved profile fields are missing. Preserve valid saved models, explicit --model and unrelated settings. Record the selected stage model in the native server environment so web starters have an actual default.

Run the existing read-only model picker inside ./install when a terminal needs a choice; fail noninteractive or cancelled/incomplete setup before claiming ready or starting services. Do not parse JSONC, open login flows, create sessions or run inference. Document the one-step configuration path.

Checks: 61 mocked native installer/lifecycle tests, 8 entrypoint tests, sh -n install install.sh, git diff --check. API/schema checked against official OpenCode V2 documentation. No operator CLI/service/model calls, live install, restart, deployment or native Linux verification.
This commit is contained in:
2026-10-20 21:56:04 +01:00
parent d3c7e6e784
commit da6daa9db5
3 changed files with 218 additions and 22 deletions
+58 -11
View File
@@ -156,7 +156,7 @@ class Setup:
if not isinstance(value, str) or len(value) > 501 or "/" not in value:
return False
provider, model = value.split("/", 1)
if not provider or not model:
if not provider or not model or value in ("default/default", "provider/model"):
return False
return all(char in cls.MODEL_CHARS for char in value)
@@ -174,16 +174,45 @@ class Setup:
return {}
return data if isinstance(data, dict) else {}
def configure_harnesses(self, harness, model=""):
def opencode_default_model(self, executable):
"""Read V2's resolved Model.Info, with OpenCode's own JSONC/auth handling.
Run in HOME, not the install checkout, so a repository's model override
does not accidentally become the host installation's global default.
No login, sessions, prompts or inference are involved.
"""
try:
result = self.runner([executable, "api", "get", "/api/model/default"],
cwd=str(Path.home()), env=dict(self.environ),
capture_output=True, text=True, timeout=30)
if result.returncode or len(result.stdout or "") > 4 * 1024 * 1024:
return ""
response = json.loads(result.stdout)
data = response.get("data") if isinstance(response, dict) else None
if not isinstance(data, dict) or data.get("enabled") is not True or data.get("status") == "deprecated":
return ""
provider, model = data.get("providerID"), data.get("modelID")
if not isinstance(provider, str) or "/" in provider or not isinstance(model, str):
return ""
value = provider + "/" + model
return value if self._valid_model(value) else ""
except (OSError, ValueError, subprocess.SubprocessError):
# Never echo raw CLI output: provider discovery can include secrets.
return ""
def configure_harnesses(self, harness, model="", allow_picker=True):
"""Write host (non-Docker) profiles for the harnesses detected on PATH.
The native install launches host harnesses, so a Docker-era
``docker_directory`` must be cleared rather than kept. Models come from
the explicit ``--model`` (recorded as PIPELINES_DEFAULT_MODEL) or the
existing valid profile; they are never invented. Unrelated keys and
existing valid profile, the explicit ``--model`` (also recorded as
PIPELINES_DEFAULT_MODEL), or OpenCode's resolved host default; they are
never invented. Unrelated keys and
profiles for harnesses that are not detected are preserved, matching
harness.Save. No harness is installed, upgraded or downloaded.
"""
if model and not self._valid_model(model):
raise ValueError("Invalid --model; supply an actual PROVIDER/MODEL, not a default placeholder")
existing = self._existing_harness_config()
profiles = existing.get("profiles")
if not isinstance(profiles, dict):
@@ -198,6 +227,10 @@ class Setup:
if name == "opencode" and self._valid_model(model):
harness_model = harness_model or model
pipeline_model = pipeline_model or model
if name == "opencode" and (not harness_model or not pipeline_model):
resolved = self.opencode_default_model(harness[name])
harness_model = harness_model or resolved
pipeline_model = pipeline_model or resolved
if not harness_model or not pipeline_model:
continue
# Host mode: no docker_directory, executable is the detected host binary.
@@ -207,9 +240,21 @@ class Setup:
merged[name] = profile
configured.append(name)
if not configured:
print("Host harness profiles were not written: no valid model was found for "
+ ", ".join(order) + ". Run 'pipelines harness setup opencode --host' to configure authoring.")
return None
if allow_picker and sys.stdin.isatty():
print("No usable OpenCode default model was found. Choose the defaults below to finish ./install.")
command = [str(self.resolve_prefix() / "bin/pipelines"), "harness", "setup", "opencode",
"--host", "--executable", harness["opencode"]]
current = merged.get("opencode", {})
for field, flag in (("model", "--model"), ("pipeline_model", "--pipeline-model")):
if self._valid_model(current.get(field)):
command.extend([flag, current[field]])
result = self.runner(command, cwd=str(Path.home()), env=dict(self.environ))
if result.returncode:
raise ValueError("Model selection did not complete; native setup is not ready. Rerun ./install or supply --model PROVIDER/MODEL.")
return self.configure_harnesses(harness, model, allow_picker=False)
raise ValueError("No usable configured harness model was found; native setup is not ready. "
"Rerun ./install in a terminal to choose models, or use ./install --model PROVIDER/MODEL. "
"Existing credentials and harness settings were retained.")
# Keep a configured host default; only a stale Docker default must change.
default = existing.get("default")
if default not in configured or profiles.get(default, {}).get("docker_directory"):
@@ -615,7 +660,9 @@ class Setup:
# The native install launches host harnesses, so write host profiles for
# the detected harnesses and clear any Docker-era link. Uses only the
# explicit --model or the existing valid profile/host configuration.
self.configure_harnesses(harness, args.model or "")
harness_path = self.configure_harnesses(harness, args.model or "")
harness_config = json.loads(harness_path.read_text())
pipeline_model = harness_config["profiles"][harness_config["default"]]["pipeline_model"]
lan_ip = lan.detect_lan()
base_url = args.ntfy_url or f"http://{lan_ip}:{args.ntfy_port}"
config_path = self.write_ntfy_config(prefix, base_url, args.ntfy_port)
@@ -636,8 +683,7 @@ class Setup:
"OPENCODE_SERVER", "OPENCODE_DB"):
if self.environ.get(key):
opencode_env[key] = self.environ[key]
if args.model:
opencode_env["PIPELINES_DEFAULT_MODEL"] = args.model
opencode_env["PIPELINES_DEFAULT_MODEL"] = args.model or pipeline_model
path_value = str(prefix / "bin") + os.pathsep + self.environ.get("PATH", "")
manifest = {
"format": 1,
@@ -712,7 +758,8 @@ def arguments(argv=None):
parser.add_argument("--bind-address", default=DEFAULT_BIND_ADDRESS,
help="Unauthenticated server bind address (default 127.0.0.1; expose only the web proxy)")
parser.add_argument("--ntfy-port", type=int, default=DEFAULT_NTFY_PORT, help="ntfy listen port")
parser.add_argument("--model", help="Record an explicit default model; otherwise host OpenCode configuration is reused")
parser.add_argument("--model", help="Explicit PROVIDER/MODEL for starter defaults and missing profile fields; "
"otherwise reuse saved/default OpenCode models or choose within this installer")
role = parser.add_mutually_exclusive_group()
role.add_argument("--allow-authoring", dest="allow_authoring", action="store_true", default=None,
help="Explicitly enable pipeline publication; fresh native installs default to enabled")
+141 -6
View File
@@ -31,7 +31,8 @@ def resolution():
class Recorder:
def __init__(self, token=TOKEN, volumes=(), image="pipelines-pipelines:latest",
fail_create=False, fail_copy=False, fail_volume_ls=False, fail_build=False):
fail_create=False, fail_copy=False, fail_volume_ls=False, fail_build=False,
host_model="fixture/host-default"):
self.calls = []
self.token = token
self.volumes = volumes
@@ -40,6 +41,7 @@ class Recorder:
self.fail_copy = fail_copy
self.fail_volume_ls = fail_volume_ls
self.fail_build = fail_build
self.host_model = host_model
self._module_cache = None
self.kwargs = []
@@ -59,6 +61,12 @@ class Recorder:
stdout, code = "", 0
if command[0].endswith("node") and command[1:] == ["--version"]:
stdout = "v22.19.0\n"
if command[0].endswith("opencode") and command[1:] == ["api", "get", "/api/model/default"]:
model = None
if self.host_model:
provider, name = self.host_model.split("/", 1)
model = {"providerID": provider, "modelID": name, "enabled": True, "status": "active"}
stdout = json.dumps({"location": {"directory": "/scratch/home"}, "data": model})
if command[0] == "docker":
if command[1:3] == ["volume", "ls"]:
if self.fail_volume_ls:
@@ -316,6 +324,10 @@ class SetupNativeTests(unittest.TestCase):
self.assertEqual(config["topic"], "pipelines")
self.assertEqual(config["opencode"], "/usr/bin/opencode")
self.assertEqual(config["pi"], "")
self.assertEqual(config["opencode_env"]["PIPELINES_DEFAULT_MODEL"], "fixture/host-default")
host = json.loads(installer.harness_config_path().read_text())["profiles"]["opencode"]
self.assertEqual(host["model"], "fixture/host-default")
self.assertEqual(host["pipeline_model"], "fixture/host-default")
self.assertTrue(Path(config["registration"]).is_file())
self.assertTrue(recorder.has("start", "--prefix", str(prefix.resolve())))
self.assertEqual((prefix / setup_native.MARKER).stat().st_mode & 0o777, 0o600)
@@ -332,7 +344,8 @@ class SetupNativeTests(unittest.TestCase):
self.assertTrue(recorder.has(str(prefix.resolve() / "bin/pipelines"), "skill", "install"))
self.assertTrue(recorder.has("access", "pipeline-publisher", "pipelines", "write-only"))
self.assertTrue(recorder.has("access", "phone-reader", "pipelines", "read-only"))
self.assertFalse(any("opencode" in part for call in recorder.calls for part in call))
discovery = [call for call in recorder.calls if call[0].endswith("opencode")]
self.assertEqual(discovery, [["/usr/bin/opencode", "api", "get", "/api/model/default"]])
self.assertFalse(any("pipelines-tui" in part for call in recorder.calls for part in call))
self.assertFalse(any(TOKEN in part for call in recorder.calls for part in call))
@@ -517,13 +530,135 @@ class SetupNativeTests(unittest.TestCase):
# Detection-only: no harness install/upgrade/download was invoked.
self.assertFalse(any("opencode" in part for call in recorder.calls for part in call))
def test_configure_harnesses_skips_without_a_valid_model(self):
def test_configure_harnesses_fails_without_a_valid_model_in_nonterminal(self):
with tempfile.TemporaryDirectory() as directory:
installer = self.installer(directory, Recorder(host_model=None))
path = installer.harness_config_path()
with patch.object(setup_native.sys.stdin, "isatty", return_value=False):
with self.assertRaisesRegex(ValueError, "setup is not ready.*--model PROVIDER/MODEL"):
installer.configure_harnesses({"opencode": "/usr/bin/opencode", "pi": ""})
self.assertFalse(path.exists(), "an invalid profile was written")
def test_opencode_default_reads_model_info_without_inference_or_jsonc_parsing(self):
with tempfile.TemporaryDirectory() as directory:
calls = []
def api(command, **kwargs):
calls.append((command, kwargs))
return subprocess.CompletedProcess(command, 0, stdout=json.dumps({"data": {
"id": "internal-id-not-a-model", "providerID": "configured-provider",
"modelID": "configured-model", "enabled": True, "status": "active"}}))
installer = self.installer(directory, api)
path = installer.configure_harnesses({"opencode": "/existing/opencode", "pi": ""})
profile = json.loads(path.read_text())["profiles"]["opencode"]
self.assertEqual(profile["model"], "configured-provider/configured-model")
self.assertEqual(profile["pipeline_model"], profile["model"])
self.assertEqual(len(calls), 1)
self.assertEqual(calls[0][0], ["/existing/opencode", "api", "get", "/api/model/default"])
self.assertEqual(calls[0][1]["cwd"], str(Path.home()))
self.assertEqual(calls[0][1]["env"], installer.environ)
self.assertEqual(calls[0][1]["timeout"], 30)
def test_opencode_default_rejects_missing_disabled_deprecated_or_placeholder_models(self):
valid = {"providerID": "fixture", "modelID": "host-default", "enabled": True, "status": "active"}
responses = [None, {}, {"data": None}, {"data": {**valid, "enabled": False}},
{"data": {**valid, "status": "deprecated"}},
{"data": {**valid, "providerID": "default", "modelID": "default"}},
{"data": {"id": "fixture/looks-valid", "enabled": True}}]
for response in responses:
with self.subTest(response=response), tempfile.TemporaryDirectory() as directory:
def api(command, **kwargs):
return subprocess.CompletedProcess(command, 0, stdout=json.dumps(response))
installer = self.installer(directory, api)
self.assertEqual(installer.opencode_default_model("/existing/opencode"), "")
def test_configure_harnesses_discovery_fills_only_missing_model_fields(self):
with tempfile.TemporaryDirectory() as directory:
installer = self.installer(directory, Recorder())
path = installer.harness_config_path()
result = installer.configure_harnesses({"opencode": "/usr/bin/opencode", "pi": ""})
self.assertIsNone(result)
self.assertFalse(path.exists(), "an invalid profile was written")
path.parent.mkdir(parents=True)
path.write_text(json.dumps({"default": "opencode", "future": "preserve", "profiles": {
"opencode": {"model": "fixture/keep-interactive", "pipeline_model": "", "future": "keep"}}}))
installer.configure_harnesses({"opencode": "/existing/opencode", "pi": ""})
saved = json.loads(path.read_text())
self.assertEqual(saved["future"], "preserve")
self.assertEqual(saved["profiles"]["opencode"]["model"], "fixture/keep-interactive")
self.assertEqual(saved["profiles"]["opencode"]["pipeline_model"], "fixture/host-default")
self.assertEqual(saved["profiles"]["opencode"]["future"], "keep")
def test_missing_default_runs_existing_picker_inside_install_only_in_terminal(self):
with tempfile.TemporaryDirectory() as directory:
recorder = Recorder(host_model=None)
installer = self.installer(directory, recorder)
path = installer.harness_config_path()
def runner(command, **kwargs):
result = recorder(command, **kwargs)
if command[1:4] == ["harness", "setup", "opencode"]:
path.parent.mkdir(parents=True)
path.write_text(json.dumps({"default": "opencode", "profiles": {"opencode": {
"executable": "/existing/opencode", "model": "fixture/chosen-interactive",
"pipeline_model": "fixture/chosen-stages", "future": "preserve"}}}))
return result
installer.runner = runner
with patch.object(setup_native.sys.stdin, "isatty", return_value=True):
installer.configure_harnesses({"opencode": "/existing/opencode", "pi": ""})
picker = [call for call in recorder.calls if call[1:4] == ["harness", "setup", "opencode"]]
self.assertEqual(picker, [[str(installer.resolve_prefix() / "bin/pipelines"), "harness", "setup", "opencode",
"--host", "--executable", "/existing/opencode"]])
profile = json.loads(path.read_text())["profiles"]["opencode"]
self.assertEqual(profile["pipeline_model"], "fixture/chosen-stages")
self.assertEqual(profile["future"], "preserve")
def test_unresolved_model_aborts_full_run_before_start_or_ready_claim(self):
with tempfile.TemporaryDirectory() as directory:
recorder = Recorder(host_model=None)
installer = self.installer(directory, recorder)
with patch.object(setup_native.shutil, "which", side_effect=self.which_all()), \
patch.object(setup_native.sys.stdin, "isatty", return_value=False), \
redirect_stdout(io.StringIO()) as output:
with self.assertRaisesRegex(ValueError, "setup is not ready"):
installer.run(setup_native.arguments([]))
self.assertFalse((installer.resolve_prefix() / setup_native.MARKER).exists())
self.assertFalse(any("start" in call for call in recorder.calls))
self.assertNotIn("Native Pipelines installed", output.getvalue())
def test_picker_cancellation_or_unsaved_models_never_claim_configuration_complete(self):
for returncode in (0, 1):
with self.subTest(returncode=returncode), tempfile.TemporaryDirectory() as directory:
calls = []
def runner(command, **kwargs):
calls.append(command)
if command[1:4] == ["harness", "setup", "opencode"]:
return subprocess.CompletedProcess(command, returncode)
return subprocess.CompletedProcess(command, 0, stdout='{"data":null}')
installer = self.installer(directory, runner)
with patch.object(setup_native.sys.stdin, "isatty", return_value=True), redirect_stdout(io.StringIO()):
with self.assertRaisesRegex(ValueError, "setup is not ready"):
installer.configure_harnesses({"opencode": "/existing/opencode", "pi": ""})
self.assertFalse(installer.harness_config_path().exists())
self.assertEqual(sum(call[1:4] == ["harness", "setup", "opencode"] for call in calls), 1)
def test_full_run_reuses_saved_stage_default_without_opencode_queries(self):
with tempfile.TemporaryDirectory() as directory:
recorder = Recorder(host_model=None)
installer = self.installer(directory, recorder)
path = installer.harness_config_path()
path.parent.mkdir(parents=True)
path.write_text(json.dumps({"default": "opencode", "profiles": {"opencode": {
"executable": "/old/opencode", "model": "fixture/interactive", "pipeline_model": "fixture/stages"}}}))
with patch.object(setup_native.shutil, "which", side_effect=self.which_all()), \
patch.object(setup_native.lan, "detect_lan", return_value="192.168.1.20"), \
patch.object(setup_native.configure, "host_timezone", return_value="UTC"), \
redirect_stdout(io.StringIO()):
installer.run(setup_native.arguments([]))
manifest = manage.load(installer.resolve_prefix())
self.assertEqual(manifest["opencode_env"]["PIPELINES_DEFAULT_MODEL"], "fixture/stages")
self.assertFalse(any(call[0].endswith("opencode") for call in recorder.calls))
def test_configure_harnesses_migrates_stale_docker_link_and_default(self):
with tempfile.TemporaryDirectory() as directory: