-
Notifications
You must be signed in to change notification settings - Fork 925
feat(sdks): expose user-defined file metadata on sandbox.files #1383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mishushakov
wants to merge
8
commits into
main
Choose a base branch
from
mishushakov/files-metadata-sdk
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
da3fc4d
feat(sdks): expose user-defined file metadata on sandbox.files
mishushakov 9c1fe5b
Merge branch 'main' into mishushakov/files-metadata-sdk
mishushakov a26b9a5
chore(sdks): sync envd spec/proto and bump metadata version gate to 0…
mishushakov effe883
refactor(sdks): address PR review — per-file metadata, WriteInfo.from…
mishushakov 7049af8
refactor(sdks): keep file metadata as a uniform writeFiles option
mishushakov f092251
fix(sdks): raise SandboxError/SandboxException for the metadata versi…
mishushakov 83961d2
fix(sdks): use TemplateError/TemplateException for the metadata versi…
mishushakov 923f2be
Merge branch 'main' into mishushakov/files-metadata-sdk
mishushakov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. Keys and values must be printable US-ASCII and keys are lowercased by the sandbox; the same metadata map is applied to every file in a multi-file upload. Requires envd 0.6.2 or later. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
mishushakov marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| import { assert } from 'vitest' | ||
|
|
||
| import { WriteEntry } from '../../../src/sandbox/filesystem' | ||
| 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; it should surface as | ||
| // metadata (with the namespace prefix stripped) when reading the file info. | ||
| await sandbox.commands.run( | ||
| `setfattr -n user.e2b.author -v mish ${filePath.trim()}` | ||
| ) | ||
|
|
||
| const info = await sandbox.files.getInfo(filename) | ||
| assert.deepEqual(info.metadata, { author: 'mish' }) | ||
|
|
||
| if (isDebug) { | ||
| await sandbox.files.remove(filename) | ||
| } | ||
| } | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 The docstring on
WriteInfo.metadatasays it is "Only populated when metadata was supplied on upload and the sandbox's envd supports it." SinceEntryInfo extends WriteInfoand inherits this doc, it also describesEntryInfo.metadatareturned bygetInfo/list/rename— which is populated from anyuser.e2b.*xattr on the inode, including ones set out-of-band (as the PR's own "metadata set via xattrs is surfaced in getInfo" test demonstrates). Consider dropping the "Only" qualifier or noting that read responses also reflect xattrs set externally. Same wording is duplicated inpackages/python-sdk/e2b/sandbox/filesystem/filesystem.py:52-57.Extended reasoning...
What the docstring says vs. what actually happens
The new
metadatafield onWriteInfois documented as:(packages/js-sdk/src/sandbox/filesystem/index.ts:81-85, mirrored verbatim in packages/python-sdk/e2b/sandbox/filesystem/filesystem.py:52-57.)
The word "Only" makes that statement incorrect because
EntryInfo extends WriteInfo(index.ts:89) and inherits this docstring.EntryInfois returned byFilesystem.getInfo,Filesystem.list, andFilesystem.rename, all of which call into envd RPCs (stat,listDir,move) that surface the protoEntryInfo.metadatafield. The proto comment on filesystem.proto:65-68 is the authoritative description and correctly says the field reflects anyuser.e2b.*xattr on the inode — regardless of whether the SDK set it on upload.Step-by-step proof from the PR's own test
The PR adds packages/js-sdk/tests/sandbox/files/metadata.test.ts:155 (
metadata set via xattrs is surfaced in getInfo):sandbox.files.write(filename, 'content')— nometadataoption, so noX-Metadata-*header is sent. Per the docstring,metadatashould remain undefined.realpath.setfattr -n user.e2b.author -v mish <path>viasandbox.commands.run— this sets auser.e2b.*xattr directly, bypassing the SDK upload entirely.sandbox.files.getInfo(filename)and assertinfo.metadatadeep-equals{ author: 'mish' }.The assertion passes, which means
EntryInfo.metadata(the inherited field) is populated from xattrs that were never "supplied on upload". The same scenario is repeated in the Python test suites (packages/python-sdk/tests/{async,sync}/.../test_metadata.py:test_metadata_set_via_xattrs_surfaced_in_get_info). The docstring's "Only" is therefore factually wrong for theEntryInfouse site.Why this is still only a nit
The refuters are correct that this is purely a documentation cleanup, not a behavior bug — the runtime is right, the OpenAPI
descriptionblock (envd.yaml:104-124) and the proto comment both document the xattr semantics properly, and any developer looking atEntryInfo.metadatain an IDE would see the cross-cutting proto docs alongside the inherited TS/Python comment. So this should not block the PR.Suggested fix
Either drop the "Only" qualifier and mention reads, e.g.:
Apply to both packages/js-sdk/src/sandbox/filesystem/index.ts:78-86 and packages/python-sdk/e2b/sandbox/filesystem/filesystem.py:52-57 so the JS/Python copies stay in sync.