coder-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.
Files changed (163) hide show
  1. coder_agent-0.1.0.dist-info/METADATA +262 -0
  2. coder_agent-0.1.0.dist-info/RECORD +163 -0
  3. coder_agent-0.1.0.dist-info/WHEEL +5 -0
  4. coder_agent-0.1.0.dist-info/entry_points.txt +2 -0
  5. coder_agent-0.1.0.dist-info/licenses/LICENSE.md +21 -0
  6. coder_agent-0.1.0.dist-info/top_level.txt +1 -0
  7. coderagent/__init__.py +20 -0
  8. coderagent/compat/__init__.py +27 -0
  9. coderagent/compat/cache_control.py +69 -0
  10. coderagent/compat/config_spec.py +60 -0
  11. coderagent/compat/exceptions.py +20 -0
  12. coderagent/compat/retry.py +27 -0
  13. coderagent/compat/serialize.py +31 -0
  14. coderagent/config/__init__.py +1 -0
  15. coderagent/config/defaults.yaml +103 -0
  16. coderagent/config/loader.py +264 -0
  17. coderagent/config/schema.py +548 -0
  18. coderagent/config/writer.py +218 -0
  19. coderagent/core/__init__.py +1 -0
  20. coderagent/core/budget.py +47 -0
  21. coderagent/core/capabilities.py +136 -0
  22. coderagent/core/checkpoint.py +164 -0
  23. coderagent/core/content.py +156 -0
  24. coderagent/core/context.py +618 -0
  25. coderagent/core/events.py +83 -0
  26. coderagent/core/history.py +56 -0
  27. coderagent/core/instructions.py +99 -0
  28. coderagent/core/loop.py +702 -0
  29. coderagent/core/memory.py +56 -0
  30. coderagent/core/mentions.py +81 -0
  31. coderagent/core/os_sandbox.py +191 -0
  32. coderagent/core/otel.py +214 -0
  33. coderagent/core/protocols.py +38 -0
  34. coderagent/core/redact.py +49 -0
  35. coderagent/core/sandbox.py +82 -0
  36. coderagent/core/schema.py +107 -0
  37. coderagent/core/session.py +344 -0
  38. coderagent/core/shell_env.py +74 -0
  39. coderagent/core/subagent.py +474 -0
  40. coderagent/core/summarize.py +312 -0
  41. coderagent/core/telemetry.py +209 -0
  42. coderagent/core/tokens.py +132 -0
  43. coderagent/core/trust.py +301 -0
  44. coderagent/environments/__init__.py +11 -0
  45. coderagent/environments/bubblewrap.py +150 -0
  46. coderagent/environments/contree.py +146 -0
  47. coderagent/environments/docker.py +160 -0
  48. coderagent/environments/local.py +110 -0
  49. coderagent/environments/singularity.py +138 -0
  50. coderagent/environments/swerex_docker.py +79 -0
  51. coderagent/environments/swerex_modal.py +123 -0
  52. coderagent/eval/__init__.py +1 -0
  53. coderagent/eval/compare.py +190 -0
  54. coderagent/eval/report.py +141 -0
  55. coderagent/eval/runner.py +368 -0
  56. coderagent/eval/task.py +194 -0
  57. coderagent/extensions/__init__.py +1 -0
  58. coderagent/extensions/hooks.py +121 -0
  59. coderagent/extensions/plugins.py +217 -0
  60. coderagent/extensions/reflect.py +342 -0
  61. coderagent/models/__init__.py +33 -0
  62. coderagent/models/backends.py +218 -0
  63. coderagent/models/call.py +18 -0
  64. coderagent/models/check.py +139 -0
  65. coderagent/models/gateway.py +60 -0
  66. coderagent/models/litellm_config.py +70 -0
  67. coderagent/models/provider.py +98 -0
  68. coderagent/models/reasoning.py +28 -0
  69. coderagent/models/router.py +136 -0
  70. coderagent/models/settings.py +172 -0
  71. coderagent/models/streaming.py +122 -0
  72. coderagent/models/verbosity.py +14 -0
  73. coderagent/paths.py +31 -0
  74. coderagent/run.py +454 -0
  75. coderagent/skills/__init__.py +119 -0
  76. coderagent/skills/errors.py +11 -0
  77. coderagent/skills/install.py +340 -0
  78. coderagent/skills/presets/code-review/SKILL.md +35 -0
  79. coderagent/skills/presets/codebase-onboarding/SKILL.md +33 -0
  80. coderagent/skills/presets/data-analysis/SKILL.md +35 -0
  81. coderagent/skills/presets/deep-research/SKILL.md +38 -0
  82. coderagent/skills/presets/frontend-dev/SKILL.md +32 -0
  83. coderagent/skills/presets/long-document/SKILL.md +37 -0
  84. coderagent/skills/presets/long-document/scripts/pdf_text.py +37 -0
  85. coderagent/skills/presets/office-docx/SKILL.md +36 -0
  86. coderagent/skills/presets/office-docx/scripts/docx_text.py +36 -0
  87. coderagent/skills/presets/office-pptx/SKILL.md +32 -0
  88. coderagent/skills/presets/office-xlsx/SKILL.md +34 -0
  89. coderagent/skills/presets/office-xlsx/scripts/xlsx_text.py +72 -0
  90. coderagent/skills/presets/writing/SKILL.md +31 -0
  91. coderagent/skills/sandbox.py +269 -0
  92. coderagent/skills/sites.py +177 -0
  93. coderagent/skills/source.py +404 -0
  94. coderagent/testing/__init__.py +5 -0
  95. coderagent/testing/factory.py +59 -0
  96. coderagent/testing/scripted_model.py +54 -0
  97. coderagent/tools/__init__.py +1 -0
  98. coderagent/tools/base.py +54 -0
  99. coderagent/tools/bash_policy.py +486 -0
  100. coderagent/tools/builtin/__init__.py +35 -0
  101. coderagent/tools/builtin/fs.py +283 -0
  102. coderagent/tools/builtin/patch.py +279 -0
  103. coderagent/tools/builtin/search.py +242 -0
  104. coderagent/tools/builtin/shell.py +336 -0
  105. coderagent/tools/builtin/skill.py +41 -0
  106. coderagent/tools/builtin/task.py +53 -0
  107. coderagent/tools/builtin/todo.py +63 -0
  108. coderagent/tools/executor.py +243 -0
  109. coderagent/tools/mcp.py +317 -0
  110. coderagent/tools/permissions.py +472 -0
  111. coderagent/tools/registry.py +83 -0
  112. coderagent/tools/rules.py +138 -0
  113. coderagent/ui/__init__.py +1 -0
  114. coderagent/ui/cli.py +498 -0
  115. coderagent/ui/commands/__init__.py +109 -0
  116. coderagent/ui/commands/builtin.py +778 -0
  117. coderagent/ui/commands/compact.py +12 -0
  118. coderagent/ui/commands/resume.py +26 -0
  119. coderagent/ui/commands/review.py +73 -0
  120. coderagent/ui/commands/rewind.py +28 -0
  121. coderagent/ui/custom_commands.py +179 -0
  122. coderagent/ui/notify.py +46 -0
  123. coderagent/ui/open_file.py +48 -0
  124. coderagent/ui/render.py +62 -0
  125. coderagent/ui/spinner.py +68 -0
  126. coderagent/ui/tui.py +610 -0
  127. coderagent/ui/widgets/__init__.py +29 -0
  128. coderagent/ui/widgets/approval_prompt.py +129 -0
  129. coderagent/ui/widgets/diff_view.py +97 -0
  130. coderagent/ui/widgets/file_tree.py +105 -0
  131. coderagent/ui/widgets/markdown_message.py +44 -0
  132. coderagent/ui/widgets/markup.py +10 -0
  133. coderagent/ui/widgets/message_block.py +61 -0
  134. coderagent/ui/widgets/path_suggester.py +66 -0
  135. coderagent/ui/widgets/session_picker.py +49 -0
  136. coderagent/ui/widgets/status_bar.py +44 -0
  137. coderagent/ui/widgets/todo_panel.py +40 -0
  138. coderagent/ui/widgets/tool_card.py +104 -0
  139. coderagent/ui/widgets/working_dots.py +26 -0
  140. coderagent/version.py +7 -0
  141. coderagent/web/__init__.py +5 -0
  142. coderagent/web/api.py +449 -0
  143. coderagent/web/app.py +113 -0
  144. coderagent/web/approver.py +73 -0
  145. coderagent/web/config_api.py +53 -0
  146. coderagent/web/events.py +159 -0
  147. coderagent/web/files.py +115 -0
  148. coderagent/web/i18n.py +346 -0
  149. coderagent/web/server.py +100 -0
  150. coderagent/web/sessions.py +404 -0
  151. coderagent/web/skills_api.py +376 -0
  152. coderagent/web/static/app.css +283 -0
  153. coderagent/web/static/app.js +1466 -0
  154. coderagent/web/templates/en/base.html +31 -0
  155. coderagent/web/templates/en/chat.html +60 -0
  156. coderagent/web/templates/en/macros.html +9 -0
  157. coderagent/web/templates/en/settings.html +80 -0
  158. coderagent/web/templates/en/skills.html +64 -0
  159. coderagent/web/templates/zh/base.html +31 -0
  160. coderagent/web/templates/zh/chat.html +60 -0
  161. coderagent/web/templates/zh/macros.html +9 -0
  162. coderagent/web/templates/zh/settings.html +77 -0
  163. coderagent/web/templates/zh/skills.html +61 -0
@@ -0,0 +1,486 @@
1
+ """What a shell command is, and where it may reach.
2
+
3
+ The known commands are a tree of prefixes: `git` has subcommands and they do
4
+ not all do the same. A command that only looks around is safe, one that
5
+ destroys something or sends it out is dangerous, and the rest is unknown.
6
+
7
+ The disk is bounded like the file tools are: the working directory is where the
8
+ agent works, and the system directories hold the programs it runs.
9
+ """
10
+
11
+ import os
12
+ import re
13
+ import shlex
14
+ from pathlib import Path
15
+
16
+ SYSTEM_PREFIXES = ("/bin", "/sbin", "/usr", "/lib", "/lib64", "/opt")
17
+ """Where the programs of the system live. A command may name them."""
18
+
19
+ VARIABLE = re.compile(r"\$(\w+|\{[A-Za-z_][A-Za-z0-9_]*\})")
20
+ """A variable of a word, like `$HOME` or `${HOME}`."""
21
+
22
+ COMMANDS: dict[str, object] = {
23
+ # the commands that only look around
24
+ "ls": None,
25
+ "pwd": None,
26
+ "echo": None,
27
+ "cat": None,
28
+ "head": None,
29
+ "tail": None,
30
+ "wc": None,
31
+ "stat": None,
32
+ "file": None,
33
+ "du": None,
34
+ "df": None,
35
+ "tree": None,
36
+ "which": None,
37
+ "basename": None,
38
+ "dirname": None,
39
+ "realpath": None,
40
+ "date": None,
41
+ "whoami": None,
42
+ "id": None,
43
+ "uname": None,
44
+ "hostname": None,
45
+ "diff": None,
46
+ "cmp": None,
47
+ "grep": None,
48
+ "rg": None,
49
+ "jq": None,
50
+ "md5sum": None,
51
+ "shasum": None,
52
+ "git": {
53
+ "status": None,
54
+ "diff": None,
55
+ "log": None,
56
+ "show": None,
57
+ "blame": None,
58
+ "rev-parse": None,
59
+ "ls-files": None,
60
+ "shortlog": None,
61
+ "cat-file": None,
62
+ "describe": None,
63
+ "push": "sends commits to a remote",
64
+ "pull": "changes the working tree from a remote",
65
+ "fetch": "downloads from a remote",
66
+ "clone": "downloads a repository",
67
+ "reset": "throws the commits away",
68
+ "clean": "deletes the files that are not tracked",
69
+ "checkout": "changes the working tree",
70
+ "commit": "writes to the history of the repository",
71
+ "add": "writes to the index of the repository",
72
+ "branch": "changes the branches of the repository",
73
+ "rebase": "rewrites the history of the repository",
74
+ "merge": "merges into the working tree",
75
+ "tag": "changes the tags of the repository",
76
+ "stash": "hides the changes of the working tree",
77
+ "config": "changes the settings of the repository",
78
+ "remote": "changes the remotes of the repository",
79
+ "submodule": "changes the submodules of the repository",
80
+ },
81
+ # the commands that destroy something or send it out
82
+ "rm": "deletes files",
83
+ "rmdir": "deletes directories",
84
+ "mv": "moves files",
85
+ "cp": "copies files",
86
+ "chmod": "changes the permissions of files",
87
+ "chown": "changes the owner of files",
88
+ "dd": "writes blocks to a device",
89
+ "mkfs": "makes a filesystem",
90
+ "curl": "sends data out of the machine",
91
+ "wget": "downloads from the network",
92
+ "ssh": "opens a connection to another machine",
93
+ "scp": "copies files from another machine",
94
+ "sftp": "opens a connection to another machine",
95
+ "rsync": "copies files to another machine",
96
+ "nc": "opens a network connection",
97
+ "kill": "stops a process",
98
+ "killall": "stops the processes of a name",
99
+ "pkill": "stops the processes of a pattern",
100
+ "sudo": "runs a command as another user",
101
+ "su": "runs a shell as another user",
102
+ "docker": "controls the containers",
103
+ "kubectl": "controls the clusters of kubernetes",
104
+ "terraform": "changes the infrastructure",
105
+ "aws": "calls amazon web services",
106
+ "gcloud": "calls google cloud",
107
+ "az": "calls microsoft azure",
108
+ "npm": "installs and runs javascript packages",
109
+ "npx": "runs a javascript package",
110
+ "yarn": "installs and runs javascript packages",
111
+ "pnpm": "installs and runs javascript packages",
112
+ "pip": "installs python packages",
113
+ "pip3": "installs python packages",
114
+ "apt": "installs the packages of the system",
115
+ "apt-get": "installs the packages of the system",
116
+ "yum": "installs the packages of the system",
117
+ "dnf": "installs the packages of the system",
118
+ "brew": "installs the packages of the system",
119
+ "crontab": "changes the jobs of the scheduler",
120
+ "systemctl": "controls the services of the system",
121
+ "launchctl": "controls the services of the system",
122
+ }
123
+ """The tree of the known commands. A leaf is `None` when it is safe, a text when it is dangerous."""
124
+
125
+ _MISSING = object()
126
+ """The marker for a command that is not in the tree."""
127
+
128
+ NETWORK_COMMANDS = {
129
+ "curl",
130
+ "wget",
131
+ "ssh",
132
+ "scp",
133
+ "sftp",
134
+ "rsync",
135
+ "nc",
136
+ "ncat",
137
+ "netcat",
138
+ "telnet",
139
+ "ftp",
140
+ "aria2c",
141
+ "http",
142
+ "https",
143
+ "ping",
144
+ "npm",
145
+ "npx",
146
+ "yarn",
147
+ "pnpm",
148
+ "pip",
149
+ "pip3",
150
+ "uv",
151
+ "poetry",
152
+ "apt",
153
+ "apt-get",
154
+ "yum",
155
+ "dnf",
156
+ "brew",
157
+ "aws",
158
+ "gcloud",
159
+ "az",
160
+ "docker",
161
+ "kubectl",
162
+ "terraform",
163
+ "gh",
164
+ "glab",
165
+ }
166
+ """The commands that reach the network."""
167
+
168
+ NETWORK_SUBCOMMANDS = {"git": ("clone", "fetch", "pull", "push")}
169
+ """The subcommands of a command that reach the network."""
170
+
171
+ EMBEDDED_PATH = re.compile(r"""['"(]\s*(/[^\s'"()]+)""")
172
+ """An absolute path inside a quoted word, like the file of a small script."""
173
+
174
+
175
+ def segments(command: str) -> list[str]:
176
+ """The parts of a command: what a shell would run one after the other.
177
+
178
+ A substitution runs a command too: what is inside `$(...)` or between
179
+ backticks is a part of its own, so it is judged on its own.
180
+ """
181
+ parts: list[str] = []
182
+ current: list[str] = []
183
+ nested: list[str] = []
184
+ quote = ""
185
+ index = 0
186
+ while index < len(command):
187
+ char = command[index]
188
+ if quote:
189
+ current.append(char)
190
+ if char == quote:
191
+ quote = ""
192
+ elif char in "'\"":
193
+ quote = char
194
+ current.append(char)
195
+ elif command[index : index + 2] == "$(":
196
+ inner, index = _inside(command, index + 2, ")")
197
+ nested.extend(segments(inner))
198
+ elif char == "`":
199
+ inner, index = _inside(command, index + 1, "`")
200
+ nested.extend(segments(inner))
201
+ elif command[index : index + 2] in ("&&", "||"):
202
+ parts.append("".join(current))
203
+ parts.extend(nested)
204
+ current, nested = [], []
205
+ index += 1
206
+ elif char in ";|\n":
207
+ parts.append("".join(current))
208
+ parts.extend(nested)
209
+ current, nested = [], []
210
+ else:
211
+ current.append(char)
212
+ index += 1
213
+ parts.append("".join(current))
214
+ parts.extend(nested)
215
+ return [part.strip() for part in parts if part.strip()]
216
+
217
+
218
+ def _inside(command: str, start: int, closer: str) -> tuple[str, int]:
219
+ """The text of a substitution, and where it ends."""
220
+ depth = 1
221
+ quote = ""
222
+ index = start
223
+ while index < len(command):
224
+ char = command[index]
225
+ if quote:
226
+ if char == quote:
227
+ quote = ""
228
+ elif char in "'\"":
229
+ quote = char
230
+ elif closer == ")" and char == "(":
231
+ depth += 1
232
+ elif char == closer:
233
+ depth -= 1
234
+ if depth == 0:
235
+ return command[start:index], index + 1
236
+ index += 1
237
+ return command[start:], len(command)
238
+
239
+
240
+ def tokens(segment: str) -> list[str]:
241
+ """The words of a part, with the quotes still on them."""
242
+ lexer = shlex.shlex(segment, posix=False)
243
+ lexer.whitespace_split = True
244
+ lexer.commenters = ""
245
+ try:
246
+ return list(lexer)
247
+ except ValueError:
248
+ return segment.split()
249
+
250
+
251
+ def classify_segment(segment: str) -> tuple[str, str]:
252
+ """The kind of a part: `safe`, `dangerous` or `unknown`, and why it is dangerous."""
253
+ parts = tokens(segment)
254
+ if not parts:
255
+ return "unknown", ""
256
+ program = parts[0].strip("\"'")
257
+ if "/" in program:
258
+ """A program of a path is not one of the known ones."""
259
+ return "unknown", ""
260
+ if writes(segment):
261
+ """A command that redirects writes something, whatever it is."""
262
+ return "unknown", ""
263
+ value = COMMANDS.get(program, _MISSING)
264
+ if isinstance(value, dict):
265
+ subcommand = _subcommand(parts[1:])
266
+ value = value.get(subcommand, _MISSING) if subcommand else _MISSING
267
+ if value is _MISSING:
268
+ return "unknown", ""
269
+ if value is None:
270
+ return "safe", ""
271
+ return "dangerous", str(value)
272
+
273
+
274
+ def classify(command: str) -> tuple[str, str]:
275
+ """The kind of a command: the strictest of its parts."""
276
+ kinds = [classify_segment(segment) for segment in segments(command)]
277
+ if not kinds:
278
+ return "unknown", ""
279
+ if any(kind == "dangerous" for kind, _ in kinds):
280
+ return "dangerous", next(reason for kind, reason in kinds if kind == "dangerous")
281
+ if all(kind == "safe" for kind, _ in kinds):
282
+ return "safe", ""
283
+ return "unknown", ""
284
+
285
+
286
+ def needs_network(segment: str) -> str:
287
+ """Why a part of a command reaches the network, empty when it does not.
288
+
289
+ Only the known commands are looked at: a program that is not known may
290
+ reach the network, but the policy cannot tell.
291
+ """
292
+ parts = tokens(segment)
293
+ if not parts:
294
+ return ""
295
+ program = parts[0].strip("\"'")
296
+ if "/" in program:
297
+ """A program of a path is not one of the known ones."""
298
+ return ""
299
+ if program in NETWORK_COMMANDS:
300
+ return f"'{program}' reaches the network"
301
+ subcommands = NETWORK_SUBCOMMANDS.get(program)
302
+ if subcommands:
303
+ """Every word is looked at: `git -C /tmp push` is a push."""
304
+ for argument in parts[1:]:
305
+ value = argument.strip("\"'")
306
+ if value in subcommands:
307
+ return f"'{program} {value}' reaches the network"
308
+ return ""
309
+
310
+
311
+ def writes(segment: str) -> bool:
312
+ """Whether a part writes a file with a redirection."""
313
+ parts = tokens(segment)
314
+ for index, token in enumerate(parts):
315
+ value = token.strip("\"'")
316
+ if value in (">", ">>"):
317
+ target = parts[index + 1].strip("\"'") if index + 1 < len(parts) else ""
318
+ elif ">" in value:
319
+ target = value.split(">", 1)[1]
320
+ else:
321
+ continue
322
+ if target and not target.startswith("/dev/null"):
323
+ return True
324
+ return False
325
+
326
+
327
+ def outside_paths(command: str, cwd: Path, include_dirs: list[Path]) -> list[str]:
328
+ """The paths a command reaches that are outside the working directory.
329
+
330
+ The system directories are open for the programs, the working directory for
331
+ everything else. A command that changes the directory moves the ground the
332
+ paths after it are relative to. The variables of the command are expanded
333
+ first: a path that hides behind `$HOME` is a path all the same.
334
+ """
335
+ outside: list[str] = []
336
+ cwd = Path(cwd).resolve()
337
+ include_dirs = [_resolve(directory, cwd) for directory in include_dirs]
338
+ virtual = cwd
339
+ variables: dict[str, str] = {}
340
+ for segment in segments(command):
341
+ parts = tokens(segment)
342
+ if not parts:
343
+ continue
344
+ program = parts[0].strip("\"'")
345
+ _remember(variables, program)
346
+ program = _expand(program, variables)
347
+ if "/" in program and not _allowed(_resolve(program, virtual), cwd, include_dirs, system=True):
348
+ outside.append(program)
349
+ for token in parts[1:]:
350
+ _remember(variables, token)
351
+ for candidate in _candidates(token.strip("\"'"), variables):
352
+ if not _allowed(_resolve(candidate, virtual), cwd, include_dirs):
353
+ outside.append(candidate)
354
+ if Path(program).name == "cd" and len(parts) > 1:
355
+ target = parts[1].strip("\"'")
356
+ resolved = _resolve(target, virtual)
357
+ if not _allowed(resolved, cwd, include_dirs):
358
+ outside.append(target)
359
+ else:
360
+ virtual = resolved
361
+ return outside
362
+
363
+
364
+ def named_paths(command: str, cwd: Path) -> list[str]:
365
+ """The paths a command names, relative to the working directory.
366
+
367
+ A word is taken for a path when it looks like one — it has a separator or
368
+ a dot in it — or when there is something of that name on the disk. The
369
+ variables are expanded first, and a command that changes the directory
370
+ moves the ground the paths after it are relative to.
371
+ """
372
+ cwd = Path(cwd).resolve()
373
+ virtual = cwd
374
+ variables: dict[str, str] = {}
375
+ found: list[str] = []
376
+ for segment in segments(command):
377
+ parts = tokens(segment)
378
+ for token in parts:
379
+ value = token.strip("\"'")
380
+ _remember(variables, value)
381
+ for candidate in _path_words(_assigned(value), variables):
382
+ resolved = _resolve(candidate, virtual)
383
+ if not ("/" in candidate or "." in candidate or resolved.exists()):
384
+ continue
385
+ found.append(
386
+ resolved.relative_to(cwd).as_posix() if resolved.is_relative_to(cwd) else resolved.as_posix()
387
+ )
388
+ if parts and Path(parts[0].strip("\"'")).name == "cd" and len(parts) > 1:
389
+ virtual = _resolve(parts[1].strip("\"'"), virtual)
390
+ return found
391
+
392
+
393
+ def _assigned(value: str) -> str:
394
+ """The path of an assignment: what follows the equal sign."""
395
+ name, separator, assignment = value.partition("=")
396
+ if separator and (name.isidentifier() or name.startswith("-")):
397
+ return assignment.strip("\"'")
398
+ return value
399
+
400
+
401
+ def _path_words(value: str, variables: dict[str, str]) -> list[str]:
402
+ """The words of a value that may name a path."""
403
+ value = _expand(value, variables)
404
+ if not value or value.startswith("-"):
405
+ return []
406
+ words = []
407
+ for part in [value, *EMBEDDED_PATH.findall(value)]:
408
+ part = part.strip("\"'")
409
+ if part and not part.startswith("-"):
410
+ words.append(part)
411
+ return words
412
+
413
+
414
+ def _subcommand(arguments: list[str]) -> str:
415
+ """The first argument that is not a flag."""
416
+ for argument in arguments:
417
+ value = argument.strip("\"'")
418
+ if not value.startswith("-"):
419
+ return value
420
+ return ""
421
+
422
+
423
+ def _candidates(value: str, variables: dict[str, str]) -> list[str]:
424
+ """The paths a word names: the absolute ones, the home ones and the ones that climb out."""
425
+ if not value:
426
+ return []
427
+ value = _expand(value, variables)
428
+ if value.startswith("$") or ":" in value:
429
+ """A list of paths, like `$PATH`, is not one path."""
430
+ return []
431
+ candidates: list[str] = []
432
+ for part in [value, *EMBEDDED_PATH.findall(value)]:
433
+ part = part.strip("\"'")
434
+ if not part:
435
+ continue
436
+ for half in [part, *part.split("=")[1:]]:
437
+ half = half.strip("\"'")
438
+ if half.startswith("/") or half.startswith("~"):
439
+ candidates.append(half)
440
+ elif ".." in half.split("/"):
441
+ candidates.append(half)
442
+ return candidates
443
+
444
+
445
+ def _expand(value: str, variables: dict[str, str]) -> str:
446
+ """The variables of a word, as the shell would see them.
447
+
448
+ A variable that is not set is empty, like it is for the shell: the rest of
449
+ the word may still name a path.
450
+ """
451
+
452
+ def replace(match: re.Match) -> str:
453
+ name = match.group(1)
454
+ if name.startswith("{"):
455
+ name = name[1:-1]
456
+ return variables.get(name, os.environ.get(name, ""))
457
+
458
+ return VARIABLE.sub(replace, value)
459
+
460
+
461
+ def _remember(variables: dict[str, str], token: str) -> None:
462
+ """The value of an assignment, so that the words after it see it."""
463
+ name, separator, value = token.partition("=")
464
+ if separator and value and name.isidentifier():
465
+ variables[name] = value.strip("\"'")
466
+
467
+
468
+ def _resolve(path: str | Path, base: Path) -> Path:
469
+ """Where a path is, relative to the directory the command is in."""
470
+ text = str(path)
471
+ if text.startswith("~"):
472
+ return Path(text).expanduser().resolve()
473
+ if text.startswith("/"):
474
+ return Path(text).resolve()
475
+ return (base / text).resolve()
476
+
477
+
478
+ def _allowed(path: Path, cwd: Path, include_dirs: list[Path], system: bool = False) -> bool:
479
+ """Whether a path is one the agent may touch."""
480
+ if path.is_relative_to(cwd):
481
+ return True
482
+ if any(path.is_relative_to(directory) for directory in include_dirs):
483
+ return True
484
+ if system:
485
+ return any(str(path).startswith(prefix) for prefix in SYSTEM_PREFIXES)
486
+ return False
@@ -0,0 +1,35 @@
1
+ """The built-in tool set."""
2
+
3
+ from coderagent.tools.base import Tool
4
+ from coderagent.tools.builtin.fs import EditTool, MultiEditTool, ReadTool, WriteTool
5
+ from coderagent.tools.builtin.patch import ApplyPatchTool
6
+ from coderagent.tools.builtin.search import GlobTool, GrepTool, LsTool
7
+ from coderagent.tools.builtin.shell import BashKillTool, BashOutputTool, BashTool
8
+ from coderagent.tools.builtin.todo import TodoWriteTool
9
+ from coderagent.tools.registry import ToolRegistry
10
+
11
+ ALL_TOOLS: list[Tool] = [
12
+ ReadTool(),
13
+ WriteTool(),
14
+ EditTool(),
15
+ MultiEditTool(),
16
+ ApplyPatchTool(),
17
+ GlobTool(),
18
+ GrepTool(),
19
+ LsTool(),
20
+ BashTool(),
21
+ BashOutputTool(),
22
+ BashKillTool(),
23
+ TodoWriteTool(),
24
+ ]
25
+
26
+ TOOL_NAMES = [tool.name for tool in ALL_TOOLS]
27
+
28
+
29
+ def build_registry(enabled: list[str] | None = None, disabled: list[str] | None = None) -> ToolRegistry:
30
+ """Registry with the built-in tools, minus the disabled ones."""
31
+ allowed = set(enabled) if enabled else set(TOOL_NAMES)
32
+ return ToolRegistry([tool for tool in ALL_TOOLS if tool.name in allowed and tool.name not in set(disabled or [])])
33
+
34
+
35
+ __all__ = ["ALL_TOOLS", "TOOL_NAMES", "build_registry"]