fix(svc-ai-agent-broker): retry a swarm service update that swarm moved out of sequence

Main run 45819530411 GitLab#3 (swarm, arm64, centos) failed the svc-ai-agent-broker CLI tests: the broker logged agent_failed 'POST /services/<id>/update -> 500: rpc error: code = Unknown desc = update out of sequence' when it started the hermes and openclaw agents of one owner. Swarm bumps a service's version on its own right after a create, so the scale-up built on the version read a moment earlier was refused. SwarmBackend._update now re-reads the spec and retries only on that error, at most UPDATE_ATTEMPTS times; ensure_pool and _scale both use it. New unit tests cover the retry, a different engine error that is not retried, and a version that never settles; the retry test fails on the old code with the CI error text.

🎯 Fix: 85 %
💥 Break: 3 %
💸 Tokens: 100 %
⏱️ Velocity: 99 %
🛡️ Security: 100 %
🏆 Score: 83
🚦 Gate: ⛔ fix 85 % < 90

Created-with: https://s.veen.world/skills
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Opus 5.5 committed 2026-11-26 19:44:44 +00:00
1 parent 6d48a2fb39
commit 2d75e2dcc7
2 files changed
+122 -38

No files matched your search

@@ -209,6 +209,62 @@ class TestSwarmBackendPool(unittest.TestCase):
)
class RacingEngine:
"""
Args:
refusals: how many updates swarm refuses before it takes one.
error: what a refused update answers.
"""
def __init__(self, refusals, error):
self.version = 1
self.refusals = refusals
self.error = error
self.updates = []
def call(self, _method, path, *_args, query=None, **_kwargs):
if path.endswith("/update"):
self.updates.append(query["version"])
if query["version"] != self.version:
raise AssertionError(f"update sent stale version {query['version']}")
if self.refusals:
self.refusals -= 1
self.version += 1
raise engine.EngineError(
f"POST {path} -> 500: rpc error: code = Unknown desc = {self.error}",
500,
)
return None
return {
"ID": "svc-1",
"Spec": {
"Name": "agent-hermes-u1",
"Mode": {"Replicated": {"Replicas": 0}},
"TaskTemplate": {"Networks": []},
},
"Version": {"Index": self.version},
}
class TestSwarmBackendUpdate(unittest.TestCase):
def test_a_start_survives_swarm_moving_the_version_after_a_create(self):
fake = RacingEngine(refusals=1, error="update out of sequence")
swarm.SwarmBackend(fake, "c", "p", 1).start({"ID": "svc-1"}, "net-p-0")
self.assertEqual(fake.updates, [1, 2])
def test_another_engine_error_is_not_retried(self):
fake = RacingEngine(refusals=1, error="no such image")
with self.assertRaises(engine.EngineError):
swarm.SwarmBackend(fake, "c", "p", 1).start({"ID": "svc-1"}, "net-p-0")
self.assertEqual(fake.updates, [1])
def test_a_version_that_never_settles_gives_up(self):
fake = RacingEngine(refusals=1000, error="update out of sequence")
with self.assertRaises(engine.EngineError):
swarm.SwarmBackend(fake, "c", "p", 1).start({"ID": "svc-1"}, "net-p-0")
self.assertLess(len(fake.updates), 10)
class TestSwarmBackend(unittest.TestCase):
def test_a_service_is_placed_on_the_sandbox_nodes_and_starts_scaled_down(self):
fake = RecordingEngine()