Repository navigation
Expand file tree
/
Copy pathgit_tools.py
More file actions
200 lines (183 loc) · 6.72 KB
/
Copy pathgit_tools.py
File metadata and controls
200 lines (183 loc) · 6.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"""Git tools for evolve publish path."""
from __future__ import annotations
from typing import Any
from ai_agent.domain.ports import ApprovalGate, GitPort
from ai_agent.domain.tool import BaseTool, ToolParameter, ToolResult
from ai_agent.harness.touch_tracker import TouchTracker
class GitStatusTool(BaseTool):
def __init__(self, git: GitPort) -> None:
super().__init__(
name="git_status",
description="Show short git status.",
parameters=[],
)
self._git = git
async def execute(self, arguments: dict[str, Any]) -> ToolResult:
return ToolResult(tool_name=self.name, success=True, output=self._git.status() or "(clean)")
class GitDiffTool(BaseTool):
def __init__(self, git: GitPort, tracker: TouchTracker | None = None) -> None:
super().__init__(
name="git_diff",
description=(
"Show git diff. Defaults to files touched in this session when available."
),
parameters=[
ToolParameter(
name="staged",
type="boolean",
description="If true, show staged diff",
required=False,
),
ToolParameter(
name="paths",
type="array",
description="Optional paths to limit the diff",
required=False,
),
],
)
self._git = git
self._tracker = tracker
async def execute(self, arguments: dict[str, Any]) -> ToolResult:
staged = bool(arguments.get("staged", False))
paths: list[str] | None = _coerce_paths(arguments.get("paths")) or None
if paths is None and self._tracker is not None:
tracked = self._tracker.paths()
paths = tracked or None
try:
output = self._git.diff(staged=staged, paths=paths) # type: ignore[call-arg]
except TypeError:
output = self._git.diff(staged=staged)
return ToolResult(
tool_name=self.name,
success=True,
output=output or "(empty diff)",
)
class GitCommitTool(BaseTool):
def __init__(
self,
git: GitPort,
gate: ApprovalGate,
tracker: TouchTracker | None = None,
) -> None:
super().__init__(
name="git_commit",
description=(
"Create/switch to a feature branch and commit ONLY files touched "
"by this session (or explicit paths). Never git add -A. "
"Requires human approval. Never commits on main/master. "
"Call alone in a round — never with open_pull_request."
),
parameters=[
ToolParameter(
name="branch",
type="string",
description="Branch name (e.g. agent/evolve-xyz)",
required=True,
),
ToolParameter(
name="message",
type="string",
description="Commit message",
required=True,
),
ToolParameter(
name="paths",
type="array",
description=(
"Optional explicit relative paths to commit. "
"Defaults to files touched via replace_in_file/write_file/apply_patch."
),
required=False,
),
],
parallel_safe=False,
)
self._git = git
self._gate = gate
self._tracker = tracker
async def execute(self, arguments: dict[str, Any]) -> ToolResult:
branch = str(arguments.get("branch") or "").strip()
message = str(arguments.get("message") or "").strip()
if not branch or not message:
return ToolResult(
tool_name=self.name,
success=False,
output="",
error="branch and message required",
)
if branch in {"main", "master"}:
return ToolResult(
tool_name=self.name,
success=False,
output="",
error="refusing to commit on protected branch name",
)
paths = _coerce_paths(arguments.get("paths"))
if not paths and self._tracker is not None:
paths = self._tracker.paths()
if not paths:
return ToolResult(
tool_name=self.name,
success=False,
output="",
error=(
"no scoped paths to commit. Edit with replace_in_file/write_file/"
"apply_patch first, or pass paths explicitly."
),
)
# Show only scoped status in the approval prompt.
try:
scoped_diff = self._git.diff(paths=paths) # type: ignore[call-arg]
except TypeError:
scoped_diff = self._git.diff()
if not (scoped_diff or "").strip():
# Still allow if untracked new files exist in paths — status check later
pass
approved = await self._gate.request(
"Create branch {branch!r} and commit ONLY these paths:\n"
"{paths}\n\n"
"Message: {message}\n\n"
"Scoped diff:\n{diff}".format(
branch=branch,
paths="\n".join(f"- {p}" for p in paths),
message=message,
diff=(scoped_diff or "(no unstaged diff — may be new/untracked files)"),
)
)
if not approved:
return ToolResult(
tool_name=self.name,
success=False,
output="",
error="denied by user",
)
try:
self._git.create_branch(branch)
sha = self._git.commit(message, paths=paths)
except Exception as exc: # noqa: BLE001
return ToolResult(
tool_name=self.name,
success=False,
output="",
error=str(exc),
)
return ToolResult(
tool_name=self.name,
success=True,
output=f"branch={branch} sha={sha} paths={','.join(paths)}",
)
def _coerce_paths(raw: object) -> list[str]:
if raw is None:
return []
if isinstance(raw, str):
cleaned = raw.strip()
return [cleaned] if cleaned else []
if isinstance(raw, list):
out: list[str] = []
for item in raw:
text = str(item).strip()
if text:
out.append(text)
return out
return []