mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 15:10:00 +08:00
## Description
`_parse_json_loose` returned `{}` for any parse failure, and
`inject_opencode_provider_config` / `apply_provider_scope` then wrote
the whole file back with only the Headroom provider, dropping every user
key. A leading UTF-8 BOM was one trigger, a syntax error another. This
PR ignores a leading BOM when parsing, and raises a `ClickException` for
a non-empty config that still cannot be parsed instead of overwriting
it. A file holding only a BOM (or a BOM and whitespace) counts as empty.
Fixes #4415
## Type of Change
- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)
## Changes Made
- `headroom/providers/opencode/config.py`: new `_try_parse_json_loose`
(strips a leading U+FEFF, returns `None` on failure);
`_parse_json_loose` keeps its `{}` contract on top of it; new
`load_user_opencode_config` raises `click.ClickException` for non-empty
unparseable text; `inject_opencode_provider_config` uses it for both
parse sites.
- `headroom/providers/opencode/install.py`: `apply_provider_scope` uses
`load_user_opencode_config`.
- Tests: BOM config keeps user keys, BOM-only and BOM-plus-whitespace
configs are treated as empty, unparseable config is left byte-identical
(config and install paths).
- Second commit (`ffef07a7`) addresses a review comment: the emptiness
check in `load_user_opencode_config` now runs after the BOM is removed.
## Testing
- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [ ] Type checking passes (`mypy headroom`)
- [x] New tests added for new functionality
- [x] Manual testing performed
### Test Output
With only the source change reverted (new tests kept),
`tests/test_providers_opencode_config.py
tests/test_providers_opencode_install.py`:
```text
FAILED tests/test_providers_opencode_config.py::test_inject_provider_config_keeps_user_keys_with_bom
FAILED tests/test_providers_opencode_config.py::test_inject_provider_config_leaves_unparseable_config_untouched
FAILED tests/test_providers_opencode_install.py::test_apply_provider_scope_leaves_unparseable_config_untouched
======================== 3 failed, 102 passed in 1.81s ========================
```
With only the second commit's source change reverted
(`tests/test_providers_opencode_config.py`):
```text
FAILED tests/test_providers_opencode_config.py::test_inject_provider_config_treats_bom_only_config_as_empty[]
FAILED tests/test_providers_opencode_config.py::test_inject_provider_config_treats_bom_only_config_as_empty[
]
========================= 2 failed, 68 passed in 1.15s =========================
```
With the fix, plus the neighbouring files
(`tests/test_providers_opencode_config.py
tests/test_providers_opencode_install.py
tests/test_providers_opencode_plugin_path.py
tests/test_mcp_registry_opencode.py`):
```text
============================= 155 passed in 1.94s ==============================
```
```text
$ ruff check headroom/providers/opencode tests/test_providers_opencode_config.py tests/test_providers_opencode_install.py
All checks passed!
$ ruff format --check headroom/providers/opencode tests/test_providers_opencode_config.py tests/test_providers_opencode_install.py
6 files already formatted
```
`mypy headroom/providers/opencode` reports one error, in
`headroom/providers/model_metadata.py:79`, which this PR does not touch;
nothing in the changed files.
## Real Behavior Proof
- Environment: WSL2 Ubuntu on Windows 11, Python 3.12.15, branch based
on upstream/main `e995eb9f`; `headroom/_core.abi3.so` taken from the
official headroom-ai 0.41.0 wheel (untracked, not committed). Throwaway
HOME under `/var/tmp`, never a real user config.
- Exact command / steps: write `opencode.json` into the throwaway HOME,
then run the real CLI `headroom wrap opencode --prepare-only --no-mcp
--no-serena --port 18900` (entry `from headroom.cli import main`), then
read the file. Case A: BOM + `{"model": "gpt-4o", "keybinds": {"leader":
"ctrl+x"}}`. Case B: `{"model": "gpt-4o" "oops": 1}`. Case C: the file
holds only the three BOM bytes `ef bb bf`. Each run on the code without
the fix, then with it (case C: without the second commit's change, then
with it).
- Observed result: Case A without the fix: exit 0, file holds only
`provider.headroom`; `model` and `keybinds` are gone. Case A with the
fix: exit 0, `model` and `keybinds` kept, `provider.headroom` added (the
file is written without the BOM). Case B without the fix: exit 0, file
replaced by the Headroom-only block. Case C without the second commit:
exit 1, `Error: could not parse OpenCode config at <path>; leaving it
unchanged.`, file still the three BOM bytes. Case C with it: exit 0,
`provider.headroom` written (same as a zero-byte file). Case B with the
fix: exit 1, `Error: could not parse OpenCode config at <path>; leaving
it unchanged. Fix the JSON/JSONC syntax and retry.`, file still
`{"model": "gpt-4o" "oops": 1}`.
- Not tested: the MCP registrar (`mcp_registry/opencode.py`), which has
its own parser and still reports a BOM config as not valid JSON (it
refuses to write and leaves the file unchanged; checked by calling
`register_server` directly, so the default `wrap opencode` run without
`--no-mcp` was not run); a real `opencode` binary reading the result;
Windows paths or a real Windows editor producing the BOM; JSONC comments
combined with a BOM (comment stripping runs after the BOM strip, only
unit-level reasoning); the `headroom install` flow end to end (only
`apply_provider_scope` via a unit test); the MCP registry parser in
`mcp_registry/opencode.py` (unchanged).
Output, case A (stderr line about the retired MCP cleanup is from the
unchanged registry code):
```text
without the fix:
"provider": { "headroom": { ... } } # only key in the file
with the fix:
"model": "gpt-4o", "keybinds": { "leader": "ctrl+x" }, "provider": { "headroom": { ... } }
```
Output, case B with the fix:
```text
exit: 1
Error: could not parse OpenCode config at .../.config/opencode/opencode.json; leaving it unchanged. Fix the JSON/JSONC syntax and retry.
--- after wrap
{"model": "gpt-4o" "oops": 1}
```
## Runtime Rollout Safety
- Rollout-managed feature(s): none
- Minimum rollout channel: n/a
- Stable/default behavior changed: yes, narrowly: `wrap opencode` and
the OpenCode provider-scope install now stop with an error on an
unparseable non-empty config instead of overwriting it; a BOM config is
now merged instead of replaced
- Kill switch / disable path: none; fix the config file syntax, or
remove the file
- Unsafe override required: no
- Qualification impact: none expected
- Rollback path: revert this commit
## Review Readiness
- [x] I have performed a self-review
- [x] This PR is ready for human review
## Checklist
- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] I did **not** edit `CHANGELOG.md` — it is generated by
release-please from my Conventional Commit PR title (a CI guard enforces
this)
## Screenshots (if applicable)
N/A
## Additional Notes
N/A items: documentation (no user-facing doc describes this failure
mode), mypy (one existing error elsewhere, see above). Behavior change
to review: refusing to write is stricter than before; a file that was
silently replaced earlier now needs a manual syntax fix.
Produced with AI assistance (Claude Code) operated by me (HetCreep); the
reproduction was run on my machine on a clone of the repository.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
785 lines
31 KiB
Python
785 lines
31 KiB
Python
"""Tests for OpenCode install-time helpers."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import click
|
|
import pytest
|
|
|
|
from headroom.install.models import ConfigScope, DeploymentManifest
|
|
from headroom.providers.opencode.install import (
|
|
apply_provider_scope,
|
|
build_install_env,
|
|
revert_provider_scope,
|
|
)
|
|
|
|
|
|
def _manifest(port: int = 8787) -> DeploymentManifest:
|
|
return DeploymentManifest(
|
|
profile="test",
|
|
preset="persistent-task",
|
|
runtime_kind="python",
|
|
supervisor_kind="none",
|
|
scope=ConfigScope.PROVIDER.value,
|
|
provider_mode="auto",
|
|
targets=[],
|
|
port=port,
|
|
host="127.0.0.1",
|
|
backend="anthropic",
|
|
proxy_args=[],
|
|
base_env={},
|
|
tool_envs={},
|
|
)
|
|
|
|
|
|
def test_build_install_env() -> None:
|
|
"""build_install_env leaves OpenCode provider env vars untouched."""
|
|
env = build_install_env(port=8787, backend="anthropic")
|
|
assert env == {}
|
|
|
|
|
|
def test_apply_provider_scope_creates_config(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
"""apply_provider_scope creates the opencode config with headroom provider."""
|
|
home = str(tmp_path)
|
|
monkeypatch.setenv("HOME", home)
|
|
monkeypatch.setenv("USERPROFILE", home)
|
|
monkeypatch.delenv("OPENCODE_HOME", raising=False)
|
|
monkeypatch.delenv("OPENCODE_CONFIG", raising=False)
|
|
|
|
manifest = _manifest(port=8787)
|
|
mutation = apply_provider_scope(manifest)
|
|
assert mutation is not None
|
|
assert mutation.target == "opencode"
|
|
assert mutation.kind == "json-block"
|
|
|
|
config_file = tmp_path / ".config" / "opencode" / "opencode.json"
|
|
assert config_file.exists()
|
|
import json
|
|
|
|
config = json.loads(config_file.read_text())
|
|
assert config["provider"]["headroom"]["options"]["baseURL"] == "http://127.0.0.1:8787/v1"
|
|
assert "mcp" not in config
|
|
|
|
|
|
def test_apply_provider_scope_skips_when_scope_is_not_provider(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
"""apply_provider_scope returns None when scope is not PROVIDER."""
|
|
manifest = _manifest()
|
|
manifest.scope = ConfigScope.USER.value
|
|
result = apply_provider_scope(manifest)
|
|
assert result is None
|
|
|
|
|
|
def test_revert_provider_scope_restores_file(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
"""revert_provider_scope strips the Headroom block from the config."""
|
|
home = str(tmp_path)
|
|
monkeypatch.setenv("HOME", home)
|
|
monkeypatch.setenv("USERPROFILE", home)
|
|
monkeypatch.delenv("OPENCODE_HOME", raising=False)
|
|
monkeypatch.delenv("OPENCODE_CONFIG", raising=False)
|
|
|
|
config_file = tmp_path / ".config" / "opencode" / "opencode.json"
|
|
config_file.parent.mkdir(parents=True, exist_ok=True)
|
|
config_file.write_text('{"model": "openai/gpt-4o"}')
|
|
|
|
from headroom.install.models import ManagedMutation
|
|
|
|
mutation = ManagedMutation(
|
|
target="opencode",
|
|
kind="json-block",
|
|
path=str(config_file),
|
|
)
|
|
manifest = _manifest()
|
|
revert_provider_scope(mutation, manifest)
|
|
assert config_file.exists()
|
|
assert config_file.read_text().strip() == '{"model": "openai/gpt-4o"}'
|
|
|
|
|
|
@pytest.mark.parametrize("suffix", [".json", ".jsonc"])
|
|
def test_temporary_deactivation_preserves_edits_and_backup(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, suffix: str
|
|
) -> None:
|
|
"""Reactivation removes only Headroom's provider and keeps the unwrap snapshot."""
|
|
import json
|
|
|
|
from headroom.cli import install as cli_install
|
|
from headroom.install.providers import revert_mutations
|
|
|
|
config_file = tmp_path / f"opencode{suffix}"
|
|
monkeypatch.setenv("OPENCODE_CONFIG", str(config_file))
|
|
monkeypatch.delenv("OPENCODE_HOME", raising=False)
|
|
monkeypatch.setattr(cli_install, "_save_apply_manifest", lambda manifest: None)
|
|
monkeypatch.setattr(cli_install, "save_manifest", lambda manifest: None)
|
|
original_json = (
|
|
b'{"theme":"original","provider":{"anthropic":{"name":"Claude"}},"mcp":{"local":{}}}\n'
|
|
)
|
|
original = (
|
|
b"// user comment in OpenCode JSONC\n" + original_json
|
|
if suffix == ".jsonc"
|
|
else original_json
|
|
)
|
|
config_file.write_bytes(original)
|
|
|
|
manifest = _manifest()
|
|
manifest.targets = ["opencode"]
|
|
cli_install._activate_deployment_mutations(manifest)
|
|
backup_file = config_file.with_name(config_file.name + ".headroom-backup")
|
|
assert backup_file.read_bytes() == original
|
|
|
|
for theme in ("edited after install", "edited again"):
|
|
data = json.loads(config_file.read_text())
|
|
data["theme"] = theme
|
|
data["provider"]["other-user-provider"] = {"name": "Other"}
|
|
data["mcp"]["remote"] = {"url": "https://example.test"}
|
|
config_file.write_text(json.dumps(data))
|
|
|
|
cli_install._deactivate_deployment_mutations(manifest)
|
|
assert manifest.mutations == []
|
|
assert backup_file.read_bytes() == original
|
|
inactive = json.loads(config_file.read_text())
|
|
assert inactive["theme"] == theme
|
|
assert inactive["provider"] == {
|
|
"anthropic": {"name": "Claude"},
|
|
"other-user-provider": {"name": "Other"},
|
|
}
|
|
assert inactive["mcp"]["remote"] == {"url": "https://example.test"}
|
|
|
|
cli_install._activate_deployment_mutations(manifest)
|
|
active = json.loads(config_file.read_text())
|
|
assert active["theme"] == theme
|
|
assert active["provider"]["headroom"]["options"]["baseURL"] == ("http://127.0.0.1:8787/v1")
|
|
assert active["provider"]["other-user-provider"] == {"name": "Other"}
|
|
assert active["mcp"]["remote"] == {"url": "https://example.test"}
|
|
|
|
revert_mutations(manifest, restore_backup=True)
|
|
assert config_file.read_bytes() == original
|
|
assert not backup_file.exists()
|
|
|
|
|
|
def test_revert_provider_scope_noop_when_file_missing(
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""revert_provider_scope is a safe no-op when the config file is gone."""
|
|
from headroom.install.models import ManagedMutation
|
|
|
|
mutation = ManagedMutation(
|
|
target="opencode",
|
|
kind="json-block",
|
|
path=str(tmp_path / "nonexistent.json"),
|
|
)
|
|
manifest = _manifest()
|
|
revert_provider_scope(mutation, manifest)
|
|
# Should not raise
|
|
|
|
|
|
@pytest.mark.windows_newline
|
|
def test_provider_config_writes_pin_lf(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""Provider edits and stale managed-block cleanup pin LF and preserve user config."""
|
|
config_file = tmp_path / "opencode.json"
|
|
monkeypatch.setenv("OPENCODE_CONFIG", str(config_file))
|
|
monkeypatch.delenv("OPENCODE_HOME", raising=False)
|
|
config_file.write_bytes(b'{"theme":"before"}\n')
|
|
|
|
writes: list[tuple[Path, str | None]] = []
|
|
original_write_text = Path.write_text
|
|
|
|
def write_text_spy(self, data, encoding=None, errors=None, newline=None):
|
|
writes.append((self, newline))
|
|
return original_write_text(self, data, encoding=encoding, errors=errors, newline=newline)
|
|
|
|
monkeypatch.setattr(Path, "write_text", write_text_spy)
|
|
|
|
manifest = _manifest()
|
|
mutation = apply_provider_scope(manifest)
|
|
assert mutation is not None
|
|
revert_provider_scope(mutation, manifest, restore_backup=False)
|
|
|
|
marker_config = (
|
|
'{\n "theme": "kept",\n'
|
|
"// --- Headroom proxy provider ---\n"
|
|
" stale generated provider block\n"
|
|
"// --- end Headroom proxy provider ---\n"
|
|
' "mcp": {"remote": {}}\n}\n'
|
|
)
|
|
config_file.write_bytes(marker_config.encode())
|
|
revert_provider_scope(mutation, manifest, restore_backup=False)
|
|
|
|
assert len(writes) == 3
|
|
assert all(path == config_file and newline == "\n" for path, newline in writes)
|
|
import json
|
|
|
|
cleaned = json.loads(config_file.read_text())
|
|
assert cleaned == {"theme": "kept", "mcp": {"remote": {}}}
|
|
|
|
|
|
@pytest.mark.parametrize("suffix", [".json", ".jsonc"])
|
|
def test_install_stop_remove_restores_original_backup(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, suffix: str
|
|
) -> None:
|
|
from headroom.cli import install as cli_install
|
|
|
|
config_file = tmp_path / f"opencode{suffix}"
|
|
monkeypatch.setenv("OPENCODE_CONFIG", str(config_file))
|
|
monkeypatch.delenv("OPENCODE_HOME", raising=False)
|
|
original = (
|
|
b'{"theme":"before","provider":{"anthropic":{}},}\n'
|
|
if suffix == ".jsonc"
|
|
else b'{"theme":"before","provider":{"anthropic":{}}}\n'
|
|
)
|
|
config_file.write_bytes(original)
|
|
manifest = _manifest()
|
|
manifest.targets = ["opencode"]
|
|
monkeypatch.setattr(cli_install, "_save_apply_manifest", lambda current: None)
|
|
monkeypatch.setattr(cli_install, "save_manifest", lambda current: None)
|
|
monkeypatch.setattr(cli_install, "stop_runtime", lambda current: None)
|
|
monkeypatch.setattr(cli_install, "wait_stopped", lambda current: True)
|
|
monkeypatch.setattr(cli_install, "remove_supervisor", lambda current: None)
|
|
monkeypatch.setattr(cli_install, "delete_manifest", lambda profile: None)
|
|
|
|
cli_install._activate_deployment_mutations(manifest)
|
|
cli_install._deactivate_deployment_mutations(manifest)
|
|
assert manifest.mutations == []
|
|
cli_install._remove_deployment(manifest, restore_backup=True)
|
|
|
|
assert config_file.read_bytes() == original
|
|
assert not config_file.with_name(config_file.name + ".headroom-backup").exists()
|
|
|
|
|
|
def test_failed_activation_retains_backup_for_final_remove_after_reload(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
from headroom.cli import install as cli_install
|
|
|
|
monkeypatch.setenv("HEADROOM_WORKSPACE_DIR", str(tmp_path / "headroom-state"))
|
|
config_file = tmp_path / "opencode.json"
|
|
monkeypatch.setenv("OPENCODE_CONFIG", str(config_file))
|
|
monkeypatch.delenv("OPENCODE_HOME", raising=False)
|
|
original = b'{"theme":"original"}\n'
|
|
config_file.write_bytes(original)
|
|
manifest = _manifest()
|
|
manifest.targets = ["opencode"]
|
|
real_save = cli_install._save_apply_manifest
|
|
real_save(manifest)
|
|
|
|
def fail_activation_save(current):
|
|
if current.mutations:
|
|
raise OSError("manifest save failed")
|
|
real_save(current)
|
|
|
|
monkeypatch.setattr(cli_install, "_save_apply_manifest", fail_activation_save)
|
|
with pytest.raises(OSError, match="manifest save failed"):
|
|
cli_install._activate_deployment_mutations(manifest)
|
|
|
|
backup_file = config_file.with_name(config_file.name + ".headroom-backup")
|
|
assert manifest.mutations == []
|
|
assert backup_file.read_bytes() == original
|
|
from headroom.install.state import load_manifest as load_state_manifest
|
|
|
|
persisted = load_state_manifest(manifest.profile)
|
|
assert persisted is not None
|
|
|
|
monkeypatch.setattr(cli_install, "_save_apply_manifest", lambda current: None)
|
|
monkeypatch.setattr(cli_install, "stop_runtime", lambda current: None)
|
|
monkeypatch.setattr(cli_install, "wait_stopped", lambda current: True)
|
|
monkeypatch.setattr(cli_install, "remove_supervisor", lambda current: None)
|
|
monkeypatch.setattr(cli_install, "delete_manifest", lambda profile: None)
|
|
cli_install._remove_deployment(persisted, restore_backup=True)
|
|
assert config_file.read_bytes() == original
|
|
assert not backup_file.exists()
|
|
|
|
|
|
@pytest.mark.parametrize("config_name", ["opencode.jsonc", "custom-settings.jsonc"])
|
|
def test_legacy_jsonc_backup_migrates_before_restart_and_final_removal(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, config_name: str
|
|
) -> None:
|
|
from headroom.cli import install as cli_install
|
|
|
|
config_file = tmp_path / config_name
|
|
monkeypatch.setenv("OPENCODE_CONFIG", str(config_file))
|
|
monkeypatch.delenv("OPENCODE_HOME", raising=False)
|
|
original = b'// user\'s original JSONC\n{"theme":"original",}\n'
|
|
config_file.write_bytes(b'{"theme":"edited","provider":{"headroom":{}}}\n')
|
|
legacy_backup = config_file.with_suffix(".json.headroom-backup")
|
|
legacy_backup.write_bytes(original)
|
|
canonical_backup = config_file.with_name(config_file.name + ".headroom-backup")
|
|
manifest = _manifest()
|
|
manifest.targets = ["opencode"]
|
|
monkeypatch.setattr(cli_install, "_save_apply_manifest", lambda current: None)
|
|
monkeypatch.setattr(cli_install, "save_manifest", lambda current: None)
|
|
monkeypatch.setattr(cli_install, "stop_runtime", lambda current: None)
|
|
monkeypatch.setattr(cli_install, "wait_stopped", lambda current: True)
|
|
monkeypatch.setattr(cli_install, "remove_supervisor", lambda current: None)
|
|
monkeypatch.setattr(cli_install, "delete_manifest", lambda profile: None)
|
|
|
|
cli_install._activate_deployment_mutations(manifest)
|
|
assert canonical_backup.read_bytes() == original
|
|
assert not legacy_backup.exists()
|
|
cli_install._deactivate_deployment_mutations(manifest)
|
|
cli_install._activate_deployment_mutations(manifest)
|
|
cli_install._remove_deployment(
|
|
manifest,
|
|
restore_backup=True,
|
|
)
|
|
assert config_file.read_bytes() == original
|
|
assert not canonical_backup.exists()
|
|
|
|
|
|
def test_jsonc_trailing_commas_preserve_consumer_edits_across_reactivation(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
from headroom.cli import install as cli_install
|
|
from headroom.install.providers import revert_mutations
|
|
from headroom.providers.opencode.config import _parse_json_loose
|
|
|
|
config_file = tmp_path / "opencode.jsonc"
|
|
monkeypatch.setenv("OPENCODE_CONFIG", str(config_file))
|
|
monkeypatch.delenv("OPENCODE_HOME", raising=False)
|
|
original = (
|
|
b'// original comment\n{"theme":"original","provider":{"anthropic":{"name":"Claude"},},'
|
|
b'"mcp":{"local":{},},}\n'
|
|
)
|
|
config_file.write_bytes(original)
|
|
manifest = _manifest()
|
|
manifest.targets = ["opencode"]
|
|
|
|
monkeypatch.setattr(cli_install, "_save_apply_manifest", lambda current: None)
|
|
cli_install._activate_deployment_mutations(manifest)
|
|
config_file.write_text(
|
|
'// consumer edit\n{"theme":"edited","provider":{"anthropic":{"name":"Claude",},'
|
|
'"headroom":{"options":{"baseURL":"http://127.0.0.1:8787/v1",},},},'
|
|
'"mcp":{"remote":{"url":"https://example.test",},},}\n'
|
|
)
|
|
cli_install._deactivate_deployment_mutations(manifest)
|
|
inactive = _parse_json_loose(config_file.read_text())
|
|
assert inactive["theme"] == "edited"
|
|
assert inactive["provider"] == {"anthropic": {"name": "Claude"}}
|
|
assert inactive["mcp"]["remote"]["url"] == "https://example.test"
|
|
cli_install._activate_deployment_mutations(manifest)
|
|
active = _parse_json_loose(config_file.read_text())
|
|
assert active["theme"] == "edited"
|
|
assert active["provider"]["headroom"]["options"]["baseURL"].endswith("/v1")
|
|
assert active["mcp"]["remote"]["url"] == "https://example.test"
|
|
revert_mutations(manifest, restore_backup=True)
|
|
assert config_file.read_bytes() == original
|
|
|
|
|
|
def test_successful_replacement_dropping_opencode_restores_original_snapshot(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
from headroom.cli import install as cli_install
|
|
from headroom.install.state import save_manifest_strict
|
|
|
|
monkeypatch.setenv("HEADROOM_WORKSPACE_DIR", str(tmp_path / "headroom-state"))
|
|
monkeypatch.setenv("HOME", str(tmp_path))
|
|
monkeypatch.setenv("OPENCODE_CONFIG", str(tmp_path / "opencode.json"))
|
|
monkeypatch.delenv("OPENCODE_HOME", raising=False)
|
|
config_file = tmp_path / "opencode.json"
|
|
original = b'{"theme":"original"}\n'
|
|
config_file.write_bytes(original)
|
|
previous = _manifest()
|
|
previous.targets = ["opencode"]
|
|
cli_install._activate_deployment_mutations(previous)
|
|
save_manifest_strict(previous)
|
|
replacement = _manifest()
|
|
replacement.targets = []
|
|
|
|
monkeypatch.setattr(cli_install, "load_manifest", lambda profile: previous)
|
|
monkeypatch.setattr(cli_install, "install_supervisor", lambda current, **kwargs: [])
|
|
monkeypatch.setattr(cli_install, "_start_deployment", lambda current: None)
|
|
monkeypatch.setattr(cli_install, "stop_runtime", lambda current: None)
|
|
monkeypatch.setattr(cli_install, "wait_stopped", lambda current: True)
|
|
monkeypatch.setattr(cli_install, "remove_supervisor", lambda current: None)
|
|
|
|
cli_install._apply_manifest(replacement)
|
|
|
|
assert config_file.read_bytes() == original
|
|
assert not config_file.with_name("opencode.json.headroom-backup").exists()
|
|
|
|
|
|
def test_failed_replacement_retains_opencode_snapshot_and_reinstates_provider(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
import click
|
|
|
|
from headroom.cli import install as cli_install
|
|
from headroom.install.state import save_manifest_strict
|
|
|
|
monkeypatch.setenv("HEADROOM_WORKSPACE_DIR", str(tmp_path / "headroom-state"))
|
|
monkeypatch.setenv("HOME", str(tmp_path))
|
|
monkeypatch.setenv("OPENCODE_CONFIG", str(tmp_path / "opencode.json"))
|
|
monkeypatch.delenv("OPENCODE_HOME", raising=False)
|
|
config_file = tmp_path / "opencode.json"
|
|
original = b'{"theme":"original"}\n'
|
|
config_file.write_bytes(original)
|
|
previous = _manifest()
|
|
previous.targets = ["opencode"]
|
|
cli_install._activate_deployment_mutations(previous)
|
|
save_manifest_strict(previous)
|
|
replacement = _manifest()
|
|
replacement.targets = []
|
|
backup_file = config_file.with_name("opencode.json.headroom-backup")
|
|
|
|
monkeypatch.setattr(cli_install, "load_manifest", lambda profile: previous)
|
|
monkeypatch.setattr(cli_install, "install_supervisor", lambda current, **kwargs: [])
|
|
|
|
def start(current):
|
|
if not current.targets:
|
|
raise click.ClickException("replacement startup failed")
|
|
|
|
monkeypatch.setattr(cli_install, "_start_deployment", start)
|
|
monkeypatch.setattr(cli_install, "stop_runtime", lambda current: None)
|
|
monkeypatch.setattr(cli_install, "wait_stopped", lambda current: True)
|
|
monkeypatch.setattr(cli_install, "remove_supervisor", lambda current: None)
|
|
|
|
with pytest.raises(click.ClickException, match="replacement startup failed"):
|
|
cli_install._apply_manifest(replacement)
|
|
|
|
assert backup_file.read_bytes() == original
|
|
from headroom.install.state import load_manifest as load_state_manifest
|
|
|
|
restored = load_state_manifest(previous.profile)
|
|
assert restored is not None and restored.mutations
|
|
active = json.loads(config_file.read_text())
|
|
assert active["provider"]["headroom"]["options"]["baseURL"].endswith("/v1")
|
|
|
|
|
|
def test_jsonc_migration_does_not_claim_json_installation_backup(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
from headroom.cli import install as cli_install
|
|
|
|
config_file = tmp_path / "opencode.jsonc"
|
|
json_config = tmp_path / "opencode.json"
|
|
monkeypatch.setenv("OPENCODE_CONFIG", str(config_file))
|
|
monkeypatch.delenv("OPENCODE_HOME", raising=False)
|
|
json_config.write_bytes(b'{"theme":"json install"}\n')
|
|
config_file.write_bytes(b'{"theme":"jsonc install"}\n')
|
|
legacy_backup = tmp_path / "opencode.json.headroom-backup"
|
|
legacy_original = b'{"theme":"json original"}\n'
|
|
legacy_backup.write_bytes(legacy_original)
|
|
manifest = _manifest()
|
|
manifest.targets = ["opencode"]
|
|
monkeypatch.setattr(cli_install, "_save_apply_manifest", lambda current: None)
|
|
|
|
cli_install._activate_deployment_mutations(manifest)
|
|
|
|
canonical_backup = tmp_path / "opencode.jsonc.headroom-backup"
|
|
assert canonical_backup.read_bytes() == b'{"theme":"jsonc install"}\n'
|
|
assert legacy_backup.read_bytes() == legacy_original
|
|
|
|
|
|
@pytest.fixture
|
|
def opencode_transaction(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
|
|
from headroom.cli import install as cli_install
|
|
|
|
monkeypatch.setenv("HEADROOM_WORKSPACE_DIR", str(tmp_path / "state"))
|
|
monkeypatch.setenv("HOME", str(tmp_path))
|
|
monkeypatch.setenv("OPENCODE_CONFIG", str(tmp_path / "opencode.json"))
|
|
monkeypatch.delenv("OPENCODE_HOME", raising=False)
|
|
monkeypatch.setattr(cli_install, "install_supervisor", lambda current, **kwargs: [])
|
|
monkeypatch.setattr(cli_install, "_start_deployment", lambda current: None)
|
|
monkeypatch.setattr(cli_install, "stop_runtime", lambda current: None)
|
|
monkeypatch.setattr(cli_install, "wait_stopped", lambda current: True)
|
|
monkeypatch.setattr(cli_install, "remove_supervisor", lambda current: None)
|
|
return cli_install
|
|
|
|
|
|
def test_failed_replacement_consumes_new_unowned_snapshot(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, opencode_transaction
|
|
) -> None:
|
|
import click
|
|
|
|
cli_install = opencode_transaction
|
|
config = tmp_path / "opencode.json"
|
|
original = b'{"theme":"dark"}\n'
|
|
config.write_bytes(original)
|
|
previous = _manifest()
|
|
cli_install.save_manifest_strict(previous)
|
|
replacement = _manifest()
|
|
replacement.targets = ["opencode"]
|
|
real_save = cli_install._save_apply_manifest
|
|
|
|
def fail_active_save(current):
|
|
if current.mutations:
|
|
raise OSError("activation persistence failed")
|
|
real_save(current)
|
|
|
|
with monkeypatch.context() as fault:
|
|
fault.setattr(cli_install, "_save_apply_manifest", fail_active_save)
|
|
with pytest.raises(click.ClickException):
|
|
cli_install._apply_manifest(replacement)
|
|
|
|
assert config.read_bytes() == original
|
|
assert not config.with_name(config.name + ".headroom-backup").exists()
|
|
later = b'{"theme":"light"}\n'
|
|
config.write_bytes(later)
|
|
retry = _manifest()
|
|
retry.targets = ["opencode"]
|
|
cli_install._apply_manifest(retry)
|
|
cli_install._remove_deployment(retry, restore_backup=True)
|
|
assert config.read_bytes() == later
|
|
|
|
|
|
@pytest.mark.parametrize("legacy_manifest", [False, True])
|
|
@pytest.mark.parametrize("retain_opencode", [False, True])
|
|
def test_changed_config_path_restores_previous_snapshot(
|
|
tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
opencode_transaction,
|
|
legacy_manifest: bool,
|
|
retain_opencode: bool,
|
|
) -> None:
|
|
cli_install = opencode_transaction
|
|
original_config = tmp_path / "opencode.json"
|
|
original = b'{"theme":"old config original"}\n'
|
|
original_config.write_bytes(original)
|
|
previous = _manifest()
|
|
previous.targets = ["opencode"]
|
|
cli_install._activate_deployment_mutations(previous)
|
|
if legacy_manifest:
|
|
previous.artifacts = []
|
|
cli_install.save_manifest_strict(previous)
|
|
new_config = tmp_path / "different.json"
|
|
new_original = b'{"theme":"new config original"}\n'
|
|
new_config.write_bytes(new_original)
|
|
monkeypatch.setenv("OPENCODE_CONFIG", str(new_config))
|
|
replacement = _manifest()
|
|
replacement.targets = ["opencode"] if retain_opencode else []
|
|
|
|
cli_install._apply_manifest(replacement)
|
|
|
|
assert original_config.read_bytes() == original
|
|
assert not original_config.with_name(original_config.name + ".headroom-backup").exists()
|
|
if retain_opencode:
|
|
assert "headroom" in json.loads(new_config.read_text())["provider"]
|
|
cli_install._remove_deployment(replacement, restore_backup=True)
|
|
assert new_config.read_bytes() == new_original
|
|
|
|
|
|
def test_canonical_snapshot_wins_over_ambiguous_legacy_backup(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, opencode_transaction
|
|
) -> None:
|
|
cli_install = opencode_transaction
|
|
config = tmp_path / "opencode.jsonc"
|
|
monkeypatch.setenv("OPENCODE_CONFIG", str(config))
|
|
config.write_bytes(b'{"theme":"live"}\n')
|
|
canonical = config.with_name("opencode.jsonc.headroom-backup")
|
|
original = b'// current original\n{"theme":"light",}\n'
|
|
canonical.write_bytes(original)
|
|
legacy = config.with_suffix(".json.headroom-backup")
|
|
legacy_original = b'{"theme":"dark"}\n'
|
|
legacy.write_bytes(legacy_original)
|
|
manifest = _manifest()
|
|
manifest.targets = ["opencode"]
|
|
|
|
cli_install._activate_deployment_mutations(manifest)
|
|
cli_install._deactivate_deployment_mutations(manifest)
|
|
cli_install._activate_deployment_mutations(manifest)
|
|
cli_install._remove_deployment(manifest, restore_backup=True)
|
|
|
|
assert config.read_bytes() == original
|
|
assert not canonical.exists()
|
|
assert legacy.read_bytes() == legacy_original
|
|
|
|
later = b'// edited after removal\n{"theme":"new choice",}\n'
|
|
config.write_bytes(later)
|
|
second = _manifest()
|
|
second.targets = ["opencode"]
|
|
cli_install._activate_deployment_mutations(second)
|
|
assert canonical.read_bytes() == later
|
|
cli_install._remove_deployment(second, restore_backup=True)
|
|
assert config.read_bytes() == later
|
|
assert not canonical.exists()
|
|
assert legacy.read_bytes() == legacy_original
|
|
|
|
|
|
def test_replacement_restore_failure_can_finish_after_manifest_reload(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, opencode_transaction
|
|
) -> None:
|
|
import click
|
|
|
|
cli_install = opencode_transaction
|
|
config = tmp_path / "opencode.json"
|
|
original = b'{"theme":"original"}\n'
|
|
config.write_bytes(original)
|
|
previous = _manifest()
|
|
previous.targets = ["opencode"]
|
|
cli_install._activate_deployment_mutations(previous)
|
|
replacement = _manifest()
|
|
restore = cli_install.restore_opencode_backup
|
|
|
|
def fail_restore(*args):
|
|
raise OSError("backup temporarily unreadable")
|
|
|
|
with monkeypatch.context() as fault:
|
|
fault.setattr(cli_install, "restore_opencode_backup", fail_restore)
|
|
with pytest.raises((OSError, click.ClickException)):
|
|
cli_install._apply_manifest(replacement)
|
|
|
|
persisted = cli_install.load_manifest(replacement.profile)
|
|
assert persisted is not None and persisted.targets == []
|
|
monkeypatch.setattr(cli_install, "restore_opencode_backup", restore)
|
|
retry = _manifest()
|
|
cli_install._apply_manifest(retry)
|
|
|
|
assert config.read_bytes() == original
|
|
assert not config.with_name(config.name + ".headroom-backup").exists()
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("config_name", "location"),
|
|
[
|
|
("opencode.jsonc", "override"),
|
|
("custom-settings.jsonc", "override"),
|
|
("opencode.jsonc", "opencode_home"),
|
|
("opencode.jsonc", "default_home"),
|
|
],
|
|
)
|
|
@pytest.mark.parametrize("ambiguity", ["canonical", "sibling"])
|
|
def test_ambiguous_legacy_backup_stays_unowned_across_two_install_remove_cycles(
|
|
tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
config_name: str,
|
|
location: str,
|
|
ambiguity: str,
|
|
) -> None:
|
|
"""Removing the conflicting file must not grant ownership of legacy bytes."""
|
|
monkeypatch.setenv("HOME", str(tmp_path))
|
|
monkeypatch.setenv("USERPROFILE", str(tmp_path))
|
|
monkeypatch.delenv("OPENCODE_CONFIG", raising=False)
|
|
monkeypatch.delenv("OPENCODE_HOME", raising=False)
|
|
config_dir = tmp_path / ".config" / "opencode" if location == "default_home" else tmp_path
|
|
config_dir.mkdir(parents=True, exist_ok=True)
|
|
config = config_dir / config_name
|
|
if location == "override":
|
|
monkeypatch.setenv("OPENCODE_CONFIG", str(config))
|
|
elif location == "opencode_home":
|
|
monkeypatch.setenv("OPENCODE_HOME", str(config_dir))
|
|
live = b'{"theme":"live"}\n'
|
|
original = b'// current original\r\n{"theme":"light",}\r\n'
|
|
unrelated = b'// unrelated original\r\n{"theme":"dark",}\r\n'
|
|
config.write_bytes(live if ambiguity == "canonical" else original)
|
|
canonical = config.with_name(config.name + ".headroom-backup")
|
|
legacy = config.with_suffix(".json.headroom-backup")
|
|
legacy.write_bytes(unrelated)
|
|
sibling = config.with_suffix(".json")
|
|
if ambiguity == "canonical":
|
|
canonical.write_bytes(original)
|
|
else:
|
|
sibling.write_bytes(b'{"theme":"separate JSON installation"}\n')
|
|
|
|
first = _manifest()
|
|
mutation = apply_provider_scope(first)
|
|
assert mutation is not None
|
|
revert_provider_scope(mutation, first)
|
|
assert config.read_bytes() == original
|
|
assert not canonical.exists()
|
|
assert legacy.read_bytes() == unrelated
|
|
if ambiguity == "sibling":
|
|
sibling.unlink()
|
|
|
|
# A new deployment after the first snapshot has been consumed must take a
|
|
# fresh snapshot, even though the formerly ambiguous legacy file remains.
|
|
later = b'// edited between installations\r\n{"theme":"new choice",}\r\n'
|
|
config.write_bytes(later)
|
|
second = _manifest()
|
|
mutation = apply_provider_scope(second)
|
|
assert mutation is not None
|
|
assert canonical.read_bytes() == later
|
|
assert legacy.read_bytes() == unrelated
|
|
revert_provider_scope(mutation, second, restore_backup=False)
|
|
mutation = apply_provider_scope(second)
|
|
assert mutation is not None
|
|
revert_provider_scope(mutation, second)
|
|
assert config.read_bytes() == later
|
|
assert not canonical.exists()
|
|
assert legacy.read_bytes() == unrelated
|
|
|
|
|
|
@pytest.mark.parametrize("operation", ["apply", "restore"])
|
|
def test_ambiguous_legacy_backup_marker_failure_preserves_all_files(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, operation: str
|
|
) -> None:
|
|
from headroom.providers.opencode.install import restore_opencode_backup
|
|
|
|
config = tmp_path / "opencode.jsonc"
|
|
monkeypatch.setenv("OPENCODE_CONFIG", str(config))
|
|
live = b'{"theme":"live"}\n'
|
|
original = b'{"theme":"original"}\n'
|
|
unrelated = b'{"theme":"unrelated"}\n'
|
|
config.write_bytes(live)
|
|
canonical = config.with_name(config.name + ".headroom-backup")
|
|
canonical.write_bytes(original)
|
|
legacy = config.with_suffix(".json.headroom-backup")
|
|
legacy.write_bytes(unrelated)
|
|
touch = Path.touch
|
|
|
|
def fail_marker(path: Path, *args, **kwargs) -> None:
|
|
if path.name.endswith(".jsonc-migration-blocked"):
|
|
raise PermissionError("migration marker is not writable")
|
|
touch(path, *args, **kwargs)
|
|
|
|
with monkeypatch.context() as fault:
|
|
fault.setattr(Path, "touch", fail_marker)
|
|
with pytest.raises(PermissionError, match="migration marker"):
|
|
if operation == "apply":
|
|
apply_provider_scope(_manifest())
|
|
else:
|
|
restore_opencode_backup(str(config), str(canonical))
|
|
|
|
assert config.read_bytes() == live
|
|
assert canonical.read_bytes() == original
|
|
assert legacy.read_bytes() == unrelated
|
|
restore_opencode_backup(str(config), str(canonical))
|
|
assert config.read_bytes() == original
|
|
assert not canonical.exists()
|
|
assert legacy.read_bytes() == unrelated
|
|
|
|
|
|
@pytest.mark.parametrize("config_name", ["opencode.jsonc", "custom-settings.jsonc"])
|
|
def test_unambiguous_legacy_backup_still_migrates_and_restores(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, config_name: str
|
|
) -> None:
|
|
config = tmp_path / config_name
|
|
monkeypatch.setenv("OPENCODE_CONFIG", str(config))
|
|
config.write_bytes(b'{"theme":"live"}\n')
|
|
legacy = config.with_suffix(".json.headroom-backup")
|
|
original = b'// original\r\n{"theme":"original",}\r\n'
|
|
legacy.write_bytes(original)
|
|
canonical = config.with_name(config.name + ".headroom-backup")
|
|
manifest = _manifest()
|
|
|
|
mutation = apply_provider_scope(manifest)
|
|
assert mutation is not None
|
|
assert canonical.read_bytes() == original
|
|
assert not legacy.exists()
|
|
revert_provider_scope(mutation, manifest)
|
|
assert config.read_bytes() == original
|
|
assert not canonical.exists()
|
|
|
|
|
|
def test_apply_provider_scope_leaves_unparseable_config_untouched(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
"""apply_provider_scope reports an unparseable config instead of replacing it."""
|
|
monkeypatch.setenv("HOME", str(tmp_path))
|
|
monkeypatch.setenv("USERPROFILE", str(tmp_path))
|
|
monkeypatch.delenv("OPENCODE_HOME", raising=False)
|
|
monkeypatch.delenv("OPENCODE_CONFIG", raising=False)
|
|
config_file = tmp_path / ".config" / "opencode" / "opencode.json"
|
|
config_file.parent.mkdir(parents=True)
|
|
original = '{"model": "gpt-4o" "oops": 1}'
|
|
config_file.write_text(original, encoding="utf-8")
|
|
|
|
with pytest.raises(click.ClickException, match="could not parse"):
|
|
apply_provider_scope(_manifest())
|
|
|
|
assert config_file.read_text(encoding="utf-8") == original
|