fix(api): guard against missing session in get_agent_session (#15011)

`GET /agents/<agent_id>/sessions/<session_id>` crashed with
`AttributeError: 'NoneType' object has no attribute 'to_dict'` when the
session lookup failed: `_, conv =
API4ConversationService.get_by_id(...)` returned `(False, None)`, then
`conv.to_dict()` was called unconditionally.

This is reachable in multi-instance deployments: the session row may not
yet be visible on the node servicing the immediate follow-up GET after a
session is created on a different node.

Add the same `if not exists` guard already used by every other call site
of `API4ConversationService.get_by_id` (see agent_api.py:1147,
sdk/session.py:179, conversation_service.py:248, canvas_service.py:323).

Closes #14989

### What problem does this PR solve?

_Briefly describe what this PR aims to solve. Include background context
that will help reviewers understand the purpose of the PR._

### Type of change

- [ ] Bug Fix (non-breaking change which fixes an issue)
- [ ] New Feature (non-breaking change which adds functionality)
- [ ] Documentation Update
- [ ] Refactoring
- [ ] Performance Improvement
- [ ] Other (please describe):
This commit is contained in:
dripsmvcp
2026-05-20 11:24:17 +09:00
committed by Jin Hai
parent ce9a4425d2
commit 12a148d541
2 changed files with 132 additions and 1 deletions

View File

@@ -412,7 +412,9 @@ async def create_agent_session(agent_id, tenant_id):
@add_tenant_id_to_kwargs
@_require_canvas_access_sync
def get_agent_session(agent_id, session_id, tenant_id):
_, conv = API4ConversationService.get_by_id(session_id)
exists, conv = API4ConversationService.get_by_id(session_id)
if not exists:
return get_data_error_result(message="Session not found!")
return get_json_result(data=conv.to_dict())