ankor 0.2.1__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.
- ankor/__init__.py +23 -0
- ankor/__main__.py +12 -0
- ankor/agents/claude.yaml +33 -0
- ankor/cli.py +297 -0
- ankor/commands/__init__.py +8 -0
- ankor/commands/agent.py +50 -0
- ankor/commands/check.py +223 -0
- ankor/commands/common.py +80 -0
- ankor/commands/doctor.py +272 -0
- ankor/commands/new.py +511 -0
- ankor/commands/run.py +73 -0
- ankor/commands/shell.py +30 -0
- ankor/commands/task.py +482 -0
- ankor/errors.py +25 -0
- ankor/licenses/README.md +5 -0
- ankor/licenses/apache-2.0.txt +201 -0
- ankor/licenses/gpl-3.0.txt +674 -0
- ankor/licenses/mit.txt +21 -0
- ankor/models/__init__.py +7 -0
- ankor/models/agent.py +41 -0
- ankor/models/pack.py +184 -0
- ankor/models/project_config.py +69 -0
- ankor/options.py +56 -0
- ankor/packs/java/pack.yaml +95 -0
- ankor/packs/java/template/.ankor/prompts/task.md.j2 +10 -0
- ankor/packs/java/template/.claude/commands/check.md +5 -0
- ankor/packs/java/template/.claude/commands/task.md +6 -0
- ankor/packs/java/template/.claude/skills/ankor/SKILL.md +25 -0
- ankor/packs/java/template/.devcontainer/devcontainer.json.j2 +25 -0
- ankor/packs/java/template/.dockerignore +4 -0
- ankor/packs/java/template/.github/ISSUE_TEMPLATE/bug_report.yml +45 -0
- ankor/packs/java/template/.github/ISSUE_TEMPLATE/feature_request.yml +20 -0
- ankor/packs/java/template/.github/dependabot.yml +26 -0
- ankor/packs/java/template/.github/pull_request_template.md +13 -0
- ankor/packs/java/template/.github/workflows/ci.yml +56 -0
- ankor/packs/java/template/.github/workflows/release.yml +65 -0
- ankor/packs/java/template/.gitignore +18 -0
- ankor/packs/java/template/.mvn/wrapper/maven-wrapper.properties +3 -0
- ankor/packs/java/template/.pre-commit-config.yaml +60 -0
- ankor/packs/java/template/AGENTS.md.j2 +91 -0
- ankor/packs/java/template/CLAUDE.md +1 -0
- ankor/packs/java/template/Dockerfile.j2 +55 -0
- ankor/packs/java/template/README.md.j2 +32 -0
- ankor/packs/java/template/cliff.toml +79 -0
- ankor/packs/java/template/compose.yaml.j2 +41 -0
- ankor/packs/java/template/config/checkstyle.xml +319 -0
- ankor/packs/java/template/docs/adr/0001-use-markdown-architectural-decision-records.md.j2 +31 -0
- ankor/packs/java/template/docs/adr/template.md +74 -0
- ankor/packs/java/template/mvnw +295 -0
- ankor/packs/java/template/mvnw.cmd +189 -0
- ankor/packs/java/template/pom.xml.j2 +355 -0
- ankor/packs/java/template/src/main/java/{{ package_path }}/{{ class_name }}.java.j2 +30 -0
- ankor/packs/java/template/src/test/java/{{ package_path }}/{{ class_name }}Test.java.j2 +32 -0
- ankor/packs/python/pack.yaml +63 -0
- ankor/packs/python/template/.ankor/prompts/task.md.j2 +10 -0
- ankor/packs/python/template/.claude/commands/check.md +5 -0
- ankor/packs/python/template/.claude/commands/task.md +6 -0
- ankor/packs/python/template/.claude/skills/ankor/SKILL.md +25 -0
- ankor/packs/python/template/.devcontainer/devcontainer.json.j2 +25 -0
- ankor/packs/python/template/.dockerignore +4 -0
- ankor/packs/python/template/.github/ISSUE_TEMPLATE/bug_report.yml +45 -0
- ankor/packs/python/template/.github/ISSUE_TEMPLATE/feature_request.yml +20 -0
- ankor/packs/python/template/.github/dependabot.yml +26 -0
- ankor/packs/python/template/.github/pull_request_template.md +13 -0
- ankor/packs/python/template/.github/workflows/ci.yml +56 -0
- ankor/packs/python/template/.github/workflows/release.yml +63 -0
- ankor/packs/python/template/.gitignore +29 -0
- ankor/packs/python/template/.pre-commit-config.yaml +60 -0
- ankor/packs/python/template/.python-version +1 -0
- ankor/packs/python/template/AGENTS.md.j2 +79 -0
- ankor/packs/python/template/CLAUDE.md +1 -0
- ankor/packs/python/template/Dockerfile.j2 +67 -0
- ankor/packs/python/template/README.md.j2 +33 -0
- ankor/packs/python/template/cliff.toml +79 -0
- ankor/packs/python/template/compose.yaml.j2 +44 -0
- ankor/packs/python/template/docs/adr/0001-use-markdown-architectural-decision-records.md.j2 +31 -0
- ankor/packs/python/template/docs/adr/template.md +74 -0
- ankor/packs/python/template/pyproject.toml.j2 +51 -0
- ankor/packs/python/template/src/{{ module_name }}/__init__.py.j2 +16 -0
- ankor/packs/python/template/tests/test_{{ module_name }}.py.j2 +15 -0
- ankor/services/__init__.py +7 -0
- ankor/services/agent_settings.py +120 -0
- ankor/services/agents.py +115 -0
- ankor/services/config_files.py +76 -0
- ankor/services/docker.py +98 -0
- ankor/services/git.py +147 -0
- ankor/services/github.py +138 -0
- ankor/services/hooks.py +26 -0
- ankor/services/host_user.py +61 -0
- ankor/services/licenses.py +26 -0
- ankor/services/logs.py +79 -0
- ankor/services/output.py +138 -0
- ankor/services/packs.py +122 -0
- ankor/services/process.py +120 -0
- ankor/services/prompts.py +35 -0
- ankor/services/protected_files.py +179 -0
- ankor/services/templates.py +114 -0
- ankor-0.2.1.dist-info/METADATA +123 -0
- ankor-0.2.1.dist-info/RECORD +103 -0
- ankor-0.2.1.dist-info/WHEEL +4 -0
- ankor-0.2.1.dist-info/entry_points.txt +2 -0
- ankor-0.2.1.dist-info/licenses/LICENSE +202 -0
- ankor-0.2.1.dist-info/licenses/NOTICE +5 -0
ankor/__init__.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""Ankor — a software engineering harness.
|
|
2
|
+
|
|
3
|
+
Creates software projects with engineering practices in place and lets AI coding
|
|
4
|
+
agents work within them.
|
|
5
|
+
|
|
6
|
+
This file makes ``src/ankor/`` a Python package and exposes the installed version
|
|
7
|
+
as ``ankor.__version__``.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
11
|
+
|
|
12
|
+
# The version is not written anywhere in the code. When the package is built,
|
|
13
|
+
# hatch-vcs derives it from the latest Git tag (ADR-0049) and records it in the
|
|
14
|
+
# package metadata; `importlib.metadata.version` reads it back from there.
|
|
15
|
+
try:
|
|
16
|
+
__version__ = version("ankor")
|
|
17
|
+
except PackageNotFoundError:
|
|
18
|
+
# The source tree is being used without the package being installed
|
|
19
|
+
# (for example, a file copied elsewhere), so no metadata exists.
|
|
20
|
+
__version__ = "0.0.0"
|
|
21
|
+
|
|
22
|
+
# Names exported by `from ankor import *`.
|
|
23
|
+
__all__ = ["__version__"]
|
ankor/__main__.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""Allow running Ankor with ``python -m ankor``.
|
|
2
|
+
|
|
3
|
+
When Python is started with ``-m ankor``, it runs this file. It does the same as the
|
|
4
|
+
``ankor`` command created from ``[project.scripts]`` in ``pyproject.toml``.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from ankor.cli import main
|
|
8
|
+
|
|
9
|
+
# `__name__` is "__main__" only when this file is executed directly (as with
|
|
10
|
+
# `python -m ankor`), not when it is imported by another module.
|
|
11
|
+
if __name__ == "__main__":
|
|
12
|
+
main()
|
ankor/agents/claude.yaml
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Adapter for Claude Code, the agent Ankor implements and tests (ADR-0053).
|
|
2
|
+
# Format: the AgentAdapter model in src/ankor/models/agent.py.
|
|
3
|
+
# Reference: https://code.claude.com/docs/en/cli-reference
|
|
4
|
+
name: claude
|
|
5
|
+
description: Claude Code, run in headless (print) mode inside the project's container
|
|
6
|
+
|
|
7
|
+
# -p (print mode) runs one request without a person and prints the result;
|
|
8
|
+
# --output-format json gives a single JSON object with the session id, the cost
|
|
9
|
+
# and the number of turns; --max-turns bounds one attempt (ADR-0068);
|
|
10
|
+
# --settings loads the permissions and hooks Ankor writes for the task (ADR-0057);
|
|
11
|
+
# --permission-mode acceptEdits lets it edit files without asking, within those
|
|
12
|
+
# permissions (in print mode nobody could answer a question).
|
|
13
|
+
command:
|
|
14
|
+
- claude
|
|
15
|
+
- -p
|
|
16
|
+
- "{prompt}"
|
|
17
|
+
- --output-format
|
|
18
|
+
- json
|
|
19
|
+
- --max-turns
|
|
20
|
+
- "{max_turns}"
|
|
21
|
+
- --settings
|
|
22
|
+
- "{settings}"
|
|
23
|
+
- --permission-mode
|
|
24
|
+
- acceptEdits
|
|
25
|
+
|
|
26
|
+
# Added to `command` for a follow-up attempt in the same session (ADR-0054).
|
|
27
|
+
resume: [--resume, "{session_id}"]
|
|
28
|
+
|
|
29
|
+
# `ankor agent login`: an interactive login, stored in the agent-auth volume (ADR-0056).
|
|
30
|
+
login: [claude, auth, login]
|
|
31
|
+
status: [claude, auth, status, --text]
|
|
32
|
+
|
|
33
|
+
output: claude-json
|
ankor/cli.py
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
"""Command-line entry point.
|
|
2
|
+
|
|
3
|
+
This module is the "front desk" of Ankor: it turns the words typed in the terminal
|
|
4
|
+
into Python values and hands them to the right function in :mod:`ankor.commands`.
|
|
5
|
+
It contains no business logic (ADR-0008).
|
|
6
|
+
|
|
7
|
+
How a command reaches this file:
|
|
8
|
+
|
|
9
|
+
1. the shell finds the ``ankor`` executable in ``PATH`` (created from
|
|
10
|
+
``[project.scripts]`` in ``pyproject.toml``);
|
|
11
|
+
2. that executable starts Python and calls :func:`main`;
|
|
12
|
+
3. :func:`main` calls the Click group :func:`cli`, which reads ``sys.argv`` (the list
|
|
13
|
+
of words typed, e.g. ``["run", "--json", "--", "12"]``), decides which
|
|
14
|
+
subcommand was requested, converts the options and calls the matching function.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
import sys
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
|
|
20
|
+
import click
|
|
21
|
+
|
|
22
|
+
from ankor import __version__
|
|
23
|
+
|
|
24
|
+
# Imported under another name so it does not clash with the `run` function below:
|
|
25
|
+
# `run` is the terminal-facing function, `run_command.run` does the actual work.
|
|
26
|
+
from ankor.commands import agent as agent_command
|
|
27
|
+
from ankor.commands import check as check_command
|
|
28
|
+
from ankor.commands import doctor as doctor_command
|
|
29
|
+
from ankor.commands import new as new_command
|
|
30
|
+
from ankor.commands import run as run_command
|
|
31
|
+
from ankor.commands import shell as shell_command
|
|
32
|
+
from ankor.commands import task as task_command
|
|
33
|
+
from ankor.errors import AnkorError
|
|
34
|
+
from ankor.options import GlobalOptions, common_options
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
# `@click.group` turns `cli` into a command that has subcommands (`ankor run`,
|
|
38
|
+
# `ankor shell`, …). Its docstring is the text shown by `ankor --help`.
|
|
39
|
+
# `help_option_names` makes `-h` work as well as `--help`.
|
|
40
|
+
@click.group(context_settings={"help_option_names": ["-h", "--help"]})
|
|
41
|
+
# Adds `--version` / `-V`, which prints e.g. "ankor, version 0.1.0" and exits.
|
|
42
|
+
@click.version_option(__version__, "--version", "-V", prog_name="ankor")
|
|
43
|
+
def cli() -> None:
|
|
44
|
+
"""Create and verify software projects, and orchestrate coding agents."""
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def split_tools(
|
|
48
|
+
context: click.Context, parameter: click.Parameter, value: str | None
|
|
49
|
+
) -> tuple[str, ...] | None:
|
|
50
|
+
"""Click callback: turn ``--analysis "a, b"`` into ``("a", "b")``."""
|
|
51
|
+
if value is None:
|
|
52
|
+
return None
|
|
53
|
+
return tuple(tool.strip() for tool in value.split(",") if tool.strip())
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@cli.command()
|
|
57
|
+
@common_options
|
|
58
|
+
@click.argument("name")
|
|
59
|
+
@click.option("--lang", help="Language pack, for example java.")
|
|
60
|
+
@click.option("--package", help="Java package (default derived from NAME).")
|
|
61
|
+
@click.option(
|
|
62
|
+
"--license",
|
|
63
|
+
"license_", # `license` is a Python built-in, so the parameter gets another name
|
|
64
|
+
type=click.Choice(["mit", "apache-2.0", "gpl-3.0", "none"]),
|
|
65
|
+
help="Project licence (default: none).",
|
|
66
|
+
)
|
|
67
|
+
@click.option("--analysis-profile", help="Static analysis profile: basic, recommended or strict.")
|
|
68
|
+
@click.option(
|
|
69
|
+
"--analysis",
|
|
70
|
+
"analysis_tools",
|
|
71
|
+
callback=split_tools,
|
|
72
|
+
help="Comma-separated analysis tools, instead of a profile.",
|
|
73
|
+
)
|
|
74
|
+
# `IntRange` rejects values outside 0-100 before the command runs.
|
|
75
|
+
@click.option("--coverage-lines", type=click.IntRange(0, 100), help="Minimum line coverage (%).")
|
|
76
|
+
@click.option(
|
|
77
|
+
"--coverage-branches", type=click.IntRange(0, 100), help="Minimum branch coverage (%)."
|
|
78
|
+
)
|
|
79
|
+
@click.option("--no-input", is_flag=True, help="Never ask; use defaults for missing values.")
|
|
80
|
+
@click.option("--no-git", is_flag=True, help="Do not create a Git repository.")
|
|
81
|
+
@click.option("--no-docker", is_flag=True, help="Do not build the image (and skip the check).")
|
|
82
|
+
@click.option("--skip-check", is_flag=True, help="Do not run the first ankor check.")
|
|
83
|
+
@click.option(
|
|
84
|
+
"--github", is_flag=True, help="Create a private GitHub repository, push and protect main."
|
|
85
|
+
)
|
|
86
|
+
@click.option("--public", is_flag=True, help="With --github: make the repository public.")
|
|
87
|
+
@click.option(
|
|
88
|
+
"--describe",
|
|
89
|
+
metavar="TEXT",
|
|
90
|
+
help="What the program should do: the project's agent writes it after the first check.",
|
|
91
|
+
)
|
|
92
|
+
def new(
|
|
93
|
+
json_output: bool,
|
|
94
|
+
verbose: bool,
|
|
95
|
+
debug: bool,
|
|
96
|
+
name: str,
|
|
97
|
+
lang: str | None,
|
|
98
|
+
package: str | None,
|
|
99
|
+
license_: str | None,
|
|
100
|
+
analysis_profile: str | None,
|
|
101
|
+
analysis_tools: tuple[str, ...] | None,
|
|
102
|
+
coverage_lines: int | None,
|
|
103
|
+
coverage_branches: int | None,
|
|
104
|
+
no_input: bool,
|
|
105
|
+
no_git: bool,
|
|
106
|
+
no_docker: bool,
|
|
107
|
+
skip_check: bool,
|
|
108
|
+
github: bool,
|
|
109
|
+
public: bool,
|
|
110
|
+
describe: str | None,
|
|
111
|
+
) -> None:
|
|
112
|
+
"""Create a new project called NAME in the current folder.
|
|
113
|
+
|
|
114
|
+
Missing values are asked for, unless --no-input is given, for example:
|
|
115
|
+
|
|
116
|
+
ankor new notas --lang java --no-input
|
|
117
|
+
"""
|
|
118
|
+
options = GlobalOptions(json_output=json_output, verbose=verbose, debug=debug)
|
|
119
|
+
request = new_command.NewRequest(
|
|
120
|
+
name=name,
|
|
121
|
+
lang=lang,
|
|
122
|
+
package=package,
|
|
123
|
+
license=license_,
|
|
124
|
+
analysis_profile=analysis_profile,
|
|
125
|
+
analysis_tools=analysis_tools,
|
|
126
|
+
coverage_lines=coverage_lines,
|
|
127
|
+
coverage_branches=coverage_branches,
|
|
128
|
+
# Without a terminal (CI, an agent) nobody can answer, so never ask.
|
|
129
|
+
no_input=no_input or not sys.stdin.isatty(),
|
|
130
|
+
no_git=no_git,
|
|
131
|
+
no_docker=no_docker,
|
|
132
|
+
skip_check=skip_check,
|
|
133
|
+
github=github,
|
|
134
|
+
public=public,
|
|
135
|
+
describe=describe,
|
|
136
|
+
interactive=sys.stdin.isatty(),
|
|
137
|
+
)
|
|
138
|
+
raise SystemExit(new_command.run(options, request))
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
@cli.command()
|
|
142
|
+
@common_options
|
|
143
|
+
@click.option("--fast", is_flag=True, help="Run only the fast steps (formatting, conventions).")
|
|
144
|
+
@click.option("--step", "step_name", metavar="NAME", help="Run only the step called NAME.")
|
|
145
|
+
def check(json_output: bool, verbose: bool, debug: bool, fast: bool, step_name: str | None) -> None:
|
|
146
|
+
"""Verify the project: formatting, analysis, build, tests and coverage.
|
|
147
|
+
|
|
148
|
+
Each step runs inside the project's container. Steps that depend on a failed
|
|
149
|
+
step are skipped. The exit code is 0 only when every step passed.
|
|
150
|
+
"""
|
|
151
|
+
options = GlobalOptions(json_output=json_output, verbose=verbose, debug=debug)
|
|
152
|
+
request = check_command.CheckRequest(fast=fast, step=step_name)
|
|
153
|
+
raise SystemExit(check_command.run(options, request))
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
# `@cli.command` registers `run` as a subcommand of the `cli` group.
|
|
157
|
+
# `ignore_unknown_options` stops Click from rejecting options meant for the program.
|
|
158
|
+
@cli.command(context_settings={"ignore_unknown_options": True})
|
|
159
|
+
@common_options
|
|
160
|
+
# `nargs=-1` accepts any number of arguments; `UNPROCESSED` passes them through
|
|
161
|
+
# untouched. Together they let `ankor run -- 12 15 18` forward "12 15 18" to the
|
|
162
|
+
# program. `--` is the Unix convention for "what follows are not my options".
|
|
163
|
+
@click.argument("args", nargs=-1, type=click.UNPROCESSED)
|
|
164
|
+
def run(json_output: bool, verbose: bool, debug: bool, args: tuple[str, ...]) -> None:
|
|
165
|
+
"""Build and run the project's program inside its container.
|
|
166
|
+
|
|
167
|
+
Arguments after -- are passed to the program, for example:
|
|
168
|
+
|
|
169
|
+
ankor run -- 12 15 18
|
|
170
|
+
"""
|
|
171
|
+
# Click passes each option as a parameter with the same name. They are bundled
|
|
172
|
+
# into one object so commands receive a single, typed value.
|
|
173
|
+
options = GlobalOptions(json_output=json_output, verbose=verbose, debug=debug)
|
|
174
|
+
# The keyboard is attached to the program only when a person is at a terminal;
|
|
175
|
+
# in CI or a pipe, Docker would refuse to allocate one.
|
|
176
|
+
request = run_command.RunRequest(args=args, interactive=sys.stdin.isatty())
|
|
177
|
+
# `SystemExit(code)` ends the process with the program's exit code (0 = success),
|
|
178
|
+
# which matters for scripts, hooks and CI.
|
|
179
|
+
raise SystemExit(run_command.run(options, request))
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
@cli.command()
|
|
183
|
+
@common_options
|
|
184
|
+
def shell(json_output: bool, verbose: bool, debug: bool) -> None:
|
|
185
|
+
"""Open a shell inside the project's container (leave it with `exit`)."""
|
|
186
|
+
options = GlobalOptions(json_output=json_output, verbose=verbose, debug=debug)
|
|
187
|
+
raise SystemExit(shell_command.run(options))
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
@cli.command()
|
|
191
|
+
@common_options
|
|
192
|
+
@click.argument("description", required=False)
|
|
193
|
+
@click.option(
|
|
194
|
+
"--file",
|
|
195
|
+
"file",
|
|
196
|
+
type=click.Path(exists=True, path_type=Path),
|
|
197
|
+
help="Read the task from a file.",
|
|
198
|
+
)
|
|
199
|
+
@click.option("--issue", type=int, help="Read the task from GitHub issue N.")
|
|
200
|
+
@click.option("--agent", "agent_name", help="Agent adapter (default: from .ankor.yaml).")
|
|
201
|
+
@click.option("--max-attempts", type=click.IntRange(1, 20), help="Attempts before giving up.")
|
|
202
|
+
@click.option("--branch", help="Work on this existing branch instead of a new one.")
|
|
203
|
+
def task(
|
|
204
|
+
json_output: bool,
|
|
205
|
+
verbose: bool,
|
|
206
|
+
debug: bool,
|
|
207
|
+
description: str | None,
|
|
208
|
+
file: Path | None,
|
|
209
|
+
issue: int | None,
|
|
210
|
+
agent_name: str | None,
|
|
211
|
+
max_attempts: int | None,
|
|
212
|
+
branch: str | None,
|
|
213
|
+
) -> None:
|
|
214
|
+
"""Let the project's agent do a task, verify it with ankor check and keep the result.
|
|
215
|
+
|
|
216
|
+
For example:
|
|
217
|
+
|
|
218
|
+
ankor task "Add a command that prints the average of the numbers given"
|
|
219
|
+
"""
|
|
220
|
+
options = GlobalOptions(json_output=json_output, verbose=verbose, debug=debug)
|
|
221
|
+
request = task_command.TaskRequest(
|
|
222
|
+
description=description,
|
|
223
|
+
file=file,
|
|
224
|
+
issue=issue,
|
|
225
|
+
agent=agent_name,
|
|
226
|
+
max_attempts=max_attempts,
|
|
227
|
+
branch=branch,
|
|
228
|
+
interactive=sys.stdin.isatty(),
|
|
229
|
+
)
|
|
230
|
+
raise SystemExit(task_command.run(options, request))
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
@cli.command()
|
|
234
|
+
@common_options
|
|
235
|
+
@click.option(
|
|
236
|
+
"--agent",
|
|
237
|
+
"check_agent",
|
|
238
|
+
is_flag=True,
|
|
239
|
+
help="Also check the agent's login (starts the container).",
|
|
240
|
+
)
|
|
241
|
+
def doctor(json_output: bool, verbose: bool, debug: bool, check_agent: bool) -> None:
|
|
242
|
+
"""Check what Ankor needs (Docker, Git, the project) and how to fix it."""
|
|
243
|
+
options = GlobalOptions(json_output=json_output, verbose=verbose, debug=debug)
|
|
244
|
+
raise SystemExit(doctor_command.run(options, check_agent=check_agent))
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
@cli.group()
|
|
248
|
+
def agent() -> None:
|
|
249
|
+
"""Log the project's coding agent in and check its login."""
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
@agent.command("login")
|
|
253
|
+
@common_options
|
|
254
|
+
def agent_login(json_output: bool, verbose: bool, debug: bool) -> None:
|
|
255
|
+
"""Log the agent in inside the project's container (once per project)."""
|
|
256
|
+
options = GlobalOptions(json_output=json_output, verbose=verbose, debug=debug)
|
|
257
|
+
raise SystemExit(agent_command.login(options))
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
@agent.command("status")
|
|
261
|
+
@common_options
|
|
262
|
+
def agent_status(json_output: bool, verbose: bool, debug: bool) -> None:
|
|
263
|
+
"""Show whether the agent is logged in inside the project's container."""
|
|
264
|
+
options = GlobalOptions(json_output=json_output, verbose=verbose, debug=debug)
|
|
265
|
+
raise SystemExit(agent_command.status(options))
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def debug_requested(words: list[str]) -> bool:
|
|
269
|
+
"""Whether ``--debug`` was typed as an Ankor option.
|
|
270
|
+
|
|
271
|
+
``main`` cannot use the value Click parsed (that happened inside ``cli()``), so
|
|
272
|
+
it looks at the raw words. Words after ``--`` belong to the user's program
|
|
273
|
+
(``ankor run -- --debug``) and are ignored.
|
|
274
|
+
"""
|
|
275
|
+
if "--" in words:
|
|
276
|
+
words = words[: words.index("--")] # keep only the words before "--"
|
|
277
|
+
return "--debug" in words
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def main() -> None:
|
|
281
|
+
"""Run the command-line interface (called by the ``ankor`` executable)."""
|
|
282
|
+
try:
|
|
283
|
+
cli()
|
|
284
|
+
except AnkorError as error:
|
|
285
|
+
if debug_requested(sys.argv[1:]):
|
|
286
|
+
raise # `raise` alone re-raises the error: Python prints the full traceback
|
|
287
|
+
# An expected failure the user can fix (ADR-0051): print the message and the
|
|
288
|
+
# hint on stderr and exit with code 1, without a Python traceback.
|
|
289
|
+
click.echo(f"Error: {error.message}", err=True)
|
|
290
|
+
if error.hint:
|
|
291
|
+
click.echo(f" → {error.hint}", err=True)
|
|
292
|
+
raise SystemExit(1) from error
|
|
293
|
+
except NotImplementedError as error:
|
|
294
|
+
# Commands that are not implemented yet raise NotImplementedError; show a
|
|
295
|
+
# short message on stderr (`err=True`) instead of a Python traceback.
|
|
296
|
+
click.echo(f"Not implemented yet: {error}", err=True)
|
|
297
|
+
raise SystemExit(2) from error
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"""Command implementations, one module per command (ADR-0008).
|
|
2
|
+
|
|
3
|
+
Each module here contains what a command actually does (for example,
|
|
4
|
+
``commands/run.py`` runs the project's program). Commands never talk to external
|
|
5
|
+
tools directly; they use :mod:`ankor.services`, so they can be tested with
|
|
6
|
+
mocked services. They also know nothing about the terminal: :mod:`ankor.cli`
|
|
7
|
+
parses what the user typed and calls these functions with ready-made values.
|
|
8
|
+
"""
|
ankor/commands/agent.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"""``ankor agent login`` and ``ankor agent status`` (ADR-0056).
|
|
2
|
+
|
|
3
|
+
The agent logs in **inside the project's container**: its credentials are kept
|
|
4
|
+
in the project's ``agent-auth`` Docker volume, never in the person's own
|
|
5
|
+
``~/.claude``. Logging in once per project is enough; ``ankor task`` reuses it.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from ankor.commands.common import open_project
|
|
11
|
+
from ankor.errors import AnkorError
|
|
12
|
+
from ankor.options import GlobalOptions
|
|
13
|
+
from ankor.services import docker
|
|
14
|
+
from ankor.services.agents import load_adapter
|
|
15
|
+
from ankor.services.output import Output
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def login(options: GlobalOptions, start: Path = Path(), output: Output | None = None) -> int:
|
|
19
|
+
"""Run the agent's interactive login in the project's container."""
|
|
20
|
+
output = output or Output(options)
|
|
21
|
+
try:
|
|
22
|
+
project = open_project(start)
|
|
23
|
+
adapter = load_adapter(project.config.agent.name)
|
|
24
|
+
output.info(f"Logging in to {adapter.description}; follow the instructions below.")
|
|
25
|
+
result = docker.compose_run(project.path, adapter.login, capture=False, interactive=True)
|
|
26
|
+
except AnkorError as error:
|
|
27
|
+
output.error(error)
|
|
28
|
+
output.finish(ok=False)
|
|
29
|
+
return 1
|
|
30
|
+
output.finish(ok=result.ok)
|
|
31
|
+
return result.returncode
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def status(options: GlobalOptions, start: Path = Path(), output: Output | None = None) -> int:
|
|
35
|
+
"""Show whether the agent is logged in inside the project's container."""
|
|
36
|
+
output = output or Output(options)
|
|
37
|
+
try:
|
|
38
|
+
project = open_project(start)
|
|
39
|
+
adapter = load_adapter(project.config.agent.name)
|
|
40
|
+
result = docker.compose_run(project.path, adapter.status)
|
|
41
|
+
except AnkorError as error:
|
|
42
|
+
output.error(error)
|
|
43
|
+
output.finish(ok=False)
|
|
44
|
+
return 1
|
|
45
|
+
text = (result.stdout or result.stderr).strip()
|
|
46
|
+
output.step("agent login", "ok" if result.ok else "failed", details=text)
|
|
47
|
+
if not result.ok:
|
|
48
|
+
output.info("Log in with `ankor agent login`.")
|
|
49
|
+
output.finish(ok=result.ok, result={"agent": adapter.name, "status": text})
|
|
50
|
+
return 0 if result.ok else 1
|
ankor/commands/check.py
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
"""``ankor check``: verify a project by running its pack's steps (ADR-0044).
|
|
2
|
+
|
|
3
|
+
The steps (format, analysis, build, tests, coverage…) are declared in the pack's
|
|
4
|
+
``pack.yaml``. ``ankor check``:
|
|
5
|
+
|
|
6
|
+
1. finds the project (the nearest folder, going up, that has an ``.ankor.yaml``);
|
|
7
|
+
2. picks the steps to run: only those whose analysis tool is enabled in
|
|
8
|
+
``.ankor.yaml``, and only the fast ones with ``--fast`` or one with ``--step``;
|
|
9
|
+
3. runs them in an order that respects their ``needs``, each inside the
|
|
10
|
+
project's container;
|
|
11
|
+
4. marks a step as failed when its command fails, or when its output matches the
|
|
12
|
+
step's ``fail_if_output_matches`` (for tools that report errors but exit with
|
|
13
|
+
code 0, such as LiquidJava);
|
|
14
|
+
5. reports steps that depend on a failed step as ``skipped``, without running them.
|
|
15
|
+
|
|
16
|
+
The exit code is 0 when every step that ran passed and 1 otherwise, so hooks and
|
|
17
|
+
CI can rely on it. With ``--json`` the result is a single JSON document with the
|
|
18
|
+
status, duration and errors (file, line and message, when they can be read from
|
|
19
|
+
the output) of each step.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
import logging
|
|
23
|
+
import re
|
|
24
|
+
from dataclasses import dataclass
|
|
25
|
+
from pathlib import Path
|
|
26
|
+
from typing import Any
|
|
27
|
+
|
|
28
|
+
from ankor.commands.common import Project, fill_placeholders, open_project
|
|
29
|
+
from ankor.errors import AnkorError
|
|
30
|
+
from ankor.models.pack import Pack, Step
|
|
31
|
+
from ankor.models.project_config import ProjectConfig
|
|
32
|
+
from ankor.options import GlobalOptions
|
|
33
|
+
from ankor.services import docker
|
|
34
|
+
from ankor.services.logs import configure_logging
|
|
35
|
+
from ankor.services.output import Output
|
|
36
|
+
|
|
37
|
+
logger = logging.getLogger(__name__)
|
|
38
|
+
|
|
39
|
+
# Error lines Maven tools print, turned into file/line/message (ADR-0044):
|
|
40
|
+
# compiler, Error Prone, NullAway, Checker Framework:
|
|
41
|
+
# [ERROR] /workspace/src/main/java/a/B.java:[5,21] message
|
|
42
|
+
# Checkstyle:
|
|
43
|
+
# [WARN] /workspace/src/main/java/a/B.java:9:20: message [RuleName]
|
|
44
|
+
# Python tools (ruff, mypy, bandit, pytest tracebacks), with or without column:
|
|
45
|
+
# src/a/b.py:12:5: F401 `os` imported but unused
|
|
46
|
+
# /workspace/src/a/b.py:12: error: Incompatible return value type
|
|
47
|
+
ERROR_PATTERNS = [
|
|
48
|
+
re.compile(r"\[ERROR\] /workspace/(?P<file>[^:\[]+):\[(?P<line>\d+),\d+\] (?P<message>.+)"),
|
|
49
|
+
re.compile(r"\[WARN\] /workspace/(?P<file>[^:\[]+):(?P<line>\d+):\d+: (?P<message>.+)"),
|
|
50
|
+
re.compile(r"^(?:/workspace/)?(?P<file>[\w./-]+\.py):(?P<line>\d+):(?:\d+:)? (?P<message>.+)"),
|
|
51
|
+
]
|
|
52
|
+
# Colour codes some tools print even without a terminal (LiquidJava does).
|
|
53
|
+
ANSI_COLOUR = re.compile(r"\x1b\[[0-9;]*m")
|
|
54
|
+
# LiquidJava prints the message and, a few lines below, where it happened:
|
|
55
|
+
# Refinement Error: found `a == -1` but expected `a > 0`
|
|
56
|
+
# ...
|
|
57
|
+
# /workspace/src/main/java/a/B.java:12
|
|
58
|
+
LIQUIDJAVA_MESSAGE = re.compile(r"^(?P<message>(State )?Refinement Error: .+)$")
|
|
59
|
+
LIQUIDJAVA_PLACE = re.compile(r"^/workspace/(?P<file>\S+\.java):(?P<line>\d+)$")
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@dataclass(frozen=True)
|
|
63
|
+
class CheckRequest:
|
|
64
|
+
"""What was typed on the command line."""
|
|
65
|
+
|
|
66
|
+
fast: bool = False
|
|
67
|
+
step: str | None = None
|
|
68
|
+
start: Path = Path() # folder from which the project is searched for
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def select_steps(pack: Pack, config: ProjectConfig, request: CheckRequest) -> list[Step]:
|
|
72
|
+
"""The steps to run, in the pack's order, according to the tools and options."""
|
|
73
|
+
enabled = set(config.analysis.tools) if config.analysis else set()
|
|
74
|
+
steps = [step for step in pack.steps if step.tool is None or step.tool in enabled]
|
|
75
|
+
|
|
76
|
+
if request.step is not None:
|
|
77
|
+
names = [step.name for step in steps]
|
|
78
|
+
if request.step not in names:
|
|
79
|
+
raise AnkorError(
|
|
80
|
+
f"Unknown or disabled step '{request.step}'.",
|
|
81
|
+
hint=f"Steps of this project: {', '.join(names)}.",
|
|
82
|
+
)
|
|
83
|
+
return [step for step in steps if step.name == request.step]
|
|
84
|
+
if request.fast:
|
|
85
|
+
return [step for step in steps if step.fast]
|
|
86
|
+
return steps
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def in_dependency_order(steps: list[Step]) -> list[Step]:
|
|
90
|
+
"""Order ``steps`` so that each comes after the steps it needs.
|
|
91
|
+
|
|
92
|
+
The pack's order is kept wherever ``needs`` allows it. Dependencies on steps
|
|
93
|
+
that are not being run (for example with ``--fast``) are ignored. ``pack.yaml``
|
|
94
|
+
is validated to have no cycles, so this always terminates.
|
|
95
|
+
"""
|
|
96
|
+
by_name = {step.name: step for step in steps}
|
|
97
|
+
ordered: list[Step] = []
|
|
98
|
+
placed: set[str] = set()
|
|
99
|
+
|
|
100
|
+
def place(step: Step) -> None:
|
|
101
|
+
if step.name in placed:
|
|
102
|
+
return
|
|
103
|
+
for name in step.needs:
|
|
104
|
+
if name in by_name: # only dependencies that are part of this run
|
|
105
|
+
place(by_name[name])
|
|
106
|
+
placed.add(step.name)
|
|
107
|
+
ordered.append(step)
|
|
108
|
+
|
|
109
|
+
for step in steps:
|
|
110
|
+
place(step)
|
|
111
|
+
return ordered
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def extract_errors(output: str) -> list[dict[str, Any]]:
|
|
115
|
+
"""File, line and message of each error recognised in a step's output."""
|
|
116
|
+
errors: list[dict[str, Any]] = []
|
|
117
|
+
seen: set[tuple[str, int, str]] = set()
|
|
118
|
+
|
|
119
|
+
def add(file: str, line: int, message: str) -> None:
|
|
120
|
+
key = (file, line, message.strip())
|
|
121
|
+
if key not in seen: # Maven often prints the same error twice
|
|
122
|
+
seen.add(key)
|
|
123
|
+
errors.append({"file": key[0], "line": key[1], "message": key[2]})
|
|
124
|
+
|
|
125
|
+
pending: str | None = None # a LiquidJava message waiting for its location
|
|
126
|
+
for line in strip_colours(output).splitlines():
|
|
127
|
+
line = line.strip()
|
|
128
|
+
for pattern in ERROR_PATTERNS:
|
|
129
|
+
match = pattern.search(line)
|
|
130
|
+
if match:
|
|
131
|
+
add(match["file"], int(match["line"]), match["message"])
|
|
132
|
+
break
|
|
133
|
+
if message := LIQUIDJAVA_MESSAGE.match(line):
|
|
134
|
+
pending = message["message"]
|
|
135
|
+
elif pending and (place := LIQUIDJAVA_PLACE.match(line)):
|
|
136
|
+
add(place["file"], int(place["line"]), pending)
|
|
137
|
+
pending = None # later locations ("declared here") belong to the same error
|
|
138
|
+
return errors
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def strip_colours(text: str) -> str:
|
|
142
|
+
"""Remove terminal colour codes such as ``\\x1b[1;31m``."""
|
|
143
|
+
return ANSI_COLOUR.sub("", text)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def step_failed(step: Step, returncode: int, output: str) -> bool:
|
|
147
|
+
"""Whether a step failed: a non-zero exit code, or output matching its pattern."""
|
|
148
|
+
if returncode != 0:
|
|
149
|
+
return True
|
|
150
|
+
return bool(step.fail_if_output_matches and re.search(step.fail_if_output_matches, output))
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
# Lines Maven adds to every failure that never explain it, plus its progress lines.
|
|
154
|
+
MAVEN_NOISE = re.compile(
|
|
155
|
+
r"^\[INFO\]|^\[ERROR\]\s*$|To see the full stack trace|Re-run Maven using|"
|
|
156
|
+
r"For more information about the errors|\[Help \d+\]|^\[ERROR\] -> "
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def summary(text: str, count: int = 5) -> str:
|
|
161
|
+
"""The last ``count`` meaningful lines of a failed step's output.
|
|
162
|
+
|
|
163
|
+
Tools print their conclusion near the end ("Rule violated for bundle…",
|
|
164
|
+
"Coverage checks have not been met"), followed by generic Maven help lines,
|
|
165
|
+
which are left out.
|
|
166
|
+
"""
|
|
167
|
+
lines = [line.strip() for line in text.splitlines()]
|
|
168
|
+
useful = [line for line in lines if line and not MAVEN_NOISE.search(line)]
|
|
169
|
+
return "\n".join(useful[-count:])
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def run_steps(project: Project, steps: list[Step], output: Output) -> bool:
|
|
173
|
+
"""Run ``steps`` in the project's container, reporting each one; return whether all passed.
|
|
174
|
+
|
|
175
|
+
Used by ``ankor check`` and by ``ankor new``'s final check. Raises
|
|
176
|
+
:class:`AnkorError` when Docker is missing or not running.
|
|
177
|
+
"""
|
|
178
|
+
failed: set[str] = set() # names of steps that failed or were skipped
|
|
179
|
+
for step in steps:
|
|
180
|
+
blocking = [name for name in step.needs if name in failed]
|
|
181
|
+
if blocking:
|
|
182
|
+
failed.add(step.name)
|
|
183
|
+
output.step(step.name, "skipped", details=f"needs {', '.join(blocking)}")
|
|
184
|
+
continue
|
|
185
|
+
|
|
186
|
+
command = fill_placeholders(step.command, project.config)
|
|
187
|
+
logger.info("step %s: %s", step.name, " ".join(command))
|
|
188
|
+
result = docker.compose_run(project.path, command)
|
|
189
|
+
text = strip_colours(result.stdout + result.stderr)
|
|
190
|
+
if step_failed(step, result.returncode, text):
|
|
191
|
+
failed.add(step.name)
|
|
192
|
+
errors = extract_errors(text)
|
|
193
|
+
output.step(
|
|
194
|
+
step.name,
|
|
195
|
+
"failed",
|
|
196
|
+
duration=result.duration,
|
|
197
|
+
details="" if errors else summary(text),
|
|
198
|
+
errors=errors,
|
|
199
|
+
)
|
|
200
|
+
else:
|
|
201
|
+
output.step(step.name, "ok", duration=result.duration)
|
|
202
|
+
return not failed
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def run(options: GlobalOptions, request: CheckRequest, output: Output | None = None) -> int:
|
|
206
|
+
"""Run the checks of the project around ``request.start``; return the exit code."""
|
|
207
|
+
output = output or Output(options)
|
|
208
|
+
try:
|
|
209
|
+
project = open_project(request.start)
|
|
210
|
+
log_file = configure_logging(
|
|
211
|
+
options, log_dir=project.path / ".ankor" / "logs", command="check"
|
|
212
|
+
)
|
|
213
|
+
steps = in_dependency_order(select_steps(project.pack, project.config, request))
|
|
214
|
+
ok = run_steps(project, steps, output)
|
|
215
|
+
except AnkorError as error:
|
|
216
|
+
output.error(error)
|
|
217
|
+
output.finish(ok=False)
|
|
218
|
+
return 1
|
|
219
|
+
|
|
220
|
+
if not ok and log_file is not None:
|
|
221
|
+
output.info(f"Full output of every step: {log_file}")
|
|
222
|
+
output.finish(ok=ok, result={"project": str(project.path), "log": str(log_file)})
|
|
223
|
+
return 0 if ok else 1
|