feat: split getPhaseReviewDiff into metadata + add getFileDiff procedure

Rewrites getPhaseReviewDiff to return file-level metadata (path, status,
additions, deletions) instead of a raw diff string, eliminating 10MB+
payloads for large repos. Adds getFileDiff for on-demand per-file hunk
content with binary detection via numstat. Multi-project initiatives
prefix file paths with the project name to avoid collisions.

Adds integration tests that use real local git repos + in-memory SQLite
to verify both procedures end-to-end (binary files, deleted files,
spaces in paths, error cases).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lukas May
2026-03-06 19:45:57 +01:00
parent 05eb160749
commit 4890721a92
3 changed files with 331 additions and 7 deletions

View File

@@ -4,11 +4,13 @@
import { TRPCError } from '@trpc/server';
import { z } from 'zod';
import { simpleGit } from 'simple-git';
import type { Phase } from '../../db/schema.js';
import type { ProcedureBuilder } from '../trpc.js';
import { requirePhaseRepository, requireTaskRepository, requireBranchManager, requireInitiativeRepository, requireProjectRepository, requireExecutionOrchestrator, requireReviewCommentRepository, requireChangeSetRepository } from './_helpers.js';
import { phaseBranchName } from '../../git/branch-naming.js';
import { ensureProjectClone } from '../../git/project-clones.js';
import type { FileStatEntry } from '../../git/types.js';
export function phaseProcedures(publicProcedure: ProcedureBuilder) {
return {
@@ -230,28 +232,93 @@ export function phaseProcedures(publicProcedure: ProcedureBuilder) {
const initBranch = initiative.branch;
const phBranch = phaseBranchName(initBranch, phase.name);
// For completed phases, use stored merge base; for pending_review, use initiative branch
const diffBase = (phase.status === 'completed' && phase.mergeBase) ? phase.mergeBase : initBranch;
const projects = await projectRepo.findProjectsByInitiativeId(phase.initiativeId);
let rawDiff = '';
const files: FileStatEntry[] = [];
for (const project of projects) {
const clonePath = await ensureProjectClone(project, ctx.workspaceRoot!);
const diff = await branchManager.diffBranches(clonePath, diffBase, phBranch);
if (diff) {
rawDiff += diff + '\n';
const entries = await branchManager.diffBranchesStat(clonePath, diffBase, phBranch);
for (const entry of entries) {
const tagged: FileStatEntry = { ...entry, projectId: project.id };
if (projects.length > 1) {
tagged.path = `${project.name}/${entry.path}`;
if (entry.oldPath) {
tagged.oldPath = `${project.name}/${entry.oldPath}`;
}
}
files.push(tagged);
}
}
const totalAdditions = files.reduce((sum, f) => sum + f.additions, 0);
const totalDeletions = files.reduce((sum, f) => sum + f.deletions, 0);
return {
phaseName: phase.name,
sourceBranch: phBranch,
targetBranch: initBranch,
rawDiff,
files,
totalAdditions,
totalDeletions,
};
}),
getFileDiff: publicProcedure
.input(z.object({
phaseId: z.string().min(1),
filePath: z.string().min(1),
projectId: z.string().optional(),
}))
.query(async ({ ctx, input }) => {
const phaseRepo = requirePhaseRepository(ctx);
const initiativeRepo = requireInitiativeRepository(ctx);
const projectRepo = requireProjectRepository(ctx);
const branchManager = requireBranchManager(ctx);
const phase = await phaseRepo.findById(input.phaseId);
if (!phase) {
throw new TRPCError({ code: 'NOT_FOUND', message: `Phase '${input.phaseId}' not found` });
}
if (phase.status !== 'pending_review' && phase.status !== 'completed') {
throw new TRPCError({ code: 'BAD_REQUEST', message: `Phase is not reviewable (status: ${phase.status})` });
}
const initiative = await initiativeRepo.findById(phase.initiativeId);
if (!initiative?.branch) {
throw new TRPCError({ code: 'BAD_REQUEST', message: 'Initiative has no branch configured' });
}
const initBranch = initiative.branch;
const phBranch = phaseBranchName(initBranch, phase.name);
const diffBase = (phase.status === 'completed' && phase.mergeBase) ? phase.mergeBase : initBranch;
const decodedPath = decodeURIComponent(input.filePath);
const projects = await projectRepo.findProjectsByInitiativeId(phase.initiativeId);
let clonePath: string;
if (input.projectId) {
const project = projects.find((p) => p.id === input.projectId);
if (!project) {
throw new TRPCError({ code: 'NOT_FOUND', message: `Project '${input.projectId}' not found for this phase` });
}
clonePath = await ensureProjectClone(project, ctx.workspaceRoot!);
} else {
clonePath = await ensureProjectClone(projects[0], ctx.workspaceRoot!);
}
const git = simpleGit(clonePath);
// Binary files appear as "-\t-\t<path>" in --numstat output
const numstatOut = await git.raw(['diff', '--numstat', `${diffBase}...${phBranch}`, '--', decodedPath]);
if (numstatOut.trim() && numstatOut.startsWith('-\t-\t')) {
return { binary: true, rawDiff: '' };
}
const rawDiff = await branchManager.diffFileSingle(clonePath, diffBase, phBranch, decodedPath);
return { binary: false, rawDiff };
}),
approvePhaseReview: publicProcedure
.input(z.object({ phaseId: z.string().min(1) }))
.mutation(async ({ ctx, input }) => {