-
Notifications
You must be signed in to change notification settings - Fork 4.4k
feat(craft): outputs artifact classifier #14326
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
nmgarza5
wants to merge
2
commits into
main
Choose a base branch
from
nikg/craft-artifact-classifier
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
2 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
193 changes: 193 additions & 0 deletions
193
backend/onyx/server/features/build/artifact_classifier.py
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,193 @@ | ||
| """Classifies a session's outputs listing into artifacts. | ||
|
|
||
| Pure functions: each visible top-level entry is one artifact, files typed by | ||
| extension with a generic fallback, directories aggregated over their visible | ||
| descendants, and the scaffolded webapp directory recognized. Hidden names (the | ||
| shared workspace visibility rule) never influence the result, so churn in | ||
| node_modules or .next cannot dirty an artifact. Callers adapt the sandbox | ||
| daemon's outputs manifest into ``OutputEntry`` values, and the listing must be | ||
| complete: a truncated manifest reduces to hashes that flap, so skip | ||
| reconciliation instead of reducing one. | ||
| """ | ||
|
|
||
| import mimetypes | ||
| from collections import defaultdict | ||
| from dataclasses import dataclass | ||
| from hashlib import sha256 | ||
| from pathlib import PurePosixPath | ||
|
|
||
| from onyx.db.enums import ArtifactType | ||
| from onyx.server.features.build.configs import is_hidden_workspace_name | ||
| from onyx.server.features.build.sandbox.nextjs_dev import ( | ||
| WEBAPP_OUTPUTS_RELATIVE_PACKAGE_JSON, | ||
| WEBAPP_ROOT_NAME, | ||
| ) | ||
|
|
||
| # Formats a product surface handles (preview, export). .doc and .xls have no | ||
| # such surface and stay generic. | ||
| _EXTENSION_TYPES = { | ||
| ".pptx": ArtifactType.PPTX, | ||
| ".ppt": ArtifactType.PPTX, | ||
| ".docx": ArtifactType.DOCX, | ||
| ".xlsx": ArtifactType.EXCEL, | ||
| ".xlsm": ArtifactType.EXCEL, | ||
| ".pdf": ArtifactType.PDF, | ||
| ".csv": ArtifactType.CSV, | ||
| ".md": ArtifactType.MARKDOWN, | ||
| ".markdown": ArtifactType.MARKDOWN, | ||
| } | ||
|
|
||
| _ARCHIVE_SUFFIXES = {".zip", ".tar", ".gz", ".tgz", ".bz2", ".xz", ".7z", ".rar"} | ||
|
|
||
| # Checked before the mimetypes families: .ts is registered as video/mp2t. | ||
| _CODE_SUFFIXES = { | ||
| ".py", | ||
| ".ts", | ||
| ".tsx", | ||
| ".js", | ||
| ".jsx", | ||
| ".html", | ||
| ".css", | ||
| ".scss", | ||
| ".json", | ||
| ".yaml", | ||
| ".yml", | ||
| ".toml", | ||
| ".sql", | ||
| ".sh", | ||
| ".ipynb", | ||
| ".go", | ||
| ".rs", | ||
| ".java", | ||
| ".c", | ||
| ".cpp", | ||
| ".rb", | ||
| ".php", | ||
| } | ||
|
|
||
| _MEDIA_FAMILY_TYPES = { | ||
| "image": ArtifactType.IMAGE, | ||
| "audio": ArtifactType.AUDIO, | ||
| "video": ArtifactType.VIDEO, | ||
| } | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class OutputEntry: | ||
| """One entry of an outputs listing, path outputs-relative with ``/`` | ||
| separators. ``sha256`` is None for directories and unhashed files.""" | ||
|
|
||
| path: str | ||
| is_directory: bool | ||
| size: int | None = None | ||
| mtime_ns: int | None = None | ||
| sha256: str | None = None | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class DerivedArtifact: | ||
| """One artifact a reconciler should upsert, keyed by ``path``.""" | ||
|
|
||
| path: str | ||
| name: str | ||
| type: ArtifactType | ||
| size_bytes: int | None | ||
| content_hash: str | None | ||
|
|
||
|
|
||
| def classify_file(filename: str) -> ArtifactType: | ||
| suffix = PurePosixPath(filename).suffix.lower() | ||
| if suffix in _EXTENSION_TYPES: | ||
| return _EXTENSION_TYPES[suffix] | ||
| if suffix in _ARCHIVE_SUFFIXES: | ||
| return ArtifactType.ARCHIVE | ||
| if suffix in _CODE_SUFFIXES: | ||
| return ArtifactType.CODE | ||
| mime, _ = mimetypes.guess_type(filename) | ||
| if mime is not None: | ||
| family = mime.split("/", 1)[0] | ||
| if family in _MEDIA_FAMILY_TYPES: | ||
| return _MEDIA_FAMILY_TYPES[family] | ||
| return ArtifactType.FILE | ||
|
|
||
|
|
||
| def _is_visible(path: str) -> bool: | ||
| return not any(is_hidden_workspace_name(part) for part in path.split("/")) | ||
|
|
||
|
|
||
| def _file_signal(entry: OutputEntry) -> str | None: | ||
| """Change signal for a file: its hash, or a size-and-mtime surrogate for | ||
| files past the hash ceiling, shaped so it can never read as a real sha. | ||
| The manifest carries fstat size and mtime for every file it lists. An | ||
| adapter omitting one degrades detection to the other field alone, which | ||
| still beats no signal, so partial metadata is accepted.""" | ||
| if entry.sha256 is not None: | ||
| return entry.sha256 | ||
| if entry.size is None and entry.mtime_ns is None: | ||
| return None | ||
| size = "" if entry.size is None else entry.size | ||
| mtime = "" if entry.mtime_ns is None else entry.mtime_ns | ||
| return f"meta:{size}:{mtime}" | ||
|
|
||
|
|
||
| def _directory_hash(root: str, contents: list[OutputEntry]) -> str: | ||
| """Deterministic hash of a directory's visible content, over paths | ||
| relative to the directory, so identical trees hash identically. A child | ||
| rename, addition, removal, or change signal all move it.""" | ||
| prefix = len(root) + 1 | ||
| digest = sha256() | ||
| for entry in sorted(contents, key=lambda e: e.path): | ||
| relative = entry.path[prefix:] | ||
| if entry.is_directory: | ||
| digest.update(f"{relative}\x00dir\n".encode()) | ||
| else: | ||
| digest.update(f"{relative}\x00{_file_signal(entry) or ''}\n".encode()) | ||
| return digest.hexdigest() | ||
|
|
||
|
|
||
| def derive_artifacts(entries: list[OutputEntry]) -> list[DerivedArtifact]: | ||
| """Reduce a complete outputs listing to its artifacts, sorted by path. | ||
|
|
||
| Each visible top-level entry is one artifact, nested files belong to | ||
| their top-level directory and never surface on their own. | ||
| """ | ||
| top_level: list[OutputEntry] = [] | ||
| children: dict[str, list[OutputEntry]] = defaultdict(list) | ||
| for entry in entries: | ||
| if not _is_visible(entry.path): | ||
| continue | ||
| root, _, nested = entry.path.partition("/") | ||
| if nested: | ||
| children[root].append(entry) | ||
| else: | ||
| top_level.append(entry) | ||
|
|
||
| artifacts: list[DerivedArtifact] = [] | ||
| for entry in top_level: | ||
| name = PurePosixPath(entry.path).name | ||
| if not entry.is_directory: | ||
| artifacts.append( | ||
| DerivedArtifact( | ||
| path=entry.path, | ||
| name=name, | ||
| type=classify_file(entry.path), | ||
| size_bytes=entry.size, | ||
| content_hash=_file_signal(entry), | ||
| ) | ||
| ) | ||
| continue | ||
| contents = children[entry.path] | ||
| is_webapp = entry.path == WEBAPP_ROOT_NAME and any( | ||
| c.path == WEBAPP_OUTPUTS_RELATIVE_PACKAGE_JSON and not c.is_directory | ||
| for c in contents | ||
| ) | ||
| artifacts.append( | ||
| DerivedArtifact( | ||
| path=entry.path, | ||
| name=name, | ||
| type=ArtifactType.WEB_APP if is_webapp else ArtifactType.DIRECTORY, | ||
| size_bytes=sum(c.size or 0 for c in contents if not c.is_directory), | ||
| content_hash=_directory_hash(entry.path, contents), | ||
| ) | ||
| ) | ||
| return sorted(artifacts, key=lambda a: a.path) | ||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.