cycode 3.15.3.dev8__py3-none-any.whl → 3.15.4.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.
cycode/__init__.py CHANGED
@@ -1 +1 @@
1
- __version__ = '3.15.3.dev8' # DON'T TOUCH. Placeholder. Will be filled automatically on poetry build from Git Tag
1
+ __version__ = '3.15.4.dev1' # DON'T TOUCH. Placeholder. Will be filled automatically on poetry build from Git Tag
@@ -7,46 +7,11 @@ from typing import Optional
7
7
  import typer
8
8
  from rich.console import Console
9
9
 
10
- from cycode.cli.apps.ai_guardrails.consts import AIIDEType
11
-
12
10
  console = Console()
13
11
 
14
12
 
15
- def validate_and_parse_ide(ide: str) -> Optional[AIIDEType]:
16
- """Validate IDE parameter, returning None for 'all'.
17
-
18
- Args:
19
- ide: IDE name string (e.g., 'cursor', 'claude-code', 'all')
20
-
21
- Returns:
22
- AIIDEType enum value, or None if 'all' was specified
23
-
24
- Raises:
25
- typer.Exit: If IDE is invalid
26
- """
27
- if ide.lower() == 'all':
28
- return None
29
- try:
30
- return AIIDEType(ide.lower())
31
- except ValueError:
32
- valid_ides = ', '.join([ide_type.value for ide_type in AIIDEType])
33
- console.print(
34
- f'[red]Error:[/] Invalid IDE "{ide}". Supported IDEs: {valid_ides}, all',
35
- style='bold red',
36
- )
37
- raise typer.Exit(1) from None
38
-
39
-
40
13
  def validate_scope(scope: str, allowed_scopes: tuple[str, ...] = ('user', 'repo')) -> None:
41
- """Validate scope parameter.
42
-
43
- Args:
44
- scope: Scope string to validate
45
- allowed_scopes: Tuple of allowed scope values
46
-
47
- Raises:
48
- typer.Exit: If scope is invalid
49
- """
14
+ """Validate scope parameter."""
50
15
  if scope not in allowed_scopes:
51
16
  scopes_list = ', '.join(f'"{s}"' for s in allowed_scopes)
52
17
  console.print(f'[red]Error:[/] Invalid scope. Use {scopes_list}.', style='bold red')
@@ -54,15 +19,7 @@ def validate_scope(scope: str, allowed_scopes: tuple[str, ...] = ('user', 'repo'
54
19
 
55
20
 
56
21
  def resolve_repo_path(scope: str, repo_path: Optional[Path]) -> Optional[Path]:
57
- """Resolve repository path, defaulting to current directory for repo scope.
58
-
59
- Args:
60
- scope: The command scope ('user' or 'repo')
61
- repo_path: Provided repo path or None
62
-
63
- Returns:
64
- Resolved Path for repo scope, None for user scope
65
- """
22
+ """Default repo_path to cwd for 'repo' scope; leave None for 'user' scope."""
66
23
  if scope == 'repo' and repo_path is None:
67
24
  return Path(os.getcwd())
68
25
  return repo_path
@@ -1,22 +1,6 @@
1
- """Constants for AI guardrails hooks management.
1
+ """Shared constants and policy/mode enums for AI guardrails."""
2
2
 
3
- Currently supports:
4
- - Cursor
5
- - Claude Code
6
- """
7
-
8
- import platform
9
- from copy import deepcopy
10
3
  from enum import Enum
11
- from pathlib import Path
12
- from typing import NamedTuple
13
-
14
-
15
- class AIIDEType(str, Enum):
16
- """Supported AI IDE types."""
17
-
18
- CURSOR = 'cursor'
19
- CLAUDE_CODE = 'claude-code'
20
4
 
21
5
 
22
6
  class PolicyMode(str, Enum):
@@ -33,123 +17,7 @@ class InstallMode(str, Enum):
33
17
  BLOCK = 'block'
34
18
 
35
19
 
36
- class IDEConfig(NamedTuple):
37
- """Configuration for an AI IDE."""
38
-
39
- name: str
40
- hooks_dir: Path
41
- repo_hooks_subdir: str # Subdirectory in repo for hooks (e.g., '.cursor')
42
- hooks_file_name: str
43
- hook_events: list[str] # List of supported hook event names for this IDE
44
-
45
-
46
- def _get_cursor_hooks_dir() -> Path:
47
- """Get Cursor hooks directory based on platform."""
48
- if platform.system() == 'Darwin':
49
- return Path.home() / '.cursor'
50
- if platform.system() == 'Windows':
51
- return Path.home() / 'AppData' / 'Roaming' / 'Cursor'
52
- # Linux
53
- return Path.home() / '.config' / 'Cursor'
54
-
55
-
56
- def _get_claude_code_hooks_dir() -> Path:
57
- """Get Claude Code hooks directory.
58
-
59
- Claude Code uses ~/.claude on all platforms.
60
- """
61
- return Path.home() / '.claude'
62
-
63
-
64
- # IDE-specific configurations
65
- IDE_CONFIGS: dict[AIIDEType, IDEConfig] = {
66
- AIIDEType.CURSOR: IDEConfig(
67
- name='Cursor',
68
- hooks_dir=_get_cursor_hooks_dir(),
69
- repo_hooks_subdir='.cursor',
70
- hooks_file_name='hooks.json',
71
- hook_events=['beforeSubmitPrompt', 'beforeReadFile', 'beforeMCPExecution'],
72
- ),
73
- AIIDEType.CLAUDE_CODE: IDEConfig(
74
- name='Claude Code',
75
- hooks_dir=_get_claude_code_hooks_dir(),
76
- repo_hooks_subdir='.claude',
77
- hooks_file_name='settings.json',
78
- hook_events=['UserPromptSubmit', 'PreToolUse:Read', 'PreToolUse:mcp'],
79
- ),
80
- }
81
-
82
- # Default IDE
83
- DEFAULT_IDE = AIIDEType.CURSOR
84
-
85
- # Command used in hooks
20
+ # Base CLI commands invoked from installed hooks. IDE classes append --ide flags
21
+ # (and any other suffix) on top of these.
86
22
  CYCODE_SCAN_PROMPT_COMMAND = 'cycode ai-guardrails scan'
87
23
  CYCODE_SESSION_START_COMMAND = 'cycode ai-guardrails session-start'
88
-
89
-
90
- def _get_cursor_hooks_config(async_mode: bool = False) -> dict:
91
- """Get Cursor-specific hooks configuration."""
92
- config = IDE_CONFIGS[AIIDEType.CURSOR]
93
- command = f'{CYCODE_SCAN_PROMPT_COMMAND} &' if async_mode else CYCODE_SCAN_PROMPT_COMMAND
94
- hooks = {event: [{'command': command}] for event in config.hook_events}
95
- hooks['sessionStart'] = [{'command': f'{CYCODE_SESSION_START_COMMAND} --ide cursor'}]
96
-
97
- return {
98
- 'version': 1,
99
- 'hooks': hooks,
100
- }
101
-
102
-
103
- def _get_claude_code_hooks_config(async_mode: bool = False) -> dict:
104
- """Get Claude Code-specific hooks configuration.
105
-
106
- Claude Code uses a different hook format with nested structure:
107
- - hooks are arrays of objects with 'hooks' containing command arrays
108
- - PreToolUse uses 'matcher' field to specify which tools to intercept
109
- """
110
- command = f'{CYCODE_SCAN_PROMPT_COMMAND} --ide claude-code'
111
-
112
- hook_entry = {'type': 'command', 'command': command}
113
- if async_mode:
114
- hook_entry['async'] = True
115
- hook_entry['timeout'] = 20
116
-
117
- return {
118
- 'hooks': {
119
- 'SessionStart': [
120
- {
121
- 'hooks': [{'type': 'command', 'command': f'{CYCODE_SESSION_START_COMMAND} --ide claude-code'}],
122
- }
123
- ],
124
- 'UserPromptSubmit': [
125
- {
126
- 'hooks': [deepcopy(hook_entry)],
127
- }
128
- ],
129
- 'PreToolUse': [
130
- {
131
- 'matcher': 'Read',
132
- 'hooks': [deepcopy(hook_entry)],
133
- },
134
- {
135
- 'matcher': 'mcp__.*',
136
- 'hooks': [deepcopy(hook_entry)],
137
- },
138
- ],
139
- },
140
- }
141
-
142
-
143
- def get_hooks_config(ide: AIIDEType, async_mode: bool = False) -> dict:
144
- """Get the hooks configuration for a specific IDE.
145
-
146
- Args:
147
- ide: The AI IDE type
148
- async_mode: If True, hooks run asynchronously (non-blocking)
149
-
150
- Returns:
151
- Dict with hooks configuration for the specified IDE
152
- """
153
- if ide == AIIDEType.CLAUDE_CODE:
154
- return _get_claude_code_hooks_config(async_mode=async_mode)
155
- return _get_cursor_hooks_config(async_mode=async_mode)
@@ -1,8 +1,8 @@
1
- """
2
- Hooks manager for AI guardrails.
1
+ """Hooks manager for AI guardrails.
3
2
 
4
- Handles installation, removal, and status checking of AI IDE hooks.
5
- Supports multiple IDEs: Cursor, Claude Code (future).
3
+ Generic install/uninstall/status logic. All IDE-specific concerns (settings
4
+ paths, hooks template shape) live on the `IDE` instance; this module is
5
+ agent-agnostic.
6
6
  """
7
7
 
8
8
  import copy
@@ -12,47 +12,45 @@ from typing import Optional
12
12
 
13
13
  import yaml
14
14
 
15
- from cycode.cli.apps.ai_guardrails.consts import (
16
- DEFAULT_IDE,
17
- IDE_CONFIGS,
18
- AIIDEType,
19
- PolicyMode,
20
- get_hooks_config,
21
- )
15
+ from cycode.cli.apps.ai_guardrails.consts import PolicyMode
16
+ from cycode.cli.apps.ai_guardrails.ides.base import IDE
22
17
  from cycode.cli.apps.ai_guardrails.scan.consts import DEFAULT_POLICY, POLICY_FILE_NAME
23
18
  from cycode.logger import get_logger
24
19
 
25
20
  logger = get_logger('AI Guardrails Hooks')
26
21
 
27
22
 
28
- def get_hooks_path(scope: str, repo_path: Optional[Path] = None, ide: AIIDEType = DEFAULT_IDE) -> Path:
29
- """Get the hooks.json path for the given scope and IDE.
23
+ _CYCODE_COMMAND_MARKERS = ('cycode ai-guardrails',)
24
+
30
25
 
31
- Args:
32
- scope: 'user' for user-level hooks, 'repo' for repository-level hooks
33
- repo_path: Repository path (required if scope is 'repo')
34
- ide: The AI IDE type (default: Cursor)
35
- """
36
- config = IDE_CONFIGS[ide]
37
- if scope == 'repo' and repo_path:
38
- return repo_path / config.repo_hooks_subdir / config.hooks_file_name
39
- return config.hooks_dir / config.hooks_file_name
26
+ def _is_cycode_command(command: str) -> bool:
27
+ return any(marker in command for marker in _CYCODE_COMMAND_MARKERS)
28
+
29
+
30
+ def is_cycode_hook_entry(entry: dict) -> bool:
31
+ """Detect Cycode hook entries in both Cursor (flat) and Claude Code (nested) shapes."""
32
+ command = entry.get('command', '')
33
+ if _is_cycode_command(command):
34
+ return True
35
+
36
+ for hook in entry.get('hooks', []):
37
+ if isinstance(hook, dict) and _is_cycode_command(hook.get('command', '')):
38
+ return True
39
+
40
+ return False
40
41
 
41
42
 
42
- def load_hooks_file(hooks_path: Path) -> Optional[dict]:
43
- """Load existing hooks.json file."""
43
+ def _load_hooks_file(hooks_path: Path) -> Optional[dict]:
44
44
  if not hooks_path.exists():
45
45
  return None
46
46
  try:
47
- content = hooks_path.read_text(encoding='utf-8')
48
- return json.loads(content)
47
+ return json.loads(hooks_path.read_text(encoding='utf-8'))
49
48
  except Exception as e:
50
49
  logger.debug('Failed to load hooks file', exc_info=e)
51
50
  return None
52
51
 
53
52
 
54
- def save_hooks_file(hooks_path: Path, hooks_config: dict) -> bool:
55
- """Save hooks.json file."""
53
+ def _save_hooks_file(hooks_path: Path, hooks_config: dict) -> bool:
56
54
  try:
57
55
  hooks_path.parent.mkdir(parents=True, exist_ok=True)
58
56
  hooks_path.write_text(json.dumps(hooks_config, indent=2), encoding='utf-8')
@@ -62,39 +60,7 @@ def save_hooks_file(hooks_path: Path, hooks_config: dict) -> bool:
62
60
  return False
63
61
 
64
62
 
65
- _CYCODE_COMMAND_MARKERS = ('cycode ai-guardrails',)
66
-
67
-
68
- def _is_cycode_command(command: str) -> bool:
69
- return any(marker in command for marker in _CYCODE_COMMAND_MARKERS)
70
-
71
-
72
- def is_cycode_hook_entry(entry: dict) -> bool:
73
- """Check if a hook entry is from cycode-cli.
74
-
75
- Handles both Cursor format (flat) and Claude Code format (nested).
76
-
77
- Cursor format: {"command": "cycode ai-guardrails scan"}
78
- Claude Code format: {"hooks": [{"type": "command", "command": "cycode ai-guardrails scan --ide claude-code"}]}
79
- """
80
- # Check Cursor format (flat command)
81
- command = entry.get('command', '')
82
- if _is_cycode_command(command):
83
- return True
84
-
85
- # Check Claude Code format (nested hooks array)
86
- hooks = entry.get('hooks', [])
87
- for hook in hooks:
88
- if isinstance(hook, dict):
89
- hook_command = hook.get('command', '')
90
- if _is_cycode_command(hook_command):
91
- return True
92
-
93
- return False
94
-
95
-
96
- def _load_policy(policy_path: Path) -> dict:
97
- """Load existing policy file merged with defaults, or return defaults if not found."""
63
+ def _load_policy_dict(policy_path: Path) -> dict:
98
64
  if not policy_path.exists():
99
65
  return copy.deepcopy(DEFAULT_POLICY)
100
66
  try:
@@ -107,22 +73,13 @@ def _load_policy(policy_path: Path) -> dict:
107
73
  def create_policy_file(scope: str, mode: PolicyMode, repo_path: Optional[Path] = None) -> tuple[bool, str]:
108
74
  """Create or update the ai-guardrails.yaml policy file.
109
75
 
110
- If the file already exists, only the mode field is updated.
111
- If it doesn't exist, a new file is created from the default policy.
112
-
113
- Args:
114
- scope: 'user' for user-level, 'repo' for repository-level
115
- mode: The policy mode to set
116
- repo_path: Repository path (required if scope is 'repo')
117
-
118
- Returns:
119
- Tuple of (success, message)
76
+ If the file already exists, only the mode field is updated; otherwise a new
77
+ file is created from the default policy.
120
78
  """
121
79
  config_dir = repo_path / '.cycode' if scope == 'repo' and repo_path else Path.home() / '.cycode'
122
80
  policy_path = config_dir / POLICY_FILE_NAME
123
81
 
124
- policy = _load_policy(policy_path)
125
-
82
+ policy = _load_policy_dict(policy_path)
126
83
  policy['mode'] = mode.value
127
84
 
128
85
  try:
@@ -135,35 +92,21 @@ def create_policy_file(scope: str, mode: PolicyMode, repo_path: Optional[Path] =
135
92
 
136
93
 
137
94
  def install_hooks(
95
+ ide: IDE,
138
96
  scope: str = 'user',
139
97
  repo_path: Optional[Path] = None,
140
- ide: AIIDEType = DEFAULT_IDE,
141
98
  report_mode: bool = False,
142
99
  ) -> tuple[bool, str]:
143
- """
144
- Install Cycode AI guardrails hooks.
145
-
146
- Args:
147
- scope: 'user' for user-level hooks, 'repo' for repository-level hooks
148
- repo_path: Repository path (required if scope is 'repo')
149
- ide: The AI IDE type (default: Cursor)
150
- report_mode: If True, install hooks in async mode (non-blocking)
100
+ """Install Cycode AI guardrails hooks for ``ide``."""
101
+ hooks_path = ide.settings_path(scope, repo_path)
151
102
 
152
- Returns:
153
- Tuple of (success, message)
154
- """
155
- hooks_path = get_hooks_path(scope, repo_path, ide)
156
-
157
- # Load existing hooks or create new
158
- existing = load_hooks_file(hooks_path) or {'version': 1, 'hooks': {}}
103
+ existing = _load_hooks_file(hooks_path) or {'version': 1, 'hooks': {}}
159
104
  existing.setdefault('version', 1)
160
105
  existing.setdefault('hooks', {})
161
106
 
162
- # Get IDE-specific hooks configuration
163
- hooks_config = get_hooks_config(ide, async_mode=report_mode)
107
+ rendered = ide.render_hooks_config(async_mode=report_mode)
164
108
 
165
- # Add/update Cycode hooks
166
- for event, entries in hooks_config['hooks'].items():
109
+ for event, entries in rendered['hooks'].items():
167
110
  existing['hooks'].setdefault(event, [])
168
111
 
169
112
  # Remove any existing Cycode entries for this event
@@ -173,47 +116,31 @@ def install_hooks(
173
116
  for entry in entries:
174
117
  existing['hooks'][event].append(entry)
175
118
 
176
- # Save
177
- if save_hooks_file(hooks_path, existing):
119
+ if _save_hooks_file(hooks_path, existing):
178
120
  return True, f'AI guardrails hooks installed: {hooks_path}'
179
121
  return False, f'Failed to install hooks to {hooks_path}'
180
122
 
181
123
 
182
- def uninstall_hooks(
183
- scope: str = 'user', repo_path: Optional[Path] = None, ide: AIIDEType = DEFAULT_IDE
184
- ) -> tuple[bool, str]:
185
- """
186
- Remove Cycode AI guardrails hooks.
187
-
188
- Args:
189
- scope: 'user' for user-level hooks, 'repo' for repository-level hooks
190
- repo_path: Repository path (required if scope is 'repo')
191
- ide: The AI IDE type (default: Cursor)
192
-
193
- Returns:
194
- Tuple of (success, message)
195
- """
196
- hooks_path = get_hooks_path(scope, repo_path, ide)
124
+ def uninstall_hooks(ide: IDE, scope: str = 'user', repo_path: Optional[Path] = None) -> tuple[bool, str]:
125
+ """Remove Cycode AI guardrails hooks for ``ide``."""
126
+ hooks_path = ide.settings_path(scope, repo_path)
197
127
 
198
- existing = load_hooks_file(hooks_path)
128
+ existing = _load_hooks_file(hooks_path)
199
129
  if existing is None:
200
130
  return True, f'No hooks file found at {hooks_path}'
201
131
 
202
- # Remove Cycode entries from all events
203
132
  modified = False
204
133
  for event in list(existing.get('hooks', {}).keys()):
205
134
  original_count = len(existing['hooks'][event])
206
135
  existing['hooks'][event] = [e for e in existing['hooks'][event] if not is_cycode_hook_entry(e)]
207
136
  if len(existing['hooks'][event]) != original_count:
208
137
  modified = True
209
- # Remove empty event lists
210
138
  if not existing['hooks'][event]:
211
139
  del existing['hooks'][event]
212
140
 
213
141
  if not modified:
214
142
  return True, 'No Cycode hooks found to remove'
215
143
 
216
- # Save or delete if empty
217
144
  if not existing.get('hooks'):
218
145
  try:
219
146
  hooks_path.unlink()
@@ -222,48 +149,35 @@ def uninstall_hooks(
222
149
  logger.debug('Failed to delete hooks file', exc_info=e)
223
150
  return False, f'Failed to remove hooks file: {hooks_path}'
224
151
 
225
- if save_hooks_file(hooks_path, existing):
152
+ if _save_hooks_file(hooks_path, existing):
226
153
  return True, f'Cycode hooks removed from: {hooks_path}'
227
154
  return False, f'Failed to update hooks file: {hooks_path}'
228
155
 
229
156
 
230
- def get_hooks_status(scope: str = 'user', repo_path: Optional[Path] = None, ide: AIIDEType = DEFAULT_IDE) -> dict:
231
- """
232
- Get the status of AI guardrails hooks.
233
-
234
- Args:
235
- scope: 'user' for user-level hooks, 'repo' for repository-level hooks
236
- repo_path: Repository path (required if scope is 'repo')
237
- ide: The AI IDE type (default: Cursor)
238
-
239
- Returns:
240
- Dict with status information
241
- """
242
- hooks_path = get_hooks_path(scope, repo_path, ide)
157
+ def get_hooks_status(ide: IDE, scope: str = 'user', repo_path: Optional[Path] = None) -> dict:
158
+ """Return installation status of Cycode hooks for ``ide``."""
159
+ hooks_path = ide.settings_path(scope, repo_path)
243
160
 
244
- status = {
161
+ status: dict = {
245
162
  'scope': scope,
246
- 'ide': ide.value,
247
- 'ide_name': IDE_CONFIGS[ide].name,
163
+ 'ide': ide.name,
164
+ 'ide_name': ide.display_name,
248
165
  'hooks_path': str(hooks_path),
249
166
  'file_exists': hooks_path.exists(),
250
167
  'cycode_installed': False,
251
168
  'hooks': {},
252
169
  }
253
170
 
254
- existing = load_hooks_file(hooks_path)
171
+ existing = _load_hooks_file(hooks_path)
255
172
  if existing is None:
256
173
  return status
257
174
 
258
- # Check each hook event for this IDE
259
- ide_config = IDE_CONFIGS[ide]
260
175
  has_cycode_hooks = False
261
- for event in ide_config.hook_events:
262
- # Handle event:matcher format
176
+ for event in ide.hook_events:
177
+ # '<event>:<matcher>' filters entries to a specific tool/matcher.
263
178
  if ':' in event:
264
179
  actual_event, matcher_prefix = event.split(':', 1)
265
180
  all_entries = existing.get('hooks', {}).get(actual_event, [])
266
- # Filter entries by matcher
267
181
  entries = [e for e in all_entries if e.get('matcher', '').startswith(matcher_prefix)]
268
182
  else:
269
183
  entries = existing.get('hooks', {}).get(event, [])
@@ -278,5 +192,4 @@ def get_hooks_status(scope: str = 'user', repo_path: Optional[Path] = None, ide:
278
192
  }
279
193
 
280
194
  status['cycode_installed'] = has_cycode_hooks
281
-
282
195
  return status
@@ -0,0 +1,44 @@
1
+ """Registry of supported AI guardrails IDE integrations.
2
+
3
+ Adding a new IDE: create `ides/<name>.py` with a subclass of `IDE`, import it
4
+ here, and include an instance in the `IDES` tuple. Nothing else in the package
5
+ needs to change.
6
+ """
7
+
8
+ import typer
9
+
10
+ from cycode.cli.apps.ai_guardrails.ides.base import IDE
11
+ from cycode.cli.apps.ai_guardrails.ides.claude_code import ClaudeCode
12
+ from cycode.cli.apps.ai_guardrails.ides.cursor import Cursor
13
+
14
+ # Single source of truth: name → singleton instance.
15
+ # `--ide` choices and install/uninstall/status iteration both derive from this.
16
+ IDES: dict[str, IDE] = {ide.name: ide for ide in (Cursor(), ClaudeCode())}
17
+
18
+ # Default IDE used when `--ide` is omitted. Kept here so the value is colocated
19
+ # with the registry; no module outside `ides/` needs to know which IDE wins.
20
+ DEFAULT_IDE_NAME = 'cursor'
21
+
22
+
23
+ def get_ide(name: str) -> IDE:
24
+ """Look up the IDE integration registered under ``name``.
25
+
26
+ Raises ``typer.BadParameter`` when the name is unknown — surfaces as a
27
+ user-friendly CLI error rather than a KeyError stack trace.
28
+ """
29
+ ide = IDES.get(name.lower())
30
+ if ide is None:
31
+ valid = ', '.join(IDES.keys())
32
+ raise typer.BadParameter(f'Unknown IDE "{name}". Supported: {valid}.')
33
+ return ide
34
+
35
+
36
+ def resolve_ides(name: str) -> list[IDE]:
37
+ """Resolve an ``--ide`` argument to one or all IDE instances.
38
+
39
+ ``"all"`` returns every registered IDE; anything else returns a single
40
+ matching IDE (raising ``typer.BadParameter`` for unknown names).
41
+ """
42
+ if name.lower() == 'all':
43
+ return list(IDES.values())
44
+ return [get_ide(name)]