feat: add in-memory diff cache with TTL and commit-hash invalidation

Adds DiffCache<T> module, extends BranchManager with getHeadCommitHash,
and wires phase-level caching into getPhaseReviewDiff and getFileDiff.
Cache is invalidated in ExecutionOrchestrator after each task merges into
the phase branch, ensuring stale diffs are never served after new commits.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lukas May
2026-03-06 19:51:04 +01:00
parent 90978d631a
commit 0996073deb
6 changed files with 112 additions and 4 deletions

View File

@@ -11,6 +11,7 @@ import { requirePhaseRepository, requireTaskRepository, requireBranchManager, re
import { phaseBranchName } from '../../git/branch-naming.js';
import { ensureProjectClone } from '../../git/project-clones.js';
import type { FileStatEntry } from '../../git/types.js';
import { phaseMetaCache, fileDiffCache } from '../../review/diff-cache.js';
export function phaseProcedures(publicProcedure: ProcedureBuilder) {
return {
@@ -237,6 +238,16 @@ export function phaseProcedures(publicProcedure: ProcedureBuilder) {
const projects = await projectRepo.findProjectsByInitiativeId(phase.initiativeId);
const files: FileStatEntry[] = [];
if (projects.length === 0) {
return { phaseName: phase.name, sourceBranch: phBranch, targetBranch: initBranch, files: [], totalAdditions: 0, totalDeletions: 0 };
}
const firstClone = await ensureProjectClone(projects[0], ctx.workspaceRoot!);
const headHash = await branchManager.getHeadCommitHash(firstClone, phBranch);
const cacheKey = `${input.phaseId}:${headHash}`;
const cached = phaseMetaCache.get(cacheKey);
if (cached) return cached;
for (const project of projects) {
const clonePath = await ensureProjectClone(project, ctx.workspaceRoot!);
const entries = await branchManager.diffBranchesStat(clonePath, diffBase, phBranch);
@@ -255,7 +266,7 @@ export function phaseProcedures(publicProcedure: ProcedureBuilder) {
const totalAdditions = files.reduce((sum, f) => sum + f.additions, 0);
const totalDeletions = files.reduce((sum, f) => sum + f.deletions, 0);
return {
const result = {
phaseName: phase.name,
sourceBranch: phBranch,
targetBranch: initBranch,
@@ -263,6 +274,8 @@ export function phaseProcedures(publicProcedure: ProcedureBuilder) {
totalAdditions,
totalDeletions,
};
phaseMetaCache.set(cacheKey, result);
return result;
}),
getFileDiff: publicProcedure
@@ -297,6 +310,13 @@ export function phaseProcedures(publicProcedure: ProcedureBuilder) {
const decodedPath = decodeURIComponent(input.filePath);
const projects = await projectRepo.findProjectsByInitiativeId(phase.initiativeId);
const firstClone = await ensureProjectClone(projects[0], ctx.workspaceRoot!);
const headHash = await branchManager.getHeadCommitHash(firstClone, phBranch);
const cacheKey = `${input.phaseId}:${headHash}:${input.filePath}`;
const cached = fileDiffCache.get(cacheKey);
if (cached) return cached;
let clonePath: string;
if (input.projectId) {
const project = projects.find((p) => p.id === input.projectId);
@@ -305,18 +325,22 @@ export function phaseProcedures(publicProcedure: ProcedureBuilder) {
}
clonePath = await ensureProjectClone(project, ctx.workspaceRoot!);
} else {
clonePath = await ensureProjectClone(projects[0], ctx.workspaceRoot!);
clonePath = firstClone;
}
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 binaryResult = { binary: true, rawDiff: '' };
fileDiffCache.set(cacheKey, binaryResult);
return binaryResult;
}
const rawDiff = await branchManager.diffFileSingle(clonePath, diffBase, phBranch, decodedPath);
return { binary: false, rawDiff };
const result = { binary: false, rawDiff };
fileDiffCache.set(cacheKey, result);
return result;
}),
approvePhaseReview: publicProcedure