Skip to content

Self-improving AI Employees for everyone

feat(temporal): wire TemporalWorkerPool into lifespan behind flag (Wave 16.2)

trohitg18df875ac69d
diff --git a/.env.template b/.env.template
index 7793d196..b7edfe79 100644
--- a/.env.template
+++ b/.env.template
@@ -56,6 +56,13 @@ TEMPORAL_NAMESPACE=default
TEMPORAL_TASK_QUEUE=machina-tasks
TEMPORAL_PER_TYPE_DISPATCH=true
TEMPORAL_AGENT_WORKFLOW_ENABLED=true
+# Wave 16: per-queue activity routing. When true, a TemporalWorkerPool
+# starts one activity-only Worker per plugin-declared task queue
+# (ai-heavy / browser / code-exec / rest-api / messaging / ...) and
+# per-type activities route to their specialised queue instead of the
+# single 100-slot pool on machina-tasks. Rollback channel: set false +
+# restart — activities route back to the manager worker unchanged.
+TEMPORAL_WORKER_POOL_ENABLED=false
# gRPC frontend port the CLI dev server binds (``--port``). Default per
# https://docs.temporal.io/references/configuration.
TEMPORAL_FRONTEND_GRPC_PORT=7233
diff --git a/server/core/config.py b/server/core/config.py
index 2532cd2a..d6222261 100644
--- a/server/core/config.py
+++ b/server/core/config.py
@@ -96,6 +96,18 @@ class Settings(BaseSettings):
default=True,
env="EVENT_FRAMEWORK_ENABLED",
)
+ # Wave 16: per-queue activity routing. When True, main.py starts a
+ # TemporalWorkerPool (one activity-only Worker per plugin-declared
+ # task queue) alongside the TemporalWorkerManager, and
+ # MachinaWorkflow._resolve_activity returns cls.task_queue so
+ # per-type activities land on their specialised pool worker.
+ # Rollback: TEMPORAL_WORKER_POOL_ENABLED=false + restart — the pool
+ # stops and every activity routes back to the single manager worker
+ # on "machina-tasks". Default off until the 16.4 stability window.
+ temporal_worker_pool_enabled: bool = Field(
+ default=False,
+ env="TEMPORAL_WORKER_POOL_ENABLED",
+ )
# gRPC frontend port — the port ``temporal server start-dev``
# exposes for clients + supervisor readiness probe. Sourced from
diff --git a/server/main.py b/server/main.py
index b4cc51b7..e09b8175 100644
--- a/server/main.py
+++ b/server/main.py
@@ -280,6 +280,7 @@ async def lifespan(app: FastAPI):
# let Temporal init happen in a background task. WorkflowService falls back to
# parallel/sequential execution until Temporal is ready.
app.state.temporal_worker_manager = None
+ app.state.temporal_pool = None
temporal_init_task: asyncio.Task | None = None
if settings.temporal_enabled:
@@ -350,6 +351,18 @@ async def lifespan(app: FastAPI):
await worker_manager.start()
app.state.temporal_worker_manager = worker_manager
+ # Wave 16.2: per-queue activity worker pool. Gated —
+ # inert until TEMPORAL_WORKER_POOL_ENABLED=true. Starts
+ # AFTER the manager so workflow registration is in
+ # place before specialised activity workers poll.
+ if settings.temporal_worker_pool_enabled:
+ from services.temporal.worker import TemporalWorkerPool
+
+ pool = TemporalWorkerPool(client=client)
+ await pool.start()
+ app.state.temporal_pool = pool
+ _startup_log(f"[Temporal] Worker pool started ({len(pool.queues)} queues)")
+
_startup_log(f"[Temporal] Worker started, execution engine ready (attempt {attempt})")
logger.info(
"Temporal integration initialized successfully",
@@ -416,6 +429,16 @@ async def lifespan(app: FastAPI):
except (asyncio.CancelledError, Exception):
pass
+ # Stop the per-queue worker pool first (activity-only workers), then
+ # the manager worker that also hosts workflows.
+ pool = getattr(app.state, "temporal_pool", None)
+ if pool is not None:
+ try:
+ await pool.stop()
+ logger.info("Temporal worker pool stopped")
+ except Exception as exc:
+ logger.warning(f"Temporal worker pool stop raised: {exc}")
+
# Stop Temporal worker if it successfully started.
worker_manager = getattr(app.state, "temporal_worker_manager", None)
if worker_manager is not None: