Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions packages/core/src/task-detail/cloudToolChanges.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { describe, expect, it } from "vitest";

import {
cachedDiffStats,
extractCloudFileContent,
extractCloudToolChangedFiles,
type ParsedToolCall,
} from "./cloudToolChanges";

function diffObj(
newText: string,
oldText: string,
): NonNullable<Parameters<typeof cachedDiffStats>[0]> {
return { type: "diff", path: "src/f.ts", newText, oldText };
}

function toolCall(overrides: Partial<ParsedToolCall>): ParsedToolCall {
return {
toolCallId: overrides.toolCallId ?? "tc-1",
Expand Down Expand Up @@ -62,6 +70,56 @@ describe("extractCloudToolChangedFiles", () => {
expect(result).toHaveLength(1);
expect(result[0].path).toBe("src/app.ts");
});

it.each([
{
name: "new file counts all lines as added",
kind: "write" as const,
oldText: undefined,
newText: "a\nb\nc",
added: 3,
removed: 0,
},
{
name: "modified file counts added and removed",
kind: "edit" as const,
oldText: "a\nb\nc",
newText: "a\nB\nc\nd",
added: 2,
removed: 1,
},
{
name: "pure removal counts removed only",
kind: "edit" as const,
oldText: "a\nb\nc",
newText: "a",
added: 0,
removed: 2,
},
])("diff stats: $name", ({ kind, oldText, newText, added, removed }) => {
const calls = makeToolCalls(
toolCall({
toolCallId: "tc",
kind,
locations: [{ path: "src/f.ts" }],
content: diffContent("src/f.ts", newText, oldText),
}),
);
const [file] = extractCloudToolChangedFiles(calls);
expect(file.linesAdded).toBe(added);
expect(file.linesRemoved).toBe(removed);
});

it("memoizes diff stats by diff-object identity", () => {
const diff = diffObj("a\nB\nc", "a\nb\nc");
const first = cachedDiffStats(diff);
expect(cachedDiffStats(diff)).toBe(first);

const distinctButEqual = diffObj("a\nB\nc", "a\nb\nc");
const recomputed = cachedDiffStats(distinctButEqual);
expect(recomputed).not.toBe(first);
expect(recomputed).toEqual(first);
});
});

describe("extractCloudFileContent", () => {
Expand Down
18 changes: 17 additions & 1 deletion packages/core/src/task-detail/cloudToolChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,22 @@ function getDiffStats(
return { added, removed };
}

const diffStatsCache = new WeakMap<
Extract<ToolCallContent, { type: "diff" }>,
{ added?: number; removed?: number }
>();

export function cachedDiffStats(
diff: Extract<ToolCallContent, { type: "diff" }> | undefined,
): { added?: number; removed?: number } {
if (!diff) return {};
const cached = diffStatsCache.get(diff);
if (cached) return cached;
const stats = getDiffStats(diff.oldText, diff.newText);
diffStatsCache.set(diff, stats);
return stats;
}

export interface CloudEventSummary {
toolCalls: Map<string, ParsedToolCall>;
}
Expand Down Expand Up @@ -266,7 +282,7 @@ export function extractCloudToolChangedFiles(
status: "deleted",
};
} else {
const diffStats = getDiffStats(diff?.oldText, diff?.newText);
const diffStats = cachedDiffStats(diff);
file = {
path,
status: kind === "write" && !diff?.oldText ? "added" : "modified",
Expand Down
Loading