opencode: clear the cached key, and let --purge keep the API key

opencode caches a provider's key in ~/.local/share/opencode/auth.json and that
copy wins over opencode.json: with a stale entry it fails auth without sending
a request at all and asks for the key interactively. Verified by A/B on a live
install -- correct entry works, wrong entry never reaches the server, absent
entry works and reads the key file.

llm-host mints a fresh key whenever the config dir is purged, so the cached
copy went stale on every reinstall. configure() now drops the entry for its own
provider, leaving the {file:...} reference authoritative; it tracks the key file
and cannot go stale. remove() takes the cached key with the provider. Other
providers' credentials are never touched, and an unreadable store is left alone
rather than repaired.

Add `uninstall --purge --keep-key` so the key survives a purge and a reinstall
reuses it -- clients elsewhere then keep working. Warn about the new key when
it doesn't. Factor the purge into purge() so it can be tested without touching
a real install.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Neil Bantoc 2026-11-03 00:19:54 -08:00
parent 13a7def080
commit 8c6615dbb4
3 changed files with 101 additions and 11 deletions

View file

@ -209,7 +209,7 @@ def setup_opencode(cfg, ask, install_ok=False, later="llmhost opencode-setup"):
say("opencode's installer failed; see the output above. Retry later with: llmhost opencode-setup")
return
say("opencode installed to ~/.opencode/bin (open a new terminal, or run: export PATH=\"$HOME/.opencode/bin:$PATH\")")
status, path, prov = opencode.configure(cfg)
status, path, prov, auth_cleared = opencode.configure(cfg)
if status in ("has-comments", "unparsable"):
why = "has comments, which an automatic edit would delete" if status == "has-comments" else "isn't valid JSON"
say(f"\n{path} {why}, so it was left as is.\n"
@ -220,6 +220,10 @@ def setup_opencode(cfg, ask, install_ok=False, later="llmhost opencode-setup"):
say(f"\nopencode: {msg} the `{prov}` provider \u2014 {label} \u2014 in {path} (http://localhost:{cfg['api_port']}/v1).")
if prov != opencode.PROVIDER:
say(f"(`{opencode.PROVIDER}` is taken by another provider, which was left alone.)")
if auth_cleared:
say(f"Cleared opencode's cached key for `{prov}` in {opencode.auth_file()}, so it reads\n"
f"{paths.KEY_FILE} instead. A cached copy goes stale whenever the key is\n"
f"regenerated, and opencode then asks for it interactively.")
if status == "added":
say(f"Start it with: opencode (pick a model with /models; this server answers as {prov}/{cfg['active']})")
@ -517,6 +521,23 @@ def hf_cache_rm_cmd(repos):
"--cache-dir", str(paths.hf_hub_dir()), *[f"model/{r}" for r in repos]]
def purge(keep_key=False):
"""Remove the config dir, the data dir and the launcher. With keep_key, the API key
survives, so a reinstall reuses it and clients configured with it keep working
(setup only mints a key when the file is absent). Returns the kept path, or None."""
keep = paths.KEY_FILE.read_text() if keep_key and paths.KEY_FILE.exists() else None
shutil.rmtree(paths.CONFIG_DIR, ignore_errors=True)
shutil.rmtree(paths.DATA, ignore_errors=True)
paths.SHIM.unlink(missing_ok=True)
if keep is None:
return None
paths.CONFIG_DIR.mkdir(parents=True, exist_ok=True)
fd = os.open(paths.KEY_FILE, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
with os.fdopen(fd, "w") as f:
f.write(keep)
return paths.KEY_FILE
def cmd_uninstall(a):
# Read the catalog now: --purge deletes it along with the rest of llm-host.
cached = cached_catalog_models()
@ -542,10 +563,14 @@ def cmd_uninstall(a):
why = "has comments" if oc_status == "has-comments" else "isn't valid JSON"
say(f"opencode: {oc_path} {why}, so it was left alone; remove the llm-host provider by hand.")
if a.purge:
shutil.rmtree(paths.CONFIG_DIR, ignore_errors=True)
shutil.rmtree(paths.DATA, ignore_errors=True)
paths.SHIM.unlink(missing_ok=True)
say("Removed config, API key and llm-host itself.")
kept = purge(a.keep_key)
if kept is None:
say("Removed config, API key and llm-host itself.")
say("A reinstall mints a new API key, so clients elsewhere will need the new one\n"
"(`llmhost opencode-config`). Keep the current key instead with: --purge --keep-key")
else:
say(f"Removed config and llm-host itself; kept the API key at {kept},\n"
"so a reinstall reuses it and clients elsewhere keep working.")
if not cached:
return
total = sum(g for _, g in cached)
@ -669,6 +694,7 @@ def main(argv=None):
s = sub.add_parser("uninstall", help="remove services and backends (asks before deleting models)")
s.add_argument("--purge", action="store_true", help="also remove config, API key and llm-host itself")
s.add_argument("--models", action="store_true", help="also delete downloaded models without asking")
s.add_argument("--keep-key", action="store_true", help="with --purge, keep the API key so a reinstall reuses it")
s.set_defaults(fn=cmd_uninstall)
sub.add_parser("run-model").set_defaults(fn=cmd_run_model)
sub.add_parser("run-web").set_defaults(fn=cmd_run_web)