mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-08-08 20:30:59 +00:00
feat: report independent instance and workspace identities (#2394)
* feat: report independent instance and workspace identities * test: include workspace in OAuth callback fixture * ci: pin production cloud adapter to Space release * fix: preserve authenticated Workspace telemetry attribution * ci: pin production cloud adapter to final Space release --------- Co-authored-by: dadachann <185672915+dadachann@users.noreply.github.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import quart
|
||||
import argon2
|
||||
import asyncio
|
||||
import datetime
|
||||
import uuid
|
||||
from urllib.parse import parse_qs, urlsplit
|
||||
|
||||
@@ -218,7 +219,22 @@ class UserRouterGroup(group.RouterGroup):
|
||||
try:
|
||||
consumed_state = await self.ap.user_service.consume_space_oauth_state_details(state, 'login')
|
||||
# Exchange code for tokens
|
||||
token_data = await self.ap.space_service.exchange_oauth_code(code)
|
||||
launch_workspace_uuid = consumed_state.launch_workspace_uuid
|
||||
workspace_uuids = [launch_workspace_uuid] if launch_workspace_uuid else []
|
||||
workspace_created_ats: dict[str, int] = {}
|
||||
if not workspace_uuids and getattr(getattr(self.ap, 'deployment', None), 'mode', 'oss') != 'cloud':
|
||||
binding = await self.ap.workspace_service.get_execution_binding()
|
||||
workspace_uuids = [binding.workspace_uuid]
|
||||
workspace_created_at = binding.workspace_created_at
|
||||
if workspace_created_at is not None:
|
||||
if workspace_created_at.tzinfo is None:
|
||||
workspace_created_at = workspace_created_at.replace(tzinfo=datetime.UTC)
|
||||
workspace_created_ats[binding.workspace_uuid] = int(workspace_created_at.timestamp())
|
||||
token_data = await self.ap.space_service.exchange_oauth_code(
|
||||
code,
|
||||
workspace_uuids,
|
||||
workspace_created_ats,
|
||||
)
|
||||
access_token = token_data.get('access_token')
|
||||
refresh_token = token_data.get('refresh_token')
|
||||
expires_in = token_data.get('expires_in', 0)
|
||||
@@ -231,7 +247,6 @@ class UserRouterGroup(group.RouterGroup):
|
||||
access_token, refresh_token, expires_in
|
||||
)
|
||||
|
||||
launch_workspace_uuid = consumed_state.launch_workspace_uuid
|
||||
if launch_workspace_uuid:
|
||||
try:
|
||||
access = await self.ap.workspace_collaboration_service.resolve_account_workspace(
|
||||
|
||||
@@ -59,6 +59,10 @@ class SpaceService:
|
||||
result_list = result.all()
|
||||
return result_list[0] if result_list else None
|
||||
|
||||
async def get_valid_access_token(self, user_email: str) -> str | None:
|
||||
"""Return a current Space bearer, refreshing and persisting it when needed."""
|
||||
return await self._ensure_valid_token(user_email)
|
||||
|
||||
async def _ensure_valid_token(self, user_email: str) -> str | None:
|
||||
"""Ensure access token is valid, refresh if expired. Returns valid access_token or None."""
|
||||
user_obj = await self._get_user_by_email(user_email)
|
||||
@@ -117,7 +121,12 @@ class SpaceService:
|
||||
params['state'] = state
|
||||
return f'{authorize_url}?{urlencode(params)}'
|
||||
|
||||
async def exchange_oauth_code(self, code: str) -> typing.Dict:
|
||||
async def exchange_oauth_code(
|
||||
self,
|
||||
code: str,
|
||||
workspace_uuids: list[str] | None = None,
|
||||
workspace_created_ats: dict[str, int] | None = None,
|
||||
) -> typing.Dict:
|
||||
"""Exchange OAuth authorization code for tokens"""
|
||||
from langbot.pkg.utils import constants
|
||||
|
||||
@@ -127,7 +136,14 @@ class SpaceService:
|
||||
session = httpclient.get_session()
|
||||
async with session.post(
|
||||
f'{space_url}/api/v1/accounts/oauth/token',
|
||||
json={'code': code, 'instance_id': constants.instance_id},
|
||||
json={
|
||||
'code': code,
|
||||
'instance_id': constants.instance_id,
|
||||
# Sending an explicit empty list tells new Space servers not to
|
||||
# synthesize a legacy instance-derived Workspace binding.
|
||||
'workspace_uuids': workspace_uuids if workspace_uuids is not None else [],
|
||||
'workspace_created_ats': workspace_created_ats or {},
|
||||
},
|
||||
) as response:
|
||||
if response.status != 200:
|
||||
error = await httpclient.read_text_limited(response)
|
||||
|
||||
@@ -779,8 +779,27 @@ class UserService:
|
||||
local_account = await self.get_user_by_email(user_email)
|
||||
if local_account is None:
|
||||
raise ValueError('User not found')
|
||||
# Exchange code for tokens
|
||||
token_data = await self.ap.space_service.exchange_oauth_code(code)
|
||||
# Exchange code for tokens and bind both installation and the active
|
||||
# OSS Workspace as independent identities.
|
||||
workspace_service = getattr(self.ap, 'workspace_service', None)
|
||||
if workspace_service is not None:
|
||||
binding = await workspace_service.get_execution_binding()
|
||||
created_at = binding.workspace_created_at
|
||||
created_ts = (
|
||||
int(created_at.replace(tzinfo=datetime.timezone.utc).timestamp())
|
||||
if created_at.tzinfo is None
|
||||
else int(created_at.timestamp())
|
||||
)
|
||||
token_data = await self.ap.space_service.exchange_oauth_code(
|
||||
code,
|
||||
[binding.workspace_uuid],
|
||||
{binding.workspace_uuid: created_ts},
|
||||
)
|
||||
else:
|
||||
# Compatibility for early/bootstrap call sites that have not wired
|
||||
# WorkspaceService yet; old Space servers still derive the legacy
|
||||
# Workspace identity from instance_id when the field is omitted.
|
||||
token_data = await self.ap.space_service.exchange_oauth_code(code)
|
||||
access_token = token_data.get('access_token')
|
||||
refresh_token = token_data.get('refresh_token')
|
||||
expires_in = token_data.get('expires_in', 0)
|
||||
|
||||
@@ -120,6 +120,7 @@ async def build_heartbeat_payload(
|
||||
ap: core_app.Application,
|
||||
*,
|
||||
workspace_uuid: str,
|
||||
workspace_create_ts: int = 0,
|
||||
workspace_resource: WorkspaceResourceSnapshot | None = None,
|
||||
) -> dict:
|
||||
"""Collect one anonymous Workspace profile snapshot."""
|
||||
@@ -212,7 +213,9 @@ async def build_heartbeat_payload(
|
||||
'event_type': 'instance_heartbeat',
|
||||
'query_id': '',
|
||||
'version': constants.semantic_version,
|
||||
'instance_id': constants.instance_id,
|
||||
'workspace_uuid': workspace_uuid,
|
||||
'workspace_create_ts': workspace_create_ts,
|
||||
'instance_create_ts': constants.instance_create_ts,
|
||||
'edition': constants.edition,
|
||||
'features': features,
|
||||
@@ -220,10 +223,24 @@ async def build_heartbeat_payload(
|
||||
}
|
||||
|
||||
|
||||
def _workspace_created_timestamp(created_at: datetime | None) -> int:
|
||||
if created_at is None:
|
||||
return 0
|
||||
if created_at.tzinfo is None:
|
||||
# SQLAlchemy may return persisted UTC values without tzinfo. Never
|
||||
# reinterpret them in the host's local timezone.
|
||||
created_at = created_at.replace(tzinfo=timezone.utc)
|
||||
return int(created_at.timestamp())
|
||||
|
||||
|
||||
async def build_heartbeat_payloads(ap: core_app.Application) -> list[dict]:
|
||||
"""Build one heartbeat per active Workspace."""
|
||||
bindings = await ap.workspace_service.list_active_execution_bindings()
|
||||
workspace_uuids = sorted({binding.workspace_uuid for binding in bindings})
|
||||
workspace_create_ts = {
|
||||
binding.workspace_uuid: _workspace_created_timestamp(getattr(binding, 'workspace_created_at', None))
|
||||
for binding in bindings
|
||||
}
|
||||
resources = {
|
||||
resource['workspace_uuid']: resource for resource in await _cloud_workspace_resource_counts(ap, bindings)
|
||||
}
|
||||
@@ -231,6 +248,7 @@ async def build_heartbeat_payloads(ap: core_app.Application) -> list[dict]:
|
||||
await build_heartbeat_payload(
|
||||
ap,
|
||||
workspace_uuid=workspace_uuid,
|
||||
workspace_create_ts=workspace_create_ts.get(workspace_uuid, 0),
|
||||
workspace_resource=resources.get(workspace_uuid),
|
||||
)
|
||||
for workspace_uuid in workspace_uuids
|
||||
|
||||
@@ -4,13 +4,19 @@ import typing
|
||||
|
||||
|
||||
class WorkspaceExecutionContext(typing.Protocol):
|
||||
@property
|
||||
def instance_uuid(self) -> str: ...
|
||||
|
||||
@property
|
||||
def workspace_uuid(self) -> str: ...
|
||||
|
||||
|
||||
def workspace_identity(execution_context: WorkspaceExecutionContext) -> dict[str, str]:
|
||||
"""Build the canonical telemetry identity for one Workspace execution."""
|
||||
"""Build both first-class telemetry identities for one execution."""
|
||||
instance_id = execution_context.instance_uuid.strip()
|
||||
workspace_uuid = execution_context.workspace_uuid.strip()
|
||||
if not instance_id:
|
||||
raise ValueError('Telemetry execution instance ID is empty')
|
||||
if not workspace_uuid:
|
||||
raise ValueError('Telemetry execution Workspace UUID is empty')
|
||||
return {'workspace_uuid': workspace_uuid}
|
||||
return {'instance_id': instance_id, 'workspace_uuid': workspace_uuid}
|
||||
|
||||
@@ -136,12 +136,31 @@ class TelemetryManager:
|
||||
try:
|
||||
# Use asyncio.wait_for to ensure we always bound the total time
|
||||
telemetry_token = os.getenv('LANGBOT_TELEMETRY_INGEST_TOKEN', '').strip()
|
||||
headers: dict[str, str] = {}
|
||||
if telemetry_token:
|
||||
request = client.post(
|
||||
url,
|
||||
json=sanitized,
|
||||
headers={'X-LangBot-Telemetry-Token': telemetry_token},
|
||||
)
|
||||
headers['X-LangBot-Telemetry-Token'] = telemetry_token
|
||||
else:
|
||||
workspace_uuid = str(sanitized.get('workspace_uuid', '')).strip()
|
||||
user_service = getattr(self.ap, 'user_service', None)
|
||||
if workspace_uuid and user_service is not None:
|
||||
try:
|
||||
owner = await user_service.get_workspace_owner(workspace_uuid)
|
||||
owner_email = str(getattr(owner, 'user', '') or '').strip()
|
||||
space_service = getattr(self.ap, 'space_service', None)
|
||||
access_token = (
|
||||
await space_service.get_valid_access_token(owner_email)
|
||||
if owner_email and space_service is not None
|
||||
else None
|
||||
)
|
||||
access_token = str(access_token or '').strip()
|
||||
if access_token:
|
||||
headers['Authorization'] = f'Bearer {access_token}'
|
||||
except Exception:
|
||||
self.ap.logger.debug(
|
||||
'Could not resolve authenticated telemetry reporter', exc_info=True
|
||||
)
|
||||
if headers:
|
||||
request = client.post(url, json=sanitized, headers=headers)
|
||||
else:
|
||||
request = client.post(url, json=sanitized)
|
||||
resp = await asyncio.wait_for(request, timeout=10 + 1)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@@ -12,3 +13,4 @@ class WorkspaceExecutionBinding:
|
||||
placement_generation: int
|
||||
write_fenced: bool
|
||||
state: str
|
||||
workspace_created_at: datetime.datetime | None = None
|
||||
|
||||
@@ -283,6 +283,7 @@ class WorkspaceService:
|
||||
placement_generation=execution_state.active_generation,
|
||||
write_fenced=execution_state.write_fenced,
|
||||
state=execution_state.state,
|
||||
workspace_created_at=workspace.created_at,
|
||||
)
|
||||
|
||||
binding = await self._run(operation, session=session)
|
||||
|
||||
Reference in New Issue
Block a user