diff --git a/application/app/dedicatedResumeProgress.test.ts b/application/app/dedicatedResumeProgress.test.ts new file mode 100644 index 000000000..3eb494961 --- /dev/null +++ b/application/app/dedicatedResumeProgress.test.ts @@ -0,0 +1,51 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import type { TransferTask } from "../../domain/models"; +import { + canApplyDedicatedResumeProgress, + createDedicatedResumeChildUpdateBatcher, + DEDICATED_RESUME_CHILD_UPDATE_BATCH_SIZE, +} from "./dedicatedResumeProgress"; + +test("deferred dedicated-resume progress cannot reopen a settled row", () => { + for (const status of ["completed", "failed", "cancelled", "attention", "interrupted"] as const) { + assert.equal(canApplyDedicatedResumeProgress(status), false, status); + } + for (const status of ["pending", "queued", "transferring"] as const) { + assert.equal(canApplyDedicatedResumeProgress(status), true, status); + } +}); + +test("50,000 retained child updates use a hard-bounded number of store scans", () => { + const retained = new Set(Array.from({ length: 50_000 }, (_, index) => `child-${index}`)); + const batches: TransferTask[][] = []; + const batcher = createDedicatedResumeChildUpdateBatcher({ + getTaskCount: () => 50_001, + hasTask: (taskId) => retained.has(taskId), + upsertTasks: (tasks) => batches.push([...tasks]), + }); + + for (let index = 0; index < 50_000; index += 1) { + const child = { + id: `child-${index}`, + status: "transferring", + parentTaskId: "parent", + } as TransferTask; + batcher.push(child); + batcher.push({ ...child, status: "completed" }); + } + batcher.flush(); + + assert.ok( + batches.length <= Math.ceil(50_000 / DEDICATED_RESUME_CHILD_UPDATE_BATCH_SIZE), + `expected bounded store scans, got ${batches.length}`, + ); + const finalById = new Map(); + for (const task of batches.flat()) finalById.set(task.id, task); + assert.equal(finalById.size, 50_000); + assert.ok([...finalById.values()].every((task) => task.status === "completed")); + assert.ok( + batches.flat().length <= 50_000 + batches.length, + "a batch-boundary transition may repeat at most one child per store scan", + ); +}); diff --git a/application/app/dedicatedResumeProgress.ts b/application/app/dedicatedResumeProgress.ts new file mode 100644 index 000000000..f885ee1e3 --- /dev/null +++ b/application/app/dedicatedResumeProgress.ts @@ -0,0 +1,48 @@ +import type { TransferStatus, TransferTask } from "../../domain/models"; + +export const DEDICATED_RESUME_LARGE_HISTORY_THRESHOLD = 4_096; +export const DEDICATED_RESUME_CHILD_UPDATE_BATCH_SIZE = 512; + +export interface DedicatedResumeChildUpdateBatcher { + push(task: TransferTask): void; + flush(): void; +} + +/** + * A restarted directory can retain tens of thousands of exception rows. The + * store intentionally performs full history compaction on each upsert, so + * feeding it one child transition at a time becomes quadratic. Keep only the + * latest state for each retained child and compact in fixed-size batches. + */ +export function createDedicatedResumeChildUpdateBatcher(deps: { + getTaskCount: () => number; + hasTask: (taskId: string) => boolean; + upsertTasks: (tasks: readonly TransferTask[]) => void; +}): DedicatedResumeChildUpdateBatcher { + const pending = new Map(); + const flush = () => { + if (pending.size === 0) return; + const batch = [...pending.values()]; + pending.clear(); + deps.upsertTasks(batch); + }; + return { + push(task) { + const shouldBatch = !!task.parentTaskId + && deps.getTaskCount() >= DEDICATED_RESUME_LARGE_HISTORY_THRESHOLD + && deps.hasTask(task.id); + if (!shouldBatch) { + deps.upsertTasks([task]); + return; + } + pending.set(task.id, task); + if (pending.size >= DEDICATED_RESUME_CHILD_UPDATE_BATCH_SIZE) flush(); + }, + flush, + }; +} + +/** Only rows still owned by an active resume may accept a deferred rAF sample. */ +export function canApplyDedicatedResumeProgress(status: TransferStatus): boolean { + return status === "pending" || status === "queued" || status === "transferring"; +} diff --git a/application/app/useAppStartupEffects.ts b/application/app/useAppStartupEffects.ts index 29914cb83..56ce04ec7 100644 --- a/application/app/useAppStartupEffects.ts +++ b/application/app/useAppStartupEffects.ts @@ -11,6 +11,10 @@ import { getSftpTransferResourceKeys, globalSftpTransferScheduler } from '../sta 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, +} from './dedicatedResumeProgress'; type StartupEffectsContext = Record; @@ -94,6 +98,10 @@ export function useAppStartupEffects(ctx: StartupEffectsContext) { } 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 @@ -131,36 +139,56 @@ export function useAppStartupEffects(ctx: StartupEffectsContext) { pendingProgress = null; if (progress) applyProgress(progress); }; - return resumeTransferWithDedicatedSession( - task, - { - hosts, - keys, - identities, - knownHosts, - terminalSettings, - }, - (progress) => { - pendingProgress = progress; - if (progressRaf == null) { - progressRaf = window.requestAnimationFrame(flushProgress); - } - }, - { - children, - onChildUpdate: (child) => { - sftpTransferCenterStore.upsertTasks([{ ...child, ownerId: "dedicated-resume" }]); + 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), + }); + try { + return await resumeTransferWithDedicatedSession( + task, + { + hosts, + keys, + identities, + knownHosts, + terminalSettings, }, - 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"; + (progress) => { + pendingProgress = progress; + if (progressRaf == null) { + progressRaf = window.requestAnimationFrame(flushProgress); + } }, - }, - ); + { + children, + onChildUpdate: (child) => { + childUpdateBatcher.push({ ...child, ownerId: "dedicated-resume" }); + }, + onDirectoryCheckpointUpdate: (checkpoint) => { + 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 { + childUpdateBatcher.flush(); + } }); return () => sftpTransferCenterStore.setDedicatedResumeHandler(null); }, [enabled, hosts, identities, isVaultInitialized, keys, knownHosts, terminalSettings]); diff --git a/application/state/pluginRuntimeStatusCache.test.ts b/application/state/pluginRuntimeStatusCache.test.ts new file mode 100644 index 000000000..08b91e573 --- /dev/null +++ b/application/state/pluginRuntimeStatusCache.test.ts @@ -0,0 +1,29 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { + _resetSharedPluginRuntimeStatusForTests, + getSharedPluginRuntimeStatus, + invalidateSharedPluginRuntimeStatus, +} from "./pluginRuntimeStatusCache"; + +test("plugin runtime status is fetched once for concurrent terminal consumers", async () => { + _resetSharedPluginRuntimeStatusForTests(); + let calls = 0; + const bridge = { + async getPluginRuntimeStatus() { + calls += 1; + await Promise.resolve(); + return { available: true } as NetcattyPluginRuntimeStatus; + }, + }; + + const statuses = await Promise.all( + Array.from({ length: 20 }, () => getSharedPluginRuntimeStatus(bridge)), + ); + assert.equal(calls, 1); + assert.equal(statuses.every((status) => status.available), true); + + invalidateSharedPluginRuntimeStatus(bridge); + await getSharedPluginRuntimeStatus(bridge); + assert.equal(calls, 2); +}); diff --git a/application/state/pluginRuntimeStatusCache.ts b/application/state/pluginRuntimeStatusCache.ts new file mode 100644 index 000000000..d00b2225e --- /dev/null +++ b/application/state/pluginRuntimeStatusCache.ts @@ -0,0 +1,39 @@ +type PluginRuntimeBridge = Pick; + +interface StatusCacheEntry { + value?: NetcattyPluginRuntimeStatus; + pending?: Promise; +} + +let statusByBridge = new WeakMap(); + +export async function getSharedPluginRuntimeStatus( + bridge: PluginRuntimeBridge, +): Promise { + const key = bridge as object; + let entry = statusByBridge.get(key); + if (!entry) { + entry = {}; + statusByBridge.set(key, entry); + } + if (entry.value) return entry.value; + if (entry.pending) return entry.pending; + + const pending = bridge.getPluginRuntimeStatus!().then((status) => { + entry!.value = status; + return status; + }).finally(() => { + if (entry?.pending === pending) entry.pending = undefined; + }); + entry.pending = pending; + return pending; +} + +export function invalidateSharedPluginRuntimeStatus(bridge: object): void { + const entry = statusByBridge.get(bridge); + if (entry) entry.value = undefined; +} + +export function _resetSharedPluginRuntimeStatusForTests(): void { + statusByBridge = new WeakMap(); +} diff --git a/application/state/sftp/dedicatedTransferResume.test.ts b/application/state/sftp/dedicatedTransferResume.test.ts index 6a7b34a93..8c76ab438 100644 --- a/application/state/sftp/dedicatedTransferResume.test.ts +++ b/application/state/sftp/dedicatedTransferResume.test.ts @@ -1,10 +1,14 @@ import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; import test from "node:test"; import type { Host, TransferTask } from "../../../domain/models"; import { classifyDedicatedResumeEndpoints, classifyResumeSourceValidationError, + createPersistedResumeChildLookup, findPersistedChildForResumeFile, MAX_CONCURRENT_DEDICATED_SESSION_OPENS, resetDedicatedSessionOpenGateForTests, @@ -15,6 +19,11 @@ import { withDedicatedSessionOpenSlot, } from "./dedicatedTransferResume"; import { netcattyBridge } from "../../../infrastructure/services/netcattyBridge"; +import { + appendDirectoryManifestIdentity, + createDirectoryEntryIdentity, + createEmptyDirectoryResumeCheckpoint, +} from "../../../domain/sftpDirectoryCheckpoint"; const host = (id: string, label: string, hostname = label): Host => ({ id, @@ -63,6 +72,27 @@ test("findPersistedChildForResumeFile matches staged target paths", () => { assert.equal(shouldSkipCompletedResumeChild(findPersistedChildForResumeFile([child], planned)), true); }); +test("persisted resume child lookup handles 50,000 retained rows in linear time", () => { + const children = Array.from({ length: 50_000 }, (_, index) => ({ + id: `child-${index}`, + status: "failed" as const, + sourcePath: `/source/file-${index}`, + targetPath: `/target/file-${index}`, + checkpointBytes: index, + transferredBytes: index, + totalBytes: index + 1, + })); + const startedAt = Date.now(); + const lookup = createPersistedResumeChildLookup(children); + for (let index = 0; index < children.length; index += 1) { + assert.equal(lookup({ + sourcePath: `/source/file-${index}`, + targetPath: `/target/file-${index}`, + })?.id, `child-${index}`); + } + assert.ok(Date.now() - startedAt < 2_000, "50k retained-child lookup should stay linear"); +}); + test("resolveHostForTransferEndpoint prefers id then label", () => { const hosts = [host("id-1", "CI-Build-01", "ci-01.example")]; assert.equal(resolveHostForTransferEndpoint(hosts, "id-1", "other")?.id, "id-1"); @@ -162,6 +192,30 @@ test("directory resume skips completed children and matches by path", () => { findPersistedChildForResumeFile(ambiguous, { sourcePath: "/s/a", targetPath: "/t/2" })?.id, "y", ); + const legacyWrongTarget = [{ + id: "legacy", + status: "completed" as const, + sourcePath: "/s/only", + targetPath: "/t/folder/folder/only", + checkpointBytes: 10, + transferredBytes: 10, + totalBytes: 10, + }]; + assert.equal( + findPersistedChildForResumeFile(legacyWrongTarget, { + sourcePath: "/s/only", + targetPath: "/t/folder/only", + }), + null, + "a completed child at an obsolete target must be retransferred to the corrected target", + ); + assert.equal( + createPersistedResumeChildLookup(legacyWrongTarget)({ + sourcePath: "/s/only", + targetPath: "/t/folder/only", + }), + null, + ); }); test("directory parent classification stays distinct from single-file downloads", () => { @@ -330,3 +384,1260 @@ test("folder restart resume reports file-count progress without child page callb resetDedicatedSessionOpenGateForTests(); } }); + +test("folder upload restart honors the real local-tree root and recreates empty directories", async (t) => { + resetDedicatedSessionOpenGateForTests(); + const previousLocalStorage = Object.getOwnPropertyDescriptor(globalThis, "localStorage"); + Object.defineProperty(globalThis, "localStorage", { + configurable: true, + value: { getItem: () => "2", setItem: () => {}, removeItem: () => {} }, + }); + t.after(() => { + if (previousLocalStorage) Object.defineProperty(globalThis, "localStorage", previousLocalStorage); + else Reflect.deleteProperty(globalThis, "localStorage"); + }); + const originalGet = netcattyBridge.get; + const createdDirectories: string[] = []; + const startedTargets: string[] = []; + (netcattyBridge as { get: () => unknown }).get = () => ({ + openSftp: async () => "dedicated-sftp", + closeSftp: async () => {}, + // Real localFsBridge contract prefixes every relativePath with rootName. + listLocalTree: async () => [ + { localPath: "/local/folder", relativePath: "folder", type: "directory", size: 0, lastModified: 1 }, + { localPath: "/local/folder/empty", relativePath: "folder/empty", type: "directory", size: 0, lastModified: 1 }, + { localPath: "/local/folder/nested", relativePath: "folder/nested", type: "directory", size: 0, lastModified: 1 }, + { localPath: "/local/folder/nested/a.bin", relativePath: "folder/nested/a.bin", type: "file", size: 10, lastModified: 2 }, + ], + mkdirSftp: async (_id: string, remotePath: string) => { createdDirectories.push(remotePath); }, + statLocal: async () => ({ size: 10, lastModified: 2 }), + startStreamTransfer: async (options: { targetPath: string; transferId: string }) => { + startedTargets.push(options.targetPath); + return { transferId: options.transferId }; + }, + }); + try { + const result = await resumeTransferWithDedicatedSession({ + id: "real-local-tree-contract", + fileName: "folder", + sourcePath: "/local/folder", + targetPath: "/remote/folder", + sourceConnectionId: "local", + targetConnectionId: "old-sftp", + targetHostId: "h1", + targetHostLabel: "box", + direction: "upload", + status: "interrupted", + totalBytes: 1, + transferredBytes: 0, + speed: 0, + startTime: 1, + isDirectory: true, + progressMode: "files", + reconnectRequired: true, + }, { + hosts: [host("h1", "box", "1.2.3.4")], + keys: [], + identities: [], + }); + + assert.equal(result.success, true, result.error); + assert.deepEqual(startedTargets, ["/remote/folder/nested/a.bin"]); + assert.ok(createdDirectories.includes("/remote/folder/empty")); + assert.ok(createdDirectories.includes("/remote/folder/nested")); + assert.equal(createdDirectories.includes("/remote/folder/folder"), false); + } finally { + (netcattyBridge as { get: typeof originalGet }).get = originalGet; + resetDedicatedSessionOpenGateForTests(); + } +}); + +test("folder download restart recreates nested empty remote directories", async (t) => { + resetDedicatedSessionOpenGateForTests(); + const previousLocalStorage = Object.getOwnPropertyDescriptor(globalThis, "localStorage"); + Object.defineProperty(globalThis, "localStorage", { + configurable: true, + value: { getItem: () => "2", setItem: () => {}, removeItem: () => {} }, + }); + t.after(() => { + if (previousLocalStorage) Object.defineProperty(globalThis, "localStorage", previousLocalStorage); + else Reflect.deleteProperty(globalThis, "localStorage"); + }); + const originalGet = netcattyBridge.get; + const createdDirectories: string[] = []; + (netcattyBridge as { get: () => unknown }).get = () => ({ + openSftp: async () => "dedicated-sftp", + closeSftp: async () => {}, + listSftp: async (_id: string, remotePath: string) => remotePath === "/remote/folder" + ? [{ name: "empty", type: "directory", size: 0 }] + : [], + mkdirLocal: async (localPath: string) => { createdDirectories.push(localPath); }, + startStreamTransfer: async () => { throw new Error("empty tree must not start a file stream"); }, + }); + try { + const result = await resumeTransferWithDedicatedSession({ + id: "remote-empty-directory", + fileName: "folder", + sourcePath: "/remote/folder", + targetPath: "/local/folder", + sourceConnectionId: "old-sftp", + targetConnectionId: "local", + sourceHostId: "h1", + sourceHostLabel: "box", + direction: "download", + status: "interrupted", + totalBytes: 0, + transferredBytes: 0, + speed: 0, + startTime: 1, + isDirectory: true, + progressMode: "files", + reconnectRequired: true, + }, { + hosts: [host("h1", "box", "1.2.3.4")], keys: [], identities: [], + }); + + assert.equal(result.success, true, result.error); + assert.ok(createdDirectories.includes("/local/folder")); + assert.ok(createdDirectories.includes("/local/folder/empty")); + } finally { + (netcattyBridge as { get: typeof originalGet }).get = originalGet; + resetDedicatedSessionOpenGateForTests(); + } +}); + +test("folder download restart skips a symlink directory cycle by canonical path", async (t) => { + resetDedicatedSessionOpenGateForTests(); + const previousLocalStorage = Object.getOwnPropertyDescriptor(globalThis, "localStorage"); + Object.defineProperty(globalThis, "localStorage", { + configurable: true, + value: { getItem: () => "2", setItem: () => {}, removeItem: () => {} }, + }); + t.after(() => { + if (previousLocalStorage) Object.defineProperty(globalThis, "localStorage", previousLocalStorage); + else Reflect.deleteProperty(globalThis, "localStorage"); + }); + const originalGet = netcattyBridge.get; + const listedPaths: string[] = []; + (netcattyBridge as { get: () => unknown }).get = () => ({ + openSftp: async () => "dedicated-sftp", + closeSftp: async () => {}, + realpathSftp: async () => "/remote/folder", + listSftp: async (_id: string, remotePath: string) => { + listedPaths.push(remotePath); + return [{ name: "loop", type: "symlink", linkTarget: "directory", size: 0 }]; + }, + mkdirLocal: async () => {}, + startStreamTransfer: async () => { throw new Error("cycle must not start a stream"); }, + }); + try { + const result = await resumeTransferWithDedicatedSession({ + id: "remote-directory-cycle", + fileName: "folder", + sourcePath: "/remote/folder", + targetPath: "/local/folder", + sourceConnectionId: "old-sftp", + targetConnectionId: "local", + sourceHostId: "h1", + sourceHostLabel: "box", + direction: "download", + status: "interrupted", + totalBytes: 0, + transferredBytes: 0, + speed: 0, + startTime: 1, + isDirectory: true, + progressMode: "files", + reconnectRequired: true, + }, { + hosts: [host("h1", "box", "1.2.3.4")], keys: [], identities: [], + }); + assert.equal(result.success, true, result.error); + assert.deepEqual(listedPaths, ["/remote/folder"]); + } finally { + (netcattyBridge as { get: typeof originalGet }).get = originalGet; + resetDedicatedSessionOpenGateForTests(); + } +}); + +test("folder download restart stops remote discovery as soon as cancellation is observed", async (t) => { + resetDedicatedSessionOpenGateForTests(); + const previousLocalStorage = Object.getOwnPropertyDescriptor(globalThis, "localStorage"); + Object.defineProperty(globalThis, "localStorage", { + configurable: true, + value: { getItem: () => "2", setItem: () => {}, removeItem: () => {} }, + }); + t.after(() => { + if (previousLocalStorage) Object.defineProperty(globalThis, "localStorage", previousLocalStorage); + else Reflect.deleteProperty(globalThis, "localStorage"); + }); + const originalGet = netcattyBridge.get; + let cancelled = false; + const listedPaths: string[] = []; + (netcattyBridge as { get: () => unknown }).get = () => ({ + openSftp: async () => "dedicated-sftp", + closeSftp: async () => {}, + listSftp: async (_id: string, remotePath: string) => { + listedPaths.push(remotePath); + cancelled = true; + return remotePath === "/remote/folder" + ? [{ name: "nested", type: "directory", size: 0 }] + : []; + }, + mkdirLocal: async () => { + throw new Error("cancelled discovery must not create destination directories"); + }, + startStreamTransfer: async () => { + throw new Error("cancelled discovery must not start file transfers"); + }, + }); + try { + const result = await resumeTransferWithDedicatedSession({ + id: "cancel-directory-discovery", + fileName: "folder", + sourcePath: "/remote/folder", + targetPath: "/local/folder", + sourceConnectionId: "old-sftp", + targetConnectionId: "local", + sourceHostId: "h1", + sourceHostLabel: "box", + direction: "download", + status: "interrupted", + totalBytes: 0, + transferredBytes: 0, + speed: 0, + startTime: 1, + isDirectory: true, + progressMode: "files", + reconnectRequired: true, + }, { + hosts: [host("h1", "box", "1.2.3.4")], keys: [], identities: [], + }, undefined, { + shouldAbort: () => cancelled, + }); + + assert.equal(result.success, false); + assert.match(result.error ?? "", /cancelled/i); + assert.deepEqual(listedPaths, ["/remote/folder"]); + } finally { + (netcattyBridge as { get: typeof originalGet }).get = originalGet; + resetDedicatedSessionOpenGateForTests(); + } +}); + +test("folder download restart rejects a Windows backslash traversal entry", async (t) => { + resetDedicatedSessionOpenGateForTests(); + const previousLocalStorage = Object.getOwnPropertyDescriptor(globalThis, "localStorage"); + Object.defineProperty(globalThis, "localStorage", { + configurable: true, + value: { getItem: () => "2", setItem: () => {}, removeItem: () => {} }, + }); + t.after(() => { + if (previousLocalStorage) Object.defineProperty(globalThis, "localStorage", previousLocalStorage); + else Reflect.deleteProperty(globalThis, "localStorage"); + }); + const originalGet = netcattyBridge.get; + let starts = 0; + (netcattyBridge as { get: () => unknown }).get = () => ({ + openSftp: async () => "dedicated-sftp", + closeSftp: async () => {}, + listSftp: async () => [{ name: "..\\outside.txt", type: "file", size: 10 }], + mkdirLocal: async () => {}, + startStreamTransfer: async () => { + starts += 1; + return { transferId: "unsafe" }; + }, + }); + try { + const result = await resumeTransferWithDedicatedSession({ + id: "unsafe-remote-name", + fileName: "folder", + sourcePath: "/remote/folder", + targetPath: "C:\\Users\\alice\\Downloads\\folder", + sourceConnectionId: "old-sftp", + targetConnectionId: "local", + sourceHostId: "h1", + sourceHostLabel: "box", + direction: "download", + status: "interrupted", + totalBytes: 1, + transferredBytes: 0, + speed: 0, + startTime: 1, + isDirectory: true, + progressMode: "files", + reconnectRequired: true, + }, { + hosts: [host("h1", "box", "1.2.3.4")], keys: [], identities: [], + }); + + assert.equal(result.success, false); + assert.match(result.error ?? "", /unsafe transfer path/i); + assert.equal(starts, 0); + } finally { + (netcattyBridge as { get: typeof originalGet }).get = originalGet; + resetDedicatedSessionOpenGateForTests(); + } +}); + +test("folder restart skips 50,000 compacted completions without rebuilding child tasks", async (t) => { + const resumeStartedAt = Date.now(); + resetDedicatedSessionOpenGateForTests(); + const previousLocalStorage = Object.getOwnPropertyDescriptor(globalThis, "localStorage"); + Object.defineProperty(globalThis, "localStorage", { + configurable: true, + value: { getItem: () => "16", setItem: () => {}, removeItem: () => {} }, + }); + t.after(() => { + if (previousLocalStorage) Object.defineProperty(globalThis, "localStorage", previousLocalStorage); + else Reflect.deleteProperty(globalThis, "localStorage"); + }); + const entries = Array.from({ length: 50_000 }, (_, index) => ({ + localPath: `/local/folder/file-${index.toString().padStart(5, "0")}.bin`, + relativePath: `file-${index.toString().padStart(5, "0")}.bin`, + type: "file" as const, + size: index + 1, + lastModified: index + 10, + })); + const checkpoint = createEmptyDirectoryResumeCheckpoint(); + for (const entry of entries) { + checkpoint.manifestHash = appendDirectoryManifestIdentity( + checkpoint.manifestHash, + createDirectoryEntryIdentity({ + sourcePath: entry.localPath, + targetPath: `/remote/folder/${entry.relativePath}`, + size: entry.size, + lastModified: entry.lastModified, + }), + ); + checkpoint.coveredEntries += 1; + checkpoint.completedEntries += 1; + } + + const originalGet = netcattyBridge.get; + let starts = 0; + let childUpdates = 0; + let eventLoopHeartbeats = 0; + const heartbeat = setInterval(() => { eventLoopHeartbeats += 1; }, 1); + t.after(() => clearInterval(heartbeat)); + (netcattyBridge as { get: () => unknown }).get = () => ({ + openSftp: async () => "dedicated-sftp", + closeSftp: async () => {}, + listLocalTree: async () => entries, + mkdirSftp: async () => {}, + startStreamTransfer: async () => { + starts += 1; + return { transferId: "unexpected" }; + }, + }); + try { + const result = await resumeTransferWithDedicatedSession({ + id: "huge-folder-restart", + fileName: "folder", + sourcePath: "/local/folder", + targetPath: "/remote/folder", + sourceConnectionId: "local", + targetConnectionId: "old-sftp", + targetHostId: "h1", + targetHostLabel: "box", + direction: "upload", + status: "interrupted", + totalBytes: 50_000, + transferredBytes: 50_000, + checkpointBytes: 50_000, + speed: 0, + startTime: 1, + isDirectory: true, + progressMode: "files", + reconnectRequired: true, + directoryResumeCheckpoint: checkpoint, + }, { + hosts: [host("h1", "box", "1.2.3.4")], + keys: [], + identities: [], + }, undefined, { + children: [], + onChildUpdate: () => { childUpdates += 1; }, + }); + + assert.equal(result.success, true, result.error); + assert.equal( + eventLoopHeartbeats >= 5, + true, + "50k indexing and manifest validation must yield repeatedly to the renderer event loop", + ); + assert.equal(starts, 0, "validated compact completions must not retransfer"); + assert.equal(childUpdates, 0, "compacted completions must not return to the task array"); + assert.ok(Date.now() - resumeStartedAt < 5_000, "50k resume validation should finish within 5 seconds"); + } finally { + (netcattyBridge as { get: typeof originalGet }).get = originalGet; + resetDedicatedSessionOpenGateForTests(); + } +}); + +test("out-of-order compact resume transfers index 0 and skips completed index 1", async (t) => { + resetDedicatedSessionOpenGateForTests(); + const previousLocalStorage = Object.getOwnPropertyDescriptor(globalThis, "localStorage"); + Object.defineProperty(globalThis, "localStorage", { + configurable: true, + value: { getItem: () => "2", setItem: () => {}, removeItem: () => {} }, + }); + t.after(() => { + if (previousLocalStorage) Object.defineProperty(globalThis, "localStorage", previousLocalStorage); + else Reflect.deleteProperty(globalThis, "localStorage"); + }); + const entries = [ + { localPath: "/local/folder/a.bin", relativePath: "a.bin", type: "file" as const, size: 10, lastModified: 1 }, + { localPath: "/local/folder/b.bin", relativePath: "b.bin", type: "file" as const, size: 20, lastModified: 2 }, + ]; + const identities = entries.map((entry) => createDirectoryEntryIdentity({ + sourcePath: entry.localPath, + targetPath: `/remote/folder/${entry.relativePath}`, + size: entry.size, + lastModified: entry.lastModified, + })); + const checkpoint = createEmptyDirectoryResumeCheckpoint(); + for (const identity of identities) { + checkpoint.manifestHash = appendDirectoryManifestIdentity(checkpoint.manifestHash, identity); + checkpoint.coveredEntries += 1; + } + checkpoint.completedEntries = 1; + + const originalGet = netcattyBridge.get; + const startedPaths: string[] = []; + (netcattyBridge as { get: () => unknown }).get = () => ({ + openSftp: async () => "dedicated-sftp", + closeSftp: async () => {}, + listLocalTree: async () => entries, + mkdirSftp: async () => {}, + statLocal: async (path: string) => ({ size: path.endsWith("a.bin") ? 10 : 20, lastModified: path.endsWith("a.bin") ? 1 : 2 }), + startStreamTransfer: async (options: { sourcePath: string; transferId: string }) => { + startedPaths.push(options.sourcePath); + return { transferId: options.transferId }; + }, + }); + try { + const parent: TransferTask = { + id: "out-of-order-parent", + fileName: "folder", + sourcePath: "/local/folder", + targetPath: "/remote/folder", + sourceConnectionId: "local", + targetConnectionId: "old-sftp", + targetHostId: "h1", + targetHostLabel: "box", + direction: "upload", + status: "interrupted", + totalBytes: 2, + transferredBytes: 1, + speed: 0, + startTime: 1, + isDirectory: true, + progressMode: "files", + reconnectRequired: true, + directoryResumeCheckpoint: checkpoint, + }; + const result = await resumeTransferWithDedicatedSession(parent, { + hosts: [host("h1", "box", "1.2.3.4")], keys: [], identities: [], + }, undefined, { + children: [{ + ...parent, + id: "unfinished-index-0", + fileName: "a.bin", + sourcePath: entries[0].localPath, + targetPath: "/remote/folder/a.bin", + parentTaskId: parent.id, + isDirectory: false, + status: "interrupted", + totalBytes: 10, + directoryEntryIndex: 0, + directoryEntryIdentity: identities[0], + }], + }); + + assert.equal(result.success, true, result.error); + assert.deepEqual(startedPaths, ["/local/folder/a.bin"]); + } finally { + (netcattyBridge as { get: typeof originalGet }).get = originalGet; + resetDedicatedSessionOpenGateForTests(); + } +}); + +test("changed directory manifest clears compact completion state and retransfers safely", async (t) => { + resetDedicatedSessionOpenGateForTests(); + const previousLocalStorage = Object.getOwnPropertyDescriptor(globalThis, "localStorage"); + Object.defineProperty(globalThis, "localStorage", { + configurable: true, + value: { getItem: () => "2", setItem: () => {}, removeItem: () => {} }, + }); + t.after(() => { + if (previousLocalStorage) Object.defineProperty(globalThis, "localStorage", previousLocalStorage); + else Reflect.deleteProperty(globalThis, "localStorage"); + }); + const originalEntries = [ + { localPath: "/local/folder/a.bin", relativePath: "a.bin", type: "file" as const, size: 10, lastModified: 1 }, + { localPath: "/local/folder/b.bin", relativePath: "b.bin", type: "file" as const, size: 20, lastModified: 2 }, + ]; + const checkpoint = createEmptyDirectoryResumeCheckpoint(); + for (const entry of originalEntries) { + checkpoint.manifestHash = appendDirectoryManifestIdentity(checkpoint.manifestHash, createDirectoryEntryIdentity({ + sourcePath: entry.localPath, + targetPath: `/remote/folder/${entry.relativePath}`, + size: entry.size, + lastModified: entry.lastModified, + })); + checkpoint.coveredEntries += 1; + checkpoint.completedEntries += 1; + } + const changedEntries = originalEntries.map((entry, index) => index === 1 + ? { ...entry, lastModified: 99 } + : entry); + const originalGet = netcattyBridge.get; + const startedPaths: string[] = []; + const startedCheckpoints = new Map(); + const checkpointUpdates: Array = []; + (netcattyBridge as { get: () => unknown }).get = () => ({ + openSftp: async () => "dedicated-sftp", + closeSftp: async () => {}, + listLocalTree: async () => changedEntries, + mkdirSftp: async () => {}, + statLocal: async (path: string) => { + const entry = changedEntries.find((candidate) => candidate.localPath === path)!; + return { size: entry.size, lastModified: entry.lastModified }; + }, + startStreamTransfer: async (options: { sourcePath: string; transferId: string; checkpointBytes?: number; sourceFingerprint?: string }) => { + startedPaths.push(options.sourcePath); + startedCheckpoints.set(options.sourcePath, { + checkpointBytes: options.checkpointBytes, + sourceFingerprint: options.sourceFingerprint, + }); + return { transferId: options.transferId }; + }, + }); + try { + const result = await resumeTransferWithDedicatedSession({ + id: "changed-parent", + fileName: "folder", + sourcePath: "/local/folder", + targetPath: "/remote/folder", + sourceConnectionId: "local", + targetConnectionId: "old-sftp", + targetHostId: "h1", + targetHostLabel: "box", + direction: "upload", + status: "interrupted", + totalBytes: 2, + transferredBytes: 2, + speed: 0, + startTime: 1, + isDirectory: true, + progressMode: "files", + reconnectRequired: true, + directoryResumeCheckpoint: checkpoint, + }, { + hosts: [host("h1", "box", "1.2.3.4")], keys: [], identities: [], + }, undefined, { + children: [{ + id: "old-completed-b", + fileName: "b.bin", + sourcePath: "/local/folder/b.bin", + targetPath: "/remote/folder/b.bin", + sourceConnectionId: "local", + targetConnectionId: "old-sftp", + targetHostId: "h1", + direction: "upload", + status: "completed", + totalBytes: 20, + transferredBytes: 20, + checkpointBytes: 20, + sourceFingerprint: "sha256:old-b", + speed: 0, + startTime: 1, + endTime: 2, + isDirectory: false, + parentTaskId: "changed-parent", + directoryEntryIndex: 1, + directoryEntryIdentity: createDirectoryEntryIdentity({ + sourcePath: "/local/folder/b.bin", + targetPath: "/remote/folder/b.bin", + size: 20, + lastModified: 2, + }), + }], + onDirectoryCheckpointUpdate: (value) => checkpointUpdates.push(value), + }); + + assert.equal(result.success, true, result.error); + assert.deepEqual(startedPaths.sort(), changedEntries.map((entry) => entry.localPath).sort()); + assert.deepEqual(startedCheckpoints.get("/local/folder/b.bin"), { + checkpointBytes: 0, + sourceFingerprint: undefined, + }); + assert.deepEqual(checkpointUpdates, [undefined]); + } finally { + (netcattyBridge as { get: typeof originalGet }).get = originalGet; + resetDedicatedSessionOpenGateForTests(); + } +}); + +test("changed replace-directory manifest rebuilds the stage before promotion", async (t) => { + resetDedicatedSessionOpenGateForTests(); + const previousLocalStorage = Object.getOwnPropertyDescriptor(globalThis, "localStorage"); + Object.defineProperty(globalThis, "localStorage", { + configurable: true, + value: { getItem: () => "2", setItem: () => {}, removeItem: () => {} }, + }); + t.after(() => { + if (previousLocalStorage) Object.defineProperty(globalThis, "localStorage", previousLocalStorage); + else Reflect.deleteProperty(globalThis, "localStorage"); + }); + const originalGet = netcattyBridge.get; + const operations: string[] = []; + (netcattyBridge as { get: () => unknown }).get = () => ({ + openSftp: async () => "dedicated-sftp", + closeSftp: async () => {}, + listSftp: async (_id: string, remotePath: string) => remotePath === "/remote/folder" + ? [{ name: "new.txt", type: "file", size: 10, lastModified: 2 }] + : [], + mkdirLocal: async (localPath: string) => { operations.push(`mkdir:${localPath}`); }, + statLocal: async (localPath: string) => { + if (localPath === "/local/final.netcatty-replace-parent.part") { + return { type: "directory", size: 0, lastModified: 1 }; + } + return null; + }, + statSftp: async () => ({ type: "file", size: 10, lastModified: 2 }), + deleteLocalFile: async (localPath: string) => { operations.push(`delete:${localPath}`); }, + renameLocalFile: async (source: string, target: string) => { + operations.push(`rename:${source}->${target}`); + if (source === "/local/final") throw new Error("ENOENT"); + }, + startStreamTransfer: async (options: { transferId: string }) => { + operations.push("start"); + return { transferId: options.transferId }; + }, + }); + try { + const stage = "/local/final.netcatty-replace-parent.part"; + const result = await resumeTransferWithDedicatedSession({ + id: "replace-parent", + fileName: "folder", + sourcePath: "/remote/folder", + targetPath: "/local/final", + stagedTargetPath: stage, + replaceExistingTarget: true, + sourceConnectionId: "old-sftp", + targetConnectionId: "local", + sourceHostId: "h1", + sourceHostLabel: "box", + direction: "download", + status: "interrupted", + totalBytes: 1, + transferredBytes: 1, + speed: 0, + startTime: 1, + isDirectory: true, + progressMode: "files", + reconnectRequired: true, + directoryResumeCheckpoint: { + version: 1, + coveredEntries: 1, + completedEntries: 1, + manifestHash: "a".repeat(64), + }, + }, { + hosts: [host("h1", "box", "1.2.3.4")], keys: [], identities: [], + }); + + assert.equal(result.success, true, result.error); + const resetIndex = operations.indexOf(`delete:${stage}`); + const startIndex = operations.indexOf("start"); + assert.ok(resetIndex >= 0, `stage was not reset: ${operations.join(", ")}`); + assert.ok(resetIndex < startIndex, `stage reset must precede transfer: ${operations.join(", ")}`); + } finally { + (netcattyBridge as { get: typeof originalGet }).get = originalGet; + resetDedicatedSessionOpenGateForTests(); + } +}); + +test("local replace retries transient old-directory backup cleanup failures", async (t) => { + resetDedicatedSessionOpenGateForTests(); + const previousLocalStorage = Object.getOwnPropertyDescriptor(globalThis, "localStorage"); + Object.defineProperty(globalThis, "localStorage", { + configurable: true, + value: { getItem: () => "2", setItem: () => {}, removeItem: () => {} }, + }); + t.after(() => { + if (previousLocalStorage) Object.defineProperty(globalThis, "localStorage", previousLocalStorage); + else Reflect.deleteProperty(globalThis, "localStorage"); + }); + const originalGet = netcattyBridge.get; + const stage = "/local/final.netcatty-local-cleanup.part"; + const backup = "/local/final.netcatty-local-cleanup.backup"; + const updates: TransferTask[] = []; + let backupDeleteAttempts = 0; + (netcattyBridge as { get: () => unknown }).get = () => ({ + openSftp: async () => "dedicated-sftp", + closeSftp: async () => {}, + listSftp: async () => [], + statLocal: async (localPath: string) => localPath === stage + ? { type: "directory", size: 0, lastModified: 1 } + : null, + mkdirLocal: async () => {}, + deleteLocalFile: async (localPath: string) => { + if (localPath === backup) { + backupDeleteAttempts += 1; + if (backupDeleteAttempts < 3) throw new Error("backup busy"); + } + }, + renameLocalFile: async () => {}, + startStreamTransfer: async () => ({ transferId: "unexpected" }), + }); + try { + const result = await resumeTransferWithDedicatedSession({ + id: "local-cleanup", + fileName: "folder", + sourcePath: "/remote/folder", + targetPath: "/local/final", + stagedTargetPath: stage, + replaceExistingTarget: true, + sourceConnectionId: "old-sftp", + targetConnectionId: "local", + sourceHostId: "h1", + direction: "download", + status: "interrupted", + totalBytes: 0, + transferredBytes: 0, + speed: 0, + startTime: 1, + isDirectory: true, + progressMode: "files", + reconnectRequired: true, + }, { + hosts: [host("h1", "box", "1.2.3.4")], keys: [], identities: [], + }, undefined, { + onChildUpdate: (update) => updates.push(update), + }); + + assert.equal(result.success, true, result.error); + assert.equal(backupDeleteAttempts, 3); + assert.equal(updates.at(-1)?.stagedTargetPath, undefined); + } finally { + (netcattyBridge as { get: typeof originalGet }).get = originalGet; + resetDedicatedSessionOpenGateForTests(); + } +}); + +test("local replace removes a stale transfer backup before promoting a rebuilt stage", async (t) => { + resetDedicatedSessionOpenGateForTests(); + const previousLocalStorage = Object.getOwnPropertyDescriptor(globalThis, "localStorage"); + Object.defineProperty(globalThis, "localStorage", { + configurable: true, + value: { getItem: () => "2", setItem: () => {}, removeItem: () => {} }, + }); + t.after(() => { + if (previousLocalStorage) Object.defineProperty(globalThis, "localStorage", previousLocalStorage); + else Reflect.deleteProperty(globalThis, "localStorage"); + }); + const originalGet = netcattyBridge.get; + const root = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-replace-stale-backup-")); + const target = path.join(root, "final"); + const stage = `${target}.netcatty-stale-backup.part`; + const backup = `${target}.netcatty-stale-backup.backup`; + await fs.promises.mkdir(target, { recursive: true }); + await fs.promises.writeFile(path.join(target, "current.txt"), "current"); + await fs.promises.mkdir(stage, { recursive: true }); + await fs.promises.mkdir(backup, { recursive: true }); + await fs.promises.writeFile(path.join(backup, "old.txt"), "old"); + t.after(async () => { + await fs.promises.rm(root, { recursive: true, force: true }); + }); + (netcattyBridge as { get: () => unknown }).get = () => ({ + openSftp: async () => "dedicated-sftp", + closeSftp: async () => {}, + listSftp: async () => [], + statLocal: async (localPath: string) => { + try { + const stat = await fs.promises.stat(localPath); + return { type: stat.isDirectory() ? "directory" : "file", size: stat.size, lastModified: stat.mtimeMs }; + } catch (error) { + if ((error as NodeJS.ErrnoException).code === "ENOENT") return null; + throw error; + } + }, + mkdirLocal: async (localPath: string) => { await fs.promises.mkdir(localPath, { recursive: true }); }, + deleteLocalFile: async (localPath: string) => { await fs.promises.rm(localPath, { recursive: true }); }, + renameLocalFile: async (source: string, destination: string) => { + await fs.promises.rename(source, destination); + }, + startStreamTransfer: async () => ({ transferId: "unexpected" }), + }); + try { + const result = await resumeTransferWithDedicatedSession({ + id: "stale-backup", + fileName: "folder", + sourcePath: "/remote/folder", + targetPath: target, + stagedTargetPath: stage, + replaceExistingTarget: true, + sourceConnectionId: "old-sftp", + targetConnectionId: "local", + sourceHostId: "h1", + direction: "download", + status: "interrupted", + totalBytes: 0, + transferredBytes: 0, + speed: 0, + startTime: 1, + isDirectory: true, + progressMode: "files", + reconnectRequired: true, + }, { + hosts: [host("h1", "box", "1.2.3.4")], keys: [], identities: [], + }); + + assert.equal(result.success, true, result.error); + await assert.rejects(fs.promises.stat(backup), { code: "ENOENT" }); + assert.equal((await fs.promises.stat(target)).isDirectory(), true); + } finally { + (netcattyBridge as { get: typeof originalGet }).get = originalGet; + resetDedicatedSessionOpenGateForTests(); + } +}); + +test("local replace restores the only backup before retrying a failed promote", async (t) => { + resetDedicatedSessionOpenGateForTests(); + const previousLocalStorage = Object.getOwnPropertyDescriptor(globalThis, "localStorage"); + Object.defineProperty(globalThis, "localStorage", { + configurable: true, + value: { getItem: () => "2", setItem: () => {}, removeItem: () => {} }, + }); + t.after(() => { + if (previousLocalStorage) Object.defineProperty(globalThis, "localStorage", previousLocalStorage); + else Reflect.deleteProperty(globalThis, "localStorage"); + }); + const originalGet = netcattyBridge.get; + const target = "/local/final"; + const stage = `${target}.netcatty-recover-backup.part`; + const backup = `${target}.netcatty-recover-backup.backup`; + const existing = new Set([stage, backup]); + const operations: string[] = []; + (netcattyBridge as { get: () => unknown }).get = () => ({ + openSftp: async () => "dedicated-sftp", + closeSftp: async () => {}, + listSftp: async () => [], + statLocal: async (localPath: string) => existing.has(localPath) + ? { type: "directory", size: 0, lastModified: 1 } + : null, + mkdirLocal: async (localPath: string) => { existing.add(localPath); }, + deleteLocalFile: async (localPath: string) => { + operations.push(`delete:${localPath}`); + existing.delete(localPath); + }, + renameLocalFile: async (source: string, destination: string) => { + operations.push(`rename:${source}->${destination}`); + if (!existing.has(source)) throw new Error(`ENOENT: ${source}`); + if (existing.has(destination)) throw new Error(`EEXIST: ${destination}`); + existing.delete(source); + existing.add(destination); + }, + startStreamTransfer: async () => ({ transferId: "unexpected" }), + }); + try { + const result = await resumeTransferWithDedicatedSession({ + id: "recover-backup", + fileName: "folder", + sourcePath: "/remote/folder", + targetPath: target, + stagedTargetPath: stage, + replaceExistingTarget: true, + sourceConnectionId: "old-sftp", + targetConnectionId: "local", + sourceHostId: "h1", + direction: "download", + status: "interrupted", + totalBytes: 0, + transferredBytes: 0, + speed: 0, + startTime: 1, + isDirectory: true, + progressMode: "files", + reconnectRequired: true, + }, { + hosts: [host("h1", "box", "1.2.3.4")], keys: [], identities: [], + }); + + assert.equal(result.success, true, result.error); + const restoreIndex = operations.indexOf(`rename:${backup}->${target}`); + const firstBackupDeleteIndex = operations.indexOf(`delete:${backup}`); + assert.ok(restoreIndex >= 0, operations.join(", ")); + assert.ok( + firstBackupDeleteIndex < 0 || restoreIndex < firstBackupDeleteIndex, + `the only backup must be restored before deletion: ${operations.join(", ")}`, + ); + assert.equal(existing.has(target), true); + assert.equal(existing.has(backup), false); + } finally { + (netcattyBridge as { get: typeof originalGet }).get = originalGet; + resetDedicatedSessionOpenGateForTests(); + } +}); + +test("remote replace stays retryable when old-directory backup cleanup keeps failing", async (t) => { + resetDedicatedSessionOpenGateForTests(); + const previousLocalStorage = Object.getOwnPropertyDescriptor(globalThis, "localStorage"); + Object.defineProperty(globalThis, "localStorage", { + configurable: true, + value: { getItem: () => "2", setItem: () => {}, removeItem: () => {} }, + }); + t.after(() => { + if (previousLocalStorage) Object.defineProperty(globalThis, "localStorage", previousLocalStorage); + else Reflect.deleteProperty(globalThis, "localStorage"); + }); + const originalGet = netcattyBridge.get; + const stage = "/remote/final.netcatty-remote-cleanup.part"; + const backup = "/remote/final.netcatty-remote-cleanup.backup"; + const updates: TransferTask[] = []; + let backupDeleteAttempts = 0; + (netcattyBridge as { get: () => unknown }).get = () => ({ + openSftp: async () => "dedicated-sftp", + closeSftp: async () => {}, + listLocalTree: async () => [{ + localPath: "/local/folder", + relativePath: "folder", + type: "directory", + size: 0, + lastModified: 1, + }], + statSftp: async (_sftpId: string, remotePath: string) => remotePath === stage + ? { type: "directory", size: 0, lastModified: 1 } + : null, + mkdirSftp: async () => {}, + deleteSftp: async (_sftpId: string, remotePath: string) => { + if (remotePath === backup) { + backupDeleteAttempts += 1; + throw new Error("backup busy"); + } + }, + renameSftp: async () => {}, + startStreamTransfer: async () => ({ transferId: "unexpected" }), + }); + try { + const result = await resumeTransferWithDedicatedSession({ + id: "remote-cleanup", + fileName: "folder", + sourcePath: "/local/folder", + targetPath: "/remote/final", + stagedTargetPath: stage, + replaceExistingTarget: true, + sourceConnectionId: "local", + targetConnectionId: "old-sftp", + targetHostId: "h1", + direction: "upload", + status: "interrupted", + totalBytes: 0, + transferredBytes: 0, + speed: 0, + startTime: 1, + isDirectory: true, + progressMode: "files", + reconnectRequired: true, + }, { + hosts: [host("h1", "box", "1.2.3.4")], keys: [], identities: [], + }, undefined, { + onChildUpdate: (update) => updates.push(update), + }); + + assert.equal(result.success, false); + assert.match(result.error ?? "", /backup busy/i); + assert.equal(backupDeleteAttempts, 3); + assert.equal( + updates.some((update) => update.id === "remote-cleanup" && update.stagedTargetPath === undefined), + false, + ); + } finally { + (netcattyBridge as { get: typeof originalGet }).get = originalGet; + resetDedicatedSessionOpenGateForTests(); + } +}); + +test("local replace does not publish the stage when backing up the target fails", async (t) => { + resetDedicatedSessionOpenGateForTests(); + const previousLocalStorage = Object.getOwnPropertyDescriptor(globalThis, "localStorage"); + Object.defineProperty(globalThis, "localStorage", { + configurable: true, + value: { getItem: () => "2", setItem: () => {}, removeItem: () => {} }, + }); + t.after(() => { + if (previousLocalStorage) Object.defineProperty(globalThis, "localStorage", previousLocalStorage); + else Reflect.deleteProperty(globalThis, "localStorage"); + }); + const originalGet = netcattyBridge.get; + const target = "/local/final"; + const stage = `${target}.netcatty-local-backup-denied.part`; + const backup = `${target}.netcatty-local-backup-denied.backup`; + const renames: string[] = []; + (netcattyBridge as { get: () => unknown }).get = () => ({ + openSftp: async () => "dedicated-sftp", + closeSftp: async () => {}, + listSftp: async () => [], + statLocal: async (localPath: string) => ( + localPath === target || localPath === stage + ? { type: "directory", size: 0, lastModified: 1 } + : null + ), + mkdirLocal: async () => {}, + deleteLocalFile: async () => {}, + renameLocalFile: async (source: string, destination: string) => { + renames.push(`${source}->${destination}`); + if (source === target && destination === backup) { + const error = new Error("EACCES: permission denied"); + (error as NodeJS.ErrnoException).code = "EACCES"; + throw error; + } + }, + startStreamTransfer: async () => ({ transferId: "unexpected" }), + }); + try { + const result = await resumeTransferWithDedicatedSession({ + id: "local-backup-denied", + fileName: "folder", + sourcePath: "/remote/folder", + targetPath: target, + stagedTargetPath: stage, + replaceExistingTarget: true, + sourceConnectionId: "old-sftp", + targetConnectionId: "local", + sourceHostId: "h1", + direction: "download", + status: "interrupted", + totalBytes: 0, + transferredBytes: 0, + speed: 0, + startTime: 1, + isDirectory: true, + progressMode: "files", + reconnectRequired: true, + }, { + hosts: [host("h1", "box", "1.2.3.4")], keys: [], identities: [], + }); + + assert.equal(result.success, false); + assert.match(result.error ?? "", /permission denied/i); + assert.deepEqual(renames, [`${target}->${backup}`]); + } finally { + (netcattyBridge as { get: typeof originalGet }).get = originalGet; + resetDedicatedSessionOpenGateForTests(); + } +}); + +test("remote replace does not publish the stage when backing up the target fails", async (t) => { + resetDedicatedSessionOpenGateForTests(); + const previousLocalStorage = Object.getOwnPropertyDescriptor(globalThis, "localStorage"); + Object.defineProperty(globalThis, "localStorage", { + configurable: true, + value: { getItem: () => "2", setItem: () => {}, removeItem: () => {} }, + }); + t.after(() => { + if (previousLocalStorage) Object.defineProperty(globalThis, "localStorage", previousLocalStorage); + else Reflect.deleteProperty(globalThis, "localStorage"); + }); + const originalGet = netcattyBridge.get; + const target = "/remote/final"; + const stage = `${target}.netcatty-remote-backup-denied.part`; + const backup = `${target}.netcatty-remote-backup-denied.backup`; + const renames: string[] = []; + (netcattyBridge as { get: () => unknown }).get = () => ({ + openSftp: async () => "dedicated-sftp", + closeSftp: async () => {}, + listLocalTree: async () => [{ + localPath: "/local/folder", + relativePath: "folder", + type: "directory", + size: 0, + lastModified: 1, + }], + statSftp: async (_sftpId: string, remotePath: string) => ( + remotePath === target || remotePath === stage + ? { type: "directory", size: 0, lastModified: 1 } + : null + ), + mkdirSftp: async () => {}, + deleteSftp: async () => {}, + renameSftp: async (_sftpId: string, source: string, destination: string) => { + renames.push(`${source}->${destination}`); + if (source === target && destination === backup) { + const error = new Error("EACCES: permission denied"); + (error as NodeJS.ErrnoException).code = "EACCES"; + throw error; + } + }, + startStreamTransfer: async () => ({ transferId: "unexpected" }), + }); + try { + const result = await resumeTransferWithDedicatedSession({ + id: "remote-backup-denied", + fileName: "folder", + sourcePath: "/local/folder", + targetPath: target, + stagedTargetPath: stage, + replaceExistingTarget: true, + sourceConnectionId: "local", + targetConnectionId: "old-sftp", + targetHostId: "h1", + direction: "upload", + status: "interrupted", + totalBytes: 0, + transferredBytes: 0, + speed: 0, + startTime: 1, + isDirectory: true, + progressMode: "files", + reconnectRequired: true, + }, { + hosts: [host("h1", "box", "1.2.3.4")], keys: [], identities: [], + }); + + assert.equal(result.success, false); + assert.match(result.error ?? "", /permission denied/i); + assert.deepEqual(renames, [`${target}->${backup}`]); + } finally { + (netcattyBridge as { get: typeof originalGet }).get = originalGet; + resetDedicatedSessionOpenGateForTests(); + } +}); + +test("valid file checkpoint still rebuilds a replace stage so deleted empty directories cannot survive", async (t) => { + resetDedicatedSessionOpenGateForTests(); + const previousLocalStorage = Object.getOwnPropertyDescriptor(globalThis, "localStorage"); + Object.defineProperty(globalThis, "localStorage", { + configurable: true, + value: { getItem: () => "2", setItem: () => {}, removeItem: () => {} }, + }); + t.after(() => { + if (previousLocalStorage) Object.defineProperty(globalThis, "localStorage", previousLocalStorage); + else Reflect.deleteProperty(globalThis, "localStorage"); + }); + const originalGet = netcattyBridge.get; + const stage = "/local/final.netcatty-exact-replace.part"; + const entry = { name: "kept.txt", type: "file", size: 10, lastModified: 2 } as const; + const identity = createDirectoryEntryIdentity({ + sourcePath: "/remote/folder/kept.txt", + targetPath: `${stage}/kept.txt`, + size: 10, + lastModified: 2, + }); + const checkpoint = createEmptyDirectoryResumeCheckpoint(); + checkpoint.manifestHash = appendDirectoryManifestIdentity(checkpoint.manifestHash, identity); + checkpoint.coveredEntries = 1; + checkpoint.completedEntries = 1; + const operations: string[] = []; + const checkpointUpdates: Array = []; + (netcattyBridge as { get: () => unknown }).get = () => ({ + openSftp: async () => "dedicated-sftp", + closeSftp: async () => {}, + listSftp: async (_id: string, remotePath: string) => remotePath === "/remote/folder" ? [entry] : [], + mkdirLocal: async () => {}, + statLocal: async () => ({ type: "directory", size: 0, lastModified: 1 }), + statSftp: async () => ({ type: "file", size: 10, lastModified: 2 }), + deleteLocalFile: async () => { operations.push("reset-stage"); }, + renameLocalFile: async (source: string) => { + if (source === "/local/final") throw new Error("ENOENT"); + }, + startStreamTransfer: async (options: { transferId: string }) => { + operations.push("retransfer-file"); + return { transferId: options.transferId }; + }, + }); + try { + const result = await resumeTransferWithDedicatedSession({ + id: "exact-replace", + fileName: "folder", + sourcePath: "/remote/folder", + targetPath: "/local/final", + stagedTargetPath: stage, + replaceExistingTarget: true, + sourceConnectionId: "old-sftp", + targetConnectionId: "local", + sourceHostId: "h1", + sourceHostLabel: "box", + direction: "download", + status: "interrupted", + totalBytes: 1, + transferredBytes: 1, + speed: 0, + startTime: 1, + isDirectory: true, + progressMode: "files", + reconnectRequired: true, + directoryResumeCheckpoint: checkpoint, + }, { + hosts: [host("h1", "box", "1.2.3.4")], keys: [], identities: [], + }, undefined, { + onDirectoryCheckpointUpdate: (value) => checkpointUpdates.push(value), + }); + + assert.equal(result.success, true, result.error); + assert.deepEqual(operations.slice(0, 2), ["reset-stage", "retransfer-file"]); + assert.deepEqual(checkpointUpdates, [undefined]); + } finally { + (netcattyBridge as { get: typeof originalGet }).get = originalGet; + resetDedicatedSessionOpenGateForTests(); + } +}); + +test("corrupted replace-stage history cannot delete an unrelated directory", async (t) => { + resetDedicatedSessionOpenGateForTests(); + const previousLocalStorage = Object.getOwnPropertyDescriptor(globalThis, "localStorage"); + Object.defineProperty(globalThis, "localStorage", { + configurable: true, + value: { getItem: () => "2", setItem: () => {}, removeItem: () => {} }, + }); + t.after(() => { + if (previousLocalStorage) Object.defineProperty(globalThis, "localStorage", previousLocalStorage); + else Reflect.deleteProperty(globalThis, "localStorage"); + }); + const originalGet = netcattyBridge.get; + let deletes = 0; + (netcattyBridge as { get: () => unknown }).get = () => ({ + openSftp: async () => "dedicated-sftp", + closeSftp: async () => {}, + listSftp: async () => [], + statLocal: async () => ({ type: "directory", size: 0, lastModified: 1 }), + deleteLocalFile: async () => { deletes += 1; }, + mkdirLocal: async () => {}, + startStreamTransfer: async () => ({ transferId: "unexpected" }), + }); + try { + const result = await resumeTransferWithDedicatedSession({ + id: "safe-id", + fileName: "folder", + sourcePath: "/remote/folder", + targetPath: "/local/final", + stagedTargetPath: "/local/unrelated-important-directory", + replaceExistingTarget: true, + sourceConnectionId: "old-sftp", + targetConnectionId: "local", + sourceHostId: "h1", + sourceHostLabel: "box", + direction: "download", + status: "interrupted", + totalBytes: 0, + transferredBytes: 0, + speed: 0, + startTime: 1, + isDirectory: true, + progressMode: "files", + reconnectRequired: true, + }, { + hosts: [host("h1", "box", "1.2.3.4")], keys: [], identities: [], + }); + + assert.equal(result.success, false); + assert.match(result.error ?? "", /unsafe replacement stage path/i); + assert.equal(deletes, 0); + } finally { + (netcattyBridge as { get: typeof originalGet }).get = originalGet; + resetDedicatedSessionOpenGateForTests(); + } +}); diff --git a/application/state/sftp/dedicatedTransferResume.ts b/application/state/sftp/dedicatedTransferResume.ts index 382bf7895..079dc1255 100644 --- a/application/state/sftp/dedicatedTransferResume.ts +++ b/application/state/sftp/dedicatedTransferResume.ts @@ -11,7 +11,23 @@ import { } from "./globalTransferScheduler"; import { runWithTransferRetry } from "./transferRetry"; import { runSftpTransferWorkers } from "./transferConcurrency"; -import { getParentPath, joinPath } from "./utils"; +import { getParentPath, joinPath, joinTransferTargetPath } from "./utils"; +import { + accountSftpDirectoryEntries, + appendDirectoryManifestIdentity, + claimSftpDirectoryVisit, + compareDirectoryTraversalPaths, + createSftpDirectoryTraversalBudget, + createDirectoryEntryIdentity, + EMPTY_DIRECTORY_MANIFEST_HASH, + isValidDirectoryResumeCheckpoint, + shouldFollowSftpSymlinkDirectory, + type SftpDirectoryTraversalBudget, +} from "../../../domain/sftpDirectoryCheckpoint"; +import { + isMissingDirectoryReplacePathError, + promoteDirectoryReplaceStage as promoteDirectoryReplacePaths, +} from "./directoryReplacePromotion"; export interface DedicatedResumeDeps { hosts: readonly Host[]; @@ -44,6 +60,7 @@ export type DedicatedResumeResult = { export type DedicatedResumeOptions = { children?: readonly TransferTask[]; onChildUpdate?: (child: TransferTask) => void; + onDirectoryCheckpointUpdate?: (checkpoint: TransferTask["directoryResumeCheckpoint"]) => void; shouldAbort?: () => boolean; }; @@ -97,17 +114,6 @@ export function resolveHostForTransferEndpoint( }) ?? null; } -function isAuthError(err: unknown): boolean { - if (!(err instanceof Error)) return false; - const msg = err.message.toLowerCase(); - return ( - msg.includes("authentication") - || msg.includes("auth") - || msg.includes("password") - || msg.includes("permission denied") - ); -} - export type OpenTransferSftpSessionOptions = { /** * Prefer a live terminal SSH transport (borrow a lease + open SFTP channel). @@ -125,9 +131,10 @@ export type OpenTransferSftpSessionOptions = { /** * Open a transfer-owned SFTP session. - * Default is a dedicated vault connection. When dedicated:false and a - * sourceSessionId is provided, borrows the terminal's SSH transport instead - * (no second MFA) via openSftpForSession. + * Default is a dedicated vault connection for restart/resume. With + * dedicated:false it uses the unified transport registry, preferring the + * specified terminal transport when available and otherwise reusing/dialing by + * endpoint without forcing a second physical SSH connection. */ export async function openTransferSftpSession( host: Host, @@ -139,6 +146,14 @@ export async function openTransferSftpSession( if (!bridge?.openSftp) throw new Error("SFTP bridge unavailable"); const wantDedicated = options?.dedicated !== false; + const credentials = buildSftpHostCredentials({ + host, + hosts: [...deps.hosts], + keys: [...deps.keys], + identities: [...deps.identities], + knownHosts: deps.knownHosts ? [...deps.knownHosts] : undefined, + terminalSettings: deps.terminalSettings, + }); // Shared transport path: open an SFTP channel on the already-authenticated // terminal connection. The transport registry lease keeps the conn alive // after the shell tab closes until this sftpId is closed. @@ -149,54 +164,23 @@ export async function openTransferSftpSession( && bridge.openSftpForSession ) { try { - const sftpId = await bridge.openSftpForSession(options.sourceSessionId); + const sftpId = await bridge.openSftpForSession(options.sourceSessionId, credentials); if (sftpId) return sftpId; } catch { // Fall through to vault credentials. } } - const credentials = buildSftpHostCredentials({ - host, - hosts: [...deps.hosts], - keys: [...deps.keys], - identities: [...deps.identities], - knownHosts: deps.knownHosts ? [...deps.knownHosts] : undefined, - terminalSettings: deps.terminalSettings, - }); - - // Dedicated transfer / restart-resume must own an independent SSH dial. - // Never silently borrow a parked terminal transport (that path is for - // dedicated:false + sourceSessionId only). App restart resume depends on - // a clean vault connection + checkpoint, not a half-bound lease. + // Restart/resume owns an independent SSH dial. Normal pooled transfer opens + // leave reuseTransport enabled so the main-process registry can reuse an + // authenticated endpoint even when no terminal tab exists. const openOpts = wantDedicated ? { ...credentials, reuseTransport: false as const } : credentials; - const hasKey = !!credentials.privateKey || !!credentials.identityFilePaths?.length; - const hasPassword = !!credentials.password; - - if (hasKey) { - try { - const keyFirst = { ...openOpts }; - if (!credentials.sudo) keyFirst.password = undefined; - return await bridge.openSftp(keyFirst); - } catch (err) { - if (hasPassword && isAuthError(err)) { - return await bridge.openSftp({ - ...openOpts, - privateKey: undefined, - certificate: undefined, - publicKey: undefined, - keyId: undefined, - keySource: undefined, - identityFilePaths: undefined, - }); - } - throw err; - } - } - + // The main-process auth driver owns method ordering, fallback, MFA and + // retry. A renderer-side key-then-password retry performs a second full SSH + // dial and can repeat interactive authentication. return bridge.openSftp(openOpts); }); } @@ -242,8 +226,85 @@ export type DirectoryResumeFilePlan = { targetPath: string; size: number; lastModified?: number; + directoryEntryIndex?: number; + directoryEntryIdentity?: string; }; +const DIRECTORY_RESUME_SYNC_BUDGET_MS = 8; +const DIRECTORY_RESUME_YIELD_CHECK_INTERVAL = 128; + +function directoryResumeNow(): number { + return globalThis.performance?.now?.() ?? Date.now(); +} + +async function yieldDirectoryResumeWork(): Promise { + const scheduler = (globalThis as typeof globalThis & { + scheduler?: { yield?: () => Promise }; + }).scheduler; + if (typeof scheduler?.yield === "function") { + await scheduler.yield(); + return; + } + await new Promise((resolve) => setTimeout(resolve, 0)); +} + +async function indexDirectoryResumeFiles(files: DirectoryResumeFilePlan[]): Promise { + const sorted = [...files] + .sort((left, right) => compareDirectoryTraversalPaths(left.relativePath, right.relativePath)); + if (sorted.length >= DIRECTORY_RESUME_YIELD_CHECK_INTERVAL) { + await yieldDirectoryResumeWork(); + } + const indexed = new Array(sorted.length); + let sliceStartedAt = directoryResumeNow(); + for (let directoryEntryIndex = 0; directoryEntryIndex < sorted.length; directoryEntryIndex += 1) { + const file = sorted[directoryEntryIndex]!; + indexed[directoryEntryIndex] = { + ...file, + directoryEntryIndex, + directoryEntryIdentity: createDirectoryEntryIdentity({ + sourcePath: file.sourcePath, + targetPath: file.targetPath, + size: file.size, + lastModified: file.lastModified, + }), + }; + if ( + directoryEntryIndex > 0 + && directoryEntryIndex % DIRECTORY_RESUME_YIELD_CHECK_INTERVAL === 0 + && directoryResumeNow() - sliceStartedAt >= DIRECTORY_RESUME_SYNC_BUDGET_MS + ) { + await yieldDirectoryResumeWork(); + sliceStartedAt = directoryResumeNow(); + } + } + return indexed; +} + +export async function validateDirectoryResumeCheckpoint( + parent: Pick, + planned: readonly DirectoryResumeFilePlan[], +): Promise { + const checkpoint = parent.directoryResumeCheckpoint; + if (!isValidDirectoryResumeCheckpoint(checkpoint)) return false; + if (checkpoint.coveredEntries > planned.length) return false; + let manifestHash = EMPTY_DIRECTORY_MANIFEST_HASH; + let sliceStartedAt = directoryResumeNow(); + for (let index = 0; index < checkpoint.coveredEntries; index += 1) { + const identity = planned[index]?.directoryEntryIdentity; + if (!identity) return false; + manifestHash = appendDirectoryManifestIdentity(manifestHash, identity); + if ( + index > 0 + && index % DIRECTORY_RESUME_YIELD_CHECK_INTERVAL === 0 + && directoryResumeNow() - sliceStartedAt >= DIRECTORY_RESUME_SYNC_BUDGET_MS + ) { + await yieldDirectoryResumeWork(); + sliceStartedAt = directoryResumeNow(); + } + } + return manifestHash === checkpoint.manifestHash; +} + /** * Destination root for directory resume. Replace-mode parents write under * `stagedTargetPath` until the full tree is ready to promote. @@ -263,10 +324,31 @@ export function findPersistedChildForResumeFile( const exact = children.find((child) => child.sourcePath === file.sourcePath && child.targetPath === file.targetPath ); - if (exact) return exact; - // Fallback: unique sourcePath match only (avoid OR that can alias wrong child). - const bySource = children.filter((child) => child.sourcePath === file.sourcePath); - return bySource.length === 1 ? bySource[0]! : null; + return exact ?? null; +} + +type PersistedResumeChild = Pick< + TransferTask, + "id" | "status" | "sourcePath" | "targetPath" | "checkpointBytes" | "transferredBytes" + | "resumeStage" | "downloadCheckpointBytes" | "uploadCheckpointBytes" | "sourceFingerprint" + | "totalBytes" | "sourceLastModified" +>; + +/** Build once per directory resume; every planned-file match is then O(1). */ +export function createPersistedResumeChildLookup( + children: readonly PersistedResumeChild[], +): (file: Pick) => PersistedResumeChild | null { + const exactBySource = new Map>(); + for (const child of children) { + let targets = exactBySource.get(child.sourcePath); + if (!targets) { + targets = new Map(); + exactBySource.set(child.sourcePath, targets); + } + if (!targets.has(child.targetPath)) targets.set(child.targetPath, child); + } + return (file) => exactBySource.get(file.sourcePath)?.get(file.targetPath) + ?? null; } export function shouldSkipCompletedResumeChild( @@ -526,21 +608,59 @@ async function resumeSingleFileWithDedicatedSession( } } +type DirectoryResumeTraversal = { + files: DirectoryResumeFilePlan[]; + directoryRelativePaths: string[]; +}; + async function listRemoteFilesRecursive( sftpId: string, rootPath: string, relativePrefix = "", -): Promise { + followSymlinkDirectories = false, + symlinkDepth = 0, + shouldAbort?: () => boolean, + traversalBudget?: SftpDirectoryTraversalBudget, +): Promise { + if (shouldAbort?.()) throw new Error("Transfer cancelled"); const bridge = netcattyBridge.get(); if (!bridge?.listSftp) throw new Error("SFTP list unavailable"); + const traversal = traversalBudget ?? createSftpDirectoryTraversalBudget(); + const canonicalPath = await bridge.realpathSftp?.(sftpId, rootPath, "auto") + .catch(() => rootPath) ?? rootPath; + if (!claimSftpDirectoryVisit(traversal, canonicalPath)) { + return { files: [], directoryRelativePaths: [] }; + } const entries = await bridge.listSftp(sftpId, rootPath, "auto"); + accountSftpDirectoryEntries(traversal, entries.length); + if (shouldAbort?.()) throw new Error("Transfer cancelled"); const files: DirectoryResumeFilePlan[] = []; + const directoryRelativePaths: string[] = []; for (const entry of entries) { + if (shouldAbort?.()) throw new Error("Transfer cancelled"); if (!entry?.name || entry.name === "." || entry.name === "..") continue; const relativePath = relativePrefix ? `${relativePrefix}/${entry.name}` : entry.name; const fullPath = joinPath(rootPath, entry.name); - if (entry.type === "directory" || (entry.type === "symlink" && entry.linkTarget === "directory")) { - files.push(...await listRemoteFilesRecursive(sftpId, fullPath, relativePath)); + const isDirectory = entry.type === "directory"; + const isFollowedSymlinkDirectory = followSymlinkDirectories + && entry.type === "symlink" + && entry.linkTarget === "directory" + && shouldFollowSftpSymlinkDirectory(symlinkDepth); + if (isDirectory || isFollowedSymlinkDirectory) { + directoryRelativePaths.push(relativePath); + const nested = await listRemoteFilesRecursive( + sftpId, + fullPath, + relativePath, + followSymlinkDirectories, + isFollowedSymlinkDirectory ? symlinkDepth + 1 : symlinkDepth, + shouldAbort, + traversal, + ); + for (const nestedFile of nested.files) files.push(nestedFile); + for (const nestedDirectory of nested.directoryRelativePaths) { + directoryRelativePaths.push(nestedDirectory); + } continue; } if (entry.type === "directory") continue; @@ -560,16 +680,26 @@ async function listRemoteFilesRecursive( lastModified, }); } - return files; + return { files, directoryRelativePaths }; +} + +function normalizeLocalTreeRelativePath(sourceRoot: string, relativePath: string): string { + const normalized = relativePath.replace(/\\/g, "/").replace(/^\/+|\/+$/g, ""); + const rootName = sourceRoot.replace(/\\/g, "/").replace(/\/+$/g, "").split("/").pop() ?? ""; + if (!rootName) return normalized; + if (normalized === rootName) return ""; + return normalized.startsWith(`${rootName}/`) ? normalized.slice(rootName.length + 1) : normalized; } async function collectDirectoryResumeFiles( parent: TransferTask, endpoints: ResolvedEndpoints, sourceSftpId: string | undefined, -): Promise { + shouldAbort?: () => boolean, +): Promise<{ files: DirectoryResumeFilePlan[]; directoryTargetPaths: string[] }> { const bridge = netcattyBridge.get(); const destRoot = resolveDirectoryResumeTargetRoot(parent); + if (shouldAbort?.()) throw new Error("Transfer cancelled"); // Upload: local source tree. if (endpoints.isUpload) { @@ -577,27 +707,65 @@ async function collectDirectoryResumeFiles( throw new Error("Local folder listing is unavailable for upload resume"); } const localEntries = await bridge.listLocalTree(parent.sourcePath); - return localEntries + if (shouldAbort?.()) throw new Error("Transfer cancelled"); + const normalizedEntries = localEntries.map((entry) => ({ + ...entry, + normalizedRelativePath: normalizeLocalTreeRelativePath(parent.sourcePath, entry.relativePath), + })); + const files = await indexDirectoryResumeFiles(normalizedEntries .filter((entry) => entry.type === "file") .map((entry) => ({ - relativePath: entry.relativePath.replace(/\\/g, "/"), + relativePath: entry.normalizedRelativePath, sourcePath: entry.localPath, - targetPath: joinPath(destRoot, entry.relativePath.replace(/\\/g, "/")), + targetPath: joinTransferTargetPath(destRoot, entry.normalizedRelativePath), size: entry.size, lastModified: entry.lastModified, - })); + }))); + return { + files, + directoryTargetPaths: normalizedEntries + .filter((entry) => entry.type === "directory" && !!entry.normalizedRelativePath) + .map((entry) => joinTransferTargetPath(destRoot, entry.normalizedRelativePath)), + }; } // Download or remote-to-remote: list remote source. if (!sourceSftpId) throw new Error("Source SFTP session missing for directory resume"); - const remoteFiles = await listRemoteFilesRecursive(sourceSftpId, parent.sourcePath); - return remoteFiles.map((file) => ({ - ...file, - targetPath: joinPath(destRoot, file.relativePath), - })); + const remote = await listRemoteFilesRecursive( + sourceSftpId, + parent.sourcePath, + "", + endpoints.isDownload, + 0, + shouldAbort, + ); + const files = await indexDirectoryResumeFiles(remote.files.map((file) => ({ + ...file, + targetPath: joinTransferTargetPath(destRoot, file.relativePath), + }))); + return { + files, + directoryTargetPaths: remote.directoryRelativePaths.map((relativePath) => ( + joinTransferTargetPath(destRoot, relativePath) + )), + }; } /** Atomically promote a replace-mode staged directory to the final target path. */ +function expectedDirectoryReplaceStage(parent: Pick): string { + const safeId = String(parent.id).replace(/[^A-Za-z0-9_-]/g, "_"); + return `${parent.targetPath}.netcatty-${safeId}.part`; +} + +function assertSafeDirectoryReplaceStage(parent: TransferTask): void { + if ( + parent.stagedTargetPath + && parent.stagedTargetPath !== expectedDirectoryReplaceStage(parent) + ) { + throw new Error("Unsafe replacement stage path in saved transfer history"); + } +} + async function promoteDirectoryReplaceStage( parent: TransferTask, endpoints: ResolvedEndpoints, @@ -605,64 +773,82 @@ async function promoteDirectoryReplaceStage( ): Promise { const staged = parent.stagedTargetPath; if (!staged || staged === parent.targetPath) return; + assertSafeDirectoryReplaceStage(parent); const bridge = netcattyBridge.get(); if (!bridge) throw new Error("Transfer bridge unavailable"); const safeId = String(parent.id).replace(/[^A-Za-z0-9_-]/g, "_"); const backupPath = `${parent.targetPath}.netcatty-${safeId}.backup`; - let backedUp = false; - try { - if (endpoints.isDownload) { - if (!bridge.renameLocalFile || !bridge.deleteLocalFile) { - throw new Error("Local directory replacement is unavailable"); - } - try { - await bridge.renameLocalFile(parent.targetPath, backupPath); - backedUp = true; - } catch { /* target may not exist */ } - await bridge.renameLocalFile(staged, parent.targetPath); - if (backedUp) await bridge.deleteLocalFile(backupPath); - return; + if (endpoints.isDownload) { + if (!bridge.statLocal || !bridge.renameLocalFile || !bridge.deleteLocalFile) { + throw new Error("Local directory replacement is unavailable"); } - if (!targetSftpId) throw new Error("Target SFTP session missing for directory promote"); - if (!bridge.renameSftp || !bridge.deleteSftp) { - throw new Error("Remote directory replacement is unavailable"); - } - try { - await bridge.renameSftp(targetSftpId, parent.targetPath, backupPath, "auto"); - backedUp = true; - } catch { /* target may not exist */ } - try { - await bridge.renameSftp(targetSftpId, staged, parent.targetPath, "auto"); - } catch (error) { - if (backedUp) { - await bridge.renameSftp(targetSftpId, backupPath, parent.targetPath, "auto").catch(() => {}); - } - throw error; - } - if (backedUp) await bridge.deleteSftp(targetSftpId, backupPath, "auto"); - } catch (error) { - if (backedUp && endpoints.isDownload) { - await bridge.renameLocalFile?.(backupPath, parent.targetPath).catch(() => {}); - } - throw error; + await promoteDirectoryReplacePaths({ + targetPath: parent.targetPath, + stagedPath: staged, + backupPath, + statPath: bridge.statLocal, + renamePath: bridge.renameLocalFile, + deletePath: bridge.deleteLocalFile, + }); + return; } + if (!targetSftpId) throw new Error("Target SFTP session missing for directory promote"); + if (!bridge.statSftp || !bridge.renameSftp || !bridge.deleteSftp) { + throw new Error("Remote directory replacement is unavailable"); + } + await promoteDirectoryReplacePaths({ + targetPath: parent.targetPath, + stagedPath: staged, + backupPath, + statPath: (candidate) => bridge.statSftp!(targetSftpId, candidate, "auto"), + renamePath: (source, target) => bridge.renameSftp!(targetSftpId, source, target, "auto"), + deletePath: (candidate) => bridge.deleteSftp!(targetSftpId, candidate, "auto"), + }); } async function ensureLocalDir(dirPath: string): Promise { if (!dirPath) return; - try { - await netcattyBridge.get()?.mkdirLocal?.(dirPath); - } catch { - // Parent may already exist. - } + const mkdirLocal = netcattyBridge.get()?.mkdirLocal; + if (!mkdirLocal) throw new Error("Local directory creation is unavailable"); + await mkdirLocal(dirPath); } async function ensureRemoteDir(sftpId: string, dirPath: string): Promise { if (!dirPath || dirPath === "/") return; + const mkdirSftp = netcattyBridge.get()?.mkdirSftp; + if (!mkdirSftp) throw new Error("Remote directory creation is unavailable"); + await mkdirSftp(sftpId, dirPath, "auto"); +} + +function isMissingTransferPathError(error: unknown): boolean { + return isMissingDirectoryReplacePathError(error); +} + +async function resetDirectoryReplaceStage( + parent: TransferTask, + endpoints: ResolvedEndpoints, + targetSftpId: string | undefined, +): Promise { + const staged = parent.stagedTargetPath; + if (!staged || staged === parent.targetPath) return; + assertSafeDirectoryReplaceStage(parent); + const bridge = netcattyBridge.get(); + if (!bridge) throw new Error("Transfer bridge unavailable"); + try { - await netcattyBridge.get()?.mkdirSftp?.(sftpId, dirPath, "auto"); - } catch { - // Intermediate exists is fine. + if (endpoints.isDownload) { + if (!bridge.deleteLocalFile) throw new Error("Local directory replacement is unavailable"); + if (bridge.statLocal && !await bridge.statLocal(staged)) return; + await bridge.deleteLocalFile(staged); + return; + } + if (!targetSftpId || !bridge.deleteSftp) { + throw new Error("Remote directory replacement is unavailable"); + } + if (bridge.statSftp && !await bridge.statSftp(targetSftpId, staged, "auto")) return; + await bridge.deleteSftp(targetSftpId, staged, "auto"); + } catch (error) { + if (!isMissingTransferPathError(error)) throw error; } } @@ -711,20 +897,68 @@ async function resumeDirectoryWithDedicatedSession( sourceSftpId = opened.sourceSftpId; targetSftpId = opened.targetSftpId; + const traversal = await collectDirectoryResumeFiles( + parent, + endpoints, + sourceSftpId, + options?.shouldAbort, + ); + const planned = traversal.files; + totalFiles = planned.length; + const hasCompactCheckpoint = isValidDirectoryResumeCheckpoint(parent.directoryResumeCheckpoint); + const compactCheckpointValid = hasCompactCheckpoint + && await validateDirectoryResumeCheckpoint(parent, planned); + // File checkpoints intentionally do not persist an unbounded directory + // path list. Consequently they cannot prove that an empty directory was + // not deleted at the source. Rebuild every interrupted replace stage so + // promotion is an exact snapshot; ordinary copy/resume still reuses its + // compact file checkpoint. + const resetReplaceStage = !!parent.stagedTargetPath; + const reusableCompactCheckpoint = compactCheckpointValid && !resetReplaceStage; + if ((hasCompactCheckpoint && !compactCheckpointValid) || resetReplaceStage) { + // Fail closed: a changed/reordered source invalidates the whole compact + // prefix. Persist the reset before retransferring so another restart + // cannot reuse stale completion state. + options?.onDirectoryCheckpointUpdate?.(undefined); + } + if (resetReplaceStage) { + // A stage is an exact replacement snapshot. Without a valid manifest, + // retained files in it cannot be distinguished from files deleted at + // the source since the interrupted attempt, so rebuild it from empty. + await resetDirectoryReplaceStage(parent, endpoints, targetSftpId); + } const destRoot = resolveDirectoryResumeTargetRoot(parent); if (endpoints.isDownload) await ensureLocalDir(destRoot); if (targetSftpId) await ensureRemoteDir(targetSftpId, destRoot); - - const planned = await collectDirectoryResumeFiles(parent, endpoints, sourceSftpId); - totalFiles = planned.length; - completedCount = planned.filter((file) => - shouldSkipCompletedResumeChild(findPersistedChildForResumeFile(existingChildren, file)), - ).length; + for (const directoryPath of traversal.directoryTargetPaths) { + if (options?.shouldAbort?.()) throw new Error("Transfer cancelled"); + if (endpoints.isDownload) await ensureLocalDir(directoryPath); + else if (targetSftpId) await ensureRemoteDir(targetSftpId, directoryPath); + } + const compactedCompleted = reusableCompactCheckpoint + ? parent.directoryResumeCheckpoint!.completedEntries + : 0; + const retainedCompleted = !resetReplaceStage && (!hasCompactCheckpoint || compactCheckpointValid) + ? existingChildren.filter((child) => child.status === "completed").length + : 0; + completedCount = compactedCompleted + retainedCompleted; bumpParentProgress(0); - const pending = planned.filter((file) => - !shouldSkipCompletedResumeChild(findPersistedChildForResumeFile(existingChildren, file)), - ); + const findPersistedChild = createPersistedResumeChildLookup(existingChildren); + const pending = planned.filter((file) => { + const persisted = findPersistedChild(file); + if ( + shouldSkipCompletedResumeChild(persisted) + && !resetReplaceStage + && (!hasCompactCheckpoint || compactCheckpointValid) + ) return false; + return !( + reusableCompactCheckpoint + && (file.directoryEntryIndex ?? Number.MAX_SAFE_INTEGER) + < parent.directoryResumeCheckpoint!.coveredEntries + && !persisted + ); + }); await runSftpTransferWorkers( pending, @@ -732,10 +966,12 @@ async function resumeDirectoryWithDedicatedSession( async (file) => { if (options?.shouldAbort?.()) throw new Error("Transfer cancelled"); - const persisted = findPersistedChildForResumeFile(existingChildren, file); + const persisted = findPersistedChild(file); if (options?.shouldAbort?.()) throw new Error("Transfer cancelled"); const childId = persisted?.id ?? crypto.randomUUID(); + const resetPersistedCheckpoint = resetReplaceStage + || (hasCompactCheckpoint && !compactCheckpointValid); let childBase: TransferTask = { ...parent, ...persisted, @@ -750,7 +986,9 @@ async function resumeDirectoryWithDedicatedSession( ownerId: "dedicated-resume", status: "transferring", totalBytes: file.size || persisted?.totalBytes || 0, - transferredBytes: persisted?.checkpointBytes ?? persisted?.transferredBytes ?? 0, + transferredBytes: resetPersistedCheckpoint + ? 0 + : (persisted?.checkpointBytes ?? persisted?.transferredBytes ?? 0), speed: 0, startTime: persisted?.startTime ?? Date.now(), endTime: undefined, @@ -758,12 +996,18 @@ async function resumeDirectoryWithDedicatedSession( reconnectRequired: false, phase: "transferring", resumable: parent.resumable !== false, - checkpointBytes: persisted?.checkpointBytes ?? persisted?.transferredBytes ?? 0, - resumeStage: persisted?.resumeStage, - downloadCheckpointBytes: persisted?.downloadCheckpointBytes, - uploadCheckpointBytes: persisted?.uploadCheckpointBytes, - sourceFingerprint: persisted?.sourceFingerprint, - sourceLastModified: file.lastModified ?? persisted?.sourceLastModified, + checkpointBytes: resetPersistedCheckpoint + ? 0 + : (persisted?.checkpointBytes ?? persisted?.transferredBytes ?? 0), + resumeStage: resetPersistedCheckpoint ? undefined : persisted?.resumeStage, + downloadCheckpointBytes: resetPersistedCheckpoint ? undefined : persisted?.downloadCheckpointBytes, + uploadCheckpointBytes: resetPersistedCheckpoint ? undefined : persisted?.uploadCheckpointBytes, + sourceFingerprint: resetPersistedCheckpoint ? undefined : persisted?.sourceFingerprint, + sourceLastModified: resetPersistedCheckpoint + ? file.lastModified + : (persisted?.sourceLastModified ?? file.lastModified), + directoryEntryIndex: file.directoryEntryIndex, + directoryEntryIdentity: file.directoryEntryIdentity, conflict: undefined, }; @@ -786,11 +1030,6 @@ async function resumeDirectoryWithDedicatedSession( if (!sourceStat) { throw new Error("Source is unavailable"); } - childBase = { - ...childBase, - totalBytes: sourceStat.size || childBase.totalBytes, - sourceLastModified: sourceStat.lastModified ?? childBase.sourceLastModified, - }; const validationError = validateTransferResumeSource(childBase, { size: sourceStat.size, lastModified: sourceStat.lastModified, @@ -802,6 +1041,7 @@ async function resumeDirectoryWithDedicatedSession( checkpointBytes: 0, transferredBytes: 0, totalBytes: sourceStat.size, + sourceLastModified: sourceStat.lastModified, }; } else if (classified.kind === "modified") { attentionCount += 1; @@ -817,6 +1057,12 @@ async function resumeDirectoryWithDedicatedSession( return; } else if (classified.kind === "fatal") { throw new Error(classified.message || validationError || "Resume validation failed"); + } else { + childBase = { + ...childBase, + totalBytes: sourceStat.size || childBase.totalBytes, + sourceLastModified: sourceStat.lastModified ?? childBase.sourceLastModified, + }; } // Re-check abort after async stat before inserting a transferring child. diff --git a/application/state/sftp/directoryListingCache.test.ts b/application/state/sftp/directoryListingCache.test.ts new file mode 100644 index 000000000..962f3cbb3 --- /dev/null +++ b/application/state/sftp/directoryListingCache.test.ts @@ -0,0 +1,51 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { + getDirectoryCacheEntry, + setDirectoryCacheEntry, + type DirectoryListingCache, +} from "./directoryListingCache"; + +const files = (count: number) => Array.from({ length: count }, (_value, index) => ({ + name: `file-${index}`, +})) as never[]; + +test("directory cache removes expired entries when it is read", () => { + const cache: DirectoryListingCache = new Map([ + ["expired", { files: files(1), timestamp: 0 }], + ["fresh", { files: files(1), timestamp: 95 }], + ]); + + assert.equal(getDirectoryCacheEntry(cache, "fresh", 100, 10)?.files.length, 1); + assert.equal(cache.has("expired"), false); +}); + +test("directory cache evicts least-recently-used listings by entry count", () => { + const cache: DirectoryListingCache = new Map(); + setDirectoryCacheEntry(cache, "a", { files: files(1), timestamp: 1 }, { now: 1, ttlMs: 1_000, maxEntries: 2 }); + setDirectoryCacheEntry(cache, "b", { files: files(1), timestamp: 2 }, { now: 2, ttlMs: 1_000, maxEntries: 2 }); + assert.ok(getDirectoryCacheEntry(cache, "a", 3, 1_000)); + setDirectoryCacheEntry(cache, "c", { files: files(1), timestamp: 4 }, { now: 4, ttlMs: 1_000, maxEntries: 2 }); + + assert.deepEqual([...cache.keys()], ["a", "c"]); +}); + +test("directory cache bounds retained file rows", () => { + const cache: DirectoryListingCache = new Map(); + setDirectoryCacheEntry(cache, "large-a", { files: files(6), timestamp: 1 }, { now: 1, ttlMs: 1_000, maxFiles: 10 }); + setDirectoryCacheEntry(cache, "large-b", { files: files(6), timestamp: 2 }, { now: 2, ttlMs: 1_000, maxFiles: 10 }); + + assert.deepEqual([...cache.keys()], ["large-b"]); +}); + +test("directory cache does not retain one listing larger than the file budget", () => { + const cache: DirectoryListingCache = new Map(); + setDirectoryCacheEntry( + cache, + "oversized", + { files: files(11), timestamp: 1 }, + { now: 1, ttlMs: 1_000, maxFiles: 10 }, + ); + + assert.equal(cache.has("oversized"), false); +}); diff --git a/application/state/sftp/directoryListingCache.ts b/application/state/sftp/directoryListingCache.ts new file mode 100644 index 000000000..3c30b1015 --- /dev/null +++ b/application/state/sftp/directoryListingCache.ts @@ -0,0 +1,69 @@ +import type { SftpFileEntry } from "../../../domain/models"; + +export interface DirectoryListingCacheEntry { + files: SftpFileEntry[]; + timestamp: number; +} + +export type DirectoryListingCache = Map; + +export const MAX_DIRECTORY_CACHE_ENTRIES = 128; +export const MAX_DIRECTORY_CACHE_FILES = 20_000; + +function removeExpiredEntries( + cache: DirectoryListingCache, + now: number, + ttlMs: number, +): void { + for (const [key, entry] of cache) { + if (now - entry.timestamp >= ttlMs) cache.delete(key); + } +} + +export function getDirectoryCacheEntry( + cache: DirectoryListingCache, + key: string, + now: number, + ttlMs: number, +): DirectoryListingCacheEntry | undefined { + removeExpiredEntries(cache, now, ttlMs); + const entry = cache.get(key); + if (!entry) return undefined; + cache.delete(key); + cache.set(key, entry); + return entry; +} + +export function setDirectoryCacheEntry( + cache: DirectoryListingCache, + key: string, + entry: DirectoryListingCacheEntry, + options: { + now?: number; + ttlMs?: number; + maxEntries?: number; + maxFiles?: number; + } = {}, +): void { + const now = options.now ?? Date.now(); + const ttlMs = options.ttlMs ?? 10_000; + const maxEntries = options.maxEntries ?? MAX_DIRECTORY_CACHE_ENTRIES; + const maxFiles = options.maxFiles ?? MAX_DIRECTORY_CACHE_FILES; + removeExpiredEntries(cache, now, ttlMs); + cache.delete(key); + // A cache budget must remain a hard bound. Truncating a directory listing + // would be incorrect, so oversized listings are served to the caller but + // deliberately not retained. + if (entry.files.length > maxFiles || maxEntries < 1) return; + cache.set(key, entry); + + let totalFiles = 0; + for (const value of cache.values()) totalFiles += value.files.length; + while (cache.size > 1 && (cache.size > maxEntries || totalFiles > maxFiles)) { + const oldestKey = cache.keys().next().value as string | undefined; + if (!oldestKey) break; + const oldest = cache.get(oldestKey); + cache.delete(oldestKey); + totalFiles -= oldest?.files.length ?? 0; + } +} diff --git a/application/state/sftp/directoryReplacePromotion.test.ts b/application/state/sftp/directoryReplacePromotion.test.ts new file mode 100644 index 000000000..9ee2e0082 --- /dev/null +++ b/application/state/sftp/directoryReplacePromotion.test.ts @@ -0,0 +1,129 @@ +import assert from "node:assert/strict"; +import fs from "node:fs"; +import test from "node:test"; + +import { promoteDirectoryReplaceStage } from "./directoryReplacePromotion"; + +function createPathHarness(initialPaths: string[]) { + const paths = new Set(initialPaths); + const operations: string[] = []; + return { + paths, + operations, + statPath: async (candidate: string) => paths.has(candidate) ? { type: "directory" } : null, + renamePath: async (source: string, target: string) => { + operations.push(`rename:${source}->${target}`); + if (!paths.has(source)) throw new Error(`ENOENT: ${source}`); + if (paths.has(target)) throw new Error(`EEXIST: ${target}`); + paths.delete(source); + paths.add(target); + }, + deletePath: async (candidate: string) => { + operations.push(`delete:${candidate}`); + if (!paths.delete(candidate)) throw new Error(`ENOENT: ${candidate}`); + }, + }; +} + +const targetPath = "/target/final"; +const stagedPath = "/target/final.netcatty-live.part"; +const backupPath = "/target/final.netcatty-live.backup"; + +test("live directory replace restores an interrupted backup before retrying publication", async () => { + const harness = createPathHarness([stagedPath, backupPath]); + + await promoteDirectoryReplaceStage({ + targetPath, + stagedPath, + backupPath, + statPath: harness.statPath, + renamePath: harness.renamePath, + deletePath: harness.deletePath, + }); + + assert.equal(harness.operations[0], `rename:${backupPath}->${targetPath}`); + assert.equal(harness.paths.has(targetPath), true); + assert.equal(harness.paths.has(stagedPath), false); + assert.equal(harness.paths.has(backupPath), false); +}); + +test("live directory replace stops when the existing target cannot be backed up", async () => { + const harness = createPathHarness([targetPath, stagedPath]); + const renamePath = async (source: string, target: string) => { + harness.operations.push(`rename:${source}->${target}`); + if (source === targetPath && target === backupPath) throw new Error("EACCES: backup denied"); + return harness.renamePath(source, target); + }; + + await assert.rejects( + promoteDirectoryReplaceStage({ + targetPath, + stagedPath, + backupPath, + statPath: harness.statPath, + renamePath, + deletePath: harness.deletePath, + }), + /backup denied/, + ); + + assert.equal(harness.paths.has(targetPath), true); + assert.equal(harness.paths.has(stagedPath), true); + assert.equal(harness.paths.has(backupPath), false); + assert.equal(harness.operations.some((operation) => operation === `rename:${stagedPath}->${targetPath}`), false); +}); + +test("live directory replace retries transient backup cleanup", async () => { + const harness = createPathHarness([targetPath, stagedPath]); + let deleteAttempts = 0; + const deletePath = async (candidate: string) => { + deleteAttempts += 1; + if (deleteAttempts < 3) throw new Error("EBUSY: backup locked"); + return harness.deletePath(candidate); + }; + + await promoteDirectoryReplaceStage({ + targetPath, + stagedPath, + backupPath, + statPath: harness.statPath, + renamePath: harness.renamePath, + deletePath, + }); + + assert.equal(deleteAttempts, 3); + assert.equal(harness.paths.has(targetPath), true); + assert.equal(harness.paths.has(backupPath), false); +}); + +test("live directory replace keeps the committed target recoverable when backup cleanup persists", async () => { + const harness = createPathHarness([targetPath, stagedPath]); + let deleteAttempts = 0; + + await assert.rejects( + promoteDirectoryReplaceStage({ + targetPath, + stagedPath, + backupPath, + statPath: harness.statPath, + renamePath: harness.renamePath, + deletePath: async () => { + deleteAttempts += 1; + throw new Error("EPERM: backup retained"); + }, + }), + /backup retained/, + ); + + assert.equal(deleteAttempts, 3); + assert.equal(harness.paths.has(targetPath), true, "the new committed target remains published"); + assert.equal(harness.paths.has(backupPath), true, "the old tree remains available for recovery"); + assert.equal(harness.paths.has(stagedPath), false); +}); + +test("live and restart-resume directory replacement both call the shared promotion helper", () => { + const liveSource = fs.readFileSync(new URL("./useSftpTransfers.ts", import.meta.url), "utf8"); + const resumeSource = fs.readFileSync(new URL("./dedicatedTransferResume.ts", import.meta.url), "utf8"); + assert.match(liveSource, /promoteDirectoryReplacePaths\(\{/); + assert.match(resumeSource, /promoteDirectoryReplacePaths\(\{/); +}); diff --git a/application/state/sftp/directoryReplacePromotion.ts b/application/state/sftp/directoryReplacePromotion.ts new file mode 100644 index 000000000..8d7b710a8 --- /dev/null +++ b/application/state/sftp/directoryReplacePromotion.ts @@ -0,0 +1,132 @@ +export const DIRECTORY_REPLACE_BACKUP_DELETE_ATTEMPTS = 3; + +export function isMissingDirectoryReplacePathError(error: unknown): boolean { + const message = error instanceof Error ? error.message : String(error); + return /\bENOENT\b|no such file|not found|does not exist/i.test(message); +} + +export async function deleteDirectoryReplaceBackup( + deleteBackup: () => Promise, + options: { + attempts?: number; + delay?: (attempt: number) => Promise; + } = {}, +): Promise { + const attempts = Math.max(1, options.attempts ?? DIRECTORY_REPLACE_BACKUP_DELETE_ATTEMPTS); + const delay = options.delay ?? ((attempt: number) => new Promise((resolve) => { + setTimeout(resolve, attempt * 25); + })); + let lastError: unknown; + for (let attempt = 1; attempt <= attempts; attempt += 1) { + try { + await deleteBackup(); + return; + } catch (error) { + lastError = error; + if (attempt < attempts) await delay(attempt); + } + } + throw lastError; +} + +export interface DirectoryReplacePromotionOptions { + targetPath: string; + stagedPath: string; + backupPath: string; + statPath: (path: string) => Promise; + renamePath: (source: string, target: string) => Promise; + deletePath: (path: string) => Promise; +} + +async function pathExists( + statPath: DirectoryReplacePromotionOptions["statPath"], + candidate: string, +): Promise { + try { + return Boolean(await statPath(candidate)); + } catch (error) { + if (isMissingDirectoryReplacePathError(error)) return false; + throw error; + } +} + +function createDirectoryReplaceRecoveryError( + promotionError: unknown, + restoreError: unknown, + options: Pick, +): Error { + const error = new Error( + `Directory replacement failed and the original could not be restored. ` + + `Target: ${options.targetPath}; backup: ${options.backupPath}; stage: ${options.stagedPath}. ` + + `Promotion error: ${promotionError instanceof Error ? promotionError.message : String(promotionError)}. ` + + `Restore error: ${restoreError instanceof Error ? restoreError.message : String(restoreError)}`, + ); + (error as Error & { cause?: unknown }).cause = promotionError; + return error; +} + +/** + * Publish a fully-written replacement directory with one shared recovery rule. + * + * Both live transfers and restart resume use this transaction: + * - restore the only known-good backup before touching anything else; + * - remove only a stale backup that accompanies a live final target; + * - ignore only a genuinely missing final target; + * - restore the original if stage promotion fails; + * - keep a committed target and its backup recoverable if cleanup keeps failing. + */ +export async function promoteDirectoryReplaceStage( + options: DirectoryReplacePromotionOptions, +): Promise { + const { + targetPath, + stagedPath, + backupPath, + statPath, + renamePath, + deletePath, + } = options; + if (!targetPath || !stagedPath || !backupPath || stagedPath === targetPath) { + throw new Error("Invalid directory replacement paths"); + } + + const backupExists = await pathExists(statPath, backupPath); + if (backupExists) { + if (await pathExists(statPath, targetPath)) { + await deleteDirectoryReplaceBackup(() => deletePath(backupPath)); + } else { + // An interrupted prior commit left the backup as the only known-good tree. + // Restore it before creating a new backup or publishing a rebuilt stage. + await renamePath(backupPath, targetPath); + } + } + + let backedUp = false; + try { + await renamePath(targetPath, backupPath); + backedUp = true; + } catch (error) { + // Permission, conflict, and transport failures must stop publication. Only + // a missing target is safe to treat as a new-directory replacement. + if (!isMissingDirectoryReplacePathError(error)) throw error; + } + + try { + await renamePath(stagedPath, targetPath); + } catch (promotionError) { + if (backedUp) { + try { + await renamePath(backupPath, targetPath); + } catch (restoreError) { + throw createDirectoryReplaceRecoveryError(promotionError, restoreError, options); + } + } + throw promotionError; + } + + if (backedUp) { + // Publication is already committed. Persistent cleanup failure deliberately + // leaves both final and backup present and reports a retryable failure. + await deleteDirectoryReplaceBackup(() => deletePath(backupPath)); + } +} diff --git a/application/state/sftp/ensureRemoteSftpSession.test.ts b/application/state/sftp/ensureRemoteSftpSession.test.ts index 072e7396a..a9a165f36 100644 --- a/application/state/sftp/ensureRemoteSftpSession.test.ts +++ b/application/state/sftp/ensureRemoteSftpSession.test.ts @@ -35,6 +35,8 @@ const remotePane = (connectionId: string): SftpPane => ({ connectionLogs: [], } as unknown as SftpPane); +const noopReleaseConnection = async () => {}; + test("returns an existing mapped SFTP session without reconnecting", async () => { let connectCalls = 0; const sftpId = await ensureRemoteSftpSession({ @@ -43,6 +45,7 @@ test("returns an existing mapped SFTP session without reconnecting", async () => sftpSessionsRef: { current: new Map([["conn-1", "sftp-live"]]) }, lastConnectedHostRef: { current: { left: host, right: null } }, connect: async () => { connectCalls += 1; }, + releaseConnection: noopReleaseConnection, }); assert.equal(sftpId, "sftp-live"); assert.equal(connectCalls, 0); @@ -63,6 +66,7 @@ test("reconnects when the mapped session is missing", async () => { connectCalls += 1; sessions.current.set("conn-1", "sftp-reconnected"); }, + releaseConnection: noopReleaseConnection, }); assert.equal(connectCalls, 1); assert.equal(sftpId, "sftp-reconnected"); @@ -70,6 +74,7 @@ test("reconnects when the mapped session is missing", async () => { test("forceReconnect reopens even when a mapping exists", async () => { let connectCalls = 0; + const released: string[] = []; const sessions = { current: new Map([["conn-1", "sftp-stale"]]) }; const sftpId = await ensureRemoteSftpSession({ side: "left", @@ -77,17 +82,20 @@ test("forceReconnect reopens even when a mapping exists", async () => { sftpSessionsRef: sessions, lastConnectedHostRef: { current: { left: host, right: null } }, forceReconnect: true, + releaseConnection: async (connectionId) => { released.push(connectionId); }, connect: async () => { connectCalls += 1; sessions.current.set("conn-1", "sftp-new"); }, }); assert.equal(connectCalls, 1); + assert.deepEqual(released, ["conn-1"]); assert.equal(sftpId, "sftp-new"); }); test("probe failure triggers reconnect", async () => { let connectCalls = 0; + const released: string[] = []; const sessions = { current: new Map([["conn-1", "sftp-dead"]]) }; const sftpId = await ensureRemoteSftpSession({ side: "left", @@ -97,12 +105,14 @@ test("probe failure triggers reconnect", async () => { probeSession: async () => { throw new Error("SFTP session not found"); }, + releaseConnection: async (connectionId) => { released.push(connectionId); }, connect: async () => { connectCalls += 1; sessions.current.set("conn-1", "sftp-fresh"); }, }); assert.equal(connectCalls, 1); + assert.deepEqual(released, ["conn-1"]); assert.equal(sftpId, "sftp-fresh"); }); @@ -119,6 +129,7 @@ test("uses resolveHostById when lastConnectedHostRef is empty", async () => { connectedHost = resolved; sessions.current.set("conn-1", "sftp-vault"); }, + releaseConnection: noopReleaseConnection, }); assert.equal(sftpId, "sftp-vault"); assert.equal((connectedHost as Host).hostname, "ci.example"); @@ -151,6 +162,7 @@ test("prefers per-tab connect-time host over vault base endpoint", async () => { connectedHost = resolved; sessions.current.set("conn-1", "sftp-session"); }, + releaseConnection: noopReleaseConnection, }); assert.equal((connectedHost as Host).hostname, "session.example"); assert.equal((connectedHost as Host).port, 2222); @@ -182,6 +194,7 @@ test("does not retarget background reconnect via side-wide lastConnectedHost ove connectedHost = resolved; sessions.current.set("conn-1", "sftp-vault"); }, + releaseConnection: noopReleaseConnection, }); assert.equal((connectedHost as Host).hostname, "vault.example"); assert.equal((connectedHost as Host).port, 22); @@ -197,6 +210,7 @@ test("refuses synthetic root@label:22 when host metadata is missing", async () = connect: async () => { throw new Error("should not connect"); }, + releaseConnection: noopReleaseConnection, }), /credentials are unavailable/, ); diff --git a/application/state/sftp/ensureRemoteSftpSession.ts b/application/state/sftp/ensureRemoteSftpSession.ts index f48684b0d..2a7c110aa 100644 --- a/application/state/sftp/ensureRemoteSftpSession.ts +++ b/application/state/sftp/ensureRemoteSftpSession.ts @@ -21,6 +21,8 @@ export interface EnsureRemoteSftpSessionParams { /** Resolve vault host by id when per-tab connect-time host is unavailable. */ resolveHostById?: (hostId: string) => Host | null | undefined; probeSession?: (sftpId: string) => Promise; + /** Remove connection metadata and close the mapped backend session. */ + releaseConnection: (connectionId: string) => Promise; forceReconnect?: boolean; /** Stable tab identity — reconnect replaces connection ids, not tab ids. */ tabId?: string; @@ -42,6 +44,7 @@ export async function ensureRemoteSftpSession( resolveConnectedHost, resolveHostById, probeSession, + releaseConnection, forceReconnect = false, tabId, } = params; @@ -98,11 +101,15 @@ export async function ensureRemoteSftpSession( if (!isSessionError(error)) throw error; } const pane = getActivePane(side); - if (pane?.connection) sftpSessionsRef.current.delete(pane.connection.id); + if (pane?.connection) { + await releaseConnection(pane.connection.id); + } } } else { const pane = getActivePane(side); - if (pane?.connection) sftpSessionsRef.current.delete(pane.connection.id); + if (pane?.connection) { + await releaseConnection(pane.connection.id); + } } const paneBefore = getActivePane(side); diff --git a/application/state/sftp/externalFileWatchLifecycle.test.ts b/application/state/sftp/externalFileWatchLifecycle.test.ts new file mode 100644 index 000000000..faf9ff96e --- /dev/null +++ b/application/state/sftp/externalFileWatchLifecycle.test.ts @@ -0,0 +1,172 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import React from "react"; +import { act, create, type ReactTestRenderer } from "react-test-renderer"; +import { + cleanupFailedExternalOpenTemp, + useExternalFileWatchLifecycle, + type ExternalFileWatchLifecycle, +} from "./externalFileWatchLifecycle"; + +test("failed external app launch unregisters and deletes its remote temp file", async () => { + const calls: string[] = []; + await cleanupFailedExternalOpenTemp({ + unregisterTempFile: async (sftpId, localPath) => { + calls.push(`${sftpId}:${localPath}`); + return { success: true }; + }, + }, "sftp-1", "/tmp/edit.txt"); + assert.deepEqual(calls, ["sftp-1:/tmp/edit.txt"]); +}); + +test("renderer tracks a reused watch once and releases it on unmount", async () => { + const stopped: Array<{ watchId: string; cleanupTempFile: boolean }> = []; + let lifecycle: ExternalFileWatchLifecycle | null = null; + let renderer: ReactTestRenderer | null = null; + + function Probe() { + lifecycle = useExternalFileWatchLifecycle(async (watchId, cleanupTempFile) => { + stopped.push({ watchId, cleanupTempFile }); + }); + return null; + } + + await act(async () => { + renderer = create(React.createElement(Probe)); + }); + await act(async () => { + lifecycle!.remember("watch-reused"); + lifecycle!.remember("watch-reused"); + }); + assert.equal(lifecycle!.activeCountRef.current, 1); + + await act(async () => renderer!.unmount()); + await new Promise((resolve) => setImmediate(resolve)); + + assert.deepEqual(stopped, [{ watchId: "watch-reused", cleanupTempFile: false }]); +}); + +test("explicit SFTP lifecycle cleanup releases every tracked watch and temp file", async () => { + const stopped: Array<{ watchId: string; cleanupTempFile: boolean }> = []; + let lifecycle: ExternalFileWatchLifecycle | null = null; + let renderer: ReactTestRenderer | null = null; + + function Probe() { + lifecycle = useExternalFileWatchLifecycle(async (watchId, cleanupTempFile) => { + stopped.push({ watchId, cleanupTempFile }); + }); + return null; + } + + await act(async () => { + renderer = create(React.createElement(Probe)); + }); + lifecycle!.remember("watch-a"); + lifecycle!.remember("watch-b"); + + await lifecycle!.releaseAll(true); + + assert.equal(lifecycle!.activeCountRef.current, 0); + assert.deepEqual(stopped, [ + { watchId: "watch-a", cleanupTempFile: true }, + { watchId: "watch-b", cleanupTempFile: true }, + ]); + await act(async () => renderer!.unmount()); +}); + +test("a watch that starts after unmount is stopped instead of being retained", async () => { + const stopped: Array<{ watchId: string; cleanupTempFile: boolean }> = []; + let lifecycle: ExternalFileWatchLifecycle | null = null; + let renderer: ReactTestRenderer | null = null; + + function Probe() { + lifecycle = useExternalFileWatchLifecycle(async (watchId, cleanupTempFile) => { + stopped.push({ watchId, cleanupTempFile }); + }); + return null; + } + + await act(async () => { + renderer = create(React.createElement(Probe)); + }); + const lateLifecycle = lifecycle!; + await act(async () => renderer!.unmount()); + + lateLifecycle.remember("watch-started-after-unmount"); + await new Promise((resolve) => setImmediate(resolve)); + + assert.deepEqual(stopped, [{ + watchId: "watch-started-after-unmount", + cleanupTempFile: false, + }]); + assert.equal(lateLifecycle.activeCountRef.current, 0); +}); + +test("release invalidates pending watch starts while a new generation remains usable", async () => { + const stopped: Array<{ watchId: string; cleanupTempFile: boolean }> = []; + let lifecycle: ExternalFileWatchLifecycle | null = null; + let renderer: ReactTestRenderer | null = null; + + function Probe() { + lifecycle = useExternalFileWatchLifecycle(async (watchId, cleanupTempFile) => { + stopped.push({ watchId, cleanupTempFile }); + }); + return null; + } + + await act(async () => { + renderer = create(React.createElement(Probe)); + }); + const oldGeneration = lifecycle!.captureGeneration(); + await lifecycle!.releaseAll(true); + + lifecycle!.remember("late-old-watch", oldGeneration); + const currentGeneration = lifecycle!.captureGeneration(); + lifecycle!.remember("fresh-watch", currentGeneration); + await new Promise((resolve) => setImmediate(resolve)); + + assert.deepEqual(stopped, [{ watchId: "late-old-watch", cleanupTempFile: true }]); + assert.equal(lifecycle!.activeCountRef.current, 1); + + await lifecycle!.releaseAll(false); + assert.deepEqual(stopped, [ + { watchId: "late-old-watch", cleanupTempFile: true }, + { watchId: "fresh-watch", cleanupTempFile: false }, + ]); + await act(async () => renderer!.unmount()); +}); + +test("backend session cleanup removes a stopped watch from renderer ownership", async () => { + const stopped: Array<{ watchId: string; cleanupTempFile: boolean }> = []; + let onBackendStopped: ((payload: { watchId: string }) => void) | null = null; + let lifecycle: ExternalFileWatchLifecycle | null = null; + let renderer: ReactTestRenderer | null = null; + + function Probe() { + lifecycle = useExternalFileWatchLifecycle( + async (watchId, cleanupTempFile) => { + stopped.push({ watchId, cleanupTempFile }); + }, + (callback) => { + onBackendStopped = callback; + return () => { onBackendStopped = null; }; + }, + ); + return null; + } + + await act(async () => { + renderer = create(React.createElement(Probe)); + }); + lifecycle!.remember("watch-closed-with-session"); + assert.equal(lifecycle!.activeCountRef.current, 1); + + await act(async () => { + onBackendStopped?.({ watchId: "watch-closed-with-session" }); + }); + assert.equal(lifecycle!.activeCountRef.current, 0); + + await act(async () => renderer!.unmount()); + await new Promise((resolve) => setImmediate(resolve)); + assert.deepEqual(stopped, [], "backend-owned cleanup must not issue a duplicate stop"); +}); diff --git a/application/state/sftp/externalFileWatchLifecycle.ts b/application/state/sftp/externalFileWatchLifecycle.ts new file mode 100644 index 000000000..850d6bf56 --- /dev/null +++ b/application/state/sftp/externalFileWatchLifecycle.ts @@ -0,0 +1,95 @@ +import { useCallback, useEffect, useRef, type MutableRefObject } from "react"; + +export async function cleanupFailedExternalOpenTemp( + bridge: Pick, + sftpId: string, + localPath: string, +): Promise { + if (!localPath) return; + if (bridge.unregisterTempFile) { + await bridge.unregisterTempFile(sftpId, localPath); + return; + } + await bridge.deleteTempFile?.(localPath); +} + +export type StopExternalFileWatch = ( + watchId: string, + cleanupTempFile: boolean, +) => void | Promise; + +export type SubscribeExternalFileWatchStopped = ( + callback: (payload: { watchId: string }) => void, +) => (() => void) | void; + +export interface ExternalFileWatchLifecycle { + activeCountRef: MutableRefObject; + captureGeneration(): number; + remember(watchId: string | undefined, generation?: number): void; + releaseAll(cleanupTempFiles?: boolean): Promise; +} + +export function useExternalFileWatchLifecycle( + stopWatch: StopExternalFileWatch, + subscribeStopped?: SubscribeExternalFileWatchStopped, +): ExternalFileWatchLifecycle { + const watchIdsRef = useRef>(new Set()); + const activeCountRef = useRef(0); + const stopWatchRef = useRef(stopWatch); + const subscribeStoppedRef = useRef(subscribeStopped); + const disposedRef = useRef(false); + const generationRef = useRef(0); + const invalidatedCleanupTempFilesRef = useRef(false); + stopWatchRef.current = stopWatch; + subscribeStoppedRef.current = subscribeStopped; + + const captureGeneration = useCallback(() => generationRef.current, []); + + const remember = useCallback((watchId: string | undefined, generation = generationRef.current) => { + if (!watchId) return; + if (disposedRef.current || generation !== generationRef.current) { + // startFileWatch may resolve after the owning React tree unmounts. There + // may also have been an explicit disconnect cleanup while IPC was pending. + // There will be no later cleanup for the old generation, so release now. + const cleanupTempFile = disposedRef.current + ? false + : invalidatedCleanupTempFilesRef.current; + void Promise.resolve() + .then(() => stopWatchRef.current(watchId, cleanupTempFile)) + .catch(() => {}); + return; + } + watchIdsRef.current.add(watchId); + activeCountRef.current = watchIdsRef.current.size; + }, []); + + const releaseAll = useCallback(async (cleanupTempFiles = false) => { + invalidatedCleanupTempFilesRef.current = cleanupTempFiles; + generationRef.current += 1; + const watchIds = [...watchIdsRef.current]; + watchIdsRef.current.clear(); + activeCountRef.current = 0; + await Promise.all(watchIds.map(async (watchId) => { + try { + await stopWatchRef.current(watchId, cleanupTempFiles); + } catch { + // The owning SFTP session or worker may already have released it. + } + })); + }, []); + + useEffect(() => { + disposedRef.current = false; + return () => { + disposedRef.current = true; + void releaseAll(false); + }; + }, [releaseAll]); + + useEffect(() => subscribeStoppedRef.current?.(({ watchId }) => { + if (!watchId || !watchIdsRef.current.delete(watchId)) return; + activeCountRef.current = watchIdsRef.current.size; + }), []); + + return { activeCountRef, captureGeneration, remember, releaseAll }; +} diff --git a/application/state/sftp/externalUploadCancel.test.ts b/application/state/sftp/externalUploadCancel.test.ts index 2b398fc2b..b4ca547eb 100644 --- a/application/state/sftp/externalUploadCancel.test.ts +++ b/application/state/sftp/externalUploadCancel.test.ts @@ -1,6 +1,8 @@ import test from "node:test"; import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; +import { drainUploadConflictResolvers } from "./useSftpExternalOperations"; +import { UploadController } from "../../../lib/uploadService"; test("external upload cancellation is keyed by transfer task id", () => { const ops = readFileSync(new URL("./useSftpExternalOperations.ts", import.meta.url), "utf8"); @@ -20,3 +22,40 @@ test("upload conflict cancel is scoped to owning controller", () => { assert.match(ops, /uploadConflictOwnersRef/); assert.match(ops, /cancelPendingUploadConflicts = useCallback\(\(controller\?: UploadController\)/); }); + +test("upload conflict cleanup resolves every pending prompt on unmount", () => { + const first = new UploadController(); + const second = new UploadController(); + const resolved: string[] = []; + const resolvers = new Map([ + ["first", { resolve: (action: string) => resolved.push(`first:${action}`), setDefault() {} }], + ["second", { resolve: (action: string) => resolved.push(`second:${action}`), setDefault() {} }], + ]) as Parameters[0]; + const owners = new Map([["first", first], ["second", second]]); + + assert.deepEqual(drainUploadConflictResolvers(resolvers, owners), ["first", "second"]); + assert.deepEqual(resolved, ["first:stop", "second:stop"]); + assert.equal(resolvers.size, 0); + assert.equal(owners.size, 0); +}); + +test("all four external folder upload entry points keep compression and conflict handling wired together", () => { + const source = readFileSync(new URL("./useSftpExternalOperations.ts", import.meta.url), "utf8"); + const entryPoints = [ + "uploadExternalFiles", + "uploadExternalFileList", + "uploadExternalFolderPath", + "uploadExternalEntries", + ]; + + for (let index = 0; index < entryPoints.length; index += 1) { + const start = source.indexOf(`const ${entryPoints[index]} = useCallback`); + const nextName = entryPoints[index + 1]; + const end = nextName ? source.indexOf(`const ${nextName} = useCallback`, start + 1) : source.length; + assert.ok(start >= 0, `${entryPoints[index]} must remain present`); + const section = source.slice(start, end >= 0 ? end : source.length); + assert.match(section, /runWithCompressedUploadSession\(\{/); + assert.match(section, /enabled: useCompressedUpload/); + assert.match(section, /resolveConflict: createUploadConflictResolver\(controller\)/); + } +}); diff --git a/application/state/sftp/openTransferSftpSession.dedicated.test.ts b/application/state/sftp/openTransferSftpSession.dedicated.test.ts index ffbb0f2ec..5abea895d 100644 --- a/application/state/sftp/openTransferSftpSession.dedicated.test.ts +++ b/application/state/sftp/openTransferSftpSession.dedicated.test.ts @@ -13,6 +13,8 @@ const host: Host = { hostname: "1.2.3.4", port: 22, username: "root", + tags: [], + os: "linux", protocol: "ssh", authType: "password", password: "secret", @@ -76,12 +78,14 @@ test("openTransferSftpSession defaults to dedicated vault open (ignores terminal test("openTransferSftpSession can use terminal session only when dedicated:false", async () => { resetDedicatedSessionOpenGateForTests(); let openForSessionCalls = 0; + let expectedEndpoint: NetcattySSHOptions | undefined; const { netcattyBridge } = await import("../../../infrastructure/services/netcattyBridge.ts"); const restore = netcattyBridge.get; (netcattyBridge as { get: () => unknown }).get = () => ({ openSftp: async () => "dedicated-sftp", - openSftpForSession: async () => { + openSftpForSession: async (_sessionId: string, endpoint?: NetcattySSHOptions) => { openForSessionCalls += 1; + expectedEndpoint = endpoint; return "session-sftp"; }, }); @@ -93,6 +97,71 @@ test("openTransferSftpSession can use terminal session only when dedicated:false ); assert.equal(id, "session-sftp"); assert.equal(openForSessionCalls, 1); + assert.equal(expectedEndpoint?.hostname, host.hostname); + assert.equal(expectedEndpoint?.password, host.password); + } finally { + (netcattyBridge as { get: typeof restore }).get = restore; + resetDedicatedSessionOpenGateForTests(); + } +}); + +test("non-dedicated transfer without a terminal keeps unified transport reuse enabled", async () => { + resetDedicatedSessionOpenGateForTests(); + const seen: NetcattySSHOptions[] = []; + let openForSessionCalls = 0; + const { netcattyBridge } = await import("../../../infrastructure/services/netcattyBridge.ts"); + const restore = netcattyBridge.get; + (netcattyBridge as { get: () => unknown }).get = () => ({ + openSftp: async (options: NetcattySSHOptions) => { + seen.push(options); + return "pooled-sftp"; + }, + openSftpForSession: async () => { + openForSessionCalls += 1; + return "unexpected-terminal-sftp"; + }, + }); + try { + const id = await openTransferSftpSession( + host, + { hosts: [host], keys: [], identities: [] }, + { dedicated: false }, + ); + assert.equal(id, "pooled-sftp"); + assert.equal(openForSessionCalls, 0); + assert.equal(seen.length, 1); + assert.notEqual(seen[0]?.reuseTransport, false); + } finally { + (netcattyBridge as { get: typeof restore }).get = restore; + resetDedicatedSessionOpenGateForTests(); + } +}); + +test("dedicated transfer delegates key and password fallback to one main-process open", async () => { + resetDedicatedSessionOpenGateForTests(); + const mixedHost = { + ...host, + authMethod: "auto", + identityFilePaths: ["/tmp/id_ed25519"], + password: "fallback-password", + } as Host; + const seen: NetcattySSHOptions[] = []; + const { netcattyBridge } = await import("../../../infrastructure/services/netcattyBridge.ts"); + const restore = netcattyBridge.get; + (netcattyBridge as { get: () => unknown }).get = () => ({ + openSftp: async (options: NetcattySSHOptions) => { + seen.push(options); + throw new Error("All configured authentication methods failed"); + }, + }); + try { + await assert.rejects( + openTransferSftpSession(mixedHost, { hosts: [mixedHost], keys: [], identities: [] }), + /authentication/i, + ); + assert.equal(seen.length, 1); + assert.equal(seen[0]?.password, "fallback-password"); + assert.deepEqual(seen[0]?.identityFilePaths, ["/tmp/id_ed25519"]); } finally { (netcattyBridge as { get: typeof restore }).get = restore; resetDedicatedSessionOpenGateForTests(); diff --git a/application/state/sftp/sharedRemoteHostCache.test.ts b/application/state/sftp/sharedRemoteHostCache.test.ts new file mode 100644 index 000000000..c01335872 --- /dev/null +++ b/application/state/sftp/sharedRemoteHostCache.test.ts @@ -0,0 +1,57 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { + MAX_SHARED_REMOTE_HOST_CACHE_ENTRIES, + MAX_SHARED_REMOTE_HOST_CACHE_FILES, + _getSharedRemoteHostCacheStatsForTests, + _resetSharedRemoteHostCacheForTests, + getSharedRemoteHostCache, + setSharedRemoteHostCache, +} from "./sharedRemoteHostCache"; + +const files = (count: number) => Array.from({ length: count }, (_value, index) => ({ + name: `file-${index}`, +})) as never[]; + +const entry = (fileCount: number) => ({ + path: "/", + homeDir: "/", + files: files(fileCount), + filenameEncoding: "utf-8" as const, +}); + +test("shared host cache is bounded across many different hosts", () => { + _resetSharedRemoteHostCacheForTests(); + for (let index = 0; index < MAX_SHARED_REMOTE_HOST_CACHE_ENTRIES + 20; index += 1) { + setSharedRemoteHostCache(`host-${index}`, entry(1)); + } + + assert.equal(_getSharedRemoteHostCacheStatsForTests().entries, MAX_SHARED_REMOTE_HOST_CACHE_ENTRIES); + assert.equal(getSharedRemoteHostCache("host-0"), null); + assert.ok(getSharedRemoteHostCache(`host-${MAX_SHARED_REMOTE_HOST_CACHE_ENTRIES + 19}`)); +}); + +test("shared host cache is bounded by retained file rows", () => { + _resetSharedRemoteHostCacheForTests(); + const perHost = Math.floor(MAX_SHARED_REMOTE_HOST_CACHE_FILES * 0.6); + setSharedRemoteHostCache("large-a", entry(perHost)); + setSharedRemoteHostCache("large-b", entry(perHost)); + + assert.deepEqual(_getSharedRemoteHostCacheStatsForTests(), { + entries: 1, + files: perHost, + }); + assert.equal(getSharedRemoteHostCache("large-a"), null); + assert.ok(getSharedRemoteHostCache("large-b")); +}); + +test("shared host cache skips one listing larger than the global file budget", () => { + _resetSharedRemoteHostCacheForTests(); + setSharedRemoteHostCache("oversized", entry(MAX_SHARED_REMOTE_HOST_CACHE_FILES + 1)); + + assert.deepEqual(_getSharedRemoteHostCacheStatsForTests(), { + entries: 0, + files: 0, + }); + assert.equal(getSharedRemoteHostCache("oversized"), null); +}); diff --git a/application/state/sftp/sharedRemoteHostCache.ts b/application/state/sftp/sharedRemoteHostCache.ts index 8f7df9aa5..1d4a54f23 100644 --- a/application/state/sftp/sharedRemoteHostCache.ts +++ b/application/state/sftp/sharedRemoteHostCache.ts @@ -9,9 +9,35 @@ export interface SharedRemoteHostCacheEntry { } const SHARED_REMOTE_HOST_CACHE_TTL_MS = 60_000; +export const MAX_SHARED_REMOTE_HOST_CACHE_ENTRIES = 64; +export const MAX_SHARED_REMOTE_HOST_CACHE_FILES = 20_000; const sharedRemoteHostCache = new Map(); +const pruneSharedRemoteHostCache = (now: number): void => { + for (const [key, entry] of sharedRemoteHostCache) { + if (now - entry.updatedAt > SHARED_REMOTE_HOST_CACHE_TTL_MS) { + sharedRemoteHostCache.delete(key); + } + } + + let totalFiles = 0; + for (const entry of sharedRemoteHostCache.values()) totalFiles += entry.files.length; + while ( + sharedRemoteHostCache.size > 1 + && ( + sharedRemoteHostCache.size > MAX_SHARED_REMOTE_HOST_CACHE_ENTRIES + || totalFiles > MAX_SHARED_REMOTE_HOST_CACHE_FILES + ) + ) { + const oldestKey = sharedRemoteHostCache.keys().next().value as string | undefined; + if (!oldestKey) break; + const oldest = sharedRemoteHostCache.get(oldestKey); + sharedRemoteHostCache.delete(oldestKey); + totalFiles -= oldest?.files.length ?? 0; + } +}; + /** * Build a cache key that includes connection details so that the same host ID * with different session-time overrides (port, protocol) uses separate entries. @@ -32,14 +58,11 @@ export const buildCacheKey = ( export const getSharedRemoteHostCache = ( cacheKey: string, ): SharedRemoteHostCacheEntry | null => { + pruneSharedRemoteHostCache(Date.now()); const entry = sharedRemoteHostCache.get(cacheKey); if (!entry) return null; - - if (Date.now() - entry.updatedAt > SHARED_REMOTE_HOST_CACHE_TTL_MS) { - sharedRemoteHostCache.delete(cacheKey); - return null; - } - + sharedRemoteHostCache.delete(cacheKey); + sharedRemoteHostCache.set(cacheKey, entry); return entry; }; @@ -47,8 +70,25 @@ export const setSharedRemoteHostCache = ( cacheKey: string, entry: Omit, ): void => { + const now = Date.now(); + pruneSharedRemoteHostCache(now); + sharedRemoteHostCache.delete(cacheKey); + // Never exceed the advertised file-row budget with a single huge listing. + // Dropping it from cache preserves the full result for this read without + // retaining an unbounded array for the lifetime of the renderer. + if (entry.files.length > MAX_SHARED_REMOTE_HOST_CACHE_FILES) return; sharedRemoteHostCache.set(cacheKey, { ...entry, - updatedAt: Date.now(), + updatedAt: now, }); + pruneSharedRemoteHostCache(now); }; + +export const _resetSharedRemoteHostCacheForTests = (): void => { + sharedRemoteHostCache.clear(); +}; + +export const _getSharedRemoteHostCacheStatsForTests = () => ({ + entries: sharedRemoteHostCache.size, + files: [...sharedRemoteHostCache.values()].reduce((sum, entry) => sum + entry.files.length, 0), +}); diff --git a/application/state/sftp/transferCancelLatch.test.ts b/application/state/sftp/transferCancelLatch.test.ts index 2dd7cdd7f..47abf08af 100644 --- a/application/state/sftp/transferCancelLatch.test.ts +++ b/application/state/sftp/transferCancelLatch.test.ts @@ -5,8 +5,10 @@ import { clearTransferCancelledTree, isTransferCancelledFlag, isTransferOrRootCancelled, + markTransferCancelled, markTransferCancelledTree, resetTransferCancelLatchesForTests, + settleTransferCancelTree, } from "./transferCancelLatch"; test("cancel flags are process-global for root and children", () => { @@ -19,3 +21,46 @@ test("cancel flags are process-global for root and children", () => { assert.equal(isTransferCancelledFlag("parent"), false); assert.equal(isTransferCancelledFlag("c1"), false); }); + +test("settled single-file cancellations do not accumulate across a large batch", () => { + resetTransferCancelLatchesForTests(); + const taskIds = Array.from({ length: 4_000 }, (_, index) => `single-${index}`); + + for (const taskId of taskIds) { + markTransferCancelled(taskId); + assert.equal(isTransferCancelledFlag(taskId), true); + settleTransferCancelTree(taskId); + } + + for (const taskId of taskIds) { + assert.equal(isTransferCancelledFlag(taskId), false); + } +}); + +test("settling a directory root releases every child recorded by tree cancellation", () => { + resetTransferCancelLatchesForTests(); + const childIds = Array.from({ length: 4_000 }, (_, index) => `directory-child-${index}`); + markTransferCancelledTree("directory-root", childIds); + + settleTransferCancelTree("directory-root"); + + assert.equal(isTransferCancelledFlag("directory-root"), false); + for (const childId of childIds) { + assert.equal(isTransferCancelledFlag(childId), false); + } +}); + +test("same-id resume clears the old tree without masking a later cancellation", () => { + resetTransferCancelLatchesForTests(); + markTransferCancelledTree("same-root", ["old-child"]); + clearTransferCancelledTree("same-root"); + + markTransferCancelledTree("same-root", ["new-child"]); + + assert.equal(isTransferCancelledFlag("old-child"), false); + assert.equal(isTransferCancelledFlag("same-root"), true); + assert.equal(isTransferCancelledFlag("new-child"), true); + settleTransferCancelTree("same-root"); + assert.equal(isTransferCancelledFlag("same-root"), false); + assert.equal(isTransferCancelledFlag("new-child"), false); +}); diff --git a/application/state/sftp/transferCancelLatch.ts b/application/state/sftp/transferCancelLatch.ts index 144a792d6..907923584 100644 --- a/application/state/sftp/transferCancelLatch.ts +++ b/application/state/sftp/transferCancelLatch.ts @@ -6,6 +6,7 @@ */ const cancelledIds = new Set(); +const childIdsByRoot = new Map>(); export function markTransferCancelled(taskId: string): void { cancelledIds.add(taskId); @@ -13,7 +14,13 @@ export function markTransferCancelled(taskId: string): void { export function markTransferCancelledTree(rootTaskId: string, childIds: readonly string[] = []): void { cancelledIds.add(rootTaskId); - for (const id of childIds) cancelledIds.add(id); + if (childIds.length === 0) return; + const recordedChildren = childIdsByRoot.get(rootTaskId) ?? new Set(); + for (const id of childIds) { + cancelledIds.add(id); + recordedChildren.add(id); + } + childIdsByRoot.set(rootTaskId, recordedChildren); } export function clearTransferCancelled(taskId: string): void { @@ -22,7 +29,26 @@ export function clearTransferCancelled(taskId: string): void { export function clearTransferCancelledTree(rootTaskId: string, childIds: readonly string[] = []): void { cancelledIds.delete(rootTaskId); + for (const id of childIdsByRoot.get(rootTaskId) ?? []) cancelledIds.delete(id); for (const id of childIds) cancelledIds.delete(id); + childIdsByRoot.delete(rootTaskId); +} + +/** + * Release cancellation state after the transfer has fully settled. + * Returns every related child id, including children no longer present in UI + * history, so sibling control state can be released from the same tree. + */ +export function settleTransferCancelTree( + rootTaskId: string, + childIds: readonly string[] = [], +): string[] { + const relatedChildIds = [...new Set([ + ...(childIdsByRoot.get(rootTaskId) ?? []), + ...childIds, + ])]; + clearTransferCancelledTree(rootTaskId, relatedChildIds); + return relatedChildIds; } export function isTransferCancelledFlag(taskId: string): boolean { @@ -36,4 +62,5 @@ export function isTransferOrRootCancelled(rootTaskId: string, taskId?: string): /** Test helper. */ export function resetTransferCancelLatchesForTests(): void { cancelledIds.clear(); + childIdsByRoot.clear(); } diff --git a/application/state/sftp/transferConflictLifecycle.test.ts b/application/state/sftp/transferConflictLifecycle.test.ts new file mode 100644 index 000000000..4475ab314 --- /dev/null +++ b/application/state/sftp/transferConflictLifecycle.test.ts @@ -0,0 +1,117 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +import type { FileConflictAction, TransferTask } from "../../../domain/models"; +import { + captureDeferredTransferAttempt, + createDeferredTransferAttemptQueue, + isDeferredTransferAttemptCurrent, + pruneTransferConflictDefaults, + type TransferConflictDefaults, +} from "./transferConflictLifecycle"; + +const task = (id: string, batchId: string, status: TransferTask["status"]): TransferTask => ({ + id, + batchId, + fileName: id, + sourcePath: `/source/${id}`, + targetPath: `/target/${id}`, + sourceConnectionId: "source", + targetConnectionId: "target", + direction: "remote-to-remote", + status, + totalBytes: 1, + transferredBytes: 0, + speed: 0, + startTime: 1, + isDirectory: false, +}); + +test("terminal batches release their apply-to-all conflict defaults", () => { + const defaults: TransferConflictDefaults = new Map>([ + ["live", new Map([["file:file", "replace"]])], + ["finished", new Map([["file:file", "skip"]])], + ]); + + pruneTransferConflictDefaults(defaults, [ + task("live-task", "live", "transferring"), + task("finished-task", "finished", "completed"), + ]); + + assert.deepEqual([...defaults.keys()], ["live"]); +}); + +test("deferred conflict attempts are cancelled on owner unmount", async () => { + const timers = new Map void>(); + let nextTimerId = 0; + let calls = 0; + const queue = createDeferredTransferAttemptQueue({ + setTimeoutFn(callback) { + const id = ++nextTimerId; + timers.set(id, callback); + return id; + }, + clearTimeoutFn(id) { + timers.delete(id as number); + }, + }); + + queue.schedule("task-a", 100, () => true, async () => { calls += 1; }); + queue.schedule("task-b", 100, () => true, async () => { calls += 1; }); + assert.equal(queue.size, 2); + + queue.dispose(); + for (const callback of timers.values()) callback(); + await Promise.resolve(); + + assert.equal(calls, 0); + assert.equal(queue.size, 0); + assert.equal(timers.size, 0); +}); + +test("deferred conflict attempts revalidate the current task before starting", async () => { + const timers: Array<() => void> = []; + let stillCurrent = true; + let calls = 0; + const queue = createDeferredTransferAttemptQueue({ + setTimeoutFn(callback) { + timers.push(callback); + return timers.length; + }, + clearTimeoutFn() {}, + }); + + queue.schedule("task-a", 100, () => stillCurrent, async () => { calls += 1; }); + stillCurrent = false; + timers[0]?.(); + await Promise.resolve(); + + assert.equal(calls, 0); + assert.equal(queue.size, 0); +}); + +test("deferred conflict identity rejects owner, endpoint, and task lifecycle changes", () => { + const current = { ...task("task-a", "batch-a", "pending"), ownerId: "owner-a" }; + const connectionKeys = new Map([ + ["source", "source-generation-1"], + ["target", "target-generation-1"], + ]); + const identity = captureDeferredTransferAttempt(current, "owner-a", connectionKeys); + + assert.equal(isDeferredTransferAttemptCurrent(current, identity, connectionKeys), true); + assert.equal(isDeferredTransferAttemptCurrent( + { ...current, ownerId: "owner-b" }, + identity, + connectionKeys, + ), false); + assert.equal(isDeferredTransferAttemptCurrent( + current, + identity, + new Map(connectionKeys).set("target", "target-generation-2"), + ), false); + assert.equal(isDeferredTransferAttemptCurrent( + { ...current, status: "cancelled" }, + identity, + connectionKeys, + ), false); +}); diff --git a/application/state/sftp/transferConflictLifecycle.ts b/application/state/sftp/transferConflictLifecycle.ts new file mode 100644 index 000000000..1527f115a --- /dev/null +++ b/application/state/sftp/transferConflictLifecycle.ts @@ -0,0 +1,131 @@ +import type { FileConflictAction, TransferTask } from "../../../domain/models"; + +export type TransferConflictDefaults = Map>; + +export interface DeferredTransferAttemptIdentity { + taskId: string; + ownerId: string; + sourceConnectionId: string; + targetConnectionId: string; + sourceConnectionKey?: string; + targetConnectionKey?: string; + sourcePath: string; + targetPath: string; +} + +export function captureDeferredTransferAttempt( + task: Pick, + ownerId: string, + connectionKeys: ReadonlyMap, +): DeferredTransferAttemptIdentity { + return { + taskId: task.id, + ownerId, + sourceConnectionId: task.sourceConnectionId, + targetConnectionId: task.targetConnectionId, + sourceConnectionKey: connectionKeys.get(task.sourceConnectionId), + targetConnectionKey: connectionKeys.get(task.targetConnectionId), + sourcePath: task.sourcePath, + targetPath: task.targetPath, + }; +} + +export function isDeferredTransferAttemptCurrent( + task: Pick | null | undefined, + identity: DeferredTransferAttemptIdentity, + connectionKeys: ReadonlyMap, +): boolean { + return !!task + && task.id === identity.taskId + && task.ownerId === identity.ownerId + && task.status === "pending" + && task.sourceConnectionId === identity.sourceConnectionId + && task.targetConnectionId === identity.targetConnectionId + && task.sourcePath === identity.sourcePath + && task.targetPath === identity.targetPath + && connectionKeys.get(task.sourceConnectionId) === identity.sourceConnectionKey + && connectionKeys.get(task.targetConnectionId) === identity.targetConnectionKey; +} + +const TERMINAL_TRANSFER_STATUSES = new Set([ + "completed", + "failed", + "cancelled", +]); + +/** Keep apply-to-all choices only while their transfer batch still has live work. */ +export function pruneTransferConflictDefaults( + defaults: TransferConflictDefaults, + tasks: readonly Pick[], +): void { + const liveBatchIds = new Set(); + for (const task of tasks) { + if (task.batchId && !TERMINAL_TRANSFER_STATUSES.has(task.status)) { + liveBatchIds.add(task.batchId); + } + } + for (const batchId of defaults.keys()) { + // Legacy/adopted rows without a batch share a fixed, tiny conflict-type set. + if (batchId !== "global" && !liveBatchIds.has(batchId)) defaults.delete(batchId); + } +} + +type TimerHandle = ReturnType | number; + +export interface DeferredTransferAttemptQueue { + readonly size: number; + schedule( + taskId: string, + delayMs: number, + isCurrent: () => boolean, + run: () => void | Promise, + ): void; + cancel(taskId: string): void; + dispose(): void; +} + +export function createDeferredTransferAttemptQueue(options: { + setTimeoutFn?: (callback: () => void, delayMs: number) => TimerHandle; + clearTimeoutFn?: (handle: TimerHandle) => void; + onError?: (error: unknown) => void; +} = {}): DeferredTransferAttemptQueue { + const setTimeoutFn = options.setTimeoutFn ?? ((callback, delayMs) => setTimeout(callback, delayMs)); + const clearTimeoutFn = options.clearTimeoutFn ?? ((handle) => clearTimeout(handle)); + const entries = new Map(); + let disposed = false; + + const cancel = (taskId: string) => { + const entry = entries.get(taskId); + if (!entry) return; + entries.delete(taskId); + clearTimeoutFn(entry.handle); + }; + + return { + get size() { + return entries.size; + }, + schedule(taskId, delayMs, isCurrent, run) { + if (disposed || !taskId) return; + cancel(taskId); + const token = {}; + const handle = setTimeoutFn(() => { + const entry = entries.get(taskId); + if (!entry || entry.token !== token) return; + entries.delete(taskId); + if (disposed || !isCurrent()) return; + void Promise.resolve() + .then(run) + .catch((error) => options.onError?.(error)); + }, Math.max(0, delayMs)); + entries.set(taskId, { handle, token }); + }, + cancel, + dispose() { + if (disposed) return; + disposed = true; + for (const entry of entries.values()) clearTimeoutFn(entry.handle); + entries.clear(); + }, + }; +} diff --git a/application/state/sftp/transferConnectionPool.test.ts b/application/state/sftp/transferConnectionPool.test.ts index 3986b6a92..6aec55726 100644 --- a/application/state/sftp/transferConnectionPool.test.ts +++ b/application/state/sftp/transferConnectionPool.test.ts @@ -4,24 +4,137 @@ import test from "node:test"; import { buildTransferPoolKey, createTransferConnectionPool, + createTransferPoolKeyCache, + DEFAULT_MAX_IDLE_TRANSFER_CONNECTIONS, DEFAULT_TRANSFER_CONNECTIONS_PER_HOST, + DEFAULT_TRANSFER_CONNECTION_IDLE_TTL_MS, } from "./transferConnectionPool.ts"; -test("buildTransferPoolKey includes endpoint when hostname is known", () => { +function createFakeClock() { + let current = 0; + let nextId = 1; + const timers = new Map void; deadline: number }>(); + return { + now: () => current, + setTimeoutFn(callback: () => void, delayMs: number) { + const id = nextId; + nextId += 1; + timers.set(id, { callback, deadline: current + Math.max(0, delayMs) }); + return id; + }, + clearTimeoutFn(id: unknown) { + timers.delete(id as number); + }, + advance(ms: number) { + current += ms; + for (;;) { + const due = [...timers.entries()] + .filter(([, timer]) => timer.deadline <= current) + .sort((left, right) => left[1].deadline - right[1].deadline)[0]; + if (!due) break; + timers.delete(due[0]); + due[1].callback(); + } + }, + }; +} + +test("transfer pool key cache resolves a host identity only once and retries failures", async () => { + let builds = 0; + let inputBuilds = 0; + let failFirst = true; + const cache = createTransferPoolKeyCache(async (input) => { + builds += 1; + if (failFirst) { + failFirst = false; + throw new Error("temporary identity failure"); + } + return `key:${input.hostId}`; + }); + const host = {}; + const createInput = () => { + inputBuilds += 1; + return { hostId: "h1" }; + }; + + await assert.rejects(cache.get(host, createInput), /temporary identity failure/); + assert.equal(await cache.get(host, createInput), "key:h1"); + assert.equal(await cache.get(host, createInput), "key:h1"); + assert.equal(builds, 2, "a rejected identity must be evicted and rebuilt once"); + assert.equal(inputBuilds, 2, "cached calls must skip credential construction too"); + + assert.equal(await cache.get({}, createInput), "key:h1"); + assert.equal(builds, 3, "different host objects must not share identity promises"); +}); + +test("buildTransferPoolKey includes endpoint when hostname is known", async () => { assert.equal( - buildTransferPoolKey({ hostId: "h1", hostname: "vault.example", port: 22, username: "root" }), + await buildTransferPoolKey({ hostId: "h1", hostname: "vault.example", port: 22, username: "root" }), "host:h1|ep:vault.example:22:root:ssh:nosudo", ); // Same hostId with session override must not share the vault pool key. assert.equal( - buildTransferPoolKey({ hostId: "h1", hostname: "override.example", port: 2222, username: "ubuntu" }), + await buildTransferPoolKey({ hostId: "h1", hostname: "override.example", port: 2222, username: "ubuntu" }), "host:h1|ep:override.example:2222:ubuntu:ssh:nosudo", ); assert.equal( - buildTransferPoolKey({ hostname: "ci.example", port: 22, username: "root" }), + await buildTransferPoolKey({ hostname: "ci.example", port: 22, username: "root" }), "ep:ci.example:22:root:ssh:nosudo", ); - assert.equal(buildTransferPoolKey({ hostId: "h1" }), "host:h1"); + assert.equal(await buildTransferPoolKey({ hostId: "h1" }), "host:h1"); +}); + +test("buildTransferPoolKey separates every transport security identity", async () => { + const connectionOptions = { + hostname: "target.example", + port: 22, + username: "root", + password: "secret-a", + keepaliveInterval: 30, + algorithmOverrides: { kex: ["curve25519-sha256"] }, + jumpHosts: [{ hostname: "jump-a", port: 22, username: "jump" }], + verifyHostKeys: true, + knownHosts: [{ + id: "known-a", + hostname: "target.example", + port: 22, + keyType: "ssh-ed25519", + publicKey: "AAAA", + fingerprint: "a", + discoveredAt: 0, + }], + } as unknown as NetcattySSHOptions; + const base = { + hostId: "h1", + hostname: "target.example", + port: 22, + username: "root", + connectionOptions, + }; + const key = await buildTransferPoolKey(base); + const variant = (overrides: Partial): NetcattySSHOptions => ({ + ...connectionOptions, + ...overrides, + }); + const variants: NetcattySSHOptions[] = [ + variant({ password: "secret-b" }), + variant({ keepaliveInterval: 60 }), + variant({ algorithmOverrides: { kex: ["diffie-hellman-group14-sha256"] } }), + variant({ jumpHosts: [{ hostname: "jump-b", port: 22, username: "jump" }] }), + variant({ verifyHostKeys: false }), + variant({ knownHosts: [{ + id: "known-b", + hostname: "target.example", + port: 22, + keyType: "ssh-ed25519", + publicKey: "BBBB", + fingerprint: "b", + discoveredAt: 0, + }] }), + ]; + for (const connectionOptions of variants) { + assert.notEqual(await buildTransferPoolKey({ ...base, connectionOptions }), key); + } }); test("pool opens at most maxPerHost channels and multiplexes when busy", async () => { @@ -51,14 +164,15 @@ test("pool opens at most maxPerHost channels and multiplexes when busy", async ( b.release(); c.release(); - // Last holder closes each channel immediately (SSH park owns keep-alive). - assert.equal(closed.length, 2); - assert.equal(pool.getStats("host:a").connections, 0); + assert.equal(closed.length, 0); + assert.equal(pool.getStats("host:a").connections, 2); + assert.equal(pool.getStats("host:a").idle, 2); const d = await pool.acquire("host:a", "t4", open); - assert.equal(opens, 3); + assert.equal(opens, 2, "the next small file must reuse a short-idle channel"); d.release(); - assert.equal(closed.length, 3); + await pool.closeAll(); + assert.equal(closed.length, 2); }); test("different hosts get independent channel pools", async () => { @@ -77,24 +191,86 @@ test("different hosts get independent channel pools", async () => { b.release(); }); -test("release of last holder closes the channel without waiting for idle TTL", async () => { +test("failed opens do not retain empty host pool keys", async () => { + const pool = createTransferConnectionPool({ maxPerHost: 1 }); + + for (let index = 0; index < 100; index += 1) { + await assert.rejects( + pool.acquire(`host:failed-${index}`, `transfer-${index}`, async () => { + throw new Error("connection failed"); + }), + /connection failed/, + ); + } + + assert.equal(pool.getStats().poolKeys, 0); + assert.equal(pool.getStats().pendingOpenLocks, 0); +}); + +test("sequential small files reuse one channel until the short idle TTL expires", async () => { + const clock = createFakeClock(); const closed: string[] = []; + let opens = 0; const pool = createTransferConnectionPool({ - maxPerHost: 2, + maxPerHost: 1, + idleTtlMs: 5_000, closeSession: async (id) => { closed.push(id); }, + now: clock.now, + setTimeoutFn: clock.setTimeoutFn, + clearTimeoutFn: clock.clearTimeoutFn, }); - const open = async () => "sftp-busy"; - const lease = await pool.acquire("host:x", "t1", open); - assert.equal(await pool.closeIdle(), 0, "busy slot must not close while held"); - lease.release(); - assert.equal(closed.length, 1); + const open = async () => `sftp-${++opens}`; + + const first = await pool.acquire("host:x", "file-1", open); + first.release(); + assert.equal(pool.getStats("host:x").idle, 1); + assert.deepEqual(closed, []); + + for (let index = 2; index <= 100; index += 1) { + const next = await pool.acquire("host:x", `file-${index}`, open); + assert.equal(next.sftpId, first.sftpId); + next.release(); + } + assert.equal(opens, 1); + + clock.advance(4_999); + assert.deepEqual(closed, []); + clock.advance(1); + assert.deepEqual(closed, ["sftp-1"]); assert.equal(pool.getStats("host:x").connections, 0); - // closeIdle is a defensive sweep; nothing left. - assert.equal(await pool.closeIdle(), 0); }); test("default max per host is FileZilla-like (2)", () => { assert.equal(DEFAULT_TRANSFER_CONNECTIONS_PER_HOST, 2); + assert.equal(DEFAULT_TRANSFER_CONNECTION_IDLE_TTL_MS, 5_000); + assert.equal(DEFAULT_MAX_IDLE_TRANSFER_CONNECTIONS, 16); +}); + +test("global idle cap evicts the oldest host channel", async () => { + const clock = createFakeClock(); + const closed: string[] = []; + let opens = 0; + const pool = createTransferConnectionPool({ + maxPerHost: 1, + maxIdleConnections: 2, + idleTtlMs: 60_000, + closeSession: async (id) => { closed.push(id); }, + now: clock.now, + setTimeoutFn: clock.setTimeoutFn, + clearTimeoutFn: clock.clearTimeoutFn, + }); + const open = async () => `sftp-${++opens}`; + + for (const [index, hostKey] of ["host:a", "host:b", "host:c"].entries()) { + const lease = await pool.acquire(hostKey, `file-${index}`, open); + lease.release(); + clock.advance(1); + } + + assert.deepEqual(closed, ["sftp-1"]); + assert.equal(pool.getStats().connections, 2); + assert.equal(pool.getStats().idle, 2); + assert.equal(pool.getStats().poolKeys, 2); }); test("concurrent acquires do not exceed maxPerHost", async () => { @@ -125,6 +301,50 @@ test("concurrent acquires do not exceed maxPerHost", async () => { for (const lease of leases) lease.release(); }); +test("per-host open serialization state survives queued work and is released after the last waiter", async () => { + let markFirstStarted!: () => void; + let markSecondStarted!: () => void; + let finishFirst!: () => void; + let finishSecond!: () => void; + const firstStarted = new Promise((resolve) => { markFirstStarted = resolve; }); + const secondStarted = new Promise((resolve) => { markSecondStarted = resolve; }); + const firstFinished = new Promise((resolve) => { finishFirst = resolve; }); + const secondFinished = new Promise((resolve) => { finishSecond = resolve; }); + const pool = createTransferConnectionPool({ maxPerHost: 2 }); + let opens = 0; + const open = async () => { + opens += 1; + if (opens === 1) { + markFirstStarted(); + await firstFinished; + } else { + markSecondStarted(); + await secondFinished; + } + return `sftp-ephemeral-${opens}`; + }; + + const firstAcquiring = pool.acquire("host:ephemeral", "t1", open); + await firstStarted; + const secondAcquiring = pool.acquire("host:ephemeral", "t2", open); + assert.equal(pool.getStats("host:ephemeral").pendingOpenLocks, 1); + + finishFirst(); + const firstLease = await firstAcquiring; + await secondStarted; + assert.equal( + pool.getStats("host:ephemeral").pendingOpenLocks, + 1, + "the first opener must not clear a later waiter's serialization state", + ); + + finishSecond(); + const secondLease = await secondAcquiring; + assert.equal(pool.getStats("host:ephemeral").pendingOpenLocks, 0); + firstLease.release(); + secondLease.release(); +}); + test("busy first channel causes a second open (FileZilla style)", async () => { let opens = 0; const pool = createTransferConnectionPool({ maxPerHost: 2 }); @@ -145,6 +365,48 @@ test("busy first channel causes a second open (FileZilla style)", async () => { second.release(); }); +test("a replacement opened while the last old holder releases stays tracked and closable", async () => { + const closed: string[] = []; + const pool = createTransferConnectionPool({ + maxPerHost: 2, + idleTtlMs: 0, + closeSession: async (id) => { closed.push(id); }, + }); + const first = await pool.acquire("host:release-during-open", "t1", async () => "sftp-old"); + + let finishReplacementOpen!: () => void; + const replacementOpenGate = new Promise((resolve) => { + finishReplacementOpen = resolve; + }); + let replacementOpenStarted!: () => void; + const replacementStarted = new Promise((resolve) => { + replacementOpenStarted = resolve; + }); + const replacementPromise = pool.acquire( + "host:release-during-open", + "t2", + async () => { + replacementOpenStarted(); + await replacementOpenGate; + return "sftp-new"; + }, + ); + await replacementStarted; + + first.release(); + finishReplacementOpen(); + const replacement = await replacementPromise; + + assert.deepEqual(closed, ["sftp-old"]); + assert.equal(pool.getStats("host:release-during-open").connections, 1); + assert.equal(pool.getStats("host:release-during-open").holders, 1); + + replacement.release(); + assert.deepEqual(closed, ["sftp-old", "sftp-new"]); + assert.equal(pool.getStats().connections, 0); + assert.equal(pool.getStats().poolKeys, 0); +}); + test("discard removes a dead session so next acquire reopens", async () => { let opens = 0; const closed: string[] = []; @@ -169,13 +431,49 @@ test("discard removes a dead session so next acquire reopens", async () => { b.release(); }); -test("closeIdle detaches leftover idle slots before closing", async () => { +test("cancellation returns a healthy channel, while a session error discards it", async () => { + const clock = createFakeClock(); + const closed: string[] = []; + let opens = 0; + const pool = createTransferConnectionPool({ + maxPerHost: 1, + idleTtlMs: 5_000, + closeSession: async (id) => { closed.push(id); }, + now: clock.now, + setTimeoutFn: clock.setTimeoutFn, + clearTimeoutFn: clock.clearTimeoutFn, + }); + const open = async () => `sftp-${++opens}`; + + const cancelled = await pool.acquire("host:a", "cancelled-transfer", open); + cancelled.release(); + assert.equal(pool.getStats("host:a").idle, 1); + assert.deepEqual(closed, []); + + const next = await pool.acquire("host:a", "next-transfer", open); + assert.equal(next.sftpId, cancelled.sftpId); + assert.equal(opens, 1); + next.discard(); + assert.deepEqual(closed, ["sftp-1"]); + assert.equal(pool.getStats("host:a").connections, 0); + + const recovered = await pool.acquire("host:a", "recovered-transfer", open); + assert.equal(recovered.sftpId, "sftp-2"); + recovered.release(); + clock.advance(5_000); + assert.deepEqual(closed, ["sftp-1", "sftp-2"]); +}); + +test("closeIdle detaches only expired idle slots before awaiting close", async () => { let opens = 0; let closeStarted = 0; + let now = 0; let releaseClose!: () => void; const closeGate = new Promise((resolve) => { releaseClose = resolve; }); const pool = createTransferConnectionPool({ maxPerHost: 1, + idleTtlMs: 100, + now: () => now, closeSession: async () => { closeStarted += 1; await closeGate; @@ -186,20 +484,35 @@ test("closeIdle detaches leftover idle slots before closing", async () => { return `sftp-${opens}`; }; - // Manually inject an idle leftover by discarding holders via internal release - // path: acquire then release closes immediately, so inject by not releasing - // and using closeIdle only works if holders===0. Force via release. const a = await pool.acquire("host:a", "t1", open); assert.equal(opens, 1); - // Release closes async; wait a tick then ensure pool empty. a.release(); - await new Promise((r) => setTimeout(r, 0)); - assert.ok(closeStarted >= 1); + assert.equal(await pool.closeIdle(now), 0); + now = 100; + const closing = pool.closeIdle(now); + assert.equal(closeStarted, 1); + assert.equal(pool.getStats().connections, 0, "expired slot must detach before close awaits"); releaseClose(); + assert.equal(await closing, 1); }); -test("setIdleTtlMs is a no-op for unified transport park", () => { - const pool = createTransferConnectionPool(); - pool.setIdleTtlMs(60_000); - assert.equal(pool.getIdleTtlMs(), 0); +test("setIdleTtlMs reschedules parked channels and zero closes them immediately", async () => { + const clock = createFakeClock(); + const closed: string[] = []; + const pool = createTransferConnectionPool({ + idleTtlMs: 60_000, + closeSession: async (id) => { closed.push(id); }, + now: clock.now, + setTimeoutFn: clock.setTimeoutFn, + clearTimeoutFn: clock.clearTimeoutFn, + }); + const lease = await pool.acquire("host:a", "t1", async () => "sftp-a"); + lease.release(); + pool.setIdleTtlMs(5_000); + assert.equal(pool.getIdleTtlMs(), 5_000); + clock.advance(4_999); + assert.deepEqual(closed, []); + pool.setIdleTtlMs(0); + assert.deepEqual(closed, ["sftp-a"]); + assert.equal(pool.getStats().connections, 0); }); diff --git a/application/state/sftp/transferConnectionPool.ts b/application/state/sftp/transferConnectionPool.ts index 219554243..4f40ba3d8 100644 --- a/application/state/sftp/transferConnectionPool.ts +++ b/application/state/sftp/transferConnectionPool.ts @@ -5,17 +5,19 @@ * limits how many SFTP channels (sftpIds) may be open per host for parallel * transfers, and reuses a busy-vs-idle slot within that cap. * - * When the last transfer releases a channel it is closed immediately so the - * transport lease can return and the unified SSH idle park can reclaim the - * connection. SSH keep-alive is controlled only by ssh transport idle TTL. + * A released channel stays available for a short bounded idle window so a + * directory of sequential small files does not reopen one SFTP channel per + * file. The global idle cap prevents many visited hosts from retaining an + * unbounded number of channels; SSH transports remain owned by the unified + * main-process registry. */ export const DEFAULT_TRANSFER_CONNECTIONS_PER_HOST = 2; export const MIN_TRANSFER_CONNECTIONS_PER_HOST = 1; export const MAX_TRANSFER_CONNECTIONS_PER_HOST = 4; -/** @deprecated Idle TTL moved to SSH transport registry; kept for import stability. */ -export const DEFAULT_TRANSFER_CONNECTION_IDLE_TTL_MS = 0; +export const DEFAULT_TRANSFER_CONNECTION_IDLE_TTL_MS = 5_000; +export const DEFAULT_MAX_IDLE_TRANSFER_CONNECTIONS = 16; export type TransferPoolOpenFn = (poolKey: string) => Promise; export type TransferPoolCloseFn = (sftpId: string) => void | Promise; @@ -23,7 +25,7 @@ export type TransferPoolCloseFn = (sftpId: string) => void | Promise; export interface TransferConnectionLease { sftpId: string; poolKey: string; - /** Drop the holder count; last holder closes the channel. */ + /** Drop the holder count; last holder enters the short idle park. */ release: () => void; /** Drop holder, remove from pool, and close — use when the session is dead. */ discard: () => void; @@ -33,6 +35,9 @@ interface PoolSlot { sftpId: string; holders: Set; lastUsedAt: number; + idleSince?: number; + idleOrder?: number; + idleTimer?: unknown; /** Session is dead; do not hand out to new transfers. Close when idle. */ unhealthy?: boolean; opening?: Promise; @@ -40,10 +45,52 @@ interface PoolSlot { export interface TransferConnectionPoolOptions { maxPerHost?: number; - /** @deprecated Ignored — channels close when idle; SSH park owns keep-alive. */ idleTtlMs?: number; + /** Global cap across all host pools; oldest idle channels are evicted first. */ + maxIdleConnections?: number; closeSession?: TransferPoolCloseFn; now?: () => number; + /** Deterministic timer hooks for tests. */ + setTimeoutFn?: (callback: () => void, delayMs: number) => unknown; + clearTimeoutFn?: (handle: unknown) => void; +} + +export interface TransferPoolKeyInput { + hostId?: string; + hostname?: string; + port?: number; + username?: string; + protocol?: string; + sftpSudo?: boolean; + /** Full resolved transport identity; kept private inside the in-memory pool. */ + connectionOptions?: NetcattySSHOptions; +} + +export interface TransferPoolKeyCache { + get(host: object, createInput: () => TransferPoolKeyInput): Promise; +} + +/** + * Resolving a transfer identity includes credential expansion and SHA-256. + * Directory transfers may acquire a lease once per file, so cache that work + * by the immutable Host object for the lifetime of the current vault inputs. + */ +export function createTransferPoolKeyCache( + build: (input: TransferPoolKeyInput) => Promise = buildTransferPoolKey, +): TransferPoolKeyCache { + const cache = new WeakMap>(); + return { + get(host, createInput) { + const existing = cache.get(host); + if (existing) return existing; + const pending = Promise.resolve().then(() => build(createInput())); + cache.set(host, pending); + void pending.catch(() => { + if (cache.get(host) === pending) cache.delete(host); + }); + return pending; + }, + }; } export interface TransferConnectionPool { @@ -52,18 +99,18 @@ export interface TransferConnectionPool { /** Remove a dead session from the pool and close it (best-effort). */ discard(sftpId: string): void; getStats(poolKey?: string): { + poolKeys: number; connections: number; busy: number; idle: number; holders: number; + pendingOpenLocks: number; }; - /** Close every channel with zero holders (defensive sweep). */ + /** Close channels whose idle deadline has passed. */ closeIdle(now?: number): Promise; closeAll(): Promise; setMaxPerHost(max: number): void; - /** @deprecated No-op — transfer channels no longer park independently. */ setIdleTtlMs(ms: number): void; - /** @deprecated Always 0 (immediate reclaim of idle channels). */ getIdleTtlMs(): number; } @@ -75,12 +122,29 @@ function normalizeMaxPerHost(value: number | undefined): number { ); } +function normalizeIdleTtlMs(value: number | undefined): number { + if (value === undefined) return DEFAULT_TRANSFER_CONNECTION_IDLE_TTL_MS; + if (!Number.isFinite(value)) return DEFAULT_TRANSFER_CONNECTION_IDLE_TTL_MS; + return Math.max(0, Math.floor(value)); +} + +function normalizeMaxIdleConnections(value: number | undefined): number { + if (value === undefined) return DEFAULT_MAX_IDLE_TRANSFER_CONNECTIONS; + if (!Number.isFinite(value)) return DEFAULT_MAX_IDLE_TRANSFER_CONNECTIONS; + return Math.max(0, Math.floor(value)); +} + export function createTransferConnectionPool( options: TransferConnectionPoolOptions = {}, ): TransferConnectionPool { let maxPerHost = normalizeMaxPerHost(options.maxPerHost); + let idleTtlMs = normalizeIdleTtlMs(options.idleTtlMs); + const maxIdleConnections = normalizeMaxIdleConnections(options.maxIdleConnections); const closeSession = options.closeSession; const now = options.now ?? (() => Date.now()); + const setTimeoutFn = options.setTimeoutFn ?? ((callback, delayMs) => setTimeout(callback, delayMs)); + const clearTimeoutFn = options.clearTimeoutFn ?? ((handle) => clearTimeout(handle as ReturnType)); + let nextIdleOrder = 1; /** poolKey -> open slots for that host endpoint */ const pools = new Map(); @@ -101,21 +165,102 @@ export function createTransferConnectionPool( let releaseLock!: () => void; const gate = new Promise((resolve) => { releaseLock = resolve; }); // Chain waiters so concurrent acquires never exceed maxPerHost. - openLocks.set(poolKey, previous.catch(() => {}).then(() => gate)); + const tail = previous.catch(() => {}).then(() => gate); + openLocks.set(poolKey, tail); await previous.catch(() => {}); try { return await work(); } finally { releaseLock(); + // A later waiter replaces our tail. Only the last waiter may remove the + // per-host serialization entry, otherwise concurrent opens can overlap. + if (openLocks.get(poolKey) === tail) openLocks.delete(poolKey); + } + }; + + const clearIdleTimer = (slot: PoolSlot) => { + if (slot.idleTimer === undefined) return; + clearTimeoutFn(slot.idleTimer); + slot.idleTimer = undefined; + }; + + const detachSlot = (poolKey: string, list: PoolSlot[], idx: number): PoolSlot | undefined => { + const [slot] = list.splice(idx, 1); + if (slot) clearIdleTimer(slot); + if (list.length === 0) pools.delete(poolKey); + else pools.set(poolKey, list); + return slot; + }; + + const closeSessionBestEffort = (sftpId: string) => { + try { + void Promise.resolve(closeSession?.(sftpId)).catch(() => {}); + } catch { + // best-effort } }; const removeAndCloseSlot = (poolKey: string, list: PoolSlot[], idx: number) => { - const [slot] = list.splice(idx, 1); - if (list.length === 0) pools.delete(poolKey); - else pools.set(poolKey, list); + const slot = detachSlot(poolKey, list, idx); if (slot) { - void Promise.resolve(closeSession?.(slot.sftpId)).catch(() => {}); + closeSessionBestEffort(slot.sftpId); + } + }; + + const scheduleIdleClose = (poolKey: string, slot: PoolSlot, delayMs?: number) => { + clearIdleTimer(slot); + if (slot.holders.size > 0) return; + const list = pools.get(poolKey); + const idx = list?.indexOf(slot) ?? -1; + if (!list || idx < 0) return; + if (slot.unhealthy || idleTtlMs <= 0) { + removeAndCloseSlot(poolKey, list, idx); + return; + } + + const remaining = delayMs ?? Math.max(0, (slot.idleSince ?? now()) + idleTtlMs - now()); + if (remaining <= 0) { + removeAndCloseSlot(poolKey, list, idx); + return; + } + const handle = setTimeoutFn(() => { + slot.idleTimer = undefined; + const currentList = pools.get(poolKey); + const currentIdx = currentList?.indexOf(slot) ?? -1; + if (!currentList || currentIdx < 0 || slot.holders.size > 0) return; + const deadline = (slot.idleSince ?? slot.lastUsedAt) + idleTtlMs; + const remainingAtFire = deadline - now(); + if (remainingAtFire > 0) { + scheduleIdleClose(poolKey, slot, remainingAtFire); + return; + } + removeAndCloseSlot(poolKey, currentList, currentIdx); + }, remaining); + slot.idleTimer = handle; + const maybeTimer = handle as { unref?: () => void } | null; + maybeTimer?.unref?.(); + }; + + const enforceGlobalIdleCap = () => { + const idleSlots: Array<{ poolKey: string; slot: PoolSlot }> = []; + for (const [poolKey, list] of pools.entries()) { + for (const slot of list) { + if (slot.holders.size === 0) idleSlots.push({ poolKey, slot }); + } + } + idleSlots.sort((left, right) => { + if (left.slot.lastUsedAt !== right.slot.lastUsedAt) { + return left.slot.lastUsedAt - right.slot.lastUsedAt; + } + return (left.slot.idleOrder ?? 0) - (right.slot.idleOrder ?? 0); + }); + for (let index = 0; index < idleSlots.length - maxIdleConnections; index += 1) { + const candidate = idleSlots[index]!; + const currentList = pools.get(candidate.poolKey); + const currentIdx = currentList?.indexOf(candidate.slot) ?? -1; + if (currentList && currentIdx >= 0 && candidate.slot.holders.size === 0) { + removeAndCloseSlot(candidate.poolKey, currentList, currentIdx); + } } }; @@ -137,11 +282,18 @@ export function createTransferConnectionPool( const idx = list.findIndex((candidate) => candidate.sftpId === sftpId); if (idx < 0) return; const slot = list[idx]!; - slot.holders.delete(transferId); + if (!slot.holders.delete(transferId)) return; slot.lastUsedAt = now(); - // Last holder → close channel so the SSH transport lease can return. if (slot.holders.size === 0) { - removeAndCloseSlot(poolKey, list, idx); + if (slot.unhealthy) { + removeAndCloseSlot(poolKey, list, idx); + return; + } + slot.idleSince = slot.lastUsedAt; + slot.idleOrder = nextIdleOrder; + nextIdleOrder += 1; + scheduleIdleClose(poolKey, slot, idleTtlMs); + enforceGlobalIdleCap(); } }; @@ -190,22 +342,39 @@ export function createTransferConnectionPool( const existing = pickSlot(list); // Reuse when we already have max healthy connections, or when an idle one exists. // FileZilla-style: open a second connection only when the first is busy. - // Note: idle slots are rare now (release closes immediately) but still - // possible while a close is in-flight or under concurrent acquire. + // Idle slots are available during the short reuse window. if (existing && (existing.holders.size === 0 || healthyCount >= maxPerHost)) { + clearIdleTimer(existing); + existing.idleSince = undefined; + existing.idleOrder = undefined; existing.holders.add(transferId); existing.lastUsedAt = now(); return makeLease(poolKey, existing.sftpId, transferId); } if (healthyCount < maxPerHost) { - const sftpId = await open(poolKey); + let sftpId: string; + try { + sftpId = await open(poolKey); + } catch (error) { + // getList() installs the per-host array before opening. A failed + // connection must not leave one empty key behind forever in the + // process-wide pool (notably when many different hosts are tried). + if (list.length === 0 && pools.get(poolKey) === list) { + pools.delete(poolKey); + } + throw error; + } const slot: PoolSlot = { sftpId, holders: new Set([transferId]), lastUsedAt: now(), }; - list.push(slot); + // The last holder of the old list can release while open() is awaiting, + // which removes that empty list from `pools`. Re-read/recreate the + // canonical list before publishing the new slot; otherwise the lease is + // returned from a detached array and can never be released or closed. + getList(poolKey).push(slot); return makeLease(poolKey, sftpId, transferId); } @@ -222,19 +391,23 @@ export function createTransferConnectionPool( return makeLease(poolKey, sftpId, transferId); } fallback.holders.add(transferId); + clearIdleTimer(fallback); + fallback.idleSince = undefined; + fallback.idleOrder = undefined; fallback.lastUsedAt = now(); return makeLease(poolKey, fallback.sftpId, transferId); }); }; - const closeIdle = async (): Promise => { - // Defensive: release() already closes zero-holder slots. Sweep any leftover - // idle channels (e.g. after a failed close) so transport leases do not stick. + const closeIdle = async (sweepNow = now()): Promise => { const toClose: string[] = []; for (const [poolKey, list] of pools.entries()) { const kept: PoolSlot[] = []; for (const slot of list) { - if (slot.holders.size === 0) { + const expired = slot.holders.size === 0 + && (slot.unhealthy || idleTtlMs <= 0 || (slot.idleSince ?? slot.lastUsedAt) + idleTtlMs <= sweepNow); + if (expired) { + clearIdleTimer(slot); toClose.push(slot.sftpId); continue; } @@ -256,16 +429,16 @@ export function createTransferConnectionPool( }; const closeAll = async () => { - for (const list of pools.values()) { - for (const slot of list) { - try { - await closeSession?.(slot.sftpId); - } catch { - // best-effort - } + const toClose = [...pools.values()].flatMap((list) => list); + pools.clear(); + for (const slot of toClose) clearIdleTimer(slot); + for (const slot of toClose) { + try { + await closeSession?.(slot.sftpId); + } catch { + // best-effort } } - pools.clear(); }; const getStats = (poolKey?: string) => { @@ -282,7 +455,10 @@ export function createTransferConnectionPool( else idle += 1; } } - return { connections, busy, idle, holders }; + const pendingOpenLocks = poolKey + ? (openLocks.has(poolKey) ? 1 : 0) + : openLocks.size; + return { poolKeys: pools.size, connections, busy, idle, holders, pendingOpenLocks }; }; return { @@ -295,11 +471,22 @@ export function createTransferConnectionPool( setMaxPerHost(max: number) { maxPerHost = normalizeMaxPerHost(max); }, - setIdleTtlMs(_ms: number) { - // No-op: SSH transport registry owns connection keep-alive. + setIdleTtlMs(ms: number) { + idleTtlMs = normalizeIdleTtlMs(ms); + for (const [poolKey, list] of [...pools.entries()]) { + for (const slot of [...list]) { + if (slot.holders.size > 0) continue; + scheduleIdleClose( + poolKey, + slot, + Math.max(0, (slot.idleSince ?? slot.lastUsedAt) + idleTtlMs - now()), + ); + } + } + enforceGlobalIdleCap(); }, getIdleTtlMs() { - return 0; + return idleTtlMs; }, }; } @@ -328,14 +515,16 @@ export function resetSharedTransferConnectionPoolForTests(): void { sharedPool = null; } -export function buildTransferPoolKey(input: { - hostId?: string; - hostname?: string; - port?: number; - username?: string; - protocol?: string; - sftpSudo?: boolean; -}): string { +export async function buildTransferPoolKey(input: TransferPoolKeyInput): Promise { + const stableSerialize = (value: unknown): string => { + if (value === undefined) return '"__undefined__"'; + if (value === null || typeof value !== "object") return JSON.stringify(value); + if (Array.isArray(value)) return `[${value.map(stableSerialize).join(",")}]`; + const entries = Object.entries(value as Record) + .filter(([key]) => key !== "sessionId") + .sort(([left], [right]) => left.localeCompare(right)); + return `{${entries.map(([key, item]) => `${JSON.stringify(key)}:${stableSerialize(item)}`).join(",")}}`; + }; // Include endpoint identity whenever hostname is known so session-time // hostname/port/username overrides do not share a pool with the vault host. if (input.hostname) { @@ -344,7 +533,12 @@ export function buildTransferPoolKey(input: { const protocol = input.protocol || "ssh"; const sudo = input.sftpSudo ? "sudo" : "nosudo"; const ep = `${input.hostname}:${port}:${user}:${protocol}:${sudo}`; - return input.hostId ? `host:${input.hostId}|ep:${ep}` : `ep:${ep}`; + const base = input.hostId ? `host:${input.hostId}|ep:${ep}` : `ep:${ep}`; + if (!input.connectionOptions) return base; + const encoded = new TextEncoder().encode(stableSerialize(input.connectionOptions)); + const digest = await crypto.subtle.digest("SHA-256", encoded); + const fingerprint = Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, "0")).join(""); + return `${base}|identity:${fingerprint}`; } if (input.hostId) return `host:${input.hostId}`; return "ep:unknown:22:root:ssh:nosudo"; diff --git a/application/state/sftp/transferControlEpoch.test.ts b/application/state/sftp/transferControlEpoch.test.ts index 3bcadecdd..e48e656ce 100644 --- a/application/state/sftp/transferControlEpoch.test.ts +++ b/application/state/sftp/transferControlEpoch.test.ts @@ -6,6 +6,7 @@ import { getTransferControlEpoch, isTransferControlEpochCurrent, resetTransferControlEpochsForTests, + settleTransferControlEpochTree, } from "./transferControlEpoch"; test("control epoch starts at 0 and bumps monotonically per id", () => { @@ -27,3 +28,43 @@ test("resume-style bump invalidates a captured pause epoch", () => { bumpTransferControlEpoch("folder"); assert.equal(isTransferControlEpochCurrent("folder", pauseEpoch), false); }); + +test("settled single-file control epochs do not accumulate across a large batch", () => { + resetTransferControlEpochsForTests(); + const taskIds = Array.from({ length: 4_000 }, (_, index) => `single-${index}`); + + for (const taskId of taskIds) { + const epoch = bumpTransferControlEpoch(taskId); + assert.equal(isTransferControlEpochCurrent(taskId, epoch), true); + settleTransferControlEpochTree(taskId); + assert.equal(getTransferControlEpoch(taskId), 0); + assert.equal(isTransferControlEpochCurrent(taskId, epoch), false); + } +}); + +test("reusing a settled task id cannot make an old pause epoch current again", () => { + resetTransferControlEpochsForTests(); + const oldPauseEpoch = bumpTransferControlEpoch("same-id"); + settleTransferControlEpochTree("same-id"); + + const retryPauseEpoch = bumpTransferControlEpoch("same-id"); + + assert.ok(retryPauseEpoch > oldPauseEpoch); + assert.equal(isTransferControlEpochCurrent("same-id", oldPauseEpoch), false); + assert.equal(isTransferControlEpochCurrent("same-id", retryPauseEpoch), true); +}); + +test("settling a directory tree releases parent and child control epochs together", () => { + resetTransferControlEpochsForTests(); + const childIds = Array.from({ length: 4_000 }, (_, index) => `directory-child-${index}`); + const rootEpoch = bumpTransferControlEpoch("directory-root"); + const childEpochs = childIds.map((childId) => bumpTransferControlEpoch(childId)); + + settleTransferControlEpochTree("directory-root", childIds); + + assert.equal(isTransferControlEpochCurrent("directory-root", rootEpoch), false); + for (let index = 0; index < childIds.length; index += 1) { + assert.equal(getTransferControlEpoch(childIds[index]), 0); + assert.equal(isTransferControlEpochCurrent(childIds[index], childEpochs[index]), false); + } +}); diff --git a/application/state/sftp/transferControlEpoch.ts b/application/state/sftp/transferControlEpoch.ts index 24cc3699a..76eca1734 100644 --- a/application/state/sftp/transferControlEpoch.ts +++ b/application/state/sftp/transferControlEpoch.ts @@ -7,6 +7,9 @@ */ const epochs = new Map(); +// A scalar generation prevents ABA when a settled task id is reused. It does +// not retain task ids, while making every new control round process-unique. +let nextEpoch = 0; export function getTransferControlEpoch(taskId: string): number { return epochs.get(taskId) ?? 0; @@ -14,16 +17,27 @@ export function getTransferControlEpoch(taskId: string): number { /** Bump and return the new epoch. Call on intentional Pause and Resume. */ export function bumpTransferControlEpoch(taskId: string): number { - const next = getTransferControlEpoch(taskId) + 1; + const next = nextEpoch + 1; + nextEpoch = next; epochs.set(taskId, next); return next; } export function isTransferControlEpochCurrent(taskId: string, epoch: number): boolean { - return getTransferControlEpoch(taskId) === epoch; + return epochs.get(taskId) === epoch; +} + +/** Release control epochs after the transfer tree has fully settled. */ +export function settleTransferControlEpochTree( + rootTaskId: string, + childIds: readonly string[] = [], +): void { + epochs.delete(rootTaskId); + for (const childId of childIds) epochs.delete(childId); } /** Test helper. */ export function resetTransferControlEpochsForTests(): void { epochs.clear(); + nextEpoch = 0; } diff --git a/application/state/sftp/transferDirectoryOps.discovery.test.tsx b/application/state/sftp/transferDirectoryOps.discovery.test.tsx new file mode 100644 index 000000000..c5f642ff6 --- /dev/null +++ b/application/state/sftp/transferDirectoryOps.discovery.test.tsx @@ -0,0 +1,233 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import React from "react"; +import { act, create, type ReactTestRenderer } from "react-test-renderer"; + +import type { SftpFileEntry, TransferTask } from "../../../domain/models"; +import { useSftpDirectoryTransferOps } from "./transferDirectoryOps"; + +const directoryEntry = (name: string): SftpFileEntry => ({ + name, + type: "directory", + size: 0, + sizeFormatted: "0 B", + lastModified: 0, + lastModifiedFormatted: "", +}); + +const fileEntry = (name: string): SftpFileEntry => ({ + name, + type: "file", + size: 1, + sizeFormatted: "1 B", + lastModified: 0, + lastModifiedFormatted: "", +}); + +const rootTask = (): TransferTask => ({ + id: "root", + fileName: "source", + sourcePath: "/source", + targetPath: "/target", + sourceConnectionId: "source-sftp", + targetConnectionId: "local", + direction: "download", + status: "transferring", + totalBytes: 0, + transferredBytes: 0, + speed: 0, + startTime: 0, + isDirectory: true, + progressMode: "files", +}); + +test("directory transfer discovers each directory once with bounded listing concurrency", async () => { + const previousWindow = (globalThis as { window?: unknown }).window; + const previousLocalStorage = (globalThis as { localStorage?: unknown }).localStorage; + const previousActEnvironment = (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT; + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; + + const directoryCount = 64; + const root = rootTask(); + let tasks: TransferTask[] = [ + root, + ...Array.from({ length: directoryCount }, (_, index): TransferTask => ({ + ...root, + id: `completed-${index}`, + fileName: `file-${index}.txt`, + sourcePath: `/source/dir-${index}/file-${index}.txt`, + targetPath: `/target/dir-${index}/file-${index}.txt`, + status: "completed", + totalBytes: 1, + transferredBytes: 1, + isDirectory: false, + progressMode: "bytes", + parentTaskId: root.id, + })), + ]; + const transfersRef = { current: tasks }; + const setTransfers = (update: React.SetStateAction) => { + tasks = typeof update === "function" ? update(tasks) : update; + transfersRef.current = tasks; + }; + + let activeListings = 0; + let maxActiveListings = 0; + const listCalls = new Map(); + const listRemoteFiles = async (_sftpId: string, path: string): Promise => { + listCalls.set(path, (listCalls.get(path) ?? 0) + 1); + activeListings += 1; + maxActiveListings = Math.max(maxActiveListings, activeListings); + await new Promise((resolve) => setTimeout(resolve, 1)); + activeListings -= 1; + if (path === "/source") { + return [ + ...Array.from({ length: directoryCount }, (_, index) => directoryEntry(`dir-${index}`)), + { ...directoryEntry("loop"), type: "symlink", linkTarget: "directory" }, + ]; + } + const index = Number(path.slice(path.lastIndexOf("-") + 1)); + return [fileEntry(`file-${index}.txt`)]; + }; + + (globalThis as { window?: unknown }).window = { + netcatty: { + mkdirLocal: async () => undefined, + statLocal: async () => ({ type: "directory" }), + realpathSftp: async (_sftpId: string, remotePath: string) => ( + remotePath === "/source/loop" ? "/source" : remotePath + ), + }, + }; + (globalThis as { localStorage?: unknown }).localStorage = { + getItem: () => null, + setItem: () => undefined, + removeItem: () => undefined, + }; + + let operations: ReturnType | undefined; + let renderer: ReactTestRenderer | null = null; + const Probe = () => { + operations = useSftpDirectoryTransferOps({ + ownerId: "owner", + cancelledTasksRef: { current: new Set() }, + pausedTasksRef: { current: new Set() }, + waitUntilTransferResumed: async () => undefined, + activeChildIdsRef: { current: new Map() }, + transfersRef, + setTransfers, + listLocalFiles: async () => [], + listRemoteFiles, + }); + return null; + }; + + try { + await act(async () => { + renderer = create(React.createElement(Probe)); + }); + assert.ok(operations); + assert.equal("countDirectoryFiles" in operations, false, "directory startup must not expose a separate full-tree count pass"); + + await operations.transferDirectory( + root, + "source-sftp", + null, + false, + true, + "auto", + "auto", + root.id, + undefined, + 0, + true, + ); + + assert.equal(listCalls.size, directoryCount + 1); + assert.ok(Array.from(listCalls.values()).every((count) => count === 1)); + assert.equal(listCalls.has("/source/loop"), false, "canonical symlink cycles must not be listed"); + assert.equal(maxActiveListings, 1); + assert.equal(tasks.find((task) => task.id === root.id)?.totalBytes, directoryCount); + } finally { + await act(async () => { + renderer?.unmount(); + }); + (globalThis as { window?: unknown }).window = previousWindow; + (globalThis as { localStorage?: unknown }).localStorage = previousLocalStorage; + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = previousActEnvironment; + } +}); + +test("live directory download rejects a Windows backslash traversal entry", async () => { + const previousWindow = (globalThis as { window?: unknown }).window; + const previousLocalStorage = (globalThis as { localStorage?: unknown }).localStorage; + const previousActEnvironment = (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT; + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; + + const root = { + ...rootTask(), + targetPath: "C:\\Users\\alice\\Downloads\\folder", + }; + let tasks: TransferTask[] = [root]; + const transfersRef = { current: tasks }; + const setTransfers = (update: React.SetStateAction) => { + tasks = typeof update === "function" ? update(tasks) : update; + transfersRef.current = tasks; + }; + (globalThis as { window?: unknown }).window = { + netcatty: { + mkdirLocal: async () => undefined, + statLocal: async () => ({ type: "directory" }), + }, + }; + (globalThis as { localStorage?: unknown }).localStorage = { + getItem: () => null, + setItem: () => undefined, + removeItem: () => undefined, + }; + + let operations: ReturnType | undefined; + let renderer: ReactTestRenderer | null = null; + const Probe = () => { + operations = useSftpDirectoryTransferOps({ + ownerId: "owner", + cancelledTasksRef: { current: new Set() }, + pausedTasksRef: { current: new Set() }, + waitUntilTransferResumed: async () => undefined, + activeChildIdsRef: { current: new Map() }, + transfersRef, + setTransfers, + listLocalFiles: async () => [], + listRemoteFiles: async () => [fileEntry("..\\outside.txt")], + }); + return null; + }; + + try { + await act(async () => { + renderer = create(React.createElement(Probe)); + }); + assert.ok(operations); + await assert.rejects( + operations.transferDirectory( + root, + "source-sftp", + null, + false, + true, + "auto", + "auto", + root.id, + ), + /unsafe transfer path/i, + ); + assert.equal(tasks.some((task) => task.parentTaskId === root.id), false); + } finally { + await act(async () => { + renderer?.unmount(); + }); + (globalThis as { window?: unknown }).window = previousWindow; + (globalThis as { localStorage?: unknown }).localStorage = previousLocalStorage; + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = previousActEnvironment; + } +}); diff --git a/application/state/sftp/transferDirectoryOps.latchFreeze.test.ts b/application/state/sftp/transferDirectoryOps.latchFreeze.test.ts index bd94885e3..096f979b5 100644 --- a/application/state/sftp/transferDirectoryOps.latchFreeze.test.ts +++ b/application/state/sftp/transferDirectoryOps.latchFreeze.test.ts @@ -2,15 +2,14 @@ import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import test from "node:test"; -test("directory progress freeze is latch-primary (no sticky status re-pause)", () => { +test("directory parent progress freezes while pausing without re-pausing child streams", () => { const source = readFileSync(new URL("./transferDirectoryOps.ts", import.meta.url), "utf8"); - // Must not re-force pause solely from status after intentional resume. - assert.match(source, /const pauseRequested = latchedNow;/); - assert.doesNotMatch( - source, - /const pauseRequested = latchedNow\s*\|\|\s*t\.status === "paused"/, - ); - assert.match(source, /Latch-primary only/); + assert.match(source, /const parentFrozen = !!parentRow/); + assert.match(source, /parentRow\.status === "paused"/); + assert.match(source, /parentRow\.status === "pausing"/); + assert.match(source, /\|\| isPauseLatched\(rootTaskId\)/); + assert.match(source, /if \(parentFrozen\) \{\s*return \{ \.\.\.t, speed: 0 \};/); + assert.doesNotMatch(source, /const pauseRequested = .*status === "paused"/); }); test("directory pauseWatch undoes pause when control epoch is superseded", () => { diff --git a/application/state/sftp/transferDirectoryOps.ts b/application/state/sftp/transferDirectoryOps.ts index bca23ebb8..afb08e1a1 100644 --- a/application/state/sftp/transferDirectoryOps.ts +++ b/application/state/sftp/transferDirectoryOps.ts @@ -1,5 +1,13 @@ import { useCallback, type Dispatch, type MutableRefObject, type SetStateAction } from "react"; import type { Host, SftpFileEntry, SftpFilenameEncoding, TransferStatus, TransferTask } from "../../../domain/models"; +import { + accountSftpDirectoryEntries, + claimSftpDirectoryVisit, + createSftpDirectoryTraversalBudget, + createDirectoryEntryIdentity, + type SftpDirectoryTraversalBudget, + shouldFollowSftpSymlinkDirectory, +} from "../../../domain/sftpDirectoryCheckpoint"; import { STORAGE_KEY_SFTP_TRANSFER_CONCURRENCY } from "../../../infrastructure/config/storageKeys"; import { localStorageAdapter } from "../../../infrastructure/persistence/localStorageAdapter"; import { netcattyBridge } from "../../../infrastructure/services/netcattyBridge"; @@ -20,7 +28,7 @@ import { isTransferControlEpochCurrent, } from "./transferControlEpoch"; import { isTransferOrRootCancelled } from "./transferCancelLatch"; -import { joinPath } from "./utils"; +import { joinPath, joinTransferTargetPath } from "./utils"; function isCancelledLocalOrGlobal( cancelledTasksRef: { current: Set }, @@ -157,78 +165,6 @@ export function useSftpDirectoryTransferOps({ } }, [isPauseLatched, setTransfers, waitWhileTransferPaused]); - const MAX_SYMLINK_DEPTH = 32; - - const estimateDirectoryBytes = useCallback( - async ( - sourcePath: string, - sourceSftpId: string | null, - sourceIsLocal: boolean, - sourceEncoding: SftpFilenameEncoding, - rootTaskId: string, - symlinkDepth = 0, - followSymlinks = false, - ): Promise => { - const estT0 = performance.now(); - if (cancelledTasksRef.current.has(rootTaskId)) { - throw new Error("Transfer cancelled"); - } - - const files = sourceIsLocal - ? await listLocalFiles(sourcePath) - : sourceSftpId - ? await listRemoteFiles(sourceSftpId, sourcePath, sourceEncoding) - : null; - - if (!files) { - throw new Error("No source connection"); - } - - let totalBytes = 0; - const subdirs: { entry: SftpFileEntry; nextDepth: number }[] = []; - - for (const file of files) { - if (file.name === ".." || file.name === ".") continue; - - if (file.type === "directory") { - subdirs.push({ entry: file, nextDepth: symlinkDepth }); - } else if (followSymlinks && file.type === "symlink" && file.linkTarget === "directory") { - if (symlinkDepth < MAX_SYMLINK_DEPTH) { - subdirs.push({ entry: file, nextDepth: symlinkDepth + 1 }); - } - // Skip at max depth — consistent with transferDirectory - } else { - totalBytes += getEntrySize(file); - } - } - - if (subdirs.length > 0) { - if (cancelledTasksRef.current.has(rootTaskId)) { - throw new Error("Transfer cancelled"); - } - - const subResults = await Promise.all( - subdirs.map(({ entry: subdir, nextDepth }) => - estimateDirectoryBytes( - joinPath(sourcePath, subdir.name), - sourceSftpId, - sourceIsLocal, - sourceEncoding, - rootTaskId, - nextDepth, - followSymlinks, - ), - ), - ); - totalBytes += subResults.reduce((sum, size) => sum + size, 0); - } - - logger.debug(`[SFTP:perf] estimateDirectoryBytes ${sourcePath} = ${totalBytes} — ${(performance.now() - estT0).toFixed(0)}ms`); - return totalBytes; - }, - [cancelledTasksRef, getEntrySize, listLocalFiles, listRemoteFiles], - ); - const transferFile = async ( task: TransferTask, sourceSftpId: string | null, @@ -469,52 +405,6 @@ export function useSftpDirectoryTransferOps({ ); }; - /** Recursively count all files under a directory (for progress display). */ - const countDirectoryFiles = async ( - sourcePath: string, - sourceSftpId: string | null, - sourceIsLocal: boolean, - sourceEncoding: SftpFilenameEncoding, - rootTaskId: string, - symlinkDepth = 0, - followSymlinks = false, - ): Promise => { - if (cancelledTasksRef.current.has(rootTaskId)) return 0; - - const files = sourceIsLocal - ? await listLocalFiles(sourcePath) - : sourceSftpId - ? await listRemoteFiles(sourceSftpId, sourcePath, sourceEncoding) - : null; - if (!files) return 0; - - let count = 0; - const subdirPromises: Promise[] = []; - for (const file of files) { - if (file.name === ".." || file.name === ".") continue; - if (file.type === "directory") { - subdirPromises.push( - countDirectoryFiles(joinPath(sourcePath, file.name), sourceSftpId, sourceIsLocal, sourceEncoding, rootTaskId, symlinkDepth, followSymlinks), - ); - } else if (followSymlinks && file.type === "symlink" && file.linkTarget === "directory") { - // Only recurse if within depth limit; skip entirely at max depth - // (consistent with transferDirectory which also skips these) - if (symlinkDepth < MAX_SYMLINK_DEPTH) { - subdirPromises.push( - countDirectoryFiles(joinPath(sourcePath, file.name), sourceSftpId, sourceIsLocal, sourceEncoding, rootTaskId, symlinkDepth + 1, followSymlinks), - ); - } - } else { - count++; - } - } - if (subdirPromises.length > 0) { - const subCounts = await Promise.all(subdirPromises); - count += subCounts.reduce((a, b) => a + b, 0); - } - return count; - }; - /** Returns number of failed child file transfers */ const transferDirectory = async ( task: TransferTask, @@ -528,6 +418,8 @@ export function useSftpDirectoryTransferOps({ sameHost?: boolean, symlinkDepth = 0, followSymlinks = false, // Only true for downloadToLocal — uploads/copies treat symlinks as files + discoveryProgress?: { discoveredFiles: number; nextEntryIndex: number }, + traversalBudget?: SftpDirectoryTraversalBudget, ) => { // Check if task or root task was cancelled before starting if (cancelledTasksRef.current.has(task.id) || cancelledTasksRef.current.has(rootTaskId)) { @@ -535,6 +427,25 @@ export function useSftpDirectoryTransferOps({ } let totalErrors = 0; + const progress = discoveryProgress ?? { discoveredFiles: 0, nextEntryIndex: 0 }; + const traversal = traversalBudget ?? createSftpDirectoryTraversalBudget(); + if (!sourceIsLocal && sourceSftpId) { + const bridge = netcattyBridge.get(); + const canonicalPath = await bridge?.realpathSftp?.(sourceSftpId, task.sourcePath, sourceEncoding) + .catch(() => task.sourcePath) ?? task.sourcePath; + if (!claimSftpDirectoryVisit(traversal, canonicalPath)) return totalErrors; + } + if (!discoveryProgress) { + // A resumed directory may already have completed children. Keep the + // denominator at least as large as the completed count while this + // single traversal rediscovers the full tree. + setTransfers((prev) => prev.map((candidate) => candidate.id === rootTaskId + ? { + ...candidate, + totalBytes: candidate.transferredBytes, + } + : candidate)); + } if (targetIsLocal) { try { @@ -563,6 +474,7 @@ export function useSftpDirectoryTransferOps({ // Filter both "." and ".." — some SFTP servers include "." in readdir const filtered = files.filter((f) => f.name !== ".." && f.name !== "."); + if (!sourceIsLocal) accountSftpDirectoryEntries(traversal, filtered.length); // Separate directories from files. // Symlink directories are only followed when followSymlinks is true // (downloadToLocal). Uploads/copies treat symlinks as regular entries @@ -573,7 +485,7 @@ export function useSftpDirectoryTransferOps({ if (f.type === "directory") { dirs.push(f); } else if (followSymlinks && f.type === "symlink" && f.linkTarget === "directory") { - if (symlinkDepth < MAX_SYMLINK_DEPTH) { + if (shouldFollowSftpSymlinkDirectory(symlinkDepth)) { dirs.push(f); } else { // Count as an error so the parent task is marked failed @@ -584,6 +496,19 @@ export function useSftpDirectoryTransferOps({ regularFiles.push(f); } } + dirs.sort((left, right) => left.name.localeCompare(right.name)); + regularFiles.sort((left, right) => left.name.localeCompare(right.name)); + + // Directory progress is discovered by the same traversal that performs + // the transfer. This avoids a second full-tree list pass and lets the UI + // grow the total incrementally without flooding the server at startup. + progress.discoveredFiles += regularFiles.length; + setTransfers((prev) => prev.map((candidate) => candidate.id === rootTaskId + ? { + ...candidate, + totalBytes: Math.max(progress.discoveredFiles, candidate.transferredBytes), + } + : candidate)); // Process subdirectories sequentially to avoid unbounded concurrent SFTP // requests from nested Promise.all + worker pools across the tree. @@ -603,7 +528,7 @@ export function useSftpDirectoryTransferOps({ fileName: dir.name, originalFileName: dir.name, sourcePath: joinPath(task.sourcePath, dir.name), - targetPath: joinPath(task.targetPath, dir.name), + targetPath: joinTransferTargetPath(task.targetPath, dir.name), isDirectory: true, progressMode: "files", parentTaskId: task.id, @@ -622,6 +547,8 @@ export function useSftpDirectoryTransferOps({ sameHost, isSymlink ? symlinkDepth + 1 : symlinkDepth, followSymlinks, + progress, + traversal, ); totalErrors += subdirErrors; } @@ -632,11 +559,13 @@ export function useSftpDirectoryTransferOps({ // If the SFTP session dies mid-directory, stop queueing more files // (remaining workers will still finish their current item). let sessionLostError: Error | null = null; + const directoryEntryBase = progress.nextEntryIndex; + progress.nextEntryIndex += regularFiles.length; await runSftpTransferWorkers( regularFiles, () => localStorageAdapter.readNumber(STORAGE_KEY_SFTP_TRANSFER_CONCURRENCY), - async (file) => { + async (file, fileIndex) => { if (sessionLostError) throw sessionLostError; if (cancelledTasksRef.current.has(task.id) || cancelledTasksRef.current.has(rootTaskId)) { throw new Error("Transfer cancelled"); @@ -644,7 +573,14 @@ export function useSftpDirectoryTransferOps({ const fileSize = getEntrySize(file); const sourcePath = joinPath(task.sourcePath, file.name); - const targetPath = joinPath(task.targetPath, file.name); + const targetPath = joinTransferTargetPath(task.targetPath, file.name); + const directoryEntryIndex = directoryEntryBase + fileIndex; + const directoryEntryIdentity = createDirectoryEntryIdentity({ + sourcePath, + targetPath, + size: fileSize, + lastModified: file.lastModified, + }); const persistedChild = transfersRef.current.find((candidate) => ( candidate.parentTaskId === rootTaskId && candidate.sourcePath === sourcePath @@ -684,6 +620,9 @@ export function useSftpDirectoryTransferOps({ progressMode: "bytes", parentTaskId: rootTaskId, totalBytes: fileSize, + sourceLastModified: file.lastModified, + directoryEntryIndex, + directoryEntryIdentity, // Inherit retryable from parent — downloadToLocal sets retryable: false // because "local" targetConnectionId can't be resolved by retryTransfer retryable: task.retryable, @@ -834,5 +773,5 @@ export function useSftpDirectoryTransferOps({ }; - return { estimateDirectoryBytes, transferFile, countDirectoryFiles, transferDirectory }; + return { transferFile, transferDirectory }; } diff --git a/application/state/sftp/transferHistoryRestoreMigration.ts b/application/state/sftp/transferHistoryRestoreMigration.ts new file mode 100644 index 000000000..feac6d980 --- /dev/null +++ b/application/state/sftp/transferHistoryRestoreMigration.ts @@ -0,0 +1,203 @@ +import type { TransferTask } from "../../../domain/models"; +import { + appendDirectoryManifestIdentity, + compareDirectoryTraversalPaths, + createDirectoryEntryIdentity, + createEmptyDirectoryResumeCheckpoint, + isValidDirectoryResumeCheckpoint, +} from "../../../domain/sftpDirectoryCheckpoint"; +import { + pruneSftpTransferHistory, + sanitizeSftpTransferTask, + SFTP_TRANSFER_CENTER_VERSION, +} from "../../../domain/sftpTransferCenter"; + +const RESTORE_SYNC_BUDGET_MS = 8; +const RESTORE_CHECK_INTERVAL = 128; +const TERMINAL_STATUSES = new Set(["completed", "failed", "cancelled"]); + +export interface CooperativeTransferHistoryRestoreResult { + valid: boolean; + tasks: TransferTask[]; +} + +function monotonicNow(): number { + return typeof globalThis.performance?.now === "function" + ? globalThis.performance.now() + : Date.now(); +} + +async function yieldToEventLoop(): Promise { + const scheduler = (globalThis as typeof globalThis & { + scheduler?: { yield?: () => Promise }; + }).scheduler; + if (typeof scheduler?.yield === "function") { + await scheduler.yield(); + return; + } + await new Promise((resolve) => setTimeout(resolve, 0)); +} + +function createRestoreYieldController() { + let sliceStartedAt = monotonicNow(); + return async (iteration: number, force = false) => { + if (!force && iteration % RESTORE_CHECK_INTERVAL !== 0) return; + if (!force && monotonicNow() - sliceStartedAt < RESTORE_SYNC_BUDGET_MS) return; + await yieldToEventLoop(); + sliceStartedAt = monotonicNow(); + }; +} + +/** + * Upgrade a large pre-checkpoint transfer history without monopolizing the + * renderer. The final bounded prune still uses the canonical domain function; + * this function only performs its expensive legacy directory normalization + * and manifest hashing in cooperative slices first. + */ +export async function restoreSftpTransferHistoryCooperatively( + raw: string, + now = Date.now(), +): Promise { + // Let first paint and store subscribers attach before parsing a legacy blob. + await yieldToEventLoop(); + + let parsed: { version?: unknown; tasks?: unknown }; + try { + parsed = JSON.parse(raw) as { version?: unknown; tasks?: unknown }; + } catch { + return { valid: false, tasks: [] }; + } + if (parsed.version !== SFTP_TRANSFER_CENTER_VERSION || !Array.isArray(parsed.tasks)) { + return { valid: false, tasks: [] }; + } + + const maybeYield = createRestoreYieldController(); + const restored: TransferTask[] = []; + for (let index = 0; index < parsed.tasks.length; index += 1) { + const task = sanitizeSftpTransferTask(parsed.tasks[index]); + if (task) restored.push(task); + await maybeYield(index); + } + + const unfinishedDirectoryParents: TransferTask[] = []; + const childrenByParent = new Map(); + for (let index = 0; index < restored.length; index += 1) { + const task = restored[index]; + if (task.isDirectory && !task.parentTaskId && !TERMINAL_STATUSES.has(task.status)) { + unfinishedDirectoryParents.push(task); + } + if (task.parentTaskId) { + const children = childrenByParent.get(task.parentTaskId) ?? []; + children.push(task); + childrenByParent.set(task.parentTaskId, children); + } + await maybeYield(index); + } + + const compactedChildIds = new Set(); + const parentUpdates = new Map(); + const normalizedChildUpdates = new Map(); + + for (let parentIndex = 0; parentIndex < unfinishedDirectoryParents.length; parentIndex += 1) { + const parent = unfinishedDirectoryParents[parentIndex]; + let parentChildren = childrenByParent.get(parent.id) ?? []; + const needsLegacyNormalization = !isValidDirectoryResumeCheckpoint(parent.directoryResumeCheckpoint) + && parentChildren.some((child) => ( + !Number.isSafeInteger(child.directoryEntryIndex) + || !/^[a-f0-9]{64}$/.test(child.directoryEntryIdentity ?? "") + )); + + if (needsLegacyNormalization) { + parentChildren = [...parentChildren] + .sort((left, right) => compareDirectoryTraversalPaths(left.sourcePath, right.sourcePath)); + await maybeYield(parentIndex, parentChildren.length > RESTORE_CHECK_INTERVAL); + for (let childIndex = 0; childIndex < parentChildren.length; childIndex += 1) { + const child = parentChildren[childIndex]; + const normalized: TransferTask = { + ...child, + directoryEntryIndex: childIndex, + directoryEntryIdentity: createDirectoryEntryIdentity({ + sourcePath: child.sourcePath, + targetPath: child.targetPath, + size: child.totalBytes, + lastModified: child.sourceLastModified, + }), + }; + parentChildren[childIndex] = normalized; + normalizedChildUpdates.set(normalized.id, normalized); + await maybeYield(childIndex); + } + } + + const checkpoint = isValidDirectoryResumeCheckpoint(parent.directoryResumeCheckpoint) + ? { ...parent.directoryResumeCheckpoint } + : createEmptyDirectoryResumeCheckpoint(); + const initialCoveredEntries = checkpoint.coveredEntries; + const childrenByIndex = new Map(); + for (let childIndex = 0; childIndex < parentChildren.length; childIndex += 1) { + const child = parentChildren[childIndex]; + if ( + Number.isSafeInteger(child.directoryEntryIndex) + && (child.directoryEntryIndex ?? -1) >= 0 + && /^[a-f0-9]{64}$/.test(child.directoryEntryIdentity ?? "") + && !childrenByIndex.has(child.directoryEntryIndex!) + ) { + childrenByIndex.set(child.directoryEntryIndex!, child); + } + await maybeYield(childIndex); + } + + let hashIterations = 0; + while (childrenByIndex.has(checkpoint.coveredEntries)) { + const child = childrenByIndex.get(checkpoint.coveredEntries)!; + checkpoint.manifestHash = appendDirectoryManifestIdentity( + checkpoint.manifestHash, + child.directoryEntryIdentity!, + ); + checkpoint.coveredEntries += 1; + hashIterations += 1; + await maybeYield(hashIterations); + } + + let newlyCompacted = 0; + for (let childIndex = 0; childIndex < parentChildren.length; childIndex += 1) { + const child = parentChildren[childIndex]; + if ( + child.status === "completed" + && Number.isSafeInteger(child.directoryEntryIndex) + && (child.directoryEntryIndex ?? checkpoint.coveredEntries) < checkpoint.coveredEntries + && /^[a-f0-9]{64}$/.test(child.directoryEntryIdentity ?? "") + ) { + compactedChildIds.add(child.id); + newlyCompacted += 1; + } + await maybeYield(childIndex); + } + + if (checkpoint.coveredEntries !== initialCoveredEntries || newlyCompacted > 0) { + checkpoint.completedEntries = Math.min( + checkpoint.coveredEntries, + checkpoint.completedEntries + newlyCompacted, + ); + parentUpdates.set(parent.id, { + ...parent, + directoryResumeCheckpoint: checkpoint, + transferredBytes: Math.max(parent.transferredBytes, checkpoint.completedEntries), + }); + } + } + + const compacted: TransferTask[] = []; + for (let index = 0; index < restored.length; index += 1) { + const task = restored[index]; + if (!compactedChildIds.has(task.id)) { + compacted.push(parentUpdates.get(task.id) ?? normalizedChildUpdates.get(task.id) ?? task); + } + await maybeYield(index); + } + + return { + valid: true, + tasks: pruneSftpTransferHistory(compacted, now), + }; +} diff --git a/application/state/sftp/transferInFlightCleanup.test.ts b/application/state/sftp/transferInFlightCleanup.test.ts new file mode 100644 index 000000000..a35696f5e --- /dev/null +++ b/application/state/sftp/transferInFlightCleanup.test.ts @@ -0,0 +1,29 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +import { runTrackedTransferAttempt } from "./useSftpTransfers.ts"; + +test("a throwing completion handler does not leave the transfer marked in flight", async () => { + const inFlight = new Set(); + const completionHandler = async () => { + throw new Error("completion callback failed"); + }; + + await assert.rejects( + runTrackedTransferAttempt(inFlight, "transfer-1", async () => { + await completionHandler(); + return "completed"; + }), + /completion callback failed/, + ); + assert.equal(inFlight.has("transfer-1"), false); + + let reruns = 0; + const result = await runTrackedTransferAttempt(inFlight, "transfer-1", async () => { + reruns += 1; + return "completed"; + }); + assert.equal(result, "completed"); + assert.equal(reruns, 1); + assert.equal(inFlight.has("transfer-1"), false); +}); diff --git a/application/state/sftp/transferRuntime.test.ts b/application/state/sftp/transferRuntime.test.ts index 4905545d6..496aba6ae 100644 --- a/application/state/sftp/transferRuntime.test.ts +++ b/application/state/sftp/transferRuntime.test.ts @@ -12,6 +12,16 @@ import { createTransferRuntime, resetTransferRuntimeRunsForTests, } from "./transferRuntime"; +import { + isTransferCancelledFlag, + markTransferCancelledTree, + resetTransferCancelLatchesForTests, +} from "./transferCancelLatch"; +import { + bumpTransferControlEpoch, + isTransferControlEpochCurrent, + resetTransferControlEpochsForTests, +} from "./transferControlEpoch"; import { isTransferPauseLatched, resetTransferPauseLatchesForTests, @@ -22,7 +32,6 @@ import { resetTransferWalkRegistryForTests, unregisterTransferWalk, } from "./transferWalkRegistry"; -import { resetTransferControlEpochsForTests } from "./transferControlEpoch"; function makeTask( id: string, @@ -67,6 +76,7 @@ function installBridge(t: test.TestContext, handlers: { } function resetGlobals() { + resetTransferCancelLatchesForTests(); resetTransferPauseLatchesForTests(); resetTransferWalkRegistryForTests(); resetTransferControlEpochsForTests(); @@ -222,6 +232,48 @@ test("runWalk registers process-global walk and survives without a panel owner", resetGlobals(); }); +test("runWalk keeps tree controls live until settle and then releases parent and children", async () => { + resetGlobals(); + const store = createSftpTransferCenterStore(); + const runtime = createTransferRuntime(store); + runtime.enqueue([ + makeTask("settling-root", "transferring", { isDirectory: true, progressMode: "files" }), + makeTask("settling-child", "transferring", { parentTaskId: "settling-root" }), + ]); + let markEntered!: () => void; + let finishRun!: () => void; + const entered = new Promise((resolve) => { markEntered = resolve; }); + const finish = new Promise((resolve) => { finishRun = resolve; }); + let rootEpoch = 0; + let childEpoch = 0; + + const run = runtime.runWalk("settling-root", async () => { + markTransferCancelledTree("settling-root", ["settling-child"]); + rootEpoch = bumpTransferControlEpoch("settling-root"); + childEpoch = bumpTransferControlEpoch("settling-child"); + // Renderer lifecycle paint can become terminal before final callbacks and + // resource release finish. Store cleanup must wait for runWalk settlement. + store.patchTask("settling-root", { status: "completed", endTime: Date.now() }); + markEntered(); + await finish; + }); + + await entered; + assert.equal(isTransferCancelledFlag("settling-root"), true); + assert.equal(isTransferCancelledFlag("settling-child"), true); + assert.equal(isTransferControlEpochCurrent("settling-root", rootEpoch), true); + assert.equal(isTransferControlEpochCurrent("settling-child", childEpoch), true); + + finishRun(); + await run; + assert.equal(isTransferCancelledFlag("settling-root"), false); + assert.equal(isTransferCancelledFlag("settling-child"), false); + assert.equal(isTransferControlEpochCurrent("settling-root", rootEpoch), false); + assert.equal(isTransferControlEpochCurrent("settling-child", childEpoch), false); + + resetGlobals(); +}); + test("publishOwner cannot strip runtime-owned live rows after panel empty publish", () => { resetGlobals(); const store = createSftpTransferCenterStore(); diff --git a/application/state/sftp/transferRuntime.ts b/application/state/sftp/transferRuntime.ts index 2a81d3445..ad9e73c6d 100644 --- a/application/state/sftp/transferRuntime.ts +++ b/application/state/sftp/transferRuntime.ts @@ -23,6 +23,8 @@ import { registerTransferWalk, unregisterTransferWalk, } from "./transferWalkRegistry"; +import { settleTransferCancelTree } from "./transferCancelLatch"; +import { settleTransferControlEpochTree } from "./transferControlEpoch"; export type TransferRuntimeSnapshot = SftpTransferCenterSnapshot; @@ -101,6 +103,11 @@ export function createTransferRuntime( try { await runner(); } finally { + const childIds = store.getSnapshot().tasks + .filter((task) => task.parentTaskId === rootTaskId) + .map((task) => task.id); + const relatedChildIds = settleTransferCancelTree(rootTaskId, childIds); + settleTransferControlEpochTree(rootTaskId, relatedChildIds); unregisterTransferWalk(rootTaskId); if (inFlightRunPromises.get(rootTaskId) === run) { inFlightRunPromises.delete(rootTaskId); diff --git a/application/state/sftp/useSftpConnections.test.ts b/application/state/sftp/useSftpConnections.test.ts index caefcffe9..6613ca77a 100644 --- a/application/state/sftp/useSftpConnections.test.ts +++ b/application/state/sftp/useSftpConnections.test.ts @@ -5,12 +5,22 @@ import test from "node:test"; import assert from "node:assert/strict"; import { + createSftpConnectionId, createPinnedReconnectSideResolver, + openSftpConnectionOnce, openSftpWithSessionPreference, rejectHostKeyVerificationRequest, + takeSftpConnectionMetadataForClose, + releaseSftpConnectionMetadata, resolvePinnedReconnectSide, } from "./useSftpConnections.ts"; +test("connection ids stay unique even when connects start in the same millisecond", () => { + const ids = ["uuid-a", "uuid-b"]; + assert.equal(createSftpConnectionId("left", () => ids.shift()!), "left-uuid-a"); + assert.equal(createSftpConnectionId("left", () => ids.shift()!), "left-uuid-b"); +}); + const openOptions = { sessionId: "sftp-request-1", hostname: "192.168.9.138", @@ -20,10 +30,12 @@ const openOptions = { test("openSftpWithSessionPreference opens session-backed SFTP before authing again", async () => { const calls: string[] = []; + let expectedEndpoint: NetcattySSHOptions | undefined; const sftpId = await openSftpWithSessionPreference({ bridge: { - openSftpForSession: async (sessionId: string) => { + openSftpForSession: async (sessionId: string, endpoint?: NetcattySSHOptions) => { calls.push(`openForSession:${sessionId}`); + expectedEndpoint = endpoint; return "session-backed-sftp"; }, openSftp: async () => { @@ -37,6 +49,7 @@ test("openSftpWithSessionPreference opens session-backed SFTP before authing aga assert.equal(sftpId, "session-backed-sftp"); assert.deepEqual(calls, ["openForSession:ssh-session-1"]); + assert.equal(expectedEndpoint, openOptions); }); test("openSftpWithSessionPreference falls back to normal SFTP when session reuse fails", async () => { @@ -81,6 +94,69 @@ test("openSftpWithSessionPreference opens normal SFTP without a source session", assert.deepEqual(calls, ["openSftp:sftp-request-1"]); }); +test("one connect attempt never repeats a failed fresh SFTP dial after reuse fails", async () => { + const calls: string[] = []; + await assert.rejects( + openSftpConnectionOnce({ + bridge: { + openSftpForSession: async () => { + calls.push("openForSession"); + throw new Error("shared channel unavailable"); + }, + openSftp: async () => { + calls.push("openSftp"); + throw new Error("authentication failed"); + }, + }, + sourceSessionId: "ssh-session-1", + openOptions, + }), + /authentication failed/, + ); + + assert.deepEqual(calls, ["openForSession", "openSftp"]); +}); + +test("closing connections releases their session and cache-key metadata", () => { + const sessions = new Map(); + const cacheKeys = new Map(); + const cleared: string[] = []; + for (let index = 0; index < 1_000; index += 1) { + const connectionId = `connection-${index}`; + sessions.set(connectionId, `sftp-${index}`); + cacheKeys.set(connectionId, `endpoint-${index}`); + assert.equal(takeSftpConnectionMetadataForClose({ + connectionId, + sftpSessions: sessions, + connectionCacheKeys: cacheKeys, + clearCacheForConnection: (id) => { cleared.push(id); }, + }), `sftp-${index}`); + } + assert.equal(sessions.size, 0); + assert.equal(cacheKeys.size, 0); + assert.equal(cleared.length, 1_000); +}); + +test("release metadata closes the backend before a failed connection is retained as an error", async () => { + const sessions = new Map([["connection-1", "sftp-1"]]); + const cacheKeys = new Map([["connection-1", "endpoint-1"]]); + const closed: string[] = []; + const cleared: string[] = []; + + await releaseSftpConnectionMetadata({ + connectionId: "connection-1", + sftpSessions: sessions, + connectionCacheKeys: cacheKeys, + clearCacheForConnection: (id) => { cleared.push(id); }, + closeSftp: async (id) => { closed.push(id); }, + }); + + assert.deepEqual(closed, ["sftp-1"]); + assert.deepEqual(cleared, ["connection-1"]); + assert.equal(sessions.size, 0); + assert.equal(cacheKeys.size, 0); +}); + test("resolvePinnedReconnectSide follows a tab moved to the other side", () => { assert.equal( resolvePinnedReconnectSide("left", "tab-1", [], [{ id: "tab-1" }]), diff --git a/application/state/sftp/useSftpConnections.ts b/application/state/sftp/useSftpConnections.ts index f553a07bd..04cf8d75d 100644 --- a/application/state/sftp/useSftpConnections.ts +++ b/application/state/sftp/useSftpConnections.ts @@ -4,9 +4,13 @@ import { netcattyBridge } from "../../../infrastructure/services/netcattyBridge" import type { Host, Identity, KnownHost, SftpConnection, SftpFileEntry, SftpFilenameEncoding, SSHKey } from "../../../domain/models"; import type { SftpHostKeyInfo, SftpHostKeyVerificationState, SftpPane } from "./types"; import { useSftpDirectoryListing } from "./useSftpDirectoryListing"; -import { buildSftpReuseCredentials, useSftpHostCredentials } from "./useSftpHostCredentials"; +import { useSftpHostCredentials } from "./useSftpHostCredentials"; import { buildCacheKey, getSharedRemoteHostCache, setSharedRemoteHostCache } from "./sharedRemoteHostCache"; import { resolveRemoteSftpStartState } from "./sftpConnectStartPath"; +import { + setDirectoryCacheEntry, + type DirectoryListingCache, +} from "./directoryListingCache"; interface UseSftpConnectionsParams { hosts: Host[]; @@ -26,7 +30,7 @@ interface UseSftpConnectionsParams { getActivePane: (side: "left" | "right") => SftpPane | null; updateTab: (side: "left" | "right", tabId: string, updater: (prev: SftpPane) => SftpPane) => void; navSeqRef: MutableRefObject<{ left: number; right: number }>; - dirCacheRef: MutableRefObject>; + dirCacheRef: MutableRefObject; sftpSessionsRef: MutableRefObject>; lastConnectedHostRef: MutableRefObject<{ left: Host | "local" | null; right: Host | "local" | null }>; connectionCacheKeyMapRef: MutableRefObject>; @@ -57,6 +61,43 @@ interface OpenSftpWithSessionPreferenceParams { openOptions: NetcattySSHOptions; } +export function takeSftpConnectionMetadataForClose(params: { + connectionId: string; + sftpSessions: Map; + connectionCacheKeys: Map; + clearCacheForConnection: (connectionId: string) => void; +}): string | undefined { + const sftpId = params.sftpSessions.get(params.connectionId); + params.sftpSessions.delete(params.connectionId); + params.connectionCacheKeys.delete(params.connectionId); + params.clearCacheForConnection(params.connectionId); + return sftpId; +} + +export async function releaseSftpConnectionMetadata(params: { + connectionId: string; + isLocal?: boolean; + sftpSessions: Map; + connectionCacheKeys: Map; + clearCacheForConnection: (connectionId: string) => void; + closeSftp: (sftpId: string) => Promise; +}): Promise { + const sftpId = takeSftpConnectionMetadataForClose(params); + if (params.isLocal || !sftpId) return; + try { + await params.closeSftp(sftpId); + } catch { + // Best-effort: backend owner cleanup remains the final safety net. + } +} + +export function createSftpConnectionId( + side: "left" | "right", + randomUUID: () => string = () => crypto.randomUUID(), +): string { + return `${side}-${randomUUID()}`; +} + /** Open SFTP through an already-authenticated terminal session before retrying normal auth. */ export async function openSftpWithSessionPreference({ bridge, @@ -66,7 +107,7 @@ export async function openSftpWithSessionPreference({ if (!bridge?.openSftp) throw new Error("SFTP bridge unavailable"); if (sourceSessionId && bridge.openSftpForSession) { try { - return await bridge.openSftpForSession(sourceSessionId); + return await bridge.openSftpForSession(sourceSessionId, openOptions); } catch { // Fall through to the existing SFTP open path so users still get a usable // file browser when the live SSH transport cannot provide an SFTP channel. @@ -75,6 +116,9 @@ export async function openSftpWithSessionPreference({ return bridge.openSftp(openOptions); } +/** One complete SFTP connect attempt: optional shared-channel reuse, then one fresh dial. */ +export const openSftpConnectionOnce = openSftpWithSessionPreference; + export function rejectHostKeyVerificationRequest( bridge: Partial> | null | undefined, requestId: string, @@ -378,7 +422,7 @@ export const useSftpConnections = ({ // immediately, avoiding race conditions with deferred effects. options?.onTabCreated?.(activeTabId); - const connectionId = `${side}-${Date.now()}`; + const connectionId = createSftpConnectionId(side); navSeqRef.current[side] += 1; const connectRequestId = navSeqRef.current[side]; @@ -399,16 +443,13 @@ export const useSftpConnections = ({ return connection?.id === connectionId && connection.currentPath === path; }; const closeSftpSessionForConnection = async () => { - const sftpId = sftpSessionsRef.current.get(connectionId); - sftpSessionsRef.current.delete(connectionId); - connectionCacheKeyMapRef.current.delete(connectionId); - clearCacheForConnection(connectionId); - if (!sftpId) return; - try { - await netcattyBridge.get()?.closeSftp(sftpId); - } catch { - // Ignore errors when closing stale SFTP sessions - } + await releaseSftpConnectionMetadata({ + connectionId, + sftpSessions: sftpSessionsRef.current, + connectionCacheKeys: connectionCacheKeyMapRef.current, + clearCacheForConnection, + closeSftp: async (sftpId) => netcattyBridge.get()?.closeSftp(sftpId), + }); }; // Keep side-wide recovery host pointed at the active tab only. @@ -437,15 +478,13 @@ export const useSftpConnections = ({ // don't close its SFTP session or clear its cache. if (!options?.forceNewTab) { if (currentPane?.connection) { - clearCacheForConnection(currentPane.connection.id); - } - if (currentPane?.connection && !currentPane.connection.isLocal) { - const oldSftpId = sftpSessionsRef.current.get(currentPane.connection.id); - if (oldSftpId) { - // Delete the mapping BEFORE the async closeSftp call to prevent - // concurrent code from using a stale sftpId that the backend may - // have already removed during the await. - sftpSessionsRef.current.delete(currentPane.connection.id); + const oldSftpId = takeSftpConnectionMetadataForClose({ + connectionId: currentPane.connection.id, + sftpSessions: sftpSessionsRef.current, + connectionCacheKeys: connectionCacheKeyMapRef.current, + clearCacheForConnection, + }); + if (!currentPane.connection.isLocal && oldSftpId) { try { await netcattyBridge.get()?.closeSftp(oldSftpId); } catch { @@ -487,7 +526,7 @@ export const useSftpConnections = ({ try { const files = await listLocalFiles(startPath); if (!isTargetConnectionAtPath(startPath)) return; - dirCacheRef.current.set(makeCacheKey(connectionId, startPath, filenameEncoding), { + setDirectoryCacheEntry(dirCacheRef.current, makeCacheKey(connectionId, startPath, filenameEncoding), { files, timestamp: Date.now(), }); @@ -598,84 +637,20 @@ export const useSftpConnections = ({ const openSftp = bridge?.openSftp; if (!openSftp) throw new Error("SFTP bridge unavailable"); - const isAuthError = (err: unknown): boolean => { - if (!(err instanceof Error)) return false; - const msg = err.message.toLowerCase(); - return ( - msg.includes("authentication") || - msg.includes("auth") || - msg.includes("password") || - msg.includes("permission denied") - ); - }; + const credentials = getHostCredentials(host); + // The main-process SSH auth driver owns key/password/MFA fallback. + // Keep one renderer connect entry so a failed fresh dial is never + // immediately repeated after a shared-channel attempt fails. + const sftpId = await openSftpConnectionOnce({ + bridge, + sourceSessionId: !host.sftpSudo ? options?.sourceSessionId : undefined, + openOptions: { + sessionId: sftpSessionId, + ...credentials, + }, + }); - let credentials: NetcattySSHOptions | null = null; - let sftpId: string | undefined; - - // Live Connected rows: try reuse with endpoint-only options first so - // missing/undecryptable vault credentials do not block an already- - // authenticated terminal session. Fall back to full credentials if - // reuse fails (bridge may also fall through to a fresh connect). - if (options?.sourceSessionId && !host.sftpSudo) { - const reuseCredentials = buildSftpReuseCredentials(host, options.sourceSessionId); - try { - sftpId = await openSftpWithSessionPreference({ - bridge, - sourceSessionId: options.sourceSessionId, - openOptions: { - sessionId: sftpSessionId, - ...reuseCredentials, - }, - }); - credentials = reuseCredentials; - } catch { - sftpId = undefined; - } - } - - if (!sftpId) { - credentials = getHostCredentials(host); - const hasKey = !!credentials.privateKey || !!credentials.identityFilePaths?.length; - const hasPassword = !!credentials.password; - - if (hasKey) { - try { - const keyFirstCredentials = { - sessionId: sftpSessionId, - ...credentials, - sourceSessionId: options?.sourceSessionId, - }; - if (!credentials.sudo) { - keyFirstCredentials.password = undefined; - } - sftpId = await openSftp(keyFirstCredentials); - } catch (err) { - if (hasPassword && isAuthError(err)) { - sftpId = await openSftp({ - sessionId: sftpSessionId, - ...credentials, - sourceSessionId: options?.sourceSessionId, - privateKey: undefined, - certificate: undefined, - publicKey: undefined, - keyId: undefined, - keySource: undefined, - identityFilePaths: undefined, - }); - } else { - throw err; - } - } - } else { - sftpId = await openSftp({ - sessionId: sftpSessionId, - ...credentials, - sourceSessionId: options?.sourceSessionId, - }); - } - } - - if (!sftpId || !credentials) throw new Error("Failed to open SFTP session"); + if (!sftpId) throw new Error("Failed to open SFTP session"); sftpSessionsRef.current.set(connectionId, sftpId); if (!isTargetConnectionCurrent()) { @@ -754,7 +729,7 @@ export const useSftpConnections = ({ ? makeCacheKey(connectionId, startPath, filenameEncoding) : null; if (sharedHostCache && provisionalCacheKey) { - dirCacheRef.current.set(provisionalCacheKey, { + setDirectoryCacheEntry(dirCacheRef.current, provisionalCacheKey, { files: sharedHostCache.files, timestamp: Date.now(), }); @@ -798,7 +773,7 @@ export const useSftpConnections = ({ await closeSftpSessionForConnection(); return; } - dirCacheRef.current.set(makeCacheKey(connectionId, startPath, filenameEncoding), { + setDirectoryCacheEntry(dirCacheRef.current, makeCacheKey(connectionId, startPath, filenameEncoding), { files, timestamp: Date.now(), }); @@ -832,6 +807,9 @@ export const useSftpConnections = ({ await closeSftpSessionForConnection(); return; } + // A backend may already be open when initial directory discovery + // fails. Never leave that handle mapped behind an error pane. + await closeSftpSessionForConnection(); clearSideReconnecting(); updateTargetTab((prev) => ({ ...prev, @@ -954,23 +932,26 @@ export const useSftpConnections = ({ navSeqRef.current[side] += 1; - if (pane.connection) { - clearCacheForConnection(pane.connection.id); - } + const sftpId = pane.connection + ? takeSftpConnectionMetadataForClose({ + connectionId: pane.connection.id, + sftpSessions: sftpSessionsRef.current, + connectionCacheKeys: connectionCacheKeyMapRef.current, + clearCacheForConnection, + }) + : undefined; reconnectingRef.current[side] = false; lastConnectedHostRef.current[side] = null; connectedHostByTabIdRef.current.delete(activeTabId); if (pane.connection && !pane.connection.isLocal) { - const sftpId = sftpSessionsRef.current.get(pane.connection.id); if (sftpId) { try { await netcattyBridge.get()?.closeSftp(sftpId); } catch { // Ignore errors when closing SFTP session during disconnect } - sftpSessionsRef.current.delete(pane.connection.id); } } diff --git a/application/state/sftp/useSftpExternalOperations.ts b/application/state/sftp/useSftpExternalOperations.ts index d752a701d..7c6b040e3 100644 --- a/application/state/sftp/useSftpExternalOperations.ts +++ b/application/state/sftp/useSftpExternalOperations.ts @@ -1,4 +1,4 @@ -import { useCallback, useRef, useState } from "react"; +import { useCallback, useEffect, useRef, useState } from "react"; import { FileConflict, FileConflictAction, Host, TransferStatus, SftpFilenameEncoding } from "../../../domain/models"; import { getSftpConflictTypeKey } from "../../../domain/sftpConflict"; import { netcattyBridge } from "../../../infrastructure/services/netcattyBridge"; @@ -36,6 +36,31 @@ import { resolveUploadTargetPane, type UploadEndpointPin, } from "./uploadTargetPin"; +import { + cleanupFailedExternalOpenTemp, + useExternalFileWatchLifecycle, +} from "./externalFileWatchLifecycle"; + +type UploadConflictResolver = { + resolve: (action: FileConflictAction) => void; + setDefault: (action: FileConflictAction) => void; +}; + +export function drainUploadConflictResolvers( + resolvers: Map, + owners: Map, + controller?: UploadController, +): string[] { + const canceledIds: string[] = []; + for (const [conflictId, resolver] of [...resolvers]) { + if (controller && owners.get(conflictId) !== controller) continue; + canceledIds.push(conflictId); + resolvers.delete(conflictId); + owners.delete(conflictId); + resolver.resolve("stop"); + } + return canceledIds; +} export const useSftpExternalOperations = ( params: UseSftpExternalOperationsParams @@ -137,14 +162,24 @@ export const useSftpExternalOperations = ( }, }), [registerUploadController]); - // Track active file watches so the side panel can block host-switching. - // Reset to 0 when the SFTP session disconnects (handled in SftpSidePanel). - const activeFileWatchCountRef = useRef(0); + // Track every renderer-owned watch id so duplicate opens stay deduplicated + // and panel/window teardown releases the worker-side polling resources. + const stopExternalFileWatch = useCallback(async (watchId: string, cleanupTempFile: boolean) => { + await netcattyBridge.get()?.stopFileWatch?.(watchId, cleanupTempFile); + }, []); + const subscribeExternalFileWatchStopped = useCallback(( + callback: (payload: { watchId: string }) => void, + ) => netcattyBridge.get()?.onFileWatchStopped?.(callback), []); + const { + activeCountRef: activeFileWatchCountRef, + captureGeneration: captureExternalFileWatchGeneration, + remember: rememberExternalFileWatch, + releaseAll: releaseExternalFileWatches, + } = useExternalFileWatchLifecycle( + stopExternalFileWatch, + subscribeExternalFileWatchStopped, + ); const [uploadConflicts, setUploadConflicts] = useState([]); - type UploadConflictResolver = { - resolve: (action: FileConflictAction) => void; - setDefault: (action: FileConflictAction) => void; - }; const uploadConflictResolversRef = useRef>(new Map()); /** Maps conflict id → owning UploadController so cancel A never stops B's prompts. */ const uploadConflictOwnersRef = useRef>(new Map()); @@ -289,7 +324,7 @@ export const useSftpExternalOperations = ( } const bridge = netcattyBridge.get(); - if (!bridge?.downloadSftpToTemp) { + if (!bridge?.downloadSftpToTempWithProgress) { throw new Error("SFTP temp download not supported"); } @@ -316,77 +351,68 @@ export const useSftpExternalOperations = ( } }; - if (bridge.downloadSftpToTempWithProgress) { - externalTransferId = `download-temp-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`; + externalTransferId = `download-temp-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`; - sftpTransferCenterStore.upsertTasks([{ - id: externalTransferId, - ownerId, - fileName, - sourcePath: remotePath, - targetPath: "(temp)", - sourceConnectionId: pane.connection.id, - targetConnectionId: "local", - direction: "download", - status: "transferring" as TransferStatus, - totalBytes: 0, - transferredBytes: 0, - speed: 0, - startTime: Date.now(), - isDirectory: false, - retryable: false, - origin: "editor-sync", - background: true, - resumable: true, - phase: "transferring", - }]); + sftpTransferCenterStore.upsertTasks([{ + id: externalTransferId, + ownerId, + fileName, + sourcePath: remotePath, + targetPath: "(temp)", + sourceConnectionId: pane.connection.id, + targetConnectionId: "local", + direction: "download", + status: "transferring" as TransferStatus, + totalBytes: 0, + transferredBytes: 0, + speed: 0, + startTime: Date.now(), + isDirectory: false, + retryable: false, + origin: "editor-sync", + background: true, + resumable: true, + phase: "transferring", + }]); - try { - const result = await bridge.downloadSftpToTempWithProgress( - sftpId, - remotePath, - fileName, - pane.filenameEncoding, - externalTransferId, - ); - wasCancelled = result.cancelled; - localTempPath = result.localPath; - } catch (err) { - sftpTransferCenterStore.patchTask(externalTransferId, { - status: "failed" as TransferStatus, - endTime: Date.now(), - error: err instanceof Error ? err.message : String(err), - speed: 0, - }); - throw err; - } - - if (wasCancelled) { - if (localTempPath && bridge.deleteTempFile) { - bridge.deleteTempFile(localTempPath).catch(() => {}); - } - return { localTempPath: "", sftpId, externalTransferId }; - } - - if (isLocalTempDownloadCancelled()) { - await cleanupTempDownload(localTempPath); - return { localTempPath: "", sftpId, externalTransferId }; - } - - sftpTransferCenterStore.patchTask(externalTransferId, { - status: "completed" as TransferStatus, - endTime: Date.now(), - speed: 0, - }); - } else { - localTempPath = await bridge.downloadSftpToTemp( + try { + const result = await bridge.downloadSftpToTempWithProgress( sftpId, remotePath, fileName, pane.filenameEncoding, + externalTransferId, ); + wasCancelled = result.cancelled; + localTempPath = result.localPath; + } catch (err) { + sftpTransferCenterStore.patchTask(externalTransferId, { + status: "failed" as TransferStatus, + endTime: Date.now(), + error: err instanceof Error ? err.message : String(err), + speed: 0, + }); + throw err; } + if (wasCancelled) { + if (localTempPath && bridge.deleteTempFile) { + bridge.deleteTempFile(localTempPath).catch(() => {}); + } + return { localTempPath: "", sftpId, externalTransferId }; + } + + if (isLocalTempDownloadCancelled()) { + await cleanupTempDownload(localTempPath); + return { localTempPath: "", sftpId, externalTransferId }; + } + + sftpTransferCenterStore.patchTask(externalTransferId, { + status: "completed" as TransferStatus, + endTime: Date.now(), + speed: 0, + }); + if (isLocalTempDownloadCancelled()) { await cleanupTempDownload(localTempPath); return { localTempPath: "", sftpId, externalTransferId }; @@ -436,6 +462,7 @@ export const useSftpExternalOperations = ( try { await bridge.openWithApplication(localTempPath, appPath); } catch (err) { + await cleanupFailedExternalOpenTemp(bridge, sftpId, localTempPath).catch(() => {}); if (externalTransferId) { sftpTransferCenterStore.patchTask(externalTransferId, { status: "failed" as TransferStatus, @@ -449,6 +476,7 @@ export const useSftpExternalOperations = ( let watchId: string | undefined; if (options?.enableWatch && bridge.startFileWatch) { + const watchGeneration = captureExternalFileWatchGeneration(); try { const result = await bridge.startFileWatch( localTempPath, @@ -457,7 +485,7 @@ export const useSftpExternalOperations = ( pane.filenameEncoding, ); watchId = result.watchId; - activeFileWatchCountRef.current += 1; + rememberExternalFileWatch(watchId, watchGeneration); } catch (err) { console.warn("[SFTP] Failed to start file watch:", err); } @@ -465,7 +493,7 @@ export const useSftpExternalOperations = ( return { localTempPath, watchId }; }, - [downloadToTemp, getActivePane], + [captureExternalFileWatchGeneration, downloadToTemp, getActivePane, rememberExternalFileWatch], ); const openWithSystemDefault = useCallback( @@ -494,8 +522,19 @@ export const useSftpExternalOperations = ( if (!localTempPath) return; - const result = await bridgeMethods.openWithSystemDefault(localTempPath); + let result; + try { + result = await bridgeMethods.openWithSystemDefault(localTempPath); + } catch (error) { + if (!pane.connection.isLocal) { + await cleanupFailedExternalOpenTemp(bridgeMethods, sftpId, localTempPath).catch(() => {}); + } + throw error; + } if (!result.success) { + if (!pane.connection.isLocal) { + await cleanupFailedExternalOpenTemp(bridgeMethods, sftpId, localTempPath).catch(() => {}); + } if (externalTransferId) { sftpTransferCenterStore.patchTask(externalTransferId, { status: "failed" as TransferStatus, @@ -509,14 +548,15 @@ export const useSftpExternalOperations = ( // Start file watch for remote SFTP auto-sync (mirrors downloadToTempAndOpen behavior) if (options?.enableWatch && !pane.connection.isLocal && bridgeMethods.startFileWatch) { + const watchGeneration = captureExternalFileWatchGeneration(); try { - await bridgeMethods.startFileWatch( + const result = await bridgeMethods.startFileWatch( localTempPath, remotePath, sftpId, pane.filenameEncoding, ); - activeFileWatchCountRef.current += 1; + rememberExternalFileWatch(result.watchId, watchGeneration); } catch (err) { console.warn("[SFTP] Failed to start file watch for default app open:", err); } @@ -525,7 +565,7 @@ export const useSftpExternalOperations = ( notify.error(err instanceof Error ? err.message : String(err), "SFTP"); } }, - [downloadToTemp, getActivePane], + [captureExternalFileWatchGeneration, downloadToTemp, getActivePane, rememberExternalFileWatch], ); // Create upload callbacks that translate to TransferTask updates @@ -559,20 +599,25 @@ export const useSftpExternalOperations = ( const cancelPendingUploadConflicts = useCallback((controller?: UploadController) => { if (uploadConflictResolversRef.current.size === 0) return; - const canceledIds: string[] = []; - for (const [conflictId, resolver] of [...uploadConflictResolversRef.current]) { - const owner = uploadConflictOwnersRef.current.get(conflictId); - // Scoped cancel: only stop prompts owned by this controller. - if (controller && owner !== controller) continue; - canceledIds.push(conflictId); - uploadConflictResolversRef.current.delete(conflictId); - uploadConflictOwnersRef.current.delete(conflictId); - resolver.resolve("stop"); - } + const canceledIds = drainUploadConflictResolvers( + uploadConflictResolversRef.current, + uploadConflictOwnersRef.current, + controller, + ); if (canceledIds.length === 0) return; setUploadConflicts((prev) => prev.filter((item) => !canceledIds.includes(item.transferId))); }, []); + useEffect(() => () => { + drainUploadConflictResolvers( + uploadConflictResolversRef.current, + uploadConflictOwnersRef.current, + ); + const controllers = [...new Set(uploadControllersByTaskRef.current.values())]; + uploadControllersByTaskRef.current.clear(); + for (const controller of controllers) void controller.cancel(); + }, []); + const createUploadConflictResolver = useCallback((controller: UploadController) => { const conflictDefaults = new Map(); @@ -625,10 +670,16 @@ export const useSftpExternalOperations = ( const createUploadBridge = useCallback((connectHost?: Host): UploadBridge => { const bridge = netcattyBridge.get(); return { + managesTransferLifecycle: Boolean( + bridge?.startStreamTransfer, + ), writeLocalFile: bridge?.writeLocalFile, mkdirLocal: bridge?.mkdirLocal, statLocal: bridge?.statLocal, deleteLocalFile: bridge?.deleteLocalFile, + stageUploadFile: bridge?.stageUploadFile, + cancelStagedUploadFile: bridge?.cancelStagedUploadFile, + deleteTempFile: bridge?.deleteTempFile, mkdirSftp: async (sftpId: string, path: string) => { const b = netcattyBridge.get(); if (b?.mkdirSftp) { @@ -646,28 +697,6 @@ export const useSftpExternalOperations = ( await b.deleteSftp(sftpId, path); } }, - writeSftpBinary: bridge?.writeSftpBinary, - // Wrap writeSftpBinaryWithProgress to adapt UploadBridge interface to NetcattyBridge interface - // UploadBridge: (sftpId, path, data, taskId, onProgress, onComplete, onError) - // NetcattyBridge: (sftpId, path, content, transferId, encoding, onProgress, onComplete, onError) - writeSftpBinaryWithProgress: bridge?.writeSftpBinaryWithProgress - ? async (sftpId, path, data, taskId, onProgress, onComplete, onError) => { - const b = netcattyBridge.get(); - if (!b?.writeSftpBinaryWithProgress) return undefined; - // Pass undefined for encoding to use session default, and forward callbacks - return b.writeSftpBinaryWithProgress( - sftpId, - path, - data, - taskId, - undefined, // encoding - use session default - onProgress, - onComplete, - onError - ); - } - : undefined, - cancelSftpUpload: bridge?.cancelSftpUpload, // Stream transfer for large files (avoids loading into memory). // FileZilla-style: each concurrent file acquires a dedicated transfer // session (max 2/host) so the browse connection stays free. @@ -1407,6 +1436,7 @@ export const useSftpExternalOperations = ( cancelExternalUpload, selectApplication, activeFileWatchCountRef, + releaseExternalFileWatches, uploadConflicts, resolveUploadConflict, }; diff --git a/application/state/sftp/useSftpExternalOperations.types.ts b/application/state/sftp/useSftpExternalOperations.types.ts index c66684f41..d3c9a8e96 100644 --- a/application/state/sftp/useSftpExternalOperations.types.ts +++ b/application/state/sftp/useSftpExternalOperations.types.ts @@ -62,6 +62,7 @@ export interface SftpExternalOperationsResult { ) => Promise<{ localTempPath: string; watchId?: string }>; openWithSystemDefault: (side: "left" | "right", remotePath: string, fileName: string, options?: { enableWatch?: boolean }) => Promise; activeFileWatchCountRef: React.MutableRefObject; + releaseExternalFileWatches: (cleanupTempFiles?: boolean) => Promise; uploadExternalFiles: ( side: "left" | "right", dataTransfer: DataTransfer, diff --git a/application/state/sftp/useSftpHostCredentials.test.ts b/application/state/sftp/useSftpHostCredentials.test.ts index ae18e6773..f3d8c55b0 100644 --- a/application/state/sftp/useSftpHostCredentials.test.ts +++ b/application/state/sftp/useSftpHostCredentials.test.ts @@ -3,7 +3,6 @@ import assert from "node:assert/strict"; import { buildSftpHostCredentials, - buildSftpReuseCredentials, } from "./useSftpHostCredentials.ts"; import type { Host, Identity, KnownHost, SSHKey } from "../../../domain/models.ts"; @@ -17,31 +16,6 @@ const host = (overrides: Partial = {}): Host => ({ ...overrides, }); -test("buildSftpReuseCredentials only needs the live endpoint and sourceSessionId", () => { - const credentials = buildSftpReuseCredentials( - host({ hostname: "live.example.com", username: "alice", port: 2222 }), - "session-live", - ); - - assert.deepEqual(credentials, { - hostname: "live.example.com", - username: "alice", - port: 2222, - sourceSessionId: "session-live", - reuseOnly: true, - sudo: false, - fileProtocol: "auto", - }); -}); - -test("buildSftpReuseCredentials forwards host file protocol preference", () => { - const credentials = buildSftpReuseCredentials( - host({ hostname: "nas.example.com", username: "root", port: 22, sftpFileProtocol: "scp" }), - "session-nas", - ); - assert.equal(credentials.fileProtocol, "scp"); -}); - test("buildSftpHostCredentials forwards system agent settings for target and jump hosts", () => { const jump = host({ id: "jump-1", diff --git a/application/state/sftp/useSftpHostCredentials.ts b/application/state/sftp/useSftpHostCredentials.ts index 7ad8626bf..1f4740c79 100644 --- a/application/state/sftp/useSftpHostCredentials.ts +++ b/application/state/sftp/useSftpHostCredentials.ts @@ -31,24 +31,6 @@ interface UseSftpHostCredentialsParams { terminalSettings?: Pick; } -/** - * Minimal options for reusing a live terminal SSH connection for SFTP. - * Must not require vault password/key material — the source session is already - * authenticated. Endpoint fields must match the session for findReusableSession. - */ -export const buildSftpReuseCredentials = ( - host: Pick, - sourceSessionId: string, -): NetcattySSHOptions => ({ - hostname: host.hostname, - username: host.username || "root", - port: host.port || 22, - sourceSessionId, - reuseOnly: true, - sudo: false, - fileProtocol: host.sftpFileProtocol || "auto", -}); - export const buildSftpHostCredentials = ({ host, hosts, diff --git a/application/state/sftp/useSftpPaneActions.ts b/application/state/sftp/useSftpPaneActions.ts index 3160adf8f..c6a5f4672 100644 --- a/application/state/sftp/useSftpPaneActions.ts +++ b/application/state/sftp/useSftpPaneActions.ts @@ -16,6 +16,11 @@ import { shouldClearSftpFilterForPathChange, } from "./utils"; import { buildCacheKey, setSharedRemoteHostCache } from "./sharedRemoteHostCache"; +import { + getDirectoryCacheEntry, + setDirectoryCacheEntry, + type DirectoryListingCache, +} from "./directoryListingCache"; /** Shared empty set for navigation resets — never mutate this. */ const EMPTY_SET = new Set(); @@ -28,7 +33,7 @@ interface UseSftpPaneActionsParams { leftTabsRef: React.MutableRefObject<{ tabs: SftpPane[]; activeTabId: string | null }>; rightTabsRef: React.MutableRefObject<{ tabs: SftpPane[]; activeTabId: string | null }>; navSeqRef: React.MutableRefObject<{ left: number; right: number }>; - dirCacheRef: React.MutableRefObject>; + dirCacheRef: React.MutableRefObject; sftpSessionsRef: React.MutableRefObject>; lastConnectedHostRef: React.MutableRefObject<{ left: Host | "local" | null; right: Host | "local" | null }>; connectionCacheKeyMapRef: React.MutableRefObject>; @@ -38,6 +43,7 @@ interface UseSftpPaneActionsParams { listLocalFiles: (path: string) => Promise; listRemoteFiles: (sftpId: string, path: string, encoding?: SftpFilenameEncoding) => Promise; handleSessionError: (side: "left" | "right", error: Error) => void; + releaseConnection: (connectionId: string) => Promise; isSessionError: (err: unknown) => boolean; clearSelectionsExcept: (target: { side: "left" | "right"; tabId: string } | null) => void; dirCacheTtlMs: number; @@ -96,6 +102,7 @@ export const useSftpPaneActions = ({ listLocalFiles, listRemoteFiles, handleSessionError, + releaseConnection, isSessionError, clearSelectionsExcept, dirCacheTtlMs, @@ -192,7 +199,7 @@ export const useSftpPaneActions = ({ const nextConfirmedFilter = getSftpFilterAfterPathChange(pane.connection.currentPath, path, pane.filter); const cached = options?.force ? undefined - : dirCacheRef.current.get(cacheKey); + : getDirectoryCacheEntry(dirCacheRef.current, cacheKey, Date.now(), dirCacheTtlMs); if ( cached && @@ -334,8 +341,7 @@ export const useSftpPaneActions = ({ restoreConfirmedStateIfCurrent(); return "aborted"; } - sftpSessionsRef.current.delete(pane.connection.id); - clearCacheForConnection(pane.connection.id); + await releaseConnection(pane.connection.id); if (options?.tabId) { updateTab(side, targetTabId, (prev) => ({ ...prev, @@ -367,10 +373,10 @@ export const useSftpPaneActions = ({ return "aborted"; } - dirCacheRef.current.set(cacheKey, { + setDirectoryCacheEntry(dirCacheRef.current, cacheKey, { files, timestamp: Date.now(), - }); + }, { ttlMs: dirCacheTtlMs }); lastConfirmedRef.current.set(targetTabId, { connectionId, @@ -446,6 +452,7 @@ export const useSftpPaneActions = ({ sftpSessionsRef, clearCacheForConnection, handleSessionError, + releaseConnection, isSessionError, ], ); diff --git a/application/state/sftp/useSftpSessionErrors.ts b/application/state/sftp/useSftpSessionErrors.ts index be2116a20..6327ed942 100644 --- a/application/state/sftp/useSftpSessionErrors.ts +++ b/application/state/sftp/useSftpSessionErrors.ts @@ -11,11 +11,10 @@ interface UseSftpSessionErrorsParams { side: "left" | "right", updater: (prev: SftpPane) => SftpPane, ) => void; - sftpSessionsRef: MutableRefObject>; - clearCacheForConnection: (connectionId: string) => void; navSeqRef: MutableRefObject<{ left: number; right: number }>; lastConnectedHostRef: MutableRefObject<{ left: Host | "local" | null; right: Host | "local" | null }>; reconnectingRef: MutableRefObject<{ left: boolean; right: boolean }>; + releaseConnection: (connectionId: string) => Promise; } /** @@ -41,11 +40,10 @@ export const useSftpSessionErrors = ({ leftTabsRef, rightTabsRef, updateActiveTab, - sftpSessionsRef, - clearCacheForConnection, navSeqRef, lastConnectedHostRef, reconnectingRef, + releaseConnection, }: UseSftpSessionErrorsParams) => useCallback( (side: "left" | "right", _error: Error) => { @@ -55,8 +53,7 @@ export const useSftpSessionErrors = ({ if (!pane || !sideTabs.activeTabId) return; if (pane.connection) { - sftpSessionsRef.current.delete(pane.connection.id); - clearCacheForConnection(pane.connection.id); + void releaseConnection(pane.connection.id); } navSeqRef.current[side] += 1; @@ -109,10 +106,9 @@ export const useSftpSessionErrors = ({ leftTabsRef, rightTabsRef, updateActiveTab, - sftpSessionsRef, - clearCacheForConnection, navSeqRef, lastConnectedHostRef, reconnectingRef, + releaseConnection, ], ); diff --git a/application/state/sftp/useSftpTransfers.ts b/application/state/sftp/useSftpTransfers.ts index 6eb829848..0c0bb464b 100644 --- a/application/state/sftp/useSftpTransfers.ts +++ b/application/state/sftp/useSftpTransfers.ts @@ -37,6 +37,7 @@ import { } from "./transferPauseLatch"; import { bumpTransferControlEpoch, + settleTransferControlEpochTree, } from "./transferControlEpoch"; import { transferRuntime } from "./transferRuntime"; import { @@ -45,10 +46,20 @@ import { isTransferCancelledFlag, markTransferCancelled, markTransferCancelledTree, + settleTransferCancelTree, } from "./transferCancelLatch"; import type { TransferResult, UseSftpTransfersParams, UseSftpTransfersResult } from "./useSftpTransfers.types"; import type { TransferConnectionLease } from "./transferConnectionPool"; +import { + captureDeferredTransferAttempt, + createDeferredTransferAttemptQueue, + isDeferredTransferAttemptCurrent, + pruneTransferConflictDefaults, + type DeferredTransferAttemptQueue, + type TransferConflictDefaults, +} from "./transferConflictLifecycle"; import { getParentPath, joinPath } from "./utils"; +import { promoteDirectoryReplaceStage as promoteDirectoryReplacePaths } from "./directoryReplacePromotion"; /** Keep the MutableRefObject mirror in sync with the process-global latch set. */ function syncPausedTasksRef(ref: { current: Set }, taskId: string, latched: boolean) { @@ -77,6 +88,22 @@ function createPanelCancelSet(): Set { return new PanelCancelSet(); } +export async function runTrackedTransferAttempt( + inFlightTransferIds: Set, + taskId: string, + runner: () => Promise, +): Promise { + inFlightTransferIds.add(taskId); + try { + return await runner(); + } finally { + // Completion callbacks are caller-owned and may throw. The process-global + // runtime already releases its walk in finally; keep the panel mirror just + // as strict so one callback failure cannot permanently block a retry. + inFlightTransferIds.delete(taskId); + } +} + /** User-facing path exclusivity notice (toast + logs). */ function notifyPathConflict( existing: Pick, @@ -207,7 +234,27 @@ export const useSftpTransfers = ({ }, []); const completionHandlersRef = useRef void | Promise>>(new Map()); - const conflictDefaultsRef = useRef>(new Map()); + const conflictDefaultsRef = useRef(new Map()); + const deferredConflictAttemptsRef = useRef(null); + + useEffect(() => { + const conflictDefaults = conflictDefaultsRef.current; + const queue = createDeferredTransferAttemptQueue({ + onError: (error) => logger.warn("[SFTP] Deferred conflict transfer failed", error), + }); + deferredConflictAttemptsRef.current = queue; + return () => { + queue.dispose(); + if (deferredConflictAttemptsRef.current === queue) { + deferredConflictAttemptsRef.current = null; + } + conflictDefaults.clear(); + }; + }, [ownerId]); + + useEffect(() => { + pruneTransferConflictDefaults(conflictDefaultsRef.current, transfers); + }, [transfers]); const clearCancelledTask = useCallback((taskId: string) => { cancelledTasksRef.current.delete(taskId); @@ -356,7 +403,7 @@ export const useSftpTransfers = ({ const { statTargetPath, getDuplicateTarget } = useSftpTransferConflictOps(); - const { estimateDirectoryBytes, transferFile, countDirectoryFiles, transferDirectory } = useSftpDirectoryTransferOps({ + const { transferFile, transferDirectory } = useSftpDirectoryTransferOps({ ownerId, cancelledTasksRef, pausedTasksRef, @@ -387,56 +434,55 @@ export const useSftpTransfers = ({ ?? transferRuntime.getTask(task.id)?.status ?? "transferring"; } - inFlightTransferIdsRef.current.add(task.id); - - // Runtime is the authority writer for live lifecycle. Also mirror into the - // panel list when mounted (view only — soft control does not depend on it). - // Progress-only patches are rAF-coalesced so large-file streams do not - // force a full React + global-center re-render on every IPC tick. - const updateTask = (updates: Partial) => { - const statusChange = updates.status !== undefined - && updates.status !== "transferring" - && updates.status !== "paused" - && updates.status !== "pausing"; - // Pause-latched progress still counts as progress-only (no publishOwner). - const progressOnly = !statusChange - && updates.error === undefined - && updates.conflict === undefined - && updates.ownerId === undefined - && ( - updates.transferredBytes !== undefined - || updates.speed !== undefined - || updates.checkpointBytes !== undefined - || updates.resumeStage !== undefined - || updates.downloadCheckpointBytes !== undefined - || updates.uploadCheckpointBytes !== undefined - || updates.sourceFingerprint !== undefined - || updates.totalBytes !== undefined - || updates.phase !== undefined - || updates.status === "transferring" - || updates.status === "paused" - || updates.status === "pausing" + return runTrackedTransferAttempt(inFlightTransferIdsRef.current, task.id, async () => { + // Runtime is the authority writer for live lifecycle. Also mirror into the + // panel list when mounted (view only — soft control does not depend on it). + // Progress-only patches are rAF-coalesced so large-file streams do not + // force a full React + global-center re-render on every IPC tick. + const updateTask = (updates: Partial) => { + const statusChange = updates.status !== undefined + && updates.status !== "transferring" + && updates.status !== "paused" + && updates.status !== "pausing"; + // Pause-latched progress still counts as progress-only (no publishOwner). + const progressOnly = !statusChange + && updates.error === undefined + && updates.conflict === undefined + && updates.ownerId === undefined + && ( + updates.transferredBytes !== undefined + || updates.speed !== undefined + || updates.checkpointBytes !== undefined + || updates.resumeStage !== undefined + || updates.downloadCheckpointBytes !== undefined + || updates.uploadCheckpointBytes !== undefined + || updates.sourceFingerprint !== undefined + || updates.totalBytes !== undefined + || updates.phase !== undefined + || updates.status === "transferring" + || updates.status === "paused" + || updates.status === "pausing" + ); + if (progressOnly) { + applyProgress(task.id, updates); + return; + } + // Lifecycle transitions: flush any pending progress first, then apply. + if (pendingProgressByIdRef.current.size > 0) { + flushPendingProgress(); + } + transferRuntime.patchTask(task.id, updates); + setTransfers((prev) => + prev.map((t) => (t.id === task.id ? { ...t, ...updates } : t)), ); - if (progressOnly) { - applyProgress(task.id, updates); - return; - } - // Lifecycle transitions: flush any pending progress first, then apply. - if (pendingProgressByIdRef.current.size > 0) { - flushPendingProgress(); - } - transferRuntime.patchTask(task.id, updates); - setTransfers((prev) => - prev.map((t) => (t.id === task.id ? { ...t, ...updates } : t)), - ); - }; + }; - let walkStatus: TransferStatus = "transferring"; - await transferRuntime.runWalk(task.id, async () => { - walkStatus = await processTransferBody(task, sourcePane, targetPane, targetSide, updateTask); + let walkStatus: TransferStatus = "transferring"; + await transferRuntime.runWalk(task.id, async () => { + walkStatus = await processTransferBody(task, sourcePane, targetPane, targetSide, updateTask); + }); + return walkStatus; }); - inFlightTransferIdsRef.current.delete(task.id); - return walkStatus; }; const processTransferBody = async ( @@ -528,21 +574,6 @@ export const useSftpTransfers = ({ const discoverTransferSize = async () => { try { - if (task.isDirectory) { - const discoveredSize = await estimateDirectoryBytes( - task.sourcePath, - sourceSftpId, - sourcePane.connection!.isLocal, - sourceEncoding, - task.id, - ); - if (cancelledTasksRef.current.has(task.id)) return; - updateTask({ - totalBytes: Math.max(discoveredSize, 0), - }); - return; - } - if (task.totalBytes > 0 || !!task.sourceLastModified) return; if (sourcePane.connection?.isLocal) { @@ -592,13 +623,15 @@ export const useSftpTransfers = ({ // seed from byte checkpointBytes (pause clears those, which would show 0/N // while completed children are skipped without re-counting). const directoryCompletedCount = task.isDirectory - ? transfersRef.current.filter( - (candidate) => candidate.parentTaskId === task.id && candidate.status === "completed", - ).length + ? (task.directoryResumeCheckpoint?.completedEntries ?? 0) + transfersRef.current.filter( + (candidate) => candidate.parentTaskId === task.id && candidate.status === "completed", + ).length : 0; updateTask({ status: "transferring", - totalBytes: Math.max(task.totalBytes, 0), + totalBytes: task.isDirectory + ? directoryCompletedCount + : Math.max(task.totalBytes, 0), transferredBytes: task.isDirectory ? Math.max(task.transferredBytes, directoryCompletedCount) : (task.checkpointBytes ?? task.transferredBytes ?? 0), @@ -681,9 +714,9 @@ export const useSftpTransfers = ({ if (cancelledTasksRef.current.has(task.id)) return "cancelled"; if (conflict) { - const defaultAction = conflictDefaultsRef.current.get( - conflictDefaultKey(task.batchId, task.isDirectory, conflict.existingType), - ); + const defaultAction = conflictDefaultsRef.current + .get(task.batchId ?? "global") + ?.get(getSftpConflictTypeKey(task.isDirectory, conflict.existingType)); if (defaultAction) { if (defaultAction === "stop") { await markBatchStopped(task); @@ -781,19 +814,6 @@ export const useSftpTransfers = ({ // transferredBytes = completed file count (incremented by child completions) // Child file tasks are registered in transfers array with their own byte progress. - // Fire-and-forget: count total files for parent progress display - void countDirectoryFiles( - task.sourcePath, - sourceSftpId, - sourcePane.connection!.isLocal, - sourceEncoding, - task.id, - ).then((fileCount) => { - if (!cancelledTasksRef.current.has(task.id)) { - updateTask({ totalBytes: fileCount }); - } - }).catch(() => {}); - const stagedTargetPath = task.replaceExistingTarget ? `${task.targetPath}.netcatty-${task.id.replace(/[^A-Za-z0-9_-]/g, "_")}.part` : undefined; @@ -817,37 +837,34 @@ export const useSftpTransfers = ({ } else if (stagedTargetPath) { const bridge = netcattyBridge.require(); const backupPath = `${task.targetPath}.netcatty-${task.id.replace(/[^A-Za-z0-9_-]/g, "_")}.backup`; - let backedUp = false; - try { - if (targetPane.connection!.isLocal) { - if (!bridge.renameLocalFile || !bridge.deleteLocalFile) throw new Error("Local directory replacement is unavailable"); - try { - await bridge.renameLocalFile(task.targetPath, backupPath); - backedUp = true; - } catch { /* target may not exist */ } - await bridge.renameLocalFile(stagedTargetPath, task.targetPath); - if (backedUp) await bridge.deleteLocalFile(backupPath); - } else if (targetSftpId) { - if (!bridge.renameSftp || !bridge.deleteSftp) throw new Error("Remote directory replacement is unavailable"); - try { - await bridge.renameSftp(targetSftpId, task.targetPath, backupPath, targetEncoding); - backedUp = true; - } catch { /* target may not exist */ } - try { - await bridge.renameSftp(targetSftpId, stagedTargetPath, task.targetPath, targetEncoding); - } catch (error) { - if (backedUp) await bridge.renameSftp(targetSftpId, backupPath, task.targetPath, targetEncoding).catch(() => {}); - throw error; - } - if (backedUp) await bridge.deleteSftp(targetSftpId, backupPath, targetEncoding); + if (targetPane.connection!.isLocal) { + if (!bridge.statLocal || !bridge.renameLocalFile || !bridge.deleteLocalFile) { + throw new Error("Local directory replacement is unavailable"); } - updateTask({ stagedTargetPath: undefined }); - } catch (error) { - if (backedUp && targetPane.connection!.isLocal) { - await bridge.renameLocalFile?.(backupPath, task.targetPath).catch(() => {}); + await promoteDirectoryReplacePaths({ + targetPath: task.targetPath, + stagedPath: stagedTargetPath, + backupPath, + statPath: bridge.statLocal, + renamePath: bridge.renameLocalFile, + deletePath: bridge.deleteLocalFile, + }); + } else if (targetSftpId) { + if (!bridge.statSftp || !bridge.renameSftp || !bridge.deleteSftp) { + throw new Error("Remote directory replacement is unavailable"); } - throw error; + await promoteDirectoryReplacePaths({ + targetPath: task.targetPath, + stagedPath: stagedTargetPath, + backupPath, + statPath: (candidate) => bridge.statSftp!(targetSftpId, candidate, targetEncoding), + renamePath: (source, target) => bridge.renameSftp!(targetSftpId, source, target, targetEncoding), + deletePath: (candidate) => bridge.deleteSftp!(targetSftpId, candidate, targetEncoding), + }); + } else { + throw new Error("Target SFTP session missing for directory promote"); } + updateTask({ stagedTargetPath: undefined }); } } else if (!task.isDirectory) { await transferFile( @@ -1255,6 +1272,7 @@ export const useSftpTransfers = ({ resumeStage: undefined, downloadCheckpointBytes: undefined, uploadCheckpointBytes: undefined, + directoryResumeCheckpoint: undefined, speed: 0, startTime: Date.now(), endTime: undefined, @@ -1289,8 +1307,8 @@ export const useSftpTransfers = ({ const unfinishedParents = new Set( prev.filter((t) => !["completed", "cancelled", "failed"].includes(t.status)).map((t) => t.id), ); - // Keep completed/cancelled children of unfinished directory parents — - // they are resume checkpoints, not disposable history. + // Keep non-compacted terminal exceptions of unfinished directory parents; + // compacted completions already live in the bounded parent checkpoint. return prev.filter((t) => { if (t.parentTaskId && unfinishedParents.has(t.parentTaskId)) return true; return t.status !== "completed" && t.status !== "cancelled"; @@ -1361,7 +1379,10 @@ export const useSftpTransfers = ({ ); if (applyToAll) { - conflictDefaultsRef.current.set(selectedConflictKey, action); + const batchId = conflict.batchId ?? "global"; + const batchDefaults = conflictDefaultsRef.current.get(batchId) ?? new Map(); + batchDefaults.set(getSftpConflictTypeKey(conflict.isDirectory, conflict.existingType), action); + conflictDefaultsRef.current.set(batchId, batchDefaults); } // Eagerly clear refs so a double-click cannot schedule two processTransfer calls. @@ -1478,20 +1499,53 @@ export const useSftpTransfers = ({ ); for (const updatedTask of liveUpdatedTasks) { - setTimeout(async () => { - if (cancelledTasksRef.current.has(updatedTask.id)) return; - const endpoints = resolveTaskEndpoints(updatedTask); - if (!endpoints) return; - await processTransfer(updatedTask, endpoints.sourcePane, endpoints.targetPane, endpoints.targetSide); - }, 100); + const identity = captureDeferredTransferAttempt( + updatedTask, + ownerId, + connectionCacheKeyMapRef.current, + ); + const resolveCurrentAttempt = () => { + const current = transfersRef.current.find((candidate) => candidate.id === updatedTask.id); + if ( + cancelledTasksRef.current.has(updatedTask.id) + || !isDeferredTransferAttemptCurrent(current, identity, connectionCacheKeyMapRef.current) + ) return null; + const endpoints = resolveTaskEndpoints(current!); + if (!endpoints?.sourcePane.connection || !endpoints.targetPane.connection) return null; + const sourceMatches = current!.sourceConnectionId === "local" + ? endpoints.sourcePane.connection.isLocal + : endpoints.sourcePane.connection.id === current!.sourceConnectionId; + const targetMatches = current!.targetConnectionId === "local" + ? endpoints.targetPane.connection.isLocal + : endpoints.targetPane.connection.id === current!.targetConnectionId; + if (!sourceMatches || !targetMatches) return null; + return { current: current!, endpoints }; + }; + deferredConflictAttemptsRef.current?.schedule( + updatedTask.id, + 100, + () => resolveCurrentAttempt() !== null, + async () => { + const attempt = resolveCurrentAttempt(); + if (!attempt) return; + await processTransfer( + attempt.current, + attempt.endpoints.sourcePane, + attempt.endpoints.targetPane, + attempt.endpoints.targetSide, + ); + }, + ); } }, // eslint-disable-next-line react-hooks/exhaustive-deps -- processTransfer is defined inline; transfers/conflicts accessed via refs [ completeCancelledTask, + connectionCacheKeyMapRef, conflictDefaultKey, getDuplicateTarget, markBatchStopped, + ownerId, resolveTaskEndpoints, sftpSessionsRef, ], @@ -1588,23 +1642,6 @@ export const useSftpTransfers = ({ try { if (params.isDirectory) { - // Count files for progress display - void countDirectoryFiles( - params.sourcePath, - workingSourceSftpId, - false, - sourceEncoding, - task.id, - 0, // symlinkDepth - true, // followSymlinks - ).then((fileCount) => { - if (!cancelledTasksRef.current.has(task.id)) { - setTransfers((prev) => - prev.map((t) => (t.id === task.id ? { ...t, totalBytes: fileCount } : t)), - ); - } - }).catch(() => {}); - childFailureCount = await transferDirectory( task, workingSourceSftpId, @@ -1640,7 +1677,8 @@ export const useSftpTransfers = ({ // transferDirectory returns cannot be overwritten by completed/failed. let appliedStatus: TransferStatus = "completed"; setTransfers((prev) => { - const completedCount = prev.filter( + const liveParent = prev.find((candidate) => candidate.id === task.id); + const completedCount = (liveParent?.directoryResumeCheckpoint?.completedEntries ?? 0) + prev.filter( (t) => t.parentTaskId === task.id && t.status === "completed", ).length; return prev.map((t) => { @@ -1700,6 +1738,11 @@ export const useSftpTransfers = ({ } finally { sourceWorkLease?.release(); sourceWorkLease = null; + const childIds = sftpTransferCenterStore.getSnapshot().tasks + .filter((candidate) => candidate.parentTaskId === task.id) + .map((candidate) => candidate.id); + const relatedChildIds = settleTransferCancelTree(task.id, childIds); + settleTransferControlEpochTree(task.id, relatedChildIds); } }, // eslint-disable-next-line react-hooks/exhaustive-deps diff --git a/application/state/sftp/utils.test.ts b/application/state/sftp/utils.test.ts index 017a404d2..cab9b1a8b 100644 --- a/application/state/sftp/utils.test.ts +++ b/application/state/sftp/utils.test.ts @@ -6,9 +6,57 @@ import { getSftpFilterAfterPathChange, getSftpFilterAfterPathChangeError, isConcreteTransferTargetPath, + joinTransferTargetPath, shouldClearSftpFilterForPathChange, } from "./utils"; +test("directory targets cannot escape the selected local root", () => { + const windowsRoot = "C:\\Users\\alice\\Downloads\\folder"; + for (const relativePath of [ + "..\\outside.txt", + "../outside.txt", + "C:\\outside.txt", + "C:/outside.txt", + "\\\\server\\share\\outside.txt", + "/absolute/outside.txt", + ".. ", + ". ", + "safe.txt:alternate-stream", + ]) { + assert.throws( + () => joinTransferTargetPath(windowsRoot, relativePath), + /unsafe transfer path/i, + relativePath, + ); + } + assert.equal( + joinTransferTargetPath(windowsRoot, "nested/report.txt"), + "C:\\Users\\alice\\Downloads\\folder\\nested\\report.txt", + ); + assert.throws( + () => joinTransferTargetPath("/Users/alice/Downloads/folder", "../outside.txt"), + /unsafe transfer path/i, + ); + + const uncRoot = "\\\\server\\share\\Downloads\\folder"; + assert.throws( + () => joinTransferTargetPath(uncRoot, "..\\outside.txt"), + /unsafe transfer path/i, + ); + assert.equal( + joinTransferTargetPath(uncRoot, "nested/report.txt"), + "\\\\server\\share\\Downloads\\folder\\nested\\report.txt", + ); + assert.equal( + getParentPath("\\\\server\\share\\Downloads\\folder"), + "\\\\server\\share\\Downloads", + ); + assert.equal( + getParentPath("\\\\server\\share"), + "\\\\server\\share", + ); +}); + test("concrete transfer target paths exclude temporary placeholders", () => { assert.equal(isConcreteTransferTargetPath({ targetPath: "/Users/alice/Downloads/report.pdf" }), true); assert.equal(isConcreteTransferTargetPath({ targetPath: "C:\\Users\\alice\\Downloads\\report.pdf" }), true); @@ -30,6 +78,10 @@ test("SFTP filter stays when refreshing the same directory", () => { assert.equal(shouldClearSftpFilterForPathChange("/srv/app", "/srv/app"), false); assert.equal(shouldClearSftpFilterForPathChange("/srv/app", "/srv/app/"), false); assert.equal(shouldClearSftpFilterForPathChange("C:\\Users\\alice", "c:/Users/alice"), false); + assert.equal( + shouldClearSftpFilterForPathChange("\\\\Server\\share\\folder", "\\\\server\\share\\folder\\"), + false, + ); assert.equal(getSftpFilterAfterPathChange("/srv/app", "/srv/app", "log"), "log"); assert.equal(getSftpFilterAfterPathChange("/srv/app", "/srv/app/", "log"), "log"); assert.equal(getSftpFilterAfterPathChange("C:\\Users\\alice", "c:/Users/alice", "docs"), "docs"); diff --git a/application/state/sftp/utils.ts b/application/state/sftp/utils.ts index 6b85c1d6a..a50747b89 100644 --- a/application/state/sftp/utils.ts +++ b/application/state/sftp/utils.ts @@ -56,8 +56,16 @@ export const isNavigableDirectory = (entry: SftpFileEntry): boolean => { return entry.type === "directory" || (entry.type === "symlink" && entry.linkTarget === "directory"); }; -// Check if path is Windows-style -export const isWindowsPath = (path: string): boolean => /^[A-Za-z]:/.test(path); +const getWindowsUncRoot = (path: string): string | null => { + const normalized = path.replace(/\//g, "\\"); + const match = normalized.match(/^\\\\([^\\]+)\\([^\\]+)(?:\\|$)/); + return match ? `\\\\${match[1]}\\${match[2]}` : null; +}; + +// Check if path is Windows-style, including network-share (UNC) paths. +export const isWindowsPath = (path: string): boolean => ( + /^[A-Za-z]:/.test(path) || getWindowsUncRoot(path) !== null +); const normalizeWindowsRoot = (path: string): string => { const normalized = path.replace(/\//g, "\\"); @@ -68,7 +76,10 @@ const normalizeWindowsRoot = (path: string): string => { export const isWindowsRoot = (path: string): boolean => { if (!isWindowsPath(path)) return false; - return /^[A-Za-z]:\\?$/.test(path.replace(/\//g, "\\")); + const normalized = path.replace(/\//g, "\\"); + if (/^[A-Za-z]:\\?$/.test(normalized)) return true; + const uncRoot = getWindowsUncRoot(normalized); + return uncRoot !== null && normalized.replace(/[\\]+$/, "") === uncRoot; }; export const joinPath = (base: string, name: string): string => { @@ -80,9 +91,51 @@ export const joinPath = (base: string, name: string): string => { return `${base.replace(/\/+$/, "")}/${name}`; }; +/** + * Join a discovered directory entry to a transfer target without allowing a + * server-controlled filename to escape the user-selected destination root. + * Forward slashes are the traversal's internal separators; a backslash is a + * literal POSIX filename character but becomes a separator on Windows targets. + */ +export function joinTransferTargetPath( + base: string, + relativePath: string, +): string { + const unsafe = () => { + throw new Error(`Unsafe transfer path outside the selected destination: ${relativePath}`); + }; + if (!relativePath || relativePath.includes("\0")) return unsafe(); + if ( + relativePath.startsWith("/") + || relativePath.startsWith("\\") + || /^[A-Za-z]:[\\/]/.test(relativePath) + ) return unsafe(); + + const parts = relativePath.split("/"); + if (parts.some((part) => !part || part === "." || part === "..")) return unsafe(); + + if (isWindowsPath(base)) { + if (parts.some((part) => ( + part.includes("\\") + || part.includes(":") + || /[. ]$/.test(part) + ))) return unsafe(); + return joinPath(base, parts.join("\\")); + } + return joinPath(base, parts.join("/")); +} + export const getParentPath = (path: string): string => { if (isWindowsPath(path)) { const normalized = normalizeWindowsRoot(path).replace(/[\\]+$/, ""); + const uncRoot = getWindowsUncRoot(normalized); + if (uncRoot) { + const rest = normalized.slice(uncRoot.length).replace(/^[\\]+/, ""); + const parts = rest ? rest.split(/[\\]+/).filter(Boolean) : []; + if (parts.length <= 1) return uncRoot; + parts.pop(); + return `${uncRoot}\\${parts.join("\\")}`; + } const drive = normalized.slice(0, 2); if (/^[A-Za-z]:$/.test(normalized) || /^[A-Za-z]:\\$/.test(normalized)) { return `${drive}\\`; @@ -117,7 +170,7 @@ export const getFileName = (path: string): string => { export const normalizeSftpPathForCompare = (path: string): string => { if (isWindowsRoot(path)) return path.replace(/\//g, "\\").toLowerCase(); - if (/^[A-Za-z]:/.test(path)) { + if (isWindowsPath(path)) { return path.replace(/\//g, "\\").replace(/[\\]+$/, "").toLowerCase(); } if (path === "/") return "/"; diff --git a/application/state/sftpTransferCenterStore.test.ts b/application/state/sftpTransferCenterStore.test.ts index 11518b094..0ab708ff1 100644 --- a/application/state/sftpTransferCenterStore.test.ts +++ b/application/state/sftpTransferCenterStore.test.ts @@ -61,6 +61,42 @@ test("store routes cancel/retry/prioritize/dismiss to the task owner; pause/resu assert.ok(!calls.some((c) => c.startsWith("pause:") || c.startsWith("resume:"))); }); +test("background-agent retry after restart uses fresh dedicated recovery instead of the dead session controller", async () => { + let controllerRetryCalls = 0; + const dedicatedTasks: TransferTask[] = []; + const store = createSftpTransferCenterStore(); + store.registerOwner("background-agent", { + pause: async () => {}, + resume: async () => {}, + cancel: async () => {}, + retry: async () => { controllerRetryCalls += 1; }, + prioritize: async () => {}, + dismiss: () => {}, + }); + store.setDedicatedResumeHandler(async (task) => { + dedicatedTasks.push(task); + return { success: true }; + }); + store.publishOwner("background-agent", [{ + ...makeTask("background-retry", "failed"), + sourceHostId: "host-a", + targetConnectionId: "dead-terminal-session", + checkpointBytes: 7, + transferredBytes: 7, + error: "connection closed", + endTime: Date.now(), + }]); + + await store.retry("background-retry"); + + assert.equal(controllerRetryCalls, 0); + assert.equal(dedicatedTasks.length, 1); + assert.equal(dedicatedTasks[0]?.checkpointBytes, 0); + assert.equal(dedicatedTasks[0]?.transferredBytes, 0); + assert.equal(dedicatedTasks[0]?.reconnectRequired, true); + assert.equal(store.getTask("background-retry")?.status, "completed"); +}); + test("resume without an owner uses a live backend transfer session when available", async (t) => { const resumeCalls: string[] = []; const previousWindow = Object.getOwnPropertyDescriptor(globalThis, "window"); @@ -201,6 +237,154 @@ test("background source fingerprint progress is persisted immediately for restar assert.equal(restored.getSnapshot().tasks[0]?.sourceFingerprint, "sha256:background-durable"); }); +test("background terminal lifecycle releases parent and child cancel and control state", async (t) => { + const { + isTransferCancelledFlag, + markTransferCancelledTree, + resetTransferCancelLatchesForTests, + } = await import("./sftp/transferCancelLatch"); + const { + bumpTransferControlEpoch, + isTransferControlEpochCurrent, + resetTransferControlEpochsForTests, + } = await import("./sftp/transferControlEpoch"); + resetTransferCancelLatchesForTests(); + resetTransferControlEpochsForTests(); + t.after(() => { + resetTransferCancelLatchesForTests(); + resetTransferControlEpochsForTests(); + }); + const store = createSftpTransferCenterStore(); + store.publishOwner("background-agent", [ + { ...makeTask("background-root"), isDirectory: true, progressMode: "files" }, + { ...makeTask("background-child"), parentTaskId: "background-root" }, + ]); + markTransferCancelledTree("background-root", ["background-child"]); + const rootEpoch = bumpTransferControlEpoch("background-root"); + const childEpoch = bumpTransferControlEpoch("background-child"); + + store.ingestBackgroundEvent({ + type: "progress", + transferId: "background-root", + transferred: 5, + totalBytes: 10, + speed: 1, + }); + assert.equal(isTransferCancelledFlag("background-root"), true); + assert.equal(isTransferControlEpochCurrent("background-root", rootEpoch), true); + + store.ingestBackgroundEvent({ type: "completed", transferId: "background-root" }); + + assert.equal(isTransferCancelledFlag("background-root"), false); + assert.equal(isTransferCancelledFlag("background-child"), false); + assert.equal(isTransferControlEpochCurrent("background-root", rootEpoch), false); + assert.equal(isTransferControlEpochCurrent("background-child", childEpoch), false); +}); + +test("registered background owner still releases a finished parent and child control tree", async (t) => { + const { + isTransferCancelledFlag, + markTransferCancelledTree, + resetTransferCancelLatchesForTests, + } = await import("./sftp/transferCancelLatch"); + const { + bumpTransferControlEpoch, + isTransferControlEpochCurrent, + resetTransferControlEpochsForTests, + } = await import("./sftp/transferControlEpoch"); + resetTransferCancelLatchesForTests(); + resetTransferControlEpochsForTests(); + t.after(() => { + resetTransferCancelLatchesForTests(); + resetTransferControlEpochsForTests(); + }); + + const store = createSftpTransferCenterStore(); + const unregisterOwner = store.registerOwner("background-agent", { + pause: async () => {}, + resume: async () => {}, + cancel: async () => {}, + retry: async () => {}, + prioritize: async () => {}, + dismiss: () => {}, + }); + t.after(unregisterOwner); + store.publishOwner("background-agent", [ + { ...makeTask("registered-root"), isDirectory: true, progressMode: "files" }, + { ...makeTask("registered-child"), parentTaskId: "registered-root" }, + ]); + markTransferCancelledTree("registered-root", ["registered-child"]); + const rootEpoch = bumpTransferControlEpoch("registered-root"); + const childEpoch = bumpTransferControlEpoch("registered-child"); + + store.ingestBackgroundEvent({ type: "completed", transferId: "registered-root" }); + + assert.equal(isTransferCancelledFlag("registered-root"), false); + assert.equal(isTransferCancelledFlag("registered-child"), false); + assert.equal(isTransferControlEpochCurrent("registered-root", rootEpoch), false); + assert.equal(isTransferControlEpochCurrent("registered-child", childEpoch), false); +}); + +test("registered owner completion waits for its active directory walk before releasing controls", async (t) => { + const { + isTransferCancelledFlag, + markTransferCancelledTree, + resetTransferCancelLatchesForTests, + } = await import("./sftp/transferCancelLatch"); + const { + bumpTransferControlEpoch, + isTransferControlEpochCurrent, + resetTransferControlEpochsForTests, + } = await import("./sftp/transferControlEpoch"); + const { + registerTransferWalk, + resetTransferWalkRegistryForTests, + unregisterTransferWalk, + } = await import("./sftp/transferWalkRegistry"); + resetTransferCancelLatchesForTests(); + resetTransferControlEpochsForTests(); + resetTransferWalkRegistryForTests(); + t.after(() => { + resetTransferCancelLatchesForTests(); + resetTransferControlEpochsForTests(); + resetTransferWalkRegistryForTests(); + }); + + const store = createSftpTransferCenterStore(); + const unregisterOwner = store.registerOwner("active-background-agent", { + pause: async () => {}, + resume: async () => {}, + cancel: async () => {}, + retry: async () => {}, + prioritize: async () => {}, + dismiss: () => {}, + }); + store.publishOwner("active-background-agent", [ + { ...makeTask("active-root"), isDirectory: true, progressMode: "files" }, + { ...makeTask("active-child"), parentTaskId: "active-root" }, + ]); + markTransferCancelledTree("active-root", ["active-child"]); + const rootEpoch = bumpTransferControlEpoch("active-root"); + const childEpoch = bumpTransferControlEpoch("active-child"); + registerTransferWalk("active-root"); + + store.ingestBackgroundEvent({ type: "completed", transferId: "active-root" }); + unregisterOwner(); + + assert.equal(isTransferCancelledFlag("active-root"), true); + assert.equal(isTransferCancelledFlag("active-child"), true); + assert.equal(isTransferControlEpochCurrent("active-root", rootEpoch), true); + assert.equal(isTransferControlEpochCurrent("active-child", childEpoch), true); + + unregisterTransferWalk("active-root"); + store.upsertTasks(store.getSnapshot().tasks); + + assert.equal(isTransferCancelledFlag("active-root"), false); + assert.equal(isTransferCancelledFlag("active-child"), false); + assert.equal(isTransferControlEpochCurrent("active-root", rootEpoch), false); + assert.equal(isTransferControlEpochCurrent("active-child", childEpoch), false); +}); + test("owner source fingerprint publish is persisted immediately for restart", () => { let persisted = ""; const first = createSftpTransferCenterStore({ @@ -296,6 +480,84 @@ test("explicit history deletions are persisted before returning", () => { } }); +test("dismissing a finished directory releases retained parent and child controls", async (t) => { + const { + isTransferCancelledFlag, + markTransferCancelledTree, + resetTransferCancelLatchesForTests, + } = await import("./sftp/transferCancelLatch"); + const { + bumpTransferControlEpoch, + isTransferControlEpochCurrent, + resetTransferControlEpochsForTests, + } = await import("./sftp/transferControlEpoch"); + resetTransferCancelLatchesForTests(); + resetTransferControlEpochsForTests(); + t.after(() => { + resetTransferCancelLatchesForTests(); + resetTransferControlEpochsForTests(); + }); + const store = createSftpTransferCenterStore(); + store.upsertTasks([ + { ...makeTask("dismiss-root", "completed"), isDirectory: true, progressMode: "files" }, + { ...makeTask("dismiss-child", "completed"), parentTaskId: "dismiss-root" }, + ]); + markTransferCancelledTree("dismiss-root", ["dismiss-child"]); + const rootEpoch = bumpTransferControlEpoch("dismiss-root"); + const childEpoch = bumpTransferControlEpoch("dismiss-child"); + + store.dismiss("dismiss-root"); + + assert.equal(isTransferCancelledFlag("dismiss-root"), false); + assert.equal(isTransferCancelledFlag("dismiss-child"), false); + assert.equal(isTransferControlEpochCurrent("dismiss-root", rootEpoch), false); + assert.equal(isTransferControlEpochCurrent("dismiss-child", childEpoch), false); +}); + +test("history pruning releases retained controls for an evicted directory tree", async (t) => { + const { + isTransferCancelledFlag, + markTransferCancelledTree, + resetTransferCancelLatchesForTests, + } = await import("./sftp/transferCancelLatch"); + const { + bumpTransferControlEpoch, + isTransferControlEpochCurrent, + resetTransferControlEpochsForTests, + } = await import("./sftp/transferControlEpoch"); + resetTransferCancelLatchesForTests(); + resetTransferControlEpochsForTests(); + t.after(() => { + resetTransferCancelLatchesForTests(); + resetTransferControlEpochsForTests(); + }); + const store = createSftpTransferCenterStore(); + const now = Date.now(); + store.upsertTasks([ + { ...makeTask("prune-root", "completed"), isDirectory: true, progressMode: "files", endTime: now - 10_000 }, + { ...makeTask("prune-child", "completed"), parentTaskId: "prune-root", endTime: now - 9_999 }, + ...Array.from({ length: 198 }, (_, index) => ({ + ...makeTask(`retained-${index}`, "completed"), + endTime: now - index, + })), + ]); + markTransferCancelledTree("prune-root", ["prune-child"]); + const rootEpoch = bumpTransferControlEpoch("prune-root"); + const childEpoch = bumpTransferControlEpoch("prune-child"); + + store.upsertTasks([ + { ...makeTask("newest-1", "completed"), endTime: now + 1 }, + { ...makeTask("newest-2", "completed"), endTime: now + 2 }, + ]); + + assert.equal(store.getTask("prune-root"), undefined); + assert.equal(store.getTask("prune-child"), undefined); + assert.equal(isTransferCancelledFlag("prune-root"), false); + assert.equal(isTransferCancelledFlag("prune-child"), false); + assert.equal(isTransferControlEpochCurrent("prune-root", rootEpoch), false); + assert.equal(isTransferControlEpochCurrent("prune-child", childEpoch), false); +}); + test("orphaned unfinished tasks stay controllable so dead rows can be cancelled", () => { const store = createSftpTransferCenterStore(); store.publishOwner("gone-panel", [ @@ -1360,12 +1622,24 @@ test("orphan compressed upload resume and cancel keep using the compression job" assert.equal(store.getSnapshot().tasks[0]?.status, "cancelled"); }); -test("orphan cancel marks process-global cancel so surviving walks stop", async () => { +test("orphan cancel marks process-global cancel so surviving walks stop", async (t) => { const { isTransferCancelledFlag, resetTransferCancelLatchesForTests, } = await import("./sftp/transferCancelLatch"); + const { + registerTransferWalk, + resetTransferWalkRegistryForTests, + unregisterTransferWalk, + } = await import("./sftp/transferWalkRegistry"); resetTransferCancelLatchesForTests(); + resetTransferWalkRegistryForTests(); + registerTransferWalk("walk-1"); + t.after(() => { + unregisterTransferWalk("walk-1"); + resetTransferCancelLatchesForTests(); + resetTransferWalkRegistryForTests(); + }); const store = createSftpTransferCenterStore(); const started = Date.now(); store.publishOwner("gone-panel", [{ @@ -1377,7 +1651,6 @@ test("orphan cancel marks process-global cancel so surviving walks stop", async await store.cancel("walk-1"); assert.equal(isTransferCancelledFlag("walk-1"), true); assert.equal(store.getSnapshot().tasks.find((row) => row.id === "walk-1")?.status, "cancelled"); - resetTransferCancelLatchesForTests(); }); test("orphan resume releases process-global pause latch without a panel owner", async (t) => { @@ -1581,6 +1854,28 @@ test("orphaned resume prefers a dedicated SFTP session without a panel owner", a assert.equal(store.getSnapshot().tasks[0]?.reconnectRequired, false); }); +test("dedicated server-to-server resume failure without a panel owner stays recoverable", async () => { + const store = createSftpTransferCenterStore(); + store.publishOwner("dedicated-resume", [{ + ...makeTask("dedicated-s2s", "interrupted"), + direction: "remote-to-remote", + sourceHostId: "host-a", + targetHostId: "host-b", + reconnectRequired: true, + }]); + store.setDedicatedResumeHandler(async () => ({ + success: false, + error: "Open the SFTP panel to reconnect both hosts", + })); + + await store.resume("dedicated-s2s"); + + const task = store.getSnapshot().tasks.find((candidate) => candidate.id === "dedicated-s2s"); + assert.equal(task?.status, "attention"); + assert.equal(task?.ownerId, "dedicated-resume"); + assert.equal(task?.reconnectRequired, true); +}); + test("force-quit continue skips dead soft-resume and uses dedicated handler", async (t) => { const previousWindow = Object.getOwnPropertyDescriptor(globalThis, "window"); t.after(() => { @@ -1745,8 +2040,8 @@ test("directory resume uses dedicated handler and rehomes children", async () => const children = snapshot.filter((task) => task.parentTaskId === "dir-parent"); assert.equal(parent?.status, "completed"); assert.equal(parent?.ownerId, "dedicated-resume"); - assert.ok(children.every((child) => child.ownerId === "dedicated-resume")); - assert.equal(children.find((child) => child.id === "dir-child-done")?.status, "completed"); + assert.equal(children.length, 0, "completed resume children stay compacted out of history"); + assert.equal(parent?.directoryResumeCheckpoint?.completedEntries, 2); }); test("upsertTasks refuses new children under a cancelled directory parent", () => { @@ -2871,6 +3166,293 @@ test("large directory progress coalesces synchronous persistence work", async () ); }); +test("store keeps 50,000 completed directory files as one bounded persisted parent", () => { + let persisted = ""; + const store = createSftpTransferCenterStore({ + read: () => null, + write(value) { persisted = value; }, + }); + const startedAt = Date.now(); + const parent: TransferTask = { + ...makeTask("bounded-directory"), + isDirectory: true, + progressMode: "files", + totalBytes: 50_000, + transferredBytes: 50_000, + }; + store.publishOwner("panel-a", [ + parent, + ...Array.from({ length: 50_000 }, (_, index): TransferTask => ({ + ...makeTask(`bounded-child-${index}`, "completed"), + parentTaskId: parent.id, + sourcePath: `/source/file-${index.toString().padStart(5, "0")}`, + targetPath: `/target/file-${index.toString().padStart(5, "0")}`, + sourceLastModified: index + 1, + endTime: Date.now(), + })), + ]); + + const snapshot = store.getSnapshot(); + const restored = createSftpTransferCenterStore({ read: () => persisted, write: () => {} }); + assert.equal(snapshot.tasks.length, 1); + assert.equal(snapshot.tasks[0]?.directoryResumeCheckpoint?.completedEntries, 50_000); + assert.equal(restored.getSnapshot().tasks.length, 1); + assert.ok(persisted.length < 10_000, "localStorage payload must stay bounded"); + assert.ok(Date.now() - startedAt < 5_000, "full store compaction should finish within 5 seconds"); +}); + +test("legacy 50,000-file history restores cooperatively and converges to one persisted row", async () => { + const now = Date.now(); + const parent: TransferTask = { + ...makeTask("legacy-bounded-directory", "paused"), + isDirectory: true, + progressMode: "files", + totalBytes: 50_000, + transferredBytes: 50_000, + }; + const legacyRaw = JSON.stringify({ + version: 1, + tasks: [ + parent, + ...Array.from({ length: 50_000 }, (_, index): TransferTask => ({ + ...makeTask(`legacy-bounded-child-${index}`, "completed"), + parentTaskId: parent.id, + sourcePath: `/source/file-${index.toString().padStart(5, "0")}`, + targetPath: `/target/file-${index.toString().padStart(5, "0")}`, + sourceLastModified: index + 1, + endTime: now, + })), + ], + }); + let persisted = legacyRaw; + const writes: string[] = []; + let eventLoopHeartbeats = 0; + const heartbeat = setInterval(() => { eventLoopHeartbeats += 1; }, 1); + const createStartedAt = performance.now(); + const store = createSftpTransferCenterStore({ + read: () => persisted, + write(value) { + writes.push(value); + persisted = value; + }, + }); + const createElapsedMs = performance.now() - createStartedAt; + + try { + assert.equal(store.getSnapshot().tasks.length, 0, "large restore must not block initial store creation"); + assert.equal(persisted, legacyRaw, "legacy data must remain untouched until migration succeeds"); + assert.ok(createElapsedMs < 50, `store creation should be fast, took ${createElapsedMs.toFixed(1)}ms`); + + await new Promise((resolve, reject) => { + let unsubscribe = () => {}; + const timeout = setTimeout(() => { + unsubscribe(); + reject(new Error("timed out waiting for legacy transfer history migration")); + }, 10_000); + const check = () => { + const restoredParent = store.getTask(parent.id); + if (restoredParent?.directoryResumeCheckpoint?.completedEntries !== 50_000) return; + clearTimeout(timeout); + unsubscribe(); + resolve(); + }; + unsubscribe = store.subscribe(check); + check(); + }); + } finally { + clearInterval(heartbeat); + } + + assert.ok( + eventLoopHeartbeats >= 5, + `legacy migration must yield repeatedly, observed ${eventLoopHeartbeats} heartbeats`, + ); + assert.equal(store.getSnapshot().tasks.length, 1); + assert.equal(writes.length, 1, "migration should publish one final compacted snapshot"); + assert.equal(JSON.parse(persisted).tasks.length, 1, "persisted history must converge to one row"); + assert.ok(persisted.length < 10_000, "migrated localStorage payload must stay bounded"); +}); + +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"); + const legacyRaw = JSON.stringify({ + version: 1, + padding: "x".repeat(600_000), + tasks: [legacyOnly, staleCollision], + }); + let persisted = legacyRaw; + const writes: string[] = []; + const store = createSftpTransferCenterStore({ + read: () => persisted, + write(value) { + writes.push(value); + persisted = value; + }, + }); + const liveCollision = { + ...makeTask("collision", "transferring"), + transferredBytes: 9, + }; + + store.publishOwner("live-owner", [liveCollision]); + assert.equal(writes.length, 0, "pending migration must hold live persistence writes"); + assert.equal(persisted, legacyRaw, "pending migration must not replace the source snapshot"); + + await new Promise((resolve, reject) => { + let unsubscribe = () => {}; + const timeout = setTimeout(() => { + unsubscribe(); + reject(new Error("timed out waiting for cooperative transfer merge")); + }, 2_000); + const check = () => { + if (!store.getTask(legacyOnly.id)) return; + clearTimeout(timeout); + unsubscribe(); + resolve(); + }; + unsubscribe = store.subscribe(check); + check(); + }); + + assert.equal(store.getTask(legacyOnly.id)?.status, "interrupted"); + assert.equal(store.getTask(liveCollision.id)?.status, "transferring"); + assert.equal(store.getTask(liveCollision.id)?.transferredBytes, 9); + assert.equal(writes.length, 1); + assert.deepEqual( + JSON.parse(persisted).tasks.map((task: TransferTask) => task.id).sort(), + [legacyOnly.id, liveCollision.id].sort(), + ); +}); + +test("background lifecycle events received during cooperative restore are applied after the snapshot", async () => { + const restoredTask = { + ...makeTask("restore-event-race", "transferring"), + ownerId: "background-agent", + transferredBytes: 4, + checkpointBytes: 4, + }; + const legacyRaw = JSON.stringify({ + version: 1, + padding: "x".repeat(600_000), + tasks: [restoredTask], + }); + let persisted = legacyRaw; + const store = createSftpTransferCenterStore({ + read: () => persisted, + write(value) { persisted = value; }, + }); + + store.ingestBackgroundEvent({ + type: "completed", + transferId: restoredTask.id, + transferred: 10, + totalBytes: 10, + endedAt: Date.now(), + }); + + await new Promise((resolve, reject) => { + let unsubscribe = () => {}; + const timeout = setTimeout(() => { + unsubscribe(); + reject(new Error("timed out waiting for cooperative lifecycle replay")); + }, 2_000); + const check = () => { + if (store.getTask(restoredTask.id)?.status !== "completed") return; + clearTimeout(timeout); + unsubscribe(); + resolve(); + }; + unsubscribe = store.subscribe(check); + check(); + }); + + const completed = store.getTask(restoredTask.id); + assert.equal(completed?.status, "completed"); + assert.equal(completed?.reconnectRequired, false, "completion must clear stale reconnect metadata"); + assert.equal(JSON.parse(persisted).tasks[0]?.status, "completed"); +}); + +test("cooperative restore replays lifecycle order without regressing byte watermarks", async () => { + const restoredTask = { + ...makeTask("restore-progress-race", "transferring"), + ownerId: "background-agent", + transferredBytes: 2, + checkpointBytes: 2, + lifecycleEpoch: 1, + }; + const legacyRaw = JSON.stringify({ + version: 1, + padding: "x".repeat(600_000), + tasks: [restoredTask], + }); + const store = createSftpTransferCenterStore({ read: () => legacyRaw, write: () => {} }); + let notifications = 0; + const unsubscribe = store.subscribe(() => { notifications += 1; }); + + store.ingestBackgroundEvent({ + type: "started", + transferId: restoredTask.id, + lifecycleEpoch: 2, + totalBytes: 20, + }); + store.ingestBackgroundEvent({ + type: "progress", + transferId: restoredTask.id, + transferred: 12, + totalBytes: 20, + checkpointBytes: 10, + lifecycleEpoch: 2, + lifecycleState: "transferring", + }); + store.ingestBackgroundEvent({ + type: "progress", + transferId: restoredTask.id, + transferred: 8, + totalBytes: 20, + checkpointBytes: 7, + lifecycleEpoch: 2, + lifecycleState: "transferring", + }); + store.ingestBackgroundEvent({ + type: "paused", + transferId: restoredTask.id, + checkpointBytes: 12, + lifecycleEpoch: 3, + }); + store.ingestBackgroundEvent({ + type: "progress", + transferId: restoredTask.id, + transferred: 18, + totalBytes: 20, + checkpointBytes: 18, + lifecycleEpoch: 2, + lifecycleState: "transferring", + }); + + await new Promise((resolve, reject) => { + const timeout = setTimeout(() => reject(new Error("timed out waiting for lifecycle replay")), 2_000); + const check = () => { + if (store.getTask(restoredTask.id)?.status !== "paused") return; + clearTimeout(timeout); + resolve(); + }; + const off = store.subscribe(() => { + check(); + if (store.getTask(restoredTask.id)?.status === "paused") off(); + }); + check(); + }); + unsubscribe(); + + const paused = store.getTask(restoredTask.id); + assert.equal(paused?.status, "paused"); + assert.equal(paused?.transferredBytes, 12, "same-epoch delayed progress must not move bytes backwards"); + assert.equal(paused?.checkpointBytes, 12, "older progress must not lower the durable checkpoint"); + assert.equal(paused?.lifecycleEpoch, 3); + assert.equal(notifications, 1, "migration and buffered events should publish one atomic snapshot"); +}); + test("completed directory history is pruned with one owner update and no reentrant dismiss storm", () => { const store = createSftpTransferCenterStore(); const now = Date.now(); @@ -2945,3 +3527,90 @@ test("storage exhaustion cannot escape into the renderer update path", () => { }); assert.equal(store.getSnapshot().tasks[0]?.id, "quota-safe"); }); + +test("patchTask lifecycle failures notify and persist through the full store path", () => { + const writes: string[] = []; + const store = createSftpTransferCenterStore({ + read: () => null, + write(value) { writes.push(value); }, + }); + store.publishOwner("panel-a", [makeTask("external-open-failure", "completed")]); + const baselineWrites = writes.length; + let notifications = 0; + store.subscribe(() => { notifications += 1; }); + + store.patchTask("external-open-failure", { + status: "failed", + error: "Could not open the downloaded file", + endTime: Date.now(), + }); + + assert.equal(store.getSnapshot().tasks[0]?.status, "failed"); + assert.equal(notifications, 1, "lifecycle subscribers must update immediately"); + assert.ok(writes.length > baselineWrites, "terminal lifecycle changes must be persisted"); +}); + +test("clearing a changed directory checkpoint is persisted before resume continues", () => { + let persisted = ""; + const store = createSftpTransferCenterStore({ + read: () => null, + write(value) { persisted = value; }, + }); + store.publishOwner("dedicated-resume", [{ + ...makeTask("changed-directory", "interrupted"), + isDirectory: true, + directoryResumeCheckpoint: { + version: 1, + coveredEntries: 2, + completedEntries: 2, + manifestHash: "a".repeat(64), + }, + }]); + + store.patchTask("changed-directory", { directoryResumeCheckpoint: undefined }); + + const restored = createSftpTransferCenterStore({ read: () => persisted, write: () => {} }); + assert.equal(restored.getTask("changed-directory")?.directoryResumeCheckpoint, undefined); +}); + +test("hard directory resume failure falls back to bounded fresh retry history", async () => { + const store = createSftpTransferCenterStore(); + const parent: TransferTask = { + ...makeTask("failed-directory-resume", "interrupted"), + isDirectory: true, + progressMode: "files", + checkpointBytes: 500, + reconnectRequired: true, + targetHostId: "host-a", + targetHostLabel: "box-a", + directoryResumeCheckpoint: { + version: 1, + coveredEntries: 500, + completedEntries: 0, + manifestHash: "a".repeat(64), + }, + }; + store.publishOwner("dedicated-resume", [parent]); + store.setDedicatedResumeHandler(async () => { + store.upsertTasks(Array.from({ length: 500 }, (_, index) => ({ + ...makeTask(`failed-directory-child-${index}`, "failed"), + ownerId: "dedicated-resume", + parentTaskId: parent.id, + sourcePath: `/source/file-${index}`, + targetPath: `/target/file-${index}`, + directoryEntryIndex: index, + directoryEntryIdentity: index.toString(16).padStart(64, "0"), + endTime: Date.now() - index, + }))); + return { success: false, error: "500 files failed to resume" }; + }); + + await store.resume(parent.id); + + const snapshot = store.getSnapshot().tasks; + const retainedParent = snapshot.find((task) => task.id === parent.id); + assert.equal(retainedParent?.status, "failed"); + assert.equal(retainedParent?.directoryResumeCheckpoint, undefined); + assert.equal(retainedParent?.checkpointBytes, 0); + assert.ok(snapshot.length <= 200, `failed directory history must stay bounded, got ${snapshot.length}`); +}); diff --git a/application/state/sftpTransferCenterStore.ts b/application/state/sftpTransferCenterStore.ts index d1495c047..dd34d263f 100644 --- a/application/state/sftpTransferCenterStore.ts +++ b/application/state/sftpTransferCenterStore.ts @@ -24,10 +24,12 @@ import { bumpTransferControlEpoch, getTransferControlEpoch, isTransferControlEpochCurrent, + settleTransferControlEpochTree, } from "./sftp/transferControlEpoch"; import { isTransferWalkInFlight } from "./sftp/transferWalkRegistry"; import { markTransferCancelledTree, + settleTransferCancelTree, } from "./sftp/transferCancelLatch"; import { defaultTransferControlBridge, @@ -35,9 +37,15 @@ import { softResumeTransfer, type TransferControlHost, } from "./sftp/globalSftpTransferControl"; +import { restoreSftpTransferHistoryCooperatively } from "./sftp/transferHistoryRestoreMigration"; type Listener = () => void; +// Ordinary bounded history restores synchronously so existing callers receive +// it immediately. Legacy directory snapshots can contain tens of thousands of +// rows, so they migrate after first paint in cooperative slices. +const ASYNC_TRANSFER_HISTORY_RESTORE_THRESHOLD_BYTES = 512 * 1024; + export interface SftpTransferOwnerControls { pause: (taskId: string) => void | Promise; resume: (taskId: string) => void | Promise; @@ -136,6 +144,8 @@ export interface SftpTransferCenterStore { resolveConflict(taskId: string, action: FileConflictAction, applyToAll?: boolean): Promise; } +type BackgroundTransferEvent = Parameters[0]; + interface StorePersistence { read(): string | null; write(value: string): void; @@ -331,6 +341,21 @@ export function mergeOwnerPublishedTask( } } + if ( + existing.directoryResumeCheckpoint + && ( + !merged.directoryResumeCheckpoint + || merged.directoryResumeCheckpoint.coveredEntries + < existing.directoryResumeCheckpoint.coveredEntries + ) + ) { + merged.directoryResumeCheckpoint = existing.directoryResumeCheckpoint; + merged.transferredBytes = Math.max( + merged.transferredBytes ?? 0, + existing.directoryResumeCheckpoint.completedEntries, + ); + } + return merged; } @@ -347,8 +372,23 @@ function areTransferTasksEquivalent(left: TransferTask, right: TransferTask): bo } export function createSftpTransferCenterStore(persistence?: StorePersistence): SftpTransferCenterStore { - const restored = deserializeSftpTransferCenter(persistence?.read() ?? null); + const persistedRaw = persistence?.read() ?? null; + const shouldRestoreCooperatively = ( + typeof persistedRaw === "string" + && persistedRaw.length >= ASYNC_TRANSFER_HISTORY_RESTORE_THRESHOLD_BYTES + ); + const restored = shouldRestoreCooperatively + ? { tasks: [] } + : deserializeSftpTransferCenter(persistedRaw); let tasks = pruneSftpTransferHistory(restored.tasks); + let restoreMigrationPending = shouldRestoreCooperatively; + let replayingRestoreEvents = false; + let deferRestoreReplayEmits = false; + let restoreEventSequence = 0; + const backgroundEventsDuringRestore = new Map>(); let snapshot = tasks.length > 0 ? buildSnapshot(tasks) : EMPTY_SNAPSHOT; let badgeSnapshot = buildBadgeSnapshot(tasks); let snapshotDirty = false; @@ -414,7 +454,7 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S }); const writePersistence = () => { - if (!persistence || !persistenceDirty) return; + if (!persistence || !persistenceDirty || restoreMigrationPending) return; persistenceDirty = false; try { persistence.write(serializeSftpTransferCenter(tasks)); @@ -431,6 +471,9 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S const persist = (immediate = false) => { if (!persistence) return; persistenceDirty = true; + // Never replace a valid legacy snapshot with the initially empty store. + // Mutations that happen during migration are merged before the final write. + if (restoreMigrationPending) return; const elapsed = Date.now() - lastPersistedAt; if (immediate || lastPersistedAt === 0 || elapsed >= PERSIST_INTERVAL_MS) { if (persistenceTimer) { @@ -473,6 +516,7 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S for (const task of next) { const before = previousById.get(task.id); if (task.sourceFingerprint !== before?.sourceFingerprint) return true; + if (task.directoryResumeCheckpoint !== before?.directoryResumeCheckpoint) return true; if (task.status === "paused" || task.status === "pausing") { if ( task.status !== before?.status @@ -524,12 +568,43 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S return snapshot; }; + const settleFinishedTransferControlState = ( + candidates: readonly TransferTask[], + allTasks: readonly TransferTask[] = tasks, + ) => { + const allTaskIds = new Set(allTasks.map((task) => task.id)); + const childIdsByRoot = new Map(); + for (const task of allTasks) { + if (!task.parentTaskId) continue; + const childIds = childIdsByRoot.get(task.parentTaskId) ?? []; + childIds.push(task.id); + childIdsByRoot.set(task.parentTaskId, childIds); + } + for (const task of candidates) { + if (!TERMINAL_OWNER_STATUSES.has(task.status)) continue; + if (task.parentTaskId && allTaskIds.has(task.parentTaskId)) continue; + if (isTransferWalkInFlight(task.id)) continue; + const childIds = childIdsByRoot.get(task.id) ?? []; + const relatedChildIds = settleTransferCancelTree(task.id, childIds); + settleTransferControlEpochTree(task.id, relatedChildIds); + } + }; + const emit = (persistImmediately = false) => { + if (deferRestoreReplayEmits) { + snapshotDirty = true; + persistenceDirty = persistenceDirty || persistImmediately; + return; + } // Lifecycle / structural changes must paint immediately. Drop any pending // progress coalesce so we do not notify twice with a stale intermediate snapshot. cancelScheduledProgressNotify(); const shouldPersistImmediately = persistImmediately || hasCriticalPersistenceChange(snapshot.tasks, tasks); + // Store terminal state is authoritative for background/detached work. A + // live renderer walk keeps its controls until TransferRuntime.runWalk + // settles; everything else can release its whole tree here. + settleFinishedTransferControlState(tasks, tasks); const beforePrune = tasks; tasks = pruneSftpTransferHistory(tasks); const retainedIds = new Set(tasks.map((task) => task.id)); @@ -559,6 +634,11 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S /** Progress-only: mutate tasks, defer snapshot rebuild, wake progress UI only. */ const emitProgress = (persistImmediately = false) => { + if (deferRestoreReplayEmits) { + snapshotDirty = true; + persistenceDirty = persistenceDirty || persistImmediately; + return; + } snapshotDirty = true; // Never JSON.stringify/localStorage on the pure-progress path. Sync storage // on large directory histories freezes CSS spinners every few seconds. @@ -578,6 +658,17 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S scheduleProgressNotify(); }; const findOwner = (taskId: string) => tasks.find((task) => task.id === taskId)?.ownerId; + const isAlreadyCompactedCompletedChild = (candidate: TransferTask) => { + if (candidate.status !== "completed" || !candidate.parentTaskId) return false; + if (!Number.isSafeInteger(candidate.directoryEntryIndex)) return false; + const parent = tasks.find((task) => task.id === candidate.parentTaskId); + const covered = parent?.directoryResumeCheckpoint?.coveredEntries ?? 0; + if ((candidate.directoryEntryIndex ?? covered) >= covered) return false; + return !tasks.some((task) => ( + task.parentTaskId === candidate.parentTaskId + && task.directoryEntryIndex === candidate.directoryEntryIndex + )); + }; const findAdopter = (task: TransferTask) => [...controllers.entries()].find(([, controls]) => ( controls.adopt && controls.canAdopt?.(task) )); @@ -1049,7 +1140,7 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S if (result.error && /SFTP panel|both hosts/i.test(result.error)) { const restoreOwner = previousOwnerId && previousOwnerId !== "dedicated-resume" ? previousOwnerId - : ownerId; + : undefined; const restoredTask = { ...(tasks.find((candidate) => candidate.id === taskId) ?? task), ownerId: restoreOwner || "dedicated-resume", @@ -1073,14 +1164,22 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S controller = undefined; } } else if (result.error) { - // Hard dedicated failure. + // A failed directory may have more exception children than the history + // cap can retain. Keeping its compact skip checkpoint would make an + // evicted failed child indistinguishable from a compacted completion. + // Fall back to an explicit fresh Retry: bounded history, no silent skip. tasks = tasks.map((candidate) => candidate.id === taskId ? { ...candidate, - status: "attention", + status: candidate.isDirectory ? "failed" : "attention", error: result.error, - reconnectRequired: true, + reconnectRequired: candidate.isDirectory ? false : true, speed: 0, phase: undefined, + ...(candidate.isDirectory ? { + checkpointBytes: 0, + directoryResumeCheckpoint: undefined, + endTime: Date.now(), + } : null), } : candidate); emit(); return; @@ -1166,7 +1265,7 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S await controller[action](taskId); }; - return { + const store: SftpTransferCenterStore = { subscribe(listener) { listeners.add(listener); return () => listeners.delete(listener); @@ -1288,6 +1387,7 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S // Never re-introduce a panel row that already exists under another owner // (e.g. completed via dedicated resume while the panel still holds interrupted). if (!existingIds.has(task.id)) { + if (isAlreadyCompactedCompletedChild(task)) continue; const parentStatus = resolveParentStatus(task.parentTaskId); const parentHoldsPause = parentStatus === "paused" || parentStatus === "pausing"; const seeded = { ...task, ownerId, updatedAt: task.updatedAt ?? Date.now() }; @@ -1324,6 +1424,10 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S if (controllers.get(ownerId) === controls) { controllers.delete(ownerId); lastPublishedByOwner.delete(ownerId); + settleFinishedTransferControlState( + tasks.filter((task) => task.ownerId === ownerId), + tasks, + ); } }; }, @@ -1433,13 +1537,29 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S next[index] = nextTask; tasks = next; if (changed) { + const lifecycleChanged = ( + (updates.status !== undefined && updates.status !== task.status) + || (updates.error !== undefined && updates.error !== task.error) + || (updates.endTime !== undefined && updates.endTime !== task.endTime) + || (updates.conflict !== undefined && updates.conflict !== task.conflict) + || ( + updates.reconnectRequired !== undefined + && updates.reconnectRequired !== task.reconnectRequired + ) + ); // Only a *new* fingerprint must flush + persist immediately. Progress // IPC always echoes the current fingerprint once computed; treating // "key present" as critical disabled emitProgress coalesce and re-pegged // the renderer (~1 notify per tick during resumable uploads). const fingerprintChanged = updates.sourceFingerprint !== undefined && updates.sourceFingerprint !== task.sourceFingerprint; + const directoryCheckpointChanged = Object.prototype.hasOwnProperty.call( + updates, + "directoryResumeCheckpoint", + ) && updates.directoryResumeCheckpoint !== task.directoryResumeCheckpoint; if (fingerprintChanged) emit(true); + else if (directoryCheckpointChanged) emit(true); + else if (lifecycleChanged) emit(); else emitProgress(false); } }, @@ -1460,6 +1580,7 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S }); for (const task of incoming) { if (seen.has(task.id)) continue; + if (isAlreadyCompactedCompletedChild(task)) continue; // Do not resurrect work under a cancelled/completed directory parent. if (task.parentTaskId) { const parentStatus = parentTerminal.get(task.parentTaskId) @@ -1608,14 +1729,21 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S cancel: (taskId) => invoke(taskId, "cancel"), async retry(taskId) { const ownerId = findOwner(taskId); - const controller = controllers.get(ownerId ?? ""); + const task = tasks.find((candidate) => candidate.id === taskId); + // The background-agent controller can only reopen an SFTP channel on the + // original terminal session. Failed rows survive an app restart but that + // session id does not, so Retry must take the same fresh dedicated path + // as any other restored task. Pause/resume of a still-live backend stream + // remains handled by invoke() and the process-global bridge controls. + const controller = ownerId === "background-agent" + ? undefined + : controllers.get(ownerId ?? ""); if (controller) { await controller.retry(taskId); return; } // Orphaned after restart: clear checkpoint so Retry truly restarts, then // resume (dedicated or adopt) from byte 0. - const task = tasks.find((candidate) => candidate.id === taskId); if (task) { tasks = tasks.map((candidate) => candidate.id === taskId ? { ...candidate, @@ -1707,6 +1835,10 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S if (controller) { controller.dismiss(taskId, task); } + const removing = tasks.filter((candidate) => ( + candidate.id === taskId || candidate.parentTaskId === taskId + )); + settleFinishedTransferControlState(removing, removing); tasks = tasks.filter((task) => task.id !== taskId && task.parentTaskId !== taskId); emit(true); }, @@ -1723,6 +1855,7 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S && !(task.parentTaskId && unfinishedParents.has(task.parentTaskId)), ); const removingIds = new Set(removing.map((task) => task.id)); + settleFinishedTransferControlState(removing, removing); tasks = tasks.filter((task) => !removingIds.has(task.id) && !removingIds.has(task.parentTaskId ?? "")); emit(true); notifyOwnersOfPrunedTasks(removing); @@ -1742,6 +1875,13 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S resumePreparationFailures.set(taskId, error); }, ingestBackgroundEvent(event) { + if (restoreMigrationPending && !replayingRestoreEvents) { + const pending = backgroundEventsDuringRestore.get(event.transferId) ?? []; + pending.push({ sequence: restoreEventSequence, event: { ...event } }); + restoreEventSequence += 1; + backgroundEventsDuringRestore.set(event.transferId, pending); + return; + } const existing = tasks.find((task) => task.id === event.transferId); const persistImmediately = event.type === "paused" || event.type === "pausing" @@ -1871,7 +2011,9 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S if (task.id !== event.transferId) return task; // Main-process progress must re-open a live bar even if the panel is // hidden and no longer painting "transferring" via React callbacks. - const nextTransferred = event.transferred ?? task.transferredBytes; + const nextTransferred = event.transferred === undefined + ? task.transferredBytes + : Math.max(task.transferredBytes ?? 0, Number(event.transferred) || 0); const currentEpoch = Number.isFinite(task.lifecycleEpoch) ? (task.lifecycleEpoch as number) : -1; const incomingEpoch = Number.isFinite(event.lifecycleEpoch) ? (event.lifecycleEpoch as number) : -1; const staleLifecycle = task.lifecycleEpoch !== undefined @@ -1951,9 +2093,13 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S ...task, status: nextStatus, transferredBytes: keepPaused ? task.transferredBytes : nextTransferred, - totalBytes: event.totalBytes ?? task.totalBytes, + totalBytes: event.totalBytes === undefined + ? task.totalBytes + : Math.max(task.totalBytes ?? 0, Number(event.totalBytes) || 0), speed: keepPaused ? 0 : (event.speed ?? task.speed), - checkpointBytes: event.checkpointBytes ?? task.checkpointBytes, + checkpointBytes: event.checkpointBytes === undefined + ? task.checkpointBytes + : Math.max(task.checkpointBytes ?? 0, Number(event.checkpointBytes) || 0), resumeStage: event.resumeStage ?? task.resumeStage, downloadCheckpointBytes: event.downloadCheckpointBytes ?? task.downloadCheckpointBytes, uploadCheckpointBytes: event.uploadCheckpointBytes ?? task.uploadCheckpointBytes, @@ -2005,9 +2151,20 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S tasks = tasks.map((task) => task.id === event.transferId ? { ...task, status: event.type === "completed" ? "completed" : event.type === "cancelled" ? "cancelled" : "failed", + transferredBytes: event.transferred === undefined + ? task.transferredBytes + : Math.max(task.transferredBytes ?? 0, Number(event.transferred) || 0), + totalBytes: event.totalBytes === undefined + ? task.totalBytes + : Math.max(task.totalBytes ?? 0, Number(event.totalBytes) || 0), + checkpointBytes: event.checkpointBytes === undefined + ? task.checkpointBytes + : Math.max(task.checkpointBytes ?? 0, Number(event.checkpointBytes) || 0), error: event.error, endTime: event.endedAt ?? Date.now(), speed: 0, + reconnectRequired: event.type === "failed" ? task.reconnectRequired : false, + phase: undefined, } : task); } // Progress-only ticks skip prune + coalesce listener paints; lifecycle @@ -2019,6 +2176,68 @@ export function createSftpTransferCenterStore(persistence?: StorePersistence): S } }, }; + + if (shouldRestoreCooperatively && persistedRaw) { + void restoreSftpTransferHistoryCooperatively(persistedRaw).then((migrated) => { + const replayBufferedBackgroundEvents = () => { + const buffered = [...backgroundEventsDuringRestore.values()] + .flat() + .sort((left, right) => left.sequence - right.sequence); + backgroundEventsDuringRestore.clear(); + if (buffered.length === 0) return; + replayingRestoreEvents = true; + deferRestoreReplayEmits = true; + try { + for (const { event } of buffered) store.ingestBackgroundEvent(event); + } finally { + deferRestoreReplayEmits = false; + replayingRestoreEvents = false; + } + }; + if (!migrated.valid) { + replayBufferedBackgroundEvents(); + restoreMigrationPending = false; + // Preserve invalid/unsupported source data unless a live mutation must + // be made durable. This mirrors the old best-effort restore behavior. + if (snapshotDirty || persistenceDirty) emit(true); + else writePersistence(); + return; + } + + // New transfers may have arrived while the old snapshot was migrating. + // Keep the live row for an ID collision, then run the canonical bounded + // prune once more over the now-small merged result. + const liveIds = new Set(tasks.map((task) => task.id)); + tasks = pruneSftpTransferHistory([ + ...migrated.tasks.filter((task) => !liveIds.has(task.id)), + ...tasks, + ]); + replayBufferedBackgroundEvents(); + restoreMigrationPending = false; + emit(true); + }).catch((error) => { + const buffered = [...backgroundEventsDuringRestore.values()] + .flat() + .sort((left, right) => left.sequence - right.sequence); + backgroundEventsDuringRestore.clear(); + replayingRestoreEvents = true; + deferRestoreReplayEmits = true; + try { + for (const { event } of buffered) store.ingestBackgroundEvent(event); + } finally { + deferRestoreReplayEmits = false; + replayingRestoreEvents = false; + } + restoreMigrationPending = false; + console.warn("[SFTP] Could not migrate legacy transfer history", error); + // Do not erase the legacy value on a migration failure. Only flush if a + // live task mutation was already waiting for durability. + if (snapshotDirty || persistenceDirty) emit(true); + else writePersistence(); + }); + } + + return store; } const browserPersistence: StorePersistence | undefined = typeof globalThis.localStorage === "undefined" diff --git a/application/state/uploadService.test.ts b/application/state/uploadService.test.ts index fd894495e..9ca461718 100644 --- a/application/state/uploadService.test.ts +++ b/application/state/uploadService.test.ts @@ -1,4 +1,4 @@ -import test from "node:test"; +import test, { type TestContext } from "node:test"; import assert from "node:assert/strict"; import { @@ -28,6 +28,60 @@ function createDataTransferWithNullEntries(files: File[]): DataTransfer { } as unknown as DataTransfer; } +function installCompressedUploadBridge( + t: TestContext, + options: { + supported?: boolean; + onStart?: (payload: { + compressionId: string; + folderPath: string; + targetPath: string; + folderName: string; + }) => void; + } = {}, +) { + const previousWindow = globalThis.window; + const previousNetcatty = previousWindow?.netcatty; + const nextWindow = previousWindow ?? ({} as Window & typeof globalThis); + nextWindow.netcatty = { + ...previousNetcatty, + getPathForFile: (file: File) => (file as File & { path?: string }).path, + checkCompressedUploadSupport: async () => ({ + supported: options.supported !== false, + localTar: true, + remoteTar: options.supported !== false, + }), + startCompressedUpload: async (payload) => { + options.onStart?.(payload); + return { compressionId: payload.compressionId, success: true }; + }, + } as NetcattyBridge; + Object.defineProperty(globalThis, "window", { + value: nextWindow, + writable: true, + configurable: true, + }); + t.after(() => { + if (previousWindow) { + previousWindow.netcatty = previousNetcatty; + Object.defineProperty(globalThis, "window", { + value: previousWindow, + writable: true, + configurable: true, + }); + } else { + Reflect.deleteProperty(globalThis, "window"); + } + }); +} + +function createPickedFolderFile(): File { + const file = new File(["folder payload"], "file.txt", { lastModified: 1234 }); + Object.defineProperty(file, "path", { value: "/local/docs/file.txt", writable: true }); + Object.defineProperty(file, "webkitRelativePath", { value: "docs/file.txt" }); + return file; +} + test("upload scanning task can be shown and cancelled before transfers start", () => { const events: string[] = []; const scanningTask = startUploadScanningTask( @@ -82,6 +136,7 @@ test("clears the scanning placeholder when every dropped file is skipped by conf test("uploads DataTransfer files when entry extraction returns no entries", async () => { const file = new File(["picked"], "picked.txt", { lastModified: 1234 }); + Object.defineProperty(file, "path", { value: "/tmp/picked.txt" }); const uploadedPaths: string[] = []; const results = await uploadFromDataTransfer( @@ -92,8 +147,9 @@ test("uploads DataTransfer files when entry extraction returns no entries", asyn isLocal: false, bridge: { mkdirSftp: async () => {}, - writeSftpBinary: async (_sftpId, path) => { - uploadedPaths.push(path); + startStreamTransfer: async ({ targetPath }) => { + uploadedPaths.push(targetPath); + return { transferId: "picked-transfer" }; }, }, joinPath: (base, name) => `${base}/${name}`, @@ -108,6 +164,7 @@ test("uploads DataTransfer files when entry extraction returns no entries", asyn test("uploads picked folder files with their relative directory structure", async () => { const file = new File(["nested"], "file.txt", { lastModified: 1234 }); + Object.defineProperty(file, "path", { value: "/tmp/folder/sub/file.txt" }); Object.defineProperty(file, "webkitRelativePath", { value: "folder/sub/file.txt", }); @@ -124,8 +181,9 @@ test("uploads picked folder files with their relative directory structure", asyn mkdirSftp: async (_sftpId, path) => { madeDirs.push(path); }, - writeSftpBinary: async (_sftpId, path) => { - uploadedPaths.push(path); + startStreamTransfer: async ({ targetPath }) => { + uploadedPaths.push(targetPath); + return { transferId: "folder-transfer" }; }, }, joinPath: (base, name) => `${base}/${name}`, @@ -139,6 +197,261 @@ test("uploads picked folder files with their relative directory structure", asyn ]); }); +test("compression remains enabled for DataTransfer folder uploads that have a conflict resolver", async (t) => { + const starts: string[] = []; + installCompressedUploadBridge(t, { + onStart: (payload) => starts.push(`${payload.folderName}:${payload.folderPath}`), + }); + let conflictCalls = 0; + let streamCalls = 0; + const file = createPickedFolderFile(); + + const results = await uploadFromDataTransfer( + createDataTransferWithNullEntries([file]), + { + targetPath: "/remote", + sftpId: "sftp-1", + isLocal: false, + bridge: { + mkdirSftp: async () => {}, + statSftp: async () => null, + startStreamTransfer: async (payload) => { + streamCalls += 1; + return { transferId: payload.transferId }; + }, + }, + joinPath: (base, name) => `${base}/${name}`, + useCompressedUpload: true, + resolveConflict: async () => { + conflictCalls += 1; + return "merge"; + }, + }, + ); + + assert.deepEqual(starts, ["docs:/local/docs"]); + assert.equal(conflictCalls, 0); + assert.equal(streamCalls, 0); + assert.deepEqual(results, [{ fileName: "docs", success: true }]); +}); + +test("compression remains enabled for picked-folder uploads that have a conflict resolver", async (t) => { + let compressedStarts = 0; + installCompressedUploadBridge(t, { + onStart: () => { compressedStarts += 1; }, + }); + let streamCalls = 0; + const results = await uploadFromFileList( + [createPickedFolderFile()], + { + targetPath: "/remote", + sftpId: "sftp-1", + isLocal: false, + bridge: { + mkdirSftp: async () => {}, + statSftp: async () => null, + startStreamTransfer: async (payload) => { + streamCalls += 1; + return { transferId: payload.transferId }; + }, + }, + joinPath: (base, name) => `${base}/${name}`, + useCompressedUpload: true, + resolveConflict: async () => "merge", + }, + ); + + assert.equal(compressedStarts, 1); + assert.equal(streamCalls, 0); + assert.deepEqual(results, [{ fileName: "docs", success: true }]); +}); + +test("compressed folder conflict decisions preserve merge, replace, and fallback semantics", async (t) => { + let supported = true; + let compressedStarts = 0; + installCompressedUploadBridge(t, { + get supported() { return supported; }, + onStart: () => { compressedStarts += 1; }, + }); + const entry = { + file: createPickedFolderFile(), + relativePath: "docs/file.txt", + isDirectory: false, + }; + + let conflictCalls = 0; + const merged = await uploadEntriesDirect( + [entry], + { + targetPath: "/remote", + sftpId: "sftp-1", + isLocal: false, + bridge: { + mkdirSftp: async () => {}, + statSftp: async () => ({ type: "directory", size: 0, lastModified: 1000 }), + }, + joinPath: (base, name) => `${base}/${name}`, + useCompressedUpload: true, + resolveConflict: async () => { + conflictCalls += 1; + return "merge"; + }, + }, + ); + assert.equal(compressedStarts, 1, "directory merge should retain atomic compressed upload"); + assert.equal(conflictCalls, 1); + assert.deepEqual(merged, [{ fileName: "docs", success: true }]); + + let exists = true; + let deletes = 0; + let streams = 0; + conflictCalls = 0; + const replaced = await uploadEntriesDirect( + [entry], + { + targetPath: "/remote", + sftpId: "sftp-1", + isLocal: false, + bridge: { + mkdirSftp: async () => {}, + statSftp: async (_sftpId, path) => ( + path === "/remote/docs" && exists + ? { type: "directory", size: 0, lastModified: 1000 } + : null + ), + deleteSftp: async () => { exists = false; deletes += 1; }, + startStreamTransfer: async (payload) => { + streams += 1; + return { transferId: payload.transferId }; + }, + }, + joinPath: (base, name) => `${base}/${name}`, + useCompressedUpload: true, + resolveConflict: async () => { + conflictCalls += 1; + return "replace"; + }, + }, + ); + assert.equal(compressedStarts, 1, "replace stays on the regular path"); + assert.equal(conflictCalls, 1, "a pre-resolved conflict must not prompt twice"); + assert.equal(deletes, 1); + assert.equal(streams, 1); + assert.equal(replaced[0]?.success, true); + + supported = false; + conflictCalls = 0; + streams = 0; + const fallback = await uploadEntriesDirect( + [entry], + { + targetPath: "/remote", + sftpId: "sftp-1", + isLocal: false, + bridge: { + mkdirSftp: async () => {}, + statSftp: async () => ({ type: "directory", size: 0, lastModified: 1000 }), + startStreamTransfer: async (payload) => { + streams += 1; + return { transferId: payload.transferId }; + }, + }, + joinPath: (base, name) => `${base}/${name}`, + useCompressedUpload: true, + resolveConflict: async () => { + conflictCalls += 1; + return "merge"; + }, + }, + ); + assert.equal(conflictCalls, 1, "compression fallback must replay the selected action without another prompt"); + assert.equal(streams, 1); + assert.equal(fallback[0]?.success, true); +}); + +test("remote upload without a local path never buffers the whole File into renderer memory", async () => { + let arrayBufferCalls = 0; + let stagedFiles = 0; + let streamTransfers = 0; + const deleted: string[] = []; + const file = new File(["payload"], "memory-only.bin") as File & { + arrayBuffer: () => Promise; + }; + Object.defineProperty(file, "arrayBuffer", { + value: async () => { + arrayBufferCalls += 1; + return new ArrayBuffer(7); + }, + }); + + const results = await uploadEntriesDirect( + [{ file, relativePath: "memory-only.bin", isDirectory: false }], + { + targetPath: "/target", + sftpId: "sftp-1", + isLocal: false, + bridge: { + mkdirSftp: async () => {}, + stageUploadFile: async (stagedFile) => { + assert.equal(stagedFile, file); + stagedFiles += 1; + return "/netcatty-temp/memory-only.bin"; + }, + deleteTempFile: async (localPath) => { deleted.push(localPath); }, + startStreamTransfer: async (payload) => { + assert.equal(payload.sourcePath, "/netcatty-temp/memory-only.bin"); + streamTransfers += 1; + return { transferId: payload.transferId }; + }, + }, + joinPath: (base, name) => `${base}/${name}`, + }, + ); + + assert.equal(arrayBufferCalls, 0); + assert.equal(stagedFiles, 1); + assert.equal(streamTransfers, 1); + assert.deepEqual(deleted, ["/netcatty-temp/memory-only.bin"]); + assert.equal(results[0]?.success, true); +}); + +test("cancelling while a pathless file is being staged aborts before stream transfer", async () => { + const controller = new UploadController(); + let rejectStage: ((error: Error) => void) | undefined; + let streamTransfers = 0; + const file = new File(["large payload"], "large.bin"); + const uploading = uploadEntriesDirect( + [{ file, relativePath: "large.bin", isDirectory: false }], + { + targetPath: "/target", + sftpId: "sftp-1", + isLocal: false, + bridge: { + mkdirSftp: async () => {}, + stageUploadFile: async () => new Promise((_resolve, reject) => { + rejectStage = reject; + }), + cancelStagedUploadFile: async () => { + rejectStage?.(new Error("Upload staging cancelled")); + }, + deleteTempFile: async () => {}, + cancelTransfer: async () => {}, + startStreamTransfer: async (payload) => { + streamTransfers += 1; + return { transferId: payload.transferId }; + }, + }, + joinPath: (base, name) => `${base}/${name}`, + }, + controller, + ); + await new Promise((resolve) => setImmediate(resolve)); + await controller.cancel(); + const results = await uploading; + assert.equal(streamTransfers, 0); + assert.equal(results.some((result) => result.cancelled), true); +}); + test("does not replace an existing directory when uploading a same-named file", async () => { const file = new File(["local"], "dddd", { lastModified: 1234 }); const deletedPaths: string[] = []; @@ -159,8 +472,9 @@ test("does not replace an existing directory when uploading a same-named file", deleteSftp: async (_sftpId, path) => { deletedPaths.push(path); }, - writeSftpBinary: async (_sftpId, path) => { + startStreamTransfer: async ({ targetPath: path }) => { uploadedPaths.push(path); + return { transferId: "must-not-run" }; }, }, joinPath: (base, name) => `${base}/${name}`, @@ -200,7 +514,7 @@ test("counts apply-to-all upload conflicts by incoming and existing type", async } return null; }, - writeSftpBinary: async () => { + startStreamTransfer: async () => { throw new Error("skipped conflicts should not upload"); }, }, @@ -244,7 +558,6 @@ test("folder drag-drop creates a bundle task with a resolved local source path", bridge: { mkdirSftp: async () => {}, startStreamTransfer: async (payload) => ({ transferId: payload.transferId }), - writeSftpBinaryWithProgress: async () => ({ success: true, transferId: "x" }), }, joinPath: (base, name) => `${base}/${name}`, callbacks: { @@ -315,6 +628,59 @@ test("uploads path-backed clipboard files through stream transfer", async () => ]); }); +test("unified transfer events are the sole writer of file completion", async () => { + const created: string[] = []; + const completed: string[] = []; + const results = await uploadEntriesDirect( + [{ + file: null, + localPath: "/Users/me/Desktop/report.txt", + relativePath: "report.txt", + isDirectory: false, + size: 42, + }], + { + targetPath: "/target", + sftpId: "sftp-1", + isLocal: false, + bridge: { + managesTransferLifecycle: true, + mkdirSftp: async () => {}, + startStreamTransfer: async (payload) => ({ transferId: payload.transferId }), + }, + joinPath: (base, name) => `${base}/${name}`, + callbacks: { + onTaskCreated: (task) => created.push(task.id), + onTaskCompleted: (taskId) => completed.push(taskId), + }, + }, + ); + + assert.equal(results[0].success, true); + assert.equal(created.length, 1); + assert.deepEqual(completed, []); +}); + +test("a failed unified SFTP upload has no secondary upload path", async () => { + const file = new File(["payload"], "payload.txt"); + Object.defineProperty(file, "path", { value: "/tmp/payload.txt" }); + const results = await uploadEntriesDirect( + [{ file, relativePath: "payload.txt", isDirectory: false }], + { + targetPath: "/target", + sftpId: "sftp-1", + isLocal: false, + bridge: { + mkdirSftp: async () => {}, + startStreamTransfer: async () => ({ transferId: "stream", error: "stream failed" }), + }, + joinPath: (base, name) => `${base}/${name}`, + }, + ); + + assert.deepEqual(results, [{ fileName: "payload.txt", success: false, error: "stream failed" }]); +}); + test("copies path-backed clipboard files into local panes through stream transfer", async () => { const transfers: Array<{ sourcePath: string; targetPath: string; targetType: string; totalBytes?: number }> = []; diff --git a/application/state/useActiveChromeTheme.test.ts b/application/state/useActiveChromeTheme.test.ts index c17ee5d1e..51ecc6609 100644 --- a/application/state/useActiveChromeTheme.test.ts +++ b/application/state/useActiveChromeTheme.test.ts @@ -2,6 +2,9 @@ import assert from "node:assert/strict"; import test from "node:test"; import { + CHROME_THEME_CSS_CACHE_MAX_ENTRIES, + _cacheChromeThemeCssForTests, + _getChromeThemeCssCacheStatsForTests, scheduleChromeLayoutAnimation, syncActiveChromeTheme, themeFingerprint, @@ -101,3 +104,25 @@ test("active chrome theme foreground uses contrast calculation", () => { assert.match(source, /resolveReadableForegroundForHsl\(cursor\)/); assert.doesNotMatch(source, /cursorLightness > 55/); }); + +test("edited theme previews cannot grow the chrome CSS cache without bound", () => { + const base = TERMINAL_THEMES[0]; + assert.ok(base); + let latestFingerprint = ""; + for (let index = 0; index < CHROME_THEME_CSS_CACHE_MAX_ENTRIES * 2; index += 1) { + const theme = { + ...base, + id: `edited-preview-${index}`, + colors: { + ...base.colors, + background: `#${index.toString(16).padStart(6, "0")}`, + }, + }; + latestFingerprint = themeFingerprint(theme); + _cacheChromeThemeCssForTests(theme); + } + + const stats = _getChromeThemeCssCacheStatsForTests(); + assert.equal(stats.size, CHROME_THEME_CSS_CACHE_MAX_ENTRIES); + assert.equal(stats.keys.at(-1), latestFingerprint); +}); diff --git a/application/state/useActiveChromeTheme.ts b/application/state/useActiveChromeTheme.ts index c4555baad..83426eb31 100644 --- a/application/state/useActiveChromeTheme.ts +++ b/application/state/useActiveChromeTheme.ts @@ -101,6 +101,7 @@ function buildChromeCss(theme: TerminalTheme): string { } const cssCache = new Map(); +export const CHROME_THEME_CSS_CACHE_MAX_ENTRIES = 256; export function themeFingerprint(theme: TerminalTheme): string { return `${theme.id}\0${theme.type}\0${theme.colors.background}\0${theme.colors.foreground}\0${theme.colors.cursor}`; @@ -111,8 +112,19 @@ function getAppliedChromeFingerprint(): string | null { return document.documentElement.dataset.activeChromeTheme ?? null; } +function retainChromeCss(fingerprint: string, css: string): string { + cssCache.delete(fingerprint); + cssCache.set(fingerprint, css); + while (cssCache.size > CHROME_THEME_CSS_CACHE_MAX_ENTRIES) { + const oldestFingerprint = cssCache.keys().next().value as string | undefined; + if (oldestFingerprint === undefined) break; + cssCache.delete(oldestFingerprint); + } + return css; +} + for (const theme of TERMINAL_THEMES) { - cssCache.set(themeFingerprint(theme), buildChromeCss(theme)); + retainChromeCss(themeFingerprint(theme), buildChromeCss(theme)); } function getChromeCss(theme: TerminalTheme): string { @@ -120,9 +132,18 @@ function getChromeCss(theme: TerminalTheme): string { let css = cssCache.get(fingerprint); if (!css) { css = buildChromeCss(theme); - cssCache.set(fingerprint, css); + } else { + cssCache.delete(fingerprint); } - return css; + return retainChromeCss(fingerprint, css); +} + +export function _cacheChromeThemeCssForTests(theme: TerminalTheme): void { + getChromeCss(theme); +} + +export function _getChromeThemeCssCacheStatsForTests(): { size: number; keys: string[] } { + return { size: cssCache.size, keys: [...cssCache.keys()] }; } const STYLE_ID = "netcatty-active-chrome-theme"; diff --git a/application/state/usePluginContributions.ts b/application/state/usePluginContributions.ts index 4c51ca7f7..24220d485 100644 --- a/application/state/usePluginContributions.ts +++ b/application/state/usePluginContributions.ts @@ -1,5 +1,9 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { netcattyBridge } from '../../infrastructure/services/netcattyBridge'; +import { + getSharedPluginRuntimeStatus, + invalidateSharedPluginRuntimeStatus, +} from './pluginRuntimeStatusCache'; const EMPTY_SNAPSHOT: NetcattyPluginContributionSnapshot = Object.freeze({ locale: 'en', @@ -92,7 +96,9 @@ export interface UsePluginContributionsResult { export function usePluginContributions( query: NetcattyPluginContributionQuery = {}, + options: { enabled?: boolean } = {}, ): UsePluginContributionsResult { + const enabled = options.enabled !== false; const bridge = typeof window === 'undefined' ? undefined : netcattyBridge.get(); const queryKey = useMemo(() => JSON.stringify(query), [query]); const [loadedSnapshot, setLoadedSnapshot] = useState(() => ({ @@ -106,7 +112,7 @@ export function usePluginContributions( const refresh = useCallback(async () => { const isCurrent = refreshGuard.current.begin(); - if (!bridge?.getPluginRuntimeStatus || !bridge.getPluginContributions) { + if (!enabled || !bridge?.getPluginRuntimeStatus || !bridge.getPluginContributions) { if (!isCurrent()) return; setAvailable(false); setLoadedSnapshot({ queryKey, snapshot: EMPTY_SNAPSHOT }); @@ -114,7 +120,7 @@ export function usePluginContributions( return; } try { - const status = await bridge.getPluginRuntimeStatus(); + const status = await getSharedPluginRuntimeStatus(bridge); if (!isCurrent()) return; setAvailable(status.available); if (!status.available) { @@ -135,17 +141,21 @@ export function usePluginContributions( } finally { if (isCurrent()) setLoading(false); } - }, [bridge, queryKey]); + }, [bridge, enabled, queryKey]); useEffect(() => { const guard = refreshGuard.current; + if (!enabled) return () => guard.invalidate(); void refresh(); - const unsubscribe = bridge?.onPluginContributionsChanged?.(() => { void refresh(); }); + const unsubscribe = bridge?.onPluginContributionsChanged?.(() => { + invalidateSharedPluginRuntimeStatus(bridge); + void refresh(); + }); return () => { guard.invalidate(); unsubscribe?.(); }; - }, [bridge, refresh]); + }, [bridge, enabled, refresh]); const executeCommand = useCallback(async (command: string, args?: unknown, context?: Record) => { if (!bridge?.executePluginCommand) throw new Error('Plugin commands are unavailable'); diff --git a/application/state/useScriptRecorder.test.ts b/application/state/useScriptRecorder.test.ts new file mode 100644 index 000000000..cb086bc67 --- /dev/null +++ b/application/state/useScriptRecorder.test.ts @@ -0,0 +1,62 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import React from "react"; +import { act, create, type ReactTestRenderer } from "react-test-renderer"; + +import { + MAX_PENDING_SCRIPT_RECORDING_INPUT_CHARS, + SCRIPT_RECORDING_LIMIT_EVENT, + useScriptRecorder, +} from "./useScriptRecorder"; + +test("oversized unsubmitted recording input stops explicitly and preserves earlier steps", async (t) => { + const previousWindow = Object.getOwnPropertyDescriptor(globalThis, "window"); + const eventTarget = new EventTarget() as EventTarget & Record; + let stopCalls = 0; + Object.assign(eventTarget, { + netcatty: { + scriptRecordingStart: async () => ({ ok: true }), + scriptRecordingStop: async () => { + stopCalls += 1; + return { + steps: [{ type: "send", value: "kept" }], + code: "await nct.screen.sendLine('kept');", + }; + }, + }, + setInterval, + clearInterval, + }); + Object.defineProperty(globalThis, "window", { configurable: true, value: eventTarget }); + t.after(() => { + if (previousWindow) Object.defineProperty(globalThis, "window", previousWindow); + else Reflect.deleteProperty(globalThis, "window"); + }); + + let recorder: ReturnType | null = null; + let renderer: ReactTestRenderer | null = null; + let limitDetail: { sessionId: string; code: string } | null = null; + eventTarget.addEventListener(SCRIPT_RECORDING_LIMIT_EVENT, (event) => { + limitDetail = (event as CustomEvent<{ sessionId: string; code: string }>).detail; + }); + + function Probe() { + recorder = useScriptRecorder("session-1"); + return null; + } + + await act(async () => { renderer = create(React.createElement(Probe)); }); + await act(async () => { await recorder!.startRecording(); }); + assert.equal(recorder!.isRecording, true); + + await act(async () => { + recorder!.recordInput("x".repeat(MAX_PENDING_SCRIPT_RECORDING_INPUT_CHARS + 1)); + await new Promise((resolve) => setImmediate(resolve)); + }); + + assert.equal(stopCalls, 1); + assert.equal(recorder!.isRecording, false); + assert.equal(limitDetail?.sessionId, "session-1"); + assert.equal(limitDetail?.code, "await nct.screen.sendLine('kept');"); + await act(async () => renderer!.unmount()); +}); diff --git a/application/state/useScriptRecorder.ts b/application/state/useScriptRecorder.ts index c517c995a..661eb8e65 100644 --- a/application/state/useScriptRecorder.ts +++ b/application/state/useScriptRecorder.ts @@ -2,6 +2,17 @@ import { useCallback, useEffect, useRef, useState } from 'react'; import { netcattyBridge } from '@/infrastructure/services/netcattyBridge.ts'; import { DEFAULT_RECORDING_PROMPT_TIMEOUT_MS } from '@/domain/snippetScript.ts'; import type { ScriptRecordingStep } from '@/types/global/netcatty-bridge-script.d.ts'; +import { notify } from '../notification'; + +export const MAX_PENDING_SCRIPT_RECORDING_INPUT_CHARS = 256 * 1024; +export const SCRIPT_RECORDING_LIMIT_EVENT = 'netcatty:script:recording:limit'; + +type ScriptRecordingLimitDetail = { + sessionId: string; + error: string; + steps: ScriptRecordingStep[]; + code: string; +}; export function useScriptRecorder(sessionId: string | undefined) { const [isRecording, setIsRecording] = useState(false); @@ -58,6 +69,17 @@ export function useScriptRecorder(sessionId: string | undefined) { return result; }, []); + const finishAutomaticStop = useCallback((detail: ScriptRecordingLimitDetail) => { + isRecordingRef.current = false; + isPausedRef.current = false; + inputBufferRef.current = ''; + startedAtRef.current = null; + setIsRecording(false); + setIsPaused(false); + notify.error(detail.error, 'Scripts'); + window.dispatchEvent(new CustomEvent(SCRIPT_RECORDING_LIMIT_EVENT, { detail })); + }, []); + const pauseRecording = useCallback(() => { isPausedRef.current = true; setIsPaused(true); @@ -71,14 +93,48 @@ export function useScriptRecorder(sessionId: string | undefined) { const appendStep = useCallback(async (step: ScriptRecordingStep) => { const sid = sessionIdRef.current; if (!sid || !isRecordingRef.current || isPausedRef.current) return; - await netcattyBridge.get()?.scriptRecordingAppendStep?.(sid, step); + const result = await netcattyBridge.get()?.scriptRecordingAppendStep?.(sid, step); + if (result?.stopped) { + finishAutomaticStop({ + sessionId: sid, + error: result.error || 'Recording stopped because it reached the safety limit', + steps: result.steps ?? [], + code: result.code ?? '', + }); + return; + } lastStepAtRef.current = Date.now(); - }, []); + }, [finishAutomaticStop]); const recordInput = useCallback((data: string) => { if (!isRecordingRef.current || isPausedRef.current) return; - inputBufferRef.current += data; - }, []); + const next = `${inputBufferRef.current}${data}`; + if (next.length <= MAX_PENDING_SCRIPT_RECORDING_INPUT_CHARS) { + inputBufferRef.current = next; + return; + } + const sid = sessionIdRef.current; + if (!sid) return; + // Stop immediately before awaiting IPC so further key events cannot grow + // the renderer buffer while the partial recording is finalized. + isRecordingRef.current = false; + inputBufferRef.current = ''; + void netcattyBridge.get()?.scriptRecordingStop?.(sid).then((result) => { + finishAutomaticStop({ + sessionId: sid, + error: 'Recording stopped because the current input exceeded the safety limit', + steps: result.steps, + code: result.code, + }); + }).catch(() => { + finishAutomaticStop({ + sessionId: sid, + error: 'Recording stopped because the current input exceeded the safety limit', + steps: [], + code: '', + }); + }); + }, [finishAutomaticStop]); const recordBackspace = useCallback(() => { if (!isRecordingRef.current || isPausedRef.current) return; diff --git a/application/state/useSftpBackend.legacyExternalEdit.test.ts b/application/state/useSftpBackend.legacyExternalEdit.test.ts new file mode 100644 index 000000000..96483e7fa --- /dev/null +++ b/application/state/useSftpBackend.legacyExternalEdit.test.ts @@ -0,0 +1,21 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; + +test("useSftpBackend no longer exposes the retired external-edit orchestration", () => { + const source = readFileSync(new URL("./useSftpBackend.ts", import.meta.url), "utf8"); + + assert.doesNotMatch(source, /downloadSftpToTempAndOpen/); + assert.doesNotMatch(source, /registerTempFile|startFileWatch|openWithApplication|downloadSftpToTemp/); +}); + +test("the active external-edit path remains owned by useSftpExternalOperations", () => { + const source = readFileSync( + new URL("./sftp/useSftpExternalOperations.ts", import.meta.url), + "utf8", + ); + + assert.match(source, /downloadToTempAndOpen/); + assert.match(source, /registerTempFile/); + assert.match(source, /startFileWatch/); +}); diff --git a/application/state/useSftpBackend.ts b/application/state/useSftpBackend.ts index e44f6b35c..9f6e8840f 100644 --- a/application/state/useSftpBackend.ts +++ b/application/state/useSftpBackend.ts @@ -45,33 +45,6 @@ export const useSftpBackend = () => { return bridge.writeSftpBinary(sftpId, path, content, encoding); }, []); - const writeSftpBinaryWithProgress = useCallback( - async ( - sftpId: string, - path: string, - content: ArrayBuffer, - transferId: string, - encoding?: SftpFilenameEncoding, - onProgress?: (transferred: number, total: number, speed: number) => void, - onComplete?: () => void, - onError?: (error: string) => void, - ) => { - const bridge = netcattyBridge.get(); - if (!bridge?.writeSftpBinaryWithProgress) return undefined; - return bridge.writeSftpBinaryWithProgress( - sftpId, - path, - content, - transferId, - encoding, - onProgress, - onComplete, - onError, - ); - }, - [], - ); - const mkdirSftp = useCallback(async (sftpId: string, path: string, encoding?: SftpFilenameEncoding) => { const bridge = netcattyBridge.get(); if (!bridge?.mkdirSftp) throw new Error("mkdirSftp unavailable"); @@ -178,12 +151,6 @@ export const useSftpBackend = () => { return bridge.resumeTransfer(transferId); }, []); - const cancelSftpUpload = useCallback(async (transferId: string) => { - const bridge = netcattyBridge.get(); - if (!bridge?.cancelSftpUpload) return undefined; - return bridge.cancelSftpUpload(transferId); - }, []); - const selectApplication = useCallback(async () => { const bridge = netcattyBridge.get(); if (!bridge?.selectApplication) return undefined; @@ -205,48 +172,6 @@ export const useSftpBackend = () => { return bridge.selectDirectory(title, defaultPath); }; - const downloadSftpToTempAndOpen = useCallback(async ( - sftpId: string, - remotePath: string, - fileName: string, - appPath: string, - options?: { enableWatch?: boolean; encoding?: SftpFilenameEncoding } - ): Promise<{ localTempPath: string; watchId?: string }> => { - const bridge = netcattyBridge.get(); - if (!bridge?.downloadSftpToTemp || !bridge?.openWithApplication) { - throw new Error("Download to temp / open with unavailable"); - } - - // Download the file to temp - const tempPath = await bridge.downloadSftpToTemp(sftpId, remotePath, fileName, options?.encoding); - - // Register temp file for cleanup when SFTP session closes (regardless of auto-sync setting) - if (bridge.registerTempFile) { - try { - await bridge.registerTempFile(sftpId, tempPath); - } catch (err) { - console.warn("[SFTPBackend] Failed to register temp file for cleanup:", err); - } - } - - // Open with the selected application - await bridge.openWithApplication(tempPath, appPath); - - // Start file watching if enabled - let watchId: string | undefined; - if (options?.enableWatch && bridge.startFileWatch) { - try { - const result = await bridge.startFileWatch(tempPath, remotePath, sftpId, options?.encoding); - watchId = result.watchId; - } catch (err) { - console.warn("[SFTPBackend] Failed to start file watch:", err); - // Don't fail the operation if watching fails - } - } - - return { localTempPath: tempPath, watchId }; - }, []); - return { openSftp, closeSftp, @@ -255,7 +180,6 @@ export const useSftpBackend = () => { readSftpBinary, writeSftp, writeSftpBinary, - writeSftpBinaryWithProgress, mkdirSftp, deleteSftp, renameSftp, @@ -276,10 +200,8 @@ export const useSftpBackend = () => { cancelTransfer, pauseTransfer, resumeTransfer, - cancelSftpUpload, selectApplication, showSaveDialog, selectDirectory, - downloadSftpToTempAndOpen, }; }; diff --git a/application/state/useSftpState.closeTab.test.ts b/application/state/useSftpState.closeTab.test.ts new file mode 100644 index 000000000..5fdd54ee1 --- /dev/null +++ b/application/state/useSftpState.closeTab.test.ts @@ -0,0 +1,25 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +import { releaseSftpTabConnection } from "./useSftpState"; + +test("closing an SFTP tab releases its backend handle and connection metadata", async () => { + const sftpSessions = new Map([["connection-1", "sftp-1"]]); + const connectionCacheKeys = new Map([["connection-1", "endpoint-1"]]); + const cleared: string[] = []; + const closed: string[] = []; + + await releaseSftpTabConnection({ + connectionId: "connection-1", + isLocal: false, + sftpSessions, + connectionCacheKeys, + clearCacheForConnection: (id) => { cleared.push(id); }, + closeSftp: async (id) => { closed.push(id); }, + }); + + assert.deepEqual(closed, ["sftp-1"]); + assert.deepEqual(cleared, ["connection-1"]); + assert.equal(sftpSessions.size, 0); + assert.equal(connectionCacheKeys.size, 0); +}); diff --git a/application/state/useSftpState.ts b/application/state/useSftpState.ts index 3ba80b9bd..366f39bbc 100644 --- a/application/state/useSftpState.ts +++ b/application/state/useSftpState.ts @@ -3,7 +3,6 @@ import { Host, Identity, SftpFilenameEncoding, - SftpFileEntry, SSHKey, } from "../../domain/models"; import { @@ -23,14 +22,18 @@ import { isSessionError } from "./sftp/errors"; import { useSftpExternalOperations } from "./sftp/useSftpExternalOperations"; import { useSftpTransfers } from "./sftp/useSftpTransfers"; import { useSftpPaneActions } from "./sftp/useSftpPaneActions"; -import { useSftpConnections } from "./sftp/useSftpConnections"; +import { + releaseSftpConnectionMetadata, + useSftpConnections, +} from "./sftp/useSftpConnections"; +import { buildSftpHostCredentials } from "./sftp/useSftpHostCredentials"; import { useSftpFileWatch } from "./sftp/useSftpFileWatch"; import { useSftpSessionCleanup } from "./sftp/useSftpSessionCleanup"; import { useSftpSessionErrors } from "./sftp/useSftpSessionErrors"; import { ensureRemoteSftpSession } from "./sftp/ensureRemoteSftpSession"; import { openTransferSftpSession } from "./sftp/dedicatedTransferResume"; import { - buildTransferPoolKey, + createTransferPoolKeyCache, getSharedTransferConnectionPool, } from "./sftp/transferConnectionPool"; import { @@ -41,9 +44,21 @@ import { import { sftpTransferCenterStore } from "./sftpTransferCenterStore"; import { netcattyBridge } from "../../infrastructure/services/netcattyBridge"; import { logger } from "../../lib/logger"; +import type { DirectoryListingCache } from "./sftp/directoryListingCache"; // types + utils now live in ./sftp/* +export async function releaseSftpTabConnection(params: { + connectionId: string; + isLocal: boolean; + sftpSessions: Map; + connectionCacheKeys: Map; + clearCacheForConnection: (connectionId: string) => void; + closeSftp: (sftpId: string) => Promise; +}): Promise { + await releaseSftpConnectionMetadata(params); +} + export const useSftpState = ( hosts: Host[], keys: SSHKey[], @@ -93,9 +108,7 @@ export const useSftpState = ( // Directory listing cache (connectionId + path) const DIR_CACHE_TTL_MS = 10_000; - const dirCacheRef = useRef< - Map - >(new Map()); + const dirCacheRef = useRef(new Map()); // Navigation sequence per pane, used to ignore stale async results const navSeqRef = useRef<{ left: number; right: number }>({ @@ -217,28 +230,48 @@ export const useSftpState = ( } }, [leftTabsRef, rightTabsRef]); - const closeTabAndClearHost = useCallback((side: "left" | "right", tabId: string) => { + const closeTabAndClearHost = useCallback(async (side: "left" | "right", tabId: string) => { + const pane = getPaneByTabId(tabId); connectedHostByTabIdRef.current.delete(tabId); + if (pane?.connection) { + await releaseSftpTabConnection({ + connectionId: pane.connection.id, + isLocal: pane.connection.isLocal, + sftpSessions: sftpSessionsRef.current, + connectionCacheKeys: connectionCacheKeyMapRef.current, + clearCacheForConnection, + closeSftp: async (sftpId) => netcattyBridge.get()?.closeSftp(sftpId), + }); + } closeTab(side, tabId); - }, [closeTab]); + }, [clearCacheForConnection, closeTab, getPaneByTabId]); + + const releaseConnection = useCallback(async (connectionId: string) => { + await releaseSftpConnectionMetadata({ + connectionId, + sftpSessions: sftpSessionsRef.current, + connectionCacheKeys: connectionCacheKeyMapRef.current, + clearCacheForConnection, + closeSftp: async (sftpId) => netcattyBridge.get()?.closeSftp(sftpId), + }); + }, [clearCacheForConnection]); const handleSessionError = useSftpSessionErrors({ getActivePane, leftTabsRef, rightTabsRef, updateActiveTab, - sftpSessionsRef, - clearCacheForConnection, navSeqRef, lastConnectedHostRef, reconnectingRef, + releaseConnection, }); useSftpSessionCleanup(sftpSessionsRef); useSftpFileWatch(options); - // Transfer channel pool (max concurrent sftpIds per host). SSH keep-alive is - // owned by the main-process transport registry, not this pool. + // Transfer channel pool (max concurrent sftpIds per host, short idle reuse). + // Physical SSH transport ownership remains in the main-process registry. const transferPoolRef = useRef( getSharedTransferConnectionPool({ closeSession: async (sftpId) => { @@ -250,6 +283,15 @@ export const useSftpState = ( }, }), ); + const transferKnownHosts = options?.knownHosts; + const transferTerminalSettings = options?.terminalSettings; + // The cache is scoped to every input that can change resolved credentials. + const transferPoolKeyCache = useMemo( + () => createTransferPoolKeyCache(), + // eslint-disable-next-line react-hooks/exhaustive-deps + [hosts, identities, keys, transferKnownHosts, transferTerminalSettings], + ); + const resolveTransferSourceSessionId = options?.resolveTransferSourceSessionId; const openPoolSftpSession = useCallback( async (host: Host) => { @@ -257,7 +299,7 @@ export const useSftpState = ( // not repeated. Transport leases keep the shared conn alive after the // terminal tab closes until the transfer SFTP lease is returned. const sourceSessionId = !host.sftpSudo - ? options?.resolveTransferSourceSessionId?.(host.id, host) + ? resolveTransferSourceSessionId?.(host.id, host) : undefined; if (sourceSessionId) { try { @@ -270,36 +312,37 @@ export const useSftpState = ( hosts, keys, identities, - knownHosts: options?.knownHosts, - terminalSettings: options?.terminalSettings, + knownHosts: transferKnownHosts, + terminalSettings: transferTerminalSettings, }, { dedicated: false, sourceSessionId }, ); } catch (err) { logger.debug( - "[SFTP] Shared-transport transfer open failed; falling back to dedicated", + "[SFTP] Source-session transfer open failed; retrying through the pooled endpoint", err, ); } } - // Independent vault dial (reuseTransport:false inside openTransferSftpSession). - // Must not silently attach to a terminal/parked lease — restart resume and - // bulk I/O need a connection the transfer owns for the stream lifetime. - logger.info(`[SFTP] Opening dedicated transfer connection for ${host.label || host.hostname}`); + // No live terminal is required: open through the unified transport + // registry so the first file authenticates and later files/operations on + // the same endpoint reuse that transport. Restart resume remains the only + // dedicated path because it has a different lifecycle contract. + logger.info(`[SFTP] Opening pooled transfer connection for ${host.label || host.hostname}`); return openTransferSftpSession( host, { hosts, keys, identities, - knownHosts: options?.knownHosts, - terminalSettings: options?.terminalSettings, + knownHosts: transferKnownHosts, + terminalSettings: transferTerminalSettings, }, - { dedicated: true }, + { dedicated: false }, ); }, - [hosts, identities, keys, options?.knownHosts, options?.resolveTransferSourceSessionId, options?.terminalSettings], + [hosts, identities, keys, resolveTransferSourceSessionId, transferKnownHosts, transferTerminalSettings], ); const acquireTransferSession = useCallback( @@ -317,17 +360,25 @@ export const useSftpState = ( `Transfer connect host id mismatch: expected ${hostId}, got ${connectHost.id}`, ); } - const poolKey = buildTransferPoolKey({ + const poolKey = await transferPoolKeyCache.get(host, () => ({ hostId: host.id, hostname: host.hostname, port: host.port, username: host.username, protocol: host.protocol, sftpSudo: host.sftpSudo, - }); + connectionOptions: buildSftpHostCredentials({ + host, + hosts, + keys, + identities, + knownHosts: transferKnownHosts, + terminalSettings: transferTerminalSettings, + }), + })); return transferPoolRef.current.acquire(poolKey, transferId, () => openPoolSftpSession(host)); }, - [hosts, openPoolSftpSession], + [hosts, identities, keys, openPoolSftpSession, transferKnownHosts, transferPoolKeyCache, transferTerminalSettings], ); /** @@ -420,6 +471,7 @@ export const useSftpState = ( listLocalFiles, listRemoteFiles, handleSessionError, + releaseConnection, isSessionError, clearSelectionsExcept, dirCacheTtlMs: DIR_CACHE_TTL_MS, @@ -501,6 +553,7 @@ export const useSftpState = ( cancelExternalUpload, selectApplication, activeFileWatchCountRef, + releaseExternalFileWatches, uploadConflicts, resolveUploadConflict, } = useSftpExternalOperations({ @@ -539,6 +592,7 @@ export const useSftpState = ( resolveConnectedHost: (id) => connectedHostByTabIdRef.current.get(id) ?? null, resolveHostById: (hostId) => hosts.find((host) => host.id === hostId) ?? null, forceReconnect: ensureOptions?.forceReconnect, + releaseConnection, tabId, probeSession: async (sftpId) => { // Lightweight liveness check; any session-error from the bridge @@ -650,6 +704,7 @@ export const useSftpState = ( } return true; }, + releaseConnection, }); logger.info(`[SFTP] Restored browse session on ${side}`); } catch (err) { @@ -672,11 +727,12 @@ export const useSftpState = ( hosts, lastConnectedHostRef, sftpSessionsRef, + releaseConnection, ]); // Store methods in a ref to create stable wrapper functions // This prevents callback reference changes from causing re-renders in consumers - const methodsRef = useRef({ + const currentMethods = { getFilteredFiles, addTab, closeTab: closeTabAndClearHost, @@ -722,6 +778,7 @@ export const useSftpState = ( uploadExternalEntries, cancelExternalUpload, selectApplication, + releaseExternalFileWatches, startTransfer, downloadToLocal, cancelTransfer, @@ -740,69 +797,9 @@ export const useSftpState = ( acceptHostKeyVerification, acceptAndSaveHostKeyVerification, warmTransferPoolForHost, - }); - methodsRef.current = { - getFilteredFiles, - addTab, - closeTab: closeTabAndClearHost, - selectTab, - reorderTabs, - moveTabToOtherSide, - getTabsInfo, - getActiveTabId, - getActivePane, - connect, - disconnect, - navigateTo, - navigateUp, - refresh, - openEntry, - toggleSelection, - rangeSelect, - clearSelection, - clearSelectionsExcept, - selectAll, - setFilter, - setFilenameEncoding, - setShowHiddenFiles, - createDirectory, - createDirectoryAtPath, - createFile, - createFileAtPath, - deleteFiles, - deleteFilesAtPath, - renameFile, - renameFileAtPath, - moveEntriesToPath, - changePermissions, - readTextFile, - readBinaryFile, - writeTextFile, - writeTextFileByConnection, - downloadToTempAndOpen, - openWithSystemDefault, - uploadExternalFiles, - uploadExternalFileList, - uploadExternalFolderPath, - uploadExternalEntries, - cancelExternalUpload, - selectApplication, - startTransfer, - downloadToLocal, - cancelTransfer, - retryTransfer, - clearCompletedTransfers, - dismissTransfer, - resolveConflict: resolveAnyConflict, - getSftpIdForConnection, - getConnectionCacheKey, - setLastConnectedHost, - reportSessionError: handleSessionError, - rejectHostKeyVerification, - acceptHostKeyVerification, - acceptAndSaveHostKeyVerification, - warmTransferPoolForHost, }; + const methodsRef = useRef(currentMethods); + methodsRef.current = currentMethods; // Create stable method wrappers that call through methodsRef // These are created once and never change reference @@ -863,6 +860,8 @@ export const useSftpState = ( methodsRef.current.uploadExternalEntries(...args), cancelExternalUpload: (taskId?: string) => methodsRef.current.cancelExternalUpload(taskId), selectApplication: () => methodsRef.current.selectApplication(), + releaseExternalFileWatches: (...args: Parameters) => + methodsRef.current.releaseExternalFileWatches(...args), startTransfer: (...args: Parameters) => methodsRef.current.startTransfer(...args), downloadToLocal: (...args: Parameters) => methodsRef.current.downloadToLocal(...args), cancelTransfer: (...args: Parameters) => methodsRef.current.cancelTransfer(...args), diff --git a/components/AIChatSidePanelHelpers.test.tsx b/components/AIChatSidePanelHelpers.test.tsx index 09f992e7d..321f41144 100644 --- a/components/AIChatSidePanelHelpers.test.tsx +++ b/components/AIChatSidePanelHelpers.test.tsx @@ -272,3 +272,33 @@ test('SDK runtime model cache ignores degraded empty catalogs', async () => { })); assert.equal(emptyCache.read('opencode:/usr/bin/opencode'), null); }); + +test('SDK runtime model cache evicts expired catalogs', async () => { + let now = 1_000; + const cache = createSdkRuntimeModelCache({ ttlMs: 100, now: () => now }); + + await cache.refresh('opencode:/opt/bin/opencode', async () => ({ + currentModelId: 'openai/gpt-5.1', + models: [{ id: 'openai/gpt-5.1', name: 'GPT-5.1' }], + })); + assert.equal(cache.size(), 1); + + now = 1_101; + assert.equal(cache.read('opencode:/opt/bin/opencode'), null); + assert.equal(cache.size(), 0); +}); + +test('SDK runtime model cache stays bounded under agent configuration churn', async () => { + const cache = createSdkRuntimeModelCache({ maxEntries: 4 }); + + for (let index = 0; index < 20; index += 1) { + await cache.refresh(`agent:${index}`, async () => ({ + currentModelId: `model-${index}`, + models: [{ id: `model-${index}`, name: `Model ${index}` }], + })); + } + + assert.equal(cache.size(), 4); + assert.equal(cache.read('agent:0'), null); + assert.equal(cache.read('agent:19')?.currentModelId, 'model-19'); +}); diff --git a/components/AIChatSidePanelHelpers.ts b/components/AIChatSidePanelHelpers.ts index d72472833..02005327a 100644 --- a/components/AIChatSidePanelHelpers.ts +++ b/components/AIChatSidePanelHelpers.ts @@ -12,6 +12,7 @@ export type SdkRuntimeModelCacheEntry = SdkRuntimeModelCatalog & { type SdkRuntimeModelCacheOptions = { ttlMs?: number; + maxEntries?: number; now?: () => number; }; @@ -20,6 +21,7 @@ type SdkRuntimeModelRefreshOptions = { }; const SDK_RUNTIME_MODEL_CACHE_TTL_MS = 5 * 60 * 1000; +const SDK_RUNTIME_MODEL_CACHE_MAX_ENTRIES = 64; // Keep in sync with main-process SDK_MODEL_CACHE_ENV_KEYS: profile-affecting // env must bust the renderer cache so we re-query after OpenCode config switches. const MODEL_CACHE_ENV_HINTS = [ @@ -95,22 +97,50 @@ export function buildSdkRuntimeModelCacheKey(agent: { export function createSdkRuntimeModelCache(options: SdkRuntimeModelCacheOptions = {}) { const ttlMs = options.ttlMs ?? SDK_RUNTIME_MODEL_CACHE_TTL_MS; + const maxEntries = Math.max(1, options.maxEntries ?? SDK_RUNTIME_MODEL_CACHE_MAX_ENTRIES); const now = options.now ?? (() => Date.now()); const entries = new Map(); const inFlight = new Map>(); + const pruneEntries = () => { + const currentTime = now(); + for (const [key, entry] of entries) { + if (currentTime - entry.updatedAt >= ttlMs) { + entries.delete(key); + } + } + while (entries.size > maxEntries) { + const oldestKey = entries.keys().next().value; + if (oldestKey === undefined) break; + entries.delete(oldestKey); + } + }; + + const touchEntry = (key: string, entry: SdkRuntimeModelCacheEntry) => { + entries.delete(key); + entries.set(key, entry); + }; + return { read(key: string): SdkRuntimeModelCacheEntry | null { + pruneEntries(); const entry = entries.get(key); + if (entry) touchEntry(key, entry); return entry ? { ...cloneCatalog(entry), updatedAt: entry.updatedAt } : null; }, + size(): number { + pruneEntries(); + return entries.size; + }, refresh( key: string, load: () => Promise, refreshOptions: SdkRuntimeModelRefreshOptions = {}, ): Promise { + pruneEntries(); const cached = entries.get(key); if (!refreshOptions.force && cached && now() - cached.updatedAt < ttlMs) { + touchEntry(key, cached); return Promise.resolve(cloneCatalog(cached)); } @@ -123,7 +153,8 @@ export function createSdkRuntimeModelCache(options: SdkRuntimeModelCacheOptions if (normalized.models.length === 0 && !normalized.currentModelId) { return cached ? cloneCatalog(cached) : cloneCatalog(normalized); } - entries.set(key, { ...cloneCatalog(normalized), updatedAt: now() }); + touchEntry(key, { ...cloneCatalog(normalized), updatedAt: now() }); + pruneEntries(); return cloneCatalog(normalized); }) .finally(() => { diff --git a/components/SftpSidePanel.tsx b/components/SftpSidePanel.tsx index d77717181..495cd325f 100644 --- a/components/SftpSidePanel.tsx +++ b/components/SftpSidePanel.tsx @@ -69,6 +69,11 @@ import { shouldResetSftpSidePanelSourceSession, shouldSkipSftpSidePanelAutoConnect, } from "./sftp/sftpSidePanelAutoConnect"; +import { + pruneSftpSidePanelTabConnectionKeys, + recallSftpSidePanelPath, + rememberSftpSidePanelPath, +} from "./sftp/sftpSidePanelConnectionMemory"; import { listSftpConnectedHosts, resolveSftpTransferSourceSessionId, sftpPickerSessionsEqual } from "../domain/sftpConnectedHosts"; import type { TerminalSession } from "../domain/models"; @@ -422,6 +427,16 @@ const SftpSidePanelInner: React.FC = ({ const [interactiveWorkActive, setInteractiveWorkActive] = useState(false); const [sftpUiReady, setSftpUiReady] = useState(false); + useEffect(() => { + pruneSftpSidePanelTabConnectionKeys( + tabConnectionKeyMapRef.current, + [ + ...sftp.leftTabs.tabs.map((tab) => tab.id), + ...sftp.rightTabs.tabs.map((tab) => tab.id), + ], + ); + }, [sftp.leftTabs.tabs, sftp.rightTabs.tabs]); + const runAutoConnect = useCallback(() => { if (!activeHost) return; @@ -569,7 +584,8 @@ const SftpSidePanelInner: React.FC = ({ && activeTab.connection.currentPath && activeTabConnectionKey === connectionKey ) { - lastBrowsedPathByConnectionKeyRef.current.set( + rememberSftpSidePanelPath( + lastBrowsedPathByConnectionKeyRef.current, connectionKey, activeTab.connection.currentPath, ); @@ -624,7 +640,10 @@ const SftpSidePanelInner: React.FC = ({ ) ) ); - const rememberedPath = lastBrowsedPathByConnectionKeyRef.current.get(connectionKey); + const rememberedPath = recallSftpSidePanelPath( + lastBrowsedPathByConnectionKeyRef.current, + connectionKey, + ); const initialPath = resolveSftpAutoConnectPath({ explicitPath: initialLocation?.hostId === activeHost.id ? initialLocation.path : null, @@ -660,11 +679,8 @@ const SftpSidePanelInner: React.FC = ({ const connection = sftp.leftPane.connection; if (!connection || connection.status === "error" || connection.status === "disconnected") { connectedKeyRef.current = null; - if (sftp.activeFileWatchCountRef) { - sftp.activeFileWatchCountRef.current = 0; - } } - }, [sftp.leftPane.connection, sftp.leftPane.connection?.status, sftp.activeFileWatchCountRef]); + }, [sftp.leftPane.connection, sftp.leftPane.connection?.status]); useEffect(() => { if (!activeHost || !initialLocation) return; @@ -725,7 +741,11 @@ const SftpSidePanelInner: React.FC = ({ } tabConnectionKeyMapRef.current.set(sftp.leftPane.id, connectionKey); - lastBrowsedPathByConnectionKeyRef.current.set(connectionKey, connection.currentPath); + rememberSftpSidePanelPath( + lastBrowsedPathByConnectionKeyRef.current, + connectionKey, + connection.currentPath, + ); onCurrentPathChangeRef.current?.({ hostId: connection.hostId, connectionKey, diff --git a/components/Terminal.tsx b/components/Terminal.tsx index 0361012e9..9190ee9f6 100644 --- a/components/Terminal.tsx +++ b/components/Terminal.tsx @@ -94,7 +94,10 @@ import { TerminalToolbar } from "./terminal/TerminalToolbar"; import { ScriptRecordingIndicator } from "./terminal/ScriptRecordingIndicator"; import { ScriptSaveRecordingDialog } from "./scripts/ScriptSaveRecordingDialog"; import { registerScreenSnapshotProvider } from "@/infrastructure/scripts/screenSnapshotRegistry.ts"; -import { useScriptRecorder } from "@/application/state/useScriptRecorder.ts"; +import { + SCRIPT_RECORDING_LIMIT_EVENT, + useScriptRecorder, +} from "@/application/state/useScriptRecorder.ts"; import { getScriptRecordingSnapshot, setScriptRecordingState } from "@/application/state/scriptRecordingStore.ts"; import { runAutomationScript, @@ -2432,11 +2435,19 @@ const TerminalComponent: React.FC = ({ setSaveRecordingOpen(true); }); }; + const limitHandler = (event: Event) => { + const detail = (event as CustomEvent<{ sessionId?: string; code?: string }>).detail; + if (detail?.sessionId !== sessionId) return; + setRecordedCode(detail.code ?? ''); + setSaveRecordingOpen(true); + }; window.addEventListener('netcatty:script:recording:start', startHandler); window.addEventListener('netcatty:script:recording:stop', stopHandler); + window.addEventListener(SCRIPT_RECORDING_LIMIT_EVENT, limitHandler); return () => { window.removeEventListener('netcatty:script:recording:start', startHandler); window.removeEventListener('netcatty:script:recording:stop', stopHandler); + window.removeEventListener(SCRIPT_RECORDING_LIMIT_EVENT, limitHandler); }; }, [sessionId]); diff --git a/components/TerminalLayer.tsx b/components/TerminalLayer.tsx index 2f5b67d74..e55c329ec 100644 --- a/components/TerminalLayer.tsx +++ b/components/TerminalLayer.tsx @@ -80,6 +80,11 @@ import { resolveSidePanelToggleIntent } from '../application/state/resolveSidePa import { resolveAiSidePanelToggleIntent } from '../application/state/resolveAiSidePanelToggleIntent'; import { terminalLayerAreEqual } from './terminalLayerMemo'; import { TerminalLayerTabBridge } from './terminalLayer/TerminalLayerTabBridge'; +import { + clearTerminalSessionRuntimeState, + pruneTerminalTabMemoryState, + pruneTerminalSessionRuntimeState, +} from './terminalLayer/useTerminalLayerEffects'; import { sftpTransferCenterStore } from '../application/state/sftpTransferCenterStore'; import { SFTP_TRANSFER_HISTORY_RETENTION_MS, @@ -262,6 +267,15 @@ const TerminalLayerInner: React.FC = ({ const cwdProbeCancelersRef = useRef void>>(new Map()); const cwdProbeGenerationRef = useRef>(new Map()); + useEffect(() => { + pruneTerminalSessionRuntimeState({ + terminalRendererCwdBySessionRef, + terminalOsc7SignalBySessionRef, + cwdProbeGenerationRef, + cwdProbeCancelersRef, + }, new Set(sessions.map((session) => session.id))); + }, [sessions]); + useEffect(() => { const runPrewarm = () => prewarmAIStateStorageSnapshots(); if (typeof window.requestIdleCallback === 'function') { @@ -319,6 +333,12 @@ const TerminalLayerInner: React.FC = ({ // Stable callback references for Terminal components const handleCloseSession = useCallback((sessionId: string) => { + clearTerminalSessionRuntimeState({ + terminalRendererCwdBySessionRef, + terminalOsc7SignalBySessionRef, + cwdProbeGenerationRef, + cwdProbeCancelersRef, + }, sessionId); codingCliSignalController.forgetSession(sessionId); sessionCapabilitiesStore.delete(sessionId); onCloseSession(sessionId); @@ -954,10 +974,12 @@ const TerminalLayerInner: React.FC = ({ }, [onCommandExecuted]); useEffect(() => () => { - for (const cancel of cwdProbeCancelersRef.current.values()) { - cancel(); - } - cwdProbeCancelersRef.current.clear(); + pruneTerminalSessionRuntimeState({ + terminalRendererCwdBySessionRef, + terminalOsc7SignalBySessionRef, + cwdProbeGenerationRef, + cwdProbeCancelersRef, + }, new Set()); }, []); const sessionSudoAutofillPasswordsMap = useMemo(() => { const map = new Map(); @@ -1014,6 +1036,14 @@ const TerminalLayerInner: React.FC = ({ return ids; }, [sessions, workspaces]); + useEffect(() => { + pruneTerminalTabMemoryState({ + lastSidePanelTabRef, + notesReturnTabRef, + sftpLastPathForSourceRef, + }, validAIScopeTargetIds); + }, [validAIScopeTargetIds]); + useEffect(() => { const storeActiveTransferTabIds = listTerminalTabIdsWithRetainingTransfers( sftpTransferCenterStore.getSnapshot().tasks, diff --git a/components/ai/ChatMessageList.test.tsx b/components/ai/ChatMessageList.test.tsx index 26c3d40b6..5a2605c0f 100644 --- a/components/ai/ChatMessageList.test.tsx +++ b/components/ai/ChatMessageList.test.tsx @@ -8,6 +8,7 @@ import type { ChatMessage } from "../../infrastructure/ai/types.ts"; import type { ApprovalRequest } from "../../infrastructure/ai/shared/approvalGate.ts"; import ChatMessageList, { buildCodexApprovalRenderPlan, + pruneResolvedApprovals, shouldProvideVaultArtifactNavigation, } from "./ChatMessageList.tsx"; import { TooltipProvider } from "../ui/tooltip.tsx"; @@ -19,6 +20,22 @@ const makeMessage = (index: number): ChatMessage => ({ timestamp: index, }); +test("resolved approval state retains only tool calls still present in messages", () => { + const previous = new Map(); + for (let index = 0; index < 1_000; index += 1) previous.set(`old-${index}`, true); + previous.set("live-call", false); + const messages: ChatMessage[] = [{ + id: "assistant-live", + role: "assistant", + content: "", + timestamp: 1, + toolCalls: [{ id: "live-call", name: "terminal_execute", arguments: {} }], + }]; + + const next = pruneResolvedApprovals(previous, messages); + assert.deepEqual([...next], [["live-call", false]]); +}); + test("ChatMessageList only renders the recent message batch by default", () => { const messages = Array.from({ length: 60 }, (_value, index) => makeMessage(index)); diff --git a/components/ai/ChatMessageList.tsx b/components/ai/ChatMessageList.tsx index 441361b84..4f2baf653 100644 --- a/components/ai/ChatMessageList.tsx +++ b/components/ai/ChatMessageList.tsx @@ -128,6 +128,21 @@ export function buildCodexApprovalRenderPlan( const MESSAGE_RENDER_BATCH = 50; const MESSAGE_RENDER_STEP = 50; +export function pruneResolvedApprovals( + previous: ReadonlyMap, + messages: readonly ChatMessage[], +): Map { + const visibleToolCallIds = new Set(); + for (const message of messages) { + for (const toolCall of message.toolCalls ?? []) visibleToolCallIds.add(toolCall.id); + } + const next = new Map(); + for (const [toolCallId, approved] of previous) { + if (visibleToolCallIds.has(toolCallId)) next.set(toolCallId, approved); + } + return next; +} + const ChatMessageList: React.FC = ({ messages, isStreaming, @@ -146,6 +161,10 @@ const ChatMessageList: React.FC = ({ const [resolvedApprovals, setResolvedApprovals] = useState>(new Map()); const [pendingCodexInteractions, setPendingCodexInteractions] = useState>(new Map()); + useEffect(() => { + setResolvedApprovals((previous) => pruneResolvedApprovals(previous, messages)); + }, [activeSessionId, messages]); + // Subscribe to approval gate events (SDK + MCP tool calls) useEffect(() => { const handler = (request: ApprovalRequest) => { diff --git a/components/ai/scopedHistorySessions.test.ts b/components/ai/scopedHistorySessions.test.ts index de02374af..6a028602d 100644 --- a/components/ai/scopedHistorySessions.test.ts +++ b/components/ai/scopedHistorySessions.test.ts @@ -2,7 +2,10 @@ import assert from "node:assert/strict"; import test from "node:test"; import type { AISession } from "../../infrastructure/ai/types.ts"; -import { getScopedHistorySessions } from "./scopedHistorySessions.ts"; +import { + _getScopedHistoryCacheSizeForTests, + getScopedHistorySessions, +} from "./scopedHistorySessions.ts"; function createSession( id: string, @@ -110,3 +113,38 @@ test("same-scope fallback excludes sessions already displayed by another termina [], ); }); + +test("workspace cache ignores unrelated active terminal churn", () => { + const sessions = [createSession("workspace", { type: "workspace", targetId: "workspace-1" }, 1)]; + const first = getScopedHistorySessions( + sessions, + "workspace", + "workspace-1", + undefined, + new Set(["terminal-0"]), + ); + for (let index = 1; index < 1_000; index += 1) { + assert.equal(getScopedHistorySessions( + sessions, + "workspace", + "workspace-1", + undefined, + new Set([`terminal-${index}`]), + ), first); + } + assert.equal(_getScopedHistoryCacheSizeForTests(sessions), 1); +}); + +test("terminal scoped history cache has a hard LRU bound", () => { + const sessions = [createSession("terminal", { type: "terminal", targetId: "terminal-current" }, 1)]; + for (let index = 0; index < 1_000; index += 1) { + getScopedHistorySessions( + sessions, + "terminal", + "terminal-current", + undefined, + new Set([`other-session-${index}`]), + ); + } + assert.ok(_getScopedHistoryCacheSizeForTests(sessions) <= 64); +}); diff --git a/components/ai/scopedHistorySessions.ts b/components/ai/scopedHistorySessions.ts index 517945d85..4182047aa 100644 --- a/components/ai/scopedHistorySessions.ts +++ b/components/ai/scopedHistorySessions.ts @@ -2,6 +2,7 @@ import type { AISession } from '../../infrastructure/ai/types'; import { getSessionScopeMatchRank } from './sessionScopeMatch'; type HistoryCacheKey = string; +const MAX_HISTORY_CACHE_ENTRIES_PER_SESSION_LIST = 64; const historyCache = new WeakMap>(); function buildHistoryCacheKey( @@ -10,8 +11,10 @@ function buildHistoryCacheKey( scopeHostIds: string[] | undefined, activeTerminalSessionIds: Set, ): HistoryCacheKey { - const hostKey = scopeHostIds?.join(',') ?? ''; - const terminalKey = [...activeTerminalSessionIds].sort().join(','); + const hostKey = scopeHostIds ? [...scopeHostIds].sort().join(',') : ''; + const terminalKey = scopeType === 'terminal' + ? [...activeTerminalSessionIds].sort().join(',') + : ''; return `${scopeType}:${scopeTargetId ?? ''}:${hostKey}:${terminalKey}`; } @@ -36,6 +39,8 @@ export function getScopedHistorySessions( ); const cached = scopeCache.get(cacheKey); if (cached) { + scopeCache.delete(cacheKey); + scopeCache.set(cacheKey, cached); return cached; } @@ -55,5 +60,14 @@ export function getScopedHistorySessions( .map(({ session }) => session); scopeCache.set(cacheKey, result); + while (scopeCache.size > MAX_HISTORY_CACHE_ENTRIES_PER_SESSION_LIST) { + const oldestKey = scopeCache.keys().next().value; + if (oldestKey == null) break; + scopeCache.delete(oldestKey); + } return result; } + +export function _getScopedHistoryCacheSizeForTests(sessions: AISession[]): number { + return historyCache.get(sessions)?.size ?? 0; +} diff --git a/components/sftp/hooks/useSftpViewFileOps.ts b/components/sftp/hooks/useSftpViewFileOps.ts index 5c6608cc8..43d5edc3a 100644 --- a/components/sftp/hooks/useSftpViewFileOps.ts +++ b/components/sftp/hooks/useSftpViewFileOps.ts @@ -10,6 +10,7 @@ import { editorTabStore } from "../../../application/state/editorTabStore"; import { toEditorTabId, activeTabStore } from "../../../application/state/activeTabStore"; import type { TextEditorModalSnapshot } from "../../TextEditorModal"; import type { UseSftpViewFileOpsParams, UseSftpViewFileOpsResult } from "./useSftpViewFileOps.types"; +import { assertSftpFileFitsBuiltinEditor } from "../sftpEditorFileLimits"; export const useSftpViewFileOps = ({ sftpRef, @@ -87,6 +88,7 @@ export const useSftpViewFileOps = ({ const resolvedFullPath = fullPath ?? sftpRef.current.joinPath(pane.connection.currentPath, file.name); try { + assertSftpFileFitsBuiltinEditor(file.size); setLoadingTextContent(true); setTextEditorTarget({ file, side, fullPath: resolvedFullPath, hostId: pane.connection.hostId }); diff --git a/components/sftp/hooks/useSftpViewTabs.ts b/components/sftp/hooks/useSftpViewTabs.ts index 9dab04422..cabb87638 100644 --- a/components/sftp/hooks/useSftpViewTabs.ts +++ b/components/sftp/hooks/useSftpViewTabs.ts @@ -97,7 +97,7 @@ export const useSftpViewTabs = ({ sftp, sftpRef, hosts = [] }: UseSftpViewTabsPa const ok = await confirmCloseEditorTabsByConnection(connectionId); if (!ok) return; } - sftpRef.current.closeTab(side, tabId); + await sftpRef.current.closeTab(side, tabId); }, [confirmCloseEditorTabsByConnection, sftpRef]); const handleCloseTabLeft = useCallback((tabId: string) => ( diff --git a/components/sftp/sftpEditorFileLimits.test.ts b/components/sftp/sftpEditorFileLimits.test.ts new file mode 100644 index 000000000..bcc7e50fe --- /dev/null +++ b/components/sftp/sftpEditorFileLimits.test.ts @@ -0,0 +1,12 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +import { assertSftpFileFitsBuiltinEditor, MAX_BUILTIN_SFTP_EDITOR_BYTES } from "./sftpEditorFileLimits.ts"; + +test("built-in editor rejects oversized files before reading them", () => { + assert.doesNotThrow(() => assertSftpFileFitsBuiltinEditor(MAX_BUILTIN_SFTP_EDITOR_BYTES)); + assert.throws( + () => assertSftpFileFitsBuiltinEditor(MAX_BUILTIN_SFTP_EDITOR_BYTES + 1), + /too large.*10 MB/i, + ); +}); diff --git a/components/sftp/sftpEditorFileLimits.ts b/components/sftp/sftpEditorFileLimits.ts new file mode 100644 index 000000000..cf9db54e2 --- /dev/null +++ b/components/sftp/sftpEditorFileLimits.ts @@ -0,0 +1,6 @@ +export const MAX_BUILTIN_SFTP_EDITOR_BYTES = 10 * 1024 * 1024; + +export function assertSftpFileFitsBuiltinEditor(size: number | undefined): void { + if (!Number.isFinite(size) || (size ?? 0) <= MAX_BUILTIN_SFTP_EDITOR_BYTES) return; + throw new Error("This file is too large for the built-in editor (maximum 10 MB). Download it or open it with another app."); +} diff --git a/components/sftp/sftpSidePanelConnectionMemory.test.ts b/components/sftp/sftpSidePanelConnectionMemory.test.ts new file mode 100644 index 000000000..bcbba97cd --- /dev/null +++ b/components/sftp/sftpSidePanelConnectionMemory.test.ts @@ -0,0 +1,47 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { + MAX_SFTP_SIDE_PANEL_REMEMBERED_PATHS, + pruneSftpSidePanelTabConnectionKeys, + recallSftpSidePanelPath, + rememberSftpSidePanelPath, +} from "./sftpSidePanelConnectionMemory"; + +test("closed SFTP side-panel tabs release their remembered connection keys", () => { + const connectionKeys = new Map(); + for (let index = 0; index < 100; index += 1) { + connectionKeys.set(`tab-${index}`, `connection-${index}`); + } + + pruneSftpSidePanelTabConnectionKeys(connectionKeys, ["tab-97", "tab-98", "tab-99"]); + + assert.deepEqual([...connectionKeys], [ + ["tab-97", "connection-97"], + ["tab-98", "connection-98"], + ["tab-99", "connection-99"], + ]); +}); + +test("SFTP side-panel path memory stays bounded during endpoint churn", () => { + const paths = new Map(); + for (let index = 0; index < 100; index += 1) { + rememberSftpSidePanelPath(paths, `endpoint-${index}`, `/path/${index}`); + } + + assert.equal(paths.size, MAX_SFTP_SIDE_PANEL_REMEMBERED_PATHS); + assert.equal(paths.has("endpoint-0"), false); + assert.equal(paths.get("endpoint-99"), "/path/99"); +}); + +test("reading a remembered SFTP path keeps that endpoint in the LRU", () => { + const paths = new Map(); + rememberSftpSidePanelPath(paths, "endpoint-a", "/a", 3); + rememberSftpSidePanelPath(paths, "endpoint-b", "/b", 3); + rememberSftpSidePanelPath(paths, "endpoint-c", "/c", 3); + + assert.equal(recallSftpSidePanelPath(paths, "endpoint-a"), "/a"); + rememberSftpSidePanelPath(paths, "endpoint-d", "/d", 3); + + assert.equal(paths.has("endpoint-a"), true); + assert.equal(paths.has("endpoint-b"), false); +}); diff --git a/components/sftp/sftpSidePanelConnectionMemory.ts b/components/sftp/sftpSidePanelConnectionMemory.ts new file mode 100644 index 000000000..7a2c1dd12 --- /dev/null +++ b/components/sftp/sftpSidePanelConnectionMemory.ts @@ -0,0 +1,39 @@ +export const MAX_SFTP_SIDE_PANEL_REMEMBERED_PATHS = 32; + +export function pruneSftpSidePanelTabConnectionKeys( + connectionKeys: Map, + activeTabIds: Iterable, +): void { + const activeIds = new Set(activeTabIds); + for (const tabId of connectionKeys.keys()) { + if (!activeIds.has(tabId)) { + connectionKeys.delete(tabId); + } + } +} + +export function rememberSftpSidePanelPath( + paths: Map, + connectionKey: string, + remotePath: string, + limit = MAX_SFTP_SIDE_PANEL_REMEMBERED_PATHS, +): void { + paths.delete(connectionKey); + paths.set(connectionKey, remotePath); + while (paths.size > limit) { + const oldestKey = paths.keys().next().value; + if (oldestKey === undefined) break; + paths.delete(oldestKey); + } +} + +export function recallSftpSidePanelPath( + paths: Map, + connectionKey: string, +): string | undefined { + const remotePath = paths.get(connectionKey); + if (remotePath === undefined) return undefined; + paths.delete(connectionKey); + paths.set(connectionKey, remotePath); + return remotePath; +} diff --git a/components/systemManager/ProcessManagerTab.tsx b/components/systemManager/ProcessManagerTab.tsx index 9313db0c4..49205ea8c 100644 --- a/components/systemManager/ProcessManagerTab.tsx +++ b/components/systemManager/ProcessManagerTab.tsx @@ -34,6 +34,10 @@ import { import { SystemPanelConfirmDialog } from './SystemPanelConfirmDialog'; import { SystemPanelPromptDialog } from './SystemPanelPromptDialog'; import { usePolling, useStableTranslate } from './hooks/useSystemManager'; +import { + getCachedProcessList, + setCachedProcessList, +} from './processListCache'; type Backend = ReturnType; type SortKey = 'cpuPercent' | 'memPercent' | 'pid' | 'command' | 'user'; @@ -54,16 +58,10 @@ function processSignalTitleKey(signal: ProcessSignal): string { } } -const PROCESS_CACHE_TTL_MS = 30_000; const PROCESS_ROW_HEIGHT = 56; const PROCESS_DETAIL_HEIGHT = 112; const PROCESS_OVERSCAN_ROWS = 8; -const processListCache = new Map(); - const SORT_OPTIONS: Array<{ key: SortKey; labelKey: string }> = [ { key: 'cpuPercent', labelKey: 'systemManager.processes.sort.cpu' }, { key: 'memPercent', labelKey: 'systemManager.processes.sort.mem' }, @@ -87,16 +85,6 @@ const mergeProcesses = ( next: SystemProcessInfo[], ) => mergePollListByKey(prev, next, (p) => p.pid, systemProcessInfoEqual); -function getCachedProcesses(sessionId: string): SystemProcessInfo[] | null { - const cached = processListCache.get(sessionId); - if (!cached) return null; - if (Date.now() - cached.updatedAt > PROCESS_CACHE_TTL_MS) { - processListCache.delete(sessionId); - return null; - } - return cached.processes; -} - const ProcessListLoading = memo(function ProcessListLoading({ message, }: { @@ -274,7 +262,7 @@ export const ProcessManagerTab = memo(function ProcessManagerTab({ const [pendingSignal, setPendingSignal] = useState(null); const [signalBusy, setSignalBusy] = useState(false); const [actionError, setActionError] = useState(null); - const [cachedProcesses, setCachedProcesses] = useState(() => getCachedProcesses(sessionId)); + const [cachedProcesses, setCachedProcesses] = useState(() => getCachedProcessList(sessionId)); const [cachedProcessesSessionId, setCachedProcessesSessionId] = useState(sessionId); const [processListPending, setProcessListPending] = useState(false); const processFetchGenerationRef = useRef(0); @@ -287,7 +275,7 @@ export const ProcessManagerTab = memo(function ProcessManagerTab({ useEffect(() => { processFetchGenerationRef.current += 1; - setCachedProcesses(getCachedProcesses(sessionId)); + setCachedProcesses(getCachedProcessList(sessionId)); setCachedProcessesSessionId(sessionId); setProcessListPending(false); // Drop in-flight dialogs so a confirm cannot act on a different host/session. @@ -339,14 +327,14 @@ export const ProcessManagerTab = memo(function ProcessManagerTab({ useEffect(() => { if (!processes) return; - processListCache.set(sessionId, { processes, updatedAt: Date.now() }); + setCachedProcessList(sessionId, processes); setCachedProcesses(processes); setCachedProcessesSessionId(sessionId); }, [processes, sessionId]); const sessionCachedProcesses = cachedProcessesSessionId === sessionId ? cachedProcesses - : getCachedProcesses(sessionId); + : getCachedProcessList(sessionId); const visibleProcesses = processes ?? sessionCachedProcesses; const showingCachedProcesses = processes === null && sessionCachedProcesses !== null; diff --git a/components/systemManager/processListCache.test.ts b/components/systemManager/processListCache.test.ts new file mode 100644 index 000000000..9907b2487 --- /dev/null +++ b/components/systemManager/processListCache.test.ts @@ -0,0 +1,67 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import type { SystemProcessInfo } from '../../domain/systemManager/types'; +import { + PROCESS_LIST_CACHE_MAX_ROWS, + PROCESS_LIST_CACHE_MAX_SESSIONS, + clearCachedProcessList, + getCachedProcessList, + getProcessListCacheStatsForTests, + resetProcessListCacheForTests, + setCachedProcessList, +} from './processListCache'; + +const processRow = (pid: number): SystemProcessInfo => ({ + pid, + ppid: 1, + user: 'root', + stat: 'S', + command: `process-${pid}`, + cpuPercent: 0, + memPercent: 0, + rssKb: 1, + vszKb: 1, + elapsed: '0:01', +}); + +test.beforeEach(resetProcessListCacheForTests); +test.afterEach(resetProcessListCacheForTests); + +test('process list cache stays bounded across session churn and evicts least-recently-used entries', () => { + const rows = [processRow(1), processRow(2)]; + const now = Date.now(); + for (let index = 0; index < PROCESS_LIST_CACHE_MAX_SESSIONS; index += 1) { + setCachedProcessList(`session-${index}`, rows, now + index); + } + assert.ok(getCachedProcessList('session-0')); + setCachedProcessList('session-overflow', rows, now + PROCESS_LIST_CACHE_MAX_SESSIONS + 1); + + const stats = getProcessListCacheStatsForTests(); + assert.equal(stats.sessions, PROCESS_LIST_CACHE_MAX_SESSIONS); + assert.ok(stats.sessionIds.includes('session-0')); + assert.ok(!stats.sessionIds.includes('session-1')); +}); + +test('process list cache enforces a total row budget and rejects one oversized response', () => { + const halfBudget = Array.from( + { length: Math.floor(PROCESS_LIST_CACHE_MAX_ROWS / 2) + 1 }, + (_, index) => processRow(index), + ); + setCachedProcessList('left', halfBudget, 1); + setCachedProcessList('right', halfBudget, 2); + assert.ok(getProcessListCacheStatsForTests().rows <= PROCESS_LIST_CACHE_MAX_ROWS); + + setCachedProcessList( + 'oversized', + Array.from({ length: PROCESS_LIST_CACHE_MAX_ROWS + 1 }, (_, index) => processRow(index)), + 3, + ); + assert.equal(getCachedProcessList('oversized'), null); +}); + +test('process list cache releases a closed or unmounted session immediately', () => { + setCachedProcessList('closed-session', [processRow(1)]); + clearCachedProcessList('closed-session'); + assert.equal(getCachedProcessList('closed-session'), null); + assert.equal(getProcessListCacheStatsForTests().sessions, 0); +}); diff --git a/components/systemManager/processListCache.ts b/components/systemManager/processListCache.ts new file mode 100644 index 000000000..fc85543ed --- /dev/null +++ b/components/systemManager/processListCache.ts @@ -0,0 +1,76 @@ +import type { SystemProcessInfo } from '../../domain/systemManager/types'; + +export const PROCESS_LIST_CACHE_TTL_MS = 30_000; +export const PROCESS_LIST_CACHE_MAX_SESSIONS = 16; +export const PROCESS_LIST_CACHE_MAX_ROWS = 20_000; + +type ProcessListCacheEntry = { + processes: SystemProcessInfo[]; + updatedAt: number; +}; + +const processListCache = new Map(); + +function pruneExpiredProcessLists(now = Date.now()): void { + for (const [sessionId, entry] of processListCache) { + if (now - entry.updatedAt > PROCESS_LIST_CACHE_TTL_MS) { + processListCache.delete(sessionId); + } + } +} + +function enforceProcessListCacheLimits(): void { + let totalRows = 0; + for (const entry of processListCache.values()) totalRows += entry.processes.length; + while ( + processListCache.size > PROCESS_LIST_CACHE_MAX_SESSIONS + || totalRows > PROCESS_LIST_CACHE_MAX_ROWS + ) { + const oldestSessionId = processListCache.keys().next().value as string | undefined; + if (!oldestSessionId) break; + const oldest = processListCache.get(oldestSessionId); + processListCache.delete(oldestSessionId); + totalRows -= oldest?.processes.length ?? 0; + } +} + +export function getCachedProcessList(sessionId: string): SystemProcessInfo[] | null { + pruneExpiredProcessLists(); + const cached = processListCache.get(sessionId); + if (!cached) return null; + processListCache.delete(sessionId); + processListCache.set(sessionId, cached); + return cached.processes; +} + +export function setCachedProcessList( + sessionId: string, + processes: SystemProcessInfo[], + now = Date.now(), +): void { + pruneExpiredProcessLists(now); + processListCache.delete(sessionId); + if (processes.length > PROCESS_LIST_CACHE_MAX_ROWS) return; + processListCache.set(sessionId, { processes, updatedAt: now }); + enforceProcessListCacheLimits(); +} + +export function clearCachedProcessList(sessionId: string): void { + processListCache.delete(sessionId); +} + +export function resetProcessListCacheForTests(): void { + processListCache.clear(); +} + +export function getProcessListCacheStatsForTests(): { + sessions: number; + rows: number; + sessionIds: string[]; +} { + return { + sessions: processListCache.size, + rows: [...processListCache.values()].reduce((sum, entry) => sum + entry.processes.length, 0), + sessionIds: [...processListCache.keys()], + }; +} diff --git a/components/terminal/TerminalContextMenu.tsx b/components/terminal/TerminalContextMenu.tsx index a3fe536fc..3f83fe039 100644 --- a/components/terminal/TerminalContextMenu.tsx +++ b/components/terminal/TerminalContextMenu.tsx @@ -168,6 +168,7 @@ export const TerminalContextMenu: React.FC = ({ onDetach, }) => { const { t } = useI18n(); + const [menuOpen, setMenuOpen] = useState(false); const terminalContext = buildTerminalPluginContributionContext({ surface: 'terminal/context', sessionId, @@ -179,7 +180,10 @@ export const TerminalContextMenu: React.FC = ({ alternateScreen: isAlternateScreen, reconnectable: Boolean(isReconnectable), }); - const pluginContributions = usePluginContributions({ context: terminalContext }); + const pluginContributions = usePluginContributions( + { context: terminalContext }, + { enabled: menuOpen }, + ); const pluginMenus = collectOwnedPluginMenus(pluginContributions.snapshot.plugins) .filter((menu) => menu.location === 'terminal/context' && menu.visible) .sort(comparePluginMenus); @@ -191,6 +195,7 @@ export const TerminalContextMenu: React.FC = ({ const [allowSuppressedMenuContent, setAllowSuppressedMenuContent] = useState(false); const handleOpenChange = useCallback((open: boolean) => { + setMenuOpen(open); if (!open) { markedPaneRef.current?.removeAttribute('data-menu-open'); markedPaneRef.current = null; diff --git a/components/terminal/TerminalView.serverStatsVisibility.test.ts b/components/terminal/TerminalView.serverStatsVisibility.test.ts new file mode 100644 index 000000000..2ea280e11 --- /dev/null +++ b/components/terminal/TerminalView.serverStatsVisibility.test.ts @@ -0,0 +1,12 @@ +import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; +import test from "node:test"; + +test("hidden terminal tabs stop server stats polling", () => { + const source = readFileSync(new URL("./TerminalView.tsx", import.meta.url), "utf8"); + assert.match( + source, + / ( + value.length <= MAX_INCOMPLETE_TERMINAL_CONTROL_SEQUENCE_CHARS +); diff --git a/components/terminal/runtime/terminalLineTimestamps.test.ts b/components/terminal/runtime/terminalLineTimestamps.test.ts index e8a30823e..1d6b75957 100644 --- a/components/terminal/runtime/terminalLineTimestamps.test.ts +++ b/components/terminal/runtime/terminalLineTimestamps.test.ts @@ -17,6 +17,7 @@ import { writeTerminalDataWithLineTimestamps, type StampCursorEstimate, } from "./terminalLineTimestamps.ts"; +import { MAX_INCOMPLETE_TERMINAL_CONTROL_SEQUENCE_CHARS } from "./terminalControlSequenceLimits.ts"; const createFakeTerm = (options: { cols?: number; @@ -633,6 +634,24 @@ test("does not withhold output when an OSC sequence is split across chunks", () assert.ok(writes.join("").includes("alice@server:~$ ")); }); +test("drops an oversized unterminated control prefix and resumes timestamp parsing", () => { + const segmenter = createTerminalLineTimestampSegmenter({ + now: () => new Date(2026, 5, 6, 12, 0, 0), + }); + const malformed = `\x1b[?1049${"9".repeat(MAX_INCOMPLETE_TERMINAL_CONTROL_SEQUENCE_CHARS)}`; + + const overflow = segmenter.append(malformed); + assert.equal(segmenter.flushPendingEscapeSequence(), ""); + assert.equal( + overflow.filter((segment) => segment.kind === "data").reduce((sum, segment) => sum + segment.data.length, 0), + malformed.length, + ); + + const recovered = segmenter.append("ready\r\n"); + assert.equal(recovered.some((segment) => segment.kind === "timestamp"), true); + assert.equal(recovered.some((segment) => segment.kind === "data" && segment.data.includes("ready")), true); +}); + test("does not timestamp output suspended on the alternate screen", () => { const { term } = createFakeTerm(); writeTerminalDataWithLineTimestamps(term as never, "\x1b[?1049hvim screen", () => {}, { diff --git a/components/terminal/runtime/terminalLineTimestamps.ts b/components/terminal/runtime/terminalLineTimestamps.ts index b1177f37b..09df93ba9 100644 --- a/components/terminal/runtime/terminalLineTimestamps.ts +++ b/components/terminal/runtime/terminalLineTimestamps.ts @@ -1,6 +1,7 @@ import type { Terminal as XTerm } from "@xterm/xterm"; import { shouldSkipTerminalLineTimestamps } from "./terminalOutputPressure"; +import { canRetainIncompleteTerminalControlSequence } from "./terminalControlSequenceLimits"; export type TerminalLineTimestampSegment = | { kind: "data"; data: string } @@ -407,7 +408,14 @@ export const createTerminalLineTimestampSegmenter = ( const sequence = readEscapeSequence(input, index); if (sequence) { if (!sequence.complete) { - pendingEscapeSequence = sequence.sequence; + if (canRetainIncompleteTerminalControlSequence(sequence.sequence)) { + pendingEscapeSequence = sequence.sequence; + } else { + // Fail open: xterm still receives the original data, while the + // timestamp parser drops the malformed prefix and resumes fresh + // on the next chunk instead of retaining/scanning it forever. + pushDataSegment(segments, sequence.sequence); + } break; } const alternateScreenAction = getAlternateScreenAction(sequence.sequence); @@ -1574,15 +1582,6 @@ export const resolveTerminalTimestampGutterRowsFromLedger = ({ .filter((entry) => entry.line >= 0) .sort((left, right) => left.line - right.line || left.secondKey - right.secondKey); - const labelAtOrBefore = (line: number): string | undefined => { - let label: string | undefined; - for (const stamp of stamps) { - if (stamp.line > line) break; - label = stamp.label; - } - return label; - }; - const resolveSourceLine = (line: number): number => { if (!isWrappedLine?.(line)) return line; let sourceLine = line; @@ -1597,13 +1596,18 @@ export const resolveTerminalTimestampGutterRowsFromLedger = ({ : Number.POSITIVE_INFINITY; const visible: TerminalTimestampGutterRow[] = []; + let stampIndex = -1; + let currentLabel: string | undefined; for (let row = 0; row < rows; row += 1) { const line = viewportY + row; if (line > maxLine) break; const sourceLine = resolveSourceLine(line); - const label = labelAtOrBefore(sourceLine); - if (label) { - visible.push({ row, label }); + while (stampIndex + 1 < stamps.length && stamps[stampIndex + 1].line <= sourceLine) { + stampIndex += 1; + currentLabel = stamps[stampIndex].label; + } + if (currentLabel) { + visible.push({ row, label: currentLabel }); } } return visible; diff --git a/components/terminalLayer/TerminalLayerSupport.dragCleanup.test.ts b/components/terminalLayer/TerminalLayerSupport.dragCleanup.test.ts new file mode 100644 index 000000000..54618f539 --- /dev/null +++ b/components/terminalLayer/TerminalLayerSupport.dragCleanup.test.ts @@ -0,0 +1,261 @@ +import assert from "node:assert/strict"; +import { registerHooks } from "node:module"; +import test from "node:test"; +import React from "react"; +import { act, create, type ReactTestRenderer } from "react-test-renderer"; + +registerHooks({ + resolve(specifier, context, nextResolve) { + if (specifier === "../Terminal" && context.parentURL?.endsWith("/TerminalLayerSupport.tsx")) { + return { + url: "data:text/javascript,export default function Terminal(){return null}", + shortCircuit: true, + }; + } + return nextResolve(specifier, context); + }, + load(url, context, nextLoad) { + if (url.endsWith(".css")) { + return { format: "module", source: "export default {};", shortCircuit: true }; + } + return nextLoad(url, context); + }, +}); + +const { useWorkspaceDetachPointerDrag } = await import("./TerminalLayerSupport"); + +(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean }) + .IS_REACT_ACT_ENVIRONMENT = true; + +type Listener = EventListenerOrEventListenerObject; + +function createDragDom() { + const documentListeners = new Map>(); + const windowListeners = new Map>(); + const appendedElements = new Set<{ remove: () => void }>(); + + const addListener = (listeners: Map>, type: string, listener: Listener) => { + const entries = listeners.get(type) ?? new Set(); + entries.add(listener); + listeners.set(type, entries); + }; + const removeListener = (listeners: Map>, type: string, listener: Listener) => { + listeners.get(type)?.delete(listener); + }; + const dispatch = (listeners: Map>, type: string, event: Event) => { + for (const listener of [...(listeners.get(type) ?? [])]) { + if (typeof listener === "function") listener(event); + else listener.handleEvent(event); + } + }; + + const fakeWindow = { + addEventListener: (type: string, listener: Listener) => addListener(windowListeners, type, listener), + removeEventListener: (type: string, listener: Listener) => removeListener(windowListeners, type, listener), + }; + const fakeDocument = { + body: { + appendChild: (element: { remove: () => void }) => appendedElements.add(element), + }, + createElement: () => { + const element = { + style: {} as Record, + textContent: "", + remove: () => appendedElements.delete(element), + }; + return element; + }, + querySelector: () => null, + addEventListener: (type: string, listener: Listener) => addListener(documentListeners, type, listener), + removeEventListener: (type: string, listener: Listener) => removeListener(documentListeners, type, listener), + defaultView: fakeWindow, + }; + + return { + document: fakeDocument as unknown as Document, + documentListenerCount: (type: string) => documentListeners.get(type)?.size ?? 0, + windowListenerCount: (type: string) => windowListeners.get(type)?.size ?? 0, + dispatchDocument: (type: string, event: Event) => dispatch(documentListeners, type, event), + dispatchWindow: (type: string, event: Event) => dispatch(windowListeners, type, event), + appendedElements, + }; +} + +test("pointer drag cleanup runs when the pane unmounts without pointerup", async () => { + const dom = createDragDom(); + let pointerDown: ((event: React.PointerEvent) => void) | null = null; + let dragStartCount = 0; + let dragEndCount = 0; + + function Harness() { + pointerDown = useWorkspaceDetachPointerDrag({ + inActiveWorkspace: true, + session: { id: "session-1", workspaceId: "workspace-1" } as never, + workspaceById: new Map(), + onStartSessionDrag: () => { dragStartCount += 1; }, + onEndSessionDrag: () => { dragEndCount += 1; }, + }); + return null; + } + + let renderer: ReactTestRenderer; + await act(async () => { + renderer = create(React.createElement(Harness)); + }); + + const pointerTarget = { ownerDocument: dom.document }; + await act(async () => { + pointerDown!({ + button: 0, + clientX: 0, + clientY: 0, + currentTarget: pointerTarget, + preventDefault: () => undefined, + stopPropagation: () => undefined, + } as unknown as React.PointerEvent); + }); + + assert.equal(dom.documentListenerCount("pointermove"), 1); + assert.equal(dom.documentListenerCount("pointerup"), 1); + assert.equal(dom.documentListenerCount("pointercancel"), 1); + assert.equal(dom.windowListenerCount("blur"), 1); + assert.equal(dom.appendedElements.size, 0); + assert.equal(dragStartCount, 0); + + await act(async () => { + renderer!.unmount(); + }); + + assert.equal(dom.documentListenerCount("pointermove"), 0); + assert.equal(dom.documentListenerCount("pointerup"), 0); + assert.equal(dom.documentListenerCount("pointercancel"), 0); + assert.equal(dom.windowListenerCount("blur"), 0); + assert.equal(dom.appendedElements.size, 0); + assert.equal(dragEndCount, 0); +}); + +test("unmount removes an active drag overlay and ends drag state", async () => { + const dom = createDragDom(); + let pointerDown: ((event: React.PointerEvent) => void) | null = null; + let dragStartCount = 0; + let dragEndCount = 0; + + function Harness() { + pointerDown = useWorkspaceDetachPointerDrag({ + inActiveWorkspace: true, + session: { id: "session-1", workspaceId: "workspace-1" } as never, + workspaceById: new Map(), + onStartSessionDrag: () => { dragStartCount += 1; }, + onEndSessionDrag: () => { dragEndCount += 1; }, + }); + return null; + } + + let renderer: ReactTestRenderer; + await act(async () => { + renderer = create(React.createElement(Harness)); + }); + await act(async () => { + pointerDown!({ + button: 0, + clientX: 0, + clientY: 0, + currentTarget: { ownerDocument: dom.document }, + preventDefault: () => undefined, + stopPropagation: () => undefined, + } as unknown as React.PointerEvent); + dom.dispatchDocument("pointermove", { clientX: 10, clientY: 10 } as PointerEvent); + }); + + assert.equal(dom.appendedElements.size, 2); + assert.equal(dragStartCount, 1); + + await act(async () => { + renderer!.unmount(); + }); + + assert.equal(dom.documentListenerCount("pointermove"), 0); + assert.equal(dom.documentListenerCount("pointerup"), 0); + assert.equal(dom.documentListenerCount("pointercancel"), 0); + assert.equal(dom.windowListenerCount("blur"), 0); + assert.equal(dom.appendedElements.size, 0); + assert.equal(dragEndCount, 1); +}); + +test("all pointer drag exit paths share one idempotent cleanup", async () => { + const dom = createDragDom(); + let pointerDown: ((event: React.PointerEvent) => void) | null = null; + let dragStartCount = 0; + let dragEndCount = 0; + + function Harness() { + pointerDown = useWorkspaceDetachPointerDrag({ + inActiveWorkspace: true, + session: { id: "session-1", workspaceId: "workspace-1" } as never, + workspaceById: new Map(), + onStartSessionDrag: () => { dragStartCount += 1; }, + onEndSessionDrag: () => { dragEndCount += 1; }, + }); + return null; + } + + let renderer: ReactTestRenderer; + await act(async () => { + renderer = create(React.createElement(Harness)); + }); + + const beginDrag = () => { + pointerDown!({ + button: 0, + clientX: 0, + clientY: 0, + currentTarget: { ownerDocument: dom.document }, + preventDefault: () => undefined, + stopPropagation: () => undefined, + } as unknown as React.PointerEvent); + dom.dispatchDocument("pointermove", { clientX: 10, clientY: 10 } as PointerEvent); + }; + const assertClean = () => { + assert.equal(dom.documentListenerCount("pointermove"), 0); + assert.equal(dom.documentListenerCount("pointerup"), 0); + assert.equal(dom.documentListenerCount("pointercancel"), 0); + assert.equal(dom.windowListenerCount("blur"), 0); + assert.equal(dom.appendedElements.size, 0); + }; + + await act(async () => { + beginDrag(); + dom.dispatchDocument("pointercancel", {} as PointerEvent); + }); + assertClean(); + assert.equal(dragStartCount, 1); + assert.equal(dragEndCount, 1); + + await act(async () => { + beginDrag(); + beginDrag(); + }); + assert.equal(dragStartCount, 3); + assert.equal(dragEndCount, 2, "starting again must clean the previous drag exactly once"); + assert.equal(dom.documentListenerCount("pointermove"), 1); + assert.equal(dom.appendedElements.size, 2); + + await act(async () => { + dom.dispatchWindow("blur", {} as Event); + }); + assertClean(); + assert.equal(dragEndCount, 3); + + await act(async () => { + beginDrag(); + dom.dispatchDocument("pointerup", { clientX: 10, clientY: 10 } as PointerEvent); + }); + assertClean(); + assert.equal(dragStartCount, 4); + assert.equal(dragEndCount, 4); + + await act(async () => { + renderer!.unmount(); + }); + assert.equal(dragEndCount, 4, "unmount after cleanup must not end the drag twice"); +}); diff --git a/components/terminalLayer/TerminalLayerSupport.escapeFilter.test.ts b/components/terminalLayer/TerminalLayerSupport.escapeFilter.test.ts new file mode 100644 index 000000000..7b1f246f4 --- /dev/null +++ b/components/terminalLayer/TerminalLayerSupport.escapeFilter.test.ts @@ -0,0 +1,19 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +import { MAX_INCOMPLETE_TERMINAL_CONTROL_SEQUENCE_CHARS } from "../terminal/runtime/terminalControlSequenceLimits"; +import { ChunkedEscapeFilter } from "./activityEscapeFilter"; + +test("activity escape filter bounds unterminated OSC data and recovers on the next chunk", () => { + const filter = new ChunkedEscapeFilter(); + const chunk = "x".repeat(1024); + let emitted = ""; + + emitted += filter.feed("\x1b]0;"); + for (let size = 0; size <= MAX_INCOMPLETE_TERMINAL_CONTROL_SEQUENCE_CHARS; size += chunk.length) { + emitted += filter.feed(chunk); + } + + assert.ok(emitted.length >= MAX_INCOMPLETE_TERMINAL_CONTROL_SEQUENCE_CHARS); + assert.equal(filter.feed("normal output"), "normal output"); +}); diff --git a/components/terminalLayer/TerminalLayerSupport.tsx b/components/terminalLayer/TerminalLayerSupport.tsx index 78ea795dc..615ce4995 100644 --- a/components/terminalLayer/TerminalLayerSupport.tsx +++ b/components/terminalLayer/TerminalLayerSupport.tsx @@ -230,40 +230,7 @@ export const filterTabsMap = (source: Map, validIds: Set) return changed ? next : source; }; -// eslint-disable-next-line no-control-regex -const TERMINAL_OSC_SEQUENCE_REGEX = new RegExp('\\u001B\\][^\\u0007\\u001B]*(?:\\u0007|\\u001B\\\\)', 'g'); -// eslint-disable-next-line no-control-regex -const TERMINAL_ESCAPE_SEQUENCE_REGEX = new RegExp('\\u001B(?:[@-Z\\\\-_]|\\[[0-?]*[ -/]*[@-~])', 'g'); -// eslint-disable-next-line no-control-regex -const TERMINAL_CONTROL_CHAR_REGEX = new RegExp('[\\u0000-\\u0008\\u000B-\\u001F\\u007F]', 'g'); -// eslint-disable-next-line no-control-regex -const INCOMPLETE_ESCAPE_TAIL_REGEX = new RegExp('\\u001B(?:\\][^\\u0007\\u001B]*(?:\\u001B)?|\\[[0-?]*[ -/]*)?$'); - -const stripTerminalControlSequences = (data: string): string => { - return data - .replace(TERMINAL_OSC_SEQUENCE_REGEX, '') - .replace(TERMINAL_ESCAPE_SEQUENCE_REGEX, '') - .replace(TERMINAL_CONTROL_CHAR_REGEX, ''); -}; - -export class ChunkedEscapeFilter { - private pending = ''; - - feed(chunk: string): string { - const data = this.pending + chunk; - const tailMatch = INCOMPLETE_ESCAPE_TAIL_REGEX.exec(data); - if (tailMatch) { - this.pending = tailMatch[0]; - return stripTerminalControlSequences(data.slice(0, tailMatch.index)); - } - this.pending = ''; - return stripTerminalControlSequences(data); - } -} - -export const hasNotifiableTerminalOutput = (filter: ChunkedEscapeFilter, chunk: string): boolean => { - return filter.feed(chunk).trim().length > 0; -}; +export { ChunkedEscapeFilter, hasNotifiableTerminalOutput } from './activityEscapeFilter'; export type AIPanelContext = { scopeType: 'terminal' | 'workspace'; @@ -834,6 +801,226 @@ const terminalPanePropsAreEqual = ( prev.onEndSessionDrag === next.onEndSessionDrag ); +type WorkspaceDetachPointerDragOptions = { + inActiveWorkspace: boolean; + session: TerminalSession; + terminalSettings?: TerminalSettings; + workspaceById: Map; + onStartSessionDrag?: (sessionId: string) => void; + onEndSessionDrag?: () => void; + onRemoveSessionFromWorkspace?: TerminalPaneProps['onRemoveSessionFromWorkspace']; + onReorderTabs?: TerminalPaneProps['onReorderTabs']; +}; + +export function useWorkspaceDetachPointerDrag({ + inActiveWorkspace, + session, + terminalSettings, + workspaceById, + onStartSessionDrag, + onEndSessionDrag, + onRemoveSessionFromWorkspace, + onReorderTabs, +}: WorkspaceDetachPointerDragOptions): (event: React.PointerEvent) => void { + const activeDragCleanupRef = useRef<(() => void) | null>(null); + const cleanupActiveDrag = useCallback(() => { + const cleanup = activeDragCleanupRef.current; + activeDragCleanupRef.current = null; + cleanup?.(); + }, []); + + useEffect(() => cleanupActiveDrag, [cleanupActiveDrag, session.id]); + + return useCallback((event: React.PointerEvent) => { + if (!inActiveWorkspace || event.button !== 0) return; + event.preventDefault(); + event.stopPropagation(); + cleanupActiveDrag(); + + const ownerDocument = event.currentTarget.ownerDocument; + const ownerWindow = ownerDocument.defaultView; + const startPoint = { clientX: event.clientX, clientY: event.clientY }; + const dragLabel = resolveSessionTabTitle(session, terminalSettings?.dynamicTabTitleMode); + let dragStarted = false; + let cleanupFinished = false; + let ghostEl: HTMLDivElement | null = null; + let insertEl: HTMLDivElement | null = null; + + const ensureDragElements = () => { + if (!ghostEl) { + ghostEl = ownerDocument.createElement('div'); + ghostEl.textContent = dragLabel; + ghostEl.style.position = 'fixed'; + ghostEl.style.left = '0'; + ghostEl.style.top = '0'; + ghostEl.style.zIndex = '2147483647'; + ghostEl.style.pointerEvents = 'none'; + ghostEl.style.maxWidth = '220px'; + ghostEl.style.padding = '5px 10px'; + ghostEl.style.borderRadius = '7px'; + ghostEl.style.border = '1px solid color-mix(in srgb, var(--top-tabs-accent, hsl(var(--accent))) 60%, transparent)'; + ghostEl.style.background = 'color-mix(in srgb, var(--top-tabs-active-bg, hsl(var(--background))) 90%, transparent)'; + ghostEl.style.color = 'var(--top-tabs-fg, hsl(var(--foreground)))'; + ghostEl.style.boxShadow = '0 12px 28px rgba(0, 0, 0, 0.28)'; + ghostEl.style.fontSize = '12px'; + ghostEl.style.fontWeight = '600'; + ghostEl.style.whiteSpace = 'nowrap'; + ghostEl.style.overflow = 'hidden'; + ghostEl.style.textOverflow = 'ellipsis'; + ownerDocument.body.appendChild(ghostEl); + } + + if (!insertEl) { + insertEl = ownerDocument.createElement('div'); + insertEl.style.position = 'fixed'; + insertEl.style.zIndex = '2147483646'; + insertEl.style.pointerEvents = 'none'; + insertEl.style.width = '2px'; + insertEl.style.borderRadius = '999px'; + insertEl.style.background = 'var(--top-tabs-accent, hsl(var(--accent)))'; + insertEl.style.boxShadow = '0 0 10px color-mix(in srgb, var(--top-tabs-accent, hsl(var(--accent))) 70%, transparent)'; + insertEl.style.display = 'none'; + ownerDocument.body.appendChild(insertEl); + } + }; + + const removeDragElements = () => { + ghostEl?.remove(); + insertEl?.remove(); + ghostEl = null; + insertEl = null; + }; + + const updateDragElements = (pointerEvent: PointerEvent) => { + ensureDragElements(); + if (ghostEl) { + ghostEl.style.transform = `translate(${pointerEvent.clientX + 12}px, ${pointerEvent.clientY + 10}px)`; + } + + const topTabsRoot = ownerDocument.querySelector('[data-top-tabs-root]'); + const insertionTarget = getTopTabInsertionTarget(pointerEvent, topTabsRoot); + if (!topTabsRoot || !insertionTarget || !insertEl) { + if (insertEl) insertEl.style.display = 'none'; + return insertionTarget; + } + + const targetTab = Array.from(topTabsRoot.querySelectorAll('[data-tab-id]')) + .find((tab) => tab.dataset.tabId === insertionTarget.tabId); + if (!targetTab) { + insertEl.style.display = 'none'; + return insertionTarget; + } + + const targetRect = targetTab.getBoundingClientRect(); + const rootRect = topTabsRoot.getBoundingClientRect(); + const lineX = insertionTarget.position === 'before' ? targetRect.left : targetRect.right; + insertEl.style.display = 'block'; + insertEl.style.left = `${lineX - 1}px`; + insertEl.style.top = `${Math.max(rootRect.top + 5, targetRect.top + 3)}px`; + insertEl.style.height = `${Math.max(18, Math.min(rootRect.bottom - rootRect.top - 8, targetRect.height - 4))}px`; + return insertionTarget; + }; + + const resolveStableInsertionTarget = (insertionTarget: ReturnType) => { + if (!insertionTarget || insertionTarget.tabId !== session.workspaceId) return insertionTarget; + const sourceWorkspace = session.workspaceId ? workspaceById.get(session.workspaceId) : undefined; + if (!sourceWorkspace) return insertionTarget; + const remainingSessionIds = collectSessionIds(sourceWorkspace.root) + .filter((candidateId) => candidateId !== session.id); + if (remainingSessionIds.length !== 1) return insertionTarget; + return { + tabId: remainingSessionIds[0], + position: insertionTarget.position, + }; + }; + + const startDragIfNeeded = (pointerEvent: PointerEvent) => { + if (cleanupFinished || dragStarted) return; + const dx = pointerEvent.clientX - startPoint.clientX; + const dy = pointerEvent.clientY - startPoint.clientY; + if (Math.hypot(dx, dy) < 4) return; + dragStarted = true; + onStartSessionDrag?.(session.id); + updateDragElements(pointerEvent); + }; + + const cleanup = () => { + if (cleanupFinished) return; + cleanupFinished = true; + ownerDocument.removeEventListener('pointermove', handlePointerMove, true); + ownerDocument.removeEventListener('pointerup', handlePointerUp, true); + ownerDocument.removeEventListener('pointercancel', handlePointerCancel, true); + ownerWindow?.removeEventListener('blur', handleWindowBlur); + removeDragElements(); + const shouldEndDrag = dragStarted; + dragStarted = false; + if (activeDragCleanupRef.current === cleanup) { + activeDragCleanupRef.current = null; + } + if (shouldEndDrag) onEndSessionDrag?.(); + }; + + const handlePointerMove = (pointerEvent: PointerEvent) => { + startDragIfNeeded(pointerEvent); + if (dragStarted) updateDragElements(pointerEvent); + }; + + const handlePointerCancel = () => { + cleanup(); + }; + + const handleWindowBlur = () => { + cleanup(); + }; + + const handlePointerUp = (pointerEvent: PointerEvent) => { + startDragIfNeeded(pointerEvent); + const topTabsRoot = ownerDocument.querySelector('[data-top-tabs-root]'); + const insertionTarget = dragStarted ? updateDragElements(pointerEvent) : null; + const shouldDetach = dragStarted + && !!topTabsRoot + && isPointInsideRect(pointerEvent, topTabsRoot.getBoundingClientRect()); + cleanup(); + if (shouldDetach) { + const stableInsertionTarget = resolveStableInsertionTarget(insertionTarget); + if (onRemoveSessionFromWorkspace) { + onRemoveSessionFromWorkspace( + session.id, + stableInsertionTarget + ? { + tabId: stableInsertionTarget.tabId, + position: stableInsertionTarget.position, + additionalTabIds: [session.id, stableInsertionTarget.tabId], + } + : undefined, + ); + } else if (stableInsertionTarget) { + onReorderTabs?.(session.id, stableInsertionTarget.tabId, stableInsertionTarget.position, [ + session.id, + stableInsertionTarget.tabId, + ]); + } + } + }; + + activeDragCleanupRef.current = cleanup; + ownerDocument.addEventListener('pointermove', handlePointerMove, true); + ownerDocument.addEventListener('pointerup', handlePointerUp, true); + ownerDocument.addEventListener('pointercancel', handlePointerCancel, true); + ownerWindow?.addEventListener('blur', handleWindowBlur); + }, [ + cleanupActiveDrag, + inActiveWorkspace, + onEndSessionDrag, + onRemoveSessionFromWorkspace, + onReorderTabs, + onStartSessionDrag, + session, + terminalSettings?.dynamicTabTitleMode, + workspaceById, + ]); +} + const TerminalPane: React.FC = memo(({ session, host, @@ -1096,173 +1283,16 @@ const TerminalPane: React.FC = memo(({ const handleDetachDragEnd = useCallback(() => { onEndSessionDrag?.(); }, [onEndSessionDrag]); - const handleDetachPointerDown = useCallback((e: React.PointerEvent) => { - if (!inActiveWorkspace || e.button !== 0) return; - e.preventDefault(); - e.stopPropagation(); - - const startPoint = { clientX: e.clientX, clientY: e.clientY }; - const dragLabel = resolveSessionTabTitle(session, terminalSettings?.dynamicTabTitleMode); - let dragStarted = false; - let ghostEl: HTMLDivElement | null = null; - let insertEl: HTMLDivElement | null = null; - - const ensureDragElements = () => { - if (!ghostEl) { - ghostEl = document.createElement('div'); - ghostEl.textContent = dragLabel; - ghostEl.style.position = 'fixed'; - ghostEl.style.left = '0'; - ghostEl.style.top = '0'; - ghostEl.style.zIndex = '2147483647'; - ghostEl.style.pointerEvents = 'none'; - ghostEl.style.maxWidth = '220px'; - ghostEl.style.padding = '5px 10px'; - ghostEl.style.borderRadius = '7px'; - ghostEl.style.border = '1px solid color-mix(in srgb, var(--top-tabs-accent, hsl(var(--accent))) 60%, transparent)'; - ghostEl.style.background = 'color-mix(in srgb, var(--top-tabs-active-bg, hsl(var(--background))) 90%, transparent)'; - ghostEl.style.color = 'var(--top-tabs-fg, hsl(var(--foreground)))'; - ghostEl.style.boxShadow = '0 12px 28px rgba(0, 0, 0, 0.28)'; - ghostEl.style.fontSize = '12px'; - ghostEl.style.fontWeight = '600'; - ghostEl.style.whiteSpace = 'nowrap'; - ghostEl.style.overflow = 'hidden'; - ghostEl.style.textOverflow = 'ellipsis'; - document.body.appendChild(ghostEl); - } - - if (!insertEl) { - insertEl = document.createElement('div'); - insertEl.style.position = 'fixed'; - insertEl.style.zIndex = '2147483646'; - insertEl.style.pointerEvents = 'none'; - insertEl.style.width = '2px'; - insertEl.style.borderRadius = '999px'; - insertEl.style.background = 'var(--top-tabs-accent, hsl(var(--accent)))'; - insertEl.style.boxShadow = '0 0 10px color-mix(in srgb, var(--top-tabs-accent, hsl(var(--accent))) 70%, transparent)'; - insertEl.style.display = 'none'; - document.body.appendChild(insertEl); - } - }; - - const removeDragElements = () => { - ghostEl?.remove(); - insertEl?.remove(); - ghostEl = null; - insertEl = null; - }; - - const updateDragElements = (event: PointerEvent) => { - ensureDragElements(); - if (ghostEl) { - ghostEl.style.transform = `translate(${event.clientX + 12}px, ${event.clientY + 10}px)`; - } - - const topTabsRoot = document.querySelector('[data-top-tabs-root]'); - const insertionTarget = getTopTabInsertionTarget(event, topTabsRoot); - if (!topTabsRoot || !insertionTarget || !insertEl) { - if (insertEl) insertEl.style.display = 'none'; - return insertionTarget; - } - - const targetTab = Array.from(topTabsRoot.querySelectorAll('[data-tab-id]')) - .find((tab) => tab.dataset.tabId === insertionTarget.tabId); - if (!targetTab) { - insertEl.style.display = 'none'; - return insertionTarget; - } - - const targetRect = targetTab.getBoundingClientRect(); - const rootRect = topTabsRoot.getBoundingClientRect(); - const lineX = insertionTarget.position === 'before' ? targetRect.left : targetRect.right; - insertEl.style.display = 'block'; - insertEl.style.left = `${lineX - 1}px`; - insertEl.style.top = `${Math.max(rootRect.top + 5, targetRect.top + 3)}px`; - insertEl.style.height = `${Math.max(18, Math.min(rootRect.bottom - rootRect.top - 8, targetRect.height - 4))}px`; - return insertionTarget; - }; - - const resolveStableInsertionTarget = (insertionTarget: ReturnType) => { - if (!insertionTarget || insertionTarget.tabId !== session.workspaceId) return insertionTarget; - const sourceWorkspace = session.workspaceId ? workspaceById.get(session.workspaceId) : undefined; - if (!sourceWorkspace) return insertionTarget; - const remainingSessionIds = collectSessionIds(sourceWorkspace.root) - .filter((candidateId) => candidateId !== session.id); - if (remainingSessionIds.length !== 1) return insertionTarget; - return { - tabId: remainingSessionIds[0], - position: insertionTarget.position, - }; - }; - - const startDragIfNeeded = (event: PointerEvent) => { - if (dragStarted) return; - const dx = event.clientX - startPoint.clientX; - const dy = event.clientY - startPoint.clientY; - if (Math.hypot(dx, dy) < 4) return; - dragStarted = true; - onStartSessionDrag?.(session.id); - updateDragElements(event); - }; - - const cleanup = () => { - document.removeEventListener('pointermove', handlePointerMove, true); - document.removeEventListener('pointerup', handlePointerUp, true); - document.removeEventListener('pointercancel', handlePointerCancel, true); - removeDragElements(); - if (dragStarted) onEndSessionDrag?.(); - }; - - const handlePointerMove = (event: PointerEvent) => { - startDragIfNeeded(event); - if (dragStarted) updateDragElements(event); - }; - - const handlePointerCancel = () => { - cleanup(); - }; - - const handlePointerUp = (event: PointerEvent) => { - startDragIfNeeded(event); - const topTabsRoot = document.querySelector('[data-top-tabs-root]'); - const insertionTarget = dragStarted ? updateDragElements(event) : null; - const shouldDetach = dragStarted && !!topTabsRoot && isPointInsideRect(event, topTabsRoot.getBoundingClientRect()); - cleanup(); - if (shouldDetach) { - const stableInsertionTarget = resolveStableInsertionTarget(insertionTarget); - if (onRemoveSessionFromWorkspace) { - onRemoveSessionFromWorkspace( - session.id, - stableInsertionTarget - ? { - tabId: stableInsertionTarget.tabId, - position: stableInsertionTarget.position, - additionalTabIds: [session.id, stableInsertionTarget.tabId], - } - : undefined, - ); - } else if (stableInsertionTarget) { - onReorderTabs?.(session.id, stableInsertionTarget.tabId, stableInsertionTarget.position, [ - session.id, - stableInsertionTarget.tabId, - ]); - } - } - }; - - document.addEventListener('pointermove', handlePointerMove, true); - document.addEventListener('pointerup', handlePointerUp, true); - document.addEventListener('pointercancel', handlePointerCancel, true); - }, [ + const handleDetachPointerDown = useWorkspaceDetachPointerDrag({ inActiveWorkspace, + session, + terminalSettings, + workspaceById, + onStartSessionDrag, onEndSessionDrag, onRemoveSessionFromWorkspace, onReorderTabs, - onStartSessionDrag, - session, - terminalSettings?.dynamicTabTitleMode, - workspaceById, - ]); + }); const handleTerminalFontSizeChange = useCallback((nextFontSize: number) => { onTerminalFontSizeChange?.(session.id, nextFontSize); }, [onTerminalFontSizeChange, session.id]); diff --git a/components/terminalLayer/activityEscapeFilter.ts b/components/terminalLayer/activityEscapeFilter.ts new file mode 100644 index 000000000..74b79dc27 --- /dev/null +++ b/components/terminalLayer/activityEscapeFilter.ts @@ -0,0 +1,42 @@ +import { canRetainIncompleteTerminalControlSequence } from "../terminal/runtime/terminalControlSequenceLimits"; + +// eslint-disable-next-line no-control-regex +const TERMINAL_OSC_SEQUENCE_REGEX = new RegExp("\\u001B\\][^\\u0007\\u001B]*(?:\\u0007|\\u001B\\\\)", "g"); +// eslint-disable-next-line no-control-regex +const TERMINAL_ESCAPE_SEQUENCE_REGEX = new RegExp("\\u001B(?:[@-Z\\\\-_]|\\[[0-?]*[ -/]*[@-~])", "g"); +// eslint-disable-next-line no-control-regex +const TERMINAL_CONTROL_CHAR_REGEX = new RegExp("[\\u0000-\\u0008\\u000B-\\u001F\\u007F]", "g"); +// eslint-disable-next-line no-control-regex +const INCOMPLETE_ESCAPE_TAIL_REGEX = new RegExp("\\u001B(?:\\][^\\u0007\\u001B]*(?:\\u001B)?|\\[[0-?]*[ -/]*)?$"); + +const stripTerminalControlSequences = (data: string): string => data + .replace(TERMINAL_OSC_SEQUENCE_REGEX, "") + .replace(TERMINAL_ESCAPE_SEQUENCE_REGEX, "") + .replace(TERMINAL_CONTROL_CHAR_REGEX, ""); + +export class ChunkedEscapeFilter { + private pending = ""; + + feed(chunk: string): string { + const data = this.pending + chunk; + const tailMatch = INCOMPLETE_ESCAPE_TAIL_REGEX.exec(data); + if (tailMatch) { + const incomplete = tailMatch[0]; + if (canRetainIncompleteTerminalControlSequence(incomplete)) { + this.pending = incomplete; + return stripTerminalControlSequences(data.slice(0, tailMatch.index)); + } + // Fail open after the safety budget. This filter is only an activity + // classifier; retaining an unterminated OSC/DCS forever is worse than + // treating its payload as visible activity once. + this.pending = ""; + return `${stripTerminalControlSequences(data.slice(0, tailMatch.index))}${incomplete}`; + } + this.pending = ""; + return stripTerminalControlSequences(data); + } +} + +export const hasNotifiableTerminalOutput = (filter: ChunkedEscapeFilter, chunk: string): boolean => ( + filter.feed(chunk).trim().length > 0 +); diff --git a/components/terminalLayer/useTerminalLayerEffects.test.ts b/components/terminalLayer/useTerminalLayerEffects.test.ts index 82dd5aecb..b746659c6 100644 --- a/components/terminalLayer/useTerminalLayerEffects.test.ts +++ b/components/terminalLayer/useTerminalLayerEffects.test.ts @@ -2,6 +2,12 @@ import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import test from "node:test"; +import { + clearTerminalSessionRuntimeState, + pruneTerminalTabMemoryState, + pruneTerminalSessionRuntimeState, +} from "./useTerminalLayerEffects"; + const source = readFileSync(new URL("./useTerminalLayerEffects.ts", import.meta.url), "utf8"); test("theme preview DOM effects were removed in favor of ThemeRuntime injection", () => { @@ -51,3 +57,100 @@ test("transfer navigation helper is used for open-target and resume routing", () assert.match(source, /openHostThenSftp/); assert.match(source, /allowLiveUploadFallback/); }); + +const createSessionRuntimeState = () => ({ + terminalRendererCwdBySessionRef: { + current: new Map([ + ["closed", "/closed"], + ["live", "/live"], + ]), + }, + terminalOsc7SignalBySessionRef: { + current: new Map([ + ["closed", 4], + ["live", 7], + ]), + }, + cwdProbeGenerationRef: { + current: new Map([ + ["closed", 2], + ["live", 3], + ]), + }, + cwdProbeCancelersRef: { + current: new Map void>(), + }, +}); + +test("closing a terminal session cancels its probe and deletes only its runtime state", () => { + const state = createSessionRuntimeState(); + let closedCancelCount = 0; + let liveCancelCount = 0; + state.cwdProbeCancelersRef.current.set("closed", () => { closedCancelCount += 1; }); + state.cwdProbeCancelersRef.current.set("live", () => { liveCancelCount += 1; }); + + clearTerminalSessionRuntimeState(state, "closed"); + + assert.equal(closedCancelCount, 1); + assert.equal(liveCancelCount, 0); + for (const runtimeMap of [ + state.terminalRendererCwdBySessionRef.current, + state.terminalOsc7SignalBySessionRef.current, + state.cwdProbeGenerationRef.current, + state.cwdProbeCancelersRef.current, + ]) { + assert.equal(runtimeMap.has("closed"), false); + assert.equal(runtimeMap.has("live"), true); + } + + clearTerminalSessionRuntimeState(state, "closed"); + assert.equal(closedCancelCount, 1, "repeated cleanup must not cancel the same probe twice"); +}); + +test("session pruning preserves reconnecting sessions and unmount cleanup removes the rest", () => { + const state = createSessionRuntimeState(); + let closedCancelCount = 0; + let liveCancelCount = 0; + state.cwdProbeCancelersRef.current.set("closed", () => { closedCancelCount += 1; }); + state.cwdProbeCancelersRef.current.set("live", () => { liveCancelCount += 1; }); + + pruneTerminalSessionRuntimeState(state, new Set(["live"])); + assert.equal(closedCancelCount, 1); + assert.equal(liveCancelCount, 0); + assert.equal(state.cwdProbeGenerationRef.current.has("live"), true); + + pruneTerminalSessionRuntimeState(state, new Set()); + assert.equal(liveCancelCount, 1); + for (const runtimeMap of [ + state.terminalRendererCwdBySessionRef.current, + state.terminalOsc7SignalBySessionRef.current, + state.cwdProbeGenerationRef.current, + state.cwdProbeCancelersRef.current, + ]) { + assert.equal(runtimeMap.size, 0); + } +}); + +test("tab memory pruning releases side-panel and SFTP paths for closed sessions", () => { + const state = { + lastSidePanelTabRef: { current: new Map([["closed", "sftp"], ["live", "scripts"]]) }, + notesReturnTabRef: { current: new Map([["closed", "notes"], ["live", "sftp"]]) }, + sftpLastPathForSourceRef: { + current: new Map([ + ["closed", { hostId: "host-1", connectionKey: "closed", path: "/old" }], + ["live", { hostId: "host-2", connectionKey: "live", path: "/current" }], + ]), + }, + }; + + pruneTerminalTabMemoryState(state, new Set(["live"])); + + for (const memoryMap of [ + state.lastSidePanelTabRef.current, + state.notesReturnTabRef.current, + state.sftpLastPathForSourceRef.current, + ]) { + assert.equal(memoryMap.has("closed"), false); + assert.equal(memoryMap.has("live"), true); + } +}); diff --git a/components/terminalLayer/useTerminalLayerEffects.ts b/components/terminalLayer/useTerminalLayerEffects.ts index 4afa59e07..938618024 100644 --- a/components/terminalLayer/useTerminalLayerEffects.ts +++ b/components/terminalLayer/useTerminalLayerEffects.ts @@ -16,9 +16,79 @@ import { getTerminalSidePanelShellWidth } from './TerminalLayerSidePanelSection' type TerminalLayerEffectsContext = Record; +type RuntimeStateRef = { current: Map }; + +export type TerminalSessionRuntimeState = { + terminalRendererCwdBySessionRef: RuntimeStateRef; + terminalOsc7SignalBySessionRef: RuntimeStateRef; + cwdProbeGenerationRef: RuntimeStateRef; + cwdProbeCancelersRef: RuntimeStateRef<() => void>; +}; + +export function clearTerminalSessionRuntimeState( + state: TerminalSessionRuntimeState, + sessionId: string, +): void { + const cancelProbe = state.cwdProbeCancelersRef.current.get(sessionId); + + state.cwdProbeCancelersRef.current.delete(sessionId); + state.cwdProbeGenerationRef.current.delete(sessionId); + state.terminalOsc7SignalBySessionRef.current.delete(sessionId); + state.terminalRendererCwdBySessionRef.current.delete(sessionId); + + if (cancelProbe) { + try { + cancelProbe(); + } catch { + // Session teardown is best-effort: one faulty canceler must not retain state. + } + } +} + +export function pruneTerminalSessionRuntimeState( + state: TerminalSessionRuntimeState, + liveSessionIds: ReadonlySet, +): void { + const trackedSessionIds = new Set([ + ...state.terminalRendererCwdBySessionRef.current.keys(), + ...state.terminalOsc7SignalBySessionRef.current.keys(), + ...state.cwdProbeGenerationRef.current.keys(), + ...state.cwdProbeCancelersRef.current.keys(), + ]); + + for (const sessionId of trackedSessionIds) { + if (!liveSessionIds.has(sessionId)) { + clearTerminalSessionRuntimeState(state, sessionId); + } + } +} + +type TabMemoryRef = { current: Map }; + +export type TerminalTabMemoryState = { + lastSidePanelTabRef: TabMemoryRef; + notesReturnTabRef: TabMemoryRef; + sftpLastPathForSourceRef: TabMemoryRef; +}; + +export function pruneTerminalTabMemoryState( + state: TerminalTabMemoryState, + liveTabIds: ReadonlySet, +): void { + for (const memoryRef of [ + state.lastSidePanelTabRef, + state.notesReturnTabRef, + state.sftpLastPathForSourceRef, + ]) { + for (const tabId of memoryRef.current.keys()) { + if (!liveTabIds.has(tabId)) memoryRef.current.delete(tabId); + } + } +} + export function useTerminalLayerEffects(ctx: TerminalLayerEffectsContext) { const { openPath } = useSftpBackend(); - const { activeSidePanelTab, activeTabId, activeTabIdRef, activeWorkspace, activityTrackedSessions, cancelAnimationFrame, ChunkedEscapeFilter, clearTimeout, clearTopTabsPreviewVars, document, dropHint, effectiveHosts, filterTabsMap, focusedSessionId, getSessionActivityIdsToClear, handleToggleAiFromTopBar, handleToggleScriptsSidePanel, handleToggleSidePanel, hasNotifiableTerminalOutput, isComposeBarOpen, isFocusMode, isTerminalLayerVisible, lastSidePanelTabRef, Map, onConnectToHost, onSessionData, onSplitSessionRef, onToggleBroadcastRef, onToggleWorkspaceViewModeRef, prevFocusedSessionIdRef, refocusActiveTerminalSession, requestAnimationFrame, ResizeObserver, sessionActivityStore, sessions, Set, setAiMountedTabIds, setDropHint, setNotesMountedTabIds, setScriptsMountedTabIds, setSystemMountedTabIds, setSftpHostForTab, setSftpInitialLocationForTab, setSftpPendingUploadsForTab, setSidePanelOpenTabs, setThemeMountedTabIds, setTimeout, setWorkspaceArea, shouldMeasureTerminalLayerLayout, sidePanelPosition, sidePanelWidth, sftpActiveHost, sftpHostForTab, shouldMarkSessionActivity, sidePanelOpenTabs, splitHorizontalHandlersRef, splitVerticalHandlersRef, terminalRendererCwdBySessionRef, toggleScriptsSidePanelRef, toggleSidePanelRef, validAIScopeTargetIds, validSessionActivityIds, window, workspaceBroadcastHandlersRef, workspaceFocusHandlersRef, workspaceInnerRef, workspaces } = ctx; + const { activeSidePanelTab, activeTabId, activeTabIdRef, activeWorkspace, activityTrackedSessions, cancelAnimationFrame, ChunkedEscapeFilter, clearTimeout, clearTopTabsPreviewVars, document, dropHint, effectiveHosts, filterTabsMap, focusedSessionId, getSessionActivityIdsToClear, handleToggleAiFromTopBar, handleToggleScriptsSidePanel, handleToggleSidePanel, hasNotifiableTerminalOutput, isComposeBarOpen, isFocusMode, isTerminalLayerVisible, lastSidePanelTabRef, Map, onConnectToHost, onSessionData, onSplitSessionRef, onToggleBroadcastRef, onToggleWorkspaceViewModeRef, prevFocusedSessionIdRef, refocusActiveTerminalSession, requestAnimationFrame, ResizeObserver, sessionActivityStore, sessions, Set, setAiMountedTabIds, setDropHint, setNotesMountedTabIds, setScriptsMountedTabIds, setSystemMountedTabIds, setSftpHostForTab, setSftpInitialLocationForTab, setSftpPendingUploadsForTab, setSidePanelOpenTabs, setThemeMountedTabIds, setTimeout, setWorkspaceArea, shouldMeasureTerminalLayerLayout, sidePanelPosition, sidePanelWidth, sftpActiveHost, sftpHostForTab, shouldMarkSessionActivity, sidePanelOpenTabs, splitHorizontalHandlersRef, splitVerticalHandlersRef, toggleScriptsSidePanelRef, toggleSidePanelRef, validAIScopeTargetIds, validSessionActivityIds, window, workspaceBroadcastHandlersRef, workspaceFocusHandlersRef, workspaceInnerRef, workspaces } = ctx; const activeWorkspaceId = activeWorkspace?.id; const activeWorkspaceViewMode = activeWorkspace?.viewMode; @@ -78,15 +148,6 @@ export function useTerminalLayerEffects(ctx: TerminalLayerEffectsContext) { } }, [clearTopTabsPreviewVars, isTerminalLayerVisible]); - useEffect(() => { - const liveSessionIds = new Set(sessions.map((session) => session.id)); - for (const sessionId of terminalRendererCwdBySessionRef.current.keys()) { - if (!liveSessionIds.has(sessionId)) { - terminalRendererCwdBySessionRef.current.delete(sessionId); - } - } - }, [sessions]); - useEffect(() => { sidePanelOpenTabs.forEach((tab, tabId) => { lastSidePanelTabRef.current.set(tabId, tab); diff --git a/domain/models/sftp.ts b/domain/models/sftp.ts index 6bcab2418..62a291607 100644 --- a/domain/models/sftp.ts +++ b/domain/models/sftp.ts @@ -45,6 +45,16 @@ export type TransferOrigin = 'manual' | 'drag-drop' | 'editor-sync' | 'agent' | export type TransferPhase = 'scanning' | 'compressing' | 'uploading' | 'transferring' | 'extracting' | 'verifying'; export type TransferControlKind = 'stream' | 'compressed-upload'; +export interface DirectoryResumeCheckpoint { + version: 1; + /** Traversal prefix whose source/target metadata is covered by manifestHash. */ + coveredEntries: number; + /** Covered entries already completed and compacted out of the task array. */ + completedEntries: number; + /** Fixed-size chained SHA-256 of the covered traversal prefix. */ + manifestHash: string; +} + export interface TransferTask { id: string; batchId?: string; @@ -97,6 +107,11 @@ export interface TransferTask { stagedTargetPath?: string; sourceFingerprint?: string; reconnectRequired?: boolean; + /** Stable position and identity used to compact completed directory children. */ + directoryEntryIndex?: number; + directoryEntryIdentity?: string; + /** Fixed-size resume record stored only on a top-level directory task. */ + directoryResumeCheckpoint?: DirectoryResumeCheckpoint; } export type FileConflictAction = 'stop' | 'skip' | 'replace' | 'duplicate' | 'merge'; diff --git a/domain/searchMatcher.test.ts b/domain/searchMatcher.test.ts index a99e68191..918a229da 100644 --- a/domain/searchMatcher.test.ts +++ b/domain/searchMatcher.test.ts @@ -2,9 +2,12 @@ import assert from "node:assert/strict"; import test from "node:test"; import { + PINYIN_CACHE_MAX_ENTRIES, + getPinyinCacheStatsForTests, getHostSearchMatch, matchesHostSearchQuery, matchesSearchQuery, + resetPinyinCacheForTests, } from "../lib/searchMatcher.ts"; test("matches mixed Chinese and dash-separated numeric suffix with spaced query", () => { @@ -150,3 +153,25 @@ test("host search scoring favors label over group when both match", () => { assert.equal(groupHit.matched, true); assert.equal(labelHit.score > groupHit.score, true); }); + +test("pinyin cache covers 8000 hosts and stays hard-bounded through import/edit churn", () => { + resetPinyinCacheForTests(); + const startedAt = performance.now(); + for (let index = 0; index < 8_000; index += 1) { + matchesHostSearchQuery("z", { label: `主机${index}` }); + } + const firstPassMs = performance.now() - startedAt; + assert.equal(getPinyinCacheStatsForTests().size, 8_000); + assert.ok(firstPassMs < 10_000, `8000-host pinyin indexing took ${firstPassMs.toFixed(1)}ms`); + + matchesHostSearchQuery("z", { label: "主机0" }); + for (let index = 8_000; index < PINYIN_CACHE_MAX_ENTRIES + 128; index += 1) { + matchesHostSearchQuery("z", { label: `主机${index}` }); + } + + const stats = getPinyinCacheStatsForTests(); + assert.equal(stats.size, PINYIN_CACHE_MAX_ENTRIES); + assert.ok(stats.keys.includes("主机0")); + assert.ok(!stats.keys.includes("主机1")); + resetPinyinCacheForTests(); +}); diff --git a/domain/sftpDirectoryCheckpoint.test.ts b/domain/sftpDirectoryCheckpoint.test.ts new file mode 100644 index 000000000..c5bf97637 --- /dev/null +++ b/domain/sftpDirectoryCheckpoint.test.ts @@ -0,0 +1,27 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +import { + accountSftpDirectoryEntries, + claimSftpDirectoryVisit, + createSftpDirectoryTraversalBudget, + MAX_SFTP_FOLLOWED_SYMLINK_DEPTH, + shouldFollowSftpSymlinkDirectory, +} from "./sftpDirectoryCheckpoint"; + +test("live and resumed directory transfers share the same symlink depth policy", () => { + assert.equal(MAX_SFTP_FOLLOWED_SYMLINK_DEPTH, 32); + assert.equal(shouldFollowSftpSymlinkDirectory(8), true); + assert.equal(shouldFollowSftpSymlinkDirectory(31), true); + assert.equal(shouldFollowSftpSymlinkDirectory(32), false); +}); + +test("remote directory traversal skips canonical cycles and enforces total work budgets", () => { + const budget = createSftpDirectoryTraversalBudget({ maxDirectories: 2, maxEntries: 3 }); + assert.equal(claimSftpDirectoryVisit(budget, "/srv/root"), true); + accountSftpDirectoryEntries(budget, 2); + assert.equal(claimSftpDirectoryVisit(budget, "/srv/root"), false); + assert.equal(claimSftpDirectoryVisit(budget, "/srv/other"), true); + assert.throws(() => accountSftpDirectoryEntries(budget, 2), /entry limit/i); + assert.throws(() => claimSftpDirectoryVisit(budget, "/srv/third"), /directory limit/i); +}); diff --git a/domain/sftpDirectoryCheckpoint.ts b/domain/sftpDirectoryCheckpoint.ts new file mode 100644 index 000000000..3889dece2 --- /dev/null +++ b/domain/sftpDirectoryCheckpoint.ts @@ -0,0 +1,166 @@ +import type { DirectoryResumeCheckpoint } from "./models"; + +export const EMPTY_DIRECTORY_MANIFEST_HASH = "0".repeat(64); +export const MAX_SFTP_FOLLOWED_SYMLINK_DEPTH = 32; +export const MAX_SFTP_DIRECTORY_TRAVERSAL_DIRECTORIES = 50_000; +export const MAX_SFTP_DIRECTORY_TRAVERSAL_ENTRIES = 200_000; + +export interface SftpDirectoryTraversalBudget { + canonicalDirectories: Set; + visitedDirectories: number; + visitedEntries: number; + maxDirectories: number; + maxEntries: number; +} + +export function createSftpDirectoryTraversalBudget(limits: { + maxDirectories?: number; + maxEntries?: number; +} = {}): SftpDirectoryTraversalBudget { + return { + canonicalDirectories: new Set(), + visitedDirectories: 0, + visitedEntries: 0, + maxDirectories: limits.maxDirectories ?? MAX_SFTP_DIRECTORY_TRAVERSAL_DIRECTORIES, + maxEntries: limits.maxEntries ?? MAX_SFTP_DIRECTORY_TRAVERSAL_ENTRIES, + }; +} + +export function claimSftpDirectoryVisit( + budget: SftpDirectoryTraversalBudget, + canonicalPath: string, +): boolean { + const normalized = canonicalPath.replace(/\\/g, "/").replace(/\/+$/, "") || "/"; + if (budget.canonicalDirectories.has(normalized)) return false; + if (budget.visitedDirectories >= budget.maxDirectories) { + throw new Error(`Remote directory traversal directory limit exceeded (${budget.maxDirectories})`); + } + budget.canonicalDirectories.add(normalized); + budget.visitedDirectories += 1; + return true; +} + +export function accountSftpDirectoryEntries( + budget: SftpDirectoryTraversalBudget, + count: number, +): void { + const next = budget.visitedEntries + Math.max(0, Number(count) || 0); + if (next > budget.maxEntries) { + throw new Error(`Remote directory traversal entry limit exceeded (${budget.maxEntries})`); + } + budget.visitedEntries = next; +} + +export function shouldFollowSftpSymlinkDirectory(symlinkDepth: number): boolean { + return symlinkDepth < MAX_SFTP_FOLLOWED_SYMLINK_DEPTH; +} + +const SHA256_TEXT_ENCODER = new TextEncoder(); +const SHA256_ROUND_CONSTANTS = new Uint32Array([ + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2, +]); + +// Synchronous SHA-256 keeps transfer-history pruning deterministic and avoids +// making every React/store lifecycle update asynchronous. +function sha256Hex(value: string): string { + const data = SHA256_TEXT_ENCODER.encode(value); + const bitLength = BigInt(data.length) * 8n; + const padded = new Uint8Array(((data.length + 9 + 63) >> 6) << 6); + padded.set(data); + padded[data.length] = 0x80; + const view = new DataView(padded.buffer); + view.setBigUint64(padded.length - 8, bitLength, false); + const H = new Uint32Array([ + 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, + 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19, + ]); + const W = new Uint32Array(64); + for (let chunk = 0; chunk < padded.length; chunk += 64) { + for (let i = 0; i < 16; i += 1) W[i] = view.getUint32(chunk + i * 4, false); + for (let i = 16; i < 64; i += 1) { + const s0 = ((W[i - 15] >>> 7) | (W[i - 15] << 25)) + ^ ((W[i - 15] >>> 18) | (W[i - 15] << 14)) ^ (W[i - 15] >>> 3); + const s1 = ((W[i - 2] >>> 17) | (W[i - 2] << 15)) + ^ ((W[i - 2] >>> 19) | (W[i - 2] << 13)) ^ (W[i - 2] >>> 10); + W[i] = (W[i - 16] + s0 + W[i - 7] + s1) >>> 0; + } + let [a, b, c, d, e, f, g, h] = H; + for (let i = 0; i < 64; i += 1) { + const s1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7)); + const ch = (e & f) ^ (~e & g); + const temp1 = (h + s1 + ch + SHA256_ROUND_CONSTANTS[i] + W[i]) >>> 0; + const s0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10)); + const majority = (a & b) ^ (a & c) ^ (b & c); + const temp2 = (s0 + majority) >>> 0; + h = g; g = f; f = e; e = (d + temp1) >>> 0; + d = c; c = b; b = a; a = (temp1 + temp2) >>> 0; + } + H[0] = (H[0] + a) >>> 0; H[1] = (H[1] + b) >>> 0; + H[2] = (H[2] + c) >>> 0; H[3] = (H[3] + d) >>> 0; + H[4] = (H[4] + e) >>> 0; H[5] = (H[5] + f) >>> 0; + H[6] = (H[6] + g) >>> 0; H[7] = (H[7] + h) >>> 0; + } + return Array.from(H, (word) => word.toString(16).padStart(8, "0")).join(""); +} + +export function createDirectoryEntryIdentity(entry: { + sourcePath: string; + targetPath: string; + size: number; + lastModified?: number; +}): string { + return sha256Hex(JSON.stringify([ + entry.sourcePath, + entry.targetPath, + Math.max(0, Number(entry.size) || 0), + Number(entry.lastModified) || 0, + ])); +} + +export function appendDirectoryManifestIdentity(manifestHash: string, entryIdentity: string): string { + return sha256Hex(`${manifestHash}:${entryIdentity}`); +} + +/** Directory traversal order: child directories first, then files, names sorted. */ +export function compareDirectoryTraversalPaths(left: string, right: string): number { + const leftParts = left.replace(/\\/g, "/").split("/").filter(Boolean); + const rightParts = right.replace(/\\/g, "/").split("/").filter(Boolean); + const length = Math.min(leftParts.length, rightParts.length); + for (let index = 0; index < length; index += 1) { + const leftIsDirectory = index < leftParts.length - 1; + const rightIsDirectory = index < rightParts.length - 1; + if (leftIsDirectory !== rightIsDirectory) return leftIsDirectory ? -1 : 1; + const compared = leftParts[index].localeCompare(rightParts[index]); + if (compared !== 0) return compared; + } + return leftParts.length - rightParts.length; +} + +export function createEmptyDirectoryResumeCheckpoint(): DirectoryResumeCheckpoint { + return { + version: 1, + coveredEntries: 0, + completedEntries: 0, + manifestHash: EMPTY_DIRECTORY_MANIFEST_HASH, + }; +} + +export function isValidDirectoryResumeCheckpoint(value: unknown): value is DirectoryResumeCheckpoint { + if (!value || typeof value !== "object") return false; + const checkpoint = value as Partial; + return checkpoint.version === 1 + && Number.isSafeInteger(checkpoint.coveredEntries) + && (checkpoint.coveredEntries ?? -1) >= 0 + && Number.isSafeInteger(checkpoint.completedEntries) + && (checkpoint.completedEntries ?? -1) >= 0 + && (checkpoint.completedEntries ?? 0) <= (checkpoint.coveredEntries ?? -1) + && typeof checkpoint.manifestHash === "string" + && /^[a-f0-9]{64}$/.test(checkpoint.manifestHash); +} diff --git a/domain/sftpTransferCenter.test.ts b/domain/sftpTransferCenter.test.ts index 50d135629..339286ea6 100644 --- a/domain/sftpTransferCenter.test.ts +++ b/domain/sftpTransferCenter.test.ts @@ -2,10 +2,12 @@ import assert from "node:assert/strict"; import test from "node:test"; import type { TransferTask } from "./models"; +import { createEmptyDirectoryResumeCheckpoint } from "./sftpDirectoryCheckpoint"; import { createSftpTransferCenter, deserializeSftpTransferCenter, pruneSftpTransferHistory, + serializeSftpTransferCenter, validateTransferResumeSource, } from "./sftpTransferCenter"; @@ -150,7 +152,7 @@ test("resume rejects changed or shortened source files", () => { assert.match(validateTransferResumeSource(resumable, { size: 40, lastModified: 50 }) ?? "", /checkpoint/); }); -test("prune keeps completed children of unfinished directory parents", () => { +test("prune upgrades legacy completed children into the parent checkpoint", () => { const now = Date.now(); const parent: TransferTask = { ...task("dir", "interrupted", now - 1000), @@ -168,11 +170,126 @@ test("prune keeps completed children of unfinished directory parents", () => { endTime: now - 1000 + i, })); const pruned = pruneSftpTransferHistory([parent, ...children, ...noise], now); - assert.ok(pruned.some((item) => item.id === "dir")); - for (const child of children) { - assert.ok( - pruned.some((item) => item.id === child.id), - `expected checkpoint child ${child.id} to survive prune`, - ); + assert.deepEqual(pruned.filter((item) => item.id === "dir").map((item) => item.id), ["dir"]); + assert.equal(pruned.some((item) => item.parentTaskId === "dir"), false); + assert.equal(pruned.find((item) => item.id === "dir")?.directoryResumeCheckpoint?.completedEntries, 5); +}); + +test("prune compacts 50,000 completed directory children into a bounded parent checkpoint", () => { + const pruneStartedAt = Date.now(); + const now = Date.now(); + const parent: TransferTask = { + ...task("huge-dir", "interrupted", now - 1000), + isDirectory: true, + fileName: "huge-folder", + totalBytes: 50_000, + transferredBytes: 50_000, + }; + const children = Array.from({ length: 50_000 }, (_, index) => ({ + ...task(`huge-child-${index}`, "completed", now - 500 + index), + parentTaskId: parent.id, + sourcePath: `/source/file-${index}`, + targetPath: `/target/file-${index}`, + endTime: now, + directoryEntryIndex: index, + directoryEntryIdentity: index.toString(16).padStart(64, "0"), + } as TransferTask)); + + const pruned = pruneSftpTransferHistory([parent, ...children], now); + const compactedParent = pruned.find((item) => item.id === parent.id) as TransferTask & { + directoryResumeCheckpoint?: { coveredEntries: number; completedEntries: number }; + }; + + assert.equal(pruned.length, 1, "completed children must leave the full task array"); + assert.equal(compactedParent.directoryResumeCheckpoint?.coveredEntries, 50_000); + assert.equal(compactedParent.directoryResumeCheckpoint?.completedEntries, 50_000); + assert.ok( + serializeSftpTransferCenter(pruned).length < 10_000, + "persisted history must stay bounded instead of serializing every completed child", + ); + const restored = deserializeSftpTransferCenter(serializeSftpTransferCenter(pruned)); + assert.equal(restored.tasks.length, 1); + assert.equal(restored.tasks[0]?.directoryResumeCheckpoint?.completedEntries, 50_000); + assert.ok(Date.now() - pruneStartedAt < 5_000, "50k compaction should finish within 5 seconds"); +}); + +test("history pruning groups many directory parents without quadratic rescans", () => { + const now = Date.now(); + const tasks: TransferTask[] = []; + for (let index = 0; index < 20_000; index += 1) { + const parent: TransferTask = { + ...task(`many-parent-${index}`, "paused", index), + isDirectory: true, + directoryResumeCheckpoint: createEmptyDirectoryResumeCheckpoint(), + }; + tasks.push(parent, { + ...task(`many-child-${index}`, "paused", index), + parentTaskId: parent.id, + directoryEntryIndex: 0, + directoryEntryIdentity: "a".repeat(64), + }); } + + const startedAt = Date.now(); + const pruned = pruneSftpTransferHistory(tasks, now); + assert.equal(pruned.length, tasks.length); + assert.ok(Date.now() - startedAt < 2_000, "parent grouping should stay linear"); +}); + +test("out-of-order completion keeps the unfinished hole while compacting the later child", () => { + const now = Date.now(); + const parent: TransferTask = { + ...task("out-of-order-dir", "paused", now - 1000), + isDirectory: true, + totalBytes: 2, + transferredBytes: 1, + }; + const unfinished = { + ...task("index-0", "paused", now), + parentTaskId: parent.id, + directoryEntryIndex: 0, + directoryEntryIdentity: "a".repeat(64), + } as TransferTask; + const completed = { + ...task("index-1", "completed", now), + parentTaskId: parent.id, + directoryEntryIndex: 1, + directoryEntryIdentity: "b".repeat(64), + } as TransferTask; + + const pruned = pruneSftpTransferHistory([parent, unfinished, completed], now); + const compactedParent = pruned.find((item) => item.id === parent.id)!; + + assert.deepEqual(pruned.map((item) => item.id).sort(), [parent.id, unfinished.id].sort()); + assert.equal(compactedParent.directoryResumeCheckpoint?.coveredEntries, 2); + assert.equal(compactedParent.directoryResumeCheckpoint?.completedEntries, 1); + assert.equal(pruned.find((item) => item.id === unfinished.id)?.status, "paused"); +}); + +test("failed directory history drops an ambiguous compact checkpoint before child history is capped", () => { + const now = Date.now(); + const parent: TransferTask = { + ...task("failed-dir", "failed", now - 1000), + isDirectory: true, + checkpointBytes: 500, + endTime: now, + directoryResumeCheckpoint: { + version: 1, + coveredEntries: 500, + completedEntries: 0, + manifestHash: "a".repeat(64), + }, + }; + const failedChildren = Array.from({ length: 500 }, (_, index) => ({ + ...task(`failed-child-${index}`, "failed", now - 900 + index), + parentTaskId: parent.id, + directoryEntryIndex: index, + directoryEntryIdentity: index.toString(16).padStart(64, "0"), + endTime: now - 1 - index, + } as TransferTask)); + + const pruned = pruneSftpTransferHistory([parent, ...failedChildren], now); + const retainedParent = pruned.find((item) => item.id === parent.id); + assert.equal(retainedParent?.directoryResumeCheckpoint, undefined); + assert.ok(pruned.length <= 200); }); diff --git a/domain/sftpTransferCenter.ts b/domain/sftpTransferCenter.ts index f91af9570..35dc5b852 100644 --- a/domain/sftpTransferCenter.ts +++ b/domain/sftpTransferCenter.ts @@ -1,4 +1,11 @@ import type { TransferTask } from "./models"; +import { + appendDirectoryManifestIdentity, + compareDirectoryTraversalPaths, + createDirectoryEntryIdentity, + createEmptyDirectoryResumeCheckpoint, + isValidDirectoryResumeCheckpoint, +} from "./sftpDirectoryCheckpoint"; export const SFTP_TRANSFER_CENTER_VERSION = 1; export const SFTP_TRANSFER_HISTORY_MAX = 200; @@ -36,9 +43,12 @@ const SAFE_TASK_KEYS: ReadonlySet = new Set([ "sourceFingerprint", "reconnectRequired", "lifecycleEpoch", + "directoryEntryIndex", + "directoryEntryIdentity", + "directoryResumeCheckpoint", ]); -function sanitizeTask(value: unknown): TransferTask | null { +export function sanitizeSftpTransferTask(value: unknown): TransferTask | null { if (!value || typeof value !== "object") return null; const source = value as Record; if (typeof source.id !== "string" || typeof source.fileName !== "string") return null; @@ -48,6 +58,25 @@ function sanitizeTask(value: unknown): TransferTask | null { if (source[key] !== undefined) sanitized[key] = source[key]; } const task = sanitized as unknown as TransferTask; + if (!Number.isSafeInteger(task.directoryEntryIndex) || (task.directoryEntryIndex ?? -1) < 0) { + task.directoryEntryIndex = undefined; + } + if (!/^[a-f0-9]{64}$/.test(task.directoryEntryIdentity ?? "")) { + task.directoryEntryIdentity = undefined; + } + if (!isValidDirectoryResumeCheckpoint(task.directoryResumeCheckpoint)) { + task.directoryResumeCheckpoint = undefined; + } else { + const checkpoint = task.directoryResumeCheckpoint; + // Rebuild the fixed schema so unknown/legacy payload fields cannot smuggle + // an unbounded path list back into localStorage. + task.directoryResumeCheckpoint = { + version: 1, + coveredEntries: checkpoint.coveredEntries, + completedEntries: checkpoint.completedEntries, + manifestHash: checkpoint.manifestHash, + }; + } // After force-quit / restart no backend stream remains. Every unfinished row // (including a previously interrupted row that was already persisted) needs a // dedicated reconnect — do not leave reconnectRequired false. @@ -69,7 +98,7 @@ function sanitizeTask(value: unknown): TransferTask | null { export function serializeSftpTransferCenter(tasks: readonly TransferTask[]): string { return JSON.stringify({ version: SFTP_TRANSFER_CENTER_VERSION, - tasks: tasks.map((task) => sanitizeTask(task)).filter((task): task is TransferTask => task !== null), + tasks: tasks.map((task) => sanitizeSftpTransferTask(task)).filter((task): task is TransferTask => task !== null), } satisfies PersistedSftpTransferCenter); } @@ -82,7 +111,7 @@ export function deserializeSftpTransferCenter(raw: string | null | undefined): P } return { version: SFTP_TRANSFER_CENTER_VERSION, - tasks: parsed.tasks.map(sanitizeTask).filter((task): task is TransferTask => task !== null), + tasks: parsed.tasks.map(sanitizeSftpTransferTask).filter((task): task is TransferTask => task !== null), }; } catch { return { version: SFTP_TRANSFER_CENTER_VERSION, tasks: [] }; @@ -93,17 +122,122 @@ export function pruneSftpTransferHistory( tasks: readonly TransferTask[], now = Date.now(), ): TransferTask[] { - const unfinished = tasks.filter((task) => !TERMINAL_STATUSES.has(task.status)); + // A failed terminal directory can have more retained failed children than + // the global history cap. Once those rows are evicted, a compact checkpoint + // cannot distinguish them from completed children. Drop the ambiguous skip + // state so Retry/Resume safely walks every source entry again. + let compacted = tasks.map((task) => ( + task.isDirectory + && !task.parentTaskId + && task.status === "failed" + && task.directoryResumeCheckpoint + ? { ...task, directoryResumeCheckpoint: undefined } + : task + )); + const unfinishedDirectoryParents = compacted.filter((task) => ( + task.isDirectory + && !task.parentTaskId + && !TERMINAL_STATUSES.has(task.status) + )); + const childrenByParent = new Map(); + for (const task of compacted) { + if (!task.parentTaskId) continue; + const children = childrenByParent.get(task.parentTaskId) ?? []; + children.push(task); + childrenByParent.set(task.parentTaskId, children); + } + const compactedChildIds = new Set(); + const parentUpdates = new Map(); + const normalizedChildUpdates = new Map(); + for (const parent of unfinishedDirectoryParents) { + let parentChildren = childrenByParent.get(parent.id) ?? []; + // Upgrade pre-checkpoint history safely. The sorted known set becomes a + // candidate prefix; dedicated resume validates it against a fresh walk. + // Any added/reordered/modified source entry invalidates the hash and causes + // a conservative restart instead of an incorrect skip. + if ( + !isValidDirectoryResumeCheckpoint(parent.directoryResumeCheckpoint) + && parentChildren.some((child) => ( + !Number.isSafeInteger(child.directoryEntryIndex) + || !/^[a-f0-9]{64}$/.test(child.directoryEntryIdentity ?? "") + )) + ) { + parentChildren = [...parentChildren] + .sort((left, right) => compareDirectoryTraversalPaths(left.sourcePath, right.sourcePath)) + .map((child, directoryEntryIndex) => ({ + ...child, + directoryEntryIndex, + directoryEntryIdentity: createDirectoryEntryIdentity({ + sourcePath: child.sourcePath, + targetPath: child.targetPath, + size: child.totalBytes, + lastModified: child.sourceLastModified, + }), + })); + for (const child of parentChildren) normalizedChildUpdates.set(child.id, child); + } + const checkpoint = isValidDirectoryResumeCheckpoint(parent.directoryResumeCheckpoint) + ? { ...parent.directoryResumeCheckpoint } + : createEmptyDirectoryResumeCheckpoint(); + const initialCoveredEntries = checkpoint.coveredEntries; + const childrenByIndex = new Map(); + for (const child of parentChildren) { + if (!Number.isSafeInteger(child.directoryEntryIndex) || (child.directoryEntryIndex ?? -1) < 0) continue; + if (!/^[a-f0-9]{64}$/.test(child.directoryEntryIdentity ?? "")) continue; + if (!childrenByIndex.has(child.directoryEntryIndex!)) { + childrenByIndex.set(child.directoryEntryIndex!, child); + } + } + while (childrenByIndex.has(checkpoint.coveredEntries)) { + const child = childrenByIndex.get(checkpoint.coveredEntries)!; + checkpoint.manifestHash = appendDirectoryManifestIdentity( + checkpoint.manifestHash, + child.directoryEntryIdentity!, + ); + checkpoint.coveredEntries += 1; + } + let newlyCompacted = 0; + for (const child of parentChildren) { + if ( + child.parentTaskId === parent.id + && child.status === "completed" + && Number.isSafeInteger(child.directoryEntryIndex) + && (child.directoryEntryIndex ?? checkpoint.coveredEntries) < checkpoint.coveredEntries + && /^[a-f0-9]{64}$/.test(child.directoryEntryIdentity ?? "") + ) { + compactedChildIds.add(child.id); + newlyCompacted += 1; + } + } + if (checkpoint.coveredEntries !== initialCoveredEntries || newlyCompacted > 0) { + checkpoint.completedEntries = Math.min( + checkpoint.coveredEntries, + checkpoint.completedEntries + newlyCompacted, + ); + parentUpdates.set(parent.id, { + ...parent, + directoryResumeCheckpoint: checkpoint, + transferredBytes: Math.max(parent.transferredBytes, checkpoint.completedEntries), + }); + } + } + if (compactedChildIds.size > 0 || parentUpdates.size > 0 || normalizedChildUpdates.size > 0) { + compacted = compacted + .filter((task) => !compactedChildIds.has(task.id)) + .map((task) => parentUpdates.get(task.id) ?? normalizedChildUpdates.get(task.id) ?? task); + } + + const unfinished = compacted.filter((task) => !TERMINAL_STATUSES.has(task.status)); const unfinishedIds = new Set(unfinished.map((task) => task.id)); - // Keep terminal children of unfinished parents forever (until the parent - // finishes) so directory resume can skip already-completed files. - const checkpointChildren = tasks.filter((task) => + // Keep terminal exceptions that cannot be compacted (failed/cancelled or a + // legacy gap). Completed indexed children normally live in the parent hash. + const checkpointChildren = compacted.filter((task) => TERMINAL_STATUSES.has(task.status) && !!task.parentTaskId && unfinishedIds.has(task.parentTaskId), ); const checkpointIds = new Set(checkpointChildren.map((task) => task.id)); - const terminal = tasks + const terminal = compacted .filter((task) => TERMINAL_STATUSES.has(task.status) && !checkpointIds.has(task.id)) .filter((task) => now - (task.endTime ?? task.updatedAt ?? task.startTime) <= SFTP_TRANSFER_HISTORY_MAX_AGE_MS) .sort((a, b) => (b.endTime ?? b.updatedAt ?? b.startTime) - (a.endTime ?? a.updatedAt ?? a.startTime)) diff --git a/electron/bridges/ai/codexHelpers.cjs b/electron/bridges/ai/codexHelpers.cjs index 181a4ac17..443a9ef69 100644 --- a/electron/bridges/ai/codexHelpers.cjs +++ b/electron/bridges/ai/codexHelpers.cjs @@ -10,6 +10,7 @@ const { createHash } = require("node:crypto"); const { existsSync, readFileSync } = require("node:fs"); const os = require("node:os"); const path = require("node:path"); +const { StringDecoder } = require("node:string_decoder"); const { stripAnsi, extractFirstNonLocalhostUrl, toUnpackedAsarPath } = require("./shellUtils.cjs"); @@ -17,6 +18,10 @@ const { stripAnsi, extractFirstNonLocalhostUrl, toUnpackedAsarPath } = require(" const codexLoginSessions = new Map(); let codexValidationCache = null; +const MAX_CODEX_LOGIN_OUTPUT_BYTES = 64 * 1024; +const MAX_CODEX_LOGIN_OUTPUT_CHARS = MAX_CODEX_LOGIN_OUTPUT_BYTES; +const MAX_CODEX_LOGIN_TERMINAL_SESSIONS = 8; +const CODEX_LOGIN_KILL_GRACE_MS = 750; const CODEX_AUTH_HINTS = [ "not logged in", @@ -41,10 +46,78 @@ function appendCodexLoginOutput(session, chunk) { const cleanChunk = stripAnsi(chunk); if (!cleanChunk) return; - session.output += cleanChunk; + const combined = `${session.output || ""}${cleanChunk}`; if (!session.url) { - session.url = extractFirstNonLocalhostUrl(session.output); + session.url = extractFirstNonLocalhostUrl(combined); } + session.output = retainUtf8Tail(combined, MAX_CODEX_LOGIN_OUTPUT_BYTES); +} + +function retainUtf8Tail(value, maxBytes) { + const buffer = Buffer.from(String(value || ""), "utf8"); + if (buffer.length <= maxBytes) return String(value || ""); + let start = buffer.length - maxBytes; + // Never begin inside a UTF-8 continuation sequence. + while (start < buffer.length && (buffer[start] & 0xc0) === 0x80) start += 1; + return buffer.subarray(start).toString("utf8"); +} + +function createCodexLoginOutputDecoder(session) { + const decoder = new StringDecoder("utf8"); + let ended = false; + return { + write(chunk) { + if (ended) return; + const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk)); + appendCodexLoginOutput(session, decoder.write(buffer)); + }, + end() { + if (ended) return; + ended = true; + appendCodexLoginOutput(session, decoder.end()); + }, + }; +} + +function pruneCodexLoginSessions() { + const terminalSessionIds = []; + for (const [sessionId, session] of codexLoginSessions) { + if (session?.state !== "running" && !session?.process) terminalSessionIds.push(sessionId); + } + const excess = terminalSessionIds.length - MAX_CODEX_LOGIN_TERMINAL_SESSIONS; + for (let index = 0; index < excess; index += 1) { + codexLoginSessions.delete(terminalSessionIds[index]); + } +} + +function clearCodexLoginKillTimer(session, clearTimeoutFn = clearTimeout) { + if (!session?.killTimer) return; + clearTimeoutFn(session.killTimer); + session.killTimer = null; +} + +function stopCodexLoginProcess(session, { + setTimeoutFn = setTimeout, + clearTimeoutFn = clearTimeout, +} = {}) { + const child = session?.process; + if (!child) return false; + clearCodexLoginKillTimer(session, clearTimeoutFn); + session.killTimer = setTimeoutFn(() => { + session.killTimer = null; + if (session.process !== child) return; + try { child.kill("SIGKILL"); } catch {} + }, CODEX_LOGIN_KILL_GRACE_MS); + session.killTimer?.unref?.(); + try { child.kill("SIGTERM"); } catch {} + return true; +} + +function recordCodexLoginSession(session) { + if (!session?.id) return; + codexLoginSessions.delete(session.id); + codexLoginSessions.set(session.id, session); + pruneCodexLoginSessions(); } function toCodexLoginSessionResponse(session) { @@ -377,8 +450,17 @@ function setCodexValidationCache(value) { } module.exports = { + MAX_CODEX_LOGIN_OUTPUT_BYTES, + MAX_CODEX_LOGIN_OUTPUT_CHARS, + MAX_CODEX_LOGIN_TERMINAL_SESSIONS, + CODEX_LOGIN_KILL_GRACE_MS, codexLoginSessions, appendCodexLoginOutput, + createCodexLoginOutputDecoder, + pruneCodexLoginSessions, + recordCodexLoginSession, + clearCodexLoginKillTimer, + stopCodexLoginProcess, toCodexLoginSessionResponse, getActiveCodexLoginSession, normalizeCodexIntegrationState, diff --git a/electron/bridges/ai/codexHelpers.test.cjs b/electron/bridges/ai/codexHelpers.test.cjs index 92ac784fd..f8469fa62 100644 --- a/electron/bridges/ai/codexHelpers.test.cjs +++ b/electron/bridges/ai/codexHelpers.test.cjs @@ -2,12 +2,123 @@ const test = require("node:test"); const assert = require("node:assert/strict"); const { + MAX_CODEX_LOGIN_OUTPUT_CHARS, + MAX_CODEX_LOGIN_OUTPUT_BYTES, + MAX_CODEX_LOGIN_TERMINAL_SESSIONS, + CODEX_LOGIN_KILL_GRACE_MS, + appendCodexLoginOutput, + createCodexLoginOutputDecoder, appendCodexChatGptValidationFailure, + codexLoginSessions, extractCodexError, isCodexAuthError, normalizeCodexIntegrationState, + recordCodexLoginSession, + stopCodexLoginProcess, } = require("./codexHelpers.cjs"); +test("Codex login output keeps a bounded tail", () => { + const session = { output: "", url: null }; + + appendCodexLoginOutput(session, "a".repeat(MAX_CODEX_LOGIN_OUTPUT_CHARS)); + appendCodexLoginOutput(session, "tail-marker"); + + assert.equal(session.output.length, MAX_CODEX_LOGIN_OUTPUT_CHARS); + assert.match(session.output, /tail-marker$/); +}); + +test("Codex login output limit is measured in UTF-8 bytes without cutting characters", () => { + const session = { output: "", url: null }; + appendCodexLoginOutput(session, "中".repeat(MAX_CODEX_LOGIN_OUTPUT_BYTES)); + + assert.ok(Buffer.byteLength(session.output, "utf8") <= MAX_CODEX_LOGIN_OUTPUT_BYTES); + assert.doesNotMatch(session.output, /�/u); + assert.match(session.output, /^中+$/u); +}); + +test("Codex login stdout and stderr decode split UTF-8 independently when interleaved", () => { + const session = { output: "", url: null }; + const stdout = createCodexLoginOutputDecoder(session); + const stderr = createCodexLoginOutputDecoder(session); + const outBytes = Buffer.from("中文", "utf8"); + const errBytes = Buffer.from("错误", "utf8"); + stdout.write(outBytes.subarray(0, 2)); + stderr.write(errBytes.subarray(0, 1)); + stdout.write(outBytes.subarray(2)); + stderr.write(errBytes.subarray(1)); + stdout.end(); + stderr.end(); + + assert.equal(session.output, "中文错误"); +}); + +test("Codex login history retains only bounded terminal sessions", (t) => { + t.after(() => codexLoginSessions.clear()); + codexLoginSessions.clear(); + recordCodexLoginSession({ id: "running", state: "running", process: { killed: false } }); + + for (let index = 0; index < MAX_CODEX_LOGIN_TERMINAL_SESSIONS + 3; index += 1) { + recordCodexLoginSession({ id: `done-${index}`, state: "success", process: null }); + } + + assert.equal(codexLoginSessions.has("running"), true); + assert.equal(codexLoginSessions.has("done-0"), false); + assert.equal(codexLoginSessions.size, MAX_CODEX_LOGIN_TERMINAL_SESSIONS + 1); +}); + +test("a newly completed Codex login remains available while older records are pruned", (t) => { + t.after(() => codexLoginSessions.clear()); + codexLoginSessions.clear(); + for (let index = 0; index < MAX_CODEX_LOGIN_TERMINAL_SESSIONS; index += 1) { + recordCodexLoginSession({ id: `old-${index}`, state: "success", process: null }); + } + const current = { id: "current", state: "running", process: { killed: false } }; + recordCodexLoginSession(current); + + current.state = "success"; + current.process = null; + recordCodexLoginSession(current); + + assert.equal(codexLoginSessions.size, MAX_CODEX_LOGIN_TERMINAL_SESSIONS); + assert.equal(codexLoginSessions.has("current"), true); + assert.equal(codexLoginSessions.has("old-0"), false); +}); + +test("live cancelled login processes stay tracked until they close", (t) => { + t.after(() => codexLoginSessions.clear()); + codexLoginSessions.clear(); + for (let index = 0; index < MAX_CODEX_LOGIN_TERMINAL_SESSIONS + 2; index += 1) { + recordCodexLoginSession({ + id: `cancelled-${index}`, + state: "cancelled", + process: { kill() {} }, + }); + } + + assert.equal(codexLoginSessions.size, MAX_CODEX_LOGIN_TERMINAL_SESSIONS + 2); +}); + +test("Codex login cancellation escalates from TERM to KILL", () => { + const signals = []; + const scheduled = []; + let escalate; + const session = { + process: { kill: (signal) => signals.push(signal) }, + killTimer: null, + }; + stopCodexLoginProcess(session, { + setTimeoutFn: (callback, delay) => { + scheduled.push(delay); + escalate = callback; + return { unref() {} }; + }, + }); + escalate(); + + assert.deepEqual(signals, ["SIGTERM", "SIGKILL"]); + assert.deepEqual(scheduled, [CODEX_LOGIN_KILL_GRACE_MS]); +}); + test("normalizeCodexIntegrationState recognizes ChatGPT login status", () => { assert.equal( normalizeCodexIntegrationState("Logged in using ChatGPT"), diff --git a/electron/bridges/ai/ptyExec.cjs b/electron/bridges/ai/ptyExec.cjs index 46a65c34c..abfafb14d 100644 --- a/electron/bridges/ai/ptyExec.cjs +++ b/electron/bridges/ai/ptyExec.cjs @@ -10,6 +10,8 @@ "use strict"; const crypto = require("crypto"); +const { StringDecoder } = require("node:string_decoder"); +const { invalidateSshTransport } = require("../sshTransportInvalidation.cjs"); const { createStatefulDecoder, detectShellKind, @@ -24,6 +26,8 @@ const { stripAnsi, } = require("./ptyExecHelpers.cjs"); +const DEFAULT_FOREGROUND_PTY_CAPTURE_CHARS = 1024 * 1024; + function stripJobMarkerLines(text, marker) { return text.replace( new RegExp(`^([^\r\n]*?)${marker}[^\r\n]*[\r\n]*`, "gm"), @@ -52,6 +56,9 @@ function startPtyJob(ptyStream, command, options) { const resolvedShellKind = resolveEffectiveShellKind(shellKind, expectedPrompt, { loginShellHint, }); + const captureLimitChars = maxBufferedChars > 0 + ? maxBufferedChars + : DEFAULT_FOREGROUND_PTY_CAPTURE_CHARS; const CANCEL_RETRY_MS = 5000; const CANCEL_WALL_TIMEOUT_MS = 30000; @@ -80,6 +87,7 @@ function startPtyJob(ptyStream, command, options) { const cleanupFns = []; let pendingStart = ""; let resolveResult; + const outputDecoder = new StringDecoder("utf8"); const resultPromise = new Promise((resolve) => { resolveResult = resolve; }); @@ -242,7 +250,17 @@ function startPtyJob(ptyStream, command, options) { function appendToVisible(text) { if (!text) return; - const normalized = consumeVisibleText(visibleCarry, text); + let boundedInput = text; + if (boundedInput.length > captureLimitChars) { + const skipped = boundedInput.length - captureLimitChars; + boundedInput = boundedInput.slice(-captureLimitChars); + // A skipped prefix may end inside a control sequence; drop any carry + // from that prefix and treat offsets as a conservative raw-char count. + visibleCarry = ""; + visibleOutputOffset += skipped; + visibleHighWatermark += skipped; + } + const normalized = consumeVisibleText(visibleCarry, boundedInput); visibleCarry = normalized.carry; if (!normalized.visibleText) return; @@ -279,14 +297,14 @@ function startPtyJob(ptyStream, command, options) { if (!cleanVisible) return; } visibleHighWatermark += cleanVisible.length; - const next = appendBoundedOutput(visibleOutput, cleanVisible, maxBufferedChars); + const next = appendBoundedOutput(visibleOutput, cleanVisible, captureLimitChars); visibleOutput = next.text; visibleOutputOffset += next.dropped; } function appendToOutput(text) { if (!text) return; - const next = appendBoundedOutput(output, text, maxBufferedChars); + const next = appendBoundedOutput(output, text, captureLimitChars); output = next.text; appendToVisible(text); } @@ -321,7 +339,7 @@ function startPtyJob(ptyStream, command, options) { const leftover = stripJobMarkerLines(visibleMarkerCarry, marker); visibleMarkerCarry = ""; if (leftover) { - const next = appendBoundedOutput(visibleOutput, leftover, maxBufferedChars); + const next = appendBoundedOutput(visibleOutput, leftover, captureLimitChars); visibleOutput = next.text; visibleOutputOffset += next.dropped; } @@ -394,17 +412,17 @@ function startPtyJob(ptyStream, command, options) { } function onData(data) { - const text = data.toString(); + const bytes = Buffer.isBuffer(data) + ? data + : data instanceof Uint8Array + ? Buffer.from(data) + : Buffer.from(String(data ?? "")); + const text = outputDecoder.write(bytes); + if (!text) return; armOutputTimeout(); if (!foundStart) { preStartOutput += text; - // Cap preStartOutput for background jobs so a noisy idle PTY can't - // accumulate megabytes before the start marker arrives. We only need - // enough tail to find the marker boundary. - if (maxBufferedChars > 0 && preStartOutput.length > maxBufferedChars) { - preStartOutput = preStartOutput.slice(preStartOutput.length - maxBufferedChars); - } const combined = pendingStart + text; pendingStart = ""; const startMarker = marker + "_S"; @@ -462,6 +480,16 @@ function startPtyJob(ptyStream, command, options) { return; } } + // A noisy PTY before the start marker must not grow without bound. Keep + // enough tail for marker/prompt detection; the current chunk was already + // scanned above so dropping its prefix cannot hide a completed marker. + if (preStartOutput.length > captureLimitChars) { + preStartOutput = preStartOutput.slice(-captureLimitChars); + } + const markerCarryLimit = marker.length + 256; + if (pendingStart.length > markerCarryLimit) { + pendingStart = pendingStart.slice(-markerCarryLimit); + } // If we're cancelling a still-queued command and the shell has returned // to its idle prompt, finish immediately as Cancelled instead of waiting // for the cancel wall-clock timer. @@ -601,13 +629,18 @@ function execViaPty(ptyStream, command, options) { * @param {string} command - The command to execute * @param {object} [options] * @param {number} [options.timeoutMs=60000] - Command timeout in milliseconds + * @param {number} [options.maxOutputBytes=1048576] - Combined stdout/stderr hard limit */ function execViaChannel(sshClient, command, options) { const { timeoutMs = 60000, + maxOutputBytes = 1024 * 1024, trackForCancellation = null, chatSessionId, } = options || {}; + const outputLimitBytes = Number.isSafeInteger(maxOutputBytes) && maxOutputBytes > 0 + ? maxOutputBytes + : 1024 * 1024; return new Promise((resolve) => { // Register a *pending* cancellation marker synchronously, before @@ -620,76 +653,163 @@ function execViaChannel(sshClient, command, options) { // check the latch and short-circuit instead of starting work. const pendingMarker = `__NCMCP_CH_PENDING_${Date.now().toString(36)}_${crypto.randomBytes(8).toString('hex')}__`; let cancelled = false; + let settled = false; + let openingTimer = null; + let activeMarker = null; + const settle = (result) => { + if (settled) return false; + settled = true; + clearTimeout(openingTimer); + if (trackForCancellation) { + trackForCancellation.delete(pendingMarker); + if (activeMarker) trackForCancellation.delete(activeMarker); + } + resolve(result); + return true; + }; + const cancelPending = () => { + cancelled = true; + settle({ ok: false, stdout: "", stderr: "", exitCode: -1, error: "Cancelled" }); + invalidateSshTransport(sshClient); + }; if (trackForCancellation) { trackForCancellation.set(pendingMarker, { chatSessionId: chatSessionId || null, - cancel: () => { cancelled = true; }, - cleanup: () => { /* nothing pending to clean up before channel opens */ }, + cancel: cancelPending, + cleanup: cancelPending, }); } + openingTimer = setTimeout(() => { + const timeoutSec = Math.round(timeoutMs / 1000); + settle({ + ok: false, + stdout: "", + stderr: "", + exitCode: -1, + error: `Command timed out (${timeoutSec}s) while opening SSH exec channel`, + }); + invalidateSshTransport(sshClient); + }, timeoutMs); + openingTimer.unref?.(); + try { sshClient.exec(command, (err, execStream) => { - if (trackForCancellation) { - trackForCancellation.delete(pendingMarker); - } - if (cancelled) { - if (execStream) { - try { execStream.close(); } catch { /* ignore */ } - } - resolve({ ok: false, stdout: "", stderr: "", exitCode: -1, error: "Cancelled" }); - return; - } - if (err) { - resolve({ ok: false, error: err.message }); - return; - } - if (!execStream) { - resolve({ ok: false, error: 'Failed to create exec stream', exitCode: 1 }); - return; - } - const marker = `__NCMCP_CH_${Date.now().toString(36)}_${crypto.randomBytes(16).toString('hex')}__`; - let stdout = ""; - let stderr = ""; - let finished = false; - const finish = (result) => { - if (finished) return; - finished = true; - clearTimeout(timeoutId); if (trackForCancellation) { - trackForCancellation.delete(marker); + trackForCancellation.delete(pendingMarker); } - resolve(result); - }; - const timeoutId = setTimeout(() => { - try { execStream.close(); } catch { /* ignore */ } - const timeoutSec = Math.round(timeoutMs / 1000); - finish({ ok: false, stdout, stderr, exitCode: -1, error: `Command timed out (${timeoutSec}s)` }); - }, timeoutMs); - if (trackForCancellation) { - trackForCancellation.set(marker, { - chatSessionId: chatSessionId || null, - cancel: () => { + clearTimeout(openingTimer); + if (settled || cancelled) { + if (execStream) { try { execStream.close(); } catch { /* ignore */ } - finish({ ok: false, stdout, stderr, exitCode: -1, error: "Cancelled" }); - }, - cleanup: () => { - clearTimeout(timeoutId); - try { execStream.close(); } catch { /* ignore */ } - }, - }); - } - execStream.on("data", (data) => { stdout += data.toString(); }); - execStream.stderr.on("data", (data) => { stderr += data.toString(); }); - execStream.on("close", (code) => { - // code is null when SSH disconnects or process is signal-terminated - if (code == null) { - finish({ ok: false, stdout, stderr, exitCode: -1, error: "Command terminated unexpectedly (connection lost or signal)" }); - } else { - finish({ ok: code === 0, stdout, stderr, exitCode: code }); + } + if (!settled) cancelPending(); + return; } + if (err) { + settle({ ok: false, error: err.message }); + return; + } + if (!execStream) { + settle({ ok: false, error: 'Failed to create exec stream', exitCode: 1 }); + return; + } + activeMarker = `__NCMCP_CH_${Date.now().toString(36)}_${crypto.randomBytes(16).toString('hex')}__`; + let stdout = ""; + let stderr = ""; + let outputBytes = 0; + const stdoutDecoder = new StringDecoder("utf8"); + const stderrDecoder = new StringDecoder("utf8"); + let finished = false; + let timeoutId = null; + let cleanupStreamListeners = () => {}; + const finish = (result) => { + if (finished) return; + finished = true; + clearTimeout(timeoutId); + cleanupStreamListeners(); + settle(result); + }; + const terminateExecStream = () => { + try { execStream.close?.(); } catch { /* ignore */ } + try { execStream.destroy?.(); } catch { /* ignore */ } + }; + const appendOutput = (target, data) => { + if (finished) return; + const chunk = Buffer.isBuffer(data) ? data : Buffer.from(String(data)); + const remaining = Math.max(0, outputLimitBytes - outputBytes); + if (chunk.length <= remaining) { + if (target === "stdout") stdout += stdoutDecoder.write(chunk); + else stderr += stderrDecoder.write(chunk); + outputBytes += chunk.length; + return; + } + if (remaining > 0) { + const accepted = chunk.subarray(0, remaining); + if (target === "stdout") stdout += stdoutDecoder.write(accepted); + else stderr += stderrDecoder.write(accepted); + outputBytes += remaining; + } + finish({ + ok: false, + stdout, + stderr, + exitCode: -1, + error: `Command output exceeded the ${outputLimitBytes} byte limit`, + }); + terminateExecStream(); + }; + timeoutId = setTimeout(() => { + const timeoutSec = Math.round(timeoutMs / 1000); + finish({ ok: false, stdout, stderr, exitCode: -1, error: `Command timed out (${timeoutSec}s)` }); + terminateExecStream(); + }, timeoutMs); + if (trackForCancellation) { + trackForCancellation.set(activeMarker, { + chatSessionId: chatSessionId || null, + cancel: () => { + finish({ ok: false, stdout, stderr, exitCode: -1, error: "Cancelled" }); + terminateExecStream(); + }, + cleanup: () => { + clearTimeout(timeoutId); + terminateExecStream(); + }, + }); + } + const onStdoutData = (data) => appendOutput("stdout", data); + const onStderrData = (data) => appendOutput("stderr", data); + const onClose = (code) => { + // code is null when SSH disconnects or process is signal-terminated + if (code == null) { + finish({ ok: false, stdout, stderr, exitCode: -1, error: "Command terminated unexpectedly (connection lost or signal)" }); + } else { + finish({ ok: code === 0, stdout, stderr, exitCode: code }); + } + }; + const onError = (error) => { + finish({ + ok: false, + stdout, + stderr, + exitCode: -1, + error: error?.message || String(error || "SSH exec channel failed"), + }); + terminateExecStream(); + }; + cleanupStreamListeners = () => { + execStream.removeListener?.("data", onStdoutData); + execStream.stderr?.removeListener?.("data", onStderrData); + execStream.stderr?.removeListener?.("error", onError); + execStream.removeListener?.("close", onClose); + execStream.removeListener?.("error", onError); + }; + execStream.on("data", onStdoutData); + execStream.stderr?.on?.("data", onStderrData); + execStream.stderr?.on?.("error", onError); + execStream.on("close", onClose); + execStream.on("error", onError); }); - }); } catch (err) { // Rare path: `sshClient.exec` itself synchronously throws (e.g. // because the underlying ssh2 client was destroyed between the @@ -697,10 +817,7 @@ function execViaChannel(sshClient, command, options) { // leak in `activePtyExecs`, and resolve as a normal failure // result instead of letting the Promise reject — the tool layer // expects `{ ok, error }` shape, not a thrown error. - if (trackForCancellation) { - trackForCancellation.delete(pendingMarker); - } - resolve({ ok: false, error: err?.message || String(err) }); + settle({ ok: false, error: err?.message || String(err) }); } }); } @@ -915,6 +1032,7 @@ function execViaRawPty(serialPort, command, options) { execViaRawPty._seq = 0; module.exports = { + DEFAULT_FOREGROUND_PTY_CAPTURE_CHARS, execViaPty, startPtyJob, execViaChannel, diff --git a/electron/bridges/ai/ptyExec.test.cjs b/electron/bridges/ai/ptyExec.test.cjs index 10053d9d0..946bd15c2 100644 --- a/electron/bridges/ai/ptyExec.test.cjs +++ b/electron/bridges/ai/ptyExec.test.cjs @@ -9,6 +9,7 @@ const { join } = require("node:path"); const { execViaPty, startPtyJob, + DEFAULT_FOREGROUND_PTY_CAPTURE_CHARS, resolveEffectiveShellKind, execViaChannel, } = require("./ptyExec.cjs"); @@ -26,6 +27,10 @@ class ShellBackedPty extends EventEmitter { } } +function markerFromWrite(data) { + return String(data).match(/(__NCMCP_[a-z0-9]+_[0-9a-f]+__)/i)?.[1] || null; +} + test("execViaPty completes when command output has no trailing newline", async () => { const result = await execViaPty(new ShellBackedPty(), "printf 'abc'", { shellKind: "posix", @@ -37,6 +42,110 @@ test("execViaPty completes when command output has no trailing newline", async ( assert.equal(result.exitCode, 0); }); +test("foreground PTY capture bounds a single 20 MiB output chunk and keeps its tail", async () => { + class LargeOutputPty extends EventEmitter { + write(data) { + const marker = markerFromWrite(data); + if (!marker) return; + queueMicrotask(() => { + this.emit("data", Buffer.from( + `${marker}_S\n${"x".repeat(20 * 1024 * 1024)}TAIL\n${marker}_E:0\n`, + )); + }); + } + } + + const result = await execViaPty(new LargeOutputPty(), "large-output", { + shellKind: "posix", + timeoutMs: 1_000, + }); + assert.equal(result.ok, true); + assert.ok(result.stdout.length <= DEFAULT_FOREGROUND_PTY_CAPTURE_CHARS); + assert.match(result.stdout, /TAIL$/u); + assert.equal(result.outputTruncated, true); + assert.ok(result.outputBaseOffset > 0); +}); + +test("foreground PTY capture preserves UTF-8 and markers split across chunks", async () => { + class SplitOutputPty extends EventEmitter { + write(data) { + const marker = markerFromWrite(data); + if (!marker) return; + queueMicrotask(() => { + const start = Buffer.from(`${marker}_S\n`); + const content = Buffer.from("中文回复", "utf8"); + const end = Buffer.from(`\n${marker}_E:0\n`); + this.emit("data", start.subarray(0, 7)); + this.emit("data", start.subarray(7)); + this.emit("data", content.subarray(0, 2)); + this.emit("data", content.subarray(2)); + this.emit("data", end.subarray(0, 9)); + this.emit("data", end.subarray(9)); + }); + } + } + + const result = await execViaPty(new SplitOutputPty(), "split-output", { + shellKind: "posix", + timeoutMs: 1_000, + }); + assert.equal(result.ok, true); + assert.equal(result.stdout, "中文回复"); +}); + +test("foreground PTY timeout returns only a bounded tail", async () => { + class TimedOutPty extends EventEmitter { + signal() {} + write(data) { + const marker = markerFromWrite(data); + if (!marker || this.started) return; + this.started = true; + queueMicrotask(() => { + this.emit("data", Buffer.from(`${marker}_S\n${"y".repeat(20 * 1024 * 1024)}`)); + }); + } + } + + const result = await execViaPty(new TimedOutPty(), "timeout-output", { + shellKind: "posix", + timeoutMs: 5, + enforceWallTimeout: true, + }); + assert.equal(result.ok, false); + assert.match(result.error, /timed out/i); + assert.ok(result.stdout.length <= DEFAULT_FOREGROUND_PTY_CAPTURE_CHARS); + assert.equal(result.outputTruncated, true); +}); + +test("foreground PTY cancellation returns only a bounded tail", async () => { + class CancelledPty extends EventEmitter { + signal() {} + write(data) { + const marker = markerFromWrite(data); + if (!marker || this.started) return; + this.started = true; + queueMicrotask(() => { + this.emit("data", Buffer.from(`${marker}_S\n${"z".repeat(20 * 1024 * 1024)}`)); + }); + } + } + + const pty = new CancelledPty(); + const job = startPtyJob(pty, "cancel-output", { + shellKind: "posix", + timeoutMs: 1_000, + expectedPrompt: "$ ", + }); + await new Promise((resolve) => setImmediate(resolve)); + job.cancel(); + pty.emit("data", Buffer.from("$ ")); + const result = await job.resultPromise; + assert.equal(result.ok, false); + assert.match(result.error, /cancelled/i); + assert.ok(result.stdout.length <= DEFAULT_FOREGROUND_PTY_CAPTURE_CHARS); + assert.equal(result.outputTruncated, true); +}); + test("background PTY jobs preserve output that has no trailing newline", async () => { const job = startPtyJob(new ShellBackedPty(), "printf 'abc'", { shellKind: "posix", @@ -308,10 +417,12 @@ test("execViaChannel drops the pending marker and resolves cleanly when sshClien test("execViaChannel short-circuits when cancel fires before the SSH channel opens", async () => { const track = new Map(); let execCallback; + let invalidations = 0; const fakeClient = { exec(_command, callback) { execCallback = callback; }, + destroy() { invalidations += 1; }, }; const resultPromise = execViaChannel(fakeClient, "sleep 5", { trackForCancellation: track, @@ -325,6 +436,15 @@ test("execViaChannel short-circuits when cancel fires before the SSH channel ope if (entry.chatSessionId === "chat-2") entry.cancel(); } + const result = await Promise.race([ + resultPromise, + new Promise((_, reject) => setTimeout(() => reject(new Error("pending cancel did not settle")), 25)), + ]); + assert.equal(result.ok, false); + assert.equal(result.error, "Cancelled"); + assert.equal(track.size, 0, "pending marker should be removed as soon as cancel settles"); + assert.equal(invalidations, 1, "pending cancel must invalidate the uncancellable channel-open request"); + // Now the channel "opens" � even though `sshClient.exec` would // hand us a working stream, we must short-circuit because the user // already cancelled. @@ -335,9 +455,201 @@ test("execViaChannel short-circuits when cancel fires before the SSH channel ope on() {}, }; execCallback(null, fakeExecStream); + assert.equal(fakeExecStream.closed, true, "should close the now-unwanted stream"); +}); + +test("execViaChannel times out while SSH never opens the exec channel", async () => { + const track = new Map(); + let execCallback; + let invalidations = 0; + const fakeClient = { + exec(_command, callback) { + execCallback = callback; + }, + destroy() { invalidations += 1; }, + }; + const result = await Promise.race([ + execViaChannel(fakeClient, "echo hi", { + trackForCancellation: track, + chatSessionId: "chat-opening-timeout", + timeoutMs: 5, + }), + new Promise((_, reject) => setTimeout(() => reject(new Error("opening timeout did not settle")), 50)), + ]); + + assert.equal(result.ok, false); + assert.match(result.error, /timed out/); + assert.equal(track.size, 0); + assert.equal(invalidations, 1); + + const lateStream = { + closed: false, + close() { this.closed = true; }, + stderr: { on() {} }, + on() {}, + }; + execCallback(null, lateStream); + assert.equal(lateStream.closed, true, "late exec channels must be closed after opening timeout"); +}); + +test("execViaChannel terminates the command when combined output exceeds its hard limit", async () => { + const track = new Map(); + const execStream = new EventEmitter(); + execStream.stderr = new EventEmitter(); + execStream.closed = false; + execStream.close = () => { execStream.closed = true; }; + execStream.destroy = () => { execStream.closed = true; }; + const fakeClient = { + exec(_command, callback) { + callback(null, execStream); + }, + }; + + const resultPromise = execViaChannel(fakeClient, "yes", { + trackForCancellation: track, + chatSessionId: "chat-output-limit", + timeoutMs: 5_000, + maxOutputBytes: 5, + }); + execStream.emit("data", Buffer.from("abc")); + execStream.stderr.emit("data", Buffer.from("de")); + execStream.emit("data", Buffer.from("f")); + const result = await resultPromise; assert.equal(result.ok, false); + assert.equal(result.stdout, "abc"); + assert.equal(result.stderr, "de"); + assert.equal(result.exitCode, -1); + assert.match(result.error, /output exceeded.*5 byte/i); + assert.equal(execStream.closed, true, "the noisy remote process must be terminated"); + assert.equal(track.size, 0, "the cancelled exec channel must not remain tracked"); + assert.equal(execStream.listenerCount("data"), 0); + assert.equal(execStream.stderr.listenerCount("data"), 0); + assert.equal(execStream.listenerCount("close"), 0); +}); + +test("execViaChannel settles and releases listeners when the SSH channel errors", async () => { + const track = new Map(); + const execStream = new EventEmitter(); + execStream.stderr = new EventEmitter(); + execStream.closed = false; + execStream.close = () => { execStream.closed = true; }; + execStream.destroy = () => { execStream.closed = true; }; + const fakeClient = { + exec(_command, callback) { + callback(null, execStream); + }, + }; + + const resultPromise = execViaChannel(fakeClient, "echo hi", { + trackForCancellation: track, + chatSessionId: "chat-channel-error", + timeoutMs: 5_000, + }); + execStream.emit("data", Buffer.from("partial")); + execStream.emit("error", new Error("channel fault")); + + const result = await resultPromise; + assert.equal(result.ok, false); + assert.equal(result.stdout, "partial"); + assert.equal(result.stderr, ""); + assert.equal(result.exitCode, -1); + assert.match(result.error, /channel fault/i); + assert.equal(execStream.closed, true); + assert.equal(track.size, 0); + assert.equal(execStream.listenerCount("data"), 0); + assert.equal(execStream.stderr.listenerCount("data"), 0); + assert.equal(execStream.stderr.listenerCount("error"), 0); + assert.equal(execStream.listenerCount("close"), 0); + assert.equal(execStream.listenerCount("error"), 0); +}); + +function createExecChannelHarness() { + const execStream = new EventEmitter(); + execStream.stderr = new EventEmitter(); + execStream.closed = false; + execStream.close = () => { execStream.closed = true; }; + execStream.destroy = () => { execStream.closed = true; }; + return { + execStream, + fakeClient: { + exec(_command, callback) { + callback(null, execStream); + }, + }, + }; +} + +test("execViaChannel preserves UTF-8 split across stdout chunks", async () => { + const { execStream, fakeClient } = createExecChannelHarness(); + const resultPromise = execViaChannel(fakeClient, "printf unicode", { timeoutMs: 5_000 }); + const bytes = Buffer.from("中文", "utf8"); + execStream.emit("data", bytes.subarray(0, 2)); + execStream.emit("data", bytes.subarray(2, 4)); + execStream.emit("data", bytes.subarray(4)); + execStream.emit("close", 0); + + assert.deepEqual(await resultPromise, { + ok: true, + stdout: "中文", + stderr: "", + exitCode: 0, + }); +}); + +test("execViaChannel decodes interleaved stdout and stderr independently", async () => { + const { execStream, fakeClient } = createExecChannelHarness(); + const resultPromise = execViaChannel(fakeClient, "printf unicode", { timeoutMs: 5_000 }); + const stdoutBytes = Buffer.from("中", "utf8"); + const stderrBytes = Buffer.from("文", "utf8"); + execStream.emit("data", stdoutBytes.subarray(0, 2)); + execStream.stderr.emit("data", stderrBytes.subarray(0, 1)); + execStream.emit("data", stdoutBytes.subarray(2)); + execStream.stderr.emit("data", stderrBytes.subarray(1)); + execStream.emit("close", 0); + + assert.deepEqual(await resultPromise, { + ok: true, + stdout: "中", + stderr: "文", + exitCode: 0, + }); +}); + +test("execViaChannel omits an incomplete UTF-8 character at the output limit", async () => { + const { execStream, fakeClient } = createExecChannelHarness(); + const resultPromise = execViaChannel(fakeClient, "printf unicode", { + timeoutMs: 5_000, + maxOutputBytes: 2, + }); + execStream.emit("data", Buffer.from("中", "utf8")); + + const result = await resultPromise; + assert.equal(result.ok, false); + assert.equal(result.stdout, ""); + assert.equal(result.stderr, ""); + assert.doesNotMatch(`${result.stdout}${result.stderr}`, /�/u); + assert.match(result.error, /output exceeded.*2 byte/i); + assert.equal(execStream.closed, true); +}); + +test("execViaChannel cancellation never exposes an incomplete UTF-8 character", async () => { + const track = new Map(); + const { execStream, fakeClient } = createExecChannelHarness(); + const resultPromise = execViaChannel(fakeClient, "printf unicode", { + timeoutMs: 5_000, + trackForCancellation: track, + chatSessionId: "chat-unicode-cancel", + }); + execStream.emit("data", Buffer.from("中", "utf8").subarray(0, 2)); + const entry = [...track.values()].find((candidate) => ( + candidate.chatSessionId === "chat-unicode-cancel" + )); + entry.cancel(); + + const result = await resultPromise; + assert.equal(result.ok, false); + assert.equal(result.stdout, ""); + assert.doesNotMatch(result.stdout, /�/u); assert.equal(result.error, "Cancelled"); - assert.equal(fakeExecStream.closed, true, "should close the now-unwanted stream"); - assert.equal(track.size, 0, "pending marker should be removed after callback runs"); }); diff --git a/electron/bridges/ai/ptyExecHelpers.cjs b/electron/bridges/ai/ptyExecHelpers.cjs index 3e66654c4..88891dcbe 100644 --- a/electron/bridges/ai/ptyExecHelpers.cjs +++ b/electron/bridges/ai/ptyExecHelpers.cjs @@ -278,8 +278,16 @@ function normalizePtyOutput(stdout, { } function appendBoundedOutput(current, chunk, maxBufferedChars) { - const combined = `${current || ""}${chunk || ""}`; const limit = Number.isFinite(maxBufferedChars) ? Math.max(0, Math.floor(maxBufferedChars)) : 0; + const currentText = String(current || ""); + const chunkText = String(chunk || ""); + if (limit > 0 && chunkText.length >= limit) { + return { + text: chunkText.slice(-limit), + dropped: currentText.length + chunkText.length - limit, + }; + } + const combined = `${currentText}${chunkText}`; if (limit <= 0 || combined.length <= limit) { return { text: combined, dropped: 0 }; } diff --git a/electron/bridges/ai/sessionShellKind.cjs b/electron/bridges/ai/sessionShellKind.cjs index f02d9d478..1ab1ddcda 100644 --- a/electron/bridges/ai/sessionShellKind.cjs +++ b/electron/bridges/ai/sessionShellKind.cjs @@ -16,6 +16,8 @@ */ "use strict"; +const { executeBoundedSshCommand } = require("../boundedSshExec.cjs"); + const crypto = require("node:crypto"); const { classifyLocalShellType } = require("../../../lib/localShell.cjs"); @@ -90,60 +92,17 @@ function parseRemoteLoginShellProbeOutput(stdout) { */ function createSshConnExecProbe(conn) { if (!conn || typeof conn.exec !== "function") return null; - return function execProbe(command, timeoutMs = DEFAULT_PROBE_TIMEOUT_MS) { - return new Promise((resolve) => { - let settled = false; - let activeStream = null; - const settle = (value) => { - if (settled) return; - settled = true; - clearTimeout(timer); - resolve(value); - }; - const timer = setTimeout(() => { - try { - activeStream?.close?.(); - } catch { - // ignore - } - settle(null); - }, timeoutMs); - - try { - conn.exec(command, (err, stream) => { - if (err || !stream) { - settle(null); - return; - } - if (settled) { - try { - stream.close?.(); - } catch { - // ignore - } - return; - } - activeStream = stream; - let stdout = ""; - stream.on("data", (chunk) => { - stdout += chunk.toString("utf8"); - }); - if (stream.stderr && typeof stream.stderr.on === "function") { - stream.stderr.on("data", () => { - // swallow — probe only needs stdout - }); - } - stream.on("close", () => { - settle(stdout); - }); - stream.on("error", () => { - settle(null); - }); - }); - } catch { - settle(null); - } - }); + return async function execProbe(command, timeoutMs = DEFAULT_PROBE_TIMEOUT_MS) { + try { + const result = await executeBoundedSshCommand(conn, command, { + openingTimeoutMs: timeoutMs, + runTimeoutMs: timeoutMs, + maxOutputBytes: 64 * 1024, + }); + return result.stdout; + } catch { + return null; + } }; } diff --git a/electron/bridges/aiBridge.cjs b/electron/bridges/aiBridge.cjs index 60335012c..d68c198ab 100644 --- a/electron/bridges/aiBridge.cjs +++ b/electron/bridges/aiBridge.cjs @@ -58,6 +58,10 @@ const CLAUDE_AUTH_HELP_MESSAGE = const { codexLoginSessions, appendCodexLoginOutput, + createCodexLoginOutputDecoder, + recordCodexLoginSession, + clearCodexLoginKillTimer, + stopCodexLoginProcess, toCodexLoginSessionResponse, getActiveCodexLoginSession, normalizeCodexIntegrationState, @@ -320,7 +324,11 @@ function getChildProcessTreePids(rootPid) { const pid = queue.shift(); if (!Number.isInteger(pid) || pid <= 0) continue; try { - const output = execFileSync("pgrep", ["-P", String(pid)], { encoding: "utf8" }).trim(); + const output = execFileSync("pgrep", ["-P", String(pid)], { + encoding: "utf8", + maxBuffer: 1024 * 1024, + timeout: 1_000, + }).trim(); if (!output) continue; for (const line of output.split(/\s+/)) { const childPid = Number(line); @@ -340,7 +348,11 @@ function killTrackedProcessTree(rootPid, childPids) { if (process.platform === "win32") { if (Number.isInteger(rootPid) && rootPid > 0) { try { - execFileSync("taskkill", ["/PID", String(rootPid), "/T", "/F"], { stdio: "ignore" }); + execFileSync("taskkill", ["/PID", String(rootPid), "/T", "/F"], { + stdio: "ignore", + timeout: 5_000, + windowsHide: true, + }); } catch { // Ignore kill failures; the process may have already exited. } @@ -520,9 +532,12 @@ function createAbortError() { } function raceAgainstAbort(promise, signal) { - if (signal.aborted) return Promise.reject(createAbortError()); + const abortReason = () => ( + signal.reason instanceof Error ? signal.reason : createAbortError() + ); + if (signal.aborted) return Promise.reject(abortReason()); return new Promise((resolve, reject) => { - const onAbort = () => reject(createAbortError()); + const onAbort = () => reject(abortReason()); signal.addEventListener("abort", onAbort, { once: true }); promise.then( (value) => { @@ -538,12 +553,25 @@ function raceAgainstAbort(promise, signal) { } async function streamRequest(url, options, event, requestId, skipTLS) { + const parsedUrl = new URL(url); // Register cancellation before any await so Stop during PAC/proxy lookup works. const controller = new AbortController(); + const totalTimeoutMs = Math.max(1, Number(options.totalTimeoutMs) || 120_000); + const maxErrorBodyBytes = Math.max(1, Number(options.maxErrorBodyBytes) || 64 * 1024); + let lifecycleFinished = false; + const finishLifecycle = () => { + if (lifecycleFinished) return; + lifecycleFinished = true; + clearTimeout(totalTimer); + activeStreams.delete(requestId); + }; + const totalTimer = setTimeout(() => { + controller.abort(new Error(`AI stream total deadline exceeded after ${totalTimeoutMs} ms`)); + }, totalTimeoutMs); activeStreams.set(requestId, controller); if (controller.signal.aborted) { - activeStreams.delete(requestId); - throw createAbortError(); + finishLifecycle(); + throw controller.signal.reason instanceof Error ? controller.signal.reason : createAbortError(); } const { resolveOutboundHttpAgent } = require("./httpNetworkProxyAgent.cjs"); @@ -558,28 +586,67 @@ async function streamRequest(url, options, event, requestId, skipTLS) { ); } catch (err) { if (err?.name === "AbortError" || controller.signal.aborted) { - activeStreams.delete(requestId); - throw createAbortError(); + finishLifecycle(); + throw controller.signal.reason instanceof Error ? controller.signal.reason : createAbortError(); } proxyAgent = undefined; } if (controller.signal.aborted) { - activeStreams.delete(requestId); - throw createAbortError(); + finishLifecycle(); + throw controller.signal.reason instanceof Error ? controller.signal.reason : createAbortError(); } return new Promise((resolve, reject) => { - const parsedUrl = new URL(url); const isHttps = parsedUrl.protocol === "https:"; const lib = isHttps ? https : http; + let requestSettled = false; + let streamFinished = false; + let req = null; + + const settleResolve = (value) => { + if (requestSettled) return; + requestSettled = true; + resolve(value); + }; + const settleReject = (error) => { + if (requestSettled) return; + requestSettled = true; + reject(error); + }; + const sendStreamError = (error) => { + safeSend(event.sender, "netcatty:ai:stream:error", { + requestId, + error: error?.message || String(error), + }); + }; + const failStream = (error, { destroy = true } = {}) => { + if (streamFinished) return; + streamFinished = true; + finishLifecycle(); + controller.signal.removeEventListener("abort", onAbort); + sendStreamError(error); + settleReject(error); + if (destroy) { + try { req?.destroy?.(); } catch { /* ignore */ } + } + }; + const onAbort = () => { + const error = controller.signal.reason instanceof Error + ? controller.signal.reason + : createAbortError(); + failStream(error); + }; // Re-check after entering the Promise in case cancel raced the await above. if (controller.signal.aborted) { - activeStreams.delete(requestId); - reject(createAbortError()); + finishLifecycle(); + settleReject(controller.signal.reason instanceof Error + ? controller.signal.reason + : createAbortError()); return; } + controller.signal.addEventListener("abort", onAbort, { once: true }); const reqOpts = { method: options.method || "POST", @@ -589,8 +656,13 @@ async function streamRequest(url, options, event, requestId, skipTLS) { if (skipTLS && isHttps) reqOpts.rejectUnauthorized = false; if (proxyAgent) reqOpts.agent = proxyAgent; - const req = lib.request(parsedUrl, reqOpts, - (res) => { + try { + req = lib.request(parsedUrl, reqOpts, + (res) => { + if (streamFinished) { + res.destroy?.(); + return; + } // Decode the response as one continuous UTF-8 stream. Calling // Buffer#toString() on each network chunk corrupts multi-byte // characters when a chunk boundary falls in the middle of one. @@ -601,8 +673,21 @@ async function streamRequest(url, options, event, requestId, skipTLS) { if (statusCode < 200 || statusCode >= 300) { // Read the error body before resolving so we can include it in the response let errorBody = ""; - res.on("data", (chunk) => { errorBody += chunk.toString(); }); + let errorBodyBytes = 0; + res.on("data", (chunk) => { + if (streamFinished) return; + const text = chunk.toString(); + errorBodyBytes += Buffer.byteLength(text); + if (errorBodyBytes > maxErrorBodyBytes) { + failStream(new Error( + `AI error response exceeded maximum size (${maxErrorBodyBytes} bytes)`, + )); + return; + } + errorBody += text; + }); res.on("end", () => { + if (streamFinished) return; // Try to extract error message from JSON response (OpenAI-compatible format) let errorDetail = statusText; try { @@ -615,34 +700,36 @@ async function streamRequest(url, options, event, requestId, skipTLS) { requestId, error: `HTTP ${statusCode}: ${errorDetail}`, }); - activeStreams.delete(requestId); - resolve({ statusCode, statusText: `${statusCode} ${errorDetail}` }); + streamFinished = true; + finishLifecycle(); + controller.signal.removeEventListener("abort", onAbort); + settleResolve({ statusCode, statusText: `${statusCode} ${errorDetail}` }); }); + res.on("error", (error) => failStream(error, { destroy: false })); return; } // Resolve with success status — data will flow via stream events - resolve({ statusCode, statusText }); + settleResolve({ statusCode, statusText }); let buffer = ""; const MAX_BUFFER_SIZE = 10 * 1024 * 1024; // 10MB safety limit res.on("data", (chunk) => { + const previousBufferLength = buffer.length; buffer += chunk.toString(); // Guard against unbounded buffer growth if (buffer.length > MAX_BUFFER_SIZE) { - safeSend(event.sender, "netcatty:ai:stream:error", { - requestId, - error: "Stream buffer exceeded maximum size (10MB)", - }); - req.destroy(); - activeStreams.delete(requestId); + failStream(new Error("Stream buffer exceeded maximum size (10MB)")); return; } - const lines = buffer.split("\n"); - buffer = lines.pop() || ""; - - for (const line of lines) { + let consumedUntil = 0; + let searchFrom = previousBufferLength; + let newlineIndex; + while ((newlineIndex = buffer.indexOf("\n", searchFrom)) >= 0) { + const line = buffer.slice(consumedUntil, newlineIndex); + consumedUntil = newlineIndex + 1; + searchFrom = consumedUntil; const trimmed = line.trim(); if (!trimmed) continue; @@ -654,9 +741,11 @@ async function streamRequest(url, options, event, requestId, skipTLS) { }); } } + if (consumedUntil > 0) buffer = buffer.slice(consumedUntil); }); res.on("end", () => { + if (streamFinished) return; // Flush any remaining buffer if (buffer.trim().startsWith("data: ")) { safeSend(event.sender, "netcatty:ai:stream:data", { @@ -665,46 +754,33 @@ async function streamRequest(url, options, event, requestId, skipTLS) { }); } safeSend(event.sender, "netcatty:ai:stream:end", { requestId }); - activeStreams.delete(requestId); + streamFinished = true; + finishLifecycle(); + controller.signal.removeEventListener("abort", onAbort); }); - res.on("error", (err) => { - safeSend(event.sender, "netcatty:ai:stream:error", { - requestId, - error: err.message, - }); - activeStreams.delete(requestId); - }); - } - ); + res.on("error", (err) => failStream(err, { destroy: false })); + } + ); + } catch (error) { + failStream(error, { destroy: false }); + return; + } - req.on("error", (err) => { - safeSend(event.sender, "netcatty:ai:stream:error", { - requestId, - error: err.message, - }); - activeStreams.delete(requestId); - reject(err); - }); + req.on("error", (err) => failStream(err, { destroy: false })); req.on("timeout", () => { - req.destroy(); - safeSend(event.sender, "netcatty:ai:stream:error", { - requestId, - error: "Request timeout", - }); - activeStreams.delete(requestId); + failStream(new Error("Request timeout")); }); - // Wire up abort signal to destroy the request - controller.signal.addEventListener("abort", () => { - req.destroy(); - }, { once: true }); - - if (options.body) { - req.write(options.body); + try { + if (options.body) { + req.write(options.body); + } + req.end(); + } catch (error) { + failStream(error); } - req.end(); }); } @@ -756,6 +832,10 @@ function createHandlerContext(ipcMain) { CLAUDE_AUTH_HELP_MESSAGE, codexLoginSessions, appendCodexLoginOutput, + createCodexLoginOutputDecoder, + recordCodexLoginSession, + clearCodexLoginKillTimer, + stopCodexLoginProcess, toCodexLoginSessionResponse, getActiveCodexLoginSession, normalizeCodexIntegrationState, @@ -816,6 +896,7 @@ function createHandlerContext(ipcMain) { normalizeAgentEnv, safeReadJson, streamRequest, + loadCodexSdk: () => import("@openai/codex-sdk"), }; } @@ -872,9 +953,7 @@ function cleanup() { for (const [id, session] of codexLoginSessions) { try { - if (session.process && !session.process.killed) { - session.process.kill("SIGTERM"); - } + stopCodexLoginProcess(session); } catch {} } codexLoginSessions.clear(); @@ -901,5 +980,7 @@ module.exports = { reportOpenedSessionActivity, buildExternalAgentSystemContext, buildExternalAgentContextualPrompt, + _streamRequestForTests: streamRequest, + _getActiveStreamCountForTests: () => activeStreams.size, getExternalMcpController: () => externalMcpController, }; diff --git a/electron/bridges/aiBridge.test.cjs b/electron/bridges/aiBridge.test.cjs index 34844bc93..ecd78afed 100644 --- a/electron/bridges/aiBridge.test.cjs +++ b/electron/bridges/aiBridge.test.cjs @@ -4,6 +4,7 @@ const fs = require("node:fs"); const Module = require("node:module"); const os = require("node:os"); const path = require("node:path"); +const http = require("node:http"); const { prepareCommandForSpawn } = require("./ai/shellUtils.cjs"); const { isCodexAuthError: realIsCodexAuthError, @@ -203,6 +204,104 @@ function loadBridgeWithMocks(options = {}) { } } +test("non-2xx streaming responses are bounded and actively terminated", async () => { + let requestClosed = false; + let resolveRequestClosed; + const requestClosedPromise = new Promise((resolve) => { resolveRequestClosed = resolve; }); + const server = http.createServer((_request, response) => { + response.writeHead(500, { "content-type": "text/plain" }); + const interval = setInterval(() => response.write("abcdefgh"), 2); + response.on("close", () => { + clearInterval(interval); + requestClosed = true; + resolveRequestClosed(); + }); + }); + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); + const address = server.address(); + const { bridge, restore } = loadBridgeWithMocks(); + bridge.init({ + sessions: new Map(), + sftpClients: new Map(), + electronModule: { app: { getPath: () => process.cwd() }, session: {} }, + }); + + try { + await assert.rejects( + () => bridge._streamRequestForTests( + `http://127.0.0.1:${address.port}/streaming-error`, + { + method: "GET", + maxErrorBodyBytes: 32, + totalTimeoutMs: 1_000, + }, + { sender: { id: 1 } }, + "streaming-error", + false, + ), + /error response exceeded/i, + ); + await Promise.race([ + requestClosedPromise, + new Promise((_, reject) => setTimeout(() => reject(new Error("server request stayed open")), 500)), + ]); + assert.equal(requestClosed, true); + assert.equal(bridge._getActiveStreamCountForTests(), 0); + } finally { + restore(); + await new Promise((resolve) => server.close(resolve)); + } +}); + +test("streaming requests enforce a total deadline even while bytes keep arriving", async () => { + let requestClosed = false; + let resolveRequestClosed; + const requestClosedPromise = new Promise((resolve) => { resolveRequestClosed = resolve; }); + const server = http.createServer((_request, response) => { + response.writeHead(500, { "content-type": "text/plain" }); + const interval = setInterval(() => response.write("x"), 2); + response.on("close", () => { + clearInterval(interval); + requestClosed = true; + resolveRequestClosed(); + }); + }); + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); + const address = server.address(); + const { bridge, restore } = loadBridgeWithMocks(); + bridge.init({ + sessions: new Map(), + sftpClients: new Map(), + electronModule: { app: { getPath: () => process.cwd() }, session: {} }, + }); + + try { + await assert.rejects( + () => bridge._streamRequestForTests( + `http://127.0.0.1:${address.port}/never-ending`, + { + method: "GET", + maxErrorBodyBytes: 1024 * 1024, + totalTimeoutMs: 30, + }, + { sender: { id: 1 } }, + "never-ending", + false, + ), + /total deadline exceeded/i, + ); + await Promise.race([ + requestClosedPromise, + new Promise((_, reject) => setTimeout(() => reject(new Error("server request stayed open")), 500)), + ]); + assert.equal(requestClosed, true); + assert.equal(bridge._getActiveStreamCountForTests(), 0); + } finally { + restore(); + await new Promise((resolve) => server.close(resolve)); + } +}); + test("mcp attachment update handler forwards current chat attachments", async () => { const calls = []; const { bridge, restore } = loadBridgeWithMocks({ diff --git a/electron/bridges/aiBridge/agentCliHelpers.cjs b/electron/bridges/aiBridge/agentCliHelpers.cjs index 9c26ea62b..b68bd52f0 100644 --- a/electron/bridges/aiBridge/agentCliHelpers.cjs +++ b/electron/bridges/aiBridge/agentCliHelpers.cjs @@ -1,6 +1,13 @@ /* eslint-disable no-undef */ +const { StringDecoder } = require("node:string_decoder"); + +const DEFAULT_CODEX_CLI_TIMEOUT_MS = 10_000; +const CODEX_AUTH_VALIDATION_TIMEOUT_MS = 10_000; +const MAX_AGENT_CLI_BUFFER_CHARS = 10 * 1024 * 1024; + function createAgentCliHelpers(ctx) { with (ctx) { + const codexAuthValidationInFlight = new Map(); async function runCommand(command, args, options) { return await new Promise((resolve, reject) => { let settled = false; @@ -28,19 +35,30 @@ function createAgentCliHelpers(ctx) { let stdout = ""; let stderr = ""; - const MAX_BUFFER = 10 * 1024 * 1024; // 10MB + let stdoutBytes = 0; + let stderrBytes = 0; + let stdoutTruncated = false; + let stderrTruncated = false; + const stdoutDecoder = new StringDecoder("utf8"); + const stderrDecoder = new StringDecoder("utf8"); const timeoutMs = Number.isFinite(options?.timeoutMs) ? Number(options.timeoutMs) : 0; child.stdout.on("data", (chunk) => { - if (stdout.length < MAX_BUFFER) { - stdout += chunk.toString("utf8"); - } + const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk)); + const remaining = Math.max(0, MAX_AGENT_CLI_BUFFER_CHARS - stdoutBytes); + const accepted = buffer.length <= remaining ? buffer : buffer.subarray(0, remaining); + if (accepted.length > 0) stdout += stdoutDecoder.write(accepted); + stdoutBytes += accepted.length; + if (accepted.length < buffer.length) stdoutTruncated = true; }); child.stderr.on("data", (chunk) => { - if (stderr.length < MAX_BUFFER) { - stderr += chunk.toString("utf8"); - } + const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk)); + const remaining = Math.max(0, MAX_AGENT_CLI_BUFFER_CHARS - stderrBytes); + const accepted = buffer.length <= remaining ? buffer : buffer.subarray(0, remaining); + if (accepted.length > 0) stderr += stderrDecoder.write(accepted); + stderrBytes += accepted.length; + if (accepted.length < buffer.length) stderrTruncated = true; }); child.once("error", (error) => { @@ -56,6 +74,8 @@ function createAgentCliHelpers(ctx) { clearTimers(); if (settled) return; settled = true; + if (!stdoutTruncated || stdoutDecoder.lastNeed === 0) stdout += stdoutDecoder.end(); + if (!stderrTruncated || stderrDecoder.lastNeed === 0) stderr += stderrDecoder.end(); resolve({ stdout: stripAnsi(stdout), stderr: stripAnsi(stderr), @@ -126,6 +146,9 @@ function createAgentCliHelpers(ctx) { return await runCommand(codexCliPath, args, { cwd: options?.cwd?.trim() || undefined, env: shellEnv, + timeoutMs: Number.isFinite(options?.timeoutMs) + ? Number(options.timeoutMs) + : DEFAULT_CODEX_CLI_TIMEOUT_MS, }); } @@ -160,44 +183,88 @@ function createAgentCliHelpers(ctx) { } const cached = getCodexValidationCache(); if (cached && now - cached.checkedAt < maxAgeMs && (cached.codexPath || null) === requestedCodexPath) return cached; + const inFlightKey = requestedCodexPath || "__auto__"; + const existingValidation = codexAuthValidationInFlight.get(inFlightKey); + if (existingValidation) return existingValidation; - const shellEnv = await getShellEnv(); - const rawCodexPath = requestedCodexPath || await resolveSdkBinPathAsync("codex", shellEnv); - const codexPath = rawCodexPath && typeof resolveCodexExecutableForSdk === "function" - ? resolveCodexExecutableForSdk(rawCodexPath) || null - : rawCodexPath; - if (!codexPath) { - const result = { ok: false, checkedAt: now, codexPath: requestedCodexPath, error: "codex binary not found", code: "ENOENT" }; - setCodexValidationCache(result); - return result; - } - try { - // Minimal read-only probe turn through the SDK to confirm auth works. - const { Codex } = await import("@openai/codex-sdk"); - const codexOptions = { env: addCodexExecutableEnvForSdk(shellEnv, codexPath) }; - if (codexPath) codexOptions.codexPathOverride = codexPath; - const codex = new Codex(codexOptions); - const thread = codex.startThread({ skipGitRepoCheck: true }); - const { events } = await thread.runStreamed("ping", { sandbox: "read-only" }); - let failed = null; - for await (const event of events) { - if (event?.type === "turn.failed") { failed = event.error; break; } - if (event?.type === "turn.completed") break; - if (event?.type === "item.completed") break; // got a response, auth fine - } - if (failed) { - const result = { ok: false, checkedAt: now, codexPath, error: failed.message || "Codex auth failed", code: undefined }; + const validationPromise = (async () => { + const shellEnv = await getShellEnv(); + const rawCodexPath = requestedCodexPath || await resolveSdkBinPathAsync("codex", shellEnv); + const codexPath = rawCodexPath && typeof resolveCodexExecutableForSdk === "function" + ? resolveCodexExecutableForSdk(rawCodexPath) || null + : rawCodexPath; + if (!codexPath) { + const result = { ok: false, checkedAt: now, codexPath: requestedCodexPath, error: "codex binary not found", code: "ENOENT" }; setCodexValidationCache(result); return result; } - const result = { ok: true, checkedAt: now, codexPath, error: null }; - setCodexValidationCache(result); - return result; - } catch (error) { - const normalized = extractCodexError(error); - const result = { ok: false, checkedAt: now, codexPath, error: normalized.message, code: normalized.code }; - setCodexValidationCache(result); - return result; + + const abortController = new AbortController(); + let timeoutId = null; + let iterator = null; + try { + const timeoutPromise = new Promise((_, reject) => { + timeoutId = setTimeout(() => { + const error = new Error( + `Codex ChatGPT auth validation timed out after ${CODEX_AUTH_VALIDATION_TIMEOUT_MS}ms`, + ); + error.code = "ETIMEDOUT"; + try { abortController.abort(error); } catch {} + reject(error); + }, CODEX_AUTH_VALIDATION_TIMEOUT_MS); + if (typeof timeoutId?.unref === "function") timeoutId.unref(); + }); + + const probePromise = (async () => { + // Minimal read-only probe turn through the SDK to confirm auth works. + const { Codex } = await (typeof loadCodexSdk === "function" + ? loadCodexSdk() + : import("@openai/codex-sdk")); + const codexOptions = { env: addCodexExecutableEnvForSdk(shellEnv, codexPath) }; + if (codexPath) codexOptions.codexPathOverride = codexPath; + const codex = new Codex(codexOptions); + const thread = codex.startThread({ skipGitRepoCheck: true }); + const { events } = await thread.runStreamed("ping", { + sandbox: "read-only", + signal: abortController.signal, + }); + iterator = events?.[Symbol.asyncIterator]?.(); + if (!iterator) throw new Error("Codex auth validation returned no event stream"); + let failed = null; + while (true) { + const next = await iterator.next(); + if (next.done) break; + const event = next.value; + if (event?.type === "turn.failed") { failed = event.error; break; } + if (event?.type === "turn.completed") break; + if (event?.type === "item.completed") break; + } + if (failed) throw failed; + })(); + + await Promise.race([probePromise, timeoutPromise]); + const result = { ok: true, checkedAt: now, codexPath, error: null }; + setCodexValidationCache(result); + return result; + } catch (error) { + const normalized = extractCodexError(error); + const result = { ok: false, checkedAt: now, codexPath, error: normalized.message, code: normalized.code }; + setCodexValidationCache(result); + return result; + } finally { + if (timeoutId) clearTimeout(timeoutId); + try { abortController.abort(); } catch {} + try { void Promise.resolve(iterator?.return?.()).catch(() => {}); } catch {} + } + })(); + + codexAuthValidationInFlight.set(inFlightKey, validationPromise); + try { + return await validationPromise; + } finally { + if (codexAuthValidationInFlight.get(inFlightKey) === validationPromise) { + codexAuthValidationInFlight.delete(inFlightKey); + } } } @@ -341,4 +408,9 @@ function createAgentCliHelpers(ctx) { } } -module.exports = { createAgentCliHelpers }; +module.exports = { + createAgentCliHelpers, + CODEX_AUTH_VALIDATION_TIMEOUT_MS, + DEFAULT_CODEX_CLI_TIMEOUT_MS, + MAX_AGENT_CLI_BUFFER_CHARS, +}; diff --git a/electron/bridges/aiBridge/agentCliHelpers.test.cjs b/electron/bridges/aiBridge/agentCliHelpers.test.cjs new file mode 100644 index 000000000..6ce8602dc --- /dev/null +++ b/electron/bridges/aiBridge/agentCliHelpers.test.cjs @@ -0,0 +1,237 @@ +"use strict"; + +const test = require("node:test"); +const assert = require("node:assert/strict"); +const { EventEmitter } = require("node:events"); +const { PassThrough } = require("node:stream"); + +const { + createAgentCliHelpers, + CODEX_AUTH_VALIDATION_TIMEOUT_MS, + DEFAULT_CODEX_CLI_TIMEOUT_MS, + MAX_AGENT_CLI_BUFFER_CHARS, +} = require("./agentCliHelpers.cjs"); + +function createHungChild() { + const child = new EventEmitter(); + child.stdout = new PassThrough(); + child.stderr = new PassThrough(); + child.kills = []; + child.kill = (signal) => { + child.kills.push(signal); + return true; + }; + return child; +} + +test("runCodexCli applies a default timeout to short status commands", async () => { + const child = createHungChild(); + const scheduled = []; + const helpers = createAgentCliHelpers({ + prepareCommandForSpawn: (command, args) => ({ command, args, shell: false }), + spawn: () => child, + stripAnsi: (value) => value, + getShellEnv: async () => ({}), + normalizeCliPathForPlatform: (value) => value, + resolveCliFromPathAsync: async () => "/fake/codex", + setTimeout: (callback, delay) => { + scheduled.push(delay); + queueMicrotask(callback); + return { unref() {} }; + }, + clearTimeout() {}, + }); + const safetyTimer = globalThis.setTimeout(() => child.emit("close", 0), 25); + + try { + await assert.rejects( + helpers.runCodexCli(["login", "status"], {}), + (error) => error?.code === "ETIMEDOUT", + ); + } finally { + globalThis.clearTimeout(safetyTimer); + } + assert.equal(scheduled[0], DEFAULT_CODEX_CLI_TIMEOUT_MS); + assert.deepEqual(child.kills, ["SIGTERM", "SIGKILL"]); +}); + +test("runCodexCli slices a single oversized output chunk to the hard limit", async () => { + const child = createHungChild(); + const helpers = createAgentCliHelpers({ + prepareCommandForSpawn: (command, args) => ({ command, args, shell: false }), + spawn: () => { + queueMicrotask(() => { + child.stdout.emit("data", Buffer.from("x".repeat(MAX_AGENT_CLI_BUFFER_CHARS + 257))); + child.emit("close", 0); + }); + return child; + }, + stripAnsi: (value) => value, + getShellEnv: async () => ({}), + normalizeCliPathForPlatform: (value) => value, + resolveCliFromPathAsync: async () => "/fake/codex", + }); + + const result = await helpers.runCodexCli(["--version"], {}); + assert.equal(result.stdout.length, MAX_AGENT_CLI_BUFFER_CHARS); +}); + +test("runCodexCli preserves split UTF-8 independently on stdout and stderr", async () => { + const child = createHungChild(); + const helpers = createAgentCliHelpers({ + prepareCommandForSpawn: (command, args) => ({ command, args, shell: false }), + spawn: () => { + queueMicrotask(() => { + const stdout = Buffer.from("中文", "utf8"); + const stderr = Buffer.from("错误", "utf8"); + child.stdout.emit("data", stdout.subarray(0, 2)); + child.stderr.emit("data", stderr.subarray(0, 1)); + child.stdout.emit("data", stdout.subarray(2)); + child.stderr.emit("data", stderr.subarray(1)); + child.emit("close", 0); + }); + return child; + }, + stripAnsi: (value) => value, + getShellEnv: async () => ({}), + normalizeCliPathForPlatform: (value) => value, + resolveCliFromPathAsync: async () => "/fake/codex", + }); + + const result = await helpers.runCodexCli(["--version"], {}); + assert.deepEqual(result, { stdout: "中文", stderr: "错误", exitCode: 0 }); +}); + +test("runCodexCli omits an incomplete UTF-8 suffix at its byte limit", async () => { + const child = createHungChild(); + const helpers = createAgentCliHelpers({ + prepareCommandForSpawn: (command, args) => ({ command, args, shell: false }), + spawn: () => { + queueMicrotask(() => { + child.stdout.emit("data", Buffer.from("x".repeat(MAX_AGENT_CLI_BUFFER_CHARS - 1))); + child.stdout.emit("data", Buffer.from("中", "utf8")); + child.emit("close", 0); + }); + return child; + }, + stripAnsi: (value) => value, + getShellEnv: async () => ({}), + normalizeCliPathForPlatform: (value) => value, + resolveCliFromPathAsync: async () => "/fake/codex", + }); + + const result = await helpers.runCodexCli(["--version"], {}); + assert.equal(result.stdout.length, MAX_AGENT_CLI_BUFFER_CHARS - 1); + assert.doesNotMatch(result.stdout, /�/u); +}); + +function createValidationHelpers({ loadCodexSdk, setTimeout, clearTimeout }) { + return createAgentCliHelpers({ + getCodexValidationCache: () => null, + setCodexValidationCache() {}, + normalizeCliPathForPlatform: (value) => value, + getShellEnv: async () => ({}), + resolveSdkBinPathAsync: async () => "/fake/codex", + resolveCodexExecutableForSdk: (value) => value, + addCodexExecutableEnvForSdk: (env) => env, + extractCodexError: (error) => ({ message: error?.message || String(error) }), + loadCodexSdk, + ...(setTimeout ? { setTimeout } : {}), + ...(clearTimeout ? { clearTimeout } : {}), + }); +} + +test("ChatGPT auth validation coalesces concurrent probes and cleans the stream", async () => { + let runCount = 0; + let returnCount = 0; + let release; + const gate = new Promise((resolve) => { release = resolve; }); + const helpers = createValidationHelpers({ + loadCodexSdk: async () => ({ + Codex: class { + startThread() { + return { + async runStreamed(_prompt, options) { + runCount += 1; + assert.equal(options.signal.aborted, false); + const iterator = { + async next() { + await gate; + return { done: false, value: { type: "item.completed" } }; + }, + async return() { + returnCount += 1; + return { done: true }; + }, + }; + return { events: { [Symbol.asyncIterator]: () => iterator } }; + }, + }; + } + }, + }), + }); + + const first = helpers.validateCodexChatGptAuth({ codexPath: "/fake/codex" }); + const second = helpers.validateCodexChatGptAuth({ codexPath: "/fake/codex" }); + await new Promise((resolve) => setImmediate(resolve)); + assert.equal(runCount, 1); + release(); + + assert.deepEqual(await Promise.all([first, second]), [ + { ok: true, checkedAt: (await first).checkedAt, codexPath: "/fake/codex", error: null }, + { ok: true, checkedAt: (await second).checkedAt, codexPath: "/fake/codex", error: null }, + ]); + assert.equal(returnCount, 1); +}); + +test("ChatGPT auth validation aborts and settles when the SDK stream hangs", async () => { + let observedSignal; + let returnCount = 0; + const scheduled = []; + let releaseSafety; + const safetyGate = new Promise((resolve) => { releaseSafety = resolve; }); + const helpers = createValidationHelpers({ + loadCodexSdk: async () => ({ + Codex: class { + startThread() { + return { + async runStreamed(_prompt, options) { + observedSignal = options.signal; + const iterator = { + async next() { + await safetyGate; + return { done: false, value: { type: "item.completed" } }; + }, + async return() { + returnCount += 1; + return { done: true }; + }, + }; + return { events: { [Symbol.asyncIterator]: () => iterator } }; + }, + }; + } + }, + }), + setTimeout: (callback, delay) => { + scheduled.push(delay); + queueMicrotask(callback); + return { unref() {} }; + }, + clearTimeout() {}, + }); + const safetyTimer = globalThis.setTimeout(releaseSafety, 25); + + try { + const result = await helpers.validateCodexChatGptAuth({ codexPath: "/fake/codex" }); + assert.equal(result.ok, false); + assert.match(result.error, /timed out/i); + } finally { + globalThis.clearTimeout(safetyTimer); + releaseSafety(); + } + assert.equal(scheduled[0], CODEX_AUTH_VALIDATION_TIMEOUT_MS); + assert.equal(observedSignal.aborted, true); + assert.equal(returnCount, 1); +}); diff --git a/electron/bridges/aiBridge/agentDiscoveryHandlers.cjs b/electron/bridges/aiBridge/agentDiscoveryHandlers.cjs index 6f5748b30..86e4b71e3 100644 --- a/electron/bridges/aiBridge/agentDiscoveryHandlers.cjs +++ b/electron/bridges/aiBridge/agentDiscoveryHandlers.cjs @@ -351,24 +351,36 @@ function registerAgentDiscoveryHandlers(ctx) { codexPath: codexCliPath, }; - const handleChunk = (chunk) => { - appendCodexLoginOutput(session, chunk.toString("utf8")); + const stdoutDecoder = createCodexLoginOutputDecoder(session); + const stderrDecoder = createCodexLoginOutputDecoder(session); + let outputEnded = false; + const endOutput = () => { + if (outputEnded) return; + outputEnded = true; + stdoutDecoder.end(); + stderrDecoder.end(); }; - child.stdout.on("data", handleChunk); - child.stderr.on("data", handleChunk); + child.stdout.on("data", (chunk) => stdoutDecoder.write(chunk)); + child.stderr.on("data", (chunk) => stderrDecoder.write(chunk)); child.once("error", (error) => { + endOutput(); + clearCodexLoginKillTimer(session); session.state = "error"; session.error = `[codex] Failed to start login flow: ${error.message}`; session.process = null; + recordCodexLoginSession(session); }); child.once("close", (exitCode) => { + endOutput(); + clearCodexLoginKillTimer(session); session.exitCode = exitCode; session.process = null; if (session.state === "cancelled") { + recordCodexLoginSession(session); return; } @@ -379,9 +391,10 @@ function registerAgentDiscoveryHandlers(ctx) { session.state = "error"; session.error = session.error || `Codex login exited with code ${exitCode ?? "unknown"}`; } + recordCodexLoginSession(session); }); - codexLoginSessions.set(sessionId, session); + recordCodexLoginSession(session); invalidateCodexValidationCache(); return { ok: true, session: toCodexLoginSessionResponse(session) }; } catch (err) { @@ -407,9 +420,8 @@ function registerAgentDiscoveryHandlers(ctx) { session.state = "cancelled"; session.error = null; - if (session.process && !session.process.killed) { - session.process.kill("SIGTERM"); - } + stopCodexLoginProcess(session); + recordCodexLoginSession(session); invalidateCodexValidationCache(); return { ok: true, found: true, session: toCodexLoginSessionResponse(session) }; diff --git a/electron/bridges/aiBridge/codexAppServer/connection.cjs b/electron/bridges/aiBridge/codexAppServer/connection.cjs index 5b1c2bdd1..0249e0918 100644 --- a/electron/bridges/aiBridge/codexAppServer/connection.cjs +++ b/electron/bridges/aiBridge/codexAppServer/connection.cjs @@ -1,13 +1,66 @@ "use strict"; const path = require("node:path"); -const readline = require("node:readline"); const { spawn } = require("node:child_process"); const { createHash } = require("node:crypto"); +const { StringDecoder } = require("node:string_decoder"); const DEFAULT_REQUEST_TIMEOUT_MS = 30_000; const INITIALIZE_TIMEOUT_MS = 10_000; const MAX_STDERR_CHARS = 32_000; +const MAX_JSONL_LINE_BYTES = 16 * 1024 * 1024; +const CLOSE_KILL_GRACE_MS = 750; + +function createBoundedLineReader(stream, onLine, onError, maxLineBytes) { + const decoder = new StringDecoder("utf8"); + let buffer = ""; + let bufferedBytes = 0; + let closed = false; + + const fail = () => { + buffer = ""; + bufferedBytes = 0; + onError(new Error(`Codex App Server message exceeded ${maxLineBytes} bytes`)); + }; + const onData = (chunk) => { + if (closed) return; + const bytes = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk || "")); + bufferedBytes += bytes.length; + buffer += decoder.write(bytes); + let index; + let consumedLine = false; + while ((index = buffer.indexOf("\n")) >= 0) { + const line = buffer.slice(0, index).trim(); + buffer = buffer.slice(index + 1); + consumedLine = true; + if (line) onLine(line); + if (closed) return; + } + if (consumedLine) bufferedBytes = Buffer.byteLength(buffer, "utf8") + decoder.lastNeed; + if (bufferedBytes > maxLineBytes) fail(); + }; + const onEnd = () => { + if (closed) return; + buffer += decoder.end(); + const line = buffer.trim(); + buffer = ""; + bufferedBytes = 0; + if (line) onLine(line); + }; + + stream?.on?.("data", onData); + stream?.once?.("end", onEnd); + return { + close() { + if (closed) return; + closed = true; + buffer = ""; + bufferedBytes = 0; + stream?.removeListener?.("data", onData); + stream?.removeListener?.("end", onEnd); + }, + }; +} function buildCodexAppServerLaunch(binPath, args = ["app-server", "--stdio"], { nodePath = process.execPath, @@ -53,6 +106,8 @@ class CodexAppServerConnection { onNotification, onServerRequest, onFatal, + closeKillGraceMs = CLOSE_KILL_GRACE_MS, + maxJsonlLineBytes = MAX_JSONL_LINE_BYTES, }) { this.binPath = binPath; this.env = env || {}; @@ -61,7 +116,10 @@ class CodexAppServerConnection { this.onNotification = onNotification; this.onServerRequest = onServerRequest; this.onFatal = onFatal; + this.closeKillGraceMs = closeKillGraceMs; + this.maxJsonlLineBytes = maxJsonlLineBytes; this.process = null; + this.closingProcesses = new Map(); this.readline = null; this.nextRequestId = 1; this.pending = new Map(); @@ -98,12 +156,21 @@ class CodexAppServerConnection { this.stderr = `${this.stderr}${String(chunk || "")}`.slice(-MAX_STDERR_CHARS); }); - this.readline = readline.createInterface({ input: child.stdout }); - this.readline.on("line", (line) => this.#handleLine(line)); + this.readline = createBoundedLineReader( + child.stdout, + (line) => this.#handleLine(line), + (error) => this.#handleFatal(error), + this.maxJsonlLineBytes, + ); - child.once("error", (error) => this.#handleFatal(error)); + child.once("error", (error) => { + if (this.closingProcesses.has(child) || this.process !== child) return; + this.#handleFatal(error); + }); child.once("exit", (code, signal) => { - if (this.closing) return; + const wasClosing = this.closingProcesses.has(child); + if (wasClosing) this.#releaseClosingProcess(child); + if (wasClosing || this.process !== child) return; const detail = this.stderr.trim(); const suffix = detail ? `\n${detail}` : ""; this.#handleFatal(new Error( @@ -236,6 +303,16 @@ class CodexAppServerConnection { this.close(); } + #releaseClosingProcess(child) { + if (!this.closingProcesses.has(child)) return; + clearTimeout(this.closingProcesses.get(child)); + this.closingProcesses.delete(child); + } + + getClosingProcessCountForTests() { + return this.closingProcesses.size; + } + close() { this.closing = true; this.initialized = false; @@ -246,9 +323,19 @@ class CodexAppServerConnection { entry.reject(new Error("Codex App Server connection closed")); } this.pending.clear(); - try { this.process?.stdin?.end?.(); } catch {} - try { this.process?.kill?.("SIGTERM"); } catch {} + const child = this.process; this.process = null; + if (!child) return; + try { child.stdin?.end?.(); } catch {} + this.closingProcesses.set(child, null); + const killTimer = setTimeout(() => { + if (!this.closingProcesses.has(child)) return; + try { child.kill?.("SIGKILL"); } catch {} + this.#releaseClosingProcess(child); + }, this.closeKillGraceMs); + killTimer.unref?.(); + this.closingProcesses.set(child, killTimer); + try { child.kill?.("SIGTERM"); } catch {} } } @@ -258,4 +345,6 @@ module.exports = { buildCodexAppServerLaunch, DEFAULT_REQUEST_TIMEOUT_MS, INITIALIZE_TIMEOUT_MS, + CLOSE_KILL_GRACE_MS, + MAX_JSONL_LINE_BYTES, }; diff --git a/electron/bridges/aiBridge/codexAppServer/connection.test.cjs b/electron/bridges/aiBridge/codexAppServer/connection.test.cjs index b9d0f4b5f..63fc8cf3b 100644 --- a/electron/bridges/aiBridge/codexAppServer/connection.test.cjs +++ b/electron/bridges/aiBridge/codexAppServer/connection.test.cjs @@ -72,6 +72,48 @@ test("App Server connection initializes once and correlates JSONL requests", asy connection.close(); }); +test("App Server connection preserves a Chinese response split across UTF-8 chunks", async () => { + const child = createFakeChild(); + const connection = new CodexAppServerConnection({ + binPath: "/usr/bin/codex", + env: {}, + spawnImpl: () => child, + }); + + const startPromise = connection.start(); + const initialize = await readJsonLine(child.stdin); + const response = Buffer.from(`${JSON.stringify({ + id: initialize.id, + result: { message: "中文" }, + })}\n`, "utf8"); + const split = response.indexOf(Buffer.from("中", "utf8")) + 1; + child.stdout.write(response.subarray(0, split)); + child.stdout.write(response.subarray(split)); + await startPromise; + await readJsonLine(child.stdin); + connection.close(); +}); + +test("App Server connection rejects an unterminated oversized JSONL message", async () => { + const child = createFakeChild(); + let fatal; + const connection = new CodexAppServerConnection({ + binPath: "/usr/bin/codex", + env: {}, + maxJsonlLineBytes: 8, + spawnImpl: () => child, + onFatal: (error) => { fatal = error; }, + }); + + const startPromise = connection.start(); + await readJsonLine(child.stdin); + child.stdout.write("123456789"); + + await assert.rejects(startPromise, /message exceeded 8 bytes/); + assert.match(fatal.message, /message exceeded 8 bytes/); + assert.equal(child.killed, true); +}); + test("App Server connection rejects pending RPCs when the process exits", async () => { const child = createFakeChild(); let fatal; @@ -94,6 +136,58 @@ test("App Server connection rejects pending RPCs when the process exits", async assert.match(fatal.message, /code 1/); }); +test("App Server close force-kills a child that ignores SIGTERM", async () => { + const child = createFakeChild(); + const signals = []; + child.kill = (signal) => { + signals.push(signal); + return true; + }; + const connection = new CodexAppServerConnection({ + binPath: "/usr/bin/codex", + env: {}, + closeKillGraceMs: 5, + spawnImpl: () => child, + }); + const startPromise = connection.start(); + const initialize = await readJsonLine(child.stdin); + child.stdout.write(`${JSON.stringify({ id: initialize.id, result: {} })}\n`); + await startPromise; + await readJsonLine(child.stdin); + + connection.close(); + await new Promise((resolve) => setTimeout(resolve, 10)); + + assert.deepEqual(signals, ["SIGTERM", "SIGKILL"]); +}); + +test("App Server close does not retain or re-kill a child that exits synchronously on SIGTERM", async () => { + const child = createFakeChild(); + const signals = []; + child.kill = (signal) => { + signals.push(signal); + if (signal === "SIGTERM") child.emit("exit", 0, "SIGTERM"); + return true; + }; + const connection = new CodexAppServerConnection({ + binPath: "/usr/bin/codex", + env: {}, + closeKillGraceMs: 5, + spawnImpl: () => child, + }); + const startPromise = connection.start(); + const initialize = await readJsonLine(child.stdin); + child.stdout.write(`${JSON.stringify({ id: initialize.id, result: {} })}\n`); + await startPromise; + await readJsonLine(child.stdin); + + connection.close(); + assert.equal(connection.getClosingProcessCountForTests(), 0); + await new Promise((resolve) => setTimeout(resolve, 10)); + + assert.deepEqual(signals, ["SIGTERM"]); +}); + test("App Server process keys include executable and environment identity", () => { assert.notEqual( buildCodexAppServerKey("/a/codex", { HOME: "/a" }), diff --git a/electron/bridges/aiBridge/codexAppServer/runtime.cjs b/electron/bridges/aiBridge/codexAppServer/runtime.cjs index 5cd3d12f8..41d4e4415 100644 --- a/electron/bridges/aiBridge/codexAppServer/runtime.cjs +++ b/electron/bridges/aiBridge/codexAppServer/runtime.cjs @@ -10,6 +10,43 @@ const { } = require("../sdk/codexDriver.cjs"); const INTERACTION_TIMEOUT_MS = 5 * 60 * 1000; +const INTERRUPT_REQUEST_TIMEOUT_MS = 5_000; +const INTERRUPT_GRACE_MS = 2_000; +const MAX_STREAMED_PREFIX_CHARS = 256 * 1024; +const MAX_TOOL_OUTPUT_CHARS = 1024 * 1024; + +function appendStreamState(map, itemId, delta, maxPrefixChars = MAX_STREAMED_PREFIX_CHARS) { + const text = String(delta || ""); + const previous = map.get(itemId) || { prefix: "", length: 0, truncated: false }; + const remaining = Math.max(0, maxPrefixChars - previous.prefix.length); + const next = { + prefix: remaining > 0 ? previous.prefix + text.slice(0, remaining) : previous.prefix, + length: previous.length + text.length, + truncated: previous.truncated || text.length > remaining, + }; + map.set(itemId, next); + return next; +} + +function appendToolOutputState(map, itemId, delta) { + const text = String(delta || ""); + const previous = map.get(itemId) || { text: "", totalLength: 0, truncated: false }; + const remaining = Math.max(0, MAX_TOOL_OUTPUT_CHARS - previous.text.length); + const next = { + text: remaining > 0 ? previous.text + text.slice(0, remaining) : previous.text, + totalLength: previous.totalLength + text.length, + truncated: previous.truncated || text.length > remaining, + }; + map.set(itemId, next); + return next; +} + +function formatBoundedToolOutput(value, totalLength = String(value || "").length) { + const text = String(value || ""); + if (text.length <= MAX_TOOL_OUTPUT_CHARS && totalLength <= MAX_TOOL_OUTPUT_CHARS) return text; + const kept = text.slice(0, MAX_TOOL_OUTPUT_CHARS); + return `${kept}\n[output truncated: ${Math.max(totalLength, text.length)} characters total]`; +} function resolveCodexPermissionConfig(permissionMode) { if (permissionMode === "observer") { @@ -74,12 +111,21 @@ function normalizeGrantedPermissions(requested) { function stringifyMcpContent(result) { if (!result) return ""; const content = Array.isArray(result.content) ? result.content : []; - const text = content.map((item) => { - if (item && typeof item === "object" && typeof item.text === "string") return item.text; - return typeof item === "string" ? item : JSON.stringify(item); - }).join(""); - if (text) return text; - return result.structuredContent == null ? "" : JSON.stringify(result.structuredContent); + let text = ""; + let totalLength = 0; + for (const item of content) { + const rawPart = item && typeof item === "object" && typeof item.text === "string" + ? item.text + : typeof item === "string" ? item : JSON.stringify(item); + const part = typeof rawPart === "string" ? rawPart : ""; + totalLength += part.length; + if (text.length < MAX_TOOL_OUTPUT_CHARS) { + text += part.slice(0, MAX_TOOL_OUTPUT_CHARS - text.length); + } + } + if (text || totalLength > 0) return formatBoundedToolOutput(text, totalLength); + if (result.structuredContent == null) return ""; + return formatBoundedToolOutput(JSON.stringify(result.structuredContent)); } function buildTurnInput(prompt, attachments) { @@ -134,12 +180,17 @@ class CodexAppServerRuntime { connectionFactory, sendInteractionRequest, sendInteractionCleared, + interruptRequestTimeoutMs = INTERRUPT_REQUEST_TIMEOUT_MS, + interruptGraceMs = INTERRUPT_GRACE_MS, } = {}) { this.appVersion = appVersion; this.connectionFactory = connectionFactory; this.sendInteractionRequest = sendInteractionRequest; this.sendInteractionCleared = sendInteractionCleared; + this.interruptRequestTimeoutMs = interruptRequestTimeoutMs; + this.interruptGraceMs = interruptGraceMs; this.connections = new Map(); + this.preferredConnectionKey = null; this.activeByRequest = new Map(); this.activeByThread = new Map(); this.activeByTurn = new Map(); @@ -154,16 +205,13 @@ class CodexAppServerRuntime { #getConnection(binPath, env) { const connectionKey = buildCodexAppServerKey(binPath, env); + this.preferredConnectionKey = connectionKey; const existing = this.connections.get(connectionKey); - if (existing) return { connection: existing, connectionKey }; - for (const [otherKey, otherConnection] of this.connections) { - const inUse = Array.from(this.activeByRequest.values()) - .some((context) => context.connectionKey === otherKey); - if (!inUse) { - otherConnection.close(); - this.connections.delete(otherKey); - } + if (existing) { + this.#closeIdleConnections(connectionKey); + return { connection: existing, connectionKey }; } + this.#closeIdleConnections(connectionKey); const factory = this.connectionFactory || ((options) => new CodexAppServerConnection(options)); const connection = factory({ binPath, @@ -177,6 +225,23 @@ class CodexAppServerRuntime { return { connection, connectionKey }; } + #closeIdleConnections(keepKey = this.preferredConnectionKey) { + const activeConnectionKeys = new Set( + Array.from(this.activeByRequest.values(), (context) => context.connectionKey), + ); + for (const [connectionKey, connection] of this.connections) { + if (connectionKey === keepKey || activeConnectionKeys.has(connectionKey)) continue; + this.connections.delete(connectionKey); + try { connection.close(); } catch {} + } + } + + #refreshPreferredConnectionKey() { + if (this.preferredConnectionKey && this.connections.has(this.preferredConnectionKey)) return; + const connectionKeys = Array.from(this.connections.keys()); + this.preferredConnectionKey = connectionKeys.at(-1) || null; + } + async runTurn({ requestId, chatSessionId, @@ -243,6 +308,8 @@ class CodexAppServerRuntime { commandOutputByItem: new Map(), emittedToolCalls: new Set(), emittedToolResults: new Set(), + forceCancelTimer: null, + abortListener: null, }; this.activeByRequest.set(requestId, context); this.activeByThread.set(this.#scopedKey(connectionKey, threadId), context); @@ -251,6 +318,11 @@ class CodexAppServerRuntime { context.resolve = resolve; context.reject = reject; }); + if (signal) { + context.abortListener = () => { void this.cancelTurn(requestId); }; + signal.addEventListener("abort", context.abortListener, { once: true }); + if (signal.aborted) context.abortListener(); + } try { const turnResult = await connection.request("turn/start", { @@ -366,19 +438,80 @@ class CodexAppServerRuntime { } context.turnId = turnId; this.activeByTurn.set(this.#scopedKey(context.connectionKey, turnId), context); - if (context.cancelRequested) void this.#interruptContext(context); + if (context.cancelRequested) void this.#interruptAndSchedule(context); } #interruptContext(context) { if (!context.turnId) return Promise.resolve(false); if (context.interruptPromise) return context.interruptPromise; - context.interruptPromise = context.connection.request("turn/interrupt", { + let timeout; + const request = Promise.resolve().then(() => context.connection.request("turn/interrupt", { threadId: context.threadId, turnId: context.turnId, - }, 5_000).then(() => true).catch(() => false); + }, this.interruptRequestTimeoutMs)).then(() => true).catch(() => false); + const deadline = new Promise((resolve) => { + timeout = setTimeout(() => resolve(false), this.interruptRequestTimeoutMs); + timeout.unref?.(); + }); + context.interruptPromise = Promise.race([request, deadline]) + .finally(() => clearTimeout(timeout)); return context.interruptPromise; } + #scheduleForcedCancellation(context, delayMs) { + if (context.settled) return; + clearTimeout(context.forceCancelTimer); + context.forceCancelTimer = setTimeout(() => { + this.#forceCancelContext(context, "Codex App Server did not complete the interrupted turn"); + }, Math.max(0, delayMs)); + context.forceCancelTimer.unref?.(); + } + + async #interruptAndSchedule(context) { + if (context.settled) return; + if (!context.turnId) { + this.#scheduleForcedCancellation( + context, + this.interruptRequestTimeoutMs + this.interruptGraceMs, + ); + return; + } + const interrupted = await this.#interruptContext(context); + if (context.settled) return; + if (!interrupted) { + this.#forceCancelContext(context, "Codex App Server could not interrupt the turn"); + return; + } + this.#scheduleForcedCancellation(context, this.interruptGraceMs); + } + + #forceCancelContext(context, reason) { + if (context.settled) return; + context.settled = true; + clearTimeout(context.forceCancelTimer); + context.forceCancelTimer = null; + this.#closeReasoning(context); + this.#clearInteractionsForContext(context, "cancel"); + context.emitter.emitDone(); + context.resolve(); + + const connection = this.connections.get(context.connectionKey); + if (connection === context.connection) { + this.connections.delete(context.connectionKey); + try { connection.close(); } catch {} + this.#refreshPreferredConnectionKey(); + } + const error = new Error(reason); + for (const candidate of this.activeByRequest.values()) { + if (candidate === context || candidate.connectionKey !== context.connectionKey || candidate.settled) continue; + candidate.settled = true; + clearTimeout(candidate.forceCancelTimer); + candidate.forceCancelTimer = null; + this.#clearInteractionsForContext(candidate, "cancel"); + candidate.reject(error); + } + } + #findContext(connectionKey, params) { if (params?.turnId) { const byTurn = this.activeByTurn.get(this.#scopedKey(connectionKey, params.turnId)); @@ -413,21 +546,18 @@ class CodexAppServerRuntime { this.#assignTurnId(context, params.turn?.id); return; case "item/agentMessage/delta": { - const previous = context.streamedTextByItem.get(params.itemId) || ""; - context.streamedTextByItem.set(params.itemId, previous + String(params.delta || "")); + appendStreamState(context.streamedTextByItem, params.itemId, params.delta); emitter.text(params.delta || ""); return; } case "item/reasoning/summaryTextDelta": { - const previous = context.streamedReasoningByItem.get(params.itemId) || ""; - context.streamedReasoningByItem.set(params.itemId, previous + String(params.delta || "")); + appendStreamState(context.streamedReasoningByItem, params.itemId, params.delta); emitter.reasoning(params.delta || ""); context.reasoningOpen = true; return; } case "item/commandExecution/outputDelta": { - const previous = context.commandOutputByItem.get(params.itemId) || ""; - context.commandOutputByItem.set(params.itemId, previous + String(params.delta || "")); + appendToolOutputState(context.commandOutputByItem, params.itemId, params.delta); return; } case "item/started": @@ -506,17 +636,21 @@ class CodexAppServerRuntime { case "agentMessage": { if (!completed) return; this.#closeReasoning(context); - const streamed = context.streamedTextByItem.get(item.id) || ""; - if (item.text && item.text.startsWith(streamed)) emitter.text(item.text.slice(streamed.length)); - else if (item.text && !streamed) emitter.text(item.text); + const streamed = context.streamedTextByItem.get(item.id); + context.streamedTextByItem.delete(item.id); + if (item.text && streamed && item.text.startsWith(streamed.prefix)) { + if (item.text.length > streamed.length) emitter.text(item.text.slice(streamed.length)); + } else if (item.text && !streamed) emitter.text(item.text); return; } case "reasoning": { if (!completed) return; const finalText = Array.isArray(item.summary) ? item.summary.join("\n") : ""; - const streamed = context.streamedReasoningByItem.get(item.id) || ""; - if (finalText && finalText.startsWith(streamed)) emitter.reasoning(finalText.slice(streamed.length)); - else if (finalText && !streamed) emitter.reasoning(finalText); + const streamed = context.streamedReasoningByItem.get(item.id); + context.streamedReasoningByItem.delete(item.id); + if (finalText && streamed && finalText.startsWith(streamed.prefix)) { + if (finalText.length > streamed.length) emitter.reasoning(finalText.slice(streamed.length)); + } else if (finalText && !streamed) emitter.reasoning(finalText); context.reasoningOpen = true; this.#closeReasoning(context); return; @@ -525,7 +659,14 @@ class CodexAppServerRuntime { const toolName = "codex.command"; this.#emitToolCallOnce(context, item, toolName, { command: item.command, cwd: item.cwd }); if (completed) { - const output = item.aggregatedOutput ?? context.commandOutputByItem.get(item.id) ?? ""; + const streamedOutput = context.commandOutputByItem.get(item.id); + context.commandOutputByItem.delete(item.id); + const output = item.aggregatedOutput == null + ? formatBoundedToolOutput( + streamedOutput?.text || "", + streamedOutput?.totalLength || 0, + ) + : formatBoundedToolOutput(item.aggregatedOutput); const suffix = item.exitCode == null ? "" : `\n[exit code: ${item.exitCode}]`; this.#emitToolResultOnce(context, item, `${output}${suffix}`, toolName); } @@ -560,6 +701,8 @@ class CodexAppServerRuntime { #completeTurn(context, turn) { if (context.settled) return; context.settled = true; + clearTimeout(context.forceCancelTimer); + context.forceCancelTimer = null; this.#closeReasoning(context); this.#clearInteractionsForContext(context, "cancel"); if (turn?.status === "failed") { @@ -713,8 +856,7 @@ class CodexAppServerRuntime { if (!context) return false; context.cancelRequested = true; this.#clearInteractionsForContext(context, "cancel"); - if (!context.turnId) return true; - await this.#interruptContext(context); + await this.#interruptAndSchedule(context); return true; } @@ -726,21 +868,33 @@ class CodexAppServerRuntime { #handleConnectionFatal(connectionKey, error) { const connection = this.connections.get(connectionKey); - if (connection) this.connections.delete(connectionKey); + if (connection) { + this.connections.delete(connectionKey); + this.#refreshPreferredConnectionKey(); + } const contexts = Array.from(this.activeByRequest.values()) .filter((context) => context.connectionKey === connectionKey); for (const context of contexts) { if (context.settled) continue; context.settled = true; + clearTimeout(context.forceCancelTimer); + context.forceCancelTimer = null; this.#clearInteractionsForContext(context, "cancel"); context.reject(error); } } #removeContext(context) { + clearTimeout(context.forceCancelTimer); + context.forceCancelTimer = null; + if (context.abortListener && context.signal) { + context.signal.removeEventListener("abort", context.abortListener); + context.abortListener = null; + } this.activeByRequest.delete(context.requestId); this.activeByThread.delete(this.#scopedKey(context.connectionKey, context.threadId)); if (context.turnId) this.activeByTurn.delete(this.#scopedKey(context.connectionKey, context.turnId)); + this.#closeIdleConnections(); } close() { @@ -749,9 +903,12 @@ class CodexAppServerRuntime { } for (const [, connection] of this.connections) connection.close(); this.connections.clear(); + this.preferredConnectionKey = null; for (const context of this.activeByRequest.values()) { if (!context.settled) { context.settled = true; + clearTimeout(context.forceCancelTimer); + context.forceCancelTimer = null; context.reject(new Error("Codex App Server shut down")); } } diff --git a/electron/bridges/aiBridge/codexAppServer/runtime.test.cjs b/electron/bridges/aiBridge/codexAppServer/runtime.test.cjs index 935d6b7e4..3e2c20cf2 100644 --- a/electron/bridges/aiBridge/codexAppServer/runtime.test.cjs +++ b/electron/bridges/aiBridge/codexAppServer/runtime.test.cjs @@ -15,6 +15,7 @@ class FakeConnection { this.responses = []; this.threadId = "thread-1"; this.turnId = "turn-1"; + this.closed = false; } async start() { return this; } async request(method, params) { @@ -31,7 +32,11 @@ class FakeConnection { if (this.turnSteerError) throw this.turnSteerError; return { turnId: this.turnId }; } - if (method === "turn/interrupt") return {}; + if (method === "turn/interrupt") { + if (this.turnInterruptGate) await this.turnInterruptGate; + if (this.turnInterruptError) throw this.turnInterruptError; + return {}; + } if (method === "model/list") { return { data: [ @@ -63,7 +68,7 @@ class FakeConnection { respondError(id, code, message) { this.responses.push({ id, error: { code, message } }); } notify(message) { this.options.onNotification(message); } serverRequest(message) { return this.options.onServerRequest(message, this); } - close() {} + close() { this.closed = true; } } function createEmitter() { @@ -155,6 +160,55 @@ test("runtime maps lifecycle, activities, usage, and retry warnings", async () = assert.ok(emitter.events.some((event) => event[0] === "done")); }); +test("runtime bounds command output and avoids replaying streamed message prefixes", async () => { + let connection; + const runtime = new CodexAppServerRuntime({ + connectionFactory: (options) => (connection = new FakeConnection(options)), + }); + const emitter = createEmitter(); + const run = runtime.runTurn({ + requestId: "request-bounded-output", + chatSessionId: "chat-bounded-output", + prompt: "run", + permissionMode: "confirm", + env: {}, + binPath: "/bin/codex", + injectedMcpServers: [], + emitter, + }); + await waitFor(() => connection?.requests.some((request) => request.method === "turn/start")); + + connection.notify({ method: "item/agentMessage/delta", params: { + threadId: "thread-1", turnId: "turn-1", itemId: "msg-bounded", delta: "Hello", + } }); + connection.notify({ method: "item/completed", params: { + threadId: "thread-1", turnId: "turn-1", + item: { type: "agentMessage", id: "msg-bounded", text: "Hello world" }, + } }); + connection.notify({ method: "item/commandExecution/outputDelta", params: { + threadId: "thread-1", turnId: "turn-1", itemId: "cmd-bounded", + delta: "x".repeat(1024 * 1024 + 1024), + } }); + connection.notify({ method: "item/completed", params: { + threadId: "thread-1", turnId: "turn-1", + item: { type: "commandExecution", id: "cmd-bounded", command: "large", exitCode: 0 }, + } }); + connection.notify({ method: "turn/completed", params: { + threadId: "thread-1", turn: { id: "turn-1", status: "completed", error: null }, + } }); + await run; + + assert.deepEqual( + emitter.events.filter((event) => event[0] === "text"), + [["text", "Hello"], ["text", " world"]], + ); + const toolResult = emitter.events.find((event) => event[0] === "tool-result"); + assert.ok(toolResult); + assert.ok(toolResult[2].length < 1024 * 1024 + 200); + assert.match(toolResult[2], /output truncated: 1049600 characters total/); + assert.match(toolResult[2], /\[exit code: 0\]$/); +}); + test("runtime delegates injected MCP approvals to Netcatty's policy gate", async () => { let connection; const runtime = new CodexAppServerRuntime({ @@ -428,6 +482,202 @@ test("stop requested while turn/start is pending interrupts the assigned turn", assert.ok(emitter.events.some((event) => event[0] === "done")); }); +test("failed interrupt force-settles the turn and releases its connection", async () => { + let connection; + const runtime = new CodexAppServerRuntime({ + interruptGraceMs: 5, + connectionFactory: (options) => { + connection = new FakeConnection(options); + connection.turnInterruptError = new Error("interrupt unavailable"); + return connection; + }, + }); + const run = runtime.runTurn({ + requestId: "request-interrupt-failure", + chatSessionId: "chat-interrupt-failure", + prompt: "wait", + permissionMode: "confirm", + env: {}, + binPath: "/bin/codex", + injectedMcpServers: [], + emitter: createEmitter(), + }); + await waitFor(() => connection?.requests.some((request) => request.method === "turn/start")); + + assert.equal(await runtime.cancelTurn("request-interrupt-failure"), true); + await Promise.race([ + run, + new Promise((_, reject) => setTimeout(() => reject(new Error("turn did not settle")), 50)), + ]); + + assert.equal(connection.closed, true); + assert.equal(runtime.activeByRequest.size, 0); + assert.equal(runtime.activeByThread.size, 0); + assert.equal(runtime.activeByTurn.size, 0); +}); + +test("hung interrupt request times out and force-settles the turn", async () => { + let connection; + const runtime = new CodexAppServerRuntime({ + interruptRequestTimeoutMs: 5, + interruptGraceMs: 5, + connectionFactory: (options) => { + connection = new FakeConnection(options); + connection.turnInterruptGate = new Promise(() => {}); + return connection; + }, + }); + const run = runtime.runTurn({ + requestId: "request-interrupt-hung", + chatSessionId: "chat-interrupt-hung", + prompt: "wait", + permissionMode: "confirm", + env: {}, + binPath: "/bin/codex", + injectedMcpServers: [], + emitter: createEmitter(), + }); + await waitFor(() => connection?.requests.some((request) => request.method === "turn/start")); + + assert.equal(await runtime.cancelTurn("request-interrupt-hung"), true); + await Promise.race([ + run, + new Promise((_, reject) => setTimeout(() => reject(new Error("turn did not settle")), 50)), + ]); + + assert.equal(connection.closed, true); + assert.equal(runtime.activeByRequest.size, 0); +}); + +test("acknowledged interrupt force-settles when completion never arrives", async () => { + let connection; + const runtime = new CodexAppServerRuntime({ + interruptGraceMs: 5, + connectionFactory: (options) => (connection = new FakeConnection(options)), + }); + const run = runtime.runTurn({ + requestId: "request-interrupt-no-completion", + chatSessionId: "chat-interrupt-no-completion", + prompt: "wait", + permissionMode: "confirm", + env: {}, + binPath: "/bin/codex", + injectedMcpServers: [], + emitter: createEmitter(), + }); + await waitFor(() => connection?.requests.some((request) => request.method === "turn/start")); + + assert.equal(await runtime.cancelTurn("request-interrupt-no-completion"), true); + await Promise.race([ + run, + new Promise((_, reject) => setTimeout(() => reject(new Error("turn did not settle")), 50)), + ]); + + assert.equal(connection.closed, true); + assert.equal(runtime.activeByRequest.size, 0); +}); + +test("idle superseded app-server connections close when their active turn finishes", async () => { + const connections = []; + const runtime = new CodexAppServerRuntime({ + connectionFactory: (options) => { + const connection = new FakeConnection(options); + connection.threadId = `thread-${connections.length + 1}`; + connection.turnId = `turn-${connections.length + 1}`; + connections.push(connection); + return connection; + }, + }); + const firstRun = runtime.runTurn({ + requestId: "request-config-a", + chatSessionId: "chat-config-a", + prompt: "first", + permissionMode: "confirm", + env: { PROFILE: "a" }, + binPath: "/bin/codex", + injectedMcpServers: [], + emitter: createEmitter(), + }); + await waitFor(() => connections[0]?.requests.some((request) => request.method === "turn/start")); + const secondRun = runtime.runTurn({ + requestId: "request-config-b", + chatSessionId: "chat-config-b", + prompt: "second", + permissionMode: "confirm", + env: { PROFILE: "b" }, + binPath: "/bin/codex", + injectedMcpServers: [], + emitter: createEmitter(), + }); + await waitFor(() => connections[1]?.requests.some((request) => request.method === "turn/start")); + + connections[0].notify({ + method: "turn/completed", + params: { threadId: "thread-1", turn: { id: "turn-1", status: "completed", error: null } }, + }); + await firstRun; + assert.equal(connections[0].closed, true); + assert.equal(connections[1].closed, false); + + connections[1].notify({ + method: "turn/completed", + params: { threadId: "thread-2", turn: { id: "turn-2", status: "completed", error: null } }, + }); + await secondRun; + assert.equal(connections[1].closed, false); + runtime.close(); +}); + +test("force-cancelling the preferred connection keeps another active connection reusable", async () => { + const connections = []; + const runtime = new CodexAppServerRuntime({ + interruptGraceMs: 5, + connectionFactory: (options) => { + const connection = new FakeConnection(options); + connection.threadId = `thread-reuse-${connections.length + 1}`; + connection.turnId = `turn-reuse-${connections.length + 1}`; + connections.push(connection); + return connection; + }, + }); + const firstRun = runtime.runTurn({ + requestId: "request-reuse-a", + chatSessionId: "chat-reuse-a", + prompt: "first", + permissionMode: "confirm", + env: { PROFILE: "reuse-a" }, + binPath: "/bin/codex", + injectedMcpServers: [], + emitter: createEmitter(), + }); + await waitFor(() => connections[0]?.requests.some((request) => request.method === "turn/start")); + const secondRun = runtime.runTurn({ + requestId: "request-reuse-b", + chatSessionId: "chat-reuse-b", + prompt: "second", + permissionMode: "confirm", + env: { PROFILE: "reuse-b" }, + binPath: "/bin/codex", + injectedMcpServers: [], + emitter: createEmitter(), + }); + await waitFor(() => connections[1]?.requests.some((request) => request.method === "turn/start")); + + assert.equal(await runtime.cancelTurn("request-reuse-b"), true); + await secondRun; + connections[0].notify({ + method: "turn/completed", + params: { + threadId: "thread-reuse-1", + turn: { id: "turn-reuse-1", status: "completed", error: null }, + }, + }); + await firstRun; + + assert.equal(connections[0].closed, false); + runtime.close(); +}); + test("unsupported requests fail immediately and warn without hanging", async () => { let connection; const emitter = createEmitter(); diff --git a/electron/bridges/aiBridge/sdk/claudeDriver.cjs b/electron/bridges/aiBridge/sdk/claudeDriver.cjs index 44dcc06c7..6b2cce94a 100644 --- a/electron/bridges/aiBridge/sdk/claudeDriver.cjs +++ b/electron/bridges/aiBridge/sdk/claudeDriver.cjs @@ -278,32 +278,67 @@ function mapClaudeModels(models) { * @param {object} [args.env] * @param {Function} [args.queryFn] inject query() for tests */ -async function listClaudeModels({ pathToClaudeCodeExecutable, env, queryFn }) { +async function listClaudeModels({ + pathToClaudeCodeExecutable, + env, + queryFn, + abortController, + signal, +}) { ensureClaudeConfig(); + const externalSignal = signal || abortController?.signal; + if (externalSignal?.aborted) return []; let query = queryFn; if (!query) { let sdk; try { sdk = await import("@anthropic-ai/claude-agent-sdk"); } catch { return []; } query = sdk.query; } - const abortController = new AbortController(); + const queryAbortController = new AbortController(); + const forwardAbort = () => { + try { queryAbortController.abort(externalSignal?.reason); } catch {} + }; + if (externalSignal) { + externalSignal.addEventListener("abort", forwardAbort, { once: true }); + if (externalSignal.aborted) forwardAbort(); + } // Idle streaming input: keeps the session open (init handshake completes) // without sending a turn, so supportedModels() resolves; then we abort. async function* idleInput() { await new Promise((resolve) => { - if (abortController.signal.aborted) return resolve(); - abortController.signal.addEventListener("abort", () => resolve(), { once: true }); + if (queryAbortController.signal.aborted) return resolve(); + queryAbortController.signal.addEventListener("abort", () => resolve(), { once: true }); }); } - const q = query({ - prompt: idleInput(), - options: { pathToClaudeCodeExecutable, env, abortController, includePartialMessages: false }, - }); + let q; try { - return mapClaudeModels(await q.supportedModels()); + q = query({ + prompt: idleInput(), + options: { + pathToClaudeCodeExecutable, + env, + abortController: queryAbortController, + includePartialMessages: false, + }, + }); + const result = await Promise.race([ + Promise.resolve(q.supportedModels()).then((models) => ({ type: "models", models })), + new Promise((resolve) => { + if (queryAbortController.signal.aborted) return resolve({ type: "aborted" }); + queryAbortController.signal.addEventListener( + "abort", + () => resolve({ type: "aborted" }), + { once: true }, + ); + }), + ]); + return result.type === "models" ? mapClaudeModels(result.models) : []; + } catch { + return []; } finally { - abortController.abort(); - try { await q.return?.(undefined); } catch { /* best effort */ } + if (externalSignal) externalSignal.removeEventListener("abort", forwardAbort); + queryAbortController.abort(); + try { void Promise.resolve(q?.return?.(undefined)).catch(() => {}); } catch { /* best effort */ } } } diff --git a/electron/bridges/aiBridge/sdk/claudeDriver.test.cjs b/electron/bridges/aiBridge/sdk/claudeDriver.test.cjs index e5a3ea1cc..e320a3d04 100644 --- a/electron/bridges/aiBridge/sdk/claudeDriver.test.cjs +++ b/electron/bridges/aiBridge/sdk/claudeDriver.test.cjs @@ -1,6 +1,6 @@ const test = require("node:test"); const assert = require("node:assert/strict"); -const { translateClaudeMessage, buildClaudeQueryOptions, buildClaudePromptInput, classifyClaudeSpawnError, mapClaudeModels, parseClaudeSettings } = require("./claudeDriver.cjs"); +const { translateClaudeMessage, buildClaudeQueryOptions, buildClaudePromptInput, classifyClaudeSpawnError, listClaudeModels, mapClaudeModels, parseClaudeSettings } = require("./claudeDriver.cjs"); function collector() { const events = []; @@ -183,3 +183,38 @@ test("buildClaudePromptInput keeps plain text when there are no supported images "hello", ); }); + +test("listClaudeModels aborts a hung SDK query and returns it for cleanup", async () => { + const abortController = new AbortController(); + let queryAbortSignal; + let returnCount = 0; + let releaseModels; + const pendingModels = new Promise((resolve) => { releaseModels = resolve; }); + const queryFn = ({ options }) => { + queryAbortSignal = options.abortController.signal; + return { + supportedModels: () => pendingModels, + async return() { + returnCount += 1; + }, + }; + }; + + const modelsPromise = listClaudeModels({ + pathToClaudeCodeExecutable: "/bin/claude", + env: {}, + queryFn, + abortController, + }); + abortController.abort(); + + const outcome = await Promise.race([ + modelsPromise.then(() => "settled"), + new Promise((resolve) => setTimeout(() => resolve("hung"), 20)), + ]); + if (outcome === "hung") releaseModels([]); + assert.equal(outcome, "settled"); + assert.deepEqual(await modelsPromise, []); + assert.equal(queryAbortSignal.aborted, true); + assert.equal(returnCount, 1); +}); diff --git a/electron/bridges/aiBridge/sdk/codebuddyDriver.cjs b/electron/bridges/aiBridge/sdk/codebuddyDriver.cjs index 7319f6b8b..e0010f08b 100644 --- a/electron/bridges/aiBridge/sdk/codebuddyDriver.cjs +++ b/electron/bridges/aiBridge/sdk/codebuddyDriver.cjs @@ -403,31 +403,64 @@ function mapCodebuddyModels(models) { * @param {object} [args.env] * @param {Function} [args.queryFn] inject query() for tests */ -async function listCodebuddyModels({ pathToCodebuddyCode, env, queryFn }) { +async function listCodebuddyModels({ + pathToCodebuddyCode, + env, + queryFn, + abortController, + signal, +}) { + const externalSignal = signal || abortController?.signal; + if (externalSignal?.aborted) return []; let query = queryFn; if (!query) { let sdk; try { sdk = await import("@tencent-ai/agent-sdk"); } catch { return []; } query = sdk.query; } - const abortController = new AbortController(); + const queryAbortController = new AbortController(); + const forwardAbort = () => { + try { queryAbortController.abort(externalSignal?.reason); } catch {} + }; + if (externalSignal) { + externalSignal.addEventListener("abort", forwardAbort, { once: true }); + if (externalSignal.aborted) forwardAbort(); + } async function* idleInput() { await new Promise((resolve) => { - if (abortController.signal.aborted) return resolve(); - abortController.signal.addEventListener("abort", () => resolve(), { once: true }); + if (queryAbortController.signal.aborted) return resolve(); + queryAbortController.signal.addEventListener("abort", () => resolve(), { once: true }); }); } - const q = query({ - prompt: idleInput(), - options: { pathToCodebuddyCode, env, abortController, includePartialMessages: false }, - }); + let q; try { - return mapCodebuddyModels(await q.supportedModels()); + q = query({ + prompt: idleInput(), + options: { + pathToCodebuddyCode, + env, + abortController: queryAbortController, + includePartialMessages: false, + }, + }); + const result = await Promise.race([ + Promise.resolve(q.supportedModels()).then((models) => ({ type: "models", models })), + new Promise((resolve) => { + if (queryAbortController.signal.aborted) return resolve({ type: "aborted" }); + queryAbortController.signal.addEventListener( + "abort", + () => resolve({ type: "aborted" }), + { once: true }, + ); + }), + ]); + return result.type === "models" ? mapCodebuddyModels(result.models) : []; } catch { return []; } finally { - abortController.abort(); - try { await q.return?.(undefined); } catch { /* best effort */ } + if (externalSignal) externalSignal.removeEventListener("abort", forwardAbort); + queryAbortController.abort(); + try { void Promise.resolve(q?.return?.(undefined)).catch(() => {}); } catch { /* best effort */ } } } diff --git a/electron/bridges/aiBridge/sdk/copilotDriver.cjs b/electron/bridges/aiBridge/sdk/copilotDriver.cjs index f35dbed22..ba6c730f6 100644 --- a/electron/bridges/aiBridge/sdk/copilotDriver.cjs +++ b/electron/bridges/aiBridge/sdk/copilotDriver.cjs @@ -493,7 +493,9 @@ function mapCopilotModels(models) { * @param {string} [args.cliPath] * @param {object} [args.sdkModule] inject the @github/copilot-sdk module (for tests) */ -async function listCopilotModels({ cliPath, sdkModule }) { +async function listCopilotModels({ cliPath, sdkModule, abortController, signal }) { + const externalSignal = signal || abortController?.signal; + if (externalSignal?.aborted) return []; let resolvedModule = sdkModule; if (!resolvedModule) { try { resolvedModule = await import("@github/copilot-sdk"); } catch { return []; } @@ -505,11 +507,37 @@ async function listCopilotModels({ cliPath, sdkModule }) { clientOptions.connection = RuntimeConnection.forStdio({ path: cliPath }); } const client = new CopilotClient(clientOptions); + let stopPromise; + const stopClient = () => { + if (!stopPromise) { + try { stopPromise = Promise.resolve(client.stop()).catch(() => {}); } catch { stopPromise = Promise.resolve(); } + } + return stopPromise; + }; + let resolveAbort; + const aborted = new Promise((resolve) => { resolveAbort = resolve; }); + const onAbort = () => { + resolveAbort({ type: "aborted" }); + void stopClient(); + }; + externalSignal?.addEventListener("abort", onAbort, { once: true }); + if (externalSignal?.aborted) onAbort(); try { - await client.start(); - return mapCopilotModels(await client.listModels()); + const started = await Promise.race([ + Promise.resolve(client.start()).then(() => ({ type: "started" })), + aborted, + ]); + if (started.type === "aborted") return []; + const result = await Promise.race([ + Promise.resolve(client.listModels()).then((models) => ({ type: "models", models })), + aborted, + ]); + return result.type === "models" ? mapCopilotModels(result.models) : []; + } catch { + return []; } finally { - try { await client.stop(); } catch { /* best effort */ } + externalSignal?.removeEventListener("abort", onAbort); + void stopClient(); } } diff --git a/electron/bridges/aiBridge/sdk/cursorCliDriver.cjs b/electron/bridges/aiBridge/sdk/cursorCliDriver.cjs index 60a770cfb..82d0a34f8 100644 --- a/electron/bridges/aiBridge/sdk/cursorCliDriver.cjs +++ b/electron/bridges/aiBridge/sdk/cursorCliDriver.cjs @@ -7,12 +7,48 @@ * CLI login quota without CURSOR_API_KEY. */ const { spawn } = require("node:child_process"); +const { StringDecoder } = require("node:string_decoder"); const fs = require("node:fs"); const path = require("node:path"); const { mcpEnvPairsToObject } = require("./injectMcp.cjs"); const DEFAULT_CURSOR_CLI_MODEL = "auto"; const NETCATTY_MCP_NAME = "netcatty-remote-hosts"; +const CURSOR_CLI_ABORT_GRACE_MS = 1_500; +const MAX_CURSOR_CLI_STDERR_CHARS = 64 * 1024; +const MAX_CURSOR_CLI_MODEL_STDOUT_CHARS = 1024 * 1024; +const MAX_CURSOR_CLI_LINE_BYTES = 10 * 1024 * 1024; + +function signalCursorCliProcessTree(child, signal, forceKillImpl) { + if (!child) return; + if (typeof forceKillImpl === "function") { + try { forceKillImpl(child, signal); } catch {} + return; + } + if (process.platform === "win32" && signal === "SIGKILL" && child.pid) { + try { + const killer = spawn("taskkill", ["/pid", String(child.pid), "/T", "/F"], { + stdio: "ignore", + windowsHide: true, + }); + killer.on("error", () => {}); + killer.unref?.(); + return; + } catch { + // Fall through to ChildProcess.kill below. + } + } + if (process.platform !== "win32" && child.pid) { + try { + process.kill(-child.pid, signal); + return; + } catch { + // The child may not be a process-group leader (for injected tests or an + // older runtime). Fall back to killing the direct child. + } + } + try { child.kill(signal); } catch { /* ignore */ } +} function stripCursorApiKeyFromEnv(env) { const out = { ...(env || {}) }; @@ -374,19 +410,37 @@ function formatCursorCliErrorForUser(message) { return text || "Cursor CLI turn failed"; } -function createLineBuffer(onLine) { +function createLineBuffer(onLine, maxBufferBytes = MAX_CURSOR_CLI_LINE_BYTES) { let buffer = ""; + let bufferedBytes = 0; + let overflowed = false; + const decoder = new StringDecoder("utf8"); return { push(chunk) { - buffer += String(chunk || ""); + if (overflowed) return; + const bytes = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk || "")); + bufferedBytes += bytes.length; + buffer += decoder.write(bytes); let idx; + let consumedLine = false; while ((idx = buffer.indexOf("\n")) >= 0) { const line = buffer.slice(0, idx).trim(); buffer = buffer.slice(idx + 1); + consumedLine = true; if (line) onLine(line); } + if (consumedLine) bufferedBytes = Buffer.byteLength(buffer, "utf8") + decoder.lastNeed; + if (bufferedBytes > maxBufferBytes) { + overflowed = true; + buffer = ""; + const error = new Error(`Cursor CLI message exceeded ${maxBufferBytes} bytes`); + error.code = "CURSOR_CLI_LINE_LIMIT"; + throw error; + } }, flush() { + if (overflowed) return; + buffer += decoder.end(); const line = buffer.trim(); buffer = ""; if (line) onLine(line); @@ -410,6 +464,8 @@ async function runCursorCliTurn({ spawnImpl, mergeMcp, workspaceCwd, + abortGraceMs = CURSOR_CLI_ABORT_GRACE_MS, + forceKillImpl, }) { const cliPath = String(binPath || "").trim(); if (!cliPath) { @@ -478,6 +534,7 @@ async function runCursorCliTurn({ env: childEnv, stdio: ["ignore", "pipe", "pipe"], windowsHide: true, + detached: process.platform !== "win32", }); } catch (err) { cleanup(); @@ -499,29 +556,41 @@ async function runCursorCliTurn({ }; const stdoutBuffer = createLineBuffer(handleLine); - const stderrChunks = []; + let stderrText = ""; + let stderrBytes = 0; + let stderrTruncated = false; + let stderrEnded = false; + const stderrDecoder = new StringDecoder("utf8"); child.stdout?.on("data", (chunk) => { if (signal?.aborted) return; - stdoutBuffer.push(chunk); + try { + stdoutBuffer.push(chunk); + } catch (error) { + if (!state.failed) { + state.failed = true; + emitter.emitError(formatCursorCliErrorForUser(error?.message || String(error))); + } + signalCursorCliProcessTree(child, "SIGKILL", forceKillImpl); + } }); child.stderr?.on("data", (chunk) => { - stderrChunks.push(String(chunk)); + if (signal?.aborted) return; + const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk)); + const remaining = Math.max(0, MAX_CURSOR_CLI_STDERR_CHARS - stderrBytes); + const accepted = buffer.length <= remaining ? buffer : buffer.subarray(0, remaining); + if (accepted.length > 0) stderrText += stderrDecoder.write(accepted); + stderrBytes += accepted.length; + if (accepted.length < buffer.length) stderrTruncated = true; }); - const abortHandler = () => { - if (!child || child.killed) return; - try { child.kill("SIGTERM"); } catch { /* ignore */ } - }; - if (signal) { - if (signal.aborted) abortHandler(); - else signal.addEventListener("abort", abortHandler, { once: true }); - } - + let abortHandler = null; + let forceKillTimer = null; await new Promise((resolve) => { const finish = () => { if (settled) return; settled = true; + clearTimeout(forceKillTimer); // Only flush remaining lines if not aborted — late error/result after // Stop must not surface as a failed turn. if (!signal?.aborted) stdoutBuffer.flush(); @@ -536,15 +605,39 @@ async function runCursorCliTurn({ finish(); }); child.on("close", (code) => { + if (!stderrEnded) { + stderrEnded = true; + if (!stderrTruncated || stderrDecoder.lastNeed === 0) stderrText += stderrDecoder.end(); + } // Soft-cancel: SIGTERM/kill after abort is not a turn failure. if (!state.failed && !signal?.aborted && code && code !== 0 && !state.streamedAssistantText) { - const stderr = stderrChunks.join("").trim(); + const stderr = stderrText.trim(); const message = stderr || `Cursor CLI exited with code ${code}`; state.failed = true; emitter.emitError(formatCursorCliErrorForUser(message)); } finish(); }); + + let terminationStarted = false; + abortHandler = () => { + if (settled || terminationStarted) return; + terminationStarted = true; + forceKillTimer = setTimeout(() => { + if (settled) return; + signalCursorCliProcessTree(child, "SIGKILL", forceKillImpl); + // Process APIs do not guarantee a close event when process-tree + // termination itself fails. Stop must still release MCP config and the + // renderer request within a fixed deadline. + finish(); + }, Math.max(0, abortGraceMs)); + forceKillTimer.unref?.(); + signalCursorCliProcessTree(child, "SIGTERM"); + }; + if (signal) { + if (signal.aborted) abortHandler(); + else signal.addEventListener("abort", abortHandler, { once: true }); + } }); if (signal) signal.removeEventListener("abort", abortHandler); @@ -559,19 +652,39 @@ async function runCursorCliTurn({ return { sessionId: state.sessionId }; } -async function listCursorCliModels({ binPath, env, spawnImpl } = {}) { +async function listCursorCliModels({ + binPath, + env, + spawnImpl, + abortController, + signal, + abortGraceMs = CURSOR_CLI_ABORT_GRACE_MS, + forceKillImpl, +} = {}) { const cliPath = String(binPath || "").trim(); if (!cliPath) return { currentModelId: null, models: [] }; + const abortSignal = signal || abortController?.signal; + if (abortSignal?.aborted) return { currentModelId: null, models: [] }; const childEnv = stripCursorApiKeyFromEnv(env || process.env); const spawnFn = spawnImpl || spawn; return await new Promise((resolve) => { let stdout = ""; + let stdoutBytes = 0; + let stdoutTruncated = false; + let stdoutEnded = false; + const stdoutDecoder = new StringDecoder("utf8"); let settled = false; + let abortHandler = null; + let forceKillTimer = null; const finish = (value) => { if (settled) return; settled = true; + clearTimeout(forceKillTimer); + if (abortSignal && abortHandler) { + abortSignal.removeEventListener("abort", abortHandler); + } resolve(value); }; @@ -581,15 +694,28 @@ async function listCursorCliModels({ binPath, env, spawnImpl } = {}) { env: childEnv, stdio: ["ignore", "pipe", "pipe"], windowsHide: true, + detached: process.platform !== "win32", }); } catch { finish({ currentModelId: null, models: [] }); return; } - child.stdout?.on("data", (chunk) => { stdout += String(chunk); }); + child.stdout?.on("data", (chunk) => { + if (abortSignal?.aborted) return; + const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk)); + const remaining = Math.max(0, MAX_CURSOR_CLI_MODEL_STDOUT_CHARS - stdoutBytes); + const accepted = buffer.length <= remaining ? buffer : buffer.subarray(0, remaining); + if (accepted.length > 0) stdout += stdoutDecoder.write(accepted); + stdoutBytes += accepted.length; + if (accepted.length < buffer.length) stdoutTruncated = true; + }); child.on("error", () => finish({ currentModelId: null, models: [] })); child.on("close", () => { + if (!stdoutEnded) { + stdoutEnded = true; + if (!stdoutTruncated || stdoutDecoder.lastNeed === 0) stdout += stdoutDecoder.end(); + } const models = []; const seen = new Set(); let currentModelId = null; @@ -615,13 +741,30 @@ async function listCursorCliModels({ binPath, env, spawnImpl } = {}) { } finish({ currentModelId, models }); }); + + abortHandler = () => { + if (settled) return; + forceKillTimer = setTimeout(() => { + if (settled) return; + signalCursorCliProcessTree(child, "SIGKILL", forceKillImpl); + finish({ currentModelId: null, models: [] }); + }, Math.max(0, abortGraceMs)); + forceKillTimer.unref?.(); + signalCursorCliProcessTree(child, "SIGTERM", forceKillImpl); + }; + if (abortSignal) { + if (abortSignal.aborted) abortHandler(); + else abortSignal.addEventListener("abort", abortHandler, { once: true }); + } }); } module.exports = { DEFAULT_CURSOR_CLI_MODEL, + MAX_CURSOR_CLI_LINE_BYTES, NETCATTY_MCP_NAME, buildCursorCliArgs, + createLineBuffer, formatCursorCliErrorForUser, listCursorCliModels, mergeWorkspaceMcpJson, diff --git a/electron/bridges/aiBridge/sdk/cursorCliDriver.test.cjs b/electron/bridges/aiBridge/sdk/cursorCliDriver.test.cjs index 79dd4404d..658b53601 100644 --- a/electron/bridges/aiBridge/sdk/cursorCliDriver.test.cjs +++ b/electron/bridges/aiBridge/sdk/cursorCliDriver.test.cjs @@ -6,6 +6,7 @@ const os = require("node:os"); const path = require("node:path"); const { buildCursorCliArgs, + createLineBuffer, formatCursorCliErrorForUser, listCursorCliModels, mergeWorkspaceMcpJson, @@ -46,6 +47,18 @@ test("stripCursorApiKeyFromEnv removes CURSOR_API_KEY", () => { ); }); +test("createLineBuffer rejects and releases an unterminated oversized message", () => { + const lines = []; + const lineBuffer = createLineBuffer((line) => lines.push(line), 8); + lineBuffer.push(Buffer.from("12345678")); + assert.throws( + () => lineBuffer.push(Buffer.from("9")), + (error) => error?.code === "CURSOR_CLI_LINE_LIMIT", + ); + lineBuffer.flush(); + assert.deepEqual(lines, []); +}); + test("buildCursorCliArgs maps permission modes and resume", () => { assert.deepEqual( buildCursorCliArgs({ @@ -269,6 +282,71 @@ test("runCursorCliTurn strips API key, parses stream, emits done", async () => { ]); }); +test("runCursorCliTurn preserves a Chinese JSON event split across UTF-8 chunks", async () => { + const emitter = makeEmitter(); + const fakeChild = new EventEmitter(); + fakeChild.stdout = new EventEmitter(); + fakeChild.stderr = new EventEmitter(); + fakeChild.kill = () => {}; + + await runCursorCliTurn({ + prompt: "hi", + binPath: "/bin/agent", + cwd: "/repo", + env: {}, + permissionMode: "confirm", + injectedMcpServers: [], + emitter, + spawnImpl: () => { + queueMicrotask(() => { + const line = Buffer.from(`${JSON.stringify({ + type: "assistant", + timestamp_ms: 1, + message: { content: [{ type: "text", text: "中文回复" }] }, + })}\n`, "utf8"); + const split = line.indexOf(Buffer.from("中", "utf8")) + 2; + fakeChild.stdout.emit("data", line.subarray(0, split)); + fakeChild.stdout.emit("data", line.subarray(split)); + fakeChild.emit("close", 0); + }); + return fakeChild; + }, + mergeMcp: () => ({ restore() {} }), + }); + + assert.ok(emitter.calls.some((call) => call[0] === "text" && call[1] === "中文回复")); +}); + +test("runCursorCliTurn preserves Chinese stderr split across UTF-8 chunks", async () => { + const emitter = makeEmitter(); + const fakeChild = new EventEmitter(); + fakeChild.stdout = new EventEmitter(); + fakeChild.stderr = new EventEmitter(); + fakeChild.kill = () => {}; + + await runCursorCliTurn({ + prompt: "hi", + binPath: "/bin/agent", + cwd: "/repo", + env: {}, + permissionMode: "confirm", + injectedMcpServers: [], + emitter, + spawnImpl: () => { + queueMicrotask(() => { + const bytes = Buffer.from("中文错误", "utf8"); + fakeChild.stderr.emit("data", bytes.subarray(0, 2)); + fakeChild.stderr.emit("data", bytes.subarray(2)); + fakeChild.emit("close", 1); + }); + return fakeChild; + }, + mergeMcp: () => ({ restore() {} }), + }); + + assert.ok(emitter.calls.some((call) => call[0] === "error" && call[1] === "中文错误")); +}); + test("runCursorCliTurn abort after text does not emit done", async () => { const emitter = makeEmitter(); const ac = new AbortController(); @@ -345,6 +423,47 @@ test("runCursorCliTurn abort before any text is soft cancel (no error/done)", as assert.deepEqual(emitter.calls, []); }); +test("runCursorCliTurn force-kills and settles when the CLI ignores SIGTERM", async () => { + const emitter = makeEmitter(); + const ac = new AbortController(); + const fakeChild = new EventEmitter(); + fakeChild.stdout = new EventEmitter(); + fakeChild.stderr = new EventEmitter(); + fakeChild.killed = false; + const signals = []; + fakeChild.kill = (signal) => { + signals.push(signal); + return true; + }; + let restored = false; + + const turn = runCursorCliTurn({ + prompt: "hi", + binPath: "/bin/agent", + cwd: "/repo", + model: "auto", + env: {}, + permissionMode: "confirm", + injectedMcpServers: [{ name: "netcatty", command: "node", args: [] }], + emitter, + signal: ac.signal, + abortGraceMs: 5, + forceKillImpl: (child) => child.kill("SIGKILL"), + spawnImpl: () => fakeChild, + mergeMcp: () => ({ restore() { restored = true; } }), + }); + ac.abort(); + + await Promise.race([ + turn, + new Promise((_, reject) => setTimeout(() => reject(new Error("aborted Cursor CLI did not settle")), 50)), + ]); + + assert.deepEqual(signals, ["SIGTERM", "SIGKILL"]); + assert.equal(restored, true); + assert.deepEqual(emitter.calls, []); +}); + test("runCursorCliTurn ignores late error events after abort (before text)", async () => { const emitter = makeEmitter(); const ac = new AbortController(); @@ -505,6 +624,61 @@ test("listCursorCliModels parses agent models output and prefers auto", async () }); }); +test("listCursorCliModels preserves Chinese model names split across UTF-8 chunks", async () => { + const fakeChild = new EventEmitter(); + fakeChild.stdout = new EventEmitter(); + fakeChild.stderr = new EventEmitter(); + fakeChild.kill = () => {}; + + const catalogPromise = listCursorCliModels({ + binPath: "/bin/agent", + env: {}, + spawnImpl: () => { + queueMicrotask(() => { + const bytes = Buffer.from("model-cn - 中文模型\n", "utf8"); + const split = bytes.indexOf(Buffer.from("中", "utf8")) + 1; + fakeChild.stdout.emit("data", bytes.subarray(0, split)); + fakeChild.stdout.emit("data", bytes.subarray(split)); + fakeChild.emit("close", 0); + }); + return fakeChild; + }, + }); + + assert.deepEqual(await catalogPromise, { + currentModelId: null, + models: [{ id: "model-cn", name: "中文模型" }], + }); +}); + +test("listCursorCliModels aborts a hung CLI and settles after forced cleanup", async () => { + const fakeChild = new EventEmitter(); + fakeChild.stdout = new EventEmitter(); + fakeChild.stderr = new EventEmitter(); + fakeChild.pid = 4242; + const signals = []; + const abortController = new AbortController(); + + const catalogPromise = listCursorCliModels({ + binPath: "/bin/agent", + env: {}, + abortController, + abortGraceMs: 0, + forceKillImpl: (_child, signal) => signals.push(signal), + spawnImpl: () => fakeChild, + }); + abortController.abort(); + + const outcome = await Promise.race([ + catalogPromise.then(() => "settled"), + new Promise((resolve) => setTimeout(() => resolve("hung"), 20)), + ]); + if (outcome === "hung") fakeChild.emit("close", 0); + assert.equal(outcome, "settled"); + assert.deepEqual(await catalogPromise, { currentModelId: null, models: [] }); + assert.deepEqual(signals, ["SIGTERM", "SIGKILL"]); +}); + test("resolveCursorCliWorkspaceCwd prefers Netcatty temp over unwritable preferred cwd", () => { const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-cli-ws-")); const resolved = resolveCursorCliWorkspaceCwd({ diff --git a/electron/bridges/aiBridge/sdk/cursorDriver.cjs b/electron/bridges/aiBridge/sdk/cursorDriver.cjs index f22874371..790eca135 100644 --- a/electron/bridges/aiBridge/sdk/cursorDriver.cjs +++ b/electron/bridges/aiBridge/sdk/cursorDriver.cjs @@ -449,15 +449,32 @@ function mapCursorModels(models) { return out; } -async function listCursorModels({ apiKey, env, sdkModule } = {}) { +async function listCursorModels({ apiKey, env, sdkModule, abortController, signal } = {}) { + const externalSignal = signal || abortController?.signal; + if (externalSignal?.aborted) return []; let resolvedModule = sdkModule; if (!resolvedModule) { try { resolvedModule = await import("@cursor/sdk"); } catch { return []; } } const effectiveApiKey = apiKey || env?.CURSOR_API_KEY || process.env.CURSOR_API_KEY; if (!effectiveApiKey) return []; - const models = await resolvedModule.Cursor.models.list({ apiKey: effectiveApiKey }); - return mapCursorModels(models); + let abortHandler; + try { + const result = await Promise.race([ + Promise.resolve(resolvedModule.Cursor.models.list({ + apiKey: effectiveApiKey, + signal: externalSignal, + })).then((models) => ({ type: "models", models })), + new Promise((resolve) => { + if (externalSignal?.aborted) return resolve({ type: "aborted" }); + abortHandler = () => resolve({ type: "aborted" }); + externalSignal?.addEventListener("abort", abortHandler, { once: true }); + }), + ]); + return result.type === "models" ? mapCursorModels(result.models) : []; + } finally { + if (abortHandler) externalSignal?.removeEventListener("abort", abortHandler); + } } module.exports = { diff --git a/electron/bridges/aiBridge/sdk/index.cjs b/electron/bridges/aiBridge/sdk/index.cjs index 581f6d9eb..7bcd1828b 100644 --- a/electron/bridges/aiBridge/sdk/index.cjs +++ b/electron/bridges/aiBridge/sdk/index.cjs @@ -34,7 +34,12 @@ const DRIVER_REGISTRY = { return claude.runClaudeTurn({ prompt: ctx.prompt, attachments: ctx.attachments, options, emitter: ctx.emitter }); }, async listModels(ctx) { - return claude.listClaudeModels({ pathToClaudeCodeExecutable: ctx.binPath, env: ctx.env }); + return claude.listClaudeModels({ + pathToClaudeCodeExecutable: ctx.binPath, + env: ctx.env, + abortController: ctx.abortController, + signal: ctx.signal, + }); }, }, codex: { @@ -81,7 +86,11 @@ const DRIVER_REGISTRY = { }); }, async listModels(ctx) { - return copilot.listCopilotModels({ cliPath: ctx.binPath }); + return copilot.listCopilotModels({ + cliPath: ctx.binPath, + abortController: ctx.abortController, + signal: ctx.signal, + }); }, }, cursor: { @@ -125,9 +134,15 @@ const DRIVER_REGISTRY = { return cursorCli.listCursorCliModels({ binPath: ctx.cursorCliBinPath || ctx.binPath, env: ctx.env, + abortController: ctx.abortController, + signal: ctx.signal, }); } - return cursor.listCursorModels({ env: ctx.env }); + return cursor.listCursorModels({ + env: ctx.env, + abortController: ctx.abortController, + signal: ctx.signal, + }); }, }, codebuddy: { @@ -150,7 +165,12 @@ const DRIVER_REGISTRY = { }); }, async listModels(ctx) { - return codebuddy.listCodebuddyModels({ pathToCodebuddyCode: ctx.binPath, env: ctx.env }); + return codebuddy.listCodebuddyModels({ + pathToCodebuddyCode: ctx.binPath, + env: ctx.env, + abortController: ctx.abortController, + signal: ctx.signal, + }); }, }, opencode: { diff --git a/electron/bridges/aiBridge/sdk/sdkStreamHandlers.cjs b/electron/bridges/aiBridge/sdk/sdkStreamHandlers.cjs index 5b2adec27..ff7b6d8e6 100644 --- a/electron/bridges/aiBridge/sdk/sdkStreamHandlers.cjs +++ b/electron/bridges/aiBridge/sdk/sdkStreamHandlers.cjs @@ -8,6 +8,7 @@ const { buildNetcattySkillsOpenCodePathAllowlist } = require("./netcattySkillsOp const { getToolCliStateDir } = require("../../../cli/discoveryPath.cjs"); const tempDirBridge = require("../../tempDirBridge.cjs"); const { realpathSync } = require("node:fs"); +const { createHash } = require("node:crypto"); const { CodexAppServerRuntime } = require("../codexAppServer/runtime.cjs"); const { probeCodexAppServer } = require("../codexAppServer/probe.cjs"); @@ -20,6 +21,7 @@ const VALID_BACKENDS = new Set(listBackends()); // is far cheaper than spawning a new opencode process on every panel render // (issue #2184). const MODEL_CACHE_TTL_MS = 5 * 60 * 1000; +const MODEL_CACHE_MAX_ENTRIES = 32; const MODEL_LIST_TIMEOUT_MS = 10000; const sdkModelCache = new Map(); const sdkModelInFlight = new Map(); @@ -77,7 +79,40 @@ function buildSdkModelCacheKey(backendKey, binPath, env, runtime = "sdk") { const envFingerprint = SDK_MODEL_CACHE_ENV_KEYS .map((key) => `${key}=${env?.[key] == null ? "" : String(env[key])}`) .join("\u0000"); - return [String(backendKey || ""), String(binPath || ""), String(runtime || "sdk"), envFingerprint].join("\u0000"); + const envHash = createHash("sha256").update(envFingerprint).digest("hex"); + return [String(backendKey || ""), String(binPath || ""), String(runtime || "sdk"), envHash].join("\u0000"); +} + +function pruneSdkModelCache(cache, { + now = Date.now(), + ttlMs = MODEL_CACHE_TTL_MS, + maxEntries = MODEL_CACHE_MAX_ENTRIES, +} = {}) { + for (const [key, entry] of cache) { + if (!entry || now - Number(entry.at || 0) >= ttlMs) cache.delete(key); + } + while (cache.size > maxEntries) { + const oldestKey = cache.keys().next().value; + if (oldestKey === undefined) break; + cache.delete(oldestKey); + } +} + +function getSdkModelCacheEntry(cache, key, options) { + pruneSdkModelCache(cache, options); + const entry = cache.get(key); + if (!entry) return null; + // Map insertion order doubles as the LRU list. + cache.delete(key); + cache.set(key, entry); + return entry; +} + +function setSdkModelCacheEntry(cache, key, entry, options) { + pruneSdkModelCache(cache, options); + cache.delete(key); + cache.set(key, entry); + pruneSdkModelCache(cache, options); } function shouldCacheSdkRuntimeModels(_backendKey) { @@ -708,8 +743,8 @@ function registerSdkStreamHandlers(ctx) { `${codexRuntime}:${cursorAuthMode}`, ); const shouldCacheModels = shouldCacheSdkRuntimeModels(backendKey); - const cached = shouldCacheModels ? sdkModelCache.get(cacheKey) : null; - if (cached && Date.now() - cached.at < MODEL_CACHE_TTL_MS) { + const cached = shouldCacheModels ? getSdkModelCacheEntry(sdkModelCache, cacheKey) : null; + if (cached) { return { ok: true, currentModelId: cached.currentModelId || null, models: cached.models }; } @@ -738,7 +773,7 @@ function registerSdkStreamHandlers(ctx) { // pinning that for TTL would block recovery (matches renderer // sdkRuntimeModelCache behavior). if (shouldCacheModels && (models.length > 0 || currentModelId)) { - sdkModelCache.set(cacheKey, { at: Date.now(), currentModelId, models }); + setSdkModelCacheEntry(sdkModelCache, cacheKey, { at: Date.now(), currentModelId, models }); } return { ok: true, currentModelId, models }; } catch (err) { @@ -888,6 +923,8 @@ module.exports = { resolveSdkBackendBinPath, buildSdkSessionKey, buildSdkModelCacheKey, + getSdkModelCacheEntry, + setSdkModelCacheEntry, normalizeSdkListModelsResult, resolveSdkResumeSessionId, expireSiblingCursorCliModeSessions, diff --git a/electron/bridges/aiBridge/sdk/sdkStreamHandlers.test.cjs b/electron/bridges/aiBridge/sdk/sdkStreamHandlers.test.cjs index f5053f1a1..942e9c0ea 100644 --- a/electron/bridges/aiBridge/sdk/sdkStreamHandlers.test.cjs +++ b/electron/bridges/aiBridge/sdk/sdkStreamHandlers.test.cjs @@ -3,6 +3,8 @@ const assert = require("node:assert/strict"); const { buildSdkTurnPrompt, buildSdkModelCacheKey, + getSdkModelCacheEntry, + setSdkModelCacheEntry, buildSdkSessionKey, normalizeSdkListModelsResult, resolveSdkResumeSessionId, @@ -60,6 +62,31 @@ test("SDK model cache keys include catalog-affecting agent environment", () => { buildSdkModelCacheKey("opencode", "/usr/bin/opencode", { HOME: "/Users/a" }), buildSdkModelCacheKey("opencode", "/usr/bin/opencode", { HOME: "/Users/a" }), ); + assert.doesNotMatch( + buildSdkModelCacheKey("cursor", "/usr/bin/cursor", { CURSOR_API_KEY: "very-secret-key" }), + /very-secret-key/, + ); +}); + +test("SDK model cache removes expired entries instead of retaining tombstones", () => { + const cache = new Map([ + ["expired", { at: 1, currentModelId: null, models: [{ id: "old" }] }], + ["fresh", { at: 95, currentModelId: null, models: [{ id: "new" }] }], + ]); + + assert.equal(getSdkModelCacheEntry(cache, "expired", { now: 100, ttlMs: 10, maxEntries: 8 }), null); + assert.equal(cache.has("expired"), false); + assert.equal(getSdkModelCacheEntry(cache, "fresh", { now: 100, ttlMs: 10, maxEntries: 8 }).models[0].id, "new"); +}); + +test("SDK model cache evicts the least recently used catalog at its hard limit", () => { + const cache = new Map(); + setSdkModelCacheEntry(cache, "a", { at: 1, models: [{ id: "a" }] }, { now: 1, ttlMs: 100, maxEntries: 2 }); + setSdkModelCacheEntry(cache, "b", { at: 2, models: [{ id: "b" }] }, { now: 2, ttlMs: 100, maxEntries: 2 }); + assert.ok(getSdkModelCacheEntry(cache, "a", { now: 3, ttlMs: 100, maxEntries: 2 })); + setSdkModelCacheEntry(cache, "c", { at: 3, models: [{ id: "c" }] }, { now: 3, ttlMs: 100, maxEntries: 2 }); + + assert.deepEqual(Array.from(cache.keys()), ["a", "c"]); }); test("normalizeSdkListModelsResult preserves current model ids from object results", () => { diff --git a/electron/bridges/boundedSftpOpen.cjs b/electron/bridges/boundedSftpOpen.cjs new file mode 100644 index 000000000..d9757ec8e --- /dev/null +++ b/electron/bridges/boundedSftpOpen.cjs @@ -0,0 +1,86 @@ +"use strict"; + +const { invalidateSshTransport } = require("./sshTransportInvalidation.cjs"); + +const DEFAULT_SFTP_CHANNEL_OPEN_TIMEOUT_MS = 10_000; + +function closeSftpChannel(channel) { + if (!channel) return; + try { channel.once?.("error", () => {}); } catch { /* ignore */ } + try { channel.end?.(); } catch { /* ignore */ } + try { channel.close?.(); } catch { /* ignore */ } + try { channel.destroy?.(); } catch { /* ignore */ } +} + +function createSftpOpenAbortError(signal) { + const reason = signal?.reason; + const error = reason instanceof Error ? reason : new Error("SFTP channel open was aborted"); + if (!error.code) error.code = "ABORT_ERR"; + return error; +} + +function openBoundedSftpChannel(sshClient, options = {}) { + if (!sshClient || typeof sshClient.sftp !== "function") { + return Promise.resolve(null); + } + const signal = options.signal || null; + const setTimeoutFn = options.setTimeoutFn || setTimeout; + const clearTimeoutFn = options.clearTimeoutFn || clearTimeout; + const timeoutMs = Math.max( + 1, + Number(options.timeoutMs) || DEFAULT_SFTP_CHANNEL_OPEN_TIMEOUT_MS, + ); + + return new Promise((resolve, reject) => { + let settled = false; + let timer = null; + const cleanup = () => { + if (timer) clearTimeoutFn(timer); + timer = null; + signal?.removeEventListener?.("abort", onAbort); + }; + const finish = (error, channel = null, { invalidateTransport = false } = {}) => { + if (settled) { + closeSftpChannel(channel); + return false; + } + settled = true; + cleanup(); + if (error && channel) closeSftpChannel(channel); + if (invalidateTransport) invalidateSshTransport(sshClient); + if (error) reject(error); + else resolve(channel); + return true; + }; + const onAbort = () => finish(createSftpOpenAbortError(signal), null, { + invalidateTransport: true, + }); + + if (signal?.aborted) { + finish(createSftpOpenAbortError(signal)); + return; + } + signal?.addEventListener?.("abort", onAbort, { once: true }); + timer = setTimeoutFn(() => { + const error = new Error(`SFTP channel open timed out after ${timeoutMs}ms`); + error.code = "SFTP_CHANNEL_OPEN_TIMEOUT"; + finish(error, null, { invalidateTransport: true }); + }, timeoutMs); + timer.unref?.(); + + try { + sshClient.sftp((error, channel) => { + if (error) finish(error); + else finish(null, channel || null); + }); + } catch (error) { + finish(error); + } + }); +} + +module.exports = { + DEFAULT_SFTP_CHANNEL_OPEN_TIMEOUT_MS, + closeSftpChannel, + openBoundedSftpChannel, +}; diff --git a/electron/bridges/boundedSftpOpen.test.cjs b/electron/bridges/boundedSftpOpen.test.cjs new file mode 100644 index 000000000..9e9aafa51 --- /dev/null +++ b/electron/bridges/boundedSftpOpen.test.cjs @@ -0,0 +1,92 @@ +"use strict"; + +const test = require("node:test"); +const assert = require("node:assert/strict"); +const { EventEmitter, getEventListeners } = require("node:events"); +const { openBoundedSftpChannel } = require("./boundedSftpOpen.cjs"); + +function createChannel() { + const channel = new EventEmitter(); + channel.endCalls = 0; + channel.closeCalls = 0; + channel.end = () => { channel.endCalls += 1; }; + channel.close = () => { channel.closeCalls += 1; }; + return channel; +} + +test("bounded SFTP open times out and closes a late channel", async () => { + let callback; + let invalidations = 0; + const sshClient = { + sftp(next) { callback = next; }, + destroy() { invalidations += 1; }, + }; + const result = openBoundedSftpChannel(sshClient, { timeoutMs: 5 }); + await assert.rejects(result, (error) => error.code === "SFTP_CHANNEL_OPEN_TIMEOUT"); + assert.equal(invalidations, 1); + + const channel = createChannel(); + callback(null, channel); + assert.ok(channel.endCalls > 0 || channel.closeCalls > 0); +}); + +test("bounded SFTP open cancellation settles immediately and closes a late channel", async () => { + let callback; + let invalidations = 0; + const controller = new AbortController(); + const sshClient = { + sftp(next) { callback = next; }, + destroy() { invalidations += 1; }, + }; + const result = openBoundedSftpChannel(sshClient, { signal: controller.signal }); + controller.abort(new Error("cancelled")); + await assert.rejects(result, /cancelled/); + assert.equal(invalidations, 1); + + const channel = createChannel(); + callback(null, channel); + assert.ok(channel.endCalls > 0 || channel.closeCalls > 0); +}); + +test("repeated unresponsive SFTP opens cannot retain channel callbacks", async () => { + const sshClient = { + destroyed: false, + pendingChannelCallbacks: [], + sftp(callback) { + if (this.destroyed) throw new Error("Not connected"); + this.pendingChannelCallbacks.push(callback); + }, + end() {}, + destroy() { + this.destroyed = true; + this.pendingChannelCallbacks.length = 0; + }, + }; + + for (let attempt = 0; attempt < 3; attempt += 1) { + await assert.rejects( + openBoundedSftpChannel(sshClient, { timeoutMs: 2 }), + /timed out|Not connected/, + ); + } + assert.equal(sshClient.pendingChannelCallbacks.length, 0); +}); + +test("bounded SFTP open removes cancellation listeners after success and failure", async () => { + for (const outcome of ["success", "error"]) { + const controller = new AbortController(); + let callback; + const sshClient = { sftp(next) { callback = next; } }; + const result = openBoundedSftpChannel(sshClient, { signal: controller.signal }); + if (outcome === "success") callback(null, createChannel()); + else callback(new Error("open failed")); + if (outcome === "success") assert.ok(await result); + else await assert.rejects(result, /open failed/); + assert.equal(getEventListeners(controller.signal, "abort").length, 0); + } +}); + +test("bounded SFTP open converts synchronous setup errors into rejections", async () => { + const sshClient = { sftp() { throw new Error("sync failure"); } }; + await assert.rejects(openBoundedSftpChannel(sshClient), /sync failure/); +}); diff --git a/electron/bridges/boundedSshChannelOpen.cjs b/electron/bridges/boundedSshChannelOpen.cjs new file mode 100644 index 000000000..33624ca0e --- /dev/null +++ b/electron/bridges/boundedSshChannelOpen.cjs @@ -0,0 +1,190 @@ +"use strict"; + +const { invalidateSshTransport } = require("./sshTransportInvalidation.cjs"); + +const DEFAULT_SSH_CHANNEL_OPEN_TIMEOUT_MS = 30_000; + +function closeLateChannel(channel) { + if (!channel || typeof channel === "number") return; + try { channel.once?.("error", () => {}); } catch { /* ignore */ } + try { channel.close?.(); } catch { /* ignore */ } + try { channel.end?.(); } catch { /* ignore */ } + try { channel.destroy?.(); } catch { /* ignore */ } +} + +function channelAbortError(signal, label) { + const reason = signal?.reason; + const error = reason instanceof Error ? reason : new Error(`${label} was cancelled`); + if (!error.code) error.code = "ABORT_ERR"; + return error; +} + +function openBoundedSshChannel(sshClient, invoke, options = {}) { + const label = String(options.label || "SSH channel open"); + const timeoutMs = Math.max( + 1, + Number(options.timeoutMs) || DEFAULT_SSH_CHANNEL_OPEN_TIMEOUT_MS, + ); + const signal = options.signal || null; + const closeLateResult = options.closeLateResult || closeLateChannel; + const timeoutCode = options.timeoutCode || "SSH_CHANNEL_OPEN_TIMEOUT"; + + return new Promise((resolve, reject) => { + let settled = false; + let timer = null; + const cleanup = () => { + if (timer) clearTimeout(timer); + timer = null; + signal?.removeEventListener?.("abort", onAbort); + }; + const finish = (error, result, { invalidate = false } = {}) => { + if (settled) { + if (!error) closeLateResult(result); + return false; + } + settled = true; + cleanup(); + if (error && result) closeLateResult(result); + if (invalidate) invalidateSshTransport(sshClient); + if (error) reject(error); + else resolve(result); + return true; + }; + const onAbort = () => finish(channelAbortError(signal, label), null, { invalidate: true }); + + if (signal?.aborted) { + finish(channelAbortError(signal, label)); + return; + } + signal?.addEventListener?.("abort", onAbort, { once: true }); + timer = setTimeout(() => { + const error = new Error(`${label} timed out after ${timeoutMs} ms`); + error.code = timeoutCode; + finish(error, null, { invalidate: true }); + }, timeoutMs); + timer.unref?.(); + + try { + invoke((error, result) => finish(error, result)); + } catch (error) { + finish(error); + } + }); +} + +function openBoundedSshShell(sshClient, windowOptions, shellOptions, options = {}) { + return openBoundedSshChannel( + sshClient, + (callback) => sshClient.shell(windowOptions, shellOptions, callback), + { + ...options, + label: options.label || "SSH shell channel open", + timeoutCode: "SSH_SHELL_OPEN_TIMEOUT", + }, + ); +} + +function openBoundedForwardOut( + sshClient, + sourceAddress, + sourcePort, + targetAddress, + targetPort, + options = {}, +) { + return openBoundedSshChannel( + sshClient, + (callback) => sshClient.forwardOut( + sourceAddress, + sourcePort, + targetAddress, + targetPort, + callback, + ), + { + ...options, + label: options.label || "SSH forwardOut channel open", + timeoutCode: "SSH_FORWARD_OUT_TIMEOUT", + }, + ); +} + +function openBoundedForwardIn(sshClient, bindAddress, bindPort, options = {}) { + return openBoundedSshChannel( + sshClient, + (callback) => sshClient.forwardIn(bindAddress, bindPort, callback), + { + ...options, + label: options.label || "SSH forwardIn request", + timeoutCode: "SSH_FORWARD_IN_TIMEOUT", + closeLateResult: () => {}, + }, + ); +} + +function deliverChannelOpenCallback(promise, callback) { + void promise.then( + (result) => callback(null, result), + (error) => callback(error), + ); +} + +function openBoundedSshShellCallback( + sshClient, + windowOptions, + shellOptions, + callback, + options = {}, +) { + deliverChannelOpenCallback( + openBoundedSshShell(sshClient, windowOptions, shellOptions, options), + callback, + ); +} + +function openBoundedForwardOutCallback( + sshClient, + sourceAddress, + sourcePort, + targetAddress, + targetPort, + callback, + options = {}, +) { + deliverChannelOpenCallback( + openBoundedForwardOut( + sshClient, + sourceAddress, + sourcePort, + targetAddress, + targetPort, + options, + ), + callback, + ); +} + +function openBoundedForwardInCallback( + sshClient, + bindAddress, + bindPort, + callback, + options = {}, +) { + deliverChannelOpenCallback( + openBoundedForwardIn(sshClient, bindAddress, bindPort, options), + callback, + ); +} + +module.exports = { + DEFAULT_SSH_CHANNEL_OPEN_TIMEOUT_MS, + closeLateChannel, + openBoundedSshChannel, + openBoundedSshShell, + openBoundedSshShellCallback, + openBoundedForwardOut, + openBoundedForwardOutCallback, + openBoundedForwardIn, + openBoundedForwardInCallback, +}; diff --git a/electron/bridges/boundedSshChannelOpen.test.cjs b/electron/bridges/boundedSshChannelOpen.test.cjs new file mode 100644 index 000000000..a3ed18bcc --- /dev/null +++ b/electron/bridges/boundedSshChannelOpen.test.cjs @@ -0,0 +1,56 @@ +"use strict"; + +const test = require("node:test"); +const assert = require("node:assert/strict"); +const { EventEmitter } = require("node:events"); +const { + openBoundedForwardIn, + openBoundedForwardOut, + openBoundedSshShell, +} = require("./boundedSshChannelOpen.cjs"); + +function pendingClient(method) { + const client = new EventEmitter(); + client.pending = []; + client.invalidations = 0; + client[method] = (...args) => client.pending.push(args.at(-1)); + client.end = () => {}; + client.destroy = () => { + client.invalidations += 1; + client.pending.length = 0; + }; + return client; +} + +test("unresponsive shell and forward opens invalidate transport and release pending callbacks", async () => { + for (const [method, open] of [ + ["shell", (client) => openBoundedSshShell(client, {}, {}, { timeoutMs: 2 })], + ["forwardOut", (client) => openBoundedForwardOut(client, "127.0.0.1", 0, "host", 22, { timeoutMs: 2 })], + ["forwardIn", (client) => openBoundedForwardIn(client, "127.0.0.1", 2222, { timeoutMs: 2 })], + ]) { + const client = pendingClient(method); + await assert.rejects(open(client), /timed out/); + assert.equal(client.invalidations, 1, method); + assert.equal(client.pending.length, 0, method); + } +}); + +test("cancelled channel open invalidates transport and a late stream is closed", async () => { + let callback; + let invalidations = 0; + const client = { + shell(_window, _options, next) { callback = next; }, + destroy() { invalidations += 1; }, + }; + const controller = new AbortController(); + const pending = openBoundedSshShell(client, {}, {}, { signal: controller.signal }); + controller.abort(new Error("cancelled")); + await assert.rejects(pending, /cancelled/); + assert.equal(invalidations, 1); + + const stream = new EventEmitter(); + stream.closed = 0; + stream.close = () => { stream.closed += 1; }; + callback(null, stream); + assert.equal(stream.closed, 1); +}); diff --git a/electron/bridges/boundedSshExec.cjs b/electron/bridges/boundedSshExec.cjs new file mode 100644 index 000000000..e9cf4b3b4 --- /dev/null +++ b/electron/bridges/boundedSshExec.cjs @@ -0,0 +1,250 @@ +"use strict"; + +const { StringDecoder } = require("node:string_decoder"); +const { invalidateSshTransport } = require("./sshTransportInvalidation.cjs"); + +const DEFAULT_SSH_EXEC_OPEN_TIMEOUT_MS = 15_000; +const DEFAULT_SSH_EXEC_RUN_TIMEOUT_MS = 10 * 60_000; +const DEFAULT_SSH_EXEC_MAX_OUTPUT_BYTES = 64 * 1024; + +function terminateSshExecStream(stream) { + if (!stream) return; + // Teardown can race with a final transport error after normal listeners have + // been removed. Keep that late event from becoming an uncaught main-process + // exception; the stream is terminal and will be collected with this listener. + try { stream.once?.("error", () => {}); } catch { /* ignore */ } + try { stream.stderr?.once?.("error", () => {}); } catch { /* ignore */ } + try { stream.close?.(); } catch { /* ignore */ } + try { stream.end?.(); } catch { /* ignore */ } + try { stream.destroy?.(); } catch { /* ignore */ } +} + +function createAbortError(signal) { + const reason = signal?.reason; + const error = reason instanceof Error ? reason : new Error("SSH command was aborted"); + if (!error.code) error.code = "ABORT_ERR"; + return error; +} + +function openBoundedSshExecStream( + sshClient, + command, + execOptions = {}, + options = {}, +) { + if (!sshClient || typeof sshClient.exec !== "function") { + return Promise.reject(new Error("SSH exec unavailable")); + } + const signal = options.signal || null; + const setTimeoutFn = options.setTimeoutFn || setTimeout; + const clearTimeoutFn = options.clearTimeoutFn || clearTimeout; + const openingTimeoutMs = Math.max( + 1, + Number(options.openingTimeoutMs) || DEFAULT_SSH_EXEC_OPEN_TIMEOUT_MS, + ); + + return new Promise((resolve, reject) => { + let settled = false; + let openingTimer = null; + const cleanup = () => { + if (openingTimer) clearTimeoutFn(openingTimer); + openingTimer = null; + signal?.removeEventListener?.("abort", onAbort); + }; + const finish = (error, stream, { invalidateTransport = false } = {}) => { + if (settled) { + if (!error) terminateSshExecStream(stream); + return false; + } + settled = true; + cleanup(); + if (error && stream) terminateSshExecStream(stream); + if (invalidateTransport) invalidateSshTransport(sshClient); + if (error) reject(error); + else if (!stream) reject(new Error("Failed to create SSH exec stream")); + else resolve(stream); + return true; + }; + const onAbort = () => finish(createAbortError(signal), null, { + invalidateTransport: true, + }); + + if (signal?.aborted) { + finish(createAbortError(signal)); + return; + } + signal?.addEventListener?.("abort", onAbort, { once: true }); + openingTimer = setTimeoutFn(() => { + const error = new Error(`SSH exec channel open timed out after ${openingTimeoutMs} ms`); + error.code = "SSH_EXEC_OPEN_TIMEOUT"; + finish(error, null, { invalidateTransport: true }); + }, openingTimeoutMs); + openingTimer.unref?.(); + + try { + sshClient.exec(command, execOptions, (error, stream) => finish(error, stream)); + } catch (error) { + finish(error); + } + }); +} + +function openBoundedSshExecStreamCallback( + sshClient, + command, + execOptions, + callback, + options = {}, +) { + void openBoundedSshExecStream(sshClient, command, execOptions, options).then( + (stream) => callback(null, stream), + (error) => callback(error), + ); +} + +function executeBoundedSshCommand(sshClient, command, options = {}) { + if (!sshClient || typeof sshClient.exec !== "function") { + return Promise.reject(new Error("SSH exec unavailable")); + } + const signal = options.signal || null; + const setTimeoutFn = options.setTimeoutFn || setTimeout; + const clearTimeoutFn = options.clearTimeoutFn || clearTimeout; + const openingTimeoutMs = Math.max(1, Number(options.openingTimeoutMs) || DEFAULT_SSH_EXEC_OPEN_TIMEOUT_MS); + const runTimeoutMs = Math.max(1, Number(options.runTimeoutMs) || DEFAULT_SSH_EXEC_RUN_TIMEOUT_MS); + const maxOutputBytes = Math.max(1, Number(options.maxOutputBytes) || DEFAULT_SSH_EXEC_MAX_OUTPUT_BYTES); + + return new Promise((resolve, reject) => { + let settled = false; + let streamRef = null; + let openingTimer = null; + let runTimer = null; + let stdout = ""; + let stderr = ""; + let outputBytes = 0; + const stdoutDecoder = new StringDecoder("utf8"); + const stderrDecoder = new StringDecoder("utf8"); + let decodersEnded = false; + let cleanupStreamListeners = () => {}; + + const cleanup = () => { + if (openingTimer) clearTimeoutFn(openingTimer); + if (runTimer) clearTimeoutFn(runTimer); + openingTimer = null; + runTimer = null; + cleanupStreamListeners(); + cleanupStreamListeners = () => {}; + signal?.removeEventListener?.("abort", onAbort); + }; + const finish = (error, code = null, { terminate = false, invalidateTransport = false } = {}) => { + if (settled) return false; + settled = true; + cleanup(); + if (terminate) terminateSshExecStream(streamRef); + if (invalidateTransport) invalidateSshTransport(sshClient); + if (error) reject(error); + else { + if (!decodersEnded) { + decodersEnded = true; + stdout += stdoutDecoder.end(); + stderr += stderrDecoder.end(); + } + resolve({ stdout, stderr, code }); + } + return true; + }; + const onAbort = () => finish(createAbortError(signal), null, { + terminate: true, + // Before the callback arrives ssh2 owns an uncancellable channel-open + // request. Closing the physical transport is the only public cleanup. + invalidateTransport: !streamRef, + }); + const append = (target, chunk) => { + if (settled) return; + const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk)); + const remaining = Math.max(0, maxOutputBytes - outputBytes); + if (remaining > 0) { + const accepted = buffer.length <= remaining ? buffer : buffer.subarray(0, remaining); + if (target === "stdout") stdout += stdoutDecoder.write(accepted); + else stderr += stderrDecoder.write(accepted); + outputBytes += accepted.length; + } + if (buffer.length > remaining) { + const error = new Error(`SSH command output exceeded ${maxOutputBytes} bytes`); + error.code = "SSH_EXEC_OUTPUT_LIMIT"; + finish(error, null, { terminate: true }); + } + }; + + if (signal?.aborted) { + finish(createAbortError(signal)); + return; + } + signal?.addEventListener?.("abort", onAbort, { once: true }); + openingTimer = setTimeoutFn(() => { + const error = new Error(`SSH exec channel open timed out after ${openingTimeoutMs} ms`); + error.code = "SSH_EXEC_OPEN_TIMEOUT"; + finish(error, null, { terminate: true, invalidateTransport: true }); + }, openingTimeoutMs); + openingTimer.unref?.(); + + try { + sshClient.exec(command, (error, stream) => { + if (openingTimer) clearTimeoutFn(openingTimer); + openingTimer = null; + if (settled) { + terminateSshExecStream(stream); + return; + } + if (error || !stream) { + if (stream) terminateSshExecStream(stream); + finish(error || new Error("Failed to create SSH exec stream")); + return; + } + streamRef = stream; + const onStdout = (chunk) => append("stdout", chunk); + const onStderr = (chunk) => append("stderr", chunk); + const onClose = (code) => finish(null, code); + const onError = (streamError) => finish( + streamError instanceof Error ? streamError : new Error(String(streamError || "SSH exec failed")), + null, + { terminate: true }, + ); + cleanupStreamListeners = () => { + stream.removeListener?.("data", onStdout); + stream.removeListener?.("close", onClose); + stream.removeListener?.("error", onError); + stream.stderr?.removeListener?.("data", onStderr); + stream.stderr?.removeListener?.("error", onError); + }; + stream.on("data", onStdout); + stream.on("close", onClose); + stream.on("error", onError); + stream.stderr?.on?.("data", onStderr); + stream.stderr?.on?.("error", onError); + runTimer = setTimeoutFn(() => { + const timeoutError = new Error(`SSH command execution timed out after ${runTimeoutMs} ms`); + timeoutError.code = "SSH_EXEC_RUN_TIMEOUT"; + finish(timeoutError, null, { terminate: true }); + }, runTimeoutMs); + runTimer.unref?.(); + try { options.onStream?.(stream); } catch (streamError) { + finish(streamError, null, { terminate: true }); + return; + } + if (signal?.aborted) onAbort(); + }); + } catch (error) { + finish(error, null, { terminate: true }); + } + }); +} + +module.exports = { + DEFAULT_SSH_EXEC_OPEN_TIMEOUT_MS, + DEFAULT_SSH_EXEC_RUN_TIMEOUT_MS, + DEFAULT_SSH_EXEC_MAX_OUTPUT_BYTES, + executeBoundedSshCommand, + openBoundedSshExecStream, + openBoundedSshExecStreamCallback, + terminateSshExecStream, +}; diff --git a/electron/bridges/boundedSshExec.test.cjs b/electron/bridges/boundedSshExec.test.cjs new file mode 100644 index 000000000..79659f584 --- /dev/null +++ b/electron/bridges/boundedSshExec.test.cjs @@ -0,0 +1,179 @@ +"use strict"; + +const test = require("node:test"); +const assert = require("node:assert/strict"); +const { EventEmitter } = require("node:events"); +const { + executeBoundedSshCommand, + openBoundedSshExecStream, +} = require("./boundedSshExec.cjs"); + +function createStream() { + const stream = new EventEmitter(); + stream.stderr = new EventEmitter(); + stream.closed = 0; + stream.destroyed = 0; + stream.close = () => { stream.closed += 1; }; + stream.destroy = () => { stream.destroyed += 1; }; + return stream; +} + +test("bounded SSH exec times out while opening and terminates a late stream", async () => { + let callback; + let endCalls = 0; + let destroyCalls = 0; + const sshClient = { + exec(_command, next) { callback = next; }, + end() { endCalls += 1; }, + destroy() { destroyCalls += 1; }, + }; + const result = executeBoundedSshCommand(sshClient, "true", { openingTimeoutMs: 5 }); + await assert.rejects(result, (error) => error.code === "SSH_EXEC_OPEN_TIMEOUT"); + assert.ok(endCalls > 0 || destroyCalls > 0, "open timeout must invalidate the physical transport"); + + const stream = createStream(); + callback(null, stream); + assert.ok(stream.closed > 0 || stream.destroyed > 0); +}); + +test("bounded raw exec stream preserves options and invalidates a hung open", async () => { + let callback; + let receivedOptions; + let invalidations = 0; + const sshClient = { + exec(_command, options, next) { + receivedOptions = options; + callback = next; + }, + destroy() { invalidations += 1; }, + }; + + const pending = openBoundedSshExecStream( + sshClient, + "sudo sftp-server", + { pty: false }, + { openingTimeoutMs: 2 }, + ); + await assert.rejects(pending, (error) => error.code === "SSH_EXEC_OPEN_TIMEOUT"); + assert.deepEqual(receivedOptions, { pty: false }); + assert.equal(invalidations, 1); + + const lateStream = createStream(); + callback(null, lateStream); + assert.ok(lateStream.closed > 0 || lateStream.destroyed > 0); +}); + +test("bounded SSH exec times out a live command and removes data listeners", async () => { + const stream = createStream(); + const sshClient = { exec(_command, next) { next(null, stream); } }; + await assert.rejects( + executeBoundedSshCommand(sshClient, "sleep", { runTimeoutMs: 5 }), + (error) => error.code === "SSH_EXEC_RUN_TIMEOUT", + ); + assert.equal(stream.listenerCount("data"), 0); + assert.equal(stream.stderr.listenerCount("data"), 0); + assert.ok(stream.closed > 0 || stream.destroyed > 0); +}); + +test("bounded SSH exec caps combined stdout and stderr and terminates the stream", async () => { + const stream = createStream(); + const sshClient = { exec(_command, next) { next(null, stream); } }; + const result = executeBoundedSshCommand(sshClient, "flood", { maxOutputBytes: 16 }); + stream.emit("data", Buffer.alloc(10, 97)); + stream.stderr.emit("data", Buffer.alloc(10, 98)); + await assert.rejects(result, (error) => error.code === "SSH_EXEC_OUTPUT_LIMIT"); + assert.equal(stream.listenerCount("data"), 0); + assert.equal(stream.stderr.listenerCount("data"), 0); + assert.ok(stream.closed > 0 || stream.destroyed > 0); +}); + +test("bounded SSH exec settles on stdout and stderr stream errors", async () => { + for (const target of ["stdout", "stderr"]) { + const stream = createStream(); + const sshClient = { exec(_command, next) { next(null, stream); } }; + const result = executeBoundedSshCommand(sshClient, "fail"); + const expected = new Error(`${target} failed`); + if (target === "stdout") stream.emit("error", expected); + else stream.stderr.emit("error", expected); + await assert.rejects(result, /failed/); + assert.equal(stream.listenerCount("data"), 0); + assert.equal(stream.stderr.listenerCount("data"), 0); + } +}); + +test("bounded SSH exec aborts before callback and terminates a late stream", async () => { + let callback; + let invalidations = 0; + const controller = new AbortController(); + const sshClient = { + exec(_command, next) { callback = next; }, + destroy() { invalidations += 1; }, + }; + const result = executeBoundedSshCommand(sshClient, "pending", { signal: controller.signal }); + controller.abort(new Error("cancelled")); + await assert.rejects(result, /cancelled/); + assert.equal(invalidations, 1); + + const stream = createStream(); + callback(null, stream); + assert.ok(stream.closed > 0 || stream.destroyed > 0); +}); + +test("repeated unresponsive exec opens release channel callbacks and evict the shared transport", async () => { + const { + borrowTransport, + createTransport, + findTransportByEndpoint, + getTransportStats, + resetSshTransportRegistryForTests, + } = require("./sshConnectionPool.cjs"); + resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 }); + const endpoint = { hostId: "host-1", hostname: "wedged.example", username: "root" }; + const conn = new EventEmitter(); + conn._sock = { destroyed: false }; + conn.pendingChannelCallbacks = []; + conn.exec = (_command, callback) => { + if (conn._sock.destroyed) throw new Error("Not connected"); + conn.pendingChannelCallbacks.push(callback); + }; + conn.end = () => {}; + conn.destroy = () => { + if (conn._sock.destroyed) return; + conn._sock.destroyed = true; + conn.pendingChannelCallbacks.length = 0; + conn.emit("close"); + }; + const transport = createTransport({ conn, endpoint }); + borrowTransport(transport, { kind: "shell", holder: {} }); + + for (let attempt = 0; attempt < 3; attempt += 1) { + await assert.rejects( + executeBoundedSshCommand(conn, "stats", { openingTimeoutMs: 2 }), + /timed out|Not connected/, + ); + } + + assert.equal(conn.pendingChannelCallbacks.length, 0); + assert.equal(getTransportStats().transports, 0); + assert.equal(findTransportByEndpoint(endpoint), null); + + const replacement = new EventEmitter(); + replacement._sock = { destroyed: false }; + replacement.end = () => {}; + const replacementTransport = createTransport({ conn: replacement, endpoint }); + assert.equal(findTransportByEndpoint(endpoint), replacementTransport, "a fresh reconnect must be reusable"); + resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 }); +}); + +test("bounded SSH exec preserves UTF-8 split across stream chunks", async () => { + const stream = createStream(); + const sshClient = { exec(_command, next) { next(null, stream); } }; + const result = executeBoundedSshCommand(sshClient, "printf unicode"); + const bytes = Buffer.from("你🙂", "utf8"); + stream.emit("data", bytes.subarray(0, 2)); + stream.emit("data", bytes.subarray(2, 5)); + stream.emit("data", bytes.subarray(5)); + stream.emit("close", 0); + + assert.deepEqual(await result, { stdout: "你🙂", stderr: "", code: 0 }); +}); diff --git a/electron/bridges/compressUploadBridge.cjs b/electron/bridges/compressUploadBridge.cjs index fad1d252d..f7d03b5aa 100644 --- a/electron/bridges/compressUploadBridge.cjs +++ b/electron/bridges/compressUploadBridge.cjs @@ -6,8 +6,11 @@ const fs = require("node:fs"); const path = require("node:path"); +const { createHash } = require("node:crypto"); const { spawn } = require("node:child_process"); +const { StringDecoder } = require("node:string_decoder"); const { getTempFilePath } = require("./tempDirBridge.cjs"); +const { invalidateSshTransport } = require("./sshTransportInvalidation.cjs"); /** * Escape shell arguments to prevent injection attacks @@ -18,6 +21,80 @@ function escapeShellArg(arg) { return "'" + arg.replace(/'/g, "'\\''") + "'"; } +function buildAtomicRemoteExtractionCommand({ + compressionId, + archivePath, + targetDir, + folderName, +}) { + const normalizedFolderName = path.posix.basename(String(folderName || "").replace(/\\/g, "/")); + if (!normalizedFolderName || normalizedFolderName === "." || normalizedFolderName === "..") { + throw new Error("Invalid compressed upload folder name"); + } + // Recovery artifacts are stable for one destination, not one attempt. A + // failed post-commit cleanup is therefore reconciled by the next upload + // instead of leaving one new backup per compression id. + const suffix = createHash("sha256") + .update(`${targetDir}\0${normalizedFolderName}`) + .digest("hex") + .slice(0, 16); + const stageDir = path.posix.join(targetDir, `.netcatty-compress-${suffix}.stage`); + const backupDir = path.posix.join(targetDir, `.netcatty-compress-${suffix}.backup`); + const finalDir = path.posix.join(targetDir, normalizedFolderName); + const stagedFinalDir = path.posix.join(stageDir, normalizedFolderName); + + return [ + "set -e", + `archive=${escapeShellArg(archivePath)}`, + `stage=${escapeShellArg(stageDir)}`, + `backup=${escapeShellArg(backupDir)}`, + `final=${escapeShellArg(finalDir)}`, + `staged_final=${escapeShellArg(stagedFinalDir)}`, + "rollback_compressed_upload() {", + " status=$1", + " trap - EXIT HUP INT TERM", + " if [ ! -e \"$final\" ] && [ ! -L \"$final\" ] && { [ -e \"$backup\" ] || [ -L \"$backup\" ]; }; then", + " mv -- \"$backup\" \"$final\" 2>/dev/null || true", + " fi", + " make_tree_writable \"$stage\"", + " rm -rf -- \"$stage\"", + " rm -f -- \"$archive\"", + " exit \"$status\"", + "}", + "make_tree_writable() {", + " candidate=$1", + " if [ -d \"$candidate\" ] && [ ! -L \"$candidate\" ]; then chmod -R u+w -- \"$candidate\" 2>/dev/null || true; fi", + "}", + "trap 'rollback_compressed_upload $?' EXIT", + "trap 'rollback_compressed_upload 129' HUP", + "trap 'rollback_compressed_upload 130' INT", + "trap 'rollback_compressed_upload 143' TERM", + // Recover a prior interrupted promotion before touching either artifact. + "if [ ! -e \"$final\" ] && [ ! -L \"$final\" ] && { [ -e \"$backup\" ] || [ -L \"$backup\" ]; }; then mv -- \"$backup\" \"$final\"; fi", + "if [ -L \"$final\" ]; then echo 'Refusing to replace a symlink target' >&2; exit 1; fi", + "if [ -e \"$backup\" ] || [ -L \"$backup\" ]; then make_tree_writable \"$backup\"; rm -rf -- \"$backup\"; fi", + "make_tree_writable \"$stage\"", + "rm -rf -- \"$stage\"", + "mkdir -p -- \"$stage\"", + // Preserve merge semantics without exposing the live destination to tar. + "if [ -e \"$final\" ]; then", + " if ! cp -a -- \"$final\" \"$staged_final\" 2>/dev/null; then", + " rm -rf -- \"$staged_final\"", + " cp -Rp -- \"$final\" \"$staged_final\"", + " fi", + "fi", + "tar -xzf \"$archive\" -C \"$stage\" --exclude='._*' --exclude='.DS_Store'", + "if [ ! -d \"$staged_final\" ]; then echo 'Compressed archive did not contain the expected folder' >&2; exit 1; fi", + "if [ -e \"$final\" ]; then mv -- \"$final\" \"$backup\"; fi", + "mv -- \"$staged_final\" \"$final\"", + "make_tree_writable \"$backup\"", + "make_tree_writable \"$stage\"", + "rm -rf -- \"$backup\" \"$stage\"", + "rm -f -- \"$archive\"", + "trap - EXIT HUP INT TERM", + ].join("\n"); +} + // Shared references let sftpClients = null; let transferBridge = null; @@ -25,6 +102,182 @@ let transferBridge = null; // Active compress operations const activeCompressions = new Map(); const workerCompressionLifecycleEpochs = new Map(); +const compressionSupportCache = new Map(); +const compressionTargetLocks = new Map(); +const COMPRESSION_SUPPORT_CACHE_TTL_MS = 10_000; +const MAX_COMPRESSION_SUPPORT_CACHE_ENTRIES = 64; +const REMOTE_TAR_PROBE_TIMEOUT_MS = 15_000; +const REMOTE_CLEANUP_TIMEOUT_MS = 15_000; +const LOCAL_TAR_PROBE_TIMEOUT_MS = 10_000; +const LOCAL_TAR_KILL_GRACE_MS = 750; +const MAX_REMOTE_EXEC_STDERR_BYTES = 64 * 1024; +const MAX_LOCAL_TAR_STDERR_BYTES = 64 * 1024; + +async function runWithCompressionTargetLock(targetKey, signal, operation) { + const predecessor = compressionTargetLocks.get(targetKey) || Promise.resolve(); + let release; + const ownGate = new Promise((resolve) => { release = resolve; }); + const ownTail = predecessor.catch(() => {}).then(() => ownGate); + compressionTargetLocks.set(targetKey, ownTail); + + let removeAbortListener = () => {}; + try { + if (signal) { + const aborted = new Promise((_, reject) => { + const onAbort = () => reject( + signal.reason instanceof Error ? signal.reason : new Error("Upload cancelled"), + ); + if (signal.aborted) onAbort(); + else { + signal.addEventListener("abort", onAbort, { once: true }); + removeAbortListener = () => signal.removeEventListener("abort", onAbort); + } + }); + await Promise.race([predecessor.catch(() => {}), aborted]); + } else { + await predecessor.catch(() => {}); + } + if (signal?.aborted) { + throw signal.reason instanceof Error ? signal.reason : new Error("Upload cancelled"); + } + return await operation(); + } finally { + removeAbortListener(); + release(); + void ownTail.finally(() => { + if (compressionTargetLocks.get(targetKey) === ownTail) { + compressionTargetLocks.delete(targetKey); + } + }); + } +} + +function buildCompressionTargetKey(connectionIdentity, targetPath, folderName) { + if (!connectionIdentity) { + throw new Error("SFTP connection identity unavailable for compressed upload"); + } + const normalizedFolderName = path.posix.basename(String(folderName || "").replace(/\\/g, "/")); + if (!normalizedFolderName || normalizedFolderName === "." || normalizedFolderName === "..") { + throw new Error("Invalid compressed upload folder name"); + } + const normalizedTarget = path.posix.normalize(path.posix.join( + String(targetPath || ".").replace(/\\/g, "/"), + normalizedFolderName, + )); + return `${connectionIdentity}\0${normalizedTarget}`; +} + +function resolveCompressionTargetKey(sftpId, targetPath, folderName) { + const client = sftpClients?.get?.(sftpId); + const connectionIdentity = client?.__netcattyEndpointKey + || client?.__netcattyRefHolder?.connRef?.endpointKey; + return buildCompressionTargetKey(connectionIdentity, targetPath, folderName); +} + +function createBoundedUtf8Collector(maxBytes) { + const limit = Math.max(1, Number(maxBytes) || 1); + const decoder = new StringDecoder("utf8"); + let text = ""; + let bytes = 0; + let truncated = false; + let ended = false; + return { + append(chunk) { + if (ended) return; + const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk)); + const remaining = Math.max(0, limit - bytes); + const accepted = buffer.length <= remaining ? buffer : buffer.subarray(0, remaining); + if (accepted.length > 0) text += decoder.write(accepted); + bytes += accepted.length; + if (accepted.length < buffer.length) truncated = true; + }, + end() { + if (ended) return text; + ended = true; + // If the byte cap cut through a code point, discard the held prefix + // instead of turning it into a replacement character in diagnostics. + if (!truncated || decoder.lastNeed === 0) text += decoder.end(); + return text; + }, + value() { + return text; + }, + }; +} + +function terminateRemoteExecStream(stream) { + if (!stream) return; + try { stream.once?.('error', () => {}); } catch { /* ignore */ } + try { stream.stderr?.once?.('error', () => {}); } catch { /* ignore */ } + try { stream.close?.(); } catch { /* ignore */ } + try { stream.end?.(); } catch { /* ignore */ } + try { stream.destroy?.(); } catch { /* ignore */ } +} + +function runRemoteExec(sshClient, command, options = {}) { + const timeoutMs = Math.max(1, Number(options.timeoutMs) || REMOTE_CLEANUP_TIMEOUT_MS); + const signal = options.signal; + return new Promise((resolve, reject) => { + let stream = null; + let settled = false; + const stderr = createBoundedUtf8Collector(MAX_REMOTE_EXEC_STDERR_BYTES); + let hasOutput = false; + + const cleanup = () => { + clearTimeout(timeout); + signal?.removeEventListener?.('abort', onAbort); + }; + const finish = (error, result) => { + if (settled) return; + settled = true; + cleanup(); + if (error) { + terminateRemoteExecStream(stream); + reject(error); + } else { + resolve(result); + } + }; + const onAbort = () => { + finish(signal?.reason instanceof Error ? signal.reason : new Error('Remote command cancelled')); + if (!stream) invalidateSshTransport(sshClient); + }; + const timeout = setTimeout(() => { + finish(new Error(`Remote command timed out after ${timeoutMs / 1000} seconds`)); + if (!stream) invalidateSshTransport(sshClient); + }, timeoutMs); + if (typeof timeout.unref === 'function') timeout.unref(); + + if (signal?.aborted) { + onAbort(); + return; + } + signal?.addEventListener?.('abort', onAbort, { once: true }); + + try { + sshClient.exec(command, (error, nextStream) => { + if (settled) { + terminateRemoteExecStream(nextStream); + return; + } + if (error) { + finish(error); + return; + } + stream = nextStream; + stream.on('data', () => { hasOutput = true; }); + stream.stderr?.on?.('data', (data) => { + stderr.append(data); + }); + stream.stderr?.once?.('error', (streamError) => finish(streamError)); + stream.once('close', (code) => finish(null, { code, hasOutput, stderr: stderr.end() })); + stream.once('error', (streamError) => finish(streamError)); + }); + } catch (error) { + finish(error); + } + }); +} function broadcastCompressionEvent(payload) { if (!payload?.transferId) return; @@ -35,6 +288,66 @@ function broadcastCompressionEvent(payload) { } } +function settleCompressionTerminal(compression, type, payload = {}) { + if (!compression || compression.terminalState) return false; + compression.terminalState = type; + broadcastCompressionEvent({ + type, + transferId: compression.compressionId, + endedAt: Date.now(), + ...payload, + }); + return true; +} + +function getCachedCompressionSupport(sftpId) { + const entry = compressionSupportCache.get(sftpId); + if (!entry) return null; + if (entry.expiresAt <= Date.now()) { + compressionSupportCache.delete(sftpId); + return null; + } + compressionSupportCache.delete(sftpId); + compressionSupportCache.set(sftpId, entry); + return entry.value; +} + +function cacheCompressionSupport(sftpId, value) { + compressionSupportCache.delete(sftpId); + compressionSupportCache.set(sftpId, { + value, + expiresAt: Date.now() + COMPRESSION_SUPPORT_CACHE_TTL_MS, + }); + while (compressionSupportCache.size > MAX_COMPRESSION_SUPPORT_CACHE_ENTRIES) { + compressionSupportCache.delete(compressionSupportCache.keys().next().value); + } + return value; +} + +async function resolveCompressedUploadSupport(sftpId, signal) { + const cached = getCachedCompressionSupport(sftpId); + if (cached) return cached; + const localTar = await checkTarAvailable(signal); + if (signal?.aborted) throw signal.reason instanceof Error ? signal.reason : new Error('Upload cancelled'); + const remoteTar = localTar ? await checkRemoteTarAvailable(sftpId, signal) : false; + if (signal?.aborted) throw signal.reason instanceof Error ? signal.reason : new Error('Upload cancelled'); + return cacheCompressionSupport(sftpId, { + supported: localTar && remoteTar, + localTar, + remoteTar, + }); +} + +function buildRemoteArchivePath(targetPath, folderName, compressionId) { + const base = path.posix.basename(String(folderName || 'folder').replace(/\\/g, '/')) + .replace(/[^A-Za-z0-9._-]/g, '_') || 'folder'; + const suffix = createHash('sha256') + .update(String(compressionId || 'compression')) + .digest('hex') + .slice(0, 16); + return path.posix.join(targetPath, `.${base}.netcatty-${suffix}.tar.gz`); +} + function waitWhilePaused(compression) { if (!compression.paused || compression.cancelled) return Promise.resolve(); broadcastCompressionEvent({ @@ -53,6 +366,28 @@ function releasePausedCompression(compression) { for (const resolve of waiters) resolve(); } +function clearCompressionProcess(compression, processHandle) { + if (compression.process !== processHandle) return; + compression.process = null; + if (compression.processKillTimer) { + clearTimeout(compression.processKillTimer); + compression.processKillTimer = null; + } +} + +function terminateCompressionProcess(compression) { + const processHandle = compression?.process; + if (!processHandle) return; + try { processHandle.kill('SIGTERM'); } catch { /* already exited */ } + if (compression.processKillTimer) clearTimeout(compression.processKillTimer); + compression.processKillTimer = setTimeout(() => { + compression.processKillTimer = null; + if (compression.process !== processHandle) return; + try { processHandle.kill('SIGKILL'); } catch { /* already exited */ } + }, LOCAL_TAR_KILL_GRACE_MS); + compression.processKillTimer.unref?.(); +} + /** * Initialize the compress upload bridge with dependencies */ @@ -64,14 +399,49 @@ function init(deps) { /** * Check if tar command is available on the system */ -async function checkTarAvailable() { +async function checkTarAvailable(signal) { return new Promise((resolve) => { const tar = spawn('tar', ['--version'], { stdio: 'ignore' }); + let settled = false; + let childClosed = false; + let forceKillTimer = null; + const finish = (available) => { + if (settled) return; + settled = true; + clearTimeout(timeout); + signal?.removeEventListener?.('abort', onAbort); + resolve(available); + }; + const stop = () => { + try { tar.kill('SIGTERM'); } catch { /* ignore */ } + if (!childClosed) { + forceKillTimer = setTimeout(() => { + forceKillTimer = null; + if (!childClosed) { + try { tar.kill('SIGKILL'); } catch { /* ignore */ } + } + }, LOCAL_TAR_KILL_GRACE_MS); + forceKillTimer.unref?.(); + } + finish(false); + }; + const onAbort = () => stop(); + const timeout = setTimeout(stop, LOCAL_TAR_PROBE_TIMEOUT_MS); + if (typeof timeout.unref === 'function') timeout.unref(); + if (signal?.aborted) { + stop(); + return; + } + signal?.addEventListener?.('abort', onAbort, { once: true }); tar.on('close', (code) => { - resolve(code === 0); + childClosed = true; + if (forceKillTimer) clearTimeout(forceKillTimer); + finish(code === 0); }); tar.on('error', () => { - resolve(false); + childClosed = true; + if (forceKillTimer) clearTimeout(forceKillTimer); + finish(false); }); }); } @@ -79,7 +449,7 @@ async function checkTarAvailable() { /** * Check if tar command is available on remote server */ -async function checkRemoteTarAvailable(sftpId) { +async function checkRemoteTarAvailable(sftpId, signal) { try { const client = sftpClients.get(sftpId); if (!client) throw new Error("SFTP session not found"); @@ -88,27 +458,11 @@ async function checkRemoteTarAvailable(sftpId) { const sshClient = client.client; // Get underlying SSH2 client if (!sshClient) throw new Error("SSH client not available"); - return new Promise((resolve) => { - sshClient.exec('tar --version', (err, stream) => { - if (err) { - resolve(false); - return; - } - - let hasOutput = false; - stream.on('data', () => { - hasOutput = true; - }); - - stream.on('close', (code) => { - resolve(code === 0 && hasOutput); - }); - - stream.on('error', () => { - resolve(false); - }); - }); + const result = await runRemoteExec(sshClient, 'tar --version', { + timeoutMs: REMOTE_TAR_PROBE_TIMEOUT_MS, + signal, }); + return result.code === 0 && result.hasOutput; } catch { return false; } @@ -146,7 +500,7 @@ async function compressFolder(folderPath, outputPath, compressionId, sendProgres }); compression.process = tar; - let stderr = ''; + const stderr = createBoundedUtf8Collector(MAX_LOCAL_TAR_STDERR_BYTES); // Monitor progress by checking output file size periodically const progressInterval = setInterval(async () => { @@ -165,11 +519,12 @@ async function compressFolder(folderPath, outputPath, compressionId, sendProgres }, 500); tar.stderr.on('data', (data) => { - stderr += data.toString(); + stderr.append(data); }); tar.on('close', (code) => { clearInterval(progressInterval); + clearCompressionProcess(compression, tar); if (compression.cancelled) { // Clean up output file if cancelled @@ -181,12 +536,13 @@ async function compressFolder(folderPath, outputPath, compressionId, sendProgres if (code === 0) { resolve(); } else { - reject(new Error(`Tar compression failed: ${stderr}`)); + reject(new Error(`Tar compression failed: ${stderr.end()}`)); } }); tar.on('error', (err) => { clearInterval(progressInterval); + clearCompressionProcess(compression, tar); reject(new Error(`Failed to start tar: ${err.message}`)); }); }); @@ -199,7 +555,15 @@ async function compressFolder(folderPath, outputPath, compressionId, sendProgres * @param {string} targetDir - Target directory for extraction * @param {number} [archiveSize] - Size of the archive in bytes (optional, for timeout calculation) */ -async function extractRemoteArchive(sftpId, archivePath, targetDir, archiveSize) { +async function extractRemoteArchive( + sftpId, + archivePath, + targetDir, + folderName, + compressionId, + archiveSize, + signal, +) { const client = sftpClients.get(sftpId); if (!client) throw new Error("SFTP session not found"); @@ -215,75 +579,29 @@ async function extractRemoteArchive(sftpId, archivePath, targetDir, archiveSize) const sizeBasedTimeout = archiveSize ? Math.ceil(archiveSize / (10 * 1024 * 1024)) * 30000 : 0; const extractionTimeout = Math.min(maxTimeout, Math.max(baseTimeout, baseTimeout + sizeBasedTimeout)); - return new Promise((resolve, reject) => { - // Create target directory, extract, then always clean up the archive - // Use && for tar success, then always try cleanup regardless of tar result - // Also exclude any ._* files that might have been included despite our compression exclusions - // Properly escape shell arguments to prevent injection attacks - const escapedTargetDir = escapeShellArg(targetDir); - const escapedArchivePath = escapeShellArg(archivePath); - const command = `mkdir -p ${escapedTargetDir} && cd ${escapedTargetDir} && tar -xzf ${escapedArchivePath} --exclude='._*' --exclude='.DS_Store' && rm -f ${escapedArchivePath} || (rm -f ${escapedArchivePath}; exit 1)`; - - sshClient.exec(command, (err, stream) => { - if (err) { - reject(new Error(`Failed to execute extraction command: ${err.message}`)); - return; - } - - let stderr = ''; - let resolved = false; - - stream.on('data', () => { - // stdout not needed, just consume the data - }); - - stream.stderr.on('data', (data) => { - stderr += data.toString(); - }); - - stream.on('close', (code) => { - if (resolved) return; - resolved = true; - - clearTimeout(timeout); - - // The command uses `;` and `||` so cleanup should always run - // We only care about the tar extraction success (first part of command) - // The rm commands are just cleanup and their failure doesn't matter - - // For most cases, code 0 means success - // If code is not 0, check if it's just cleanup failure - if (code === 0) { - resolve(); - } else { - // Check if the error is from tar extraction or just cleanup - // If stderr contains tar errors, it's a real extraction failure - if (stderr.includes('tar:') || stderr.includes('gzip:') || stderr.includes('Cannot open:') || stderr.includes('not found in archive')) { - reject(new Error(`Remote extraction failed: ${stderr || 'Tar extraction error'}`)); - } else { - // Likely just cleanup failure - consider it successful if no tar-specific errors - resolve(); - } - } - }); - - stream.on('error', (err) => { - if (resolved) return; - resolved = true; - - clearTimeout(timeout); - reject(new Error(`Stream error: ${err.message}`)); - }); - - // Add timeout to prevent hanging (uses dynamic timeout based on archive size) - const timeout = setTimeout(() => { - if (resolved) return; - resolved = true; - - reject(new Error(`Remote extraction timed out after ${extractionTimeout / 1000} seconds`)); - }, extractionTimeout); - }); + // Extract into a sibling staging directory, then atomically swap the complete + // folder into place. Existing directory contents are copied into the stage so + // compressed upload keeps its historical merge semantics. + const command = buildAtomicRemoteExtractionCommand({ + compressionId, + archivePath, + targetDir, + folderName, }); + let result; + try { + result = await runRemoteExec(sshClient, command, { + timeoutMs: extractionTimeout, + signal, + }); + } catch (error) { + if (/timed out/i.test(error?.message || '')) { + throw new Error(`Remote extraction timed out after ${extractionTimeout / 1000} seconds`); + } + throw new Error(`Remote extraction failed: ${error?.message || String(error)}`); + } + if (result.code === 0) return; + throw new Error(`Remote extraction failed: ${result.stderr || `exit code ${result.code}`}`); } /** @@ -298,18 +616,23 @@ async function startCompressedUpload(event, payload) { folderName, totalBytes = 0, } = payload; + if (activeCompressions.has(compressionId)) { + throw new Error(`Compressed upload is already active: ${compressionId}`); + } const sender = event.sender; - // Register compression for cancellation const compression = { compressionId, cancelled: false, process: null, + processKillTimer: null, phase: 'preparing', paused: false, lifecycleEpoch: Math.max(0, Number(payload.lifecycleEpoch) || 0), lifecycleState: 'transferring', + terminalState: null, resumeWaiters: [], + remoteExecAbortController: new AbortController(), }; activeCompressions.set(compressionId, compression); @@ -333,10 +656,10 @@ async function startCompressedUpload(event, payload) { if (compression.cancelled) return; const ratio = total > 0 ? Math.max(0, Math.min(1, transferred / total)) : 0; const transferredBytes = Math.floor(ratio * totalBytes); - sender.send("netcatty:compress:progress", { - compressionId, - phase, - transferred, + sender.send("netcatty:compress:progress", { + compressionId, + phase, + transferred, total, transferredBytes, totalBytes, @@ -359,52 +682,64 @@ async function startCompressedUpload(event, payload) { }; const sendComplete = () => { + if (compression.terminalState) return; // Send final 100% progress before completion if (!compression.cancelled) { - sender.send("netcatty:compress:progress", { - compressionId, - phase: 'extracting', - transferred: 100, - total: 100 - }); + sendProgress('extracting', 100, 100); } - activeCompressions.delete(compressionId); - sender.send("netcatty:compress:complete", { compressionId }); - broadcastCompressionEvent({ - type: 'completed', - transferId: compressionId, + if (!settleCompressionTerminal(compression, 'completed', { transferred: totalBytes, totalBytes, - endedAt: Date.now(), - }); + })) return; + sender.send("netcatty:compress:complete", { compressionId }); }; const sendError = (error) => { - activeCompressions.delete(compressionId); - sender.send("netcatty:compress:error", { - compressionId, - error: error.message || String(error) - }); - broadcastCompressionEvent({ - type: 'failed', - transferId: compressionId, + if (!settleCompressionTerminal(compression, 'failed', { + error: error.message || String(error), + })) return; + sender.send("netcatty:compress:error", { + compressionId, error: error.message || String(error), - endedAt: Date.now(), }); }; + const sendCancelled = () => { + if (!settleCompressionTerminal(compression, 'cancelled')) return; + sender.send("netcatty:compress:cancelled", { compressionId }); + }; + // Declare tempArchivePath in outer scope for cleanup access let tempArchivePath = null; + const lifecycleLeaseId = `compress-lifecycle:${compressionId}`; + let lifecycleLeasedSftpIds = []; try { - // Check if tar is available locally and remotely - const localTarAvailable = await checkTarAvailable(); - if (!localTarAvailable) { + if ( + !transferBridge?.acquireTransferSessionLeases + || !transferBridge?.releaseTransferSessionLeases + ) { + throw new Error("Transfer runtime session leasing is unavailable"); + } + // The archive upload has its own short-lived transfer lease, but probe, + // compression, extraction and cleanup also use this SFTP/SSH handle. Hold + // one outer lease so closing the browse tab soft-closes instead of tearing + // down the connection halfway through the operation. + lifecycleLeasedSftpIds = transferBridge.acquireTransferSessionLeases( + lifecycleLeaseId, + { targetSftpId: sftpId }, + ); + + // Reuse the short-lived result produced by the renderer's support check. + // A cache miss still performs one bounded, cancellable probe here. + const support = await resolveCompressedUploadSupport( + sftpId, + compression.remoteExecAbortController.signal, + ); + if (!support.localTar) { throw new Error("tar command not available on local system. Please install tar."); } - - const remoteTarAvailable = await checkRemoteTarAvailable(sftpId); - if (!remoteTarAvailable) { + if (!support.remoteTar) { throw new Error("tar command not available on remote server. Please install tar on the remote system."); } @@ -440,7 +775,7 @@ async function startCompressedUpload(event, payload) { await waitWhilePaused(compression); sendProgress('uploading', 30, 100); - const remoteArchivePath = `${targetPath}/${folderName}.tar.gz`; + const remoteArchivePath = buildRemoteArchivePath(targetPath, folderName, compressionId); // Use existing transfer bridge for upload with progress const transferId = `compress-${compressionId}`; @@ -454,7 +789,7 @@ async function startCompressedUpload(event, payload) { }; // Start the transfer with progress callback - await transferBridge.startTransfer(event, { + const uploadResult = await transferBridge.startInternalTransfer(event, { transferId, sourcePath: tempArchivePath, targetPath: remoteArchivePath, @@ -465,6 +800,9 @@ async function startCompressedUpload(event, payload) { resumable: true, checkpointBytes: compression.checkpointBytes || 0, }, onUploadProgress); + if (uploadResult?.error) { + throw new Error(uploadResult.error); + } if (compression.cancelled) { await fs.promises.unlink(tempArchivePath).catch(() => {}); @@ -479,7 +817,19 @@ async function startCompressedUpload(event, payload) { await waitWhilePaused(compression); sendProgress('extracting', 90, 100); - await extractRemoteArchive(sftpId, remoteArchivePath, targetPath, compressedSize); + await runWithCompressionTargetLock( + resolveCompressionTargetKey(sftpId, targetPath, folderName), + compression.remoteExecAbortController.signal, + () => extractRemoteArchive( + sftpId, + remoteArchivePath, + targetPath, + folderName, + compressionId, + compressedSize, + compression.remoteExecAbortController.signal, + ), + ); // Extraction is an atomic remote step. A pause requested during extraction // takes effect here, before completion is published. @@ -488,58 +838,21 @@ async function startCompressedUpload(event, payload) { // Update progress to 95% after extraction sendProgress('extracting', 95, 100); - // Perform cleanup operations asynchronously without blocking completion - // Note: These cleanup operations are best-effort; if the SFTP session closes before - // cleanup completes, errors will be silently ignored - setImmediate(async () => { - // Additional cleanup: remove any ._* files that might have been extracted - try { - const client = sftpClients.get(sftpId); - // Check both that client exists and connection is still open - if (client && client.client && client.client.writable !== false) { - const cleanupCommand = `find ${escapeShellArg(targetPath)} -name "._*" -type f -delete 2>/dev/null || true`; - client.client.exec(cleanupCommand, (err, stream) => { - if (err) { - // Silently ignore - session may have closed - return; - } - - stream.on('close', () => { - // Cleanup completed - }); - - stream.on('error', () => { - // Silently ignore cleanup errors - }); - }); - } - } catch { - // Silently ignore cleanup errors + // Keep the outer SFTP lease until best-effort archive cleanup settles. + // The extraction command already excludes macOS AppleDouble files. Never + // run a parent-directory-wide find/delete here: that can remove unrelated, + // pre-existing files whose names happen to begin with "._". + try { + const client = sftpClients.get(sftpId); + if (client && client.client && client.client.writable !== false) { + await runRemoteExec(client.client, `rm -f ${escapeShellArg(remoteArchivePath)}`, { + timeoutMs: REMOTE_CLEANUP_TIMEOUT_MS, + signal: compression.remoteExecAbortController.signal, + }); } - - // Additional cleanup attempt - ensure remote archive is removed - try { - const client = sftpClients.get(sftpId); - if (client && client.client && client.client.writable !== false) { - client.client.exec(`rm -f ${escapeShellArg(remoteArchivePath)}`, (err, stream) => { - if (err) { - // Silently ignore - session may have closed - return; - } - - stream.on('close', () => { - // Cleanup completed - }); - - stream.on('error', () => { - // Silently ignore cleanup errors - }); - }); - } - } catch { - // Silently ignore cleanup errors - } - }); + } catch { + // Cleanup is best-effort; staging/promotion already committed atomically. + } // Clean up local temp file try { @@ -550,8 +863,7 @@ async function startCompressedUpload(event, payload) { // Check if cancelled during extraction before reporting completion if (compression.cancelled) { - sender.send("netcatty:compress:cancelled", { compressionId }); - broadcastCompressionEvent({ type: 'cancelled', transferId: compressionId, endedAt: Date.now() }); + sendCancelled(); return { compressionId, cancelled: true }; } @@ -569,16 +881,26 @@ async function startCompressedUpload(event, payload) { } if (err.message === 'Upload cancelled' || err.message === 'Compression cancelled' || err.message === 'Transfer cancelled') { - activeCompressions.delete(compressionId); - sender.send("netcatty:compress:cancelled", { compressionId }); - broadcastCompressionEvent({ type: 'cancelled', transferId: compressionId, endedAt: Date.now() }); + sendCancelled(); } else { sendError(err.message || 'Unknown error occurred'); } return { compressionId, error: err.message }; } finally { // Always clean up the active compression entry - activeCompressions.delete(compressionId); + try { + compression.remoteExecAbortController.abort(new Error('Compression finished')); + } catch { /* ignore */ } + if (lifecycleLeasedSftpIds.length > 0) { + transferBridge.releaseTransferSessionLeases( + lifecycleLeaseId, + lifecycleLeasedSftpIds, + ); + lifecycleLeasedSftpIds = []; + } + if (activeCompressions.get(compressionId) === compression) { + activeCompressions.delete(compressionId); + } } } @@ -589,18 +911,16 @@ async function cancelCompression(event, payload) { const { compressionId } = payload; const compression = activeCompressions.get(compressionId); - if (compression) { + if (compression && !compression.terminalState) { compression.cancelled = true; + settleCompressionTerminal(compression, 'cancelled'); + event?.sender?.send?.("netcatty:compress:cancelled", { compressionId }); releasePausedCompression(compression); + try { + compression.remoteExecAbortController?.abort(new Error('Upload cancelled')); + } catch { /* ignore */ } - // Kill the tar process if running - if (compression.process) { - try { - compression.process.kill('SIGTERM'); - } catch { - // Ignore errors when killing process - } - } + terminateCompressionProcess(compression); // Cancel the associated transfer if it's running const transferId = `compress-${compressionId}`; @@ -613,9 +933,9 @@ async function cancelCompression(event, payload) { } } - broadcastCompressionEvent({ type: 'cancelled', transferId: compressionId, endedAt: Date.now() }); - - return { success: true }; + return compression + ? { success: true } + : { success: false, reason: 'Compression is not active' }; } async function pauseCompression(event, payload) { @@ -704,14 +1024,7 @@ async function checkCompressedUploadSupport(event, payload) { const { sftpId } = payload; try { - const localSupport = await checkTarAvailable(); - const remoteSupport = await checkRemoteTarAvailable(sftpId); - - return { - supported: localSupport && remoteSupport, - localTar: localSupport, - remoteTar: remoteSupport - }; + return await resolveCompressedUploadSupport(sftpId); } catch (err) { return { supported: false, @@ -738,20 +1051,36 @@ function registerHandlers(ipcMain, options = {}) { webContentsId: event?.sender?.id, }); const nextEpoch = (compressionId, suggestedEpoch) => { - const current = Math.max(0, Number(workerCompressionLifecycleEpochs.get(compressionId)) || 0); + const entry = workerCompressionLifecycleEpochs.get(compressionId); + const current = Math.max(0, Number(entry?.epoch) || 0); const suggested = Number(suggestedEpoch); const next = Number.isFinite(suggested) && suggested > current ? suggested : current + 1; - workerCompressionLifecycleEpochs.set(compressionId, next); + if (entry) entry.epoch = next; return next; }; ipcMain.handle("netcatty:compress:start", (event, payload) => { + let lifecycleEntry = null; if (payload?.compressionId) { - workerCompressionLifecycleEpochs.set( - payload.compressionId, - Math.max(0, Number(payload.lifecycleEpoch) || 0), - ); + lifecycleEntry = { + epoch: Math.max(0, Number(payload.lifecycleEpoch) || 0), + }; + workerCompressionLifecycleEpochs.set(payload.compressionId, lifecycleEntry); + } + const releaseLifecycleEntry = () => { + if ( + payload?.compressionId + && workerCompressionLifecycleEpochs.get(payload.compressionId) === lifecycleEntry + ) { + workerCompressionLifecycleEpochs.delete(payload.compressionId); + } + }; + try { + return Promise.resolve(workerRequest(event, "netcatty:compress:start", payload)) + .finally(releaseLifecycleEntry); + } catch (error) { + releaseLifecycleEntry(); + throw error; } - return workerRequest(event, "netcatty:compress:start", payload); }); ipcMain.handle("netcatty:compress:pause", async (event, payload) => { const lifecycleEpoch = nextEpoch(payload?.compressionId); @@ -823,4 +1152,15 @@ module.exports = { registerHandlers, pauseCompression, resumeCompression, + _runRemoteExecForTests: runRemoteExec, + _buildAtomicRemoteExtractionCommandForTests: buildAtomicRemoteExtractionCommand, + _buildRemoteArchivePathForTests: buildRemoteArchivePath, + _createBoundedUtf8CollectorForTests: createBoundedUtf8Collector, + _getActiveCompressionCountForTests: () => activeCompressions.size, + _getWorkerCompressionLifecycleEpochCountForTests: () => workerCompressionLifecycleEpochs.size, + _resetCompressionSupportCacheForTests: () => compressionSupportCache.clear(), + _runWithCompressionTargetLockForTests: runWithCompressionTargetLock, + _buildCompressionTargetKeyForTests: buildCompressionTargetKey, + _resolveCompressionTargetKeyForTests: resolveCompressionTargetKey, + _getCompressionTargetLockCountForTests: () => compressionTargetLocks.size, }; diff --git a/electron/bridges/compressUploadBridge.lifecycle.test.cjs b/electron/bridges/compressUploadBridge.lifecycle.test.cjs index 9d3d0fdcc..b1ce186b9 100644 --- a/electron/bridges/compressUploadBridge.lifecycle.test.cjs +++ b/electron/bridges/compressUploadBridge.lifecycle.test.cjs @@ -1,16 +1,105 @@ "use strict"; const assert = require("node:assert/strict"); +const fs = require("node:fs"); +const os = require("node:os"); +const path = require("node:path"); +const { EventEmitter } = require("node:events"); const test = require("node:test"); const compressUploadBridge = require("./compressUploadBridge.cjs"); +test("compressed commits to the same remote folder are serialized", async () => { + let active = 0; + let maxActive = 0; + const order = []; + const run = compressUploadBridge._runWithCompressionTargetLockForTests; + const first = run("sftp-1:/target/folder", null, async () => { + active += 1; + maxActive = Math.max(maxActive, active); + order.push("first-start"); + await new Promise((resolve) => setTimeout(resolve, 10)); + order.push("first-end"); + active -= 1; + }); + const second = run("sftp-1:/target/folder", null, async () => { + active += 1; + maxActive = Math.max(maxActive, active); + order.push("second-start"); + active -= 1; + }); + await Promise.all([first, second]); + assert.equal(maxActive, 1); + assert.deepEqual(order, ["first-start", "first-end", "second-start"]); + assert.equal(compressUploadBridge._getCompressionTargetLockCountForTests(), 0); +}); + +test("compressed commits through different SFTP channels share the endpoint target lock", async () => { + compressUploadBridge.init({ + sftpClients: new Map([ + ["window-a-sftp", { __netcattyEndpointKey: "host|route|auth" }], + ["window-b-sftp", { __netcattyEndpointKey: "host|route|auth" }], + ]), + transferBridge: {}, + }); + const resolveKey = compressUploadBridge._resolveCompressionTargetKeyForTests; + const firstKey = resolveKey("window-a-sftp", "/target/./nested/..", "folder"); + const secondKey = resolveKey("window-b-sftp", "/target", "folder"); + assert.equal(firstKey, secondKey); + + let active = 0; + let maxActive = 0; + let releaseFirst; + const run = compressUploadBridge._runWithCompressionTargetLockForTests; + const first = run(firstKey, null, async () => { + active += 1; + maxActive = Math.max(maxActive, active); + await new Promise((resolve) => { releaseFirst = resolve; }); + active -= 1; + }); + await new Promise((resolve) => setImmediate(resolve)); + const second = run(secondKey, null, async () => { + active += 1; + maxActive = Math.max(maxActive, active); + active -= 1; + }); + await new Promise((resolve) => setImmediate(resolve)); + assert.equal(maxActive, 1); + releaseFirst(); + await Promise.all([first, second]); + assert.equal(maxActive, 1); +}); + +test("an aborted lock waiter cannot let a later compressed commit bypass the active owner", async () => { + const run = compressUploadBridge._runWithCompressionTargetLockForTests; + let releaseFirst; + let thirdStarted = false; + const first = run("sftp-1:/target/abort", null, () => new Promise((resolve) => { + releaseFirst = resolve; + })); + await new Promise((resolve) => setImmediate(resolve)); + const controller = new AbortController(); + const second = run("sftp-1:/target/abort", controller.signal, async () => {}); + controller.abort(new Error("cancelled waiter")); + await assert.rejects(second, /cancelled waiter/); + const third = run("sftp-1:/target/abort", null, async () => { thirdStarted = true; }); + await new Promise((resolve) => setImmediate(resolve)); + assert.equal(thirdStarted, false); + releaseFirst(); + await Promise.all([first, third]); + assert.equal(thirdStarted, true); +}); + test("worker-backed compressed pause and resume publish ordered lifecycle", async () => { const handlers = new Map(); const events = []; + let finishStart; + const startGate = new Promise((resolve) => { finishStart = resolve; }); compressUploadBridge.init({ sftpClients: new Map(), transferBridge: { + acquireTransferSessionLeases(_leaseId, payload) { return [payload.targetSftpId]; }, + releaseTransferSessionLeases() {}, broadcastGlobalTransferEvent(event) { events.push(event); }, }, }); @@ -19,6 +108,7 @@ test("worker-backed compressed pause and resume publish ordered lifecycle", asyn }, { terminalWorkerManager: { async request(channel) { + if (channel === "netcatty:compress:start") return startGate; if (channel === "netcatty:compress:pause") { return { success: true, deferred: false }; } @@ -27,6 +117,10 @@ test("worker-backed compressed pause and resume publish ordered lifecycle", asyn }, }); + const starting = handlers.get("netcatty:compress:start")( + { sender: { id: 1 } }, + { compressionId: "compressed-worker" }, + ); await handlers.get("netcatty:compress:pause")( { sender: { id: 1 } }, { compressionId: "compressed-worker" }, @@ -56,4 +150,383 @@ test("worker-backed compressed pause and resume publish ordered lifecycle", asyn lifecycleState: "transferring", }, ]); + finishStart({ compressionId: "compressed-worker", success: true }); + await starting; + assert.equal(compressUploadBridge._getWorkerCompressionLifecycleEpochCountForTests(), 0); +}); + +test("worker compression lifecycle epochs survive a reused id and clear after the latest start settles", async () => { + const handlers = new Map(); + let finishFirst; + let finishSecond; + const firstGate = new Promise((resolve) => { finishFirst = resolve; }); + const secondGate = new Promise((resolve) => { finishSecond = resolve; }); + let startCalls = 0; + compressUploadBridge.registerHandlers({ + handle(channel, handler) { handlers.set(channel, handler); }, + }, { + terminalWorkerManager: { + request(channel) { + if (channel === "netcatty:compress:start") { + startCalls += 1; + return startCalls === 1 ? firstGate : secondGate; + } + return Promise.resolve({ success: true }); + }, + }, + }); + + const firstStarting = handlers.get("netcatty:compress:start")( + { sender: { id: 1 } }, + { compressionId: "compression-cache-lifetime" }, + ); + const secondStarting = handlers.get("netcatty:compress:start")( + { sender: { id: 1 } }, + { compressionId: "compression-cache-lifetime" }, + ); + assert.equal(compressUploadBridge._getWorkerCompressionLifecycleEpochCountForTests(), 1); + + finishFirst({ compressionId: "compression-cache-lifetime", success: true }); + await firstStarting; + assert.equal( + compressUploadBridge._getWorkerCompressionLifecycleEpochCountForTests(), + 1, + "an older completion must not clear the newer start's lifecycle state", + ); + + finishSecond({ compressionId: "compression-cache-lifetime", success: true }); + await secondStarting; + assert.equal(compressUploadBridge._getWorkerCompressionLifecycleEpochCountForTests(), 0); +}); + +test("worker compression lifecycle epoch clears after a synchronous start failure", () => { + const handlers = new Map(); + compressUploadBridge.registerHandlers({ + handle(channel, handler) { handlers.set(channel, handler); }, + }, { + terminalWorkerManager: { + request(channel) { + if (channel === "netcatty:compress:start") throw new Error("worker unavailable"); + return Promise.resolve({ success: true }); + }, + }, + }); + + assert.throws(() => handlers.get("netcatty:compress:start")( + { sender: { id: 1 } }, + { compressionId: "compression-cache-sync-failure" }, + ), /worker unavailable/); + assert.equal(compressUploadBridge._getWorkerCompressionLifecycleEpochCountForTests(), 0); +}); + +test("cancelling while the remote tar probe is pending publishes cancelled", async () => { + const handlers = new Map(); + const events = []; + let remoteProbeStarted; + const remoteProbeGate = new Promise((resolve) => { remoteProbeStarted = resolve; }); + const remoteStream = new (require("node:events").EventEmitter)(); + remoteStream.stderr = new (require("node:events").EventEmitter)(); + remoteStream.close = () => {}; + remoteStream.end = () => {}; + remoteStream.destroy = () => {}; + const sender = { id: 1, send() {} }; + + compressUploadBridge.init({ + sftpClients: new Map([["sftp-probe", { + client: { + exec(_command, callback) { + callback(null, remoteStream); + remoteProbeStarted(); + }, + }, + }]]), + transferBridge: { + acquireTransferSessionLeases(_leaseId, payload) { return [payload.targetSftpId]; }, + releaseTransferSessionLeases() {}, + broadcastGlobalTransferEvent(event) { events.push(event); }, + }, + }); + compressUploadBridge.registerHandlers({ + handle(channel, handler) { handlers.set(channel, handler); }, + }); + + const starting = handlers.get("netcatty:compress:start")({ sender }, { + compressionId: "cancel-during-probe", + folderPath: "/unused", + targetPath: "/unused", + sftpId: "sftp-probe", + folderName: "unused", + totalBytes: 1, + }); + await remoteProbeGate; + await handlers.get("netcatty:compress:cancel")({ sender }, { + compressionId: "cancel-during-probe", + }); + await starting; + + assert.equal(events.at(-1)?.type, "cancelled"); + assert.equal(events.some((event) => event.type === "failed"), false); +}); + +test("compressed upload holds its SFTP session until remote extraction settles", async (t) => { + const handlers = new Map(); + const calls = []; + const root = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-compress-lease-")); + const folderPath = path.join(root, "folder"); + fs.mkdirSync(folderPath); + fs.writeFileSync(path.join(folderPath, "file.txt"), "payload"); + t.after(() => fs.rmSync(root, { recursive: true, force: true })); + + let extractionStarted; + const extractionGate = new Promise((resolve) => { extractionStarted = resolve; }); + let cleanupStarted; + const cleanupGate = new Promise((resolve) => { cleanupStarted = resolve; }); + const streams = []; + const cleanupStreams = []; + let extractionStream = null; + const makeStream = () => { + const stream = new EventEmitter(); + stream.stderr = new EventEmitter(); + stream.close = () => stream.emit("close", 1); + stream.end = () => {}; + stream.destroy = () => {}; + streams.push(stream); + return stream; + }; + const sshClient = { + exec(command, callback) { + const stream = makeStream(); + callback(null, stream); + if (command === "tar --version") { + process.nextTick(() => { + stream.emit("data", Buffer.from("tar")); + stream.emit("close", 0); + }); + } else if (command.includes("tar -xzf")) { + extractionStream = stream; + extractionStarted(); + } else { + cleanupStreams.push(stream); + cleanupStarted(); + } + }, + }; + + compressUploadBridge.init({ + sftpClients: new Map([["sftp-compressed", { + client: sshClient, + __netcattyEndpointKey: "endpoint:compressed", + }]]), + transferBridge: { + acquireTransferSessionLeases(leaseId, payload) { + calls.push(`acquire:${leaseId}:${payload.targetSftpId}`); + return [payload.targetSftpId]; + }, + releaseTransferSessionLeases(leaseId, sftpIds) { + calls.push(`release:${leaseId}:${sftpIds.join(",")}`); + }, + async startInternalTransfer() { + calls.push("upload"); + return { success: true }; + }, + async cancelTransfer() { return { success: true }; }, + broadcastGlobalTransferEvent() {}, + }, + }); + compressUploadBridge.registerHandlers({ + handle(channel, handler) { handlers.set(channel, handler); }, + }); + + const sender = { id: 1, send() {} }; + const starting = handlers.get("netcatty:compress:start")({ sender }, { + compressionId: "compressed-lifecycle", + folderPath, + targetPath: "/tmp", + sftpId: "sftp-compressed", + folderName: "folder", + totalBytes: 7, + }); + let startSettled = false; + void starting.then(() => { startSettled = true; }, () => { startSettled = true; }); + await extractionGate; + + assert.deepEqual(calls, [ + "acquire:compress-lifecycle:compressed-lifecycle:sftp-compressed", + "upload", + ]); + + extractionStream.emit("close", 0); + await cleanupGate; + assert.deepEqual( + calls, + [ + "acquire:compress-lifecycle:compressed-lifecycle:sftp-compressed", + "upload", + ], + "the outer SFTP lease must remain held while remote cleanup is still running", + ); + await new Promise((resolve) => setImmediate(resolve)); + assert.equal(startSettled, false, "compressed upload must await remote cleanup before releasing its lease"); + for (const stream of cleanupStreams) stream.emit("close", 0); + await starting; + + assert.deepEqual(calls, [ + "acquire:compress-lifecycle:compressed-lifecycle:sftp-compressed", + "upload", + "release:compress-lifecycle:compressed-lifecycle:sftp-compressed", + ]); +}); + +test("compressed upload never deletes AppleDouble files across the target parent", async (t) => { + const remoteCommands = []; + const harness = createSuccessfulCompressionHarness(t, { + onRemoteCommand(command) { remoteCommands.push(command); }, + }); + + const result = await harness.handlers.get("netcatty:compress:start")({ sender: harness.sender }, { + compressionId: "no-parent-cleanup", + folderPath: harness.folderPath, + targetPath: "/srv/shared", + sftpId: "sftp-harness", + folderName: "folder", + totalBytes: 7, + }); + + assert.equal(result.success, true); + assert.equal( + remoteCommands.some((command) => /\bfind\b[\s\S]*\._\*/.test(command)), + false, + "cleanup must not scan and delete pre-existing ._* files under the whole target parent", + ); + assert.equal( + remoteCommands.some((command) => /^rm -f /.test(command)), + true, + "the uploaded archive still receives a bounded best-effort cleanup", + ); +}); + +function createSuccessfulCompressionHarness(t, { onRemoteCommand, onUpload } = {}) { + const handlers = new Map(); + const events = []; + const root = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-compress-harness-")); + const folderPath = path.join(root, "folder"); + fs.mkdirSync(folderPath); + fs.writeFileSync(path.join(folderPath, "file.txt"), "payload"); + t.after(() => fs.rmSync(root, { recursive: true, force: true })); + const sshClient = { + writable: true, + exec(command, callback) { + onRemoteCommand?.(command); + const stream = new EventEmitter(); + stream.stderr = new EventEmitter(); + stream.close = () => {}; + stream.end = () => {}; + stream.destroy = () => {}; + callback(null, stream); + process.nextTick(() => { + if (command === "tar --version") stream.emit("data", Buffer.from("tar")); + stream.emit("close", 0); + }); + }, + }; + compressUploadBridge._resetCompressionSupportCacheForTests?.(); + compressUploadBridge.init({ + sftpClients: new Map([["sftp-harness", { + client: sshClient, + __netcattyEndpointKey: "endpoint:harness", + }]]), + transferBridge: { + acquireTransferSessionLeases(_leaseId, payload) { return [payload.targetSftpId]; }, + releaseTransferSessionLeases() {}, + broadcastGlobalTransferEvent(event) { events.push(event); }, + async startInternalTransfer(_event, payload) { + onUpload?.(payload); + return { success: true }; + }, + async cancelTransfer() { return { success: true }; }, + }, + }); + compressUploadBridge.registerHandlers({ handle(channel, handler) { handlers.set(channel, handler); } }); + return { handlers, events, folderPath, sender: { id: 1, send() {} } }; +} + +test("support preflight is reused by the immediately following compressed start", async (t) => { + let remoteProbeCalls = 0; + const harness = createSuccessfulCompressionHarness(t, { + onRemoteCommand(command) { + if (command === "tar --version") remoteProbeCalls += 1; + }, + }); + const support = await harness.handlers.get("netcatty:compress:checkSupport")( + { sender: harness.sender }, + { sftpId: "sftp-harness" }, + ); + assert.equal(support.supported, true); + + const result = await harness.handlers.get("netcatty:compress:start")({ sender: harness.sender }, { + compressionId: "cached-support-start", + folderPath: harness.folderPath, + targetPath: "/tmp", + sftpId: "sftp-harness", + folderName: "folder", + totalBytes: 7, + }); + + assert.equal(result.success, true); + assert.equal(remoteProbeCalls, 1); +}); + +test("remote archive paths are unique for concurrent operation identities", async (t) => { + const uploadedPaths = []; + const harness = createSuccessfulCompressionHarness(t, { + onUpload(payload) { uploadedPaths.push(payload.targetPath); }, + }); + + await Promise.all([ + harness.handlers.get("netcatty:compress:start")({ sender: harness.sender }, { + compressionId: "archive-one", + folderPath: harness.folderPath, + targetPath: "/tmp", + sftpId: "sftp-harness", + folderName: "folder", + totalBytes: 7, + }), + harness.handlers.get("netcatty:compress:start")({ sender: harness.sender }, { + compressionId: "archive-two", + folderPath: harness.folderPath, + targetPath: "/tmp", + sftpId: "sftp-harness", + folderName: "folder", + totalBytes: 7, + }), + ]); + + assert.equal(uploadedPaths.length, 2); + assert.notEqual(uploadedPaths[0], uploadedPaths[1]); +}); + +test("a late cancel after completion cannot overwrite the completed terminal event", async (t) => { + const harness = createSuccessfulCompressionHarness(t); + await harness.handlers.get("netcatty:compress:start")({ sender: harness.sender }, { + compressionId: "completed-before-cancel", + folderPath: harness.folderPath, + targetPath: "/tmp", + sftpId: "sftp-harness", + folderName: "folder", + totalBytes: 7, + }); + const terminalBefore = harness.events.filter((event) => ( + event.transferId === "completed-before-cancel" + && ["completed", "cancelled", "failed"].includes(event.type) + )); + assert.deepEqual(terminalBefore.map((event) => event.type), ["completed"]); + + await harness.handlers.get("netcatty:compress:cancel")({ sender: harness.sender }, { + compressionId: "completed-before-cancel", + }); + const terminalAfter = harness.events.filter((event) => ( + event.transferId === "completed-before-cancel" + && ["completed", "cancelled", "failed"].includes(event.type) + )); + assert.deepEqual(terminalAfter.map((event) => event.type), ["completed"]); }); diff --git a/electron/bridges/compressUploadBridge.remoteExec.test.cjs b/electron/bridges/compressUploadBridge.remoteExec.test.cjs new file mode 100644 index 000000000..69c792831 --- /dev/null +++ b/electron/bridges/compressUploadBridge.remoteExec.test.cjs @@ -0,0 +1,356 @@ +const assert = require("node:assert/strict"); +const { execFileSync, spawnSync } = require("node:child_process"); +const { EventEmitter } = require("node:events"); +const fs = require("node:fs"); +const Module = require("node:module"); +const os = require("node:os"); +const path = require("node:path"); +const test = require("node:test"); + +const { + _createBoundedUtf8CollectorForTests: createBoundedUtf8Collector, + _buildAtomicRemoteExtractionCommandForTests: buildAtomicRemoteExtractionCommand, + _runRemoteExecForTests: runRemoteExec, +} = require("./compressUploadBridge.cjs"); + +function createHungStream() { + const stream = new EventEmitter(); + stream.stderr = new EventEmitter(); + stream.closedByTest = false; + stream.close = () => { stream.closedByTest = true; }; + stream.end = () => { stream.closedByTest = true; }; + stream.destroy = () => { stream.closedByTest = true; }; + return stream; +} + +test("remote exec timeout closes a hung SSH channel", async () => { + const stream = createHungStream(); + const sshClient = { exec: (_command, callback) => callback(null, stream) }; + + await assert.rejects( + runRemoteExec(sshClient, "tar --version", { timeoutMs: 5 }), + /timed out/i, + ); + assert.equal(stream.closedByTest, true); +}); + +test("remote exec cancellation closes a hung SSH channel", async () => { + const stream = createHungStream(); + const sshClient = { exec: (_command, callback) => callback(null, stream) }; + const controller = new AbortController(); + const running = runRemoteExec(sshClient, "tar --version", { + timeoutMs: 1_000, + signal: controller.signal, + }); + + controller.abort(new Error("Upload cancelled")); + await assert.rejects(running, /cancel/i); + assert.equal(stream.closedByTest, true); +}); + +test("a late SSH exec callback after timeout closes its stream", async () => { + let callback; + let invalidations = 0; + const sshClient = { + exec: (_command, next) => { callback = next; }, + destroy: () => { invalidations += 1; }, + }; + const running = runRemoteExec(sshClient, "tar --version", { timeoutMs: 5 }); + await assert.rejects(running, /timed out/i); + assert.equal(invalidations, 1); + + const stream = createHungStream(); + callback(null, stream); + assert.equal(stream.closedByTest, true); +}); + +test("local tar diagnostics stay bounded for very large stderr output", () => { + const collector = createBoundedUtf8Collector(64 * 1_024); + for (let index = 0; index < 100; index += 1) { + collector.append("x".repeat(4_096)); + } + const value = collector.end(); + assert.equal(value.length, 64 * 1_024); +}); + +test("remote exec preserves split UTF-8 stderr", async () => { + const stream = createHungStream(); + const sshClient = { exec: (_command, callback) => callback(null, stream) }; + const running = runRemoteExec(sshClient, "failing command", { timeoutMs: 1_000 }); + const bytes = Buffer.from("中文错误", "utf8"); + stream.stderr.emit("data", bytes.subarray(0, 2)); + stream.stderr.emit("data", bytes.subarray(2)); + stream.emit("close", 1); + + assert.deepEqual(await running, { code: 1, hasOutput: false, stderr: "中文错误" }); +}); + +test("bounded UTF-8 diagnostics omit a character cut by the byte cap", () => { + const collector = createBoundedUtf8Collector(2); + collector.append(Buffer.from("中", "utf8")); + const value = collector.end(); + assert.equal(value, ""); + assert.doesNotMatch(value, /�/u); +}); + +test("a timed-out local tar probe escalates from TERM to KILL", async (t) => { + const bridgePath = require.resolve("./compressUploadBridge.cjs"); + const originalLoad = Module._load; + const originalSetTimeout = globalThis.setTimeout; + const signals = []; + const tar = new EventEmitter(); + tar.kill = (signal) => { + signals.push(signal); + return true; + }; + + Module._load = function load(request, parent, isMain) { + if (request === "node:child_process") { + return { spawn: () => tar }; + } + return originalLoad.call(this, request, parent, isMain); + }; + delete require.cache[bridgePath]; + const isolatedBridge = require("./compressUploadBridge.cjs"); + Module._load = originalLoad; + t.after(() => { + Module._load = originalLoad; + globalThis.setTimeout = originalSetTimeout; + delete require.cache[bridgePath]; + }); + + // Keep the behavioral timeout test fast without exposing timer knobs in the + // production API: only the two known probe deadlines are shortened. + globalThis.setTimeout = (callback, delay, ...args) => originalSetTimeout( + callback, + delay === 10_000 || delay === 750 ? 5 : delay, + ...args, + ); + const handlers = new Map(); + isolatedBridge.init({ + sftpClients: new Map(), + transferBridge: { + acquireTransferSessionLeases(_leaseId, payload) { return [payload.targetSftpId]; }, + releaseTransferSessionLeases() {}, + broadcastGlobalTransferEvent() {}, + }, + }); + isolatedBridge.registerHandlers({ + handle(channel, handler) { handlers.set(channel, handler); }, + }); + await handlers.get("netcatty:compress:start")({ sender: { id: 1, send() {} } }, { + compressionId: "hung-local-probe", + folderPath: "/unused", + targetPath: "/unused", + sftpId: "unused", + folderName: "unused", + totalBytes: 1, + }); + await new Promise((resolve) => originalSetTimeout(resolve, 15)); + + assert.deepEqual(signals, ["SIGTERM", "SIGKILL"]); +}); + +test("cancelling the real local compressor escalates TERM to KILL and releases its SFTP lease", async (t) => { + const bridgePath = require.resolve("./compressUploadBridge.cjs"); + const originalLoad = Module._load; + const originalSetTimeout = globalThis.setTimeout; + const signals = []; + let spawnCount = 0; + let compressor; + const makeChild = (probe) => { + const child = new EventEmitter(); + child.stdout = new EventEmitter(); + child.stderr = new EventEmitter(); + child.kill = (signal) => { + signals.push(signal); + if (!probe && signal === "SIGKILL") process.nextTick(() => child.emit("close", null)); + return true; + }; + if (probe) process.nextTick(() => child.emit("close", 0)); + else compressor = child; + return child; + }; + + Module._load = function load(request, parent, isMain) { + if (request === "node:child_process") { + return { spawn: () => makeChild(++spawnCount === 1) }; + } + return originalLoad.call(this, request, parent, isMain); + }; + delete require.cache[bridgePath]; + const isolatedBridge = require("./compressUploadBridge.cjs"); + Module._load = originalLoad; + t.after(() => { + Module._load = originalLoad; + globalThis.setTimeout = originalSetTimeout; + delete require.cache[bridgePath]; + }); + globalThis.setTimeout = (callback, delay, ...args) => originalSetTimeout( + callback, + delay === 750 ? 5 : delay, + ...args, + ); + + const remoteStream = new EventEmitter(); + remoteStream.stderr = new EventEmitter(); + remoteStream.close = () => {}; + remoteStream.end = () => {}; + remoteStream.destroy = () => {}; + let released = 0; + isolatedBridge.init({ + sftpClients: new Map([["sftp-real-compressor", { + __netcattyEndpointKey: "endpoint:real-compressor", + client: { + exec(_command, callback) { + callback(null, remoteStream); + process.nextTick(() => { + remoteStream.emit("data", Buffer.from("tar")); + remoteStream.emit("close", 0); + }); + }, + }, + }]]), + transferBridge: { + acquireTransferSessionLeases() { return ["sftp-real-compressor"]; }, + releaseTransferSessionLeases() { released += 1; }, + broadcastGlobalTransferEvent() {}, + async cancelTransfer() { return { success: true }; }, + }, + }); + const handlers = new Map(); + isolatedBridge.registerHandlers({ handle(channel, handler) { handlers.set(channel, handler); } }); + const sender = { id: 1, send() {} }; + const starting = handlers.get("netcatty:compress:start")({ sender }, { + compressionId: "real-compressor-cancel", + folderPath: "/unused", + targetPath: "/unused", + sftpId: "sftp-real-compressor", + folderName: "unused", + totalBytes: 1, + }); + while (!compressor) await new Promise((resolve) => originalSetTimeout(resolve, 1)); + await handlers.get("netcatty:compress:cancel")({ sender }, { + compressionId: "real-compressor-cancel", + }); + try { + await new Promise((resolve) => originalSetTimeout(resolve, 20)); + assert.deepEqual(signals, ["SIGTERM", "SIGKILL"]); + await starting; + assert.equal(released, 1); + assert.equal(isolatedBridge._getActiveCompressionCountForTests(), 0); + } finally { + compressor.emit("close", null); + await starting.catch(() => {}); + } +}); + +test("failed compressed extraction leaves the existing final directory untouched", (t) => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-compress-atomic-")); + t.after(() => fs.rmSync(root, { recursive: true, force: true })); + const target = path.join(root, "target"); + const finalDir = path.join(target, "folder"); + fs.mkdirSync(finalDir, { recursive: true }); + fs.writeFileSync(path.join(finalDir, "old.txt"), "old"); + + const archive = path.join(target, "missing-folder.tar.gz"); + + const command = buildAtomicRemoteExtractionCommand({ + compressionId: "atomic-failure", + archivePath: archive, + targetDir: target, + folderName: "folder", + }); + assert.throws(() => execFileSync("/bin/sh", ["-c", command], { stdio: "pipe" })); + + assert.equal(fs.readFileSync(path.join(finalDir, "old.txt"), "utf8"), "old"); + assert.equal(fs.existsSync(path.join(finalDir, "new-first.txt")), false); + assert.equal( + fs.readdirSync(target).some((name) => name.includes(".netcatty-compress-")), + false, + ); +}); + +test("successful compressed extraction atomically merges and removes staging artifacts", (t) => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-compress-commit-")); + t.after(() => fs.rmSync(root, { recursive: true, force: true })); + const target = path.join(root, "target"); + const finalDir = path.join(target, "folder"); + const source = path.join(root, "source"); + fs.mkdirSync(finalDir, { recursive: true }); + fs.mkdirSync(path.join(source, "folder"), { recursive: true }); + fs.writeFileSync(path.join(finalDir, "old.txt"), "old"); + fs.writeFileSync(path.join(source, "folder", "new.txt"), "new"); + + const archive = path.join(target, "folder.tar.gz"); + const created = spawnSync("tar", ["-czf", archive, "-C", source, "folder"]); + assert.equal(created.status, 0, created.stderr?.toString()); + const command = buildAtomicRemoteExtractionCommand({ + compressionId: "atomic-success", + archivePath: archive, + targetDir: target, + folderName: "folder", + }); + execFileSync("/bin/sh", ["-c", command], { stdio: "pipe" }); + + assert.equal(fs.readFileSync(path.join(finalDir, "old.txt"), "utf8"), "old"); + assert.equal(fs.readFileSync(path.join(finalDir, "new.txt"), "utf8"), "new"); + assert.equal(fs.existsSync(archive), false); + assert.equal( + fs.readdirSync(target).some((name) => name.includes(".netcatty-compress-")), + false, + ); +}); + +test("compressed replacement uses one stable recovery backup across operation ids", () => { + const first = buildAtomicRemoteExtractionCommand({ + compressionId: "operation-one", + archivePath: "/target/.folder-one.tar.gz", + targetDir: "/target", + folderName: "folder", + }); + const second = buildAtomicRemoteExtractionCommand({ + compressionId: "operation-two", + archivePath: "/target/.folder-two.tar.gz", + targetDir: "/target", + folderName: "folder", + }); + const valueFor = (command, name) => command.split("\n").find((line) => line.startsWith(`${name}=`)); + + assert.equal(valueFor(first, "backup"), valueFor(second, "backup")); + assert.equal(valueFor(first, "stage"), valueFor(second, "stage")); + assert.notEqual(valueFor(first, "archive"), valueFor(second, "archive")); +}); + +test("compressed promotion can clean a read-only old directory instead of leaking its backup", (t) => { + if (process.platform === "win32") return t.skip("POSIX mode test"); + const root = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-compress-protected-backup-")); + t.after(() => { + try { execFileSync("chmod", ["-R", "u+w", root]); } catch { /* cleanup */ } + fs.rmSync(root, { recursive: true, force: true }); + }); + const target = path.join(root, "target"); + const finalDir = path.join(target, "folder"); + const source = path.join(root, "source"); + fs.mkdirSync(path.join(finalDir, "locked"), { recursive: true }); + fs.mkdirSync(path.join(source, "folder"), { recursive: true }); + fs.writeFileSync(path.join(finalDir, "locked", "old.txt"), "old"); + fs.writeFileSync(path.join(source, "folder", "new.txt"), "new"); + fs.chmodSync(path.join(finalDir, "locked"), 0o500); + const archive = path.join(target, ".folder-operation.tar.gz"); + assert.equal(spawnSync("tar", ["-czf", archive, "-C", source, "folder"]).status, 0); + + const command = buildAtomicRemoteExtractionCommand({ + compressionId: "protected-cleanup", + archivePath: archive, + targetDir: target, + folderName: "folder", + }); + execFileSync("/bin/sh", ["-c", command], { stdio: "pipe" }); + + assert.equal(fs.readFileSync(path.join(finalDir, "new.txt"), "utf8"), "new"); + assert.equal( + fs.readdirSync(target).some((name) => name.includes(".netcatty-compress-")), + false, + ); +}); diff --git a/electron/bridges/emitTerminalSessionData.cjs b/electron/bridges/emitTerminalSessionData.cjs index c41774347..f8527dfd9 100644 --- a/electron/bridges/emitTerminalSessionData.cjs +++ b/electron/bridges/emitTerminalSessionData.cjs @@ -13,6 +13,22 @@ let onSessionActivity = null; /** @type {Set<(sessionId: string, data: string) => void>} */ const dataTaps = new Set(); const emitPerfLogStateBySession = new Map(); +const MAX_EMIT_PERF_SESSION_STATES = 512; + +function retainTerminalSessionPerformanceState(key, state) { + emitPerfLogStateBySession.delete(key); + emitPerfLogStateBySession.set(key, state); + while (emitPerfLogStateBySession.size > MAX_EMIT_PERF_SESSION_STATES) { + const oldestKey = emitPerfLogStateBySession.keys().next().value; + if (oldestKey === undefined) break; + emitPerfLogStateBySession.delete(oldestKey); + } +} + +function clearTerminalSessionPerformanceState(sessionId) { + if (!sessionId) return; + emitPerfLogStateBySession.delete(sessionId); +} function configureTerminalSessionDataEmitter(options = {}) { getSession = typeof options.getSession === "function" ? options.getSession : null; @@ -50,7 +66,7 @@ function getEmitPerfLogDetails(sessionId, terminalPerf, options = {}) { || now - state.lastLoggedAt >= 1000; if (!shouldLog) { - emitPerfLogStateBySession.set(key, state); + retainTerminalSessionPerformanceState(key, state); return null; } @@ -66,7 +82,7 @@ function getEmitPerfLogDetails(sessionId, terminalPerf, options = {}) { rows: options?.rows, }; - emitPerfLogStateBySession.set(key, { + retainTerminalSessionPerformanceState(key, { lastLoggedAt: now, batchChunks: 0, batchChars: 0, @@ -118,4 +134,6 @@ module.exports = { configureTerminalSessionDataEmitter, emitTerminalSessionData, addTerminalDataTap, + clearTerminalSessionPerformanceState, + _getTerminalSessionPerformanceStateCountForTests: () => emitPerfLogStateBySession.size, }; diff --git a/electron/bridges/emitTerminalSessionData.test.cjs b/electron/bridges/emitTerminalSessionData.test.cjs index 075300c86..58a6f8466 100644 --- a/electron/bridges/emitTerminalSessionData.test.cjs +++ b/electron/bridges/emitTerminalSessionData.test.cjs @@ -3,8 +3,10 @@ const test = require("node:test"); const { addTerminalDataTap, + clearTerminalSessionPerformanceState, configureTerminalSessionDataEmitter, emitTerminalSessionData, + _getTerminalSessionPerformanceStateCountForTests, } = require("./emitTerminalSessionData.cjs"); test("emitTerminalSessionData rejects output from a replaced backend session", () => { @@ -150,3 +152,28 @@ test("emitTerminalSessionData reports terminal output as session activity", () = ]); configureTerminalSessionDataEmitter({}); }); + +test("terminal output performance state is bounded and removable on session close", () => { + const previousDebug = process.env.NETCATTY_TERMINAL_PERF_DEBUG; + process.env.NETCATTY_TERMINAL_PERF_DEBUG = "1"; + const originalConsoleInfo = console.info; + console.info = () => {}; + try { + configureTerminalSessionDataEmitter({ + getSession: () => null, + outputChannel: { send: () => true }, + }); + for (let index = 0; index < 700; index += 1) { + emitTerminalSessionData(null, `perf-${index}`, "x"); + } + assert.equal(_getTerminalSessionPerformanceStateCountForTests(), 512); + + clearTerminalSessionPerformanceState("perf-699"); + assert.equal(_getTerminalSessionPerformanceStateCountForTests(), 511); + } finally { + console.info = originalConsoleInfo; + if (previousDebug === undefined) delete process.env.NETCATTY_TERMINAL_PERF_DEBUG; + else process.env.NETCATTY_TERMINAL_PERF_DEBUG = previousDebug; + configureTerminalSessionDataEmitter({}); + } +}); diff --git a/electron/bridges/externalMcp/boundedCliCommand.cjs b/electron/bridges/externalMcp/boundedCliCommand.cjs new file mode 100644 index 000000000..f3758aad4 --- /dev/null +++ b/electron/bridges/externalMcp/boundedCliCommand.cjs @@ -0,0 +1,161 @@ +"use strict"; + +const { StringDecoder } = require("node:string_decoder"); + +const DEFAULT_EXTERNAL_MCP_CLI_TIMEOUT_MS = 30_000; +const DEFAULT_EXTERNAL_MCP_CLI_MAX_OUTPUT_BYTES = 1024 * 1024; +const DEFAULT_EXTERNAL_MCP_CLI_KILL_GRACE_MS = 750; + +function runBoundedCliCommand(deps, command, args = [], options = {}) { + const timeoutMs = Math.max( + 1, + Number(options.timeoutMs) || DEFAULT_EXTERNAL_MCP_CLI_TIMEOUT_MS, + ); + const maxOutputBytes = Math.max( + 1, + Number(options.maxOutputBytes) || DEFAULT_EXTERNAL_MCP_CLI_MAX_OUTPUT_BYTES, + ); + const killGraceMs = Math.max( + 1, + Number(options.killGraceMs) || DEFAULT_EXTERNAL_MCP_CLI_KILL_GRACE_MS, + ); + const signal = options.signal || null; + + return new Promise((resolve, reject) => { + let child; + let settled = false; + let closed = false; + let timeoutTimer = null; + let forceKillTimer = null; + let stdout = ""; + let stderr = ""; + let outputBytes = 0; + const stdoutDecoder = new StringDecoder("utf8"); + const stderrDecoder = new StringDecoder("utf8"); + let decodersEnded = false; + + const clearTimeoutTimer = () => { + if (timeoutTimer) clearTimeout(timeoutTimer); + timeoutTimer = null; + }; + const clearForceKillTimer = () => { + if (forceKillTimer) clearTimeout(forceKillTimer); + forceKillTimer = null; + }; + const removeDataListeners = () => { + child?.stdout?.removeListener?.("data", onStdout); + child?.stderr?.removeListener?.("data", onStderr); + }; + const detachAbort = () => signal?.removeEventListener?.("abort", onAbort); + const armForcedKill = () => { + if (!child || closed || forceKillTimer) return; + try { child.kill?.("SIGTERM"); } catch { /* ignore */ } + forceKillTimer = setTimeout(() => { + if (closed) return; + try { child.kill?.("SIGKILL"); } catch { /* ignore */ } + }, killGraceMs); + forceKillTimer.unref?.(); + }; + const rejectAndTerminate = (error) => { + if (settled) return; + settled = true; + clearTimeoutTimer(); + detachAbort(); + removeDataListeners(); + armForcedKill(); + reject(error); + }; + const append = (target, chunk) => { + if (settled) return; + const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk)); + const remaining = Math.max(0, maxOutputBytes - outputBytes); + if (remaining > 0) { + const accepted = buffer.length <= remaining ? buffer : buffer.subarray(0, remaining); + if (target === "stdout") stdout += stdoutDecoder.write(accepted); + else stderr += stderrDecoder.write(accepted); + outputBytes += accepted.length; + } + if (buffer.length > remaining) { + const error = new Error(`CLI output exceeded ${maxOutputBytes} bytes`); + error.code = "CLI_OUTPUT_LIMIT"; + rejectAndTerminate(error); + } + }; + const onStdout = (chunk) => append("stdout", chunk); + const onStderr = (chunk) => append("stderr", chunk); + const onAbort = () => { + const reason = signal?.reason; + const error = reason instanceof Error ? reason : new Error("CLI command was cancelled"); + if (!error.code) error.code = "ABORT_ERR"; + rejectAndTerminate(error); + }; + const onError = (error) => { + closed = true; + clearTimeoutTimer(); + clearForceKillTimer(); + detachAbort(); + removeDataListeners(); + if (settled) return; + settled = true; + reject(error); + }; + const onClose = (exitCode) => { + closed = true; + clearTimeoutTimer(); + clearForceKillTimer(); + detachAbort(); + removeDataListeners(); + if (settled) return; + settled = true; + if (!decodersEnded) { + decodersEnded = true; + stdout += stdoutDecoder.end(); + stderr += stderrDecoder.end(); + } + resolve({ + exitCode, + stdout: deps.stripAnsi(stdout), + stderr: deps.stripAnsi(stderr), + }); + }; + + if (signal?.aborted) { + onAbort(); + return; + } + try { + const spawnSpec = deps.prepareCommandForSpawn(command, args); + child = deps.spawn(spawnSpec.command, spawnSpec.args || [], { + stdio: ["ignore", "pipe", "pipe"], + cwd: options.cwd || undefined, + env: options.env || process.env, + shell: spawnSpec.shell, + windowsHide: true, + }); + } catch (error) { + settled = true; + reject(error); + return; + } + + child.stdout?.on?.("data", onStdout); + child.stderr?.on?.("data", onStderr); + child.once?.("error", onError); + child.once?.("close", onClose); + signal?.addEventListener?.("abort", onAbort, { once: true }); + timeoutTimer = setTimeout(() => { + const error = new Error(`CLI command timed out after ${timeoutMs} ms`); + error.code = "CLI_TIMEOUT"; + rejectAndTerminate(error); + }, timeoutMs); + timeoutTimer.unref?.(); + if (signal?.aborted) onAbort(); + }); +} + +module.exports = { + DEFAULT_EXTERNAL_MCP_CLI_TIMEOUT_MS, + DEFAULT_EXTERNAL_MCP_CLI_MAX_OUTPUT_BYTES, + DEFAULT_EXTERNAL_MCP_CLI_KILL_GRACE_MS, + runBoundedCliCommand, +}; diff --git a/electron/bridges/externalMcp/boundedCliCommand.test.cjs b/electron/bridges/externalMcp/boundedCliCommand.test.cjs new file mode 100644 index 000000000..5afa4eb3f --- /dev/null +++ b/electron/bridges/externalMcp/boundedCliCommand.test.cjs @@ -0,0 +1,129 @@ +"use strict"; + +const test = require("node:test"); +const assert = require("node:assert/strict"); +const { EventEmitter } = require("node:events"); +const { runBoundedCliCommand } = require("./boundedCliCommand.cjs"); + +function createChild() { + const child = new EventEmitter(); + child.stdout = new EventEmitter(); + child.stderr = new EventEmitter(); + child.kills = []; + child.kill = (signal) => { + child.kills.push(signal); + return true; + }; + return child; +} + +function depsFor(child) { + return { + prepareCommandForSpawn: (command, args) => ({ command, args, shell: false }), + spawn: () => child, + stripAnsi: (value) => value, + }; +} + +test("bounded external MCP CLI times out and escalates termination", async () => { + const child = createChild(); + await assert.rejects( + runBoundedCliCommand(depsFor(child), "codex", [], { timeoutMs: 5, killGraceMs: 5 }), + (error) => error.code === "CLI_TIMEOUT", + ); + assert.deepEqual(child.kills, ["SIGTERM"]); + await new Promise((resolve) => setTimeout(resolve, 10)); + assert.deepEqual(child.kills, ["SIGTERM", "SIGKILL"]); + child.emit("close", null); +}); + +test("bounded external MCP CLI caps combined output and removes data listeners", async () => { + const child = createChild(); + const result = runBoundedCliCommand(depsFor(child), "claude", [], { + timeoutMs: 100, + maxOutputBytes: 8, + }); + child.stdout.emit("data", Buffer.from("12345")); + child.stderr.emit("data", Buffer.from("67890")); + await assert.rejects(result, (error) => error.code === "CLI_OUTPUT_LIMIT"); + assert.equal(child.stdout.listenerCount("data"), 0); + assert.equal(child.stderr.listenerCount("data"), 0); + assert.deepEqual(child.kills, ["SIGTERM"]); + child.emit("close", null); +}); + +test("bounded external MCP CLI propagates spawn errors and clears timers", async () => { + const child = createChild(); + const result = runBoundedCliCommand(depsFor(child), "grok", [], { timeoutMs: 5 }); + child.emit("error", new Error("spawn failed")); + await assert.rejects(result, /spawn failed/); + await new Promise((resolve) => setTimeout(resolve, 10)); + assert.deepEqual(child.kills, []); +}); + +test("bounded external MCP CLI cancels and force-closes a stuck child", async () => { + const child = createChild(); + const controller = new AbortController(); + const result = runBoundedCliCommand(depsFor(child), "codex", [], { + signal: controller.signal, + timeoutMs: 100, + killGraceMs: 5, + }); + controller.abort(new Error("cancelled")); + await assert.rejects(result, /cancelled/); + assert.deepEqual(child.kills, ["SIGTERM"]); + await new Promise((resolve) => setTimeout(resolve, 10)); + assert.deepEqual(child.kills, ["SIGTERM", "SIGKILL"]); + child.emit("close", null); +}); + +test("bounded external MCP CLI resolves output and leaves no listeners", async () => { + const child = createChild(); + const result = runBoundedCliCommand(depsFor(child), "codex", [], { timeoutMs: 100 }); + child.stdout.emit("data", Buffer.from("ok")); + child.stderr.emit("data", Buffer.from("warn")); + child.emit("close", 0); + assert.deepEqual(await result, { exitCode: 0, stdout: "ok", stderr: "warn" }); + assert.equal(child.stdout.listenerCount("data"), 0); + assert.equal(child.stderr.listenerCount("data"), 0); +}); + +test("bounded external MCP CLI preserves UTF-8 split across stdout chunks", async () => { + const child = createChild(); + const result = runBoundedCliCommand(depsFor(child), "codex", [], { timeoutMs: 100 }); + const bytes = Buffer.from("中文", "utf8"); + child.stdout.emit("data", bytes.subarray(0, 2)); + child.stdout.emit("data", bytes.subarray(2, 4)); + child.stdout.emit("data", bytes.subarray(4)); + child.emit("close", 0); + + assert.deepEqual(await result, { exitCode: 0, stdout: "中文", stderr: "" }); +}); + +test("bounded external MCP CLI completes a UTF-8 code point at the byte limit", async () => { + const child = createChild(); + const bytes = Buffer.from("中", "utf8"); + const result = runBoundedCliCommand(depsFor(child), "codex", [], { + timeoutMs: 100, + maxOutputBytes: bytes.length, + }); + child.stdout.emit("data", bytes.subarray(0, 2)); + child.stdout.emit("data", bytes.subarray(2)); + child.emit("close", 0); + + assert.deepEqual(await result, { exitCode: 0, stdout: "中", stderr: "" }); +}); + +test("bounded external MCP CLI decodes stdout and stderr independently", async () => { + const child = createChild(); + const stdoutBytes = Buffer.from("中", "utf8"); + const stderrBytes = Buffer.from("文", "utf8"); + const result = runBoundedCliCommand(depsFor(child), "codex", [], { timeoutMs: 100 }); + child.stdout.emit("data", stdoutBytes.subarray(0, 2)); + child.stderr.emit("data", stderrBytes.subarray(0, 1)); + child.stdout.emit("data", stdoutBytes.subarray(2)); + child.stderr.emit("data", stderrBytes.subarray(1)); + child.emit("close", 0); + + assert.deepEqual(await result, { exitCode: 0, stdout: "中", stderr: "文" }); +}); diff --git a/electron/bridges/externalMcp/claudeSetup.cjs b/electron/bridges/externalMcp/claudeSetup.cjs index 96089c243..179681179 100644 --- a/electron/bridges/externalMcp/claudeSetup.cjs +++ b/electron/bridges/externalMcp/claudeSetup.cjs @@ -1,5 +1,7 @@ "use strict"; +const { runBoundedCliCommand } = require("./boundedCliCommand.cjs"); + const EXTERNAL_MCP_CLAUDE_NAME = "netcatty-external"; const { formatDiscoveryEnvCliFlags, @@ -304,33 +306,7 @@ function createExternalMcpClaudeSetup(options = {}) { } async function runClaude(claudePath, shellEnv, args) { - return await new Promise((resolve, reject) => { - const spawnSpec = deps.prepareCommandForSpawn(claudePath, args); - const child = deps.spawn(spawnSpec.command, spawnSpec.args || [], { - stdio: ["ignore", "pipe", "pipe"], - env: shellEnv, - shell: spawnSpec.shell, - windowsHide: true, - }); - - let stdout = ""; - let stderr = ""; - - child.stdout.on("data", (chunk) => { - stdout += chunk.toString("utf8"); - }); - child.stderr.on("data", (chunk) => { - stderr += chunk.toString("utf8"); - }); - child.once("error", reject); - child.once("close", (exitCode) => { - resolve({ - exitCode, - stdout: deps.stripAnsi(stdout), - stderr: deps.stripAnsi(stderr), - }); - }); - }); + return await runBoundedCliCommand(deps, claudePath, args, { env: shellEnv }); } async function getStatus() { diff --git a/electron/bridges/externalMcp/codexSetup.cjs b/electron/bridges/externalMcp/codexSetup.cjs index d6c77de95..fc440d021 100644 --- a/electron/bridges/externalMcp/codexSetup.cjs +++ b/electron/bridges/externalMcp/codexSetup.cjs @@ -1,5 +1,7 @@ "use strict"; +const { runBoundedCliCommand } = require("./boundedCliCommand.cjs"); + const EXTERNAL_MCP_CODEX_NAME = "netcatty-external"; const { formatDiscoveryEnvCliFlags, @@ -202,33 +204,7 @@ function createExternalMcpCodexSetup(options = {}) { } async function runCodex(codexPath, shellEnv, args) { - return await new Promise((resolve, reject) => { - const spawnSpec = deps.prepareCommandForSpawn(codexPath, args); - const child = deps.spawn(spawnSpec.command, spawnSpec.args || [], { - stdio: ["ignore", "pipe", "pipe"], - env: shellEnv, - shell: spawnSpec.shell, - windowsHide: true, - }); - - let stdout = ""; - let stderr = ""; - - child.stdout.on("data", (chunk) => { - stdout += chunk.toString("utf8"); - }); - child.stderr.on("data", (chunk) => { - stderr += chunk.toString("utf8"); - }); - child.once("error", reject); - child.once("close", (exitCode) => { - resolve({ - exitCode, - stdout: deps.stripAnsi(stdout), - stderr: deps.stripAnsi(stderr), - }); - }); - }); + return await runBoundedCliCommand(deps, codexPath, args, { env: shellEnv }); } function summarizeFailure(result, fallback) { diff --git a/electron/bridges/externalMcp/grokSetup.cjs b/electron/bridges/externalMcp/grokSetup.cjs index e08498d55..9163eb0e8 100644 --- a/electron/bridges/externalMcp/grokSetup.cjs +++ b/electron/bridges/externalMcp/grokSetup.cjs @@ -1,5 +1,7 @@ "use strict"; +const { runBoundedCliCommand } = require("./boundedCliCommand.cjs"); + const EXTERNAL_MCP_GROK_NAME = "netcatty-external"; const { formatDiscoveryEnvCliFlags, @@ -271,33 +273,7 @@ function createExternalMcpGrokSetup(options = {}) { } async function runGrok(grokPath, shellEnv, args) { - return await new Promise((resolve, reject) => { - const spawnSpec = deps.prepareCommandForSpawn(grokPath, args); - const child = deps.spawn(spawnSpec.command, spawnSpec.args || [], { - stdio: ["ignore", "pipe", "pipe"], - env: shellEnv, - shell: spawnSpec.shell, - windowsHide: true, - }); - - let stdout = ""; - let stderr = ""; - - child.stdout.on("data", (chunk) => { - stdout += chunk.toString("utf8"); - }); - child.stderr.on("data", (chunk) => { - stderr += chunk.toString("utf8"); - }); - child.once("error", reject); - child.once("close", (exitCode) => { - resolve({ - exitCode, - stdout: deps.stripAnsi(stdout), - stderr: deps.stripAnsi(stderr), - }); - }); - }); + return await runBoundedCliCommand(deps, grokPath, args, { env: shellEnv }); } function summarizeFailure(result, fallback) { diff --git a/electron/bridges/fileWatcherBridge.cjs b/electron/bridges/fileWatcherBridge.cjs index f59dfd23e..34ededf73 100644 --- a/electron/bridges/fileWatcherBridge.cjs +++ b/electron/bridges/fileWatcherBridge.cjs @@ -10,21 +10,24 @@ const fs = require("node:fs"); const path = require("node:path"); const crypto = require("node:crypto"); -// Lazy-load encodePathForSession to avoid circular dependency issues -let encodePathForSession = null; - // Map of watchId -> { watcher, localPath, remotePath, sftpId, lastModified, lastSize } const activeWatchers = new Map(); +const activeWatchIdsByKey = new Map(); +const observedOwnerSenders = new WeakSet(); +const pendingWatchStarts = new Set(); +const MAX_ACTIVE_FILE_WATCHERS = 256; +const TEMP_FILE_CLEANUP_RETRY_DELAYS_MS = [100, 250, 500, 1000, 2000]; // Debounce map to prevent multiple rapid syncs const debounceTimers = new Map(); -// Map of sftpId -> Set to track temp files even without watching +// Map of sftpId -> Map> to track temp files even without watching // This allows cleanup when SFTP session closes, regardless of auto-sync setting const tempFilesMap = new Map(); let sftpClients = null; let electronModule = null; +let transferBridge = null; /** * Initialize the file watcher bridge with dependencies @@ -32,20 +35,85 @@ let electronModule = null; function init(deps) { sftpClients = deps.sftpClients; electronModule = deps.electronModule; + transferBridge = deps.transferBridge || null; } /** * Register a temp file for cleanup when SFTP session closes * Called regardless of whether auto-sync is enabled */ -function registerTempFile(sftpId, localPath) { +function getOwnerId(sender, fallback) { + if (Number.isSafeInteger(sender?.id)) return sender.id; + return Number.isSafeInteger(fallback) ? fallback : null; +} + +function buildWatchKey(sftpId, localPath, remotePath) { + return `${sftpId}\0${path.resolve(localPath)}\0${remotePath}`; +} + +function registerTempFile(sftpId, localPath, ownerId = null) { if (!tempFilesMap.has(sftpId)) { - tempFilesMap.set(sftpId, new Set()); + tempFilesMap.set(sftpId, new Map()); } - tempFilesMap.get(sftpId).add(localPath); + const files = tempFilesMap.get(sftpId); + const owners = files.get(localPath) ?? new Set(); + if (Number.isSafeInteger(ownerId)) owners.add(ownerId); + files.set(localPath, owners); console.log(`[FileWatcher] Registered temp file for cleanup: ${localPath} (session: ${sftpId})`); } +function releaseTempFileReference( + sftpId, + localPath, + ownerId, + { force = false, cleanup = false } = {}, +) { + const files = tempFilesMap.get(sftpId); + const owners = files?.get(localPath); + if (!files || !owners) return false; + + if (!force && ownerId != null) owners.delete(ownerId); + if (!force && ownerId != null && owners.size > 0) return false; + + files.delete(localPath); + if (files.size === 0) tempFilesMap.delete(sftpId); + if (cleanup) cleanupTempFileAsync(localPath); + return true; +} + +async function unregisterTempFile(event, payload = {}) { + const { sftpId, localPath } = payload; + if (!sftpId || !localPath) return { success: false }; + const ownerId = getOwnerId(event?.sender, payload.webContentsId); + const wasTracked = tempFilesMap.get(sftpId)?.has(localPath) === true; + releaseTempFileReference(sftpId, localPath, ownerId, { + force: ownerId == null, + cleanup: false, + }); + const retained = tempFilesMap.get(sftpId)?.has(localPath) === true; + if (!retained) await cleanupTempFileAsync(localPath); + return { success: true, retained, wasTracked }; +} + +function hasOtherOwnedWatch(sftpId, localPath, ownerId, excludedWatchId) { + if (ownerId == null) return false; + for (const [watchId, watchInfo] of activeWatchers) { + if (watchId === excludedWatchId) continue; + if (watchInfo.sftpId !== sftpId || watchInfo.localPath !== localPath) continue; + if (watchInfo.owners.has(ownerId)) return true; + } + return false; +} + +function observeOwnerSender(sender) { + if (!sender || typeof sender !== "object" || observedOwnerSenders.has(sender)) return; + observedOwnerSenders.add(sender); + sender.once?.("destroyed", () => { + const ownerId = getOwnerId(sender); + if (ownerId != null) releaseOwnerResources(ownerId, true); + }); +} + /** * Show a system notification for file sync events * Works on macOS, Windows, and Linux @@ -82,7 +150,22 @@ function showSystemNotification(title, body) { * Returns a watchId that can be used to stop watching */ async function startWatching(event, { localPath, remotePath, sftpId, encoding }) { + const ownerId = getOwnerId(event?.sender); + observeOwnerSender(event?.sender); + const watchKey = buildWatchKey(sftpId, localPath, remotePath); + const existingWatchId = activeWatchIdsByKey.get(watchKey); + const existingWatch = existingWatchId ? activeWatchers.get(existingWatchId) : null; + if (existingWatch) { + if (ownerId != null) existingWatch.owners.set(ownerId, event.sender); + existingWatch.encoding = encoding; + return { watchId: existingWatchId, reused: true }; + } + if (activeWatchers.size + pendingWatchStarts.size >= MAX_ACTIVE_FILE_WATCHERS) { + throw new Error(`Too many active external file watches (max ${MAX_ACTIVE_FILE_WATCHERS})`); + } const watchId = `watch-${crypto.randomUUID()}`; + const pendingStart = { watchId, watchKey, sftpId, ownerId, cancelled: false }; + pendingWatchStarts.add(pendingStart); console.log(`[FileWatcher] Starting watch: ${localPath} -> ${remotePath}`); @@ -95,19 +178,33 @@ async function startWatching(event, { localPath, remotePath, sftpId, encoding }) lastSize = stat.size; console.log(`[FileWatcher] Initial file stats: mtime=${lastModified}, size=${lastSize}`); } catch (err) { + pendingWatchStarts.delete(pendingStart); console.error(`[FileWatcher] Failed to stat file ${localPath}:`, err.message); throw new Error(`Cannot watch file: ${err.message}`); } - - // Store webContents reference for later notifications - const webContents = event.sender; + + if (pendingStart.cancelled) { + pendingWatchStarts.delete(pendingStart); + throw new Error("File watch start cancelled because its owner or SFTP session closed"); + } + + // Two identical starts can overlap during the async stat. Re-check after the + // await so only the first continuation installs a polling listener. + const racedWatchId = activeWatchIdsByKey.get(watchKey); + const racedWatch = racedWatchId ? activeWatchers.get(racedWatchId) : null; + if (racedWatch) { + if (ownerId != null) racedWatch.owners.set(ownerId, event.sender); + racedWatch.encoding = encoding; + pendingWatchStarts.delete(pendingStart); + return { watchId: racedWatchId, reused: true }; + } // Use fs.watchFile (polling) instead of fs.watch for better reliability on Windows // fs.watch can miss events when editors use atomic writes (save to temp, then rename) // fs.watchFile polls the file system at regular intervals const pollInterval = 1000; // Check every 1 second - fs.watchFile(localPath, { persistent: true, interval: pollInterval }, async (curr, prev) => { + const listener = async (curr, prev) => { console.log(`[FileWatcher] File stat change detected for ${localPath}`); console.log(`[FileWatcher] Previous: mtime=${prev.mtimeMs}, size=${prev.size}`); console.log(`[FileWatcher] Current: mtime=${curr.mtimeMs}, size=${curr.size}`); @@ -115,7 +212,7 @@ async function startWatching(event, { localPath, remotePath, sftpId, encoding }) // Check if file was deleted if (curr.nlink === 0) { console.log(`[FileWatcher] File ${localPath} was deleted, stopping watch`); - stopWatching(null, { watchId }); + stopWatching(null, { watchId, force: true, cleanupTempFile: true }); return; } @@ -133,23 +230,36 @@ async function startWatching(event, { localPath, remotePath, sftpId, encoding }) const timer = setTimeout(async () => { debounceTimers.delete(watchId); - await handleFileChange(watchId, webContents); + await handleFileChange(watchId); }, 500); // 500ms debounce debounceTimers.set(watchId, timer); - }); + }; + try { + fs.watchFile(localPath, { persistent: true, interval: pollInterval }, listener); - activeWatchers.set(watchId, { - watcher: null, // fs.watchFile doesn't return a watcher object - localPath, - remotePath, - sftpId, - encoding, - lastModified, - lastSize, - webContents, - useWatchFile: true, // Flag to indicate we're using fs.watchFile - }); + activeWatchers.set(watchId, { + watcher: null, // fs.watchFile doesn't return a watcher object + localPath, + remotePath, + sftpId, + encoding, + lastModified, + lastSize, + listener, + watchKey, + owners: new Map(ownerId == null ? [] : [[ownerId, event.sender]]), + activeTransferId: null, + syncPromise: null, + syncRequested: false, + syncSequence: 0, + stopped: false, + useWatchFile: true, // Flag to indicate we're using fs.watchFile + }); + activeWatchIdsByKey.set(watchKey, watchId); + } finally { + pendingWatchStarts.delete(pendingStart); + } console.log(`[FileWatcher] Watch started with ID: ${watchId} (using fs.watchFile polling every ${pollInterval}ms)`); return { watchId }; @@ -158,126 +268,171 @@ async function startWatching(event, { localPath, remotePath, sftpId, encoding }) /** * Handle file change event - sync to remote */ -async function handleFileChange(watchId, webContents) { - const watchInfo = activeWatchers.get(watchId); - if (!watchInfo) return; +function notifyWatchOwners(watchInfo, channel, payload) { + for (const [ownerId, webContents] of [...watchInfo.owners]) { + if (webContents?.isDestroyed?.()) { + watchInfo.owners.delete(ownerId); + continue; + } + webContents?.send?.(channel, payload); + } +} + +async function syncFileChangeOnce(watchId, watchInfo) { + if (watchInfo.stopped || activeWatchers.get(watchId) !== watchInfo) return; const { localPath, remotePath, sftpId, encoding, lastModified: previousModified, lastSize: previousSize } = watchInfo; - // Lazy-load encodePathForSession to avoid circular dependency - if (encodePathForSession === null) { - ({ encodePathForSession } = require("./sftpBridge.cjs")); - } - // Extract file name once for notifications and logging const fileName = path.basename(remotePath); console.log(`[FileWatcher] File change detected: ${localPath}`); - try { - // Check if file was actually modified (compare mtime and size) - const stat = await fs.promises.stat(localPath); - - // Skip if neither mtime nor size changed (prevents spurious events on some platforms) - if (stat.mtimeMs <= previousModified && stat.size === previousSize) { - console.log(`[FileWatcher] File unchanged (mtime and size same), skipping sync`); - return; - } - - // Update lastModified and lastSize - watchInfo.lastModified = stat.mtimeMs; - watchInfo.lastSize = stat.size; - - // Get the SFTP client - if (!sftpClients) { - throw new Error("SFTP clients not initialized"); - } - - const client = sftpClients.get(sftpId); - if (!client) { - throw new Error("SFTP session not found or expired"); - } - - // Read the local file - const content = await fs.promises.readFile(localPath); - - console.log(`[FileWatcher] Syncing ${content.length} bytes to ${remotePath}`); - - // Upload to remote (SCP-mode sessions have no SFTP channel — use SCP backend). - const { isScpModeClient, getScpBackendForClient } = require("./sftpBridge/scpBackend.cjs"); - if (isScpModeClient(client)) { - // Prefer session-resolved encoding (auto may have upgraded to gb18030). - let enc = encoding; - try { - const { getResolvedFilenameEncoding } = require("./sftpBridge.cjs"); - if (typeof getResolvedFilenameEncoding === "function") { - enc = getResolvedFilenameEncoding(sftpId, encoding) || encoding; - } - } catch { /* ignore */ } - if (!enc || enc === "auto") enc = "utf-8"; - const backend = getScpBackendForClient(client); - let existingMode = 0o0644; - try { - const st = await backend.stat(remotePath, { encoding: enc }); - if (typeof st.mode === "number" && st.mode > 0) existingMode = st.mode & 0o7777; - } catch { /* new file defaults */ } - await backend.writeFile(remotePath, content, { encoding: enc, mode: existingMode }); - try { await backend.chmod(remotePath, existingMode, { encoding: enc }); } catch { /* ignore */ } - } else { - const encodedPath = encodePathForSession(sftpId, remotePath, encoding); - await client.put(content, encodedPath); - } - - console.log(`[FileWatcher] Sync complete: ${remotePath}`); - - // Show system notification for successful sync - showSystemNotification( - "Netcatty", - `File synced to remote: ${fileName}` - ); - - // Notify the renderer about successful sync - if (webContents && !webContents.isDestroyed()) { - webContents.send("netcatty:filewatch:synced", { - watchId, - localPath, - remotePath, - bytesWritten: content.length, - }); - } - - } catch (err) { - console.error(`[FileWatcher] Sync failed for ${localPath}:`, err.message); - - // Show system notification for sync failure - showSystemNotification( - "Netcatty", - `Failed to sync ${fileName}: ${err.message}` - ); - - // Notify the renderer about sync failure - if (webContents && !webContents.isDestroyed()) { - webContents.send("netcatty:filewatch:error", { - watchId, - localPath, - remotePath, - error: err.message, - }); - } + // Check if file was actually modified (compare mtime and size). + const stat = await fs.promises.stat(localPath); + if (stat.mtimeMs <= previousModified && stat.size === previousSize) { + console.log(`[FileWatcher] File unchanged (mtime and size same), skipping sync`); + return; } + + // Record the observed version before the upload. A save that lands while the + // stream is running triggers another watcher tick and is serialized below. + watchInfo.lastModified = stat.mtimeMs; + watchInfo.lastSize = stat.size; + + if (!sftpClients?.has(sftpId)) throw new Error("SFTP session not found or expired"); + if (!transferBridge?.startInternalTransfer) throw new Error("Transfer runtime not initialized"); + if (watchInfo.stopped || activeWatchers.get(watchId) !== watchInfo) return; + + const transferId = `filewatch-${watchId}-${++watchInfo.syncSequence}`; + watchInfo.activeTransferId = transferId; + console.log(`[FileWatcher] Streaming ${stat.size} bytes to ${remotePath}`); + try { + const result = await transferBridge.startInternalTransfer(null, { + transferId, + sourcePath: localPath, + targetPath: remotePath, + sourceType: "local", + targetType: "sftp", + targetSftpId: sftpId, + targetEncoding: encoding, + totalBytes: stat.size, + resumable: false, + sourceIsOwnedTemp: false, + }); + if (result?.cancelled || /cancel/i.test(result?.error || "")) { + throw new Error("Transfer cancelled"); + } + if (result?.error) throw new Error(result.error); + } 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}`); + notifyWatchOwners(watchInfo, "netcatty:filewatch:synced", { + watchId, + localPath, + remotePath, + bytesWritten: stat.size, + }); +} + +async function handleFileChange(watchId) { + const watchInfo = activeWatchers.get(watchId); + if (!watchInfo || watchInfo.stopped) return; + watchInfo.syncRequested = true; + if (watchInfo.syncPromise) return watchInfo.syncPromise; + + const syncPromise = (async () => { + while ( + watchInfo.syncRequested + && !watchInfo.stopped + && activeWatchers.get(watchId) === watchInfo + ) { + watchInfo.syncRequested = false; + try { + await syncFileChangeOnce(watchId, watchInfo); + } catch (err) { + // A newer save already queued behind this attempt will be retried; avoid + // flashing a stale failure for the superseded version. + if (watchInfo.stopped || activeWatchers.get(watchId) !== watchInfo) break; + if (watchInfo.syncRequested) continue; + const message = err?.message || String(err); + console.error(`[FileWatcher] Sync failed for ${watchInfo.localPath}:`, message); + showSystemNotification( + "Netcatty", + `Failed to sync ${path.basename(watchInfo.remotePath)}: ${message}`, + ); + notifyWatchOwners(watchInfo, "netcatty:filewatch:error", { + watchId, + localPath: watchInfo.localPath, + remotePath: watchInfo.remotePath, + error: message, + }); + } + } + })().finally(() => { + if (watchInfo.syncPromise === syncPromise) watchInfo.syncPromise = null; + }); + watchInfo.syncPromise = syncPromise; + return syncPromise; } /** * Stop watching a file and optionally clean up the temp file */ -function stopWatching(event, { watchId, cleanupTempFile = false }) { +function stopWatching(event, { + watchId, + cleanupTempFile = false, + force = false, + webContentsId, +}) { const watchInfo = activeWatchers.get(watchId); if (!watchInfo) { console.log(`[FileWatcher] Watch ID not found: ${watchId}`); return { success: false }; } + const ownerId = getOwnerId(event?.sender, webContentsId); + let tempFileCleanupQueued = false; + if (force) { + notifyWatchOwners(watchInfo, "netcatty:filewatch:stopped", { + watchId, + localPath: watchInfo.localPath, + remotePath: watchInfo.remotePath, + sftpId: watchInfo.sftpId, + }); + } + if (!force && ownerId != null) { + if (!watchInfo.owners.delete(ownerId)) return { success: false }; + if (cleanupTempFile && !hasOtherOwnedWatch( + watchInfo.sftpId, + watchInfo.localPath, + ownerId, + watchId, + )) { + tempFileCleanupQueued = releaseTempFileReference( + watchInfo.sftpId, + watchInfo.localPath, + ownerId, + { + cleanup: true, + }, + ); + } + if (watchInfo.owners.size > 0) return { success: true, retained: true }; + } + console.log(`[FileWatcher] Stopping watch: ${watchInfo.localPath}`); + watchInfo.stopped = true; + watchInfo.syncRequested = false; + const activeTransferId = watchInfo.activeTransferId; + watchInfo.activeTransferId = null; + if (activeTransferId && transferBridge?.cancelTransfer) { + void Promise.resolve(transferBridge.cancelTransfer(null, { transferId: activeTransferId })).catch(() => {}); + } // Clear debounce timer if any const timer = debounceTimers.get(watchId); @@ -290,7 +445,7 @@ function stopWatching(event, { watchId, cleanupTempFile = false }) { try { if (watchInfo.useWatchFile) { // Using fs.watchFile - need to use fs.unwatchFile - fs.unwatchFile(watchInfo.localPath); + fs.unwatchFile(watchInfo.localPath, watchInfo.listener); } else if (watchInfo.watcher) { // Using fs.watch - close the watcher watchInfo.watcher.close(); @@ -300,11 +455,25 @@ function stopWatching(event, { watchId, cleanupTempFile = false }) { } // Clean up temp file if requested - if (cleanupTempFile && watchInfo.localPath) { - cleanupTempFileAsync(watchInfo.localPath); + if ( + cleanupTempFile + && !tempFileCleanupQueued + && watchInfo.localPath + && !hasOtherOwnedWatch(watchInfo.sftpId, watchInfo.localPath, ownerId, watchId) + ) { + const releasedRegisteredFile = releaseTempFileReference( + watchInfo.sftpId, + watchInfo.localPath, + ownerId, + { force: force || ownerId == null, cleanup: true }, + ); + if (!releasedRegisteredFile) cleanupTempFileAsync(watchInfo.localPath); } activeWatchers.delete(watchId); + if (activeWatchIdsByKey.get(watchInfo.watchKey) === watchId) { + activeWatchIdsByKey.delete(watchInfo.watchKey); + } return { success: true }; } @@ -313,12 +482,27 @@ function stopWatching(event, { watchId, cleanupTempFile = false }) { * Asynchronously delete a temp file, logging success and silently handling failures */ async function cleanupTempFileAsync(filePath) { - try { - await fs.promises.unlink(filePath); - console.log(`[FileWatcher] Temp file cleaned up: ${filePath}`); - } catch (err) { - // Silently ignore deletion failures (file may be in use or already deleted) - console.log(`[FileWatcher] Could not delete temp file (may be in use): ${filePath}`); + for (let attempt = 0; ; attempt += 1) { + try { + await fs.promises.unlink(filePath); + console.log(`[FileWatcher] Temp file cleaned up: ${filePath}`); + return true; + } catch (err) { + if (err?.code === "ENOENT") return true; + const retryable = err?.code === "EBUSY" + || err?.code === "EPERM" + || err?.code === "EACCES"; + const delayMs = TEMP_FILE_CLEANUP_RETRY_DELAYS_MS[attempt]; + if (!retryable || delayMs == null) { + console.log(`[FileWatcher] Could not delete temp file (may be in use): ${filePath}`); + return false; + } + await new Promise((resolve) => { + const timer = setTimeout(resolve, delayMs); + // Cleanup is best-effort and must not keep Electron alive during quit. + timer.unref?.(); + }); + } } } @@ -328,11 +512,14 @@ async function cleanupTempFileAsync(filePath) { */ function stopWatchersForSession(sftpId, cleanupTempFiles = true) { let watcherCount = 0; + for (const pendingStart of pendingWatchStarts) { + if (pendingStart.sftpId === sftpId) pendingStart.cancelled = true; + } // Stop active watchers for (const [watchId, watchInfo] of activeWatchers.entries()) { if (watchInfo.sftpId === sftpId) { - stopWatching(null, { watchId, cleanupTempFile: cleanupTempFiles }); + stopWatching(null, { watchId, cleanupTempFile: cleanupTempFiles, force: true }); watcherCount++; } } @@ -344,7 +531,7 @@ function stopWatchersForSession(sftpId, cleanupTempFiles = true) { if (cleanupTempFiles && tempFilesMap.has(sftpId)) { const tempFiles = tempFilesMap.get(sftpId); let cleanedCount = 0; - for (const filePath of tempFiles) { + for (const filePath of tempFiles.keys()) { cleanupTempFileAsync(filePath); cleanedCount++; } @@ -355,6 +542,33 @@ function stopWatchersForSession(sftpId, cleanupTempFiles = true) { } } +function releaseOwnerResources(webContentsId, cleanupTempFiles = true) { + if (!Number.isSafeInteger(webContentsId)) return { removed: 0 }; + for (const pendingStart of pendingWatchStarts) { + if (pendingStart.ownerId === webContentsId) pendingStart.cancelled = true; + } + let removed = 0; + for (const [watchId, watchInfo] of [...activeWatchers]) { + if (!watchInfo.owners.has(webContentsId)) continue; + const result = stopWatching(null, { + watchId, + cleanupTempFile: false, + webContentsId, + }); + if (result.success) removed += 1; + } + for (const [sftpId, files] of [...tempFilesMap]) { + for (const [filePath, owners] of [...files]) { + if (!owners.has(webContentsId)) continue; + releaseTempFileReference(sftpId, filePath, webContentsId, { + cleanup: cleanupTempFiles, + }); + } + if (files.size === 0) tempFilesMap.delete(sftpId); + } + return { removed }; +} + /** * Get list of active watchers */ @@ -384,12 +598,31 @@ function registerHandlers(ipcMain, options = {}) { console.log("[FileWatcher] Registering IPC handlers"); const terminalWorkerManager = options.terminalWorkerManager || null; if (terminalWorkerManager) { - [ + const observedSenders = new WeakSet(); + const observeSender = (sender) => { + if (!sender || typeof sender !== "object" || observedSenders.has(sender)) return; + observedSenders.add(sender); + sender.once?.("destroyed", () => { + void terminalWorkerManager.request( + "netcatty:filewatch:releaseOwner", + { webContentsId: sender.id, cleanupTempFiles: true }, + { webContentsId: sender.id }, + ).catch(() => {}); + }); + }; + for (const channel of [ "netcatty:filewatch:start", - "netcatty:filewatch:stop", - "netcatty:filewatch:list", "netcatty:filewatch:registerTempFile", - ].forEach((channel) => registerWorkerHandle(ipcMain, terminalWorkerManager, channel)); + "netcatty:filewatch:unregisterTempFile", + ]) { + ipcMain.handle(channel, (event, payload) => { + observeSender(event?.sender); + return terminalWorkerManager.request(channel, payload, { webContentsId: event?.sender?.id }); + }); + } + for (const channel of ["netcatty:filewatch:stop", "netcatty:filewatch:list"]) { + registerWorkerHandle(ipcMain, terminalWorkerManager, channel); + } return; } ipcMain.handle("netcatty:filewatch:start", (event, args) => { @@ -398,10 +631,15 @@ function registerHandlers(ipcMain, options = {}) { }); ipcMain.handle("netcatty:filewatch:stop", stopWatching); ipcMain.handle("netcatty:filewatch:list", listWatchers); - ipcMain.handle("netcatty:filewatch:registerTempFile", (_event, { sftpId, localPath }) => { - registerTempFile(sftpId, localPath); + ipcMain.handle("netcatty:filewatch:registerTempFile", (event, { sftpId, localPath }) => { + observeOwnerSender(event?.sender); + registerTempFile(sftpId, localPath, getOwnerId(event?.sender)); return { success: true }; }); + ipcMain.handle("netcatty:filewatch:unregisterTempFile", unregisterTempFile); + ipcMain.handle("netcatty:filewatch:releaseOwner", (_event, payload = {}) => ( + releaseOwnerResources(payload.webContentsId, payload.cleanupTempFiles !== false) + )); } /** @@ -409,9 +647,11 @@ function registerHandlers(ipcMain, options = {}) { */ function cleanup() { console.log(`[FileWatcher] Cleaning up ${activeWatchers.size} watcher(s)`); + for (const pendingStart of pendingWatchStarts) pendingStart.cancelled = true; for (const [watchId] of activeWatchers.entries()) { - stopWatching(null, { watchId }); + stopWatching(null, { watchId, force: true, cleanupTempFile: true }); } + for (const [sftpId] of tempFilesMap) stopWatchersForSession(sftpId, true); } module.exports = { diff --git a/electron/bridges/fileWatcherBridge.test.cjs b/electron/bridges/fileWatcherBridge.test.cjs new file mode 100644 index 000000000..00d9f08bc --- /dev/null +++ b/electron/bridges/fileWatcherBridge.test.cjs @@ -0,0 +1,411 @@ +"use strict"; + +const assert = require("node:assert/strict"); +const EventEmitter = require("node:events"); +const fs = require("node:fs"); +const os = require("node:os"); +const path = require("node:path"); +const test = require("node:test"); + +const fileWatcherBridge = require("./fileWatcherBridge.cjs"); + +function createHarness(t) { + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-filewatch-test-")); + const localPath = path.join(tempDir, "edit.txt"); + fs.writeFileSync(localPath, "initial"); + + const listenersByPath = new Map(); + const originalWatchFile = fs.watchFile; + const originalUnwatchFile = fs.unwatchFile; + fs.watchFile = (filePath, _options, listener) => { + const listeners = listenersByPath.get(filePath) ?? new Set(); + listeners.add(listener); + listenersByPath.set(filePath, listeners); + }; + fs.unwatchFile = (filePath, listener) => { + const listeners = listenersByPath.get(filePath); + if (!listeners) return; + if (typeof listener === "function") listeners.delete(listener); + else listeners.clear(); + if (listeners.size === 0) listenersByPath.delete(filePath); + }; + + const handlers = new Map(); + fileWatcherBridge.init({ + sftpClients: new Map(), + electronModule: {}, + transferBridge: require("./transferBridge.cjs"), + }); + fileWatcherBridge.registerHandlers({ + handle(channel, handler) { + handlers.set(channel, handler); + }, + }); + + const sender = new EventEmitter(); + sender.id = Math.floor(Math.random() * 1_000_000) + 1; + sender.isDestroyed = () => false; + sender.send = () => {}; + const event = { sender }; + + t.after(() => { + fileWatcherBridge.stopWatchersForSession(`sftp-${sender.id}`, false); + fs.watchFile = originalWatchFile; + fs.unwatchFile = originalUnwatchFile; + fs.rmSync(tempDir, { recursive: true, force: true }); + }); + + return { + event, + handlers, + listenersByPath, + localPath, + sender, + sftpId: `sftp-${sender.id}`, + }; +} + +test("opening the same external edit twice reuses one polling watcher", async (t) => { + const harness = createHarness(t); + const payload = { + localPath: harness.localPath, + remotePath: "/remote/edit.txt", + sftpId: harness.sftpId, + encoding: "utf-8", + }; + + const first = await harness.handlers.get("netcatty:filewatch:start")(harness.event, payload); + const second = await harness.handlers.get("netcatty:filewatch:start")(harness.event, payload); + const listed = await harness.handlers.get("netcatty:filewatch:list")(harness.event, {}); + + assert.equal(second.watchId, first.watchId); + assert.equal(harness.listenersByPath.get(harness.localPath)?.size, 1); + assert.equal(listed.length, 1); +}); + +test("concurrent identical starts still install only one polling watcher", async (t) => { + const harness = createHarness(t); + const payload = { + localPath: harness.localPath, + remotePath: "/remote/concurrent.txt", + sftpId: harness.sftpId, + encoding: "utf-8", + }; + + const [first, second] = await Promise.all([ + harness.handlers.get("netcatty:filewatch:start")(harness.event, payload), + harness.handlers.get("netcatty:filewatch:start")(harness.event, payload), + ]); + + assert.equal(second.watchId, first.watchId); + assert.equal(harness.listenersByPath.get(harness.localPath)?.size, 1); +}); + +test("stopping one watch removes only its listener from a shared local path", async (t) => { + const harness = createHarness(t); + const first = await harness.handlers.get("netcatty:filewatch:start")(harness.event, { + localPath: harness.localPath, + remotePath: "/remote/first.txt", + sftpId: harness.sftpId, + encoding: "utf-8", + }); + const second = await harness.handlers.get("netcatty:filewatch:start")(harness.event, { + localPath: harness.localPath, + remotePath: "/remote/second.txt", + sftpId: harness.sftpId, + encoding: "utf-8", + }); + + assert.notEqual(first.watchId, second.watchId); + assert.equal(harness.listenersByPath.get(harness.localPath)?.size, 2); + + await harness.handlers.get("netcatty:filewatch:stop")(harness.event, { watchId: first.watchId }); + const listed = await harness.handlers.get("netcatty:filewatch:list")(harness.event, {}); + + assert.equal(harness.listenersByPath.get(harness.localPath)?.size, 1); + assert.deepEqual(listed.map((watch) => watch.watchId), [second.watchId]); +}); + +test("destroying the owner window releases its external file watches", async (t) => { + const harness = createHarness(t); + await harness.handlers.get("netcatty:filewatch:start")(harness.event, { + localPath: harness.localPath, + remotePath: "/remote/edit.txt", + sftpId: harness.sftpId, + encoding: "utf-8", + }); + + harness.sender.emit("destroyed"); + await new Promise((resolve) => setImmediate(resolve)); + + const listed = await harness.handlers.get("netcatty:filewatch:list")(harness.event, {}); + assert.equal(listed.length, 0); + assert.equal(harness.listenersByPath.has(harness.localPath), false); +}); + +test("closing an SFTP session releases every watcher for that session", async (t) => { + const harness = createHarness(t); + const stoppedEvents = []; + harness.sender.send = (channel, payload) => { + if (channel === "netcatty:filewatch:stopped") stoppedEvents.push(payload); + }; + await harness.handlers.get("netcatty:filewatch:start")(harness.event, { + localPath: harness.localPath, + remotePath: "/remote/first.txt", + sftpId: harness.sftpId, + encoding: "utf-8", + }); + await harness.handlers.get("netcatty:filewatch:start")(harness.event, { + localPath: harness.localPath, + remotePath: "/remote/second.txt", + sftpId: harness.sftpId, + encoding: "utf-8", + }); + + fileWatcherBridge.stopWatchersForSession(harness.sftpId, false); + + const listed = await harness.handlers.get("netcatty:filewatch:list")(harness.event, {}); + assert.equal(listed.length, 0); + assert.equal(harness.listenersByPath.has(harness.localPath), false); + assert.equal(stoppedEvents.length, 2); + assert.deepEqual(new Set(stoppedEvents.map((event) => event.sftpId)), new Set([harness.sftpId])); +}); + +test("worker proxy releases renderer-owned watchers when its window is destroyed", async () => { + const handlers = new Map(); + const requests = []; + const terminalWorkerManager = { + request(channel, payload, options) { + requests.push({ channel, payload, options }); + return Promise.resolve(channel === "netcatty:filewatch:start" ? { watchId: "worker-watch" } : null); + }, + }; + fileWatcherBridge.registerHandlers({ + handle(channel, handler) { + handlers.set(channel, handler); + }, + }, { terminalWorkerManager }); + const sender = new EventEmitter(); + sender.id = 72; + const event = { sender }; + + await handlers.get("netcatty:filewatch:start")(event, { + localPath: "/tmp/edit.txt", + remotePath: "/remote/edit.txt", + sftpId: "sftp-worker", + }); + sender.emit("destroyed"); + await new Promise((resolve) => setImmediate(resolve)); + + assert.deepEqual(requests.at(-1), { + channel: "netcatty:filewatch:releaseOwner", + payload: { webContentsId: 72, cleanupTempFiles: true }, + options: { webContentsId: 72 }, + }); +}); + +test("session close cancels a watch whose initial stat is still pending", async (t) => { + const harness = createHarness(t); + const originalStat = fs.promises.stat; + let finishStat; + fs.promises.stat = () => new Promise((resolve) => { + finishStat = () => resolve({ mtimeMs: 1, size: 7 }); + }); + t.after(() => { fs.promises.stat = originalStat; }); + + const starting = harness.handlers.get("netcatty:filewatch:start")(harness.event, { + localPath: harness.localPath, + remotePath: "/remote/pending.txt", + sftpId: harness.sftpId, + encoding: "utf-8", + }); + await new Promise((resolve) => setImmediate(resolve)); + fileWatcherBridge.stopWatchersForSession(harness.sftpId, true); + finishStat(); + + await assert.rejects(starting, /cancelled|closed/i); + const listed = await harness.handlers.get("netcatty:filewatch:list")(harness.event, {}); + assert.equal(listed.length, 0); + 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) => { + const harness = createHarness(t); + const transferBridge = require("./transferBridge.cjs"); + const originalStartInternalTransfer = transferBridge.startInternalTransfer; + const originalReadFile = fs.promises.readFile; + let directPutCalls = 0; + let wholeFileReadCalls = 0; + const internalTransfers = []; + const sent = []; + + transferBridge.startInternalTransfer = async (_event, payload) => { + internalTransfers.push(payload); + return { transferId: payload.transferId }; + }; + fs.promises.readFile = async (...args) => { + wholeFileReadCalls += 1; + return originalReadFile(...args); + }; + harness.sender.send = (channel, payload) => { + sent.push({ channel, payload }); + }; + fileWatcherBridge.init({ + sftpClients: new Map([[ + harness.sftpId, + { + put: async () => { directPutCalls += 1; }, + }, + ]]), + electronModule: {}, + transferBridge, + }); + t.after(() => { + transferBridge.startInternalTransfer = originalStartInternalTransfer; + fs.promises.readFile = originalReadFile; + }); + + await harness.handlers.get("netcatty:filewatch:start")(harness.event, { + localPath: harness.localPath, + remotePath: "/remote/edit.txt", + sftpId: harness.sftpId, + encoding: "utf-8", + }); + const listener = [...harness.listenersByPath.get(harness.localPath)][0]; + const previousStat = fs.statSync(harness.localPath); + fs.writeFileSync(harness.localPath, "modified external edit"); + const currentStat = fs.statSync(harness.localPath); + listener( + currentStat, + previousStat, + ); + + const deadline = Date.now() + 2_000; + while (!sent.some((event) => event.channel === "netcatty:filewatch:synced")) { + if (Date.now() >= deadline) throw new Error("Timed out waiting for external edit sync"); + await new Promise((resolve) => setTimeout(resolve, 10)); + } + + assert.equal(internalTransfers.length, 1); + assert.equal(internalTransfers[0].sourcePath, harness.localPath); + assert.equal(internalTransfers[0].targetPath, "/remote/edit.txt"); + assert.equal(internalTransfers[0].sourceType, "local"); + assert.equal(internalTransfers[0].targetType, "sftp"); + assert.equal(internalTransfers[0].targetSftpId, harness.sftpId); + assert.equal(wholeFileReadCalls, 0); + assert.equal(directPutCalls, 0); +}); + +test("closing the SFTP session cancels an active external edit stream", async (t) => { + const harness = createHarness(t); + const transferBridge = require("./transferBridge.cjs"); + const originalStartInternalTransfer = transferBridge.startInternalTransfer; + const originalCancelTransfer = transferBridge.cancelTransfer; + let activeTransferId; + let finishTransfer; + const cancelledTransferIds = []; + + transferBridge.startInternalTransfer = async (_event, payload) => { + activeTransferId = payload.transferId; + return new Promise((resolve) => { + finishTransfer = resolve; + }); + }; + transferBridge.cancelTransfer = async (_event, payload) => { + cancelledTransferIds.push(payload.transferId); + finishTransfer?.({ transferId: payload.transferId, cancelled: true, error: "Transfer cancelled" }); + return { success: true }; + }; + fileWatcherBridge.init({ + sftpClients: new Map([[harness.sftpId, {}]]), + electronModule: {}, + transferBridge, + }); + t.after(() => { + transferBridge.startInternalTransfer = originalStartInternalTransfer; + transferBridge.cancelTransfer = originalCancelTransfer; + }); + + await harness.handlers.get("netcatty:filewatch:start")(harness.event, { + localPath: harness.localPath, + remotePath: "/remote/edit.txt", + sftpId: harness.sftpId, + encoding: "utf-8", + }); + const listener = [...harness.listenersByPath.get(harness.localPath)][0]; + const previousStat = fs.statSync(harness.localPath); + fs.writeFileSync(harness.localPath, "modified before close"); + listener(fs.statSync(harness.localPath), previousStat); + + const startDeadline = Date.now() + 2_000; + while (!activeTransferId) { + if (Date.now() >= startDeadline) throw new Error("Timed out waiting for external edit stream"); + await new Promise((resolve) => setTimeout(resolve, 10)); + } + + fileWatcherBridge.stopWatchersForSession(harness.sftpId, false); + const cancelDeadline = Date.now() + 1_000; + while (cancelledTransferIds.length === 0) { + if (Date.now() >= cancelDeadline) throw new Error("Timed out waiting for stream cancellation"); + await new Promise((resolve) => setTimeout(resolve, 10)); + } + assert.deepEqual(cancelledTransferIds, [activeTransferId]); +}); + +test("session cleanup retries a temporarily locked external edit file", async (t) => { + const harness = createHarness(t); + const originalUnlink = fs.promises.unlink; + let unlinkAttempts = 0; + fs.promises.unlink = async (filePath) => { + if (filePath !== harness.localPath) return originalUnlink(filePath); + unlinkAttempts += 1; + if (unlinkAttempts === 1) { + const error = new Error("file is still in use"); + error.code = "EBUSY"; + throw error; + } + return originalUnlink(filePath); + }; + t.after(() => { fs.promises.unlink = originalUnlink; }); + + await harness.handlers.get("netcatty:filewatch:registerTempFile")(harness.event, { + sftpId: harness.sftpId, + localPath: harness.localPath, + }); + await harness.handlers.get("netcatty:filewatch:start")(harness.event, { + localPath: harness.localPath, + remotePath: "/remote/edit.txt", + sftpId: harness.sftpId, + encoding: "utf-8", + }); + + fileWatcherBridge.stopWatchersForSession(harness.sftpId, true); + + const deadline = Date.now() + 2_000; + while (Date.now() < deadline) { + try { + await fs.promises.stat(harness.localPath); + } catch (error) { + if (error?.code === "ENOENT") break; + throw error; + } + await new Promise((resolve) => setTimeout(resolve, 10)); + } + assert.ok(unlinkAttempts >= 2, "a transient Windows file lock must be retried"); + await assert.rejects(fs.promises.stat(harness.localPath), { code: "ENOENT" }); +}); + +test("failed external app launch can unregister and immediately delete its temp file", async (t) => { + const harness = createHarness(t); + await harness.handlers.get("netcatty:filewatch:registerTempFile")(harness.event, { + sftpId: harness.sftpId, + localPath: harness.localPath, + }); + const result = await harness.handlers.get("netcatty:filewatch:unregisterTempFile")(harness.event, { + sftpId: harness.sftpId, + localPath: harness.localPath, + }); + assert.equal(result.success, true); + assert.equal(result.retained, false); + await assert.rejects(fs.promises.stat(harness.localPath), { code: "ENOENT" }); +}); diff --git a/electron/bridges/localFsBridge.cjs b/electron/bridges/localFsBridge.cjs index fea2de1a6..6f8a89a14 100644 --- a/electron/bridges/localFsBridge.cjs +++ b/electron/bridges/localFsBridge.cjs @@ -10,6 +10,41 @@ const { execFile } = require("node:child_process"); const { promisify } = require("node:util"); const execFileAsync = promisify(execFile); +const MAX_LOCAL_TREE_DIRECTORIES = 50_000; +const MAX_LOCAL_TREE_ENTRIES = 200_000; +const WINDOWS_ATTRIB_TIMEOUT_MS = 15_000; + +function normalizeLocalTreeLimit(value, fallback) { + return Number.isSafeInteger(value) && value >= 0 ? value : fallback; +} + +function createLocalTreeTraversalBudget(limits = {}) { + return { + directories: 0, + entries: 0, + maxDirectories: normalizeLocalTreeLimit(limits.maxDirectories, MAX_LOCAL_TREE_DIRECTORIES), + maxEntries: normalizeLocalTreeLimit(limits.maxEntries, MAX_LOCAL_TREE_ENTRIES), + }; +} + +function claimLocalTreeDirectory(budget) { + if (budget.directories >= budget.maxDirectories) { + throw new Error( + `Local directory traversal directory limit exceeded (${budget.maxDirectories}). Select a smaller folder to upload.`, + ); + } + budget.directories += 1; +} + +function accountLocalTreeEntries(budget, count) { + const next = budget.entries + Math.max(0, Number(count) || 0); + if (next > budget.maxEntries) { + throw new Error( + `Local directory traversal entry limit exceeded (${budget.maxEntries}). Select a smaller folder to upload.`, + ); + } + budget.entries = next; +} /** * Parse the output of `attrib.exe \*` into a set of basenames whose @@ -72,6 +107,7 @@ async function listWindowsHiddenBasenames(dirPath) { // that passed each entry's full path directly). const { stdout } = await execFileAsync("attrib.exe", [pattern, "/d"], { maxBuffer: 64 * 1024 * 1024, + timeout: WINDOWS_ATTRIB_TIMEOUT_MS, windowsHide: true, }); return parseAttribOutput(stdout); @@ -271,13 +307,16 @@ async function statLocal(event, payload) { }; } -async function collectLocalTreeEntries(rootPath) { +async function collectLocalTreeEntries(rootPath, limits = {}) { const rootStat = await fs.promises.stat(rootPath); if (!rootStat.isDirectory()) { throw new Error("Selected path is not a directory"); } + const traversalBudget = createLocalTreeTraversalBudget(limits); + claimLocalTreeDirectory(traversalBudget); const rootName = path.basename(rootPath); + const rootRealPath = await fs.promises.realpath(rootPath); const entries = [{ localPath: rootPath, relativePath: rootName, @@ -285,29 +324,76 @@ async function collectLocalTreeEntries(rootPath) { size: rootStat.size, lastModified: rootStat.mtime.getTime(), }]; - const queue = [{ localPath: rootPath, relativePath: rootName }]; + const queue = [{ + localPath: rootPath, + relativePath: rootName, + ancestorRealPaths: new Set([rootRealPath]), + }]; + let queueIndex = 0; - while (queue.length > 0) { - const current = queue.shift(); + while (queueIndex < queue.length) { + const current = queue[queueIndex++]; const children = await fs.promises.readdir(current.localPath, { withFileTypes: true }); + accountLocalTreeEntries(traversalBudget, children.length); children.sort((a, b) => a.name.localeCompare(b.name)); - for (const child of children) { - const childPath = path.join(current.localPath, child.name); - const childRelativePath = `${current.relativePath}/${child.name}`; - const stat = await fs.promises.stat(childPath); - const isDirectory = stat.isDirectory(); + const metadataConcurrency = 32; + for (let start = 0; start < children.length; start += metadataConcurrency) { + const inspected = await Promise.all( + children.slice(start, start + metadataConcurrency).map(async (child) => { + const childPath = path.join(current.localPath, child.name); + const childRelativePath = `${current.relativePath}/${child.name}`; + // Use lstat to distinguish links, then stat the target. Directory + // links retain the established folder-upload behavior, while the + // real-path ancestor chain prevents junction/symlink cycles. + const linkStat = await fs.promises.lstat(childPath); + const stat = linkStat.isSymbolicLink() + ? await fs.promises.stat(childPath).catch(() => linkStat) + : linkStat; + const isDirectory = stat.isDirectory(); + const realPath = isDirectory + ? await fs.promises.realpath(childPath) + : null; + const isCycle = !!realPath && current.ancestorRealPaths.has(realPath); + const ancestorRealPaths = realPath + ? new Set([...current.ancestorRealPaths, realPath]) + : current.ancestorRealPaths; + return { + childPath, + childRelativePath, + stat, + isDirectory, + isCycle, + ancestorRealPaths, + }; + }), + ); - entries.push({ - localPath: childPath, - relativePath: childRelativePath, - type: isDirectory ? "directory" : "file", - size: stat.size, - lastModified: stat.mtime.getTime(), - }); + // Promise.all preserves the sorted child order, so restart manifests stay + // deterministic while metadata I/O is parallelized. + for (const child of inspected) { + // Count every directory-shaped alias before cycle suppression. Otherwise + // a non-cyclic symlink fan-out can expand one real tree thousands of + // times without consuming the global directory budget. + if (child.isDirectory) claimLocalTreeDirectory(traversalBudget); + // Cyclic links cannot be represented by a finite copied tree. Skip the + // loop itself instead of misreporting it as a file that later fails. + if (child.isCycle) continue; + entries.push({ + localPath: child.childPath, + relativePath: child.childRelativePath, + type: child.isDirectory ? "directory" : "file", + size: child.stat.size, + lastModified: child.stat.mtime.getTime(), + }); - if (isDirectory) { - queue.push({ localPath: childPath, relativePath: childRelativePath }); + if (child.isDirectory) { + queue.push({ + localPath: child.childPath, + relativePath: child.childRelativePath, + ancestorRealPaths: child.ancestorRealPaths, + }); + } } } } @@ -403,6 +489,7 @@ function registerHandlers(ipcMain) { } module.exports = { + WINDOWS_ATTRIB_TIMEOUT_MS, registerHandlers, listLocalDir, readLocalFile, @@ -412,6 +499,9 @@ module.exports = { mkdirLocal, statLocal, collectLocalTreeEntries, + createLocalTreeTraversalBudget, + MAX_LOCAL_TREE_DIRECTORIES, + MAX_LOCAL_TREE_ENTRIES, listLocalTree, getHomeDir, listDrives, diff --git a/electron/bridges/localFsBridge.test.cjs b/electron/bridges/localFsBridge.test.cjs index ad3b18a71..9a7109fb6 100644 --- a/electron/bridges/localFsBridge.test.cjs +++ b/electron/bridges/localFsBridge.test.cjs @@ -7,10 +7,18 @@ const path = require("node:path"); const { collectLocalTreeEntries, + MAX_LOCAL_TREE_DIRECTORIES, + MAX_LOCAL_TREE_ENTRIES, + WINDOWS_ATTRIB_TIMEOUT_MS, parseAttribOutput, listWindowsHiddenBasenames, } = require("./localFsBridge.cjs"); +test("local tree traversal defaults match the remote traversal safety budget", () => { + assert.equal(MAX_LOCAL_TREE_DIRECTORIES, 50_000); + assert.equal(MAX_LOCAL_TREE_ENTRIES, 200_000); +}); + test("parseAttribOutput returns an empty set for empty input", () => { assert.equal(parseAttribOutput("").size, 0); assert.equal(parseAttribOutput("\r\n\r\n").size, 0); @@ -106,14 +114,16 @@ test("listWindowsHiddenBasenames invokes attrib.exe with /d so hidden directorie let capturedArgs = null; let capturedExecutable = null; + let capturedOptions = null; Module._load = function patchedLoad(request, parent, isMain) { if (request === "node:child_process") { return { ...realChildProcess, - execFile: (executable, args, _options, cb) => { + execFile: (executable, args, options, cb) => { capturedExecutable = executable; capturedArgs = args; + capturedOptions = options; cb(null, { stdout: "", stderr: "" }); }, }; @@ -144,6 +154,8 @@ test("listWindowsHiddenBasenames invokes attrib.exe with /d so hidden directorie Array.isArray(capturedArgs) && capturedArgs.includes("/d"), `expected /d in attrib args so hidden directories are included, got ${JSON.stringify(capturedArgs)}`, ); + assert.equal(capturedOptions.timeout, WINDOWS_ATTRIB_TIMEOUT_MS); + assert.equal(capturedOptions.maxBuffer, 64 * 1024 * 1024); }); test("collectLocalTreeEntries preserves empty directories in selected folders", async () => { @@ -171,6 +183,213 @@ test("collectLocalTreeEntries preserves empty directories in selected folders", } }); +test("collectLocalTreeEntries bounds and parallelizes metadata reads within a directory", async (t) => { + const root = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-upload-tree-parallel-")); + const selected = path.join(root, "project"); + await fs.promises.mkdir(selected, { recursive: true }); + await Promise.all(Array.from({ length: 64 }, (_, index) => ( + fs.promises.writeFile(path.join(selected, `file-${String(index).padStart(3, "0")}.txt`), "x") + ))); + + const originalLstat = fs.promises.lstat; + let active = 0; + let maxActive = 0; + fs.promises.lstat = async (...args) => { + active += 1; + maxActive = Math.max(maxActive, active); + try { + await new Promise((resolve) => setImmediate(resolve)); + return await originalLstat.apply(fs.promises, args); + } finally { + active -= 1; + } + }; + t.after(async () => { + fs.promises.lstat = originalLstat; + await fs.promises.rm(root, { recursive: true, force: true }); + }); + + const entries = await collectLocalTreeEntries(selected); + assert.equal(entries.length, 65); + assert.ok(maxActive > 1, `expected concurrent lstat calls, got ${maxActive}`); + assert.ok(maxActive <= 32, `metadata concurrency must stay bounded, got ${maxActive}`); +}); + +test("collectLocalTreeEntries does not follow a directory symlink cycle", async (t) => { + const root = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-upload-tree-cycle-")); + const selected = path.join(root, "project"); + const nested = path.join(selected, "nested"); + await fs.promises.mkdir(nested, { recursive: true }); + try { + await fs.promises.symlink(selected, path.join(nested, "back-to-project"), process.platform === "win32" ? "junction" : "dir"); + } catch (error) { + await fs.promises.rm(root, { recursive: true, force: true }); + t.skip(`symlink creation unavailable: ${error instanceof Error ? error.message : String(error)}`); + return; + } + + try { + const entries = await Promise.race([ + collectLocalTreeEntries(selected), + new Promise((_, reject) => setTimeout(() => reject(new Error("local tree scan followed a symlink cycle")), 500)), + ]); + const loopRows = entries.filter((entry) => entry.relativePath.endsWith("back-to-project")); + assert.equal(loopRows.length, 0, "cyclic directory links must be skipped instead of uploaded as files"); + assert.ok(entries.length <= 3, `cycle scan must stay bounded, got ${entries.length} rows`); + } finally { + await fs.promises.rm(root, { recursive: true, force: true }); + } +}); + +test("collectLocalTreeEntries counts a cyclic directory alias against the global budget", async (t) => { + const root = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-upload-tree-cycle-budget-")); + const selected = path.join(root, "project"); + const nested = path.join(selected, "nested"); + await fs.promises.mkdir(nested, { recursive: true }); + try { + await fs.promises.symlink(selected, path.join(nested, "back"), process.platform === "win32" ? "junction" : "dir"); + } catch (error) { + await fs.promises.rm(root, { recursive: true, force: true }); + t.skip(`symlink creation unavailable: ${error instanceof Error ? error.message : String(error)}`); + return; + } + + try { + await assert.rejects( + collectLocalTreeEntries(selected, { maxDirectories: 2, maxEntries: 10 }), + /Local directory traversal directory limit exceeded \(2\).*smaller folder/i, + ); + } finally { + await fs.promises.rm(root, { recursive: true, force: true }); + } +}); + +test("collectLocalTreeEntries bounds non-cyclic directory symlink fan-out", async (t) => { + const root = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-upload-tree-fanout-")); + const selected = path.join(root, "project"); + const shared = path.join(root, "shared"); + await fs.promises.mkdir(selected, { recursive: true }); + await fs.promises.mkdir(shared, { recursive: true }); + await fs.promises.writeFile(path.join(shared, "payload.txt"), "payload"); + try { + for (let index = 0; index < 4; index += 1) { + await fs.promises.symlink( + shared, + path.join(selected, `alias-${index}`), + process.platform === "win32" ? "junction" : "dir", + ); + } + } catch (error) { + await fs.promises.rm(root, { recursive: true, force: true }); + t.skip(`symlink creation unavailable: ${error instanceof Error ? error.message : String(error)}`); + return; + } + + try { + await assert.rejects( + collectLocalTreeEntries(selected, { maxDirectories: 4, maxEntries: 20 }), + /Local directory traversal directory limit exceeded \(4\).*smaller folder/i, + ); + } finally { + await fs.promises.rm(root, { recursive: true, force: true }); + } +}); + +test("collectLocalTreeEntries accepts exact directory and entry budget boundaries", async (t) => { + const root = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-upload-tree-budget-edge-")); + const selected = path.join(root, "project"); + await fs.promises.mkdir(path.join(selected, "nested"), { recursive: true }); + await fs.promises.writeFile(path.join(selected, "one.txt"), "1"); + await fs.promises.writeFile(path.join(selected, "nested", "two.txt"), "2"); + t.after(() => fs.promises.rm(root, { recursive: true, force: true })); + + const entries = await collectLocalTreeEntries(selected, { + maxDirectories: 2, + maxEntries: 3, + }); + assert.deepEqual(entries.map((entry) => entry.relativePath), [ + "project", + "project/nested", + "project/one.txt", + "project/nested/two.txt", + ]); +}); + +test("collectLocalTreeEntries rejects one entry beyond the global entry budget", async (t) => { + const root = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-upload-tree-entry-budget-")); + const selected = path.join(root, "project"); + await fs.promises.mkdir(selected, { recursive: true }); + await Promise.all([ + fs.promises.writeFile(path.join(selected, "one.txt"), "1"), + fs.promises.writeFile(path.join(selected, "two.txt"), "2"), + fs.promises.writeFile(path.join(selected, "three.txt"), "3"), + ]); + t.after(() => fs.promises.rm(root, { recursive: true, force: true })); + + await assert.rejects( + collectLocalTreeEntries(selected, { maxDirectories: 1, maxEntries: 2 }), + /Local directory traversal entry limit exceeded \(2\).*smaller folder/i, + ); +}); + +test("collectLocalTreeEntries follows a non-cyclic directory symlink for folder upload", async (t) => { + const root = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-upload-tree-directory-link-")); + const selected = path.join(root, "project"); + const target = path.join(root, "shared-assets"); + const link = path.join(selected, "assets"); + await fs.promises.mkdir(selected, { recursive: true }); + await fs.promises.mkdir(target, { recursive: true }); + await fs.promises.writeFile(path.join(target, "logo.txt"), "logo"); + try { + await fs.promises.symlink(target, link, process.platform === "win32" ? "junction" : "dir"); + } catch (error) { + await fs.promises.rm(root, { recursive: true, force: true }); + t.skip(`symlink creation unavailable: ${error instanceof Error ? error.message : String(error)}`); + return; + } + + try { + const entries = await collectLocalTreeEntries(selected); + const linkedDirectory = entries.find((entry) => entry.relativePath === "project/assets"); + const linkedFile = entries.find((entry) => entry.relativePath === "project/assets/logo.txt"); + assert.equal(linkedDirectory?.type, "directory"); + assert.equal(linkedFile?.type, "file"); + assert.equal(linkedFile?.localPath, path.join(link, "logo.txt")); + } finally { + await fs.promises.rm(root, { recursive: true, force: true }); + } +}); + +test("collectLocalTreeEntries uses the target metadata for a leaf file symlink", async (t) => { + const root = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-upload-tree-file-link-")); + const selected = path.join(root, "project"); + const target = path.join(root, "target.bin"); + const link = path.join(selected, "linked.bin"); + await fs.promises.mkdir(selected, { recursive: true }); + await fs.promises.writeFile(target, "target-file-contents"); + try { + await fs.promises.symlink(target, link, "file"); + } catch (error) { + await fs.promises.rm(root, { recursive: true, force: true }); + t.skip(`symlink creation unavailable: ${error instanceof Error ? error.message : String(error)}`); + return; + } + + try { + const targetStat = await fs.promises.stat(target); + const linkStat = await fs.promises.lstat(link); + assert.notEqual(linkStat.size, targetStat.size, "fixture must distinguish link metadata from target metadata"); + + const entries = await collectLocalTreeEntries(selected); + const linked = entries.find((entry) => entry.relativePath === "project/linked.bin"); + assert.equal(linked?.type, "file"); + assert.equal(linked?.size, targetStat.size); + assert.equal(linked?.lastModified, targetStat.mtime.getTime()); + } finally { + await fs.promises.rm(root, { recursive: true, force: true }); + } +}); + test("readLocalFile returns only the trailing maxBytes when requested", async () => { const { readLocalFile } = require("./localFsBridge.cjs"); const root = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-local-read-")); diff --git a/electron/bridges/mcpServerBridge.cjs b/electron/bridges/mcpServerBridge.cjs index edbd1eb9b..c84cf5503 100644 --- a/electron/bridges/mcpServerBridge.cjs +++ b/electron/bridges/mcpServerBridge.cjs @@ -339,6 +339,19 @@ function init(deps) { } } +async function listActivePortForwards() { + if (terminalWorkerManager?.request) { + const tunnels = await terminalWorkerManager.request( + "netcatty:portforward:list", + {}, + {}, + ); + return Array.isArray(tunnels) ? tunnels : []; + } + const tunnels = await portForwardingBridge.listPortForwards(); + return Array.isArray(tunnels) ? tunnels : []; +} + function writeCliDiscoveryFile() { if (!tcpPort || !authToken || !cliDiscoveryFilePath) return; const payload = { @@ -978,15 +991,20 @@ function handleConnection(socket) { socket.destroy(); return; } + const previousBufferLength = buffer.length; buffer += chunk; + let consumedUntil = 0; + let searchFrom = previousBufferLength; let newlineIdx; - while ((newlineIdx = buffer.indexOf("\n")) !== -1) { - const line = buffer.slice(0, newlineIdx); - buffer = buffer.slice(newlineIdx + 1); + while ((newlineIdx = buffer.indexOf("\n", searchFrom)) !== -1) { + const line = buffer.slice(consumedUntil, newlineIdx); + consumedUntil = newlineIdx + 1; + searchFrom = consumedUntil; if (!line.trim()) continue; debugLog("Incoming line", line); handleMessage(socket, line); } + if (consumedUntil > 0) buffer = buffer.slice(consumedUntil); }); socket.on("error", () => { @@ -1206,6 +1224,7 @@ const dispatchCapabilityRpc = createCapabilityRpcDispatcher({ isChatSessionCancelled, requestApprovalFromRenderer, USER_DENIED_MESSAGE, + listPortForwards: () => listActivePortForwards(), sessionService, captureHostOpenScope: (chatSessionId) => openedSessionOwnership.captureGeneration(chatSessionId), onHostOpened: (chatSessionId, sessionId, generation) => { @@ -1790,7 +1809,7 @@ async function handleGetContext(params) { let activePortForwardTunnels = []; try { - activePortForwardTunnels = await portForwardingBridge.listPortForwards() || []; + activePortForwardTunnels = await listActivePortForwards(); } catch { activePortForwardTunnels = []; } diff --git a/electron/bridges/mcpServerBridge.workerTerminal.test.cjs b/electron/bridges/mcpServerBridge.workerTerminal.test.cjs index 40f302407..319b3b7f8 100644 --- a/electron/bridges/mcpServerBridge.workerTerminal.test.cjs +++ b/electron/bridges/mcpServerBridge.workerTerminal.test.cjs @@ -10,13 +10,18 @@ function loadFreshBridge() { } test("MCP/Catty capability context uses scoped metadata when terminal sessions live in worker", async () => { + const requests = []; const bridge = loadFreshBridge(); bridge.init({ sessions: new Map(), electronModule: null, terminalWorkerManager: { - request() { - throw new Error("getContext should not need a worker round trip"); + request(channel, payload, options) { + requests.push({ channel, payload, options }); + if (channel === "netcatty:portforward:list") { + return Promise.resolve([{ tunnelId: "worker-pf-1", status: "active" }]); + } + throw new Error(`unexpected worker request: ${channel}`); }, }, }); @@ -41,6 +46,14 @@ test("MCP/Catty capability context uses scoped metadata when terminal sessions l assert.equal(result.tools.terminal.execute, "terminal_execute"); assert.equal(result.tools.terminal.start, "terminal_start"); assert.match(result.description, /terminal_execute/); + assert.deepEqual(result.activePortForwardTunnels, [ + { tunnelId: "worker-pf-1", status: "active" }, + ]); + assert.deepEqual(requests, [{ + channel: "netcatty:portforward:list", + payload: {}, + options: {}, + }]); assert.deepEqual(result.hosts[0], { sessionId: "ssh-1", hostname: "host.example", diff --git a/electron/bridges/mcpServerBridge/capabilityRpcDispatch.cjs b/electron/bridges/mcpServerBridge/capabilityRpcDispatch.cjs index cc606a8c6..8ea3e1ec8 100644 --- a/electron/bridges/mcpServerBridge/capabilityRpcDispatch.cjs +++ b/electron/bridges/mcpServerBridge/capabilityRpcDispatch.cjs @@ -88,7 +88,10 @@ function createCapabilityRpcDispatcher(deps) { } = deps; const vaultService = createVaultService({ invokeVaultAgent }); - const portforwardService = createPortForwardService({ invokeVaultAgent }); + const portforwardService = createPortForwardService({ + invokeVaultAgent, + listPortForwards: deps.listPortForwards, + }); const sessionService = deps.sessionService || createSessionService({ invokeSessionAgent: invokeVaultAgent, validateClose: deps.validateSessionClose, diff --git a/electron/bridges/mcpServerBridge/capabilityRpcDispatch.test.cjs b/electron/bridges/mcpServerBridge/capabilityRpcDispatch.test.cjs index cf31ec6a3..93ddc9281 100644 --- a/electron/bridges/mcpServerBridge/capabilityRpcDispatch.test.cjs +++ b/electron/bridges/mcpServerBridge/capabilityRpcDispatch.test.cjs @@ -267,6 +267,23 @@ test("dispatchCapabilityRpc routes portforward start to portforward service", as assert.equal(result.ruleId, "rule-1"); }); +test("dispatchCapabilityRpc lists port forwards from the configured runtime", async () => { + const calls = []; + const dispatch = createTestDispatcher({ + listPortForwards: async () => { + calls.push("list"); + return [{ tunnelId: "worker-pf", status: "active" }]; + }, + }); + + const result = await dispatch("portforward/tunnels/list", {}); + assert.deepEqual(result, { + ok: true, + tunnels: [{ tunnelId: "worker-pf", status: "active" }], + }); + assert.deepEqual(calls, ["list"]); +}); + test("dispatchCapabilityRpc reads permissionMode from deps on each call", async () => { const seenModes = []; const mutableDeps = { permissionMode: PERMISSION_MODES.CONFIRM }; diff --git a/electron/bridges/mcpServerBridge/sessionOwnership.cjs b/electron/bridges/mcpServerBridge/sessionOwnership.cjs index 5b9a50078..83ab600b3 100644 --- a/electron/bridges/mcpServerBridge/sessionOwnership.cjs +++ b/electron/bridges/mcpServerBridge/sessionOwnership.cjs @@ -6,16 +6,25 @@ function createSessionOwnershipRegistry() { function captureGeneration(chatSessionId) { if (!chatSessionId) return null; - return scopeGenerations.get(chatSessionId) || 0; + let generation = scopeGenerations.get(chatSessionId); + if (!generation) { + generation = { chatSessionId, revoked: false }; + scopeGenerations.set(chatSessionId, generation); + } + return generation; } function register(chatSessionId, sessionId, expectedGeneration = null) { if (!chatSessionId || !sessionId) return false; - if ( - expectedGeneration !== null - && expectedGeneration !== captureGeneration(chatSessionId) - ) { - return false; + if (expectedGeneration !== null) { + const currentGeneration = scopeGenerations.get(chatSessionId); + if ( + expectedGeneration.revoked + || expectedGeneration.chatSessionId !== chatSessionId + || currentGeneration !== expectedGeneration + ) { + return false; + } } const owned = ownedByScope.get(chatSessionId) || new Set(); owned.add(sessionId); @@ -43,10 +52,23 @@ function createSessionOwnershipRegistry() { function clearScope(chatSessionId) { ownedByScope.delete(chatSessionId); - scopeGenerations.set(chatSessionId, captureGeneration(chatSessionId) + 1); + const generation = scopeGenerations.get(chatSessionId); + if (generation) generation.revoked = true; + scopeGenerations.delete(chatSessionId); } - return { captureGeneration, register, validate, forgetSession, clearScope }; + function getTrackedGenerationCountForTests() { + return scopeGenerations.size; + } + + return { + captureGeneration, + register, + validate, + forgetSession, + clearScope, + getTrackedGenerationCountForTests, + }; } module.exports = { createSessionOwnershipRegistry }; diff --git a/electron/bridges/mcpServerBridge/sessionOwnership.test.cjs b/electron/bridges/mcpServerBridge/sessionOwnership.test.cjs index 34b4337ae..a7dfac8c8 100644 --- a/electron/bridges/mcpServerBridge/sessionOwnership.test.cjs +++ b/electron/bridges/mcpServerBridge/sessionOwnership.test.cjs @@ -44,3 +44,19 @@ test("a host open that finishes after scope cleanup cannot restore ownership", ( const nextGeneration = ownership.captureGeneration("chat-a"); assert.equal(ownership.register("chat-a", "session-2", nextGeneration), true); }); + +test("cleared scope generations are released without allowing a late host open", () => { + const ownership = createSessionOwnershipRegistry(); + const staleGenerations = []; + + for (let index = 0; index < 100; index += 1) { + const scopeId = `chat-${index}`; + staleGenerations.push([scopeId, ownership.captureGeneration(scopeId)]); + ownership.clearScope(scopeId); + } + + assert.equal(ownership.getTrackedGenerationCountForTests(), 0); + for (const [scopeId, generation] of staleGenerations) { + assert.equal(ownership.register(scopeId, `late-${scopeId}`, generation), false); + } +}); diff --git a/electron/bridges/portForwardingBridge.cancel.test.cjs b/electron/bridges/portForwardingBridge.cancel.test.cjs index 41eaf7807..c0a01c2f6 100644 --- a/electron/bridges/portForwardingBridge.cancel.test.cjs +++ b/electron/bridges/portForwardingBridge.cancel.test.cjs @@ -7,6 +7,12 @@ const { spawnSync } = require("node:child_process"); const passphraseHandler = require("./passphraseHandler.cjs"); const keyboardInteractiveHandler = require("./keyboardInteractiveHandler.cjs"); +const { + beginTransportDial, + failTransportDial, + getTransportStats, + resetSshTransportRegistryForTests, +} = require("./sshConnectionPool.cjs"); const { startPortForward, stopPortForward, @@ -18,6 +24,7 @@ const { publishTunnelStatus, shouldFinalizeTunnelClose, isReusableTunnelStatus, + _bindPortForwardChannelsForTests: bindPortForwardChannels, } = require("./portForwardingBridge.cjs"); function createEncryptedKey(t) { @@ -136,6 +143,103 @@ test("cancelling a tunnel clears its pending keyboard-interactive request", asyn assert.equal(keyboardInteractiveHandler.getRequests().has(requestId), false); }); +test("stopping a pending remote forward invalidates its connection immediately", async () => { + let finishForward; + let endCalls = 0; + let unforwardCalls = 0; + const conn = { + forwardIn(_bind, _port, callback) { + finishForward = callback; + }, + unforwardIn(_bind, _port, callback) { + unforwardCalls += 1; + callback(null); + }, + end() { endCalls += 1; }, + on() {}, + removeListener() {}, + }; + const tunnelId = "pf-remote-pending-stop"; + const tunnel = { + tunnelId, + conn, + status: "connecting", + cancelled: false, + sshTransportManaged: false, + chainConnections: [], + }; + const binding = bindPortForwardChannels({ + type: "remote", + conn, + tunnelId, + tunnelState: tunnel, + sender: createSender(), + bindAddress: "127.0.0.1", + localPort: 22022, + remoteHost: "127.0.0.1", + remotePort: 22, + chainConnections: [], + sendStatus() {}, + }); + + const stopping = cancelTunnel(tunnelId, tunnel, () => {}); + await new Promise((resolve) => setImmediate(resolve)); + assert.equal(unforwardCalls, 0); + assert.equal(endCalls, 1, "cancel must invalidate the transport that owns the pending callback"); + + finishForward(null); + assert.deepEqual(await binding, { tunnelId, success: false, cancelled: true }); + await stopping; + assert.equal(unforwardCalls, 0, "a late response on an invalidated transport must be ignored"); + assert.equal(endCalls, 1, "cleanup must not end the invalidated connection twice"); +}); + +test("stopping a remote forward whose bind callback hangs discards the connection", async () => { + let endCalls = 0; + let unforwardCalls = 0; + const conn = { + forwardIn() {}, + unforwardIn() { unforwardCalls += 1; }, + end() { endCalls += 1; }, + on() {}, + removeListener() {}, + }; + const tunnelId = "pf-remote-hung-bind-stop"; + const tunnel = { + tunnelId, + conn, + status: "connecting", + cancelled: false, + sshTransportManaged: false, + chainConnections: [], + _remoteForwardStartCleanupTimeoutMs: 5, + }; + const binding = bindPortForwardChannels({ + type: "remote", + conn, + tunnelId, + tunnelState: tunnel, + sender: createSender(), + bindAddress: "127.0.0.1", + localPort: 22023, + remoteHost: "127.0.0.1", + remotePort: 22, + chainConnections: [], + sendStatus() {}, + }); + + await Promise.race([ + cancelTunnel(tunnelId, tunnel, () => {}), + new Promise((_, reject) => setTimeout(() => reject(new Error("cancel did not settle")), 100)), + ]); + assert.equal(unforwardCalls, 0); + assert.equal(endCalls, 1); + + // The bind request itself remains pending because the fake server never + // calls back; it must not keep the test process alive. + void binding.catch(() => {}); +}); + test("port forwarding can be stopped while waiting for a key passphrase", async (t) => { const keyPath = createEncryptedKey(t); if (!keyPath) return; @@ -220,6 +324,62 @@ test("port forwarding can be stopped while waiting for a key passphrase", async }); }); +test("port forwarding can be stopped while waiting for a matching shared transport dial", async (t) => { + resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 }); + t.after(() => resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 })); + const publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAITestPublicKey"; + const endpoint = { + hostId: "host-shared-wait", + hostname: "shared-wait.test", + port: 22, + username: "alice", + jumpHosts: [], + proxy: null, + authType: "publickey", + keyId: "key-shared-wait", + certificate: "", + requiresMfa: false, + verifyHostKeys: false, + useSshAgent: false, + agentForwarding: false, + publicKey, + }; + const leader = beginTransportDial(endpoint, { kind: "channel" }); + t.after(() => failTransportDial(leader, new Error("test cleanup"))); + const tunnelId = "pf-shared-wait"; + const event = { sender: createSender() }; + + const started = startPortForward(event, { + tunnelId, + ruleId: "rule-shared-wait", + type: "local", + localPort: 0, + bindAddress: "127.0.0.1", + remoteHost: "127.0.0.1", + remotePort: 80, + hostname: endpoint.hostname, + hostId: endpoint.hostId, + port: endpoint.port, + username: endpoint.username, + authMethod: endpoint.authType, + keyId: endpoint.keyId, + publicKey, + verifyHostKeys: false, + useSshAgent: false, + }); + await new Promise((resolve) => setImmediate(resolve)); + + assert.equal(getTransportStats().pendingDials, 1, "PF joins the matching authenticated identity"); + assert.deepEqual(await getPortForwardStatus(event, { tunnelId }), { + tunnelId, + status: "connecting", + type: "local", + }); + assert.deepEqual(await stopPortForward(event, { tunnelId }), { tunnelId, success: true }); + assert.deepEqual(await started, { tunnelId, success: false, cancelled: true }); + assert.equal(getTransportStats().pendingDials, 1, "stopping a waiter does not cancel the dial leader"); +}); + test("port forwarding stops when the key passphrase prompt is cancelled", async (t) => { const keyPath = createEncryptedKey(t); if (!keyPath) return; diff --git a/electron/bridges/portForwardingBridge.cjs b/electron/bridges/portForwardingBridge.cjs index 306f83a7b..067b88ccf 100644 --- a/electron/bridges/portForwardingBridge.cjs +++ b/electron/bridges/portForwardingBridge.cjs @@ -12,6 +12,10 @@ const { connectThroughChain, buildAlgorithms } = require("./sshBridge.cjs"); const { resolveSshConnectionTimeouts } = require("./sshBridge/startSession.cjs"); const hostKeyVerifier = require("./hostKeyVerifier.cjs"); const { createProxySocket, runWhenProxyConnectionReady } = require("./proxyUtils.cjs"); +const { + openBoundedForwardInCallback, + openBoundedForwardOutCallback, +} = require("./boundedSshChannelOpen.cjs"); const { buildAuthHandler, createKeyboardInteractiveHandler, @@ -30,42 +34,36 @@ const { returnTransport, discardTransport, findTransportByEndpoint, + beginTransportDial, + waitForTransportDial, + completeTransportDial, + failTransportDial, + buildConnectionReuseEndpoint, + resolveConnectionKeepalivePolicy, LEASE_KINDS, } = require("./sshConnectionPool.cjs"); // Active port forwarding tunnels const portForwardingTunnels = new Map(); -function buildPortForwardEndpoint({ - hostId, hostname, port, username, jumpHosts, proxy, - authType, authMethod, keyId, identityId, certificate, requiresMfa, verifyHostKeys, useSshAgent, - agentForwarding, - password, privateKey, publicKey, passphrase, identityFilePaths, -}) { - return { - hostId: hostId || "", - hostname, - port: port || 22, - username: username || "root", - jumpHosts: Array.isArray(jumpHosts) ? jumpHosts : [], - proxy: proxy || null, - authType: authType || authMethod || "", - keyId: keyId || identityId || "", - certificate: certificate || "", - requiresMfa: !!requiresMfa, - verifyHostKeys, - useSshAgent, - // Port-forward never enables agent forwarding; record nofwd so a terminal - // transport with ForwardAgent enabled does not get cross-matched incorrectly - // only when the PF caller later wants a different policy (future-proof). - agentForwarding: !!agentForwarding, - // Credential material for digest fingerprint (not retained on transport). - password, - privateKey, - publicKey, - passphrase, - identityFilePaths, - }; +function buildPortForwardEndpoint(options = {}) { + return buildConnectionReuseEndpoint({ + ...options, + // Port forwarding never requests agent forwarding. Channel reuse remains + // asymmetric: a ForwardAgent terminal can serve PF, but not vice versa. + agentForwarding: false, + protocol: "ssh", + sftpSudo: false, + }, { sftpSudo: false }); +} + +function buildPortForwardEndpointFromStartPayload(payload = {}) { + return buildPortForwardEndpoint({ + ...payload, + authType: payload.authType || payload.authMethod, + keepaliveInterval: payload.resolvedKeepaliveInterval ?? payload.keepaliveInterval, + keepaliveCountMax: payload.resolvedKeepaliveCountMax ?? payload.keepaliveCountMax, + }); } function normalizeRemoteAddress(value) { @@ -101,7 +99,9 @@ function attachSharedTransportLifecycle(tunnelId, tunnelState, conn, sendStatus) // Errors mean the shared conn may be half-dead; discard so later // terminal/SFTP/PF work does not park and reuse a broken socket. if (discard && typeof discardTransport === "function") { - discardTransport(tunnel.connRef || tunnel, "shared-transport-error"); + if (tunnel.connRef) { + discardTransport(tunnel.connRef, "shared-transport-error"); + } } else { returnTransport(tunnel); } @@ -153,28 +153,62 @@ function isLocalBindFailure(err) { || message.includes("listen"); } -function trackTunnelPipe(tunnelState, socket, stream) { - if (!tunnelState) return; - if (!(tunnelState.activePipes instanceof Set)) tunnelState.activePipes = new Set(); - const entry = { socket, stream }; - tunnelState.activePipes.add(entry); - const drop = () => { - try { tunnelState.activePipes?.delete(entry); } catch { /* ignore */ } - }; - try { socket?.once?.("close", drop); } catch { /* ignore */ } +function destroyTunnelPipeEndpoint(endpoint) { + if (!endpoint) return; + try { endpoint.destroy?.(); } catch { /* ignore */ } + try { endpoint.close?.(); } catch { /* ignore */ } + try { endpoint.end?.(); } catch { /* ignore */ } +} + +function destroyTunnelPipeEntry(tunnelState, entry) { + if (!entry || entry.closed) return; + entry.closed = true; + const openAbortController = entry.openAbortController; + entry.openAbortController = null; + if (openAbortController && !openAbortController.signal.aborted) { + try { openAbortController.abort(new Error("Port forward client closed during SSH channel open")); } catch { /* ignore */ } + } + try { tunnelState?.activePipes?.delete(entry); } catch { /* ignore */ } + destroyTunnelPipeEndpoint(entry.socket); + destroyTunnelPipeEndpoint(entry.stream); +} + +function attachTunnelPipeStream(tunnelState, entry, stream) { + if (!entry || entry.closed || isTunnelCancelled(tunnelState)) { + destroyTunnelPipeEndpoint(entry?.socket); + destroyTunnelPipeEndpoint(stream); + if (entry) destroyTunnelPipeEntry(tunnelState, entry); + return false; + } + entry.openAbortController = null; + entry.stream = stream; + const drop = () => destroyTunnelPipeEntry(tunnelState, entry); try { stream?.once?.("close", drop); } catch { /* ignore */ } - try { socket?.once?.("error", drop); } catch { /* ignore */ } try { stream?.once?.("error", drop); } catch { /* ignore */ } + return true; +} + +function trackTunnelPipe(tunnelState, socket, stream = null) { + if (!tunnelState) return null; + if (!(tunnelState.activePipes instanceof Set)) tunnelState.activePipes = new Set(); + const entry = { + socket, + stream: null, + closed: false, + openAbortController: stream ? null : new AbortController(), + }; + tunnelState.activePipes.add(entry); + const drop = () => destroyTunnelPipeEntry(tunnelState, entry); + try { socket?.once?.("close", drop); } catch { /* ignore */ } + try { socket?.once?.("error", drop); } catch { /* ignore */ } + if (stream) attachTunnelPipeStream(tunnelState, entry, stream); + return entry; } function destroyTunnelPipes(tunnel) { if (!(tunnel?.activePipes instanceof Set)) return; - for (const entry of tunnel.activePipes) { - try { entry.socket?.destroy?.(); } catch { /* ignore */ } - try { entry.socket?.end?.(); } catch { /* ignore */ } - try { entry.stream?.close?.(); } catch { /* ignore */ } - try { entry.stream?.end?.(); } catch { /* ignore */ } - try { entry.stream?.destroy?.(); } catch { /* ignore */ } + for (const entry of [...tunnel.activePipes]) { + destroyTunnelPipeEntry(tunnel, entry); } tunnel.activePipes.clear(); } @@ -211,7 +245,7 @@ function releaseTunnelSsh(tunnel) { * Lets terminal/SFTP later borrow the same conn; stop returns the lease only. */ function attachForwardTransportLease(tunnel, conn, chainConnections, endpoint) { - if (!tunnel || !conn) return; + if (!tunnel || !conn) return null; const transport = createTransport({ conn, chainConnections: Array.isArray(chainConnections) ? chainConnections : [], @@ -227,10 +261,12 @@ function attachForwardTransportLease(tunnel, conn, chainConnections, endpoint) { tunnel.conn = conn; // Chain is owned by the transport registry now. tunnel.chainConnections = []; + return transport; } /** Max wait for remote-side unforwardIn before treating the listen as stuck. */ const UNFORWARD_TIMEOUT_MS = 5_000; +const REMOTE_FORWARD_START_CLEANUP_TIMEOUT_MS = 5_000; /** * Force-end a shared transport after unforward failure/timeout so a remote @@ -312,6 +348,53 @@ function unforwardRemoteListen(conn, bindAddress, port, transport = null) { }); } +function settleRemoteForwardStart(tunnel, outcome) { + tunnel._remoteForwardOutcome = outcome; + tunnel.pendingRemoteForward = false; + const resolve = tunnel._resolveRemoteForwardStart; + tunnel._resolveRemoteForwardStart = null; + resolve?.(outcome); +} + +function waitForRemoteForwardStart(tunnel) { + if (!tunnel?.pendingRemoteForward) { + return Promise.resolve(tunnel?._remoteForwardOutcome || { ok: false }); + } + const pending = tunnel._remoteForwardStartPromise; + if (!pending) return Promise.resolve({ ok: false }); + return new Promise((resolve) => { + let settled = false; + const finish = (outcome) => { + if (settled) return; + settled = true; + clearTimeout(timer); + resolve(outcome); + }; + const configuredTimeoutMs = Number(tunnel?._remoteForwardStartCleanupTimeoutMs); + const timeoutMs = Number.isFinite(configuredTimeoutMs) && configuredTimeoutMs >= 0 + ? configuredTimeoutMs + : REMOTE_FORWARD_START_CLEANUP_TIMEOUT_MS; + const timer = setTimeout(() => finish({ ok: false, timedOut: true }), timeoutMs); + timer.unref?.(); + pending.then(finish, (error) => finish({ ok: false, error })); + }); +} + +function unforwardRemoteListenOnce(tunnel, conn, bindAddress, port, transport = null) { + if (tunnel._remoteUnforwardPromise) return tunnel._remoteUnforwardPromise; + if (tunnel._remoteUnforwardDone) { + return Promise.resolve(tunnel._remoteUnforwardResult || { ok: true }); + } + const promise = unforwardRemoteListen(conn, bindAddress, port, transport) + .then((result) => { + tunnel._remoteUnforwardDone = true; + tunnel._remoteUnforwardResult = result; + return result; + }); + tunnel._remoteUnforwardPromise = promise; + return promise; +} + /** * Bind local/remote/dynamic forwarding onto an already-authenticated conn. * Used for both shared-transport and post-dial paths. @@ -331,6 +414,7 @@ function bindPortForwardChannels({ releaseOnError = false, endpoint = null, registerTransport = false, + dialCoordination = null, }) { return new Promise((resolve, reject) => { let settled = false; @@ -370,7 +454,9 @@ function bindPortForwardChannels({ if (type === "local") { const server = net.createServer((socket) => { - conn.forwardOut( + const pipeEntry = trackTunnelPipe(tunnelState, socket); + openBoundedForwardOutCallback( + conn, bindAddress, localPort, remoteHost, @@ -378,14 +464,15 @@ function bindPortForwardChannels({ (err, stream) => { if (err) { console.error(`[PortForward] Forward error:`, err.message); - socket.end(); + destroyTunnelPipeEntry(tunnelState, pipeEntry); return; } - trackTunnelPipe(tunnelState, socket, stream); + if (!attachTunnelPipeStream(tunnelState, pipeEntry, stream)) return; socket.pipe(stream).pipe(socket); socket.on("error", (e) => console.warn("[PortForward] Socket error:", e.message)); stream.on("error", (e) => console.warn("[PortForward] Stream error:", e.message)); }, + { signal: pipeEntry?.openAbortController?.signal }, ); }); @@ -414,7 +501,8 @@ function bindPortForwardChannels({ tunnelState.webContentsId = sender.id; tunnelState.pendingConn = null; if (registerTransport && endpoint && !tunnelState.sshTransportManaged) { - attachForwardTransportLease(tunnelState, conn, chainConnections, endpoint); + const transport = attachForwardTransportLease(tunnelState, conn, chainConnections, endpoint); + if (dialCoordination && transport) completeTransportDial(dialCoordination, transport); } portForwardingTunnels.set(tunnelId, tunnelState); sendStatus?.("active"); @@ -473,10 +561,21 @@ function bindPortForwardChannels({ tunnelState.remotePort = remotePort; tunnelState.pendingRemoteForward = true; tunnelState.conn = conn; + tunnelState._remoteForwardOutcome = null; + tunnelState._remoteForwardStartPromise = new Promise((resolve) => { + tunnelState._resolveRemoteForwardStart = resolve; + }); + tunnelState.remoteForwardAbortController = new AbortController(); - conn.forwardIn(bindAddress, localPort, (err) => { - tunnelState.pendingRemoteForward = false; + openBoundedForwardInCallback(conn, bindAddress, localPort, (err) => { + tunnelState.remoteForwardAbortController = null; + settleRemoteForwardStart(tunnelState, err ? { ok: false, error: err } : { ok: true }); if (err) { + if (isTunnelCancelled(tunnelState)) { + settled = true; + resolve({ tunnelId, success: false, cancelled: true }); + return; + } console.error(`[PortForward] Remote forward error:`, err.message); fail(err); return; @@ -488,21 +587,14 @@ function bindPortForwardChannels({ void (async () => { try { if (isTunnelCancelled(tunnelState)) { - // Cancel path may already have awaited unforward via cancelTunnel; - // only unforward again if stop did not clear the pending bind. - if (tunnelState._remoteUnforwardDone) { - settled = true; - resolve({ tunnelId, success: false, cancelled: true }); - return; - } const transport = tunnelState.connRef || null; - const unfwd = await unforwardRemoteListen( + const unfwd = await unforwardRemoteListenOnce( + tunnelState, conn, bindAddress, localPort, transport, ); - tunnelState._remoteUnforwardDone = true; if (unfwd.discarded || unfwd.timedOut) { // Transport already discarded; drop local refs so release does // not try to park a dead shared conn. @@ -522,7 +614,8 @@ function bindPortForwardChannels({ tunnelState.pendingConn = null; conn.on("tcp connection", onTcpConnection); if (registerTransport && endpoint && !tunnelState.sshTransportManaged) { - attachForwardTransportLease(tunnelState, conn, chainConnections, endpoint); + const transport = attachForwardTransportLease(tunnelState, conn, chainConnections, endpoint); + if (dialCoordination && transport) completeTransportDial(dialCoordination, transport); } portForwardingTunnels.set(tunnelId, tunnelState); sendStatus?.("active"); @@ -531,13 +624,13 @@ function bindPortForwardChannels({ } catch (regErr) { try { conn.removeListener("tcp connection", onTcpConnection); } catch { /* ignore */ } const transport = tunnelState.connRef || null; - const unfwd = await unforwardRemoteListen( + const unfwd = await unforwardRemoteListenOnce( + tunnelState, conn, bindAddress, localPort, transport, ); - tunnelState._remoteUnforwardDone = true; if (unfwd.discarded || unfwd.timedOut) { tunnelState.sshTransportManaged = false; tunnelState.conn = null; @@ -546,12 +639,13 @@ function bindPortForwardChannels({ fail(regErr); } })(); - }); + }, { signal: tunnelState.remoteForwardAbortController.signal }); return; } if (type === "dynamic") { const server = net.createServer((socket) => { + const pipeEntry = trackTunnelPipe(tunnelState, socket); socket.once("data", (data) => { if (data[0] !== 0x05) { socket.end(); @@ -586,7 +680,8 @@ function bindPortForwardChannels({ return; } - conn.forwardOut( + openBoundedForwardOutCallback( + conn, bindAddress, 0, targetHost, @@ -597,6 +692,7 @@ function bindPortForwardChannels({ socket.end(); return; } + if (!attachTunnelPipeStream(tunnelState, pipeEntry, stream)) return; const reply = Buffer.alloc(10); reply[0] = 0x05; reply[1] = 0x00; @@ -604,11 +700,11 @@ function bindPortForwardChannels({ reply[3] = 0x01; reply.writeUInt16BE(0, 8); socket.write(reply); - trackTunnelPipe(tunnelState, socket, stream); socket.pipe(stream).pipe(socket); socket.on("error", () => stream.end()); stream.on("error", () => socket.end()); }, + { signal: pipeEntry?.openAbortController?.signal }, ); }); }); @@ -637,7 +733,8 @@ function bindPortForwardChannels({ tunnelState.webContentsId = sender.id; tunnelState.pendingConn = null; if (registerTransport && endpoint && !tunnelState.sshTransportManaged) { - attachForwardTransportLease(tunnelState, conn, chainConnections, endpoint); + const transport = attachForwardTransportLease(tunnelState, conn, chainConnections, endpoint); + if (dialCoordination && transport) completeTransportDial(dialCoordination, transport); } portForwardingTunnels.set(tunnelId, tunnelState); sendStatus?.("active"); @@ -715,6 +812,34 @@ async function cancelTunnel(tunnelId, tunnel, sendStatus, { deleteEntry = false if (tunnel.passphraseAbortController && !tunnel.passphraseAbortController.signal.aborted) { cleanup('passphrase prompt', () => tunnel.passphraseAbortController.abort()); } + if (tunnel.transportWaitAbortController && !tunnel.transportWaitAbortController.signal.aborted) { + cleanup('shared SSH wait', () => tunnel.transportWaitAbortController.abort( + new Error("Port forward connection cancelled"), + )); + } + if (tunnel.remoteForwardAbortController && !tunnel.remoteForwardAbortController.signal.aborted) { + const transport = tunnel.connRef || null; + const abortedRemoteOpen = cleanup('remote forward open', () => tunnel.remoteForwardAbortController.abort( + new Error("Port forward connection cancelled"), + )); + if (abortedRemoteOpen) { + // forwardIn has no request-level cancellation in ssh2. The bounded open + // invalidates the physical connection so its retained callback cannot + // accumulate. Drop the tunnel's references as well: release cleanup must + // not end the same dedicated client twice or return the dead shared + // transport to the pool. + if (transport && typeof discardTransport === "function") { + cleanup('remote forward transport', () => discardTransport( + transport, + "pending-remote-forward-cancel", + )); + } + tunnel._remoteForwardTransportInvalidated = true; + tunnel.sshTransportManaged = false; + tunnel.conn = null; + tunnel.connRef = null; + } + } if (tunnel.pendingConn) { if (cleanup('pending SSH connection', () => tunnel.pendingConn.end())) tunnel.pendingConn = null; } @@ -727,6 +852,27 @@ async function cancelTunnel(tunnelId, tunnel, sendStatus, { deleteEntry = false tunnel.tcpConnectionHandler = null; }); } + if (tunnel.pendingRemoteForward && tunnel._remoteForwardStartPromise) { + const startOutcome = await waitForRemoteForwardStart(tunnel); + if (tunnel._remoteForwardTransportInvalidated) { + tunnel.pendingRemoteForward = false; + } + if (startOutcome?.timedOut) { + const transport = tunnel.connRef || null; + discardUnforwardTransport( + tunnel.conn, + transport, + "pending-remote-forward-timeout", + ); + tunnel._remoteForwardOutcome = startOutcome; + tunnel._remoteUnforwardDone = true; + tunnel._remoteUnforwardResult = { ok: true, discarded: true, timedOut: true }; + tunnel.pendingRemoteForward = false; + tunnel.sshTransportManaged = false; + tunnel.conn = null; + tunnel.connRef = null; + } + } // Remote forwards leave a server-side listen until unforwardIn succeeds. // Also cover cancel-during-forwardIn: bind target is recorded before the // ssh2 callback so we never skip unforward just because status was still @@ -737,16 +883,20 @@ async function cancelTunnel(tunnelId, tunnel, sendStatus, { deleteEntry = false && typeof tunnel.conn.unforwardIn === "function" && Number.isFinite(tunnel.localPort) && !tunnel._remoteUnforwardDone + && ( + tunnel._remoteForwardOutcome?.ok === true + || !tunnel._remoteForwardStartPromise + ) ) { const bind = tunnel.bindAddress || "127.0.0.1"; const port = tunnel.localPort; - const unfwd = await unforwardRemoteListen( + const unfwd = await unforwardRemoteListenOnce( + tunnel, tunnel.conn, bind, port, tunnel.connRef || null, ); - tunnel._remoteUnforwardDone = true; tunnel.pendingRemoteForward = false; if (unfwd.discarded || unfwd.timedOut) { // Transport already discarded inside the helper; clear local refs so @@ -774,15 +924,10 @@ async function cancelTunnel(tunnelId, tunnel, sendStatus, { deleteEntry = false } catch { /* ignore */ } tunnel.pendingRemoteForward = false; } - if (errors.length > 0) { - const error = errors.join('; '); - tunnel.status = 'error'; - tunnel.error = error; - tunnel.cleanupFailed = true; - tunnel.cleanupInProgress = false; - sendStatus?.('error', error); - throw new Error(error); - } + // Keep cleaning the SSH connection even when an earlier resource (for + // example the local listener) failed to close. Throw only after every + // independent cleanup step has had a chance to run; otherwise a listener + // error strands the underlying SSH socket indefinitely. if (tunnel.sshTransportManaged) { if (cleanup('SSH transport lease', () => releaseTunnelSsh(tunnel))) { /* lease returned */ @@ -834,9 +979,11 @@ async function startPortForward(event, payload) { port = 22, username, authMethod, + authPolicyVersion, requiresMfa, password, privateKey, + publicKey, certificate, keyId, passphrase, @@ -858,6 +1005,7 @@ async function startPortForward(event, payload) { keepaliveCountMax: resolvedKeepaliveCountMax, sshTcpConnectTimeoutMs, sshAuthReadyTimeoutMs, + reuseTransport = true, } = payload; // The rule is the durable identity; tunnelId is only one renderer's @@ -908,35 +1056,79 @@ async function startPortForward(event, payload) { let chainConnections = []; let connectionSocket = null; const passphraseAbortController = new AbortController(); - const reuseEndpoint = buildPortForwardEndpoint({ - hostId, hostname, port, username, jumpHosts, proxy, - authType: authMethod, keyId, certificate, requiresMfa, verifyHostKeys, useSshAgent, - password, privateKey, passphrase, identityFilePaths, - }); + const transportWaitAbortController = new AbortController(); + const tunnelState = { + type, + tunnelId, + conn: null, + pendingConn: null, + server: null, + chainConnections, + passphraseAbortController, + ruleId, + status: 'connecting', + webContentsId: sender.id, + subscribers: new Map([[sender.id, sender]]), + cancelled: false, + sshTransportManaged: false, + }; + const sendStatus = (status, error = null) => { + publishTunnelStatus(tunnelId, tunnelState, status, error); + }; + // Publish before waiting for another opener's physical dial. A stop request + // must be able to find and cancel this waiter immediately. + portForwardingTunnels.set(tunnelId, tunnelState); + sendStatus('connecting'); + const reuseEndpoint = buildPortForwardEndpointFromStartPayload(payload); + + // Atomically join a compatible physical dial when no authenticated + // transport exists yet. Explicitly dedicated forwards remain isolated and + // are not published into the shared pool. + let pendingDialCoordination = null; + let existingTransport = reuseTransport !== false + ? findTransportByEndpoint(reuseEndpoint) + : null; + try { + if (!existingTransport && reuseTransport !== false && typeof beginTransportDial === "function") { + const coordination = beginTransportDial(reuseEndpoint, { kind: "channel" }); + if (coordination.role === "reuse") { + existingTransport = coordination.transport; + } else if (coordination.role === "join") { + tunnelState.transportWaitAbortController = transportWaitAbortController; + existingTransport = await waitForTransportDial(coordination, { + signal: transportWaitAbortController.signal, + }); + tunnelState.transportWaitAbortController = null; + } else { + pendingDialCoordination = coordination; + } + } + } catch (error) { + if (isTunnelCancelled(tunnelState) || transportWaitAbortController.signal.aborted) { + portForwardingTunnels.delete(tunnelId); + return { tunnelId, success: false, cancelled: true }; + } + portForwardingTunnels.delete(tunnelId); + sendStatus('error', error?.message || String(error)); + throw error; + } + if (isTunnelCancelled(tunnelState)) { + portForwardingTunnels.delete(tunnelId); + return { tunnelId, success: false, cancelled: true }; + } + const abandonPendingDial = (reason) => { + if (!pendingDialCoordination) return; + failTransportDial( + pendingDialCoordination, + reason instanceof Error ? reason : new Error(String(reason || "Port forward connection cancelled")), + ); + }; // Prefer an already-authenticated transport (live terminal or idle park). - const existingTransport = findTransportByEndpoint(reuseEndpoint); if (existingTransport?.conn) { - const tunnelState = { - type, - tunnelId, - conn: existingTransport.conn, - pendingConn: null, - server: null, - chainConnections: [], - passphraseAbortController, - ruleId, - status: 'connecting', - webContentsId: sender.id, - subscribers: new Map([[sender.id, sender]]), - cancelled: false, - sshTransportManaged: false, - }; - const sendStatus = (status, error = null) => { - publishTunnelStatus(tunnelId, tunnelState, status, error); - }; + tunnelState.conn = existingTransport.conn; + tunnelState.chainConnections = []; portForwardingTunnels.set(tunnelId, tunnelState); - sendStatus("connecting"); try { borrowTransport(existingTransport, { kind: LEASE_KINDS.forward, @@ -1001,30 +1193,15 @@ async function startPortForward(event, payload) { try { detachSharedTransportLifecycle(tunnelState); } catch { /* ignore */ } try { returnTransport(tunnelState); } catch { /* ignore */ } portForwardingTunnels.delete(tunnelId); + tunnelState.conn = null; + tunnelState.sshTransportManaged = false; // Fall through to a dedicated dial. } } const conn = new SSHClient(); - const tunnelState = { - type, - tunnelId, - conn, - pendingConn: null, - server: null, - chainConnections, - passphraseAbortController, - ruleId, - status: 'connecting', - webContentsId: sender.id, - subscribers: new Map([[sender.id, sender]]), - cancelled: false, - sshTransportManaged: false, - }; - - const sendStatus = (status, error = null) => { - publishTunnelStatus(tunnelId, tunnelState, status, error); - }; + tunnelState.conn = conn; + tunnelState.chainConnections = chainConnections; // Keepalive policy: // - positive value: honor it @@ -1033,20 +1210,18 @@ async function startPortForward(event, payload) { // otherwise be killed by ssh2 after countMax unanswered probes) // - undefined: legacy caller path, fall back to 10s/3 so an idle // forwarded TCP tunnel doesn't get dropped by NAT state tables. - const tunnelKeepaliveMs = resolvedKeepaliveInterval == null - ? 10000 - : (resolvedKeepaliveInterval > 0 ? resolvedKeepaliveInterval * 1000 : 0); - const tunnelKeepaliveCountMax = resolvedKeepaliveInterval == null - ? 3 - : (resolvedKeepaliveInterval > 0 ? (resolvedKeepaliveCountMax ?? 3) : 0); + const keepalivePolicy = resolveConnectionKeepalivePolicy({ + keepaliveInterval: resolvedKeepaliveInterval, + keepaliveCountMax: resolvedKeepaliveCountMax, + }); const connectOpts = { host: hostname, port: port, username: username || 'root', timeout: connectionTimeouts.tcpConnectTimeoutMs, readyTimeout: 0, - keepaliveInterval: tunnelKeepaliveMs, - keepaliveCountMax: tunnelKeepaliveCountMax, + keepaliveInterval: keepalivePolicy.keepaliveIntervalMs, + keepaliveCountMax: keepalivePolicy.keepaliveCountMax, // Enable keyboard-interactive authentication (required for 2FA/MFA) tryKeyboard: true, algorithms: buildAlgorithms(legacyAlgorithms, { skipEcdsaHostKey, algorithmOverrides }), @@ -1062,7 +1237,6 @@ async function startPortForward(event, payload) { }); const hasCertificate = typeof certificate === "string" && certificate.trim().length > 0; - sendStatus('connecting'); portForwardingTunnels.set(tunnelId, tunnelState); let defaultKeys = []; @@ -1116,6 +1290,7 @@ async function startPortForward(event, payload) { if (isTunnelCancelled(tunnelState)) { portForwardingTunnels.delete(tunnelId); + abandonPendingDial("Port forward connection cancelled"); return { tunnelId, success: false, cancelled: true }; } @@ -1151,6 +1326,7 @@ async function startPortForward(event, payload) { : discoveredDefaultKeys; if (isTunnelCancelled(tunnelState)) { portForwardingTunnels.delete(tunnelId); + abandonPendingDial("Port forward connection cancelled"); return { tunnelId, success: false, cancelled: true }; } @@ -1172,6 +1348,7 @@ async function startPortForward(event, payload) { portForwardAuthPhase = authConfig.authPhase || portForwardAuthPhase; if (isTunnelCancelled(tunnelState)) { portForwardingTunnels.delete(tunnelId); + abandonPendingDial("Port forward connection cancelled"); return { tunnelId, success: false, cancelled: true }; } @@ -1220,6 +1397,7 @@ async function startPortForward(event, payload) { if (!tunnelState.cleanupFailed) { portForwardingTunnels.delete(tunnelId); } + abandonPendingDial("Port forward connection cancelled"); return { tunnelId, success: false, cancelled: true }; } connectOpts.sock = connectionSocket; @@ -1238,6 +1416,7 @@ async function startPortForward(event, payload) { if (!tunnelState.cleanupFailed) { portForwardingTunnels.delete(tunnelId); } + abandonPendingDial("Port forward connection cancelled"); return { tunnelId, success: false, cancelled: true }; } tunnelState.pendingConn = null; @@ -1250,6 +1429,7 @@ async function startPortForward(event, payload) { if (!tunnelState.cleanupFailed) { portForwardingTunnels.delete(tunnelId); } + abandonPendingDial("Port forward connection cancelled"); return { tunnelId, success: false, cancelled: true }; } if (isPassphraseCancelledError(err)) { @@ -1258,6 +1438,7 @@ async function startPortForward(event, payload) { } catch { /* best-effort cancel on passphrase cancel */ } + abandonPendingDial(err); return { tunnelId, success: false, cancelled: true }; } tunnelState.cancelled = true; @@ -1272,6 +1453,7 @@ async function startPortForward(event, payload) { keyboardInteractiveHandler.cancelRequestsForSession(tunnelId, "connection-ended"); portForwardingTunnels.delete(tunnelId); sendStatus('error', err?.message || String(err)); + abandonPendingDial(err); throw err; } @@ -1332,11 +1514,16 @@ async function startPortForward(event, payload) { sendStatus, releaseOnError: false, endpoint: reuseEndpoint, - registerTransport: true, + registerTransport: reuseTransport !== false, + dialCoordination: pendingDialCoordination, }).then((result) => { + if (!result?.success && pendingDialCoordination) { + failTransportDial(pendingDialCoordination, new Error("Port forward cancelled before activation")); + } settled = true; resolve(result); }).catch((err) => { + if (pendingDialCoordination) failTransportDial(pendingDialCoordination, err); settled = true; reject(err); }); @@ -1346,6 +1533,7 @@ async function startPortForward(event, payload) { clearAuthReadyTimer(); console.error(`[PortForward] SSH error:`, err.message); if (settled) return; + if (pendingDialCoordination) failTransportDial(pendingDialCoordination, err); sendStatus('error', err.message); cleanupChainConnections(chainConnections); settled = true; @@ -1384,9 +1572,14 @@ async function startPortForward(event, payload) { if (!settled) { settled = true; if (wasCancelled) { + if (pendingDialCoordination) { + failTransportDial(pendingDialCoordination, new Error("Port forward connection cancelled")); + } resolve({ tunnelId, success: false, cancelled: true }); } else { - reject(new Error(`Tunnel ${tunnelId} closed before connection established`)); + const err = new Error(`Tunnel ${tunnelId} closed before connection established`); + if (pendingDialCoordination) failTransportDial(pendingDialCoordination, err); + reject(err); } } }); @@ -1395,6 +1588,7 @@ async function startPortForward(event, payload) { clearAuthReadyTimer(); if (settled) return; const err = new Error(`Connection timeout to ${hostname}`); + if (pendingDialCoordination) failTransportDial(pendingDialCoordination, err); sendStatus('error', err.message); cleanupChainConnections(chainConnections); settled = true; @@ -1473,6 +1667,19 @@ async function subscribePortForward(event, payload) { }; } +/** Remove a renderer-owned subscription from every tunnel in this process. */ +async function unsubscribePortForwardSender(event, payload = {}) { + const webContentsId = payload.webContentsId ?? event?.sender?.id; + if (!Number.isSafeInteger(webContentsId)) return { removed: 0 }; + let removed = 0; + for (const tunnel of portForwardingTunnels.values()) { + if (tunnel.subscribers instanceof Map && tunnel.subscribers.delete(webContentsId)) { + removed += 1; + } + } + return { removed }; +} + /** * List all active port forwards */ @@ -1546,7 +1753,132 @@ async function stopPortForwardByRuleId(_event, { ruleId }) { /** * Register IPC handlers for port forwarding operations */ -function registerHandlers(ipcMain) { +function registerHandlers(ipcMain, options = {}) { + const terminalWorkerManager = options.terminalWorkerManager || null; + if (terminalWorkerManager) { + const subscriptionsBySender = new Map(); + const trackedTunnelIds = new Set(); + const tunnelRuleIds = new Map(); + + const unsubscribeDestroyedSender = (webContentsId) => { + void terminalWorkerManager.request( + "netcatty:portforward:unsubscribeSender", + { webContentsId }, + { webContentsId }, + ).catch(() => {}); + }; + + const forgetTunnel = (tunnelId) => { + if (!tunnelId) return; + trackedTunnelIds.delete(tunnelId); + tunnelRuleIds.delete(tunnelId); + for (const [webContentsId, entry] of subscriptionsBySender) { + entry.tunnelIds.delete(tunnelId); + if (entry.tunnelIds.size > 0) continue; + entry.sender.removeListener?.("destroyed", entry.onDestroyed); + subscriptionsBySender.delete(webContentsId); + } + }; + + const ensureSenderLifecycle = (sender) => { + if (!sender || !Number.isSafeInteger(sender.id)) return null; + if (sender.isDestroyed?.()) { + unsubscribeDestroyedSender(sender.id); + return null; + } + let entry = subscriptionsBySender.get(sender.id); + if (!entry) { + const onDestroyed = () => { + subscriptionsBySender.delete(sender.id); + unsubscribeDestroyedSender(sender.id); + }; + entry = { sender, tunnelIds: new Set(), onDestroyed }; + subscriptionsBySender.set(sender.id, entry); + sender.once?.("destroyed", onDestroyed); + } + return entry; + }; + + const releaseEmptySenderLifecycle = (sender, entry) => { + if (!entry || entry.tunnelIds.size > 0 || subscriptionsBySender.get(sender?.id) !== entry) return; + entry.sender.removeListener?.("destroyed", entry.onDestroyed); + subscriptionsBySender.delete(sender.id); + }; + + const trackSubscription = (sender, tunnelId, ruleId) => { + if (!tunnelId) return; + const entry = ensureSenderLifecycle(sender); + if (!entry) return; + entry.tunnelIds.add(tunnelId); + trackedTunnelIds.add(tunnelId); + if (ruleId) tunnelRuleIds.set(tunnelId, ruleId); + }; + + const requestWorker = (channel, { track = false, cleanup = null } = {}) => { + ipcMain.handle(channel, async (event, payload) => { + const pendingSenderEntry = track ? ensureSenderLifecycle(event?.sender) : null; + try { + const result = await terminalWorkerManager.request(channel, payload, { + webContentsId: event?.sender?.id, + }); + if ( + track + && result?.tunnelId + && result.success !== false + && result.status !== "inactive" + && result.status !== "error" + ) { + trackSubscription(event?.sender, result.tunnelId, payload?.ruleId); + } + if (cleanup === "tunnel" && result?.success) forgetTunnel(payload?.tunnelId); + if (cleanup === "all") { + for (const tunnelId of [...trackedTunnelIds]) forgetTunnel(tunnelId); + } + if (cleanup === "rule" && result?.failed === 0) { + for (const [tunnelId, ruleId] of tunnelRuleIds) { + if (ruleId === payload?.ruleId) forgetTunnel(tunnelId); + } + } + return result; + } finally { + releaseEmptySenderLifecycle(event?.sender, pendingSenderEntry); + } + }); + }; + + requestWorker("netcatty:portforward:start", { track: true }); + requestWorker("netcatty:portforward:stop", { cleanup: "tunnel" }); + requestWorker("netcatty:portforward:status"); + requestWorker("netcatty:portforward:subscribe", { track: true }); + requestWorker("netcatty:portforward:list"); + requestWorker("netcatty:portforward:stopAll", { cleanup: "all" }); + requestWorker("netcatty:portforward:stopByRuleId", { cleanup: "rule" }); + + terminalWorkerManager.onWorkerRendererEvent?.((message) => { + if (message?.channel !== "netcatty:portforward:status") return; + if (message.payload?.status === "inactive" || message.payload?.status === "error") { + forgetTunnel(message.payload?.tunnelId); + } + }); + terminalWorkerManager.onWorkerExit?.((error) => { + const message = error?.message || "Terminal worker exited"; + const notified = new Set(); + for (const entry of subscriptionsBySender.values()) { + for (const tunnelId of entry.tunnelIds) { + const key = `${entry.sender.id}:${tunnelId}`; + if (notified.has(key) || entry.sender.isDestroyed?.()) continue; + notified.add(key); + safeSend(entry.sender, "netcatty:portforward:status", { + tunnelId, + status: "error", + error: message, + }); + } + } + for (const tunnelId of [...trackedTunnelIds]) forgetTunnel(tunnelId); + }); + return; + } ipcMain.handle("netcatty:portforward:start", startPortForward); ipcMain.handle("netcatty:portforward:stop", stopPortForward); ipcMain.handle("netcatty:portforward:status", getPortForwardStatus); @@ -1554,6 +1886,7 @@ function registerHandlers(ipcMain) { ipcMain.handle("netcatty:portforward:list", listPortForwards); ipcMain.handle("netcatty:portforward:stopAll", () => stopAllPortForwards()); ipcMain.handle("netcatty:portforward:stopByRuleId", stopPortForwardByRuleId); + ipcMain.handle("netcatty:portforward:unsubscribeSender", unsubscribePortForwardSender); } module.exports = { @@ -1562,6 +1895,7 @@ module.exports = { stopPortForward, getPortForwardStatus, subscribePortForward, + unsubscribePortForwardSender, listPortForwards, stopAllPortForwards, stopPortForwardByRuleId, @@ -1569,4 +1903,10 @@ module.exports = { publishTunnelStatus, shouldFinalizeTunnelClose, isReusableTunnelStatus, + buildPortForwardEndpoint, + buildPortForwardEndpointFromStartPayload, + _bindPortForwardChannelsForTests: bindPortForwardChannels, + _trackTunnelPipeForTests: trackTunnelPipe, + _attachTunnelPipeStreamForTests: attachTunnelPipeStream, + _destroyTunnelPipesForTests: destroyTunnelPipes, }; diff --git a/electron/bridges/portForwardingBridge.endpoint.test.cjs b/electron/bridges/portForwardingBridge.endpoint.test.cjs new file mode 100644 index 000000000..7e5ca4744 --- /dev/null +++ b/electron/bridges/portForwardingBridge.endpoint.test.cjs @@ -0,0 +1,121 @@ +const assert = require("node:assert/strict"); +const test = require("node:test"); + +const { + buildPortForwardEndpoint, + buildPortForwardEndpointFromStartPayload, +} = require("./portForwardingBridge.cjs"); +const { + buildConnectionReuseEndpoint, + buildEndpointKey, + normalizeEndpoint, +} = require("./sshConnectionPool.cjs"); + +const baseOptions = { + hostId: "pf-profile", + hostname: "pf-endpoint.test", + port: 22, + username: "alice", + authMethod: "key", + keyId: "pf-key", + privateKey: "PF_PRIVATE_SECRET", + publicKey: "ssh-ed25519 PF_PUBLIC_KEY", + passphrase: "PF_PASSPHRASE_SECRET", + identityFilePaths: ["/keys/pf"], + useSshAgent: true, + identityAgent: "/run/user/1000/pf-agent-a.sock", + identitiesOnly: true, + agentPublicKeys: ["ssh-ed25519 PF_AGENT_KEY_A"], + addKeysToAgent: "confirm", + useKeychain: true, + legacyAlgorithms: false, + skipEcdsaHostKey: false, + algorithmOverrides: { kex: ["curve25519-sha256"] }, + verifyHostKeys: true, + knownHosts: [{ + id: "kh-pf-endpoint", + hostname: "pf-endpoint.test", + port: 22, + keyType: "ssh-ed25519", + fingerprint: "SHA256:pf-endpoint-a", + publicKey: "ssh-ed25519 PF_KNOWN_HOST_KEY_A", + }], +}; + +function endpointKey(options = baseOptions) { + return buildEndpointKey(buildPortForwardEndpoint(options)); +} + +test("port-forward endpoint identity changes for every effective agent selection setting", () => { + const baseKey = endpointKey(); + const variants = [ + { identityAgent: "/run/user/1000/pf-agent-b.sock" }, + { identitiesOnly: false }, + { agentPublicKeys: ["ssh-ed25519 PF_AGENT_KEY_B"] }, + { addKeysToAgent: "yes" }, + { useKeychain: false }, + ]; + + for (const variant of variants) { + assert.notEqual(endpointKey({ ...baseOptions, ...variant }), baseKey); + } +}); + +test("port-forward endpoint identity changes for every effective algorithm setting", () => { + const baseKey = endpointKey(); + const variants = [ + { legacyAlgorithms: true }, + { skipEcdsaHostKey: true }, + { algorithmOverrides: { kex: ["diffie-hellman-group14-sha256"] } }, + ]; + + for (const variant of variants) { + assert.notEqual(endpointKey({ ...baseOptions, ...variant }), baseKey); + } +}); + +test("port-forward endpoint identity includes the effective known-host trust set", () => { + const baseKey = endpointKey(); + const rotatedKnownHosts = [{ + ...baseOptions.knownHosts[0], + fingerprint: "SHA256:pf-endpoint-b", + publicKey: "ssh-ed25519 PF_KNOWN_HOST_KEY_B", + }]; + + assert.notEqual(endpointKey({ ...baseOptions, knownHosts: rotatedKnownHosts }), baseKey); + assert.equal( + endpointKey(), + buildEndpointKey(buildConnectionReuseEndpoint(baseOptions, { sftpSudo: false })), + ); +}); + +test("port-forward endpoint normalizes to the common digest-only connection identity", () => { + const normalized = normalizeEndpoint(buildPortForwardEndpoint(baseOptions)); + const serialized = JSON.stringify(normalized); + + assert.ok(normalized?.authFingerprint); + for (const secret of [ + baseOptions.privateKey, + baseOptions.passphrase, + baseOptions.agentPublicKeys[0], + baseOptions.knownHosts[0].publicKey, + ]) { + assert.equal(serialized.includes(secret), false); + } +}); + +test("port-forward start identity uses the resolved keepalive policy", () => { + const payload = { + ...baseOptions, + resolvedKeepaliveInterval: 30, + resolvedKeepaliveCountMax: 10, + }; + assert.equal( + buildEndpointKey(buildPortForwardEndpointFromStartPayload(payload)), + buildEndpointKey(buildConnectionReuseEndpoint({ + ...baseOptions, + keepaliveInterval: 30, + keepaliveCountMax: 10, + })), + ); +}); diff --git a/electron/bridges/portForwardingBridge.jumpScope.test.cjs b/electron/bridges/portForwardingBridge.jumpScope.test.cjs index 12a94f009..0ff88c5ce 100644 --- a/electron/bridges/portForwardingBridge.jumpScope.test.cjs +++ b/electron/bridges/portForwardingBridge.jumpScope.test.cjs @@ -7,6 +7,7 @@ const { readFileSync } = require("node:fs"); const { Duplex } = require("node:stream"); const Module = require("node:module"); const keyboardInteractiveHandler = require("./keyboardInteractiveHandler.cjs"); +const { resetSshTransportRegistryForTests } = require("./sshConnectionPool.cjs"); function createSender(onSend = () => {}) { return { @@ -22,6 +23,7 @@ function loadBridgeWithMocks(t, { systemAgent = false, chainError = null } = {}) let capturedConnectOptions = null; let connectedClient = null; let capturedSystemAgentOptions = null; + let physicalDialCount = 0; class MockSshClient extends EventEmitter { constructor() { @@ -33,6 +35,7 @@ function loadBridgeWithMocks(t, { systemAgent = false, chainError = null } = {}) } connect(options) { + physicalDialCount += 1; this.options = options; connectedClient = this; capturedConnectOptions = options; @@ -129,9 +132,83 @@ function loadBridgeWithMocks(t, { systemAgent = false, chainError = null } = {}) getCapturedConnectOptions: () => capturedConnectOptions, getConnectedClient: () => connectedClient, getCapturedSystemAgentOptions: () => capturedSystemAgentOptions, + getPhysicalDialCount: () => physicalDialCount, }; } +test("simultaneous port forwards to one endpoint share one physical SSH dial", async (t) => { + resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 }); + t.after(() => resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 })); + const { bridge, getPhysicalDialCount } = loadBridgeWithMocks(t); + const payload = { + type: "local", + localPort: 0, + bindAddress: "127.0.0.1", + remoteHost: "127.0.0.1", + remotePort: 3306, + hostname: "db.internal", + hostId: "db-host", + port: 22, + username: "dbuser", + password: "target-password", + authMethod: "password", + verifyHostKeys: false, + useSshAgent: false, + }; + const firstEvent = { sender: { ...createSender(), id: 31 } }; + const secondEvent = { sender: { ...createSender(), id: 32 } }; + + try { + const [first, second] = await Promise.all([ + bridge.startPortForward(firstEvent, { ...payload, tunnelId: "pf-shared-1", ruleId: "rule-shared-1" }), + bridge.startPortForward(secondEvent, { ...payload, tunnelId: "pf-shared-2", ruleId: "rule-shared-2" }), + ]); + assert.equal(first.success, true); + assert.equal(second.success, true); + assert.equal(getPhysicalDialCount(), 1); + } finally { + await bridge.stopPortForward(firstEvent, { tunnelId: "pf-shared-1" }); + await bridge.stopPortForward(secondEvent, { tunnelId: "pf-shared-2" }); + } +}); + +test("explicitly dedicated port forwards do not enter shared dial coordination", async (t) => { + resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 }); + t.after(() => resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 })); + const { bridge, getPhysicalDialCount } = loadBridgeWithMocks(t); + const payload = { + type: "local", + localPort: 0, + bindAddress: "127.0.0.1", + remoteHost: "127.0.0.1", + remotePort: 3306, + hostname: "db.internal", + hostId: "db-host", + port: 22, + username: "dbuser", + password: "target-password", + authMethod: "password", + verifyHostKeys: false, + useSshAgent: false, + reuseTransport: false, + }; + const firstEvent = { sender: { ...createSender(), id: 41 } }; + const secondEvent = { sender: { ...createSender(), id: 42 } }; + + try { + const [first, second] = await Promise.all([ + bridge.startPortForward(firstEvent, { ...payload, tunnelId: "pf-dedicated-1", ruleId: "rule-dedicated-1" }), + bridge.startPortForward(secondEvent, { ...payload, tunnelId: "pf-dedicated-2", ruleId: "rule-dedicated-2" }), + ]); + assert.equal(first.success, true); + assert.equal(second.success, true); + assert.equal(getPhysicalDialCount(), 2); + } finally { + await bridge.stopPortForward(firstEvent, { tunnelId: "pf-dedicated-1" }); + await bridge.stopPortForward(secondEvent, { tunnelId: "pf-dedicated-2" }); + } +}); + test("port forwarding routes jump-host keyboard-interactive prompts through the external scope", async (t) => { const { bridge, diff --git a/electron/bridges/portForwardingBridge.pipeLifecycle.test.cjs b/electron/bridges/portForwardingBridge.pipeLifecycle.test.cjs new file mode 100644 index 000000000..26e3aa307 --- /dev/null +++ b/electron/bridges/portForwardingBridge.pipeLifecycle.test.cjs @@ -0,0 +1,54 @@ +const assert = require("node:assert/strict"); +const { EventEmitter } = require("node:events"); +const test = require("node:test"); + +const { + _attachTunnelPipeStreamForTests: attachTunnelPipeStream, + _destroyTunnelPipesForTests: destroyTunnelPipes, + _trackTunnelPipeForTests: trackTunnelPipe, +} = require("./portForwardingBridge.cjs"); + +function fakeEndpoint() { + const endpoint = new EventEmitter(); + endpoint.destroyedByTest = false; + endpoint.destroy = () => { endpoint.destroyedByTest = true; }; + endpoint.end = () => { endpoint.destroyedByTest = true; }; + endpoint.close = () => { endpoint.destroyedByTest = true; }; + return endpoint; +} + +test("accepted sockets are tracked before forwardOut finishes and stop destroys them", () => { + const tunnel = { cancelled: false, activePipes: new Set() }; + const socket = fakeEndpoint(); + trackTunnelPipe(tunnel, socket); + assert.equal(tunnel.activePipes.size, 1); + + destroyTunnelPipes(tunnel); + assert.equal(socket.destroyedByTest, true); + assert.equal(tunnel.activePipes.size, 0); +}); + +test("a late forwardOut stream after stop is rejected and destroyed", () => { + const tunnel = { cancelled: false, activePipes: new Set() }; + const socket = fakeEndpoint(); + const entry = trackTunnelPipe(tunnel, socket); + tunnel.cancelled = true; + const stream = fakeEndpoint(); + + assert.equal(attachTunnelPipeStream(tunnel, entry, stream), false); + assert.equal(socket.destroyedByTest, true); + assert.equal(stream.destroyedByTest, true); + assert.equal(tunnel.activePipes.size, 0); +}); + +test("closing either side tears down the paired endpoint and releases tracking", () => { + const tunnel = { cancelled: false, activePipes: new Set() }; + const socket = fakeEndpoint(); + const stream = fakeEndpoint(); + const entry = trackTunnelPipe(tunnel, socket); + assert.equal(attachTunnelPipeStream(tunnel, entry, stream), true); + + socket.emit("close"); + assert.equal(stream.destroyedByTest, true); + assert.equal(tunnel.activePipes.size, 0); +}); diff --git a/electron/bridges/portForwardingBridge.worker.test.cjs b/electron/bridges/portForwardingBridge.worker.test.cjs new file mode 100644 index 000000000..ec38df41e --- /dev/null +++ b/electron/bridges/portForwardingBridge.worker.test.cjs @@ -0,0 +1,237 @@ +const assert = require("node:assert/strict"); +const EventEmitter = require("node:events"); +const test = require("node:test"); + +const { registerHandlers } = require("./portForwardingBridge.cjs"); + +function createIpcMain() { + const handlers = new Map(); + return { + handlers, + handle(channel, handler) { + handlers.set(channel, handler); + }, + }; +} + +function createSender(id) { + const sender = new EventEmitter(); + sender.id = id; + sender.isDestroyed = () => false; + sender.sent = []; + sender.send = (channel, payload) => sender.sent.push({ channel, payload }); + return sender; +} + +test("worker mode routes every port-forward operation through the terminal worker", async () => { + const ipcMain = createIpcMain(); + const requests = []; + const terminalWorkerManager = { + request(channel, payload, options) { + requests.push({ channel, payload, options }); + if (channel === "netcatty:portforward:start") { + return Promise.resolve({ tunnelId: payload.tunnelId, success: true }); + } + if (channel === "netcatty:portforward:subscribe") { + return Promise.resolve({ tunnelId: payload.tunnelId, status: "active" }); + } + return Promise.resolve(null); + }, + onWorkerExit() { + return { dispose() {} }; + }, + }; + registerHandlers(ipcMain, { terminalWorkerManager }); + const sender = createSender(17); + const event = { sender }; + const channels = [ + "netcatty:portforward:start", + "netcatty:portforward:stop", + "netcatty:portforward:status", + "netcatty:portforward:subscribe", + "netcatty:portforward:list", + "netcatty:portforward:stopAll", + "netcatty:portforward:stopByRuleId", + ]; + + for (const channel of channels) { + await ipcMain.handlers.get(channel)(event, { tunnelId: "pf-worker", ruleId: "rule-worker" }); + } + + assert.deepEqual(requests.map((entry) => entry.channel), channels); + assert.ok(requests.every((entry) => entry.options.webContentsId === 17)); +}); + +test("legacy mode keeps port-forward handlers in the main process", async () => { + const ipcMain = createIpcMain(); + registerHandlers(ipcMain); + + assert.deepEqual( + await ipcMain.handlers.get("netcatty:portforward:status")( + { sender: createSender(19) }, + { tunnelId: "pf-legacy-missing" }, + ), + { tunnelId: "pf-legacy-missing", status: "inactive" }, + ); + assert.deepEqual(await ipcMain.handlers.get("netcatty:portforward:list")(), []); +}); + +test("worker mode drops renderer subscriptions when their window is destroyed", async () => { + const ipcMain = createIpcMain(); + const requests = []; + const terminalWorkerManager = { + request(channel, payload, options) { + requests.push({ channel, payload, options }); + return Promise.resolve({ tunnelId: payload.tunnelId, status: "active" }); + }, + onWorkerExit() { + return { dispose() {} }; + }, + }; + registerHandlers(ipcMain, { terminalWorkerManager }); + const sender = createSender(23); + + await ipcMain.handlers.get("netcatty:portforward:subscribe")( + { sender }, + { tunnelId: "pf-destroyed" }, + ); + sender.emit("destroyed"); + await new Promise((resolve) => setImmediate(resolve)); + + assert.equal(requests.at(-1).channel, "netcatty:portforward:unsubscribeSender"); + assert.deepEqual(requests.at(-1).payload, { webContentsId: 23 }); +}); + +test("worker mode drops a renderer destroyed while its start request is pending", async () => { + const ipcMain = createIpcMain(); + const requests = []; + let finishStart; + const startResult = new Promise((resolve) => { finishStart = resolve; }); + const terminalWorkerManager = { + request(channel, payload, options) { + requests.push({ channel, payload, options }); + if (channel === "netcatty:portforward:start") return startResult; + return Promise.resolve({ removed: 1 }); + }, + onWorkerExit() { + return { dispose() {} }; + }, + }; + registerHandlers(ipcMain, { terminalWorkerManager }); + const sender = createSender(29); + let destroyed = false; + sender.isDestroyed = () => destroyed; + const pending = ipcMain.handlers.get("netcatty:portforward:start")( + { sender }, + { tunnelId: "pf-pending-destroy", ruleId: "rule-pending-destroy" }, + ); + + destroyed = true; + sender.emit("destroyed"); + await new Promise((resolve) => setImmediate(resolve)); + assert.equal(requests.at(-1).channel, "netcatty:portforward:unsubscribeSender"); + finishStart({ tunnelId: "pf-pending-destroy", success: true }); + await pending; + await new Promise((resolve) => setImmediate(resolve)); + assert.equal(requests.at(-1).channel, "netcatty:portforward:unsubscribeSender"); +}); + +test("worker mode does not retain renderer lifecycle after a failed start", async () => { + const ipcMain = createIpcMain(); + const terminalWorkerManager = { + request(_channel, payload) { + return Promise.resolve({ + tunnelId: payload.tunnelId, + success: false, + error: "bind failed", + }); + }, + onWorkerExit() { + return { dispose() {} }; + }, + }; + registerHandlers(ipcMain, { terminalWorkerManager }); + const sender = createSender(30); + + await ipcMain.handlers.get("netcatty:portforward:start")( + { sender }, + { tunnelId: "pf-failed", ruleId: "rule-failed" }, + ); + + assert.equal(sender.listenerCount("destroyed"), 0); +}); + +test("worker error terminal status releases the renderer subscription", async () => { + const ipcMain = createIpcMain(); + let workerRendererEventListener; + const terminalWorkerManager = { + request(_channel, payload) { + return Promise.resolve({ tunnelId: payload.tunnelId, success: true }); + }, + onWorkerRendererEvent(listener) { + workerRendererEventListener = listener; + return { dispose() {} }; + }, + onWorkerExit() { + return { dispose() {} }; + }, + }; + registerHandlers(ipcMain, { terminalWorkerManager }); + const sender = createSender(32); + + await ipcMain.handlers.get("netcatty:portforward:start")( + { sender }, + { tunnelId: "pf-error", ruleId: "rule-error" }, + ); + assert.equal(sender.listenerCount("destroyed"), 1); + + workerRendererEventListener({ + channel: "netcatty:portforward:status", + payload: { tunnelId: "pf-error", status: "error", error: "remote closed" }, + }); + + assert.equal(sender.listenerCount("destroyed"), 0); +}); + +test("worker crashes notify subscribed renderers and permit a replacement worker start", async () => { + const ipcMain = createIpcMain(); + let workerExitListener; + let starts = 0; + const terminalWorkerManager = { + request(channel, payload) { + if (channel === "netcatty:portforward:start") { + starts += 1; + return Promise.resolve({ tunnelId: payload.tunnelId, success: true }); + } + return Promise.resolve({ tunnelId: payload.tunnelId, status: "active" }); + }, + onWorkerExit(listener) { + workerExitListener = listener; + return { dispose() {} }; + }, + }; + registerHandlers(ipcMain, { terminalWorkerManager }); + const sender = createSender(31); + const event = { sender }; + + await ipcMain.handlers.get("netcatty:portforward:start")( + event, + { tunnelId: "pf-crash", ruleId: "rule-crash" }, + ); + workerExitListener(new Error("Terminal worker exited with code 9")); + + assert.deepEqual(sender.sent, [{ + channel: "netcatty:portforward:status", + payload: { + tunnelId: "pf-crash", + status: "error", + error: "Terminal worker exited with code 9", + }, + }]); + + await ipcMain.handlers.get("netcatty:portforward:start")( + event, + { tunnelId: "pf-recovered", ruleId: "rule-crash" }, + ); + assert.equal(starts, 2); +}); diff --git a/electron/bridges/ptyProcessTree.cjs b/electron/bridges/ptyProcessTree.cjs index 05ea92dfa..50cf4d280 100644 --- a/electron/bridges/ptyProcessTree.cjs +++ b/electron/bridges/ptyProcessTree.cjs @@ -1,5 +1,8 @@ const { execFile } = require("node:child_process"); +const POSIX_PROCESS_LIST_TIMEOUT_MS = 3_000; +const POSIX_PROCESS_LIST_MAX_BUFFER_BYTES = 4 * 1024 * 1024; + function createProcessTree({ platform, listPosix, listWindows } = {}) { const sessionPidMap = new Map(); @@ -29,12 +32,16 @@ function createProcessTree({ platform, listPosix, listWindows } = {}) { return { registerPid, unregisterPid, getChildProcesses }; } -function defaultListPosix(ppid) { +function defaultListPosix(ppid, { execFileFn = execFile } = {}) { return new Promise((resolve) => { // `ps -A -o pid=,ppid=,args=` works on both BSD (macOS) and GNU (Linux). // `args=` shows the full command line (not truncated like `comm=`). // The trailing `=` on each column suppresses the header row. - execFile("ps", ["-A", "-o", "pid=,ppid=,args="], (err, stdout) => { + execFileFn("ps", ["-A", "-o", "pid=,ppid=,args="], { + timeout: POSIX_PROCESS_LIST_TIMEOUT_MS, + maxBuffer: POSIX_PROCESS_LIST_MAX_BUFFER_BYTES, + windowsHide: true, + }, (err, stdout) => { if (err || typeof stdout !== "string") return resolve([]); const out = []; for (const line of stdout.split("\n")) { @@ -81,7 +88,10 @@ function createDefaultProcessTree() { const defaultTree = createDefaultProcessTree(); module.exports = { + POSIX_PROCESS_LIST_TIMEOUT_MS, + POSIX_PROCESS_LIST_MAX_BUFFER_BYTES, createProcessTree, + defaultListPosix, processTree: defaultTree, registerPid: (id, pid) => defaultTree.registerPid(id, pid), unregisterPid: (id) => defaultTree.unregisterPid(id), diff --git a/electron/bridges/ptyProcessTree.test.cjs b/electron/bridges/ptyProcessTree.test.cjs index ffbcccd87..175c6acca 100644 --- a/electron/bridges/ptyProcessTree.test.cjs +++ b/electron/bridges/ptyProcessTree.test.cjs @@ -1,7 +1,12 @@ const test = require("node:test"); const assert = require("node:assert/strict"); -const { createProcessTree } = require("./ptyProcessTree.cjs"); +const { + POSIX_PROCESS_LIST_TIMEOUT_MS, + POSIX_PROCESS_LIST_MAX_BUFFER_BYTES, + createProcessTree, + defaultListPosix, +} = require("./ptyProcessTree.cjs"); test("getChildProcesses returns [] when session has no registered pid", async () => { const tree = createProcessTree({ platform: "darwin", listPosix: async () => [] }); @@ -77,3 +82,36 @@ test("registerPid warns when overwriting an existing sessionId with a different console.warn = origWarn; } }); + +test("default posix process listing bounds the child lifetime and captured output", async () => { + let invocation = null; + const processes = await defaultListPosix(1234, { + execFileFn(command, args, options, callback) { + invocation = { command, args, options }; + callback(null, [ + "2001 1234 node server.js", + "2002 9999 sleep 100", + "2003 1234 python worker.py", + ].join("\n")); + }, + }); + + assert.deepEqual(processes, [ + { pid: 2001, command: "node server.js" }, + { pid: 2003, command: "python worker.py" }, + ]); + assert.equal(invocation.command, "ps"); + assert.deepEqual(invocation.args, ["-A", "-o", "pid=,ppid=,args="]); + assert.equal(invocation.options.timeout, POSIX_PROCESS_LIST_TIMEOUT_MS); + assert.equal(invocation.options.maxBuffer, POSIX_PROCESS_LIST_MAX_BUFFER_BYTES); + assert.equal(invocation.options.windowsHide, true); +}); + +test("default posix process listing returns an empty result after command failure", async () => { + const processes = await defaultListPosix(1234, { + execFileFn(_command, _args, _options, callback) { + callback(Object.assign(new Error("timed out"), { killed: true }), ""); + }, + }); + assert.deepEqual(processes, []); +}); diff --git a/electron/bridges/registerBridgesTransferLimits.test.cjs b/electron/bridges/registerBridgesTransferLimits.test.cjs index ed745b918..faa98bce4 100644 --- a/electron/bridges/registerBridgesTransferLimits.test.cjs +++ b/electron/bridges/registerBridgesTransferLimits.test.cjs @@ -109,7 +109,7 @@ function createBridgeRegistrarForTest({ }); } -test("downloadToTemp routes direct downloads through the shared transfer bridge", async (t) => { +test("the single temp-download entry routes direct downloads through the shared transfer bridge", async (t) => { const tempDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-download-temp-test-")); t.after(async () => { await fs.promises.rm(tempDir, { recursive: true, force: true }); @@ -134,24 +134,27 @@ test("downloadToTemp routes direct downloads through the shared transfer bridge" registerBridges({}); - const handler = ipcMain.handlers.get("netcatty:sftp:downloadToTemp"); + const handler = ipcMain.handlers.get("netcatty:sftp:downloadToTempWithProgress"); assert.equal(typeof handler, "function"); - const localPath = await handler( + const result = await handler( { sender: { id: 1 } }, { sftpId: "sftp-1", remotePath: "/remote/report.bin", fileName: "report.bin", encoding: "utf-8", + transferId: "editor-download-1", }, ); - assert.equal(localPath, path.join(tempDir, "report.bin")); + assert.deepEqual(result, { + localPath: path.join(tempDir, "report.bin"), + cancelled: false, + }); assert.equal(observed.event.sender.id, 1); - assert.equal(typeof observed.payload.transferId, "string"); - const { transferId: _transferId, ...payload } = observed.payload; - assert.deepEqual(payload, { + assert.deepEqual(observed.payload, { + transferId: "editor-download-1", sourcePath: "/remote/report.bin", targetPath: path.join(tempDir, "report.bin"), sourceType: "sftp", @@ -162,60 +165,21 @@ test("downloadToTemp routes direct downloads through the shared transfer bridge" }); }); -test("downloadToTemp proxies shared transfer work to the terminal worker", async (t) => { +test("the retired no-progress temp-download IPC is not registered", async (t) => { const tempDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-download-worker-test-")); t.after(async () => { await fs.promises.rm(tempDir, { recursive: true, force: true }); }); const ipcMain = createIpcMainStub(); - const child = new FakeWorkerChild(); const registerBridges = createBridgeRegistrarForTest({ ipcMain, tempDir, - electronModuleOverrides: { - utilityProcess: { - fork() { - return child; - }, - }, - }, }); registerBridges({}); - - const handler = ipcMain.handlers.get("netcatty:sftp:downloadToTemp"); - const promise = handler( - { sender: { id: 12 } }, - { - sftpId: "worker-sftp-1", - remotePath: "/remote/report.bin", - fileName: "report.bin", - encoding: "utf-8", - }, - ); - await new Promise((resolve) => setImmediate(resolve)); - - assert.equal(child.messages[0].channel, "netcatty:transfer:start"); - assert.equal(child.messages[0].webContentsId, 12); - assert.equal(typeof child.messages[0].payload.transferId, "string"); - const { transferId: _transferId, ...workerPayload } = child.messages[0].payload; - assert.deepEqual(workerPayload, { - sourcePath: "/remote/report.bin", - targetPath: path.join(tempDir, "report.bin"), - sourceType: "sftp", - targetType: "local", - sourceSftpId: "worker-sftp-1", - sourceEncoding: "utf-8", - totalBytes: 0, - }); - child.emit("message", { - kind: "response", - requestId: child.messages[0].requestId, - result: { success: true }, - }); - - assert.equal(await promise, path.join(tempDir, "report.bin")); + assert.equal(ipcMain.handlers.has("netcatty:sftp:downloadToTemp"), false); + assert.equal(typeof ipcMain.handlers.get("netcatty:sftp:downloadToTempWithProgress"), "function"); }); test("downloadToTempWithProgress proxies transfer work to the terminal worker in worker mode", async (t) => { diff --git a/electron/bridges/scriptBridge.cjs b/electron/bridges/scriptBridge.cjs index bad41037a..ce9fbb0c3 100644 --- a/electron/bridges/scriptBridge.cjs +++ b/electron/bridges/scriptBridge.cjs @@ -10,9 +10,17 @@ const { removeSessionBuffer, } = require("../scripts/sessionOutputBuffer.cjs"); const { shellPromptPatterns } = require("../scripts/shellPromptPatterns.cjs"); +const { + appendRetainedRunLog, + pruneCompletedRuns, +} = require("../scripts/scriptRunRetention.cjs"); const { addTerminalDataTap } = require("../bridges/emitTerminalSessionData.cjs"); const sessionLogStreamManager = require("./sessionLogStreamManager.cjs"); +const MAX_SCRIPT_RECORDING_STEPS = 10_000; +const MAX_SCRIPT_RECORDING_BYTES = 2 * 1024 * 1024; +const SCRIPT_RECORDING_LIMIT_ERROR = "Recording stopped because it reached the safety limit"; + let sessions = null; let electronModule = null; let terminalBridge = null; @@ -35,6 +43,24 @@ const sessionRunChains = new Map(); const runAbortControls = new Map(); /** @type {Map} */ const rendererSessionMetaById = new Map(); +let disposeTerminalDataTap = null; +let disposeWorkerOutputTap = null; +let disposeWorkerSessionClosed = null; + +function disposeSubscription(subscription) { + if (typeof subscription === "function") { + subscription(); + } else { + subscription?.dispose?.(); + } +} + +function releaseSessionResources(sessionId) { + if (!sessionId) return; + removeSessionBuffer(sessionId); + rendererSessionMetaById.delete(sessionId); + recordings.delete(sessionId); +} function enqueueSessionRun(sessionId, task) { const previous = sessionRunChains.get(sessionId) || Promise.resolve(); @@ -52,18 +78,41 @@ function enqueueSessionRun(sessionId, task) { } function init(deps) { + for (const [runId, control] of runAbortControls.entries()) { + const run = runs.get(runId); + if (run && !run.endedAt) { + run.aborted = true; + run.paused = false; + run.status = "failed"; + run.error = "Script runtime reinitialized"; + run.endedAt = Date.now(); + getOrCreateBuffer(run.sessionId).abortWaiters(run.error); + settlePendingRunRequests(runId, { reject: false, reason: new Error(run.error) }); + } + control.abort(new Error("Script runtime reinitialized")); + } + disposeSubscription(disposeTerminalDataTap); + disposeSubscription(disposeWorkerOutputTap); + disposeSubscription(disposeWorkerSessionClosed); + disposeTerminalDataTap = null; + disposeWorkerOutputTap = null; + disposeWorkerSessionClosed = null; + sessions = deps.sessions; electronModule = deps.electronModule; terminalBridge = deps.terminalBridge; terminalWorkerManager = deps.terminalWorkerManager || null; getMainWindow = deps.getMainWindow; - addTerminalDataTap((sessionId, data) => { + disposeTerminalDataTap = addTerminalDataTap((sessionId, data) => { appendSessionOutput(sessionId, data); }); - terminalWorkerManager?.addOutputTap?.((sessionId, data) => { + disposeWorkerOutputTap = terminalWorkerManager?.addOutputTap?.((sessionId, data) => { appendSessionOutput(sessionId, data); - }); + }) ?? null; + disposeWorkerSessionClosed = terminalWorkerManager?.onSessionClosed?.(({ sessionId }) => { + releaseSessionResources(sessionId); + }) ?? null; } function broadcastRuns() { @@ -438,7 +487,7 @@ async function runScriptOnSession({ appendLog: (id, message) => { const entry = runs.get(id); if (!entry || entry.aborted || entry.endedAt) return; - entry.logs.push({ at: Date.now(), message }); + appendRetainedRunLog(entry, { at: Date.now(), message }); broadcastRuns(); }, writeToSession: (sid, data, options) => { @@ -491,6 +540,12 @@ async function runScriptOnSession({ broadcastRuns(); }, }); + runAbortControls.set(runId, { + abort: (reason) => { + runtime.stop(reason || new Error("Script stopped")); + abortRun(reason); + }, + }); try { const operationPromise = (async () => { @@ -521,7 +576,7 @@ async function runScriptOnSession({ run.progressCurrent = undefined; run.progressTotal = undefined; run.error = err?.message || String(err); - run.logs.push({ at: Date.now(), message: run.error }); + appendRetainedRunLog(run, { at: Date.now(), message: run.error }); } finally { settlePendingRunRequests(runId, { reject: run.aborted, @@ -529,6 +584,7 @@ async function runScriptOnSession({ }); await stopScriptSessionLog(sessionId, runId); runAbortControls.delete(runId); + pruneCompletedRuns(runs); broadcastRuns(); } } @@ -663,6 +719,7 @@ function handleRecordingStart(_event, payload = {}) { sessionId, startedAt: Date.now(), steps: [], + totalBytes: 0, lastTimestamp: Date.now(), }); return { ok: true }; @@ -682,14 +739,50 @@ function handleRecordingStop(_event, payload = {}) { function handleRecordingAppendStep(_event, payload = {}) { const { sessionId, step } = payload; const recording = recordings.get(sessionId); - if (!recording || !step) return { ok: false }; + if (!recording) return { ok: false, error: "Recording not started" }; + if (!step) return { ok: false, error: "Recording step is required" }; const now = Date.now(); const gap = now - recording.lastTimestamp; + const additions = []; if (gap > 1000 && step.type === "send") { - recording.steps.push({ type: "sleep", value: gap }); + additions.push({ type: "sleep", value: gap }); + } + additions.push(step); + let additionBytes = 0; + try { + for (const addition of additions) { + additionBytes += Buffer.byteLength(JSON.stringify(addition), "utf8"); + } + } catch { + return { ok: false, error: "Invalid recording step" }; + } + const nextStepCount = recording.steps.length + additions.length; + const nextTotalBytes = recording.totalBytes + additionBytes; + const exceedsLimit = nextStepCount > MAX_SCRIPT_RECORDING_STEPS + || nextTotalBytes > MAX_SCRIPT_RECORDING_BYTES; + if (!exceedsLimit) { + recording.steps.push(...additions); + recording.totalBytes = nextTotalBytes; + recording.lastTimestamp = now; + } + if ( + exceedsLimit + || recording.steps.length >= MAX_SCRIPT_RECORDING_STEPS + || recording.totalBytes >= MAX_SCRIPT_RECORDING_BYTES + ) { + recordings.delete(sessionId); + const retainedSteps = recording.steps; + return { + ok: false, + stopped: true, + reason: "limit", + error: SCRIPT_RECORDING_LIMIT_ERROR, + steps: retainedSteps, + code: retainedSteps.length > 0 + ? stepsToJavaScript(retainedSteps, new Date(recording.startedAt).toISOString()) + : "", + }; } - recording.steps.push(step); - recording.lastTimestamp = now; return { ok: true }; } @@ -707,9 +800,12 @@ function registerHandlers(ipcMain) { } module.exports = { + MAX_SCRIPT_RECORDING_STEPS, + MAX_SCRIPT_RECORDING_BYTES, init, registerHandlers, appendSessionOutput, removeSessionBuffer, + releaseSessionResources, resolveStartupSeedText, }; diff --git a/electron/bridges/scriptBridge.test.cjs b/electron/bridges/scriptBridge.test.cjs index 4ef0fcc83..a35e8b0f5 100644 --- a/electron/bridges/scriptBridge.test.cjs +++ b/electron/bridges/scriptBridge.test.cjs @@ -6,6 +6,7 @@ const test = require("node:test"); const scriptBridge = require("./scriptBridge.cjs"); const sessionLogStreamManager = require("./sessionLogStreamManager.cjs"); +const { getOrCreateBuffer } = require("../scripts/sessionOutputBuffer.cjs"); function delay(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); @@ -21,6 +22,31 @@ async function waitUntil(predicate, timeoutMs = 1000) { throw new Error("Timed out waiting for condition"); } +test("closing a terminal releases its script output buffer", () => { + let closeListener = null; + scriptBridge.init({ + sessions: new Map(), + electronModule: {}, + terminalBridge: {}, + terminalWorkerManager: { + addOutputTap() { return () => {}; }, + onSessionClosed(listener) { + closeListener = listener; + return { dispose() {} }; + }, + }, + getMainWindow: () => null, + }); + + scriptBridge.appendSessionOutput("closed-session", "stale output"); + assert.equal(getOrCreateBuffer("closed-session").getText(), "stale output"); + assert.equal(typeof closeListener, "function"); + + closeListener({ sessionId: "closed-session", reason: "closed" }); + assert.equal(getOrCreateBuffer("closed-session").getText(), ""); + scriptBridge.removeSessionBuffer("closed-session"); +}); + test("script run writes through terminal worker manager when enabled", async () => { const handlers = new Map(); const workerSends = []; @@ -283,6 +309,55 @@ test("stopping a script releases the session queue so it can run again", async ( assert.equal(stoppedRun.error, "Stopped by user"); }); +test("reinitializing the script bridge drops an accepted old-run sendLine tail", async () => { + const handlers = new Map(); + const oldWrites = []; + const replacementWrites = []; + + scriptBridge.init({ + sessions: new Map(), + electronModule: { + app: { getVersion: () => "test", getPath: () => process.cwd() }, + webContents: { fromId: () => null }, + }, + terminalBridge: { + writeToSession(_event, payload) { oldWrites.push(payload.data); }, + }, + terminalWorkerManager: null, + getMainWindow: () => null, + }); + scriptBridge.registerHandlers({ + handle(channel, handler) { handlers.set(channel, handler); }, + }); + + const oldRun = handlers.get("netcatty:script:run")({}, { + scriptId: "old-sendline-tail", + scriptLabel: "Old sendLine tail", + sessionId: "session-old-sendline-tail", + content: "await nct.screen.sendLine('old-body');", + permissionMode: "auto", + }); + await waitUntil(() => oldWrites.includes("old-body")); + + scriptBridge.init({ + sessions: new Map(), + electronModule: { + app: { getVersion: () => "test", getPath: () => process.cwd() }, + webContents: { fromId: () => null }, + }, + terminalBridge: { + writeToSession(_event, payload) { replacementWrites.push(payload.data); }, + }, + terminalWorkerManager: null, + getMainWindow: () => null, + }); + + await oldRun; + await delay(60); + assert.deepEqual(oldWrites, ["old-body"]); + assert.deepEqual(replacementWrites, []); +}); + test("late startup snapshots from a stopped script do not seed the next run", async () => { const handlers = new Map(); const writes = []; @@ -1966,3 +2041,63 @@ test("resolveStartupSeedText prefers viewport when buffer only adds scrolled-off assert.equal(seed, viewport); assert.doesNotMatch(seed, /READY marker/); }); + +test("script recording stops explicitly when the retained step limit is reached", async () => { + const handlers = new Map(); + scriptBridge.init({ + sessions: new Map(), + electronModule: {}, + terminalBridge: {}, + getMainWindow: () => null, + }); + scriptBridge.registerHandlers({ + handle(channel, handler) { handlers.set(channel, handler); }, + }); + + const sessionId = "recording-step-budget"; + await handlers.get("netcatty:script:recording:start")({}, { sessionId }); + let result; + for (let index = 0; index < 10_000; index += 1) { + result = await handlers.get("netcatty:script:recording:append-step")({}, { + sessionId, + step: { type: "sleep", value: index }, + }); + } + + assert.equal(result.stopped, true); + assert.match(result.error, /safety limit/i); + assert.equal(result.steps.length, 10_000); + assert.match(result.code, /sleep/); + assert.deepEqual( + await handlers.get("netcatty:script:recording:append-step")({}, { + sessionId, + step: { type: "sleep", value: 10_001 }, + }), + { ok: false, error: "Recording not started" }, + ); +}); + +test("script recording rejects one step beyond the total byte budget without retaining it", async () => { + const handlers = new Map(); + scriptBridge.init({ + sessions: new Map(), + electronModule: {}, + terminalBridge: {}, + getMainWindow: () => null, + }); + scriptBridge.registerHandlers({ + handle(channel, handler) { handlers.set(channel, handler); }, + }); + + const sessionId = "recording-byte-budget"; + await handlers.get("netcatty:script:recording:start")({}, { sessionId }); + const result = await handlers.get("netcatty:script:recording:append-step")({}, { + sessionId, + step: { type: "send", value: "x".repeat(2 * 1024 * 1024 + 1) }, + }); + + assert.equal(result.stopped, true); + assert.match(result.error, /safety limit/i); + assert.deepEqual(result.steps, []); + assert.equal(result.code, ""); +}); diff --git a/electron/bridges/sftpBridge.authRetry.test.cjs b/electron/bridges/sftpBridge.authRetry.test.cjs index 76ae9a8ee..f5b448aa8 100644 --- a/electron/bridges/sftpBridge.authRetry.test.cjs +++ b/electron/bridges/sftpBridge.authRetry.test.cjs @@ -5,6 +5,18 @@ const test = require("node:test"); const assert = require("node:assert/strict"); const { EventEmitter } = require("node:events"); const Module = require("node:module"); +const { + beginTransportDial, + failTransportDial, + getTransportStats, + resetSshTransportRegistryForTests, +} = require("./sshConnectionPool.cjs"); +const { shouldRegisterFreshSftpTransport } = require("./sftpBridge/openConnection.cjs"); + +test("sudo SFTP connections are never parked in the shared transport registry", () => { + assert.equal(shouldRegisterFreshSftpTransport({ sudo: true, reuseTransport: true }), false); + assert.equal(shouldRegisterFreshSftpTransport({ sudo: false, reuseTransport: true }), true); +}); /** Build a minimal renderer sender for SFTP progress and auth prompt IPC. */ function makeSender() { @@ -40,7 +52,16 @@ function loadSftpBridgeWithAuthRetryMocks(t, options = {}) { this.connect(opts); }; this.client.sftp = (cb) => { - setImmediate(() => cb(null, new EventEmitter())); + if (options.sftpError) { + setImmediate(() => cb(options.sftpError)); + return; + } + const channel = new EventEmitter(); + channel.readdir = () => {}; + channel.stat = () => {}; + channel.mkdir = () => {}; + channel.unlink = () => {}; + setImmediate(() => cb(null, channel)); }; this.client.end = () => { this.client.ended = true; @@ -58,6 +79,10 @@ function loadSftpBridgeWithAuthRetryMocks(t, options = {}) { setImmediate(() => { this.client.emit("connect"); this.client.emit("handshake"); + if (options.alwaysSucceed) { + this.client.emit("ready"); + return; + } const offerNext = (methodsLeft, partialSuccess) => { let nextMethod; opts.authHandler(methodsLeft, partialSuccess, (method) => { @@ -116,7 +141,10 @@ function loadSftpBridgeWithAuthRetryMocks(t, options = {}) { if (request === "./sshAuthHelper.cjs") { return { ...realAuthHelper, - findAllDefaultPrivateKeys: async () => options.defaultKeys || [], + findAllDefaultPrivateKeys: async () => { + if (options.defaultKeysError) throw options.defaultKeysError; + return options.defaultKeys || []; + }, getAvailableAgentSocket: async () => null, prepareSystemSshAgentForAuth: async () => null, }; @@ -135,6 +163,215 @@ function loadSftpBridgeWithAuthRetryMocks(t, options = {}) { return { bridge, MockSftpClient }; } +test("simultaneous SFTP browser opens share one physical SSH dial", async (t) => { + resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 }); + t.after(() => resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 })); + const { bridge, MockSftpClient } = loadSftpBridgeWithAuthRetryMocks(t, { alwaysSucceed: true }); + const sftpClients = new Map(); + bridge.init({ sftpClients, sessions: new Map(), electronModule: {} }); + const options = { + hostname: "192.168.9.138", + hostId: "host-sftp-shared", + port: 22, + username: "root", + authMethod: "password", + password: "saved-login-password", + useSshAgent: false, + verifyHostKeys: false, + fileProtocol: "sftp", + }; + + const [first, second] = await Promise.all([ + bridge.openSftp({ sender: makeSender() }, { ...options, sessionId: "sftp-shared-1" }), + bridge.openSftp({ sender: makeSender() }, { ...options, sessionId: "sftp-shared-2" }), + ]); + + assert.equal(first.sftpId, "sftp-shared-1"); + assert.equal(second.sftpId, "sftp-shared-2"); + assert.equal(MockSftpClient.instances.length, 1); + assert.equal(sftpClients.size, 2); +}); + +test("sequential SFTP opens without a terminal authenticate only once", async (t) => { + resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 }); + t.after(() => resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 })); + const { bridge, MockSftpClient } = loadSftpBridgeWithAuthRetryMocks(t, { alwaysSucceed: true }); + const sftpClients = new Map(); + bridge.init({ sftpClients, sessions: new Map(), electronModule: {} }); + const options = { + hostname: "no-terminal.example", + hostId: "host-no-terminal", + port: 22, + username: "root", + authMethod: "password", + password: "saved-login-password", + useSshAgent: false, + verifyHostKeys: false, + fileProtocol: "sftp", + }; + + const first = await bridge.openSftp( + { sender: makeSender() }, + { ...options, sessionId: "sftp-no-terminal-1" }, + ); + await bridge.closeSftp(null, { sftpId: first.sftpId }); + assert.equal(sftpClients.size, 0); + assert.equal(getTransportStats().idle, 1); + + const second = await bridge.openSftp( + { sender: makeSender() }, + { ...options, sessionId: "sftp-no-terminal-2" }, + ); + assert.equal(second.sftpId, "sftp-no-terminal-2"); + assert.equal(MockSftpClient.instances.length, 1, "the second open must not repeat SSH authentication"); + assert.equal(sftpClients.size, 1); +}); + +test("explicitly dedicated SFTP opens stay outside shared dial coordination", async (t) => { + resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 }); + t.after(() => resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 })); + const { bridge, MockSftpClient } = loadSftpBridgeWithAuthRetryMocks(t, { alwaysSucceed: true }); + const sftpClients = new Map(); + bridge.init({ sftpClients, sessions: new Map(), electronModule: {} }); + const options = { + hostname: "192.168.9.138", + hostId: "host-sftp-dedicated", + port: 22, + username: "root", + authMethod: "password", + password: "saved-login-password", + useSshAgent: false, + verifyHostKeys: false, + fileProtocol: "sftp", + reuseTransport: false, + }; + + await Promise.all([ + bridge.openSftp({ sender: makeSender() }, { ...options, sessionId: "sftp-dedicated-1" }), + bridge.openSftp({ sender: makeSender() }, { ...options, sessionId: "sftp-dedicated-2" }), + ]); + + assert.equal(MockSftpClient.instances.length, 2); + assert.deepEqual(getTransportStats(), { + transports: 0, + pendingDials: 0, + live: 0, + idle: 0, + leases: 0, + defaultIdleTtlMs: 0, + }); + + await bridge.openSftp( + { sender: makeSender() }, + { ...options, reuseTransport: true, sessionId: "sftp-shareable-after-dedicated" }, + ); + assert.equal( + MockSftpClient.instances.length, + 3, + "a later shareable open must dial instead of borrowing a dedicated connection", + ); +}); + +test("early SFTP preparation failure releases the coordinated dial", async (t) => { + resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 }); + t.after(() => resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 })); + const { bridge } = loadSftpBridgeWithAuthRetryMocks(t, { + alwaysSucceed: true, + defaultKeysError: new Error("identity discovery failed"), + }); + bridge.init({ sftpClients: new Map(), sessions: new Map(), electronModule: {} }); + const options = { + hostname: "early-failure.example", + hostId: "host-early-failure", + username: "root", + authMethod: "password", + password: "secret", + useSshAgent: false, + }; + + await assert.rejects( + bridge.openSftp({ sender: makeSender() }, options), + /identity discovery failed/, + ); + assert.equal(getTransportStats().pendingDials, 0); + + const retry = beginTransportDial(options, { kind: "channel" }); + assert.equal(retry.role, "leader", "the failed opener must not leave a permanent join slot"); + failTransportDial(retry, new Error("test cleanup")); +}); + +test("forced SFTP subsystem rejection closes the authenticated client without retaining pool state", async (t) => { + resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 }); + t.after(() => resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 })); + const { bridge, MockSftpClient } = loadSftpBridgeWithAuthRetryMocks(t, { + alwaysSucceed: true, + sftpError: new Error("SFTP subsystem rejected"), + }); + const sftpClients = new Map(); + bridge.init({ sftpClients, sessions: new Map(), electronModule: {} }); + + await assert.rejects( + bridge.openSftp( + { sender: makeSender() }, + { + sessionId: "sftp-subsystem-rejected", + hostname: "no-sftp.example", + hostId: "host-no-sftp", + username: "root", + authMethod: "password", + password: "secret", + useSshAgent: false, + verifyHostKeys: false, + fileProtocol: "sftp", + }, + ), + /SFTP subsystem rejected/, + ); + + assert.equal(MockSftpClient.instances.length, 1); + assert.equal(MockSftpClient.instances[0].client.ended, true); + assert.equal(MockSftpClient.instances[0].client.destroyed, true); + assert.equal(sftpClients.size, 0); + assert.deepEqual(getTransportStats(), { + transports: 0, + pendingDials: 0, + live: 0, + idle: 0, + leases: 0, + defaultIdleTtlMs: 0, + }); +}); + +test("concurrent SFTP waiter survives the leader's recoverable auth retry", async (t) => { + resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 }); + t.after(() => resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 })); + const { bridge, MockSftpClient } = loadSftpBridgeWithAuthRetryMocks(t); + const sftpClients = new Map(); + bridge.init({ sftpClients, sessions: new Map(), electronModule: {} }); + const options = { + hostname: "retry-shared.example", + hostId: "host-retry-shared", + port: 22, + username: "root", + authMethod: "password", + password: "saved-login-password", + useSshAgent: false, + verifyHostKeys: false, + fileProtocol: "sftp", + }; + + const [leader, waiter] = await Promise.all([ + bridge.openSftp({ sender: makeSender() }, { ...options, sessionId: "sftp-retry-leader" }), + bridge.openSftp({ sender: makeSender() }, { ...options, sessionId: "sftp-retry-waiter" }), + ]); + + assert.equal(leader.sftpId, "sftp-retry-leader"); + assert.equal(waiter.sftpId, "sftp-retry-waiter"); + assert.equal(MockSftpClient.instances.length, 2, "only the leader's initial and retry dials are physical"); + assert.equal(sftpClients.size, 2); + assert.equal(getTransportStats().pendingDials, 0); +}); + test("openSftp retries keyboard-interactive first when password rejection removes KI", async (t) => { const { bridge, MockSftpClient } = loadSftpBridgeWithAuthRetryMocks(t); const sftpClients = new Map(); diff --git a/electron/bridges/sftpBridge.cjs b/electron/bridges/sftpBridge.cjs index d7713cd5c..19b06a94a 100644 --- a/electron/bridges/sftpBridge.cjs +++ b/electron/bridges/sftpBridge.cjs @@ -9,6 +9,10 @@ const net = require("node:net"); const { createHash, randomUUID } = require("node:crypto"); const { pipeline } = require("node:stream/promises"); const { TextDecoder } = require("node:util"); +const { StringDecoder } = require("node:string_decoder"); +const { executeBoundedSshCommand } = require("./boundedSshExec.cjs"); +const { openBoundedSftpChannel } = require("./boundedSftpOpen.cjs"); +const { invalidateSshTransport } = require("./sshTransportInvalidation.cjs"); require("./boringSslDhCompat.cjs").installBoringSslDhCompat(); const SftpClient = require("ssh2-sftp-client"); const { Client: SSHClient } = require("ssh2"); @@ -52,14 +56,14 @@ let electronModule = null; let sessions = null; let reportOpenedSessionActivity = null; const rendererSftpSourceSessions = new Map(); +const REMOTE_DELETE_EXEC_OPEN_TIMEOUT_MS = 15_000; +const REMOTE_DELETE_EXEC_RUN_TIMEOUT_MS = 10 * 60_000; +const REMOTE_DELETE_EXEC_MAX_OUTPUT_BYTES = 64 * 1024; +const REMOTE_BACKUP_DELETE_ATTEMPTS = 3; // Storage for jump host connections that need to be cleaned up const jumpConnectionsMap = new Map(); // connId -> { connections: SSHClient[], socket: stream } -// Storage for active buffer uploads that can be cancelled (writeBinaryWithProgress). -// Bulk panel transfers cancel via transferBridge; this map is only the buffer path. -const activeSftpUploads = new Map(); // transferId -> { cancelled: boolean, transfer?: { cancelled, abort } } - // Track requested/resolved filename encoding per SFTP session const sftpEncodingState = new Map(); // stateKey -> { requested: 'auto'|'utf-8'|'gb18030', resolved: 'utf-8'|'gb18030' } const utf8Decoder = new TextDecoder("utf-8", { fatal: true }); @@ -189,8 +193,6 @@ const hasSftpChannelApi = (value) => typeof value.mkdir === "function" && typeof value.unlink === "function"; -const DEFAULT_SFTP_CHANNEL_OPEN_TIMEOUT_MS = 10_000; - function createAbortError(signal, fallbackMessage = "The operation was aborted.") { const reason = signal?.reason; if (reason instanceof Error) { @@ -210,73 +212,7 @@ function isAbortError(error) { } const tryOpenSftpChannel = (client, options = {}) => - new Promise((resolve, reject) => { - const sshClient = client?.client; - if (!sshClient || typeof sshClient.sftp !== "function") { - resolve(null); - return; - } - const signal = options?.signal || null; - const timeoutMs = Number.isFinite(options?.timeoutMs) && options.timeoutMs > 0 - ? options.timeoutMs - : DEFAULT_SFTP_CHANNEL_OPEN_TIMEOUT_MS; - if (signal?.aborted) { - reject(createAbortError(signal, "SFTP channel open was aborted")); - return; - } - let settled = false; - let timer = null; - const cleanup = () => { - if (timer) { - clearTimeout(timer); - timer = null; - } - if (signal) { - signal.removeEventListener("abort", onAbort); - } - }; - const closeOrphanedChannel = (sftp) => { - try { sftp?.end?.(); } catch {} - try { sftp?.close?.(); } catch {} - }; - const finishReject = (err) => { - if (settled) return; - settled = true; - cleanup(); - reject(err); - }; - const finishResolve = (sftp) => { - if (settled) { - closeOrphanedChannel(sftp); - return; - } - settled = true; - cleanup(); - resolve(sftp || null); - }; - const onAbort = () => { - finishReject(createAbortError(signal, "SFTP channel open was aborted")); - }; - if (signal) { - signal.addEventListener("abort", onAbort, { once: true }); - } - if (timeoutMs) { - timer = setTimeout(() => { - finishReject(new Error(`SFTP channel open timed out after ${timeoutMs}ms`)); - }, timeoutMs); - } - try { - sshClient.sftp((err, sftp) => { - if (err) { - finishReject(err); - return; - } - finishResolve(sftp); - }); - } catch (err) { - finishReject(err); - } - }); + openBoundedSftpChannel(client?.client, options); const getSftpChannel = async (client, options = {}) => { if (!client) return null; @@ -559,25 +495,71 @@ const removeRemotePathInternal = async (sftp, targetPath, encoding, signal = nul * Run a one-shot remote shell command on the SSH connection underlying an SFTP client. * Used for fast directory delete (`rm -rf`) when SFTP-protocol recursion would be slow. */ -async function execRemoteShellCommand(sshClient, command, signal = null) { +function terminateRemoteShellStream(stream) { + if (!stream) return; + try { stream.once?.("error", () => {}); } catch { /* ignore */ } + try { stream.stderr?.once?.("error", () => {}); } catch { /* ignore */ } + try { stream.close?.(); } catch { /* ignore */ } + try { stream.end?.(); } catch { /* ignore */ } + try { stream.destroy?.(); } catch { /* ignore */ } +} + +async function execRemoteShellCommand(sshClient, command, optionsOrSignal = null) { if (!sshClient || typeof sshClient.exec !== "function") { throw new Error("SSH exec unavailable"); } + const isAbortSignal = optionsOrSignal + && typeof optionsOrSignal === "object" + && typeof optionsOrSignal.addEventListener === "function" + && typeof optionsOrSignal.aborted === "boolean"; + const options = isAbortSignal ? { signal: optionsOrSignal } : (optionsOrSignal || {}); + const signal = options.signal || null; + const openingTimeoutMs = Math.max( + 1, + Number(options.openingTimeoutMs) || REMOTE_DELETE_EXEC_OPEN_TIMEOUT_MS, + ); + const runTimeoutMs = Math.max( + 1, + Number(options.runTimeoutMs) || REMOTE_DELETE_EXEC_RUN_TIMEOUT_MS, + ); + const maxOutputBytes = Math.max( + 1, + Number(options.maxOutputBytes) || REMOTE_DELETE_EXEC_MAX_OUTPUT_BYTES, + ); return await new Promise((resolve, reject) => { let settled = false; let streamRef = null; + let openingTimer = null; + let runTimer = null; + let outputBytes = 0; let stdout = ""; let stderr = ""; - const finish = (error, code = 0) => { - if (settled) return; - settled = true; + const stdoutDecoder = new StringDecoder("utf8"); + const stderrDecoder = new StringDecoder("utf8"); + let decodersEnded = false; + const cleanup = () => { + if (openingTimer) clearTimeout(openingTimer); + if (runTimer) clearTimeout(runTimer); + openingTimer = null; + runTimer = null; if (signal) { try { signal.removeEventListener("abort", onAbort); } catch { /* ignore */ } } + }; + const finish = (error, code = 0) => { + if (settled) return; + settled = true; + cleanup(); if (error) { + terminateRemoteShellStream(streamRef); reject(error); return; } + if (!decodersEnded) { + decodersEnded = true; + stdout += stdoutDecoder.end(); + stderr += stderrDecoder.end(); + } if (code === 0) { resolve({ stdout, stderr, code }); return; @@ -587,9 +569,19 @@ async function execRemoteShellCommand(sshClient, command, signal = null) { )); }; const onAbort = () => { - try { streamRef?.close?.(); } catch { /* ignore */ } - try { streamRef?.destroy?.(); } catch { /* ignore */ } finish(createAbortError(signal, "Remote command was aborted")); + if (!streamRef) invalidateSshTransport(sshClient); + }; + const appendOutput = (target, chunk) => { + if (settled) return; + const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk)); + outputBytes += buffer.length; + if (outputBytes > maxOutputBytes) { + finish(new Error(`Remote command output exceeded ${maxOutputBytes} bytes`)); + return; + } + if (target === "stdout") stdout += stdoutDecoder.write(buffer); + else stderr += stderrDecoder.write(buffer); }; if (signal) { if (signal.aborted) { @@ -598,20 +590,30 @@ async function execRemoteShellCommand(sshClient, command, signal = null) { } signal.addEventListener("abort", onAbort, { once: true }); } + openingTimer = setTimeout(() => { + finish(new Error(`Remote command open timed out after ${openingTimeoutMs} ms`)); + invalidateSshTransport(sshClient); + }, openingTimeoutMs); try { sshClient.exec(command, (error, stream) => { + if (openingTimer) clearTimeout(openingTimer); + openingTimer = null; + if (settled) { + terminateRemoteShellStream(stream); + return; + } if (error) { finish(error); return; } streamRef = stream; - if (settled) { - try { stream.close?.(); } catch { /* ignore */ } - return; - } - stream.on("data", (chunk) => { stdout += chunk.toString(); }); + runTimer = setTimeout(() => { + finish(new Error(`Remote command execution timed out after ${runTimeoutMs} ms`)); + }, runTimeoutMs); + stream.on("data", (chunk) => { appendOutput("stdout", chunk); }); if (stream.stderr && typeof stream.stderr.on === "function") { - stream.stderr.on("data", (chunk) => { stderr += chunk.toString(); }); + stream.stderr.on("data", (chunk) => { appendOutput("stderr", chunk); }); + stream.stderr.on("error", (streamError) => finish(streamError)); } stream.on("close", (code) => finish(null, code ?? 0)); stream.on("error", (streamError) => finish(streamError)); @@ -647,7 +649,7 @@ async function tryFastShellDirectoryDelete(client, remotePath, encoding = "utf-8 } try { - await execRemoteShellCommand(sshClient, command, signal); + await execRemoteShellCommand(sshClient, command, { signal }); } catch { return false; } @@ -774,12 +776,72 @@ function buildBackupRemotePath(remotePath) { const lastSeparatorIndex = Math.max(remotePath.lastIndexOf("/"), remotePath.lastIndexOf("\\")); const dir = lastSeparatorIndex >= 0 ? remotePath.slice(0, lastSeparatorIndex + 1) : ""; const baseName = lastSeparatorIndex >= 0 ? remotePath.slice(lastSeparatorIndex + 1) : remotePath; - // ".netcatty-backup-" (17) + 8 hex + "-" (1) + ".bak" (4) = 30 - const safeBaseName = clipRemoteBaseName(baseName, 30); - const backupName = `.netcatty-backup-${randomUUID().slice(0, 8)}-${safeBaseName}.bak`; + // One stable recovery path per destination prevents repeated retries from + // leaving a new random backup each time cleanup is temporarily unavailable. + // ".netcatty-backup-" (17) + 16 hex + "-" (1) + ".bak" (4) = 38 + const safeBaseName = clipRemoteBaseName(baseName, 38); + const targetHash = createHash("sha256").update(String(remotePath)).digest("hex").slice(0, 16); + const backupName = `.netcatty-backup-${targetHash}-${safeBaseName}.bak`; return dir ? `${dir}${backupName}` : backupName; } +function createRemoteBackupCleanupError(cleanupError, paths = {}) { + const error = new Error( + `Remote upload completed, but the original backup could not be removed. ` + + `Final: ${String(paths.finalPath || "unknown")}; backup: ${String(paths.backupPath || "unknown")}: ` + + `${cleanupError?.message || String(cleanupError)}`, + { cause: cleanupError }, + ); + error.recoverable = true; + error.remoteBackupPath = paths.backupPath || null; + error.remoteFinalPath = paths.finalPath || null; + return error; +} + +async function deleteRemoteBackupWithRetry(deleteBackup, paths = {}, options = {}) { + const attempts = Math.max(1, Number(options.attempts) || REMOTE_BACKUP_DELETE_ATTEMPTS); + const delay = options.delay || ((attempt) => new Promise((resolve) => { + setTimeout(resolve, attempt * 25); + })); + let lastError = null; + for (let attempt = 1; attempt <= attempts; attempt += 1) { + try { + await deleteBackup(); + return; + } catch (error) { + lastError = error; + if (attempt < attempts) await delay(attempt); + } + } + throw createRemoteBackupCleanupError(lastError, paths); +} + +async function readRemotePathIfPresent(statPath) { + try { + return await statPath(); + } catch (error) { + if (isRemoteMissingError(error)) return null; + throw error; + } +} + +async function reconcileRemoteUploadBackup(options) { + const backup = await readRemotePathIfPresent(options.statBackup); + if (!backup) return; + const target = await readRemotePathIfPresent(options.statTarget); + if (!target) { + // An interrupted promotion left the backup as the only known-good copy. + await options.restoreBackup(); + return; + } + // The final target is live, so the stable backup belongs to an earlier + // committed upload. Remove it before this attempt creates a new backup. + await deleteRemoteBackupWithRetry(options.deleteBackup, { + finalPath: options.finalPath, + backupPath: options.backupPath, + }); +} + function isRemotePermissionError(err) { // Codes only — do not match message substrings (filenames may contain // "permission"/"access"/"denied" and must not trigger in-place fallback). @@ -800,6 +862,28 @@ function isRemoteMissingError(err) { || String(err?.message || "").trim() === "ENOENT"; } +function isRemoteUnsupportedError(err) { + const code = err?.code; + return code === 8 + || code === "ENOTSUP" + || code === "EOPNOTSUPP" + || code === "SSH_FX_OP_UNSUPPORTED"; +} + +async function inspectRemoteRecoveryEntry(sftp, targetPath) { + try { + return await lstatAsync(sftp, targetPath); + } catch (error) { + // Some SFTP v3 servers expose lstat but reject the request at runtime. + // A Netcatty-created recovery artifact is always a regular file, so stat + // remains a safe compatibility fallback for this narrow inspection. + if (typeof sftp?.lstat === "function" && isRemoteUnsupportedError(error)) { + return statAsync(sftp, targetPath); + } + throw error; + } +} + function attrsIndicateSymlink(attrs) { if (!attrs) return false; if (typeof attrs.isSymbolicLink === "function") return !!attrs.isSymbolicLink(); @@ -971,52 +1055,25 @@ async function hashReadableForDigest(readable, signal = null) { async function tryRemoteSha256Sum(sshClient, remotePath, signal = null) { if (!sshClient || typeof sshClient.exec !== "function") return null; const escapedPath = String(remotePath).replace(/'/g, "'\\''"); - return await new Promise((resolve) => { - let settled = false; - let streamRef = null; - const finish = (value) => { - if (settled) return; - settled = true; - if (signal) { - try { signal.removeEventListener("abort", onAbort); } catch { /* ignore */ } - } - resolve(value); - }; - const onAbort = () => { - try { streamRef?.close?.(); } catch { /* ignore */ } - try { streamRef?.destroy?.(); } catch { /* ignore */ } - finish(null); - }; - if (signal) { - if (signal.aborted) { - finish(null); - return; - } - signal.addEventListener("abort", onAbort, { once: true }); + try { + const result = await executeBoundedSshCommand( + sshClient, + `sha256sum -- '${escapedPath}'`, + { + signal, + openingTimeoutMs: 15_000, + runTimeoutMs: 10 * 60_000, + maxOutputBytes: 64 * 1024, + }, + ); + const match = result.stdout.match(/^([a-fA-F0-9]{64})\s/); + return result.code === 0 && match ? match[1].toLowerCase() : null; + } catch (error) { + if (signal?.aborted || isAbortError(error)) { + throw createAbortError(signal, "Remote target verification was aborted"); } - try { - sshClient.exec(`sha256sum -- '${escapedPath}'`, (error, stream) => { - if (error) { - finish(null); - return; - } - streamRef = stream; - if (settled) { - try { stream.close?.(); } catch { /* ignore */ } - return; - } - let stdout = ""; - stream.on("data", (chunk) => { stdout += chunk.toString(); }); - stream.on("close", (code) => { - const match = stdout.match(/^([a-fA-F0-9]{64})\s/); - finish(code === 0 && match ? match[1].toLowerCase() : null); - }); - stream.on("error", () => finish(null)); - }); - } catch { - finish(null); - } - }); + return null; + } } /** @@ -1311,7 +1368,10 @@ async function promoteScpStagedUpload( throw promotionError; } if (movedExisting) { - try { await backend.remove(backupPath, { recursive: false, encoding }); } catch { /* ignore */ } + await deleteRemoteBackupWithRetry( + () => backend.remove(backupPath, { recursive: false, encoding }), + { finalPath: targetPath, backupPath }, + ); } } @@ -1343,6 +1403,42 @@ async function runRemoteUploadTransaction(client, localPath, remotePath, options const { isScpModeClient, getScpBackendForClient } = require("./sftpBridge/scpBackend.cjs"); const scpMode = isScpModeClient(client); const encodedPath = encodePath(remotePath, encoding); + // Build stable recovery paths before inspecting the destination. A previous + // interrupted/unclean promotion must be reconciled before we snapshot the + // target for this upload. + const stagedLogical = options?.stagedPath || buildStagedRemotePath(remotePath); + const backupLogical = options?.backupPath || buildBackupRemotePath(remotePath); + const encodedStagedPath = encodePath(stagedLogical, encoding); + const encodedBackupPath = encodePath(backupLogical, encoding); + if (scpMode) { + const backend = getScpBackendForClient(client); + await reconcileRemoteUploadBackup({ + finalPath: remotePath, + backupPath: backupLogical, + statTarget: () => runCancelablePreflight(() => backend.stat(remotePath, { encoding, signal })), + statBackup: () => runCancelablePreflight(() => backend.stat(backupLogical, { encoding, signal })), + restoreBackup: () => backend.rename(backupLogical, remotePath, { encoding }), + deleteBackup: () => backend.remove(backupLogical, { recursive: false, encoding }), + }); + } else { + // Inspect recovery artifacts through the raw channel. Every production + // client has one, while `client.stat` is only a convenience supplied by + // some adapters. Recovery must not make otherwise-valid raw SFTP clients + // fail before the normal upload plan can run. + const recoverySftp = await requireSftpChannel(client, { signal }); + await reconcileRemoteUploadBackup({ + finalPath: remotePath, + backupPath: backupLogical, + statTarget: () => runCancelablePreflight(() => inspectRemoteRecoveryEntry(recoverySftp, encodedPath)), + statBackup: () => runCancelablePreflight(() => inspectRemoteRecoveryEntry(recoverySftp, encodedBackupPath)), + restoreBackup: () => client.rename(encodedBackupPath, encodedPath), + deleteBackup: () => ( + typeof client.delete === "function" + ? client.delete(encodedBackupPath) + : unlinkAsync(recoverySftp, encodedBackupPath) + ), + }); + } const plan = await runCancelablePreflight(() => ( scpMode ? planScpRemoteUploadReplace(client, remotePath, encoding, signal) @@ -1404,11 +1500,6 @@ async function runRemoteUploadTransaction(client, localPath, remotePath, options return uploadDirect(); } - // Build stage/backup names from the logical string, then encode each path. - const stagedLogical = options?.stagedPath || buildStagedRemotePath(remotePath); - const backupLogical = options?.backupPath || buildBackupRemotePath(remotePath); - const encodedStagedPath = encodePath(stagedLogical, encoding); - const encodedBackupPath = encodePath(backupLogical, encoding); const cleanupStage = async () => { try { if (scpMode) { @@ -1568,11 +1659,13 @@ async function renameRemotePath(client, fromPath, toPath, backupPath = null, rec } if (movedExistingTarget) { - try { - await client.delete(backupPath); - } catch { - // Ignore backup cleanup failures after the final file is in place. - } + await deleteRemoteBackupWithRetry( + () => client.delete(backupPath), + { + finalPath: recoveryPaths.finalPath || toPath, + backupPath: recoveryPaths.backupPath || backupPath, + }, + ); } } } @@ -1691,8 +1784,8 @@ function createSessionBackedSftpClient(sessionId, sshClient, options = {}) { /** * Pipelined local→remote upload via the raw ssh2 SFTP channel. * Session-backed clients are not ssh2-sftp-client instances and do not - * inherit client.fastPut — expose the channel method so uploadLocal / - * writeSftpBinaryWithProgress keep the high-throughput path (#2449). + * inherit client.fastPut — expose the channel method so uploadLocal keeps + * the high-throughput path (#2449). * * When `options.signal` is provided, open a disposable SFTP channel so * abort can end the transfer without killing the browse session. @@ -1776,6 +1869,15 @@ async function openSftpForSession(_event, payload) { throwIfAborted(payload?.abortSignal); const { session, sshClient } = ensureRemoteSftpSupport(sessionId); + const actualEndpoint = session._reuseEndpoint || session.connRef?.endpoint; + if (payload?.expectedEndpoint) { + const expectedEndpoint = buildConnectionReuseEndpoint(payload.expectedEndpoint); + if (!actualEndpoint || !endpointAllowsReuse(expectedEndpoint, actualEndpoint, "channel")) { + const err = new Error("Source session SSH route does not match the requested SFTP endpoint"); + err.code = "ERR_SFTP_SOURCE_ROUTE_MISMATCH"; + throw err; + } + } const sftpId = `${sessionId}-sftp-${randomUUID()}`; const refHolder = { id: sftpId, __sshLeaseKind: "sftp" }; if (session.connRef && typeof acquireConnectionRef === "function") { @@ -1785,6 +1887,7 @@ async function openSftpForSession(_event, payload) { refHolder, sourceSessionId: sessionId, }); + client.__netcattyEndpointKey = session.connRef?.endpointKey || buildEndpointKey(actualEndpoint); const { normalizeFileProtocol } = require("./sftpBridge/scpShell.cjs"); const { getScpBackendForClient } = require("./sftpBridge/scpBackend.cjs"); // Prefer explicit payload, then the host preference stored when the SSH @@ -2142,6 +2245,14 @@ const { createTransport, borrowTransport, findTransportByEndpoint, + beginTransportDial, + waitForTransportDial, + completeTransportDial, + failTransportDial, + buildEndpointKey, + buildConnectionReuseEndpoint, + resolveConnectionKeepalivePolicy, + endpointAllowsReuse, } = require("./sshConnectionPool.cjs"); const openConnectionApi = createOpenConnectionApi({ get sftpClients() { return sftpClients; }, @@ -2159,13 +2270,17 @@ const openConnectionApi = createOpenConnectionApi({ connectSudoSftp: undefined, acquireConnectionRef, releaseConnectionRef, findReusableSession, createSessionBackedSftpClient, createTransport, borrowTransport, findTransportByEndpoint, + beginTransportDial, waitForTransportDial, completeTransportDial, failTransportDial, + buildEndpointKey, + buildConnectionReuseEndpoint, + resolveConnectionKeepalivePolicy, }); const { connectThroughChainForSftp, connectSudoSftp, openSftp } = openConnectionApi; const { createFileOpsApi } = require("./sftpBridge/fileOps.cjs"); const fileOpsApi = createFileOpsApi({ get sftpClients() { return sftpClients; }, get electronModule() { return electronModule; }, - activeSftpUploads, fileWatcherBridge, fs, path, Buffer, console, setTimeout, clearTimeout, + fileWatcherBridge, fs, path, Buffer, console, setTimeout, clearTimeout, jumpConnectionsMap, sftpEncodingState, normalizeEncoding, isAsciiString, requireSftpChannel, resolveEncodingForRequest, updateResolvedEncoding, encodePath, decodeName, detectEncodingFromList, statResultFromAttrs, normalizeRemotePathString, collectReadable, writeToWritable, @@ -2179,12 +2294,11 @@ const fileOpsApi = createFileOpsApi({ }); const { listSftp, + realpathSftp, readSftp, readSftpBinary, writeSftp, writeSftpBinary, - writeSftpBinaryWithProgress, - cancelSftpUpload, closeSftp, mkdirSftp, deleteSftp, @@ -2213,14 +2327,119 @@ function reportSftpActivity(sessionId, phase) { } } -function registerActivityHandle(ipcMain, channel, handler) { +function createRendererSftpOwnership(closeOwnedSftp) { + const owners = new Map(); + const isOpenChannel = (channel) => ( + channel === "netcatty:sftp:open" || channel === "netcatty:sftp:openForSession" + ); + const cleanupOwnerIfDone = (entry) => { + if (!entry?.destroyed || entry.pendingOpens > 0 || entry.sftpIds.size > 0) return; + owners.delete(entry.senderId); + }; + const closeForOwner = async (entry, sftpId) => { + entry.sftpIds.delete(sftpId); + try { + await closeOwnedSftp(sftpId, entry.senderId); + } catch (error) { + console.warn(`[SFTP] Failed to close renderer-owned session ${sftpId}:`, error?.message || error); + } finally { + cleanupOwnerIfDone(entry); + } + }; + const getOwner = (sender) => { + const senderId = sender?.id; + if (!Number.isSafeInteger(senderId)) return null; + let entry = owners.get(senderId); + if (entry) return entry; + entry = { + senderId, + sender, + destroyed: sender?.isDestroyed?.() === true, + pendingOpens: 0, + sftpIds: new Set(), + }; + owners.set(senderId, entry); + if (!entry.destroyed && typeof sender?.once === "function") { + sender.once("destroyed", () => { + entry.destroyed = true; + for (const sftpId of [...entry.sftpIds]) { + void closeForOwner(entry, sftpId); + } + cleanupOwnerIfDone(entry); + }); + } + return entry; + }; + return { + shouldRememberOpenResult(sender, sftpId) { + const senderId = sender?.id; + if (!Number.isSafeInteger(senderId)) return true; + return owners.get(senderId)?.sftpIds.has(sftpId) === true; + }, + async run(channel, event, payload, handler) { + if (isOpenChannel(channel)) { + const entry = getOwner(event?.sender); + if (!entry) return handler(); + entry.pendingOpens += 1; + try { + const result = await handler(); + const sftpId = result?.sftpId; + if (sftpId) { + if (entry.destroyed || entry.sender?.isDestroyed?.() === true) { + entry.destroyed = true; + await closeForOwner(entry, sftpId); + } else { + entry.sftpIds.add(sftpId); + } + } + return result; + } finally { + entry.pendingOpens -= 1; + cleanupOwnerIfDone(entry); + } + } + if (channel === "netcatty:sftp:close" && payload?.sftpId) { + const claimedEntries = []; + for (const entry of owners.values()) { + if (entry.sftpIds.delete(payload.sftpId)) { + claimedEntries.push(entry); + } + cleanupOwnerIfDone(entry); + } + try { + return await handler(); + } catch (error) { + await Promise.all(claimedEntries.map(async (entry) => { + if (entry.destroyed || entry.sender?.isDestroyed?.() === true) { + entry.destroyed = true; + await closeForOwner(entry, payload.sftpId); + } else { + entry.sftpIds.add(payload.sftpId); + } + })); + throw error; + } + } + return handler(); + }, + }; +} + +function registerActivityHandle(ipcMain, channel, handler, ownership = null) { ipcMain.handle(channel, async (event, payload) => { const sourceSessionId = resolveRendererSftpSourceSession(channel, payload); reportSftpActivity(sourceSessionId, "begin"); try { - const result = await handler(event, payload); + const invoke = () => handler(event, payload); + const result = ownership + ? await ownership.run(channel, event, payload, invoke) + : await invoke(); const sftpId = result?.sftpId; - if (sourceSessionId && sftpId) { + if ( + sourceSessionId + && sftpId + && (!ownership || ownership.shouldRememberOpenResult(event?.sender, sftpId)) + ) { rendererSftpSourceSessions.set(sftpId, sourceSessionId); } return result; @@ -2233,12 +2452,12 @@ function registerActivityHandle(ipcMain, channel, handler) { }); } -function registerWorkerHandle(ipcMain, terminalWorkerManager, channel) { +function registerWorkerHandle(ipcMain, terminalWorkerManager, channel, ownership) { registerActivityHandle(ipcMain, channel, (event, payload) => ( terminalWorkerManager.request(channel, payload, { webContentsId: event?.sender?.id, }) - )); + ), ownership); } /** @@ -2246,19 +2465,28 @@ function registerWorkerHandle(ipcMain, terminalWorkerManager, channel) { */ function registerHandlers(ipcMain, options = {}) { const terminalWorkerManager = options.terminalWorkerManager || null; + const ownership = createRendererSftpOwnership((sftpId, webContentsId) => { + rendererSftpSourceSessions.delete(sftpId); + return terminalWorkerManager + ? terminalWorkerManager.request( + "netcatty:sftp:close", + { sftpId }, + { webContentsId }, + ) + : closeSftp(null, { sftpId }); + }); if (terminalWorkerManager) { [ "netcatty:sftp:open", "netcatty:sftp:openForSession", "netcatty:sftp:list", + "netcatty:sftp:realpath", "netcatty:sftp:read", "netcatty:sftp:readBinary", "netcatty:sftp:write", "netcatty:sftp:writeBinary", - "netcatty:sftp:writeBinaryWithProgress", "netcatty:sftp:downloadToLocal", "netcatty:sftp:uploadLocal", - "netcatty:sftp:cancelUpload", "netcatty:sftp:close", "netcatty:sftp:mkdir", "netcatty:sftp:delete", @@ -2266,21 +2494,20 @@ function registerHandlers(ipcMain, options = {}) { "netcatty:sftp:stat", "netcatty:sftp:chmod", "netcatty:sftp:homeDir", - ].forEach((channel) => registerWorkerHandle(ipcMain, terminalWorkerManager, channel)); + ].forEach((channel) => registerWorkerHandle(ipcMain, terminalWorkerManager, channel, ownership)); return; } [ ["netcatty:sftp:open", openSftp], ["netcatty:sftp:openForSession", openSftpForSession], ["netcatty:sftp:list", listSftp], + ["netcatty:sftp:realpath", realpathSftp], ["netcatty:sftp:read", readSftp], ["netcatty:sftp:readBinary", readSftpBinary], ["netcatty:sftp:write", writeSftp], ["netcatty:sftp:writeBinary", writeSftpBinary], - ["netcatty:sftp:writeBinaryWithProgress", writeSftpBinaryWithProgress], ["netcatty:sftp:downloadToLocal", downloadSftpToLocal], ["netcatty:sftp:uploadLocal", uploadLocalToSftp], - ["netcatty:sftp:cancelUpload", cancelSftpUpload], ["netcatty:sftp:close", closeSftp], ["netcatty:sftp:mkdir", mkdirSftp], ["netcatty:sftp:delete", deleteSftp], @@ -2288,7 +2515,7 @@ function registerHandlers(ipcMain, options = {}) { ["netcatty:sftp:stat", statSftp], ["netcatty:sftp:chmod", chmodSftp], ["netcatty:sftp:homeDir", getSftpHomeDir], - ].forEach(([channel, handler]) => registerActivityHandle(ipcMain, channel, handler)); + ].forEach(([channel, handler]) => registerActivityHandle(ipcMain, channel, handler, ownership)); } /** @@ -2312,18 +2539,21 @@ module.exports = { clearSftpEncodingStateByPrefix, openSftpForSession, openSftp, + _createRendererSftpOwnershipForTests: createRendererSftpOwnership, listSftp, + realpathSftp, readSftp, readSftpBinary, writeSftp, writeSftpBinary, - writeSftpBinaryWithProgress, - cancelSftpUpload, downloadSftpToLocal, uploadLocalToSftp, pipelinedUploadLocalFile, runRemoteUploadTransaction, _renameRemotePathForTests: renameRemotePath, + _buildBackupRemotePathForTests: buildBackupRemotePath, + _deleteRemoteBackupWithRetryForTests: deleteRemoteBackupWithRetry, + _reconcileRemoteUploadBackupForTests: reconcileRemoteUploadBackup, closeSftp, mkdirSftp, deleteSftp, @@ -2332,4 +2562,6 @@ module.exports = { chmodSftp, getSftpHomeDir, resolveEncodingForRequest, + _execRemoteShellCommandForTests: execRemoteShellCommand, + _tryRemoteSha256SumForTests: tryRemoteSha256Sum, }; diff --git a/electron/bridges/sftpBridge.ownerCleanup.test.cjs b/electron/bridges/sftpBridge.ownerCleanup.test.cjs new file mode 100644 index 000000000..9ef6ffb98 --- /dev/null +++ b/electron/bridges/sftpBridge.ownerCleanup.test.cjs @@ -0,0 +1,172 @@ +const assert = require("node:assert/strict"); +const { EventEmitter } = require("node:events"); +const test = require("node:test"); + +const { + _createRendererSftpOwnershipForTests: createRendererSftpOwnership, + init, + registerHandlers, +} = require("./sftpBridge.cjs"); + +function sender(id) { + const value = new EventEmitter(); + value.id = id; + value.isDestroyed = () => value.destroyed === true; + value.destroyed = false; + value.destroy = () => { + value.destroyed = true; + value.emit("destroyed"); + }; + return value; +} + +function registerWorkerHandlers(request, reportOpenedSessionActivity) { + const handlers = new Map(); + init({ + electronModule: null, + reportOpenedSessionActivity, + sessions: new Map(), + sftpClients: new Map(), + }); + registerHandlers({ + handle(channel, handler) { + handlers.set(channel, handler); + }, + }, { + terminalWorkerManager: { request }, + }); + return handlers; +} + +test("destroying a renderer closes every SFTP handle it opened", async () => { + const closed = []; + const ownership = createRendererSftpOwnership(async (sftpId) => { closed.push(sftpId); }); + const owner = sender(1); + await ownership.run("netcatty:sftp:open", { sender: owner }, {}, async () => ({ sftpId: "sftp-1" })); + + owner.destroy(); + await new Promise((resolve) => setImmediate(resolve)); + assert.deepEqual(closed, ["sftp-1"]); +}); + +test("an SFTP open that finishes after renderer destruction is closed immediately", async () => { + const closed = []; + const ownership = createRendererSftpOwnership(async (sftpId) => { closed.push(sftpId); }); + const owner = sender(2); + let finishOpen; + const pending = ownership.run("netcatty:sftp:open", { sender: owner }, {}, () => ( + new Promise((resolve) => { finishOpen = resolve; }) + )); + + owner.destroy(); + finishOpen({ sftpId: "sftp-late" }); + await pending; + assert.deepEqual(closed, ["sftp-late"]); +}); + +test("an explicitly closed SFTP handle is not closed again on renderer destruction", async () => { + const closed = []; + const ownership = createRendererSftpOwnership(async (sftpId) => { closed.push(sftpId); }); + const owner = sender(3); + await ownership.run("netcatty:sftp:open", { sender: owner }, {}, async () => ({ sftpId: "sftp-3" })); + await ownership.run( + "netcatty:sftp:close", + { sender: owner }, + { sftpId: "sftp-3" }, + async () => ({ success: true }), + ); + + owner.destroy(); + await new Promise((resolve) => setImmediate(resolve)); + assert.deepEqual(closed, []); +}); + +test("renderer destruction does not close an SFTP handle again while explicit close is pending", async () => { + const ownerCleanupCloses = []; + const ownership = createRendererSftpOwnership(async (sftpId) => { + ownerCleanupCloses.push(sftpId); + }); + const owner = sender(31); + await ownership.run( + "netcatty:sftp:open", + { sender: owner }, + {}, + async () => ({ sftpId: "sftp-pending-close" }), + ); + let finishExplicitClose; + let explicitCloseCalls = 0; + const explicitClose = ownership.run( + "netcatty:sftp:close", + { sender: owner }, + { sftpId: "sftp-pending-close" }, + () => { + explicitCloseCalls += 1; + return new Promise((resolve) => { finishExplicitClose = resolve; }); + }, + ); + + owner.destroy(); + await new Promise((resolve) => setImmediate(resolve)); + assert.deepEqual(ownerCleanupCloses, []); + + finishExplicitClose({ success: true }); + await explicitClose; + assert.equal(explicitCloseCalls, 1); + assert.deepEqual(ownerCleanupCloses, []); +}); + +test("renderer destruction removes the source-session association for its SFTP handle", async () => { + const activity = []; + const closed = []; + const handlers = registerWorkerHandlers(async (channel, payload) => { + if (channel === "netcatty:sftp:openForSession") return { sftpId: "sftp-owned" }; + if (channel === "netcatty:sftp:close") closed.push(payload.sftpId); + return { success: true }; + }, (event) => activity.push(event)); + const owner = sender(4); + + await handlers.get("netcatty:sftp:openForSession")( + { sender: owner }, + { sessionId: "terminal-source" }, + ); + owner.destroy(); + await new Promise((resolve) => setImmediate(resolve)); + + activity.length = 0; + await handlers.get("netcatty:sftp:list")( + { sender: sender(40) }, + { sftpId: "sftp-owned", path: "/" }, + ); + assert.deepEqual(closed, ["sftp-owned"]); + assert.deepEqual(activity, []); +}); + +test("a late SFTP open does not restore its destroyed renderer source-session association", async () => { + const activity = []; + const closed = []; + let finishOpen; + const handlers = registerWorkerHandlers((channel, payload) => { + if (channel === "netcatty:sftp:openForSession") { + return new Promise((resolve) => { finishOpen = resolve; }); + } + if (channel === "netcatty:sftp:close") closed.push(payload.sftpId); + return Promise.resolve({ success: true }); + }, (event) => activity.push(event)); + const owner = sender(5); + + const pending = handlers.get("netcatty:sftp:openForSession")( + { sender: owner }, + { sessionId: "terminal-late-source" }, + ); + owner.destroy(); + finishOpen({ sftpId: "sftp-late-owned" }); + await pending; + + activity.length = 0; + await handlers.get("netcatty:sftp:list")( + { sender: sender(50) }, + { sftpId: "sftp-late-owned", path: "/" }, + ); + assert.deepEqual(closed, ["sftp-late-owned"]); + assert.deepEqual(activity, []); +}); diff --git a/electron/bridges/sftpBridge.readLimit.test.cjs b/electron/bridges/sftpBridge.readLimit.test.cjs new file mode 100644 index 000000000..f401b07b6 --- /dev/null +++ b/electron/bridges/sftpBridge.readLimit.test.cjs @@ -0,0 +1,194 @@ +"use strict"; + +const assert = require("node:assert/strict"); +const test = require("node:test"); +const { Readable } = require("node:stream"); +const { EventEmitter } = require("node:events"); + +const { createFileOpsApi } = require("./sftpBridge/fileOps.cjs"); + +const MAX_IN_MEMORY_READ_BYTES = 10 * 1024 * 1024; + +function createApi(client, overrides = {}) { + return createFileOpsApi({ + sftpClients: new Map([["sftp-1", client]]), + resolveEncodingForRequest: () => "utf-8", + encodePath: (value) => value, + Buffer, + ...overrides, + }); +} + +test("SFTP text reads reject oversized files before opening the data stream", async () => { + let streamOpened = false; + const sftp = { + createReadStream() { + streamOpened = true; + throw new Error("oversized file stream should not open"); + }, + }; + const api = createApi({}, { + requireSftpChannel: async () => sftp, + statAsync: async () => ({ size: MAX_IN_MEMORY_READ_BYTES + 1 }), + }); + + await assert.rejects( + api.readSftp(null, { sftpId: "sftp-1", path: "/large.log" }), + /10 MB.*download.*external app/i, + ); + assert.equal(streamOpened, false); +}); + +test("SFTP text reads still stream ordinary files", async () => { + const sftp = { + createReadStream() { + return Readable.from([Buffer.from("hel"), Buffer.from("lo")]); + }, + }; + const api = createApi({}, { + requireSftpChannel: async () => sftp, + statAsync: async () => ({ size: 5 }), + }); + + assert.equal( + await api.readSftp(null, { sftpId: "sftp-1", path: "/small.txt" }), + "hello", + ); +}); + +test("SFTP binary reads reject oversized files before opening the data stream", async () => { + let streamOpened = false; + const sftp = { + createReadStream() { + streamOpened = true; + throw new Error("oversized file stream should not open"); + }, + }; + const api = createApi({}, { + requireSftpChannel: async () => sftp, + statAsync: async () => ({ size: MAX_IN_MEMORY_READ_BYTES + 1 }), + }); + + await assert.rejects( + api.readSftpBinary(null, { sftpId: "sftp-1", path: "/large.bin" }), + /10 MB.*download.*external app/i, + ); + assert.equal(streamOpened, false); +}); + +test("SFTP reads stop at the byte limit when the remote file grows after stat", async () => { + let legacyGetCalled = false; + const stream = Readable.from([ + Buffer.alloc(MAX_IN_MEMORY_READ_BYTES), + Buffer.from("x"), + ]); + const sftp = { + createReadStream() { + return stream; + }, + }; + const api = createApi({ + async get() { + legacyGetCalled = true; + return Buffer.alloc(MAX_IN_MEMORY_READ_BYTES + 1); + }, + }, { + requireSftpChannel: async () => sftp, + statAsync: async () => ({ size: 1 }), + }); + + await assert.rejects( + api.readSftp(null, { sftpId: "sftp-1", path: "/growing.log" }), + /10 MB.*download.*external app/i, + ); + assert.equal(legacyGetCalled, false); + assert.equal(stream.destroyed, true); + assert.equal(stream.listenerCount("data"), 0); + assert.equal(stream.listenerCount("end"), 0); + assert.equal(stream.listenerCount("error"), 0); + assert.equal(stream.listenerCount("close"), 0); +}); + +test("SCP reads reject oversized files before starting the transfer", async () => { + let readStarted = false; + const backend = { + async stat() { + return { size: MAX_IN_MEMORY_READ_BYTES + 1 }; + }, + async readFile() { + readStarted = true; + throw new Error("oversized SCP transfer should not start"); + }, + }; + const api = createApi({ + __netcattyFileProtocol: "scp", + __netcattyScpBackend: backend, + }); + + await assert.rejects( + api.readSftp(null, { sftpId: "sftp-1", path: "/large.log" }), + /10 MB.*download.*external app/i, + ); + assert.equal(readStarted, false); +}); + +test("the total in-memory SFTP read deadline also bounds a stalled stat", async () => { + const api = createApi({}, { + requireSftpChannel: async () => ({}), + statAsync: async () => new Promise(() => {}), + }); + + await assert.rejects( + Promise.race([ + api.readSftp(null, { sftpId: "sftp-1", path: "/stalled.txt", timeoutMs: 5 }), + new Promise((_, reject) => setTimeout(() => reject(new Error("test watchdog")), 100)), + ]), + /SFTP in-memory read timed out/i, + ); +}); + +test("a timed-out in-memory SFTP stream is destroyed and releases every listener", async () => { + const stream = new EventEmitter(); + let destroyed = 0; + stream.destroy = () => { destroyed += 1; }; + stream.close = () => { destroyed += 1; }; + const api = createApi({}, { + requireSftpChannel: async () => ({ createReadStream: () => stream }), + statAsync: async () => ({ size: 1 }), + }); + + await assert.rejects( + Promise.race([ + api.readSftpBinary(null, { sftpId: "sftp-1", path: "/stalled.bin", timeoutMs: 5 }), + new Promise((_, reject) => setTimeout(() => reject(new Error("test watchdog")), 100)), + ]), + /SFTP in-memory read timed out/i, + ); + assert.ok(destroyed > 0); + assert.equal(stream.listenerCount("data"), 0); + assert.equal(stream.listenerCount("end"), 0); + assert.equal(stream.listenerCount("error"), 0); + assert.equal(stream.listenerCount("close"), 0); +}); + +test("in-memory SFTP stream error and premature close both release listeners", async () => { + for (const terminalEvent of ["error", "close"]) { + const stream = new EventEmitter(); + stream.destroy = () => {}; + stream.close = () => {}; + const api = createApi({}, { + requireSftpChannel: async () => ({ createReadStream: () => stream }), + statAsync: async () => ({ size: 1 }), + }); + const reading = api.readSftp(null, { sftpId: "sftp-1", path: "/broken.txt", timeoutMs: 100 }); + process.nextTick(() => { + if (terminalEvent === "error") stream.emit("error", new Error("read failed")); + else stream.emit("close"); + }); + await assert.rejects(reading, /read failed|closed before/i); + assert.equal(stream.listenerCount("data"), 0); + assert.equal(stream.listenerCount("end"), 0); + assert.equal(stream.listenerCount("error"), 0); + assert.equal(stream.listenerCount("close"), 0); + } +}); diff --git a/electron/bridges/sftpBridge.remoteExec.test.cjs b/electron/bridges/sftpBridge.remoteExec.test.cjs new file mode 100644 index 000000000..e25420042 --- /dev/null +++ b/electron/bridges/sftpBridge.remoteExec.test.cjs @@ -0,0 +1,140 @@ +"use strict"; + +const assert = require("node:assert/strict"); +const { EventEmitter } = require("node:events"); +const test = require("node:test"); + +const { _execRemoteShellCommandForTests: execRemoteShellCommand } = require("./sftpBridge.cjs"); + +function createRemoteStream() { + const stream = new EventEmitter(); + stream.stderr = new EventEmitter(); + stream.closeCalls = 0; + stream.destroyCalls = 0; + stream.close = () => { stream.closeCalls += 1; }; + stream.destroy = () => { stream.destroyCalls += 1; }; + return stream; +} + +test("remote delete exec times out while opening and closes a stream that arrives late", async () => { + let callback; + let invalidations = 0; + const sshClient = { + exec(_command, next) { callback = next; }, + destroy() { invalidations += 1; }, + }; + + await assert.rejects( + () => execRemoteShellCommand(sshClient, "rm -rf -- /tmp/example", { + openingTimeoutMs: 10, + runTimeoutMs: 100, + maxOutputBytes: 64, + }), + /open timed out/i, + ); + assert.equal(invalidations, 1); + + const lateStream = createRemoteStream(); + callback(null, lateStream); + assert.ok(lateStream.closeCalls + lateStream.destroyCalls > 0); +}); + +test("remote delete exec has a running deadline and destroys the live stream", async () => { + const stream = createRemoteStream(); + const sshClient = { + exec(_command, next) { next(null, stream); }, + }; + + await assert.rejects( + () => execRemoteShellCommand(sshClient, "rm -rf -- /tmp/example", { + openingTimeoutMs: 100, + runTimeoutMs: 10, + maxOutputBytes: 64, + }), + /execution timed out/i, + ); + assert.ok(stream.closeCalls + stream.destroyCalls > 0); +}); + +test("remote delete exec cancellation closes a stream that arrives after cancellation", async () => { + let callback; + let invalidations = 0; + const controller = new AbortController(); + const sshClient = { + exec(_command, next) { callback = next; }, + destroy() { invalidations += 1; }, + }; + const pending = execRemoteShellCommand(sshClient, "rm -rf -- /tmp/example", { + signal: controller.signal, + openingTimeoutMs: 100, + runTimeoutMs: 100, + maxOutputBytes: 64, + }); + + controller.abort(new Error("cancelled by test")); + await assert.rejects(() => pending, /cancelled by test/i); + assert.equal(invalidations, 1); + + const lateStream = createRemoteStream(); + callback(null, lateStream); + assert.ok(lateStream.closeCalls + lateStream.destroyCalls > 0); +}); + +test("remote delete exec rejects and closes the stream when output exceeds its hard limit", async () => { + const stream = createRemoteStream(); + const sshClient = { + exec(_command, next) { + next(null, stream); + queueMicrotask(() => stream.emit("data", Buffer.alloc(65, "x"))); + }, + }; + + await assert.rejects( + () => execRemoteShellCommand(sshClient, "rm -rf -- /tmp/example", { + openingTimeoutMs: 100, + runTimeoutMs: 100, + maxOutputBytes: 64, + }), + /output exceeded/i, + ); + assert.ok(stream.closeCalls + stream.destroyCalls > 0); +}); + +test("remote delete exec preserves split UTF-8 independently on stdout and stderr", async () => { + const stream = createRemoteStream(); + const sshClient = { + exec(_command, next) { next(null, stream); }, + }; + const running = execRemoteShellCommand(sshClient, "printf unicode", { + openingTimeoutMs: 100, + runTimeoutMs: 100, + maxOutputBytes: 64, + }); + const stdout = Buffer.from("中文", "utf8"); + const stderr = Buffer.from("错误", "utf8"); + stream.emit("data", stdout.subarray(0, 2)); + stream.stderr.emit("data", stderr.subarray(0, 1)); + stream.emit("data", stdout.subarray(2)); + stream.stderr.emit("data", stderr.subarray(1)); + stream.emit("close", 0); + + assert.deepEqual(await running, { stdout: "中文", stderr: "错误", code: 0 }); +}); + +test("remote delete exec rejects before exposing a character cut by its byte limit", async () => { + const stream = createRemoteStream(); + const sshClient = { + exec(_command, next) { next(null, stream); }, + }; + const running = execRemoteShellCommand(sshClient, "printf unicode", { + openingTimeoutMs: 100, + runTimeoutMs: 100, + maxOutputBytes: 2, + }); + stream.emit("data", Buffer.from("中", "utf8")); + + await assert.rejects(running, (error) => ( + /output exceeded/i.test(error?.message || "") && !/�/u.test(error?.message || "") + )); + assert.ok(stream.closeCalls + stream.destroyCalls > 0); +}); diff --git a/electron/bridges/sftpBridge.reuseOnly.test.cjs b/electron/bridges/sftpBridge.reuseOnly.test.cjs deleted file mode 100644 index eec32d684..000000000 --- a/electron/bridges/sftpBridge.reuseOnly.test.cjs +++ /dev/null @@ -1,113 +0,0 @@ -const test = require("node:test"); -const assert = require("node:assert/strict"); -const Module = require("node:module"); - -function loadOpenConnectionWithSftpClient(SftpClient) { - const openConnectionPath = require.resolve("./sftpBridge/openConnection.cjs"); - delete require.cache[openConnectionPath]; - - const originalLoad = Module._load; - Module._load = function patchedLoad(request, parent, isMain) { - if (request === "ssh2-sftp-client") { - return SftpClient; - } - return originalLoad.call(this, request, parent, isMain); - }; - - try { - delete require.cache[openConnectionPath]; - return require("./sftpBridge/openConnection.cjs"); - } finally { - Module._load = originalLoad; - } -} - -function createReuseOnlyApi({ findReusableSession, createSessionBackedSftpClient, SftpClient }) { - const { createOpenConnectionApi } = loadOpenConnectionWithSftpClient(SftpClient); - return createOpenConnectionApi({ - sessions: new Map(), - findReusableSession, - acquireConnectionRef: () => {}, - createSessionBackedSftpClient, - requireSftpChannel: async () => {}, - sendSftpProgress: () => {}, - sftpClients: new Map(), - randomUUID: () => "conn-1", - findAllDefaultPrivateKeysFromHelper: async () => [], - getAvailableAgentSocket: async () => null, - hasUsableProxy: () => false, - }); -} - -test("openSftp with reuseOnly throws instead of dialing fresh when source is missing", async () => { - let dialedFresh = false; - class TrackingSftpClient { - constructor() { - dialedFresh = true; - } - } - - const api = createReuseOnlyApi({ - findReusableSession: () => null, - createSessionBackedSftpClient: () => { - throw new Error("should not create reused client"); - }, - SftpClient: TrackingSftpClient, - }); - - await assert.rejects( - () => api.openSftp( - { sender: { id: 1, isDestroyed: () => false, send: () => {} } }, - { - sessionId: "sftp-1", - hostname: "example.test", - username: "alice", - port: 22, - sourceSessionId: "missing", - reuseOnly: true, - }, - ), - /not reusable/, - ); - assert.equal(dialedFresh, false); -}); - -test("openSftp with reuseOnly does not require renderer endpoint to match", async () => { - let requestedTarget; - const source = { - conn: { _sock: { destroyed: false } }, - connRef: { id: "ref-1" }, - }; - const reusedClient = { - end: async () => {}, - }; - - const api = createReuseOnlyApi({ - findReusableSession: (_sessions, sourceSessionId, target) => { - assert.equal(sourceSessionId, "live-session"); - requestedTarget = target; - return source; - }, - createSessionBackedSftpClient: () => reusedClient, - SftpClient: class { - constructor() { - throw new Error("should not dial fresh"); - } - }, - }); - - const result = await api.openSftp( - { sender: { id: 1, isDestroyed: () => false, send: () => {} } }, - { - sessionId: "sftp-1", - hostname: "stale.example.test", - username: "stale-user", - port: 2222, - sourceSessionId: "live-session", - reuseOnly: true, - }, - ); - - assert.equal(requestedTarget, undefined); - assert.deepEqual(result, { sftpId: "sftp-1", fileProtocol: "sftp" }); -}); diff --git a/electron/bridges/sftpBridge.sessionBackedUpload.test.cjs b/electron/bridges/sftpBridge.sessionBackedUpload.test.cjs index 36d80a160..8251a7009 100644 --- a/electron/bridges/sftpBridge.sessionBackedUpload.test.cjs +++ b/electron/bridges/sftpBridge.sessionBackedUpload.test.cjs @@ -3,7 +3,7 @@ /** * Session-backed SFTP clients (openForSession / terminal reuse) are not * ssh2-sftp-client instances. They must still expose pipelined fastPut so - * uploadLocal / writeSftpBinaryWithProgress stay on the high-throughput path + * uploadLocal stays on the high-throughput path * (#2449 fail-closed alignment; no serial WriteStream crawl). */ @@ -15,7 +15,12 @@ const os = require("node:os"); const path = require("node:path"); const sftpBridge = require("./sftpBridge.cjs"); +const transferBridge = require("./transferBridge.cjs"); const tempDirBridge = require("./tempDirBridge.cjs"); +const { + buildConnectionReuseEndpoint, + normalizeEndpoint, +} = require("./sshConnectionPool.cjs"); const { TRANSFER_CHUNK_SIZE, UPLOAD_TRANSFER_CONCURRENCY, @@ -156,6 +161,59 @@ function createSessionChannel(options = {}) { return { channel, fastPutCalls, remoteFiles, remoteMeta, chmodCalls }; } +test("openSftpForSession rejects a same-target session reached through a different jump route", async () => { + let openedChannel = false; + const connection = { + sftp(callback) { + openedChannel = true; + callback(null, createSessionChannel().channel); + }, + }; + const base = { + hostId: "same-host", + hostname: "target.example", + port: 22, + username: "alice", + authMethod: "password", + password: "target-password", + verifyHostKeys: true, + }; + const actualEndpoint = normalizeEndpoint(buildConnectionReuseEndpoint({ + ...base, + jumpHosts: [{ + hostId: "jump-a", + hostname: "jump-a.example", + username: "jump-user", + password: "jump-a-password", + }], + })); + sftpBridge.init({ + electronModule: { webContents: { fromId: () => null } }, + sessions: new Map([["split-route-session", { + conn: connection, + _reuseEndpoint: actualEndpoint, + }]]), + sftpClients: new Map(), + }); + + await assert.rejects( + sftpBridge.openSftpForSession(null, { + sessionId: "split-route-session", + expectedEndpoint: { + ...base, + jumpHosts: [{ + hostId: "jump-b", + hostname: "jump-b.example", + username: "jump-user", + password: "jump-b-password", + }], + }, + }), + (err) => err?.code === "ERR_SFTP_SOURCE_ROUTE_MISMATCH", + ); + assert.equal(openedChannel, false, "the mismatched transport must not be borrowed even briefly"); +}); + test("downloadSftpToLocal aborts while initial SFTP metadata is stalled", async (t) => { const tempRoot = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-sftp-stat-abort-")); t.after(async () => fs.promises.rm(tempRoot, { recursive: true, force: true })); @@ -285,437 +343,6 @@ test("session-backed uploadLocalToSftp uses pipelined fastPut on the raw SFTP ch assert.deepEqual(remoteFiles.get("/home/alice/payload.bin"), payload); }); -test("session-backed writeSftpBinaryWithProgress uses pipelined fastPut", async (t) => { - const tempRoot = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-session-write-progress-")); - t.after(async () => { - await fs.promises.rm(tempRoot, { recursive: true, force: true }); - }); - tempDirBridge.init?.({ getPath: () => tempRoot }); - // ensureTempDir may be required by getTempFilePath - if (typeof tempDirBridge.ensureTempDir === "function") { - tempDirBridge.ensureTempDir(); - } - - const payload = Buffer.alloc(40 * 1024, 29); - const { channel, fastPutCalls, remoteFiles } = createSessionChannel(); - const connection = { - sftp(callback) { - callback(null, channel); - }, - }; - const sftpClients = new Map(); - sftpBridge.init({ - electronModule: { - webContents: { - fromId: () => ({ send() {} }), - }, - }, - sessions: new Map([["session-write", { conn: connection }]]), - sftpClients, - }); - - const opened = await sftpBridge.openSftpForSession(null, { - sessionId: "session-write", - fileProtocol: "sftp", - }); - - let progressError = null; - const result = await sftpBridge.writeSftpBinaryWithProgress( - { sender: { id: 1 } }, - { - sftpId: opened.sftpId, - path: "/home/alice/mem.bin", - content: payload, - transferId: "mem-upload-1", - encoding: "utf-8", - onProgress() {}, - onComplete() {}, - onError(message) { - progressError = message; - }, - }, - ); - - assert.equal(progressError, null, progressError); - assert.equal(result.success, true, result.error || progressError || "upload failed"); - assert.equal(fastPutCalls.length, 1); - assert.equal(fastPutCalls[0].concurrency, UPLOAD_TRANSFER_CONCURRENCY); - assert.equal(fastPutCalls[0].chunkSize, TRANSFER_CHUNK_SIZE); - assert.match(path.basename(fastPutCalls[0].localPath), /sftp-upload-/); - assert.doesNotMatch(fastPutCalls[0].localPath, /upload-source-/); - await assert.rejects(fs.promises.stat(fastPutCalls[0].localPath), { code: "ENOENT" }); - // New destinations stage to a remote .part path, then rename into place. - assert.match(fastPutCalls[0].remotePath, /\.netcatty-upload-.*\.part$/); - assert.notEqual(fastPutCalls[0].remotePath, "/home/alice/mem.bin"); - assert.ok(remoteFiles.has("/home/alice/mem.bin")); - assert.deepEqual(remoteFiles.get("/home/alice/mem.bin"), payload); -}); - -test("SCP writeSftpBinaryWithProgress uses the shared staged transaction", async (t) => { - const tempRoot = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-scp-write-progress-")); - t.after(async () => fs.promises.rm(tempRoot, { recursive: true, force: true })); - tempDirBridge.init?.({ getPath: () => tempRoot }); - - const payload = Buffer.from("new executable payload"); - const finalPath = "/目录/工具"; - const remoteFiles = new Map([[finalPath, Buffer.from("old")]]); - const remoteModes = new Map([[finalPath, 0o755]]); - const uploadPaths = []; - const chmodCalls = []; - const backend = { - async stat(remotePath) { - if (!remoteFiles.has(remotePath)) { - const error = new Error("No such file"); - error.code = "ENOENT"; - throw error; - } - return { - type: "file", - isDirectory: false, - size: remoteFiles.get(remotePath).length, - mode: remoteModes.get(remotePath) ?? 0o644, - permissions: (remoteModes.get(remotePath) ?? 0o644) === 0o755 - ? "rwxr-xr-x" - : "rw-r--r--", - }; - }, - async uploadFile(localPath, remotePath, options = {}) { - uploadPaths.push(remotePath); - const contents = await fs.promises.readFile(localPath); - remoteFiles.set(remotePath, contents); - remoteModes.set(remotePath, 0o644); - options.onProgress?.(contents.length, contents.length); - }, - async chmod(remotePath, mode) { - chmodCalls.push({ remotePath, mode }); - remoteModes.set(remotePath, mode); - }, - async rename(fromPath, toPath) { - remoteFiles.set(toPath, remoteFiles.get(fromPath)); - remoteFiles.delete(fromPath); - remoteModes.set(toPath, remoteModes.get(fromPath)); - remoteModes.delete(fromPath); - }, - async remove(remotePath) { - remoteFiles.delete(remotePath); - remoteModes.delete(remotePath); - }, - }; - const sftpClients = new Map([["scp-memory", { - __netcattyFileProtocol: "scp", - __netcattyScpBackend: backend, - }]]); - sftpBridge.init({ - electronModule: { webContents: { fromId: () => ({ send() {} }) } }, - sessions: new Map(), - sftpClients, - }); - - const result = await sftpBridge.writeSftpBinaryWithProgress( - { sender: { id: 1 } }, - { - sftpId: "scp-memory", - path: finalPath, - content: payload, - transferId: "scp-memory-upload", - onProgress() {}, - onComplete() {}, - }, - ); - - assert.equal(result.success, true); - assert.equal(uploadPaths.length, 1); - assert.match(uploadPaths[0], /\.netcatty-upload-.*\.part$/); - assert.equal(typeof uploadPaths[0], "string"); - assert.match(uploadPaths[0], /^\/目录\/\.netcatty-upload-/); - assert.notEqual(uploadPaths[0], finalPath); - assert.deepEqual(remoteFiles.get(finalPath), payload); - assert.equal(remoteModes.get(finalPath), 0o755); - assert.deepEqual(chmodCalls, [{ remotePath: uploadPaths[0], mode: 0o755 }]); -}); - -test("SCP buffer upload creates a new remote file with mode 0644 under restrictive umask", async (t) => { - const tempRoot = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-scp-buffer-mode-")); - t.after(async () => fs.promises.rm(tempRoot, { recursive: true, force: true })); - tempDirBridge.init?.({ getPath: () => tempRoot }); - - const payload = Buffer.from("ordinary buffer payload"); - const finalPath = "/tmp/new-buffer.bin"; - const remoteFiles = new Map(); - const remoteModes = new Map(); - let uploadedSourcePath = null; - const backend = { - async stat(remotePath) { - if (!remoteFiles.has(remotePath)) { - const error = new Error("No such file"); - error.code = "ENOENT"; - throw error; - } - return { - type: "file", - isDirectory: false, - size: remoteFiles.get(remotePath).length, - mode: remoteModes.get(remotePath), - }; - }, - async uploadFile(localPath, remotePath, options = {}) { - uploadedSourcePath = localPath; - const contents = await fs.promises.readFile(localPath); - remoteFiles.set(remotePath, contents); - remoteModes.set(remotePath, (await fs.promises.stat(localPath)).mode & 0o777); - options.onProgress?.(contents.length, contents.length); - }, - async rename(fromPath, toPath) { - remoteFiles.set(toPath, remoteFiles.get(fromPath)); - remoteFiles.delete(fromPath); - remoteModes.set(toPath, remoteModes.get(fromPath)); - remoteModes.delete(fromPath); - }, - async remove(remotePath) { - remoteFiles.delete(remotePath); - remoteModes.delete(remotePath); - }, - async chmod(remotePath, mode) { - remoteModes.set(remotePath, mode); - }, - }; - const sftpClients = new Map([["scp-buffer-mode", { - __netcattyFileProtocol: "scp", - __netcattyScpBackend: backend, - }]]); - sftpBridge.init({ - electronModule: { webContents: { fromId: () => ({ send() {} }) } }, - sessions: new Map(), - sftpClients, - }); - - const previousUmask = process.umask(0o077); - let result; - try { - result = await sftpBridge.writeSftpBinaryWithProgress( - { sender: { id: 1 } }, - { - sftpId: "scp-buffer-mode", - path: finalPath, - content: payload, - transferId: "scp-buffer-mode-upload", - onProgress() {}, - onComplete() {}, - }, - ); - } finally { - process.umask(previousUmask); - } - - assert.equal(result.success, true); - assert.match(path.basename(uploadedSourcePath), /sftp-upload-/); - assert.doesNotMatch(uploadedSourcePath, /upload-source-/); - assert.deepEqual(remoteFiles.get(finalPath), payload); - assert.equal(remoteModes.get(finalPath), 0o644); - await assert.rejects(fs.promises.stat(uploadedSourcePath), { code: "ENOENT" }); -}); - -test("SCP staged uploads preserve an existing mode 000", async (t) => { - const tempRoot = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-scp-mode-zero-")); - t.after(async () => fs.promises.rm(tempRoot, { recursive: true, force: true })); - tempDirBridge.init?.({ getPath: () => tempRoot }); - - const payload = Buffer.from("replacement over mode-zero file"); - const finalPath = "/tmp/locked.bin"; - const remoteFiles = new Map([[finalPath, Buffer.from("old")]]); - const remoteModes = new Map([[finalPath, 0]]); - const uploadPaths = []; - const chmodCalls = []; - const backend = { - async stat(remotePath) { - if (!remoteFiles.has(remotePath)) { - const error = new Error("No such file"); - error.code = "ENOENT"; - throw error; - } - return { - type: "file", - isDirectory: false, - size: remoteFiles.get(remotePath).length, - mode: remoteModes.get(remotePath) ?? 0o644, - permissions: remoteModes.get(remotePath) === 0 ? "---------" : "rw-r--r--", - }; - }, - async uploadFile(localPath, remotePath, options = {}) { - uploadPaths.push(remotePath); - const contents = await fs.promises.readFile(localPath); - remoteFiles.set(remotePath, contents); - remoteModes.set(remotePath, 0o644); - options.onProgress?.(contents.length, contents.length); - }, - async chmod(remotePath, mode) { - chmodCalls.push({ remotePath, mode }); - remoteModes.set(remotePath, mode); - }, - async rename(fromPath, toPath) { - remoteFiles.set(toPath, remoteFiles.get(fromPath)); - remoteFiles.delete(fromPath); - remoteModes.set(toPath, remoteModes.get(fromPath)); - remoteModes.delete(fromPath); - }, - async remove(remotePath) { - remoteFiles.delete(remotePath); - remoteModes.delete(remotePath); - }, - }; - const sftpClients = new Map([["scp-mode-zero", { - __netcattyFileProtocol: "scp", - __netcattyScpBackend: backend, - }]]); - sftpBridge.init({ - electronModule: { webContents: { fromId: () => ({ send() {} }) } }, - sessions: new Map(), - sftpClients, - }); - - const result = await sftpBridge.writeSftpBinaryWithProgress( - { sender: { id: 1 } }, - { - sftpId: "scp-mode-zero", - path: finalPath, - content: payload, - transferId: "scp-mode-zero-upload", - onProgress() {}, - onComplete() {}, - }, - ); - - assert.equal(result.success, true, result.error || "upload failed"); - assert.equal(uploadPaths.length, 1); - assert.match(uploadPaths[0], /\.netcatty-upload-.*\.part$/); - assert.deepEqual(remoteFiles.get(finalPath), payload); - assert.deepEqual(chmodCalls, [{ remotePath: uploadPaths[0], mode: 0 }]); - assert.equal(remoteModes.get(finalPath), 0); -}); - -test("SCP staged uploads do not treat an unparseable zero mode as mode 000", async () => { - let chmodCalls = 0; - const backend = { - async stat(remotePath) { - return { - type: "file", - isDirectory: false, - size: String(remotePath).includes(".netcatty-upload-") ? 7 : 3, - mode: 0, - // No permissions field: the shell mode could not be parsed. - }; - }, - async chmod() { - chmodCalls += 1; - }, - async rename() {}, - async remove() {}, - }; - const client = { - __netcattyFileProtocol: "scp", - __netcattyScpBackend: backend, - }; - - const result = await sftpBridge.runRemoteUploadTransaction( - client, - "/tmp/local.bin", - "/tmp/final.bin", - { - expectedSize: 7, - async uploadFile() {}, - }, - ); - assert.deepEqual(result, { staged: true }); - assert.equal(chmodCalls, 0); -}); - -test("SCP symlink uploads do not compare content size with the link node", async () => { - let uploadCalls = 0; - const backend = { - async stat() { - return { type: "symlink", isSymbolicLink: true, size: 99 }; - }, - async uploadFile() { - uploadCalls += 1; - }, - }; - const client = { - __netcattyFileProtocol: "scp", - __netcattyScpBackend: backend, - }; - - const result = await sftpBridge.runRemoteUploadTransaction( - client, - "/tmp/local.bin", - "/目录/链接.bin", - { - expectedSize: 7, - async uploadFile(remotePath) { - assert.equal(remotePath, "/目录/链接.bin"); - uploadCalls += 1; - }, - }, - ); - assert.deepEqual(result, { staged: false }); - assert.equal(uploadCalls, 1); -}); - -test("cancelling an SCP memory upload leaves the final destination untouched", async (t) => { - const tempRoot = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-scp-memory-cancel-")); - t.after(async () => fs.promises.rm(tempRoot, { recursive: true, force: true })); - tempDirBridge.init?.({ getPath: () => tempRoot }); - - let uploadStarted; - const started = new Promise((resolve) => { uploadStarted = resolve; }); - let renameCalls = 0; - let removedStage = false; - const backend = { - async stat() { - const error = new Error("No such file"); - error.code = "ENOENT"; - throw error; - }, - async uploadFile(_localPath, _remotePath, options = {}) { - uploadStarted(); - await new Promise((resolve, reject) => { - options.transfer.abort = () => reject(new Error("Transfer cancelled")); - }); - }, - async rename() { - renameCalls += 1; - }, - async remove(remotePath) { - if (String(remotePath).includes(".netcatty-upload-")) removedStage = true; - }, - }; - sftpBridge.init({ - electronModule: { webContents: { fromId: () => ({ send() {} }) } }, - sessions: new Map(), - sftpClients: new Map([["scp-memory-cancel", { - __netcattyFileProtocol: "scp", - __netcattyScpBackend: backend, - }]]), - }); - - const upload = sftpBridge.writeSftpBinaryWithProgress( - { sender: { id: 1 } }, - { - sftpId: "scp-memory-cancel", - path: "/tmp/final.bin", - content: Buffer.alloc(1024, 1), - transferId: "scp-memory-cancel-transfer", - onProgress() {}, - }, - ); - await started; - await sftpBridge.cancelSftpUpload(null, { transferId: "scp-memory-cancel-transfer" }); - const result = await upload; - - assert.equal(result.cancelled, true); - assert.equal(renameCalls, 0); - assert.equal(removedStage, true); -}); - test("existing destinations stage then restore mode after rename", async (t) => { const tempRoot = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-stage-meta-")); t.after(async () => { @@ -978,6 +605,173 @@ test("rename fallback replaces safely and restores the old target on promotion f ); }); +test("SFTP rename fallback retries backup cleanup and reports a recoverable persistent failure", async () => { + const makeClient = (persistent) => { + const files = new Map([ + ["/tmp/stage", Buffer.from("new")], + ["/tmp/final", Buffer.from("old")], + ]); + let promoteAttempts = 0; + let deleteAttempts = 0; + return { + files, + get deleteAttempts() { return deleteAttempts; }, + client: { + sftp: { + readdir(_path, cb) { cb(null, []); }, + mkdir(_path, cb) { cb(null); }, + unlink(_path, cb) { cb(null); }, + stat(targetPath, cb) { + if (!files.has(targetPath)) { + const error = new Error("ENOENT"); + error.code = 2; + cb(error); + return; + } + cb(null, { size: files.get(targetPath).length, isDirectory: false }); + }, + }, + async stat(targetPath) { + return { size: files.get(targetPath)?.length || 0, isDirectory: false }; + }, + async rename(from, to) { + if (from === "/tmp/stage" && to === "/tmp/final" && promoteAttempts++ === 0) { + throw new Error("overwrite unsupported"); + } + if (!files.has(from)) throw new Error("ENOENT"); + files.set(to, files.get(from)); + files.delete(from); + }, + async delete(targetPath) { + deleteAttempts += 1; + if (persistent || deleteAttempts < 3) throw new Error("backup busy"); + files.delete(targetPath); + }, + }, + }; + }; + + const transient = makeClient(false); + await sftpBridge._renameRemotePathForTests( + transient.client, + "/tmp/stage", + "/tmp/final", + "/tmp/backup", + { finalPath: "/tmp/final", backupPath: "/tmp/backup" }, + ); + assert.equal(transient.deleteAttempts, 3); + assert.deepEqual(transient.files.get("/tmp/final"), Buffer.from("new")); + assert.equal(transient.files.has("/tmp/backup"), false); + + const persistent = makeClient(true); + await assert.rejects( + () => sftpBridge._renameRemotePathForTests( + persistent.client, + "/tmp/stage", + "/tmp/final", + "/tmp/backup", + { finalPath: "/tmp/final", backupPath: "/tmp/backup" }, + ), + (error) => { + assert.equal(error.recoverable, true); + assert.equal(error.remoteFinalPath, "/tmp/final"); + assert.equal(error.remoteBackupPath, "/tmp/backup"); + return true; + }, + ); + assert.equal(persistent.deleteAttempts, 3); + assert.deepEqual(persistent.files.get("/tmp/final"), Buffer.from("new")); + assert.deepEqual(persistent.files.get("/tmp/backup"), Buffer.from("old")); +}); + +function createScpBackupCleanupHarness({ persistent }) { + const finalPath = "/tmp/scp-final.bin"; + const backupPath = sftpBridge._buildBackupRemotePathForTests(finalPath); + const files = new Map([[finalPath, Buffer.from("old")]]); + let backupDeleteAttempts = 0; + let uploadCalls = 0; + const missing = (candidate) => { + const error = new Error(`ENOENT: ${candidate}`); + error.code = "ENOENT"; + return error; + }; + const backend = { + async stat(candidate) { + if (!files.has(candidate)) throw missing(candidate); + return { + type: "file", + isDirectory: false, + isSymbolicLink: false, + size: files.get(candidate).length, + mode: 0o100644, + permissions: "rw-r--r--", + modifyTime: 1, + }; + }, + async chmod() {}, + async rename(from, to) { + if (!files.has(from)) throw missing(from); + if (files.has(to)) throw new Error(`EEXIST: ${to}`); + files.set(to, files.get(from)); + files.delete(from); + }, + async remove(candidate) { + if (candidate === backupPath) { + backupDeleteAttempts += 1; + if (persistent || backupDeleteAttempts < 3) throw new Error("backup cleanup unavailable"); + } + files.delete(candidate); + }, + }; + const client = { + __netcattyFileProtocol: "scp", + __netcattyScpBackend: backend, + }; + const upload = () => sftpBridge.runRemoteUploadTransaction(client, "/tmp/local.bin", finalPath, { + expectedSize: 3, + async uploadFile(_encodedPath, uploadTarget) { + uploadCalls += 1; + files.set(uploadTarget.logicalPath, Buffer.from("new")); + }, + }); + return { + finalPath, + backupPath, + files, + upload, + get backupDeleteAttempts() { return backupDeleteAttempts; }, + get uploadCalls() { return uploadCalls; }, + }; +} + +test("SCP upload retries transient backup cleanup", async () => { + const harness = createScpBackupCleanupHarness({ persistent: false }); + await harness.upload(); + assert.equal(harness.backupDeleteAttempts, 3); + assert.deepEqual(harness.files.get(harness.finalPath), Buffer.from("new")); + assert.equal(harness.files.has(harness.backupPath), false); +}); + +test("SCP persistent cleanup failure remains recoverable and repeated upload creates no new backup", async () => { + const harness = createScpBackupCleanupHarness({ persistent: true }); + await assert.rejects(harness.upload, (error) => { + assert.equal(error.recoverable, true); + assert.equal(error.remoteFinalPath, harness.finalPath); + assert.equal(error.remoteBackupPath, harness.backupPath); + return true; + }); + assert.deepEqual(harness.files.get(harness.finalPath), Buffer.from("new")); + assert.deepEqual(harness.files.get(harness.backupPath), Buffer.from("old")); + assert.equal(harness.uploadCalls, 1); + + await assert.rejects(harness.upload, /backup cleanup unavailable/); + assert.equal(harness.uploadCalls, 1, "the next attempt must reconcile the stable backup before uploading"); + assert.deepEqual( + [...harness.files.keys()].filter((candidate) => candidate.includes(".netcatty-backup-")), + [harness.backupPath], + ); +}); + test("SCP upload stops when the destination type cannot be inspected", async (t) => { const tempRoot = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-scp-stat-fail-")); t.after(async () => fs.promises.rm(tempRoot, { recursive: true, force: true })); @@ -1019,13 +813,22 @@ test("SCP upload does not replace a symlink that appears before promotion", asyn t.after(async () => fs.promises.rm(tempRoot, { recursive: true, force: true })); const localPath = path.join(tempRoot, "local.bin"); await fs.promises.writeFile(localPath, Buffer.from("payload")); - let statCalls = 0; + let targetStatCalls = 0; let renameCalls = 0; let removedStage = false; const backend = { - async stat() { - statCalls += 1; - if (statCalls === 1) return { type: "file", isDirectory: false }; + async stat(remotePath) { + const candidate = String(remotePath); + if (candidate.includes(".netcatty-backup-")) { + const error = new Error("ENOENT"); + error.code = "ENOENT"; + throw error; + } + if (candidate.includes(".netcatty-upload-")) { + return { type: "file", isDirectory: false, size: 7 }; + } + targetStatCalls += 1; + if (targetStatCalls === 1) return { type: "file", isDirectory: false }; return { type: "symlink", isDirectory: false, isSymbolicLink: true }; }, async uploadFile() {}, @@ -1061,16 +864,22 @@ test("SCP upload does not replace a symlink that appears before promotion", asyn test("SCP upload cancelled during the final target check does not promote", async () => { const controller = new AbortController(); - let statCalls = 0; + let targetStatCalls = 0; let renameCalls = 0; let removedStage = false; const backend = { async stat(remotePath) { - statCalls += 1; - if (String(remotePath).includes(".netcatty-upload-")) { + const candidate = String(remotePath); + if (candidate.includes(".netcatty-backup-")) { + const error = new Error("ENOENT"); + error.code = "ENOENT"; + throw error; + } + if (candidate.includes(".netcatty-upload-")) { return { type: "file", isDirectory: false, size: 7 }; } - if (statCalls > 2) controller.abort(); + targetStatCalls += 1; + if (targetStatCalls > 1) controller.abort(); return { type: "file", isDirectory: false, size: 3 }; }, async rename() { @@ -1451,7 +1260,7 @@ test("permission failure during final target recheck never falls back to direct const originalLstat = created.channel.lstat.bind(created.channel); let lstatCalls = 0; created.channel.lstat = (targetPath, callback) => { - lstatCalls += 1; + if (String(targetPath) === "/tmp/data.bin") lstatCalls += 1; if (lstatCalls >= 2 && String(targetPath) === "/tmp/data.bin") { const err = new Error("Permission denied"); err.code = "EACCES"; diff --git a/electron/bridges/sftpBridge/fileOps.cjs b/electron/bridges/sftpBridge/fileOps.cjs index 041ab0e1a..e675a00e3 100644 --- a/electron/bridges/sftpBridge/fileOps.cjs +++ b/electron/bridges/sftpBridge/fileOps.cjs @@ -1,4 +1,123 @@ /* eslint-disable no-undef */ +const { executeBoundedSshCommand } = require("../boundedSshExec.cjs"); +const MAX_IN_MEMORY_SFTP_READ_BYTES = 10 * 1024 * 1024; +const DEFAULT_IN_MEMORY_SFTP_READ_TIMEOUT_MS = 30_000; + +function createSftpReadLimitError(size) { + const error = new Error( + `Remote file is too large to read into memory (${size} bytes; maximum 10 MB). ` + + "Use SFTP download for large files, or open it with an external app.", + ); + error.code = "SFTP_READ_TOO_LARGE"; + error.maxBytes = MAX_IN_MEMORY_SFTP_READ_BYTES; + error.actualBytes = size; + return error; +} + +function assertSftpReadSize(size) { + const value = Number(size); + if (Number.isFinite(value) && value > MAX_IN_MEMORY_SFTP_READ_BYTES) { + throw createSftpReadLimitError(value); + } +} + +function createSftpReadTimeoutError(timeoutMs) { + const error = new Error(`SFTP in-memory read timed out after ${timeoutMs} ms`); + error.code = "SFTP_READ_TIMEOUT"; + return error; +} + +async function runBoundedSftpMemoryRead(payload, operation) { + const requestedTimeout = Number(payload?.timeoutMs); + const timeoutMs = Number.isFinite(requestedTimeout) && requestedTimeout > 0 + ? requestedTimeout + : DEFAULT_IN_MEMORY_SFTP_READ_TIMEOUT_MS; + const controller = new AbortController(); + const parentSignal = payload?.abortSignal || null; + const abortFromParent = () => { + const reason = parentSignal?.reason instanceof Error + ? parentSignal.reason + : new Error("SFTP read cancelled"); + controller.abort(reason); + }; + if (parentSignal?.aborted) abortFromParent(); + else parentSignal?.addEventListener?.("abort", abortFromParent, { once: true }); + + let rejectOnAbort; + const aborted = new Promise((_, reject) => { rejectOnAbort = reject; }); + const onAbort = () => rejectOnAbort(controller.signal.reason || new Error("SFTP read cancelled")); + controller.signal.addEventListener("abort", onAbort, { once: true }); + const timer = setTimeout(() => controller.abort(createSftpReadTimeoutError(timeoutMs)), timeoutMs); + timer.unref?.(); + try { + return await Promise.race([operation(controller.signal), aborted]); + } finally { + clearTimeout(timer); + controller.signal.removeEventListener("abort", onAbort); + parentSignal?.removeEventListener?.("abort", abortFromParent); + } +} + +function readSftpStreamIntoBuffer(stream, signal = null) { + if (!stream || typeof stream.on !== "function") { + throw new Error("SFTP streaming read is unavailable. Use SFTP download instead."); + } + return new Promise((resolve, reject) => { + const chunks = []; + let totalBytes = 0; + let settled = false; + const cleanup = () => { + stream.removeListener("data", onData); + stream.removeListener("end", onEnd); + stream.removeListener("error", onError); + stream.removeListener("close", onClose); + signal?.removeEventListener?.("abort", onAbort); + }; + const finish = (error, value) => { + if (settled) return; + settled = true; + cleanup(); + if (error) reject(error); + else resolve(value); + }; + const stopStream = () => { + try { stream.destroy?.(); } catch { /* ignore */ } + try { stream.close?.(); } catch { /* ignore */ } + }; + const onData = (chunk) => { + const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk); + const nextTotal = totalBytes + buffer.length; + if (nextTotal > MAX_IN_MEMORY_SFTP_READ_BYTES) { + finish(createSftpReadLimitError(nextTotal)); + stopStream(); + return; + } + chunks.push(buffer); + totalBytes = nextTotal; + }; + const onEnd = () => finish(null, Buffer.concat(chunks, totalBytes)); + const onError = (error) => finish(error); + const onClose = () => { + if (!settled) finish(new Error("SFTP read stream closed before the file was complete")); + }; + const onAbort = () => { + const reason = signal?.reason instanceof Error ? signal.reason : new Error("SFTP read cancelled"); + finish(reason); + stopStream(); + }; + + stream.on("data", onData); + stream.once("end", onEnd); + stream.once("error", onError); + stream.once("close", onClose); + if (signal?.aborted) { + onAbort(); + return; + } + signal?.addEventListener?.("abort", onAbort, { once: true }); + }); +} + function createFileOpsApi(ctx) { with (ctx) { const { @@ -223,52 +342,104 @@ function createFileOpsApi(ctx) { return results; } + + async function realpathSftp(event, payload) { + const client = sftpClients.get(payload.sftpId); + if (!client) throw new Error("SFTP session not found"); + const signal = payload?.abortSignal || null; + const encoding = resolveEncodingForRequest(payload.sftpId, payload.encoding); + if (isScpModeClient(client)) { + return getScpBackendForClient(client).realpath(payload.path || ".", { encoding, signal }); + } + const sftp = await requireSftpChannel(client, { signal, timeoutMs: payload?.timeoutMs }); + const encodedPath = encodePath(payload.path || ".", encoding); + return realpathAsync(sftp, encodedPath); + } /** * Read file content */ async function readSftp(event, payload) { - const client = sftpClients.get(payload.sftpId); - if (!client) throw new Error("SFTP session not found"); + return runBoundedSftpMemoryRead(payload, async (signal) => { + const client = sftpClients.get(payload.sftpId); + if (!client) throw new Error("SFTP session not found"); - if (isScpModeClient(client)) { + if (isScpModeClient(client)) { const encoding = resolveEncodingForRequest(payload.sftpId, payload.encoding); - const buffer = await getScpBackendForClient(client).readFile(payload.path, { + const backend = getScpBackendForClient(client); + try { + const attrs = await backend.stat(payload.path, { + encoding, + signal, + }); + assertSftpReadSize(attrs?.size); + } catch (error) { + if (signal.aborted || error?.code === "SFTP_READ_TOO_LARGE") throw error; + // Some restricted shells cannot stat even though scp -f is allowed. + // The SCP protocol header below still enforces the limit before data. + } + const buffer = await backend.readFile(payload.path, { encoding, - signal: payload?.abortSignal || null, + signal, + maxBytes: MAX_IN_MEMORY_SFTP_READ_BYTES, }); + assertSftpReadSize(buffer?.length); return buffer.toString(); - } + } - await requireSftpChannel(client); + const sftp = await requireSftpChannel(client, { signal, timeoutMs: payload?.timeoutMs }); const encoding = resolveEncodingForRequest(payload.sftpId, payload.encoding); const encodedPath = encodePath(payload.path, encoding); - const buffer = await client.get(encodedPath); + const attrs = await statAsync(sftp, encodedPath); + if (signal.aborted) throw signal.reason; + assertSftpReadSize(attrs?.size); + const stream = sftp.createReadStream?.(encodedPath); + const buffer = await readSftpStreamIntoBuffer(stream, signal); return buffer.toString(); + }); } /** * Read file as binary (returns ArrayBuffer for binary files like images) */ async function readSftpBinary(event, payload) { - const client = sftpClients.get(payload.sftpId); - if (!client) throw new Error("SFTP session not found"); + return runBoundedSftpMemoryRead(payload, async (signal) => { + const client = sftpClients.get(payload.sftpId); + if (!client) throw new Error("SFTP session not found"); - if (isScpModeClient(client)) { + if (isScpModeClient(client)) { const encoding = resolveEncodingForRequest(payload.sftpId, payload.encoding); - const buffer = await getScpBackendForClient(client).readFile(payload.path, { + const backend = getScpBackendForClient(client); + try { + const attrs = await backend.stat(payload.path, { + encoding, + signal, + }); + assertSftpReadSize(attrs?.size); + } catch (error) { + if (signal.aborted || error?.code === "SFTP_READ_TOO_LARGE") throw error; + // See the text-read path: protocol-header enforcement is the fallback. + } + const buffer = await backend.readFile(payload.path, { encoding, - signal: payload?.abortSignal || null, + signal, + maxBytes: MAX_IN_MEMORY_SFTP_READ_BYTES, }); + assertSftpReadSize(buffer?.length); return buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength); - } + } - await requireSftpChannel(client); + const sftp = await requireSftpChannel(client, { signal, timeoutMs: payload?.timeoutMs }); const encoding = resolveEncodingForRequest(payload.sftpId, payload.encoding); const encodedPath = encodePath(payload.path, encoding); - const buffer = await client.get(encodedPath); + const attrs = await statAsync(sftp, encodedPath); + if (signal.aborted) throw signal.reason; + assertSftpReadSize(attrs?.size); + const stream = sftp.createReadStream?.(encodedPath); + const buffer = await readSftpStreamIntoBuffer(stream, signal); // Convert Node.js Buffer to ArrayBuffer return buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength); + }); } /** @@ -368,197 +539,6 @@ function createFileOpsApi(ctx) { return true; } - /** - * Write binary data with progress callback - * Supports cancellation via activeSftpUploads map - * Optimized for performance with throttled progress updates - */ - async function writeSftpBinaryWithProgress(event, payload) { - const client = sftpClients.get(payload.sftpId); - if (!client) throw new Error("SFTP session not found"); - - const { sftpId, path: remotePath, content, transferId } = payload; - - if (!isScpModeClient(client)) { - await requireSftpChannel(client); - } - const encoding = resolveEncodingForRequest(payload.sftpId, payload.encoding); - - // Extract callback functions from payload - const onProgress = payload.onProgress; - const onComplete = payload.onComplete; - const onError = payload.onError; - - // Optimize: Use Buffer.isBuffer to avoid unnecessary copy if already a Buffer - // For ArrayBuffer from renderer, we still need to convert but use a more efficient method - const buffer = Buffer.isBuffer(content) ? content : Buffer.from(content); - const totalBytes = buffer.length; - const emitProgress = (transferred, speed = 0) => { - if (typeof onProgress === "function") { - try { - onProgress(transferred, totalBytes, speed); - } catch (err) { - console.warn("[SFTP] Progress callback error:", err); - } - } else { - const contents = electronModule.webContents.fromId(event.sender.id); - contents?.send("netcatty:upload:progress", { - transferId, - transferred, - totalBytes, - speed, - }); - } - }; - - const emitComplete = () => { - if (typeof onComplete === "function") { - try { - onComplete(); - } catch (err) { - console.warn("[SFTP] Complete callback error:", err); - } - } else { - const contents = electronModule.webContents.fromId(event.sender.id); - contents?.send("netcatty:upload:complete", { transferId }); - } - }; - - const emitError = (message) => { - if (typeof onError === "function") { - try { - onError(message); - } catch (callbackErr) { - console.warn("[SFTP] Error callback error:", callbackErr); - } - } else { - const contents = electronModule.webContents.fromId(event.sender.id); - contents?.send("netcatty:upload:error", { transferId, error: message }); - } - }; - - // Pipelined fastPut via local temp + optional remote .part. Serial put()/WriteStream - // is not used as a silent fallback (#2449). Prefer disposable channel for cancel. - // Stage+rename by default so shared-channel cancel cannot keep writing the final path. - let tempPath = null; - let lastProgressTime = Date.now(); - let lastTransferredBytes = 0; - let lastProgressSentTime = 0; - let lastProgressSentBytes = 0; - const PROGRESS_THROTTLE_MS = 500; - - const transferControl = { - cancelled: false, - abort: null, - }; - const abortController = typeof AbortController === "function" - ? new AbortController() - : null; - transferControl.abort = () => { - transferControl.cancelled = true; - try { abortController?.abort(); } catch { /* ignore */ } - }; - activeSftpUploads.set(transferId, { - cancelled: false, - transfer: transferControl, - }); - - try { - tempPath = await tempDirBridge.getTempFilePath( - `sftp-upload-${transferId || Date.now()}.bin`, - ); - // Stage to disk with abort support (Node writeFile accepts signal). - const writeOpts = abortController?.signal - ? { signal: abortController.signal } - : undefined; - await fs.promises.writeFile(tempPath, buffer, writeOpts); - // Buffer uploads have always created ordinary data files. Do not let a - // restrictive process umask leak scratch-file permissions to SCP. - await fs.promises.chmod(tempPath, 0o644); - if (activeSftpUploads.get(transferId)?.cancelled || transferControl.cancelled) { - throw new Error("Upload cancelled"); - } - - const step = (transferred, _chunk, total) => { - if (activeSftpUploads.get(transferId)?.cancelled || transferControl.cancelled) return; - const now = Date.now(); - const elapsed = (now - lastProgressTime) / 1000; - let speed = 0; - if (elapsed >= 0.1) { - speed = (transferred - lastTransferredBytes) / elapsed; - lastProgressTime = now; - lastTransferredBytes = transferred; - } - const totalSize = Number(total) > 0 ? Number(total) : totalBytes; - const isComplete = transferred >= totalSize; - const timeSinceLast = now - lastProgressSentTime; - if (isComplete || timeSinceLast >= PROGRESS_THROTTLE_MS) { - emitProgress(transferred, speed); - lastProgressSentTime = now; - lastProgressSentBytes = transferred; - } - }; - - await runUnifiedSftpTransfer({ - sftpId, - localPath: tempPath, - remotePath, - transferId, - encoding, - abortSignal: abortController?.signal || null, - resumable: false, - sourceIsOwnedTemp: true, - onTransferEvent(channel, eventPayload) { - if (channel === "netcatty:transfer:progress") { - step(eventPayload.transferred, 0, eventPayload.totalBytes); - } - }, - }, "upload"); - emitComplete(); - return { success: true, transferId }; - } catch (err) { - const uploadState = activeSftpUploads.get(transferId); - if ( - uploadState?.cancelled - || transferControl.cancelled - || err.message === "Upload cancelled" - || /abort/i.test(err.message || "") - ) { - try { - const contents = electronModule.webContents.fromId(event?.sender?.id); - contents?.send("netcatty:upload:cancelled", { transferId }); - } catch { /* ignore */ } - return { success: false, transferId, cancelled: true }; - } - emitError(err.message); - throw err; - } finally { - activeSftpUploads.delete(transferId); - if (tempPath) { - try { await fs.promises.unlink(tempPath); } catch { /* ignore */ } - } - } - } - - /** - * Cancel an in-progress buffer upload (writeBinaryWithProgress). - * Only sets cancelled / aborts the owned signal here; cleanup of - * activeSftpUploads stays in writeSftpBinaryWithProgress's finally block. - * Panel bulk transfers cancel via transferBridge.cancelTransfer instead. - */ - async function cancelSftpUpload(event, payload) { - const { transferId } = payload; - const uploadState = activeSftpUploads.get(transferId); - if (uploadState) { - uploadState.cancelled = true; - if (uploadState.transfer) { - uploadState.transfer.cancelled = true; - try { uploadState.transfer.abort?.(); } catch { /* ignore */ } - } - } - return { success: true }; - } - /** * Close an SFTP connection. * Also cleans up any jump host connections and file watchers if present. @@ -599,6 +579,20 @@ function createFileOpsApi(ctx) { leaseCount: sftpTransferSessionLeaseStore.getLeaseCount(sftpId), }; } + // No transfer currently holds the client. Commit the close before + // the first async teardown step so a new transfer cannot borrow a + // client whose end() is already in progress. + const closeToken = sftpTransferSessionLeaseStore.beginHardClose(sftpId); + if (!sftpTransferSessionLeaseStore.commitHardClose(sftpId, closeToken)) { + if (sftpTransferSessionLeaseStore.isHeld(sftpId)) { + sftpTransferSessionLeaseStore.markSoftClosed(sftpId); + return { + success: true, + deferred: true, + leaseCount: sftpTransferSessionLeaseStore.getLeaseCount(sftpId), + }; + } + } } catch { // Lease module unavailable — fall through to hard close. } @@ -915,62 +909,12 @@ function createFileOpsApi(ctx) { if (sshClient && typeof sshClient.exec === "function") { let execStream = null; try { - const result = await new Promise((resolve, reject) => { - let settled = false; - let timer = null; - const cleanup = () => { - if (timer) { - clearTimeout(timer); - timer = null; - } - if (signal) { - signal.removeEventListener("abort", onAbort); - } - }; - const closeExecStream = () => { - try { execStream?.close?.(); } catch {} - try { execStream?.destroy?.(); } catch {} - }; - const finishResolve = (value) => { - if (settled) return; - settled = true; - cleanup(); - resolve(value); - }; - const finishReject = (err) => { - if (settled) return; - settled = true; - cleanup(); - reject(err); - }; - const onAbort = () => { - closeExecStream(); - finishReject(createAbortError(signal, "SFTP home probe was aborted")); - }; - if (signal) { - signal.addEventListener("abort", onAbort, { once: true }); - } - timer = setTimeout(() => { - closeExecStream(); - finishReject(new Error("SFTP home probe timed out after 5000ms")); - }, 5000); - sshClient.exec("echo ~", (err, stream) => { - if (err) { - finishReject(err); - return; - } - if (settled) { - try { stream?.close?.(); } catch {} - try { stream?.destroy?.(); } catch {} - return; - } - execStream = stream; - let stdout = ""; - stream.once("error", finishReject); - stream.on("close", (code) => finishResolve({ stdout, code })); - stream.on("data", (data) => { stdout += data.toString(); }); - stream.stderr.on("data", () => {}); - }); + const result = await executeBoundedSshCommand(sshClient, "echo ~", { + signal, + openingTimeoutMs: 5000, + runTimeoutMs: 5000, + maxOutputBytes: 16 * 1024, + onStream(stream) { execStream = stream; }, }); throwIfAborted(signal); const home = result.stdout?.trim(); @@ -1013,12 +957,11 @@ function createFileOpsApi(ctx) { return { listSftp, + realpathSftp, readSftp, readSftpBinary, writeSftp, writeSftpBinary, - writeSftpBinaryWithProgress, - cancelSftpUpload, closeSftp, mkdirSftp, deleteSftp, diff --git a/electron/bridges/sftpBridge/openConnection.cjs b/electron/bridges/sftpBridge/openConnection.cjs index 17c25febe..64410a449 100644 --- a/electron/bridges/sftpBridge/openConnection.cjs +++ b/electron/bridges/sftpBridge/openConnection.cjs @@ -3,6 +3,13 @@ const { resolveSshConnectionTimeouts } = require("../sshBridge/startSession.cjs" const { runWhenProxyConnectionReady } = require("../proxyUtils.cjs"); const { normalizeFileProtocol } = require("./scpShell.cjs"); const { getScpBackendForClient } = require("./scpBackend.cjs"); +const { + executeBoundedSshCommand, + openBoundedSshExecStreamCallback, + terminateSshExecStream, +} = require("../boundedSshExec.cjs"); +const { openBoundedSftpChannel } = require("../boundedSftpOpen.cjs"); +const { openBoundedForwardOutCallback } = require("../boundedSshChannelOpen.cjs"); /** Bound shell/scp probes so a hung remote exec cannot leave the panel connecting forever. */ const SCP_PROBE_TIMEOUT_MS = 15_000; @@ -61,6 +68,10 @@ function shouldRetrySftpKeyboardInteractiveFirst(options, authConfig, err) { ); } +function shouldRegisterFreshSftpTransport(options) { + return options?.reuseTransport !== false && !options?.sudo; +} + function createOpenConnectionApi(ctx) { with (ctx) { /** @@ -450,7 +461,7 @@ function createOpenConnectionApi(ctx) { reject(new Error(`Connection timeout to ${nextHost}:${nextPort}`)); }, nextConnectionTimeouts.tcpConnectTimeoutMs); timeout.unref?.(); - conn.forwardOut('127.0.0.1', 0, nextHost, nextPort, (err, stream) => { + openBoundedForwardOutCallback(conn, '127.0.0.1', 0, nextHost, nextPort, (err, stream) => { if (settled) { try { stream?.end?.(); } catch { /* ignore */ } return; @@ -512,15 +523,12 @@ function createOpenConnectionApi(ctx) { // Try to find the path for (const p of sftpPaths) { try { - await new Promise((resolve, reject) => { - client.exec(`test -x ${p}`, (err, stream) => { - if (err) return reject(err); - stream.on('exit', (code) => { - if (code === 0) resolve(); - else reject(new Error('Not found')); - }); - }); + const result = await executeBoundedSshCommand(client, `test -x ${p}`, { + openingTimeoutMs: SCP_PROBE_TIMEOUT_MS, + runTimeoutMs: SCP_PROBE_TIMEOUT_MS, + maxOutputBytes: 16 * 1024, }); + if (result.code !== 0) throw new Error('Not found'); serverPath = p; break; } catch (e) { @@ -548,9 +556,8 @@ function createOpenConnectionApi(ctx) { const cmd = `sudo -S -p '${prompt}' sh -c 'printf ${readyMarker}; exec ${serverPath} -e'`; console.log(`[SFTP] Executing sudo command: ${cmd}`); - // Disable pty to ensure clean binary stream for SFTP - client.exec(cmd, { pty: false }, (err, stream) => { + openBoundedSshExecStreamCallback(client, cmd, { pty: false }, (err, stream) => { if (err) return reject(err); // Add stream lifecycle logging @@ -565,23 +572,32 @@ function createOpenConnectionApi(ctx) { let stderrBuffer = ""; let pendingAfterMarker = null; let sftpCreated = false; + let initDelayId = null; const timeoutMs = 20000; const timeoutId = setTimeout(() => { if (sftpInitialized || settled) return; settled = true; stream.stderr?.removeListener('data', onStderr); stream.removeListener('data', onStdout); + if (initDelayId) clearTimeout(initDelayId); + terminateSshExecStream(stream); const error = new Error("SFTP sudo handshake timed out. This may happen if: (1) the password is incorrect, (2) sudo requires a TTY, or (3) the user does not have sudo privileges."); reject(error); }, timeoutMs); + timeoutId.unref?.(); const finalize = (err, result) => { if (settled) return; settled = true; clearTimeout(timeoutId); + if (initDelayId) clearTimeout(initDelayId); + initDelayId = null; stream.stderr?.removeListener('data', onStderr); stream.removeListener('data', onStdout); - if (err) reject(err); + if (err) { + terminateSshExecStream(stream); + reject(err); + } else resolve(result); }; @@ -664,18 +680,21 @@ function createOpenConnectionApi(ctx) { // Delay SFTP initialization to ensure sftp-server is fully started and stream is clean // Increased timeout to 1000ms to be safe - setTimeout(() => { + initDelayId = setTimeout(() => { + initDelayId = null; initSftp(); }, 1000); + initDelayId.unref?.(); } else if (stdoutBuffer.length > 256) { stdoutBuffer = stdoutBuffer.subarray(stdoutBuffer.length - 256); } }; const onStderr = (data) => { - const chunk = data.toString(); + const raw = Buffer.isBuffer(data) ? data : Buffer.from(String(data)); + const chunk = raw.subarray(Math.max(0, raw.length - 1024)).toString(); // Only log that we received stderr data, not the content (may contain sensitive prompts) - stderrBuffer += chunk; + stderrBuffer = (stderrBuffer + chunk).slice(-1024); if (stderrBuffer.includes(prompt)) { console.log("[SFTP] Sudo requested password, sending..."); // Send password @@ -686,9 +705,7 @@ function createOpenConnectionApi(ctx) { stream.write('\n'); } stderrBuffer = ""; - } else if (stderrBuffer.length > 256) { - stderrBuffer = stderrBuffer.slice(-256); - } + } else if (stderrBuffer.length > 256) stderrBuffer = stderrBuffer.slice(-256); }; stream.on('data', onStdout); @@ -708,7 +725,16 @@ function createOpenConnectionApi(ctx) { finalize(error); } }); - }); + stream.on('error', (error) => { + if (!sftpInitialized) finalize(error); + }); + stream.on('close', () => { + if (!sftpInitialized) finalize(new Error("SFTP sudo stream closed before protocol initialization")); + }); + stream.on('end', () => { + if (!sftpInitialized) finalize(new Error("SFTP sudo stream ended before protocol initialization")); + }); + }, { openingTimeoutMs: 20_000 }); }); } @@ -718,165 +744,71 @@ function createOpenConnectionApi(ctx) { */ async function openSftp(event, options) { const connId = options.sessionId || randomUUID(); + const reuseEndpoint = buildConnectionReuseEndpoint(options, { + sftpSudo: Boolean(options.sudo), + }); + let pendingDialCoordination = options._pendingDialCoordination || null; - if (options.sourceSessionId && !options.sudo) { - // reuseOnly: the caller named a specific live session (Connected picker). - // Skip endpoint matching — renderer session.username/port can lag the - // authenticated _reuseEndpoint (identity/auth-dialog username, default port). - // Non-reuseOnly callers still pass the requested target so Copy/SFTP - // reuse cannot attach to a connection for a different host. - const sourceSession = findReusableSession?.( - sessions, - options.sourceSessionId, - options.reuseOnly - ? undefined - : { - hostname: options.hostname, - port: options.port || 22, - username: options.username || "root", - }, - ); - if (sourceSession?.conn && sourceSession?.connRef) { - const refHolder = { - id: connId, - conn: sourceSession.conn, - stream: null, - webContentsId: event.sender?.id, - __sshLeaseKind: "sftp", - }; - acquireConnectionRef?.(refHolder, sourceSession.connRef); - const reusedClient = createSessionBackedSftpClient( - connId, - sourceSession.conn, - { refHolder, sourceSessionId: options.sourceSessionId }, - ); - const fileProtocol = normalizeFileProtocol(options.fileProtocol); - try { - sendSftpProgress(event.sender, connId, options.hostname, 'connecting', 'reusing terminal connection'); - if (fileProtocol === "scp") { - await activateScpMode(reusedClient); - reusedClient.__netcattySudoMode = false; - sftpClients.set(connId, reusedClient); - sendSftpProgress(event.sender, connId, options.hostname, 'connected', 'reused terminal connection (SCP mode)'); - console.log(`[SFTP] Reused terminal SSH connection ${options.sourceSessionId} for ${connId} in SCP mode`); - return { sftpId: connId, fileProtocol: "scp" }; - } - try { - await requireSftpChannel(reusedClient); - reusedClient.__netcattyFileProtocol = "sftp"; - reusedClient.__netcattySudoMode = false; - sftpClients.set(connId, reusedClient); - sendSftpProgress(event.sender, connId, options.hostname, 'connected', 'reused terminal connection'); - console.log(`[SFTP] Reused terminal SSH connection ${options.sourceSessionId} for ${connId}`); - return { sftpId: connId, fileProtocol: "sftp" }; - } catch (sftpErr) { - if (fileProtocol === "sftp") throw sftpErr; - // Auto: fall back to SCP-mode on the same reused SSH connection - console.warn( - `[SFTP] SFTP channel unavailable on reused session ${options.sourceSessionId}; falling back to SCP mode:`, - sftpErr?.message || String(sftpErr), - ); - await activateScpMode(reusedClient); - reusedClient.__netcattySudoMode = false; - sftpClients.set(connId, reusedClient); - sendSftpProgress(event.sender, connId, options.hostname, 'connected', 'reused terminal connection (SCP mode)'); - console.log(`[SFTP] Reused terminal SSH connection ${options.sourceSessionId} for ${connId} in SCP mode (auto fallback)`); - return { sftpId: connId, fileProtocol: "scp" }; - } - } catch (reuseErr) { - try { - await reusedClient.end(); - } catch { - // Ignore cleanup errors while falling back to a fresh SFTP connection. - } - if (options.reuseOnly) { - throw new Error( - `Failed to reuse terminal SSH connection ${options.sourceSessionId}: ${reuseErr?.message || String(reuseErr)}`, - ); - } - console.warn( - `[SFTP] Failed to reuse terminal SSH connection ${options.sourceSessionId} for ${connId}; falling back to fresh connection:`, - reuseErr?.message || String(reuseErr), - ); + const openOnSharedTransport = async (transport, detail = "reusing shared SSH transport") => { + const refHolder = { + id: connId, + __sshLeaseKind: "sftp", + webContentsId: event.sender?.id, + }; + acquireConnectionRef?.(refHolder, transport); + const reusedClient = createSessionBackedSftpClient(connId, transport.conn, { + refHolder, + sourceSessionId: options.sourceSessionId || null, + }); + reusedClient.__netcattyEndpointKey = transport.endpointKey || buildEndpointKey(reuseEndpoint); + reusedClient.__netcattyTransportManaged = true; + const fileProtocol = normalizeFileProtocol(options.fileProtocol); + try { + sendSftpProgress(event.sender, connId, options.hostname, "connecting", detail); + if (fileProtocol === "scp") { + await activateScpMode(reusedClient); + reusedClient.__netcattySudoMode = false; + sftpClients.set(connId, reusedClient); + sendSftpProgress(event.sender, connId, options.hostname, "connected", `${detail} (SCP mode)`); + return { sftpId: connId, fileProtocol: "scp" }; } - } else if (options.reuseOnly) { - throw new Error( - `Source session ${options.sourceSessionId} is not reusable for SFTP`, - ); - } else { - console.log(`[SFTP] Reuse requested for ${connId} but source session is not reusable; connecting fresh`); + try { + await requireSftpChannel(reusedClient); + reusedClient.__netcattyFileProtocol = "sftp"; + reusedClient.__netcattySudoMode = false; + sftpClients.set(connId, reusedClient); + sendSftpProgress(event.sender, connId, options.hostname, "connected", detail); + return { sftpId: connId, fileProtocol: "sftp" }; + } catch (sftpErr) { + if (fileProtocol === "sftp") throw sftpErr; + await activateScpMode(reusedClient); + reusedClient.__netcattySudoMode = false; + sftpClients.set(connId, reusedClient); + sendSftpProgress(event.sender, connId, options.hostname, "connected", `${detail} (SCP mode)`); + return { sftpId: connId, fileProtocol: "scp" }; + } + } catch (err) { + try { await reusedClient.end(); } catch { /* ignore */ } + throw err; } - } + }; // Parked / shared transport without a live terminal session: open an SFTP // channel on the registry transport (no second MFA). // Dedicated bulk transfer / restart-resume passes reuseTransport:false so // a transfer never silently attaches to a terminal/parked conn. if ( - options.reuseTransport !== false + !pendingDialCoordination + && options.reuseTransport !== false && !options.sudo && typeof findTransportByEndpoint === "function" && typeof createSessionBackedSftpClient === "function" ) { - const endpoint = { - hostId: options.hostId || "", - hostname: options.hostname, - port: options.port || 22, - username: options.username || "root", - jumpHosts: options.jumpHosts, - proxy: options.proxy || null, - authType: options.authType || options.authMethod || "", - keyId: options.keyId || options.identityId || "", - certificate: options.certificate || "", - requiresMfa: !!options.requiresMfa, - verifyHostKeys: options.verifyHostKeys, - useSshAgent: options.useSshAgent, - password: options.password, - privateKey: options.privateKey, - publicKey: options.publicKey, - passphrase: options.passphrase, - identityFilePaths: options.identityFilePaths, - }; - const transport = findTransportByEndpoint(endpoint); + const transport = findTransportByEndpoint(reuseEndpoint); if (transport?.conn) { - const refHolder = { - id: connId, - __sshLeaseKind: "sftp", - webContentsId: event.sender?.id, - }; try { - acquireConnectionRef?.(refHolder, transport); - const reusedClient = createSessionBackedSftpClient(connId, transport.conn, { - refHolder, - sourceSessionId: options.sourceSessionId || null, - }); - reusedClient.__netcattyTransportManaged = true; - const fileProtocol = normalizeFileProtocol(options.fileProtocol); - sendSftpProgress(event.sender, connId, options.hostname, "connecting", "reusing parked SSH transport"); - if (fileProtocol === "scp") { - await activateScpMode(reusedClient); - reusedClient.__netcattySudoMode = false; - sftpClients.set(connId, reusedClient); - sendSftpProgress(event.sender, connId, options.hostname, "connected", "reused parked transport (SCP mode)"); - return { sftpId: connId, fileProtocol: "scp" }; - } - try { - await requireSftpChannel(reusedClient); - reusedClient.__netcattyFileProtocol = "sftp"; - reusedClient.__netcattySudoMode = false; - sftpClients.set(connId, reusedClient); - sendSftpProgress(event.sender, connId, options.hostname, "connected", "reused parked transport"); - return { sftpId: connId, fileProtocol: "sftp" }; - } catch (sftpErr) { - if (fileProtocol === "sftp") throw sftpErr; - await activateScpMode(reusedClient); - reusedClient.__netcattySudoMode = false; - sftpClients.set(connId, reusedClient); - sendSftpProgress(event.sender, connId, options.hostname, "connected", "reused parked transport (SCP mode)"); - return { sftpId: connId, fileProtocol: "scp" }; - } + return await openOnSharedTransport(transport, "reused parked transport"); } catch (parkErr) { - try { releaseConnectionRef?.(refHolder); } catch { /* ignore */ } console.warn( `[SFTP] Parked transport reuse failed for ${connId}; connecting fresh:`, parkErr?.message || String(parkErr), @@ -885,7 +817,52 @@ function createOpenConnectionApi(ctx) { } } + if ( + !pendingDialCoordination + && options.reuseTransport !== false + && !options.sudo + && typeof beginTransportDial === "function" + ) { + const coordination = beginTransportDial(reuseEndpoint, { kind: "channel" }); + if (coordination.role === "reuse" || coordination.role === "join") { + try { + const transport = coordination.role === "reuse" + ? coordination.transport + : await waitForTransportDial(coordination); + return await openOnSharedTransport(transport, "reused coordinated transport"); + } catch (coordinationErr) { + if (coordination.role === "join") throw coordinationErr; + console.warn( + `[SFTP] Coordinated transport reuse failed for ${connId}; connecting fresh:`, + coordinationErr?.message || String(coordinationErr), + ); + } + } else { + pendingDialCoordination = coordination; + } + } + + try { + return await openFreshSftp(); + } catch (err) { + if (pendingDialCoordination) failTransportDial(pendingDialCoordination, err); + throw err; + } + + async function openFreshSftp() { const client = new SftpClient(); + client.__netcattyEndpointKey = buildEndpointKey(reuseEndpoint); + let freshClientClosed = false; + const closeFreshClient = () => { + if (freshClientClosed) return; + freshClientClosed = true; + // A failure after SSH authentication but before registration in + // sftpClients/the transport pool otherwise leaves an unreachable live + // socket. This is deliberately low-level: there may be no SFTP channel + // for ssh2-sftp-client.end() to close yet. + try { client.client?.end?.(); } catch { /* ignore */ } + try { client.client?.destroy?.(); } catch { /* ignore */ } + }; // Get default keys early to use for both chain and target const defaultKeys = await findAllDefaultPrivateKeysFromHelper(); @@ -939,6 +916,7 @@ function createOpenConnectionApi(ctx) { ); } + const keepalivePolicy = resolveConnectionKeepalivePolicy(options); const connectOpts = { host: options.hostname, port: options.port || 22, @@ -955,12 +933,8 @@ function createOpenConnectionApi(ctx) { // unanswered probes) // - undefined: legacy caller path, fall back to 10s/3 so an idle SFTP // browse over a NAT doesn't drop (the original #669 protection) - keepaliveInterval: options.keepaliveInterval == null - ? 10000 - : (options.keepaliveInterval > 0 ? options.keepaliveInterval * 1000 : 0), - keepaliveCountMax: options.keepaliveInterval == null - ? 3 - : (options.keepaliveInterval > 0 ? (options.keepaliveCountMax ?? 3) : 0), + keepaliveInterval: keepalivePolicy.keepaliveIntervalMs, + keepaliveCountMax: keepalivePolicy.keepaliveCountMax, algorithms: buildSftpAlgorithms(options.legacyAlgorithms, { skipEcdsaHostKey: options.skipEcdsaHostKey, algorithmOverrides: options.algorithmOverrides, @@ -1235,7 +1209,7 @@ function createOpenConnectionApi(ctx) { await activateScpMode(client); resolve(); } catch (scpErr) { - try { sshClient.end(); } catch { /* ignore */ } + closeFreshClient(); reject(scpErr); } }; @@ -1243,7 +1217,7 @@ function createOpenConnectionApi(ctx) { if (fileProtocol === "scp") { if (options.sudo) { // Forced SCP cannot provide sudo elevation; reject contradictory host data. - try { sshClient.end(); } catch { /* ignore */ } + closeFreshClient(); reject(new Error( "Sudo Mode is not supported with File Protocol set to SCP. Disable Sudo Mode or use Auto/SFTP.", )); @@ -1272,31 +1246,29 @@ function createOpenConnectionApi(ctx) { if (e.message && e.message.includes('exit code 127')) { console.warn('[SFTP] sftp-server not found, falling back to standard SFTP subsystem'); options.sudo = false; // Mark as non-sudo for downstream logic - sshClient.sftp((sftpErr, sftp) => { - if (sftpErr) { - // Do not drop to SCP after a sudo-mode open: elevation was requested. - sshClient.end(); - return reject(sftpErr); - } + openBoundedSftpChannel(sshClient).then((sftp) => { finishSftp(sftp); + }).catch((sftpErr) => { + // Do not drop to SCP after a sudo-mode open: elevation was requested. + closeFreshClient(); + reject(sftpErr); }); } else { - sshClient.end(); + closeFreshClient(); reject(e); } } })(); } else { // Open standard SFTP subsystem channel - sshClient.sftp((err, sftp) => { - if (err) { + openBoundedSftpChannel(sshClient).then((sftp) => { + finishSftp(sftp); + }).catch((err) => { if (fileProtocol === "auto") { void finishScp(`SFTP subsystem unavailable (${err.message})`); return; } - return reject(err); - } - finishSftp(sftp); + reject(err); }); } }); @@ -1326,29 +1298,11 @@ function createOpenConnectionApi(ctx) { const sshConn = client.client; if ( sshConn + && shouldRegisterFreshSftpTransport(options) && typeof createTransport === "function" && typeof borrowTransport === "function" ) { try { - const endpoint = { - hostId: options.hostId || "", - hostname: options.hostname, - port: options.port || 22, - username: options.username || "root", - jumpHosts: options.jumpHosts, - proxy: options.proxy || null, - authType: options.authType || options.authMethod || "", - keyId: options.keyId || options.identityId || "", - certificate: options.certificate || "", - requiresMfa: !!options.requiresMfa, - verifyHostKeys: options.verifyHostKeys, - useSshAgent: options.useSshAgent, - password: options.password, - privateKey: options.privateKey, - publicKey: options.publicKey, - passphrase: options.passphrase, - identityFilePaths: options.identityFilePaths, - }; const refHolder = { id: connId, __sshLeaseKind: "sftp", @@ -1357,7 +1311,7 @@ function createOpenConnectionApi(ctx) { const transport = createTransport({ conn: sshConn, chainConnections, - endpoint, + endpoint: reuseEndpoint, }); borrowTransport(transport, { kind: "sftp", @@ -1367,9 +1321,15 @@ function createOpenConnectionApi(ctx) { }); client.__netcattyRefHolder = refHolder; client.__netcattyTransportManaged = true; + if (pendingDialCoordination) { + completeTransportDial(pendingDialCoordination, transport); + } // Chain lifetime is owned by the transport; do not double-end on close. chainConnections = []; } catch (regErr) { + if (pendingDialCoordination) { + failTransportDial(pendingDialCoordination, regErr); + } console.warn( `[SFTP] Failed to register dedicated transport for ${connId}:`, regErr?.message || String(regErr), @@ -1393,17 +1353,19 @@ function createOpenConnectionApi(ctx) { } catch (err) { // Cleanup jump connections on error cleanupPendingConnection(); + closeFreshClient(); if (shouldRetrySftpKeyboardInteractiveFirst(options, authConfig, err)) { - try { client.client?.end?.(); } catch { /* ignore */ } - try { client.client?.destroy?.(); } catch { /* ignore */ } console.log("[SFTP] Password auth removed keyboard-interactive; retrying with keyboard-interactive first..."); return openSftp(event, { ...options, _skipPasswordMethod: true, + _pendingDialCoordination: pendingDialCoordination, }); } + if (pendingDialCoordination) failTransportDial(pendingDialCoordination, err); throw err; } + } } return { connectThroughChainForSftp, connectSudoSftp, openSftp }; } @@ -1412,5 +1374,6 @@ function createOpenConnectionApi(ctx) { module.exports = { createOpenConnectionApi, createBoundedProbeSignal, + shouldRegisterFreshSftpTransport, SCP_PROBE_TIMEOUT_MS, }; diff --git a/electron/bridges/sftpBridge/scpBackend.cjs b/electron/bridges/sftpBridge/scpBackend.cjs index ecb7c7ab3..40d5538e4 100644 --- a/electron/bridges/sftpBridge/scpBackend.cjs +++ b/electron/bridges/sftpBridge/scpBackend.cjs @@ -11,6 +11,8 @@ const fs = require("node:fs"); const path = require("node:path"); +const { executeBoundedSshCommand, terminateSshExecStream } = require("../boundedSshExec.cjs"); +const { invalidateSshTransport } = require("../sshTransportInvalidation.cjs"); const { buildFileControlLine, buildAck, @@ -327,6 +329,7 @@ function createScpBackend(deps = {}) { onProgress: null, encoding: options.encoding || "utf-8", signal: options.signal || null, + maxBytes: options.maxBytes, }); return Buffer.concat(chunks); } @@ -610,6 +613,7 @@ function createScpBackend(deps = {}) { onProgress, encoding = "utf-8", signal = null, + maxBytes = null, } = {}) { assertSafeRemotePath(remotePath); const command = buildScpSourceCommand(remotePath, encoding); @@ -635,6 +639,17 @@ function createScpBackend(deps = {}) { let transferred = 0; let expectedSize = fileSize; let gotFile = false; + const readLimit = Number.isSafeInteger(maxBytes) && maxBytes >= 0 ? maxBytes : null; + const createReadLimitError = (actualBytes) => { + const error = new Error( + `Remote file is too large to read into memory (${actualBytes} bytes; maximum ${readLimit} bytes). ` + + "Use SFTP download for large files.", + ); + error.code = "SFTP_READ_TOO_LARGE"; + error.maxBytes = readLimit; + error.actualBytes = actualBytes; + return error; + }; await new Promise((resolve, reject) => { let settled = false; @@ -670,8 +685,18 @@ function createScpBackend(deps = {}) { if (ev.type === "file-start") { gotFile = true; expectedSize = ev.size; + if (readLimit != null && ev.size > readLimit) { + finish(createReadLimitError(ev.size)); + abort(); + return; + } stream.write(buildAck()); } else if (ev.type === "file-data") { + if (readLimit != null && transferred + ev.data.length > readLimit) { + finish(createReadLimitError(transferred + ev.data.length)); + abort(); + return; + } const canContinue = writable.write(ev.data); transferred += ev.data.length; if (typeof onProgress === "function") { @@ -972,54 +997,11 @@ function createScpBackend(deps = {}) { */ function createSshExecAdapters(sshClient) { function exec(command, options = {}) { - const signal = options.signal || null; - return new Promise((resolve, reject) => { - if (signal?.aborted) { - reject(new Error("Transfer cancelled")); - return; - } - let settled = false; - let streamRef = null; - const cleanup = () => { - if (signal) { - try { signal.removeEventListener("abort", onAbort); } catch { /* ignore */ } - } - }; - const finish = (fn, value) => { - if (settled) return; - settled = true; - cleanup(); - fn(value); - }; - const onAbort = () => { - try { streamRef?.close?.(); } catch { /* ignore */ } - try { streamRef?.destroy?.(); } catch { /* ignore */ } - finish(reject, new Error("Transfer cancelled")); - }; - if (signal) { - signal.addEventListener("abort", onAbort, { once: true }); - } - try { - sshClient.exec(command, (err, stream) => { - if (err) { - finish(reject, err); - return; - } - if (settled) { - try { stream.close?.(); } catch { /* ignore */ } - return; - } - streamRef = stream; - let stdout = ""; - let stderr = ""; - stream.on("data", (d) => { stdout += d.toString(); }); - stream.stderr?.on("data", (d) => { stderr += d.toString(); }); - stream.on("close", (code) => finish(resolve, { stdout, stderr, code })); - stream.on("error", (streamErr) => finish(reject, streamErr)); - }); - } catch (err) { - finish(reject, err); - } + return executeBoundedSshCommand(sshClient, command, { + signal: options.signal || null, + openingTimeoutMs: options.openingTimeoutMs, + runTimeoutMs: options.runTimeoutMs, + maxOutputBytes: options.maxOutputBytes, }); } @@ -1032,6 +1014,7 @@ function createSshExecAdapters(sshClient) { } let settled = false; let streamRef = null; + let openingTimer = null; let abortCleaned = false; const cleanupAbort = () => { if (abortCleaned || !signal) return; @@ -1040,6 +1023,8 @@ function createSshExecAdapters(sshClient) { if (streamRef?.__netcattyCleanupAbort === cleanupAbort) { delete streamRef.__netcattyCleanupAbort; } + if (openingTimer) clearTimeout(openingTimer); + openingTimer = null; }; const onAbort = () => { try { streamRef?.close?.(); } catch { /* ignore */ } @@ -1048,6 +1033,7 @@ function createSshExecAdapters(sshClient) { if (!settled) { settled = true; reject(new Error("Transfer cancelled")); + if (!streamRef) invalidateSshTransport(sshClient); } }; if (signal) { @@ -1057,8 +1043,19 @@ function createSshExecAdapters(sshClient) { return; } } + openingTimer = setTimeout(() => { + if (settled) return; + settled = true; + cleanupAbort(); + terminateSshExecStream(streamRef); + reject(new Error("SSH exec channel open timed out after 15000 ms")); + invalidateSshTransport(sshClient); + }, 15_000); + openingTimer.unref?.(); try { sshClient.exec(command, (err, stream) => { + if (openingTimer) clearTimeout(openingTimer); + openingTimer = null; if (settled) { try { stream?.close?.(); } catch { /* ignore */ } try { stream?.destroy?.(); } catch { /* ignore */ } @@ -1139,51 +1136,13 @@ function getScpBackendForClient(client) { }; // Wrap base.exec so shell list/stat/mkdir streams are also abortable on closeSftp. const trackedExec = (command, options = {}) => new Promise((resolve, reject) => { - const signal = options.signal || null; - if (signal?.aborted) { - reject(new Error("Transfer cancelled")); - return; - } - let settled = false; - let streamRef = null; - const cleanup = () => { - if (streamRef) { - try { client.__netcattyScpActiveStreams.delete(streamRef); } catch { /* ignore */ } - } - if (signal) { - try { signal.removeEventListener("abort", onAbort); } catch { /* ignore */ } - } - }; - const finish = (fn, value) => { - if (settled) return; - settled = true; - cleanup(); - fn(value); - }; - const onAbort = () => { - try { streamRef?.close?.(); } catch { /* ignore */ } - try { streamRef?.destroy?.(); } catch { /* ignore */ } - finish(reject, new Error("Transfer cancelled")); - }; - if (signal) signal.addEventListener("abort", onAbort, { once: true }); - sshClient.exec(command, (err, stream) => { - if (err) { - finish(reject, err); - return; - } - if (settled) { - try { stream.close?.(); } catch { /* ignore */ } - return; - } - streamRef = stream; - track(stream); - let stdout = ""; - let stderr = ""; - stream.on("data", (d) => { stdout += d.toString(); }); - stream.stderr?.on("data", (d) => { stderr += d.toString(); }); - stream.on("close", (code) => finish(resolve, { stdout, stderr, code })); - stream.on("error", (streamErr) => finish(reject, streamErr)); - }); + executeBoundedSshCommand(sshClient, command, { + signal: options.signal || null, + openingTimeoutMs: options.openingTimeoutMs, + runTimeoutMs: options.runTimeoutMs, + maxOutputBytes: options.maxOutputBytes, + onStream: track, + }).then(resolve, reject); }); const adapters = { exec: trackedExec, diff --git a/electron/bridges/sftpBridge/scpBackend.test.cjs b/electron/bridges/sftpBridge/scpBackend.test.cjs index a0ab8f27b..0b0f05efc 100644 --- a/electron/bridges/sftpBridge/scpBackend.test.cjs +++ b/electron/bridges/sftpBridge/scpBackend.test.cjs @@ -199,6 +199,34 @@ describe("scpBackend browse/manage with fake exec", () => { }); }); +it("SCP in-memory reads reject a grown file from its protocol header", async () => { + const stream = createMockStream(); + let ackCount = 0; + stream.on("_write", (buf) => { + if (!(buf.length === 1 && buf[0] === SCP_OK)) return; + ackCount += 1; + if (ackCount === 1) { + setImmediate(() => stream.pushFromRemote( + buildFileControlLine({ mode: 0o644, size: 5, name: "grown.bin" }), + )); + } else if (ackCount === 2) { + setImmediate(() => stream.pushFromRemote( + Buffer.concat([Buffer.from("ABCDE"), Buffer.from([0x00])]), + )); + } + }); + const backend = createScpBackend({ + exec: async () => ({ stdout: "", stderr: "", code: 0 }), + execStream: async () => stream, + }); + + await assert.rejects( + backend.readFile("/remote/grown.bin", { maxBytes: 4 }), + /too large.*4 bytes.*download/i, + ); + assert.equal(ackCount, 1, "oversized content must not be acknowledged for transfer"); +}); + describe("scpBackend upload/download with fake scp streams", () => { let tmpDir; @@ -507,11 +535,13 @@ describe("scpBackend upload/download with fake scp streams", () => { it("rejects signal-only upload cancellation when exec never calls back", async () => { const controller = new AbortController(); + let invalidations = 0; const sshClient = { exec() { // Deliberately never calls back: cancellation must settle the pending // execStream promise without waiting for an SSH channel. }, + destroy() { invalidations += 1; }, }; const backend = createScpBackend(createSshExecAdapters(sshClient)); const upload = backend.uploadBuffer(Buffer.from("payload"), "file.bin", { @@ -526,6 +556,7 @@ describe("scpBackend upload/download with fake scp streams", () => { ]), /cancel|abort/i, ); + assert.equal(invalidations, 1); }); it("rejects upload streams that close or error before ACK listeners attach", async () => { diff --git a/electron/bridges/sftpBridge/scpIntegration.fileOps.real.test.cjs b/electron/bridges/sftpBridge/scpIntegration.fileOps.real.test.cjs index ca7d17b8d..0f1ec8540 100644 --- a/electron/bridges/sftpBridge/scpIntegration.fileOps.real.test.cjs +++ b/electron/bridges/sftpBridge/scpIntegration.fileOps.real.test.cjs @@ -75,7 +75,6 @@ suite("real SCP via fileOps IPC surface", () => { get electronModule() { return { webContents: { fromId: () => ({ send: () => {} }) } }; }, - activeSftpUploads: new Map(), fileWatcherBridge: { stopWatchersForSession: () => {} }, fs, path, @@ -155,28 +154,6 @@ suite("real SCP via fileOps IPC surface", () => { console.log("[scp-fileops-it] ops ok", { names, afterDel }); }); - it("writeSftpBinaryWithProgress completes on real SCP session", async () => { - const buf = crypto.randomBytes(4096); - const remote = `${remotePrefix}/prog.bin`; - const progress = []; - const result = await api.writeSftpBinaryWithProgress(null, { - sftpId, - path: remote, - content: buf, - transferId: "it-transfer-1", - onProgress: (t, total) => progress.push([t, total]), - onComplete: () => progress.push(["done"]), - }); - assert.equal(result.success, true); - const read = await api.readSftpBinary(null, { sftpId, path: remote }); - const got = Buffer.from(read); - assert.equal(got.length, buf.length); - assert.equal( - crypto.createHash("sha256").update(got).digest("hex"), - crypto.createHash("sha256").update(buf).digest("hex"), - ); - console.log("[scp-fileops-it] binary progress events", progress.length); - }); }); if (!ENABLED) { diff --git a/electron/bridges/sftpBridge/scpSessionOps.test.cjs b/electron/bridges/sftpBridge/scpSessionOps.test.cjs index 7b4d59d27..4be8d2169 100644 --- a/electron/bridges/sftpBridge/scpSessionOps.test.cjs +++ b/electron/bridges/sftpBridge/scpSessionOps.test.cjs @@ -116,7 +116,6 @@ describe("SCP-mode session ops via fileOps entry points (AI/UI shared path)", () webContents: { fromId: () => ({ send: () => {} }) }, }; }, - activeSftpUploads: new Map(), fileWatcherBridge: { stopWatchersForSession: () => {} }, fs, path, diff --git a/electron/bridges/sftpTransferSessionLease.cjs b/electron/bridges/sftpTransferSessionLease.cjs index cd87aad83..2fe66faa9 100644 --- a/electron/bridges/sftpTransferSessionLease.cjs +++ b/electron/bridges/sftpTransferSessionLease.cjs @@ -27,6 +27,10 @@ * @property {(sftpId: string) => string[]} listTransferIds * @property {(sftpId: string) => boolean} markSoftClosed * @property {(sftpId: string) => boolean} isSoftClosed + * @property {(sftpId: string) => number|undefined} getPendingHardCloseToken + * @property {(sftpId: string) => number|undefined} beginHardClose + * @property {(sftpId: string, closeToken: number) => boolean} commitHardClose + * @property {(sftpId: string) => boolean} isHardCloseCommitted * @property {(sftpId: string) => void} clear * @property {() => void} resetForTests */ @@ -39,6 +43,20 @@ function createSftpTransferSessionLeaseStore() { const leases = new Map(); /** @type {Set} */ const softClosed = new Set(); + /** @type {Map} */ + const hardCloseClaims = new Map(); + let nextHardCloseToken = 0; + + function beginHardClose(sftpId) { + const existing = hardCloseClaims.get(sftpId); + if (existing) return existing.token; + nextHardCloseToken += 1; + hardCloseClaims.set(sftpId, { + token: nextHardCloseToken, + committed: false, + }); + return nextHardCloseToken; + } function getSet(sftpId) { let set = leases.get(sftpId); @@ -52,6 +70,17 @@ function createSftpTransferSessionLeaseStore() { return { acquire(sftpId, transferId) { if (!sftpId || !transferId) return false; + const closeClaim = hardCloseClaims.get(sftpId); + // Before commit, a directory walk's next child may cancel the deferred + // close and keep using the live session. After commit, the SSH/SFTP close + // has started and this client must never be lent again. + if (closeClaim?.committed) return false; + if (closeClaim) { + hardCloseClaims.delete(sftpId); + // The panel's close intent remains sticky across the cancelled close; + // the next last release must claim hard-close again. + softClosed.add(sftpId); + } const set = getSet(sftpId); const before = set.size; set.add(String(transferId)); @@ -79,7 +108,10 @@ function createSftpTransferSessionLeaseStore() { if (remaining === 0) { leases.delete(sftpId); const shouldHardClose = softClosed.has(sftpId); - if (shouldHardClose) softClosed.delete(sftpId); + if (shouldHardClose) { + beginHardClose(sftpId); + softClosed.delete(sftpId); + } return { released: true, shouldHardClose, remaining: 0 }; } return { released: true, shouldHardClose: false, remaining }; @@ -120,15 +152,40 @@ function createSftpTransferSessionLeaseStore() { return !!sftpId && softClosed.has(sftpId); }, + getPendingHardCloseToken(sftpId) { + return hardCloseClaims.get(sftpId)?.token; + }, + + beginHardClose(sftpId) { + if (!sftpId) return undefined; + return beginHardClose(sftpId); + }, + + commitHardClose(sftpId, closeToken) { + const claim = hardCloseClaims.get(sftpId); + if (!claim || claim.token !== closeToken || claim.committed || this.isHeld(sftpId)) { + return false; + } + claim.committed = true; + return true; + }, + + isHardCloseCommitted(sftpId) { + return hardCloseClaims.get(sftpId)?.committed === true; + }, + clear(sftpId) { if (!sftpId) return; leases.delete(sftpId); softClosed.delete(sftpId); + hardCloseClaims.delete(sftpId); }, resetForTests() { leases.clear(); softClosed.clear(); + hardCloseClaims.clear(); + nextHardCloseToken = 0; }, }; } diff --git a/electron/bridges/sftpTransferSessionLease.test.cjs b/electron/bridges/sftpTransferSessionLease.test.cjs index d9c62d8da..6dd47a0fb 100644 --- a/electron/bridges/sftpTransferSessionLease.test.cjs +++ b/electron/bridges/sftpTransferSessionLease.test.cjs @@ -69,6 +69,40 @@ describe("sftpTransferSessionLease", () => { assert.equal(store.isSoftClosed("sftp-a"), false); }); + it("lets the next directory child cancel an uncommitted hard close", () => { + const store = createSftpTransferSessionLeaseStore(); + store.acquire("sftp-a", "child-1"); + store.markSoftClosed("sftp-a"); + const firstRelease = store.release("sftp-a", "child-1"); + const firstCloseToken = store.getPendingHardCloseToken("sftp-a"); + assert.equal(firstRelease.shouldHardClose, true); + assert.equal(Number.isSafeInteger(firstCloseToken), true); + + assert.equal(store.acquire("sftp-a", "child-2"), true); + assert.equal(store.commitHardClose("sftp-a", firstCloseToken), false); + assert.equal(store.isHeld("sftp-a"), true); + + const secondRelease = store.release("sftp-a", "child-2"); + assert.equal(secondRelease.shouldHardClose, true); + assert.notEqual(store.getPendingHardCloseToken("sftp-a"), firstCloseToken); + }); + + it("refuses new leases after hard close commits", () => { + const store = createSftpTransferSessionLeaseStore(); + store.acquire("sftp-a", "child-1"); + store.markSoftClosed("sftp-a"); + store.release("sftp-a", "child-1"); + const closeToken = store.getPendingHardCloseToken("sftp-a"); + + assert.equal(store.commitHardClose("sftp-a", closeToken), true); + assert.equal(store.isHardCloseCommitted("sftp-a"), true); + assert.equal(store.acquire("sftp-a", "child-2"), false); + assert.equal(store.isHeld("sftp-a"), false); + + store.clear("sftp-a"); + assert.equal(store.isHardCloseCommitted("sftp-a"), false); + }); + it("tracks multiple sessions independently", () => { const store = createSftpTransferSessionLeaseStore(); store.acquire("sftp-a", "t1"); diff --git a/electron/bridges/sshAuthMethodCache.cjs b/electron/bridges/sshAuthMethodCache.cjs new file mode 100644 index 000000000..9cb88212d --- /dev/null +++ b/electron/bridges/sshAuthMethodCache.cjs @@ -0,0 +1,44 @@ +"use strict"; + +const MAX_SSH_AUTH_METHOD_CACHE_ENTRIES = 512; + +function createSshAuthMethodCache(options = {}) { + const maxEntries = Number.isFinite(options.maxEntries) + ? Math.max(1, Math.floor(options.maxEntries)) + : MAX_SSH_AUTH_METHOD_CACHE_ENTRIES; + const entries = new Map(); + + return { + get size() { + return entries.size; + }, + get(key) { + const value = entries.get(key); + if (value === undefined) return undefined; + entries.delete(key); + entries.set(key, value); + return value; + }, + set(key, value) { + entries.delete(key); + entries.set(key, value); + while (entries.size > maxEntries) { + const oldestKey = entries.keys().next().value; + if (oldestKey === undefined) break; + entries.delete(oldestKey); + } + return this; + }, + delete(key) { + return entries.delete(key); + }, + clear() { + entries.clear(); + }, + }; +} + +module.exports = { + MAX_SSH_AUTH_METHOD_CACHE_ENTRIES, + createSshAuthMethodCache, +}; diff --git a/electron/bridges/sshAuthMethodCache.test.cjs b/electron/bridges/sshAuthMethodCache.test.cjs new file mode 100644 index 000000000..b54720d52 --- /dev/null +++ b/electron/bridges/sshAuthMethodCache.test.cjs @@ -0,0 +1,34 @@ +"use strict"; + +const assert = require("node:assert/strict"); +const test = require("node:test"); + +const { + MAX_SSH_AUTH_METHOD_CACHE_ENTRIES, + createSshAuthMethodCache, +} = require("./sshAuthMethodCache.cjs"); + +test("auth method cache stays hard-bounded across thousands of endpoints", () => { + const cache = createSshAuthMethodCache(); + for (let index = 0; index < 4_000; index += 1) { + cache.set(`root@host-${index}:22`, { method: "publickey" }); + } + + assert.equal(cache.size, MAX_SSH_AUTH_METHOD_CACHE_ENTRIES); + assert.equal(cache.get("root@host-0:22"), undefined); + assert.deepEqual(cache.get("root@host-3999:22"), { method: "publickey" }); +}); + +test("auth method cache refreshes recency on successful lookup", () => { + const cache = createSshAuthMethodCache({ maxEntries: 3 }); + cache.set("a", { method: "password" }); + cache.set("b", { method: "publickey" }); + cache.set("c", { method: "publickey-default" }); + + assert.equal(cache.get("a")?.method, "password"); + cache.set("d", { method: "password" }); + + assert.equal(cache.get("b"), undefined, "least-recently-used endpoint should be evicted"); + assert.equal(cache.get("a")?.method, "password", "cache hit must protect the refreshed endpoint"); + assert.equal(cache.size, 3); +}); diff --git a/electron/bridges/sshBridge.authRetryExit.test.cjs b/electron/bridges/sshBridge.authRetryExit.test.cjs index a4cd84eb5..e29e48655 100644 --- a/electron/bridges/sshBridge.authRetryExit.test.cjs +++ b/electron/bridges/sshBridge.authRetryExit.test.cjs @@ -7,6 +7,21 @@ const os = require("node:os"); const path = require("node:path"); const keyboardInteractiveHandler = require("./keyboardInteractiveHandler.cjs"); const sessionLogStreamManager = require("./sessionLogStreamManager.cjs"); +const { + beginTransportDial, + buildConnectionReuseEndpoint, + getTransportStats, + resetSshTransportRegistryForTests, + waitForTransportDial, +} = require("./sshConnectionPool.cjs"); + +test.beforeEach(() => { + resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 }); +}); + +test.afterEach(() => { + resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 }); +}); function makeSender(events = null) { return { @@ -78,6 +93,10 @@ function loadBridgeWithAuthRetryMocks(t, options = {}) { const originalLoad = Module._load; const originalAuthHelper = require(authHelperPath); const connectEvents = options.connectEvents || ["auth-error", "ready"]; + const originalHome = process.env.HOME; + const isolatedHome = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-auth-retry-home-")); + fs.mkdirSync(path.join(isolatedHome, ".ssh")); + process.env.HOME = isolatedHome; class MockSSHClient extends EventEmitter { constructor() { @@ -447,6 +466,30 @@ function loadBridgeWithAuthRetryMocks(t, options = {}) { this.emit("error", err); return; } + if (eventName === "encrypted-key-ready") { + this.authMethodsOffered = []; + this.emit("connect"); + this.emit("handshake"); + const offerNext = (methodsLeft, partialSuccess) => { + let offered; + opts.authHandler(methodsLeft, partialSuccess, (method) => { + offered = method; + this.authMethodsOffered.push(method); + }); + return offered; + }; + offerNext(null, null); + const password = offerNext(["publickey", "password", "keyboard-interactive"], false); + const unlockedKey = offerNext(["publickey"], false); + if (password?.type !== "password" || unlockedKey?.key !== "UNLOCKED_PRIVATE_KEY") { + const err = new Error("Unlocked encrypted key was not offered after password rejection"); + err.level = "client-authentication"; + this.emit("error", err); + return; + } + this.emit("ready"); + return; + } if (eventName === "socket-error") { const err = new Error("Connection reset by auth-bastion.example.com"); err.level = "client-socket"; @@ -528,6 +571,9 @@ function loadBridgeWithAuthRetryMocks(t, options = {}) { delete require.cache[bridgePath]; delete require.cache[startSessionPath]; Module._load = originalLoad; + if (originalHome === undefined) delete process.env.HOME; + else process.env.HOME = originalHome; + fs.rmSync(isolatedHome, { recursive: true, force: true }); }); return { bridge, MockSSHClient }; @@ -653,6 +699,62 @@ test("terminal SSH retries keyboard-interactive first when password rejection re ); }); +test("terminal recoverable auth retry keeps SFTP and port-forward waiters on the same dial", async (t) => { + const { bridge, MockSSHClient } = loadBridgeWithAuthRetryMocks(t, { + connectEvents: ["mfa-keyboard-interactive-before-password", "mfa-keyboard-interactive-before-password"], + }); + const ipcMain = makeIpcMain(); + bridge.init({ sessions: new Map(), electronModule: {} }); + bridge.registerHandlers(ipcMain); + const sender = makeSender(); + const send = sender.send.bind(sender); + sender.send = (channel, payload) => { + send(channel, payload); + if (channel === "netcatty:keyboard-interactive") { + keyboardInteractiveHandler.handleResponse( + { sender }, + { + requestId: payload.requestId, + responses: ["secondary-password"], + cancelled: false, + }, + ); + } + }; + const options = { + sessionId: "mfa-shared-dial-leader", + hostId: "mfa-shared-host", + hostname: "corp-edr.example.com", + username: "alice", + authMethod: "password", + password: "login-password", + useSshAgent: false, + port: 22, + knownHosts: [], + }; + + const terminalOpen = ipcMain.handlers.get("netcatty:start")({ sender }, options); + while (getTransportStats().pendingDials === 0) { + await nextTick(); + } + const endpoint = buildConnectionReuseEndpoint(options); + const sftpWaiter = beginTransportDial(endpoint, { kind: "channel" }); + const forwardWaiter = beginTransportDial(endpoint, { kind: "channel" }); + assert.equal(sftpWaiter.role, "join"); + assert.equal(forwardWaiter.role, "join"); + + const [terminalResult, sftpTransport, forwardTransport] = await Promise.all([ + terminalOpen, + waitForTransportDial(sftpWaiter), + waitForTransportDial(forwardWaiter), + ]); + + assert.deepEqual(terminalResult, { sessionId: options.sessionId }); + assert.equal(sftpTransport, forwardTransport); + assert.equal(sftpTransport.conn, MockSSHClient.instances[1]); + assert.equal(MockSSHClient.instances.length, 2); +}); + test("terminal SSH requiresMfa prefers keyboard-interactive before password", async (t) => { const { bridge, MockSSHClient } = loadBridgeWithAuthRetryMocks(t, { connectEvents: ["mfa-keyboard-interactive-before-password"], @@ -756,7 +858,7 @@ test("terminal SSH requiresMfa prefers keyboard-interactive before automatic key test("failed keyboard-interactive retry still offers encrypted default key fallback", async (t) => { const events = []; const { bridge, MockSSHClient } = loadBridgeWithAuthRetryMocks(t, { - connectEvents: ["mfa-keyboard-interactive-before-password", "auth-error", "ready"], + connectEvents: ["mfa-keyboard-interactive-before-password", "auth-error", "encrypted-key-ready"], encryptedKeys: [ { keyPath: "/Users/test/.ssh/id_ed25519", @@ -807,6 +909,13 @@ test("failed keyboard-interactive retry still offers encrypted default key fallb ], ); assert.equal(events.includes("passphrase-request"), true); + assert.deepEqual( + MockSSHClient.instances[2].authMethodsOffered.map((method) => ( + method && typeof method === "object" ? method.type : method + )), + ["none", "password", "publickey"], + ); + assert.equal(MockSSHClient.instances[2].authMethodsOffered[2].key, "UNLOCKED_PRIVATE_KEY"); assert.equal( sender.sent.some((message) => ( message.channel === "netcatty:exit" diff --git a/electron/bridges/sshBridge.cjs b/electron/bridges/sshBridge.cjs index 3058a8230..345945b51 100644 --- a/electron/bridges/sshBridge.cjs +++ b/electron/bridges/sshBridge.cjs @@ -406,7 +406,8 @@ let selectZmodemDownloadDirectory = null; // Key format: "username@hostname:port" // Value: { method: "password" | "publickey" | "publickey-default" } // Cache persists until auth failure, then cleared to retry all methods -const authMethodCache = new Map(); +const { createSshAuthMethodCache } = require("./sshAuthMethodCache.cjs"); +const authMethodCache = createSshAuthMethodCache(); // Per-session terminal encoding (default: utf-8) const sessionEncodings = new Map(); @@ -461,6 +462,7 @@ function clearCachedAuthMethod(username, hostname, port) { } const { safeSend } = require("./ipcUtils.cjs"); +const { openBoundedForwardOutCallback } = require("./boundedSshChannelOpen.cjs"); const { createConnectionRef, acquireConnectionRef, @@ -469,6 +471,13 @@ const { findReusableSession, findTransportByEndpoint, resolveTransportForReuse, + beginTransportDial, + waitForTransportDial, + completeTransportDial, + failTransportDial, + buildConnectionReuseEndpoint, + resolveConnectionKeepalivePolicy, + normalizeEndpoint, discardAllTransports, } = require("./sshConnectionPool.cjs"); @@ -899,7 +908,7 @@ async function connectThroughChain(event, options, jumpHosts, targetHost, target reject(new Error(errMsg)); }, forwardTimeoutMs); if (!options?._forwardTimeoutMs) timeout.unref?.(); - conn.forwardOut('127.0.0.1', 0, nextHost, nextPort, (err, stream) => { + openBoundedForwardOutCallback(conn, '127.0.0.1', 0, nextHost, nextPort, (err, stream) => { if (settled) { try { stream?.destroy?.(); } catch { } return; @@ -959,6 +968,10 @@ const startSessionApi = createStartSessionApi({ preparePrivateKeyForAuth, loadFirstIdentityFileForAuth, prepareSystemSshAgentForAuth, hasUserConfiguredKey, isPasswordProvided, createKeyboardInteractiveHandler, createOrderedStringAuthHandler, createAuthPhase, markAuthPhasePartialSuccess, canRepeatKeyboardInteractive, shouldSkipKiPasswordAutoFill, createConnectionRef, acquireConnectionRef, releaseConnectionRef, transferConnectionRef, findReusableSession, findTransportByEndpoint, resolveTransportForReuse, discardAllTransports, + beginTransportDial, waitForTransportDial, completeTransportDial, failTransportDial, + buildConnectionReuseEndpoint, + resolveConnectionKeepalivePolicy, + normalizeEndpoint, get probeReceiveConflicts() { return probeReceiveConflicts; }, get removeRemoteFiles() { return removeRemoteFiles; }, get restoreRemoteModes() { return restoreRemoteModes; }, @@ -1112,7 +1125,7 @@ function sendFinalStartFailureExit(event, options, err) { }); } -async function startSSHSessionWrapper(event, options) { +async function startSSHSessionWithRetries(event, options, pendingDialState) { let retryableEncryptedKeys = []; let loadedRetryableEncryptedKeys = false; let shouldSuppressInitialAuthExit = false; @@ -1142,6 +1155,8 @@ async function startSSHSessionWrapper(event, options) { return await startSSHSession(event, { ...options, _suppressPreShellAuthExit: shouldSuppressInitialAuthExit, + _pendingDialState: pendingDialState, + _deferPendingDialFailure: true, }); } catch (err) { const isAuthError = isStartAuthError(err); @@ -1156,6 +1171,8 @@ async function startSSHSessionWrapper(event, options) { ...options, _skipPasswordMethod: true, _suppressPreShellAuthExit: shouldSuppressInitialAuthExit, + _pendingDialState: pendingDialState, + _deferPendingDialFailure: true, }); } catch (retryErr) { const isRetryAuthError = isStartAuthError(retryErr); @@ -1206,6 +1223,8 @@ async function startSSHSessionWrapper(event, options) { return await startSSHSession(event, { ...options, _unlockedEncryptedKeys: passphraseResult.keys, + _pendingDialState: pendingDialState, + _deferPendingDialFailure: true, }); } catch (retryErr) { // Only purge cached passphrases if the error is specifically @@ -1277,6 +1296,18 @@ async function startSSHSessionWrapper(event, options) { } } +async function startSSHSessionWrapper(event, options) { + const pendingDialState = { coordination: null }; + try { + return await startSSHSessionWithRetries(event, options, pendingDialState); + } catch (err) { + if (pendingDialState.coordination) { + failTransportDial(pendingDialState.coordination, err); + } + throw err; + } +} + /** * Get current working directory from an active SSH session * This sends 'pwd' to the existing shell stream and captures the output diff --git a/electron/bridges/sshBridge.connectionReuse.test.cjs b/electron/bridges/sshBridge.connectionReuse.test.cjs index b5c7d0941..b45f14d0d 100644 --- a/electron/bridges/sshBridge.connectionReuse.test.cjs +++ b/electron/bridges/sshBridge.connectionReuse.test.cjs @@ -3,28 +3,54 @@ const assert = require("node:assert/strict"); const { EventEmitter } = require("node:events"); const Module = require("node:module"); -const { createConnectionRef, releaseConnectionRef } = require("./sshConnectionPool.cjs"); +const { + createConnectionRef, + releaseConnectionRef, + resetSshTransportRegistryForTests, +} = require("./sshConnectionPool.cjs"); // Load sshBridge with a mocked ssh2 module so we can observe whether a *new* // SSH client is constructed (a fresh connection) versus an existing connection // being reused for a new shell channel (issue #1204). -function loadBridgeWithMockedSsh2(t) { +function loadBridgeWithMockedSsh2(t, { connectReady = false } = {}) { const bridgePath = require.resolve("./sshBridge.cjs"); const authHelperPath = require.resolve("./sshAuthHelper.cjs"); const originalLoad = Module._load; let clientConstructCount = 0; class MockSSHClient extends EventEmitter { + constructor() { + super(); + this._sock = { + destroyed: false, + setTimeout() {}, + setNoDelay() {}, + }; + this._remoteVer = "OpenSSH_9.0"; + this.openedShells = []; + } connect() { clientConstructCount += 1; + if (connectReady) { + setImmediate(() => { + this.emit("connect"); + this.emit("handshake"); + this.emit("ready"); + }); + return; + } // We never want the reuse test to reach a real connect; if it does the // test asserts on clientConstructCount and fails clearly. setImmediate(() => this.emit("error", new Error("unexpected fresh connect"))); } end() {} destroy() {} - exec() {} - shell() {} + exec(_command, callback) { callback?.(new Error("exec unavailable")); } + shell(_pty, _options, callback) { + const stream = makeStream(); + this.openedShells.push(stream); + setImmediate(() => callback(null, stream)); + } } Module._load = function patchedLoad(request, parent, isMain) { @@ -50,6 +76,34 @@ function loadBridgeWithMockedSsh2(t) { return { bridge, getClientConstructCount: () => clientConstructCount }; } +test("simultaneous normal opens of the same host make one physical SSH dial", async (t) => { + resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 }); + t.after(() => resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 })); + const { bridge, getClientConstructCount } = loadBridgeWithMockedSsh2(t, { connectReady: true }); + const sessions = new Map(); + const start = registerStartHandler(bridge, sessions); + const options = { + hostname: "10.0.0.50", + username: "alice", + port: 22, + authMethod: "password", + password: "secret", + useSshAgent: false, + verifyHostKeys: false, + }; + + const [first, second] = await Promise.all([ + start({ sender: makeSender() }, { ...options, sessionId: "normal-1" }), + start({ sender: makeSender() }, { ...options, sessionId: "normal-2" }), + ]); + + assert.equal(first.sessionId, "normal-1"); + assert.equal(second.sessionId, "normal-2"); + assert.equal(getClientConstructCount(), 1); + assert.equal(sessions.get("normal-1").conn, sessions.get("normal-2").conn); + assert.equal(sessions.get("normal-1").connRef.count, 2); +}); + function makeSender() { return { id: 1, @@ -63,6 +117,14 @@ function getConnectionReuseFallbackEvents(sender) { return sender.sent.filter((m) => m.channel === "netcatty:connection-reuse:fallback"); } +test.beforeEach(() => { + resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 }); +}); + +test.afterEach(() => { + resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 }); +}); + // A fake ssh2 shell channel. function makeStream() { const stream = new EventEmitter(); @@ -485,9 +547,10 @@ test("closing the reused channel keeps the source connection alive", async (t) = assert.equal(sessions.has("copy"), false, "copy session cleaned up"); assert.ok(sessions.has("source"), "source session still alive"); - // Now releasing the source (last holder) ends the connection. - assert.equal(releaseConnectionRef(sessions.get("source")), true); - assert.equal(sourceConn.ended, 1); + // Last release parks the healthy connection for later reuse. + assert.equal(releaseConnectionRef(sessions.get("source")), false); + assert.equal(sourceConn.ended, 0); + assert.equal(connRef.state, "idle"); }); test("skips reuse for X11-forwarding hosts and connects fresh", async (t) => { @@ -552,9 +615,10 @@ test("source closed while reused shell is pending keeps the connection alive", a assert.equal(copy.connRef, connRef); assert.equal(connRef.count, 1, "count reflects exactly the one remaining channel"); - // Closing the copy (last holder) finally ends the connection. + // Closing the copy (last holder) parks the healthy connection. terminalBridge.closeSession({ sender: {} }, { sessionId: "copy" }); - assert.equal(conn.ended, 1); + assert.equal(conn.ended, 0); + assert.equal(connRef.state, "idle"); }); test("does not reuse when the source endpoint differs from the requested target", async (t) => { diff --git a/electron/bridges/sshBridge.execCommandPassphrase.test.cjs b/electron/bridges/sshBridge.execCommandPassphrase.test.cjs index aff7c3440..611c243b7 100644 --- a/electron/bridges/sshBridge.execCommandPassphrase.test.cjs +++ b/electron/bridges/sshBridge.execCommandPassphrase.test.cjs @@ -73,7 +73,7 @@ class SuccessfulSshClient extends EventEmitter { const stream = new EventEmitter(); stream.stderr = new EventEmitter(); callback(null, stream); - queueMicrotask(() => stream.stderr.emit("close", 0)); + queueMicrotask(() => stream.emit("close", 0)); } end() {} diff --git a/electron/bridges/sshBridge/execCommand.cjs b/electron/bridges/sshBridge/execCommand.cjs index 67c3613eb..639f45b56 100644 --- a/electron/bridges/sshBridge/execCommand.cjs +++ b/electron/bridges/sshBridge/execCommand.cjs @@ -1,5 +1,8 @@ /* eslint-disable no-undef */ const { runWhenProxyConnectionReady } = require("../proxyUtils.cjs"); +const { executeBoundedSshCommand } = require("../boundedSshExec.cjs"); + +const SSH_EXEC_COMMAND_MAX_OUTPUT_BYTES = 1024 * 1024; function createExecCommandApi(ctx) { with (ctx) { @@ -62,30 +65,21 @@ function createExecCommandApi(ctx) { return new Promise((resolve, reject) => { const conn = new SSHClient(); - let stdout = ""; - let stderr = ""; let settled = false; - let commandTimer = null; let authReadyTimer = null; + let commandController = null; const clearTimers = () => { - if (commandTimer) clearTimeout(commandTimer); if (authReadyTimer) clearTimeout(authReadyTimer); - commandTimer = null; authReadyTimer = null; }; const rejectConnection = (err) => { if (settled) return; settled = true; clearTimers(); + commandController?.abort?.(err); conn.end(); reject(err); }; - const startCommandTimer = () => { - commandTimer = setTimeout(() => { - rejectConnection(new Error("SSH exec timeout")); - }, commandTimeoutMs); - commandTimer.unref?.(); - }; conn .once("connect", () => { @@ -100,29 +94,23 @@ function createExecCommandApi(ctx) { .once("ready", () => { if (authReadyTimer) clearTimeout(authReadyTimer); authReadyTimer = null; - startCommandTimer(); - conn.exec(payload.command, (err, stream) => { - if (err) { - clearTimers(); - settled = true; - conn.end(); - return reject(err); - } - stream - .on("data", (data) => { - stdout += data.toString(); - }) - .stderr.on("data", (data) => { - stderr += data.toString(); - }) - .on("close", (code) => { - if (settled) return; - clearTimers(); - settled = true; - conn.end(); - resolve({ stdout, stderr, code: code ?? (stderr ? 1 : 0) }); - }); - }); + commandController = new AbortController(); + void executeBoundedSshCommand(conn, payload.command, { + signal: commandController.signal, + openingTimeoutMs: commandTimeoutMs, + runTimeoutMs: commandTimeoutMs, + maxOutputBytes: SSH_EXEC_COMMAND_MAX_OUTPUT_BYTES, + }).then((result) => { + if (settled) return; + settled = true; + clearTimers(); + conn.end(); + resolve({ + stdout: result.stdout, + stderr: result.stderr, + code: result.code ?? (result.stderr ? 1 : 0), + }); + }, rejectConnection); }) .on("error", (err) => { rejectConnection(err); @@ -132,13 +120,7 @@ function createExecCommandApi(ctx) { }) .once("end", () => { if (settled) return; - clearTimers(); - settled = true; - if (stderr || stdout) { - resolve({ stdout, stderr, code: 0 }); - } else { - reject(new Error("SSH connection closed unexpectedly")); - } + rejectConnection(new Error("SSH connection closed unexpectedly")); }); const connectOpts = { @@ -234,4 +216,4 @@ function createExecCommandApi(ctx) { } } -module.exports = { createExecCommandApi }; +module.exports = { createExecCommandApi, SSH_EXEC_COMMAND_MAX_OUTPUT_BYTES }; diff --git a/electron/bridges/sshBridge/sessionOps.cjs b/electron/bridges/sshBridge/sessionOps.cjs index 3c9c693f2..d253f8721 100644 --- a/electron/bridges/sshBridge/sessionOps.cjs +++ b/electron/bridges/sshBridge/sessionOps.cjs @@ -1,4 +1,5 @@ /* eslint-disable no-undef */ +const { executeBoundedSshCommand } = require("../boundedSshExec.cjs"); function decodeLsofFileName(value) { if (typeof value !== 'string') return null; // lsof's caret form is ambiguous: a BEL byte and the literal characters @@ -107,39 +108,16 @@ function createSessionOpsApi(ctx) { return { success: false, error: 'Session not found or not connected' }; } const command = "cat /etc/os-release 2>/dev/null || uname -a"; - return new Promise((resolve) => { - let settled = false; - const settle = (result) => { - if (settled) return; - settled = true; - clearTimeout(timer); - resolve(result); - }; - const timer = setTimeout(() => { - settle({ success: false, error: 'Timeout probing distro' }); - // Clean up the exec channel so it doesn't linger. - try { if (activeStream) activeStream.close(); } catch { /* ignore */ } - }, 5000); - let activeStream = null; - try { - session.conn.exec(command, (err, stream) => { - if (err) { - settle({ success: false, error: err.message || String(err) }); - return; - } - activeStream = stream; - let stdout = ''; - let stderr = ''; - stream.on('data', (chunk) => { stdout += chunk.toString(); }); - stream.stderr.on('data', (chunk) => { stderr += chunk.toString(); }); - stream.on('close', () => { - settle({ success: true, stdout, stderr }); - }); - }); - } catch (err) { - settle({ success: false, error: err?.message || String(err) }); - } - }); + try { + const { stdout, stderr } = await executeBoundedSshCommand(session.conn, command, { + openingTimeoutMs: 5000, + runTimeoutMs: 5000, + maxOutputBytes: 256 * 1024, + }); + return { success: true, stdout, stderr }; + } catch (err) { + return { success: false, error: err?.message || String(err) }; + } } async function readRemoteHistory(event, payload) { @@ -245,37 +223,16 @@ function createSessionOpsApi(ctx) { return { success: false, error: 'Session not found or not connected' }; } - return new Promise((resolve) => { - let settled = false; - let activeStream = null; - const settle = (result) => { - if (settled) return; - settled = true; - clearTimeout(timer); - resolve(result); - }; - const timer = setTimeout(() => { - settle({ success: false, error: 'Timeout reading remote history' }); - try { if (activeStream) activeStream.close(); } catch { /* ignore */ } - }, 8000); - try { - conn.exec(command, (err, stream) => { - if (err) { - settle({ success: false, error: err.message || String(err) }); - return; - } - activeStream = stream; - let stdout = ''; - stream.on('data', (chunk) => { stdout += chunk.toString('utf8'); }); - if (stream.stderr) stream.stderr.on('data', () => { /* swallow */ }); - stream.on('close', () => { - settle({ success: true, ...parse(stdout) }); - }); - }); - } catch (err) { - settle({ success: false, error: err?.message || String(err) }); - } - }); + try { + const { stdout } = await executeBoundedSshCommand(conn, command, { + openingTimeoutMs: 8000, + runTimeoutMs: 8000, + maxOutputBytes: 10 * 1024 * 1024, + }); + return { success: true, ...parse(stdout) }; + } catch (err) { + return { success: false, error: err?.message || String(err) }; + } } async function getSessionPwd(event, payload) { @@ -308,17 +265,11 @@ function createSessionOpsApi(ctx) { // so we find the shell as a sibling via $PPID. return new Promise((resolve) => { let settled = false; - let activeStream = null; const settle = (result) => { if (settled) return; settled = true; - clearTimeout(timer); resolve(result); }; - const timer = setTimeout(() => { - settle({ success: false, error: 'Timeout getting pwd' }); - try { activeStream?.close(); } catch { /* ignore */ } - }, 5000); // POSIX sh script that: // 1. Finds the user's interactive shell on the same SSH connection @@ -468,55 +419,46 @@ function createSessionOpsApi(ctx) { exit 1`; const cmd = `exec sh -c ${quoteShellArg(posixScript)}`; - try { - session.conn.exec(cmd, (err, stream) => { - if (settled) { - try { stream?.close(); } catch { /* ignore */ } - return; - } - if (err) { - log('[getSessionPwd] exec error:', err.message); - settle({ success: false, error: err.message }); - return; - } - activeStream = stream; - let out = ''; - let errOut = ''; - stream.on('data', (d) => { out += d.toString(); }); - stream.stderr?.on('data', (d) => { errOut += d.toString(); }); - stream.on('close', (code) => { - if (settled) return; - const rawPath = out.replace(/\r?\n$/, ''); + void executeBoundedSshCommand(session.conn, cmd, { + openingTimeoutMs: 5000, + runTimeoutMs: 5000, + maxOutputBytes: 256 * 1024, + setTimeoutFn: setTimeout, + clearTimeoutFn: clearTimeout, + }).then(({ stdout, stderr, code }) => { + const rawPath = stdout.replace(/\r?\n$/, ''); const lsofPrefix = 'NETCATTY_LSOF_CWD='; const path = rawPath.startsWith(lsofPrefix) ? decodeLsofFileName(rawPath.slice(lsofPrefix.length)) : rawPath; - const loginPidMatch = errOut.match(/(?:^|\n)NETCATTY_LOGIN_PID=(\d+)(?:\n|$)/); + const loginPidMatch = stderr.match(/(?:^|\n)NETCATTY_LOGIN_PID=(\d+)(?:\n|$)/); if (loginPidMatch) { session.shellPid = loginPidMatch[1]; } - log('[getSessionPwd]', { stdout: rawPath, stderr: errOut.trim(), exitCode: code }); + log('[getSessionPwd]', { stdout: rawPath, stderr: stderr.trim(), exitCode: code }); if (path && path.startsWith('/')) { settle({ success: true, cwd: path }); } else { settle({ success: false, error: 'Could not determine cwd' }); } - }); + }, (err) => { + log('[getSessionPwd] exec error:', err?.message || String(err)); + settle({ + success: false, + error: err?.code === "SSH_EXEC_OPEN_TIMEOUT" || err?.code === "SSH_EXEC_RUN_TIMEOUT" + ? "Timeout getting pwd" + : err?.message || String(err), }); - } catch (err) { - settle({ success: false, error: err?.message || String(err) }); - } + }); }); } // Resolve the directory the running `rz` writes to (its own cwd) and report // which of `names` already exist there. Returns { dir, existing } or null. - function probeReceiveConflicts(session, names) { - return new Promise((resolve) => { + async function probeReceiveConflicts(session, names) { if (!session || !session.conn || !Array.isArray(names) || names.length === 0) { - return resolve(null); + return null; } - const timer = setTimeout(() => resolve(null), 5000); const script = `SELF=$$ find_login_shell() { ps -e -o pid=,ppid=,tty=,comm= 2>/dev/null | awk -v pp="$1" -v self="$SELF" ' @@ -549,12 +491,12 @@ function createSessionOpsApi(ctx) { done`; const argv = names.map((n) => quoteShellArg(n)).join(" "); const cmd = `exec sh -c ${quoteShellArg(script)} sh ${argv}`; - session.conn.exec(cmd, (err, stream) => { - if (err) { clearTimeout(timer); return resolve(null); } - let out = ""; - stream.on("data", (d) => { out += d.toString(); }); - stream.on("close", () => { - clearTimeout(timer); + try { + const { stdout: out } = await executeBoundedSshCommand(session.conn, cmd, { + openingTimeoutMs: 5000, + runTimeoutMs: 5000, + maxOutputBytes: 1024 * 1024, + }); let dir = null; const existing = []; const modes = {}; for (const line of out.split("\n")) { const [tag, val, mode] = line.split("\t"); @@ -564,46 +506,40 @@ function createSessionOpsApi(ctx) { if (mode && /^[0-7]{3,4}$/.test(mode)) modes[val] = mode; } } - resolve(dir ? { dir, existing, modes } : null); - }); - }); - }); + return dir ? { dir, existing, modes } : null; + } catch { + return null; + } } // rm -f the given absolute remote paths (quoted; injection-safe). - function removeRemoteFiles(session, paths) { - return new Promise((resolve) => { - if (!session || !session.conn || !Array.isArray(paths) || paths.length === 0) return resolve(); + async function removeRemoteFiles(session, paths) { + if (!session || !session.conn || !Array.isArray(paths) || paths.length === 0) return; const argv = paths.map((p) => quoteShellArg(p)).join(" "); - const timer = setTimeout(resolve, 5000); - session.conn.exec(`exec sh -c 'rm -f -- "$@"' sh ${argv}`, (err, stream) => { - if (err) { clearTimeout(timer); return resolve(); } - stream.on("data", () => {}); stream.stderr?.on("data", () => {}); - stream.on("close", () => { clearTimeout(timer); resolve(); }); - }); - }); + await executeBoundedSshCommand( + session.conn, + `exec sh -c 'rm -f -- "$@"' sh ${argv}`, + { openingTimeoutMs: 5000, runTimeoutMs: 5000, maxOutputBytes: 64 * 1024 }, + ).catch(() => {}); } // chmod the given { path, mode } entries back to their captured permissions // (parameterized; injection-safe). Modes are validated octal before use. - function restoreRemoteModes(session, entries) { - return new Promise((resolve) => { - if (!session || !session.conn || !Array.isArray(entries) || entries.length === 0) return resolve(); + async function restoreRemoteModes(session, entries) { + if (!session || !session.conn || !Array.isArray(entries) || entries.length === 0) return; const args = []; for (const e of entries) { if (!e || !e.path || !/^[0-7]{3,4}$/.test(String(e.mode))) continue; args.push(quoteShellArg(String(e.mode))); args.push(quoteShellArg(e.path)); } - if (args.length === 0) return resolve(); - const timer = setTimeout(resolve, 5000); + if (args.length === 0) return; const script = 'while [ "$#" -ge 2 ]; do chmod "$1" "$2" 2>/dev/null; shift 2; done'; - session.conn.exec(`exec sh -c ${quoteShellArg(script)} sh ${args.join(" ")}`, (err, stream) => { - if (err) { clearTimeout(timer); return resolve(); } - stream.on("data", () => {}); stream.stderr?.on("data", () => {}); - stream.on("close", () => { clearTimeout(timer); resolve(); }); - }); - }); + await executeBoundedSshCommand( + session.conn, + `exec sh -c ${quoteShellArg(script)} sh ${args.join(" ")}`, + { openingTimeoutMs: 5000, runTimeoutMs: 5000, maxOutputBytes: 64 * 1024 }, + ).catch(() => {}); } /** @@ -630,20 +566,11 @@ function createSessionOpsApi(ctx) { return new Promise((resolve) => { let settled = false; - let streamRef = null; const resolveOnce = (result) => { if (settled) return; settled = true; - clearTimeout(timer); resolve(result); }; - const timer = setTimeout(() => { - try { - streamRef?.close?.(); - streamRef?.destroy?.(); - } catch {} - resolveOnce({ success: false, entries: [], error: 'Timeout listing directory' }); - }, 3000); // Emit a NUL-delimited stream from plain POSIX shell/find so we don't depend on // Python/Perl, while still preserving whitespace and newline characters in filenames. @@ -699,20 +626,14 @@ function createSessionOpsApi(ctx) { done ' sh '${safePrefix}' ${foldersOnly ? 1 : 0} ${maxEntries} {} + 2>/dev/null`; - session.conn.exec(cmd, (err, stream) => { - if (err) { - resolveOnce({ success: false, entries: [], error: err.message }); - return; - } - streamRef = stream; - const chunks = []; - let errOut = ''; - stream.on('data', (d) => { chunks.push(Buffer.from(d)); }); - stream.stderr?.on('data', (d) => { errOut += d.toString(); }); - stream.on('close', () => { + void executeBoundedSshCommand(session.conn, cmd, { + openingTimeoutMs: 3000, + runTimeoutMs: 3000, + maxOutputBytes: 1024 * 1024, + }).then(({ stdout, stderr: errOut }) => { if (settled) return; try { - const output = Buffer.concat(chunks); + const output = Buffer.from(stdout, 'utf8'); const entries = []; let fieldStart = 0; let pendingName = null; @@ -743,7 +664,8 @@ function createSessionOpsApi(ctx) { error: errOut.trim() || 'Failed to parse directory listing', }); } - }); + }, (err) => { + resolveOnce({ success: false, entries: [], error: err?.message || String(err) }); }); }); } @@ -928,58 +850,21 @@ function createSessionOpsApi(ctx) { ? Promise.resolve(measureTcpConnectLatency(tcpLatencyTarget)).catch(() => null) : Promise.resolve(null); return new Promise((resolve) => { - let activeStream = null; let settled = false; - let timeout = null; - const disposeStream = (stream) => { - if (!stream) return; - try { - if (typeof stream.close === 'function') stream.close(); - else if (typeof stream.destroy === 'function') stream.destroy(); - } catch { - try { stream.destroy?.(); } catch { /* ignore cleanup errors */ } - } - }; const settle = (result) => { if (settled) return false; settled = true; - if (timeout) clearTimeout(timeout); resolve(result); return true; }; - timeout = setTimeout(() => { - const stream = activeStream; - activeStream = null; - if (settle({ success: false, error: 'Timeout getting server stats' })) { - disposeStream(stream); - } - }, 10000); - - conn.exec(statsCommand, (err, stream) => { - if (settled) { - disposeStream(stream); - return; - } - if (err) { - settle({ success: false, error: err.message }); - return; - } - activeStream = stream; - - let stdout = ''; - let stderr = ''; - - stream.on('data', (data) => { - stdout += data.toString(); - }); - - stream.stderr.on('data', (data) => { - stderr += data.toString(); - }); - - stream.on('close', async () => { + void executeBoundedSshCommand(conn, statsCommand, { + openingTimeoutMs: 10000, + runTimeoutMs: 10000, + maxOutputBytes: 1024 * 1024, + setTimeoutFn: setTimeout, + clearTimeoutFn: clearTimeout, + }).then(async ({ stdout }) => { if (settled) return; - activeStream = null; const measuredLatency = await tcpLatencyPromise; if (settled) return; const latencyMs = Number.isFinite(measuredLatency) ? measuredLatency : null; @@ -1300,9 +1185,14 @@ function createSessionOpsApi(ctx) { loadAverage, }, }); + }, (error) => { + settle({ + success: false, + error: error?.code === "SSH_EXEC_OPEN_TIMEOUT" || error?.code === "SSH_EXEC_RUN_TIMEOUT" + ? "Timeout getting server stats" + : error?.message || String(error), + }); }); - - }); }); } diff --git a/electron/bridges/sshBridge/sessionOps.serverStats.test.cjs b/electron/bridges/sshBridge/sessionOps.serverStats.test.cjs index cb8077e35..657387997 100644 --- a/electron/bridges/sshBridge/sessionOps.serverStats.test.cjs +++ b/electron/bridges/sshBridge/sessionOps.serverStats.test.cjs @@ -4,6 +4,13 @@ const { spawnSync } = require("node:child_process"); const { EventEmitter } = require("node:events"); const { createSessionOpsApi } = require("./sessionOps.cjs"); +const { + borrowTransport, + createTransport, + findTransportByEndpoint, + getTransportStats, + resetSshTransportRegistryForTests, +} = require("../sshConnectionPool.cjs"); // A fake ssh2 exec stream that emits the canned stdout then closes. function fakeStream(stdout) { @@ -337,6 +344,60 @@ test("getServerStats closes a stats stream delivered after timeout", async () => assert.equal(closeCalls, 1); }); +test("three stats retries on an unresponsive exec open leave no pooled transport or channel callbacks", async () => { + resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 }); + const endpoint = { hostId: "stats-host", hostname: "wedged.example", username: "root" }; + const conn = new EventEmitter(); + conn._sock = { destroyed: false }; + conn.pendingChannelCallbacks = []; + conn.exec = (_command, callback) => { + if (conn._sock.destroyed) throw new Error("Not connected"); + conn.pendingChannelCallbacks.push(callback); + }; + conn.end = () => {}; + conn.destroy = () => { + if (conn._sock.destroyed) return; + conn._sock.destroyed = true; + conn.pendingChannelCallbacks.length = 0; + conn.emit("close"); + }; + const transport = createTransport({ conn, endpoint }); + borrowTransport(transport, { kind: "shell", holder: {} }); + + const timers = []; + const api = createSessionOpsApi({ + sessions: new Map([["sid", { type: "ssh", conn }]]), + Date, + setTimeout: (callback) => { + const timer = { callback, cleared: false, unref() {} }; + timers.push(timer); + return timer; + }, + clearTimeout: (timer) => { if (timer) timer.cleared = true; }, + Buffer, + }); + + const first = api.getServerStats({ sender: {} }, { sessionId: "sid" }); + const openingTimer = timers.find((timer) => !timer.cleared); + assert.ok(openingTimer); + openingTimer.callback(); + const results = [await first]; + results.push(await api.getServerStats({ sender: {} }, { sessionId: "sid" })); + results.push(await api.getServerStats({ sender: {} }, { sessionId: "sid" })); + + assert.ok(results.every((result) => result.success === false)); + assert.equal(conn.pendingChannelCallbacks.length, 0); + assert.equal(getTransportStats().transports, 0); + assert.equal(findTransportByEndpoint(endpoint), null); + + const replacement = new EventEmitter(); + replacement._sock = { destroyed: false }; + replacement.end = () => {}; + const replacementTransport = createTransport({ conn: replacement, endpoint }); + assert.equal(findTransportByEndpoint(endpoint), replacementTransport); + resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 }); +}); + test("getServerStats includes host identity, load average, and uptime", async () => { const sessions = new Map(); const session = { diff --git a/electron/bridges/sshBridge/startSession.cjs b/electron/bridges/sshBridge/startSession.cjs index f842e17bf..57754d0c6 100644 --- a/electron/bridges/sshBridge/startSession.cjs +++ b/electron/bridges/sshBridge/startSession.cjs @@ -15,6 +15,8 @@ const { } = require("../terminalInterruptDiagnostics.cjs"); const { runWhenProxyConnectionReady } = require("../proxyUtils.cjs"); const { getAttachHomeWebContentsId } = require("../terminalAttachRestore.cjs"); +const { executeBoundedSshCommand } = require("../boundedSshExec.cjs"); +const { openBoundedSshShellCallback } = require("../boundedSshChannelOpen.cjs"); const SSH_TCP_CONNECT_TIMEOUT_MS = 20000; const SSH_AUTH_READY_TIMEOUT_MS = 120000; @@ -135,7 +137,7 @@ function shouldPromoteCachedAuthMethod(authMethod, cachedMethod) { function createStartSessionApi(ctx) { with (ctx) { - const listInteractiveShellPids = (conn) => { + const listInteractiveShellPids = async (conn) => { if (!conn || typeof conn.exec !== "function") { return Promise.resolve({ available: false, pids: [] }); } @@ -170,49 +172,26 @@ ps_output=$(ps -e -o pid=,ppid=,tty=,comm= 2>/dev/null) || exit 69 } | awk '/^[0-9]+$/ && !seen[$1]++ { print $1 }' printf '%s\n' '${scanCompleteMarker}'`; - return new Promise((resolve) => { - let settled = false; - let activeStream = null; - const settle = (result) => { - if (settled) return; - settled = true; - clearTimeout(timer); - resolve(result); + try { + const result = await executeBoundedSshCommand( + conn, + `exec sh -c ${quoteShellArg(script)}`, + { + openingTimeoutMs: 1500, + runTimeoutMs: 1500, + maxOutputBytes: 1024 * 1024, + }, + ); + const lines = result.stdout.split(/\r?\n/); + const completed = lines.includes(scanCompleteMarker); + const available = completed && (result.code === null || result.code === 0); + return { + available, + pids: available ? lines.filter((value) => /^\d+$/.test(value)) : [], }; - const timer = setTimeout(() => { - try { activeStream?.close?.(); } catch { /* ignore */ } - settle({ available: false, pids: [] }); - }, 1500); - - try { - conn.exec(`exec sh -c ${quoteShellArg(script)}`, (err, stream) => { - if (err || !stream) { - settle({ available: false, pids: [] }); - return; - } - activeStream = stream; - let stdout = ""; - let exitStatus = null; - stream.on("data", (chunk) => { stdout += chunk.toString(); }); - stream.stderr?.on("data", () => {}); - stream.on("exit", (code) => { - if (typeof code === "number") exitStatus = code; - }); - stream.on("close", (code) => { - const effectiveExitStatus = typeof code === "number" ? code : exitStatus; - const lines = stdout.split(/\r?\n/); - const completed = lines.includes(scanCompleteMarker); - const available = completed && (effectiveExitStatus === null || effectiveExitStatus === 0); - settle({ - available, - pids: available ? lines.filter((value) => /^\d+$/.test(value)) : [], - }); - }); - }); - } catch { - settle({ available: false, pids: [] }); - } - }); + } catch { + return { available: false, pids: [] }; + } }; const waitForNewInteractiveShellPid = async (conn, previousPids) => { @@ -288,50 +267,9 @@ printf '%s\n' '${scanCompleteMarker}'`; // sure a "Copy Tab" reuse opens its channel on a connection going to the // *same* host — a saved host edited after the source connected must not // silently run commands on the old machine (issue #1204 review). - _reuseEndpoint: (() => { - // Build fingerprint with credential digests at connect time; do not - // retain password/privateKey on the session after normalize. - const { fingerprintAuth } = require("../sshConnectionPool.cjs"); - const authFields = { - authType: options.authType || options.authMethod || '', - keyId: options.keyId || options.identityId || '', - certificate: options.certificate || '', - requiresMfa: !!options.requiresMfa, - verifyHostKeys: options.verifyHostKeys, - useSshAgent: options.useSshAgent, - password: options.password, - privateKey: options.privateKey, - publicKey: options.publicKey, - passphrase: options.passphrase, - identityFilePaths: options.identityFilePaths, - }; - return { - // Profile identity first: vault hostId prevents cross-profile reuse - // when two hosts share hostname:port:user but differ in auth policy. - hostId: options.hostId || '', - hostname: options.hostname || '', - port: options.port || 22, - username: options.username || 'root', - // Include jump chain + proxy so SFTP/port-forward/parked lookup cannot - // match a different route to the same final host. - jumpHosts: Array.isArray(options.jumpHosts) ? options.jumpHosts : [], - proxy: options.proxy || null, - // Auth/security knobs so key rotation / MFA / host-key verify changes - // invalidate parked transports for the same hostId. - authType: authFields.authType, - keyId: authFields.keyId, - // Presence flag only — full cert material is digested into authFingerprint. - certificate: authFields.certificate ? 'cert' : '', - requiresMfa: authFields.requiresMfa, - verifyHostKeys: authFields.verifyHostKeys, - useSshAgent: authFields.useSshAgent, - // agentForward is negotiated at connection time; keep it in the - // transport fingerprint so reopening with ForwardAgent enabled does - // not reuse a parked conn that never enabled it. - agentForwarding: !!options.agentForwarding, - authFingerprint: fingerprintAuth(authFields), - }; - })(), + _reuseEndpoint: normalizeEndpoint(buildConnectionReuseEndpoint(options, { + agentForwarding: options._actualAgentForwarding ?? options.agentForwarding, + })), tcpLatencyDirect: !Array.isArray(options.jumpHosts) || options.jumpHosts.length === 0 ? !options.proxy @@ -747,7 +685,8 @@ printf '%s\n' '${scanCompleteMarker}'`; conn.once("error", onConnError); try { - conn.shell( + openBoundedSshShellCallback( + conn, { term: "xterm-256color", cols, @@ -878,28 +817,7 @@ printf '%s\n' '${scanCompleteMarker}'`; // cookie wired up at connection time, so a reused channel would not carry // X11. For X11 hosts we deliberately skip reuse and make a fresh // connection so the duplicate keeps working X11 forwarding. - const reuseEndpoint = { - hostId: options.hostId || "", - hostname: options.hostname, - port: options.port || 22, - username: options.username || "root", - jumpHosts: Array.isArray(options.jumpHosts) ? options.jumpHosts : [], - proxy: options.proxy || null, - authType: options.authType || options.authMethod || "", - keyId: options.keyId || options.identityId || "", - certificate: options.certificate || "", - requiresMfa: !!options.requiresMfa, - verifyHostKeys: options.verifyHostKeys, - useSshAgent: options.useSshAgent, - agentForwarding: !!options.agentForwarding, - // Credential material for digest-based auth fingerprint (not retained - // on the parked transport — only the digest is indexed). - password: options.password, - privateKey: options.privateKey, - publicKey: options.publicKey, - passphrase: options.passphrase, - identityFilePaths: options.identityFilePaths, - }; + const reuseEndpoint = buildConnectionReuseEndpoint(options); if (options.sourceSessionId && !options.x11Forwarding) { const sourceSession = findReusableSession(sessions, options.sourceSessionId, reuseEndpoint); @@ -962,6 +880,44 @@ printf '%s\n' '${scanCompleteMarker}'`; } } + // Atomically reserve the physical dial before any asynchronous key, + // proxy, or jump-host preparation. A second terminal/SFTP/forward open + // for the same compatible endpoint can wait for this leader and then + // open its own channel on the authenticated transport. + let pendingDialCoordination = options._pendingDialState?.coordination || null; + if (!pendingDialCoordination && !options.x11Forwarding && typeof beginTransportDial === "function") { + const coordination = beginTransportDial(reuseEndpoint, { kind: "shell" }); + if (coordination.role === "reuse" || coordination.role === "join") { + try { + const transport = coordination.role === "reuse" + ? coordination.transport + : await waitForTransportDial(coordination); + return await reuseShellSession( + event, + options, + { conn: transport.conn, connRef: transport, stream: {} }, + sessionId, + log, + ); + } catch (coordinationErr) { + // A waiter must observe the leader's real failure instead of + // immediately starting a second authentication prompt. Existing + // transport reuse keeps its historical fresh-dial fallback. + if (coordination.role === "join") throw coordinationErr; + log("coordinated transport reuse failed, connecting fresh", { + sessionId, + hostname: options.hostname, + error: coordinationErr?.message, + }); + } + } else { + pendingDialCoordination = coordination; + if (options._pendingDialState) { + options._pendingDialState.coordination = coordination; + } + } + } + const cols = options.cols || 80; const rows = options.rows || 24; @@ -995,6 +951,7 @@ printf '%s\n' '${scanCompleteMarker}'`; const totalHops = jumpHosts.length + 1; // +1 for final target // Build base connection options for final target + const keepalivePolicy = resolveConnectionKeepalivePolicy(options); const connectOpts = { host: options.hostname, port: options.port || 22, @@ -1008,8 +965,8 @@ printf '%s\n' '${scanCompleteMarker}'`; // Resolved keepalive (caller decides whether host override or global // applies). interval is in seconds; 0 means truly disabled, so // countMax also goes to 0 to skip ssh2's dead-connection check. - keepaliveInterval: options.keepaliveInterval > 0 ? options.keepaliveInterval * 1000 : 0, - keepaliveCountMax: options.keepaliveInterval > 0 ? (options.keepaliveCountMax ?? 10) : 0, + keepaliveInterval: keepalivePolicy.keepaliveIntervalMs, + keepaliveCountMax: keepalivePolicy.keepaliveCountMax, // Enable keyboard-interactive authentication (required for 2FA/MFA) tryKeyboard: true, algorithms: buildAlgorithms(options.legacyAlgorithms, { @@ -1800,7 +1757,8 @@ printf '%s\n' '${scanCompleteMarker}'`; }; } - conn.shell( + openBoundedSshShellCallback( + conn, { term: "xterm-256color", cols, @@ -1832,7 +1790,10 @@ printf '%s\n' '${scanCompleteMarker}'`; const ownerSession = setupShellSession({ conn, stream, - options, + options: { + ...options, + _actualAgentForwarding: Boolean(connectOpts.agentForward), + }, sessionId, event, log, @@ -1842,6 +1803,9 @@ printf '%s\n' '${scanCompleteMarker}'`; }); establishedOwnerSession = ownerSession; connRef = createConnectionRef(ownerSession, conn, chainConnections); + if (pendingDialCoordination) { + completeTransportDial(pendingDialCoordination, connRef); + } // Capture this connection's log stream token in the closure so // the connection-level handlers below stop the right stream even // after a same-sessionId reconnect (#916). @@ -2131,8 +2095,16 @@ printf '%s\n' '${scanCompleteMarker}'`; : undefined, }); conn.connect(connectOpts); + }).catch((err) => { + if (pendingDialCoordination && !options._deferPendingDialFailure) { + failTransportDial(pendingDialCoordination, err); + } + throw err; }); } catch (err) { + if (pendingDialCoordination && !options._deferPendingDialFailure) { + failTransportDial(pendingDialCoordination, err); + } console.error("[Chain] SSH chain connection error:", err.message); const isAuthError = isSshAuthFailure(err); const suppressPreShellAuthExit = Boolean(options._suppressPreShellAuthExit && isAuthError); diff --git a/electron/bridges/sshConnectionPool.cjs b/electron/bridges/sshConnectionPool.cjs index 0be397bfa..32d3c836d 100644 --- a/electron/bridges/sshConnectionPool.cjs +++ b/electron/bridges/sshConnectionPool.cjs @@ -27,6 +27,9 @@ */ const { randomUUID, createHash } = require("node:crypto"); +const fs = require("node:fs"); +const os = require("node:os"); +const path = require("node:path"); /** * Default idle park after last lease returns (5 minutes). @@ -35,6 +38,12 @@ const { randomUUID, createHash } = require("node:crypto"); */ const DEFAULT_SSH_TRANSPORT_IDLE_TTL_MS = 5 * 60_000; +/** + * Global safety bound for authenticated transports with no active leases. + * Active transports never count toward this limit and are never evicted. + */ +const DEFAULT_MAX_IDLE_SSH_TRANSPORTS = 128; + /** Storage key mirrored in infrastructure/config/storageKeys.ts (main + renderer). */ const STORAGE_KEY_SSH_TRANSPORT_IDLE_TTL_MS = "netcatty_ssh_transport_idle_ttl_ms_v1"; @@ -51,8 +60,13 @@ const transportsById = new Map(); const transportIdsByEndpoint = new Map(); /** @type {Map} leaseId -> entry */ const leasesById = new Map(); +/** @type {Map>} endpointKey -> pending physical dials */ +const pendingDialsByEndpoint = new Map(); +/** @type {Map} oldest -> newest idle transport */ +const idleTransportsLru = new Map(); let defaultIdleTtlMs = resolveEnvIdleTtlMs(DEFAULT_SSH_TRANSPORT_IDLE_TTL_MS); +let maxIdleTransports = DEFAULT_MAX_IDLE_SSH_TRANSPORTS; let timerApi = { setTimeout: (...args) => setTimeout(...args), clearTimeout: (...args) => clearTimeout(...args), @@ -60,6 +74,156 @@ let timerApi = { let nowFn = () => Date.now(); let nextLeaseSeq = 0; +function removePendingDial(record) { + if (!record?.endpointKey) return; + const records = pendingDialsByEndpoint.get(record.endpointKey); + if (!records) return; + records.delete(record); + if (records.size === 0) pendingDialsByEndpoint.delete(record.endpointKey); +} + +/** + * Atomically choose whether an opener should reuse, join, or lead a physical + * SSH dial. Keeping this registry beside the authenticated transport index + * closes the gap where two callers both observe "no transport" and connect. + */ +function beginTransportDial(endpoint, opts = {}) { + const kind = opts?.kind === "shell" ? "shell" : "channel"; + const reusable = findTransportByEndpoint(endpoint, { kind }); + if (reusable) { + return { role: "reuse", transport: reusable, endpoint, kind }; + } + + const normalizedEndpoint = normalizeEndpoint(endpoint); + const endpointKey = buildEndpointKey(normalizedEndpoint); + if (!endpointKey) { + throw new Error("Cannot coordinate SSH dial without a valid endpoint"); + } + + const pending = pendingDialsByEndpoint.get(endpointKey); + if (pending) { + for (const record of pending) { + if (record.settled) continue; + // Reuse is evaluated from the waiter's requirements against the leader's + // negotiated policy. This preserves ForwardAgent's asymmetric channel + // rule and exact shell rule while the connection is still being opened. + if (endpointAllowsReuse(normalizedEndpoint, record.endpoint, kind)) { + return { + role: "join", + promise: record.promise, + endpoint: normalizedEndpoint, + kind, + _record: record, + }; + } + } + } + + let resolvePromise; + let rejectPromise; + const promise = new Promise((resolve, reject) => { + resolvePromise = resolve; + rejectPromise = reject; + }); + // A leader may fail before any waiter joins. Mark the rejection observed + // without changing what actual waiters receive from the original promise. + promise.catch(() => {}); + const record = { + endpoint: normalizedEndpoint, + endpointKey, + kind, + promise, + resolve: resolvePromise, + reject: rejectPromise, + settled: false, + }; + let records = pendingDialsByEndpoint.get(endpointKey); + if (!records) { + records = new Set(); + pendingDialsByEndpoint.set(endpointKey, records); + } + records.add(record); + return { + role: "leader", + promise, + endpoint: normalizedEndpoint, + kind, + _record: record, + }; +} + +function waitForTransportDial(coordination, opts = {}) { + if (coordination?.role === "reuse") { + return Promise.resolve(coordination.transport); + } + const promise = coordination?.promise; + if (!promise || typeof promise.then !== "function") { + return Promise.reject(new Error("Invalid SSH dial coordination handle")); + } + const validateTransport = (transport) => { + if (!endpointAllowsReuse(coordination.endpoint, transport?.endpoint, coordination.kind)) { + throw new Error("SSH dial completed with an incompatible endpoint"); + } + return transport; + }; + const signal = opts?.signal; + if (!signal) return promise.then(validateTransport); + if (signal.aborted) { + return Promise.reject(signal.reason || new Error("SSH connection wait cancelled")); + } + return new Promise((resolve, reject) => { + const onAbort = () => { + cleanup(); + reject(signal.reason || new Error("SSH connection wait cancelled")); + }; + const cleanup = () => signal.removeEventListener("abort", onAbort); + signal.addEventListener("abort", onAbort, { once: true }); + promise.then( + (transport) => { + cleanup(); + try { + resolve(validateTransport(transport)); + } catch (error) { + reject(error); + } + }, + (err) => { cleanup(); reject(err); }, + ); + }); +} + +function completeTransportDial(coordination, transport) { + const record = coordination?._record; + if (coordination?.role !== "leader" || !record || record.settled) return false; + if (!transport || !isTransportSocketHealthy(transport)) { + return failTransportDial(coordination, new Error("SSH dial completed without a healthy transport")); + } + record.settled = true; + removePendingDial(record); + record.resolve(transport); + return true; +} + +function failTransportDial(coordination, error) { + const record = coordination?._record; + if (coordination?.role !== "leader" || !record || record.settled) return false; + record.settled = true; + removePendingDial(record); + record.reject(error instanceof Error ? error : new Error(String(error || "SSH dial failed"))); + return true; +} + +function failAllPendingDials(reason = "SSH connection pool reset") { + const records = []; + for (const set of pendingDialsByEndpoint.values()) records.push(...set); + for (const record of records) { + failTransportDial( + { role: "leader", _record: record }, + new Error(reason), + ); + } +} + function resolveEnvIdleTtlMs(fallback) { const raw = process.env.NETCATTY_SSH_TRANSPORT_IDLE_TTL_MS; if (raw === undefined || raw === "") return fallback; @@ -68,33 +232,137 @@ function resolveEnvIdleTtlMs(fallback) { return n; } +function stableSerialize(value, seen = new WeakSet()) { + if (value === null) return "null"; + if (value === undefined) return "undefined"; + if (Buffer.isBuffer(value)) return `buffer:${value.toString("base64")}`; + const type = typeof value; + if (type === "string" || type === "boolean") return JSON.stringify(value); + if (type === "number") return Number.isFinite(value) ? String(value) : JSON.stringify(String(value)); + if (type !== "object") return JSON.stringify(String(value)); + if (seen.has(value)) return '"[circular]"'; + seen.add(value); + let result; + if (Array.isArray(value)) { + result = `[${value.map((item) => stableSerialize(item, seen)).join(",")}]`; + } else { + result = `{${Object.keys(value).sort().map((key) => ( + `${JSON.stringify(key)}:${stableSerialize(value[key], seen)}` + )).join(",")}}`; + } + seen.delete(value); + return result; +} + +function secureDigest(value) { + return createHash("sha256").update(stableSerialize(value)).digest("hex"); +} + +function expandIdentityFilePath(filePath) { + if (typeof filePath !== "string" || !filePath.trim()) return ""; + return filePath.trim() + .replace(/^~(?=$|[\\/])/, os.homedir()) + .replace(/\$\{([A-Za-z_][A-Za-z0-9_]*)\}/g, (_match, name) => process.env[name] ?? "") + .replace(/\$([A-Za-z_][A-Za-z0-9_]*)/g, (_match, name) => process.env[name] ?? ""); +} + +function fingerprintIdentityFiles(identityFilePaths) { + const paths = Array.isArray(identityFilePaths) + ? identityFilePaths + : (identityFilePaths ? [identityFilePaths] : []); + return secureDigest(paths.map((filePath) => { + const expanded = expandIdentityFilePath(filePath); + if (!expanded) return { path: String(filePath || ""), state: "invalid" }; + try { + return { + path: path.normalize(expanded), + content: createHash("sha256").update(fs.readFileSync(expanded)).digest("hex"), + }; + } catch (err) { + return { + path: path.normalize(expanded), + state: err?.code || "unreadable", + }; + } + })); +} + function fingerprintProxy(proxy) { if (!proxy || typeof proxy !== "object") return "-"; const type = proxy.type || proxy.proxyType || proxy.mode || ""; const host = proxy.host || proxy.hostname || proxy.server || ""; const port = proxy.port || ""; - const user = proxy.username || proxy.user || ""; - // Command/ProxyCommand proxies often have empty host/port — include the - // command string so edits invalidate warm-transport reuse. const command = proxy.command || proxy.proxyCommand || proxy.cmd || ""; if (!type && !host && !port && !command) return "-"; - return `${type}:${host}:${port}:${user}:${command}`; + // The full resolved proxy object includes authentication and identity + // selection. Only the digest is retained in the transport registry. + return secureDigest(proxy); } function fingerprintJumpHosts(jumpHosts) { if (!Array.isArray(jumpHosts) || jumpHosts.length === 0) return "-"; - return jumpHosts.map((h) => { - if (typeof h === "string") return h; - // Include vault id AND effective endpoint/proxy so editing a jump host's - // hostname/port/user/proxy while keeping the same id still invalidates reuse. - const id = h?.hostId || h?.id || ""; - const host = h?.hostname || h?.host || ""; - const port = h?.port || 22; - const user = h?.username || "root"; - const hopProxy = fingerprintProxy(h?.proxy || h?.proxyConfig || null); - const ep = `${host}:${port}:${user}:${hopProxy}`; - return id ? `id:${id}|${ep}` : ep; - }).join(">"); + return secureDigest(jumpHosts.map((hop) => { + if (!hop || typeof hop !== "object") return hop; + return { + ...hop, + identityFileContentFingerprint: fingerprintIdentityFiles(hop.identityFilePaths), + proxyFingerprint: fingerprintProxy(hop.proxy || hop.proxyConfig || null), + }; + })); +} + +function parseKnownHostTarget(value) { + const first = String(value || "").trim().split(",")[0]; + if (!first) return null; + const bracketed = first.match(/^\[([^\]]+)\]:(\d+)$/); + if (bracketed) { + return { + hostname: bracketed[1].trim().toLowerCase(), + port: Number.parseInt(bracketed[2], 10), + }; + } + return { hostname: first.toLowerCase(), port: 22 }; +} + +/** + * Digest only the known-host records that can authorize this target or one of + * its SSH jump hosts. The registry retains only the digest, never public-key + * text or the full known-hosts vault. + */ +function fingerprintKnownHostTrust(endpoint) { + if (!endpoint || endpoint.verifyHostKeys === false) return "disabled"; + const targets = []; + const addTarget = (hostname, port, verifyHostKeys = true) => { + if (verifyHostKeys === false) return; + const normalizedHostname = String(hostname || "").trim().toLowerCase(); + if (!normalizedHostname) return; + targets.push(`${normalizedHostname}|${Number(port) || 22}`); + }; + addTarget(endpoint.hostname, endpoint.port, endpoint.verifyHostKeys); + for (const hop of Array.isArray(endpoint.jumpHosts) ? endpoint.jumpHosts : []) { + addTarget( + hop?.hostname, + hop?.port, + hop?.verifyHostKeys ?? endpoint.verifyHostKeys, + ); + } + const targetSet = new Set(targets); + const relevant = []; + for (const knownHost of Array.isArray(endpoint.knownHosts) ? endpoint.knownHosts : []) { + const parsed = parseKnownHostTarget(knownHost?.hostname); + if (!parsed || parsed.hostname === "(hashed)") continue; + const port = Number.isFinite(knownHost?.port) ? Number(knownHost.port) : parsed.port; + if (!targetSet.has(`${parsed.hostname}|${port || 22}`)) continue; + relevant.push({ + hostname: parsed.hostname, + port: port || 22, + keyType: String(knownHost?.keyType || ""), + fingerprint: String(knownHost?.fingerprint || ""), + publicKeyDigest: secureDigest(String(knownHost?.publicKey || "")), + }); + } + relevant.sort((a, b) => stableSerialize(a).localeCompare(stableSerialize(b))); + return secureDigest(relevant); } /** @@ -115,6 +383,7 @@ function digestAuthMaterial(endpoint) { const identityPaths = Array.isArray(endpoint.identityFilePaths) ? endpoint.identityFilePaths.join("\n") : (endpoint.identityFilePaths || ""); + const identityFileContentFingerprint = fingerprintIdentityFiles(endpoint.identityFilePaths); if (method === "password") { h.update(String(endpoint.password || "")); @@ -130,27 +399,30 @@ function digestAuthMaterial(endpoint) { h.update(String(endpoint.passphrase || "")); h.update("\0"); h.update(String(identityPaths)); + h.update("\0"); + h.update(identityFileContentFingerprint); } else { // auto / agent: prefer publicKey / identity paths as rotation signals. // Do not always fold password + privateKey together — transfer key-first // strips password and would miss a multi-material parked transport. h.update(String(endpoint.publicKey || "")); h.update("\0"); + if (endpoint.privateKey) { + h.update("private-key:"); + h.update(String(endpoint.privateKey)); + h.update("\0"); + } h.update(endpoint.certificate ? "cert" : "nocert"); h.update("\0"); h.update(String(identityPaths)); + h.update("\0"); + h.update(identityFileContentFingerprint); // Password-only auto (no key material): include password so vault password // rotation invalidates parked reuse. When any key material is present, // omit password so key-first opens still match. if (!endpoint.privateKey && !endpoint.publicKey && !identityPaths) { h.update("\0pw:"); h.update(String(endpoint.password || "")); - } else if (endpoint.privateKey && !endpoint.publicKey && !identityPaths) { - // Key material present only as privateKey (common) — digest it so key - // rotation invalidates; password-retry that strips privateKey will miss - // park and dial dedicated (acceptable). - h.update("\0pk:"); - h.update(String(endpoint.privateKey)); } } return h.digest("hex").slice(0, 16); @@ -163,12 +435,91 @@ function fingerprintAuth(endpoint) { // the same hostId/route (key rotation, MFA toggle, verifyHostKeys, etc.). const authType = endpoint.authType || endpoint.authMethod || ""; const keyId = endpoint.keyId || endpoint.identityId || ""; - const cert = endpoint.certificate ? "cert" : "nocert"; - const mfa = endpoint.requiresMfa ? "mfa" : "nomfa"; - const verify = endpoint.verifyHostKeys === false ? "noverify" : "verify"; - const agent = endpoint.useSshAgent === false ? "noagent" : (endpoint.useSshAgent ? "agent" : "agentauto"); const material = digestAuthMaterial(endpoint); - return [authType, keyId, cert, mfa, verify, agent, material].join(":"); + return secureDigest({ + authType, + keyId, + material, + certificate: endpoint.certificate ? secureDigest(endpoint.certificate) : "none", + requiresMfa: Boolean(endpoint.requiresMfa), + verifyHostKeys: endpoint.verifyHostKeys !== false, + knownHostTrust: fingerprintKnownHostTrust(endpoint), + authPolicyVersion: endpoint.authPolicyVersion ?? null, + useSshAgent: endpoint.useSshAgent ?? null, + identityAgent: endpoint.identityAgent ?? null, + identitiesOnly: endpoint.identitiesOnly ?? null, + addKeysToAgent: endpoint.addKeysToAgent ?? null, + useKeychain: endpoint.useKeychain ?? null, + agentPublicKeys: endpoint.agentPublicKeys ?? null, + legacyAlgorithms: endpoint.legacyAlgorithms ?? null, + skipEcdsaHostKey: endpoint.skipEcdsaHostKey ?? null, + algorithmOverrides: endpoint.algorithmOverrides ?? null, + }); +} + +function resolveConnectionKeepalivePolicy(options = {}) { + const configuredInterval = options.keepaliveInterval; + const intervalSeconds = configuredInterval == null + ? 10 + : Number(configuredInterval); + const keepaliveIntervalMs = Number.isFinite(intervalSeconds) && intervalSeconds > 0 + ? intervalSeconds * 1000 + : 0; + const configuredCount = configuredInterval == null + ? 3 + : options.keepaliveCountMax; + const count = Number(configuredCount ?? 3); + return { + keepaliveIntervalMs, + keepaliveCountMax: keepaliveIntervalMs > 0 && Number.isFinite(count) && count >= 0 + ? count + : 0, + }; +} + +/** + * Build the common terminal/SFTP endpoint description consumed by the pool. + * Raw credentials exist only while the caller computes the normalized hashes; + * beginTransportDial/createTransport retain the normalized, digest-only form. + */ +function buildConnectionReuseEndpoint(options = {}, overrides = {}) { + const keepalive = resolveConnectionKeepalivePolicy({ + keepaliveInterval: overrides.keepaliveInterval ?? options.keepaliveInterval, + keepaliveCountMax: overrides.keepaliveCountMax ?? options.keepaliveCountMax, + }); + return { + hostId: options.hostId || "", + hostname: options.hostname, + port: options.port || 22, + username: options.username || "root", + protocol: options.protocol || "ssh", + sftpSudo: overrides.sftpSudo ?? Boolean(options.sftpSudo || options.sudo), + jumpHosts: Array.isArray(options.jumpHosts) ? options.jumpHosts : [], + proxy: options.proxy || null, + authType: options.authType || options.authMethod || "", + authPolicyVersion: options.authPolicyVersion, + keyId: options.keyId || options.identityId || "", + certificate: options.certificate || "", + requiresMfa: Boolean(options.requiresMfa), + verifyHostKeys: options.verifyHostKeys, + knownHosts: options.knownHosts, + useSshAgent: options.useSshAgent, + identityAgent: options.identityAgent, + identitiesOnly: options.identitiesOnly, + addKeysToAgent: options.addKeysToAgent, + useKeychain: options.useKeychain, + agentPublicKeys: options.agentPublicKeys, + agentForwarding: Boolean(overrides.agentForwarding ?? options.agentForwarding), + ...keepalive, + password: options.password, + privateKey: options.privateKey, + publicKey: options.publicKey, + passphrase: options.passphrase, + identityFilePaths: options.identityFilePaths, + legacyAlgorithms: options.legacyAlgorithms, + skipEcdsaHostKey: options.skipEcdsaHostKey, + algorithmOverrides: options.algorithmOverrides, + }; } function normalizeEndpoint(endpoint) { @@ -181,6 +532,12 @@ function normalizeEndpoint(endpoint) { const hostId = endpoint.hostId != null && String(endpoint.hostId).trim() ? String(endpoint.hostId).trim() : ""; + const keepalive = endpoint.keepaliveIntervalMs != null + ? { + keepaliveIntervalMs: Number(endpoint.keepaliveIntervalMs) || 0, + keepaliveCountMax: Number(endpoint.keepaliveCountMax) || 0, + } + : resolveConnectionKeepalivePolicy(endpoint); return { hostId, hostname, @@ -199,6 +556,7 @@ function normalizeEndpoint(endpoint) { // A transport opened with ForwardAgent can serve SFTP/PF; a transport // without it cannot satisfy a later shell open that needs agentForwarding. agentForwarding: Boolean(endpoint.agentForwarding), + ...keepalive, }; } @@ -220,6 +578,7 @@ function buildEndpointKey(endpoint) { jump, proxy, auth, + `keepalive:${ep.keepaliveIntervalMs}:${ep.keepaliveCountMax}`, ].join("|"); } @@ -238,7 +597,9 @@ function sameEndpoint(a, b) { && left.sftpSudo === right.sftpSudo && left.jumpFingerprint === right.jumpFingerprint && left.proxyFingerprint === right.proxyFingerprint - && left.authFingerprint === right.authFingerprint; + && left.authFingerprint === right.authFingerprint + && left.keepaliveIntervalMs === right.keepaliveIntervalMs + && left.keepaliveCountMax === right.keepaliveCountMax; } /** @@ -283,8 +644,46 @@ function clearIdleTimer(transport) { transport.idleDeadlineAt = null; } +function removeIdleTransportLru(transport) { + if (!transport?.id) return; + idleTransportsLru.delete(transport.id); +} + +function touchIdleTransportLru(transport) { + if (!transport?.id || transport.state !== "idle" || transport.leases?.size !== 0) { + removeIdleTransportLru(transport); + return; + } + idleTransportsLru.delete(transport.id); + idleTransportsLru.set(transport.id, transport); +} + +function enforceIdleTransportLimit() { + for (const [id, transport] of idleTransportsLru) { + if ( + transportsById.get(id) !== transport + || transport.state !== "idle" + || transport.leases?.size !== 0 + ) { + idleTransportsLru.delete(id); + } + } + + while (idleTransportsLru.size > maxIdleTransports) { + const oldest = idleTransportsLru.values().next().value; + if (!oldest) break; + idleTransportsLru.delete(oldest.id); + // Only zero-lease idle transports are eligible. If state changed between + // selection and eviction, leave the active transport alone. + if (oldest.state === "idle" && oldest.leases?.size === 0) { + endTransport(oldest, "idle-cap"); + } + } +} + function unregisterTransport(transport) { if (!transport) return; + removeIdleTransportLru(transport); transportsById.delete(transport.id); if (transport.endpointKey) { const set = transportIdsByEndpoint.get(transport.endpointKey); @@ -295,12 +694,41 @@ function unregisterTransport(transport) { } } -function endTransport(transport, reason = "end") { +function detachTransportLifecycle(transport) { + const conn = transport?.conn; + if (!conn?.removeListener) return; + if (transport._poolOnConnectionClose) { + try { conn.removeListener("close", transport._poolOnConnectionClose); } catch { /* ignore */ } + } + if (transport._poolOnConnectionError) { + try { conn.removeListener("error", transport._poolOnConnectionError); } catch { /* ignore */ } + } + transport._poolOnConnectionClose = null; + transport._poolOnConnectionError = null; +} + +function attachTransportLifecycle(transport) { + const conn = transport?.conn; + if (!conn?.once) return; + const onClose = () => { + endTransport(transport, "socket-close", { skipConnectionEnd: true }); + }; + const onError = () => { + endTransport(transport, "socket-error"); + }; + transport._poolOnConnectionClose = onClose; + transport._poolOnConnectionError = onError; + conn.once("close", onClose); + conn.once("error", onError); +} + +function endTransport(transport, reason = "end", opts = {}) { if (!transport) return false; if (transport.state === "dead" || transport.state === "closing") return false; transport.state = "closing"; clearIdleTimer(transport); + detachTransportLifecycle(transport); for (const leaseId of [...transport.leases.keys()]) { const entry = leasesById.get(leaseId); @@ -313,10 +741,12 @@ function endTransport(transport, reason = "end") { } transport.count = 0; - try { - transport.conn?.end(); - } catch { - /* connection may already be gone */ + if (!opts.skipConnectionEnd) { + try { + transport.conn?.end(); + } catch { + /* connection may already be gone */ + } } const chain = Array.isArray(transport.chainConnections) ? transport.chainConnections : []; for (const c of chain) { @@ -337,9 +767,10 @@ function endTransport(transport, reason = "end") { /** * Park a transport until TTL elapses (or forever when TTL is 0). * @param {object} transport - * @param {{ preserveIdleSince?: boolean }} [opts] + * @param {{ preserveIdleSince?: boolean, preserveLru?: boolean }} [opts] * preserveIdleSince: when rescheduling after a settings change, keep the * original idle start so remaining lifetime is not extended. + * preserveLru: keep the existing idle recency while only changing its timer. */ function scheduleIdleEnd(transport, opts = {}) { clearIdleTimer(transport); @@ -356,6 +787,14 @@ function scheduleIdleEnd(transport, opts = {}) { transport.idleSince = now; } + if (!opts.preserveLru || !idleTransportsLru.has(transport.id)) { + touchIdleTransportLru(transport); + } + enforceIdleTransportLimit(); + if (transport.state === "dead" || transport.state === "closing") { + return { ended: true, idle: false }; + } + // 0 (or negative/non-finite): park until quit/discard — matches settings // "Until app quit" and OpenSSH ControlPersist yes. if (!Number.isFinite(ttl) || ttl <= 0) { @@ -375,12 +814,16 @@ function scheduleIdleEnd(transport, opts = {}) { if (transport.state !== "idle" || transport.leases.size > 0) return; endTransport(transport, "idle-ttl"); }, remaining); + // Idle reuse is an optimization, not a reason to keep the app/test process + // alive after every consumer has gone away. + transport.idleTimer?.unref?.(); return { ended: false, idle: true }; } function wakeFromIdle(transport) { if (transport.state !== "idle") return; + removeIdleTransportLru(transport); clearIdleTimer(transport); transport.state = "live"; transport.idleSince = null; @@ -436,10 +879,13 @@ function createTransport({ createdAt: nowFn(), meta: meta || null, endedReason: null, + _poolOnConnectionClose: null, + _poolOnConnectionError: null, }; transportsById.set(transport.id, transport); attachEndpointIndex(transport); + attachTransportLifecycle(transport); return transport; } @@ -600,6 +1046,7 @@ function discardTransport(transportOrId, reason = "discard") { } function discardAllTransports(reason = "discard-all") { + failAllPendingDials(reason); let n = 0; for (const transport of [...transportsById.values()]) { if (endTransport(transport, reason)) n += 1; @@ -610,6 +1057,7 @@ function discardAllTransports(reason = "discard-all") { function findTransportById(id) { const transport = transportsById.get(id); if (!transport || !isTransportSocketHealthy(transport)) return null; + if (transport.state === "idle") touchIdleTransportLru(transport); return transport; } @@ -658,7 +1106,9 @@ function findTransportByEndpoint(endpoint, opts = {}) { if (a.state !== b.state) return a.state === "live" ? -1 : 1; return a.leases.size - b.leases.size; }); - return candidates[0]; + const selected = candidates[0]; + if (selected.state === "idle") touchIdleTransportLru(selected); + return selected; } function getTransportStats() { @@ -672,6 +1122,8 @@ function getTransportStats() { } return { transports: transportsById.size, + pendingDials: [...pendingDialsByEndpoint.values()] + .reduce((total, records) => total + records.size, 0), live, idle, leases, @@ -691,7 +1143,7 @@ function setDefaultTransportIdleTtlMs(ms) { for (const transport of transportsById.values()) { transport.idleTtlMs = defaultIdleTtlMs; if (transport.state === "idle" && transport.leases.size === 0) { - scheduleIdleEnd(transport, { preserveIdleSince: true }); + scheduleIdleEnd(transport, { preserveIdleSince: true, preserveLru: true }); } } return defaultIdleTtlMs; @@ -709,10 +1161,16 @@ function resetSshTransportRegistryForTests(options = {}) { transportsById.clear(); transportIdsByEndpoint.clear(); leasesById.clear(); + pendingDialsByEndpoint.clear(); + idleTransportsLru.clear(); nextLeaseSeq = 0; defaultIdleTtlMs = Number.isFinite(options.defaultIdleTtlMs) ? options.defaultIdleTtlMs : resolveEnvIdleTtlMs(DEFAULT_SSH_TRANSPORT_IDLE_TTL_MS); + maxIdleTransports = Number.isFinite(options.maxIdleTransports) + && options.maxIdleTransports >= 0 + ? Math.floor(options.maxIdleTransports) + : DEFAULT_MAX_IDLE_SSH_TRANSPORTS; timerApi = { setTimeout: options.setTimeout || ((...args) => setTimeout(...args)), clearTimeout: options.clearTimeout || ((...args) => clearTimeout(...args)), @@ -755,6 +1213,8 @@ function createConnectionRef(session, conn, chainConnections) { verifyHostKeys: session._reuseEndpoint.verifyHostKeys, useSshAgent: session._reuseEndpoint.useSshAgent, agentForwarding: session._reuseEndpoint.agentForwarding, + keepaliveIntervalMs: session._reuseEndpoint.keepaliveIntervalMs, + keepaliveCountMax: session._reuseEndpoint.keepaliveCountMax, authFingerprint: session._reuseEndpoint.authFingerprint, } : null; @@ -808,7 +1268,8 @@ function acquireConnectionRef(session, connRef) { * Release this session's hold on its shared connection. * * Decrements the lease count. When it reaches zero the transport enters idle - * park (or ends immediately when idle TTL is 0). The caller remains responsible + * park; TTL 0 keeps it parked until app quit, while a positive TTL reclaims it + * when the deadline expires. The caller remains responsible * for closing this session's own shell stream/channel; this only governs the * *shared* transport. * @@ -899,6 +1360,7 @@ function resolveTransportForReuse({ module.exports = { // Constants DEFAULT_SSH_TRANSPORT_IDLE_TTL_MS, + DEFAULT_MAX_IDLE_SSH_TRANSPORTS, STORAGE_KEY_SSH_TRANSPORT_IDLE_TTL_MS, LEASE_KINDS, // New registry API @@ -910,10 +1372,16 @@ module.exports = { findTransportById, findTransportByEndpoint, resolveTransportForReuse, + beginTransportDial, + waitForTransportDial, + completeTransportDial, + failTransportDial, getTransportStats, setDefaultTransportIdleTtlMs, getDefaultTransportIdleTtlMs, buildEndpointKey, + buildConnectionReuseEndpoint, + resolveConnectionKeepalivePolicy, normalizeEndpoint, sameEndpoint, endpointAllowsReuse, diff --git a/electron/bridges/sshConnectionPool.test.cjs b/electron/bridges/sshConnectionPool.test.cjs index 5ccd8d33a..17320f1f3 100644 --- a/electron/bridges/sshConnectionPool.test.cjs +++ b/electron/bridges/sshConnectionPool.test.cjs @@ -1,5 +1,9 @@ const test = require("node:test"); const assert = require("node:assert/strict"); +const { EventEmitter } = require("node:events"); +const fs = require("node:fs"); +const os = require("node:os"); +const path = require("node:path"); const { createConnectionRef, @@ -14,14 +18,21 @@ const { discardAllTransports, findTransportByEndpoint, resolveTransportForReuse, + beginTransportDial, + waitForTransportDial, + completeTransportDial, + failTransportDial, getTransportStats, setDefaultTransportIdleTtlMs, getDefaultTransportIdleTtlMs, buildEndpointKey, + buildConnectionReuseEndpoint, + normalizeEndpoint, endpointAllowsReuse, fingerprintAuth, resetSshTransportRegistryForTests, DEFAULT_SSH_TRANSPORT_IDLE_TTL_MS, + DEFAULT_MAX_IDLE_SSH_TRANSPORTS, LEASE_KINDS, } = require("./sshConnectionPool.cjs"); @@ -40,6 +51,17 @@ function makeChainConn() { }; } +function makeLifecycleConn({ emitCloseFromEnd = false } = {}) { + const conn = new EventEmitter(); + conn.ended = 0; + conn._sock = { destroyed: false }; + conn.end = () => { + conn.ended += 1; + if (emitCloseFromEnd) conn.emit("close"); + }; + return conn; +} + /** Product default: TTL 0 parks until quit (never auto-reclaim). */ function useParkForever() { resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 }); @@ -79,6 +101,150 @@ test.afterEach(() => { resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 }); }); +test("concurrent compatible opens share one in-flight physical dial", async () => { + const endpoint = { + hostId: "host-1", + hostname: "same.example", + username: "alice", + authType: "password", + password: "secret", + }; + let physicalDials = 0; + let releaseDial; + const dialGate = new Promise((resolve) => { releaseDial = resolve; }); + + const open = async () => { + const coordination = beginTransportDial(endpoint, { kind: "shell" }); + if (coordination.role === "reuse") return coordination.transport; + if (coordination.role === "join") return waitForTransportDial(coordination); + + physicalDials += 1; + await dialGate; + const transport = createTransport({ conn: makeConn(), endpoint }); + completeTransportDial(coordination, transport); + return transport; + }; + + const first = open(); + const second = open(); + assert.equal(physicalDials, 1, "only the leader may create the physical SSH connection"); + releaseDial(); + const [left, right] = await Promise.all([first, second]); + assert.equal(left, right); + + const sequential = beginTransportDial(endpoint, { kind: "shell" }); + assert.equal(sequential.role, "reuse"); + assert.equal(sequential.transport, left); +}); + +test("in-flight dials isolate endpoint, profile, credentials, and ForwardAgent policy", () => { + const base = { + hostId: "host-1", + hostname: "same.example", + username: "alice", + authType: "password", + password: "secret", + agentForwarding: false, + }; + const leader = beginTransportDial(base, { kind: "shell" }); + assert.equal(leader.role, "leader"); + assert.equal(beginTransportDial({ ...base }, { kind: "shell" }).role, "join"); + + assert.equal( + beginTransportDial({ ...base, hostname: "other.example" }, { kind: "shell" }).role, + "leader", + ); + assert.equal( + beginTransportDial({ ...base, hostId: "host-2" }, { kind: "shell" }).role, + "leader", + ); + assert.equal( + beginTransportDial({ ...base, password: "rotated" }, { kind: "shell" }).role, + "leader", + ); + assert.equal( + beginTransportDial({ ...base, agentForwarding: true }, { kind: "shell" }).role, + "leader", + ); +}); + +test("channel opens can join a ForwardAgent leader but not the reverse", () => { + const base = { + hostId: "host-1", + hostname: "same.example", + username: "alice", + }; + const forwardAgentLeader = beginTransportDial( + { ...base, agentForwarding: true }, + { kind: "shell" }, + ); + assert.equal(forwardAgentLeader.role, "leader"); + assert.equal( + beginTransportDial({ ...base, agentForwarding: false }, { kind: "channel" }).role, + "join", + ); + + resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 }); + const noForwardAgentLeader = beginTransportDial( + { ...base, agentForwarding: false }, + { kind: "channel" }, + ); + assert.equal(noForwardAgentLeader.role, "leader"); + assert.equal( + beginTransportDial({ ...base, agentForwarding: true }, { kind: "shell" }).role, + "leader", + ); +}); + +test("a channel waiter can use a leader transport that negotiated ForwardAgent off", async () => { + const requestedByLeader = { + hostId: "host-agent-downgrade", + hostname: "agent-downgrade.example", + username: "alice", + agentForwarding: true, + }; + const leader = beginTransportDial(requestedByLeader, { kind: "shell" }); + const channelWaiter = beginTransportDial({ + ...requestedByLeader, + agentForwarding: false, + }, { kind: "channel" }); + assert.equal(channelWaiter.role, "join"); + + const transport = createTransport({ + conn: makeConn(), + endpoint: { ...requestedByLeader, agentForwarding: false }, + }); + assert.equal(completeTransportDial(leader, transport), true); + assert.equal(await waitForTransportDial(channelWaiter), transport); + await assert.rejects(waitForTransportDial(leader), /incompatible/i); +}); + +test("cancelled in-flight waiter does not cancel the shared physical dial", async () => { + const endpoint = { hostname: "same.example", username: "alice" }; + const leader = beginTransportDial(endpoint, { kind: "shell" }); + const joined = beginTransportDial(endpoint, { kind: "shell" }); + const controller = new AbortController(); + const waiting = waitForTransportDial(joined, { signal: controller.signal }); + controller.abort(new Error("caller cancelled")); + await assert.rejects(waiting, /caller cancelled/); + + const transport = createTransport({ conn: makeConn(), endpoint }); + assert.equal(completeTransportDial(leader, transport), true); + assert.equal(beginTransportDial(endpoint, { kind: "shell" }).transport, transport); +}); + +test("failed in-flight dial releases the slot so a later open can retry", async () => { + const endpoint = { hostname: "retry.example", username: "alice" }; + const leader = beginTransportDial(endpoint, { kind: "shell" }); + const joined = beginTransportDial(endpoint, { kind: "shell" }); + const waiting = waitForTransportDial(joined); + assert.equal(failTransportDial(leader, new Error("authentication failed")), true); + await assert.rejects(waiting, /authentication failed/); + + const retry = beginTransportDial(endpoint, { kind: "shell" }); + assert.equal(retry.role, "leader"); +}); + test("releaseConnectionRef parks on last channel when TTL is 0 (until quit)", () => { const conn = makeConn(); const chain = [makeChainConn(), makeChainConn()]; @@ -246,6 +412,115 @@ test("findReusableSession refuses reuse when the source has no recorded endpoint test("default idle TTL constant is 5 minutes", () => { assert.equal(DEFAULT_SSH_TRANSPORT_IDLE_TTL_MS, 5 * 60_000); + assert.equal(DEFAULT_MAX_IDLE_SSH_TRANSPORTS, 128); +}); + +test("TTL-zero idle pool enforces a hard global limit without ending active transports", () => { + resetSshTransportRegistryForTests({ + defaultIdleTtlMs: 0, + maxIdleTransports: 3, + }); + + const activeConn = makeConn(); + const active = createTransport({ + conn: activeConn, + endpoint: { hostname: "active.example", username: "root" }, + }); + borrowTransport(active, { holder: {} }); + + const parked = []; + for (let index = 0; index < 5; index += 1) { + const conn = makeConn(); + const holder = {}; + const transport = createTransport({ + conn, + endpoint: { hostname: `idle-${index}.example`, username: "root" }, + }); + borrowTransport(transport, { holder }); + returnTransport(holder); + parked.push({ conn, transport }); + } + + assert.deepEqual(getTransportStats(), { + transports: 4, + pendingDials: 0, + live: 1, + idle: 3, + leases: 1, + defaultIdleTtlMs: 0, + }); + assert.equal(active.state, "live"); + assert.equal(activeConn.ended, 0, "a leased transport must never be evicted"); + assert.deepEqual(parked.map(({ conn }) => conn.ended), [1, 1, 0, 0, 0]); + assert.deepEqual(parked.map(({ transport }) => transport.state), [ + "dead", + "dead", + "idle", + "idle", + "idle", + ]); +}); + +test("idle endpoint hits refresh LRU order before the next eviction", () => { + resetSshTransportRegistryForTests({ + defaultIdleTtlMs: 0, + maxIdleTransports: 2, + }); + + const park = (hostname) => { + const endpoint = { hostname, username: "root" }; + const conn = makeConn(); + const holder = {}; + const transport = createTransport({ conn, endpoint }); + borrowTransport(transport, { holder }); + returnTransport(holder); + return { conn, endpoint, transport }; + }; + + const first = park("first.example"); + const second = park("second.example"); + assert.equal(findTransportByEndpoint(first.endpoint), first.transport); + const third = park("third.example"); + + assert.equal(first.conn.ended, 0); + assert.equal(second.conn.ended, 1, "the untouched oldest idle transport is evicted"); + assert.equal(third.conn.ended, 0); + assert.equal(findTransportByEndpoint(first.endpoint), first.transport); + assert.equal(findTransportByEndpoint(second.endpoint), null); + assert.equal(findTransportByEndpoint(third.endpoint), third.transport); +}); + +test("borrow and return make a previously idle transport the newest LRU entry", () => { + resetSshTransportRegistryForTests({ + defaultIdleTtlMs: 0, + maxIdleTransports: 2, + }); + + const park = (hostname) => { + const conn = makeConn(); + const holder = {}; + const transport = createTransport({ + conn, + endpoint: { hostname, username: "root" }, + }); + borrowTransport(transport, { holder }); + returnTransport(holder); + return { conn, transport }; + }; + + const first = park("first.example"); + const second = park("second.example"); + const borrowed = {}; + borrowTransport(first.transport, { holder: borrowed }); + const third = park("third.example"); + + assert.equal(second.conn.ended, 0, "capacity ignores the active borrowed transport"); + returnTransport(borrowed); + + assert.equal(first.conn.ended, 0); + assert.equal(second.conn.ended, 1, "returning the borrowed transport evicts the oldest idle one"); + assert.equal(third.conn.ended, 0); + assert.equal(first.transport.state, "idle"); }); test("buildEndpointKey normalizes port and username defaults", () => { @@ -331,6 +606,229 @@ test("fingerprintAuth changes when credential material rotates under same keyId" ); }); +test("endpoint key securely covers jump, proxy, agent, and SSH security policy", () => { + const base = buildConnectionReuseEndpoint({ + hostId: "target-id", + hostname: "target.example", + username: "alice", + authMethod: "key", + privateKey: "target-private-key", + passphrase: "target-passphrase", + useSshAgent: true, + identityAgent: "/run/user/1000/agent-a.sock", + identitiesOnly: true, + agentPublicKeys: ["ssh-ed25519 TARGET-A"], + verifyHostKeys: true, + legacyAlgorithms: false, + skipEcdsaHostKey: false, + algorithmOverrides: { kex: ["curve25519-sha256"] }, + proxy: { + type: "socks5", + host: "proxy.example", + port: 1080, + identityId: "proxy-identity-a", + username: "proxy-user", + password: "proxy-secret-a", + }, + jumpHosts: [{ + hostId: "jump-id", + hostname: "jump.example", + username: "jump-user", + authMethod: "password", + password: "jump-secret-a", + requiresMfa: true, + verifyHostKeys: true, + legacyAlgorithms: false, + skipEcdsaHostKey: false, + algorithmOverrides: { serverHostKey: ["ssh-ed25519"] }, + proxy: { + type: "http", + host: "jump-proxy.example", + port: 8080, + username: "jump-proxy-user", + password: "jump-proxy-secret-a", + }, + }], + }); + const baseKey = buildEndpointKey(base); + const variants = [ + { ...base, identityAgent: "/run/user/1000/agent-b.sock" }, + { ...base, identitiesOnly: false }, + { ...base, agentPublicKeys: ["ssh-ed25519 TARGET-B"] }, + { ...base, verifyHostKeys: false }, + { ...base, algorithmOverrides: { kex: ["diffie-hellman-group14-sha256"] } }, + { ...base, proxy: { ...base.proxy, password: "proxy-secret-b" } }, + { ...base, proxy: { ...base.proxy, identityId: "proxy-identity-b" } }, + { ...base, jumpHosts: [{ ...base.jumpHosts[0], password: "jump-secret-b" }] }, + { ...base, jumpHosts: [{ ...base.jumpHosts[0], verifyHostKeys: false }] }, + { ...base, jumpHosts: [{ + ...base.jumpHosts[0], + proxy: { ...base.jumpHosts[0].proxy, password: "jump-proxy-secret-b" }, + }] }, + ]; + + for (const variant of variants) { + assert.notEqual(buildEndpointKey(variant), baseKey); + } + for (const secret of [ + "target-private-key", + "target-passphrase", + "proxy-secret-a", + "jump-secret-a", + "jump-proxy-secret-a", + ]) { + assert.equal(baseKey.includes(secret), false, `endpoint key must not expose ${secret}`); + } +}); + +test("endpoint key changes when known-host trust content changes", () => { + const base = { + hostId: "known-host-profile", + hostname: "known-host.example", + port: 22, + username: "alice", + authMethod: "password", + password: "secret", + verifyHostKeys: true, + }; + const first = buildEndpointKey(buildConnectionReuseEndpoint({ + ...base, + knownHosts: [{ hostname: "known-host.example", port: 22, publicKey: "ssh-ed25519 AAAA_FIRST" }], + })); + const rotated = buildEndpointKey(buildConnectionReuseEndpoint({ + ...base, + knownHosts: [{ hostname: "known-host.example", port: 22, publicKey: "ssh-ed25519 AAAA_ROTATED" }], + })); + + assert.notEqual(rotated, first); + + const jumpFirst = buildEndpointKey(buildConnectionReuseEndpoint({ + ...base, + jumpHosts: [{ hostname: "bastion.example", port: 2222 }], + knownHosts: [{ + hostname: "[bastion.example]:2222", + port: 2222, + publicKey: "ssh-ed25519 AAAA_JUMP_FIRST", + }], + })); + const jumpRotatedEndpoint = buildConnectionReuseEndpoint({ + ...base, + jumpHosts: [{ hostname: "bastion.example", port: 2222 }], + knownHosts: [{ + hostname: "[bastion.example]:2222", + port: 2222, + publicKey: "ssh-ed25519 AAAA_JUMP_ROTATED", + }], + }); + assert.notEqual(buildEndpointKey(jumpRotatedEndpoint), jumpFirst); + assert.equal( + JSON.stringify(normalizeEndpoint(jumpRotatedEndpoint)).includes("AAAA_JUMP_ROTATED"), + false, + ); +}); + +test("endpoint key changes when an identity file changes in place", (t) => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-pool-key-")); + t.after(() => fs.rmSync(dir, { recursive: true, force: true })); + const identityPath = path.join(dir, "id_ed25519"); + fs.writeFileSync(identityPath, "first-private-key"); + const endpoint = buildConnectionReuseEndpoint({ + hostname: "identity-file.example", + username: "alice", + authMethod: "key", + identityFilePaths: [identityPath], + }); + const first = buildEndpointKey(endpoint); + + fs.writeFileSync(identityPath, "rotated-private-key"); + const second = buildEndpointKey(endpoint); + assert.notEqual(second, first); + assert.equal(first.includes("first-private-key"), false); + assert.equal(second.includes("rotated-private-key"), false); +}); + +test("auto-auth endpoint changes when the inline private key rotates", () => { + const base = { + hostId: "inline-key-profile", + hostname: "inline-key.example", + username: "alice", + authMethod: "auto", + publicKey: "stale-public-key-metadata", + }; + const first = buildEndpointKey(buildConnectionReuseEndpoint({ + ...base, + privateKey: "first-private-key", + })); + const rotated = buildEndpointKey(buildConnectionReuseEndpoint({ + ...base, + privateKey: "rotated-private-key", + })); + + assert.notEqual(rotated, first); + assert.equal(first.includes("first-private-key"), false); + assert.equal(rotated.includes("rotated-private-key"), false); +}); + +test("Terminal and SFTP aliases build the same reuse endpoint semantics", () => { + const options = { + hostId: "shared-profile", + hostname: "shared.example", + username: "alice", + authMethod: "certificate", + keyId: "key-1", + privateKey: "private", + publicKey: "public", + certificate: "certificate", + passphrase: "passphrase", + identityFilePaths: ["/keys/id"], + useSshAgent: true, + identityAgent: "SSH_AUTH_SOCK", + identitiesOnly: true, + addKeysToAgent: "confirm", + useKeychain: true, + agentPublicKeys: ["ssh-ed25519 SELECTED"], + verifyHostKeys: true, + legacyAlgorithms: true, + skipEcdsaHostKey: true, + algorithmOverrides: { hmac: ["hmac-sha2-256"] }, + jumpHosts: [{ hostname: "jump", password: "jump-password" }], + proxy: { type: "http", host: "proxy", port: 8080, password: "proxy-password" }, + }; + assert.equal( + buildEndpointKey(buildConnectionReuseEndpoint(options)), + buildEndpointKey(buildConnectionReuseEndpoint({ + ...options, + authType: options.authMethod, + authMethod: undefined, + })), + ); +}); + +test("connection reuse identity changes with the effective keepalive policy", () => { + const base = { hostname: "keepalive.example", username: "root", port: 22 }; + const enabled = buildConnectionReuseEndpoint({ + ...base, + keepaliveInterval: 30, + keepaliveCountMax: 10, + }); + const disabled = buildConnectionReuseEndpoint({ + ...base, + keepaliveInterval: 0, + keepaliveCountMax: 0, + }); + + assert.notEqual(buildEndpointKey(enabled), buildEndpointKey(disabled)); + assert.equal(endpointAllowsReuse(enabled, disabled, "shell"), false); +}); + +test("connection endpoint can record negotiated agent forwarding instead of the request", () => { + const endpoint = buildConnectionReuseEndpoint( + { hostname: "agent.example", username: "root", agentForwarding: true }, + { agentForwarding: false }, + ); + assert.equal(normalizeEndpoint(endpoint).agentForwarding, false); +}); + test("endpointAllowsReuse: shell exact vs channel asymmetric for agentForwarding", () => { const base = { hostname: "a.example", username: "root", port: 22 }; // Channel (default): request needs ForwardAgent, existing never enabled it -> reject. @@ -423,6 +921,24 @@ test("last return parks with positive TTL then ends when timer fires", () => { assert.equal(transport.state, "dead"); }); +test("idle reclaim timer does not keep the app process alive", () => { + let unrefCalls = 0; + resetSshTransportRegistryForTests({ + defaultIdleTtlMs: 60_000, + setTimeout: () => ({ + unref() { unrefCalls += 1; }, + }), + clearTimeout: () => {}, + }); + const transport = createTransport({ conn: makeConn() }); + const holder = {}; + borrowTransport(transport, { holder }); + + returnTransport(holder); + + assert.equal(unrefCalls, 1); +}); + test("TTL 0 parks forever without scheduling a timer", () => { const timers = []; resetSshTransportRegistryForTests({ @@ -563,6 +1079,54 @@ test("discardAllTransports clears the registry", () => { assert.equal(getTransportStats().transports, 0); }); +test("active transport error unregisters leases and closes its jump chain once", () => { + useParkForever(); + const conn = makeLifecycleConn({ emitCloseFromEnd: true }); + const chain = makeChainConn(); + const holder = {}; + const transport = createTransport({ + conn, + chainConnections: [chain], + endpoint: { hostname: "active-error.example", username: "alice" }, + }); + borrowTransport(transport, { holder, kind: "sftp" }); + + conn.emit("error", new Error("remote transport failed")); + + assert.equal(getTransportStats().transports, 0); + assert.equal(getTransportStats().leases, 0); + assert.equal(holder.connRef, null); + assert.equal(holder._sshTransportLeaseId, null); + assert.equal(conn.ended, 1); + assert.equal(chain.ended, 1); + assert.equal(transport.state, "dead"); +}); + +test("remote close unregisters a TTL-zero idle transport and its jump chain once", () => { + useParkForever(); + const conn = makeLifecycleConn(); + const chain = makeChainConn(); + const holder = {}; + const transport = createTransport({ + conn, + chainConnections: [chain], + endpoint: { hostname: "idle-close.example", username: "alice" }, + }); + borrowTransport(transport, { holder, kind: "shell" }); + returnTransport(holder); + assert.equal(getTransportStats().idle, 1); + + conn._sock.destroyed = true; + conn.emit("close"); + conn.emit("close"); + + assert.equal(getTransportStats().transports, 0); + assert.equal(getTransportStats().idle, 0); + assert.equal(conn.ended, 0, "a remote close must not call end on the closed socket again"); + assert.equal(chain.ended, 1); + assert.equal(transport.state, "dead"); +}); + test("setDefaultTransportIdleTtlMs updates default and reschedules idle transports", () => { const timers = []; resetSshTransportRegistryForTests({ diff --git a/electron/bridges/sshTransportInvalidation.cjs b/electron/bridges/sshTransportInvalidation.cjs new file mode 100644 index 000000000..0d83499c3 --- /dev/null +++ b/electron/bridges/sshTransportInvalidation.cjs @@ -0,0 +1,32 @@ +"use strict"; + +/** + * A timed-out SSH channel-open request cannot be cancelled through ssh2's + * public API. ssh2 retains the request callback in its channel manager until + * the server replies or the physical socket closes, so merely rejecting the + * caller would leak one pending channel per retry on a wedged transport. + * + * Invalidate the physical connection to make ssh2 run its channel-manager + * cleanup. The shared transport registry observes the resulting close event + * and removes the transport from every reuse index. + */ +function invalidateSshTransport(sshClient) { + if (!sshClient) return false; + try { sshClient.once?.("error", () => {}); } catch { /* ignore */ } + let attempted = false; + try { + if (typeof sshClient.end === "function") { + attempted = true; + sshClient.end(); + } + } catch { /* continue with hard destroy */ } + try { + if (typeof sshClient.destroy === "function") { + attempted = true; + sshClient.destroy(); + } + } catch { /* transport may already be closing */ } + return attempted; +} + +module.exports = { invalidateSshTransport }; diff --git a/electron/bridges/systemManager/execOnSession.cjs b/electron/bridges/systemManager/execOnSession.cjs index dc8a3d198..d0efa458b 100644 --- a/electron/bridges/systemManager/execOnSession.cjs +++ b/electron/bridges/systemManager/execOnSession.cjs @@ -1,6 +1,7 @@ /* eslint-disable no-undef */ const { isSshConnAlive, isTransportExecError } = require("./execConnHealth.cjs"); +const { executeBoundedSshCommand } = require("../boundedSshExec.cjs"); function createExecOnSessionApi(ctx) { with (ctx) { @@ -92,61 +93,34 @@ function createExecOnSessionApi(ctx) { } function execOnConnection(conn, command, timeoutMs, execOptions = {}) { - return new Promise((resolve) => { - let settled = false; - let activeStream = null; - const maxBuffer = normalizeExecMaxBuffer(execOptions.maxBuffer); - const settle = (result) => { - if (settled) return; - settled = true; - clearTimeout(timer); - resolve(result); - }; - const timer = setTimeout(() => { - settle({ success: false, error: "Command timeout" }); - try { if (activeStream) activeStream.close(); } catch { /* ignore */ } - }, timeoutMs); - - try { - conn.exec(command, (err, stream) => { - if (err) { - settle({ success: false, error: err.message || String(err) }); - return; - } - activeStream = stream; - let stdout = ""; - let stderr = ""; - const appendOutput = (streamName, current, chunk) => { - const next = current + chunk.toString(); - if (next.length > maxBuffer) { - settle({ - success: false, - error: `${streamName} maxBuffer exceeded`, - stdout: "", - stderr: "", - code: 1, - }); - try { stream.close(); } catch { /* ignore */ } - return current; - } - return next; - }; - stream.on("data", (chunk) => { stdout = appendOutput("stdout", stdout, chunk); }); - if (stream.stderr) { - stream.stderr.on("data", (chunk) => { stderr = appendOutput("stderr", stderr, chunk); }); - } - if (typeof execOptions.stdin === "string") { - stream.write(execOptions.stdin); - stream.end(); - } - stream.on("close", (code) => { - settle({ success: true, stdout, stderr, code: code ?? 0 }); - }); - }); - } catch (err) { - settle({ success: false, error: err?.message || String(err) }); - } - }); + const maxBuffer = normalizeExecMaxBuffer(execOptions.maxBuffer); + return executeBoundedSshCommand(conn, command, { + openingTimeoutMs: timeoutMs, + runTimeoutMs: timeoutMs, + maxOutputBytes: maxBuffer, + onStream(stream) { + if (typeof execOptions.stdin === "string") { + stream.write(execOptions.stdin); + stream.end(); + } + }, + }).then( + ({ stdout, stderr, code }) => ({ + success: true, + stdout, + stderr, + code: code ?? 0, + }), + (error) => ({ + success: false, + error: error?.code === "SSH_EXEC_OUTPUT_LIMIT" + ? "SSH command maxBuffer exceeded" + : error?.message || String(error), + stdout: "", + stderr: "", + code: 1, + }), + ); } async function execOnSshSession(session, sessionId, command, timeoutMs, event, execOptions = {}, allowCompanionRetry = true) { diff --git a/electron/bridges/systemManager/execOnSession.stdin.test.cjs b/electron/bridges/systemManager/execOnSession.stdin.test.cjs index 92d137ded..56b47ff85 100644 --- a/electron/bridges/systemManager/execOnSession.stdin.test.cjs +++ b/electron/bridges/systemManager/execOnSession.stdin.test.cjs @@ -81,3 +81,42 @@ test("execOnSession enforces maxBuffer for SSH streamed stdout", async () => { assert.equal(result.stdout, ""); assert.equal(closed, true); }); + +test("execOnSession closes an SSH stream that arrives after the open timeout", async () => { + let callback; + const conn = { exec(_command, next) { callback = next; } }; + const execApi = createExecOnSessionApi({ + sessions: { get: () => ({ conn, type: "ssh" }) }, + }); + + const result = await execApi.execOnSession(null, "s1", "pending", 5); + assert.equal(result.success, false); + assert.match(result.error, /open timed out/); + + const stream = new EventEmitter(); + stream.stderr = new EventEmitter(); + let closed = 0; + stream.close = () => { closed += 1; }; + callback(null, stream); + assert.ok(closed > 0); +}); + +test("execOnSession settles and releases listeners on SSH stream errors", async () => { + const stream = new EventEmitter(); + stream.stderr = new EventEmitter(); + stream.close = () => {}; + stream.destroy = () => {}; + const conn = { exec(_command, callback) { callback(null, stream); } }; + const execApi = createExecOnSessionApi({ + sessions: { get: () => ({ conn, type: "ssh" }) }, + }); + + const pending = execApi.execOnSession(null, "s1", "fail", 1000); + await new Promise((resolve) => setImmediate(resolve)); + stream.stderr.emit("error", new Error("remote stderr failed")); + const result = await pending; + assert.equal(result.success, false); + assert.match(result.error, /stderr failed/); + assert.equal(stream.listenerCount("data"), 0); + assert.equal(stream.stderr.listenerCount("data"), 0); +}); diff --git a/electron/bridges/tempDirBridge.cjs b/electron/bridges/tempDirBridge.cjs index 8dd1e75b3..a5c44d99f 100644 --- a/electron/bridges/tempDirBridge.cjs +++ b/electron/bridges/tempDirBridge.cjs @@ -787,6 +787,10 @@ function registerHandlers(ipcMain, shell, electronModule) { ipcMain.handle("netcatty:tempdir:getPath", () => { return getTempDir(); }); + + ipcMain.handle("netcatty:tempdir:createUploadPath", (_event, payload = {}) => ( + getTransferTempFilePath(payload.transferId, payload.fileName) + )); ipcMain.handle("netcatty:tempdir:open", async () => { const tempDir = getTempDir(); diff --git a/electron/bridges/terminalAttachRestore.cjs b/electron/bridges/terminalAttachRestore.cjs index d2ce26b58..58fe3782f 100644 --- a/electron/bridges/terminalAttachRestore.cjs +++ b/electron/bridges/terminalAttachRestore.cjs @@ -46,6 +46,14 @@ function releaseAttachPopupAuthorization(token) { if (token) attachPopupAuthorizations.delete(token); } +function releaseAttachedSessionState(sessionId) { + if (!sessionId) return; + pendingAttachRestores.delete(sessionId); + for (const [token, grant] of attachPopupAuthorizations) { + if (grant?.sessionId === sessionId) attachPopupAuthorizations.delete(token); + } +} + function setRestoreAttachedSessionOutput(fn) { restoreImpl = typeof fn === "function" ? fn : null; } @@ -123,6 +131,7 @@ module.exports = { markAttachPopupClosePrepared, isAttachPopupClosePrepared, releaseAttachPopupAuthorization, + releaseAttachedSessionState, setRestoreAttachedSessionOutput, restoreAttachedSessionOutput, retryPendingAttachedSessionOutputs, diff --git a/electron/bridges/terminalBridge.cjs b/electron/bridges/terminalBridge.cjs index cc754971b..52d623ecd 100644 --- a/electron/bridges/terminalBridge.cjs +++ b/electron/bridges/terminalBridge.cjs @@ -158,6 +158,7 @@ const attachHomeWebContentsIds = new Map(); const { markAttachPopupClosePrepared, retryPendingAttachedSessionOutput, + releaseAttachedSessionState, setRestoreAttachedSessionOutput, setAttachHomeLookup, setFanoutSessionExit, @@ -762,7 +763,13 @@ function findExecutable(name, opts = {}) { : process.env.PATH; const env = { ...process.env, PATH: pathOverride || "" }; const whereExe = path.join(process.env.SystemRoot || "C:\\Windows", "System32", "where.exe"); - const result = execFileSync(fs.existsSync(whereExe) ? whereExe : "where.exe", [name], { encoding: "utf8", env }); + const result = execFileSync(fs.existsSync(whereExe) ? whereExe : "where.exe", [name], { + encoding: "utf8", + env, + maxBuffer: 1024 * 1024, + timeout: 3_000, + windowsHide: true, + }); const candidates = result .split(/\r?\n/) .map((line) => line.trim()) @@ -1903,6 +1910,7 @@ function clearSessionPtyBuffer(event, payload) { */ function closeSession(event, payload) { const session = sessions.get(payload.sessionId); + releaseAttachedSessionState(payload.sessionId); if (!session) return; terminalFlowPauseArbiter.clearSession(payload.sessionId); session.closed = true; diff --git a/electron/bridges/terminalBridge.closeReusedSession.test.cjs b/electron/bridges/terminalBridge.closeReusedSession.test.cjs index 0e10d177a..9a724d41a 100644 --- a/electron/bridges/terminalBridge.closeReusedSession.test.cjs +++ b/electron/bridges/terminalBridge.closeReusedSession.test.cjs @@ -2,12 +2,25 @@ const test = require("node:test"); const assert = require("node:assert/strict"); const terminalBridge = require("./terminalBridge.cjs"); -const { createConnectionRef, acquireConnectionRef } = require("./sshConnectionPool.cjs"); +const { + createConnectionRef, + acquireConnectionRef, + discardTransport, + resetSshTransportRegistryForTests, +} = require("./sshConnectionPool.cjs"); // Verifies the terminalBridge close path respects connection multiplexing // (issue #1204): closing one tab that shares an SSH connection must only close -// that tab's channel, and only tear the shared transport down when the last -// tab is gone. +// that tab's channel. Once the last tab is gone, the transport is parked briefly +// so a later same-host open can reuse it without another authentication round. + +test.beforeEach(() => { + resetSshTransportRegistryForTests(); +}); + +test.afterEach(() => { + resetSshTransportRegistryForTests(); +}); function makeStream() { return { @@ -55,11 +68,14 @@ test("closing a reused SSH tab keeps the shared connection alive", () => { assert.equal(connRef.count, 1); assert.equal(sessions.has("copy"), false); - // Close the owner tab — now the last holder, so the connection is ended. + // Close the owner tab — now the last holder, so the connection is parked. terminalBridge.closeSession({ sender: {} }, { sessionId: "owner" }); assert.equal(owner.stream.closed, 1, "owner channel closed"); - assert.equal(conn.ended, 1, "connection ended once last channel closes"); + assert.equal(conn.ended, 0, "connection remains parked for same-host reuse"); assert.equal(connRef.count, 0); + assert.equal(connRef.state, "idle"); + assert.equal(discardTransport(connRef), true); + assert.equal(conn.ended, 1, "parked connection is still reclaimable"); }); test("closing the owner tab first does not strand the copy's connection", () => { @@ -80,8 +96,11 @@ test("closing the owner tab first does not strand the copy's connection", () => assert.equal(conn.ended, 0, "connection survives for the still-open copy"); assert.equal(connRef.count, 1); - // Closing the copy (now last) ends the connection. + // Closing the copy (now last) parks the connection for reuse. terminalBridge.closeSession({ sender: {} }, { sessionId: "copy" }); + assert.equal(conn.ended, 0); + assert.equal(connRef.state, "idle"); + assert.equal(discardTransport(connRef), true); assert.equal(conn.ended, 1); }); diff --git a/electron/bridges/terminalBridge.rebindOutput.test.cjs b/electron/bridges/terminalBridge.rebindOutput.test.cjs index d168b431f..32aad9111 100644 --- a/electron/bridges/terminalBridge.rebindOutput.test.cjs +++ b/electron/bridges/terminalBridge.rebindOutput.test.cjs @@ -529,6 +529,28 @@ test("failed attach restores retry when a main renderer becomes ready", async () assert.equal(calls, 2, "successful retry clears the pending restore"); }); +test("closing a terminal clears failed restore and popup authorization state", async () => { + const registry = require("./terminalAttachRestore.cjs"); + let calls = 0; + registry.setRestoreAttachedSessionOutput(() => { + calls += 1; + return { success: false, restored: false }; + }); + registry.registerAttachPopupAuthorization("closed-grant", "closed-session", 42); + await registry.restoreAttachedSessionOutput("closed-session"); + + registry.releaseAttachedSessionState("closed-session"); + const result = await registry.retryPendingAttachedSessionOutput("closed-session", 42); + + assert.equal(calls, 1); + assert.deepEqual(result, { success: true, restored: false }); + assert.equal( + registry.validateAttachPopupAuthorization("closed-grant", "closed-session", 42), + false, + ); + registry.setRestoreAttachedSessionOutput(null); +}); + test("a ready replacement main renderer becomes the explicit restore target", async () => { const registry = require("./terminalAttachRestore.cjs"); const targets = []; diff --git a/electron/bridges/terminalBridge/moshSession.cjs b/electron/bridges/terminalBridge/moshSession.cjs index b7534bdab..f085bd9e6 100644 --- a/electron/bridges/terminalBridge/moshSession.cjs +++ b/electron/bridges/terminalBridge/moshSession.cjs @@ -213,9 +213,10 @@ function createMoshSessionApi(ctx) { let lastError = null; for (const identity of identities) { try { - await execFileAsync("icacls.exe", [target, "/grant:r", `${identity}:F`], { windowsHide: true }); - await execFileAsync("icacls.exe", [target, "/inheritance:r"], { windowsHide: true }); - await execFileAsync("icacls.exe", [target, "/grant:r", `${identity}:F`], { windowsHide: true }); + const commandOptions = { timeout: 5_000, maxBuffer: 1024 * 1024, windowsHide: true }; + await execFileAsync("icacls.exe", [target, "/grant:r", `${identity}:F`], commandOptions); + await execFileAsync("icacls.exe", [target, "/inheritance:r"], commandOptions); + await execFileAsync("icacls.exe", [target, "/grant:r", `${identity}:F`], commandOptions); return true; } catch (err) { lastError = err; diff --git a/electron/bridges/terminalWorkerManager.cjs b/electron/bridges/terminalWorkerManager.cjs index 5ef3b26a4..624c16476 100644 --- a/electron/bridges/terminalWorkerManager.cjs +++ b/electron/bridges/terminalWorkerManager.cjs @@ -1,5 +1,7 @@ "use strict"; +const { releaseAttachedSessionState } = require("./terminalAttachRestore.cjs"); + const path = require("node:path"); const { randomUUID } = require("node:crypto"); const { @@ -9,6 +11,12 @@ const { const { TERMINAL_URGENT_INPUT_PORT_CHANNEL, } = require("./terminalUrgentInputChannel.cjs"); +const { + clearTerminalSessionPerformanceState, +} = require("./emitTerminalSessionData.cjs"); + +const DEFAULT_CLOSED_SESSION_TOMBSTONE_TTL_MS = 60_000; +const DEFAULT_MAX_CLOSED_SESSION_TOMBSTONES = 2_048; function isTerminalWorkerEnabled(options = {}) { const env = options.env || process.env; @@ -22,6 +30,10 @@ const TRANSFER_RENDERER_CHANNELS = new Set([ "netcatty:transfer:complete", "netcatty:transfer:cancelled", "netcatty:transfer:error", + "netcatty:compress:progress", + "netcatty:compress:complete", + "netcatty:compress:cancelled", + "netcatty:compress:error", ]); function shouldForwardWorkerRendererEvent(channel) { @@ -328,6 +340,7 @@ function createTerminalWorkerManager(options = {}) { const pendingOutputBytes = new Map(); const closedSessions = new Set(); const closedSessionSequences = new Map(); + const closedSessionTombstoneTimes = new Map(); let sessionLifecycleSequence = 0; const outputPortPending = new Map(); const outputPortReady = new Set(); @@ -346,6 +359,8 @@ function createTerminalWorkerManager(options = {}) { const terminalInterceptorWarningListeners = new Set(); const sessionOwnedListeners = new Set(); const sessionClosedListeners = new Set(); + const workerExitListeners = new Set(); + const workerRendererEventListeners = new Set(); const externalSessions = new Map(); const maxPendingOutputChunks = Number.isFinite(options.maxPendingOutputChunks) ? Math.max(0, Math.trunc(options.maxPendingOutputChunks)) @@ -354,6 +369,13 @@ function createTerminalWorkerManager(options = {}) { ? Math.max(0, Math.trunc(options.maxPendingOutputBytes)) : 2 * 1024 * 1024; const maxDroppedStateScanBytes = Math.max(256, options.maxDroppedStateScanBytes ?? 2048); + const now = typeof options.now === "function" ? options.now : Date.now; + const closedSessionTombstoneTtlMs = Number.isFinite(options.closedSessionTombstoneTtlMs) + ? Math.max(0, Number(options.closedSessionTombstoneTtlMs)) + : DEFAULT_CLOSED_SESSION_TOMBSTONE_TTL_MS; + const maxClosedSessionTombstones = Number.isFinite(options.maxClosedSessionTombstones) + ? Math.max(1, Math.floor(Number(options.maxClosedSessionTombstones))) + : DEFAULT_MAX_CLOSED_SESSION_TOMBSTONES; function rejectAllPending(error) { for (const { reject } of pending.values()) { @@ -817,16 +839,57 @@ function createTerminalWorkerManager(options = {}) { if (sessionId) attachHomeWebContentsIds.delete(sessionId); } + function deleteClosedSessionTombstone(sessionId) { + closedSessionTombstoneTimes.delete(sessionId); + closedSessions.delete(sessionId); + closedSessionSequences.delete(sessionId); + closedSessionGenerations.delete(sessionId); + } + + function canPruneClosedSessionTombstone(sessionId) { + return !pendingSessionStartSequences.has(sessionId) + && !sessionGenerations.has(sessionId) + && !workerSessionIds.has(sessionId) + && !sessionWebContentsIds.has(sessionId) + && !outputRoutePending.has(sessionId); + } + + function pruneClosedSessionTombstones() { + const currentTime = now(); + for (const [sessionId, closedAt] of closedSessionTombstoneTimes) { + if (currentTime - closedAt < closedSessionTombstoneTtlMs) continue; + if (!canPruneClosedSessionTombstone(sessionId)) continue; + deleteClosedSessionTombstone(sessionId); + } + if (closedSessionTombstoneTimes.size <= maxClosedSessionTombstones) return; + for (const sessionId of [...closedSessionTombstoneTimes.keys()]) { + if (closedSessionTombstoneTimes.size <= maxClosedSessionTombstones) break; + if (!canPruneClosedSessionTombstone(sessionId)) continue; + deleteClosedSessionTombstone(sessionId); + } + } + + function touchClosedSessionTombstone(sessionId) { + if (!sessionId) return; + closedSessionTombstoneTimes.delete(sessionId); + closedSessionTombstoneTimes.set(sessionId, now()); + } + + function updateClosedSessionSequence(sessionId) { + closedSessionSequences.set(sessionId, ++sessionLifecycleSequence); + touchClosedSessionTombstone(sessionId); + } + function markSessionClosed(sessionId) { if (!sessionId || closedSessions.has(sessionId)) return; closedSessions.add(sessionId); - closedSessionSequences.set(sessionId, ++sessionLifecycleSequence); + updateClosedSessionSequence(sessionId); } function cancelPendingSessionStart(sessionId) { if (!pendingSessionStartSequences.delete(sessionId)) return; if (closedSessions.has(sessionId)) { - closedSessionSequences.set(sessionId, ++sessionLifecycleSequence); + updateClosedSessionSequence(sessionId); } } @@ -849,6 +912,7 @@ function createTerminalWorkerManager(options = {}) { const previous = closedSessionGenerations.get(sessionId); if (previous == null || generation > previous) { closedSessionGenerations.set(sessionId, generation); + touchClosedSessionTombstone(sessionId); } } @@ -925,6 +989,9 @@ function createTerminalWorkerManager(options = {}) { sessionGenerations.delete(sessionId); supersedingSessionGenerations.delete(sessionId); clearAttachHome(sessionId); + clearTerminalSessionPerformanceState(sessionId); + releaseAttachedSessionState(sessionId); + pruneClosedSessionTombstones(); const worker = child; try { worker?.postMessage?.({ kind: "close-output-port", sessionId }); @@ -1143,6 +1210,7 @@ function createTerminalWorkerManager(options = {}) { closedSessionGenerations.delete(sessionId); closedSessions.delete(sessionId); closedSessionSequences.delete(sessionId); + closedSessionTombstoneTimes.delete(sessionId); workerSessionIds.add(sessionId); if (Number.isSafeInteger(message.sessionGeneration)) { sessionGenerations.set(sessionId, message.sessionGeneration); @@ -1246,6 +1314,9 @@ function createTerminalWorkerManager(options = {}) { return; } if (message.kind === "renderer-event") { + for (const listener of [...workerRendererEventListeners]) { + try { listener(message); } catch {} + } // Transfer lifecycle from the utilityProcess cannot use BrowserWindow // inside the worker. Fan global-center events from the main process so // progress survives SFTP panel hide/unmount. @@ -1451,6 +1522,9 @@ function createTerminalWorkerManager(options = {}) { ? cause : new Error(`Terminal worker exited${Number.isFinite(code) ? ` with code ${code}` : ""}`); const exitCode = Number.isFinite(code) ? code : 1; + for (const listener of [...workerExitListeners]) { + try { listener(error); } catch {} + } const affectedSessionIds = new Set([ ...sessionWebContentsIds.keys(), ...outputRoutePending.keys(), @@ -1463,7 +1537,7 @@ function createTerminalWorkerManager(options = {}) { const hasNewPendingLifecycle = hasPendingSessionLifecycle(sessionId); if (closedSessions.has(sessionId) && !hasNewPendingLifecycle) continue; if (closedSessions.has(sessionId)) { - closedSessionSequences.set(sessionId, ++sessionLifecycleSequence); + updateClosedSessionSequence(sessionId); } else { markSessionClosed(sessionId); } @@ -1492,6 +1566,7 @@ function createTerminalWorkerManager(options = {}) { closedSessionGenerations.clear(); supersedingSessionGenerations.clear(); attachHomeWebContentsIds.clear(); + pruneClosedSessionTombstones(); closeAllUrgentInputPorts(); rejectAllPending(error); terminalOutputChannel?.closeAll?.(); @@ -1541,6 +1616,7 @@ function createTerminalWorkerManager(options = {}) { } function request(channel, payload, optionsForRequest = {}) { + pruneClosedSessionTombstones(); if (channel === "netcatty:close:await" && payload?.sessionId && closedSessions.has(payload.sessionId) @@ -1676,6 +1752,7 @@ function createTerminalWorkerManager(options = {}) { } function send(channel, payload, optionsForSend = {}) { + pruneClosedSessionTombstones(); if (channel === "netcatty:close" && payload?.sessionId && closedSessions.has(payload.sessionId) @@ -1770,6 +1847,18 @@ function createTerminalWorkerManager(options = {}) { return Object.freeze({ dispose: () => sessionClosedListeners.delete(listener) }); } + function onWorkerExit(listener) { + if (typeof listener !== "function") throw new TypeError("Worker exit listener is required"); + workerExitListeners.add(listener); + return Object.freeze({ dispose: () => workerExitListeners.delete(listener) }); + } + + function onWorkerRendererEvent(listener) { + if (typeof listener !== "function") throw new TypeError("Worker renderer event listener is required"); + workerRendererEventListeners.add(listener); + return Object.freeze({ dispose: () => workerRendererEventListeners.delete(listener) }); + } + function stop() { if (!child) return; const current = child; @@ -1781,6 +1870,7 @@ function createTerminalWorkerManager(options = {}) { pendingOutputBytes.clear(); closedSessions.clear(); closedSessionSequences.clear(); + closedSessionTombstoneTimes.clear(); outputPortPending.clear(); outputPortReady.clear(); outputRoutePending.clear(); @@ -1797,6 +1887,8 @@ function createTerminalWorkerManager(options = {}) { terminalInterceptorWarningListeners.clear(); sessionOwnedListeners.clear(); sessionClosedListeners.clear(); + workerExitListeners.clear(); + workerRendererEventListeners.clear(); for (const external of externalSessions.values()) { for (const resolve of external.resumeWaiters.splice(0)) resolve(); } @@ -1824,6 +1916,8 @@ function createTerminalWorkerManager(options = {}) { onTerminalInterceptorWarning, onSessionOwned, onSessionClosed, + onWorkerExit, + onWorkerRendererEvent, hasOpenSession(sessionId) { return Boolean( sessionId @@ -1865,6 +1959,14 @@ function createTerminalWorkerManager(options = {}) { outputTaps.add(listener); return () => outputTaps.delete(listener); }, + _getClosedSessionTombstoneCountsForTests() { + pruneClosedSessionTombstones(); + return { + sessions: closedSessions.size, + sequences: closedSessionSequences.size, + generations: closedSessionGenerations.size, + }; + }, stop, }; } diff --git a/electron/bridges/terminalWorkerManager.test.cjs b/electron/bridges/terminalWorkerManager.test.cjs index fd999c2a8..9900db0a3 100644 --- a/electron/bridges/terminalWorkerManager.test.cjs +++ b/electron/bridges/terminalWorkerManager.test.cjs @@ -5,6 +5,7 @@ const test = require("node:test"); const { createTerminalWorkerManager, isTerminalWorkerEnabled, + shouldForwardWorkerRendererEvent, } = require("./terminalWorkerManager.cjs"); const { createTerminalWorkerRuntime } = require("../terminalWorker/runtime.cjs"); @@ -26,6 +27,17 @@ class FakeChild extends EventEmitter { } } +test("compressed transfer lifecycle uses only the global renderer channel", () => { + for (const channel of [ + "netcatty:compress:progress", + "netcatty:compress:complete", + "netcatty:compress:cancelled", + "netcatty:compress:error", + ]) { + assert.equal(shouldForwardWorkerRendererEvent(channel), false, channel); + } +}); + class LinkedRuntimeChild extends FakeChild { connectRuntime(registerBridges) { let workerMessageListener = null; @@ -124,6 +136,62 @@ test("request sends a worker command and resolves matching response", async () = assert.deepEqual(await promise, { sessionId: "local-1" }); }); +test("worker lifecycle observers receive renderer events and survive a worker replacement", async () => { + const children = [new FakeChild(), new FakeChild()]; + let forkIndex = 0; + const rendererEvents = []; + const exits = []; + const manager = createTerminalWorkerManager({ + utilityProcess: { fork: () => children[forkIndex++] }, + electronModule: { + webContents: { + fromId(id) { + return { id, send() {} }; + }, + }, + }, + workerScriptPath: "/worker.cjs", + }); + manager.onWorkerRendererEvent((message) => rendererEvents.push(message.payload)); + manager.onWorkerExit((error) => exits.push(error.message)); + + const firstRequest = manager.request("netcatty:test", {}); + children[0].emit("message", { + kind: "response", + requestId: children[0].messages[0].requestId, + result: { ok: true }, + }); + await firstRequest; + children[0].emit("message", { + kind: "renderer-event", + webContentsId: 7, + channel: "netcatty:portforward:status", + payload: { tunnelId: "pf-first", status: "active" }, + }); + children[0].emit("exit", 9); + + const secondRequest = manager.request("netcatty:test", {}); + children[1].emit("message", { + kind: "response", + requestId: children[1].messages[0].requestId, + result: { ok: true }, + }); + await secondRequest; + children[1].emit("message", { + kind: "renderer-event", + webContentsId: 7, + channel: "netcatty:portforward:status", + payload: { tunnelId: "pf-second", status: "active" }, + }); + + assert.deepEqual(rendererEvents, [ + { tunnelId: "pf-first", status: "active" }, + { tunnelId: "pf-second", status: "active" }, + ]); + assert.deepEqual(exits, ["Terminal worker exited with code 9"]); + manager.stop(); +}); + test("worker manager retains the stable host id for session-backed transfers", async () => { const child = new FakeChild(); const manager = createTerminalWorkerManager({ @@ -2953,6 +3021,87 @@ test("an old start response cannot reopen a session claimed by a later same-id s assert.deepEqual(routed, [{ sessionId: "session-1", data: "new-banner" }]); }); +test("closed-session tombstones are bounded without admitting recent stale output", async () => { + const child = new FakeChild(); + const tapped = []; + let now = 0; + const manager = createTerminalWorkerManager({ + utilityProcess: { fork: () => child }, + terminalOutputChannel: { openSession: () => true, closeSession() {} }, + electronModule: { + webContents: { + fromId(id) { + return { id, isDestroyed: () => false, send() {} }; + }, + }, + }, + workerScriptPath: "/worker.cjs", + now: () => now, + closedSessionTombstoneTtlMs: 100, + maxClosedSessionTombstones: 8, + }); + manager.addOutputTap((sessionId, data) => tapped.push({ sessionId, data })); + + for (let index = 0; index < 24; index += 1) { + const sessionId = `closed-${index}`; + const started = manager.request("netcatty:local:reconnect", { sessionId }, { webContentsId: 7 }); + const startRequest = [...child.messages].reverse().find((message) => ( + message.kind === "request" && message.payload?.sessionId === sessionId + )); + child.emit("message", { + kind: "response", + requestId: startRequest.requestId, + result: { sessionId }, + sessionGeneration: 0, + }); + await started; + manager.send("netcatty:close", { sessionId }, { webContentsId: 7 }); + } + + assert.deepEqual(manager._getClosedSessionTombstoneCountsForTests(), { + sessions: 8, + sequences: 8, + generations: 8, + }); + + const sessionId = "closed-23"; + now = 99; + child.emit("message", { + kind: "output-tap", + sessionId, + data: "stale", + sessionGeneration: 0, + }); + assert.deepEqual(tapped, [], "a recent close still rejects its old generation"); + + const restarted = manager.request("netcatty:local:reconnect", { sessionId }, { webContentsId: 7 }); + const restartRequest = [...child.messages].reverse().find((message) => ( + message.kind === "request" && message.payload?.sessionId === sessionId + )); + child.emit("message", { + kind: "output-tap", + sessionId, + data: "fresh", + sessionGeneration: 1, + originRequestId: restartRequest.requestId, + }); + child.emit("message", { + kind: "response", + requestId: restartRequest.requestId, + result: { sessionId }, + sessionGeneration: 1, + }); + await restarted; + + assert.deepEqual(tapped, [{ sessionId, data: "fresh" }]); + assert.equal(manager.hasOpenSession(sessionId), true, "same-id restart is not killed by its tombstone"); + assert.deepEqual(manager._getClosedSessionTombstoneCountsForTests(), { + sessions: 7, + sequences: 7, + generations: 7, + }); +}); + test("a later same-id start response cannot be overwritten by an older response", async () => { const child = new FakeChild(); const owners = []; diff --git a/electron/bridges/transferBridge.cjs b/electron/bridges/transferBridge.cjs index b9d0d008f..6f7a00727 100644 --- a/electron/bridges/transferBridge.cjs +++ b/electron/bridges/transferBridge.cjs @@ -16,6 +16,8 @@ const { resolveEncodingForRequest, } = require("./sftpBridge.cjs"); const { isScpModeClient, getScpBackendForClient } = require("./sftpBridge/scpBackend.cjs"); +const { executeBoundedSshCommand } = require("./boundedSshExec.cjs"); +const { openBoundedSftpChannel } = require("./boundedSftpOpen.cjs"); const { DOWNLOAD_TRANSFER_CONCURRENCY, FAST_DOWNLOAD_CHANNELS_PER_SESSION, @@ -33,6 +35,7 @@ const PAUSE_RANGE_DRAIN_MS = 50; const PAUSE_STREAM_DRAIN_MS = 80; /** Resume never overlaps old range writes; report a retryable miss if they stall. */ const RESUME_RANGE_SETTLE_MS = 1500; +const internalTransferIds = new Set(); /** * Fan transfer lifecycle to every window so the global transfer center keeps @@ -43,6 +46,7 @@ const RESUME_RANGE_SETTLE_MS = 1500; */ function broadcastGlobalTransferEvent(payload) { if (!payload || !payload.transferId) return; + if (internalTransferIds.has(String(payload.transferId))) return; // Bare Node unit tests must never require("electron"): that package's entry // downloads the binary when dist/ is missing, hanging transferBridge tests // for tens of seconds per progress/pause event. process.versions.electron is @@ -251,19 +255,17 @@ async function hashRemoteFile(client, sftpId, filePath, encoding, options = {}) // verification therefore uses the SFTP stream path below. if (!options.signal && !options.onProgress && sshClient && typeof sshClient.exec === "function") { const escapedPath = String(filePath).replace(/'/g, "'\\''"); - const digest = await new Promise((resolve, reject) => { - sshClient.exec(`sha256sum -- '${escapedPath}'`, (error, stream) => { - if (error) return reject(error); - let stdout = ""; - let stderr = ""; - stream.on("data", (chunk) => { stdout += chunk.toString(); }); - stream.stderr?.on?.("data", (chunk) => { stderr += chunk.toString(); }); - stream.on("close", (code) => { - const match = stdout.match(/^([a-fA-F0-9]{64})\s/); - if (code === 0 && match) resolve(match[1].toLowerCase()); - else reject(new Error(stderr || "Remote SHA-256 is unavailable")); - }); - }); + const digest = await executeBoundedSshCommand( + sshClient, + `sha256sum -- '${escapedPath}'`, + { + openingTimeoutMs: 15_000, + runTimeoutMs: 10 * 60_000, + maxOutputBytes: 64 * 1024, + }, + ).then(({ stdout, code }) => { + const match = stdout.match(/^([a-fA-F0-9]{64})\s/); + return code === 0 && match ? match[1].toLowerCase() : null; }).catch(() => null); if (digest) return digest; } @@ -733,13 +735,67 @@ const admittedTransferQueue = []; const pausedAdmittedTransfers = new Map(); const workerTransferLifecycleEpochs = new Map(); /** Transfer ids cancelled before startTransferNow registered them (skipAdmission open window). */ -const pendingCancelTransferIds = new Set(); +const pendingCancelTransferIds = new Map(); +const MAX_PENDING_CANCEL_TRANSFER_IDS = 4_096; +const PENDING_CANCEL_TTL_MS = 5 * 60 * 1000; +let pendingCancelCleanupTimer = null; + +function prunePendingCancelTransferIds(now = Date.now()) { + for (const [transferId, createdAt] of pendingCancelTransferIds) { + if (now - createdAt < PENDING_CANCEL_TTL_MS) break; + pendingCancelTransferIds.delete(transferId); + } + while (pendingCancelTransferIds.size > MAX_PENDING_CANCEL_TRANSFER_IDS) { + const oldestTransferId = pendingCancelTransferIds.keys().next().value; + if (oldestTransferId == null) break; + pendingCancelTransferIds.delete(oldestTransferId); + } +} + +function schedulePendingCancelCleanup() { + if (pendingCancelCleanupTimer || pendingCancelTransferIds.size === 0) return; + const oldestCreatedAt = pendingCancelTransferIds.values().next().value ?? Date.now(); + const delay = Math.max(1, oldestCreatedAt + PENDING_CANCEL_TTL_MS - Date.now()); + pendingCancelCleanupTimer = setTimeout(() => { + pendingCancelCleanupTimer = null; + prunePendingCancelTransferIds(); + schedulePendingCancelCleanup(); + }, delay); + if (typeof pendingCancelCleanupTimer.unref === "function") { + pendingCancelCleanupTimer.unref(); + } +} + +function rememberPendingCancel(transferId) { + if (!transferId) return; + const id = String(transferId); + pendingCancelTransferIds.delete(id); + pendingCancelTransferIds.set(id, Date.now()); + prunePendingCancelTransferIds(); + schedulePendingCancelCleanup(); +} + +function forgetPendingCancel(transferId) { + if (!transferId) return false; + const deleted = pendingCancelTransferIds.delete(String(transferId)); + if (pendingCancelTransferIds.size === 0 && pendingCancelCleanupTimer) { + clearTimeout(pendingCancelCleanupTimer); + pendingCancelCleanupTimer = null; + } + return deleted; +} + +function takePendingCancel(transferId) { + if (!transferId) return false; + prunePendingCancelTransferIds(); + return forgetPendingCancel(transferId); +} const admittedActiveByResource = new Map(); let admittedTransferLimit = 2; const isolatedDownloadChannelPools = new WeakMap(); -// Cache sftpIds where remote cp is known to be unavailable, so we skip -// repeated failed exec attempts for each file in a multi-file transfer. -const cpUnavailableSet = new Set(); +// Cache live SFTP clients where remote cp is known to be unavailable, so we +// skip repeated failed exec attempts without retaining closed session ids. +const cpUnavailableSet = new WeakSet(); const { sftpTransferSessionLeaseStore, @@ -810,20 +866,33 @@ function listTransferSftpIds(payload = {}) { function acquireTransferSessionLeases(transferId, payload) { const sftpIds = listTransferSftpIds(payload); + // This whole function is synchronous, so preflight every endpoint before + // cancelling any uncommitted close claim. That keeps a two-server transfer + // from partially acquiring one side when the other is already closing. + const closingSftpId = sftpIds.find((sftpId) => ( + sftpTransferSessionLeaseStore.isHardCloseCommitted(sftpId) + )); + if (closingSftpId) { + throw new Error(`SFTP session is closing: ${closingSftpId}`); + } for (const sftpId of sftpIds) { sftpTransferSessionLeaseStore.acquire(sftpId, transferId); } return sftpIds; } -async function hardCloseSftpSession(sftpId) { +async function hardCloseSftpSession(sftpId, closeToken) { if (!sftpId) return; + // Give a directory walk one turn to acquire the next child lease. That + // cancels the still-uncommitted claim and safely keeps the session alive. + await new Promise((resolve) => setImmediate(resolve)); // TOCTOU: another transfer may have acquired between shouldHardClose and here. // Re-arm soft-close and abort the force close so we don't kill live work. if (sftpTransferSessionLeaseStore.isHeld(sftpId)) { sftpTransferSessionLeaseStore.markSoftClosed(sftpId); return; } + if (!sftpTransferSessionLeaseStore.commitHardClose(sftpId, closeToken)) return; try { const sftpBridge = require("./sftpBridge.cjs"); if (typeof sftpBridge.closeSftp === "function") { @@ -859,7 +928,10 @@ function releaseTransferSessionLeases(transferId, sftpIds) { for (const sftpId of sftpIds || []) { const result = sftpTransferSessionLeaseStore.release(sftpId, transferId); if (result.shouldHardClose) { - void hardCloseSftpSession(sftpId); + void hardCloseSftpSession( + sftpId, + sftpTransferSessionLeaseStore.getPendingHardCloseToken(sftpId), + ); } } } @@ -882,50 +954,35 @@ function getGlobalTransferConcurrency() { * which sends SIGHUP to the remote process. */ function execSshCommandCancellable(sshClient, command, transfer) { - return new Promise((resolve, reject) => { - if (transfer.cancelled) return reject(new Error('Transfer cancelled')); - - sshClient.exec(command, (err, stream) => { - if (err) return reject(err); - - // If cancelled between exec() call and callback, kill immediately - if (transfer.cancelled) { - try { stream.close(); } catch { } - return reject(new Error('Transfer cancelled')); - } - - let stdout = ''; - let stderr = ''; - - // Wire abort: closing the stream kills the remote process - const prevAbort = transfer.abort; - transfer.abort = () => { - try { stream.close(); } catch { } - if (typeof prevAbort === 'function') prevAbort(); - }; - - stream.on('close', (code) => { - transfer.abort = prevAbort; // restore - if (transfer.cancelled) return reject(new Error('Transfer cancelled')); - resolve({ stdout, stderr, code }); - }); - - stream.on('data', (data) => { stdout += data.toString(); }); - stream.stderr.on('data', (data) => { stderr += data.toString(); }); - }); + if (transfer.cancelled) return Promise.reject(new Error('Transfer cancelled')); + const controller = new AbortController(); + const prevAbort = transfer.abort; + const abort = () => { + controller.abort(new Error('Transfer cancelled')); + if (typeof prevAbort === 'function') prevAbort(); + }; + transfer.abort = abort; + return executeBoundedSshCommand(sshClient, command, { + signal: controller.signal, + openingTimeoutMs: 15_000, + runTimeoutMs: 10 * 60_000, + maxOutputBytes: 64 * 1024, + }).then((result) => { + if (transfer.cancelled) throw new Error('Transfer cancelled'); + return result; + }).catch((error) => { + if (transfer.cancelled || error?.code === 'ABORT_ERR') { + throw new Error('Transfer cancelled'); + } + throw error; + }).finally(() => { + if (transfer.abort === abort) transfer.abort = prevAbort; }); } -async function openIsolatedSftpChannel(client) { +async function openIsolatedSftpChannel(client, signal = null) { const sshClient = client?.client; - if (!sshClient || typeof sshClient.sftp !== "function") return null; - - return new Promise((resolve, reject) => { - sshClient.sftp((err, sftp) => { - if (err) reject(err); - else resolve(sftp); - }); - }); + return openBoundedSftpChannel(sshClient, { signal }); } /** @@ -1115,7 +1172,7 @@ async function acquireIsolatedDownloadChannel(client, transfer) { pool.opening += 1; try { - const opened = await openIsolatedSftpChannel(client); + const opened = await openIsolatedSftpChannel(client, transfer?.signal); pool.opening -= 1; if (!opened) return null; if (transfer?.cancelled) { @@ -1412,7 +1469,7 @@ async function uploadFile( if (!client.__netcattySudoMode) { let isolated = null; try { - isolated = await openIsolatedSftpChannel(client); + isolated = await openIsolatedSftpChannel(client, transfer?.signal); } catch (err) { rememberPipelineError(err); console.warn( @@ -1467,7 +1524,7 @@ async function uploadFile( if (!isolated) { try { - isolated = await openIsolatedSftpChannel(client); + isolated = await openIsolatedSftpChannel(client, transfer?.signal); } catch (err) { rememberPipelineError(err); console.warn( @@ -2696,8 +2753,7 @@ async function startTransferNow(event, payload, onProgress) { const sender = event.sender; // Cancel may have won the race during open/reconnect before we register. - if (transferId && pendingCancelTransferIds.has(transferId)) { - pendingCancelTransferIds.delete(transferId); + if (takePendingCancel(transferId)) { sender.send?.("netcatty:transfer:cancelled", { transferId }); broadcastGlobalTransferEvent({ type: "cancelled", transferId, endedAt: Date.now() }); return { transferId, error: "Transfer cancelled", cancelled: true }; @@ -2785,11 +2841,13 @@ async function startTransferNow(event, payload, onProgress) { transfer.abortOwnedSignal = () => { try { ownedAbort.abort(); } catch { /* ignore */ } }; - activeTransfers.set(transferId, transfer); // Hold panel/agent SFTP sessions for the full transfer lifetime (including // pause). Panel close becomes a soft-close until we release these leases. const leasedSftpIds = acquireTransferSessionLeases(transferId, payload); transfer.leasedSftpIds = leasedSftpIds; + // Publish only after every requested session lease was acquired. A closing + // endpoint rejects synchronously and must not leave a ghost active transfer. + activeTransfers.set(transferId, transfer); const transferCreatedAt = Date.now(); const runCancelablePreflight = (operation) => runTransferCancelablePreflight(transfer, operation); @@ -3471,12 +3529,13 @@ async function startTransferNow(event, payload, onProgress) { let sameHostDone = false; const resolvedSourceEnc = sourceSftpId ? resolveEncodingForRequest(sourceSftpId, sourceEncoding) : sourceEncoding; const resolvedTargetEnc = targetSftpId ? resolveEncodingForRequest(targetSftpId, targetEncoding) : targetEncoding; + const srcClient = sftpClients.get(sourceSftpId); if (!transfer.resumable && sameHost && (!resolvedSourceEnc || resolvedSourceEnc === 'utf-8') && (!resolvedTargetEnc || resolvedTargetEnc === 'utf-8') - && !cpUnavailableSet.has(sourceSftpId)) { - const srcClient = sftpClients.get(sourceSftpId); + && srcClient + && !cpUnavailableSet.has(srcClient)) { const sshClient = srcClient?.client; if (sshClient && typeof sshClient.exec === 'function') { try { @@ -3502,7 +3561,7 @@ async function startTransferNow(event, payload, onProgress) { sameHostDone = true; } else if (result.code === 127) { // Exit 127 = command not found — cache to skip future attempts - cpUnavailableSet.add(sourceSftpId); + cpUnavailableSet.add(srcClient); } // Other non-zero exits (permission denied, disk full, etc.) // fall through to download+upload without caching @@ -3935,26 +3994,37 @@ function startTransfer(event, payload, onProgress) { return runAdmittedTransfer(event, payload, onProgress); } +async function startInternalTransfer(event, payload, onProgress) { + const transferId = String(payload?.transferId || ""); + if (!transferId) throw new Error("Internal transfer ID is required"); + internalTransferIds.add(transferId); + try { + return await startTransfer({ ...event, sender: { send() {} } }, payload, onProgress); + } finally { + internalTransferIds.delete(transferId); + } +} + /** * Cancel a transfer */ async function cancelTransfer(event, payload) { const { transferId } = payload; - if (transferId) pendingCancelTransferIds.add(String(transferId)); + rememberPendingCancel(transferId); if (cancelQueuedTransfer(transferId)) { // Queued cancel already settled the job; clear pending so a later retry // with a new open can proceed if the UI reuses the id unexpectedly. - pendingCancelTransferIds.delete(String(transferId)); + forgetPendingCancel(transferId); return { success: true }; } const transfer = activeTransfers.get(transferId); if (transfer) { if (transfer.completionCommitted) { - pendingCancelTransferIds.delete(String(transferId)); + forgetPendingCancel(transferId); return { success: true }; } transfer.cancelled = true; - pendingCancelTransferIds.delete(String(transferId)); + forgetPendingCancel(transferId); if (typeof transfer.abortOwnedSignal === "function") { try { transfer.abortOwnedSignal(); } catch { /* ignore */ } } @@ -3975,7 +4045,7 @@ async function cancelTransfer(event, payload) { /** Clear a pre-start cancel latch (used when retrying the same transfer id). */ function clearPendingCancel(transferId) { - if (transferId) pendingCancelTransferIds.delete(String(transferId)); + forgetPendingCancel(transferId); } /** @@ -4384,10 +4454,9 @@ async function sameHostCopyDirectory(event, payload) { } try { - if (cpUnavailableSet.has(sftpId)) return { success: false }; - const client = sftpClients.get(sftpId); if (!client) return { success: false }; + if (cpUnavailableSet.has(client)) return { success: false }; const sshClient = client.client; if (!sshClient || typeof sshClient.exec !== 'function') { @@ -4428,7 +4497,7 @@ async function sameHostCopyDirectory(event, payload) { try { const result = await execSshCommandCancellable(sshClient, command, transfer); if (result.code === 127) { - cpUnavailableSet.add(sftpId); + cpUnavailableSet.add(client); return { success: false }; } if (result.code !== 0) { @@ -4472,36 +4541,51 @@ function registerHandlers(ipcMain, options = {}) { const terminalWorkerManager = options.terminalWorkerManager || null; if (terminalWorkerManager) { const nextWorkerLifecycleEpoch = (transferId, suggestedEpoch) => { - const current = Math.max(0, Number(workerTransferLifecycleEpochs.get(transferId)) || 0); + const entry = workerTransferLifecycleEpochs.get(transferId); + const current = Math.max(0, Number(entry?.epoch) || 0); const suggested = Number(suggestedEpoch); const next = Number.isFinite(suggested) && suggested > current ? suggested : current + 1; - workerTransferLifecycleEpochs.set(transferId, next); + if (entry) entry.epoch = next; return next; }; const workerRequest = (event, channel, payload) => terminalWorkerManager.request(channel, payload, { webContentsId: event?.sender?.id, }); ipcMain.handle("netcatty:transfer:start", (event, payload) => { + let lifecycleEntry = null; if (payload?.transferId) { - workerTransferLifecycleEpochs.set( - payload.transferId, - Math.max(0, Number(payload.lifecycleEpoch) || 0), - ); + lifecycleEntry = { + epoch: Math.max(0, Number(payload.lifecycleEpoch) || 0), + }; + workerTransferLifecycleEpochs.set(payload.transferId, lifecycleEntry); } + const releaseLifecycleEntry = () => { + if ( + payload?.transferId + && workerTransferLifecycleEpochs.get(payload.transferId) === lifecycleEntry + ) { + workerTransferLifecycleEpochs.delete(payload.transferId); + } + }; // Renderer (or outer main) already admitted — skip a second queue so // dedicated pool leases are not pinned while waiting on main admission. - if (payload?.skipAdmission === true) { - return workerRequest(event, "netcatty:transfer:start", payload); + try { + const operation = payload?.skipAdmission === true + ? workerRequest(event, "netcatty:transfer:start", payload) + : runAdmittedTransfer( + event, + payload, + undefined, + () => workerRequest(event, "netcatty:transfer:start", { + ...payload, + skipAdmission: true, + }), + ); + return Promise.resolve(operation).finally(releaseLifecycleEntry); + } catch (error) { + releaseLifecycleEntry(); + throw error; } - return runAdmittedTransfer( - event, - payload, - undefined, - () => workerRequest(event, "netcatty:transfer:start", { - ...payload, - skipAdmission: true, - }), - ); }); ipcMain.handle("netcatty:transfer:cancel", (event, payload) => ( cancelQueuedTransfer(payload?.transferId) @@ -4613,6 +4697,7 @@ module.exports = { init, registerHandlers, startTransfer, + startInternalTransfer, runAdmittedTransfer, cancelTransfer, clearPendingCancel, @@ -4630,4 +4715,8 @@ module.exports = { listTransferSftpIds, _promoteLocalTransferForTests: promoteLocalTransfer, _stableLocalFileIdentityForTests: stableLocalFileIdentity, + _getWorkerTransferLifecycleEpochCountForTests: () => workerTransferLifecycleEpochs.size, + _getPendingCancelCountForTests: () => pendingCancelTransferIds.size, + _getActiveTransferCountForTests: () => activeTransfers.size, + _execSshCommandCancellableForTests: execSshCommandCancellable, }; diff --git a/electron/bridges/transferBridge.globalFanout.test.cjs b/electron/bridges/transferBridge.globalFanout.test.cjs index 2d168b237..dd9ebdc4e 100644 --- a/electron/bridges/transferBridge.globalFanout.test.cjs +++ b/electron/bridges/transferBridge.globalFanout.test.cjs @@ -1,6 +1,7 @@ "use strict"; const assert = require("node:assert/strict"); +const { EventEmitter } = require("node:events"); const test = require("node:test"); const Module = require("node:module"); const path = require("node:path"); @@ -99,6 +100,113 @@ test("broadcastGlobalTransferEvent no-ops without transferId", () => { assert.doesNotThrow(() => bridge.broadcastGlobalTransferEvent(null)); }); +test("same-host cp capability cache follows the live SFTP client, not a recycled session id", async () => { + const bridge = require("./transferBridge.cjs"); + const sftpClients = new Map(); + bridge.init({ sftpClients }); + + const makeClient = (exitCode, calls) => ({ + client: { + exec(_command, callback) { + calls.count += 1; + const stream = new EventEmitter(); + stream.stderr = new EventEmitter(); + stream.close = () => {}; + callback(null, stream); + queueMicrotask(() => stream.emit("close", exitCode)); + }, + }, + }); + const firstCalls = { count: 0 }; + const secondCalls = { count: 0 }; + const event = { sender: { send() {} } }; + const payload = { + sftpId: "recycled-sftp-id", + sourcePath: "/source", + targetPath: "/target", + encoding: "utf-8", + }; + + sftpClients.set(payload.sftpId, makeClient(127, firstCalls)); + assert.equal((await bridge.sameHostCopyDirectory(event, payload)).success, false); + assert.equal(firstCalls.count, 1); + + sftpClients.set(payload.sftpId, makeClient(0, secondCalls)); + assert.equal((await bridge.sameHostCopyDirectory(event, payload)).success, true); + assert.equal(secondCalls.count, 1, "a replacement connection must probe cp again"); +}); + +test("worker transfer lifecycle epochs survive a reused id and clear after the latest start settles", async () => { + const bridgePath = path.join(__dirname, "transferBridge.cjs"); + delete require.cache[require.resolve(bridgePath)]; + const bridge = require(bridgePath); + const handlers = new Map(); + let finishFirst; + let finishSecond; + const firstGate = new Promise((resolve) => { finishFirst = resolve; }); + const secondGate = new Promise((resolve) => { finishSecond = resolve; }); + let startCalls = 0; + + bridge.registerHandlers({ + handle(channel, handler) { handlers.set(channel, handler); }, + }, { + terminalWorkerManager: { + request(channel) { + if (channel === "netcatty:transfer:start") { + startCalls += 1; + return startCalls === 1 ? firstGate : secondGate; + } + return Promise.resolve({ success: true }); + }, + }, + }); + + const firstStarting = handlers.get("netcatty:transfer:start")( + { sender: { id: 1 } }, + { transferId: "worker-cache-lifetime", skipAdmission: true }, + ); + const secondStarting = handlers.get("netcatty:transfer:start")( + { sender: { id: 1 } }, + { transferId: "worker-cache-lifetime", skipAdmission: true }, + ); + assert.equal(bridge._getWorkerTransferLifecycleEpochCountForTests(), 1); + + finishFirst({ transferId: "worker-cache-lifetime" }); + await firstStarting; + assert.equal( + bridge._getWorkerTransferLifecycleEpochCountForTests(), + 1, + "an older completion must not clear the newer start's lifecycle state", + ); + + finishSecond({ transferId: "worker-cache-lifetime" }); + await secondStarting; + assert.equal(bridge._getWorkerTransferLifecycleEpochCountForTests(), 0); +}); + +test("worker transfer lifecycle epoch clears after a synchronous start failure", () => { + const bridgePath = path.join(__dirname, "transferBridge.cjs"); + delete require.cache[require.resolve(bridgePath)]; + const bridge = require(bridgePath); + const handlers = new Map(); + bridge.registerHandlers({ + handle(channel, handler) { handlers.set(channel, handler); }, + }, { + terminalWorkerManager: { + request(channel) { + if (channel === "netcatty:transfer:start") throw new Error("worker unavailable"); + return Promise.resolve({ success: true }); + }, + }, + }); + + assert.throws(() => handlers.get("netcatty:transfer:start")( + { sender: { id: 1 } }, + { transferId: "worker-cache-sync-failure", skipAdmission: true }, + ), /worker unavailable/); + assert.equal(bridge._getWorkerTransferLifecycleEpochCountForTests(), 0); +}); + test("worker-backed pause and resume fan authoritative lifecycle to every window", async () => { const sent = []; const restoreElectronVersion = withElectronVersionStub(); @@ -125,11 +233,14 @@ test("worker-backed pause and resume fan authoritative lifecycle to every window delete require.cache[require.resolve(bridgePath)]; const bridge = require(bridgePath); const handlers = new Map(); + let finishStart; + const startGate = new Promise((resolve) => { finishStart = resolve; }); bridge.registerHandlers({ handle(channel, handler) { handlers.set(channel, handler); }, }, { terminalWorkerManager: { async request(channel) { + if (channel === "netcatty:transfer:start") return startGate; if (channel === "netcatty:transfer:pause") { return { success: true, checkpointBytes: 50, resumeStage: "upload" }; } @@ -138,6 +249,10 @@ test("worker-backed pause and resume fan authoritative lifecycle to every window }, }); + const starting = handlers.get("netcatty:transfer:start")( + { sender: { id: 1 } }, + { transferId: "worker-transfer", skipAdmission: true }, + ); await handlers.get("netcatty:transfer:pause")( { sender: { id: 1 } }, { transferId: "worker-transfer" }, @@ -175,6 +290,9 @@ test("worker-backed pause and resume fan authoritative lifecycle to every window lifecycleState: "transferring", }, ]); + finishStart({ transferId: "worker-transfer" }); + await starting; + assert.equal(bridge._getWorkerTransferLifecycleEpochCountForTests(), 0); } finally { Module._load = originalLoad; restoreElectronVersion(); diff --git a/electron/bridges/transferBridge.remoteExec.test.cjs b/electron/bridges/transferBridge.remoteExec.test.cjs new file mode 100644 index 000000000..e90d18a2b --- /dev/null +++ b/electron/bridges/transferBridge.remoteExec.test.cjs @@ -0,0 +1,53 @@ +"use strict"; + +const test = require("node:test"); +const assert = require("node:assert/strict"); +const { EventEmitter } = require("node:events"); +const { _execSshCommandCancellableForTests } = require("./transferBridge.cjs"); + +test("same-host copy can cancel before SSH exec opens and closes a late stream", async () => { + let callback; + const sshClient = { exec(_command, next) { callback = next; } }; + const transfer = { cancelled: false, abort: null }; + const result = _execSshCommandCancellableForTests(sshClient, "cp", transfer); + transfer.cancelled = true; + transfer.abort(); + await assert.rejects(result, /Transfer cancelled/); + + const stream = new EventEmitter(); + stream.stderr = new EventEmitter(); + let closed = 0; + stream.close = () => { closed += 1; }; + callback(null, stream); + assert.ok(closed > 0); + assert.equal(transfer.abort, null); +}); + +test("same-host copy rejects output floods and SSH stream errors", async () => { + { + const stream = new EventEmitter(); + stream.stderr = new EventEmitter(); + stream.close = () => {}; + stream.destroy = () => {}; + const result = _execSshCommandCancellableForTests( + { exec(_command, next) { next(null, stream); } }, + "cp", + { cancelled: false, abort: null }, + ); + stream.stderr.emit("data", Buffer.alloc(64 * 1024 + 1)); + await assert.rejects(result, /output exceeded/); + } + { + const stream = new EventEmitter(); + stream.stderr = new EventEmitter(); + stream.close = () => {}; + stream.destroy = () => {}; + const result = _execSshCommandCancellableForTests( + { exec(_command, next) { next(null, stream); } }, + "cp", + { cancelled: false, abort: null }, + ); + stream.emit("error", new Error("copy stream failed")); + await assert.rejects(result, /copy stream failed/); + } +}); diff --git a/electron/bridges/transferBridge.test.cjs b/electron/bridges/transferBridge.test.cjs index fba20a0d8..2d945c180 100644 --- a/electron/bridges/transferBridge.test.cjs +++ b/electron/bridges/transferBridge.test.cjs @@ -559,7 +559,13 @@ test("in-place upload ignores cancellation during final size verification", asyn let markFinalStatStarted; const finalStatStarted = new Promise((resolve) => { markFinalStatStarted = resolve; }); const fastSftp = createFastSftp({ - lstat(_remotePath, callback) { + lstat(remotePath, callback) { + if (String(remotePath).includes(".netcatty-backup-")) { + const error = new Error("ENOENT"); + error.code = 2; + callback(error); + return; + } callback(null, { size: remote.length, mode: 0o120777, @@ -582,7 +588,12 @@ test("in-place upload ignores cancellation during final size verification", asyn const client = { __netcattySudoMode: true, sftp: fastSftp, - async stat() { + async stat(remotePath) { + if (String(remotePath).includes(".netcatty-backup-")) { + const error = new Error("ENOENT"); + error.code = "ENOENT"; + throw error; + } statCalls += 1; if (statCalls === 1) { markFinalStatStarted(); @@ -632,7 +643,13 @@ test("in-place concurrent upload ignores cancellation while the remote handle cl let markCloseStarted; const closeStarted = new Promise((resolve) => { markCloseStarted = resolve; }); const fastSftp = createFastSftp({ - lstat(_remotePath, callback) { + lstat(remotePath, callback) { + if (String(remotePath).includes(".netcatty-backup-")) { + const error = new Error("ENOENT"); + error.code = 2; + callback(error); + return; + } callback(null, { size: remote.length, mode: 0o120777, @@ -656,7 +673,14 @@ test("in-place concurrent upload ignores cancellation while the remote handle cl const client = { __netcattySudoMode: true, sftp: fastSftp, - stat: async () => ({ size: remote.length }), + stat: async (remotePath) => { + if (String(remotePath).includes(".netcatty-backup-")) { + const error = new Error("ENOENT"); + error.code = "ENOENT"; + throw error; + } + return { size: remote.length }; + }, }; transferBridge.init({ sftpClients: new Map([["target", client]]) }); const sender = createSender(); @@ -5556,6 +5580,21 @@ test("cancel before skipAdmission start rejects the transfer without writing", a assert.equal(source.listenerCount("data"), 0); }); +test("pre-start cancel latches stay hard bounded when cancelled work never starts", async (t) => { + t.after(() => { + for (let index = 0; index < 5_000; index += 1) { + transferBridge.clearPendingCancel(`orphan-cancel-${index}`); + } + }); + for (let index = 0; index < 5_000; index += 1) { + await transferBridge.cancelTransfer(null, { transferId: `orphan-cancel-${index}` }); + } + assert.ok( + transferBridge._getPendingCancelCountForTests() <= 4_096, + `pending cancel latches grew to ${transferBridge._getPendingCancelCountForTests()}`, + ); +}); + test("pausing a queued admission job preserves the payload checkpoint", async (t) => { const tempDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-transfer-queued-checkpoint-")); t.after(async () => { await fs.promises.rm(tempDir, { recursive: true, force: true }); }); @@ -5690,6 +5729,127 @@ test("transfer session leases hold SFTP ids across soft-close until release", as assert.equal(sftpTransferSessionLeaseStore.isHeld("s1"), false); }); +test("a committed soft-close never lends the SFTP client again while client.end is pending", async (t) => { + const { + sftpTransferSessionLeaseStore, + } = require("./sftpTransferSessionLease.cjs"); + const sftpBridge = require("./sftpBridge.cjs"); + sftpTransferSessionLeaseStore.resetForTests(); + let releaseEnd; + const endGate = new Promise((resolve) => { releaseEnd = resolve; }); + let markEndStarted; + const endStarted = new Promise((resolve) => { markEndStarted = resolve; }); + let endCalls = 0; + const client = { + async end() { + endCalls += 1; + markEndStarted(); + await endGate; + }, + }; + const sftpClients = new Map([["sftp-soft-close-race", client]]); + transferBridge.init({ sftpClients }); + sftpBridge.init({ sftpClients, sessions: new Map(), electronModule: {} }); + t.after(() => { + releaseEnd(); + sftpTransferSessionLeaseStore.resetForTests(); + sftpClients.clear(); + }); + + transferBridge.acquireTransferSessionLeases("directory-child-1", { + sourceSftpId: "sftp-soft-close-race", + }); + const softClose = await sftpBridge.closeSftp(null, { sftpId: "sftp-soft-close-race" }); + assert.equal(softClose.deferred, true); + + transferBridge.releaseTransferSessionLeases("directory-child-1", ["sftp-soft-close-race"]); + await endStarted; + + assert.throws( + () => transferBridge.acquireTransferSessionLeases("directory-child-2", { + sourceSftpId: "sftp-soft-close-race", + }), + /closing|closed/i, + ); + assert.equal(sftpTransferSessionLeaseStore.isHeld("sftp-soft-close-race"), false); + + releaseEnd(); + const deadline = Date.now() + 1_000; + while (sftpClients.has("sftp-soft-close-race")) { + if (Date.now() >= deadline) throw new Error("Timed out waiting for committed SFTP close"); + await new Promise((resolve) => setImmediate(resolve)); + } + assert.equal(endCalls, 1); +}); + +test("a direct panel close never lends the SFTP client while client.end is pending", async (t) => { + const { + sftpTransferSessionLeaseStore, + } = require("./sftpTransferSessionLease.cjs"); + const sftpBridge = require("./sftpBridge.cjs"); + sftpTransferSessionLeaseStore.resetForTests(); + let releaseEnd; + const endGate = new Promise((resolve) => { releaseEnd = resolve; }); + let markEndStarted; + const endStarted = new Promise((resolve) => { markEndStarted = resolve; }); + const client = { + async end() { + markEndStarted(); + await endGate; + }, + }; + const sftpClients = new Map([["sftp-direct-close-race", client]]); + transferBridge.init({ sftpClients }); + sftpBridge.init({ sftpClients, sessions: new Map(), electronModule: {} }); + t.after(() => { + releaseEnd(); + sftpTransferSessionLeaseStore.resetForTests(); + sftpClients.clear(); + }); + + const closePromise = sftpBridge.closeSftp(null, { sftpId: "sftp-direct-close-race" }); + await endStarted; + + assert.throws( + () => transferBridge.acquireTransferSessionLeases("new-transfer", { + sourceSftpId: "sftp-direct-close-race", + }), + /closing|closed/i, + ); + + releaseEnd(); + const closeResult = await closePromise; + assert.equal(closeResult.deferred, false); + assert.equal(sftpClients.has("sftp-direct-close-race"), false); + assert.equal(sftpTransferSessionLeaseStore.isHeld("sftp-direct-close-race"), false); +}); + +test("a transfer rejected by a committed SFTP close leaves no active transfer entry", async (t) => { + const { + sftpTransferSessionLeaseStore, + } = require("./sftpTransferSessionLease.cjs"); + const sftpId = "sftp-rejected-before-lease"; + const closeToken = sftpTransferSessionLeaseStore.beginHardClose(sftpId); + assert.equal(sftpTransferSessionLeaseStore.commitHardClose(sftpId, closeToken), true); + transferBridge.init({ sftpClients: new Map([[sftpId, {}]]) }); + t.after(() => sftpTransferSessionLeaseStore.resetForTests()); + + await assert.rejects( + transferBridge.startTransfer({ sender: createSender() }, { + transferId: "rejected-before-lease", + sourcePath: "/remote/file.bin", + targetPath: "/tmp/rejected-before-lease.bin", + sourceType: "sftp", + targetType: "local", + sourceSftpId: sftpId, + totalBytes: 1, + skipAdmission: true, + }), + /closing|closed/i, + ); + assert.equal(transferBridge._getActiveTransferCountForTests(), 0); +}); + test("local promotion restores concurrent replacement moved into the backup", async (t) => { const tempDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-local-promo-backup-")); t.after(async () => { diff --git a/electron/capabilities/services/portforwardService.cjs b/electron/capabilities/services/portforwardService.cjs index 5524ebb25..a40358a75 100644 --- a/electron/capabilities/services/portforwardService.cjs +++ b/electron/capabilities/services/portforwardService.cjs @@ -3,10 +3,14 @@ const portForwardingBridge = require("../../bridges/portForwardingBridge.cjs"); /** - * Port forwarding domain service. Tunnels live in main; rules live in renderer vault. + * Port forwarding domain service. Tunnels live in the configured runtime + * (terminal worker in the current architecture); rules live in renderer vault. */ function createPortForwardService(ctx = {}) { const { invokeVaultAgent } = ctx; + const listPortForwards = typeof ctx.listPortForwards === "function" + ? ctx.listPortForwards + : () => portForwardingBridge.listPortForwards(); return { listRules: async () => { @@ -32,7 +36,7 @@ function createPortForwardService(ctx = {}) { return invokeVaultAgent("portforward.rules.delete", params); }, listTunnels: async () => { - const tunnels = await portForwardingBridge.listPortForwards(); + const tunnels = await listPortForwards(); return { ok: true, tunnels }; }, start: async (params = {}) => { diff --git a/electron/capabilities/services/portforwardService.test.cjs b/electron/capabilities/services/portforwardService.test.cjs index 890e92d03..23b24798b 100644 --- a/electron/capabilities/services/portforwardService.test.cjs +++ b/electron/capabilities/services/portforwardService.test.cjs @@ -5,13 +5,19 @@ const assert = require("node:assert/strict"); const { createPortForwardService } = require("./portforwardService.cjs"); -test("portforward service lists active tunnels from main bridge", async () => { +test("portforward service lists active tunnels through the configured runtime", async () => { + const calls = []; const service = createPortForwardService({ invokeVaultAgent: async () => ({ ok: true, rules: [] }), + listPortForwards: async () => { + calls.push("list"); + return [{ tunnelId: "worker-tunnel", status: "active" }]; + }, }); const result = await service.listTunnels(); assert.equal(result.ok, true); - assert.ok(Array.isArray(result.tunnels)); + assert.deepEqual(result.tunnels, [{ tunnelId: "worker-tunnel", status: "active" }]); + assert.deepEqual(calls, ["list"]); }); test("portforward start delegates to vault agent bridge after approval path", async () => { diff --git a/electron/main/registerBridges.cjs b/electron/main/registerBridges.cjs index 7f0cf63a5..b1dc7c68b 100644 --- a/electron/main/registerBridges.cjs +++ b/electron/main/registerBridges.cjs @@ -2,7 +2,6 @@ let bridgesRegistered = false; let cloudSyncSessionPassword = null; -const { randomUUID } = require("node:crypto"); const { readClipboardFiles, readClipboardImage } = require("../bridges/clipboardFiles.cjs"); const excludedFigSpecPrefixes = ["aws", "gcloud", "az"]; @@ -15,6 +14,34 @@ function filterExcludedFigSpecs(specNames) { return specNames.filter((name) => !isExcludedFigSpec(name)); } +function waitForApplicationSpawn(child, requireCleanLauncherExit = false) { + return new Promise((resolve, reject) => { + const onSpawn = () => { + if (requireCleanLauncherExit) return; + cleanup(); + resolve(); + }; + const onClose = (code) => { + if (!requireCleanLauncherExit) return; + cleanup(); + if (code === 0) resolve(); + else reject(new Error(`Application launcher exited with code ${code}`)); + }; + const onError = (error) => { + cleanup(); + reject(error); + }; + const cleanup = () => { + child.removeListener("spawn", onSpawn); + child.removeListener("error", onError); + child.removeListener("close", onClose); + }; + child.once("spawn", onSpawn); + child.once("error", onError); + if (requireCleanLauncherExit) child.once("close", onClose); + }); +} + function createBridgeRegistrar(context) { const { electronModule, @@ -339,6 +366,16 @@ function createBridgeRegistrar(context) { MessageChannelMain: electronModule.MessageChannelMain, }) : null; + if (terminalWorkerManager) { + const { registerPluginShutdown } = require("../plugins/shutdownCoordinator.cjs"); + registerPluginShutdown(async () => { + try { + await terminalWorkerManager.request("netcatty:portforward:stopAll", {}, {}); + } catch (error) { + console.warn("[PortForward] Worker shutdown cleanup failed:", error?.message || error); + } + }); + } const terminalPipelineSessionManager = terminalWorkerManager ?? { getSessionOwnerWebContentsId(sessionId) { const owner = sessions.get(sessionId)?.webContentsId; @@ -394,7 +431,7 @@ function createBridgeRegistrar(context) { sftpBridge.registerHandlers(ipcMain, { terminalWorkerManager }); localFsBridge.registerHandlers(ipcMain); transferBridge.registerHandlers(ipcMain, { terminalWorkerManager }); - portForwardingBridge.registerHandlers(ipcMain); + portForwardingBridge.registerHandlers(ipcMain, { terminalWorkerManager }); terminalBridge.registerHandlers(ipcMain, { terminalWorkerManager }); const scriptBridge = require("../bridges/scriptBridge.cjs"); @@ -886,11 +923,10 @@ function createBridgeRegistrar(context) { console.log(`[Main] Command: open ${args.join(' ')}`); child = cpSpawn("open", args, { detached: true, stdio: "pipe" }); } else if (process.platform === "win32") { - // On Windows, use cmd /c start to properly handle paths with spaces - // The empty string "" as window title is required when the first arg has quotes - const args = ["/c", "start", "\"\"", `"${appPath}"`, `"${filePath}"`]; - console.log(`[Main] Command: cmd ${args.join(' ')}`); - child = cpSpawn("cmd", args, { detached: true, stdio: "pipe", windowsVerbatimArguments: true }); + // Spawn the selected executable directly. This preserves paths with + // spaces without a shell and reports an invalid app path as ENOENT. + console.log(`[Main] Command: ${appPath} ${filePath}`); + child = cpSpawn(appPath, [filePath], { detached: true, stdio: "pipe" }); } else { // On Linux, spawn the app with the file console.log(`[Main] Command: ${appPath} ${filePath}`); @@ -933,7 +969,11 @@ function createBridgeRegistrar(context) { console.log(`[Main] Application started successfully`); } }); - + + // spawn() reports ENOENT and similar launch failures asynchronously. + // Do not acknowledge success until the child has actually started, so + // the renderer can unregister and delete its downloaded temp file. + await waitForApplicationSpawn(child, process.platform === "darwin"); child.unref(); return true; } catch (err) { @@ -1008,54 +1048,6 @@ function createBridgeRegistrar(context) { return result.filePaths[0]; }); - // Download SFTP file to temp and return local path - ipcMain.handle("netcatty:sftp:downloadToTemp", async (event, { sftpId, remotePath, fileName, encoding }) => { - console.log(`[Main] Downloading SFTP file to temp:`); - console.log(`[Main] SFTP ID: ${sftpId}`); - console.log(`[Main] Remote path: ${remotePath}`); - console.log(`[Main] File name: ${fileName}`); - - // Use tempDirBridge for dedicated Netcatty temp directory - const localPath = await getTempDirBridge().getTempFilePath(fileName); - - console.log(`[Main] Local temp path: ${localPath}`); - - const payload = { - transferId: `download-temp-${randomUUID()}`, - sourcePath: remotePath, - targetPath: localPath, - sourceType: "sftp", - targetType: "local", - sourceSftpId: sftpId, - sourceEncoding: encoding, - totalBytes: 0, - }; - - try { - const result = terminalWorkerManager - ? await (transferBridge.runAdmittedTransfer ? transferBridge.runAdmittedTransfer( - event, - payload, - undefined, - () => terminalWorkerManager.request("netcatty:transfer:start", { - ...payload, - skipAdmission: true, - }, { - webContentsId: event?.sender?.id, - }), - ) : terminalWorkerManager.request("netcatty:transfer:start", payload, { - webContentsId: event?.sender?.id, - })) - : await transferBridge.startTransfer(event, payload); - if (result?.error) throw new Error(result.error); - console.log(`[Main] File downloaded successfully`); - return localPath; - } catch (err) { - try { await fs.promises.rm(localPath, { force: true }); } catch { /* ignore */ } - throw err; - } - }); - // Download SFTP file to temp with progress reporting via transfer events. // Progress/complete/cancelled events are delivered via the netcatty:transfer:* // channels (handled by transferBridge.startTransfer), so the IPC return value @@ -1140,4 +1132,9 @@ function createBridgeRegistrar(context) { return registerBridges; } -module.exports = { createBridgeRegistrar, filterExcludedFigSpecs, isExcludedFigSpec }; +module.exports = { + createBridgeRegistrar, + filterExcludedFigSpecs, + isExcludedFigSpec, + _waitForApplicationSpawnForTests: waitForApplicationSpawn, +}; diff --git a/electron/main/registerBridges.openApplication.test.cjs b/electron/main/registerBridges.openApplication.test.cjs new file mode 100644 index 000000000..f06fe755a --- /dev/null +++ b/electron/main/registerBridges.openApplication.test.cjs @@ -0,0 +1,35 @@ +"use strict"; + +const assert = require("node:assert/strict"); +const { EventEmitter } = require("node:events"); +const test = require("node:test"); +const { + _waitForApplicationSpawnForTests: waitForApplicationSpawn, +} = require("./registerBridges.cjs"); + +test("application launch acknowledgement rejects an asynchronous spawn failure", async () => { + const child = new EventEmitter(); + const waiting = waitForApplicationSpawn(child); + const error = Object.assign(new Error("spawn ENOENT"), { code: "ENOENT" }); + process.nextTick(() => child.emit("error", error)); + await assert.rejects(waiting, { code: "ENOENT" }); + assert.equal(child.listenerCount("spawn"), 0); +}); + +test("application launch acknowledgement resolves only after spawn", async () => { + const child = new EventEmitter(); + const waiting = waitForApplicationSpawn(child); + process.nextTick(() => child.emit("spawn")); + await waiting; + assert.equal(child.listenerCount("spawn"), 0); +}); + +test("macOS launcher acknowledgement rejects a nonzero open command exit", async () => { + const child = new EventEmitter(); + const waiting = waitForApplicationSpawn(child, true); + process.nextTick(() => { + child.emit("spawn"); + child.emit("close", 1); + }); + await assert.rejects(waiting, /launcher exited with code 1/i); +}); diff --git a/electron/plugins/pluginBridge.cjs b/electron/plugins/pluginBridge.cjs index 77afd25fc..a1cb4c532 100644 --- a/electron/plugins/pluginBridge.cjs +++ b/electron/plugins/pluginBridge.cjs @@ -241,6 +241,22 @@ function registerPluginBridge(ipcMain, options) { return callback(activeManager, payload, event); }); }; + // These channels are called by ordinary host/terminal lifecycles even when + // the experimental plugin runtime is disabled. Treat the disabled runtime as + // an empty provider set instead of throwing (and making Electron print a + // stack trace) on every terminal mount, Vault refresh, and scope update. + const handlePassive = (channel, fallback, callback) => { + ipcMain.handle(channel, async (event, payload) => { + if (!isTrustedSender(event)) throw new Error("Untrusted plugin management sender"); + let activeManager; + try { + activeManager = await resolveManager(); + } catch { + return typeof fallback === "function" ? fallback(payload, event) : fallback; + } + return callback(activeManager, payload, event); + }); + }; const terminalRequestMap = (sender) => { if (!sender || typeof sender !== "object") throw new Error("Plugin terminal request sender is unavailable"); let requests = terminalRequestsBySender.get(sender); @@ -570,7 +586,7 @@ function registerPluginBridge(ipcMain, options) { viewHost?.setEnvironment?.(payload ?? {}); return null; }); - handle(CHANNELS.terminalProviders, async (_activeManager, payload) => { + handlePassive(CHANNELS.terminalProviders, [], async (_activeManager, payload) => { if (!terminalProviderService) throw new Error("Plugin Terminal Providers are unavailable"); return terminalProviderService.listProviders(payload ?? {}); }); @@ -607,7 +623,7 @@ function registerPluginBridge(ipcMain, options) { controller?.abort(); return controller != null; }); - handle(CHANNELS.terminalSessionEvent, async (_activeManager, payload, event) => { + handlePassive(CHANNELS.terminalSessionEvent, [], async (_activeManager, payload, event) => { if (!terminalProviderService) throw new Error("Plugin Terminal Providers are unavailable"); if (terminalDataPipelineService?.acceptsSessionEvent && !terminalDataPipelineService.acceptsSessionEvent(payload, event?.sender?.id)) { @@ -621,11 +637,11 @@ function registerPluginBridge(ipcMain, options) { ]); return providers; }); - handle(CHANNELS.extensionProviders, async (_activeManager, payload) => { + handlePassive(CHANNELS.extensionProviders, [], async (_activeManager, payload) => { if (!extensionProviderService) throw new Error("Plugin extension Providers are unavailable"); return extensionProviderService.listProviders(payload ?? {}); }); - handle(CHANNELS.credentialCatalogUpdate, async (_activeManager, payload) => { + handlePassive(CHANNELS.credentialCatalogUpdate, 0, async (_activeManager, payload) => { if (!credentialResolver || typeof credentialResolver.update !== "function") { throw new Error("Plugin Vault credential catalog is unavailable"); } @@ -991,8 +1007,8 @@ function registerPluginBridge(ipcMain, options) { await viewHost.postMessage(payload?.instanceId, payload?.message, event.sender); return null; }); - handle(CHANNELS.getScopeCatalog, async () => currentScopeCatalog()); - handle(CHANNELS.setScopeCatalog, async (_activeManager, payload, event) => { + handlePassive(CHANNELS.getScopeCatalog, () => currentScopeCatalog(), async () => currentScopeCatalog()); + handlePassive(CHANNELS.setScopeCatalog, null, async (_activeManager, payload, event) => { const key = scopeCatalogSenderKey(event); const scopeCatalog = normalizePluginScopeCatalog(payload); scopeCatalogs.set(key, scopeCatalog); diff --git a/electron/plugins/pluginBridge.test.cjs b/electron/plugins/pluginBridge.test.cjs index ba837912f..92dbb9cda 100644 --- a/electron/plugins/pluginBridge.test.cjs +++ b/electron/plugins/pluginBridge.test.cjs @@ -35,6 +35,27 @@ test("plugin management bridge is unavailable unless the local development gate await assert.rejects(ipcMain.handlers.get(CHANNELS.list)({}), /runtime is disabled/); }); +test("disabled plugin runtime keeps passive host integration channels quiet", async () => { + const ipcMain = createIpcMain(); + registerPluginBridge(ipcMain, { + manager: null, + env: {}, + isTrustedSender: () => true, + }); + + assert.deepEqual(await ipcMain.handlers.get(CHANNELS.terminalProviders)({}, {}), []); + assert.deepEqual(await ipcMain.handlers.get(CHANNELS.terminalSessionEvent)({}, {}), []); + assert.deepEqual(await ipcMain.handlers.get(CHANNELS.extensionProviders)({}, {}), []); + assert.equal(await ipcMain.handlers.get(CHANNELS.credentialCatalogUpdate)({}, { entries: [] }), 0); + assert.equal(await ipcMain.handlers.get(CHANNELS.setScopeCatalog)({}, {}), null); + assert.deepEqual(await ipcMain.handlers.get(CHANNELS.getScopeCatalog)({}, {}), { + workspace: [], + host: [], + session: [], + device: [{ id: "device", label: "This device" }], + }); +}); + test("plugin management bridge fails closed when the host manager is unavailable", async () => { const ipcMain = createIpcMain(); registerPluginBridge(ipcMain, { diff --git a/electron/plugins/shutdownCoordinator.cjs b/electron/plugins/shutdownCoordinator.cjs index ff23cfdc0..656b37539 100644 --- a/electron/plugins/shutdownCoordinator.cjs +++ b/electron/plugins/shutdownCoordinator.cjs @@ -6,21 +6,21 @@ const { PLUGIN_UTILITY_TERMINATION_GRACE_MS, } = require("./constants.cjs"); -let shutdownHandler = null; +const shutdownHandlers = new Set(); let shutdownPromise = null; function registerPluginShutdown(handler) { if (typeof handler !== "function") throw new TypeError("Plugin shutdown handler must be a function"); - shutdownHandler = handler; + shutdownHandlers.add(handler); shutdownPromise = null; return () => { - if (shutdownHandler === handler) shutdownHandler = null; + shutdownHandlers.delete(handler); }; } function runPluginShutdown(options = {}) { if (shutdownPromise) return shutdownPromise; - if (!shutdownHandler) return Promise.resolve({ timedOut: false }); + if (shutdownHandlers.size === 0) return Promise.resolve({ timedOut: false }); const timeoutMs = options.timeoutMs ?? PLUGIN_DEACTIVATION_TIMEOUT_MS + PLUGIN_UTILITY_TERMINATION_GRACE_MS @@ -31,14 +31,19 @@ function runPluginShutdown(options = {}) { timer = setTimeout(() => resolve({ timedOut: true }), timeoutMs); }); shutdownPromise = Promise.race([ - Promise.resolve().then(shutdownHandler).then(() => ({ timedOut: false })), + Promise.allSettled([...shutdownHandlers].map((handler) => Promise.resolve().then(handler))) + .then((results) => { + const failed = results.find((result) => result.status === "rejected"); + if (failed) throw failed.reason; + return { timedOut: false }; + }), timeout, ]).finally(() => clearTimeout(timer)); return shutdownPromise; } function resetPluginShutdownForTests() { - shutdownHandler = null; + shutdownHandlers.clear(); shutdownPromise = null; } diff --git a/electron/plugins/shutdownCoordinator.test.cjs b/electron/plugins/shutdownCoordinator.test.cjs index 3265f005d..c346a605c 100644 --- a/electron/plugins/shutdownCoordinator.test.cjs +++ b/electron/plugins/shutdownCoordinator.test.cjs @@ -24,3 +24,46 @@ test("plugin shutdown fails open after the bounded quit deadline", async (contex registerPluginShutdown(async () => new Promise(() => {})); assert.deepEqual(await runPluginShutdown({ timeoutMs: 5 }), { timedOut: true }); }); + +test("app shutdown waits for every registered cleanup handler", async (context) => { + context.after(resetPluginShutdownForTests); + const calls = []; + let releasePortForwardCleanup; + const portForwardCleanup = new Promise((resolve) => { releasePortForwardCleanup = resolve; }); + registerPluginShutdown(async () => { calls.push("plugins"); }); + registerPluginShutdown(async () => { + await portForwardCleanup; + calls.push("port-forwards"); + }); + + let settled = false; + const shutdown = runPluginShutdown({ timeoutMs: 1_000 }).then((result) => { + settled = true; + return result; + }); + await new Promise((resolve) => setImmediate(resolve)); + assert.equal(settled, false); + assert.deepEqual(calls, ["plugins"]); + + releasePortForwardCleanup(); + assert.deepEqual(await shutdown, { timedOut: false }); + assert.deepEqual(calls, ["plugins", "port-forwards"]); +}); + +test("one failed cleanup does not let app shutdown abandon other handlers", async (context) => { + context.after(resetPluginShutdownForTests); + let releaseRemainingCleanup; + const remainingCleanup = new Promise((resolve) => { releaseRemainingCleanup = resolve; }); + registerPluginShutdown(async () => { throw new Error("plugin cleanup failed"); }); + registerPluginShutdown(async () => remainingCleanup); + + let settled = false; + const shutdown = runPluginShutdown({ timeoutMs: 1_000 }).finally(() => { + settled = true; + }); + await new Promise((resolve) => setImmediate(resolve)); + assert.equal(settled, false, "failure must not abandon another live cleanup"); + + releaseRemainingCleanup(); + await assert.rejects(shutdown, /plugin cleanup failed/); +}); diff --git a/electron/plugins/terminalDataPipelineService.cjs b/electron/plugins/terminalDataPipelineService.cjs index 119e12e8d..9fc56f827 100644 --- a/electron/plugins/terminalDataPipelineService.cjs +++ b/electron/plugins/terminalDataPipelineService.cjs @@ -75,6 +75,7 @@ class PluginTerminalDataPipelineService { this.finalizedActivations = new WeakSet(); this.permissionGenerations = new Map(); this.sessionEpochs = new Map(); + this.nextSessionEpoch = 0; this.pendingOwnership = new Map(); this.closed = false; this.permissionRevocationSubscription = this.permissionEngine.onDidRevoke?.((event) => { @@ -133,7 +134,7 @@ class PluginTerminalDataPipelineService { ...this.pendingActivations, ...this.failedActivations, ]) { - if ((this.sessionEpochs.get(pending.sessionId) ?? 0) !== pending.sessionEpoch) { + if (this.sessionEpochs.get(pending.sessionId) !== pending.sessionEpoch) { if (this.failedActivations.get(key) === pending) this.failedActivations.delete(key); continue; } @@ -226,7 +227,10 @@ class PluginTerminalDataPipelineService { #disposeSession(sessionId) { this.permissionEngine.revokeSession?.(sessionId); this.pendingOwnership.delete(sessionId); - this.sessionEpochs.set(sessionId, (this.sessionEpochs.get(sessionId) ?? 0) + 1); + // Absence is itself an invalid generation. A later reuse of the same + // session id receives a process-unique token, so late async work from this + // lifetime cannot match the replacement without retaining disposed ids. + this.sessionEpochs.delete(sessionId); this.detachSession(sessionId); for (const direction of DIRECTIONS) { const key = this.#key(sessionId, direction); @@ -242,7 +246,7 @@ class PluginTerminalDataPipelineService { if (!pending || pending.webContentsId !== event?.webContentsId) return; this.pendingOwnership.delete(event.sessionId); if (this.closed - || (this.sessionEpochs.get(event.sessionId) ?? 0) !== pending.sessionEpoch + || this.sessionEpochs.get(event.sessionId) !== pending.sessionEpoch || !this.terminalWorkerManager?.ownsSession?.(event.sessionId, event.webContentsId)) { return; } @@ -298,8 +302,11 @@ class PluginTerminalDataPipelineService { async configureDirection(sessionValue, direction, options = {}) { const session = normalizeTerminalSessionSnapshot(sessionValue); if (!DIRECTIONS.includes(direction)) throw new TypeError("Terminal interceptor direction is invalid"); + if (this.closed) { + throw new PluginRpcError(RPC_ERRORS.unavailable, "Terminal data pipeline is closed"); + } const key = this.#key(session.sessionId, direction); - const epoch = options.sessionEpoch ?? this.sessionEpochs.get(session.sessionId) ?? 0; + const epoch = options.sessionEpoch ?? this.#ensureSessionEpoch(session.sessionId); const previous = this.operations.get(key) ?? Promise.resolve(); const operation = previous.catch(() => {}).then(() => ( this.#configureDirection(session, direction, { ...options, sessionEpoch: epoch }) @@ -312,11 +319,21 @@ class PluginTerminalDataPipelineService { } #assertSessionCurrent(sessionId, epoch) { - if (this.closed || (this.sessionEpochs.get(sessionId) ?? 0) !== epoch) { + if (this.closed || this.sessionEpochs.get(sessionId) !== epoch) { throw new PluginRpcError(RPC_ERRORS.unavailable, "Terminal session changed during interceptor activation"); } } + #replaceSessionEpoch(sessionId) { + this.nextSessionEpoch += 1; + this.sessionEpochs.set(sessionId, this.nextSessionEpoch); + return this.nextSessionEpoch; + } + + #ensureSessionEpoch(sessionId) { + return this.sessionEpochs.get(sessionId) ?? this.#replaceSessionEpoch(sessionId); + } + #assertAttachmentCurrent(session, direction, providerId, identity, options) { this.#assertSessionCurrent(session.sessionId, options.sessionEpoch); if (options.webContentsId != null @@ -430,7 +447,7 @@ class PluginTerminalDataPipelineService { try { activation = await this.contributionService.activateProvider(providerId); } catch (error) { - if ((this.sessionEpochs.get(session.sessionId) ?? 0) === options.sessionEpoch + if (this.sessionEpochs.get(session.sessionId) === options.sessionEpoch && !this.quarantined.has(key) && !this.finalizedActivations.has(pendingActivation)) { this.failedActivations.set(key, pendingActivation); @@ -615,14 +632,14 @@ class PluginTerminalDataPipelineService { async handleSessionEvent(event, options = {}) { const session = normalizeTerminalSessionSnapshot(event?.session); + if (this.closed) return Object.freeze([]); if (!this.acceptsSessionEvent(event, options.webContentsId)) return Object.freeze([]); if (event?.type === "disposed") { this.#disposeSession(session.sessionId); return Object.freeze([]); } if (event?.type === "disconnected") { - const sessionEpoch = (this.sessionEpochs.get(session.sessionId) ?? 0) + 1; - this.sessionEpochs.set(session.sessionId, sessionEpoch); + const sessionEpoch = this.#replaceSessionEpoch(session.sessionId); this.detachSession(session.sessionId, "session-disconnected"); if (Number.isSafeInteger(options.webContentsId)) { this.pendingOwnership.set(session.sessionId, Object.freeze({ @@ -649,10 +666,9 @@ class PluginTerminalDataPipelineService { this.selectedProviders.delete(key); this.failedActivations.delete(key); } - this.sessionEpochs.set(session.sessionId, (this.sessionEpochs.get(session.sessionId) ?? 0) + 1); + this.#replaceSessionEpoch(session.sessionId); } - if (!this.sessionEpochs.has(session.sessionId)) this.sessionEpochs.set(session.sessionId, 0); - const sessionEpoch = this.sessionEpochs.get(session.sessionId) ?? 0; + const sessionEpoch = this.#ensureSessionEpoch(session.sessionId); if (event.type === "created" && options.webContentsId != null) { this.pendingOwnership.set(session.sessionId, Object.freeze({ session, @@ -677,7 +693,7 @@ class PluginTerminalDataPipelineService { } const key = this.#key(session.sessionId, direction); if (error?.code === RPC_ERRORS.cancelled - || (this.sessionEpochs.get(session.sessionId) ?? 0) !== sessionEpoch + || this.sessionEpochs.get(session.sessionId) !== sessionEpoch || this.quarantined.has(key)) { results.push(Object.freeze({ status: "cancelled", direction })); continue; @@ -700,9 +716,6 @@ class PluginTerminalDataPipelineService { shutdown() { this.closed = true; - for (const sessionId of this.sessionEpochs.keys()) { - this.sessionEpochs.set(sessionId, (this.sessionEpochs.get(sessionId) ?? 0) + 1); - } for (const key of [...this.active.keys()]) this.#detachKey(key, "shutdown"); this.declined.clear(); this.quarantined.clear(); @@ -710,6 +723,7 @@ class PluginTerminalDataPipelineService { this.pendingActivations.clear(); this.failedActivations.clear(); this.pendingOwnership.clear(); + this.sessionEpochs.clear(); this.workerWarningSubscription?.dispose?.(); this.sessionOwnedSubscription?.dispose?.(); this.sessionClosedSubscription?.dispose?.(); @@ -719,6 +733,10 @@ class PluginTerminalDataPipelineService { this.permissionRevocationSubscription?.dispose?.(); this.permissionRevocationSubscription = null; } + + _getSessionEpochCountForTests() { + return this.sessionEpochs.size; + } } module.exports = { diff --git a/electron/plugins/terminalDataPipelineService.test.cjs b/electron/plugins/terminalDataPipelineService.test.cjs index 0df3be8dc..b466511a1 100644 --- a/electron/plugins/terminalDataPipelineService.test.cjs +++ b/electron/plugins/terminalDataPipelineService.test.cjs @@ -918,3 +918,16 @@ test("contribution changes do not clear a failed interceptor quarantine", async assert.equal(h.authorized.length, 2); assert.equal(h.attached.length, 2); }); + +test("disposed terminal sessions do not retain lifecycle generations", async () => { + const h = harness({ providers: [] }); + for (let index = 0; index < 2_000; index += 1) { + const nextSession = { + ...session, + sessionId: `disposed-session-${index}`, + }; + await h.service.handleSessionEvent({ type: "created", session: nextSession }); + await h.service.handleSessionEvent({ type: "disposed", session: nextSession }); + } + assert.equal(h.service._getSessionEpochCountForTests(), 0); +}); diff --git a/electron/preload.cjs b/electron/preload.cjs index a8fa90fe2..0b8e55ed9 100644 --- a/electron/preload.cjs +++ b/electron/preload.cjs @@ -1,6 +1,7 @@ const { ipcRenderer, contextBridge, webUtils } = require("electron"); const os = require("node:os"); const { randomUUID } = require("node:crypto"); +const { SessionTombstones } = require("./preload/sessionTombstones.cjs"); const { createPreloadApi } = require("./preload/api.cjs"); const { clearTerminalDataBacklog, @@ -23,9 +24,10 @@ const { const dataListeners = new Map(); const displayDataListeners = new Map(); const terminalDataBacklog = createTerminalDataBacklog(); -const closedTerminalDataSessions = new Set(); +const closedTerminalDataSessions = new SessionTombstones(); const exitListeners = new Map(); const globalSftpTransferListeners = new Set(); +const pluginContributionsChangedListeners = new Set(); const chainProgressListeners = new Map(); const connectionReuseFallbackListeners = new Set(); const zmodemListeners = new Map(); @@ -69,6 +71,16 @@ function dispatchGlobalSftpTransferEvent(payload) { } } +function dispatchPluginContributionsChanged(payload) { + for (const cb of pluginContributionsChangedListeners) { + try { + cb(payload); + } catch (err) { + console.error("Plugin contributions callback failed", err); + } + } +} + // ── MCP marker filter with per-session line buffering ── // PTY data arrives in arbitrary chunks; the marker string (__NCMCP_) can be // split across chunk boundaries so a simple data.includes() guard misses it. @@ -80,6 +92,18 @@ const _mcpLineMetas = new Map(); // sessionId -> trailing fragment metadata const _mcpPendingMetas = new Map(); // sessionId -> metadata from filtered-empty chunks const _mcpFlushTimers = new Map(); // sessionId -> delayed-flush timer const _mcpDroppingWrappedLine = new Set(); // sessionIds with a split marker echo line in progress +const MAX_MCP_BUFFERED_LINE_CHARS = 64 * 1024; + +function clearMcpSessionState(sessionId) { + if (!sessionId) return; + const pendingTimer = _mcpFlushTimers.get(sessionId); + if (pendingTimer) clearTimeout(pendingTimer); + _mcpFlushTimers.delete(sessionId); + _mcpLineBufs.delete(sessionId); + _mcpLineMetas.delete(sessionId); + _mcpPendingMetas.delete(sessionId); + _mcpDroppingWrappedLine.delete(sessionId); +} // Returns true if `s` ends with a non-empty prefix of "__NCMCP_" // (i.e. the next chunk might complete it into a marker-containing line). @@ -143,12 +167,22 @@ function filterMcpChunk(sessionId, chunk, meta) { // contain __NCMCP_ would otherwise leak through as garbage. const tail = data.slice(pos); if (droppedAny || tail.includes("__NCMCP_") || _endsWithMarkerPrefix(tail)) { - _mcpLineBufs.set(sessionId, tail); let tailMeta = !held && tail === chunk ? sameChunkMeta : stateMeta; if (heldIngressAlreadyAcknowledged && !hasPluginPipelineIngress(tailMeta)) { tailMeta = { ...(tailMeta || {}), pluginPipelineIngressBytes: 0 }; } - if (tailMeta) _mcpLineMetas.set(sessionId, tailMeta); + if (tail.length <= MAX_MCP_BUFFERED_LINE_CHARS) { + _mcpLineBufs.set(sessionId, tail); + if (tailMeta) _mcpLineMetas.set(sessionId, tailMeta); + } else { + // A malformed/wrapped marker line can otherwise grow forever while + // high-rate output keeps postponing the delayed flush. Its visible + // content is already being suppressed, so discard the oversized + // prefix and retain only the drop-until-newline state. + _mcpLineBufs.delete(sessionId); + _mcpLineMetas.delete(sessionId); + _mcpDroppingWrappedLine.add(sessionId); + } if (droppedAny) _mcpDroppingWrappedLine.add(sessionId); } else { result += tail; // safe to display immediately @@ -374,15 +408,8 @@ ipcRenderer.on("netcatty:exit", (_event, payload) => { telnetAutoLoginCancelledListeners.delete(sessionId); telnetEchoModeListeners.delete(sessionId); moshSessionReadyListeners.delete(sessionId); - const pendingTimer = _mcpFlushTimers.get(sessionId); - if (pendingTimer) { - clearTimeout(pendingTimer); - _mcpFlushTimers.delete(sessionId); - } - _mcpLineBufs.delete(sessionId); // clean up any held fragment - _mcpLineMetas.delete(sessionId); - _mcpPendingMetas.delete(sessionId); - _mcpDroppingWrappedLine.delete(sessionId); + authFailedListeners.delete(sessionId); + clearMcpSessionState(sessionId); }); // Chain progress events (for jump host connections) @@ -674,104 +701,8 @@ ipcRenderer.on("netcatty:sftp:global-transfer", (_event, payload) => { dispatchGlobalSftpTransferEvent(payload); }); -// Upload with progress listeners -const uploadProgressListeners = new Map(); -const uploadCompleteListeners = new Map(); -const uploadErrorListeners = new Map(); - -// Compress upload listeners -const compressProgressListeners = new Map(); -const compressCompleteListeners = new Map(); -const compressErrorListeners = new Map(); - -ipcRenderer.on("netcatty:upload:progress", (_event, payload) => { - const cb = uploadProgressListeners.get(payload.transferId); - if (cb) { - try { - cb(payload.transferred, payload.totalBytes, payload.speed); - } catch (err) { - console.error("Upload progress callback failed", err); - } - } -}); - -ipcRenderer.on("netcatty:upload:complete", (_event, payload) => { - const cb = uploadCompleteListeners.get(payload.transferId); - if (cb) { - try { - cb(); - } catch (err) { - console.error("Upload complete callback failed", err); - } - } - // Cleanup listeners - uploadProgressListeners.delete(payload.transferId); - uploadCompleteListeners.delete(payload.transferId); - uploadErrorListeners.delete(payload.transferId); -}); - -ipcRenderer.on("netcatty:upload:error", (_event, payload) => { - const cb = uploadErrorListeners.get(payload.transferId); - if (cb) { - try { - cb(payload.error); - } catch (err) { - console.error("Upload error callback failed", err); - } - } - // Cleanup listeners - uploadProgressListeners.delete(payload.transferId); - uploadCompleteListeners.delete(payload.transferId); - uploadErrorListeners.delete(payload.transferId); -}); - -// Compress upload events -ipcRenderer.on("netcatty:compress:progress", (_event, payload) => { - const cb = compressProgressListeners.get(payload.compressionId); - if (cb) { - try { - cb(payload.phase, payload.transferred, payload.total); - } catch (err) { - console.error("Compress progress callback failed", err); - } - } -}); - -ipcRenderer.on("netcatty:compress:complete", (_event, payload) => { - const cb = compressCompleteListeners.get(payload.compressionId); - if (cb) { - try { - cb(); - } catch (err) { - console.error("Compress complete callback failed", err); - } - } - // Cleanup listeners - compressProgressListeners.delete(payload.compressionId); - compressCompleteListeners.delete(payload.compressionId); - compressErrorListeners.delete(payload.compressionId); -}); - -ipcRenderer.on("netcatty:compress:error", (_event, payload) => { - const cb = compressErrorListeners.get(payload.compressionId); - if (cb) { - try { - cb(payload.error); - } catch (err) { - console.error("Compress error callback failed", err); - } - } - // Cleanup listeners - compressProgressListeners.delete(payload.compressionId); - compressCompleteListeners.delete(payload.compressionId); - compressErrorListeners.delete(payload.compressionId); -}); - -ipcRenderer.on("netcatty:compress:cancelled", (_event, payload) => { - // Just cleanup listeners, the UI already knows it's cancelled - compressProgressListeners.delete(payload.compressionId); - compressCompleteListeners.delete(payload.compressionId); - compressErrorListeners.delete(payload.compressionId); +ipcRenderer.on("netcatty:plugins:contributions-changed", (_event, payload) => { + dispatchPluginContributionsChanged(payload); }); // Port forwarding status listeners @@ -794,6 +725,7 @@ ipcRenderer.on("netcatty:portforward:status", (_event, payload) => { // File watcher listeners (for auto-sync feature) const fileWatchSyncedListeners = new Set(); const fileWatchErrorListeners = new Set(); +const fileWatchStoppedListeners = new Set(); ipcRenderer.on("netcatty:filewatch:synced", (_event, payload) => { fileWatchSyncedListeners.forEach((cb) => { @@ -815,6 +747,16 @@ ipcRenderer.on("netcatty:filewatch:error", (_event, payload) => { }); }); +ipcRenderer.on("netcatty:filewatch:stopped", (_event, payload) => { + fileWatchStoppedListeners.forEach((cb) => { + try { + cb(payload); + } catch (err) { + console.error("File watch stopped callback failed", err); + } + }); +}); + // Buffer the latest tray menu data so it can be replayed when the React // component subscribes after lazy-mount (avoiding the first-open race). let _lastTrayMenuData = null; @@ -832,6 +774,7 @@ const api = createPreloadApi({ exitListeners, closedTerminalDataSessions, globalSftpTransferListeners, + pluginContributionsChangedListeners, chainProgressListeners, connectionReuseFallbackListeners, zmodemListeners, @@ -844,6 +787,7 @@ const api = createPreloadApi({ moshSessionReadyListeners, terminalDataBacklog, terminalOutputPorts, + clearTerminalOutputSessionState: clearMcpSessionState, terminalOutputDrainListeners, terminalUrgentInputPorts, languageChangeListeners, @@ -865,15 +809,10 @@ const api = createPreloadApi({ updateErrorListeners, updateNeedsSaveListeners, terminalPopupConfigState, - uploadProgressListeners, - uploadCompleteListeners, - uploadErrorListeners, - compressProgressListeners, - compressCompleteListeners, - compressErrorListeners, portForwardStatusListeners, fileWatchSyncedListeners, fileWatchErrorListeners, + fileWatchStoppedListeners, get _lastTrayMenuData() { return _lastTrayMenuData; }, set _lastTrayMenuData(value) { _lastTrayMenuData = value; }, }); diff --git a/electron/preload/api.cjs b/electron/preload/api.cjs index def2ef421..0feae3382 100644 --- a/electron/preload/api.cjs +++ b/electron/preload/api.cjs @@ -3,12 +3,16 @@ const { hasPluginPipelineIngressMarker, } = require("./terminalDataBacklog.cjs"); const { randomUUID } = require("node:crypto"); +const fs = require("node:fs"); +const { stageRendererFileToTemp } = require("./stageUploadFile.cjs"); function createPreloadApi(ctx) { const terminalDataBacklog = ctx.terminalDataBacklog || null; const displayDataListeners = ctx.displayDataListeners || new Map(); const closedTerminalDataSessions = ctx.closedTerminalDataSessions || null; const globalSftpTransferListeners = ctx.globalSftpTransferListeners || new Set(); + const pluginContributionsChangedListeners = ctx.pluginContributionsChangedListeners || new Set(); + const uploadStageControllers = new Map(); // Lightweight test contexts may omit this map; default so closeSession never throws. if (!ctx.moshSessionReadyListeners) { ctx.moshSessionReadyListeners = new Map(); @@ -23,6 +27,7 @@ function createPreloadApi(ctx) { const markTerminalDataSessionClosed = (sessionId) => { if (!sessionId) return; closedTerminalDataSessions?.add?.(sessionId); + ctx.clearTerminalOutputSessionState?.(sessionId); clearTerminalDataSession({ dataListeners: ctx.dataListeners, displayDataListeners, @@ -30,6 +35,18 @@ function createPreloadApi(ctx) { }, sessionId); ctx.terminalOutputPorts?.closeSession?.(sessionId); }; + const removeSessionListener = (listeners, sessionId, cb) => { + const set = listeners?.get?.(sessionId); + set?.delete(cb); + if (set?.size === 0) listeners.delete(sessionId); + }; + const clearSessionScopedTerminalListeners = (sessionId) => { + ctx.telnetAutoLoginCompleteListeners?.delete?.(sessionId); + ctx.telnetAutoLoginCancelledListeners?.delete?.(sessionId); + ctx.moshSessionReadyListeners?.delete?.(sessionId); + ctx.telnetEchoModeListeners?.delete?.(sessionId); + ctx.authFailedListeners?.delete?.(sessionId); + }; const sanitizeInterruptTrace = (trace) => { if (!trace || typeof trace !== "object") return undefined; const priority = trace.rendererPriority && typeof trace.rendererPriority === "object" @@ -173,9 +190,8 @@ function createPreloadApi(ctx) { }), postPluginViewMessage: (instanceId, message) => ipcRenderer.invoke("netcatty:plugins:view-message", { instanceId, message }), onPluginContributionsChanged: (callback) => { - const listener = (_event, payload) => callback(payload); - ipcRenderer.on("netcatty:plugins:contributions-changed", listener); - return () => ipcRenderer.removeListener("netcatty:plugins:contributions-changed", listener); + pluginContributionsChangedListeners.add(callback); + return () => pluginContributionsChangedListeners.delete(callback); }, onPluginViewMessage: (callback) => { const listener = (_event, payload) => callback(payload); @@ -431,10 +447,9 @@ function createPreloadApi(ctx) { }, closeSession: async (sessionId) => { markTerminalDataSessionClosed(sessionId); - telnetEchoModeListeners.delete(sessionId); - // closeSession sets session.closed before kill; mosh exit handlers skip - // the exit event in that case, so clear ready listeners here too. - moshSessionReadyListeners.delete(sessionId); + // closeSession sets session.closed before kill, so some protocol-specific + // exit events can be skipped. Release every session-scoped listener here. + clearSessionScopedTerminalListeners(sessionId); try { await ipcRenderer.invoke("netcatty:close:await", { sessionId }); } catch { @@ -614,35 +629,35 @@ function createPreloadApi(ctx) { telnetAutoLoginCompleteListeners.set(sessionId, new Set()); } telnetAutoLoginCompleteListeners.get(sessionId).add(cb); - return () => telnetAutoLoginCompleteListeners.get(sessionId)?.delete(cb); + return () => removeSessionListener(telnetAutoLoginCompleteListeners, sessionId, cb); }, onTelnetAutoLoginCancelled: (sessionId, cb) => { if (!telnetAutoLoginCancelledListeners.has(sessionId)) { telnetAutoLoginCancelledListeners.set(sessionId, new Set()); } telnetAutoLoginCancelledListeners.get(sessionId).add(cb); - return () => telnetAutoLoginCancelledListeners.get(sessionId)?.delete(cb); + return () => removeSessionListener(telnetAutoLoginCancelledListeners, sessionId, cb); }, onMoshSessionReady: (sessionId, cb) => { if (!moshSessionReadyListeners.has(sessionId)) { moshSessionReadyListeners.set(sessionId, new Set()); } moshSessionReadyListeners.get(sessionId).add(cb); - return () => moshSessionReadyListeners.get(sessionId)?.delete(cb); + return () => removeSessionListener(moshSessionReadyListeners, sessionId, cb); }, onTelnetEchoMode: (sessionId, cb) => { if (!telnetEchoModeListeners.has(sessionId)) { telnetEchoModeListeners.set(sessionId, new Set()); } telnetEchoModeListeners.get(sessionId).add(cb); - return () => telnetEchoModeListeners.get(sessionId)?.delete(cb); + return () => removeSessionListener(telnetEchoModeListeners, sessionId, cb); }, getTelnetEchoMode: (sessionId) => ipcRenderer.invoke("netcatty:telnet:getEchoMode", { sessionId }), onAuthFailed: (sessionId, cb) => { if (!authFailedListeners.has(sessionId)) authFailedListeners.set(sessionId, new Set()); authFailedListeners.get(sessionId).add(cb); - return () => authFailedListeners.get(sessionId)?.delete(cb); + return () => removeSessionListener(authFailedListeners, sessionId, cb); }, // Keyboard-interactive authentication (2FA/MFA) onKeyboardInteractive: (cb) => { @@ -706,13 +721,19 @@ function createPreloadApi(ctx) { const result = await ipcRenderer.invoke("netcatty:sftp:open", options); return result.sftpId; }, - openSftpForSession: async (sessionId) => { - const result = await ipcRenderer.invoke("netcatty:sftp:openForSession", { sessionId }); + openSftpForSession: async (sessionId, expectedEndpoint) => { + const result = await ipcRenderer.invoke("netcatty:sftp:openForSession", { + sessionId, + expectedEndpoint, + }); return result.sftpId; }, listSftp: async (sftpId, path, encoding) => { return ipcRenderer.invoke("netcatty:sftp:list", { sftpId, path, encoding }); }, + realpathSftp: async (sftpId, path, encoding) => { + return ipcRenderer.invoke("netcatty:sftp:realpath", { sftpId, path, encoding }); + }, readSftp: async (sftpId, path, encoding) => { return ipcRenderer.invoke("netcatty:sftp:read", { sftpId, path, encoding }); }, @@ -746,29 +767,6 @@ function createPreloadApi(ctx) { getSftpHomeDir: async (sftpId, encoding) => { return ipcRenderer.invoke("netcatty:sftp:homeDir", { sftpId, encoding }); }, - // Write binary with real-time progress callback - writeSftpBinaryWithProgress: async (sftpId, path, content, transferId, encoding, onProgress, onComplete, onError) => { - // Register callbacks - if (onProgress) uploadProgressListeners.set(transferId, onProgress); - if (onComplete) uploadCompleteListeners.set(transferId, onComplete); - if (onError) uploadErrorListeners.set(transferId, onError); - - return ipcRenderer.invoke("netcatty:sftp:writeBinaryWithProgress", { - sftpId, - path, - content, - transferId, - encoding, - }); - }, - // Cancel an in-progress SFTP upload - cancelSftpUpload: async (transferId) => { - // Cleanup listeners - uploadProgressListeners.delete(transferId); - uploadCompleteListeners.delete(transferId); - uploadErrorListeners.delete(transferId); - return ipcRenderer.invoke("netcatty:sftp:cancelUpload", { transferId }); - }, // Local filesystem operations listLocalDir: async (path) => { return ipcRenderer.invoke("netcatty:local:list", { path }); @@ -860,20 +858,10 @@ function createPreloadApi(ctx) { return ipcRenderer.invoke("netcatty:transfer:same-host-copy-dir", { sftpId, sourcePath, targetPath, encoding, transferId }); }, // Compressed folder upload - startCompressedUpload: async (options, onProgress, onComplete, onError) => { - const { compressionId } = options; - // Register callbacks - if (onProgress) compressProgressListeners.set(compressionId, onProgress); - if (onComplete) compressCompleteListeners.set(compressionId, onComplete); - if (onError) compressErrorListeners.set(compressionId, onError); - + startCompressedUpload: async (options) => { return ipcRenderer.invoke("netcatty:compress:start", options); }, cancelCompressedUpload: async (compressionId) => { - // Cleanup listeners - compressProgressListeners.delete(compressionId); - compressCompleteListeners.delete(compressionId); - compressErrorListeners.delete(compressionId); return ipcRenderer.invoke("netcatty:compress:cancel", { compressionId }); }, pauseCompressedUpload: async (compressionId) => { @@ -1157,8 +1145,6 @@ function createPreloadApi(ctx) { ipcRenderer.invoke("netcatty:openWithApplication", { filePath, appPath }), openWithSystemDefault: (filePath) => ipcRenderer.invoke("netcatty:openWithSystemDefault", { filePath }), - downloadSftpToTemp: (sftpId, remotePath, fileName, encoding) => - ipcRenderer.invoke("netcatty:sftp:downloadToTemp", { sftpId, remotePath, fileName, encoding }), downloadSftpToTempWithProgress: (sftpId, remotePath, fileName, encoding, transferId) => ipcRenderer.invoke("netcatty:sftp:downloadToTempWithProgress", { sftpId, @@ -1185,6 +1171,8 @@ function createPreloadApi(ctx) { ipcRenderer.invoke("netcatty:filewatch:list"), registerTempFile: (sftpId, localPath) => ipcRenderer.invoke("netcatty:filewatch:registerTempFile", { sftpId, localPath }), + unregisterTempFile: (sftpId, localPath) => + ipcRenderer.invoke("netcatty:filewatch:unregisterTempFile", { sftpId, localPath }), onFileWatchSynced: (cb) => { fileWatchSyncedListeners.add(cb); return () => fileWatchSyncedListeners.delete(cb); @@ -1193,6 +1181,10 @@ function createPreloadApi(ctx) { fileWatchErrorListeners.add(cb); return () => fileWatchErrorListeners.delete(cb); }, + onFileWatchStopped: (cb) => { + fileWatchStoppedListeners.add(cb); + return () => fileWatchStoppedListeners.delete(cb); + }, // Temp file cleanup deleteTempFile: (filePath) => @@ -1344,6 +1336,31 @@ function createPreloadApi(ctx) { return undefined; } }, + stageUploadFile: async (file, transferId) => { + uploadStageControllers.get(transferId)?.abort(new Error("Upload staging superseded")); + const controller = new AbortController(); + uploadStageControllers.set(transferId, controller); + const localPath = await ipcRenderer.invoke("netcatty:tempdir:createUploadPath", { + transferId, + fileName: file?.name || "upload.bin", + }); + try { + return await stageRendererFileToTemp(file, localPath, fs, controller.signal); + } catch (error) { + await ipcRenderer.invoke("netcatty:deleteTempFile", { filePath: localPath }).catch(() => {}); + throw error; + } finally { + if (uploadStageControllers.get(transferId) === controller) { + uploadStageControllers.delete(transferId); + } + } + }, + cancelStagedUploadFile: async (transferId) => { + const controller = uploadStageControllers.get(transferId); + if (!controller) return { success: false }; + controller.abort(new Error("Upload staging cancelled")); + return { success: true }; + }, // Clipboard fallback helpers readClipboardText: async () => { diff --git a/electron/preload/sessionTombstones.cjs b/electron/preload/sessionTombstones.cjs new file mode 100644 index 000000000..ddb772c91 --- /dev/null +++ b/electron/preload/sessionTombstones.cjs @@ -0,0 +1,59 @@ +"use strict"; + +const DEFAULT_SESSION_TOMBSTONE_TTL_MS = 5 * 60_000; +const DEFAULT_MAX_SESSION_TOMBSTONES = 4096; + +class SessionTombstones { + constructor(options = {}) { + this.maxEntries = options.maxEntries ?? DEFAULT_MAX_SESSION_TOMBSTONES; + this.ttlMs = options.ttlMs ?? DEFAULT_SESSION_TOMBSTONE_TTL_MS; + this.now = options.now ?? (() => Date.now()); + this.entries = new Map(); + } + + prune() { + const now = this.now(); + for (const [sessionId, closedAt] of this.entries) { + if (now - closedAt < this.ttlMs) break; + this.entries.delete(sessionId); + } + while (this.entries.size > this.maxEntries) { + const oldest = this.entries.keys().next().value; + if (oldest === undefined) break; + this.entries.delete(oldest); + } + } + + add(sessionId) { + if (!sessionId) return this; + this.entries.delete(sessionId); + this.entries.set(sessionId, this.now()); + this.prune(); + return this; + } + + delete(sessionId) { + return this.entries.delete(sessionId); + } + + has(sessionId) { + this.prune(); + return this.entries.has(sessionId); + } + + get size() { + this.prune(); + return this.entries.size; + } + + [Symbol.iterator]() { + this.prune(); + return this.entries.keys(); + } +} + +module.exports = { + DEFAULT_SESSION_TOMBSTONE_TTL_MS, + DEFAULT_MAX_SESSION_TOMBSTONES, + SessionTombstones, +}; diff --git a/electron/preload/sessionTombstones.test.cjs b/electron/preload/sessionTombstones.test.cjs new file mode 100644 index 000000000..a01d7ba13 --- /dev/null +++ b/electron/preload/sessionTombstones.test.cjs @@ -0,0 +1,32 @@ +"use strict"; + +const assert = require("node:assert/strict"); +const test = require("node:test"); +const { SessionTombstones } = require("./sessionTombstones.cjs"); + +test("closed session tombstones stay bounded across unique session ids", () => { + const tombstones = new SessionTombstones({ maxEntries: 3, ttlMs: 10_000, now: () => 0 }); + for (let index = 0; index < 10; index += 1) tombstones.add(`session-${index}`); + + assert.equal(tombstones.size, 3); + assert.equal(tombstones.has("session-0"), false); + assert.equal(tombstones.has("session-9"), true); +}); + +test("closed session tombstones reject late events only during the safety window", () => { + let now = 0; + const tombstones = new SessionTombstones({ maxEntries: 10, ttlMs: 100, now: () => now }); + tombstones.add("session-1"); + assert.equal(tombstones.has("session-1"), true); + + now = 101; + assert.equal(tombstones.has("session-1"), false); + assert.equal(tombstones.size, 0); +}); + +test("reopening the same session removes its tombstone", () => { + const tombstones = new SessionTombstones(); + tombstones.add("session-1"); + tombstones.delete("session-1"); + assert.equal(tombstones.has("session-1"), false); +}); diff --git a/electron/preload/stageUploadFile.cjs b/electron/preload/stageUploadFile.cjs new file mode 100644 index 000000000..0731134c1 --- /dev/null +++ b/electron/preload/stageUploadFile.cjs @@ -0,0 +1,45 @@ +"use strict"; + +async function stageRendererFileToTemp(file, localPath, fsImpl, signal = null) { + if (!file || typeof file.stream !== "function") { + throw new Error("Upload file streaming is unavailable"); + } + let handle = null; + let reader = null; + const cancellationError = () => ( + signal?.reason instanceof Error ? signal.reason : new Error("Upload staging cancelled") + ); + const onAbort = () => { + void Promise.resolve(reader?.cancel?.(cancellationError())).catch(() => {}); + void handle?.close?.().catch(() => {}); + }; + try { + handle = await fsImpl.promises.open(localPath, "wx", 0o600); + // Stream/getReader can throw synchronously (revoked File, renderer teardown, + // or a malformed provider). Initialize them inside the cleanup boundary so + // the just-created file handle is never stranded. + reader = file.stream().getReader(); + if (signal?.aborted) throw cancellationError(); + signal?.addEventListener?.("abort", onAbort, { once: true }); + while (true) { + if (signal?.aborted) throw cancellationError(); + const { done, value } = await reader.read(); + if (signal?.aborted) throw cancellationError(); + if (done) break; + if (!value) continue; + const chunk = Buffer.from(value.buffer, value.byteOffset, value.byteLength); + await handle.write(chunk); + } + return localPath; + } catch (error) { + await handle?.close?.().catch(() => {}); + await fsImpl.promises.unlink(localPath).catch(() => {}); + throw error; + } finally { + signal?.removeEventListener?.("abort", onAbort); + reader?.releaseLock?.(); + await handle?.close?.().catch(() => {}); + } +} + +module.exports = { stageRendererFileToTemp }; diff --git a/electron/preload/stageUploadFile.test.cjs b/electron/preload/stageUploadFile.test.cjs new file mode 100644 index 000000000..97f4a5e27 --- /dev/null +++ b/electron/preload/stageUploadFile.test.cjs @@ -0,0 +1,131 @@ +"use strict"; + +const assert = require("node:assert/strict"); +const fs = require("node:fs"); +const os = require("node:os"); +const path = require("node:path"); +const test = require("node:test"); +const { stageRendererFileToTemp } = require("./stageUploadFile.cjs"); + +test("pathless renderer files stream into a controlled temp file without arrayBuffer", async (t) => { + const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-stage-upload-")); + t.after(() => fs.promises.rm(dir, { recursive: true, force: true })); + const localPath = path.join(dir, "upload.part"); + let arrayBufferCalls = 0; + const chunks = [new Uint8Array([1, 2]), new Uint8Array([3, 4, 5])]; + const file = { + arrayBuffer: async () => { arrayBufferCalls += 1; return new ArrayBuffer(0); }, + stream: () => new ReadableStream({ + start(controller) { + for (const chunk of chunks) controller.enqueue(chunk); + controller.close(); + }, + }), + }; + assert.equal(await stageRendererFileToTemp(file, localPath, fs), localPath); + assert.deepEqual(await fs.promises.readFile(localPath), Buffer.from([1, 2, 3, 4, 5])); + assert.equal(arrayBufferCalls, 0); +}); + +test("failed pathless-file staging removes its partial temp file", async (t) => { + const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-stage-upload-fail-")); + t.after(() => fs.promises.rm(dir, { recursive: true, force: true })); + const localPath = path.join(dir, "upload.part"); + let reads = 0; + const file = { + stream: () => ({ + getReader: () => ({ + read: async () => { + reads += 1; + if (reads === 1) return { done: false, value: new Uint8Array([1]) }; + throw new Error("source failed"); + }, + releaseLock: () => {}, + }), + }), + }; + await assert.rejects(stageRendererFileToTemp(file, localPath, fs), /source failed/); + await assert.rejects(fs.promises.stat(localPath), { code: "ENOENT" }); +}); + +test("a synchronous File.stream failure closes the new handle and removes the temp file", async (t) => { + const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-stage-upload-stream-init-")); + t.after(() => fs.promises.rm(dir, { recursive: true, force: true })); + const localPath = path.join(dir, "upload.part"); + + await assert.rejects(stageRendererFileToTemp({ + stream() { + throw new Error("stream init failed"); + }, + }, localPath, fs), /stream init failed/); + + await assert.rejects(fs.promises.stat(localPath), { code: "ENOENT" }); +}); + +test("a synchronous getReader failure closes the new handle and removes the temp file", async (t) => { + const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-stage-upload-reader-init-")); + t.after(() => fs.promises.rm(dir, { recursive: true, force: true })); + const localPath = path.join(dir, "upload.part"); + + await assert.rejects(stageRendererFileToTemp({ + stream: () => ({ + getReader() { + throw new Error("reader init failed"); + }, + }), + }, localPath, fs), /reader init failed/); + + await assert.rejects(fs.promises.stat(localPath), { code: "ENOENT" }); +}); + +test("stream initialization failure still closes the handle when temp deletion fails", async () => { + let closeCalls = 0; + let unlinkCalls = 0; + const fakeFs = { + promises: { + open: async () => ({ + close: async () => { closeCalls += 1; }, + }), + unlink: async () => { + unlinkCalls += 1; + throw new Error("delete denied"); + }, + }, + }; + + await assert.rejects(stageRendererFileToTemp({ + stream() { + throw new Error("stream init failed"); + }, + }, "/controlled/upload.part", fakeFs), /stream init failed/); + + assert.equal(unlinkCalls, 1); + assert.ok(closeCalls >= 1, "the file handle must close even if cleanup unlink fails"); +}); + +test("cancelling a blocked pathless-file read closes it and removes the partial file", async (t) => { + const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "netcatty-stage-upload-cancel-")); + t.after(() => fs.promises.rm(dir, { recursive: true, force: true })); + const localPath = path.join(dir, "upload.part"); + const controller = new AbortController(); + let cancelCalls = 0; + let finishRead; + const file = { + stream: () => ({ + getReader: () => ({ + read: () => new Promise((resolve) => { finishRead = resolve; }), + cancel: async () => { + cancelCalls += 1; + finishRead?.({ done: true }); + }, + releaseLock: () => {}, + }), + }), + }; + const staging = stageRendererFileToTemp(file, localPath, fs, controller.signal); + while (!finishRead) await new Promise((resolve) => setImmediate(resolve)); + controller.abort(new Error("cancel now")); + await assert.rejects(staging, /cancel now/); + assert.equal(cancelCalls, 1); + await assert.rejects(fs.promises.stat(localPath), { code: "ENOENT" }); +}); diff --git a/electron/preloadDataBacklog.test.cjs b/electron/preloadDataBacklog.test.cjs index 8b064afa7..e703aef8b 100644 --- a/electron/preloadDataBacklog.test.cjs +++ b/electron/preloadDataBacklog.test.cjs @@ -31,11 +31,13 @@ function createFakePort() { function loadPreloadWithFakeElectron() { const handlers = new Map(); + const listenerCounts = new Map(); let exposedApi = null; const fakeElectron = { ipcRenderer: { on(channel, handler) { handlers.set(channel, handler); + listenerCounts.set(channel, (listenerCounts.get(channel) || 0) + 1); }, send() {}, async invoke(channel, payload) { @@ -79,6 +81,7 @@ function loadPreloadWithFakeElectron() { return { api: exposedApi, handlers, + listenerCounts, cleanup() { delete require.cache[preloadPath]; if (previousElectron) { @@ -95,6 +98,31 @@ function loadPreloadWithFakeElectron() { }; } +test("plugin contribution subscribers share one IPC listener", (t) => { + const preload = loadPreloadWithFakeElectron(); + t.after(preload.cleanup); + const observed = Array.from({ length: 20 }, () => 0); + const unsubscribes = observed.map((_value, index) => ( + preload.api.onPluginContributionsChanged(() => { observed[index] += 1; }) + )); + + assert.equal( + preload.listenerCounts.get("netcatty:plugins:contributions-changed"), + 1, + "one window-wide IPC listener should fan out to every subscriber", + ); + + preload.handlers.get("netcatty:plugins:contributions-changed")?.({}, { reason: "test" }); + assert.deepEqual(observed, Array.from({ length: 20 }, () => 1)); + + unsubscribes[0](); + preload.handlers.get("netcatty:plugins:contributions-changed")?.({}, { reason: "test-2" }); + assert.equal(observed[0], 1); + assert.deepEqual(observed.slice(1), Array.from({ length: 19 }, () => 2)); + + for (const unsubscribe of unsubscribes.slice(1)) unsubscribe(); +}); + test("stream transfer progress enters the unified transfer event interface", async (t) => { const preload = loadPreloadWithFakeElectron(); t.after(preload.cleanup); @@ -530,6 +558,32 @@ test("legacy MCP-filtered plugin output returns ingress credit immediately", () } }); +test("oversized unterminated MCP marker output stays bounded and hidden", async () => { + const preload = loadPreloadWithFakeElectron(); + try { + const received = []; + preload.api.onSessionData("session-1", (chunk, meta) => received.push({ chunk, meta })); + preload.handlers.get("netcatty:data")?.({}, { + sessionId: "session-1", + data: `__NCMCP_TEST${"x".repeat(70 * 1024)}`, + }); + + await sleep(100); + assert.deepEqual(received, []); + + preload.handlers.get("netcatty:data")?.({}, { + sessionId: "session-1", + data: "tail\nREADY\n", + }); + assert.deepEqual(received, [{ + chunk: "READY\n", + meta: undefined, + }]); + } finally { + preload.cleanup(); + } +}); + test("terminal output port delivery preserves terminal perf metadata", () => { const preload = loadPreloadWithFakeElectron(); try { @@ -828,6 +882,35 @@ test("MCP-filtered empty terminal metadata is cleared on session exit", async () } }); +test("MCP-filtered tail metadata is cleared on acknowledged close without an exit event", async () => { + const preload = loadPreloadWithFakeElectron(); + try { + preload.api.onSessionData("session-1", () => {}); + preload.handlers.get("netcatty:data")?.({}, { + sessionId: "session-1", + data: "__NCMCP_TEST\n", + meta: { droppedOutputMayAffectTerminalState: true }, + }); + + // Some protocol close paths intentionally suppress their later exit event. + await preload.api.closeSession("session-1"); + await preload.api.startLocalSession({ sessionId: "session-1" }); + + const received = []; + preload.api.onSessionData("session-1", (chunk, meta) => { + received.push({ chunk, meta }); + }); + preload.handlers.get("netcatty:data")?.({}, { + sessionId: "session-1", + data: "READY\n", + }); + + assert.deepEqual(received, [{ chunk: "READY\n", meta: undefined }]); + } finally { + preload.cleanup(); + } +}); + test("non-display listeners do not drain pending terminal data", () => { const dataListeners = new Map(); const displayDataListeners = new Map(); @@ -1194,6 +1277,60 @@ test("onSessionExit unsubscribe removes empty listener set", () => { assert.equal(exitListeners.has("session-1"), false); }); +test("session-scoped terminal listener unsubscribe removes empty listener sets", () => { + const listenerMaps = { + telnetAutoLoginCompleteListeners: new Map(), + telnetAutoLoginCancelledListeners: new Map(), + moshSessionReadyListeners: new Map(), + telnetEchoModeListeners: new Map(), + authFailedListeners: new Map(), + }; + const api = createPreloadApi({ + ipcRenderer: { + invoke() {}, + send() {}, + on() {}, + removeListener() {}, + }, + os: { + release: () => "10.0.19045", + }, + dataListeners: new Map(), + displayDataListeners: new Map(), + terminalDataBacklog: createTerminalDataBacklog(), + ...listenerMaps, + }); + const subscriptions = [ + ["telnetAutoLoginCompleteListeners", api.onTelnetAutoLoginComplete], + ["telnetAutoLoginCancelledListeners", api.onTelnetAutoLoginCancelled], + ["moshSessionReadyListeners", api.onMoshSessionReady], + ["telnetEchoModeListeners", api.onTelnetEchoMode], + ["authFailedListeners", api.onAuthFailed], + ]; + + for (const [mapName, subscribe] of subscriptions) { + const off = subscribe("session-1", () => {}); + assert.equal(listenerMaps[mapName].has("session-1"), true, `${mapName} should register`); + off(); + assert.equal(listenerMaps[mapName].has("session-1"), false, `${mapName} should release its empty session entry`); + } +}); + +test("backend exit clears auth-failure listeners for the closed session", () => { + const preload = loadPreloadWithFakeElectron(); + try { + let calls = 0; + preload.api.onAuthFailed("session-1", () => { calls += 1; }); + + preload.handlers.get("netcatty:exit")?.({}, { sessionId: "session-1" }); + preload.handlers.get("netcatty:auth:failed")?.({}, { sessionId: "session-1" }); + + assert.equal(calls, 0); + } finally { + preload.cleanup(); + } +}); + test("closeSession clears terminal data state and waits for close acknowledgement", async () => { const listener = () => {}; const dataListeners = new Map([ @@ -1207,6 +1344,18 @@ test("closeSession clears terminal data state and waits for close acknowledgemen const telnetEchoModeListeners = new Map([ ["session-1", new Set([listener])], ]); + const telnetAutoLoginCompleteListeners = new Map([ + ["session-1", new Set([listener])], + ]); + const telnetAutoLoginCancelledListeners = new Map([ + ["session-1", new Set([listener])], + ]); + const moshSessionReadyListeners = new Map([ + ["session-1", new Set([listener])], + ]); + const authFailedListeners = new Map([ + ["session-1", new Set([listener])], + ]); const zmodemListeners = new Map([ ["session-1", new Set([listener])], ]); @@ -1237,6 +1386,10 @@ test("closeSession clears terminal data state and waits for close acknowledgemen terminalDataBacklog, closedTerminalDataSessions, telnetEchoModeListeners, + telnetAutoLoginCompleteListeners, + telnetAutoLoginCancelledListeners, + moshSessionReadyListeners, + authFailedListeners, zmodemListeners, zmodemOverwriteListeners, terminalOutputPorts: { @@ -1253,6 +1406,10 @@ test("closeSession clears terminal data state and waits for close acknowledgemen assert.equal(terminalDataBacklog.take("session-1"), ""); assert.equal(closedTerminalDataSessions.has("session-1"), true); assert.equal(telnetEchoModeListeners.has("session-1"), false); + assert.equal(telnetAutoLoginCompleteListeners.has("session-1"), false); + assert.equal(telnetAutoLoginCancelledListeners.has("session-1"), false); + assert.equal(moshSessionReadyListeners.has("session-1"), false); + assert.equal(authFailedListeners.has("session-1"), false); // Zmodem listeners are preserved: reconnect closes the session without // unmounting the subscriber, so cleanup is left to subscriber dispose. assert.equal(zmodemListeners.has("session-1"), true); diff --git a/electron/scripts/scriptExecutionWorker.cjs b/electron/scripts/scriptExecutionWorker.cjs new file mode 100644 index 000000000..6723e399e --- /dev/null +++ b/electron/scripts/scriptExecutionWorker.cjs @@ -0,0 +1,212 @@ +"use strict"; + +const vm = require("node:vm"); +const { parentPort } = require("node:worker_threads"); + +if (!parentPort) { + throw new Error("Script execution worker requires a parent port"); +} + +const pendingRequests = new Map(); +let nextRequestId = 1; +let finished = false; +let started = false; +let heartbeat = null; +let heartbeatTimer = null; +let runtimeSnapshot = null; +let nct = null; +let maxPendingHostRequests = 128; +let maxLogNotifications = 512; +let maxTotalNotifications = 20_000; +let logNotificationCount = 0; +let totalNotificationCount = 0; + +function beat() { + if (heartbeat) Atomics.store(heartbeat, 0, BigInt(Date.now())); +} + +function serializeError(error) { + return { + name: error?.name || "Error", + message: error?.message || String(error), + stack: typeof error?.stack === "string" ? error.stack : undefined, + }; +} + +function reviveError(value) { + const error = new Error(value?.message || "Script API request failed"); + error.name = value?.name || "Error"; + if (typeof value?.stack === "string") error.stack = value.stack; + return error; +} + +function updateSnapshot(snapshot) { + if (!snapshot || typeof snapshot !== "object") return; + if (snapshot.session && typeof snapshot.session === "object") { + Object.assign(runtimeSnapshot.session, snapshot.session); + } + if (snapshot.screen && typeof snapshot.screen === "object") { + Object.assign(runtimeSnapshot.screen, snapshot.screen); + } +} + +function callHost(method, args = []) { + if (finished) return Promise.reject(new Error("Script execution finished")); + if (pendingRequests.size >= maxPendingHostRequests) { + throw new Error( + `Script exceeded the ${maxPendingHostRequests} pending host request limit`, + ); + } + const requestId = nextRequestId++; + return new Promise((resolve, reject) => { + pendingRequests.set(requestId, { resolve, reject }); + parentPort.postMessage({ type: "rpc", requestId, method, args }); + }); +} + +function notifyHost(method, args = []) { + if (finished) return; + totalNotificationCount += 1; + if (totalNotificationCount > maxTotalNotifications) { + throw new Error(`Script exceeded the ${maxTotalNotifications} notification limit`); + } + if (method === "log" || method === "console.log") { + logNotificationCount += 1; + if (logNotificationCount > maxLogNotifications) { + throw new Error(`Script exceeded the ${maxLogNotifications} log notification limit`); + } + } + parentPort.postMessage({ type: "notify", method, args }); +} + +const sessionApi = { + get connected() { return runtimeSnapshot.session.connected; }, + get hostname() { return runtimeSnapshot.session.hostname; }, + get username() { return runtimeSnapshot.session.username; }, + sleep: (ms) => callHost("session.sleep", [ms]), + startLog: (path) => callHost("session.startLog", [path == null ? path : String(path)]), + stopLog: () => callHost("session.stopLog"), + disconnect: () => callHost("session.disconnect"), +}; + +const screenApi = { + send: (text, options) => callHost("screen.send", [ + String(text ?? ""), + { sensitive: options?.sensitive === true }, + ]), + sendLine: (text, options) => callHost("screen.sendLine", [ + String(text ?? ""), + { sensitive: options?.sensitive === true }, + ]), + waitFor: (pattern, timeoutMs) => callHost("screen.waitFor", [pattern, timeoutMs]), + waitForText: (text, timeoutMs) => callHost("screen.waitForText", [text, timeoutMs]), + waitForRegex: (pattern, timeoutMs) => callHost("screen.waitForRegex", [pattern, timeoutMs]), + waitForPrompt: (timeoutMs) => callHost("screen.waitForPrompt", [timeoutMs]), + waitForAny: (patterns, timeoutMs) => callHost("screen.waitForAny", [patterns, timeoutMs]), + getText: (startRow, endRow) => callHost("screen.getText", [startRow, endRow]), + get currentRow() { return runtimeSnapshot.screen.currentRow; }, + get rows() { return runtimeSnapshot.screen.rows; }, + get cols() { return runtimeSnapshot.screen.cols; }, + clear: () => callHost("screen.clear"), +}; + +const dialogApi = { + alert: (message) => callHost("dialog.alert", [String(message ?? "")]), + confirm: (message) => callHost("dialog.confirm", [String(message ?? "")]), + prompt: (message, defaultValue, options) => callHost("dialog.prompt", [ + String(message ?? ""), + String(defaultValue ?? ""), + { sensitive: options?.sensitive === true }, + ]), + form: (spec) => callHost("dialog.form", [spec]), + select: (message, options, defaultValue) => callHost("dialog.select", [message, options, defaultValue]), + radio: (message, options, defaultValue) => callHost("dialog.radio", [message, options, defaultValue]), + checkbox: (message, defaultChecked) => callHost("dialog.checkbox", [message, defaultChecked]), +}; + +const progressApi = { + start(label, total) { notifyHost("progress.start", [label, total]); }, + set(current, detail) { notifyHost("progress.set", [current, detail]); }, + step(detail) { notifyHost("progress.step", [detail]); }, + done() { notifyHost("progress.done"); }, +}; + +parentPort.on("message", (message) => { + if (message?.type === "start") { + if (started) return; + started = true; + void run(message.config); + return; + } + if (message?.type === "snapshot") { + updateSnapshot(message.snapshot); + return; + } + if (message?.type !== "rpc-result") return; + const pending = pendingRequests.get(message.requestId); + if (!pending) return; + pendingRequests.delete(message.requestId); + updateSnapshot(message.snapshot); + if (message.ok) pending.resolve(message.value); + else pending.reject(reviveError(message.error)); +}); + +async function run(config) { + maxPendingHostRequests = Math.max(1, Number(config.maxPendingHostRequests) || 128); + maxLogNotifications = Math.max(1, Number(config.maxLogNotifications) || 512); + maxTotalNotifications = Math.max( + maxLogNotifications, + Number(config.maxTotalNotifications) || 20_000, + ); + heartbeat = new BigInt64Array(config.heartbeatBuffer); + const heartbeatIntervalMs = Math.max(2, Number(config.heartbeatIntervalMs) || 10); + runtimeSnapshot = { + session: { + connected: Boolean(config.snapshot?.session?.connected), + hostname: String(config.snapshot?.session?.hostname || ""), + username: String(config.snapshot?.session?.username || ""), + }, + screen: { + currentRow: Number(config.snapshot?.screen?.currentRow) || 0, + rows: Number(config.snapshot?.screen?.rows) || 24, + cols: Number(config.snapshot?.screen?.cols) || 80, + }, + }; + nct = { + session: sessionApi, + screen: screenApi, + dialog: dialogApi, + progress: progressApi, + version: String(config.version || "0.0.0"), + sleep: sessionApi.sleep, + log(message) { notifyHost("log", [String(message ?? "")]); }, + }; + heartbeatTimer = setInterval(beat, heartbeatIntervalMs); + beat(); + try { + const sandbox = { + nct, + SharedArrayBuffer: undefined, + console: { + log: (...args) => notifyHost("console.log", [args.map((arg) => String(arg)).join(" ")]), + }, + }; + vm.createContext(sandbox, { + codeGeneration: { strings: false, wasm: false }, + }); + const script = new vm.Script(config.source, { + filename: config.filename || "netcatty-script.js", + }); + const result = script.runInContext(sandbox, { displayErrors: true }); + if (result && typeof result.then === "function") await result; + finished = true; + clearInterval(heartbeatTimer); + parentPort.postMessage({ type: "completed" }); + } catch (error) { + finished = true; + clearInterval(heartbeatTimer); + parentPort.postMessage({ type: "failed", error: serializeError(error) }); + } +} + +parentPort.postMessage({ type: "ready" }); diff --git a/electron/scripts/scriptRunRetention.cjs b/electron/scripts/scriptRunRetention.cjs new file mode 100644 index 000000000..a3efbc3c0 --- /dev/null +++ b/electron/scripts/scriptRunRetention.cjs @@ -0,0 +1,34 @@ +"use strict"; + +const MAX_RETAINED_COMPLETED_RUNS = 200; +const MAX_RETAINED_LOGS_PER_RUN = 200; + +function appendRetainedRunLog(run, entry, limit = MAX_RETAINED_LOGS_PER_RUN) { + if (!run || !Array.isArray(run.logs)) return; + run.logs.push(entry); + const overflow = run.logs.length - limit; + if (overflow > 0) run.logs.splice(0, overflow); +} + +function pruneCompletedRuns(runs, limit = MAX_RETAINED_COMPLETED_RUNS) { + if (!(runs instanceof Map)) return 0; + const completed = [...runs.values()] + .filter((run) => Number.isFinite(run?.endedAt)) + .sort((left, right) => ( + left.endedAt - right.endedAt + || left.startedAt - right.startedAt + || String(left.runId).localeCompare(String(right.runId)) + )); + const overflow = completed.length - limit; + for (let index = 0; index < overflow; index += 1) { + runs.delete(completed[index].runId); + } + return Math.max(0, overflow); +} + +module.exports = { + MAX_RETAINED_COMPLETED_RUNS, + MAX_RETAINED_LOGS_PER_RUN, + appendRetainedRunLog, + pruneCompletedRuns, +}; diff --git a/electron/scripts/scriptRunRetention.test.cjs b/electron/scripts/scriptRunRetention.test.cjs new file mode 100644 index 000000000..cd8fda541 --- /dev/null +++ b/electron/scripts/scriptRunRetention.test.cjs @@ -0,0 +1,39 @@ +"use strict"; + +const assert = require("node:assert/strict"); +const test = require("node:test"); +const { + MAX_RETAINED_COMPLETED_RUNS, + MAX_RETAINED_LOGS_PER_RUN, + appendRetainedRunLog, + pruneCompletedRuns, +} = require("./scriptRunRetention.cjs"); + +test("completed script history is bounded without removing active runs", () => { + const runs = new Map(); + for (let index = 0; index < MAX_RETAINED_COMPLETED_RUNS + 25; index += 1) { + runs.set(`completed-${index}`, { + runId: `completed-${index}`, + startedAt: index, + endedAt: index + 1, + }); + } + runs.set("active", { runId: "active", startedAt: 0 }); + + assert.equal(pruneCompletedRuns(runs), 25); + assert.equal(runs.size, MAX_RETAINED_COMPLETED_RUNS + 1); + assert.equal(runs.has("active"), true); + assert.equal(runs.has("completed-0"), false); + assert.equal(runs.has(`completed-${MAX_RETAINED_COMPLETED_RUNS + 24}`), true); +}); + +test("script logs keep only the newest bounded tail", () => { + const run = { logs: [] }; + for (let index = 0; index < MAX_RETAINED_LOGS_PER_RUN + 30; index += 1) { + appendRetainedRunLog(run, { message: `log-${index}` }); + } + + assert.equal(run.logs.length, MAX_RETAINED_LOGS_PER_RUN); + assert.equal(run.logs[0].message, "log-30"); + assert.equal(run.logs.at(-1).message, `log-${MAX_RETAINED_LOGS_PER_RUN + 29}`); +}); diff --git a/electron/scripts/scriptRuntime.cjs b/electron/scripts/scriptRuntime.cjs index 06f791783..a92619779 100644 --- a/electron/scripts/scriptRuntime.cjs +++ b/electron/scripts/scriptRuntime.cjs @@ -1,8 +1,72 @@ "use strict"; -const vm = require("node:vm"); +const path = require("node:path"); +const { AsyncLocalStorage } = require("node:async_hooks"); +const { Worker } = require("node:worker_threads"); const { shellPromptPatterns } = require("./shellPromptPatterns.cjs"); +const SCRIPT_SYNC_EXECUTION_TIMEOUT_MS = 1_000; +const SCRIPT_WORKER_START_TIMEOUT_MS = 10_000; +const SCRIPT_WORKER_MAX_OLD_GENERATION_MB = 64; +const SCRIPT_WORKER_MAX_YOUNG_GENERATION_MB = 16; +const SCRIPT_WORKER_MAX_PENDING_HOST_REQUESTS = 128; +const SCRIPT_WORKER_MAX_LOG_NOTIFICATIONS = 512; +const SCRIPT_WORKER_MAX_TOTAL_NOTIFICATIONS = 20_000; +const SCRIPT_WORKER_IMMEDIATE_PROGRESS_NOTIFICATIONS = 64; +const SCRIPT_WORKER_PROGRESS_THROTTLE_MS = 50; +const activeScriptWorkers = new Set(); +const activeScriptHostRequests = new Set(); +let prewarmedScriptWorker = null; + +function createScriptWorkerHandle() { + const worker = new Worker(path.join(__dirname, "scriptExecutionWorker.cjs"), { + resourceLimits: { + maxOldGenerationSizeMb: SCRIPT_WORKER_MAX_OLD_GENERATION_MB, + maxYoungGenerationSizeMb: SCRIPT_WORKER_MAX_YOUNG_GENERATION_MB, + stackSizeMb: 4, + }, + }); + worker.unref(); + let resolveReady; + let rejectReady; + const readyPromise = new Promise((resolve, reject) => { + resolveReady = resolve; + rejectReady = reject; + }); + const onMessage = (message) => { + if (message?.type !== "ready") return; + worker.removeListener("message", onMessage); + worker.removeListener("error", onStartupError); + resolveReady(); + }; + const onStartupError = (error) => { + worker.removeListener("message", onMessage); + rejectReady(error); + }; + worker.on("message", onMessage); + worker.once("error", onStartupError); + void readyPromise.catch(() => {}); + return { worker, readyPromise }; +} + +function ensurePrewarmedScriptWorker() { + if (prewarmedScriptWorker) return prewarmedScriptWorker; + const handle = createScriptWorkerHandle(); + prewarmedScriptWorker = handle; + void handle.readyPromise.catch(() => { + if (prewarmedScriptWorker === handle) prewarmedScriptWorker = null; + }); + return handle; +} + +function acquireScriptWorker() { + const handle = prewarmedScriptWorker || createScriptWorkerHandle(); + if (prewarmedScriptWorker === handle) prewarmedScriptWorker = null; + handle.worker.ref(); + ensurePrewarmedScriptWorker(); + return handle; +} + function wrapScriptSource(source) { const trimmed = String(source || "").trim(); if (!trimmed) { @@ -25,6 +89,373 @@ function wrapScriptSource(source) { return `(async () => {\n${trimmed}\n})();`; } +function serializeWorkerError(error) { + return { + name: error?.name || "Error", + message: error?.message || String(error), + stack: typeof error?.stack === "string" ? error.stack : undefined, + }; +} + +function reviveWorkerError(value) { + const error = new Error(value?.message || "Script worker failed"); + error.name = value?.name || "Error"; + if (typeof value?.stack === "string") error.stack = value.stack; + return error; +} + +function readRuntimeSnapshot(nct) { + return { + session: { + connected: Boolean(nct.session.connected), + hostname: String(nct.session.hostname || ""), + username: String(nct.session.username || ""), + }, + screen: { + currentRow: Number(nct.screen.currentRow) || 0, + rows: Number(nct.screen.rows) || 24, + cols: Number(nct.screen.cols) || 80, + }, + }; +} + +const WORKER_RPC_METHODS = new Set([ + "session.sleep", + "session.startLog", + "session.stopLog", + "session.disconnect", + "screen.send", + "screen.sendLine", + "screen.waitFor", + "screen.waitForText", + "screen.waitForRegex", + "screen.waitForPrompt", + "screen.waitForAny", + "screen.getText", + "screen.clear", + "dialog.alert", + "dialog.confirm", + "dialog.prompt", + "dialog.form", + "dialog.select", + "dialog.radio", + "dialog.checkbox", +]); + +function invokeWorkerRpc(nct, method, args) { + if (!WORKER_RPC_METHODS.has(method)) { + throw new Error(`Unsupported script API method: ${method}`); + } + const [namespace, methodName] = method.split("."); + const target = nct[namespace]; + const fn = target?.[methodName]; + if (typeof fn !== "function") { + throw new Error(`Script API method unavailable: ${method}`); + } + return fn.apply(target, Array.isArray(args) ? args : []); +} + +function handleWorkerNotification({ nct, appendLog, runId }, method, args) { + if (method === "log") { + nct.log(args?.[0]); + return; + } + if (method === "console.log") { + appendLog(runId, String(args?.[0] ?? "")); + return; + } + if (method.startsWith("progress.")) { + const methodName = method.slice("progress.".length); + const fn = nct.progress?.[methodName]; + if (typeof fn !== "function") throw new Error(`Unsupported script notification: ${method}`); + fn.apply(nct.progress, Array.isArray(args) ? args : []); + return; + } + throw new Error(`Unsupported script notification: ${method}`); +} + +function executeInScriptWorker({ + source, + runId, + nct, + appendLog, + isAborted, + syncExecutionTimeoutMs, + registerStop, + executionContext, + executionToken, +}) { + const heartbeatIntervalMs = Math.max( + 2, + Math.min(50, Math.floor(syncExecutionTimeoutMs / 4) || 2), + ); + const heartbeatBuffer = new SharedArrayBuffer(BigInt64Array.BYTES_PER_ELEMENT); + const heartbeat = new BigInt64Array(heartbeatBuffer); + Atomics.store(heartbeat, 0, BigInt(Date.now())); + const workerHandle = acquireScriptWorker(); + const { worker } = workerHandle; + activeScriptWorkers.add(worker); + + return new Promise((resolve, reject) => { + let settled = false; + let ready = false; + let watchdog = null; + const pendingHostRequests = new Set(); + let notificationCount = 0; + let logNotificationCount = 0; + let progressNotificationCount = 0; + let pendingProgressNotification = null; + let progressTimer = null; + let progressCurrent = 0; + let progressTotal = 1; + let rejectHostRequests; + const hostRequestsClosed = new Promise((_, rejectClosed) => { + rejectHostRequests = rejectClosed; + }); + void hostRequestsClosed.catch(() => {}); + + const cleanup = () => { + clearTimeout(startTimer); + if (watchdog) clearInterval(watchdog); + if (progressTimer) clearTimeout(progressTimer); + watchdog = null; + progressTimer = null; + pendingProgressNotification = null; + worker.removeAllListeners(); + registerStop?.(() => {}); + }; + const finish = (error) => { + if (settled) return; + settled = true; + executionToken.closed = true; + rejectHostRequests?.(error || new Error("Script execution finished")); + const pendingDrain = Promise.allSettled([...pendingHostRequests]); + cleanup(); + void Promise.all([ + worker.terminate().catch(() => {}), + pendingDrain, + ]).then(() => { + pendingHostRequests.clear(); + activeScriptWorkers.delete(worker); + if (error) reject(error); + else resolve(undefined); + }); + }; + registerStop?.((reason) => { + finish(reason instanceof Error ? reason : new Error("Script stopped")); + }); + const postSnapshot = () => { + if (settled) return; + try { + worker.postMessage({ type: "snapshot", snapshot: readRuntimeSnapshot(nct) }); + } catch { + // Worker may have crossed its terminal boundary between the checks. + } + }; + const deliverNotification = (message) => { + executionContext.run(executionToken, () => { + handleWorkerNotification({ nct, appendLog, runId }, message.method, message.args); + }); + }; + const flushPendingProgress = () => { + if (progressTimer) clearTimeout(progressTimer); + progressTimer = null; + const pending = pendingProgressNotification; + pendingProgressNotification = null; + if (!pending || settled) return; + deliverNotification(pending); + }; + const scheduleProgressFlush = () => { + if (progressTimer || settled) return; + progressTimer = setTimeout(() => { + progressTimer = null; + try { + flushPendingProgress(); + } catch (error) { + finish(error); + } + }, SCRIPT_WORKER_PROGRESS_THROTTLE_MS); + progressTimer.unref?.(); + }; + const handleProgressNotification = (message) => { + const methodName = message.method.slice("progress.".length); + if (methodName === "start") { + flushPendingProgress(); + progressCurrent = 0; + progressTotal = Math.max(1, Number(message.args?.[1]) || 1); + } else if (methodName === "set") { + progressCurrent = Math.max(0, Math.min(progressTotal, Number(message.args?.[0]) || 0)); + } else if (methodName === "step") { + progressCurrent = Math.min(progressTotal, progressCurrent + 1); + } else if (methodName === "done") { + flushPendingProgress(); + } + + progressNotificationCount += 1; + if ( + progressNotificationCount <= SCRIPT_WORKER_IMMEDIATE_PROGRESS_NOTIFICATIONS + || methodName === "start" + || methodName === "done" + ) { + deliverNotification(message); + return; + } + pendingProgressNotification = { + type: "notify", + method: "progress.set", + args: [progressCurrent, message.args?.[1] ?? message.args?.[0]], + }; + scheduleProgressFlush(); + }; + const startTimer = setTimeout(() => { + finish(new Error(`Script worker failed to start after ${SCRIPT_WORKER_START_TIMEOUT_MS}ms`)); + }, SCRIPT_WORKER_START_TIMEOUT_MS); + startTimer.unref?.(); + + const startWatchdog = () => { + if (watchdog || settled) return; + const checkIntervalMs = Math.max(2, Math.min(25, heartbeatIntervalMs)); + watchdog = setInterval(() => { + if (isAborted?.()) { + finish(new Error("Script stopped")); + return; + } + const lastHeartbeat = Number(Atomics.load(heartbeat, 0)); + if (Date.now() - lastHeartbeat > syncExecutionTimeoutMs + heartbeatIntervalMs) { + finish(new Error(`Script execution timed out after ${syncExecutionTimeoutMs}ms`)); + return; + } + postSnapshot(); + }, checkIntervalMs); + watchdog.unref?.(); + }; + + worker.on("message", (message) => { + if (settled) return; + if (message?.type === "notify") { + try { + notificationCount += 1; + if (notificationCount > SCRIPT_WORKER_MAX_TOTAL_NOTIFICATIONS) { + finish(new Error( + `Script exceeded the ${SCRIPT_WORKER_MAX_TOTAL_NOTIFICATIONS} notification limit`, + )); + return; + } + if (message.method === "log" || message.method === "console.log") { + flushPendingProgress(); + logNotificationCount += 1; + if (logNotificationCount > SCRIPT_WORKER_MAX_LOG_NOTIFICATIONS) { + finish(new Error( + `Script exceeded the ${SCRIPT_WORKER_MAX_LOG_NOTIFICATIONS} log notification limit`, + )); + return; + } + deliverNotification(message); + return; + } + if (message.method?.startsWith("progress.")) { + handleProgressNotification(message); + return; + } + flushPendingProgress(); + deliverNotification(message); + } catch (error) { + finish(error); + } + return; + } + if (message?.type === "rpc") { + if (pendingHostRequests.size >= SCRIPT_WORKER_MAX_PENDING_HOST_REQUESTS) { + finish(new Error( + `Script exceeded the ${SCRIPT_WORKER_MAX_PENDING_HOST_REQUESTS} pending host request limit`, + )); + return; + } + const hostRequest = executionContext.run(executionToken, () => ( + Promise.resolve().then(() => invokeWorkerRpc(nct, message.method, message.args)) + )); + // The underlying API observes executionToken and normally stops itself. + // Race it as well so one misbehaving host API cannot keep bookkeeping, + // timers, or the script completion promise alive after worker teardown. + void hostRequest.catch(() => {}); + const request = Promise.race([hostRequest, hostRequestsClosed]); + pendingHostRequests.add(request); + activeScriptHostRequests.add(request); + void request.then( + (value) => { + if (settled) return; + worker.postMessage({ + type: "rpc-result", + requestId: message.requestId, + ok: true, + value, + snapshot: readRuntimeSnapshot(nct), + }); + }, + (error) => { + if (settled) return; + worker.postMessage({ + type: "rpc-result", + requestId: message.requestId, + ok: false, + error: serializeWorkerError(error), + snapshot: readRuntimeSnapshot(nct), + }); + }, + ).finally(() => { + pendingHostRequests.delete(request); + activeScriptHostRequests.delete(request); + }).catch((error) => { + if (!settled) finish(error); + }); + return; + } + if (message?.type === "completed") { + try { flushPendingProgress(); } catch (error) { + finish(error); + return; + } + finish(); + return; + } + if (message?.type === "failed") { + finish(reviveWorkerError(message.error)); + } + }); + worker.once("error", (error) => finish(error)); + worker.once("exit", (code) => { + if (!settled) { + finish(new Error( + ready + ? `Script worker exited before completion (code ${code})` + : `Script worker failed to start (code ${code})`, + )); + } + }); + void workerHandle.readyPromise.then(() => { + if (settled) return; + ready = true; + clearTimeout(startTimer); + Atomics.store(heartbeat, 0, BigInt(Date.now())); + worker.postMessage({ + type: "start", + config: { + source, + filename: `script-${runId}.js`, + version: nct.version, + snapshot: readRuntimeSnapshot(nct), + heartbeatBuffer, + heartbeatIntervalMs, + maxPendingHostRequests: SCRIPT_WORKER_MAX_PENDING_HOST_REQUESTS, + maxLogNotifications: SCRIPT_WORKER_MAX_LOG_NOTIFICATIONS, + maxTotalNotifications: SCRIPT_WORKER_MAX_TOTAL_NOTIFICATIONS, + }, + }); + startWatchdog(); + }, (error) => finish(error)); + }); +} + function truncateActivityLabel(value, max = 80) { const text = String(value ?? ""); return text.length > max ? `${text.slice(0, max - 1)}…` : text; @@ -254,6 +685,7 @@ function normalizeDialogFormSpec(spec) { } function createScriptRuntime(deps) { + ensurePrewarmedScriptWorker(); const { sessionId, runId, @@ -278,6 +710,8 @@ function createScriptRuntime(deps) { let progressLabel; let progressCurrent = 0; let progressTotal = 0; + let stopActiveExecution = () => {}; + const executionContext = new AsyncLocalStorage(); let screenSnapshot = { rows: 24, @@ -286,16 +720,17 @@ function createScriptRuntime(deps) { lines: [], }; + function isExecutionAborted() { + return Boolean(executionContext.getStore()?.closed || deps.isAborted?.()); + } + function assertNotAborted() { - if (deps.isAborted?.()) { + if (isExecutionAborted()) { throw new Error("Script stopped"); } } function abortable(promise) { - if (typeof deps.isAborted !== "function") { - return promise; - } assertNotAborted(); return new Promise((resolve, reject) => { let settled = false; @@ -306,19 +741,19 @@ function createScriptRuntime(deps) { callback(value); }; const timer = setInterval(() => { - if (!deps.isAborted?.()) return; + if (!isExecutionAborted()) return; finish(reject, new Error("Script stopped")); }, 50); Promise.resolve(promise).then( (value) => { - if (deps.isAborted?.()) { + if (isExecutionAborted()) { finish(reject, new Error("Script stopped")); return; } finish(resolve, value); }, (err) => { - if (deps.isAborted?.()) { + if (isExecutionAborted()) { finish(reject, new Error("Script stopped")); return; } @@ -329,7 +764,8 @@ function createScriptRuntime(deps) { } function markHandled(promise) { - Promise.resolve(promise).catch(() => {}); + const observed = Promise.resolve(promise); + observed.catch(() => {}); return promise; } @@ -337,7 +773,7 @@ function createScriptRuntime(deps) { try { return await task(); } catch (err) { - if (deps.isAborted?.() && err?.message === "Script stopped") { + if (isExecutionAborted() && err?.message === "Script stopped") { return undefined; } throw err; @@ -388,7 +824,7 @@ function createScriptRuntime(deps) { async function waitForPromptWithRecovery(timeoutMs = 60000) { let stepTracked = false; while (true) { - if (deps.isAborted?.()) { + if (isExecutionAborted()) { throw new Error("Script stopped"); } if (!stepTracked) { @@ -400,7 +836,7 @@ function createScriptRuntime(deps) { return await getOutputBuffer(sessionId).waitForAny( shellPromptPatterns(), timeoutMs, - () => Boolean(deps.isAborted?.()), + isExecutionAborted, { allowPreservedTailMatch: true }, ); } catch (err) { @@ -430,7 +866,7 @@ function createScriptRuntime(deps) { : String(patterns); let stepTracked = false; while (true) { - if (deps.isAborted?.()) { + if (isExecutionAborted()) { throw new Error("Script stopped"); } if (!stepTracked) { @@ -442,7 +878,7 @@ function createScriptRuntime(deps) { return await getOutputBuffer(sessionId).waitForAny( patterns, timeoutMs, - () => Boolean(deps.isAborted?.()), + isExecutionAborted, ); } catch (err) { if (!String(err?.message || err).includes("timed out")) { @@ -471,7 +907,7 @@ function createScriptRuntime(deps) { const patternLabel = pattern instanceof RegExp ? pattern.source : String(pattern); let stepTracked = false; while (true) { - if (deps.isAborted?.()) { + if (isExecutionAborted()) { throw new Error("Script stopped"); } if (!stepTracked) { @@ -483,7 +919,7 @@ function createScriptRuntime(deps) { return await getOutputBuffer(sessionId)[waitMethod]( pattern, timeoutMs, - () => Boolean(deps.isAborted?.()), + isExecutionAborted, ); } catch (err) { if (!String(err?.message || err).includes("timed out")) { @@ -580,14 +1016,14 @@ function createScriptRuntime(deps) { }, sleep(ms) { const delay = Math.max(0, Number(ms) || 0); - return markHandled(trackStep(`sleep ${delay}ms`).then(() => interruptibleSleep(delay, deps.isAborted))); + return markHandled(trackStep(`sleep ${delay}ms`).then(() => interruptibleSleep(delay, isExecutionAborted))); }, startLog(path) { return markHandled(ignoreIfStopped(async () => { - if (deps.isAborted?.()) return; + if (isExecutionAborted()) return; assertWriteAllowed("session.startLog"); await trackStep("startLog"); - if (deps.isAborted?.()) return; + if (isExecutionAborted()) return; await startSessionLog?.(sessionId, path); })); }, @@ -598,10 +1034,10 @@ function createScriptRuntime(deps) { }, disconnect() { return markHandled(ignoreIfStopped(async () => { - if (deps.isAborted?.()) return; + if (isExecutionAborted()) return; assertWriteAllowed("session.disconnect"); await trackStep("disconnect"); - if (deps.isAborted?.()) return; + if (isExecutionAborted()) return; await disconnectSession?.(sessionId); })); }, @@ -610,28 +1046,28 @@ function createScriptRuntime(deps) { const screenApi = { send(text, options = {}) { return markHandled(ignoreIfStopped(async () => { - if (deps.isAborted?.()) return; + if (isExecutionAborted()) return; assertWriteAllowed("screen.send"); await waitIfPaused(); const payload = String(text ?? ""); const sensitive = options?.sensitive === true; const visiblePayload = sensitive ? "[sensitive]" : formatScriptInputForLog(payload); await trackStep(`send: ${truncateActivityLabel(visiblePayload, 60)}`); - if (deps.isAborted?.()) return; + if (isExecutionAborted()) return; appendLog(runId, `→ ${visiblePayload}`); writeToSession(sessionId, payload, { automated: true, sensitive }); })); }, sendLine(text, options = {}) { return markHandled(ignoreIfStopped(async () => { - if (deps.isAborted?.()) return; + if (isExecutionAborted()) return; assertWriteAllowed("screen.sendLine"); await waitIfPaused(); const line = String(text ?? ""); const sensitive = options?.sensitive === true; const visibleLine = sensitive ? "[sensitive]" : line; await trackStep(`sendLine: ${truncateActivityLabel(visibleLine, 60)}`); - if (deps.isAborted?.()) return; + if (isExecutionAborted()) return; appendLog(runId, `→ ${visibleLine}`); // Bastion menus can ignore a single "line\r" packet even when // stream.write succeeds. Match xterm: body, then Enter (#1960). @@ -645,8 +1081,8 @@ function createScriptRuntime(deps) { sensitive, invalidateStartupSeed: false, }); - await interruptibleSleep(30, deps.isAborted); - if (deps.isAborted?.()) return; + await interruptibleSleep(30, isExecutionAborted); + if (isExecutionAborted()) return; } writeToSession(sessionId, "\r", { automated: true, @@ -701,10 +1137,10 @@ function createScriptRuntime(deps) { }, clear() { return markHandled(ignoreIfStopped(async () => { - if (deps.isAborted?.()) return; + if (isExecutionAborted()) return; assertWriteAllowed("screen.clear"); await trackStep("clear"); - if (deps.isAborted?.()) return; + if (isExecutionAborted()) return; writeToSession(sessionId, "\x1b[2J\x1b[H", { automated: true }); })); }, @@ -713,25 +1149,25 @@ function createScriptRuntime(deps) { const dialogApi = { alert(message) { assertNotAborted(); - return markHandled(showDialog("alert", String(message ?? ""))); + return markHandled(abortable(showDialog("alert", String(message ?? "")))); }, confirm(message) { assertNotAborted(); - return markHandled(showDialog("confirm", String(message ?? ""))); + return markHandled(abortable(showDialog("confirm", String(message ?? "")))); }, prompt(message, defaultValue = "", options = {}) { assertNotAborted(); - return markHandled(showDialog( + return markHandled(abortable(showDialog( "prompt", String(message ?? ""), String(defaultValue ?? ""), { sensitive: options?.sensitive === true }, - )); + ))); }, form(spec) { assertNotAborted(); const form = normalizeDialogFormSpec(spec); - return markHandled(showDialog("form", form.message, undefined, { form })); + return markHandled(abortable(showDialog("form", form.message, undefined, { form }))); }, select(message, options, defaultValue) { return markHandled((async () => { @@ -787,7 +1223,7 @@ function createScriptRuntime(deps) { version: deps.appVersion || "0.0.0", sleep: sessionApi.sleep.bind(sessionApi), log(message) { - if (deps.isAborted?.()) return; + if (isExecutionAborted()) return; assertNotAborted(); stepIndex += 1; emitStatus({ @@ -803,7 +1239,7 @@ function createScriptRuntime(deps) { while (isPaused?.()) { assertNotAborted(); onStatusChange?.(runId, { status: "paused", elapsedMs: Math.max(0, Date.now() - startedAt) }); - await interruptibleSleep(100, deps.isAborted); + await interruptibleSleep(100, isExecutionAborted); } assertNotAborted(); onStatusChange?.(runId, { status: "running", elapsedMs: Math.max(0, Date.now() - startedAt) }); @@ -811,25 +1247,32 @@ function createScriptRuntime(deps) { async function execute(source) { assertNotAborted(); - const wrapped = wrapScriptSource(source); - const sandbox = { + const executionToken = { closed: false }; + const configuredSyncTimeoutMs = Number(deps.syncExecutionTimeoutMs); + const syncExecutionTimeoutMs = Number.isFinite(configuredSyncTimeoutMs) && configuredSyncTimeoutMs > 0 + ? Math.floor(configuredSyncTimeoutMs) + : SCRIPT_SYNC_EXECUTION_TIMEOUT_MS; + await executeInScriptWorker({ + source: wrapScriptSource(source), + runId, nct, - console: { - log: (...args) => { - appendLog(runId, args.map((arg) => String(arg)).join(" ")); - }, - }, - }; - vm.createContext(sandbox); - const script = new vm.Script(wrapped, { filename: `script-${runId}.js` }); - const result = script.runInContext(sandbox, { displayErrors: true }); - if (result && typeof result.then === "function") { - await abortable(result); - } + appendLog, + isAborted: () => executionToken.closed || Boolean(deps.isAborted?.()), + syncExecutionTimeoutMs, + registerStop: (stop) => { stopActiveExecution = stop; }, + executionContext, + executionToken, + }); assertNotAborted(); } - return { execute, nct }; + return { + execute, + nct, + stop(reason = new Error("Script stopped")) { + stopActiveExecution(reason); + }, + }; } function interruptibleSleep(ms, isAborted) { @@ -862,9 +1305,16 @@ function formatScriptInputForLog(data) { } module.exports = { + SCRIPT_WORKER_MAX_PENDING_HOST_REQUESTS, + SCRIPT_WORKER_MAX_LOG_NOTIFICATIONS, + SCRIPT_WORKER_MAX_TOTAL_NOTIFICATIONS, + SCRIPT_WORKER_IMMEDIATE_PROGRESS_NOTIFICATIONS, + SCRIPT_SYNC_EXECUTION_TIMEOUT_MS, createScriptRuntime, wrapScriptSource, interruptibleSleep, formatScriptInputForLog, normalizeDialogFormSpec, + _getActiveScriptWorkerCountForTests: () => activeScriptWorkers.size, + _getActiveScriptHostRequestCountForTests: () => activeScriptHostRequests.size, }; diff --git a/electron/scripts/scriptRuntime.test.cjs b/electron/scripts/scriptRuntime.test.cjs index 7fcd28465..71456b30d 100644 --- a/electron/scripts/scriptRuntime.test.cjs +++ b/electron/scripts/scriptRuntime.test.cjs @@ -4,9 +4,15 @@ const test = require("node:test"); const assert = require("node:assert/strict"); const { createScriptRuntime, + SCRIPT_WORKER_MAX_PENDING_HOST_REQUESTS, + SCRIPT_WORKER_MAX_LOG_NOTIFICATIONS, + SCRIPT_WORKER_MAX_TOTAL_NOTIFICATIONS, + SCRIPT_WORKER_IMMEDIATE_PROGRESS_NOTIFICATIONS, wrapScriptSource, interruptibleSleep, normalizeDialogFormSpec, + _getActiveScriptWorkerCountForTests, + _getActiveScriptHostRequestCountForTests, } = require("./scriptRuntime.cjs"); const { SessionOutputBuffer } = require("./sessionOutputBuffer.cjs"); @@ -89,6 +95,299 @@ test("createScriptRuntime executes simple log script", async () => { assert.deepEqual(logs, ["hello"]); }); +test("createScriptRuntime releases its isolated worker after normal completion", async () => { + const runtime = createScriptRuntime({ + sessionId: "s1", + runId: "r-bounded-sync", + appendLog: () => {}, + writeToSession: () => {}, + getOutputBuffer: () => ({ getText: () => "" }), + getSessionMeta: () => ({ connected: true }), + showDialog: async () => true, + isPaused: () => false, + isAborted: () => false, + onStatusChange: () => {}, + }); + + await runtime.execute("void 0;"); + assert.equal(_getActiveScriptWorkerCountForTests(), 0); +}); + +test("createScriptRuntime stops a synchronous infinite loop", async () => { + const runtime = createScriptRuntime({ + sessionId: "s1", + runId: "r-infinite-loop", + appendLog: () => {}, + writeToSession: () => {}, + getOutputBuffer: () => ({ getText: () => "" }), + getSessionMeta: () => ({ connected: true }), + showDialog: async () => true, + isPaused: () => false, + isAborted: () => false, + onStatusChange: () => {}, + syncExecutionTimeoutMs: 20, + }); + + await assert.rejects(runtime.execute("while (true) {}"), /timed out/i); +}); + +test("createScriptRuntime stop forcibly terminates a blocked worker", async () => { + const runtime = createScriptRuntime({ + sessionId: "s1", + runId: "r-explicit-stop", + appendLog: () => {}, + writeToSession: () => {}, + getOutputBuffer: () => ({ getText: () => "" }), + getSessionMeta: () => ({ connected: true }), + showDialog: async () => true, + isPaused: () => false, + isAborted: () => false, + onStatusChange: () => {}, + syncExecutionTimeoutMs: 5_000, + }); + + const startedAt = Date.now(); + const execution = runtime.execute(` + await nct.sleep(1); + /^(a+)+$/.test('a'.repeat(30) + '!'); + `); + setTimeout(() => runtime.stop(new Error("Stopped by user")), 50); + await assert.rejects(execution, /stopped by user/i); + assert.ok(Date.now() - startedAt < 1_000, "explicit stop did not terminate the worker promptly"); + assert.equal(_getActiveScriptWorkerCountForTests(), 0); +}); + +test("createScriptRuntime enforces the worker deadline across promise continuations", async () => { + const runtime = createScriptRuntime({ + sessionId: "s1", + runId: "r-post-await-infinite-loop", + appendLog: () => {}, + writeToSession: () => {}, + getOutputBuffer: () => ({ getText: () => "" }), + getSessionMeta: () => ({ connected: true }), + showDialog: async () => true, + isPaused: () => false, + isAborted: () => false, + onStatusChange: () => {}, + syncExecutionTimeoutMs: 20, + }); + + await assert.rejects( + runtime.execute("await Promise.resolve(); while (true) {}"), + /timed out/i, + ); + await assert.rejects( + runtime.execute("await nct.sleep(1); while (true) {}"), + /timed out/i, + ); + await assert.rejects( + runtime.execute(` + await nct.sleep(1); + Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 250); + `), + /blocking atomics|sharedarraybuffer/i, + ); + const startedAt = Date.now(); + await assert.rejects( + runtime.execute(` + await nct.sleep(1); + /^(a+)+$/.test('a'.repeat(28) + '!'); + `), + /timed out/i, + ); + assert.ok(Date.now() - startedAt < 1_000, "catastrophic regexp was not terminated promptly"); + assert.equal(_getActiveScriptWorkerCountForTests(), 0); +}); + +test("createScriptRuntime drains normal host-backed promise continuations", async () => { + const logs = []; + const runtime = createScriptRuntime({ + sessionId: "s1", + runId: "r-post-await-normal", + appendLog: (_id, message) => logs.push(message), + writeToSession: () => {}, + getOutputBuffer: () => ({ getText: () => "" }), + getSessionMeta: () => ({ connected: true }), + showDialog: async () => true, + isPaused: () => false, + isAborted: () => false, + onStatusChange: () => {}, + syncExecutionTimeoutMs: 20, + }); + + await runtime.execute(` + await nct.sleep(1); + nct.log('first'); + await Promise.resolve(); + await nct.sleep(1); + nct.log(nct.session.connected ? 'second' : 'disconnected'); + `); + assert.deepEqual(logs, ["first", "second"]); + assert.equal(_getActiveScriptWorkerCountForTests(), 0); +}); + +test("createScriptRuntime heartbeat remains healthy while a host request is paused", async () => { + let paused = true; + const writes = []; + const runtime = createScriptRuntime({ + sessionId: "s1", + runId: "r-paused-host-request", + appendLog: () => {}, + writeToSession: (_sessionId, data) => writes.push(data), + getOutputBuffer: () => ({ getText: () => "" }), + getSessionMeta: () => ({ connected: true }), + showDialog: async () => true, + isPaused: () => paused, + isAborted: () => false, + onStatusChange: () => {}, + syncExecutionTimeoutMs: 20, + }); + + const execution = runtime.execute("await nct.screen.send('after-pause');"); + setTimeout(() => { paused = false; }, 80); + await execution; + assert.deepEqual(writes, ["after-pause"]); + assert.equal(_getActiveScriptWorkerCountForTests(), 0); +}); + +test("createScriptRuntime terminates unawaited host requests at the worker boundary", async () => { + let finishDialog; + let disconnectCalls = 0; + const runtime = createScriptRuntime({ + sessionId: "s1", + runId: "r-unawaited-request", + appendLog: () => {}, + writeToSession: () => {}, + getOutputBuffer: () => ({ getText: () => "" }), + getSessionMeta: () => ({ connected: true }), + showDialog: () => new Promise((resolve) => { finishDialog = resolve; }), + disconnectSession: async () => { disconnectCalls += 1; }, + isPaused: () => false, + isAborted: () => false, + onStatusChange: () => {}, + }); + + await runtime.execute(` + void nct.dialog.confirm('background').then(() => nct.session.disconnect()); + nct.log('done'); + `); + assert.equal(_getActiveScriptWorkerCountForTests(), 0); + const cleanupStartedAt = Date.now(); + while (_getActiveScriptHostRequestCountForTests() > 0 && Date.now() - cleanupStartedAt < 500) { + await new Promise((resolve) => setTimeout(resolve, 10)); + } + assert.equal(_getActiveScriptHostRequestCountForTests(), 0); + finishDialog(true); + await new Promise((resolve) => setTimeout(resolve, 20)); + assert.equal(disconnectCalls, 0); +}); + +test("createScriptRuntime terminates a 20k unawaited host-request fan-out at the pending limit", async () => { + const runtime = createScriptRuntime({ + sessionId: "s1", + runId: "r-host-request-fanout", + appendLog: () => {}, + writeToSession: () => {}, + getOutputBuffer: () => ({ getText: () => "" }), + getSessionMeta: () => ({ connected: true }), + showDialog: async () => true, + isPaused: () => false, + isAborted: () => false, + onStatusChange: () => {}, + syncExecutionTimeoutMs: 5_000, + }); + + const startedAt = Date.now(); + await assert.rejects( + runtime.execute("for (let i = 0; i < 20000; i += 1) void nct.sleep(60000);"), + new RegExp(`${SCRIPT_WORKER_MAX_PENDING_HOST_REQUESTS} pending host request limit`, "i"), + ); + assert.ok(Date.now() - startedAt < 1_000, "fan-out was not stopped promptly"); + assert.equal(_getActiveScriptHostRequestCountForTests(), 0); + assert.equal(_getActiveScriptWorkerCountForTests(), 0); +}); + +test("createScriptRuntime still permits normal sequential host requests", async () => { + const logs = []; + const runtime = createScriptRuntime({ + sessionId: "s1", + runId: "r-sequential-host-requests", + appendLog: (_id, message) => logs.push(message), + writeToSession: () => {}, + getOutputBuffer: () => ({ getText: () => "" }), + getSessionMeta: () => ({ connected: true }), + showDialog: async () => true, + isPaused: () => false, + isAborted: () => false, + onStatusChange: () => {}, + }); + + await runtime.execute(` + for (let i = 0; i < 5; i += 1) await nct.sleep(0); + nct.log('done'); + `); + assert.deepEqual(logs, ["done"]); + assert.equal(_getActiveScriptHostRequestCountForTests(), 0); +}); + +test("createScriptRuntime bounds a 200k log notification flood", async () => { + let deliveredLogs = 0; + let statusUpdates = 0; + const runtime = createScriptRuntime({ + sessionId: "s1", + runId: "r-log-flood", + appendLog: () => { deliveredLogs += 1; }, + writeToSession: () => {}, + getOutputBuffer: () => ({ getText: () => "" }), + getSessionMeta: () => ({ connected: true }), + showDialog: async () => true, + isPaused: () => false, + isAborted: () => false, + onStatusChange: () => { statusUpdates += 1; }, + syncExecutionTimeoutMs: 5_000, + }); + + const startedAt = Date.now(); + await assert.rejects( + runtime.execute("for (let i = 0; i < 200000; i += 1) nct.log('x');"), + new RegExp(`${SCRIPT_WORKER_MAX_LOG_NOTIFICATIONS} log notification limit`, "i"), + ); + assert.ok(Date.now() - startedAt < 1_000); + assert.equal(deliveredLogs, SCRIPT_WORKER_MAX_LOG_NOTIFICATIONS); + assert.equal(statusUpdates, SCRIPT_WORKER_MAX_LOG_NOTIFICATIONS); + assert.equal(_getActiveScriptWorkerCountForTests(), 0); +}); + +test("createScriptRuntime coalesces large progress floods before the total budget", async () => { + let statusUpdates = 0; + const runtime = createScriptRuntime({ + sessionId: "s1", + runId: "r-progress-flood", + appendLog: () => {}, + writeToSession: () => {}, + getOutputBuffer: () => ({ getText: () => "" }), + getSessionMeta: () => ({ connected: true }), + showDialog: async () => true, + isPaused: () => false, + isAborted: () => false, + onStatusChange: () => { statusUpdates += 1; }, + syncExecutionTimeoutMs: 5_000, + }); + + await assert.rejects( + runtime.execute(` + nct.progress.start('many', 200000); + for (let i = 0; i < 200000; i += 1) nct.progress.step('item ' + i); + `), + new RegExp(`${SCRIPT_WORKER_MAX_TOTAL_NOTIFICATIONS} notification limit`, "i"), + ); + assert.ok( + statusUpdates <= SCRIPT_WORKER_IMMEDIATE_PROGRESS_NOTIFICATIONS + 4, + `progress broadcast count was not coalesced: ${statusUpdates}`, + ); + assert.equal(_getActiveScriptWorkerCountForTests(), 0); +}); + test("sensitive script input is masked in UI and logs and remains host-bypassed", async () => { const logs = []; const writes = []; diff --git a/electron/terminalWorker/portForwardingSharedPool.test.cjs b/electron/terminalWorker/portForwardingSharedPool.test.cjs new file mode 100644 index 000000000..d2ad2897a --- /dev/null +++ b/electron/terminalWorker/portForwardingSharedPool.test.cjs @@ -0,0 +1,207 @@ +const assert = require("node:assert/strict"); +const EventEmitter = require("node:events"); +const Module = require("node:module"); +const test = require("node:test"); + +let physicalDialCount = 0; + +class MockSshClient extends EventEmitter { + constructor() { + super(); + this._sock = { destroyed: false, writable: true, setTimeout() {} }; + } + + connect() { + physicalDialCount += 1; + setImmediate(() => { + this.emit("connect"); + this.emit("ready"); + }); + } + + end() { + if (this._sock.destroyed) return; + this._sock.destroyed = true; + this._sock.writable = false; + setImmediate(() => this.emit("close")); + } +} + +const originalLoad = Module._load; +Module._load = function mockSsh2(request, parent, isMain) { + if (request === "ssh2") return { Client: MockSshClient }; + return originalLoad.call(this, request, parent, isMain); +}; +const { createTerminalWorkerRuntime } = require("./runtime.cjs"); +const { registerPortForwardingWorkerBridge } = require("./process.cjs"); +// Load the production worker bridge while ssh2 is replaced by the deterministic +// transport used by this integration test. The registration helper then uses +// this exact shared module instance. +require("../bridges/portForwardingBridge.cjs"); +const { + LEASE_KINDS, + borrowTransport, + createTransport, + findTransportByEndpoint, + resetSshTransportRegistryForTests, + returnTransport, +} = require("../bridges/sshConnectionPool.cjs"); +Module._load = originalLoad; + +const endpoint = { + hostId: "worker-host", + hostname: "worker-host.test", + port: 22, + username: "alice", + jumpHosts: [], + proxy: null, + authType: "password", + keyId: "", + certificate: "", + requiresMfa: false, + verifyHostKeys: true, + knownHosts: [{ + id: "kh-worker-host", + hostname: "worker-host.test", + port: 22, + keyType: "ssh-ed25519", + fingerprint: "SHA256:worker-host", + publicKey: "ssh-ed25519 WORKER_HOST_PUBLIC_KEY", + }], + useSshAgent: false, + agentForwarding: false, + password: "worker-password", +}; + +function createParentPort() { + const listeners = new Map(); + const waiters = new Map(); + return { + messages: [], + on(channel, listener) { + listeners.set(channel, listener); + }, + postMessage(message) { + this.messages.push(message); + if (message.kind === "response") { + const waiter = waiters.get(message.requestId); + if (waiter) { + waiters.delete(message.requestId); + message.error ? waiter.reject(new Error(message.error)) : waiter.resolve(message.result); + } + } + }, + request(channel, payload, webContentsId = 7) { + const requestId = `${channel}:${Math.random()}`; + const promise = new Promise((resolve, reject) => waiters.set(requestId, { resolve, reject })); + listeners.get("message")?.({ kind: "request", requestId, channel, payload, webContentsId }); + return promise; + }, + }; +} + +function createHarness() { + const parentPort = createParentPort(); + const siblingHolders = []; + const runtime = createTerminalWorkerRuntime({ + parentPort, + registerBridges(ipcMain) { + registerPortForwardingWorkerBridge(ipcMain); + for (const [channel, kind] of [ + ["netcatty:test:terminal-open", LEASE_KINDS.shell], + ["netcatty:test:sftp-open", LEASE_KINDS.sftp], + ]) { + ipcMain.handle(channel, async (_event, payload) => { + let transport = findTransportByEndpoint(endpoint, { kind: "channel" }); + if (!transport) { + physicalDialCount += 1; + transport = createTransport({ conn: new MockSshClient(), chainConnections: [], endpoint }); + } + const holder = { id: payload.id }; + borrowTransport(transport, { + kind, + holder, + leaseId: `${kind}:${payload.id}`, + }); + siblingHolders.push(holder); + return { id: payload.id }; + }); + } + }, + }); + runtime.start(); + return { parentPort, siblingHolders }; +} + +function portForwardPayload(tunnelId, overrides = {}) { + return { + tunnelId, + ruleId: `rule-${tunnelId}`, + type: "local", + localPort: 0, + bindAddress: "127.0.0.1", + remoteHost: "127.0.0.1", + remotePort: 3306, + ...endpoint, + authMethod: endpoint.authType, + ...overrides, + }; +} + +async function cleanupHarness(parentPort, siblingHolders, tunnelId) { + if (tunnelId) { + await parentPort.request("netcatty:portforward:stop", { tunnelId }).catch(() => {}); + } + for (const holder of siblingHolders) returnTransport(holder); + resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 }); +} + +for (const [label, channel] of [ + ["terminal", "netcatty:test:terminal-open"], + ["SFTP", "netcatty:test:sftp-open"], +]) { + test(`worker ${label} opened after port forwarding reuses one physical SSH dial`, async (t) => { + physicalDialCount = 0; + resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 }); + const { parentPort, siblingHolders } = createHarness(); + t.after(() => cleanupHarness(parentPort, siblingHolders, "pf-first")); + + assert.equal((await parentPort.request( + "netcatty:portforward:start", + portForwardPayload("pf-first"), + )).success, true); + await parentPort.request(channel, { id: `${label}-after` }); + + assert.equal(physicalDialCount, 1); + }); + + test(`worker port forwarding opened after ${label} reuses one physical SSH dial`, async (t) => { + physicalDialCount = 0; + resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 }); + const { parentPort, siblingHolders } = createHarness(); + t.after(() => cleanupHarness(parentPort, siblingHolders, "pf-after")); + + await parentPort.request(channel, { id: `${label}-first` }); + assert.equal((await parentPort.request( + "netcatty:portforward:start", + portForwardPayload("pf-after"), + )).success, true); + + assert.equal(physicalDialCount, 1); + }); +} + +test("worker port forwarding with reuseTransport false stays physically independent", async (t) => { + physicalDialCount = 0; + resetSshTransportRegistryForTests({ defaultIdleTtlMs: 0 }); + const { parentPort, siblingHolders } = createHarness(); + t.after(() => cleanupHarness(parentPort, siblingHolders, "pf-dedicated")); + + await parentPort.request("netcatty:test:sftp-open", { id: "sftp-first" }); + assert.equal((await parentPort.request( + "netcatty:portforward:start", + portForwardPayload("pf-dedicated", { reuseTransport: false }), + )).success, true); + + assert.equal(physicalDialCount, 2); +}); diff --git a/electron/terminalWorker/process.cjs b/electron/terminalWorker/process.cjs index 9e0c28993..c1afa42c0 100644 --- a/electron/terminalWorker/process.cjs +++ b/electron/terminalWorker/process.cjs @@ -218,6 +218,11 @@ function registerExternalSessionHandlers(ipcMain, options) { }); } +function registerPortForwardingWorkerBridge(ipcMain) { + const portForwardingBridge = require("../bridges/portForwardingBridge.cjs"); + portForwardingBridge.registerHandlers(ipcMain); +} + function main() { const parentPort = process.parentPort; if (!parentPort) { @@ -272,7 +277,10 @@ function main() { terminalBridge.init(deps); sftpBridge.init(deps); transferBridge.init(deps); - fileWatcherBridge.init(deps); + fileWatcherBridge.init({ + ...deps, + transferBridge, + }); compressUploadBridge.init({ ...deps, transferBridge, @@ -280,6 +288,7 @@ function main() { sshBridge.registerHandlers(ipcMain); terminalBridge.registerHandlers(ipcMain); sftpBridge.registerHandlers(ipcMain); + registerPortForwardingWorkerBridge(ipcMain); transferBridge.registerHandlers(ipcMain); fileWatcherBridge.registerHandlers(ipcMain); compressUploadBridge.registerHandlers(ipcMain); @@ -368,5 +377,6 @@ module.exports = { createZmodemUploadFileSelector, normalizeParentPortMessage, registerExternalSessionHandlers, + registerPortForwardingWorkerBridge, main, }; diff --git a/electron/terminalWorker/runtime.cjs b/electron/terminalWorker/runtime.cjs index 425dacb39..f6a122924 100644 --- a/electron/terminalWorker/runtime.cjs +++ b/electron/terminalWorker/runtime.cjs @@ -4,6 +4,12 @@ const { logTerminalInterruptDebug, normalizeTrace, } = require("../bridges/terminalInterruptDiagnostics.cjs"); +const { + clearTerminalSessionPerformanceState, +} = require("../bridges/emitTerminalSessionData.cjs"); + +const DEFAULT_SESSION_LIFECYCLE_TOMBSTONE_TTL_MS = 60_000; +const DEFAULT_MAX_SESSION_LIFECYCLE_TOMBSTONES = 2_048; const SESSION_START_CHANNELS = new Set([ "netcatty:start", @@ -162,6 +168,7 @@ function createSender( sessionOutputGenerations = new Map(), sessionRequestIds = new Map(), fixedOriginRequestId = null, + onCurrentSessionExit = null, ) { const ownedSessionGenerations = new Map(); const getOwnedSessionGeneration = (sessionId) => { @@ -201,6 +208,7 @@ function createSender( pendingOutputBySession.delete(payload.sessionId); outputPorts?.closeSession?.(payload.sessionId); terminalDataPipeline?.detach?.(payload.sessionId, undefined, "session-closed"); + onCurrentSessionExit?.(payload.sessionId, sessionGeneration); } } parentPort.postMessage({ @@ -350,8 +358,58 @@ function createTerminalWorkerRuntime(options = {}) { const sessionOperationTails = new Map(); const sessionOperationKinds = new Map(); const sessionCloseEpochs = new Map(); + const sessionLifecycleTombstoneTimes = new Map(); const sessionStartMarkers = new Set(); let urgentInputPorts = null; + const now = typeof options.now === "function" ? options.now : Date.now; + const sessionLifecycleTombstoneTtlMs = Number.isFinite(options.sessionLifecycleTombstoneTtlMs) + ? Math.max(0, Number(options.sessionLifecycleTombstoneTtlMs)) + : DEFAULT_SESSION_LIFECYCLE_TOMBSTONE_TTL_MS; + const maxSessionLifecycleTombstones = Number.isFinite(options.maxSessionLifecycleTombstones) + ? Math.max(1, Math.floor(Number(options.maxSessionLifecycleTombstones))) + : DEFAULT_MAX_SESSION_LIFECYCLE_TOMBSTONES; + + function canPruneSessionLifecycleTombstone(sessionId) { + return !sessionOperationTails.has(sessionId) + && !sessionStartMarkers.has(sessionId) + && !pendingOutputBySession.has(sessionId); + } + + function deleteSessionLifecycleTombstone(sessionId) { + sessionLifecycleTombstoneTimes.delete(sessionId); + sessionOutputGenerations.delete(sessionId); + sessionCloseEpochs.delete(sessionId); + } + + function pruneSessionLifecycleTombstones() { + const currentTime = now(); + for (const [sessionId, closedAt] of sessionLifecycleTombstoneTimes) { + if (currentTime - closedAt < sessionLifecycleTombstoneTtlMs) continue; + if (!canPruneSessionLifecycleTombstone(sessionId)) continue; + deleteSessionLifecycleTombstone(sessionId); + } + if (sessionLifecycleTombstoneTimes.size <= maxSessionLifecycleTombstones) return; + for (const sessionId of [...sessionLifecycleTombstoneTimes.keys()]) { + if (sessionLifecycleTombstoneTimes.size <= maxSessionLifecycleTombstones) break; + if (!canPruneSessionLifecycleTombstone(sessionId)) continue; + deleteSessionLifecycleTombstone(sessionId); + } + } + + function touchSessionLifecycleTombstone(sessionId) { + if (!sessionId) return; + sessionLifecycleTombstoneTimes.delete(sessionId); + sessionLifecycleTombstoneTimes.set(sessionId, now()); + } + + function finalizeNaturalSessionExit(sessionId) { + if (!sessionId) return; + sessionStartMarkers.delete(sessionId); + sessionRequestIds.delete(sessionId); + clearTerminalSessionPerformanceState(sessionId); + touchSessionLifecycleTombstone(sessionId); + pruneSessionLifecycleTombstones(); + } const createWorkerOutputSender = () => createSender( parentPort, @@ -361,6 +419,8 @@ function createTerminalWorkerRuntime(options = {}) { pendingOutputBySession, sessionOutputGenerations, sessionRequestIds, + null, + finalizeNaturalSessionExit, ); function replayWorkerOutput(sessionId, chunks) { @@ -389,6 +449,9 @@ function createTerminalWorkerRuntime(options = {}) { outputPorts.closeSession(sessionId); terminalDataPipeline?.detach?.(sessionId, undefined, "session-closed"); sessionRequestIds.delete(sessionId); + clearTerminalSessionPerformanceState(sessionId); + touchSessionLifecycleTombstone(sessionId); + pruneSessionLifecycleTombstones(); } async function handleRequest(message) { @@ -404,7 +467,14 @@ function createTerminalWorkerRuntime(options = {}) { try { const isSessionStart = SESSION_START_CHANNELS.has(message.channel); const requestedSessionId = isSessionStart ? message.payload?.sessionId : null; - if (requestedSessionId) sessionRequestIds.set(requestedSessionId, message.requestId); + const naturalExitGenerations = new Map(); + const requestedSessionGeneration = requestedSessionId + ? (sessionOutputGenerations.get(requestedSessionId) ?? 0) + : null; + if (requestedSessionId) { + sessionRequestIds.set(requestedSessionId, message.requestId); + sessionLifecycleTombstoneTimes.delete(requestedSessionId); + } const result = await handler({ sender: createSender( parentPort, @@ -415,12 +485,28 @@ function createTerminalWorkerRuntime(options = {}) { sessionOutputGenerations, sessionRequestIds, isSessionStart ? message.requestId : null, + (sessionId, generation) => { + naturalExitGenerations.set(sessionId, generation); + finalizeNaturalSessionExit(sessionId); + }, ), }, message.payload); const sessionId = result?.sessionId; if (isSessionStart && sessionId) { - sessionRequestIds.set(sessionId, message.requestId); - sessionStartMarkers.add(sessionId); + const currentGeneration = sessionOutputGenerations.get(sessionId) ?? 0; + const exitedGeneration = naturalExitGenerations.get(sessionId); + const exitedDuringRequest = exitedGeneration !== undefined + && currentGeneration > exitedGeneration; + const requestedGenerationStillCurrent = requestedSessionGeneration === null + || currentGeneration === requestedSessionGeneration; + if (!exitedDuringRequest && requestedGenerationStillCurrent) { + sessionRequestIds.set(sessionId, message.requestId); + sessionStartMarkers.add(sessionId); + sessionLifecycleTombstoneTimes.delete(sessionId); + } else { + sessionRequestIds.delete(sessionId); + sessionStartMarkers.delete(sessionId); + } } parentPort.postMessage({ kind: "response", @@ -460,6 +546,8 @@ function createTerminalWorkerRuntime(options = {}) { pendingOutputBySession, sessionOutputGenerations, sessionRequestIds, + null, + finalizeNaturalSessionExit, ), }, { sessionId }); } @@ -480,11 +568,13 @@ function createTerminalWorkerRuntime(options = {}) { if (sessionOperationTails.get(sessionId) === operation) { sessionOperationTails.delete(sessionId); sessionOperationKinds.delete(sessionId); + pruneSessionLifecycleTombstones(); } }); } function dispatchRequest(message) { + pruneSessionLifecycleTombstones(); const sessionId = SESSION_START_CHANNELS.has(message.channel) ? message.payload?.sessionId : null; @@ -528,6 +618,7 @@ function createTerminalWorkerRuntime(options = {}) { return; } sessionCloseEpochs.set(sessionId, (sessionCloseEpochs.get(sessionId) ?? 0) + 1); + touchSessionLifecycleTombstone(sessionId); const previous = sessionOperationTails.get(sessionId); const current = (previous || Promise.resolve()).catch(() => {}).then(async () => { try { @@ -543,6 +634,7 @@ function createTerminalWorkerRuntime(options = {}) { } }); trackSessionOperation(sessionId, current, "close"); + pruneSessionLifecycleTombstones(); } function handleSend(message) { @@ -567,6 +659,9 @@ function createTerminalWorkerRuntime(options = {}) { terminalDataPipeline, pendingOutputBySession, sessionOutputGenerations, + sessionRequestIds, + null, + finalizeNaturalSessionExit, ), }, message.payload); } @@ -678,11 +773,20 @@ function createTerminalWorkerRuntime(options = {}) { pendingOutputBySession, sessionOutputGenerations, sessionRequestIds, + null, + finalizeNaturalSessionExit, ); }, closeUrgentInputPortsForTest() { urgentInputPorts?.closeAll(); }, + _getSessionLifecycleStateCountsForTests() { + pruneSessionLifecycleTombstones(); + return { + outputGenerations: sessionOutputGenerations.size, + closeEpochs: sessionCloseEpochs.size, + }; + }, }; } diff --git a/electron/terminalWorker/runtime.test.cjs b/electron/terminalWorker/runtime.test.cjs index aadac99ef..8c7533ebf 100644 --- a/electron/terminalWorker/runtime.test.cjs +++ b/electron/terminalWorker/runtime.test.cjs @@ -395,6 +395,43 @@ test("runtime tags a failing start exit so its queued replacement can distinguis )), true); }); +test("runtime prunes sessions that naturally exit before their start response completes", async () => { + const parentPort = createParentPort(); + const runtime = createTerminalWorkerRuntime({ + parentPort, + sessionLifecycleTombstoneTtlMs: 60_000, + maxSessionLifecycleTombstones: 1, + registerBridges(ipcMain) { + ipcMain.handle("netcatty:local:start", async (event, payload) => { + const generation = event.sender.claimSessionGeneration(payload.sessionId); + event.sender.send("netcatty:exit", { + sessionId: payload.sessionId, + reason: "exited-during-start", + _terminalSessionGeneration: generation, + }); + return { sessionId: payload.sessionId }; + }); + }, + }); + runtime.start(); + + for (let index = 0; index < 3; index += 1) { + parentPort.emitMessage({ + kind: "request", + requestId: `start-exit-${index}`, + channel: "netcatty:local:start", + payload: { sessionId: `start-exit-${index}` }, + webContentsId: 7, + }); + await new Promise((resolve) => setImmediate(resolve)); + } + + assert.deepEqual(runtime._getSessionLifecycleStateCountsForTests(), { + outputGenerations: 1, + closeEpochs: 0, + }); +}); + test("runtime routes interceptor ports to the worker-owned data pipeline", () => { const parentPort = createParentPort(); const attached = []; @@ -513,6 +550,148 @@ test("stale backend output is rejected before taps, observation, or interception assert.deepEqual(parentPort.messages.map((message) => message.kind), ["output-tap", "output"]); }); +test("runtime bounds closed lifecycle generations and safely reuses a recent session id", async () => { + const parentPort = createParentPort(); + let now = 0; + const runtime = createTerminalWorkerRuntime({ + parentPort, + now: () => now, + sessionLifecycleTombstoneTtlMs: 100, + maxSessionLifecycleTombstones: 8, + registerBridges(ipcMain) { + ipcMain.on("netcatty:close", () => {}); + }, + }); + runtime.start(); + + const recentSenders = new Map(); + for (let index = 0; index < 24; index += 1) { + const sessionId = `closed-${index}`; + const sender = runtime.createSender(7); + recentSenders.set(sessionId, { + sender, + generation: sender.claimSessionGeneration(sessionId), + }); + parentPort.emitMessage({ + kind: "send", + channel: "netcatty:close", + payload: { sessionId }, + webContentsId: 7, + }); + } + await new Promise((resolve) => setImmediate(resolve)); + await new Promise((resolve) => setImmediate(resolve)); + + assert.deepEqual(runtime._getSessionLifecycleStateCountsForTests(), { + outputGenerations: 8, + closeEpochs: 8, + }); + + const sessionId = "closed-23"; + const recent = recentSenders.get(sessionId); + parentPort.messages.length = 0; + now = 99; + recent.sender.send("netcatty:data", { + sessionId, + data: "stale", + _terminalSessionGeneration: recent.generation, + }); + await new Promise((resolve) => setImmediate(resolve)); + assert.deepEqual(parentPort.messages, [], "old sender stays fenced during the safety window"); + + const replacement = runtime.createSender(8); + const replacementGeneration = replacement.claimSessionGeneration(sessionId); + replacement.send("netcatty:data", { + sessionId, + data: "fresh", + _terminalSessionGeneration: replacementGeneration, + }); + await new Promise((resolve) => setImmediate(resolve)); + assert.ok(replacementGeneration > recent.generation); + assert.deepEqual(parentPort.messages.map((message) => message.kind), ["output-tap", "output"]); +}); + +test("runtime bounds natural-exit lifecycle generations and allows same-id restart", async () => { + const parentPort = createParentPort(); + let now = 0; + let starts = 0; + const senders = new Map(); + const generations = new Map(); + const runtime = createTerminalWorkerRuntime({ + parentPort, + now: () => now, + sessionLifecycleTombstoneTtlMs: 100, + maxSessionLifecycleTombstones: 8, + registerBridges(ipcMain) { + ipcMain.handle("netcatty:local:start", async (event, payload) => { + starts += 1; + senders.set(payload.sessionId, event.sender); + generations.set(payload.sessionId, event.sender.claimSessionGeneration(payload.sessionId)); + return { sessionId: payload.sessionId }; + }); + }, + }); + runtime.start(); + + for (let index = 0; index < 24; index += 1) { + const sessionId = `natural-exit-${index}`; + parentPort.emitMessage({ + kind: "request", + requestId: `start-${index}`, + channel: "netcatty:local:start", + payload: { sessionId }, + webContentsId: 7, + }); + await new Promise((resolve) => setImmediate(resolve)); + senders.get(sessionId).send("netcatty:exit", { + sessionId, + reason: "exited", + _terminalSessionGeneration: generations.get(sessionId), + }); + } + await new Promise((resolve) => setImmediate(resolve)); + + assert.deepEqual(runtime._getSessionLifecycleStateCountsForTests(), { + outputGenerations: 8, + closeEpochs: 0, + }); + + const sessionId = "natural-exit-23"; + const exitedSender = senders.get(sessionId); + const exitedGeneration = generations.get(sessionId); + parentPort.messages.length = 0; + now = 99; + exitedSender.send("netcatty:data", { + sessionId, + data: "late-output", + _terminalSessionGeneration: exitedGeneration, + }); + await new Promise((resolve) => setImmediate(resolve)); + assert.deepEqual(parentPort.messages, [], "natural exit keeps late output fenced"); + + parentPort.emitMessage({ + kind: "request", + requestId: "restart-natural-exit", + channel: "netcatty:local:start", + payload: { sessionId }, + webContentsId: 8, + }); + await new Promise((resolve) => setImmediate(resolve)); + const replacementSender = senders.get(sessionId); + const replacementGeneration = generations.get(sessionId); + assert.ok(replacementGeneration > exitedGeneration); + assert.equal(starts, 25); + + parentPort.messages.length = 0; + replacementSender.send("netcatty:data", { + sessionId, + data: "fresh-output", + _terminalSessionGeneration: replacementGeneration, + }); + await new Promise((resolve) => setImmediate(resolve)); + assert.deepEqual(parentPort.messages.map((message) => message.kind), ["output-tap", "output"]); +}); + test("runtime invokes fire-and-forget listeners", () => { const parentPort = createParentPort(); const calls = []; diff --git a/global.d.ts b/global.d.ts index bf3b3f77b..517b9104a 100644 --- a/global.d.ts +++ b/global.d.ts @@ -160,16 +160,10 @@ declare global { // When set, reuse the already-authenticated SSH connection of this existing // session by opening a new shell channel on it, instead of dialing a fresh // connection. Lets a duplicated tab skip a second MFA prompt (issue #1204). - // The bridge falls back to a fresh connection if the source is gone, unless - // reuseOnly is also set. + // The bridge falls back to a fresh connection if the source is gone. sourceSessionId?: string; // Skip POSIX process discovery when copying a network-device session. skipShellPidDiscovery?: boolean; - // When true with sourceSessionId: (1) fail instead of falling back to a - // fresh SSH dial, and (2) skip renderer endpoint matching so a Connected - // picker probe can reuse the named live session even if session.username - // /port lag the authenticated bridge endpoint. - reuseOnly?: boolean; /** * When false, openSftp must dial a fresh SSH connection and must not * borrow a parked/shared registry transport. Used for dedicated bulk diff --git a/infrastructure/ai/harness/agentRuntime.test.ts b/infrastructure/ai/harness/agentRuntime.test.ts index 8c29a7c10..a12469c6a 100644 --- a/infrastructure/ai/harness/agentRuntime.test.ts +++ b/infrastructure/ai/harness/agentRuntime.test.ts @@ -22,6 +22,22 @@ class MockTurnDriver implements TurnDriver { abort(): void {} } +test('AgentRuntime clearChatSession releases its trace history', () => { + const traceStore = new TraceStore(); + const runtime = new AgentRuntime({ drivers: [new MockTurnDriver()], traceStore }); + traceStore.append({ + id: 'trace-clear-1', + type: 'turn_start', + sessionId: 'chat-clear', + turnId: 'turn-clear', + startedAt: Date.now(), + } as import('./types').AgentEvent); + + runtime.clearChatSession('chat-clear'); + assert.equal(traceStore.getEvents('chat-clear').length, 0); + assert.equal(traceStore.getCompactions('chat-clear').length, 0); +}); + test('AgentRuntime runTurn emits turn lifecycle and records trace', async () => { const traceStore = new TraceStore(); const driver = new MockTurnDriver(); diff --git a/infrastructure/ai/harness/agentRuntime.ts b/infrastructure/ai/harness/agentRuntime.ts index 7c6e42169..eb5bf7286 100644 --- a/infrastructure/ai/harness/agentRuntime.ts +++ b/infrastructure/ai/harness/agentRuntime.ts @@ -72,6 +72,7 @@ export class AgentRuntime { clearChatSession(chatSessionId: string): void { this.toolOutputStore.prune(chatSessionId); this.sessionStateStore.clear(chatSessionId); + this.traceStore.clear(chatSessionId); globalTwoPassCompactionCache.clear(chatSessionId); globalTerminalMonitorGuard.clearPrefix(`${chatSessionId}:`); } diff --git a/infrastructure/ai/harness/contextManager.test.ts b/infrastructure/ai/harness/contextManager.test.ts index cd94394ba..800b670ab 100644 --- a/infrastructure/ai/harness/contextManager.test.ts +++ b/infrastructure/ai/harness/contextManager.test.ts @@ -132,6 +132,19 @@ test('TraceStore records compaction events for export', async () => { assert.equal(exported.compactions[0]?.trigger, 'force'); }); +test('TraceStore bounds compaction history independently from events', () => { + const store = new TraceStore(20, 3); + for (let index = 0; index < 10; index += 1) { + store.append({ + id: `compaction-${index}`, + type: 'compaction', + sessionId: 'chat-bounded-compactions', + trace: { trigger: 'force' }, + } as import('./types').AgentEvent); + } + assert.equal(store.getCompactions('chat-bounded-compactions').length, 3); +}); + test('prepareStepContext replaces prior step handle notices under v7 carry-forward semantics', async () => { const store = new ToolOutputStore(); store.store({ diff --git a/infrastructure/ai/harness/toolOutputStore.test.ts b/infrastructure/ai/harness/toolOutputStore.test.ts index fb04d905b..4aa6a0512 100644 --- a/infrastructure/ai/harness/toolOutputStore.test.ts +++ b/infrastructure/ai/harness/toolOutputStore.test.ts @@ -1,6 +1,8 @@ import assert from 'node:assert/strict'; import test from 'node:test'; import { + TOOL_OUTPUT_MAX_CLOSED_TERMINAL_SESSIONS, + TOOL_OUTPUT_MAX_FAILED_SESSION_DELETIONS, TOOL_OUTPUT_READ_MAX_CHARS, type PersistedToolOutputRecord, type ToolOutputPersistence, @@ -638,6 +640,344 @@ test('ToolOutputStore rejects cross-chat handle reads', async () => { assert.equal(await store.readChunkAsync({ handleId: handle.id }, 'chat-other'), null); }); +test('ToolOutputStore reclaims chat generations after deletion churn settles', () => { + const store = new ToolOutputStore(); + for (let index = 0; index < 2_000; index += 1) { + store.prune(`chat-deleted-${index}`); + } + + assert.equal(store.getLifecycleMetadataStatsForTests().sessionGenerations, 0); +}); + +test('ToolOutputStore rejects output that arrives after its chat was deleted', () => { + const store = new ToolOutputStore(); + store.prune('chat-deleted-before-late-output'); + + const lateHandle = store.store({ + chatSessionId: 'chat-deleted-before-late-output', + capabilityId: 'terminal.execute', + content: 'late output', + }); + + assert.equal(lateHandle.evicted, true); + assert.equal(lateHandle.storedChars, 0); + assert.equal(store.listPendingHandles('chat-deleted-before-late-output').length, 0); +}); + +test('ToolOutputStore reclaims per-chat terminal deletion metadata after churn settles', () => { + const store = new ToolOutputStore(); + for (let index = 0; index < 2_000; index += 1) { + store.pruneTerminalSession(`chat-${index}`, `terminal-${index}`); + } + + const stats = store.getLifecycleMetadataStatsForTests(); + assert.equal(stats.terminalMutationGenerations, 0); + assert.equal(stats.deletedTerminalSessions, 0); +}); + +test('ToolOutputStore bounds closed-terminal tombstones while isolating recent late writes', async () => { + const store = new ToolOutputStore(); + const churnCount = TOOL_OUTPUT_MAX_CLOSED_TERMINAL_SESSIONS + 2_000; + for (let index = 0; index < churnCount; index += 1) { + store.pruneTerminalSessionEverywhere(`terminal-closed-${index}`); + } + + assert.equal( + store.getLifecycleMetadataStatsForTests().closedTerminalSessions, + TOOL_OUTPUT_MAX_CLOSED_TERMINAL_SESSIONS, + ); + const lateHandle = store.store({ + chatSessionId: 'chat-late-after-churn', + capabilityId: 'terminal.execute', + sessionId: `terminal-closed-${churnCount - 1}`, + content: 'late output after heavy churn', + }); + assert.equal(lateHandle.evicted, true); + assert.equal(store.listPendingHandles('chat-late-after-churn').length, 0); + assert.equal(await store.readChunkAsync({ handleId: lateHandle.id }, 'chat-late-after-churn'), null); + const oldestLateHandle = store.store({ + chatSessionId: 'chat-oldest-late-after-churn', + capabilityId: 'terminal.execute', + sessionId: 'terminal-closed-0', + content: 'very late output after exact tombstone eviction', + }); + assert.equal(oldestLateHandle.evicted, true); + assert.equal(store.listPendingHandles('chat-oldest-late-after-churn').length, 0); +}); + +test('ToolOutputStore bounds fallback terminal metadata when durable deletion is unavailable', () => { + const store = new ToolOutputStore({ + persistence: { + write: async () => '/unused', + restore: async () => null, + read: async () => null, + delete: async () => {}, + }, + }); + for (let index = 0; index < 2_000; index += 1) { + store.pruneTerminalSession(`chat-fallback-${index}`, `terminal-fallback-${index}`); + } + + const stats = store.getLifecycleMetadataStatsForTests(); + assert.equal(stats.terminalMutationGenerations, TOOL_OUTPUT_MAX_CLOSED_TERMINAL_SESSIONS); + assert.equal(stats.deletedTerminalSessions, TOOL_OUTPUT_MAX_CLOSED_TERMINAL_SESSIONS); +}); + +test('ToolOutputStore keeps an in-flight restore tombstone protected during metadata churn', async () => { + const chatSessionId = 'chat-protected-restore'; + const terminalSessionId = 'terminal-protected-restore'; + const handleId = 'tool-output-protected-restore'; + let finishRestore!: (value: { path: string; record: PersistedToolOutputRecord }) => void; + const restoreFinished = new Promise<{ path: string; record: PersistedToolOutputRecord }>(resolve => { + finishRestore = resolve; + }); + const deletedPaths: string[] = []; + const store = new ToolOutputStore({ + persistence: { + write: async () => '/unused', + restore: async (requestedHandleId, requestedChatSessionId) => { + if (requestedHandleId !== handleId || requestedChatSessionId !== chatSessionId) return null; + return restoreFinished; + }, + read: async () => null, + delete: async path => { deletedPaths.push(path); }, + }, + }); + + const reading = store.readChunkAsync({ handleId }, chatSessionId); + store.pruneTerminalSession(chatSessionId, terminalSessionId); + for (let index = 0; index < 2_000; index += 1) { + store.pruneTerminalSession(`chat-churn-${index}`, `terminal-churn-${index}`); + } + finishRestore({ + path: '/netcatty/protected-old-output.log', + record: { + schemaVersion: 1, + handleId, + chatSessionId, + capabilityId: 'terminal.execute', + terminalSessionId, + totalChars: 3, + storedChars: 3, + sourceTruncated: false, + preview: 'old', + storedAt: 1, + accessedAt: 1, + }, + }); + + assert.equal(await reading, null); + await new Promise(resolve => setTimeout(resolve, 0)); + assert.deepEqual(deletedPaths, ['/netcatty/protected-old-output.log']); + const stats = store.getLifecycleMetadataStatsForTests(); + assert.ok(stats.terminalMutationGenerations <= TOOL_OUTPUT_MAX_CLOSED_TERMINAL_SESSIONS); + assert.ok(stats.deletedTerminalSessions <= TOOL_OUTPUT_MAX_CLOSED_TERMINAL_SESSIONS); +}); + +test('ToolOutputStore retains a bounded tombstone when durable terminal deletion fails', async () => { + const record: PersistedToolOutputRecord = { + schemaVersion: 1, + handleId: 'tool-output-delete-failed', + chatSessionId: 'chat-delete-failed', + capabilityId: 'terminal.execute', + terminalSessionId: 'terminal-delete-failed', + totalChars: 3, + storedChars: 3, + sourceTruncated: false, + preview: 'old', + storedAt: 1, + accessedAt: 1, + }; + const deletedPaths: string[] = []; + const store = new ToolOutputStore({ + persistence: { + write: async () => '/unused', + restore: async () => ({ path: '/netcatty/delete-failed.log', record }), + read: async () => ({ + mode: 'head', + content: 'old', + totalChars: 3, + startOffset: 0, + endOffset: 3, + nextOffset: 3, + hasMore: false, + }), + delete: async path => { deletedPaths.push(path); }, + deleteTerminalSession: async () => { throw new Error('disk busy'); }, + }, + }); + + store.pruneTerminalSession(record.chatSessionId, record.terminalSessionId!); + await new Promise(resolve => setTimeout(resolve, 0)); + + assert.equal(await store.readChunkAsync({ handleId: record.handleId }, record.chatSessionId), null); + assert.deepEqual(deletedPaths, ['/netcatty/delete-failed.log']); + const stats = store.getLifecycleMetadataStatsForTests(); + assert.equal(stats.terminalMutationGenerations, 1); + assert.equal(stats.deletedTerminalSessions, 1); +}); + +test('ToolOutputStore ignores an older terminal deletion failure after a newer deletion succeeds', async () => { + let rejectFirstDeletion!: (error: Error) => void; + let finishSecondDeletion!: () => void; + const firstDeletion = new Promise((_resolve, reject) => { + rejectFirstDeletion = reject; + }); + const secondDeletion = new Promise(resolve => { + finishSecondDeletion = resolve; + }); + let deletionCalls = 0; + const store = new ToolOutputStore({ + persistence: { + write: async () => '/unused', + restore: async () => null, + read: async () => null, + delete: async () => {}, + deleteTerminalSession: async () => { + deletionCalls += 1; + return deletionCalls === 1 ? firstDeletion : secondDeletion; + }, + }, + }); + + store.pruneTerminalSession('chat-repeat-delete', 'terminal-repeat-delete'); + store.pruneTerminalSession('chat-repeat-delete', 'terminal-repeat-delete'); + finishSecondDeletion(); + await new Promise(resolve => setTimeout(resolve, 0)); + rejectFirstDeletion(new Error('older deletion failed')); + await new Promise(resolve => setTimeout(resolve, 0)); + + const stats = store.getLifecycleMetadataStatsForTests(); + assert.equal(stats.terminalMutationGenerations, 0); + assert.equal(stats.deletedTerminalSessions, 0); + assert.equal(stats.failedTerminalDeletions, 0); +}); + +test('ToolOutputStore does not restore old output after durable chat deletion fails', async () => { + const record: PersistedToolOutputRecord = { + schemaVersion: 1, + handleId: 'tool-output-chat-delete-failed', + chatSessionId: 'chat-delete-failed', + capabilityId: 'terminal.execute', + totalChars: 3, + storedChars: 3, + sourceTruncated: false, + preview: 'old', + storedAt: 1, + accessedAt: 1, + }; + const deletedPaths: string[] = []; + const store = new ToolOutputStore({ + persistence: { + write: async () => '/unused', + restore: async () => ({ path: '/netcatty/chat-delete-failed.log', record }), + read: async () => ({ + mode: 'head', + content: 'old', + totalChars: 3, + startOffset: 0, + endOffset: 3, + nextOffset: 3, + hasMore: false, + }), + delete: async path => { deletedPaths.push(path); }, + deleteSession: async () => { throw new Error('disk busy'); }, + }, + }); + + store.prune(record.chatSessionId); + await new Promise(resolve => setTimeout(resolve, 0)); + + assert.equal(await store.readChunkAsync({ handleId: record.handleId }, record.chatSessionId), null); + assert.deepEqual(deletedPaths, ['/netcatty/chat-delete-failed.log']); +}); + +test('ToolOutputStore bounds failed chat deletion tombstones after heavy churn', async () => { + const oldRecord: PersistedToolOutputRecord = { + schemaVersion: 1, + handleId: 'tool-output-old-failed-chat', + chatSessionId: 'chat-delete-failure-0', + capabilityId: 'terminal.execute', + totalChars: 3, + storedChars: 3, + sourceTruncated: false, + preview: 'old', + storedAt: 1, + accessedAt: 1, + }; + const store = new ToolOutputStore({ + persistence: { + write: async () => '/unused', + restore: async (handleId, chatSessionId) => ( + handleId === oldRecord.handleId && chatSessionId === oldRecord.chatSessionId + ? { path: '/old-failed-chat.log', record: oldRecord } + : null + ), + read: async () => ({ + mode: 'head', content: 'old', totalChars: 3, startOffset: 0, endOffset: 3, nextOffset: 3, hasMore: false, + }), + delete: async () => {}, + deleteSession: async () => { throw new Error('disk busy'); }, + }, + }); + for (let index = 0; index < 2_000; index += 1) { + store.prune(`chat-delete-failure-${index}`); + } + await new Promise(resolve => setTimeout(resolve, 0)); + + const stats = store.getLifecycleMetadataStatsForTests(); + assert.equal(stats.failedSessionDeletions, TOOL_OUTPUT_MAX_FAILED_SESSION_DELETIONS); + assert.equal(stats.sessionGenerations, TOOL_OUTPUT_MAX_FAILED_SESSION_DELETIONS); + assert.equal( + await store.readChunkAsync({ handleId: oldRecord.handleId }, oldRecord.chatSessionId), + null, + ); +}); + +test('ToolOutputStore never restores a failed terminal deletion after exact tombstone churn', async () => { + const oldRecord: PersistedToolOutputRecord = { + schemaVersion: 1, + handleId: 'tool-output-old-failed-terminal', + chatSessionId: 'chat-terminal-failure-0', + capabilityId: 'terminal.execute', + terminalSessionId: 'terminal-failure-0', + totalChars: 3, + storedChars: 3, + sourceTruncated: false, + preview: 'old', + storedAt: 1, + accessedAt: 1, + }; + const store = new ToolOutputStore({ + persistence: { + write: async () => '/unused', + restore: async (handleId, chatSessionId) => ( + handleId === oldRecord.handleId && chatSessionId === oldRecord.chatSessionId + ? { path: '/old-failed-terminal.log', record: oldRecord } + : null + ), + read: async () => ({ + mode: 'head', content: 'old', totalChars: 3, startOffset: 0, endOffset: 3, nextOffset: 3, hasMore: false, + }), + delete: async () => {}, + deleteTerminalSession: async () => { throw new Error('disk busy'); }, + }, + }); + for (let index = 0; index < 2_000; index += 1) { + store.pruneTerminalSession(`chat-terminal-failure-${index}`, `terminal-failure-${index}`); + } + await new Promise(resolve => setTimeout(resolve, 0)); + + assert.equal( + await store.readChunkAsync({ handleId: oldRecord.handleId }, oldRecord.chatSessionId), + null, + ); + assert.ok( + store.getLifecycleMetadataStatsForTests().deletedTerminalSessions + <= TOOL_OUTPUT_MAX_CLOSED_TERMINAL_SESSIONS, + ); +}); + test('saved-output read budgets reset at the start of each turn', () => { const dedup = new ToolResultDedup(); dedup.beginTurn(); diff --git a/infrastructure/ai/harness/toolOutputStore.ts b/infrastructure/ai/harness/toolOutputStore.ts index aee7262bc..089304e88 100644 --- a/infrastructure/ai/harness/toolOutputStore.ts +++ b/infrastructure/ai/harness/toolOutputStore.ts @@ -65,10 +65,51 @@ export const TOOL_OUTPUT_MAX_HANDLES_PER_SESSION = 64; export const TOOL_OUTPUT_MAX_CHARS_PER_SESSION = 8_000_000; export const TOOL_OUTPUT_MAX_HANDLES_GLOBAL = 256; export const TOOL_OUTPUT_MAX_CHARS_GLOBAL = 32_000_000; +export const TOOL_OUTPUT_MAX_CLOSED_TERMINAL_SESSIONS = 1_024; +export const TOOL_OUTPUT_MAX_FAILED_SESSION_DELETIONS = 1_024; export const TOOL_OUTPUT_TTL_MS = 30 * 60 * 1_000; export const TOOL_OUTPUT_SPILL_THRESHOLD_CHARS = 0; const TOOL_OUTPUT_SEARCH_CONTEXT_CHARS = 320; const TOOL_OUTPUT_SEARCH_MAX_MATCHES = 20; +const TOOL_OUTPUT_LIFECYCLE_BLOOM_BITS = 1 << 22; +const TOOL_OUTPUT_LIFECYCLE_BLOOM_HASHES = 4; + +/** + * Fixed-memory deny set with no false negatives. Lifecycle ids are UUID-like + * and never intentionally reused, so rare false positives are safer than + * accepting output for a deleted chat/terminal after exact tombstone churn. + */ +class FixedStringBloomFilter { + private readonly words = new Uint32Array(TOOL_OUTPUT_LIFECYCLE_BLOOM_BITS >>> 5); + + add(value: string): void { + for (const bit of this.bitsFor(value)) { + this.words[bit >>> 5] |= 1 << (bit & 31); + } + } + + has(value: string): boolean { + for (const bit of this.bitsFor(value)) { + if ((this.words[bit >>> 5] & (1 << (bit & 31))) === 0) return false; + } + return true; + } + + private bitsFor(value: string): number[] { + let first = 0x811c9dc5; + let second = 0x9e3779b9; + for (let index = 0; index < value.length; index += 1) { + const code = value.charCodeAt(index); + first = Math.imul(first ^ code, 0x01000193); + second = Math.imul(second ^ (code + index), 0x85ebca6b); + } + second |= 1; + const mask = TOOL_OUTPUT_LIFECYCLE_BLOOM_BITS - 1; + return Array.from({ length: TOOL_OUTPUT_LIFECYCLE_BLOOM_HASHES }, (_, index) => ( + (first + Math.imul(index, second)) >>> 0 + ) & mask); + } +} export interface ToolOutputPersistence { write(record: PersistedToolOutputRecord, content: string): Promise; @@ -134,9 +175,13 @@ export class ToolOutputStore { private readonly restorePromises = new Map>(); private readonly sessionGenerations = new Map(); private readonly sessionDeletionPromises = new Map>(); + private readonly failedSessionDeletions = new Set(); private readonly terminalMutationGenerations = new Map(); - private readonly deletedTerminalSessions = new Set(); + private readonly terminalDeletionPromises = new Map>(); + private readonly failedTerminalDeletions = new Set(); + private readonly deletedTerminalSessions = new Map(); private readonly closedTerminalSessions = new Set(); + private readonly lifecycleDenyFilter = new FixedStringBloomFilter(); private persistence?: ToolOutputPersistence; constructor(options: ToolOutputStoreOptions = {}) { @@ -159,10 +204,51 @@ export class ToolOutputStore { return this.resolveRestartPersistenceNoticesValue(value, chatSessionId) as T; } + getLifecycleMetadataStatsForTests(): { + sessionGenerations: number; + failedSessionDeletions: number; + terminalMutationGenerations: number; + failedTerminalDeletions: number; + deletedTerminalSessions: number; + closedTerminalSessions: number; + } { + return { + sessionGenerations: this.sessionGenerations.size, + failedSessionDeletions: this.failedSessionDeletions.size, + terminalMutationGenerations: this.terminalMutationGenerations.size, + failedTerminalDeletions: this.failedTerminalDeletions.size, + deletedTerminalSessions: this.deletedTerminalSessions.size, + closedTerminalSessions: this.closedTerminalSessions.size, + }; + } + store(input: StoreToolOutputInput): ToolOutputHandle { const previewChars = input.previewChars ?? 240; - const retainedContent = retainBoundedContent(input.content, this.maxHandleChars); const now = this.now(); + const lifecycleDenied = this.lifecycleDenyFilter.has(`chat:${input.chatSessionId}`) + || Boolean( + input.sessionId + && ( + this.closedTerminalSessions.has(input.sessionId) + || this.lifecycleDenyFilter.has(`closed-terminal:${input.sessionId}`) + ) + ); + if (lifecycleDenied) { + return { + id: nextHandleId(), + chatSessionId: input.chatSessionId, + capabilityId: input.capabilityId, + sessionId: input.sessionId, + totalChars: input.content.length, + storedChars: 0, + sourceTruncated: input.content.length > 0, + preview: "", + storedAt: now, + accessedAt: now, + evicted: true, + }; + } + const retainedContent = retainBoundedContent(input.content, this.maxHandleChars); const handle: ToolOutputHandle = { id: nextHandleId(), chatSessionId: input.chatSessionId, @@ -176,11 +262,6 @@ export class ToolOutputStore { accessedAt: now, fullContent: retainedContent, }; - if (input.sessionId && this.closedTerminalSessions.has(input.sessionId)) { - handle.evicted = true; - handle.fullContent = undefined; - return handle; - } const sessionMap = this.bySession.get(input.chatSessionId) ?? new Map(); sessionMap.set(handle.id, handle); this.bySession.set(input.chatSessionId, sessionMap); @@ -252,31 +333,54 @@ export class ToolOutputStore { } prune(chatSessionId: string): void { + this.lifecycleDenyFilter.add(`chat:${chatSessionId}`); + this.failedSessionDeletions.delete(chatSessionId); this.sessionGenerations.set(chatSessionId, (this.sessionGenerations.get(chatSessionId) ?? 0) + 1); - for (const key of this.deletedTerminalSessions) { - if (key.startsWith(`${chatSessionId}:`)) this.deletedTerminalSessions.delete(key); + for (const key of this.deletedTerminalSessions.keys()) { + if (key.startsWith(`${chatSessionId}:`)) { + this.deletedTerminalSessions.delete(key); + this.terminalMutationGenerations.delete(key); + this.failedTerminalDeletions.delete(key); + } } const sessionMap = this.bySession.get(chatSessionId); if (sessionMap) { for (const handle of sessionMap.values()) this.evictHandle(handle); } this.bySession.delete(chatSessionId); + let deletionSucceeded = false; const deletion = this.persistence?.deleteSession?.(chatSessionId) - .catch(() => {}) - .then(() => {}); + .then( + () => { deletionSucceeded = true; }, + () => {}, + ); if (deletion) { this.sessionDeletionPromises.set(chatSessionId, deletion); void deletion.finally(() => { - if (this.sessionDeletionPromises.get(chatSessionId) === deletion) { - this.sessionDeletionPromises.delete(chatSessionId); + if (this.sessionDeletionPromises.get(chatSessionId) !== deletion) { + return; + } + this.sessionDeletionPromises.delete(chatSessionId); + if (deletionSucceeded) { + this.cleanupSessionGeneration(chatSessionId); + } else { + this.failedSessionDeletions.add(chatSessionId); + this.enforceSessionDeletionMetadataLimit(); } }); + } else if (this.persistence?.restore && !this.persistence.deleteSession) { + this.failedSessionDeletions.add(chatSessionId); + this.enforceSessionDeletionMetadataLimit(); } + this.cleanupSessionGeneration(chatSessionId); } pruneTerminalSession(chatSessionId: string, terminalSessionId: string): void { const terminalKey = `${chatSessionId}:${terminalSessionId}`; - this.deletedTerminalSessions.add(terminalKey); + this.lifecycleDenyFilter.add(`terminal-key:${terminalKey}`); + this.deletedTerminalSessions.delete(terminalKey); + this.deletedTerminalSessions.set(terminalKey, chatSessionId); + this.failedTerminalDeletions.delete(terminalKey); this.terminalMutationGenerations.set( terminalKey, (this.terminalMutationGenerations.get(terminalKey) ?? 0) + 1, @@ -290,11 +394,40 @@ export class ToolOutputStore { } if (sessionMap.size === 0) this.bySession.delete(chatSessionId); } - void this.persistence?.deleteTerminalSession?.(chatSessionId, terminalSessionId).catch(() => {}); + let deletionSucceeded = false; + const deletion = this.persistence?.deleteTerminalSession?.(chatSessionId, terminalSessionId) + .then( + () => { deletionSucceeded = true; }, + () => {}, + ); + if (deletion) { + this.terminalDeletionPromises.set(terminalKey, deletion); + void deletion.finally(() => { + if (this.terminalDeletionPromises.get(terminalKey) !== deletion) { + return; + } + this.terminalDeletionPromises.delete(terminalKey); + if (deletionSucceeded) { + this.cleanupTerminalMutationMetadata(terminalKey, chatSessionId); + } else { + this.failedTerminalDeletions.add(terminalKey); + this.enforceTerminalMutationMetadataLimit(); + } + }); + } + this.cleanupTerminalMutationMetadata(terminalKey, chatSessionId); + this.enforceTerminalMutationMetadataLimit(); } pruneTerminalSessionEverywhere(terminalSessionId: string): void { + this.lifecycleDenyFilter.add(`closed-terminal:${terminalSessionId}`); + this.closedTerminalSessions.delete(terminalSessionId); this.closedTerminalSessions.add(terminalSessionId); + while (this.closedTerminalSessions.size > TOOL_OUTPUT_MAX_CLOSED_TERMINAL_SESSIONS) { + const oldestTerminalSessionId = this.closedTerminalSessions.values().next().value; + if (oldestTerminalSessionId === undefined) break; + this.closedTerminalSessions.delete(oldestTerminalSessionId); + } const chatSessionIds = [...this.bySession.keys()]; for (const chatSessionId of chatSessionIds) { this.pruneTerminalSession(chatSessionId, terminalSessionId); @@ -416,6 +549,9 @@ export class ToolOutputStore { terminalMutationGenerations, ).finally(() => { this.restorePromises.delete(key); + this.cleanupSessionGeneration(chatSessionId); + this.enforceSessionDeletionMetadataLimit(); + this.cleanupTerminalMutationMetadataForChat(chatSessionId); }); this.restorePromises.set(key, restorePromise); return restorePromise; @@ -433,7 +569,9 @@ export class ToolOutputStore { ? `${chatSessionId}:${restored.record.terminalSessionId}` : undefined; if ( - (this.sessionGenerations.get(chatSessionId) ?? 0) !== generation + this.failedSessionDeletions.has(chatSessionId) + || this.lifecycleDenyFilter.has(`chat:${chatSessionId}`) + || (this.sessionGenerations.get(chatSessionId) ?? 0) !== generation || ( restoredTerminalKey && (this.terminalMutationGenerations.get(restoredTerminalKey) ?? 0) @@ -441,11 +579,17 @@ export class ToolOutputStore { ) || ( restoredTerminalKey - && this.deletedTerminalSessions.has(restoredTerminalKey) + && ( + this.deletedTerminalSessions.has(restoredTerminalKey) + || this.lifecycleDenyFilter.has(`terminal-key:${restoredTerminalKey}`) + ) ) || ( restored.record.terminalSessionId - && this.closedTerminalSessions.has(restored.record.terminalSessionId) + && ( + this.closedTerminalSessions.has(restored.record.terminalSessionId) + || this.lifecycleDenyFilter.has(`closed-terminal:${restored.record.terminalSessionId}`) + ) ) ) { void this.persistence?.delete(restored.path).catch(() => {}); @@ -482,6 +626,76 @@ export class ToolOutputStore { if (sessionMap?.size === 0) this.bySession.delete(handle.chatSessionId); this.evictHandle(handle); } + + private cleanupSessionGeneration(chatSessionId: string): void { + if (this.sessionDeletionPromises.has(chatSessionId)) return; + if (this.failedSessionDeletions.has(chatSessionId)) return; + if (this.hasPendingRestoreForChat(chatSessionId)) return; + this.sessionGenerations.delete(chatSessionId); + } + + private enforceSessionDeletionMetadataLimit(): void { + while (this.failedSessionDeletions.size > TOOL_OUTPUT_MAX_FAILED_SESSION_DELETIONS) { + let removed = false; + for (const chatSessionId of this.failedSessionDeletions) { + if ( + this.sessionDeletionPromises.has(chatSessionId) + || this.hasPendingRestoreForChat(chatSessionId) + ) { + continue; + } + this.failedSessionDeletions.delete(chatSessionId); + this.sessionGenerations.delete(chatSessionId); + removed = true; + break; + } + if (!removed) break; + } + } + + private cleanupTerminalMutationMetadata(terminalKey: string, chatSessionId: string): void { + if (this.terminalDeletionPromises.has(terminalKey)) return; + if (this.failedTerminalDeletions.has(terminalKey)) return; + if (this.hasPendingRestoreForChat(chatSessionId)) return; + if (this.persistence?.restore && !this.persistence.deleteTerminalSession) return; + this.terminalMutationGenerations.delete(terminalKey); + this.deletedTerminalSessions.delete(terminalKey); + } + + private cleanupTerminalMutationMetadataForChat(chatSessionId: string): void { + const terminalPrefix = `${chatSessionId}:`; + for (const terminalKey of this.terminalMutationGenerations.keys()) { + if (terminalKey.startsWith(terminalPrefix)) { + this.cleanupTerminalMutationMetadata(terminalKey, chatSessionId); + } + } + this.enforceTerminalMutationMetadataLimit(); + } + + private hasPendingRestoreForChat(chatSessionId: string): boolean { + const restorePrefix = `${chatSessionId}:`; + return [...this.restorePromises.keys()].some(key => key.startsWith(restorePrefix)); + } + + private enforceTerminalMutationMetadataLimit(): void { + while (this.deletedTerminalSessions.size > TOOL_OUTPUT_MAX_CLOSED_TERMINAL_SESSIONS) { + let removed = false; + for (const [terminalKey, chatSessionId] of this.deletedTerminalSessions) { + if ( + this.terminalDeletionPromises.has(terminalKey) + || this.hasPendingRestoreForChat(chatSessionId) + ) { + continue; + } + this.deletedTerminalSessions.delete(terminalKey); + this.terminalMutationGenerations.delete(terminalKey); + this.failedTerminalDeletions.delete(terminalKey); + removed = true; + break; + } + if (!removed) break; + } + } } function removeRestartPersistenceWarning(value: string): string { diff --git a/infrastructure/ai/harness/traceStore.ts b/infrastructure/ai/harness/traceStore.ts index 0c9d85f57..1896f936c 100644 --- a/infrastructure/ai/harness/traceStore.ts +++ b/infrastructure/ai/harness/traceStore.ts @@ -1,6 +1,7 @@ import type { AgentEvent, CompactionTrace } from './types'; const DEFAULT_MAX_EVENTS = 2_000; +const DEFAULT_MAX_COMPACTIONS = 500; export interface TraceExport { sessionId: string; @@ -13,9 +14,14 @@ export class TraceStore { private readonly events = new Map(); private readonly compactions = new Map(); private readonly maxEvents: number; + private readonly maxCompactions: number; - constructor(maxEvents = DEFAULT_MAX_EVENTS) { - this.maxEvents = maxEvents; + constructor( + maxEvents = DEFAULT_MAX_EVENTS, + maxCompactions = Math.min(DEFAULT_MAX_COMPACTIONS, maxEvents), + ) { + this.maxEvents = Math.max(1, Math.floor(maxEvents)); + this.maxCompactions = Math.max(1, Math.floor(maxCompactions)); } append(event: AgentEvent): void { @@ -29,6 +35,9 @@ export class TraceStore { if (event.type === 'compaction') { const traces = this.compactions.get(event.sessionId) ?? []; traces.push(event.trace); + if (traces.length > this.maxCompactions) { + traces.splice(0, traces.length - this.maxCompactions); + } this.compactions.set(event.sessionId, traces); } } diff --git a/infrastructure/services/compressUploadService.ts b/infrastructure/services/compressUploadService.ts index bd9d4346e..b0be63f6b 100644 --- a/infrastructure/services/compressUploadService.ts +++ b/infrastructure/services/compressUploadService.ts @@ -28,18 +28,11 @@ export interface CompressUploadSupport { error?: string; } -export type CompressUploadProgressCallback = (phase: string, transferred: number, total: number) => void; -export type CompressUploadCompleteCallback = () => void; -export type CompressUploadErrorCallback = (error: string) => void; - /** * Start a compressed folder upload */ export async function startCompressedUpload( options: CompressUploadOptions, - onProgress?: CompressUploadProgressCallback, - onComplete?: CompressUploadCompleteCallback, - onError?: CompressUploadErrorCallback ): Promise<{ compressionId: string; success?: boolean; error?: string }> { const bridge = netcattyBridge.get(); if (!bridge?.startCompressedUpload) { @@ -47,7 +40,7 @@ export async function startCompressedUpload( } try { - return await bridge.startCompressedUpload(options, onProgress, onComplete, onError); + return await bridge.startCompressedUpload(options); } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); return { diff --git a/lib/searchMatcher.ts b/lib/searchMatcher.ts index 8405c4d00..662d3d229 100644 --- a/lib/searchMatcher.ts +++ b/lib/searchMatcher.ts @@ -5,6 +5,7 @@ const SEARCH_REMOVE_REGEX = /[\s\p{Pd}_/\\|.,,。;;::!!??()()[\]{} const SEARCH_QUERY_SEGMENT_SPLIT_REGEX = /\s+/u; const SEARCH_QUERY_PUNCT_REGEX = /[\p{Pd}_/\\|.,,。;;::!!??()()[\]{}<>《》、"'`~·]/u; const DASH_SEPARATOR_REGEX = /[\u2010-\u2015\u2212\uFE58\uFE63\uFF0D]/gu; +export const PINYIN_CACHE_MAX_ENTRIES = 16_384; const PINYIN_CACHE = new Map(); const IPV4_LIKE_REGEX = /^\d{1,3}(?:\.\d{1,3})+$/; const HOST_PHASE_SCORE_BONUS = { @@ -35,7 +36,11 @@ function compactText(input: string): string { function getPinyinVariants(sourceText: string): { full: string; initials: string } { const cacheKey = normalizeText(sourceText); const cached = PINYIN_CACHE.get(cacheKey); - if (cached) return cached; + if (cached) { + PINYIN_CACHE.delete(cacheKey); + PINYIN_CACHE.set(cacheKey, cached); + return cached; + } let full = ""; let initials = ""; @@ -57,10 +62,24 @@ function getPinyinVariants(sourceText: string): { full: string; initials: string } const next = { full, initials }; + PINYIN_CACHE.delete(cacheKey); PINYIN_CACHE.set(cacheKey, next); + while (PINYIN_CACHE.size > PINYIN_CACHE_MAX_ENTRIES) { + const oldestKey = PINYIN_CACHE.keys().next().value as string | undefined; + if (oldestKey === undefined) break; + PINYIN_CACHE.delete(oldestKey); + } return next; } +export function resetPinyinCacheForTests(): void { + PINYIN_CACHE.clear(); +} + +export function getPinyinCacheStatsForTests(): { size: number; keys: string[] } { + return { size: PINYIN_CACHE.size, keys: [...PINYIN_CACHE.keys()] }; +} + export function tokenizeSearchQuery(query: string): string[] { const normalized = normalizeText(query); if (!normalized) return []; diff --git a/lib/uploadCompressed.ts b/lib/uploadCompressed.ts index 162b83a57..1f0e7e2b3 100644 --- a/lib/uploadCompressed.ts +++ b/lib/uploadCompressed.ts @@ -144,61 +144,14 @@ export async function uploadFoldersCompressed( folderName, totalBytes, }, - (phase, transferred, total) => { - // Check for cancellation during progress updates - if (controller?.isCancelled()) { - return; - } - - if (callbacks?.onTaskProgress) { - // Map compression progress to actual file bytes - const progressPercent = total > 0 ? (transferred / total) * 100 : 0; - const mappedTransferred = Math.floor((progressPercent / 100) * totalBytes); - - callbacks.onTaskProgress(taskId, { - transferred: mappedTransferred, - total: totalBytes, - speed: 0, // Speed is handled by the compression service - percent: progressPercent, - }); - } - - // Update task name based on phase - if (callbacks?.onTaskNameUpdate) { - // Pass phase identifier for UI layer to handle i18n - // Format: "folderName|phase" where phase is: compressing, extracting, uploading, or compressed - const phaseKey = phase === 'compressing' ? 'compressing' - : phase === 'extracting' ? 'extracting' - : phase === 'uploading' ? 'uploading' - : 'compressed'; - callbacks.onTaskNameUpdate(taskId, `${folderName}|${phaseKey}`); - } - }, - () => { - // Remove compression ID from controller - controller?.removeActiveCompression(compressionId); - // Mark task as completed immediately - if (callbacks?.onTaskCompleted) { - callbacks.onTaskCompleted(taskId, totalBytes); - } - }, - (error) => { - // Remove compression ID from controller on error - controller?.removeActiveCompression(compressionId); - if (callbacks?.onTaskFailed) { - callbacks.onTaskFailed(taskId, error); - } - } ); + controller?.removeActiveCompression(compressionId); if (result.success) { results.push({ fileName: folderName, success: true }); } else if (result.error?.includes('cancelled') || controller?.isCancelled()) { // Handle cancellation results.push({ fileName: folderName, success: false, cancelled: true }); - if (callbacks?.onTaskCancelled) { - callbacks.onTaskCancelled(taskId); - } } else { results.push({ fileName: folderName, success: false, error: result.error }); } @@ -214,15 +167,8 @@ export async function uploadFoldersCompressed( // Check if this was a cancellation if (controller?.isCancelled() || errorMessage.includes('cancelled')) { results.push({ fileName: folderName, success: false, cancelled: true }); - if (callbacks?.onTaskCancelled && taskId) { - callbacks.onTaskCancelled(taskId); - } } else { results.push({ fileName: folderName, success: false, error: errorMessage }); - // Only call onTaskFailed if we have a valid taskId (task was created) and it's not a cancellation - if (callbacks?.onTaskFailed && taskId) { - callbacks.onTaskFailed(taskId, errorMessage); - } } } } diff --git a/lib/uploadController.test.ts b/lib/uploadController.test.ts new file mode 100644 index 000000000..a61d3312f --- /dev/null +++ b/lib/uploadController.test.ts @@ -0,0 +1,17 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +import { UploadController } from "./uploadController"; + +test("cancelling an upload aborts an active pathless-file staging job", async () => { + const calls: string[] = []; + const controller = new UploadController(); + controller.setBridge({ + mkdirSftp: async () => {}, + cancelStagedUploadFile: async (transferId) => { calls.push(`stage:${transferId}`); }, + cancelTransfer: async (transferId) => { calls.push(`transfer:${transferId}`); }, + }); + controller.addActiveTransfer("upload-1"); + await controller.cancel(); + assert.deepEqual(calls, ["stage:upload-1", "transfer:upload-1"]); +}); diff --git a/lib/uploadController.ts b/lib/uploadController.ts index 5c4413817..5e320a594 100644 --- a/lib/uploadController.ts +++ b/lib/uploadController.ts @@ -29,14 +29,12 @@ export class UploadController { const activeIds = Array.from(this.activeFileTransferIds); for (const transferId of activeIds) { try { - // Try cancelTransfer first (for stream transfers) + if (this.bridge?.cancelStagedUploadFile) { + await this.bridge.cancelStagedUploadFile(transferId); + } if (this.bridge?.cancelTransfer) { await this.bridge.cancelTransfer(transferId); } - // Also try cancelSftpUpload (for legacy uploads) - if (this.bridge?.cancelSftpUpload) { - await this.bridge.cancelSftpUpload(transferId); - } } catch { // Ignore cancel errors } @@ -48,9 +46,6 @@ export class UploadController { if (this.bridge?.cancelTransfer) { await this.bridge.cancelTransfer(this.currentTransferId); } - if (this.bridge?.cancelSftpUpload) { - await this.bridge.cancelSftpUpload(this.currentTransferId); - } } catch { // Ignore cancel errors } diff --git a/lib/uploadService.ts b/lib/uploadService.ts index 38bdb8dcb..f737b220d 100644 --- a/lib/uploadService.ts +++ b/lib/uploadService.ts @@ -28,7 +28,7 @@ export type { UploadResult, UploadTaskInfo, } from "./uploadService.types"; -import type { UploadBridge, UploadCallbacks, UploadConfig, UploadProgress, UploadResult } from "./uploadService.types"; +import type { UploadBridge, UploadCallbacks, UploadConfig, UploadResult } from "./uploadService.types"; // ============================================================================ // Helper Functions @@ -148,12 +148,12 @@ export async function uploadFromDataTransfer( config: UploadConfig, controller?: UploadController ): Promise { - const { targetPath, sftpId, isLocal, bridge, joinPath, callbacks, useCompressedUpload, resolveConflict } = config; + const { callbacks } = config; // Reset controller if provided if (controller) { controller.reset(); - controller.setBridge(bridge); + controller.setBridge(config.bridge); } // Create scanning placeholder @@ -174,55 +174,7 @@ export async function uploadFromDataTransfer( return []; } - // Check if this is a folder upload and compressed upload is enabled - if (useCompressedUpload && !resolveConflict && !isLocal && sftpId) { - const rootFolders = detectRootFolders(entries); - const folderEntries = Array.from(rootFolders.entries()).filter(([key]) => !key.startsWith("__file__")); - const standaloneFileEntries = Array.from(rootFolders.entries()).filter(([key]) => key.startsWith("__file__")); - - if (folderEntries.length > 0) { - try { - const compressedResults = await uploadFoldersCompressed(folderEntries, targetPath, sftpId, callbacks, controller); - - // Check if any folders failed due to lack of compression support - const failedFolders = compressedResults.filter(result => - !result.success && result.error === "Compressed upload not supported - fallback needed" - ); - const successfulFolders = compressedResults.filter(result => - result.success || result.error !== "Compressed upload not supported - fallback needed" - ); - - let fallbackResults: UploadResult[] = []; - if (failedFolders.length > 0) { - // Get entries only for failed folders, not already successful ones - const failedFolderNames = new Set(failedFolders.map(f => f.fileName)); - const failedFolderEntries = entries.filter(entry => { - const topFolder = entry.relativePath.split('/')[0]; - return failedFolderNames.has(topFolder); - }); - - if (failedFolderEntries.length > 0) { - fallbackResults = await uploadEntries(failedFolderEntries, targetPath, sftpId, isLocal, bridge, joinPath, callbacks, controller, resolveConflict, config.targetHostId); - } - } - - // Upload standalone files using regular upload if any exist - let standaloneResults: UploadResult[] = []; - if (standaloneFileEntries.length > 0) { - const standaloneEntries = standaloneFileEntries.flatMap(([, entries]) => entries); - standaloneResults = await uploadEntries(standaloneEntries, targetPath, sftpId, isLocal, bridge, joinPath, callbacks, controller, resolveConflict, config.targetHostId); - } - - // Combine results: successful compressed + fallback results + standalone files - return [...successfulFolders, ...fallbackResults, ...standaloneResults]; - } catch { - // Fall back to regular upload - return uploadEntries(entries, targetPath, sftpId, isLocal, bridge, joinPath, callbacks, controller, resolveConflict, config.targetHostId); - } - } - } - - return uploadEntries(entries, targetPath, sftpId, isLocal, bridge, joinPath, callbacks, controller, resolveConflict, config.targetHostId); + return uploadEntriesWithOptionalCompression(entries, config, controller); } /** @@ -233,11 +185,9 @@ export async function uploadFromFileList( config: UploadConfig, controller?: UploadController ): Promise { - const { targetPath, sftpId, isLocal, bridge, joinPath, callbacks, useCompressedUpload, resolveConflict } = config; - if (controller) { controller.reset(); - controller.setBridge(bridge); + controller.setBridge(config.bridge); } // Convert FileList to DropEntry array @@ -261,55 +211,195 @@ export async function uploadFromFileList( return []; } - // Check if this is a folder upload and compressed upload is enabled - if (useCompressedUpload && !resolveConflict && !isLocal && sftpId) { - const rootFolders = detectRootFolders(entries); - const folderEntries = Array.from(rootFolders.entries()).filter(([key]) => !key.startsWith("__file__")); - const standaloneFileEntries = Array.from(rootFolders.entries()).filter(([key]) => key.startsWith("__file__")); + return uploadEntriesWithOptionalCompression(entries, config, controller); +} - if (folderEntries.length > 0) { - try { - const compressedResults = await uploadFoldersCompressed(folderEntries, targetPath, sftpId, callbacks, controller); +type UploadConflictAction = Awaited>>; - // Check if any folders failed due to lack of compression support - const failedFolders = compressedResults.filter(result => - !result.success && result.error === "Compressed upload not supported - fallback needed" - ); - const successfulFolders = compressedResults.filter(result => - result.success || result.error !== "Compressed upload not supported - fallback needed" - ); +const compressionFallbackError = "Compressed upload not supported - fallback needed"; - let fallbackResults: UploadResult[] = []; - if (failedFolders.length > 0) { - // Get entries only for failed folders, not already successful ones - const failedFolderNames = new Set(failedFolders.map(f => f.fileName)); - const failedFolderEntries = entries.filter(entry => { - const topFolder = entry.relativePath.split('/')[0]; - return failedFolderNames.has(topFolder); - }); +const uploadConflictGroupKey = (fileName: string, isDirectory: boolean) => ( + `${isDirectory ? "directory" : "file"}:${fileName}` +); - if (failedFolderEntries.length > 0) { - fallbackResults = await uploadEntries(failedFolderEntries, targetPath, sftpId, isLocal, bridge, joinPath, callbacks, controller, resolveConflict, config.targetHostId); - } - } +/** + * Resolve root conflicts once before selecting compressed vs regular transfer. + * New folders and directory merges can use the atomic compressed path. Replace, + * duplicate, and invalid merge cases stay on the regular path so their existing + * semantics are preserved exactly and the user is never prompted twice. + */ +async function uploadEntriesWithOptionalCompression( + entries: DropEntry[], + config: UploadConfig, + controller?: UploadController, +): Promise { + const { + targetPath, + sftpId, + isLocal, + bridge, + joinPath, + callbacks, + useCompressedUpload, + resolveConflict, + } = config; - // Upload standalone files using regular upload if any exist - let standaloneResults: UploadResult[] = []; - if (standaloneFileEntries.length > 0) { - const standaloneEntries = standaloneFileEntries.flatMap(([, entries]) => entries); - standaloneResults = await uploadEntries(standaloneEntries, targetPath, sftpId, isLocal, bridge, joinPath, callbacks, controller, resolveConflict, config.targetHostId); - } + if (!useCompressedUpload || isLocal || !sftpId) { + return uploadEntries( + entries, + targetPath, + sftpId, + isLocal, + bridge, + joinPath, + callbacks, + controller, + resolveConflict, + config.targetHostId, + ); + } - // Combine results: successful compressed + fallback results + standalone files - return [...successfulFolders, ...fallbackResults, ...standaloneResults]; - } catch { - // Fall back to regular upload - return uploadEntries(entries, targetPath, sftpId, isLocal, bridge, joinPath, callbacks, controller, resolveConflict, config.targetHostId); - } + const rootGroups = Array.from(detectRootFolders(entries).entries()); + if (!rootGroups.some(([key]) => !key.startsWith("__file__"))) { + return uploadEntries( + entries, + targetPath, + sftpId, + isLocal, + bridge, + joinPath, + callbacks, + controller, + resolveConflict, + config.targetHostId, + ); + } + + const statTarget = async (path: string) => { + try { + return await bridge.statSftp?.(sftpId, path) ?? null; + } catch { + return null; + } + }; + const groupInfos = await Promise.all(rootGroups.map(async ([key, groupEntries]) => { + const isStandaloneFile = key.startsWith("__file__"); + const rootName = isStandaloneFile ? key.slice("__file__".length) : key; + const isDirectory = !isStandaloneFile; + const rootTargetPath = joinPath(targetPath, rootName); + return { + groupEntries, + rootName, + isDirectory, + rootTargetPath, + existing: resolveConflict ? await statTarget(rootTargetPath) : null, + }; + })); + + const conflictCounts = new Map(); + for (const info of groupInfos) { + if (!info.existing) continue; + const key = getSftpConflictTypeKey(info.isDirectory, info.existing.type); + conflictCounts.set(key, (conflictCounts.get(key) ?? 0) + 1); + } + + const compressedGroups: Array<[string, DropEntry[]]> = []; + const regularEntries: DropEntry[] = []; + const resolvedResults: UploadResult[] = []; + const selectedActions = new Map(); + + for (const info of groupInfos) { + if (controller?.isCancelled()) break; + if (!info.existing || !resolveConflict) { + if (info.isDirectory) compressedGroups.push([info.rootName, info.groupEntries]); + else regularEntries.push(...info.groupEntries); + continue; + } + + const conflictKey = getSftpConflictTypeKey(info.isDirectory, info.existing.type); + const action = await resolveConflict({ + fileName: info.rootName, + targetPath: info.rootTargetPath, + isDirectory: info.isDirectory, + existingType: info.existing.type, + existingSize: info.existing.size, + newSize: info.groupEntries.reduce((sum, entry) => sum + getDropEntrySize(entry), 0), + existingModified: info.existing.lastModified, + newModified: Date.now(), + applyToAllCount: conflictCounts.get(conflictKey) ?? 1, + }); + + if (action === "stop") { + await controller?.cancel(); + return [{ fileName: info.rootName, success: false, cancelled: true }]; + } + if (action === "skip") { + resolvedResults.push({ fileName: info.rootName, success: false, cancelled: true }); + continue; + } + + selectedActions.set(uploadConflictGroupKey(info.rootName, info.isDirectory), action); + if (info.isDirectory && action === "merge" && info.existing.type === "directory") { + compressedGroups.push([info.rootName, info.groupEntries]); + } else { + regularEntries.push(...info.groupEntries); } } - return uploadEntries(entries, targetPath, sftpId, isLocal, bridge, joinPath, callbacks, controller, resolveConflict, config.targetHostId); + let compressedResults: UploadResult[] = []; + if (compressedGroups.length > 0 && !controller?.isCancelled()) { + try { + compressedResults = await uploadFoldersCompressed( + compressedGroups, + targetPath, + sftpId, + callbacks, + controller, + ); + } catch { + compressedResults = compressedGroups.map(([fileName]) => ({ + fileName, + success: false, + error: compressionFallbackError, + })); + } + } + + const fallbackFolderNames = new Set( + compressedResults + .filter((result) => !result.success && result.error === compressionFallbackError) + .map((result) => result.fileName), + ); + for (const [fileName, groupEntries] of compressedGroups) { + if (fallbackFolderNames.has(fileName)) regularEntries.push(...groupEntries); + } + + const terminalCompressedResults = compressedResults.filter((result) => ( + result.success || result.error !== compressionFallbackError + )); + if (regularEntries.length === 0) { + return [...resolvedResults, ...terminalCompressedResults]; + } + + const replayResolvedConflict: UploadConfig["resolveConflict"] = resolveConflict + ? async (conflict) => ( + selectedActions.get(uploadConflictGroupKey(conflict.fileName, conflict.isDirectory)) + ?? resolveConflict(conflict) + ) + : undefined; + const regularResults = await uploadEntries( + regularEntries, + targetPath, + sftpId, + isLocal, + bridge, + joinPath, + callbacks, + controller, + replayResolvedConflict, + config.targetHostId, + ); + return [...resolvedResults, ...terminalCompressedResults, ...regularResults]; } /** @@ -644,60 +734,35 @@ async function uploadEntries( standaloneTransferId: string, fileTotalBytes: number, ): Promise<{ cancelled?: boolean; error?: string }> => { - const localFilePath = getDropEntryLocalPath(entry); + let localFilePath = getDropEntryLocalPath(entry); + let ownedTempPath: string | undefined; - // Progress callback factory for both stream and memory paths - const makeOnProgress = () => { - let pendingProgressUpdate: { - transferred: number; - total: number; - speed: number; - phase?: UploadProgress['phase']; - checkpointBytes?: number; - sourceFingerprint?: string; - resumable?: boolean; - pauseUnavailableReason?: string; - } | null = null; - let rafScheduled = false; - - return (transferred: number, total: number, speed: number, capability?: { - phase?: UploadProgress['phase']; - checkpointBytes?: number; - sourceFingerprint?: string; - resumable?: boolean; - pauseUnavailableReason?: string; - }) => { - if (controller?.isCancelled()) return; - pendingProgressUpdate = { transferred, total, speed, ...capability }; - - if (!rafScheduled) { - rafScheduled = true; - requestAnimationFrame(() => { - rafScheduled = false; - const update = pendingProgressUpdate; - pendingProgressUpdate = null; - if (!update || controller?.isCancelled() || !callbacks?.onTaskProgress) return; - - if (standaloneTransferId) { - callbacks.onTaskProgress(standaloneTransferId, { - transferred: update.transferred, - total: update.total, - speed: update.speed, - phase: update.phase, - percent: update.total > 0 ? (update.transferred / update.total) * 100 : 0, - // Forward contiguous offset + fingerprint for soft-drain pause safety. - checkpointBytes: update.checkpointBytes, - sourceFingerprint: update.sourceFingerprint, - resumable: update.resumable, - pauseUnavailableReason: update.pauseUnavailableReason, - }); - } - }); + if ( + !localFilePath + && !isLocal + && entry.file + && bridge.stageUploadFile + && bridge.deleteTempFile + ) { + controller?.addActiveTransfer(standaloneTransferId); + try { + ownedTempPath = await bridge.stageUploadFile(entry.file, standaloneTransferId); + } catch (error) { + controller?.removeActiveTransfer(standaloneTransferId); + if (controller?.isCancelled() || /cancel/i.test(error instanceof Error ? error.message : String(error))) { + return { cancelled: true }; } - }; - }; + throw error; + } + localFilePath = ownedTempPath; + if (controller?.isCancelled()) { + await bridge.deleteTempFile(ownedTempPath).catch(() => {}); + return { cancelled: true }; + } + } - if (localFilePath && bridge.startStreamTransfer && (!isLocal ? !!sftpId : true)) { + try { + if (localFilePath && bridge.startStreamTransfer && (!isLocal ? !!sftpId : true)) { const fileTransferId = standaloneTransferId; controller?.addActiveTransfer(fileTransferId); @@ -725,54 +790,28 @@ async function uploadEntries( if (streamResult?.error) { return { error: streamResult.error }; } - } else { + } else { if (!entry.file) { return { error: "No local file data available" }; } - const arrayBuffer = await entry.file.arrayBuffer(); - if (isLocal) { + const arrayBuffer = await entry.file.arrayBuffer(); if (!bridge.writeLocalFile) throw new Error("writeLocalFile not available"); await bridge.writeLocalFile(entryTargetPath, arrayBuffer); } else if (sftpId) { - if (bridge.writeSftpBinaryWithProgress) { - const onProgress = makeOnProgress(); - const fileTransferId = standaloneTransferId; - controller?.addActiveTransfer(fileTransferId); - - let result; - try { - result = await bridge.writeSftpBinaryWithProgress( - sftpId, - entryTargetPath, - arrayBuffer, - fileTransferId, - onProgress, - () => {}, - () => {} - ); - } finally { - controller?.removeActiveTransfer(fileTransferId); - } - - if (result?.cancelled) { - return { cancelled: true }; - } - if (!result || result.success === false) { - if (bridge.writeSftpBinary) { - await bridge.writeSftpBinary(sftpId, entryTargetPath, arrayBuffer); - } else { - return { error: "Upload failed and no fallback method available" }; - } - } - } else if (bridge.writeSftpBinary) { - await bridge.writeSftpBinary(sftpId, entryTargetPath, arrayBuffer); - } else { - return { error: "No SFTP write method available" }; - } + // Electron-backed files must expose a real local path so the unified + // transfer runtime can stream them. Falling back to File.arrayBuffer() + // duplicates the entire payload in renderer + IPC memory. + return { error: "A local file path is required for streaming SFTP upload" }; } + } + return {}; + } finally { + if (ownedTempPath) { + controller?.removeActiveTransfer(standaloneTransferId); + await bridge.deleteTempFile?.(ownedTempPath).catch(() => {}); + } } - return {}; }; // Filter to only file entries (directories are pre-created above) @@ -837,6 +876,19 @@ async function uploadEntries( settle(taskId); }; + const settleFileTask = ( + taskId: string, + lifecycleManaged: boolean, + settle: (taskId: string) => void, + ) => { + if (!taskId) return; + if (lifecycleManaged) { + pendingTaskIds.delete(taskId); + return; + } + settleTask(taskId, settle); + }; + // Keep external multi-file uploads conservative: each file may open an // isolated fastPut channel with its own in-flight WRITE fanout. const UPLOAD_CONCURRENCY = 2; @@ -855,6 +907,10 @@ async function uploadEntries( const bundledChildTaskId = bundleTaskId ? createBundledChildTask(entry, bundleTaskId) : ""; const standaloneTransferId = standaloneTaskIds.get(entry.relativePath) || ""; const fileTotalBytes = getDropEntrySize(entry); + const localFilePath = getDropEntryLocalPath(entry); + const lifecycleManaged = bridge.managesTransferLifecycle === true && Boolean( + localFilePath && bridge.startStreamTransfer, + ); try { const uploadResult = await uploadSingleFile( @@ -866,9 +922,9 @@ async function uploadEntries( if (uploadResult.cancelled) { wasCancelled = true; - settleTask(bundledChildTaskId, (taskId) => callbacks?.onTaskCancelled?.(taskId)); + settleFileTask(bundledChildTaskId, lifecycleManaged, (taskId) => callbacks?.onTaskCancelled?.(taskId)); settleTask(bundleTaskId ?? "", (taskId) => callbacks?.onTaskCancelled?.(taskId)); - settleTask(!bundleTaskId ? standaloneTransferId : "", (taskId) => callbacks?.onTaskCancelled?.(taskId)); + settleFileTask(!bundleTaskId ? standaloneTransferId : "", lifecycleManaged, (taskId) => callbacks?.onTaskCancelled?.(taskId)); break; } @@ -882,7 +938,7 @@ async function uploadEntries( if (bundleTaskId) { const progress = bundleProgress.get(bundleTaskId); if (bundledChildTaskId) { - settleTask(bundledChildTaskId, (taskId) => callbacks?.onTaskCompleted?.(taskId, fileTotalBytes)); + settleFileTask(bundledChildTaskId, lifecycleManaged, (taskId) => callbacks?.onTaskCompleted?.(taskId, fileTotalBytes)); } if (progress) { progress.completedCount++; @@ -907,14 +963,14 @@ async function uploadEntries( } } } else if (standaloneTransferId) { - settleTask(standaloneTransferId, (taskId) => callbacks?.onTaskCompleted?.(taskId, fileTotalBytes)); + settleFileTask(standaloneTransferId, lifecycleManaged, (taskId) => callbacks?.onTaskCompleted?.(taskId, fileTotalBytes)); } } catch (error) { if (controller?.isCancelled()) { wasCancelled = true; - settleTask(bundledChildTaskId, (taskId) => callbacks?.onTaskCancelled?.(taskId)); + settleFileTask(bundledChildTaskId, lifecycleManaged, (taskId) => callbacks?.onTaskCancelled?.(taskId)); settleTask(bundleTaskId ?? "", (taskId) => callbacks?.onTaskCancelled?.(taskId)); - settleTask(!bundleTaskId ? standaloneTransferId : "", (taskId) => callbacks?.onTaskCancelled?.(taskId)); + settleFileTask(!bundleTaskId ? standaloneTransferId : "", lifecycleManaged, (taskId) => callbacks?.onTaskCancelled?.(taskId)); break; } @@ -928,8 +984,8 @@ async function uploadEntries( } } - settleTask(bundledChildTaskId, (taskId) => callbacks?.onTaskFailed?.(taskId, errorMessage)); - settleTask(!bundleTaskId ? standaloneTransferId : "", (taskId) => callbacks?.onTaskFailed?.(taskId, errorMessage)); + settleFileTask(bundledChildTaskId, lifecycleManaged, (taskId) => callbacks?.onTaskFailed?.(taskId, errorMessage)); + settleFileTask(!bundleTaskId ? standaloneTransferId : "", lifecycleManaged, (taskId) => callbacks?.onTaskFailed?.(taskId, errorMessage)); } } }; @@ -980,62 +1036,18 @@ export async function uploadEntriesDirect( config: UploadConfig, controller?: UploadController ): Promise { - const { targetPath, sftpId, isLocal, bridge, joinPath, callbacks, useCompressedUpload, resolveConflict } = config; - if (controller?.isCancelled()) { return [{ fileName: "", success: false, cancelled: true }]; } if (controller) { controller.reset(); - controller.setBridge(bridge); + controller.setBridge(config.bridge); } if (entries.length === 0) { return []; } - // Support compressed folder uploads (same logic as uploadFromDataTransfer) - if (useCompressedUpload && !resolveConflict && !isLocal && sftpId) { - const rootFolders = detectRootFolders(entries); - const folderEntries = Array.from(rootFolders.entries()).filter(([key]) => !key.startsWith("__file__")); - const standaloneFileEntries = Array.from(rootFolders.entries()).filter(([key]) => key.startsWith("__file__")); - - if (folderEntries.length > 0) { - try { - const compressedResults = await uploadFoldersCompressed(folderEntries, targetPath, sftpId, callbacks, controller); - - const failedFolders = compressedResults.filter(result => - !result.success && result.error === "Compressed upload not supported - fallback needed" - ); - const successfulFolders = compressedResults.filter(result => - result.success || result.error !== "Compressed upload not supported - fallback needed" - ); - - let fallbackResults: UploadResult[] = []; - if (failedFolders.length > 0) { - const failedFolderNames = new Set(failedFolders.map(f => f.fileName)); - const failedFolderEntries = entries.filter(entry => { - const topFolder = entry.relativePath.split('/')[0]; - return failedFolderNames.has(topFolder); - }); - if (failedFolderEntries.length > 0) { - fallbackResults = await uploadEntries(failedFolderEntries, targetPath, sftpId, isLocal, bridge, joinPath, callbacks, controller, resolveConflict, config.targetHostId); - } - } - - let standaloneResults: UploadResult[] = []; - if (standaloneFileEntries.length > 0) { - const standaloneEntries = standaloneFileEntries.flatMap(([, e]) => e); - standaloneResults = await uploadEntries(standaloneEntries, targetPath, sftpId, isLocal, bridge, joinPath, callbacks, controller, resolveConflict, config.targetHostId); - } - - return [...successfulFolders, ...fallbackResults, ...standaloneResults]; - } catch { - return uploadEntries(entries, targetPath, sftpId, isLocal, bridge, joinPath, callbacks, controller, resolveConflict, config.targetHostId); - } - } - } - - return uploadEntries(entries, targetPath, sftpId, isLocal, bridge, joinPath, callbacks, controller, resolveConflict, config.targetHostId); + return uploadEntriesWithOptionalCompression(entries, config, controller); } diff --git a/lib/uploadService.types.ts b/lib/uploadService.types.ts index 2d82ce0de..d150dafbe 100644 --- a/lib/uploadService.types.ts +++ b/lib/uploadService.types.ts @@ -57,27 +57,18 @@ export interface UploadCallbacks { } export interface UploadBridge { + /** Main-process transfer events own file progress and terminal lifecycle. */ + managesTransferLifecycle?: boolean; writeLocalFile?: (path: string, data: ArrayBuffer) => Promise; mkdirLocal?: (path: string) => Promise; statLocal?: (path: string) => Promise<{ type: 'file' | 'directory' | 'symlink'; size: number; lastModified: number } | null>; deleteLocalFile?: (path: string) => Promise; + stageUploadFile?: (file: File, taskId: string) => Promise; + cancelStagedUploadFile?: (taskId: string) => Promise; + deleteTempFile?: (path: string) => Promise; mkdirSftp: (sftpId: string, path: string) => Promise; statSftp?: (sftpId: string, path: string) => Promise<{ type: 'file' | 'directory' | 'symlink'; size: number; lastModified: number } | null>; deleteSftp?: (sftpId: string, path: string) => Promise; - writeSftpBinary?: (sftpId: string, path: string, data: ArrayBuffer) => Promise; - writeSftpBinaryWithProgress?: ( - sftpId: string, - path: string, - data: ArrayBuffer, - taskId: string, - onProgress: (transferred: number, total: number, speed: number, capability?: { - resumable?: boolean; - pauseUnavailableReason?: string; - }) => void, - onComplete?: () => void, - onError?: (error: string) => void - ) => Promise<{ success: boolean; cancelled?: boolean } | undefined>; - cancelSftpUpload?: (taskId: string) => Promise; /** Stream transfer using local file path (avoids loading file into memory) */ startStreamTransfer?: ( options: { diff --git a/types/global/netcatty-bridge-files.d.ts b/types/global/netcatty-bridge-files.d.ts index 6558330f6..23608fe29 100644 --- a/types/global/netcatty-bridge-files.d.ts +++ b/types/global/netcatty-bridge-files.d.ts @@ -6,7 +6,6 @@ declare global { selectApplication?(): Promise<{ path: string; name: string } | null>; openWithApplication?(filePath: string, appPath: string): Promise; openWithSystemDefault?(filePath: string): Promise<{ success: boolean; error?: string }>; - downloadSftpToTemp?(sftpId: string, remotePath: string, fileName: string, encoding?: SftpFilenameEncoding): Promise; downloadSftpToTempWithProgress?( sftpId: string, remotePath: string, @@ -25,11 +24,15 @@ declare global { stopFileWatch?(watchId: string, cleanupTempFile?: boolean): Promise<{ success: boolean }>; listFileWatches?(): Promise>; 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; 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; // Temp file cleanup deleteTempFile?(filePath: string): Promise<{ success: boolean }>; + stageUploadFile?(file: File, transferId: string): Promise; + cancelStagedUploadFile?(transferId: string): Promise<{ success: boolean }>; // Crash Logs getCrashLogs?(): Promise>; diff --git a/types/global/netcatty-bridge-script.d.ts b/types/global/netcatty-bridge-script.d.ts index 8f75153ca..53d4b3ec9 100644 --- a/types/global/netcatty-bridge-script.d.ts +++ b/types/global/netcatty-bridge-script.d.ts @@ -149,7 +149,14 @@ declare global { scriptScreenSnapshotResponse(requestId: string, snapshot: ScriptScreenSnapshot): Promise<{ ok: boolean }>; scriptRecordingStart(sessionId: string): Promise<{ ok: boolean }>; scriptRecordingStop(sessionId: string): Promise<{ steps: ScriptRecordingStep[]; code: string }>; - scriptRecordingAppendStep(sessionId: string, step: ScriptRecordingStep): Promise<{ ok: boolean }>; + scriptRecordingAppendStep(sessionId: string, step: ScriptRecordingStep): Promise<{ + ok: boolean; + stopped?: boolean; + reason?: 'limit'; + error?: string; + steps?: ScriptRecordingStep[]; + code?: string; + }>; onScriptRunsUpdated(cb: (payload: { runs: ScriptRun[] }) => void): () => void; onScriptDialogRequest(cb: (payload: ScriptDialogRequest) => void): () => void; onScriptScreenSnapshotRequest(cb: (payload: { requestId: string; sessionId: string }) => void): () => void; diff --git a/types/global/netcatty-bridge-sftp.d.ts b/types/global/netcatty-bridge-sftp.d.ts index f163c73c2..f18903352 100644 --- a/types/global/netcatty-bridge-sftp.d.ts +++ b/types/global/netcatty-bridge-sftp.d.ts @@ -4,8 +4,9 @@ declare global { interface NetcattyBridge { // SFTP operations openSftp(options: NetcattySSHOptions): Promise; - openSftpForSession?(sessionId: string): Promise; + openSftpForSession?(sessionId: string, expectedEndpoint?: NetcattySSHOptions): Promise; listSftp(sftpId: string, path: string, encoding?: SftpFilenameEncoding): Promise; + realpathSftp?(sftpId: string, path: string, encoding?: SftpFilenameEncoding): Promise; readSftp(sftpId: string, path: string, encoding?: SftpFilenameEncoding): Promise; readSftpBinary?(sftpId: string, path: string, encoding?: SftpFilenameEncoding): Promise; writeSftp(sftpId: string, path: string, content: string, encoding?: SftpFilenameEncoding): Promise; @@ -18,24 +19,7 @@ declare global { chmodSftp?(sftpId: string, path: string, mode: string, encoding?: SftpFilenameEncoding): Promise; getSftpHomeDir?(sftpId: string, encoding?: SftpFilenameEncoding): Promise<{ success: boolean; homeDir?: string; error?: string }>; - // Write binary with real-time progress callback - writeSftpBinaryWithProgress?( - sftpId: string, - path: string, - content: ArrayBuffer, - transferId: string, - encoding?: SftpFilenameEncoding, - onProgress?: (transferred: number, total: number, speed: number) => void, - onComplete?: () => void, - onError?: (error: string) => void - ): Promise<{ success: boolean; transferId: string; cancelled?: boolean }>; - - // Cancel an in-progress SFTP upload - cancelSftpUpload?(transferId: string): Promise<{ success: boolean }>; - // Transfer with progress - uploadFile?(sftpId: string, localPath: string, remotePath: string, transferId: string): Promise; - downloadFile?(sftpId: string, remotePath: string, localPath: string, transferId: string): Promise; cancelTransfer?(transferId: string): Promise; /** Clear a pre-start cancel latch so intentional same-id resume/retry can run. */ clearPendingTransferCancel?(transferId: string): Promise<{ success: boolean } | void>; @@ -50,10 +34,7 @@ declare global { sftpId: string; folderName: string; totalBytes: number; - }, - onProgress?: (phase: string, transferred: number, total: number) => void, - onComplete?: () => void, - onError?: (error: string) => void + } ): Promise<{ compressionId: string; success?: boolean; error?: string }>; cancelCompressedUpload?(compressionId: string): Promise<{ success: boolean }>; pauseCompressedUpload?(compressionId: string): Promise<{ success: boolean; deferred?: boolean; lifecycleEpoch?: number; reason?: string }>;