Skip to content

Commit abd9d9f

Browse files
t3dotggclaude
andauthored
perf: opening a thread loads 85% less data (pingdotgg#24899)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 1a4bb08 commit abd9d9f

16 files changed

Lines changed: 615 additions & 28 deletions

File tree

‎apps/mobile/src/features/threads/use-composer-command-menu.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ import type { ComposerCommandItem } from "./ComposerCommandPopover";
5959
import { matchesSlashSkillQuery } from "./composerSlashSkillSearch";
6060

6161
const WORKSPACE_SNAPSHOT_RETRY_COOLDOWN_MS = 10_000;
62+
// A cwd that never scans, such as a deleted worktree, would otherwise retry forever.
63+
const WORKSPACE_SNAPSHOT_MAX_ATTEMPTS = 3;
6264

6365
function composerSelectionAtEnd(draftMessage: string): ComposerEditorSelection {
6466
return { start: draftMessage.length, end: draftMessage.length };
@@ -252,6 +254,7 @@ export function useComposerCommandMenu({
252254
);
253255
// Record client request time to enforce TTL even when the client clock runs ahead of the server.
254256
const workspaceRefreshKeyRef = useRef<{ key: string; requestedAt: number } | null>(null);
257+
const failedRefreshesRef = useRef<{ key: string; count: number } | null>(null);
255258
const [workspaceRefreshRetry, setWorkspaceRefreshRetry] = useState<{
256259
key: string;
257260
notBefore: number;
@@ -307,6 +310,11 @@ export function useComposerCommandMenu({
307310
workspaceRefreshKeyRef.current = request;
308311
const retryLater = () => {
309312
if (workspaceRefreshKeyRef.current !== request) return;
313+
const failed = failedRefreshesRef.current;
314+
const count = failed?.key === key ? failed.count + 1 : 1;
315+
failedRefreshesRef.current = { key, count };
316+
// Keeping the request marker lets the TTL hold off the next scan.
317+
if (count >= WORKSPACE_SNAPSHOT_MAX_ATTEMPTS) return;
310318
workspaceRefreshKeyRef.current = null;
311319
setWorkspaceRefreshRetry({
312320
key,
@@ -326,6 +334,7 @@ export function useComposerCommandMenu({
326334
projectCwd,
327335
);
328336
if (!refreshed) retryLater();
337+
else failedRefreshesRef.current = null;
329338
}, retryLater);
330339
}, [
331340
draftMessage,

‎apps/server/src/environment/ServerEnvironment.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ export const make = Effect.gen(function* () {
239239
threadSnooze: true,
240240
environmentThemes: true,
241241
usageLimitSources: true,
242+
providerWorkspaceSnapshotsOnDemand: true,
242243
usagePriceOverrides: true,
243244
usageModelAliases: true,
244245
threadPinning: true,

‎apps/server/src/provider/ClaudeCapabilitiesProbe.test.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
// @effect-diagnostics nodeBuiltinImport:off - cleanup uses Node's retrying rm, which the FileSystem service does not expose.
2+
import * as NodeServices from "@effect/platform-node/NodeServices";
3+
import { ClaudeSettings, type ServerProvider } from "@t3tools/contracts";
24
import { vi } from "vite-plus/test";
35
import { assert, it } from "@effect/vitest";
6+
import * as Effect from "effect/Effect";
7+
import * as Schema from "effect/Schema";
48

5-
import { buildClaudeCapabilitiesProbeQueryOptions } from "./ClaudeProvider.ts";
9+
import {
10+
buildClaudeCapabilitiesProbeQueryOptions,
11+
probeClaudeWorkspaceSnapshot,
12+
} from "./ClaudeProvider.ts";
613

714
vi.mock("@anthropic-ai/claude-agent-sdk", { spy: true });
815

@@ -20,3 +27,15 @@ it("drops strict MCP mode, and only that, next to an administrator's managed-mcp
2027
assert.deepEqual(options.mcpServers, {});
2128
assert.equal(options.env?.ENABLE_CLAUDEAI_MCP_SERVERS, "false");
2229
});
30+
31+
it.effect("reports a deleted worktree's scan as finished, not pending", () =>
32+
Effect.gen(function* () {
33+
const snapshot = yield* probeClaudeWorkspaceSnapshot(
34+
yield* Schema.decodeEffect(ClaudeSettings)({}),
35+
{ skills: [], slashCommands: [] } as unknown as ServerProvider,
36+
"/nonexistent/t3-deleted-worktree",
37+
{ HOME: "/nonexistent/t3-home" },
38+
);
39+
assert.strictEqual(snapshot.slashCommandsPending, false);
40+
}).pipe(Effect.provide(NodeServices.layer)),
41+
);

‎apps/server/src/provider/ClaudeProvider.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -431,18 +431,22 @@ export const probeClaudeWorkspaceSnapshot = Effect.fn("probeClaudeWorkspaceSnaps
431431
): Effect.fn.Return<ProviderWorkspaceSnapshot, never, FileSystem.FileSystem | Path.Path> {
432432
if (!claudeSettings.enabled) return machineSnapshot;
433433
const skills = yield* discoverClaudeSkills(claudeSettings, cwd, environment);
434-
const probeResult = yield* probeClaudeCapabilities(claudeSettings, environment, cwd, false).pipe(
435-
Effect.result,
436-
);
437-
const capabilities = Result.isSuccess(probeResult) ? probeResult.success : undefined;
434+
const fileSystem = yield* FileSystem.FileSystem;
435+
// A deleted worktree can never answer the probe. Pending would make clients rescan it forever.
436+
const cwdExists = yield* fileSystem.exists(cwd).pipe(Effect.orElseSucceed(() => true));
437+
const probeResult = cwdExists
438+
? yield* probeClaudeCapabilities(claudeSettings, environment, cwd, false).pipe(Effect.result)
439+
: undefined;
440+
const capabilities =
441+
probeResult && Result.isSuccess(probeResult) ? probeResult.success : undefined;
438442
return {
439443
...machineSnapshot,
440444
skills,
441445
slashCommands: dedupeSlashCommands([
442446
COMPACT_SLASH_COMMAND,
443447
...(capabilities?.slashCommands ?? []),
444448
]),
445-
slashCommandsPending: !capabilities,
449+
slashCommandsPending: cwdExists && !capabilities,
446450
};
447451
});
448452

‎apps/server/src/provider/ProviderRegistry.test.ts‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,98 @@ it.layer(
12461246
);
12471247
});
12481248

1249+
it.effect(
1250+
"a second request for a cwd being scanned gets its result, even if the first leaves",
1251+
() =>
1252+
Effect.gen(function* () {
1253+
const driver = ProviderDriverKind.make("codex");
1254+
const instanceId = ProviderInstanceId.make("codex");
1255+
const provider = {
1256+
instanceId,
1257+
driver,
1258+
status: "ready",
1259+
enabled: true,
1260+
installed: true,
1261+
auth: { status: "authenticated" },
1262+
checkedAt: "2026-06-10T00:00:00.000Z",
1263+
version: "1.0.0",
1264+
models: [],
1265+
slashCommands: [],
1266+
skills: [],
1267+
} as const satisfies ServerProvider;
1268+
const scanStarted = yield* Deferred.make<void>();
1269+
const releaseScan = yield* Deferred.make<void>();
1270+
const scans = yield* Ref.make(0);
1271+
const instance: ProviderInstance = {
1272+
instanceId,
1273+
driverKind: driver,
1274+
continuationIdentity: { driverKind: driver, continuationKey: "codex:instance:codex" },
1275+
displayName: undefined,
1276+
enabled: true,
1277+
snapshot: {
1278+
resolveMaintenance: () =>
1279+
Effect.succeed(
1280+
makeManualOnlyProviderMaintenanceCapabilities({
1281+
provider: driver,
1282+
packageName: null,
1283+
}),
1284+
),
1285+
getSnapshot: Effect.succeed(provider),
1286+
refresh: Effect.succeed(provider),
1287+
streamChanges: Stream.empty,
1288+
applyUsageLimits: () => Effect.void,
1289+
},
1290+
snapshotForCwd: () =>
1291+
Ref.update(scans, (count) => count + 1).pipe(
1292+
Effect.andThen(Deferred.succeed(scanStarted, undefined)),
1293+
Effect.andThen(Deferred.await(releaseScan)),
1294+
Effect.as(provider),
1295+
),
1296+
orchestrationAdapter: {} as ProviderInstance["orchestrationAdapter"],
1297+
textGeneration: {} as ProviderInstance["textGeneration"],
1298+
};
1299+
const layerInstanceRegistry = Layer.succeed(
1300+
ProviderInstanceRegistry.ProviderInstanceRegistry,
1301+
{
1302+
getInstance: (requestedId) =>
1303+
Effect.succeed(requestedId === instanceId ? instance : undefined),
1304+
listInstances: Effect.succeed([instance]),
1305+
listUnavailable: Effect.succeed([]),
1306+
streamChanges: Stream.empty,
1307+
subscribeChanges: Effect.flatMap(PubSub.unbounded<void>(), PubSub.subscribe),
1308+
},
1309+
);
1310+
const scope = yield* Scope.make();
1311+
yield* Effect.addFinalizer(() => Scope.close(scope, Exit.void));
1312+
const runtimeServices = yield* Layer.build(
1313+
ProviderRegistry.layer.pipe(
1314+
Layer.provideMerge(layerInstanceRegistry),
1315+
Layer.provideMerge(
1316+
ServerConfig.layerTest(process.cwd(), {
1317+
prefix: "t3-provider-registry-shared-workspace-scan-",
1318+
}),
1319+
),
1320+
Layer.provideMerge(NodeServices.layer),
1321+
),
1322+
).pipe(Scope.provide(scope));
1323+
1324+
yield* Effect.gen(function* () {
1325+
const registry = yield* ProviderRegistry.ProviderRegistry;
1326+
const refresh = registry.refreshWorkspaceSnapshot({ instanceId, cwd: "/workspace" });
1327+
const first = yield* Effect.forkChild(refresh);
1328+
yield* Deferred.await(scanStarted);
1329+
const second = yield* Effect.forkChild(refresh, { startImmediately: true });
1330+
yield* Fiber.interrupt(first);
1331+
yield* Deferred.succeed(releaseScan, undefined);
1332+
assert.deepStrictEqual(
1333+
(yield* Fiber.join(second))[0]?.workspaceSnapshots?.map((snapshot) => snapshot.cwd),
1334+
["/workspace"],
1335+
);
1336+
assert.strictEqual(yield* Ref.get(scans), 1);
1337+
}).pipe(Effect.provide(runtimeServices));
1338+
}),
1339+
);
1340+
12491341
it.effect("publishes workspace skills while machine health is in error", () =>
12501342
Effect.gen(function* () {
12511343
const driver = ProviderDriverKind.make("codex");

‎apps/server/src/provider/ProviderRegistry.ts‎

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import * as DateTime from "effect/DateTime";
1212
import * as Deferred from "effect/Deferred";
1313
import * as Effect from "effect/Effect";
1414
import * as Equal from "effect/Equal";
15+
import * as Fiber from "effect/Fiber";
1516
import * as FileSystem from "effect/FileSystem";
1617
import * as Layer from "effect/Layer";
1718
import * as Option from "effect/Option";
@@ -405,7 +406,7 @@ export const layer = Layer.effect(
405406
cachedProviders.map((provider) => classifyCompatibility(provider, initialManifest)),
406407
);
407408
const workspaceRefreshesRef = yield* Ref.make<
408-
ReadonlyMap<ProviderInstance, ReadonlyMap<string, symbol>>
409+
ReadonlyMap<ProviderInstance, ReadonlyMap<string, Deferred.Deferred<void>>>
409410
>(new Map());
410411
const maintenanceActionStatesRef = yield* Ref.make<
411412
ReadonlyMap<ProviderInstanceId, { readonly update?: ServerProviderUpdateState | undefined }>
@@ -922,21 +923,13 @@ export const layer = Layer.effect(
922923
const scannedAt = DateTime.formatIso(now);
923924
const instance = yield* instanceRegistry.getInstance(input.instanceId);
924925
if (!instance?.snapshotForCwd) return providers;
925-
const token = Symbol();
926-
const claimed = yield* Ref.modify(workspaceRefreshesRef, (refreshes) => {
927-
const current = refreshes.get(instance);
928-
if (!input.fresh && current?.has(input.cwd)) return [false, refreshes] as const;
929-
const next = new Map(refreshes);
930-
next.set(instance, new Map(current).set(input.cwd, token));
931-
return [true, next] as const;
932-
});
933-
if (!claimed) return yield* Ref.get(providersRef);
926+
const token = yield* Deferred.make<void>();
934927
const refreshMachineSnapshot = input.fresh
935928
? (instance.invalidateCaches ?? Effect.void).pipe(
936929
Effect.andThen(refreshInstance(input.instanceId)),
937930
)
938931
: Effect.void;
939-
return yield* refreshMachineSnapshot.pipe(
932+
const runScan = refreshMachineSnapshot.pipe(
940933
Effect.andThen(instance.snapshotForCwd(input.cwd)),
941934
// Scans fail on error; returned inventory remains valid even when provider health reports an error.
942935
Effect.flatMap((scopedSnapshot) =>
@@ -974,9 +967,33 @@ export const layer = Layer.effect(
974967
if (nextCwds.size) next.set(instance, nextCwds);
975968
else next.delete(instance);
976969
return next;
977-
}),
970+
}).pipe(Effect.andThen(Deferred.succeed(token, undefined))),
978971
),
979972
);
973+
// The claim and the fork happen together, so the token is always completed. The scan is
974+
// detached so a disconnecting owner cannot cancel the result its waiters depend on.
975+
const claim = yield* Effect.uninterruptible(
976+
Effect.gen(function* () {
977+
const inFlight = yield* Ref.modify(workspaceRefreshesRef, (refreshes) => {
978+
const current = refreshes.get(instance);
979+
const running = current?.get(input.cwd);
980+
if (!input.fresh && running) return [running, refreshes] as const;
981+
const next = new Map(refreshes);
982+
next.set(instance, new Map(current).set(input.cwd, token));
983+
return [undefined, next] as const;
984+
});
985+
if (inFlight) return { inFlight } as const;
986+
return {
987+
// Starting now attaches the scan's `ensuring` before anything can close the scope.
988+
scan: yield* Effect.forkIn(runScan, serviceScope, { startImmediately: true }),
989+
} as const;
990+
}),
991+
);
992+
// Wait for the running scan: on-demand clients get its result only in this reply.
993+
if ("inFlight" in claim) {
994+
return yield* Deferred.await(claim.inFlight).pipe(Effect.andThen(Ref.get(providersRef)));
995+
}
996+
return yield* Fiber.join(claim.scan);
980997
});
981998

982999
return {

0 commit comments

Comments
 (0)