mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-22 16:10:50 +00:00
* fix(sftp): keep cancel shared write OPEN drain gated on callback Cancel settle must not force-complete sharedWriteOpenDrain — only channel error does — so stage cleanup cannot race a late truncating OPEN after #2720. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(sftp): bound cancel OPEN drain and unlink late truncates Restore cancel drain force-complete so stalled shared OPENs cannot hang the lease, and best-effort unlink late truncating "w" opens after close so force-complete cannot leave a recreated stage orphan. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(sftp): only unlink generated stages on cancel OPEN Gate cancel-time truncating OPEN unlink on generatedStagePath so in-place and symlink destinations keep their final path; cover with a late-OPEN test. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(sftp): unlink late staged OPEN after channel-error settle Late truncating generated-stage OPENs after settle must unlink even when transfer.cancelled is false, so channel-error force-complete cannot leave a recreated .netcatty-upload orphan. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(sftp): skip late OPEN unlink when same-id retry owns stage After shared write OPEN drain force-complete, a late truncating "w" callback still closed and unlinked generated stages. Resumable retries reuse `.netcatty-<transferId>.part`, so a stale callback could delete the new attempt's stage. Gate unlink on activeTransfers ownership (generated stages + truncating only), matching the in-place final target guard. * fix(sftp): only skip late OPEN unlink for reusable resume stages Same-id retry ownership must not suppress unlink for every generated stage path. Non-resumable uploads use unique .netcatty-upload-* paths; a stale late OPEN on those must still clean orphans. Only deterministic .netcatty-<transferId>.part resume stages are protected. * fix(sftp): gate late OPEN unlink skip on actual staged path Only suppress late generated-stage unlink when the same-id retry's active.stagedRemote.path matches. Do not treat resumable+targetPath or path shape alone as ownership for in-place retries. * fix(sftp): compare late OPEN stage paths in the same representation OPEN filePath may be a session-encoded Buffer while stagedRemote.path is the logical string, so strict equality failed and could unlink a same-id retry stage. Normalize via encodePathForSession / Buffer equals before ownership skip. * fix(sftp): gate truncating shared OPEN against same-path retries Serialize truncating shared-write OPENs per remote path so a same-id retry cannot issue OPEN "w" while a prior attempt is still unsettled. When a drain force-complete already released the gate and a late stale OPEN re-truncates a retry-owned resume stage, invalidate that retry instead of allowing a sparse promote. Keep late-OPEN unlink skip for retry-owned stages. * fix(sftp): bound best-effort unlink so cancel cannot hang Shared/sudo cancel after OPEN "w" waited on unlink before finishCancel. A dead channel that never invokes the unlink callback left the transfer and its session lease active indefinitely. Cap unlink at the same 2s grace used for shared-write drain force-complete. * fix(sftp): make truncating shared OPEN path-gate waits cancelable A same-path upload queued behind a prior truncating OPEN could keep its lease/admission slot forever if cancelled before runOpen. Race the gate wait with transfer.abort and release the gate + drain on cancel. * fix(sftp): scope truncating OPEN path gates per SFTP session A module-global path-only key serialized unrelated hosts that shared the same remote pathname. Include transfer/sftp session identity in the gate key so only same-endpoint opens wait on each other. * fix(sftp): refuse stage promote after stale OPEN truncation A late force-completed OPEN w from a prior same-id attempt can zero the deterministic stage after concurrent write finishes. Latch staleOpenTruncatedStage through assertCanPromote and uploadFileConcurrent so promotion cannot rename a corrupt stage. * fix(sftp): keep path gate chained when canceling a queued OPEN wait Releasing a cancelled waiter's gate immediately cleared the barrier while the prior truncating OPEN was still in flight. Chain the waiter's release to waitForPrior so later same-path attempts stay serialized. * fix(sftp): hold reusable-stage path gate past OPEN force-complete Force-completing drain no longer frees the path gate for generated stages immediately. Same-id retries stay serialized until the late OPEN settles (or a 10s hard cap) so promotion cannot race a still-processing OPEN w. * test(sftp): wait for generated-stage path gate hold on retry OPEN After force-complete, generated stages keep the path gate for 10s more. Align the late-OPEN retry test wait with that hold. * fix(sftp): hold truncating OPEN path gates for in-place targets too In-place shared OPEN force-complete no longer frees the path gate immediately, and late OPEN invalidation applies to final-path retries so a stale truncating OPEN cannot wipe an in-flight same-id rewrite. * fix(sftp): keep path gate held until OPEN settles (no hard cap) Remove the post-force-complete gate timeout so a different transferId cannot reopen the same target while a stale truncating OPEN is pending. * test(sftp): accept normal {transferId,totalBytes} retry success startTransferNow returns transferId/totalBytes on success, not success:true. * fix(sftp): reject path-gate waits when the SFTP channel errors Queued same-path OPEN waiters now observe channel error while waiting on the prior gate, so a dead session cannot pin lease/admission forever. * fix(sftp): treat superseded OPEN as cancelled, not failed Late OPEN rejections after a same-id retry must not broadcast a terminal failed event that marks the live retry task failed in transfer center. * fix(sftp): emit cancelled channel for superseded OPEN failures Worker fan-out only maps transfer:error as cancelled for cancel messages. Superseded attempts now use transfer:cancelled so retries are not failed. * fix(sftp): silence superseded OPEN terminal events for live retries When a same-id retry already owns activeTransfers, do not emit cancelled or failed for the stale superseded attempt or the transfer center sticks. * fix(sftp): release path gate on OPEN force-complete for dead channels When the OPEN callback never arrives, free same-path waiters after drain force-complete so later uploads do not hang forever on the gate. * fix(sftp): hold path gate until OPEN settles; 30s dead-channel grace Force-complete no longer frees the gate immediately. Late OPEN release keeps retries serialized; a 30s grace frees dead channels. * fix(sftp): never force-release path gate on OPEN drain force-complete A dead-channel force-complete must free the transfer/lease, but keep the path gate held until the OPEN callback actually settles. Force-releasing lets a same-path retry write then get wiped by a late truncating OPEN. * fix(sftp): skip upload fallback when OPEN is superseded by a retry A same-id retry can already own the transfer while the old OPEN callback still rejects. Mark that rejection noTransferFallback so we never truncate the shared stage via prepareUploadFallbackCheckpoint under the retry. * fix(sftp): bound late OPEN close before releasing path gate After drain force-complete, a late truncating OPEN must still close the handle before releasing the path gate — but a dead channel may never invoke CLOSE. Race close with a 2s timeout so same-path uploads cannot wait forever on pathGate.waitForPrior. * chore: re-trigger Codex review automation * fix(sftp): always suppress terminal events for superseded OPEN attempts Superseded means this attempt lost ownership. Do not require the retry to still own activeTransfers — if the retry already completed, emitting cancelled for the same transferId rewrites a completed transfer. * chore: re-trigger Codex for path-gate head * chore: nudge Codex automation after cancelled run * fix(sftp): keep SFTP leases when suppressing superseded attempts Superseded OPEN cleanup must not release transferId leases — a same-id retry shares that lease set entry and would lose its hold if we release. * fix(sftp): do not return result.error for superseded transfer attempts Renderer directory ops treat any startStreamTransfer error as failure. Superseded same-id OPEN attempts should return a non-error marker so the live retry is not marked failed. * fix(sftp): bound CLOSE on pre-settle cancel OPEN path Reuse the 2s close timeout for cancel-before-settle so a dead channel cannot leave the path gate held after finishCancel never runs. * fix(sftp): hold path gate until unlink callback settles Remove the 2s force-complete on late shared OPEN unlink so a delayed server unlink cannot race a same-id retry that already reopened the stage. * fix(sftp): path-gate r+ resume OPENs; release only unused superseded leases Serialize all shared write opens including resume r+. On superseded, release only SFTP leases not also held by the live same-id retry. * fix(sftp): serialize shared and isolated OPENs by host+path Path gates keyed only by SFTP object let an isolated-channel retry race a stale shared truncating OPEN on the same host path. Prefer targetHostId so both channels wait on the same gate. * fix(sftp): path-gate isolated writes; revalidate checkpoint after gate Gate all write OPENs including isolated channels so a shared stale OPEN "w" cannot race. After waiting on the path gate, re-stat the stage and shrink the resume checkpoint (and flags) before issuing r+/w. * fix(sftp): dynamic truncating flags; cross-id stale OPEN; bound unlink Resolve r+/w at OPEN/unlink time so afterPathGate flag getters still unlink generated stages on cancel. Invalidate any same-path active transfer (not only same-id) when a late truncating OPEN lands, and force checkpoint restart when the stage was wiped before OPEN accepted. Bound best-effort unlink so a dead channel cannot hang cancel settle. * fix(sftp): clear staleOpenTruncatedStage after gated full restart When a late truncating OPEN zeros the stage while a retry waits on the path gate, afterPathGate restarts from offset zero. Consume the stale flag there so a successful full rewrite is not failed by the post-upload guard. * fix(sftp): keep cancel active during post-gate checkpoint revalidation After the path gate opens, afterPathGate may stat a remote stage. Keep the transfer abort hook and channel-error listener live for that await so cancel or a dead channel can still release the path gate and lease. * fix(sftp): hold path gate for isolated write OPENs until settle Isolated truncating OPENs previously released the host/path gate on cancel as soon as sftp.end() was called, allowing a same-path retry to race a still in-flight OPEN that can truncate the stage without invalidation. Keep the gate until the OPEN callback (or a bounded force-release after end), and run late OPEN invalidation for isolated channels too. * fix(sftp): close late shared read OPEN handles after settle Restores close of late shared/sudo OPEN "r" handles after cancel or channel error settle. Write-only gate invalidation remains for isolated late OPENs. * fix(sftp): copy host IDs onto transfer for path-gate keys dedicatedTransferResume passes targetHostId with a new targetSftpId. Keep those host IDs on the transfer object so sharedWriteOpenSessionKey stays host-scoped across session replacement instead of falling back to sftpId. * fix(sftp): do not complete directory children on superseded invoke When a same-id retry steals ownership mid-OPEN, the old startStreamTransfer returns { superseded: true }. Wait for the live owner's terminal task status instead of treating that result as a successful child completion. * fix(sftp): pass host IDs on background restart; scope stale-OPEN invalidation Background agent resume now includes source/target host IDs so path gates stay host-scoped after openSftpForSession. Stale OPEN invalidation requires a matching host or sftp session instead of treating missing host as a wildcard. * fix(sftp): do not unlink stage after same-transfer fastPut fallback When concurrent isolated OPEN dies without a callback and upload falls back to fastPut on the same generated stage, a late OPEN must not unlink that stage. Skip unlink when the same transfer already accepted an OPEN, owns stagedRemote, or moved to a fastPut/stream strategy. * fix(sftp): narrow fastPut unlink skip; handle superseded in all callers Only suppress late-stage unlink when the same transfer moved to fastPut/stream — ordinary cancel still unlinks. Background restart and dedicated resume wait for the live same-id owner instead of treating superseded invokes as completed. * fix(sftp): never force-release isolated write path gates Hold host/path gates for isolated truncating OPENs until the OPEN callback settles, matching shared-channel behavior. Force-releasing after sftp.end() allowed a late OPEN to wipe a completed same-path destination with no live transfer left to invalidate. * fix(sftp): hold path gate until late unlink settles Transfer cancel can settle after a 2s unlink grace so dead channels do not hang invokes, but the host/path gate stays held until the unlink callback actually fires so a delayed unlink cannot delete a same-path retry stage. * fix(sftp): wait for live owner without fixed superseded deadline Superseded invokes now wait only on the live same-id transfer's terminal status (or cancel). A 30-minute timeout could mark a still-running long transfer failed and stick that status after the owner later completed. * fix(sftp): classify superseded before cancelled cleanup When a cancelled attempt loses ownership to a same-id retry mid-OPEN, the error is Transfer superseded while cancelled is still true. Handle supersede first so cancel cleanup cannot delete the retry stage, free its leases, or emit a terminal cancelled event for the live owner. * fix(sftp): wait for pending write OPEN before fastPut fallback Expose pendingWriteOpenPathGate on the transfer so non-resumable concurrent isolated failures wait for their own truncating OPEN to settle before fastPut rewrites the same stage. --------- Co-authored-by: Cursor <cursoragent@cursor.com>
549 lines
23 KiB
TypeScript
549 lines
23 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
import { useEffect, useRef } from 'react';
|
|
import { usePortForwardingAutoStart } from '../state/usePortForwardingAutoStart';
|
|
import { editorTabStore } from '../state/editorTabStore';
|
|
import { netcattyBridge } from '../../infrastructure/services/netcattyBridge';
|
|
import { localStorageAdapter } from '../../infrastructure/persistence/localStorageAdapter';
|
|
import { toast } from '../../components/ui/toast';
|
|
import { sftpTransferCenterStore } from '../state/sftpTransferCenterStore';
|
|
import { resumeTransferWithDedicatedSession } from '../state/sftp/dedicatedTransferResume';
|
|
import { getSftpTransferResourceKeys, globalSftpTransferScheduler } from '../state/sftp/globalTransferScheduler';
|
|
import { hasNewSourceFingerprint } from '../state/sftp/transferProgressMetadata';
|
|
import { STORAGE_KEY_SFTP_TRANSFER_CONCURRENCY } from '../../infrastructure/config/storageKeys';
|
|
import type { TransferTask } from '../../domain/models';
|
|
import {
|
|
canApplyDedicatedResumeProgress,
|
|
createDedicatedResumeChildUpdateBatcher,
|
|
createDedicatedResumeProgressBatcher,
|
|
} from './dedicatedResumeProgress';
|
|
|
|
type StartupEffectsContext = Record<string, any>;
|
|
|
|
type KeyboardInteractiveScope = "terminal" | "external";
|
|
type KeyboardInteractiveRequestLike = {
|
|
scope?: KeyboardInteractiveScope;
|
|
sessionId?: string;
|
|
hostId?: string;
|
|
};
|
|
type SessionIdLike = { id: string; hostId?: string; hostname?: string };
|
|
type KeyboardInteractiveQueueItem = { requestId: string };
|
|
|
|
export function shouldQueueKeyboardInteractiveRequest(
|
|
request: KeyboardInteractiveRequestLike,
|
|
sessions: SessionIdLike[],
|
|
): boolean {
|
|
if (request.scope !== "terminal") return true;
|
|
if (!request.sessionId) return false;
|
|
return sessions.some((session) => session.id === request.sessionId);
|
|
}
|
|
|
|
export function removeKeyboardInteractiveRequest<T extends KeyboardInteractiveQueueItem>(
|
|
queue: T[],
|
|
requestId: string,
|
|
): T[] {
|
|
return queue.filter(request => request.requestId !== requestId);
|
|
}
|
|
|
|
export function useAppStartupEffects(ctx: StartupEffectsContext) {
|
|
const {dismissUpdate, enabled = true, groupConfigs, hosts, resumeHosts, identities,
|
|
hasRuntimeTunnel, installUpdate, isVaultInitialized, keys, knownHosts, openSettingsWindow, portForwardingRules, proxyProfiles, sessions, setKeyboardInteractiveQueue,
|
|
t, terminalSettings, updateState, workspaces,
|
|
} = ctx;
|
|
// Vault hosts for tray/menu; resumeHosts may include ephemeral quick-connect rows.
|
|
const dedicatedResumeHosts = resumeHosts ?? hosts;
|
|
const sessionsRef = useRef(sessions);
|
|
|
|
useEffect(() => {
|
|
sessionsRef.current = sessions;
|
|
}, [sessions]);
|
|
|
|
// After app restart (or soft-resume miss), unfinished transfers reconnect via
|
|
// a dedicated SFTP session. Prefer resumeHosts (vault + ephemeral) so
|
|
// quick-connect transfers can re-auth without "Cannot find host in your vault".
|
|
useEffect(() => {
|
|
if (!enabled || !isVaultInitialized) {
|
|
sftpTransferCenterStore.setDedicatedResumeHandler(null);
|
|
return;
|
|
}
|
|
sftpTransferCenterStore.setDedicatedResumeHandler(async (task) => {
|
|
// Keep reconnectRequired true until the first progress/completion so the
|
|
// play control stays a spinner during auth + session setup.
|
|
sftpTransferCenterStore.patchTask(task.id, {
|
|
status: "pending",
|
|
error: undefined,
|
|
reconnectRequired: true,
|
|
speed: 0,
|
|
phase: undefined,
|
|
});
|
|
const children = sftpTransferCenterStore.getSnapshot().tasks.filter(
|
|
(row) => row.parentTaskId === task.id,
|
|
);
|
|
// rAF-coalesce progress so dedicated resume does not flood the global center.
|
|
type ProgressSample = {
|
|
transferred: number;
|
|
total: number;
|
|
speed: number;
|
|
checkpointBytes?: number;
|
|
resumeStage?: TransferTask["resumeStage"];
|
|
downloadCheckpointBytes?: number;
|
|
uploadCheckpointBytes?: number;
|
|
sourceFingerprint?: string;
|
|
};
|
|
// One rAF coalesce only — main process already time-throttles IPC.
|
|
// A second 500ms timer here made dedicated-resume bars jump.
|
|
const applyProgress = (progress: ProgressSample) => {
|
|
const current = sftpTransferCenterStore.getSnapshot().tasks.find((row) => row.id === task.id);
|
|
if (!current || current.status === "cancelled") return;
|
|
if (current.status === "pausing" || current.status === "paused") {
|
|
if (hasNewSourceFingerprint(current.sourceFingerprint, progress.sourceFingerprint)) {
|
|
sftpTransferCenterStore.patchTask(task.id, { sourceFingerprint: progress.sourceFingerprint });
|
|
}
|
|
return;
|
|
}
|
|
// The final sample can still be queued in requestAnimationFrame after
|
|
// the resume promise settles. Never let it turn a completed/failed row
|
|
// back into a permanently "transferring" task.
|
|
if (!canApplyDedicatedResumeProgress(current.status)) return;
|
|
// Directory parents use file-count progress; single files use bytes.
|
|
// Prefer durable contiguous checkpoint when the bridge supplies it.
|
|
const durableCheckpoint = task.isDirectory
|
|
? progress.transferred
|
|
: (progress.checkpointBytes ?? progress.transferred);
|
|
// Keep progress monotonic so a late force-checkpoint paint cannot hide
|
|
// later bytes, and the bar never freezes at the pre-quit offset.
|
|
const nextTransferred = Math.max(current.transferredBytes ?? 0, progress.transferred);
|
|
const nextCheckpoint = task.isDirectory
|
|
? Math.max(current.checkpointBytes ?? 0, progress.transferred)
|
|
: Math.max(current.checkpointBytes ?? 0, durableCheckpoint);
|
|
sftpTransferCenterStore.patchTask(task.id, {
|
|
status: "transferring",
|
|
transferredBytes: nextTransferred,
|
|
...(progress.total > 0 ? { totalBytes: progress.total } : {}),
|
|
speed: progress.speed,
|
|
...(task.isDirectory
|
|
? { checkpointBytes: nextCheckpoint, progressMode: "files" as const }
|
|
: {
|
|
checkpointBytes: nextCheckpoint,
|
|
resumeStage: progress.resumeStage,
|
|
downloadCheckpointBytes: progress.downloadCheckpointBytes,
|
|
uploadCheckpointBytes: progress.uploadCheckpointBytes,
|
|
sourceFingerprint: progress.sourceFingerprint,
|
|
}),
|
|
reconnectRequired: false,
|
|
error: undefined,
|
|
phase: "transferring",
|
|
ownerId: "dedicated-resume",
|
|
});
|
|
};
|
|
const progressBatcher = createDedicatedResumeProgressBatcher<ProgressSample>({
|
|
requestFrame: (callback) => window.requestAnimationFrame(callback),
|
|
cancelFrame: (handle) => window.cancelAnimationFrame(handle),
|
|
canApply: () => {
|
|
const current = sftpTransferCenterStore.getSnapshot().tasks.find((row) => row.id === task.id);
|
|
return !!current && canApplyDedicatedResumeProgress(current.status);
|
|
},
|
|
apply: applyProgress,
|
|
});
|
|
const childUpdateBatcher = createDedicatedResumeChildUpdateBatcher({
|
|
// Use the restart snapshot, not repeated linear store lookups. Completed
|
|
// rows disappear as batches compact, but later updates for those ids can
|
|
// still stay in the same bounded batching path safely.
|
|
getTaskCount: () => children.length + 1,
|
|
hasTask: (() => {
|
|
const retainedChildIds = new Set(children.map((child) => child.id));
|
|
return (taskId: string) => retainedChildIds.has(taskId);
|
|
})(),
|
|
upsertTasks: (updates) => sftpTransferCenterStore.upsertTasks(updates),
|
|
});
|
|
let acceptsResumeCallbacks = true;
|
|
try {
|
|
return await resumeTransferWithDedicatedSession(
|
|
task,
|
|
{
|
|
hosts: dedicatedResumeHosts,
|
|
keys,
|
|
identities,
|
|
knownHosts,
|
|
terminalSettings,
|
|
},
|
|
(progress) => {
|
|
if (acceptsResumeCallbacks) progressBatcher.push(progress);
|
|
},
|
|
{
|
|
children,
|
|
onChildUpdate: (child) => {
|
|
if (acceptsResumeCallbacks) {
|
|
childUpdateBatcher.push({ ...child, ownerId: "dedicated-resume" });
|
|
}
|
|
},
|
|
onDirectoryCheckpointUpdate: (checkpoint) => {
|
|
if (acceptsResumeCallbacks) {
|
|
sftpTransferCenterStore.patchTask(task.id, {
|
|
directoryResumeCheckpoint: checkpoint,
|
|
});
|
|
}
|
|
},
|
|
shouldAbort: () => {
|
|
const current = sftpTransferCenterStore.getSnapshot().tasks.find((row) => row.id === task.id);
|
|
// interrupted is the pre-reconnect persisted state — do not abort a
|
|
// live dedicated walk just because children/parent still show it.
|
|
return !current
|
|
|| current.status === "cancelled"
|
|
|| current.status === "paused";
|
|
},
|
|
},
|
|
);
|
|
} finally {
|
|
acceptsResumeCallbacks = false;
|
|
progressBatcher.finish();
|
|
childUpdateBatcher.flush();
|
|
}
|
|
});
|
|
return () => sftpTransferCenterStore.setDedicatedResumeHandler(null);
|
|
}, [dedicatedResumeHosts, enabled, identities, isVaultInitialized, keys, knownHosts, terminalSettings]);
|
|
|
|
// Show toast notification when update is available (only when auto-download is idle)
|
|
useEffect(() => {
|
|
if (!enabled) return;
|
|
// Skip "update available" toast if auto-download has already started or completed
|
|
if (updateState.autoDownloadStatus !== 'idle') return;
|
|
// Don't show automatic notification when auto-update is disabled
|
|
if (localStorageAdapter.readString('netcatty_auto_update_enabled_v1') === 'false') return;
|
|
if (updateState.hasUpdate && updateState.latestRelease) {
|
|
const version = updateState.latestRelease.version;
|
|
toast.info(
|
|
t('update.available.message', { version }),
|
|
{
|
|
title: t('update.available.title'),
|
|
duration: 8000, // Show longer for update notifications
|
|
onClick: () => {
|
|
void openSettingsWindow();
|
|
// Dismiss the update so the toast doesn't re-fire on every render.
|
|
// On unsupported platforms (where autoDownloadStatus stays 'idle')
|
|
// this is the only way to suppress the notification for this version.
|
|
// On supported platforms this toast only shows before auto-download
|
|
// starts, and the Settings window's own useUpdateCheck will pick up
|
|
// the download state via IPC events independently of the dismiss.
|
|
dismissUpdate();
|
|
},
|
|
actionLabel: t('update.viewInSettings'),
|
|
}
|
|
);
|
|
}
|
|
}, [enabled, updateState.hasUpdate, updateState.latestRelease, updateState.autoDownloadStatus, t, openSettingsWindow, dismissUpdate]);
|
|
|
|
// Track previous autoDownloadStatus so toast effects fire only on actual transitions,
|
|
// not when unrelated deps (installUpdate, openSettingsWindow) change their reference.
|
|
const prevAutoDownloadStatusRef = useRef(updateState.autoDownloadStatus);
|
|
useEffect(() => {
|
|
if (!enabled) return;
|
|
const prev = prevAutoDownloadStatusRef.current;
|
|
prevAutoDownloadStatusRef.current = updateState.autoDownloadStatus;
|
|
if (prev === updateState.autoDownloadStatus) return;
|
|
|
|
if (updateState.autoDownloadStatus === 'ready') {
|
|
const version = updateState.latestRelease?.version ?? '';
|
|
toast.info(
|
|
t('update.readyToInstall.message', { version }),
|
|
{
|
|
title: t('update.readyToInstall.title'),
|
|
duration: 0,
|
|
actionLabel: t('update.restartNow'),
|
|
onClick: () => installUpdate(),
|
|
}
|
|
);
|
|
} else if (updateState.autoDownloadStatus === 'error') {
|
|
toast.error(
|
|
t('update.downloadFailed.message'),
|
|
{
|
|
title: t('update.downloadFailed.title'),
|
|
actionLabel: t('update.viewInSettings'),
|
|
onClick: () => void openSettingsWindow(),
|
|
}
|
|
);
|
|
}
|
|
}, [enabled, updateState.autoDownloadStatus, updateState.latestRelease?.version, t, installUpdate, openSettingsWindow]);
|
|
|
|
// Auto-start port forwarding rules on app launch
|
|
usePortForwardingAutoStart({
|
|
enabled,
|
|
isVaultInitialized,
|
|
hosts,
|
|
keys,
|
|
identities,
|
|
knownHosts,
|
|
proxyProfiles,
|
|
groupConfigs,
|
|
terminalSettings,
|
|
});
|
|
|
|
// Sync tray menu data + handle tray actions
|
|
useEffect(() => {
|
|
if (!enabled) return;
|
|
const bridge = netcattyBridge.get();
|
|
if (!bridge?.updateTrayMenuData) return;
|
|
|
|
let cancelled = false;
|
|
const timer = setTimeout(() => {
|
|
if (cancelled) return;
|
|
|
|
const sessionsForTray = sessions.map((s) => {
|
|
const ws = s.workspaceId ? workspaces.find((w) => w.id === s.workspaceId) : undefined;
|
|
return {
|
|
id: s.id,
|
|
label: s.hostname,
|
|
hostLabel: s.hostLabel,
|
|
status: s.status,
|
|
workspaceId: s.workspaceId,
|
|
workspaceTitle: ws?.title,
|
|
aiHidden: s.hiddenFromTabs === true,
|
|
};
|
|
});
|
|
|
|
const hostsForSystemMenu = hosts
|
|
.filter((host: any) => typeof host?.id === "string" && host.id.length > 0)
|
|
.map((host: any) => ({
|
|
id: host.id,
|
|
label: host.label,
|
|
hostname: host.hostname,
|
|
group: host.group,
|
|
pinned: host.pinned,
|
|
lastConnectedAt: host.lastConnectedAt,
|
|
protocol: host.protocol,
|
|
}));
|
|
|
|
void bridge.updateTrayMenuData({
|
|
sessions: sessionsForTray,
|
|
portForwardRules: portForwardingRules.map((rule: any) => ({
|
|
...rule,
|
|
canStop: hasRuntimeTunnel(rule.id),
|
|
})),
|
|
hosts: hostsForSystemMenu,
|
|
});
|
|
}, 250);
|
|
|
|
return () => {
|
|
cancelled = true;
|
|
clearTimeout(timer);
|
|
};
|
|
}, [enabled, hasRuntimeTunnel, hosts, sessions, portForwardingRules, workspaces]);
|
|
|
|
// Quit guard: block app exit while any editor tab has unsaved changes.
|
|
// Main process sends "app:query-dirty-editors"; we respond with the result.
|
|
useEffect(() => {
|
|
const bridge = netcattyBridge.get();
|
|
if (!bridge?.onCheckDirtyEditors) return;
|
|
const unsub = bridge.onCheckDirtyEditors(async () => {
|
|
// Always report SOMETHING so the main process doesn't time out for
|
|
// 5 s on an unhandled exception. If we can't determine the state,
|
|
// fail open — losing unsaved work is bad, but stranding the user
|
|
// on a slow quit and then quitting anyway after the timeout is
|
|
// exactly the same outcome.
|
|
let hasDirty = false;
|
|
try {
|
|
hasDirty = editorTabStore.getTabs().some((tab) => tab.content !== tab.baselineContent);
|
|
if (hasDirty) toast.warning(t('sftp.editor.quitBlockedByDirty'), 'SFTP');
|
|
if (!hasDirty) {
|
|
const unfinishedTasks = sftpTransferCenterStore.getSnapshot().tasks.filter((task) => (
|
|
!task.parentTaskId && !["completed", "failed", "cancelled"].includes(task.status)
|
|
));
|
|
if (unfinishedTasks.length > 0) {
|
|
await Promise.allSettled(unfinishedTasks.map((task) => sftpTransferCenterStore.pause(task.id)));
|
|
hasDirty = !window.confirm(t('sftp.transferCenter.quitConfirm', { count: unfinishedTasks.length }));
|
|
}
|
|
}
|
|
} catch (err) {
|
|
console.error('[App] dirty-editors check failed:', err);
|
|
}
|
|
try {
|
|
bridge.reportDirtyEditorsResult?.(hasDirty);
|
|
} catch (err) {
|
|
// Reporting itself shouldn't throw, but if the IPC bridge is in a
|
|
// bad state we'd rather log than bubble out of the listener and
|
|
// disable the quit guard for the rest of the session.
|
|
console.error('[App] reportDirtyEditorsResult failed:', err);
|
|
}
|
|
});
|
|
return unsub;
|
|
}, [enabled, t]);
|
|
|
|
useEffect(() => {
|
|
const bridge = netcattyBridge.get();
|
|
const unsubscribeEvents = bridge?.onGlobalSftpTransferEvent?.((event) => {
|
|
sftpTransferCenterStore.ingestBackgroundEvent(event);
|
|
});
|
|
const restartBackgroundTransfer = async (taskId: string, fromBeginning: boolean) => {
|
|
const task = sftpTransferCenterStore.getSnapshot().tasks.find((candidate) => candidate.id === taskId);
|
|
if (!task || !bridge?.openSftpForSession || !bridge.startStreamTransfer) return;
|
|
const sessionId = task.direction === "upload" ? task.targetConnectionId : task.sourceConnectionId;
|
|
if (!sessionId || sessionId === "agent" || sessionId === "local") {
|
|
sftpTransferCenterStore.ingestBackgroundEvent({
|
|
type: "failed",
|
|
transferId: taskId,
|
|
error: "The original server session is unavailable",
|
|
endedAt: Date.now(),
|
|
});
|
|
return;
|
|
}
|
|
let sftpId: string | undefined;
|
|
try {
|
|
const checkpointBytes = fromBeginning ? 0 : (task.checkpointBytes ?? task.transferredBytes ?? 0);
|
|
sftpTransferCenterStore.ingestBackgroundEvent({ type: "queued", transferId: taskId });
|
|
// Admit first so agent resume does not pin session-backed SFTP handles
|
|
// while waiting for main-process concurrency.
|
|
const result = await globalSftpTransferScheduler.run(
|
|
"background-agent",
|
|
task.id,
|
|
getSftpTransferResourceKeys({
|
|
sourceHostId: task.sourceHostId,
|
|
targetHostId: task.targetHostId,
|
|
}),
|
|
() => localStorageAdapter.readNumber(STORAGE_KEY_SFTP_TRANSFER_CONCURRENCY),
|
|
async () => {
|
|
sftpId = await bridge.openSftpForSession!(sessionId);
|
|
return bridge.startStreamTransfer!({
|
|
transferId: task.id,
|
|
sourcePath: task.sourcePath,
|
|
targetPath: task.targetPath,
|
|
sourceType: task.direction === "upload" ? "local" : "sftp",
|
|
targetType: task.direction === "download" ? "local" : "sftp",
|
|
sourceSftpId: task.direction === "download" ? sftpId : undefined,
|
|
targetSftpId: task.direction === "upload" ? sftpId : undefined,
|
|
// Keep host-scoped path gates across session reopen (Codex P1).
|
|
sourceHostId: task.sourceHostId,
|
|
targetHostId: task.targetHostId,
|
|
totalBytes: task.totalBytes,
|
|
resumable: task.resumable !== false,
|
|
checkpointBytes,
|
|
resumeStage: fromBeginning ? undefined : task.resumeStage,
|
|
downloadCheckpointBytes: fromBeginning ? 0 : task.downloadCheckpointBytes,
|
|
uploadCheckpointBytes: fromBeginning ? 0 : task.uploadCheckpointBytes,
|
|
sourceFingerprint: fromBeginning ? undefined : task.sourceFingerprint,
|
|
skipAdmission: true,
|
|
});
|
|
},
|
|
);
|
|
// Same-id retry stole ownership; wait for the live owner's terminal
|
|
// status instead of treating this invoke as completed (Codex P2).
|
|
if (result?.superseded === true) {
|
|
// Wait for live owner terminal status only (no fixed deadline).
|
|
for (;;) {
|
|
const latest = sftpTransferCenterStore.getSnapshot().tasks.find((candidate) => candidate.id === task.id);
|
|
const status = latest?.status;
|
|
if (status === "completed" || status === "cancelled" || status === "failed") {
|
|
if (status === "failed") {
|
|
throw new Error(latest?.error || "Transfer failed");
|
|
}
|
|
if (status === "cancelled") {
|
|
sftpTransferCenterStore.ingestBackgroundEvent({
|
|
type: "cancelled",
|
|
transferId: task.id,
|
|
endedAt: Date.now(),
|
|
});
|
|
}
|
|
// completed: events already applied; cancelled handled above.
|
|
break;
|
|
}
|
|
await new Promise((resolve) => setTimeout(resolve, 200));
|
|
}
|
|
} else if (result?.cancelled || result?.error === "Transfer cancelled") {
|
|
sftpTransferCenterStore.ingestBackgroundEvent({ type: "cancelled", transferId: task.id, endedAt: Date.now() });
|
|
} else if (result?.error) {
|
|
throw new Error(result.error);
|
|
} else {
|
|
sftpTransferCenterStore.ingestBackgroundEvent({ type: "completed", transferId: task.id, endedAt: Date.now() });
|
|
}
|
|
} catch (error) {
|
|
sftpTransferCenterStore.ingestBackgroundEvent({
|
|
type: "failed",
|
|
transferId: task.id,
|
|
error: error instanceof Error ? error.message : String(error),
|
|
endedAt: Date.now(),
|
|
});
|
|
} finally {
|
|
if (sftpId) await bridge.closeSftp?.(sftpId).catch(() => {});
|
|
}
|
|
};
|
|
const unregisterOwner = sftpTransferCenterStore.registerOwner("background-agent", {
|
|
pause: async (taskId) => {
|
|
const result = await bridge?.pauseTransfer?.(taskId);
|
|
if (result?.success) sftpTransferCenterStore.ingestBackgroundEvent({
|
|
type: "paused",
|
|
transferId: taskId,
|
|
checkpointBytes: result.checkpointBytes,
|
|
resumeStage: result.resumeStage,
|
|
downloadCheckpointBytes: result.downloadCheckpointBytes,
|
|
uploadCheckpointBytes: result.uploadCheckpointBytes,
|
|
sourceFingerprint: result.sourceFingerprint,
|
|
});
|
|
},
|
|
resume: async (taskId) => {
|
|
const result = await bridge?.resumeTransfer?.(taskId);
|
|
if (result?.success) {
|
|
sftpTransferCenterStore.ingestBackgroundEvent({ type: "resumed", transferId: taskId });
|
|
} else {
|
|
sftpTransferCenterStore.markReconnectRequired(
|
|
taskId,
|
|
result?.reason ?? "The original server connection is unavailable",
|
|
);
|
|
setTimeout(() => { void sftpTransferCenterStore.resume(taskId); }, 0);
|
|
}
|
|
},
|
|
cancel: async (taskId) => {
|
|
await bridge?.cancelTransfer?.(taskId);
|
|
sftpTransferCenterStore.ingestBackgroundEvent({ type: "cancelled", transferId: taskId, endedAt: Date.now() });
|
|
},
|
|
retry: async (taskId) => { await restartBackgroundTransfer(taskId, true); },
|
|
prioritize: async (taskId) => { await bridge?.prioritizeTransfer?.(taskId); },
|
|
dismiss: (taskId, prunedTask) => {
|
|
const task = prunedTask
|
|
?? sftpTransferCenterStore.getSnapshot().tasks.find((candidate) => candidate.id === taskId);
|
|
if (!task) return;
|
|
void bridge?.cleanupTransferArtifacts?.({
|
|
transferId: task.id,
|
|
sourcePath: task.sourcePath,
|
|
targetPath: task.targetPath,
|
|
stagedTargetPath: task.stagedTargetPath,
|
|
});
|
|
},
|
|
});
|
|
return () => {
|
|
unsubscribeEvents?.();
|
|
unregisterOwner();
|
|
};
|
|
}, [enabled]);
|
|
|
|
// Keyboard-interactive authentication (2FA/MFA) event listener
|
|
useEffect(() => {
|
|
const bridge = netcattyBridge.get();
|
|
if (!bridge?.onKeyboardInteractive) return;
|
|
|
|
const unsubscribe = bridge.onKeyboardInteractive((request) => {
|
|
if (!shouldQueueKeyboardInteractiveRequest(request, sessionsRef.current)) return;
|
|
console.log('[App] Keyboard-interactive request received:', request);
|
|
// Add to queue instead of replacing - supports multiple concurrent sessions
|
|
setKeyboardInteractiveQueue(prev => [...prev, {
|
|
requestId: request.requestId,
|
|
sessionId: request.sessionId,
|
|
hostId: request.hostId,
|
|
name: request.name,
|
|
instructions: request.instructions,
|
|
prompts: request.prompts,
|
|
hostname: request.hostname,
|
|
savedPassword: request.savedPassword,
|
|
allowSavePassword: request.allowSavePassword !== false,
|
|
}]);
|
|
});
|
|
const unsubscribeCancelled = bridge.onKeyboardInteractiveCancelled?.((event) => {
|
|
setKeyboardInteractiveQueue(prev => removeKeyboardInteractiveRequest(prev, event.requestId));
|
|
});
|
|
|
|
return () => {
|
|
unsubscribe?.();
|
|
unsubscribeCancelled?.();
|
|
};
|
|
}, [enabled, setKeyboardInteractiveQueue]);
|
|
|
|
|
|
}
|