Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

Move diff parsing to worker pool with stable patch cache keys

Julius Marminge5cfab38f3159
diff --git a/apps/web/src/components/DiffPanel.tsx b/apps/web/src/components/DiffPanel.tsx
index 37a4369a5..c1bdc862a 100644
--- a/apps/web/src/components/DiffPanel.tsx
+++ b/apps/web/src/components/DiffPanel.tsx
@@ -1,5 +1,5 @@
import { parsePatchFiles } from "@pierre/diffs";
-import { FileDiff, type FileDiffMetadata } from "@pierre/diffs/react";
+import { FileDiff, type FileDiffMetadata, WorkerPoolContextProvider } from "@pierre/diffs/react";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { Columns2Icon, Rows3Icon } from "lucide-react";
import { useEffect, useMemo, useRef, useState } from "react";
@@ -8,6 +8,7 @@ import { cn } from "~/lib/utils";
import { isElectron } from "../env";
import { useNativeApi } from "../hooks/useNativeApi";
import { useTheme } from "../hooks/useTheme";
+import { buildPatchCacheKey } from "../lib/diffRendering";
import { useTurnDiffSummaries } from "../hooks/useTurnDiffSummaries";
import { formatTimestamp } from "../session-logic";
import { useStore } from "../store";
@@ -32,7 +33,7 @@ function getRenderablePatch(patch: string | undefined): RenderablePatch | null {
if (normalizedPatch.length === 0) return null;
try {
- const parsedPatches = parsePatchFiles(normalizedPatch, "diff-panel");
+ const parsedPatches = parsePatchFiles(normalizedPatch, buildPatchCacheKey(normalizedPatch));
const files = parsedPatches.flatMap((parsedPatch) => parsedPatch.files);
if (files.length > 0) {
return { kind: "files", files };
@@ -288,135 +289,151 @@ export default function DiffPanel({ mode = "inline" }: DiffPanelProps) {
};
const shouldUseDragRegion = isElectron && mode === "inline";
+ const workerPoolSize = useMemo(() => {
+ const cores = typeof navigator === "undefined" ? 4 : Math.max(1, navigator.hardwareConcurrency || 4);
+ return Math.max(2, Math.min(6, Math.floor(cores / 2)));
+ }, []);
return (
- <aside
- className={cn(
- "flex h-full min-w-0 flex-col bg-card",
- mode === "inline"
- ? "w-[42vw] min-w-[360px] max-w-[560px] shrink-0 border-l border-border"
- : "w-full",
- )}
+ <WorkerPoolContextProvider
+ poolOptions={{
+ workerFactory: () =>
+ new Worker(new URL("../workers/diffs.worker.ts", import.meta.url), { type: "module" }),
+ poolSize: workerPoolSize,
+ totalASTLRUCacheSize: 240,
+ }}
+ highlighterOptions={{
+ tokenizeMaxLineLength: 1_000,
+ }}
>
- <div
+ <aside
className={cn(
- "flex items-center justify-between border-b border-border px-4",
- shouldUseDragRegion ? "drag-region h-[52px]" : "py-3",
+ "flex h-full min-w-0 flex-col bg-card",
+ mode === "inline"
+ ? "w-[42vw] min-w-[360px] max-w-[560px] shrink-0 border-l border-border"
+ : "w-full",
)}
>
- <div className="flex gap-1.5 overflow-x-auto pb-0.5">
- <button type="button" className="shrink-0 rounded-md" onClick={selectWholeConversation}>
- <div
- className={cn(
- "rounded-md border px-2 py-1 text-left transition-colors",
- selectedTurnId === null
- ? "border-border bg-accent text-accent-foreground"
- : "border-border/70 bg-background/70 text-muted-foreground/80 hover:border-border hover:text-foreground/80",
- )}
- >
- <div className="text-[10px] font-medium">All turns</div>
- <div className="text-[8px] opacity-70">Conversation</div>
- </div>
- </button>
- {turnDiffSummaries.map((summary, index) => (
- <button
- key={summary.turnId}
- type="button"
- className="shrink-0 rounded-md"
- onClick={() => selectTurn(summary.turnId)}
- title={summary.turnId}
- >
+ <div
+ className={cn(
+ "flex items-center justify-between border-b border-border px-4",
+ shouldUseDragRegion ? "drag-region h-[52px]" : "py-3",
+ )}
+ >
+ <div className="flex gap-1.5 overflow-x-auto pb-0.5">
+ <button type="button" className="shrink-0 rounded-md" onClick={selectWholeConversation}>
<div
className={cn(
"rounded-md border px-2 py-1 text-left transition-colors",
- summary.turnId === selectedTurn?.turnId
+ selectedTurnId === null
? "border-border bg-accent text-accent-foreground"
: "border-border/70 bg-background/70 text-muted-foreground/80 hover:border-border hover:text-foreground/80",
)}
>
- <div className="text-[10px] font-medium">
- Turn {turnDiffSummaries.length - index}
- </div>
- <div className="text-[8px] opacity-70">{formatTimestamp(summary.completedAt)}</div>
+ <div className="text-[10px] font-medium">All turns</div>
+ <div className="text-[8px] opacity-70">Conversation</div>
</div>
</button>
- ))}
+ {turnDiffSummaries.map((summary, index) => (
+ <button
+ key={summary.turnId}
+ type="button"
+ className="shrink-0 rounded-md"
+ onClick={() => selectTurn(summary.turnId)}
+ title={summary.turnId}
+ >
+ <div
+ className={cn(
+ "rounded-md border px-2 py-1 text-left transition-colors",
+ summary.turnId === selectedTurn?.turnId
+ ? "border-border bg-accent text-accent-foreground"
+ : "border-border/70 bg-background/70 text-muted-foreground/80 hover:border-border hover:text-foreground/80",
+ )}
+ >
+ <div className="text-[10px] font-medium">
+ Turn {turnDiffSummaries.length - index}
+ </div>
+ <div className="text-[8px] opacity-70">{formatTimestamp(summary.completedAt)}</div>
+ </div>
+ </button>
+ ))}
+ </div>
+ <ToggleGroup
+ variant="outline"
+ size="xs"
+ value={[diffRenderMode]}
+ onValueChange={(value) => {
+ const next = value[0];
+ if (next === "stacked" || next === "split") {
+ setDiffRenderMode(next);
+ }
+ }}
+ >
+ <Toggle aria-label="Stacked diff view" value="stacked">
+ <Rows3Icon className="size-3" />
+ </Toggle>
+ <Toggle aria-label="Split diff view" value="split">
+ <Columns2Icon className="size-3" />
+ </Toggle>
+ </ToggleGroup>
</div>
- <ToggleGroup
- variant="outline"
- size="xs"
- value={[diffRenderMode]}
- onValueChange={(value) => {
- const next = value[0];
- if (next === "stacked" || next === "split") {
- setDiffRenderMode(next);
- }
- }}
- >
- <Toggle aria-label="Stacked diff view" value="stacked">
- <Rows3Icon className="size-3" />
- </Toggle>
- <Toggle aria-label="Split diff view" value="split">
- <Columns2Icon className="size-3" />
- </Toggle>
- </ToggleGroup>
- </div>
- {!activeThread ? (
- <div className="flex flex-1 items-center justify-center px-5 text-center text-xs text-muted-foreground/70">
- Select a thread to inspect turn diffs.
- </div>
- ) : turnDiffSummaries.length === 0 ? (
- <div className="flex flex-1 items-center justify-center px-5 text-center text-xs text-muted-foreground/70">
- No completed turns yet.
- </div>
- ) : (
- <>
- <div ref={patchViewportRef} className="min-w-0 flex-1 overflow-auto px-3 py-2">
- {!canApplyStoredTarget && state.diffThreadId && (
- <p className="mb-2 text-[11px] text-muted-foreground/65">
- Showing diffs for the active thread.
- </p>
- )}
- {checkpointDiffError && !renderablePatch && (
- <p className="mb-2 text-[11px] text-red-500/80">{checkpointDiffError}</p>
- )}
- {!renderablePatch ? (
- <div className="flex h-full items-center justify-center text-xs text-muted-foreground/70">
- {isLoadingCheckpointDiff
- ? "Loading checkpoint diff..."
- : "No patch available for this selection."}
- </div>
- ) : renderablePatch.kind === "files" ? (
- <div className="space-y-3">
- {renderableFiles.map((fileDiff) => (
- <div
- key={fileDiff.cacheKey ?? `${fileDiff.prevName ?? "none"}:${fileDiff.name}`}
- data-diff-file-path={resolveFileDiffPath(fileDiff)}
- className="rounded-md"
- >
- <FileDiff
- fileDiff={fileDiff}
- options={{
- diffStyle: diffRenderMode === "split" ? "split" : "unified",
- lineDiffType: "none",
- themeType: resolvedTheme,
- }}
- />
- </div>
- ))}
- </div>
- ) : (
- <div className="space-y-2">
- <p className="text-[11px] text-muted-foreground/75">{renderablePatch.reason}</p>
- <pre className="max-h-[72vh] overflow-auto rounded-md border border-border/70 bg-background/70 p-3 font-mono text-[11px] leading-relaxed text-muted-foreground/90">
- {renderablePatch.text}
- </pre>
- </div>
- )}
+ {!activeThread ? (
+ <div className="flex flex-1 items-center justify-center px-5 text-center text-xs text-muted-foreground/70">
+ Select a thread to inspect turn diffs.
</div>
- </>
- )}
- </aside>
+ ) : turnDiffSummaries.length === 0 ? (
+ <div className="flex flex-1 items-center justify-center px-5 text-center text-xs text-muted-foreground/70">
+ No completed turns yet.
+ </div>
+ ) : (
+ <>
+ <div ref={patchViewportRef} className="min-w-0 flex-1 overflow-auto px-3 py-2">
+ {!canApplyStoredTarget && state.diffThreadId && (
+ <p className="mb-2 text-[11px] text-muted-foreground/65">
+ Showing diffs for the active thread.
+ </p>
+ )}
+ {checkpointDiffError && !renderablePatch && (
+ <p className="mb-2 text-[11px] text-red-500/80">{checkpointDiffError}</p>
+ )}
+ {!renderablePatch ? (
+ <div className="flex h-full items-center justify-center text-xs text-muted-foreground/70">
+ {isLoadingCheckpointDiff
+ ? "Loading checkpoint diff..."
+ : "No patch available for this selection."}
+ </div>
+ ) : renderablePatch.kind === "files" ? (
+ <div className="space-y-3">
+ {renderableFiles.map((fileDiff) => (
+ <div
+ key={fileDiff.cacheKey ?? `${fileDiff.prevName ?? "none"}:${fileDiff.name}`}
+ data-diff-file-path={resolveFileDiffPath(fileDiff)}
+ className="rounded-md"
+ >
+ <FileDiff
+ fileDiff={fileDiff}
+ options={{
+ diffStyle: diffRenderMode === "split" ? "split" : "unified",
+ lineDiffType: "none",
+ themeType: resolvedTheme,
+ }}
+ />
+ </div>
+ ))}
+ </div>
+ ) : (
+ <div className="space-y-2">
+ <p className="text-[11px] text-muted-foreground/75">{renderablePatch.reason}</p>
+ <pre className="max-h-[72vh] overflow-auto rounded-md border border-border/70 bg-background/70 p-3 font-mono text-[11px] leading-relaxed text-muted-foreground/90">
+ {renderablePatch.text}
+ </pre>
+ </div>
+ )}
+ </div>
+ </>
+ )}
+ </aside>
+ </WorkerPoolContextProvider>
);
}
diff --git a/apps/web/src/lib/diffRendering.test.ts b/apps/web/src/lib/diffRendering.test.ts
new file mode 100644
index 000000000..08bc25b69
--- /dev/null
+++ b/apps/web/src/lib/diffRendering.test.ts
@@ -0,0 +1,23 @@
+import { describe, expect, it } from "vitest";
+import { buildPatchCacheKey } from "./diffRendering";
+
+describe("buildPatchCacheKey", () => {
+ it("returns a stable cache key for identical content", () => {
+ const patch = "diff --git a/a.ts b/a.ts\n+console.log('hello')";
+
+ expect(buildPatchCacheKey(patch)).toBe(buildPatchCacheKey(patch));
+ });
+
+ it("normalizes outer whitespace before hashing", () => {
+ const patch = "diff --git a/a.ts b/a.ts\n+console.log('hello')";
+
+ expect(buildPatchCacheKey(`\n${patch}\n`)).toBe(buildPatchCacheKey(patch));
+ });
+
+ it("changes when diff content changes", () => {
+ const before = "diff --git a/a.ts b/a.ts\n+console.log('hello')";
+ const after = "diff --git a/a.ts b/a.ts\n+console.log('hello world')";
+
+ expect(buildPatchCacheKey(before)).not.toBe(buildPatchCacheKey(after));
+ });
+});
diff --git a/apps/web/src/lib/diffRendering.ts b/apps/web/src/lib/diffRendering.ts
new file mode 100644
index 000000000..a5631ef85
--- /dev/null
+++ b/apps/web/src/lib/diffRendering.ts
@@ -0,0 +1,24 @@
+const FNV_OFFSET_BASIS_32 = 0x811c9dc5;
+const FNV_PRIME_32 = 0x01000193;
+const SECONDARY_HASH_SEED = 0x9e3779b9;
+const SECONDARY_HASH_MULTIPLIER = 0x85ebca6b;
+
+function fnv1a32(input: string, seed: number, multiplier: number): number {
+ let hash = seed >>> 0;
+ for (let index = 0; index < input.length; index += 1) {
+ hash ^= input.charCodeAt(index);
+ hash = Math.imul(hash, multiplier) >>> 0;
+ }
+ return hash >>> 0;
+}
+
+export function buildPatchCacheKey(patch: string, scope = "diff-panel"): string {
+ const normalizedPatch = patch.trim();
+ const primary = fnv1a32(normalizedPatch, FNV_OFFSET_BASIS_32, FNV_PRIME_32).toString(36);
+ const secondary = fnv1a32(
+ normalizedPatch,
+ SECONDARY_HASH_SEED,
+ SECONDARY_HASH_MULTIPLIER,
+ ).toString(36);
+ return `${scope}:${normalizedPatch.length}:${primary}:${secondary}`;
+}
diff --git a/apps/web/src/workers/diffs.worker.ts b/apps/web/src/workers/diffs.worker.ts
new file mode 100644
index 000000000..3cbb6a350
--- /dev/null
+++ b/apps/web/src/workers/diffs.worker.ts
@@ -0,0 +1 @@
+import "@pierre/diffs/worker/worker.js";