Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
feat(sdks): expose user-defined file metadata on sandbox.files (#1383)
Mish Ushakov961ffbae848a
diff --git a/.changeset/file-metadata-xattrs.md b/.changeset/file-metadata-xattrs.mdnew file mode 100644index 000000000..d8da10393--- /dev/null+++ b/.changeset/file-metadata-xattrs.md@@ -0,0 +1,8 @@+---+'e2b': minor+'@e2b/python-sdk': minor+---++feat(sdks): expose user-defined file metadata on `sandbox.files`++Adds a `metadata` option to file uploads (`write` / `writeFiles` / `write_files`) and surfaces persisted metadata on every `EntryInfo` / `WriteInfo` returned by `getInfo`, `list`, `rename`, and write responses. On upload, metadata is sent as `X-Metadata-<key>: <value>` request headers; envd persists the values as extended attributes in the `user.e2b.` xattr namespace and returns them on subsequent filesystem reads (including `user.e2b.*` xattrs set out-of-band). Keys are sent as HTTP header names and are lowercased by the sandbox; metadata is validated client-side (keys must be valid HTTP header tokens, values must be printable US-ASCII) and invalid input raises `InvalidArgumentError` / `InvalidArgumentException`. The same metadata map is applied to every file in a multi-file upload. Requires envd 0.6.2 or later.diff --git a/packages/js-sdk/src/envd/filesystem/filesystem_pb.ts b/packages/js-sdk/src/envd/filesystem/filesystem_pb.tsindex 06138291c..ee1b4167c 100644--- a/packages/js-sdk/src/envd/filesystem/filesystem_pb.ts+++ b/packages/js-sdk/src/envd/filesystem/filesystem_pb.ts@@ -24,7 +24,7 @@ import type { Message } from '@bufbuild/protobuf'export const file_filesystem_filesystem: GenFile =/*@__PURE__*/fileDesc(- 'ChtmaWxlc3lzdGVtL2ZpbGVzeXN0ZW0ucHJvdG8SCmZpbGVzeXN0ZW0iMgoLTW92ZVJlcXVlc3QSDgoGc291cmNlGAEgASgJEhMKC2Rlc3RpbmF0aW9uGAIgASgJIjQKDE1vdmVSZXNwb25zZRIkCgVlbnRyeRgBIAEoCzIVLmZpbGVzeXN0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xlc3lzdGVtXEdQQk1ldGFkYXRh6gIKRmlsZXN5c3RlbWIGcHJvdG8z',[file_google_protobuf_timestamp])@@ -227,6 +227,15 @@ export type EntryInfo = Message<'filesystem.EntryInfo'> & {* @generated from field: optional string symlink_target = 10;*/symlinkTarget?: string++ /**+ * User-defined metadata stored as extended attributes (xattrs) on the file.+ * Keys live under the `user.e2b.` xattr namespace; the prefix is stripped here.+ * Plain `user.*` xattrs written by other tooling are not reflected.+ *+ * @generated from field: map<string, string> metadata = 11;+ */+ metadata: { [key: string]: string }}/**diff --git a/packages/js-sdk/src/envd/schema.gen.ts b/packages/js-sdk/src/envd/schema.gen.tsindex 9c39a43d3..a6b872514 100644--- a/packages/js-sdk/src/envd/schema.gen.ts+++ b/packages/js-sdk/src/envd/schema.gen.ts@@ -74,7 +74,30 @@ export interface paths {};};put?: never;- /** Upload a file and ensure the parent directories exist. If the file exists, it will be overwritten. */+ /**+ * Upload a file and ensure the parent directories exist. If the file exists, it will be overwritten.+ * @description Any request header of the form `X-Metadata-<key>: <value>` is persisted+ * as a user-defined extended attribute on the uploaded file. The+ * `X-Metadata-` prefix is stripped and the remaining header name is+ * lowercased to form the metadata key; the resulting map is returned on+ * `EntryInfo` lookups (e.g. `Stat`, `ListDir`).+ *+ * Each upload replaces the file's metadata with the keys provided in+ * that request: keys previously stored but absent from the new request+ * are removed, and an upload that sends no `X-Metadata-*` header clears+ * all existing metadata.+ *+ * Both keys and values must be printable US-ASCII (bytes `0x20`-`0x7E`)+ * and are rejected with HTTP 400 otherwise. Each key is capped at 246+ * bytes (the Linux VFS xattr-name limit minus the namespace prefix), and+ * the combined size of all metadata on a file (keys plus values, with the+ * namespace prefix counted per key) is capped at 4096 bytes to stay within+ * the filesystem's per-inode xattr budget. Multiple files in a single+ * multipart upload receive the same metadata. If the same+ * `X-Metadata-<key>` header is sent more than once, only the first+ * value is used.+ *+ */post: {parameters: {query?: {@@ -234,6 +257,10 @@ export type webhooks = Record<string, never>;export interface components {schemas: {EntryInfo: {+ /** @description User-defined metadata stored as extended attributes on the file. */+ metadata?: {+ [key: string]: string;+ };/** @description Name of the file */name: string;/** @description Path to the file */diff --git a/packages/js-sdk/src/envd/versions.ts b/packages/js-sdk/src/envd/versions.tsindex 6348ff675..53e5bf644 100644--- a/packages/js-sdk/src/envd/versions.ts+++ b/packages/js-sdk/src/envd/versions.ts@@ -4,3 +4,4 @@ export const ENVD_COMMANDS_STDIN = '0.3.0'export const ENVD_DEFAULT_USER = '0.4.0'export const ENVD_ENVD_CLOSE = '0.5.2'export const ENVD_OCTET_STREAM_UPLOAD = '0.5.7'+export const ENVD_FILE_METADATA = '0.6.2'diff --git a/packages/js-sdk/src/sandbox/filesystem/index.ts b/packages/js-sdk/src/sandbox/filesystem/index.tsindex 099564e53..7e9fa2009 100644--- a/packages/js-sdk/src/sandbox/filesystem/index.ts+++ b/packages/js-sdk/src/sandbox/filesystem/index.ts@@ -30,6 +30,7 @@ import type { Timestamp } from '@bufbuild/protobuf/wkt'import { compareVersions } from 'compare-versions'import {ENVD_DEFAULT_USER,+ ENVD_FILE_METADATA,ENVD_OCTET_STREAM_UPLOAD,ENVD_VERSION_RECURSIVE_WATCH,} from '../../envd/versions'@@ -77,6 +78,13 @@ export interface WriteInfo {* Path to the filesystem object.*/path: string+ /**+ * User-defined metadata stored on the file as `user.e2b.*` extended+ * attributes. On writes this reflects the metadata supplied on upload; on+ * reads (`getInfo`, `list`, `rename`) it reflects any `user.e2b.*` xattr on+ * the file, including ones set out-of-band. `undefined` when none is set.+ */+ metadata?: Record<string, string>}export interface EntryInfo extends WriteInfo {@@ -153,6 +161,52 @@ function mapModifiedTime(modifiedTime: Timestamp | undefined) {)}+function mapMetadata(+ metadata: Record<string, string> | undefined+): Record<string, string> | undefined {+ if (!metadata) return undefined+ return Object.keys(metadata).length === 0 ? undefined : metadata+}++const METADATA_HEADER_PREFIX = 'X-Metadata-'++// Metadata keys travel as `X-Metadata-<key>` HTTP header names, so they must be+// valid header tokens (RFC 7230); values travel as header values, restricted to+// printable US-ASCII.+const METADATA_KEY_REGEX = /^[A-Za-z0-9!#$%&'*+\-.^_`|~]+$/+const METADATA_VALUE_REGEX = /^[\x20-\x7e]*$/++function validateMetadata(metadata: Record<string, string> | undefined): void {+ if (!metadata) return+ for (const [key, value] of Object.entries(metadata)) {+ if (!METADATA_KEY_REGEX.test(key)) {+ throw new InvalidArgumentError(+ `Invalid metadata key ${JSON.stringify(+ key+ )}: keys must be non-empty and use only HTTP token characters (letters, digits and !#$%&'*+-.^_\`|~).`+ )+ }+ if (!METADATA_VALUE_REGEX.test(value)) {+ throw new InvalidArgumentError(+ `Invalid metadata value for key ${JSON.stringify(+ key+ )}: values must be printable US-ASCII.`+ )+ }+ }+}++function metadataHeaders(+ metadata: Record<string, string> | undefined+): Record<string, string> {+ if (!metadata) return {}+ const headers: Record<string, string> = {}+ for (const [key, value] of Object.entries(metadata)) {+ headers[`${METADATA_HEADER_PREFIX}${key}`] = value+ }+ return headers+}+/*** Options for the sandbox filesystem operations.*/@@ -180,6 +234,14 @@ export interface FilesystemWriteOpts extends FilesystemRequestOpts {* the sandbox's envd version, the upload falls back to `multipart/form-data`.*/useOctetStream?: boolean+ /**+ * User-defined metadata to persist on the uploaded file(s) as extended+ * attributes. Keys are lowercased by the sandbox, so they may differ in case+ * when read back. Invalid keys or values throw an `InvalidArgumentError`.+ * The same metadata is applied to every file in a multi-file upload.+ * Requires envd 0.6.2 or later.+ */+ metadata?: Record<string, string>}/**@@ -431,6 +493,19 @@ export class Filesystem {const useOctetStream =(writeOpts?.useOctetStream ?? false) && supportsOctetStream+ const metadata = writeOpts?.metadata+ validateMetadata(metadata)+ if (+ metadata &&+ Object.keys(metadata).length > 0 &&+ compareVersions(this.envdApi.version, ENVD_FILE_METADATA) < 0+ ) {+ throw new TemplateError('File metadata requires envd 0.6.2 or later.')+ }+ // Metadata is sent as request-scoped `X-Metadata-*` headers, so the same+ // metadata is applied to every file in a multi-file upload.+ const extraHeaders = metadataHeaders(metadata)+const results: WriteInfo[] = []const useGzip = writeOpts?.gzip === true@@ -438,6 +513,7 @@ export class Filesystem {if (useOctetStream) {const headers: Record<string, string> = {'Content-Type': 'application/octet-stream',+ ...extraHeaders,}if (useGzip) {headers['Content-Encoding'] = 'gzip'@@ -476,6 +552,10 @@ export class Filesystem {)}+ for (const f of files) {+ f.metadata = mapMetadata(f.metadata)+ }+return files}))@@ -501,6 +581,7 @@ export class Filesystem {},},bodySerializer: () => formData,+ headers: extraHeaders,signal: this.connectionConfig.getSignal(writeOpts?.requestTimeoutMs,writeOpts?.signal@@ -518,6 +599,10 @@ export class Filesystem {throw new Error('Expected to receive information about written file')}+ for (const f of files) {+ f.metadata = mapMetadata(f.metadata)+ }+results.push(...files)}@@ -591,6 +676,7 @@ export class Filesystem {group: e.group,modifiedTime: mapModifiedTime(e.modifiedTime),symlinkTarget: e.symlinkTarget,+ metadata: mapMetadata(e.metadata),})}}@@ -679,6 +765,7 @@ export class Filesystem {group: entry.group,modifiedTime: mapModifiedTime(entry.modifiedTime),symlinkTarget: entry.symlinkTarget,+ metadata: mapMetadata(entry.metadata),}} catch (err) {throw handleFilesystemRpcError(err)@@ -782,6 +869,7 @@ export class Filesystem {group: res.entry.group,modifiedTime: mapModifiedTime(res.entry.modifiedTime),symlinkTarget: res.entry.symlinkTarget,+ metadata: mapMetadata(res.entry.metadata),}} catch (err) {throw handleFilesystemRpcError(err)diff --git a/packages/js-sdk/tests/sandbox/files/metadata.test.ts b/packages/js-sdk/tests/sandbox/files/metadata.test.tsnew file mode 100644index 000000000..f6ff5d533--- /dev/null+++ b/packages/js-sdk/tests/sandbox/files/metadata.test.ts@@ -0,0 +1,186 @@+import { assert, expect } from 'vitest'++import { WriteEntry } from '../../../src/sandbox/filesystem'+import { InvalidArgumentError } from '../../../src/errors.js'+import { isDebug, sandboxTest } from '../../setup.js'++sandboxTest('write file with metadata', async ({ sandbox }) => {+ const filename = 'test_metadata.txt'+ const content = 'This is a test file with metadata.'+ const metadata = { author: 'mish', purpose: 'upload' }++ const info = await sandbox.files.write(filename, content, { metadata })+ assert.isFalse(Array.isArray(info))+ assert.deepEqual(info.metadata, metadata)++ // Metadata is persisted and surfaced on subsequent reads.+ const stat = await sandbox.files.getInfo(filename)+ assert.deepEqual(stat.metadata, metadata)++ if (isDebug) {+ await sandbox.files.remove(filename)+ }+})++sandboxTest(+ 'write file with metadata using octet-stream',+ async ({ sandbox }) => {+ const filename = 'test_metadata_octet.txt'+ const content = 'This is a test file with metadata.'+ const metadata = { author: 'mish', purpose: 'upload' }++ const info = await sandbox.files.write(filename, content, {+ metadata,+ useOctetStream: true,+ })+ assert.deepEqual(info.metadata, metadata)++ const stat = await sandbox.files.getInfo(filename)+ assert.deepEqual(stat.metadata, metadata)++ if (isDebug) {+ await sandbox.files.remove(filename)+ }+ }+)++sandboxTest('write file without metadata', async ({ sandbox }) => {+ const filename = 'test_no_metadata.txt'++ const info = await sandbox.files.write(filename, 'no metadata here')+ assert.isUndefined(info.metadata)++ const stat = await sandbox.files.getInfo(filename)+ assert.isUndefined(stat.metadata)++ if (isDebug) {+ await sandbox.files.remove(filename)+ }+})++sandboxTest(+ 'writeFiles applies metadata to every file',+ async ({ sandbox }) => {+ // The same metadata is applied to every file in the upload.+ const metadata = { source: 'test-suite' }+ const files: WriteEntry[] = [+ { path: 'metadata_multi_1.txt', data: 'File 1' },+ { path: 'metadata_multi_2.txt', data: 'File 2' },+ ]++ const infos = await sandbox.files.writeFiles(files, { metadata })+ assert.equal(infos.length, files.length)++ for (const info of infos) {+ assert.deepEqual(info.metadata, metadata)++ const stat = await sandbox.files.getInfo(info.path)+ assert.deepEqual(stat.metadata, metadata)+ }++ if (isDebug) {+ for (const file of files) {+ await sandbox.files.remove(file.path)+ }+ }+ }+)++sandboxTest('metadata is surfaced when listing', async ({ sandbox }) => {+ const dirname = 'metadata_list_dir'+ const filename = 'listed.txt'+ const metadata = { tag: 'listed' }++ await sandbox.files.makeDir(dirname)+ await sandbox.files.write(`${dirname}/${filename}`, 'content', { metadata })++ const entries = await sandbox.files.list(dirname)+ const entry = entries.find((e) => e.name === filename)+ assert.isDefined(entry)+ assert.deepEqual(entry?.metadata, metadata)++ if (isDebug) {+ await sandbox.files.remove(dirname)+ }+})++sandboxTest('metadata is surfaced after rename', async ({ sandbox }) => {+ const oldPath = 'metadata_rename_old.txt'+ const newPath = 'metadata_rename_new.txt'+ const metadata = { stage: 'renamed' }++ await sandbox.files.write(oldPath, 'content', { metadata })+ const info = await sandbox.files.rename(oldPath, newPath)+ assert.deepEqual(info.metadata, metadata)++ if (isDebug) {+ await sandbox.files.remove(newPath)+ }+})++sandboxTest('overwriting a file clears stale metadata', async ({ sandbox }) => {+ const filename = 'metadata_overwrite.txt'++ await sandbox.files.write(filename, 'first', {+ metadata: { author: 'mish' },+ })++ // Overwriting without metadata removes the previously stored metadata.+ const info = await sandbox.files.write(filename, 'second')+ assert.isUndefined(info.metadata)++ const stat = await sandbox.files.getInfo(filename)+ assert.isUndefined(stat.metadata)++ if (isDebug) {+ await sandbox.files.remove(filename)+ }+})++sandboxTest(+ 'metadata set via xattrs is surfaced in getInfo',+ async ({ sandbox }) => {+ const filename = 'metadata_xattr.txt'+ await sandbox.files.write(filename, 'content')++ const { stdout: filePath } = await sandbox.commands.run(+ `realpath ${filename}`+ )++ // Set an xattr directly in the `user.e2b.` namespace (out-of-band, not via+ // the SDK upload); it should surface as metadata (with the namespace prefix+ // stripped) when reading the file info.+ await sandbox.commands.run(+ `python3 -c "import os; os.setxattr('${filePath.trim()}', 'user.e2b.author', b'mish')"`+ )++ const info = await sandbox.files.getInfo(filename)+ assert.deepEqual(info.metadata, { author: 'mish' })++ if (isDebug) {+ await sandbox.files.remove(filename)+ }+ }+)++sandboxTest('rejects invalid metadata', async ({ sandbox }) => {+ const filename = 'invalid_metadata.txt'++ // Key with a space is not a valid HTTP header token.+ await expect(+ sandbox.files.write(filename, 'x', { metadata: { 'bad key': 'value' } })+ ).rejects.toThrow(InvalidArgumentError)++ // Empty key.+ await expect(+ sandbox.files.write(filename, 'x', { metadata: { '': 'value' } })+ ).rejects.toThrow(InvalidArgumentError)++ // Value with a non-printable / non-ASCII character.+ await expect(+ sandbox.files.write(filename, 'x', { metadata: { good: 'bad\nvalue' } })+ ).rejects.toThrow(InvalidArgumentError)++ // The file must not have been created by a rejected write.+ assert.isFalse(await sandbox.files.exists(filename))+})diff --git a/packages/python-sdk/e2b/envd/filesystem/filesystem_pb2.py b/packages/python-sdk/e2b/envd/filesystem/filesystem_pb2.pyindex 54bb90c49..9d9a39428 100644--- a/packages/python-sdk/e2b/envd/filesystem/filesystem_pb2.py+++ b/packages/python-sdk/e2b/envd/filesystem/filesystem_pb2.py@@ -15,7 +15,7 @@ _sym_db = _symbol_database.Default()from google.protobuf import timestamp_pb2 as google_dot_protobuf_dot_timestamp__pb2-DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x1b\x66ilesystem/filesystem.proto\x12\nfilesystem\x1a\x1fgoogle/protobuf/timestamp.proto\"G\n\x0bMoveRequest\x12\x16\n\x06source\x18\x01 \x01(\tR\x06source\x12 \n\x0b\x64\x65stination\x18\x02 \x01(\tR\x0b\x64\x65stination\";\n\x0cMoveResponse\x12+\n\x05\x65ntry\x18\x01 \x01(\x0b\x32\x15.filesystem.EntryInfoR\x05\x65ntry\"$\n\x0eMakeDirRequest\x12\x12\n\x04path\x18\x01 \x01(\tR\x04path\">\n\x0fMakeDirResponse\x12+\n\x05\x65ntry\x18\x01 \x01(\x0b\x32\x15.filesystem.EntryInfoR\x05\x65ntry\"#\n\rRemoveRequest\x12\x12\n\x04path\x18\x01 \x01(\tR\x04path\"\x10\n\x0eRemoveResponse\"!\n\x0bStatRequest\x12\x12\n\x04path\x18\x01 \x01(\tR\x04path\";\n\x0cStatResponse\x12+\n\x05\x65ntry\x18\x01 \x01(\x0b\x32\x15.filesystem.EntryInfoR\x05\x65ntry\"\xd3\x02\n\tEntryInfo\x12\x12\n\x04name\x18\x01 \x01(\tR\x04name\x12(\n\x04type\x18\x02 \x01(\x0e\x32\x14.filesystem.FileTypeR\x04type\x12\x12\n\x04path\x18\x03 \x01(\tR\x04path\x12\x12\n\x04size\x18\x04 \x01(\x03R\x04size\x12\x12\n\x04mode\x18\x05 \x01(\rR\x04mode\x12 \n\x0bpermissions\x18\x06 \x01(\tR\x0bpermissions\x12\x14\n\x05owner\x18\x07 \x01(\tR\x05owner\x12\x14\n\x05group\x18\x08 \x01(\tR\x05group\x12?\n\rmodified_time\x18\t \x01(\x0b\x32\x1a.google.protobuf.TimestampR\x0cmodifiedTime\x12*\n\x0esymlink_target\x18\n \x01(\tH\x00R\rsymlinkTarget\x88\x01\x01\x42\x11\n\x0f_symlink_target\":\n\x0eListDirRequest\x12\x12\n\x04path\x18\x01 \x01(\tR\x04path\x12\x14\n\x05\x64\x65pth\x18\x02 \x01(\rR\x05\x64\x65pth\"B\n\x0fListDirResponse\x12/\n\x07\x65ntries\x18\x01 \x03(\x0b\x32\x15.filesystem.EntryInfoR\x07\x65ntries\"C\n\x0fWatchDirRequest\x12\x12\n\x04path\x18\x01 \x01(\tR\x04path\x12\x1c\n\trecursive\x18\x02 \x01(\x08R\trecursive\"P\n\x0f\x46ilesystemEvent\x12\x12\n\x04name\x18\x01 \x01(\tR\x04name\x12)\n\x04type\x18\x02 \x01(\x0e\x32\x15.filesystem.EventTypeR\x04type\"\xfe\x01\n\x10WatchDirResponse\x12?\n\x05start\x18\x01 \x01(\x0b\x32\'.filesystem.WatchDirResponse.StartEventH\x00R\x05start\x12=\n\nfilesystem\x18\x02 \x01(\x0b\x32\x1b.filesystem.FilesystemEventH\x00R\nfilesystem\x12\x46\n\tkeepalive\x18\x03 \x01(\x0b\x32&.filesystem.WatchDirResponse.KeepAliveH\x00R\tkeepalive\x1a\x0c\n\nStartEvent\x1a\x0b\n\tKeepAliveB\x07\n\x05\x65vent\"H\n\x14\x43reateWatcherRequest\x12\x12\n\x04path\x18\x01 \x01(\tR\x04path\x12\x1c\n\trecursive\x18\x02 \x01(\x08R\trecursive\"6\n\x15\x43reateWatcherResponse\x12\x1d\n\nwatcher_id\x18\x01 \x01(\tR\twatcherId\"8\n\x17GetWatcherEventsRequest\x12\x1d\n\nwatcher_id\x18\x01 \x01(\tR\twatcherId\"O\n\x18GetWatcherEventsResponse\x12\x33\n\x06\x65vents\x18\x01 \x03(\x0b\x32\x1b.filesystem.FilesystemEventR\x06\x65vents\"5\n\x14RemoveWatcherRequest\x12\x1d\n\nwatcher_id\x18\x01 \x01(\tR\twatcherId\"\x17\n\x15RemoveWatcherResponse*R\n\x08\x46ileType\x12\x19\n\x15\x46ILE_TYPE_UNSPECIFIED\x10\x00\x12\x12\n\x0e\x46ILE_TYPE_FILE\x10\x01\x12\x17\n\x13\x46ILE_TYPE_DIRECTORY\x10\x02*\x98\x01\n\tEventType\x12\x1a\n\x16\x45VENT_TYPE_UNSPECIFIED\x10\x00\x12\x15\n\x11\x45VENT_TYPE_CREATE\x10\x01\x12\x14\n\x10\x45VENT_TYPE_WRITE\x10\x02\x12\x15\n\x11\x45VENT_TYPE_REMOVE\x10\x03\x12\x15\n\x11\x45VENT_TYPE_RENAME\x10\x04\x12\x14\n\x10\x45VENT_TYPE_CHMOD\x10\x05\x32\x9f\x05\n\nFilesystem\x12\x39\n\x04Stat\x12\x17.filesystem.StatRequest\x1a\x18.filesystem.StatResponse\x12\x42\n\x07MakeDir\x12\x1a.filesystem.MakeDirRequest\x1a\x1b.filesystem.MakeDirResponse\x12\x39\n\x04Move\x12\x17.filesystem.MoveRequest\x1a\x18.filesystem.MoveResponse\x12\x42\n\x07ListDir\x12\x1a.filesystem.ListDirRequest\x1a\x1b.filesystem.ListDirResponse\x12?\n\x06Remove\x12\x19.filesystem.RemoveRequest\x1a\x1a.filesystem.RemoveResponse\x12G\n\x08WatchDir\x12\x1b.filesystem.WatchDirRequest\x1a\x1c.filesystem.WatchDirResponse0\x01\x12T\n\rCreateWatcher\x12 .filesystem.CreateWatcherRequest\x1a!.filesystem.CreateWatcherResponse\x12]\n\x10GetWatcherEvents\x12#.filesystem.GetWatcherEventsRequest\x1a$.filesystem.GetWatcherEventsResponse\x12T\n\rRemoveWatcher\x12 .filesystem.RemoveWatcherRequest\x1a!.filesystem.RemoveWatcherResponseBi\n\x0e\x63om.filesystemB\x0f\x46ilesystemProtoP\x01\xa2\x02\x03\x46XX\xaa\x02\nFilesystem\xca\x02\nFilesystem\xe2\x02\x16\x46ilesystem\\GPBMetadata\xea\x02\nFilesystemb\x06proto3')+DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x1b\x66ilesystem/filesystem.proto\x12\nfilesystem\x1a\x1fgoogle/protobuf/timestamp.proto\"G\n\x0bMoveRequest\x12\x16\n\x06source\x18\x01 \x01(\tR\x06source\x12 \n\x0b\x64\x65stination\x18\x02 \x01(\tR\x0b\x64\x65stination\";\n\x0cMoveResponse\x12+\n\x05\x65ntry\x18\x01 \x01(\x0b\x32\x15.filesystem.EntryInfoR\x05\x65ntry\"$\n\x0eMakeDirRequest\x12\x12\n\x04path\x18\x01 \x01(\tR\x04path\">\n\x0fMakeDirResponse\x12+\n\x05\x65ntry\x18\x01 \x01(\x0b\x32\x15.filesystem.EntryInfoR\x05\x65ntry\"#\n\rRemoveRequest\x12\x12\n\x04path\x18\x01 \x01(\tR\x04path\"\x10\n\x0eRemoveResponse\"!\n\x0bStatRequest\x12\x12\n\x04path\x18\x01 \x01(\tR\x04path\";\n\x0cStatResponse\x12+\n\x05\x65ntry\x18\x01 \x01(\x0b\x32\x15.filesystem.EntryInfoR\x05\x65ntry\"\xd1\x03\n\tEntryInfo\x12\x12\n\x04name\x18\x01 \x01(\tR\x04name\x12(\n\x04type\x18\x02 \x01(\x0e\x32\x14.filesystem.FileTypeR\x04type\x12\x12\n\x04path\x18\x03 \x01(\tR\x04path\x12\x12\n\x04size\x18\x04 \x01(\x03R\x04size\x12\x12\n\x04mode\x18\x05 \x01(\rR\x04mode\x12 \n\x0bpermissions\x18\x06 \x01(\tR\x0bpermissions\x12\x14\n\x05owner\x18\x07 \x01(\tR\x05owner\x12\x14\n\x05group\x18\x08 \x01(\tR\x05group\x12?\n\rmodified_time\x18\t \x01(\x0b\x32\x1a.google.protobuf.TimestampR\x0cmodifiedTime\x12*\n\x0esymlink_target\x18\n \x01(\tH\x00R\rsymlinkTarget\x88\x01\x01\x12?\n\x08metadata\x18\x0b \x03(\x0b\x32#.filesystem.EntryInfo.MetadataEntryR\x08metadata\x1a;\n\rMetadataEntry\x12\x10\n\x03key\x18\x01 \x01(\tR\x03key\x12\x14\n\x05value\x18\x02 \x01(\tR\x05value:\x02\x38\x01\x42\x11\n\x0f_symlink_target\":\n\x0eListDirRequest\x12\x12\n\x04path\x18\x01 \x01(\tR\x04path\x12\x14\n\x05\x64\x65pth\x18\x02 \x01(\rR\x05\x64\x65pth\"B\n\x0fListDirResponse\x12/\n\x07\x65ntries\x18\x01 \x03(\x0b\x32\x15.filesystem.EntryInfoR\x07\x65ntries\"C\n\x0fWatchDirRequest\x12\x12\n\x04path\x18\x01 \x01(\tR\x04path\x12\x1c\n\trecursive\x18\x02 \x01(\x08R\trecursive\"P\n\x0f\x46ilesystemEvent\x12\x12\n\x04name\x18\x01 \x01(\tR\x04name\x12)\n\x04type\x18\x02 \x01(\x0e\x32\x15.filesystem.EventTypeR\x04type\"\xfe\x01\n\x10WatchDirResponse\x12?\n\x05start\x18\x01 \x01(\x0b\x32\'.filesystem.WatchDirResponse.StartEventH\x00R\x05start\x12=\n\nfilesystem\x18\x02 \x01(\x0b\x32\x1b.filesystem.FilesystemEventH\x00R\nfilesystem\x12\x46\n\tkeepalive\x18\x03 \x01(\x0b\x32&.filesystem.WatchDirResponse.KeepAliveH\x00R\tkeepalive\x1a\x0c\n\nStartEvent\x1a\x0b\n\tKeepAliveB\x07\n\x05\x65vent\"H\n\x14\x43reateWatcherRequest\x12\x12\n\x04path\x18\x01 \x01(\tR\x04path\x12\x1c\n\trecursive\x18\x02 \x01(\x08R\trecursive\"6\n\x15\x43reateWatcherResponse\x12\x1d\n\nwatcher_id\x18\x01 \x01(\tR\twatcherId\"8\n\x17GetWatcherEventsRequest\x12\x1d\n\nwatcher_id\x18\x01 \x01(\tR\twatcherId\"O\n\x18GetWatcherEventsResponse\x12\x33\n\x06\x65vents\x18\x01 \x03(\x0b\x32\x1b.filesystem.FilesystemEventR\x06\x65vents\"5\n\x14RemoveWatcherRequest\x12\x1d\n\nwatcher_id\x18\x01 \x01(\tR\twatcherId\"\x17\n\x15RemoveWatcherResponse*R\n\x08\x46ileType\x12\x19\n\x15\x46ILE_TYPE_UNSPECIFIED\x10\x00\x12\x12\n\x0e\x46ILE_TYPE_FILE\x10\x01\x12\x17\n\x13\x46ILE_TYPE_DIRECTORY\x10\x02*\x98\x01\n\tEventType\x12\x1a\n\x16\x45VENT_TYPE_UNSPECIFIED\x10\x00\x12\x15\n\x11\x45VENT_TYPE_CREATE\x10\x01\x12\x14\n\x10\x45VENT_TYPE_WRITE\x10\x02\x12\x15\n\x11\x45VENT_TYPE_REMOVE\x10\x03\x12\x15\n\x11\x45VENT_TYPE_RENAME\x10\x04\x12\x14\n\x10\x45VENT_TYPE_CHMOD\x10\x05\x32\x9f\x05\n\nFilesystem\x12\x39\n\x04Stat\x12\x17.filesystem.StatRequest\x1a\x18.filesystem.StatResponse\x12\x42\n\x07MakeDir\x12\x1a.filesystem.MakeDirRequest\x1a\x1b.filesystem.MakeDirResponse\x12\x39\n\x04Move\x12\x17.filesystem.MoveRequest\x1a\x18.filesystem.MoveResponse\x12\x42\n\x07ListDir\x12\x1a.filesystem.ListDirRequest\x1a\x1b.filesystem.ListDirResponse\x12?\n\x06Remove\x12\x19.filesystem.RemoveRequest\x1a\x1a.filesystem.RemoveResponse\x12G\n\x08WatchDir\x12\x1b.filesystem.WatchDirRequest\x1a\x1c.filesystem.WatchDirResponse0\x01\x12T\n\rCreateWatcher\x12 .filesystem.CreateWatcherRequest\x1a!.filesystem.CreateWatcherResponse\x12]\n\x10GetWatcherEvents\x12#.filesystem.GetWatcherEventsRequest\x1a$.filesystem.GetWatcherEventsResponse\x12T\n\rRemoveWatcher\x12 .filesystem.RemoveWatcherRequest\x1a!.filesystem.RemoveWatcherResponseBi\n\x0e\x63om.filesystemB\x0f\x46ilesystemProtoP\x01\xa2\x02\x03\x46XX\xaa\x02\nFilesystem\xca\x02\nFilesystem\xe2\x02\x16\x46ilesystem\\GPBMetadata\xea\x02\nFilesystemb\x06proto3')_globals = globals()_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals)@@ -23,10 +23,12 @@ _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'filesystem.filesystem_pb2',if not _descriptor._USE_C_DESCRIPTORS:_globals['DESCRIPTOR']._loaded_options = None_globals['DESCRIPTOR']._serialized_options = b'\n\016com.filesystemB\017FilesystemProtoP\001\242\002\003FXX\252\002\nFilesystem\312\002\nFilesystem\342\002\026Filesystem\\GPBMetadata\352\002\nFilesystem'- _globals['_FILETYPE']._serialized_start=1690- _globals['_FILETYPE']._serialized_end=1772- _globals['_EVENTTYPE']._serialized_start=1775- _globals['_EVENTTYPE']._serialized_end=1927+ _globals['_ENTRYINFO_METADATAENTRY']._loaded_options = None+ _globals['_ENTRYINFO_METADATAENTRY']._serialized_options = b'8\001'+ _globals['_FILETYPE']._serialized_start=1816+ _globals['_FILETYPE']._serialized_end=1898+ _globals['_EVENTTYPE']._serialized_start=1901+ _globals['_EVENTTYPE']._serialized_end=2053_globals['_MOVEREQUEST']._serialized_start=76_globals['_MOVEREQUEST']._serialized_end=147_globals['_MOVERESPONSE']._serialized_start=149@@ -44,33 +46,35 @@ if not _descriptor._USE_C_DESCRIPTORS:_globals['_STATRESPONSE']._serialized_start=402_globals['_STATRESPONSE']._serialized_end=461_globals['_ENTRYINFO']._serialized_start=464- _globals['_ENTRYINFO']._serialized_end=803- _globals['_LISTDIRREQUEST']._serialized_start=805- _globals['_LISTDIRREQUEST']._serialized_end=863- _globals['_LISTDIRRESPONSE']._serialized_start=865- _globals['_LISTDIRRESPONSE']._serialized_end=931- _globals['_WATCHDIRREQUEST']._serialized_start=933- _globals['_WATCHDIRREQUEST']._serialized_end=1000- _globals['_FILESYSTEMEVENT']._serialized_start=1002- _globals['_FILESYSTEMEVENT']._serialized_end=1082- _globals['_WATCHDIRRESPONSE']._serialized_start=1085- _globals['_WATCHDIRRESPONSE']._serialized_end=1339- _globals['_WATCHDIRRESPONSE_STARTEVENT']._serialized_start=1305- _globals['_WATCHDIRRESPONSE_STARTEVENT']._serialized_end=1317- _globals['_WATCHDIRRESPONSE_KEEPALIVE']._serialized_start=1319- _globals['_WATCHDIRRESPONSE_KEEPALIVE']._serialized_end=1330- _globals['_CREATEWATCHERREQUEST']._serialized_start=1341- _globals['_CREATEWATCHERREQUEST']._serialized_end=1413- _globals['_CREATEWATCHERRESPONSE']._serialized_start=1415- _globals['_CREATEWATCHERRESPONSE']._serialized_end=1469- _globals['_GETWATCHEREVENTSREQUEST']._serialized_start=1471- _globals['_GETWATCHEREVENTSREQUEST']._serialized_end=1527- _globals['_GETWATCHEREVENTSRESPONSE']._serialized_start=1529- _globals['_GETWATCHEREVENTSRESPONSE']._serialized_end=1608- _globals['_REMOVEWATCHERREQUEST']._serialized_start=1610- _globals['_REMOVEWATCHERREQUEST']._serialized_end=1663- _globals['_REMOVEWATCHERRESPONSE']._serialized_start=1665- _globals['_REMOVEWATCHERRESPONSE']._serialized_end=1688- _globals['_FILESYSTEM']._serialized_start=1930- _globals['_FILESYSTEM']._serialized_end=2601+ _globals['_ENTRYINFO']._serialized_end=929+ _globals['_ENTRYINFO_METADATAENTRY']._serialized_start=851+ _globals['_ENTRYINFO_METADATAENTRY']._serialized_end=910+ _globals['_LISTDIRREQUEST']._serialized_start=931+ _globals['_LISTDIRREQUEST']._serialized_end=989+ _globals['_LISTDIRRESPONSE']._serialized_start=991+ _globals['_LISTDIRRESPONSE']._serialized_end=1057+ _globals['_WATCHDIRREQUEST']._serialized_start=1059+ _globals['_WATCHDIRREQUEST']._serialized_end=1126+ _globals['_FILESYSTEMEVENT']._serialized_start=1128+ _globals['_FILESYSTEMEVENT']._serialized_end=1208+ _globals['_WATCHDIRRESPONSE']._serialized_start=1211+ _globals['_WATCHDIRRESPONSE']._serialized_end=1465+ _globals['_WATCHDIRRESPONSE_STARTEVENT']._serialized_start=1431+ _globals['_WATCHDIRRESPONSE_STARTEVENT']._serialized_end=1443+ _globals['_WATCHDIRRESPONSE_KEEPALIVE']._serialized_start=1445+ _globals['_WATCHDIRRESPONSE_KEEPALIVE']._serialized_end=1456+ _globals['_CREATEWATCHERREQUEST']._serialized_start=1467+ _globals['_CREATEWATCHERREQUEST']._serialized_end=1539+ _globals['_CREATEWATCHERRESPONSE']._serialized_start=1541+ _globals['_CREATEWATCHERRESPONSE']._serialized_end=1595+ _globals['_GETWATCHEREVENTSREQUEST']._serialized_start=1597+ _globals['_GETWATCHEREVENTSREQUEST']._serialized_end=1653+ _globals['_GETWATCHEREVENTSRESPONSE']._serialized_start=1655+ _globals['_GETWATCHEREVENTSRESPONSE']._serialized_end=1734+ _globals['_REMOVEWATCHERREQUEST']._serialized_start=1736+ _globals['_REMOVEWATCHERREQUEST']._serialized_end=1789+ _globals['_REMOVEWATCHERRESPONSE']._serialized_start=1791+ _globals['_REMOVEWATCHERRESPONSE']._serialized_end=1814+ _globals['_FILESYSTEM']._serialized_start=2056+ _globals['_FILESYSTEM']._serialized_end=2727# @@protoc_insertion_point(module_scope)diff --git a/packages/python-sdk/e2b/envd/filesystem/filesystem_pb2.pyi b/packages/python-sdk/e2b/envd/filesystem/filesystem_pb2.pyiindex 477097952..c63dd9ed0 100644--- a/packages/python-sdk/e2b/envd/filesystem/filesystem_pb2.pyi+++ b/packages/python-sdk/e2b/envd/filesystem/filesystem_pb2.pyi@@ -100,7 +100,18 @@ class EntryInfo(_message.Message):"group","modified_time","symlink_target",+ "metadata",)+ class MetadataEntry(_message.Message):+ __slots__ = ("key", "value")+ KEY_FIELD_NUMBER: _ClassVar[int]+ VALUE_FIELD_NUMBER: _ClassVar[int]+ key: str+ value: str+ def __init__(+ self, key: _Optional[str] = ..., value: _Optional[str] = ...+ ) -> None: ...+NAME_FIELD_NUMBER: _ClassVar[int]TYPE_FIELD_NUMBER: _ClassVar[int]PATH_FIELD_NUMBER: _ClassVar[int]@@ -111,6 +122,7 @@ class EntryInfo(_message.Message):GROUP_FIELD_NUMBER: _ClassVar[int]MODIFIED_TIME_FIELD_NUMBER: _ClassVar[int]SYMLINK_TARGET_FIELD_NUMBER: _ClassVar[int]+ METADATA_FIELD_NUMBER: _ClassVar[int]name: strtype: FileTypepath: str@@ -121,6 +133,7 @@ class EntryInfo(_message.Message):group: strmodified_time: _timestamp_pb2.Timestampsymlink_target: str+ metadata: _containers.ScalarMap[str, str]def __init__(self,name: _Optional[str] = ...,@@ -133,6 +146,7 @@ class EntryInfo(_message.Message):group: _Optional[str] = ...,modified_time: _Optional[_Union[_timestamp_pb2.Timestamp, _Mapping]] = ...,symlink_target: _Optional[str] = ...,+ metadata: _Optional[_Mapping[str, str]] = ...,) -> None: ...class ListDirRequest(_message.Message):diff --git a/packages/python-sdk/e2b/envd/versions.py b/packages/python-sdk/e2b/envd/versions.pyindex 94e9e238c..485f9889d 100644--- a/packages/python-sdk/e2b/envd/versions.py+++ b/packages/python-sdk/e2b/envd/versions.py@@ -6,3 +6,4 @@ ENVD_COMMANDS_STDIN = Version("0.3.0")ENVD_DEFAULT_USER = Version("0.4.0")ENVD_ENVD_CLOSE = Version("0.5.2")ENVD_OCTET_STREAM_UPLOAD = Version("0.5.7")+ENVD_FILE_METADATA = Version("0.6.2")diff --git a/packages/python-sdk/e2b/sandbox/filesystem/filesystem.py b/packages/python-sdk/e2b/sandbox/filesystem/filesystem.pyindex 1cee7327a..83fff10d2 100644--- a/packages/python-sdk/e2b/sandbox/filesystem/filesystem.py+++ b/packages/python-sdk/e2b/sandbox/filesystem/filesystem.py@@ -1,9 +1,10 @@import gzip-from dataclasses import dataclass+import re+from dataclasses import dataclass, fieldfrom datetime import datetimefrom enum import Enumfrom io import IOBase, TextIOBase-from typing import IO, Optional, Union, TypedDict+from typing import IO, Dict, Optional, Union, TypedDictfrom e2b.envd.filesystem import filesystem_pb2from e2b.exceptions import InvalidArgumentException@@ -49,6 +50,23 @@ class WriteInfo:"""Path to the filesystem object."""+ metadata: Optional[Dict[str, str]] = field(default=None, kw_only=True)+ """+ User-defined metadata stored on the file as `user.e2b.*` extended+ attributes. On writes this reflects the metadata supplied on upload; on+ reads (`get_info`, `list`, `rename`) it reflects any `user.e2b.*` xattr on+ the file, including ones set out-of-band. `None` when none is set.+ """++ @classmethod+ def from_dict(cls, payload: Dict) -> "WriteInfo":+ """Build a `WriteInfo` from a `/files` upload response entry."""+ return cls(+ name=payload["name"],+ type=payload.get("type"),+ path=payload["path"],+ metadata=map_metadata(payload.get("metadata")),+ )@dataclass@@ -97,6 +115,18 @@ class WriteEntry(TypedDict):data: Union[str, bytes, IO]+def _to_httpx_file(file_path: str, file_data: Union[str, bytes, IO]):+ """Build an httpx multipart `("file", (name, data))` tuple for the upload."""+ if isinstance(file_data, (str, bytes)):+ return ("file", (file_path, file_data))+ elif isinstance(file_data, TextIOBase):+ return ("file", (file_path, file_data.read()))+ elif isinstance(file_data, IOBase):+ return ("file", (file_path, file_data))+ else:+ raise InvalidArgumentException(f"Unsupported data type for file {file_path}")++def to_upload_body(data: Union[str, bytes, IO],use_gzip: bool = False,@@ -114,3 +144,44 @@ def to_upload_body(raise InvalidArgumentException(f"Unsupported data type: {type(data)}")return gzip.compress(raw) if use_gzip else raw+++METADATA_HEADER_PREFIX = "X-Metadata-"++# Metadata keys travel as `X-Metadata-<key>` HTTP header names, so they must be+# valid header tokens (RFC 7230); values travel as header values, restricted to+# printable US-ASCII.+_METADATA_KEY_REGEX = re.compile(r"^[A-Za-z0-9!#$%&'*+\-.^_`|~]+$")+_METADATA_VALUE_REGEX = re.compile(r"^[\x20-\x7e]*$")+++def validate_metadata(metadata: Optional[Dict[str, str]]) -> None:+ """Validate metadata keys/values before they are sent as upload headers."""+ if not metadata:+ return+ for key, value in metadata.items():+ if not _METADATA_KEY_REGEX.match(key):+ raise InvalidArgumentException(+ f"Invalid metadata key {key!r}: keys must be non-empty and use only "+ "HTTP token characters (letters, digits and !#$%&'*+-.^_`|~)."+ )+ if not _METADATA_VALUE_REGEX.match(value):+ raise InvalidArgumentException(+ f"Invalid metadata value for key {key!r}: values must be printable US-ASCII."+ )+++def metadata_to_headers(+ metadata: Optional[Dict[str, str]],+) -> Dict[str, str]:+ """Translate user metadata into the `X-Metadata-*` upload headers envd reads."""+ if not metadata:+ return {}+ return {f"{METADATA_HEADER_PREFIX}{key}": value for key, value in metadata.items()}+++def map_metadata(metadata) -> Optional[Dict[str, str]]:+ """Normalize a proto/HTTP metadata map: drop empties and return a plain dict or None."""+ if not metadata:+ return None+ return dict(metadata)diff --git a/packages/python-sdk/e2b/sandbox_async/filesystem/filesystem.py b/packages/python-sdk/e2b/sandbox_async/filesystem/filesystem.pyindex 77b11f64c..9362b2fda 100644--- a/packages/python-sdk/e2b/sandbox_async/filesystem/filesystem.py+++ b/packages/python-sdk/e2b/sandbox_async/filesystem/filesystem.py@@ -1,6 +1,5 @@import asyncio-from io import IOBase, TextIOBase-from typing import IO, AsyncIterator, List, Literal, Optional, Union, overload+from typing import IO, AsyncIterator, Dict, List, Literal, Optional, Union, overloadimport httpcore@@ -20,6 +19,7 @@ from e2b.envd.filesystem import filesystem_connect, filesystem_pb2from e2b.envd.rpc import authentication_header, handle_rpc_exceptionfrom e2b.envd.versions import (ENVD_DEFAULT_USER,+ ENVD_FILE_METADATA,ENVD_OCTET_STREAM_UPLOAD,ENVD_VERSION_RECURSIVE_WATCH,)@@ -33,8 +33,12 @@ from e2b.sandbox.filesystem.filesystem import (EntryInfo,WriteEntry,WriteInfo,+ _to_httpx_file,map_file_type,+ map_metadata,+ metadata_to_headers,to_upload_body,+ validate_metadata,)from e2b.sandbox.filesystem.watch_handle import FilesystemEventfrom e2b.sandbox_async.filesystem.watch_handle import AsyncWatchHandle@@ -198,6 +202,7 @@ class Filesystem:request_timeout: Optional[float] = None,gzip: bool = False,use_octet_stream: bool = False,+ metadata: Optional[Dict[str, str]] = None,) -> WriteInfo:"""Write content to a file on the path.@@ -211,6 +216,7 @@ class Filesystem::param request_timeout: Timeout for the request in **seconds**:param gzip: Use gzip compression for the request:param use_octet_stream: Upload using `application/octet-stream` instead of `multipart/form-data`. Defaults to `False`. Requires envd 0.5.7 or later — when not supported, the upload falls back to `multipart/form-data`.+ :param metadata: User-defined metadata to persist on the uploaded file as extended attributes. Keys are lowercased by the sandbox; invalid keys or values raise an `InvalidArgumentException`. Requires envd 0.6.2 or later.:return: Information about the written file"""@@ -220,6 +226,7 @@ class Filesystem:request_timeout=request_timeout,gzip=gzip,use_octet_stream=use_octet_stream,+ metadata=metadata,)if len(result) != 1:@@ -234,6 +241,7 @@ class Filesystem:request_timeout: Optional[float] = None,gzip: bool = False,use_octet_stream: bool = False,+ metadata: Optional[Dict[str, str]] = None,) -> List[WriteInfo]:"""Writes multiple files.@@ -248,6 +256,7 @@ class Filesystem::param request_timeout: Timeout for the request:param gzip: Use gzip compression for the request:param use_octet_stream: Upload using `application/octet-stream` instead of `multipart/form-data`. Defaults to `False`. Requires envd 0.5.7 or later — when not supported, the upload falls back to `multipart/form-data`.+ :param metadata: User-defined metadata to persist on each uploaded file as extended attributes; the same map is applied to every file. Keys are lowercased by the sandbox; invalid keys or values raise an `InvalidArgumentException`. Requires envd 0.6.2 or later.:return: Information about the written files"""username = user@@ -257,9 +266,18 @@ class Filesystem:if len(files) == 0:return []+ validate_metadata(metadata)++ if metadata and self._envd_version < ENVD_FILE_METADATA:+ raise TemplateException("File metadata requires envd 0.6.2 or later.")+supports_octet_stream = self._envd_version >= ENVD_OCTET_STREAM_UPLOADuse_octet_stream = use_octet_stream and supports_octet_stream+ # Metadata is sent as request-scoped X-Metadata-* headers, so the same+ # metadata is applied to every file in a multi-file upload.+ extra_headers = metadata_to_headers(metadata)+results: List[WriteInfo] = []if use_octet_stream:@@ -267,19 +285,17 @@ class Filesystem:async def _upload_file(file):file_path, file_data = file["path"], file["data"]- content = to_upload_body(file_data, gzip)-params = {"path": file_path}if username:params["username"] = username- headers = {"Content-Type": "application/octet-stream"}+ headers = {"Content-Type": "application/octet-stream", **extra_headers}if gzip:headers["Content-Encoding"] = "gzip"r = await self._envd_api.post(ENVD_API_FILES_ROUTE,- content=content,+ content=to_upload_body(file_data, gzip),headers=headers,params=params,timeout=self._connection_config.get_request_timeout(@@ -298,7 +314,7 @@ class Filesystem:"Expected to receive information about written file")- return [WriteInfo(**f) for f in write_result]+ return [WriteInfo.from_dict(f) for f in write_result]upload_results = await asyncio.gather(*[_upload_file(file) for file in files]@@ -312,19 +328,7 @@ class Filesystem:if len(files) == 1:params["path"] = files[0]["path"]- httpx_files = []- for file in files:- file_path, file_data = file["path"], file["data"]- if isinstance(file_data, (str, bytes)):- httpx_files.append(("file", (file_path, file_data)))- elif isinstance(file_data, TextIOBase):- httpx_files.append(("file", (file_path, file_data.read())))- elif isinstance(file_data, IOBase):- httpx_files.append(("file", (file_path, file_data)))- else:- raise InvalidArgumentException(- f"Unsupported data type for file {file_path}"- )+ httpx_files = [_to_httpx_file(file["path"], file["data"]) for file in files]if len(httpx_files) == 0:return []@@ -333,6 +337,7 @@ class Filesystem:ENVD_API_FILES_ROUTE,files=httpx_files,params=params,+ headers=extra_headers,timeout=self._connection_config.get_request_timeout(request_timeout),)@@ -347,7 +352,7 @@ class Filesystem:"Expected to receive information about written file")- results.extend([WriteInfo(**f) for f in write_result])+ results.extend([WriteInfo.from_dict(f) for f in write_result])return results@@ -402,6 +407,7 @@ class Filesystem:if entry.HasField("symlink_target")else None),+ metadata=map_metadata(entry.metadata),))@@ -480,6 +486,7 @@ class Filesystem:if r.entry.HasField("symlink_target")else None),+ metadata=map_metadata(r.entry.metadata),)except Exception as e:raise _handle_filesystem_rpc_exception(e)@@ -553,6 +560,7 @@ class Filesystem:if r.entry.HasField("symlink_target")else None),+ metadata=map_metadata(r.entry.metadata),)except Exception as e:raise _handle_filesystem_rpc_exception(e)diff --git a/packages/python-sdk/e2b/sandbox_sync/filesystem/filesystem.py b/packages/python-sdk/e2b/sandbox_sync/filesystem/filesystem.pyindex a41046b30..71068d511 100644--- a/packages/python-sdk/e2b/sandbox_sync/filesystem/filesystem.py+++ b/packages/python-sdk/e2b/sandbox_sync/filesystem/filesystem.py@@ -1,5 +1,4 @@-from io import IOBase, TextIOBase-from typing import IO, Iterator, List, Literal, Optional, Union, overload+from typing import IO, Dict, Iterator, List, Literal, Optional, Union, overloadimport httpcoreimport httpx@@ -20,6 +19,7 @@ from e2b.envd.filesystem import filesystem_connect, filesystem_pb2from e2b.envd.rpc import authentication_header, handle_rpc_exceptionfrom e2b.envd.versions import (ENVD_DEFAULT_USER,+ ENVD_FILE_METADATA,ENVD_OCTET_STREAM_UPLOAD,ENVD_VERSION_RECURSIVE_WATCH,)@@ -33,8 +33,12 @@ from e2b.sandbox.filesystem.filesystem import (EntryInfo,WriteEntry,WriteInfo,+ _to_httpx_file,map_file_type,+ map_metadata,+ metadata_to_headers,to_upload_body,+ validate_metadata,)from e2b.sandbox_sync.filesystem.watch_handle import WatchHandle@@ -196,6 +200,7 @@ class Filesystem:request_timeout: Optional[float] = None,gzip: bool = False,use_octet_stream: bool = False,+ metadata: Optional[Dict[str, str]] = None,) -> WriteInfo:"""Write content to a file on the path.@@ -209,6 +214,7 @@ class Filesystem::param request_timeout: Timeout for the request in **seconds**:param gzip: Use gzip compression for the request:param use_octet_stream: Upload using `application/octet-stream` instead of `multipart/form-data`. Defaults to `False`. Requires envd 0.5.7 or later — when not supported, the upload falls back to `multipart/form-data`.+ :param metadata: User-defined metadata to persist on the uploaded file as extended attributes. Keys are lowercased by the sandbox; invalid keys or values raise an `InvalidArgumentException`. Requires envd 0.6.2 or later.:return: Information about the written file"""@@ -218,6 +224,7 @@ class Filesystem:request_timeout=request_timeout,gzip=gzip,use_octet_stream=use_octet_stream,+ metadata=metadata,)if len(result) != 1:@@ -232,6 +239,7 @@ class Filesystem:request_timeout: Optional[float] = None,gzip: bool = False,use_octet_stream: bool = False,+ metadata: Optional[Dict[str, str]] = None,) -> List[WriteInfo]:"""Writes multiple files.@@ -246,6 +254,7 @@ class Filesystem::param request_timeout: Timeout for the request:param gzip: Use gzip compression for the request:param use_octet_stream: Upload using `application/octet-stream` instead of `multipart/form-data`. Defaults to `False`. Requires envd 0.5.7 or later — when not supported, the upload falls back to `multipart/form-data`.+ :param metadata: User-defined metadata to persist on each uploaded file as extended attributes; the same map is applied to every file. Keys are lowercased by the sandbox; invalid keys or values raise an `InvalidArgumentException`. Requires envd 0.6.2 or later.:return: Information about the written files"""username = user@@ -255,28 +264,35 @@ class Filesystem:if len(files) == 0:return []+ validate_metadata(metadata)++ if metadata and self._envd_version < ENVD_FILE_METADATA:+ raise TemplateException("File metadata requires envd 0.6.2 or later.")+supports_octet_stream = self._envd_version >= ENVD_OCTET_STREAM_UPLOADuse_octet_stream = use_octet_stream and supports_octet_stream+ # Metadata is sent as request-scoped X-Metadata-* headers, so the same+ # metadata is applied to every file in a multi-file upload.+ extra_headers = metadata_to_headers(metadata)+results: List[WriteInfo] = []if use_octet_stream:for file in files:file_path, file_data = file["path"], file["data"]- content = to_upload_body(file_data, gzip)-params = {"path": file_path}if username:params["username"] = username- headers = {"Content-Type": "application/octet-stream"}+ headers = {"Content-Type": "application/octet-stream", **extra_headers}if gzip:headers["Content-Encoding"] = "gzip"r = self._envd_api.post(ENVD_API_FILES_ROUTE,- content=content,+ content=to_upload_body(file_data, gzip),headers=headers,params=params,timeout=self._connection_config.get_request_timeout(@@ -295,7 +311,7 @@ class Filesystem:"Expected to receive information about written file")- results.extend([WriteInfo(**f) for f in write_result])+ results.extend([WriteInfo.from_dict(f) for f in write_result])else:params = {}if username:@@ -303,19 +319,7 @@ class Filesystem:if len(files) == 1:params["path"] = files[0]["path"]- httpx_files = []- for file in files:- file_path, file_data = file["path"], file["data"]- if isinstance(file_data, (str, bytes)):- httpx_files.append(("file", (file_path, file_data)))- elif isinstance(file_data, TextIOBase):- httpx_files.append(("file", (file_path, file_data.read())))- elif isinstance(file_data, IOBase):- httpx_files.append(("file", (file_path, file_data)))- else:- raise InvalidArgumentException(- f"Unsupported data type for file {file_path}"- )+ httpx_files = [_to_httpx_file(file["path"], file["data"]) for file in files]if len(httpx_files) == 0:return []@@ -324,6 +328,7 @@ class Filesystem:ENVD_API_FILES_ROUTE,files=httpx_files,params=params,+ headers=extra_headers,timeout=self._connection_config.get_request_timeout(request_timeout),)@@ -338,7 +343,7 @@ class Filesystem:"Expected to receive information about written file")- results.extend([WriteInfo(**f) for f in write_result])+ results.extend([WriteInfo.from_dict(f) for f in write_result])return results@@ -393,6 +398,7 @@ class Filesystem:if entry.HasField("symlink_target")else None),+ metadata=map_metadata(entry.metadata),))@@ -471,6 +477,7 @@ class Filesystem:if r.entry.HasField("symlink_target")else None),+ metadata=map_metadata(r.entry.metadata),)except Exception as e:raise _handle_filesystem_rpc_exception(e)@@ -544,6 +551,7 @@ class Filesystem:if r.entry.HasField("symlink_target")else None),+ metadata=map_metadata(r.entry.metadata),)except Exception as e:raise _handle_filesystem_rpc_exception(e)diff --git a/packages/python-sdk/tests/async/sandbox_async/files/test_metadata.py b/packages/python-sdk/tests/async/sandbox_async/files/test_metadata.pynew file mode 100644index 000000000..11230c3de--- /dev/null+++ b/packages/python-sdk/tests/async/sandbox_async/files/test_metadata.py@@ -0,0 +1,166 @@+import pytest++from e2b import AsyncSandbox+from e2b.exceptions import InvalidArgumentException+from e2b.sandbox.filesystem.filesystem import WriteEntry+++async def test_write_file_with_metadata(async_sandbox: AsyncSandbox, debug):+ filename = "test_metadata.txt"+ content = "This is a test file with metadata."+ metadata = {"author": "mish", "purpose": "upload"}++ info = await async_sandbox.files.write(filename, content, metadata=metadata)+ assert info.metadata == metadata++ # Metadata is persisted and surfaced on subsequent reads.+ stat = await async_sandbox.files.get_info(filename)+ assert stat.metadata == metadata++ if debug:+ await async_sandbox.files.remove(filename)+++async def test_write_file_with_metadata_octet_stream(+ async_sandbox: AsyncSandbox, debug+):+ filename = "test_metadata_octet.txt"+ content = "This is a test file with metadata."+ metadata = {"author": "mish", "purpose": "upload"}++ info = await async_sandbox.files.write(+ filename, content, metadata=metadata, use_octet_stream=True+ )+ assert info.metadata == metadata++ stat = await async_sandbox.files.get_info(filename)+ assert stat.metadata == metadata++ if debug:+ await async_sandbox.files.remove(filename)+++async def test_write_file_without_metadata(async_sandbox: AsyncSandbox, debug):+ filename = "test_no_metadata.txt"++ info = await async_sandbox.files.write(filename, "no metadata here")+ assert info.metadata is None++ stat = await async_sandbox.files.get_info(filename)+ assert stat.metadata is None++ if debug:+ await async_sandbox.files.remove(filename)+++async def test_write_files_applies_metadata_to_every_file(+ async_sandbox: AsyncSandbox, debug+):+ # The same metadata is applied to every file in the upload.+ metadata = {"source": "test-suite"}+ files = [+ WriteEntry(path="metadata_multi_1.txt", data="File 1"),+ WriteEntry(path="metadata_multi_2.txt", data="File 2"),+ ]++ infos = await async_sandbox.files.write_files(files, metadata=metadata)+ assert len(infos) == len(files)++ for info in infos:+ assert info.metadata == metadata+ stat = await async_sandbox.files.get_info(info.path)+ assert stat.metadata == metadata++ if debug:+ for file in files:+ await async_sandbox.files.remove(file["path"])+++async def test_metadata_surfaced_when_listing(async_sandbox: AsyncSandbox, debug):+ dirname = "metadata_list_dir"+ filename = "listed.txt"+ metadata = {"tag": "listed"}++ await async_sandbox.files.make_dir(dirname)+ await async_sandbox.files.write(+ f"{dirname}/{filename}", "content", metadata=metadata+ )++ entries = await async_sandbox.files.list(dirname)+ entry = next((e for e in entries if e.name == filename), None)+ assert entry is not None+ assert entry.metadata == metadata++ if debug:+ await async_sandbox.files.remove(dirname)+++async def test_metadata_surfaced_after_rename(async_sandbox: AsyncSandbox, debug):+ old_path = "metadata_rename_old.txt"+ new_path = "metadata_rename_new.txt"+ metadata = {"stage": "renamed"}++ await async_sandbox.files.write(old_path, "content", metadata=metadata)+ info = await async_sandbox.files.rename(old_path, new_path)+ assert info.metadata == metadata++ if debug:+ await async_sandbox.files.remove(new_path)+++async def test_overwriting_clears_stale_metadata(async_sandbox: AsyncSandbox, debug):+ filename = "metadata_overwrite.txt"++ await async_sandbox.files.write(filename, "first", metadata={"author": "mish"})++ # Overwriting without metadata removes the previously stored metadata.+ info = await async_sandbox.files.write(filename, "second")+ assert info.metadata is None++ stat = await async_sandbox.files.get_info(filename)+ assert stat.metadata is None++ if debug:+ await async_sandbox.files.remove(filename)+++async def test_metadata_set_via_xattrs_surfaced_in_get_info(+ async_sandbox: AsyncSandbox, debug+):+ filename = "metadata_xattr.txt"+ await async_sandbox.files.write(filename, "content")++ cmd = await async_sandbox.commands.run(f"realpath {filename}")+ file_path = cmd.stdout.strip()++ # Set an xattr directly in the `user.e2b.` namespace (out-of-band, not via+ # the SDK upload); it should surface as metadata (with the namespace prefix+ # stripped) when reading the file info.+ await async_sandbox.commands.run(+ f"python3 -c \"import os; os.setxattr('{file_path}', 'user.e2b.author', b'mish')\""+ )++ info = await async_sandbox.files.get_info(filename)+ assert info.metadata == {"author": "mish"}++ if debug:+ await async_sandbox.files.remove(filename)+++async def test_write_rejects_invalid_metadata(async_sandbox: AsyncSandbox):+ filename = "invalid_metadata.txt"++ # Key with a space is not a valid HTTP header token.+ with pytest.raises(InvalidArgumentException):+ await async_sandbox.files.write(filename, "x", metadata={"bad key": "value"})++ # Empty key.+ with pytest.raises(InvalidArgumentException):+ await async_sandbox.files.write(filename, "x", metadata={"": "value"})++ # Value with a non-printable / non-ASCII character.+ with pytest.raises(InvalidArgumentException):+ await async_sandbox.files.write(filename, "x", metadata={"good": "bad\nvalue"})++ # The file must not have been created by a rejected write.+ assert not await async_sandbox.files.exists(filename)diff --git a/packages/python-sdk/tests/sync/sandbox_sync/files/test_metadata.py b/packages/python-sdk/tests/sync/sandbox_sync/files/test_metadata.pynew file mode 100644index 000000000..4a393bcc7--- /dev/null+++ b/packages/python-sdk/tests/sync/sandbox_sync/files/test_metadata.py@@ -0,0 +1,157 @@+import pytest++from e2b.exceptions import InvalidArgumentException+++def test_write_file_with_metadata(sandbox, debug):+ filename = "test_metadata.txt"+ content = "This is a test file with metadata."+ metadata = {"author": "mish", "purpose": "upload"}++ info = sandbox.files.write(filename, content, metadata=metadata)+ assert info.metadata == metadata++ # Metadata is persisted and surfaced on subsequent reads.+ stat = sandbox.files.get_info(filename)+ assert stat.metadata == metadata++ if debug:+ sandbox.files.remove(filename)+++def test_write_file_with_metadata_octet_stream(sandbox, debug):+ filename = "test_metadata_octet.txt"+ content = "This is a test file with metadata."+ metadata = {"author": "mish", "purpose": "upload"}++ info = sandbox.files.write(+ filename, content, metadata=metadata, use_octet_stream=True+ )+ assert info.metadata == metadata++ stat = sandbox.files.get_info(filename)+ assert stat.metadata == metadata++ if debug:+ sandbox.files.remove(filename)+++def test_write_file_without_metadata(sandbox, debug):+ filename = "test_no_metadata.txt"++ info = sandbox.files.write(filename, "no metadata here")+ assert info.metadata is None++ stat = sandbox.files.get_info(filename)+ assert stat.metadata is None++ if debug:+ sandbox.files.remove(filename)+++def test_write_files_applies_metadata_to_every_file(sandbox, debug):+ from e2b.sandbox.filesystem.filesystem import WriteEntry++ # The same metadata is applied to every file in the upload.+ metadata = {"source": "test-suite"}+ files = [+ WriteEntry(path="metadata_multi_1.txt", data="File 1"),+ WriteEntry(path="metadata_multi_2.txt", data="File 2"),+ ]++ infos = sandbox.files.write_files(files, metadata=metadata)+ assert len(infos) == len(files)++ for info in infos:+ assert info.metadata == metadata+ stat = sandbox.files.get_info(info.path)+ assert stat.metadata == metadata++ if debug:+ for file in files:+ sandbox.files.remove(file["path"])+++def test_metadata_surfaced_when_listing(sandbox, debug):+ dirname = "metadata_list_dir"+ filename = "listed.txt"+ metadata = {"tag": "listed"}++ sandbox.files.make_dir(dirname)+ sandbox.files.write(f"{dirname}/{filename}", "content", metadata=metadata)++ entries = sandbox.files.list(dirname)+ entry = next((e for e in entries if e.name == filename), None)+ assert entry is not None+ assert entry.metadata == metadata++ if debug:+ sandbox.files.remove(dirname)+++def test_metadata_surfaced_after_rename(sandbox, debug):+ old_path = "metadata_rename_old.txt"+ new_path = "metadata_rename_new.txt"+ metadata = {"stage": "renamed"}++ sandbox.files.write(old_path, "content", metadata=metadata)+ info = sandbox.files.rename(old_path, new_path)+ assert info.metadata == metadata++ if debug:+ sandbox.files.remove(new_path)+++def test_overwriting_clears_stale_metadata(sandbox, debug):+ filename = "metadata_overwrite.txt"++ sandbox.files.write(filename, "first", metadata={"author": "mish"})++ # Overwriting without metadata removes the previously stored metadata.+ info = sandbox.files.write(filename, "second")+ assert info.metadata is None++ stat = sandbox.files.get_info(filename)+ assert stat.metadata is None++ if debug:+ sandbox.files.remove(filename)+++def test_metadata_set_via_xattrs_surfaced_in_get_info(sandbox, debug):+ filename = "metadata_xattr.txt"+ sandbox.files.write(filename, "content")++ file_path = sandbox.commands.run(f"realpath {filename}").stdout.strip()++ # Set an xattr directly in the `user.e2b.` namespace (out-of-band, not via+ # the SDK upload); it should surface as metadata (with the namespace prefix+ # stripped) when reading the file info.+ sandbox.commands.run(+ f"python3 -c \"import os; os.setxattr('{file_path}', 'user.e2b.author', b'mish')\""+ )++ info = sandbox.files.get_info(filename)+ assert info.metadata == {"author": "mish"}++ if debug:+ sandbox.files.remove(filename)+++def test_write_rejects_invalid_metadata(sandbox):+ filename = "invalid_metadata.txt"++ # Key with a space is not a valid HTTP header token.+ with pytest.raises(InvalidArgumentException):+ sandbox.files.write(filename, "x", metadata={"bad key": "value"})++ # Empty key.+ with pytest.raises(InvalidArgumentException):+ sandbox.files.write(filename, "x", metadata={"": "value"})++ # Value with a non-printable / non-ASCII character.+ with pytest.raises(InvalidArgumentException):+ sandbox.files.write(filename, "x", metadata={"good": "bad\nvalue"})++ # The file must not have been created by a rejected write.+ assert not sandbox.files.exists(filename)diff --git a/spec/envd/envd.yaml b/spec/envd/envd.yamlindex 9649c24a1..6b776a726 100644--- a/spec/envd/envd.yaml+++ b/spec/envd/envd.yaml@@ -1,7 +1,7 @@openapi: 3.0.0info:title: envd- version: 0.1.1+ version: 0.1.3description: API for managing files' content and controlling envdtags:@@ -102,6 +102,27 @@ paths:$ref: '#/components/responses/InternalServerError'post:summary: Upload a file and ensure the parent directories exist. If the file exists, it will be overwritten.+ description: |+ Any request header of the form `X-Metadata-<key>: <value>` is persisted+ as a user-defined extended attribute on the uploaded file. The+ `X-Metadata-` prefix is stripped and the remaining header name is+ lowercased to form the metadata key; the resulting map is returned on+ `EntryInfo` lookups (e.g. `Stat`, `ListDir`).++ Each upload replaces the file's metadata with the keys provided in+ that request: keys previously stored but absent from the new request+ are removed, and an upload that sends no `X-Metadata-*` header clears+ all existing metadata.++ Both keys and values must be printable US-ASCII (bytes `0x20`-`0x7E`)+ and are rejected with HTTP 400 otherwise. Each key is capped at 246+ bytes (the Linux VFS xattr-name limit minus the namespace prefix), and+ the combined size of all metadata on a file (keys plus values, with the+ namespace prefix counted per key) is capped at 4096 bytes to stay within+ the filesystem's per-inode xattr budget. Multiple files in a single+ multipart upload receive the same metadata. If the same+ `X-Metadata-<key>` header is sent more than once, only the first+ value is used.tags: [files]security:- AccessTokenAuth: []@@ -252,6 +273,11 @@ components:description: Type of the fileenum:- file+ metadata:+ type: object+ description: User-defined metadata stored as extended attributes on the file.+ additionalProperties:+ type: stringEnvVars:type: objectdescription: Environment variables to setdiff --git a/spec/envd/filesystem/filesystem.proto b/spec/envd/filesystem/filesystem.protoindex ca9aeb12d..74692721e 100644--- a/spec/envd/filesystem/filesystem.proto+++ b/spec/envd/filesystem/filesystem.proto@@ -62,6 +62,10 @@ message EntryInfo {google.protobuf.Timestamp modified_time = 9;// If the entry is a symlink, this field contains the target of the symlink.optional string symlink_target = 10;+ // User-defined metadata stored as extended attributes (xattrs) on the file.+ // Keys live under the `user.e2b.` xattr namespace; the prefix is stripped here.+ // Plain `user.*` xattrs written by other tooling are not reflected.+ map<string, string> metadata = 11;}enum FileType {