Skip to content

Add opt-in repository context to reviews (Phase 9, K1–K5) - #21

Closed
ziadantar90 wants to merge 6 commits into
developmentfrom
claude/project-thread-27m4k1
Closed

ziadantar90 wants to merge 6 commits into
developmentfrom
claude/project-thread-27m4k1

Conversation

@ziadantar90

Copy link
Copy Markdown
Contributor

Requested by Ziad · project thread

What

A new setting, reviewRepoContext (default false), lets the reviewer see code outside the diff. With it on, each batch also gets:

  • a repository map: folders and file names, or the top folders with file counts in a large repo, plus the file names in the changed files' folders;
  • excerpts of up to 12 related files:
    • callers: files that use a function, method, class or type the batch changes;
    • imports: files the changed files import (TS/JS, Python, Go, PowerShell, Bicep).

The context is read with git from the checkout the pipeline already makes (fetchDepth: 0). That means no clone, no Azure DevOps API call and no new permission. It is capped by reviewContextMaxTokens (default 12000). That budget comes out of reviewMaxBatchTokens, so no single request grows.

Safety

  • The context comes from the PR branch, so it is untrusted. It goes in its own random-id fence, like the diff.
  • The base prompt has a new section, "Using Repository Context". It tells the model never to comment on context files, to anchor findings on the diff line instead, and not to treat a missing caller as proof that none exists.
  • The existing anchor check drops any comment on a file outside the diff.
  • If the context fails to build, the review runs without it.

Commits (one per plan item)

Item Commit
K1 Repository map builder
K2 Related files: callers per language family, imports, numbered excerpts
K3 Sends the context to the model behind the opt-in, with the prompt section
K4 Template parameters, metrics fields contextFiles/contextTokens, run summary, docs, CHANGELOG
K5 tools/Measure-RepoContext.ps1, plus tuning based on the measurements

Measurements (prompt tokens, estimated; no model called)

Sample Total extra Median extra
This repo, 18 merged PRs +20% +10%
nestjs/nest (2,700 files), 10 recent commits, 12k budget +79% +74%
Same, 6k budget +54% —
  • Output tokens do not change.
  • Building the context takes about 1 s or less per batch.
  • Review quality is not measured yet: that needs a live model. The suggested next step is to run a few PRs in an ADO test project with the setting on and off.

Checks

  • Pester: 281 passed (was 231)
  • PSScriptAnalyzer: 0 findings
  • Update-ParameterReference.ps1 -Check: up to date
  • No Go changes

Generated by Claude Code

Get-RepoContext.ps1 lists the files tracked on the pull request branch
with git ls-tree, applies the review excludes, and renders one line per
folder with its file names. Over budget it falls back to file counts,
shallow folders first, and says how many deeper folders it left out.
It reads the pipeline's existing checkout: no clone, no API call.

Nothing calls it yet; the review wires it in behind an opt-in setting
in a later commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01D8A4vvpSVaT7B5TkFLqmvF
Signed-off-by: Claude <noreply@anthropic.com>
For the files in a batch, Get-RepoContext.ps1 now finds:

- callers: files on the PR branch that use a function, method, class
  or type the batch declares, changes or removes, or whose body changed
  (from git's hunk headers). One git grep -w per language family, so a
  Go Parse does not match a PowerShell [int]::Parse. Comments, test
  helpers, short and generic names, and names used in more than 40
  files are skipped.
- imports: files the changed files import, for TS/JS (relative and @/),
  Python (relative and absolute), Go (through go.mod), PowerShell
  dot-sourcing and Bicep modules. Imports the changed lines use rank
  first.

New-BatchRepoContext takes callers and imports in turn, callers first,
as numbered excerpts ('NN | ', unlike the diff's 'NN+ '): the lines
around each use for a caller, the whole file or its declarations for an
import. It stops at the token budget or 12 files and never repeats a
file that is in the batch. Get-CodeChanges keeps each file's hunk
header function names for this.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01D8A4vvpSVaT7B5TkFLqmvF
Signed-off-by: Claude <noreply@anthropic.com>
Invoke-LLMCodeReview takes -RepoContext ('false' by default) and
-ContextMaxTokens (12000). When on, each batch gets a "Repository
context" user message before the diff, in its own fence with a random
id, like the diff. The wrapper says the context is untrusted and that
comments must point at a + line in the diff, so a legacy prompt is
covered too; the anchor check already drops anything else.

The context budget comes out of reviewMaxBatchTokens (the diff keeps at
least half), so a request with context is no larger than one without.
A batch that is split in two rebuilds context for each half. If the
repository cannot be read or a batch's context fails, the review goes
on without it. The result reports ContextFiles and ContextTokens.

The base prompt gets a "Using Repository Context" section: check
callers, existing helpers and conventions; never comment on context
files; anchor on the diff line and name the affected place; do not
treat a caller missing from the excerpts as proof there is none.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01D8A4vvpSVaT7B5TkFLqmvF
Signed-off-by: Claude <noreply@anthropic.com>
stage-template.yml gets two parameters: reviewRepoContext (boolean,
default false) and reviewContextMaxTokens (number, default 12000), and
passes them to Invoke-LLMCodeReview. The metrics event adds
contextFiles and contextTokens (custom fields, so the metrics API needs
no change), and the run summary line reports repository context when
it was sent.

Docs: regenerated parameter reference; SECURITY.md lists repository
context in "What the model receives", the prompt-injection measures and
the metrics fields; README feature list, configuration table and cost
notes; troubleshooting rows for the two context log lines; the example
pipeline shows the setting; CHANGELOG entry under Unreleased.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01D8A4vvpSVaT7B5TkFLqmvF
Signed-off-by: Claude <noreply@anthropic.com>
tools/Measure-RepoContext.ps1 builds the review input for two commits
with and without repository context and prints the estimated prompt
tokens per batch, without calling a model. With -OutputPath it writes
each batch's context to a file so it can be read.

Measured on this repository's 18 merged pull requests and 10 recent
nestjs/nest commits (2,700 files), then tuned:

- Large repositories: the map was 3,000 tokens of single-file folder
  counts. It now shows the top two (or one) folder levels with counts,
  within min(1500, budget/8), and lists the file names of the changed
  files' folders per batch instead.
- Callers: test, spec, e2e and sample files rank after production
  code, at most three test files and three files per symbol, and a
  symbol used in more than 25 files is dropped (was 40).
- Imports: ESM TypeScript imports of './x.js' resolve to './x.ts', and
  imports the changed lines do not use only fill leftover budget.

Result at the default 12,000-token budget: prompt tokens +20% in total
on this repository (median +10%), +79% on the nestjs sample (median
+74%; +54% at a 6,000 budget). Context builds in about a second or
less per batch. Review quality needs a live model and is not measured
here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01D8A4vvpSVaT7B5TkFLqmvF
Signed-off-by: Claude <noreply@anthropic.com>
@ziadantar90 ziadantar90 self-assigned this Nov 2, 2026
It stays off by default and is not part of a release until a live
comparison on real pull requests shows it improves reviews.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01D8A4vvpSVaT7B5TkFLqmvF
Signed-off-by: Claude <noreply@anthropic.com>
@ziadantar90 ziadantar90 closed this Nov 2, 2026
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