ripperdoc 0.2.0__py3-none-any.whl → 0.2.3__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.
- ripperdoc/__init__.py +1 -1
- ripperdoc/cli/cli.py +74 -9
- ripperdoc/cli/commands/__init__.py +4 -0
- ripperdoc/cli/commands/agents_cmd.py +30 -4
- ripperdoc/cli/commands/context_cmd.py +11 -1
- ripperdoc/cli/commands/cost_cmd.py +5 -0
- ripperdoc/cli/commands/doctor_cmd.py +208 -0
- ripperdoc/cli/commands/memory_cmd.py +202 -0
- ripperdoc/cli/commands/models_cmd.py +61 -6
- ripperdoc/cli/commands/resume_cmd.py +4 -2
- ripperdoc/cli/commands/status_cmd.py +1 -1
- ripperdoc/cli/commands/tasks_cmd.py +27 -0
- ripperdoc/cli/ui/rich_ui.py +258 -11
- ripperdoc/cli/ui/thinking_spinner.py +128 -0
- ripperdoc/core/agents.py +14 -4
- ripperdoc/core/config.py +56 -3
- ripperdoc/core/default_tools.py +16 -2
- ripperdoc/core/permissions.py +19 -0
- ripperdoc/core/providers/__init__.py +31 -0
- ripperdoc/core/providers/anthropic.py +136 -0
- ripperdoc/core/providers/base.py +187 -0
- ripperdoc/core/providers/gemini.py +172 -0
- ripperdoc/core/providers/openai.py +142 -0
- ripperdoc/core/query.py +510 -386
- ripperdoc/core/query_utils.py +578 -0
- ripperdoc/core/system_prompt.py +2 -1
- ripperdoc/core/tool.py +16 -1
- ripperdoc/sdk/client.py +12 -1
- ripperdoc/tools/background_shell.py +63 -21
- ripperdoc/tools/bash_tool.py +48 -13
- ripperdoc/tools/file_edit_tool.py +20 -0
- ripperdoc/tools/file_read_tool.py +23 -0
- ripperdoc/tools/file_write_tool.py +20 -0
- ripperdoc/tools/glob_tool.py +59 -15
- ripperdoc/tools/grep_tool.py +7 -0
- ripperdoc/tools/ls_tool.py +246 -73
- ripperdoc/tools/mcp_tools.py +32 -10
- ripperdoc/tools/multi_edit_tool.py +23 -0
- ripperdoc/tools/notebook_edit_tool.py +18 -3
- ripperdoc/tools/task_tool.py +7 -0
- ripperdoc/tools/todo_tool.py +157 -25
- ripperdoc/tools/tool_search_tool.py +17 -4
- ripperdoc/utils/file_watch.py +134 -0
- ripperdoc/utils/git_utils.py +274 -0
- ripperdoc/utils/json_utils.py +27 -0
- ripperdoc/utils/log.py +129 -29
- ripperdoc/utils/mcp.py +71 -6
- ripperdoc/utils/memory.py +12 -1
- ripperdoc/utils/message_compaction.py +22 -5
- ripperdoc/utils/messages.py +72 -17
- ripperdoc/utils/output_utils.py +34 -9
- ripperdoc/utils/permissions/path_validation_utils.py +6 -0
- ripperdoc/utils/prompt.py +17 -0
- ripperdoc/utils/safe_get_cwd.py +4 -0
- ripperdoc/utils/session_history.py +27 -9
- ripperdoc/utils/session_usage.py +7 -0
- ripperdoc/utils/shell_utils.py +159 -0
- ripperdoc/utils/todo.py +2 -2
- {ripperdoc-0.2.0.dist-info → ripperdoc-0.2.3.dist-info}/METADATA +4 -2
- ripperdoc-0.2.3.dist-info/RECORD +95 -0
- ripperdoc-0.2.0.dist-info/RECORD +0 -81
- {ripperdoc-0.2.0.dist-info → ripperdoc-0.2.3.dist-info}/WHEEL +0 -0
- {ripperdoc-0.2.0.dist-info → ripperdoc-0.2.3.dist-info}/entry_points.txt +0 -0
- {ripperdoc-0.2.0.dist-info → ripperdoc-0.2.3.dist-info}/licenses/LICENSE +0 -0
- {ripperdoc-0.2.0.dist-info → ripperdoc-0.2.3.dist-info}/top_level.txt +0 -0
ripperdoc/tools/todo_tool.py
CHANGED
|
@@ -14,6 +14,7 @@ from ripperdoc.core.tool import (
|
|
|
14
14
|
ToolUseExample,
|
|
15
15
|
ValidationResult,
|
|
16
16
|
)
|
|
17
|
+
from ripperdoc.utils.log import get_logger
|
|
17
18
|
from ripperdoc.utils.todo import (
|
|
18
19
|
TodoItem,
|
|
19
20
|
TodoPriority,
|
|
@@ -27,9 +28,14 @@ from ripperdoc.utils.todo import (
|
|
|
27
28
|
validate_todos,
|
|
28
29
|
)
|
|
29
30
|
|
|
31
|
+
logger = get_logger()
|
|
32
|
+
|
|
33
|
+
DEFAULT_ACTION = "Edit"
|
|
34
|
+
|
|
30
35
|
TODO_WRITE_PROMPT = dedent(
|
|
31
|
-
"""\
|
|
32
|
-
Use this tool to create and manage a structured task list for your current coding session. This helps you track progress, organize complex tasks, and demonstrate thoroughness to the user.
|
|
36
|
+
f"""\
|
|
37
|
+
Use this tool to create and manage a structured task list for your current coding session. This helps you track progress, organize complex tasks, and demonstrate thoroughness to the user.
|
|
38
|
+
It also helps the user understand the progress of the task and overall progress of their requests.
|
|
33
39
|
|
|
34
40
|
## When to Use This Tool
|
|
35
41
|
Use this tool proactively in these scenarios:
|
|
@@ -52,30 +58,155 @@ TODO_WRITE_PROMPT = dedent(
|
|
|
52
58
|
|
|
53
59
|
NOTE that you should not use this tool if there is only one trivial task to do. In this case you are better off just doing the task directly.
|
|
54
60
|
|
|
61
|
+
## Examples of When to Use the Todo List
|
|
62
|
+
|
|
63
|
+
<example>
|
|
64
|
+
User: I want to add a dark mode toggle to the application settings. Make sure you run the tests and build when you're done!
|
|
65
|
+
Assistant: I'll help add a dark mode toggle to your application settings. Let me create a todo list to track this implementation.
|
|
66
|
+
*Creates todo list with the following items:*
|
|
67
|
+
1. Create dark mode toggle component in Settings page
|
|
68
|
+
2. Add dark mode state management (context/store)
|
|
69
|
+
3. Implement CSS-in-JS styles for dark theme
|
|
70
|
+
4. Update existing components to support theme switching
|
|
71
|
+
5. Run tests and build process, addressing any failures or errors that occur
|
|
72
|
+
*Begins working on the first task*
|
|
73
|
+
|
|
74
|
+
<reasoning>
|
|
75
|
+
The assistant used the todo list because:
|
|
76
|
+
1. Adding dark mode is a multi-step feature requiring UI, state management, and styling changes
|
|
77
|
+
2. The user explicitly requested tests and build be run afterward
|
|
78
|
+
3. The assistant inferred that tests and build need to pass by adding "Ensure tests and build succeed" as the final task
|
|
79
|
+
</reasoning>
|
|
80
|
+
</example>
|
|
81
|
+
|
|
82
|
+
<example>
|
|
83
|
+
User: Help me rename the function getCwd to getCurrentWorkingDirectory across my project
|
|
84
|
+
Assistant: Let me first search through your codebase to find all occurrences of 'getCwd'.
|
|
85
|
+
*Uses grep or search tools to locate all instances of getCwd in the codebase*
|
|
86
|
+
Assistant: I've found 15 instances of 'getCwd' across 8 different files. Let me create a todo list to track these changes.
|
|
87
|
+
*Creates todo list with specific items for each file that needs updating*
|
|
88
|
+
|
|
89
|
+
<reasoning>
|
|
90
|
+
The assistant used the todo list because:
|
|
91
|
+
1. First, the assistant searched to understand the scope of the task
|
|
92
|
+
2. Upon finding multiple occurrences across different files, it determined this was a complex task with multiple steps
|
|
93
|
+
3. The todo list helps ensure every instance is tracked and updated systematically
|
|
94
|
+
4. This approach prevents missing any occurrences and maintains code consistency
|
|
95
|
+
</reasoning>
|
|
96
|
+
</example>
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
<example>
|
|
100
|
+
User: I need to implement these features for my e-commerce site: user registration, product catalog, shopping cart, and checkout flow.
|
|
101
|
+
Assistant: I'll help implement these features. First, let's add all the features to the todo list.
|
|
102
|
+
*Creates a todo list breaking down each feature into specific tasks based on the project architecture*
|
|
103
|
+
Assistant: Let's start with user registration. This will involve creating database models, API endpoints, and frontend forms that integrate with your existing authentication system.
|
|
104
|
+
|
|
105
|
+
<reasoning>
|
|
106
|
+
The assistant used the todo list because:
|
|
107
|
+
1. The user provided multiple complex features to implement in a comma separated list
|
|
108
|
+
2. The todo list helps organize these large features into manageable tasks
|
|
109
|
+
3. This approach allows for tracking progress across the entire implementation
|
|
110
|
+
</reasoning>
|
|
111
|
+
</example>
|
|
112
|
+
|
|
113
|
+
<example>
|
|
114
|
+
User: Can you help optimize my React application? It's rendering slowly and has performance issues.</user>
|
|
115
|
+
Assistant: I'll help optimize your React application. First, let me examine your codebase to identify potential performance bottlenecks.
|
|
116
|
+
*Reviews component structure, render patterns, state management, and data fetching*
|
|
117
|
+
Assistant: After analyzing your codebase, I've identified several performance issues. Let me create a todo list to track our optimization efforts.
|
|
118
|
+
*Creates todo list with items like: 1) Implement memoization for expensive calculations in ProductList, 2) Add virtualization for long lists in Dashboard, 3) Optimize image loading in Gallery component, 4) Fix state update loops in ShoppingCart, 5) Review bundle size and implement code splitting*
|
|
119
|
+
Let's start by implementing memoization for the expensive calculations in your ProductList component.</assistant>
|
|
120
|
+
|
|
121
|
+
<reasoning>
|
|
122
|
+
The assistant used the todo list because:
|
|
123
|
+
1. First, the assistant examined the codebase to identify specific performance issues
|
|
124
|
+
2. Based on this analysis, it identified multiple optimization opportunities
|
|
125
|
+
3. Performance optimization is a non-trivial task requiring multiple steps
|
|
126
|
+
4. The todo list helps methodically track improvements across different components
|
|
127
|
+
5. This systematic approach ensures all performance bottlenecks are addressed
|
|
128
|
+
</reasoning>
|
|
129
|
+
</example>
|
|
130
|
+
|
|
131
|
+
## Examples of When NOT to Use the Todo List
|
|
132
|
+
|
|
133
|
+
<example>
|
|
134
|
+
User: How do I print 'Hello World' in Python?
|
|
135
|
+
Assistant: In Python, you can print "Hello World" with this simple code:
|
|
136
|
+
|
|
137
|
+
python
|
|
138
|
+
print("Hello World")
|
|
139
|
+
|
|
140
|
+
This will output the text "Hello World" to the console when executed.</assistant>
|
|
141
|
+
|
|
142
|
+
<reasoning>
|
|
143
|
+
The assistant did not use the todo list because this is a single, trivial task that can be completed in one step. There's no need to track multiple tasks or steps for such a straightforward request.
|
|
144
|
+
</reasoning>
|
|
145
|
+
</example>
|
|
146
|
+
|
|
147
|
+
<example>
|
|
148
|
+
User: What does the git status command do?
|
|
149
|
+
Assistant: The git status command shows the current state of your working directory and staging area. It displays which changes have been staged, which haven't, and which files aren't being tracked by Git. This is a fundamental command for understanding the current state of your repository before making commits.
|
|
150
|
+
|
|
151
|
+
<reasoning>
|
|
152
|
+
The assistant did not use the todo list because this is an informational request with no actual coding task to complete. The user is simply asking for an explanation, not for the assistant to perform multiple steps or tasks.
|
|
153
|
+
</reasoning>
|
|
154
|
+
</example>
|
|
155
|
+
|
|
156
|
+
<example>
|
|
157
|
+
User: Can you add a comment to the calculateTotal function to explain what it does?
|
|
158
|
+
Assistant: Sure, let me add a comment to the calculateTotal function to explain what it does.
|
|
159
|
+
* Uses the ${DEFAULT_ACTION} tool to add a comment to the calculateTotal function *
|
|
160
|
+
|
|
161
|
+
<reasoning>
|
|
162
|
+
The assistant did not use the todo list because this is a single, straightforward task confined to one location in the code. Adding a comment doesn't require tracking multiple steps or systematic organization.
|
|
163
|
+
</reasoning>
|
|
164
|
+
</example>
|
|
165
|
+
|
|
166
|
+
<example>
|
|
167
|
+
User: Run npm install for me and tell me what happens.
|
|
168
|
+
Assistant: I'll run the npm install command for you.
|
|
169
|
+
|
|
170
|
+
*Executes: npm install*
|
|
171
|
+
|
|
172
|
+
The command completed successfully. Here's the output:
|
|
173
|
+
[Output of npm install command]
|
|
174
|
+
|
|
175
|
+
All dependencies have been installed according to your package.json file.
|
|
176
|
+
|
|
177
|
+
<reasoning>
|
|
178
|
+
The assistant did not use the todo list because this is a single command execution with immediate results. There are no multiple steps to track or organize, making the todo list unnecessary for this straightforward task.
|
|
179
|
+
</reasoning>
|
|
180
|
+
</example>
|
|
181
|
+
|
|
55
182
|
## Task States and Management
|
|
56
183
|
|
|
57
|
-
1. Task States:
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
2. Task Management
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
3. Task Completion Requirements
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
184
|
+
1. **Task States**: Use these states to track progress:
|
|
185
|
+
- pending: Task not yet started
|
|
186
|
+
- in_progress: Currently working on (limit to ONE task at a time)
|
|
187
|
+
- completed: Task finished successfully
|
|
188
|
+
|
|
189
|
+
2. **Task Management**:
|
|
190
|
+
- Update task status in real-time as you work
|
|
191
|
+
- Mark tasks complete IMMEDIATELY after finishing (don't batch completions)
|
|
192
|
+
- Only have ONE task in_progress at any time
|
|
193
|
+
- Complete current tasks before starting new ones
|
|
194
|
+
- Remove tasks that are no longer relevant from the list entirely
|
|
195
|
+
|
|
196
|
+
3. **Task Completion Requirements**:
|
|
197
|
+
- ONLY mark a task as completed when you have FULLY accomplished it
|
|
198
|
+
- If you encounter errors, blockers, or cannot finish, keep the task as in_progress
|
|
199
|
+
- When blocked, create a new task describing what needs to be resolved
|
|
200
|
+
- Never mark a task as completed if:
|
|
201
|
+
- Tests are failing
|
|
202
|
+
- Implementation is partial
|
|
203
|
+
- You encountered unresolved errors
|
|
204
|
+
- You couldn't find necessary files or dependencies
|
|
205
|
+
|
|
206
|
+
4. **Task Breakdown**:
|
|
207
|
+
- Create specific, actionable items
|
|
208
|
+
- Break complex tasks into smaller, manageable steps
|
|
209
|
+
- Use clear, descriptive task names
|
|
79
210
|
|
|
80
211
|
When in doubt, use this tool. Being proactive with task management demonstrates attentiveness and ensures you complete all requirements successfully.
|
|
81
212
|
"""
|
|
@@ -130,7 +261,7 @@ class TodoWriteTool(Tool[TodoWriteToolInput, TodoToolOutput]):
|
|
|
130
261
|
return "TodoWrite"
|
|
131
262
|
|
|
132
263
|
async def description(self) -> str:
|
|
133
|
-
return "
|
|
264
|
+
return "Update the todo list for the current session. To be used proactively and often to track progress and pending tasks."
|
|
134
265
|
|
|
135
266
|
@property
|
|
136
267
|
def input_schema(self) -> type[TodoWriteToolInput]:
|
|
@@ -230,6 +361,7 @@ class TodoWriteTool(Tool[TodoWriteToolInput, TodoToolOutput]):
|
|
|
230
361
|
)
|
|
231
362
|
yield ToolResult(data=output, result_for_assistant=result_text)
|
|
232
363
|
except Exception as exc:
|
|
364
|
+
logger.exception("[todo_tool] Error updating todos", extra={"error": str(exc)})
|
|
233
365
|
error = f"Error updating todos: {exc}"
|
|
234
366
|
yield ToolResult(
|
|
235
367
|
data=TodoToolOutput(
|
|
@@ -19,6 +19,10 @@ from ripperdoc.core.tool import (
|
|
|
19
19
|
ValidationResult,
|
|
20
20
|
build_tool_description,
|
|
21
21
|
)
|
|
22
|
+
from ripperdoc.utils.log import get_logger
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
logger = get_logger()
|
|
22
26
|
|
|
23
27
|
|
|
24
28
|
class ToolSearchInput(BaseModel):
|
|
@@ -115,7 +119,9 @@ class ToolSearchTool(Tool[ToolSearchInput, ToolSearchOutput]):
|
|
|
115
119
|
def is_concurrency_safe(self) -> bool:
|
|
116
120
|
return True
|
|
117
121
|
|
|
118
|
-
def needs_permissions(
|
|
122
|
+
def needs_permissions(
|
|
123
|
+
self, input_data: Optional[ToolSearchInput] = None
|
|
124
|
+
) -> bool: # noqa: ARG002
|
|
119
125
|
return False
|
|
120
126
|
|
|
121
127
|
async def validate_input(
|
|
@@ -174,6 +180,7 @@ class ToolSearchTool(Tool[ToolSearchInput, ToolSearchOutput]):
|
|
|
174
180
|
regex = re.compile(normalized[1:-1], re.IGNORECASE)
|
|
175
181
|
except re.error:
|
|
176
182
|
regex = None
|
|
183
|
+
logger.exception("[tool_search] Invalid regex search query", extra={"query": query})
|
|
177
184
|
|
|
178
185
|
def _tokenize(text: str) -> List[str]:
|
|
179
186
|
return re.findall(r"[a-z0-9]+", text.lower())
|
|
@@ -186,6 +193,10 @@ class ToolSearchTool(Tool[ToolSearchInput, ToolSearchOutput]):
|
|
|
186
193
|
)
|
|
187
194
|
except Exception:
|
|
188
195
|
description = ""
|
|
196
|
+
logger.exception(
|
|
197
|
+
"[tool_search] Failed to build tool description",
|
|
198
|
+
extra={"tool_name": getattr(tool, "name", None)},
|
|
199
|
+
)
|
|
189
200
|
doc_text = " ".join([name, tool.user_facing_name(), description])
|
|
190
201
|
tokens = _tokenize(doc_text)
|
|
191
202
|
corpus.append((name, tool, tokens, len(tokens), description))
|
|
@@ -271,9 +282,11 @@ class ToolSearchTool(Tool[ToolSearchInput, ToolSearchOutput]):
|
|
|
271
282
|
"name": name,
|
|
272
283
|
"user_facing_name": tool.user_facing_name(),
|
|
273
284
|
"description": description,
|
|
274
|
-
"active":
|
|
275
|
-
|
|
276
|
-
|
|
285
|
+
"active": (
|
|
286
|
+
getattr(registry, "is_active", lambda *_: False)(name)
|
|
287
|
+
if hasattr(registry, "is_active")
|
|
288
|
+
else False
|
|
289
|
+
),
|
|
277
290
|
"deferred": name in getattr(registry, "deferred_names", set()),
|
|
278
291
|
"score": 0.0,
|
|
279
292
|
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"""Lightweight file-change tracking for notifying the model about user edits."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import difflib
|
|
6
|
+
import os
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from typing import Dict, List, Optional
|
|
9
|
+
|
|
10
|
+
from ripperdoc.utils.log import get_logger
|
|
11
|
+
|
|
12
|
+
logger = get_logger()
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass
|
|
16
|
+
class FileSnapshot:
|
|
17
|
+
"""Snapshot of a file read by the agent."""
|
|
18
|
+
|
|
19
|
+
content: str
|
|
20
|
+
timestamp: float
|
|
21
|
+
offset: int = 0
|
|
22
|
+
limit: Optional[int] = None
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass
|
|
26
|
+
class ChangedFileNotice:
|
|
27
|
+
"""Information about a file that changed after it was read."""
|
|
28
|
+
|
|
29
|
+
file_path: str
|
|
30
|
+
summary: str
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def record_snapshot(
|
|
34
|
+
file_path: str,
|
|
35
|
+
content: str,
|
|
36
|
+
cache: Dict[str, FileSnapshot],
|
|
37
|
+
*,
|
|
38
|
+
offset: int = 0,
|
|
39
|
+
limit: Optional[int] = None,
|
|
40
|
+
) -> None:
|
|
41
|
+
"""Store the current contents and mtime for a file."""
|
|
42
|
+
try:
|
|
43
|
+
timestamp = os.path.getmtime(file_path)
|
|
44
|
+
except OSError:
|
|
45
|
+
timestamp = 0.0
|
|
46
|
+
cache[file_path] = FileSnapshot(
|
|
47
|
+
content=content, timestamp=timestamp, offset=offset, limit=limit
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _read_portion(file_path: str, offset: int, limit: Optional[int]) -> str:
|
|
52
|
+
with open(file_path, "r", encoding="utf-8", errors="replace") as handle:
|
|
53
|
+
lines = handle.readlines()
|
|
54
|
+
start = max(offset, 0)
|
|
55
|
+
if limit is None:
|
|
56
|
+
selected = lines[start:]
|
|
57
|
+
else:
|
|
58
|
+
selected = lines[start : start + limit]
|
|
59
|
+
return "".join(selected)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _build_diff_summary(old_content: str, new_content: str, file_path: str, max_lines: int) -> str:
|
|
63
|
+
diff = list(
|
|
64
|
+
difflib.unified_diff(
|
|
65
|
+
old_content.splitlines(),
|
|
66
|
+
new_content.splitlines(),
|
|
67
|
+
fromfile=file_path,
|
|
68
|
+
tofile=file_path,
|
|
69
|
+
lineterm="",
|
|
70
|
+
)
|
|
71
|
+
)
|
|
72
|
+
if not diff:
|
|
73
|
+
return "File was modified but contents appear unchanged."
|
|
74
|
+
|
|
75
|
+
# Keep the diff short to avoid flooding the model.
|
|
76
|
+
if len(diff) > max_lines:
|
|
77
|
+
diff = diff[:max_lines] + ["... (diff truncated)"]
|
|
78
|
+
return "\n".join(diff)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def detect_changed_files(
|
|
82
|
+
cache: Dict[str, FileSnapshot], *, max_diff_lines: int = 80
|
|
83
|
+
) -> List[ChangedFileNotice]:
|
|
84
|
+
"""Return notices for files whose mtime increased since they were read."""
|
|
85
|
+
notices: List[ChangedFileNotice] = []
|
|
86
|
+
|
|
87
|
+
# Iterate over a static list so we can mutate cache safely.
|
|
88
|
+
for file_path, snapshot in list(cache.items()):
|
|
89
|
+
try:
|
|
90
|
+
current_mtime = os.path.getmtime(file_path)
|
|
91
|
+
except OSError:
|
|
92
|
+
notices.append(
|
|
93
|
+
ChangedFileNotice(
|
|
94
|
+
file_path=file_path, summary="File was deleted or is no longer accessible."
|
|
95
|
+
)
|
|
96
|
+
)
|
|
97
|
+
cache.pop(file_path, None)
|
|
98
|
+
continue
|
|
99
|
+
|
|
100
|
+
if current_mtime <= snapshot.timestamp:
|
|
101
|
+
continue
|
|
102
|
+
|
|
103
|
+
try:
|
|
104
|
+
new_content = _read_portion(file_path, snapshot.offset, snapshot.limit)
|
|
105
|
+
except Exception as exc: # pragma: no cover - best-effort telemetry
|
|
106
|
+
logger.exception(
|
|
107
|
+
"[file_watch] Failed reading changed file",
|
|
108
|
+
extra={"file_path": file_path, "error": str(exc)},
|
|
109
|
+
)
|
|
110
|
+
notices.append(
|
|
111
|
+
ChangedFileNotice(
|
|
112
|
+
file_path=file_path,
|
|
113
|
+
summary=f"File changed but could not be read: {exc}",
|
|
114
|
+
)
|
|
115
|
+
)
|
|
116
|
+
# Avoid spamming repeated errors by updating timestamp.
|
|
117
|
+
snapshot.timestamp = current_mtime
|
|
118
|
+
cache[file_path] = snapshot
|
|
119
|
+
continue
|
|
120
|
+
|
|
121
|
+
diff_summary = _build_diff_summary(
|
|
122
|
+
snapshot.content, new_content, file_path, max_lines=max_diff_lines
|
|
123
|
+
)
|
|
124
|
+
notices.append(ChangedFileNotice(file_path=file_path, summary=diff_summary))
|
|
125
|
+
# Update snapshot so we only notify on subsequent changes.
|
|
126
|
+
record_snapshot(
|
|
127
|
+
file_path,
|
|
128
|
+
new_content,
|
|
129
|
+
cache,
|
|
130
|
+
offset=snapshot.offset,
|
|
131
|
+
limit=snapshot.limit,
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
return notices
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
"""Git utilities for Ripperdoc."""
|
|
2
|
+
|
|
3
|
+
import subprocess
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from typing import Dict, List, Optional, Tuple
|
|
6
|
+
import fnmatch
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def is_git_repository(path: Path) -> bool:
|
|
10
|
+
"""Check if a directory is a git repository."""
|
|
11
|
+
try:
|
|
12
|
+
result = subprocess.run(
|
|
13
|
+
["git", "rev-parse", "--is-inside-work-tree"],
|
|
14
|
+
cwd=path,
|
|
15
|
+
capture_output=True,
|
|
16
|
+
text=True,
|
|
17
|
+
timeout=5,
|
|
18
|
+
)
|
|
19
|
+
return result.returncode == 0 and result.stdout.strip() == "true"
|
|
20
|
+
except (subprocess.SubprocessError, FileNotFoundError):
|
|
21
|
+
return False
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def get_git_root(path: Path) -> Optional[Path]:
|
|
25
|
+
"""Get the git root directory for a given path."""
|
|
26
|
+
try:
|
|
27
|
+
result = subprocess.run(
|
|
28
|
+
["git", "rev-parse", "--show-toplevel"],
|
|
29
|
+
cwd=path,
|
|
30
|
+
capture_output=True,
|
|
31
|
+
text=True,
|
|
32
|
+
timeout=5,
|
|
33
|
+
)
|
|
34
|
+
if result.returncode == 0:
|
|
35
|
+
return Path(result.stdout.strip())
|
|
36
|
+
return None
|
|
37
|
+
except (subprocess.SubprocessError, FileNotFoundError):
|
|
38
|
+
return None
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def read_gitignore_patterns(path: Path) -> List[str]:
|
|
42
|
+
"""Read .gitignore patterns from a directory and its parent directories."""
|
|
43
|
+
patterns: List[str] = []
|
|
44
|
+
current = path
|
|
45
|
+
|
|
46
|
+
# Read .gitignore from current directory up to git root
|
|
47
|
+
git_root = get_git_root(path)
|
|
48
|
+
|
|
49
|
+
while current and (git_root is None or current.is_relative_to(git_root)):
|
|
50
|
+
gitignore_file = current / ".gitignore"
|
|
51
|
+
if gitignore_file.exists():
|
|
52
|
+
try:
|
|
53
|
+
with open(gitignore_file, "r", encoding="utf-8") as f:
|
|
54
|
+
for line in f:
|
|
55
|
+
line = line.strip()
|
|
56
|
+
if line and not line.startswith("#"):
|
|
57
|
+
patterns.append(line)
|
|
58
|
+
except (IOError, UnicodeDecodeError):
|
|
59
|
+
pass
|
|
60
|
+
|
|
61
|
+
# Also check for .git/info/exclude
|
|
62
|
+
git_info_exclude = current / ".git" / "info" / "exclude"
|
|
63
|
+
if git_info_exclude.exists():
|
|
64
|
+
try:
|
|
65
|
+
with open(git_info_exclude, "r", encoding="utf-8") as f:
|
|
66
|
+
for line in f:
|
|
67
|
+
line = line.strip()
|
|
68
|
+
if line and not line.startswith("#"):
|
|
69
|
+
patterns.append(line)
|
|
70
|
+
except (IOError, UnicodeDecodeError):
|
|
71
|
+
pass
|
|
72
|
+
|
|
73
|
+
if current.parent == current: # Reached root
|
|
74
|
+
break
|
|
75
|
+
current = current.parent
|
|
76
|
+
|
|
77
|
+
# Add global gitignore patterns
|
|
78
|
+
global_gitignore = Path.home() / ".gitignore"
|
|
79
|
+
if global_gitignore.exists():
|
|
80
|
+
try:
|
|
81
|
+
with open(global_gitignore, "r", encoding="utf-8") as f:
|
|
82
|
+
for line in f:
|
|
83
|
+
line = line.strip()
|
|
84
|
+
if line and not line.startswith("#"):
|
|
85
|
+
patterns.append(line)
|
|
86
|
+
except (IOError, UnicodeDecodeError):
|
|
87
|
+
pass
|
|
88
|
+
|
|
89
|
+
return patterns
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def parse_gitignore_pattern(pattern: str, root_path: Path) -> Tuple[str, Optional[Path]]:
|
|
93
|
+
"""Parse a gitignore pattern and return (relative_pattern, root)."""
|
|
94
|
+
pattern = pattern.strip()
|
|
95
|
+
|
|
96
|
+
# Handle absolute paths
|
|
97
|
+
if pattern.startswith("/"):
|
|
98
|
+
return pattern[1:], root_path
|
|
99
|
+
|
|
100
|
+
# Handle patterns relative to home directory
|
|
101
|
+
if pattern.startswith("~/"):
|
|
102
|
+
home_pattern = pattern[2:]
|
|
103
|
+
return home_pattern, Path.home()
|
|
104
|
+
|
|
105
|
+
# Handle patterns with leading slash (relative to repository root)
|
|
106
|
+
if pattern.startswith("/"):
|
|
107
|
+
return pattern[1:], root_path
|
|
108
|
+
|
|
109
|
+
# Default: pattern is relative to the directory containing .gitignore
|
|
110
|
+
return pattern, None
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def build_ignore_patterns_map(
|
|
114
|
+
root_path: Path,
|
|
115
|
+
user_ignore_patterns: Optional[List[str]] = None,
|
|
116
|
+
include_gitignore: bool = True,
|
|
117
|
+
) -> Dict[Optional[Path], List[str]]:
|
|
118
|
+
"""Build a map of ignore patterns by root directory."""
|
|
119
|
+
ignore_map: Dict[Optional[Path], List[str]] = {}
|
|
120
|
+
|
|
121
|
+
# Add user-provided ignore patterns
|
|
122
|
+
if user_ignore_patterns:
|
|
123
|
+
for pattern in user_ignore_patterns:
|
|
124
|
+
relative_pattern, pattern_root = parse_gitignore_pattern(pattern, root_path)
|
|
125
|
+
if pattern_root not in ignore_map:
|
|
126
|
+
ignore_map[pattern_root] = []
|
|
127
|
+
ignore_map[pattern_root].append(relative_pattern)
|
|
128
|
+
|
|
129
|
+
# Add .gitignore patterns
|
|
130
|
+
if include_gitignore and is_git_repository(root_path):
|
|
131
|
+
gitignore_patterns = read_gitignore_patterns(root_path)
|
|
132
|
+
for pattern in gitignore_patterns:
|
|
133
|
+
relative_pattern, pattern_root = parse_gitignore_pattern(pattern, root_path)
|
|
134
|
+
if pattern_root not in ignore_map:
|
|
135
|
+
ignore_map[pattern_root] = []
|
|
136
|
+
ignore_map[pattern_root].append(relative_pattern)
|
|
137
|
+
|
|
138
|
+
return ignore_map
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def should_ignore_path(
|
|
142
|
+
path: Path, root_path: Path, ignore_map: Dict[Optional[Path], List[str]]
|
|
143
|
+
) -> bool:
|
|
144
|
+
"""Check if a path should be ignored based on ignore patterns."""
|
|
145
|
+
# Check against each root in the ignore map
|
|
146
|
+
for pattern_root, patterns in ignore_map.items():
|
|
147
|
+
# Determine the actual root to use for pattern matching
|
|
148
|
+
actual_root = pattern_root if pattern_root is not None else root_path
|
|
149
|
+
|
|
150
|
+
try:
|
|
151
|
+
# Get relative path from actual_root
|
|
152
|
+
rel_path = path.relative_to(actual_root).as_posix()
|
|
153
|
+
except ValueError:
|
|
154
|
+
# Path is not under this root, skip
|
|
155
|
+
continue
|
|
156
|
+
|
|
157
|
+
# For directories, also check with trailing slash
|
|
158
|
+
rel_path_dir = f"{rel_path}/" if path.is_dir() else rel_path
|
|
159
|
+
|
|
160
|
+
# Check each pattern
|
|
161
|
+
for pattern in patterns:
|
|
162
|
+
# Handle directory-specific patterns
|
|
163
|
+
if pattern.endswith("/"):
|
|
164
|
+
if not path.is_dir():
|
|
165
|
+
continue
|
|
166
|
+
pattern_without_slash = pattern[:-1]
|
|
167
|
+
if fnmatch.fnmatch(rel_path, pattern_without_slash) or fnmatch.fnmatch(
|
|
168
|
+
rel_path_dir, pattern
|
|
169
|
+
):
|
|
170
|
+
return True
|
|
171
|
+
else:
|
|
172
|
+
if fnmatch.fnmatch(rel_path, pattern) or fnmatch.fnmatch(rel_path_dir, pattern):
|
|
173
|
+
return True
|
|
174
|
+
|
|
175
|
+
return False
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def get_git_status_files(root_path: Path) -> Tuple[List[str], List[str]]:
|
|
179
|
+
"""Get tracked and untracked files from git status."""
|
|
180
|
+
tracked: List[str] = []
|
|
181
|
+
untracked: List[str] = []
|
|
182
|
+
|
|
183
|
+
if not is_git_repository(root_path):
|
|
184
|
+
return tracked, untracked
|
|
185
|
+
|
|
186
|
+
try:
|
|
187
|
+
# Get tracked files (modified, added, etc.)
|
|
188
|
+
result = subprocess.run(
|
|
189
|
+
["git", "status", "--porcelain"],
|
|
190
|
+
cwd=root_path,
|
|
191
|
+
capture_output=True,
|
|
192
|
+
text=True,
|
|
193
|
+
timeout=10,
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
if result.returncode == 0:
|
|
197
|
+
for line in result.stdout.strip().split("\n"):
|
|
198
|
+
if line:
|
|
199
|
+
status = line[:2].strip()
|
|
200
|
+
file_path = line[3:].strip()
|
|
201
|
+
|
|
202
|
+
# Remove quotes if present
|
|
203
|
+
if file_path.startswith('"') and file_path.endswith('"'):
|
|
204
|
+
file_path = file_path[1:-1]
|
|
205
|
+
|
|
206
|
+
if status == "??": # Untracked
|
|
207
|
+
untracked.append(file_path)
|
|
208
|
+
else: # Tracked (modified, added, etc.)
|
|
209
|
+
tracked.append(file_path)
|
|
210
|
+
|
|
211
|
+
except (subprocess.SubprocessError, FileNotFoundError):
|
|
212
|
+
pass
|
|
213
|
+
|
|
214
|
+
return tracked, untracked
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def get_current_git_branch(root_path: Path) -> Optional[str]:
|
|
218
|
+
"""Get the current git branch name."""
|
|
219
|
+
if not is_git_repository(root_path):
|
|
220
|
+
return None
|
|
221
|
+
|
|
222
|
+
try:
|
|
223
|
+
result = subprocess.run(
|
|
224
|
+
["git", "branch", "--show-current"],
|
|
225
|
+
cwd=root_path,
|
|
226
|
+
capture_output=True,
|
|
227
|
+
text=True,
|
|
228
|
+
timeout=5,
|
|
229
|
+
)
|
|
230
|
+
if result.returncode == 0:
|
|
231
|
+
return result.stdout.strip()
|
|
232
|
+
except (subprocess.SubprocessError, FileNotFoundError):
|
|
233
|
+
pass
|
|
234
|
+
|
|
235
|
+
return None
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def get_git_commit_hash(root_path: Path) -> Optional[str]:
|
|
239
|
+
"""Get the current git commit hash."""
|
|
240
|
+
if not is_git_repository(root_path):
|
|
241
|
+
return None
|
|
242
|
+
|
|
243
|
+
try:
|
|
244
|
+
result = subprocess.run(
|
|
245
|
+
["git", "rev-parse", "HEAD"],
|
|
246
|
+
cwd=root_path,
|
|
247
|
+
capture_output=True,
|
|
248
|
+
text=True,
|
|
249
|
+
timeout=5,
|
|
250
|
+
)
|
|
251
|
+
if result.returncode == 0:
|
|
252
|
+
return result.stdout.strip()[:8] # Short hash
|
|
253
|
+
except (subprocess.SubprocessError, FileNotFoundError):
|
|
254
|
+
pass
|
|
255
|
+
|
|
256
|
+
return None
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def is_working_directory_clean(root_path: Path) -> bool:
|
|
260
|
+
"""Check if the working directory is clean (no uncommitted changes)."""
|
|
261
|
+
if not is_git_repository(root_path):
|
|
262
|
+
return True
|
|
263
|
+
|
|
264
|
+
try:
|
|
265
|
+
result = subprocess.run(
|
|
266
|
+
["git", "status", "--porcelain"],
|
|
267
|
+
cwd=root_path,
|
|
268
|
+
capture_output=True,
|
|
269
|
+
text=True,
|
|
270
|
+
timeout=5,
|
|
271
|
+
)
|
|
272
|
+
return result.returncode == 0 and not result.stdout.strip()
|
|
273
|
+
except (subprocess.SubprocessError, FileNotFoundError):
|
|
274
|
+
return True
|