Open History to see the last commit
1import type { TurnDiffFileChange } from "../types";
2
3export interface TurnDiffStat {
4 additions: number;
5 deletions: number;
6}
7
8export interface TurnDiffTreeDirectoryNode {
9 kind: "directory";
10 name: string;
11 path: string;
12 stat: TurnDiffStat;
13 children: TurnDiffTreeNode[];
14}
15
16export interface TurnDiffTreeFileNode {
17 kind: "file";
18 name: string;
19 path: string;
20 stat: TurnDiffStat | null;
21}
22
23export type TurnDiffTreeNode = TurnDiffTreeDirectoryNode | TurnDiffTreeFileNode;
24
25interface MutableDirectoryNode {
26 name: string;
27 path: string;
28 stat: TurnDiffStat;
29 directories: Map<string, MutableDirectoryNode>;
30 files: TurnDiffTreeFileNode[];
31}
32
33const SORT_LOCALE_OPTIONS: Intl.CollatorOptions = { numeric: true, sensitivity: "base" };
34
35function normalizePathSegments(pathValue: string): string[] {
36 return pathValue
37 .replaceAll("\\", "/")
38 .split("/")
39 .filter((segment) => segment.length > 0);
40}
41
42function compareByName(a: { name: string }, b: { name: string }): number {
43 return a.name.localeCompare(b.name, undefined, SORT_LOCALE_OPTIONS);
44}
45
46function readStat(file: TurnDiffFileChange): TurnDiffStat | null {
47 if (typeof file.additions !== "number" || typeof file.deletions !== "number") {
48 return null;
49 }
50 return {
51 additions: file.additions,
52 deletions: file.deletions,
53 };
54}
55
56function compactDirectoryNode(node: TurnDiffTreeDirectoryNode): TurnDiffTreeDirectoryNode {
57 const compactedChildren = node.children.map((child) =>
58 child.kind === "directory" ? compactDirectoryNode(child) : child,
59 );
60
61 let compactedNode: TurnDiffTreeDirectoryNode = {
62 ...node,
63 children: compactedChildren,
64 };
65
66 while (compactedNode.children.length === 1 && compactedNode.children[0]?.kind === "directory") {
67 const onlyChild = compactedNode.children[0];
68 compactedNode = {
69 kind: "directory",
70 name: `${compactedNode.name}/${onlyChild.name}`,
71 path: onlyChild.path,
72 stat: onlyChild.stat,
73 children: onlyChild.children,
74 };
75 }
76
77 return compactedNode;
78}
79
80function toTreeNodes(directory: MutableDirectoryNode): TurnDiffTreeNode[] {
81 const subdirectories: TurnDiffTreeDirectoryNode[] = Array.from(directory.directories.values())
82 .toSorted(compareByName)
83 .map<TurnDiffTreeDirectoryNode>((subdirectory) => ({
84 kind: "directory",
85 name: subdirectory.name,
86 path: subdirectory.path,
87 stat: {
88 additions: subdirectory.stat.additions,
89 deletions: subdirectory.stat.deletions,
90 },
91 children: toTreeNodes(subdirectory),
92 }))
93 .map((subdirectory) => compactDirectoryNode(subdirectory));
94
95 const files = directory.files.toSorted(compareByName);
96 return [...subdirectories, ...files];
97}
98
99/**
100 * Split a turn's changed files into agent-authored work and changes that
101 * arrived via pre-existing git history (pull, checkout, cherry-pick, rebase).
102 * Files without an origin (captured before attribution shipped) count as
103 * agent work.
104 */
105export function partitionTurnDiffFiles(files: ReadonlyArray<TurnDiffFileChange>): {
106 agentFiles: ReadonlyArray<TurnDiffFileChange>;
107 gitFiles: ReadonlyArray<TurnDiffFileChange>;
108} {
109 const agentFiles: TurnDiffFileChange[] = [];
110 const gitFiles: TurnDiffFileChange[] = [];
111 for (const file of files) {
112 (file.origin === "git" ? gitFiles : agentFiles).push(file);
113 }
114 return { agentFiles, gitFiles };
115}
116
117export function summarizeTurnDiffStats(files: ReadonlyArray<TurnDiffFileChange>): TurnDiffStat {
118 return files.reduce(
119 (acc, file) => {
120 const stat = readStat(file);
121 if (!stat) return acc;
122 return {
123 additions: acc.additions + stat.additions,
124 deletions: acc.deletions + stat.deletions,
125 };
126 },
127 { additions: 0, deletions: 0 },
128 );
129}
130
131export function buildTurnDiffTree(files: ReadonlyArray<TurnDiffFileChange>): TurnDiffTreeNode[] {
132 const root: MutableDirectoryNode = {
133 name: "",
134 path: "",
135 stat: { additions: 0, deletions: 0 },
136 directories: new Map(),
137 files: [],
138 };
139
140 for (const file of files) {
141 const segments = normalizePathSegments(file.path);
142 if (segments.length === 0) {
143 continue;
144 }
145
146 const filePath = segments.join("/");
147 const fileName = segments.at(-1);
148 if (!fileName) {
149 continue;
150 }
151 const stat = readStat(file);
152 const ancestors: MutableDirectoryNode[] = [root];
153 let currentDirectory = root;
154
155 for (const segment of segments.slice(0, -1)) {
156 const nextPath = currentDirectory.path ? `${currentDirectory.path}/${segment}` : segment;
157 const existing = currentDirectory.directories.get(segment);
158 if (existing) {
159 currentDirectory = existing;
160 } else {
161 const created: MutableDirectoryNode = {
162 name: segment,
163 path: nextPath,
164 stat: { additions: 0, deletions: 0 },
165 directories: new Map(),
166 files: [],
167 };
168 currentDirectory.directories.set(segment, created);
169 currentDirectory = created;
170 }
171 ancestors.push(currentDirectory);
172 }
173
174 currentDirectory.files.push({
175 kind: "file",
176 name: fileName,
177 path: filePath,
178 stat,
179 });
180
181 if (stat) {
182 for (const ancestor of ancestors) {
183 ancestor.stat.additions += stat.additions;
184 ancestor.stat.deletions += stat.deletions;
185 }
186 }
187 }
188
189 return toTreeNodes(root);
190}