# Conflicts: # sidecar/labsync.py # sidecar/routes_runs.py # sidecar/test_harness_loader.py
205 lines
10 KiB
Python
205 lines
10 KiB
Python
"""Lab sync's harness / provider bundles (workers/labsync.py
|
|
``plugin_bundles`` / ``with_plugins``): what a worker gets of the materialized
|
|
live plugin dirs, and that a guest's computer gets only guestAllowed harnesses
|
|
and no provider usage reader. ``python -m unittest test_labsync_plugins``."""
|
|
|
|
import base64
|
|
import json
|
|
import pathlib
|
|
import tempfile
|
|
import unittest
|
|
from unittest import mock
|
|
|
|
from workers import labsync
|
|
|
|
|
|
def _plugin(root: pathlib.Path, pid: str, kind: str, cid: str, guest: bool = False) -> None:
|
|
d = root / pid
|
|
(d / "sidecar").mkdir(parents=True)
|
|
obj = ({"harnessId": cid, "guestAllowed": guest, "sidecar": {"entry": "driver.py"}}
|
|
if kind == "harness" else {"vendorId": cid, "usageReader": {"entry": "usage.py"}})
|
|
(d / "plugin.json").write_text(json.dumps({"id": pid, "kind": kind, kind: obj}))
|
|
(d / "sidecar" / ("driver.py" if kind == "harness" else "usage.py")).write_text("# code\n")
|
|
(d / "sidecar" / "__pycache__").mkdir()
|
|
(d / "sidecar" / "__pycache__" / "x.pyc").write_bytes(b"junk")
|
|
(d / ".env").write_text("SECRET=1")
|
|
idx_p = root / "index.json"
|
|
key = "harnesses" if kind == "harness" else "providers"
|
|
idx = json.loads(idx_p.read_text()) if idx_p.exists() else {key: {}}
|
|
idx[key][cid] = {"pluginId": pid, "dir": pid, "guestAllowed": guest}
|
|
idx_p.write_text(json.dumps(idx))
|
|
|
|
|
|
class PluginBundlesTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tmp = tempfile.TemporaryDirectory()
|
|
t = pathlib.Path(self.tmp.name)
|
|
self.h, self.p = t / "live-harnesses", t / "live-providers"
|
|
_plugin(self.h, "harness-claude", "harness", "claude", guest=True)
|
|
_plugin(self.h, "harness-opencode", "harness", "opencode")
|
|
_plugin(self.p, "provider-anthropic", "provider", "anthropic")
|
|
self.patches = [mock.patch.object(labsync, "LIVE_HARNESSES_DIR", self.h),
|
|
mock.patch.object(labsync, "LIVE_PROVIDERS_DIR", self.p),
|
|
mock.patch.object(labsync, "plugin_store", lambda: None)]
|
|
for p in self.patches:
|
|
p.start()
|
|
labsync._plugin_bundles.clear()
|
|
|
|
def tearDown(self):
|
|
for p in self.patches:
|
|
p.stop()
|
|
self.tmp.cleanup()
|
|
|
|
def test_an_owner_worker_gets_every_harness_and_provider(self):
|
|
b = labsync.plugin_bundles()
|
|
self.assertEqual(sorted(b["harnesses"]), ["harness-claude", "harness-opencode"])
|
|
self.assertEqual(sorted(b["providers"]), ["provider-anthropic"])
|
|
files = b["harnesses"]["harness-claude"]["files"]
|
|
self.assertEqual(sorted(files), ["plugin.json", "sidecar/driver.py"]) # no .env, no pyc
|
|
self.assertEqual(base64.b64decode(files["sidecar/driver.py"]), b"# code\n")
|
|
|
|
def test_a_guest_computer_gets_guest_allowed_harnesses_only(self):
|
|
b = labsync.plugin_bundles(guest_only=True)
|
|
self.assertEqual(list(b["harnesses"]), ["harness-claude"])
|
|
self.assertEqual(b["providers"], {})
|
|
|
|
def test_the_hash_moves_with_the_plugins_and_not_without(self):
|
|
base = {"hash": "abc", "skills": {}, "agents": {}, "executables": [], "skipped": []}
|
|
# nothing to add: both maps still go (``{}`` clears a worker), same hash
|
|
empty = labsync.with_plugins(base, {"harnesses": {}, "providers": {}})
|
|
self.assertEqual(empty, {**base, "harnesses": {}, "providers": {}, "hooks": {}})
|
|
self.assertEqual(empty["hash"], "abc")
|
|
b1 = labsync.with_plugins(base, labsync.plugin_bundles())
|
|
self.assertNotEqual(b1["hash"], "abc")
|
|
self.assertIn("harnesses", b1)
|
|
(self.h / "harness-opencode" / "sidecar" / "driver.py").write_text("# v2\n")
|
|
b2 = labsync.with_plugins(base, labsync.plugin_bundles())
|
|
self.assertNotEqual(b1["hash"], b2["hash"])
|
|
|
|
def test_removing_every_harness_still_sends_empty_maps(self):
|
|
with mock.patch.object(labsync, "items_for", lambda w, source=None: []):
|
|
had = labsync.for_worker({"id": "w1"})
|
|
(self.h / "index.json").write_text(json.dumps({"harnesses": {}}))
|
|
(self.p / "index.json").write_text(json.dumps({"providers": {}}))
|
|
labsync._plugin_bundles.clear()
|
|
now = labsync.for_worker({"id": "w1"})
|
|
self.assertEqual((now["harnesses"], now["providers"]), ({}, {}))
|
|
self.assertNotEqual(now["hash"], had["hash"]) # the worker is re-synced
|
|
|
|
def test_a_guest_never_gets_a_harness_its_manifest_doesnt_allow(self):
|
|
# the index says guestAllowed, the plugin's own plugin.json doesn't
|
|
idx = json.loads((self.h / "index.json").read_text())
|
|
idx["harnesses"]["opencode"]["guestAllowed"] = True
|
|
(self.h / "index.json").write_text(json.dumps(idx))
|
|
self.assertEqual(list(labsync.plugin_bundles(guest_only=True)["harnesses"]),
|
|
["harness-claude"])
|
|
|
|
def test_a_store_that_can_say_is_asked_first(self):
|
|
class Store:
|
|
def sidecar_bundles(self, guest_only=False):
|
|
return {"harnesses": {"harness-x": {"hash": "1", "files": {}}},
|
|
"providers": {"provider-y": {"hash": "2", "files": {}}}}
|
|
with mock.patch.object(labsync, "plugin_store", lambda: Store()):
|
|
self.assertEqual(list(labsync.plugin_bundles()["harnesses"]), ["harness-x"])
|
|
guest = labsync.plugin_bundles(guest_only=True)
|
|
self.assertEqual(guest["providers"], {})
|
|
self.assertEqual(guest["harnesses"], {}) # it doesn't say guestAllowed
|
|
|
|
def test_for_worker_carries_them_and_push_sends_them(self):
|
|
with mock.patch.object(labsync, "items_for", lambda w, source=None: []):
|
|
owner = labsync.for_worker({"id": "w1"})
|
|
guest = labsync.for_worker({"id": "g1", "owner": "dom"})
|
|
self.assertEqual(sorted(owner["providers"]), ["provider-anthropic"])
|
|
self.assertEqual(list(guest["harnesses"]), ["harness-claude"])
|
|
self.assertEqual(guest.get("providers"), {})
|
|
sent = {}
|
|
|
|
def fake_request(url, token, path, body, **kw):
|
|
sent.update(body)
|
|
return {"labSync": {"hash": body["hash"]}}
|
|
|
|
store = mock.Mock()
|
|
with mock.patch("workers.store.sidecar_request", fake_request):
|
|
labsync.push(store, {"id": "w1", "url": "u", "token": "t"}, owner)
|
|
self.assertIn("harnesses", sent)
|
|
self.assertIn("providers", sent)
|
|
|
|
def test_hidden_files_are_never_shipped(self):
|
|
"""The worker refuses a hidden path part and fails the whole /sync on
|
|
it — harness-pi's runner/.gitignore kept every owner worker off its
|
|
harness plugins (2026-11-26)."""
|
|
d = self.h / "harness-opencode"
|
|
(d / "runner").mkdir()
|
|
(d / "runner" / ".gitignore").write_text("node_modules\n")
|
|
(d / "runner" / "cli.mjs").write_text("// cli\n")
|
|
(d / ".hidden").mkdir()
|
|
(d / ".hidden" / "x.txt").write_text("x")
|
|
files = labsync.plugin_bundles()["harnesses"]["harness-opencode"]["files"]
|
|
self.assertIn("runner/cli.mjs", files)
|
|
self.assertNotIn("runner/.gitignore", files)
|
|
self.assertFalse([r for r in files if any(p.startswith(".") for p in r.split("/"))])
|
|
|
|
|
|
class PluginDriftTest(unittest.TestCase):
|
|
"""A worker that holds the bundle's hash but not its plugin maps is
|
|
outdated: labsync.status re-sends, the worker re-applies the maps."""
|
|
|
|
def setUp(self):
|
|
self.bundle = {"hash": "abcd1234",
|
|
"harnesses": {"harness-claude": {"hash": "h1", "files": {}}},
|
|
"providers": {"provider-anthropic": {"hash": "p1", "files": {}}}}
|
|
self.held = {"status": "online", "labSync": {"hash": "abcd1234"}}
|
|
|
|
def _sync(self, h=None, p=None, loaded=True):
|
|
h = labsync.plugins_hash(self.bundle["harnesses"]) if h is None else h
|
|
p = labsync.plugins_hash(self.bundle["providers"]) if p is None else p
|
|
return {"harnesses": {"version": h, "loaded": loaded},
|
|
"providers": {"version": p, "loaded": True}}
|
|
|
|
def test_held_and_run_is_current(self):
|
|
st = {**self.held, "pluginSync": self._sync()}
|
|
self.assertEqual(labsync.status(st, self.bundle), labsync.CURRENT)
|
|
|
|
def test_the_hash_without_its_maps_is_outdated(self):
|
|
for ps in ({"harnesses": {"version": None, "loaded": None},
|
|
"providers": {"version": None, "loaded": None}}, # the outage
|
|
self._sync(h="0" * 16), # another version
|
|
self._sync(p="0" * 16), # its readers' too
|
|
self._sync(loaded=False)): # held, not run
|
|
st = {**self.held, "pluginSync": ps}
|
|
self.assertEqual(labsync.status(st, self.bundle), labsync.OUTDATED, ps)
|
|
st = {**self.held, "pluginSync": self._sync(h="0" * 16), "labSyncError": "boom"}
|
|
self.assertEqual(labsync.status(st, self.bundle), labsync.FAILED)
|
|
|
|
def test_a_sidecar_before_plugin_sync_is_read_off_its_harnesses(self):
|
|
st = {**self.held, "liveHarnesses": []}
|
|
self.assertEqual(labsync.status(st, self.bundle), labsync.OUTDATED)
|
|
st = {**self.held, "liveHarnesses": [{"id": "claude", "source": "plugin", "hash": "h1"}]}
|
|
self.assertEqual(labsync.status(st, self.bundle), labsync.CURRENT)
|
|
|
|
def test_a_sidecar_before_the_plugin_maps_is_never_re_sent(self):
|
|
self.assertEqual(labsync.status(self.held, self.bundle), labsync.CURRENT)
|
|
|
|
def test_a_guest_computer_compares_no_provider_map(self):
|
|
b = {**self.bundle, "providers": {}}
|
|
st = {**self.held, "pluginSync": {
|
|
"harnesses": {"version": labsync.plugins_hash(b["harnesses"]), "loaded": True},
|
|
"providers": {"version": labsync.plugins_hash({}), "loaded": True}}}
|
|
self.assertEqual(labsync.status(st, b), labsync.CURRENT)
|
|
|
|
def test_the_hash_matches_the_workers(self):
|
|
side = pathlib.Path(__file__).resolve().parent.parent / "sidecar" / "labsync.py"
|
|
if not side.is_file():
|
|
self.skipTest("no sidecar checkout beside the backend")
|
|
src = side.read_text()
|
|
start = src.index("def _plugins_hash")
|
|
ns: dict = {}
|
|
exec(src[start:src.index("\n\n\n", start)], ns) # the function alone
|
|
m = {"b": {"hash": "2"}, "a": {"hash": "1"}}
|
|
self.assertEqual(ns["_plugins_hash"](m), labsync.plugins_hash(m))
|
|
self.assertEqual(ns["_plugins_hash"]({}), labsync.plugins_hash({}))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|