From 31146c1d3aa5a43b1dcdae59e62cbe565cbdc033 Mon Sep 17 00:00:00 2001 From: "tbhb.dev Agent" Date: Wed, 25 Nov 2026 07:39:43 +0000 Subject: [PATCH] feat: add a tested Temporal workflow skeleton Generated-by: LLM effort=high;harness=codex:0.171.0;model=openai:gpt-6.1-sol;session=sha-256-64:dfcf609669cdcf62;surface=exec --- .cspell-words.txt | 2 + BUILD.bazel | 4 ++ pyproject.toml | 5 +- src/tbhbgraph/temporal.py | 33 ++++++++++ tests/test_temporal.py | 128 ++++++++++++++++++++++++++++++++++++++ uv.lock | 63 ++++++++++++++++++- 6 files changed, 233 insertions(+), 2 deletions(-) create mode 100644 src/tbhbgraph/temporal.py create mode 100644 tests/test_temporal.py diff --git a/.cspell-words.txt b/.cspell-words.txt index 6dd8702..6731c02 100644 --- a/.cspell-words.txt +++ b/.cspell-words.txt @@ -10,6 +10,7 @@ Cocogitto complexipy cpus cuda +defn deptry docstrings dunder @@ -45,6 +46,7 @@ starlark tbhb tbhbdev tbhbgraph +temporalio tera testdata testpaths diff --git a/BUILD.bazel b/BUILD.bazel index 6b8e6df..9cd08bc 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -184,8 +184,12 @@ pytest_test( args = [ "-n", "auto", + "-rP", + "--log-level=INFO", ], coverage = ["src/tbhbgraph"], + env = {"TEMPORAL_BINARY": "temporal"}, + path_tools = [":temporal"], ) # The advisory database changes without any input changing: pip_audit_test diff --git a/pyproject.toml b/pyproject.toml index ee45187..ff98953 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,7 +8,10 @@ version = "0" description = "Graph plugin for tbhbdev" requires-python = ">=3.15" # tbhbdev loads this package; the package never imports tbhbdev. -dependencies = ["click>=8.3.0"] +dependencies = [ + "click>=8.3.0", + "temporalio>=1.36.1", +] # tbhbdev reads its plugins from this group (tbhbdev/plugins.py). [project.entry-points."tbhbdev.plugins"] diff --git a/src/tbhbgraph/temporal.py b/src/tbhbgraph/temporal.py new file mode 100644 index 0000000..7c3644d --- /dev/null +++ b/src/tbhbgraph/temporal.py @@ -0,0 +1,33 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright Tony Burns + +"""A minimal echo workflow and its worker.""" + +from datetime import timedelta + +from temporalio import activity, workflow +from temporalio.client import Client +from temporalio.worker import Worker + + +@activity.defn +async def echo(value: str) -> str: + return value + + +# The plugin package imports Click, which uses restricted gettext calls. +@workflow.defn(sandboxed=False) +class PingWorkflow: + @workflow.run + async def run(self, value: str) -> str: + return await workflow.execute_activity( + echo, value, start_to_close_timeout=timedelta(seconds=10) + ) + + +async def run_worker(address: str, task_queue: str) -> None: + client = await Client.connect(address) + worker = Worker( + client, task_queue=task_queue, workflows=[PingWorkflow], activities=[echo] + ) + await worker.run() diff --git a/tests/test_temporal.py b/tests/test_temporal.py new file mode 100644 index 0000000..f98a379 --- /dev/null +++ b/tests/test_temporal.py @@ -0,0 +1,128 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright Tony Burns + +import asyncio +import logging +import os +import shutil +import socket +import subprocess +import sys +import time +from contextlib import suppress +from datetime import timedelta +from typing import TYPE_CHECKING + +import pytest +from temporalio.client import Client + +from tbhbgraph.temporal import PingWorkflow, run_worker + +if TYPE_CHECKING: + from collections.abc import Iterator + from pathlib import Path + + +_LOG = logging.getLogger(__name__) + + +@pytest.fixture +def temporal_address(tmp_path: Path) -> Iterator[str]: + binary = shutil.which(os.environ["TEMPORAL_BINARY"]) + assert binary is not None + for port in range(17233, 17333): + with socket.socket() as connection: + connection.settimeout(1) + if connection.connect_ex(("127.0.0.1", port)) != 0: + break + else: + pytest.fail("No free Temporal port found") + + address = f"127.0.0.1:{port}" + with (tmp_path / "temporal-server.log").open("w+") as server_log: + process = subprocess.Popen( + [ + binary, + "server", + "start-dev", + "--headless", + "--ip", + "127.0.0.1", + "--port", + str(port), + "--dynamic-config-value", + "frontend.enableServerVersionCheck=false", + ], + stdout=server_log, + stderr=subprocess.STDOUT, + ) + try: + deadline = time.monotonic() + 30 + while time.monotonic() < deadline: + if process.poll() is not None: + server_log.seek(0) + pytest.fail(f"Temporal dev server exited: {server_log.read()}") + health = subprocess.run( + [ + binary, + "operator", + "cluster", + "health", + "--address", + address, + "--command-timeout", + "1s", + ], + capture_output=True, + text=True, + check=False, + timeout=2, + ) + if health.returncode == 0: + _LOG.info("Started Temporal dev server; cluster is healthy") + break + time.sleep(1) + else: + server_log.seek(0) + pytest.fail(f"Temporal health timed out: {server_log.read()}") + yield address + finally: + process.terminate() + try: + process.wait(timeout=10) + except subprocess.TimeoutExpired: + process.kill() + process.wait() + pytest.fail( + "Temporal dev server leaked after shutdown; killed and reaped" + ) + _LOG.info("Stopped and reaped Temporal dev server") + + +async def _execute_ping(address: str) -> None: + task_queue = "test-ping" + worker = asyncio.create_task(run_worker(address, task_queue)) + try: + client = await Client.connect(address) + result = await client.execute_workflow( + PingWorkflow.run, + "ping", + id="test-ping", + task_queue=task_queue, + execution_timeout=timedelta(seconds=15), + rpc_timeout=timedelta(seconds=20), + ) + assert result == "ping" + _LOG.info("Ping workflow echoed its input through the activity") + finally: + worker.cancel() + with suppress(asyncio.CancelledError): + await worker + + +@pytest.mark.skipif( + sys.platform == "win32", + reason="Tech debt: pytest dev-server test skipped on Windows (same gap as PR #5)", +) +def test_ping_workflow(temporal_address: str) -> None: + asyncio.run(_execute_ping(temporal_address)) diff --git a/uv.lock b/uv.lock index f265595..46a80a2 100644 --- a/uv.lock +++ b/uv.lock @@ -462,6 +462,18 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/b5/99/7b1adac7e97ce7bbda34792dc6e9b2f5cc5cffd1875883f78a818b2a2687/mypy_extensions-1.2.0-py3-none-any.whl", hash = "sha256:993e7a20aa5671eb41a7132cf555af30062da99729601601b9a8382b11cae768", size = 5114, upload-time = "2026-11-03T16:26:32.731Z" }, ] +[[package]] +name = "nexus-rpc" +version = "1.5.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "typing-extensions" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/25/e5/25be948c92492cfc8ac24a524ad8828fe0cb9f71f46a974d7a37f0794a26/nexus_rpc-1.5.0.tar.gz", hash = "sha256:be2d6934414677bc9dd8e571fb5106c22b8769edc19562c3f49d9988edd24e1c", size = 97404, upload-time = "2026-10-28T17:12:21.82Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/33/e2/00541e7997f3dfb294f7be3dafea3307ea09b9d3fd332054fe4d30627efc/nexus_rpc-1.5.0-py3-none-any.whl", hash = "sha256:5d6e856972be812573948df02da2257344c407a8eaa99977b5d38be54448305a", size = 30406, upload-time = "2026-10-28T17:12:20.245Z" }, +] + [[package]] name = "packageurl-python" version = "0.17.6" @@ -553,6 +565,21 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/40/9e/2b38731e0fc536806f16490e1a12d7f0dc2a1235aa8cc07bcc75416a7daa/pluggy-1.7.0-py3-none-any.whl", hash = "sha256:7dd7b0d8832ba3cb632c306926ded123429211b83641b35dc5c41ad2d34f9bec", size = 27082, upload-time = "2026-10-15T09:50:56.808Z" }, ] +[[package]] +name = "protobuf" +version = "7.36.2" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/d9/89/5b8517baa72f84a67b8a307ba953c91057af618bf40bf676f3c03551f8f0/protobuf-7.36.2.tar.gz", hash = "sha256:497d0463ff3316681da6c0b9e8d06cb465d61abce00b613ab42226175644d1bb", size = 512737, upload-time = "2026-09-17T20:07:59.326Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/32/72/98342feb672507c8f3a69e34b4fa8961f608edba5c1a48a6f47156d92cb5/protobuf-7.36.2-cp310-abi3-macosx_10_9_universal2.whl", hash = "sha256:cbc70b17ee27e28894c7fee8bb04be1abead49e936bc70eb60052531eee2079e", size = 456039, upload-time = "2026-09-17T20:07:51.542Z" }, + { url = "https://files.pythonhosted.org/packages/b6/ea/91fdf7c2b8bbd49cde056f00a9df6773532987e1c00fe2830b895af95c7e/protobuf-7.36.2-cp310-abi3-manylinux2014_aarch64.whl", hash = "sha256:e11e1f0180583a2af89db6a2ecd9e8dc40aa6d2988ca175bfd0e6d12ea72d74e", size = 344219, upload-time = "2026-09-17T20:07:52.914Z" }, + { url = "https://files.pythonhosted.org/packages/17/ab/5fd5f8ece73fad885c5a09aa849b32d70472f954ba3a92d3bb5974ea953b/protobuf-7.36.2-cp310-abi3-manylinux2014_s390x.whl", hash = "sha256:f4fee11ec330d238b34a05c9b675f693c20415d1c5bd7d5320cc2f8a798eb9cf", size = 357223, upload-time = "2026-09-17T20:07:53.985Z" }, + { url = "https://files.pythonhosted.org/packages/db/f3/3996583dd2906297a637af12114deddf7658af6e683fedb83be061983fb5/protobuf-7.36.2-cp310-abi3-manylinux2014_x86_64.whl", hash = "sha256:89f23aa53c24553a2416fd4fd1ec06f74fa42b14b546d8883128813f775bbfd2", size = 343223, upload-time = "2026-09-17T20:07:54.931Z" }, + { url = "https://files.pythonhosted.org/packages/fc/1b/dcc64f358fcb51811b58ae40b3d28f820725f116d86487cc20bd4b130701/protobuf-7.36.2-cp310-abi3-win32.whl", hash = "sha256:912c1221170e16c08d1f086762f563dd61ff83c18b5fa6652952dfaded66f728", size = 442998, upload-time = "2026-09-17T20:07:55.826Z" }, + { url = "https://files.pythonhosted.org/packages/8a/55/b77bda4e5e5f5971fb51b07663694690e9afdb9402136c16a522bd621cad/protobuf-7.36.2-cp310-abi3-win_amd64.whl", hash = "sha256:a300819d441e078a5608c0d3c709796bb548136058fda017ae51d425b44fd353", size = 456514, upload-time = "2026-09-17T20:07:57.188Z" }, + { url = "https://files.pythonhosted.org/packages/e4/04/d52c7016b04b6c5108f26691f9d33ec82a9b65d041f1a9c771137693d618/protobuf-7.36.2-py3-none-any.whl", hash = "sha256:bdb3a345d48db958e6ce1f18e508beb0cc981d64f24088427549c866cd039f1e", size = 179806, upload-time = "2026-09-17T20:07:58.211Z" }, +] + [[package]] name = "py-serializable" version = "2.2.0" @@ -791,6 +818,7 @@ version = "0" source = { editable = "." } dependencies = [ { name = "click" }, + { name = "temporalio" }, ] [package.dev-dependencies] @@ -815,7 +843,10 @@ test = [ ] [package.metadata] -requires-dist = [{ name = "click", specifier = ">=8.3.0" }] +requires-dist = [ + { name = "click", specifier = ">=8.3.0" }, + { name = "temporalio", specifier = ">=1.36.1" }, +] [package.metadata.requires-dev] dev = [ @@ -838,6 +869,27 @@ test = [ { name = "pytest-xdist", specifier = ">=3.8.0" }, ] +[[package]] +name = "temporalio" +version = "1.36.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "nexus-rpc" }, + { name = "protobuf" }, + { name = "types-protobuf" }, + { name = "typing-extensions" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/3c/7b/d425d26b4e6a525e7b87eadba201f0f72eedee91d65712e9cb772a51a586/temporalio-1.36.1.tar.gz", hash = "sha256:1342db50b6609718d5b222bb4f3070f7b8f17265ba027f3106d7b6a1ccf8af42", size = 2923760, upload-time = "2026-11-14T02:21:01.023Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/55/79/5dfa2c09a0dffb856491eb4d6f28bdde4b0ff4229d9001d7bcc179f9c9d4/temporalio-1.36.1-cp311-abi3-macosx_10_12_x86_64.whl", hash = "sha256:55c0144c77182fa26a5172eab066a0661632f7ac2d8054d7d7fa9dd4ede93cc2", size = 14155700, upload-time = "2026-11-14T02:20:42.053Z" }, + { url = "https://files.pythonhosted.org/packages/84/bc/d526ba83d13b09b53d70dae19d7417dcbb36ad951e79427c949b5c3cd166/temporalio-1.36.1-cp311-abi3-macosx_11_0_arm64.whl", hash = "sha256:175a89e59ed9e7cc5795609d937c80111513ca9d70b0ed9da4ed7792163c863a", size = 13720267, upload-time = "2026-11-14T02:20:45.68Z" }, + { url = "https://files.pythonhosted.org/packages/e4/27/931e15f50e2965091d72732a960f1f5865dfcf2894cd5f693abf9bf38a3c/temporalio-1.36.1-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:72b3c4f700e48f3953192bb2d66700f74c1ff1ecf131e82c710761ae09fb4c0f", size = 14106450, upload-time = "2026-11-14T02:20:48.074Z" }, + { url = "https://files.pythonhosted.org/packages/12/62/c9d8a9a6c10ddfdbfb4d18e9dabf9bb812d237f7df13bc1a5a5b1a266f47/temporalio-1.36.1-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:5ce0ddfaf35316271eee632e1579890be6f3c0f1de7fddfd1f3086f3f83d6b78", size = 14529397, upload-time = "2026-11-14T02:20:50.604Z" }, + { url = "https://files.pythonhosted.org/packages/f9/50/b0572f6ac5863999f93500d12bbed476542df2fee4d39115dc5a9ba8805a/temporalio-1.36.1-cp311-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:cfa4f316cc23a7757e03650087cf471fffa4ca27ab40bba6c09d98fe4ab3d5e6", size = 14169959, upload-time = "2026-11-14T02:20:53.019Z" }, + { url = "https://files.pythonhosted.org/packages/d7/3e/241ae6f12139b94945b673e503b79f669305418f47216adb78e3a4b78b08/temporalio-1.36.1-cp311-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:b22d93523b65f17e39970a341ad17de2100fdb5a8cd23c7499ffbd342ffb412a", size = 14633381, upload-time = "2026-11-14T02:20:55.636Z" }, + { url = "https://files.pythonhosted.org/packages/83/0c/51e526f3926a29a442e703009c203fd301b30a41aeb963cd8be7617ac011/temporalio-1.36.1-cp311-abi3-win_amd64.whl", hash = "sha256:42675a52865cdd71c3a7a42f7415157af7574458788df419b723167be282e51d", size = 15509357, upload-time = "2026-11-14T02:20:58.109Z" }, +] + [[package]] name = "textual" version = "8.2.8" @@ -900,6 +952,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/da/46/7d84138a60a8bd796825b2d38ebe92d0d16501ca57d501b7c5d677401bbc/tomlkit-0.15.2-py3-none-any.whl", hash = "sha256:05562412163a3037b94497385fb7f9204c637bc605af7a48199e41cf2272f437", size = 52225, upload-time = "2026-10-16T04:07:09.216Z" }, ] +[[package]] +name = "types-protobuf" +version = "7.35.1.20260906" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/e1/6c/e3e5b3e10bc328126a39637c138f9ebfd734bf14342b9f3540039b4ab995/types_protobuf-7.35.1.20260906.tar.gz", hash = "sha256:efd1a3862d4c967dad5512ef8d56b1530ac84f182c41735b94004756518c4998", size = 69895, upload-time = "2026-09-06T06:35:28.308Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/44/4e/f63e826c68f77ef875506d72f225918800346545ee99847bc28f3394f18d/types_protobuf-7.35.1.20260906-py3-none-any.whl", hash = "sha256:5155e48569e0dabff303fdf578db96cd31ea9a4a63b18018a4ceac6b0ae17462", size = 86419, upload-time = "2026-09-06T06:35:27.247Z" }, +] + [[package]] name = "typing-extensions" version = "4.16.0"