bootstrap: first-run interview + answer-driven config and installs
Bootstrap now asks the questions that make a fresh machine work and installs only what the answers require. `cli setup` (new `semif_agent/setup.py`) runs the interview — decision engine (local SemIf 4B or remote Winnow), new-skill authoring mode (+ llm/codegen endpoints/keys when enabled), capability packs, gateways, timezone — and writes the split config + secrets.json from the answers, plus a machine-readable install plan. Both ends of the spectrum work end to end: the minimum is the local SemIf 4B engine + the essentials pack (canned replies, time/timers, calculator) with no gateway, authoring, or heavy downloads; the maximum enables every pack, a remote engine, remote llm/codegen, and all gateways. `--yes` takes the minimum unattended; `--answers FILE` replays a saved answer set; a rerun skips the interview and `--reconfigure` re-runs it. The plan drives the conditional stages: engine clone/GGUF/tokenizer and requirements/engine.txt only for a local engine; requirements/transports.txt only when a SimpleX/LXMF transport is selected; the simplex-chat binary, Playwright, and the voice stack only when their pack/gateway is on; and only the systemd units the answers need (a reconfigure retires dropped ones). Also fixed: - `config-init`/`setup` could never run on a fresh machine: main() loaded config before parsing the subcommand. Config-independent subcommands now run before the config is required. - codegen was mandatory at startup even with authoring disabled (SystemExit on an empty model); build_codegen_client now returns None, and the scheduler guards every create door. build_llm_client likewise returns None for a non-semif provider with no model. - the gateway unit ran `--platform all`, which would try the foreground-only voice platform in a headless service; it now runs the explicit daemon list. requirements/ is split by concern (core/engine/transports + voice/browser); staging.txt is the union. Docs updated (README, AGENTS). New tests/test_setup.py covers both paths, interview branching, secrets merge, and the plan round-trip.
This commit is contained in:
1 parent
d67ea26f3d
commit
2296801b22
12 files changed
+1789
-386
No files matched your search
@@ -0,0 +1,311 @@
|
||||
"""The first-run setup interview: answers -> split config, secrets, install plan.
|
||||
|
||||
These exercise the two ends of the spectrum the bootstrap must support:
|
||||
|
||||
* **minimum** — local SemIf 4B + the ``essentials`` pack, no gateway, no
|
||||
authoring, no ``llm``/``codegen`` endpoint, nothing heavy to install;
|
||||
* **maximum** — every pack, all gateways, a remote Winnow engine, remote
|
||||
``llm``/``codegen`` endpoints, and every conditional install enabled.
|
||||
|
||||
The interview is driven with a scripted ``input_fn`` (no TTY); config
|
||||
generation is validated with the real pack schemas.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
from semif_agent.config import load_config_file, validate_config
|
||||
from semif_agent.packs import load_packs
|
||||
from semif_agent.setup import (
|
||||
Answers,
|
||||
build_config,
|
||||
build_plan,
|
||||
build_secrets,
|
||||
plan_from_config,
|
||||
run_interview,
|
||||
run_setup,
|
||||
)
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parent.parent
|
||||
|
||||
|
||||
def _packs():
|
||||
return load_packs({"skill_packs": str(REPO_ROOT / "packs")})
|
||||
|
||||
|
||||
def _scripted(answers):
|
||||
"""An input_fn that yields `answers` in order and raises past the end."""
|
||||
it = iter(answers)
|
||||
|
||||
def _input(prompt: str) -> str:
|
||||
try:
|
||||
return next(it)
|
||||
except StopIteration:
|
||||
raise AssertionError(f"unexpected prompt: {prompt!r}") from None
|
||||
|
||||
return _input
|
||||
|
||||
|
||||
def _silent(_: str) -> None:
|
||||
pass
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Minimum path
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
def test_minimal_config_is_essentials_only_with_no_endpoints():
|
||||
config = build_config(Answers.minimal(), _packs())
|
||||
assert config["engine"]["provider"] == "semif"
|
||||
assert config["new_skill_creation"] == "deny"
|
||||
# llm reuses the loaded engine; codegen is deliberately unconfigured.
|
||||
assert config["llm"]["provider"] == "semif"
|
||||
assert config["codegen"]["model"] == ""
|
||||
# essentials on, every other pack off.
|
||||
assert config["packs"]["essentials"]["enabled"] is True
|
||||
for bridge in ("simplex", "nextcloud", "outlook", "browser"):
|
||||
assert config["bridges"][bridge]["enabled"] is False
|
||||
# no gateway, and the shared llm bridge (only useful to a bridge pack) off.
|
||||
assert config["gateway"]["simplex"]["enabled"] is False
|
||||
assert config["gateway"]["lxmf"]["enabled"] is False
|
||||
assert config["gateway"]["voice"]["enabled"] is False
|
||||
assert config["bridges"]["llm"]["enabled"] is False
|
||||
# The generated config passes schema validation.
|
||||
assert validate_config(config, _packs()) == []
|
||||
|
||||
|
||||
def test_minimum_plan_installs_nothing_heavy():
|
||||
plan = build_plan(Answers.minimal(), _packs())
|
||||
assert plan["install_engine"] is True
|
||||
assert plan["install_bridge_service"] is False
|
||||
assert plan["install_gateway_service"] is False
|
||||
assert plan["install_simplex_binary"] is False
|
||||
assert plan["install_simplex_forward"] is False
|
||||
assert plan["install_browser"] is False
|
||||
assert plan["install_voice"] is False
|
||||
assert plan["packs"] == ["essentials"]
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Maximum path
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
def test_maximum_config_enables_everything():
|
||||
answers = Answers(
|
||||
engine_provider="winnow",
|
||||
winnow_base_url="http://10.0.0.5:8091",
|
||||
winnow_model="winnow-1",
|
||||
winnow_api_key="wk",
|
||||
new_skill_creation="allow",
|
||||
llm_provider="ollama",
|
||||
llm_model="small",
|
||||
codegen_provider="opencode",
|
||||
codegen_model="coder",
|
||||
codegen_api_key="sk",
|
||||
packs=["simplex", "browser", "nextcloud", "outlook"],
|
||||
gateways=["simplex", "lxmf", "voice"],
|
||||
nextcloud_app_password="nc",
|
||||
nextcloud_url="https://cloud.example.com",
|
||||
outlook_client_id="client",
|
||||
)
|
||||
config = build_config(answers, _packs())
|
||||
assert config["engine"]["provider"] == "winnow"
|
||||
assert config["engine"]["winnow"]["model"] == "winnow-1"
|
||||
assert config["engine"]["winnow"]["api_key"] == "!secret engine_winnow_api_key"
|
||||
assert config["new_skill_creation"] == "allow"
|
||||
assert config["llm"]["model"] == "small"
|
||||
assert config["codegen"]["model"] == "coder"
|
||||
assert config["codegen"]["api_key"] == "!secret codegen_api_key"
|
||||
assert config["bridges"]["simplex"]["enabled"] is True
|
||||
assert config["bridges"]["browser"]["enabled"] is True
|
||||
assert config["bridges"]["llm"]["enabled"] is True
|
||||
assert config["gateway"]["voice"]["enabled"] is True
|
||||
assert config["gateway"]["humanize"]["enabled"] is True
|
||||
assert config["bridges"]["nextcloud"]["app_password"] == (
|
||||
"!secret nextcloud_app_password"
|
||||
)
|
||||
assert validate_config(config, _packs()) == []
|
||||
|
||||
plan = build_plan(answers, _packs())
|
||||
assert plan["install_engine"] is False
|
||||
assert plan["install_simplex_binary"] is True
|
||||
assert plan["install_simplex_forward"] is True
|
||||
assert plan["install_browser"] is True
|
||||
assert plan["install_voice"] is True
|
||||
assert plan["install_bridge_service"] is True
|
||||
assert plan["install_gateway_service"] is True
|
||||
|
||||
secrets = build_secrets(answers)
|
||||
assert secrets["engine_winnow_api_key"] == "wk"
|
||||
assert secrets["codegen_api_key"] == "sk"
|
||||
assert secrets["nextcloud_app_password"] == "nc"
|
||||
# llm has no key configured, so no llm_api_key entry.
|
||||
assert "llm_api_key" not in secrets
|
||||
|
||||
|
||||
def test_simplex_gateway_without_pack_still_needs_the_binary():
|
||||
answers = Answers(gateways=["simplex"])
|
||||
plan = build_plan(answers, _packs())
|
||||
assert plan["install_simplex_binary"] is True
|
||||
assert plan["install_simplex_forward"] is False
|
||||
assert plan["install_gateway_service"] is True
|
||||
assert plan["install_bridge_service"] is False
|
||||
|
||||
|
||||
def test_voice_only_gateway_is_foreground_not_a_service():
|
||||
answers = Answers(gateways=["voice"])
|
||||
plan = build_plan(answers, _packs())
|
||||
# Voice is foreground-only: no systemd gateway unit, no daemon platforms.
|
||||
assert plan["install_voice"] is True
|
||||
assert plan["install_gateway_service"] is False
|
||||
assert plan["gateway_platforms"] == []
|
||||
assert plan["install_simplex_binary"] is False
|
||||
|
||||
|
||||
def test_gateway_platforms_exclude_voice():
|
||||
answers = Answers(gateways=["simplex", "voice"])
|
||||
plan = build_plan(answers, _packs())
|
||||
assert plan["gateway_platforms"] == ["simplex"]
|
||||
assert plan["install_gateway_service"] is True
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Interview branching
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
def test_interview_collects_authoring_endpoints_and_packs():
|
||||
# engine=1(local); authoring=2(ask); llm provider=1(semif);
|
||||
# codegen provider=1(ollama); codegen base URL='' (default);
|
||||
# codegen model='my-coder'; codegen key=''; packs='a,c' (simplex+nextcloud);
|
||||
# nextcloud url/user/pass; gateways='b' (lxmf); timezone.
|
||||
script = _scripted(
|
||||
[
|
||||
"1", # engine: local semif
|
||||
"2", # authoring: ask
|
||||
"1", # llm provider: semif (in-process)
|
||||
"1", # codegen provider: ollama
|
||||
"", # codegen base URL: default
|
||||
"my-coder", # codegen model
|
||||
"", # codegen api key
|
||||
"a,c", # packs: simplex + nextcloud
|
||||
"https://nc.example", # nextcloud url
|
||||
"bob", # nextcloud username
|
||||
"pw", # nextcloud app password
|
||||
"b", # gateways: lxmf
|
||||
"Europe/Paris", # timezone
|
||||
]
|
||||
)
|
||||
answers = run_interview(input_fn=script, output=_silent)
|
||||
assert answers.engine_provider == "semif"
|
||||
assert answers.new_skill_creation == "ask"
|
||||
assert answers.llm_provider == "semif"
|
||||
assert answers.codegen_provider == "ollama"
|
||||
assert answers.codegen_model == "my-coder"
|
||||
assert answers.packs == ["essentials", "simplex", "nextcloud"]
|
||||
assert answers.nextcloud_url == "https://nc.example"
|
||||
assert answers.gateways == ["lxmf"]
|
||||
assert answers.timezone == "Europe/Paris"
|
||||
|
||||
|
||||
def test_interview_non_interactive_returns_minimum():
|
||||
answers = run_interview(interactive=False, output=_silent)
|
||||
assert answers.new_skill_creation == "deny"
|
||||
assert answers.packs == ["essentials"]
|
||||
assert answers.gateways == []
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# run_setup: files, secrets, plan, idempotency
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
def test_run_setup_writes_split_config_and_secrets(tmp_path):
|
||||
plan_out = tmp_path / "plan.json"
|
||||
code = run_setup(
|
||||
root=tmp_path,
|
||||
path=tmp_path / "config.json",
|
||||
packs=_packs(),
|
||||
yes=True,
|
||||
plan_out=str(plan_out),
|
||||
output=_silent,
|
||||
)
|
||||
assert code == 0
|
||||
assert (tmp_path / "config" / "core.json").is_file()
|
||||
assert (tmp_path / "config" / "packs" / "essentials.json").is_file()
|
||||
assert (tmp_path / "secrets.json").is_file()
|
||||
plan = json.loads(plan_out.read_text())
|
||||
assert plan["packs"] == ["essentials"]
|
||||
# The written index reloads to the answered values.
|
||||
reloaded = load_config_file(tmp_path / "config.json")
|
||||
assert reloaded["engine"]["provider"] == "semif"
|
||||
assert reloaded["codegen"]["model"] == ""
|
||||
assert reloaded["packs"]["essentials"]["enabled"] is True
|
||||
|
||||
|
||||
def test_run_setup_answers_file_and_secrets_merge(tmp_path):
|
||||
answers = {
|
||||
"new_skill_creation": "allow",
|
||||
"llm_provider": "ollama",
|
||||
"llm_model": "small",
|
||||
"codegen_provider": "ollama",
|
||||
"codegen_model": "coder",
|
||||
"codegen_api_key": "sk-codegen",
|
||||
}
|
||||
answers_path = tmp_path / "answers.json"
|
||||
answers_path.write_text(json.dumps(answers))
|
||||
# A pre-existing secret must survive a merge.
|
||||
(tmp_path / "secrets.json").write_text(json.dumps({"codegen_api_key": "old"}))
|
||||
|
||||
code = run_setup(
|
||||
root=tmp_path,
|
||||
path=tmp_path / "config.json",
|
||||
packs=_packs(),
|
||||
answers_file=str(answers_path),
|
||||
output=_silent,
|
||||
)
|
||||
assert code == 0
|
||||
secrets = json.loads((tmp_path / "secrets.json").read_text())
|
||||
assert secrets["codegen_api_key"] == "sk-codegen"
|
||||
reloaded = load_config_file(tmp_path / "config.json")
|
||||
assert reloaded["new_skill_creation"] == "allow"
|
||||
assert reloaded["codegen"]["model"] == "coder"
|
||||
|
||||
|
||||
def test_run_setup_refuses_to_overwrite_without_force(tmp_path):
|
||||
run_setup(
|
||||
root=tmp_path, path=tmp_path / "config.json", packs=_packs(),
|
||||
yes=True, output=_silent,
|
||||
)
|
||||
code = run_setup(
|
||||
root=tmp_path, path=tmp_path / "config.json", packs=_packs(),
|
||||
yes=True, output=_silent,
|
||||
)
|
||||
assert code == 1
|
||||
# --force re-seeds.
|
||||
code = run_setup(
|
||||
root=tmp_path, path=tmp_path / "config.json", packs=_packs(),
|
||||
yes=True, force=True, output=_silent,
|
||||
)
|
||||
assert code == 0
|
||||
|
||||
|
||||
def test_plan_only_matches_build_plan(tmp_path):
|
||||
answers = Answers(packs=["simplex"], gateways=["simplex"])
|
||||
run_setup(
|
||||
root=tmp_path, path=tmp_path / "config.json", packs=_packs(),
|
||||
answers_file=_answers_file(tmp_path, answers), output=_silent,
|
||||
)
|
||||
reloaded = load_config_file(tmp_path / "config.json")
|
||||
derived = plan_from_config(reloaded, _packs())
|
||||
assert derived == build_plan(answers, _packs())
|
||||
|
||||
|
||||
def _answers_file(tmp_path: Path, answers: Answers) -> str:
|
||||
path = tmp_path / "answers-in.json"
|
||||
path.write_text(json.dumps(answers.to_dict()))
|
||||
return str(path)
|
||||
Reference in new issue
Block a user