Skip to content

Commit 3667f08

Browse files
willsheldonclaude
andcommitted
fix(server): cache project favicon resolution
resolvePath walks up to 12 well-known favicon paths and then reads 7 source files looking for icon metadata, so a miss costs roughly 20 filesystem probes. AssetAccess calls it on every project-favicon asset URL and nothing cached the answer, even though a project's icon does not move. Traced over 100 minutes on one install: 451 resolvePath calls issued 9,106 findExistingFile probes and spent 313s of the 422s that issueAssetUrl consumed in total. The slowest asset URL took 67.8s. Cache the resolved path per workspace root and saved favicon path, 10 minutes for a hit and 1 minute for a miss. A hit still confirms the file with a single stat rather than the full walk, so deleting an icon falls back immediately instead of serving a stale path until the entry expires. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01KX8tw6Sqs3h2oYKmi18Ptr
1 parent 31c1c59 commit 3667f08

2 files changed

Lines changed: 137 additions & 3 deletions

File tree

‎apps/server/src/project/ProjectFaviconResolver.test.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import * as NodeServices from "@effect/platform-node/NodeServices";
22
import { it, describe, expect } from "@effect/vitest";
3+
import * as Duration from "effect/Duration";
34
import * as Effect from "effect/Effect";
45
import * as FileSystem from "effect/FileSystem";
56
import * as Layer from "effect/Layer";
67
import * as Path from "effect/Path";
78
import * as PlatformError from "effect/PlatformError";
9+
import { TestClock } from "effect/testing";
810

911
import * as WorkspacePaths from "../workspace/WorkspacePaths.ts";
1012
import * as ProjectFaviconResolver from "./ProjectFaviconResolver.ts";
@@ -49,6 +51,64 @@ const makeResolverWithFileSystem = (fileSystem: FileSystem.FileSystem) =>
4951

5052
it.layer(TestLayer)("ProjectFaviconResolverLive", (it) => {
5153
describe("resolvePath", () => {
54+
it.effect("serves repeated resolves from cache instead of re-walking candidates", () =>
55+
Effect.gen(function* () {
56+
const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
57+
const cwd = yield* makeTempDir;
58+
yield* writeTextFile(cwd, "public/favicon.svg", "<svg>public</svg>");
59+
60+
const resolved = yield* resolver.resolvePath(cwd);
61+
expect(resolved?.endsWith("public/favicon.svg")).toBe(true);
62+
63+
// `favicon.svg` outranks `public/favicon.svg`, so a resolver that walked
64+
// the candidate list again would switch to it. Staying on the original
65+
// answer is only possible from cache.
66+
yield* writeTextFile(cwd, "favicon.svg", "<svg>root</svg>");
67+
68+
for (const _attempt of [1, 2, 3]) {
69+
expect(yield* resolver.resolvePath(cwd)).toBe(resolved);
70+
}
71+
72+
yield* TestClock.adjust(Duration.minutes(11));
73+
74+
expect((yield* resolver.resolvePath(cwd))?.endsWith("/favicon.svg")).toBe(true);
75+
expect(yield* resolver.resolvePath(cwd)).not.toBe(resolved);
76+
}).pipe(Effect.provide(TestClock.layer())),
77+
);
78+
79+
it.effect("falls back at once when a cached favicon is deleted", () =>
80+
Effect.gen(function* () {
81+
const fileSystem = yield* FileSystem.FileSystem;
82+
const path = yield* Path.Path;
83+
const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
84+
const cwd = yield* makeTempDir;
85+
yield* writeTextFile(cwd, "favicon.svg", "<svg>favicon</svg>");
86+
87+
expect(yield* resolver.resolvePath(cwd)).not.toBeNull();
88+
89+
yield* fileSystem.remove(path.join(cwd, "favicon.svg")).pipe(Effect.orDie);
90+
91+
// Still inside the positive TTL: the cached path must not be served.
92+
expect(yield* resolver.resolvePath(cwd)).toBeNull();
93+
}).pipe(Effect.provide(TestClock.layer())),
94+
);
95+
96+
it.effect("re-probes for a favicon added after a miss once the negative TTL expires", () =>
97+
Effect.gen(function* () {
98+
const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
99+
const cwd = yield* makeTempDir;
100+
101+
expect(yield* resolver.resolvePath(cwd)).toBeNull();
102+
103+
yield* writeTextFile(cwd, "favicon.svg", "<svg>favicon</svg>");
104+
expect(yield* resolver.resolvePath(cwd)).toBeNull();
105+
106+
yield* TestClock.adjust(Duration.minutes(2));
107+
108+
expect(yield* resolver.resolvePath(cwd)).not.toBeNull();
109+
}).pipe(Effect.provide(TestClock.layer())),
110+
);
111+
52112
it.effect("prefers well-known favicon files", () =>
53113
Effect.gen(function* () {
54114
const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;

‎apps/server/src/project/ProjectFaviconResolver.ts‎

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
*
77
* @module ProjectFaviconResolver
88
*/
9+
import * as Cache from "effect/Cache";
910
import * as Context from "effect/Context";
11+
import * as Duration from "effect/Duration";
1012
import * as Effect from "effect/Effect";
13+
import * as Exit from "effect/Exit";
1114
import * as FileSystem from "effect/FileSystem";
1215
import * as Layer from "effect/Layer";
1316
import * as Option from "effect/Option";
@@ -18,6 +21,30 @@ import * as Schema from "effect/Schema";
1821
import * as WorkspacePaths from "../workspace/WorkspacePaths.ts";
1922
import * as T3ProjectFileLoader from "./T3ProjectFileLoader.ts";
2023

24+
// Resolution walks up to 12 well-known paths plus 7 source files, so a miss
25+
// costs ~20 filesystem probes. AssetAccess resolves on every project-favicon
26+
// asset URL, and a project's icon does not move, so the answer is cached.
27+
const FAVICON_CACHE_CAPACITY = 512;
28+
const FAVICON_POSITIVE_CACHE_TTL = Duration.minutes(10);
29+
const FAVICON_NEGATIVE_CACHE_TTL = Duration.minutes(1);
30+
31+
function faviconCacheKey(cwd: string, faviconPath?: string): string {
32+
return `${faviconPath ?? ""}\0${cwd}`;
33+
}
34+
35+
function parseFaviconCacheKey(key: string): {
36+
readonly cwd: string;
37+
readonly faviconPath?: string;
38+
} {
39+
const separatorIndex = key.indexOf("\0");
40+
if (separatorIndex === -1) {
41+
return { cwd: key };
42+
}
43+
const faviconPath = key.slice(0, separatorIndex);
44+
const cwd = key.slice(separatorIndex + 1);
45+
return faviconPath.length === 0 ? { cwd } : { cwd, faviconPath };
46+
}
47+
2148
// Well-known favicon paths checked in order.
2249
const FAVICON_CANDIDATES = [
2350
"favicon.svg",
@@ -178,9 +205,10 @@ export const make = Effect.gen(function* () {
178205
return null;
179206
});
180207

181-
const resolvePath: ProjectFaviconResolver["Service"]["resolvePath"] = Effect.fn(
182-
"ProjectFaviconResolver.resolvePath",
183-
)(function* (cwd, faviconPath) {
208+
const resolvePathUncached = Effect.fn("ProjectFaviconResolver.resolvePathUncached")(function* (
209+
cwd: string,
210+
faviconPath?: string,
211+
): Effect.fn.Return<string | null, ProjectFaviconResolutionError> {
184212
const projectCwd = yield* workspacePaths.normalizeWorkspaceRoot(cwd).pipe(
185213
Effect.mapError(
186214
(cause) =>
@@ -267,6 +295,52 @@ export const make = Effect.gen(function* () {
267295
return null;
268296
});
269297

298+
const faviconCache = yield* Cache.makeWith<string, string | null, ProjectFaviconResolutionError>(
299+
(key) => {
300+
const { cwd, faviconPath } = parseFaviconCacheKey(key);
301+
return resolvePathUncached(cwd, faviconPath);
302+
},
303+
{
304+
capacity: FAVICON_CACHE_CAPACITY,
305+
timeToLive: Exit.match({
306+
onSuccess: (value: string | null) =>
307+
value === null ? FAVICON_NEGATIVE_CACHE_TTL : FAVICON_POSITIVE_CACHE_TTL,
308+
onFailure: () => Duration.zero,
309+
}),
310+
},
311+
);
312+
313+
const resolvePath: ProjectFaviconResolver["Service"]["resolvePath"] = Effect.fn(
314+
"ProjectFaviconResolver.resolvePath",
315+
)(function* (cwd, faviconPath) {
316+
const key = faviconCacheKey(cwd, faviconPath);
317+
const cached = yield* Cache.get(faviconCache, key);
318+
if (cached === null) {
319+
return null;
320+
}
321+
322+
// A hit still confirms the file with one stat rather than the ~20 probes a
323+
// full walk costs, so a deleted icon falls back at once instead of after
324+
// the TTL.
325+
const stats = yield* optionOnNotFound(fileSystem.stat(cached)).pipe(
326+
Effect.mapError(
327+
(cause) =>
328+
new ProjectFaviconResolutionError({
329+
operation: "stat-candidate",
330+
workspaceRoot: cwd,
331+
absolutePath: cached,
332+
cause,
333+
}),
334+
),
335+
);
336+
if (Option.isSome(stats) && stats.value.type === "File") {
337+
return cached;
338+
}
339+
340+
yield* Cache.invalidate(faviconCache, key);
341+
return yield* Cache.get(faviconCache, key);
342+
});
343+
270344
return ProjectFaviconResolver.of({ resolvePath });
271345
});
272346

0 commit comments

Comments
 (0)