Skip to content

Commit e3e45d7

Browse files
committed
fix(api): keep the Workspace tenant scope for streamed Agent debug runs
The NDJSON debug stream emits its frames after the request handler returned, so the request-scoped tenant scope is already closed by then. The deferred execution therefore failed with TenantScopeRequiredError on its first persistence access and the client only saw a generic runner_error frame with no log entry. Re-enter the Workspace tenant scope around the deferred execution and log unexpected failures with their traceback.
1 parent 05de725 commit e3e45d7

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

‎src/langbot/pkg/api/http/controller/groups/agent_debug_stream.py‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,25 @@ async def on_result(result: dict) -> None:
2626
await queue.put({'kind': 'result', 'data': result})
2727

2828
async def execute() -> None:
29+
application = getattr(service, 'ap', None)
30+
persistence_mgr = getattr(application, 'persistence_mgr', None)
31+
tenant_scope = getattr(persistence_mgr, 'tenant_scope', None)
32+
scope = (
33+
tenant_scope(context.workspace_uuid)
34+
if callable(tenant_scope)
35+
else contextlib.nullcontext()
36+
)
2937
try:
30-
result = await service.debug_agent(context, agent_uuid, payload, on_result=on_result)
38+
# The streamed body is emitted after the request handler returned,
39+
# so the request's tenant scope has already closed: carry the
40+
# trusted Workspace identity into the deferred execution.
41+
async with scope:
42+
result = await service.debug_agent(
43+
context,
44+
agent_uuid,
45+
payload,
46+
on_result=on_result,
47+
)
3148
await queue.put({'kind': 'completed', 'data': result})
3249
except Exception as exc:
3350
# The stream still uses HTTP 200 when execution returns an
@@ -46,6 +63,9 @@ async def execute() -> None:
4663
elif isinstance(exc, RunnerError):
4764
code, message = 'runner_error', 'The Agent runner could not complete this test'
4865
else:
66+
logger = getattr(application, 'logger', None)
67+
if logger is not None:
68+
logger.exception('Agent debug stream execution failed')
4969
code, message = 'runner_error', 'The Agent debug execution failed'
5070
await queue.put({'kind': 'error', 'code': code, 'msg': message})
5171

0 commit comments

Comments
 (0)