code-puppy 0.0.302__py3-none-any.whl → 0.0.323__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.
Files changed (65) hide show
  1. code_puppy/agents/base_agent.py +373 -46
  2. code_puppy/chatgpt_codex_client.py +283 -0
  3. code_puppy/cli_runner.py +795 -0
  4. code_puppy/command_line/add_model_menu.py +8 -1
  5. code_puppy/command_line/autosave_menu.py +266 -35
  6. code_puppy/command_line/colors_menu.py +515 -0
  7. code_puppy/command_line/command_handler.py +8 -2
  8. code_puppy/command_line/config_commands.py +59 -10
  9. code_puppy/command_line/core_commands.py +19 -7
  10. code_puppy/command_line/mcp/edit_command.py +3 -1
  11. code_puppy/command_line/mcp/handler.py +7 -2
  12. code_puppy/command_line/mcp/install_command.py +8 -3
  13. code_puppy/command_line/mcp/logs_command.py +173 -64
  14. code_puppy/command_line/mcp/restart_command.py +7 -2
  15. code_puppy/command_line/mcp/search_command.py +10 -4
  16. code_puppy/command_line/mcp/start_all_command.py +16 -6
  17. code_puppy/command_line/mcp/start_command.py +3 -1
  18. code_puppy/command_line/mcp/status_command.py +2 -1
  19. code_puppy/command_line/mcp/stop_all_command.py +5 -1
  20. code_puppy/command_line/mcp/stop_command.py +3 -1
  21. code_puppy/command_line/mcp/wizard_utils.py +10 -4
  22. code_puppy/command_line/model_settings_menu.py +53 -7
  23. code_puppy/command_line/prompt_toolkit_completion.py +16 -2
  24. code_puppy/command_line/session_commands.py +11 -4
  25. code_puppy/config.py +103 -15
  26. code_puppy/keymap.py +8 -2
  27. code_puppy/main.py +5 -828
  28. code_puppy/mcp_/__init__.py +17 -0
  29. code_puppy/mcp_/blocking_startup.py +61 -32
  30. code_puppy/mcp_/config_wizard.py +5 -1
  31. code_puppy/mcp_/managed_server.py +23 -3
  32. code_puppy/mcp_/manager.py +65 -0
  33. code_puppy/mcp_/mcp_logs.py +224 -0
  34. code_puppy/messaging/__init__.py +20 -4
  35. code_puppy/messaging/bus.py +64 -0
  36. code_puppy/messaging/markdown_patches.py +57 -0
  37. code_puppy/messaging/messages.py +16 -0
  38. code_puppy/messaging/renderers.py +21 -9
  39. code_puppy/messaging/rich_renderer.py +113 -67
  40. code_puppy/messaging/spinner/console_spinner.py +34 -0
  41. code_puppy/model_factory.py +185 -30
  42. code_puppy/model_utils.py +57 -48
  43. code_puppy/models.json +19 -5
  44. code_puppy/plugins/chatgpt_oauth/config.py +5 -1
  45. code_puppy/plugins/chatgpt_oauth/oauth_flow.py +5 -6
  46. code_puppy/plugins/chatgpt_oauth/register_callbacks.py +3 -3
  47. code_puppy/plugins/chatgpt_oauth/test_plugin.py +26 -11
  48. code_puppy/plugins/chatgpt_oauth/utils.py +180 -65
  49. code_puppy/plugins/claude_code_oauth/register_callbacks.py +28 -0
  50. code_puppy/plugins/claude_code_oauth/utils.py +1 -0
  51. code_puppy/plugins/shell_safety/agent_shell_safety.py +1 -118
  52. code_puppy/plugins/shell_safety/register_callbacks.py +44 -3
  53. code_puppy/prompts/codex_system_prompt.md +310 -0
  54. code_puppy/pydantic_patches.py +131 -0
  55. code_puppy/terminal_utils.py +126 -0
  56. code_puppy/tools/agent_tools.py +34 -9
  57. code_puppy/tools/command_runner.py +361 -32
  58. code_puppy/tools/file_operations.py +33 -45
  59. {code_puppy-0.0.302.data → code_puppy-0.0.323.data}/data/code_puppy/models.json +19 -5
  60. {code_puppy-0.0.302.dist-info → code_puppy-0.0.323.dist-info}/METADATA +1 -1
  61. {code_puppy-0.0.302.dist-info → code_puppy-0.0.323.dist-info}/RECORD +65 -57
  62. {code_puppy-0.0.302.data → code_puppy-0.0.323.data}/data/code_puppy/models_dev_api.json +0 -0
  63. {code_puppy-0.0.302.dist-info → code_puppy-0.0.323.dist-info}/WHEEL +0 -0
  64. {code_puppy-0.0.302.dist-info → code_puppy-0.0.323.dist-info}/entry_points.txt +0 -0
  65. {code_puppy-0.0.302.dist-info → code_puppy-0.0.323.dist-info}/licenses/LICENSE +0 -0
@@ -19,7 +19,6 @@ from .config import CHATGPT_OAUTH_CONFIG
19
19
  from .utils import (
20
20
  add_models_to_extra_config,
21
21
  assign_redirect_uri,
22
- fetch_chatgpt_models,
23
22
  load_stored_tokens,
24
23
  parse_jwt_claims,
25
24
  prepare_oauth_context,
@@ -318,12 +317,12 @@ def run_oauth_flow() -> None:
318
317
  )
319
318
 
320
319
  if api_key:
321
- emit_info("Fetching available ChatGPT models…")
322
- models = fetch_chatgpt_models(api_key)
320
+ emit_info("Registering ChatGPT Codex models…")
321
+ from .utils import DEFAULT_CODEX_MODELS
322
+
323
+ models = DEFAULT_CODEX_MODELS
323
324
  if models:
324
- if add_models_to_extra_config(models, api_key):
325
+ if add_models_to_extra_config(models):
325
326
  emit_success(
326
327
  "ChatGPT models registered. Use the `chatgpt-` prefix in /model."
327
328
  )
328
- else:
329
- emit_warning("API key obtained, but model list could not be fetched.")
@@ -5,6 +5,7 @@ from __future__ import annotations
5
5
  import os
6
6
  from typing import List, Optional, Tuple
7
7
 
8
+ from code_puppy.callbacks import register_callback
8
9
  from code_puppy.messaging import emit_info, emit_success, emit_warning
9
10
 
10
11
  from .config import CHATGPT_OAUTH_CONFIG, get_token_storage_path
@@ -87,6 +88,5 @@ def _handle_custom_command(command: str, name: str) -> Optional[bool]:
87
88
  return None
88
89
 
89
90
 
90
- # Temporarily disabled - chatgpt-oauth plugin not working yet
91
- # register_callback("custom_command_help", _custom_help)
92
- # register_callback("custom_command", _handle_custom_command)
91
+ register_callback("custom_command_help", _custom_help)
92
+ register_callback("custom_command", _handle_custom_command)
@@ -235,25 +235,41 @@ def test_exchange_code_for_tokens(mock_post):
235
235
 
236
236
  @patch("code_puppy.plugins.chatgpt_oauth.utils.requests.get")
237
237
  def test_fetch_chatgpt_models(mock_get):
238
- """Test fetching models from OpenAI API."""
238
+ """Test fetching models from ChatGPT Codex API."""
239
239
  mock_response = MagicMock()
240
240
  mock_response.status_code = 200
241
+ # New response format uses "models" key with "slug" field
241
242
  mock_response.json.return_value = {
242
- "data": [
243
- {"id": "gpt-4o"},
244
- {"id": "gpt-3.5-turbo"},
245
- {"id": "whisper-1"}, # Should be filtered out
246
- {"id": "o1-preview"},
243
+ "models": [
244
+ {"slug": "gpt-4o"},
245
+ {"slug": "gpt-3.5-turbo"},
246
+ {"slug": "o1-preview"},
247
+ {"slug": "codex-mini"},
247
248
  ]
248
249
  }
249
250
  mock_get.return_value = mock_response
250
251
 
251
- models = utils.fetch_chatgpt_models("test_api_key")
252
+ models = utils.fetch_chatgpt_models("test_access_token", "test_account_id")
252
253
  assert models is not None
253
254
  assert "gpt-4o" in models
254
255
  assert "gpt-3.5-turbo" in models
255
256
  assert "o1-preview" in models
256
- assert "whisper-1" not in models # Should be filtered
257
+ assert "codex-mini" in models
258
+
259
+
260
+ @patch("code_puppy.plugins.chatgpt_oauth.utils.requests.get")
261
+ def test_fetch_chatgpt_models_fallback(mock_get):
262
+ """Test that fetch_chatgpt_models returns default list on API failure."""
263
+ mock_response = MagicMock()
264
+ mock_response.status_code = 404
265
+ mock_response.text = '{"detail":"Not Found"}'
266
+ mock_get.return_value = mock_response
267
+
268
+ models = utils.fetch_chatgpt_models("test_access_token", "test_account_id")
269
+ assert models is not None
270
+ # Should return default models
271
+ assert "gpt-5.2" in models
272
+ assert "gpt-4o" in models
257
273
 
258
274
 
259
275
  def test_add_models_to_chatgpt_config(tmp_path):
@@ -262,14 +278,13 @@ def test_add_models_to_chatgpt_config(tmp_path):
262
278
  config, "get_chatgpt_models_path", return_value=tmp_path / "chatgpt_models.json"
263
279
  ):
264
280
  models = ["gpt-4o", "gpt-3.5-turbo"]
265
- api_key = "sk-test"
266
281
 
267
- assert utils.add_models_to_extra_config(models, api_key)
282
+ assert utils.add_models_to_extra_config(models)
268
283
 
269
284
  loaded = utils.load_chatgpt_models()
270
285
  assert "chatgpt-gpt-4o" in loaded
271
286
  assert "chatgpt-gpt-3.5-turbo" in loaded
272
- assert loaded["chatgpt-gpt-4o"]["type"] == "openai"
287
+ assert loaded["chatgpt-gpt-4o"]["type"] == "chatgpt_oauth"
273
288
  assert loaded["chatgpt-gpt-4o"]["name"] == "gpt-4o"
274
289
  assert loaded["chatgpt-gpt-4o"]["oauth_source"] == "chatgpt-oauth-plugin"
275
290
 
@@ -149,6 +149,98 @@ def load_stored_tokens() -> Optional[Dict[str, Any]]:
149
149
  return None
150
150
 
151
151
 
152
+ def get_valid_access_token() -> Optional[str]:
153
+ """Get a valid access token, refreshing if expired.
154
+
155
+ Returns:
156
+ Valid access token string, or None if not authenticated or refresh failed.
157
+ """
158
+ tokens = load_stored_tokens()
159
+ if not tokens:
160
+ logger.debug("No stored ChatGPT OAuth tokens found")
161
+ return None
162
+
163
+ access_token = tokens.get("access_token")
164
+ if not access_token:
165
+ logger.debug("No access_token in stored tokens")
166
+ return None
167
+
168
+ # Check if token is expired by parsing JWT claims
169
+ claims = parse_jwt_claims(access_token)
170
+ if claims:
171
+ exp = claims.get("exp")
172
+ if exp and isinstance(exp, (int, float)):
173
+ # Add 30 second buffer before expiry
174
+ if time.time() > exp - 30:
175
+ logger.info("ChatGPT OAuth token expired, attempting refresh")
176
+ refreshed = refresh_access_token()
177
+ if refreshed:
178
+ return refreshed
179
+ logger.warning("Token refresh failed")
180
+ return None
181
+
182
+ return access_token
183
+
184
+
185
+ def refresh_access_token() -> Optional[str]:
186
+ """Refresh the access token using the refresh token.
187
+
188
+ Returns:
189
+ New access token if refresh succeeded, None otherwise.
190
+ """
191
+ tokens = load_stored_tokens()
192
+ if not tokens:
193
+ return None
194
+
195
+ refresh_token = tokens.get("refresh_token")
196
+ if not refresh_token:
197
+ logger.debug("No refresh_token available")
198
+ return None
199
+
200
+ payload = {
201
+ "grant_type": "refresh_token",
202
+ "refresh_token": refresh_token,
203
+ "client_id": CHATGPT_OAUTH_CONFIG["client_id"],
204
+ }
205
+
206
+ headers = {
207
+ "Content-Type": "application/x-www-form-urlencoded",
208
+ }
209
+
210
+ try:
211
+ response = requests.post(
212
+ CHATGPT_OAUTH_CONFIG["token_url"],
213
+ data=payload,
214
+ headers=headers,
215
+ timeout=30,
216
+ )
217
+
218
+ if response.status_code == 200:
219
+ new_tokens = response.json()
220
+ # Merge with existing tokens (preserve account_id, etc.)
221
+ tokens.update(
222
+ {
223
+ "access_token": new_tokens.get("access_token"),
224
+ "refresh_token": new_tokens.get("refresh_token", refresh_token),
225
+ "id_token": new_tokens.get("id_token", tokens.get("id_token")),
226
+ "last_refresh": datetime.datetime.now(datetime.timezone.utc)
227
+ .isoformat()
228
+ .replace("+00:00", "Z"),
229
+ }
230
+ )
231
+ if save_tokens(tokens):
232
+ logger.info("Successfully refreshed ChatGPT OAuth token")
233
+ return tokens["access_token"]
234
+ else:
235
+ logger.error(
236
+ "Token refresh failed: %s - %s", response.status_code, response.text
237
+ )
238
+ except Exception as exc:
239
+ logger.error("Token refresh error: %s", exc)
240
+
241
+ return None
242
+
243
+
152
244
  def save_tokens(tokens: Dict[str, Any]) -> bool:
153
245
  if tokens is None:
154
246
  raise TypeError("tokens cannot be None")
@@ -248,103 +340,126 @@ def exchange_code_for_tokens(
248
340
  return None
249
341
 
250
342
 
251
- def fetch_chatgpt_models(api_key: str) -> Optional[List[str]]:
252
- """Fetch available models from OpenAI API.
343
+ # Default models available via ChatGPT Codex API
344
+ # These are the known models that work with ChatGPT OAuth tokens
345
+ # Based on codex-rs CLI and shell-scripts/codex-call.sh
346
+ DEFAULT_CODEX_MODELS = [
347
+ "gpt-5.2",
348
+ "gpt-5.2-codex",
349
+ ]
350
+
253
351
 
254
- Makes a real HTTP GET request to OpenAI's models endpoint and filters
255
- the results to include only GPT series models while preserving server order.
352
+ def fetch_chatgpt_models(access_token: str, account_id: str) -> Optional[List[str]]:
353
+ """Fetch available models from ChatGPT Codex API.
354
+
355
+ Attempts to fetch models from the API, but falls back to a default list
356
+ of known Codex-compatible models if the API is unavailable.
256
357
 
257
358
  Args:
258
- api_key: OpenAI API key for authentication
359
+ access_token: OAuth access token for authentication
360
+ account_id: ChatGPT account ID (required for the API)
259
361
 
260
362
  Returns:
261
- List of filtered model IDs preserving server order, or None if request fails
363
+ List of model IDs, or default list if API fails
262
364
  """
263
- # Build the models URL, ensuring it ends with /v1/models
264
- base_url = CHATGPT_OAUTH_CONFIG["api_base_url"].rstrip("/")
265
- models_url = f"{base_url}/v1/models"
365
+ import platform
266
366
 
267
- # Blocklist of model IDs to exclude
268
- blocklist = {"whisper-1"}
367
+ # Build the models URL with client version
368
+ client_version = CHATGPT_OAUTH_CONFIG.get("client_version", "0.72.0")
369
+ base_url = CHATGPT_OAUTH_CONFIG["api_base_url"].rstrip("/")
370
+ models_url = f"{base_url}/models"
371
+
372
+ # Build User-Agent to match codex-rs CLI format
373
+ originator = CHATGPT_OAUTH_CONFIG.get("originator", "codex_cli_rs")
374
+ os_name = platform.system()
375
+ if os_name == "Darwin":
376
+ os_name = "Mac OS"
377
+ os_version = platform.release()
378
+ arch = platform.machine()
379
+ user_agent = (
380
+ f"{originator}/{client_version} ({os_name} {os_version}; {arch}) "
381
+ "Terminal_Codex_CLI"
382
+ )
269
383
 
270
384
  headers = {
271
- "Authorization": f"Bearer {api_key}",
385
+ "Authorization": f"Bearer {access_token}",
386
+ "ChatGPT-Account-Id": account_id,
387
+ "User-Agent": user_agent,
388
+ "originator": originator,
389
+ "Accept": "application/json",
272
390
  }
273
391
 
274
- try:
275
- response = requests.get(models_url, headers=headers, timeout=30)
276
-
277
- if response.status_code != 200:
278
- logger.error(
279
- "Failed to fetch models: HTTP %d - %s",
280
- response.status_code,
281
- response.text,
282
- )
283
- return None
284
-
285
- # Parse JSON response
286
- try:
287
- data = response.json()
288
- if "data" not in data or not isinstance(data["data"], list):
289
- logger.error("Invalid response format: missing 'data' list")
290
- return None
291
- except (json.JSONDecodeError, ValueError) as exc:
292
- logger.error("Failed to parse JSON response: %s", exc)
293
- return None
294
-
295
- # Filter models: start with "gpt-" or "o1-" and not in blocklist
296
- filtered_models = []
297
- seen_models = set() # For deduplication while preserving order
298
-
299
- for model in data["data"]:
300
- # Skip None entries
301
- if model is None:
302
- continue
392
+ # Query params
393
+ params = {"client_version": client_version}
303
394
 
304
- model_id = model.get("id")
305
- if not model_id:
306
- continue
307
-
308
- # Skip if already seen (deduplication)
309
- if model_id in seen_models:
310
- continue
311
-
312
- # Check if model starts with allowed prefixes and not in blocklist
313
- if (
314
- model_id.startswith("gpt-") or model_id.startswith("o1-")
315
- ) and model_id not in blocklist:
316
- filtered_models.append(model_id)
317
- seen_models.add(model_id)
395
+ try:
396
+ response = requests.get(models_url, headers=headers, params=params, timeout=30)
318
397
 
319
- return filtered_models
398
+ if response.status_code == 200:
399
+ # Parse JSON response
400
+ try:
401
+ data = response.json()
402
+ # The response has a "models" key with list of model objects
403
+ if "models" in data and isinstance(data["models"], list):
404
+ models = []
405
+ for model in data["models"]:
406
+ if model is None:
407
+ continue
408
+ model_id = (
409
+ model.get("slug") or model.get("id") or model.get("name")
410
+ )
411
+ if model_id:
412
+ models.append(model_id)
413
+ if models:
414
+ return models
415
+ except (json.JSONDecodeError, ValueError) as exc:
416
+ logger.warning("Failed to parse models response: %s", exc)
417
+
418
+ # API didn't return valid models, use default list
419
+ logger.info(
420
+ "Models endpoint returned %d, using default model list",
421
+ response.status_code,
422
+ )
320
423
 
321
424
  except requests.exceptions.Timeout:
322
- logger.error("Timeout while fetching models after 30 seconds")
323
- return None
425
+ logger.warning("Timeout fetching models, using default list")
324
426
  except requests.exceptions.RequestException as exc:
325
- logger.error("Network error while fetching models: %s", exc)
326
- return None
427
+ logger.warning("Network error fetching models: %s, using default list", exc)
327
428
  except Exception as exc:
328
- logger.error("Unexpected error while fetching models: %s", exc)
329
- return None
429
+ logger.warning("Error fetching models: %s, using default list", exc)
330
430
 
431
+ # Return default models when API fails
432
+ logger.info("Using default Codex models: %s", DEFAULT_CODEX_MODELS)
433
+ return DEFAULT_CODEX_MODELS
331
434
 
332
- def add_models_to_extra_config(models: List[str], api_key: str) -> bool:
435
+
436
+ def add_models_to_extra_config(models: List[str]) -> bool:
333
437
  """Add ChatGPT models to chatgpt_models.json configuration."""
334
438
  try:
335
439
  chatgpt_models = load_chatgpt_models()
336
440
  added = 0
337
441
  for model_name in models:
338
442
  prefixed = f"{CHATGPT_OAUTH_CONFIG['prefix']}{model_name}"
443
+
444
+ # Determine supported settings based on model type
445
+ # All GPT-5.x models support reasoning_effort and verbosity
446
+ supported_settings = ["reasoning_effort", "verbosity"]
447
+
448
+ # Only codex models support xhigh reasoning effort
449
+ # Regular gpt-5.2 is capped at "high"
450
+ is_codex = "codex" in model_name.lower()
451
+
339
452
  chatgpt_models[prefixed] = {
340
- "type": "openai",
453
+ "type": "chatgpt_oauth",
341
454
  "name": model_name,
342
455
  "custom_endpoint": {
456
+ # Codex API uses chatgpt.com/backend-api/codex, not api.openai.com
343
457
  "url": CHATGPT_OAUTH_CONFIG["api_base_url"],
344
- "api_key": "${" + CHATGPT_OAUTH_CONFIG["api_key_env_var"] + "}",
345
458
  },
346
459
  "context_length": CHATGPT_OAUTH_CONFIG["default_context_length"],
347
460
  "oauth_source": "chatgpt-oauth-plugin",
461
+ "supported_settings": supported_settings,
462
+ "supports_xhigh_reasoning": is_codex,
348
463
  }
349
464
  added += 1
350
465
  if save_chatgpt_models(chatgpt_models):
@@ -180,6 +180,31 @@ def _custom_help() -> List[Tuple[str, str]]:
180
180
  ]
181
181
 
182
182
 
183
+ def _reload_current_agent() -> None:
184
+ """Reload the current agent so new auth tokens are picked up immediately."""
185
+ try:
186
+ from code_puppy.agents import get_current_agent
187
+
188
+ current_agent = get_current_agent()
189
+ if current_agent is None:
190
+ logger.debug("No current agent to reload")
191
+ return
192
+
193
+ # JSON agents may need to refresh their config before reload
194
+ if hasattr(current_agent, "refresh_config"):
195
+ try:
196
+ current_agent.refresh_config()
197
+ except Exception:
198
+ # Non-fatal, continue to reload
199
+ pass
200
+
201
+ current_agent.reload_code_generation_agent()
202
+ emit_info("Active agent reloaded with new authentication")
203
+ except Exception as e:
204
+ emit_warning(f"Authentication succeeded but agent reload failed: {e}")
205
+ logger.exception("Failed to reload agent after authentication")
206
+
207
+
183
208
  def _perform_authentication() -> None:
184
209
  context = prepare_oauth_context()
185
210
  code = _await_callback(context)
@@ -219,6 +244,9 @@ def _perform_authentication() -> None:
219
244
  "Claude Code models added to your configuration. Use the `claude-code-` prefix!"
220
245
  )
221
246
 
247
+ # Reload the current agent so the new auth token is picked up immediately
248
+ _reload_current_agent()
249
+
222
250
 
223
251
  def _handle_custom_command(command: str, name: str) -> Optional[bool]:
224
252
  if not name:
@@ -368,6 +368,7 @@ def add_models_to_extra_config(models: List[str]) -> bool:
368
368
  "temperature",
369
369
  "extended_thinking",
370
370
  "budget_tokens",
371
+ "interleaved_thinking",
371
372
  ],
372
373
  }
373
374
  added += 1
@@ -5,13 +5,12 @@ It's designed to be ultra-lightweight with a concise prompt (<200 tokens) and
5
5
  uses structured output for reliable parsing.
6
6
  """
7
7
 
8
- import asyncio
9
8
  from typing import TYPE_CHECKING, List
10
9
 
11
10
  from code_puppy.agents.base_agent import BaseAgent
12
11
 
13
12
  if TYPE_CHECKING:
14
- from code_puppy.tools.command_runner import ShellSafetyAssessment
13
+ pass
15
14
 
16
15
 
17
16
  class ShellSafetyAgent(BaseAgent):
@@ -68,119 +67,3 @@ class ShellSafetyAgent(BaseAgent):
68
67
  def get_available_tools(self) -> List[str]:
69
68
  """This agent uses no tools - pure reasoning only."""
70
69
  return []
71
-
72
- async def assess_command(
73
- self, command: str, cwd: str | None = None
74
- ) -> "ShellSafetyAssessment":
75
- """Assess the safety risk of a shell command.
76
-
77
- Args:
78
- command: The shell command to assess
79
- cwd: Optional working directory context
80
-
81
- Returns:
82
- ShellSafetyAssessment with risk level and reasoning
83
-
84
- Note:
85
- On timeout or error, defaults to 'high' risk with error reasoning
86
- to fail safe. Optionally uses DBOS for durable execution tracking.
87
- """
88
- import uuid
89
-
90
- from pydantic_ai import Agent, UsageLimits
91
-
92
- from code_puppy.config import get_use_dbos
93
- from code_puppy.model_factory import ModelFactory
94
- from code_puppy.tools.command_runner import ShellSafetyAssessment
95
-
96
- try:
97
- # Build the assessment prompt
98
- prompt = f"Assess this shell command:\n\nCommand: {command}"
99
- if cwd:
100
- prompt += f"\nWorking directory: {cwd}"
101
-
102
- # Get the current model
103
- model_name = self.get_model_name()
104
- models_config = ModelFactory.load_config()
105
-
106
- if model_name not in models_config:
107
- # Fall back to high risk if model config fails
108
- return ShellSafetyAssessment(
109
- risk="high",
110
- reasoning="Model configuration unavailable - failing safe",
111
- is_fallback=True,
112
- )
113
-
114
- model = ModelFactory.get_model(model_name, models_config)
115
-
116
- # Handle claude-code models: swap instructions and prepend system prompt
117
- from code_puppy.model_utils import prepare_prompt_for_model
118
-
119
- instructions = self.get_system_prompt()
120
- prepared = prepare_prompt_for_model(model_name, instructions, prompt)
121
- instructions = prepared.instructions
122
- prompt = prepared.user_prompt
123
-
124
- from code_puppy.model_factory import make_model_settings
125
-
126
- model_settings = make_model_settings(model_name)
127
-
128
- temp_agent = Agent(
129
- model=model,
130
- system_prompt=instructions,
131
- retries=2, # Increase from 1 to 2 for better reliability
132
- output_type=ShellSafetyAssessment,
133
- model_settings=model_settings,
134
- )
135
-
136
- # Generate unique agent name and workflow ID for DBOS (if enabled)
137
- agent_name = f"shell-safety-{uuid.uuid4().hex[:8]}"
138
- workflow_id = f"shell-safety-{uuid.uuid4().hex[:8]}"
139
-
140
- # Wrap with DBOS if enabled (same pattern as agent_tools.py)
141
- if get_use_dbos():
142
- from pydantic_ai.durable_exec.dbos import DBOSAgent
143
-
144
- dbos_agent = DBOSAgent(temp_agent, name=agent_name)
145
- temp_agent = dbos_agent
146
-
147
- # Run the agent as a cancellable task
148
- # Import the shared task registry for cancellation support
149
- from code_puppy.tools.agent_tools import _active_subagent_tasks
150
-
151
- if get_use_dbos():
152
- from dbos import DBOS, SetWorkflowID
153
-
154
- with SetWorkflowID(workflow_id):
155
- task = asyncio.create_task(
156
- temp_agent.run(
157
- prompt,
158
- usage_limits=UsageLimits(request_limit=3),
159
- )
160
- )
161
- _active_subagent_tasks.add(task)
162
- else:
163
- task = asyncio.create_task(
164
- temp_agent.run(
165
- prompt,
166
- usage_limits=UsageLimits(request_limit=3),
167
- )
168
- )
169
- _active_subagent_tasks.add(task)
170
-
171
- try:
172
- result = await task
173
- finally:
174
- _active_subagent_tasks.discard(task)
175
- if task.cancelled():
176
- if get_use_dbos():
177
- DBOS.cancel_workflow(workflow_id)
178
-
179
- return result.output
180
-
181
- except Exception as e:
182
- return ShellSafetyAssessment(
183
- risk="high",
184
- reasoning=f"Safety assessment failed: {str(e)[:200]} - failing safe",
185
- is_fallback=True,
186
- )
@@ -7,12 +7,42 @@ and assesses their safety risk before execution.
7
7
  from typing import Any, Dict, Optional
8
8
 
9
9
  from code_puppy.callbacks import register_callback
10
- from code_puppy.config import get_safety_permission_level, get_yolo_mode
10
+ from code_puppy.config import (
11
+ get_global_model_name,
12
+ get_safety_permission_level,
13
+ get_yolo_mode,
14
+ )
11
15
  from code_puppy.messaging import emit_info
12
16
  from code_puppy.plugins.shell_safety.command_cache import (
13
17
  cache_assessment,
14
18
  get_cached_assessment,
15
19
  )
20
+ from code_puppy.tools.command_runner import ShellSafetyAssessment
21
+
22
+ # OAuth model prefixes - these models have their own safety mechanisms
23
+ OAUTH_MODEL_PREFIXES = (
24
+ "claude-code-", # Anthropic OAuth
25
+ "chatgpt-", # OpenAI OAuth
26
+ "gemini-oauth", # Google OAuth
27
+ )
28
+
29
+
30
+ def is_oauth_model(model_name: str | None) -> bool:
31
+ """Check if the model is an OAuth model that should skip safety checks.
32
+
33
+ OAuth models have their own built-in safety mechanisms, so we skip
34
+ the shell safety callback to avoid redundant checks and potential bugs.
35
+
36
+ Args:
37
+ model_name: The name of the current model
38
+
39
+ Returns:
40
+ True if the model is an OAuth model, False otherwise
41
+ """
42
+ if not model_name:
43
+ return False
44
+ return model_name.startswith(OAUTH_MODEL_PREFIXES)
45
+
16
46
 
17
47
  # Risk level hierarchy for numeric comparison
18
48
  # Lower numbers = safer commands, higher numbers = more dangerous
@@ -68,6 +98,11 @@ async def shell_safety_callback(
68
98
  None if command is safe to proceed
69
99
  Dict with rejection info if command should be blocked
70
100
  """
101
+ # Skip safety checks for OAuth models - they have their own safety mechanisms
102
+ current_model = get_global_model_name()
103
+ if is_oauth_model(current_model):
104
+ return None
105
+
71
106
  # Only check safety in yolo_mode - otherwise user is reviewing manually
72
107
  yolo_mode = get_yolo_mode()
73
108
  if not yolo_mode:
@@ -108,8 +143,14 @@ async def shell_safety_callback(
108
143
  # Create agent and assess command
109
144
  agent = ShellSafetyAgent()
110
145
 
111
- # Run async assessment (we're in an async callback now!)
112
- assessment = await agent.assess_command(command, cwd)
146
+ # Build the assessment prompt with optional cwd context
147
+ prompt = f"Assess this shell command:\n\nCommand: {command}"
148
+ if cwd:
149
+ prompt += f"\nWorking directory: {cwd}"
150
+
151
+ # Run async assessment with structured output type
152
+ result = await agent.run_with_mcp(prompt, output_type=ShellSafetyAssessment)
153
+ assessment = result.output
113
154
 
114
155
  # Cache the result for future use, but only if it's not a fallback assessment
115
156
  if not getattr(assessment, "is_fallback", False):