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.
- coder_agent-0.1.0.dist-info/METADATA +262 -0
- coder_agent-0.1.0.dist-info/RECORD +163 -0
- coder_agent-0.1.0.dist-info/WHEEL +5 -0
- coder_agent-0.1.0.dist-info/entry_points.txt +2 -0
- coder_agent-0.1.0.dist-info/licenses/LICENSE.md +21 -0
- coder_agent-0.1.0.dist-info/top_level.txt +1 -0
- coderagent/__init__.py +20 -0
- coderagent/compat/__init__.py +27 -0
- coderagent/compat/cache_control.py +69 -0
- coderagent/compat/config_spec.py +60 -0
- coderagent/compat/exceptions.py +20 -0
- coderagent/compat/retry.py +27 -0
- coderagent/compat/serialize.py +31 -0
- coderagent/config/__init__.py +1 -0
- coderagent/config/defaults.yaml +103 -0
- coderagent/config/loader.py +264 -0
- coderagent/config/schema.py +548 -0
- coderagent/config/writer.py +218 -0
- coderagent/core/__init__.py +1 -0
- coderagent/core/budget.py +47 -0
- coderagent/core/capabilities.py +136 -0
- coderagent/core/checkpoint.py +164 -0
- coderagent/core/content.py +156 -0
- coderagent/core/context.py +618 -0
- coderagent/core/events.py +83 -0
- coderagent/core/history.py +56 -0
- coderagent/core/instructions.py +99 -0
- coderagent/core/loop.py +702 -0
- coderagent/core/memory.py +56 -0
- coderagent/core/mentions.py +81 -0
- coderagent/core/os_sandbox.py +191 -0
- coderagent/core/otel.py +214 -0
- coderagent/core/protocols.py +38 -0
- coderagent/core/redact.py +49 -0
- coderagent/core/sandbox.py +82 -0
- coderagent/core/schema.py +107 -0
- coderagent/core/session.py +344 -0
- coderagent/core/shell_env.py +74 -0
- coderagent/core/subagent.py +474 -0
- coderagent/core/summarize.py +312 -0
- coderagent/core/telemetry.py +209 -0
- coderagent/core/tokens.py +132 -0
- coderagent/core/trust.py +301 -0
- coderagent/environments/__init__.py +11 -0
- coderagent/environments/bubblewrap.py +150 -0
- coderagent/environments/contree.py +146 -0
- coderagent/environments/docker.py +160 -0
- coderagent/environments/local.py +110 -0
- coderagent/environments/singularity.py +138 -0
- coderagent/environments/swerex_docker.py +79 -0
- coderagent/environments/swerex_modal.py +123 -0
- coderagent/eval/__init__.py +1 -0
- coderagent/eval/compare.py +190 -0
- coderagent/eval/report.py +141 -0
- coderagent/eval/runner.py +368 -0
- coderagent/eval/task.py +194 -0
- coderagent/extensions/__init__.py +1 -0
- coderagent/extensions/hooks.py +121 -0
- coderagent/extensions/plugins.py +217 -0
- coderagent/extensions/reflect.py +342 -0
- coderagent/models/__init__.py +33 -0
- coderagent/models/backends.py +218 -0
- coderagent/models/call.py +18 -0
- coderagent/models/check.py +139 -0
- coderagent/models/gateway.py +60 -0
- coderagent/models/litellm_config.py +70 -0
- coderagent/models/provider.py +98 -0
- coderagent/models/reasoning.py +28 -0
- coderagent/models/router.py +136 -0
- coderagent/models/settings.py +172 -0
- coderagent/models/streaming.py +122 -0
- coderagent/models/verbosity.py +14 -0
- coderagent/paths.py +31 -0
- coderagent/run.py +454 -0
- coderagent/skills/__init__.py +119 -0
- coderagent/skills/errors.py +11 -0
- coderagent/skills/install.py +340 -0
- coderagent/skills/presets/code-review/SKILL.md +35 -0
- coderagent/skills/presets/codebase-onboarding/SKILL.md +33 -0
- coderagent/skills/presets/data-analysis/SKILL.md +35 -0
- coderagent/skills/presets/deep-research/SKILL.md +38 -0
- coderagent/skills/presets/frontend-dev/SKILL.md +32 -0
- coderagent/skills/presets/long-document/SKILL.md +37 -0
- coderagent/skills/presets/long-document/scripts/pdf_text.py +37 -0
- coderagent/skills/presets/office-docx/SKILL.md +36 -0
- coderagent/skills/presets/office-docx/scripts/docx_text.py +36 -0
- coderagent/skills/presets/office-pptx/SKILL.md +32 -0
- coderagent/skills/presets/office-xlsx/SKILL.md +34 -0
- coderagent/skills/presets/office-xlsx/scripts/xlsx_text.py +72 -0
- coderagent/skills/presets/writing/SKILL.md +31 -0
- coderagent/skills/sandbox.py +269 -0
- coderagent/skills/sites.py +177 -0
- coderagent/skills/source.py +404 -0
- coderagent/testing/__init__.py +5 -0
- coderagent/testing/factory.py +59 -0
- coderagent/testing/scripted_model.py +54 -0
- coderagent/tools/__init__.py +1 -0
- coderagent/tools/base.py +54 -0
- coderagent/tools/bash_policy.py +486 -0
- coderagent/tools/builtin/__init__.py +35 -0
- coderagent/tools/builtin/fs.py +283 -0
- coderagent/tools/builtin/patch.py +279 -0
- coderagent/tools/builtin/search.py +242 -0
- coderagent/tools/builtin/shell.py +336 -0
- coderagent/tools/builtin/skill.py +41 -0
- coderagent/tools/builtin/task.py +53 -0
- coderagent/tools/builtin/todo.py +63 -0
- coderagent/tools/executor.py +243 -0
- coderagent/tools/mcp.py +317 -0
- coderagent/tools/permissions.py +472 -0
- coderagent/tools/registry.py +83 -0
- coderagent/tools/rules.py +138 -0
- coderagent/ui/__init__.py +1 -0
- coderagent/ui/cli.py +498 -0
- coderagent/ui/commands/__init__.py +109 -0
- coderagent/ui/commands/builtin.py +778 -0
- coderagent/ui/commands/compact.py +12 -0
- coderagent/ui/commands/resume.py +26 -0
- coderagent/ui/commands/review.py +73 -0
- coderagent/ui/commands/rewind.py +28 -0
- coderagent/ui/custom_commands.py +179 -0
- coderagent/ui/notify.py +46 -0
- coderagent/ui/open_file.py +48 -0
- coderagent/ui/render.py +62 -0
- coderagent/ui/spinner.py +68 -0
- coderagent/ui/tui.py +610 -0
- coderagent/ui/widgets/__init__.py +29 -0
- coderagent/ui/widgets/approval_prompt.py +129 -0
- coderagent/ui/widgets/diff_view.py +97 -0
- coderagent/ui/widgets/file_tree.py +105 -0
- coderagent/ui/widgets/markdown_message.py +44 -0
- coderagent/ui/widgets/markup.py +10 -0
- coderagent/ui/widgets/message_block.py +61 -0
- coderagent/ui/widgets/path_suggester.py +66 -0
- coderagent/ui/widgets/session_picker.py +49 -0
- coderagent/ui/widgets/status_bar.py +44 -0
- coderagent/ui/widgets/todo_panel.py +40 -0
- coderagent/ui/widgets/tool_card.py +104 -0
- coderagent/ui/widgets/working_dots.py +26 -0
- coderagent/version.py +7 -0
- coderagent/web/__init__.py +5 -0
- coderagent/web/api.py +449 -0
- coderagent/web/app.py +113 -0
- coderagent/web/approver.py +73 -0
- coderagent/web/config_api.py +53 -0
- coderagent/web/events.py +159 -0
- coderagent/web/files.py +115 -0
- coderagent/web/i18n.py +346 -0
- coderagent/web/server.py +100 -0
- coderagent/web/sessions.py +404 -0
- coderagent/web/skills_api.py +376 -0
- coderagent/web/static/app.css +283 -0
- coderagent/web/static/app.js +1466 -0
- coderagent/web/templates/en/base.html +31 -0
- coderagent/web/templates/en/chat.html +60 -0
- coderagent/web/templates/en/macros.html +9 -0
- coderagent/web/templates/en/settings.html +80 -0
- coderagent/web/templates/en/skills.html +64 -0
- coderagent/web/templates/zh/base.html +31 -0
- coderagent/web/templates/zh/chat.html +60 -0
- coderagent/web/templates/zh/macros.html +9 -0
- coderagent/web/templates/zh/settings.html +77 -0
- coderagent/web/templates/zh/skills.html +61 -0
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
"""Putting a skill on the disk: the staging, the install and the lock file.
|
|
2
|
+
|
|
3
|
+
A skill that comes from the network waits before it is used. It is downloaded
|
|
4
|
+
into a staging directory, the user is shown what it is, and only then it is
|
|
5
|
+
moved into the directory the agent reads. What was installed, and where it
|
|
6
|
+
came from, is written into a lock file.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
import shutil
|
|
11
|
+
import time
|
|
12
|
+
import uuid
|
|
13
|
+
from dataclasses import dataclass, field
|
|
14
|
+
from datetime import datetime, timezone
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
|
|
17
|
+
from coderagent.paths import config_home
|
|
18
|
+
from coderagent.skills import SKILL_FILE, SKILLS_DIR, parse_skill
|
|
19
|
+
from coderagent.skills.errors import SkillError
|
|
20
|
+
from coderagent.skills.sandbox import (
|
|
21
|
+
SKILL_NAME,
|
|
22
|
+
FileEntry,
|
|
23
|
+
Policy,
|
|
24
|
+
check_name,
|
|
25
|
+
copy_tree,
|
|
26
|
+
digest,
|
|
27
|
+
entries_of,
|
|
28
|
+
sanitize,
|
|
29
|
+
)
|
|
30
|
+
from coderagent.skills.source import Source, fetch, source_of
|
|
31
|
+
|
|
32
|
+
LOCK_FILE = "skills.lock.json"
|
|
33
|
+
"""The file that says what was installed here, and where it came from."""
|
|
34
|
+
|
|
35
|
+
CACHE_DIR = "cache/skills"
|
|
36
|
+
"""Where a download waits for the user to look at it."""
|
|
37
|
+
|
|
38
|
+
STAGE_FILE = "stage.json"
|
|
39
|
+
"""What is known about a staged skill."""
|
|
40
|
+
|
|
41
|
+
STAGE_LIFE = 30 * 60
|
|
42
|
+
"""The seconds a staged skill waits. After that it is of no use to anybody."""
|
|
43
|
+
|
|
44
|
+
SCRIPTS = (".py", ".sh", ".js", ".mjs", ".cjs", ".ts", ".rb", ".pl", ".bat", ".ps1")
|
|
45
|
+
"""The files the model may run with `bash`."""
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@dataclass
|
|
49
|
+
class Staged:
|
|
50
|
+
"""A skill that waits for the user to say yes."""
|
|
51
|
+
|
|
52
|
+
stage_id: str
|
|
53
|
+
source: Source
|
|
54
|
+
name: str
|
|
55
|
+
description: str
|
|
56
|
+
body: str
|
|
57
|
+
files: list[FileEntry]
|
|
58
|
+
sha256: str
|
|
59
|
+
warnings: list[str] = field(default_factory=list)
|
|
60
|
+
directory: Path = Path()
|
|
61
|
+
"""The directory of the skill. The scripts of the skill live in it."""
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@dataclass
|
|
65
|
+
class Installed:
|
|
66
|
+
"""What the lock file knows about an installed skill."""
|
|
67
|
+
|
|
68
|
+
name: str
|
|
69
|
+
source: Source
|
|
70
|
+
sha256: str
|
|
71
|
+
installed_at: str
|
|
72
|
+
files: list[FileEntry] = field(default_factory=list)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def stage(source: Source, policy: Policy, user_home: Path | None = None, token: str = "") -> Staged:
|
|
76
|
+
"""Download a skill and wait for the user to look at it.
|
|
77
|
+
|
|
78
|
+
Nothing is put into the directory of the agent: the skill waits in the
|
|
79
|
+
cache until the user says yes.
|
|
80
|
+
"""
|
|
81
|
+
home = Path(user_home) if user_home else config_home()
|
|
82
|
+
_clear_old_stages(home)
|
|
83
|
+
stage_id = uuid.uuid4().hex
|
|
84
|
+
workdir = home / CACHE_DIR / stage_id
|
|
85
|
+
workdir.mkdir(parents=True, exist_ok=True)
|
|
86
|
+
done = False
|
|
87
|
+
try:
|
|
88
|
+
fetched = fetch(source, workdir, policy, token=token)
|
|
89
|
+
directory = workdir / "skill"
|
|
90
|
+
if fetched.is_file():
|
|
91
|
+
directory.mkdir(parents=True, exist_ok=True)
|
|
92
|
+
shutil.copyfile(fetched, directory / SKILL_FILE)
|
|
93
|
+
files = entries_of(directory)
|
|
94
|
+
else:
|
|
95
|
+
files = copy_tree(fetched, directory, policy)
|
|
96
|
+
skill = parse_skill(directory / SKILL_FILE)
|
|
97
|
+
if skill is None:
|
|
98
|
+
raise SkillError("there is no SKILL.md in it")
|
|
99
|
+
name, warnings = _name(skill, source, files)
|
|
100
|
+
staged = Staged(
|
|
101
|
+
stage_id=stage_id,
|
|
102
|
+
source=source,
|
|
103
|
+
name=name,
|
|
104
|
+
description=skill.description,
|
|
105
|
+
body=skill.body,
|
|
106
|
+
files=files,
|
|
107
|
+
sha256=digest(files),
|
|
108
|
+
warnings=warnings,
|
|
109
|
+
directory=directory,
|
|
110
|
+
)
|
|
111
|
+
_write_stage(staged, workdir)
|
|
112
|
+
done = True
|
|
113
|
+
return staged
|
|
114
|
+
finally:
|
|
115
|
+
if not done:
|
|
116
|
+
shutil.rmtree(workdir, ignore_errors=True)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def read_stage(stage_id: str, user_home: Path | None = None) -> Staged:
|
|
120
|
+
"""A staged skill that is still waiting."""
|
|
121
|
+
home = Path(user_home) if user_home else config_home()
|
|
122
|
+
workdir = home / CACHE_DIR / stage_id
|
|
123
|
+
path = workdir / STAGE_FILE
|
|
124
|
+
if not path.exists():
|
|
125
|
+
raise SkillError("the preview is gone, download it again")
|
|
126
|
+
data = _read_json(path)
|
|
127
|
+
directory = workdir / "skill"
|
|
128
|
+
skill = parse_skill(directory / SKILL_FILE)
|
|
129
|
+
return Staged(
|
|
130
|
+
stage_id=stage_id,
|
|
131
|
+
source=source_of(data.get("source") or {}),
|
|
132
|
+
name=str(data.get("name") or ""),
|
|
133
|
+
description=str(data.get("description") or ""),
|
|
134
|
+
body=skill.body if skill else "",
|
|
135
|
+
files=[
|
|
136
|
+
FileEntry(str(item.get("path") or ""), int(item.get("size") or 0), str(item.get("sha256") or ""))
|
|
137
|
+
for item in data.get("files") or []
|
|
138
|
+
],
|
|
139
|
+
sha256=str(data.get("sha256") or ""),
|
|
140
|
+
warnings=[str(warning) for warning in data.get("warnings") or []],
|
|
141
|
+
directory=directory,
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def install(staged: Staged, user_home: Path | None = None, force: bool = False) -> Installed:
|
|
146
|
+
"""Put a staged skill where the agent finds it.
|
|
147
|
+
|
|
148
|
+
A skill that is installed already is not replaced, unless the caller says
|
|
149
|
+
that it means it.
|
|
150
|
+
"""
|
|
151
|
+
home = Path(user_home) if user_home else config_home()
|
|
152
|
+
skills = home / SKILLS_DIR
|
|
153
|
+
target = skills / staged.name
|
|
154
|
+
if target.exists():
|
|
155
|
+
if not force:
|
|
156
|
+
raise SkillError(f"'{staged.name}' is installed already")
|
|
157
|
+
shutil.rmtree(target)
|
|
158
|
+
skills.mkdir(parents=True, exist_ok=True)
|
|
159
|
+
temporary = skills / f".{staged.name}.tmp"
|
|
160
|
+
shutil.rmtree(temporary, ignore_errors=True)
|
|
161
|
+
shutil.copytree(staged.directory, temporary)
|
|
162
|
+
temporary.rename(target)
|
|
163
|
+
installed = Installed(
|
|
164
|
+
name=staged.name,
|
|
165
|
+
source=staged.source,
|
|
166
|
+
sha256=staged.sha256,
|
|
167
|
+
installed_at=datetime.now(timezone.utc).isoformat(timespec="seconds"),
|
|
168
|
+
files=staged.files,
|
|
169
|
+
)
|
|
170
|
+
lock = read_lock(user_home)
|
|
171
|
+
lock[staged.name] = installed
|
|
172
|
+
write_lock(lock, user_home)
|
|
173
|
+
shutil.rmtree(staged.directory.parent, ignore_errors=True)
|
|
174
|
+
return installed
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def check_update(installed: Installed, policy: Policy, user_home: Path | None = None, token: str = "") -> Staged | None:
|
|
178
|
+
"""The new version of an installed skill, when the source has one.
|
|
179
|
+
|
|
180
|
+
Nothing when the source still says the same thing: the digest of the
|
|
181
|
+
content is the same.
|
|
182
|
+
"""
|
|
183
|
+
staged = stage(installed.source, policy, user_home, token)
|
|
184
|
+
if staged.sha256 == installed.sha256:
|
|
185
|
+
shutil.rmtree(staged.directory.parent, ignore_errors=True)
|
|
186
|
+
return None
|
|
187
|
+
return staged
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def uninstall(name: str, user_home: Path | None = None) -> None:
|
|
191
|
+
"""Remove a skill that was installed here.
|
|
192
|
+
|
|
193
|
+
A skill the user put there himself is not ours to remove.
|
|
194
|
+
"""
|
|
195
|
+
home = Path(user_home) if user_home else config_home()
|
|
196
|
+
lock = read_lock(user_home)
|
|
197
|
+
if name not in lock:
|
|
198
|
+
raise SkillError(f"'{name}' was not installed here, it is yours")
|
|
199
|
+
shutil.rmtree(home / SKILLS_DIR / name)
|
|
200
|
+
lock.pop(name)
|
|
201
|
+
write_lock(lock, user_home)
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def read_lock(user_home: Path | None = None) -> dict[str, Installed]:
|
|
205
|
+
"""The skills that were installed here."""
|
|
206
|
+
path = lock_path(user_home)
|
|
207
|
+
if not path.exists():
|
|
208
|
+
return {}
|
|
209
|
+
data = _read_json(path)
|
|
210
|
+
skills = data.get("skills") if isinstance(data, dict) else {}
|
|
211
|
+
result = {}
|
|
212
|
+
for name, item in (skills or {}).items():
|
|
213
|
+
installed = _installed(name, item)
|
|
214
|
+
if installed is not None:
|
|
215
|
+
result[name] = installed
|
|
216
|
+
return result
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def write_lock(skills: dict[str, Installed], user_home: Path | None = None) -> Path:
|
|
220
|
+
"""Write the lock file."""
|
|
221
|
+
path = lock_path(user_home)
|
|
222
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
223
|
+
data = {
|
|
224
|
+
"version": 1,
|
|
225
|
+
"skills": {
|
|
226
|
+
name: {
|
|
227
|
+
"source": item.source.as_dict(),
|
|
228
|
+
"sha256": item.sha256,
|
|
229
|
+
"installed_at": item.installed_at,
|
|
230
|
+
"files": [{"path": entry.path, "size": entry.size, "sha256": entry.sha256} for entry in item.files],
|
|
231
|
+
}
|
|
232
|
+
for name, item in skills.items()
|
|
233
|
+
},
|
|
234
|
+
}
|
|
235
|
+
path.write_text(json.dumps(data, indent=2, sort_keys=True), encoding="utf-8")
|
|
236
|
+
return path
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def lock_path(user_home: Path | None = None) -> Path:
|
|
240
|
+
"""Where the lock file is."""
|
|
241
|
+
home = Path(user_home) if user_home else config_home()
|
|
242
|
+
return home / LOCK_FILE
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def _read_json(path: Path) -> dict:
|
|
246
|
+
"""A file of ours that holds JSON. It is not JSON: something is wrong with it."""
|
|
247
|
+
try:
|
|
248
|
+
data = json.loads(path.read_text(encoding="utf-8"))
|
|
249
|
+
except (OSError, json.JSONDecodeError) as error:
|
|
250
|
+
raise SkillError(f"{path.name} cannot be read: {error}") from None
|
|
251
|
+
return data if isinstance(data, dict) else {}
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def _installed(name: str, item: object) -> Installed | None:
|
|
255
|
+
"""One entry of the lock file. An entry that cannot be read is left out."""
|
|
256
|
+
if not isinstance(item, dict):
|
|
257
|
+
return None
|
|
258
|
+
try:
|
|
259
|
+
source = source_of(item.get("source"))
|
|
260
|
+
except SkillError:
|
|
261
|
+
return None
|
|
262
|
+
return Installed(
|
|
263
|
+
name=name,
|
|
264
|
+
source=source,
|
|
265
|
+
sha256=str(item.get("sha256") or ""),
|
|
266
|
+
installed_at=str(item.get("installed_at") or ""),
|
|
267
|
+
files=[
|
|
268
|
+
FileEntry(str(entry.get("path") or ""), int(entry.get("size") or 0), str(entry.get("sha256") or ""))
|
|
269
|
+
for entry in item.get("files") or []
|
|
270
|
+
if isinstance(entry, dict)
|
|
271
|
+
],
|
|
272
|
+
)
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
def _name(skill, source: Source, files: list[FileEntry]) -> tuple[str, list[str]]:
|
|
276
|
+
"""The name of a staged skill, and what the user should know about it."""
|
|
277
|
+
warnings = []
|
|
278
|
+
name = skill.name
|
|
279
|
+
if not SKILL_NAME.match(name):
|
|
280
|
+
"""The name of the skill is a directory name, so it has to be safe."""
|
|
281
|
+
warnings.append(f"the skill calls itself '{skill.name}', which is not a name a skill can have")
|
|
282
|
+
name = sanitize(skill.name)
|
|
283
|
+
if not SKILL_NAME.match(name):
|
|
284
|
+
name = sanitize(_fallback_name(source))
|
|
285
|
+
scripts = [entry.path for entry in files if entry.path.lower().endswith(SCRIPTS)]
|
|
286
|
+
if scripts:
|
|
287
|
+
warnings.append(f"the skill has {len(scripts)} script(s). The model may run them with `bash`.")
|
|
288
|
+
return check_name(name), warnings
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
def _fallback_name(source: Source) -> str:
|
|
292
|
+
"""The name of a skill that does not have a usable one of its own."""
|
|
293
|
+
if source.kind in ("github", "gitee", "git"):
|
|
294
|
+
if source.path:
|
|
295
|
+
return source.path.rstrip("/").split("/")[-1]
|
|
296
|
+
if source.repo:
|
|
297
|
+
return source.repo.split("/")[-1]
|
|
298
|
+
return source.url.rstrip("/").split("/")[-1].removesuffix(".git")
|
|
299
|
+
return source.url.rstrip("/").split("/")[-1].removesuffix(".md")
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
def _write_stage(staged: Staged, workdir: Path) -> None:
|
|
303
|
+
"""Write down what is known about a staged skill."""
|
|
304
|
+
data = {
|
|
305
|
+
"stage_id": staged.stage_id,
|
|
306
|
+
"source": staged.source.as_dict(),
|
|
307
|
+
"name": staged.name,
|
|
308
|
+
"description": staged.description,
|
|
309
|
+
"sha256": staged.sha256,
|
|
310
|
+
"warnings": staged.warnings,
|
|
311
|
+
"files": [{"path": entry.path, "size": entry.size, "sha256": entry.sha256} for entry in staged.files],
|
|
312
|
+
}
|
|
313
|
+
(workdir / STAGE_FILE).write_text(json.dumps(data, indent=2, sort_keys=True), encoding="utf-8")
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def _clear_old_stages(home: Path) -> None:
|
|
317
|
+
"""The stages nobody looked at are of no use to anybody."""
|
|
318
|
+
cache = home / CACHE_DIR
|
|
319
|
+
if not cache.is_dir():
|
|
320
|
+
return
|
|
321
|
+
for path in cache.iterdir():
|
|
322
|
+
if path.is_dir() and time.time() - path.stat().st_mtime > STAGE_LIFE:
|
|
323
|
+
shutil.rmtree(path, ignore_errors=True)
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
__all__ = [
|
|
327
|
+
"CACHE_DIR",
|
|
328
|
+
"Installed",
|
|
329
|
+
"LOCK_FILE",
|
|
330
|
+
"STAGE_LIFE",
|
|
331
|
+
"Staged",
|
|
332
|
+
"check_update",
|
|
333
|
+
"install",
|
|
334
|
+
"lock_path",
|
|
335
|
+
"read_lock",
|
|
336
|
+
"read_stage",
|
|
337
|
+
"stage",
|
|
338
|
+
"uninstall",
|
|
339
|
+
"write_lock",
|
|
340
|
+
]
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: code-review
|
|
3
|
+
description: Review a change before it is committed - correctness first, then the tests, then the shape of the code and its docs.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Code review
|
|
7
|
+
|
|
8
|
+
A review that lists style nits is noise. A review that misses a bug is
|
|
9
|
+
worse. The order below is the order of importance.
|
|
10
|
+
|
|
11
|
+
## Steps
|
|
12
|
+
|
|
13
|
+
1. **Read the change as a whole.** `git diff` first, then the files around
|
|
14
|
+
it. A diff without its context hides the bug.
|
|
15
|
+
2. **Correctness.** For each change ask: what input breaks it? What happens
|
|
16
|
+
when the value is empty, zero, huge or missing? Is the error handled
|
|
17
|
+
where it can be? Is the state shared between two callers that assume
|
|
18
|
+
they own it?
|
|
19
|
+
3. **The tests.** Is there a test for the behaviour that changed? Does it
|
|
20
|
+
fail without the change? Do the tests that exist still pass? Run them.
|
|
21
|
+
4. **The shape.** Names that say what the thing is, functions that do one
|
|
22
|
+
thing, no dead code, no duplication that has to be kept in step.
|
|
23
|
+
5. **The edges.** Security (injection, secrets in the code, paths from the
|
|
24
|
+
outside), performance (a loop inside a loop, a query in a loop), the
|
|
25
|
+
compatibility of the interface.
|
|
26
|
+
6. **Write the review.** Every point has a file, a line, what is wrong and
|
|
27
|
+
what to do about it. Mark the blocking points as such. Say plainly when
|
|
28
|
+
the change is good: a review that only lists faults teaches nothing.
|
|
29
|
+
|
|
30
|
+
## Rules
|
|
31
|
+
|
|
32
|
+
- One issue per point, with the line that holds it.
|
|
33
|
+
- Say what is wrong before saying how to fix it.
|
|
34
|
+
- Do not rewrite the code in the review; the author decides.
|
|
35
|
+
- A question is not a fault: mark it as a question.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: codebase-onboarding
|
|
3
|
+
description: Get to know an unfamiliar codebase - its layout, its entry points, its build and its tests - and write the map down.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Reading a codebase
|
|
7
|
+
|
|
8
|
+
## Steps
|
|
9
|
+
|
|
10
|
+
1. **Look around.** The top-level files say what the project is:
|
|
11
|
+
`README`, `pyproject.toml` or `package.json`, the directory of the
|
|
12
|
+
tests, the directory of the source. Read them.
|
|
13
|
+
2. **Find the entry points.** The command that starts it, the module that
|
|
14
|
+
runs it, the place where the requests or the jobs come in. Two or three
|
|
15
|
+
files, not twenty.
|
|
16
|
+
3. **Follow one path through.** Take a small thing the project does and
|
|
17
|
+
follow it from the entry point to where it ends: the call, the class, the
|
|
18
|
+
file it writes. That one path explains more than a directory tree.
|
|
19
|
+
4. **Find the seams.** Where does the code talk to the outside world (the
|
|
20
|
+
network, the disk, the clock)? Those are the places to look at twice.
|
|
21
|
+
5. **Find the tests.** How they are run, what they cover, what they do not.
|
|
22
|
+
6. **Write the map.** `CODEBASE.md`: what the project does, the layout, the
|
|
23
|
+
entry points, the path you followed, the build and the test commands, and
|
|
24
|
+
the open questions.
|
|
25
|
+
|
|
26
|
+
## Rules
|
|
27
|
+
|
|
28
|
+
- Read before you judge. A file that looks wrong is often doing something
|
|
29
|
+
the name does not say.
|
|
30
|
+
- Prefer the names the project itself uses: the terms of the README and of
|
|
31
|
+
the code, not your own.
|
|
32
|
+
- The map is for a reader who knows nothing about the project. That is the
|
|
33
|
+
test of every line.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: data-analysis
|
|
3
|
+
description: Analyse data (CSV, Excel, JSON, logs) - clean it, compute the figures, draw the charts and state what the data says.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Data analysis
|
|
7
|
+
|
|
8
|
+
## Steps
|
|
9
|
+
|
|
10
|
+
1. **Look at the data first.** Print the shape (rows, columns), the head and
|
|
11
|
+
the tail, the missing values and the types. `pandas` when it is
|
|
12
|
+
installed, the `csv` module of the standard library when it is not.
|
|
13
|
+
2. **Clean it, and write down what was cleaned.** Rows that are duplicated,
|
|
14
|
+
columns that mix units, dates in two formats, values that are a dash
|
|
15
|
+
instead of a zero. Every decision is a line in the report.
|
|
16
|
+
3. **Ask what the question is.** What is being measured, over which period,
|
|
17
|
+
compared with what. When the question is missing, the numbers you find
|
|
18
|
+
are decoration.
|
|
19
|
+
4. **Compute.** The figures are simple: totals, shares, changes, rates,
|
|
20
|
+
distributions. Keep the formula next to the number.
|
|
21
|
+
5. **Draw.** A chart per finding, not per column: a line for a trend, a bar
|
|
22
|
+
for a comparison, a scatter for a relation. `matplotlib` when it is
|
|
23
|
+
installed; otherwise a Markdown table and a note that the chart needs
|
|
24
|
+
it.
|
|
25
|
+
6. **Write it up.** `analysis.md`: the question, the method, the findings
|
|
26
|
+
with their numbers, and the caveats.
|
|
27
|
+
|
|
28
|
+
## Rules
|
|
29
|
+
|
|
30
|
+
- The denominator is written next to every rate.
|
|
31
|
+
- A correlation is not a cause, and an outlier is not an error until it is
|
|
32
|
+
looked at.
|
|
33
|
+
- Never fill a missing value silently: either drop the rows and say how
|
|
34
|
+
many, or fill it and say with what.
|
|
35
|
+
- Show the code that produced the number, so the number can be checked.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: deep-research
|
|
3
|
+
description: Look a question up and write a report that carries its sources. Use it when the answer has to be found, compared or cited.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Deep research
|
|
7
|
+
|
|
8
|
+
A report nobody can check is a guess with a table of contents. Every claim
|
|
9
|
+
carries the source it came from, and every source is reachable.
|
|
10
|
+
|
|
11
|
+
## Steps
|
|
12
|
+
|
|
13
|
+
1. **Fix the question.** Write it in one sentence, then list the
|
|
14
|
+
sub-questions that have to be answered for it. Ask the user when the
|
|
15
|
+
question is ambiguous in a way that changes the answer.
|
|
16
|
+
2. **Search.** Search the web for each sub-question, from the most specific
|
|
17
|
+
wording to the most general. Keep every page worth reading in
|
|
18
|
+
`research/` as markdown: the title, the URL, the date, the text.
|
|
19
|
+
3. **Read.** Read the sources themselves, not the summaries of them. The
|
|
20
|
+
paper, the filing and the documentation beat the article about them.
|
|
21
|
+
4. **Cross-check.** Every number that matters must show up in two sources
|
|
22
|
+
that do not know each other. Where they disagree, the report says so.
|
|
23
|
+
5. **Write** `research/report.md`:
|
|
24
|
+
- the answer in the first paragraph, in three sentences;
|
|
25
|
+
- one section per finding;
|
|
26
|
+
- a table at the end: claim, source, URL, date.
|
|
27
|
+
6. **Check.** Open every URL of the table and confirm it says what the report
|
|
28
|
+
claims it says.
|
|
29
|
+
|
|
30
|
+
## Rules
|
|
31
|
+
|
|
32
|
+
- Never invent a URL, a title, a date or a number. What cannot be found is
|
|
33
|
+
written as "not found".
|
|
34
|
+
- A claim without a source is an assumption: say so, in those words.
|
|
35
|
+
- Quote the sentence that carries the claim, with its location, when the
|
|
36
|
+
wording matters.
|
|
37
|
+
- Prefer a table over a paragraph when there are more than three things to
|
|
38
|
+
compare.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: frontend-dev
|
|
3
|
+
description: Build a web page or a component - HTML, CSS, JavaScript or React - with a layout that works on a phone and on a desktop.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Frontend
|
|
7
|
+
|
|
8
|
+
## Steps
|
|
9
|
+
|
|
10
|
+
1. **Look at what is there.** Find the stack: the files of the page, the
|
|
11
|
+
framework, the CSS it uses, the command that starts it. Build inside
|
|
12
|
+
that stack; do not add a framework to a page that has none.
|
|
13
|
+
2. **Write the markup.** Semantic elements first: `header`, `nav`, `main`,
|
|
14
|
+
`section`, `footer`. A `div` soup is a sign the structure was not
|
|
15
|
+
thought about.
|
|
16
|
+
3. **Write the style.** One system: spacing steps, two or three text sizes,
|
|
17
|
+
one accent colour. Colours, spacing and radii are variables at the top of
|
|
18
|
+
the file, not values scattered in the rules.
|
|
19
|
+
4. **Make it work on a phone.** The layout works at 360 pixels wide before
|
|
20
|
+
it looks good on a wide screen.
|
|
21
|
+
5. **Check it.** Run the project's build and its tests. When the page can be
|
|
22
|
+
opened, open it and look at it: the console has to be quiet and the
|
|
23
|
+
layout must not break.
|
|
24
|
+
|
|
25
|
+
## Rules
|
|
26
|
+
|
|
27
|
+
- No dependency without a reason the user can see.
|
|
28
|
+
- Images and fonts that are not local are a failure when the machine is
|
|
29
|
+
offline: keep the page working without the network.
|
|
30
|
+
- Accessibility is part of the build: a label for every input, an
|
|
31
|
+
`alt` for every image, a visible focus, and a contrast that can be read.
|
|
32
|
+
- Do not refactor the code around the change. The page is the deliverable.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: long-document
|
|
3
|
+
description: Read a long document (PDF, Word file, paper, contract, report) and answer questions about it with quotes and page numbers.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Long documents
|
|
7
|
+
|
|
8
|
+
A long document is read, not skimmed. The text is extracted first, then
|
|
9
|
+
structured, then answered.
|
|
10
|
+
|
|
11
|
+
## Steps
|
|
12
|
+
|
|
13
|
+
1. **Find the text.** For a PDF run `scripts/pdf_text.py FILE.pdf` (the script
|
|
14
|
+
is in the directory of the skill): it uses `pdftotext` when it is installed
|
|
15
|
+
and `pypdf` otherwise. For a Word file use the script of the `office-docx`
|
|
16
|
+
skill, `docx_text.py`. Keep the extracted text in `work/` next to the
|
|
17
|
+
original.
|
|
18
|
+
2. **Outline it.** Write down the structure of the document: the headings,
|
|
19
|
+
the tables, the appendices. A document that is scanned images has no
|
|
20
|
+
text: say so and ask for a text version instead of guessing.
|
|
21
|
+
3. **Answer.** Every answer quotes the sentence it rests on and names where
|
|
22
|
+
it is (page, section or heading). Numbers keep the unit and the date of
|
|
23
|
+
the document.
|
|
24
|
+
4. **Check the tables.** A table is read as a table: read the header row and
|
|
25
|
+
the units first, then the rows. Do not average a column that holds
|
|
26
|
+
categories.
|
|
27
|
+
5. **Report.** Give the answer first, then the evidence, then what the
|
|
28
|
+
document does not say.
|
|
29
|
+
|
|
30
|
+
## Rules
|
|
31
|
+
|
|
32
|
+
- The document is the truth here, not your own knowledge. When they differ,
|
|
33
|
+
the document wins and the difference is written down.
|
|
34
|
+
- Never cite a page you did not read. When the text has no page numbers,
|
|
35
|
+
cite the heading.
|
|
36
|
+
- Long documents are read in parts: read the whole text once for the shape,
|
|
37
|
+
then read closely the parts the question needs.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""The text of a PDF: with `pdftotext` when it is there, with `pypdf` otherwise."""
|
|
2
|
+
|
|
3
|
+
import shutil
|
|
4
|
+
import subprocess
|
|
5
|
+
import sys
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def with_pdftotext(path: Path) -> str | None:
|
|
10
|
+
"""The text of the file, when poppler is installed."""
|
|
11
|
+
if not shutil.which("pdftotext"):
|
|
12
|
+
return None
|
|
13
|
+
done = subprocess.run(["pdftotext", "-layout", str(path), "-"], capture_output=True, text=True)
|
|
14
|
+
return done.stdout if done.returncode == 0 else None
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def with_pypdf(path: Path) -> str | None:
|
|
18
|
+
"""The text of the file, when the library is installed."""
|
|
19
|
+
try:
|
|
20
|
+
from pypdf import PdfReader
|
|
21
|
+
except ImportError:
|
|
22
|
+
return None
|
|
23
|
+
return "\n\n".join(page.extract_text() or "" for page in PdfReader(str(path)).pages)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def text_of(path: Path) -> str:
|
|
27
|
+
"""The text of the file, by whatever means are available."""
|
|
28
|
+
text = with_pdftotext(path) or with_pypdf(path)
|
|
29
|
+
if text is None:
|
|
30
|
+
raise SystemExit(f"no way to read {path}: install poppler (pdftotext) or pypdf")
|
|
31
|
+
return text
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
if __name__ == "__main__":
|
|
35
|
+
if len(sys.argv) != 2:
|
|
36
|
+
raise SystemExit("usage: pdf_text.py FILE.pdf")
|
|
37
|
+
print(text_of(Path(sys.argv[1])))
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: office-docx
|
|
3
|
+
description: Write a Word document (.docx) - a report, a letter, the minutes of a meeting - with headings, tables and a table of contents.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Word documents
|
|
7
|
+
|
|
8
|
+
## Steps
|
|
9
|
+
|
|
10
|
+
1. **Plan the document.** Write the outline first: the headings and what
|
|
11
|
+
goes under each of them. The outline is what the reader skims, so it has
|
|
12
|
+
to carry the meaning on its own.
|
|
13
|
+
2. **Write it.** Use `python-docx` when it is installed
|
|
14
|
+
(`python -c "import docx"`): it makes headings, tables and a table of
|
|
15
|
+
contents straightforward. When it is not, write the text as Markdown and
|
|
16
|
+
convert it with `pandoc FILE.md -o FILE.docx`; when `pandoc` is not there
|
|
17
|
+
either, write Markdown and tell the user that the file is Markdown.
|
|
18
|
+
3. **Fill in the parts that matter.**
|
|
19
|
+
- A cover: title, subtitle, date, author.
|
|
20
|
+
- A table of contents for anything longer than two pages.
|
|
21
|
+
- Headings that number themselves (`1`, `1.1`) when the document is
|
|
22
|
+
technical.
|
|
23
|
+
- Tables for comparisons: a row per item, a column per property, the
|
|
24
|
+
units in the header.
|
|
25
|
+
4. **Read it back.** Run `scripts/docx_text.py FILE.docx` (the script is in
|
|
26
|
+
the directory of the skill) and read the output. It is what the reader
|
|
27
|
+
gets, so it is what has to be right: no stray placeholders, no empty
|
|
28
|
+
headings, no lost tables.
|
|
29
|
+
|
|
30
|
+
## Rules
|
|
31
|
+
|
|
32
|
+
- Short sentences, active voice, no adjective that does not carry
|
|
33
|
+
information.
|
|
34
|
+
- Numbers keep their unit and their date.
|
|
35
|
+
- A document that says everything says nothing: cut what the reader already
|
|
36
|
+
knows.
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""The text of a Word file: its paragraphs and its tables."""
|
|
2
|
+
|
|
3
|
+
import sys
|
|
4
|
+
import zipfile
|
|
5
|
+
from xml.etree import ElementTree
|
|
6
|
+
|
|
7
|
+
WORD = "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}"
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def text_of(node) -> str:
|
|
11
|
+
"""The text of a node of the document."""
|
|
12
|
+
return "".join(part.text or "" for part in node.iter(f"{WORD}t"))
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def lines_of(archive: zipfile.ZipFile) -> list[str]:
|
|
16
|
+
"""The paragraphs and the rows of the tables, in the order of the document."""
|
|
17
|
+
root = ElementTree.fromstring(archive.read("word/document.xml"))
|
|
18
|
+
body = root.find(f"{WORD}body")
|
|
19
|
+
lines = []
|
|
20
|
+
for child in body if body is not None else []:
|
|
21
|
+
if child.tag == f"{WORD}p":
|
|
22
|
+
line = text_of(child).strip()
|
|
23
|
+
if line:
|
|
24
|
+
lines.append(line)
|
|
25
|
+
elif child.tag == f"{WORD}tbl":
|
|
26
|
+
for row in child.iter(f"{WORD}tr"):
|
|
27
|
+
cells = [text_of(cell).strip() for cell in row.findall(f"{WORD}tc")]
|
|
28
|
+
lines.append("\t".join(cells))
|
|
29
|
+
return lines
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
if __name__ == "__main__":
|
|
33
|
+
if len(sys.argv) != 2:
|
|
34
|
+
raise SystemExit("usage: docx_text.py FILE.docx")
|
|
35
|
+
with zipfile.ZipFile(sys.argv[1]) as archive:
|
|
36
|
+
print("\n".join(lines_of(archive)))
|