A monorepo
This is for a repository that holds several packages, such as Node
packages under packages/ and Python services under services/. At the
end, a session that changes one package runs
that package's checks, its gate, not every
package's, and a change to a file nobody accounted for still runs them
all.
Set the factory up first, at the repository's root, with the
quick start: one factory init, one
.claude/factory.yaml, and one board, as for any repository. This
page changes the commands under commands in that file, on the
branch the quick start leaves you on.
If your monorepo already has Nx, Turborepo, or Pants, it already
knows which packages a change affects, and you skip steps 1 and 2. Make
your gate one command that asks it, and pass it
$BASE, the commit your
branch started from on your default branch:
commands:
gate:
- npx nx affected -t lint test --base=$BASE
# Turborepo: npx turbo run lint test --filter="...[$BASE]"
# Pants: pants --changed-since=$BASE --changed-dependents=transitive testIt worked when factory gate prints $ npx nx affected ... with no
line before it, and ends with ✓ Every gate command passed. When it
starts with ? Can't tell what changed, $BASE is empty: run the fetch
command that line prints, and run the gate again. Read on only to split
up your other commands, such as
commands.test-one, by package
(The other settings).
1. Write each package's commands
Write commands.gate in
.claude/factory.yaml, replacing the one
command factory init asked you for, if you gave one, with a command for
each kind of package:
commands:
gate:
- npm run -s format:check # from the root, for every change
- run: npm test
in: packages/* # in each package, when that package changes
files: [./**, packages/shared/**, package.json, package-lock.json]
- run: poetry run pytest -q
in: services/* # in each service, when that service changesin: packages/*makes one command for each folder underpackages/, and runs each inside its own folder.fileslists the changed files that run it, as patterns such assrc/**/*.ts../means the command's own folder, so./**is every file in that package. A pattern without./starts at the root.- Without
files, a change to any file in its folder runs it, as withpytestabove. Writingfilesreplaces that, so keep./**in the list, or the package's own changes stop running it. - List what a package's checks read outside its folder, such as a
shared library or the root's lock file, as
npm testdoes above. Then a change to that file runs every package's tests. The factory looks at which files changed, not at what your code imports, so a file you leave out runs nothing of that package's when it changes. - A plain text command, such as the format check, runs from the root for every change, as in any repository.
A changed file is covered when a gate command's files match it, or
its folder does when it has no files. A changed file that nothing
covers runs every gate command, since the factory can't tell what it
might break. That is what keeps an edit to a root file such as
tsconfig.base.json from passing with no type check.
It worked when factory gate --dry-run lists each command as it
will run, such as cd packages/api && npm test, each marked runs or
left out for the change on your branch.
2. Settle what nothing covers
Your repository has files outside every package: CI workflows, scripts,
and root settings. factory config check lists the ones nothing covers.
A folder with nothing covered in it is one line, with how many files it
holds:
$ factory config check
? 4 paths are in no gate command's files and not in commands.ignore, so a change to one runs every command:
.github/ 6 files
scripts/ 11 files
docs/ 9 files
tsconfig.base.json
Add each to commands.ignore, or to the files of the gate commands it affects.
✓ The settings are valid.For each, decide what a change to it could break:
-
Some of your checks: add it to those commands'
files.tsconfig.base.jsongoes innpm test's, since every Node package reads it, andscripts/**goes in thefilesof the commands that run those scripts. -
None of them: add it to
commands.ignore. Your CI workflows,.github/**, are checked by your CI itself, anddocs/**holds no code:commands: ignore: - .github/** - docs/**A change to an ignored file runs only the plain text commands, such as the format check, even where a command's
./**would match it. -
You're not sure: leave it. A change to it runs every command, which is slow but never misses anything.
Then run every command once, whatever changed, to check that each works in its folder:
factory gate --allIt worked when factory config check prints only
✓ The settings are valid., or lists only files you chose to leave, and
factory gate --all ends with ✓ Every gate command passed. Commit
.claude/factory.yaml on that branch.
What runs from then on
A session's factory gate compares its branch with the commit the branch
started from on your default branch, which it reads from origin/ and
the default branch's name, and runs each command a changed file sets
off. With fifty packages and twenty services, and main as the default
branch:
$ factory gate
commands.gate: 2 of 71 run, for the 3 files changed since origin/main
npm run -s format:check every change
cd packages/api && npm test packages/api/src/orders.ts, and 1 more
- 69 left out, since this change touches none of their files: factory gate --dry-run lists eachA change to .claude/factory.yaml runs every command, so a command you
add is tried in the pull request that adds it. A changed file nothing
covers also runs every command, and the first line names it, so you know
what to add. Commands for some folders
has every rule.
The other settings
Each of these is optional. Split one only when your packages need different commands for it.
commands.test-oneandcommands.end-to-end, which a session uses to run one test file, if your packages run their tests with different tools: give a command for each kind of package, and the factory runs the one whose folder holds the test file.commands.coverage, if each package measures its own coverage: give each its command and report, andfactory coverageruns the ones a change needs.commands.relock, if your packages keep their own lock files, such as one in each Python service:factory relockrebuilds the ones a change needs after a conflict.site, if the repository holds more than one web app: list a site for each, andfactory site comparetakes screenshots of the ones a change touches.- A preset, a shared set of settings, if your company
has one written for one package, such as a Python preset: apply it to
each of your services with
factory preset add NAME --in 'services/*'. - Notes for a session about one package go in the coding agent's
instructions file in that package's folder, a
CLAUDE.mdon Claude Code, which it reads when it works there.