-
Notifications
You must be signed in to change notification settings - Fork 66
CM-71972: Collect Claude Code skills in the Guardrails session sweep #538
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Altruistus
wants to merge
4
commits into
main
Choose a base branch
from
CM-71972-be-implement-skills-gathering-using-cli
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fcc2d6f
CM-71972: Collect Claude Code skills in the Guardrails session sweep
Altruistus 705985b
CM-71972: Collect plugin skills for every IDE, not just Claude Code
Altruistus 5f646b0
CM-71972: Resolve the skills directory at call time, not at import
Altruistus d9c1d44
CM-71972: Patch home with a plain function in the regression test
Altruistus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| """Shared skill-collection helpers for IDE integrations. | ||
|
|
||
| A skill is a directory holding a ``SKILL.md``: ``<skills root>/<skill name>/SKILL.md``. | ||
| The same layout is used for user-scope skills (``~/.claude/skills/``) and for the skills a | ||
| plugin ships (``<plugin dir>/skills/``), so one walker serves both. | ||
|
|
||
| Unlike an MCP config - a small JSON file at a known path - a ``SKILL.md`` body is unbounded | ||
| prose, and the number of installed skills is unbounded too. Both are capped here rather than | ||
| downstream: the whole session-context report is one request, so an oversized skill would cost | ||
| the device its MCP inventory as well. | ||
| """ | ||
|
|
||
| from pathlib import Path | ||
| from typing import Optional | ||
|
|
||
| from cycode.logger import get_logger | ||
|
|
||
| logger = get_logger('AI Guardrails Skills') | ||
|
|
||
| SKILL_FILE_NAME = 'SKILL.md' | ||
|
|
||
| # Where a plugin keeps its skills, relative to the plugin directory. A property of the plugin format | ||
| # rather than of any one IDE, so Claude Code, Codex and Copilot plugins all use it. | ||
| PLUGIN_SKILLS_SUBDIR = 'skills' | ||
|
|
||
| # A skill is instructions, not data. Anything larger is not a skill we can usefully inventory, | ||
| # and sending it would push the one-request report toward the API's body limit. | ||
| MAX_SKILL_FILE_BYTES = 256 * 1024 | ||
|
|
||
| # Per skills root, not per device: a developer with more installed skills than this in one place | ||
| # is an outlier we would rather truncate than let define the payload size. | ||
| MAX_SKILLS_PER_ROOT = 200 | ||
|
|
||
|
|
||
| def _read_skill_file(skill_file: Path) -> Optional[dict]: | ||
| """Read one ``SKILL.md`` into the session-context file shape, or None if unusable.""" | ||
| try: | ||
| size = skill_file.stat().st_size | ||
| except OSError as e: | ||
| logger.debug('Failed to stat skill file, %s', {'path': str(skill_file)}, exc_info=e) | ||
| return None | ||
|
|
||
| if size > MAX_SKILL_FILE_BYTES: | ||
| logger.debug( | ||
| 'Skill file exceeds the size cap; skipping, %s', | ||
| {'path': str(skill_file), 'size': size, 'cap': MAX_SKILL_FILE_BYTES}, | ||
| ) | ||
| return None | ||
|
|
||
| try: | ||
| content = skill_file.read_text(encoding='utf-8') | ||
| except Exception as e: | ||
| logger.debug('Failed to read skill file, %s', {'path': str(skill_file)}, exc_info=e) | ||
| return None | ||
|
|
||
| if not content.strip(): | ||
| return None | ||
|
|
||
| return {'path': str(skill_file), 'content': content} | ||
|
|
||
|
|
||
| def walk_skill_dirs(skills_root: Path) -> list[dict]: | ||
| """Collect every ``<skills_root>/<name>/SKILL.md`` as ``{"path", "content"}``. | ||
|
|
||
| Exactly one directory level is scanned. A skill directory may hold nested references and | ||
| scripts, but its ``SKILL.md`` always sits at the top of it, so there is nothing to recurse | ||
| into - which is also what keeps this bounded without a depth cap. | ||
|
|
||
| Results are sorted by path: the session-context report is deduplicated by hashing the whole | ||
| payload, so an unstable order would re-send an unchanged inventory. | ||
| """ | ||
| if not skills_root.is_dir(): | ||
| return [] | ||
|
|
||
| try: | ||
| skill_dirs = sorted(d for d in skills_root.iterdir() if d.is_dir()) | ||
| except OSError as e: | ||
| logger.debug('Failed to list skills root, %s', {'path': str(skills_root)}, exc_info=e) | ||
| return [] | ||
|
|
||
| skills: list[dict] = [] | ||
| for skill_dir in skill_dirs: | ||
| if len(skills) >= MAX_SKILLS_PER_ROOT: | ||
| logger.debug( | ||
| 'Skills root exceeds the count cap; truncating, %s', | ||
| {'path': str(skills_root), 'cap': MAX_SKILLS_PER_ROOT}, | ||
| ) | ||
| break | ||
|
|
||
| skill = _read_skill_file(skill_dir / SKILL_FILE_NAME) | ||
| if skill: | ||
| skills.append(skill) | ||
|
|
||
| return skills | ||
|
|
||
|
|
||
| def walk_plugin_skills(plugin_dir: Path) -> list[dict]: | ||
| """Collect the skills a plugin ships, from ``<plugin_dir>/skills/<name>/SKILL.md``. | ||
|
|
||
| Shared by every IDE with a plugin system: the layout belongs to the plugin format, so a plugin | ||
| shipping skills is inventoried whichever IDE loaded it. | ||
| """ | ||
| return walk_skill_dirs(plugin_dir / PLUGIN_SKILLS_SUBDIR) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,6 +37,7 @@ | |
| load_plugin_json, | ||
| walk_enabled_plugins, | ||
| ) | ||
| from cycode.cli.apps.ai_guardrails.ides._skill_utils import walk_plugin_skills | ||
| from cycode.cli.apps.ai_guardrails.ides.base import IDE, DecisionAction, HookDecision | ||
| from cycode.cli.apps.ai_guardrails.scan.payload import AIHookPayload | ||
| from cycode.cli.apps.ai_guardrails.scan.types import AiHookEventType | ||
|
|
@@ -178,6 +179,12 @@ def _read_copilot_plugin(plugin_dir: Path) -> tuple[dict, dict]: | |
| if field in manifest: | ||
| entry[field] = manifest[field] | ||
|
|
||
| # Same plugin-format layout as every other IDE's plugins, so a plugin shipping skills is | ||
| # inventoried whichever IDE loaded it. | ||
| skill_files = walk_plugin_skills(plugin_dir) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about plugin skills for cursor? |
||
| if skill_files: | ||
| entry['skill_files'] = skill_files | ||
|
|
||
| mcp_ref = manifest.get('mcpServers') | ||
| mcp_config_path = plugin_dir / mcp_ref if isinstance(mcp_ref, str) else plugin_dir / '.mcp.json' | ||
| mcp_doc = load_plugin_json(mcp_config_path) or {} | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about supporting other agents like codex etc.