Module 07 · 12 min
Set limits with a contract
Write the file that sets what AlbertCode SWE may do in a repository: the paths, the commands, the checks, the reviews and who approves.
By the end of this module you can
- Say what a contract controls, and what applies without one
- Read and write the main sections of a contract
- Explain why a task can never widen the limits it works under
A repository can carry a contract: a file that sets what AlbertCode SWE may do there. It says which paths a change may touch and how big it may be, which commands may run, which checks must pass, which reviews are required, and who may approve. It is the boundary every task in that repository works under.
Without one, and with one
With no contract, sensible defaults apply. A change keeps to the files its plan named, files that look like secrets are never changed, credentials are never written into files, and both reviews run.
AlbertCode SWE reads the first of these files that it finds at the root of the repository:
albertcode.contract.tomlalbertcode.contract.json.albertcode/contract.toml.albertcode/contract.json
The contract is read from your repository, under version control, and never from Albert's isolated copy. So a task cannot widen the limits that govern it. If the file changes while a task is under way, accepting that task is refused until it is planned again.
A first contract
This is the guide's own example:
version = 1
name = "shop"
[scope]
forbidden_paths = ["src/payments/**", "infra/**", "**/migrations/**"]
max_files_changed = 15
[verification]
required = true
must_pass = [["npm", "test"]]
[approvals]
require_admin_for_risk = ["high"]Commit it. From then on:
- a plan that wants to touch
src/paymentsis refused at once; - a change that fails
npm testcannot be accepted; - a high-risk plan waits for an administrator.
In paths, ** spans folders, while * and ? stay within one folder. A bare path such as docs covers everything beneath it.
The sections
| Section | What it sets |
|---|---|
[scope] |
Which files may change, and how much. It is enforced twice: a write outside it is refused as it happens, and the whole change is checked again before you can accept it |
[commands] |
Which commands may run. allowed and forbidden only narrow the commands AlbertCode SWE allows anyway, and run grants extra ones. forbidden always wins |
[verification] |
The checks that must pass. The must_pass commands must succeed in the isolated copy before the change can be accepted |
[review] |
Which reviews run, and how severe a finding must be to block the change |
[approvals] |
Whether the person who asked may also approve, which risk levels need an administrator, and whether a blocked change can ever be accepted with a written, recorded reason |
[obligations] |
Engineering rules, such as forbid_new_dependencies and forbid_committed_secrets, checked on every plan and every change |
[extensions] |
Which extra tools and skills may be switched on |
Every section is optional. The result of every check is part of the task's evidence (Contract).
Writing one from words
You don't have to write the file by hand. Describe what you want, and AlbertCode SWE drafts the file and shows it to you before saving:
- in the browser, use Contract and memory… in the Evidence menu;
- in the terminal, use
/contract.
Every saved version is kept. /contract history lists them, and /contract restore <n> makes an earlier one active again.
Practise
Know the sections
Every section of a contract is optional. Match each one to what it sets.
Guard a folder with a contract
Use the example contract in the lesson above. Writing it needs nothing installed.
- 1
Save the example as
albertcode.contract.tomlat the root of a repository, and commit it. - 2
Start AlbertCode SWE and read the boundary it shows:
/contract - 3
Ask for a governed change that touches
src/payments, and see the plan refused at once. - 4
Optional, in the classic terminal (
albertcode classic), draft a contract from your own words:/contract draft never touch src/payments, and run pytest before accepting - 5
List every saved version, any of which
/contract restore <n>makes active again:/contract history
Check your understanding
6 questions. Answer 5 correctly to complete the module. Checked here, in your browser.