From 0325435125d7d367c91744d2e13e685968f51232 Mon Sep 17 00:00:00 2001 From: Yiling-J Date: Thu, 12 Nov 2026 09:22:48 +0800 Subject: [PATCH] fix(storage): remove empty local embedding directories (#527) --- CHANGELOG.md | 2 ++ packages/core/src/s3/s3.test.ts | 12 ++++++++++++ packages/core/src/s3/s3.ts | 19 ++++++++++++++++++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ea624e2..9b076071 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ### Fixed +- **storage**: Remove empty local folders left behind after generating video search embeddings. + ### Changed - **agent**: Upgrade the agent runtime and automatically migrate existing conversations, keep thread histories independent, prevent duplicate history during simultaneous mentions, improve session log scrolling and readability, and fix authentication for OpenAI and custom providers. Agents now run with the main server; standalone agent workers are no longer supported. diff --git a/packages/core/src/s3/s3.test.ts b/packages/core/src/s3/s3.test.ts index 3f673fdf..bc8b177d 100644 --- a/packages/core/src/s3/s3.test.ts +++ b/packages/core/src/s3/s3.test.ts @@ -651,6 +651,18 @@ describe('S3Service implementations', () => { expect(size).toBe(11) }) + it('removes empty embedding chunk directories after deleting the last chunk', async () => { + const key = 'files/asset-1/tmp-embedding-chunks/chunk.mp4' + await localS3.putObject('test-bucket', key, 'chunk', 5) + + expect(await localS3.deleteObject('test-bucket', key)).toBe(1) + + expect(fs.existsSync(path.join(TEST_BASE_PATH, 'test-bucket', 'files', 'asset-1'))).toBe( + false, + ) + expect(fs.existsSync(path.join(TEST_BASE_PATH, 'test-bucket', 'files'))).toBe(true) + }) + it('should write Node.js Readable stream in putObject without corrupting to [object Object]', async () => { const { Readable } = await import('stream') const stream = Readable.from(['hello ', 'node ', 'stream']) diff --git a/packages/core/src/s3/s3.ts b/packages/core/src/s3/s3.ts index a107baac..ca22ad6d 100644 --- a/packages/core/src/s3/s3.ts +++ b/packages/core/src/s3/s3.ts @@ -939,7 +939,6 @@ export class LocalStorageService implements S3Service { const filePath = this.getFilePath(bucket, key) try { await fs.promises.unlink(filePath) - return 1 } catch (e: unknown) { if (e instanceof Error && (e as NodeJS.ErrnoException).code === 'ENOENT') { // Ignore if file doesn't exist @@ -947,6 +946,24 @@ export class LocalStorageService implements S3Service { } throw e } + + const bucketPath = path.resolve(this.basePath, bucket) + let directory = path.dirname(filePath) + // Preserve the bucket and its top-level directories, such as files/. + while ( + directory.startsWith(`${bucketPath}${path.sep}`) && + path.dirname(directory) !== bucketPath + ) { + try { + await fs.promises.rmdir(directory) + } catch (e: unknown) { + const code = e instanceof Error ? (e as NodeJS.ErrnoException).code : undefined + if (code === 'ENOTEMPTY' || code === 'EEXIST') break + if (code !== 'ENOENT') throw e + } + directory = path.dirname(directory) + } + return 1 } async deletePrefix(bucket: string, prefix: string): Promise {