From 3f02ef6b341c633da54b8392b5cedf66df73ca47 Mon Sep 17 00:00:00 2001 From: Elon-Jiang <191222665+Elon-Jiang@users.noreply.github.com> Date: Tue, 10 Nov 2026 13:38:54 +0800 Subject: [PATCH 1/2] fix(core): report in-memory file sizes in UTF-8 bytes Co-authored-by: OpenAI Codex --- .changeset/in-memory-file-byte-sizes.md | 5 +++ .../filesystem/backends/in-memory.ts | 6 +-- .../src/workspace/filesystem/index.spec.ts | 40 ++++++++++++++++++- 3 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 .changeset/in-memory-file-byte-sizes.md diff --git a/.changeset/in-memory-file-byte-sizes.md b/.changeset/in-memory-file-byte-sizes.md new file mode 100644 index 000000000..603a8bd9d --- /dev/null +++ b/.changeset/in-memory-file-byte-sizes.md @@ -0,0 +1,5 @@ +--- +"@voltagent/core": patch +--- + +Report UTF-8 byte sizes for in-memory workspace files in stat, directory listings, and glob results, matching the disk backend for non-ASCII content. diff --git a/packages/core/src/workspace/filesystem/backends/in-memory.ts b/packages/core/src/workspace/filesystem/backends/in-memory.ts index b0a88406c..3090f9a61 100644 --- a/packages/core/src/workspace/filesystem/backends/in-memory.ts +++ b/packages/core/src/workspace/filesystem/backends/in-memory.ts @@ -81,7 +81,7 @@ export class InMemoryFilesystemBackend implements FilesystemBackend { continue; } - const size = fd.content.join("\n").length; + const size = new TextEncoder().encode(fd.content.join("\n")).byteLength; infos.push({ path: filePath, is_dir: false, @@ -153,7 +153,7 @@ export class InMemoryFilesystemBackend implements FilesystemBackend { const fileData = files[filePath]; if (fileData) { - const size = fileData.content.join("\n").length; + const size = new TextEncoder().encode(fileData.content.join("\n")).byteLength; return { path: filePath, is_dir: false, @@ -285,7 +285,7 @@ export class InMemoryFilesystemBackend implements FilesystemBackend { const infos: FileInfo[] = []; for (const filePath of paths) { const fd = files[filePath]; - const size = fd ? fd.content.join("\n").length : 0; + const size = fd ? new TextEncoder().encode(fd.content.join("\n")).byteLength : 0; infos.push({ path: filePath, is_dir: false, diff --git a/packages/core/src/workspace/filesystem/index.spec.ts b/packages/core/src/workspace/filesystem/index.spec.ts index 173949219..13b9f6bd9 100644 --- a/packages/core/src/workspace/filesystem/index.spec.ts +++ b/packages/core/src/workspace/filesystem/index.spec.ts @@ -1,7 +1,45 @@ +import { mkdtemp, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import path from "node:path"; import { describe, expect, it } from "vitest"; -import { WorkspaceFilesystem } from "./index"; +import { NodeFilesystemBackend, WorkspaceFilesystem } from "./index"; describe("WorkspaceFilesystem", () => { + describe.each([ + ["ASCII", "hello", 5], + ["empty", "", 0], + ["Chinese", "你好", 6], + ["emoji and newline", "a\n😀", 6], + ["combining mark", "e\u0301", 3], + ] as const)("UTF-8 byte sizes for %s files", (_label, content, expectedSize) => { + it.each(["stat", "lsInfo", "globInfo"] as const)( + "reports byte sizes via %s", + async (operation) => { + const root = await mkdtemp(path.join(tmpdir(), "va-byte-size-")); + try { + const memory = new WorkspaceFilesystem(); + const disk = new WorkspaceFilesystem({ + backend: new NodeFilesystemBackend({ rootDir: root }), + }); + for (const filesystem of [disk, memory]) { + const result = await filesystem.write("/content.txt", content); + expect(result.error).toBeUndefined(); + expect((await filesystem.readRaw("/content.txt")).content.join("\n")).toBe(content); + const info = + operation === "stat" + ? await filesystem.stat("/content.txt") + : operation === "lsInfo" + ? (await filesystem.lsInfo("/"))[0] + : (await filesystem.globInfo("*.txt", "/"))[0]; + expect(info?.size).toBe(expectedSize); + } + } finally { + await rm(root, { recursive: true, force: true }); + } + }, + ); + }); + it("writes, reads, edits, and deletes files", async () => { const filesystem = new WorkspaceFilesystem(); From 250fe187233692affb0210d351daf2843495c57d Mon Sep 17 00:00:00 2001 From: Elon-Jiang <191222665+Elon-Jiang@users.noreply.github.com> Date: Tue, 10 Nov 2026 13:47:40 +0800 Subject: [PATCH 2/2] perf(core): avoid encoded buffers for Node file size queries Co-authored-by: OpenAI Codex --- .../workspace/filesystem/backends/in-memory.ts | 13 ++++++++++--- .../core/src/workspace/filesystem/index.spec.ts | 15 ++++++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/packages/core/src/workspace/filesystem/backends/in-memory.ts b/packages/core/src/workspace/filesystem/backends/in-memory.ts index 3090f9a61..4f388829f 100644 --- a/packages/core/src/workspace/filesystem/backends/in-memory.ts +++ b/packages/core/src/workspace/filesystem/backends/in-memory.ts @@ -38,6 +38,13 @@ export class InMemoryFilesystemBackend implements FilesystemBackend { return this.directories; } + private getFileSize(fileData: FileData): number { + const content = fileDataToString(fileData); + return typeof Buffer !== "undefined" + ? Buffer.byteLength(content, "utf8") + : new TextEncoder().encode(content).byteLength; + } + private normalizeDirPath(path: string): string { const trimmed = path.trim(); if (!trimmed) { @@ -81,7 +88,7 @@ export class InMemoryFilesystemBackend implements FilesystemBackend { continue; } - const size = new TextEncoder().encode(fd.content.join("\n")).byteLength; + const size = this.getFileSize(fd); infos.push({ path: filePath, is_dir: false, @@ -153,7 +160,7 @@ export class InMemoryFilesystemBackend implements FilesystemBackend { const fileData = files[filePath]; if (fileData) { - const size = new TextEncoder().encode(fileData.content.join("\n")).byteLength; + const size = this.getFileSize(fileData); return { path: filePath, is_dir: false, @@ -285,7 +292,7 @@ export class InMemoryFilesystemBackend implements FilesystemBackend { const infos: FileInfo[] = []; for (const filePath of paths) { const fd = files[filePath]; - const size = fd ? new TextEncoder().encode(fd.content.join("\n")).byteLength : 0; + const size = fd ? this.getFileSize(fd) : 0; infos.push({ path: filePath, is_dir: false, diff --git a/packages/core/src/workspace/filesystem/index.spec.ts b/packages/core/src/workspace/filesystem/index.spec.ts index 13b9f6bd9..388864235 100644 --- a/packages/core/src/workspace/filesystem/index.spec.ts +++ b/packages/core/src/workspace/filesystem/index.spec.ts @@ -1,10 +1,23 @@ import { mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; -import { describe, expect, it } from "vitest"; +import { describe, expect, it, vi } from "vitest"; import { NodeFilesystemBackend, WorkspaceFilesystem } from "./index"; describe("WorkspaceFilesystem", () => { + it("reports byte sizes when Buffer is unavailable", async () => { + const filesystem = new WorkspaceFilesystem(); + await filesystem.write("/content.txt", "你好"); + vi.stubGlobal("Buffer", undefined); + try { + expect((await filesystem.stat("/content.txt"))?.size).toBe(6); + expect((await filesystem.lsInfo("/"))[0].size).toBe(6); + expect((await filesystem.globInfo("*.txt", "/"))[0].size).toBe(6); + } finally { + vi.unstubAllGlobals(); + } + }); + describe.each([ ["ASCII", "hello", 5], ["empty", "", 0],