File size: 24,570 Bytes
22087d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
import json
import asyncio
import time
from dataclasses import dataclass, field
from datetime import datetime, timezone
from typing import Dict, List, Any, Optional
from uuid import uuid4, UUID
from enum import Enum

from core.services.supabase import DBConnection
from core.utils.logger import logger

MCP_CONFIG_QUERY_TIMEOUT = 10.0


class VersionStatus(Enum):
    ACTIVE = "active"
    INACTIVE = "inactive"
    ARCHIVED = "archived"


@dataclass
class AgentVersion:
    version_id: str
    agent_id: str
    version_number: int
    version_name: str
    system_prompt: str
    model: Optional[str] = None  # Add model field
    configured_mcps: List[Dict[str, Any]] = field(default_factory=list)
    custom_mcps: List[Dict[str, Any]] = field(default_factory=list)
    agentpress_tools: Dict[str, Any] = field(default_factory=dict)
    is_active: bool = True
    created_at: datetime = field(default_factory=lambda: datetime.now(timezone.utc))
    updated_at: datetime = field(default_factory=lambda: datetime.now(timezone.utc))
    created_by: str = ""
    change_description: Optional[str] = None
    previous_version_id: Optional[str] = None
    
    def to_dict(self) -> Dict[str, Any]:
        return {
            'version_id': self.version_id,
            'agent_id': self.agent_id,
            'version_number': self.version_number,
            'version_name': self.version_name,
            'system_prompt': self.system_prompt,
            'model': self.model,
            'configured_mcps': self.configured_mcps,
            'custom_mcps': self.custom_mcps,
            'agentpress_tools': self.agentpress_tools,
            'is_active': self.is_active,
            'status': VersionStatus.ACTIVE.value if self.is_active else VersionStatus.INACTIVE.value,
            'created_at': self.created_at.isoformat(),
            'updated_at': self.updated_at.isoformat(),
            'created_by': self.created_by,
            'change_description': self.change_description,
            'previous_version_id': self.previous_version_id
        }


class VersionServiceError(Exception):
    pass

class VersionNotFoundError(VersionServiceError):
    pass

class AgentNotFoundError(VersionServiceError):
    pass

class UnauthorizedError(VersionServiceError):
    pass

class InvalidVersionError(VersionServiceError):
    pass

class VersionConflictError(VersionServiceError):
    pass


class VersionService:
    def __init__(self):
        self.db = DBConnection()
    
    async def _get_client(self):
        return await self.db.client
    
    async def _verify_and_authorize_agent_access(self, agent_id: str, user_id: str) -> tuple[bool, bool]:
        if user_id == "system":
            return True, True
            
        from core.versioning import repo as versioning_repo
        
        access_info = await versioning_repo.check_agent_access(agent_id, user_id)
        return access_info["is_owner"], access_info["is_public"]
    
    async def _get_next_version_number(self, agent_id: str) -> int:
        from core.versioning import repo as versioning_repo
        return await versioning_repo.get_next_version_number(agent_id)
    
    async def _count_versions(self, agent_id: str) -> int:
        from core.versioning import repo as versioning_repo
        return await versioning_repo.count_agent_versions(agent_id)
    
    async def _update_agent_current_version(self, agent_id: str, version_id: str, version_count: int):
        from core.versioning import repo as versioning_repo
        from core.agents import repo as agents_repo
        
        # Update agent's current version ID
        await versioning_repo.update_agent_current_version(agent_id, version_id)
        
        # Update version count using agents repo (since it's in agents table)
        await versioning_repo.update_agent_version_stats(agent_id, version_count)
        
        from core.cache.runtime_cache import invalidate_mcp_version_config
        await invalidate_mcp_version_config(agent_id)
        logger.debug(f"Invalidated MCP config cache for agent {agent_id} after version update")
    
    def _version_from_db_row(self, row: Dict[str, Any]) -> AgentVersion:
        config = row.get('config', {})
        tools = config.get('tools', {})
        
        return AgentVersion(
            version_id=row['version_id'],
            agent_id=row['agent_id'],
            version_number=row['version_number'],
            version_name=row['version_name'],
            system_prompt=config.get('system_prompt', ''),
            model=config.get('model'),  # Extract model from config
            configured_mcps=tools.get('mcp', []),
            custom_mcps=tools.get('custom_mcp', []),
            agentpress_tools=tools.get('agentpress', {}),
            is_active=row.get('is_active', False),
            created_at=datetime.fromisoformat(row['created_at'].replace('Z', '+00:00')),
            updated_at=datetime.fromisoformat(row['updated_at'].replace('Z', '+00:00')),
            created_by=row['created_by'],
            change_description=row.get('change_description'),
            previous_version_id=row.get('previous_version_id')
        )
    
    def _normalize_custom_mcps(self, custom_mcps: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
        normalized = []
        for mcp in custom_mcps:
            if not isinstance(mcp, dict):
                continue
                
            mcp_copy = mcp.copy()
            config = mcp_copy.get('config', {})
            mcp_type = mcp_copy.get('type', 'sse')
            mcp_name = mcp_copy.get('name', '')
            
            if mcp_type == 'composio':
                if 'mcp_qualified_name' not in mcp_copy:
                    mcp_copy['mcp_qualified_name'] = config.get('mcp_qualified_name') or config.get('qualifiedName') or f"composio.{mcp_name.lower().replace(' ', '_')}"
                if 'toolkit_slug' not in mcp_copy:
                    mcp_copy['toolkit_slug'] = config.get('toolkit_slug') or mcp_name.lower().replace(' ', '_')
                
                mcp_copy['config'] = {k: v for k, v in config.items() if k == 'profile_id'}
            
            normalized.append(mcp_copy)
        return normalized

    async def create_version(
        self,
        agent_id: str,
        user_id: str,
        system_prompt: str,
        configured_mcps: List[Dict[str, Any]],
        custom_mcps: List[Dict[str, Any]],
        agentpress_tools: Dict[str, Any],
        model: Optional[str] = None,
        version_name: Optional[str] = None,
        change_description: Optional[str] = None
    ) -> AgentVersion:
        
        logger.debug(f"Creating version for agent {agent_id}")
        client = await self.db.client
        
        is_owner, _ = await self._verify_and_authorize_agent_access(agent_id, user_id)
        if not is_owner:
            raise UnauthorizedError("Unauthorized to create version for this agent")
        
        from core.agents import repo as agents_repo
        from core.versioning import repo as versioning_repo
        
        agent_info = await agents_repo.get_agent_by_id(agent_id)
        if not agent_info:
            raise Exception("Agent not found")

        previous_version_id = agent_info.get('current_version_id')
        
        auto_generate_version_name = version_name is None
        
        max_retries = 3
        for attempt in range(max_retries):
            try:
                version_number = await versioning_repo.get_next_version_number(agent_id)
                
                if auto_generate_version_name:
                    version_name = f"v{version_number}"
                        
                triggers = await versioning_repo.get_agent_triggers(agent_id)
                for trigger in triggers:
                    if 'config' in trigger and isinstance(trigger['config'], str):
                        try:
                            import json
                            trigger['config'] = json.loads(trigger['config'])
                        except json.JSONDecodeError:
                            logger.warning(f"Failed to parse trigger config for {trigger.get('trigger_id')}")
                            trigger['config'] = {}
                
                normalized_custom_mcps = self._normalize_custom_mcps(custom_mcps)
                
                version = AgentVersion(
                    version_id=str(uuid4()),
                    agent_id=agent_id,
                    version_number=version_number,
                    version_name=version_name,
                    system_prompt=system_prompt,
                    model=model,
                    configured_mcps=configured_mcps,
                    custom_mcps=normalized_custom_mcps,
                    agentpress_tools=agentpress_tools,
                    is_active=True,
                    created_at=datetime.now(timezone.utc),
                    updated_at=datetime.now(timezone.utc),
                    created_by=user_id,
                    change_description=change_description,
                    previous_version_id=previous_version_id
                )
                
                data = {
                    'version_id': version.version_id,
                    'agent_id': version.agent_id,
                    'version_number': version.version_number,
                    'version_name': version.version_name,
                    'is_active': version.is_active,
                    'created_at': version.created_at.isoformat(),
                    'updated_at': version.updated_at.isoformat(),
                    'created_by': version.created_by,
                    'change_description': version.change_description,
                    'previous_version_id': version.previous_version_id,
                    'config': {
                        'system_prompt': version.system_prompt,
                        'model': version.model,
                        'tools': {
                            'agentpress': version.agentpress_tools,
                            'mcp': version.configured_mcps,
                            'custom_mcp': normalized_custom_mcps
                        },
                        'triggers': triggers
                    }
                }
                
                from core.versioning import repo as versioning_repo
                
                await versioning_repo.create_agent_version_with_config(
                    version_id=version.version_id,
                    agent_id=version.agent_id,
                    version_number=version.version_number,
                    version_name=version.version_name,
                    system_prompt=version.system_prompt,
                    model=version.model,
                    configured_mcps=version.configured_mcps,
                    custom_mcps=normalized_custom_mcps,
                    agentpress_tools=version.agentpress_tools,
                    triggers=triggers,
                    created_by=version.created_by,
                    change_description=version.change_description,
                    previous_version_id=version.previous_version_id
                )
                
                version_count = await self._count_versions(agent_id)
                await self._update_agent_current_version(agent_id, version.version_id, version_count)
                
                try:
                    from core.cache.runtime_cache import invalidate_agent_config_cache
                    await invalidate_agent_config_cache(agent_id)
                    logger.debug(f"🗑️ Invalidated cache for agent {agent_id} after version create")
                except Exception as e:
                    logger.warning(f"Failed to invalidate cache for agent {agent_id}: {e}")
                
                logger.debug(f"Created version {version.version_name} for agent {agent_id}")
                return version
                
            except Exception as e:
                error_msg = str(e)
                # Handle both version_number and version_name unique constraint violations
                is_version_conflict = (
                    "duplicate key value violates unique constraint" in error_msg and 
                    ("agent_versions_agent_id_version_number_key" in error_msg or 
                     "agent_versions_agent_id_version_name_key" in error_msg)
                )
                if is_version_conflict:
                    if attempt < max_retries - 1:
                        await asyncio.sleep(0.1 * (attempt + 1))
                        logger.warning(f"Version conflict for agent {agent_id}, attempt {attempt + 1}/{max_retries}, retrying...")
                        continue
                    else:
                        logger.error(f"Failed to create version after {max_retries} attempts due to version conflicts")
                        raise VersionConflictError("Unable to create version due to concurrent modifications. Please try again.")
                else:
                    raise e
    
    async def get_version(self, agent_id: str, version_id: str, user_id: str) -> AgentVersion:
        is_owner, is_public = await self._verify_and_authorize_agent_access(agent_id, user_id)
        if not is_owner and not is_public:
            raise UnauthorizedError("You don't have permission to view this version")
        
        from core.versioning import repo as versioning_repo
        
        result = await versioning_repo.get_agent_version_by_id(agent_id, version_id)
        
        if not result:
            raise VersionNotFoundError(f"Version {version_id} not found")
        
        return self._version_from_db_row(result)
    
    async def get_active_version(self, agent_id: str, user_id: str = "system") -> Optional[AgentVersion]:
        is_owner, is_public = await self._verify_and_authorize_agent_access(agent_id, user_id)
        if not is_owner and not is_public:
            raise UnauthorizedError("You don't have permission to view this agent")
        
        from core.versioning import repo as versioning_repo
        
        result = await versioning_repo.get_agent_current_version(agent_id)
        
        if not result:
            logger.warning(f"No current version found for agent {agent_id}")
            return None
        
        version = self._version_from_db_row(result)
        logger.debug(f"Retrieved active version for agent {agent_id}: model='{version.model}', version_name='{version.version_name}'")
        return version
    
    async def get_all_versions(self, agent_id: str, user_id: str) -> List[AgentVersion]:
        is_owner, is_public = await self._verify_and_authorize_agent_access(agent_id, user_id)
        if not is_owner and not is_public:
            raise UnauthorizedError("You don't have permission to view versions")
        
        from core.versioning import repo as versioning_repo
        
        rows = await versioning_repo.get_agent_versions_list(agent_id)
        versions = [self._version_from_db_row(row) for row in rows]
        return versions
    
    async def activate_version(self, agent_id: str, version_id: str, user_id: str) -> None:
        is_owner, _ = await self._verify_and_authorize_agent_access(agent_id, user_id)
        if not is_owner:
            raise UnauthorizedError("You don't have permission to activate versions")
        
        from core.versioning import repo as versioning_repo
        
        version_data = await versioning_repo.get_agent_version_by_id(agent_id, version_id)
        
        if not version_data:
            raise VersionNotFoundError(f"Version {version_id} not found")
        
        # Deactivate all versions, then activate the target
        await versioning_repo.deactivate_agent_versions(agent_id)
        await versioning_repo.activate_agent_version(version_id)
        
        version_count = await self._count_versions(agent_id)
        await self._update_agent_current_version(agent_id, version_id, version_count)
        
        # Invalidate agent config cache (active version changed)
        try:
            from core.cache.runtime_cache import invalidate_agent_config_cache
            await invalidate_agent_config_cache(agent_id)
            logger.debug(f"🗑️ Invalidated cache for agent {agent_id} after version activate")
        except Exception as e:
            logger.warning(f"Failed to invalidate cache for agent {agent_id}: {e}")
        
        logger.debug(f"Activated version {version_data['version_name']} for agent {agent_id}")
        
    async def compare_versions(
        self,
        agent_id: str,
        version1_id: str,
        version2_id: str,
        user_id: str
    ) -> Dict[str, Any]:
        version1 = await self.get_version(agent_id, version1_id, user_id)
        version2 = await self.get_version(agent_id, version2_id, user_id)
        
        differences = self._calculate_differences(version1, version2)
        
        return {
            'version1': version1.to_dict(),
            'version2': version2.to_dict(),
            'differences': differences
        }
    
    def _calculate_differences(self, v1: AgentVersion, v2: AgentVersion) -> List[Dict[str, Any]]:
        differences = []
        
        if v1.system_prompt != v2.system_prompt:
            differences.append({
                'field': 'system_prompt',
                'type': 'modified',
                'old_value': v1.system_prompt,
                'new_value': v2.system_prompt
            })
        
        if v1.model != v2.model:
            differences.append({
                'field': 'model',
                'type': 'modified',
                'old_value': v1.model,
                'new_value': v2.model
            })
        
        v1_tools = set(v1.agentpress_tools.keys())
        v2_tools = set(v2.agentpress_tools.keys())
        
        for tool in v2_tools - v1_tools:
            differences.append({
                'field': f'tool.{tool}',
                'type': 'added',
                'new_value': v2.agentpress_tools[tool]
            })
        
        for tool in v1_tools - v2_tools:
            differences.append({
                'field': f'tool.{tool}',
                'type': 'removed',
                'old_value': v1.agentpress_tools[tool]
            })
        
        for tool in v1_tools & v2_tools:
            if v1.agentpress_tools[tool] != v2.agentpress_tools[tool]:
                differences.append({
                    'field': f'tool.{tool}',
                    'type': 'modified',
                    'old_value': v1.agentpress_tools[tool],
                    'new_value': v2.agentpress_tools[tool]
                })
        
        return differences
    
    async def rollback_to_version(
        self,
        agent_id: str,
        version_id: str,
        user_id: str
    ) -> AgentVersion:
        version_to_restore = await self.get_version(agent_id, version_id, user_id)
        
        is_owner, _ = await self._verify_and_authorize_agent_access(agent_id, user_id)
        if not is_owner:
            raise UnauthorizedError("You don't have permission to rollback versions")
        
        new_version = await self.create_version(
            agent_id=agent_id,
            user_id=user_id,
            system_prompt=version_to_restore.system_prompt,
            configured_mcps=version_to_restore.configured_mcps,
            custom_mcps=version_to_restore.custom_mcps,
            agentpress_tools=version_to_restore.agentpress_tools,
            model=version_to_restore.model,
            change_description=f"Rolled back to version {version_to_restore.version_name}"
        )
        
        return new_version
    
    async def get_current_mcp_config(self, agent_id: str, user_id: str = "system") -> Optional[Dict[str, Any]]:
        import time
        start_time = time.time()
        
        from core.cache.runtime_cache import (
            get_cached_mcp_version_config,
            set_cached_mcp_version_config
        )
        
        t1 = time.time()
        cached = await get_cached_mcp_version_config(agent_id)
        cache_time = (time.time() - t1) * 1000
        
        if cached:
            logger.debug(f"⚡ [MCP CONFIG] Cache HIT for {agent_id} in {cache_time:.1f}ms")
            cached['account_id'] = user_id
            return cached
        
        logger.debug(f"⏱️ [MCP CONFIG] Cache MISS for {agent_id}, fetching from DB (cache check: {cache_time:.1f}ms)")
        
        try:
            t2 = time.time()
            client = await self._get_client()
            client_time = (time.time() - t2) * 1000
            logger.debug(f"⏱️ [MCP CONFIG] DB client acquired in {client_time:.1f}ms")
            
            t3 = time.time()
            rpc_result = await client.rpc('get_agent_mcp_config', {'p_agent_id': agent_id}).execute()
            rpc_time = (time.time() - t3) * 1000
            logger.debug(f"⏱️ [MCP CONFIG] RPC query: {rpc_time:.1f}ms")
            
            if not rpc_result.data:
                logger.warning(f"⚠️ [MCP CONFIG] No config found for agent {agent_id}, using empty")
                empty_config = {'custom_mcp': [], 'configured_mcps': [], 'account_id': user_id}
                await set_cached_mcp_version_config(agent_id, {'custom_mcp': [], 'configured_mcps': []})
                return empty_config
            
            result_data = rpc_result.data
            if isinstance(result_data, list) and len(result_data) > 0:
                result_data = result_data[0]
            
            custom_mcps = result_data.get('custom_mcp', []) if result_data else []
            configured_mcps = result_data.get('configured_mcps', []) if result_data else []
            
            cache_data = {
                'custom_mcp': custom_mcps,
                'configured_mcps': configured_mcps,
            }
            
            t4 = time.time()
            await set_cached_mcp_version_config(agent_id, cache_data)
            cache_set_time = (time.time() - t4) * 1000
            
            total_time = (time.time() - start_time) * 1000
            logger.info(f"✅ [MCP CONFIG] Loaded for {agent_id} in {total_time:.1f}ms (client:{client_time:.0f}ms, rpc:{rpc_time:.0f}ms, cache_set:{cache_set_time:.0f}ms)")
            
            final_config = {
                'custom_mcp': custom_mcps,
                'configured_mcps': configured_mcps,
                'account_id': user_id
            }
            
            return final_config
            
        except Exception as e:
            total_time = (time.time() - start_time) * 1000
            logger.error(f"❌ [MCP CONFIG] Error loading for agent {agent_id} after {total_time:.1f}ms: {e}", exc_info=True)
            return {'custom_mcp': [], 'configured_mcps': [], 'account_id': user_id}

    async def update_version_details(
        self,
        agent_id: str,
        version_id: str,
        user_id: str,
        version_name: Optional[str] = None,
        change_description: Optional[str] = None
    ) -> AgentVersion:
        is_owner, _ = await self._verify_and_authorize_agent_access(agent_id, user_id)
        if not is_owner:
            raise UnauthorizedError("You don't have permission to update this version")
        
        client = await self._get_client()
        
        version_result = await client.table('agent_versions').select('*').eq(
            'version_id', version_id
        ).eq('agent_id', agent_id).execute()
        
        if not version_result.data:
            raise VersionNotFoundError(f"Version {version_id} not found")
        
        update_data = {
            'updated_at': datetime.now(timezone.utc).isoformat()
        }
        
        if version_name is not None:
            update_data['version_name'] = version_name
        if change_description is not None:
            update_data['change_description'] = change_description
        
        result = await client.table('agent_versions').update(update_data).eq(
            'version_id', version_id
        ).execute()
        
        if not result.data:
            raise Exception("Failed to update version")
        
        return self._version_from_db_row(result.data[0])


_version_service_instance = None

async def get_version_service() -> VersionService:
    global _version_service_instance
    if _version_service_instance is None:
        _version_service_instance = VersionService()
    return _version_service_instance