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
+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: