|
| 1 | +"""Keep the reported conversation model in sync with the model the IDE is actually using. |
| 2 | +
|
| 3 | +The conversation record - and with it the model the platform attributes every event in that |
| 4 | +conversation to - is created by the session-start hook. Switching models does not start a new |
| 5 | +conversation (Cursor keeps the same ``conversation_id`` when the composer's model changes, and |
| 6 | +no ``sessionStart`` fires), so the model captured at session start goes stale the moment the |
| 7 | +user picks a different one. |
| 8 | +
|
| 9 | +Every hook payload carries the model that is configured *now*, so each hook compares it against |
| 10 | +the model this device last reported for the conversation and re-reports the conversation when it |
| 11 | +changed. The last reported model is cached on disk, which keeps the common case - the model did |
| 12 | +not change - free of any extra request. |
| 13 | +""" |
| 14 | + |
| 15 | +import json |
| 16 | +import os |
| 17 | +import time |
| 18 | +from dataclasses import replace |
| 19 | +from pathlib import Path |
| 20 | +from typing import TYPE_CHECKING |
| 21 | + |
| 22 | +from cycode.cli.exceptions.custom_exceptions import HttpUnauthorizedError |
| 23 | +from cycode.logger import get_logger |
| 24 | + |
| 25 | +if TYPE_CHECKING: |
| 26 | + from cycode.cli.apps.ai_guardrails.scan.payload import AIHookPayload |
| 27 | + from cycode.cyclient.ai_security_manager_client import AISecurityManagerClient |
| 28 | + |
| 29 | +logger = get_logger('AI Guardrails') |
| 30 | + |
| 31 | +_CACHE_FILE_NAME = '.conversation-model-cache' |
| 32 | +# A conversation untouched for a week falls out of the cache; resuming it re-reports the model once. |
| 33 | +_CACHE_TTL_SECONDS = 7 * 24 * 60 * 60 |
| 34 | +_MAX_CACHED_CONVERSATIONS = 200 |
| 35 | + |
| 36 | + |
| 37 | +def _conversation_model_cache_path() -> Path: |
| 38 | + return Path.home() / '.cycode' / _CACHE_FILE_NAME |
| 39 | + |
| 40 | + |
| 41 | +def _seen_at(entry: dict) -> float: |
| 42 | + try: |
| 43 | + return float(entry.get('seen_at', 0)) |
| 44 | + except (TypeError, ValueError): |
| 45 | + return 0.0 |
| 46 | + |
| 47 | + |
| 48 | +def _load_cache() -> dict[str, dict]: |
| 49 | + """Read the cache. A missing or corrupt file reads as empty - over-reporting is harmless.""" |
| 50 | + try: |
| 51 | + cache = json.loads(_conversation_model_cache_path().read_text(encoding='utf-8')) |
| 52 | + except Exception: |
| 53 | + return {} |
| 54 | + |
| 55 | + if not isinstance(cache, dict): |
| 56 | + return {} |
| 57 | + |
| 58 | + return {conversation_id: entry for conversation_id, entry in cache.items() if isinstance(entry, dict)} |
| 59 | + |
| 60 | + |
| 61 | +def _prune(cache: dict[str, dict]) -> dict[str, dict]: |
| 62 | + """Drop expired entries and bound the file by the most recently seen conversations.""" |
| 63 | + now = time.time() |
| 64 | + fresh = { |
| 65 | + conversation_id: entry for conversation_id, entry in cache.items() if now - _seen_at(entry) < _CACHE_TTL_SECONDS |
| 66 | + } |
| 67 | + if len(fresh) <= _MAX_CACHED_CONVERSATIONS: |
| 68 | + return fresh |
| 69 | + |
| 70 | + newest = sorted(fresh.items(), key=lambda item: _seen_at(item[1]), reverse=True) |
| 71 | + return dict(newest[:_MAX_CACHED_CONVERSATIONS]) |
| 72 | + |
| 73 | + |
| 74 | +def _write_cache(cache: dict[str, dict]) -> None: |
| 75 | + path = _conversation_model_cache_path() |
| 76 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 77 | + |
| 78 | + # Hooks of the same generation run concurrently (a prompt and the files it makes the agent read), |
| 79 | + # and a half-written file reads back as a miss for every conversation on the device - swap it in |
| 80 | + # atomically instead. |
| 81 | + tmp_path = path.parent / f'{path.name}.{os.getpid()}.tmp' |
| 82 | + tmp_path.write_text(json.dumps(cache), encoding='utf-8') |
| 83 | + os.replace(tmp_path, path) |
| 84 | + |
| 85 | + |
| 86 | +def remember_reported_model(payload: 'AIHookPayload') -> None: |
| 87 | + """Record the model the backend was last told about for this conversation. Never raises.""" |
| 88 | + if not payload.conversation_id or not payload.model: |
| 89 | + return |
| 90 | + |
| 91 | + try: |
| 92 | + cache = _load_cache() |
| 93 | + cache[payload.conversation_id] = { |
| 94 | + 'model': payload.model, |
| 95 | + 'source': payload.source, |
| 96 | + 'seen_at': time.time(), |
| 97 | + } |
| 98 | + _write_cache(_prune(cache)) |
| 99 | + except Exception as e: |
| 100 | + logger.debug('Failed to update the conversation model cache', exc_info=e) |
| 101 | + |
| 102 | + |
| 103 | +def sync_conversation_model(ai_client: 'AISecurityManagerClient', payload: 'AIHookPayload') -> bool: |
| 104 | + """Re-report the conversation when the IDE switched models since the last reported hook. |
| 105 | +
|
| 106 | + Returns whether the conversation was re-reported. Only ``HttpUnauthorizedError`` propagates |
| 107 | + (the scan command turns it into an auth message); model tracking never fails a hook otherwise. |
| 108 | + """ |
| 109 | + if not payload.conversation_id or not payload.model: |
| 110 | + return False |
| 111 | + |
| 112 | + try: |
| 113 | + cached = _load_cache().get(payload.conversation_id, {}) |
| 114 | + if cached.get('model') == payload.model: |
| 115 | + return False |
| 116 | + |
| 117 | + logger.debug( |
| 118 | + 'Conversation model changed, re-reporting the conversation', |
| 119 | + extra={ |
| 120 | + 'conversation_id': payload.conversation_id, |
| 121 | + 'reported_model': cached.get('model'), |
| 122 | + 'current_model': payload.model, |
| 123 | + }, |
| 124 | + ) |
| 125 | + |
| 126 | + # Hook payloads carry no session `source`; replay the one session start reported so the |
| 127 | + # update does not blank it. |
| 128 | + updated = replace(payload, source=payload.source or cached.get('source')) |
| 129 | + if not ai_client.create_conversation(updated): |
| 130 | + # The report failed: leave the cache alone so the next hook tries again. |
| 131 | + return False |
| 132 | + |
| 133 | + remember_reported_model(updated) |
| 134 | + return True |
| 135 | + except HttpUnauthorizedError: |
| 136 | + raise |
| 137 | + except Exception as e: |
| 138 | + logger.debug('Failed to sync the conversation model', exc_info=e) |
| 139 | + return False |
0 commit comments