koder 0.4.5__tar.gz → 0.4.6__tar.gz

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 (70) hide show
  1. {koder-0.4.5 → koder-0.4.6}/.claude/settings.local.json +3 -1
  2. {koder-0.4.5 → koder-0.4.6}/PKG-INFO +1 -1
  3. {koder-0.4.5 → koder-0.4.6}/koder_agent/agentic/agent.py +0 -3
  4. {koder-0.4.5 → koder-0.4.6}/koder_agent/agentic/skill_guardrail.py +11 -2
  5. {koder-0.4.5 → koder-0.4.6}/koder_agent/cli.py +11 -17
  6. {koder-0.4.5 → koder-0.4.6}/koder_agent/tools/__init__.py +19 -4
  7. koder-0.4.6/koder_agent/tools/skill_context.py +229 -0
  8. {koder-0.4.5 → koder-0.4.6}/pyproject.toml +1 -1
  9. koder-0.4.6/tests/test_skill_restrictions.py +713 -0
  10. {koder-0.4.5 → koder-0.4.6}/uv.lock +4 -4
  11. koder-0.4.5/koder_agent/tools/skill_context.py +0 -127
  12. koder-0.4.5/tests/test_skill_restrictions.py +0 -340
  13. {koder-0.4.5 → koder-0.4.6}/.github/dependabot.yml +0 -0
  14. {koder-0.4.5 → koder-0.4.6}/.gitignore +0 -0
  15. {koder-0.4.5 → koder-0.4.6}/AGENTS.md +0 -0
  16. {koder-0.4.5 → koder-0.4.6}/CLAUDE.md +0 -0
  17. {koder-0.4.5 → koder-0.4.6}/CONTRIBUTING.md +0 -0
  18. {koder-0.4.5 → koder-0.4.6}/GEMINI.md +0 -0
  19. {koder-0.4.5 → koder-0.4.6}/LICENSE +0 -0
  20. {koder-0.4.5 → koder-0.4.6}/README.md +0 -0
  21. {koder-0.4.5 → koder-0.4.6}/koder_agent/__init__.py +0 -0
  22. {koder-0.4.5 → koder-0.4.6}/koder_agent/agentic/__init__.py +0 -0
  23. {koder-0.4.5 → koder-0.4.6}/koder_agent/agentic/approval_hooks.py +0 -0
  24. {koder-0.4.5 → koder-0.4.6}/koder_agent/agentic/hooks.py +0 -0
  25. {koder-0.4.5 → koder-0.4.6}/koder_agent/config/__init__.py +0 -0
  26. {koder-0.4.5 → koder-0.4.6}/koder_agent/config/cli_handler.py +0 -0
  27. {koder-0.4.5 → koder-0.4.6}/koder_agent/config/manager.py +0 -0
  28. {koder-0.4.5 → koder-0.4.6}/koder_agent/config/models.py +0 -0
  29. {koder-0.4.5 → koder-0.4.6}/koder_agent/core/__init__.py +0 -0
  30. {koder-0.4.5 → koder-0.4.6}/koder_agent/core/commands.py +0 -0
  31. {koder-0.4.5 → koder-0.4.6}/koder_agent/core/interactive.py +0 -0
  32. {koder-0.4.5 → koder-0.4.6}/koder_agent/core/keyboard_listener.py +0 -0
  33. {koder-0.4.5 → koder-0.4.6}/koder_agent/core/scheduler.py +0 -0
  34. {koder-0.4.5 → koder-0.4.6}/koder_agent/core/security.py +0 -0
  35. {koder-0.4.5 → koder-0.4.6}/koder_agent/core/session.py +0 -0
  36. {koder-0.4.5 → koder-0.4.6}/koder_agent/core/status_line.py +0 -0
  37. {koder-0.4.5 → koder-0.4.6}/koder_agent/core/streaming_display.py +0 -0
  38. {koder-0.4.5 → koder-0.4.6}/koder_agent/core/usage_tracker.py +0 -0
  39. {koder-0.4.5 → koder-0.4.6}/koder_agent/mcp/__init__.py +0 -0
  40. {koder-0.4.5 → koder-0.4.6}/koder_agent/mcp/cli_handler.py +0 -0
  41. {koder-0.4.5 → koder-0.4.6}/koder_agent/mcp/server_config.py +0 -0
  42. {koder-0.4.5 → koder-0.4.6}/koder_agent/mcp/server_factory.py +0 -0
  43. {koder-0.4.5 → koder-0.4.6}/koder_agent/mcp/server_manager.py +0 -0
  44. {koder-0.4.5 → koder-0.4.6}/koder_agent/tools/engine.py +0 -0
  45. {koder-0.4.5 → koder-0.4.6}/koder_agent/tools/file.py +0 -0
  46. {koder-0.4.5 → koder-0.4.6}/koder_agent/tools/search.py +0 -0
  47. {koder-0.4.5 → koder-0.4.6}/koder_agent/tools/shell.py +0 -0
  48. {koder-0.4.5 → koder-0.4.6}/koder_agent/tools/skill.py +0 -0
  49. {koder-0.4.5 → koder-0.4.6}/koder_agent/tools/task.py +0 -0
  50. {koder-0.4.5 → koder-0.4.6}/koder_agent/tools/todo.py +0 -0
  51. {koder-0.4.5 → koder-0.4.6}/koder_agent/tools/web.py +0 -0
  52. {koder-0.4.5 → koder-0.4.6}/koder_agent/utils/__init__.py +0 -0
  53. {koder-0.4.5 → koder-0.4.6}/koder_agent/utils/client.py +0 -0
  54. {koder-0.4.5 → koder-0.4.6}/koder_agent/utils/model_info.py +0 -0
  55. {koder-0.4.5 → koder-0.4.6}/koder_agent/utils/prompts.py +0 -0
  56. {koder-0.4.5 → koder-0.4.6}/koder_agent/utils/queue.py +0 -0
  57. {koder-0.4.5 → koder-0.4.6}/koder_agent/utils/sessions.py +0 -0
  58. {koder-0.4.5 → koder-0.4.6}/koder_agent/utils/terminal_theme.py +0 -0
  59. {koder-0.4.5 → koder-0.4.6}/scripts/upgrade_dependency.py +0 -0
  60. {koder-0.4.5 → koder-0.4.6}/tests/fixtures/skills/large-skill/SKILL.md +0 -0
  61. {koder-0.4.5 → koder-0.4.6}/tests/fixtures/skills/large-skill/reference.md +0 -0
  62. {koder-0.4.5 → koder-0.4.6}/tests/fixtures/skills/medium-skill/SKILL.md +0 -0
  63. {koder-0.4.5 → koder-0.4.6}/tests/fixtures/skills/small-skill/SKILL.md +0 -0
  64. {koder-0.4.5 → koder-0.4.6}/tests/fixtures/skills/template-skill/SKILL.md +0 -0
  65. {koder-0.4.5 → koder-0.4.6}/tests/test_file_tools.py +0 -0
  66. {koder-0.4.5 → koder-0.4.6}/tests/test_model_resolution.py +0 -0
  67. {koder-0.4.5 → koder-0.4.6}/tests/test_shell_background.py +0 -0
  68. {koder-0.4.5 → koder-0.4.6}/tests/test_skill_loader.py +0 -0
  69. {koder-0.4.5 → koder-0.4.6}/tests/test_skill_token_efficiency.py +0 -0
  70. {koder-0.4.5 → koder-0.4.6}/tests/test_web.py +0 -0
@@ -9,7 +9,9 @@
9
9
  "Bash(find:*)",
10
10
  "WebFetch(domain:www.anthropic.com)",
11
11
  "Bash(uv run ruff check:*)",
12
- "mcp__firecrawl__firecrawl_scrape"
12
+ "mcp__firecrawl__firecrawl_scrape",
13
+ "Bash(uv run:*)",
14
+ "Bash(uv run pip:*)"
13
15
  ],
14
16
  "deny": [],
15
17
  "ask": []
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: koder
3
- Version: 0.4.5
3
+ Version: 0.4.6
4
4
  Summary: An intuitive AI coding assistant and interactive CLI tool that boosts developer productivity with intelligent automation and context-aware support.
5
5
  Project-URL: Homepage, https://github.com/feiskyer/koder
6
6
  Project-URL: Repository, https://github.com/feiskyer/koder
@@ -17,7 +17,6 @@ from ..tools.skill import SkillLoader
17
17
  from ..utils.client import get_litellm_model_kwargs, get_model_name, is_native_openai_provider
18
18
  from ..utils.model_info import get_maximum_output_tokens
19
19
  from ..utils.prompts import KODER_SYSTEM_PROMPT
20
- from .skill_guardrail import skill_restriction_guardrail
21
20
 
22
21
  console = Console()
23
22
  logger = logging.getLogger(__name__)
@@ -130,8 +129,6 @@ async def create_dev_agent(tools) -> Agent:
130
129
  tools=tools,
131
130
  mcp_servers=mcp_servers,
132
131
  model_settings=model_settings,
133
- # Add skill-based tool restriction enforcement
134
- input_guardrails=[skill_restriction_guardrail],
135
132
  )
136
133
 
137
134
  if "github_copilot" in model_name_str:
@@ -26,6 +26,12 @@ def skill_tool_restriction_guardrail(
26
26
  (plus always-allowed tools like get_skill, todo_read, todo_write)
27
27
  can be used.
28
28
 
29
+ Supports pattern matching for fine-grained control:
30
+ - "read_file" - Exact tool name match
31
+ - "run_shell:git *" - Shell commands matching glob pattern
32
+ - "run_shell:*" - All shell commands allowed
33
+ - "*" - Wildcard, all tools allowed
34
+
29
35
  Args:
30
36
  data: The guardrail input data containing tool context
31
37
 
@@ -47,8 +53,11 @@ def skill_tool_restriction_guardrail(
47
53
  output_info={"error": "missing_tool_name"},
48
54
  )
49
55
 
50
- # Check if tool is allowed
51
- if restrictions.is_tool_allowed(tool_name):
56
+ # Get tool arguments for pattern matching (e.g., shell command matching)
57
+ tool_args = getattr(data.context, "tool_arguments", None)
58
+
59
+ # Check if tool is allowed (with pattern matching support)
60
+ if restrictions.is_tool_allowed(tool_name, tool_args):
52
61
  return ToolGuardrailFunctionOutput.allow()
53
62
 
54
63
  # Tool is not allowed - reject with informative message
@@ -132,11 +132,8 @@ async def main():
132
132
  parser.add_argument(
133
133
  "--resume", action="store_true", help="List and select a previous session to resume"
134
134
  )
135
- stream_group = parser.add_mutually_exclusive_group()
136
- stream_group.add_argument(
137
- "--stream", action="store_true", help="Force enable streaming mode"
138
- )
139
- stream_group.add_argument("--no-stream", action="store_true", help="Disable streaming mode")
135
+ parser.add_argument("--no-stream", action="store_true", help="Disable streaming mode")
136
+ parser.add_argument("--debug", action="store_true", help="Enable debug logging")
140
137
 
141
138
  subparsers = parser.add_subparsers(dest="command", help="Available commands")
142
139
  create_mcp_subparsers(subparsers)
@@ -148,11 +145,8 @@ async def main():
148
145
  parser.add_argument(
149
146
  "--resume", action="store_true", help="List and select a previous session to resume"
150
147
  )
151
- stream_group = parser.add_mutually_exclusive_group()
152
- stream_group.add_argument(
153
- "--stream", action="store_true", help="Force enable streaming mode"
154
- )
155
- stream_group.add_argument("--no-stream", action="store_true", help="Disable streaming mode")
148
+ parser.add_argument("--no-stream", action="store_true", help="Disable streaming mode")
149
+ parser.add_argument("--debug", action="store_true", help="Enable debug logging")
156
150
 
157
151
  parser.add_argument(
158
152
  "prompt", nargs="*", help="Prompt text (if not provided, starts interactive mode)"
@@ -160,6 +154,11 @@ async def main():
160
154
 
161
155
  args = parser.parse_args()
162
156
 
157
+ # Configure logging level based on --debug flag
158
+ if args.debug:
159
+ logging.getLogger().setLevel(logging.DEBUG)
160
+ logging.debug("Debug logging enabled")
161
+
163
162
  # Set command to None for prompt mode
164
163
  if not hasattr(args, "command"):
165
164
  args.command = None
@@ -195,13 +194,8 @@ async def main():
195
194
 
196
195
  context = await load_context()
197
196
 
198
- # Determine streaming setting: CLI flag overrides config
199
- if args.stream:
200
- streaming = True
201
- elif args.no_stream:
202
- streaming = False
203
- else:
204
- streaming = config.cli.stream
197
+ # Determine streaming setting: --no-stream overrides config default
198
+ streaming = config.cli.stream and not args.no_stream
205
199
 
206
200
  scheduler = AgentScheduler(session_id=args.session, streaming=streaming)
207
201
 
@@ -2,8 +2,9 @@
2
2
 
3
3
  from typing import List
4
4
 
5
- from agents import Tool
5
+ from agents import FunctionTool, Tool
6
6
 
7
+ from ..agentic.skill_guardrail import skill_restriction_guardrail
7
8
  from .engine import ToolEngine
8
9
  from .file import (
9
10
  FileEditModel,
@@ -64,7 +65,11 @@ tool_engine.register(GitModel)(git_command)
64
65
 
65
66
 
66
67
  def get_all_tools() -> List[Tool]:
67
- """Get all registered tools as a list."""
68
+ """Get all registered tools as a list.
69
+
70
+ Each FunctionTool is configured with the skill_restriction_guardrail
71
+ to enforce skill-based tool restrictions when skills are active.
72
+ """
68
73
  # Collect all @function_tool decorated functions directly
69
74
  tools = [
70
75
  read_file,
@@ -86,8 +91,18 @@ def get_all_tools() -> List[Tool]:
86
91
  get_skill,
87
92
  ]
88
93
 
89
- # Filter to only include properly decorated tools
90
- return [tool for tool in tools if hasattr(tool, "name")]
94
+ # Filter to only include properly decorated tools and attach guardrails
95
+ result = []
96
+ for tool in tools:
97
+ if hasattr(tool, "name"):
98
+ # Attach skill restriction guardrail to each FunctionTool
99
+ if isinstance(tool, FunctionTool):
100
+ if tool.tool_input_guardrails is None:
101
+ tool.tool_input_guardrails = [skill_restriction_guardrail]
102
+ elif skill_restriction_guardrail not in tool.tool_input_guardrails:
103
+ tool.tool_input_guardrails.append(skill_restriction_guardrail)
104
+ result.append(tool)
105
+ return result
91
106
 
92
107
 
93
108
  __all__ = [
@@ -0,0 +1,229 @@
1
+ """Skill context manager for tracking active skill restrictions.
2
+
3
+ This module provides async-safe state management for skill-based tool restrictions
4
+ using Python's contextvars. When a skill with `allowed_tools` is loaded, only
5
+ those tools (plus always-allowed tools) can be used.
6
+
7
+ The restriction model uses UNION semantics:
8
+ - Multiple skills with `allowed_tools` accumulate their allowed tools
9
+ - Loading a skill without `allowed_tools` clears all restrictions
10
+
11
+ Pattern syntax for allowed_tools:
12
+ - "read_file" - Exact tool name match
13
+ - "run_shell:git *" - Shell commands matching glob pattern
14
+ - "run_shell:*" - All shell commands allowed
15
+ - "*" - Wildcard, all tools allowed
16
+
17
+ Note on empty `allowed_tools`:
18
+ - A skill with `allowed_tools: []` (empty list) is treated as "no restrictions"
19
+ - This is intentional: empty means "didn't specify restrictions", not "block all"
20
+ - To block all tools, you would need explicit tooling support (not yet implemented)
21
+ """
22
+
23
+ from __future__ import annotations
24
+
25
+ import fnmatch
26
+ import json
27
+ from contextvars import ContextVar
28
+ from dataclasses import dataclass, field
29
+ from typing import TYPE_CHECKING, ClassVar, Optional
30
+
31
+ if TYPE_CHECKING:
32
+ from .skill import Skill
33
+
34
+ # Context variable to track active skill restrictions (async-safe)
35
+ _active_restrictions: ContextVar[Optional["SkillRestrictions"]] = ContextVar(
36
+ "active_skill_restrictions", default=None
37
+ )
38
+
39
+
40
+ @dataclass
41
+ class SkillRestrictions:
42
+ """Tracks tool restrictions from active skills.
43
+
44
+ Uses union semantics: tools from multiple loaded skills are combined.
45
+
46
+ Pattern syntax for allowed_tools:
47
+ - "read_file" - Exact tool name match
48
+ - "run_shell:git *" - Shell commands matching glob pattern
49
+ - "run_shell:*" - All shell commands allowed
50
+ - "*" - Wildcard, all tools allowed
51
+ """
52
+
53
+ # Names of skills that contributed to the current restrictions
54
+ loaded_skills: list[str] = field(default_factory=list)
55
+
56
+ # Union of all allowed tools from loaded skills (may include patterns)
57
+ allowed_tools: set[str] = field(default_factory=set)
58
+
59
+ # Tools that should always be allowed regardless of skill restrictions
60
+ # - get_skill: Must be able to load different skills to change/escape restrictions
61
+ # - todo_read, todo_write: Task management shouldn't be blocked
62
+ ALWAYS_ALLOWED: ClassVar[frozenset[str]] = frozenset({"get_skill", "todo_read", "todo_write"})
63
+
64
+ def is_tool_allowed(self, tool_name: str, tool_args: Optional[str] = None) -> bool:
65
+ """Check if a tool is allowed under current restrictions.
66
+
67
+ Supports pattern matching:
68
+ - Exact match: "read_file" matches tool_name="read_file"
69
+ - Wildcard: "*" matches any tool
70
+ - Command pattern: "run_shell:git *" matches run_shell with command starting with "git "
71
+
72
+ Args:
73
+ tool_name: The name of the tool to check
74
+ tool_args: JSON string of tool arguments (for command pattern matching)
75
+
76
+ Returns:
77
+ True if the tool is allowed, False otherwise
78
+ """
79
+ # Always-allowed tools bypass restrictions
80
+ if tool_name in SkillRestrictions.ALWAYS_ALLOWED:
81
+ return True
82
+
83
+ # If no restrictions defined, allow all
84
+ if not self.allowed_tools:
85
+ return True
86
+
87
+ # Check each allowed pattern
88
+ for pattern in self.allowed_tools:
89
+ if self._matches_pattern(pattern, tool_name, tool_args):
90
+ return True
91
+
92
+ return False
93
+
94
+ def _matches_pattern(
95
+ self, pattern: str, tool_name: str, tool_args: Optional[str] = None
96
+ ) -> bool:
97
+ """Check if a tool call matches an allowed pattern.
98
+
99
+ Args:
100
+ pattern: The allowed pattern (e.g., "read_file", "run_shell:git *", "*")
101
+ tool_name: The actual tool name being called
102
+ tool_args: JSON string of tool arguments
103
+
104
+ Returns:
105
+ True if the pattern matches the tool call
106
+ """
107
+ # Universal wildcard - matches everything
108
+ if pattern == "*":
109
+ return True
110
+
111
+ # Check for command pattern syntax: "tool_name:command_pattern"
112
+ if ":" in pattern:
113
+ pattern_tool, command_pattern = pattern.split(":", 1)
114
+
115
+ # Tool name must match exactly
116
+ if pattern_tool != tool_name:
117
+ return False
118
+
119
+ # For run_shell, match against the command argument
120
+ if tool_name == "run_shell" and tool_args:
121
+ return self._matches_shell_command(command_pattern, tool_args)
122
+
123
+ # For git_command, match against the args argument
124
+ if tool_name == "git_command" and tool_args:
125
+ return self._matches_git_command(command_pattern, tool_args)
126
+
127
+ # Pattern with ":" but no matching logic - treat as no match
128
+ return False
129
+
130
+ # Exact tool name match (or glob pattern on tool name)
131
+ return fnmatch.fnmatch(tool_name, pattern)
132
+
133
+ def _matches_shell_command(self, pattern: str, tool_args: str) -> bool:
134
+ """Match a shell command against a glob pattern.
135
+
136
+ Args:
137
+ pattern: Glob pattern to match (e.g., "git *", "cat *", "*")
138
+ tool_args: JSON string containing {"command": "..."}
139
+
140
+ Returns:
141
+ True if the command matches the pattern
142
+ """
143
+ try:
144
+ args = json.loads(tool_args)
145
+ if not isinstance(args, dict):
146
+ return False
147
+ command = args.get("command", "")
148
+ return fnmatch.fnmatch(command, pattern)
149
+ except (json.JSONDecodeError, TypeError, AttributeError):
150
+ return False
151
+
152
+ def _matches_git_command(self, pattern: str, tool_args: str) -> bool:
153
+ """Match a git command against a glob pattern.
154
+
155
+ Args:
156
+ pattern: Glob pattern to match (e.g., "status", "commit *", "*")
157
+ tool_args: JSON string containing {"args": "..."}
158
+
159
+ Returns:
160
+ True if the git args match the pattern
161
+ """
162
+ try:
163
+ args = json.loads(tool_args)
164
+ if not isinstance(args, dict):
165
+ return False
166
+ git_args = args.get("args", "")
167
+ return fnmatch.fnmatch(git_args, pattern)
168
+ except (json.JSONDecodeError, TypeError, AttributeError):
169
+ return False
170
+
171
+ def add_skill(self, skill_name: str, tools: list[str]) -> None:
172
+ """Add a skill's allowed tools to the union.
173
+
174
+ Args:
175
+ skill_name: Name of the skill being added
176
+ tools: List of tools the skill allows
177
+ """
178
+ if skill_name not in self.loaded_skills:
179
+ self.loaded_skills.append(skill_name)
180
+ self.allowed_tools.update(tools)
181
+
182
+
183
+ def get_active_restrictions() -> Optional[SkillRestrictions]:
184
+ """Get the currently active skill restrictions.
185
+
186
+ Returns:
187
+ SkillRestrictions instance if restrictions are active, None otherwise
188
+ """
189
+ return _active_restrictions.get()
190
+
191
+
192
+ def clear_restrictions() -> None:
193
+ """Clear any active skill restrictions.
194
+
195
+ Called when a skill without `allowed_tools` is loaded, or to reset state.
196
+ """
197
+ _active_restrictions.set(None)
198
+
199
+
200
+ def add_skill_restrictions(skill: "Skill") -> None:
201
+ """Add tool restrictions from a loaded skill.
202
+
203
+ Uses union semantics: if restrictions already exist, the skill's
204
+ allowed tools are added to the existing set.
205
+
206
+ Args:
207
+ skill: The skill whose restrictions should be added
208
+ """
209
+ if not skill.allowed_tools:
210
+ return
211
+
212
+ current = _active_restrictions.get()
213
+
214
+ if current is None:
215
+ # First skill with restrictions
216
+ current = SkillRestrictions()
217
+ _active_restrictions.set(current)
218
+
219
+ current.add_skill(skill.name, skill.allowed_tools)
220
+
221
+
222
+ def has_active_restrictions() -> bool:
223
+ """Check if any skill restrictions are currently active.
224
+
225
+ Returns:
226
+ True if restrictions are active, False otherwise
227
+ """
228
+ restrictions = _active_restrictions.get()
229
+ return restrictions is not None and bool(restrictions.allowed_tools)
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "koder"
3
- version = "0.4.5"
3
+ version = "0.4.6"
4
4
  description = "An intuitive AI coding assistant and interactive CLI tool that boosts developer productivity with intelligent automation and context-aware support."
5
5
  authors = [{ name = "Pengfei Ni" }]
6
6
  readme = "README.md"