The changed-files card and per-turn inline diff are derived from full-tree checkpoint snapshots, so any tree-rewriting git command the agent runs (pull, checkout, cherry-pick, rebase) shows up as hundreds of "changed files" the agent never authored. This adds a read-side attribution layer on top of the existing checkpoint mechanism (capture, storage, and revert are untouched): - Checkpoint commits now record the HEAD oid and branch in their commit message. Legacy checkpoints without metadata degrade to today's behavior. - attributeCheckpointDiff classifies each path in a checkpoint tree delta: a path is "git" only when its exact blob transition also appears in the HEAD-movement delta for the turn and no commit authored during the turn touched it. Every ambiguity resolves to "agent" (shown), so misclassification can add noise but never hide agent work. Merge commits use the combined diff so only conflict resolutions count as agent work. - The checkpoint reactor tags each file with an origin; attribution failures degrade to unattributed files and never block the pipeline. - getTurnDiff/getFullThreadDiff drop git-attributed file sections from the patch unless the new includeGitChanges input is set. - The changed-files card lists agent files only, with a muted "Updated via git — N files" row for demoted changes. The diff panel shows a banner with a show/hide toggle so the full tree delta stays one click away. Co-Authored-By: Claude Fable 5 <[email protected]>
99 lines
2.8 KiB
TypeScript
99 lines
2.8 KiB
TypeScript
import { parsePatchFiles } from "@pierre/diffs/utils/parsePatchFiles";
|
|
|
|
export interface TurnDiffFileSummary {
|
|
readonly path: string;
|
|
readonly additions: number;
|
|
readonly deletions: number;
|
|
}
|
|
|
|
export function parseTurnDiffFilesFromUnifiedDiff(
|
|
diff: string,
|
|
): ReadonlyArray<TurnDiffFileSummary> {
|
|
const normalized = diff.replace(/\r\n/g, "\n").trim();
|
|
if (normalized.length === 0) {
|
|
return [];
|
|
}
|
|
|
|
const parsedPatches = parsePatchFiles(normalized);
|
|
const files = parsedPatches.flatMap((patch) =>
|
|
patch.files.map((file) => ({
|
|
path: file.name,
|
|
additions: file.hunks.reduce((total, hunk) => total + hunk.additionLines, 0),
|
|
deletions: file.hunks.reduce((total, hunk) => total + hunk.deletionLines, 0),
|
|
})),
|
|
);
|
|
|
|
return files.toSorted((left, right) => left.path.localeCompare(right.path));
|
|
}
|
|
|
|
function stripGitPathQuoting(value: string): string {
|
|
return value.startsWith('"') && value.endsWith('"') && value.length >= 2
|
|
? value.slice(1, -1)
|
|
: value;
|
|
}
|
|
|
|
/**
|
|
* Extract the post-image path of one `diff --git` section, preferring the
|
|
* `+++ b/` line and falling back to `--- a/` for deletions. Returns null when
|
|
* the section is unparseable; callers should keep such sections.
|
|
*/
|
|
function resolveDiffSectionPath(section: string): string | null {
|
|
for (const line of section.split("\n")) {
|
|
if (line.startsWith("+++ ")) {
|
|
const target = stripGitPathQuoting(line.slice(4).trim());
|
|
if (target !== "/dev/null") {
|
|
return target.startsWith("b/") ? target.slice(2) : target;
|
|
}
|
|
} else if (line.startsWith("--- ")) {
|
|
const source = stripGitPathQuoting(line.slice(4).trim());
|
|
if (source !== "/dev/null" && source.startsWith("a/")) {
|
|
return source.slice(2);
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Remove file sections from a unified diff by path.
|
|
*
|
|
* Sections whose path cannot be determined are kept (fail toward showing).
|
|
*/
|
|
export function filterUnifiedDiffFiles(
|
|
diff: string,
|
|
shouldKeepPath: (path: string) => boolean,
|
|
): string {
|
|
const normalized = diff.replace(/\r\n/g, "\n");
|
|
if (normalized.trim().length === 0) {
|
|
return diff;
|
|
}
|
|
|
|
const sections: Array<string> = [];
|
|
let currentStart = -1;
|
|
const lines = normalized.split("\n");
|
|
const flush = (endExclusive: number) => {
|
|
if (currentStart >= 0) {
|
|
sections.push(lines.slice(currentStart, endExclusive).join("\n"));
|
|
}
|
|
};
|
|
for (let index = 0; index < lines.length; index += 1) {
|
|
if (lines[index]!.startsWith("diff --git ")) {
|
|
flush(index);
|
|
currentStart = index;
|
|
}
|
|
}
|
|
flush(lines.length);
|
|
if (sections.length === 0) {
|
|
return diff;
|
|
}
|
|
|
|
const kept = sections.filter((section) => {
|
|
const path = resolveDiffSectionPath(section);
|
|
return path === null || shouldKeepPath(path);
|
|
});
|
|
if (kept.length === sections.length) {
|
|
return diff;
|
|
}
|
|
return kept.join("\n");
|
|
}
|