shotgun-sh 0.2.11.dev5__py3-none-any.whl → 0.2.17.dev1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of shotgun-sh might be problematic. Click here for more details.
- shotgun/agents/agent_manager.py +44 -1
- shotgun/agents/history/history_processors.py +99 -3
- shotgun/agents/history/token_counting/openai.py +3 -1
- shotgun/build_constants.py +1 -1
- shotgun/exceptions.py +32 -0
- shotgun/posthog_telemetry.py +18 -25
- shotgun/prompts/agents/partials/common_agent_system_prompt.j2 +3 -2
- shotgun/sentry_telemetry.py +157 -1
- shotgun/tui/screens/chat/chat_screen.py +85 -33
- shotgun/tui/screens/chat_screen/history/chat_history.py +1 -2
- shotgun/tui/widgets/widget_coordinator.py +3 -2
- shotgun_sh-0.2.17.dev1.dist-info/METADATA +465 -0
- {shotgun_sh-0.2.11.dev5.dist-info → shotgun_sh-0.2.17.dev1.dist-info}/RECORD +16 -15
- shotgun_sh-0.2.11.dev5.dist-info/METADATA +0 -130
- {shotgun_sh-0.2.11.dev5.dist-info → shotgun_sh-0.2.17.dev1.dist-info}/WHEEL +0 -0
- {shotgun_sh-0.2.11.dev5.dist-info → shotgun_sh-0.2.17.dev1.dist-info}/entry_points.txt +0 -0
- {shotgun_sh-0.2.11.dev5.dist-info → shotgun_sh-0.2.17.dev1.dist-info}/licenses/LICENSE +0 -0
shotgun/agents/agent_manager.py
CHANGED
|
@@ -58,7 +58,12 @@ from shotgun.agents.context_analyzer import (
|
|
|
58
58
|
ContextCompositionTelemetry,
|
|
59
59
|
ContextFormatter,
|
|
60
60
|
)
|
|
61
|
-
from shotgun.agents.models import
|
|
61
|
+
from shotgun.agents.models import (
|
|
62
|
+
AgentResponse,
|
|
63
|
+
AgentType,
|
|
64
|
+
FileOperation,
|
|
65
|
+
FileOperationTracker,
|
|
66
|
+
)
|
|
62
67
|
from shotgun.posthog_telemetry import track_event
|
|
63
68
|
from shotgun.tui.screens.chat_screen.hint_message import HintMessage
|
|
64
69
|
from shotgun.utils.source_detection import detect_source
|
|
@@ -769,6 +774,12 @@ class AgentManager(Widget):
|
|
|
769
774
|
HintMessage(message=agent_response.response)
|
|
770
775
|
)
|
|
771
776
|
|
|
777
|
+
# Add file operation hints before questions (so they appear first in UI)
|
|
778
|
+
if file_operations:
|
|
779
|
+
file_hint = self._create_file_operation_hint(file_operations)
|
|
780
|
+
if file_hint:
|
|
781
|
+
self.ui_message_history.append(HintMessage(message=file_hint))
|
|
782
|
+
|
|
772
783
|
if len(agent_response.clarifying_questions) == 1:
|
|
773
784
|
# Single question - treat as non-blocking suggestion, DON'T enter Q&A mode
|
|
774
785
|
self.ui_message_history.append(
|
|
@@ -1134,6 +1145,38 @@ class AgentManager(Widget):
|
|
|
1134
1145
|
)
|
|
1135
1146
|
)
|
|
1136
1147
|
|
|
1148
|
+
def _create_file_operation_hint(
|
|
1149
|
+
self, file_operations: list[FileOperation]
|
|
1150
|
+
) -> str | None:
|
|
1151
|
+
"""Create a hint message for file operations.
|
|
1152
|
+
|
|
1153
|
+
Args:
|
|
1154
|
+
file_operations: List of file operations to create a hint for
|
|
1155
|
+
|
|
1156
|
+
Returns:
|
|
1157
|
+
Hint message string or None if no operations
|
|
1158
|
+
"""
|
|
1159
|
+
if not file_operations:
|
|
1160
|
+
return None
|
|
1161
|
+
|
|
1162
|
+
tracker = FileOperationTracker(operations=file_operations)
|
|
1163
|
+
display_path = tracker.get_display_path()
|
|
1164
|
+
|
|
1165
|
+
if not display_path:
|
|
1166
|
+
return None
|
|
1167
|
+
|
|
1168
|
+
path_obj = Path(display_path)
|
|
1169
|
+
|
|
1170
|
+
if len(file_operations) == 1:
|
|
1171
|
+
return f"📝 Modified: `{display_path}`"
|
|
1172
|
+
else:
|
|
1173
|
+
num_files = len({op.file_path for op in file_operations})
|
|
1174
|
+
if path_obj.is_dir():
|
|
1175
|
+
return f"📁 Modified {num_files} files in: `{display_path}`"
|
|
1176
|
+
else:
|
|
1177
|
+
# Common path is a file, show parent directory
|
|
1178
|
+
return f"📁 Modified {num_files} files in: `{path_obj.parent}`"
|
|
1179
|
+
|
|
1137
1180
|
def _post_messages_updated(
|
|
1138
1181
|
self, file_operations: list[FileOperation] | None = None
|
|
1139
1182
|
) -> None:
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"""History processors for managing conversation history in Shotgun agents."""
|
|
2
2
|
|
|
3
|
+
from collections.abc import Awaitable, Callable
|
|
3
4
|
from typing import TYPE_CHECKING, Any, Protocol
|
|
4
5
|
|
|
6
|
+
from anthropic import APIStatusError
|
|
5
7
|
from pydantic_ai import ModelSettings
|
|
6
8
|
from pydantic_ai.messages import (
|
|
7
9
|
ModelMessage,
|
|
@@ -14,6 +16,7 @@ from pydantic_ai.messages import (
|
|
|
14
16
|
from shotgun.agents.llm import shotgun_model_request
|
|
15
17
|
from shotgun.agents.messages import AgentSystemPrompt, SystemStatusPrompt
|
|
16
18
|
from shotgun.agents.models import AgentDeps
|
|
19
|
+
from shotgun.exceptions import ContextSizeLimitExceeded
|
|
17
20
|
from shotgun.logging_config import get_logger
|
|
18
21
|
from shotgun.posthog_telemetry import track_event
|
|
19
22
|
from shotgun.prompts import PromptLoader
|
|
@@ -51,6 +54,86 @@ logger = get_logger(__name__)
|
|
|
51
54
|
prompt_loader = PromptLoader()
|
|
52
55
|
|
|
53
56
|
|
|
57
|
+
async def _safe_token_estimation(
|
|
58
|
+
estimation_func: Callable[..., Awaitable[int]],
|
|
59
|
+
model_name: str,
|
|
60
|
+
max_tokens: int,
|
|
61
|
+
*args: Any,
|
|
62
|
+
**kwargs: Any,
|
|
63
|
+
) -> int:
|
|
64
|
+
"""Safely estimate tokens with proper error handling.
|
|
65
|
+
|
|
66
|
+
Wraps token estimation functions to handle failures gracefully.
|
|
67
|
+
Only RuntimeError (from token counters) is wrapped in ContextSizeLimitExceeded.
|
|
68
|
+
Other errors (network, auth) are allowed to bubble up.
|
|
69
|
+
|
|
70
|
+
Args:
|
|
71
|
+
estimation_func: Async function that estimates tokens
|
|
72
|
+
model_name: Name of the model for error messages
|
|
73
|
+
max_tokens: Maximum tokens for the model
|
|
74
|
+
*args: Arguments to pass to estimation_func
|
|
75
|
+
**kwargs: Keyword arguments to pass to estimation_func
|
|
76
|
+
|
|
77
|
+
Returns:
|
|
78
|
+
Token count from estimation_func
|
|
79
|
+
|
|
80
|
+
Raises:
|
|
81
|
+
ContextSizeLimitExceeded: If token counting fails with RuntimeError
|
|
82
|
+
Exception: Any other exceptions from estimation_func
|
|
83
|
+
"""
|
|
84
|
+
try:
|
|
85
|
+
return await estimation_func(*args, **kwargs)
|
|
86
|
+
except Exception as e:
|
|
87
|
+
# Log the error with full context
|
|
88
|
+
logger.warning(
|
|
89
|
+
f"Token counting failed for {model_name}",
|
|
90
|
+
extra={
|
|
91
|
+
"error_type": type(e).__name__,
|
|
92
|
+
"error_message": str(e),
|
|
93
|
+
"model": model_name,
|
|
94
|
+
},
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
# Token counting behavior with oversized context (verified via testing):
|
|
98
|
+
#
|
|
99
|
+
# 1. OpenAI/tiktoken:
|
|
100
|
+
# - Successfully counts any size (tested with 752K tokens, no error)
|
|
101
|
+
# - Library errors: ValueError, KeyError, AttributeError, SSLError (file/cache issues)
|
|
102
|
+
# - Wrapped as: RuntimeError by our counter
|
|
103
|
+
#
|
|
104
|
+
# 2. Gemini/SentencePiece:
|
|
105
|
+
# - Successfully counts any size (tested with 752K tokens, no error)
|
|
106
|
+
# - Library errors: RuntimeError, IOError, TypeError (file/model loading issues)
|
|
107
|
+
# - Wrapped as: RuntimeError by our counter
|
|
108
|
+
#
|
|
109
|
+
# 3. Anthropic API:
|
|
110
|
+
# - Successfully counts large token counts (tested with 752K tokens, no error)
|
|
111
|
+
# - Only enforces 32 MB request size limit (not token count)
|
|
112
|
+
# - Raises: APIStatusError(413) with error type 'request_too_large' for 32MB+ requests
|
|
113
|
+
# - Other API errors: APIConnectionError, RateLimitError, APIStatusError (4xx/5xx)
|
|
114
|
+
# - Wrapped as: RuntimeError by our counter
|
|
115
|
+
#
|
|
116
|
+
# IMPORTANT: No provider raises errors for "too many tokens" during counting.
|
|
117
|
+
# Token count validation happens separately by comparing count to max_input_tokens.
|
|
118
|
+
#
|
|
119
|
+
# We wrap RuntimeError (library-level failures from tiktoken/sentencepiece).
|
|
120
|
+
# We also wrap Anthropic's 413 error (request exceeds 32 MB) as it indicates
|
|
121
|
+
# context is effectively too large and needs user action to reduce it.
|
|
122
|
+
if isinstance(e, RuntimeError):
|
|
123
|
+
raise ContextSizeLimitExceeded(
|
|
124
|
+
model_name=model_name, max_tokens=max_tokens
|
|
125
|
+
) from e
|
|
126
|
+
|
|
127
|
+
# Check for Anthropic's 32 MB request size limit (APIStatusError with status 413)
|
|
128
|
+
if isinstance(e, APIStatusError) and e.status_code == 413:
|
|
129
|
+
raise ContextSizeLimitExceeded(
|
|
130
|
+
model_name=model_name, max_tokens=max_tokens
|
|
131
|
+
) from e
|
|
132
|
+
|
|
133
|
+
# Re-raise other exceptions (network errors, auth failures, etc.)
|
|
134
|
+
raise
|
|
135
|
+
|
|
136
|
+
|
|
54
137
|
def is_summary_part(part: Any) -> bool:
|
|
55
138
|
"""Check if a message part is a compacted summary."""
|
|
56
139
|
return isinstance(part, TextPart) and part.content.startswith(SUMMARY_MARKER)
|
|
@@ -157,9 +240,15 @@ async def token_limit_compactor(
|
|
|
157
240
|
|
|
158
241
|
if last_summary_index is not None:
|
|
159
242
|
# Check if post-summary conversation exceeds threshold for incremental compaction
|
|
160
|
-
post_summary_tokens = await
|
|
161
|
-
|
|
243
|
+
post_summary_tokens = await _safe_token_estimation(
|
|
244
|
+
estimate_post_summary_tokens,
|
|
245
|
+
deps.llm_model.name,
|
|
246
|
+
model_max_tokens,
|
|
247
|
+
messages,
|
|
248
|
+
last_summary_index,
|
|
249
|
+
deps.llm_model,
|
|
162
250
|
)
|
|
251
|
+
|
|
163
252
|
post_summary_percentage = (
|
|
164
253
|
(post_summary_tokens / max_tokens) * 100 if max_tokens > 0 else 0
|
|
165
254
|
)
|
|
@@ -366,7 +455,14 @@ async def token_limit_compactor(
|
|
|
366
455
|
|
|
367
456
|
else:
|
|
368
457
|
# Check if total conversation exceeds threshold for full compaction
|
|
369
|
-
total_tokens = await
|
|
458
|
+
total_tokens = await _safe_token_estimation(
|
|
459
|
+
estimate_tokens_from_messages,
|
|
460
|
+
deps.llm_model.name,
|
|
461
|
+
model_max_tokens,
|
|
462
|
+
messages,
|
|
463
|
+
deps.llm_model,
|
|
464
|
+
)
|
|
465
|
+
|
|
370
466
|
total_percentage = (total_tokens / max_tokens) * 100 if max_tokens > 0 else 0
|
|
371
467
|
|
|
372
468
|
logger.debug(
|
|
@@ -63,7 +63,9 @@ class OpenAITokenCounter(TokenCounter):
|
|
|
63
63
|
|
|
64
64
|
try:
|
|
65
65
|
return len(self.encoding.encode(text))
|
|
66
|
-
except
|
|
66
|
+
except BaseException as e:
|
|
67
|
+
# Must catch BaseException to handle PanicException from tiktoken's Rust layer
|
|
68
|
+
# which can occur with extremely long texts. Regular Exception won't catch it.
|
|
67
69
|
raise RuntimeError(
|
|
68
70
|
f"Failed to count tokens for OpenAI model {self.model_name}"
|
|
69
71
|
) from e
|
shotgun/build_constants.py
CHANGED
|
@@ -8,7 +8,7 @@ DO NOT EDIT MANUALLY.
|
|
|
8
8
|
SENTRY_DSN = 'https://2818a6d165c64eccc94cfd51ce05d6aa@o4506813296738304.ingest.us.sentry.io/4510045952409600'
|
|
9
9
|
|
|
10
10
|
# PostHog configuration embedded at build time (empty strings if not provided)
|
|
11
|
-
POSTHOG_API_KEY = ''
|
|
11
|
+
POSTHOG_API_KEY = 'phc_KKnChzZUKeNqZDOTJ6soCBWNQSx3vjiULdwTR9H5Mcr'
|
|
12
12
|
POSTHOG_PROJECT_ID = '191396'
|
|
13
13
|
|
|
14
14
|
# Logfire configuration embedded at build time (only for dev builds)
|
shotgun/exceptions.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"""General exceptions for Shotgun application."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class ErrorNotPickedUpBySentry(Exception): # noqa: N818
|
|
5
|
+
"""Base for user-actionable errors that shouldn't be sent to Sentry.
|
|
6
|
+
|
|
7
|
+
These errors represent expected user conditions requiring action
|
|
8
|
+
rather than bugs that need tracking.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class ContextSizeLimitExceeded(ErrorNotPickedUpBySentry):
|
|
13
|
+
"""Raised when conversation context exceeds the model's limits.
|
|
14
|
+
|
|
15
|
+
This is a user-actionable error - they need to either:
|
|
16
|
+
1. Switch to a larger context model
|
|
17
|
+
2. Switch to a larger model, compact their conversation, then switch back
|
|
18
|
+
3. Clear the conversation and start fresh
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
def __init__(self, model_name: str, max_tokens: int):
|
|
22
|
+
"""Initialize the exception.
|
|
23
|
+
|
|
24
|
+
Args:
|
|
25
|
+
model_name: Name of the model whose limit was exceeded
|
|
26
|
+
max_tokens: Maximum tokens allowed by the model
|
|
27
|
+
"""
|
|
28
|
+
self.model_name = model_name
|
|
29
|
+
self.max_tokens = max_tokens
|
|
30
|
+
super().__init__(
|
|
31
|
+
f"Context too large for {model_name} (limit: {max_tokens:,} tokens)"
|
|
32
|
+
)
|
shotgun/posthog_telemetry.py
CHANGED
|
@@ -18,6 +18,9 @@ logger = get_early_logger(__name__)
|
|
|
18
18
|
# Global PostHog client instance
|
|
19
19
|
_posthog_client = None
|
|
20
20
|
|
|
21
|
+
# Cache the shotgun instance ID to avoid async calls during event tracking
|
|
22
|
+
_shotgun_instance_id: str | None = None
|
|
23
|
+
|
|
21
24
|
|
|
22
25
|
def setup_posthog_observability() -> bool:
|
|
23
26
|
"""Set up PostHog analytics for usage tracking.
|
|
@@ -25,7 +28,7 @@ def setup_posthog_observability() -> bool:
|
|
|
25
28
|
Returns:
|
|
26
29
|
True if PostHog was successfully set up, False otherwise
|
|
27
30
|
"""
|
|
28
|
-
global _posthog_client
|
|
31
|
+
global _posthog_client, _shotgun_instance_id
|
|
29
32
|
|
|
30
33
|
try:
|
|
31
34
|
# Check if PostHog is already initialized
|
|
@@ -57,31 +60,20 @@ def setup_posthog_observability() -> bool:
|
|
|
57
60
|
# Store the client for later use
|
|
58
61
|
_posthog_client = posthog
|
|
59
62
|
|
|
60
|
-
#
|
|
63
|
+
# Cache the shotgun instance ID for later use (avoids async issues)
|
|
61
64
|
try:
|
|
62
65
|
import asyncio
|
|
63
66
|
|
|
64
67
|
config_manager = get_config_manager()
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
# Identify the user in PostHog
|
|
68
|
-
posthog.identify( # type: ignore[attr-defined]
|
|
69
|
-
distinct_id=shotgun_instance_id,
|
|
70
|
-
properties={
|
|
71
|
-
"version": __version__,
|
|
72
|
-
"environment": environment,
|
|
73
|
-
},
|
|
74
|
-
)
|
|
75
|
-
|
|
76
|
-
# Set default properties for all events
|
|
77
|
-
posthog.disabled = False
|
|
78
|
-
posthog.personal_api_key = None # Not needed for event tracking
|
|
68
|
+
_shotgun_instance_id = asyncio.run(config_manager.get_shotgun_instance_id())
|
|
79
69
|
|
|
80
70
|
logger.debug(
|
|
81
|
-
"PostHog
|
|
71
|
+
"PostHog initialized with shotgun instance ID: %s",
|
|
72
|
+
_shotgun_instance_id,
|
|
82
73
|
)
|
|
83
74
|
except Exception as e:
|
|
84
|
-
logger.warning("Failed to
|
|
75
|
+
logger.warning("Failed to load shotgun instance ID: %s", e)
|
|
76
|
+
# Continue anyway - we'll try to get it during event tracking
|
|
85
77
|
|
|
86
78
|
logger.debug(
|
|
87
79
|
"PostHog analytics configured successfully (environment: %s, version: %s)",
|
|
@@ -102,18 +94,19 @@ def track_event(event_name: str, properties: dict[str, Any] | None = None) -> No
|
|
|
102
94
|
event_name: Name of the event to track
|
|
103
95
|
properties: Optional properties to include with the event
|
|
104
96
|
"""
|
|
105
|
-
global _posthog_client
|
|
97
|
+
global _posthog_client, _shotgun_instance_id
|
|
106
98
|
|
|
107
99
|
if _posthog_client is None:
|
|
108
100
|
logger.debug("PostHog not initialized, skipping event: %s", event_name)
|
|
109
101
|
return
|
|
110
102
|
|
|
111
103
|
try:
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
104
|
+
# Use cached instance ID (loaded during setup)
|
|
105
|
+
if _shotgun_instance_id is None:
|
|
106
|
+
logger.warning(
|
|
107
|
+
"Shotgun instance ID not available, skipping event: %s", event_name
|
|
108
|
+
)
|
|
109
|
+
return
|
|
117
110
|
|
|
118
111
|
# Add version and environment to properties
|
|
119
112
|
if properties is None:
|
|
@@ -128,7 +121,7 @@ def track_event(event_name: str, properties: dict[str, Any] | None = None) -> No
|
|
|
128
121
|
|
|
129
122
|
# Track the event using PostHog's capture method
|
|
130
123
|
_posthog_client.capture(
|
|
131
|
-
distinct_id=
|
|
124
|
+
distinct_id=_shotgun_instance_id, event=event_name, properties=properties
|
|
132
125
|
)
|
|
133
126
|
logger.debug("Tracked PostHog event: %s", event_name)
|
|
134
127
|
except Exception as e:
|
|
@@ -7,10 +7,11 @@ Your extensive expertise spans, among other things:
|
|
|
7
7
|
## KEY RULES
|
|
8
8
|
|
|
9
9
|
{% if interactive_mode %}
|
|
10
|
-
0. Always ask CLARIFYING QUESTIONS using structured output
|
|
10
|
+
0. Always ask CLARIFYING QUESTIONS using structured output before doing work.
|
|
11
11
|
- Return your response with the clarifying_questions field populated
|
|
12
|
-
- Do not make assumptions about what the user wants
|
|
12
|
+
- Do not make assumptions about what the user wants, get a clear understanding first.
|
|
13
13
|
- Questions should be clear, specific, and answerable
|
|
14
|
+
- Do not ask too many questions that might overwhelm the user; prioritize the most important ones.
|
|
14
15
|
{% endif %}
|
|
15
16
|
1. Above all, prefer using tools to do the work and NEVER respond with text.
|
|
16
17
|
2. IMPORTANT: Always ask for review and go ahead to move forward after using write_file().
|
shotgun/sentry_telemetry.py
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
"""Sentry observability setup for Shotgun."""
|
|
2
2
|
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
3
6
|
from shotgun import __version__
|
|
4
7
|
from shotgun.logging_config import get_early_logger
|
|
5
8
|
from shotgun.settings import settings
|
|
@@ -8,6 +11,122 @@ from shotgun.settings import settings
|
|
|
8
11
|
logger = get_early_logger(__name__)
|
|
9
12
|
|
|
10
13
|
|
|
14
|
+
def _scrub_path(path: str) -> str:
|
|
15
|
+
"""Scrub sensitive information from file paths.
|
|
16
|
+
|
|
17
|
+
Removes home directory and current working directory prefixes to prevent
|
|
18
|
+
leaking usernames that might be part of the path.
|
|
19
|
+
|
|
20
|
+
Args:
|
|
21
|
+
path: The file path to scrub
|
|
22
|
+
|
|
23
|
+
Returns:
|
|
24
|
+
The scrubbed path with sensitive prefixes removed
|
|
25
|
+
"""
|
|
26
|
+
if not path:
|
|
27
|
+
return path
|
|
28
|
+
|
|
29
|
+
try:
|
|
30
|
+
# Get home and cwd as Path objects for comparison
|
|
31
|
+
home = Path.home()
|
|
32
|
+
cwd = Path.cwd()
|
|
33
|
+
|
|
34
|
+
# Convert path to Path object
|
|
35
|
+
path_obj = Path(path)
|
|
36
|
+
|
|
37
|
+
# Try to make path relative to cwd first (most common case)
|
|
38
|
+
try:
|
|
39
|
+
relative_to_cwd = path_obj.relative_to(cwd)
|
|
40
|
+
return str(relative_to_cwd)
|
|
41
|
+
except ValueError:
|
|
42
|
+
pass
|
|
43
|
+
|
|
44
|
+
# Try to replace home directory with ~
|
|
45
|
+
try:
|
|
46
|
+
relative_to_home = path_obj.relative_to(home)
|
|
47
|
+
return f"~/{relative_to_home}"
|
|
48
|
+
except ValueError:
|
|
49
|
+
pass
|
|
50
|
+
|
|
51
|
+
# If path is absolute but not under cwd or home, just return filename
|
|
52
|
+
if path_obj.is_absolute():
|
|
53
|
+
return path_obj.name
|
|
54
|
+
|
|
55
|
+
# Return as-is if already relative
|
|
56
|
+
return path
|
|
57
|
+
|
|
58
|
+
except Exception:
|
|
59
|
+
# If anything goes wrong, return the original path
|
|
60
|
+
# Better to leak a path than break error reporting
|
|
61
|
+
return path
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _scrub_sensitive_paths(event: dict[str, Any]) -> None:
|
|
65
|
+
"""Scrub sensitive paths from Sentry event data.
|
|
66
|
+
|
|
67
|
+
Modifies the event in-place to remove:
|
|
68
|
+
- Home directory paths (might contain usernames)
|
|
69
|
+
- Current working directory paths (might contain usernames)
|
|
70
|
+
- Server name/hostname
|
|
71
|
+
- Paths in sys.argv
|
|
72
|
+
|
|
73
|
+
Args:
|
|
74
|
+
event: The Sentry event dictionary to scrub
|
|
75
|
+
"""
|
|
76
|
+
extra = event.get("extra", {})
|
|
77
|
+
if "sys.argv" in extra:
|
|
78
|
+
argv = extra["sys.argv"]
|
|
79
|
+
if isinstance(argv, list):
|
|
80
|
+
extra["sys.argv"] = [
|
|
81
|
+
_scrub_path(arg) if isinstance(arg, str) else arg for arg in argv
|
|
82
|
+
]
|
|
83
|
+
|
|
84
|
+
# Scrub server name if present
|
|
85
|
+
if "server_name" in event:
|
|
86
|
+
event["server_name"] = ""
|
|
87
|
+
|
|
88
|
+
# Scrub contexts that might contain paths
|
|
89
|
+
if "contexts" in event:
|
|
90
|
+
contexts = event["contexts"]
|
|
91
|
+
# Remove runtime context if it has CWD
|
|
92
|
+
if "runtime" in contexts:
|
|
93
|
+
if "cwd" in contexts["runtime"]:
|
|
94
|
+
del contexts["runtime"]["cwd"]
|
|
95
|
+
# Scrub sys.argv to remove paths
|
|
96
|
+
if "sys.argv" in contexts["runtime"]:
|
|
97
|
+
argv = contexts["runtime"]["sys.argv"]
|
|
98
|
+
if isinstance(argv, list):
|
|
99
|
+
contexts["runtime"]["sys.argv"] = [
|
|
100
|
+
_scrub_path(arg) if isinstance(arg, str) else arg
|
|
101
|
+
for arg in argv
|
|
102
|
+
]
|
|
103
|
+
|
|
104
|
+
# Scrub exception stack traces
|
|
105
|
+
if "exception" in event and "values" in event["exception"]:
|
|
106
|
+
for exception in event["exception"]["values"]:
|
|
107
|
+
if "stacktrace" in exception and "frames" in exception["stacktrace"]:
|
|
108
|
+
for frame in exception["stacktrace"]["frames"]:
|
|
109
|
+
# Scrub file paths
|
|
110
|
+
if "abs_path" in frame:
|
|
111
|
+
frame["abs_path"] = _scrub_path(frame["abs_path"])
|
|
112
|
+
if "filename" in frame:
|
|
113
|
+
frame["filename"] = _scrub_path(frame["filename"])
|
|
114
|
+
|
|
115
|
+
# Scrub local variables that might contain paths
|
|
116
|
+
if "vars" in frame:
|
|
117
|
+
for var_name, var_value in frame["vars"].items():
|
|
118
|
+
if isinstance(var_value, str):
|
|
119
|
+
frame["vars"][var_name] = _scrub_path(var_value)
|
|
120
|
+
|
|
121
|
+
# Scrub breadcrumbs that might contain paths
|
|
122
|
+
if "breadcrumbs" in event and "values" in event["breadcrumbs"]:
|
|
123
|
+
for breadcrumb in event["breadcrumbs"]["values"]:
|
|
124
|
+
if "data" in breadcrumb:
|
|
125
|
+
for key, value in breadcrumb["data"].items():
|
|
126
|
+
if isinstance(value, str):
|
|
127
|
+
breadcrumb["data"][key] = _scrub_path(value)
|
|
128
|
+
|
|
129
|
+
|
|
11
130
|
def setup_sentry_observability() -> bool:
|
|
12
131
|
"""Set up Sentry observability for error tracking.
|
|
13
132
|
|
|
@@ -32,20 +151,57 @@ def setup_sentry_observability() -> bool:
|
|
|
32
151
|
logger.debug("Using Sentry DSN from settings, proceeding with setup")
|
|
33
152
|
|
|
34
153
|
# Determine environment based on version
|
|
35
|
-
# Dev versions contain "dev", "rc", "alpha",
|
|
154
|
+
# Dev versions contain "dev", "rc", "alpha", "beta"
|
|
36
155
|
if any(marker in __version__ for marker in ["dev", "rc", "alpha", "beta"]):
|
|
37
156
|
environment = "development"
|
|
38
157
|
else:
|
|
39
158
|
environment = "production"
|
|
40
159
|
|
|
160
|
+
def before_send(event: Any, hint: dict[str, Any]) -> Any:
|
|
161
|
+
"""Filter out user-actionable errors and scrub sensitive paths.
|
|
162
|
+
|
|
163
|
+
User-actionable errors (like context size limits) are expected conditions
|
|
164
|
+
that users need to resolve, not bugs that need tracking.
|
|
165
|
+
|
|
166
|
+
Also scrubs sensitive information like usernames from file paths and
|
|
167
|
+
working directories to protect user privacy.
|
|
168
|
+
"""
|
|
169
|
+
|
|
170
|
+
log_record = hint.get("log_record")
|
|
171
|
+
if log_record:
|
|
172
|
+
# Scrub pathname using the helper function
|
|
173
|
+
log_record.pathname = _scrub_path(log_record.pathname)
|
|
174
|
+
|
|
175
|
+
# Scrub traceback text if it exists
|
|
176
|
+
if hasattr(log_record, "exc_text") and isinstance(
|
|
177
|
+
log_record.exc_text, str
|
|
178
|
+
):
|
|
179
|
+
# Replace home directory in traceback text
|
|
180
|
+
home = Path.home()
|
|
181
|
+
log_record.exc_text = log_record.exc_text.replace(str(home), "~")
|
|
182
|
+
|
|
183
|
+
if "exc_info" in hint:
|
|
184
|
+
_, exc_value, _ = hint["exc_info"]
|
|
185
|
+
from shotgun.exceptions import ErrorNotPickedUpBySentry
|
|
186
|
+
|
|
187
|
+
if isinstance(exc_value, ErrorNotPickedUpBySentry):
|
|
188
|
+
# Don't send to Sentry - this is user-actionable, not a bug
|
|
189
|
+
return None
|
|
190
|
+
|
|
191
|
+
# Scrub sensitive paths from the event
|
|
192
|
+
_scrub_sensitive_paths(event)
|
|
193
|
+
return event
|
|
194
|
+
|
|
41
195
|
# Initialize Sentry
|
|
42
196
|
sentry_sdk.init(
|
|
43
197
|
dsn=dsn,
|
|
44
198
|
release=f"shotgun-sh@{__version__}",
|
|
45
199
|
environment=environment,
|
|
46
200
|
send_default_pii=False, # Privacy-first: never send PII
|
|
201
|
+
server_name="", # Privacy: don't send hostname (may contain username)
|
|
47
202
|
traces_sample_rate=0.1 if environment == "production" else 1.0,
|
|
48
203
|
profiles_sample_rate=0.1 if environment == "production" else 1.0,
|
|
204
|
+
before_send=before_send,
|
|
49
205
|
)
|
|
50
206
|
|
|
51
207
|
# Set user context with anonymous shotgun instance ID from config
|