Group committed starter skills into packs (semif_agent/packs.py) that bind a
concern to its categories, bridge(s), requirements, and config schema:
- seeds/ -> packs/<name>/ with a pack.json manifest + skills/<category>/<leaf>/.
Shipped: nextcloud, simplex, outlook, browser (each owning its bridge) and
essentials (time + calculator, no bridge).
- PackRegistry discovers packs (a root with no manifest is one implicit pack);
merge_packs loads every enabled pack. Pack enable is one switch
(packs.<name>.enabled -> bridges.<bridge>.enabled -> enabled_by_default);
disabling a pack drops its categories (incl. generated bodies) and its bridge.
- CATEGORY_DESCRIPTIONS now comes from the pack manifests (built-ins stay).
Adopt a Home-Assistant-style layered config (semif_agent/config.py):
- config.json is a thin index ({"include": [...]}) over config/core.json,
config/gateway.json, config/bridges.json, config/packs/*.json, deep-merged.
- Secrets live in the gitignored secrets.json, referenced as "!secret <key>";
api_key_env is removed.
- CORE_SCHEMA + each pack's config schema own defaults and GENERATE the
committed config.example.json; new `check-config` (validate + --write-example)
and `config-init` (seed the split layout) commands.
- scripts/config_merge.py (stdlib) lets bootstrap read the merged config.
Tests: tests/test_packs.py (renamed from test_seed_skills, +enable/merge),
new tests/test_config.py. All unit tests pass (772).
115 lines
4.6 KiB
Python
115 lines
4.6 KiB
Python
"""The layered config: index includes, !secret resolution, and schema validation.
|
|
|
|
These exercise the Home-Assistant-style split directly: config.json is a thin
|
|
index over fragments, secrets live in a separate file referenced as
|
|
``"!secret <key>"``, and the core + pack schemas own defaults and validate a
|
|
live config.
|
|
"""
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
from semif_agent.config import (
|
|
ConfigError,
|
|
generate_example_config,
|
|
iter_secret_refs,
|
|
load_config_file,
|
|
load_secrets,
|
|
missing_secret_refs,
|
|
resolve_secrets,
|
|
split_config,
|
|
validate_config,
|
|
write_split_config,
|
|
)
|
|
from semif_agent.packs import load_packs
|
|
|
|
|
|
def _write(path: Path, data: dict) -> None:
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(json.dumps(data))
|
|
|
|
|
|
def test_index_includes_deep_merge_later_wins(tmp_path):
|
|
_write(tmp_path / "config" / "core.json", {"tau": 0.6, "queue": {"max_size": 10}})
|
|
_write(tmp_path / "config" / "gateway.json", {"gateway": {"simplex": {"enabled": True}}})
|
|
_write(
|
|
tmp_path / "config.json",
|
|
{"include": ["config/core.json", "config/gateway.json"]},
|
|
)
|
|
config = load_config_file(tmp_path / "config.json")
|
|
assert config["tau"] == 0.6
|
|
assert config["queue"]["max_size"] == 10
|
|
assert config["gateway"]["simplex"]["enabled"] is True
|
|
|
|
|
|
def test_include_cycle_is_an_error(tmp_path):
|
|
_write(tmp_path / "a.json", {"include": ["b.json"]})
|
|
_write(tmp_path / "b.json", {"include": ["a.json"]})
|
|
try:
|
|
load_config_file(tmp_path / "a.json")
|
|
except ConfigError as exc:
|
|
assert "cycle" in str(exc)
|
|
else:
|
|
raise AssertionError("expected a cycle error")
|
|
|
|
|
|
def test_secret_resolution_and_missing_refs(tmp_path):
|
|
_write(tmp_path / "secrets.json", {"nextcloud_app_password": "s3cret"})
|
|
secrets = load_secrets(tmp_path / "secrets.json")
|
|
config = {
|
|
"bridges": {"nextcloud": {"app_password": "!secret nextcloud_app_password"}},
|
|
"llm": {"api_key": "!secret absent_key"},
|
|
}
|
|
assert list(iter_secret_refs(config)) == ["nextcloud_app_password", "absent_key"]
|
|
resolved = resolve_secrets(config, secrets)
|
|
assert resolved["bridges"]["nextcloud"]["app_password"] == "s3cret"
|
|
# A missing secret resolves to "" at load (startup never hard-fails) ...
|
|
assert resolved["llm"]["api_key"] == ""
|
|
# ... and is reported by the validator.
|
|
assert missing_secret_refs(config, secrets) == ["absent_key"]
|
|
|
|
|
|
def test_generate_example_marks_secrets_and_never_leaks_values(tmp_path):
|
|
packs = load_packs({"skill_packs": str(Path(__file__).resolve().parent.parent / "packs")})
|
|
example = generate_example_config(packs)
|
|
# Every secret-typed key is a !secret ref, never a value.
|
|
assert example["codegen"]["api_key"] == "!secret codegen_api_key"
|
|
assert example["bridges"]["nextcloud"]["app_password"] == "!secret nextcloud_app_password"
|
|
assert example["bridges"]["nextcloud"]["token"] == "!secret nextcloud_token"
|
|
# The pack's non-secret defaults are present.
|
|
assert example["bridges"]["nextcloud"]["port"] == 5230
|
|
assert example["packs"]["essentials"]["enabled"] is True
|
|
|
|
|
|
def test_validate_config_reports_types_unknowns_and_secrets(tmp_path):
|
|
_write(tmp_path / "secrets.json", {})
|
|
config = {
|
|
"tau": "not-a-number",
|
|
"totally_unknown_key": 1,
|
|
"codegen": {"api_key": "!secret codegen_api_key"},
|
|
}
|
|
problems = validate_config(config, packs=[])
|
|
assert any("tau: expected float" in p for p in problems)
|
|
assert any("warning: totally_unknown_key: unknown config key" in p for p in problems)
|
|
assert "codegen_api_key" in missing_secret_refs(config, load_secrets(tmp_path / "secrets.json"))
|
|
|
|
|
|
def test_split_then_load_roundtrip(tmp_path):
|
|
packs = load_packs({"skill_packs": str(Path(__file__).resolve().parent.parent / "packs")})
|
|
merged = generate_example_config(packs)
|
|
written = write_split_config(tmp_path, merged, packs)
|
|
assert any(p.endswith("config/core.json") for p in written)
|
|
assert any(p.endswith("config/packs/nextcloud.json") for p in written)
|
|
# The written index reloads to the same core values.
|
|
reloaded = load_config_file(tmp_path / "config.json")
|
|
assert reloaded["tau"] == merged["tau"]
|
|
assert reloaded["bridges"]["nextcloud"]["port"] == 5230
|
|
|
|
|
|
def test_split_config_places_shared_bridge_outside_packs(tmp_path):
|
|
packs = load_packs({"skill_packs": str(Path(__file__).resolve().parent.parent / "packs")})
|
|
merged = generate_example_config(packs)
|
|
files = split_config(merged, packs)
|
|
assert "bridges" in files["config/bridges.json"]
|
|
assert "llm" in files["config/bridges.json"]["bridges"]
|