Writing an extension#
An extension adds a repository’s own commands to graftkit. Every extension has the same
shape, so a command in one repository looks like a command in any other, and the core is built
the same way.
Quick start#
In a repository with a graftkit.toml and a Python project (pyproject.toml):
graftkit extension new # asks for the name and package; --yes takes the defaults
uv sync
graftkit hello --name Ada # the example command
uv run pytest tests/graftkit # its test and the conformance test
extension new creates the layout below, registers the entry point, adds graftkit~=<running version> as a
dev dependency, and sets [cli] extension in graftkit.toml.
Layout#
src/<package>/graftkit/
__init__.py EXTENSION = Extension.from_package("<name>", f"{__name__}.commands")
commands/
hello.py def command(repo: Repo) -> Command one command per module
_shared.py helpers: modules starting with _ are not commands
workflows/
hello.py what the command does, with no command-line concerns
tests/graftkit/
test_hello_command.py
test_graftkit_conformance.py
# pyproject.toml
[project.entry-points."graftkit.extensions"]
acme-tools = "acme_tools.graftkit:EXTENSION" # <name> = "<package>.graftkit:EXTENSION"
Commands declare; workflows do. A command module holds the
Commanddeclaration: help, examples, options, interview questions, consents. It hands the answers to a workflow function. Workflows take plain values and are tested without the CLI.The root is given, never computed.
command(repo)receives theRepo:repo.rootand the validatedrepo.config.Prompts are questions. Anything a command asks is an interview
Questionwith a flag, so every command also runs unattended (--yes) and from an AI agent (--describe).Examples show the way. A command declares
examples: command lines an agent or a person can copy. cli-interview-kit checks each one against the command’s flags when the app is built, so an example that no longer parses fails every test.--helpand--describeshow them.Errors are
CliError. Raise it with a message that says what to do; the CLI prints it and exits 1. Anything else is a bug and prints a pointer to--debug.Output goes through
invocation.console.printfor results,info,success,warninganderrorfor status lines on stderr.
The command standard#
Checked by graftkit extension check, by graftkit doctor, and by the conformance test
in every extension:
Names are lowercase words joined by single hyphens. A module
deploy_bundle.pydefines the commanddeploy-bundle.Top-level names avoid the core’s:
config,describe,doctor,extension,help,init,org,skills.Every help text (commands, options, questions) is one sentence: it starts with a capital letter or a backtick, ends with a period, and fits in 80 characters.
graftkit.*annotations are known ones with valid values (see below).A command an agent may run (
graftkit.agent = "run") declares at least one example, so an agent copies a command line that works instead of guessing. On ahand-offcommand, examples are optional.
Agent policy#
Every command tells coding agents whether they may run it themselves:
Command(
name="status",
help="Show the bundle's deployment status.",
annotations={"graftkit.agent": "run"}, # an agent may run it
run=...,
)
A command without the annotation is hand-off: an agent gives the user the exact command to
run instead. Declare run for commands that are safe to run on the user’s request (reads,
checks, local scaffolding); leave deploys, releases and anything that changes shared state as
hand-off. Consents still apply either way: an agent passes --yes only after the user agreed.
graftkit describe --json reports each command’s policy, and graftkit skills sync writes the
graftkit skill: every command of the repository, core and extension, with what it does, its
policy, and how to read one command’s examples, options and questions with --describe. skills sync --check fails when the commands
change until the skill is regenerated.
The README’s For agents section#
graftkit org map shows every repository of the organisation to people and agents, with its
GitHub description, its commands (from the generated skill) and its README’s ## For agents
section: what other repositories can use from it, which a description and a command list cannot
say. Write it as exactly three labelled bullets:
## For agents
- **Provides:** what other repositories use from this one.
- **Concepts:** the domain terms it owns.
- **Entry points:** where to start reading or running it.
graftkit doctor warns when the README lacks the section or its bullets; nothing fails.
Testing#
graftkit ships a pytest plugin, active wherever it is installed:
Fixture |
Gives |
|---|---|
|
a temporary root holding that |
|
runs the CLI in-process; returns |
|
a fresh in-memory GitHub; pass it as |
|
a fresh in-memory git: clones and pull requests; pass it as |
assert_conformant(EXTENSION, root) fails with every violation of the standard. Pass
extension=EXTENSION to graftkit_run to test commands before the package is installed.
FakeGit records every clone (clones) and pull request (opened); change a clone’s state,
such as clean=False, to test what a command does with it, without running git.
The public API#
Import only from graftkit (Extension, Repo, Command, Option, Interview,
Question, Consent, Invocation, Console, CliError, non_empty, when) and
graftkit.testing. Everything else is internal. The public API follows semantic
versioning; while the version is 0.x, a breaking change bumps the minor version and is listed
in the changelog.
GitHub access#
The CLI reaches GitHub only through the GitHub CLI (gh), and never reads or stores a token.
On a workstation, gh auth login once; in CI, set GH_TOKEN. graftkit doctor checks that
gh is installed and logged in.