From c540dc43617c0b33e9e130e127f4bf0ecbf116c7 Mon Sep 17 00:00:00 2001 From: RaghwendraSingh Date: Wed, 18 Nov 2026 21:10:29 +0530 Subject: [PATCH 1/2] feat(review): `--all-projects` reviews every dbt project a PR touches A mono-repo PR can change several dbt projects; until now the review only covered the project found from `--cwd` and listed every other project's files as "outside the dbt project". - `reviewAllProjects` (new `multi-project.ts`) collects the changed files once, discovers every `dbt_project.yml` at the head side (skipping `dbt_packages/`, `target/`, `node_modules/`), and assigns each file to the deepest project containing it. Each touched project is reviewed sequentially with `reviewPullRequest` (its own policy, layout and manifest auto-discovery; `--manifest` applies only to the project that contains it), seeing only its own files plus files outside every project. - The results merge into one signed envelope: findings concatenated and de-duplicated by id, the most severe verdict and highest tier, summary recomputed, the not-reviewed ledger unioned (other projects' files are no longer "outside"), engine errors combined, AI usage summed, and per-project roots/verdicts/AI states in `scope.projects` / `ai.projects`. - With 0 or 1 touched projects the single-project envelope is returned. - `altimate review --all-projects` wires it into the CLI; the summary adds a "Reviewed N dbt projects" line only when more than one was reviewed. - `reviewPullRequest` gains an optional `prFiles` (the full PR file list when `changedFiles` is a subset); the default path is unchanged. Co-Authored-By: Claude Opus 5.5 --- .../opencode/src/altimate/review/format.ts | 2 + .../opencode/src/altimate/review/index.ts | 1 + .../src/altimate/review/multi-project.ts | 226 ++++++++++++++++++ packages/opencode/src/altimate/review/run.ts | 4 +- .../opencode/src/altimate/review/verdict.ts | 10 +- packages/opencode/src/cli/cmd/review.ts | 11 +- .../test/altimate/review-all-projects.test.ts | 225 +++++++++++++++++ 7 files changed, 476 insertions(+), 3 deletions(-) create mode 100644 packages/opencode/src/altimate/review/multi-project.ts create mode 100644 packages/opencode/test/altimate/review-all-projects.test.ts diff --git a/packages/opencode/src/altimate/review/format.ts b/packages/opencode/src/altimate/review/format.ts index bf2e831947..aa7ef341f7 100644 --- a/packages/opencode/src/altimate/review/format.ts +++ b/packages/opencode/src/altimate/review/format.ts @@ -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(", ")}`, "") if (env.summary.degraded) { lines.push( diff --git a/packages/opencode/src/altimate/review/index.ts b/packages/opencode/src/altimate/review/index.ts index f668a88793..336421b30c 100644 --- a/packages/opencode/src/altimate/review/index.ts +++ b/packages/opencode/src/altimate/review/index.ts @@ -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" diff --git a/packages/opencode/src/altimate/review/multi-project.ts b/packages/opencode/src/altimate/review/multi-project.ts new file mode 100644 index 0000000000..1142db887c --- /dev/null +++ b/packages/opencode/src/altimate/review/multi-project.ts @@ -0,0 +1,226 @@ +import path from "node:path" +import { realpath } from "node:fs/promises" +import { collectChangedFiles, gitRepoRoot, listFilesAt, listWorkingTreeFiles, resolveReviewBase } from "./git" +import { reviewPullRequest, type ReviewPullRequestOptions } from "./run" +import { applyMode, signEnvelope, summarize, VerdictEnvelope, type RiskTier, type Verdict } from "./verdict" +import type { ChangedFile } from "./diff-filter" + +/** + * Multi-project review (`altimate review --all-projects`): a PR in a mono-repo + * can change several dbt projects. Each touched project is reviewed on its own + * (its policy, manifest and layout) and the results are merged into one signed + * verdict envelope, so the PR gets one comment. + */ + +const PROJECT_FILE = /(^|\/)dbt_project\.ya?ml$/ +const IGNORED_DIR = /(^|\/)(dbt_packages|target|node_modules)\// + +const VERDICT_RANK: Record = { APPROVE: 0, COMMENT: 1, REQUEST_CHANGES: 2 } +const TIER_RANK: Record = { trivial: 0, lite: 1, full: 2 } +const OUTSIDE_PROJECT = /^outside the dbt project/ +/** Reason for a file outside every reviewed project (the projects are listed in `scope.projects`). */ +const OUTSIDE_ALL = "outside the dbt projects reviewed" + +/** Repo-relative roots ("" = repo root) of every dbt project at the head side. */ +export async function discoverDbtProjects(gitRoot: string, head?: string): Promise { + const files = (head ? await listFilesAt(gitRoot, head) : await listWorkingTreeFiles(gitRoot)) ?? [] + const roots = new Set() + for (const f of files) { + if (!PROJECT_FILE.test(f) || IGNORED_DIR.test(f)) continue + roots.add(path.posix.dirname(f) === "." ? "" : path.posix.dirname(f)) + } + return [...roots].sort() +} + +/** The deepest project root containing the repo-relative path `p`, or undefined. */ +export function owningProject(p: string, roots: string[]): string | undefined { + let best: string | undefined + for (const r of roots) { + if (r !== "" && !p.startsWith(r + "/")) continue + if (best === undefined || r.length > best.length) best = r + } + return best +} + +/** Assign each changed file to the deepest project containing it (new path first, then the old path of a rename). */ +export function assignToProjects( + files: ChangedFile[], + roots: string[], +): { byProject: Map; unassigned: ChangedFile[] } { + const byProject = new Map() + const unassigned: ChangedFile[] = [] + for (const f of files) { + const root = owningProject(f.path, roots) ?? (f.oldPath ? owningProject(f.oldPath, roots) : undefined) + if (root === undefined) unassigned.push(f) + else byProject.set(root, [...(byProject.get(root) ?? []), f]) + } + return { byProject, unassigned } +} + +async function canonical(p: string): Promise { + try { + return await realpath(p) + } catch { + return path.resolve(p) + } +} + +/** `abs` relative to the repo root in POSIX form, or undefined when outside it. */ +function repoRel(gitRootReal: string, abs: string): string | undefined { + const rel = path.relative(gitRootReal, abs).replace(/\\/g, "/") + if (rel.startsWith("..") || path.isAbsolute(rel)) return undefined + return rel +} + +export async function reviewAllProjects(opts: ReviewPullRequestOptions): Promise { + const gitRoot = (await gitRepoRoot(opts.cwd)) ?? opts.cwd + const gitRootReal = await canonical(gitRoot) + + // Collect the PR's changed files once, against the same merge-base reviewPullRequest resolves. + const collected = + opts.changedFiles ?? + (await (async () => { + const resolved = await resolveReviewBase({ cwd: opts.cwd, base: opts.base, head: opts.head }) + return collectChangedFiles({ base: resolved.sha, head: opts.head, cwd: opts.cwd }) + })()) + + const roots = await discoverDbtProjects(gitRoot, opts.head) + const { byProject, unassigned } = assignToProjects(collected, roots) + const touched = roots.filter((r) => byProject.has(r)) + if (touched.length === 0) return reviewPullRequest(opts) + + // An explicit --manifest belongs to the project that contains it; the others auto-discover. + const manifestAbs = opts.manifestPath + ? await canonical(path.isAbsolute(opts.manifestPath) ? opts.manifestPath : path.join(opts.cwd, opts.manifestPath)) + : undefined + const manifestRel = manifestAbs ? repoRel(gitRootReal, manifestAbs) : undefined + const manifestOwner = manifestRel !== undefined ? owningProject(manifestRel, roots) : undefined + const optsFor = (root: string): ReviewPullRequestOptions => ({ + ...opts, + cwd: path.join(gitRootReal, root), + manifestPath: root === manifestOwner ? manifestAbs : undefined, + }) + + if (touched.length === 1) { + // Invoked from inside the touched project: exactly the single-project review. + const cwdRel = repoRel(gitRootReal, await canonical(opts.cwd)) + if (cwdRel !== undefined && owningProject(cwdRel + "/", roots) === touched[0]) return reviewPullRequest(opts) + return reviewPullRequest(optsFor(touched[0])) + } + + // Each project sees its own files plus the files outside every project (so + // those are listed as not reviewed); other projects' files are left out. + const prFiles = collected.map((f) => f.path) + const runs: ProjectRun[] = [] + for (const root of touched) { + const own = byProject.get(root)! + process.stderr.write(`ℹ️ reviewing dbt project ${root || "."} (${own.length} changed file(s))\n`) + const env = await reviewPullRequest({ ...optsFor(root), changedFiles: [...own, ...unassigned], prFiles }) + runs.push({ root, paths: own.map((f) => f.path), env }) + } + return mergeProjectEnvelopes(runs, opts.mode) +} + +/** One project's review: its repo-relative root, the changed files assigned to it, and its envelope. */ +export interface ProjectRun { + root: string + paths: string[] + env: VerdictEnvelope +} + +/** Merge per-project envelopes into one signed envelope (see reviewAllProjects). */ +export function mergeProjectEnvelopes( + runs: ProjectRun[], + modeOverride?: VerdictEnvelope["mode"], +): VerdictEnvelope { + const first = runs[0].env + const envs = runs.map((r) => r.env) + + const seenIds = new Set() + const findings = envs.flatMap((e) => e.findings).filter((f) => !seenIds.has(f.id) && !!seenIds.add(f.id)) + + const mode = modeOverride ?? (envs.some((e) => e.mode === "gate") ? "gate" : first.mode) + const idealVerdict = envs.map((e) => e.idealVerdict).reduce((a, b) => (VERDICT_RANK[b] > VERDICT_RANK[a] ? b : a)) + const tier = envs.map((e) => e.tier).reduce((a, b) => (TIER_RANK[b] > TIER_RANK[a] ? b : a)) + const tierReasons = [...new Set(envs.flatMap((e) => e.tierReasons ?? []))] + + // A path some project reviewed is not "outside the dbt project" for the PR. + const reviewed = new Set(runs.flatMap((r) => r.paths)) + const notReviewed: Array<{ path: string; reason: string }> = [] + const seenEntry = new Set() + for (const e of envs) { + for (const n of e.scope?.notReviewed ?? []) { + const outside = OUTSIDE_PROJECT.test(n.reason) + if (outside && reviewed.has(n.path)) continue + const key = outside ? `outside\0${n.path}` : `${n.path}\0${n.reason}` + if (seenEntry.has(key)) continue + seenEntry.add(key) + notReviewed.push(outside ? { path: n.path, reason: OUTSIDE_ALL } : n) + } + } + const outsideCount = notReviewed.filter((n) => OUTSIDE_PROJECT.test(n.reason)).length + const movedCount = envs.reduce((s, e) => s + (e.scope?.movedUnchangedFiles ?? 0), 0) + const unverified = envs.flatMap((e) => e.scope?.unverified ?? []) + + const engineErrors = new Map() + for (const err of envs.flatMap((e) => e.engineErrors ?? [])) { + const key = `${err.method}\0${err.reason}` + const prev = engineErrors.get(key) + engineErrors.set(key, prev ? { ...prev, count: prev.count + err.count } : { ...err }) + } + + const merged: VerdictEnvelope = { + ...first, + verdict: applyMode(idealVerdict, mode), + idealVerdict, + mode, + tier, + tierReasons: tierReasons.length ? tierReasons : undefined, + findings, + summary: summarize(findings, envs.some((e) => e.summary.degraded)), + staleManifest: envs.some((e) => e.staleManifest) ? true : undefined, + engineErrors: engineErrors.size ? [...engineErrors.values()] : undefined, + scope: { + ...first.scope, + outOfScopeFiles: outsideCount || undefined, + movedUnchangedFiles: movedCount || undefined, + unverified: unverified.length ? unverified : undefined, + notReviewed, + projects: runs.map((r) => ({ root: r.root, files: r.paths.length, verdict: r.env.verdict })), + }, + ai: mergeAi(runs), + signature: undefined, + } + return signEnvelope(VerdictEnvelope.parse(merged)) +} + +function mergeAi(runs: Array<{ root: string; env: VerdictEnvelope }>): VerdictEnvelope["ai"] { + const lanes = runs.filter((r) => r.env.ai) + if (!lanes.length) return undefined + const first = lanes[0].env.ai! + const ok = lanes.find((r) => r.env.ai!.state === "ok")?.env.ai + const sum = (pick: (a: NonNullable) => number | undefined) => { + const vals = lanes.map((r) => pick(r.env.ai!)).filter((v): v is number => v !== undefined) + return vals.length ? vals.reduce((a, b) => a + b, 0) : undefined + } + const tokenLanes = lanes.map((r) => r.env.ai!.tokens).filter((t) => t !== undefined) + const tokens = tokenLanes.length + ? tokenLanes.reduce((a, b) => ({ + input: a.input + b.input, + inputTotal: a.inputTotal + b.inputTotal, + output: a.output + b.output, + reasoning: a.reasoning + b.reasoning, + cacheRead: a.cacheRead + b.cacheRead, + })) + : undefined + const base = ok ?? first + return { + ...base, + model: base.model ?? first.model, + calls: sum((a) => a.calls), + tokens, + cost: sum((a) => a.cost), + durationMs: sum((a) => a.durationMs), + projects: lanes.map((r) => ({ root: r.root, state: r.env.ai!.state, calls: r.env.ai!.calls })), + } +} diff --git a/packages/opencode/src/altimate/review/run.ts b/packages/opencode/src/altimate/review/run.ts index e36f57912d..92184609db 100644 --- a/packages/opencode/src/altimate/review/run.ts +++ b/packages/opencode/src/altimate/review/run.ts @@ -71,6 +71,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. */ @@ -461,7 +463,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, diff --git a/packages/opencode/src/altimate/review/verdict.ts b/packages/opencode/src/altimate/review/verdict.ts index 78770ea8be..d0966b3856 100644 --- a/packages/opencode/src/altimate/review/verdict.ts +++ b/packages/opencode/src/altimate/review/verdict.ts @@ -148,6 +148,9 @@ 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`): 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 })).optional(), }) export type ReviewScope = z.infer @@ -222,6 +225,10 @@ 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(), 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. */ @@ -379,7 +386,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 = { 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 } diff --git a/packages/opencode/src/cli/cmd/review.ts b/packages/opencode/src/cli/cmd/review.ts index d1f58a3e65..603ee84a0e 100644 --- a/packages/opencode/src/cli/cmd/review.ts +++ b/packages/opencode/src/cli/cmd/review.ts @@ -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 @@ -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() @@ -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 + env = await review({ ...prContext, aiModel: args.aiModel as string | undefined, cwd, diff --git a/packages/opencode/test/altimate/review-all-projects.test.ts b/packages/opencode/test/altimate/review-all-projects.test.ts new file mode 100644 index 0000000000..4eddf3ff50 --- /dev/null +++ b/packages/opencode/test/altimate/review-all-projects.test.ts @@ -0,0 +1,225 @@ +import { describe, test, expect } from "bun:test" +import { promises as fs } from "node:fs" +import path from "node:path" +import { tmpdir } from "node:os" +import { execFile } from "node:child_process" +import { promisify } from "node:util" +import { reviewPullRequest } from "../../src/altimate/review/run" +import { + assignToProjects, + mergeProjectEnvelopes, + owningProject, + reviewAllProjects, +} from "../../src/altimate/review/multi-project" +import { buildEnvelope, signEnvelope, verifyEnvelope, type VerdictEnvelope } from "../../src/altimate/review/verdict" +import { makeFinding } from "../../src/altimate/review/finding" +import { renderSummary } from "../../src/altimate/review/format" +import type { ChangedFile } from "../../src/altimate/review/diff-filter" + +/** + * `altimate review --all-projects`: a mono-repo PR that changes several dbt + * projects is reviewed per project and merged into one signed envelope. Each + * test builds a synthetic git repo with two dbt projects in a temp dir. + */ + +const exec = promisify(execFile) +const g = (cwd: string, ...args: string[]) => exec("git", args, { cwd }).then((r) => r.stdout.trim()) + +async function write(root: string, rel: string, body: string) { + await fs.mkdir(path.dirname(path.join(root, rel)), { recursive: true }) + await fs.writeFile(path.join(root, rel), body) +} + +async function commitAll(root: string, msg: string) { + await g(root, "add", "-A") + await g(root, "commit", "-qm", msg) + return g(root, "rev-parse", "HEAD") +} + +const STAR_MODEL = "select * from {{ ref('stg') }}\n" +const ORDINAL_MACRO = "{% macro m() %}select a, count(*) from t group by 1{% endmacro %}\n" + +/** Two dbt projects (proj_a, proj_b), each with a model and a macro, plus a workflow file; returns the base sha. */ +async function fixture(opts: { nested?: boolean } = {}): Promise<{ root: string; base: string }> { + const root = await fs.realpath(await fs.mkdtemp(path.join(tmpdir(), "review-all-projects-"))) + await g(root, "init", "-q", "-b", "main") + await g(root, "config", "user.email", "t@test") + await g(root, "config", "user.name", "t") + await g(root, "config", "commit.gpgsign", "false") + const projects = ["proj_a", "proj_b", ...(opts.nested ? ["proj_a/integration_tests"] : [])] + for (const p of projects) { + await write(root, `${p}/dbt_project.yml`, `name: ${p.replace("/", "_")}\nversion: '1.0'\nprofile: x\n`) + await write(root, `${p}/models/orders.sql`, "select id, amount from {{ ref('stg') }}\n") + await write(root, `${p}/macros/m.sql`, "{% macro m() %}select 1{% endmacro %}\n") + } + await write(root, ".github/workflows/x.yml", "on: push\n") + return { root, base: await commitAll(root, "base") } +} + +const strip = (e: VerdictEnvelope) => ({ ...e, signature: undefined, generatedAt: undefined }) + +describe("reviewAllProjects: one envelope for every dbt project a PR touches", () => { + test("merges findings from both projects, scopes the ledger, and signs the result", async () => { + const { root, base } = await fixture() + await write(root, "proj_a/models/orders.sql", STAR_MODEL) + await write(root, "proj_b/models/orders.sql", STAR_MODEL) + await write(root, "proj_b/macros/m.sql", ORDINAL_MACRO) + await write(root, ".github/workflows/x.yml", "on: [push]\n") + await commitAll(root, "head") + + const env = await reviewAllProjects({ cwd: root, base, head: "HEAD", noAi: true }) + + const files = new Set(env.findings.map((f) => f.file)) + expect(files.has("proj_a/models/orders.sql")).toBe(true) + expect(files.has("proj_b/models/orders.sql")).toBe(true) + expect(files.has("proj_b/macros/m.sql")).toBe(true) + expect(new Set(env.findings.map((f) => f.id)).size).toBe(env.findings.length) + + expect(env.scope?.projects?.map((p) => p.root)).toEqual(["proj_a", "proj_b"]) + expect(env.scope?.projects?.map((p) => p.files)).toEqual([1, 2]) + expect(env.scope?.projectRoot).toBe("proj_a") + const outside = (env.scope?.notReviewed ?? []).filter((n) => /^outside the dbt project/.test(n.reason)) + expect(outside.map((n) => n.path)).toEqual([".github/workflows/x.yml"]) + expect(env.scope?.outOfScopeFiles).toBe(1) + + const rank = { APPROVE: 0, COMMENT: 1, REQUEST_CHANGES: 2 } + const worst = env.scope!.projects!.map((p) => p.verdict).reduce((a, b) => (rank[b] > rank[a] ? b : a)) + expect(env.verdict).toBe(worst) + const sev = (s: string) => env.findings.filter((f) => f.severity === s).length + expect(env.summary.suggestion).toBe(sev("suggestion")) + expect(env.summary.warning).toBe(sev("warning")) + expect(env.ai?.projects?.map((p) => p.state)).toEqual(["skipped", "skipped"]) + expect(verifyEnvelope(env)).toBe(true) + + expect(renderSummary(env, { maxChars: Infinity, inline: false, allowSilent: false })).toContain( + "Reviewed 2 dbt projects: proj_a, proj_b", + ) + }, 60_000) + + test("a PR touching one project returns the single-project envelope", async () => { + const { root, base } = await fixture() + await write(root, "proj_a/models/orders.sql", STAR_MODEL) + await write(root, ".github/workflows/x.yml", "on: [push]\n") + await commitAll(root, "head") + const cwd = path.join(root, "proj_a") + + const single = await reviewPullRequest({ cwd, base, head: "HEAD", noAi: true }) + const all = await reviewAllProjects({ cwd, base, head: "HEAD", noAi: true }) + expect(strip(all)).toEqual(strip(single)) + expect(all.scope?.projects).toBeUndefined() + // From the repo root the one touched project is reviewed as if invoked from it. + const fromRoot = await reviewAllProjects({ cwd: root, base, head: "HEAD", noAi: true }) + expect(strip(fromRoot)).toEqual(strip(single)) + expect(renderSummary(all, { maxChars: Infinity, inline: false, allowSilent: false })).not.toContain("dbt projects:") + }, 60_000) + + test("a file goes to the deepest project containing it", async () => { + const { root, base } = await fixture({ nested: true }) + await write(root, "proj_a/macros/m.sql", ORDINAL_MACRO) + await write(root, "proj_a/integration_tests/models/orders.sql", STAR_MODEL) + await commitAll(root, "head") + + const env = await reviewAllProjects({ cwd: root, base, head: "HEAD", noAi: true }) + expect(env.scope?.projects?.map((p) => [p.root, p.files])).toEqual([ + ["proj_a", 1], + ["proj_a/integration_tests", 1], + ]) + expect(env.findings.some((f) => f.file === "proj_a/integration_tests/models/orders.sql")).toBe(true) + expect(env.findings.filter((f) => f.file === "proj_a/integration_tests/models/orders.sql").length).toBe( + new Set(env.findings.filter((f) => f.file === "proj_a/integration_tests/models/orders.sql").map((f) => f.id)).size, + ) + }, 60_000) + + test("--manifest applies only to the project that contains it", async () => { + const { root, base } = await fixture() + await write(root, "proj_a/models/orders.sql", STAR_MODEL) + await write(root, "proj_b/models/orders.sql", STAR_MODEL) + await commitAll(root, "head") + // Outside target/, so auto-discovery cannot find it: only the flag supplies it. + // The merged envelope carries the first project's (proj_a) manifest hash, + // so it shows whether proj_a received the manifest. + const body = JSON.stringify({ metadata: { adapter_type: "postgres" }, nodes: {}, sources: {} }) + for (const p of ["proj_a", "proj_b"]) await write(root, `${p}/artifacts/manifest.json`, body) + const run = (owner: string) => + reviewAllProjects({ cwd: root, base, head: "HEAD", noAi: true, manifestPath: path.join(root, owner, "artifacts", "manifest.json") }) + + expect((await run("proj_a")).manifestHash).toBeDefined() + // A relative --manifest resolves against --cwd, as in the single-project path. + const rel = await reviewAllProjects({ cwd: root, base, head: "HEAD", noAi: true, manifestPath: "proj_a/artifacts/manifest.json" }) + expect(rel.manifestHash).toBeDefined() + expect((await run("proj_b")).manifestHash).toBeUndefined() + }, 60_000) +}) + +describe("mergeProjectEnvelopes", () => { + const finding = (file: string, severity: "critical" | "suggestion", tool: string) => + makeFinding({ severity, category: "semantic_change", title: `t ${file}`, body: "b", file, confidence: "high", evidence: { tool, result: {} } }) + + test("takes the most severe verdict and highest tier, sums AI usage, de-duplicates findings by id", () => { + const blocking = finding("b/models/x.sql", "critical", "altimate_core.equivalence") + const shared = finding("a/models/y.sql", "suggestion", "rule-catalog") + const a = signEnvelope( + buildEnvelope({ + findings: [shared], + tier: "lite", + mode: "gate", + ai: { state: "skipped", reason: "x", calls: 0 }, + scope: { projectRoot: "a", notReviewed: [{ path: "b/models/x.sql", reason: "outside the dbt project (a)" }] }, + }), + ) + const b = signEnvelope( + buildEnvelope({ + findings: [blocking, shared], + tier: "full", + mode: "gate", + ai: { + state: "ok", + model: "p/m", + calls: 2, + cost: 0.5, + tokens: { input: 1, inputTotal: 2, output: 3, reasoning: 4, cacheRead: 1 }, + }, + scope: { projectRoot: "b", notReviewed: [{ path: "a/models/y.sql", reason: "outside the dbt project (b)" }] }, + }), + ) + expect(a.verdict).toBe("COMMENT") + expect(b.verdict).toBe("REQUEST_CHANGES") + + const merged = mergeProjectEnvelopes([ + { root: "a", paths: ["a/models/y.sql"], env: a }, + { root: "b", paths: ["b/models/x.sql"], env: b }, + ]) + expect(merged.verdict).toBe("REQUEST_CHANGES") + expect(merged.idealVerdict).toBe("REQUEST_CHANGES") + expect(merged.tier).toBe("full") + expect(merged.findings.map((f) => f.id)).toEqual([shared.id, blocking.id]) + expect(merged.summary).toMatchObject({ critical: 1, suggestion: 1, warning: 0 }) + expect(merged.scope?.notReviewed).toEqual([]) + expect(merged.scope?.outOfScopeFiles).toBeUndefined() + expect(merged.ai).toMatchObject({ state: "ok", model: "p/m", calls: 2, cost: 0.5 }) + expect(merged.ai?.tokens).toEqual({ input: 1, inputTotal: 2, output: 3, reasoning: 4, cacheRead: 1 }) + expect(merged.ai?.projects).toEqual([ + { root: "a", state: "skipped", calls: 0 }, + { root: "b", state: "ok", calls: 2 }, + ]) + expect(verifyEnvelope(merged)).toBe(true) + + // A comment-mode override softens the merged ideal verdict. + expect(mergeProjectEnvelopes([{ root: "a", paths: [], env: a }, { root: "b", paths: [], env: b }], "comment").verdict).toBe("COMMENT") + }) + + test("assignment helpers pick the deepest root and fall back to a rename's old path", () => { + const roots = ["", "pkg", "pkg/integration_tests"] + expect(owningProject("pkg/macros/a.sql", roots)).toBe("pkg") + expect(owningProject("pkg/integration_tests/models/a.sql", roots)).toBe("pkg/integration_tests") + expect(owningProject("README.md", roots)).toBe("") + expect(owningProject("README.md", ["pkg"])).toBeUndefined() + const files: ChangedFile[] = [ + { path: "docs/a.sql", oldPath: "pkg/models/a.sql", status: "renamed" } as ChangedFile, + { path: "other/x.yml", status: "modified" } as ChangedFile, + ] + const { byProject, unassigned } = assignToProjects(files, ["pkg"]) + expect(byProject.get("pkg")?.map((f) => f.path)).toEqual(["docs/a.sql"]) + expect(unassigned.map((f) => f.path)).toEqual(["other/x.yml"]) + }) +}) From 5470bfe8a925a8792fcfed3c475bb112eafe5648 Mon Sep 17 00:00:00 2001 From: RaghwendraSingh Date: Fri, 20 Nov 2026 10:25:23 +0530 Subject: [PATCH 2/2] fix(review): address review comments: --all-projects manifest, mode, envelope, AI state, path listing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - An explicit `--manifest` outside every project (CI `$RUNNER_TEMP/manifest.json`) now goes to the touched project when the PR touches only one; when it can't be attributed (several touched projects, or it belongs to an untouched one) a warning says no project uses it instead of silently degrading to lint-only. - Merged verdict: the worst of each project's post-mode verdict, so a comment-mode project's REQUEST_CHANGES no longer blocks because a sibling uses gate mode. `scope.projects[]` records each project's `mode`. - Merged envelope is built explicitly instead of spreading the first project: `manifestHash` and `policy` are kept only when every project agrees (otherwise `policy` carries the least-trusted source, a combined hash, the union of `changedInPr` and a shared posting policy); `scope.projectRoot` is dropped; each project's `manifestHash` and `policyHash` go in `scope.projects[]`; `tierClassified` is the highest classified tier. - Merged AI state is the worst project state (`error` > `timeout` > `ok` > `skipped`) with that project's reason, so a sibling's failure is not hidden by another project's `ok`; `ai.projects[]` carries each `reason`. - `listFilesAt` / `listWorkingTreeFiles` use `-z`, so `dbt_project.yml` under non-ASCII directory names is discovered (git C-quoted those paths). - `repoRel` rejects only `..` and `../…`, not in-repo names such as `..foo`. Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/altimate/review/git.ts | 9 +- .../src/altimate/review/multi-project.ts | 83 ++++++++++++++-- .../opencode/src/altimate/review/verdict.ts | 23 ++++- .../test/altimate/review-all-projects.test.ts | 98 +++++++++++++++++-- 4 files changed, 190 insertions(+), 23 deletions(-) diff --git a/packages/opencode/src/altimate/review/git.ts b/packages/opencode/src/altimate/review/git.ts index a675ece0e8..6927075de4 100644 --- a/packages/opencode/src/altimate/review/git.ts +++ b/packages/opencode/src/altimate/review/git.ts @@ -171,8 +171,9 @@ export async function gitRepoRoot(cwd: string): Promise { /** 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 { 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 } @@ -181,8 +182,8 @@ export async function listFilesAt(cwd: string, ref?: string): Promise { 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 } diff --git a/packages/opencode/src/altimate/review/multi-project.ts b/packages/opencode/src/altimate/review/multi-project.ts index 1142db887c..ad17b44082 100644 --- a/packages/opencode/src/altimate/review/multi-project.ts +++ b/packages/opencode/src/altimate/review/multi-project.ts @@ -1,5 +1,6 @@ import path from "node:path" import { realpath } from "node:fs/promises" +import { createHash } from "node:crypto" import { collectChangedFiles, gitRepoRoot, listFilesAt, listWorkingTreeFiles, resolveReviewBase } from "./git" import { reviewPullRequest, type ReviewPullRequestOptions } from "./run" import { applyMode, signEnvelope, summarize, VerdictEnvelope, type RiskTier, type Verdict } from "./verdict" @@ -68,7 +69,7 @@ async function canonical(p: string): Promise { /** `abs` relative to the repo root in POSIX form, or undefined when outside it. */ function repoRel(gitRootReal: string, abs: string): string | undefined { const rel = path.relative(gitRootReal, abs).replace(/\\/g, "/") - if (rel.startsWith("..") || path.isAbsolute(rel)) return undefined + if (rel === ".." || rel.startsWith("../") || path.isAbsolute(rel)) return undefined return rel } @@ -95,18 +96,28 @@ export async function reviewAllProjects(opts: ReviewPullRequestOptions): Promise : undefined const manifestRel = manifestAbs ? repoRel(gitRootReal, manifestAbs) : undefined const manifestOwner = manifestRel !== undefined ? owningProject(manifestRel, roots) : undefined + // A manifest outside every project (CI's `$RUNNER_TEMP/manifest.json`) can't be matched by + // path; it goes to the touched project when there is only one. + const manifestFor = (root: string) => + root === manifestOwner || (manifestOwner === undefined && touched.length === 1) ? manifestAbs : undefined const optsFor = (root: string): ReviewPullRequestOptions => ({ ...opts, cwd: path.join(gitRootReal, root), - manifestPath: root === manifestOwner ? manifestAbs : undefined, + manifestPath: manifestFor(root), }) if (touched.length === 1) { // Invoked from inside the touched project: exactly the single-project review. const cwdRel = repoRel(gitRootReal, await canonical(opts.cwd)) if (cwdRel !== undefined && owningProject(cwdRel + "/", roots) === touched[0]) return reviewPullRequest(opts) - return reviewPullRequest(optsFor(touched[0])) } + if (manifestAbs && !touched.some((r) => manifestFor(r))) { + const where = manifestOwner === undefined ? "is outside every dbt project" : `belongs to ${manifestOwner || "."}, which this PR does not change` + process.stderr.write( + `⚠️ --manifest ${opts.manifestPath} ${where}, so no reviewed project uses it; each project auto-discovers its own. Place the manifest inside the project it describes (e.g. its target/), or review that project on its own.\n`, + ) + } + if (touched.length === 1) return reviewPullRequest(optsFor(touched[0])) // Each project sees its own files plus the files outside every project (so // those are listed as not reviewed); other projects' files are left out. @@ -140,7 +151,10 @@ export function mergeProjectEnvelopes( const findings = envs.flatMap((e) => e.findings).filter((f) => !seenIds.has(f.id) && !!seenIds.add(f.id)) const mode = modeOverride ?? (envs.some((e) => e.mode === "gate") ? "gate" : first.mode) - const idealVerdict = envs.map((e) => e.idealVerdict).reduce((a, b) => (VERDICT_RANK[b] > VERDICT_RANK[a] ? b : a)) + const worst = (vs: Verdict[]) => vs.reduce((a, b) => (VERDICT_RANK[b] > VERDICT_RANK[a] ? b : a)) + const idealVerdict = worst(envs.map((e) => e.idealVerdict)) + // Each project's verdict already has its own mode applied: a comment-mode project never blocks. + const verdict = applyMode(worst(envs.map((e) => e.verdict)), mode) const tier = envs.map((e) => e.tier).reduce((a, b) => (TIER_RANK[b] > TIER_RANK[a] ? b : a)) const tierReasons = [...new Set(envs.flatMap((e) => e.tierReasons ?? []))] @@ -169,31 +183,71 @@ export function mergeProjectEnvelopes( engineErrors.set(key, prev ? { ...prev, count: prev.count + err.count } : { ...err }) } + // Per-project values are recorded in `scope.projects`; the top level carries one only when every project agrees. + const shared = (vals: T[]): T | undefined => + vals.every((v) => JSON.stringify(v) === JSON.stringify(vals[0])) ? vals[0] : undefined + const classified = envs.map((e) => e.tierClassified) const merged: VerdictEnvelope = { - ...first, - verdict: applyMode(idealVerdict, mode), + version: first.version, + verdict, idealVerdict, mode, tier, tierReasons: tierReasons.length ? tierReasons : undefined, + tierForced: first.tierForced, + tierClassified: classified.every((t) => t !== undefined) + ? classified.reduce((a, b) => (TIER_RANK[b!] > TIER_RANK[a!] ? b : a)) + : undefined, findings, summary: summarize(findings, envs.some((e) => e.summary.degraded)), + engine: first.engine, + manifestHash: shared(envs.map((e) => e.manifestHash)), staleManifest: envs.some((e) => e.staleManifest) ? true : undefined, engineErrors: engineErrors.size ? [...engineErrors.values()] : undefined, + policy: mergePolicy(envs), scope: { - ...first.scope, + baseRef: first.scope?.baseRef, + baseSha: first.scope?.baseSha, + baseSource: first.scope?.baseSource, outOfScopeFiles: outsideCount || undefined, movedUnchangedFiles: movedCount || undefined, unverified: unverified.length ? unverified : undefined, notReviewed, - projects: runs.map((r) => ({ root: r.root, files: r.paths.length, verdict: r.env.verdict })), + projects: runs.map((r) => ({ + root: r.root, + files: r.paths.length, + verdict: r.env.verdict, + mode: r.env.mode, + manifestHash: r.env.manifestHash, + policyHash: r.env.policy?.hash, + })), }, ai: mergeAi(runs), - signature: undefined, + generatedAt: first.generatedAt, } return signEnvelope(VerdictEnvelope.parse(merged)) } +/** Least-trusted source first: a policy the PR controls outranks the defaults, which outrank the base branch's. */ +const POLICY_SOURCE_RANK = { base: 0, default: 1, working_tree: 2 } as const + +/** One project's policy when all agree; otherwise the least-trusted source, a hash of every + * project's hash, every setting the PR changes, and the posting policy only if shared. */ +function mergePolicy(envs: VerdictEnvelope[]): VerdictEnvelope["policy"] { + const policies = envs.map((e) => e.policy) + if (policies.some((p) => !p)) return undefined + const all = policies as NonNullable[] + if (all.every((p) => JSON.stringify(p) === JSON.stringify(all[0]))) return all[0] + const changed = [...new Set(all.flatMap((p) => p.changedInPr ?? []))] + const postings = all.map((p) => JSON.stringify(p.posting)) + return { + source: all.map((p) => p.source).reduce((a, b) => (POLICY_SOURCE_RANK[b] > POLICY_SOURCE_RANK[a] ? b : a)), + hash: createHash("sha256").update(all.map((p) => p.hash).join("\n")).digest("hex").slice(0, 16), + changedInPr: changed.length ? changed : undefined, + posting: postings.every((x) => x === postings[0]) ? all[0].posting : undefined, + } +} + function mergeAi(runs: Array<{ root: string; env: VerdictEnvelope }>): VerdictEnvelope["ai"] { const lanes = runs.filter((r) => r.env.ai) if (!lanes.length) return undefined @@ -213,14 +267,23 @@ function mergeAi(runs: Array<{ root: string; env: VerdictEnvelope }>): VerdictEn cacheRead: a.cacheRead + b.cacheRead, })) : undefined + // The worst state wins (a sibling's error or timeout is not hidden by another project's ok), + // with that project's reason; `skipped` (nothing to send) ranks below `ok`. + const worst = lanes.reduce((a, b) => (AI_STATE_RANK[b.env.ai!.state] > AI_STATE_RANK[a.env.ai!.state] ? b : a)) + const state = worst.env.ai!.state + const reason = worst.env.ai!.reason const base = ok ?? first return { ...base, + state, + reason: state === "ok" ? base.reason : reason && lanes.length > 1 ? `${worst.root || "."}: ${reason}` : reason, model: base.model ?? first.model, calls: sum((a) => a.calls), tokens, cost: sum((a) => a.cost), durationMs: sum((a) => a.durationMs), - projects: lanes.map((r) => ({ root: r.root, state: r.env.ai!.state, calls: r.env.ai!.calls })), + projects: lanes.map((r) => ({ root: r.root, state: r.env.ai!.state, reason: r.env.ai!.reason, calls: r.env.ai!.calls })), } } + +const AI_STATE_RANK: Record["state"], number> = { skipped: 0, ok: 1, timeout: 2, error: 3 } diff --git a/packages/opencode/src/altimate/review/verdict.ts b/packages/opencode/src/altimate/review/verdict.ts index d0966b3856..303d3d3112 100644 --- a/packages/opencode/src/altimate/review/verdict.ts +++ b/packages/opencode/src/altimate/review/verdict.ts @@ -150,7 +150,19 @@ export const ReviewScope = z.object({ notReviewed: z.array(z.object({ path: z.string(), reason: z.string() })).default([]), /** 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 })).optional(), + 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 @@ -227,7 +239,14 @@ export const VerdictEnvelope = z.object({ 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(), calls: z.number().int().nonnegative().optional() })) + .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(), diff --git a/packages/opencode/test/altimate/review-all-projects.test.ts b/packages/opencode/test/altimate/review-all-projects.test.ts index 4eddf3ff50..fdb178cf1e 100644 --- a/packages/opencode/test/altimate/review-all-projects.test.ts +++ b/packages/opencode/test/altimate/review-all-projects.test.ts @@ -7,6 +7,7 @@ import { promisify } from "node:util" import { reviewPullRequest } from "../../src/altimate/review/run" import { assignToProjects, + discoverDbtProjects, mergeProjectEnvelopes, owningProject, reviewAllProjects, @@ -77,7 +78,8 @@ describe("reviewAllProjects: one envelope for every dbt project a PR touches", ( expect(env.scope?.projects?.map((p) => p.root)).toEqual(["proj_a", "proj_b"]) expect(env.scope?.projects?.map((p) => p.files)).toEqual([1, 2]) - expect(env.scope?.projectRoot).toBe("proj_a") + // No single project root: the projects carry their own. + expect(env.scope?.projectRoot).toBeUndefined() const outside = (env.scope?.notReviewed ?? []).filter((n) => /^outside the dbt project/.test(n.reason)) expect(outside.map((n) => n.path)).toEqual([".github/workflows/x.yml"]) expect(env.scope?.outOfScopeFiles).toBe(1) @@ -136,18 +138,44 @@ describe("reviewAllProjects: one envelope for every dbt project a PR touches", ( await write(root, "proj_b/models/orders.sql", STAR_MODEL) await commitAll(root, "head") // Outside target/, so auto-discovery cannot find it: only the flag supplies it. - // The merged envelope carries the first project's (proj_a) manifest hash, - // so it shows whether proj_a received the manifest. const body = JSON.stringify({ metadata: { adapter_type: "postgres" }, nodes: {}, sources: {} }) for (const p of ["proj_a", "proj_b"]) await write(root, `${p}/artifacts/manifest.json`, body) + const hashes = (env: VerdictEnvelope) => env.scope?.projects?.map((p) => [p.root, !!p.manifestHash]) const run = (owner: string) => reviewAllProjects({ cwd: root, base, head: "HEAD", noAi: true, manifestPath: path.join(root, owner, "artifacts", "manifest.json") }) - expect((await run("proj_a")).manifestHash).toBeDefined() + const a = await run("proj_a") + expect(hashes(a)).toEqual([["proj_a", true], ["proj_b", false]]) + // The projects disagree, so the top level carries no manifest hash. + expect(a.manifestHash).toBeUndefined() // A relative --manifest resolves against --cwd, as in the single-project path. const rel = await reviewAllProjects({ cwd: root, base, head: "HEAD", noAi: true, manifestPath: "proj_a/artifacts/manifest.json" }) - expect(rel.manifestHash).toBeDefined() - expect((await run("proj_b")).manifestHash).toBeUndefined() + expect(hashes(rel)).toEqual([["proj_a", true], ["proj_b", false]]) + expect(hashes(await run("proj_b"))).toEqual([["proj_a", false], ["proj_b", true]]) + }, 60_000) + + test("--manifest outside every project (CI's $RUNNER_TEMP) goes to the one touched project", async () => { + const { root, base } = await fixture() + await write(root, "proj_a/models/orders.sql", STAR_MODEL) + await commitAll(root, "head") + const tmp = await fs.mkdtemp(path.join(tmpdir(), "review-manifest-")) + const manifestPath = path.join(tmp, "manifest.json") + await fs.writeFile(manifestPath, JSON.stringify({ metadata: { adapter_type: "postgres" }, nodes: {}, sources: {} })) + const env = await reviewAllProjects({ cwd: root, base, head: "HEAD", noAi: true, manifestPath }) + expect(env.manifestHash).toBeDefined() + // Two touched projects: it can't be attributed, so neither uses it (and a warning says so). + await write(root, "proj_b/models/orders.sql", STAR_MODEL) + await commitAll(root, "head2") + const two = await reviewAllProjects({ cwd: root, base, head: "HEAD", noAi: true, manifestPath }) + expect(two.scope?.projects?.map((p) => p.manifestHash)).toEqual([undefined, undefined]) + }, 60_000) + + test("projects are discovered under non-ASCII directory names (head tree and working tree)", async () => { + const { root } = await fixture() + await write(root, "projé/dbt_project.yml", "name: proj_e\nversion: '1.0'\nprofile: x\n") + await commitAll(root, "head") + expect(await discoverDbtProjects(root, "HEAD")).toEqual(["proj_a", "proj_b", "projé"]) + expect(await discoverDbtProjects(root)).toEqual(["proj_a", "proj_b", "projé"]) }, 60_000) }) @@ -199,7 +227,7 @@ describe("mergeProjectEnvelopes", () => { expect(merged.ai).toMatchObject({ state: "ok", model: "p/m", calls: 2, cost: 0.5 }) expect(merged.ai?.tokens).toEqual({ input: 1, inputTotal: 2, output: 3, reasoning: 4, cacheRead: 1 }) expect(merged.ai?.projects).toEqual([ - { root: "a", state: "skipped", calls: 0 }, + { root: "a", state: "skipped", reason: "x", calls: 0 }, { root: "b", state: "ok", calls: 2 }, ]) expect(verifyEnvelope(merged)).toBe(true) @@ -208,6 +236,62 @@ describe("mergeProjectEnvelopes", () => { expect(mergeProjectEnvelopes([{ root: "a", paths: [], env: a }, { root: "b", paths: [], env: b }], "comment").verdict).toBe("COMMENT") }) + test("a comment-mode project's REQUEST_CHANGES does not block a gate-mode merge", () => { + const soft = signEnvelope(buildEnvelope({ findings: [finding("a/models/x.sql", "critical", "altimate_core.equivalence")], tier: "full", mode: "comment" })) + const gate = signEnvelope(buildEnvelope({ findings: [finding("b/models/y.sql", "suggestion", "rule-catalog")], tier: "full", mode: "gate" })) + expect([soft.idealVerdict, soft.verdict, gate.verdict]).toEqual(["REQUEST_CHANGES", "COMMENT", "COMMENT"]) + const merged = mergeProjectEnvelopes([ + { root: "a", paths: [], env: soft }, + { root: "b", paths: [], env: gate }, + ]) + expect(merged.mode).toBe("gate") + expect(merged.verdict).toBe("COMMENT") + expect(merged.idealVerdict).toBe("REQUEST_CHANGES") + expect(merged.scope?.projects?.map((p) => p.mode)).toEqual(["comment", "gate"]) + }) + + test("a sibling's AI error is not hidden by another project's ok; differing policies and manifests stay per project", () => { + const env = (root: string, ai: VerdictEnvelope["ai"], hash: string, manifestHash: string) => + signEnvelope( + buildEnvelope({ + findings: [], + tier: "lite", + mode: "comment", + ai, + manifestHash, + policy: { source: root === "a" ? "base" : "working_tree", hash, posting: undefined }, + scope: { projectRoot: root, notReviewed: [] }, + }), + ) + const a = env("a", { state: "ok", model: "p/m", calls: 1 }, "h_a", "m_a") + const b = env("b", { state: "error", reason: "rate limited", calls: 1 }, "h_b", "m_b") + const merged = mergeProjectEnvelopes([ + { root: "a", paths: [], env: a }, + { root: "b", paths: [], env: b }, + ]) + expect(merged.ai).toMatchObject({ state: "error", reason: "b: rate limited", model: "p/m", calls: 2 }) + expect(merged.ai?.projects?.map((p) => [p.root, p.state, p.reason])).toEqual([ + ["a", "ok", undefined], + ["b", "error", "rate limited"], + ]) + expect(merged.manifestHash).toBeUndefined() + expect(merged.policy?.source).toBe("working_tree") + expect(merged.policy?.hash).not.toBe("h_a") + expect(merged.scope?.projectRoot).toBeUndefined() + expect(merged.scope?.projects?.map((p) => [p.manifestHash, p.policyHash])).toEqual([ + ["m_a", "h_a"], + ["m_b", "h_b"], + ]) + expect(verifyEnvelope(merged)).toBe(true) + // Identical policies are kept as they are. + const same = mergeProjectEnvelopes([ + { root: "a", paths: [], env: a }, + { root: "c", paths: [], env: env("a", { state: "ok", calls: 0 }, "h_a", "m_a") }, + ]) + expect(same.policy).toEqual(a.policy) + expect(same.manifestHash).toBe("m_a") + }) + test("assignment helpers pick the deepest root and fall back to a rename's old path", () => { const roots = ["", "pkg", "pkg/integration_tests"] expect(owningProject("pkg/macros/a.sql", roots)).toBe("pkg")