Keep daemon diff highlighting responsive and bounded (#7945)

* fix(server): keep diff highlighting responsive and bounded

Tiny patches previously parsed both full source versions synchronously. Yield between parser work units and admit source by UTF-8 bytes, keeping large patches renderable without tokens. Cancel obsolete highlighting when a newer diff supersedes it.

* fix(server): keep diff cancellation with its requester

Independent commit viewers must not cancel each other. Use request-source cancellation for commit highlighting and cache invalidation for checkout highlighting. Oversized source versions use the existing budgeted hunk reconstruction so large lockfiles retain useful syntax tokens.
This commit is contained in:
Mohamed Boudra
2026-10-27 09:56:36 +07:00
committed by GitHub
parent 9c7d4ace65
commit 6077bc6219
13 changed files with 567 additions and 65 deletions
@@ -4,6 +4,7 @@ import { tmpdir } from "os";
import { join } from "path";
import { afterEach, describe, expect, it } from "vitest";
import { getCommitFileDiff, listCheckoutCommits } from "./checkout-git.js";
import { SessionDelivery } from "../server/session/owned-subscriptions/index.js";
import { startGitCommandMetrics, stopGitCommandMetrics } from "./run-git-command.js";
const tempDirs: string[] = [];
@@ -302,3 +303,44 @@ describe("getCommitFileDiff", () => {
expect(file?.deletions).toBe(0);
});
});
it("identical commit-file requests from two requesters both retain syntax tokens", async () => {
const repoDir = initRepo();
const original = "const value = Math.max(42, 7);\n".repeat(3_000);
commitFile(repoDir, "file.ts", original, "initial");
commitFile(repoDir, "file.ts", original.replace("42", "43"), "edit");
const input = { cwd: repoDir, sha: headSha(repoDir), path: "file.ts" };
const results = await Promise.all([getCommitFileDiff(input), getCommitFileDiff(input)]);
for (const file of results) {
expect(
file?.hunks[0].lines
.find((line) => line.type === "add")
?.tokens?.some((token) => token.style !== null),
).toBe(true);
}
});
it("a disconnected requester cancels commit highlighting before its sources arrive", async () => {
const repoDir = initRepo();
commitFile(repoDir, "file.ts", "const value = 42;\n", "initial");
commitFile(repoDir, "file.ts", "const value = 43;\n", "edit");
const delivery = new SessionDelivery(() => {});
const source = {};
delivery.attach(source, true);
const request = {
type: "checkout.commits.file_diff.request" as const,
cwd: repoDir,
sha: headSha(repoDir),
path: "file.ts",
requestId: "disconnect",
};
const results: Awaited<ReturnType<typeof getCommitFileDiff>>[] = [];
const pending = delivery.request(source, request, async () => {
results.push(await getCommitFileDiff({ ...request, signal: delivery.requestSignal }));
});
await delivery.detach(source);
await pending;
expect(
results[0]?.hunks.flatMap((hunk) => hunk.lines).every((line) => line.tokens === undefined),
).toBe(true);
});