| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389 |
- /* eslint-disable max-lines -- 远端 workspace 历史目前需要在一个 hook 内同时收口恢复、重连、持久化和清理流程,先保留同文件协作边界,避免为过 lint 临时拆分后引入状态回归。*/
- import { useCallback, useEffect, useMemo, useRef, useState } from "react";
- import type { Dispatch, SetStateAction } from "react";
- import type {
- AppSettings,
- BotRemoteWorkspaceReconnectedEvent,
- IPlatformService,
- RemoteSessionClosedEvent,
- RemoteWorkspaceSessionEntry,
- } from "@zcode/shared";
- import { buildSshRemoteHostKey, createUuid, stripRemoteTargetSecrets } from "@zcode/shared";
- import type { IServiceAccessor } from "@zcode/services";
- import {
- bindRemoteWorkspaceIdentity,
- bindRemoteWorkspacePath,
- getRemoteWorkspaceSession,
- type RemoteWorkspaceSession,
- unregisterRemoteWorkspaceSession,
- } from "@/store/remoteWorkspaceSessionStore.js";
- import { useZCodeSessionStore } from "@/store/zcodeSessionStore.js";
- import { refreshRemotePinnedTasksForSession } from "@/store/remotePinnedTaskStore.js";
- import { refreshRemoteTimelineTasksForSession } from "@/store/remoteTimelineTaskStore.js";
- import { toast } from "@/components/ui/toast.js";
- import {
- buildRemoteWorkspaceIdentity,
- buildRemoteWorkspaceSessionMutation,
- buildWorkspaceSessionKey,
- createRemoteTargetFromSnapshot,
- getRemoteWorkspaceSessionEntries,
- removeRemoteWorkspaceSessionEntries,
- } from "@/lib/remoteWorkspaceHistory.js";
- import { getErrorMessage } from "@/lib/errorMessage.js";
- import { logger } from "@/logger.js";
- import { isWorkspaceTab, type TabStoreState, type WindowTabState } from "@/store/tabStore.js";
- import {
- buildRemoteWorkspacePersistPatch,
- restorePersistedRemoteWorkspaceSessions,
- } from "@/root/remoteWorkspaceSessionPersistence.js";
- import { useReconnectingRemoteWorkspaceLogs } from "@/root/useReconnectingRemoteWorkspaceLogs.js";
- import {
- reconnectRemoteWorkspaceHistoryEntry,
- type BindRemoteWorkspaceSessionContextFn,
- type ReconnectRemoteWorkspaceOptions,
- type SshReconnectCredentials,
- } from "@/root/reconnectRemoteWorkspaceHistoryEntry.js";
- import { useRemoteConnectionEntryVisibility } from "@/hooks/useRemoteConnectionEntryVisibility.js";
- import { markRemoteWorkspaceRunningTasksFailed } from "@/lib/remoteWorkspaceSessionRuntime.js";
- export { reconnectRemoteWorkspaceHistoryEntry };
- export async function bindRemoteWorkspaceContextAndGetSession(params: {
- platform: Pick<IPlatformService, "bindRemoteWorkspaceSessionContext">;
- sessionId: string;
- workspacePath: string;
- workspaceIdentity?: string;
- }): Promise<RemoteWorkspaceSession> {
- if (!getRemoteWorkspaceSession(params.sessionId)) {
- throw new Error(`远程 workspace session 不存在: ${params.sessionId}`);
- }
- await params.platform.bindRemoteWorkspaceSessionContext?.({
- remoteSessionId: params.sessionId,
- workspacePath: params.workspacePath,
- workspaceIdentity: params.workspaceIdentity,
- });
- // bind 会让同一 remoteSessionId 的 attachment/services 从 A 换代到 B。
- // bind 前捕获的对象仍指向 A,因此必须在 ready ACK 后按 sessionId 重新读取当前 services。
- const currentSession = getRemoteWorkspaceSession(params.sessionId);
- if (!currentSession) {
- throw new Error(`远程 workspace session 不存在: ${params.sessionId}`);
- }
- return currentSession;
- }
- export function resolveBotRemoteWorkspaceReconnectedIdentity(params: {
- event: Pick<BotRemoteWorkspaceReconnectedEvent, "workspaceIdentity">;
- resolvedWorkspacePath: string;
- target: BotRemoteWorkspaceReconnectedEvent["target"];
- }): string {
- const eventWorkspaceIdentity = params.event.workspaceIdentity.trim();
- if (eventWorkspaceIdentity) {
- // Bugfix: Bot task/stream 广播继续使用 bot context 捕获的 workspaceIdentity。
- // 这里如果用 canonical path 重新计算 identity,UI tab 会订阅到另一个 key,导致远端结果被过滤掉。
- return eventWorkspaceIdentity;
- }
- return buildRemoteWorkspaceIdentity(params.resolvedWorkspacePath, params.target);
- }
- export function shouldPersistRemoteWorkspaceFailure(params: {
- pendingReconnectRequestIds: ReadonlyMap<string, string>;
- sessionEntry: RemoteWorkspaceSessionEntry;
- workspaceKey: string;
- }): boolean {
- if (params.sessionEntry.target.kind !== "wsl") {
- return true;
- }
- // WSL 偶发断连时,旧 session 的关闭事件可能会晚于手动重连流程。
- // 只要当前 workspace 已经有 pending reconnect,就先别把 failed 写死到 setting,
- // 让最终结果由这次重连成功/失败决定。
- return !params.pendingReconnectRequestIds.has(params.workspaceKey);
- }
- interface RemoteWorkspaceTabStoreReader {
- getState(): {
- tabs: WindowTabState[];
- };
- }
- interface OpenRemoteWorkspaceFromHistoryParams {
- workspaceKey: string;
- tabStoreApi: RemoteWorkspaceTabStoreReader;
- getRemoteSessions: () => RemoteWorkspaceSessionEntry[];
- inflightReconnectWorkspaceKeys: Set<string>;
- activateTabByPath: (workspacePath: string, options?: { workspaceIdentity?: string }) => boolean;
- setReconnectingRemoteWorkspaceKeys: Dispatch<SetStateAction<string[]>>;
- loadCredential: IServiceAccessor["credentialService"]["load"];
- connectRemoteWorkspaceTarget: (
- target: Parameters<IPlatformService["connectRemote"]>[0],
- requestId?: string,
- ) => Promise<string>;
- resolveRemoteWorkspaceCanonicalPath: (
- sessionId: string,
- workspacePath: string,
- ) => Promise<string>;
- disposeRemoteWorkspaceSession: (sessionId: string) => Promise<void>;
- bindRemoteWorkspaceSessionContext: BindRemoteWorkspaceSessionContextFn;
- addTab: (
- workspacePath: string,
- options?: {
- remoteSessionId?: string;
- remoteTarget?: Parameters<IPlatformService["connectRemote"]>[0];
- workspaceIdentity?: string;
- localWorkspacePath?: string;
- },
- ) => void;
- commitRemoteWorkspaceSessionMutation: (
- mutation: ReturnType<typeof buildRemoteWorkspaceSessionMutation>,
- ) => Promise<RemoteWorkspaceSessionEntry>;
- resetLogsForWorkspaceKey: (workspaceKey: string) => void;
- onWorkspaceActivated?: (target: { workspacePath: string; workspaceIdentity: string }) => void;
- createReconnectRequestId?: () => string;
- pendingReconnectRequestIds?: Map<string, string>;
- reconnectImpl?: typeof reconnectRemoteWorkspaceHistoryEntry;
- }
- interface ReconnectRemoteWorkspaceByKeyParams {
- workspaceKey: string;
- canUseRemoteWorkspace: boolean;
- getRemoteSessions: () => RemoteWorkspaceSessionEntry[];
- runReconnectRemoteWorkspace: (
- sessionEntry: RemoteWorkspaceSessionEntry,
- options?: ReconnectRemoteWorkspaceOptions,
- ) => Promise<void>;
- options?: ReconnectRemoteWorkspaceOptions;
- }
- export async function reconnectRemoteWorkspaceByKey({
- workspaceKey,
- canUseRemoteWorkspace,
- getRemoteSessions,
- runReconnectRemoteWorkspace,
- options,
- }: ReconnectRemoteWorkspaceByKeyParams): Promise<boolean> {
- if (!canUseRemoteWorkspace) {
- throw new Error("Remote workspace is disabled in this mode");
- }
- const sessionEntry = getRemoteSessions().find(
- (entry) => buildWorkspaceSessionKey(entry) === workspaceKey,
- );
- if (!sessionEntry) {
- throw new Error(`远程 workspace 不在当前窗口中,无法重连: ${workspaceKey}`);
- }
- await runReconnectRemoteWorkspace(sessionEntry, options);
- return true;
- }
- export function collectSshReconnectGroup(params: {
- selected: RemoteWorkspaceSessionEntry;
- sessions: RemoteWorkspaceSessionEntry[];
- tabs: WindowTabState[];
- }): RemoteWorkspaceSessionEntry[] {
- if (params.selected.target.kind !== "ssh") {
- return [params.selected];
- }
- const remoteHostKey = buildSshRemoteHostKey(params.selected.target);
- const disconnectedWorkspaceKeys = new Set(
- params.tabs.flatMap((tab) => {
- if (!isWorkspaceTab(tab) || tab.remoteSessionId) {
- return [];
- }
- return [buildWorkspaceSessionKey(tab)];
- }),
- );
- const reconnectGroup = params.sessions.filter(
- (entry) =>
- entry.target.kind === "ssh" &&
- buildSshRemoteHostKey(entry.target) === remoteHostKey &&
- disconnectedWorkspaceKeys.has(buildWorkspaceSessionKey(entry)),
- );
- const selectedWorkspaceKey = buildWorkspaceSessionKey(params.selected);
- if (!reconnectGroup.some((entry) => buildWorkspaceSessionKey(entry) === selectedWorkspaceKey)) {
- return [params.selected];
- }
- // 历史记录顺序不代表本次重连发起者。initiator 必须固定在首位,
- // 否则并发加载凭据时 sibling 可能先创建共享 Host,导致整组误用旧凭据。
- return [
- params.selected,
- ...reconnectGroup.filter((entry) => buildWorkspaceSessionKey(entry) !== selectedWorkspaceKey),
- ];
- }
- export async function reconnectRemoteWorkspaceGroup(params: {
- selected: RemoteWorkspaceSessionEntry;
- reconnectGroup: RemoteWorkspaceSessionEntry[];
- reconnectEntry: (
- entry: RemoteWorkspaceSessionEntry,
- options?: ReconnectRemoteWorkspaceOptions,
- ) => Promise<boolean>;
- options?: ReconnectRemoteWorkspaceOptions;
- }): Promise<void> {
- const selectedWorkspaceKey = buildWorkspaceSessionKey(params.selected);
- const siblings = params.reconnectGroup.filter(
- (entry) => buildWorkspaceSessionKey(entry) !== selectedWorkspaceKey,
- );
- if (params.selected.target.kind !== "ssh") {
- try {
- await params.reconnectEntry(params.selected, {
- ...params.options,
- throwOnFailure: true,
- });
- } catch (error) {
- if (params.options?.throwOnFailure) {
- throw error;
- }
- }
- return;
- }
- type InitiatorHostGate =
- | { status: "ready"; credentials: SshReconnectCredentials }
- | { status: "skipped" }
- | { status: "failed"; error: unknown };
- let hostReadyCredentials: SshReconnectCredentials | undefined;
- let resolveHostReady!: (result: InitiatorHostGate) => void;
- const hostReadyPromise = new Promise<InitiatorHostGate>((resolve) => {
- resolveHostReady = resolve;
- });
- // 组内并发连接会让最先完成 credential load 的 sibling 抢建共享 Host。
- // initiator 先负责把 Host 建到 ready;后续 provider/task 初始化不再阻塞 sibling attachment。
- const initiatorPromise = params.reconnectEntry(params.selected, {
- ...params.options,
- throwOnFailure: true,
- onSshHostReady: (credentials) => {
- if (hostReadyCredentials) {
- return;
- }
- hostReadyCredentials = credentials;
- resolveHostReady({ status: "ready", credentials });
- },
- });
- const initiatorFinishedBeforeReady = initiatorPromise.then<InitiatorHostGate, InitiatorHostGate>(
- (didReconnect) => {
- if (hostReadyCredentials) {
- return { status: "ready", credentials: hostReadyCredentials };
- }
- return didReconnect
- ? {
- status: "failed",
- error: new Error("SSH initiator completed without reporting Host ready"),
- }
- : { status: "skipped" };
- },
- (error: unknown) =>
- hostReadyCredentials
- ? { status: "ready", credentials: hostReadyCredentials }
- : { status: "failed", error },
- );
- const hostGate = await Promise.race([hostReadyPromise, initiatorFinishedBeforeReady]);
- if (hostGate.status !== "ready") {
- if (hostGate.status === "failed" && params.options?.throwOnFailure) {
- throw hostGate.error;
- }
- return;
- }
- // sibling 只复用 initiator credential 命中 ready Host,不再读取各自历史 credential;
- // path/provider/task 初始化与 initiator 并行并保持独立失败语义。
- const siblingPromise = Promise.allSettled(
- siblings.map((entry) =>
- params.reconnectEntry(entry, {
- ...params.options,
- activateWorkspaceAfterReconnect: false,
- sshCredentialsOverride: hostGate.credentials,
- }),
- ),
- );
- const [initiatorResult] = await Promise.all([
- initiatorPromise.then(
- () => ({ status: "fulfilled" as const }),
- (error: unknown) => ({ status: "rejected" as const, error }),
- ),
- siblingPromise,
- ]);
- if (initiatorResult.status === "rejected" && params.options?.throwOnFailure) {
- throw initiatorResult.error;
- }
- }
- function shouldKeepRemoteWorkspaceInTabs(params: {
- tabStoreApi: RemoteWorkspaceTabStoreReader;
- workspacePath: string;
- workspaceIdentity?: string;
- }): boolean {
- const reconnectWorkspaceKey = params.workspaceIdentity?.trim() || params.workspacePath;
- return params.tabStoreApi
- .getState()
- .tabs.some(
- (tab): tab is import("@/store/tabStore.js").WorkspaceTabState =>
- isWorkspaceTab(tab) &&
- ((tab.workspaceIdentity?.trim() || tab.workspacePath) === reconnectWorkspaceKey ||
- tab.workspacePath === params.workspacePath),
- );
- }
- export async function cancelPendingRemoteReconnectsForWorkspaceKeys(params: {
- workspaceKeys: string[];
- pendingRequestIds: Map<string, string>;
- cancelPendingRemoteConnection?: (requestId?: string) => Promise<void>;
- logger: Pick<typeof logger, "warn">;
- }): Promise<void> {
- const requestIds = params.workspaceKeys.flatMap((workspaceKey) => {
- const requestId = params.pendingRequestIds.get(workspaceKey);
- if (!requestId) {
- return [];
- }
- params.pendingRequestIds.delete(workspaceKey);
- return [requestId];
- });
- if (!params.cancelPendingRemoteConnection || requestIds.length === 0) {
- return;
- }
- await Promise.all(
- requestIds.map(async (requestId) => {
- try {
- await params.cancelPendingRemoteConnection?.(requestId);
- } catch (error) {
- params.logger.warn("[Root] 取消远程 workspace 重连失败", {
- requestId,
- error,
- });
- }
- }),
- );
- }
- export async function openRemoteWorkspaceFromHistoryEntry({
- workspaceKey,
- tabStoreApi,
- getRemoteSessions,
- inflightReconnectWorkspaceKeys,
- activateTabByPath,
- setReconnectingRemoteWorkspaceKeys,
- loadCredential,
- connectRemoteWorkspaceTarget,
- resolveRemoteWorkspaceCanonicalPath,
- disposeRemoteWorkspaceSession,
- bindRemoteWorkspaceSessionContext,
- addTab,
- commitRemoteWorkspaceSessionMutation,
- resetLogsForWorkspaceKey,
- createReconnectRequestId,
- pendingReconnectRequestIds,
- reconnectImpl = reconnectRemoteWorkspaceHistoryEntry,
- onWorkspaceActivated,
- }: OpenRemoteWorkspaceFromHistoryParams): Promise<void> {
- const sessionEntry = getRemoteSessions().find(
- (entry) => buildWorkspaceSessionKey(entry) === workspaceKey,
- );
- if (!sessionEntry) {
- return;
- }
- if (inflightReconnectWorkspaceKeys.has(workspaceKey)) {
- return;
- }
- const existingWorkspaceTab = tabStoreApi
- .getState()
- .tabs.find(
- (tab): tab is import("@/store/tabStore.js").WorkspaceTabState =>
- isWorkspaceTab(tab) && buildWorkspaceSessionKey(tab) === workspaceKey,
- );
- if (existingWorkspaceTab?.remoteSessionId) {
- activateTabByPath(existingWorkspaceTab.workspacePath, {
- workspaceIdentity: existingWorkspaceTab.workspaceIdentity,
- });
- return;
- }
- // 选择页远程历史与侧栏重连都属于“恢复已有 remote workspace”语义。
- // 如果这里缺少“仍需保留该 workspace”的二次校验,用户在重连中移除后仍会被成功回调重新加回 tab。
- // 这里复用同一套 shouldKeep 判定,保证两条入口的竞态行为一致。
- resetLogsForWorkspaceKey(workspaceKey);
- inflightReconnectWorkspaceKeys.add(workspaceKey);
- const requestId = createReconnectRequestId?.();
- if (requestId) {
- pendingReconnectRequestIds?.set(workspaceKey, requestId);
- }
- try {
- await reconnectImpl({
- sessionEntry,
- activateTabByPath,
- setReconnectingRemoteWorkspaceKeys,
- loadCredential,
- connectRemoteWorkspaceTarget,
- resolveRemoteWorkspaceCanonicalPath,
- disposeRemoteWorkspaceSession,
- bindRemoteWorkspaceSessionContext,
- bindRemoteWorkspacePath,
- bindRemoteWorkspaceIdentity,
- upsertWorkspaceTab: addTab,
- commitRemoteWorkspaceSessionMutation,
- getRemoteSessions,
- logger,
- toast,
- shouldKeepReconnectedWorkspace: ({ workspacePath, workspaceIdentity }) =>
- shouldKeepRemoteWorkspaceInTabs({
- tabStoreApi,
- workspacePath,
- workspaceIdentity,
- }),
- onWorkspaceActivated: (target) => {
- logger.debug("[Root] 远程历史 workspace ready,提交 tab 与 draft 激活", target);
- onWorkspaceActivated?.(target);
- },
- options: {
- // 历史入口过去只回填 connected tab,却没有提交 tab activation 和 draft owner,
- // 所以连接成功后右侧仍停留在旧 workspace。共享 helper 只会在 services ready 后执行此提交,
- // 不会提前暴露缺少 remoteSessionId 的 remote-waiting tab。
- activateWorkspaceAfterReconnect: true,
- showErrorToast: true,
- requestId,
- },
- });
- } finally {
- inflightReconnectWorkspaceKeys.delete(workspaceKey);
- if (requestId && pendingReconnectRequestIds?.get(workspaceKey) === requestId) {
- pendingReconnectRequestIds.delete(workspaceKey);
- }
- }
- }
- export async function selectRemoteWorkspaceProjectFromDialog({
- canUseRemoteWorkspace,
- sessionId,
- path,
- localWorkspacePath,
- loadingMessage,
- getRemoteWorkspaceSession,
- connectionTarget,
- getWorkspaceTabs,
- resolveRemoteWorkspaceCanonicalPath,
- activateTabByPath,
- handleCancelRemoteProject,
- bindRemoteWorkspaceSessionContext,
- commitRemoteWorkspaceSessionMutation,
- getRemoteSessions,
- bindRemoteWorkspacePath,
- bindRemoteWorkspaceIdentity,
- addTab,
- onWorkspaceActivated,
- refreshPinnedTasks,
- refreshTimelineTasks,
- }: {
- canUseRemoteWorkspace: boolean;
- sessionId: string;
- path: string;
- localWorkspacePath?: string;
- loadingMessage: string;
- getRemoteWorkspaceSession: (sessionId: string) => RemoteWorkspaceSession | null;
- connectionTarget?: Parameters<IPlatformService["connectRemote"]>[0];
- getWorkspaceTabs: () => WindowTabState[];
- resolveRemoteWorkspaceCanonicalPath: (
- sessionId: string,
- workspacePath: string,
- ) => Promise<string>;
- activateTabByPath: (workspacePath: string, options?: { workspaceIdentity?: string }) => boolean;
- handleCancelRemoteProject: (sessionId: string) => Promise<void>;
- bindRemoteWorkspaceSessionContext: BindRemoteWorkspaceSessionContextFn;
- commitRemoteWorkspaceSessionMutation: (
- mutation: ReturnType<typeof buildRemoteWorkspaceSessionMutation>,
- ) => Promise<RemoteWorkspaceSessionEntry>;
- getRemoteSessions: () => RemoteWorkspaceSessionEntry[];
- bindRemoteWorkspacePath: (workspacePath: string, sessionId: string) => void;
- bindRemoteWorkspaceIdentity: (workspaceIdentity: string, sessionId: string) => void;
- addTab: (
- workspacePath: string,
- options?: {
- remoteSessionId?: string;
- remoteTarget?: Parameters<IPlatformService["connectRemote"]>[0];
- workspaceIdentity?: string;
- localWorkspacePath?: string;
- },
- ) => void;
- onWorkspaceActivated?: (target: { workspacePath: string; workspaceIdentity: string }) => void;
- refreshPinnedTasks: (params: {
- sessionId: string;
- workspacePath: string;
- workspaceIdentity?: string;
- }) => Promise<void>;
- refreshTimelineTasks: (params: {
- sessionId: string;
- workspacePath: string;
- workspaceIdentity?: string;
- }) => Promise<void>;
- }): Promise<void> {
- if (!canUseRemoteWorkspace) {
- throw new Error("Remote workspace is disabled in this mode");
- }
- const remoteSession = getRemoteWorkspaceSession(sessionId);
- if (!remoteSession) {
- throw new Error(loadingMessage);
- }
- const remoteTarget = connectionTarget ?? remoteSession.target;
- if (!remoteTarget) {
- // 手机 web relay 复用 remote session store 只做服务路由,没有本地可重连 target。
- // 远程历史的选目录/持久化流程必须有 target,缺失时直接阻断,避免把无 target 的桥接 session 写进历史。
- throw new Error(`远程 workspace session 缺少连接目标: ${sessionId}`);
- }
- const canonicalPath = await resolveRemoteWorkspaceCanonicalPath(sessionId, path);
- const workspaceIdentity = buildRemoteWorkspaceIdentity(canonicalPath, remoteTarget);
- const existingWorkspaceTab = getWorkspaceTabs().find(
- (tab): tab is import("@/store/tabStore.js").WorkspaceTabState =>
- isWorkspaceTab(tab) &&
- tab.workspacePath === canonicalPath &&
- (tab.workspaceIdentity?.trim() || tab.workspacePath) === workspaceIdentity,
- );
- if (
- existingWorkspaceTab?.remoteSessionId &&
- activateTabByPath(canonicalPath, { workspaceIdentity })
- ) {
- onWorkspaceActivated?.({ workspacePath: canonicalPath, workspaceIdentity });
- await handleCancelRemoteProject(sessionId);
- return;
- }
- // 断连 tab 以前会在 provider/session 绑定完成前先被 activateTabByPath 激活,
- // V4PaneConversationProvider 此时只能得到 remote-waiting,因 rpcReady=false 返回 null,
- // 右侧便会先空白,等后续 addTab 写回 remoteSessionId 后才出现新建对话。
- // 断连 tab 与首次连接统一等到服务绑定完成后再由 addTab 原子激活,避免暴露半连接 workspace。
- // 新建连接不带 context,main/host 的 logical session
- // descriptor 停留在连接根目录 "/",identity 也是 Host 用解析后 target 自建的;而 tab、远程历史与
- // 手机可见 workspace 列表用的都是这里算出的 canonicalPath/workspaceIdentity。
- // 手机桥接(attachRemoteWorkspaceSessionHost)要求二者三元全等,所以选目录后必须先把 canonical
- // context 绑定回 main,再提交 connected 状态与 tab。df2db1df7a 把 provider 同步移到 main/host 时
- // 顺带删掉了这次 bind,导致新建连接后选的目录在手机端必然被 REMOTE_WORKSPACE_IDENTITY_MISMATCH 拒绝。
- // bind 失败时 fail-closed:回收 session 并把错误抛回连接弹窗,不留下 descriptor 与 tab 不一致的 session。
- try {
- await bindRemoteWorkspaceSessionContext({
- sessionId,
- workspacePath: canonicalPath,
- workspaceIdentity,
- });
- } catch (error) {
- await handleCancelRemoteProject(sessionId);
- throw error;
- }
- await commitRemoteWorkspaceSessionMutation(
- buildRemoteWorkspaceSessionMutation({
- remoteSessions: getRemoteSessions(),
- workspacePath: canonicalPath,
- localWorkspacePath,
- workspaceIdentity,
- target: remoteTarget,
- lastConnectionStatus: "connected",
- touchOpenedAt: true,
- }),
- );
- bindRemoteWorkspacePath(canonicalPath, sessionId);
- bindRemoteWorkspaceIdentity(workspaceIdentity, sessionId);
- addTab(canonicalPath, {
- remoteSessionId: sessionId,
- remoteTarget: stripRemoteTargetSecrets(remoteTarget),
- workspaceIdentity,
- localWorkspacePath,
- });
- // startDraft 之前在调用方等待 pinned/timeline 刷新结束后才执行,
- // tab 已切到远端但草稿 owner 仍是旧状态,形成可见的空白中间帧。
- // 激活回调必须紧跟 addTab,在任何列表刷新 await 之前提交同一 workspaceKey 的草稿态。
- onWorkspaceActivated?.({ workspacePath: canonicalPath, workspaceIdentity });
- await Promise.all([
- refreshPinnedTasks({
- sessionId,
- workspacePath: canonicalPath,
- workspaceIdentity,
- }),
- refreshTimelineTasks({
- sessionId,
- workspacePath: canonicalPath,
- workspaceIdentity,
- }),
- ]);
- }
- export function useRemoteWorkspaceHistory({
- intl,
- services,
- platform,
- supportsSettings,
- allowRemoteWorkspace = true,
- ensureConversationWorkspaceOnRestore = false,
- deferInactiveWorkspaceRestore = false,
- unavailableWorkspacePath,
- tabStoreApi,
- activateTabByPath,
- addTab,
- onWorkspaceActivated,
- }: {
- intl: ReturnType<typeof import("@/i18n/IntlProvider.js").useZCodeIntl>["intl"];
- services: IServiceAccessor;
- platform: IPlatformService;
- supportsSettings: boolean;
- allowRemoteWorkspace?: boolean;
- ensureConversationWorkspaceOnRestore?: boolean;
- /** 仅 Desktop 主窗口:输入可用后再把 inactive workspace 加入 sidebar/task 数据源。 */
- deferInactiveWorkspaceRestore?: boolean;
- unavailableWorkspacePath?: string;
- tabStoreApi: ReturnType<typeof import("@/store/TabStoreProvider.js").useTabStoreApi>;
- activateTabByPath: (workspacePath: string, options?: { workspaceIdentity?: string }) => boolean;
- addTab: (
- workspacePath: string,
- options?: {
- remoteSessionId?: string;
- remoteTarget?: Parameters<IPlatformService["connectRemote"]>[0];
- workspaceIdentity?: string;
- localWorkspacePath?: string;
- },
- ) => void;
- onWorkspaceActivated?: (target: { workspacePath: string; workspaceIdentity: string }) => void;
- }) {
- const showRemoteConnectionEntry = useRemoteConnectionEntryVisibility();
- const canUseRemoteWorkspace = allowRemoteWorkspace && showRemoteConnectionEntry;
- const allowRemoteWorkspaceRestore = canUseRemoteWorkspace;
- const [remoteWorkspaceSessions, setRemoteWorkspaceSessions] = useState<
- RemoteWorkspaceSessionEntry[]
- >([]);
- const remoteWorkspaceSessionsRef = useRef<RemoteWorkspaceSessionEntry[]>([]);
- const [reconnectingRemoteWorkspaceKeys, setReconnectingRemoteWorkspaceKeys] = useState<string[]>(
- [],
- );
- const inflightReconnectWorkspaceKeysRef = useRef<Set<string>>(new Set());
- const pendingReconnectRequestIdsRef = useRef<Map<string, string>>(new Map());
- const pendingConnectionTargetsBySessionIdRef = useRef<
- Map<string, Parameters<IPlatformService["connectRemote"]>[0]>
- >(new Map());
- // 上一个版本这里有一个启动重连尝试用的 useRef。
- // 移除自动重连逻辑后,Vite Fast Refresh 会复用旧 fiber 的 hook slot,
- // 导致下一层 useReconnectingRemoteWorkspaceLogs 里的 useState 落到旧 useRef slot 上并触发 React "Should have a queue"。
- // 保留一个空 ref 只用于稳定热更新中的 hook 顺序,不恢复任何启动重连行为。
- const remoteStartupReconnectRefreshCompatibilityRef = useRef<null>(null);
- void remoteStartupReconnectRefreshCompatibilityRef;
- const {
- logsByWorkspaceKey: reconnectingRemoteWorkspaceLogsByWorkspaceKey,
- resetLogsForWorkspaceKey,
- } = useReconnectingRemoteWorkspaceLogs({
- platform,
- reconnectingWorkspaceKeys: reconnectingRemoteWorkspaceKeys,
- resolveWorkspaceTargetByKey: (workspaceKey) =>
- remoteWorkspaceSessionsRef.current.find(
- (entry) => buildWorkspaceSessionKey(entry) === workspaceKey,
- )?.target ?? null,
- resolveWorkspaceRequestIdByKey: (workspaceKey) =>
- pendingReconnectRequestIdsRef.current.get(workspaceKey) ?? null,
- });
- const syncPersistedWorkspaceSession = useCallback(
- async (nextRemoteSessions: readonly RemoteWorkspaceSessionEntry[]) => {
- setRemoteWorkspaceSessions([...nextRemoteSessions]);
- remoteWorkspaceSessionsRef.current = [...nextRemoteSessions];
- if (!supportsSettings) {
- return;
- }
- await services.settingService.update(
- buildRemoteWorkspacePersistPatch(tabStoreApi.getState(), nextRemoteSessions),
- );
- },
- [services.settingService, supportsSettings, tabStoreApi],
- );
- const commitRemoteWorkspaceSessionMutation = useCallback(
- async (mutation: ReturnType<typeof buildRemoteWorkspaceSessionMutation>) => {
- for (const credentialKey of mutation.credentialKeysToDelete) {
- try {
- await services.credentialService.delete(credentialKey);
- } catch (error) {
- logger.warn("[Root] 删除远程 workspace 凭据失败", {
- credentialKey,
- error,
- });
- }
- }
- for (const credential of mutation.credentialsToSave) {
- await services.credentialService.save(credential.key, credential.value);
- }
- await syncPersistedWorkspaceSession(mutation.nextRemoteSessions);
- return mutation.entry;
- },
- [services.credentialService, syncPersistedWorkspaceSession],
- );
- const waitForRemoteWorkspaceSessionReady = useCallback(async (sessionId: string) => {
- if (getRemoteWorkspaceSession(sessionId)) {
- return;
- }
- await new Promise<void>((resolve, reject) => {
- const startedAt = Date.now();
- const pollTimer = window.setInterval(() => {
- if (getRemoteWorkspaceSession(sessionId)) {
- window.clearInterval(pollTimer);
- resolve();
- return;
- }
- if (Date.now() - startedAt >= 3000) {
- window.clearInterval(pollTimer);
- reject(new Error(`等待远程 workspace session 就绪超时: ${sessionId}`));
- }
- }, 50);
- });
- }, []);
- const connectRemoteWorkspaceTarget = useCallback(
- async (
- target: Parameters<IPlatformService["connectRemote"]>[0],
- requestId?: string,
- context?: Parameters<IPlatformService["connectRemote"]>[2],
- ) => {
- const result = await platform.connectRemote(target, requestId, context);
- if (!result.success) {
- throw new Error(getErrorMessage(result.error || "Connection failed"));
- }
- if (!result.sessionId) {
- throw new Error("Remote session was not created");
- }
- // 远程连接成功只代表 main/host 已经建好 session,
- // renderer 侧的 MessagePort 仍然可能在下一拍才注册进 zustand store。
- // 如果此时立刻 addTab,会短暂走到本地 services,导致首屏读目录/预热 ZCode Agent 命中错误服务。
- // 这里等 session 真正挂进 store 再继续。
- await waitForRemoteWorkspaceSessionReady(result.sessionId);
- if (!context) {
- // 共享 Host 只向 renderer store 回传脱敏 target;完整凭据只在选目录流程完成前临时保留。
- pendingConnectionTargetsBySessionIdRef.current.set(result.sessionId, target);
- }
- return result.sessionId;
- },
- [platform, waitForRemoteWorkspaceSessionReady],
- );
- const resolveRemoteWorkspaceCanonicalPath = useCallback(
- async (sessionId: string, workspacePath: string): Promise<string> => {
- const remoteSession = getRemoteWorkspaceSession(sessionId);
- if (!remoteSession) {
- return workspacePath;
- }
- try {
- // 同一目录可能通过符号链接别名输入(例如 /dev 与 /home/dev),
- // 之前直接持久化用户输入会把同一 workspace 识别成两个身份。
- // 这里在远端 host 上做一次 realpath 归一化,再参与 identity 计算与持久化。
- return await remoteSession.services.fileService.resolvePath({
- path: workspacePath,
- });
- } catch {
- return workspacePath;
- }
- },
- [],
- );
- const buildPersistedTabPatch = useCallback(
- (state: TabStoreState) =>
- buildRemoteWorkspacePersistPatch(state, remoteWorkspaceSessionsRef.current),
- [],
- );
- const handleCancelRemoteProject = useCallback(
- async (sessionId: string) => {
- try {
- await platform.disposeRemoteSession(sessionId);
- } finally {
- pendingConnectionTargetsBySessionIdRef.current.delete(sessionId);
- // 远程目录选择如果在确认前就取消,session 释放失败也不能把前端状态卡在“仍有一个待选远程 session”。
- // 这里始终清掉本地映射,避免下次再次打开弹窗时复用到一条已经失效的 session 记录。
- unregisterRemoteWorkspaceSession(sessionId);
- }
- },
- [platform],
- );
- const bindRemoteWorkspaceSessionContext = useCallback<BindRemoteWorkspaceSessionContextFn>(
- async ({ sessionId, workspacePath, workspaceIdentity }) => {
- // bind 会换代同一 remoteSessionId 的 renderer attachment;
- // 复用 bindRemoteWorkspaceContextAndGetSession 等 ready ACK 后再返回,之后按 sessionId 读取的才是新代 services。
- await bindRemoteWorkspaceContextAndGetSession({
- platform,
- sessionId,
- workspacePath,
- workspaceIdentity,
- });
- },
- [platform],
- );
- const runReconnectRemoteWorkspaceEntry = useCallback(
- async (
- sessionEntry: RemoteWorkspaceSessionEntry,
- options?: ReconnectRemoteWorkspaceOptions,
- ): Promise<boolean> => {
- const workspaceKey = buildWorkspaceSessionKey(sessionEntry);
- if (inflightReconnectWorkspaceKeysRef.current.has(workspaceKey)) {
- return false;
- }
- const workspaceTab = tabStoreApi
- .getState()
- .tabs.find(
- (tab): tab is import("@/store/tabStore.js").WorkspaceTabState =>
- isWorkspaceTab(tab) && buildWorkspaceSessionKey(tab) === workspaceKey,
- );
- if (!workspaceTab || workspaceTab.remoteSessionId) {
- return false;
- }
- resetLogsForWorkspaceKey(workspaceKey);
- inflightReconnectWorkspaceKeysRef.current.add(workspaceKey);
- const requestId = createUuid();
- pendingReconnectRequestIdsRef.current.set(workspaceKey, requestId);
- try {
- // 重连期间只静默回填 ready 的 remoteSessionId;激活统一由 reconnect helper
- // 在回填之后执行,避免 active tab 暴露 remote-waiting 中间态。
- const upsertWorkspaceTab = tabStoreApi.getState().ensureWorkspaceTab;
- logger.debug("[Root] 远程 workspace 重连中,保留当前 conversation", {
- workspaceKey,
- });
- await reconnectRemoteWorkspaceHistoryEntry({
- sessionEntry,
- activateTabByPath,
- setReconnectingRemoteWorkspaceKeys,
- loadCredential: services.credentialService.load,
- connectRemoteWorkspaceTarget,
- resolveRemoteWorkspaceCanonicalPath,
- disposeRemoteWorkspaceSession: handleCancelRemoteProject,
- bindRemoteWorkspaceSessionContext,
- bindRemoteWorkspacePath,
- bindRemoteWorkspaceIdentity,
- upsertWorkspaceTab,
- commitRemoteWorkspaceSessionMutation,
- getRemoteSessions: () => remoteWorkspaceSessionsRef.current,
- logger,
- toast,
- shouldKeepReconnectedWorkspace: ({ workspacePath, workspaceIdentity }) =>
- shouldKeepRemoteWorkspaceInTabs({
- tabStoreApi,
- workspacePath,
- workspaceIdentity,
- }),
- onWorkspaceActivated: (target) => {
- logger.debug("[Root] 远程 workspace ready,提交 tab 与 draft 激活", target);
- onWorkspaceActivated?.(target);
- },
- options: {
- ...options,
- requestId,
- },
- });
- return true;
- } finally {
- inflightReconnectWorkspaceKeysRef.current.delete(workspaceKey);
- if (pendingReconnectRequestIdsRef.current.get(workspaceKey) === requestId) {
- pendingReconnectRequestIdsRef.current.delete(workspaceKey);
- }
- }
- },
- [
- activateTabByPath,
- bindRemoteWorkspaceSessionContext,
- commitRemoteWorkspaceSessionMutation,
- connectRemoteWorkspaceTarget,
- handleCancelRemoteProject,
- onWorkspaceActivated,
- resolveRemoteWorkspaceCanonicalPath,
- resetLogsForWorkspaceKey,
- services.credentialService,
- tabStoreApi,
- ],
- );
- const runReconnectRemoteWorkspace = useCallback(
- async (
- sessionEntry: RemoteWorkspaceSessionEntry,
- options?: ReconnectRemoteWorkspaceOptions,
- ) => {
- const reconnectGroup = collectSshReconnectGroup({
- selected: sessionEntry,
- sessions: remoteWorkspaceSessionsRef.current,
- tabs: tabStoreApi.getState().tabs,
- });
- await reconnectRemoteWorkspaceGroup({
- selected: sessionEntry,
- reconnectGroup,
- reconnectEntry: runReconnectRemoteWorkspaceEntry,
- options,
- });
- },
- [runReconnectRemoteWorkspaceEntry, tabStoreApi],
- );
- useEffect(() => {
- // 这里原来是启动自动重连 effect。
- // 删除 effect 本身会让 Fast Refresh 中已挂载的 RootInner 后续 hook 全部前移,
- // 旧 effect slot 被 useCallback 复用后容易触发 React hook 队列错位。
- // 这个空 effect 只保留 hook slot;启动恢复仍只产生断开态 tab,不会发起远程连接。
- const preserveRemoteStartupReconnectEffectSlot = true;
- void preserveRemoteStartupReconnectEffectSlot;
- }, []);
- const restorePersistedSession = useCallback(
- async (settings: AppSettings) => {
- const persistedRemoteSessions = getRemoteWorkspaceSessionEntries(settings);
- setRemoteWorkspaceSessions(persistedRemoteSessions);
- remoteWorkspaceSessionsRef.current = persistedRemoteSessions;
- let conversationWorkspacePath: string | undefined;
- if (ensureConversationWorkspaceOnRestore) {
- try {
- conversationWorkspacePath = (await services.fileService.ensureConversationWorkspace())
- .path;
- } catch (error) {
- // 路径创建失败不能连带吞掉真实项目恢复;后续显式新建对话仍会走原有可重试错误入口。
- logger.warn("[Root] 恢复阶段解析 conversation workspace 失败", { error });
- }
- }
- // 启动恢复远程 workspace 时只还原任务列表里的断开态 tab。
- // 之前这里之后还有后台 effect 会自动发起 SSH/WSL/Docker 重连,用户只是打开应用查看任务列表也会触发远端连接和 runtime 上传。
- // 现在把重连入口收口到用户点击“重连”或从远程历史主动打开,避免启动阶段产生隐藏副作用。
- // Web 普通模式还会把 allowRemoteWorkspaceRestore 置为 false:保留 setting 里的远程快照,但不恢复 tab/不展示入口。
- const workspaceRestore = restorePersistedRemoteWorkspaceSessions({
- settings,
- tabStoreApi,
- allowRemoteWorkspaceRestore,
- unavailableWorkspacePath,
- conversationWorkspacePath,
- restoreMode: deferInactiveWorkspaceRestore ? "active-first" : "all",
- });
- return {
- ...(conversationWorkspacePath
- ? { excludedRecentProjectPaths: [conversationWorkspacePath] }
- : {}),
- ...(workspaceRestore?.deferredRestore
- ? { deferredRestore: workspaceRestore.deferredRestore }
- : {}),
- };
- },
- [
- allowRemoteWorkspaceRestore,
- deferInactiveWorkspaceRestore,
- ensureConversationWorkspaceOnRestore,
- services.fileService,
- tabStoreApi,
- unavailableWorkspacePath,
- ],
- );
- const handleSelectRemoteProject = useCallback(
- async (sessionId: string, path: string, localWorkspacePath?: string) => {
- try {
- await selectRemoteWorkspaceProjectFromDialog({
- canUseRemoteWorkspace,
- sessionId,
- path,
- localWorkspacePath,
- loadingMessage: intl.formatMessage({ id: "common.loading" }),
- getRemoteWorkspaceSession,
- connectionTarget: pendingConnectionTargetsBySessionIdRef.current.get(sessionId),
- getWorkspaceTabs: () => tabStoreApi.getState().tabs,
- resolveRemoteWorkspaceCanonicalPath,
- activateTabByPath,
- handleCancelRemoteProject,
- bindRemoteWorkspaceSessionContext,
- commitRemoteWorkspaceSessionMutation,
- getRemoteSessions: () => remoteWorkspaceSessionsRef.current,
- bindRemoteWorkspacePath,
- bindRemoteWorkspaceIdentity,
- addTab,
- onWorkspaceActivated,
- refreshPinnedTasks: refreshRemotePinnedTasksForSession,
- refreshTimelineTasks: refreshRemoteTimelineTasksForSession,
- });
- } finally {
- pendingConnectionTargetsBySessionIdRef.current.delete(sessionId);
- }
- },
- [
- activateTabByPath,
- addTab,
- bindRemoteWorkspaceSessionContext,
- tabStoreApi,
- commitRemoteWorkspaceSessionMutation,
- handleCancelRemoteProject,
- intl,
- onWorkspaceActivated,
- resolveRemoteWorkspaceCanonicalPath,
- canUseRemoteWorkspace,
- ],
- );
- const handleConnectRemote = useCallback(
- async (
- options: Parameters<IPlatformService["connectRemote"]>[0],
- requestId?: string,
- context?: Parameters<IPlatformService["connectRemote"]>[2],
- ) => {
- if (!canUseRemoteWorkspace) {
- throw new Error("Remote workspace is disabled in this mode");
- }
- return connectRemoteWorkspaceTarget(options, requestId, context);
- },
- [canUseRemoteWorkspace, connectRemoteWorkspaceTarget],
- );
- const handleReconnectRemoteWorkspace = useCallback(
- async (workspaceKey: string, options?: ReconnectRemoteWorkspaceOptions) => {
- await reconnectRemoteWorkspaceByKey({
- workspaceKey,
- canUseRemoteWorkspace,
- getRemoteSessions: () => remoteWorkspaceSessionsRef.current,
- runReconnectRemoteWorkspace,
- options: {
- activateWorkspaceAfterReconnect: true,
- showErrorToast: true,
- throwOnFailure: false,
- ...options,
- },
- });
- },
- [canUseRemoteWorkspace, runReconnectRemoteWorkspace],
- );
- const handleOpenRemoteWorkspaceFromHistory = useCallback(
- async (workspaceKey: string) => {
- if (!canUseRemoteWorkspace) {
- return;
- }
- // 远程历史入口与侧栏重连都可能命中“重连中被用户移除”的竞态。
- // 这里抽成统一入口,确保两条路径共享相同的并发保护、保留校验与错误处理语义。
- await openRemoteWorkspaceFromHistoryEntry({
- workspaceKey,
- tabStoreApi,
- getRemoteSessions: () => remoteWorkspaceSessionsRef.current,
- inflightReconnectWorkspaceKeys: inflightReconnectWorkspaceKeysRef.current,
- activateTabByPath,
- setReconnectingRemoteWorkspaceKeys,
- loadCredential: services.credentialService.load,
- connectRemoteWorkspaceTarget,
- resolveRemoteWorkspaceCanonicalPath,
- disposeRemoteWorkspaceSession: handleCancelRemoteProject,
- bindRemoteWorkspaceSessionContext,
- addTab,
- commitRemoteWorkspaceSessionMutation,
- createReconnectRequestId: createUuid,
- pendingReconnectRequestIds: pendingReconnectRequestIdsRef.current,
- resetLogsForWorkspaceKey,
- onWorkspaceActivated,
- });
- },
- [
- activateTabByPath,
- addTab,
- bindRemoteWorkspaceSessionContext,
- commitRemoteWorkspaceSessionMutation,
- connectRemoteWorkspaceTarget,
- handleCancelRemoteProject,
- resolveRemoteWorkspaceCanonicalPath,
- resetLogsForWorkspaceKey,
- onWorkspaceActivated,
- services.credentialService,
- tabStoreApi,
- canUseRemoteWorkspace,
- ],
- );
- const handleRemoteWorkspaceSessionClosed = useCallback(
- async (event: RemoteSessionClosedEvent) => {
- const sessionId = event.sessionId.trim();
- if (!sessionId) {
- return;
- }
- const matchedTabs = tabStoreApi
- .getState()
- .tabs.filter(
- (tab): tab is import("@/store/tabStore.js").WorkspaceTabState =>
- isWorkspaceTab(tab) && tab.remoteSessionId === sessionId,
- );
- if (matchedTabs.length === 0) {
- unregisterRemoteWorkspaceSession(sessionId);
- return;
- }
- // 远端 host 退出后,tab 上的 remoteSessionId 之前不会被清空。
- // UI 因此持续显示“已连接”,并继续把请求路由到失效 session。
- // 这里在收到 main 进程的 session-close 事件时立即降级为断连态,后续只走用户手动重连。
- tabStoreApi.setState((state) => ({
- tabs: state.tabs.map((tab) =>
- isWorkspaceTab(tab) && tab.remoteSessionId === sessionId
- ? { ...tab, remoteSessionId: undefined }
- : tab,
- ),
- }));
- unregisterRemoteWorkspaceSession(sessionId);
- const matchedWorkspaceKeys = [
- ...new Set(matchedTabs.map((tab) => buildWorkspaceSessionKey(tab))),
- ];
- const reason = [
- "远程连接已断开",
- event.exitCode != null ? `exitCode=${event.exitCode}` : null,
- event.signal ? `signal=${event.signal}` : null,
- ]
- .filter(Boolean)
- .join(" ");
- const zcodeSessionStore = useZCodeSessionStore.getState();
- const failedTaskCount = markRemoteWorkspaceRunningTasksFailed({
- tabs: matchedTabs,
- getWorkspaceState: zcodeSessionStore.getWorkspaceState,
- setTaskRuntimeState: zcodeSessionStore.setTaskRuntimeState,
- reason,
- });
- logger.warn("[Root] 远程 workspace session 已关闭", {
- sessionId,
- reason: event.reason,
- exitCode: event.exitCode,
- signal: event.signal,
- matchedWorkspaceKeys,
- failedTaskCount,
- });
- for (const workspaceKey of matchedWorkspaceKeys) {
- const sessionEntry = remoteWorkspaceSessionsRef.current.find(
- (entry) => buildWorkspaceSessionKey(entry) === workspaceKey,
- );
- if (!sessionEntry) {
- continue;
- }
- const pendingReconnectRequestId = pendingReconnectRequestIdsRef.current.get(workspaceKey);
- if (
- !shouldPersistRemoteWorkspaceFailure({
- pendingReconnectRequestIds: pendingReconnectRequestIdsRef.current,
- sessionEntry,
- workspaceKey,
- })
- ) {
- logger.info("[Root] WSL workspace session 关闭时跳过失败落盘,等待重连结果", {
- pendingReconnectRequestId,
- sessionId,
- workspaceIdentity: sessionEntry.workspaceIdentity ?? null,
- workspacePath: sessionEntry.workspacePath,
- workspaceKey,
- });
- continue;
- }
- await commitRemoteWorkspaceSessionMutation(
- buildRemoteWorkspaceSessionMutation({
- remoteSessions: remoteWorkspaceSessionsRef.current,
- workspacePath: sessionEntry.workspacePath,
- workspaceIdentity: sessionEntry.workspaceIdentity,
- target: createRemoteTargetFromSnapshot(sessionEntry.target, {
- password: null,
- privateKeyPassphrase: null,
- }),
- lastConnectionStatus: "failed",
- lastConnectionError: reason,
- touchOpenedAt: false,
- }),
- );
- }
- },
- [commitRemoteWorkspaceSessionMutation, tabStoreApi],
- );
- const handleBotRemoteWorkspaceReconnected = useCallback(
- async (event: BotRemoteWorkspaceReconnectedEvent) => {
- if (!canUseRemoteWorkspace) {
- return;
- }
- const sessionId = event.sessionId.trim();
- if (!sessionId) {
- return;
- }
- try {
- await waitForRemoteWorkspaceSessionReady(sessionId);
- const remoteSession = getRemoteWorkspaceSession(sessionId);
- if (!remoteSession) {
- throw new Error(`远程 workspace session 不存在: ${sessionId}`);
- }
- if (!remoteSession.target) {
- // Bugfix: Bot 重连事件来自 main/host,必须带可持久化的 target。
- // Web relay 的桥接 session 没有 target,不能进入远程历史重连分支。
- throw new Error(`远程 workspace session 缺少连接目标: ${sessionId}`);
- }
- const resolvedWorkspacePath = await resolveRemoteWorkspaceCanonicalPath(
- sessionId,
- event.workspacePath,
- );
- const resolvedWorkspaceIdentity = resolveBotRemoteWorkspaceReconnectedIdentity({
- event,
- resolvedWorkspacePath,
- target: remoteSession.target,
- });
- bindRemoteWorkspacePath(resolvedWorkspacePath, sessionId);
- bindRemoteWorkspaceIdentity(resolvedWorkspaceIdentity, sessionId);
- // Bugfix: Bot 触发的远端重连发生在 main/host,不会经过侧栏手动重连的 React 流程。
- // 这里收到 main 的成功事件后,把已创建的 session 绑定回 tab 和远端历史,UI 才会从“未连接”变为“已连接”。
- tabStoreApi.getState().ensureWorkspaceTab(resolvedWorkspacePath, {
- remoteSessionId: sessionId,
- remoteTarget: remoteSession.target,
- workspaceIdentity: resolvedWorkspaceIdentity,
- });
- await commitRemoteWorkspaceSessionMutation(
- buildRemoteWorkspaceSessionMutation({
- remoteSessions: remoteWorkspaceSessionsRef.current,
- workspacePath: resolvedWorkspacePath,
- workspaceIdentity: resolvedWorkspaceIdentity,
- target: remoteSession.target,
- lastConnectionStatus: "connected",
- touchOpenedAt: true,
- }),
- );
- await Promise.all([
- refreshRemotePinnedTasksForSession({
- sessionId,
- workspacePath: resolvedWorkspacePath,
- workspaceIdentity: resolvedWorkspaceIdentity,
- }),
- refreshRemoteTimelineTasksForSession({
- sessionId,
- workspacePath: resolvedWorkspacePath,
- workspaceIdentity: resolvedWorkspaceIdentity,
- }),
- ]);
- } catch (error) {
- logger.warn("[Root] Bot 远端 workspace 重连成功后同步 UI 状态失败", {
- sessionId,
- workspacePath: event.workspacePath,
- workspaceIdentity: event.workspaceIdentity,
- error: error instanceof Error ? error.message : String(error),
- });
- }
- },
- [
- canUseRemoteWorkspace,
- commitRemoteWorkspaceSessionMutation,
- resolveRemoteWorkspaceCanonicalPath,
- tabStoreApi,
- waitForRemoteWorkspaceSessionReady,
- ],
- );
- useEffect(() => {
- return platform.onRemoteSessionClosed((event) => {
- void handleRemoteWorkspaceSessionClosed(event);
- });
- }, [handleRemoteWorkspaceSessionClosed, platform]);
- useEffect(() => {
- return platform.onBotRemoteWorkspaceReconnected((event) => {
- void handleBotRemoteWorkspaceReconnected(event);
- });
- }, [handleBotRemoteWorkspaceReconnected, platform]);
- const handleRemoteWorkspaceTabsClosed = useCallback(
- (workspaceKeys: string[]) => {
- if (workspaceKeys.length === 0) {
- return;
- }
- const workspaceKeySet = new Set(workspaceKeys);
- void (async () => {
- // 关闭断连态 remote tab 时,tab 上还没有 remoteSessionId,但 main 进程可能已经在上传 remote runtime。
- // 这里用重连 requestId 精准取消 pending host,避免 UI 已移除而后台 upload 继续跑。
- await cancelPendingRemoteReconnectsForWorkspaceKeys({
- workspaceKeys,
- pendingRequestIds: pendingReconnectRequestIdsRef.current,
- cancelPendingRemoteConnection: platform.cancelPendingRemoteConnection
- ? (requestId) =>
- platform.cancelPendingRemoteConnection?.(requestId) ?? Promise.resolve()
- : undefined,
- logger,
- });
- setReconnectingRemoteWorkspaceKeys((currentKeys) =>
- currentKeys.filter((key) => !workspaceKeySet.has(key)),
- );
- const removal = removeRemoteWorkspaceSessionEntries(
- remoteWorkspaceSessionsRef.current,
- workspaceKeys,
- );
- if (
- removal.nextRemoteSessions.length === remoteWorkspaceSessionsRef.current.length &&
- removal.credentialKeysToDelete.length === 0
- ) {
- return;
- }
- // 用户在侧栏“移除”远程 workspace 后,只删 tab 不够:
- // remoteWorkspaceSessionsRef 仍被持久化补丁合并回 setting.json,
- // 所以下次启动又会恢复同一个断连项。这里把显式移除视为删除远程历史,
- // 同时清理该历史独占的 SSH 凭据,避免留下不可达的 credential key。
- await syncPersistedWorkspaceSession(removal.nextRemoteSessions);
- for (const credentialKey of removal.credentialKeysToDelete) {
- try {
- await services.credentialService.delete(credentialKey);
- } catch (error) {
- logger.warn("[Root] 删除已移除远程 workspace 凭据失败", {
- credentialKey,
- error,
- });
- }
- }
- })();
- },
- [platform, services.credentialService, syncPersistedWorkspaceSession],
- );
- const remoteWorkspaceErrorByWorkspaceKey = useMemo(
- () =>
- Object.fromEntries(
- remoteWorkspaceSessions.flatMap((entry) =>
- entry.lastConnectionError?.trim()
- ? [[buildWorkspaceSessionKey(entry), entry.lastConnectionError] as const]
- : [],
- ),
- ),
- [remoteWorkspaceSessions],
- );
- return {
- remoteWorkspaceSessions,
- reconnectingRemoteWorkspaceKeys,
- remoteWorkspaceErrorByWorkspaceKey,
- reconnectingRemoteWorkspaceLogsByWorkspaceKey,
- buildPersistedTabPatch,
- restorePersistedSession,
- handleCancelRemoteProject,
- handleSelectRemoteProject,
- handleConnectRemote,
- handleReconnectRemoteWorkspace,
- handleOpenRemoteWorkspaceFromHistory,
- handleRemoteWorkspaceTabsClosed,
- };
- }
|