diff --git a/install.sh b/install.sh index cf6514b2..406e7be2 100755 --- a/install.sh +++ b/install.sh @@ -3607,9 +3607,13 @@ if os.environ.get("OC_DENY") is not None: doc["permission"] = json.loads(os.environ["OC_DENY"]) if os.environ.get("OC_DENY_V2") is not None and os.environ.get("OC_MAJOR") == "2": doc["permissions"] = json.loads(os.environ["OC_DENY_V2"]) - # the cloud and the outside services, cut for the Agent tab (measured 2026-11-14: - # without policies the OpenCode provider and its free models answer on /api/model) - doc["policies"] = [{"action": "provider.use", "resource": "opencode", "effect": "deny"}] + # the outside services, cut for the Agent tab: session sharing and the web search tool. + # opencode's own hosted provider stays listed, with its free models, beside this box's, + # as 1.x lists it: v1.56.0 to v1.59.0 denied it here ("policies", provider.use) and its + # free models left the Agent tab's picker (decision 1595, 2026-11-25). The default and + # the small model above stay this box's lane, so a prompt goes to the cloud only when a + # model of it is picked; "disabled_providers": ["opencode"] in the user's own config + # keeps it out (2.x reads it as that same deny, config/normalize.ts of 2.3.2). doc["share"] = "disabled" doc["websearch"] = False # With the version pinned, opencode looks for no update at all: false, the JSON boolean @@ -3681,6 +3685,10 @@ else if [ "${OC_SERVE_MAJOR:-1}" = "2" ]; then python3 "$REPO_DIR/oc-merge-limits.py" "$OC_USER_CFG" --deny-permissions || true fi + # a fresh install of v1.56.0 to v1.59.0 copied the generated config here with its deny on + # opencode's hosted provider, which kept that provider's free models out of the Agent tab's + # picker (decision 1595): only that exact deny goes, and a policy of the user's own stays + python3 "$REPO_DIR/oc-merge-limits.py" "$OC_USER_CFG" --remove-provider-deny || true fi # An existing opencode.json keeps the user's other providers, but its limits for # THIS lane must follow the served engine (v1.5.2: a flash conversation allowed diff --git a/oc-merge-limits.py b/oc-merge-limits.py index dfe5bb75..38ae1b09 100755 --- a/oc-merge-limits.py +++ b/oc-merge-limits.py @@ -9,6 +9,7 @@ Usage: oc-merge-limits.py oc-merge-limits.py --deny-bash oc-merge-limits.py --deny-permissions oc-merge-limits.py --remove-permissions + oc-merge-limits.py --remove-provider-deny oc-merge-limits.py --remove-compaction-v2 oc-merge-limits.py --agent-auto-permission oc-merge-limits.py --agent-auto-config-content @@ -86,6 +87,11 @@ separate commands on purpose: OpenCode 1.19 refuses to start when a config holds 1.19.5 on 2026-11-14), so the v2 keys are written only by the v2 paths and --remove- permissions takes OUR array back out when a box rolls back to 1.x. deny_permissions_v2() is the array for the config install.sh generates. +--remove-provider-deny takes "policies" out when it is exactly the deny on opencode's hosted +provider that v1.56.0 to v1.59.0 wrote in the generated v2 config, and a fresh install copied +into the user's global one: it took that provider's free models out of the Agent tab's +picker, where 1.x lists them (decision 1595, 2026-11-25). Policies of the person's own stay, +and "disabled_providers" is never touched: that is how a person keeps the provider out. --agent-auto-permission prints the OPENCODE_PERMISSION the Agent tab's 1.x unit takes with AGENT_AUTO=1: opencode merges it after every rule of the files, so the same rules come again after its {"*": "allow"}, under "*bash" and "*webfetch", keys the files do not have. OpenCode 2 ignores @@ -1065,6 +1071,57 @@ def remove_permissions(path: str) -> int: return 0 +# What install.sh wrote in the generated OpenCode 2 config from v1.56.0 to v1.59.0, and a fresh install +# copied into the user's global config with it: a deny on opencode's hosted provider, which took its free +# models out of the Agent tab's picker, where 1.x lists them (decision 1595, 2026-11-25). No release writes +# it any more; --remove-provider-deny takes it out, and the purge of --remove-providers still counts it as +# this box's in a copy made then. +PROVIDER_DENY_V156 = [{"action": "provider.use", "resource": "opencode", "effect": "deny"}] + + +def remove_provider_deny(path: str) -> int: + """Take the deny on opencode's hosted provider out of a config whose "policies" is exactly the one + v1.56.0 to v1.59.0 wrote (the copy a fresh install made). Policies of the person's own are theirs: + left alone, and said when that deny is among them. A config without "policies" is not written at + all, and "disabled_providers" is never touched: it is how a person keeps the provider out (2.x reads + it as this same deny, config/normalize.ts of 2.3.2).""" + opened = _open(path) + if opened is None: + return 1 + text, doc = opened + if "policies" not in doc: + print("no policies here: unchanged") + return 0 + pol = doc["policies"] + if pol != PROVIDER_DENY_V156: + if isinstance(pol, list) and PROVIDER_DENY_V156[0] in pol: + print(f"{path}: its policies holds rules of yours beside the deny on opencode's provider; left " + f"alone (that provider's free models stay out of the picker while the deny is there)") + else: + print(f"{path}: its policies is not the one this box wrote; left alone") + return 0 + want = copy.deepcopy(doc) + del want["policies"] + try: + new = remove_member(text, ["policies"]) + except ValueError as e: + print(f"{path}: refused, {e}; unchanged") + return 1 + saved, bad = commit(path, new, want) + if bad: + print(f"{path}: refused, {bad}; unchanged.") + return 1 + try: + wrong = "the policies are still there" if "policies" in _reread(path) else "" + except ValueError as e: + wrong = str(e) + if not _landed(path, saved, wrong, "the removal of the provider deny"): + return 1 + print(f"the deny on opencode's hosted provider is out of {path}: its free models are listed again " + f"(backup {saved})") + return 0 + + def agent_auto_permission() -> str: """OPENCODE_PERMISSION for AGENT_AUTO=1. opencode merges it into the config after every rule of the files, so its "*" would win over DENY_KEY's rules (an echo naming an aliyuncs.com host ran @@ -1299,10 +1356,11 @@ def remove_providers(path: str, key_file: str) -> int: # since v1.40.4 the copy also holds the rules --deny-bash put in it: a "permission" of any other # content is the user's, and keeps the file (the copy stayed, emptied of its provider, found in review). # OpenCode 2's "permissions" from the same command is ours too, and no reason to keep the copy. - # A fresh OpenCode 2 install copies the generated config with its v2 keys too (install.sh: policies, - # share, websearch, update): ours when they hold exactly what install.sh writes, the user's - # otherwise (review of v1.56.0, finding 3: the copy stayed, emptied of its provider). - ours_v2 = {"policies": [{"action": "provider.use", "resource": "opencode", "effect": "deny"}], + # A fresh OpenCode 2 install copies the generated config with its v2 keys too (install.sh: share, + # websearch, update, and from v1.56.0 to v1.59.0 the provider deny): ours when they hold exactly what + # install.sh writes or wrote, the user's otherwise (review of v1.56.0, finding 3: the copy stayed, + # emptied of its provider). + ours_v2 = {"policies": PROVIDER_DENY_V156, "share": "disabled", "websearch": False, "update": "disable"} if (set(cur) <= {"$schema", "autoupdate", "compaction", "provider", "permission", "permissions", *ours_v2} @@ -1357,6 +1415,8 @@ def main(argv: list[str]) -> int: return merge_deny_permissions(argv[1]) if len(argv) == 3 and argv[2] == "--remove-permissions": return remove_permissions(argv[1]) + if len(argv) == 3 and argv[2] == "--remove-provider-deny": + return remove_provider_deny(argv[1]) if len(argv) == 3 and argv[2] == "--remove-compaction-v2": return remove_compaction_v2(argv[1]) if argv[1:] == ["--agent-auto-permission"]: diff --git a/tests/test_install_oc_artifact.py b/tests/test_install_oc_artifact.py index e9886a85..49f2f445 100644 --- a/tests/test_install_oc_artifact.py +++ b/tests/test_install_oc_artifact.py @@ -670,8 +670,8 @@ class TheGeneratedConfigFollowsTheBinaryThatStands(unittest.TestCase): self.assertEqual([(p["action"], p["resource"]) for p in d["permissions"]], [(a, r) for a in ("shell", "webfetch") for r in ("*aliyuncs.com*", "*169.254.169.254*", "*100.100.100.200*")]) - self.assertEqual(d["policies"], - [{"action": "provider.use", "resource": "opencode", "effect": "deny"}]) + # no provider is denied: opencode's hosted one stays listed with its free models (decision 1595) + self.assertNotIn("policies", d) self.assertEqual(d["share"], "disabled") def test_serve_1_writes_no_v2_key_at_all(self): @@ -688,5 +688,23 @@ class TheGeneratedConfigFollowsTheBinaryThatStands(unittest.TestCase): "*100.100.100.200*": "deny"}) +class TheGeneratedConfigDeniesNoProvider(unittest.TestCase): + """opencode's hosted provider stays listed with its free models beside the box's, on both + majors, as 1.x always listed it: from v1.56.0 to v1.59.0 the v2 config denied it ("policies", + provider.use on "opencode") and the free models left the Agent tab's picker, which nobody had + decided (decision 1595, 2026-11-25). The default and the small model stay this box's lane, so + a prompt goes to the cloud only when a model of it is picked.""" + + def test_no_major_denies_or_narrows_the_providers(self): + for major, serve in (("2", None), ("1", None), ("1", "2"), ("2", "1")): + with self.subTest(major=major, serve=serve): + d = generate("27b", "1m", unit_ctx=1010000, flash_unit=True, pair=("551000", "160000"), + doc=True, major=major, serve_major=serve) + for key in ("policies", "disabled_providers", "enabled_providers"): + self.assertNotIn(key, d) + for key in ("model", "small_model"): + self.assertIn(d[key].split("/", 1)[0], d["provider"], f"{key} is not one of this box's") + + if __name__ == "__main__": unittest.main(verbosity=2) diff --git a/tests/test_oc_provider_deny.py b/tests/test_oc_provider_deny.py new file mode 100644 index 00000000..4a6ac84d --- /dev/null +++ b/tests/test_oc_provider_deny.py @@ -0,0 +1,203 @@ +#!/usr/bin/env python3 +"""OpenCode's hosted provider and its free models stay listed beside this box's (decision 1595, 2026-11-25). + +1.x lists opencode's own provider and its free models (big-pickle and the others) in the Agent tab's +picker, beside this box's lanes. From v1.56.0 to v1.59.0 the config install.sh generates for OpenCode 2 +denied that provider ("policies": provider.use on "opencode"), and a fresh install copied the deny into +the user's global config, which opencode reads too: on 2.x the free models were gone from the picker, +and nobody had decided it (the owner found it on the reference box, 2026-11-25). The generated config +denies no provider now (tests/test_install_oc_artifact.py), and an update takes that exact deny back +out of the global config: --remove-provider-deny. A deny of the person's own stays, and +"disabled_providers": ["opencode"], which 2.x reads as that same deny (config/normalize.ts of 2.3.2), +is how a person keeps the provider out: nothing here ever touches it.""" +import importlib.util +import json +import pathlib +import subprocess +import sys +import tempfile +import unittest + +REPO = pathlib.Path(__file__).resolve().parents[1] +SCRIPT = REPO / "oc-merge-limits.py" +INSTALL = REPO / "install.sh" +DENY = {"action": "provider.use", "resource": "opencode", "effect": "deny"} +BOX = {"qwen38": {"npm": "@ai-sdk/openai-compatible", "name": "Qwen3.8-27B (DGX Spark)", + "models": {"qwen3.8-27b": {"name": "Qwen3.8-27B (local)"}}}} + + +def ocm(): + spec = importlib.util.spec_from_file_location("ocm_provider_deny", SCRIPT) + m = importlib.util.module_from_spec(spec) + spec.loader.exec_module(m) + return m + + +def the_copy(**changes) -> dict: + """The global config a fresh v1.56.0 to v1.59.0 install of OpenCode 2 left: its generated config.""" + doc = {"$schema": "https://opencode.ai/config.json", "provider": BOX, + "compaction": {"keep": {"tokens": 132000}, "background": True}, + "model": "qwen38/qwen3.8-27b", "small_model": "qwen38/qwen3.8-27b", + "policies": [DENY], "share": "disabled", "websearch": False, "update": "disable"} + doc.update(changes) + return doc + + +# the same copy, edited by its person: a comment, a key of theirs, a trailing comma +EDITED = ('{\n' + ' "$schema": "https://opencode.ai/config.json",\n' + ' // mine: keep this line\n' + ' "provider": ' + json.dumps(BOX) + ',\n' + ' "model": "qwen38/qwen3.8-27b",\n' + ' "policies": [\n' + ' {"action": "provider.use", "resource": "opencode", "effect": "deny"} /* the copy\'s */\n' + ' ],\n' + ' "share": "disabled",\n' + ' "plugin": ["mine.js"],\n' + '}\n') + + +def run(path): + r = subprocess.run([sys.executable, str(SCRIPT), str(path), "--remove-provider-deny"], + capture_output=True, text=True, timeout=60) + return r.returncode, r.stdout + r.stderr + + +class TheDenyACopyCarriedGoes(unittest.TestCase): + + def setUp(self): + self.dir = pathlib.Path(tempfile.mkdtemp(prefix="oc-provider-deny-")) + self.cfg = self.dir / "opencode.json" + + def backups(self): + return sorted(self.dir.glob("opencode.json.bak-*")) + + def test_the_exact_deny_goes_and_nothing_else_moves(self): + m = ocm() + for name, text in (("the copy", json.dumps(the_copy(), indent=2) + "\n"), ("edited by its person", EDITED)): + with self.subTest(name): + for b in self.backups(): + b.unlink() + self.cfg.write_text(text) + rc, out = run(self.cfg) + self.assertEqual(rc, 0, out) + self.assertIn("free models are listed again", out) + after = self.cfg.read_text() + want = m.load(text) + del want["policies"] + self.assertEqual(m.load(after), want) + self.assertNotIn("provider.use", after) + # every line outside the policies member is as it was, in its order + kept = [ln for ln in text.splitlines() + if not any(w in ln for w in ('"policies"', "provider.use", '"action"', '"resource"', + '"effect"', " {", " }", " ],"))] + self.assertEqual([ln for ln in after.splitlines() if ln in kept], kept) + if name == "edited by its person": + self.assertIn(" // mine: keep this line\n", after) + self.assertIn(' "plugin": ["mine.js"],\n}', after) + # one backup, holding the file as it was + self.assertEqual([b.read_text() for b in self.backups()], [text]) + + def test_a_second_run_changes_nothing(self): + self.cfg.write_text(json.dumps(the_copy(), indent=2) + "\n") + self.assertEqual(run(self.cfg)[0], 0) + once = self.cfg.read_text() + rc, out = run(self.cfg) + self.assertEqual(rc, 0, out) + self.assertIn("unchanged", out) + self.assertEqual(self.cfg.read_text(), once) + self.assertEqual(len(self.backups()), 1, "no backup for a run that changes nothing") + + def test_policies_of_the_persons_own_stay(self): + anthropic = {"action": "provider.use", "resource": "anthropic", "effect": "deny"} + for pol, said in (([DENY, anthropic], "rules of yours"), ([anthropic, DENY], "rules of yours"), + ([DENY, DENY], "rules of yours"), + ([{"action": "provider.use", "resource": "*", "effect": "deny"}], "not the one this box wrote"), + ([{**DENY, "effect": "allow"}], "not the one this box wrote"), + ([], "not the one this box wrote"), + ({"provider.use": "deny"}, "not the one this box wrote")): + with self.subTest(pol=pol): + text = json.dumps(the_copy(policies=pol), indent=2) + "\n" + self.cfg.write_text(text) + rc, out = run(self.cfg) + self.assertEqual(rc, 0, out) + self.assertIn(said, out) + self.assertEqual(self.cfg.read_text(), text) + self.assertEqual(self.backups(), []) + + def test_disabled_providers_is_never_touched(self): + # how a person keeps opencode's provider out: 2.x reads it as the same deny, and no update takes it out + text = json.dumps(the_copy(policies=None, disabled_providers=["opencode"]), indent=2) + "\n" + text = text.replace(' "policies": null,\n', "") + self.assertNotIn("policies", text) + self.cfg.write_text(text) + rc, out = run(self.cfg) + self.assertEqual(rc, 0, out) + self.assertIn("unchanged", out) + self.assertEqual(self.cfg.read_text(), text) + self.assertEqual(self.backups(), []) + + def test_a_file_it_cannot_read_is_left_as_it_is(self): + for text in ('{"policies": [', "[1, 2]\n"): + with self.subTest(text=text): + self.cfg.write_text(text) + rc, out = run(self.cfg) + self.assertEqual(rc, 1, out) + self.assertEqual(self.cfg.read_text(), text) + self.assertEqual(self.backups(), []) + + def test_the_deny_is_the_one_the_releases_wrote(self): + # v1.56.0 to v1.59.0 wrote exactly this (install.sh, the generated v2 config); the purge of + # uninstall.sh still counts it as this box's in a copy made then + self.assertEqual(ocm().PROVIDER_DENY_V156, [DENY]) + + +class TheInstallTakesItOutOfTheGlobalConfig(unittest.TestCase): + """install.sh runs it on the user's global config when that one already lists this box's providers, as + the copy of a fresh v1.56.0 to v1.59.0 install does, whichever major the box serves. The installer's + own lines, run as written.""" + + def block(self): + text = INSTALL.read_text() + start = text.index('OC_USER_CFG="$HOME/.config/opencode/opencode.json"\n# A box with no opencode config') + end = text.index("\nfi\n", text.index("block from $CONFIG_DIR/opencode.json into it", start)) + 4 + return text[start:end] + + def run_it(self, existing: str, serve_major: str): + home = pathlib.Path(tempfile.mkdtemp(prefix="oc-provider-deny-home-")) + cfg_dir = home / ".config" / "qwen38" + cfg_dir.mkdir(parents=True) + generated = the_copy() + del generated["policies"] + (cfg_dir / "opencode.json").write_text(json.dumps(generated, indent=2) + "\n") + user = home / ".config" / "opencode" / "opencode.json" + user.parent.mkdir(parents=True) + user.write_text(existing) + prelude = f'CONFIG_DIR="{cfg_dir}"; REPO_DIR="{REPO}"; OC_SERVE_MAJOR={serve_major}\n' + r = subprocess.run(["bash", "-c", "set -euo pipefail\n" + prelude + self.block()], + capture_output=True, text=True, timeout=120, + env={"HOME": str(home), "PATH": "/usr/bin:/bin"}) + return r.returncode, r.stdout + r.stderr, user + + def test_an_update_takes_the_copys_deny_out(self): + for serve_major in ("2", "1"): + with self.subTest(serve_major=serve_major): + rc, out, user = self.run_it(json.dumps(the_copy(), indent=2) + "\n", serve_major) + self.assertEqual(rc, 0, out) + doc = json.loads(user.read_text()) + self.assertNotIn("policies", doc) + self.assertIn("free models are listed again", out) + # the rest of the copy is kept + self.assertEqual((doc["model"], doc["small_model"], doc["share"], doc["websearch"], doc["update"]), + ("qwen38/qwen3.8-27b", "qwen38/qwen3.8-27b", "disabled", False, "disable")) + + def test_a_deny_of_the_persons_own_stays(self): + anthropic = {"action": "provider.use", "resource": "anthropic", "effect": "deny"} + rc, out, user = self.run_it(json.dumps(the_copy(policies=[anthropic, DENY]), indent=2) + "\n", "2") + self.assertEqual(rc, 0, out) + self.assertEqual(json.loads(user.read_text())["policies"], [anthropic, DENY]) + self.assertIn("rules of yours", out) + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/tests/test_uninstall_opencode.py b/tests/test_uninstall_opencode.py index c8682b48..1edaebbb 100644 --- a/tests/test_uninstall_opencode.py +++ b/tests/test_uninstall_opencode.py @@ -89,26 +89,32 @@ class UninstallLeavesOpencodeStartable(unittest.TestCase): spec = importlib.util.spec_from_file_location("ocm", REPO / "oc-merge-limits.py") ocm = importlib.util.module_from_spec(spec) spec.loader.exec_module(ocm) - # the values the purge counts as ours are the ones install.sh writes in the generated v2 config + # the values the purge counts as ours are the ones install.sh writes in the generated v2 config, + # and the deny on opencode's hosted provider that v1.56.0 to v1.59.0 wrote there too: a copy made + # then still holds it (decision 1595, 2026-11-25: no release writes it any more) install = (REPO / "install.sh").read_text() - for line in ('doc["policies"] = [{"action": "provider.use", "resource": "opencode", "effect": "deny"}]', - 'doc["share"] = "disabled"', 'doc["websearch"] = False', 'doc["update"] = "disable"'): + for line in ('doc["share"] = "disabled"', 'doc["websearch"] = False', 'doc["update"] = "disable"'): self.assertIn(line, install, "install.sh and the purge must agree on the v2 keys") - for websearch, gone in ((False, True), (True, False)): - with self.subTest(gone=gone): - mine = lambda k: json.dumps({ - "$schema": "https://opencode.ai/config.json", "provider": {"qwen38": box(k)}, - "compaction": {"keep": {"tokens": 135000}, "background": True}, - "model": "qwen38/qwen3.8-27b", "small_model": "qwen38/qwen3.8-27b", - "permission": RULES, "permissions": ocm.deny_permissions_v2(), - "policies": [{"action": "provider.use", "resource": "opencode", "effect": "deny"}], - "share": "disabled", "websearch": websearch, "update": "disable"}, indent=2) - out, home, cfg, oc = self.run_it(mine, purge=1) - self.assertEqual(oc.exists(), not gone, out) - if gone: - self.assertIn("held only this box's providers (qwen38): moved to", out) - else: - self.assertIs(json.loads(oc.read_text())["websearch"], True) + self.assertNotIn('doc["policies"]', install) + self.assertEqual(ocm.PROVIDER_DENY_V156, + [{"action": "provider.use", "resource": "opencode", "effect": "deny"}]) + for policies in (ocm.PROVIDER_DENY_V156, None): + for websearch, gone in ((False, True), (True, False)): + with self.subTest(policies=policies is not None, gone=gone): + doc = {"$schema": "https://opencode.ai/config.json", + "compaction": {"keep": {"tokens": 135000}, "background": True}, + "model": "qwen38/qwen3.8-27b", "small_model": "qwen38/qwen3.8-27b", + "permission": RULES, "permissions": ocm.deny_permissions_v2(), + "share": "disabled", "websearch": websearch, "update": "disable"} + if policies is not None: + doc["policies"] = policies + mine = lambda k: json.dumps({**doc, "provider": {"qwen38": box(k)}}, indent=2) + out, home, cfg, oc = self.run_it(mine, purge=1) + self.assertEqual(oc.exists(), not gone, out) + if gone: + self.assertIn("held only this box's providers (qwen38): moved to", out) + else: + self.assertIs(json.loads(oc.read_text())["websearch"], True) def test_the_copy_of_a_release_before_v157_goes_with_the_key_too(self): # up to v1.56.0 the copy held the shell rules alone ("bash*" in 1.x, three shell rules in 2.x): a box