Part of #16946. Found while building presence (#16947, PR #16965).
Finding
A terminal session has no tenant, and none can be recovered after the fact. So presence reports every session as UNKNOWN_TENANT — correctly fail-closed, but it means no session can be discovered or addressed within its own tenant.
The route that looked like it might work does not exist:
AgentTerminalSession (services/agent_terminal/models.py) has no tenant, company, org or owner field.
conversation_id is used only for log linkage, everywhere it appears (session_manager.py:88, 181, 210, 244).
- The chat-history session and conversation stores composed by
ChatHistoryManager carry no user or tenant field.
- The authenticated creator's
owner is passed through SessionManager.create_session → _setup_pty_for_session → _register_pty_with_terminal_manager, but only for PTY registration, and it is never persisted on the session.
No username → organisation lookup exists either, so that may need building.
Fix
Capture the tenant at creation, while the authenticated owner is still in hand:
- Resolve the creating principal's organisation when the session is created.
- Persist it on
AgentTerminalSession as an explicit field.
- Have the presence feed report it, falling back to
UNKNOWN_TENANT only when it genuinely could not be determined.
Acceptance criteria
Part of #16946. Found while building presence (#16947, PR #16965).
Finding
A terminal session has no tenant, and none can be recovered after the fact. So presence reports every session as
UNKNOWN_TENANT— correctly fail-closed, but it means no session can be discovered or addressed within its own tenant.The route that looked like it might work does not exist:
AgentTerminalSession(services/agent_terminal/models.py) has no tenant, company, org or owner field.conversation_idis used only for log linkage, everywhere it appears (session_manager.py:88, 181, 210, 244).ChatHistoryManagercarry no user or tenant field.owneris passed throughSessionManager.create_session→_setup_pty_for_session→_register_pty_with_terminal_manager, but only for PTY registration, and it is never persisted on the session.No username → organisation lookup exists either, so that may need building.
Fix
Capture the tenant at creation, while the authenticated owner is still in hand:
AgentTerminalSessionas an explicit field.UNKNOWN_TENANTonly when it genuinely could not be determined.Acceptance criteria
UNKNOWN_TENANTUNKNOWN_TENANT, not guessed