mycode-coding-agent 0.1.0__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.
- mycode/__init__.py +0 -0
- mycode/adapters/__init__.py +21 -0
- mycode/adapters/jsonl.py +692 -0
- mycode/agent/__init__.py +25 -0
- mycode/agent/events.py +111 -0
- mycode/agent/outcome.py +103 -0
- mycode/agent/progress.py +373 -0
- mycode/agent/runner.py +1481 -0
- mycode/application/__init__.py +38 -0
- mycode/application/agent_session.py +367 -0
- mycode/application/events.py +59 -0
- mycode/application/runtime.py +211 -0
- mycode/application/sessions.py +180 -0
- mycode/cli.py +840 -0
- mycode/config.py +355 -0
- mycode/context/__init__.py +1 -0
- mycode/context/artifacts.py +672 -0
- mycode/context/budget.py +752 -0
- mycode/context/builder.py +112 -0
- mycode/context/compact.py +795 -0
- mycode/context/tool_result_format.py +199 -0
- mycode/context/tool_result_retention.py +261 -0
- mycode/conversation.py +78 -0
- mycode/error_handling.py +481 -0
- mycode/event_format.py +147 -0
- mycode/instructions.py +285 -0
- mycode/llm.py +771 -0
- mycode/mcp/__init__.py +41 -0
- mycode/mcp/client.py +44 -0
- mycode/mcp/config.py +207 -0
- mycode/mcp/errors.py +302 -0
- mycode/mcp/manager.py +339 -0
- mycode/mcp/models.py +20 -0
- mycode/mcp/result_adapter.py +58 -0
- mycode/mcp/tool_adapter.py +145 -0
- mycode/mcp/trust.py +313 -0
- mycode/memory.py +570 -0
- mycode/memory_context.py +245 -0
- mycode/messages.py +63 -0
- mycode/observability.py +28 -0
- mycode/permissions.py +262 -0
- mycode/persistence/__init__.py +1 -0
- mycode/persistence/filesystem.py +291 -0
- mycode/persistence/project_storage.py +208 -0
- mycode/persistence/session_lock.py +138 -0
- mycode/persistence/session_store.py +503 -0
- mycode/presentation/__init__.py +1 -0
- mycode/presentation/cli/__init__.py +14 -0
- mycode/presentation/cli/confirmer.py +116 -0
- mycode/presentation/cli/mcp_trust.py +61 -0
- mycode/presentation/cli/presenter.py +320 -0
- mycode/presentation/cli/session_menu.py +146 -0
- mycode/presentation/cli/subagent_observer.py +124 -0
- mycode/presentation/command_format.py +90 -0
- mycode/presentation/commands.py +95 -0
- mycode/presentation/tui/__init__.py +6 -0
- mycode/presentation/tui/app.py +1351 -0
- mycode/presentation/tui/interactions.py +253 -0
- mycode/presentation/tui/presenter.py +266 -0
- mycode/presentation/tui/screens.py +305 -0
- mycode/presentation/tui/widgets.py +214 -0
- mycode/project.py +22 -0
- mycode/prompts.py +181 -0
- mycode/reasoning.py +40 -0
- mycode/session.py +86 -0
- mycode/skills/__init__.py +27 -0
- mycode/skills/builtin/database-recovery/SKILL.md +138 -0
- mycode/skills/builtin/database-recovery/references/sqlite.md +235 -0
- mycode/skills/registry.py +295 -0
- mycode/skills/state.py +68 -0
- mycode/subagents/__init__.py +1 -0
- mycode/subagents/audit.py +212 -0
- mycode/subagents/concurrency.py +124 -0
- mycode/subagents/contracts.py +421 -0
- mycode/subagents/delegate.py +80 -0
- mycode/subagents/delegation.py +128 -0
- mycode/subagents/lifecycle.py +86 -0
- mycode/subagents/limits.py +7 -0
- mycode/subagents/observability.py +150 -0
- mycode/subagents/persistence.py +152 -0
- mycode/subagents/profiles.py +184 -0
- mycode/subagents/prompts.py +67 -0
- mycode/subagents/results.py +178 -0
- mycode/subagents/runtime.py +528 -0
- mycode/subagents/snapshots.py +211 -0
- mycode/subagents/tool_batch.py +260 -0
- mycode/tools/__init__.py +81 -0
- mycode/tools/base.py +222 -0
- mycode/tools/bounds.py +14 -0
- mycode/tools/command_executor.py +167 -0
- mycode/tools/command_output.py +166 -0
- mycode/tools/command_risk.py +596 -0
- mycode/tools/defaults.py +59 -0
- mycode/tools/edit_file.py +524 -0
- mycode/tools/file_mutation.py +30 -0
- mycode/tools/glob.py +247 -0
- mycode/tools/grep.py +324 -0
- mycode/tools/ignore.py +122 -0
- mycode/tools/inspect_changes.py +269 -0
- mycode/tools/load_skill.py +92 -0
- mycode/tools/memory.py +264 -0
- mycode/tools/path_permissions.py +78 -0
- mycode/tools/patterns.py +48 -0
- mycode/tools/permission_metadata.py +27 -0
- mycode/tools/process_tree.py +166 -0
- mycode/tools/read_file.py +242 -0
- mycode/tools/read_skill_resource.py +93 -0
- mycode/tools/registry.py +279 -0
- mycode/tools/run_command.py +237 -0
- mycode/tools/run_skill_script.py +206 -0
- mycode/tools/run_validation.py +107 -0
- mycode/tools/submit_result.py +93 -0
- mycode/tools/text.py +15 -0
- mycode/tools/validation_command.py +377 -0
- mycode/tools/workspace.py +33 -0
- mycode/tools/write_file.py +169 -0
- mycode_coding_agent-0.1.0.dist-info/METADATA +244 -0
- mycode_coding_agent-0.1.0.dist-info/RECORD +121 -0
- mycode_coding_agent-0.1.0.dist-info/WHEEL +4 -0
- mycode_coding_agent-0.1.0.dist-info/entry_points.txt +2 -0
- mycode_coding_agent-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,524 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
from pydantic import Field
|
|
4
|
+
|
|
5
|
+
from mycode.permissions import PermissionChecker, PermissionDecision, PermissionRequest
|
|
6
|
+
from mycode.tools.base import PydanticTool, ToolArgs, ToolResult
|
|
7
|
+
from mycode.tools.file_mutation import (
|
|
8
|
+
display_path,
|
|
9
|
+
failure_metadata,
|
|
10
|
+
resolved_path_from_decision,
|
|
11
|
+
with_permission_metadata,
|
|
12
|
+
)
|
|
13
|
+
from mycode.tools.path_permissions import PathPermissionPolicy
|
|
14
|
+
from mycode.tools.text import contains_nul_byte, decode_text
|
|
15
|
+
from mycode.tools.workspace import Workspace
|
|
16
|
+
|
|
17
|
+
SNIPPET_CONTEXT_LINES = 3
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class _ReplacementMatch:
|
|
21
|
+
def __init__(
|
|
22
|
+
self,
|
|
23
|
+
*,
|
|
24
|
+
exact_match_count: int,
|
|
25
|
+
normalized_match_count: int | None = None,
|
|
26
|
+
start: int | None = None,
|
|
27
|
+
end: int | None = None,
|
|
28
|
+
old_text: str | None = None,
|
|
29
|
+
new_text: str | None = None,
|
|
30
|
+
newline_normalized: bool = False,
|
|
31
|
+
) -> None:
|
|
32
|
+
self.exact_match_count = exact_match_count
|
|
33
|
+
self.normalized_match_count = normalized_match_count
|
|
34
|
+
self.start = start
|
|
35
|
+
self.end = end
|
|
36
|
+
self.old_text = old_text
|
|
37
|
+
self.new_text = new_text
|
|
38
|
+
self.newline_normalized = newline_normalized
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def match_count(self) -> int:
|
|
42
|
+
if self.start is not None:
|
|
43
|
+
return 1
|
|
44
|
+
if self.exact_match_count > 0:
|
|
45
|
+
return self.exact_match_count
|
|
46
|
+
if self.normalized_match_count is not None:
|
|
47
|
+
return self.normalized_match_count
|
|
48
|
+
return 0
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class EditFileArgs(ToolArgs):
|
|
52
|
+
path: str = Field(min_length=1)
|
|
53
|
+
old_text: str = Field(
|
|
54
|
+
min_length=1,
|
|
55
|
+
description="要替换的原始文本,必须在文件中唯一匹配一次。",
|
|
56
|
+
)
|
|
57
|
+
new_text: str = Field(
|
|
58
|
+
description="用于替换 old_text 的新文本;可以为空字符串以删除匹配内容。",
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class EditFileTool(PydanticTool[EditFileArgs]):
|
|
63
|
+
name = "edit_file"
|
|
64
|
+
description = "Replace one exact text occurrence in an existing text file."
|
|
65
|
+
args_model = EditFileArgs
|
|
66
|
+
capability = "write"
|
|
67
|
+
risk = "medium"
|
|
68
|
+
|
|
69
|
+
def __init__(self, workspace: Workspace) -> None:
|
|
70
|
+
self.workspace = workspace
|
|
71
|
+
|
|
72
|
+
def build_permission_request(self, args: EditFileArgs) -> PermissionRequest:
|
|
73
|
+
return PermissionRequest(
|
|
74
|
+
tool_name=self.name,
|
|
75
|
+
capability=self.capability,
|
|
76
|
+
action=self.name,
|
|
77
|
+
target=args.path,
|
|
78
|
+
arguments=args.model_dump(),
|
|
79
|
+
description="Replace one exact text occurrence in a file.",
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
def check_permission(
|
|
83
|
+
self,
|
|
84
|
+
args: EditFileArgs,
|
|
85
|
+
permission_checker: PermissionChecker,
|
|
86
|
+
) -> tuple[PermissionRequest, PermissionDecision]:
|
|
87
|
+
request = self.build_permission_request(args)
|
|
88
|
+
path_decision = PathPermissionPolicy(self.workspace).check_path(
|
|
89
|
+
request,
|
|
90
|
+
args.path,
|
|
91
|
+
)
|
|
92
|
+
metadata = _edit_metadata(
|
|
93
|
+
path=resolved_path_from_decision(path_decision),
|
|
94
|
+
args=args,
|
|
95
|
+
path_decision=path_decision,
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
if not metadata["target_exists"]:
|
|
99
|
+
return request, PermissionDecision.deny(
|
|
100
|
+
reason="unsupported_operation",
|
|
101
|
+
message=f"File not found: {args.path}",
|
|
102
|
+
metadata=metadata,
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
if not metadata["target_is_file"]:
|
|
106
|
+
return request, PermissionDecision.deny(
|
|
107
|
+
reason="unsupported_operation",
|
|
108
|
+
message=f"Path is not a file: {args.path}",
|
|
109
|
+
metadata=metadata,
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
if path_decision.status != "allow":
|
|
113
|
+
return request, PermissionDecision.ask(
|
|
114
|
+
reason=path_decision.reason,
|
|
115
|
+
message=path_decision.message,
|
|
116
|
+
metadata=metadata,
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
decision = permission_checker.check(
|
|
120
|
+
request,
|
|
121
|
+
self.get_permission_profile(),
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
return request, with_permission_metadata(decision, metadata)
|
|
125
|
+
|
|
126
|
+
def run_authorized(
|
|
127
|
+
self,
|
|
128
|
+
args: EditFileArgs,
|
|
129
|
+
decision: PermissionDecision,
|
|
130
|
+
) -> ToolResult:
|
|
131
|
+
try:
|
|
132
|
+
return self._edit_path(
|
|
133
|
+
args,
|
|
134
|
+
resolved_path_from_decision(decision),
|
|
135
|
+
decision,
|
|
136
|
+
)
|
|
137
|
+
except Exception as error:
|
|
138
|
+
return ToolResult.failure(
|
|
139
|
+
error=f"Tool execution failed: {error}",
|
|
140
|
+
metadata=failure_metadata(
|
|
141
|
+
decision,
|
|
142
|
+
{"exception_type": type(error).__name__},
|
|
143
|
+
),
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
def _run(self, args: EditFileArgs) -> ToolResult:
|
|
147
|
+
return ToolResult.failure(
|
|
148
|
+
error="edit_file must be run through ToolRegistry.run_tool().",
|
|
149
|
+
metadata={"path": args.path, "reason": "permission_required"},
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
def _edit_path(
|
|
153
|
+
self,
|
|
154
|
+
args: EditFileArgs,
|
|
155
|
+
path: Path,
|
|
156
|
+
decision: PermissionDecision,
|
|
157
|
+
) -> ToolResult:
|
|
158
|
+
if not path.exists():
|
|
159
|
+
return ToolResult.failure(
|
|
160
|
+
error=f"File not found: {args.path}",
|
|
161
|
+
metadata=failure_metadata(
|
|
162
|
+
decision,
|
|
163
|
+
{
|
|
164
|
+
"path": args.path,
|
|
165
|
+
"target_exists": False,
|
|
166
|
+
},
|
|
167
|
+
),
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
if not path.is_file():
|
|
171
|
+
return ToolResult.failure(
|
|
172
|
+
error=f"Path is not a file: {args.path}",
|
|
173
|
+
metadata=failure_metadata(
|
|
174
|
+
decision,
|
|
175
|
+
{
|
|
176
|
+
"path": args.path,
|
|
177
|
+
"target_is_file": False,
|
|
178
|
+
},
|
|
179
|
+
),
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
raw_content = path.read_bytes()
|
|
183
|
+
if contains_nul_byte(raw_content):
|
|
184
|
+
return ToolResult.failure(
|
|
185
|
+
error=f"File is not a supported text file: {args.path}",
|
|
186
|
+
metadata=failure_metadata(
|
|
187
|
+
decision,
|
|
188
|
+
{"path": args.path, "reason": "nul_byte"},
|
|
189
|
+
),
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
decoded = decode_text(raw_content)
|
|
193
|
+
if decoded is None:
|
|
194
|
+
return ToolResult.failure(
|
|
195
|
+
error=f"File is not a supported text file: {args.path}",
|
|
196
|
+
metadata=failure_metadata(
|
|
197
|
+
decision,
|
|
198
|
+
{"path": args.path, "supported_encodings": ["utf-8", "gbk"]},
|
|
199
|
+
),
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
text, encoding = decoded
|
|
203
|
+
replacement = _find_replacement_match(text, args.old_text, args.new_text)
|
|
204
|
+
if replacement.match_count == 0:
|
|
205
|
+
return ToolResult.failure(
|
|
206
|
+
error="Target text not found.",
|
|
207
|
+
metadata=failure_metadata(
|
|
208
|
+
decision,
|
|
209
|
+
_result_metadata(
|
|
210
|
+
args,
|
|
211
|
+
path,
|
|
212
|
+
self.workspace.root,
|
|
213
|
+
text,
|
|
214
|
+
encoding,
|
|
215
|
+
replacement,
|
|
216
|
+
),
|
|
217
|
+
),
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
if replacement.match_count > 1:
|
|
221
|
+
return ToolResult.failure(
|
|
222
|
+
error="Target text is not unique.",
|
|
223
|
+
metadata=failure_metadata(
|
|
224
|
+
decision,
|
|
225
|
+
_result_metadata(
|
|
226
|
+
args,
|
|
227
|
+
path,
|
|
228
|
+
self.workspace.root,
|
|
229
|
+
text,
|
|
230
|
+
encoding,
|
|
231
|
+
replacement,
|
|
232
|
+
),
|
|
233
|
+
),
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
replacement_start = replacement.start
|
|
237
|
+
if (
|
|
238
|
+
replacement_start is None
|
|
239
|
+
or replacement.end is None
|
|
240
|
+
or replacement.new_text is None
|
|
241
|
+
):
|
|
242
|
+
return ToolResult.failure(
|
|
243
|
+
error="Target text not found.",
|
|
244
|
+
metadata=failure_metadata(
|
|
245
|
+
decision,
|
|
246
|
+
_result_metadata(
|
|
247
|
+
args,
|
|
248
|
+
path,
|
|
249
|
+
self.workspace.root,
|
|
250
|
+
text,
|
|
251
|
+
encoding,
|
|
252
|
+
replacement,
|
|
253
|
+
),
|
|
254
|
+
),
|
|
255
|
+
)
|
|
256
|
+
|
|
257
|
+
edited_text = (
|
|
258
|
+
text[:replacement_start] + replacement.new_text + text[replacement.end :]
|
|
259
|
+
)
|
|
260
|
+
try:
|
|
261
|
+
path.write_bytes(edited_text.encode(encoding))
|
|
262
|
+
except UnicodeEncodeError:
|
|
263
|
+
return ToolResult.failure(
|
|
264
|
+
error=f"Edited content cannot be encoded as {encoding}.",
|
|
265
|
+
metadata=failure_metadata(
|
|
266
|
+
decision,
|
|
267
|
+
_result_metadata(
|
|
268
|
+
args,
|
|
269
|
+
path,
|
|
270
|
+
self.workspace.root,
|
|
271
|
+
text,
|
|
272
|
+
encoding,
|
|
273
|
+
replacement,
|
|
274
|
+
),
|
|
275
|
+
),
|
|
276
|
+
)
|
|
277
|
+
|
|
278
|
+
snippet = _build_edit_snippet(
|
|
279
|
+
edited_text=edited_text,
|
|
280
|
+
replacement_start=replacement_start,
|
|
281
|
+
replacement_chars=len(args.new_text),
|
|
282
|
+
context_lines=SNIPPET_CONTEXT_LINES,
|
|
283
|
+
)
|
|
284
|
+
result_metadata = {
|
|
285
|
+
**decision.metadata,
|
|
286
|
+
**_result_metadata(
|
|
287
|
+
args,
|
|
288
|
+
path,
|
|
289
|
+
self.workspace.root,
|
|
290
|
+
text,
|
|
291
|
+
encoding,
|
|
292
|
+
replacement,
|
|
293
|
+
),
|
|
294
|
+
"edited_chars": len(edited_text),
|
|
295
|
+
"edited_bytes": len(edited_text.encode(encoding)),
|
|
296
|
+
"permission_status": decision.status,
|
|
297
|
+
"line_start": snippet.line_start,
|
|
298
|
+
"line_end": snippet.line_end,
|
|
299
|
+
"snippet_truncated": snippet.truncated,
|
|
300
|
+
}
|
|
301
|
+
if _should_return_snippet(decision):
|
|
302
|
+
result_metadata["snippet"] = snippet.content
|
|
303
|
+
result_content = (
|
|
304
|
+
f"Edited file: {display_path(path, self.workspace.root)}\n\n"
|
|
305
|
+
f"Changed context (lines {snippet.line_start}-{snippet.line_end}):\n"
|
|
306
|
+
f"{snippet.content}"
|
|
307
|
+
)
|
|
308
|
+
else:
|
|
309
|
+
result_metadata["snippet_suppressed"] = True
|
|
310
|
+
result_content = (
|
|
311
|
+
f"Edited file: {display_path(path, self.workspace.root)}\n"
|
|
312
|
+
"Changed context suppressed for sensitive path."
|
|
313
|
+
)
|
|
314
|
+
|
|
315
|
+
return ToolResult.success(
|
|
316
|
+
content=result_content,
|
|
317
|
+
metadata=result_metadata,
|
|
318
|
+
)
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
def _edit_metadata(
|
|
322
|
+
*,
|
|
323
|
+
path: Path,
|
|
324
|
+
args: EditFileArgs,
|
|
325
|
+
path_decision: PermissionDecision,
|
|
326
|
+
) -> dict[str, object]:
|
|
327
|
+
target_exists = path.exists()
|
|
328
|
+
return {
|
|
329
|
+
**path_decision.metadata,
|
|
330
|
+
"operation": "replace",
|
|
331
|
+
"target_exists": target_exists,
|
|
332
|
+
"target_is_file": path.is_file() if target_exists else None,
|
|
333
|
+
"old_text_chars": len(args.old_text),
|
|
334
|
+
"new_text_chars": len(args.new_text),
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
def _result_metadata(
|
|
339
|
+
args: EditFileArgs,
|
|
340
|
+
path: Path,
|
|
341
|
+
root: Path,
|
|
342
|
+
text: str,
|
|
343
|
+
encoding: str,
|
|
344
|
+
replacement: _ReplacementMatch,
|
|
345
|
+
) -> dict[str, object]:
|
|
346
|
+
metadata: dict[str, object] = {
|
|
347
|
+
"path": display_path(path, root),
|
|
348
|
+
"encoding": encoding,
|
|
349
|
+
"operation": "replace",
|
|
350
|
+
"match_count": replacement.match_count,
|
|
351
|
+
"old_text_chars": len(args.old_text),
|
|
352
|
+
"new_text_chars": len(args.new_text),
|
|
353
|
+
"original_chars": len(text),
|
|
354
|
+
"original_bytes": len(text.encode(encoding)),
|
|
355
|
+
}
|
|
356
|
+
if replacement.normalized_match_count is not None:
|
|
357
|
+
metadata.update(
|
|
358
|
+
{
|
|
359
|
+
"exact_match_count": replacement.exact_match_count,
|
|
360
|
+
"newline_normalized_match_count": replacement.normalized_match_count,
|
|
361
|
+
"newline_normalized_match": replacement.newline_normalized,
|
|
362
|
+
}
|
|
363
|
+
)
|
|
364
|
+
|
|
365
|
+
return metadata
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
def _find_replacement_match(
|
|
369
|
+
text: str,
|
|
370
|
+
old_text: str,
|
|
371
|
+
new_text: str,
|
|
372
|
+
) -> _ReplacementMatch:
|
|
373
|
+
exact_match_count = text.count(old_text)
|
|
374
|
+
if exact_match_count == 1:
|
|
375
|
+
start = text.index(old_text)
|
|
376
|
+
return _ReplacementMatch(
|
|
377
|
+
exact_match_count=exact_match_count,
|
|
378
|
+
start=start,
|
|
379
|
+
end=start + len(old_text),
|
|
380
|
+
old_text=old_text,
|
|
381
|
+
new_text=new_text,
|
|
382
|
+
)
|
|
383
|
+
if exact_match_count > 1:
|
|
384
|
+
return _ReplacementMatch(exact_match_count=exact_match_count)
|
|
385
|
+
|
|
386
|
+
normalized_text, normalized_to_original = _normalize_newlines_with_mapping(text)
|
|
387
|
+
normalized_old_text = _normalize_newlines(old_text)
|
|
388
|
+
normalized_match_count = normalized_text.count(normalized_old_text)
|
|
389
|
+
if normalized_match_count != 1:
|
|
390
|
+
return _ReplacementMatch(
|
|
391
|
+
exact_match_count=exact_match_count,
|
|
392
|
+
normalized_match_count=normalized_match_count,
|
|
393
|
+
)
|
|
394
|
+
|
|
395
|
+
normalized_start = normalized_text.index(normalized_old_text)
|
|
396
|
+
normalized_end = normalized_start + len(normalized_old_text)
|
|
397
|
+
original_start = normalized_to_original[normalized_start]
|
|
398
|
+
original_end = (
|
|
399
|
+
normalized_to_original[normalized_end]
|
|
400
|
+
if normalized_end < len(normalized_to_original)
|
|
401
|
+
else len(text)
|
|
402
|
+
)
|
|
403
|
+
original_old_text = text[original_start:original_end]
|
|
404
|
+
adapted_new_text = _adapt_replacement_newlines(
|
|
405
|
+
new_text,
|
|
406
|
+
original_old_text=original_old_text,
|
|
407
|
+
)
|
|
408
|
+
return _ReplacementMatch(
|
|
409
|
+
exact_match_count=exact_match_count,
|
|
410
|
+
normalized_match_count=normalized_match_count,
|
|
411
|
+
start=original_start,
|
|
412
|
+
end=original_end,
|
|
413
|
+
old_text=original_old_text,
|
|
414
|
+
new_text=adapted_new_text,
|
|
415
|
+
newline_normalized=True,
|
|
416
|
+
)
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
def _normalize_newlines(text: str) -> str:
|
|
420
|
+
return text.replace("\r\n", "\n").replace("\r", "\n")
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
def _normalize_newlines_with_mapping(text: str) -> tuple[str, list[int]]:
|
|
424
|
+
normalized: list[str] = []
|
|
425
|
+
normalized_to_original: list[int] = []
|
|
426
|
+
index = 0
|
|
427
|
+
while index < len(text):
|
|
428
|
+
char = text[index]
|
|
429
|
+
if char == "\r":
|
|
430
|
+
normalized.append("\n")
|
|
431
|
+
normalized_to_original.append(index)
|
|
432
|
+
if index + 1 < len(text) and text[index + 1] == "\n":
|
|
433
|
+
index += 2
|
|
434
|
+
else:
|
|
435
|
+
index += 1
|
|
436
|
+
continue
|
|
437
|
+
|
|
438
|
+
normalized.append(char)
|
|
439
|
+
normalized_to_original.append(index)
|
|
440
|
+
index += 1
|
|
441
|
+
|
|
442
|
+
return "".join(normalized), normalized_to_original
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
def _adapt_replacement_newlines(new_text: str, *, original_old_text: str) -> str:
|
|
446
|
+
newline = _dominant_newline(original_old_text)
|
|
447
|
+
if newline is None:
|
|
448
|
+
return new_text
|
|
449
|
+
|
|
450
|
+
return _normalize_newlines(new_text).replace("\n", newline)
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
def _dominant_newline(text: str) -> str | None:
|
|
454
|
+
counts = {
|
|
455
|
+
"\r\n": text.count("\r\n"),
|
|
456
|
+
"\n": text.count("\n") - text.count("\r\n"),
|
|
457
|
+
"\r": text.count("\r") - text.count("\r\n"),
|
|
458
|
+
}
|
|
459
|
+
newline, count = max(counts.items(), key=lambda item: item[1])
|
|
460
|
+
if count == 0:
|
|
461
|
+
return None
|
|
462
|
+
|
|
463
|
+
return newline
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
def _should_return_snippet(decision: PermissionDecision) -> bool:
|
|
467
|
+
return decision.metadata.get("path_scope") != "sensitive_path"
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
class _EditSnippet:
|
|
471
|
+
def __init__(
|
|
472
|
+
self,
|
|
473
|
+
*,
|
|
474
|
+
content: str,
|
|
475
|
+
line_start: int,
|
|
476
|
+
line_end: int,
|
|
477
|
+
truncated: bool,
|
|
478
|
+
) -> None:
|
|
479
|
+
self.content = content
|
|
480
|
+
self.line_start = line_start
|
|
481
|
+
self.line_end = line_end
|
|
482
|
+
self.truncated = truncated
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
def _build_edit_snippet(
|
|
486
|
+
*,
|
|
487
|
+
edited_text: str,
|
|
488
|
+
replacement_start: int,
|
|
489
|
+
replacement_chars: int,
|
|
490
|
+
context_lines: int,
|
|
491
|
+
) -> _EditSnippet:
|
|
492
|
+
if edited_text == "":
|
|
493
|
+
return _EditSnippet(
|
|
494
|
+
content="",
|
|
495
|
+
line_start=1,
|
|
496
|
+
line_end=1,
|
|
497
|
+
truncated=False,
|
|
498
|
+
)
|
|
499
|
+
|
|
500
|
+
lines = edited_text.splitlines()
|
|
501
|
+
if not lines:
|
|
502
|
+
lines = [edited_text]
|
|
503
|
+
|
|
504
|
+
first_changed_line = edited_text[:replacement_start].count("\n")
|
|
505
|
+
replacement_end = replacement_start + replacement_chars
|
|
506
|
+
last_changed_line = edited_text[:replacement_end].count("\n")
|
|
507
|
+
last_changed_line = min(last_changed_line, len(lines) - 1)
|
|
508
|
+
|
|
509
|
+
snippet_start = max(0, first_changed_line - context_lines)
|
|
510
|
+
snippet_end = min(len(lines) - 1, last_changed_line + context_lines)
|
|
511
|
+
snippet_lines = [
|
|
512
|
+
f"{line_number} | {lines[line_index]}"
|
|
513
|
+
for line_index, line_number in enumerate(
|
|
514
|
+
range(snippet_start + 1, snippet_end + 2),
|
|
515
|
+
start=snippet_start,
|
|
516
|
+
)
|
|
517
|
+
]
|
|
518
|
+
|
|
519
|
+
return _EditSnippet(
|
|
520
|
+
content="\n".join(snippet_lines),
|
|
521
|
+
line_start=snippet_start + 1,
|
|
522
|
+
line_end=snippet_end + 1,
|
|
523
|
+
truncated=snippet_start > 0 or snippet_end < len(lines) - 1,
|
|
524
|
+
)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
from mycode.permissions import PermissionDecision
|
|
4
|
+
from mycode.tools.permission_metadata import with_permission_metadata
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def resolved_path_from_decision(decision: PermissionDecision) -> Path:
|
|
8
|
+
resolved_path = decision.metadata.get("resolved_path")
|
|
9
|
+
if isinstance(resolved_path, str):
|
|
10
|
+
return Path(resolved_path)
|
|
11
|
+
|
|
12
|
+
raise ValueError("Path permission decision did not include resolved_path.")
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def display_path(path: Path, root: Path) -> str:
|
|
16
|
+
try:
|
|
17
|
+
return path.relative_to(root).as_posix()
|
|
18
|
+
except ValueError:
|
|
19
|
+
return path.as_posix()
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def failure_metadata(
|
|
23
|
+
decision: PermissionDecision,
|
|
24
|
+
metadata: dict[str, object],
|
|
25
|
+
) -> dict[str, object]:
|
|
26
|
+
return {
|
|
27
|
+
**decision.metadata,
|
|
28
|
+
**metadata,
|
|
29
|
+
"permission_status": decision.status,
|
|
30
|
+
}
|