perf(server): background sweeps only read threads that can still settle #13765
Mergedt3dotgg(GitHub profile, opens in a new tab)merged 7 commits into
main fromrefs/pull/13765/headmerged by t3dotgg(GitHub profile, opens in a new tab) View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/project/RepositoryIdentityResolver.ts lines 230 to 230OutdatedResolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -180,9 +215,16 @@215+216+ // Reads both caches without a lookup. Expired and pending entries read as none.217+ const cachedIdentity = Effect.fnUntraced(function* (cwd: string) {218+ const cacheKey = yield* Cache.getSuccess(repositoryRootCache, cwd);219+ if (Option.isNone(cacheKey)) return Option.none();220+ if (cacheKey.value === null) return Option.some(null);221+ return yield* Cache.getSuccess(repositoryIdentityCache, cacheKey.value);222+ });223+180224 // Untraced because almost every call is a cache hit. The lookups that spawn181225 // git keep their own spans.182226 const resolve: RepositoryIdentityResolver["Service"]["resolve"] = Effect.fnUntraced(183227 function* (cwd, options) {184- if (options?.refresh) yield* Cache.invalidate(repositoryRootCache, cwd);185- const cacheKey = yield* Cache.get(repositoryRootCache, cwd);186- if (cacheKey === null) return null;187- if (options?.refresh) yield* Cache.invalidate(repositoryIdentityCache, cacheKey);188- return yield* Cache.get(repositoryIdentityCache, cacheKey);228+ if (options?.refresh) return yield* lookup(cwd, true);229+ const cached = yield* cachedIdentity(cwd);230+ if (Option.isSome(cached)) return cached.value;
- apps/server/src/project/RepositoryIdentityResolver.test.ts line 240OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -176,0 +220,21 @@220+ Effect.gen(function* () {221+ calls.push(args);222+ if (holdGit) {223+ yield* Queue.offer(heldCalls, args);224+ yield* Deferred.await(release);225+ }226+ return args.includes("rev-parse") ? "/repo\n" : `origin\t${remoteUrl} (fetch)\n`;227+ }),228+ );229+230+ const first = yield* resolver.resolve("/repo");231+ yield* TestClock.adjust(Duration.minutes(15));232+ remoteUrl = "git@github.com:T3Tools/t3code-next.git";233+ holdGit = true;234+235+ const reads = yield* Effect.forkChild(236+ Effect.all([resolver.resolve("/repo"), resolver.resolve("/repo")]),237+ );238+ expect(yield* Queue.take(heldCalls)).toEqual(["-C", "/repo", "rev-parse", "--show-toplevel"]);239+ // git is still held, yet both reads have answered with the last identity.240+ expect(reads.pollUnsafe()).toBeDefined();apps/server/src/project/RepositoryIdentityResolver.ts lines 215 to 218OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -179,0 +198,21 @@198+ // the caches. A plain lookup whose cache entry expired answers with it.199+ const lastIdentities = new Map<string, RepositoryIdentity | null>();200+ const remember = (cwd: string, identity: RepositoryIdentity | null) => {201+ lastIdentities.delete(cwd);202+ lastIdentities.set(cwd, identity);203+ if (lastIdentities.size > cacheCapacity) {204+ const [oldest] = lastIdentities.keys();205+ if (oldest !== undefined) lastIdentities.delete(oldest);206+ }207+ return identity;208+ };209+ const backgroundRefreshes = yield* Semaphore.make(BACKGROUND_REFRESH_CONCURRENCY);210+211+ const lookup = Effect.fnUntraced(function* (cwd: string, refresh: boolean) {212+ if (refresh) yield* Cache.invalidate(repositoryRootCache, cwd);213+ const cacheKey = yield* Cache.get(repositoryRootCache, cwd);214+ if (refresh && cacheKey !== null) yield* Cache.invalidate(repositoryIdentityCache, cacheKey);215+ return remember(216+ cwd,217+ cacheKey === null ? null : yield* Cache.get(repositoryIdentityCache, cacheKey),218+ );apps/server/src/project/RepositoryIdentityResolver.ts line 239OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -180,9 +224,16 @@224+ if (Option.isNone(cacheKey)) return Option.none();225+ if (cacheKey.value === null) return Option.some(null);226+ return yield* Cache.getSuccess(repositoryIdentityCache, cacheKey.value);227+ });228+180229 // Untraced because almost every call is a cache hit. The lookups that spawn181230 // git keep their own spans.182231 const resolve: RepositoryIdentityResolver["Service"]["resolve"] = Effect.fnUntraced(183232 function* (cwd, options) {184- if (options?.refresh) yield* Cache.invalidate(repositoryRootCache, cwd);185- const cacheKey = yield* Cache.get(repositoryRootCache, cwd);186- if (cacheKey === null) return null;187- if (options?.refresh) yield* Cache.invalidate(repositoryIdentityCache, cacheKey);188- return yield* Cache.get(repositoryIdentityCache, cacheKey);233+ if (options?.refresh) return yield* lookup(cwd, true);234+ const cached = yield* cachedIdentity(cwd);235+ if (Option.isSome(cached)) return remember(cwd, cached.value);236+ const last = lastIdentities.get(cwd);237+ if (last === undefined) return yield* lookup(cwd, false);238+ // Callers that find the same expired entry share one lookup through the cache.239+ yield* lookup(cwd, false).pipe(backgroundRefreshes.withPermits(1), Effect.forkIn(scope));
- apps/server/src/project/RepositoryIdentityResolver.test.ts lines 325 to 328OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -176,0 +308,21 @@308+ yield* Deferred.await(release);309+ }310+ return args.includes("rev-parse")311+ ? `${args[1]}\n`312+ : "origin\tgit@github.com:T3Tools/t3code.git (fetch)\n";313+ }),314+ );315+ yield* resolver.resolve("/a");316+ yield* resolver.resolve("/b");317+ yield* TestClock.adjust(Duration.minutes(15));318+ holdGit = true;319+320+ yield* Effect.forkChild(321+ Effect.forEach(["/a", "/a", "/a", "/a", "/b"], (cwd) => resolver.resolve(cwd), {322+ concurrency: "unbounded",323+ }),324+ );325+ expect((yield* Queue.take(heldCalls))[1]).toBe("/a");326+ yield* Effect.yieldNow;327+ // Repeat reads of "/a" did not take the slots "/b" needs.328+ expect(Option.getOrUndefined(yield* Queue.poll(heldCalls))?.[1]).toBe("/b");apps/server/src/project/RepositoryIdentityResolver.test.ts line 361OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -176,0 +341,21 @@341+ if (args.includes("rev-parse")) return "/repo\n";342+ // Read the remote before waiting, like a git call that started earlier.343+ const stdout = `origin\t${remoteUrl} (fetch)\n`;344+ if (holdNextRemoteRead) {345+ holdNextRemoteRead = false;346+ yield* Queue.offer(heldCalls, args);347+ yield* Deferred.await(release);348+ }349+ return stdout;350+ }),351+ );352+ yield* resolver.resolve("/repo");353+ yield* TestClock.adjust(Duration.minutes(15));354+ holdNextRemoteRead = true;355+ yield* Effect.forkChild(resolver.resolve("/repo"));356+ yield* Queue.take(heldCalls);357+358+ remoteUrl = "git@github.com:T3Tools/new.git";359+ yield* resolver.resolve("/repo", { refresh: true });360+ yield* Deferred.succeed(release, undefined);361+ yield* Effect.yieldNow;
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
Thread transfer impact#
✅ Thread transfer remains within every enforced ceiling.
Baseline:
92f0af2· PR result:4143070· Source CI: successScenario and decoded snapshot size
10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.