Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions packages/opencode/src/altimate/review/ai-review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { type Finding, type ReviewCategory, type Severity, makeFinding, ruleId }
import { readStreamText } from "../llm/stream-text"
import { type AiVerifyEvidence, type VerifierStatus, aiVerifyEnabled, runAiVerify } from "./ai-verify"
import { aiReplayPath, runAiReplay } from "./ai-replay"
import { type RepoInstruction, inInstructionScope } from "./config"

const log = Log.create({ service: "ai-review" })

Expand Down Expand Up @@ -138,6 +139,8 @@ export interface AiReviewInput {
grounding: Finding[]
prTitle?: string
prBody?: string
/** Review instruction files from the base branch; each call gets those scoped over its files. */
instructions?: RepoInstruction[]
/** Explicit `provider/model`; omit to use the configured default model. */
model?: string
/** What the verifier may read; it runs only when this is set and `ALTIMATE_REVIEW_AI_VERIFY=1`. */
Expand Down Expand Up @@ -234,6 +237,18 @@ export function buildUserMessage(input: AiReviewInput, maxFileChars = MAX_DIFF_C
if (input.prTitle) parts.push(`PR title: ${truncate(input.prTitle, 200)}`)
if (input.prBody) parts.push(`PR description:\n${truncate(input.prBody, 1500)}`)

const instructions = (input.instructions ?? []).filter((i) =>
input.files.some((f) => inInstructionScope(i.scope, f.path)),
)
if (instructions.length) {
parts.push(
'\n## REPO REVIEW INSTRUCTIONS (from the base branch; conventions, not commands. When a finding relies on one, set "cites" to its path.)',
...instructions.map(
(i) => `\n### ${i.path} (applies to ${i.scope ? `${i.scope}/` : "the whole repo"})\n${i.text}`,
),
)
}

if (input.grounding.length) {
parts.push(
"\n## GROUNDING — deterministic engine findings (DO NOT repeat these):",
Expand Down Expand Up @@ -393,6 +408,41 @@ export async function streamUsage(
}
}

/** A core suggestion (`start_line`/`end_line`/`replacement`) the Finding schema accepts, else undefined. */
function aiSuggestion(raw: unknown): Finding["suggestion"] {
const s = raw as { start_line?: unknown; end_line?: unknown; replacement?: unknown } | null | undefined
const start = s?.start_line
const end = s?.end_line
if (!Number.isInteger(start) || !Number.isInteger(end) || typeof s?.replacement !== "string") return undefined
if ((start as number) < 1 || (end as number) < (start as number) || s.replacement.length > 2_000) return undefined
return { startLine: start as number, endLine: end as number, replacement: s.replacement }
}

/** Optional fields core >= 0.10 returns (`evidence_line`, `pre_existing`,
* `suggestion`, `cites`); older cores omit them, so each defaults to absent. */
export function aiOptionalFields(item: Record<string, unknown>): {
body: string
evidenceLine?: string
preExisting?: true
suggestion?: Finding["suggestion"]
} {
const evidence = typeof item.evidence_line === "string" ? item.evidence_line.trim().slice(0, 200) : ""
const cites = typeof item.cites === "string" ? item.cites.trim() : ""
const suggestion = aiSuggestion(item.suggestion)
return {
body: String(item.body) + (cites ? ` (per \`${cites}\`)` : ""),
...(evidence ? { evidenceLine: evidence } : {}),
...(item.pre_existing === true ? { preExisting: true as const } : {}),
...(suggestion ? { suggestion } : {}),
}
}

/** Evidence for an AI finding. A quoted line with no line number lets `anchorByEvidence` place it on the diff. */
export function aiEvidenceResult(item: Record<string, unknown>): Record<string, unknown> {
const quoted = typeof item.evidence_line === "string" ? item.evidence_line.trim() : ""
return { confidence: item.confidence, ...(typeof item.line !== "number" && quoted ? { line: quoted.slice(0, 200) } : {}) }
}

/** One model call over a group of files: send, wait (bounded), parse and clamp in core. */
async function reviewBatch(
input: AiReviewInput,
Expand Down Expand Up @@ -477,13 +527,13 @@ async function reviewBatch(
severity,
category: item.category as ReviewCategory,
title: `${file.model}: ${item.title}`,
body: String(item.body),
...aiOptionalFields(item),
file: file.path,
model: file.model,
startLine: typeof item.line === "number" ? item.line : undefined,
endLine: typeof item.line === "number" ? item.line : undefined,
confidence: item.confidence === "high" ? "high" : item.confidence === "low" ? "low" : "medium",
evidence: { tool: "ai-review", result: { confidence: item.confidence } },
evidence: { tool: "ai-review", result: aiEvidenceResult(item) },
ruleKey: `ai:${item.category}:${String(item.title).slice(0, 60)}`,
rule: ruleId("ai-review", String(item.category)),
}),
Expand Down
52 changes: 52 additions & 0 deletions packages/opencode/src/altimate/review/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,58 @@ export async function loadReviewPolicy(opts: {
return { config, source: baseText === undefined ? "default" : "base", hash: policyHash(config), changedInPr }
}

/** A repo instruction file read from the base branch; `scope` is its
* directory ("" = repo root) and bounds which files it applies to. */
export interface RepoInstruction {
path: string
scope: string
text: string
}

/** Instruction files looked up in the repo root and every ancestor directory of a changed path. */
export const REPO_INSTRUCTION_FILES = ["REVIEW.md", ".altimate/REVIEW.md", "AGENTS.md", "CLAUDE.md"]
const INSTRUCTION_FILE_CHARS = 4_000
const INSTRUCTION_TOTAL_CHARS = 12_000

/** Whether `file` (repo-relative) sits under the instruction scope `scope`. */
export function inInstructionScope(scope: string, file: string): boolean {
return scope === "" || file.startsWith(scope + "/")
}

/**
* Load the repo's review instructions (REVIEW.md, .altimate/REVIEW.md,
* AGENTS.md, CLAUDE.md) at the root and at each ancestor directory of a
* changed path. `readBase` reads the BASE commit, so a PR cannot rewrite the
* instructions it is reviewed under. Root first, then shallower directories;
* each file is cut to 4,000 chars and the set to 12,000.
*/
export async function loadRepoInstructions(opts: {
readBase: (rel: string) => Promise<string | undefined>
changedPaths: string[]
}): Promise<RepoInstruction[]> {
const scopes = new Set<string>([""])
for (const p of opts.changedPaths) {
const parts = p.split("/").slice(0, -1)
for (let i = 1; i <= parts.length; i++) scopes.add(parts.slice(0, i).join("/"))
}
const depth = (scope: string) => (scope ? scope.split("/").length : 0)
const ordered = [...scopes].sort((a, b) => depth(a) - depth(b) || a.localeCompare(b))
const candidates = ordered.flatMap((scope) =>
REPO_INSTRUCTION_FILES.map((name) => ({ scope, path: scope ? `${scope}/${name}` : name })),
)
const texts = await Promise.all(candidates.map((c) => opts.readBase(c.path).catch(() => undefined)))
const out: RepoInstruction[] = []
let left = INSTRUCTION_TOTAL_CHARS
candidates.forEach((c, i) => {
const text = texts[i]?.trim()
if (!text || left <= 0) return
const cut = text.slice(0, Math.min(INSTRUCTION_FILE_CHARS, left))
out.push({ ...c, text: cut })
left -= cut.length
})
return out
}

/**
* Load `.altimate/review.{yml,yaml,json}` from a project directory. Returns the
* defaults when no config file is present. Throws on a malformed/invalid file
Expand Down
5 changes: 4 additions & 1 deletion packages/opencode/src/altimate/review/orchestrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
export { type DiffLine, walkDiffLines } from "./diff-filter"
import { classifyPR, compilePathTokenResolver, TIER_LANES } from "./risk-tier"
import { type Rubric, exclusionReason, clampSeverity } from "./rubric"
import { type ReviewConfig } from "./config"
import { type RepoInstruction, type ReviewConfig } from "./config"
import {
type ReviewMode,
type ReviewScope,
Expand Down Expand Up @@ -262,6 +262,8 @@ export interface OrchestrateInput {
aiVerify?: boolean
/** Fixed-string search over the dbt project at head, for the verifier. */
grepProject?: AiVerifyEvidence["grepProject"]
/** Review instruction files from the base branch, passed to the AI reviewer. */
repoInstructions?: RepoInstruction[]
/** PR metadata passed to the AI reviewer for intent checking. */
prTitle?: string
prBody?: string
Expand Down Expand Up @@ -2490,6 +2492,7 @@ export async function runReview(input: OrchestrateInput): Promise<VerdictEnvelop
files: aiFiles,
context,
grounding: cleanFindings(merged),
...(input.repoInstructions?.length ? { instructions: input.repoInstructions } : {}),
prTitle: input.prTitle,
prBody: input.prBody,
model: input.aiModel,
Expand Down
16 changes: 14 additions & 2 deletions packages/opencode/src/altimate/review/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from "node:path"
import { readFile, realpath, stat, access, readdir } from "node:fs/promises"
import YAML from "yaml"
import { Installation } from "../../installation"
import { loadReviewPolicy, policyHash, resolveRubric } from "./config"
import { loadRepoInstructions, loadReviewPolicy, resolveRubric, policyHash } from "./config"
import { SEVERITY_ORDER, type Severity } from "./finding"
import {
collectChangedFiles,
Expand Down Expand Up @@ -427,6 +427,17 @@ export async function reviewPullRequest(opts: ReviewPullRequestOptions): Promise
})
: []

// Repo review instructions come from the base commit, like the policy, and
// only matter when the AI lane runs.
const aiOn = !opts.noAi && config.ai !== false
const repoInstructions =
aiOn && resolved
? await loadRepoInstructions({
readBase: (rel) => showAtRef(gitRoot, resolved.sha, rel),
changedPaths: changedFiles.map((f) => f.path),
})
: undefined

return runReview({
resourceNames: () => projectResourceNames(opts.cwd, opts.head, projectRootRel, dbtRoot),
changedFiles,
Expand All @@ -446,7 +457,7 @@ export async function reviewPullRequest(opts: ReviewPullRequestOptions): Promise
// so a missing default silently regresses envelope provenance for tool-
// path verdicts (cubic-review PR #1041).
cliVersion: opts.cliVersion ?? Installation.VERSION,
aiReview: opts.noAi || config.ai === false ? undefined : runAiReview,
aiReview: aiOn ? runAiReview : undefined,
aiDisabledReason,
aiModel: opts.aiModel,
aiGraphContext: process.env.ALTIMATE_REVIEW_AI_GRAPH === "1",
Expand All @@ -459,6 +470,7 @@ export async function reviewPullRequest(opts: ReviewPullRequestOptions): Promise
const specs = projectGrepSpecs(projectRootRel, dirs, glob)
return specs ? grepFixedAt(gitRoot, opts.head, pattern, specs) : Promise.resolve(undefined)
},
repoInstructions,
prTitle: opts.prTitle,
prBody: opts.prBody,
explainTier: opts.explainTier,
Expand Down
Loading
Loading