Compare commits

...
Author SHA1 Message Date
陈大猫 d6953bcf55 fix(sftp): retain folder cleanup warnings before child completion 2026-10-24 13:04:36 +08:00
陈大猫 5e833fcd63 fix(sftp): preserve successful uploads with backup cleanup warnings
Warn only on confirmed backup cleanup permission refusal after final publication. Preserve actionable warnings across transfer events, history restoration and completion ordering without masking deletion or commit failures.

Narrow follow-up to #3830; related #3879.
2026-10-24 12:34:09 +08:00
41 changed files with 833 additions and 40 deletions

No files matched your search

+1
View File
@@ -370,6 +370,7 @@ export const enVaultMessages: Messages = {
'sftp.transfers.openTargetFolder': 'Open target folder',
'sftp.transfers.openTargetFolderError': 'Could not open target folder',
'sftp.transfers.copyTargetPath': 'Copy target path',
'sftp.transfers.cleanupWarnings': 'Backup cleanup warnings: {count}. Show files for details.',
'sftp.transfers.copyTargetPathSuccess': 'Target path copied',
'sftp.transfers.copyTargetPathError': 'Could not copy target path',
'sftp.transfers.pathConflict.title': 'Transfer blocked',
+1
View File
@@ -370,6 +370,7 @@ export const esVaultMessages: Messages = {
'sftp.transfers.openTargetFolder': 'Abrir carpeta de destino',
'sftp.transfers.openTargetFolderError': 'No se pudo abrir la carpeta de destino',
'sftp.transfers.copyTargetPath': 'Copiar ruta de destino',
'sftp.transfers.cleanupWarnings': 'Avisos de limpieza de copias de seguridad: {count}. Muestra los archivos para ver los detalles.',
'sftp.transfers.copyTargetPathSuccess': 'Ruta de destino copiada',
'sftp.transfers.copyTargetPathError': 'No se pudo copiar la ruta de destino',
'sftp.transfers.pathConflict.title': 'Transferencia bloqueada',
+1
View File
@@ -413,6 +413,7 @@ export const ruVaultMessages: Messages = {
'sftp.transfers.openTargetFolder': 'Открыть целевую папку',
'sftp.transfers.openTargetFolderError': 'Не удалось открыть целевую папку',
'sftp.transfers.copyTargetPath': 'Копировать целевой путь',
'sftp.transfers.cleanupWarnings': 'Предупреждения об удалении резервных копий: {count}. Откройте список файлов для подробностей.',
'sftp.transfers.copyTargetPathSuccess': 'Целевой путь скопирован',
'sftp.transfers.copyTargetPathError': 'Не удалось скопировать целевой путь',
'sftp.transfers.pathConflict.title': 'Передача заблокирована',
@@ -0,0 +1,13 @@
import assert from "node:assert/strict";
import test from "node:test";
import en from "./en";
import es from "./es";
import ru from "./ru";
import zhCN from "./zh-CN";
import zhTW from "./zh-TW";
test("folder cleanup-warning counts are localized in every supported locale", () => {
for (const [locale, messages] of Object.entries({ en, es, ru, zhCN, zhTW })) {
assert.match(messages["sftp.transfers.cleanupWarnings"] ?? "", /\{count\}/, locale);
}
});
+1
View File
@@ -1035,6 +1035,7 @@ Vaults 标签保持扁平,不使用这块背景。
'sftp.transfers.openTargetFolder': '打开目标目录',
'sftp.transfers.openTargetFolderError': '无法打开目标目录',
'sftp.transfers.copyTargetPath': '复制目标路径',
'sftp.transfers.cleanupWarnings': '备份清理警告:{count}。展开文件列表查看详情。',
'sftp.transfers.copyTargetPathSuccess': '已复制目标路径',
'sftp.transfers.copyTargetPathError': '无法复制目标路径',
'sftp.transfers.pathConflict.title': '无法开始传输',
+1
View File
@@ -1037,6 +1037,7 @@ Vaults 分頁維持扁平,不使用這塊背景。
'sftp.transfers.openTargetFolder': '開啟目標目錄',
'sftp.transfers.openTargetFolderError': '無法開啟目標目錄',
'sftp.transfers.copyTargetPath': '複製目標路徑',
'sftp.transfers.cleanupWarnings': '備份清理警告:{count}。展開檔案清單查看詳情。',
'sftp.transfers.copyTargetPathSuccess': '已複製目標路徑',
'sftp.transfers.copyTargetPathError': '無法複製目標路徑',
'sftp.transfers.pathConflict.title': '無法開始傳輸',
@@ -163,6 +163,9 @@ export async function restoreSftpTransferHistoryCooperatively(
const child = parentChildren[childIndex];
if (
child.status === "completed"
// Match canonical pruning: warning rows carry recovery paths that
// cannot be represented by the directory's compact manifest.
&& !child.warning
&& Number.isSafeInteger(child.directoryEntryIndex)
&& (child.directoryEntryIndex ?? checkpoint.coveredEntries) < checkpoint.coveredEntries
&& /^[a-f0-9]{64}$/.test(child.directoryEntryIdentity ?? "")
@@ -135,6 +135,34 @@ test("completed dispatch consumes only exact identity and never restarts", async
assert.equal(store.getTask(task.id)?.status, "completed");
});
test("successful invoke warnings reach the child before local completion can compact it", async (t) => {
const { sftpTransferCenterStore: store } = await import("../sftpTransferCenterStore");
const { runTransferAndWaitForOwner } = await import("./waitForTransferOwner");
const task = { ...child(), id: "warning-result-child", parentTaskId: "warning-result-root" };
const root = { ...task, id: task.parentTaskId, parentTaskId: undefined, isDirectory: true };
const warning = "Upload completed; remove backup /target/.netcatty-backup-a manually.";
store.upsertTasks([root, task]);
t.after(() => store.dismiss(root.id));
const result = await runTransferAndWaitForOwner(task, async () => ({ warning }), () => false);
assert.equal(result?.warning, warning);
assert.equal(store.getTask(task.id)?.warning, warning, "capture metadata before the caller finishes the child");
store.patchTask(task.id, { status: "completed", transferredBytes: 1 });
assert.equal(store.getTask(task.id)?.status, "completed", "warning-bearing children stay available for inspection");
assert.equal(store.getTask(task.id)?.warning, warning);
});
for (const outcome of [{ error: "write failed" }, { cancelled: true }]) {
test(`unsuccessful invoke results do not add cleanup warnings (${JSON.stringify(outcome)})`, async (t) => {
const { sftpTransferCenterStore: store } = await import("../sftpTransferCenterStore");
const { runTransferAndWaitForOwner } = await import("./waitForTransferOwner");
const task = { ...child(), id: `unsuccessful-warning-${"error" in outcome ? "failed" : "cancelled"}`, parentTaskId: undefined };
store.upsertTasks([task]);
t.after(() => store.dismiss(task.id));
await runTransferAndWaitForOwner(task, async () => ({ ...outcome, warning: "not a committed upload" }), () => false);
assert.equal(store.getTask(task.id)?.warning, undefined);
});
}
test("a resumed retry does not inherit failed settlement captured while admission was paused", async (t) => {
const { sftpTransferCenterStore: store } = await import("../sftpTransferCenterStore");
const { runTransferAndWaitForOwner } = await import("./waitForTransferOwner");
+1
View File
@@ -65,6 +65,7 @@ export interface FileWatchSyncedEvent {
localPath: string;
remotePath: string;
bytesWritten: number;
warning?: string;
}
export interface FileWatchErrorEvent {
@@ -156,6 +156,10 @@ export const createUploadTaskCallbacks = ({
phase: phase === "compressed" ? "transferring" : phase as TransferTask["phase"],
});
},
onTaskWarning: (taskId: string, warning: string) => {
// Preserve cleanup details before completion can compact a child row.
store.patchTask(taskId, { warning });
},
onTaskCompleted: (taskId: string, totalBytes: number) => {
store.patchTask(taskId, {
status: "completed" as TransferStatus,
+12 -2
View File
@@ -6,7 +6,7 @@ export class TransferOwnerChangedError extends Error {}
export type TransferOwnerObservation = ReturnType<typeof sftpTransferCenterStore.observeTaskSettlement>;
type StreamResult = { error?: string; cancelled?: boolean; superseded?: boolean } | undefined;
type StreamResult = { error?: string; cancelled?: boolean; superseded?: boolean; warning?: string } | undefined;
/** A live waiter owns bounded settlement evidence; persisted history stays compact. */
export async function runTransferAndWaitForOwner(
@@ -37,7 +37,17 @@ export async function runTransferAndWaitForOwner(
observation = sftpTransferCenterStore.observeTaskSettlement(task, undefined, onOwnerChanged);
const result = await start();
if (observation.hasIdentityConflict()) throw new TransferOwnerChangedError("Transfer identity changed while waiting for its owner");
if (!result?.superseded) return result;
if (!result?.superseded) {
// The invoke response may arrive before completed IPC. Preserve cleanup
// details before the caller completes (and may compact) this child.
if (result?.warning && !result.error && !result.cancelled) {
const latest = sftpTransferCenterStore.getTask(task.id);
if (latest && latest.status !== "failed" && latest.status !== "cancelled") {
sftpTransferCenterStore.patchTask(task.id, { warning: result.warning });
}
}
return result;
}
for (;;) {
if (shouldAbort()) throw new Error("Transfer cancelled");
if (observation.hasIdentityConflict()) throw new TransferOwnerChangedError("Transfer identity changed while waiting for its owner");
@@ -33,6 +33,85 @@ test("store aggregates owner snapshots without duplicating tasks", () => {
]);
});
test("completion cleanup warnings survive owner snapshots and restored history", () => {
let persisted: string | null = null;
const store = createSftpTransferCenterStore({ read: () => persisted, write: (value) => { persisted = value; } });
const source = makeTask("cleanup-warning");
const warning = "Upload completed; remove backup /target/.netcatty-backup-file manually.";
store.publishOwner("panel", [source]);
store.ingestBackgroundEvent({ type: "completed", transferId: source.id, transferred: 10, warning });
assert.equal(store.getTask(source.id)?.status, "completed");
assert.equal(store.getTask(source.id)?.warning, warning);
assert.equal(store.getTask(source.id)?.reconnectRequired, false);
store.publishOwner("panel", [{ ...source, status: "completed", transferredBytes: 10, endTime: Date.now() }]);
assert.equal(store.getTask(source.id)?.warning, warning, "a stale renderer completion cannot erase cleanup details");
const restored = createSftpTransferCenterStore({ read: () => persisted, write: () => {} });
assert.equal(restored.getTask(source.id)?.warning, warning);
assert.equal(restored.getTask(source.id)?.status, "completed");
assert.equal(restored.getTask(source.id)?.error, undefined);
});
test("invoke cleanup warnings survive stale live and completion snapshots until an explicit restart", () => {
const store = createSftpTransferCenterStore();
const source = makeTask("invoke-warning-snapshot");
const warning = "Upload completed; remove backup /target/.netcatty-backup-file manually.";
store.publishOwner("panel", [source]);
store.patchTask(source.id, { warning });
store.publishOwner("panel", [{ ...source, transferredBytes: 9 }]);
assert.equal(store.getTask(source.id)?.warning, warning);
store.publishOwner("panel", [{ ...source, status: "completed", transferredBytes: 10, endTime: Date.now() }]);
assert.equal(store.getTask(source.id)?.warning, warning);
store.publishOwner("panel", [{ ...source, status: "queued", transferredBytes: 0, checkpointBytes: 0 }]);
assert.equal(store.getTask(source.id)?.warning, undefined, "new attempts do not inherit cleanup warnings");
});
test("a clean completed event cannot erase a warning already returned by the invoke", () => {
const store = createSftpTransferCenterStore();
const source = makeTask("invoke-warning-event");
const warning = "Upload completed; remove backup /target/.netcatty-backup-file manually.";
store.publishOwner("panel", [source]);
store.patchTask(source.id, { warning });
store.ingestBackgroundEvent({ type: "completed", transferId: source.id, transferred: 10 });
assert.equal(store.getTask(source.id)?.status, "completed");
assert.equal(store.getTask(source.id)?.warning, warning);
});
test("late completion enriches a locally completed task with its cleanup warning", () => {
let persisted: string | null = null;
const store = createSftpTransferCenterStore({ read: () => persisted, write: (value) => { persisted = value; } });
const source = { ...makeTask("result-first-warning"), phase: "transferring" as const };
const warning = "Upload completed; remove backup /target/.netcatty-backup-file manually.";
const endTime = Date.now();
store.publishOwner("panel", [source]);
// The invoke response can settle the renderer before the completed IPC arrives.
store.patchTask(source.id, { status: "completed", transferredBytes: 10, endTime });
let notifications = 0;
const unsubscribe = store.subscribe(() => { notifications += 1; });
store.ingestBackgroundEvent({ type: "completed", transferId: source.id, transferred: 10, endedAt: endTime + 1, warning });
assert.equal(store.getTask(source.id)?.warning, warning);
assert.equal(store.getTask(source.id)?.status, "completed");
assert.equal(store.getTask(source.id)?.endTime, endTime, "late metadata does not replace the terminal outcome");
assert.equal(notifications, 1, "the warning is visible without another transfer triggering a render");
store.ingestBackgroundEvent({ type: "completed", transferId: source.id, transferred: 10 });
assert.equal(store.getTask(source.id)?.warning, warning, "a duplicate clean event cannot erase the warning");
const restored = createSftpTransferCenterStore({ read: () => persisted, write: () => {} });
assert.equal(restored.getTask(source.id)?.warning, warning);
unsubscribe();
});
for (const status of ["failed", "cancelled"] as const) {
test(`late cleanup warnings cannot replace a ${status} terminal outcome`, () => {
const store = createSftpTransferCenterStore();
const endTime = Date.now();
const task = { ...makeTask(`late-warning-${status}`, status), error: "original outcome", endTime };
store.publishOwner("panel", [task]);
store.ingestBackgroundEvent({ type: "completed", transferId: task.id, warning: "cleanup warning" });
assert.equal(store.getTask(task.id)?.status, status);
assert.equal(store.getTask(task.id)?.error, "original outcome");
assert.equal(store.getTask(task.id)?.warning, undefined);
});
}
test("store routes cancel/retry/prioritize/dismiss to the task owner; pause/resume are process-global", async () => {
const calls: string[] = [];
const store = createSftpTransferCenterStore();
@@ -3576,6 +3655,46 @@ test("legacy 50,000-file history restores cooperatively and converges to one per
assert.ok(persisted.length < 10_000, "migrated localStorage payload must stay bounded");
});
for (const indexed of [false, true]) {
test(`cooperative ${indexed ? "indexed" : "legacy"} restore retains completed child cleanup warnings`, async () => {
const now = Date.now();
const warning = "Upload completed; remove backup /target/.netcatty-backup-file manually.";
const parent: TransferTask = { ...makeTask("restore-warning-folder", "paused"), isDirectory: true };
const children: TransferTask[] = [0, 1].map((index) => ({
...makeTask(`restore-warning-child-${index}`, "completed"),
parentTaskId: parent.id, endTime: now,
warning: index === 0 ? warning : undefined,
...(indexed ? { directoryEntryIndex: index, directoryEntryIdentity: String(index).padStart(64, "0") } : {}),
}));
let persisted = JSON.stringify({ version: 1, padding: "x".repeat(600_000), tasks: [parent, ...children] });
const store = createSftpTransferCenterStore({ read: () => persisted, write: (value) => { persisted = value; } });
assert.equal(store.getSnapshot().tasks.length, 0, "large history must take cooperative restoration");
await new Promise<void>((resolve, reject) => {
let unsubscribe = () => {};
const timeout = setTimeout(() => {
unsubscribe();
reject(new Error("timed out waiting for cleanup-warning history migration"));
}, 10_000);
const check = () => {
if (!store.getTask(parent.id)) return;
clearTimeout(timeout);
unsubscribe();
resolve();
};
unsubscribe = store.subscribe(check);
check();
});
assert.equal(store.getTask("restore-warning-child-0")?.warning, warning);
assert.equal(store.getTask("restore-warning-child-0")?.status, "completed");
assert.equal(store.getTask("restore-warning-child-1"), undefined, "clean completed children still compact");
assert.equal(store.getTask(parent.id)?.directoryResumeCheckpoint?.coveredEntries, 2);
assert.equal(store.getTask(parent.id)?.directoryResumeCheckpoint?.completedEntries, 1);
const restarted = createSftpTransferCenterStore({ read: () => persisted, write: () => {} });
assert.equal(restarted.getTask("restore-warning-child-0")?.warning, warning);
assert.equal(restarted.getTask(parent.id)?.directoryResumeCheckpoint?.completedEntries, 1);
});
}
test("live mutations wait for cooperative restore and merge without erasing old rows", async () => {
const legacyOnly = makeTask("legacy-only", "paused");
const staleCollision = makeTask("collision", "paused");
@@ -152,6 +152,7 @@ export interface SftpTransferCenterStore {
startedAt?: number;
endedAt?: number;
error?: string;
warning?: string;
transferred?: number;
totalBytes?: number;
speed?: number;
@@ -289,6 +290,12 @@ export function mergeOwnerPublishedTask(
updatedAt: incoming.updatedAt ?? Date.now(),
};
// A panel snapshot may predate cleanup metadata returned by the invoke or
// completed IPC. Keep it through this attempt, but not a restart or failure.
if (!incomingReset && incoming.status !== "failed" && incoming.status !== "cancelled") {
merged.warning = incoming.warning ?? existing.warning;
}
if (incomingReset || incomingTerminal) {
return merged;
}
@@ -2274,6 +2281,15 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S
if (terminal && event.type !== "cancelled") {
// Allow a late explicit cancel to stick; ignore everything else.
if (event.type === "completed" || event.type === "failed") {
// An invoke response can complete the renderer row before its IPC
// event arrives. Enrich that same success with cleanup metadata,
// without replacing any terminal outcome or reviving stopped work.
if (existing.status === "completed" && event.type === "completed"
&& event.warning && event.warning !== existing.warning) {
tasks = tasks.map((task) => task.id === existing.id ? { ...task, warning: event.warning } : task);
emit(true);
return;
}
// Keep existing terminal state (prefer cancelled over late completed).
publishRepairedHierarchy();
return;
@@ -2561,6 +2577,7 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S
? task.checkpointBytes
: Math.max(task.checkpointBytes ?? 0, Number(event.checkpointBytes) || 0),
error: event.error,
warning: event.type === "completed" ? event.warning ?? task.warning : undefined,
endTime: event.endedAt ?? Date.now(),
speed: 0,
reconnectRequired: event.type === "failed" ? task.reconnectRequired : false,
@@ -0,0 +1,59 @@
import assert from "node:assert/strict";
import test from "node:test";
import React from "react";
import { createDomRenderer, dispatchDomEvent, flushEffects, installDomEnvironment } from "./test-support/renderReactDom";
test("global completed rows and expanded folder children show cleanup paths without a failed status", async (t) => {
const env = installDomEnvironment();
const previous = new Map<string, PropertyDescriptor | undefined>();
for (const [key, value] of Object.entries({
MutationObserver: env.window.MutationObserver, NodeFilter: env.window.NodeFilter,
HTMLInputElement: env.window.HTMLInputElement, Element: env.window.Element,
requestAnimationFrame: (callback: FrameRequestCallback) => setTimeout(() => callback(Date.now()), 0),
cancelAnimationFrame: clearTimeout,
})) {
previous.set(key, Object.getOwnPropertyDescriptor(globalThis, key));
Object.defineProperty(globalThis, key, { configurable: true, writable: true, value });
}
const { I18nProvider } = await import("../application/i18n/I18nProvider");
const { sftpTransferCenterStore: store } = await import("../application/state/sftpTransferCenterStore");
const { GlobalSftpTransferCenter } = await import("./GlobalSftpTransferCenter");
const { TooltipProvider } = await import("./ui/tooltip");
const renderer = await createDomRenderer(env.document);
t.after(async () => {
await renderer.unmount();
for (const [key, descriptor] of previous) {
if (descriptor) Object.defineProperty(globalThis, key, descriptor);
else Reflect.deleteProperty(globalThis, key);
}
env.cleanup();
});
const warning = "Upload completed; remove backup /remote/.netcatty-backup-file.bin manually.";
const base = {
fileName: "file.bin", sourcePath: "/local/file.bin", targetPath: "/remote/file.bin",
sourceConnectionId: "local", targetConnectionId: "remote", direction: "upload" as const,
status: "completed" as const, totalBytes: 10, transferredBytes: 10, speed: 0,
startTime: Date.now(), endTime: Date.now(), isDirectory: false,
};
store.publishOwner("cleanup-ui", [
{ ...base, id: "warning-file", warning },
{ ...base, id: "warning-folder", fileName: "folder", isDirectory: true },
{ ...base, id: "warning-child", parentTaskId: "warning-folder", warning },
]);
await renderer.render(<I18nProvider locale="en"><TooltipProvider><GlobalSftpTransferCenter /></TooltipProvider></I18nProvider>);
const click = async (selector: string) => {
const node = env.document.querySelector(selector);
assert.ok(node, selector);
await dispatchDomEvent(node, new env.window.MouseEvent("click", { bubbles: true }));
await flushEffects();
};
await click("[data-section=global-sftp-transfer-toggle]");
assert.match(env.document.body.textContent ?? "", /Backup cleanup warnings: 1/);
await click('button[aria-label="Show files"]');
const notices = env.document.querySelectorAll('[data-section="sftp-transfer-warning"]');
assert.equal(notices.length, 2);
for (const notice of notices) assert.equal(notice.textContent, warning);
assert.equal(env.document.querySelector('[data-transfer-status="failed"]'), null);
assert.equal(store.getTask("warning-child")?.status, "completed");
await click("[data-section=global-sftp-transfer-toggle]");
});
+11 -1
View File
@@ -37,6 +37,7 @@ import { cn } from "../lib/utils";
import { Button } from "./ui/button";
import { Popover, PopoverContent, PopoverTrigger } from "./ui/popover";
import { Tooltip, TooltipContent, TooltipTrigger } from "./ui/tooltip";
import { SftpTransferWarning } from "./sftp/SftpTransferWarning";
/** Stable empty snapshot while the popover is closed — progress ticks must not
* re-render TopTabs just to update hidden list rows. */
@@ -350,7 +351,9 @@ const TransferChildRow = React.memo(function TransferChildRow({ task: snapshotTa
return (
<div className="flex h-6 items-center gap-2 rounded px-1 text-[10px] hover:bg-muted/40" data-transfer-status={task.status}>
<TruncatedTextWithTooltip text={task.fileName} tooltip={task.targetPath || task.fileName} className="flex-1 text-muted-foreground" />
<span className="shrink-0 font-mono text-muted-foreground">{detail}</span>
{task.status === "completed" && task.warning
? <div className="min-w-0 flex-1"><SftpTransferWarning warning={task.warning} compact /></div>
: <span className="shrink-0 font-mono text-muted-foreground">{detail}</span>}
{task.status === "transferring" && <Loader2 size={10} className="shrink-0 animate-spin text-primary" />}
</div>
);
@@ -658,6 +661,13 @@ function TransferRow({
</span>
</div>
{task.status === "completed" && task.warning && <div className="mt-1"><SftpTransferWarning warning={task.warning} /></div>}
{childTasks.some((child) => child.status === "completed" && !!child.warning) && (
<p className="mt-1 text-[10px] text-amber-600 dark:text-amber-400">
{t("sftp.transfers.cleanupWarnings", { count: childTasks.filter((child) => child.status === "completed" && !!child.warning).length })}
</p>
)}
{/* Collapsed: show currently transferring child file(s) without expanding the whole tree. */}
{isDirParent && !expanded && displayChildren.length > 0 && (
<div
+3 -2
View File
@@ -265,9 +265,10 @@ const SftpSidePanelInner: React.FC<SftpSidePanelProps> = ({
}, [activeHost, activeSessionId, resolveTransferSourceSessionId, sessions]);
const fileWatchHandlers = useMemo(() => ({
onFileWatchSynced: (payload: { remotePath: string }) => {
onFileWatchSynced: (payload: { remotePath: string; warning?: string }) => {
const fileName = payload.remotePath.split('/').pop() || payload.remotePath;
toast.success(t('sftp.autoSync.success', { fileName }));
if (payload.warning) toast.warning(payload.warning);
else toast.success(t('sftp.autoSync.success', { fileName }));
logger.info("[SFTP] File auto-synced to remote", payload);
},
onFileWatchError: (payload: { error: string }) => {
+21
View File
@@ -52,6 +52,27 @@ test("renders failed transfer actions with custom tooltips and readable labels",
assert.match(markup, /focus-visible:ring-1/);
});
for (const isChild of [false, true]) {
test(`completed ${isChild ? "child" : "file"} shows the cleanup path without a retry action`, () => {
const warning = "Upload completed; remove backup /remote/.netcatty-backup-archive.tar.gz manually.";
const markup = renderTransferItem({
...baseTask, status: "completed", error: undefined, warning, transferredBytes: 1024,
}, { isChild });
assert.match(markup, /data-transfer-status="completed"/);
assert.match(markup, /data-section="sftp-transfer-warning"/);
assert.ok(markup.includes(warning));
assert.doesNotMatch(markup, /aria-label="Retry:/);
assert.doesNotMatch(markup, /text-destructive/);
});
}
test("folder rows disclose retained child cleanup warnings", () => {
const markup = renderTransferItem({ ...baseTask, status: "completed", error: undefined, isDirectory: true }, {
childWarningCount: 2, canToggleChildren: true, onToggleChildren: () => {},
});
assert.match(markup, /Backup cleanup warnings: 2/);
});
test("renders active transfer cancel action with an item-specific label", () => {
const markup = renderTransferItem({
...baseTask,
+3 -2
View File
@@ -119,9 +119,10 @@ const SftpViewInner: React.FC<SftpViewProps> = ({
// File watch event handlers (stable refs to avoid re-creating the useSftpState options)
const fileWatchHandlers = useMemo(() => ({
onFileWatchSynced: (payload: { remotePath: string }) => {
onFileWatchSynced: (payload: { remotePath: string; warning?: string }) => {
const fileName = payload.remotePath.split('/').pop() || payload.remotePath;
toast.success(t('sftp.autoSync.success', { fileName }));
if (payload.warning) toast.warning(payload.warning);
else toast.success(t('sftp.autoSync.success', { fileName }));
logger.info("[SFTP] File auto-synced to remote", payload);
},
onFileWatchError: (payload: { error: string }) => {
+12 -2
View File
@@ -33,6 +33,7 @@ import {
import { Button } from '../ui/button';
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '../ui/tooltip';
import { formatSpeed, formatTransferBytes } from './utils';
import { SftpTransferWarning } from './SftpTransferWarning';
/** Child rows need room for Pause + Cancel (2×24px icons + gap). */
const CHILD_ACTIONS_COLUMN_PX = 56;
@@ -54,6 +55,7 @@ interface SftpTransferItemProps {
canToggleChildren?: boolean;
isExpanded?: boolean;
visibleChildCount?: number;
childWarningCount?: number;
onToggleChildren?: () => void;
onSetNameColumnWidth?: (width: number) => void;
childNameColumnMinWidth?: number;
@@ -124,6 +126,7 @@ const SftpTransferItemInner: React.FC<SftpTransferItemProps> = ({
canToggleChildren = false,
isExpanded = false,
visibleChildCount: _visibleChildCount = 0,
childWarningCount = 0,
onToggleChildren,
onSetNameColumnWidth,
childNameColumnMinWidth = 160,
@@ -288,7 +291,8 @@ const SftpTransferItemInner: React.FC<SftpTransferItemProps> = ({
// pause button never looks dead when the backend refuses.
const showPauseUnavailable = !!task.pauseUnavailableReason
&& (task.status === 'transferring' || task.status === 'queued' || task.status === 'pending');
const hasFooterContent = showTransferSizeCalculation || showFailedError || showPauseUnavailable;
const showWarning = task.status === 'completed' && !!task.warning;
const hasFooterContent = showTransferSizeCalculation || showFailedError || showPauseUnavailable || showWarning || childWarningCount > 0;
const retryActionLabel = t('sftp.transfers.retryAction');
const cancelActionLabel = t('common.cancel');
const pauseActionLabel = t('sftp.transferCenter.pause');
@@ -480,7 +484,7 @@ const SftpTransferItemInner: React.FC<SftpTransferItemProps> = ({
<TooltipContent side="top">{resizeNameColumnLabel}</TooltipContent>
</Tooltip>
<div className="min-w-0 overflow-hidden">
{childProgressBar}
{showWarning ? <SftpTransferWarning warning={task.warning} compact /> : childProgressBar}
</div>
<div className="flex h-full min-w-0 items-center justify-end gap-0.5 pl-1">
{actionButtons}
@@ -609,6 +613,10 @@ const SftpTransferItemInner: React.FC<SftpTransferItemProps> = ({
{showPauseUnavailable && (
<span className="text-amber-600 dark:text-amber-400">{task.pauseUnavailableReason}</span>
)}
{showWarning && <SftpTransferWarning warning={task.warning} />}
{childWarningCount > 0 && (
<span className="text-amber-600 dark:text-amber-400">{t('sftp.transfers.cleanupWarnings', { count: childWarningCount })}</span>
)}
</div>
)}
</div>
@@ -631,6 +639,7 @@ const arePropsEqual = (
if (prev.status !== next.status) return false;
if (prev.error !== next.error) return false;
if (prev.warning !== next.warning) return false;
if (prev.pauseUnavailableReason !== next.pauseUnavailableReason) return false;
if (prev.reconnectRequired !== next.reconnectRequired) return false;
if (prev.resumable !== next.resumable) return false;
@@ -647,6 +656,7 @@ const arePropsEqual = (
if ((prevProps.canToggleChildren ?? false) !== (nextProps.canToggleChildren ?? false)) return false;
if ((prevProps.isExpanded ?? false) !== (nextProps.isExpanded ?? false)) return false;
if ((prevProps.visibleChildCount ?? 0) !== (nextProps.visibleChildCount ?? 0)) return false;
if ((prevProps.childWarningCount ?? 0) !== (nextProps.childWarningCount ?? 0)) return false;
if ((prevProps.childNameColumnMinWidth ?? 160) !== (nextProps.childNameColumnMinWidth ?? 160)) return false;
if ((prevProps.childNameColumnMaxWidth ?? 480) !== (nextProps.childNameColumnMaxWidth ?? 480)) return false;
if ((prevProps.childListId ?? '') !== (nextProps.childListId ?? '')) return false;
+1
View File
@@ -423,6 +423,7 @@ export const SftpTransferQueue: React.FC<SftpTransferQueueProps> = ({
canToggleChildren={childTasks.length > 0}
isExpanded={isExpanded}
visibleChildCount={childTasks.length}
childWarningCount={childTasks.filter((child) => child.status === 'completed' && !!child.warning).length}
childListId={childListId}
onToggleChildren={() => toggleExpanded(task.id)}
onCancel={() => {
+21
View File
@@ -0,0 +1,21 @@
import { AlertTriangle } from "lucide-react";
import React from "react";
import { cn } from "../../lib/utils";
import { Tooltip, TooltipContent, TooltipTrigger } from "../ui/tooltip";
/** Cleanup warnings are not transfer errors: retain the completed lifecycle. */
export function SftpTransferWarning({ warning, compact = false }: { warning?: string; compact?: boolean }) {
if (!warning) return null;
const content = (
<span className={cn("flex min-w-0 items-start gap-1 text-[10px] text-amber-600 dark:text-amber-400", !compact && "select-text")} data-section="sftp-transfer-warning">
<AlertTriangle size={12} className="mt-0.5 shrink-0" aria-hidden="true" />
<span className={compact ? "truncate" : "break-all"}>{warning}</span>
</span>
);
return compact ? (
<Tooltip>
<TooltipTrigger asChild><span className="block min-w-0" tabIndex={0}>{content}</span></TooltipTrigger>
<TooltipContent className="max-w-md select-text break-all">{warning}</TooltipContent>
</Tooltip>
) : content;
}
@@ -58,6 +58,21 @@ test("toasts multi-file success count", () => {
assert.deepEqual(calls, [{ type: "success", message: "sftp.uploadFiles: 2" }]);
});
test("successful uploads surface cleanup warnings rather than upload failures", () => {
const calls: Array<{ type: string; message: string }> = [];
const warning = "Upload completed; remove backup /remote/.netcatty-backup-a manually.";
reportSftpUploadResults({
results: [{ fileName: "a", success: true, warning }], t,
toast: {
success: (message) => calls.push({ type: "success", message }),
error: (message) => calls.push({ type: "error", message }),
info: (message) => calls.push({ type: "info", message }),
warning: (message) => calls.push({ type: "warning", message }),
},
});
assert.deepEqual(calls, [{ type: "warning", message: warning }, { type: "success", message: "sftp.upload: a" }]);
});
test("terminal drop success names the exact remote destination", () => {
const calls: Array<{ type: string; message: string }> = [];
reportSftpUploadResults({
@@ -7,6 +7,7 @@ export type ReportSftpUploadResultsOptions = {
success: (message: string, title?: string) => void;
error: (message: string, title?: string) => void;
info: (message: string, title?: string) => void;
warning?: (message: string, title?: string) => void;
};
/** Optional single-item success message override (e.g. folder name). */
successMessage?: string;
@@ -28,6 +29,11 @@ export function reportSftpUploadResults({
const withTargetPath = (message: string): string => (
targetPath ? `${message} (${targetPath})` : message
);
for (const result of results) {
if (result.success && result.warning) {
(toast.warning ?? toast.info)(result.warning, "SFTP");
}
}
if (results.some((result) => result.cancelled)) {
toast.info(t("sftp.upload.cancelled"), "SFTP");
return;
+2
View File
@@ -80,6 +80,8 @@ export interface TransferTask {
speed: number; // bytes per second
error?: string;
startTime: number;
/** Upload completed, but a generated backup needs manual cleanup. */
warning?: string;
endTime?: number;
isDirectory: boolean;
progressMode?: 'bytes' | 'files';
+21
View File
@@ -294,6 +294,27 @@ test("prune upgrades legacy completed children into the parent checkpoint", () =
assert.equal(pruned.find((item) => item.id === "dir")?.directoryResumeCheckpoint?.completedEntries, 5);
});
test("completed cleanup-warning children retain actionable paths across compaction and restart", () => {
const now = Date.now();
const warning = "Upload completed; remove backup /target/.netcatty-backup-file manually.";
const parent: TransferTask = { ...task("warning-folder", "transferring", now), isDirectory: true };
const children: TransferTask[] = [0, 1].map((index) => ({
...task(`warning-child-${index}`, "completed", now),
parentTaskId: parent.id, endTime: now, directoryEntryIndex: index,
directoryEntryIdentity: String(index).padStart(64, "0"),
warning: index === 0 ? warning : undefined,
}));
const pruned = pruneSftpTransferHistory([parent, ...children], now);
assert.equal(pruned.some((row) => row.id === "warning-child-1"), false);
assert.equal(pruned.find((row) => row.id === parent.id)?.directoryResumeCheckpoint?.completedEntries, 1);
const restored = deserializeSftpTransferCenter(serializeSftpTransferCenter(pruned));
const retained = restored.tasks.find((row) => row.id === "warning-child-0");
assert.equal(retained?.status, "completed");
assert.equal(retained?.warning, warning);
assert.equal(retained?.error, undefined);
assert.equal(pruneSftpTransferHistory(restored.tasks, now).find((row) => row.id === retained?.id)?.warning, warning);
});
test("prune compacts 50,000 completed directory children into a bounded parent checkpoint", () => {
const pruneStartedAt = Date.now();
const now = Date.now();
+4 -1
View File
@@ -32,7 +32,7 @@ export interface PersistedSftpTransferCenter {
const SAFE_TASK_KEYS: ReadonlySet<keyof TransferTask> = new Set([
"id", "batchId", "fileName", "originalFileName", "sourcePath", "targetPath",
"sourceConnectionId", "targetConnectionId", "targetHostId", "targetConnectionKey",
"direction", "status", "totalBytes", "transferredBytes", "speed", "error",
"direction", "status", "totalBytes", "transferredBytes", "speed", "error", "warning",
"startTime", "endTime", "isDirectory", "progressMode", "childTasks", "parentTaskId",
"sourceLastModified", "preflightStatSkipped", "skipConflictCheck", "replaceExistingTarget", "retryable",
"ownerId", "sourceHostId", "sourceHostLabel", "targetHostLabel", "origin", "background",
@@ -58,6 +58,7 @@ export function sanitizeSftpTransferTask(value: unknown): TransferTask | null {
if (source[key] !== undefined) sanitized[key] = source[key];
}
const task = sanitized as unknown as TransferTask;
if (typeof task.warning !== "string" || !task.warning.trim()) task.warning = undefined;
if (task.preflightStatSkipped !== true) task.preflightStatSkipped = undefined;
if (!Number.isSafeInteger(task.directoryEntryIndex) || (task.directoryEntryIndex ?? -1) < 0) {
task.directoryEntryIndex = undefined;
@@ -213,6 +214,8 @@ export function pruneSftpTransferHistory(
if (
child.parentTaskId === parent.id
&& child.status === "completed"
// Keep per-file cleanup paths visible instead of losing them in the hash.
&& !child.warning
&& Number.isSafeInteger(child.directoryEntryIndex)
&& (child.directoryEntryIndex ?? checkpoint.coveredEntries) < checkpoint.coveredEntries
&& /^[a-f0-9]{64}$/.test(child.directoryEntryIdentity ?? "")
+4 -1
View File
@@ -306,6 +306,7 @@ async function syncFileChangeOnce(watchId, watchInfo) {
const transferId = `filewatch-${watchId}-${++watchInfo.syncSequence}`;
watchInfo.activeTransferId = transferId;
let warning;
console.log(`[FileWatcher] Streaming ${stat.size} bytes to ${remotePath}`);
try {
const result = await transferBridge.startInternalTransfer(null, {
@@ -324,18 +325,20 @@ async function syncFileChangeOnce(watchId, watchInfo) {
throw new Error("Transfer cancelled");
}
if (result?.error) throw new Error(result.error);
warning = result?.warning;
} finally {
if (watchInfo.activeTransferId === transferId) watchInfo.activeTransferId = null;
}
if (watchInfo.stopped || activeWatchers.get(watchId) !== watchInfo) return;
console.log(`[FileWatcher] Sync complete: ${remotePath}`);
showSystemNotification("Netcatty", `File synced to remote: ${fileName}`);
showSystemNotification("Netcatty", warning || `File synced to remote: ${fileName}`);
notifyWatchOwners(watchInfo, "netcatty:filewatch:synced", {
watchId,
localPath,
remotePath,
bytesWritten: stat.size,
...(warning ? { warning } : {}),
});
}
+6 -2
View File
@@ -229,7 +229,8 @@ test("session close cancels a watch whose initial stat is still pending", async
assert.equal(harness.listenersByPath.has(harness.localPath), false);
});
test("external edit sync streams through the unified transfer runtime without buffering the whole file", async (t) => {
for (const warning of [undefined, "Upload completed; remove backup /remote/.netcatty-backup-edit.txt manually."]) {
test(`external edit sync streams without buffering and ${warning ? "preserves a cleanup warning" : "reports clean success"}`, async (t) => {
const harness = createHarness(t);
const transferBridge = require("./transferBridge.cjs");
const originalStartInternalTransfer = transferBridge.startInternalTransfer;
@@ -241,7 +242,7 @@ test("external edit sync streams through the unified transfer runtime without bu
transferBridge.startInternalTransfer = async (_event, payload) => {
internalTransfers.push(payload);
return { transferId: payload.transferId };
return { transferId: payload.transferId, ...(warning ? { warning } : {}) };
};
fs.promises.readFile = async (...args) => {
wholeFileReadCalls += 1;
@@ -294,7 +295,10 @@ test("external edit sync streams through the unified transfer runtime without bu
assert.equal(internalTransfers[0].targetSftpId, harness.sftpId);
assert.equal(wholeFileReadCalls, 0);
assert.equal(directPutCalls, 0);
assert.equal(sent.find((event) => event.channel === "netcatty:filewatch:synced")?.payload.warning, warning);
assert.equal(sent.some((event) => event.channel === "netcatty:filewatch:error"), false);
});
}
test("closing the SFTP session cancels an active external edit stream", async (t) => {
const harness = createHarness(t);
@@ -493,7 +493,7 @@ function createSftpHandlerApi(ctx) {
return { ok: false, cancelled: true, transferId };
}
if (result?.error) throw new Error(result.error);
reportTransferEvent({ type: "completed", transferId, endedAt: Date.now() });
reportTransferEvent({ type: "completed", transferId, endedAt: Date.now(), ...(result?.warning ? { warning: result.warning } : {}) });
return { ok: true, transferId, ...result };
} catch (error) {
reportTransferEvent({ type: "failed", transferId, endedAt: Date.now(), error: error?.message || String(error) });
+27 -12
View File
@@ -1626,6 +1626,7 @@ async function runRemoteUploadTransaction(client, localPath, remotePath, options
throw err;
}
let backupCleanupWarning;
try {
assertCanPromote();
if (Number.isFinite(expectedSize) && expectedSize >= 0) {
@@ -1690,13 +1691,13 @@ async function runRemoteUploadTransaction(client, localPath, remotePath, options
assertCanPromote();
}
commitPromotion();
await renameRemotePath(client, encodedStagedPath, encodedPath, encodedBackupPath, {
backupCleanupWarning = await renameRemotePath(client, encodedStagedPath, encodedPath, encodedBackupPath, {
stagePath: stagedLogical,
backupPath: backupLogical,
finalPath: remotePath,
});
}, { allowBackupCleanupWarning: true });
}
return { staged: true };
return { staged: true, ...(backupCleanupWarning ? { warning: backupCleanupWarning } : {}) };
} catch (err) {
if (!err?.preserveStagedUpload) {
await cleanupStage();
@@ -1714,7 +1715,7 @@ const posixRenameAsync = (sftp, fromPath, toPath) =>
sftp.ext_openssh_rename(fromPath, toPath, (err) => (err ? reject(err) : resolve()));
});
async function renameRemotePath(client, fromPath, toPath, backupPath = null, recoveryPaths = {}) {
async function renameRemotePath(client, fromPath, toPath, backupPath = null, recoveryPaths = {}, options = {}) {
const sftp = await requireSftpChannel(client);
if (typeof sftp?.ext_openssh_rename === "function") {
try {
@@ -1758,13 +1759,27 @@ async function renameRemotePath(client, fromPath, toPath, backupPath = null, rec
}
if (movedExistingTarget) {
await deleteRemoteBackupWithRetry(
() => client.delete(backupPath),
{
finalPath: recoveryPaths.finalPath || toPath,
backupPath: recoveryPaths.backupPath || backupPath,
},
);
try {
await deleteRemoteBackupWithRetry(
() => client.delete(backupPath),
{
finalPath: recoveryPaths.finalPath || toPath,
backupPath: recoveryPaths.backupPath || backupPath,
},
);
} catch (error) {
const code = error?.cause?.code;
// Only a confirmed permission refusal after stage -> final succeeded is
// a warning. Preflight recovery, failed promotion, and transport errors
// must still fail; never borrow an interactive shell to bypass refusal.
if (!options.allowBackupCleanupWarning
|| ![3, "3", "EACCES", "EPERM", "SSH_FX_PERMISSION_DENIED"].includes(code)) {
throw error;
}
return `Upload completed: ${String(recoveryPaths.finalPath || toPath)}. `
+ `Permission denied removing backup: ${String(recoveryPaths.backupPath || backupPath)}. `
+ "The backup was retained. Check the uploaded file before removing the backup manually.";
}
}
}
}
@@ -2205,7 +2220,7 @@ async function runUnifiedSftpTransfer(payload, direction) {
throw new Error(result.error);
}
return direction === "upload"
? { success: true, transferId, remotePath: payload.remotePath }
? { success: true, transferId, remotePath: payload.remotePath, ...(result.warning ? { warning: result.warning } : {}) }
: { success: true, transferId, localPath: payload.localPath };
} finally {
payload.abortSignal?.removeEventListener?.("abort", cancel);
@@ -2875,3 +2875,85 @@ test("pipelinedUploadLocalFile falls back to raw sftp.fastPut when client.fastPu
await fs.promises.rm(tempRoot, { recursive: true, force: true });
});
function createPermissionBackupUploadHarness({ cleanupCode = 3, failPromotion = false } = {}) {
const { channel, remoteFiles, remoteMeta } = createSessionChannel();
const finalPath = "/tmp/permission-final.bin";
const backupPath = sftpBridge._buildBackupRemotePathForTests(finalPath);
remoteFiles.set(finalPath, Buffer.from("old"));
remoteMeta.set(finalPath, { mode: 0o100644 });
let uploads = 0;
let removed = 0;
const client = {
__netcattySingleChannelSsh: true,
sftp: channel,
async stat(p) {
return new Promise((resolve, reject) => channel.stat(p, (err, st) => err ? reject(err) : resolve({ ...st, isDirectory: st.isDirectory() })));
},
async rename(from, to) {
if (remoteFiles.has(to)) throw Object.assign(new Error("Overwrite unsupported"), { code: 4 });
if (failPromotion && String(from).includes(".netcatty-upload-") && to === finalPath) {
throw Object.assign(new Error("Permission denied promoting stage"), { code: 3 });
}
return new Promise((resolve, reject) => channel.rename(from, to, err => err ? reject(err) : resolve()));
},
async delete(p) {
if (p === backupPath) throw Object.assign(new Error("Permission denied"), { code: cleanupCode });
removed++;
return new Promise((resolve, reject) => channel.unlink(p, err => err ? reject(err) : resolve()));
},
};
return {
client, finalPath, backupPath, remoteFiles,
get uploads() { return uploads; },
get removed() { return removed; },
upload: () => sftpBridge.runRemoteUploadTransaction(client, "/tmp/local.bin", finalPath, {
expectedSize: 3,
uploadFile: async (p) => { uploads++; remoteFiles.set(p, Buffer.from("new")); },
}),
};
}
test("committed SFTP upload returns a visible warning when backup cleanup is permission denied", async () => {
const h = createPermissionBackupUploadHarness();
const result = await h.upload();
assert.equal(result.staged, true);
assert.match(result.warning, /Upload completed/);
assert.ok(result.warning.includes(h.finalPath));
assert.ok(result.warning.includes(h.backupPath));
assert.match(result.warning, /retained/);
assert.deepEqual(h.remoteFiles.get(h.finalPath), Buffer.from("new"));
assert.deepEqual(h.remoteFiles.get(h.backupPath), Buffer.from("old"));
assert.equal(h.removed, 0, "successful publication must not enter failure stage cleanup");
await assert.rejects(h.upload, /backup could not be removed/);
assert.equal(h.uploads, 1, "a retained recovery artifact must not be silently overwritten on retry");
});
test("permission denied promoting stage remains failure and restores the old final", async () => {
const h = createPermissionBackupUploadHarness({ failPromotion: true });
await assert.rejects(h.upload, /Permission denied promoting stage/);
assert.deepEqual(h.remoteFiles.get(h.finalPath), Buffer.from("old"));
assert.equal(h.remoteFiles.has(h.backupPath), false);
});
test("connection errors during backup cleanup are not downgraded to permission warnings", async () => {
const h = createPermissionBackupUploadHarness({ cleanupCode: "ECONNRESET" });
await assert.rejects(h.upload, /backup could not be removed/);
assert.deepEqual(h.remoteFiles.get(h.finalPath), Buffer.from("new"));
assert.deepEqual(h.remoteFiles.get(h.backupPath), Buffer.from("old"));
});
for (const code of ["3", "EACCES", "EPERM", "SSH_FX_PERMISSION_DENIED"]) {
test(`backup permission code ${code} returns warning only after publication`, async () => {
const h = createPermissionBackupUploadHarness({ cleanupCode: code });
const result = await h.upload();
assert.match(result.warning, /backup was retained/);
assert.deepEqual(h.remoteFiles.get(h.finalPath), Buffer.from("new"));
assert.deepEqual(h.remoteFiles.get(h.backupPath), Buffer.from("old"));
});
}
test("permission-looking message without a permission code remains a failure", async () => {
const h = createPermissionBackupUploadHarness({ cleanupCode: "UNKNOWN" });
await assert.rejects(h.upload, /Permission denied/);
});
@@ -133,6 +133,7 @@ function mapWorkerTransferChannelToGlobalEvent(channel, payload) {
parentTaskId: payload.parentTaskId,
directoryEntryIndex: payload.directoryEntryIndex,
directoryEntryIdentity: payload.directoryEntryIdentity,
...(payload.warning ? { warning: payload.warning } : {}),
};
}
if (channel === "netcatty:transfer:cancelled") {
@@ -103,6 +103,20 @@ test("mapWorkerTransferChannelToGlobalEvent maps complete/error/cancel", () => {
);
});
test("worker completion preserves a per-file cleanup warning without changing status", () => {
const warning = "Upload completed; remove backup /remote/.netcatty-backup-file.bin manually.";
const event = mapWorkerTransferChannelToGlobalEvent("netcatty:transfer:complete", {
transferId: "cleanup-warning", warning, totalBytes: 42, transferred: 42,
parentTaskId: "folder", directoryEntryIndex: 4, directoryEntryIdentity: "entry",
});
assert.equal(event.type, "completed");
assert.equal(event.warning, warning);
assert.equal(event.error, undefined);
assert.equal(event.totalBytes, 42);
assert.equal(event.parentTaskId, "folder");
assert.equal(event.directoryEntryIndex, 4);
});
test("mapWorkerTransferChannelToGlobalEvent maps compressed parent lifecycle", () => {
const progress = mapWorkerTransferChannelToGlobalEvent("netcatty:compress:progress", {
compressionId: "compressed-1",
+10 -6
View File
@@ -6557,8 +6557,7 @@ async function startTransferNow(event, payload, onProgress) {
};
const sendComplete = () => {
sender.send("netcatty:transfer:complete", { transferId });
broadcastGlobalTransferEvent({
const completedEvent = {
type: "completed",
transferId,
endedAt: Date.now(),
@@ -6567,7 +6566,10 @@ async function startTransferNow(event, payload, onProgress) {
parentTaskId: transfer.parentTaskId,
directoryEntryIndex: transfer.directoryEntryIndex,
directoryEntryIdentity: transfer.directoryEntryIdentity,
});
...(transfer.warning ? { warning: transfer.warning } : {}),
};
sender.send("netcatty:transfer:complete", completedEvent);
broadcastGlobalTransferEvent(completedEvent);
cleanupTransfer();
forgetLocalUploadAcknowledgedPrefix(transferId);
};
@@ -6771,7 +6773,7 @@ async function startTransferNow(event, payload, onProgress) {
const resolvedTargetEncoding = resolveEncodingForRequest(targetSftpId, targetEncoding);
let existingRemoteMode = null;
const deterministicStagePath = buildRemoteTransferStagePath(targetPath, transferId);
await runRemoteUploadTransaction(client, sourcePath, targetPath, {
const uploadResult = await runRemoteUploadTransaction(client, sourcePath, targetPath, {
encoding: resolvedTargetEncoding,
expectedSize: fileSize,
stagedPath: transfer.resumable ? deterministicStagePath : undefined,
@@ -6859,6 +6861,7 @@ async function startTransferNow(event, payload, onProgress) {
);
},
});
transfer.warning = uploadResult?.warning;
transfer.stagedRemote = null;
await restoreRemoteUploadModeBestEffort(
client,
@@ -7268,7 +7271,7 @@ async function startTransferNow(event, payload, onProgress) {
const resolvedTargetEncoding = resolveEncodingForRequest(targetSftpId, targetEncoding);
let existingRemoteMode = null;
const deterministicStagePath = buildRemoteTransferStagePath(targetPath, transferId);
await runRemoteUploadTransaction(targetClient, tempPath, targetPath, {
const uploadResult = await runRemoteUploadTransaction(targetClient, tempPath, targetPath, {
encoding: resolvedTargetEncoding,
expectedSize: fileSize,
stagedPath: transfer.resumable ? deterministicStagePath : undefined,
@@ -7344,6 +7347,7 @@ async function startTransferNow(event, payload, onProgress) {
);
},
});
transfer.warning = uploadResult?.warning;
transfer.stagedRemote = null;
await restoreRemoteUploadModeBestEffort(
targetClient,
@@ -7374,7 +7378,7 @@ async function startTransferNow(event, payload, onProgress) {
});
sendComplete();
return { transferId, totalBytes: fileSize };
return { transferId, totalBytes: fileSize, ...(transfer.warning ? { warning: transfer.warning } : {}) };
} catch (err) {
const failureMessage = err?.message || String(err);
if (
@@ -0,0 +1,121 @@
"use strict";
const test = require("node:test");
const assert = require("node:assert/strict");
const fs = require("node:fs");
const { EventEmitter } = require("node:events");
const { Readable } = require("node:stream");
const transferBridge = require("./transferBridge.cjs");
const sftpBridge = require("./sftpBridge.cjs");
const tempDirBridge = require("./tempDirBridge.cjs");
function createClient(files, targetPath, cleanupCode) {
const missing = () => Object.assign(new Error("No such file"), { code: 2 });
const attrs = (remotePath) => {
const bytes = files.get(String(remotePath));
if (!bytes) throw missing();
return { size: bytes.length, mode: 0o100644, isDirectory: () => false, isSymbolicLink: () => false };
};
const sftp = new EventEmitter();
Object.assign(sftp, {
lstat(remotePath, callback) { try { callback(null, attrs(remotePath)); } catch (error) { callback(error); } },
stat(remotePath, callback) { try { callback(null, attrs(remotePath)); } catch (error) { callback(error); } },
mkdir(_remotePath, callback) { callback(null); },
unlink(remotePath, callback) { files.delete(String(remotePath)); callback(null); },
readdir(_remotePath, callback) { callback(null, []); },
open(remotePath, flags, callback) {
if (String(flags).includes("w")) files.set(String(remotePath), Buffer.alloc(0));
callback(null, Buffer.from(String(remotePath)));
},
write(handle, buffer, offset, length, position, callback) {
const key = handle.toString();
const bytes = Buffer.alloc(Math.max(files.get(key)?.length || 0, position + length));
files.get(key)?.copy(bytes);
buffer.copy(bytes, position, offset, offset + length);
files.set(key, bytes);
setImmediate(() => callback(null));
},
read(handle, buffer, offset, length, position, callback) {
const bytes = files.get(handle.toString()) || Buffer.alloc(0);
const part = bytes.subarray(position, position + length);
part.copy(buffer, offset);
setImmediate(() => callback(null, part.length));
},
close(_handle, callback) { callback(null); },
createReadStream(remotePath, options = {}) {
const bytes = files.get(String(remotePath)) || Buffer.alloc(0);
return Readable.from([bytes.subarray(options.start || 0, options.end === undefined ? undefined : options.end + 1)]);
},
end() { assert.fail("cleanup warning must not close the shared SFTP channel"); },
});
return {
__netcattySudoMode: true,
sftp,
async stat(remotePath) { const value = attrs(remotePath); return { size: value.size, isDirectory: false }; },
async chmod() {},
async delete(remotePath) {
if (String(remotePath).includes(".netcatty-backup-")) {
throw Object.assign(new Error("fixture cleanup failure"), { code: cleanupCode });
}
files.delete(String(remotePath));
},
async rename(fromPath, toPath) {
const from = String(fromPath);
const to = String(toPath);
if (to === targetPath && files.has(to)) throw new Error("atomic replace unavailable");
if (!files.has(from)) throw missing();
files.set(to, files.get(from));
files.delete(from);
},
async end() { assert.fail("cleanup warning must not disconnect the SFTP session"); },
};
}
for (const mode of ["upload", "remote-to-remote", "direct-api", "real-failure"]) {
test(`${mode} propagates the committed backup cleanup outcome`, async (t) => {
const localPath = tempDirBridge.getTempFilePath(`upload-warning-${mode}.bin`);
const payload = Buffer.from("new complete upload");
fs.writeFileSync(localPath, payload);
t.after(() => fs.rmSync(localPath, { force: true }));
const targetPath = "/remote/file.bin";
const targetFiles = new Map([[targetPath, Buffer.from("original bytes")]]);
const target = createClient(targetFiles, targetPath, mode === "real-failure" ? 4 : 3);
const source = createClient(new Map([["/source/file.bin", payload]]), null, 3);
const clients = new Map([["target", target], ["source", source]]);
transferBridge.init({ sftpClients: clients });
sftpBridge.init({ sftpClients: clients });
const sent = [];
const sender = { send(channel, event) { sent.push({ channel, event }); } };
const transferId = `warning-${mode}`;
const result = mode === "direct-api"
? await sftpBridge.uploadLocalToSftp(null, {
sftpId: "target", localPath, remotePath: targetPath, transferId,
onTransferEvent: (channel, event) => sender.send(channel, event),
})
: await transferBridge.startTransfer({ sender }, {
transferId,
sourceType: mode === "remote-to-remote" ? "sftp" : "local",
sourcePath: mode === "remote-to-remote" ? "/source/file.bin" : localPath,
sourceSftpId: mode === "remote-to-remote" ? "source" : undefined,
targetPath, targetType: "sftp", targetSftpId: "target", totalBytes: payload.length,
});
assert.deepEqual(targetFiles.get(targetPath), payload);
const backupPath = [...targetFiles.keys()].find((key) => key.includes(".netcatty-backup-"));
assert.ok(backupPath);
assert.deepEqual(targetFiles.get(backupPath), Buffer.from("original bytes"));
if (mode === "real-failure") {
assert.ok(result.error);
assert.equal(result.warning, undefined);
assert.equal(sent.some(({ channel }) => channel === "netcatty:transfer:complete"), false);
assert.equal(sent.some(({ channel }) => channel === "netcatty:transfer:error"), true);
} else {
assert.equal(result.error, undefined);
assert.ok(result.warning?.includes(backupPath), "return value includes the actionable backup path");
const completion = sent.find(({ channel }) => channel === "netcatty:transfer:complete")?.event;
assert.equal(completion?.warning, result.warning);
assert.equal(completion?.totalBytes, payload.length);
assert.equal(sent.some(({ channel }) => channel === "netcatty:transfer:error" || channel === "netcatty:transfer:cancelled"), false);
}
assert.equal(clients.get("target"), target);
});
}
+3 -2
View File
@@ -540,7 +540,7 @@ export async function uploadLocalFoldersProgressively(
throw new Error("A local file path is required for streaming SFTP upload");
}
controller?.addActiveTransfer(childId);
let streamResult: { error?: string; cancelled?: boolean } | undefined;
let streamResult: { error?: string; cancelled?: boolean; warning?: string } | undefined;
try {
streamResult = await bridge.startStreamTransfer({
transferId: childId,
@@ -566,7 +566,8 @@ export async function uploadLocalFoldersProgressively(
throw new Error(streamResult.error);
}
results.push({ fileName: entry.relativePath, success: true });
if (streamResult?.warning) callbacks?.onTaskWarning?.(childId, streamResult.warning);
results.push({ fileName: entry.relativePath, success: true, ...(streamResult?.warning ? { warning: streamResult.warning } : {}) });
stats.completed += 1;
callbacks?.onTaskCompleted?.(childId, fileTotalBytes);
publishParentProgress(parentId, "transferring");
+169
View File
@@ -0,0 +1,169 @@
import assert from "node:assert/strict";
import test from "node:test";
import { uploadEntriesDirect } from "./uploadService";
import { uploadLocalFoldersProgressively } from "./progressiveFolderUpload";
import type { UploadCallbacks, UploadTaskInfo } from "./uploadService.types";
import { createSftpTransferCenterStore } from "../application/state/sftpTransferCenterStore";
import { createUploadTaskCallbacks } from "../application/state/sftp/uploadTaskCallbacks";
const warning = "Upload completed; remove backup /remote/.netcatty-backup-file.bin manually.";
const config = {
targetPath: "/remote", sftpId: "remote", isLocal: false,
joinPath: (base: string, name: string) => `${base}/${name}`,
bridge: {
mkdirSftp: async () => {},
startStreamTransfer: async ({ transferId }: { transferId: string }) => ({ transferId, warning }),
},
};
test("external file upload keeps success and the cleanup warning in its direct result", async () => {
const results = await uploadEntriesDirect([
{ file: null, localPath: "/local/file.bin", relativePath: "file.bin", size: 10, isDirectory: false },
], config);
assert.deepEqual(results, [{ fileName: "file.bin", success: true, warning }]);
});
test("progressive folder upload keeps each successful child's cleanup warning", async () => {
const results = await uploadLocalFoldersProgressively([{ name: "folder", localPath: "/local/folder" }], {
...config,
listLocalTree: async (_path, options) => {
options.onEntries?.([
{ localPath: "/local/folder/file.bin", relativePath: "folder/file.bin", type: "file", size: 10, lastModified: 1 },
]);
return [];
},
});
assert.deepEqual(results, [{ fileName: "folder/file.bin", success: true, warning }]);
});
for (const mode of ["progressive", "bundled", "standalone"] as const) {
for (const managesTransferLifecycle of [false, true]) {
test(`${mode} result-first warnings survive late completion and history (IPC lifecycle: ${managesTransferLifecycle})`, { timeout: 5000 }, async () => {
let persisted: string | null = null;
let onPersist: (() => void) | undefined;
const store = createSftpTransferCenterStore({
read: () => persisted,
write: (value) => { persisted = value; onPersist?.(); },
});
const created: UploadTaskInfo[] = [];
const warningAtCompletion = new Map<string, string | undefined>();
const taskCallbacks = createUploadTaskCallbacks({
ownerId: "warning-test", connectionId: "remote", targetPath: "/remote", store,
});
const callbacks: UploadCallbacks = {
...taskCallbacks,
onTaskCreated: (task) => { created.push(task); taskCallbacks.onTaskCreated?.(task); },
onTaskCompleted: (taskId, totalBytes) => {
warningAtCompletion.set(taskId, store.getTask(taskId)?.warning);
taskCallbacks.onTaskCompleted?.(taskId, totalBytes);
},
};
const uploadConfig = {
...config,
callbacks,
fileTransferConcurrency: 1,
bridge: {
...config.bridge,
managesTransferLifecycle,
// Deliberately return before delivering any completed IPC.
startStreamTransfer: async ({ transferId, sourcePath }: { transferId: string; sourcePath: string }) => ({
transferId, ...(sourcePath.endsWith("warned.bin") ? { warning } : {}),
}),
},
};
const names = ["warned.bin", "clean.bin"];
const entries = names.map((name) => ({
localPath: `/local/folder/${name}`,
relativePath: mode === "standalone" ? name : `folder/${name}`,
size: 10,
}));
const results = mode === "progressive"
? await uploadLocalFoldersProgressively([{ name: "folder", localPath: "/local/folder" }], {
...uploadConfig,
listLocalTree: async (_path, options) => {
options.onEntries?.(entries.map((entry) => ({ ...entry, type: "file" as const, lastModified: 1 })));
return [];
},
})
: await uploadEntriesDirect(entries.map((entry) => ({ ...entry, file: null, isDirectory: false })), uploadConfig);
assert.equal(results.length, 2);
assert.ok(results.every((result) => result.success));
const warned = created.find((task) => !task.isDirectory && task.fileName.endsWith("warned.bin"))!;
const clean = created.find((task) => !task.isDirectory && task.fileName.endsWith("clean.bin"))!;
assert.ok(warned);
assert.equal(store.getTask(warned.id)?.warning, warning, "invoke metadata is retained before any late event");
if (mode === "progressive" || !managesTransferLifecycle) {
assert.equal(warningAtCompletion.get(warned.id), warning, "warning precedes the compaction-triggering callback");
assert.equal(store.getTask(warned.id)?.status, "completed");
} else {
assert.equal(warningAtCompletion.has(warned.id), false, "IPC-owned files keep their existing lifecycle owner");
assert.equal(store.getTask(warned.id)?.status, "transferring", "metadata callback does not claim completion");
}
for (const task of [warned, clean]) {
store.ingestBackgroundEvent({
type: "completed", transferId: task.id, transferred: 10,
...(task.id === warned.id ? { warning } : {}),
});
}
assert.equal(store.getTask(warned.id)?.status, "completed");
assert.equal(store.getTask(warned.id)?.warning, warning);
if (mode !== "standalone") {
assert.equal(store.getTask(clean.id)?.warning, undefined, "warnings stay on the affected file, not siblings");
assert.equal(store.getTask(warned.parentTaskId!)?.status, "completed");
}
// Child lifecycle persistence is intentionally coalesced. Await its write,
// not an arbitrary delay, before simulating a process restart.
await new Promise<void>((resolve) => {
onPersist = () => {
const saved = JSON.parse(persisted || '{"tasks":[]}').tasks as Array<{ id: string; status: string; warning?: string }>;
if (saved.some((task) => task.id === warned.id && task.status === "completed" && task.warning === warning)) resolve();
};
onPersist();
});
const restored = createSftpTransferCenterStore({ read: () => persisted, write: () => {} });
assert.equal(restored.getTask(warned.id)?.status, "completed");
assert.equal(restored.getTask(warned.id)?.warning, warning, "the actionable backup path survives restart");
});
}
}
for (const mode of ["progressive", "bundled"] as const) {
for (const outcome of [{ error: "write failed" }, { cancelled: true }]) {
test(`${mode} unsuccessful results do not publish cleanup metadata (${"error" in outcome ? "failed" : "cancelled"})`, async () => {
const store = createSftpTransferCenterStore();
const created: UploadTaskInfo[] = [];
const warnings: string[] = [];
const taskCallbacks = createUploadTaskCallbacks({
ownerId: "warning-test", connectionId: "remote", targetPath: "/remote", store,
});
const uploadConfig = {
...config,
callbacks: {
...taskCallbacks,
onTaskCreated: (task: UploadTaskInfo) => { created.push(task); taskCallbacks.onTaskCreated?.(task); },
onTaskWarning: (taskId: string, value: string) => { warnings.push(value); taskCallbacks.onTaskWarning?.(taskId, value); },
},
bridge: {
...config.bridge,
startStreamTransfer: async ({ transferId }: { transferId: string }) => ({ transferId, ...outcome, warning }),
},
};
const entry = { localPath: "/local/folder/file.bin", relativePath: "folder/file.bin", size: 10 };
if (mode === "progressive") {
await uploadLocalFoldersProgressively([{ name: "folder", localPath: "/local/folder" }], {
...uploadConfig,
listLocalTree: async (_path, options) => {
options.onEntries?.([{ ...entry, type: "file", lastModified: 1 }]);
return [];
},
});
} else {
await uploadEntriesDirect([{ ...entry, file: null, isDirectory: false }], uploadConfig);
}
assert.deepEqual(warnings, []);
const file = created.find((task) => !task.isDirectory)!;
assert.equal(store.getTask(file.id)?.status, "error" in outcome ? "failed" : "cancelled");
assert.equal(store.getTask(file.id)?.warning, undefined);
});
}
}
+7 -3
View File
@@ -768,7 +768,7 @@ async function uploadEntries(
entryTargetPath: string,
standaloneTransferId: string,
fileTotalBytes: number,
): Promise<{ cancelled?: boolean; error?: string }> => {
): Promise<{ cancelled?: boolean; error?: string; warning?: string }> => {
if (controller?.isCancelled(standaloneTransferId)) return { cancelled: true };
let localFilePath = getDropEntryLocalPath(entry);
let ownedTempPath: string | undefined;
@@ -802,7 +802,7 @@ async function uploadEntries(
const fileTransferId = standaloneTransferId;
controller?.addActiveTransfer(fileTransferId);
let streamResult: { transferId: string; totalBytes?: number; error?: string; cancelled?: boolean } | undefined;
let streamResult: { transferId: string; totalBytes?: number; error?: string; cancelled?: boolean; warning?: string } | undefined;
try {
streamResult = await bridge.startStreamTransfer({
transferId: fileTransferId,
@@ -826,6 +826,7 @@ async function uploadEntries(
if (streamResult?.error) {
return { error: streamResult.error };
}
return streamResult?.warning ? { warning: streamResult.warning } : {};
} else {
if (!entry.file) {
return { error: "No local file data available" };
@@ -973,7 +974,10 @@ async function uploadEntries(
throw new Error(uploadResult.error);
}
results.push({ fileName: entry.relativePath, success: true });
if (uploadResult.warning) {
callbacks?.onTaskWarning?.(bundledChildTaskId || standaloneTransferId, uploadResult.warning);
}
results.push({ fileName: entry.relativePath, success: true, ...(uploadResult.warning ? { warning: uploadResult.warning } : {}) });
// Update progress tracking
if (bundleTaskId) {
+4 -1
View File
@@ -34,6 +34,7 @@ export interface UploadResult {
fileName: string;
success: boolean;
error?: string;
warning?: string;
cancelled?: boolean;
}
@@ -42,6 +43,8 @@ export interface UploadCallbacks {
onTaskCreated?: (task: UploadTaskInfo) => void;
/** Called when task progress is updated */
onTaskProgress?: (taskId: string, progress: UploadProgress) => void;
/** Cleanup metadata from a successful upload, before terminal callbacks. Does not change lifecycle ownership. */
onTaskWarning?: (taskId: string, warning: string) => void;
/** Called when a task is completed */
onTaskCompleted?: (taskId: string, totalBytes: number) => void;
/** Called when a task fails */
@@ -109,7 +112,7 @@ export interface UploadBridge {
globalConcurrency?: number;
skipAdmission?: boolean;
}
) => Promise<{ transferId: string; totalBytes?: number; error?: string; cancelled?: boolean }>;
) => Promise<{ transferId: string; totalBytes?: number; error?: string; cancelled?: boolean; warning?: string }>;
cancelTransfer?: (transferId: string) => Promise<void>;
}
+1 -1
View File
@@ -25,7 +25,7 @@ declare global {
listFileWatches?(): Promise<Array<{ watchId: string; localPath: string; remotePath: string; sftpId: string }>>;
registerTempFile?(sftpId: string, localPath: string): Promise<{ success: boolean }>;
unregisterTempFile?(sftpId: string, localPath: string): Promise<{ success: boolean; retained?: boolean }>;
onFileWatchSynced?(cb: (payload: { watchId: string; localPath: string; remotePath: string; bytesWritten: number }) => void): () => void;
onFileWatchSynced?(cb: (payload: { watchId: string; localPath: string; remotePath: string; bytesWritten: number; warning?: string }) => void): () => void;
onFileWatchError?(cb: (payload: { watchId: string; localPath: string; remotePath: string; error: string }) => void): () => void;
onFileWatchStopped?(cb: (payload: { watchId: string; localPath: string; remotePath: string; sftpId: string }) => void): () => void;
+2 -1
View File
@@ -89,7 +89,7 @@ declare global {
/** When true, skip main-process admission (renderer already scheduled). */
skipAdmission?: boolean;
}
): Promise<{ transferId: string; totalBytes?: number; error?: string; cancelled?: boolean }>;
): Promise<{ transferId: string; totalBytes?: number; error?: string; cancelled?: boolean; warning?: string }>;
pauseTransfer?(transferId: string): Promise<{
success: boolean;
superseded?: boolean;
@@ -123,6 +123,7 @@ declare global {
startedAt?: number;
endedAt?: number;
error?: string;
warning?: string;
transferred?: number;
totalBytes?: number;
speed?: number;