Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions apps/web/src/components/Sidebar.logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

import {
createThreadJumpHintVisibilityController,
getSidebarThreadIdsToPrewarm,
getVisibleSidebarThreadIds,
resolveAdjacentThreadId,
getFallbackThreadIdAfterDelete,
Expand Down Expand Up @@ -121,6 +122,20 @@ describe("createThreadJumpHintVisibilityController", () => {
});
});

describe("getSidebarThreadIdsToPrewarm", () => {
it("returns only the first visible thread ids up to the prewarm limit", () => {
expect(getSidebarThreadIdsToPrewarm(["t1", "t2", "t3"], 2)).toEqual(["t1", "t2"]);
});

it("returns all visible thread ids when they fit within the limit", () => {
expect(getSidebarThreadIdsToPrewarm(["t1", "t2"], 10)).toEqual(["t1", "t2"]);
});

it("returns no thread ids when the limit is zero", () => {
expect(getSidebarThreadIdsToPrewarm(["t1", "t2"], 0)).toEqual([]);
});
});

describe("shouldClearThreadSelectionOnMouseDown", () => {
it("preserves selection for thread items", () => {
const child = {
Expand Down
10 changes: 10 additions & 0 deletions apps/web/src/components/Sidebar.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import { isLatestTurnSettled } from "../session-logic";

export const THREAD_SELECTION_SAFE_SELECTOR = "[data-thread-item], [data-thread-selection-safe]";
export const THREAD_JUMP_HINT_SHOW_DELAY_MS = 100;
// Visible sidebar rows are prewarmed into the thread-detail cache so opening a
// nearby thread usually reuses an already-hot subscription.
export const SIDEBAR_THREAD_PREWARM_LIMIT = 10;
export type SidebarNewThreadEnvMode = "local" | "worktree";
type SidebarProject = {
id: string;
Expand Down Expand Up @@ -243,6 +246,13 @@ export function getVisibleSidebarThreadIds<TThreadId>(
);
}

export function getSidebarThreadIdsToPrewarm<TThreadId>(
visibleThreadIds: readonly TThreadId[],
limit = SIDEBAR_THREAD_PREWARM_LIMIT,
): TThreadId[] {
return visibleThreadIds.slice(0, Math.max(0, limit));
}

export function resolveAdjacentThreadId<T>(input: {
threadIds: readonly T[];
currentThreadId: T | null;
Expand Down
27 changes: 27 additions & 0 deletions apps/web/src/components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
type GitStatusResult,
} from "@t3tools/contracts";
import {
parseScopedThreadKey,
scopedProjectKey,
scopedThreadKey,
scopeProjectRef,
Expand Down Expand Up @@ -81,6 +82,7 @@ import { useGitStatus } from "../lib/gitStatusState";
import { readLocalApi } from "../localApi";
import { useComposerDraftStore } from "../composerDraftStore";
import { useNewThreadHandler } from "../hooks/useHandleNewThread";
import { retainThreadDetailSubscription } from "../environments/runtime/service";

import { useThreadActions } from "../hooks/useThreadActions";
import {
Expand Down Expand Up @@ -122,6 +124,7 @@ import {
import { useThreadSelectionStore } from "../threadSelectionStore";
import { isNonEmpty as isNonEmptyString } from "effect/String";
import {
getSidebarThreadIdsToPrewarm,
resolveAdjacentThreadId,
isContextMenuPointerDown,
resolveProjectStatusIndicator,
Expand Down Expand Up @@ -2878,6 +2881,30 @@ export default function Sidebar() {
? threadJumpLabelByKey
: EMPTY_THREAD_JUMP_LABELS;
const orderedSidebarThreadKeys = visibleSidebarThreadKeys;
const prewarmedSidebarThreadKeys = useMemo(
() => getSidebarThreadIdsToPrewarm(visibleSidebarThreadKeys),
[visibleSidebarThreadKeys],
);
const prewarmedSidebarThreadRefs = useMemo(
() =>
prewarmedSidebarThreadKeys.flatMap((threadKey) => {
const ref = parseScopedThreadKey(threadKey);
return ref ? [ref] : [];
}),
[prewarmedSidebarThreadKeys],
);

useEffect(() => {
const releases = prewarmedSidebarThreadRefs.map((ref) =>
retainThreadDetailSubscription(ref.environmentId, ref.threadId),
);

return () => {
for (const release of releases) {
release();
}
};
}, [prewarmedSidebarThreadRefs]);

useEffect(() => {
const clearThreadJumpHints = () => {
Expand Down
153 changes: 150 additions & 3 deletions apps/web/src/environments/runtime/service.threadSubscriptions.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { QueryClient } from "@tanstack/react-query";
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
import {
EnvironmentId,
ProjectId,
ThreadId,
TurnId,
type OrchestrationShellSnapshot,
} from "@t3tools/contracts";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

const mockSubscribeThread = vi.fn();
Expand Down Expand Up @@ -65,6 +71,74 @@ vi.mock("../../rpc/wsTransport", () => ({
WsTransport: MockWsTransport,
}));

function makeThreadShellSnapshot(params: {
readonly threadId: ThreadId;
readonly sessionStatus?:
| "idle"
| "starting"
| "running"
| "ready"
| "interrupted"
| "stopped"
| "error";
readonly hasPendingApprovals?: boolean;
readonly hasPendingUserInput?: boolean;
readonly hasActionableProposedPlan?: boolean;
}): OrchestrationShellSnapshot {
const projectId = ProjectId.make("project-1");
const turnId = TurnId.make("turn-1");

return {
snapshotSequence: 1,
projects: [],
updatedAt: "2026-04-13T00:00:00.000Z",
threads: [
{
id: params.threadId,
projectId,
title: "Thread",
modelSelection: {
provider: "codex",
model: "gpt-5-codex",
},
runtimeMode: "full-access",
interactionMode: "default",
branch: null,
worktreePath: null,
latestTurn:
params.sessionStatus === "running"
? {
turnId,
state: "running",
requestedAt: "2026-04-13T00:00:00.000Z",
startedAt: "2026-04-13T00:00:01.000Z",
completedAt: null,
assistantMessageId: null,
}
: null,
createdAt: "2026-04-13T00:00:00.000Z",
updatedAt: "2026-04-13T00:00:00.000Z",
archivedAt: null,
session: params.sessionStatus
? {
threadId: params.threadId,
status: params.sessionStatus,
providerName: "codex",
runtimeMode: "full-access",
activeTurnId: params.sessionStatus === "running" ? turnId : null,
lastError: null,
updatedAt: "2026-04-13T00:00:00.000Z",
}
: null,
latestUserMessageAt: null,
hasPendingApprovals: params.hasPendingApprovals ?? false,
hasPendingUserInput: params.hasPendingUserInput ?? false,
hasActionableProposedPlan: params.hasActionableProposedPlan ?? false,
},
],
};
}

describe("retainThreadDetailSubscription", () => {
beforeEach(() => {
vi.useFakeTimers();
Expand Down Expand Up @@ -119,16 +193,89 @@ describe("retainThreadDetailSubscription", () => {
expect(mockSubscribeThread).toHaveBeenCalledTimes(1);

releaseSecond();
await vi.advanceTimersByTimeAsync(2 * 60 * 1000 - 1);
await vi.advanceTimersByTimeAsync(2 * 60 * 1000);
expect(mockThreadUnsubscribe).not.toHaveBeenCalled();

await vi.advanceTimersByTimeAsync(1);
await vi.advanceTimersByTimeAsync(28 * 60 * 1000);
expect(mockThreadUnsubscribe).toHaveBeenCalledTimes(1);

stop();
await resetEnvironmentServiceForTests();
});

it("keeps non-idle thread detail subscriptions attached until the thread becomes idle", async () => {
const {
retainThreadDetailSubscription,
startEnvironmentConnectionService,
resetEnvironmentServiceForTests,
} = await import("./service");

const stop = startEnvironmentConnectionService(new QueryClient());
const environmentId = EnvironmentId.make("env-1");
const threadId = ThreadId.make("thread-active");

const connectionInput = mockCreateEnvironmentConnection.mock.calls[0]?.[0];
expect(connectionInput).toBeDefined();

connectionInput.syncShellSnapshot(
makeThreadShellSnapshot({
threadId,
sessionStatus: "ready",
hasPendingApprovals: true,
}),
environmentId,
);

const release = retainThreadDetailSubscription(environmentId, threadId);
expect(mockSubscribeThread).toHaveBeenCalledTimes(1);

release();
await vi.advanceTimersByTimeAsync(30 * 60 * 1000);
expect(mockThreadUnsubscribe).not.toHaveBeenCalled();

connectionInput.applyShellEvent(
{
kind: "thread-upserted",
sequence: 2,
thread: makeThreadShellSnapshot({
threadId,
sessionStatus: "idle",
}).threads[0]!,
},
environmentId,
);

await vi.advanceTimersByTimeAsync(30 * 60 * 1000);
expect(mockThreadUnsubscribe).toHaveBeenCalledTimes(1);

stop();
await resetEnvironmentServiceForTests();
});

it("allows a larger idle cache before capacity eviction starts", async () => {
const {
retainThreadDetailSubscription,
startEnvironmentConnectionService,
resetEnvironmentServiceForTests,
} = await import("./service");

const stop = startEnvironmentConnectionService(new QueryClient());
const environmentId = EnvironmentId.make("env-1");

for (let index = 0; index < 12; index += 1) {
const release = retainThreadDetailSubscription(
environmentId,
ThreadId.make(`thread-${index + 1}`),
);
release();
}

expect(mockThreadUnsubscribe).not.toHaveBeenCalled();

stop();
await resetEnvironmentServiceForTests();
});

it("disposes cached thread detail subscriptions when the environment service resets", async () => {
const {
retainThreadDetailSubscription,
Expand Down
Loading
Loading