diff --git a/hindsight-api/hindsight_api/engine/memory_engine.py b/hindsight-api/hindsight_api/engine/memory_engine.py index a41d0e87..c21285da 100644 --- a/hindsight-api/hindsight_api/engine/memory_engine.py +++ b/hindsight-api/hindsight_api/engine/memory_engine.py @@ -545,16 +545,19 @@ class MemoryEngine(MemoryEngineInterface): f"[BATCH_RETAIN_TASK] Starting background batch retain for bank_id={bank_id}, {len(contents)} items" ) - # Restore tenant_id/api_key_id from task payload so downstream operations - # (e.g., consolidation and mental model refreshes) can attribute usage. + # Restore tenant_id/api_key_id from task payload so extensions + # (e.g., operation validators) can attribute the operation correctly. + # internal=True to skip extension auth (worker has no API key), + # user_initiated=True so extensions know this originated from a user request. from hindsight_api.models import RequestContext - internal_context = RequestContext( + context = RequestContext( internal=True, + user_initiated=True, tenant_id=task_dict.get("_tenant_id"), api_key_id=task_dict.get("_api_key_id"), ) - await self.retain_batch_async(bank_id=bank_id, contents=contents, request_context=internal_context) + await self.retain_batch_async(bank_id=bank_id, contents=contents, request_context=context) logger.info(f"[BATCH_RETAIN_TASK] Completed background batch retain for bank_id={bank_id}") diff --git a/hindsight-api/hindsight_api/models.py b/hindsight-api/hindsight_api/models.py index b5ecf4ca..38b1a4e5 100644 --- a/hindsight-api/hindsight_api/models.py +++ b/hindsight-api/hindsight_api/models.py @@ -20,7 +20,8 @@ class RequestContext: api_key: str | None = None api_key_id: str | None = None # UUID of the API key used for authentication tenant_id: str | None = None # Tenant identifier (set by extension after auth) - internal: bool = False # True for background/internal operations (not user-visible) + internal: bool = False # True for background/internal operations (skips extension auth) + user_initiated: bool = False # True for async operations that originated from a user request from pgvector.sqlalchemy import Vector