Files
ai-agent/backend/test_worker_updates.py
Gabriel VidalandClaude Opus 5.5 98b1fe85e9 fix(workers): harness plugins reach every worker — no more "invalid harness: 'claude'"
Since the strict plugin-only sidecar (Wave 3C) a worker runs only the
harness plugins the hub syncs to it, and every worker came up with none:

- the probe that asked a worker to update also pushed it the new bundle;
  the old sidecar took the bundle's hash and dropped the harness/provider
  maps it didn't know, then restarted onto the strict code empty — and the
  hub saw the hash "current" forever (a same-hash /sync was a no-op too);
- harness-pi ships runner/.gitignore, which the worker refuses, so every
  later /sync of the owner's workers failed whole (400 bad file path).

Hub: plugin bundles never ship a path the worker would refuse (hidden
parts, > 8 parts); labsync.plugins_drift makes a worker that holds the
bundle's hash without its maps "outdated", read off /health's new
pluginSync (or its harnesses list on an older sidecar); no lab-sync push
in the probe that triggers an update, nor while the worker is updating.

Sidecar: /health reports pluginSync ({kind: {version, loaded}}) and the
loaded usage readers; /sync re-applies plugin maps the worker doesn't
hold or run even under a hash it already has (labsync.plugins_stale);
a well-formed harness no driver runs is a 409 harness_unavailable on
spawn/resume/fork (only a malformed id stays a 400), and a worker with no
harness at all says its lab sync hasn't brought them yet.

Also fixes test_guest_computers' lab-sync test, broken on main since the
plugin maps (its stubbed bundle is a list).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 19:17:45 +01:00

153 lines
6.7 KiB
Python

"""Workers follow the hub's version: update_status against the deployed
commit, the probe asking a worker that is behind to update, the retry delay,
and a worker ahead of the hub left alone. ``python -m unittest
test_worker_updates`` from ``backend/``."""
import pathlib
import subprocess
import tempfile
import time
import unittest
import workers
from workers import updates
from _fakeworker import FakeWorker
def _git(repo, *args) -> str:
return subprocess.run(["git", "-C", str(repo), *args], check=True,
capture_output=True, text=True).stdout.strip()
class WorkerUpdates(unittest.TestCase):
def setUp(self):
self.tmp = pathlib.Path(tempfile.mkdtemp())
# A source checkout with history old → hub → new, for the ancestry.
repo = self.tmp / "src"
repo.mkdir()
_git(repo, "init", "-q")
shas = []
for n in ("old", "hub", "new"):
(repo / "f").write_text(n)
_git(repo, "add", "f")
_git(repo, "-c", "user.name=t", "-c", "user.email=t@t", "commit",
"-qm", n)
shas.append(_git(repo, "rev-parse", "HEAD"))
self.old, self.hub, self.new = shas
self.deployed = self.tmp / "deployed-commit"
self.deployed.write_text(self.hub + "\n")
self._saved = (updates.DEPLOYED_COMMIT_FILE, updates.SRC_REPO)
updates.DEPLOYED_COMMIT_FILE, updates.SRC_REPO = str(self.deployed), str(repo)
updates._hub[:] = [None, None]
updates._ancestry.clear()
updates._attempts.clear()
self.fw = FakeWorker(self.tmp / "projects", self.tmp / "state")
self.store = workers.WorkerStore(str(self.tmp / "workers.json"))
self.w = workers.pair(self.store, self.fw.pairing_string("work"),
account=None, name=None, hub_name="t",
resolve_login=lambda _: None,
valid_account=lambda a: a if a == "work" else None)
def tearDown(self):
self.fw.stop()
updates.DEPLOYED_COMMIT_FILE, updates.SRC_REPO = self._saved
def _probe(self):
workers.probe(self.store, self.store.get(self.w["id"]))
return self.store.public(self.store.get(self.w["id"]))
def _updates(self):
return [b for p, b in self.fw.calls if p == "/update"]
def test_a_worker_from_before_auto_update_is_unsupported(self):
pub = self._probe()
self.assertEqual(pub["updateStatus"], "unsupported")
self.assertEqual(self._updates(), [])
def test_a_worker_behind_is_asked_to_move_to_the_hubs_commit(self):
self.fw.commit, self.fw.updatable = self.old, True
pub = self._probe()
self.assertEqual(self._updates(), [{"commit": self.hub}])
self.assertEqual(pub["updateStatus"], "updating")
self.assertEqual(pub["hubCommit"], self.hub)
# it moved: current, and nothing more is asked
self.fw.commit = self.hub
self.fw.update_state = {"state": "done", "from": self.old, "to": self.hub}
self.assertEqual(self._probe()["updateStatus"], "current")
self.assertEqual(len(self._updates()), 1)
def test_no_second_request_inside_the_retry_delay(self):
self.fw.commit, self.fw.updatable = self.old, True
self._probe()
self.fw.update_state = {"state": "interrupted", "from": self.old,
"to": self.hub}
self.assertEqual(self._probe()["updateStatus"], "outdated")
self.assertEqual(len(self._updates()), 1)
updates._attempts[self.w["id"]] = (self.hub, time.time() - updates.RETRY_S - 1)
self._probe()
self.assertEqual(len(self._updates()), 2)
def test_no_lab_sync_push_while_the_worker_restarts_onto_new_code(self):
"""The probe that asks a worker to update sends it no bundle, nor do
the ones while it is updating: the old sidecar would take the
bundle's hash and drop what it doesn't know (the harness plugin maps
— the 2026-11-26 outage). The probe after it moved sends it."""
from unittest import mock
from workers import labsync
bundle = {"hash": "beefbeef", "skills": {}, "agents": {}, "executables": [],
"harnesses": {}, "providers": {}}
labsync._attempts.clear()
self.fw.commit, self.fw.updatable = self.old, True
self.fw.lab_sync = {"hash": "0ld0ld00", "skills": [], "agents": [], "conflicts": []}
syncs = lambda: [b for p, b in self.fw.calls if p == "/sync"] # noqa: E731
with mock.patch.object(labsync, "for_worker", lambda w, source=None: bundle):
self._probe()
self.assertEqual(len(self._updates()), 1)
self.assertEqual(syncs(), [])
self._probe() # still updating
self.assertEqual(syncs(), [])
self.fw.commit = self.hub
self.fw.update_state = {"state": "done", "from": self.old, "to": self.hub}
self._probe()
self.assertEqual([b["hash"] for b in syncs()], ["beefbeef"])
def test_a_failed_target_waits_longer(self):
self.fw.commit, self.fw.updatable = self.old, True
self._probe()
self.fw.update_state = {"state": "rolledback", "from": self.old,
"to": self.hub, "error": "didn't come up"}
updates._attempts[self.w["id"]] = (self.hub, time.time() - updates.RETRY_S - 1)
self.assertEqual(self._probe()["updateStatus"], "failed")
self.assertEqual(len(self._updates()), 1)
def test_a_worker_ahead_of_the_hub_is_left_alone(self):
self.fw.commit, self.fw.updatable = self.new, True
self.assertEqual(self._probe()["updateStatus"], "ahead")
self.assertEqual(self._updates(), [])
def test_auto_update_off_only_reports(self):
self.fw.commit, self.fw.updatable = self.old, True
self.store.patch(self.w["id"], {"autoUpdate": False})
pub = self._probe()
self.assertEqual(pub["updateStatus"], "outdated")
self.assertFalse(pub["autoUpdate"])
self.assertEqual(self._updates(), [])
updates.trigger(self.store, self.store.get(self.w["id"]), self.hub)
self.assertEqual(self._updates(), [{"commit": self.hub}])
def test_a_dev_checkout_is_not_updatable(self):
self.fw.commit, self.fw.updatable = self.old, False
self.assertEqual(self._probe()["updateStatus"], "unsupported")
self.assertEqual(self._updates(), [])
def test_no_deployed_commit_means_unknown_and_no_update(self):
self.deployed.unlink()
self.fw.commit, self.fw.updatable = self.old, True
self.assertEqual(self._probe()["updateStatus"], "unknown")
self.assertEqual(self._updates(), [])
if __name__ == "__main__":
unittest.main()