Skip to content
Open
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
2 changes: 2 additions & 0 deletions packages/opencode/src/altimate/review/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export function renderSummary(env: VerdictEnvelope, opts: RenderOptions = {}): s
return ""

const lines: string[] = [REVIEW_MARKER, "", `## ${verdictHeadline(env)}`, ""]
const projects = env.scope?.projects ?? []
if (projects.length > 1) lines.push(`Reviewed ${projects.length} dbt projects: ${projects.map((p) => p.root || ".").join(", ")}`, "")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: The project list is unbounded even though this renderer promises a character budget for the posted comment. With enough touched projects, this line alone can exceed 5,000 characters and the later finding-budget loop cannot trim it; cap the displayed roots and retain the full list in scope.projects.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/opencode/src/altimate/review/format.ts, line 76:

<comment>The project list is unbounded even though this renderer promises a character budget for the posted comment. With enough touched projects, this line alone can exceed 5,000 characters and the later finding-budget loop cannot trim it; cap the displayed roots and retain the full list in `scope.projects`.</comment>

<file context>
@@ -72,6 +72,8 @@ export function renderSummary(env: VerdictEnvelope, opts: RenderOptions = {}): s
 
   const lines: string[] = [REVIEW_MARKER, "", `## ${verdictHeadline(env)}`, ""]
+  const projects = env.scope?.projects ?? []
+  if (projects.length > 1) lines.push(`Reviewed ${projects.length} dbt projects: ${projects.map((p) => p.root || ".").join(", ")}`, "")
 
   if (env.summary.degraded) {
</file context>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up: low priority, tracked. We'll cap it at RENDER_CAP with "+N more".

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Project roots are repository-controlled strings, but this line inserts them raw into Markdown. A project directory containing a backtick or newline can break the summary's formatting or inject content; render each root with codeSpan (or otherwise escape Markdown) before joining them.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/opencode/src/altimate/review/format.ts, line 76:

<comment>Project roots are repository-controlled strings, but this line inserts them raw into Markdown. A project directory containing a backtick or newline can break the summary's formatting or inject content; render each root with `codeSpan` (or otherwise escape Markdown) before joining them.</comment>

<file context>
@@ -72,6 +72,8 @@ export function renderSummary(env: VerdictEnvelope, opts: RenderOptions = {}): s
 
   const lines: string[] = [REVIEW_MARKER, "", `## ${verdictHeadline(env)}`, ""]
+  const projects = env.scope?.projects ?? []
+  if (projects.length > 1) lines.push(`Reviewed ${projects.length} dbt projects: ${projects.map((p) => p.root || ".").join(", ")}`, "")
 
   if (env.summary.degraded) {
</file context>
Suggested change
if (projects.length > 1) lines.push(`Reviewed ${projects.length} dbt projects: ${projects.map((p) => p.root || ".").join(", ")}`, "")
if (projects.length > 1) lines.push(`Reviewed ${projects.length} dbt projects: ${projects.map((p) => codeSpan(p.root || ".")).join(", ")}`, "")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up: low priority, tracked (codeSpan).

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SUGGESTION: The project list is unbounded, unlike the other multi-entry renders (RENDER_CAP = 8 at line 96, LEDGER_MAX_PATHS = 30 at line 283).

On a monorepo with many (or long-named) projects this single fixed line can consume most of SUMMARY_BUDGET_CHARS (5,000), after which fit drops every finding bullet into the "N more finding(s)" notice. Cap the roots shown and append (+N more var); wrapping/escaping the roots like the rest of the renderer (codeSpan, ledger escaping) would also be safer.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as r4626966319; tracked.


if (env.summary.degraded) {
lines.push(
Expand Down
9 changes: 5 additions & 4 deletions packages/opencode/src/altimate/review/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@ export async function gitRepoRoot(cwd: string): Promise<string | undefined> {
/** Every tracked file at `ref` (default HEAD), repo-root relative; undefined when git can't list it. */
export async function listFilesAt(cwd: string, ref?: string): Promise<string[] | undefined> {
try {
const out = await git(["ls-tree", "-r", "--full-tree", "--name-only", ref || "HEAD"], cwd)
return out.split("\n").filter(Boolean)
// -z: paths verbatim (no C-quoting of non-ASCII or special characters).
const out = await git(["ls-tree", "-r", "-z", "--full-tree", "--name-only", ref || "HEAD"], cwd)
return out.split("\0").filter(Boolean)
} catch {
return undefined
}
Expand All @@ -181,8 +182,8 @@ export async function listFilesAt(cwd: string, ref?: string): Promise<string[] |
/** Files in the index plus untracked, non-ignored files (the working tree), repo-root relative; undefined when git fails. */
export async function listWorkingTreeFiles(cwd: string): Promise<string[] | undefined> {
try {
const out = await git(["ls-files", "--full-name", "--cached", "--others", "--exclude-standard", "--", ":(top)"], cwd)
return out.split("\n").filter(Boolean)
const out = await git(["ls-files", "-z", "--full-name", "--cached", "--others", "--exclude-standard", "--", ":(top)"], cwd)
return out.split("\0").filter(Boolean)
} catch {
return undefined
}
Expand Down
1 change: 1 addition & 0 deletions packages/opencode/src/altimate/review/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export * from "./format"
export * from "./git"
export * from "./runner"
export * from "./run"
export * from "./multi-project"
export * from "./post-github"
export * from "./schema-context"
export * from "./dbt-patterns"
Expand Down
349 changes: 349 additions & 0 deletions packages/opencode/src/altimate/review/multi-project.ts

Large diffs are not rendered by default.

47 changes: 34 additions & 13 deletions packages/opencode/src/altimate/review/post-github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,31 @@ export function runCompleted(env: VerdictEnvelope): boolean {
const BOT_REVIEW_RE = /(Approved|Reviewed with comments|Changes requested)\b.*\((trivial|lite|full) tier/
/** Hidden marker in the body of every review this bot posts; reviews of a
* dbt project below the repository root also name it (`project=<dir>`,
* URI-encoded so a directory with spaces stays one token). */
const BOT_REVIEW_MARKER_RE = /<!-- altimate-code-review:verdict(?: project=(\S+))? -->/
* URI-encoded so a directory with spaces stays one token). An `--all-projects`
* review names every project it covers (`projects=<dir>,<dir>`, each
* URI-component-encoded, "." for the repository root). */
const BOT_REVIEW_MARKER_RE = /<!-- altimate-code-review:verdict(?: project=(\S+)| projects=(\S+))? -->/

function botReviewMarker(projectRoot: string | undefined): string {
function botReviewMarker(projectRoot: string | undefined, coveredProjects: string[] | undefined): string {
if (coveredProjects?.length)
return `<!-- altimate-code-review:verdict projects=${coveredProjects.map((r) => encodeURIComponent(r || ".")).join(",")} -->`
return projectRoot
? `<!-- altimate-code-review:verdict project=${encodeURI(projectRoot)} -->`
: "<!-- altimate-code-review:verdict -->"
}

function markerProjects(encoded: string): string[] {
return encoded.split(",").map((e) => {
let r = e
try {
r = decodeURIComponent(e)
} catch {
/* keep as written */
}
return r === "." ? "" : r
})
}

function markerProject(encoded: string | undefined): string {
if (!encoded) return ""
try {
Expand All @@ -105,20 +121,23 @@ function markerProject(encoded: string | undefined): string {

type ReviewLike = { body?: string | null; user?: { type?: string; login?: string } | null }

/** A review this bot posted for this dbt project. The author must be a bot
* (GitHub App / `github-actions[bot]`) or the token's own login: a human
* review that quotes the bot's body, marker included, is never the bot's.
* The marker then scopes it to a project, so in a monorepo one project's run
* never touches another project's review. A bot review without the marker
* (posted before it existed) matches every project. */
function isBotReview(r: ReviewLike, projectRoot: string | undefined, selfLogin: string | undefined): boolean {
/** A review this bot posted for the projects this run covers. The author must
* be a bot (GitHub App / `github-actions[bot]`) or the token's own login: a
* human review that quotes the bot's body, marker included, is never the bot's.
* The marker then scopes it to projects, so in a monorepo one project's run
* never touches another project's review: it is this run's only when every
* project it names is in `covered` (one project's root, or every project an
* `--all-projects` run covers). A bot review without the marker (posted
* before it existed) matches every project. */
function isBotReview(r: ReviewLike, covered: Set<string>, selfLogin: string | undefined): boolean {
const body = r.body ?? ""
if (!BOT_REVIEW_RE.test(body)) return false
const byBot = r.user?.type === "Bot" || (!!selfLogin && r.user?.login === selfLogin)
if (!byBot) return false
const marker = BOT_REVIEW_MARKER_RE.exec(body)
if (!marker) return true
return markerProject(marker[1]) === (projectRoot ?? "")
const named = marker[2] !== undefined ? markerProjects(marker[2]) : [markerProject(marker[1])]
return named.every((p) => covered.has(p))
}

/** True only when the run is known to have reviewed the PR's current head:
Expand Down Expand Up @@ -158,6 +177,8 @@ export async function postGitHubReview(
const { owner, repo, prNumber } = target
const result: PostResult = { inlineFellBack: false }
const projectRoot = env.scope?.projectRoot || undefined
const coveredProjects = env.scope?.coveredProjects
const covered = new Set(coveredProjects?.length ? coveredProjects : [projectRoot ?? ""])

// 1. Read what is already on the PR. Rerun dedup: a finding already
// commented inline (same fingerprint, which ignores line numbers) is not
Expand All @@ -173,7 +194,7 @@ export async function postGitHubReview(
posted = postedFindingIds(reviewComments.map((c) => c.body))
const needsLogin = reviews.some((r) => r.user?.type !== "Bot" && BOT_REVIEW_MARKER_RE.test(r.body ?? ""))
const selfLogin = needsLogin ? await tokenLogin(octo) : undefined
const botReviews = reviews.filter((r) => isBotReview(r, projectRoot, selfLogin))
const botReviews = reviews.filter((r) => isBotReview(r, covered, selfLogin))
priorBotReview = botReviews.length > 0
staleBlocking = botReviews.filter((r) => r.state === "CHANGES_REQUESTED").map((r) => r.id)
} catch {
Expand Down Expand Up @@ -209,7 +230,7 @@ export async function postGitHubReview(
// COMMENT event — the bot must never submit a formal GitHub approval.
const event = VCS_EVENT[env.verdict]
const headline = verdictHeadline(env)
const reviewBody = `${headline}\n\n${botReviewMarker(projectRoot)}`
const reviewBody = `${headline}\n\n${botReviewMarker(projectRoot, coveredProjects)}`
const { comments } = inlineComments(env, posted)
const posting = postingPolicy(env)
result.inlineSkippedAsPosted = env.findings.filter((f) => inlineEligible(f, posting) && posted.has(f.id)).length
Expand Down
33 changes: 24 additions & 9 deletions packages/opencode/src/altimate/review/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export interface ReviewPullRequestOptions {
head?: string
/** Override the manifest path from config. */
manifestPath?: string
/** Directories whose `.altimate/review` policy applies, in order, when `cwd` has none
* (`--all-projects`: the invocation directory, then the repository root). */
policyFallbackDirs?: string[]
/** Override the review mode from config. */
mode?: ReviewMode
/** Override the minimum severity to surface. */
Expand All @@ -72,6 +75,8 @@ export interface ReviewPullRequestOptions {
/** G2 — [EXPERIMENTAL] bypass the tier classifier with the supplied tier.
* Envelope carries `tierForced: true` when used. Debug / bench only. */
forceTier?: "trivial" | "lite" | "full"
/** Every file the PR changes, when `changedFiles` is a subset of it (multi-project review). */
prFiles?: string[]
}

/** dbt adapter_type → core SQL dialect. Mostly identity; a few aliases. */
Expand Down Expand Up @@ -256,14 +261,24 @@ export async function reviewPullRequest(opts: ReviewPullRequestOptions): Promise

// The policy (.altimate/review.yml) comes from the BASE commit, so the PR
// under review cannot relax its own rules; what it would change is listed.
const cwdRel = await repoRelative(gitRoot, opts.cwd, true)
const policy = await loadReviewPolicy({
cwd: opts.cwd,
readBase:
resolved && cwdRel !== undefined
? (rel) => showAtRef(opts.cwd, resolved.sha, path.posix.join(cwdRel, rel))
: undefined,
})
// The first of cwd and `policyFallbackDirs` that has a policy file wins.
const policyAt = async (dir: string) => {
const dirRel = await repoRelative(gitRoot, dir, true)
return loadReviewPolicy({
cwd: dir,
readBase:
resolved && dirRel !== undefined ? (rel) => showAtRef(opts.cwd, resolved.sha, path.posix.join(dirRel, rel)) : undefined,
})
}
let policy = await policyAt(opts.cwd)
const policyChangedHere = policy.changedInPr
for (const dir of opts.policyFallbackDirs ?? []) {
if (policy.source !== "default") break
policy = await policyAt(dir)
}
// A policy file the PR adds next to the project would replace the fallback: list it too.
if (policyChangedHere.length && policy.changedInPr !== policyChangedHere)
policy.changedInPr = [...new Set([...policy.changedInPr, ...policyChangedHere])]
const config = policy.config
if (policy.changedInPr.length) {
process.stderr.write(
Expand Down Expand Up @@ -466,7 +481,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)
},
prFiles: collected.map((f) => f.path),
prFiles: opts.prFiles ?? collected.map((f) => f.path),
prTitle: opts.prTitle,
prBody: opts.prBody,
explainTier: opts.explainTier,
Expand Down
33 changes: 32 additions & 1 deletion packages/opencode/src/altimate/review/verdict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,25 @@ export const ReviewScope = z.object({
* project, moved, not a dbt file, no check at this tier, no manifest), with
* the reason; also repeats the `unverified` entries. */
notReviewed: z.array(z.object({ path: z.string(), reason: z.string() })).default([]),
/** Multi-project review (`--all-projects`): every dbt project root the review
* speaks for (the projects the PR does not touch have nothing to flag). The
* poster treats earlier bot reviews of these projects as this review's own. */
coveredProjects: z.array(z.string()).optional(),
/** Multi-project review (`--all-projects`): each dbt project reviewed, its
* changed-file count and its own verdict. */
projects: z
.array(
z.object({
root: z.string(),
files: z.number().int().nonnegative(),
verdict: Verdict,
mode: ReviewMode.optional(),
/** The project's own manifest and policy (the top level keeps one only when all agree). */
manifestHash: z.string().optional(),
policyHash: z.string().optional(),
}),
)
.optional(),
})
export type ReviewScope = z.infer<typeof ReviewScope>

Expand Down Expand Up @@ -233,6 +252,17 @@ export const VerdictEnvelope = z.object({
/** Estimated USD cost from the model's price table. */
cost: z.number().nonnegative().optional(),
calls: z.number().int().nonnegative().optional(),
/** Multi-project review (`--all-projects`): each project's AI lane state. */
projects: z
.array(
z.object({
root: z.string(),
state: z.string(),
reason: z.string().optional(),
calls: z.number().int().nonnegative().optional(),
}),
)
.optional(),
/** Generator findings replayed from an earlier envelope: tokens, cost and calls are that run's. */
replayed: z.boolean().optional(),
/** The replayed envelope did not list every generator finding. */
Expand Down Expand Up @@ -390,7 +420,8 @@ export interface BuildEnvelopeInput {
engineErrors?: VerdictEnvelope["engineErrors"]
}

function summarize(findings: Finding[], degraded: boolean): VerdictEnvelope["summary"] {
/** Severity tally of `findings` for the envelope summary. */
export function summarize(findings: Finding[], degraded: boolean): VerdictEnvelope["summary"] {
const tally: Record<Severity, number> = { critical: 0, warning: 0, suggestion: 0 }
for (const f of findings) tally[f.severity]++
return { critical: tally.critical, warning: tally.warning, suggestion: tally.suggestion, degraded }
Expand Down
11 changes: 10 additions & 1 deletion packages/opencode/src/cli/cmd/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { cmd } from "./cmd"
import { bootstrap } from "../bootstrap"
import { Installation } from "../../installation"
import { prContextFromEnv, reviewPullRequest } from "../../altimate/review/run"
import { reviewAllProjects } from "../../altimate/review/multi-project"
import { renderSummary } from "../../altimate/review/format"
import { postGitHubReview, resolveGitHubTarget } from "../../altimate/review/post-github"
// altimate_change — review feature telemetry
Expand Down Expand Up @@ -70,6 +71,13 @@ export const ReviewCommand = cmd({
type: "string",
describe: "provider/model for the advisory AI lane (default: the configured default model)",
})
.option("all-projects", {
type: "boolean",
default: false,
describe:
"review every dbt project the PR changes (mono-repo) and emit one merged verdict / one comment; " +
"--manifest then applies only to the project that contains it",
})
.option("cwd", { type: "string", describe: "project directory (default: current dir)" }),
async handler(args) {
const cwd = (args.cwd as string) || process.cwd()
Expand Down Expand Up @@ -100,7 +108,8 @@ export const ReviewCommand = cmd({
const prContext = await prContextFromEnv()
let env
try {
env = await reviewPullRequest({
const review = args.allProjects === true ? reviewAllProjects : reviewPullRequest

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: --all-projects can silently skip a dbt project deleted by the PR because discovery only scans the head tree. Discover projects from both head and merge-base, or retain base roots that own deleted paths.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/opencode/src/cli/cmd/review.ts, line 111:

<comment>`--all-projects` can silently skip a dbt project deleted by the PR because discovery only scans the head tree. Discover projects from both head and merge-base, or retain base roots that own deleted paths.</comment>

<file context>
@@ -100,7 +108,8 @@ export const ReviewCommand = cmd({
       let env
       try {
-        env = await reviewPullRequest({
+        const review = args.allProjects === true ? reviewAllProjects : reviewPullRequest
+        env = await review({
           ...prContext,
</file context>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up: low priority, tracked. Discovering roots at the merge-base as well would cover it.

env = await review({
...prContext,
aiModel: args.aiModel as string | undefined,
cwd,
Expand Down
Loading
Loading