Skip to content

Commit 1058aa5

Browse files
authored
feat(check): add --base flag for cross-file rule parity between local and CI (#339)
* feat(check): add --base flag for cross-file rule parity between local and CI `ctx.changedFiles` was empty by default, making cross-file dependency rules (e.g., "if config.yml changed, manifest.yml must also change") silently pass locally. These rules only worked in CI via a non-obvious `git reset --soft` workaround. Now `archgate check` auto-detects the base branch and populates `changedFiles` with the branch diff (`git diff <base>...HEAD`). The resolution priority is: `--staged` flag (skips base) → `--base <ref>` flag → `.archgate/config.json` `baseBranch` → git auto-detect → empty. Changes: - Add `detectBaseRef()` and `getFilesChangedSinceRef()` to git-files.ts - Add `--base [ref]` flag to `check` and `review-context` commands - Extend `ProjectConfigSchema` with optional `baseBranch` field - Extend `runChecks` and `buildReviewContext` options with `base` - Add `used_base` to check telemetry event - Add unit tests for new git helpers and integration tests for --base - Document Bun.env parallel test leakage pattern in agent memory Closes #338 Signed-off-by: Rhuan Barreto <rhuan@barreto.work> * refactor: extract resolveBaseRef helper and add review-context --base tests Extract duplicated base resolution logic from check.ts and review-context.ts into a shared resolveBaseRef() function in git-files.ts. Add integration tests for review-context --base to close the coverage gap. Signed-off-by: Rhuan Barreto <rhuan@barreto.work> * docs: document --base flag and changedFiles auto-detection Update CLI reference, rule API, concepts, writing-rules guide, and CI integration guide to document the new --base flag and automatic base branch detection for changedFiles. Includes both English and Portuguese (pt-br) translations. Signed-off-by: Rhuan Barreto <rhuan@barreto.work> * docs: regenerate llms-full.txt Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --------- Signed-off-by: Rhuan Barreto <rhuan@barreto.work> Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 93b2495 commit 1058aa5

26 files changed

Lines changed: 709 additions & 71 deletions

File tree

‎.claude/agent-memory/archgate-developer/MEMORY.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Skipping steps 2 or 3 is a workflow violation. The user should NEVER have to inv
5656
- **GitHub CodeQL "default setup" can silently skip PRs — use an explicit workflow** — The repository-level "default setup" for CodeQL does not guarantee analysis on every PR. PR #279 (Renovate deps update) had zero CodeQL analyses, dropping the Scorecard SAST score from 10 to 9. Fix: add an explicit `.github/workflows/codeql.yml` that runs on `push: [main]`, `pull_request: [main]`, and a weekly schedule. After merging, disable the "default setup" in repository Settings > Code security > Code scanning to avoid duplicate analyses. The explicit workflow gives Scorecard a detectable `github/codeql-action` reference and guarantees coverage.
5757
- **`GITHUB_TOKEN`-authored pushes do NOT trigger downstream workflows — release.yml MUST use the GH App token** — When an Actions workflow pushes commits or opens PRs using `${{ github.token }}` / `secrets.GITHUB_TOKEN`, GitHub intentionally suppresses the resulting `push` / `pull_request` events to prevent recursion. Symptom on release PRs: the head SHA has no `pull_request`-event check runs, so `Validate Code` / `Lint, Test & Check` / `DCO Sign-off Check` are missing from the PR rollup and branch protection treats the PR as missing required checks. PR [#131](https://github.com/archgate/cli/pull/131) papered over this by manually `gh workflow run` + posting commit statuses, but `workflow_dispatch` runs land on `head_branch: release` with `pull_requests: []` — they are not associated with the PR ref, so `Lint, Test & Check` stayed orphaned and the bug recurred on PR [#251](https://github.com/archgate/cli/pull/251). Root-cause fix: in `release.yml` the `pull-request` job MUST generate a GitHub App installation token via `actions/create-github-app-token` (using `secrets.GH_APP_APP_ID` / `secrets.GH_APP_PRIVATE_KEY`) and pass it to BOTH `actions/checkout` and `simple-release-action`. App-token-authored pushes DO trigger `pull_request` events naturally. Apply the same pattern to any future workflow that pushes to a branch whose downstream CI must run.
5858

59+
- **`Bun.env` modifications in parallel test files leak into integration test subprocesses** — Bun test runner runs all test files in a single process sharing `Bun.env`. Tests that set `Bun.env.HOME`, `Bun.env.GIT_CONFIG_NOSYSTEM`, or `Bun.env.GIT_CONFIG_GLOBAL` (e.g., `auth.test.ts`, `credential-store.test.ts`) modify the shared environment. Integration tests that spawn CLI subprocesses via `runCli()` spread `process.env` (which IS `Bun.env`) into the child, inheriting the leaked values. Symptom: git operations in the subprocess fail with "not a git repo" or similar, but the test passes in isolation. Fix: integration tests that rely on git must explicitly reset git-related env vars in the `runCli` call: `runCli(args, dir, { GIT_CONFIG_NOSYSTEM: "", GIT_CONFIG_GLOBAL: "" })`. Applied in `tests/integration/check.test.ts` for the `--base` tests.
5960
- **Cross-command I/O sharing: export from the existing command file, don't create shared files** — When two commands need to share I/O functions (console.log with styleText), you CANNOT put them in `src/helpers/` (ARCH-002 forbids console.log in helpers) or create a new file under `src/commands/<parent>/` without a register function (ARCH-001 requires register\*Command export, ARCH-016 requires docs heading). The correct pattern: export the shared functions from the command file that already defines them (e.g., `plugin/install.ts` exports `installForEditor()` and `printManualInstructions()`) and import them in the other command. Applied in `upgrade.ts` importing from `./plugin/install`.
6061

6162
## Validation Pipeline

‎docs/public/llms-full.txt‎

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -789,11 +789,11 @@ The `check` function receives a `RuleContext` object that provides everything a
789789

790790
### Project Information
791791

792-
| Property | Type | Description |
793-
| ------------------ | ---------- | -------------------------------------------------------------------------------- |
794-
| `ctx.projectRoot` | `string` | Absolute path to the project root directory |
795-
| `ctx.scopedFiles` | `string[]` | Files matching the ADR's `files` globs, or all project files if no globs are set |
796-
| `ctx.changedFiles` | `string[]` | Files changed in git (populated when running with `--staged`) |
792+
| Property | Type | Description |
793+
| ------------------ | ---------- | ---------------------------------------------------------------------------------- |
794+
| `ctx.projectRoot` | `string` | Absolute path to the project root directory |
795+
| `ctx.scopedFiles` | `string[]` | Files matching the ADR's `files` globs, or all project files if no globs are set |
796+
| `ctx.changedFiles` | `string[]` | Files changed in git (auto-detected from branch diff, or from `--staged`/`--base`) |
797797

798798
### File Operations
799799

@@ -897,7 +897,7 @@ Rules execute with the following guarantees:
897897
- **Parallel across ADRs** -- Rules from different ADRs run concurrently for faster execution.
898898
- **Sequential within an ADR** -- Rules belonging to the same ADR run one after another, so earlier rules can establish context for later ones.
899899
- **Scoped files are pre-resolved** -- The `ctx.scopedFiles` array is populated before your `check` function is called, based on the ADR's `files` globs.
900-
- **Changed files for staged mode** -- When running `archgate check --staged`, `ctx.changedFiles` contains only the files staged in git, letting rules skip unchanged files for faster feedback.
900+
- **Changed files auto-detected** -- `ctx.changedFiles` is automatically populated with the branch diff against the base branch (e.g., `main`). Use `--staged` for pre-commit hooks (staged files only) or `--base <ref>` for an explicit base. This enables cross-file dependency rules to work locally, not just in CI.
901901

902902
The editor plugins for [Claude Code](/guides/claude-code-plugin/) and [Cursor](/guides/cursor-integration/) run `archgate check` automatically after every code change. The agent reads the applicable ADRs, writes compliant code, and validates -- no manual check commands needed. [Sign up for beta access](https://plugins.archgate.dev).
903903

@@ -1045,6 +1045,16 @@ Warnings (severity `warning`) are logged but do not affect the exit code. Only `
10451045

10461046
## Narrowing scope
10471047

1048+
### Check files changed in the PR
1049+
1050+
Use `--base` to compare against the PR's base branch. This gives cross-file dependency rules the full picture of what changed:
1051+
1052+
```yaml
1053+
- run: archgate check --base origin/${{ github.base_ref }}
1054+
```
1055+
1056+
Without `--base`, the base branch is auto-detected from `origin/HEAD`. The explicit form is recommended in CI for deterministic behavior.
1057+
10481058
### Check only staged files
10491059

10501060
Use `--staged` to limit checking to git-staged files. This is useful in pre-commit hooks or when you only want to validate what is about to be committed:
@@ -2721,14 +2731,22 @@ Use `ctx.scopedFiles` when your rule should only apply to files the ADR governs.
27212731

27222732
### ctx.changedFiles
27232733

2724-
An array of file paths that have been modified (git staged or changed). Useful for incremental checking -- only validate files that were actually touched.
2734+
An array of file paths that differ from the base branch. Auto-detected by default, or populated from `--staged` / `--base <ref>`. Useful for incremental checking and cross-file dependency rules.
27252735

27262736
```typescript
2737+
// Incremental checking -- only validate changed files
27272738
const filesToCheck = ctx.scopedFiles.filter((f) =>
27282739
ctx.changedFiles.includes(f)
27292740
);
2730-
for (const file of filesToCheck) {
2731-
// Only check changed files
2741+
2742+
// Cross-file dependency -- if file A changed, file B must also change
2743+
if (ctx.changedFiles.includes("config/database.yml")) {
2744+
if (!ctx.changedFiles.includes("deploy/manifest.yml")) {
2745+
ctx.report.violation({
2746+
message: "config changed but manifest was not bumped",
2747+
file: "config/database.yml",
2748+
});
2749+
}
27322750
}
27332751
```
27342752

@@ -2899,7 +2917,7 @@ ARCH-006/no-unapproved-deps
28992917
}
29002918
```
29012919

2902-
2. **Use `ctx.changedFiles` for incremental checking.** When running `archgate check --staged`, `ctx.changedFiles` contains only the git-staged files. Filter `ctx.scopedFiles` against it to check only what changed.
2920+
2. **Use `ctx.changedFiles` for incremental checking.** `ctx.changedFiles` is auto-populated with the branch diff (or staged files with `--staged`). Filter `ctx.scopedFiles` against it to check only what changed, or use it directly for cross-file dependency rules.
29032921

29042922
3. **Keep rules focused on one concern.** A rule that checks both naming conventions and import patterns should be split into two rules with separate IDs.
29052923

@@ -3571,13 +3589,14 @@ Loads every ADR with `rules: true` in its frontmatter, executes the companion `.
35713589

35723590
## Options
35733591

3574-
| Option | Description |
3575-
| ------------ | --------------------------------------------------------- |
3576-
| `--staged` | Only check git-staged files (useful for pre-commit hooks) |
3577-
| `--json` | Machine-readable JSON output |
3578-
| `--ci` | GitHub Actions annotation format |
3579-
| `--adr <id>` | Only check rules from a specific ADR |
3580-
| `--verbose` | Show passing rules and timing info |
3592+
| Option | Description |
3593+
| -------------- | -------------------------------------------------------------------- |
3594+
| `--staged` | Only check git-staged files (useful for pre-commit hooks) |
3595+
| `--base [ref]` | Compare changed files against a base ref (auto-detects when omitted) |
3596+
| `--json` | Machine-readable JSON output |
3597+
| `--ci` | GitHub Actions annotation format |
3598+
| `--adr <id>` | Only check rules from a specific ADR |
3599+
| `--verbose` | Show passing rules and timing info |
35813600

35823601
## Arguments
35833602

@@ -3607,6 +3626,12 @@ Check only staged files before committing:
36073626
archgate check --staged
36083627
```
36093628

3629+
Check all files changed on the current branch vs `main`:
3630+
3631+
```bash
3632+
archgate check --base main
3633+
```
3634+
36103635
Check a single ADR:
36113636

36123637
```bash
@@ -3637,6 +3662,26 @@ Get GitHub Actions annotations:
36373662
archgate check --ci
36383663
```
36393664

3665+
## Changed files detection
3666+
3667+
By default, `archgate check` auto-detects the base branch and populates `ctx.changedFiles` with the branch diff (`git diff <base>...HEAD`). This enables cross-file dependency rules to work locally -- not just in CI.
3668+
3669+
The base ref is resolved in priority order:
3670+
3671+
| Priority | Source | `changedFiles` populated with |
3672+
| -------- | ------------------------------------ | -------------------------------- |
3673+
| 1 | `--staged` | Git staging area only |
3674+
| 2 | `--base <ref>` | `git diff <ref>...HEAD` |
3675+
| 3 | `.archgate/config.json` `baseBranch` | `git diff <resolved-ref>...HEAD` |
3676+
| 4 | Git auto-detect | `git diff <detected-ref>...HEAD` |
3677+
| 5 | Detection fails | Empty (full-scan mode) |
3678+
3679+
Auto-detection tries `origin/HEAD`, then `origin/main`, `origin/master`, local `main`, and local `master`. To set a project default, add `baseBranch` to `.archgate/config.json`:
3680+
3681+
```json
3682+
{ "baseBranch": "main" }
3683+
```
3684+
36403685
## Diagnostics
36413686

36423687
During execution, `archgate check` emits warnings for common misconfigurations that may cause slow or unexpected results:
@@ -4108,11 +4153,12 @@ archgate review-context [options]
41084153

41094154
## Options
41104155

4111-
| Option | Description |
4112-
| ------------------- | ------------------------------------ |
4113-
| `--staged` | Only include git-staged files |
4114-
| `--run-checks` | Include ADR compliance check results |
4115-
| `--domain <domain>` | Filter to a single domain |
4156+
| Option | Description |
4157+
| ------------------- | -------------------------------------------------------------------- |
4158+
| `--staged` | Only include git-staged files |
4159+
| `--base [ref]` | Compare changed files against a base ref (auto-detects when omitted) |
4160+
| `--run-checks` | Include ADR compliance check results |
4161+
| `--domain <domain>` | Filter to a single domain |
41164162

41174163
## Example
41184164

@@ -4624,7 +4670,7 @@ Files matching the ADR's `files` glob patterns from its frontmatter. If the ADR
46244670
changedFiles: string[];
46254671
```
46264672

4627-
Files that have been modified according to git. When `--staged` is used, this contains only staged files. When running without `--staged`, this contains all changed files (staged and unstaged). Empty when no git changes are detected.
4673+
Files that have been modified according to git. By default, this is auto-populated with the branch diff against the detected base branch (e.g., `origin/main`). When `--staged` is used, this contains only staged files. When `--base <ref>` is used, this contains all files changed since that ref. Empty when base detection fails or no changes are found. Use this to build cross-file dependency rules (e.g., "if file A changed, file B must also change").
46284674

46294675
#### report
46304676

‎docs/src/content/docs/concepts/rules.mdx‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ The `check` function receives a `RuleContext` object that provides everything a
5555

5656
### Project Information
5757

58-
| Property | Type | Description |
59-
| ------------------ | ---------- | -------------------------------------------------------------------------------- |
60-
| `ctx.projectRoot` | `string` | Absolute path to the project root directory |
61-
| `ctx.scopedFiles` | `string[]` | Files matching the ADR's `files` globs, or all project files if no globs are set |
62-
| `ctx.changedFiles` | `string[]` | Files changed in git (populated when running with `--staged`) |
58+
| Property | Type | Description |
59+
| ------------------ | ---------- | ---------------------------------------------------------------------------------- |
60+
| `ctx.projectRoot` | `string` | Absolute path to the project root directory |
61+
| `ctx.scopedFiles` | `string[]` | Files matching the ADR's `files` globs, or all project files if no globs are set |
62+
| `ctx.changedFiles` | `string[]` | Files changed in git (auto-detected from branch diff, or from `--staged`/`--base`) |
6363

6464
### File Operations
6565

@@ -163,7 +163,7 @@ Rules execute with the following guarantees:
163163
- **Parallel across ADRs** -- Rules from different ADRs run concurrently for faster execution.
164164
- **Sequential within an ADR** -- Rules belonging to the same ADR run one after another, so earlier rules can establish context for later ones.
165165
- **Scoped files are pre-resolved** -- The `ctx.scopedFiles` array is populated before your `check` function is called, based on the ADR's `files` globs.
166-
- **Changed files for staged mode** -- When running `archgate check --staged`, `ctx.changedFiles` contains only the files staged in git, letting rules skip unchanged files for faster feedback.
166+
- **Changed files auto-detected** -- `ctx.changedFiles` is automatically populated with the branch diff against the base branch (e.g., `main`). Use `--staged` for pre-commit hooks (staged files only) or `--base <ref>` for an explicit base. This enables cross-file dependency rules to work locally, not just in CI.
167167

168168
:::tip[Run checks automatically with editor plugins]
169169
The editor plugins for [Claude Code](/guides/claude-code-plugin/) and [Cursor](/guides/cursor-integration/) run `archgate check` automatically after every code change. The agent reads the applicable ADRs, writes compliant code, and validates -- no manual check commands needed. [Sign up for beta access](https://plugins.archgate.dev).

‎docs/src/content/docs/guides/ci-integration.mdx‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,16 @@ Warnings (severity `warning`) are logged but do not affect the exit code. Only `
142142

143143
## Narrowing scope
144144

145+
### Check files changed in the PR
146+
147+
Use `--base` to compare against the PR's base branch. This gives cross-file dependency rules the full picture of what changed:
148+
149+
```yaml
150+
- run: archgate check --base origin/${{ github.base_ref }}
151+
```
152+
153+
Without `--base`, the base branch is auto-detected from `origin/HEAD`. The explicit form is recommended in CI for deterministic behavior.
154+
145155
### Check only staged files
146156

147157
Use `--staged` to limit checking to git-staged files. This is useful in pre-commit hooks or when you only want to validate what is about to be committed:

‎docs/src/content/docs/guides/writing-rules.mdx‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,22 @@ Use `ctx.scopedFiles` when your rule should only apply to files the ADR governs.
9999

100100
### ctx.changedFiles
101101

102-
An array of file paths that have been modified (git staged or changed). Useful for incremental checking -- only validate files that were actually touched.
102+
An array of file paths that differ from the base branch. Auto-detected by default, or populated from `--staged` / `--base <ref>`. Useful for incremental checking and cross-file dependency rules.
103103

104104
```typescript
105+
// Incremental checking -- only validate changed files
105106
const filesToCheck = ctx.scopedFiles.filter((f) =>
106107
ctx.changedFiles.includes(f)
107108
);
108-
for (const file of filesToCheck) {
109-
// Only check changed files
109+
110+
// Cross-file dependency -- if file A changed, file B must also change
111+
if (ctx.changedFiles.includes("config/database.yml")) {
112+
if (!ctx.changedFiles.includes("deploy/manifest.yml")) {
113+
ctx.report.violation({
114+
message: "config changed but manifest was not bumped",
115+
file: "config/database.yml",
116+
});
117+
}
110118
}
111119
```
112120

@@ -277,7 +285,7 @@ ARCH-006/no-unapproved-deps
277285
}
278286
```
279287

280-
2. **Use `ctx.changedFiles` for incremental checking.** When running `archgate check --staged`, `ctx.changedFiles` contains only the git-staged files. Filter `ctx.scopedFiles` against it to check only what changed.
288+
2. **Use `ctx.changedFiles` for incremental checking.** `ctx.changedFiles` is auto-populated with the branch diff (or staged files with `--staged`). Filter `ctx.scopedFiles` against it to check only what changed, or use it directly for cross-file dependency rules.
281289

282290
3. **Keep rules focused on one concern.** A rule that checks both naming conventions and import patterns should be split into two rules with separate IDs.
283291

0 commit comments

Comments
 (0)