shotgun-sh 0.1.14__py3-none-any.whl → 0.2.11__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.

Files changed (143) hide show
  1. shotgun/agents/agent_manager.py +715 -75
  2. shotgun/agents/common.py +80 -75
  3. shotgun/agents/config/constants.py +21 -10
  4. shotgun/agents/config/manager.py +322 -97
  5. shotgun/agents/config/models.py +114 -84
  6. shotgun/agents/config/provider.py +232 -88
  7. shotgun/agents/context_analyzer/__init__.py +28 -0
  8. shotgun/agents/context_analyzer/analyzer.py +471 -0
  9. shotgun/agents/context_analyzer/constants.py +9 -0
  10. shotgun/agents/context_analyzer/formatter.py +115 -0
  11. shotgun/agents/context_analyzer/models.py +212 -0
  12. shotgun/agents/conversation_history.py +125 -2
  13. shotgun/agents/conversation_manager.py +57 -19
  14. shotgun/agents/export.py +6 -7
  15. shotgun/agents/history/compaction.py +10 -5
  16. shotgun/agents/history/context_extraction.py +93 -6
  17. shotgun/agents/history/history_processors.py +129 -12
  18. shotgun/agents/history/token_counting/__init__.py +31 -0
  19. shotgun/agents/history/token_counting/anthropic.py +127 -0
  20. shotgun/agents/history/token_counting/base.py +78 -0
  21. shotgun/agents/history/token_counting/openai.py +90 -0
  22. shotgun/agents/history/token_counting/sentencepiece_counter.py +127 -0
  23. shotgun/agents/history/token_counting/tokenizer_cache.py +92 -0
  24. shotgun/agents/history/token_counting/utils.py +144 -0
  25. shotgun/agents/history/token_estimation.py +12 -12
  26. shotgun/agents/llm.py +62 -0
  27. shotgun/agents/models.py +59 -4
  28. shotgun/agents/plan.py +6 -7
  29. shotgun/agents/research.py +7 -8
  30. shotgun/agents/specify.py +6 -7
  31. shotgun/agents/tasks.py +6 -7
  32. shotgun/agents/tools/__init__.py +0 -2
  33. shotgun/agents/tools/codebase/codebase_shell.py +6 -0
  34. shotgun/agents/tools/codebase/directory_lister.py +6 -0
  35. shotgun/agents/tools/codebase/file_read.py +11 -2
  36. shotgun/agents/tools/codebase/query_graph.py +6 -0
  37. shotgun/agents/tools/codebase/retrieve_code.py +6 -0
  38. shotgun/agents/tools/file_management.py +82 -16
  39. shotgun/agents/tools/registry.py +217 -0
  40. shotgun/agents/tools/web_search/__init__.py +55 -16
  41. shotgun/agents/tools/web_search/anthropic.py +76 -51
  42. shotgun/agents/tools/web_search/gemini.py +50 -27
  43. shotgun/agents/tools/web_search/openai.py +26 -17
  44. shotgun/agents/tools/web_search/utils.py +2 -2
  45. shotgun/agents/usage_manager.py +164 -0
  46. shotgun/api_endpoints.py +15 -0
  47. shotgun/cli/clear.py +53 -0
  48. shotgun/cli/compact.py +186 -0
  49. shotgun/cli/config.py +41 -67
  50. shotgun/cli/context.py +111 -0
  51. shotgun/cli/export.py +1 -1
  52. shotgun/cli/feedback.py +50 -0
  53. shotgun/cli/models.py +3 -2
  54. shotgun/cli/plan.py +1 -1
  55. shotgun/cli/research.py +1 -1
  56. shotgun/cli/specify.py +1 -1
  57. shotgun/cli/tasks.py +1 -1
  58. shotgun/cli/update.py +16 -2
  59. shotgun/codebase/core/change_detector.py +5 -3
  60. shotgun/codebase/core/code_retrieval.py +4 -2
  61. shotgun/codebase/core/ingestor.py +57 -16
  62. shotgun/codebase/core/manager.py +20 -7
  63. shotgun/codebase/core/nl_query.py +1 -1
  64. shotgun/codebase/models.py +4 -4
  65. shotgun/exceptions.py +32 -0
  66. shotgun/llm_proxy/__init__.py +19 -0
  67. shotgun/llm_proxy/clients.py +44 -0
  68. shotgun/llm_proxy/constants.py +15 -0
  69. shotgun/logging_config.py +18 -27
  70. shotgun/main.py +91 -12
  71. shotgun/posthog_telemetry.py +81 -10
  72. shotgun/prompts/agents/export.j2 +18 -1
  73. shotgun/prompts/agents/partials/common_agent_system_prompt.j2 +5 -1
  74. shotgun/prompts/agents/partials/interactive_mode.j2 +24 -7
  75. shotgun/prompts/agents/plan.j2 +1 -1
  76. shotgun/prompts/agents/research.j2 +1 -1
  77. shotgun/prompts/agents/specify.j2 +270 -3
  78. shotgun/prompts/agents/state/system_state.j2 +4 -0
  79. shotgun/prompts/agents/tasks.j2 +1 -1
  80. shotgun/prompts/loader.py +2 -2
  81. shotgun/prompts/tools/web_search.j2 +14 -0
  82. shotgun/sentry_telemetry.py +27 -18
  83. shotgun/settings.py +238 -0
  84. shotgun/shotgun_web/__init__.py +19 -0
  85. shotgun/shotgun_web/client.py +138 -0
  86. shotgun/shotgun_web/constants.py +21 -0
  87. shotgun/shotgun_web/models.py +47 -0
  88. shotgun/telemetry.py +24 -36
  89. shotgun/tui/app.py +251 -23
  90. shotgun/tui/commands/__init__.py +1 -1
  91. shotgun/tui/components/context_indicator.py +179 -0
  92. shotgun/tui/components/mode_indicator.py +70 -0
  93. shotgun/tui/components/status_bar.py +48 -0
  94. shotgun/tui/containers.py +91 -0
  95. shotgun/tui/dependencies.py +39 -0
  96. shotgun/tui/protocols.py +45 -0
  97. shotgun/tui/screens/chat/__init__.py +5 -0
  98. shotgun/tui/screens/chat/chat.tcss +54 -0
  99. shotgun/tui/screens/chat/chat_screen.py +1234 -0
  100. shotgun/tui/screens/chat/codebase_index_prompt_screen.py +64 -0
  101. shotgun/tui/screens/chat/codebase_index_selection.py +12 -0
  102. shotgun/tui/screens/chat/help_text.py +40 -0
  103. shotgun/tui/screens/chat/prompt_history.py +48 -0
  104. shotgun/tui/screens/chat.tcss +11 -0
  105. shotgun/tui/screens/chat_screen/command_providers.py +226 -11
  106. shotgun/tui/screens/chat_screen/history/__init__.py +22 -0
  107. shotgun/tui/screens/chat_screen/history/agent_response.py +66 -0
  108. shotgun/tui/screens/chat_screen/history/chat_history.py +116 -0
  109. shotgun/tui/screens/chat_screen/history/formatters.py +115 -0
  110. shotgun/tui/screens/chat_screen/history/partial_response.py +43 -0
  111. shotgun/tui/screens/chat_screen/history/user_question.py +42 -0
  112. shotgun/tui/screens/confirmation_dialog.py +151 -0
  113. shotgun/tui/screens/feedback.py +193 -0
  114. shotgun/tui/screens/github_issue.py +102 -0
  115. shotgun/tui/screens/model_picker.py +352 -0
  116. shotgun/tui/screens/onboarding.py +431 -0
  117. shotgun/tui/screens/pipx_migration.py +153 -0
  118. shotgun/tui/screens/provider_config.py +156 -39
  119. shotgun/tui/screens/shotgun_auth.py +295 -0
  120. shotgun/tui/screens/welcome.py +198 -0
  121. shotgun/tui/services/__init__.py +5 -0
  122. shotgun/tui/services/conversation_service.py +184 -0
  123. shotgun/tui/state/__init__.py +7 -0
  124. shotgun/tui/state/processing_state.py +185 -0
  125. shotgun/tui/utils/mode_progress.py +14 -7
  126. shotgun/tui/widgets/__init__.py +5 -0
  127. shotgun/tui/widgets/widget_coordinator.py +262 -0
  128. shotgun/utils/datetime_utils.py +77 -0
  129. shotgun/utils/env_utils.py +13 -0
  130. shotgun/utils/file_system_utils.py +22 -2
  131. shotgun/utils/marketing.py +110 -0
  132. shotgun/utils/update_checker.py +69 -14
  133. shotgun_sh-0.2.11.dist-info/METADATA +130 -0
  134. shotgun_sh-0.2.11.dist-info/RECORD +194 -0
  135. {shotgun_sh-0.1.14.dist-info → shotgun_sh-0.2.11.dist-info}/entry_points.txt +1 -0
  136. {shotgun_sh-0.1.14.dist-info → shotgun_sh-0.2.11.dist-info}/licenses/LICENSE +1 -1
  137. shotgun/agents/history/token_counting.py +0 -429
  138. shotgun/agents/tools/user_interaction.py +0 -37
  139. shotgun/tui/screens/chat.py +0 -797
  140. shotgun/tui/screens/chat_screen/history.py +0 -350
  141. shotgun_sh-0.1.14.dist-info/METADATA +0 -466
  142. shotgun_sh-0.1.14.dist-info/RECORD +0 -133
  143. {shotgun_sh-0.1.14.dist-info → shotgun_sh-0.2.11.dist-info}/WHEEL +0 -0
@@ -0,0 +1,471 @@
1
+ """Core context analysis logic."""
2
+
3
+ import json
4
+ from collections.abc import Sequence
5
+
6
+ from pydantic_ai.messages import (
7
+ ModelMessage,
8
+ ModelRequest,
9
+ ModelResponse,
10
+ SystemPromptPart,
11
+ TextPart,
12
+ ToolCallPart,
13
+ ToolReturnPart,
14
+ UserPromptPart,
15
+ )
16
+
17
+ from shotgun.agents.config.models import ModelConfig
18
+ from shotgun.agents.history.token_counting.utils import count_tokens_from_messages
19
+ from shotgun.agents.history.token_estimation import estimate_tokens_from_messages
20
+ from shotgun.agents.messages import AgentSystemPrompt, SystemStatusPrompt
21
+ from shotgun.logging_config import get_logger
22
+ from shotgun.tui.screens.chat_screen.hint_message import HintMessage
23
+
24
+ from .constants import ToolCategory, get_tool_category
25
+ from .models import ContextAnalysis, MessageTypeStats, TokenAllocation
26
+
27
+ logger = get_logger(__name__)
28
+
29
+
30
+ class ContextAnalyzer:
31
+ """Analyzes conversation message history for context composition."""
32
+
33
+ def __init__(self, model_config: ModelConfig):
34
+ """Initialize the analyzer with model configuration for token counting.
35
+
36
+ Args:
37
+ model_config: Model configuration for accurate token counting
38
+ """
39
+ self.model_config = model_config
40
+
41
+ async def _allocate_tokens_from_usage(
42
+ self,
43
+ message_history: list[ModelMessage],
44
+ ) -> TokenAllocation:
45
+ """Allocate tokens from actual API usage data proportionally to parts.
46
+
47
+ This uses the ground truth token counts from ModelResponse.usage instead of
48
+ creating synthetic messages, which avoids inflating counts with message framing overhead.
49
+
50
+ IMPORTANT: usage.input_tokens is cumulative (includes all conversation history), so we:
51
+ 1. Use the LAST response's input_tokens as the ground truth total
52
+ 2. Calculate proportions based on content size across ALL requests
53
+ 3. Allocate the ground truth total proportionally
54
+
55
+ If usage data is missing or zero (e.g., after compaction), falls back to token estimation.
56
+
57
+ Args:
58
+ message_history: List of actual messages from conversation
59
+
60
+ Returns:
61
+ TokenAllocation with token counts by message/tool type
62
+ """
63
+ # Step 1: Find the last response's usage data (ground truth for input tokens)
64
+ last_input_tokens = 0
65
+ total_output_tokens = 0
66
+
67
+ for msg in reversed(message_history):
68
+ if isinstance(msg, ModelResponse) and msg.usage:
69
+ last_input_tokens = msg.usage.input_tokens + msg.usage.cache_read_tokens
70
+ break
71
+
72
+ if last_input_tokens == 0:
73
+ # Fallback to token estimation (no logging to reduce verbosity)
74
+ last_input_tokens = await estimate_tokens_from_messages(
75
+ message_history, self.model_config
76
+ )
77
+
78
+ # Step 2: Calculate total output tokens (sum across all responses)
79
+ for msg in message_history:
80
+ if isinstance(msg, ModelResponse) and msg.usage:
81
+ total_output_tokens += msg.usage.output_tokens
82
+
83
+ # Step 3: Calculate content size proportions for each part type across ALL requests
84
+ # Initialize size accumulators
85
+ user_size = 0
86
+ system_prompts_size = 0
87
+ system_status_size = 0
88
+ codebase_understanding_input_size = 0
89
+ artifact_management_input_size = 0
90
+ web_research_input_size = 0
91
+ unknown_input_size = 0
92
+
93
+ for msg in message_history:
94
+ if isinstance(msg, ModelRequest):
95
+ for part in msg.parts:
96
+ if isinstance(part, (SystemPromptPart, UserPromptPart)):
97
+ size = len(part.content)
98
+ elif isinstance(part, ToolReturnPart):
99
+ # ToolReturnPart.content can be Any type
100
+ try:
101
+ content_str = (
102
+ json.dumps(part.content)
103
+ if part.content is not None
104
+ else ""
105
+ )
106
+ except (TypeError, ValueError):
107
+ content_str = (
108
+ str(part.content) if part.content is not None else ""
109
+ )
110
+ size = len(content_str)
111
+ else:
112
+ size = 0
113
+
114
+ # Categorize by part type
115
+ # Note: Check subclasses first (AgentSystemPrompt, SystemStatusPrompt)
116
+ # before checking base class (SystemPromptPart)
117
+ if isinstance(part, SystemStatusPrompt):
118
+ system_status_size += size
119
+ elif isinstance(part, AgentSystemPrompt):
120
+ system_prompts_size += size
121
+ elif isinstance(part, SystemPromptPart):
122
+ # Generic system prompt (not AgentSystemPrompt or SystemStatusPrompt)
123
+ system_prompts_size += size
124
+ elif isinstance(part, UserPromptPart):
125
+ user_size += size
126
+ elif isinstance(part, ToolReturnPart):
127
+ # Categorize tool results by tool category
128
+ category = get_tool_category(part.tool_name)
129
+ if category == ToolCategory.CODEBASE_UNDERSTANDING:
130
+ codebase_understanding_input_size += size
131
+ elif category == ToolCategory.ARTIFACT_MANAGEMENT:
132
+ artifact_management_input_size += size
133
+ elif category == ToolCategory.WEB_RESEARCH:
134
+ web_research_input_size += size
135
+ elif category == ToolCategory.UNKNOWN:
136
+ unknown_input_size += size
137
+
138
+ # Step 4: Calculate output proportions by tool category
139
+ codebase_understanding_size = 0
140
+ artifact_management_size = 0
141
+ web_research_size = 0
142
+ unknown_size = 0
143
+ agent_response_size = 0
144
+
145
+ for msg in message_history:
146
+ if isinstance(msg, ModelResponse):
147
+ for part in msg.parts: # type: ignore[assignment]
148
+ if isinstance(part, ToolCallPart):
149
+ category = get_tool_category(part.tool_name)
150
+ size = len(str(part.args))
151
+
152
+ if category == ToolCategory.AGENT_RESPONSE:
153
+ agent_response_size += size
154
+ elif category == ToolCategory.CODEBASE_UNDERSTANDING:
155
+ codebase_understanding_size += size
156
+ elif category == ToolCategory.ARTIFACT_MANAGEMENT:
157
+ artifact_management_size += size
158
+ elif category == ToolCategory.WEB_RESEARCH:
159
+ web_research_size += size
160
+ elif category == ToolCategory.UNKNOWN:
161
+ unknown_size += size
162
+ elif isinstance(part, TextPart):
163
+ agent_response_size += len(part.content)
164
+
165
+ # Step 5: Allocate input tokens proportionally
166
+ # Initialize TokenAllocation fields
167
+ user_tokens = 0
168
+ agent_response_tokens = 0
169
+ system_prompt_tokens = 0
170
+ system_status_tokens = 0
171
+ codebase_understanding_tokens = 0
172
+ artifact_management_tokens = 0
173
+ web_research_tokens = 0
174
+ unknown_tokens = 0
175
+
176
+ total_input_size = (
177
+ user_size
178
+ + system_prompts_size
179
+ + system_status_size
180
+ + codebase_understanding_input_size
181
+ + artifact_management_input_size
182
+ + web_research_input_size
183
+ + unknown_input_size
184
+ )
185
+
186
+ if total_input_size > 0 and last_input_tokens > 0:
187
+ user_tokens = int(last_input_tokens * (user_size / total_input_size))
188
+ system_prompt_tokens = int(
189
+ last_input_tokens * (system_prompts_size / total_input_size)
190
+ )
191
+ system_status_tokens = int(
192
+ last_input_tokens * (system_status_size / total_input_size)
193
+ )
194
+ codebase_understanding_tokens = int(
195
+ last_input_tokens
196
+ * (codebase_understanding_input_size / total_input_size)
197
+ )
198
+ artifact_management_tokens = int(
199
+ last_input_tokens * (artifact_management_input_size / total_input_size)
200
+ )
201
+ web_research_tokens = int(
202
+ last_input_tokens * (web_research_input_size / total_input_size)
203
+ )
204
+ unknown_tokens = int(
205
+ last_input_tokens * (unknown_input_size / total_input_size)
206
+ )
207
+
208
+ # Step 6: Allocate output tokens proportionally
209
+ total_output_size = (
210
+ codebase_understanding_size
211
+ + artifact_management_size
212
+ + web_research_size
213
+ + unknown_size
214
+ + agent_response_size
215
+ )
216
+
217
+ if total_output_size > 0 and total_output_tokens > 0:
218
+ codebase_understanding_tokens += int(
219
+ total_output_tokens * (codebase_understanding_size / total_output_size)
220
+ )
221
+ artifact_management_tokens += int(
222
+ total_output_tokens * (artifact_management_size / total_output_size)
223
+ )
224
+ web_research_tokens += int(
225
+ total_output_tokens * (web_research_size / total_output_size)
226
+ )
227
+ unknown_tokens += int(
228
+ total_output_tokens * (unknown_size / total_output_size)
229
+ )
230
+ agent_response_tokens += int(
231
+ total_output_tokens * (agent_response_size / total_output_size)
232
+ )
233
+ elif total_output_tokens > 0:
234
+ # If no content, put all in agent responses
235
+ agent_response_tokens = total_output_tokens
236
+
237
+ # Token allocation complete (no logging to reduce verbosity)
238
+
239
+ # Create TokenAllocation model
240
+ return TokenAllocation(
241
+ user=user_tokens,
242
+ agent_responses=agent_response_tokens,
243
+ system_prompts=system_prompt_tokens,
244
+ system_status=system_status_tokens,
245
+ codebase_understanding=codebase_understanding_tokens,
246
+ artifact_management=artifact_management_tokens,
247
+ web_research=web_research_tokens,
248
+ unknown=unknown_tokens,
249
+ )
250
+
251
+ async def analyze_conversation(
252
+ self,
253
+ message_history: list[ModelMessage],
254
+ ui_message_history: list[ModelMessage | HintMessage],
255
+ ) -> ContextAnalysis:
256
+ """Analyze the conversation to determine message type composition.
257
+
258
+ Args:
259
+ message_history: The agent message history (for token counting)
260
+ ui_message_history: The UI message history (includes hints)
261
+
262
+ Returns:
263
+ ContextAnalysis with statistics for each message type
264
+ """
265
+ # Track counts for each message type
266
+ user_count = 0
267
+ agent_responses_count = 0
268
+ system_prompts_count = 0
269
+ system_status_count = 0
270
+ codebase_understanding_count = 0
271
+ artifact_management_count = 0
272
+ web_research_count = 0
273
+ unknown_count = 0
274
+
275
+ # Analyze message_history to count message types
276
+ for msg in message_history:
277
+ if isinstance(msg, ModelRequest):
278
+ # Track what types are in this message for counting
279
+ has_user_prompt = False
280
+ has_system_prompt = False
281
+ has_system_status = False
282
+
283
+ # Check what part types this message contains
284
+ for part in msg.parts:
285
+ if isinstance(part, AgentSystemPrompt):
286
+ has_system_prompt = True
287
+ elif isinstance(part, SystemStatusPrompt):
288
+ has_system_status = True
289
+ elif isinstance(part, SystemPromptPart):
290
+ # Generic system prompt
291
+ has_system_prompt = True
292
+ elif isinstance(part, UserPromptPart):
293
+ has_user_prompt = True
294
+ elif isinstance(part, ToolReturnPart):
295
+ # Categorize tool results by category
296
+ category = get_tool_category(part.tool_name)
297
+ if category == ToolCategory.CODEBASE_UNDERSTANDING:
298
+ codebase_understanding_count += 1
299
+ elif category == ToolCategory.ARTIFACT_MANAGEMENT:
300
+ artifact_management_count += 1
301
+ elif category == ToolCategory.WEB_RESEARCH:
302
+ web_research_count += 1
303
+ elif category == ToolCategory.UNKNOWN:
304
+ unknown_count += 1
305
+
306
+ # Count the message types (only count once per message)
307
+ if has_system_prompt:
308
+ system_prompts_count += 1
309
+ if has_system_status:
310
+ system_status_count += 1
311
+ if has_user_prompt:
312
+ user_count += 1
313
+
314
+ elif isinstance(msg, ModelResponse):
315
+ # Agent responses - count entire response as one
316
+ agent_responses_count += 1
317
+
318
+ # Check for tool calls in the response
319
+ for part in msg.parts: # type: ignore[assignment]
320
+ if isinstance(part, ToolCallPart):
321
+ category = get_tool_category(part.tool_name)
322
+ if category == ToolCategory.CODEBASE_UNDERSTANDING:
323
+ codebase_understanding_count += 1
324
+ elif category == ToolCategory.ARTIFACT_MANAGEMENT:
325
+ artifact_management_count += 1
326
+ elif category == ToolCategory.WEB_RESEARCH:
327
+ web_research_count += 1
328
+ elif category == ToolCategory.UNKNOWN:
329
+ unknown_count += 1
330
+
331
+ # Count hints from ui_message_history
332
+ hint_count = sum(
333
+ 1 for msg in ui_message_history if isinstance(msg, HintMessage)
334
+ )
335
+
336
+ # Use actual API usage data for accurate token counting (avoids synthetic message overhead)
337
+ usage_tokens = await self._allocate_tokens_from_usage(message_history)
338
+
339
+ user_tokens = usage_tokens.user
340
+ agent_response_tokens = usage_tokens.agent_responses
341
+ system_prompt_tokens = usage_tokens.system_prompts
342
+ system_status_tokens = usage_tokens.system_status
343
+ codebase_understanding_tokens = usage_tokens.codebase_understanding
344
+ artifact_management_tokens = usage_tokens.artifact_management
345
+ web_research_tokens = usage_tokens.web_research
346
+ unknown_tokens = usage_tokens.unknown
347
+
348
+ # Estimate hint tokens (rough estimate based on character count)
349
+ hint_tokens = 0
350
+ for msg in ui_message_history: # type: ignore[assignment]
351
+ if isinstance(msg, HintMessage):
352
+ # Rough estimate: ~4 chars per token
353
+ hint_tokens += len(msg.message) // 4
354
+
355
+ # Calculate agent context tokens (excluding UI-only hints)
356
+ agent_context_tokens = (
357
+ user_tokens
358
+ + agent_response_tokens
359
+ + system_prompt_tokens
360
+ + system_status_tokens
361
+ + codebase_understanding_tokens
362
+ + artifact_management_tokens
363
+ + web_research_tokens
364
+ + unknown_tokens
365
+ )
366
+
367
+ # Total tokens includes hints for display purposes, but agent_context_tokens does not
368
+ total_tokens = agent_context_tokens + hint_tokens
369
+ total_messages = (
370
+ user_count
371
+ + agent_responses_count
372
+ + system_prompts_count
373
+ + system_status_count
374
+ + codebase_understanding_count
375
+ + artifact_management_count
376
+ + web_research_count
377
+ + unknown_count
378
+ + hint_count
379
+ )
380
+
381
+ # Calculate usable context limit (80% of max_input_tokens) and free space
382
+ # This matches the TOKEN_LIMIT_RATIO = 0.8 from history/constants.py
383
+ max_usable_tokens = int(self.model_config.max_input_tokens * 0.8)
384
+ free_space_tokens = max_usable_tokens - agent_context_tokens
385
+
386
+ return ContextAnalysis(
387
+ user_messages=MessageTypeStats(count=user_count, tokens=user_tokens),
388
+ agent_responses=MessageTypeStats(
389
+ count=agent_responses_count, tokens=agent_response_tokens
390
+ ),
391
+ system_prompts=MessageTypeStats(
392
+ count=system_prompts_count, tokens=system_prompt_tokens
393
+ ),
394
+ system_status=MessageTypeStats(
395
+ count=system_status_count, tokens=system_status_tokens
396
+ ),
397
+ codebase_understanding=MessageTypeStats(
398
+ count=codebase_understanding_count,
399
+ tokens=codebase_understanding_tokens,
400
+ ),
401
+ artifact_management=MessageTypeStats(
402
+ count=artifact_management_count, tokens=artifact_management_tokens
403
+ ),
404
+ web_research=MessageTypeStats(
405
+ count=web_research_count, tokens=web_research_tokens
406
+ ),
407
+ unknown=MessageTypeStats(count=unknown_count, tokens=unknown_tokens),
408
+ hint_messages=MessageTypeStats(count=hint_count, tokens=hint_tokens),
409
+ total_tokens=total_tokens,
410
+ total_messages=total_messages,
411
+ context_window=self.model_config.max_input_tokens,
412
+ agent_context_tokens=agent_context_tokens,
413
+ model_name=self.model_config.name.value,
414
+ max_usable_tokens=max_usable_tokens,
415
+ free_space_tokens=free_space_tokens,
416
+ )
417
+
418
+ async def _count_tokens_for_parts(
419
+ self,
420
+ parts: Sequence[
421
+ UserPromptPart | SystemPromptPart | ToolReturnPart | ToolCallPart
422
+ ],
423
+ part_type: str,
424
+ ) -> int:
425
+ """Count tokens for a list of parts by creating synthetic single-part messages.
426
+
427
+ This avoids double-counting when a message contains multiple part types.
428
+
429
+ Args:
430
+ parts: List of parts to count tokens for
431
+ part_type: Type of parts ("user", "system", "tool_return", "tool_call")
432
+
433
+ Returns:
434
+ Total token count for all parts
435
+ """
436
+ if not parts:
437
+ return 0
438
+
439
+ # Create synthetic messages with single parts for accurate token counting
440
+ synthetic_messages: list[ModelMessage] = []
441
+
442
+ for part in parts:
443
+ if part_type in ("user", "system", "tool_return"):
444
+ # These are request parts - wrap in ModelRequest
445
+ synthetic_messages.append(ModelRequest(parts=[part])) # type: ignore[list-item]
446
+ elif part_type == "tool_call":
447
+ # Tool calls are in responses - wrap in ModelResponse
448
+ synthetic_messages.append(ModelResponse(parts=[part])) # type: ignore[list-item]
449
+
450
+ # Count tokens for the synthetic messages
451
+ return await self._count_tokens_safe(synthetic_messages)
452
+
453
+ async def _count_tokens_safe(self, messages: Sequence[ModelMessage]) -> int:
454
+ """Count tokens for a list of messages, returning 0 on error.
455
+
456
+ Args:
457
+ messages: List of messages to count tokens for
458
+
459
+ Returns:
460
+ Token count or 0 if counting fails
461
+ """
462
+ if not messages:
463
+ return 0
464
+
465
+ try:
466
+ return await count_tokens_from_messages(list(messages), self.model_config)
467
+ except Exception as e:
468
+ logger.warning(f"Failed to count tokens: {e}")
469
+ # Fallback to rough estimate
470
+ total_chars = sum(len(str(msg)) for msg in messages)
471
+ return total_chars // 4 # Rough estimate: 4 chars per token
@@ -0,0 +1,9 @@
1
+ """Tool category registry for context analysis.
2
+
3
+ This module re-exports the tool registry functionality for backward compatibility.
4
+ The actual implementation is in shotgun.agents.tools.registry.
5
+ """
6
+
7
+ from shotgun.agents.tools.registry import ToolCategory, get_tool_category
8
+
9
+ __all__ = ["ToolCategory", "get_tool_category"]
@@ -0,0 +1,115 @@
1
+ """Format context analysis for various output types."""
2
+
3
+ from typing import Any
4
+
5
+ from .models import ContextAnalysis
6
+
7
+
8
+ class ContextFormatter:
9
+ """Formats context analysis for various output types."""
10
+
11
+ @staticmethod
12
+ def format_markdown(analysis: ContextAnalysis) -> str:
13
+ """Format the analysis as markdown for display.
14
+
15
+ Args:
16
+ analysis: Context analysis to format
17
+
18
+ Returns:
19
+ Markdown-formatted string
20
+ """
21
+ lines = ["# Conversation Context Analysis", ""]
22
+
23
+ # Top-level summary with model and usage info
24
+ usage_percent = (
25
+ (analysis.agent_context_tokens / analysis.max_usable_tokens * 100)
26
+ if analysis.max_usable_tokens > 0
27
+ else 0
28
+ )
29
+ free_percent = (
30
+ (analysis.free_space_tokens / analysis.max_usable_tokens * 100)
31
+ if analysis.max_usable_tokens > 0
32
+ else 0
33
+ )
34
+
35
+ lines.extend(
36
+ [
37
+ f"Model: {analysis.model_name}",
38
+ "",
39
+ f"Total Context: {analysis.agent_context_tokens:,} / {analysis.max_usable_tokens:,} tokens ({usage_percent:.1f}%)",
40
+ "",
41
+ f"Free Space: {analysis.free_space_tokens:,} tokens ({free_percent:.1f}%)",
42
+ "",
43
+ "Autocompact Buffer: 500 tokens",
44
+ "",
45
+ ]
46
+ )
47
+
48
+ # Create 25-character visual bar showing proportional usage
49
+ # Each character represents 4% of total context
50
+ filled_chars = int(usage_percent / 4)
51
+ empty_chars = 25 - filled_chars
52
+ visual_bar = "●" * filled_chars + "○" * empty_chars
53
+
54
+ lines.extend(
55
+ [
56
+ "## Context Composition",
57
+ visual_bar,
58
+ "",
59
+ ]
60
+ )
61
+
62
+ # Add agent context categories only (hints are not part of agent context)
63
+ agent_categories = [
64
+ ("🧑 User Messages", analysis.user_messages),
65
+ ("🤖 Agent Responses", analysis.agent_responses),
66
+ ("📋 System Prompts", analysis.system_prompts),
67
+ ("📊 System Status", analysis.system_status),
68
+ ("🔍 Codebase Understanding", analysis.codebase_understanding),
69
+ ("📦 Artifact Management", analysis.artifact_management),
70
+ ("🌐 Web Research", analysis.web_research),
71
+ ]
72
+
73
+ # Only add unknown if it has content
74
+ if analysis.unknown.count > 0:
75
+ agent_categories.append(("⚠️ Unknown Tools", analysis.unknown))
76
+
77
+ for label, stats in agent_categories:
78
+ if stats.count > 0:
79
+ percentage = analysis.get_percentage(stats)
80
+ # Align labels to 30 characters for clean visual layout
81
+ lines.append(
82
+ f"{label:<30} {percentage:>5.1f}% ({stats.count} messages, ~{stats.tokens:,} tokens)"
83
+ )
84
+ # Add blank line to prevent Textual's Markdown widget from reflowing
85
+ lines.append("")
86
+
87
+ return "\n".join(lines)
88
+
89
+ @staticmethod
90
+ def format_json(analysis: ContextAnalysis) -> dict[str, Any]:
91
+ """Format the analysis as a JSON-serializable dictionary.
92
+
93
+ Args:
94
+ analysis: Context analysis to format
95
+
96
+ Returns:
97
+ Dictionary with context analysis data
98
+ """
99
+ # Use Pydantic's model_dump() to serialize the model
100
+ data = analysis.model_dump()
101
+
102
+ # Add computed summary field
103
+ data["summary"] = {
104
+ "total_messages": analysis.total_messages - analysis.hint_messages.count,
105
+ "agent_context_tokens": analysis.agent_context_tokens,
106
+ "context_window": analysis.context_window,
107
+ "usage_percentage": round(
108
+ (analysis.agent_context_tokens / analysis.context_window * 100)
109
+ if analysis.context_window > 0
110
+ else 0,
111
+ 1,
112
+ ),
113
+ }
114
+
115
+ return data