Skip to content

feat(cli): detect files pinned by contract tests (#445) - #466

Merged
stackbilt-admin merged 1 commit into
mainfrom
feat/issue-445-contract-pins
Nov 28, 2026
Merged

stackbilt-admin merged 1 commit into
mainfrom
feat/issue-445-contract-pins

Conversation

@stackbilt-admin

Copy link
Copy Markdown
Member

Summary

Adds packages/cli/src/commands/contract-pins.ts, a new module that tells an agent before it edits whether a file's behaviour is pinned by a contract test. It reports which tests pin the file and what a change means for semver.

  • detectContractPins(root, files) -> ContractPin[]: one result per file, as { file, pinned: true|false|'unknown', by, surfaces, guidance, evidence }.
  • summarizeContractPins(pins): folds several files into the single contract answer from the issue. It is true if any file is pinned, false only if every file is known not to be pinned, and 'unknown' otherwise. Wiring it into preflight is a one-liner: contract: summarizeContractPins(detectContractPins(root, files)).

How detection works:

  • Convention. A repo has a convention when it has *.test.* or *.spec.* files whose name or directory contains contract (node_modules, dist and .claude are skipped). With no such files, every file gets pinned: 'unknown', never a guess. The file list comes from git ls-files --cached --others --exclude-standard. Outside a git work tree it falls back to a bounded directory walk.
  • Evidence (real, not name matching):
    • import: the contract test imports the file with a value import, through a relative specifier that resolves to it. .js specifiers resolve to .ts files and directories resolve to index.*. import type does not count. This is how evidence-contracts.test.ts exercises adf-evidence.ts.
    • cli: the file is under commands/ (adf-evidence.ts maps to adf evidence) and the test runs that command through the CLI binary, either as an argument array whose first element is CLI_BIN or bin.js, or as a charter <cmd> / bin.js <cmd> string passed straight to exec*/spawn*. A string that only mentions the command, such as the charter adf metrics recalibrate hint asserted in evidence-contracts, does not count. The tests cover that case.
  • Surfaces come from what the pinning tests assert:
    • exit codes: EXIT_CODE.*, expect(exitCode) or expect(r.status).
    • --format json: a JSON format flag together with JSON.parse.
  • Guidance, from core.adf: breaking = major + migration note; additive = minor when pinned. There is distinct text for not pinned, unknown and outside-root files.

Results on this repo:

  • adf-evidence.ts is pinned by packages/cli/src/__tests__/integration/evidence-contracts.test.ts, with surfaces exit codes and --format json.
  • packages/cli/src/index.ts is also pinned: the contract test imports the EXIT_CODE values from it, and those values are a real contract.
  • Every other command file is false.
  • Scanning all command files takes about 80 ms.

Only new files are added. No existing code, JSON output or exit code changes.

Verified

  • pnpm exec vitest run packages/cli/src/__tests__/pinned-by-tests.test.ts: 15/15 passed. The tests cover this repo (the acceptance case, an absolute path, a hint string that is not an invocation, a file that isn't pinned) and fixtures (no convention gives unknown, CLI-invocation evidence with surfaces, a contracts/ directory, a type-only import that doesn't pin, a non-test *.contract.ts, a file outside the root), plus the helpers and the summary.
  • pnpm exec tsc --build tsconfig.build.json: ok.
  • pnpm exec tsc --noEmit -p tsconfig.json: ok.
  • node packages/cli/dist/bin.js adf evidence --auto-measure --ci --format text: PASS, with 1 warning (adf-migrate.ts at 792/792, already on main). The pre-commit hook passed too.
  • Full suite, pnpm exec vitest run, ran twice on a machine loaded by parallel worktrees:
    • First run: 28 failed. Second run: 11 failed, 2179 passed, 2 skipped across 108 files.
    • In the second run, the failures in the known Windows-only files were bootstrap (bootstrap: phases 6/7 collect warnings without printing them; absoluteAiDirArg misses Windows paths on POSIX #307), context-refresh and serve-context (path separators), and github-commit-range (3).
    • It also had failures in adf-vendor-doctor-score, bootstrap-reset-cli and doctor-staged-bloat. Rerun on their own, those 3 files pass 109/109, so the cause is load.
    • This PR only adds files that no existing module imports, so it cannot affect them.

Not done

  • Manifest-declared contract-test lists (the issue's "or a list declared in the manifest"). This first version uses only the *contract* file convention, because adding a manifest key would mean editing .ai/*.adf conventions.
  • Wiring into the preflight answer in adf-preflight.ts / adf-context.ts. That is left to the integration PR, as planned.
  • Evidence is direct only: the test must import the file or run its command. Files reached only through other modules are reported false.
  • The test file is named pinned-by-tests.test.ts rather than contract-pins.test.ts, so it doesn't match the convention itself.

Closes #445
Part of #437

🤖 Generated with Claude Code

New detectContractPins(root, files) reports, per file, whether a test
under the repo's *contract* test convention exercises it (value import
that resolves to the file, or a run of the CLI command it implements),
which tests pin it, the surfaces they assert (exit codes, --format json)
and the semver guidance from core.adf. Repos with no contract-test
convention get pinned: 'unknown'. summarizeContractPins folds several
files into one answer for the preflight integration.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants