Skip to content

Commit f963ea5

Browse files
test(obs): exercise the worker's obs path, don't just read its source
The two existing worker tests assert on `inspect.getsource(AgentexWorker.run)` — they pin that init_sgp_obs() and both drains are CALLED, which is worth pinning, but they cannot catch a call that is written correctly and then raises. So the claim "a Temporal worker starts fine without the broker token" rested on source inspection, not on the path having been run. That matters because a build with no broker token produces an image with no sgp-obs in it, and for a Temporal agent the model calls happen in the worker process — the one this PR newly touches. Now exercised for real with the import blocked: the module imports, an AgentexWorker constructs, init_sgp_obs() returns not_installed, and both drains complete with nothing ever wired. Verified by hand first, at which point it passed — so this is a guard rather than a fix. Keeps the source-inspection tests alongside it: one proves the wiring exists, the other proves it is harmless. Neither alone is enough. 137 tests, ruff clean, pyright 0 errors, all with sgp-obs absent. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 8be24ed commit f963ea5

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

‎src/agentex/lib/sdk/fastacp/base/tests/test_shutdown_hooks.py‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@
1616
)
1717

1818

19+
def _block_sgp_obs_import(monkeypatch):
20+
"""Make `import sgp_obs` fail, i.e. the image a tokenless build produces."""
21+
import sys
22+
import builtins
23+
24+
monkeypatch.delitem(sys.modules, "sgp_obs", raising=False)
25+
real_import = builtins.__import__
26+
27+
def blocked(name, *args, **kwargs):
28+
if name == "sgp_obs" or name.startswith("sgp_obs."):
29+
raise ImportError("No module named 'sgp_obs'")
30+
return real_import(name, *args, **kwargs)
31+
32+
monkeypatch.setattr(builtins, "__import__", blocked)
33+
34+
1935
class _Processor:
2036
def __init__(self, explode: bool = False) -> None:
2137
self.calls = 0
@@ -247,3 +263,40 @@ def shutdown(self):
247263
f"process took {elapsed:.1f}s to exit with a 30s stalled flush and a "
248264
"0.25s budget; the flush thread is blocking interpreter shutdown"
249265
)
266+
267+
268+
class TestTheWorkerObsPathRunsWithoutSgpObs:
269+
"""The image a build with NO broker token produces has no sgp-obs in it, and a
270+
Temporal agent's model calls happen in this process.
271+
272+
The two tests above pin that ``run()`` *calls* these, by reading its source. That
273+
cannot catch a call that is written correctly and then raises, so this exercises the
274+
sequence for real. Together: one proves the wiring exists, the other proves it is
275+
harmless.
276+
"""
277+
278+
def test_the_worker_module_imports_and_constructs(self):
279+
from agentex.lib.core.temporal.workers.worker import AgentexWorker
280+
281+
# port 0 so nothing binds a real health port during the test
282+
assert AgentexWorker(task_queue="probe", health_check_port=0) is not None
283+
284+
async def test_init_and_both_drains_are_inert(self, monkeypatch):
285+
"""Exactly what ``run()`` does: init at entry, both drains in its finally —
286+
with nothing wired, which is every agent that has not adopted."""
287+
from agentex.lib.core.observability import sgp_obs_setup
288+
from agentex.lib.core.observability.sgp_obs_setup import (
289+
init_sgp_obs,
290+
shutdown_sgp_obs,
291+
)
292+
293+
monkeypatch.delenv("SGP_OBS_ENABLED", raising=False)
294+
sgp_obs_setup._reset_for_tests()
295+
try:
296+
_block_sgp_obs_import(monkeypatch)
297+
assert init_sgp_obs() == "not_installed"
298+
# Neither drain may raise just because nothing was ever wired.
299+
await shutdown_sync_tracing_processors()
300+
await shutdown_sgp_obs()
301+
finally:
302+
sgp_obs_setup._reset_for_tests()

0 commit comments

Comments
 (0)