AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
fix(pieces): FTP/SFTP New File trigger now reports a failed login instead of silently finding no files (#16467)
Bartosz Majewskif42c0ed6b624
diff --git a/brain/knowledge/flows-execution/triggers.md b/brain/knowledge/flows-execution/triggers.mdindex bdea9271972..0cd2b0efaa8 100644--- a/brain/knowledge/flows-execution/triggers.md+++ b/brain/knowledge/flows-execution/triggers.md@@ -27,7 +27,9 @@ Triggers define how and when a flow starts. The module handles registration, eve- The **simulate flag** lets a production source and a test source coexist independently.- **Renewal jobs** re-register expiring webhook pieces via the ON_RENEW hook.- **`*/X` cron is not "every X minutes"** — it means "minutes divisible by X", so it double-fires at :00 and :X for X > 30 and gaps unevenly when X doesn't divide 60. Use `INTERVAL`/`intervalMs` for a rolling interval; reserve cron for wall-clock schedules. This bit the default poll schedule until GIT-1632.+- **A `basic-ftp` login that fails leaves the control socket open.** `access()` connects first and *then* logs in, so a refused login rejects with the socket still established, and the caller's client variable was never assigned, so no `finally` closes it. Measured against vsftpd on one refused login: the Node process stayed alive 304s waiting on that handle, versus 3s once `getClient` calls `close()` before rethrowing. Any helper that builds an FTP client must close it on a failed `access()`.- **Trigger health** (`triggerRunStats`): Redis key `trigger_run:{platformId}:{pieceName}:{date}:{status}`, 14-day retention, shown in Platform Admin (Cloud).+- **A polling `items` that catches its own connect or auth error and returns `[]` hides the outage.** The worker counts the poll COMPLETED, logs no warning and the flow stays enabled, so a rotated password looks like an empty folder. Let it throw: `pollingHelper.poll` fails before it writes `lastPoll`, the poll counts FAILED with a worker warn, and the next good poll catches up on everything missed. SFTP New File was fixed this way (ENG-796); dozens of community triggers still swallow errors the same way.### EditionsAll four strategies available in CE/EE/Cloud. Cloud additionally surfaces trigger health stats in Platform Admin.diff --git a/bun.lock b/bun.lockindex 5701bafeead..f35b4c2aa3f 100644--- a/bun.lock+++ b/bun.lock@@ -10766,7 +10766,7 @@},"packages/pieces/core/sftp": {"name": "@activepieces/piece-sftp",- "version": "0.5.10",+ "version": "0.5.11","dependencies": {"@activepieces/core-piece-types": "workspace:*","@activepieces/core-utils": "workspace:*",@@ -10782,6 +10782,7 @@"@types/ssh2": "1.15.5","@types/ssh2-sftp-client": "9.0.6","tslib": "2.6.2",+ "vitest": "3.2.6",},},"packages/pieces/core/smtp": {diff --git a/packages/pieces/core/sftp/package.json b/packages/pieces/core/sftp/package.jsonindex 7f1771bde59..ef6f855911c 100644--- a/packages/pieces/core/sftp/package.json+++ b/packages/pieces/core/sftp/package.json@@ -1,6 +1,6 @@{"name": "@activepieces/piece-sftp",- "version": "0.5.10",+ "version": "0.5.11","main": "./dist/src/index.js","types": "./dist/src/index.d.ts","dependencies": {@@ -17,11 +17,13 @@"devDependencies": {"@types/ssh2": "1.15.5","@types/ssh2-sftp-client": "9.0.6",- "tslib": "2.6.2"+ "tslib": "2.6.2",+ "vitest": "3.2.6"},"scripts": {"build": "tsc -p tsconfig.lib.json && cp package.json dist/","bundle": "node ../../../../dist/packages/cli/src/index.js pieces bundle",- "lint": "eslint 'src/**/*.ts'"+ "lint": "eslint 'src/**/*.ts'",+ "test": "vitest run"}}diff --git a/packages/pieces/core/sftp/src/lib/common.ts b/packages/pieces/core/sftp/src/lib/common.tsindex ddad50ddea0..9f70b71ffde 100644--- a/packages/pieces/core/sftp/src/lib/common.ts+++ b/packages/pieces/core/sftp/src/lib/common.ts@@ -1,4 +1,4 @@-import { isNil } from '@activepieces/pieces-framework';+import { isNil, tryCatch } from '@activepieces/pieces-framework';import Client from 'ssh2-sftp-client';import { ServerHostKeyAlgorithm } from 'ssh2';import { Client as FTPClient } from 'basic-ftp';@@ -87,7 +87,7 @@ export async function getClient<T extends Client | FTPClient>(auth: { protocol:return sftp as T;} else {const ftpClient = new FTPClient(undefined, { allowSeparateTransferHost: true });- await ftpClient.access({+ const { error } = await tryCatch(() => ftpClient.access({host,port,user: username,@@ -96,7 +96,11 @@ export async function getClient<T extends Client | FTPClient>(auth: { protocol:secureOptions: {rejectUnauthorized: !(allow_unauthorized_certificates ?? false),}- });+ }));+ if (error) {+ ftpClient.close();+ throw error;+ }return ftpClient as T;}}diff --git a/packages/pieces/core/sftp/src/lib/triggers/new-modified-file.ts b/packages/pieces/core/sftp/src/lib/triggers/new-modified-file.tsindex dea989e18a3..6bd96aedb70 100644--- a/packages/pieces/core/sftp/src/lib/triggers/new-modified-file.ts+++ b/packages/pieces/core/sftp/src/lib/triggers/new-modified-file.ts@@ -42,8 +42,6 @@ const polling: Polling<AppConnectionValueForAuthProperty<typeof sftpAuth>, { patepochMilliSeconds: dayjs(modTime).valueOf(),};});- } catch (err) {- return [];} finally {if (client) {await endClient(client, auth.props.protocol);diff --git a/packages/pieces/core/sftp/test/new-file-trigger-login-failure.test.ts b/packages/pieces/core/sftp/test/new-file-trigger-login-failure.test.tsnew file mode 100644index 00000000000..51aac8910c0--- /dev/null+++ b/packages/pieces/core/sftp/test/new-file-trigger-login-failure.test.ts@@ -0,0 +1,89 @@+import { Store } from '@activepieces/pieces-framework';+import { beforeEach, describe, expect, test, vi } from 'vitest';+import { newOrModifiedFile } from '../src/lib/triggers/new-modified-file';++const { sftpClient, ftpClient } = vi.hoisted(() => ({+ sftpClient: { connect: vi.fn(), list: vi.fn(), end: vi.fn() },+ ftpClient: { access: vi.fn(), list: vi.fn(), close: vi.fn() },+}));++vi.mock('ssh2-sftp-client', () => ({ default: vi.fn(() => sftpClient) }));+vi.mock('basic-ftp', () => ({ Client: vi.fn(() => ftpClient) }));++const LOGIN_REFUSED = 'All configured authentication methods failed';++describe('New File trigger', () => {+ beforeEach(() => {+ vi.resetAllMocks();+ sftpClient.connect.mockResolvedValue(undefined);+ sftpClient.list.mockResolvedValue([]);+ ftpClient.access.mockResolvedValue(undefined);+ ftpClient.list.mockResolvedValue([]);+ });++ test('fails the poll and keeps the checkpoint while the login is refused, then catches up once it works', async () => {+ const lastPoll = Date.now() - 5 * 60_000;+ const store = memoryStore({ lastPoll });+ sftpClient.connect.mockRejectedValue(new Error(LOGIN_REFUSED));++ await expect(newOrModifiedFile.run(pollContext({ store }))).rejects.toThrow(LOGIN_REFUSED);+ expect(await store.get('lastPoll')).toBe(lastPoll);++ sftpClient.connect.mockResolvedValue(undefined);+ sftpClient.list.mockResolvedValue([{ name: 'invoice.csv', modifyTime: Date.now(), type: '-' }]);++ await expect(newOrModifiedFile.run(pollContext({ store }))).resolves.toEqual([+ expect.objectContaining({ name: 'invoice.csv' }),+ ]);+ });++ test('fails the poll when the watched folder cannot be listed, and still closes the client', async () => {+ sftpClient.list.mockRejectedValue(new Error('No such file'));++ await expect(newOrModifiedFile.run(pollContext({}))).rejects.toThrow('No such file');+ expect(sftpClient.end).toHaveBeenCalledTimes(1);+ });++ test('fails the builder sample-data test while the login is refused', async () => {+ sftpClient.connect.mockRejectedValue(new Error(LOGIN_REFUSED));++ await expect(newOrModifiedFile.test(pollContext({}))).rejects.toThrow(LOGIN_REFUSED);+ });++ test('closes the ftp control socket when the login is refused', async () => {+ ftpClient.access.mockRejectedValue(new Error('530 Login incorrect.'));++ await expect(newOrModifiedFile.run(pollContext({ protocol: 'ftp' }))).rejects.toThrow('530 Login incorrect.');+ expect(ftpClient.close).toHaveBeenCalledTimes(1);+ });++ test('still reports an empty folder as no new files', async () => {+ sftpClient.list.mockResolvedValue([]);++ await expect(newOrModifiedFile.run(pollContext({}))).resolves.toEqual([]);+ });+});++function memoryStore(initial: Record<string, unknown>): Store {+ const entries = new Map(Object.entries(initial));+ return {+ async put<T>(key: string, value: T): Promise<T> {+ entries.set(key, value);+ return value;+ },+ async get<T>(key: string): Promise<T | null> {+ return entries.has(key) ? (entries.get(key) as T) : null;+ },+ async delete(key: string): Promise<void> {+ entries.delete(key);+ },+ };+}++function pollContext({ store = memoryStore({ lastPoll: Date.now() - 60_000 }), protocol = 'sftp' }: { store?: Store; protocol?: string }) {+ return {+ auth: { props: { protocol, host: 'files.example.com', port: 22, username: 'user', password: 'rotated-on-the-server' } },+ propsValue: { path: '/inbox', ignoreHiddenFiles: false },+ store,+ } as never;+}diff --git a/packages/pieces/core/sftp/vitest.config.ts b/packages/pieces/core/sftp/vitest.config.tsnew file mode 100644index 00000000000..57159d92096--- /dev/null+++ b/packages/pieces/core/sftp/vitest.config.ts@@ -0,0 +1,16 @@+import path from 'path'+import { defineConfig } from 'vitest/config'++const repoRoot = path.resolve(__dirname, '../../../..')++export default defineConfig({+ test: {+ environment: 'node',+ },+ resolve: {+ alias: {+ '@activepieces/pieces-framework': path.resolve(repoRoot, 'packages/pieces/framework/src/index.ts'),+ '@activepieces/pieces-common': path.resolve(repoRoot, 'packages/pieces/common/src/index.ts'),+ },+ },+})