From 9c95a1ac1dfa758f7e7082cf4c07e00ab914e56a Mon Sep 17 00:00:00 2001 From: Chris Bartholomew Date: Thu, 29 Jan 2026 12:35:38 -0500 Subject: [PATCH] fix: pass tenant extension to worker MemoryEngine for correct schema context (#236) The worker loaded the tenant extension for the poller (schema discovery) but did not pass it to MemoryEngine. When execute_task set _current_schema via the _schema field, _authenticate_tenant would immediately reset it to "public" because self._tenant_extension was None, causing all worker writes to land in the public schema instead of the tenant schema. Move load_extension() before MemoryEngine creation and pass tenant_extension to the constructor. --- hindsight-api/hindsight_api/worker/main.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/hindsight-api/hindsight_api/worker/main.py b/hindsight-api/hindsight_api/worker/main.py index d5f6fb47..b5d69466 100644 --- a/hindsight-api/hindsight_api/worker/main.py +++ b/hindsight-api/hindsight_api/worker/main.py @@ -183,21 +183,25 @@ def main(): from ..extensions import TenantExtension, load_extension + # Load tenant extension BEFORE creating MemoryEngine so it can + # set correct schema context during task execution. Without this, + # _authenticate_tenant sees no extension and resets schema to "public", + # causing worker writes to land in the wrong schema. + tenant_extension = load_extension("TENANT", TenantExtension) + # Initialize MemoryEngine # Workers use SyncTaskBackend because they execute tasks directly, # they don't need to store tasks (they poll from DB) memory = MemoryEngine( run_migrations=False, # Workers don't run migrations task_backend=SyncTaskBackend(), + tenant_extension=tenant_extension, ) await memory.initialize() print(f"Database connected: {config.database_url}") - # Load tenant extension for dynamic schema discovery - tenant_extension = load_extension("TENANT", TenantExtension) - if tenant_extension: print("Tenant extension loaded - schemas will be discovered dynamically on each poll") else: