-
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathduties.py
More file actions
311 lines (252 loc) · 10 KB
/
duties.py
File metadata and controls
311 lines (252 loc) · 10 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
"""Project tasks."""
from __future__ import annotations
from pathlib import Path
import re
from typing import Literal
from duty import duty # pyright: ignore[reportMissingImports] # ty:ignore[unresolved-import]
# Read configuration from copier-answers.yml
_answers_file = Path(".copier-answers.yml")
if _answers_file.exists():
content = _answers_file.read_text()
match = re.search(r"^python_package_import_name:\s*(.+)$", content, re.MULTILINE)
if match:
PACKAGE_NAME = match.group(1).strip()
else:
msg = "python_package_import_name not found in copier-answers.yml"
raise ValueError(msg)
else:
msg = "copier-answers.yml not found in project root"
raise FileNotFoundError(msg)
@duty(capture=False)
def build(ctx, *args: str):
"""Build documentation."""
import subprocess
args_str = " " + " ".join(args) if args else ""
result = subprocess.run(
f"uv run zensical build{args_str}",
check=False,
shell=True,
capture_output=True,
text=True,
)
# Print output
if result.stdout:
print(result.stdout)
if result.stderr:
print(result.stderr)
# Check for errors in output (mkdocs may exit 0 even with template errors)
error_patterns = ["Error:", "error:", "template not found"]
combined_output = (result.stdout or "") + (result.stderr or "")
for pattern in error_patterns:
if pattern in combined_output:
msg = f"Build failed: found '{pattern}' in output"
raise RuntimeError(msg)
if result.returncode != 0:
msg = f"Build failed with exit code {result.returncode}"
raise RuntimeError(msg)
ctx.run("uv run python scripts/reorder_nav.py")
@duty(capture=False)
def serve(ctx, *args: str):
"""Serve documentation. Pass --reorder to build, reorder nav, and serve static files.
With --reorder, also accepts --port=XXXX (default: 8000).
"""
reorder = "--reorder" in args
args = tuple(a for a in args if a != "--reorder")
# Extract --port for reorder mode
port = "8000"
new_args = []
for arg in args:
if arg.startswith("--port="):
port = arg.split("=", 1)[1]
else:
new_args.append(arg)
args = tuple(new_args)
if reorder:
ctx.run("uv run zensical build")
ctx.run("uv run python scripts/reorder_nav.py")
# Serve the static site directly (zensical serve would rebuild and undo reorder)
# Create a temp structure to match zensical's URL scheme: /agentpool/
import tempfile
with tempfile.TemporaryDirectory() as tmpdir:
tmp_path = Path(tmpdir)
(tmp_path / "agentpool").symlink_to(Path("site").resolve())
print(f"Serving at http://localhost:{port}/agentpool/")
ctx.run(f"uv run python -m http.server {port} -d {tmpdir} -b localhost")
else:
args_str = " " + " ".join(args) if args else ""
ctx.run(f"uv run zensical serve{args_str}")
@duty(capture=False)
def test(ctx, *args: str):
"""Run tests."""
args_str = " " + " ".join(args) if args else ""
args_str = " -n auto" + args_str
ctx.run(f"uv run pytest{args_str}")
@duty(capture=False)
def clean(ctx):
"""Clean all files from the Git directory except checked-in files."""
ctx.run("git clean -dfX")
@duty(capture=False)
def update(ctx):
"""Update all environment packages using pip directly."""
ctx.run("uv lock --upgrade")
ctx.run("uv sync --all-extras")
def _get_lint_targets(filepath: str | None) -> tuple[str, str, str | None]:
"""Get lint targets based on optional filepath.
Returns:
Tuple of (ruff_target, mypy_target, jsonschema_files_or_none)
"""
if filepath is None:
return (
".",
"src/",
"src/agentpool/config_resources/*.yml docs/examples/**/config.yml",
)
path = Path(filepath)
# For ruff, use the file directly
ruff_target = filepath
# For mypy, if it's under src/, use the file; otherwise skip
mypy_target = filepath if filepath.startswith("src/") else ""
# For jsonschema, only run if the file matches the patterns
jsonschema_files = None
if path.suffix in {".yml", ".yaml"} and (
"config_resources" in filepath
or ("docs/examples" in filepath and path.name == "config.yml")
):
jsonschema_files = filepath
return ruff_target, mypy_target, jsonschema_files
@duty(capture=False)
def lint(ctx, filepath: str | None = None): # noqa: D417
"""Lint the code and fix issues if possible.
Args:
filepath: Optional path to a specific file to lint.
If not provided, lints the entire project.
"""
ruff_target, mypy_target, jsonschema_files = _get_lint_targets(filepath)
ctx.run("ty check .")
ctx.run(f"uv run ruff check --fix --unsafe-fixes {ruff_target}")
ctx.run(f"uv run ruff format {ruff_target}")
if mypy_target:
ctx.run(f"uv run mypy {mypy_target} --fixed-format-cache")
if jsonschema_files:
ctx.run(
f"uv run check-jsonschema --schemafile schema/config-schema.json {jsonschema_files}"
)
elif filepath is None:
# Full project lint - run jsonschema on all config files
ctx.run(
"uv run check-jsonschema --schemafile schema/config-schema.json "
"src/agentpool/config_resources/*.yml "
"docs/examples/**/config.yml"
)
@duty(capture=False)
def lint_check(ctx, filepath: str | None = None): # noqa: D417
"""Lint the code (check only, no fixes).
Args:
filepath: Optional path to a specific file to lint.
If not provided, lints the entire project.
"""
ruff_target, mypy_target, jsonschema_files = _get_lint_targets(filepath)
ctx.run(f"uv run ruff check {ruff_target}")
ctx.run(f"uv run ruff format --check {ruff_target}")
if mypy_target:
ctx.run(f"uv run mypy {mypy_target} --fixed-format-cache")
if jsonschema_files:
ctx.run(
f"uv run check-jsonschema --schemafile schema/config-schema.json {jsonschema_files}"
)
elif filepath is None:
# Full project lint - run jsonschema on all config files
ctx.run(
"uv run check-jsonschema --schemafile schema/config-schema.json "
"src/agentpool/config_resources/*.yml "
"docs/examples/**/config.yml"
)
@duty(capture=False)
def version(
ctx,
*bump_type: Literal["major", "minor", "patch", "stable", "alpha", "beta", "rc"],
):
"""Release a new version with git operations. (major|minor|patch|stable|alpha|beta|rc)."""
# Check for uncommitted changes
result = ctx.run("git status --porcelain", capture=True)
if result.strip():
msg = "Cannot release with uncommitted changes. Please commit or stash first."
raise RuntimeError(msg)
# Read current version
old_version = ctx.run("uv version --short", capture=True).strip()
print(f"Current version: {old_version}")
bump_str = " ".join(f"--bump {i}" for i in bump_type)
ctx.run(f"uv version {bump_str}")
new_version = ctx.run("uv version --short", capture=True).strip()
print(f"New version: {new_version}")
ctx.run("uv lock")
# Update extension.toml
ext_toml = Path("distribution/zed/extension.toml")
if ext_toml.exists():
content = ext_toml.read_text()
content = content.replace(old_version, new_version)
ext_toml.write_text(content)
ctx.run("git add distribution/zed/extension.toml")
ctx.run("git add pyproject.toml uv.lock")
ctx.run(f'git commit -m "chore: bump version {old_version} -> {new_version}"')
# Create and push tag
tag = f"v{new_version}"
ctx.run(f"git tag {tag}")
print(f"Created tag: {tag}")
@duty(capture=False)
def smoke_test(ctx, timeout: int = 10): # noqa: D417
"""Build wheel and verify serve-acp starts successfully via uvx.
Builds the package, installs it in an isolated uvx environment,
and checks that serve-acp stays alive for the given timeout.
Exit code 124 from timeout means the process was still running (success).
Any other exit code means it crashed (failure).
Args:
timeout: Seconds to wait before considering the server healthy.
"""
import subprocess
import tempfile
with tempfile.TemporaryDirectory() as tmpdir:
# Build wheel
ctx.run(f"uv build --wheel --out-dir {tmpdir}")
# Find the built wheel
wheels = list(Path(tmpdir).glob("*.whl"))
if not wheels:
msg = "No wheel found after build"
raise RuntimeError(msg)
wheel = wheels[0]
print(f"Built: {wheel.name}")
# Run serve-acp from the wheel in an isolated environment
result = subprocess.run( # noqa: PLW1510
["timeout", str(timeout), "uvx", "--from", str(wheel), "agentpool", "serve-acp"],
capture_output=True,
text=True,
)
if result.returncode == 124: # noqa: PLR2004
print(f"serve-acp stayed alive for {timeout}s (healthy)")
else:
print(f"serve-acp exited with code {result.returncode}")
if result.stdout:
print(f"stdout: {result.stdout}")
if result.stderr:
print(f"stderr: {result.stderr}")
msg = f"serve-acp crashed (exit code {result.returncode})"
raise RuntimeError(msg)
@duty(capture=False)
def schema_html(ctx):
"""Create HTML documentation for JSON schema."""
ctx.run(
"generate-schema-doc "
"--config template_name=js "
"--config expand_buttons=true "
"schema/config-schema.json "
"docs/schema/index.html"
)
@duty(capture=False)
def opencode_server(ctx, *args: str):
"""Start the OpenCode-compatible API server.
Usage:
duty opencode-server # Start on default port 4096
duty opencode-server --port 8080 # Start on custom port
"""
args_str = " " + " ".join(args) if args else ""
ctx.run(f"uv run agentpool serve-opencode{args_str}")