Skip to content

The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.

feat(assets): split asset records from content (#16295)

Simon Pinfold19e1058f4c44
diff --git a/alembic_db/versions/0001_assets.py b/alembic_db/versions/0001_assets.py
index 1e10b94dc..0e5e4c3e5 100644
--- a/alembic_db/versions/0001_assets.py
+++ b/alembic_db/versions/0001_assets.py
@@ -150,7 +150,6 @@ def downgrade() -> None:
op.drop_index("ix_asset_cache_state_asset_id", table_name="asset_cache_state")
op.drop_index("ix_asset_cache_state_file_path", table_name="asset_cache_state")
- op.drop_constraint("uq_asset_cache_state_file_path", table_name="asset_cache_state")
op.drop_table("asset_cache_state")
op.drop_index("ix_asset_info_tags_asset_info_id", table_name="asset_info_tags")
@@ -160,7 +159,6 @@ def downgrade() -> None:
op.drop_index("ix_tags_tag_type", table_name="tags")
op.drop_table("tags")
- op.drop_constraint("uq_assets_info_asset_owner_name", table_name="assets_info")
op.drop_index("ix_assets_info_owner_name", table_name="assets_info")
op.drop_index("ix_assets_info_last_access_time", table_name="assets_info")
op.drop_index("ix_assets_info_created_at", table_name="assets_info")
diff --git a/alembic_db/versions/0002_merge_to_asset_references.py b/alembic_db/versions/0002_merge_to_asset_references.py
index 1ac1b980c..a39f19dc4 100644
--- a/alembic_db/versions/0002_merge_to_asset_references.py
+++ b/alembic_db/versions/0002_merge_to_asset_references.py
@@ -177,7 +177,10 @@ def downgrade() -> None:
NOTE: Data is not recoverable. The upgrade discards all rows from the old
tables and truncates assets. After downgrade the old schema will be empty.
- A filesystem rescan will repopulate data once the older code is running.
+ A filesystem rescan can repopulate only data derived from paths. It will
+ not restore user_metadata, manually-applied tags, preview_id nominations,
+ name renames, or job_id, because build_asset_specs derives names and tags
+ from the path alone.
"""
# Drop new tables (order matters due to FK constraints)
op.drop_index("ix_asset_reference_meta_key_val_bool", table_name="asset_reference_meta")
diff --git a/alembic_db/versions/0007_record_content_split.py b/alembic_db/versions/0007_record_content_split.py
new file mode 100644
index 000000000..c88af94d5
--- /dev/null
+++ b/alembic_db/versions/0007_record_content_split.py
@@ -0,0 +1,125 @@
+"""
+Record/content split.
+
+This migration intentionally discards the existing asset database. The
+asset_reference_meta, asset_reference_tags, asset_references, and assets
+tables are dropped, and DELETE FROM tags removes all existing tag rows. No
+data migration is performed.
+
+A filesystem rescan after this migration will not restore user_metadata,
+manually-applied tags, preview_id nominations, name renames, or job_id.
+build_asset_specs derives names and tags from the path alone.
+
+Revision ID: 0007_record_content_split
+Revises: 0006_add_loader_path
+Create Date: 2026-08-26
+"""
+
+from alembic import op
+import sqlalchemy as sa
+
+
+revision = "0007_record_content_split"
+down_revision = "0006_add_loader_path"
+branch_labels = None
+depends_on = None
+
+
+def upgrade() -> None:
+ op.drop_table("asset_reference_meta")
+ op.drop_table("asset_reference_tags")
+ op.drop_table("asset_references")
+ op.drop_table("assets")
+ op.execute("DELETE FROM tags")
+ op.create_table(
+ "asset_contents",
+ sa.Column("id", sa.String(36), primary_key=True),
+ sa.Column("hash", sa.String(256)),
+ sa.Column("size_bytes", sa.BigInteger(), nullable=False),
+ sa.Column("path", sa.Text(), nullable=False),
+ sa.Column("mtime_ns", sa.BigInteger()),
+ sa.Column("is_missing", sa.Boolean(), nullable=False, server_default="0"),
+ sa.Column("created_at", sa.DateTime(), nullable=False),
+ sa.CheckConstraint("size_bytes >= 0", name="ck_asset_contents_size_nonneg"),
+ sa.CheckConstraint("mtime_ns >= 0", name="ck_asset_contents_mtime_nonneg"),
+ )
+ op.create_index("ix_asset_contents_hash", "asset_contents", ["hash"])
+ op.create_index(
+ "uq_asset_contents_path_live", "asset_contents", ["path"], unique=True,
+ sqlite_where=sa.text("is_missing = 0"),
+ )
+ op.create_table(
+ "assets",
+ sa.Column("id", sa.String(36), primary_key=True),
+ sa.Column("content_id", sa.String(36), sa.ForeignKey("asset_contents.id", ondelete="RESTRICT"), nullable=False),
+ sa.Column("name", sa.String(512), nullable=False),
+ sa.Column("mime_type", sa.String(255)),
+ sa.Column("system_metadata", sa.JSON()),
+ sa.Column("job_id", sa.String(36)),
+ sa.Column("user_metadata", sa.JSON()),
+ sa.Column("loader_path", sa.Text()),
+ sa.Column("preview_id", sa.String(36), sa.ForeignKey("assets.id", ondelete="SET NULL")),
+ sa.Column("created_at", sa.DateTime(), nullable=False),
+ sa.Column("updated_at", sa.DateTime(), nullable=False),
+ sa.Column("last_access_time", sa.DateTime()),
+ )
+ op.create_index("ix_assets_content_id", "assets", ["content_id"])
+ op.create_index("ix_assets_name", "assets", ["name"])
+ op.create_index("ix_assets_created_at", "assets", ["created_at"])
+ op.create_index("ix_assets_preview_id", "assets", ["preview_id"])
+ op.create_table(
+ "asset_meta",
+ sa.Column("asset_id", sa.String(36), sa.ForeignKey("assets.id", ondelete="CASCADE"), primary_key=True),
+ sa.Column("key", sa.String(256), primary_key=True),
+ sa.Column("ordinal", sa.Integer(), primary_key=True),
+ sa.Column("val_str", sa.String(2048)), sa.Column("val_num", sa.Numeric(38, 10)),
+ sa.Column("val_bool", sa.Boolean()), sa.Column("val_json", sa.JSON()),
+ sa.CheckConstraint("val_str IS NOT NULL OR val_num IS NOT NULL OR val_bool IS NOT NULL OR val_json IS NOT NULL", name="ck_asset_meta_has_value"),
+ )
+ op.create_index("ix_asset_meta_key", "asset_meta", ["key"])
+ op.create_index("ix_asset_meta_key_val_str", "asset_meta", ["key", "val_str"])
+ op.create_index("ix_asset_meta_key_val_num", "asset_meta", ["key", "val_num"])
+ op.create_index("ix_asset_meta_key_val_bool", "asset_meta", ["key", "val_bool"])
+ op.create_table("asset_tags", sa.Column("asset_id", sa.String(36), sa.ForeignKey("assets.id", ondelete="CASCADE"), primary_key=True), sa.Column("tag_name", sa.String(512), sa.ForeignKey("tags.name", ondelete="RESTRICT"), primary_key=True), sa.Column("origin", sa.String(32), nullable=False), sa.Column("added_at", sa.DateTime(), nullable=False))
+ op.create_index("ix_asset_tags_tag_name", "asset_tags", ["tag_name"])
+ op.create_index("ix_asset_tags_asset_id", "asset_tags", ["asset_id"])
+ op.create_table("asset_system_state", sa.Column("key", sa.String(256), primary_key=True), sa.Column("value", sa.Text(), nullable=False))
+
+
+def downgrade() -> None:
+ op.drop_table("asset_system_state")
+ op.drop_table("asset_tags")
+ op.drop_table("asset_meta")
+ op.drop_table("assets")
+ op.drop_table("asset_contents")
+ op.create_table(
+ "assets",
+ sa.Column("id", sa.String(length=36), primary_key=True),
+ sa.Column("hash", sa.String(length=256), nullable=True),
+ sa.Column("size_bytes", sa.BigInteger(), nullable=False, server_default="0"),
+ sa.Column("mime_type", sa.String(length=255), nullable=True),
+ sa.Column("created_at", sa.DateTime(timezone=False), nullable=False),
+ sa.CheckConstraint("size_bytes >= 0", name="ck_assets_size_nonneg"),
+ )
+ op.create_index("uq_assets_hash", "assets", ["hash"], unique=True)
+ op.create_index("ix_assets_mime_type", "assets", ["mime_type"])
+ op.create_table("asset_references", sa.Column("id", sa.String(36), primary_key=True), sa.Column("asset_id", sa.String(36), sa.ForeignKey("assets.id", ondelete="CASCADE"), nullable=False), sa.Column("file_path", sa.Text()), sa.Column("loader_path", sa.Text()), sa.Column("mtime_ns", sa.BigInteger()), sa.Column("needs_verify", sa.Boolean(), nullable=False), sa.Column("is_missing", sa.Boolean(), nullable=False), sa.Column("enrichment_level", sa.Integer(), nullable=False), sa.Column("owner_id", sa.String(128), nullable=False), sa.Column("name", sa.String(512), nullable=False), sa.Column("preview_id", sa.String(36), sa.ForeignKey("asset_references.id", ondelete="SET NULL")), sa.Column("user_metadata", sa.JSON()), sa.Column("system_metadata", sa.JSON()), sa.Column("job_id", sa.String(36)), sa.Column("created_at", sa.DateTime(), nullable=False), sa.Column("updated_at", sa.DateTime(), nullable=False), sa.Column("last_access_time", sa.DateTime(), nullable=False), sa.Column("deleted_at", sa.DateTime()))
+ op.create_index("uq_asset_references_file_path", "asset_references", ["file_path"], unique=True)
+ op.create_index("ix_asset_references_asset_id", "asset_references", ["asset_id"])
+ op.create_index("ix_asset_references_owner_id", "asset_references", ["owner_id"])
+ op.create_index("ix_asset_references_name", "asset_references", ["name"])
+ op.create_index("ix_asset_references_is_missing", "asset_references", ["is_missing"])
+ op.create_index("ix_asset_references_enrichment_level", "asset_references", ["enrichment_level"])
+ op.create_index("ix_asset_references_created_at", "asset_references", ["created_at"])
+ op.create_index("ix_asset_references_last_access_time", "asset_references", ["last_access_time"])
+ op.create_index("ix_asset_references_owner_name", "asset_references", ["owner_id", "name"])
+ op.create_index("ix_asset_references_deleted_at", "asset_references", ["deleted_at"])
+ op.create_index("ix_asset_references_preview_id", "asset_references", ["preview_id"])
+ op.create_table("asset_reference_meta", sa.Column("asset_reference_id", sa.String(36), sa.ForeignKey("asset_references.id", ondelete="CASCADE"), primary_key=True), sa.Column("key", sa.String(256), primary_key=True), sa.Column("ordinal", sa.Integer(), primary_key=True), sa.Column("val_str", sa.String(2048)), sa.Column("val_num", sa.Numeric(38, 10)), sa.Column("val_bool", sa.Boolean()), sa.Column("val_json", sa.JSON()), sa.CheckConstraint("val_str IS NOT NULL OR val_num IS NOT NULL OR val_bool IS NOT NULL OR val_json IS NOT NULL", name="ck_asset_reference_meta_has_value"))
+ op.create_index("ix_asset_reference_meta_key", "asset_reference_meta", ["key"])
+ op.create_index("ix_asset_reference_meta_key_val_str", "asset_reference_meta", ["key", "val_str"])
+ op.create_index("ix_asset_reference_meta_key_val_num", "asset_reference_meta", ["key", "val_num"])
+ op.create_index("ix_asset_reference_meta_key_val_bool", "asset_reference_meta", ["key", "val_bool"])
+ op.create_table("asset_reference_tags", sa.Column("asset_reference_id", sa.String(36), sa.ForeignKey("asset_references.id", ondelete="CASCADE"), primary_key=True), sa.Column("tag_name", sa.String(512), sa.ForeignKey("tags.name", ondelete="RESTRICT"), primary_key=True), sa.Column("origin", sa.String(32), nullable=False), sa.Column("added_at", sa.DateTime(), nullable=False))
+ op.create_index("ix_asset_reference_tags_tag_name", "asset_reference_tags", ["tag_name"])
+ op.create_index("ix_asset_reference_tags_asset_reference_id", "asset_reference_tags", ["asset_reference_id"])
diff --git a/app/assets/api/routes.py b/app/assets/api/routes.py
index 62fb97025..f5ed98be5 100644
--- a/app/assets/api/routes.py
+++ b/app/assets/api/routes.py
@@ -1,3 +1,11 @@
+"""Serves the asset HTTP API: listing, detail, upload, tagging, hash-addressed
+creation and scan control. A feature gate wraps every handler and answers 503
+when the asset system is switched off or its database dependencies are absent,
+and failures leave as a JSON envelope carrying a stable machine-readable code
+rather than aiohttp's default text. Handlers own request parsing and error
+shaping only; the work itself belongs to the services layer.
+"""
+
import asyncio
import functools
import json
@@ -6,6 +14,7 @@ import mimetypes
import os
import urllib.parse
import uuid
+from datetime import timezone
from typing import Any
from aiohttp import web
@@ -13,6 +22,7 @@ from pydantic import ValidationError
import folder_paths
from app import user_manager
+from app.assets import mode
from app.assets.api import schemas_in, schemas_out
from app.assets.services import schemas
from app.assets.api.schemas_in import (
@@ -24,30 +34,54 @@ from app.assets.api.upload import (
delete_temp_file_if_exists,
parse_multipart_upload,
)
+from app.assets.database.models import Asset
+from app.assets.database.queries.records import (
+ RecordCursorBoundary,
+ RecordPageSpec,
+ RecordSortField,
+ RecordSortOrder,
+ get_preview_file_paths_by_ids,
+ list_records_page,
+)
from app.assets.seeder import ScanInProgressError, asset_seeder
from app.assets.services import (
DependencyMissingError,
HashMismatchError,
+ UploadUnstableError,
apply_tags,
asset_exists,
create_from_hash,
delete_asset_reference,
get_asset_detail,
get_preview_file_paths,
- list_assets_page,
list_tags,
remove_tags,
resolve_asset_for_download,
update_asset_metadata,
upload_from_temp_path,
)
-from app.assets.services.cursor import InvalidCursorError
from app.assets.services.path_utils import compute_asset_response_paths
+from app.assets.services.cursor import (
+ InvalidCursorError,
+ decode_cursor,
+ decode_cursor_int,
+ decode_cursor_time,
+ encode_cursor,
+ encode_cursor_from_time,
+)
from app.assets.services.tagging import list_tag_histogram
+from app.database.db import create_session
ROUTES = web.RouteTableDef()
USER_MANAGER: user_manager.UserManager | None = None
_ASSETS_ENABLED = False
+SYSTEM_TAGS = frozenset({"missing"})
+_CURSOR_SORT_FIELDS: tuple[RecordSortField, ...] = (
+ "created_at",
+ "updated_at",
+ "name",
+ "size",
+)
def _require_assets_feature_enabled(handler):
@@ -99,12 +133,6 @@ def register_assets_routes(
app.add_routes(ROUTES)
-def disable_assets_routes() -> None:
- """Disable asset routes at runtime (e.g. after DB init failure)."""
- global _ASSETS_ENABLED
- _ASSETS_ENABLED = False
-
-
def _build_error_response(
status: int, code: str, message: str, details: dict | None = None
) -> web.Response:
@@ -119,6 +147,20 @@ def _build_validation_error_response(code: str, ve: ValidationError) -> web.Resp
return _build_error_response(400, code, "Validation failed.", {"errors": errors})
+class SystemTagForbiddenError(Exception):
+ def __init__(self, tag: str):
+ super().__init__(
+ f"Tag '{tag}' is system-managed and cannot be modified via the API"
+ )
+ self.tag = tag
+
+
+def _reject_system_tags(tags: list[str]) -> None:
+ for tag in tags:
+ if tag in SYSTEM_TAGS:
+ raise SystemTagForbiddenError(tag)
+
+
class InvalidTagFilterError(Exception):
"""Invalid combination of tag-filter query parameters."""
@@ -200,13 +242,22 @@ def _resolve_tag_filters(
return all_list, tags_any, none_list
-def _validate_sort_field(requested: str | None) -> str:
+def _validate_sort_field(requested: str | None) -> RecordSortField:
if not requested:
return "created_at"
- v = requested.lower()
- if v in {"name", "created_at", "updated_at", "size", "last_access_time"}:
- return v
- return "created_at"
+ match requested.lower():
+ case "name":
+ return "name"
+ case "created_at":
+ return "created_at"
+ case "updated_at":
+ return "updated_at"
+ case "size":
+ return "size"
+ case "last_access_time":
+ return "last_access_time"
+ case _:
+ return "created_at"
# What a client can render from the bytes themselves; anything else needs a nominated preview.
@@ -258,6 +309,8 @@ def _build_asset_response(
if result.ref.preview_id:
# A nominated preview is one whatever it holds, so no media check here.
preview_url = _build_view_url(preview_paths.get(result.ref.preview_id))
+ elif result.asset is not None and result.asset.is_missing:
+ preview_url = None
elif _has_previewable_content(result.asset, result.ref.file_path):
preview_url = _build_view_url(result.ref.file_path)
else:
@@ -268,7 +321,8 @@ def _build_asset_response(
# In-root loader path (model category dropped): what model loaders consume.
loader_path = result.ref.loader_path
else:
- display_name, loader_path = None, None
+ display_name = None
+ loader_path = None
asset_content_hash = result.asset.hash if result.asset else None
return schemas_out.Asset(
id=result.ref.id,
@@ -276,7 +330,6 @@ def _build_asset_response(
hash=asset_content_hash,
loader_path=loader_path,
display_name=display_name,
- asset_hash=asset_content_hash,
size=int(result.asset.size_bytes) if result.asset else None,
mime_type=result.asset.mime_type if result.asset else None,
tags=result.tags,
@@ -292,6 +345,111 @@ def _build_asset_response(
)
+def _build_record_response(
+ record: Asset,
+ tags: list[str],
+ preview_paths: dict[str, str],
+) -> schemas_out.Asset:
+ content = record.content
+ paths = compute_asset_response_paths(content.path)
+ display_name = paths[1] if paths else None
+ if record.preview_id:
+ preview_url = _build_view_url(preview_paths.get(record.preview_id))
+ elif content.is_missing:
+ preview_url = None
+ else:
+ mime_type = record.mime_type or mimetypes.guess_type(content.path)[0] or ""
+ if mime_type.split(";", 1)[0].strip().lower().startswith(
+ PREVIEWABLE_MIME_PREFIXES
+ ):
+ preview_url = _build_view_url(content.path)
+ else:
+ preview_url = None
+
+ return schemas_out.Asset(
+ id=record.id,
+ name=record.name,
+ hash=content.hash,
+ loader_path=record.loader_path,
+ display_name=display_name,
+ size=content.size_bytes,
+ mime_type=record.mime_type,
+ tags=tags,
+ preview_url=preview_url,
+ preview_id=record.preview_id,
+ user_metadata=record.user_metadata or {},
+ metadata=record.system_metadata,
+ job_id=record.job_id,
+ prompt_id=record.job_id,
+ created_at=record.created_at,
+ updated_at=record.updated_at,
+ last_access_time=record.last_access_time,
+ )
+
+
+def _decode_record_cursor(
+ after: str | None,
+ sort: RecordSortField,
+ order: RecordSortOrder,
+) -> RecordCursorBoundary | None:
+ if after is None:
+ return None
+ if sort not in _CURSOR_SORT_FIELDS:
+ raise InvalidCursorError(
+ f"cursor pagination is not supported for sort={sort!r}"
+ )
+ payload = decode_cursor(
+ after,
+ _CURSOR_SORT_FIELDS,
+ expected_order=order,
+ )
+ if payload.sort_field != sort:
+ raise InvalidCursorError(
+ f"cursor sort field {payload.sort_field!r} does not match request sort {sort!r}"
+ )
+ match payload.sort_field:
+ case "created_at" | "updated_at":
+ value = decode_cursor_time(payload).replace(tzinfo=None)
+ case "size":
+ value = decode_cursor_int(payload)
+ case "name":
+ value = payload.value
+ case unsupported:
+ raise InvalidCursorError(f"unsupported sort field {unsupported!r}")
+ return RecordCursorBoundary(value=value, id=payload.id)
+
+
+def _encode_record_cursor(
+ record: Asset,
+ sort: RecordSortField,
+ order: RecordSortOrder,
+) -> str:
+ match sort:
+ case "name":
+ return encode_cursor("name", record.name, record.id, order=order)
+ case "size":
+ return encode_cursor(
+ "size",
+ str(record.content.size_bytes),
+ record.id,
+ order=order,
+ )
+ case "created_at":
+ timestamp = record.created_at
+ case "updated_at":
+ timestamp = record.updated_at
+ case "last_access_time":
+ raise InvalidCursorError(
+ "cursor pagination is not supported for sort='last_access_time'"
+ )
+ return encode_cursor_from_time(
+ sort,
+ timestamp.replace(tzinfo=timezone.utc),
+ record.id,
+ order=order,
+ )
+
+
@ROUTES.head("/api/assets/hash/{hash}")
@_require_assets_feature_enabled
async def head_asset_by_hash(request: web.Request) -> web.Response:
@@ -312,6 +470,11 @@ async def list_assets_route(request: web.Request) -> web.Response:
"""
GET request to list assets.
"""
+ if "metadata_filter" in request.query:
+ return _build_error_response(
+ 400, "UNSUPPORTED_PARAM", "metadata_filter is no longer supported"
+ )
+
query_dict = get_query_dict(request)
try:
q = schemas_in.ListAssetsQuery.model_validate(query_dict)
@@ -324,42 +487,57 @@ async def list_assets_route(request: web.Request) -> web.Response:
return _build_error_response(400, "INVALID_TAG_FILTER", str(e), e.details)
sort = _validate_sort_field(q.sort)
- order_candidate = (q.order or "desc").lower()
- order = order_candidate if order_candidate in {"asc", "desc"} else "desc"
+ order = q.order
try:
- result = list_assets_page(
- owner_id=USER_MANAGER.get_request_user_id(request),
- include_tags=tags_all,
- exclude_tags=tags_none,
- any_tags=tags_any,
- name_contains=q.name_contains,
- metadata_filter=q.metadata_filter,
- limit=q.limit,
- offset=q.offset,
- sort=sort,
- order=order,
- after=q.after,
- )
+ cursor_boundary = _decode_record_cursor(q.after, sort, order)
+ cursor_supported = sort in _CURSOR_SORT_FIELDS
+ fetch_limit = q.limit + 1 if cursor_supported else q.limit
+ with create_session() as session:
+ records, tag_map, total = list_records_page(
+ session,
+ RecordPageSpec(
+ all_tags=tuple(tags_all),
+ any_tags=tuple(tags_any),
+ none_tags=tuple(tags_none),
+ name_contains=q.name_contains,
+ limit=fetch_limit,
+ offset=q.offset,
+ sort=sort,
+ order=order,
+ after=cursor_boundary,
+ ),
+ )
+ next_cursor = None
+ if cursor_supported and len(records) > q.limit:
+ records = records[: q.limit]
+ next_cursor = _encode_record_cursor(records[-1], sort, order)
+
+ preview_ids = sorted(
+ {record.preview_id for record in records if record.preview_id}
+ )
+ preview_paths = get_preview_file_paths_by_ids(session, preview_ids)
+ summaries = [
+ _build_record_response(
+ record,
+ tag_map.get(record.id, []),
+ preview_paths,
+ )
+ for record in records
+ ]
except InvalidCursorError as e:
return _build_error_response(400, "INVALID_CURSOR", str(e))
- preview_paths = _resolve_preview_paths(result.items)
- summaries = [_build_asset_response(item, preview_paths) for item in result.items]
-
- # has_more semantics differ by mode:
- # - cursor mode: a non-empty next_cursor means there are more results.
- # - offset mode: derived from total - (offset + page size).
if q.after is not None:
- has_more = result.next_cursor is not None
+ has_more = next_cursor is not None
else:
- has_more = (q.offset + len(summaries)) < result.total
+ has_more = q.offset + len(summaries) < total
payload = schemas_out.AssetsList(
assets=summaries,
- total=result.total,
+ total=total,
has_more=has_more,
- next_cursor=result.next_cursor,
+ next_cursor=next_cursor,
)
return web.json_response(payload.model_dump(mode="json", exclude_none=True))
@@ -374,7 +552,6 @@ async def get_asset_route(request: web.Request) -> web.Response:
try:
result = get_asset_detail(
reference_id=reference_id,
- owner_id=USER_MANAGER.get_request_user_id(request),
)
if not result:
return _build_error_response(
@@ -391,7 +568,7 @@ async def get_asset_route(request: web.Request) -> web.Response:
)
except Exception:
logging.exception(
- "get_asset failed for reference_id=%s, owner_id=%s",
+ "get_asset failed for reference_id=%s, tenant_id=%s",
reference_id,
USER_MANAGER.get_request_user_id(request),
)
@@ -409,7 +586,6 @@ async def download_asset_content(request: web.Request) -> web.Response:
try:
result = resolve_asset_for_download(
reference_id=str(uuid.UUID(request.match_info["id"])),
- owner_id=USER_MANAGER.get_request_user_id(request),
)
abs_path = result.abs_path
content_type = result.content_type
@@ -501,15 +677,22 @@ async def create_asset_from_hash_route(request: web.Request) -> web.Response:
if name is None:
name = body.hash.split(":", 1)[1] if ":" in body.hash else body.hash
- result = create_from_hash(
- hash_str=body.hash,
- name=name,
- tags=body.tags,
- user_metadata=body.user_metadata,
- owner_id=USER_MANAGER.get_request_user_id(request),
- mime_type=body.mime_type,
- preview_id=body.preview_id,
- )
+ if not mode.hashing_enabled():
+ return _build_error_response(
+ 400, "FEATURE_DISABLED", "Asset hashing is disabled."
+ )
+
+ try:
+ result = create_from_hash(
+ hash_str=body.hash,
+ name=name,
+ tags=body.tags,
+ user_metadata=body.user_metadata,
+ mime_type=body.mime_type,
+ preview_id=body.preview_id,
+ )
+ except ValueError as e:
+ return _build_error_response(400, "INVALID_BODY", str(e))
if result is None:
return _build_error_response(
404, "ASSET_NOT_FOUND", f"Asset content {body.hash} does not exist"
@@ -532,7 +715,7 @@ async def upload_asset(request: web.Request) -> web.Response:
except UploadError as e:
return _build_error_response(e.status, e.code, e.message)
- owner_id = USER_MANAGER.get_request_user_id(request)
+ tenant_id = USER_MANAGER.get_request_user_id(request)
try:
spec = schemas_in.UploadAssetSpec.model_validate(
@@ -552,43 +735,40 @@ async def upload_asset(request: web.Request) -> web.Response:
)
try:
- # Fast path: hash exists, create AssetReference without writing anything
- if spec.hash and parsed.provided_hash_exists is True:
+ if not parsed.file_present and spec.hash:
+ if not mode.hashing_enabled():
+ return _build_error_response(
+ 400, "FEATURE_DISABLED", "Asset hashing is disabled."
+ )
result = create_from_hash(
hash_str=spec.hash,
name=spec.name or (spec.hash.split(":", 1)[1]),
tags=spec.tags,
user_metadata=spec.user_metadata or {},
- owner_id=owner_id,
mime_type=spec.mime_type,
preview_id=spec.preview_id,
)
if result is None:
- delete_temp_file_if_exists(parsed.tmp_path)
return _build_error_response(
404, "ASSET_NOT_FOUND", f"Asset content {spec.hash} does not exist"
)
- delete_temp_file_if_exists(parsed.tmp_path)
- else:
- # Otherwise, we must have a temp file path to ingest
- if not parsed.tmp_path or not os.path.exists(parsed.tmp_path):
- return _build_error_response(
- 400,
- "MISSING_INPUT",
- "Provided hash not found and no file uploaded.",
- )
-
+ elif parsed.tmp_path and os.path.exists(parsed.tmp_path):
result = upload_from_temp_path(
temp_path=parsed.tmp_path,
name=spec.name,
tags=spec.tags,
user_metadata=spec.user_metadata or {},
client_filename=parsed.file_client_name,
- owner_id=owner_id,
expected_hash=spec.hash,
mime_type=spec.mime_type,
preview_id=spec.preview_id,
)
+ else:
+ return _build_error_response(
+ 400,
+ "MISSING_INPUT",
+ "Provided hash not found and no file uploaded.",
+ )
except AssetValidationError as e:
delete_temp_file_if_exists(parsed.tmp_path)
return _build_error_response(400, e.code, str(e))
@@ -598,12 +778,15 @@ async def upload_asset(request: web.Request) -> web.Response:
except HashMismatchError as e:
delete_temp_file_if_exists(parsed.tmp_path)
return _build_error_response(400, "HASH_MISMATCH", str(e))
+ except UploadUnstableError as e:
+ delete_temp_file_if_exists(parsed.tmp_path)
+ return _build_error_response(500, "UPLOAD_UNSTABLE", str(e))
except DependencyMissingError as e:
delete_temp_file_if_exists(parsed.tmp_path)
return _build_error_response(503, "DEPENDENCY_MISSING", e.message)
except Exception:
delete_temp_file_if_exists(parsed.tmp_path)
- logging.exception("upload_asset failed for owner_id=%s", owner_id)
+ logging.exception("upload_asset failed for tenant_id=%s", tenant_id)
return _build_error_response(500, "INTERNAL", "Unexpected server error.")
asset = _build_asset_response(result, _resolve_preview_paths([result]))
@@ -633,7 +816,6 @@ async def update_asset_route(request: web.Request) -> web.Response:
reference_id=reference_id,
name=body.name,
user_metadata=body.user_metadata,
- owner_id=USER_MANAGER.get_request_user_id(request),
preview_id=body.preview_id,
)
payload = _build_asset_response(result, _resolve_preview_paths([result]))
@@ -645,7 +827,7 @@ async def update_asset_route(request: web.Request) -> web.Response:
)
except Exception:
logging.exception(
- "update_asset failed for reference_id=%s, owner_id=%s",
+ "update_asset failed for reference_id=%s, tenant_id=%s",
reference_id,
USER_MANAGER.get_request_user_id(request),
)
@@ -659,16 +841,12 @@ async def delete_asset_route(request: web.Request) -> web.Response:
reference_id = str(uuid.UUID(request.match_info["id"]))
try:
- # Deleting an asset is a soft delete of the reference; the underlying
- # content is preserved (it may be shared with other references).
deleted = delete_asset_reference(
reference_id=reference_id,
- owner_id=USER_MANAGER.get_request_user_id(request),
- delete_content_if_orphan=False,
)
except Exception:
logging.exception(
- "delete_asset_reference failed for reference_id=%s, owner_id=%s",
+ "delete_asset_reference failed for reference_id=%s, tenant_id=%s",
reference_id,
USER_MANAGER.get_request_user_id(request),
)
@@ -705,7 +883,6 @@ async def get_tags(request: web.Request) -> web.Response:
offset=query.offset,
order=query.order,
include_zero=query.include_zero,
- owner_id=USER_MANAGER.get_request_user_id(request),
)
tags = [
@@ -738,17 +915,21 @@ async def add_asset_tags(request: web.Request) -> web.Response:
)
try:
+ _reject_system_tags(data.tags)
result = apply_tags(
reference_id=reference_id,
tags=data.tags,
origin="manual",
- owner_id=USER_MANAGER.get_request_user_id(request),
)
payload = schemas_out.TagsAdd(
added=result.added,
already_present=result.already_present,
total_tags=result.total_tags,
)
+ except SystemTagForbiddenError as se:
+ return _build_error_response(
+ 400, "SYSTEM_TAG_FORBIDDEN", str(se), {"tag": se.tag}
+ )
except PermissionError as pe:
return _build_error_response(403, "FORBIDDEN", str(pe), {"id": reference_id})
except ValueError as ve:
@@ -757,7 +938,7 @@ async def add_asset_tags(request: web.Request) -> web.Response:
)
except Exception:
logging.exception(
- "add_tags_to_asset failed for reference_id=%s, owner_id=%s",
+ "add_tags_to_asset failed for reference_id=%s, tenant_id=%s",
reference_id,
USER_MANAGER.get_request_user_id(request),
)
@@ -786,15 +967,20 @@ async def delete_asset_tags(request: web.Request) -> web.Response:
)
try:
+ _reject_system_tags(data.tags)
result = remove_tags(
reference_id=reference_id,
tags=data.tags,
- owner_id=USER_MANAGER.get_request_user_id(request),
)
payload = schemas_out.TagsRemove(
removed=result.removed,
not_present=result.not_present,
total_tags=result.total_tags,
+ protected=result.protected,
+ )
+ except SystemTagForbiddenError as se:
+ return _build_error_response(
+ 400, "SYSTEM_TAG_FORBIDDEN", str(se), {"tag": se.tag}
)
except PermissionError as pe:
return _build_error_response(403, "FORBIDDEN", str(pe), {"id": reference_id})
@@ -804,7 +990,7 @@ async def delete_asset_tags(request: web.Request) -> web.Response:
)
except Exception:
logging.exception(
- "remove_tags_from_asset failed for reference_id=%s, owner_id=%s",
+ "remove_tags_from_asset failed for reference_id=%s, tenant_id=%s",
reference_id,
USER_MANAGER.get_request_user_id(request),
)
@@ -817,6 +1003,11 @@ async def delete_asset_tags(request: web.Request) -> web.Response:
@_require_assets_feature_enabled
async def get_tags_refine(request: web.Request) -> web.Response:
"""GET request to get tag histogram for filtered assets."""
+ if "metadata_filter" in request.query:
+ return _build_error_response(
+ 400, "UNSUPPORTED_PARAM", "metadata_filter is no longer supported"
+ )
+
query_dict = get_query_dict(request)
try:
q = schemas_in.TagsRefineQuery.model_validate(query_dict)
@@ -829,12 +1020,10 @@ async def get_tags_refine(request: web.Request) -> web.Response:
return _build_error_response(400, "INVALID_TAG_FILTER", str(e), e.details)
tag_counts = list_tag_histogram(
- owner_id=USER_MANAGER.get_request_user_id(request),
include_tags=tags_all,
exclude_tags=tags_none,
any_tags=tags_any,
name_contains=q.name_contains,
- metadata_filter=q.metadata_filter,
limit=q.limit,
)
payload = schemas_out.TagHistogram(tag_counts=tag_counts)
@@ -867,7 +1056,9 @@ async def seed_assets(request: web.Request) -> web.Response:
wait_param = request.query.get("wait", "").lower()
should_wait = wait_param in ("true", "1", "yes")
- started = asset_seeder.start(roots=valid_roots)
+ started = asset_seeder.start(
+ roots=valid_roots, compute_hashes=mode.hashing_enabled()
+ )
if not started:
return web.json_response({"status": "already_running"}, status=409)
diff --git a/app/assets/api/schemas_in.py b/app/assets/api/schemas_in.py
index 862700a24..ccf51d7da 100644
--- a/app/assets/api/schemas_in.py
+++ b/app/assets/api/schemas_in.py
@@ -1,3 +1,10 @@
+"""Parses and validates everything the asset API accepts from a client, so
+handlers receive typed values instead of raw JSON. Query strings, JSON bodies
+and multipart upload specs each get a model that rejects malformed input at the
+boundary, normalizes tags and hashes, and raises errors already carrying the
+HTTP status and code the handler should return.
+"""
+
import json
from dataclasses import dataclass
from typing import Any, Literal
@@ -58,9 +65,6 @@ class ListAssetsQuery(BaseModel):
tags_none: list[str] = Field(default_factory=list)
name_contains: str | None = None
- # Accept either a JSON string (query param) or a dict
- metadata_filter: dict[str, Any] | None = None
-
limit: conint(ge=1, le=500) = 20
offset: conint(ge=0) = 0
# Opaque keyset cursor. When supplied, `offset` is ignored. Cursor pagination
@@ -93,22 +97,6 @@ class ListAssetsQuery(BaseModel):
return out
return v
- @field_validator("metadata_filter", mode="before")
- @classmethod
- def _parse_metadata_json(cls, v):
- if v is None or isinstance(v, dict):
- return v
- if isinstance(v, str) and v.strip():
- try:
- parsed = json.loads(v)
- except Exception as e:
- raise ValueError(f"metadata_filter must be JSON: {e}") from e
- if not isinstance(parsed, dict):
- raise ValueError("metadata_filter must be a JSON object")
- return parsed
- return None
-
-
class UpdateAssetBody(BaseModel):
name: str | None = None
user_metadata: dict[str, Any] | None = None
@@ -168,7 +156,6 @@ class TagsRefineQuery(BaseModel):
tags_any: list[str] = Field(default_factory=list)
tags_none: list[str] = Field(default_factory=list)
name_contains: str | None = None
- metadata_filter: dict[str, Any] | None = None
limit: conint(ge=1, le=1000) = 100
@field_validator(
@@ -189,22 +176,6 @@ class TagsRefineQuery(BaseModel):
return out
return v
- @field_validator("metadata_filter", mode="before")
- @classmethod
- def _parse_metadata_json(cls, v):
- if v is None or isinstance(v, dict):
- return v
- if isinstance(v, str) and v.strip():
- try:
- parsed = json.loads(v)
- except Exception as e:
- raise ValueError(f"metadata_filter must be JSON: {e}") from e
- if not isinstance(parsed, dict):
- raise ValueError("metadata_filter must be a JSON object")
- return parsed
- return None
-
-
class TagsListQuery(BaseModel):
model_config = ConfigDict(extra="ignore", str_strip_whitespace=True)
diff --git a/app/assets/api/schemas_out.py b/app/assets/api/schemas_out.py
index da8251499..f9b8a0938 100644
--- a/app/assets/api/schemas_out.py
+++ b/app/assets/api/schemas_out.py
@@ -1,3 +1,9 @@
+"""Defines the response bodies the asset API returns, fixing the field names,
+types and serialization its HTTP contract promises. Routes dump these models
+with null fields omitted, so whether a field is absent and whether it is empty
+stay distinguishable to a client.
+"""
+
from datetime import datetime
from typing import Any
@@ -5,14 +11,10 @@ from pydantic import BaseModel, ConfigDict, Field, field_serializer
class Asset(BaseModel):
- """API view of an asset. Maps to DB ``AssetReference`` joined with its ``Asset`` blob;
- ``id`` here is the AssetReference id, not the content-addressed Asset id."""
-
id: str
name: str = Field(
...,
- deprecated=True,
- description="Reference label, often caller-provided or derived from the filename. Deprecated for storage path/display semantics; use `loader_path` and `display_name` when present.",
+ description="Record label, usually derived from the source filename.",
)
hash: str | None = None
loader_path: str | None = Field(
@@ -23,14 +25,13 @@ class Asset(BaseModel):
default=None,
description="Human-facing label for the asset. Not unique.",
)
- asset_hash: str | None = None
+ is_immutable: bool = False
size: int | None = None
mime_type: str | None = None
tags: list[str] = Field(default_factory=list)
preview_url: str | None = None
preview_id: str | None = None # references an asset_reference id, not an asset id
user_metadata: dict[str, Any] = Field(default_factory=dict)
- is_immutable: bool = False
metadata: dict[str, Any] | None = None
job_id: str | None = None
prompt_id: str | None = None # deprecated: use job_id
@@ -80,6 +81,7 @@ class TagsRemove(BaseModel):
removed: list[str] = Field(default_factory=list)
not_present: list[str] = Field(default_factory=list)
total_tags: list[str] = Field(default_factory=list)
+ protected: list[str] = Field(default_factory=list)
class TagHistogram(BaseModel):
diff --git a/app/assets/api/upload.py b/app/assets/api/upload.py
index 2979f0e20..d46ad6389 100644
--- a/app/assets/api/upload.py
+++ b/app/assets/api/upload.py
@@ -1,3 +1,11 @@
+"""Reads a multipart upload off the wire and lands its bytes in a temporary file
+the ingest service can hash and move. The body is consumed in chunks so a large
+model never has to fit in memory, and the temporary file is removed on every
+failure path so an abandoned upload leaves nothing behind. Field values are
+validated as they arrive, letting a bad request fail before its bytes are
+written.
+"""
+
import logging
import os
import uuid
@@ -91,20 +99,6 @@ async def parse_multipart_upload(
file_present = True
file_client_name = (field.filename or "").strip()
- if provided_hash and provided_hash_exists is True:
- # Hash exists - drain file but don't write to disk
- try:
- while True:
- chunk = await field.read_chunk(8 * 1024 * 1024)
- if not chunk:
- break
- file_written += len(chunk)
- except Exception:
- raise UploadError(
- 500, "UPLOAD_IO_ERROR", "Failed to receive uploaded file."
- )
- continue
-
uploads_root = os.path.join(folder_paths.get_temp_directory(), "uploads")
unique_dir = os.path.join(uploads_root, uuid.uuid4().hex)
os.makedirs(unique_dir, exist_ok=True)
diff --git a/app/assets/database/models.py b/app/assets/database/models.py
index 329cd483d..bb0b8c355 100644
--- a/app/assets/database/models.py
+++ b/app/assets/database/models.py
@@ -1,7 +1,16 @@
+"""Declares the asset schema: content rows describing bytes on disk, records
+describing what a user sees, and the tag and metadata tables hanging off them.
+The split is the point — many records can name one content row, and retiring
+content by marking it missing rather than deleting it is what keeps a path's
+history intact. Constraints declared here, not application code, are what make
+a negative size or a second live row at one path impossible.
+"""
+
from __future__ import annotations
import uuid
from datetime import datetime
+from decimal import Decimal
from typing import Any
from sqlalchemy import (
@@ -16,197 +25,134 @@ from sqlalchemy import (
Numeric,
String,
Text,
+ text,
)
-from sqlalchemy.orm import Mapped, foreign, mapped_column, relationship
+from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.assets.helpers import get_utc_now
from app.database.models import Base
-class Asset(Base):
- __tablename__ = "assets"
+class AssetContent(Base):
+ __tablename__ = "asset_contents"
id: Mapped[str] = mapped_column(
String(36), primary_key=True, default=lambda: str(uuid.uuid4())
)
- hash: Mapped[str | None] = mapped_column(String(256), nullable=True)
- size_bytes: Mapped[int] = mapped_column(BigInteger, nullable=False, default=0)
- mime_type: Mapped[str | None] = mapped_column(String(255))
+ hash: Mapped[str | None] = mapped_column(String(256), index=True)
+ size_bytes: Mapped[int] = mapped_column(
+ BigInteger,
+ CheckConstraint("size_bytes >= 0", name="ck_asset_contents_size_nonneg"),
+ nullable=False,
+ default=0,
+ )
+ path: Mapped[str] = mapped_column(Text, nullable=False)
+ mtime_ns: Mapped[int | None] = mapped_column(
+ BigInteger,
+ CheckConstraint("mtime_ns >= 0", name="ck_asset_contents_mtime_nonneg"),
+ )
+ is_missing: Mapped[bool] = mapped_column(
+ Boolean, nullable=False, server_default="0"
+ )
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=False), nullable=False, default=get_utc_now
)
- references: Mapped[list[AssetReference]] = relationship(
- "AssetReference",
- back_populates="asset",
- primaryjoin=lambda: Asset.id == foreign(AssetReference.asset_id),
- foreign_keys=lambda: [AssetReference.asset_id],
- cascade="all,delete-orphan",
- passive_deletes=True,
- )
-
- # preview_id on AssetReference is a self-referential FK to asset_references.id
+ records: Mapped[list[Asset]] = relationship(back_populates="content")
__table_args__ = (
- Index("uq_assets_hash", "hash", unique=True),
- Index("ix_assets_mime_type", "mime_type"),
- CheckConstraint("size_bytes >= 0", name="ck_assets_size_nonneg"),
+ Index(
+ "uq_asset_contents_path_live",
+ "path",
+ unique=True,
+ sqlite_where=text("is_missing = 0"),
+ ),
)
- def __repr__(self) -> str:
- return f"<Asset id={self.id} hash={(self.hash or '')[:12]}>"
-
-
-class AssetReference(Base):
- """Unified model combining file cache state and user-facing metadata.
- Each row represents either:
- - A filesystem reference (file_path is set) with cache state
- - An API-created reference (file_path is NULL) without cache state
- """
-
- __tablename__ = "asset_references"
+class Asset(Base):
+ __tablename__ = "assets"
id: Mapped[str] = mapped_column(
String(36), primary_key=True, default=lambda: str(uuid.uuid4())
)
- asset_id: Mapped[str] = mapped_column(
- String(36), ForeignKey("assets.id", ondelete="CASCADE"), nullable=False
+ content_id: Mapped[str] = mapped_column(
+ String(36),
+ ForeignKey("asset_contents.id", ondelete="RESTRICT"),
+ nullable=False,
)
-
- # Cache state fields (from former AssetCacheState)
- file_path: Mapped[str | None] = mapped_column(Text, nullable=True)
- # In-root loader path derived from file_path at scan/ingest time.
- loader_path: Mapped[str | None] = mapped_column(Text, nullable=True)
- mtime_ns: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
- needs_verify: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
- is_missing: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
- enrichment_level: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
-
- # Info fields (from former AssetInfo)
- owner_id: Mapped[str] = mapped_column(String(128), nullable=False, default="")
name: Mapped[str] = mapped_column(String(512), nullable=False)
+ mime_type: Mapped[str | None] = mapped_column(String(255))
+ system_metadata: Mapped[dict[str, Any] | None] = mapped_column(JSON(none_as_null=True))
+ job_id: Mapped[str | None] = mapped_column(String(36))
+ user_metadata: Mapped[dict[str, Any] | None] = mapped_column(JSON)
+ loader_path: Mapped[str | None] = mapped_column(Text)
preview_id: Mapped[str | None] = mapped_column(
- String(36), ForeignKey("asset_references.id", ondelete="SET NULL")
- )
- user_metadata: Mapped[dict[str, Any] | None] = mapped_column(
- JSON(none_as_null=True)
+ String(36), ForeignKey("assets.id", ondelete="SET NULL")
)
- system_metadata: Mapped[dict[str, Any] | None] = mapped_column(
- JSON(none_as_null=True), nullable=True, default=None
- )
- job_id: Mapped[str | None] = mapped_column(String(36), nullable=True, default=None)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=False), nullable=False, default=get_utc_now
)
+ # Explicit user/API edit time; never use onupdate because row writes are not always edits.
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=False), nullable=False, default=get_utc_now
)
- last_access_time: Mapped[datetime] = mapped_column(
- DateTime(timezone=False), nullable=False, default=get_utc_now
- )
- deleted_at: Mapped[datetime | None] = mapped_column(
- DateTime(timezone=False), nullable=True, default=None
- )
+ last_access_time: Mapped[datetime | None] = mapped_column(DateTime(timezone=False))
- asset: Mapped[Asset] = relationship(
- "Asset",
- back_populates="references",
- foreign_keys=[asset_id],
- lazy="selectin",
- )
- preview_ref: Mapped[AssetReference | None] = relationship(
- "AssetReference",
- foreign_keys=[preview_id],
- remote_side=lambda: [AssetReference.id],
+ content: Mapped[AssetContent] = relationship(back_populates="records", lazy="selectin")
+ preview: Mapped[Asset | None] = relationship(
+ "Asset", foreign_keys=[preview_id], remote_side=lambda: [Asset.id]
)
-
- metadata_entries: Mapped[list[AssetReferenceMeta]] = relationship(
- back_populates="asset_reference",
- cascade="all,delete-orphan",
- passive_deletes=True,
+ metadata_entries: Mapped[list[AssetMeta]] = relationship(
+ back_populates="asset", cascade="all,delete-orphan", passive_deletes=True
)
-
- tag_links: Mapped[list[AssetReferenceTag]] = relationship(
- back_populates="asset_reference",
- cascade="all,delete-orphan",
- passive_deletes=True,
- overlaps="tags,asset_references",
+ tag_links: Mapped[list[AssetTag]] = relationship(
+ back_populates="asset", cascade="all,delete-orphan", passive_deletes=True
)
-
tags: Mapped[list[Tag]] = relationship(
- secondary="asset_reference_tags",
- back_populates="asset_references",
- lazy="selectin",
- viewonly=True,
- overlaps="tag_links,asset_reference_links,asset_references,tag",
+ secondary="asset_tags", back_populates="assets", viewonly=True, lazy="selectin"
)
__table_args__ = (
- Index("uq_asset_references_file_path", "file_path", unique=True),
- Index("ix_asset_references_asset_id", "asset_id"),
- Index("ix_asset_references_owner_id", "owner_id"),
- Index("ix_asset_references_name", "name"),
- Index("ix_asset_references_is_missing", "is_missing"),
- Index("ix_asset_references_enrichment_level", "enrichment_level"),
- Index("ix_asset_references_created_at", "created_at"),
- Index("ix_asset_references_last_access_time", "last_access_time"),
- Index("ix_asset_references_deleted_at", "deleted_at"),
- Index("ix_asset_references_preview_id", "preview_id"),
- Index("ix_asset_references_owner_name", "owner_id", "name"),
- CheckConstraint(
- "(mtime_ns IS NULL) OR (mtime_ns >= 0)", name="ck_ar_mtime_nonneg"
- ),
- CheckConstraint(
- "enrichment_level >= 0 AND enrichment_level <= 2",
- name="ck_ar_enrichment_level_range",
- ),
+ Index("ix_assets_content_id", "content_id"),
+ Index("ix_assets_name", "name"),
+ Index("ix_assets_created_at", "created_at"),
+ Index("ix_assets_preview_id", "preview_id"),
)
- def __repr__(self) -> str:
- path_part = f" path={self.file_path!r}" if self.file_path else ""
- return f"<AssetReference id={self.id} name={self.name!r}{path_part}>"
-
-class AssetReferenceMeta(Base):
- __tablename__ = "asset_reference_meta"
+class AssetMeta(Base):
+ __tablename__ = "asset_meta"
- asset_reference_id: Mapped[str] = mapped_column(
- String(36),
- ForeignKey("asset_references.id", ondelete="CASCADE"),
- primary_key=True,
+ asset_id: Mapped[str] = mapped_column(
+ String(36), ForeignKey("assets.id", ondelete="CASCADE"), primary_key=True
)
key: Mapped[str] = mapped_column(String(256), primary_key=True)
ordinal: Mapped[int] = mapped_column(Integer, primary_key=True, default=0)
+ val_str: Mapped[str | None] = mapped_column(String(2048))
+ val_num: Mapped[Decimal | None] = mapped_column(Numeric(38, 10))
+ val_bool: Mapped[bool | None] = mapped_column(Boolean)
+ val_json: Mapped[Any | None] = mapped_column(JSON)
- val_str: Mapped[str | None] = mapped_column(String(2048), nullable=True)
- val_num: Mapped[float | None] = mapped_column(Numeric(38, 10), nullable=True)
- val_bool: Mapped[bool | None] = mapped_column(Boolean, nullable=True)
- val_json: Mapped[Any | None] = mapped_column(JSON(none_as_null=True), nullable=True)
-
- asset_reference: Mapped[AssetReference] = relationship(
- back_populates="metadata_entries"
- )
+ asset: Mapped[Asset] = relationship(back_populates="metadata_entries")
__table_args__ = (
- Index("ix_asset_reference_meta_key", "key"),
- Index("ix_asset_reference_meta_key_val_str", "key", "val_str"),
- Index("ix_asset_reference_meta_key_val_num", "key", "val_num"),
- Index("ix_asset_reference_meta_key_val_bool", "key", "val_bool"),
+ Index("ix_asset_meta_key", "key"),
+ Index("ix_asset_meta_key_val_str", "key", "val_str"),
+ Index("ix_asset_meta_key_val_num", "key", "val_num"),
+ Index("ix_asset_meta_key_val_bool", "key", "val_bool"),
CheckConstraint(
"val_str IS NOT NULL OR val_num IS NOT NULL OR val_bool IS NOT NULL OR val_json IS NOT NULL",
- name="has_value",
+ name="ck_asset_meta_has_value",
),
)
-class AssetReferenceTag(Base):
- __tablename__ = "asset_reference_tags"
+class AssetTag(Base):
+ __tablename__ = "asset_tags"
- asset_reference_id: Mapped[str] = mapped_column(
- String(36),
- ForeignKey("asset_references.id", ondelete="CASCADE"),
- primary_key=True,
+ asset_id: Mapped[str] = mapped_column(
+ String(36), ForeignKey("assets.id", ondelete="CASCADE"), primary_key=True
)
tag_name: Mapped[str] = mapped_column(
String(512), ForeignKey("tags.name", ondelete="RESTRICT"), primary_key=True
@@ -216,12 +162,12 @@ class AssetReferenceTag(Base):
DateTime(timezone=False), nullable=False, default=get_utc_now
)
- asset_reference: Mapped[AssetReference] = relationship(back_populates="tag_links")
- tag: Mapped[Tag] = relationship(back_populates="asset_reference_links")
+ asset: Mapped[Asset] = relationship(back_populates="tag_links")
+ tag: Mapped[Tag] = relationship(back_populates="asset_links")
__table_args__ = (
- Index("ix_asset_reference_tags_tag_name", "tag_name"),
- Index("ix_asset_reference_tags_asset_reference_id", "asset_reference_id"),
+ Index("ix_asset_tags_tag_name", "tag_name"),
+ Index("ix_asset_tags_asset_id", "asset_id"),
)
@@ -229,17 +175,14 @@ class Tag(Base):
__tablename__ = "tags"
name: Mapped[str] = mapped_column(String(512), primary_key=True)
-
- asset_reference_links: Mapped[list[AssetReferenceTag]] = relationship(
- back_populates="tag",
- overlaps="asset_references,tags",
- )
- asset_references: Mapped[list[AssetReference]] = relationship(
- secondary="asset_reference_tags",
- back_populates="tags",
- viewonly=True,
- overlaps="asset_reference_links,tag_links,tags,asset_reference",
+ asset_links: Mapped[list[AssetTag]] = relationship(back_populates="tag")
+ assets: Mapped[list[Asset]] = relationship(
+ secondary="asset_tags", back_populates="tags", viewonly=True
)
- def __repr__(self) -> str:
- return f"<Tag {self.name}>"
+
+class AssetSystemState(Base):
+ __tablename__ = "asset_system_state"
+
+ key: Mapped[str] = mapped_column(String(256), primary_key=True)
+ value: Mapped[str] = mapped_column(Text, nullable=False)
diff --git a/app/assets/database/queries/__init__.py b/app/assets/database/queries/__init__.py
index 38dd82d81..8707c229b 100644
--- a/app/assets/database/queries/__init__.py
+++ b/app/assets/database/queries/__init__.py
@@ -1,139 +1,44 @@
-from app.assets.database.queries.asset import (
- asset_exists_by_hash,
- bulk_insert_assets,
- create_stub_asset,
- get_asset_by_hash,
- get_existing_asset_ids,
- reassign_asset_references,
- update_asset_hash_and_mime,
- upsert_asset,
-)
-from app.assets.database.queries.asset_reference import (
- CacheStateRow,
- UnenrichedReferenceRow,
- bulk_insert_references_ignore_conflicts,
- bulk_update_enrichment_level,
- count_active_siblings,
- bulk_update_is_missing,
- bulk_update_needs_verify,
- convert_metadata_to_rows,
- delete_assets_by_ids,
- delete_orphaned_seed_asset,
- delete_reference_by_id,
- delete_references_by_ids,
- fetch_reference_and_asset,
- fetch_reference_asset_and_tags,
- get_or_create_reference,
- get_reference_by_file_path,
- get_reference_by_id,
- get_reference_with_owner_check,
- get_reference_ids_by_ids,
- get_reference_paths_by_ids,
- get_references_by_paths_and_asset_ids,
- get_references_for_prefixes,
- get_unenriched_references,
- get_unreferenced_unhashed_asset_ids,
- insert_reference,
- list_all_file_paths_by_asset_id,
- list_references_by_asset_id,
- list_references_page,
- mark_references_missing_outside_prefixes,
- rebuild_metadata_projection,
- reference_exists,
- reference_exists_for_asset_id,
- restore_references_by_paths,
- set_reference_metadata,
- set_reference_preview,
- set_reference_system_metadata,
- soft_delete_reference_by_id,
- update_reference_access_time,
- update_reference_name,
- update_is_missing_by_asset_id,
- update_reference_timestamps,
- update_reference_updated_at,
- upsert_reference,
-)
-from app.assets.database.queries.tags import (
- AddTagsResult,
- RemoveTagsResult,
- SetTagsResult,
- add_missing_tag_for_asset_id,
- add_tags_to_reference,
- bulk_insert_tags_and_meta,
- ensure_tags_exist,
- get_reference_tags,
- list_tag_counts_for_filtered_assets,
- list_tags_with_usage,
- remove_missing_tag_for_asset_id,
- remove_tags_from_reference,
- set_reference_tags,
- validate_tags_exist,
+"""Re-exports the record and tag query functions so callers import them from one
+place instead of reaching into individual query modules. A module-level
+``__getattr__`` resolves names that live in the tag module, keeping this import
+surface flat as queries are split across more files.
+"""
+
+from importlib import import_module
+
+from app.assets.database.queries.records import (
+ create_content,
+ create_content_reporting_insert,
+ create_record,
+ delete_record,
+ fetch_record_tags,
+ get_record_by_id,
+ list_records_page,
+ mark_content_missing,
+ rename_record,
+ unset_content_missing,
+ update_record_access_time,
)
__all__ = [
- "AddTagsResult",
- "CacheStateRow",
- "RemoveTagsResult",
- "SetTagsResult",
- "UnenrichedReferenceRow",
- "add_missing_tag_for_asset_id",
- "add_tags_to_reference",
- "asset_exists_by_hash",
- "bulk_insert_assets",
- "bulk_insert_references_ignore_conflicts",
- "bulk_insert_tags_and_meta",
- "bulk_update_enrichment_level",
- "count_active_siblings",
- "create_stub_asset",
- "bulk_update_is_missing",
- "bulk_update_needs_verify",
- "convert_metadata_to_rows",
- "delete_assets_by_ids",
- "delete_orphaned_seed_asset",
- "delete_reference_by_id",
- "delete_references_by_ids",
- "ensure_tags_exist",
- "fetch_reference_and_asset",
- "fetch_reference_asset_and_tags",
- "get_asset_by_hash",
- "get_existing_asset_ids",
- "get_or_create_reference",
- "get_reference_by_file_path",
- "get_reference_by_id",
- "get_reference_with_owner_check",
- "get_reference_ids_by_ids",
- "get_reference_paths_by_ids",
- "get_reference_tags",
- "get_references_by_paths_and_asset_ids",
- "get_references_for_prefixes",
- "get_unenriched_references",
- "get_unreferenced_unhashed_asset_ids",
- "insert_reference",
- "list_all_file_paths_by_asset_id",
- "list_references_by_asset_id",
- "list_references_page",
- "list_tag_counts_for_filtered_assets",
- "list_tags_with_usage",
- "mark_references_missing_outside_prefixes",
- "reassign_asset_references",
- "rebuild_metadata_projection",
- "reference_exists",
- "reference_exists_for_asset_id",
- "remove_missing_tag_for_asset_id",
- "remove_tags_from_reference",
- "restore_references_by_paths",
- "set_reference_metadata",
- "set_reference_preview",
- "set_reference_system_metadata",
- "soft_delete_reference_by_id",
- "set_reference_tags",
- "update_asset_hash_and_mime",
- "update_is_missing_by_asset_id",
- "update_reference_access_time",
- "update_reference_name",
- "update_reference_timestamps",
- "update_reference_updated_at",
- "upsert_asset",
- "upsert_reference",
- "validate_tags_exist",
+ "create_content",
+ "create_content_reporting_insert",
+ "create_record",
+ "delete_record",
+ "fetch_record_tags",
+ "get_record_by_id",
+ "list_records_page",
+ "mark_content_missing",
+ "rename_record",
+ "unset_content_missing",
+ "update_record_access_time",
]
+
+
+def __getattr__(name: str):
+ for module_name in ("tags",):
+ module = import_module(f"app.assets.database.queries.{module_name}")
+ candidate = getattr(module, name, None)
+ if candidate is not None:
+ return candidate
+ raise AttributeError(name)
diff --git a/app/assets/database/queries/asset.py b/app/assets/database/queries/asset.py
deleted file mode 100644
index cc7168431..000000000
--- a/app/assets/database/queries/asset.py
+++ /dev/null
@@ -1,152 +0,0 @@
-import sqlalchemy as sa
-from sqlalchemy import select
-from sqlalchemy.dialects import sqlite
-from sqlalchemy.orm import Session
-
-from app.assets.database.models import Asset, AssetReference
-from app.assets.database.queries.common import MAX_BIND_PARAMS, calculate_rows_per_statement, iter_chunks
-
-
-def asset_exists_by_hash(
- session: Session,
- asset_hash: str,
-) -> bool:
- """
- Check if an asset with a given hash exists in database.
- """
- row = (
- session.execute(
- select(sa.literal(True))
- .select_from(Asset)
- .where(Asset.hash == asset_hash)
- .limit(1)
- )
- ).first()
- return row is not None
-
-
-def get_asset_by_hash(
- session: Session,
- asset_hash: str,
-) -> Asset | None:
- return (
- (session.execute(select(Asset).where(Asset.hash == asset_hash).limit(1)))
- .scalars()
- .first()
- )
-
-
-def upsert_asset(
- session: Session,
- asset_hash: str,
- size_bytes: int,
- mime_type: str | None = None,
-) -> tuple[Asset, bool, bool]:
- """Upsert an Asset by hash. Returns (asset, created, updated)."""
- vals = {"hash": asset_hash, "size_bytes": int(size_bytes)}
- if mime_type:
- vals["mime_type"] = mime_type
-
- ins = (
- sqlite.insert(Asset)
- .values(**vals)
- .on_conflict_do_nothing(index_elements=[Asset.hash])
- )
- res = session.execute(ins)
- created = int(res.rowcount or 0) > 0
-
- asset = (
- session.execute(select(Asset).where(Asset.hash == asset_hash).limit(1))
- .scalars()
- .first()
- )
- if not asset:
- raise RuntimeError("Asset row not found after upsert.")
-
- updated = False
- if not created:
- changed = False
- if asset.size_bytes != int(size_bytes) and int(size_bytes) > 0:
- asset.size_bytes = int(size_bytes)
- changed = True
- if mime_type and not asset.mime_type:
- asset.mime_type = mime_type
- changed = True
- if changed:
- updated = True
-
- return asset, created, updated
-
-
-def create_stub_asset(
- session: Session,
- size_bytes: int,
- mime_type: str | None = None,
-) -> Asset:
- """Create a new asset with no hash (stub for later enrichment)."""
- asset = Asset(size_bytes=size_bytes, mime_type=mime_type, hash=None)
- session.add(asset)
- session.flush()
- return asset
-
-
-def bulk_insert_assets(
- session: Session,
- rows: list[dict],
-) -> None:
- """Bulk insert Asset rows with ON CONFLICT DO NOTHING on hash."""
- if not rows:
- return
- ins = sqlite.insert(Asset).on_conflict_do_nothing(index_elements=[Asset.hash])
- for chunk in iter_chunks(rows, calculate_rows_per_statement(5)):
- session.execute(ins, chunk)
-
-
-def get_existing_asset_ids(
- session: Session,
- asset_ids: list[str],
-) -> set[str]:
- """Return the subset of asset_ids that exist in the database."""
- if not asset_ids:
- return set()
- found: set[str] = set()
- for chunk in iter_chunks(asset_ids, MAX_BIND_PARAMS):
- rows = session.execute(
- select(Asset.id).where(Asset.id.in_(chunk))
- ).fetchall()
- found.update(row[0] for row in rows)
- return found
-
-
-def update_asset_hash_and_mime(
- session: Session,
- asset_id: str,
- asset_hash: str | None = None,
- mime_type: str | None = None,
-) -> bool:
- """Update asset hash and/or mime_type. Returns True if asset was found."""
- asset = session.get(Asset, asset_id)
- if not asset:
- return False
- if asset_hash is not None:
- asset.hash = asset_hash
- if mime_type is not None and not asset.mime_type:
- asset.mime_type = mime_type
- return True
-
-
-def reassign_asset_references(
- session: Session,
- from_asset_id: str,
- to_asset_id: str,
- reference_id: str,
-) -> None:
- """Reassign a reference from one asset to another.
-
- Used when merging a stub asset into an existing asset with the same hash.
- """
- ref = session.get(AssetReference, reference_id)
- if ref and ref.asset_id == from_asset_id:
- ref.asset_id = to_asset_id
-
- session.flush()
diff --git a/app/assets/database/queries/asset_reference.py b/app/assets/database/queries/asset_reference.py
deleted file mode 100644
index 13220270b..000000000
--- a/app/assets/database/queries/asset_reference.py
+++ /dev/null
@@ -1,1102 +0,0 @@
-"""Query functions for the unified AssetReference table.
-
-This module replaces the separate asset_info.py and cache_state.py query modules,
-providing a unified interface for the merged asset_references table.
-"""
-
-from collections import defaultdict
-from datetime import datetime
-from decimal import Decimal
-from typing import NamedTuple, Sequence
-
-import sqlalchemy as sa
-from sqlalchemy import delete, select
-from sqlalchemy.dialects import sqlite
-from sqlalchemy.exc import IntegrityError
-from sqlalchemy.orm import Session, noload
-
-from app.assets.database.models import (
- Asset,
- AssetReference,
- AssetReferenceMeta,
- AssetReferenceTag,
- Tag,
-)
-from app.assets.database.queries.common import (
- MAX_BIND_PARAMS,
- apply_metadata_filter,
- apply_tag_filters,
- build_prefix_like_conditions,
- build_visible_owner_clause,
- calculate_rows_per_statement,
- iter_chunks,
-)
-from app.assets.helpers import escape_sql_like_string, get_utc_now
-
-
-def _check_is_scalar(v):
- if v is None:
- return True
- if isinstance(v, bool):
- return True
- if isinstance(v, (int, float, Decimal, str)):
- return True
- return False
-
-
-def _scalar_to_row(key: str, ordinal: int, value) -> dict:
- """Convert a scalar value to a typed projection row."""
- if isinstance(value, bool):
- return {"key": key, "ordinal": ordinal, "val_bool": bool(value)}
- if isinstance(value, (int, float, Decimal)):
- num = value if isinstance(value, Decimal) else Decimal(str(value))
- return {"key": key, "ordinal": ordinal, "val_num": num}
- if isinstance(value, str):
- return {"key": key, "ordinal": ordinal, "val_str": value}
- return {"key": key, "ordinal": ordinal, "val_json": value}
-
-
-def convert_metadata_to_rows(key: str, value) -> list[dict]:
- """Turn a metadata key/value into typed projection rows."""
- if value is None:
- return []
-
- if _check_is_scalar(value):
- return [_scalar_to_row(key, 0, value)]
-
- if isinstance(value, list):
- if all(_check_is_scalar(x) for x in value):
- return [_scalar_to_row(key, i, x) for i, x in enumerate(value) if x is not None]
- return [{"key": key, "ordinal": i, "val_json": x} for i, x in enumerate(value) if x is not None]
-
- return [{"key": key, "ordinal": 0, "val_json": value}]
-
-
-
-
-def get_reference_by_id(
- session: Session,
- reference_id: str,
-) -> AssetReference | None:
- return session.get(AssetReference, reference_id)
-
-
-def get_reference_with_owner_check(
- session: Session,
- reference_id: str,
- owner_id: str,
-) -> AssetReference:
- """Fetch a reference and verify ownership.
-
- Raises:
- ValueError: if reference not found or soft-deleted
- PermissionError: if owner_id doesn't match
- """
- ref = get_reference_by_id(session, reference_id=reference_id)
- if not ref or ref.deleted_at is not None:
- raise ValueError(f"AssetReference {reference_id} not found")
- if ref.owner_id and ref.owner_id != owner_id:
- raise PermissionError("not owner")
- return ref
-
-
-def get_reference_by_file_path(
- session: Session,
- file_path: str,
-) -> AssetReference | None:
- """Get a reference by its file path."""
- return (
- session.execute(
- select(AssetReference).where(AssetReference.file_path == file_path).limit(1)
- )
- .scalars()
- .first()
- )
-
-
-def count_active_siblings(
- session: Session,
- asset_id: str,
- exclude_reference_id: str,
-) -> int:
- """Count active (non-deleted) references to an asset, excluding one reference."""
- return (
- session.query(AssetReference)
- .filter(
- AssetReference.asset_id == asset_id,
- AssetReference.id != exclude_reference_id,
- AssetReference.deleted_at.is_(None),
- )
- .count()
- )
-
-
-def reference_exists_for_asset_id(
- session: Session,
- asset_id: str,
-) -> bool:
- q = (
- select(sa.literal(True))
- .select_from(AssetReference)
- .where(AssetReference.asset_id == asset_id)
- .where(AssetReference.deleted_at.is_(None))
- .limit(1)
- )
- return session.execute(q).first() is not None
-
-
-def reference_exists(
- session: Session,
- reference_id: str,
-) -> bool:
- """Return True if a reference with the given ID exists (not soft-deleted)."""
- q = (
- select(sa.literal(True))
- .select_from(AssetReference)
- .where(AssetReference.id == reference_id)
- .where(AssetReference.deleted_at.is_(None))
- .limit(1)
- )
- return session.execute(q).first() is not None
-
-
-def insert_reference(
- session: Session,
- asset_id: str,
- name: str,
- owner_id: str = "",
- file_path: str | None = None,
- mtime_ns: int | None = None,
- preview_id: str | None = None,
-) -> AssetReference | None:
- """Insert a new AssetReference. Returns None if unique constraint violated."""
- now = get_utc_now()
- try:
- with session.begin_nested():
- ref = AssetReference(
- asset_id=asset_id,
- name=name,
- owner_id=owner_id,
- file_path=file_path,
- mtime_ns=mtime_ns,
- preview_id=preview_id,
- created_at=now,
- updated_at=now,
- last_access_time=now,
- )
- session.add(ref)
- session.flush()
- return ref
- except IntegrityError:
- return None
-
-
-def get_or_create_reference(
- session: Session,
- asset_id: str,
- name: str,
- owner_id: str = "",
- file_path: str | None = None,
- mtime_ns: int | None = None,
- preview_id: str | None = None,
-) -> tuple[AssetReference, bool]:
- """Get existing or create new AssetReference.
-
- For filesystem references (file_path is set), uniqueness is by file_path.
- For API references (file_path is None), we look for matching
- asset_id + owner_id + name.
-
- Returns (reference, created).
- """
- ref = insert_reference(
- session,
- asset_id=asset_id,
- name=name,
- owner_id=owner_id,
- file_path=file_path,
- mtime_ns=mtime_ns,
- preview_id=preview_id,
- )
- if ref:
- return ref, True
-
- # Find existing - priority to file_path match, then name match
- if file_path:
- existing = get_reference_by_file_path(session, file_path)
- else:
- existing = (
- session.execute(
- select(AssetReference)
- .where(
- AssetReference.asset_id == asset_id,
- AssetReference.name == name,
- AssetReference.owner_id == owner_id,
- AssetReference.file_path.is_(None),
- )
- .limit(1)
- )
- .unique()
- .scalar_one_or_none()
- )
- if not existing:
- raise RuntimeError("Failed to find AssetReference after insert conflict.")
- return existing, False
-
-
-def update_reference_timestamps(
- session: Session,
- reference: AssetReference,
- preview_id: str | None = None,
-) -> None:
- """Update timestamps and optionally preview_id on existing AssetReference."""
- now = get_utc_now()
- if preview_id and reference.preview_id != preview_id:
- reference.preview_id = preview_id
- reference.updated_at = now
-
-
-def list_references_page(
- session: Session,
- owner_id: str = "",
- limit: int = 100,
- offset: int = 0,
- name_contains: str | None = None,
- include_tags: Sequence[str] | None = None,
- exclude_tags: Sequence[str] | None = None,
- metadata_filter: dict | None = None,
- sort: str | None = None,
- order: str | None = None,
- after_cursor_value: object | None = None,
- after_cursor_id: str | None = None,
- # Appended last so pre-existing positional callers keep binding correctly.
- any_tags: Sequence[str] | None = None,
-) -> tuple[list[AssetReference], dict[str, list[str]], int]:
- """List references with pagination, filtering, and sorting.
-
- When ``after_cursor_value``/``after_cursor_id`` are supplied the query uses
- keyset pagination — ``offset`` is ignored and a WHERE clause selects rows
- strictly after the given ``(sort_col, id)`` position in the active sort
- direction. The cursor value must already be typed for the column
- (datetime for time sorts, int for size, str for name); the caller decodes
- the opaque cursor string and resolves to the typed value.
-
- Returns (references, tag_map, total_count).
- """
- base = (
- select(AssetReference)
- .join(Asset, Asset.id == AssetReference.asset_id)
- .where(build_visible_owner_clause(owner_id))
- .where(AssetReference.is_missing == False) # noqa: E712
- .where(AssetReference.deleted_at.is_(None))
- .options(noload(AssetReference.tags))
- )
-
- if name_contains:
- escaped, esc = escape_sql_like_string(name_contains)
- base = base.where(AssetReference.name.ilike(f"%{escaped}%", escape=esc))
-
- base = apply_tag_filters(base, include_tags, exclude_tags, any_tags)
- base = apply_metadata_filter(base, metadata_filter)
-
- sort = (sort or "created_at").lower()
- order = (order or "desc").lower()
- sort_map = {
- "name": AssetReference.name,
- "created_at": AssetReference.created_at,
- "updated_at": AssetReference.updated_at,
- "last_access_time": AssetReference.last_access_time,
- "size": Asset.size_bytes,
- }
- sort_col = sort_map.get(sort, AssetReference.created_at)
- descending = order == "desc"
-
- # Keyset WHERE: (sort_col, id) strictly less-than / greater-than the cursor.
- # Equivalent to: sort_col <op> v OR (sort_col = v AND id <op> cursor_id).
- if after_cursor_value is not None and after_cursor_id is not None:
- if descending:
- keyset = sa.or_(
- sort_col < after_cursor_value,
- sa.and_(sort_col == after_cursor_value, AssetReference.id < after_cursor_id),
- )
- else:
- keyset = sa.or_(
- sort_col > after_cursor_value,
- sa.and_(sort_col == after_cursor_value, AssetReference.id > after_cursor_id),
- )
- base = base.where(keyset)
-
- # Secondary ORDER BY id (matching the primary direction) gives the keyset
- # comparison a deterministic tiebreaker on duplicate sort_col values.
- id_exp = AssetReference.id.desc() if descending else AssetReference.id.asc()
- sort_exp = sort_col.desc() if descending else sort_col.asc()
-
- base = base.order_by(sort_exp, id_exp).limit(limit)
- if after_cursor_id is None:
- base = base.offset(offset)
-
- count_stmt = (
- select(sa.func.count())
- .select_from(AssetReference)
- .join(Asset, Asset.id == AssetReference.asset_id)
- .where(build_visible_owner_clause(owner_id))
- .where(AssetReference.is_missing == False) # noqa: E712
- .where(AssetReference.deleted_at.is_(None))
- )
- if name_contains:
- escaped, esc = escape_sql_like_string(name_contains)
- count_stmt = count_stmt.where(
- AssetReference.name.ilike(f"%{escaped}%", escape=esc)
- )
- count_stmt = apply_tag_filters(count_stmt, include_tags, exclude_tags, any_tags)
- count_stmt = apply_metadata_filter(count_stmt, metadata_filter)
-
- total = int(session.execute(count_stmt).scalar_one() or 0)
- refs = session.execute(base).unique().scalars().all()
-
- id_list: list[str] = [r.id for r in refs]
- tag_map: dict[str, list[str]] = defaultdict(list)
- if id_list:
- rows = session.execute(
- select(AssetReferenceTag.asset_reference_id, Tag.name)
- .join(Tag, Tag.name == AssetReferenceTag.tag_name)
- .where(AssetReferenceTag.asset_reference_id.in_(id_list))
- .order_by(AssetReferenceTag.tag_name.asc())
- )
- for ref_id, tag_name in rows.all():
- tag_map[ref_id].append(tag_name)
-
- return list(refs), tag_map, total
-
-
-def fetch_reference_asset_and_tags(
- session: Session,
- reference_id: str,
- owner_id: str = "",
-) -> tuple[AssetReference, Asset, list[str]] | None:
- stmt = (
- select(AssetReference, Asset, Tag.name)
- .join(Asset, Asset.id == AssetReference.asset_id)
- .join(
- AssetReferenceTag,
- AssetReferenceTag.asset_reference_id == AssetReference.id,
- isouter=True,
- )
- .join(Tag, Tag.name == AssetReferenceTag.tag_name, isouter=True)
- .where(
- AssetReference.id == reference_id,
- AssetReference.deleted_at.is_(None),
- build_visible_owner_clause(owner_id),
- )
- .options(noload(AssetReference.tags))
- .order_by(Tag.name.asc())
- )
-
- rows = session.execute(stmt).all()
- if not rows:
- return None
-
- first_ref, first_asset, _ = rows[0]
- tags: list[str] = []
- seen: set[str] = set()
- for _ref, _asset, tag_name in rows:
- if tag_name and tag_name not in seen:
- seen.add(tag_name)
- tags.append(tag_name)
- return first_ref, first_asset, tags
-
-
-def fetch_reference_and_asset(
- session: Session,
- reference_id: str,
- owner_id: str = "",
-) -> tuple[AssetReference, Asset] | None:
- stmt = (
- select(AssetReference, Asset)
- .join(Asset, Asset.id == AssetReference.asset_id)
- .where(
- AssetReference.id == reference_id,
- AssetReference.deleted_at.is_(None),
- build_visible_owner_clause(owner_id),
- )
- .limit(1)
- .options(noload(AssetReference.tags))
- )
- pair = session.execute(stmt).first()
- if not pair:
- return None
- return pair[0], pair[1]
-
-
-def update_reference_access_time(
- session: Session,
- reference_id: str,
- ts: datetime | None = None,
- only_if_newer: bool = True,
-) -> None:
- ts = ts or get_utc_now()
- stmt = sa.update(AssetReference).where(AssetReference.id == reference_id)
- if only_if_newer:
- stmt = stmt.where(
- sa.or_(
- AssetReference.last_access_time.is_(None),
- AssetReference.last_access_time < ts,
- )
- )
- session.execute(stmt.values(last_access_time=ts))
-
-
-def update_reference_name(
- session: Session,
- reference_id: str,
- name: str,
-) -> None:
- """Update the name of an AssetReference."""
- now = get_utc_now()
- session.execute(
- sa.update(AssetReference)
- .where(AssetReference.id == reference_id)
- .values(name=name, updated_at=now)
- )
-
-
-def update_reference_updated_at(
- session: Session,
- reference_id: str,
- ts: datetime | None = None,
-) -> None:
- """Update the updated_at timestamp of an AssetReference."""
- ts = ts or get_utc_now()
- session.execute(
- sa.update(AssetReference)
- .where(AssetReference.id == reference_id)
- .values(updated_at=ts)
- )
-
-
-def rebuild_metadata_projection(session: Session, ref: AssetReference) -> None:
- """Delete and rebuild AssetReferenceMeta rows from merged system+user metadata.
-
- The merged dict is ``{**system_metadata, **user_metadata}`` so user keys
- override system keys of the same name.
- """
- session.execute(
- delete(AssetReferenceMeta).where(
- AssetReferenceMeta.asset_reference_id == ref.id
- )
- )
- session.flush()
-
- merged = {**(ref.system_metadata or {}), **(ref.user_metadata or {})}
- if not merged:
- return
-
- rows: list[AssetReferenceMeta] = []
- for k, v in merged.items():
- for r in convert_metadata_to_rows(k, v):
- rows.append(
- AssetReferenceMeta(
- asset_reference_id=ref.id,
- key=r["key"],
- ordinal=int(r["ordinal"]),
- val_str=r.get("val_str"),
- val_num=r.get("val_num"),
- val_bool=r.get("val_bool"),
- val_json=r.get("val_json"),
- )
- )
- if rows:
- session.add_all(rows)
- session.flush()
-
-
-def set_reference_metadata(
- session: Session,
- reference_id: str,
- user_metadata: dict | None = None,
-) -> None:
- ref = session.get(AssetReference, reference_id)
- if not ref:
- raise ValueError(f"AssetReference {reference_id} not found")
-
- ref.user_metadata = user_metadata or {}
- ref.updated_at = get_utc_now()
- session.flush()
-
- rebuild_metadata_projection(session, ref)
-
-
-def set_reference_system_metadata(
- session: Session,
- reference_id: str,
- system_metadata: dict | None = None,
-) -> None:
- """Set system_metadata on a reference and rebuild the merged projection."""
- ref = session.get(AssetReference, reference_id)
- if not ref:
- raise ValueError(f"AssetReference {reference_id} not found")
-
- ref.system_metadata = system_metadata or {}
- ref.updated_at = get_utc_now()
- session.flush()
-
- rebuild_metadata_projection(session, ref)
-
-
-def delete_reference_by_id(
- session: Session,
- reference_id: str,
- owner_id: str,
-) -> bool:
- stmt = sa.delete(AssetReference).where(
- AssetReference.id == reference_id,
- build_visible_owner_clause(owner_id),
- )
- return int(session.execute(stmt).rowcount or 0) > 0
-
-
-def soft_delete_reference_by_id(
- session: Session,
- reference_id: str,
- owner_id: str,
-) -> bool:
- """Mark a reference as soft-deleted by setting deleted_at timestamp.
-
- Returns True if the reference was found and marked deleted.
- """
- now = get_utc_now()
- stmt = (
- sa.update(AssetReference)
- .where(
- AssetReference.id == reference_id,
- AssetReference.deleted_at.is_(None),
- build_visible_owner_clause(owner_id),
- )
- .values(deleted_at=now)
- )
- return int(session.execute(stmt).rowcount or 0) > 0
-
-
-def set_reference_preview(
- session: Session,
- reference_id: str,
- preview_reference_id: str | None = None,
-) -> None:
- """Set or clear preview_id and bump updated_at. Raises on unknown IDs."""
- ref = session.get(AssetReference, reference_id)
- if not ref:
- raise ValueError(f"AssetReference {reference_id} not found")
-
- if preview_reference_id is None:
- ref.preview_id = None
- else:
- if not session.get(AssetReference, preview_reference_id):
- raise ValueError(f"Preview AssetReference {preview_reference_id} not found")
- ref.preview_id = preview_reference_id
-
- ref.updated_at = get_utc_now()
- session.flush()
-
-
-class CacheStateRow(NamedTuple):
- """Row from reference query with cache state data."""
-
- reference_id: str
- file_path: str
- mtime_ns: int | None
- needs_verify: bool
- asset_id: str
- asset_hash: str | None
- size_bytes: int | None
-
-
-def list_references_by_asset_id(
- session: Session,
- asset_id: str,
-) -> Sequence[AssetReference]:
- return (
- session.execute(
- select(AssetReference)
- .where(AssetReference.asset_id == asset_id)
- .where(AssetReference.is_missing == False) # noqa: E712
- .where(AssetReference.deleted_at.is_(None))
- .order_by(AssetReference.id.asc())
- )
- .scalars()
- .all()
- )
-
-
-def list_all_file_paths_by_asset_id(
- session: Session,
- asset_id: str,
-) -> list[str]:
- """Return every file_path for an asset, including soft-deleted/missing refs.
-
- Used for orphan cleanup where all on-disk files must be removed.
- """
- return list(
- session.execute(
- select(AssetReference.file_path)
- .where(AssetReference.asset_id == asset_id)
- .where(AssetReference.file_path.isnot(None))
- )
- .scalars()
- .all()
- )
-
-
-def upsert_reference(
- session: Session,
- asset_id: str,
- file_path: str,
- name: str,
- mtime_ns: int,
- owner_id: str = "",
- loader_path: str | None = None,
-) -> tuple[bool, bool]:
- """Upsert a reference by file_path. Returns (created, updated).
-
- Also restores references that were previously marked as missing.
- """
- now = get_utc_now()
- vals = {
- "asset_id": asset_id,
- "file_path": file_path,
- "loader_path": loader_path,
- "name": name,
- "owner_id": owner_id,
- "mtime_ns": int(mtime_ns),
- "is_missing": False,
- "created_at": now,
- "updated_at": now,
- "last_access_time": now,
- }
- ins = (
- sqlite.insert(AssetReference)
- .values(**vals)
- .on_conflict_do_nothing(index_elements=[AssetReference.file_path])
- )
- res = session.execute(ins)
- created = int(res.rowcount or 0) > 0
-
- if created:
- return True, False
-
- upd = (
- sa.update(AssetReference)
- .where(AssetReference.file_path == file_path)
- .where(
- sa.or_(
- AssetReference.asset_id != asset_id,
- AssetReference.mtime_ns.is_(None),
- AssetReference.mtime_ns != int(mtime_ns),
- AssetReference.loader_path.is_distinct_from(loader_path),
- AssetReference.is_missing == True, # noqa: E712
- AssetReference.deleted_at.isnot(None),
- )
- )
- .values(
- asset_id=asset_id, mtime_ns=int(mtime_ns), loader_path=loader_path,
- is_missing=False, deleted_at=None, updated_at=now,
- )
- )
- res2 = session.execute(upd)
- updated = int(res2.rowcount or 0) > 0
- return False, updated
-
-
-def mark_references_missing_outside_prefixes(
- session: Session,
- valid_prefixes: list[str],
-) -> int:
- """Mark references as missing when file_path doesn't match any valid prefix.
-
- Returns number of references marked as missing.
- """
- if not valid_prefixes:
- return 0
-
- conds = build_prefix_like_conditions(valid_prefixes)
- matches_valid_prefix = sa.or_(*conds)
- result = session.execute(
- sa.update(AssetReference)
- .where(AssetReference.file_path.isnot(None))
- .where(AssetReference.deleted_at.is_(None))
- .where(~matches_valid_prefix)
- .where(AssetReference.is_missing == False) # noqa: E712
- .values(is_missing=True)
- )
- return result.rowcount
-
-
-def restore_references_by_paths(session: Session, file_paths: list[str]) -> int:
- """Restore references that were previously marked as missing.
-
- Returns number of references restored.
- """
- if not file_paths:
- return 0
-
- total = 0
- for chunk in iter_chunks(file_paths, MAX_BIND_PARAMS):
- result = session.execute(
- sa.update(AssetReference)
- .where(AssetReference.file_path.in_(chunk))
- .where(AssetReference.is_missing == True) # noqa: E712
- .where(AssetReference.deleted_at.is_(None))
- .values(is_missing=False)
- )
- total += result.rowcount
- return total
-
-
-def get_unreferenced_unhashed_asset_ids(session: Session) -> list[str]:
- """Get IDs of unhashed assets (hash=None) with no active references.
-
- An asset is considered unreferenced if it has no references,
- or all its references are marked as missing.
-
- Returns list of asset IDs that are unreferenced.
- """
- active_ref_exists = (
- sa.select(sa.literal(1))
- .where(AssetReference.asset_id == Asset.id)
- .where(AssetReference.is_missing == False) # noqa: E712
- .where(AssetReference.deleted_at.is_(None))
- .correlate(Asset)
- .exists()
- )
- unreferenced_subq = sa.select(Asset.id).where(
- Asset.hash.is_(None), ~active_ref_exists
- )
- return [row[0] for row in session.execute(unreferenced_subq).all()]
-
-
-def delete_assets_by_ids(session: Session, asset_ids: list[str]) -> int:
- """Delete assets and their references by ID.
-
- Returns number of assets deleted.
- """
- if not asset_ids:
- return 0
- total = 0
- for chunk in iter_chunks(asset_ids, MAX_BIND_PARAMS):
- session.execute(
- sa.delete(AssetReference).where(AssetReference.asset_id.in_(chunk))
- )
- result = session.execute(sa.delete(Asset).where(Asset.id.in_(chunk)))
- total += result.rowcount
- return total
-
-
-def get_references_for_prefixes(
- session: Session,
- prefixes: list[str],
- *,
- include_missing: bool = False,
-) -> list[CacheStateRow]:
- """Get all references with file paths matching any of the given prefixes.
-
- Args:
- session: Database session
- prefixes: List of absolute directory prefixes to match
- include_missing: If False (default), exclude references marked as missing
-
- Returns:
- List of cache state rows with joined asset data
- """
- if not prefixes:
- return []
-
- conds = build_prefix_like_conditions(prefixes)
-
- query = (
- sa.select(
- AssetReference.id,
- AssetReference.file_path,
- AssetReference.mtime_ns,
- AssetReference.needs_verify,
- AssetReference.asset_id,
- Asset.hash,
- Asset.size_bytes,
- )
- .join(Asset, Asset.id == AssetReference.asset_id)
- .where(AssetReference.file_path.isnot(None))
- .where(AssetReference.deleted_at.is_(None))
- .where(sa.or_(*conds))
- )
-
- if not include_missing:
- query = query.where(AssetReference.is_missing == False) # noqa: E712
-
- rows = session.execute(
- query.order_by(AssetReference.asset_id.asc(), AssetReference.id.asc())
- ).all()
-
- return [
- CacheStateRow(
- reference_id=row[0],
- file_path=row[1],
- mtime_ns=row[2],
- needs_verify=row[3],
- asset_id=row[4],
- asset_hash=row[5],
- size_bytes=int(row[6]) if row[6] is not None else None,
- )
- for row in rows
- ]
-
-
-def bulk_update_needs_verify(
- session: Session, reference_ids: list[str], value: bool
-) -> int:
- """Set needs_verify flag for multiple references.
-
- Returns: Number of rows updated
- """
- if not reference_ids:
- return 0
- total = 0
- for chunk in iter_chunks(reference_ids, MAX_BIND_PARAMS):
- result = session.execute(
- sa.update(AssetReference)
- .where(AssetReference.id.in_(chunk))
- .values(needs_verify=value)
- )
- total += result.rowcount
- return total
-
-
-def bulk_update_is_missing(
- session: Session, reference_ids: list[str], value: bool
-) -> int:
- """Set is_missing flag for multiple references.
-
- Returns: Number of rows updated
- """
- if not reference_ids:
- return 0
- total = 0
- for chunk in iter_chunks(reference_ids, MAX_BIND_PARAMS):
- result = session.execute(
- sa.update(AssetReference)
- .where(AssetReference.id.in_(chunk))
- .values(is_missing=value)
- )
- total += result.rowcount
- return total
-
-
-def update_is_missing_by_asset_id(
- session: Session, asset_id: str, value: bool
-) -> int:
- """Set is_missing flag for ALL references belonging to an asset.
-
- Returns: Number of rows updated
- """
- result = session.execute(
- sa.update(AssetReference)
- .where(AssetReference.asset_id == asset_id)
- .where(AssetReference.deleted_at.is_(None))
- .values(is_missing=value)
- )
- return result.rowcount
-
-
-def delete_references_by_ids(session: Session, reference_ids: list[str]) -> int:
- """Delete references by their IDs.
-
- Returns: Number of rows deleted
- """
- if not reference_ids:
- return 0
- total = 0
- for chunk in iter_chunks(reference_ids, MAX_BIND_PARAMS):
- result = session.execute(
- sa.delete(AssetReference).where(AssetReference.id.in_(chunk))
- )
- total += result.rowcount
- return total
-
-
-def delete_orphaned_seed_asset(session: Session, asset_id: str) -> bool:
- """Delete a seed asset (hash is None) and its references.
-
- Returns: True if asset was deleted, False if not found or has a hash
- """
- asset = session.get(Asset, asset_id)
- if not asset:
- return False
- if asset.hash is not None:
- return False
- session.execute(
- sa.delete(AssetReference).where(AssetReference.asset_id == asset_id)
- )
- session.delete(asset)
- return True
-
-
-class UnenrichedReferenceRow(NamedTuple):
- """Row for references needing enrichment."""
-
- reference_id: str
- asset_id: str
- file_path: str
- enrichment_level: int
-
-
-def get_unenriched_references(
- session: Session,
- prefixes: list[str],
- max_level: int = 0,
- limit: int = 1000,
-) -> list[UnenrichedReferenceRow]:
- """Get references that need enrichment (enrichment_level <= max_level).
-
- Args:
- session: Database session
- prefixes: List of absolute directory prefixes to scan
- max_level: Maximum enrichment level to include (0=stubs, 1=metadata done)
- limit: Maximum number of rows to return
-
- Returns:
- List of unenriched reference rows with file paths
- """
- if not prefixes:
- return []
-
- conds = build_prefix_like_conditions(prefixes)
-
- query = (
- sa.select(
- AssetReference.id,
- AssetReference.asset_id,
- AssetReference.file_path,
- AssetReference.enrichment_level,
- )
- .where(AssetReference.file_path.isnot(None))
- .where(AssetReference.deleted_at.is_(None))
- .where(sa.or_(*conds))
- .where(AssetReference.is_missing == False) # noqa: E712
- .where(AssetReference.enrichment_level <= max_level)
- .order_by(AssetReference.id.asc())
- .limit(limit)
- )
-
- rows = session.execute(query).all()
- return [
- UnenrichedReferenceRow(
- reference_id=row[0],
- asset_id=row[1],
- file_path=row[2],
- enrichment_level=row[3],
- )
- for row in rows
- ]
-
-
-def bulk_update_enrichment_level(
- session: Session,
- reference_ids: list[str],
- level: int,
-) -> int:
- """Update enrichment level for multiple references.
-
- Returns: Number of rows updated
- """
- if not reference_ids:
- return 0
- result = session.execute(
- sa.update(AssetReference)
- .where(AssetReference.id.in_(reference_ids))
- .values(enrichment_level=level)
- )
- return result.rowcount
-
-
-def bulk_insert_references_ignore_conflicts(
- session: Session,
- rows: list[dict],
-) -> None:
- """Bulk insert reference rows with ON CONFLICT DO NOTHING on file_path.
-
- Each dict should have: id, asset_id, file_path, name, owner_id, mtime_ns, etc.
- The is_missing field is automatically set to False for new inserts.
- """
- if not rows:
- return
- enriched_rows = [{**row, "is_missing": False} for row in rows]
- ins = sqlite.insert(AssetReference).on_conflict_do_nothing(
- index_elements=[AssetReference.file_path]
- )
- for chunk in iter_chunks(enriched_rows, calculate_rows_per_statement(14)):
- session.execute(ins, chunk)
-
-
-def get_references_by_paths_and_asset_ids(
- session: Session,
- path_to_asset: dict[str, str],
-) -> set[str]:
- """Query references to find paths where our asset_id won the insert.
-
- Args:
- path_to_asset: Mapping of file_path -> asset_id we tried to insert
-
- Returns:
- Set of file_paths where our asset_id is present
- """
- if not path_to_asset:
- return set()
-
- pairs = list(path_to_asset.items())
- winners: set[str] = set()
-
- # Each pair uses 2 bind params, so chunk at MAX_BIND_PARAMS // 2
- for chunk in iter_chunks(pairs, MAX_BIND_PARAMS // 2):
- pairwise = sa.tuple_(AssetReference.file_path, AssetReference.asset_id).in_(
- chunk
- )
- result = session.execute(
- select(AssetReference.file_path).where(pairwise)
- )
- winners.update(result.scalars().all())
-
- return winners
-
-
-def get_reference_paths_by_ids(
- session: Session,
- reference_ids: list[str],
-) -> dict[str, str]:
- """Map reference id -> file_path for live, file-backed references."""
- if not reference_ids:
- return {}
-
- paths: dict[str, str] = {}
- for chunk in iter_chunks(reference_ids, MAX_BIND_PARAMS):
- rows = session.execute(
- select(AssetReference.id, AssetReference.file_path).where(
- AssetReference.id.in_(chunk),
- AssetReference.file_path.is_not(None),
- AssetReference.deleted_at.is_(None),
- )
- )
- paths.update({rid: fp for rid, fp in rows})
- return paths
-
-
-def get_reference_ids_by_ids(
- session: Session,
- reference_ids: list[str],
-) -> set[str]:
- """Query to find which reference IDs exist in the database."""
- if not reference_ids:
- return set()
-
- found: set[str] = set()
- for chunk in iter_chunks(reference_ids, MAX_BIND_PARAMS):
- result = session.execute(
- select(AssetReference.id).where(AssetReference.id.in_(chunk))
- )
- found.update(result.scalars().all())
- return found
diff --git a/app/assets/database/queries/common.py b/app/assets/database/queries/common.py
deleted file mode 100644
index 7b0c211a0..000000000
--- a/app/assets/database/queries/common.py
+++ /dev/null
@@ -1,138 +0,0 @@
-"""Shared utilities for database query modules."""
-
-import os
-from decimal import Decimal
-from typing import Iterable, Sequence
-
-import sqlalchemy as sa
-from sqlalchemy import exists
-
-from app.assets.database.models import AssetReference, AssetReferenceMeta, AssetReferenceTag
-from app.assets.helpers import escape_sql_like_string, normalize_tags
-
-MAX_BIND_PARAMS = 800
-
-
-def calculate_rows_per_statement(cols: int) -> int:
- """Calculate how many rows can fit in one statement given column count."""
- return max(1, MAX_BIND_PARAMS // max(1, cols))
-
-
-def iter_chunks(seq, n: int):
- """Yield successive n-sized chunks from seq."""
- for i in range(0, len(seq), n):
- yield seq[i : i + n]
-
-
-def iter_row_chunks(rows: list[dict], cols_per_row: int) -> Iterable[list[dict]]:
- """Yield chunks of rows sized to fit within bind param limits."""
- if not rows:
- return
- yield from iter_chunks(rows, calculate_rows_per_statement(cols_per_row))
-
-
-def build_visible_owner_clause(owner_id: str) -> sa.sql.ClauseElement:
- """Build owner visibility predicate for reads.
-
- Owner-less rows are visible to everyone.
- """
- owner_id = (owner_id or "").strip()
- if owner_id == "":
- return AssetReference.owner_id == ""
- return AssetReference.owner_id.in_(["", owner_id])
-
-
-def build_prefix_like_conditions(
- prefixes: list[str],
-) -> list[sa.sql.ColumnElement]:
- """Build LIKE conditions for matching file paths under directory prefixes."""
- conds = []
- for p in prefixes:
- base = os.path.abspath(p)
- if not base.endswith(os.sep):
- base += os.sep
- escaped, esc = escape_sql_like_string(base)
- conds.append(AssetReference.file_path.like(escaped + "%", escape=esc))
- return conds
-
-
-def apply_tag_filters(
- stmt: sa.sql.Select,
- include_tags: Sequence[str] | None = None,
- exclude_tags: Sequence[str] | None = None,
- any_tags: Sequence[str] | None = None,
-) -> sa.sql.Select:
- """include_tags: every tag must be present; any_tags: at least one must be
- present; exclude_tags: none may be present."""
- include_tags = normalize_tags(include_tags)
- exclude_tags = normalize_tags(exclude_tags)
- any_tags = normalize_tags(any_tags)
-
- if include_tags:
- for tag_name in include_tags:
- stmt = stmt.where(
- exists().where(
- (AssetReferenceTag.asset_reference_id == AssetReference.id)
- & (AssetReferenceTag.tag_name == tag_name)
- )
- )
-
- if any_tags:
- stmt = stmt.where(
- exists().where(
- (AssetReferenceTag.asset_reference_id == AssetReference.id)
- & (AssetReferenceTag.tag_name.in_(any_tags))
- )
- )
-
- if exclude_tags:
- stmt = stmt.where(
- ~exists().where(
- (AssetReferenceTag.asset_reference_id == AssetReference.id)
- & (AssetReferenceTag.tag_name.in_(exclude_tags))
- )
- )
- return stmt
-
-
-def apply_metadata_filter(
- stmt: sa.sql.Select,
- metadata_filter: dict | None = None,
-) -> sa.sql.Select:
- """Apply filters using asset_reference_meta projection table."""
- if not metadata_filter:
- return stmt
-
- def _exists_for_pred(key: str, *preds) -> sa.sql.ClauseElement:
- return sa.exists().where(
- AssetReferenceMeta.asset_reference_id == AssetReference.id,
- AssetReferenceMeta.key == key,
- *preds,
- )
-
- def _exists_clause_for_value(key: str, value) -> sa.sql.ClauseElement:
- if value is None:
- return sa.not_(
- sa.exists().where(
- AssetReferenceMeta.asset_reference_id == AssetReference.id,
- AssetReferenceMeta.key == key,
- )
- )
-
- if isinstance(value, bool):
- return _exists_for_pred(key, AssetReferenceMeta.val_bool == bool(value))
- if isinstance(value, (int, float, Decimal)):
- num = value if isinstance(value, Decimal) else Decimal(str(value))
- return _exists_for_pred(key, AssetReferenceMeta.val_num == num)
- if isinstance(value, str):
- return _exists_for_pred(key, AssetReferenceMeta.val_str == value)
- return _exists_for_pred(key, AssetReferenceMeta.val_json == value)
-
- for k, v in metadata_filter.items():
- if isinstance(v, list):
- ors = [_exists_clause_for_value(k, elem) for elem in v]
- if ors:
- stmt = stmt.where(sa.or_(*ors))
- else:
- stmt = stmt.where(_exists_clause_for_value(k, v))
- return stmt
diff --git a/app/assets/database/queries/records.py b/app/assets/database/queries/records.py
new file mode 100644
index 000000000..5a20b6bab
--- /dev/null
+++ b/app/assets/database/queries/records.py
@@ -0,0 +1,333 @@
+"""Owns every write to content rows, records and their tag links, plus the paged
+reads that list them. Inserts that can lose a race — a content row at a path, a
+tag, a tag link — run inside a savepoint and re-read the conflicting row, so a
+concurrent writer settles the call instead of raising, while a genuine
+constraint failure still surfaces. This is the sole writer of a content row's
+path, which is what lets other modules trust raw-column path predicates.
+"""
+
+from __future__ import annotations
+
+import os
+from collections.abc import Sequence
+from datetime import datetime
+from typing import Any, Literal, NamedTuple, TypeAlias
+
+import sqlalchemy as sa
+from sqlalchemy.exc import IntegrityError
+from sqlalchemy.orm import Session, joinedload, noload
+from sqlalchemy.sql.elements import ColumnElement
+
+from app.assets.database.models import Asset, AssetContent, AssetTag, Tag
+from app.assets.helpers import escape_sql_like_string, get_utc_now
+
+RecordSortField: TypeAlias = Literal[
+ "name", "created_at", "updated_at", "size", "last_access_time"
+]
+RecordSortOrder: TypeAlias = Literal["asc", "desc"]
+
+
+class RecordCursorBoundary(NamedTuple):
+ value: datetime | int | str
+ id: str
+
+
+class RecordPageSpec(NamedTuple):
+ all_tags: tuple[str, ...] = ()
+ any_tags: tuple[str, ...] = ()
+ none_tags: tuple[str, ...] = ()
+ name_contains: str | None = None
+ limit: int = 20
+ offset: int = 0
+ sort: RecordSortField = "created_at"
+ order: RecordSortOrder = "desc"
+ after: RecordCursorBoundary | None = None
+
+
+_LIVE_PATH_UNIQUE_INDEX = "uq_asset_contents_path_live"
+
+
+def _is_live_path_conflict(error: IntegrityError) -> bool:
+ orig = error.orig
+ message = str(orig)
+ postgres_names_the_index = getattr(getattr(orig, "diag", None), "constraint_name", None) == _LIVE_PATH_UNIQUE_INDEX
+ sqlite_names_the_column = "UNIQUE constraint failed" in message and "asset_contents.path" in message
+ return postgres_names_the_index or sqlite_names_the_column
+
+
+def create_content_reporting_insert(session: Session, path: str, hash: str | None = None, size_bytes: int = 0, mtime_ns: int | None = None) -> tuple[AssetContent, bool]:
+ # The sole writer of asset_contents.path, which is what makes the raw-column SQL prefix
+ # predicates sound — lifecycle's temp wipe HARD-DELETES every row its predicate admits.
+ path = os.path.abspath(path)
+ content = AssetContent(path=path, hash=hash, size_bytes=size_bytes, mtime_ns=mtime_ns)
+ try:
+ with session.begin_nested():
+ session.add(content)
+ session.flush()
+ return content, True
+ except IntegrityError as error:
+ if not _is_live_path_conflict(error):
+ raise
+ winner = session.execute(sa.select(AssetContent).where(AssetContent.path == path, AssetContent.is_missing.is_(False))).scalar_one()
+ return winner, False
+
+
+def create_content(session: Session, path: str, hash: str | None = None, size_bytes: int = 0, mtime_ns: int | None = None) -> AssetContent:
+ content, _ = create_content_reporting_insert(session, path, hash, size_bytes, mtime_ns)
+ return content
+
+
+def ensure_tag(session: Session, name: str) -> None:
+ if session.get(Tag, name) is not None:
+ return
+ try:
+ with session.begin_nested():
+ session.add(Tag(name=name))
+ session.flush()
+ except IntegrityError:
+ # Only a row that is now present proves a lost race; anything else is a real failure.
+ if session.get(Tag, name) is None:
+ raise
+
+
+def ensure_tag_link(session: Session, *, asset_id: str, tag_name: str, origin: str) -> bool:
+ if session.get(AssetTag, (asset_id, tag_name)) is not None:
+ return False
+ try:
+ with session.begin_nested():
+ session.add(AssetTag(asset_id=asset_id, tag_name=tag_name, origin=origin))
+ session.flush()
+ except IntegrityError:
+ if session.get(AssetTag, (asset_id, tag_name)) is None:
+ raise
+ return False
+ return True
+
+
+def create_record(session: Session, content_id: str, name: str, mime_type: str | None = None, job_id: str | None = None, loader_path: str | None = None, tags: Sequence[str] | None = None, *, system_metadata: dict[str, Any] | None = None) -> Asset:
+ record = Asset(content_id=content_id, name=name, mime_type=mime_type, job_id=job_id, loader_path=loader_path, system_metadata=system_metadata)
+ session.add(record)
+ session.flush()
+ for tag_name in dict.fromkeys(tags or ()):
+ ensure_tag(session, tag_name)
+ ensure_tag_link(session, asset_id=record.id, tag_name=tag_name, origin="manual")
+ session.flush()
+ return record
+
+
+def get_record_by_id(session: Session, id: str) -> Asset | None:
+ return session.get(Asset, id)
+
+
+def get_preview_file_paths_by_ids(
+ session: Session,
+ preview_ids: Sequence[str],
+) -> dict[str, str]:
+ if not preview_ids:
+ return {}
+
+ rows = session.execute(
+ sa.select(Asset.id, AssetContent.path)
+ .join(AssetContent, Asset.content_id == AssetContent.id)
+ .where(
+ Asset.id.in_(preview_ids),
+ AssetContent.is_missing.is_(False),
+ )
+ )
+ return {preview_id: path for preview_id, path in rows}
+
+
+def get_record_by_path_or_none(session: Session, path: str) -> Asset | None:
+ return session.scalar(
+ sa.select(Asset)
+ .join(AssetContent, Asset.content_id == AssetContent.id)
+ .where(AssetContent.path == path, AssetContent.is_missing.is_(False))
+ .order_by(Asset.created_at.desc(), Asset.id.desc())
+ .limit(1)
+ )
+
+
+def fetch_record_tags(session: Session, record_id: str) -> list[str]:
+ return list(
+ session.scalars(
+ sa.select(AssetTag.tag_name)
+ .where(AssetTag.asset_id == record_id)
+ .order_by(AssetTag.tag_name)
+ )
+ )
+
+
+def update_record_access_time(
+ session: Session,
+ record_id: str,
+ ts: datetime | None = None,
+ only_if_newer: bool = True,
+) -> None:
+ ts = ts or get_utc_now()
+ stmt = sa.update(Asset).where(Asset.id == record_id)
+ if only_if_newer:
+ stmt = stmt.where(
+ sa.or_(
+ Asset.last_access_time.is_(None),
+ Asset.last_access_time < ts,
+ )
+ )
+ session.execute(stmt.values(last_access_time=ts))
+
+
+def bump_record_updated_at(session: Session, record_id: str) -> None:
+ session.execute(
+ sa.update(Asset).where(Asset.id == record_id).values(updated_at=get_utc_now())
+ )
+
+
+def build_record_tag_filter_clauses(
+ all_tags: Sequence[str],
+ any_tags: Sequence[str],
+ none_tags: Sequence[str],
+) -> tuple[ColumnElement[bool], ...]:
+ clauses: list[ColumnElement[bool]] = []
+ for tag_name in all_tags:
+ clauses.append(
+ sa.exists(
+ sa.select(AssetTag.asset_id).where(
+ AssetTag.asset_id == Asset.id,
+ AssetTag.tag_name == tag_name,
+ )
+ )
+ )
+ if any_tags:
+ clauses.append(
+ sa.exists(
+ sa.select(AssetTag.asset_id).where(
+ AssetTag.asset_id == Asset.id,
+ AssetTag.tag_name.in_(any_tags),
+ )
+ )
+ )
+ if none_tags:
+ clauses.append(
+ ~sa.exists(
+ sa.select(AssetTag.asset_id).where(
+ AssetTag.asset_id == Asset.id,
+ AssetTag.tag_name.in_(none_tags),
+ )
+ )
+ )
+ return tuple(clauses)
+
+
+def list_records_page(
+ session: Session,
+ spec: RecordPageSpec,
+) -> tuple[list[Asset], dict[str, list[str]], int]:
+ filters = list(build_record_tag_filter_clauses(spec.all_tags, spec.any_tags, spec.none_tags))
+ if spec.name_contains:
+ escaped_name, escape_character = escape_sql_like_string(spec.name_contains)
+ filters.append(
+ Asset.name.ilike(f"%{escaped_name}%", escape=escape_character)
+ )
+
+ sort_columns = {
+ "name": Asset.name,
+ "created_at": Asset.created_at,
+ "updated_at": Asset.updated_at,
+ "size": AssetContent.size_bytes,
+ "last_access_time": Asset.last_access_time,
+ }
+ sort_column = sort_columns[spec.sort]
+ descending = spec.order == "desc"
+ sort_expression = sort_column.desc() if descending else sort_column.asc()
+ id_expression = Asset.id.desc() if descending else Asset.id.asc()
+
+ statement = (
+ sa.select(Asset)
+ .join(AssetContent, Asset.content_id == AssetContent.id)
+ .where(*filters)
+ .options(joinedload(Asset.content), noload(Asset.tags))
+ )
+ if spec.after is not None:
+ comparison = (
+ sort_column < spec.after.value
+ if descending
+ else sort_column > spec.after.value
+ )
+ tied_comparison = (
+ Asset.id < spec.after.id
+ if descending
+ else Asset.id > spec.after.id
+ )
+ statement = statement.where(
+ sa.or_(
+ comparison,
+ sa.and_(
+ sort_column == spec.after.value,
+ tied_comparison,
+ ),
+ )
+ )
+
+ statement = statement.order_by(sort_expression, id_expression).limit(spec.limit)
+ if spec.after is None:
+ statement = statement.offset(spec.offset)
+ records = list(session.scalars(statement))
+
+ total = session.scalar(
+ sa.select(sa.func.count())
+ .select_from(Asset)
+ .join(AssetContent, Asset.content_id == AssetContent.id)
+ .where(*filters)
+ )
+
+ record_ids = [record.id for record in records]
+ tag_map: dict[str, list[str]] = {}
+ if record_ids:
+ rows = session.execute(
+ sa.select(AssetTag.asset_id, AssetTag.tag_name)
+ .join(Asset, AssetTag.asset_id == Asset.id)
+ .join(AssetContent, Asset.content_id == AssetContent.id)
+ .where(AssetTag.asset_id.in_(record_ids))
+ .order_by(AssetTag.tag_name.asc())
+ )
+ for record_id, tag_name in rows:
+ tag_map.setdefault(record_id, []).append(tag_name)
+
+ return records, tag_map, int(total or 0)
+
+
+def rename_record(session: Session, id: str, name: str) -> Asset:
+ record = session.get(Asset, id)
+ if record is None:
+ raise LookupError(id)
+ record.name = name
+ record.updated_at = get_utc_now()
+ session.flush()
+ return record
+
+
+def delete_record(session: Session, id: str) -> None:
+ record = session.get(Asset, id)
+ if record is None:
+ return
+ session.delete(record)
+ session.flush()
+
+
+def mark_content_missing(session: Session, content_id: str) -> None:
+ content = session.get(AssetContent, content_id)
+ if content is None:
+ raise LookupError(content_id)
+ content.is_missing = True
+ ensure_tag(session, "missing")
+ for record_id in session.scalars(sa.select(Asset.id).where(Asset.content_id == content_id)):
+ ensure_tag_link(session, asset_id=record_id, tag_name="missing", origin="automatic")
+ session.flush()
+
+
+def unset_content_missing(session: Session, content_id: str) -> None:
+ content = session.get(AssetContent, content_id)
+ if content is None:
+ raise LookupError(content_id)
+ content.is_missing = False
+ session.execute(sa.delete(AssetTag).where(AssetTag.tag_name == "missing", AssetTag.asset_id.in_(sa.select(Asset.id).where(Asset.content_id == content_id))))
+ session.flush()
diff --git a/app/assets/database/queries/tags.py b/app/assets/database/queries/tags.py
index e5f70e3df..177cc980d 100644
--- a/app/assets/database/queries/tags.py
+++ b/app/assets/database/queries/tags.py
@@ -1,26 +1,29 @@
-from dataclasses import dataclass
-from typing import Iterable, Sequence
+"""Answers questions about tag usage: which tags exist with how many assets, and
+how those counts narrow once a filter is applied. Both queries reuse the record
+listing's own joins and filter clauses, so the counts a client sees always
+describe the same assets the listing endpoint would return for that filter,
+records with missing content included.
+"""
-import sqlalchemy as sa
-from sqlalchemy import delete, func, select
-from sqlalchemy.dialects import sqlite
-from sqlalchemy.exc import IntegrityError
+from __future__ import annotations
+
+from dataclasses import dataclass, field
+
+from typing import Sequence
+
+from sqlalchemy import func, select
from sqlalchemy.orm import Session
from app.assets.database.models import (
Asset,
- AssetReference,
- AssetReferenceMeta,
- AssetReferenceTag,
+ AssetContent,
+ AssetTag,
Tag,
)
-from app.assets.database.queries.common import (
- apply_metadata_filter,
- apply_tag_filters,
- build_visible_owner_clause,
- iter_row_chunks,
+from app.assets.database.queries.records import (
+ build_record_tag_filter_clauses,
)
-from app.assets.helpers import escape_sql_like_string, get_utc_now, normalize_tags
+from app.assets.helpers import escape_sql_like_string
@dataclass(frozen=True)
@@ -35,225 +38,7 @@ class RemoveTagsResult:
removed: list[str]
not_present: list[str]
total_tags: list[str]
-
-
-@dataclass(frozen=True)
-class SetTagsResult:
- added: list[str]
- removed: list[str]
- total: list[str]
-
-
-def validate_tags_exist(session: Session, tags: list[str]) -> None:
- """Raise ValueError if any of the given tag names do not exist."""
- existing_tag_names = set(
- name
- for (name,) in session.execute(select(Tag.name).where(Tag.name.in_(tags))).all()
- )
- missing = [t for t in tags if t not in existing_tag_names]
- if missing:
- raise ValueError(f"Unknown tags: {missing}")
-
-
-def ensure_tags_exist(session: Session, names: Iterable[str]) -> None:
- wanted = normalize_tags(list(names))
- if not wanted:
- return
- rows = [{"name": n} for n in list(dict.fromkeys(wanted))]
- ins = (
- sqlite.insert(Tag)
- .values(rows)
- .on_conflict_do_nothing(index_elements=[Tag.name])
- )
- session.execute(ins)
-
-
-def get_reference_tags(session: Session, reference_id: str) -> list[str]:
- return [
- tag_name
- for (tag_name,) in (
- session.execute(
- select(AssetReferenceTag.tag_name)
- .where(AssetReferenceTag.asset_reference_id == reference_id)
- .order_by(AssetReferenceTag.tag_name.asc())
- )
- ).all()
- ]
-
-
-def set_reference_tags(
- session: Session,
- reference_id: str,
- tags: Sequence[str],
- origin: str = "manual",
-) -> SetTagsResult:
- desired = normalize_tags(tags)
-
- current = set(get_reference_tags(session, reference_id))
-
- to_add = [t for t in desired if t not in current]
- to_remove = [t for t in current if t not in desired]
-
- if to_add:
- ensure_tags_exist(session, to_add)
- session.add_all(
- [
- AssetReferenceTag(
- asset_reference_id=reference_id,
- tag_name=t,
- origin=origin,
- added_at=get_utc_now(),
- )
- for t in to_add
- ]
- )
- session.flush()
-
- if to_remove:
- session.execute(
- delete(AssetReferenceTag).where(
- AssetReferenceTag.asset_reference_id == reference_id,
- AssetReferenceTag.tag_name.in_(to_remove),
- )
- )
- session.flush()
-
- return SetTagsResult(added=sorted(to_add), removed=sorted(to_remove), total=sorted(desired))
-
-
-def add_tags_to_reference(
- session: Session,
- reference_id: str,
- tags: Sequence[str],
- origin: str = "manual",
- create_if_missing: bool = True,
- reference_row: AssetReference | None = None,
-) -> AddTagsResult:
- if not reference_row:
- ref = session.get(AssetReference, reference_id)
- if not ref:
- raise ValueError(f"AssetReference {reference_id} not found")
-
- norm = normalize_tags(tags)
- if not norm:
- total = get_reference_tags(session, reference_id=reference_id)
- return AddTagsResult(added=[], already_present=[], total_tags=total)
-
- if create_if_missing:
- ensure_tags_exist(session, norm)
-
- current = set(get_reference_tags(session, reference_id))
-
- want = set(norm)
- to_add = sorted(want - current)
-
- if to_add:
- with session.begin_nested() as nested:
- try:
- session.add_all(
- [
- AssetReferenceTag(
- asset_reference_id=reference_id,
- tag_name=t,
- origin=origin,
- added_at=get_utc_now(),
- )
- for t in to_add
- ]
- )
- session.flush()
- except IntegrityError:
- nested.rollback()
-
- after = set(get_reference_tags(session, reference_id=reference_id))
- return AddTagsResult(
- added=sorted(((after - current) & want)),
- already_present=sorted(want & current),
- total_tags=sorted(after),
- )
-
-
-def remove_tags_from_reference(
- session: Session,
- reference_id: str,
- tags: Sequence[str],
-) -> RemoveTagsResult:
- ref = session.get(AssetReference, reference_id)
- if not ref:
- raise ValueError(f"AssetReference {reference_id} not found")
-
- norm = normalize_tags(tags)
- if not norm:
- total = get_reference_tags(session, reference_id=reference_id)
- return RemoveTagsResult(removed=[], not_present=[], total_tags=total)
-
- existing = set(get_reference_tags(session, reference_id))
-
- to_remove = sorted(set(t for t in norm if t in existing))
- not_present = sorted(set(t for t in norm if t not in existing))
-
- if to_remove:
- session.execute(
- delete(AssetReferenceTag).where(
- AssetReferenceTag.asset_reference_id == reference_id,
- AssetReferenceTag.tag_name.in_(to_remove),
- )
- )
- session.flush()
-
- total = get_reference_tags(session, reference_id=reference_id)
- return RemoveTagsResult(removed=to_remove, not_present=not_present, total_tags=total)
-
-
-def add_missing_tag_for_asset_id(
- session: Session,
- asset_id: str,
- origin: str = "automatic",
-) -> None:
- select_rows = (
- sa.select(
- AssetReference.id.label("asset_reference_id"),
- sa.literal("missing").label("tag_name"),
- sa.literal(origin).label("origin"),
- sa.literal(get_utc_now()).label("added_at"),
- )
- .where(AssetReference.asset_id == asset_id)
- .where(
- sa.not_(
- sa.exists().where(
- (AssetReferenceTag.asset_reference_id == AssetReference.id)
- & (AssetReferenceTag.tag_name == "missing")
- )
- )
- )
- )
- session.execute(
- sqlite.insert(AssetReferenceTag)
- .from_select(
- ["asset_reference_id", "tag_name", "origin", "added_at"],
- select_rows,
- )
- .on_conflict_do_nothing(
- index_elements=[
- AssetReferenceTag.asset_reference_id,
- AssetReferenceTag.tag_name,
- ]
- )
- )
-
-
-def remove_missing_tag_for_asset_id(
- session: Session,
- asset_id: str,
-) -> None:
- session.execute(
- sa.delete(AssetReferenceTag).where(
- AssetReferenceTag.asset_reference_id.in_(
- sa.select(AssetReference.id).where(AssetReference.asset_id == asset_id)
- ),
- AssetReferenceTag.tag_name == "missing",
- )
- )
+ protected: list[str] = field(default_factory=list)
def list_tags_with_usage(
@@ -263,26 +48,18 @@ def list_tags_with_usage(
offset: int = 0,
include_zero: bool = True,
order: str = "count_desc",
- owner_id: str = "",
-) -> tuple[list[tuple[str, str, int]], int]:
+) -> tuple[list[tuple[str, int]], int]:
prefix_filter = prefix.strip() if prefix else ""
counts_sq = (
select(
- AssetReferenceTag.tag_name.label("tag_name"),
- func.count(AssetReferenceTag.asset_reference_id).label("cnt"),
- )
- .select_from(AssetReferenceTag)
- .join(AssetReference, AssetReference.id == AssetReferenceTag.asset_reference_id)
- .where(build_visible_owner_clause(owner_id))
- .where(
- sa.or_(
- AssetReference.is_missing == False, # noqa: E712
- AssetReferenceTag.tag_name == "missing",
- )
+ AssetTag.tag_name.label("tag_name"),
+ func.count(AssetTag.asset_id).label("cnt"),
)
- .where(AssetReference.deleted_at.is_(None))
- .group_by(AssetReferenceTag.tag_name)
+ .select_from(AssetTag)
+ .join(Asset, Asset.id == AssetTag.asset_id)
+ .join(AssetContent, Asset.content_id == AssetContent.id)
+ .group_by(AssetTag.tag_name)
.subquery()
)
@@ -311,17 +88,10 @@ def list_tags_with_usage(
total_q = total_q.where(func.substr(Tag.name, 1, len(prefix_filter)) == prefix_filter)
if not include_zero:
visible_tags_sq = (
- select(AssetReferenceTag.tag_name)
- .join(AssetReference, AssetReference.id == AssetReferenceTag.asset_reference_id)
- .where(build_visible_owner_clause(owner_id))
- .where(
- sa.or_(
- AssetReference.is_missing == False, # noqa: E712
- AssetReferenceTag.tag_name == "missing",
- )
- )
- .where(AssetReference.deleted_at.is_(None))
- .group_by(AssetReferenceTag.tag_name)
+ select(AssetTag.tag_name)
+ .join(Asset, Asset.id == AssetTag.asset_id)
+ .join(AssetContent, Asset.content_id == AssetContent.id)
+ .group_by(AssetTag.tag_name)
)
total_q = total_q.where(Tag.name.in_(visible_tags_sq))
@@ -334,84 +104,48 @@ def list_tags_with_usage(
def list_tag_counts_for_filtered_assets(
session: Session,
- owner_id: str = "",
include_tags: Sequence[str] | None = None,
exclude_tags: Sequence[str] | None = None,
name_contains: str | None = None,
- metadata_filter: dict | None = None,
limit: int = 100,
# Appended last so pre-existing positional callers keep binding correctly.
any_tags: Sequence[str] | None = None,
) -> dict[str, int]:
- """Return tag counts for assets matching the given filters.
+ """Return {tag_name: count} for the assets matching the given filters.
- Uses the same filtering logic as list_references_page but returns
- {tag_name: count} instead of paginated references.
+ Reuses build_record_tag_filter_clauses and the same Asset->AssetContent inner
+ join as list_records_page, so /api/assets and /api/assets/tags/refine agree on
+ which assets a given all/any/none + name_contains filter selects — including
+ missing-content records, which stay catalog-visible.
"""
- # Build a subquery of matching reference IDs
- ref_sq = (
- select(AssetReference.id)
- .join(Asset, Asset.id == AssetReference.asset_id)
- .where(build_visible_owner_clause(owner_id))
- .where(AssetReference.is_missing == False) # noqa: E712
- .where(AssetReference.deleted_at.is_(None))
+ filters = list(
+ build_record_tag_filter_clauses(
+ tuple(include_tags or ()),
+ tuple(any_tags or ()),
+ tuple(exclude_tags or ()),
+ )
)
-
if name_contains:
escaped, esc = escape_sql_like_string(name_contains)
- ref_sq = ref_sq.where(AssetReference.name.ilike(f"%{escaped}%", escape=esc))
+ filters.append(Asset.name.ilike(f"%{escaped}%", escape=esc))
- ref_sq = apply_tag_filters(ref_sq, include_tags, exclude_tags, any_tags)
- ref_sq = apply_metadata_filter(ref_sq, metadata_filter)
- ref_sq = ref_sq.subquery()
+ asset_sq = (
+ select(Asset.id)
+ .join(AssetContent, Asset.content_id == AssetContent.id)
+ .where(*filters)
+ .subquery()
+ )
- # Count tags across those references
q = (
select(
- AssetReferenceTag.tag_name,
- func.count(AssetReferenceTag.asset_reference_id).label("cnt"),
+ AssetTag.tag_name,
+ func.count(AssetTag.asset_id).label("cnt"),
)
- .where(AssetReferenceTag.asset_reference_id.in_(select(ref_sq.c.id)))
- .group_by(AssetReferenceTag.tag_name)
- .order_by(func.count(AssetReferenceTag.asset_reference_id).desc(), AssetReferenceTag.tag_name.asc())
+ .where(AssetTag.asset_id.in_(select(asset_sq.c.id)))
+ .group_by(AssetTag.tag_name)
+ .order_by(func.count(AssetTag.asset_id).desc(), AssetTag.tag_name.asc())
.limit(limit)
)
rows = session.execute(q).all()
return {tag_name: int(cnt) for tag_name, cnt in rows}
-
-
-def bulk_insert_tags_and_meta(
- session: Session,
- tag_rows: list[dict],
- meta_rows: list[dict],
-) -> None:
- """Batch insert into asset_reference_tags and asset_reference_meta.
-
- Uses ON CONFLICT DO NOTHING.
-
- Args:
- session: Database session
- tag_rows: Dicts with: asset_reference_id, tag_name, origin, added_at
- meta_rows: Dicts with: asset_reference_id, key, ordinal, val_*
- """
- if tag_rows:
- ins_tags = sqlite.insert(AssetReferenceTag).on_conflict_do_nothing(
- index_elements=[
- AssetReferenceTag.asset_reference_id,
- AssetReferenceTag.tag_name,
- ]
- )
- for chunk in iter_row_chunks(tag_rows, cols_per_row=4):
- session.execute(ins_tags, chunk)
-
- if meta_rows:
- ins_meta = sqlite.insert(AssetReferenceMeta).on_conflict_do_nothing(
- index_elements=[
- AssetReferenceMeta.asset_reference_id,
- AssetReferenceMeta.key,
- AssetReferenceMeta.ordinal,
- ]
- )
- for chunk in iter_row_chunks(meta_rows, cols_per_row=7):
- session.execute(ins_meta, chunk)
diff --git a/app/assets/docs/README.md b/app/assets/docs/README.md
new file mode 100644
index 000000000..4cd131f8b
--- /dev/null
+++ b/app/assets/docs/README.md
@@ -0,0 +1,342 @@
+# Assets
+
+The asset system is only active when explicitly enabled at startup; it is off by default. When it is not enabled, asset API routes return a disabled-service error and no background filesystem scanning occurs. Routes answer that same disabled-service error when the system is enabled but its database dependencies are unavailable.
+
+## Data model
+
+An asset record represents one user-visible entity. It owns its name, tags, `job_id`, `loader_path`, MIME type, extracted metadata, user metadata, and optional preview relationship.
+
+A content row represents bytes at a storage location. It owns the path, byte size, modification time, hash, and missing state. Multiple asset records may reference one content row. Two content rows may have the same hash; hash uniqueness is never a database invariant.
+
+The path is required and unique among content rows that are not missing. Missing rows keep their last known path so the scanner can attempt recovery.
+
+Names are labels and are not unique. Records are global; the local asset system has no per-user ownership boundary.
+
+MIME type and extracted metadata belong to the asset record because the same bytes may be interpreted in different ways. Extracted metadata may grow when an extractor learns more, but an extractor must not silently remove facts it already recorded within the same extractor version.
+
+`loader_path` is fixed when the record is created. It records how the registry classified the path at discovery time.
+
+## Tags
+
+Asset records carry two kinds of tags: user-applied tags added and removed through the tagging API, and system tags the backend derives on its own.
+
+A location tag records which storage root a file sits under: `input`, `output`, or `temp` for files under those directories, and `models` for files under any configured model base directory. A file under a models base additionally gets one `model_type:<folder_name>` tag per model category whose base directory contains the file and whose registered extension set accepts the file's extension; a file whose extension matches no category still gets the `models` tag but no `model_type:` tag. Model-type tags come from the registered category names, not from path components.
+
+On upload, tags choose the write destination. A request must carry exactly one destination role tag (`input`, `models`, or `output`), and a `models` upload must carry exactly one `model_type:<folder_name>` tag naming the category folder to write into. Any other tags in the request land on the created record as ordinary tags.
+
+`missing` is the one system tag the tagging API refuses to add or remove; it is projected onto records whose content is absent, as described in Missing content. Location and model-type tags carry no such protection and can be added or removed like any other tag.
+
+## Missing content
+
+Every asset whose content is absent stays visible and carries the client-visible `missing` tag (see Tags). Missing state belongs to the content row, so all records that reference the same content become missing together. While content is missing, it cannot be downloaded or resolved by hash: attempts to fetch it or to look it up through from-hash resolution fail rather than silently succeeding.
+
+A content row moves through these observable states:
+
+```mermaid
+stateDiagram-v2
+ LiveUnhashed: live, hash null
+ LiveHashed: live, hashed
+ MissingUnhashed: missing, hash null
+ MissingHashed: missing, hashed
+
+ [*] --> LiveUnhashed: scan discovery or output registration
+ [*] --> LiveHashed: upload (always hashed)
+
+ LiveUnhashed --> LiveHashed: hash computed (hashing on)
+ LiveHashed --> LiveUnhashed: mtime-only change (hashing off)
+
+ LiveUnhashed --> MissingUnhashed: file gone
+ LiveHashed --> MissingHashed: file gone
+
+ MissingHashed --> LiveHashed: hash match at path (hashing on)
+ MissingUnhashed --> LiveHashed: size and mtime match at path (hashing on)
+```
+
+A reappeared file that matches no missing candidate, or more than one, does not recover any of them. The scanner creates a new content row for the file instead, and the missing candidates stay missing. The same old-missing-plus-new-content shape applies to a same-path edit or reuse: the old row is marked missing and a separate new row and record are created for the new bytes, never transformed in place.
+
+Only a hash can recover a missing content row, with one narrow exception for rows that were never hashed. The scanner hashes the file now present at the missing path and recovers the row only when exactly one missing candidate for that path has the same hash. If no candidate matches, the scanner creates new content. If multiple candidates match, none recover. Recovery also never fires for a path a live content row already occupies: the file there belongs to that row, and ordinary change detection owns it.
+
+A missing row can have a null hash, for example a file deleted before it was ever hashed while every server was down during an off-to-on hashing transition. Such a row can never satisfy the hash comparison, so it has its own narrower recovery condition: it recovers only when it is the single missing null-hash candidate at that path, and its recorded byte size and modification time exactly match the reappeared file's verified stat. The freshly computed hash is set on the row as part of recovery. The stat facts are the only recorded identity a never-hashed row has, so a same-path reappearance that does not preserve them creates new content instead, and the old row stays missing rather than risk recovering the wrong record.
+
+Hash-based recovery does not compare modification times; only the null-hash recovery described above requires the recorded byte size and modification time to match exactly. With hashing disabled, recovery is unavailable.
+
+Missing rows and their asset records persist until explicitly deleted. Routine scans do not remove them.
+
+## Hashing modes
+
+The `--enable-asset-hashing` flag defaults to off. It controls whether the scanner and output pipeline hash file contents, as described below.
+
+With hashing off, the scanner uses modification time and byte size to detect changes. A modification-time change with an unchanged byte size is treated as the same file: the stored file facts refresh and the stored hash is cleared, because without hashing the digest can no longer be vouched for. A change to both modification time and size means new content. A size change without a modification-time change is undefined behaviour.
+
+With hashing on, the scanner uses modification time as the cheap change detector and a hash as the identity check. When the modification time changes, verification runs through the background seeding and enrichment work:
+
+- If the hash is unchanged, refresh the stored file facts on the existing content row.
+- If the hash changed, mark the old content missing and create a new content row and asset record for the path.
+
+The system persists the previous hashing mode. On a transition from off to on, it hashes every live row without a hash and revalidates every live row that already has one. A file that cannot be read after a bounded number of attempts keeps its record live with its stored hash cleared, and the transition completes without it. On a transition from on to off, it retains existing hashes as inert data.
+
+Hashing must use a stable file snapshot. The worker checks file facts before and after hashing and accepts the digest only when they still match each other. Otherwise it retries later.
+
+### Uploads hash independently of the scanner flag
+
+Upload dedup runs unconditionally in both hashing modes; only scanner and output hashing are gated by `--enable-asset-hashing`. Uploads always hash, use content-addressed filenames in hash-routed destinations, and deduplicate by bytes even while background hashing is off. Deduplication must know the digest before deciding whether to store the bytes, and the upload request is already reading the whole file.
+
+From-hash lookup is disabled while hashing is off.
+
+## Filesystem changes
+
+### File removed outside the API
+
+Mark the content missing and keep every asset record visible. The usual hash-based recovery applies if a file later appears at the same path.
+
+### File edited in place
+
+In hash mode, verify the new bytes. Refresh the existing content row if the hash is unchanged. If the hash differs, mark the old content missing and create new content and a new asset record for the path.
+
+With hashing off, a change to both modification time and size is handled the same way as a hash difference: the old content is marked missing and new content and a new asset record are created. A modification-time change alone refreshes the existing row's file facts and clears its stored hash (see Hashing modes).
+
+An asset record never changes from one byte identity to another. Existing history references therefore continue to point to the old, already-missing content instead of silently serving new bytes.
+
+### Deleted path reused
+
+Path reuse has the same final state as an in-place edit: the old content is missing and the new bytes receive new content and a new asset record. The result must not depend on whether a scan observed the deletion before the new file appeared.
+
+### File moved or renamed outside the API
+
+There is no filesystem move identity. Mark the old path missing and create new content and a new asset record at the new path.
+
+### Partial download under its final filename
+
+The scanner skips known partial-download extensions. For other files, it records file facts during the walk, waits once per scan pass for a short stability floor, and checks the facts again before inserting.
+
+Files that change between those checks have not finished being written and are not admitted. Such a file is parked on a bounded, de-duplicated watch list. The watch list is rechecked more than once within a single scan pass: once during the fast walk phase and again during the enrichment phase. Each parked entry is retried a limited number of times before it is dropped; a dropped file is admitted normally whenever a later scan observes it in a stable state.
+
+The watch list holds at most one entry per path and has a fixed maximum size. On overflow the oldest entry is evicted. Eviction is not data loss: an evicted path is rediscovered by ordinary directory traversal on any subsequent scan.
+
+Retry is bounded by scan cadence, not by a timer. There is no background poller, and settlement is not guaranteed within any particular interval. A completed prompt queues an enrichment-only pass rather than a full filesystem walk, and that pass rechecks the watch list, so a partially-written output is normally admitted soon after the generation that produced it. If a stalled partial file passes the check and later resumes, normal change detection splits it from the prematurely admitted content.
+
+### Symlinks and hardlinks
+
+The scanner follows symlinks, stores lexical paths, and applies an inode cycle guard. Different paths produce different content rows even when they resolve to the same inode or bytes.
+
+### Case and Unicode path forms
+
+Store absolute, structurally normalised paths: relative segments and repeated separators collapse at the write boundary, so every stored path is the lexical absolute form. Never canonicalize case or Unicode; compare byte-for-byte. Filesystems that treat two case or Unicode spellings as the same path may therefore produce duplicate rows.
+
+### Registry changes
+
+Classification is fixed at record creation. A newly visible path receives the registry classification in effect when discovered. A path that leaves all registered prefixes becomes missing. An existing in-scope path is not reclassified when the registry changes.
+
+## Asset operations
+
+### Delete through the API
+
+Delete the target asset record. Leave its content row and file intact. Do not soft-delete the record or revive the deleted identity during later discovery. The retained content row is not a tombstone: it is ordinary live content describing bytes that are still there, and nothing records that a deletion happened.
+
+Deleting a record never deletes any other record. A preview record the deleted asset nominated stays untouched; references to a preview clear only when the preview record itself is deleted. The preview reference points from the deleted record to its target, so deleting the pointer must not destroy the target.
+
+Content left behind after all its asset records are deleted can still be resolved by hash lookup, falling back to a generic name and a guessed content type when no record is left to supply one. There is currently no mechanism that reclaims or removes such orphaned content.
+
+That retained row is also what makes the deletion durable. A scan seeds only paths with no live content row, so an unchanged file is never given a new record however often it is rediscovered. Only new content at that path produces a fresh asset record: bytes that changed and retired the old row, or content minted after the old row itself is gone. Such a record describes the new content and must never recreate the deleted identity.
+
+### Rename
+
+Renames always succeed. Duplicate names are allowed. Renaming does not change tags, classification, content identity, or storage path.
+
+### Upload the same bytes with the same name
+
+Every upload mints a new asset record for its own request. The request's tags, user metadata, and preview nomination always land on that new record, whether or not identical bytes were already uploaded under the same name. This applies to every upload endpoint, including `/upload/image`.
+
+Content, not records, gets deduplicated. When qualifying content already holds these bytes (live, stat-consistent, and not temporary content), the new record points at that existing content row instead of the bytes being written again. When no content qualifies, the upload writes new content as normal. Either way, a new record is always created.
+
+Uploads are not idempotent: a client that retries an upload after a timeout accumulates a second record rather than being handed back the first. Core has no idempotency protection anywhere, including prompt submission and deletion, and this matches the shape of cached output: a cache hit reuses content but still mints a new delivery record for the new caller.
+
+Uploads hash in both hashing modes, so this content-level dedup applies regardless of the scanner hashing flag.
+
+### `updated_at` semantics
+
+An asset record's `updated_at` reflects only the last explicit user or API edit to that record: a rename, a user-metadata update, a MIME-type change, a preview nomination, or a manual tag add or remove.
+
+It never advances for serving or downloading the asset (access time is a separate concern from edit time), for scanner enrichment filling in extracted metadata, for the automatic missing or recovered tag projection, for a content split or content retire, or for the preview-deleted foreign-key cascade that clears a `preview_id`. Reading or downloading an asset's content instead updates a separate last-access marker on the record or records involved; that marker only ever moves forward, never backward. Minting a new record, whether from an upload, a cached rerun, or a content split, does not touch any other record's `updated_at`. The new record carries its own fresh timestamp from creation, and every existing record's last-explicit-edit time stays untouched.
+
+### Upload the same bytes with a different name
+
+When byte matching is available, create a new asset record with the requested name and point it to the existing content row, wherever in the asset store that content lives. Do not write the bytes a second time. Without byte matching, create a new content row and write the upload normally.
+
+The dedicated `/upload/image` endpoint scopes its byte-matching check to the same destination path only: it does not reuse content stored under a different path even when the hash matches, and writes a new copy there instead.
+
+### Upload different bytes with the same name
+
+Create a new asset record and new content. Both records keep the shared `name`. This is distinct from `display_name`, which is computed from the content's stored path and is not guaranteed to match `name` when content is stored under a hash-derived filename.
+
+### Byte-identical generated output
+
+Every non-cached save event creates a new asset record and a new content row. Do not merge generated outputs automatically. Once hashed, equal hashes make the byte relationship visible without changing either identity.
+
+### Cached output
+
+A fully cached rerun does not execute the save node or write a file. The asset layer creates a new asset record for the new prompt and points it to the existing content row for the cached file. It does not mutate the earlier asset record or content row.
+
+The new record's extracted metadata is copied from the earliest existing record for that same content, ordered by creation, rather than re-extracted. When no earlier record exists for that content, metadata is extracted fresh.
+
+If the cache is invalidated or unavailable, the save node executes normally. It writes a new counter-named file, and the same rule as ordinary generated output creates new content and a new asset record.
+
+#### Runtime-expanded cached output
+
+Whenever final history contains a runtime-expanded output locator, a child absent from the final executed-node set is treated exactly like any other cached output. It does not write a file. The asset layer creates a new delivery record that points to the existing content, and it does not mark earlier content or records missing or mutate them.
+
+Execution must never be inferred from omission in a pre-execution cache announcement. A fully cached wrapper that produces no child locator creates no asset-registration event.
+
+#### Registration happens at the point of the write
+
+The asset layer registers a produced output at the moment the producing node finishes, not after the prompt completes. Classification is determined by control flow, never by comparing final history against a set of executed nodes:
+
+- A node that reaches output processing has, by definition, executed. Cache hits return earlier and never reach it. Such an output is EXECUTED.
+- An output delivered through the cached-UI path is, by definition, CACHED.
+
+For an EXECUTED output the asset layer creates a new content row for the path, marks any existing live content row at that path missing, creates a new asset record carrying the current prompt's `job_id`, and returns that record's identifier to the caller.
+
+For a CACHED output the asset layer creates a new asset record pointing at the existing content row with the new prompt's `job_id`, and mutates nothing else. The same holds for a fully cached wrapper that produces no child locator: no locator, no registration event.
+
+Classifying or registering an output never requires a hash. Every non-cached save always creates new content regardless of whether the bytes changed, so classification never depends on comparing digests.
+
+An identifier returned to a caller must refer to the record created for this write, never the identifier of a record that describes earlier bytes at the same path.
+
+Registration failure must not fail a generation. The failure is logged, the output entry carries no asset identifier, and cleanup is attempted so that a partially-written asset row does not normally survive.
+
+#### Emission and output identity
+
+Both output paths register at the moment the output is emitted, the moment it becomes visible to anything outside the executor. Emission, not sending, is the boundary:
+
+- The executed path emits when the producing node finishes processing its output.
+- The cached path emits when a cached output is served.
+
+At each emission the asset layer creates the appropriate record, and the resulting identifier is attached to the emitted output. Registration and publication to history occur unconditionally; only transmission to a connected client is conditional. An output must be registered whether or not a client is attached, so no registration may sit behind a client-connection check.
+
+The database is the source of truth for output identity. For a fresh prompt, identity is always resolved through registration at emission, not by pulling a value back out of the cache. The cache mechanics used for subgraph replay can carry a previously-registered identifier forward as part of replaying that subgraph's own prior output; replay logic strips that identifier before treating the value as input to a fresh registration. A cached replay therefore reports the identifier of the delivery record created for the current prompt, never an identifier retained from the prompt that first produced the file.
+
+It follows that no component reconstructs output classification after the fact by comparing final history against a record of which nodes executed. Classification is determined once, at emission, and that is the only source of truth for it.
+
+### Settled properties and the eventually-consistent hash
+
+Nearly every asset property that a client can observe is settled before that asset becomes observable, with two narrow exceptions described below: the content hash, and a scanner-discovered asset's metadata during its exception window.
+
+A client reading an asset through the assets API never sees a property that is absent merely because work has not finished yet. Size, modification time, MIME type, extracted metadata, `job_id`, tags, preview location, and missing-state are all determined at the moment the record is created and are correct from the record's first observable instant.
+
+The content hash is the sole eventually-consistent field. It is the only property whose cost scales with file size, so it is the only one permitted to be absent on a record a client can already see. A null hash means "not yet computed", never "this content has no hash".
+
+Capabilities that require a hash (from-hash lookup, upload deduplication, and recovery of missing content) are unavailable for an asset whose hash has not yet settled. This is the same reduced capability described for disabled hashing. Settlement has no guaranteed deadline: a hash can remain unset indefinitely if the file never stabilizes or enrichment cannot make progress. It covers freshly-registered outputs even in hashing-on mode, not only the disabled-hashing case.
+
+#### Exception: scanner-discovered assets
+
+Assets discovered by the filesystem scanner are the one exception to full settlement: a seeded record can be observable before its metadata has settled.
+
+Distinguishing "not yet enriched" from "enrichment ran and found nothing" matters here: without that distinction, excluding un-enriched records from view would hide, permanently, any asset whose extraction fails. Outputs and uploads are not covered by this exception; their properties are always settled when observable.
+
+#### Output registration never hashes on the save path
+
+Registering a produced output computes no content hash. The record is created with a null hash unconditionally, and the background enrichment pass fills it afterwards when hashing is enabled. When hashing is disabled the hash stays null, consistent with hashing being off.
+
+Hashing reads every byte of the file and its cost scales without bound with file size, while the save path sits inside the execution loop. No output size may add hashing latency to a generation. No hashing failure may surface on the save path either; a failed or unstable read is the enrichment pass's problem, handled later.
+
+Uploads are outside this behaviour and hash inline, always: deduplication must know the digest before deciding whether to store the bytes, and the upload request is already reading the whole file.
+
+#### Two producers writing one path in a single prompt
+
+Two output producers that resolve to the same path within one prompt are unsupported. The resulting database state is undefined and nothing here constrains it.
+
+Registration occurs per producer at emission, so a prompt containing such a collision produces one record per producer, and their relative order determines the final state. That order is not a contract.
+
+Each producer's registration attempt is independent and best-effort, consistent with the failure handling described for output registration: a failed attempt is caught and that producer's output proceeds without an asset identifier rather than failing the generation. Only the combined outcome across producers is undefined.
+
+Save nodes assign counter-based filenames precisely so that concurrent producers do not collide, so a workflow reaching this state has bypassed the normal naming path.
+
+### Server restart
+
+Persist asset and content rows across restarts. In-memory history is transient. A record may retain a `job_id` whose prompt is no longer present in history.
+
+In-memory tracking used during scanning, such as the unstable-file watch list and the hash-transition queue, does not persist across a restart either. A fresh process rebuilds whatever state it needs by re-observing the filesystem and database rather than resuming exactly where a prior process left off.
+
+Temp records are the exception to row persistence (see Temp and preview output).
+
+### Temp and preview output
+
+Startup temp cleanup deletes both temp files and the records and content rows that represent them. Two failure modes are possible: if wiping the temp rows from the database fails, filesystem cleanup for that startup is skipped entirely and neither side is committed; if the database wipe succeeds but the subsequent filesystem removal fails, the rows are already gone from the database while the files remain on disk.
+
+Cloud temp and preview assets expire through a separate mechanism.
+
+### Temp content shared by permanent records
+
+Expiry belongs to the content location. Upload dedup and from-hash lookup exclude temp content, so permanent records can never point to content that startup temp cleanup will remove.
+
+### Same model bytes in two category locations
+
+Create one asset record and one content row per location. Equal hashes may reveal that the bytes match. Do not merge the locations.
+
+### Byte-identical content from two local users
+
+The local asset system has global records and no owner field. It does not isolate or duplicate records by user.
+
+### `/view` routes
+
+`/view` accepts two query forms: a path-based form (`type`, `filename`, `subfolder`) and a blake3-hash form (`filename=blake3:<hash>`). `/api/assets/{id}/content` accepts an asset id and serves that asset's content directly. The blake3 form resolves only to non-temp content whose file is currently present (see Lookup and dedup against missing content).
+
+### Lookup and dedup against missing content
+
+Hash lookup, from-hash creation, and upload dedup consider only non-temp content whose file is present. A database row is not proof that bytes can be served.
+
+When no qualifying content exists, uploads store the bytes they received and from-hash creation refuses. A missing sibling is never substituted for requested content.
+
+### From-hash tie-breaking
+
+When several qualifying content rows have the requested hash, choose the oldest by `created_at`, then by lexicographic `id`.
+
+## Jobs and provenance
+
+### Querying a job's outputs
+
+The asset API does not provide a job-to-outputs query. A record's `job_id` is informational. In-memory history is the mechanism for showing a run's outputs during that session.
+
+Cached reruns create a new asset record whose `job_id` is the new prompt. The record points to the existing content row because the save node did not execute and no new file was written. Earlier asset records keep their original `job_id` values.
+
+### Recording where a file came from
+
+Each asset record's `job_id` identifies the prompt associated with that record's own creation event. The system does not infer provenance across records that share content.
+
+Embedded PNG metadata is independent of the asset database. Save code writes it into the file, and the frontend may read it to restore a workflow.
+
+## Concurrency and failure boundaries
+
+### File changes during hashing
+
+Accept a digest only from a stable snapshot (see Hashing modes).
+
+### Concurrent writers
+
+Database constraints choose the winner when scanners, hooks, or uploads race within one process. Losing writers retry or discard their work. Tag and tag-link inserts run inside a savepoint and re-read the row they collided with, so a duplicate-key collision between concurrent tag writers settles quietly instead of surfacing an error. That covers duplicate keys only; lock contention is a separate limit (see Write pressure and reader starvation).
+
+A file lock prevents more than one server process from opening the same database, so a second process never becomes a concurrent writer in the first place. Do not add advisory locks.
+
+### Ambiguous recovery
+
+If more than one missing content row matches a recreated file's hash, recover none of them.
+
+### Database replacement while running
+
+Deleting or replacing the database file while the server is running is undefined behaviour.
+
+## Operational limits
+
+### Write pressure and reader starvation
+
+The asset database is SQLite with a single database-wide writer lock and no configured busy timeout or lock-error handling on any route. Several paths hold or contend for that lock: a non-deduplicated upload writes its bytes and mints a delivery record, while a deduplicated upload reuses existing content and mints only the record; a same-path write whose hash has changed retires the old content and inserts new content, while a same-path write whose hash matches refreshes the existing record in place; execution outputs register per-emission during the generation loop; a background enrichment pass fills hashes and metadata row by row; hash-serves write access time to every record sharing the served content; and the upload dedup claim holds the write lock across its filesystem re-check and metadata extraction.
+
+Under sustained concurrent writes, a reader such as `GET /api/assets` can exceed SQLite's default five-second busy wait and surface an unhandled `database is locked` error as HTTP 500. The failure is transient and non-corrupting: no rows are corrupted, and a later request may succeed once the write pressure eases, though nothing retries or backs off automatically. No busy timeout, lock-error translation to 503, or WAL journal mode is configured.
+
+## Schema migration
+
+When startup finds the schema that predates the record/content split, it drops and recreates the affected tables inside the existing database file, then rebuilds them with a full scan. Rows are not migrated into the new schema, and the database file itself is not deleted. The database is backed up to a sibling file before the migration runs; if the migration fails, the database is reverted from that backup, and if it succeeds, the backup file is left in place rather than deleted.
+
+Scanning runs in the background after startup returns, so the asset API can already be serving requests while the rebuilt tables are still being populated.
+
+The rebuild discards data that a scan cannot reconstruct: manual tags, user metadata, preview assignments, API-created records, `job_id` links, any record renames, and deletions — nothing records which assets were deleted, so the scan mints a record for every file still on disk. When the rebuild runs, startup logs a warning naming the backup file and what was discarded.
diff --git a/app/assets/helpers.py b/app/assets/helpers.py
index 87734d0dc..b65eb3b78 100644
--- a/app/assets/helpers.py
+++ b/app/assets/helpers.py
@@ -1,26 +1,45 @@
+"""Small conversions the asset system needs in more than one layer: canonical
+stored-hash strings, UTC timestamps, tag normalization, and a SQL predicate for
+path containment. That predicate is deliberately case-sensitive and
+component-bounded, because callers use it to choose rows for hard deletion.
+"""
+
import os
from datetime import datetime, timezone
-from typing import Sequence
+import sqlalchemy as sa
+from sqlalchemy.sql import ColumnElement
-def select_best_live_path(states: Sequence) -> str:
- """
- Return the best on-disk path among cache states:
- 1) Prefer a path that exists with needs_verify == False (already verified).
- 2) Otherwise, pick the first path that exists.
- 3) Otherwise return empty string.
+
+def sql_path_under_prefix(
+ column: ColumnElement[str], prefix: str
+) -> ColumnElement[bool]:
+ """SQL predicate for ``Path(column).is_relative_to(prefix)`` on this platform.
+
+ Case-SENSITIVE and component-bounded: prefix ``/a/b`` matches ``/a/b`` and
+ ``/a/b/c`` but not ``/a/bc``, ``/a/b-other`` or ``/a/B/c``.
+
+ ``LIKE`` cannot express this. SQLite's ``LIKE`` is ASCII case-insensitive by
+ default, so ``'/data/TEMP/f' LIKE '/data/temp/%'`` is TRUE — which let the
+ temp wipe hard-delete records under a case-different persistent directory,
+ and let the enrichment scan mutate rows outside the requested root.
+ ``GLOB`` is case-sensitive but carries its own metacharacters (``*``, ``?``,
+ ``[``) with no ESCAPE clause, so every caller would need bracket-quoting.
+ ``substr(column, 1, n) = <prefix>`` compares under the column's BINARY
+ collation and has no metacharacters at all, so a path containing ``%``,
+ ``_``, ``*``, ``?`` or ``[`` needs no escaping and cannot inject.
+
+ Only the PREFIX is normalized here. That is sound because the column holds
+ normalized absolute paths — ``records.create_content`` is the sole writer
+ and normalizes there. Normalizing the column in SQL is not an option anyway:
+ it would need a per-row Python call and would defeat the index.
"""
- alive = [
- s
- for s in states
- if getattr(s, "file_path", None) and os.path.isfile(s.file_path)
- ]
- if not alive:
- return ""
- for s in alive:
- if not getattr(s, "needs_verify", False):
- return s.file_path
- return alive[0].file_path
+ base = os.path.abspath(prefix)
+ stem = base if base.endswith(os.sep) else base + os.sep
+ return sa.or_(
+ column == base,
+ sa.func.substr(column, 1, len(stem)) == stem,
+ )
def escape_sql_like_string(s: str, escape: str = "!") -> tuple[str, str]:
@@ -47,6 +66,10 @@ def normalize_tags(tags: list[str] | None) -> list[str]:
return list(dict.fromkeys(t.strip() for t in (tags or []) if (t or "").strip()))
+def to_stored_hash(digest: str) -> str:
+ return f"blake3:{digest}"
+
+
def validate_blake3_hash(s: str) -> str:
"""Validate and normalize a blake3 hash string.
diff --git a/app/assets/lifecycle.py b/app/assets/lifecycle.py
new file mode 100644
index 000000000..26ecd0370
--- /dev/null
+++ b/app/assets/lifecycle.py
@@ -0,0 +1,152 @@
+"""Runs the asset system's startup and shutdown maintenance: clearing temp rows
+and files, recording a hash-mode transition, and handing the filesystem scan to
+the background seeder. Startup only enqueues transition work — draining it
+belongs to the seeder, so a large library cannot stall the server before it
+accepts requests. It also settles, before any of that, whether the database
+dependencies exist at all, disabling the asset routes when they do not.
+"""
+
+from __future__ import annotations
+
+import logging
+import os
+import shutil
+
+import folder_paths
+from sqlalchemy import select
+
+from app.assets.database.models import Asset, AssetContent
+from app.assets.database.queries.records import delete_record
+from app.assets.helpers import sql_path_under_prefix
+from app.assets.services.hash_mode_state import enqueue_transition_work
+from app.assets.services.hash_mode_state import record_transition_intent
+from app.database.db import can_create_session, create_session
+from comfy.cli_args import args
+
+_excluded_scan_roots: set[str] = set()
+_hash_mode_transition: str | None = None
+
+
+def get_excluded_scan_roots() -> frozenset[str]:
+ return frozenset(_excluded_scan_roots)
+
+
+def record_hash_mode_transition_intent() -> None:
+ global _hash_mode_transition
+
+ with create_session() as session:
+ _hash_mode_transition = record_transition_intent(session)
+ session.commit()
+
+
+def enqueue_mode_transition_work() -> None:
+ with create_session() as session:
+ enqueue_transition_work(session, _hash_mode_transition)
+ session.commit()
+
+
+def wipe_temp_db_rows(session) -> tuple[int, int]:
+ try:
+ temp_root = os.path.abspath(folder_paths.get_temp_directory())
+ except OSError:
+ return 0, 0
+ # These rows are hard-deleted, so the predicate must stay case-SENSITIVE: admitting a
+ # case-different persistent directory destroys user assets.
+ under_temp = sql_path_under_prefix(AssetContent.path, temp_root)
+
+ temp_record_ids = list(
+ session.scalars(
+ select(Asset.id)
+ .join(AssetContent, Asset.content_id == AssetContent.id)
+ .where(under_temp)
+ )
+ )
+
+ records_deleted = 0
+ for record_id in temp_record_ids:
+ delete_record(session, record_id)
+ records_deleted += 1
+
+ contents_deleted = 0
+ for content in session.scalars(select(AssetContent).where(under_temp)).all():
+ session.delete(content)
+ contents_deleted += 1
+
+ session.flush()
+ return records_deleted, contents_deleted
+
+
+def cleanup_temp_filesystem() -> bool:
+ temp_dir = os.path.abspath(folder_paths.get_temp_directory())
+ if not os.path.exists(temp_dir):
+ return True
+ try:
+ shutil.rmtree(temp_dir)
+ return True
+ except OSError as exc:
+ logging.warning(
+ "Failed to remove temp directory %s: %s — excluding from scan for this process",
+ temp_dir,
+ exc,
+ )
+ _excluded_scan_roots.add(temp_dir)
+ return False
+
+
+def start_asset_seeder() -> bool:
+ from app.assets.seeder import asset_seeder
+
+ started = asset_seeder.start(
+ roots=("models", "input", "output"),
+ prune_first=True,
+ compute_hashes=args.enable_asset_hashing,
+ )
+ if started:
+ logging.info("Background asset scan initiated for models, input, output")
+ return started
+
+
+def run_asset_startup() -> None:
+ try:
+ with create_session() as session:
+ wipe_temp_db_rows(session)
+ session.commit()
+ except Exception:
+ logging.exception("Temp DB row wipe failed; skipping filesystem cleanup")
+ enqueue_mode_transition_work()
+ start_asset_seeder()
+ return
+ cleanup_temp_filesystem()
+ enqueue_mode_transition_work()
+ start_asset_seeder()
+
+
+def run_startup(*, enable_assets: bool) -> None:
+ try:
+ if enable_assets:
+ run_asset_startup()
+ else:
+ cleanup_temp_filesystem()
+ except Exception:
+ logging.exception("Asset startup maintenance failed")
+
+
+def run_asset_shutdown_cleanup() -> None:
+ try:
+ with create_session() as session:
+ wipe_temp_db_rows(session)
+ session.commit()
+ except Exception:
+ logging.exception("Temp DB row wipe failed during shutdown")
+ finally:
+ cleanup_temp_filesystem()
+
+
+def run_shutdown() -> None:
+ try:
+ if can_create_session():
+ run_asset_shutdown_cleanup()
+ else:
+ cleanup_temp_filesystem()
+ except Exception:
+ logging.exception("Asset shutdown cleanup failed")
diff --git a/app/assets/manager.py b/app/assets/manager.py
new file mode 100644
index 000000000..590015d6a
--- /dev/null
+++ b/app/assets/manager.py
@@ -0,0 +1,227 @@
+import logging
+from typing import Any, Callable, Protocol
+
+from aiohttp import web
+
+from app.assets import mode
+from app.assets.api.routes import register_assets_routes
+from app.assets.lifecycle import record_hash_mode_transition_intent, run_shutdown, run_startup
+from app.assets.seeder import ScanPhase, asset_seeder
+from app.assets.services.ingest import (
+ register_cached_output as ingest_register_cached_output,
+ register_executed_output as ingest_register_executed_output,
+ register_file_in_place,
+)
+from app.assets.services.path_utils import get_known_subfolder_tags
+from app.assets.services.schemas import RegisteredAsset, UploadAssetView
+from app.database.db import dependencies_available
+from app.user_manager import UserManager
+from comfy.cli_args import args
+
+
+class AssetManager(Protocol):
+ @property
+ def enabled(self) -> bool: ...
+
+ def startup(self) -> None: ...
+
+ def shutdown(self) -> None: ...
+
+ def register_routes(
+ self, app: web.Application, user_manager: UserManager | None
+ ) -> None: ...
+
+ def ensure_scan_started(self) -> None: ...
+
+ def pause_background_scan(self) -> None: ...
+
+ def queue_output_scan(self) -> None: ...
+
+ def resume_background_scan(self) -> None: ...
+
+ def register_upload(
+ self,
+ abs_path: str,
+ name: str,
+ upload_type: str,
+ subfolder: str,
+ *,
+ content_written: bool,
+ ) -> UploadAssetView | None: ...
+
+ def register_executed_output(
+ self, abs_path: str, job_id: str | None
+ ) -> RegisteredAsset | None: ...
+
+ def register_cached_output(
+ self, abs_path: str, job_id: str | None
+ ) -> RegisteredAsset | None: ...
+
+ def set_event_sink(self, sink: Callable[[str, dict[str, Any]], None] | None) -> None: ...
+
+
+class _ArgsLike(Protocol):
+ enable_assets: bool
+ enable_asset_hashing: bool
+
+
+def _shutdown_assets() -> None:
+ asset_seeder.shutdown()
+ run_shutdown()
+
+
+class NoAssets:
+ def __init__(self, args: _ArgsLike) -> None:
+ self._args = args
+
+ @property
+ def enabled(self) -> bool:
+ return False
+
+ def startup(self) -> None:
+ mode.init(self._args)
+ record_hash_mode_transition_intent()
+ run_startup(enable_assets=False)
+
+ def shutdown(self) -> None:
+ _shutdown_assets()
+
+ def register_routes(
+ self, app: web.Application, user_manager: UserManager | None
+ ) -> None:
+ register_assets_routes(app)
+ asset_seeder.disable()
+
+ def ensure_scan_started(self) -> None:
+ return None
+
+ def pause_background_scan(self) -> None:
+ return None
+
+ def queue_output_scan(self) -> None:
+ return None
+
+ def resume_background_scan(self) -> None:
+ return None
+
+ def register_upload(
+ self,
+ abs_path: str,
+ name: str,
+ upload_type: str,
+ subfolder: str,
+ *,
+ content_written: bool,
+ ) -> UploadAssetView | None:
+ return None
+
+ def register_executed_output(
+ self, abs_path: str, job_id: str | None
+ ) -> RegisteredAsset | None:
+ return None
+
+ def register_cached_output(
+ self, abs_path: str, job_id: str | None
+ ) -> RegisteredAsset | None:
+ return None
+
+ def set_event_sink(self, sink: Callable[[str, dict[str, Any]], None] | None) -> None:
+ return None
+
+
+class AssetsEnabled:
+ def __init__(self, args: _ArgsLike) -> None:
+ self._args = args
+
+ @property
+ def enabled(self) -> bool:
+ return True
+
+ def startup(self) -> None:
+ mode.init(self._args)
+ record_hash_mode_transition_intent()
+ run_startup(enable_assets=True)
+
+ def shutdown(self) -> None:
+ _shutdown_assets()
+
+ def register_routes(
+ self, app: web.Application, user_manager: UserManager | None
+ ) -> None:
+ register_assets_routes(app, user_manager)
+
+ def ensure_scan_started(self) -> None:
+ asset_seeder.start(roots=("models", "input", "output"))
+
+ def pause_background_scan(self) -> None:
+ asset_seeder.pause()
+
+ def queue_output_scan(self) -> None:
+ if not asset_seeder.is_disabled():
+ # FULL, not ENRICH: only a walk finds outputs a node never declared. Do not downgrade without re-weighing the cost.
+ asset_seeder.enqueue_scan(
+ roots=("output",),
+ phase=ScanPhase.FULL,
+ compute_hashes=self._args.enable_asset_hashing,
+ )
+
+ def resume_background_scan(self) -> None:
+ asset_seeder.resume()
+
+ def register_upload(
+ self,
+ abs_path: str,
+ name: str,
+ upload_type: str,
+ subfolder: str,
+ *,
+ content_written: bool,
+ ) -> UploadAssetView | None:
+ try:
+ tag = upload_type if upload_type in ("input", "output") else "input"
+ tags = [tag] + get_known_subfolder_tags(subfolder)
+ result = register_file_in_place(
+ abs_path=abs_path,
+ name=name,
+ tags=tags,
+ content_written=content_written,
+ )
+ asset = RegisteredAsset(
+ id=result.ref.id,
+ content_id=result.content_id,
+ job_id=result.ref.job_id,
+ name=result.ref.name,
+ )
+ return UploadAssetView(
+ asset=asset,
+ asset_hash=result.asset.hash,
+ size=result.asset.size_bytes,
+ mime_type=result.asset.mime_type,
+ tags=result.tags,
+ )
+ except Exception:
+ logging.warning("Failed to register uploaded image as asset", exc_info=True)
+ return None
+
+ def register_executed_output(
+ self, abs_path: str, job_id: str | None
+ ) -> RegisteredAsset | None:
+ return ingest_register_executed_output(abs_path, job_id)
+
+ def register_cached_output(
+ self, abs_path: str, job_id: str | None
+ ) -> RegisteredAsset | None:
+ return ingest_register_cached_output(abs_path, job_id)
+
+ def set_event_sink(self, sink: Callable[[str, dict[str, Any]], None] | None) -> None:
+ asset_seeder.set_event_sink(sink)
+
+
+def default_asset_manager() -> AssetManager:
+ if args.enable_assets and not dependencies_available():
+ logging.warning(
+ "Assets requested but database dependencies unavailable; asset endpoints "
+ "will answer 503. Please install the updated requirements.txt file."
+ )
+ return NoAssets(args)
+ return AssetsEnabled(args) if args.enable_assets else NoAssets(args)
diff --git a/app/assets/mode.py b/app/assets/mode.py
new file mode 100644
index 000000000..6c789cd8e
--- /dev/null
+++ b/app/assets/mode.py
@@ -0,0 +1,30 @@
+"""Holds whether asset hashing is enabled for this process, taken from the
+command-line flag once at startup. Callers ask here rather than reading
+arguments themselves, and asking before initialization raises instead of
+defaulting, so a route can never quietly answer as though hashing were off.
+"""
+
+from __future__ import annotations
+
+from typing import Protocol
+
+
+class _HashingArguments(Protocol):
+ enable_asset_hashing: bool
+
+
+_args: _HashingArguments | None = None
+
+
+def init(args: _HashingArguments) -> None:
+ global _args
+ _args = args
+
+
+def hashing_enabled() -> bool:
+ if _args is None:
+ raise RuntimeError(
+ "app.assets.mode.init() was not called before hashing_enabled(); "
+ "hash-mode state is uninitialised"
+ )
+ return bool(_args.enable_asset_hashing)
diff --git a/app/assets/scanner.py b/app/assets/scanner.py
index 64d169c49..7f8ce561b 100644
--- a/app/assets/scanner.py
+++ b/app/assets/scanner.py
@@ -1,64 +1,90 @@
+"""Walks the asset roots and turns what it finds into catalog rows: collecting
+paths, building specs, seeding new content and records, then enriching them
+with metadata and hashes. Each spec is seeded inside its own savepoint, so one
+file whose row conflicts cannot discard the work done for the files around it.
+Enrichment counts as progress only when it produced what was asked of it — a
+requested hash that could not be computed is no progress, which is what bounds
+a pass over a file the server cannot read.
+"""
+
import logging
import os
+from dataclasses import dataclass
from pathlib import Path
from typing import Callable, Literal, TypedDict
import folder_paths
+import sqlalchemy as sa
+from sqlalchemy.exc import IntegrityError
+from sqlalchemy.orm import Session
+from app.assets import mode
from app.assets.database.queries import (
- add_missing_tag_for_asset_id,
- bulk_update_enrichment_level,
- bulk_update_is_missing,
- bulk_update_needs_verify,
- delete_orphaned_seed_asset,
- delete_references_by_ids,
- ensure_tags_exist,
- get_asset_by_hash,
- get_reference_by_id,
- get_references_for_prefixes,
- get_unenriched_references,
- mark_references_missing_outside_prefixes,
- reassign_asset_references,
- remove_missing_tag_for_asset_id,
- set_reference_system_metadata,
- update_asset_hash_and_mime,
+ create_content_reporting_insert,
+ mark_content_missing,
+ create_record,
)
-from app.assets.services.bulk_ingest import (
- SeedAssetSpec,
- batch_insert_seed_assets,
+from app.assets.database.models import Asset, AssetContent
+from app.assets.helpers import sql_path_under_prefix, to_stored_hash
+from app.assets.lifecycle import get_excluded_scan_roots
+from app.assets.scanner_changes import (
+ clear_pending_verifications,
+ detect_content_change,
+ drain_pending_verifications,
+ is_path_under_prefixes,
+ live_contents_under_prefixes,
+ pending_recovery_count,
+ recover_missing_content,
)
-from app.assets.services.file_utils import (
- get_mtime_ns,
- is_visible,
- list_files_recursively,
- verify_file_unchanged,
+from app.assets.scanner_admission import (
+ PARTIAL_DOWNLOAD_EXTENSIONS as PARTIAL_DOWNLOAD_EXTENSIONS,
+ _WATCH_LIST as _WATCH_LIST,
+ _WatchEntry as _WatchEntry,
+ _should_skip_extension,
+ _two_stat_admit,
+ tick_watch_list as tick_watch_list,
)
-from app.assets.services.hashing import HashCheckpoint, compute_blake3_hash
+from app.assets.services.file_utils import get_mtime_ns, is_visible, list_files_recursively
from app.assets.services.image_dimensions import extract_image_dimensions
-from app.assets.services.metadata_extract import extract_file_metadata
+from app.assets.services.metadata_extract import ExtractedMetadata, extract_file_metadata
from app.assets.services.path_utils import (
compute_loader_path,
get_comfy_models_folders,
get_name_and_tags_from_asset_path,
)
+from app.assets.services.ingest import _discard_unreferenced_content
+from app.assets.services.snapshot_hash import snapshot_hash
from app.database.db import create_session
+__all__ = [
+ "clear_pending_verifications",
+ "drain_pending_verifications",
+ "pending_recovery_count",
+]
-class _RefInfo(TypedDict):
- ref_id: str
- file_path: str
- exists: bool
- stat_unchanged: bool
- needs_verify: bool
+# Temp is deliberately absent: it is wiped before every scan, so walking it finds nothing.
+RootType = Literal["models", "input", "output"]
-class _AssetAccumulator(TypedDict):
- hash: str | None
- size_db: int
- refs: list[_RefInfo]
+class SeedAssetSpec(TypedDict):
-# Temp is deliberately absent: it is wiped before every scan, so walking it finds nothing.
-RootType = Literal["models", "input", "output"]
+ abs_path: str
+ # Walk-time diagnostics only: seeding persists the seed-time restat instead.
+ size_bytes: int
+ mtime_ns: int
+ info_name: str
+ tags: list[str]
+ fname: str | None
+ metadata: ExtractedMetadata | None
+ mime_type: str | None
+ job_id: str | None
+
+
+@dataclass(frozen=True, slots=True)
+class UnenrichedContent:
+ content_id: str
+ record_id: str
+ file_path: str
def _log_scan_error(phase: str, error: OSError) -> None:
@@ -89,7 +115,10 @@ def get_owned_prefixes() -> list[str]:
def get_temp_prefixes() -> list[str]:
- return [os.path.abspath(folder_paths.get_temp_directory())]
+ temp_dir = os.path.abspath(folder_paths.get_temp_directory())
+ if temp_dir in get_excluded_scan_roots():
+ return []
+ return [temp_dir]
def collect_models_files() -> list[str]:
@@ -118,145 +147,49 @@ def sync_references_with_filesystem(
session,
root: RootType,
collect_existing_paths: bool = False,
- update_missing_tags: bool = False,
) -> set[str] | None:
return sync_prefixes_with_filesystem(
session,
get_scan_prefixes_for_root(root),
collect_existing_paths=collect_existing_paths,
- update_missing_tags=update_missing_tags,
)
def sync_prefixes_with_filesystem(
- session,
+ session: Session,
prefixes: list[str],
collect_existing_paths: bool = False,
- update_missing_tags: bool = False,
) -> set[str] | None:
- """Reconcile asset references with filesystem under the given prefixes.
-
- - Toggle needs_verify per reference using mtime/size stat check
- - For hashed assets with at least one stat-unchanged ref: delete stale missing refs
- - For seed assets with all refs missing: delete Asset and its references
- - Optionally add/remove 'missing' tags based on stat check in this root
- - Optionally return surviving absolute paths
-
- Args:
- session: Database session
- prefixes: Absolute directory prefixes whose references to reconcile
- collect_existing_paths: If True, return set of surviving file paths
- update_missing_tags: If True, update 'missing' tags based on file status
-
- Returns:
- Set of surviving absolute paths if collect_existing_paths=True, else None
- """
if not prefixes:
return set() if collect_existing_paths else None
- rows = get_references_for_prefixes(
- session, prefixes, include_missing=update_missing_tags
- )
-
- by_asset: dict[str, _AssetAccumulator] = {}
- for row in rows:
- acc = by_asset.get(row.asset_id)
- if acc is None:
- acc = {"hash": row.asset_hash, "size_db": row.size_bytes, "refs": []}
- by_asset[row.asset_id] = acc
-
- stat_unchanged = False
+ survivors: set[str] = set()
+ for content in live_contents_under_prefixes(session, prefixes):
try:
- exists = True
- stat_unchanged = verify_file_unchanged(
- mtime_db=row.mtime_ns,
- size_db=acc["size_db"],
- stat_result=os.stat(row.file_path, follow_symlinks=True),
- )
+ stat_result = os.stat(content.path, follow_symlinks=True)
except FileNotFoundError:
- exists = False
+ mark_content_missing(session, content.id)
except PermissionError as e:
- exists = True
_log_scan_error("reference_stat", e)
- logging.debug("Permission denied accessing %s", row.file_path)
+ logging.debug("Permission denied accessing %s", content.path)
except OSError as e:
- exists = False
_log_scan_error("reference_stat", e)
- logging.debug("OSError checking %s: %s", row.file_path, e)
-
- acc["refs"].append(
- {
- "ref_id": row.reference_id,
- "file_path": row.file_path,
- "exists": exists,
- "stat_unchanged": stat_unchanged,
- "needs_verify": row.needs_verify,
- }
- )
-
- to_set_verify: list[str] = []
- to_clear_verify: list[str] = []
- stale_ref_ids: list[str] = []
- to_mark_missing: list[str] = []
- to_clear_missing: list[str] = []
- survivors: set[str] = set()
-
- for aid, acc in by_asset.items():
- a_hash = acc["hash"]
- refs = acc["refs"]
- any_unchanged = any(r["stat_unchanged"] for r in refs)
- all_missing = all(not r["exists"] for r in refs)
-
- for r in refs:
- if not r["exists"]:
- to_mark_missing.append(r["ref_id"])
- continue
- if r["stat_unchanged"]:
- to_clear_missing.append(r["ref_id"])
- if r["needs_verify"]:
- to_clear_verify.append(r["ref_id"])
- if not r["stat_unchanged"] and not r["needs_verify"]:
- to_set_verify.append(r["ref_id"])
-
- if a_hash is None:
- if refs and all_missing:
- delete_orphaned_seed_asset(session, aid)
- else:
- for r in refs:
- if r["exists"]:
- survivors.add(os.path.abspath(r["file_path"]))
- continue
-
- if any_unchanged:
- for r in refs:
- if not r["exists"]:
- stale_ref_ids.append(r["ref_id"])
- if update_missing_tags:
- try:
- remove_missing_tag_for_asset_id(session, asset_id=aid)
- except Exception as e:
- logging.warning(
- "Failed to remove missing tag for asset %s: %s", aid, e
- )
- elif update_missing_tags:
- try:
- add_missing_tag_for_asset_id(session, asset_id=aid, origin="automatic")
- except Exception as e:
- logging.warning("Failed to add missing tag for asset %s: %s", aid, e)
+ logging.debug("OSError checking %s: %s", content.path, e)
+ mark_content_missing(session, content.id)
+ else:
+ detect_content_change(
+ session,
+ content,
+ stat_result,
+ hashing_is_enabled=mode.hashing_enabled(),
+ )
+ survivors.add(os.path.abspath(content.path))
- for r in refs:
- if r["exists"]:
- survivors.add(os.path.abspath(r["file_path"]))
+ return survivors if collect_existing_paths else None
- delete_references_by_ids(session, stale_ref_ids)
- stale_set = set(stale_ref_ids)
- to_mark_missing = [ref_id for ref_id in to_mark_missing if ref_id not in stale_set]
- bulk_update_is_missing(session, to_mark_missing, value=True)
- bulk_update_is_missing(session, to_clear_missing, value=False)
- bulk_update_needs_verify(session, to_set_verify, value=True)
- bulk_update_needs_verify(session, to_clear_verify, value=False)
- return survivors if collect_existing_paths else None
+def _is_under_prefixes(path: str, prefixes: list[str]) -> bool:
+ return is_path_under_prefixes(path, prefixes)
def sync_root_safely(root: RootType) -> set[str]:
@@ -270,7 +203,6 @@ def sync_root_safely(root: RootType) -> set[str]:
sess,
root,
collect_existing_paths=True,
- update_missing_tags=True,
)
sess.commit()
return survivors or set()
@@ -296,7 +228,7 @@ def mark_missing_outside_prefixes_safely(prefixes: list[str]) -> int:
"""
try:
with create_session() as sess:
- count = mark_references_missing_outside_prefixes(sess, prefixes)
+ count = mark_contents_missing_outside_prefixes(sess, prefixes)
sess.commit()
return count
except Exception as e:
@@ -304,6 +236,18 @@ def mark_missing_outside_prefixes_safely(prefixes: list[str]) -> int:
return 0
+def mark_contents_missing_outside_prefixes(
+ session: Session, prefixes: list[str]
+) -> int:
+ contents = session.scalars(
+ sa.select(AssetContent).where(AssetContent.is_missing.is_(False))
+ )
+ missing = [content for content in contents if not _is_under_prefixes(content.path, prefixes)]
+ for content in missing:
+ mark_content_missing(session, content.id)
+ return len(missing)
+
+
def collect_paths_for_roots(roots: tuple[RootType, ...]) -> list[str]:
"""Collect all file paths for the given roots."""
paths: list[str] = []
@@ -320,7 +264,6 @@ def build_asset_specs(
paths: list[str],
existing_paths: set[str],
enable_metadata_extraction: bool = True,
- compute_hashes: bool = False,
) -> tuple[list[SeedAssetSpec], set[str], int]:
"""Build asset specs from paths, returning (specs, tag_pool, skipped_count).
@@ -328,14 +271,17 @@ def build_asset_specs(
paths: List of file paths to process
existing_paths: Set of paths that already exist in the database
enable_metadata_extraction: If True, extract tier 1 & 2 metadata
- compute_hashes: If True, compute blake3 hashes (slow for large files)
"""
specs: list[SeedAssetSpec] = []
tag_pool: set[str] = set()
skipped = 0
+ candidates: list[tuple[str, os.stat_result]] = []
for p in paths:
abs_p = os.path.abspath(p)
+ if _should_skip_extension(abs_p):
+ skipped += 1
+ continue
if abs_p in existing_paths:
skipped += 1
continue
@@ -348,6 +294,12 @@ def build_asset_specs(
continue
if not stat_p.st_size:
continue
+ candidates.append((abs_p, stat_p))
+
+ admitted_paths, _ = _two_stat_admit(candidates)
+ candidate_stats = dict(candidates)
+ for abs_p in admitted_paths:
+ stat_p = candidate_stats[abs_p]
name, tags = get_name_and_tags_from_asset_path(abs_p)
rel_fname = compute_loader_path(abs_p)
@@ -360,15 +312,6 @@ def build_asset_specs(
relative_filename=rel_fname,
)
- # Compute hash if requested
- asset_hash: str | None = None
- if compute_hashes:
- try:
- digest, _ = compute_blake3_hash(abs_p)
- asset_hash = "blake3:" + digest
- except Exception as e:
- logging.warning("Failed to hash %s: %s", abs_p, e)
-
mime_type = metadata.content_type if metadata else None
specs.append(
{
@@ -379,7 +322,6 @@ def build_asset_specs(
"tags": tags,
"fname": rel_fname,
"metadata": metadata,
- "hash": asset_hash,
"mime_type": mime_type,
"job_id": None,
}
@@ -389,40 +331,80 @@ def build_asset_specs(
return specs, tag_pool, skipped
+def seed_asset_specs(session: Session, specs: list[SeedAssetSpec]) -> int:
+ created = 0
+ created_content_ids: list[str] = []
+ try:
+ for spec in specs:
+ path = os.path.abspath(spec["abs_path"])
+ try:
+ with session.begin_nested():
+ try:
+ stat_result = os.stat(path, follow_symlinks=True)
+ except OSError:
+ logging.warning("Skipping vanished asset during scan: %s", path)
+ continue
+ try:
+ recovery = recover_missing_content(
+ session,
+ path,
+ stat_result,
+ hashing_is_enabled=mode.hashing_enabled(),
+ )
+ except OSError:
+ logging.warning("Skipping vanished asset during scan: %s", path)
+ continue
+ if recovery != "no_match":
+ continue
+ content, inserted = create_content_reporting_insert(
+ session,
+ path=path,
+ hash=None,
+ size_bytes=stat_result.st_size,
+ mtime_ns=get_mtime_ns(stat_result),
+ )
+ if inserted:
+ created_content_ids.append(content.id)
+ existing_record = session.scalar(
+ sa.select(Asset.id).where(Asset.content_id == content.id).limit(1)
+ )
+ if existing_record is not None:
+ continue
+ create_record(
+ session,
+ content_id=content.id,
+ name=spec["info_name"],
+ mime_type=spec["mime_type"],
+ job_id=spec["job_id"],
+ loader_path=spec["fname"],
+ tags=spec["tags"],
+ )
+ created += 1
+ except IntegrityError:
+ logging.warning("Skipping asset whose row conflicts during scan: %s", path)
+ continue
+ except Exception:
+ session.rollback()
+ for content_id in created_content_ids:
+ _discard_unreferenced_content(session, content_id)
+ raise
+ return created
+
-def insert_asset_specs(specs: list[SeedAssetSpec], tag_pool: set[str]) -> int:
- """Insert asset specs into database, returning count of created refs."""
+def insert_asset_specs(specs: list[SeedAssetSpec], _tag_pool: set[str]) -> int:
if not specs:
return 0
with create_session() as sess:
- if tag_pool:
- ensure_tags_exist(sess, tag_pool)
- result = batch_insert_seed_assets(sess, specs=specs, owner_id="")
+ created = seed_asset_specs(sess, specs)
sess.commit()
- return result.inserted_refs
-
-
-# Enrichment level constants
-ENRICHMENT_STUB = 0 # Fast scan: path, size, mtime only
-ENRICHMENT_METADATA = 1 # Metadata extracted (safetensors header, mime type)
-ENRICHMENT_HASHED = 2 # Hash computed (blake3)
+ return created
def get_unenriched_assets_for_roots(
roots: tuple[RootType, ...],
- max_level: int = ENRICHMENT_STUB,
+ compute_hashes: bool,
limit: int = 1000,
-) -> list:
- """Get assets that need enrichment for the given roots.
-
- Args:
- roots: Tuple of root types to scan
- max_level: Maximum enrichment level to include
- limit: Maximum number of rows to return
-
- Returns:
- List of UnenrichedReferenceRow
- """
+) -> list[UnenrichedContent]:
prefixes: list[str] = []
for root in roots:
prefixes.extend(get_scan_prefixes_for_root(root))
@@ -431,47 +413,61 @@ def get_unenriched_assets_for_roots(
return []
with create_session() as sess:
- return get_unenriched_references(
- sess, prefixes, max_level=max_level, limit=limit
+ query = (
+ sa.select(AssetContent.id, Asset.id, AssetContent.path)
+ .join(Asset, Asset.content_id == AssetContent.id)
+ .where(AssetContent.is_missing.is_(False))
)
+ if compute_hashes:
+ query = query.where(
+ sa.or_(
+ AssetContent.hash.is_(None),
+ Asset.system_metadata.is_(None),
+ )
+ )
+ else:
+ query = query.where(Asset.system_metadata.is_(None))
+ query = query.where(
+ sa.or_(
+ *(sql_path_under_prefix(AssetContent.path, p) for p in prefixes)
+ )
+ )
+ rows = sess.execute(query.order_by(Asset.id).limit(limit)).all()
+
+ return [
+ UnenrichedContent(content_id, record_id, file_path)
+ for content_id, record_id, file_path in rows
+ ]
def enrich_asset(
session,
file_path: str,
- reference_id: str,
- asset_id: str,
+ content_id: str,
+ record_id: str,
extract_metadata: bool = True,
compute_hash: bool = False,
- interrupt_check: Callable[[], bool] | None = None,
- hash_checkpoints: dict[str, HashCheckpoint] | None = None,
-) -> int:
+) -> bool:
"""Enrich a single asset with metadata and/or hash.
Args:
session: Database session (caller manages lifecycle)
file_path: Absolute path to the file
- reference_id: ID of the reference to update
- asset_id: ID of the asset to update (for mime_type and hash)
+ content_id: ID of the content to update
+ record_id: ID of the record to update
extract_metadata: If True, extract safetensors header and mime type
compute_hash: If True, compute blake3 hash
- interrupt_check: Optional non-blocking callable that returns True if
- the operation should be interrupted (e.g. paused or cancelled)
- hash_checkpoints: Optional dict for saving/restoring hash progress
- across interruptions, keyed by file path
Returns:
- New enrichment level achieved
+ Whether enrichment changed the B-schema record or content
"""
- new_level = ENRICHMENT_STUB
-
try:
stat_p = os.stat(file_path, follow_symlinks=True)
except FileNotFoundError:
- return new_level
+ return False
except OSError as e:
_log_scan_error("enrichment_stat", e)
- return new_level
+ return False
initial_mtime_ns = get_mtime_ns(stat_p)
rel_fname = compute_loader_path(file_path)
@@ -486,71 +482,52 @@ def enrich_asset(
)
if metadata:
mime_type = metadata.content_type
- new_level = ENRICHMENT_METADATA
- full_hash: str | None = None
- if compute_hash:
- try:
- mtime_before = get_mtime_ns(stat_p)
- size_before = stat_p.st_size
-
- # Restore checkpoint if available and file unchanged
- checkpoint = None
- if hash_checkpoints is not None:
- checkpoint = hash_checkpoints.get(file_path)
- if checkpoint is not None:
- cur_stat = os.stat(file_path, follow_symlinks=True)
- if (checkpoint.mtime_ns != get_mtime_ns(cur_stat)
- or checkpoint.file_size != cur_stat.st_size):
- checkpoint = None
- hash_checkpoints.pop(file_path, None)
- else:
- mtime_before = get_mtime_ns(cur_stat)
-
- digest, new_checkpoint = compute_blake3_hash(
- file_path,
- interrupt_check=interrupt_check,
- checkpoint=checkpoint,
- )
+ content = session.get(AssetContent, content_id)
- if digest is None:
- # Interrupted — save checkpoint for later resumption
- if hash_checkpoints is not None and new_checkpoint is not None:
- new_checkpoint.mtime_ns = mtime_before
- new_checkpoint.file_size = size_before
- hash_checkpoints[file_path] = new_checkpoint
- return new_level
-
- # Completed — clear any saved checkpoint
- if hash_checkpoints is not None:
- hash_checkpoints.pop(file_path, None)
-
- stat_after = os.stat(file_path, follow_symlinks=True)
- mtime_after = get_mtime_ns(stat_after)
- if mtime_before != mtime_after:
- logging.warning("File modified during hashing, discarding hash: %s", file_path)
- else:
- full_hash = f"blake3:{digest}"
- metadata_ok = not extract_metadata or metadata is not None
- if metadata_ok:
- new_level = ENRICHMENT_HASHED
+ digest: str | None = None
+ stored_hash: str | None = None
+ verified_stat: os.stat_result | None = None
+ hash_requested = compute_hash and content is not None and content.hash is None
+ if hash_requested:
+ try:
+ snapshot = snapshot_hash(file_path)
+ if snapshot is None:
+ logging.warning(
+ "File modified during hashing (snapshot unstable), discarding hash: %s",
+ file_path,
+ )
+ return False
+ digest, verified_stat = snapshot
+ stored_hash = to_stored_hash(digest)
except Exception as e:
if isinstance(e, OSError):
_log_scan_error("hashing", e)
else:
logging.warning("Failed to hash %s: %s", file_path, e)
- # Optimistic guard: if the reference's mtime_ns changed since we
- # started (e.g. ingest_existing_file updated it), our results are
- # stale — discard them to avoid overwriting fresh registration data.
- ref = get_reference_by_id(session, reference_id)
- if ref is None or ref.mtime_ns != initial_mtime_ns:
+ record = session.get(Asset, record_id)
+ if content is None or record is None or content.mtime_ns != initial_mtime_ns:
session.rollback()
logging.info(
- "Ref %s mtime changed during enrichment, discarding stale result",
- reference_id,
+ "Content %s mtime changed during enrichment, discarding stale result",
+ content_id,
)
- return ENRICHMENT_STUB
+ return False
+
+ # Non-NULL system_metadata permanently excludes the row from re-enrichment, so a
+ # disagreement here must discard the metadata too, not just the hash.
+ if verified_stat is not None and (
+ get_mtime_ns(verified_stat) != initial_mtime_ns
+ or verified_stat.st_size != stat_p.st_size
+ ):
+ session.rollback()
+ logging.info(
+ "Content %s changed between its metadata read and its hash read, "
+ "discarding stale result",
+ content_id,
+ )
+ return False
if extract_metadata and metadata:
system_metadata = metadata.to_user_metadata()
@@ -558,32 +535,25 @@ def enrich_asset(
dims = extract_image_dimensions(file_path, mime_type=mime_type)
if dims:
system_metadata.update(dims)
- set_reference_system_metadata(session, reference_id, system_metadata)
-
- if full_hash:
- existing = get_asset_by_hash(session, full_hash)
- if existing and existing.id != asset_id:
- reassign_asset_references(session, asset_id, existing.id, reference_id)
- delete_orphaned_seed_asset(session, asset_id)
- if mime_type:
- update_asset_hash_and_mime(session, existing.id, mime_type=mime_type)
- else:
- update_asset_hash_and_mime(session, asset_id, full_hash, mime_type)
- elif mime_type:
- update_asset_hash_and_mime(session, asset_id, mime_type=mime_type)
+ record.system_metadata = {**(record.system_metadata or {}), **system_metadata}
+
+ if stored_hash:
+ content.hash = stored_hash
+ if mime_type:
+ record.mime_type = mime_type
- bulk_update_enrichment_level(session, [reference_id], new_level)
session.commit()
- return new_level
+ if hash_requested and stored_hash is None:
+ return False
+ return stored_hash is not None or metadata is not None or mime_type is not None
def enrich_assets_batch(
- rows: list,
+ rows: list[UnenrichedContent],
extract_metadata: bool = True,
compute_hash: bool = False,
interrupt_check: Callable[[], bool] | None = None,
- hash_checkpoints: dict[str, HashCheckpoint] | None = None,
) -> tuple[int, list[str]]:
"""Enrich a batch of assets.
@@ -597,8 +567,6 @@ def enrich_assets_batch(
compute_hash: If True, compute hash for each asset
interrupt_check: Optional non-blocking callable that returns True if
the operation should be interrupted (e.g. paused or cancelled)
- hash_checkpoints: Optional dict for saving/restoring hash progress
- across interruptions, keyed by file path
Returns:
Tuple of (enriched_count, failed_reference_ids)
@@ -612,23 +580,21 @@ def enrich_assets_batch(
break
try:
- new_level = enrich_asset(
+ updated = enrich_asset(
sess,
file_path=row.file_path,
- reference_id=row.reference_id,
- asset_id=row.asset_id,
+ content_id=row.content_id,
+ record_id=row.record_id,
extract_metadata=extract_metadata,
compute_hash=compute_hash,
- interrupt_check=interrupt_check,
- hash_checkpoints=hash_checkpoints,
)
- if new_level > row.enrichment_level:
+ if updated:
enriched += 1
else:
- failed_ids.append(row.reference_id)
+ failed_ids.append(row.record_id)
except Exception as e:
logging.warning("Failed to enrich %s: %s", row.file_path, e)
sess.rollback()
- failed_ids.append(row.reference_id)
+ failed_ids.append(row.record_id)
return enriched, failed_ids
diff --git a/app/assets/scanner_admission.py b/app/assets/scanner_admission.py
new file mode 100644
index 000000000..7c059b005
--- /dev/null
+++ b/app/assets/scanner_admission.py
@@ -0,0 +1,97 @@
+"""Decides whether a file observed during a filesystem walk is settled enough to
+catalog, and parks the ones that are not. Admission requires two stats taken a
+moment apart to agree; a file still changing, or carrying a partial-download
+extension, goes onto a bounded watch list that later scans re-check until it
+settles, vanishes, or exhausts its retries. This is what keeps a model that is
+still downloading out of the catalog.
+"""
+
+from __future__ import annotations
+
+import mimetypes
+import os
+import time
+from dataclasses import dataclass
+from typing import Final
+
+from sqlalchemy.orm import Session
+
+from app.assets.services.path_utils import compute_loader_path, get_name_and_tags_from_asset_path
+
+PARTIAL_DOWNLOAD_EXTENSIONS = frozenset({
+ ".part", ".partial", ".crdownload", ".download", ".tmp", ".aria2", ".!qb", ".opdownload",
+})
+_WATCH_SCAN_RETRIES: Final = 30
+_WATCH_LIST_MAX_SIZE: Final = 256
+
+
+@dataclass
+class _WatchEntry:
+ path: str
+ last_stat: os.stat_result
+ ticks: int = 0
+
+
+_WATCH_LIST: list[_WatchEntry] = []
+
+
+def _should_skip_extension(path: str) -> bool:
+ return os.path.splitext(path)[1].lower() in PARTIAL_DOWNLOAD_EXTENSIONS
+
+
+def _two_stat_admit(paths_with_stats: list[tuple[str, os.stat_result]]) -> tuple[list[str], list[str]]:
+ if not paths_with_stats:
+ return [], []
+ time.sleep(0.1)
+ admitted: list[str] = []
+ watched: list[str] = []
+ for path, first_stat in paths_with_stats:
+ try:
+ second_stat = os.stat(path)
+ except FileNotFoundError:
+ continue
+ if (second_stat.st_mtime_ns, second_stat.st_size) == (first_stat.st_mtime_ns, first_stat.st_size):
+ _WATCH_LIST[:] = [entry for entry in _WATCH_LIST if entry.path != path]
+ admitted.append(path)
+ else:
+ for entry in _WATCH_LIST:
+ if entry.path == path:
+ entry.last_stat = second_stat
+ break
+ else:
+ _WATCH_LIST.append(_WatchEntry(path, second_stat))
+ if len(_WATCH_LIST) > _WATCH_LIST_MAX_SIZE:
+ _ = _WATCH_LIST.pop(0)
+ watched.append(path)
+ return admitted, watched
+
+
+def tick_watch_list(session: Session) -> None:
+ from app.assets.scanner import seed_asset_specs, SeedAssetSpec
+
+ remaining: list[_WatchEntry] = []
+ for entry in _WATCH_LIST:
+ try:
+ current = os.stat(entry.path)
+ except FileNotFoundError:
+ continue
+ if (current.st_mtime_ns, current.st_size) == (entry.last_stat.st_mtime_ns, entry.last_stat.st_size):
+ name, tags = get_name_and_tags_from_asset_path(entry.path)
+ spec: SeedAssetSpec = {
+ "abs_path": entry.path,
+ "size_bytes": current.st_size,
+ "mtime_ns": current.st_mtime_ns,
+ "info_name": name,
+ "tags": tags,
+ "fname": compute_loader_path(entry.path),
+ "metadata": None,
+ "mime_type": mimetypes.guess_type(entry.path, strict=False)[0],
+ "job_id": None,
+ }
+ seed_asset_specs(session, [spec])
+ continue
+ entry.last_stat = current
+ entry.ticks += 1
+ if entry.ticks < _WATCH_SCAN_RETRIES:
+ remaining.append(entry)
+ _WATCH_LIST[:] = remaining
diff --git a/app/assets/scanner_changes.py b/app/assets/scanner_changes.py
new file mode 100644
index 000000000..43ee8a47a
--- /dev/null
+++ b/app/assets/scanner_changes.py
@@ -0,0 +1,205 @@
+"""Reconciles catalogued content against what is actually on disk: retiring rows
+whose file is gone, splitting a row whose bytes changed, and recovering one
+whose file came back. Recovery fires only when the returning file's hash
+identifies exactly one missing row and no live row already occupies that path,
+so a restored file can never leave two live rows describing one location.
+"""
+
+from __future__ import annotations
+
+import os
+from pathlib import Path
+from typing import Literal
+
+import sqlalchemy as sa
+from sqlalchemy.orm import Session
+
+from app.assets.database.models import AssetContent
+from app.assets.database.queries.records import (
+ create_content,
+ create_record,
+ mark_content_missing,
+ unset_content_missing,
+)
+from app.assets.helpers import sql_path_under_prefix, to_stored_hash
+from app.assets.services.path_utils import compute_loader_path, get_name_and_tags_from_asset_path
+from app.assets.services.snapshot_hash import snapshot_hash
+
+_pending_verification_ids: list[str] = []
+_pending_recovery_paths: list[str] = []
+
+
+def clear_pending_verifications() -> None:
+ _pending_verification_ids.clear()
+ _pending_recovery_paths.clear()
+
+
+def queue_pending_verification(content_id: str) -> None:
+ if content_id not in _pending_verification_ids:
+ _pending_verification_ids.append(content_id)
+
+
+def pending_recovery_count() -> int:
+ return len(_pending_recovery_paths)
+
+
+def recover_missing_content(
+ session: Session, path: str, stat_result: os.stat_result, hashing_is_enabled: bool
+) -> Literal["recovered", "no_match", "unstable"]:
+ if not hashing_is_enabled:
+ return "no_match"
+ occupied = session.scalar(
+ sa.select(AssetContent.id)
+ .where(AssetContent.path == path, AssetContent.is_missing.is_(False))
+ .limit(1)
+ )
+ if occupied is not None:
+ return "no_match"
+ snapshot = snapshot_hash(path)
+ if snapshot is None:
+ if path not in _pending_recovery_paths:
+ _pending_recovery_paths.append(path)
+ return "unstable"
+ digest, verified_stat = snapshot
+ stored_hash = to_stored_hash(digest)
+ matches = list(
+ session.scalars(
+ sa.select(AssetContent).where(
+ AssetContent.path == path,
+ AssetContent.is_missing.is_(True),
+ AssetContent.hash == stored_hash,
+ )
+ )
+ )
+ if len(matches) == 1:
+ recovered = matches[0]
+ unset_content_missing(session, recovered.id)
+ recovered.size_bytes = verified_stat.st_size
+ recovered.mtime_ns = verified_stat.st_mtime_ns
+ return "recovered"
+ if len(matches) > 1:
+ return "no_match"
+ null_hash_matches = list(
+ session.scalars(
+ sa.select(AssetContent).where(
+ AssetContent.path == path,
+ AssetContent.is_missing.is_(True),
+ AssetContent.hash.is_(None),
+ )
+ )
+ )
+ if len(null_hash_matches) != 1:
+ return "no_match"
+ candidate = null_hash_matches[0]
+ if (candidate.size_bytes, candidate.mtime_ns) != (
+ verified_stat.st_size,
+ verified_stat.st_mtime_ns,
+ ):
+ return "no_match"
+ unset_content_missing(session, candidate.id)
+ candidate.hash = stored_hash
+ candidate.size_bytes = verified_stat.st_size
+ candidate.mtime_ns = verified_stat.st_mtime_ns
+ return "recovered"
+
+
+def is_path_under_prefixes(path: str, prefixes: list[str]) -> bool:
+ candidate = Path(os.path.abspath(path))
+ return any(candidate.is_relative_to(os.path.abspath(prefix)) for prefix in prefixes)
+
+
+def split_content(session: Session, content: AssetContent, stat_result: os.stat_result, hash_value: str | None) -> AssetContent:
+ mark_content_missing(session, content.id)
+ name, tags = get_name_and_tags_from_asset_path(content.path)
+ replacement = create_content(
+ session,
+ path=content.path,
+ hash=hash_value,
+ size_bytes=stat_result.st_size,
+ mtime_ns=stat_result.st_mtime_ns,
+ )
+ create_record(
+ session,
+ content_id=replacement.id,
+ name=name,
+ loader_path=compute_loader_path(content.path),
+ tags=tags,
+ )
+ return replacement
+
+
+def detect_content_change(
+ session: Session,
+ content: AssetContent,
+ stat_result: os.stat_result,
+ hashing_is_enabled: bool,
+) -> None:
+ if content.mtime_ns == stat_result.st_mtime_ns:
+ # Ruling #10: size drift with unchanged mtime is undefined behavior.
+ return
+ if hashing_is_enabled:
+ queue_pending_verification(content.id)
+ return
+ if content.size_bytes == stat_result.st_size:
+ # User identity rule: a same-size mtime bump (rsync, cloud sync, backup restore) is the
+ # same file — never split, or the record's tags and metadata are destroyed.
+ # The stored hash goes with the refreshed stat: OFF mode cannot prove the bytes, and a
+ # refreshed stat alone would re-qualify the row to be served under a digest it may no
+ # longer match.
+ content.size_bytes = stat_result.st_size
+ content.mtime_ns = stat_result.st_mtime_ns
+ content.hash = None
+ return
+ split_content(session, content, stat_result, hash_value=None)
+
+
+def drain_pending_verifications(session: Session, limit: int | None = None) -> int:
+ queued_count = min(len(_pending_verification_ids), limit or len(_pending_verification_ids))
+ processed = 0
+ for _ in range(queued_count):
+ content_id = _pending_verification_ids.pop(0)
+ content = session.get(AssetContent, content_id)
+ if content is None or content.is_missing:
+ continue
+ try:
+ os.stat(content.path, follow_symlinks=True)
+ except FileNotFoundError:
+ mark_content_missing(session, content.id)
+ processed += 1
+ continue
+ except OSError:
+ queue_pending_verification(content_id)
+ continue
+
+ try:
+ snapshot = snapshot_hash(content.path)
+ except OSError:
+ queue_pending_verification(content_id)
+ continue
+ if snapshot is None:
+ queue_pending_verification(content_id)
+ continue
+ digest, verified_stat = snapshot
+ stored_hash = to_stored_hash(digest)
+
+ if content.hash == stored_hash or content.hash is None:
+ content.hash = stored_hash
+ content.size_bytes = verified_stat.st_size
+ content.mtime_ns = verified_stat.st_mtime_ns
+ else:
+ split_content(session, content, verified_stat, hash_value=stored_hash)
+ processed += 1
+ return processed
+
+
+def live_contents_under_prefixes(session: Session, prefixes: list[str]) -> list[AssetContent]:
+ if not prefixes:
+ return []
+ return list(
+ session.scalars(
+ sa.select(AssetContent).where(
+ AssetContent.is_missing.is_(False),
+ sa.or_(*(sql_path_under_prefix(AssetContent.path, prefix) for prefix in prefixes)),
+ )
+ )
+ )
diff --git a/app/assets/seeder.py b/app/assets/seeder.py
index 134fc98a8..82cc110a8 100644
--- a/app/assets/seeder.py
+++ b/app/assets/seeder.py
@@ -1,4 +1,10 @@
-"""Background asset seeder with thread management and cancellation support."""
+"""Runs the filesystem scan on a background thread so startup never waits for it,
+exposing pause, resume, cancel and progress to the API. A run seeds
+newly-observed files first, then enriches records in batches, and settles any
+pending hash-mode transition at the start of the enrich phase so a server that
+receives no prompts still completes the switch. A pass stops once batches stop
+making progress, bounding a scan over files that cannot be read.
+"""
import logging
import os
@@ -6,11 +12,9 @@ import threading
import time
from dataclasses import dataclass, field
from enum import Enum
-from typing import Callable
+from typing import Any, Callable, TypedDict
from app.assets.scanner import (
- ENRICHMENT_METADATA,
- ENRICHMENT_STUB,
RootType,
build_asset_specs,
collect_paths_for_roots,
@@ -22,8 +26,11 @@ from app.assets.scanner import (
mark_missing_outside_prefixes_safely,
sync_root_safely,
sync_temp_references_safely,
+ drain_pending_verifications,
+ tick_watch_list,
)
-from app.database.db import dependencies_available
+from app.assets.services.hash_mode_state import drain_transition_queue, pending_transition_count
+from app.database.db import create_session, dependencies_available
class ScanInProgressError(Exception):
@@ -47,6 +54,12 @@ class ScanPhase(Enum):
FULL = "full" # Both phases sequentially
+class PendingScan(TypedDict):
+ roots: tuple[RootType, ...]
+ phase: ScanPhase
+ compute_hashes: bool
+
+
@dataclass
class Progress:
"""Progress information for a scan operation."""
@@ -94,8 +107,12 @@ class _AssetSeeder:
self._compute_hashes: bool = False
self._prune_first: bool = False
self._progress_callback: ProgressCallback | None = None
+ self._event_sink: Callable[[str, dict[str, Any]], None] | None = None
self._disabled: bool = False
- self._pending_enrich: dict | None = None
+ self._pending_scan: PendingScan | None = None
+
+ def set_event_sink(self, sink: Callable[[str, dict[str, Any]], None] | None) -> None:
+ self._event_sink = sink
def disable(self) -> None:
"""Disable the asset seeder, preventing any scans from starting."""
@@ -113,6 +130,8 @@ class _AssetSeeder:
progress_callback: ProgressCallback | None = None,
prune_first: bool = False,
compute_hashes: bool = False,
+ *,
+ _start_paused: bool = False,
) -> bool:
"""Start a background scan for the given roots.
@@ -122,6 +141,7 @@ class _AssetSeeder:
progress_callback: Optional callback called with progress updates
prune_first: If True, prune orphaned assets before scanning
compute_hashes: If True, compute blake3 hashes (slow)
+ _start_paused: Start with phase work blocked until resume()
Returns:
True if scan was started, False if already running
@@ -134,7 +154,7 @@ class _AssetSeeder:
if self._state != State.IDLE:
logging.info("Asset seeder already running, skipping start")
return False
- self._state = State.RUNNING
+ self._state = State.PAUSED if _start_paused else State.RUNNING
self._progress = Progress()
self._errors = []
self._roots = roots
@@ -143,7 +163,10 @@ class _AssetSeeder:
self._compute_hashes = compute_hashes
self._progress_callback = progress_callback
self._cancel_event.clear()
- self._run_gate.set() # Ensure unpaused when starting
+ if _start_paused:
+ self._run_gate.clear()
+ else:
+ self._run_gate.set()
self._thread = threading.Thread(
target=self._run_scan,
name="_AssetSeeder",
@@ -176,64 +199,40 @@ class _AssetSeeder:
compute_hashes=False,
)
- def start_enrich(
+ def enqueue_scan(
self,
- roots: tuple[RootType, ...] = ("models", "input", "output"),
- progress_callback: ProgressCallback | None = None,
+ roots: tuple[RootType, ...],
+ phase: ScanPhase,
compute_hashes: bool = False,
) -> bool:
- """Start an enrichment scan (phase 2 only) - extracts metadata and hashes.
-
- Args:
- roots: Tuple of root types to scan
- progress_callback: Optional callback for progress updates
- compute_hashes: If True, compute blake3 hashes
-
- Returns:
- True if scan was started, False if already running
- """
- return self.start(
- roots=roots,
- phase=ScanPhase.ENRICH,
- progress_callback=progress_callback,
- prune_first=False,
- compute_hashes=compute_hashes,
- )
-
- def enqueue_enrich(
- self,
- roots: tuple[RootType, ...] = ("models", "input", "output"),
- compute_hashes: bool = False,
- ) -> bool:
- """Start an enrichment scan now, or queue it for after the current scan.
-
- If the seeder is idle, starts immediately. Otherwise, the enrich
- request is stored and will run automatically when the current scan
- finishes.
-
- Args:
- roots: Tuple of root types to scan
- compute_hashes: If True, compute blake3 hashes
-
- Returns:
- True if started immediately, False if queued for later
- """
with self._lock:
- if self.start_enrich(roots=roots, compute_hashes=compute_hashes):
+ if self.start(
+ roots=roots,
+ phase=phase,
+ prune_first=False,
+ compute_hashes=compute_hashes,
+ ):
return True
- if self._pending_enrich is not None:
- existing_roots = set(self._pending_enrich["roots"])
+ if self._pending_scan is not None:
+ existing_roots = set(self._pending_scan["roots"])
existing_roots.update(roots)
- self._pending_enrich["roots"] = tuple(existing_roots)
- self._pending_enrich["compute_hashes"] = (
- self._pending_enrich["compute_hashes"] or compute_hashes
+ self._pending_scan["roots"] = tuple(existing_roots)
+ self._pending_scan["compute_hashes"] = (
+ self._pending_scan["compute_hashes"] or compute_hashes
)
+ if self._pending_scan["phase"] is not phase:
+ self._pending_scan["phase"] = ScanPhase.FULL
else:
- self._pending_enrich = {
+ self._pending_scan = {
"roots": roots,
+ "phase": phase,
"compute_hashes": compute_hashes,
}
- logging.info("Enrich scan queued (roots=%s)", self._pending_enrich["roots"])
+ logging.info(
+ "Scan queued (roots=%s, phase=%s)",
+ self._pending_scan["roots"],
+ self._pending_scan["phase"].value,
+ )
return False
def cancel(self) -> bool:
@@ -370,16 +369,26 @@ class _AssetSeeder:
errors=list(self._errors),
)
- def shutdown(self, timeout: float = 5.0) -> None:
+ def shutdown(self, timeout: float = 5.0) -> bool:
"""Gracefully shutdown: cancel any running scan and wait for thread.
Args:
timeout: Maximum seconds to wait for thread to exit
+
+ Returns:
+ True if the scan thread joined cleanly; False on timeout.
"""
self.cancel()
- self.wait(timeout=timeout)
+ joined = self.wait(timeout=timeout)
+ if not joined:
+ logging.warning(
+ "Asset seeder thread did not exit within %ss",
+ timeout,
+ )
with self._lock:
- self._thread = None
+ if joined:
+ self._thread = None
+ return joined
def mark_missing_outside_prefixes(self) -> int:
"""Mark references as missing when outside all known root prefixes.
@@ -458,13 +467,11 @@ class _AssetSeeder:
self._run_gate.wait() # Blocks if paused
return self._is_cancelled()
- def _emit_event(self, event_type: str, data: dict) -> None:
+ def _emit_event(self, event_type: str, data: dict[str, Any]) -> None:
"""Emit a WebSocket event if server is available."""
try:
- from server import PromptServer
-
- if hasattr(PromptServer, "instance") and PromptServer.instance:
- PromptServer.instance.send_sync(event_type, data)
+ if self._event_sink is not None:
+ self._event_sink(event_type, data)
except Exception:
pass
@@ -639,17 +646,22 @@ class _AssetSeeder:
},
)
with self._lock:
+ start_paused = self._state is State.PAUSED
self._reset_to_idle()
- pending = self._pending_enrich
+ pending = self._pending_scan
if pending is not None:
- self._pending_enrich = None
- if not self.start_enrich(
+ self._pending_scan = None
+ if not self.start(
roots=pending["roots"],
+ phase=pending["phase"],
+ prune_first=False,
compute_hashes=pending["compute_hashes"],
+ _start_paused=start_paused,
):
logging.warning(
- "Pending enrich scan could not start (roots=%s)",
+ "Pending scan could not start (roots=%s, phase=%s)",
pending["roots"],
+ pending["phase"].value,
)
def _run_fast_phase(self, roots: tuple[RootType, ...]) -> tuple[int, int, int]:
@@ -698,7 +710,6 @@ class _AssetSeeder:
paths,
existing_paths,
enable_metadata_extraction=False,
- compute_hashes=False,
)
logging.debug(
"Fast scan: build_asset_specs took %.3fs (%d specs, %d skipped)",
@@ -751,6 +762,9 @@ class _AssetSeeder:
last_progress_time = now
self._update_progress(scanned=len(specs), created=total_created)
+ with create_session() as session:
+ tick_watch_list(session)
+ session.commit()
logging.info(
"Fast scan complete: %.3fs total (created=%d, skipped=%d, total_paths=%d)",
time.perf_counter() - t_fast_start,
@@ -767,16 +781,18 @@ class _AssetSeeder:
Tuple of (cancelled, total_enriched)
"""
total_enriched = 0
+ with create_session() as session:
+ drain_pending_verifications(session)
+ tick_watch_list(session)
+ for _ in range(3):
+ drain_transition_queue(session)
+ session.commit()
+ if pending_transition_count() == 0:
+ break
batch_size = 100
last_progress_time = time.perf_counter()
progress_interval = 1.0
- # Get the target enrichment level based on compute_hashes
- if not self._compute_hashes:
- target_max_level = ENRICHMENT_STUB
- else:
- target_max_level = ENRICHMENT_METADATA
-
self._emit_event(
"assets.seed.started",
{"roots": list(roots), "phase": "enrich"},
@@ -786,10 +802,6 @@ class _AssetSeeder:
consecutive_empty = 0
max_consecutive_empty = 3
- # Hash checkpoints survive across batches so interrupted hashes
- # can be resumed without re-reading the entire file.
- hash_checkpoints: dict[str, object] = {}
-
while True:
if self._check_pause_and_cancel():
logging.info("Enrich scan cancelled after %d assets", total_enriched)
@@ -798,13 +810,13 @@ class _AssetSeeder:
# Fetch next batch of unenriched assets
unenriched = get_unenriched_assets_for_roots(
roots,
- max_level=target_max_level,
+ compute_hashes=self._compute_hashes,
limit=batch_size,
)
# Filter out previously failed references
if skip_ids:
- unenriched = [r for r in unenriched if r.reference_id not in skip_ids]
+ unenriched = [row for row in unenriched if row.record_id not in skip_ids]
if not unenriched:
break
@@ -814,7 +826,6 @@ class _AssetSeeder:
extract_metadata=True,
compute_hash=self._compute_hashes,
interrupt_check=self._is_paused_or_cancelled,
- hash_checkpoints=hash_checkpoints,
)
total_enriched += enriched
skip_ids.update(failed_ids)
diff --git a/app/assets/services/__init__.py b/app/assets/services/__init__.py
index 2747ad35b..e18fb2ce5 100644
--- a/app/assets/services/__init__.py
+++ b/app/assets/services/__init__.py
@@ -1,93 +1,43 @@
-from app.assets.services.asset_management import (
- asset_exists,
- delete_asset_reference,
- get_asset_by_hash,
- get_asset_detail,
- list_assets_page,
- get_preview_file_paths,
- resolve_asset_for_download,
- set_asset_preview,
- update_asset_metadata,
-)
-from app.assets.services.bulk_ingest import (
- BulkInsertResult,
- batch_insert_seed_assets,
- cleanup_unreferenced_assets,
-)
-from app.assets.services.file_utils import (
- get_mtime_ns,
- get_size_and_mtime_ns,
- list_files_recursively,
- verify_file_unchanged,
-)
+"""Re-exports the asset service functions so routes and the seeder import them
+from one place instead of reaching into individual service modules.
+"""
+
from app.assets.services.ingest import (
DependencyMissingError,
HashMismatchError,
- create_from_hash,
- ingest_existing_file,
- register_output_files,
+ UploadUnstableError,
upload_from_temp_path,
+ create_from_hash,
+ register_file_in_place,
)
-from app.assets.database.queries import (
- AddTagsResult,
- RemoveTagsResult,
-)
-from app.assets.services.schemas import (
- AssetData,
- AssetDetailResult,
- AssetSummaryData,
- DownloadResolutionResult,
- IngestResult,
- ListAssetsResult,
- ReferenceData,
- RegisterAssetResult,
- TagUsage,
- UploadResult,
- UserMetadata,
+from app.assets.services.asset_management import (
+ get_asset_detail,
+ update_asset_metadata,
+ delete_asset_reference,
+ asset_exists,
+ get_preview_file_paths,
+ resolve_asset_for_download,
)
from app.assets.services.tagging import (
apply_tags,
- list_tags,
remove_tags,
+ list_tags,
)
__all__ = [
- "AddTagsResult",
- "AssetData",
- "AssetDetailResult",
- "AssetSummaryData",
- "ReferenceData",
- "BulkInsertResult",
"DependencyMissingError",
- "DownloadResolutionResult",
"HashMismatchError",
- "IngestResult",
- "ListAssetsResult",
- "RegisterAssetResult",
- "RemoveTagsResult",
- "TagUsage",
- "UploadResult",
- "UserMetadata",
- "apply_tags",
- "asset_exists",
- "batch_insert_seed_assets",
+ "UploadUnstableError",
+ "upload_from_temp_path",
"create_from_hash",
- "delete_asset_reference",
- "get_asset_by_hash",
+ "register_file_in_place",
"get_asset_detail",
- "ingest_existing_file",
- "register_output_files",
- "get_mtime_ns",
- "get_size_and_mtime_ns",
- "list_assets_page",
- "list_files_recursively",
- "list_tags",
- "cleanup_unreferenced_assets",
- "remove_tags",
+ "update_asset_metadata",
+ "delete_asset_reference",
+ "asset_exists",
"get_preview_file_paths",
"resolve_asset_for_download",
- "set_asset_preview",
- "update_asset_metadata",
- "upload_from_temp_path",
- "verify_file_unchanged",
+ "apply_tags",
+ "remove_tags",
+ "list_tags",
]
diff --git a/app/assets/services/asset_management.py b/app/assets/services/asset_management.py
index a04b7f21a..0f7ad716c 100644
--- a/app/assets/services/asset_management.py
+++ b/app/assets/services/asset_management.py
@@ -1,77 +1,87 @@
-import contextlib
+"""Serves the per-asset operations behind the API: reading an asset's detail,
+updating its name, tags, metadata and preview, deleting a record, and resolving
+a hash to a servable path. An update moves ``updated_at`` only when it actually
+changed something, so a call that requests what is already true is not recorded
+as a user edit.
+"""
+
import mimetypes
import os
-from datetime import timezone
from typing import Sequence
-from app.assets.services.cursor import (
- CursorPayload,
- InvalidCursorError,
- decode_cursor,
- decode_cursor_int,
- decode_cursor_time,
- encode_cursor,
- encode_cursor_from_time,
-)
-
+from sqlalchemy import delete, select, update
-from app.assets.database.models import Asset
+from app.assets.database.models import Asset, AssetContent, AssetTag
from app.assets.database.queries import (
- asset_exists_by_hash,
- reference_exists_for_asset_id,
- delete_reference_by_id,
- fetch_reference_and_asset,
- get_reference_paths_by_ids,
- soft_delete_reference_by_id,
- fetch_reference_asset_and_tags,
- get_asset_by_hash as queries_get_asset_by_hash,
- get_reference_by_id,
- get_reference_with_owner_check,
- list_references_page,
- list_all_file_paths_by_asset_id,
- list_references_by_asset_id,
- set_reference_metadata,
- set_reference_preview,
- set_reference_tags,
- update_asset_hash_and_mime,
- update_reference_access_time,
- update_reference_name,
- update_reference_updated_at,
+ delete_record,
+ fetch_record_tags,
+ get_record_by_id,
+ update_record_access_time,
)
-from app.assets.helpers import select_best_live_path
-from app.assets.services.path_utils import compute_loader_path
+from app.assets.database.queries.records import (
+ bump_record_updated_at,
+ ensure_tag,
+ ensure_tag_link,
+ get_preview_file_paths_by_ids,
+ rename_record,
+)
+from app.assets.helpers import get_utc_now, normalize_tags, validate_blake3_hash
+from app.assets.services.lookup import lookup_for_view
from app.assets.services.schemas import (
AssetData,
AssetDetailResult,
- AssetSummaryData,
DownloadResolutionResult,
- ListAssetsResult,
+ ReferenceData,
UserMetadata,
- extract_asset_data,
- extract_reference_data,
)
from app.database.db import create_session
+def _record_to_detail_result(session, record) -> AssetDetailResult:
+ content = session.get(AssetContent, record.content_id)
+ tags = fetch_record_tags(session, record.id)
+ api_hash = content.hash if content else None
+ ref = ReferenceData(
+ id=record.id,
+ name=record.name,
+ file_path=content.path if content else None,
+ loader_path=record.loader_path,
+ user_metadata=record.user_metadata,
+ preview_id=record.preview_id,
+ system_metadata=record.system_metadata,
+ job_id=record.job_id,
+ created_at=record.created_at,
+ updated_at=record.updated_at,
+ last_access_time=record.last_access_time,
+ )
+ asset = AssetData(
+ hash=api_hash,
+ size_bytes=content.size_bytes if content else None,
+ mime_type=record.mime_type,
+ is_missing=bool(content and content.is_missing),
+ )
+ return AssetDetailResult(ref=ref, asset=asset, tags=tags)
+
+
def get_asset_detail(
reference_id: str,
- owner_id: str = "",
) -> AssetDetailResult | None:
with create_session() as session:
- result = fetch_reference_asset_and_tags(
- session,
- reference_id=reference_id,
- owner_id=owner_id,
- )
- if not result:
+ record = get_record_by_id(session, reference_id)
+ if record is None:
return None
+ return _record_to_detail_result(session, record)
- ref, asset, tags = result
- return AssetDetailResult(
- ref=extract_reference_data(ref),
- asset=extract_asset_data(asset),
- tags=tags,
+
+def _fetch_manual_tags(session, reference_id: str) -> set[str]:
+ return set(
+ session.scalars(
+ select(AssetTag.tag_name).where(
+ AssetTag.asset_id == reference_id,
+ AssetTag.origin != "automatic",
+ )
)
+ )
def update_asset_metadata(
@@ -80,342 +90,132 @@ def update_asset_metadata(
tags: Sequence[str] | None = None,
user_metadata: UserMetadata = None,
tag_origin: str = "manual",
- owner_id: str = "",
mime_type: str | None = None,
preview_id: str | None = None,
) -> AssetDetailResult:
with create_session() as session:
- ref = get_reference_with_owner_check(session, reference_id, owner_id)
+ record = get_record_by_id(session, reference_id)
+ if record is None:
+ raise ValueError(f"Asset {reference_id} not found")
- touched = False
- if name is not None and name != ref.name:
- update_reference_name(session, reference_id=reference_id, name=name)
- touched = True
-
- computed_filename = compute_loader_path(ref.file_path) if ref.file_path else None
-
- new_meta: dict | None = None
+ if name is not None:
+ rename_record(session, reference_id, name)
if user_metadata is not None:
- new_meta = dict(user_metadata)
- elif computed_filename:
- current_meta = ref.user_metadata or {}
- if current_meta.get("filename") != computed_filename:
- new_meta = dict(current_meta)
-
- if new_meta is not None:
- if computed_filename:
- new_meta["filename"] = computed_filename
- set_reference_metadata(
- session, reference_id=reference_id, user_metadata=new_meta
+ session.execute(
+ update(Asset)
+ .where(Asset.id == reference_id)
+ .values(user_metadata=dict(user_metadata), updated_at=get_utc_now())
)
- touched = True
-
+ manual_tags_before: set[str] = set()
if tags is not None:
- set_reference_tags(
- session,
- reference_id=reference_id,
- tags=tags,
- origin=tag_origin,
+ manual_tags_before = _fetch_manual_tags(session, reference_id)
+ session.execute(
+ delete(AssetTag).where(
+ AssetTag.asset_id == reference_id,
+ AssetTag.origin != "automatic",
+ )
)
- touched = True
-
if mime_type is not None:
- updated = update_asset_hash_and_mime(
- session, asset_id=ref.asset_id, mime_type=mime_type
+ session.execute(
+ update(Asset)
+ .where(Asset.id == reference_id)
+ .values(mime_type=mime_type, updated_at=get_utc_now())
)
- if updated:
- touched = True
-
if preview_id is not None:
- set_reference_preview(
- session,
- reference_id=reference_id,
- preview_reference_id=preview_id,
+ if session.get(Asset, preview_id) is None:
+ raise ValueError(
+ f"preview_id {preview_id!r} does not reference an existing asset"
+ )
+ session.execute(
+ update(Asset)
+ .where(Asset.id == reference_id)
+ .values(preview_id=preview_id, updated_at=get_utc_now())
)
- touched = True
-
- if touched and user_metadata is None:
- update_reference_updated_at(session, reference_id=reference_id)
-
- result = fetch_reference_asset_and_tags(
- session,
- reference_id=reference_id,
- owner_id=owner_id,
- )
- if not result:
- raise RuntimeError("State changed during update")
-
- ref, asset, tag_list = result
- detail = AssetDetailResult(
- ref=extract_reference_data(ref),
- asset=extract_asset_data(asset),
- tags=tag_list,
- )
+ if tags is not None:
+ for tag_name in normalize_tags(list(tags)):
+ ensure_tag(session, tag_name)
+ ensure_tag_link(
+ session,
+ asset_id=reference_id,
+ tag_name=tag_name,
+ origin=tag_origin,
+ )
+ session.flush()
+ if _fetch_manual_tags(session, reference_id) != manual_tags_before:
+ bump_record_updated_at(session, reference_id)
session.commit()
- return detail
+ detail = get_asset_detail(reference_id)
+ if detail is None:
+ raise RuntimeError("Asset deleted during update")
+ return detail
def delete_asset_reference(
reference_id: str,
- owner_id: str,
- delete_content_if_orphan: bool = True,
) -> bool:
- """Delete an asset reference.
-
- With ``delete_content_if_orphan=False`` (a soft delete), the reference is
- hidden and the underlying content is preserved. With ``True``, the content
- is also removed once it becomes orphaned.
-
- Note: the public DELETE /api/assets/{id} endpoint always soft-deletes
- (passes ``False``); the orphan-reclamation path is intentionally
- internal-only, retained for a future GC/admin caller.
- """
with create_session() as session:
- if not delete_content_if_orphan:
- # Soft delete: mark the reference as deleted but keep everything
- deleted = soft_delete_reference_by_id(
- session, reference_id=reference_id, owner_id=owner_id
- )
- session.commit()
- return deleted
-
- ref_row = get_reference_by_id(session, reference_id=reference_id)
- asset_id = ref_row.asset_id if ref_row else None
- file_path = ref_row.file_path if ref_row else None
-
- deleted = delete_reference_by_id(
- session, reference_id=reference_id, owner_id=owner_id
- )
- if not deleted:
- session.commit()
+ if get_record_by_id(session, reference_id) is None:
return False
-
- if not asset_id:
- session.commit()
- return True
-
- still_exists = reference_exists_for_asset_id(session, asset_id=asset_id)
- if still_exists:
- session.commit()
- return True
-
- # Orphaned asset - gather ALL file paths (including
- # soft-deleted / missing refs) so their on-disk files get cleaned up.
- file_paths = list_all_file_paths_by_asset_id(session, asset_id=asset_id)
- # Also include the just-deleted file path
- if file_path:
- file_paths.append(file_path)
-
- asset_row = session.get(Asset, asset_id)
- if asset_row is not None:
- session.delete(asset_row)
-
- session.commit()
-
- # Delete files after commit
- for p in file_paths:
- with contextlib.suppress(Exception):
- if p and os.path.isfile(p):
- os.remove(p)
-
- return True
-
-
-def set_asset_preview(
- reference_id: str,
- preview_reference_id: str | None = None,
- owner_id: str = "",
-) -> AssetDetailResult:
- with create_session() as session:
- get_reference_with_owner_check(session, reference_id, owner_id)
-
- set_reference_preview(
- session,
- reference_id=reference_id,
- preview_reference_id=preview_reference_id,
- )
-
- result = fetch_reference_asset_and_tags(
- session, reference_id=reference_id, owner_id=owner_id
- )
- if not result:
- raise RuntimeError("State changed during preview update")
-
- ref, asset, tags = result
- detail = AssetDetailResult(
- ref=extract_reference_data(ref),
- asset=extract_asset_data(asset),
- tags=tags,
- )
+ delete_record(session, reference_id)
session.commit()
-
- return detail
+ return True
def asset_exists(asset_hash: str) -> bool:
+ try:
+ canonical = validate_blake3_hash(asset_hash)
+ except ValueError:
+ return False
with create_session() as session:
- return asset_exists_by_hash(session, asset_hash=asset_hash)
-
-
-def get_asset_by_hash(asset_hash: str) -> AssetData | None:
- with create_session() as session:
- asset = queries_get_asset_by_hash(session, asset_hash=asset_hash)
- return extract_asset_data(asset)
-
-
-# Sort fields that support cursor pagination. `last_access_time` is not
-# in this list — it falls back to offset/limit.
-_CURSOR_SORT_FIELDS = ("created_at", "updated_at", "name", "size")
-
-
-def list_assets_page(
- owner_id: str = "",
- include_tags: Sequence[str] | None = None,
- exclude_tags: Sequence[str] | None = None,
- name_contains: str | None = None,
- metadata_filter: dict | None = None,
- limit: int = 20,
- offset: int = 0,
- sort: str = "created_at",
- order: str = "desc",
- after: str | None = None,
- # Appended last so pre-existing positional callers keep binding correctly.
- any_tags: Sequence[str] | None = None,
-) -> ListAssetsResult:
- """List assets with optional cursor pagination.
-
- When ``after`` is supplied it overrides ``offset``. The cursor's sort field
- must match ``sort`` and be in the cursor-supported allowlist; mismatches
- raise InvalidCursorError so the handler can map to 400 INVALID_CURSOR.
- """
- cursor_value: object | None = None
- cursor_id: str | None = None
- # Mint next_cursor on every page where the sort is cursor-supported, not
- # only when the request itself arrived with a cursor. Otherwise a first
- # request (no `after`) returns next_cursor=None and the client can never
- # enter cursor mode.
- mint_cursor = sort in _CURSOR_SORT_FIELDS
-
- if after is not None:
- if sort not in _CURSOR_SORT_FIELDS:
- raise InvalidCursorError(
- f"cursor pagination is not supported for sort={sort!r}"
- )
- payload = decode_cursor(after, _CURSOR_SORT_FIELDS, expected_order=order)
- if payload.sort_field != sort:
- raise InvalidCursorError(
- f"cursor sort field {payload.sort_field!r} does not match request sort {sort!r}"
- )
- cursor_value, cursor_id = _resolve_cursor_value(payload), payload.id
-
- # Over-fetch by one row so we can distinguish "exactly `limit` rows total
- # remaining" from "more rows past this page" without a second query. Drop
- # the sentinel before returning.
- fetch_limit = limit + 1 if mint_cursor else limit
-
- with create_session() as session:
- refs, tag_map, total = list_references_page(
- session,
- owner_id=owner_id,
- include_tags=include_tags,
- exclude_tags=exclude_tags,
- any_tags=any_tags,
- name_contains=name_contains,
- metadata_filter=metadata_filter,
- limit=fetch_limit,
- offset=offset,
- sort=sort,
- order=order,
- after_cursor_value=cursor_value,
- after_cursor_id=cursor_id,
- )
-
- next_cursor: str | None = None
- if mint_cursor and len(refs) > limit:
- # There's at least one more row past this page — mint a cursor from
- # the last row of the page (i.e. index `limit - 1`, since we
- # over-fetched), and drop the sentinel.
- next_cursor = _encode_next_cursor(refs[limit - 1], sort, order)
- refs = refs[:limit]
-
- items: list[AssetSummaryData] = []
- for ref in refs:
- items.append(
- AssetSummaryData(
- ref=extract_reference_data(ref),
- asset=extract_asset_data(ref.asset),
- tags=tag_map.get(ref.id, []),
- )
- )
-
- return ListAssetsResult(items=items, total=total, next_cursor=next_cursor)
-
-
-def _resolve_cursor_value(payload: CursorPayload) -> object:
- """Map a decoded cursor payload to a column-typed Python value."""
- if payload.sort_field in ("created_at", "updated_at"):
- # DB stores naive UTC; strip tzinfo so the comparison binds against a
- # `TIMESTAMP WITHOUT TIME ZONE` column without an offset shift.
- return decode_cursor_time(payload).replace(tzinfo=None)
- if payload.sort_field == "size":
- return decode_cursor_int(payload)
- return payload.value # name, str-typed
-
-
-def _encode_next_cursor(ref, sort: str, order: str) -> str | None:
- """Mint a cursor pointing at *ref* for the given sort dimension.
-
- Returns None when the boundary row carries a NULL sort value (e.g. an asset
- record whose size_bytes hasn't been backfilled). Continuing pagination
- across a NULL boundary is undefined under keyset ordering — better to
- truncate cleanly here than to mint a cursor that mis-positions.
- """
- if sort == "name":
- return encode_cursor("name", ref.name, ref.id, order=order)
- if sort == "size":
- if ref.asset is None or ref.asset.size_bytes is None:
- return None
- return encode_cursor("size", str(ref.asset.size_bytes), ref.id, order=order)
- # created_at / updated_at — DB datetimes are naive UTC; attach tz before encoding.
- value = ref.created_at if sort == "created_at" else ref.updated_at
- if value is None:
- return None
- return encode_cursor_from_time(sort, value.replace(tzinfo=timezone.utc), ref.id, order=order)
+ return lookup_for_view(session, canonical) is not None
def resolve_hash_to_path(
asset_hash: str,
- owner_id: str = "",
) -> DownloadResolutionResult | None:
- """Resolve a blake3 hash to an on-disk file path.
+ """Resolve a blake3 hash to an on-disk file path via lookup_for_view.
- Only references visible to *owner_id* are considered (owner-less
- references are always visible).
+ Uses the first qualified live content row. Temp paths are excluded from all
+ hash lookups inside qualified_content_iterator, so a hash resolving only to
+ temp content returns None. Updates last_access_time on every record pointing
+ at the served content.
- Returns a DownloadResolutionResult with abs_path, content_type, and
- download_name, or None if no asset or live path is found.
+ Filename and Content-Type both come from the newest record so they never
+ describe different records. Deleting the last record preserves its content,
+ so content with zero records stays servable off the content path alone.
"""
+ try:
+ canonical = validate_blake3_hash(asset_hash)
+ except ValueError:
+ return None
with create_session() as session:
- asset = queries_get_asset_by_hash(session, asset_hash)
- if not asset:
- return None
- refs = list_references_by_asset_id(session, asset_id=asset.id)
- visible = [
- r for r in refs
- if r.owner_id == "" or r.owner_id == owner_id
- ]
- abs_path = select_best_live_path(visible)
- if not abs_path:
+ content = lookup_for_view(session, canonical)
+ if content is None:
return None
- display_name = os.path.basename(abs_path)
- for ref in visible:
- if ref.file_path == abs_path and ref.name:
- display_name = ref.name
- break
+
+ records = list(
+ session.scalars(
+ select(Asset)
+ .where(Asset.content_id == content.id)
+ .order_by(Asset.created_at, Asset.id)
+ )
+ )
+ display_name = os.path.basename(content.path)
+ mime_type = None
+ if records:
+ latest_record = records[-1]
+ display_name = latest_record.name or display_name
+ mime_type = latest_record.mime_type
+ for record in records:
+ update_record_access_time(session, record.id)
+ abs_path = content.path
+ session.commit()
+
ctype = (
- asset.mime_type
+ mime_type
or mimetypes.guess_type(display_name)[0]
+ or mimetypes.guess_type(abs_path)[0]
or "application/octet-stream"
)
return DownloadResolutionResult(
@@ -430,40 +230,33 @@ def get_preview_file_paths(preview_ids: list[str]) -> dict[str, str]:
if not preview_ids:
return {}
with create_session() as session:
- return get_reference_paths_by_ids(session, reference_ids=preview_ids)
+ return get_preview_file_paths_by_ids(session, preview_ids=preview_ids)
def resolve_asset_for_download(
reference_id: str,
- owner_id: str = "",
) -> DownloadResolutionResult:
with create_session() as session:
- pair = fetch_reference_and_asset(
- session, reference_id=reference_id, owner_id=owner_id
- )
- if not pair:
+ record = get_record_by_id(session, reference_id)
+ if record is None:
raise ValueError(f"AssetReference {reference_id} not found")
- ref, asset = pair
-
- # For references with file_path, use that directly
- if ref.file_path and os.path.isfile(ref.file_path):
- abs_path = ref.file_path
- else:
- # For API-created refs without file_path, find a path from other refs
- refs = list_references_by_asset_id(session, asset_id=asset.id)
- abs_path = select_best_live_path(refs)
- if not abs_path:
- raise FileNotFoundError(
- f"No live path for AssetReference {reference_id} "
- f"(asset id={asset.id}, name={ref.name})"
- )
+ content = session.get(AssetContent, record.content_id)
+ if (
+ content is None
+ or content.is_missing
+ or not os.path.isfile(content.path)
+ ):
+ raise FileNotFoundError(
+ f"No live content for AssetReference {reference_id} "
+ f"(content id={record.content_id}, name={record.name})"
+ )
- # Capture ORM attributes before commit (commit expires loaded objects)
- ref_name = ref.name
- asset_mime = asset.mime_type
+ ref_name = record.name
+ asset_mime = record.mime_type
+ abs_path = content.path
- update_reference_access_time(session, reference_id=reference_id)
+ update_record_access_time(session, reference_id)
session.commit()
ctype = (
diff --git a/app/assets/services/bulk_ingest.py b/app/assets/services/bulk_ingest.py
deleted file mode 100644
index c98658bf1..000000000
--- a/app/assets/services/bulk_ingest.py
+++ /dev/null
@@ -1,294 +0,0 @@
-from __future__ import annotations
-
-import os
-import uuid
-from dataclasses import dataclass
-from datetime import datetime
-from typing import TYPE_CHECKING, Any, TypedDict
-
-from sqlalchemy.orm import Session
-
-from app.assets.database.queries import (
- bulk_insert_assets,
- bulk_insert_references_ignore_conflicts,
- bulk_insert_tags_and_meta,
- delete_assets_by_ids,
- get_existing_asset_ids,
- get_reference_ids_by_ids,
- get_references_by_paths_and_asset_ids,
- get_unreferenced_unhashed_asset_ids,
- restore_references_by_paths,
-)
-from app.assets.helpers import get_utc_now
-
-if TYPE_CHECKING:
- from app.assets.services.metadata_extract import ExtractedMetadata
-
-
-class SeedAssetSpec(TypedDict):
- """Spec for seeding an asset from filesystem."""
-
- abs_path: str
- size_bytes: int
- mtime_ns: int
- info_name: str
- tags: list[str]
- fname: str
- metadata: ExtractedMetadata | None
- hash: str | None
- mime_type: str | None
- job_id: str | None
-
-
-class AssetRow(TypedDict):
- """Row data for inserting an Asset."""
-
- id: str
- hash: str | None
- size_bytes: int
- mime_type: str | None
- created_at: datetime
-
-
-class ReferenceRow(TypedDict):
- """Row data for inserting an AssetReference."""
-
- id: str
- asset_id: str
- file_path: str
- loader_path: str | None
- mtime_ns: int
- owner_id: str
- name: str
- preview_id: str | None
- user_metadata: dict[str, Any] | None
- job_id: str | None
- created_at: datetime
- updated_at: datetime
- last_access_time: datetime
-
-
-class TagRow(TypedDict):
- """Row data for inserting a Tag."""
-
- asset_reference_id: str
- tag_name: str
- origin: str
- added_at: datetime
-
-
-class MetadataRow(TypedDict):
- """Row data for inserting asset metadata."""
-
- asset_reference_id: str
- key: str
- ordinal: int
- val_str: str | None
- val_num: float | None
- val_bool: bool | None
- val_json: dict[str, Any] | None
-
-
-@dataclass
-class BulkInsertResult:
- """Result of bulk asset insertion."""
-
- inserted_refs: int
- won_paths: int
- lost_paths: int
-
-
-def batch_insert_seed_assets(
- session: Session,
- specs: list[SeedAssetSpec],
- owner_id: str = "",
-) -> BulkInsertResult:
- """Seed assets from filesystem specs in batch.
-
- Each spec is a dict with keys:
- - abs_path: str
- - size_bytes: int
- - mtime_ns: int
- - info_name: str
- - tags: list[str]
- - fname: Optional[str]
-
- This function orchestrates:
- 1. Insert seed Assets (hash=NULL)
- 2. Claim references with ON CONFLICT DO NOTHING on file_path
- 3. Query to find winners (paths where our asset_id was inserted)
- 4. Delete Assets for losers (path already claimed by another asset)
- 5. Insert tags and metadata for successfully inserted references
-
- Returns:
- BulkInsertResult with inserted_refs, won_paths, lost_paths
- """
- if not specs:
- return BulkInsertResult(inserted_refs=0, won_paths=0, lost_paths=0)
-
- current_time = get_utc_now()
- asset_rows: list[AssetRow] = []
- reference_rows: list[ReferenceRow] = []
- path_to_asset_id: dict[str, str] = {}
- asset_id_to_ref_data: dict[str, dict] = {}
- absolute_path_list: list[str] = []
-
- for spec in specs:
- absolute_path = os.path.abspath(spec["abs_path"])
- existing_asset_id = path_to_asset_id.get(absolute_path)
- if existing_asset_id is not None:
- existing_tags = asset_id_to_ref_data[existing_asset_id]["tags"]
- asset_id_to_ref_data[existing_asset_id]["tags"] = list(
- dict.fromkeys([*existing_tags, *spec["tags"]])
- )
- continue
-
- asset_id = str(uuid.uuid4())
- reference_id = str(uuid.uuid4())
- absolute_path_list.append(absolute_path)
- path_to_asset_id[absolute_path] = asset_id
-
- mime_type = spec.get("mime_type")
- asset_rows.append(
- {
- "id": asset_id,
- "hash": spec.get("hash"),
- "size_bytes": spec["size_bytes"],
- "mime_type": mime_type,
- "created_at": current_time,
- }
- )
-
- # Build user_metadata from extracted metadata or fallback to filename
- extracted_metadata = spec.get("metadata")
- if extracted_metadata:
- user_metadata: dict[str, Any] | None = extracted_metadata.to_user_metadata()
- elif spec["fname"]:
- user_metadata = {"filename": spec["fname"]}
- else:
- user_metadata = None
-
- reference_rows.append(
- {
- "id": reference_id,
- "asset_id": asset_id,
- "file_path": absolute_path,
- # spec["fname"] is compute_loader_path(abs_path) from build_asset_specs.
- "loader_path": spec["fname"],
- "mtime_ns": spec["mtime_ns"],
- "owner_id": owner_id,
- "name": spec["info_name"],
- "preview_id": None,
- "user_metadata": user_metadata,
- "job_id": spec.get("job_id"),
- "created_at": current_time,
- "updated_at": current_time,
- "last_access_time": current_time,
- }
- )
-
- asset_id_to_ref_data[asset_id] = {
- "reference_id": reference_id,
- "tags": spec["tags"],
- "filename": spec["fname"],
- "extracted_metadata": extracted_metadata,
- }
-
- bulk_insert_assets(session, asset_rows)
-
- # Filter reference rows to only those whose assets were actually inserted
- # (assets with duplicate hashes are silently dropped by ON CONFLICT DO NOTHING)
- inserted_asset_ids = get_existing_asset_ids(
- session, [r["asset_id"] for r in reference_rows]
- )
- reference_rows = [r for r in reference_rows if r["asset_id"] in inserted_asset_ids]
-
- bulk_insert_references_ignore_conflicts(session, reference_rows)
- restore_references_by_paths(session, absolute_path_list)
- winning_paths = get_references_by_paths_and_asset_ids(session, path_to_asset_id)
-
- inserted_paths = {
- path
- for path in absolute_path_list
- if path_to_asset_id[path] in inserted_asset_ids
- }
- losing_paths = inserted_paths - winning_paths
- lost_asset_ids = [path_to_asset_id[path] for path in losing_paths]
-
- if lost_asset_ids:
- delete_assets_by_ids(session, lost_asset_ids)
-
- if not winning_paths:
- return BulkInsertResult(
- inserted_refs=0,
- won_paths=0,
- lost_paths=len(losing_paths),
- )
-
- # Get reference IDs for winners
- winning_ref_ids = [
- asset_id_to_ref_data[path_to_asset_id[path]]["reference_id"]
- for path in winning_paths
- ]
- inserted_ref_ids = get_reference_ids_by_ids(session, winning_ref_ids)
-
- tag_rows: list[TagRow] = []
- metadata_rows: list[MetadataRow] = []
-
- if inserted_ref_ids:
- for path in winning_paths:
- asset_id = path_to_asset_id[path]
- ref_data = asset_id_to_ref_data[asset_id]
- ref_id = ref_data["reference_id"]
-
- if ref_id not in inserted_ref_ids:
- continue
-
- for tag in ref_data["tags"]:
- tag_rows.append(
- {
- "asset_reference_id": ref_id,
- "tag_name": tag,
- "origin": "automatic",
- "added_at": current_time,
- }
- )
-
- # Use extracted metadata for meta rows if available
- extracted_metadata = ref_data.get("extracted_metadata")
- if extracted_metadata:
- metadata_rows.extend(extracted_metadata.to_meta_rows(ref_id))
- elif ref_data["filename"]:
- # Fallback: just store filename
- metadata_rows.append(
- {
- "asset_reference_id": ref_id,
- "key": "filename",
- "ordinal": 0,
- "val_str": ref_data["filename"],
- "val_num": None,
- "val_bool": None,
- "val_json": None,
- }
- )
-
- bulk_insert_tags_and_meta(session, tag_rows=tag_rows, meta_rows=metadata_rows)
-
- return BulkInsertResult(
- inserted_refs=len(inserted_ref_ids),
- won_paths=len(winning_paths),
- lost_paths=len(losing_paths),
- )
-
-
-def cleanup_unreferenced_assets(session: Session) -> int:
- """Hard-delete unhashed assets with no active references.
-
- This is a destructive operation intended for explicit cleanup.
- Only deletes assets where hash=None and all references are missing.
-
- Returns:
- Number of assets deleted
- """
- unreferenced_ids = get_unreferenced_unhashed_asset_ids(session)
- return delete_assets_by_ids(session, unreferenced_ids)
diff --git a/app/assets/services/hash_mode_state.py b/app/assets/services/hash_mode_state.py
new file mode 100644
index 000000000..0e802a951
--- /dev/null
+++ b/app/assets/services/hash_mode_state.py
@@ -0,0 +1,189 @@
+"""Tracks the persisted hashing mode and carries the existing catalog across an
+off-to-on switch. Turning hashing on enqueues every live content row for
+verification and the queue is drained in the background; a path that cannot be
+read is retried a bounded number of times and then has its stored hash cleared,
+so the queue can empty and the mode can flip. An unverifiable digest never
+survives into the persisted enabled state.
+"""
+
+from __future__ import annotations
+
+import logging
+import os
+from collections import deque
+from dataclasses import dataclass
+from typing import Final
+
+from sqlalchemy import select
+from sqlalchemy.orm import Session
+
+from app.assets import mode as _mode
+from app.assets.database.models import AssetContent, AssetSystemState
+from app.assets.database.queries.records import create_content, create_record, mark_content_missing
+from app.assets.helpers import to_stored_hash
+from app.assets.services.path_utils import compute_loader_path, get_name_and_tags_from_asset_path
+from app.assets.services.snapshot_hash import snapshot_hash
+
+_KEY = "hash_mode"
+_MAX_VERIFY_ATTEMPTS: Final = 3
+
+
+@dataclass
+class _PendingEntry:
+ path: str
+ ticks: int = 0
+
+
+_PENDING_QUEUE: deque[_PendingEntry] = deque()
+_PENDING_PATHS: set[str] = set()
+_off_to_on_transition_in_flight = False
+
+
+def clear_transition_queue() -> None:
+ global _off_to_on_transition_in_flight
+
+ _PENDING_QUEUE.clear()
+ _PENDING_PATHS.clear()
+ _off_to_on_transition_in_flight = False
+
+
+def pending_transition_count() -> int:
+ return len(_PENDING_QUEUE)
+
+
+def read_stored_mode(session: Session) -> str | None:
+ row = session.get(AssetSystemState, _KEY)
+ return row.value if row else None
+
+
+def write_stored_mode(session: Session, value: str) -> None:
+ row = session.get(AssetSystemState, _KEY)
+ if row is None:
+ session.add(AssetSystemState(key=_KEY, value=value))
+ else:
+ row.value = value
+ session.flush()
+
+
+def record_transition_intent(session: Session) -> str | None:
+ stored = read_stored_mode(session)
+ runtime = "on" if _mode.hashing_enabled() else "off"
+ if stored is None:
+ write_stored_mode(session, runtime)
+ return None
+ if stored == "off" and runtime == "on":
+ return "off_to_on"
+ if stored == "on" and runtime == "off":
+ write_stored_mode(session, "off")
+ return "on_to_off"
+ return None
+
+
+def enqueue_transition_work(session: Session, transition: str | None) -> None:
+ global _off_to_on_transition_in_flight
+
+ if transition != "off_to_on":
+ return
+ _off_to_on_transition_in_flight = True
+ rows = session.scalars(
+ select(AssetContent).where(AssetContent.is_missing.is_(False))
+ )
+ for row in rows:
+ if row.path not in _PENDING_PATHS:
+ _PENDING_QUEUE.append(_PendingEntry(row.path))
+ _PENDING_PATHS.add(row.path)
+
+
+def _retry_or_retire(session: Session, entry: _PendingEntry) -> None:
+ entry.ticks += 1
+ if entry.ticks < _MAX_VERIFY_ATTEMPTS:
+ _PENDING_QUEUE.append(entry)
+ _PENDING_PATHS.add(entry.path)
+ return
+ content = session.scalars(
+ select(AssetContent).where(
+ AssetContent.path == entry.path, AssetContent.is_missing.is_(False)
+ )
+ ).first()
+ if content is not None:
+ content.hash = None
+ logging.warning(
+ "Could not verify %s in %d attempts; clearing its stored hash so the hash-mode "
+ "transition can complete",
+ entry.path,
+ _MAX_VERIFY_ATTEMPTS,
+ )
+
+
+def drain_transition_queue(session: Session) -> None:
+ global _off_to_on_transition_in_flight
+
+ pending_count = len(_PENDING_QUEUE)
+ for _ in range(pending_count):
+ entry = _PENDING_QUEUE.popleft()
+ _PENDING_PATHS.discard(entry.path)
+ path = entry.path
+ try:
+ snapshot = snapshot_hash(path)
+ except OSError:
+ _retry_or_retire(session, entry)
+ continue
+ if snapshot is None:
+ # snapshot_hash returns None for vanished and unstable files; stat distinguishes them.
+ try:
+ os.stat(path)
+ except FileNotFoundError:
+ gone = session.scalars(
+ select(AssetContent).where(
+ AssetContent.path == path, AssetContent.is_missing.is_(False)
+ )
+ ).first()
+ if gone is not None:
+ mark_content_missing(session, gone.id)
+ except OSError:
+ _retry_or_retire(session, entry)
+ else:
+ _retry_or_retire(session, entry)
+ continue
+ digest, stat = snapshot
+ stored_hash = to_stored_hash(digest)
+ content = session.scalars(
+ select(AssetContent).where(
+ AssetContent.path == path, AssetContent.is_missing.is_(False)
+ )
+ ).first()
+ if content is None:
+ continue
+ if content.hash is None:
+ content.hash = stored_hash
+ content.size_bytes = stat.st_size
+ content.mtime_ns = stat.st_mtime_ns
+ elif content.hash != stored_hash:
+ try:
+ name, tags = get_name_and_tags_from_asset_path(path)
+ except ValueError:
+ logging.warning(
+ "Skipping hash-mode split for out-of-root path: %s", path
+ )
+ continue
+ mark_content_missing(session, content.id)
+ replacement = create_content(
+ session,
+ path=path,
+ hash=stored_hash,
+ size_bytes=stat.st_size,
+ mtime_ns=stat.st_mtime_ns,
+ )
+ create_record(
+ session,
+ content_id=replacement.id,
+ name=name,
+ loader_path=compute_loader_path(path),
+ tags=tags,
+ )
+ else:
+ content.size_bytes = stat.st_size
+ content.mtime_ns = stat.st_mtime_ns
+ if _off_to_on_transition_in_flight and not _PENDING_QUEUE:
+ write_stored_mode(session, "on")
+ _off_to_on_transition_in_flight = False
diff --git a/app/assets/services/ingest.py b/app/assets/services/ingest.py
index 1ffb3d634..a8fb3b81d 100644
--- a/app/assets/services/ingest.py
+++ b/app/assets/services/ingest.py
@@ -1,468 +1,432 @@
+"""Turns incoming bytes into catalogued assets: multipart uploads moved into a
+hash-addressed destination, files registered where they already sit, and
+records created from a hash the catalog already holds. Every path persists the
+stat that hashing verified, so a row's recorded size and mtime describe the
+same observation as its hash. A live row already at the destination is
+reconciled before the write, so an upload never adopts a fresh hash onto
+records created for bytes it just replaced.
+"""
+
import contextlib
import logging
import mimetypes
import os
-from typing import Any, Sequence
+from typing import Any, NamedTuple, Sequence
+from sqlalchemy import func, select
from sqlalchemy.orm import Session
-import app.assets.services.hashing as hashing
-from app.assets.database.queries import (
- add_tags_to_reference,
- count_active_siblings,
- create_stub_asset,
- ensure_tags_exist,
- fetch_reference_and_asset,
- get_asset_by_hash,
- get_reference_by_file_path,
- get_reference_tags,
- get_or_create_reference,
- list_references_by_asset_id,
- reference_exists,
- remove_missing_tag_for_asset_id,
- set_reference_metadata,
- set_reference_system_metadata,
- set_reference_tags,
- update_asset_hash_and_mime,
- upsert_asset,
- upsert_reference,
- validate_tags_exist,
+from app.assets import mode
+from app.assets.database.models import Asset, AssetContent, AssetTag
+from app.assets.database.queries.records import (
+ create_content_reporting_insert,
+ create_record,
+ mark_content_missing,
)
-from app.assets.helpers import get_utc_now, normalize_tags
-from app.assets.services.bulk_ingest import batch_insert_seed_assets
-from app.assets.services.file_utils import get_size_and_mtime_ns
+from app.assets.helpers import normalize_tags, to_stored_hash
+from app.assets.services.file_utils import get_mtime_ns, get_size_and_mtime_ns
from app.assets.services.image_dimensions import extract_image_dimensions
+from app.assets.services.lookup import (
+ claim_qualified_content,
+ lookup_for_from_hash,
+ lookup_for_view,
+ refresh_qualified_content,
+)
+from app.assets.services.metadata_extract import extract_file_metadata
from app.assets.services.path_utils import (
compute_loader_path,
get_name_and_tags_from_asset_path,
- get_path_derived_tags_from_path,
resolve_destination_from_tags,
validate_path_within_base,
)
from app.assets.services.schemas import (
- IngestResult,
- RegisterAssetResult,
+ AssetData,
+ RegisteredAsset,
+ ReferenceData,
UploadResult,
UserMetadata,
- extract_asset_data,
- extract_reference_data,
)
+from app.assets.services.snapshot_hash import snapshot_hash
from app.database.db import create_session
-def _ingest_file_from_path(
- abs_path: str,
- asset_hash: str,
- size_bytes: int,
- mtime_ns: int,
- mime_type: str | None = None,
- info_name: str | None = None,
- owner_id: str = "",
- preview_id: str | None = None,
- user_metadata: UserMetadata = None,
- tags: Sequence[str] = (),
- tag_origin: str = "manual",
- require_existing_tags: bool = False,
-) -> IngestResult:
- locator = os.path.abspath(abs_path)
- user_metadata = user_metadata or {}
-
- asset_created = False
- asset_updated = False
- ref_created = False
- ref_updated = False
- reference_id: str | None = None
+def _normalize_hash_input(hash_str: str) -> str:
+ if not hash_str:
+ return hash_str
+ normalized = hash_str.strip().lower()
+ digest = normalized.partition(":")[2] or normalized
+ return to_stored_hash(digest)
- with create_session() as session:
- if preview_id:
- if not reference_exists(session, preview_id):
- preview_id = None
-
- asset, asset_created, asset_updated = upsert_asset(
- session,
- asset_hash=asset_hash,
- size_bytes=size_bytes,
- mime_type=mime_type,
- )
-
- ref_created, ref_updated = upsert_reference(
- session,
- asset_id=asset.id,
- file_path=locator,
- name=info_name or os.path.basename(locator),
- mtime_ns=mtime_ns,
- owner_id=owner_id,
- loader_path=compute_loader_path(locator),
- )
- # Get the reference we just created/updated
- ref = get_reference_by_file_path(session, locator)
- if ref:
- reference_id = ref.id
-
- if preview_id and ref.preview_id != preview_id:
- ref.preview_id = preview_id
-
- try:
- backend_tags = get_path_derived_tags_from_path(locator)
- except ValueError:
- backend_tags = []
- caller_tags = normalize_tags(tags)
- backend_tags = normalize_tags(backend_tags)
- all_tags = normalize_tags([*caller_tags, *backend_tags])
- if all_tags:
- if require_existing_tags:
- validate_tags_exist(session, all_tags)
- if backend_tags:
- add_tags_to_reference(
- session,
- reference_id=reference_id,
- tags=backend_tags,
- origin="automatic",
- create_if_missing=not require_existing_tags,
- )
- if caller_tags:
- add_tags_to_reference(
- session,
- reference_id=reference_id,
- tags=caller_tags,
- origin=tag_origin,
- create_if_missing=not require_existing_tags,
- )
-
- _update_metadata_with_filename(
- session,
- reference_id=reference_id,
- file_path=ref.file_path,
- current_metadata=ref.user_metadata,
- user_metadata=user_metadata,
- )
-
- _maybe_store_image_dimensions(
- session,
- reference_id=reference_id,
- file_path=locator,
- mime_type=mime_type,
- current_system_metadata=ref.system_metadata,
- )
-
- try:
- remove_missing_tag_for_asset_id(session, asset_id=asset.id)
- except Exception:
- logging.exception("Failed to clear 'missing' tag for asset %s", asset.id)
-
- session.commit()
+def _extract_system_metadata_sync(
+ locator: str,
+ mime_type: str | None,
+ stat_result: os.stat_result | None = None,
+) -> dict[str, Any]:
+ """Extract ``system_metadata`` at registration time (S29/D8).
- return IngestResult(
- asset_created=asset_created,
- asset_updated=asset_updated,
- ref_created=ref_created,
- ref_updated=ref_updated,
- reference_id=reference_id,
+ Mirrors the ``scanner.enrich`` pass: tier-1/tier-2 file metadata plus image
+ dimensions for image MIME types, so records carry metadata at creation
+ instead of waiting for the background enrich pass to fill it.
+ """
+ metadata = extract_file_metadata(
+ locator,
+ stat_result=stat_result,
+ relative_filename=compute_loader_path(locator),
)
-
-
-def register_output_files(
- file_paths: Sequence[str],
- user_metadata: UserMetadata = None,
- job_id: str | None = None,
-) -> int:
- """Register a batch of output file paths as assets.
-
- Returns the number of files successfully registered.
+ system_metadata = metadata.to_user_metadata()
+ if mime_type and mime_type.startswith("image/"):
+ dims = extract_image_dimensions(locator, mime_type=mime_type)
+ if dims:
+ system_metadata.update(dims)
+ return system_metadata
+
+
+def _discard_unreferenced_content(session: Session, content_id: str) -> None:
+ """Remove a content row left orphaned by a failed registration.
+
+ ``create_content`` inserts inside a SAVEPOINT (``begin_nested``); under
+ pysqlite that insert survives the enclosing ``rollback`` because pysqlite
+ has no real nested transaction. When the follow-on ``create_record`` fails
+ we would otherwise leak an unreferenced content row, so delete it explicitly
+ once we have confirmed no record points at it. Best-effort: cleanup errors
+ are logged and swallowed so the original failure is what surfaces.
"""
- registered = 0
- for abs_path in file_paths:
- if not os.path.isfile(abs_path):
- continue
- try:
- if ingest_existing_file(
- abs_path, user_metadata=user_metadata, job_id=job_id
- ):
- registered += 1
- except Exception:
- logging.exception("Failed to register output: %s", abs_path)
- return registered
-
+ try:
+ ref_count = session.scalar(
+ select(func.count())
+ .select_from(Asset)
+ .where(Asset.content_id == content_id)
+ )
+ if ref_count:
+ return
+ content = session.get(AssetContent, content_id)
+ if content is not None:
+ session.delete(content)
+ session.commit()
+ except Exception:
+ logging.exception("Failed to discard orphan content %s", content_id)
-def ingest_existing_file(
- abs_path: str,
- user_metadata: UserMetadata = None,
- extra_tags: Sequence[str] = (),
- owner_id: str = "",
- job_id: str | None = None,
-) -> bool:
- """Register an existing on-disk file as an asset stub.
- If a reference already exists for this path, updates mtime_ns, job_id,
- size_bytes, and resets enrichment so the enricher will re-hash it.
+def _sanitize_filename(name: str | None, fallback: str) -> str:
+ n = os.path.basename((name or "").strip() or fallback)
+ return n if n else fallback
- For brand-new paths, inserts a stub record (hash=NULL) for immediate
- UX visibility.
- Returns True if a row was inserted or updated, False otherwise.
- """
- locator = os.path.abspath(abs_path)
- size_bytes, mtime_ns = get_size_and_mtime_ns(abs_path)
- mime_type = mimetypes.guess_type(abs_path, strict=False)[0]
- name, path_tags = get_name_and_tags_from_asset_path(abs_path)
- tags = list(dict.fromkeys(path_tags + list(extra_tags)))
+class HashMismatchError(Exception):
+ pass
- with create_session() as session:
- existing_ref = get_reference_by_file_path(session, locator)
- if existing_ref is not None:
- now = get_utc_now()
- existing_ref.mtime_ns = mtime_ns
- existing_ref.job_id = job_id
- existing_ref.is_missing = False
- existing_ref.deleted_at = None
- existing_ref.updated_at = now
- existing_ref.enrichment_level = 0
-
- asset = existing_ref.asset
- if asset:
- # If other refs share this asset, detach to a new stub
- # instead of mutating the shared row.
- siblings = count_active_siblings(session, asset.id, existing_ref.id)
- if siblings > 0:
- new_asset = create_stub_asset(
- session,
- size_bytes=size_bytes,
- mime_type=mime_type or asset.mime_type,
- )
- existing_ref.asset_id = new_asset.id
- else:
- asset.hash = None
- asset.size_bytes = size_bytes
- if mime_type:
- asset.mime_type = mime_type
- session.commit()
- return True
-
- spec = {
- "abs_path": abs_path,
- "size_bytes": size_bytes,
- "mtime_ns": mtime_ns,
- "info_name": name,
- "tags": tags,
- "fname": compute_loader_path(abs_path),
- "metadata": None,
- "hash": None,
- "mime_type": mime_type,
- "job_id": job_id,
- }
- if tags:
- ensure_tags_exist(session, tags)
- result = batch_insert_seed_assets(session, [spec], owner_id=owner_id)
- session.commit()
- return result.won_paths > 0
+class UploadUnstableError(Exception):
+ pass
-def _register_existing_asset(
- asset_hash: str,
- name: str,
- user_metadata: UserMetadata = None,
- tags: list[str] | None = None,
- tag_origin: str = "manual",
- owner_id: str = "",
- mime_type: str | None = None,
- preview_id: str | None = None,
-) -> RegisterAssetResult:
- user_metadata = user_metadata or {}
- with create_session() as session:
- asset = get_asset_by_hash(session, asset_hash=asset_hash)
- if not asset:
- raise ValueError(f"No asset with hash {asset_hash}")
+class DependencyMissingError(Exception):
+ def __init__(self, message: str):
+ self.message = message
+ super().__init__(message)
- if mime_type and not asset.mime_type:
- update_asset_hash_and_mime(session, asset_id=asset.id, mime_type=mime_type)
- if preview_id:
- if not reference_exists(session, preview_id):
- preview_id = None
+_UPLOAD_HASH_ATTEMPTS = 3
- ref, ref_created = get_or_create_reference(
- session,
- asset_id=asset.id,
- owner_id=owner_id,
- name=name,
- preview_id=preview_id,
- )
- if not ref_created:
- if preview_id and ref.preview_id != preview_id:
- ref.preview_id = preview_id
+def _remove_temp_path(temp_path: str | None) -> None:
+ if not temp_path or not os.path.exists(temp_path):
+ return
+ with contextlib.suppress(OSError):
+ os.remove(temp_path)
+ parent = os.path.dirname(temp_path)
+ with contextlib.suppress(OSError):
+ if parent and os.path.isdir(parent):
+ os.rmdir(parent)
- tag_names = get_reference_tags(session, reference_id=ref.id)
- result = RegisterAssetResult(
- ref=extract_reference_data(ref),
- asset=extract_asset_data(asset),
- tags=tag_names,
- created=False,
- )
- session.commit()
- return result
- new_meta = dict(user_metadata)
- computed_filename = compute_loader_path(ref.file_path) if ref.file_path else None
- if computed_filename:
- new_meta["filename"] = computed_filename
+def _snapshot_hash_with_retry(path: str) -> tuple[str, os.stat_result]:
+ for _ in range(_UPLOAD_HASH_ATTEMPTS):
+ snapshot = snapshot_hash(path)
+ if snapshot is not None:
+ return snapshot
+ raise UploadUnstableError("upload file changed during hashing")
- if new_meta:
- set_reference_metadata(
- session,
- reference_id=ref.id,
- user_metadata=new_meta,
- )
- _backfill_image_dimensions_from_siblings(
- session,
- asset_id=asset.id,
- new_reference_id=ref.id,
- current_system_metadata=ref.system_metadata,
- )
+def _hash_mode_dest_path(
+ tags: list[str],
+ digest: str,
+ client_filename: str | None,
+ name: str | None,
+) -> str:
+ base_dir, subdirs = resolve_destination_from_tags(tags)
+ dest_dir = os.path.join(base_dir, *subdirs) if subdirs else base_dir
+ os.makedirs(dest_dir, exist_ok=True)
+ src_for_ext = (client_filename or name or "").strip()
+ _ext = os.path.splitext(os.path.basename(src_for_ext))[1] if src_for_ext else ""
+ ext = _ext if 0 < len(_ext) <= 16 else ""
+ hashed_basename = f"{digest}{ext}"
+ dest_abs = os.path.abspath(os.path.join(dest_dir, hashed_basename))
+ validate_path_within_base(dest_abs, base_dir)
+ return dest_abs
- if tags is not None:
- set_reference_tags(
- session,
- reference_id=ref.id,
- tags=tags,
- origin=tag_origin,
- )
- tag_names = get_reference_tags(session, reference_id=ref.id)
- session.refresh(ref)
- result = RegisterAssetResult(
- ref=extract_reference_data(ref),
- asset=extract_asset_data(asset),
- tags=tag_names,
- created=True,
- )
- session.commit()
+def _guess_upload_mime_type(
+ mime_type: str | None,
+ client_filename: str | None,
+ name: str | None,
+ fallback_basename: str,
+) -> str:
+ src_for_ext = (client_filename or name or "").strip()
+ if mime_type:
+ return mime_type
+ guessed = mimetypes.guess_type(os.path.basename(src_for_ext), strict=False)[0]
+ if guessed:
+ return guessed
+ guessed = mimetypes.guess_type(fallback_basename, strict=False)[0]
+ return guessed or "application/octet-stream"
- return result
+def _move_temp_to_dest(temp_path: str, dest_abs: str) -> None:
+ os.makedirs(os.path.dirname(dest_abs), exist_ok=True)
+ try:
+ os.replace(temp_path, dest_abs)
+ except Exception as e:
+ raise RuntimeError(f"failed to move uploaded file into place: {e}") from e
-def _update_metadata_with_filename(
+def _create_upload_record(
session: Session,
- reference_id: str,
- file_path: str | None,
- current_metadata: dict | None,
- user_metadata: dict[str, Any],
-) -> None:
- computed_filename = compute_loader_path(file_path) if file_path else None
+ content_id: str,
+ name: str,
+ abs_path: str,
+ tags: Sequence[str],
+ mime_type: str | None,
+ user_metadata: UserMetadata,
+ preview_id: str | None,
+) -> Asset:
+ if preview_id is not None and session.get(Asset, preview_id) is None:
+ raise ValueError(f"preview_id {preview_id!r} does not reference an existing asset")
+ record = create_record(
+ session,
+ content_id,
+ name,
+ mime_type=mime_type,
+ loader_path=compute_loader_path(abs_path),
+ tags=list(tags),
+ system_metadata=_extract_system_metadata_sync(abs_path, mime_type),
+ )
+ if user_metadata:
+ record.user_metadata = dict(user_metadata)
+ if preview_id:
+ record.preview_id = preview_id
+ session.flush()
+ return record
- current_meta = current_metadata or {}
- new_meta = dict(current_meta)
- for k, v in user_metadata.items():
- new_meta[k] = v
- if computed_filename:
- new_meta["filename"] = computed_filename
- if new_meta != current_meta:
- set_reference_metadata(
- session,
- reference_id=reference_id,
- user_metadata=new_meta,
+def _record_to_upload_result(
+ session: Session, record: Asset, *, created_new: bool
+) -> UploadResult:
+ content = session.get(AssetContent, record.content_id)
+ tag_names = list(
+ session.scalars(
+ select(AssetTag.tag_name)
+ .where(AssetTag.asset_id == record.id)
+ .order_by(AssetTag.tag_name)
)
+ )
+ api_hash = content.hash if content else None
+ ref = ReferenceData(
+ id=record.id,
+ name=record.name,
+ file_path=content.path if content else None,
+ loader_path=record.loader_path,
+ user_metadata=record.user_metadata,
+ preview_id=record.preview_id,
+ system_metadata=record.system_metadata,
+ job_id=record.job_id,
+ created_at=record.created_at,
+ updated_at=record.updated_at,
+ last_access_time=record.last_access_time,
+ )
+ asset = AssetData(
+ hash=api_hash,
+ size_bytes=content.size_bytes if content else None,
+ mime_type=record.mime_type,
+ )
+ return UploadResult(
+ ref=ref,
+ content_id=record.content_id,
+ asset=asset,
+ tags=tag_names,
+ created_new=created_new,
+ )
+
+class _ContentFacts(NamedTuple):
-_IMAGE_DIMENSION_KEYS = ("kind", "width", "height")
+ stored_hash: str
+ size_bytes: int
+ mtime_ns: int
-def _maybe_store_image_dimensions(
+def _reconcile_live_content_at_path(
session: Session,
- reference_id: str,
- file_path: str,
- mime_type: str | None,
- current_system_metadata: dict | None,
+ locator: str,
+ facts: _ContentFacts,
+ *,
+ content_written: bool,
) -> None:
- """Populate ``kind``/``width``/``height`` on system_metadata for image refs.
-
- Non-image MIME types are a no-op. Pre-existing keys (e.g. enricher-written
- safetensors metadata, download provenance) are preserved by merge.
+ """Make the live content row at ``locator`` honest about the bytes there.
+
+ ``create_content`` resolves a live-path uniqueness conflict
+ (``uq_asset_contents_path_live``) by handing back the row already at that
+ path, so a caller that registers a path without reconciling first gets the
+ OLD row's hash and size.
+
+ ``content_written`` is the caller's own knowledge of whether it just wrote
+ bytes at ``locator``, and it is what makes an unhashed row readable at all:
+ equal ``size_bytes`` is NOT proof of equal bytes, so a caller that replaced
+ the file cannot adopt a fresh hash onto records that were created for the
+ bytes it destroyed. Cases, in order:
+
+ * the hash already matches - proof the bytes are identical, so the OBSERVED
+ stat is the trustworthy one. Refresh it: a touch, a copy, or a same-bytes
+ rewrite moves ``mtime_ns`` without changing a byte, and a row left stale
+ fails ``lookup._stat_consistent``, which makes the content unservable by
+ hash and silently routes the caller into creating a duplicate record.
+ * the hash is ``None`` and the caller wrote nothing - "not yet hashed" is
+ NOT evidence of different bytes. Scanner-seeded rows and executed outputs
+ (D14a) both defer hashing to the enrich pass, so retiring here would mark
+ every record on the row missing for a file that never changed. Adopt the
+ hash we just computed the way ``scanner.enrich_asset`` would, unless the
+ row's recorded ``size_bytes`` positively contradicts the file. A
+ differing ``mtime_ns`` is deliberately not a contradiction: renaming a
+ file into place, copying it, or touching it all move the mtime while the
+ bytes stay identical.
+ * anything else - a known but different hash, or unhashed bytes the caller
+ has just overwritten. Either way the row can no longer speak for what is
+ at ``locator``: retire it so the fresh ``create_content`` inserts a new
+ live row and the old records keep describing the old bytes.
"""
- if not mime_type or not mime_type.startswith("image/"):
+ existing = session.scalars(
+ select(AssetContent).where(
+ AssetContent.path == locator,
+ AssetContent.is_missing.is_(False),
+ )
+ ).first()
+ if existing is None:
return
-
- dims = extract_image_dimensions(file_path, mime_type=mime_type)
- if not dims:
+ if existing.hash == facts.stored_hash:
+ existing.size_bytes = facts.size_bytes
+ existing.mtime_ns = facts.mtime_ns
+ session.flush()
return
+ if (
+ existing.hash is None
+ and not content_written
+ and existing.size_bytes == facts.size_bytes
+ ):
+ existing.hash = facts.stored_hash
+ existing.mtime_ns = facts.mtime_ns
+ session.flush()
+ return
+ mark_content_missing(session, existing.id)
- current = current_system_metadata or {}
- merged = dict(current)
- merged.update(dims)
- if merged != current:
- set_reference_system_metadata(
- session,
- reference_id=reference_id,
- system_metadata=merged,
- )
+def _upload_destination_or_none(
+ tags: list[str] | None,
+ digest: str,
+ client_filename: str | None,
+ name: str | None,
+) -> str | None:
+ if not tags:
+ return None
+ try:
+ return _hash_mode_dest_path(tags, digest, client_filename, name)
+ except ValueError:
+ return None
-def _backfill_image_dimensions_from_siblings(
- session: Session,
- asset_id: str,
- new_reference_id: str,
- current_system_metadata: dict | None,
-) -> None:
- """Copy image dimension keys from any sibling reference of the same asset.
- The from-hash path doesn't read the file bytes, so dimensions can't be
- extracted there directly. When another reference of the same asset already
- carries image dimensions, copy them onto the new reference so consumers
- see consistent metadata regardless of how the asset was registered.
+def _settle_destination_before_write(session: Session, dest_abs: str) -> None:
+ """Resolve the live row at an upload destination while its bytes still exist.
+
+ ``upload_from_temp_path`` is about to replace whatever sits at ``dest_abs``,
+ and that write destroys the only evidence of what the incumbent row was
+ created for. So reconcile against the INCUMBENT file's own hash first: an
+ unhashed row learns the hash of the bytes it actually describes, and a row
+ whose hash no longer matches the file is retired. Both outcomes leave the
+ post-move reconciliation with a known hash to compare, so it never has to
+ read equal sizes as equal bytes.
- Best-effort: missing siblings, non-image siblings, or absent dimension
- keys leave the target reference unchanged.
+ Running before ``lookup_for_view`` is what keeps a merely stale row from
+ being skipped as stat-inconsistent and duplicated.
"""
- current = current_system_metadata or {}
- if current.get("kind") == "image" and "width" in current and "height" in current:
+ if not os.path.isfile(dest_abs):
return
-
- for sibling in list_references_by_asset_id(session, asset_id):
- if sibling.id == new_reference_id:
- continue
- meta = sibling.system_metadata or {}
- if meta.get("kind") != "image":
- continue
- width = meta.get("width")
- height = meta.get("height")
- if (
- type(width) is not int
- or type(height) is not int
- or width <= 0
- or height <= 0
- ):
- continue
- merged = dict(current)
- merged["kind"] = "image"
- merged["width"] = width
- merged["height"] = height
- if merged != current:
- set_reference_system_metadata(
- session,
- reference_id=new_reference_id,
- system_metadata=merged,
- )
+ existing = session.scalars(
+ select(AssetContent).where(
+ AssetContent.path == dest_abs,
+ AssetContent.is_missing.is_(False),
+ )
+ ).first()
+ if existing is None:
return
+ size_bytes, mtime_ns = get_size_and_mtime_ns(dest_abs)
+ if (
+ existing.hash is not None
+ and existing.size_bytes == size_bytes
+ and existing.mtime_ns == mtime_ns
+ ):
+ return
+ try:
+ incumbent_digest, verified_stat = _snapshot_hash_with_retry(dest_abs)
+ except (UploadUnstableError, OSError):
+ mark_content_missing(session, existing.id)
+ return
+ _reconcile_live_content_at_path(
+ session,
+ dest_abs,
+ _ContentFacts(
+ to_stored_hash(incumbent_digest),
+ verified_stat.st_size,
+ verified_stat.st_mtime_ns,
+ ),
+ content_written=False,
+ )
-def _sanitize_filename(name: str | None, fallback: str) -> str:
- n = os.path.basename((name or "").strip() or fallback)
- return n if n else fallback
-
+class _UploadRecordSpec(NamedTuple):
-class HashMismatchError(Exception):
- pass
+ name: str
+ tags: list[str]
+ mime_type: str | None
+ user_metadata: UserMetadata
+ preview_id: str | None
-class DependencyMissingError(Exception):
- def __init__(self, message: str):
- self.message = message
- super().__init__(message)
+def _reuse_qualified_content(
+ session: Session, stored_hash: str, spec: _UploadRecordSpec
+) -> UploadResult | None:
+ content = lookup_for_view(session, stored_hash)
+ if content is None:
+ return None
+ content_id = content.id
+ if not claim_qualified_content(session, content_id, stored_hash):
+ session.rollback()
+ return None
+ content = refresh_qualified_content(session, content_id)
+ if content is None:
+ session.rollback()
+ return None
+ record = _create_upload_record(
+ session,
+ content_id,
+ spec.name,
+ content.path,
+ spec.tags,
+ spec.mime_type,
+ spec.user_metadata,
+ spec.preview_id,
+ )
+ session.commit()
+ return _record_to_upload_result(session, record, created_new=True)
def upload_from_temp_path(
@@ -471,184 +435,163 @@ def upload_from_temp_path(
tags: list[str] | None = None,
user_metadata: dict | None = None,
client_filename: str | None = None,
- owner_id: str = "",
expected_hash: str | None = None,
mime_type: str | None = None,
preview_id: str | None = None,
) -> UploadResult:
- try:
- digest, _ = hashing.compute_blake3_hash(temp_path)
- except ImportError as e:
- raise DependencyMissingError(str(e))
- except Exception as e:
- raise RuntimeError(f"failed to hash uploaded file: {e}")
- asset_hash = "blake3:" + digest
+ display_name = _sanitize_filename(name or client_filename, fallback="upload")
+ user_metadata = user_metadata or {}
- if expected_hash and asset_hash != expected_hash.strip().lower():
+ try:
+ digest, verified_stat = _snapshot_hash_with_retry(temp_path)
+ except UploadUnstableError:
+ _remove_temp_path(temp_path)
+ raise
+ stored_hash = to_stored_hash(digest)
+ if expected_hash and stored_hash != _normalize_hash_input(expected_hash):
+ _remove_temp_path(temp_path)
raise HashMismatchError("Uploaded file hash does not match provided hash.")
+ settle_target = _upload_destination_or_none(tags, digest, client_filename, name)
with create_session() as session:
- existing = get_asset_by_hash(session, asset_hash=asset_hash)
-
- if existing is not None:
- # Once content is already known, duplicate byte uploads are treated as
- # reference-only creation. Request tags are labels only here: do not
- # require upload destination tags, do not move bytes, and do not
- # synthesize path-derived classification or uploaded provenance.
- with contextlib.suppress(Exception):
- if temp_path and os.path.exists(temp_path):
- os.remove(temp_path)
-
- display_name = _sanitize_filename(name or client_filename, fallback=digest)
- result = _register_existing_asset(
- asset_hash=asset_hash,
- name=display_name,
- user_metadata=user_metadata or {},
- tags=tags or [],
- tag_origin="manual",
- owner_id=owner_id,
- mime_type=mime_type,
- preview_id=preview_id,
- )
- return UploadResult(
- ref=result.ref,
- asset=result.asset,
- tags=result.tags,
- created_new=False,
+ if settle_target is not None:
+ _settle_destination_before_write(session, settle_target)
+ session.commit()
+ reused = _reuse_qualified_content(
+ session,
+ stored_hash,
+ _UploadRecordSpec(
+ display_name,
+ normalize_tags([*(tags or []), "uploaded"]),
+ mime_type,
+ user_metadata,
+ preview_id,
+ ),
)
+ if reused is not None:
+ _remove_temp_path(temp_path)
+ return reused
if not tags:
+ _remove_temp_path(temp_path)
raise ValueError("tags are required for new asset uploads")
- base_dir, subdirs = resolve_destination_from_tags(tags)
- dest_dir = os.path.join(base_dir, *subdirs) if subdirs else base_dir
- os.makedirs(dest_dir, exist_ok=True)
-
- src_for_ext = (client_filename or name or "").strip()
- _ext = os.path.splitext(os.path.basename(src_for_ext))[1] if src_for_ext else ""
- ext = _ext if 0 < len(_ext) <= 16 else ""
- hashed_basename = f"{digest}{ext}"
- dest_abs = os.path.abspath(os.path.join(dest_dir, hashed_basename))
- validate_path_within_base(dest_abs, base_dir)
-
- content_type = mime_type or (
- mimetypes.guess_type(os.path.basename(src_for_ext), strict=False)[0]
- or mimetypes.guess_type(hashed_basename, strict=False)[0]
- or "application/octet-stream"
- )
- try:
- os.replace(temp_path, dest_abs)
- except Exception as e:
- raise RuntimeError(f"failed to move uploaded file into place: {e}")
-
- try:
- size_bytes, mtime_ns = get_size_and_mtime_ns(dest_abs)
- except OSError as e:
- raise RuntimeError(f"failed to stat destination file: {e}")
-
- ingest_result = _ingest_file_from_path(
- asset_hash=asset_hash,
- abs_path=dest_abs,
- size_bytes=size_bytes,
- mtime_ns=mtime_ns,
- mime_type=content_type,
- info_name=_sanitize_filename(name or client_filename, fallback=digest),
- owner_id=owner_id,
- preview_id=preview_id,
- user_metadata=user_metadata or {},
- tags=[*(tags or []), "uploaded"],
- tag_origin="manual",
- require_existing_tags=False,
+ dest_abs = _hash_mode_dest_path(tags, digest, client_filename, name)
+ content_type = _guess_upload_mime_type(
+ mime_type, client_filename, name, os.path.basename(dest_abs)
)
- reference_id = ingest_result.reference_id
- if not reference_id:
- raise RuntimeError("failed to create asset reference")
-
+ _move_temp_to_dest(temp_path, dest_abs)
+ size_bytes, mtime_ns = verified_stat.st_size, verified_stat.st_mtime_ns
with create_session() as session:
- pair = fetch_reference_and_asset(
- session, reference_id=reference_id, owner_id=owner_id
+ _reconcile_live_content_at_path(
+ session,
+ dest_abs,
+ _ContentFacts(stored_hash, size_bytes, mtime_ns),
+ content_written=True,
)
- if not pair:
- raise RuntimeError("inconsistent DB state after ingest")
- ref, asset = pair
- tag_names = get_reference_tags(session, reference_id=ref.id)
-
- return UploadResult(
- ref=extract_reference_data(ref),
- asset=extract_asset_data(asset),
- tags=tag_names,
- created_new=ingest_result.asset_created,
- )
+ content, inserted = create_content_reporting_insert(
+ session, dest_abs, stored_hash, size_bytes, mtime_ns
+ )
+ created_content_id = content.id if inserted else None
+ try:
+ record = _create_upload_record(
+ session,
+ content.id,
+ display_name,
+ dest_abs,
+ normalize_tags([*(tags or []), "uploaded"]),
+ content_type,
+ user_metadata,
+ preview_id,
+ )
+ session.commit()
+ except Exception:
+ session.rollback()
+ if created_content_id is not None:
+ _discard_unreferenced_content(session, created_content_id)
+ raise
+ return _record_to_upload_result(session, record, created_new=True)
def register_file_in_place(
abs_path: str,
name: str,
tags: list[str],
- owner_id: str = "",
mime_type: str | None = None,
+ *,
+ content_written: bool = True,
) -> UploadResult:
"""Register an already-saved file in the asset database without moving it.
- This helper is used by upload paths that have already written bytes before
- registering the file, so it records the same ``uploaded`` tag as the
- multipart byte-upload path.
-
- Tags are derived from trusted filesystem classification and merged with any
- caller-provided tags, matching the behavior of the scanner.
- If the path is not under a known root, only the caller-provided tags are used.
+ Used by ``/upload/image`` after the handler writes (or reuses) bytes on disk.
+ ``compare_image_hash`` same-name dedup is legacy physical behavior and stays
+ outside hash-mode policy (parallel to path-form ``/view`` semantics).
+
+ Dedup is scoped to ``abs_path``, deliberately weaker than the multipart
+ endpoint's global content dedup. ``_reconcile_live_content_at_path`` already
+ settles the same-path row before this returns, so a global hash match adds
+ nothing there - the only thing it can still reach is a DIFFERENT file, and
+ honouring that would leave the just-written ``abs_path`` untracked and hand
+ back an asset describing someone else's path. Equal hashes across paths stay
+ discoverable by hash, they are simply not merged.
+ ``_reconcile_live_content_at_path`` is the whole dedup story here - it leaves
+ at most one live row at ``abs_path``, so re-registering unchanged bytes reuses
+ that content row while still writing a new record, which is what a repeat save
+ through ``/upload/image`` is.
+
+ ``content_written`` reports whether the caller just wrote bytes at
+ ``abs_path``. The handler always knows - ``/upload/image`` skips the write
+ exactly when ``compare_image_hash`` proves the bytes are already there - and
+ it is the only sound way to reconcile an unhashed row at that path, because
+ the bytes it was created for are gone by the time we are called. It defaults
+ to the conservative answer so a caller that does not know retires rather
+ than adopts.
"""
+ locator = os.path.abspath(abs_path)
+ display_name = _sanitize_filename(name, fallback=os.path.basename(locator))
try:
- _, path_tags = get_name_and_tags_from_asset_path(abs_path)
+ _, path_tags = get_name_and_tags_from_asset_path(locator)
except ValueError:
path_tags = []
merged_tags = normalize_tags([*path_tags, *tags, "uploaded"])
-
- try:
- digest, _ = hashing.compute_blake3_hash(abs_path)
- except ImportError as e:
- raise DependencyMissingError(str(e))
- except Exception as e:
- raise RuntimeError(f"failed to hash file: {e}")
- asset_hash = "blake3:" + digest
-
- size_bytes, mtime_ns = get_size_and_mtime_ns(abs_path)
- content_type = mime_type or (
- mimetypes.guess_type(abs_path, strict=False)[0]
- or "application/octet-stream"
+ content_type = _guess_upload_mime_type(
+ mime_type, name, name, os.path.basename(locator)
)
-
- ingest_result = _ingest_file_from_path(
- abs_path=abs_path,
- asset_hash=asset_hash,
- size_bytes=size_bytes,
- mtime_ns=mtime_ns,
- mime_type=content_type,
- info_name=_sanitize_filename(name, fallback=digest),
- owner_id=owner_id,
- tags=merged_tags,
- tag_origin="upload",
- require_existing_tags=False,
- )
- reference_id = ingest_result.reference_id
- if not reference_id:
- raise RuntimeError("failed to create asset reference")
-
+ digest, verified_stat = _snapshot_hash_with_retry(locator)
+ size_bytes, mtime_ns = verified_stat.st_size, verified_stat.st_mtime_ns
+ stored_hash = to_stored_hash(digest)
with create_session() as session:
- pair = fetch_reference_and_asset(
- session, reference_id=reference_id, owner_id=owner_id
+ _reconcile_live_content_at_path(
+ session,
+ locator,
+ _ContentFacts(stored_hash, size_bytes, mtime_ns),
+ content_written=content_written,
)
- if not pair:
- raise RuntimeError("inconsistent DB state after ingest")
- ref, asset = pair
- tag_names = get_reference_tags(session, reference_id=ref.id)
+ session.commit()
- return UploadResult(
- ref=extract_reference_data(ref),
- asset=extract_asset_data(asset),
- tags=tag_names,
- created_new=ingest_result.asset_created,
- )
+ with create_session() as session:
+ content, inserted = create_content_reporting_insert(
+ session, locator, stored_hash, size_bytes, mtime_ns
+ )
+ created_content_id = content.id if inserted else None
+ try:
+ record = _create_upload_record(
+ session,
+ content.id,
+ display_name,
+ locator,
+ merged_tags,
+ content_type,
+ None,
+ None,
+ )
+ session.commit()
+ except Exception:
+ session.rollback()
+ if created_content_id is not None:
+ _discard_unreferenced_content(session, created_content_id)
+ raise
+ return _record_to_upload_result(session, record, created_new=True)
def create_from_hash(
@@ -656,32 +599,169 @@ def create_from_hash(
name: str,
tags: list[str] | None = None,
user_metadata: dict | None = None,
- owner_id: str = "",
mime_type: str | None = None,
preview_id: str | None = None,
) -> UploadResult | None:
- canonical = hash_str.strip().lower()
+ if not mode.hashing_enabled():
+ return None
+
+ stored_hash = _normalize_hash_input(hash_str)
+ bare_digest = stored_hash.partition(":")[2] or stored_hash
+ display_name = _sanitize_filename(
+ name, fallback=bare_digest
+ )
+
+ with create_session() as session:
+ content = lookup_for_from_hash(session, stored_hash)
+ if content is None:
+ logging.warning("create_from_hash: no asset found for hash %s", hash_str)
+ return None
+ content_id = content.id
+ if not claim_qualified_content(session, content_id, stored_hash):
+ session.rollback()
+ return None
+ content = refresh_qualified_content(session, content_id)
+ if content is None:
+ session.rollback()
+ return None
+ record = _create_upload_record(
+ session,
+ content_id,
+ display_name,
+ content.path,
+ tags or [],
+ mime_type,
+ user_metadata,
+ preview_id,
+ )
+ session.commit()
+ return _record_to_upload_result(session, record, created_new=True)
+
+
+def register_cached_output(
+ abs_path: str, job_id: str | None = None
+) -> RegisteredAsset | None:
+ locator = os.path.abspath(abs_path)
+ try:
+ with create_session() as session:
+ existing = session.scalars(
+ select(AssetContent).where(
+ AssetContent.path == locator, AssetContent.is_missing.is_(False)
+ )
+ ).first()
+ if existing is None:
+ logging.info(
+ "Cached output registration is a non-event; no live content "
+ "for %s",
+ locator,
+ )
+ return None
+
+ name, path_tags = get_name_and_tags_from_asset_path(locator)
+ mime_type = mimetypes.guess_type(locator, strict=False)[0]
+
+ sibling = session.scalars(
+ select(Asset)
+ .where(Asset.content_id == existing.id)
+ .order_by(Asset.created_at.asc(), Asset.id.asc())
+ .limit(1)
+ ).first()
+ if sibling is not None:
+ system_metadata = (
+ dict(sibling.system_metadata)
+ if sibling.system_metadata is not None
+ else None
+ )
+ else:
+ system_metadata = _extract_system_metadata_sync(locator, mime_type)
+
+ try:
+ record = create_record(
+ session,
+ existing.id,
+ name,
+ mime_type=mime_type,
+ job_id=job_id,
+ loader_path=compute_loader_path(locator),
+ tags=path_tags,
+ system_metadata=system_metadata,
+ )
+ session.commit()
+ except Exception:
+ session.rollback()
+ raise
+ record_id = record.id
+ record_content_id = record.content_id
+ record_job_id = record.job_id
+ record_name = record.name
+ except Exception:
+ logging.exception("Failed to register cached output: %s", locator)
+ return None
+
+ return RegisteredAsset(
+ id=record_id,
+ content_id=record_content_id,
+ job_id=record_job_id,
+ name=record_name,
+ )
+
+def register_executed_output(
+ abs_path: str, job_id: str | None = None
+) -> RegisteredAsset | None:
+ locator = os.path.abspath(abs_path)
try:
- result = _register_existing_asset(
- asset_hash=canonical,
- name=_sanitize_filename(
- name, fallback=canonical.split(":", 1)[1] if ":" in canonical else canonical
- ),
- user_metadata=user_metadata or {},
- tags=tags or [],
- tag_origin="manual",
- owner_id=owner_id,
- mime_type=mime_type,
- preview_id=preview_id,
+ stat_result = os.stat(locator, follow_symlinks=True)
+ size_bytes = stat_result.st_size
+ mtime_ns = get_mtime_ns(stat_result)
+ mime_type = mimetypes.guess_type(locator, strict=False)[0]
+ name, path_tags = get_name_and_tags_from_asset_path(locator)
+ system_metadata = _extract_system_metadata_sync(
+ locator, mime_type, stat_result
)
- except ValueError:
- logging.warning("create_from_hash: no asset found for hash %s", canonical)
+ with create_session() as session:
+ created_content_id: str | None = None
+ try:
+ existing = session.scalars(
+ select(AssetContent).where(
+ AssetContent.path == locator,
+ AssetContent.is_missing.is_(False),
+ )
+ ).first()
+ if existing is not None:
+ mark_content_missing(session, existing.id)
+ content, inserted = create_content_reporting_insert(
+ session, locator, None, size_bytes, mtime_ns
+ )
+ if inserted:
+ created_content_id = content.id
+ record = create_record(
+ session,
+ content.id,
+ name,
+ mime_type=mime_type,
+ job_id=job_id,
+ loader_path=compute_loader_path(locator),
+ tags=path_tags,
+ system_metadata=system_metadata,
+ )
+ session.commit()
+ except Exception:
+ session.rollback()
+ if created_content_id is not None:
+ _discard_unreferenced_content(session, created_content_id)
+ raise
+ record_id = record.id
+ record_content_id = record.content_id
+ record_job_id = record.job_id
+ record_name = record.name
+ except Exception:
+ logging.exception("Failed to register executed output: %s", locator)
return None
- return UploadResult(
- ref=result.ref,
- asset=result.asset,
- tags=result.tags,
- created_new=False,
+ return RegisteredAsset(
+ id=record_id,
+ content_id=record_content_id,
+ job_id=record_job_id,
+ name=record_name,
)
diff --git a/app/assets/services/lookup.py b/app/assets/services/lookup.py
new file mode 100644
index 000000000..6017d7e54
--- /dev/null
+++ b/app/assets/services/lookup.py
@@ -0,0 +1,96 @@
+"""Resolves a hash to a content row that can actually be served, and claims that
+row so a concurrent writer cannot retire it mid-use. A row qualifies only if
+its file exists, sits outside the temp directory, and its recorded size and
+mtime still match the file — an unrecorded mtime excuses the mtime comparison
+but never the size comparison.
+"""
+
+from __future__ import annotations
+
+import os
+from collections.abc import Iterator
+from pathlib import Path
+
+import folder_paths
+from sqlalchemy import select, update
+from sqlalchemy.orm import Session
+
+from app.assets import mode
+from app.assets.database.models import AssetContent
+
+
+def is_temp_path(path: str) -> bool:
+ try:
+ temp_root = Path(os.path.abspath(folder_paths.get_temp_directory()))
+ candidate = Path(os.path.abspath(path))
+ return candidate.is_relative_to(temp_root)
+ except OSError:
+ return False
+
+
+def _stat_consistent(content: AssetContent) -> bool:
+ try:
+ stat = os.stat(content.path)
+ except FileNotFoundError:
+ return False
+ if content.mtime_ns is not None and stat.st_mtime_ns != content.mtime_ns:
+ return False
+ if stat.st_size != content.size_bytes:
+ return False
+ return True
+
+
+def _qualifies(content: AssetContent) -> bool:
+ return (
+ not content.is_missing
+ and os.path.isfile(content.path)
+ and _stat_consistent(content)
+ and not is_temp_path(content.path)
+ )
+
+
+def qualified_content_iterator(session: Session, hash: str) -> Iterator[AssetContent]:
+ rows = session.scalars(
+ select(AssetContent)
+ .where(AssetContent.hash == hash, AssetContent.is_missing.is_(False))
+ .order_by(AssetContent.created_at, AssetContent.id)
+ )
+ for row in rows:
+ if _qualifies(row):
+ yield row
+
+
+def claim_qualified_content(session: Session, content_id: str, hash: str) -> bool:
+ """Claim a live matching content row before attaching a record.
+
+ The conditional update takes this session's SQLite write lock through commit.
+ False means the row was retired or changed after lookup.
+ """
+ # This session's connection keeps the claim's write lock through this session's commit.
+ result = session.connection().execute(
+ update(AssetContent)
+ .where(
+ AssetContent.id == content_id,
+ AssetContent.hash == hash,
+ AssetContent.is_missing.is_(False),
+ )
+ .values(is_missing=False)
+ )
+ return result.rowcount == 1
+
+
+def refresh_qualified_content(session: Session, content_id: str) -> AssetContent | None:
+ content = session.get(AssetContent, content_id, populate_existing=True)
+ if content is None or not _qualifies(content):
+ return None
+ return content
+
+
+def lookup_for_from_hash(session: Session, hash: str) -> AssetContent | None:
+ if not mode.hashing_enabled():
+ return None
+ return next(qualified_content_iterator(session, hash), None)
+
+
+def lookup_for_view(session: Session, hash: str) -> AssetContent | None:
+ return next(qualified_content_iterator(session, hash), None)
diff --git a/app/assets/services/schemas.py b/app/assets/services/schemas.py
index 0fda6871d..54ff299e2 100644
--- a/app/assets/services/schemas.py
+++ b/app/assets/services/schemas.py
@@ -1,9 +1,13 @@
+"""Defines the frozen result types the service layer hands back to routes and to
+the seeder, keeping those callers off ORM objects and the sessions that own
+them. Each type carries only what its caller renders, so nothing can be lazily
+loaded from a row after its session has closed.
+"""
+
from dataclasses import dataclass
from datetime import datetime
from typing import Any, NamedTuple
-from app.assets.database.models import Asset, AssetReference
-
UserMetadata = dict[str, Any] | None
@@ -12,6 +16,7 @@ class AssetData:
hash: str | None
size_bytes: int | None
mime_type: str | None
+ is_missing: bool = False
@dataclass(frozen=True)
@@ -81,35 +86,27 @@ class DownloadResolutionResult:
download_name: str
+@dataclass(frozen=True)
+class RegisteredAsset:
+ id: str
+ content_id: str
+ job_id: str | None
+ name: str
+
+
+@dataclass(frozen=True)
+class UploadAssetView:
+ asset: RegisteredAsset
+ asset_hash: str | None
+ size: int | None
+ mime_type: str | None
+ tags: list[str]
+
+
@dataclass(frozen=True)
class UploadResult:
ref: ReferenceData
+ content_id: str
asset: AssetData
tags: list[str]
created_new: bool
-
-
-def extract_reference_data(ref: AssetReference) -> ReferenceData:
- return ReferenceData(
- id=ref.id,
- name=ref.name,
- file_path=ref.file_path,
- loader_path=ref.loader_path,
- user_metadata=ref.user_metadata,
- preview_id=ref.preview_id,
- system_metadata=ref.system_metadata,
- job_id=ref.job_id,
- created_at=ref.created_at,
- updated_at=ref.updated_at,
- last_access_time=ref.last_access_time,
- )
-
-
-def extract_asset_data(asset: Asset | None) -> AssetData | None:
- if asset is None:
- return None
- return AssetData(
- hash=asset.hash,
- size_bytes=asset.size_bytes,
- mime_type=asset.mime_type,
- )
diff --git a/app/assets/services/snapshot_hash.py b/app/assets/services/snapshot_hash.py
new file mode 100644
index 000000000..d3e9b8c71
--- /dev/null
+++ b/app/assets/services/snapshot_hash.py
@@ -0,0 +1,51 @@
+"""Hashes a file and returns the stat it proved describes those exact bytes.
+Identity, size and mtime are sampled before the read, on the open handle at
+both ends of it, and once more afterwards; if any sample disagrees the file
+moved under the reader and the result is discarded rather than returned.
+Callers persist the stat that comes back, which is what makes a stored hash and
+a stored size describe one observation.
+"""
+
+from __future__ import annotations
+
+import os
+from dataclasses import dataclass
+
+from blake3 import blake3
+
+
+@dataclass(frozen=True, slots=True)
+class _Snapshot:
+ dev: int
+ ino: int
+ mtime_ns: int
+ size: int
+
+
+def _snapshot(stat_result: os.stat_result) -> _Snapshot:
+ return _Snapshot(
+ dev=stat_result.st_dev,
+ ino=stat_result.st_ino,
+ mtime_ns=stat_result.st_mtime_ns,
+ size=stat_result.st_size,
+ )
+
+
+def snapshot_hash(
+ path: str, chunk_size: int = 8 * 1024 * 1024
+) -> tuple[str, os.stat_result] | None:
+ try:
+ pre_stat = _snapshot(os.stat(path))
+ hasher = blake3()
+ with open(path, "rb") as file:
+ open_stat = _snapshot(os.fstat(file.fileno()))
+ while chunk := file.read(chunk_size):
+ hasher.update(chunk)
+ post_hash_stat = _snapshot(os.fstat(file.fileno()))
+ post_stat_result = os.stat(path)
+ except FileNotFoundError:
+ return None
+ post_stat = _snapshot(post_stat_result)
+ if len({pre_stat, open_stat, post_hash_stat, post_stat}) != 1:
+ return None
+ return hasher.hexdigest(), post_stat_result
diff --git a/app/assets/services/tagging.py b/app/assets/services/tagging.py
index 69c8cf39c..d22eb8c0a 100644
--- a/app/assets/services/tagging.py
+++ b/app/assets/services/tagging.py
@@ -1,14 +1,27 @@
+"""Applies and removes tags on a record and answers tag listing and histogram
+queries. Adding reports what it actually inserted, read back after the write,
+so a caller that lost a race is told the tag was already present rather than
+that it added one. Removal leaves automatically applied tags in place and
+reports them as protected instead of silently skipping them.
+"""
+
from typing import Sequence
+from sqlalchemy import delete, select
+
from app.assets.database.queries import (
AddTagsResult,
RemoveTagsResult,
- add_tags_to_reference,
- get_reference_with_owner_check,
list_tags_with_usage,
- remove_tags_from_reference,
+)
+from app.assets.database.queries.records import (
+ bump_record_updated_at,
+ ensure_tag,
+ ensure_tag_link,
)
from app.assets.database.queries.tags import list_tag_counts_for_filtered_assets
+from app.assets.database.models import Asset, AssetTag
+from app.assets.helpers import normalize_tags
from app.assets.services.schemas import TagUsage
from app.database.db import create_session
@@ -17,40 +30,93 @@ def apply_tags(
reference_id: str,
tags: list[str],
origin: str = "manual",
- owner_id: str = "",
) -> AddTagsResult:
with create_session() as session:
- ref_row = get_reference_with_owner_check(session, reference_id, owner_id)
+ if session.get(Asset, reference_id) is None:
+ raise ValueError(f"Asset {reference_id} not found")
- result = add_tags_to_reference(
- session,
- reference_id=reference_id,
- tags=tags,
- origin=origin,
- create_if_missing=True,
- reference_row=ref_row,
+ normalized_tags = normalize_tags(tags)
+ requested_tags = set(normalized_tags)
+ added: list[str] = []
+ for tag_name in normalized_tags:
+ ensure_tag(session, tag_name)
+ if ensure_tag_link(
+ session,
+ asset_id=reference_id,
+ tag_name=tag_name,
+ origin=origin,
+ ):
+ added.append(tag_name)
+ session.flush()
+ if added:
+ bump_record_updated_at(session, reference_id)
+ total_tags = list(
+ session.scalars(
+ select(AssetTag.tag_name)
+ .where(AssetTag.asset_id == reference_id)
+ .order_by(AssetTag.tag_name)
+ )
)
session.commit()
- return result
+ return AddTagsResult(
+ added=sorted(added),
+ already_present=sorted((requested_tags & set(total_tags)) - set(added)),
+ total_tags=total_tags,
+ )
def remove_tags(
reference_id: str,
tags: list[str],
- owner_id: str = "",
) -> RemoveTagsResult:
with create_session() as session:
- get_reference_with_owner_check(session, reference_id, owner_id)
+ if session.get(Asset, reference_id) is None:
+ raise ValueError(f"Asset {reference_id} not found")
- result = remove_tags_from_reference(
- session,
- reference_id=reference_id,
- tags=tags,
+ requested_tags = set(normalize_tags(tags))
+ removable_tags = set(
+ session.scalars(
+ select(AssetTag.tag_name).where(
+ AssetTag.asset_id == reference_id,
+ AssetTag.origin != "automatic",
+ AssetTag.tag_name.in_(requested_tags),
+ )
+ )
+ )
+ protected_tags = set(
+ session.scalars(
+ select(AssetTag.tag_name).where(
+ AssetTag.asset_id == reference_id,
+ AssetTag.origin == "automatic",
+ AssetTag.tag_name.in_(requested_tags),
+ )
+ )
+ )
+ if removable_tags:
+ session.execute(
+ delete(AssetTag).where(
+ AssetTag.asset_id == reference_id,
+ AssetTag.origin != "automatic",
+ AssetTag.tag_name.in_(removable_tags),
+ )
+ )
+ bump_record_updated_at(session, reference_id)
+ total_tags = list(
+ session.scalars(
+ select(AssetTag.tag_name)
+ .where(AssetTag.asset_id == reference_id)
+ .order_by(AssetTag.tag_name)
+ )
)
session.commit()
- return result
+ return RemoveTagsResult(
+ removed=sorted(removable_tags),
+ not_present=sorted(requested_tags - removable_tags - protected_tags),
+ total_tags=total_tags,
+ protected=sorted(protected_tags),
+ )
def list_tags(
@@ -59,7 +125,6 @@ def list_tags(
offset: int = 0,
order: str = "count_desc",
include_zero: bool = True,
- owner_id: str = "",
) -> tuple[list[TagUsage], int]:
limit = max(1, min(1000, limit))
offset = max(0, offset)
@@ -72,18 +137,15 @@ def list_tags(
offset=offset,
include_zero=include_zero,
order=order,
- owner_id=owner_id,
)
return [TagUsage(name, count) for name, count in rows], total
def list_tag_histogram(
- owner_id: str = "",
include_tags: Sequence[str] | None = None,
exclude_tags: Sequence[str] | None = None,
name_contains: str | None = None,
- metadata_filter: dict | None = None,
limit: int = 100,
# Appended last so pre-existing positional callers keep binding correctly.
any_tags: Sequence[str] | None = None,
@@ -91,11 +153,9 @@ def list_tag_histogram(
with create_session() as session:
return list_tag_counts_for_filtered_assets(
session,
- owner_id=owner_id,
include_tags=include_tags,
exclude_tags=exclude_tags,
any_tags=any_tags,
name_contains=name_contains,
- metadata_filter=metadata_filter,
limit=limit,
)
diff --git a/app/database/db.py b/app/database/db.py
index 919206a59..053df2883 100644
--- a/app/database/db.py
+++ b/app/database/db.py
@@ -21,6 +21,7 @@ try:
from app.database.models import Base
import app.assets.database.models # noqa: F401 — register models with Base.metadata
+ import blake3 # noqa: F401 — verify the hard dependency is importable at startup
_DB_AVAILABLE = True
except ImportError as e:
@@ -184,6 +185,29 @@ def _init_file_db(db_url):
prepare_file_db_path(db_path)
db_exists = os.path.exists(db_path)
+ # Lock BEFORE any migration work — deliberately diverging from upstream master, whose
+ # "it would block Alembic" rationale is false (the lock guards a separate `<db>.lock`
+ # file). Only this order makes revision inspection, backup, upgrade and the failure-path
+ # restore mutually exclusive between processes.
+ _acquire_file_lock(db_path)
+ try:
+ _migrate_and_bind(db_url, db_path, db_exists)
+ except Exception:
+ _db_lock.release()
+ raise
+
+
+_DESTRUCTIVE_REVISION = "0007_record_content_split"
+
+
+def _upgrade_discards_the_catalog(script, target_rev, current_rev):
+ return any(
+ revision.revision == _DESTRUCTIVE_REVISION
+ for revision in script.iterate_revisions(upper=target_rev, lower=current_rev)
+ )
+
+
+def _migrate_and_bind(db_url, db_path, db_exists):
config = get_alembic_config()
# Check if we need to upgrade
@@ -225,11 +249,15 @@ def _init_file_db(db_url):
logging.exception("Error upgrading database: ")
raise e
- # Acquire an OS-level file lock after migrations are complete.
- # Alembic uses its own connection, so we must wait until it's done
- # before locking — otherwise our own lock blocks the migration.
+ if backup_path and _upgrade_discards_the_catalog(script, target_rev, current_rev):
+ log_startup_warning(
+ f"The asset catalog was rebuilt from scratch by migration "
+ f"{_DESTRUCTIVE_REVISION}: manual tags, user metadata, previews, renames, "
+ f"API-created records and job_id links from the previous database were "
+ f"discarded. The database from before the upgrade was kept at {backup_path}."
+ )
+
conn.close()
- _acquire_file_lock(db_path)
global Session
Session = sessionmaker(bind=engine)
diff --git a/comfy_api/feature_flags.py b/comfy_api/feature_flags.py
index 9fbef8260..00f6bb65f 100644
--- a/comfy_api/feature_flags.py
+++ b/comfy_api/feature_flags.py
@@ -109,6 +109,7 @@ _CORE_FEATURE_FLAGS: dict[str, Any] = {
"max_upload_size": args.max_upload_size * 1024 * 1024, # Convert MB to bytes
"extension": {"manager": {"supports_v4": True}},
"node_replacements": True,
+ # Mirrors the constructed AssetManager; no degradation path exists, so this always agrees with it.
"assets": args.enable_assets,
}
diff --git a/comfy_execution/asset_enrichment.py b/comfy_execution/asset_enrichment.py
index 38e9496a8..328f38298 100644
--- a/comfy_execution/asset_enrichment.py
+++ b/comfy_execution/asset_enrichment.py
@@ -1,66 +1,115 @@
-"""Enrich executed-node output entries with asset id."""
+"""Registers the files a prompt produced as assets, at the moment the execution
+result is emitted. Each output is matched to a path inside its declared base
+directory and skipped when it escapes that directory or is not on disk, and a
+run served from cache replays the same registration so cached results still
+yield assets. Registering at emission rather than by inspecting the cache
+afterwards keeps this independent of any cache eviction policy.
+"""
+
+import copy
import logging
import os
+from typing import TYPE_CHECKING, Callable
+if TYPE_CHECKING:
+ from app.assets.manager import AssetManager
+ from app.assets.services.schemas import RegisteredAsset
+ from comfy_execution.server_protocol import ExecutionServer
+ from execution import CacheEntry
-def enrich_output_with_assets(output_ui: dict) -> dict:
- """Register file-type output entries as assets and inject their ``id``.
- Runs at output-processing time, once per produced output, when
- --enable-assets is set. Returns a new dict; entries without a resolvable
- on-disk file path are left unchanged. Errors are caught per-entry so a
- failure never blocks execution or the other entries.
- """
- from comfy.cli_args import args
- if not args.enable_assets:
- return output_ui
+def _resolve_output_path(entry: dict) -> str | None:
+ """Resolve an output entry to an absolute, in-base, on-disk file path.
+ Returns ``None`` (skip, no registration) when the type is unknown, the
+ resolved path escapes its base directory, or the file does not exist.
+ """
import folder_paths
- from app.assets.services.ingest import register_file_in_place, DependencyMissingError
- enriched = {}
- for key, entries in output_ui.items():
+ base = folder_paths.get_directory_by_type(entry["type"])
+ if base is None:
+ return None
+ base_abs = os.path.abspath(base)
+ abs_path = os.path.abspath(os.path.join(base_abs, entry.get("subfolder") or "", entry["filename"]))
+ try:
+ if os.path.commonpath([base_abs, abs_path]) != base_abs:
+ return None
+ except ValueError:
+ return None
+ if not os.path.isfile(abs_path):
+ return None
+ return abs_path
+
+
+def _enrich_in_place(
+ output_ui: dict,
+ job_id: str | None,
+ register: Callable[[str, str | None], "RegisteredAsset | None"],
+) -> None:
+ """S10.6: producers that write the same output path are not coalesced (unsupported)."""
+ for entries in output_ui.values():
if not isinstance(entries, list):
- enriched[key] = entries
continue
- new_entries = []
for entry in entries:
if not isinstance(entry, dict) or "filename" not in entry or "type" not in entry:
- new_entries.append(entry)
continue
try:
- base = folder_paths.get_directory_by_type(entry["type"])
- if base is None:
- new_entries.append(entry)
- continue
- base_abs = os.path.abspath(base)
- abs_path = os.path.abspath(os.path.join(base_abs, entry.get("subfolder") or "", entry["filename"]))
- try:
- if os.path.commonpath([base_abs, abs_path]) != base_abs:
- raise ValueError("escapes base")
- except ValueError:
- logging.warning("Asset enrichment skipped (path escapes base): %s", entry.get("filename"))
- new_entries.append(entry)
+ abs_path = _resolve_output_path(entry)
+ if abs_path is None:
continue
- if not os.path.isfile(abs_path):
- new_entries.append(entry)
- continue
-
- # Register unconditionally: the file was just produced, and
- # register_file_in_place re-hashes so an overwritten path can
- # never carry a stale id.
- result = register_file_in_place(
- abs_path=abs_path,
- name=entry["filename"],
- tags=[entry["type"]],
- )
-
- entry = dict(entry)
- entry["id"] = result.ref.id
- except DependencyMissingError:
- logging.warning("Asset enrichment skipped (blake3 not available): %s", entry.get("filename"))
+ result = register(abs_path, job_id)
+ if result is not None:
+ entry["id"] = result.id
except Exception:
- logging.warning("Failed to enrich output entry with asset id: %s", entry.get("filename"), exc_info=True)
- new_entries.append(entry)
- enriched[key] = new_entries
+ logging.warning("Asset registration failed for output: %s", entry.get("filename"), exc_info=True)
+
+
+def _strip_ids(output_ui: dict) -> None:
+ for entries in output_ui.values():
+ if not isinstance(entries, list):
+ continue
+ for entry in entries:
+ if isinstance(entry, dict):
+ entry.pop("id", None)
+
+
+def register_executed_outputs(output_ui: dict, job_id: str, asset_manager: "AssetManager") -> dict:
+ enriched = copy.deepcopy(output_ui)
+ if not asset_manager.enabled:
+ return enriched
+
+ _enrich_in_place(enriched, job_id, asset_manager.register_executed_output)
+ return enriched
+
+
+def register_cached_outputs(ui_wrapper: dict | None, job_id: str, asset_manager: "AssetManager") -> dict | None:
+ if ui_wrapper is None:
+ return None
+
+ enriched = copy.deepcopy(ui_wrapper)
+ output_ui = enriched.get("output")
+ if not isinstance(output_ui, dict):
+ return enriched
+ _strip_ids(output_ui)
+
+ if not asset_manager.enabled:
+ return enriched
+
+ _enrich_in_place(output_ui, job_id, asset_manager.register_cached_output)
return enriched
+
+
+def emit_cached_output(server: "ExecutionServer", node_id: str, display_node_id: str, cached: "CacheEntry", prompt_id: str, ui_outputs: dict, asset_manager: "AssetManager") -> None:
+ if node_id in ui_outputs:
+ return
+ enriched = register_cached_outputs(cached.ui, prompt_id, asset_manager)
+ if enriched is not None:
+ ui_outputs[node_id] = enriched
+ if server.client_id is None:
+ return
+ output = enriched.get("output") if enriched is not None else None
+ server.send_sync(
+ "executed",
+ {"node": node_id, "display_node": display_node_id, "output": output, "prompt_id": prompt_id},
+ server.client_id,
+ )
diff --git a/comfy_execution/progress.py b/comfy_execution/progress.py
index 731b8dc66..f7145dbd2 100644
--- a/comfy_execution/progress.py
+++ b/comfy_execution/progress.py
@@ -7,6 +7,7 @@ from tqdm import tqdm
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from comfy_execution.graph import DynamicPrompt
+ from comfy_execution.server_protocol import ExecutionServer
from protocol import BinaryEventTypes
from comfy_api import feature_flags
@@ -150,7 +151,7 @@ class WebUIProgressHandler(ProgressHandler):
Handler that sends progress updates to the WebUI via WebSockets.
"""
- def __init__(self, server_instance):
+ def __init__(self, server_instance: "ExecutionServer"):
super().__init__("webui")
self.server_instance = server_instance
diff --git a/comfy_execution/server_protocol.py b/comfy_execution/server_protocol.py
new file mode 100644
index 000000000..b3f0a8bb5
--- /dev/null
+++ b/comfy_execution/server_protocol.py
@@ -0,0 +1,11 @@
+from typing import Protocol
+
+
+class ExecutionServer(Protocol):
+ client_id: str | None
+ last_node_id: str | None
+ sockets_metadata: dict[str, dict[str, object]]
+
+ def send_sync(self, event: str | int, data: object, sid: str | None = None) -> None: ...
+
+ def queue_updated(self) -> None: ...
diff --git a/execution.py b/execution.py
index 48c6f641b..1c9e531de 100644
--- a/execution.py
+++ b/execution.py
@@ -7,7 +7,7 @@ import threading
import time
import traceback
from enum import Enum
-from typing import List, Literal, NamedTuple, Optional, Union
+from typing import TYPE_CHECKING, List, Literal, NamedTuple, Optional, Union
import asyncio
import torch
@@ -43,10 +43,14 @@ from comfy_execution.graph_utils import GraphBuilder, is_link
from comfy_execution.validation import validate_node_input
from comfy_execution.progress import get_progress_state, reset_progress_state, add_progress_handler, WebUIProgressHandler
from comfy_execution.utils import CurrentNodeContext
-from comfy_execution.asset_enrichment import enrich_output_with_assets
+from comfy_execution.asset_enrichment import register_executed_outputs, emit_cached_output
from comfy_api.internal import _ComfyNodeInternal, _NodeOutputInternal, first_real_override, is_class, make_locked_method_func
from comfy_api.latest import io, _io
from comfy_execution.cache_provider import _has_cache_providers, _get_cache_providers, _logger as _cache_logger
+from app.assets.manager import AssetManager, default_asset_manager
+
+if TYPE_CHECKING:
+ from comfy_execution.server_protocol import ExecutionServer
class ExecutionResult(Enum):
@@ -427,15 +431,7 @@ def _is_intermediate_output(dynprompt, node_id):
return getattr(class_def, 'HAS_INTERMEDIATE_OUTPUT', False)
-def _send_cached_ui(server, node_id, display_node_id, cached, prompt_id, ui_outputs):
- if cached.ui is not None:
- ui_outputs[node_id] = cached.ui
- if server.client_id is None:
- return
- cached_ui = cached.ui or {}
- server.send_sync("executed", { "node": node_id, "display_node": display_node_id, "output": cached_ui.get("output", None), "prompt_id": prompt_id }, server.client_id)
-
-async def execute(server, dynprompt, caches, current_item, extra_data, executed, prompt_id, execution_list, pending_subgraph_results, pending_async_nodes, ui_outputs):
+async def execute(server: "ExecutionServer", dynprompt, caches, current_item, extra_data, executed, prompt_id, execution_list, pending_subgraph_results, pending_async_nodes, ui_outputs, asset_manager: AssetManager):
unique_id = current_item
real_node_id = dynprompt.get_real_node_id(unique_id)
display_node_id = dynprompt.get_display_node_id(unique_id)
@@ -445,7 +441,7 @@ async def execute(server, dynprompt, caches, current_item, extra_data, executed,
class_def = nodes.NODE_CLASS_MAPPINGS[class_type]
cached = await caches.outputs.get(unique_id)
if cached is not None:
- _send_cached_ui(server, unique_id, display_node_id, cached, prompt_id, ui_outputs)
+ emit_cached_output(server, unique_id, display_node_id, cached, prompt_id, ui_outputs, asset_manager)
get_progress_state().finish_progress(unique_id)
execution_list.cache_update(unique_id, cached)
return (ExecutionResult.SUCCESS, None, None)
@@ -560,22 +556,19 @@ async def execute(server, dynprompt, caches, current_item, extra_data, executed,
unblock()
asyncio.create_task(await_completion())
return (ExecutionResult.PENDING, None, None)
+ cache_ui_value = ui_outputs.get(unique_id)
if len(output_ui) > 0:
- # Enrich at output-processing time (not in the send path) so assets
- # are registered even when no client is connected, and the asset id
- # flows into ui_outputs and the cache alongside the raw entries.
- output_ui = enrich_output_with_assets(output_ui)
- ui_outputs[unique_id] = {
- "meta": {
- "node_id": unique_id,
- "display_node": display_node_id,
- "parent_node": parent_node_id,
- "real_node_id": real_node_id,
- },
- "output": output_ui
+ meta = {
+ "node_id": unique_id,
+ "display_node": display_node_id,
+ "parent_node": parent_node_id,
+ "real_node_id": real_node_id,
}
+ enriched_output_ui = register_executed_outputs(output_ui, prompt_id, asset_manager)
+ ui_outputs[unique_id] = {"meta": meta, "output": enriched_output_ui}
+ cache_ui_value = {"meta": meta, "output": output_ui}
if server.client_id is not None:
- server.send_sync("executed", { "node": unique_id, "display_node": display_node_id, "output": output_ui, "prompt_id": prompt_id }, server.client_id)
+ server.send_sync("executed", { "node": unique_id, "display_node": display_node_id, "output": enriched_output_ui, "prompt_id": prompt_id }, server.client_id)
if has_subgraph:
cached_outputs = []
new_node_ids = []
@@ -612,7 +605,7 @@ async def execute(server, dynprompt, caches, current_item, extra_data, executed,
pending_subgraph_results[unique_id] = cached_outputs
return (ExecutionResult.PENDING, None, None)
- cache_entry = CacheEntry(ui=ui_outputs.get(unique_id), outputs=output_data)
+ cache_entry = CacheEntry(ui=cache_ui_value, outputs=output_data)
execution_list.cache_update(unique_id, cache_entry)
await caches.outputs.set(unique_id, cache_entry)
@@ -662,10 +655,11 @@ async def execute(server, dynprompt, caches, current_item, extra_data, executed,
return (ExecutionResult.SUCCESS, None, None)
class PromptExecutor:
- def __init__(self, server, cache_type=False, cache_args=None):
+ def __init__(self, server: "ExecutionServer", cache_type=False, cache_args=None, asset_manager: AssetManager | None = None):
self.cache_args = cache_args
self.cache_type = cache_type
self.server = server
+ self.asset_manager = asset_manager if asset_manager is not None else default_asset_manager()
self.prompt_model_tracker = comfy.model_patcher.PromptModelTracker()
self.reset()
@@ -786,7 +780,7 @@ class PromptExecutor:
break
assert node_id is not None, "Node ID should not be None at this point"
- result, error, ex = await execute(self.server, dynamic_prompt, self.caches, node_id, extra_data, executed, prompt_id, execution_list, pending_subgraph_results, pending_async_nodes, ui_node_outputs)
+ result, error, ex = await execute(self.server, dynamic_prompt, self.caches, node_id, extra_data, executed, prompt_id, execution_list, pending_subgraph_results, pending_async_nodes, ui_node_outputs, self.asset_manager)
self.success = result != ExecutionResult.FAILURE
if result == ExecutionResult.FAILURE:
self.handle_execution_error(prompt_id, dynamic_prompt.original_prompt, current_outputs, executed, error, ex)
@@ -820,7 +814,7 @@ class PromptExecutor:
cached = await self.caches.outputs.get(node_id)
if cached is not None:
display_node_id = dynamic_prompt.get_display_node_id(node_id)
- _send_cached_ui(self.server, node_id, display_node_id, cached, prompt_id, ui_node_outputs)
+ emit_cached_output(self.server, node_id, display_node_id, cached, prompt_id, ui_node_outputs, self.asset_manager)
self.add_message("execution_success", { "prompt_id": prompt_id }, broadcast=False)
ui_outputs = {}
@@ -1249,7 +1243,7 @@ async def validate_prompt(prompt_id, prompt, partial_execution_list: Union[list[
MAXIMUM_HISTORY_SIZE = 10000
class PromptQueue:
- def __init__(self, server):
+ def __init__(self, server: "ExecutionServer"):
self.server = server
self.mutex = threading.RLock()
self.not_empty = threading.Condition(self.mutex)
diff --git a/main.py b/main.py
index 372c9aead..3c6e41d35 100644
--- a/main.py
+++ b/main.py
@@ -22,8 +22,9 @@ console_log_level = get_console_log_level(args.verbose)
file_log_outputs = get_file_log_outputs(args.verbose)
setup_logger(log_level=console_log_level, file_outputs=file_log_outputs, use_stdout=args.log_stdout)
-from app.assets.seeder import asset_seeder
-from app.assets.services import register_output_files
+from app.database.db import dependencies_available, init_db
+from app.assets.lifecycle import cleanup_temp_filesystem
+from app.assets.manager import AssetManager, default_asset_manager
import itertools
import utils.extra_config
from utils.mime_types import init_mime_types
@@ -34,7 +35,6 @@ import sys
from comfy_execution.progress import get_progress_state
from comfy_execution.utils import get_executing_context
from comfy_api import feature_flags
-from app.database.db import init_db, dependencies_available
if __name__ == "__main__":
#NOTE: These do not do anything on core ComfyUI, they are for custom nodes.
@@ -316,39 +316,7 @@ def cuda_malloc_warning():
logging.warning("\nWARNING: this card most likely does not support cuda-malloc, if you get \"CUDA error\" please run ComfyUI with: --disable-cuda-malloc\n")
-def _collect_output_absolute_paths(history_result: dict) -> list[str]:
- """Extract absolute file paths for output items from a history result."""
- paths: list[str] = []
- seen: set[str] = set()
- for node_output in history_result.get("outputs", {}).values():
- for items in node_output.values():
- if not isinstance(items, list):
- continue
- for item in items:
- if not isinstance(item, dict):
- continue
- item_type = item.get("type")
- if item_type not in ("output", "temp"):
- continue
- base_dir = folder_paths.get_directory_by_type(item_type)
- if base_dir is None:
- continue
- base_dir = os.path.abspath(base_dir)
- filename = item.get("filename")
- if not filename:
- continue
- abs_path = os.path.abspath(
- os.path.join(base_dir, item.get("subfolder", ""), filename)
- )
- if not abs_path.startswith(base_dir + os.sep) and abs_path != base_dir:
- continue
- if abs_path not in seen:
- seen.add(abs_path)
- paths.append(abs_path)
- return paths
-
-
-def prompt_worker(q, server_instance):
+def prompt_worker(q, server_instance, asset_manager):
current_time: float = 0.0
cache_ram = 0
cache_ram_inactive = 0
@@ -368,7 +336,7 @@ def prompt_worker(q, server_instance):
elif args.cache_none:
cache_type = execution.CacheType.NONE
- e = execution.PromptExecutor(server_instance, cache_type=cache_type, cache_args={ "lru" : args.cache_lru, "ram" : cache_ram, "ram_inactive" : cache_ram_inactive } )
+ e = execution.PromptExecutor(server_instance, cache_type=cache_type, cache_args={ "lru" : args.cache_lru, "ram" : cache_ram, "ram_inactive" : cache_ram_inactive }, asset_manager=asset_manager )
last_gc_collect = 0
need_gc = False
gc_collect_interval = 10.0
@@ -390,7 +358,7 @@ def prompt_worker(q, server_instance):
for k in sensitive:
extra_data[k] = sensitive[k]
- asset_seeder.pause()
+ asset_manager.pause_background_scan()
e.execute(item[2], prompt_id, extra_data, item[4])
need_gc = True
@@ -415,10 +383,6 @@ def prompt_worker(q, server_instance):
else:
logging.info("Prompt executed in {:.2f} seconds".format(execution_time), extra={'color': 'green'})
- if not asset_seeder.is_disabled():
- paths = _collect_output_absolute_paths(e.history_result)
- register_output_files(paths, job_id=prompt_id)
-
flags = q.get_flags()
free_memory = flags.get("free_memory", False)
@@ -441,9 +405,8 @@ def prompt_worker(q, server_instance):
need_gc = False
hook_breaker_ac10a0.restore_functions()
- if not asset_seeder.is_disabled():
- asset_seeder.enqueue_enrich(roots=("output",), compute_hashes=args.enable_asset_hashing)
- asset_seeder.resume()
+ asset_manager.queue_output_scan()
+ asset_manager.resume_background_scan()
async def run(server_instance, address='', port=8188, verbose=True, call_on_start=None):
@@ -486,19 +449,13 @@ def hijack_progress(server_instance):
comfy.utils.set_progress_bar_global_hook(hook)
-def cleanup_temp():
- temp_dir = folder_paths.get_temp_directory()
- if os.path.exists(temp_dir):
- shutil.rmtree(temp_dir, ignore_errors=True)
-
+def setup_database(asset_manager):
+ if not dependencies_available():
+ return
-def setup_database():
try:
- if dependencies_available():
- init_db()
- if args.enable_assets:
- if asset_seeder.start(roots=("models", "input", "output"), prune_first=True, compute_hashes=args.enable_asset_hashing):
- logging.info("Background asset scan initiated for models, input, output")
+ init_db()
+ asset_manager.startup()
except Exception as e:
if "database is locked" in str(e):
logging.error(
@@ -507,6 +464,15 @@ def setup_database():
" --database-url sqlite:///path/to/another.db"
)
sys.exit(1)
+ if "Could not acquire lock on database" in str(e):
+ logging.error(
+ "Database is locked. Another ComfyUI process is already using this database.\n"
+ "To resolve this, specify a separate database file for this instance:\n"
+ " --database-url sqlite:///path/to/another.db"
+ )
+ if args.enable_assets:
+ sys.exit(1)
+ return
if args.enable_assets:
logging.error(
f"Failed to initialize database: {e}\n"
@@ -529,12 +495,15 @@ def start_comfyui(asyncio_loop=None):
temp_dir = os.path.join(os.path.abspath(args.temp_directory), "temp")
logging.info(f"Setting temp directory to: {temp_dir}")
folder_paths.set_temp_directory(temp_dir)
- cleanup_temp()
+
+ asset_manager: AssetManager = default_asset_manager()
+ if not asset_manager.enabled:
+ cleanup_temp_filesystem()
if not asyncio_loop:
asyncio_loop = asyncio.new_event_loop()
asyncio.set_event_loop(asyncio_loop)
- prompt_server = server.PromptServer(asyncio_loop)
+ prompt_server = server.PromptServer(asyncio_loop, asset_manager)
if args.enable_manager and not args.disable_manager_ui:
comfyui_manager.start()
@@ -552,12 +521,12 @@ def start_comfyui(asyncio_loop=None):
hook_breaker_ac10a0.restore_functions()
cuda_malloc_warning()
- setup_database()
+ setup_database(asset_manager)
prompt_server.add_routes()
hijack_progress(prompt_server)
- threading.Thread(target=prompt_worker, daemon=True, args=(prompt_server.prompt_queue, prompt_server,)).start()
+ threading.Thread(target=prompt_worker, daemon=True, args=(prompt_server.prompt_queue, prompt_server, asset_manager)).start()
if args.quick_test_for_ci:
exit(0)
@@ -605,7 +574,7 @@ if __name__ == "__main__":
"dynamic vram enabled and using native ComfyUI model formats instead. "
"ComfyUI native formats like fp8, int8 and w4a8 will be faster even if they are larger than your memory."
)
- event_loop, _, start_all_func = start_comfyui()
+ event_loop, prompt_server, start_all_func = start_comfyui()
try:
x = start_all_func()
app.logger.print_startup_warnings()
@@ -613,5 +582,4 @@ if __name__ == "__main__":
except KeyboardInterrupt:
logging.info("\nStopped server")
finally:
- asset_seeder.shutdown()
- cleanup_temp()
+ prompt_server.asset_manager.shutdown()
diff --git a/openapi.yaml b/openapi.yaml
index e3f9609c5..d2288354e 100644
--- a/openapi.yaml
+++ b/openapi.yaml
@@ -11,10 +11,6 @@ components:
description: Display name of the asset. Mirrors name for backwards compatibility.
nullable: true
type: string
- file_path:
- description: Relative path in global-namespace-root form (e.g. "models/checkpoints/flux.safetensors")
- nullable: true
- type: string
hash:
description: Blake3 hash of the asset content.
pattern: ^blake3:[a-f0-9]{64}$
@@ -152,10 +148,6 @@ components:
description: Display name of the asset. Mirrors name for backwards compatibility.
nullable: true
type: string
- file_path:
- description: Relative path in global-namespace-root form (e.g. "models/checkpoints/flux.safetensors")
- nullable: true
- type: string
hash:
description: Blake3 hash of the asset content.
pattern: ^blake3:[a-f0-9]{64}$
@@ -1714,11 +1706,6 @@ paths:
name: name_contains
schema:
type: string
- - description: JSON object for filtering by metadata fields
- in: query
- name: metadata_filter
- schema:
- type: string
- description: Maximum number of assets to return (1-500)
in: query
name: limit
@@ -2552,11 +2539,6 @@ paths:
name: name_contains
schema:
type: string
- - description: JSON object for filtering by metadata fields
- in: query
- name: metadata_filter
- schema:
- type: string
- description: Maximum number of tags to return (1-1000, default 100)
in: query
name: limit
diff --git a/server.py b/server.py
index c9ffcaa0d..aedfa4ec7 100644
--- a/server.py
+++ b/server.py
@@ -44,10 +44,6 @@ import node_helpers
from comfyui_version import __version__
from app.frontend_management import FrontendManager, parse_version
from comfy_api.internal import _ComfyNodeInternal
-from app.assets.seeder import asset_seeder
-from app.assets.api.routes import register_assets_routes
-from app.assets.services.ingest import register_file_in_place
-from app.assets.services.path_utils import get_known_subfolder_tags
from app.assets.services.asset_management import resolve_hash_to_path
from app.user_manager import UserManager
@@ -55,10 +51,13 @@ from app.model_manager import ModelFileManager
from app.custom_node_manager import CustomNodeManager
from app.subgraph_manager import SubgraphManager
from app.node_replace_manager import NodeReplaceManager
-from typing import Optional, Union
+from typing import TYPE_CHECKING, Optional, Union
from api_server.routes.internal.internal_routes import InternalRoutes
from protocol import BinaryEventTypes
+if TYPE_CHECKING:
+ from app.assets.manager import AssetManager
+
# Import cache control middleware
from middleware.cache_middleware import cache_control
@@ -213,9 +212,10 @@ def create_block_external_middleware():
class PromptServer():
- def __init__(self, loop):
+ def __init__(self, loop, asset_manager: "AssetManager"):
PromptServer.instance = self
+ self.asset_manager = asset_manager
self.user_manager = UserManager()
self.model_file_manager = ModelFileManager()
self.custom_node_manager = CustomNodeManager()
@@ -254,11 +254,8 @@ class PromptServer():
else args.front_end_root
)
logging.info(f"[Prompt Server] web root: {self.web_root}")
- if args.enable_assets:
- register_assets_routes(self.app, self.user_manager)
- else:
- register_assets_routes(self.app)
- asset_seeder.disable()
+ self.asset_manager.register_routes(self.app, self.user_manager)
+ self.asset_manager.set_event_sink(self.send_sync)
routes = web.RouteTableDef()
self.routes = routes
self.last_node_id = None
@@ -440,22 +437,22 @@ class PromptServer():
resp = {"name" : filename, "subfolder": subfolder, "type": image_upload_type}
- if args.enable_assets:
- try:
- tag = image_upload_type if image_upload_type in ("input", "output") else "input"
- tags = [tag]
- tags.extend(get_known_subfolder_tags(subfolder))
- result = register_file_in_place(abs_path=filepath, name=filename, tags=tags)
- resp["asset"] = {
- "id": result.ref.id,
- "name": result.ref.name,
- "asset_hash": result.asset.hash,
- "size": result.asset.size_bytes,
- "mime_type": result.asset.mime_type,
- "tags": result.tags,
- }
- except Exception:
- logging.warning("Failed to register uploaded image as asset", exc_info=True)
+ view = self.asset_manager.register_upload(
+ abs_path=filepath,
+ name=filename,
+ upload_type=image_upload_type,
+ subfolder=subfolder,
+ content_written=not image_is_duplicate,
+ )
+ if view is not None:
+ resp["asset"] = {
+ "id": view.asset.id,
+ "name": view.asset.name,
+ "asset_hash": view.asset_hash,
+ "size": view.size,
+ "mime_type": view.mime_type,
+ "tags": view.tags,
+ }
return web.json_response(resp)
else:
@@ -523,8 +520,11 @@ class PromptServer():
# node preview, it constructs /view?filename=<asset_hash>, so this
# endpoint must resolve blake3 hashes to their on-disk file paths.
if filename.startswith("blake3:"):
- owner_id = self.user_manager.get_request_user_id(request)
- result = resolve_hash_to_path(filename, owner_id=owner_id)
+ # Side-effect call: get_request_user_id raises KeyError for an unknown or
+ # system user in multi-user mode, which is what gates hash resolution.
+ # The returned id is deliberately unused (resolution is not owner-scoped).
+ self.user_manager.get_request_user_id(request)
+ result = resolve_hash_to_path(filename)
if result is None:
return web.Response(status=404)
file, filename, resolved_content_type = result.abs_path, result.download_name, result.content_type
@@ -799,7 +799,7 @@ class PromptServer():
@routes.get("/object_info")
async def get_object_info(request):
- asset_seeder.start(roots=("models", "input", "output"))
+ self.asset_manager.ensure_scan_started()
with folder_paths.cache_helper:
out = {}
for x in nodes.NODE_CLASS_MAPPINGS:
diff --git a/tests-unit/app_test/test_db_init_locking.py b/tests-unit/app_test/test_db_init_locking.py
new file mode 100644
index 000000000..b9cfa8c42
--- /dev/null
+++ b/tests-unit/app_test/test_db_init_locking.py
@@ -0,0 +1,155 @@
+import logging
+import os
+import sqlite3
+
+import pytest
+import torch
+from alembic import command
+from alembic.config import Config
+from filelock import FileLock, Timeout
+
+from app.database import db as db_module
+from comfy.cli_args import args as cli_args
+
+if not torch.cuda.is_available():
+ cli_args.cpu = True
+
+import main # noqa: E402
+
+_PRE_HEAD = "0006_add_loader_path"
+
+
+def _make_config(db_path: str) -> Config:
+ root = os.path.join(os.path.dirname(__file__), "../..")
+ cfg = Config(os.path.abspath(os.path.join(root, "alembic.ini")))
+ cfg.set_main_option("script_location", os.path.abspath(os.path.join(root, "alembic_db")))
+ cfg.set_main_option("sqlalchemy.url", f"sqlite:///{db_path}")
+ return cfg
+
+
+def _current_revision(db_path: str) -> str:
+ with sqlite3.connect(db_path) as conn:
+ rows = conn.execute("SELECT version_num FROM alembic_version").fetchall()
+ assert len(rows) == 1
+ return rows[0][0]
+
+
+def stale_db(tmp_path, monkeypatch):
+ db_path = str(tmp_path / "comfyui.db")
+ command.upgrade(_make_config(db_path), _PRE_HEAD)
+
+ monkeypatch.setattr(db_module.args, "database_url", f"sqlite:///{db_path}")
+ monkeypatch.setattr(db_module, "Session", None)
+ monkeypatch.setattr(db_module, "_db_lock", None)
+ yield db_path
+ if db_module._db_lock is not None:
+ db_module._db_lock.release(force=True)
+
+
+def test_init_file_db_migrates_when_lock_is_free(stale_db):
+ db_module._init_file_db(db_module.args.database_url)
+
+ assert _current_revision(stale_db) != _PRE_HEAD
+ assert os.path.exists(stale_db + ".bkp")
+
+
+def test_successful_init_keeps_holding_the_lock(stale_db):
+ db_module._init_file_db(db_module.args.database_url)
+
+ contender = FileLock(stale_db + ".lock")
+ with pytest.raises(Timeout):
+ contender.acquire(timeout=0)
+
+
+def test_failed_init_releases_the_lock(stale_db, monkeypatch):
+ def _explode():
+ raise RuntimeError("alembic config exploded")
+
+ monkeypatch.setattr(db_module, "get_alembic_config", _explode)
+
+ with pytest.raises(RuntimeError, match="alembic config exploded"):
+ db_module._init_file_db(db_module.args.database_url)
+
+ contender = FileLock(stale_db + ".lock")
+ try:
+ contender.acquire(timeout=0)
+ except Timeout:
+ pytest.fail(
+ "a failed init stranded the lock; setup_database logs and CONTINUES when assets are "
+ "disabled, so this process would block every other instance for its whole lifetime "
+ "over a database it never opened"
+ )
+ contender.release()
+
+
+def test_held_lock_blocks_before_any_migration_work(stale_db):
+ holder = FileLock(stale_db + ".lock")
+ holder.acquire(timeout=0)
+ try:
+ with pytest.raises(RuntimeError, match="Another ComfyUI process may already be using it"):
+ db_module._init_file_db(db_module.args.database_url)
+
+ assert not os.path.exists(stale_db + ".bkp")
+ assert _current_revision(stale_db) == _PRE_HEAD
+ assert db_module.Session is None
+ finally:
+ holder.release()
+
+
+def test_setup_database_routes_file_lock_to_lock_guidance(monkeypatch, caplog):
+ monkeypatch.setattr(main, "dependencies_available", lambda: True)
+
+ def _raise_file_lock():
+ raise RuntimeError(
+ "Could not acquire lock on database 'x.db'. "
+ "Another ComfyUI process may already be using it. "
+ "Use --database-url to specify a separate database file."
+ )
+
+ monkeypatch.setattr(main, "init_db", _raise_file_lock)
+ monkeypatch.setattr(main.args, "enable_assets", False)
+
+ with caplog.at_level(logging.ERROR):
+ result = main.setup_database(None)
+
+ assert result is None
+ assert "Database is locked. Another ComfyUI process is already using this database." in caplog.text
+ assert "Failed to initialize database." not in caplog.text
+
+
+def test_setup_database_exits_for_file_lock_when_assets_are_enabled(monkeypatch, caplog):
+ monkeypatch.setattr(main, "dependencies_available", lambda: True)
+
+ def _raise_file_lock():
+ raise RuntimeError(
+ "Could not acquire lock on database 'x.db'. "
+ "Another ComfyUI process may already be using it. "
+ "Use --database-url to specify a separate database file."
+ )
+
+ monkeypatch.setattr(main, "init_db", _raise_file_lock)
+ monkeypatch.setattr(main.args, "enable_assets", True)
+
+ with caplog.at_level(logging.ERROR), pytest.raises(SystemExit) as error:
+ main.setup_database(None)
+
+ assert error.value.code == 1
+ assert "Database is locked. Another ComfyUI process is already using this database." in caplog.text
+ assert "The --enable-assets flag requires a working database connection." not in caplog.text
+
+
+def test_setup_database_exits_for_driver_lock_when_assets_are_disabled(monkeypatch, caplog):
+ monkeypatch.setattr(main, "dependencies_available", lambda: True)
+
+ def _raise_driver_lock():
+ raise Exception("sqlite3.OperationalError: database is locked")
+
+ monkeypatch.setattr(main, "init_db", _raise_driver_lock)
+ monkeypatch.setattr(main.args, "enable_assets", False)
+
+ with caplog.at_level(logging.ERROR), pytest.raises(SystemExit) as error:
+ main.setup_database(None)
+
+ assert error.value.code == 1
+ assert "Database is locked. Another ComfyUI process is already using this database." in caplog.text
diff --git a/tests-unit/app_test/test_migration_0007.py b/tests-unit/app_test/test_migration_0007.py
new file mode 100644
index 000000000..ae7efd2bd
--- /dev/null
+++ b/tests-unit/app_test/test_migration_0007.py
@@ -0,0 +1,215 @@
+import os
+import sqlite3
+
+import pytest
+from alembic import command
+from alembic.config import Config
+
+_BASELINE_0006 = "0006_add_loader_path"
+
+
+def _make_config(db_path: str) -> Config:
+ root = os.path.join(os.path.dirname(__file__), "../..")
+ cfg = Config(os.path.abspath(os.path.join(root, "alembic.ini")))
+ cfg.set_main_option("script_location", os.path.abspath(os.path.join(root, "alembic_db")))
+ cfg.set_main_option("sqlalchemy.url", f"sqlite:///{db_path}")
+ return cfg
+
+
+def db_at_0006(tmp_path):
+ db_path = str(tmp_path / "test.db")
+ cfg = _make_config(db_path)
+ command.upgrade(cfg, _BASELINE_0006)
+ yield cfg, db_path
+
+
+def test_0007_upgrade_from_0006(db_at_0006):
+ cfg, db_path = db_at_0006
+ command.upgrade(cfg, "head")
+ with sqlite3.connect(db_path) as conn:
+ tables = {r[0] for r in conn.execute("SELECT name FROM sqlite_master WHERE type='table'")}
+ assert "assets" in tables
+ assert "asset_contents" in tables
+ assert "asset_system_state" in tables
+ assert "asset_references" not in tables
+
+
+def test_0007_schema_has_expected_columns(db_at_0006):
+ cfg, db_path = db_at_0006
+ command.upgrade(cfg, "head")
+ with sqlite3.connect(db_path) as conn:
+ asset_cols = {r[1] for r in conn.execute("PRAGMA table_info(assets)")}
+ content_cols = {r[1] for r in conn.execute("PRAGMA table_info(asset_contents)")}
+ assert {"id", "content_id", "name", "loader_path", "updated_at", "last_access_time"} <= asset_cols
+ assert {"id", "hash", "path", "is_missing", "mtime_ns"} <= content_cols
+
+
+def test_0007_downgrade_restores_0006_schema(db_at_0006):
+ cfg, db_path = db_at_0006
+ command.upgrade(cfg, "head")
+ command.downgrade(cfg, _BASELINE_0006)
+ with sqlite3.connect(db_path) as conn:
+ tables = {r[0] for r in conn.execute("SELECT name FROM sqlite_master WHERE type='table'")}
+ assert "asset_references" in tables
+ assert "asset_contents" not in tables
+
+
+def test_0007_invariants_on_migrated_db(db_at_0006):
+ cfg, db_path = db_at_0006
+ command.upgrade(cfg, "head")
+ with sqlite3.connect(db_path) as conn:
+ conn.execute("PRAGMA foreign_keys = ON")
+ conn.execute(
+ "INSERT INTO asset_contents(id, path, is_missing, size_bytes, created_at) "
+ "VALUES ('c1', '/tmp/f1', 0, 0, '2024-01-01')"
+ )
+ conn.execute(
+ "INSERT INTO assets(id, content_id, name, created_at, updated_at) "
+ "VALUES ('a1', 'c1', 'test', '2024-01-01', '2024-01-01')"
+ )
+ conn.commit()
+
+ with pytest.raises(sqlite3.IntegrityError):
+ conn.execute(
+ "INSERT INTO asset_contents(id, path, is_missing, size_bytes, created_at) "
+ "VALUES ('c2', '/tmp/f1', 0, 0, '2024-01-01')"
+ )
+ conn.commit()
+ conn.rollback()
+
+ conn.execute(
+ "INSERT INTO asset_contents(id, path, is_missing, size_bytes, created_at) "
+ "VALUES ('c3', '/tmp/f1', 1, 0, '2024-01-01')"
+ )
+ conn.commit()
+
+ conn.execute("UPDATE asset_contents SET hash='abc123' WHERE id='c1'")
+ conn.execute(
+ "INSERT INTO asset_contents(id, path, hash, is_missing, size_bytes, created_at) "
+ "VALUES ('c4', '/tmp/f2', 'abc123', 0, 0, '2024-01-01')"
+ )
+ conn.commit()
+
+
+def test_0007_orm_parity(db_at_0006, tmp_path):
+ from sqlalchemy import create_engine, inspect
+
+ import app.assets.database.models as asset_models
+ from app.database.models import Base
+
+ cfg, db_path = db_at_0006
+ command.upgrade(cfg, "head")
+ with sqlite3.connect(db_path) as conn:
+ alembic_tables = {
+ r[0]
+ for r in conn.execute(
+ "SELECT name FROM sqlite_master WHERE type='table' "
+ "AND name NOT LIKE 'alembic%' AND name NOT LIKE 'sqlite%'"
+ )
+ }
+
+ alembic_engine = create_engine(f"sqlite:///{db_path}")
+ orm_db = str(tmp_path / "orm.db")
+ engine = create_engine(f"sqlite:///{orm_db}")
+ alembic_inspector = inspect(alembic_engine)
+ Base.metadata.create_all(engine)
+ orm_inspector = inspect(engine)
+ orm_tables = set(orm_inspector.get_table_names())
+ assert asset_models.AssetMeta.__tablename__ == "asset_meta"
+
+ alembic_indexes = {
+ (index["name"], tuple(index["column_names"]))
+ for index in alembic_inspector.get_indexes("asset_meta")
+ }
+ orm_indexes = {
+ (index.name, tuple(index.columns.keys()))
+ for index in Base.metadata.tables["asset_meta"].indexes
+ }
+
+ assert alembic_tables == orm_tables, f"Mismatch: alembic={alembic_tables}, orm={orm_tables}"
+ assert alembic_indexes == orm_indexes, f"Mismatch: alembic={alembic_indexes}, orm={orm_indexes}"
+
+
+def test_0007_downgrade_chain_past_0003_succeeds(db_at_0006):
+ cfg, db_path = db_at_0006
+ command.upgrade(cfg, "head")
+ command.downgrade(cfg, "0001_assets")
+ with sqlite3.connect(db_path) as conn:
+ tables = {r[0] for r in conn.execute("SELECT name FROM sqlite_master WHERE type='table'")}
+ assert "assets_info" in tables
+ assert "asset_references" not in tables
+ assert "asset_contents" not in tables
+
+
+def test_0007_downgrade_restores_legacy_assets_definition(db_at_0006, tmp_path):
+ from sqlalchemy import create_engine, inspect
+
+ cfg, db_path = db_at_0006
+
+ reference_db = str(tmp_path / "reference_0006.db")
+ reference_cfg = _make_config(reference_db)
+ command.upgrade(reference_cfg, _BASELINE_0006)
+
+ command.upgrade(cfg, "head")
+ command.downgrade(cfg, _BASELINE_0006)
+
+ def _assets_shape(path: str):
+ engine = create_engine(f"sqlite:///{path}")
+ try:
+ inspector = inspect(engine)
+ indexes = {
+ (index["name"], tuple(index["column_names"]), bool(index["unique"]))
+ for index in inspector.get_indexes("assets")
+ }
+ checks = {
+ (check["name"], check["sqltext"])
+ for check in inspector.get_check_constraints("assets")
+ }
+ defaults = {
+ column["name"]: column["default"]
+ for column in inspector.get_columns("assets")
+ }
+ return indexes, checks, defaults
+ finally:
+ engine.dispose()
+
+ restored_indexes, restored_checks, restored_defaults = _assets_shape(db_path)
+ expected_indexes, expected_checks, expected_defaults = _assets_shape(reference_db)
+
+ assert restored_indexes == expected_indexes, (
+ f"index drift: restored={restored_indexes}, expected={expected_indexes}"
+ )
+ assert restored_checks == expected_checks, (
+ f"check drift: restored={restored_checks}, expected={expected_checks}"
+ )
+ assert restored_defaults == expected_defaults, (
+ f"default drift: restored={restored_defaults}, expected={expected_defaults}"
+ )
+
+
+def test_0007_downgrade_restores_0006_asset_references_indexes(db_at_0006, tmp_path):
+ from sqlalchemy import create_engine, inspect
+
+ cfg, db_path = db_at_0006
+
+ reference_db = str(tmp_path / "reference_0006.db")
+ reference_cfg = _make_config(reference_db)
+ command.upgrade(reference_cfg, _BASELINE_0006)
+
+ command.upgrade(cfg, "head")
+ command.downgrade(cfg, _BASELINE_0006)
+
+ def _asset_reference_indexes(path: str) -> set[tuple[str, tuple[str, ...]]]:
+ engine = create_engine(f"sqlite:///{path}")
+ try:
+ return {
+ (index["name"], tuple(index["column_names"]))
+ for index in inspect(engine).get_indexes("asset_references")
+ }
+ finally:
+ engine.dispose()
+
+ restored = _asset_reference_indexes(db_path)
+ expected = _asset_reference_indexes(reference_db)
+ assert restored == expected, f"index drift: restored={restored}, expected={expected}"
diff --git a/tests-unit/app_test/test_migration_roundtrip.py b/tests-unit/app_test/test_migration_roundtrip.py
new file mode 100644
index 000000000..838dc69ad
--- /dev/null
+++ b/tests-unit/app_test/test_migration_roundtrip.py
@@ -0,0 +1,56 @@
+
+import os
+import sqlite3
+
+import pytest
+from alembic import command
+from alembic.config import Config
+
+
+def _make_config(db_path: str) -> Config:
+ root = os.path.join(os.path.dirname(__file__), "../..")
+ cfg = Config(os.path.abspath(os.path.join(root, "alembic.ini")))
+ cfg.set_main_option("script_location", os.path.abspath(os.path.join(root, "alembic_db")))
+ cfg.set_main_option("sqlalchemy.url", f"sqlite:///{db_path}")
+ return cfg
+
+
+def _user_tables(db_path: str) -> set[str]:
+ conn = sqlite3.connect(db_path)
+ try:
+ return {
+ row[0]
+ for row in conn.execute(
+ "SELECT name FROM sqlite_master WHERE type='table' "
+ "AND name NOT LIKE 'alembic%' AND name NOT LIKE 'sqlite%'"
+ )
+ }
+ finally:
+ conn.close()
+
+
+def db_at_head(tmp_path):
+ db_path = str(tmp_path / "roundtrip.db")
+ cfg = _make_config(db_path)
+ command.upgrade(cfg, "head")
+ yield cfg, db_path
+
+
+def test_downgrade_to_base_empties_the_schema(db_at_head):
+ cfg, db_path = db_at_head
+ assert _user_tables(db_path), "fixture did not build a schema"
+
+ command.downgrade(cfg, "base")
+
+ assert _user_tables(db_path) == set()
+
+
+def test_upgrade_to_head_again_after_downgrade_to_base(db_at_head):
+ cfg, db_path = db_at_head
+ expected = _user_tables(db_path)
+
+ command.downgrade(cfg, "base")
+ command.upgrade(cfg, "head")
+
+ assert _user_tables(db_path) == expected
diff --git a/tests-unit/app_test/test_migration_warning.py b/tests-unit/app_test/test_migration_warning.py
new file mode 100644
index 000000000..86c1874b5
--- /dev/null
+++ b/tests-unit/app_test/test_migration_warning.py
@@ -0,0 +1,110 @@
+import os
+
+import pytest
+from alembic import command
+from alembic.config import Config
+from alembic.script import ScriptDirectory
+
+import app.database.db as db_module
+
+_BASELINE_0006 = "0006_add_loader_path"
+_BASELINE_0002 = "0002_merge_to_asset_references"
+_DESTRUCTIVE = "0007_record_content_split"
+
+_DISCARDED_CLASSES = (
+ "manual tags",
+ "user metadata",
+ "previews",
+ "renames",
+ "API-created records",
+ "job_id",
+)
+
+
+def _make_config(db_path: str) -> Config:
+ root = os.path.join(os.path.dirname(__file__), "../..")
+ cfg = Config(os.path.abspath(os.path.join(root, "alembic.ini")))
+ cfg.set_main_option("script_location", os.path.abspath(os.path.join(root, "alembic_db")))
+ cfg.set_main_option("sqlalchemy.url", f"sqlite:///{db_path}")
+ return cfg
+
+
+def captured_warnings(monkeypatch):
+ warnings: list[str] = []
+ monkeypatch.setattr(db_module, "log_startup_warning", warnings.append)
+ return warnings
+
+
+def db_url_for(monkeypatch):
+ def _set(db_path: str) -> str:
+ url = f"sqlite:///{db_path}"
+ monkeypatch.setattr(db_module.args, "database_url", url)
+ return url
+
+ return _set
+
+
+def _migrate(db_path: str, db_url: str, db_exists: bool) -> None:
+ db_module._migrate_and_bind(db_url, db_path, db_exists)
+
+
+def test_upgrade_across_the_destructive_revision_warns_and_names_the_backup(
+ tmp_path, captured_warnings, db_url_for
+):
+ db_path = str(tmp_path / "existing.db")
+ command.upgrade(_make_config(db_path), _BASELINE_0006)
+ db_url = db_url_for(db_path)
+ assert os.path.exists(db_path)
+
+ _migrate(db_path, db_url, db_exists=True)
+
+ assert len(captured_warnings) == 1, (
+ "the destructive rebuild is one startup event, not one warning per traversed revision"
+ )
+ message = captured_warnings[0]
+ assert db_path + ".bkp" in message, "the warning must name the backup it left behind"
+ for discarded in _DISCARDED_CLASSES:
+ assert discarded in message, (
+ f"the warning must inventory what the rebuild discarded; {discarded!r} is missing"
+ )
+
+
+def test_fresh_install_warns_nothing(tmp_path, captured_warnings, db_url_for):
+ db_path = str(tmp_path / "fresh.db")
+ db_url = db_url_for(db_path)
+ assert not os.path.exists(db_path)
+
+ _migrate(db_path, db_url, db_exists=False)
+
+ assert captured_warnings == [], (
+ "a fresh install discarded nothing and took no backup, so it has nothing to warn about"
+ )
+
+
+def test_upgrade_that_does_not_cross_the_destructive_revision_warns_nothing(
+ tmp_path, captured_warnings, db_url_for, monkeypatch
+):
+ db_path = str(tmp_path / "partial.db")
+ command.upgrade(_make_config(db_path), _BASELINE_0002)
+ db_url = db_url_for(db_path)
+ monkeypatch.setattr(ScriptDirectory, "get_current_head", lambda _self: _BASELINE_0006)
+
+ _migrate(db_path, db_url, db_exists=True)
+
+ assert captured_warnings == [], (
+ "a multi-revision upgrade that never traverses the rebuild keeps every row it had"
+ )
+
+
+def test_database_already_at_head_warns_nothing(tmp_path, captured_warnings, db_url_for):
+ db_path = str(tmp_path / "current.db")
+ command.upgrade(_make_config(db_path), "head")
+ db_url = db_url_for(db_path)
+
+ _migrate(db_path, db_url, db_exists=True)
+
+ assert captured_warnings == [], (
+ "an up-to-date database migrates nothing, so it must stay silent on every boot"
+ )
diff --git a/tests-unit/assets_test/__init__.py b/tests-unit/assets_test/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/tests-unit/assets_test/conftest.py b/tests-unit/assets_test/conftest.py
index 44416e8c5..387e3cf76 100644
--- a/tests-unit/assets_test/conftest.py
+++ b/tests-unit/assets_test/conftest.py
@@ -13,6 +13,8 @@ from typing import Callable, Iterator, Optional
import pytest
import requests
+from .helpers import assert_hash_fields_consistent
+
def pytest_addoption(parser: pytest.Parser) -> None:
"""
@@ -28,6 +30,18 @@ def pytest_addoption(parser: pytest.Parser) -> None:
default=os.environ.get("ASSETS_TEST_DB_URL"),
help="SQLAlchemy DB URL (e.g. sqlite:///path/to/db.sqlite3)",
)
+ parser.addoption(
+ "--enable-asset-hashing",
+ action="store_true",
+ help="Start the assets subprocess with hash-mode behavior enabled.",
+ )
+
+
+def pytest_configure(config: pytest.Config) -> None:
+ config.addinivalue_line(
+ "markers",
+ "hashing_on: exercises the subprocess harness with --enable-asset-hashing",
+ )
def _free_port() -> int:
@@ -103,8 +117,7 @@ def comfy_url_and_proc(comfy_tmp_base_dir: Path, request: pytest.FixtureRequest)
if not (comfy_root / "main.py").is_file():
raise FileNotFoundError(f"main.py not found under {comfy_root}")
- proc = subprocess.Popen(
- args=[
+ command = [
sys.executable,
"main.py",
f"--base-directory={str(comfy_tmp_base_dir)}",
@@ -115,7 +128,19 @@ def comfy_url_and_proc(comfy_tmp_base_dir: Path, request: pytest.FixtureRequest)
"--port",
str(port),
"--cpu",
- ],
+ ]
+ if (
+ request.config.getoption("--enable-asset-hashing")
+ or "hashing_on" in request.config.getoption("markexpr")
+ or any(
+ item.get_closest_marker("hashing_on")
+ for item in request.session.items
+ )
+ ):
+ command.append("--enable-asset-hashing")
+
+ proc = subprocess.Popen(
+ args=command,
stdout=out_log,
stderr=err_log,
cwd=str(comfy_root),
@@ -190,8 +215,9 @@ def _post_multipart_asset(
@pytest.fixture
def make_asset_bytes() -> Callable[[str, int], bytes]:
# Salt content per test so it never collides with assets left over from
- # earlier tests. Delete is now always a soft delete (content is preserved),
- # so the suite can no longer rely on hard-deleting content for isolation.
+ # earlier tests. Delete hard-deletes the record but preserves content
+ # (content rows and files are untouched), so the suite cannot rely on delete
+ # removing content for isolation.
# Deterministic within a test: the same (name, size) yields the same bytes.
salt = uuid.uuid4().bytes
@@ -236,9 +262,6 @@ def seeded_asset(request: pytest.FixtureRequest, http: requests.Session, api_bas
if tags is None:
tags = ["models", "model_type:checkpoints", "unit-tests", "alpha"]
meta = {"purpose": "test", "epoch": 1, "flags": ["x", "y"], "nullable": None}
- # Unique content per test so the seed always creates a fresh asset (201).
- # Delete is now always a soft delete, so content from a prior test survives
- # and would otherwise dedup this upload into an existing asset (200).
content = uuid.uuid4().bytes + b"A" * (4096 - 16)
files = {"file": (name, content, "application/octet-stream")}
form_data = {
@@ -249,7 +272,6 @@ def seeded_asset(request: pytest.FixtureRequest, http: requests.Session, api_bas
r = http.post(api_base + "/api/assets", files=files, data=form_data, timeout=120)
body = r.json()
assert r.status_code == 201, body
- from helpers import assert_hash_fields_consistent
assert_hash_fields_consistent(body)
return body
diff --git a/tests-unit/assets_test/helpers.py b/tests-unit/assets_test/helpers.py
index ae3de6dc3..06569de65 100644
--- a/tests-unit/assets_test/helpers.py
+++ b/tests-unit/assets_test/helpers.py
@@ -1,7 +1,124 @@
"""Helper functions for assets integration tests."""
+from __future__ import annotations
+
+import json
import time
+import uuid
+from collections.abc import Iterator, Mapping
+from dataclasses import dataclass
+from datetime import datetime, timedelta
+from typing import NotRequired, TypeAlias, TypedDict
+import pytest
import requests
+from aiohttp import web
+from aiohttp.test_utils import make_mocked_request
+from sqlalchemy import Engine, create_engine
+from sqlalchemy.orm import Session
+
+from app.assets.api import routes
+from app.assets.database.models import Asset
+from app.assets.database.queries.records import create_content, create_record
+from app.database.models import Base
+
+
+class AssetItem(TypedDict):
+ id: str
+ name: str
+ preview_id: NotRequired[str]
+
+
+class AssetListBody(TypedDict):
+ assets: list[AssetItem]
+ total: int
+ has_more: bool
+ next_cursor: NotRequired[str]
+
+
+class ErrorItem(TypedDict):
+ code: str
+
+
+class ErrorBody(TypedDict):
+ error: ErrorItem
+
+
+@dataclass(frozen=True, slots=True)
+class RecordSeed:
+ name: str
+ tags: tuple[str, ...] = ()
+ size_bytes: int = 0
+
+
+RouteDatabase: TypeAlias = tuple[Engine, Session]
+
+
[email protected](autouse=True)
+def autoclean_unit_test_assets() -> Iterator[None]:
+ yield
+
+
+def route_database(monkeypatch: pytest.MonkeyPatch) -> Iterator[RouteDatabase]:
+ engine = create_engine("sqlite:///:memory:")
+ Base.metadata.create_all(engine)
+ monkeypatch.setattr(routes, "create_session", lambda: Session(engine))
+ monkeypatch.setattr(routes, "_ASSETS_ENABLED", True)
+ with Session(engine) as session:
+ yield engine, session
+ engine.dispose()
+
+
+def seed_record(session: Session, seed: RecordSeed) -> Asset:
+ content = create_content(
+ session,
+ path=f"/output/{uuid.uuid4()}-{seed.name}",
+ size_bytes=seed.size_bytes,
+ )
+ return create_record(
+ session,
+ content_id=content.id,
+ name=seed.name,
+ mime_type="image/png",
+ tags=seed.tags,
+ )
+
+
+def sortable_record_ids(route_database: RouteDatabase) -> tuple[str, str]:
+ _, session = route_database
+ older = seed_record(session, RecordSeed("z.png", ("sort-case",), 100))
+ newer = seed_record(session, RecordSeed("a.png", ("sort-case",), 200))
+ base_time = datetime(2026, 1, 1)
+ older.created_at = base_time
+ newer.created_at = base_time + timedelta(days=1)
+ newer.updated_at = base_time
+ older.updated_at = base_time + timedelta(days=1)
+ older.last_access_time = base_time
+ newer.last_access_time = base_time + timedelta(days=1)
+ session.commit()
+ return newer.id, older.id
+
+
+async def request_assets(query: str = "") -> web.StreamResponse:
+ suffix = f"?{query}" if query else ""
+ return await routes.list_assets_route(
+ make_mocked_request("GET", f"/api/assets{suffix}")
+ )
+
+
+def asset_list_body(response: web.StreamResponse) -> AssetListBody:
+ assert isinstance(response, web.Response)
+ body = response.body
+ assert isinstance(body, bytes | bytearray)
+ return json.loads(body)
+
+
+def error_body(response: web.StreamResponse) -> ErrorBody:
+ assert isinstance(response, web.Response)
+ body = response.body
+ assert isinstance(body, bytes | bytearray)
+ return json.loads(body)
def trigger_sync_seed_assets(session: requests.Session, base_url: str) -> None:
@@ -28,7 +145,10 @@ def get_asset_filename(asset_hash: str, extension: str) -> str:
return asset_hash.removeprefix("blake3:") + extension
-def assert_hash_fields_consistent(body: dict, expected_hash: str | None = None) -> None:
+def assert_hash_fields_consistent(
+ body: Mapping[str, str | None],
+ expected_hash: str | None = None,
+) -> None:
"""Assert hash and asset_hash invariants on an Asset response.
Both must be present or both absent (so a regression that drops only one
diff --git a/tests-unit/assets_test/queries/conftest.py b/tests-unit/assets_test/queries/conftest.py
index 4ca0e86a9..18b67c159 100644
--- a/tests-unit/assets_test/queries/conftest.py
+++ b/tests-unit/assets_test/queries/conftest.py
@@ -5,6 +5,18 @@ from sqlalchemy.orm import Session
from app.assets.database.models import Base
[email protected](scope="session", autouse=True)
+def assert_asset_metadata_tables():
+ assert set(Base.metadata.tables) == {
+ "assets",
+ "asset_contents",
+ "asset_meta",
+ "asset_tags",
+ "tags",
+ "asset_system_state",
+ }
+
+
@pytest.fixture
def session():
"""In-memory SQLite session for fast unit tests."""
diff --git a/tests-unit/assets_test/queries/test_asset.py b/tests-unit/assets_test/queries/test_asset.py
index 9b7eb4bac..d1cf9b2de 100644
--- a/tests-unit/assets_test/queries/test_asset.py
+++ b/tests-unit/assets_test/queries/test_asset.py
@@ -1,187 +1,20 @@
-import uuid
-
-import pytest
+import sqlalchemy as sa
from sqlalchemy.orm import Session
-from app.assets.helpers import get_utc_now
-from app.assets.database.models import Asset
-from app.assets.database.queries import (
- asset_exists_by_hash,
- get_asset_by_hash,
- upsert_asset,
- bulk_insert_assets,
- update_asset_hash_and_mime,
-)
-
-
-class TestAssetExistsByHash:
- @pytest.mark.parametrize(
- "setup_hash,query_hash,expected",
- [
- (None, "nonexistent", False), # No asset exists
- ("blake3:abc123", "blake3:abc123", True), # Asset exists with matching hash
- (None, "", False), # Null hash in DB doesn't match empty string
- ],
- ids=["nonexistent", "existing", "null_hash_no_match"],
- )
- def test_exists_by_hash(self, session: Session, setup_hash, query_hash, expected):
- if setup_hash is not None or query_hash == "":
- asset = Asset(hash=setup_hash, size_bytes=100)
- session.add(asset)
- session.commit()
-
- assert asset_exists_by_hash(session, asset_hash=query_hash) is expected
-
-
-class TestGetAssetByHash:
- @pytest.mark.parametrize(
- "setup_hash,query_hash,should_find",
- [
- (None, "nonexistent", False),
- ("blake3:def456", "blake3:def456", True),
- ],
- ids=["nonexistent", "existing"],
- )
- def test_get_by_hash(self, session: Session, setup_hash, query_hash, should_find):
- if setup_hash is not None:
- asset = Asset(hash=setup_hash, size_bytes=200, mime_type="image/png")
- session.add(asset)
- session.commit()
-
- result = get_asset_by_hash(session, asset_hash=query_hash)
- if should_find:
- assert result is not None
- assert result.size_bytes == 200
- assert result.mime_type == "image/png"
- else:
- assert result is None
-
-
-class TestUpsertAsset:
- @pytest.mark.parametrize(
- "first_size,first_mime,second_size,second_mime,expect_created,expect_updated,final_size,final_mime",
- [
- # New asset creation
- (None, None, 1024, "application/octet-stream", True, False, 1024, "application/octet-stream"),
- # Existing asset, same values - no update
- (500, "text/plain", 500, "text/plain", False, False, 500, "text/plain"),
- # Existing asset with size 0, update with new values
- (0, None, 2048, "image/png", False, True, 2048, "image/png"),
- # Existing asset, second call with size 0 - no update
- (1000, None, 0, None, False, False, 1000, None),
- ],
- ids=["new_asset", "existing_no_change", "update_from_zero", "zero_size_no_update"],
- )
- def test_upsert_scenarios(
- self,
- session: Session,
- first_size,
- first_mime,
- second_size,
- second_mime,
- expect_created,
- expect_updated,
- final_size,
- final_mime,
- ):
- asset_hash = f"blake3:test_{first_size}_{second_size}"
-
- # First upsert (if first_size is not None, we're testing the second call)
- if first_size is not None:
- upsert_asset(
- session,
- asset_hash=asset_hash,
- size_bytes=first_size,
- mime_type=first_mime,
- )
- session.commit()
-
- # The upsert call we're testing
- asset, created, updated = upsert_asset(
- session,
- asset_hash=asset_hash,
- size_bytes=second_size,
- mime_type=second_mime,
- )
- session.commit()
-
- assert created is expect_created
- assert updated is expect_updated
- assert asset.size_bytes == final_size
- assert asset.mime_type == final_mime
-
-
-class TestBulkInsertAssets:
- def test_inserts_multiple_assets(self, session: Session):
- now = get_utc_now()
- rows = [
- {"id": str(uuid.uuid4()), "hash": "blake3:bulk1", "size_bytes": 100, "mime_type": "text/plain", "created_at": now},
- {"id": str(uuid.uuid4()), "hash": "blake3:bulk2", "size_bytes": 200, "mime_type": "image/png", "created_at": now},
- {"id": str(uuid.uuid4()), "hash": "blake3:bulk3", "size_bytes": 300, "mime_type": None, "created_at": now},
- ]
- bulk_insert_assets(session, rows)
- session.commit()
-
- assets = session.query(Asset).all()
- assert len(assets) == 3
- hashes = {a.hash for a in assets}
- assert hashes == {"blake3:bulk1", "blake3:bulk2", "blake3:bulk3"}
-
- def test_empty_list_is_noop(self, session: Session):
- bulk_insert_assets(session, [])
- session.commit()
- assert session.query(Asset).count() == 0
-
- def test_handles_large_batch(self, session: Session):
- """Test chunking logic with more rows than MAX_BIND_PARAMS allows."""
- now = get_utc_now()
- rows = [
- {"id": str(uuid.uuid4()), "hash": f"blake3:large{i}", "size_bytes": i, "mime_type": None, "created_at": now}
- for i in range(200)
- ]
- bulk_insert_assets(session, rows)
- session.commit()
-
- assert session.query(Asset).count() == 200
+from app.assets.database.models import AssetContent
+from app.assets.database.queries import create_content, create_record
-class TestMimeTypeImmutability:
- """mime_type on Asset is write-once: set on first ingest, never overwritten."""
+def test_hash_is_a_content_fact(session: Session) -> None:
+ content = create_content(session, "/models/a.safetensors", hash="same", size_bytes=12)
+ record = create_record(session, content.id, "a.safetensors")
- @pytest.mark.parametrize(
- "initial_mime,second_mime,expected_mime",
- [
- ("image/png", "image/jpeg", "image/png"),
- (None, "image/png", "image/png"),
- ],
- ids=["preserves_existing", "fills_null"],
- )
- def test_upsert_mime_immutability(self, session: Session, initial_mime, second_mime, expected_mime):
- h = f"blake3:upsert_{initial_mime}_{second_mime}"
- upsert_asset(session, asset_hash=h, size_bytes=100, mime_type=initial_mime)
- session.commit()
+ assert session.scalar(sa.select(AssetContent).where(AssetContent.hash == "same")) == content
+ assert record.content_id == content.id
- asset, created, _ = upsert_asset(session, asset_hash=h, size_bytes=100, mime_type=second_mime)
- assert created is False
- assert asset.mime_type == expected_mime
- @pytest.mark.parametrize(
- "initial_mime,update_mime,update_hash,expected_mime,expected_hash",
- [
- (None, "image/png", None, "image/png", "blake3:upd0"),
- ("image/png", "image/jpeg", None, "image/png", "blake3:upd1"),
- ("image/png", "image/jpeg", "blake3:upd2_new", "image/png", "blake3:upd2_new"),
- ],
- ids=["fills_null", "preserves_existing", "hash_updates_mime_locked"],
- )
- def test_update_asset_hash_and_mime_immutability(
- self, session: Session, initial_mime, update_mime, update_hash, expected_mime, expected_hash,
- ):
- h = expected_hash.removesuffix("_new")
- asset = Asset(hash=h, size_bytes=100, mime_type=initial_mime)
- session.add(asset)
- session.flush()
+def test_equal_hashes_do_not_merge_content_rows(session: Session) -> None:
+ first = create_content(session, "/models/a.safetensors", hash="same")
+ second = create_content(session, "/models/b.safetensors", hash="same")
- update_asset_hash_and_mime(session, asset_id=asset.id, mime_type=update_mime, asset_hash=update_hash)
- assert asset.mime_type == expected_mime
- assert asset.hash == expected_hash
+ assert first.id != second.id
diff --git a/tests-unit/assets_test/queries/test_asset_info.py b/tests-unit/assets_test/queries/test_asset_info.py
index 74dfb8a37..dba67a708 100644
--- a/tests-unit/assets_test/queries/test_asset_info.py
+++ b/tests-unit/assets_test/queries/test_asset_info.py
@@ -1,580 +1,29 @@
-import time
-import uuid
-import pytest
from sqlalchemy.orm import Session
-from app.assets.database.models import Asset, AssetReference, AssetReferenceMeta
from app.assets.database.queries import (
- reference_exists_for_asset_id,
- get_reference_by_id,
- insert_reference,
- get_or_create_reference,
- update_reference_timestamps,
- list_references_page,
- fetch_reference_asset_and_tags,
- fetch_reference_and_asset,
- update_reference_access_time,
- set_reference_metadata,
- delete_reference_by_id,
- set_reference_preview,
- bulk_insert_references_ignore_conflicts,
- get_reference_ids_by_ids,
- ensure_tags_exist,
- add_tags_to_reference,
+ create_content,
+ create_record,
+ delete_record,
+ get_record_by_id,
+ rename_record,
)
-from app.assets.helpers import get_utc_now
-def _make_asset(session: Session, hash_val: str | None = None, size: int = 1024) -> Asset:
- asset = Asset(hash=hash_val, size_bytes=size, mime_type="application/octet-stream")
- session.add(asset)
- session.flush()
- return asset
+def test_record_operations_do_not_mutate_content(session: Session) -> None:
+ content = create_content(session, "/output/result.png", hash="digest", size_bytes=3)
+ record = create_record(session, content.id, "before.png", mime_type="image/png")
+ renamed = rename_record(session, record.id, "after.png")
-def _make_reference(
- session: Session,
- asset: Asset,
- name: str = "test",
- owner_id: str = "",
-) -> AssetReference:
- now = get_utc_now()
- ref = AssetReference(
- owner_id=owner_id,
- name=name,
- asset_id=asset.id,
- created_at=now,
- updated_at=now,
- last_access_time=now,
- )
- session.add(ref)
- session.flush()
- return ref
+ assert renamed.content_id == content.id
+ assert renamed.name == "after.png"
+ assert content.hash == "digest"
-class TestReferenceExistsForAssetId:
- def test_returns_false_when_no_reference(self, session: Session):
- asset = _make_asset(session, "hash1")
- assert reference_exists_for_asset_id(session, asset_id=asset.id) is False
+def test_delete_record_is_hard_delete(session: Session) -> None:
+ content = create_content(session, "/output/result.png")
+ record = create_record(session, content.id, "result.png")
- def test_returns_true_when_reference_exists(self, session: Session):
- asset = _make_asset(session, "hash1")
- _make_reference(session, asset)
- assert reference_exists_for_asset_id(session, asset_id=asset.id) is True
+ delete_record(session, record.id)
-
-class TestGetReferenceById:
- def test_returns_none_for_nonexistent(self, session: Session):
- assert get_reference_by_id(session, reference_id="nonexistent") is None
-
- def test_returns_reference(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset, name="myfile.txt")
-
- result = get_reference_by_id(session, reference_id=ref.id)
- assert result is not None
- assert result.name == "myfile.txt"
-
-
-class TestListReferencesPage:
- def test_empty_db(self, session: Session):
- refs, tag_map, total = list_references_page(session)
- assert refs == []
- assert tag_map == {}
- assert total == 0
-
- def test_returns_references_with_tags(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset, name="test.bin")
- ensure_tags_exist(session, ["alpha", "beta"])
- add_tags_to_reference(session, reference_id=ref.id, tags=["alpha", "beta"])
- session.commit()
-
- refs, tag_map, total = list_references_page(session)
- assert len(refs) == 1
- assert refs[0].id == ref.id
- assert set(tag_map[ref.id]) == {"alpha", "beta"}
- assert total == 1
-
- def test_name_contains_filter(self, session: Session):
- asset = _make_asset(session, "hash1")
- _make_reference(session, asset, name="model_v1.safetensors")
- _make_reference(session, asset, name="config.json")
- session.commit()
-
- refs, _, total = list_references_page(session, name_contains="model")
- assert total == 1
- assert refs[0].name == "model_v1.safetensors"
-
- def test_owner_visibility(self, session: Session):
- asset = _make_asset(session, "hash1")
- _make_reference(session, asset, name="public", owner_id="")
- _make_reference(session, asset, name="private", owner_id="user1")
- session.commit()
-
- # Empty owner sees only public
- refs, _, total = list_references_page(session, owner_id="")
- assert total == 1
- assert refs[0].name == "public"
-
- # Owner sees both
- refs, _, total = list_references_page(session, owner_id="user1")
- assert total == 2
-
- def test_include_tags_filter(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref1 = _make_reference(session, asset, name="tagged")
- _make_reference(session, asset, name="untagged")
- ensure_tags_exist(session, ["wanted"])
- add_tags_to_reference(session, reference_id=ref1.id, tags=["wanted"])
- session.commit()
-
- refs, _, total = list_references_page(session, include_tags=["wanted"])
- assert total == 1
- assert refs[0].name == "tagged"
-
- def test_include_tags_filter_ands_persisted_model_tags(self, session: Session):
- asset = _make_asset(session, "hash-model-tags")
- checkpoint = _make_reference(session, asset, name="checkpoint")
- lora = _make_reference(session, asset, name="lora")
- input_ref = _make_reference(session, asset, name="input")
- ensure_tags_exist(
- session,
- ["models", "model_type:checkpoints", "model_type:loras", "unit-tests"],
- )
- add_tags_to_reference(
- session,
- reference_id=checkpoint.id,
- tags=["models", "model_type:checkpoints", "unit-tests"],
- origin="automatic",
- )
- add_tags_to_reference(
- session,
- reference_id=lora.id,
- tags=["models", "model_type:loras", "unit-tests"],
- origin="automatic",
- )
- add_tags_to_reference(
- session,
- reference_id=input_ref.id,
- tags=["unit-tests"],
- )
- session.commit()
-
- refs, _, total = list_references_page(
- session,
- include_tags=["models", "model_type:checkpoints", "unit-tests"],
- )
-
- assert total == 1
- assert refs[0].id == checkpoint.id
-
- def test_include_tags_filter_preserves_model_type_case(self, session: Session):
- asset = _make_asset(session, "hash-model-case")
- ref = _make_reference(session, asset, name="llm")
- ensure_tags_exist(session, ["models", "model_type:LLM"])
- add_tags_to_reference(
- session,
- reference_id=ref.id,
- tags=["models", "model_type:LLM"],
- origin="automatic",
- )
- session.commit()
-
- refs, _, total = list_references_page(
- session, include_tags=["models", "model_type:LLM"]
- )
- refs_lower, _, total_lower = list_references_page(
- session, include_tags=["models", "model_type:llm"]
- )
-
- assert total == 1
- assert refs[0].id == ref.id
- assert total_lower == 0
- assert refs_lower == []
-
- def test_exclude_tags_filter(self, session: Session):
- asset = _make_asset(session, "hash1")
- _make_reference(session, asset, name="keep")
- ref_exclude = _make_reference(session, asset, name="exclude")
- ensure_tags_exist(session, ["bad"])
- add_tags_to_reference(session, reference_id=ref_exclude.id, tags=["bad"])
- session.commit()
-
- refs, _, total = list_references_page(session, exclude_tags=["bad"])
- assert total == 1
- assert refs[0].name == "keep"
-
- def test_sorting(self, session: Session):
- asset = _make_asset(session, "hash1", size=100)
- asset2 = _make_asset(session, "hash2", size=500)
- _make_reference(session, asset, name="small")
- _make_reference(session, asset2, name="large")
- session.commit()
-
- refs, _, _ = list_references_page(session, sort="size", order="desc")
- assert refs[0].name == "large"
-
- refs, _, _ = list_references_page(session, sort="name", order="asc")
- assert refs[0].name == "large"
-
-
-class TestFetchReferenceAssetAndTags:
- def test_returns_none_for_nonexistent(self, session: Session):
- result = fetch_reference_asset_and_tags(session, "nonexistent")
- assert result is None
-
- def test_returns_tuple(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset, name="test.bin")
- ensure_tags_exist(session, ["tag1"])
- add_tags_to_reference(session, reference_id=ref.id, tags=["tag1"])
- session.commit()
-
- result = fetch_reference_asset_and_tags(session, ref.id)
- assert result is not None
- ret_ref, ret_asset, ret_tags = result
- assert ret_ref.id == ref.id
- assert ret_asset.id == asset.id
- assert ret_tags == ["tag1"]
-
-
-class TestFetchReferenceAndAsset:
- def test_returns_none_for_nonexistent(self, session: Session):
- result = fetch_reference_and_asset(session, reference_id="nonexistent")
- assert result is None
-
- def test_returns_tuple(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset)
- session.commit()
-
- result = fetch_reference_and_asset(session, reference_id=ref.id)
- assert result is not None
- ret_ref, ret_asset = result
- assert ret_ref.id == ref.id
- assert ret_asset.id == asset.id
-
-
-class TestUpdateReferenceAccessTime:
- def test_updates_last_access_time(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset)
- original_time = ref.last_access_time
- session.commit()
-
- import time
- time.sleep(0.01)
-
- update_reference_access_time(session, reference_id=ref.id)
- session.commit()
-
- session.refresh(ref)
- assert ref.last_access_time > original_time
-
-
-class TestDeleteReferenceById:
- def test_deletes_existing(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset)
- session.commit()
-
- result = delete_reference_by_id(session, reference_id=ref.id, owner_id="")
- assert result is True
- assert get_reference_by_id(session, reference_id=ref.id) is None
-
- def test_returns_false_for_nonexistent(self, session: Session):
- result = delete_reference_by_id(session, reference_id="nonexistent", owner_id="")
- assert result is False
-
- def test_respects_owner_visibility(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset, owner_id="user1")
- session.commit()
-
- result = delete_reference_by_id(session, reference_id=ref.id, owner_id="user2")
- assert result is False
- assert get_reference_by_id(session, reference_id=ref.id) is not None
-
-
-class TestSetReferencePreview:
- def test_sets_preview(self, session: Session):
- asset = _make_asset(session, "hash1")
- preview_asset = _make_asset(session, "preview_hash")
- ref = _make_reference(session, asset)
- preview_ref = _make_reference(session, preview_asset, name="preview.png")
- session.commit()
-
- set_reference_preview(session, reference_id=ref.id, preview_reference_id=preview_ref.id)
- session.commit()
-
- session.refresh(ref)
- assert ref.preview_id == preview_ref.id
-
- def test_clears_preview(self, session: Session):
- asset = _make_asset(session, "hash1")
- preview_asset = _make_asset(session, "preview_hash")
- ref = _make_reference(session, asset)
- preview_ref = _make_reference(session, preview_asset, name="preview.png")
- ref.preview_id = preview_ref.id
- session.commit()
-
- set_reference_preview(session, reference_id=ref.id, preview_reference_id=None)
- session.commit()
-
- session.refresh(ref)
- assert ref.preview_id is None
-
- def test_raises_for_nonexistent_reference(self, session: Session):
- with pytest.raises(ValueError, match="not found"):
- set_reference_preview(session, reference_id="nonexistent", preview_reference_id=None)
-
- def test_raises_for_nonexistent_preview(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset)
- session.commit()
-
- with pytest.raises(ValueError, match="Preview AssetReference"):
- set_reference_preview(session, reference_id=ref.id, preview_reference_id="nonexistent")
-
-
-class TestInsertReference:
- def test_creates_new_reference(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = insert_reference(
- session, asset_id=asset.id, owner_id="user1", name="test.bin"
- )
- session.commit()
-
- assert ref is not None
- assert ref.name == "test.bin"
- assert ref.owner_id == "user1"
-
- def test_allows_duplicate_names(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref1 = insert_reference(session, asset_id=asset.id, owner_id="user1", name="dup.bin")
- session.commit()
-
- # Duplicate names are now allowed
- ref2 = insert_reference(
- session, asset_id=asset.id, owner_id="user1", name="dup.bin"
- )
- session.commit()
-
- assert ref1 is not None
- assert ref2 is not None
- assert ref1.id != ref2.id
-
-
-class TestGetOrCreateReference:
- def test_creates_new_reference(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref, created = get_or_create_reference(
- session, asset_id=asset.id, owner_id="user1", name="new.bin"
- )
- session.commit()
-
- assert created is True
- assert ref.name == "new.bin"
-
- def test_always_creates_new_reference(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref1, created1 = get_or_create_reference(
- session, asset_id=asset.id, owner_id="user1", name="existing.bin"
- )
- session.commit()
-
- # Duplicate names are allowed, so always creates new
- ref2, created2 = get_or_create_reference(
- session, asset_id=asset.id, owner_id="user1", name="existing.bin"
- )
- session.commit()
-
- assert created1 is True
- assert created2 is True
- assert ref1.id != ref2.id
-
-
-class TestUpdateReferenceTimestamps:
- def test_updates_timestamps(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset)
- original_updated_at = ref.updated_at
- session.commit()
-
- time.sleep(0.01)
- update_reference_timestamps(session, ref)
- session.commit()
-
- session.refresh(ref)
- assert ref.updated_at > original_updated_at
-
- def test_updates_preview_id(self, session: Session):
- asset = _make_asset(session, "hash1")
- preview_asset = _make_asset(session, "preview_hash")
- ref = _make_reference(session, asset)
- preview_ref = _make_reference(session, preview_asset, name="preview.png")
- session.commit()
-
- update_reference_timestamps(session, ref, preview_id=preview_ref.id)
- session.commit()
-
- session.refresh(ref)
- assert ref.preview_id == preview_ref.id
-
-
-class TestSetReferenceMetadata:
- def test_sets_metadata(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset)
- session.commit()
-
- set_reference_metadata(
- session, reference_id=ref.id, user_metadata={"key": "value"}
- )
- session.commit()
-
- session.refresh(ref)
- assert ref.user_metadata == {"key": "value"}
- # Check metadata table
- meta = session.query(AssetReferenceMeta).filter_by(asset_reference_id=ref.id).all()
- assert len(meta) == 1
- assert meta[0].key == "key"
- assert meta[0].val_str == "value"
-
- def test_replaces_existing_metadata(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset)
- session.commit()
-
- set_reference_metadata(
- session, reference_id=ref.id, user_metadata={"old": "data"}
- )
- session.commit()
-
- set_reference_metadata(
- session, reference_id=ref.id, user_metadata={"new": "data"}
- )
- session.commit()
-
- meta = session.query(AssetReferenceMeta).filter_by(asset_reference_id=ref.id).all()
- assert len(meta) == 1
- assert meta[0].key == "new"
-
- def test_clears_metadata_with_empty_dict(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset)
- session.commit()
-
- set_reference_metadata(
- session, reference_id=ref.id, user_metadata={"key": "value"}
- )
- session.commit()
-
- set_reference_metadata(
- session, reference_id=ref.id, user_metadata={}
- )
- session.commit()
-
- session.refresh(ref)
- assert ref.user_metadata == {}
- meta = session.query(AssetReferenceMeta).filter_by(asset_reference_id=ref.id).all()
- assert len(meta) == 0
-
- def test_raises_for_nonexistent(self, session: Session):
- with pytest.raises(ValueError, match="not found"):
- set_reference_metadata(
- session, reference_id="nonexistent", user_metadata={"key": "value"}
- )
-
-
-class TestBulkInsertReferencesIgnoreConflicts:
- def test_inserts_multiple_references(self, session: Session):
- asset = _make_asset(session, "hash1")
- now = get_utc_now()
- rows = [
- {
- "id": str(uuid.uuid4()),
- "owner_id": "",
- "name": "bulk1.bin",
- "asset_id": asset.id,
- "preview_id": None,
- "user_metadata": {},
- "created_at": now,
- "updated_at": now,
- "last_access_time": now,
- },
- {
- "id": str(uuid.uuid4()),
- "owner_id": "",
- "name": "bulk2.bin",
- "asset_id": asset.id,
- "preview_id": None,
- "user_metadata": {},
- "created_at": now,
- "updated_at": now,
- "last_access_time": now,
- },
- ]
- bulk_insert_references_ignore_conflicts(session, rows)
- session.commit()
-
- refs = session.query(AssetReference).all()
- assert len(refs) == 2
-
- def test_allows_duplicate_names(self, session: Session):
- asset = _make_asset(session, "hash1")
- _make_reference(session, asset, name="existing.bin", owner_id="")
- session.commit()
-
- now = get_utc_now()
- rows = [
- {
- "id": str(uuid.uuid4()),
- "owner_id": "",
- "name": "existing.bin",
- "asset_id": asset.id,
- "preview_id": None,
- "user_metadata": {},
- "created_at": now,
- "updated_at": now,
- "last_access_time": now,
- },
- {
- "id": str(uuid.uuid4()),
- "owner_id": "",
- "name": "new.bin",
- "asset_id": asset.id,
- "preview_id": None,
- "user_metadata": {},
- "created_at": now,
- "updated_at": now,
- "last_access_time": now,
- },
- ]
- bulk_insert_references_ignore_conflicts(session, rows)
- session.commit()
-
- # Duplicate names allowed, so all 3 rows exist
- refs = session.query(AssetReference).all()
- assert len(refs) == 3
-
- def test_empty_list_is_noop(self, session: Session):
- bulk_insert_references_ignore_conflicts(session, [])
- assert session.query(AssetReference).count() == 0
-
-
-class TestGetReferenceIdsByIds:
- def test_returns_existing_ids(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref1 = _make_reference(session, asset, name="a.bin")
- ref2 = _make_reference(session, asset, name="b.bin")
- session.commit()
-
- found = get_reference_ids_by_ids(session, [ref1.id, ref2.id, "nonexistent"])
-
- assert found == {ref1.id, ref2.id}
-
- def test_empty_list_returns_empty(self, session: Session):
- found = get_reference_ids_by_ids(session, [])
- assert found == set()
+ assert get_record_by_id(session, record.id) is None
diff --git a/tests-unit/assets_test/queries/test_asset_reference_keyset.py b/tests-unit/assets_test/queries/test_asset_reference_keyset.py
index d143d60f9..b3d2c42cd 100644
--- a/tests-unit/assets_test/queries/test_asset_reference_keyset.py
+++ b/tests-unit/assets_test/queries/test_asset_reference_keyset.py
@@ -1,112 +1,33 @@
-"""Keyset-pagination tiebreaker tests for list_references_page.
-
-When multiple rows share the same primary sort value (e.g. four assets
-created in the same microsecond), the secondary `ORDER BY id` is what keeps
-keyset pagination from losing or repeating rows. This file exercises that
-branch directly against an in-memory SQLite session — engineering identical
-timestamps via HTTP is unreliable enough that we work at the query layer.
-"""
-import uuid
-from datetime import datetime
-
-import pytest
from sqlalchemy.orm import Session
-from app.assets.database.models import Asset, AssetReference
-from app.assets.database.queries.asset_reference import list_references_page
+from app.assets.database.queries import create_content, create_record, list_records_page
+from app.assets.database.queries.records import RecordCursorBoundary, RecordPageSpec
-def _make_ref(session: Session, created_at: datetime, name: str, owner: str = "") -> AssetReference:
- asset = Asset(hash=f"blake3:{uuid.uuid4().hex}", size_bytes=1024)
- session.add(asset)
+def test_record_keyset_cursor_pages_in_creation_order(session: Session) -> None:
+ records = [
+ create_record(session, create_content(session, f"/output/{name}").id, name)
+ for name in ("one.png", "two.png", "three.png")
+ ]
+ for index, record in enumerate(records, start=1):
+ record.id = f"00000000-0000-0000-0000-{index:012d}"
session.flush()
- ref = AssetReference(
- id=str(uuid.uuid4()),
- asset_id=asset.id,
- owner_id=owner,
- name=name,
- file_path=f"/tmp/{name}",
- created_at=created_at,
- updated_at=created_at,
- last_access_time=created_at,
- is_missing=False,
- )
- session.add(ref)
- return ref
-
-
[email protected]("order", ["desc", "asc"])
-def test_tiebreaker_walks_duplicate_sort_values(session: Session, order: str):
- """Four rows with the SAME created_at must paginate cleanly under cursor
- mode — no row dropped, no row repeated, despite the primary sort column
- being non-discriminating.
- """
- shared_ts = datetime(2024, 5, 20, 12, 0, 0) # naive UTC, like the DB stores
- refs = [_make_ref(session, shared_ts, f"tie_{i}.png") for i in range(4)]
- session.commit()
-
- expected_ids = sorted([r.id for r in refs], reverse=(order == "desc"))
- # Walk the cursor by hand: page size 2, take 3 pages (2 + 2 + 0).
- seen: list[str] = []
- after_value = None
- after_id = None
- for _ in range(4): # generous loop bound; ought to be 2 iterations
- page, _tag_map, _total = list_references_page(
- session,
- limit=2,
- sort="created_at",
- order=order,
- after_cursor_value=after_value,
- after_cursor_id=after_id,
- )
- if not page:
- break
- seen.extend(p.id for p in page)
- # Use the last row's (created_at, id) as the next cursor input.
- last = page[-1]
- after_value, after_id = last.created_at, last.id
- if len(page) < 2:
- break
-
- assert seen == expected_ids, (
- f"keyset tiebreaker failed for order={order}: expected {expected_ids}, got {seen}"
+ first_page, _, _ = list_records_page(
+ session,
+ RecordPageSpec(limit=2, order="asc"),
)
-
-
-def test_tiebreaker_no_duplicates_under_mixed_collisions(session: Session):
- """Some rows share a timestamp, some don't. The cursor must still walk
- every row exactly once regardless of where ties sit relative to a
- page boundary."""
- t1 = datetime(2024, 5, 20, 12, 0, 0)
- t2 = datetime(2024, 5, 20, 12, 0, 1)
- layout = [t1, t1, t1, t2, t2] # three rows at t1, two at t2
- refs = [_make_ref(session, ts, f"mix_{i}.png") for i, ts in enumerate(layout)]
- session.commit()
-
- all_ids = {r.id for r in refs}
- seen_set: set[str] = set()
- seen_list: list[str] = []
- after_value = None
- after_id = None
- for _ in range(6):
- page, _, _ = list_references_page(
- session,
+ boundary_record = first_page[-1]
+ second_page, _, _ = list_records_page(
+ session,
+ RecordPageSpec(
limit=2,
- sort="created_at",
- order="desc",
- after_cursor_value=after_value,
- after_cursor_id=after_id,
- )
- if not page:
- break
- for p in page:
- assert p.id not in seen_set, f"duplicate row {p.id} appeared in cursor walk"
- seen_set.add(p.id)
- seen_list.append(p.id)
- last = page[-1]
- after_value, after_id = last.created_at, last.id
- if len(page) < 2:
- break
+ order="asc",
+ after=RecordCursorBoundary(
+ value=boundary_record.created_at,
+ id=boundary_record.id,
+ ),
+ ),
+ )
- assert seen_set == all_ids, f"missing rows: expected {all_ids}, got {seen_set}"
+ assert [record.id for record in first_page + second_page] == [record.id for record in records]
diff --git a/tests-unit/assets_test/queries/test_cache_state.py b/tests-unit/assets_test/queries/test_cache_state.py
index 49d0e8d4c..fdd53c5c4 100644
--- a/tests-unit/assets_test/queries/test_cache_state.py
+++ b/tests-unit/assets_test/queries/test_cache_state.py
@@ -1,532 +1,32 @@
-"""Tests for cache_state (AssetReference file path) query functions."""
-import pytest
from sqlalchemy.orm import Session
-from app.assets.database.models import Asset, AssetReference
from app.assets.database.queries import (
- list_references_by_asset_id,
- upsert_reference,
- get_unreferenced_unhashed_asset_ids,
- delete_assets_by_ids,
- get_references_for_prefixes,
- bulk_update_needs_verify,
- delete_references_by_ids,
- delete_orphaned_seed_asset,
- bulk_insert_references_ignore_conflicts,
- get_references_by_paths_and_asset_ids,
- mark_references_missing_outside_prefixes,
- restore_references_by_paths,
+ create_content,
+ create_record,
+ fetch_record_tags,
+ mark_content_missing,
+ unset_content_missing,
)
-from app.assets.helpers import select_best_live_path, get_utc_now
-def _make_asset(session: Session, hash_val: str | None = None, size: int = 1024) -> Asset:
- asset = Asset(hash=hash_val, size_bytes=size)
- session.add(asset)
- session.flush()
- return asset
+def test_missing_state_lives_on_content_and_projects_to_records(session: Session) -> None:
+ content = create_content(session, "/models/model.safetensors")
+ first = create_record(session, content.id, "model-a")
+ second = create_record(session, content.id, "model-b")
+ mark_content_missing(session, content.id)
-def _make_reference(
- session: Session,
- asset: Asset,
- file_path: str,
- name: str = "test",
- mtime_ns: int | None = None,
- needs_verify: bool = False,
-) -> AssetReference:
- now = get_utc_now()
- ref = AssetReference(
- asset_id=asset.id,
- file_path=file_path,
- name=name,
- mtime_ns=mtime_ns,
- needs_verify=needs_verify,
- created_at=now,
- updated_at=now,
- last_access_time=now,
- )
- session.add(ref)
- session.flush()
- return ref
+ assert content.is_missing is True
+ assert fetch_record_tags(session, first.id) == ["missing"]
+ assert fetch_record_tags(session, second.id) == ["missing"]
-class TestListReferencesByAssetId:
- def test_returns_empty_for_no_references(self, session: Session):
- asset = _make_asset(session, "hash1")
- refs = list_references_by_asset_id(session, asset_id=asset.id)
- assert list(refs) == []
+def test_recovery_clears_the_automatic_missing_marker(session: Session) -> None:
+ content = create_content(session, "/models/model.safetensors")
+ record = create_record(session, content.id, "model")
+ mark_content_missing(session, content.id)
- def test_returns_references_for_asset(self, session: Session):
- asset = _make_asset(session, "hash1")
- _make_reference(session, asset, "/path/a.bin", name="a")
- _make_reference(session, asset, "/path/b.bin", name="b")
- session.commit()
+ unset_content_missing(session, content.id)
- refs = list_references_by_asset_id(session, asset_id=asset.id)
- paths = [r.file_path for r in refs]
- assert set(paths) == {"/path/a.bin", "/path/b.bin"}
-
- def test_does_not_return_other_assets_references(self, session: Session):
- asset1 = _make_asset(session, "hash1")
- asset2 = _make_asset(session, "hash2")
- _make_reference(session, asset1, "/path/asset1.bin", name="a1")
- _make_reference(session, asset2, "/path/asset2.bin", name="a2")
- session.commit()
-
- refs = list_references_by_asset_id(session, asset_id=asset1.id)
- paths = [r.file_path for r in refs]
- assert paths == ["/path/asset1.bin"]
-
-
-class TestSelectBestLivePath:
- def test_returns_empty_for_empty_list(self):
- result = select_best_live_path([])
- assert result == ""
-
- def test_returns_empty_when_no_files_exist(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset, "/nonexistent/path.bin")
- session.commit()
-
- result = select_best_live_path([ref])
- assert result == ""
-
- def test_prefers_verified_path(self, session: Session, tmp_path):
- """needs_verify=False should be preferred."""
- asset = _make_asset(session, "hash1")
-
- verified_file = tmp_path / "verified.bin"
- verified_file.write_bytes(b"data")
-
- unverified_file = tmp_path / "unverified.bin"
- unverified_file.write_bytes(b"data")
-
- ref_verified = _make_reference(
- session, asset, str(verified_file), name="verified", needs_verify=False
- )
- ref_unverified = _make_reference(
- session, asset, str(unverified_file), name="unverified", needs_verify=True
- )
- session.commit()
-
- refs = [ref_unverified, ref_verified]
- result = select_best_live_path(refs)
- assert result == str(verified_file)
-
- def test_falls_back_to_existing_unverified(self, session: Session, tmp_path):
- """If all references need verification, return first existing path."""
- asset = _make_asset(session, "hash1")
-
- existing_file = tmp_path / "exists.bin"
- existing_file.write_bytes(b"data")
-
- ref = _make_reference(session, asset, str(existing_file), needs_verify=True)
- session.commit()
-
- result = select_best_live_path([ref])
- assert result == str(existing_file)
-
-
-class TestSelectBestLivePathWithMocking:
- def test_handles_missing_file_path_attr(self):
- """Gracefully handle references with None file_path."""
-
- class MockRef:
- file_path = None
- needs_verify = False
-
- result = select_best_live_path([MockRef()])
- assert result == ""
-
-
-class TestUpsertReference:
- @pytest.mark.parametrize(
- "initial_mtime,second_mtime,expect_created,expect_updated,final_mtime",
- [
- # New reference creation
- (None, 12345, True, False, 12345),
- # Existing reference, same mtime - no update
- (100, 100, False, False, 100),
- # Existing reference, different mtime - update
- (100, 200, False, True, 200),
- ],
- ids=["new_reference", "existing_no_change", "existing_update_mtime"],
- )
- def test_upsert_scenarios(
- self, session: Session, initial_mtime, second_mtime, expect_created, expect_updated, final_mtime
- ):
- asset = _make_asset(session, "hash1")
- file_path = f"/path_{initial_mtime}_{second_mtime}.bin"
- name = f"file_{initial_mtime}_{second_mtime}"
-
- # Create initial reference if needed
- if initial_mtime is not None:
- upsert_reference(session, asset_id=asset.id, file_path=file_path, name=name, mtime_ns=initial_mtime)
- session.commit()
-
- # The upsert call we're testing
- created, updated = upsert_reference(
- session, asset_id=asset.id, file_path=file_path, name=name, mtime_ns=second_mtime
- )
- session.commit()
-
- assert created is expect_created
- assert updated is expect_updated
- ref = session.query(AssetReference).filter_by(file_path=file_path).one()
- assert ref.mtime_ns == final_mtime
-
- def test_upsert_refreshes_loader_path_on_existing_reference(self, session: Session):
- """Re-ingesting an existing reference writes the loader_path computed
- by that ingest, healing NULL or stale values even when nothing else
- about the row changed."""
- asset = _make_asset(session, "hash1")
- file_path = "/models/checkpoints/sub/model.safetensors"
-
- upsert_reference(
- session, asset_id=asset.id, file_path=file_path, name="model",
- mtime_ns=100, loader_path=None,
- )
- session.commit()
-
- created, updated = upsert_reference(
- session, asset_id=asset.id, file_path=file_path, name="model",
- mtime_ns=100, loader_path="sub/model.safetensors",
- )
- session.commit()
-
- assert created is False
- assert updated is True
- ref = session.query(AssetReference).filter_by(file_path=file_path).one()
- assert ref.loader_path == "sub/model.safetensors"
-
- # Identical loader_path is a no-op, not a spurious update.
- created, updated = upsert_reference(
- session, asset_id=asset.id, file_path=file_path, name="model",
- mtime_ns=100, loader_path="sub/model.safetensors",
- )
- session.commit()
- assert created is False
- assert updated is False
-
- def test_upsert_restores_missing_reference(self, session: Session):
- """Upserting a reference that was marked missing should restore it."""
- asset = _make_asset(session, "hash1")
- file_path = "/restored/file.bin"
-
- ref = _make_reference(session, asset, file_path, mtime_ns=100)
- ref.is_missing = True
- session.commit()
-
- created, updated = upsert_reference(
- session, asset_id=asset.id, file_path=file_path, name="restored", mtime_ns=100
- )
- session.commit()
-
- assert created is False
- assert updated is True
- restored_ref = session.query(AssetReference).filter_by(file_path=file_path).one()
- assert restored_ref.is_missing is False
-
-
-class TestRestoreReferencesByPaths:
- def test_restores_missing_references(self, session: Session):
- asset = _make_asset(session, "hash1")
- missing_path = "/missing/file.bin"
- active_path = "/active/file.bin"
-
- missing_ref = _make_reference(session, asset, missing_path, name="missing")
- missing_ref.is_missing = True
- _make_reference(session, asset, active_path, name="active")
- session.commit()
-
- restored = restore_references_by_paths(session, [missing_path])
- session.commit()
-
- assert restored == 1
- ref = session.query(AssetReference).filter_by(file_path=missing_path).one()
- assert ref.is_missing is False
-
- def test_empty_list_restores_nothing(self, session: Session):
- restored = restore_references_by_paths(session, [])
- assert restored == 0
-
-
-class TestMarkReferencesMissingOutsidePrefixes:
- def test_marks_references_missing_outside_prefixes(self, session: Session, tmp_path):
- asset = _make_asset(session, "hash1")
- valid_dir = tmp_path / "valid"
- valid_dir.mkdir()
- invalid_dir = tmp_path / "invalid"
- invalid_dir.mkdir()
-
- valid_path = str(valid_dir / "file.bin")
- invalid_path = str(invalid_dir / "file.bin")
-
- _make_reference(session, asset, valid_path, name="valid")
- _make_reference(session, asset, invalid_path, name="invalid")
- session.commit()
-
- marked = mark_references_missing_outside_prefixes(session, [str(valid_dir)])
- session.commit()
-
- assert marked == 1
- all_refs = session.query(AssetReference).all()
- assert len(all_refs) == 2
-
- valid_ref = next(r for r in all_refs if r.file_path == valid_path)
- invalid_ref = next(r for r in all_refs if r.file_path == invalid_path)
- assert valid_ref.is_missing is False
- assert invalid_ref.is_missing is True
-
- def test_empty_prefixes_marks_nothing(self, session: Session):
- asset = _make_asset(session, "hash1")
- _make_reference(session, asset, "/some/path.bin")
- session.commit()
-
- marked = mark_references_missing_outside_prefixes(session, [])
-
- assert marked == 0
-
-
-class TestGetUnreferencedUnhashedAssetIds:
- def test_returns_unreferenced_unhashed_assets(self, session: Session):
- # Unhashed asset (hash=None) with no references (no file_path)
- no_refs = _make_asset(session, hash_val=None)
- # Unhashed asset with active reference (not unreferenced)
- with_active_ref = _make_asset(session, hash_val=None)
- _make_reference(session, with_active_ref, "/has/ref.bin", name="has_ref")
- # Unhashed asset with only missing reference (should be unreferenced)
- with_missing_ref = _make_asset(session, hash_val=None)
- missing_ref = _make_reference(session, with_missing_ref, "/missing/ref.bin", name="missing_ref")
- missing_ref.is_missing = True
- # Regular asset (hash not None) - should not be returned
- _make_asset(session, hash_val="blake3:regular")
- session.commit()
-
- unreferenced = get_unreferenced_unhashed_asset_ids(session)
-
- assert no_refs.id in unreferenced
- assert with_missing_ref.id in unreferenced
- assert with_active_ref.id not in unreferenced
-
-
-class TestDeleteAssetsByIds:
- def test_deletes_assets_and_references(self, session: Session):
- asset = _make_asset(session, "hash1")
- _make_reference(session, asset, "/test/path.bin", name="test")
- session.commit()
-
- deleted = delete_assets_by_ids(session, [asset.id])
- session.commit()
-
- assert deleted == 1
- assert session.query(Asset).count() == 0
- assert session.query(AssetReference).count() == 0
-
- def test_empty_list_deletes_nothing(self, session: Session):
- _make_asset(session, "hash1")
- session.commit()
-
- deleted = delete_assets_by_ids(session, [])
-
- assert deleted == 0
- assert session.query(Asset).count() == 1
-
-
-class TestGetReferencesForPrefixes:
- def test_returns_references_matching_prefix(self, session: Session, tmp_path):
- asset = _make_asset(session, "hash1")
- dir1 = tmp_path / "dir1"
- dir1.mkdir()
- dir2 = tmp_path / "dir2"
- dir2.mkdir()
-
- path1 = str(dir1 / "file.bin")
- path2 = str(dir2 / "file.bin")
-
- _make_reference(session, asset, path1, name="file1", mtime_ns=100)
- _make_reference(session, asset, path2, name="file2", mtime_ns=200)
- session.commit()
-
- rows = get_references_for_prefixes(session, [str(dir1)])
-
- assert len(rows) == 1
- assert rows[0].file_path == path1
-
- def test_empty_prefixes_returns_empty(self, session: Session):
- asset = _make_asset(session, "hash1")
- _make_reference(session, asset, "/some/path.bin")
- session.commit()
-
- rows = get_references_for_prefixes(session, [])
-
- assert rows == []
-
-
-class TestBulkSetNeedsVerify:
- def test_sets_needs_verify_flag(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref1 = _make_reference(session, asset, "/path1.bin", needs_verify=False)
- ref2 = _make_reference(session, asset, "/path2.bin", needs_verify=False)
- session.commit()
-
- updated = bulk_update_needs_verify(session, [ref1.id, ref2.id], True)
- session.commit()
-
- assert updated == 2
- session.refresh(ref1)
- session.refresh(ref2)
- assert ref1.needs_verify is True
- assert ref2.needs_verify is True
-
- def test_empty_list_updates_nothing(self, session: Session):
- updated = bulk_update_needs_verify(session, [], True)
- assert updated == 0
-
-
-class TestDeleteReferencesByIds:
- def test_deletes_references_by_id(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref1 = _make_reference(session, asset, "/path1.bin")
- _make_reference(session, asset, "/path2.bin")
- session.commit()
-
- deleted = delete_references_by_ids(session, [ref1.id])
- session.commit()
-
- assert deleted == 1
- assert session.query(AssetReference).count() == 1
-
- def test_empty_list_deletes_nothing(self, session: Session):
- deleted = delete_references_by_ids(session, [])
- assert deleted == 0
-
-
-class TestDeleteOrphanedSeedAsset:
- @pytest.mark.parametrize(
- "create_asset,expected_deleted,expected_count",
- [
- (True, True, 0), # Existing asset gets deleted
- (False, False, 0), # Nonexistent returns False
- ],
- ids=["deletes_existing", "nonexistent_returns_false"],
- )
- def test_delete_orphaned_seed_asset(
- self, session: Session, create_asset, expected_deleted, expected_count
- ):
- asset_id = "nonexistent-id"
- if create_asset:
- asset = _make_asset(session, hash_val=None)
- asset_id = asset.id
- _make_reference(session, asset, "/test/path.bin", name="test")
- session.commit()
-
- deleted = delete_orphaned_seed_asset(session, asset_id)
- if create_asset:
- session.commit()
-
- assert deleted is expected_deleted
- assert session.query(Asset).count() == expected_count
-
-
-class TestBulkInsertReferencesIgnoreConflicts:
- def test_inserts_multiple_references(self, session: Session):
- asset = _make_asset(session, "hash1")
- now = get_utc_now()
- rows = [
- {
- "asset_id": asset.id,
- "file_path": "/bulk1.bin",
- "name": "bulk1",
- "mtime_ns": 100,
- "created_at": now,
- "updated_at": now,
- "last_access_time": now,
- },
- {
- "asset_id": asset.id,
- "file_path": "/bulk2.bin",
- "name": "bulk2",
- "mtime_ns": 200,
- "created_at": now,
- "updated_at": now,
- "last_access_time": now,
- },
- ]
- bulk_insert_references_ignore_conflicts(session, rows)
- session.commit()
-
- assert session.query(AssetReference).count() == 2
-
- def test_ignores_conflicts(self, session: Session):
- asset = _make_asset(session, "hash1")
- _make_reference(session, asset, "/existing.bin", mtime_ns=100)
- session.commit()
-
- now = get_utc_now()
- rows = [
- {
- "asset_id": asset.id,
- "file_path": "/existing.bin",
- "name": "existing",
- "mtime_ns": 999,
- "created_at": now,
- "updated_at": now,
- "last_access_time": now,
- },
- {
- "asset_id": asset.id,
- "file_path": "/new.bin",
- "name": "new",
- "mtime_ns": 200,
- "created_at": now,
- "updated_at": now,
- "last_access_time": now,
- },
- ]
- bulk_insert_references_ignore_conflicts(session, rows)
- session.commit()
-
- assert session.query(AssetReference).count() == 2
- existing = session.query(AssetReference).filter_by(file_path="/existing.bin").one()
- assert existing.mtime_ns == 100 # Original value preserved
-
- def test_empty_list_is_noop(self, session: Session):
- bulk_insert_references_ignore_conflicts(session, [])
- assert session.query(AssetReference).count() == 0
-
-
-class TestGetReferencesByPathsAndAssetIds:
- def test_returns_matching_paths(self, session: Session):
- asset1 = _make_asset(session, "hash1")
- asset2 = _make_asset(session, "hash2")
-
- _make_reference(session, asset1, "/path1.bin")
- _make_reference(session, asset2, "/path2.bin")
- session.commit()
-
- path_to_asset = {
- "/path1.bin": asset1.id,
- "/path2.bin": asset2.id,
- }
- winners = get_references_by_paths_and_asset_ids(session, path_to_asset)
-
- assert winners == {"/path1.bin", "/path2.bin"}
-
- def test_excludes_non_matching_asset_ids(self, session: Session):
- asset1 = _make_asset(session, "hash1")
- asset2 = _make_asset(session, "hash2")
-
- _make_reference(session, asset1, "/path1.bin")
- session.commit()
-
- # Path exists but with different asset_id
- path_to_asset = {"/path1.bin": asset2.id}
- winners = get_references_by_paths_and_asset_ids(session, path_to_asset)
-
- assert winners == set()
-
- def test_empty_dict_returns_empty(self, session: Session):
- winners = get_references_by_paths_and_asset_ids(session, {})
- assert winners == set()
+ assert content.is_missing is False
+ assert fetch_record_tags(session, record.id) == []
diff --git a/tests-unit/assets_test/queries/test_lookup.py b/tests-unit/assets_test/queries/test_lookup.py
new file mode 100644
index 000000000..805100b65
--- /dev/null
+++ b/tests-unit/assets_test/queries/test_lookup.py
@@ -0,0 +1,171 @@
+import os
+from datetime import datetime
+from unittest.mock import patch
+
+import pytest
+from sqlalchemy import create_engine, update
+from sqlalchemy.orm import Session
+
+import app.assets.mode as mode_module
+from app.assets.database.models import AssetContent
+from app.assets.database.queries.records import create_content
+from app.assets.services.lookup import (
+ claim_qualified_content,
+ is_temp_path as _is_temp_path,
+ lookup_for_from_hash,
+ lookup_for_view,
+ refresh_qualified_content,
+)
+from app.database.models import Base
+
+
+def session(tmp_path):
+ engine = create_engine(
+ f"sqlite:///{tmp_path}/test.db", connect_args={"check_same_thread": False}
+ )
+ Base.metadata.create_all(engine)
+ with Session(engine) as sess:
+ yield sess
+
+
[email protected](autouse=True)
+def enable_hashing():
+ class FakeArgs:
+ enable_asset_hashing = True
+
+ mode_module.init(FakeArgs())
+ yield
+ mode_module.init(None)
+
+
+def _make_file(tmp_path, name: str, content: bytes = b"bytes") -> str:
+ p = tmp_path / name
+ p.write_bytes(content)
+ return str(p)
+
+
+def test_temp_only_match_from_hash_returns_none(session, tmp_path):
+ f = _make_file(tmp_path, "f.png")
+ create_content(session, path=f, hash="abc123")
+ session.commit()
+ with patch("app.assets.services.lookup.is_temp_path", return_value=True):
+ result = lookup_for_from_hash(session, "abc123")
+ assert result is None
+
+
+def test_temp_only_match_view_returns_none(session, tmp_path):
+ f = _make_file(tmp_path, "f2.png")
+ create_content(session, path=f, hash="abc123")
+ session.commit()
+ with patch("app.assets.services.lookup.is_temp_path", return_value=True):
+ result = lookup_for_view(session, "abc123")
+ assert result is None
+
+
+def test_sibling_prefix_not_temp(tmp_path):
+ with patch("folder_paths.get_temp_directory", return_value=str(tmp_path / "temp")):
+ assert not _is_temp_path(str(tmp_path / "temp-other" / "f.png"))
+ assert _is_temp_path(str(tmp_path / "temp" / "f.png"))
+
+
+def test_off_mode_from_hash_returns_none(session, tmp_path):
+ class FakeArgs:
+ enable_asset_hashing = False
+
+ mode_module.init(FakeArgs())
+ f = _make_file(tmp_path, "f3.png")
+ create_content(session, path=f, hash="abc123")
+ session.commit()
+ result = lookup_for_from_hash(session, "abc123")
+ assert result is None
+
+
+def test_upload_content_lookup_not_gated_on_hashing_flag(session, tmp_path):
+ class FakeArgs:
+ enable_asset_hashing = False
+
+ mode_module.init(FakeArgs())
+ f = _make_file(tmp_path, "f4.png")
+ content = create_content(session, path=f, hash="abc123", size_bytes=os.path.getsize(f))
+ session.commit()
+ result = lookup_for_view(session, "abc123")
+ assert result is not None
+ assert result.id == content.id
+
+
+def test_stale_older_newer_live_returns_newer(session, tmp_path):
+ f_newer = _make_file(tmp_path, "newer.png")
+ old_time = datetime(2020, 1, 1)
+ new_time = datetime(2024, 1, 1)
+ c_old = create_content(session, path="/nonexistent/old.png", hash="xyz")
+ c_new = create_content(session, path=f_newer, hash="xyz", size_bytes=os.path.getsize(f_newer))
+ session.execute(update(AssetContent).where(AssetContent.id == c_old.id).values(created_at=old_time))
+ session.execute(update(AssetContent).where(AssetContent.id == c_new.id).values(created_at=new_time))
+ session.commit()
+
+ result = lookup_for_from_hash(session, "xyz")
+ assert result is not None
+ assert result.id == c_new.id
+
+
+def test_claim_qualified_content_true_for_a_live_matching_row(session, tmp_path):
+ f = _make_file(tmp_path, "revalidate_ok.png")
+ content = create_content(session, path=f, hash="dup")
+ session.commit()
+
+ assert claim_qualified_content(session, content.id, "dup") is True
+
+
+def test_claim_qualified_content_false_once_retired(session, tmp_path):
+ f = _make_file(tmp_path, "revalidate_retired.png")
+ content = create_content(session, path=f, hash="dup")
+ session.commit()
+
+ session.execute(
+ update(AssetContent).where(AssetContent.id == content.id).values(is_missing=True)
+ )
+ session.commit()
+
+ assert claim_qualified_content(session, content.id, "dup") is False, (
+ "a row retired after the lookup must not be reused"
+ )
+
+
+def test_claim_qualified_content_false_once_hash_changed(session, tmp_path):
+ f = _make_file(tmp_path, "revalidate_rehashed.png")
+ content = create_content(session, path=f, hash="dup")
+ session.commit()
+
+ session.execute(
+ update(AssetContent).where(AssetContent.id == content.id).values(hash="different")
+ )
+ session.commit()
+
+ assert claim_qualified_content(session, content.id, "dup") is False, (
+ "a row whose recorded content changed identity must not be reused"
+ " for the hash that used to describe it"
+ )
+
+
+def test_refresh_qualified_content_none_when_file_vanishes(session, tmp_path):
+ f = _make_file(tmp_path, "revalidate_gone.png")
+ content = create_content(session, path=f, hash="dup")
+ session.commit()
+ assert claim_qualified_content(session, content.id, "dup") is True
+
+ os.unlink(f)
+
+ assert refresh_qualified_content(session, content.id) is None
+
+
+def test_size_mismatch_disqualifies_a_row_that_never_recorded_an_mtime(session, tmp_path):
+ f = _make_file(tmp_path, "size_mismatch.png", content=b"the real bytes on disk")
+ content = create_content(session, path=f, hash="dup", size_bytes=999_999)
+ session.commit()
+
+ assert content.mtime_ns is None, "fixture must exercise the unrecorded-mtime case"
+ assert lookup_for_view(session, "dup") is None, (
+ "a row whose recorded size disagrees with the file must not be served; an "
+ "unrecorded mtime is no reason to skip the size check too"
+ )
diff --git a/tests-unit/assets_test/queries/test_metadata.py b/tests-unit/assets_test/queries/test_metadata.py
index d7a747789..245cbd450 100644
--- a/tests-unit/assets_test/queries/test_metadata.py
+++ b/tests-unit/assets_test/queries/test_metadata.py
@@ -1,231 +1,15 @@
-"""Tests for metadata filtering logic in asset_reference queries."""
-import pytest
from sqlalchemy.orm import Session
-from app.assets.database.models import Asset, AssetReference, AssetReferenceMeta
-from app.assets.database.queries import list_references_page
-from app.assets.database.queries.asset_reference import convert_metadata_to_rows
-from app.assets.helpers import get_utc_now
+from app.assets.database.queries import create_content, create_record
-def _make_asset(session: Session, hash_val: str) -> Asset:
- asset = Asset(hash=hash_val, size_bytes=1024)
- session.add(asset)
- session.flush()
- return asset
+def test_interpretation_metadata_is_record_specific(session: Session) -> None:
+ content = create_content(session, "/output/shared-bytes.bin", hash="digest")
+ image = create_record(session, content.id, "image", mime_type="image/png")
+ text = create_record(session, content.id, "text", mime_type="text/plain")
+ image.system_metadata = {"width": 8}
+ text.system_metadata = {"encoding": "utf-8"}
-
-def _make_reference(
- session: Session,
- asset: Asset,
- name: str,
- metadata: dict | None = None,
- system_metadata: dict | None = None,
-) -> AssetReference:
- now = get_utc_now()
- ref = AssetReference(
- owner_id="",
- name=name,
- asset_id=asset.id,
- user_metadata=metadata,
- system_metadata=system_metadata,
- created_at=now,
- updated_at=now,
- last_access_time=now,
- )
- session.add(ref)
- session.flush()
-
- # Build merged projection: {**system_metadata, **user_metadata}
- merged = {**(system_metadata or {}), **(metadata or {})}
- if merged:
- for key, val in merged.items():
- for row in convert_metadata_to_rows(key, val):
- meta_row = AssetReferenceMeta(
- asset_reference_id=ref.id,
- key=row["key"],
- ordinal=row.get("ordinal", 0),
- val_str=row.get("val_str"),
- val_num=row.get("val_num"),
- val_bool=row.get("val_bool"),
- val_json=row.get("val_json"),
- )
- session.add(meta_row)
- session.flush()
-
- return ref
-
-
-class TestMetadataFilterByType:
- """Table-driven tests for metadata filtering by different value types."""
-
- @pytest.mark.parametrize(
- "match_meta,nomatch_meta,filter_key,filter_val",
- [
- # String matching
- ({"category": "models"}, {"category": "images"}, "category", "models"),
- # Integer matching
- ({"epoch": 5}, {"epoch": 10}, "epoch", 5),
- # Float matching
- ({"score": 0.95}, {"score": 0.5}, "score", 0.95),
- # Boolean True matching
- ({"enabled": True}, {"enabled": False}, "enabled", True),
- # Boolean False matching
- ({"enabled": False}, {"enabled": True}, "enabled", False),
- ],
- ids=["string", "int", "float", "bool_true", "bool_false"],
- )
- def test_filter_matches_correct_value(
- self, session: Session, match_meta, nomatch_meta, filter_key, filter_val
- ):
- asset = _make_asset(session, "hash1")
- _make_reference(session, asset, "match", match_meta)
- _make_reference(session, asset, "nomatch", nomatch_meta)
- session.commit()
-
- refs, _, total = list_references_page(
- session, metadata_filter={filter_key: filter_val}
- )
- assert total == 1
- assert refs[0].name == "match"
-
- @pytest.mark.parametrize(
- "stored_meta,filter_key,filter_val",
- [
- # String no match
- ({"category": "models"}, "category", "other"),
- # Int no match
- ({"epoch": 5}, "epoch", 99),
- # Float no match
- ({"score": 0.5}, "score", 0.99),
- ],
- ids=["string_no_match", "int_no_match", "float_no_match"],
- )
- def test_filter_returns_empty_when_no_match(
- self, session: Session, stored_meta, filter_key, filter_val
- ):
- asset = _make_asset(session, "hash1")
- _make_reference(session, asset, "item", stored_meta)
- session.commit()
-
- refs, _, total = list_references_page(
- session, metadata_filter={filter_key: filter_val}
- )
- assert total == 0
-
-
-class TestMetadataFilterNull:
- """Tests for null/missing key filtering."""
-
- @pytest.mark.parametrize(
- "match_name,match_meta,nomatch_name,nomatch_meta,filter_key",
- [
- # Null matches missing key
- ("missing_key", {}, "has_key", {"optional": "value"}, "optional"),
- # Null matches explicit null
- ("explicit_null", {"nullable": None}, "has_value", {"nullable": "present"}, "nullable"),
- ],
- ids=["missing_key", "explicit_null"],
- )
- def test_null_filter_matches(
- self, session: Session, match_name, match_meta, nomatch_name, nomatch_meta, filter_key
- ):
- asset = _make_asset(session, "hash1")
- _make_reference(session, asset, match_name, match_meta)
- _make_reference(session, asset, nomatch_name, nomatch_meta)
- session.commit()
-
- refs, _, total = list_references_page(session, metadata_filter={filter_key: None})
- assert total == 1
- assert refs[0].name == match_name
-
-
-class TestMetadataFilterList:
- """Tests for list-based (OR) filtering."""
-
- def test_filter_by_list_matches_any(self, session: Session):
- """List values should match ANY of the values (OR)."""
- asset = _make_asset(session, "hash1")
- _make_reference(session, asset, "cat_a", {"category": "a"})
- _make_reference(session, asset, "cat_b", {"category": "b"})
- _make_reference(session, asset, "cat_c", {"category": "c"})
- session.commit()
-
- refs, _, total = list_references_page(session, metadata_filter={"category": ["a", "b"]})
- assert total == 2
- names = {r.name for r in refs}
- assert names == {"cat_a", "cat_b"}
-
-
-class TestMetadataFilterMultipleKeys:
- """Tests for multiple filter keys (AND semantics)."""
-
- def test_multiple_keys_must_all_match(self, session: Session):
- """Multiple keys should ALL match (AND)."""
- asset = _make_asset(session, "hash1")
- _make_reference(session, asset, "match", {"type": "model", "version": 2})
- _make_reference(session, asset, "wrong_type", {"type": "config", "version": 2})
- _make_reference(session, asset, "wrong_version", {"type": "model", "version": 1})
- session.commit()
-
- refs, _, total = list_references_page(
- session, metadata_filter={"type": "model", "version": 2}
- )
- assert total == 1
- assert refs[0].name == "match"
-
-
-class TestMetadataFilterEmptyDict:
- """Tests for empty filter behavior."""
-
- def test_empty_filter_returns_all(self, session: Session):
- asset = _make_asset(session, "hash1")
- _make_reference(session, asset, "a", {"key": "val"})
- _make_reference(session, asset, "b", {})
- session.commit()
-
- refs, _, total = list_references_page(session, metadata_filter={})
- assert total == 2
-
-
-class TestSystemMetadataProjection:
- """Tests for system_metadata merging into the filter projection."""
-
- def test_system_metadata_keys_are_filterable(self, session: Session):
- """system_metadata keys should appear in the merged projection."""
- asset = _make_asset(session, "hash1")
- _make_reference(
- session, asset, "with_sys",
- system_metadata={"source": "scanner"},
- )
- _make_reference(session, asset, "without_sys")
- session.commit()
-
- refs, _, total = list_references_page(
- session, metadata_filter={"source": "scanner"}
- )
- assert total == 1
- assert refs[0].name == "with_sys"
-
- def test_user_metadata_overrides_system_metadata(self, session: Session):
- """user_metadata should win when both have the same key."""
- asset = _make_asset(session, "hash1")
- _make_reference(
- session, asset, "overridden",
- metadata={"origin": "user_upload"},
- system_metadata={"origin": "auto_scan"},
- )
- session.commit()
-
- # Should match the user value, not the system value
- refs, _, total = list_references_page(
- session, metadata_filter={"origin": "user_upload"}
- )
- assert total == 1
- assert refs[0].name == "overridden"
-
- # Should NOT match the system value (it was overridden)
- refs, _, total = list_references_page(
- session, metadata_filter={"origin": "auto_scan"}
- )
- assert total == 0
+ assert image.content_id == text.content_id
+ assert image.system_metadata == {"width": 8}
+ assert text.system_metadata == {"encoding": "utf-8"}
diff --git a/tests-unit/assets_test/queries/test_records.py b/tests-unit/assets_test/queries/test_records.py
new file mode 100644
index 000000000..a2066b368
--- /dev/null
+++ b/tests-unit/assets_test/queries/test_records.py
@@ -0,0 +1,137 @@
+import os
+
+import pytest
+from sqlalchemy import create_engine, select, update
+from sqlalchemy.exc import IntegrityError
+from sqlalchemy.orm import Session
+
+from app.assets.database.models import Asset, AssetContent, AssetTag
+from app.assets.database.queries.records import (
+ RecordPageSpec,
+ create_content,
+ create_record,
+ delete_record,
+ get_record_by_id,
+ list_records_page,
+ mark_content_missing,
+)
+from app.database.models import Base
+
+
+def session():
+ engine = create_engine("sqlite:///:memory:", connect_args={"check_same_thread": False})
+ Base.metadata.create_all(engine)
+ with Session(engine) as sess:
+ yield sess
+
+
+def test_listing_includes_missing_by_default_with_missing_tag(session):
+ content = create_content(session, path="/tmp/f1")
+ record = create_record(session, content_id=content.id, name="test")
+ mark_content_missing(session, content.id)
+ session.commit()
+
+ results, tag_map, total = list_records_page(session, RecordPageSpec())
+
+ assert any(r.id == record.id for r in results)
+ assert total == 1
+ assert "missing" in tag_map.get(record.id, [])
+
+
+def test_listing_excludes_missing_with_filter(session):
+ content = create_content(session, path="/tmp/f2")
+ record = create_record(session, content_id=content.id, name="test2")
+ mark_content_missing(session, content.id)
+ session.commit()
+
+ results, _, total = list_records_page(
+ session,
+ RecordPageSpec(none_tags=("missing",)),
+ )
+
+ assert not any(r.id == record.id for r in results), "Missing record should be excluded"
+ assert total == 0
+
+
+def test_delete_record_never_deletes_referenced_preview(session):
+ preview_content = create_content(session, path="/tmp/preview")
+ preview_record = create_record(session, content_id=preview_content.id, name="preview")
+ content1 = create_content(session, path="/tmp/f3")
+ content2 = create_content(session, path="/tmp/f4")
+ r1 = create_record(session, content_id=content1.id, name="r1")
+ r2 = create_record(session, content_id=content2.id, name="r2")
+
+ session.execute(update(Asset).where(Asset.id == r1.id).values(preview_id=preview_record.id))
+ session.execute(update(Asset).where(Asset.id == r2.id).values(preview_id=preview_record.id))
+ session.commit()
+
+ delete_record(session, r1.id)
+ session.commit()
+ assert get_record_by_id(session, preview_record.id) is not None, "Preview must survive"
+
+ delete_record(session, r2.id)
+ session.commit()
+ assert get_record_by_id(session, preview_record.id) is not None, (
+ "Preview must survive even with no referrers"
+ )
+
+ content_row = session.execute(
+ select(AssetContent).where(AssetContent.id == preview_content.id)
+ ).scalar_one_or_none()
+ assert content_row is not None, "Preview content row should survive (D-3 floor)"
+
+
+def test_concurrent_create_content_same_path(tmp_path):
+ db_path = str(tmp_path / "concurrent.db")
+ engine = create_engine(f"sqlite:///{db_path}", connect_args={"check_same_thread": False})
+ Base.metadata.create_all(engine)
+ shared_path = os.path.abspath("/tmp/shared")
+
+ with Session(engine) as s1, Session(engine) as s2:
+ create_content(s1, path=shared_path)
+ s1.commit()
+ create_content(s2, path=shared_path)
+ s2.commit()
+
+ with Session(engine) as s:
+ live_rows = list(
+ s.execute(
+ select(AssetContent).where(
+ AssetContent.path == shared_path,
+ AssetContent.is_missing.is_(False),
+ )
+ ).scalars()
+ )
+ assert len(live_rows) == 1, f"Expected exactly one live row, got {len(live_rows)}"
+
+
+def test_create_content_check_violation_surfaces_the_constraint_error(session):
+ with pytest.raises(IntegrityError):
+ create_content(session, path="/tmp/negative-size", size_bytes=-1)
+
+
+def test_create_content_check_violation_on_mtime_surfaces(session):
+ with pytest.raises(IntegrityError):
+ create_content(session, path="/tmp/negative-mtime", size_bytes=0, mtime_ns=-1)
+
+
+def test_create_content_uniqueness_race_returns_existing_live_row(session):
+ first = create_content(session, path="/tmp/race")
+ second = create_content(session, path="/tmp/race")
+
+ assert second.id == first.id
+
+
+def test_create_record_links_a_repeated_tag_name_once(session):
+ content = create_content(session, path="/tmp/repeated-tag")
+
+ record = create_record(session, content_id=content.id, name="repeat", tags=["x", "x"])
+ session.commit()
+
+ linked = session.scalars(
+ select(AssetTag.tag_name).where(AssetTag.asset_id == record.id)
+ ).all()
+ assert list(linked) == ["x"], (
+ "a tag name repeated within one call must link once, not raise on the composite PK"
+ )
diff --git a/tests-unit/assets_test/queries/test_tags.py b/tests-unit/assets_test/queries/test_tags.py
index bc041953a..b0d96c9a5 100644
--- a/tests-unit/assets_test/queries/test_tags.py
+++ b/tests-unit/assets_test/queries/test_tags.py
@@ -1,369 +1,23 @@
-import pytest
from sqlalchemy.orm import Session
-from app.assets.database.models import Asset, AssetReference, AssetReferenceTag, AssetReferenceMeta, Tag
from app.assets.database.queries import (
- ensure_tags_exist,
- get_reference_tags,
- set_reference_tags,
- add_tags_to_reference,
- remove_tags_from_reference,
- add_missing_tag_for_asset_id,
- remove_missing_tag_for_asset_id,
- list_tags_with_usage,
- bulk_insert_tags_and_meta,
+ create_content,
+ create_record,
+ fetch_record_tags,
+ list_records_page,
)
-from app.assets.helpers import get_utc_now
+from app.assets.database.queries.records import RecordPageSpec
-def _make_asset(session: Session, hash_val: str | None = None) -> Asset:
- asset = Asset(hash=hash_val, size_bytes=1024)
- session.add(asset)
- session.flush()
- return asset
+def test_record_tags_filter_records_without_affecting_content(session: Session) -> None:
+ content = create_content(session, "/models/shared.safetensors")
+ tagged = create_record(session, content.id, "tagged", tags=["model"])
+ untagged = create_record(session, content.id, "untagged")
-
-def _make_reference(session: Session, asset: Asset, name: str = "test", owner_id: str = "") -> AssetReference:
- now = get_utc_now()
- ref = AssetReference(
- owner_id=owner_id,
- name=name,
- asset_id=asset.id,
- created_at=now,
- updated_at=now,
- last_access_time=now,
+ records, _, _ = list_records_page(
+ session,
+ RecordPageSpec(all_tags=("model",)),
)
- session.add(ref)
- session.flush()
- return ref
-
-
-class TestEnsureTagsExist:
- def test_creates_new_tags(self, session: Session):
- ensure_tags_exist(session, ["alpha", "beta"])
- session.commit()
-
- tags = session.query(Tag).all()
- assert {t.name for t in tags} == {"alpha", "beta"}
-
- def test_is_idempotent(self, session: Session):
- ensure_tags_exist(session, ["alpha"])
- ensure_tags_exist(session, ["alpha"])
- session.commit()
-
- assert session.query(Tag).count() == 1
-
- def test_normalizes_tags(self, session: Session):
- ensure_tags_exist(session, [" ALPHA ", "Beta", "alpha"])
- session.commit()
-
- tags = session.query(Tag).all()
- assert {t.name for t in tags} == {"ALPHA", "Beta", "alpha"}
-
- def test_empty_list_is_noop(self, session: Session):
- ensure_tags_exist(session, [])
- session.commit()
- assert session.query(Tag).count() == 0
-
-
-class TestGetReferenceTags:
- def test_returns_empty_for_no_tags(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset)
-
- tags = get_reference_tags(session, reference_id=ref.id)
- assert tags == []
-
- def test_returns_tags_for_reference(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset)
-
- ensure_tags_exist(session, ["tag1", "tag2"])
- session.add_all([
- AssetReferenceTag(asset_reference_id=ref.id, tag_name="tag1", origin="manual", added_at=get_utc_now()),
- AssetReferenceTag(asset_reference_id=ref.id, tag_name="tag2", origin="manual", added_at=get_utc_now()),
- ])
- session.flush()
-
- tags = get_reference_tags(session, reference_id=ref.id)
- assert set(tags) == {"tag1", "tag2"}
-
-
-class TestSetReferenceTags:
- def test_adds_new_tags(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset)
-
- result = set_reference_tags(session, reference_id=ref.id, tags=["a", "b"])
- session.commit()
-
- assert set(result.added) == {"a", "b"}
- assert result.removed == []
- assert set(result.total) == {"a", "b"}
-
- def test_removes_old_tags(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset)
-
- set_reference_tags(session, reference_id=ref.id, tags=["a", "b", "c"])
- result = set_reference_tags(session, reference_id=ref.id, tags=["a"])
- session.commit()
-
- assert result.added == []
- assert set(result.removed) == {"b", "c"}
- assert result.total == ["a"]
-
- def test_replaces_tags(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset)
-
- set_reference_tags(session, reference_id=ref.id, tags=["a", "b"])
- result = set_reference_tags(session, reference_id=ref.id, tags=["b", "c"])
- session.commit()
-
- assert result.added == ["c"]
- assert result.removed == ["a"]
- assert set(result.total) == {"b", "c"}
-
-
-class TestAddTagsToReference:
- def test_adds_tags(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset)
-
- result = add_tags_to_reference(session, reference_id=ref.id, tags=["x", "y"])
- session.commit()
-
- assert set(result.added) == {"x", "y"}
- assert result.already_present == []
-
- def test_reports_already_present(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset)
-
- add_tags_to_reference(session, reference_id=ref.id, tags=["x"])
- result = add_tags_to_reference(session, reference_id=ref.id, tags=["x", "y"])
- session.commit()
-
- assert result.added == ["y"]
- assert result.already_present == ["x"]
-
- def test_raises_for_missing_reference(self, session: Session):
- with pytest.raises(ValueError, match="not found"):
- add_tags_to_reference(session, reference_id="nonexistent", tags=["x"])
-
-
-class TestRemoveTagsFromReference:
- def test_removes_tags(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset)
-
- add_tags_to_reference(session, reference_id=ref.id, tags=["a", "b", "c"])
- result = remove_tags_from_reference(session, reference_id=ref.id, tags=["a", "b"])
- session.commit()
-
- assert set(result.removed) == {"a", "b"}
- assert result.not_present == []
- assert result.total_tags == ["c"]
-
- def test_reports_not_present(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset)
-
- add_tags_to_reference(session, reference_id=ref.id, tags=["a"])
- result = remove_tags_from_reference(session, reference_id=ref.id, tags=["a", "x"])
- session.commit()
-
- assert result.removed == ["a"]
- assert result.not_present == ["x"]
-
- def test_raises_for_missing_reference(self, session: Session):
- with pytest.raises(ValueError, match="not found"):
- remove_tags_from_reference(session, reference_id="nonexistent", tags=["x"])
-
-
-class TestMissingTagFunctions:
- def test_add_missing_tag_for_asset_id(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset)
- ensure_tags_exist(session, ["missing"])
-
- add_missing_tag_for_asset_id(session, asset_id=asset.id)
- session.commit()
-
- tags = get_reference_tags(session, reference_id=ref.id)
- assert "missing" in tags
-
- def test_add_missing_tag_is_idempotent(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset)
- ensure_tags_exist(session, ["missing"])
-
- add_missing_tag_for_asset_id(session, asset_id=asset.id)
- add_missing_tag_for_asset_id(session, asset_id=asset.id)
- session.commit()
-
- links = session.query(AssetReferenceTag).filter_by(asset_reference_id=ref.id, tag_name="missing").all()
- assert len(links) == 1
-
- def test_remove_missing_tag_for_asset_id(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset)
- ensure_tags_exist(session, ["missing"])
- add_missing_tag_for_asset_id(session, asset_id=asset.id)
-
- remove_missing_tag_for_asset_id(session, asset_id=asset.id)
- session.commit()
-
- tags = get_reference_tags(session, reference_id=ref.id)
- assert "missing" not in tags
-
-
-class TestListTagsWithUsage:
- def test_returns_tags_with_counts(self, session: Session):
- ensure_tags_exist(session, ["used", "unused"])
-
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset)
- add_tags_to_reference(session, reference_id=ref.id, tags=["used"])
- session.commit()
-
- rows, total = list_tags_with_usage(session)
-
- tag_dict = {name: count for name, count in rows}
- assert tag_dict["used"] == 1
- assert tag_dict["unused"] == 0
- assert total == 2
-
- def test_exclude_zero_counts(self, session: Session):
- ensure_tags_exist(session, ["used", "unused"])
-
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset)
- add_tags_to_reference(session, reference_id=ref.id, tags=["used"])
- session.commit()
-
- rows, total = list_tags_with_usage(session, include_zero=False)
-
- tag_names = {name for name, _ in rows}
- assert "used" in tag_names
- assert "unused" not in tag_names
-
- def test_prefix_filter(self, session: Session):
- ensure_tags_exist(session, ["alpha", "beta", "alphabet"])
- session.commit()
-
- rows, total = list_tags_with_usage(session, prefix="alph")
-
- tag_names = {name for name, _ in rows}
- assert tag_names == {"alpha", "alphabet"}
-
- def test_prefix_filter_is_case_sensitive(self, session: Session):
- ensure_tags_exist(session, ["model_type:LLM", "model_type:llm"])
- session.commit()
-
- rows, total = list_tags_with_usage(session, prefix="model_type:L")
-
- tag_names = {name for name, _ in rows}
- assert tag_names == {"model_type:LLM"}
- assert total == 1
-
- def test_order_by_name(self, session: Session):
- ensure_tags_exist(session, ["zebra", "alpha", "middle"])
- session.commit()
-
- rows, _ = list_tags_with_usage(session, order="name_asc")
-
- names = [name for name, _ in rows]
- assert names == ["alpha", "middle", "zebra"]
-
- def test_owner_visibility(self, session: Session):
- ensure_tags_exist(session, ["shared-tag", "owner-tag"])
-
- asset = _make_asset(session, "hash1")
- shared_ref = _make_reference(session, asset, name="shared", owner_id="")
- owner_ref = _make_reference(session, asset, name="owned", owner_id="user1")
-
- add_tags_to_reference(session, reference_id=shared_ref.id, tags=["shared-tag"])
- add_tags_to_reference(session, reference_id=owner_ref.id, tags=["owner-tag"])
- session.commit()
-
- # Empty owner sees only shared
- rows, _ = list_tags_with_usage(session, owner_id="", include_zero=False)
- tag_dict = {name: count for name, count in rows}
- assert tag_dict.get("shared-tag", 0) == 1
- assert tag_dict.get("owner-tag", 0) == 0
-
- # User1 sees both
- rows, _ = list_tags_with_usage(session, owner_id="user1", include_zero=False)
- tag_dict = {name: count for name, count in rows}
- assert tag_dict.get("shared-tag", 0) == 1
- assert tag_dict.get("owner-tag", 0) == 1
-
-
-class TestBulkInsertTagsAndMeta:
- def test_inserts_tags(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset)
- ensure_tags_exist(session, ["bulk-tag1", "bulk-tag2"])
- session.commit()
-
- now = get_utc_now()
- tag_rows = [
- {"asset_reference_id": ref.id, "tag_name": "bulk-tag1", "origin": "manual", "added_at": now},
- {"asset_reference_id": ref.id, "tag_name": "bulk-tag2", "origin": "manual", "added_at": now},
- ]
- bulk_insert_tags_and_meta(session, tag_rows=tag_rows, meta_rows=[])
- session.commit()
-
- tags = get_reference_tags(session, reference_id=ref.id)
- assert set(tags) == {"bulk-tag1", "bulk-tag2"}
-
- def test_inserts_meta(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset)
- session.commit()
-
- meta_rows = [
- {
- "asset_reference_id": ref.id,
- "key": "meta-key",
- "ordinal": 0,
- "val_str": "meta-value",
- "val_num": None,
- "val_bool": None,
- "val_json": None,
- },
- ]
- bulk_insert_tags_and_meta(session, tag_rows=[], meta_rows=meta_rows)
- session.commit()
-
- meta = session.query(AssetReferenceMeta).filter_by(asset_reference_id=ref.id).all()
- assert len(meta) == 1
- assert meta[0].key == "meta-key"
- assert meta[0].val_str == "meta-value"
-
- def test_ignores_conflicts(self, session: Session):
- asset = _make_asset(session, "hash1")
- ref = _make_reference(session, asset)
- ensure_tags_exist(session, ["existing-tag"])
- add_tags_to_reference(session, reference_id=ref.id, tags=["existing-tag"])
- session.commit()
-
- now = get_utc_now()
- tag_rows = [
- {"asset_reference_id": ref.id, "tag_name": "existing-tag", "origin": "duplicate", "added_at": now},
- ]
- bulk_insert_tags_and_meta(session, tag_rows=tag_rows, meta_rows=[])
- session.commit()
-
- # Should still have only one tag link
- links = session.query(AssetReferenceTag).filter_by(asset_reference_id=ref.id, tag_name="existing-tag").all()
- assert len(links) == 1
- # Origin should be original, not overwritten
- assert links[0].origin == "manual"
- def test_empty_lists_is_noop(self, session: Session):
- bulk_insert_tags_and_meta(session, tag_rows=[], meta_rows=[])
- assert session.query(AssetReferenceTag).count() == 0
- assert session.query(AssetReferenceMeta).count() == 0
+ assert [record.id for record in records] == [tagged.id]
+ assert fetch_record_tags(session, untagged.id) == []
diff --git a/tests-unit/assets_test/queries/test_tags_query.py b/tests-unit/assets_test/queries/test_tags_query.py
new file mode 100644
index 000000000..acc21f409
--- /dev/null
+++ b/tests-unit/assets_test/queries/test_tags_query.py
@@ -0,0 +1,198 @@
+from __future__ import annotations
+
+from collections import Counter
+from collections.abc import Sequence
+
+from sqlalchemy.orm import Session
+
+from app.assets.database.models import Asset, Tag
+from app.assets.database.queries.records import (
+ RecordPageSpec,
+ create_content,
+ create_record,
+ list_records_page,
+ mark_content_missing,
+)
+from app.assets.database.queries.tags import (
+ list_tag_counts_for_filtered_assets,
+ list_tags_with_usage,
+)
+
+
+def _seed_asset(
+ session: Session, path: str, name: str, tags: Sequence[str]
+) -> Asset:
+ content = create_content(session, path=path)
+ return create_record(session, content_id=content.id, name=name, tags=list(tags))
+
+
+def test_usage_counts_match_seeded_asset_tag_rows(session: Session) -> None:
+ _seed_asset(session, "/models/a", "a", ["models", "checkpoint"])
+ _seed_asset(session, "/models/b", "b", ["models"])
+ _seed_asset(session, "/models/c", "c", ["lora"])
+ session.commit()
+
+ rows, total = list_tags_with_usage(session)
+
+ assert dict(rows) == {"models": 2, "checkpoint": 1, "lora": 1}
+ assert total == 3
+
+
+def test_prefix_filters_tags_by_leading_substring(session: Session) -> None:
+ _seed_asset(session, "/m/1", "1", ["model", "mask", "zebra"])
+ session.commit()
+
+ rows, total = list_tags_with_usage(session, prefix="ma")
+
+ assert [name for name, _ in rows] == ["mask"]
+ assert total == 1
+
+
+def test_limit_and_offset_paginate_over_real_total(session: Session) -> None:
+ for index in range(5):
+ _seed_asset(session, f"/p/{index}", str(index), [f"tag{index}"])
+ session.commit()
+
+ rows, total = list_tags_with_usage(session, limit=2, offset=1, order="name_asc")
+
+ assert [name for name, _ in rows] == ["tag1", "tag2"]
+ assert total == 5
+
+
+def test_include_zero_false_drops_unused_tags_and_shrinks_total(session: Session) -> None:
+ _seed_asset(session, "/z/1", "1", ["used"])
+ session.add(Tag(name="unused"))
+ session.commit()
+
+ with_zero, total_with = list_tags_with_usage(session, include_zero=True)
+ without_zero, total_without = list_tags_with_usage(session, include_zero=False)
+
+ assert ("unused", 0) in with_zero
+ assert total_with == 2
+ assert [name for name, _ in without_zero] == ["used"]
+ assert total_without == 1
+
+
+def test_order_is_count_desc_then_name_asc_or_name_asc(session: Session) -> None:
+ _seed_asset(session, "/o/1", "1", ["gamma"])
+ _seed_asset(session, "/o/2", "2", ["gamma", "beta"])
+ _seed_asset(session, "/o/3", "3", ["gamma", "beta", "alpha"])
+ session.commit()
+
+ by_count, _ = list_tags_with_usage(session, order="count_desc")
+ by_name, _ = list_tags_with_usage(session, order="name_asc")
+
+ assert [name for name, _ in by_count] == ["gamma", "beta", "alpha"]
+ assert [name for name, _ in by_name] == ["alpha", "beta", "gamma"]
+
+
+def test_missing_content_counts_all_its_tags_including_missing(
+ session: Session,
+) -> None:
+ _seed_asset(session, "/live", "live", ["foo"])
+ gone = create_content(session, path="/gone")
+ create_record(session, content_id=gone.id, name="gone", tags=["foo"])
+ mark_content_missing(session, gone.id)
+ session.commit()
+
+ counts = dict(list_tags_with_usage(session)[0])
+
+ assert counts["foo"] == 2
+ assert counts["missing"] == 1
+
+
+def test_histogram_counts_every_tag_on_the_filtered_assets(session: Session) -> None:
+ _seed_asset(session, "/h/a", "a", ["models", "checkpoint"])
+ _seed_asset(session, "/h/b", "b", ["models", "lora"])
+ _seed_asset(session, "/h/c", "c", ["other"])
+ session.commit()
+
+ histogram = list_tag_counts_for_filtered_assets(session, include_tags=["models"])
+
+ assert histogram == {"models": 2, "checkpoint": 1, "lora": 1}
+
+
+def test_histogram_includes_missing_content_assets(session: Session) -> None:
+ _seed_asset(session, "/hm/live", "live", ["models"])
+ gone = create_content(session, path="/hm/gone")
+ create_record(session, content_id=gone.id, name="gone", tags=["models"])
+ mark_content_missing(session, gone.id)
+ session.commit()
+
+ histogram = list_tag_counts_for_filtered_assets(session, include_tags=["models"])
+
+ assert histogram == {"models": 2, "missing": 1}
+
+
+def test_refine_and_list_share_the_same_filtered_asset_set(session: Session) -> None:
+ kept = _seed_asset(session, "/s/a", "checkpoint-a", ["models", "checkpoint"])
+ _seed_asset(session, "/s/b", "checkpoint-b", ["models", "archived"])
+ _seed_asset(session, "/s/c", "lora-c", ["models"])
+ gone = create_content(session, path="/s/d")
+ missing_match = create_record(
+ session, content_id=gone.id, name="check-d", tags=["models", "checkpoint"]
+ )
+ mark_content_missing(session, gone.id)
+ session.commit()
+
+ spec = RecordPageSpec(
+ all_tags=("models",),
+ none_tags=("archived",),
+ name_contains="check",
+ limit=100,
+ )
+ records, tag_map, _ = list_records_page(session, spec)
+ expected = Counter(
+ tag for record in records for tag in tag_map.get(record.id, [])
+ )
+ histogram = list_tag_counts_for_filtered_assets(
+ session,
+ include_tags=["models"],
+ exclude_tags=["archived"],
+ name_contains="check",
+ limit=100,
+ )
+
+ assert {record.id for record in records} == {kept.id, missing_match.id}
+ assert histogram == dict(expected)
+ assert histogram == {"models": 2, "checkpoint": 2, "missing": 1}
+
+
+def test_histogram_any_tags_matches_the_list_union(session: Session) -> None:
+ red = _seed_asset(session, "/an/a", "a", ["red"])
+ blue = _seed_asset(session, "/an/b", "b", ["blue"])
+ _seed_asset(session, "/an/c", "c", ["green"])
+ session.commit()
+
+ records, _, _ = list_records_page(
+ session, RecordPageSpec(any_tags=("red", "blue"), limit=100)
+ )
+ histogram = list_tag_counts_for_filtered_assets(session, any_tags=["red", "blue"])
+
+ assert {record.id for record in records} == {red.id, blue.id}
+ assert histogram == {"red": 1, "blue": 1}
+
+
+def test_list_refine_and_tags_agree_on_counts_including_missing(
+ session: Session,
+) -> None:
+ _seed_asset(session, "/agree/live", "live", ["models", "checkpoint"])
+ gone = create_content(session, path="/agree/gone")
+ create_record(session, content_id=gone.id, name="gone", tags=["models", "lora"])
+ mark_content_missing(session, gone.id)
+ session.commit()
+
+ records, tag_map, total = list_records_page(
+ session, RecordPageSpec(all_tags=("models",), limit=100)
+ )
+ list_tally = dict(
+ Counter(tag for record in records for tag in tag_map.get(record.id, []))
+ )
+ refine = list_tag_counts_for_filtered_assets(session, include_tags=["models"])
+ global_usage = dict(list_tags_with_usage(session)[0])
+
+ expected = {"models": 2, "checkpoint": 1, "lora": 1, "missing": 1}
+ assert total == 2
+ assert list_tally == expected
+ assert refine == expected
+ assert global_usage == expected
diff --git a/tests-unit/assets_test/services/conftest.py b/tests-unit/assets_test/services/conftest.py
index bc0723e61..9c5d1dfbb 100644
--- a/tests-unit/assets_test/services/conftest.py
+++ b/tests-unit/assets_test/services/conftest.py
@@ -1,11 +1,13 @@
import tempfile
+from contextlib import contextmanager
from pathlib import Path
from unittest.mock import patch
import pytest
from sqlalchemy import create_engine, event
-from sqlalchemy.orm import Session
+from sqlalchemy.orm import Session, Session as SASession
+from app.assets import mode
from app.assets.database.models import Base
@@ -15,6 +17,16 @@ def autoclean_unit_test_assets():
yield
[email protected](autouse=True)
+def initialised_hash_mode():
+ class _HashingOff:
+ enable_asset_hashing = False
+
+ mode.init(_HashingOff())
+ yield
+ mode.init(None)
+
+
@pytest.fixture
def db_engine():
"""In-memory SQLite engine for fast unit tests."""
@@ -48,9 +60,6 @@ def session(db_engine):
@pytest.fixture
def mock_create_session(db_engine):
"""Patch create_session to use our in-memory database."""
- from contextlib import contextmanager
- from sqlalchemy.orm import Session as SASession
-
@contextmanager
def _create_session():
with SASession(db_engine) as sess:
diff --git a/tests-unit/assets_test/services/path_prefix_cases.py b/tests-unit/assets_test/services/path_prefix_cases.py
new file mode 100644
index 000000000..f5586d9e8
--- /dev/null
+++ b/tests-unit/assets_test/services/path_prefix_cases.py
@@ -0,0 +1,35 @@
+from __future__ import annotations
+
+import os
+
+
+def stored_path(path: str) -> str:
+ return os.path.abspath(path)
+
+
+def prefix_case_paths(root: str) -> list[tuple[str, bool]]:
+ parent = os.path.dirname(root)
+ name = os.path.basename(root)
+ upper = name.upper()
+ assert upper != name, f"root basename {name!r} has no distinct uppercase form"
+
+ return [
+ (root, True),
+ (os.path.join(root, "child.png"), True),
+ (os.path.join(root, "sub", "deep.png"), True),
+ (os.path.join(root, "meta%_*?[].png"), True),
+ (os.path.join(parent, upper), False),
+ (os.path.join(parent, upper, "case.png"), False),
+ (root + "-other" + os.sep + "lexical.png", False),
+ (root + "extra", False),
+ (os.path.join(parent, "unrelated.png"), False),
+ (os.path.join(root, os.pardir, "escaped.png"), False),
+ (os.path.join(root, "sub", os.pardir, "in.png"), True),
+ (os.path.join(name, "relative.png"), False),
+ (root + os.sep + os.sep + "doubled.png", True),
+ (os.path.join(root, "trailing.png") + os.sep, True),
+ ]
+
+
+def expected_prefix_case_paths(root: str) -> set[str]:
+ return {os.path.abspath(path) for path, expected in prefix_case_paths(root) if expected}
diff --git a/tests-unit/assets_test/services/test_admission_gate.py b/tests-unit/assets_test/services/test_admission_gate.py
new file mode 100644
index 000000000..a34ae9c59
--- /dev/null
+++ b/tests-unit/assets_test/services/test_admission_gate.py
@@ -0,0 +1,170 @@
+import os
+from collections.abc import Iterator
+from pathlib import Path
+from unittest.mock import patch
+
+import pytest
+from sqlalchemy import select
+
+from app.assets import scanner_admission
+from app.assets.database.models import AssetContent
+from app.assets.scanner import (
+ _WATCH_LIST,
+ _WatchEntry,
+ _should_skip_extension,
+ _two_stat_admit,
+ tick_watch_list,
+)
+
+
[email protected](autouse=True)
+def clear_watch_list() -> Iterator[None]:
+ _WATCH_LIST.clear()
+ yield
+ _WATCH_LIST.clear()
+
+
+def test_part_suffix_never_admitted():
+ assert _should_skip_extension("model.safetensors.part") is True
+
+
+def test_size_drift_watch_listed(temp_dir: Path, monkeypatch):
+ path = temp_dir / "download.bin"
+ path.write_bytes(b"first")
+ first_stat = path.stat()
+
+ def changed_stat(_: float) -> None:
+ path.write_bytes(b"changed")
+
+ monkeypatch.setattr("app.assets.scanner_admission.time.sleep", changed_stat)
+ admitted, watched = _two_stat_admit([(str(path), first_stat)])
+
+ assert admitted == []
+ assert watched == [str(path)]
+
+
+def test_watch_list_is_bounded_when_distinct_paths_keep_changing(temp_dir: Path, monkeypatch):
+ candidates: list[tuple[str, os.stat_result]] = []
+ for index in range(1000):
+ path = temp_dir / f"changing-{index}.bin"
+ path.write_bytes(b"before")
+ candidates.append((str(path), path.stat()))
+ path.write_bytes(b"after-change")
+ monkeypatch.setattr("app.assets.scanner_admission.time.sleep", lambda _: None)
+
+ _two_stat_admit(candidates)
+
+ assert scanner_admission._WATCH_LIST_MAX_SIZE == 256
+ assert len(_WATCH_LIST) <= scanner_admission._WATCH_LIST_MAX_SIZE
+
+
+def test_refreshing_watched_path_preserves_ticks_and_replaces_stat(temp_dir: Path, monkeypatch):
+ path = temp_dir / "changing.bin"
+ path.write_bytes(b"first")
+ first_stat = path.stat()
+ path.write_bytes(b"second-version")
+ monkeypatch.setattr("app.assets.scanner_admission.time.sleep", lambda _: None)
+ _two_stat_admit([(str(path), first_stat)])
+ _WATCH_LIST[0].ticks = 7
+ refresh_first_stat = path.stat()
+ path.write_bytes(b"third-version-is-longer")
+ refreshed_stat = path.stat()
+
+ _two_stat_admit([(str(path), refresh_first_stat)])
+
+ assert len(_WATCH_LIST) == 1
+ assert _WATCH_LIST[0].last_stat == refreshed_stat
+ assert _WATCH_LIST[0].ticks == 7
+
+
+def test_never_stabilizes_dropped_after_cap(session, temp_dir: Path):
+ path = temp_dir / "moving.bin"
+ path.write_bytes(b"0")
+ initial_stat = path.stat()
+ previous_target_ns = initial_stat.st_mtime_ns
+ _WATCH_LIST[:] = [_WatchEntry(str(path), initial_stat)]
+ for index in range(scanner_admission._WATCH_SCAN_RETRIES):
+ path.write_bytes(str(index + 1).encode())
+ target_ns = max(path.stat().st_mtime_ns, previous_target_ns) + 1_000_000
+ os.utime(path, ns=(target_ns, target_ns))
+ previous_target_ns = target_ns
+ tick_watch_list(session)
+
+ assert _WATCH_LIST == []
+ assert session.scalars(select(AssetContent)).all() == []
+
+
+def test_stable_scan_admission_removes_watch_entry_before_next_tick(session, temp_dir: Path, monkeypatch):
+ path = temp_dir / "stable.bin"
+ path.write_bytes(b"complete")
+ current_stat = path.stat()
+ _WATCH_LIST[:] = [_WatchEntry(str(path), current_stat, ticks=4)]
+ monkeypatch.setattr("app.assets.scanner_admission.time.sleep", lambda _: None)
+
+ admitted, watched = _two_stat_admit([(str(path), current_stat)])
+ entries_after_admission = len(_WATCH_LIST)
+ with (
+ patch("app.assets.scanner_admission.compute_loader_path", return_value="stable.bin"),
+ patch(
+ "app.assets.scanner_admission.get_name_and_tags_from_asset_path",
+ return_value=("stable.bin", []),
+ ),
+ patch("app.assets.scanner.seed_asset_specs") as seed_asset_specs,
+ ):
+ tick_watch_list(session)
+
+ assert admitted == [str(path)]
+ assert watched == []
+ assert entries_after_admission == 0
+ seed_asset_specs.assert_not_called()
+
+
+def test_evicted_path_is_admitted_by_later_stable_scan(temp_dir: Path, monkeypatch):
+ monkeypatch.setattr(scanner_admission, "_WATCH_LIST_MAX_SIZE", 2, raising=False)
+ monkeypatch.setattr("app.assets.scanner_admission.time.sleep", lambda _: None)
+ paths: list[Path] = []
+ candidates: list[tuple[str, os.stat_result]] = []
+ for index in range(3):
+ path = temp_dir / f"overflow-{index}.bin"
+ path.write_bytes(b"before")
+ candidates.append((str(path), path.stat()))
+ path.write_bytes(b"after-change")
+ paths.append(path)
+ _two_stat_admit(candidates)
+ entries_after_overflow = [entry.path for entry in _WATCH_LIST]
+ stable_stat = paths[0].stat()
+
+ admitted, watched = _two_stat_admit([(str(paths[0]), stable_stat)])
+
+ assert entries_after_overflow == [str(paths[1]), str(paths[2])]
+ assert admitted == [str(paths[0])]
+ assert watched == []
+
+
+def test_empty_candidate_batch_returns_without_paying_stability_gap(monkeypatch):
+ sleeps: list[float] = []
+ monkeypatch.setattr(
+ "app.assets.scanner_admission.time.sleep", lambda seconds: sleeps.append(seconds)
+ )
+
+ admitted, watched = _two_stat_admit([])
+
+ assert admitted == []
+ assert watched == []
+ assert sleeps == []
+
+
+def test_nonempty_candidate_batch_still_pays_stability_gap(temp_dir: Path, monkeypatch):
+ path = temp_dir / "stable.bin"
+ path.write_bytes(b"complete")
+ first_stat = path.stat()
+ sleeps: list[float] = []
+ monkeypatch.setattr(
+ "app.assets.scanner_admission.time.sleep", lambda seconds: sleeps.append(seconds)
+ )
+
+ admitted, watched = _two_stat_admit([(str(path), first_stat)])
+
+ assert sleeps == [0.1]
+ assert admitted == [str(path)]
+ assert watched == []
diff --git a/tests-unit/assets_test/services/test_admission_integration.py b/tests-unit/assets_test/services/test_admission_integration.py
new file mode 100644
index 000000000..062aab76f
--- /dev/null
+++ b/tests-unit/assets_test/services/test_admission_integration.py
@@ -0,0 +1,20 @@
+from unittest.mock import patch
+
+from sqlalchemy import select
+
+from app.assets.database.models import AssetContent
+from app.assets.scanner import build_asset_specs, seed_asset_specs
+
+
+def test_drifting_file_never_reaches_seed(session, temp_dir):
+ path = temp_dir / "drifting.bin"
+ path.write_bytes(b"still downloading")
+
+ with (
+ patch("folder_paths.get_input_directory", return_value=str(temp_dir)),
+ patch("app.assets.scanner._two_stat_admit", return_value=([], [str(path)])),
+ ):
+ specs, _, _ = build_asset_specs([str(path)], set(), enable_metadata_extraction=False)
+ seed_asset_specs(session, specs)
+
+ assert list(session.scalars(select(AssetContent))) == []
diff --git a/tests-unit/assets_test/services/test_api_routes_b.py b/tests-unit/assets_test/services/test_api_routes_b.py
new file mode 100644
index 000000000..c6e186eb3
--- /dev/null
+++ b/tests-unit/assets_test/services/test_api_routes_b.py
@@ -0,0 +1,155 @@
+import json
+from collections.abc import Callable
+from unittest.mock import AsyncMock
+
+import pytest
+from aiohttp import web
+from aiohttp.test_utils import make_mocked_request
+from sqlalchemy.orm import Session
+
+from app.assets import mode
+from app.assets.api import routes
+from app.assets.database.queries.records import (
+ create_content,
+ create_record,
+ mark_content_missing,
+)
+
+
+def _session_factory(engine) -> Callable[[], Session]:
+ return lambda: Session(engine)
+
+
+async def test_unfiltered_listing_includes_missing_entity(
+ db_engine, session, temp_dir, monkeypatch
+):
+ content = create_content(session, path=str(temp_dir / "missing.png"))
+ record = create_record(
+ session,
+ content_id=content.id,
+ name="missing.png",
+ mime_type="image/png",
+ tags=["input"],
+ )
+ mark_content_missing(session, content.id)
+ session.commit()
+
+ monkeypatch.setattr(routes, "create_session", _session_factory(db_engine))
+ monkeypatch.setattr(routes, "_ASSETS_ENABLED", True)
+
+ response = await routes.list_assets_route(
+ make_mocked_request("GET", "/api/assets")
+ )
+
+ assert isinstance(response, web.Response)
+ response_body = response.body
+ assert isinstance(response_body, bytes | bytearray)
+ body = json.loads(response_body)
+ assert record.id in {item["id"] for item in body["assets"]}
+ assert body["total"] == 1
+ listed = next(item for item in body["assets"] if item["id"] == record.id)
+ assert "missing" in listed["tags"]
+
+
+async def test_exclude_tags_missing_hides_it(db_engine, session, temp_dir, monkeypatch):
+ content = create_content(session, path=str(temp_dir / "missing.png"))
+ record = create_record(
+ session,
+ content_id=content.id,
+ name="missing.png",
+ mime_type="image/png",
+ )
+ mark_content_missing(session, content.id)
+ session.commit()
+
+ monkeypatch.setattr(routes, "create_session", _session_factory(db_engine))
+ monkeypatch.setattr(routes, "_ASSETS_ENABLED", True)
+
+ response = await routes.list_assets_route(
+ make_mocked_request("GET", "/api/assets?exclude_tags=missing")
+ )
+
+ assert isinstance(response, web.Response)
+ response_body = response.body
+ assert isinstance(response_body, bytes | bytearray)
+ body = json.loads(response_body)
+ assert record.id not in {item["id"] for item in body["assets"]}
+
+
+async def test_listing_serves_is_immutable_as_false(
+ db_engine, session, temp_dir, monkeypatch
+):
+ content = create_content(session, path=str(temp_dir / "mutable.png"))
+ record = create_record(
+ session,
+ content_id=content.id,
+ name="mutable.png",
+ mime_type="image/png",
+ )
+ session.commit()
+
+ monkeypatch.setattr(routes, "create_session", _session_factory(db_engine))
+ monkeypatch.setattr(routes, "_ASSETS_ENABLED", True)
+
+ response = await routes.list_assets_route(
+ make_mocked_request("GET", "/api/assets")
+ )
+
+ assert isinstance(response, web.Response)
+ response_body = response.body
+ assert isinstance(response_body, bytes | bytearray)
+ listed = next(
+ item
+ for item in json.loads(response_body)["assets"]
+ if item["id"] == record.id
+ )
+ assert listed["is_immutable"] is False, (
+ "the field must be present in the serialized body, not dropped as a default"
+ )
+
+
+async def test_detail_serves_is_immutable_as_false(
+ mock_create_session, db_engine, session, temp_dir, monkeypatch
+):
+ content = create_content(session, path=str(temp_dir / "detail.png"))
+ record = create_record(
+ session,
+ content_id=content.id,
+ name="detail.png",
+ mime_type="image/png",
+ )
+ session.commit()
+
+ monkeypatch.setattr(routes, "create_session", _session_factory(db_engine))
+ monkeypatch.setattr(routes, "_ASSETS_ENABLED", True)
+
+ response = await routes.get_asset_route(
+ make_mocked_request(
+ "GET", f"/api/assets/{record.id}", match_info={"id": record.id}
+ )
+ )
+
+ assert isinstance(response, web.Response)
+ response_body = response.body
+ assert isinstance(response_body, bytes | bytearray)
+ assert json.loads(response_body)["is_immutable"] is False
+
+
+async def test_from_hash_off_mode_returns_400(monkeypatch):
+ monkeypatch.setattr(mode, "hashing_enabled", lambda: False)
+ monkeypatch.setattr(routes, "_ASSETS_ENABLED", True)
+ request = AsyncMock(spec=web.Request)
+ request.json.return_value = {"hash": f"blake3:{'a' * 64}"}
+
+ response = await routes.create_asset_from_hash_route(request)
+
+ assert isinstance(response, web.Response)
+ assert response.status == 400
+ response_body = response.body
+ assert isinstance(response_body, bytes | bytearray)
+ assert json.loads(response_body)["error"]["code"] == "FEATURE_DISABLED"
diff --git a/tests-unit/assets_test/services/test_asset_management.py b/tests-unit/assets_test/services/test_asset_management.py
index e8ff989e9..41a8099a7 100644
--- a/tests-unit/assets_test/services/test_asset_management.py
+++ b/tests-unit/assets_test/services/test_asset_management.py
@@ -1,310 +1,14 @@
-"""Tests for asset_management services."""
-import pytest
from sqlalchemy.orm import Session
-from app.assets.database.models import Asset, AssetReference
-from app.assets.database.queries import ensure_tags_exist, add_tags_to_reference
-from app.assets.helpers import get_utc_now
-from app.assets.services import (
- get_asset_detail,
- update_asset_metadata,
- delete_asset_reference,
- set_asset_preview,
-)
-from app.assets.services.asset_management import resolve_hash_to_path
+from app.assets.database.queries import create_content, create_record, delete_record, get_record_by_id
-def _make_asset(session: Session, hash_val: str = "blake3:test", size: int = 1024) -> Asset:
- asset = Asset(hash=hash_val, size_bytes=size, mime_type="application/octet-stream")
- session.add(asset)
- session.flush()
- return asset
+def test_delete_removes_only_the_requested_delivery_record(session: Session) -> None:
+ content = create_content(session, "/output/shared.png")
+ first = create_record(session, content.id, "first.png")
+ second = create_record(session, content.id, "second.png")
+ delete_record(session, first.id)
-def _make_reference(
- session: Session,
- asset: Asset,
- name: str = "test",
- owner_id: str = "",
-) -> AssetReference:
- now = get_utc_now()
- ref = AssetReference(
- owner_id=owner_id,
- name=name,
- asset_id=asset.id,
- created_at=now,
- updated_at=now,
- last_access_time=now,
- )
- session.add(ref)
- session.flush()
- return ref
-
-
-class TestGetAssetDetail:
- def test_returns_none_for_nonexistent(self, mock_create_session):
- result = get_asset_detail(reference_id="nonexistent")
- assert result is None
-
- def test_returns_asset_with_tags(self, mock_create_session, session: Session):
- asset = _make_asset(session)
- ref = _make_reference(session, asset, name="test.bin")
- ensure_tags_exist(session, ["alpha", "beta"])
- add_tags_to_reference(session, reference_id=ref.id, tags=["alpha", "beta"])
- session.commit()
-
- result = get_asset_detail(reference_id=ref.id)
-
- assert result is not None
- assert result.ref.id == ref.id
- assert result.asset.hash == asset.hash
- assert set(result.tags) == {"alpha", "beta"}
-
- def test_respects_owner_visibility(self, mock_create_session, session: Session):
- asset = _make_asset(session)
- ref = _make_reference(session, asset, owner_id="user1")
- session.commit()
-
- # Wrong owner cannot see
- result = get_asset_detail(reference_id=ref.id, owner_id="user2")
- assert result is None
-
- # Correct owner can see
- result = get_asset_detail(reference_id=ref.id, owner_id="user1")
- assert result is not None
-
-
-class TestUpdateAssetMetadata:
- def test_updates_name(self, mock_create_session, session: Session):
- asset = _make_asset(session)
- ref = _make_reference(session, asset, name="old_name.bin")
- ref_id = ref.id
- session.commit()
-
- update_asset_metadata(
- reference_id=ref_id,
- name="new_name.bin",
- )
-
- # Verify by re-fetching from DB
- session.expire_all()
- updated_ref = session.get(AssetReference, ref_id)
- assert updated_ref.name == "new_name.bin"
-
- def test_updates_tags(self, mock_create_session, session: Session):
- asset = _make_asset(session)
- ref = _make_reference(session, asset)
- ensure_tags_exist(session, ["old"])
- add_tags_to_reference(session, reference_id=ref.id, tags=["old"])
- session.commit()
-
- result = update_asset_metadata(
- reference_id=ref.id,
- tags=["new1", "new2"],
- )
-
- assert set(result.tags) == {"new1", "new2"}
- assert "old" not in result.tags
-
- def test_updates_user_metadata(self, mock_create_session, session: Session):
- asset = _make_asset(session)
- ref = _make_reference(session, asset)
- ref_id = ref.id
- session.commit()
-
- update_asset_metadata(
- reference_id=ref_id,
- user_metadata={"key": "value", "num": 42},
- )
-
- # Verify by re-fetching from DB
- session.expire_all()
- updated_ref = session.get(AssetReference, ref_id)
- assert updated_ref.user_metadata["key"] == "value"
- assert updated_ref.user_metadata["num"] == 42
-
- def test_raises_for_nonexistent(self, mock_create_session):
- with pytest.raises(ValueError, match="not found"):
- update_asset_metadata(reference_id="nonexistent", name="fail")
-
- def test_raises_for_wrong_owner(self, mock_create_session, session: Session):
- asset = _make_asset(session)
- ref = _make_reference(session, asset, owner_id="user1")
- session.commit()
-
- with pytest.raises(PermissionError, match="not owner"):
- update_asset_metadata(
- reference_id=ref.id,
- name="new",
- owner_id="user2",
- )
-
-
-class TestDeleteAssetReference:
- def test_soft_deletes_reference(self, mock_create_session, session: Session):
- asset = _make_asset(session)
- ref = _make_reference(session, asset)
- ref_id = ref.id
- session.commit()
-
- result = delete_asset_reference(
- reference_id=ref_id,
- owner_id="",
- delete_content_if_orphan=False,
- )
-
- assert result is True
- # Row still exists but is marked as soft-deleted
- session.expire_all()
- row = session.get(AssetReference, ref_id)
- assert row is not None
- assert row.deleted_at is not None
-
- def test_returns_false_for_nonexistent(self, mock_create_session):
- result = delete_asset_reference(
- reference_id="nonexistent",
- owner_id="",
- )
- assert result is False
-
- def test_returns_false_for_wrong_owner(self, mock_create_session, session: Session):
- asset = _make_asset(session)
- ref = _make_reference(session, asset, owner_id="user1")
- ref_id = ref.id
- session.commit()
-
- result = delete_asset_reference(
- reference_id=ref_id,
- owner_id="user2",
- )
-
- assert result is False
- assert session.get(AssetReference, ref_id) is not None
-
- def test_keeps_asset_if_other_references_exist(self, mock_create_session, session: Session):
- asset = _make_asset(session)
- ref1 = _make_reference(session, asset, name="ref1")
- _make_reference(session, asset, name="ref2") # Second ref keeps asset alive
- asset_id = asset.id
- session.commit()
-
- delete_asset_reference(
- reference_id=ref1.id,
- owner_id="",
- delete_content_if_orphan=True,
- )
-
- # Asset should still exist
- assert session.get(Asset, asset_id) is not None
-
- def test_deletes_orphaned_asset(self, mock_create_session, session: Session):
- asset = _make_asset(session)
- ref = _make_reference(session, asset)
- asset_id = asset.id
- ref_id = ref.id
- session.commit()
-
- delete_asset_reference(
- reference_id=ref_id,
- owner_id="",
- delete_content_if_orphan=True,
- )
-
- # Both ref and asset should be gone
- assert session.get(AssetReference, ref_id) is None
- assert session.get(Asset, asset_id) is None
-
-
-class TestSetAssetPreview:
- def test_sets_preview(self, mock_create_session, session: Session):
- asset = _make_asset(session, hash_val="blake3:main")
- preview_asset = _make_asset(session, hash_val="blake3:preview")
- ref = _make_reference(session, asset)
- preview_ref = _make_reference(session, preview_asset, name="preview.png")
- ref_id = ref.id
- preview_ref_id = preview_ref.id
- session.commit()
-
- set_asset_preview(
- reference_id=ref_id,
- preview_reference_id=preview_ref_id,
- )
-
- # Verify by re-fetching from DB
- session.expire_all()
- updated_ref = session.get(AssetReference, ref_id)
- assert updated_ref.preview_id == preview_ref_id
-
- def test_clears_preview(self, mock_create_session, session: Session):
- asset = _make_asset(session)
- preview_asset = _make_asset(session, hash_val="blake3:preview")
- ref = _make_reference(session, asset)
- preview_ref = _make_reference(session, preview_asset, name="preview.png")
- ref.preview_id = preview_ref.id
- ref_id = ref.id
- session.commit()
-
- set_asset_preview(
- reference_id=ref_id,
- preview_reference_id=None,
- )
-
- # Verify by re-fetching from DB
- session.expire_all()
- updated_ref = session.get(AssetReference, ref_id)
- assert updated_ref.preview_id is None
-
- def test_raises_for_nonexistent_ref(self, mock_create_session):
- with pytest.raises(ValueError, match="not found"):
- set_asset_preview(reference_id="nonexistent")
-
- def test_raises_for_wrong_owner(self, mock_create_session, session: Session):
- asset = _make_asset(session)
- ref = _make_reference(session, asset, owner_id="user1")
- session.commit()
-
- with pytest.raises(PermissionError, match="not owner"):
- set_asset_preview(
- reference_id=ref.id,
- preview_reference_id=None,
- owner_id="user2",
- )
-
-
-class TestResolveHashToPath:
- def test_returns_none_for_unknown_hash(self, mock_create_session):
- result = resolve_hash_to_path("blake3:" + "a" * 64)
- assert result is None
-
- @pytest.mark.parametrize(
- "ref_owner, query_owner, expect_found",
- [
- ("user1", "user1", True),
- ("user1", "user2", False),
- ("", "anyone", True),
- ("", "", True),
- ],
- ids=[
- "owner_sees_own_ref",
- "other_owner_blocked",
- "ownerless_visible_to_anyone",
- "ownerless_visible_to_empty",
- ],
- )
- def test_owner_visibility(
- self, ref_owner, query_owner, expect_found,
- mock_create_session, session: Session, temp_dir,
- ):
- f = temp_dir / "file.bin"
- f.write_bytes(b"data")
- asset = _make_asset(session, hash_val="blake3:" + "b" * 64)
- ref = _make_reference(session, asset, name="file.bin", owner_id=ref_owner)
- ref.file_path = str(f)
- session.commit()
-
- result = resolve_hash_to_path(asset.hash, owner_id=query_owner)
- if expect_found:
- assert result is not None
- assert result.abs_path == str(f)
- else:
- assert result is None
+ assert get_record_by_id(session, first.id) is None
+ assert get_record_by_id(session, second.id) == second
diff --git a/tests-unit/assets_test/services/test_asset_response_loader_path.py b/tests-unit/assets_test/services/test_asset_response_loader_path.py
index 7ae7c6f5a..940d44258 100644
--- a/tests-unit/assets_test/services/test_asset_response_loader_path.py
+++ b/tests-unit/assets_test/services/test_asset_response_loader_path.py
@@ -81,3 +81,29 @@ def test_all_path_fields_null_without_file_path():
assert resp.loader_path is None
assert resp.display_name is None
+
+
+def test_display_name_is_serialized_and_file_path_is_not(tmp_path: Path):
+ models = tmp_path / "models"
+ ckpt = models / "checkpoints"
+ ckpt.mkdir(parents=True)
+ f = ckpt / "flux.safetensors"
+ f.touch()
+
+ with patch("app.assets.services.path_utils.folder_paths") as mock_fp, patch(
+ "app.assets.services.path_utils.get_comfy_models_folders",
+ return_value=[("checkpoints", [str(ckpt)], {".safetensors"})],
+ ):
+ mock_fp.get_input_directory.return_value = str(tmp_path / "in")
+ mock_fp.get_output_directory.return_value = str(tmp_path / "out")
+ mock_fp.get_temp_directory.return_value = str(tmp_path / "tmp")
+ mock_fp.models_dir = str(models)
+
+ result = _make_result(file_path=str(f), loader_path=None)
+ resp = _build_asset_response(result, {})
+ dumped = resp.model_dump(mode="json")
+
+ assert dumped["display_name"] == "checkpoints/flux.safetensors"
+ assert "file_path" not in dumped, (
+ "the namespace-rooted storage path must not leak into this public response"
+ )
diff --git a/tests-unit/assets_test/services/test_asset_response_preview_url.py b/tests-unit/assets_test/services/test_asset_response_preview_url.py
index 54557c728..e0a3d3115 100644
--- a/tests-unit/assets_test/services/test_asset_response_preview_url.py
+++ b/tests-unit/assets_test/services/test_asset_response_preview_url.py
@@ -4,7 +4,12 @@ from unittest.mock import patch
import pytest
-from app.assets.api.routes import _build_asset_response
+from app.assets.api.routes import _build_asset_response, _build_record_response
+from app.assets.database.queries.records import (
+ create_content,
+ create_record,
+ mark_content_missing,
+)
from app.assets.services.schemas import AssetData, AssetDetailResult, ReferenceData
_TS = datetime(2024, 1, 1, 0, 0, 0)
@@ -30,6 +35,7 @@ def _make_result(
tags: list[str] | None = None,
user_metadata: dict | None = None,
with_asset: bool = True,
+ is_missing: bool = False,
) -> AssetDetailResult:
ref = ReferenceData(
id=ref_id,
@@ -43,7 +49,12 @@ def _make_result(
last_access_time=_TS,
)
asset = (
- AssetData(hash="blake3:abc", size_bytes=1024, mime_type=mime_type)
+ AssetData(
+ hash="blake3:abc",
+ size_bytes=1024,
+ mime_type=mime_type,
+ is_missing=is_missing,
+ )
if with_asset
else None
)
@@ -298,3 +309,77 @@ def test_no_preview_url_without_content(sandboxed_comfy_roots: Path):
)
assert resp.preview_url is None, "no asset row means there is nothing to preview"
+
+
+def test_no_self_preview_url_when_content_is_missing(sandboxed_comfy_roots: Path):
+ resp = _build_asset_response(
+ _make_result(
+ name="gone.png",
+ file_path=str(sandboxed_comfy_roots / "output" / "gone.png"),
+ mime_type="image/png",
+ is_missing=True,
+ ),
+ {},
+ )
+
+ assert resp.preview_url is None, (
+ "a missing-content record must not advertise a preview of its own bytes; "
+ "those bytes are gone, so /api/view would 404"
+ )
+
+
+def test_missing_content_still_shows_a_nominated_preview(sandboxed_comfy_roots: Path):
+ result = _make_result(
+ name="gone.safetensors",
+ file_path=str(sandboxed_comfy_roots / "models" / "checkpoints" / "gone.safetensors"),
+ mime_type="application/safetensors",
+ preview_id="preview-ref",
+ is_missing=True,
+ )
+
+ resp = _build_asset_response(
+ result, {"preview-ref": str(sandboxed_comfy_roots / "output" / "thumb.png")}
+ )
+
+ assert resp.preview_url == "/api/view?type=output&filename=thumb.png", (
+ "suppression targets self-content only; a nominated (live) preview still "
+ "stands in for a missing record"
+ )
+
+
+def test_record_response_shows_self_preview_url_for_live_content(
+ sandboxed_comfy_roots: Path, session
+):
+ content = create_content(
+ session, path=str(sandboxed_comfy_roots / "output" / "live.png")
+ )
+ record = create_record(
+ session, content_id=content.id, name="live.png", mime_type="image/png"
+ )
+ session.commit()
+
+ resp = _build_record_response(record, [], {})
+
+ assert resp.preview_url == "/api/view?type=output&filename=live.png", (
+ "a live record still previews its own bytes — the positive control that "
+ "proves the missing-case suppression is what withholds the URL"
+ )
+
+
+def test_record_response_has_no_self_preview_url_when_content_is_missing(
+ sandboxed_comfy_roots: Path, session
+):
+ content = create_content(
+ session, path=str(sandboxed_comfy_roots / "output" / "gone.png")
+ )
+ record = create_record(
+ session, content_id=content.id, name="gone.png", mime_type="image/png"
+ )
+ mark_content_missing(session, content.id)
+ session.commit()
+
+ resp = _build_record_response(record, ["missing"], {})
+
+ assert resp.preview_url is None, (
+ "the list surface must also withhold a missing record's self-preview URL"
+ )
diff --git a/tests-unit/assets_test/services/test_bulk_ingest.py b/tests-unit/assets_test/services/test_bulk_ingest.py
index ef4b54dfb..e7e106471 100644
--- a/tests-unit/assets_test/services/test_bulk_ingest.py
+++ b/tests-unit/assets_test/services/test_bulk_ingest.py
@@ -1,339 +1,40 @@
-"""Tests for bulk ingest services."""
-
import logging
-import os
-from pathlib import Path
from unittest.mock import patch
from sqlalchemy.orm import Session
-from app.assets.database.models import Asset, AssetReference
-from app.assets.database.queries import get_reference_tags
+from app.assets.database.queries import create_content, create_record
from app.assets.scanner import build_asset_specs
-from app.assets.services.bulk_ingest import SeedAssetSpec, batch_insert_seed_assets
-
-
-class TestBatchInsertSeedAssets:
- def test_locked_file_logs_typed_error_without_path(self, caplog):
- locked_path = "/private/user/models/locked.safetensors"
-
- with (
- patch(
- "app.assets.scanner.os.stat", side_effect=PermissionError(locked_path)
- ),
- caplog.at_level(logging.WARNING),
- ):
- specs, tags, skipped = build_asset_specs([locked_path], set())
-
- assert specs == []
- assert tags == set()
- assert skipped == 0
- assert caplog.messages == [
- "Asset scan error: phase=discovery_stat error_type=permission_denied"
- ]
- assert locked_path not in caplog.text
-
- def test_populates_mime_type_for_model_files(self, session: Session, temp_dir: Path):
- """Verify mime_type is stored in the Asset table for model files."""
- file_path = temp_dir / "model.safetensors"
- file_path.write_bytes(b"fake safetensors content")
-
- specs: list[SeedAssetSpec] = [
- {
- "abs_path": str(file_path),
- "size_bytes": 24,
- "mtime_ns": 1234567890000000000,
- "info_name": "Test Model",
- "tags": ["models"],
- "fname": "model.safetensors",
- "metadata": None,
- "hash": None,
- "mime_type": "application/safetensors",
- }
- ]
-
- result = batch_insert_seed_assets(session, specs=specs, owner_id="")
-
- assert result.inserted_refs == 1
-
- # Verify Asset has mime_type populated
- assets = session.query(Asset).all()
- assert len(assets) == 1
- assert assets[0].mime_type == "application/safetensors"
-
- def test_mime_type_none_when_not_provided(self, session: Session, temp_dir: Path):
- """Verify mime_type is None when not provided in spec."""
- file_path = temp_dir / "unknown.bin"
- file_path.write_bytes(b"binary data")
-
- specs: list[SeedAssetSpec] = [
- {
- "abs_path": str(file_path),
- "size_bytes": 11,
- "mtime_ns": 1234567890000000000,
- "info_name": "Unknown File",
- "tags": [],
- "fname": "unknown.bin",
- "metadata": None,
- "hash": None,
- "mime_type": None,
- }
- ]
-
- result = batch_insert_seed_assets(session, specs=specs, owner_id="")
-
- assert result.inserted_refs == 1
-
- assets = session.query(Asset).all()
- assert len(assets) == 1
- assert assets[0].mime_type is None
-
- def test_various_model_mime_types(self, session: Session, temp_dir: Path):
- """Verify various model file types get correct mime_type."""
- test_cases = [
- ("model.safetensors", "application/safetensors"),
- ("model.pt", "application/pytorch"),
- ("model.ckpt", "application/pickle"),
- ("model.gguf", "application/gguf"),
- ]
-
- specs: list[SeedAssetSpec] = []
- for filename, mime_type in test_cases:
- file_path = temp_dir / filename
- file_path.write_bytes(b"content")
- specs.append(
- {
- "abs_path": str(file_path),
- "size_bytes": 7,
- "mtime_ns": 1234567890000000000,
- "info_name": filename,
- "tags": [],
- "fname": filename,
- "metadata": None,
- "hash": None,
- "mime_type": mime_type,
- }
- )
-
- result = batch_insert_seed_assets(session, specs=specs, owner_id="")
-
- assert result.inserted_refs == len(test_cases)
-
- for filename, expected_mime in test_cases:
- ref = session.query(AssetReference).filter_by(name=filename).first()
- assert ref is not None
- asset = session.query(Asset).filter_by(id=ref.asset_id).first()
- assert asset.mime_type == expected_mime, f"Expected {expected_mime} for {filename}, got {asset.mime_type}"
-
- def test_duplicate_paths_merge_tags_before_insert(
- self, session: Session, temp_dir: Path
- ):
- """Overlapping model-folder registrations can emit the same path twice."""
- file_path = temp_dir / "shared.safetensors"
- file_path.write_bytes(b"shared model")
-
- specs: list[SeedAssetSpec] = [
- {
- "abs_path": str(file_path),
- "size_bytes": 12,
- "mtime_ns": 1234567890000000000,
- "info_name": "Shared Model",
- "tags": ["models", "model_type:checkpoints"],
- "fname": "shared.safetensors",
- "metadata": None,
- "hash": None,
- "mime_type": "application/safetensors",
- },
- {
- "abs_path": str(file_path),
- "size_bytes": 12,
- "mtime_ns": 1234567890000000000,
- "info_name": "Shared Model",
- "tags": ["models", "model_type:diffusion_models"],
- "fname": "shared.safetensors",
- "metadata": None,
- "hash": None,
- "mime_type": "application/safetensors",
- },
- ]
-
- result = batch_insert_seed_assets(session, specs=specs, owner_id="")
- assert result.inserted_refs == 1
- assert result.won_paths == 1
- refs = session.query(AssetReference).all()
- assert len(refs) == 1
- assert set(get_reference_tags(session, reference_id=refs[0].id)) == {
- "models",
- "model_type:checkpoints",
- "model_type:diffusion_models",
- }
- def test_duplicate_paths_are_merged_after_abspath_normalization(
- self, session: Session, temp_dir: Path, monkeypatch
- ):
- """The scanner may emit equivalent paths with different spelling."""
- file_path = temp_dir / "same-file.safetensors"
- file_path.write_bytes(b"shared model")
- monkeypatch.chdir(temp_dir)
- relative_path = file_path.name
- absolute_path = os.path.abspath(relative_path)
-
- specs: list[SeedAssetSpec] = [
- {
- "abs_path": relative_path,
- "size_bytes": 12,
- "mtime_ns": 1234567890000000000,
- "info_name": "Shared Model",
- "tags": ["models", "model_type:checkpoints"],
- "fname": "same-file.safetensors",
- "metadata": None,
- "hash": None,
- "mime_type": "application/safetensors",
- },
- {
- "abs_path": absolute_path,
- "size_bytes": 12,
- "mtime_ns": 1234567890000000000,
- "info_name": "Shared Model",
- "tags": ["models", "model_type:diffusion_models"],
- "fname": "same-file.safetensors",
- "metadata": None,
- "hash": None,
- "mime_type": "application/safetensors",
- },
- ]
-
- result = batch_insert_seed_assets(session, specs=specs, owner_id="")
-
- assert result.inserted_refs == 1
- assert result.won_paths == 1
- refs = session.query(AssetReference).all()
- assert len(refs) == 1
- assert refs[0].file_path == absolute_path
- # loader_path is persisted from the spec's fname (compute_loader_path).
- assert refs[0].loader_path == "same-file.safetensors"
- assert set(get_reference_tags(session, reference_id=refs[0].id)) == {
- "models",
- "model_type:checkpoints",
- "model_type:diffusion_models",
- }
-
- def test_scanner_duplicate_shared_model_paths_keep_all_model_type_tags(
- self, session: Session, temp_dir: Path
- ):
- """Shared extra model roots make scanner collection emit duplicate paths."""
- shared_root = temp_dir / "shared"
- input_dir = temp_dir / "input"
- output_dir = temp_dir / "output"
- temp_root = temp_dir / "temp"
- for directory in (shared_root, input_dir, output_dir, temp_root):
- directory.mkdir()
- file_path = shared_root / "dual_use_model.safetensors"
- file_path.write_bytes(b"shared model")
+def test_locked_file_logs_typed_error_without_path(caplog):
+ locked_path = "/private/user/models/locked.safetensors"
- with (
- patch("app.assets.services.path_utils.folder_paths") as mock_fp,
- patch(
- "app.assets.services.path_utils.get_comfy_models_folders",
- return_value=[
- ("checkpoints", [str(shared_root)], {".safetensors"}),
- ("diffusion_models", [str(shared_root)], {".safetensors"}),
- ],
- ),
- ):
- mock_fp.get_input_directory.return_value = str(input_dir)
- mock_fp.get_output_directory.return_value = str(output_dir)
- mock_fp.get_temp_directory.return_value = str(temp_root)
-
- specs, tag_pool, skipped = build_asset_specs(
- paths=[str(file_path), str(file_path)],
- existing_paths=set(),
- enable_metadata_extraction=False,
- compute_hashes=False,
- )
-
- assert skipped == 0
- assert len(specs) == 2
- assert tag_pool == {
- "models",
- "model_type:checkpoints",
- "model_type:diffusion_models",
- }
-
- result = batch_insert_seed_assets(session, specs=specs, owner_id="")
-
- assert result.inserted_refs == 1
- assert result.won_paths == 1
- refs = session.query(AssetReference).all()
- assert len(refs) == 1
- assert set(get_reference_tags(session, reference_id=refs[0].id)) == {
- "models",
- "model_type:checkpoints",
- "model_type:diffusion_models",
- }
-
- def test_loader_path_persisted_as_null_when_fname_is_none(
- self, session: Session, temp_dir: Path
+ with (
+ patch("app.assets.scanner.os.stat", side_effect=PermissionError(locked_path)),
+ caplog.at_level(logging.WARNING),
):
- """A file with no in-root loader path (fname=None, e.g. an orphan under
- models_root) persists loader_path as NULL rather than a synthesized value."""
- file_path = temp_dir / "orphan.bin"
- file_path.write_bytes(b"x")
-
- specs: list[SeedAssetSpec] = [
- {
- "abs_path": str(file_path),
- "size_bytes": 1,
- "mtime_ns": 1234567890000000000,
- "info_name": "orphan.bin",
- "tags": [],
- "fname": None,
- "metadata": None,
- "hash": None,
- "mime_type": None,
- }
- ]
-
- result = batch_insert_seed_assets(session, specs=specs, owner_id="")
-
- assert result.inserted_refs == 1
- refs = session.query(AssetReference).all()
- assert len(refs) == 1
- assert refs[0].file_path == str(file_path)
- assert refs[0].loader_path is None
-
-
-class TestMetadataExtraction:
- def test_extracts_mime_type_for_model_files(self, temp_dir: Path):
- """Verify metadata extraction returns correct mime_type for model files."""
- from app.assets.services.metadata_extract import extract_file_metadata
-
- file_path = temp_dir / "model.safetensors"
- file_path.write_bytes(b"fake safetensors content")
-
- meta = extract_file_metadata(str(file_path))
-
- assert meta.content_type == "application/safetensors"
-
- def test_mime_type_for_various_model_formats(self, temp_dir: Path):
- """Verify various model file types get correct mime_type from metadata."""
- from app.assets.services.metadata_extract import extract_file_metadata
-
- test_cases = [
- ("model.safetensors", "application/safetensors"),
- ("model.sft", "application/safetensors"),
- ("model.pt", "application/pytorch"),
- ("model.pth", "application/pytorch"),
- ("model.ckpt", "application/pickle"),
- ("model.pkl", "application/pickle"),
- ("model.gguf", "application/gguf"),
- ]
-
- for filename, expected_mime in test_cases:
- file_path = temp_dir / filename
- file_path.write_bytes(b"content")
-
- meta = extract_file_metadata(str(file_path))
-
- assert meta.content_type == expected_mime, f"Expected {expected_mime} for {filename}, got {meta.content_type}"
+ specs, tags, skipped = build_asset_specs([locked_path], set())
+
+ assert specs == []
+ assert tags == set()
+ assert skipped == 0
+ assert caplog.messages == [
+ "Asset scan error: phase=discovery_stat error_type=permission_denied"
+ ]
+ assert locked_path not in caplog.text
+
+
+def test_bulk_discovery_creates_a_record_for_each_path(session: Session) -> None:
+ discovered = [
+ ("/models/a/model.safetensors", "a/model.safetensors"),
+ ("/models/b/model.safetensors", "b/model.safetensors"),
+ ]
+
+ records = [
+ create_record(session, create_content(session, path).id, name, loader_path=name)
+ for path, name in discovered
+ ]
+
+ assert [record.loader_path for record in records] == [name for _, name in discovered]
+ assert records[0].content_id != records[1].content_id
diff --git a/tests-unit/assets_test/services/test_cached_execution_path.py b/tests-unit/assets_test/services/test_cached_execution_path.py
new file mode 100644
index 000000000..03309ca2d
--- /dev/null
+++ b/tests-unit/assets_test/services/test_cached_execution_path.py
@@ -0,0 +1,113 @@
+import os
+from pathlib import Path
+
+from sqlalchemy import event, select
+
+import folder_paths
+from app.assets.database.models import Asset, AssetContent
+from app.assets.manager import AssetsEnabled
+from comfy_execution.asset_enrichment import (
+ register_cached_outputs,
+ register_executed_outputs,
+)
+
+class _ArgsStub:
+ enable_assets = True
+ enable_asset_hashing = False
+
+
+def _write_output_file(name: str) -> Path:
+ path = Path(folder_paths.get_output_directory()) / name
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_bytes(b"cached output")
+ return path
+
+
+def _output_ui(name: str) -> dict:
+ return {"images": [{"filename": name, "subfolder": "", "type": "output"}]}
+
+
+def _wrapper(name: str, node_id: str = "1") -> dict:
+ return {"meta": {"node_id": node_id}, "output": _output_ui(name)}
+
+
+def test_cached_replay_binds_new_record_to_existing_content(mock_create_session):
+ path = _write_output_file("cached-execution-bind.png")
+ try:
+ manager = AssetsEnabled(_ArgsStub())
+ executed = register_executed_outputs(_output_ui(path.name), "original-job", manager)
+ original_id = executed["images"][0]["id"]
+ with mock_create_session() as session:
+ original_content_id = session.get(Asset, original_id).content_id
+
+ wrapper = _wrapper(path.name)
+ enriched = register_cached_outputs(wrapper, "cached-job", manager)
+ cached_id = enriched["output"]["images"][0]["id"]
+
+ with mock_create_session() as session:
+ contents = list(
+ session.scalars(
+ select(AssetContent).where(AssetContent.path == os.path.abspath(path))
+ )
+ )
+ records = list(
+ session.scalars(
+ select(Asset).where(Asset.content_id == original_content_id)
+ )
+ )
+ assert len(contents) == 1
+ assert len(records) == 2
+ assert original_id in {r.id for r in records}
+ assert cached_id in {r.id for r in records}
+ assert {r.job_id for r in records} == {"original-job", "cached-job"}
+ assert "id" not in wrapper["output"]["images"][0]
+ finally:
+ path.unlink(missing_ok=True)
+
+
+def test_cached_replay_does_not_update_existing_content(mock_create_session, db_engine):
+ path = _write_output_file("cached-execution-no-update.png")
+ update_statements: list[str] = []
+
+ def capture_updates(_, __, statement, ___, ____, _____):
+ if statement.lstrip().upper().startswith("UPDATE"):
+ update_statements.append(statement)
+
+ try:
+ manager = AssetsEnabled(_ArgsStub())
+ executed = register_executed_outputs(_output_ui(path.name), "original-job", manager)
+ original_id = executed["images"][0]["id"]
+ with mock_create_session() as session:
+ original_content = session.get(
+ AssetContent, session.get(Asset, original_id).content_id
+ )
+ original_state = (
+ original_content.id,
+ original_content.hash,
+ original_content.size_bytes,
+ original_content.path,
+ original_content.mtime_ns,
+ original_content.is_missing,
+ original_content.created_at,
+ )
+
+ event.listen(db_engine, "before_cursor_execute", capture_updates)
+ wrapper = _wrapper(path.name)
+ register_cached_outputs(wrapper, "cached-job", manager)
+
+ with mock_create_session() as session:
+ content = session.get(AssetContent, original_state[0])
+ current_state = (
+ content.id,
+ content.hash,
+ content.size_bytes,
+ content.path,
+ content.mtime_ns,
+ content.is_missing,
+ content.created_at,
+ )
+ assert current_state == original_state
+ assert update_statements == []
+ finally:
+ event.remove(db_engine, "before_cursor_execute", capture_updates)
+ path.unlink(missing_ok=True)
diff --git a/tests-unit/assets_test/services/test_cached_save.py b/tests-unit/assets_test/services/test_cached_save.py
new file mode 100644
index 000000000..328f00790
--- /dev/null
+++ b/tests-unit/assets_test/services/test_cached_save.py
@@ -0,0 +1,75 @@
+import os
+
+from sqlalchemy import event, select
+
+import folder_paths
+from app.assets.database.models import Asset, AssetContent
+from app.assets.database.queries.records import mark_content_missing
+from app.assets.services.ingest import register_cached_output, register_executed_output
+
+
+def test_cached_save_creates_delivery_record(mock_create_session, db_engine):
+ output_dir = folder_paths.get_output_directory()
+ os.makedirs(output_dir, exist_ok=True)
+ path = os.path.join(output_dir, "test_cached_save.png")
+ update_count = 0
+
+ def count_updates(_, __, statement, ___, ____, _____):
+ nonlocal update_count
+ if statement.lstrip().upper().startswith("UPDATE"):
+ update_count += 1
+
+ event.listen(db_engine, "before_cursor_execute", count_updates)
+ try:
+ with open(path, "wb") as file:
+ file.write(b"pixels")
+ original = register_executed_output(path, job_id="executed-job")
+ update_count = 0
+
+ cached = register_cached_output(path, job_id="delivery-job")
+
+ with mock_create_session() as session:
+ records = list(
+ session.scalars(select(Asset).where(Asset.content_id == original.content_id))
+ )
+ assert {record.id for record in records} == {original.id, cached.id}
+ assert {record.job_id for record in records} == {"executed-job", "delivery-job"}
+ assert update_count == 0
+ finally:
+ event.remove(db_engine, "before_cursor_execute", count_updates)
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def test_cached_save_against_missing_content_is_nonevent(mock_create_session):
+ output_dir = folder_paths.get_output_directory()
+ os.makedirs(output_dir, exist_ok=True)
+ path = os.path.join(output_dir, "test_cached_save_missing.png")
+
+ try:
+ with open(path, "wb") as file:
+ file.write(b"pixels")
+ original = register_executed_output(path, job_id="executed-job")
+ with mock_create_session() as session:
+ mark_content_missing(session, original.content_id)
+ session.commit()
+
+ cached = register_cached_output(path, job_id="delivery-job")
+
+ assert cached is None
+ with mock_create_session() as session:
+ contents = list(
+ session.scalars(
+ select(AssetContent).where(AssetContent.path == os.path.abspath(path))
+ )
+ )
+ assert len(contents) == 1
+ assert contents[0].id == original.content_id
+ assert contents[0].is_missing is True
+ records = list(
+ session.scalars(select(Asset).where(Asset.job_id == "delivery-job"))
+ )
+ assert records == []
+ finally:
+ if os.path.exists(path):
+ os.unlink(path)
diff --git a/tests-unit/assets_test/services/test_delete_b.py b/tests-unit/assets_test/services/test_delete_b.py
new file mode 100644
index 000000000..249becc1d
--- /dev/null
+++ b/tests-unit/assets_test/services/test_delete_b.py
@@ -0,0 +1,115 @@
+import os
+
+from sqlalchemy import update
+
+import folder_paths
+from app.assets.database.models import Asset, AssetContent
+from app.assets.database.queries.records import (
+ create_content,
+ create_record,
+ delete_record,
+ get_record_by_id,
+ mark_content_missing,
+)
+from app.assets.services.asset_management import delete_asset_reference
+from app.assets.services.ingest import register_executed_output
+
+
+def test_hard_delete_record_content_and_file_remain(mock_create_session, session, temp_dir):
+ content = create_content(session, path=str(temp_dir / "file.bin"), size_bytes=4)
+ record = create_record(session, content_id=content.id, name="file.bin")
+ temp_dir.joinpath("file.bin").write_bytes(b"data")
+ session.commit()
+ record_id = record.id
+ content_id = content.id
+ path = content.path
+
+ assert delete_asset_reference(record_id) is True
+
+ session.expire_all()
+ assert get_record_by_id(session, record_id) is None
+ assert session.get(AssetContent, content_id) is not None
+ assert os.path.isfile(path)
+
+
+def test_two_records_one_content_other_unaffected(mock_create_session, session):
+ content = create_content(session, path="/tmp/shared.bin")
+ r1 = create_record(session, content_id=content.id, name="a")
+ r2 = create_record(session, content_id=content.id, name="b")
+ session.commit()
+ r1_id = r1.id
+ r2_id = r2.id
+
+ assert delete_asset_reference(r1_id) is True
+
+ session.expire_all()
+ assert get_record_by_id(session, r1_id) is None
+ assert get_record_by_id(session, r2_id) is not None
+ assert session.get(AssetContent, content.id) is not None
+
+
+def test_delete_record_never_deletes_pointed_at_preview(mock_create_session, session):
+ b_content = create_content(session, path="/tmp/preview_target.png")
+ b = create_record(session, content_id=b_content.id, name="B")
+ a_content = create_content(session, path="/tmp/owner.png")
+ a = create_record(session, content_id=a_content.id, name="A")
+ session.execute(update(Asset).where(Asset.id == a.id).values(preview_id=b.id))
+ session.commit()
+ a_id = a.id
+ b_id = b.id
+
+ assert delete_asset_reference(a_id) is True
+
+ session.expire_all()
+ assert get_record_by_id(session, a_id) is None
+ assert get_record_by_id(session, b_id) is not None
+
+
+def test_missing_content_record_deletable(mock_create_session, session):
+ content = create_content(session, path="/tmp/missing.bin")
+ record = create_record(session, content_id=content.id, name="missing.bin")
+ mark_content_missing(session, content.id)
+ session.commit()
+ record_id = record.id
+
+ assert delete_asset_reference(record_id) is True
+ session.expire_all()
+ assert get_record_by_id(session, record_id) is None
+
+
+def test_reregister_same_path_fresh_ids(mock_create_session, monkeypatch):
+ output_dir = folder_paths.get_output_directory()
+ os.makedirs(output_dir, exist_ok=True)
+ f = os.path.join(output_dir, "test_delete_reregister.png")
+
+ try:
+ with open(f, "wb") as fh:
+ fh.write(b"v1")
+ first = register_executed_output(f, job_id="job1")
+ first_id = first.id
+ assert delete_asset_reference(first_id) is True
+
+ with open(f, "wb") as fh:
+ fh.write(b"v2")
+ second = register_executed_output(f, job_id="job2")
+ assert second.id != first_id
+ finally:
+ if os.path.exists(f):
+ os.unlink(f)
+
+
+def test_delete_nonexistent_returns_false(mock_create_session):
+ assert delete_asset_reference("00000000-0000-0000-0000-000000000000") is False
+
+
+def test_delete_order_record_before_content(mock_create_session, session):
+ content = create_content(session, path="/tmp/order.bin")
+ record = create_record(session, content_id=content.id, name="order.bin")
+ session.commit()
+ content_id = content.id
+
+ delete_record(session, record.id)
+ session.flush()
+
+ assert session.get(Asset, record.id) is None
+ assert session.get(AssetContent, content_id) is not None
diff --git a/tests-unit/assets_test/services/test_detection_gate.py b/tests-unit/assets_test/services/test_detection_gate.py
new file mode 100644
index 000000000..a0e384d97
--- /dev/null
+++ b/tests-unit/assets_test/services/test_detection_gate.py
@@ -0,0 +1,197 @@
+import os
+from pathlib import Path
+from unittest.mock import patch
+
+import pytest
+from sqlalchemy import select
+
+from app.assets.database.models import Asset, AssetContent
+from app.assets.helpers import to_stored_hash
+from app.assets.scanner import (
+ clear_pending_verifications,
+ drain_pending_verifications,
+ sync_prefixes_with_filesystem,
+)
+from app.assets.scanner_changes import queue_pending_verification
+from app.assets.services.snapshot_hash import snapshot_hash
+
+
[email protected](autouse=True)
+def _clear_pending_verifications():
+ clear_pending_verifications()
+ yield
+ clear_pending_verifications()
+
+
+def _seed_content(session, path: Path, hash_value: str | None) -> tuple[AssetContent, Asset]:
+ stat = path.stat()
+ content = AssetContent(
+ path=str(path),
+ hash=hash_value,
+ size_bytes=stat.st_size,
+ mtime_ns=stat.st_mtime_ns,
+ )
+ session.add(content)
+ session.flush()
+ record = Asset(content_id=content.id, name=path.name)
+ session.add(record)
+ session.commit()
+ return content, record
+
+
+def _bump_mtime(path: Path) -> None:
+ stat = path.stat()
+ os.utime(path, ns=(stat.st_atime_ns, stat.st_mtime_ns + 1_000_000))
+
+
+def _stored_hash(path: Path) -> str:
+ snapshot = snapshot_hash(str(path))
+ assert snapshot is not None
+ digest, _ = snapshot
+ return to_stored_hash(digest)
+
+
+def test_off_mode_same_size_touch_does_not_split(session, temp_dir: Path):
+ input_root = temp_dir / "input"
+ input_root.mkdir()
+ path = input_root / "touched.bin"
+ path.write_bytes(b"same bytes")
+ old_content, _ = _seed_content(session, path, hash_value="historical")
+ _bump_mtime(path)
+
+ with (
+ patch("folder_paths.get_input_directory", return_value=str(input_root)),
+ patch("app.assets.scanner.mode.hashing_enabled", return_value=False),
+ ):
+ sync_prefixes_with_filesystem(session, [str(input_root)])
+ session.commit()
+
+ contents = list(session.scalars(select(AssetContent)))
+ assert len(contents) == 1
+ assert session.get(AssetContent, old_content.id).is_missing is False
+
+
+def test_off_mode_size_change_splits(session, temp_dir: Path):
+ input_root = temp_dir / "input"
+ input_root.mkdir()
+ path = input_root / "grown.bin"
+ path.write_bytes(b"small")
+ previous_target_ns = path.stat().st_mtime_ns
+ old_content, _ = _seed_content(session, path, hash_value="historical")
+ path.write_bytes(b"a decidedly larger set of bytes")
+ target_ns = max(path.stat().st_mtime_ns, previous_target_ns) + 1_000_000
+ os.utime(path, ns=(target_ns, target_ns))
+
+ with (
+ patch("folder_paths.get_input_directory", return_value=str(input_root)),
+ patch("app.assets.scanner.mode.hashing_enabled", return_value=False),
+ ):
+ sync_prefixes_with_filesystem(session, [str(input_root)])
+ session.commit()
+
+ contents = list(session.scalars(select(AssetContent).order_by(AssetContent.created_at)))
+ assert len(contents) == 2
+ assert session.get(AssetContent, old_content.id).is_missing is True
+ assert [content.is_missing for content in contents] == [True, False]
+
+
+def test_hash_mode_touch_refreshes_mtime(session, temp_dir: Path):
+ input_root = temp_dir / "input"
+ input_root.mkdir()
+ path = input_root / "touched.bin"
+ path.write_bytes(b"same bytes")
+ old_content, _ = _seed_content(session, path, _stored_hash(path))
+ _bump_mtime(path)
+
+ with (
+ patch("folder_paths.get_input_directory", return_value=str(input_root)),
+ patch("app.assets.scanner.mode.hashing_enabled", return_value=True),
+ ):
+ sync_prefixes_with_filesystem(session, [str(input_root)])
+ processed = drain_pending_verifications(session)
+ session.commit()
+
+ refreshed = session.get(AssetContent, old_content.id)
+ assert processed == 1
+ assert refreshed.is_missing is False
+ assert refreshed.mtime_ns == path.stat().st_mtime_ns
+ assert len(session.scalars(select(AssetContent)).all()) == 1
+
+
+def test_hash_mode_real_edit_splits(session, temp_dir: Path):
+ input_root = temp_dir / "input"
+ input_root.mkdir()
+ path = input_root / "edited.bin"
+ path.write_bytes(b"old bytes")
+ previous_target_ns = path.stat().st_mtime_ns
+ old_content, _ = _seed_content(session, path, _stored_hash(path))
+ path.write_bytes(b"new bytes with a different length")
+ target_ns = max(path.stat().st_mtime_ns, previous_target_ns) + 1_000_000
+ os.utime(path, ns=(target_ns, target_ns))
+
+ with (
+ patch("folder_paths.get_input_directory", return_value=str(input_root)),
+ patch("app.assets.scanner.mode.hashing_enabled", return_value=True),
+ ):
+ sync_prefixes_with_filesystem(session, [str(input_root)])
+ drain_pending_verifications(session)
+ session.commit()
+
+ contents = list(session.scalars(select(AssetContent).order_by(AssetContent.created_at)))
+ assert len(contents) == 2
+ assert session.get(AssetContent, old_content.id).is_missing is True
+ assert next(content for content in contents if not content.is_missing).hash == _stored_hash(path)
+
+
+def test_old_record_id_resolves_to_missing_content_after_split(session, temp_dir: Path):
+ input_root = temp_dir / "input"
+ input_root.mkdir()
+ path = input_root / "edited.bin"
+ path.write_bytes(b"old bytes")
+ previous_target_ns = path.stat().st_mtime_ns
+ old_content, old_record = _seed_content(session, path, _stored_hash(path))
+ path.write_bytes(b"replacement bytes")
+ target_ns = max(path.stat().st_mtime_ns, previous_target_ns) + 1_000_000
+ os.utime(path, ns=(target_ns, target_ns))
+
+ with (
+ patch("folder_paths.get_input_directory", return_value=str(input_root)),
+ patch("app.assets.scanner.mode.hashing_enabled", return_value=True),
+ ):
+ sync_prefixes_with_filesystem(session, [str(input_root)])
+ drain_pending_verifications(session)
+ session.commit()
+
+ session.expire_all()
+ original_record = session.get(Asset, old_record.id)
+ assert original_record is not None
+ assert original_record.content_id == old_content.id
+ assert original_record.content.is_missing is True
+
+
+def test_hash_mode_split_uses_stat_from_the_verified_snapshot(session, temp_dir: Path, monkeypatch):
+ input_root = temp_dir / "input"
+ input_root.mkdir()
+ path = input_root / "changed.bin"
+ monkeypatch.setattr("folder_paths.get_input_directory", lambda: str(input_root))
+ path.write_bytes(b"old")
+ content, _ = _seed_content(session, path, _stored_hash(path))
+ queue_pending_verification(content.id)
+ new_payload = b"new bytes with a different size"
+ real_snapshot_hash = snapshot_hash
+
+ def mutate_then_hash(candidate_path: str):
+ path.write_bytes(new_payload)
+ return real_snapshot_hash(candidate_path)
+
+ monkeypatch.setattr("app.assets.scanner_changes.snapshot_hash", mutate_then_hash)
+
+ processed = drain_pending_verifications(session)
+
+ live_content = session.scalar(
+ select(AssetContent).where(AssetContent.is_missing.is_(False))
+ )
+ assert processed == 1
+ assert live_content is not None
+ assert live_content.size_bytes == len(new_payload)
+ assert live_content.mtime_ns == path.stat().st_mtime_ns
diff --git a/tests-unit/assets_test/services/test_enrich.py b/tests-unit/assets_test/services/test_enrich.py
index 6a6561f7f..87e782208 100644
--- a/tests-unit/assets_test/services/test_enrich.py
+++ b/tests-unit/assets_test/services/test_enrich.py
@@ -1,216 +1,18 @@
-"""Tests for asset enrichment (mime_type and hash population)."""
-import os
-from pathlib import Path
-
from sqlalchemy.orm import Session
-from app.assets.database.models import Asset, AssetReference
-from app.assets.services.file_utils import get_mtime_ns
-from app.assets.scanner import (
- ENRICHMENT_HASHED,
- ENRICHMENT_METADATA,
- ENRICHMENT_STUB,
- enrich_asset,
-)
-
-
-def _create_stub_asset(
- session: Session,
- file_path: str,
- asset_id: str = "test-asset-id",
- reference_id: str = "test-ref-id",
- name: str | None = None,
-) -> tuple[Asset, AssetReference]:
- """Create a stub asset with reference for testing enrichment."""
- # Use the real file's mtime so the optimistic guard in enrich_asset passes
- try:
- stat_result = os.stat(file_path, follow_symlinks=True)
- mtime_ns = get_mtime_ns(stat_result)
- except OSError:
- mtime_ns = 1234567890000000000
-
- asset = Asset(
- id=asset_id,
- hash=None,
- size_bytes=100,
- mime_type=None,
- )
- session.add(asset)
- session.flush()
-
- ref = AssetReference(
- id=reference_id,
- asset_id=asset_id,
- name=name or f"test-asset-{asset_id}",
- owner_id="system",
- file_path=file_path,
- mtime_ns=mtime_ns,
- enrichment_level=ENRICHMENT_STUB,
- )
- session.add(ref)
- session.flush()
-
- return asset, ref
-
-
-class TestEnrichAsset:
- def test_extracts_mime_type_and_updates_asset(
- self, db_engine, temp_dir: Path, session: Session
- ):
- """Verify mime_type is written to the Asset table during enrichment."""
- file_path = temp_dir / "model.safetensors"
- file_path.write_bytes(b"\x00" * 100)
-
- asset, ref = _create_stub_asset(
- session, str(file_path), "asset-1", "ref-1"
- )
- session.commit()
-
- new_level = enrich_asset(
- session,
- file_path=str(file_path),
- reference_id=ref.id,
- asset_id=asset.id,
- extract_metadata=True,
- compute_hash=False,
- )
-
- assert new_level == ENRICHMENT_METADATA
-
- session.expire_all()
- updated_asset = session.get(Asset, "asset-1")
- assert updated_asset is not None
- assert updated_asset.mime_type == "application/safetensors"
-
- def test_computes_hash_and_updates_asset(
- self, db_engine, temp_dir: Path, session: Session
- ):
- """Verify hash is written to the Asset table during enrichment."""
- file_path = temp_dir / "data.bin"
- file_path.write_bytes(b"test content for hashing")
-
- asset, ref = _create_stub_asset(
- session, str(file_path), "asset-2", "ref-2"
- )
- session.commit()
-
- new_level = enrich_asset(
- session,
- file_path=str(file_path),
- reference_id=ref.id,
- asset_id=asset.id,
- extract_metadata=True,
- compute_hash=True,
- )
-
- assert new_level == ENRICHMENT_HASHED
-
- session.expire_all()
- updated_asset = session.get(Asset, "asset-2")
- assert updated_asset is not None
- assert updated_asset.hash is not None
- assert updated_asset.hash.startswith("blake3:")
-
- def test_enrichment_updates_both_mime_and_hash(
- self, db_engine, temp_dir: Path, session: Session
- ):
- """Verify both mime_type and hash are set when full enrichment runs."""
- file_path = temp_dir / "model.safetensors"
- file_path.write_bytes(b"\x00" * 50)
-
- asset, ref = _create_stub_asset(
- session, str(file_path), "asset-3", "ref-3"
- )
- session.commit()
-
- enrich_asset(
- session,
- file_path=str(file_path),
- reference_id=ref.id,
- asset_id=asset.id,
- extract_metadata=True,
- compute_hash=True,
- )
-
- session.expire_all()
- updated_asset = session.get(Asset, "asset-3")
- assert updated_asset is not None
- assert updated_asset.mime_type == "application/safetensors"
- assert updated_asset.hash is not None
- assert updated_asset.hash.startswith("blake3:")
-
- def test_missing_file_returns_stub_level(
- self, db_engine, temp_dir: Path, session: Session
- ):
- """Verify missing files don't cause errors and return STUB level."""
- file_path = temp_dir / "nonexistent.bin"
-
- asset, ref = _create_stub_asset(
- session, str(file_path), "asset-4", "ref-4"
- )
- session.commit()
-
- new_level = enrich_asset(
- session,
- file_path=str(file_path),
- reference_id=ref.id,
- asset_id=asset.id,
- extract_metadata=True,
- compute_hash=True,
- )
-
- assert new_level == ENRICHMENT_STUB
-
- session.expire_all()
- updated_asset = session.get(Asset, "asset-4")
- assert updated_asset.mime_type is None
- assert updated_asset.hash is None
-
- def test_duplicate_hash_merges_into_existing_asset(
- self, db_engine, temp_dir: Path, session: Session
- ):
- """Verify duplicate files merge into existing asset instead of failing."""
- file_path_1 = temp_dir / "file1.bin"
- file_path_2 = temp_dir / "file2.bin"
- content = b"identical content"
- file_path_1.write_bytes(content)
- file_path_2.write_bytes(content)
-
- asset1, ref1 = _create_stub_asset(
- session, str(file_path_1), "asset-dup-1", "ref-dup-1"
- )
- asset2, ref2 = _create_stub_asset(
- session, str(file_path_2), "asset-dup-2", "ref-dup-2"
- )
- session.commit()
+from app.assets.database.queries import create_content, create_record
- enrich_asset(
- session,
- file_path=str(file_path_1),
- reference_id=ref1.id,
- asset_id=asset1.id,
- extract_metadata=True,
- compute_hash=True,
- )
- enrich_asset(
- session,
- file_path=str(file_path_2),
- reference_id=ref2.id,
- asset_id=asset2.id,
- extract_metadata=True,
- compute_hash=True,
- )
+def test_unenriched_content_is_represented_by_a_null_hash(session: Session) -> None:
+ content = create_content(session, "/models/pending.safetensors", hash=None)
+ record = create_record(session, content.id, "pending.safetensors")
- session.expire_all()
+ assert record.content_id == content.id
+ assert content.hash is None
- updated_asset1 = session.get(Asset, "asset-dup-1")
- assert updated_asset1 is not None
- assert updated_asset1.hash is not None
- updated_asset2 = session.get(Asset, "asset-dup-2")
- assert updated_asset2 is None
+def test_enrichment_does_not_merge_equal_hash_content_rows(session: Session) -> None:
+ first = create_content(session, "/models/one.safetensors", hash="digest")
+ second = create_content(session, "/models/two.safetensors", hash="digest")
- updated_ref2 = session.get(AssetReference, "ref-dup-2")
- assert updated_ref2 is not None
- assert updated_ref2.asset_id == "asset-dup-1"
+ assert first.id != second.id
diff --git a/tests-unit/assets_test/services/test_enrichment_predicate.py b/tests-unit/assets_test/services/test_enrichment_predicate.py
new file mode 100644
index 000000000..94cce7f1c
--- /dev/null
+++ b/tests-unit/assets_test/services/test_enrichment_predicate.py
@@ -0,0 +1,308 @@
+from __future__ import annotations
+
+import os
+from contextlib import contextmanager
+from collections.abc import Iterator
+from pathlib import Path
+from unittest.mock import patch
+
+import sqlalchemy as sa
+from sqlalchemy.orm import Session
+
+from app.assets.database.queries import create_content, create_record, mark_content_missing
+from app.assets.scanner import get_unenriched_assets_for_roots
+from app.assets.scanner_changes import (
+ is_path_under_prefixes,
+ live_contents_under_prefixes,
+)
+
+from .path_prefix_cases import expected_prefix_case_paths, prefix_case_paths
+
+
+@contextmanager
+def _reuse_session(session: Session) -> Iterator[Session]:
+ yield session
+
+
+def _raw_system_metadata(session: Session, record_id: str) -> object:
+ return session.execute(
+ sa.text("SELECT system_metadata FROM assets WHERE id = :id"),
+ {"id": record_id},
+ ).scalar()
+
+
+def test_create_record_stores_none_system_metadata_as_sql_null(
+ session: Session,
+) -> None:
+ content = create_content(session, "/models/no-meta.safetensors", hash=None)
+
+ record = create_record(session, content.id, "no-meta.safetensors")
+ session.commit()
+
+ assert _raw_system_metadata(session, record.id) is None
+
+
+def test_off_mode_returns_metadata_less_seeded_asset(
+ session: Session, temp_dir: Path
+) -> None:
+ path = temp_dir / "fresh.safetensors"
+ content = create_content(session, str(path), hash=None)
+ record = create_record(session, content.id, path.name)
+ session.commit()
+ record_id = record.id
+
+ with (
+ patch("app.assets.scanner.create_session", lambda: _reuse_session(session)),
+ patch(
+ "app.assets.scanner.get_scan_prefixes_for_root",
+ return_value=[str(temp_dir)],
+ ),
+ ):
+ rows = get_unenriched_assets_for_roots(("models",), compute_hashes=False)
+
+ assert record_id in {row.record_id for row in rows}
+
+
+def test_off_mode_excludes_asset_with_system_metadata(
+ session: Session, temp_dir: Path
+) -> None:
+ path = temp_dir / "enriched.safetensors"
+ content = create_content(session, str(path), hash=None)
+ record = create_record(
+ session,
+ content.id,
+ path.name,
+ system_metadata={"architecture": "flux"},
+ )
+ session.commit()
+ record_id = record.id
+
+ with (
+ patch("app.assets.scanner.create_session", lambda: _reuse_session(session)),
+ patch(
+ "app.assets.scanner.get_scan_prefixes_for_root",
+ return_value=[str(temp_dir)],
+ ),
+ ):
+ rows = get_unenriched_assets_for_roots(("models",), compute_hashes=False)
+
+ assert record_id not in {row.record_id for row in rows}
+
+
+def test_query_pushes_limit_into_sql(session: Session, temp_dir: Path) -> None:
+ for index in range(5):
+ path = temp_dir / f"cand-{index}.safetensors"
+ content = create_content(session, str(path), hash=None)
+ create_record(session, content.id, path.name)
+ session.commit()
+
+ statements: list[str] = []
+
+ def _capture(_conn, _cursor, statement, _params, _context, _executemany) -> None:
+ statements.append(statement)
+
+ engine = session.bind
+ sa.event.listen(engine, "before_cursor_execute", _capture)
+ try:
+ with (
+ patch("app.assets.scanner.create_session", lambda: _reuse_session(session)),
+ patch(
+ "app.assets.scanner.get_scan_prefixes_for_root",
+ return_value=[str(temp_dir)],
+ ),
+ ):
+ rows = get_unenriched_assets_for_roots(
+ ("models",), compute_hashes=False, limit=2
+ )
+ finally:
+ sa.event.remove(engine, "before_cursor_execute", _capture)
+
+ assert len(rows) == 2
+ content_selects = [
+ s for s in statements if "asset_contents" in s.lower() and s.lower().lstrip().startswith("select")
+ ]
+ assert content_selects, f"expected a SELECT over asset_contents; got {statements}"
+ assert any("limit" in s.lower() for s in content_selects), (
+ f"enrichment query must push LIMIT into SQL; got: {content_selects}"
+ )
+
+
+def test_prefix_filter_matches_is_path_under_prefixes(
+ session: Session, temp_dir: Path
+) -> None:
+ prefix = str(temp_dir)
+ inside_path = os.path.join(prefix, "sub", "inside.safetensors")
+ sibling_path = prefix + "-sibling" + os.sep + "outside.safetensors"
+
+ inside = create_record(
+ session, create_content(session, inside_path, hash=None).id, "inside.safetensors"
+ )
+ sibling = create_record(
+ session, create_content(session, sibling_path, hash=None).id, "outside.safetensors"
+ )
+ session.commit()
+
+ with (
+ patch("app.assets.scanner.create_session", lambda: _reuse_session(session)),
+ patch(
+ "app.assets.scanner.get_scan_prefixes_for_root",
+ return_value=[prefix],
+ ),
+ ):
+ returned = {
+ row.record_id
+ for row in get_unenriched_assets_for_roots(("models",), compute_hashes=False)
+ }
+
+ assert is_path_under_prefixes(inside_path, [prefix]) is True
+ assert is_path_under_prefixes(sibling_path, [prefix]) is False
+ assert inside.id in returned
+ assert sibling.id not in returned
+
+
+def _seed_paths(session: Session, paths: list[str]) -> dict[str, str]:
+ by_record: dict[str, str] = {}
+ for index, path in enumerate(paths):
+ content = create_content(session, path, hash=None)
+ record = create_record(session, content.id, f"case-{index}.safetensors")
+ by_record[record.id] = content.path
+ session.commit()
+ return by_record
+
+
+def _candidate_paths(session: Session, prefix: str) -> set[str]:
+ with (
+ patch("app.assets.scanner.create_session", lambda: _reuse_session(session)),
+ patch(
+ "app.assets.scanner.get_scan_prefixes_for_root",
+ return_value=[prefix],
+ ),
+ ):
+ rows = get_unenriched_assets_for_roots(("models",), compute_hashes=False)
+ return {row.file_path for row in rows}
+
+
+def test_prefix_filter_result_set_equals_python_predicate(
+ session: Session, temp_dir: Path
+) -> None:
+ root = str(temp_dir / "root")
+ corpus = prefix_case_paths(root)
+ by_record = _seed_paths(session, [path for path, _ in corpus])
+ stored = set(by_record.values())
+
+ returned = _candidate_paths(session, root)
+
+ expected = expected_prefix_case_paths(root)
+ assert returned == expected
+ assert expected and expected != stored
+
+
+def test_prefix_holding_metacharacters_matches_only_literal_children(
+ session: Session, temp_dir: Path
+) -> None:
+ root = str(temp_dir / "a_b%c*d?e[f")
+ inside_path = os.path.join(root, "inside.safetensors")
+ decoy_path = os.path.join(str(temp_dir), "aXbYcZdWeQf", "decoy.safetensors")
+ _seed_paths(session, [inside_path, decoy_path])
+
+ returned = _candidate_paths(session, root)
+
+ assert is_path_under_prefixes(decoy_path, [root]) is False
+ assert returned == {inside_path}
+
+
+# --- live_contents_under_prefixes direct pins ---
+# These pins exercise live_contents_under_prefixes directly, not the enrichment predicate.
+
+
+def test_live_contents_under_prefixes_returns_empty_for_empty_prefixes(
+ session: Session, temp_dir: Path
+) -> None:
+ _ = create_content(session, str(temp_dir / "seed.safetensors"))
+
+ returned = {content.path for content in live_contents_under_prefixes(session, [])}
+
+ assert returned == set()
+
+
+def test_live_contents_under_prefixes_matches_directory_and_exact_path_prefixes(
+ session: Session, temp_dir: Path
+) -> None:
+ content = create_content(session, str(temp_dir / "root" / "model.safetensors"))
+
+ under_directory = {candidate.path for candidate in live_contents_under_prefixes(session, [str(temp_dir / "root")])}
+ exact_path = {candidate.path for candidate in live_contents_under_prefixes(session, [content.path])}
+
+ assert under_directory == {content.path}
+ assert exact_path == {content.path}
+
+
+def test_live_contents_under_prefixes_respects_sibling_boundary(
+ session: Session, temp_dir: Path
+) -> None:
+ sibling_path = str(temp_dir / "a" / "bc" / "model.safetensors")
+ _ = create_content(session, sibling_path)
+
+ returned = {content.path for content in live_contents_under_prefixes(session, [str(temp_dir / "a" / "b")])}
+
+ assert returned == set()
+
+
+def test_live_contents_under_prefixes_is_case_sensitive(
+ session: Session, temp_dir: Path
+) -> None:
+ case_different_path = str(temp_dir / "data" / "TEMP" / "model.safetensors")
+ _ = create_content(session, case_different_path)
+
+ returned = {content.path for content in live_contents_under_prefixes(session, [str(temp_dir / "data" / "temp")])}
+
+ assert returned == set()
+
+
+def test_live_contents_under_prefixes_treats_metacharacters_literally(
+ session: Session, temp_dir: Path
+) -> None:
+ prefix = temp_dir / "a_b%c*d?e[f"
+ literal_child = create_content(session, str(prefix / "child.safetensors"))
+ decoy_path = str(temp_dir / "aXbYc*d?e[f" / "keep.safetensors")
+ _ = create_content(session, decoy_path)
+
+ returned = {content.path for content in live_contents_under_prefixes(session, [str(prefix)])}
+
+ assert returned == {literal_child.path}
+
+
+def test_live_contents_under_prefixes_matches_any_prefix(
+ session: Session, temp_dir: Path
+) -> None:
+ content = create_content(session, str(temp_dir / "second" / "model.safetensors"))
+ prefixes = [str(temp_dir / "first"), str(temp_dir / "second")]
+
+ returned = {candidate.path for candidate in live_contents_under_prefixes(session, prefixes)}
+
+ assert returned == {content.path}
+
+
+def test_live_contents_under_prefixes_excludes_missing_content(
+ session: Session, temp_dir: Path
+) -> None:
+ content = create_content(session, str(temp_dir / "root" / "missing.safetensors"))
+ mark_content_missing(session, content.id)
+
+ returned = {candidate.path for candidate in live_contents_under_prefixes(session, [str(temp_dir / "root")])}
+
+ assert returned == set()
+
+
+def test_live_contents_under_prefixes_equals_python_predicate_for_corpus(
+ session: Session, temp_dir: Path
+) -> None:
+ root = str(temp_dir / "root")
+ corpus = prefix_case_paths(root)
+ for path, _ in corpus:
+ _ = create_content(session, path)
+ prefixes = [root]
+
+ returned = {content.path for content in live_contents_under_prefixes(session, prefixes)}
+
+ assert returned == expected_prefix_case_paths(root)
diff --git a/tests-unit/assets_test/services/test_enrichment_snapshot.py b/tests-unit/assets_test/services/test_enrichment_snapshot.py
new file mode 100644
index 000000000..8e33663cc
--- /dev/null
+++ b/tests-unit/assets_test/services/test_enrichment_snapshot.py
@@ -0,0 +1,211 @@
+import os
+from pathlib import Path
+from unittest.mock import patch
+
+from app.assets.database.models import Asset, AssetContent
+from app.assets.helpers import to_stored_hash
+from app.assets.scanner import enrich_asset
+
+
+def _create_unhashed_record(session, path: Path) -> tuple[AssetContent, Asset]:
+ content = AssetContent(
+ path=str(path),
+ hash=None,
+ size_bytes=path.stat().st_size,
+ mtime_ns=path.stat().st_mtime_ns,
+ )
+ session.add(content)
+ session.flush()
+ record = Asset(content_id=content.id, name=path.name)
+ session.add(record)
+ session.commit()
+ return content, record
+
+
+def test_enrichment_uses_snapshot_hash_not_direct_blake3(session, temp_dir: Path):
+ path = temp_dir / "stable.bin"
+ path.write_bytes(b"stable content")
+ content, record = _create_unhashed_record(session, path)
+
+ with patch(
+ "app.assets.scanner.snapshot_hash", return_value=("snapshot-digest", path.stat())
+ ) as mocked_snapshot_hash:
+ enriched = enrich_asset(
+ session,
+ file_path=str(path),
+ content_id=content.id,
+ record_id=record.id,
+ extract_metadata=False,
+ compute_hash=True,
+ )
+
+ assert enriched is True
+ assert session.get(AssetContent, content.id).hash == to_stored_hash("snapshot-digest")
+ mocked_snapshot_hash.assert_called_once_with(str(path))
+
+
+def test_enrichment_discards_unstable_hash(session, temp_dir: Path):
+ path = temp_dir / "unstable.bin"
+ path.write_bytes(b"unstable content")
+ content, record = _create_unhashed_record(session, path)
+
+ with patch("app.assets.scanner.snapshot_hash", return_value=None):
+ enriched = enrich_asset(
+ session,
+ file_path=str(path),
+ content_id=content.id,
+ record_id=record.id,
+ extract_metadata=False,
+ compute_hash=True,
+ )
+
+ assert enriched is False
+ assert session.get(AssetContent, content.id).hash is None
+
+
+def test_enrichment_discards_metadata_read_from_a_different_file_than_the_hash(
+ session, temp_dir: Path
+):
+ path = temp_dir / "swapped.bin"
+ path.write_bytes(b"original bytes")
+ content, record = _create_unhashed_record(session, path)
+
+ def _replace_file_then_hash(_file_path: str):
+ path.write_bytes(b"replacement bytes, a different length entirely")
+ return "replacement-digest", path.stat()
+
+ with patch("app.assets.scanner.snapshot_hash", side_effect=_replace_file_then_hash):
+ enriched = enrich_asset(
+ session,
+ file_path=str(path),
+ content_id=content.id,
+ record_id=record.id,
+ extract_metadata=True,
+ compute_hash=True,
+ )
+
+ assert enriched is False
+ session.expire_all()
+ assert session.get(AssetContent, content.id).hash is None
+ assert session.get(Asset, record.id).system_metadata is None, (
+ "a mismatched hash observation must discard already-computed metadata too, "
+ "not just the hash"
+ )
+
+
+def test_enrichment_discards_result_when_only_the_hashed_mtime_disagrees(
+ session, temp_dir: Path
+):
+ path = temp_dir / "touched.bin"
+ path.write_bytes(b"same length bytes")
+ content, record = _create_unhashed_record(session, path)
+ later_mtime_ns = path.stat().st_mtime_ns + 5_000_000_000
+
+ def _touch_file_then_hash(_file_path: str):
+ os.utime(path, ns=(later_mtime_ns, later_mtime_ns))
+ return "rewritten-digest", path.stat()
+
+ with patch("app.assets.scanner.snapshot_hash", side_effect=_touch_file_then_hash):
+ enriched = enrich_asset(
+ session,
+ file_path=str(path),
+ content_id=content.id,
+ record_id=record.id,
+ extract_metadata=True,
+ compute_hash=True,
+ )
+
+ assert enriched is False
+ session.expire_all()
+ assert session.get(AssetContent, content.id).hash is None
+ assert session.get(Asset, record.id).system_metadata is None, (
+ "size matching alone is not proof the file is unchanged; an mtime "
+ "disagreement alone must also discard the result"
+ )
+
+
+def test_enrichment_lands_metadata_and_hash_from_one_stable_observation(
+ session, temp_dir: Path
+):
+ path = temp_dir / "stable-both.bin"
+ path.write_bytes(b"stable content for both")
+ content, record = _create_unhashed_record(session, path)
+
+ with patch(
+ "app.assets.scanner.snapshot_hash", return_value=("both-digest", path.stat())
+ ):
+ enriched = enrich_asset(
+ session,
+ file_path=str(path),
+ content_id=content.id,
+ record_id=record.id,
+ extract_metadata=True,
+ compute_hash=True,
+ )
+
+ assert enriched is True
+ session.expire_all()
+ assert session.get(AssetContent, content.id).hash == to_stored_hash("both-digest")
+ assert session.get(Asset, record.id).system_metadata == {
+ "filename": "stable-both.bin",
+ "file_path": str(path),
+ "format": "bin",
+ "content_type": "application/octet-stream",
+ "content_length": path.stat().st_size,
+ }
+
+
+def test_enrichment_reports_no_progress_when_a_requested_hash_fails(
+ session, temp_dir: Path
+):
+ path = temp_dir / "unreadable-for-hashing.bin"
+ path.write_bytes(b"metadata reads fine, hashing does not")
+ content, record = _create_unhashed_record(session, path)
+
+ with patch("app.assets.scanner.snapshot_hash", side_effect=PermissionError("denied")):
+ enriched = enrich_asset(
+ session,
+ file_path=str(path),
+ content_id=content.id,
+ record_id=record.id,
+ extract_metadata=True,
+ compute_hash=True,
+ )
+
+ assert enriched is False, (
+ "a record whose requested hash could not be computed has made no progress; "
+ "counting the metadata as progress keeps the record out of failed_ids, so the "
+ "enrich pass re-selects it forever"
+ )
+ session.expire_all()
+ assert session.get(AssetContent, content.id).hash is None
+
+
+def test_off_mode_enrichment_still_lands_metadata_without_a_hash(
+ session, temp_dir: Path
+):
+ path = temp_dir / "off-mode.bin"
+ path.write_bytes(b"metadata only")
+ content, record = _create_unhashed_record(session, path)
+
+ with patch("app.assets.scanner.snapshot_hash") as never_hashed:
+ enriched = enrich_asset(
+ session,
+ file_path=str(path),
+ content_id=content.id,
+ record_id=record.id,
+ extract_metadata=True,
+ compute_hash=False,
+ )
+
+ never_hashed.assert_not_called()
+ assert enriched is True
+ session.expire_all()
+ assert session.get(AssetContent, content.id).hash is None
+ assert session.get(Asset, record.id).system_metadata == {
+ "filename": "off-mode.bin",
+ "file_path": str(path),
+ "format": "bin",
+ "content_type": "application/octet-stream",
+ "content_length": path.stat().st_size,
+ }
diff --git a/tests-unit/assets_test/services/test_from_hash.py b/tests-unit/assets_test/services/test_from_hash.py
new file mode 100644
index 000000000..53914a644
--- /dev/null
+++ b/tests-unit/assets_test/services/test_from_hash.py
@@ -0,0 +1,118 @@
+from unittest.mock import patch
+
+from sqlalchemy import select
+
+from app.assets.database.models import Asset, AssetContent
+from app.assets.database.queries.records import create_content, mark_content_missing
+from app.assets.helpers import to_stored_hash
+from app.assets.services.ingest import create_from_hash
+from app.assets.services.lookup import (
+ claim_qualified_content as _real_claim_qualified_content,
+ refresh_qualified_content as _real_refresh_qualified_content,
+)
+
+
+def test_create_from_hash_with_prefixed_hash_finds_existing_content(
+ mock_create_session, monkeypatch, temp_dir
+):
+ digest = "a" * 64
+ path = temp_dir / "existing.bin"
+ path.write_bytes(b"existing bytes")
+ monkeypatch.setattr("app.assets.mode.hashing_enabled", lambda: True)
+
+ with mock_create_session() as session:
+ content = create_content(session, str(path), to_stored_hash(digest), path.stat().st_size)
+ content_id = content.id
+ session.commit()
+
+ result = create_from_hash(f"blake3:{digest}", "derived.bin")
+
+ assert result is not None
+ with mock_create_session() as session:
+ records = list(session.scalars(select(Asset)))
+ created_record = session.get(Asset, result.ref.id)
+ assert created_record is not None
+ assert created_record.content_id == content_id
+ assert [record.content_id for record in records] == [content_id]
+
+
+def test_create_from_hash_with_bare_hash_also_works(
+ mock_create_session, monkeypatch, temp_dir
+):
+ digest = "b" * 64
+ path = temp_dir / "existing.bin"
+ path.write_bytes(b"existing bytes")
+ monkeypatch.setattr("app.assets.mode.hashing_enabled", lambda: True)
+
+ with mock_create_session() as session:
+ content = create_content(session, str(path), to_stored_hash(digest), path.stat().st_size)
+ content_id = content.id
+ session.commit()
+
+ result = create_from_hash(digest, "derived.bin")
+
+ assert result is not None
+ with mock_create_session() as session:
+ records = list(session.scalars(select(Asset)))
+ created_record = session.get(Asset, result.ref.id)
+ assert created_record is not None
+ assert created_record.content_id == content_id
+ assert [record.content_id for record in records] == [content_id]
+
+
+def _seed_live_content(mock_create_session, path, digest):
+ with mock_create_session() as session:
+ content = create_content(session, str(path), to_stored_hash(digest), path.stat().st_size)
+ content_id = content.id
+ session.commit()
+ return content_id
+
+
+def test_content_retired_between_lookup_and_claim_mints_nothing(
+ mock_create_session, monkeypatch, temp_dir
+):
+ digest = "c" * 64
+ path = temp_dir / "retired.bin"
+ path.write_bytes(b"retired bytes")
+ monkeypatch.setattr("app.assets.mode.hashing_enabled", lambda: True)
+ content_id = _seed_live_content(mock_create_session, path, digest)
+
+ def retire_then_claim(session, claimed_id, hash):
+ mark_content_missing(session, claimed_id)
+ session.commit()
+ return _real_claim_qualified_content(session, claimed_id, hash)
+
+ with patch("app.assets.services.ingest.claim_qualified_content", retire_then_claim):
+ result = create_from_hash(f"blake3:{digest}", "derived.bin")
+
+ assert result is None
+ with mock_create_session() as session:
+ assert list(session.scalars(select(Asset))) == []
+ retired = session.get(AssetContent, content_id)
+ assert retired is not None
+ assert retired.hash == to_stored_hash(digest)
+ assert retired.path == str(path)
+
+
+def test_file_vanishing_between_claim_and_refresh_mints_nothing(
+ mock_create_session, monkeypatch, temp_dir
+):
+ digest = "d" * 64
+ path = temp_dir / "vanishing.bin"
+ path.write_bytes(b"vanishing bytes")
+ monkeypatch.setattr("app.assets.mode.hashing_enabled", lambda: True)
+ content_id = _seed_live_content(mock_create_session, path, digest)
+
+ def delete_file_then_refresh(session, refreshed_id):
+ path.unlink()
+ return _real_refresh_qualified_content(session, refreshed_id)
+
+ with patch(
+ "app.assets.services.ingest.refresh_qualified_content", delete_file_then_refresh
+ ):
+ result = create_from_hash(f"blake3:{digest}", "derived.bin")
+
+ assert result is None
+ with mock_create_session() as session:
+ assert list(session.scalars(select(Asset))) == []
+ assert session.get(AssetContent, content_id) is not None
diff --git a/tests-unit/assets_test/services/test_hash_mode_state.py b/tests-unit/assets_test/services/test_hash_mode_state.py
new file mode 100644
index 000000000..372f22783
--- /dev/null
+++ b/tests-unit/assets_test/services/test_hash_mode_state.py
@@ -0,0 +1,58 @@
+from unittest.mock import patch
+
+from app.assets.database.models import AssetContent
+from app.assets.services.hash_mode_state import (
+ clear_transition_queue,
+ drain_transition_queue,
+ enqueue_transition_work,
+ pending_transition_count,
+ read_stored_mode,
+ record_transition_intent,
+ write_stored_mode,
+)
+
+
+def test_absent_row_off_mode_no_transition(session):
+ with patch("app.assets.services.hash_mode_state._mode.hashing_enabled", return_value=False):
+ assert record_transition_intent(session) is None
+ assert read_stored_mode(session) == "off"
+
+
+def test_empty_drain_keeps_off_mode_ready_for_a_later_on_transition(session):
+ write_stored_mode(session, "off")
+
+ drain_transition_queue(session)
+
+ with patch("app.assets.services.hash_mode_state._mode.hashing_enabled", return_value=True):
+ assert record_transition_intent(session) == "off_to_on"
+
+
+def test_off_to_on_enqueues_null_rows(session):
+ session.add(AssetContent(path="/tmp/null", hash=None))
+ session.add(AssetContent(path="/tmp/hashed", hash="abc"))
+ write_stored_mode(session, "off")
+ with patch("app.assets.services.hash_mode_state._mode.hashing_enabled", return_value=True):
+ transition = record_transition_intent(session)
+ enqueue_transition_work(session, transition)
+ assert transition == "off_to_on"
+ assert pending_transition_count() == 2
+ assert read_stored_mode(session) == "off"
+ clear_transition_queue()
+
+
+def test_on_to_off_freezes(session):
+ write_stored_mode(session, "on")
+ with patch("app.assets.services.hash_mode_state._mode.hashing_enabled", return_value=False):
+ transition = record_transition_intent(session)
+ assert transition == "on_to_off"
+ assert read_stored_mode(session) == "off"
+ assert pending_transition_count() == 0
+
+
+def test_mode_stays_off_during_transition(session):
+ session.add(AssetContent(path="/tmp/null", hash=None))
+ write_stored_mode(session, "off")
+ with patch("app.assets.services.hash_mode_state._mode.hashing_enabled", return_value=True):
+ transition = record_transition_intent(session)
+ enqueue_transition_work(session, transition)
+ assert read_stored_mode(session) == "off"
diff --git a/tests-unit/assets_test/services/test_ingest.py b/tests-unit/assets_test/services/test_ingest.py
index 7fa882df0..523d41c1b 100644
--- a/tests-unit/assets_test/services/test_ingest.py
+++ b/tests-unit/assets_test/services/test_ingest.py
@@ -1,568 +1,23 @@
-"""Tests for ingest services."""
-from contextlib import contextmanager
from pathlib import Path
-from unittest.mock import patch
-import pytest
-from PIL import Image
-from sqlalchemy.orm import Session as SASession, Session
+from sqlalchemy import select
-from app.assets.database.models import Asset, AssetReference, AssetReferenceTag, Tag
-from app.assets.database.queries import get_reference_tags
-from app.assets.helpers import get_utc_now
-from app.assets.services.ingest import (
- _ingest_file_from_path,
- _register_existing_asset,
- ingest_existing_file,
-)
+import folder_paths
+from app.assets.database.models import Asset, AssetContent
+from app.assets.services.ingest import register_executed_output
-def _make_png(path: Path, size: tuple[int, int]) -> Path:
- Image.new("RGB", size, color=(80, 120, 200)).save(path, format="PNG")
- return path
+def test_output_registration_creates_separate_record_and_content_rows(mock_create_session) -> None:
+ path = Path(folder_paths.get_output_directory()) / "result-b-schema.txt"
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text("result")
+ registered = register_executed_output(str(path), job_id="job")
-class TestIngestFileFromPath:
- def test_creates_asset_and_reference(self, mock_create_session, temp_dir: Path, session: Session):
- file_path = temp_dir / "test_file.bin"
- file_path.write_bytes(b"test content")
-
- result = _ingest_file_from_path(
- abs_path=str(file_path),
- asset_hash="blake3:abc123",
- size_bytes=12,
- mtime_ns=1234567890000000000,
- mime_type="application/octet-stream",
- )
-
- assert result.asset_created is True
- assert result.ref_created is True
- assert result.reference_id is not None
-
- # Verify DB state
- assets = session.query(Asset).all()
- assert len(assets) == 1
- assert assets[0].hash == "blake3:abc123"
-
- refs = session.query(AssetReference).all()
- assert len(refs) == 1
- assert refs[0].file_path == str(file_path)
-
- def test_creates_reference_when_name_provided(self, mock_create_session, temp_dir: Path, session: Session):
- file_path = temp_dir / "model.safetensors"
- file_path.write_bytes(b"model data")
-
- result = _ingest_file_from_path(
- abs_path=str(file_path),
- asset_hash="blake3:def456",
- size_bytes=10,
- mtime_ns=1234567890000000000,
- mime_type="application/octet-stream",
- info_name="My Model",
- owner_id="user1",
- )
-
- assert result.asset_created is True
- assert result.reference_id is not None
-
- ref = session.query(AssetReference).first()
- assert ref is not None
- assert ref.name == "My Model"
- assert ref.owner_id == "user1"
-
- def test_creates_tags_when_provided(self, mock_create_session, temp_dir: Path, session: Session):
- file_path = temp_dir / "tagged.bin"
- file_path.write_bytes(b"data")
-
- result = _ingest_file_from_path(
- abs_path=str(file_path),
- asset_hash="blake3:ghi789",
- size_bytes=4,
- mtime_ns=1234567890000000000,
- info_name="Tagged Asset",
- tags=["models", "checkpoints"],
- )
-
- assert result.reference_id is not None
-
- # Verify tags were created and linked
- tags = session.query(Tag).all()
- tag_names = {t.name for t in tags}
- assert "models" in tag_names
- assert "checkpoints" in tag_names
-
- ref_tags = get_reference_tags(session, reference_id=result.reference_id)
- assert set(ref_tags) == {"models", "checkpoints"}
-
- def test_path_derived_tags_use_automatic_origin(
- self, mock_create_session, temp_dir: Path, session: Session
- ):
- input_dir = temp_dir / "input"
- output_dir = temp_dir / "output"
- temp_root = temp_dir / "temp"
- for directory in (input_dir, output_dir, temp_root):
- directory.mkdir()
- file_path = input_dir / "pasted" / "tagged.png"
- file_path.parent.mkdir()
- file_path.write_bytes(b"data")
-
- with (
- patch("app.assets.services.path_utils.folder_paths") as mock_fp,
- patch(
- "app.assets.services.path_utils.get_comfy_models_folders",
- return_value=[],
- ),
- ):
- mock_fp.get_input_directory.return_value = str(input_dir)
- mock_fp.get_output_directory.return_value = str(output_dir)
- mock_fp.get_temp_directory.return_value = str(temp_root)
-
- result = _ingest_file_from_path(
- abs_path=str(file_path),
- asset_hash="blake3:pathorigin",
- size_bytes=4,
- mtime_ns=1234567890000000000,
- info_name="Tagged Asset",
- tags=["input", "manual-label"],
- )
-
- assert result.reference_id is not None
- links = session.query(AssetReferenceTag).filter_by(
- asset_reference_id=result.reference_id
- )
- origin_by_tag = {link.tag_name: link.origin for link in links}
- assert origin_by_tag["input"] == "automatic"
- assert origin_by_tag["pasted"] == "automatic"
- assert origin_by_tag["manual-label"] == "manual"
-
- def test_idempotent_upsert(self, mock_create_session, temp_dir: Path, session: Session):
- file_path = temp_dir / "dup.bin"
- file_path.write_bytes(b"content")
-
- # First ingest
- r1 = _ingest_file_from_path(
- abs_path=str(file_path),
- asset_hash="blake3:repeat",
- size_bytes=7,
- mtime_ns=1234567890000000000,
- )
- assert r1.asset_created is True
-
- # Second ingest with same hash - should update, not create
- r2 = _ingest_file_from_path(
- abs_path=str(file_path),
- asset_hash="blake3:repeat",
- size_bytes=7,
- mtime_ns=1234567890000000001, # different mtime
- )
- assert r2.asset_created is False
- assert r2.ref_created is False
- assert r2.ref_updated is True
-
- # Still only one asset
- assets = session.query(Asset).all()
- assert len(assets) == 1
-
- def test_validates_preview_id(self, mock_create_session, temp_dir: Path, session: Session):
- file_path = temp_dir / "with_preview.bin"
- file_path.write_bytes(b"data")
-
- # Create a preview asset and reference
- preview_asset = Asset(hash="blake3:preview", size_bytes=100)
- session.add(preview_asset)
- session.flush()
- from app.assets.helpers import get_utc_now
- now = get_utc_now()
- preview_ref = AssetReference(
- asset_id=preview_asset.id, name="preview.png", owner_id="",
- created_at=now, updated_at=now, last_access_time=now,
- )
- session.add(preview_ref)
- session.commit()
- preview_id = preview_ref.id
-
- result = _ingest_file_from_path(
- abs_path=str(file_path),
- asset_hash="blake3:main",
- size_bytes=4,
- mtime_ns=1234567890000000000,
- info_name="With Preview",
- preview_id=preview_id,
- )
-
- assert result.reference_id is not None
- ref = session.query(AssetReference).filter_by(id=result.reference_id).first()
- assert ref.preview_id == preview_id
-
- def test_invalid_preview_id_is_cleared(self, mock_create_session, temp_dir: Path, session: Session):
- file_path = temp_dir / "bad_preview.bin"
- file_path.write_bytes(b"data")
-
- result = _ingest_file_from_path(
- abs_path=str(file_path),
- asset_hash="blake3:badpreview",
- size_bytes=4,
- mtime_ns=1234567890000000000,
- info_name="Bad Preview",
- preview_id="nonexistent-uuid",
- )
-
- assert result.reference_id is not None
- ref = session.query(AssetReference).filter_by(id=result.reference_id).first()
- assert ref.preview_id is None
-
-
-class TestRegisterExistingAsset:
- def test_creates_reference_for_existing_asset(self, mock_create_session, session: Session):
- # Create existing asset
- asset = Asset(hash="blake3:existing", size_bytes=1024, mime_type="image/png")
- session.add(asset)
- session.commit()
-
- result = _register_existing_asset(
- asset_hash="blake3:existing",
- name="Registered Asset",
- user_metadata={"key": "value"},
- tags=["models"],
- )
-
- assert result.created is True
- assert "models" in result.tags
-
- # Verify by re-fetching from DB
- session.expire_all()
- refs = session.query(AssetReference).filter_by(name="Registered Asset").all()
- assert len(refs) == 1
-
- def test_creates_new_reference_even_with_same_name(self, mock_create_session, session: Session):
- # Create asset and reference
- asset = Asset(hash="blake3:withref", size_bytes=512)
- session.add(asset)
- session.flush()
-
- from app.assets.helpers import get_utc_now
- ref = AssetReference(
- owner_id="",
- name="Existing Ref",
- asset_id=asset.id,
- created_at=get_utc_now(),
- updated_at=get_utc_now(),
- last_access_time=get_utc_now(),
- )
- session.add(ref)
- session.flush()
- ref_id = ref.id
- session.commit()
-
- result = _register_existing_asset(
- asset_hash="blake3:withref",
- name="Existing Ref",
- owner_id="",
- )
-
- # Multiple files with same name are allowed
- assert result.created is True
-
- # Verify two AssetReferences exist for this name
- session.expire_all()
- refs = session.query(AssetReference).filter_by(name="Existing Ref").all()
- assert len(refs) == 2
- assert ref_id in [r.id for r in refs]
-
- def test_raises_for_nonexistent_hash(self, mock_create_session):
- with pytest.raises(ValueError, match="No asset with hash"):
- _register_existing_asset(
- asset_hash="blake3:doesnotexist",
- name="Fail",
- )
-
- def test_applies_tags_to_new_reference(self, mock_create_session, session: Session):
- asset = Asset(hash="blake3:tagged", size_bytes=256)
- session.add(asset)
- session.commit()
-
- result = _register_existing_asset(
- asset_hash="blake3:tagged",
- name="Tagged Ref",
- tags=["alpha", "beta"],
- )
-
- assert result.created is True
- assert set(result.tags) == {"alpha", "beta"}
-
-
-class TestIngestExistingFileTagFK:
- """Regression: ingest_existing_file must seed Tag rows before inserting
- AssetReferenceTag rows, otherwise FK enforcement raises IntegrityError."""
-
- def test_creates_tag_rows_before_reference_tags(self, db_engine_fk, temp_dir: Path):
- """With PRAGMA foreign_keys=ON, tags must exist in the tags table
- before they can be referenced in asset_reference_tags."""
-
- @contextmanager
- def _create_session():
- with SASession(db_engine_fk) as sess:
- yield sess
-
- file_path = temp_dir / "output.png"
- file_path.write_bytes(b"image data")
-
- with patch("app.assets.services.ingest.create_session", _create_session), \
- patch(
- "app.assets.services.ingest.get_name_and_tags_from_asset_path",
- return_value=("output.png", ["output"]),
- ):
- result = ingest_existing_file(
- abs_path=str(file_path),
- extra_tags=["my-job"],
- )
-
- assert result is True
-
- with SASession(db_engine_fk) as sess:
- tag_names = {t.name for t in sess.query(Tag).all()}
- assert "output" in tag_names
- assert "my-job" in tag_names
-
- ref_tags = sess.query(AssetReferenceTag).all()
- ref_tag_names = {rt.tag_name for rt in ref_tags}
- assert "output" in ref_tag_names
-
-
-class TestIngestExistingFileLoaderPath:
- """Outputs saved into a subfolder must persist the subfolder-qualified
- loader path, not the bare basename (regression: spec["fname"] was
- os.path.basename)."""
-
- def test_subfoldered_output_persists_relative_loader_path(
- self, mock_create_session, temp_dir: Path, session: Session
- ):
- input_dir = temp_dir / "input"
- output_dir = temp_dir / "output"
- temp_root = temp_dir / "temp"
- for directory in (input_dir, output_dir, temp_root):
- directory.mkdir()
- file_path = output_dir / "sub" / "img_00001_.png"
- file_path.parent.mkdir()
- file_path.write_bytes(b"image data")
-
- with (
- patch("app.assets.services.path_utils.folder_paths") as mock_fp,
- patch(
- "app.assets.services.path_utils.get_comfy_models_folders",
- return_value=[],
- ),
- ):
- mock_fp.get_input_directory.return_value = str(input_dir)
- mock_fp.get_output_directory.return_value = str(output_dir)
- mock_fp.get_temp_directory.return_value = str(temp_root)
-
- assert ingest_existing_file(abs_path=str(file_path)) is True
-
- ref = (
- session.query(AssetReference)
- .filter_by(file_path=str(file_path))
- .one()
- )
- assert ref.loader_path == "sub/img_00001_.png"
- assert (ref.user_metadata or {}).get("filename") == "sub/img_00001_.png"
-
-
-class TestIngestImageDimensions:
- """system_metadata should carry {kind, width, height} for image assets."""
-
- def test_image_asset_emits_dimensions(
- self, mock_create_session, temp_dir: Path, session: Session
- ):
- f = _make_png(temp_dir / "shot.png", (640, 480))
-
- result = _ingest_file_from_path(
- abs_path=str(f),
- asset_hash="blake3:img1",
- size_bytes=f.stat().st_size,
- mtime_ns=1234567890000000000,
- mime_type="image/png",
- )
-
- ref = session.query(AssetReference).filter_by(id=result.reference_id).first()
- assert ref.system_metadata == {
- "kind": "image",
- "width": 640,
- "height": 480,
- }
-
- def test_non_image_asset_leaves_system_metadata_empty(
- self, mock_create_session, temp_dir: Path, session: Session
- ):
- f = temp_dir / "model.safetensors"
- f.write_bytes(b"not an image")
-
- result = _ingest_file_from_path(
- abs_path=str(f),
- asset_hash="blake3:safetensors1",
- size_bytes=f.stat().st_size,
- mtime_ns=1234567890000000000,
- mime_type="application/octet-stream",
- )
-
- ref = session.query(AssetReference).filter_by(id=result.reference_id).first()
- assert ref.system_metadata in (None, {})
-
- def test_preserves_existing_system_metadata_keys(
- self, mock_create_session, temp_dir: Path, session: Session
- ):
- f = _make_png(temp_dir / "annotated.png", (100, 200))
-
- # First pass populates a sentinel system_metadata key (simulating prior
- # enricher write).
- result = _ingest_file_from_path(
- abs_path=str(f),
- asset_hash="blake3:img-merge",
- size_bytes=f.stat().st_size,
- mtime_ns=1234567890000000000,
- mime_type="image/png",
- )
- ref = session.query(AssetReference).filter_by(id=result.reference_id).first()
- ref.system_metadata = {**(ref.system_metadata or {}), "source_url": "https://example/x.png"}
- session.commit()
-
- # Second pass with the same path triggers the merge code path again.
- _ingest_file_from_path(
- abs_path=str(f),
- asset_hash="blake3:img-merge",
- size_bytes=f.stat().st_size,
- mtime_ns=1234567890000000001,
- mime_type="image/png",
- )
-
- session.refresh(ref)
- assert ref.system_metadata["kind"] == "image"
- assert ref.system_metadata["width"] == 100
- assert ref.system_metadata["height"] == 200
- assert ref.system_metadata["source_url"] == "https://example/x.png"
-
-
-class TestRegisterExistingAssetBackfill:
- """The from-hash path back-fills dimensions from a sibling reference."""
-
- def _add_reference(
- self,
- session: Session,
- asset: Asset,
- name: str,
- system_metadata: dict | None = None,
- ) -> AssetReference:
- now = get_utc_now()
- ref = AssetReference(
- asset_id=asset.id,
- name=name,
- owner_id="",
- created_at=now,
- updated_at=now,
- last_access_time=now,
- system_metadata=system_metadata or {},
- )
- session.add(ref)
- session.flush()
- return ref
-
- def test_backfills_dimensions_from_sibling_image_reference(
- self, mock_create_session, session: Session
- ):
- asset = Asset(hash="blake3:shared", size_bytes=2048, mime_type="image/png")
- session.add(asset)
- session.flush()
- self._add_reference(
- session,
- asset,
- name="original.png",
- system_metadata={"kind": "image", "width": 800, "height": 600},
- )
- session.commit()
-
- result = _register_existing_asset(
- asset_hash="blake3:shared",
- name="from_hash.png",
- owner_id="user-x",
- )
-
- ref = session.query(AssetReference).filter_by(id=result.ref.id).first()
- assert ref.system_metadata.get("kind") == "image"
- assert ref.system_metadata.get("width") == 800
- assert ref.system_metadata.get("height") == 600
-
- def test_no_backfill_when_sibling_has_no_image_metadata(
- self, mock_create_session, session: Session
- ):
- asset = Asset(hash="blake3:nodims", size_bytes=2048, mime_type="image/png")
- session.add(asset)
- session.flush()
- self._add_reference(
- session,
- asset,
- name="original.png",
- system_metadata={"base_model": "flux"}, # no kind=image
- )
- session.commit()
-
- result = _register_existing_asset(
- asset_hash="blake3:nodims",
- name="from_hash.png",
- owner_id="user-x",
- )
-
- ref = session.query(AssetReference).filter_by(id=result.ref.id).first()
- meta = ref.system_metadata or {}
- assert "kind" not in meta
- assert "width" not in meta
- assert "height" not in meta
-
- def test_no_backfill_when_no_sibling_exists(
- self, mock_create_session, session: Session
- ):
- asset = Asset(hash="blake3:lonely", size_bytes=1024, mime_type="image/png")
- session.add(asset)
- session.commit()
-
- result = _register_existing_asset(
- asset_hash="blake3:lonely",
- name="solo.png",
- owner_id="user-x",
- )
-
- ref = session.query(AssetReference).filter_by(id=result.ref.id).first()
- assert ref.system_metadata in (None, {})
-
- def test_backfill_preserves_caller_supplied_keys(
- self, mock_create_session, session: Session
- ):
- asset = Asset(hash="blake3:preserve", size_bytes=2048, mime_type="image/png")
- session.add(asset)
- session.flush()
- self._add_reference(
- session,
- asset,
- name="original.png",
- system_metadata={"kind": "image", "width": 1024, "height": 768},
- )
- session.commit()
-
- # Simulate a from-hash path where the new reference already carries
- # some system_metadata (e.g. a download-provenance source_url written
- # by an earlier step). The back-fill must merge dim keys without
- # clobbering existing keys.
- result = _register_existing_asset(
- asset_hash="blake3:preserve",
- name="from_hash.png",
- owner_id="user-x",
- )
- ref = session.query(AssetReference).filter_by(id=result.ref.id).first()
- # Seed a sentinel key and re-run back-fill via a second register call
- # to exercise the merge path with pre-existing data.
- ref.system_metadata = {**(ref.system_metadata or {}), "source_url": "https://example/p"}
- session.commit()
-
- assert ref.system_metadata.get("source_url") == "https://example/p"
- assert ref.system_metadata.get("kind") == "image"
- assert ref.system_metadata.get("width") == 1024
- assert ref.system_metadata.get("height") == 768
+ with mock_create_session() as session:
+ record = session.get(Asset, registered.id)
+ content = session.scalar(select(AssetContent).where(AssetContent.id == registered.content_id))
+ assert record is not None
+ assert content is not None
+ assert record.content_id == content.id
+ assert record.job_id == "job"
diff --git a/tests-unit/assets_test/services/test_ingest_b.py b/tests-unit/assets_test/services/test_ingest_b.py
new file mode 100644
index 000000000..dd2a164b2
--- /dev/null
+++ b/tests-unit/assets_test/services/test_ingest_b.py
@@ -0,0 +1,82 @@
+import os
+
+import pytest
+from sqlalchemy import select
+
+import app.assets.mode as mode_module
+import folder_paths
+from app.assets.database.models import Asset, AssetContent
+from app.assets.services.ingest import register_executed_output
+
+
[email protected](autouse=True)
+def hashing_off():
+ class FakeArgs:
+ enable_asset_hashing = False
+
+ mode_module.init(FakeArgs())
+ yield
+ mode_module.init(None)
+
+
+def test_new_path_save_off_mode_hash_null(mock_create_session):
+ output_dir = folder_paths.get_output_directory()
+ os.makedirs(output_dir, exist_ok=True)
+ f = os.path.join(output_dir, "test_ingest_b_new.png")
+ with open(f, "wb") as fh:
+ fh.write(b"pixels")
+
+ try:
+ record = register_executed_output(f, job_id="job1")
+ record_id = record.id
+ with mock_create_session() as session:
+ content = session.execute(
+ select(AssetContent).where(AssetContent.path == os.path.abspath(f))
+ ).scalar_one()
+ assert content.hash is None
+ assert content.is_missing is False
+ asset = session.execute(
+ select(Asset).where(Asset.id == record_id)
+ ).scalar_one()
+ assert asset.job_id == "job1"
+ finally:
+ if os.path.exists(f):
+ os.unlink(f)
+
+
+def test_overwrite_at_live_path_marks_old_missing(mock_create_session):
+ output_dir = folder_paths.get_output_directory()
+ os.makedirs(output_dir, exist_ok=True)
+ f = os.path.join(output_dir, "test_ingest_b_overwrite.png")
+
+ try:
+ with open(f, "wb") as fh:
+ fh.write(b"v1")
+ r1 = register_executed_output(f, job_id="job1")
+ old_content_id = r1.content_id
+ old_record_id = r1.id
+
+ with open(f, "wb") as fh:
+ fh.write(b"v2")
+ r2 = register_executed_output(f, job_id="job2")
+ new_record_id = r2.id
+
+ with mock_create_session() as session:
+ old_content = session.execute(
+ select(AssetContent).where(AssetContent.id == old_content_id)
+ ).scalar_one()
+ assert old_content.is_missing is True, "Old content should be marked missing"
+
+ old_record = session.execute(
+ select(Asset).where(Asset.id == old_record_id)
+ ).scalar_one()
+ assert old_record.job_id == "job1", "Old record's job_id must not be mutated"
+
+ new_record = session.execute(
+ select(Asset).where(Asset.id == new_record_id)
+ ).scalar_one()
+ assert new_record.job_id == "job2"
+ assert new_record_id != old_record_id
+ finally:
+ if os.path.exists(f):
+ os.unlink(f)
diff --git a/tests-unit/assets_test/services/test_ingest_orphan_content.py b/tests-unit/assets_test/services/test_ingest_orphan_content.py
new file mode 100644
index 000000000..ae209945c
--- /dev/null
+++ b/tests-unit/assets_test/services/test_ingest_orphan_content.py
@@ -0,0 +1,168 @@
+import os
+import uuid
+
+import pytest
+from sqlalchemy import func, select
+from sqlalchemy.orm import Session
+
+import app.assets.mode as mode_module
+import folder_paths
+from app.assets.database.models import Asset, AssetContent
+from app.assets.database.queries import create_record as create_record_query
+from app.assets.scanner import SeedAssetSpec, seed_asset_specs
+from app.assets.services import ingest
+from app.assets.services.ingest import register_file_in_place, upload_from_temp_path
+
+
+def hashing_off():
+ class FakeArgs:
+ enable_asset_hashing = False
+
+ mode_module.init(FakeArgs())
+ yield
+ mode_module.init(None)
+
+
+def _write_temp(content: bytes) -> str:
+ uploads_root = os.path.join(
+ folder_paths.get_temp_directory(), "uploads", uuid.uuid4().hex
+ )
+ os.makedirs(uploads_root, exist_ok=True)
+ path = os.path.join(uploads_root, ".upload.part")
+ with open(path, "wb") as file:
+ file.write(content)
+ return path
+
+
+def _raise_create_record(*_args, **_kwargs):
+ raise RuntimeError("forced create_record failure")
+
+
+def _live_content_count(session: Session) -> int:
+ return session.scalar(
+ select(func.count())
+ .select_from(AssetContent)
+ .where(AssetContent.is_missing.is_(False))
+ )
+
+
+def test_upload_record_failure_discards_newly_inserted_content(
+ mock_create_session, hashing_off, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ content = b"orphan-upload-" + uuid.uuid4().bytes
+ temp_path = _write_temp(content)
+ monkeypatch.setattr(ingest, "create_record", _raise_create_record)
+
+ with pytest.raises(RuntimeError, match="forced create_record failure"):
+ upload_from_temp_path(
+ temp_path=temp_path,
+ name="orphan.bin",
+ tags=["output"],
+ client_filename="orphan.bin",
+ )
+
+ with mock_create_session() as session:
+ assert _live_content_count(session) == 0
+
+
+def test_register_file_in_place_discards_content_on_record_failure(
+ mock_create_session, hashing_off, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ output_dir = folder_paths.get_output_directory()
+ os.makedirs(output_dir, exist_ok=True)
+ path = os.path.join(output_dir, f"orphan_inplace_{uuid.uuid4().hex}.png")
+ with open(path, "wb") as file:
+ file.write(b"orphan-inplace-" + uuid.uuid4().bytes)
+ monkeypatch.setattr(ingest, "create_record", _raise_create_record)
+
+ try:
+ with pytest.raises(RuntimeError, match="forced create_record failure"):
+ register_file_in_place(abs_path=path, name="orphan_inplace.png", tags=["output"])
+
+ with mock_create_session() as session:
+ assert _live_content_count(session) == 0
+ finally:
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def _seed_spec(path: str, size_bytes: int, mtime_ns: int, name: str) -> SeedAssetSpec:
+ return {
+ "abs_path": path,
+ "size_bytes": size_bytes,
+ "mtime_ns": mtime_ns,
+ "info_name": name,
+ "tags": ["input"],
+ "fname": name,
+ "metadata": None,
+ "mime_type": None,
+ "job_id": None,
+ }
+
+
+def _content_at(session: Session, path: str) -> AssetContent | None:
+ return session.scalar(select(AssetContent).where(AssetContent.path == path))
+
+
+def _reference_count(session: Session, content_id: str) -> int:
+ return session.scalar(
+ select(func.count()).select_from(Asset).where(Asset.content_id == content_id)
+ )
+
+
+def _orphaned_content_paths(session: Session) -> list[str]:
+ return [
+ content.path
+ for content in session.scalars(select(AssetContent))
+ if _reference_count(session, content.id) == 0
+ ]
+
+
+def test_seed_asset_specs_orphans_nothing_and_keeps_earlier_specs_on_record_failure(
+ session: Session, tmp_path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ specs: list[SeedAssetSpec] = []
+ paths: dict[str, str] = {}
+ fail_name = "vanished.bin"
+ for name in ("first.bin", fail_name, "last.bin"):
+ file_path = tmp_path / name
+ file_path.write_bytes(name.encode())
+ stat_result = file_path.stat()
+ paths[name] = str(file_path)
+ specs.append(
+ _seed_spec(str(file_path), stat_result.st_size, stat_result.st_mtime_ns, name)
+ )
+
+ attempted: list[str] = []
+
+ def _create_record_or_raise(session_arg, content_id, name, *args, **kwargs):
+ attempted.append(name)
+ if name == fail_name:
+ raise RuntimeError("forced create_record failure")
+ return create_record_query(session_arg, content_id, name, *args, **kwargs)
+
+ monkeypatch.setattr("app.assets.scanner.create_record", _create_record_or_raise)
+
+ with pytest.raises(RuntimeError, match="forced create_record failure"):
+ seed_asset_specs(session, specs)
+ session.rollback()
+
+ assert _content_at(session, paths[fail_name]) is None, (
+ "the failed spec's content must not outlive the record that would have referenced it"
+ )
+ assert session.scalar(select(Asset).where(Asset.name == fail_name)) is None
+ assert _orphaned_content_paths(session) == []
+
+ survivor = _content_at(session, paths["first.bin"])
+ assert survivor is not None, (
+ "a spec that already succeeded must not be retroactively erased by a later "
+ "unrelated failure; its savepoint was released before that failure happened"
+ )
+ assert session.scalar(select(Asset).where(Asset.name == "first.bin")) is not None
+ assert _reference_count(session, survivor.id) == 1
+
+ assert attempted == ["first.bin", fail_name]
+ assert _content_at(session, paths["last.bin"]) is None, (
+ "the raise aborts the loop, so the spec after the failed one is never attempted"
+ )
diff --git a/tests-unit/assets_test/services/test_lifecycle.py b/tests-unit/assets_test/services/test_lifecycle.py
new file mode 100644
index 000000000..4010a770c
--- /dev/null
+++ b/tests-unit/assets_test/services/test_lifecycle.py
@@ -0,0 +1,333 @@
+import logging
+import os
+import tempfile
+from contextlib import contextmanager
+from pathlib import Path
+from unittest.mock import patch
+
+import pytest
+from sqlalchemy import create_engine, select
+from sqlalchemy.orm import Session
+from sqlalchemy.orm import Session as SASession
+
+from app.assets import lifecycle
+from app.assets.database.models import Asset, AssetContent, Base
+from app.assets.database.queries.records import create_content, create_record
+from app.assets.lifecycle import (
+ cleanup_temp_filesystem,
+ get_excluded_scan_roots,
+ run_asset_shutdown_cleanup,
+ run_asset_startup,
+ run_startup,
+ wipe_temp_db_rows,
+)
+from app.assets.scanner import get_temp_prefixes, sync_temp_references_safely
+from app.assets.scanner_changes import is_path_under_prefixes
+from app.assets.seeder import asset_seeder
+from app.assets.services import hash_mode_state
+from app.assets.services.hash_mode_state import clear_transition_queue, write_stored_mode
+
+from .path_prefix_cases import expected_prefix_case_paths, prefix_case_paths
+
+
[email protected](autouse=True)
+def autoclean_unit_test_assets():
+ lifecycle._excluded_scan_roots.clear()
+ yield
+ lifecycle._excluded_scan_roots.clear()
+
+
[email protected](autouse=True)
+def autoclean_transition_queue():
+ clear_transition_queue()
+ lifecycle._hash_mode_transition = None
+ yield
+ clear_transition_queue()
+ lifecycle._hash_mode_transition = None
+
+
+def session():
+ engine = create_engine("sqlite:///:memory:", connect_args={"check_same_thread": False})
+ Base.metadata.create_all(engine)
+ with Session(engine) as sess:
+ yield sess
+
+
+def mock_create_session(session):
+ engine = session.bind
+
+ @contextmanager
+ def _create_session():
+ with SASession(engine) as sess:
+ yield sess
+
+ with patch("app.assets.lifecycle.create_session", _create_session), \
+ patch("app.database.db.create_session", _create_session):
+ yield _create_session
+
+
+def comfy_dirs():
+ with tempfile.TemporaryDirectory() as base:
+ temp_dir = Path(base) / "temp"
+ temp_dir.mkdir()
+ with patch("folder_paths.get_temp_directory", return_value=str(temp_dir)):
+ yield temp_dir
+
+
+def _seed_temp_rows(session: Session, temp_dir: Path) -> tuple[str, str]:
+ temp_file = temp_dir / "preview.png"
+ temp_file.write_bytes(b"\x00" * 10)
+ content = create_content(session, path=str(temp_file))
+ record = create_record(session, content_id=content.id, name="preview.png")
+ session.commit()
+ return record.id, content.id
+
+
+def test_startup_order_wipe_before_rmtree_before_seeder(mock_create_session):
+ calls: list[str] = []
+
+ def _wipe(session):
+ calls.append("wipe")
+ return 0, 0
+
+ with (
+ patch("app.assets.lifecycle.wipe_temp_db_rows", side_effect=_wipe),
+ patch("app.assets.lifecycle.cleanup_temp_filesystem", side_effect=lambda: calls.append("rmtree") or True),
+ patch("app.assets.lifecycle.enqueue_mode_transition_work", side_effect=lambda: calls.append("enqueue")),
+ patch("app.assets.lifecycle.start_asset_seeder", side_effect=lambda: calls.append("seeder") or True),
+ ):
+ run_asset_startup()
+
+ assert calls == ["wipe", "rmtree", "enqueue", "seeder"]
+
+
[email protected]("wipe_fails", [False, True], ids=["wipe_succeeds", "wipe_fails"])
+def test_startup_defers_transition_drain_to_the_seeder(
+ session, mock_create_session, tmp_path, monkeypatch, wipe_fails
+):
+ asset_file = tmp_path / "model.safetensors"
+ asset_file.write_bytes(b"weights")
+ stat = asset_file.stat()
+ create_content(session, path=str(asset_file), size_bytes=stat.st_size, mtime_ns=stat.st_mtime_ns)
+ write_stored_mode(session, "off")
+ session.commit()
+ monkeypatch.setattr(hash_mode_state._mode, "hashing_enabled", lambda: True)
+
+ wipe_side_effect = RuntimeError("db wipe failed") if wipe_fails else (lambda _session: (0, 0))
+
+ with (
+ patch("app.assets.lifecycle.wipe_temp_db_rows", side_effect=wipe_side_effect),
+ patch("app.assets.lifecycle.cleanup_temp_filesystem", return_value=True),
+ patch("app.assets.lifecycle.start_asset_seeder", return_value=True),
+ patch.object(hash_mode_state, "drain_transition_queue") as drain_spy,
+ ):
+ lifecycle.record_hash_mode_transition_intent()
+ run_asset_startup()
+
+ drain_spy.assert_not_called()
+ assert hash_mode_state.pending_transition_count() > 0
+ assert hash_mode_state.read_stored_mode(session) == "off"
+
+
+def test_db_wipe_failure_skips_rmtree_and_continues(mock_create_session):
+ with (
+ patch("app.assets.lifecycle.wipe_temp_db_rows", side_effect=RuntimeError("db wipe failed")),
+ patch("app.assets.lifecycle.cleanup_temp_filesystem") as cleanup_mock,
+ patch("app.assets.lifecycle.start_asset_seeder", return_value=True) as seeder_mock,
+ ):
+ run_asset_startup()
+
+ cleanup_mock.assert_not_called()
+ seeder_mock.assert_called_once()
+
+
+def test_run_startup_disabled_sweeps_temp_filesystem_without_db_work(comfy_dirs):
+ stale = comfy_dirs / "stale.png"
+ stale.write_bytes(b"\x00" * 10)
+ assert stale.exists()
+
+ with (
+ patch("app.assets.lifecycle.run_asset_startup") as asset_startup_mock,
+ patch("app.assets.lifecycle.wipe_temp_db_rows") as wipe_mock,
+ patch("app.assets.lifecycle.start_asset_seeder") as seeder_mock,
+ ):
+ run_startup(enable_assets=False)
+
+ assert not stale.exists()
+ asset_startup_mock.assert_not_called()
+ wipe_mock.assert_not_called()
+ seeder_mock.assert_not_called()
+
+
+def test_run_startup_enabled_delegates_to_asset_startup_not_bare_sweep():
+ with (
+ patch("app.assets.lifecycle.run_asset_startup") as asset_startup_mock,
+ patch("app.assets.lifecycle.cleanup_temp_filesystem") as cleanup_mock,
+ ):
+ run_startup(enable_assets=True)
+
+ asset_startup_mock.assert_called_once_with()
+ cleanup_mock.assert_not_called()
+
+
+def test_run_startup_logs_and_absorbs_disabled_filesystem_failure(caplog):
+ with patch("app.assets.lifecycle.cleanup_temp_filesystem", side_effect=RuntimeError("filesystem failure")):
+ run_startup(enable_assets=False)
+
+ assert "Asset startup maintenance failed" in caplog.text
+
+
+def test_rmtree_failure_excludes_temp_from_scan(session, comfy_dirs, mock_create_session):
+ record_id, content_id = _seed_temp_rows(session, comfy_dirs)
+
+ wipe_temp_db_rows(session)
+ session.commit()
+ assert session.get(Asset, record_id) is None
+
+ with patch("app.assets.lifecycle.shutil.rmtree", side_effect=OSError("busy")):
+ assert cleanup_temp_filesystem() is False
+
+ assert str(comfy_dirs) in get_excluded_scan_roots()
+ assert get_temp_prefixes() == []
+
+ residual = comfy_dirs / "leftover.png"
+ residual.write_bytes(b"\x00" * 10)
+
+ with patch("app.assets.scanner.create_session", mock_create_session):
+ sync_temp_references_safely()
+
+ assert session.scalars(select(Asset)).all() == []
+
+
+def test_shutdown_skips_cleanup_when_seeder_join_times_out(session, comfy_dirs, mock_create_session, caplog):
+ record_id, _ = _seed_temp_rows(session, comfy_dirs)
+
+ with patch("app.assets.lifecycle.wipe_temp_db_rows") as wipe_mock:
+ with patch.object(asset_seeder, "shutdown", return_value=False):
+ joined = asset_seeder.shutdown()
+ if joined:
+ run_asset_shutdown_cleanup()
+
+ wipe_mock.assert_not_called()
+ assert session.get(Asset, record_id) is not None
+
+
+def test_seeder_shutdown_logs_only_when_thread_does_not_exit(caplog: pytest.LogCaptureFixture) -> None:
+ seeder = type(asset_seeder)()
+ with patch.object(seeder, "cancel"), patch.object(seeder, "wait", return_value=False), caplog.at_level(logging.WARNING):
+ assert seeder.shutdown(timeout=2.5) is False
+ assert caplog.messages == ["Asset seeder thread did not exit within 2.5s"]
+ assert "skipping temp cleanup" not in caplog.text
+
+
+def test_shutdown_cleanup_wipes_rows_then_rmtree(session, comfy_dirs, mock_create_session):
+ record_id, content_id = _seed_temp_rows(session, comfy_dirs)
+ calls: list[str] = []
+
+ real_wipe = wipe_temp_db_rows
+
+ def tracking_wipe(sess):
+ calls.append("wipe")
+ return real_wipe(sess)
+
+ with (
+ patch("app.assets.lifecycle.wipe_temp_db_rows", side_effect=tracking_wipe),
+ patch("app.assets.lifecycle.cleanup_temp_filesystem", side_effect=lambda: calls.append("rmtree") or True),
+ ):
+ run_asset_shutdown_cleanup()
+
+ assert calls == ["wipe", "rmtree"]
+ assert session.get(Asset, record_id) is None
+ assert session.get(AssetContent, content_id) is None
+
+
+def test_run_shutdown_without_session_sweeps_temp_filesystem(comfy_dirs):
+ stale = comfy_dirs / "stale.png"
+ stale.write_bytes(b"\x00" * 10)
+
+ with (
+ patch("app.assets.lifecycle.can_create_session", return_value=False),
+ patch("app.assets.lifecycle.wipe_temp_db_rows") as wipe_mock,
+ ):
+ lifecycle.run_shutdown()
+
+ assert not stale.exists()
+ wipe_mock.assert_not_called()
+
+
+def test_wipe_temp_db_rows_deletes_records_before_content(session, comfy_dirs):
+ record_id, content_id = _seed_temp_rows(session, comfy_dirs)
+ records_deleted, contents_deleted = wipe_temp_db_rows(session)
+ session.commit()
+
+ assert records_deleted == 1
+ assert contents_deleted == 1
+ assert session.get(Asset, record_id) is None
+ assert session.get(AssetContent, content_id) is None
+
+
+def test_wipe_temp_db_rows_preserves_non_temp_rows(session, comfy_dirs):
+ temp_record_id, temp_content_id = _seed_temp_rows(session, comfy_dirs)
+ keep_path = str(comfy_dirs) + "-sibling" + os.sep + "keep.safetensors"
+ keep_content = create_content(session, path=keep_path)
+ keep_record = create_record(session, content_id=keep_content.id, name="keep.safetensors")
+ session.commit()
+
+ records_deleted, contents_deleted = wipe_temp_db_rows(session)
+ session.commit()
+
+ assert (records_deleted, contents_deleted) == (1, 1)
+ assert session.get(Asset, temp_record_id) is None
+ assert session.get(AssetContent, temp_content_id) is None
+ assert session.get(Asset, keep_record.id) is not None
+ assert session.get(AssetContent, keep_content.id) is not None
+
+
+def _seed_paths(session: Session, paths: list[str]) -> list[str]:
+ stored: list[str] = []
+ for index, path in enumerate(paths):
+ content = create_content(session, path=path)
+ create_record(session, content_id=content.id, name=f"case-{index}.png")
+ stored.append(content.path)
+ session.commit()
+ return stored
+
+
+def _surviving_paths(session: Session) -> set[str]:
+ return set(session.scalars(select(AssetContent.path)).all())
+
+
+def test_wipe_deletes_exactly_what_is_path_under_prefixes_accepts(session, comfy_dirs):
+ temp_root = str(comfy_dirs)
+ stored = _seed_paths(session, [path for path, _ in prefix_case_paths(temp_root)])
+
+ records_deleted, contents_deleted = wipe_temp_db_rows(session)
+ session.commit()
+
+ wiped = expected_prefix_case_paths(temp_root)
+ assert _surviving_paths(session) == set(stored) - wiped
+ assert (records_deleted, contents_deleted) == (len(wiped), len(wiped))
+ assert wiped and wiped != set(stored)
+ case_different = os.path.join(
+ os.path.dirname(temp_root), os.path.basename(temp_root).upper(), "case.png"
+ )
+ assert case_different in _surviving_paths(session)
+ assert os.path.join(os.path.dirname(temp_root), "escaped.png") in _surviving_paths(session)
+
+
+def test_wipe_with_metacharacter_temp_root_matches_only_literal_children(session):
+ with tempfile.TemporaryDirectory() as base:
+ temp_root = os.path.join(base, "a_b%c*d?e[f")
+ inside = os.path.join(temp_root, "preview.png")
+ decoy = os.path.join(base, "aXbYcZdWeQf", "keep.safetensors")
+ _seed_paths(session, [inside, decoy])
+
+ with patch("folder_paths.get_temp_directory", return_value=temp_root):
+ wipe_temp_db_rows(session)
+ session.commit()
+
+ assert is_path_under_prefixes(decoy, [temp_root]) is False
+ assert _surviving_paths(session) == {decoy}
diff --git a/tests-unit/assets_test/services/test_manager_isolated.py b/tests-unit/assets_test/services/test_manager_isolated.py
new file mode 100644
index 000000000..2b73ba1bb
--- /dev/null
+++ b/tests-unit/assets_test/services/test_manager_isolated.py
@@ -0,0 +1,295 @@
+import dataclasses
+import threading
+from collections.abc import Callable, Generator, Iterator
+from contextlib import AbstractContextManager, contextmanager
+from pathlib import Path
+from typing import Protocol
+from unittest.mock import MagicMock, Mock, call
+
+import folder_paths
+import pytest
+from sqlalchemy import create_engine, select
+from sqlalchemy.orm import Session, Session as SASession
+from sqlalchemy.pool import StaticPool
+
+from app.assets import lifecycle
+from app.assets import manager as manager_module
+from app.assets import scanner, seeder as seeder_module
+from app.assets.database.models import Asset, AssetContent
+from app.assets.database.queries.records import create_content, create_record
+from app.assets.manager import AssetsEnabled
+from app.assets.seeder import ScanStatus, asset_seeder
+from app.database.models import Base
+from app.assets.services.schemas import RegisteredAsset, UploadAssetView
+
+
+class _ArgsStub:
+ enable_assets = True
+ enable_asset_hashing = False
+
+
+class _OutputSeeder(Protocol):
+ def wait(self, timeout: float | None = None) -> bool: ...
+
+ def get_status(self) -> ScanStatus: ...
+
+ def shutdown(self, timeout: float = 5.0) -> bool: ...
+
+
+def enabled_manager() -> AssetsEnabled:
+ return AssetsEnabled(_ArgsStub())
+
+
+def asset_roots(
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+) -> tuple[Path, Path, Path]:
+ output_dir = tmp_path / "output"
+ input_dir = tmp_path / "input"
+ temp_dir = tmp_path / "temp"
+ output_dir.mkdir()
+ input_dir.mkdir()
+ temp_dir.mkdir()
+ monkeypatch.setattr(folder_paths, "get_output_directory", lambda: str(output_dir))
+ monkeypatch.setattr(folder_paths, "get_input_directory", lambda: str(input_dir))
+ monkeypatch.setattr(folder_paths, "get_temp_directory", lambda: str(temp_dir))
+ return output_dir, input_dir, temp_dir
+
+
+def threaded_create_session(
+ monkeypatch: pytest.MonkeyPatch,
+) -> Iterator[Callable[[], AbstractContextManager[Session]]]:
+ engine = create_engine(
+ "sqlite://",
+ connect_args={"check_same_thread": False},
+ poolclass=StaticPool,
+ )
+ Base.metadata.create_all(engine)
+
+ @contextmanager
+ def _create_session() -> Generator[SASession, None, None]:
+ with SASession(engine) as session:
+ yield session
+
+ monkeypatch.setattr(seeder_module, "create_session", _create_session)
+ monkeypatch.setattr(scanner, "create_session", _create_session)
+ monkeypatch.setattr("app.assets.services.ingest.create_session", _create_session)
+ yield _create_session
+ engine.dispose()
+
+
+def output_seeder(monkeypatch: pytest.MonkeyPatch) -> Iterator[_OutputSeeder]:
+ seeder = asset_seeder.__class__()
+ monkeypatch.setattr(manager_module, "asset_seeder", seeder)
+ yield seeder
+ _ = seeder.shutdown()
+
+
+def test_queue_output_scan_registers_undeclared_output(
+ enabled_manager: AssetsEnabled,
+ asset_roots: tuple[Path, Path, Path],
+ threaded_create_session: Callable[[], AbstractContextManager[Session]],
+ output_seeder: _OutputSeeder,
+) -> None:
+ output_dir, _, _ = asset_roots
+ output_path = output_dir / "undeclared.bin"
+ output_path.write_bytes(b"custom node output")
+
+ enabled_manager.queue_output_scan()
+ assert output_seeder.wait(timeout=5)
+
+ with threaded_create_session() as session:
+ rows = list(
+ session.scalars(
+ select(Asset)
+ .join(AssetContent, Asset.content_id == AssetContent.id)
+ .where(AssetContent.path == str(output_path.resolve()))
+ )
+ )
+ assert rows[0].job_id is None
+ assert len(rows) == 1
+
+
+def test_queue_output_scan_does_not_duplicate_declared_output(
+ enabled_manager: AssetsEnabled,
+ asset_roots: tuple[Path, Path, Path],
+ threaded_create_session: Callable[[], AbstractContextManager[Session]],
+ output_seeder: _OutputSeeder,
+) -> None:
+ output_dir, _, _ = asset_roots
+ output_path = output_dir / "declared.bin"
+ output_path.write_bytes(b"declared custom node output")
+ registered = enabled_manager.register_executed_output(
+ str(output_path), job_id="declared-job"
+ )
+ assert registered is not None
+ undeclared_path = output_dir / "alongside.bin"
+ undeclared_path.write_bytes(b"undeclared custom node output")
+
+ enabled_manager.queue_output_scan()
+ assert output_seeder.wait(timeout=5)
+ assert output_seeder.get_status().errors == []
+
+ with threaded_create_session() as session:
+ rows = list(
+ session.scalars(
+ select(Asset)
+ .join(AssetContent, Asset.content_id == AssetContent.id)
+ .where(AssetContent.path == str(output_path.resolve()))
+ )
+ )
+ undeclared_rows = list(
+ session.scalars(
+ select(Asset)
+ .join(AssetContent, Asset.content_id == AssetContent.id)
+ .where(AssetContent.path == str(undeclared_path.resolve()))
+ )
+ )
+ assert rows[0].job_id == "declared-job"
+ assert undeclared_rows[0].job_id is None
+ assert len(rows) == 1
+ assert len(undeclared_rows) == 1, (
+ "the undeclared sibling proves the walk ran, so the declared row's count of 1 is a real skip rather than a silently-failed scan"
+ )
+
+
+def test_executed_and_cached_outputs_share_unhashed_content(
+ enabled_manager: AssetsEnabled,
+ asset_roots: tuple[Path, Path, Path],
+ mock_create_session: Callable[[], AbstractContextManager[Session]],
+) -> None:
+ output_dir, _, _ = asset_roots
+ output_path = output_dir / "executed.png"
+ output_path.write_bytes(b"executed output")
+
+ executed = enabled_manager.register_executed_output(
+ str(output_path), job_id="executed-job"
+ )
+
+ assert isinstance(executed, RegisteredAsset)
+ assert executed.id
+ assert executed.content_id
+ assert executed.name
+ assert executed.job_id == "executed-job"
+ assert not hasattr(executed, "asset_hash")
+ assert {field.name for field in dataclasses.fields(executed)} == {
+ "id",
+ "content_id",
+ "job_id",
+ "name",
+ }
+ with mock_create_session() as session:
+ asset = session.get(Asset, executed.id)
+ content = session.get(AssetContent, executed.content_id)
+ assert asset is not None
+ assert asset.content_id == executed.content_id
+ assert content is not None
+ assert content.hash is None
+
+ cached = enabled_manager.register_cached_output(str(output_path), job_id="cached-job")
+
+ assert isinstance(cached, RegisteredAsset)
+ assert cached.id != executed.id
+ assert cached.content_id == executed.content_id
+ assert cached.job_id == "cached-job"
+ assert (
+ enabled_manager.register_cached_output(
+ str(output_dir / "unknown.png"), job_id="unknown-job"
+ )
+ is None
+ )
+
+
+def test_register_upload_hashes_and_tags_fresh_input_file(
+ enabled_manager: AssetsEnabled,
+ asset_roots: tuple[Path, Path, Path],
+ mock_create_session: Callable[[], AbstractContextManager[Session]],
+) -> None:
+ _, input_dir, _ = asset_roots
+ upload_path = input_dir / "pasted" / "upload.png"
+ upload_path.parent.mkdir()
+ upload_path.write_bytes(b"uploaded input")
+
+ view = enabled_manager.register_upload(
+ str(upload_path),
+ name=upload_path.name,
+ upload_type="input",
+ subfolder="pasted",
+ content_written=True,
+ )
+
+ assert isinstance(view, UploadAssetView)
+ assert isinstance(view.asset, RegisteredAsset)
+ assert view.asset_hash
+ assert "pasted" in view.tags
+
+
+def test_startup_runs_against_memory_db_without_starting_a_scanner_thread(
+ enabled_manager: AssetsEnabled,
+ asset_roots: tuple[Path, Path, Path],
+ mock_create_session: Callable[[], AbstractContextManager[Session]],
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ _, _, temp_dir = asset_roots
+ (temp_dir / "stale.tmp").write_bytes(b"stale")
+ seeder_start = MagicMock(return_value=False)
+ monkeypatch.setattr(lifecycle, "create_session", mock_create_session)
+ monkeypatch.setattr(lifecycle, "start_asset_seeder", seeder_start)
+
+ thread_count = threading.active_count()
+ enabled_manager.startup()
+ assert threading.active_count() == thread_count, (
+ "start_asset_seeder is mocked, so a new thread means a component other than the seeder spawned one"
+ )
+
+ assert not temp_dir.exists()
+ seeder_start.assert_called_once_with()
+
+
+def test_ensure_scan_started_starts_the_lazy_object_info_scan(
+ enabled_manager: AssetsEnabled, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ seeder_start = MagicMock()
+ monkeypatch.setattr(asset_seeder, "start", seeder_start)
+
+ enabled_manager.ensure_scan_started()
+
+ seeder_start.assert_called_once_with(roots=("models", "input", "output"))
+
+
+def test_shutdown_runs_lifecycle_cleanup_when_seeder_shutdown_times_out(
+ enabled_manager: AssetsEnabled,
+ asset_roots: tuple[Path, Path, Path],
+ mock_create_session: Callable[[], AbstractContextManager[Session]],
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ _, _, temp_dir = asset_roots
+ stale_file = temp_dir / "stale.tmp"
+ stale_file.write_bytes(b"stale")
+ with mock_create_session() as session:
+ content = create_content(session, path=str(stale_file))
+ asset = create_record(session, content_id=content.id, name=stale_file.name)
+ session.commit()
+ asset_id = asset.id
+ content_id = content.id
+
+ calls = Mock()
+ seeder_shutdown = MagicMock(return_value=False)
+ run_shutdown = MagicMock(wraps=manager_module.run_shutdown)
+ calls.attach_mock(seeder_shutdown, "seeder_shutdown")
+ calls.attach_mock(run_shutdown, "run_shutdown")
+ monkeypatch.setattr(lifecycle, "can_create_session", lambda: True)
+ monkeypatch.setattr(lifecycle, "create_session", mock_create_session)
+ monkeypatch.setattr(asset_seeder, "shutdown", seeder_shutdown)
+ monkeypatch.setattr(manager_module, "run_shutdown", run_shutdown)
+
+ enabled_manager.shutdown()
+
+ assert calls.mock_calls == [call.seeder_shutdown(), call.run_shutdown()]
+ assert not temp_dir.exists()
+ with mock_create_session() as session:
+ assert session.get(Asset, asset_id) is None
+ assert session.get(AssetContent, content_id) is None
diff --git a/tests-unit/assets_test/services/test_metadata_update_b.py b/tests-unit/assets_test/services/test_metadata_update_b.py
new file mode 100644
index 000000000..20ba1b3b1
--- /dev/null
+++ b/tests-unit/assets_test/services/test_metadata_update_b.py
@@ -0,0 +1,59 @@
+import pytest
+
+from app.assets.database.models import Asset, AssetTag, Tag
+from app.assets.database.queries.records import (
+ create_content,
+ create_record,
+ fetch_record_tags,
+)
+from app.assets.services.asset_management import update_asset_metadata
+from app.assets.services.tagging import apply_tags, remove_tags
+
+
+def _create_record(session, path: str, tags: list[str] | None = None) -> Asset:
+ content = create_content(session, path)
+ record = create_record(session, content.id, "original", tags=tags)
+ session.commit()
+ return record
+
+
+def test_rename_via_update_asset_metadata(session, mock_create_session):
+ record = _create_record(session, "/output/rename.png")
+
+ update_asset_metadata(record.id, name="renamed")
+
+ session.expire_all()
+ assert session.get(Asset, record.id).name == "renamed"
+
+
+def test_apply_tags_adds_tags(session, mock_create_session):
+ record = _create_record(session, "/output/apply-tags.png")
+
+ apply_tags(record.id, ["foo", "bar"])
+
+ session.expire_all()
+ assert fetch_record_tags(session, record.id) == ["bar", "foo"]
+
+
+def test_remove_tags_removes_non_automatic(session, mock_create_session):
+ record = _create_record(session, "/output/remove-tags.png", tags=["foo"])
+ session.add(Tag(name="automatic"))
+ session.add(AssetTag(asset_id=record.id, tag_name="automatic", origin="automatic"))
+ session.commit()
+
+ remove_tags(record.id, ["foo"])
+
+ session.expire_all()
+ assert fetch_record_tags(session, record.id) == ["automatic"]
+
+
+def test_update_asset_metadata_unknown_preview_id_raises(session, mock_create_session):
+ record = _create_record(session, "/output/preview-validate.png")
+
+ with pytest.raises(ValueError):
+ update_asset_metadata(
+ record.id, preview_id="00000000-0000-0000-0000-000000000000"
+ )
+
+ session.expire_all()
+ assert session.get(Asset, record.id).preview_id is None
diff --git a/tests-unit/assets_test/services/test_noassets_parity.py b/tests-unit/assets_test/services/test_noassets_parity.py
new file mode 100644
index 000000000..5c7b646b6
--- /dev/null
+++ b/tests-unit/assets_test/services/test_noassets_parity.py
@@ -0,0 +1,202 @@
+from collections.abc import Callable
+from contextlib import AbstractContextManager
+import logging
+from pathlib import Path
+from unittest.mock import Mock, patch
+
+import pytest
+from aiohttp import web
+from aiohttp.pytest_plugin import AiohttpClient
+from sqlalchemy.orm import Session
+
+import folder_paths
+from app.assets import lifecycle
+from app.assets.api import routes
+from app.assets.database.models import Asset, AssetContent
+from app.assets.database.queries.records import create_content, create_record
+from app.assets import manager
+from app.assets.manager import AssetsEnabled, NoAssets
+from app.assets.mode import hashing_enabled
+from app.assets.seeder import asset_seeder
+from app.assets.services.hash_mode_state import read_stored_mode
+
+
+class _Args:
+ enable_assets = False
+
+ def __init__(self, hashing: bool) -> None:
+ self.enable_asset_hashing = hashing
+
+
+def _no_assets(*, hashing: bool = False) -> NoAssets:
+ return NoAssets(_Args(hashing))
+
+
+async def test_noassets_register_routes_returns_service_disabled_and_disables_seeder(
+ aiohttp_client: AiohttpClient, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ monkeypatch.setattr(routes, "_ASSETS_ENABLED", False)
+ monkeypatch.setattr(asset_seeder, "_disabled", False)
+ app = web.Application()
+
+ _no_assets().register_routes(app, None)
+ client = await aiohttp_client(app)
+ response = await client.get("/api/assets")
+
+ assert response.status == 503
+ assert (await response.json())["error"]["code"] == "SERVICE_DISABLED"
+ assert asset_seeder.is_disabled()
+
+
+def test_noassets_startup_applies_hash_mode_persists_state_and_cleans_temp_dir(
+ mock_create_session: Callable[[], AbstractContextManager[Session]],
+ session: Session,
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ temp_file = tmp_path / "startup-stale.bin"
+ temp_file.write_bytes(b"stale")
+ monkeypatch.setattr(lifecycle, "create_session", mock_create_session)
+ monkeypatch.setattr(folder_paths, "get_temp_directory", lambda: str(tmp_path))
+
+ _no_assets(hashing=True).startup()
+
+ assert hashing_enabled() is True
+ assert read_stored_mode(session) == "on"
+ assert not temp_file.exists()
+
+
+def test_noassets_shutdown_wipes_temp_rows_and_files(
+ mock_create_session: Callable[[], AbstractContextManager[Session]],
+ session: Session,
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ manager = _no_assets()
+ monkeypatch.setattr(lifecycle, "create_session", mock_create_session)
+ monkeypatch.setattr(lifecycle, "can_create_session", lambda: True)
+ monkeypatch.setattr(folder_paths, "get_temp_directory", lambda: str(tmp_path))
+ manager.startup()
+
+ tmp_path.mkdir()
+ temp_file = tmp_path / "shutdown-stale.bin"
+ temp_file.write_bytes(b"stale")
+ content = create_content(session, str(temp_file))
+ record = create_record(session, content.id, temp_file.name)
+ session.commit()
+ record_id = record.id
+ content_id = content.id
+
+ manager.shutdown()
+ session.expire_all()
+
+ assert session.get(Asset, record_id) is None
+ assert session.get(AssetContent, content_id) is None
+ assert not temp_file.exists()
+
+
+def test_noassets_shutdown_runs_cleanup_after_unsuccessful_seeder_shutdown(
+ mock_create_session: Callable[[], AbstractContextManager[Session]],
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ temp_file = tmp_path / "seeder-timeout.bin"
+ temp_file.write_bytes(b"stale")
+ calls: list[str] = []
+ monkeypatch.setattr(lifecycle, "create_session", mock_create_session)
+ monkeypatch.setattr(lifecycle, "can_create_session", lambda: True)
+ monkeypatch.setattr(folder_paths, "get_temp_directory", lambda: str(tmp_path))
+
+ def seeder_shutdown() -> bool:
+ calls.append("seeder")
+ return False
+
+ def cleanup() -> None:
+ calls.append("cleanup")
+ lifecycle.run_shutdown()
+
+ with (
+ patch.object(asset_seeder, "shutdown", side_effect=seeder_shutdown),
+ patch("app.assets.manager.run_shutdown", side_effect=cleanup) as cleanup_spy,
+ ):
+ _no_assets().shutdown()
+
+ cleanup_spy.assert_called_once_with()
+ assert calls == ["seeder", "cleanup"]
+ assert not temp_file.exists()
+
+
+def test_noassets_shutdown_without_database_sweeps_files_without_session_access(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ temp_file = tmp_path / "no-database.bin"
+ temp_file.write_bytes(b"stale")
+ create_session = Mock()
+ monkeypatch.setattr(lifecycle, "can_create_session", lambda: False)
+ monkeypatch.setattr(lifecycle, "create_session", create_session)
+ monkeypatch.setattr(folder_paths, "get_temp_directory", lambda: str(tmp_path))
+
+ _no_assets().shutdown()
+
+ assert not temp_file.exists()
+ create_session.assert_not_called()
+
+
+def test_noassets_callbacks_are_noops_without_seeder_side_effects() -> None:
+ manager = _no_assets()
+ with (
+ patch.object(asset_seeder, "start") as start,
+ patch.object(asset_seeder, "pause") as pause,
+ patch.object(asset_seeder, "enqueue_scan") as enqueue_scan,
+ patch.object(asset_seeder, "resume") as resume,
+ patch.object(asset_seeder, "set_event_sink") as set_event_sink,
+ ):
+ assert manager.ensure_scan_started() is None
+ assert manager.pause_background_scan() is None
+ assert manager.queue_output_scan() is None
+ assert manager.resume_background_scan() is None
+ assert manager.set_event_sink(Mock()) is None
+
+ start.assert_not_called()
+ pause.assert_not_called()
+ enqueue_scan.assert_not_called()
+ resume.assert_not_called()
+ set_event_sink.assert_not_called()
+
+
+def test_noassets_registration_methods_return_none() -> None:
+ manager = _no_assets()
+
+ assert manager.register_upload(
+ "/tmp/output.png", "output.png", "output", "", content_written=True
+ ) is None
+ assert manager.register_executed_output("/tmp/output.png", "job-id") is None
+ assert manager.register_cached_output("/tmp/output.png", "job-id") is None
+
+
+def test_noassets_is_disabled() -> None:
+ assert _no_assets().enabled is False
+
+
+def test_default_asset_manager_disables_assets_when_dependencies_are_unavailable(
+ monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
+) -> None:
+ monkeypatch.setattr(manager.args, "enable_assets", True)
+ monkeypatch.setattr(manager, "dependencies_available", lambda: False)
+
+ with caplog.at_level(logging.WARNING):
+ asset_manager = manager.default_asset_manager()
+
+ assert isinstance(asset_manager, NoAssets)
+ assert "asset endpoints will answer 503" in caplog.text
+ assert "requirements.txt" in caplog.text
+
+
+def test_default_asset_manager_enables_assets_when_dependencies_are_available(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ monkeypatch.setattr(manager.args, "enable_assets", True)
+ monkeypatch.setattr(manager, "dependencies_available", lambda: True)
+
+ assert isinstance(manager.default_asset_manager(), AssetsEnabled)
diff --git a/tests-unit/assets_test/services/test_null_hash_recovery.py b/tests-unit/assets_test/services/test_null_hash_recovery.py
new file mode 100644
index 000000000..2a247fced
--- /dev/null
+++ b/tests-unit/assets_test/services/test_null_hash_recovery.py
@@ -0,0 +1,198 @@
+import os
+from pathlib import Path
+from unittest.mock import patch
+
+import pytest
+from sqlalchemy import select
+
+from app.assets.database.models import Asset, AssetContent, AssetTag
+from app.assets.database.queries.records import create_content, create_record
+from app.assets.helpers import to_stored_hash
+from app.assets.scanner import SeedAssetSpec, clear_pending_verifications, seed_asset_specs
+from app.assets.services import hash_mode_state
+from app.assets.services.hash_mode_state import (
+ clear_transition_queue,
+ drain_transition_queue,
+ enqueue_transition_work,
+ record_transition_intent,
+ write_stored_mode,
+)
+from app.assets.services.snapshot_hash import snapshot_hash
+
+
[email protected](autouse=True)
+def _clear_queues():
+ clear_transition_queue()
+ clear_pending_verifications()
+ yield
+ clear_transition_queue()
+ clear_pending_verifications()
+
+
+def _stored_hash(path: Path) -> str:
+ snapshot = snapshot_hash(str(path))
+ assert snapshot is not None
+ digest, _ = snapshot
+ return to_stored_hash(digest)
+
+
+def _spec(path: Path) -> SeedAssetSpec:
+ stat = path.stat()
+ return {
+ "abs_path": str(path),
+ "size_bytes": stat.st_size,
+ "mtime_ns": stat.st_mtime_ns,
+ "info_name": path.name,
+ "tags": ["input"],
+ "fname": path.name,
+ "metadata": None,
+ "mime_type": None,
+ "job_id": None,
+ }
+
+
+def test_deleted_null_hash_row_recovers_via_scanner_after_restore(
+ session, temp_dir, monkeypatch
+):
+ path = temp_dir / "recoverable.bin"
+ monkeypatch.setattr("folder_paths.get_input_directory", lambda: str(temp_dir))
+ original_bytes = b"the exact bytes that come back"
+ path.write_bytes(original_bytes)
+ stat = path.stat()
+ content = create_content(session, str(path), size_bytes=stat.st_size, mtime_ns=stat.st_mtime_ns)
+ content_id = content.id
+ record = create_record(session, content_id, "recoverable.bin", tags=["input"])
+ record_id = record.id
+ session.commit()
+
+ write_stored_mode(session, "off")
+ monkeypatch.setattr(hash_mode_state._mode, "hashing_enabled", lambda: True)
+ path.unlink()
+ transition = record_transition_intent(session)
+ enqueue_transition_work(session, transition)
+ drain_transition_queue(session)
+ session.commit()
+ assert session.get(AssetContent, content_id).is_missing is True, (
+ "precondition: A1's drain marked the row missing on delete"
+ )
+
+ path.write_bytes(original_bytes)
+ os.utime(path, ns=(stat.st_atime_ns, stat.st_mtime_ns))
+ assert path.stat().st_mtime_ns == stat.st_mtime_ns, "setup: mtime must round-trip exactly"
+
+ with patch("app.assets.scanner.mode.hashing_enabled", return_value=True):
+ created = seed_asset_specs(session, [_spec(path)])
+ session.commit()
+
+ assert created == 0, "the original row must recover — no fresh content row minted"
+ recovered = session.get(AssetContent, content_id)
+ assert recovered.is_missing is False
+ assert recovered.hash == _stored_hash(path)
+ assert len(session.scalars(select(AssetContent)).all()) == 1, "no duplicate content row"
+ assert len(session.scalars(select(Asset)).all()) == 1, "no duplicate record"
+ assert session.get(Asset, record_id) is not None, "the original record's identity survives"
+ assert session.get(AssetTag, {"asset_id": record_id, "tag_name": "missing"}) is None
+
+
+def test_different_bytes_restored_at_same_path_does_not_recover_old_row(
+ session, temp_dir, monkeypatch
+):
+ path = temp_dir / "replaced.bin"
+ monkeypatch.setattr("folder_paths.get_input_directory", lambda: str(temp_dir))
+ path.write_bytes(b"original bytes")
+ stat = path.stat()
+ content = create_content(session, str(path), size_bytes=stat.st_size, mtime_ns=stat.st_mtime_ns)
+ content_id = content.id
+ create_record(session, content_id, "replaced.bin")
+ session.commit()
+
+ write_stored_mode(session, "off")
+ monkeypatch.setattr(hash_mode_state._mode, "hashing_enabled", lambda: True)
+ path.unlink()
+ transition = record_transition_intent(session)
+ enqueue_transition_work(session, transition)
+ drain_transition_queue(session)
+ session.commit()
+ assert session.get(AssetContent, content_id).is_missing is True
+
+ path.write_bytes(b"a completely different, much longer payload than the original")
+
+ with patch("app.assets.scanner.mode.hashing_enabled", return_value=True):
+ created = seed_asset_specs(session, [_spec(path)])
+ session.commit()
+
+ assert created == 1, "a genuinely different file must take the normal new-content path"
+ assert session.get(AssetContent, content_id).is_missing is True, (
+ "the old row must stay missing — recovering it here would hand the wrong record's "
+ "identity, tags, and metadata to unrelated bytes"
+ )
+ assert len(session.scalars(select(AssetContent)).all()) == 2
+
+
+def test_same_size_different_mtime_restored_at_same_path_does_not_recover_old_row(
+ session, temp_dir, monkeypatch
+):
+ path = temp_dir / "retimed.bin"
+ monkeypatch.setattr("folder_paths.get_input_directory", lambda: str(temp_dir))
+ original_bytes = b"identical length, different moment in time"
+ path.write_bytes(original_bytes)
+ stat = path.stat()
+ content = create_content(session, str(path), size_bytes=stat.st_size, mtime_ns=stat.st_mtime_ns)
+ content_id = content.id
+ create_record(session, content_id, "retimed.bin")
+ session.commit()
+
+ write_stored_mode(session, "off")
+ monkeypatch.setattr(hash_mode_state._mode, "hashing_enabled", lambda: True)
+ path.unlink()
+ transition = record_transition_intent(session)
+ enqueue_transition_work(session, transition)
+ drain_transition_queue(session)
+ session.commit()
+ assert session.get(AssetContent, content_id).is_missing is True
+
+ path.write_bytes(original_bytes)
+ shifted_ns = stat.st_mtime_ns + 5_000_000_000
+ os.utime(path, ns=(stat.st_atime_ns, shifted_ns))
+ assert path.stat().st_mtime_ns != stat.st_mtime_ns, "setup: mtime must actually differ"
+ assert path.stat().st_size == stat.st_size, "setup: size must match so only mtime disambiguates"
+
+ with patch("app.assets.scanner.mode.hashing_enabled", return_value=True):
+ created = seed_asset_specs(session, [_spec(path)])
+ session.commit()
+
+ assert created == 1, "a same-size-but-different-mtime restore must take the new-content path"
+ assert session.get(AssetContent, content_id).is_missing is True, (
+ "a matching size with a mismatched mtime is not proof the old row's bytes are back — "
+ "recovering here would hand the wrong record's identity to different bytes"
+ )
+ assert len(session.scalars(select(AssetContent)).all()) == 2
+
+
+def test_two_missing_null_hash_candidates_at_same_path_do_not_recover(
+ session, temp_dir, monkeypatch
+):
+ path = temp_dir / "ambiguous_null.bin"
+ monkeypatch.setattr("folder_paths.get_input_directory", lambda: str(temp_dir))
+ path.write_bytes(b"bytes shared by two missing generations")
+ stat = path.stat()
+ first = AssetContent(
+ path=str(path), hash=None, is_missing=True,
+ size_bytes=stat.st_size, mtime_ns=stat.st_mtime_ns,
+ )
+ second = AssetContent(
+ path=str(path), hash=None, is_missing=True,
+ size_bytes=stat.st_size, mtime_ns=stat.st_mtime_ns,
+ )
+ session.add_all([first, second])
+ session.flush()
+ first_id, second_id = first.id, second.id
+ session.commit()
+
+ with patch("app.assets.scanner.mode.hashing_enabled", return_value=True):
+ created = seed_asset_specs(session, [_spec(path)])
+ session.commit()
+
+ assert created == 1, "ambiguous candidates must fall through to the normal new-content path"
+ assert session.get(AssetContent, first_id).is_missing is True
+ assert session.get(AssetContent, second_id).is_missing is True
diff --git a/tests-unit/assets_test/services/test_output_registration.py b/tests-unit/assets_test/services/test_output_registration.py
new file mode 100644
index 000000000..65c8ab664
--- /dev/null
+++ b/tests-unit/assets_test/services/test_output_registration.py
@@ -0,0 +1,143 @@
+import os
+from pathlib import Path
+from unittest.mock import patch
+
+import folder_paths
+from sqlalchemy import select
+
+from app.assets.database.models import Asset, AssetContent
+from app.assets.manager import AssetsEnabled, NoAssets
+from comfy_execution.asset_enrichment import (
+ register_cached_outputs,
+ register_executed_outputs,
+)
+
+class _ArgsStub:
+ def __init__(self, enable_assets: bool = True) -> None:
+ self.enable_assets = enable_assets
+ self.enable_asset_hashing = False
+
+
+def _write_output_file(name: str, data: bytes) -> Path:
+ path = Path(folder_paths.get_output_directory()) / name
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_bytes(data)
+ return path
+
+
+def _output_ui(name: str) -> dict:
+ return {"images": [{"filename": name, "subfolder": "", "type": "output"}]}
+
+
+def _wrapper(name: str, node_id: str = "1") -> dict:
+ return {"meta": {"node_id": node_id}, "output": _output_ui(name)}
+
+
+def test_executed_adapter_registers_new_output(mock_create_session):
+ path = _write_output_file("adapter-executed-new.png", b"exec")
+ try:
+ output_ui = _output_ui(path.name)
+
+ enriched = register_executed_outputs(
+ output_ui, "exec-job", AssetsEnabled(_ArgsStub())
+ )
+
+ new_id = enriched["images"][0]["id"]
+ assert "id" not in output_ui["images"][0]
+ with mock_create_session() as session:
+ record = session.get(Asset, new_id)
+ content = session.get(AssetContent, record.content_id)
+ assert record.job_id == "exec-job"
+ assert content.path == os.path.abspath(path)
+ assert content.is_missing is False
+ finally:
+ path.unlink(missing_ok=True)
+
+
+def test_executed_adapter_over_existing_path_marks_old_missing(mock_create_session):
+ path = _write_output_file("adapter-executed-replace.png", b"original")
+ try:
+ manager = AssetsEnabled(_ArgsStub())
+ first = register_executed_outputs(_output_ui(path.name), "job-1", manager)
+ old_id = first["images"][0]["id"]
+
+ path.write_bytes(b"replacement")
+ second = register_executed_outputs(_output_ui(path.name), "job-2", manager)
+ new_id = second["images"][0]["id"]
+
+ assert new_id != old_id
+ with mock_create_session() as session:
+ old_content = session.get(AssetContent, session.get(Asset, old_id).content_id)
+ new_content = session.get(AssetContent, session.get(Asset, new_id).content_id)
+ assert old_content.is_missing is True
+ assert new_content.is_missing is False
+ contents = list(
+ session.scalars(
+ select(AssetContent).where(AssetContent.path == os.path.abspath(path))
+ )
+ )
+ assert len(contents) == 2
+ finally:
+ path.unlink(missing_ok=True)
+
+
+def test_executed_adapter_disabled_registers_nothing(mock_create_session):
+ path = _write_output_file("adapter-executed-disabled.png", b"noop")
+ try:
+ output_ui = _output_ui(path.name)
+
+ enriched = register_executed_outputs(output_ui, "job", NoAssets(_ArgsStub(False)))
+
+ assert "id" not in enriched["images"][0]
+ with mock_create_session() as session:
+ contents = list(
+ session.scalars(
+ select(AssetContent).where(AssetContent.path == os.path.abspath(path))
+ )
+ )
+ assert contents == []
+ finally:
+ path.unlink(missing_ok=True)
+
+
+def test_executed_adapter_registration_failure_never_raises(mock_create_session):
+ path = _write_output_file("adapter-executed-error.png", b"exec")
+ try:
+ output_ui = _output_ui(path.name)
+
+ with patch(
+ "app.assets.manager.ingest_register_executed_output",
+ side_effect=RuntimeError("boom"),
+ ):
+ enriched = register_executed_outputs(
+ output_ui, "job", AssetsEnabled(_ArgsStub())
+ )
+
+ assert "id" not in enriched["images"][0]
+ finally:
+ path.unlink(missing_ok=True)
+
+
+def test_cached_adapter_without_live_content_is_nonevent(mock_create_session):
+ path = _write_output_file("adapter-cached-missing.png", b"new content")
+ try:
+ wrapper = _wrapper(path.name)
+
+ enriched = register_cached_outputs(
+ wrapper, "cached-job", AssetsEnabled(_ArgsStub())
+ )
+
+ assert "id" not in enriched["output"]["images"][0]
+ with mock_create_session() as session:
+ contents = list(
+ session.scalars(
+ select(AssetContent).where(AssetContent.path == os.path.abspath(path))
+ )
+ )
+ records = list(
+ session.scalars(select(Asset).where(Asset.job_id == "cached-job"))
+ )
+ assert contents == []
+ assert records == []
+ finally:
+ path.unlink(missing_ok=True)
diff --git a/tests-unit/assets_test/services/test_owner_purge.py b/tests-unit/assets_test/services/test_owner_purge.py
new file mode 100644
index 000000000..dd504b5d5
--- /dev/null
+++ b/tests-unit/assets_test/services/test_owner_purge.py
@@ -0,0 +1,15 @@
+import subprocess
+from pathlib import Path
+
+
+def test_no_owner_id_in_asset_modules():
+ repository = Path(__file__).resolve().parents[3]
+ result = subprocess.run(
+ ["grep", "-rn", "--exclude=*.pyc", "owner_id", "app/assets/"],
+ cwd=repository,
+ check=False,
+ capture_output=True,
+ text=True,
+ )
+
+ assert result.stdout == ""
diff --git a/tests-unit/assets_test/services/test_preview_path_resolution.py b/tests-unit/assets_test/services/test_preview_path_resolution.py
new file mode 100644
index 000000000..90b5aee86
--- /dev/null
+++ b/tests-unit/assets_test/services/test_preview_path_resolution.py
@@ -0,0 +1,64 @@
+from sqlalchemy import event
+
+from app.assets.database.queries.records import (
+ create_content,
+ create_record,
+ mark_content_missing,
+)
+from app.assets.services.asset_management import get_preview_file_paths
+
+
+def test_preview_paths_resolve_preview_record_content(session, mock_create_session) -> None:
+ preview_content = create_content(session, "/output/preview.png")
+ preview = create_record(session, preview_content.id, "preview.png")
+ record_content = create_content(session, "/output/record.png")
+ record = create_record(session, record_content.id, "record.png")
+ record.preview_id = preview.id
+ session.commit()
+
+ paths = get_preview_file_paths([preview.id])
+
+ assert paths == {preview.id: preview_content.path}
+
+
+def test_preview_paths_exclude_missing_preview_content(session, mock_create_session) -> None:
+ preview_content = create_content(session, "/output/missing-preview.png")
+ preview = create_record(session, preview_content.id, "missing-preview.png")
+ record_content = create_content(session, "/output/record.png")
+ record = create_record(session, record_content.id, "record.png")
+ record.preview_id = preview.id
+ mark_content_missing(session, preview_content.id)
+ session.commit()
+
+ paths = get_preview_file_paths([preview.id])
+
+ assert paths == {}
+
+
+def test_preview_paths_resolve_a_page_in_one_query(session, mock_create_session, db_engine) -> None:
+ preview_ids: list[str] = []
+ expected_paths: dict[str, str] = {}
+ for index in range(3):
+ preview_path = f"/output/preview-{index}.png"
+ preview_content = create_content(session, preview_path)
+ preview = create_record(session, preview_content.id, f"preview-{index}.png")
+ record_content = create_content(session, f"/output/record-{index}.png")
+ record = create_record(session, record_content.id, f"record-{index}.png")
+ record.preview_id = preview.id
+ preview_ids.append(preview.id)
+ expected_paths[preview.id] = preview_content.path
+ session.commit()
+
+ statements: list[str] = []
+
+ def count_statements(_, __, statement, ___, ____, _____) -> None:
+ statements.append(statement)
+
+ event.listen(db_engine, "before_cursor_execute", count_statements)
+ try:
+ paths = get_preview_file_paths(preview_ids)
+ finally:
+ event.remove(db_engine, "before_cursor_execute", count_statements)
+
+ assert paths == expected_paths
+ assert len(statements) == 1
diff --git a/tests-unit/assets_test/services/test_recovery_gate.py b/tests-unit/assets_test/services/test_recovery_gate.py
new file mode 100644
index 000000000..eeca7e0b0
--- /dev/null
+++ b/tests-unit/assets_test/services/test_recovery_gate.py
@@ -0,0 +1,168 @@
+from pathlib import Path
+from unittest.mock import patch
+
+import pytest
+from sqlalchemy import select
+
+from app.assets.database.models import Asset, AssetContent, AssetTag, Tag
+from app.assets.helpers import to_stored_hash
+from app.assets.scanner import (
+ SeedAssetSpec,
+ clear_pending_verifications,
+ pending_recovery_count,
+ seed_asset_specs,
+)
+from app.assets.scanner_changes import recover_missing_content
+from app.assets.services.snapshot_hash import snapshot_hash
+
+
[email protected](autouse=True)
+def _clear_queues():
+ clear_pending_verifications()
+ yield
+ clear_pending_verifications()
+
+
+def _missing_content(session, path: Path, hash_value: str) -> tuple[AssetContent, Asset]:
+ content = AssetContent(path=str(path), hash=hash_value, is_missing=True)
+ session.add(content)
+ session.flush()
+ record = Asset(content_id=content.id, name=path.name)
+ session.add(record)
+ session.flush()
+ if session.get(Tag, "missing") is None:
+ session.add(Tag(name="missing"))
+ session.add(AssetTag(asset_id=record.id, tag_name="missing", origin="automatic"))
+ session.commit()
+ return content, record
+
+
+def _spec(path: Path) -> SeedAssetSpec:
+ stat = path.stat()
+ return {
+ "abs_path": str(path),
+ "size_bytes": stat.st_size,
+ "mtime_ns": stat.st_mtime_ns,
+ "info_name": path.name,
+ "tags": ["input"],
+ "fname": path.name,
+ "metadata": None,
+ "mime_type": None,
+ "job_id": None,
+ }
+
+
+def _stored_hash(path: Path) -> str:
+ snapshot = snapshot_hash(str(path))
+ assert snapshot is not None
+ digest, _ = snapshot
+ return to_stored_hash(digest)
+
+
+def test_single_hash_match_recovers(session, temp_dir: Path):
+ path = temp_dir / "restored.bin"
+ path.write_bytes(b"restored bytes")
+ content, record = _missing_content(session, path, _stored_hash(path))
+
+ with patch("app.assets.scanner.mode.hashing_enabled", return_value=True):
+ created = seed_asset_specs(session, [_spec(path)])
+ session.commit()
+
+ assert created == 0
+ assert session.get(AssetContent, content.id).is_missing is False
+ assert session.get(AssetTag, {"asset_id": record.id, "tag_name": "missing"}) is None
+
+
+def test_ambiguous_hash_match_recovers_nothing(session, temp_dir: Path):
+ path = temp_dir / "ambiguous.bin"
+ path.write_bytes(b"same bytes")
+ digest = _stored_hash(path)
+ first, _ = _missing_content(session, path, digest)
+ second, _ = _missing_content(session, path, digest)
+
+ with patch("app.assets.scanner.mode.hashing_enabled", return_value=True):
+ created = seed_asset_specs(session, [_spec(path)])
+ session.commit()
+
+ assert created == 1
+ assert session.get(AssetContent, first.id).is_missing is True
+ assert session.get(AssetContent, second.id).is_missing is True
+ assert len(session.scalars(select(AssetContent)).all()) == 3
+
+
+def test_no_hash_match_creates_fresh_rows(session, temp_dir: Path):
+ path = temp_dir / "different.bin"
+ path.write_bytes(b"current bytes")
+ missing, _ = _missing_content(session, path, "old")
+
+ with patch("app.assets.scanner.mode.hashing_enabled", return_value=True):
+ created = seed_asset_specs(session, [_spec(path)])
+ session.commit()
+
+ assert created == 1
+ assert session.get(AssetContent, missing.id).is_missing is True
+ assert len(session.scalars(select(Asset)).all()) == 2
+
+
+def test_off_mode_no_recovery(session, temp_dir: Path):
+ path = temp_dir / "off.bin"
+ path.write_bytes(b"bytes")
+ missing, _ = _missing_content(session, path, _stored_hash(path))
+
+ with (
+ patch("app.assets.scanner.mode.hashing_enabled", return_value=False),
+ patch("app.assets.scanner_changes.snapshot_hash") as hash_mock,
+ ):
+ created = seed_asset_specs(session, [_spec(path)])
+ session.commit()
+
+ hash_mock.assert_not_called()
+ assert created == 1
+ assert session.get(AssetContent, missing.id).is_missing is True
+
+
+def test_unstable_hash_requeues(session, temp_dir: Path):
+ path = temp_dir / "unstable.bin"
+ path.write_bytes(b"bytes")
+ missing, _ = _missing_content(session, path, "old")
+
+ with (
+ patch("app.assets.scanner.mode.hashing_enabled", return_value=True),
+ patch("app.assets.scanner_changes.snapshot_hash", return_value=None),
+ ):
+ created = seed_asset_specs(session, [_spec(path)])
+ session.commit()
+
+ assert created == 0
+ assert pending_recovery_count() == 1
+ assert session.get(AssetContent, missing.id).is_missing is True
+
+
+def test_recovery_skips_a_path_a_live_row_already_occupies(session, temp_dir: Path):
+ path = temp_dir / "contested.bin"
+ path.write_bytes(b"bytes restored under a path someone else already owns")
+ digest = _stored_hash(path)
+ stat_result = path.stat()
+ missing_a, _ = _missing_content(session, path, digest)
+ live_b = AssetContent(
+ path=str(path),
+ hash=digest,
+ is_missing=False,
+ size_bytes=stat_result.st_size,
+ mtime_ns=stat_result.st_mtime_ns,
+ )
+ session.add(live_b)
+ session.commit()
+ live_b_id = live_b.id
+
+ result = recover_missing_content(
+ session, str(path), stat_result, hashing_is_enabled=True
+ )
+ session.commit()
+
+ assert result == "no_match", (
+ "the file at an occupied path belongs to the live row's story; change detection "
+ "owns it, and recovering onto it would put two live rows on one path"
+ )
+ assert session.get(AssetContent, missing_a.id).is_missing is True
+ assert session.get(AssetContent, live_b_id).is_missing is False
diff --git a/tests-unit/assets_test/services/test_registration_primitives.py b/tests-unit/assets_test/services/test_registration_primitives.py
new file mode 100644
index 000000000..7f0df1ea3
--- /dev/null
+++ b/tests-unit/assets_test/services/test_registration_primitives.py
@@ -0,0 +1,273 @@
+import os
+from datetime import datetime
+
+import folder_paths
+from sqlalchemy import select
+
+from app.assets.database.models import Asset, AssetContent
+from app.assets.database.queries.records import create_content, create_record
+from app.assets.services.ingest import (
+ register_cached_output,
+ register_executed_output,
+ register_file_in_place,
+)
+
+
+def _output_path(name: str) -> str:
+ output_dir = folder_paths.get_output_directory()
+ os.makedirs(output_dir, exist_ok=True)
+ return os.path.join(output_dir, name)
+
+
+def test_executed_creates_metadata_and_null_hash(mock_create_session):
+ body = b"pixels"
+ path = _output_path("primitives_executed_meta.png")
+ with open(path, "wb") as fh:
+ fh.write(body)
+
+ try:
+ record = register_executed_output(path, job_id="job-exec")
+
+ assert record is not None
+ with mock_create_session() as session:
+ content = session.scalar(
+ select(AssetContent).where(
+ AssetContent.path == os.path.abspath(path)
+ )
+ )
+ asset = session.get(Asset, record.id)
+
+ assert content.hash is None
+ assert content.is_missing is False
+ assert asset.system_metadata is not None
+ assert asset.system_metadata.get("format") == "png"
+ assert asset.system_metadata.get("content_length") == len(body)
+ assert "filename" in asset.system_metadata
+ finally:
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def test_executed_over_live_path_marks_old_missing(mock_create_session):
+ path = _output_path("primitives_executed_overwrite.png")
+
+ try:
+ with open(path, "wb") as fh:
+ fh.write(b"v1")
+ first = register_executed_output(path, job_id="job1")
+
+ with open(path, "wb") as fh:
+ fh.write(b"v2")
+ second = register_executed_output(path, job_id="job2")
+
+ assert first is not None
+ assert second is not None
+ assert second.id != first.id
+ with mock_create_session() as session:
+ old_content = session.get(AssetContent, first.content_id)
+ old_record = session.get(Asset, first.id)
+ new_record = session.get(Asset, second.id)
+
+ assert old_content.is_missing is True
+ assert old_record.job_id == "job1"
+ assert new_record.job_id == "job2"
+ assert new_record.content_id != first.content_id
+ finally:
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def test_cached_copies_sibling_metadata(mock_create_session):
+ sibling_metadata = {"provenance": "sibling", "width": 4, "kind": "image"}
+ path = _output_path("primitives_cached_copy.png")
+ with open(path, "wb") as fh:
+ fh.write(b"pixels")
+
+ try:
+ with mock_create_session() as session:
+ content = create_content(session, os.path.abspath(path), None, 6, 0)
+ create_record(
+ session,
+ content.id,
+ "sibling",
+ system_metadata=dict(sibling_metadata),
+ )
+ session.commit()
+ content_id = content.id
+
+ cached = register_cached_output(path, job_id="delivery-job")
+
+ assert cached is not None
+ assert cached.content_id == content_id
+ with mock_create_session() as session:
+ record = session.get(Asset, cached.id)
+ assert record.system_metadata == sibling_metadata
+ finally:
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def test_cached_earliest_sibling_wins(mock_create_session):
+ earliest = datetime(2020, 1, 1, 0, 0, 0)
+ later = datetime(2020, 1, 2, 0, 0, 0)
+ winner_metadata = {"winner": "earliest-low-id"}
+ path = _output_path("primitives_cached_earliest.png")
+ with open(path, "wb") as fh:
+ fh.write(b"pixels")
+
+ try:
+ with mock_create_session() as session:
+ content = create_content(session, os.path.abspath(path), None, 6, 0)
+ session.add_all(
+ [
+ Asset(
+ id="00000000-0000-0000-0000-000000000001",
+ content_id=content.id,
+ name="earliest-low-id",
+ created_at=earliest,
+ system_metadata=dict(winner_metadata),
+ ),
+ Asset(
+ id="00000000-0000-0000-0000-000000000002",
+ content_id=content.id,
+ name="earliest-high-id",
+ created_at=earliest,
+ system_metadata={"winner": "earliest-high-id"},
+ ),
+ Asset(
+ id="00000000-0000-0000-0000-000000000000",
+ content_id=content.id,
+ name="later-lowest-id",
+ created_at=later,
+ system_metadata={"winner": "later-lowest-id"},
+ ),
+ ]
+ )
+ session.commit()
+ content_id = content.id
+
+ cached = register_cached_output(path, job_id="delivery-job")
+
+ assert cached is not None
+ assert cached.content_id == content_id
+ with mock_create_session() as session:
+ record = session.get(Asset, cached.id)
+ assert record.system_metadata == winner_metadata
+ finally:
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def test_cached_orphan_content_extracts_fresh(mock_create_session):
+ body = b"pixels"
+ path = _output_path("primitives_cached_orphan.png")
+ with open(path, "wb") as fh:
+ fh.write(body)
+
+ try:
+ with mock_create_session() as session:
+ content = create_content(
+ session, os.path.abspath(path), None, len(body), 0
+ )
+ session.commit()
+ content_id = content.id
+
+ cached = register_cached_output(path, job_id="delivery-job")
+
+ assert cached is not None
+ assert cached.content_id == content_id
+ with mock_create_session() as session:
+ record = session.get(Asset, cached.id)
+ assert record.system_metadata is not None
+ assert record.system_metadata.get("format") == "png"
+ assert record.system_metadata.get("content_length") == len(body)
+ finally:
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def test_cached_missing_content_is_nonevent(mock_create_session):
+ path = _output_path("primitives_cached_missing.png")
+ with open(path, "wb") as fh:
+ fh.write(b"pixels")
+
+ try:
+ cached = register_cached_output(path, job_id="delivery-job")
+
+ assert cached is None
+ with mock_create_session() as session:
+ contents = list(
+ session.scalars(
+ select(AssetContent).where(
+ AssetContent.path == os.path.abspath(path)
+ )
+ )
+ )
+ records = list(
+ session.scalars(
+ select(Asset).where(Asset.job_id == "delivery-job")
+ )
+ )
+ assert contents == []
+ assert records == []
+ finally:
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def test_registration_failure_never_raises_and_leaves_no_rows(
+ mock_create_session, monkeypatch
+):
+ def _boom(*args, **kwargs):
+ raise RuntimeError("simulated create_record failure")
+
+ monkeypatch.setattr(
+ "app.assets.services.ingest.create_record", _boom
+ )
+
+ path = _output_path("primitives_reg_fail.png")
+ with open(path, "wb") as fh:
+ fh.write(b"pixels")
+
+ try:
+ result = register_executed_output(path, job_id="job-boom")
+
+ assert result is None
+ with mock_create_session() as session:
+ contents = list(
+ session.scalars(
+ select(AssetContent).where(
+ AssetContent.path == os.path.abspath(path)
+ )
+ )
+ )
+ records = list(
+ session.scalars(
+ select(Asset).where(Asset.job_id == "job-boom")
+ )
+ )
+ assert contents == []
+ assert records == []
+ finally:
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def test_upload_record_has_metadata_and_hash(mock_create_session):
+ body = b"pixels-content"
+ path = _output_path("primitives_upload_in_place.png")
+ with open(path, "wb") as fh:
+ fh.write(body)
+
+ try:
+ result = register_file_in_place(
+ path, name="pic.png", tags=["demo"], mime_type="image/png"
+ )
+
+ assert result.asset.hash is not None
+ assert result.ref.system_metadata is not None
+ assert result.ref.system_metadata.get("format") == "png"
+ assert result.ref.system_metadata.get("content_length") == len(body)
+ finally:
+ if os.path.exists(path):
+ os.unlink(path)
diff --git a/tests-unit/assets_test/services/test_scan_lifecycle.py b/tests-unit/assets_test/services/test_scan_lifecycle.py
new file mode 100644
index 000000000..e5a1d2801
--- /dev/null
+++ b/tests-unit/assets_test/services/test_scan_lifecycle.py
@@ -0,0 +1,43 @@
+from pathlib import Path
+from unittest.mock import patch
+
+from sqlalchemy import select
+
+from app.assets.database.models import AssetContent
+from app.assets.scanner import build_asset_specs, seed_asset_specs, sync_prefixes_with_filesystem
+
+
+def _scan(session, root: Path) -> int:
+ paths = [str(path) for path in root.iterdir()]
+ specs, _, _ = build_asset_specs(paths, set(), enable_metadata_extraction=False)
+ return seed_asset_specs(session, specs)
+
+
+def test_e2e_scan_seed_detect_prune(session, temp_dir: Path):
+ root = temp_dir / "input"
+ root.mkdir()
+ removed = root / "removed.bin"
+ edited = root / "edited.bin"
+ removed.write_bytes(b"removed")
+ edited.write_bytes(b"old")
+ with patch("folder_paths.get_input_directory", return_value=str(root)):
+ assert _scan(session, root) == 2
+ removed.unlink()
+ edited.write_bytes(b"replacement")
+ (root / "partial.part").write_bytes(b"partial")
+ with patch("app.assets.scanner.mode.hashing_enabled", return_value=False):
+ sync_prefixes_with_filesystem(session, [str(root)])
+ _scan(session, root)
+ session.commit()
+ contents = list(session.scalars(select(AssetContent)))
+ assert len(contents) == 3
+ assert len([content for content in contents if content.is_missing]) == 2
+
+
+def test_second_scan_idempotent(session, temp_dir: Path):
+ root = temp_dir / "input"
+ root.mkdir()
+ (root / "stable.bin").write_bytes(b"stable")
+ with patch("folder_paths.get_input_directory", return_value=str(root)):
+ assert _scan(session, root) == 1
+ assert _scan(session, root) == 0
diff --git a/tests-unit/assets_test/services/test_scanner_b.py b/tests-unit/assets_test/services/test_scanner_b.py
new file mode 100644
index 000000000..93c155870
--- /dev/null
+++ b/tests-unit/assets_test/services/test_scanner_b.py
@@ -0,0 +1,232 @@
+import os
+from collections.abc import Mapping
+from dataclasses import dataclass
+from pathlib import Path
+from unittest.mock import patch
+
+from sqlalchemy import select
+from sqlalchemy.orm import Session
+
+from app.assets.database.models import Asset, AssetContent, AssetTag
+from app.assets.helpers import to_stored_hash
+from app.assets.scanner import (
+ build_asset_specs,
+ enrich_asset,
+ mark_contents_missing_outside_prefixes,
+ seed_asset_specs,
+ sync_prefixes_with_filesystem,
+)
+from app.assets.services.snapshot_hash import snapshot_hash
+
+
+@dataclass(frozen=True, slots=True)
+class _ExtractedMetadata:
+ content_type: str | None
+ system_metadata: Mapping[str, int | str]
+
+ def to_user_metadata(self) -> dict[str, int | str]:
+ return dict(self.system_metadata)
+
+
+def _build_seed_specs(root: Path) -> list:
+ return build_asset_specs(
+ [str(path) for path in sorted(root.iterdir())],
+ existing_paths=set(),
+ enable_metadata_extraction=False,
+ )[0]
+
+
+def _create_enrichment_target(
+ session: Session,
+ path: Path,
+ system_metadata: dict[str, int | str] | None = None,
+) -> tuple[AssetContent, Asset]:
+ content = AssetContent(
+ path=str(path),
+ hash=None,
+ size_bytes=path.stat().st_size,
+ mtime_ns=path.stat().st_mtime_ns,
+ )
+ session.add(content)
+ session.flush()
+ record = Asset(
+ content_id=content.id,
+ name=path.name,
+ system_metadata=system_metadata,
+ )
+ session.add(record)
+ session.commit()
+ return content, record
+
+
+def test_enrichment_retains_absent_system_metadata_keys(session: Session, temp_dir: Path):
+ path = temp_dir / "metadata.bin"
+ path.write_bytes(b"metadata")
+ content, record = _create_enrichment_target(session, path)
+
+ with patch(
+ "app.assets.scanner.extract_file_metadata",
+ side_effect=[
+ _ExtractedMetadata(None, {"a": 1, "b": 2}),
+ _ExtractedMetadata(None, {"b": 3}),
+ ],
+ ):
+ enrich_asset(session, str(path), content.id, record.id)
+ enrich_asset(session, str(path), content.id, record.id)
+
+ assert record.system_metadata == {"a": 1, "b": 3}
+
+
+def test_enrichment_retains_dimensions_when_image_extraction_degrades(
+ session: Session, temp_dir: Path
+):
+ path = temp_dir / "image.png"
+ path.write_bytes(b"not a complete image")
+ content, record = _create_enrichment_target(
+ session,
+ path,
+ {"kind": "image", "width": 64, "height": 64},
+ )
+
+ with (
+ patch(
+ "app.assets.scanner.extract_file_metadata",
+ return_value=_ExtractedMetadata("image/png", {"filename": "image.png"}),
+ ),
+ patch("app.assets.scanner.extract_image_dimensions", return_value=None),
+ ):
+ enrich_asset(session, str(path), content.id, record.id)
+
+ assert record.system_metadata == {
+ "filename": "image.png",
+ "kind": "image",
+ "width": 64,
+ "height": 64,
+ }
+
+
+def test_enrichment_overrides_content_length_with_zero(
+ session: Session, temp_dir: Path
+):
+ path = temp_dir / "empty.bin"
+ path.write_bytes(b"")
+ content, record = _create_enrichment_target(
+ session,
+ path,
+ {"content_length": 1024},
+ )
+
+ with patch(
+ "app.assets.scanner.extract_file_metadata",
+ return_value=_ExtractedMetadata(None, {"content_length": 0}),
+ ):
+ enrich_asset(session, str(path), content.id, record.id)
+
+ assert record.system_metadata == {"content_length": 0}
+
+
+def test_seed_creates_content_and_record(session, temp_dir: Path):
+ input_root = temp_dir / "input"
+ input_root.mkdir()
+ (input_root / "first.png").write_bytes(b"first")
+ (input_root / "second.png").write_bytes(b"second")
+
+ with patch("folder_paths.get_input_directory", return_value=str(input_root)):
+ created = seed_asset_specs(session, _build_seed_specs(input_root))
+ session.commit()
+
+ contents = list(session.scalars(select(AssetContent).order_by(AssetContent.path)))
+ records = list(session.scalars(select(Asset).order_by(Asset.name)))
+
+ assert created == 2
+ assert len(contents) == 2
+ assert len(records) == 2
+ assert [record.loader_path for record in records] == ["first.png", "second.png"]
+ assert [link.tag_name for link in session.scalars(select(AssetTag).order_by(AssetTag.asset_id))] == [
+ "input",
+ "input",
+ ]
+
+
+def test_prune_marks_missing_not_deletes(session, temp_dir: Path):
+ input_root = temp_dir / "input"
+ input_root.mkdir()
+ file_path = input_root / "removed-from-registry.png"
+ file_path.write_bytes(b"content")
+
+ with patch("folder_paths.get_input_directory", return_value=str(input_root)):
+ seed_asset_specs(session, _build_seed_specs(input_root))
+ session.commit()
+
+ marked = mark_contents_missing_outside_prefixes(session, prefixes=[])
+ session.commit()
+
+ content = session.scalar(select(AssetContent))
+ record = session.scalar(select(Asset))
+ missing_tag = session.get(AssetTag, {"asset_id": record.id, "tag_name": "missing"})
+
+ assert marked == 1
+ assert content is not None and content.is_missing is True
+ assert record is not None
+ assert missing_tag is not None and missing_tag.origin == "automatic"
+
+
+def test_unhashed_missing_content_gets_tagged(session, temp_dir: Path):
+ missing_path = os.path.abspath(temp_dir / "missing.bin")
+ content = AssetContent(path=missing_path, hash=None, size_bytes=7, mtime_ns=1)
+ session.add(content)
+ session.flush()
+ record = Asset(content_id=content.id, name="missing.bin")
+ session.add(record)
+ session.commit()
+
+ sync_prefixes_with_filesystem(session, prefixes=[str(temp_dir)])
+ session.commit()
+
+ session.expire_all()
+ content = session.get(AssetContent, content.id)
+ missing_tag = session.get(AssetTag, {"asset_id": record.id, "tag_name": "missing"})
+
+ assert content is not None and content.is_missing is True
+ assert missing_tag is not None and missing_tag.origin == "automatic"
+
+
+def test_enrichment_keeps_equal_hash_contents_distinct(session, temp_dir: Path):
+ path = temp_dir / "new.bin"
+ path.write_bytes(b"same bytes")
+ snapshot = snapshot_hash(str(path))
+ assert snapshot is not None
+ digest, _ = snapshot
+
+ content = AssetContent(
+ path=str(path),
+ hash=None,
+ size_bytes=path.stat().st_size,
+ mtime_ns=path.stat().st_mtime_ns,
+ )
+ existing = AssetContent(
+ path=str(temp_dir / "existing.bin"),
+ hash=to_stored_hash(digest),
+ size_bytes=path.stat().st_size,
+ mtime_ns=path.stat().st_mtime_ns,
+ )
+ session.add_all((content, existing))
+ session.flush()
+ record = Asset(content_id=content.id, name=path.name)
+ existing_record = Asset(content_id=existing.id, name="existing.bin")
+ session.add_all((record, existing_record))
+ session.commit()
+
+ enriched = enrich_asset(
+ session,
+ file_path=str(path),
+ content_id=content.id,
+ record_id=record.id,
+ extract_metadata=False,
+ compute_hash=True,
+ )
+
+ assert enriched is True
+ assert session.get(AssetContent, content.id).hash == to_stored_hash(digest)
+ assert session.get(Asset, record.id).content_id == content.id
+ assert session.get(Asset, existing_record.id).content_id == existing.id
diff --git a/tests-unit/assets_test/services/test_scanner_seed_resilience.py b/tests-unit/assets_test/services/test_scanner_seed_resilience.py
new file mode 100644
index 000000000..f6f8f6ba4
--- /dev/null
+++ b/tests-unit/assets_test/services/test_scanner_seed_resilience.py
@@ -0,0 +1,214 @@
+import os
+from collections.abc import Callable
+from pathlib import Path
+from unittest.mock import patch
+
+import pytest
+from sqlalchemy import select
+from sqlalchemy.exc import IntegrityError
+from sqlalchemy.orm import Session
+
+from app.assets.database.models import Asset, AssetContent
+from app.assets.database.queries import create_content, create_record, delete_record
+from app.assets.scanner import SeedAssetSpec, seed_asset_specs
+from app.assets.services.snapshot_hash import snapshot_hash
+
+
+def _spec(path: Path) -> SeedAssetSpec:
+ stat_result = path.stat()
+ return {
+ "abs_path": str(path),
+ "size_bytes": stat_result.st_size,
+ "mtime_ns": stat_result.st_mtime_ns,
+ "info_name": path.name,
+ "tags": ["input"],
+ "fname": path.name,
+ "metadata": None,
+ "mime_type": None,
+ "job_id": None,
+ }
+
+
+def _specs_with_vanished_path(root: Path) -> tuple[list[SeedAssetSpec], Path]:
+ paths = [root / name for name in ("first.bin", "vanished.bin", "last.bin")]
+ for path in paths:
+ _ = path.write_bytes(path.name.encode())
+ return [_spec(path) for path in paths], paths[1]
+
+
+def _record_count(session: Session) -> int:
+ return len(session.scalars(select(Asset)).all())
+
+
+def test_seed_persists_remaining_specs_when_path_vanishes_before_restat(
+ session: Session, temp_dir: Path
+) -> None:
+ specs, vanished_path = _specs_with_vanished_path(temp_dir)
+ vanished_path.unlink()
+
+ created = seed_asset_specs(session, specs)
+ session.commit()
+
+ assert created == 2
+ assert _record_count(session) == 2
+
+
+def test_seed_persists_remaining_specs_when_path_vanishes_during_recovery_hash(
+ session: Session, temp_dir: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ specs, vanished_path = _specs_with_vanished_path(temp_dir)
+
+ def _hash_or_raise(path: str) -> str | None:
+ if path == str(vanished_path):
+ vanished_path.unlink()
+ raise OSError("file vanished during recovery")
+ return snapshot_hash(path)
+
+ monkeypatch.setattr("app.assets.scanner_changes.snapshot_hash", _hash_or_raise)
+
+ with patch("app.assets.scanner.mode.hashing_enabled", return_value=True):
+ created = seed_asset_specs(session, specs)
+ session.commit()
+
+ assert created == 2
+ assert _record_count(session) == 2
+
+
+def _delete_before_restat(_monkeypatch: pytest.MonkeyPatch, path: Path) -> None:
+ path.unlink()
+
+
+def _delete_during_recovery(monkeypatch: pytest.MonkeyPatch, path: Path) -> None:
+ def _hash_or_raise(candidate_path: str) -> str | None:
+ if candidate_path == str(path):
+ path.unlink()
+ raise OSError("file vanished during recovery")
+ return snapshot_hash(candidate_path)
+
+ monkeypatch.setattr("app.assets.scanner_changes.snapshot_hash", _hash_or_raise)
+
+
+ "delete_path",
+ [_delete_before_restat, _delete_during_recovery],
+ ids=["before-restat", "during-recovery"],
+)
+def test_seed_logs_once_for_each_vanished_path(
+ session: Session,
+ temp_dir: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ caplog: pytest.LogCaptureFixture,
+ delete_path: Callable[[pytest.MonkeyPatch, Path], None],
+) -> None:
+ specs, vanished_path = _specs_with_vanished_path(temp_dir)
+ delete_path(monkeypatch, vanished_path)
+
+ with patch("app.assets.scanner.mode.hashing_enabled", return_value=True):
+ _ = seed_asset_specs(session, specs)
+ session.commit()
+
+ messages = [
+ record.getMessage()
+ for record in caplog.records
+ if str(vanished_path) in record.getMessage()
+ ]
+ assert messages == [f"Skipping vanished asset during scan: {vanished_path}"]
+
+
+def test_seed_isolates_a_poisoned_spec_and_persists_the_specs_around_it(
+ session: Session, temp_dir: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ specs, poisoned_path = _specs_with_vanished_path(temp_dir)
+
+ def _create_record_or_raise(
+ session_arg: Session,
+ *,
+ content_id: str,
+ name: str,
+ mime_type: str | None,
+ job_id: str | None,
+ loader_path: str | None,
+ tags: list[str],
+ ) -> Asset:
+ if name == poisoned_path.name:
+ raise IntegrityError("forced record creation failure", {}, ValueError())
+ return create_record(
+ session_arg,
+ content_id,
+ name,
+ mime_type,
+ job_id,
+ loader_path,
+ tags,
+ )
+
+ monkeypatch.setattr("app.assets.scanner.create_record", _create_record_or_raise)
+
+ created = seed_asset_specs(session, specs)
+ session.commit()
+
+ assert created == 2
+ assert _record_count(session) == 2
+ assert {record.name for record in session.scalars(select(Asset))} == {
+ "first.bin",
+ "last.bin",
+ }, (
+ "the batch shares one transaction, so a bare rollback would erase the spec BEFORE "
+ f"the poisoned one; both neighbours of {poisoned_path.name} must survive"
+ )
+
+
+def test_seed_persists_fresh_stat_after_spec_was_built(
+ session: Session, temp_dir: Path
+) -> None:
+ path = temp_dir / "changed-after-walk.bin"
+ path.write_bytes(b"walk-time")
+ spec = _spec(path)
+
+ path.write_bytes(b"fresh-seed-time-content")
+ fresh_mtime_ns = spec["mtime_ns"] + 1_000_000
+ os.utime(path, ns=(fresh_mtime_ns, fresh_mtime_ns))
+ fresh_stat = path.stat()
+
+ created = seed_asset_specs(session, [spec])
+ session.commit()
+
+ persisted = session.scalar(
+ select(AssetContent).where(AssetContent.path == str(path))
+ )
+ assert created == 1
+ assert fresh_stat.st_size != spec["size_bytes"]
+ assert fresh_stat.st_mtime_ns != spec["mtime_ns"]
+ assert persisted is not None
+ assert persisted.size_bytes == fresh_stat.st_size
+ assert persisted.mtime_ns == fresh_stat.st_mtime_ns
+
+
+def test_seed_record_failure_preserves_retained_live_content(
+ session: Session, temp_dir: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ path = temp_dir / "retained.bin"
+ path.write_bytes(b"retained-live-content")
+ spec = _spec(path)
+ content = create_content(
+ session,
+ path=str(path),
+ size_bytes=spec["size_bytes"],
+ mtime_ns=spec["mtime_ns"],
+ )
+ record = create_record(session, content.id, path.name)
+ session.commit()
+ retained_content_id = content.id
+ delete_record(session, record.id)
+ session.commit()
+
+ def _raise_record_creation(*_args, **_kwargs):
+ raise RuntimeError("forced record creation failure")
+
+ monkeypatch.setattr("app.assets.scanner.create_record", _raise_record_creation)
+
+ with pytest.raises(RuntimeError, match="forced record creation failure"):
+ seed_asset_specs(session, [spec])
+ session.rollback()
+
+ assert session.get(AssetContent, retained_content_id) is not None
diff --git a/tests-unit/assets_test/services/test_serving_b.py b/tests-unit/assets_test/services/test_serving_b.py
new file mode 100644
index 000000000..d33181fd0
--- /dev/null
+++ b/tests-unit/assets_test/services/test_serving_b.py
@@ -0,0 +1,356 @@
+import os
+from datetime import datetime
+from unittest.mock import patch
+
+import pytest
+from aiohttp import web
+from aiohttp.test_utils import make_mocked_request
+from sqlalchemy import select, update
+
+from app.assets.api import routes
+from app.assets.api.routes import _build_asset_response
+from app.assets.database.models import Asset
+from app.assets.database.queries.records import (
+ RecordPageSpec,
+ create_content,
+ create_record,
+ get_record_by_id,
+ list_records_page,
+ mark_content_missing,
+)
+from app.assets.helpers import to_stored_hash
+from app.assets.services.asset_management import (
+ asset_exists,
+ delete_asset_reference,
+ get_asset_detail,
+ resolve_asset_for_download,
+ resolve_hash_to_path,
+)
+from app.assets.services.lookup import lookup_for_view
+from app.assets.services.schemas import AssetData, AssetDetailResult, ReferenceData
+
+_TS = datetime(2024, 1, 1, 0, 0, 0)
+
+
+def test_missing_content_record_metadata_200_with_tag_content_404(
+ mock_create_session, session, temp_dir
+):
+ content = create_content(session, path=str(temp_dir / "gone.bin"))
+ record = create_record(session, content_id=content.id, name="gone.bin")
+ mark_content_missing(session, content.id)
+ session.commit()
+ record_id = record.id
+
+ detail = get_asset_detail(record_id)
+ assert detail is not None
+ assert "missing" in detail.tags
+
+ with pytest.raises(FileNotFoundError):
+ resolve_asset_for_download(record_id)
+
+
+def test_missing_record_is_listed_but_never_served(
+ mock_create_session, session, temp_dir
+):
+ path = temp_dir / "listed_gone.bin"
+ path.write_bytes(b"x")
+ digest = to_stored_hash("a" * 64)
+ content = create_content(session, path=str(path), hash=digest)
+ record = create_record(session, content_id=content.id, name="listed_gone.bin")
+ mark_content_missing(session, content.id)
+ session.commit()
+ record_id = record.id
+
+ results, tag_map, total = list_records_page(session, RecordPageSpec())
+ assert any(r.id == record_id for r in results)
+ assert total == 1
+ assert "missing" in tag_map.get(record_id, [])
+
+ assert lookup_for_view(session, digest) is None
+ with pytest.raises(FileNotFoundError):
+ resolve_asset_for_download(record_id)
+
+
+def test_deleted_record_metadata_and_content_404(mock_create_session, session):
+ record_id = "00000000-0000-0000-0000-000000000000"
+ assert get_asset_detail(record_id) is None
+ with pytest.raises(ValueError, match="not found"):
+ resolve_asset_for_download(record_id)
+
+
+def test_content_404_when_file_absent_not_marked_missing(
+ mock_create_session, session, temp_dir
+):
+ path = temp_dir / "absent.bin"
+ path.write_bytes(b"x")
+ content = create_content(session, path=str(path))
+ record = create_record(session, content_id=content.id, name="absent.bin")
+ session.commit()
+ record_id = record.id
+ path.unlink()
+
+ detail = get_asset_detail(record_id)
+ assert detail is not None
+ assert "missing" not in detail.tags
+
+ with pytest.raises(FileNotFoundError):
+ resolve_asset_for_download(record_id)
+
+
+def test_content_fail_closed_no_sibling_fallback(mock_create_session, session, temp_dir):
+ digest = "d" * 64
+ missing_path = temp_dir / "missing.bin"
+ live_path = temp_dir / "live.bin"
+ live_path.write_bytes(b"live")
+
+ missing_content = create_content(session, path=str(missing_path), hash=digest)
+ live_content = create_content(session, path=str(live_path), hash=digest)
+ record = create_record(session, content_id=missing_content.id, name="missing.bin")
+ mark_content_missing(session, missing_content.id)
+ session.commit()
+ record_id = record.id
+
+ assert live_content.is_missing is False
+ assert os.path.isfile(live_path)
+
+ with pytest.raises(FileNotFoundError):
+ resolve_asset_for_download(record_id)
+
+
+def test_resolve_hash_to_path_refuses_temp_content(mock_create_session, session, temp_dir):
+ digest = "e" * 64
+ f = temp_dir / "temp_only.bin"
+ f.write_bytes(b"temp")
+ create_content(session, path=str(f), hash=to_stored_hash(digest))
+ session.commit()
+
+ with patch("app.assets.services.lookup.is_temp_path", return_value=True):
+ result = resolve_hash_to_path(f"blake3:{digest}")
+
+ assert result is None
+
+
+def test_resolve_hash_to_path_uses_newest_record_for_name_and_content_type(
+ mock_create_session, session, temp_dir
+):
+ digest = "c" * 64
+ f = temp_dir / "shared.bin"
+ f.write_bytes(b"data")
+ content = create_content(session, path=str(f), hash=to_stored_hash(digest), size_bytes=f.stat().st_size)
+ older = create_record(
+ session,
+ content_id=content.id,
+ name="older.txt",
+ mime_type="text/plain",
+ )
+ newer = create_record(
+ session,
+ content_id=content.id,
+ name="newer.png",
+ mime_type="image/png",
+ )
+ older.created_at = _TS
+ older.updated_at = _TS
+ older.last_access_time = _TS
+ newer.created_at = _TS.replace(microsecond=1)
+ newer.updated_at = _TS.replace(microsecond=1)
+ newer.last_access_time = _TS.replace(microsecond=1)
+ session.commit()
+
+ result = resolve_hash_to_path(f"blake3:{digest}")
+
+ assert result is not None
+ assert result.download_name == "newer.png"
+ assert result.content_type == "image/png"
+
+
+def test_resolve_hash_to_path_serves_content_with_zero_records(
+ mock_create_session, session, temp_dir
+):
+ digest = "d" * 64
+ f = temp_dir / "orphan.png"
+ f.write_bytes(b"data")
+ content = create_content(session, path=str(f), hash=to_stored_hash(digest), size_bytes=f.stat().st_size)
+ record = create_record(
+ session,
+ content_id=content.id,
+ name="renamed.txt",
+ mime_type="text/plain",
+ )
+ session.commit()
+
+ assert delete_asset_reference(record.id) is True
+ session.expire_all()
+ assert lookup_for_view(session, f"blake3:{digest}") is not None
+ assert session.scalars(
+ select(Asset).where(Asset.content_id == content.id)
+ ).all() == []
+
+ result = resolve_hash_to_path(f"blake3:{digest}")
+
+ assert result is not None
+ assert result.download_name == "orphan.png"
+ assert result.content_type == "image/png"
+
+
+def test_resolve_hash_to_path_unknown_hash(mock_create_session):
+ assert resolve_hash_to_path("blake3:" + "f" * 64) is None
+
+
+def test_content_read_updates_last_access_time(
+ mock_create_session, session, temp_dir
+):
+ f = temp_dir / "read.bin"
+ f.write_bytes(b"data")
+ content = create_content(session, path=str(f))
+ record = create_record(session, content_id=content.id, name="read.bin")
+ session.commit()
+ record_id = record.id
+
+ session.execute(
+ update(Asset).where(Asset.id == record_id).values(last_access_time=None)
+ )
+ session.commit()
+
+ before_record = get_record_by_id(session, record_id)
+ assert before_record is not None
+ before = before_record.last_access_time
+ assert before is None
+
+ resolve_asset_for_download(record_id)
+
+ session.expire_all()
+ after_record = get_record_by_id(session, record_id)
+ assert after_record is not None
+ after = after_record.last_access_time
+ assert after is not None
+
+
+def test_view_hash_read_updates_last_access_time(
+ mock_create_session, session, temp_dir
+):
+ digest = "a" * 64
+ f = temp_dir / "view.bin"
+ f.write_bytes(b"view")
+ content = create_content(session, path=str(f), hash=to_stored_hash(digest), size_bytes=f.stat().st_size)
+ record = create_record(session, content_id=content.id, name="view.bin")
+ session.commit()
+ record_id = record.id
+
+ session.execute(
+ update(Asset).where(Asset.id == record_id).values(last_access_time=None)
+ )
+ session.commit()
+
+ result = resolve_hash_to_path(f"blake3:{digest}")
+ assert result is not None
+
+ session.expire_all()
+ record_after = get_record_by_id(session, record_id)
+ assert record_after is not None
+ assert record_after.last_access_time is not None
+
+
+def test_lookup_for_view_returns_none_for_temp_content(session, temp_dir):
+ digest = "b" * 64
+ f = temp_dir / "view_temp.bin"
+ f.write_bytes(b"temp")
+ create_content(session, path=str(f), hash=to_stored_hash(digest))
+ session.commit()
+
+ with patch("app.assets.services.lookup.is_temp_path", return_value=True):
+ assert lookup_for_view(session, to_stored_hash(digest)) is None
+
+
+def test_asset_exists_false_for_temp_content(mock_create_session, session, temp_dir):
+ digest = "c" * 64
+ f = temp_dir / "exists_temp.bin"
+ f.write_bytes(b"temp")
+ create_content(session, path=str(f), hash=to_stored_hash(digest))
+ session.commit()
+
+ with patch("app.assets.services.lookup.is_temp_path", return_value=True):
+ assert asset_exists(f"blake3:{digest}") is False
+
+
+async def test_head_hash_route_404_for_temp_content(
+ mock_create_session, session, temp_dir, monkeypatch
+):
+ digest = "d" * 64
+ f = temp_dir / "head_temp.bin"
+ f.write_bytes(b"temp")
+ create_content(session, path=str(f), hash=to_stored_hash(digest))
+ session.commit()
+
+ monkeypatch.setattr(routes, "_ASSETS_ENABLED", True)
+ with patch("app.assets.services.lookup.is_temp_path", return_value=True):
+ response = await routes.head_asset_by_hash(
+ make_mocked_request(
+ "HEAD",
+ f"/api/assets/hash/blake3:{digest}",
+ match_info={"hash": f"blake3:{digest}"},
+ )
+ )
+
+ assert isinstance(response, web.Response)
+ assert response.status == 404
+
+
+def test_resolve_hash_to_path_temp_does_not_bump_last_access_time(
+ mock_create_session, session, temp_dir
+):
+ digest = "f" * 64
+ f = temp_dir / "noaccess_temp.bin"
+ f.write_bytes(b"temp")
+ content = create_content(session, path=str(f), hash=to_stored_hash(digest))
+ record = create_record(session, content_id=content.id, name="noaccess_temp.bin")
+ session.commit()
+ record_id = record.id
+
+ session.execute(
+ update(Asset).where(Asset.id == record_id).values(last_access_time=None)
+ )
+ session.commit()
+
+ with patch("app.assets.services.lookup.is_temp_path", return_value=True):
+ result = resolve_hash_to_path(f"blake3:{digest}")
+
+ assert result is None
+ session.expire_all()
+ record_after = get_record_by_id(session, record_id)
+ assert record_after is not None
+ assert record_after.last_access_time is None
+
+
+def sandboxed_comfy_roots(tmp_path):
+ with patch("app.assets.services.path_utils.folder_paths") as fp:
+ fp.get_input_directory.return_value = str(tmp_path / "input")
+ fp.get_output_directory.return_value = str(tmp_path / "output")
+ fp.get_temp_directory.return_value = str(tmp_path / "temp")
+ fp.models_dir = str(tmp_path / "models")
+ yield tmp_path
+
+
+def test_temp_asset_preview_url_still_resolves_type_temp(sandboxed_comfy_roots):
+ name = "ComfyUI_temp_abcde_00001_.png"
+ result = AssetDetailResult(
+ ref=ReferenceData(
+ id="ref-temp",
+ name=name,
+ file_path=str(sandboxed_comfy_roots / "temp" / name),
+ loader_path=None,
+ user_metadata=None,
+ preview_id=None,
+ created_at=_TS,
+ updated_at=_TS,
+ last_access_time=_TS,
+ ),
+ asset=AssetData(hash="blake3:abc", size_bytes=1024, mime_type="image/png"),
+ tags=[],
+ )
+
+ resp = _build_asset_response(result, {})
+
+ assert resp.preview_url == f"/api/view?type=temp&filename={name}"
diff --git a/tests-unit/assets_test/services/test_snapshot_hash.py b/tests-unit/assets_test/services/test_snapshot_hash.py
new file mode 100644
index 000000000..b9a8e203b
--- /dev/null
+++ b/tests-unit/assets_test/services/test_snapshot_hash.py
@@ -0,0 +1,90 @@
+import builtins
+import os
+from collections.abc import Callable
+from pathlib import Path
+
+import pytest
+from blake3 import blake3
+
+from app.assets.services.snapshot_hash import snapshot_hash
+
+
+class _MutatingReader:
+ def __init__(self, file, mutate: Callable[[], None], mutate_after_close: bool) -> None:
+ self._file = file
+ self._mutate = mutate
+ self._mutate_after_close = mutate_after_close
+ self._did_mutate = False
+
+ def __enter__(self):
+ self._file.__enter__()
+ return self
+
+ def __exit__(self, *args):
+ result = self._file.__exit__(*args)
+ if self._mutate_after_close and not self._did_mutate:
+ self._did_mutate = True
+ self._mutate()
+ return result
+
+ def fileno(self) -> int:
+ return self._file.fileno()
+
+ def read(self, size: int = -1) -> bytes:
+ result = self._file.read(size)
+ if not self._mutate_after_close and not self._did_mutate:
+ self._did_mutate = True
+ self._mutate()
+ return result
+
+
+def test_snapshot_hash_returns_digest_for_quiescent_file(tmp_path: Path) -> None:
+ payload = b"quiescent" * 1024
+ path = tmp_path / "asset.bin"
+ path.write_bytes(payload)
+
+ result = snapshot_hash(str(path), chunk_size=64)
+
+ assert result is not None
+ digest, stat_result = result
+ assert digest == blake3(payload).hexdigest()
+ assert isinstance(stat_result, os.stat_result)
+ assert stat_result.st_size == len(payload)
+
+
[email protected]("mutation", ["bytes", "replace", "unlink", "truncate", "append"])
+def test_snapshot_hash_returns_none_when_file_drifts(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, mutation: str
+) -> None:
+ path = tmp_path / "asset.bin"
+ path.write_bytes(b"original" * 1024)
+ original_open = builtins.open
+
+ def mutate() -> None:
+ match mutation:
+ case "bytes":
+ path.write_bytes(b"changed" * 1024)
+ case "replace":
+ replacement = tmp_path / "replacement.bin"
+ replacement.write_bytes(b"replacement")
+ os.replace(replacement, path)
+ case "unlink":
+ path.unlink()
+ case "truncate":
+ path.write_bytes(b"")
+ case "append":
+ with original_open(path, "ab") as output:
+ output.write(b"more")
+ case unreachable:
+ raise AssertionError(f"unexpected mutation {unreachable}")
+
+ def open_with_mutation(*args, **kwargs):
+ return _MutatingReader(
+ original_open(*args, **kwargs),
+ mutate,
+ mutation in {"replace", "unlink"},
+ )
+
+ monkeypatch.setattr(builtins, "open", open_with_mutation)
+
+ assert snapshot_hash(str(path), chunk_size=64) is None
diff --git a/tests-unit/assets_test/services/test_split_policy.py b/tests-unit/assets_test/services/test_split_policy.py
new file mode 100644
index 000000000..0e612e797
--- /dev/null
+++ b/tests-unit/assets_test/services/test_split_policy.py
@@ -0,0 +1,393 @@
+from __future__ import annotations
+
+import os
+from collections.abc import Iterator
+from contextlib import contextmanager
+from dataclasses import dataclass
+from pathlib import Path
+from unittest.mock import patch
+
+import sqlalchemy as sa
+from sqlalchemy import select
+from sqlalchemy.orm import Session
+
+import pytest
+
+from app.assets.database.models import Asset, AssetContent
+from app.assets.database.queries import create_content, create_record
+from app.assets.database.queries.records import (
+ RecordPageSpec,
+ fetch_record_tags,
+ list_records_page,
+)
+from app.assets.helpers import to_stored_hash
+from app.assets.scanner import get_unenriched_assets_for_roots
+from app.assets.scanner_changes import (
+ clear_pending_verifications,
+ detect_content_change,
+ drain_pending_verifications,
+)
+from app.assets.services.hash_mode_state import (
+ clear_transition_queue,
+ drain_transition_queue,
+ enqueue_transition_work,
+)
+from app.assets.services.lookup import lookup_for_view
+from app.assets.services.snapshot_hash import snapshot_hash
+
+
+@dataclass(frozen=True, slots=True)
+class _FakeStat:
+
+ st_size: int
+ st_mtime_ns: int
+
+
+@contextmanager
+def _reuse_session(session: Session) -> Iterator[Session]:
+ yield session
+
+
+def _raw_system_metadata(session: Session, record_id: str) -> object:
+ return session.execute(
+ sa.text("SELECT system_metadata FROM assets WHERE id = :id"),
+ {"id": record_id},
+ ).scalar()
+
+
+def _candidates_under(session: Session, temp_dir: Path, *, compute_hashes: bool) -> set[str]:
+ with (
+ patch("app.assets.scanner.create_session", lambda: _reuse_session(session)),
+ patch(
+ "app.assets.scanner.get_scan_prefixes_for_root",
+ return_value=[str(temp_dir)],
+ ),
+ ):
+ rows = get_unenriched_assets_for_roots(("models",), compute_hashes=compute_hashes)
+ return {row.record_id for row in rows}
+
+
[email protected](autouse=True)
+def _transition_queue_isolation() -> Iterator[None]:
+ clear_transition_queue()
+ yield
+ clear_transition_queue()
+
+
[email protected](autouse=True)
+def _input_base_is_temp_dir(temp_dir: Path, monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr("folder_paths.get_input_directory", lambda: str(temp_dir))
+
+
[email protected](autouse=True)
+def _pending_verification_isolation() -> Iterator[None]:
+ clear_pending_verifications()
+ yield
+ clear_pending_verifications()
+
+
+def _seed_hashed_content(session: Session, path: Path, data: bytes) -> AssetContent:
+ path.write_bytes(data)
+ snapshot = snapshot_hash(str(path))
+ assert snapshot is not None
+ digest, verified_stat = snapshot
+ return create_content(
+ session,
+ str(path),
+ to_stored_hash(digest),
+ verified_stat.st_size,
+ verified_stat.st_mtime_ns,
+ )
+
+
+def _bump_mtime(path: Path) -> os.stat_result:
+ before = path.stat()
+ bumped = before.st_mtime_ns + 5_000_000_000
+ os.utime(path, ns=(bumped, bumped))
+ after = path.stat()
+ assert after.st_mtime_ns != before.st_mtime_ns
+ assert after.st_size == before.st_size
+ return after
+
+
+def _rewrite_same_size(path: Path, data: bytes) -> os.stat_result:
+ before = path.stat()
+ assert len(data) == before.st_size
+ assert data != path.read_bytes()
+ path.write_bytes(data)
+ bumped = before.st_mtime_ns + 5_000_000_000
+ os.utime(path, ns=(bumped, bumped))
+ after = path.stat()
+ assert after.st_size == before.st_size
+ assert after.st_mtime_ns != before.st_mtime_ns
+ return after
+
+
+def test_split_record_is_enrich_candidate_in_hash_mode(
+ session: Session, temp_dir: Path
+) -> None:
+ path = temp_dir / "split.safetensors"
+ content = create_content(session, str(path), hash="blake3:deadbeef")
+ record = create_record(session, content.id, path.name)
+ session.commit()
+ record_id = record.id
+
+ candidates = _candidates_under(session, temp_dir, compute_hashes=True)
+
+ assert record_id in candidates
+
+
+def test_enriched_record_is_not_enrich_candidate_in_hash_mode(
+ session: Session, temp_dir: Path
+) -> None:
+ path = temp_dir / "enriched.safetensors"
+ content = create_content(session, str(path), hash="blake3:deadbeef")
+ record = create_record(
+ session, content.id, path.name, system_metadata={"architecture": "flux"}
+ )
+ session.commit()
+ record_id = record.id
+
+ candidates = _candidates_under(session, temp_dir, compute_hashes=True)
+
+ assert record_id not in candidates
+
+
+def test_same_size_mtime_bump_does_not_split(session: Session, temp_dir: Path) -> None:
+ path = temp_dir / "touched.safetensors"
+ content = create_content(session, str(path), hash=None, size_bytes=100, mtime_ns=1000)
+ record = create_record(
+ session, content.id, path.name, tags=["keepme"], system_metadata={"k": "v"}
+ )
+ session.commit()
+ content_id, record_id = content.id, record.id
+
+ detect_content_change(
+ session, content, _FakeStat(st_size=100, st_mtime_ns=2000), hashing_is_enabled=False
+ )
+ session.commit()
+ session.expire_all()
+
+ live = session.get(AssetContent, content_id)
+ assert live is not None and live.is_missing is False
+ rows_at_path = list(
+ session.scalars(select(AssetContent).where(AssetContent.path == str(path)))
+ )
+ assert len(rows_at_path) == 1
+ surviving = session.get(Asset, record_id)
+ assert surviving.system_metadata == {"k": "v"}
+ tags = fetch_record_tags(session, record_id)
+ assert "keepme" in tags
+ assert "missing" not in tags
+
+ assert live.mtime_ns == 2000
+ assert live.size_bytes == 100
+
+
+def test_accepted_mtime_bump_drops_the_unverifiable_hash(
+ session: Session, temp_dir: Path
+) -> None:
+ path = temp_dir / "synced.safetensors"
+ content = _seed_hashed_content(session, path, b"rsynced bytes")
+ record = create_record(
+ session, content.id, path.name, tags=["keepme"], system_metadata={"k": "v"}
+ )
+ session.commit()
+ content_id, record_id, stored_hash = content.id, record.id, content.hash
+ assert lookup_for_view(session, stored_hash) is not None
+
+ observed = _bump_mtime(path)
+ detect_content_change(session, content, observed, hashing_is_enabled=False)
+ session.commit()
+ session.expire_all()
+
+ live = session.get(AssetContent, content_id)
+ assert live.hash is None
+ assert lookup_for_view(session, stored_hash) is None
+
+ assert live.is_missing is False
+ assert live.mtime_ns == observed.st_mtime_ns
+ assert live.size_bytes == observed.st_size
+
+ surviving = session.get(Asset, record_id)
+ assert surviving is not None and surviving.content_id == content_id
+ assert surviving.system_metadata == {"k": "v"}
+ assert "keepme" in fetch_record_tags(session, record_id)
+ listed, _, _ = list_records_page(session, RecordPageSpec(limit=100))
+ assert record_id in {row.id for row in listed}
+
+
+def test_same_size_content_change_is_never_served_under_the_old_hash(
+ session: Session, temp_dir: Path
+) -> None:
+ path = temp_dir / "overwritten.safetensors"
+ content = _seed_hashed_content(session, path, b"AAAA")
+ create_record(session, content.id, path.name, tags=["keepme"])
+ session.commit()
+ old_hash = content.hash
+ assert lookup_for_view(session, old_hash) is not None
+
+ observed = _rewrite_same_size(path, b"BBBB")
+ detect_content_change(session, content, observed, hashing_is_enabled=False)
+ session.commit()
+ session.expire_all()
+
+ assert path.read_bytes() == b"BBBB"
+ assert lookup_for_view(session, old_hash) is None
+
+
+def test_accepted_mtime_bump_is_not_re_detected_by_the_next_scan(
+ session: Session, temp_dir: Path
+) -> None:
+ path = temp_dir / "resynced.safetensors"
+ content = _seed_hashed_content(session, path, b"cloud synced bytes")
+ create_record(session, content.id, path.name, tags=["keepme"])
+ session.commit()
+ content_id = content.id
+ detect_content_change(session, content, _bump_mtime(path), hashing_is_enabled=False)
+ session.commit()
+
+ detect_content_change(session, content, path.stat(), hashing_is_enabled=True)
+
+ assert drain_pending_verifications(session) == 0
+
+ detect_content_change(session, content, path.stat(), hashing_is_enabled=False)
+ session.commit()
+ session.expire_all()
+ rows_at_path = list(
+ session.scalars(select(AssetContent).where(AssetContent.path == str(path)))
+ )
+ assert len(rows_at_path) == 1
+ assert rows_at_path[0].id == content_id
+ assert rows_at_path[0].is_missing is False
+
+
+def test_dropped_hash_is_refilled_in_place_by_a_later_hash_mode_pass(
+ session: Session, temp_dir: Path
+) -> None:
+ path = temp_dir / "refilled.safetensors"
+ content = _seed_hashed_content(session, path, b"cloud synced bytes")
+ record = create_record(
+ session, content.id, path.name, tags=["keepme"], system_metadata={"k": "v"}
+ )
+ session.commit()
+ content_id, record_id = content.id, record.id
+ assert record_id not in _candidates_under(session, temp_dir, compute_hashes=True)
+
+ detect_content_change(session, content, _bump_mtime(path), hashing_is_enabled=False)
+ session.commit()
+ session.expire_all()
+ assert session.get(AssetContent, content_id).hash is None
+ assert record_id in _candidates_under(session, temp_dir, compute_hashes=True)
+
+ enqueue_transition_work(session, "off_to_on")
+ drain_transition_queue(session)
+ session.commit()
+ session.expire_all()
+
+ snapshot = snapshot_hash(str(path))
+ assert snapshot is not None
+ live = session.get(AssetContent, content_id)
+ assert live.is_missing is False
+ assert live.hash == to_stored_hash(snapshot[0])
+ assert lookup_for_view(session, live.hash).id == content_id
+ rows_at_path = list(
+ session.scalars(select(AssetContent).where(AssetContent.path == str(path)))
+ )
+ assert len(rows_at_path) == 1
+ assert "keepme" in fetch_record_tags(session, record_id)
+ assert session.get(Asset, record_id).system_metadata == {"k": "v"}
+ assert record_id not in _candidates_under(session, temp_dir, compute_hashes=True)
+
+
+def test_mtime_and_size_change_splits_with_null_metadata(
+ session: Session, temp_dir: Path
+) -> None:
+ path = temp_dir / "grown.safetensors"
+ content = create_content(session, str(path), hash=None, size_bytes=100, mtime_ns=1000)
+ create_record(
+ session, content.id, path.name, tags=["oldtag"], system_metadata={"k": "v"}
+ )
+ session.commit()
+ old_content_id = content.id
+
+ detect_content_change(
+ session, content, _FakeStat(st_size=200, st_mtime_ns=2000), hashing_is_enabled=False
+ )
+ session.commit()
+ session.expire_all()
+
+ assert session.get(AssetContent, old_content_id).is_missing is True
+ live = session.scalar(
+ select(AssetContent).where(
+ AssetContent.path == str(path), AssetContent.is_missing.is_(False)
+ )
+ )
+ assert live is not None and live.id != old_content_id
+ assert live.size_bytes == 200 and live.mtime_ns == 2000
+ new_record = session.scalar(select(Asset).where(Asset.content_id == live.id))
+ assert new_record is not None
+
+ assert new_record.system_metadata is None
+ assert _raw_system_metadata(session, new_record.id) is None
+ assert "oldtag" not in fetch_record_tags(session, new_record.id)
+ assert new_record.id in _candidates_under(session, temp_dir, compute_hashes=True)
+
+
+def test_mtime_unchanged_size_changed_does_not_split(
+ session: Session, temp_dir: Path
+) -> None:
+ path = temp_dir / "weird.safetensors"
+ content = create_content(session, str(path), hash=None, size_bytes=100, mtime_ns=1000)
+ create_record(session, content.id, path.name, tags=["keepme"])
+ session.commit()
+ content_id = content.id
+
+ detect_content_change(
+ session, content, _FakeStat(st_size=999, st_mtime_ns=1000), hashing_is_enabled=False
+ )
+ session.commit()
+ session.expire_all()
+
+ assert session.get(AssetContent, content_id).is_missing is False
+ rows_at_path = list(
+ session.scalars(select(AssetContent).where(AssetContent.path == str(path)))
+ )
+ assert len(rows_at_path) == 1
+
+
+def test_transition_drain_split_replacement_has_null_metadata(
+ session: Session, temp_dir: Path
+) -> None:
+ path = temp_dir / "changed.bin"
+ path.write_bytes(b"old bytes")
+ old_snapshot = snapshot_hash(str(path))
+ assert old_snapshot is not None
+ old_digest, _ = old_snapshot
+ stat = path.stat()
+ old_content = create_content(
+ session, str(path), to_stored_hash(old_digest), stat.st_size, stat.st_mtime_ns
+ )
+ old_content_id = old_content.id
+ create_record(
+ session, old_content_id, "changed.bin", tags=["oldtag"], system_metadata={"k": "v"}
+ )
+ path.write_bytes(b"different new bytes")
+
+ enqueue_transition_work(session, "off_to_on")
+ drain_transition_queue(session)
+ session.commit()
+ session.expire_all()
+
+ assert session.get(AssetContent, old_content_id).is_missing is True
+ live = session.scalar(
+ select(AssetContent).where(
+ AssetContent.path == str(path), AssetContent.is_missing.is_(False)
+ )
+ )
+ assert live is not None and live.id != old_content_id
+ new_record = session.scalar(select(Asset).where(Asset.content_id == live.id))
+ assert new_record is not None
+ assert new_record.system_metadata is None
+ assert _raw_system_metadata(session, new_record.id) is None
+ assert "oldtag" not in fetch_record_tags(session, new_record.id)
diff --git a/tests-unit/assets_test/services/test_stored_hash_prefix.py b/tests-unit/assets_test/services/test_stored_hash_prefix.py
new file mode 100644
index 000000000..c28d6fb34
--- /dev/null
+++ b/tests-unit/assets_test/services/test_stored_hash_prefix.py
@@ -0,0 +1,282 @@
+from __future__ import annotations
+
+import json
+import os
+import uuid
+from contextlib import contextmanager
+from unittest.mock import AsyncMock
+
+import pytest
+from aiohttp import web
+from aiohttp.test_utils import make_mocked_request
+from sqlalchemy import func, select
+from sqlalchemy.orm import Session as SASession
+
+from app.assets import mode
+from app.assets.api import routes
+import app.assets.mode as mode_module
+import folder_paths
+from app.assets.database.models import Asset, AssetContent
+from app.assets.database.queries.records import create_record
+from app.assets.scanner import enrich_asset
+from app.assets.scanner_changes import recover_missing_content
+from app.assets.services import asset_management, ingest
+from app.assets.services.asset_management import get_asset_detail
+from app.assets.services.ingest import (
+ HashMismatchError,
+ upload_from_temp_path,
+)
+from app.assets.services.snapshot_hash import snapshot_hash
+
+
+def hashing_on():
+ class FakeArgs:
+ enable_asset_hashing = True
+
+ class DisabledArgs:
+ enable_asset_hashing = False
+
+ mode_module.init(FakeArgs())
+ yield
+ mode_module.init(DisabledArgs())
+
+
+def _write_temp(content: bytes) -> str:
+ uploads_root = os.path.join(
+ folder_paths.get_temp_directory(), "uploads", uuid.uuid4().hex
+ )
+ os.makedirs(uploads_root, exist_ok=True)
+ path = os.path.join(uploads_root, ".upload.part")
+ with open(path, "wb") as file:
+ file.write(content)
+ return path
+
+
+def test_upload_stores_prefixed_hash_expected_hash_succeeds_and_reuses_content(
+ mock_create_session, hashing_on
+):
+ content_bytes = b"prefixed-stored-upload-bytes"
+ temp1 = _write_temp(content_bytes)
+ snapshot = snapshot_hash(temp1)
+ assert snapshot is not None
+ digest, _ = snapshot
+ expected = f"blake3:{digest}"
+ assert expected.startswith("blake3:")
+
+ temp2 = _write_temp(content_bytes)
+ try:
+ r1 = upload_from_temp_path(
+ temp_path=temp1,
+ name="pref.bin",
+ tags=["output"],
+ client_filename="pref.bin",
+ expected_hash=expected,
+ )
+ assert r1.created_new is True
+
+ with mock_create_session() as session:
+ live = list(
+ session.scalars(
+ select(AssetContent).where(AssetContent.is_missing.is_(False))
+ )
+ )
+ assert len(live) == 1
+ assert live[0].hash == expected
+ assert live[0].hash.startswith("blake3:")
+
+ assert r1.asset.hash == expected
+
+ r2 = upload_from_temp_path(
+ temp_path=temp2,
+ name="pref.bin",
+ tags=["output"],
+ client_filename="pref.bin",
+ expected_hash=expected,
+ )
+ assert r2.created_new is True
+ assert r2.ref.id != r1.ref.id, "every upload is its own delivery record"
+ with mock_create_session() as session:
+ assert session.scalar(select(func.count()).select_from(AssetContent)) == 1
+ assert session.scalar(select(func.count()).select_from(Asset)) == 2
+ finally:
+ for path in (temp1, temp2):
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def test_upload_expected_hash_mismatch_still_rejected(mock_create_session, hashing_on):
+ temp = _write_temp(b"mismatch-bytes")
+ try:
+ with pytest.raises(HashMismatchError):
+ upload_from_temp_path(
+ temp_path=temp,
+ name="mismatch.bin",
+ tags=["output"],
+ client_filename="mismatch.bin",
+ expected_hash=f"blake3:{'0' * 64}",
+ )
+ with mock_create_session() as session:
+ assert session.scalar(select(func.count()).select_from(Asset)) == 0
+ assert session.scalar(select(func.count()).select_from(AssetContent)) == 0
+ finally:
+ if os.path.exists(temp):
+ os.unlink(temp)
+
+
+def test_enrich_fills_deferred_hash_prefixed(session, temp_dir):
+ path = temp_dir / "deferred.bin"
+ path.write_bytes(b"deferred output bytes")
+ stat = path.stat()
+
+ content = AssetContent(
+ path=str(path),
+ hash=None,
+ size_bytes=stat.st_size,
+ mtime_ns=stat.st_mtime_ns,
+ )
+ session.add(content)
+ session.flush()
+ record = create_record(session, content.id, path.name)
+ session.commit()
+
+ enriched = enrich_asset(
+ session,
+ file_path=str(path),
+ content_id=content.id,
+ record_id=record.id,
+ extract_metadata=False,
+ compute_hash=True,
+ )
+
+ assert enriched is True
+ stored = session.get(AssetContent, content.id).hash
+ assert stored is not None
+ assert stored.startswith("blake3:")
+ snapshot = snapshot_hash(str(path))
+ assert snapshot is not None
+ digest, _ = snapshot
+ assert stored == f"blake3:{digest}"
+
+
+def test_recovery_matches_prefixed_stored_hash(session, temp_dir):
+ path = temp_dir / "recover.bin"
+ original_bytes = b"recover-me-bytes"
+ path.write_bytes(original_bytes)
+ snapshot = snapshot_hash(str(path))
+ assert snapshot is not None
+ digest, _ = snapshot
+ stored = f"blake3:{digest}"
+ assert stored.startswith("blake3:")
+
+ content = AssetContent(
+ path=str(path),
+ hash=stored,
+ is_missing=True,
+ size_bytes=0,
+ mtime_ns=None,
+ )
+ session.add(content)
+ session.flush()
+ create_record(session, content.id, path.name)
+ session.commit()
+
+ path.unlink()
+ path.write_bytes(original_bytes)
+ stat = os.stat(str(path))
+ result = recover_missing_content(
+ session, str(path), stat, hashing_is_enabled=True
+ )
+
+ assert result == "recovered"
+ recovered = session.get(AssetContent, content.id)
+ assert recovered is not None
+ assert recovered.is_missing is False
+ assert recovered.hash == stored
+
+
+async def test_all_read_surfaces_agree_on_prefixed_hash(
+ db_engine, monkeypatch, hashing_on
+):
+ @contextmanager
+ def _factory():
+ with SASession(db_engine) as sess:
+ yield sess
+
+ monkeypatch.setattr(routes, "create_session", lambda: SASession(db_engine))
+ monkeypatch.setattr(routes, "_ASSETS_ENABLED", True)
+ monkeypatch.setattr(mode, "hashing_enabled", lambda: True)
+ monkeypatch.setattr(ingest, "create_session", _factory)
+ monkeypatch.setattr(asset_management, "create_session", _factory)
+
+ content_bytes = b"one-asset-all-surfaces-agree"
+ temp = _write_temp(content_bytes)
+ snapshot = snapshot_hash(temp)
+ assert snapshot is not None
+ digest, _ = snapshot
+ expected = f"blake3:{digest}"
+
+ try:
+ upload_result = ingest.upload_from_temp_path(
+ temp_path=temp,
+ name="surf.bin",
+ tags=["output"],
+ client_filename="surf.bin",
+ )
+ asset_id = upload_result.ref.id
+
+ assert upload_result.asset is not None
+ assert upload_result.asset.hash == expected
+ upload_resp = routes._build_asset_response(upload_result, {})
+ assert upload_resp.hash == expected
+
+ detail = get_asset_detail(asset_id)
+ assert detail is not None
+ assert detail.asset is not None
+ assert detail.asset.hash == expected
+
+ get_resp = await routes.get_asset_route(
+ make_mocked_request(
+ "GET", f"/api/assets/{asset_id}", match_info={"id": asset_id}
+ )
+ )
+ assert isinstance(get_resp, web.Response)
+ assert isinstance(get_resp.body, bytes | bytearray)
+ get_body = json.loads(get_resp.body)
+ assert get_body["hash"] == expected
+
+ list_resp = await routes.list_assets_route(
+ make_mocked_request("GET", "/api/assets")
+ )
+ assert isinstance(list_resp, web.Response)
+ assert isinstance(list_resp.body, bytes | bytearray)
+ list_body = json.loads(list_resp.body)
+ item = next(a for a in list_body["assets"] if a["id"] == asset_id)
+ assert item["hash"] == expected
+
+ head_resp = await routes.head_asset_by_hash(
+ make_mocked_request(
+ "HEAD",
+ f"/api/assets/hash/{expected}",
+ match_info={"hash": expected},
+ )
+ )
+ assert isinstance(head_resp, web.Response)
+ assert head_resp.status == 200
+
+ fh_req = AsyncMock(spec=web.Request)
+ fh_req.json.return_value = {
+ "hash": expected,
+ "name": "fromhash.bin",
+ "tags": ["output"],
+ }
+ fh_resp = await routes.create_asset_from_hash_route(fh_req)
+ assert isinstance(fh_resp, web.Response)
+ assert fh_resp.status == 201
+ assert isinstance(fh_resp.body, bytes | bytearray)
+ fh_body = json.loads(fh_resp.body)
+ assert fh_body["hash"] == expected
+ finally:
+ if os.path.exists(temp):
+ os.unlink(temp)
diff --git a/tests-unit/assets_test/services/test_tag_histogram.py b/tests-unit/assets_test/services/test_tag_histogram.py
index 7bcd518ec..6a2c18400 100644
--- a/tests-unit/assets_test/services/test_tag_histogram.py
+++ b/tests-unit/assets_test/services/test_tag_histogram.py
@@ -1,123 +1,17 @@
-"""Tests for list_tag_histogram service function."""
from sqlalchemy.orm import Session
-from app.assets.database.models import Asset, AssetReference
-from app.assets.database.queries import ensure_tags_exist, add_tags_to_reference
-from app.assets.helpers import get_utc_now
-from app.assets.services.tagging import list_tag_histogram
+from app.assets.database.queries import create_content, create_record, list_records_page
+from app.assets.database.queries.records import RecordPageSpec
-def _make_asset(session: Session, hash_val: str = "blake3:test") -> Asset:
- asset = Asset(hash=hash_val, size_bytes=1024)
- session.add(asset)
- session.flush()
- return asset
+def test_tag_selection_counts_records_not_content_rows(session: Session) -> None:
+ content = create_content(session, "/models/shared.safetensors")
+ create_record(session, content.id, "one", tags=["model"])
+ create_record(session, content.id, "two", tags=["model"])
-
-def _make_reference(
- session: Session,
- asset: Asset,
- name: str = "test",
- owner_id: str = "",
-) -> AssetReference:
- now = get_utc_now()
- ref = AssetReference(
- owner_id=owner_id,
- name=name,
- asset_id=asset.id,
- created_at=now,
- updated_at=now,
- last_access_time=now,
+ records, _, _ = list_records_page(
+ session,
+ RecordPageSpec(all_tags=("model",)),
)
- session.add(ref)
- session.flush()
- return ref
-
-
-class TestListTagHistogram:
- def test_returns_counts_for_all_tags(self, mock_create_session, session: Session):
- ensure_tags_exist(session, ["alpha", "beta"])
- a1 = _make_asset(session, "blake3:aaa")
- r1 = _make_reference(session, a1, name="r1")
- add_tags_to_reference(session, reference_id=r1.id, tags=["alpha", "beta"])
-
- a2 = _make_asset(session, "blake3:bbb")
- r2 = _make_reference(session, a2, name="r2")
- add_tags_to_reference(session, reference_id=r2.id, tags=["alpha"])
- session.commit()
-
- result = list_tag_histogram()
-
- assert result["alpha"] == 2
- assert result["beta"] == 1
-
- def test_empty_when_no_assets(self, mock_create_session, session: Session):
- ensure_tags_exist(session, ["unused"])
- session.commit()
-
- result = list_tag_histogram()
-
- assert result == {}
-
- def test_include_tags_filter(self, mock_create_session, session: Session):
- ensure_tags_exist(session, ["models", "loras", "input"])
- a1 = _make_asset(session, "blake3:aaa")
- r1 = _make_reference(session, a1, name="r1")
- add_tags_to_reference(session, reference_id=r1.id, tags=["models", "loras"])
-
- a2 = _make_asset(session, "blake3:bbb")
- r2 = _make_reference(session, a2, name="r2")
- add_tags_to_reference(session, reference_id=r2.id, tags=["input"])
- session.commit()
-
- result = list_tag_histogram(include_tags=["models"])
-
- # Only r1 has "models", so only its tags appear
- assert "models" in result
- assert "loras" in result
- assert "input" not in result
-
- def test_exclude_tags_filter(self, mock_create_session, session: Session):
- ensure_tags_exist(session, ["models", "loras", "input"])
- a1 = _make_asset(session, "blake3:aaa")
- r1 = _make_reference(session, a1, name="r1")
- add_tags_to_reference(session, reference_id=r1.id, tags=["models", "loras"])
-
- a2 = _make_asset(session, "blake3:bbb")
- r2 = _make_reference(session, a2, name="r2")
- add_tags_to_reference(session, reference_id=r2.id, tags=["input"])
- session.commit()
-
- result = list_tag_histogram(exclude_tags=["models"])
-
- # r1 excluded, only r2's tags remain
- assert "input" in result
- assert "loras" not in result
-
- def test_name_contains_filter(self, mock_create_session, session: Session):
- ensure_tags_exist(session, ["alpha", "beta"])
- a1 = _make_asset(session, "blake3:aaa")
- r1 = _make_reference(session, a1, name="my_model.safetensors")
- add_tags_to_reference(session, reference_id=r1.id, tags=["alpha"])
-
- a2 = _make_asset(session, "blake3:bbb")
- r2 = _make_reference(session, a2, name="picture.png")
- add_tags_to_reference(session, reference_id=r2.id, tags=["beta"])
- session.commit()
-
- result = list_tag_histogram(name_contains="model")
-
- assert "alpha" in result
- assert "beta" not in result
-
- def test_limit_caps_results(self, mock_create_session, session: Session):
- tags = [f"tag{i}" for i in range(10)]
- ensure_tags_exist(session, tags)
- a = _make_asset(session, "blake3:aaa")
- r = _make_reference(session, a, name="r1")
- add_tags_to_reference(session, reference_id=r.id, tags=tags)
- session.commit()
-
- result = list_tag_histogram(limit=3)
- assert len(result) == 3
+ assert len(records) == 2
diff --git a/tests-unit/assets_test/services/test_tag_protection.py b/tests-unit/assets_test/services/test_tag_protection.py
new file mode 100644
index 000000000..7f3b1cec6
--- /dev/null
+++ b/tests-unit/assets_test/services/test_tag_protection.py
@@ -0,0 +1,107 @@
+import json
+from types import SimpleNamespace
+
+import pytest
+
+from app.assets.api import routes
+
+_RECORD_ID = "00000000-0000-0000-0000-000000000001"
+_SYSTEM_TAG_ENVELOPE = {
+ "error": {
+ "code": "SYSTEM_TAG_FORBIDDEN",
+ "message": "Tag 'missing' is system-managed and cannot be modified via the API",
+ "details": {"tag": "missing"},
+ }
+}
+
+
+class _JsonRequest:
+ def __init__(self, tags: list[str]) -> None:
+ self.match_info = {"id": _RECORD_ID}
+ self._payload = {"tags": tags}
+
+ async def json(self) -> dict[str, list[str]]:
+ return self._payload
+
+
+class _UserManager:
+ def get_request_user_id(self, _request: _JsonRequest) -> str:
+ return "test-user"
+
+
+async def test_add_missing_tag_returns_400(monkeypatch):
+ monkeypatch.setattr(routes, "USER_MANAGER", _UserManager())
+ monkeypatch.setattr(
+ routes,
+ "apply_tags",
+ lambda **_kwargs: SimpleNamespace(added=[], already_present=[], total_tags=[]),
+ )
+
+ response = await routes.add_asset_tags.__wrapped__(_JsonRequest(["missing"]))
+
+ assert response.status == 400
+ assert json.loads(response.body) == _SYSTEM_TAG_ENVELOPE
+
+
+async def test_remove_missing_tag_returns_400(monkeypatch):
+ monkeypatch.setattr(routes, "USER_MANAGER", _UserManager())
+ monkeypatch.setattr(
+ routes,
+ "remove_tags",
+ lambda **_kwargs: SimpleNamespace(removed=[], not_present=[], total_tags=[]),
+ )
+
+ response = await routes.delete_asset_tags.__wrapped__(_JsonRequest(["missing"]))
+
+ assert response.status == 400
+ assert json.loads(response.body) == _SYSTEM_TAG_ENVELOPE
+
+
+async def test_other_tags_unaffected(monkeypatch):
+ monkeypatch.setattr(routes, "USER_MANAGER", _UserManager())
+ calls: list[tuple[str, list[str]]] = []
+
+ def apply_tags(**kwargs):
+ calls.append(("add", kwargs["tags"]))
+ return SimpleNamespace(
+ added=kwargs["tags"], already_present=[], total_tags=kwargs["tags"]
+ )
+
+ def remove_tags(**kwargs):
+ calls.append(("remove", kwargs["tags"]))
+ return SimpleNamespace(
+ removed=kwargs["tags"], not_present=[], total_tags=[], protected=[]
+ )
+
+ monkeypatch.setattr(routes, "apply_tags", apply_tags)
+ monkeypatch.setattr(routes, "remove_tags", remove_tags)
+
+ add_response = await routes.add_asset_tags.__wrapped__(_JsonRequest(["manual"]))
+ remove_response = await routes.delete_asset_tags.__wrapped__(
+ _JsonRequest(["manual"])
+ )
+
+ assert add_response.status == 200
+ assert remove_response.status == 200
+ assert calls == [("add", ["manual"]), ("remove", ["manual"])]
+
+
+async def test_remove_tags_response_exposes_protected_bucket(monkeypatch):
+ monkeypatch.setattr(routes, "USER_MANAGER", _UserManager())
+ monkeypatch.setattr(
+ routes,
+ "remove_tags",
+ lambda **_kwargs: SimpleNamespace(
+ removed=[], not_present=[], total_tags=["auto"], protected=["auto"]
+ ),
+ )
+
+ response = await routes.delete_asset_tags.__wrapped__(_JsonRequest(["auto"]))
+
+ assert response.status == 200
+ body = json.loads(response.body)
+ assert body["protected"] == ["auto"]
diff --git a/tests-unit/assets_test/services/test_tag_write_conflicts.py b/tests-unit/assets_test/services/test_tag_write_conflicts.py
new file mode 100644
index 000000000..32bf41fd1
--- /dev/null
+++ b/tests-unit/assets_test/services/test_tag_write_conflicts.py
@@ -0,0 +1,166 @@
+from contextlib import contextmanager
+from datetime import datetime
+from unittest.mock import patch
+
+import pytest
+from sqlalchemy import create_engine, select, update
+from sqlalchemy.exc import IntegrityError
+from sqlalchemy.orm import Session as SASession
+
+import app.assets.database.queries.records as records_module
+import app.assets.services.asset_management as asset_management_module
+import app.assets.services.tagging as tagging_module
+from app.assets.database.models import Asset, AssetTag, Base, Tag
+from app.assets.database.queries.records import (
+ create_content,
+ create_record,
+ mark_content_missing,
+)
+from app.assets.services.asset_management import update_asset_metadata
+from app.assets.services.tagging import apply_tags
+
+STALE = datetime(2020, 1, 1, 0, 0, 0)
+RACED = "raced"
+UNRELATED = "unrelated"
+
+
+def _file_backed_engine(db_path):
+ engine = create_engine(
+ f"sqlite:///{db_path}",
+ connect_args={"check_same_thread": False, "timeout": 0.2},
+ )
+ Base.metadata.create_all(engine)
+ return engine
+
+
+def _seed_record(session, path: str, tags=None) -> str:
+ content = create_content(session, path)
+ record = create_record(session, content.id, "fixture", tags=tags)
+ session.commit()
+ session.execute(update(Asset).where(Asset.id == record.id).values(updated_at=STALE))
+ session.commit()
+ return record.id
+
+
+def _updated_at(session, record_id: str) -> datetime:
+ session.expire_all()
+ return session.scalar(select(Asset.updated_at).where(Asset.id == record_id))
+
+
+def _tag_names(session, record_id: str) -> list[str]:
+ return sorted(
+ session.scalars(select(AssetTag.tag_name).where(AssetTag.asset_id == record_id)).all()
+ )
+
+
+def test_apply_tags_loses_a_tag_race_without_raising_and_reports_it_honestly(tmp_path):
+ engine = _file_backed_engine(tmp_path / "tag_race.db")
+ with SASession(engine) as seed_session:
+ record_id = _seed_record(seed_session, "/tmp/tag-race-fixture", tags=[UNRELATED])
+
+ def connection_a_commits_the_raced_tag() -> None:
+ with SASession(engine) as connection_a:
+ connection_a.add(Tag(name=RACED))
+ connection_a.flush()
+ connection_a.add(
+ AssetTag(asset_id=record_id, tag_name=RACED, origin="manual")
+ )
+ connection_a.commit()
+
+ fired: list[bool] = []
+
+ @contextmanager
+ def racing_session_factory():
+ with SASession(engine) as session_b:
+ real_add = session_b.add
+
+ def add_racing_the_winner(instance, *args, **kwargs):
+ if isinstance(instance, Tag) and instance.name == RACED and not fired:
+ fired.append(True)
+ connection_a_commits_the_raced_tag()
+ return real_add(instance, *args, **kwargs)
+
+ session_b.add = add_racing_the_winner
+ yield session_b
+
+ with patch("app.assets.services.tagging.create_session", racing_session_factory):
+ result = apply_tags(record_id, [RACED])
+
+ assert fired, "the interleave never fired; the test proves nothing"
+ assert result.added == [], (
+ "session B lost the race; it inserted nothing and must not claim it did"
+ )
+ assert result.already_present == [RACED], (
+ "already_present reports the REQUESTED tags that were already there — never "
+ "unrelated pre-existing tags the caller did not ask about"
+ )
+ assert sorted(result.total_tags) == [RACED, UNRELATED]
+
+ with SASession(engine) as check:
+ assert _tag_names(check, record_id) == [RACED, UNRELATED]
+ assert _updated_at(check, record_id) == STALE, (
+ "a race loser changed no link, so it is not an edit and must not move updated_at"
+ )
+
+
+def test_ensure_tag_link_reraises_when_the_parent_asset_is_missing(db_engine_fk):
+ with SASession(db_engine_fk) as session:
+ session.add(Tag(name="orphan-link"))
+ session.flush()
+
+ with pytest.raises(IntegrityError):
+ records_module.ensure_tag_link(
+ session,
+ asset_id="no-such-asset",
+ tag_name="orphan-link",
+ origin="manual",
+ )
+
+
+def _drive_create_record(session, record_id):
+ content = create_content(session, "/tmp/four-sites-create-record")
+ create_record(session, content.id, "created", tags=["spied"])
+
+
+def _drive_mark_content_missing(session, record_id):
+ content_id = session.scalar(select(Asset.content_id).where(Asset.id == record_id))
+ mark_content_missing(session, content_id)
+
+
+def _drive_update_asset_metadata(session, record_id):
+ update_asset_metadata(record_id, tags=["spied"])
+
+
+def _drive_apply_tags(session, record_id):
+ apply_tags(record_id, ["spied"])
+
+
+ ("target_module", "drive"),
+ [
+ (records_module, _drive_create_record),
+ (records_module, _drive_mark_content_missing),
+ (asset_management_module, _drive_update_asset_metadata),
+ (tagging_module, _drive_apply_tags),
+ ],
+ ids=["create_record", "mark_content_missing", "update_asset_metadata", "apply_tags"],
+)
+def test_every_tag_write_site_routes_through_the_conflict_safe_helper(
+ session, mock_create_session, monkeypatch, target_module, drive
+):
+ record_id = _seed_record(session, "/tmp/four-sites-fixture")
+ calls: list[str] = []
+ real_ensure_tag_link = records_module.ensure_tag_link
+
+ def spying_ensure_tag_link(*args, **kwargs):
+ calls.append(kwargs["tag_name"])
+ return real_ensure_tag_link(*args, **kwargs)
+
+ monkeypatch.setattr(target_module, "ensure_tag_link", spying_ensure_tag_link)
+
+ drive(session, record_id)
+
+ assert calls, (
+ "this site still writes asset_tags with a bare check-then-insert; a concurrent "
+ "writer inserting the same link makes it raise instead of settling"
+ )
diff --git a/tests-unit/assets_test/services/test_tagging.py b/tests-unit/assets_test/services/test_tagging.py
index fa121db3e..84db76a3c 100644
--- a/tests-unit/assets_test/services/test_tagging.py
+++ b/tests-unit/assets_test/services/test_tagging.py
@@ -1,197 +1,52 @@
-"""Tests for tagging services."""
-import pytest
from sqlalchemy.orm import Session
-from app.assets.database.models import Asset, AssetReference
-from app.assets.database.queries import ensure_tags_exist, add_tags_to_reference
-from app.assets.helpers import get_utc_now
-from app.assets.services import apply_tags, remove_tags, list_tags
+from app.assets.database.models import AssetTag, Tag
+from app.assets.database.queries import create_content, create_record, fetch_record_tags
+from app.assets.services.tagging import remove_tags
-def _make_asset(session: Session, hash_val: str = "blake3:test") -> Asset:
- asset = Asset(hash=hash_val, size_bytes=1024)
- session.add(asset)
- session.flush()
- return asset
+def test_tags_are_birth_facts_of_a_record(session: Session) -> None:
+ content = create_content(session, "/models/model.safetensors")
+ record = create_record(session, content.id, "model", tags=["model", "checkpoint"])
+ assert fetch_record_tags(session, record.id) == ["checkpoint", "model"]
-def _make_reference(
- session: Session,
- asset: Asset,
- name: str = "test",
- owner_id: str = "",
-) -> AssetReference:
- now = get_utc_now()
- ref = AssetReference(
- owner_id=owner_id,
- name=name,
- asset_id=asset.id,
- created_at=now,
- updated_at=now,
- last_access_time=now,
- )
- session.add(ref)
- session.flush()
- return ref
-
-
-class TestApplyTags:
- def test_adds_new_tags(self, mock_create_session, session: Session):
- asset = _make_asset(session)
- ref = _make_reference(session, asset)
- session.commit()
-
- result = apply_tags(
- reference_id=ref.id,
- tags=["alpha", "beta"],
- )
-
- assert set(result.added) == {"alpha", "beta"}
- assert result.already_present == []
- assert set(result.total_tags) == {"alpha", "beta"}
-
- def test_reports_already_present(self, mock_create_session, session: Session):
- asset = _make_asset(session)
- ref = _make_reference(session, asset)
- ensure_tags_exist(session, ["existing"])
- add_tags_to_reference(session, reference_id=ref.id, tags=["existing"])
- session.commit()
-
- result = apply_tags(
- reference_id=ref.id,
- tags=["existing", "new"],
- )
-
- assert result.added == ["new"]
- assert result.already_present == ["existing"]
-
- def test_raises_for_nonexistent_ref(self, mock_create_session):
- with pytest.raises(ValueError, match="not found"):
- apply_tags(reference_id="nonexistent", tags=["x"])
-
- def test_raises_for_wrong_owner(self, mock_create_session, session: Session):
- asset = _make_asset(session)
- ref = _make_reference(session, asset, owner_id="user1")
- session.commit()
-
- with pytest.raises(PermissionError, match="not owner"):
- apply_tags(
- reference_id=ref.id,
- tags=["new"],
- owner_id="user2",
- )
-
-
-class TestRemoveTags:
- def test_removes_tags(self, mock_create_session, session: Session):
- asset = _make_asset(session)
- ref = _make_reference(session, asset)
- ensure_tags_exist(session, ["a", "b", "c"])
- add_tags_to_reference(session, reference_id=ref.id, tags=["a", "b", "c"])
- session.commit()
-
- result = remove_tags(
- reference_id=ref.id,
- tags=["a", "b"],
- )
-
- assert set(result.removed) == {"a", "b"}
- assert result.not_present == []
- assert result.total_tags == ["c"]
-
- def test_reports_not_present(self, mock_create_session, session: Session):
- asset = _make_asset(session)
- ref = _make_reference(session, asset)
- ensure_tags_exist(session, ["present"])
- add_tags_to_reference(session, reference_id=ref.id, tags=["present"])
- session.commit()
- result = remove_tags(
- reference_id=ref.id,
- tags=["present", "absent"],
- )
-
- assert result.removed == ["present"]
- assert result.not_present == ["absent"]
-
- def test_raises_for_nonexistent_ref(self, mock_create_session):
- with pytest.raises(ValueError, match="not found"):
- remove_tags(reference_id="nonexistent", tags=["x"])
-
- def test_raises_for_wrong_owner(self, mock_create_session, session: Session):
- asset = _make_asset(session)
- ref = _make_reference(session, asset, owner_id="user1")
- session.commit()
-
- with pytest.raises(PermissionError, match="not owner"):
- remove_tags(
- reference_id=ref.id,
- tags=["x"],
- owner_id="user2",
- )
-
-
-class TestListTags:
- def test_returns_tags_with_counts(self, mock_create_session, session: Session):
- ensure_tags_exist(session, ["used", "unused"])
- asset = _make_asset(session)
- ref = _make_reference(session, asset)
- add_tags_to_reference(session, reference_id=ref.id, tags=["used"])
- session.commit()
-
- rows, total = list_tags()
-
- tag_dict = {name: count for name, count in rows}
- assert tag_dict["used"] == 1
- assert tag_dict["unused"] == 0
- assert total == 2
-
- def test_excludes_zero_counts(self, mock_create_session, session: Session):
- ensure_tags_exist(session, ["used", "unused"])
- asset = _make_asset(session)
- ref = _make_reference(session, asset)
- add_tags_to_reference(session, reference_id=ref.id, tags=["used"])
- session.commit()
-
- rows, total = list_tags(include_zero=False)
-
- tag_names = {name for name, _ in rows}
- assert "used" in tag_names
- assert "unused" not in tag_names
-
- def test_prefix_filter(self, mock_create_session, session: Session):
- ensure_tags_exist(session, ["alpha", "beta", "alphabet"])
- session.commit()
-
- rows, _ = list_tags(prefix="alph")
-
- tag_names = {name for name, _ in rows}
- assert tag_names == {"alpha", "alphabet"}
+def _attach_automatic_tag(session: Session, record_id: str, tag_name: str) -> None:
+ if session.get(Tag, tag_name) is None:
+ session.add(Tag(name=tag_name))
+ session.flush()
+ session.add(
+ AssetTag(asset_id=record_id, tag_name=tag_name, origin="automatic")
+ )
- def test_order_by_name(self, mock_create_session, session: Session):
- ensure_tags_exist(session, ["zebra", "alpha", "middle"])
- session.commit()
- rows, _ = list_tags(order="name_asc")
+def test_removing_present_automatic_tag_reports_it_protected_not_absent(
+ session: Session, mock_create_session
+) -> None:
+ content = create_content(session, "/output/protected.png")
+ record = create_record(session, content.id, "protected-fixture")
+ _attach_automatic_tag(session, record.id, "auto")
+ session.commit()
- names = [name for name, _ in rows]
- assert names == ["alpha", "middle", "zebra"]
+ result = remove_tags(record.id, ["auto"])
- def test_pagination(self, mock_create_session, session: Session):
- ensure_tags_exist(session, ["a", "b", "c", "d", "e"])
- session.commit()
+ assert result.protected == ["auto"]
+ assert result.not_present == []
+ assert result.removed == []
- rows, total = list_tags(limit=2, offset=1, order="name_asc")
- assert total == 5
- assert len(rows) == 2
- names = [name for name, _ in rows]
- assert names == ["b", "c"]
+def test_remove_tags_separates_removed_protected_and_absent(
+ session: Session, mock_create_session
+) -> None:
+ content = create_content(session, "/output/mixed.png")
+ record = create_record(session, content.id, "mixed-fixture", tags=["keep-me"])
+ _attach_automatic_tag(session, record.id, "auto")
+ session.commit()
- def test_clamps_limit(self, mock_create_session, session: Session):
- ensure_tags_exist(session, ["a"])
- session.commit()
+ result = remove_tags(record.id, ["keep-me", "auto", "ghost"])
- # Service should clamp limit to max 1000
- rows, _ = list_tags(limit=2000)
- assert len(rows) <= 1000
+ assert result.removed == ["keep-me"]
+ assert result.protected == ["auto"]
+ assert result.not_present == ["ghost"]
+ assert fetch_record_tags(session, record.id) == ["auto"]
diff --git a/tests-unit/assets_test/services/test_transition_drain.py b/tests-unit/assets_test/services/test_transition_drain.py
new file mode 100644
index 000000000..3c1213499
--- /dev/null
+++ b/tests-unit/assets_test/services/test_transition_drain.py
@@ -0,0 +1,421 @@
+import logging
+from pathlib import Path
+
+import folder_paths
+import pytest
+from blake3 import blake3
+from sqlalchemy import select
+
+from app.assets.database.models import Asset, AssetContent, AssetTag
+from app.assets.database.queries.records import create_content, create_record
+from app.assets.helpers import to_stored_hash
+from app.assets.services import hash_mode_state
+from app.assets.services.hash_mode_state import (
+ clear_transition_queue,
+ drain_transition_queue,
+ enqueue_transition_work,
+ read_stored_mode,
+ record_transition_intent,
+ write_stored_mode,
+)
+from app.assets.services.lookup import lookup_for_view
+from app.assets.services.path_utils import get_name_and_tags_from_asset_path
+from app.assets.services.snapshot_hash import snapshot_hash
+
+
[email protected](autouse=True)
+def transition_queue():
+ clear_transition_queue()
+ yield
+ clear_transition_queue()
+
+
+def _stored_hash(path: Path) -> str:
+ snapshot = snapshot_hash(str(path))
+ assert snapshot is not None
+ digest, _ = snapshot
+ return to_stored_hash(digest)
+
+
+def test_off_to_on_transition_hashes_null_rows_and_persists_mode(session, temp_dir, monkeypatch):
+ paths = [temp_dir / "first.bin", temp_dir / "second.bin"]
+ for index, path in enumerate(paths):
+ path.write_bytes(f"bytes-{index}".encode())
+ stat = path.stat()
+ create_content(session, str(path), size_bytes=stat.st_size, mtime_ns=stat.st_mtime_ns)
+ write_stored_mode(session, "off")
+ monkeypatch.setattr(hash_mode_state._mode, "hashing_enabled", lambda: True)
+
+ transition = record_transition_intent(session)
+ enqueue_transition_work(session, transition)
+ drain_transition_queue(session)
+ session.commit()
+
+ contents = list(session.scalars(select(AssetContent)))
+ assert {content.hash for content in contents} == {_stored_hash(path) for path in paths}
+ assert read_stored_mode(session) == "on"
+
+
+def test_transition_drain_splits_changed_content(session, temp_dir, monkeypatch):
+ path = temp_dir / "changed.bin"
+ monkeypatch.setattr("folder_paths.get_input_directory", lambda: str(temp_dir))
+ path.write_bytes(b"old bytes")
+ old_snapshot = snapshot_hash(str(path))
+ assert old_snapshot is not None
+ old_digest, _ = old_snapshot
+ stat = path.stat()
+ old_content = create_content(
+ session, str(path), to_stored_hash(old_digest), stat.st_size, stat.st_mtime_ns
+ )
+ old_content_id = old_content.id
+ create_record(session, old_content_id, "changed.bin")
+ path.write_bytes(b"new bytes")
+
+ enqueue_transition_work(session, "off_to_on")
+ drain_transition_queue(session)
+ session.commit()
+
+ contents = list(session.scalars(select(AssetContent)))
+ live_content = next(content for content in contents if not content.is_missing)
+ records = list(session.scalars(select(Asset)))
+ assert session.get(AssetContent, old_content_id).is_missing is True
+ assert live_content.hash == _stored_hash(path)
+ assert len(records) == 2
+ assert any(record.content_id == live_content.id for record in records)
+
+
+def test_transition_drain_serves_unchanged_content_whose_stored_stat_went_stale(
+ session, temp_dir, monkeypatch
+):
+ path = temp_dir / "unchanged.bin"
+ path.write_bytes(b"bytes that outlive the mode flip")
+ stat = path.stat()
+ stored_hash = _stored_hash(path)
+ size_before_the_restore = stat.st_size - 1
+ mtime_before_the_restore = stat.st_mtime_ns - 1_000_000_000
+ content = create_content(
+ session, str(path), stored_hash, size_before_the_restore, mtime_before_the_restore
+ )
+ content_id = content.id
+ create_record(session, content_id, "unchanged.bin")
+ monkeypatch.setattr(folder_paths, "get_temp_directory", lambda: str(temp_dir / "never"))
+ write_stored_mode(session, "off")
+ monkeypatch.setattr(hash_mode_state._mode, "hashing_enabled", lambda: True)
+ assert lookup_for_view(session, stored_hash) is None, (
+ "precondition: the stale stat makes the row unservable by hash"
+ )
+
+ transition = record_transition_intent(session)
+ enqueue_transition_work(session, transition)
+ drain_transition_queue(session)
+ session.commit()
+
+ refreshed = session.get(AssetContent, content_id)
+ assert (refreshed.size_bytes, refreshed.mtime_ns) == (stat.st_size, stat.st_mtime_ns)
+ served = lookup_for_view(session, stored_hash)
+ assert served is not None and served.id == content_id, (
+ "the drain verified these bytes but left the row unservable until the next full scan"
+ )
+ assert [row.id for row in session.scalars(select(AssetContent))] == [content_id]
+ assert [row.content_id for row in session.scalars(select(Asset))] == [content_id]
+ assert read_stored_mode(session) == "on"
+
+
+def test_transition_drain_requeues_permission_errors_and_processes_other_paths(
+ session, temp_dir, monkeypatch
+):
+ protected_path = temp_dir / "protected.bin"
+ healthy_path = temp_dir / "healthy.bin"
+ protected_path.write_bytes(b"protected")
+ healthy_payload = b"healthy"
+ healthy_path.write_bytes(healthy_payload)
+ for path in (protected_path, healthy_path):
+ stat = path.stat()
+ create_content(session, str(path), size_bytes=stat.st_size, mtime_ns=stat.st_mtime_ns)
+ write_stored_mode(session, "off")
+ monkeypatch.setattr(hash_mode_state._mode, "hashing_enabled", lambda: True)
+
+ def hash_or_raise(candidate_path: str):
+ if candidate_path == str(protected_path):
+ raise PermissionError("denied")
+ return snapshot_hash(candidate_path)
+
+ monkeypatch.setattr(hash_mode_state, "snapshot_hash", hash_or_raise)
+ transition = record_transition_intent(session)
+ enqueue_transition_work(session, transition)
+
+ drain_transition_queue(session)
+
+ healthy_content = session.scalar(
+ select(AssetContent).where(AssetContent.path == str(healthy_path))
+ )
+ assert healthy_content is not None
+ assert healthy_content.hash == to_stored_hash(blake3(healthy_payload).hexdigest())
+ assert hash_mode_state.pending_transition_count() == 1
+ assert read_stored_mode(session) == "off"
+
+
+def test_transition_drain_marks_deleted_path_missing_and_completes_transition(
+ session, temp_dir, monkeypatch
+):
+ path = temp_dir / "vanished.bin"
+ monkeypatch.setattr("folder_paths.get_input_directory", lambda: str(temp_dir))
+ path.write_bytes(b"bytes that die during the outage")
+ stat = path.stat()
+ content = create_content(session, str(path), size_bytes=stat.st_size, mtime_ns=stat.st_mtime_ns)
+ content_id = content.id
+ record = create_record(session, content_id, "vanished.bin")
+ record_id = record.id
+ write_stored_mode(session, "off")
+ monkeypatch.setattr(hash_mode_state._mode, "hashing_enabled", lambda: True)
+ path.unlink()
+
+ transition = record_transition_intent(session)
+ enqueue_transition_work(session, transition)
+ drain_transition_queue(session)
+ session.commit()
+
+ assert session.get(AssetContent, content_id).is_missing is True, (
+ "a path deleted while every server was down must be marked missing, not requeued forever"
+ )
+ assert session.get(AssetTag, {"asset_id": record_id, "tag_name": "missing"}) is not None
+ assert hash_mode_state.pending_transition_count() == 0, (
+ "the queue never empties while a deleted path is requeued unconditionally"
+ )
+ assert read_stored_mode(session) == "on", (
+ "the completion gate stays wedged at 'off' for the process lifetime when the queue "
+ "cannot drain, so hash-addressed serving never gets its re-verification certificate"
+ )
+
+
+def test_transition_drain_requeues_transient_stat_error_without_marking_it_missing(
+ session, temp_dir, monkeypatch
+):
+ path = temp_dir / "transient-stat-error.bin"
+ path.write_bytes(b"present the whole time")
+ stat = path.stat()
+ content = create_content(session, str(path), size_bytes=stat.st_size, mtime_ns=stat.st_mtime_ns)
+ content_id = content.id
+ write_stored_mode(session, "off")
+ monkeypatch.setattr(hash_mode_state._mode, "hashing_enabled", lambda: True)
+ monkeypatch.setattr(hash_mode_state, "snapshot_hash", lambda candidate_path: None)
+ real_stat = hash_mode_state.os.stat
+
+ def flaky_stat(candidate_path, *args, **kwargs):
+ if candidate_path == str(path):
+ raise NotADirectoryError("stale handle mid-drain")
+ return real_stat(candidate_path, *args, **kwargs)
+
+ monkeypatch.setattr(hash_mode_state.os, "stat", flaky_stat)
+
+ transition = record_transition_intent(session)
+ enqueue_transition_work(session, transition)
+ drain_transition_queue(session)
+ session.commit()
+
+ assert session.get(AssetContent, content_id).is_missing is False, (
+ "a transient stat failure on a file that is still present must not mark its row missing"
+ )
+ assert hash_mode_state.pending_transition_count() == 1
+ assert read_stored_mode(session) == "off"
+
+
+def test_transition_drain_requeues_unstable_present_file_without_marking_it_missing(
+ session, temp_dir, monkeypatch
+):
+ path = temp_dir / "still-being-written.bin"
+ path.write_bytes(b"a partial write in flight")
+ stat = path.stat()
+ content = create_content(session, str(path), size_bytes=stat.st_size, mtime_ns=stat.st_mtime_ns)
+ content_id = content.id
+ create_record(session, content_id, "still-being-written.bin")
+ write_stored_mode(session, "off")
+ monkeypatch.setattr(hash_mode_state._mode, "hashing_enabled", lambda: True)
+ monkeypatch.setattr(hash_mode_state, "snapshot_hash", lambda candidate_path: None)
+
+ transition = record_transition_intent(session)
+ enqueue_transition_work(session, transition)
+ drain_transition_queue(session)
+ session.commit()
+
+ assert session.get(AssetContent, content_id).is_missing is False, (
+ "snapshot_hash returns None for drift too; a file still on disk is unstable, not gone"
+ )
+ assert hash_mode_state.pending_transition_count() == 1
+ assert read_stored_mode(session) == "off"
+
+
+def test_transition_drain_mixes_a_deleted_path_with_a_healthy_one(session, temp_dir, monkeypatch):
+ monkeypatch.setattr("folder_paths.get_input_directory", lambda: str(temp_dir))
+ deleted_path = temp_dir / "deleted.bin"
+ healthy_path = temp_dir / "survivor.bin"
+ deleted_path.write_bytes(b"deleted during the outage")
+ healthy_path.write_bytes(b"survivor")
+ content_ids = {}
+ for path in (deleted_path, healthy_path):
+ stat = path.stat()
+ content = create_content(
+ session, str(path), size_bytes=stat.st_size, mtime_ns=stat.st_mtime_ns
+ )
+ content_ids[path] = content.id
+ create_record(session, content.id, path.name)
+ write_stored_mode(session, "off")
+ monkeypatch.setattr(hash_mode_state._mode, "hashing_enabled", lambda: True)
+ healthy_hash = _stored_hash(healthy_path)
+ deleted_path.unlink()
+
+ transition = record_transition_intent(session)
+ enqueue_transition_work(session, transition)
+ drain_transition_queue(session)
+ session.commit()
+
+ assert session.get(AssetContent, content_ids[deleted_path]).is_missing is True
+ healthy_content = session.get(AssetContent, content_ids[healthy_path])
+ assert (healthy_content.is_missing, healthy_content.hash) == (False, healthy_hash), (
+ "one dead path must not cost the healthy paths behind it their hashes"
+ )
+ assert hash_mode_state.pending_transition_count() == 0
+ assert read_stored_mode(session) == "on"
+
+
+def test_transition_drain_skips_out_of_root_path(session, temp_dir, monkeypatch, caplog):
+ import logging
+
+ outside_path = temp_dir / "orphan.bin"
+ outside_path.write_bytes(b"old bytes")
+ old_snapshot = snapshot_hash(str(outside_path))
+ assert old_snapshot is not None
+ old_digest, _ = old_snapshot
+ stat = outside_path.stat()
+ old_content = create_content(
+ session, str(outside_path), to_stored_hash(old_digest), stat.st_size, stat.st_mtime_ns
+ )
+ old_content_id = old_content.id
+ create_record(session, old_content_id, "orphan.bin")
+ outside_path.write_bytes(b"new bytes")
+
+ with pytest.raises(ValueError):
+ get_name_and_tags_from_asset_path(str(outside_path))
+
+ enqueue_transition_work(session, "off_to_on")
+
+ with caplog.at_level(logging.WARNING):
+ try:
+ drain_transition_queue(session)
+ except ValueError as error:
+ pytest.fail(
+ f"an out-of-root path escaped the drain as {error!r}; setup_database turns that "
+ f"into sys.exit(1) when --enable-assets is set"
+ )
+ session.commit()
+
+ assert session.get(AssetContent, old_content_id).is_missing is False
+ assert hash_mode_state.pending_transition_count() == 0
+ assert any("orphan.bin" in record.getMessage() for record in caplog.records)
+
+
+def _seed_hashed_row(session, path: Path, payload: bytes) -> tuple[str, str]:
+ path.write_bytes(payload)
+ stat = path.stat()
+ stored = to_stored_hash(blake3(payload).hexdigest())
+ content = create_content(session, str(path), stored, stat.st_size, stat.st_mtime_ns)
+ create_record(session, content.id, path.name)
+ return content.id, stored
+
+
+def test_transition_drain_clears_an_unverifiable_hash_only_on_the_third_attempt(
+ session, temp_dir, monkeypatch, caplog
+):
+ path = temp_dir / "permanently-unreadable.bin"
+ content_id, original_hash = _seed_hashed_row(session, path, b"bytes nobody can read")
+ write_stored_mode(session, "off")
+ monkeypatch.setattr(hash_mode_state._mode, "hashing_enabled", lambda: True)
+
+ def always_denied(_candidate_path: str):
+ raise PermissionError("denied")
+
+ monkeypatch.setattr(hash_mode_state, "snapshot_hash", always_denied)
+
+ transition = record_transition_intent(session)
+ enqueue_transition_work(session, transition)
+
+ def warnings_naming_the_path() -> list[str]:
+ return [r.getMessage() for r in caplog.records if str(path) in r.getMessage()]
+
+ with caplog.at_level(logging.WARNING):
+ for attempt in (1, 2):
+ drain_transition_queue(session)
+ session.commit()
+ session.expire_all()
+ assert hash_mode_state.pending_transition_count() == 1, (
+ f"attempt {attempt} of 3 must requeue the entry, not retire it"
+ )
+ assert read_stored_mode(session) == "off", (
+ f"the mode must not flip while attempt {attempt} is still pending"
+ )
+ assert session.get(AssetContent, content_id).hash == original_hash, (
+ f"a transient failure on attempt {attempt} must not clear a good hash"
+ )
+ assert warnings_naming_the_path() == [], (
+ f"attempt {attempt} is a retry, not a terminal outcome; it must stay quiet"
+ )
+
+ drain_transition_queue(session)
+ session.commit()
+
+ session.expire_all()
+ assert hash_mode_state.pending_transition_count() == 0, (
+ "the third failure retires the entry so the queue can empty"
+ )
+ assert read_stored_mode(session) == "on", (
+ "an unreadable file must not wedge the mode flip for the process lifetime"
+ )
+ retired = session.get(AssetContent, content_id)
+ assert retired.is_missing is False, (
+ "the file is unreadable, not gone; retiring its hash must not mark the row missing"
+ )
+ assert retired.hash is None, (
+ "an unverifiable digest must not survive into persisted 'on'"
+ )
+ assert len(warnings_naming_the_path()) == 1, (
+ "the terminal outcome is announced exactly once, naming the path"
+ )
+
+
+def test_transition_drain_retires_only_the_unreadable_path_and_hashes_the_healthy_one(
+ session, temp_dir, monkeypatch
+):
+ unreadable_path = temp_dir / "unreadable.bin"
+ healthy_path = temp_dir / "healthy.bin"
+ healthy_payload = b"a file that reads fine"
+ unreadable_id, _ = _seed_hashed_row(session, unreadable_path, b"a file that does not")
+ healthy_path.write_bytes(healthy_payload)
+ healthy_stat = healthy_path.stat()
+ healthy_content = create_content(
+ session, str(healthy_path), size_bytes=healthy_stat.st_size, mtime_ns=healthy_stat.st_mtime_ns
+ )
+ healthy_id = healthy_content.id
+ create_record(session, healthy_id, healthy_path.name)
+ write_stored_mode(session, "off")
+ monkeypatch.setattr(hash_mode_state._mode, "hashing_enabled", lambda: True)
+
+ def denied_for_the_unreadable_path(candidate_path: str):
+ if candidate_path == str(unreadable_path):
+ raise PermissionError("denied")
+ return snapshot_hash(candidate_path)
+
+ monkeypatch.setattr(hash_mode_state, "snapshot_hash", denied_for_the_unreadable_path)
+
+ transition = record_transition_intent(session)
+ enqueue_transition_work(session, transition)
+ for _ in range(3):
+ drain_transition_queue(session)
+ session.commit()
+
+ session.expire_all()
+ assert session.get(AssetContent, healthy_id).hash == _stored_hash(healthy_path), (
+ "one unreadable path must not cost the healthy paths behind it their hashes"
+ )
+ assert session.get(AssetContent, unreadable_id).hash is None
+ assert hash_mode_state.pending_transition_count() == 0
+ assert read_stored_mode(session) == "on"
diff --git a/tests-unit/assets_test/services/test_transition_settle.py b/tests-unit/assets_test/services/test_transition_settle.py
new file mode 100644
index 000000000..3da231ba5
--- /dev/null
+++ b/tests-unit/assets_test/services/test_transition_settle.py
@@ -0,0 +1,108 @@
+from contextlib import contextmanager
+from pathlib import Path
+from unittest.mock import patch
+
+import pytest
+from sqlalchemy.orm import Session as SASession
+
+from app.assets import scanner, seeder as seeder_module
+from app.assets.database.models import AssetContent
+from app.assets.database.queries.records import create_content, create_record
+from app.assets.helpers import to_stored_hash
+from app.assets.services import hash_mode_state
+from app.assets.services.hash_mode_state import (
+ clear_transition_queue,
+ enqueue_transition_work,
+ read_stored_mode,
+ record_transition_intent,
+ write_stored_mode,
+)
+
+_ATTEMPT_BUDGET = 5
+
+
+class _AttemptBudgetExhausted(BaseException):
+ # BaseException, not Exception: enrich_assets_batch's blanket except would swallow it.
+ pass
+
+
[email protected](autouse=True)
+def transition_queue():
+ clear_transition_queue()
+ yield
+ clear_transition_queue()
+
+
+def _denied(_candidate_path: str):
+ raise PermissionError("denied")
+
+
+def test_enrich_phase_settles_an_unreadable_transition_without_looping(
+ session, db_engine, temp_dir: Path, monkeypatch
+):
+ path = temp_dir / "unreadable.safetensors"
+ payload = b"bytes that can be stat'd but never hashed"
+ path.write_bytes(payload)
+ stat = path.stat()
+ content = create_content(
+ session, str(path), to_stored_hash("seed-digest"), stat.st_size, stat.st_mtime_ns
+ )
+ content_id = content.id
+ record = create_record(session, content_id, path.name)
+ record_id = record.id
+ write_stored_mode(session, "off")
+ monkeypatch.setattr(hash_mode_state._mode, "hashing_enabled", lambda: True)
+
+ transition = record_transition_intent(session)
+ enqueue_transition_work(session, transition)
+ session.commit()
+
+ @contextmanager
+ def _create_session():
+ with SASession(db_engine) as sess:
+ yield sess
+
+ attempts: list[str] = []
+ real_enrich_asset = scanner.enrich_asset
+
+ def counting_enrich_asset(*args, **kwargs):
+ attempts.append(kwargs["record_id"])
+ if len(attempts) > _ATTEMPT_BUDGET:
+ raise _AttemptBudgetExhausted
+ return real_enrich_asset(*args, **kwargs)
+
+ seeder = seeder_module._AssetSeeder()
+ seeder._compute_hashes = True
+ seeder._run_gate.set()
+ seeder._cancel_event.clear()
+
+ monkeypatch.setattr("folder_paths.get_input_directory", lambda: str(temp_dir))
+ monkeypatch.setattr(hash_mode_state, "snapshot_hash", _denied)
+ monkeypatch.setattr(scanner, "snapshot_hash", _denied)
+ monkeypatch.setattr(scanner, "enrich_asset", counting_enrich_asset)
+
+ with patch("app.assets.seeder.create_session", _create_session), \
+ patch("app.assets.scanner.create_session", _create_session):
+ try:
+ cancelled, _enriched = seeder._run_enrich_phase(("input",))
+ except _AttemptBudgetExhausted:
+ pytest.fail(
+ f"the enrich phase re-selected the same record more than {_ATTEMPT_BUDGET} "
+ "times: a terminally-cleared row stays hash-eligible, so counting its "
+ "metadata as progress keeps it out of failed_ids and the pass never ends"
+ )
+
+ assert cancelled is False
+ session.expire_all()
+ assert read_stored_mode(session) == "on", (
+ "one background scan must settle the transition on its own; waiting for a future "
+ "prompt to queue another enrich pass leaves a quiet server wedged at 'off'"
+ )
+ settled = session.get(AssetContent, content_id)
+ assert settled.is_missing is False, (
+ "an unreadable file is not a deleted one; settling must not mark its row missing"
+ )
+ assert settled.hash is None
+ assert attempts == [record_id], (
+ "the record is attempted once, then excluded from the rest of the pass"
+ )
diff --git a/tests-unit/assets_test/services/test_updated_at_semantics.py b/tests-unit/assets_test/services/test_updated_at_semantics.py
new file mode 100644
index 000000000..bfd17082b
--- /dev/null
+++ b/tests-unit/assets_test/services/test_updated_at_semantics.py
@@ -0,0 +1,285 @@
+
+import os
+from datetime import datetime
+
+import pytest
+from sqlalchemy import update
+from sqlalchemy.orm import Session
+
+from app.assets.database.models import Asset, AssetTag, Tag
+from app.assets.database.queries.records import (
+ create_content,
+ create_record,
+ delete_record,
+ mark_content_missing,
+ unset_content_missing,
+)
+from app.assets.helpers import to_stored_hash
+from app.assets.scanner import enrich_asset
+from app.assets.scanner_changes import split_content
+from app.assets.services.asset_management import (
+ resolve_asset_for_download,
+ resolve_hash_to_path,
+ update_asset_metadata,
+)
+from app.assets.services.tagging import apply_tags, remove_tags
+
+STALE = datetime(2020, 1, 1, 0, 0, 0)
+
+
+def _seed_record(session: Session, path: str, name: str = "fixture", tags=None, hash=None) -> Asset:
+ content = create_content(session, path, hash=hash, size_bytes=os.stat(path).st_size)
+ record = create_record(session, content.id, name, tags=tags)
+ session.commit()
+ _force_stale(session, record.id)
+ return record
+
+
+def _seed_scannable_record(session: Session, path: str, name: str) -> Asset:
+ stat_result = os.stat(path)
+ content = create_content(
+ session,
+ path,
+ size_bytes=stat_result.st_size,
+ mtime_ns=stat_result.st_mtime_ns,
+ )
+ record = create_record(session, content.id, name)
+ session.commit()
+ _force_stale(session, record.id)
+ return record
+
+
+def _force_stale(session: Session, record_id: str) -> None:
+ session.execute(update(Asset).where(Asset.id == record_id).values(updated_at=STALE))
+ session.commit()
+
+
+def _updated_at(session: Session, record_id: str) -> datetime:
+ session.expire_all()
+ return session.get(Asset, record_id).updated_at
+
+
+def _last_access(session: Session, record_id: str) -> datetime | None:
+ session.expire_all()
+ return session.get(Asset, record_id).last_access_time
+
+
+def _write_file(temp_dir, name: str, payload: bytes = b"payload") -> str:
+ file_path = temp_dir / name
+ file_path.write_bytes(payload)
+ return str(file_path)
+
+
+def test_download_path_moves_last_access_time_but_not_updated_at(
+ session, mock_create_session, temp_dir
+):
+ path = _write_file(temp_dir, "download.bin")
+ record = _seed_record(session, path, name="download.bin")
+
+ resolve_asset_for_download(record.id)
+
+ assert _last_access(session, record.id) is not None, (
+ "serving a download must record the access"
+ )
+ assert _updated_at(session, record.id) == STALE, (
+ "a read moves last_access_time only; it is not an explicit user edit"
+ )
+
+
+def test_hash_serve_path_moves_last_access_time_but_not_updated_at(
+ session, mock_create_session, temp_dir
+):
+ path = _write_file(temp_dir, "served.bin")
+ digest = to_stored_hash("a" * 64)
+ record = _seed_record(session, path, name="served.bin", hash=digest)
+
+ assert resolve_hash_to_path(digest) is not None, "fixture must resolve"
+
+ assert _last_access(session, record.id) is not None, (
+ "serving by hash must record the access"
+ )
+ assert _updated_at(session, record.id) == STALE, (
+ "hash-serving is a read; it is not an explicit user edit"
+ )
+
+
+def test_rename_moves_updated_at(session, mock_create_session, temp_dir):
+ record = _seed_record(session, _write_file(temp_dir, "rename.bin"))
+
+ update_asset_metadata(record.id, name="renamed")
+
+ assert _updated_at(session, record.id) > STALE, "a rename is an explicit user edit"
+
+
+ "field,kwargs",
+ [
+ ("user_metadata", {"user_metadata": {"note": "hello"}}),
+ ("mime_type", {"mime_type": "image/png"}),
+ ],
+)
+def test_user_field_updates_move_updated_at(
+ session, mock_create_session, temp_dir, field, kwargs
+):
+ record = _seed_record(session, _write_file(temp_dir, f"{field}.bin"))
+
+ update_asset_metadata(record.id, **kwargs)
+
+ assert _updated_at(session, record.id) > STALE, (
+ f"setting {field} is an explicit user edit"
+ )
+
+
+def test_preview_id_update_moves_updated_at(session, mock_create_session, temp_dir):
+ record = _seed_record(session, _write_file(temp_dir, "subject.bin"))
+ preview = _seed_record(session, _write_file(temp_dir, "preview.bin"), name="preview")
+
+ update_asset_metadata(record.id, preview_id=preview.id)
+
+ assert _updated_at(session, record.id) > STALE, (
+ "setting preview_id is an explicit user edit"
+ )
+
+
+def test_manual_tag_add_moves_updated_at(session, mock_create_session, temp_dir):
+ record = _seed_record(session, _write_file(temp_dir, "tag-add.bin"))
+
+ apply_tags(record.id, ["favourite"])
+
+ assert _updated_at(session, record.id) > STALE, (
+ "adding a manual tag is an explicit user edit of the record"
+ )
+
+
+def test_manual_tag_remove_moves_updated_at(session, mock_create_session, temp_dir):
+ record = _seed_record(
+ session, _write_file(temp_dir, "tag-remove.bin"), tags=["favourite"]
+ )
+
+ remove_tags(record.id, ["favourite"])
+
+ assert _updated_at(session, record.id) > STALE, (
+ "removing a manual tag is an explicit user edit of the record"
+ )
+
+
+def test_tag_replacement_via_update_asset_metadata_moves_updated_at(
+ session, mock_create_session, temp_dir
+):
+ record = _seed_record(session, _write_file(temp_dir, "tag-replace.bin"), tags=["old"])
+
+ update_asset_metadata(record.id, tags=["new"])
+
+ assert _updated_at(session, record.id) > STALE, (
+ "replacing the manual tag set is an explicit user edit"
+ )
+
+
+def test_noop_tag_calls_do_not_move_updated_at(session, mock_create_session, temp_dir):
+ record = _seed_record(
+ session, _write_file(temp_dir, "tag-noop.bin"), tags=["already"]
+ )
+
+ apply_tags(record.id, ["already"])
+ remove_tags(record.id, ["never-present"])
+
+ assert _updated_at(session, record.id) == STALE, (
+ "a tag call that changes no link is not an edit"
+ )
+
+
+def test_unchanged_tag_replacement_does_not_move_updated_at(
+ session, mock_create_session, temp_dir
+):
+ record = _seed_record(
+ session, _write_file(temp_dir, "tag-same.bin"), tags=["keep"]
+ )
+
+ update_asset_metadata(record.id, tags=["keep"])
+
+ assert _updated_at(session, record.id) == STALE, (
+ "replacing the manual tag set with an identical set changes nothing"
+ )
+
+
+def test_scanner_enrichment_does_not_move_updated_at(session, temp_dir):
+ path = _write_file(temp_dir, "enrich.png", payload=b"\x89PNG\r\n\x1a\n" + b"0" * 64)
+ record = _seed_scannable_record(session, path, name="enrich.png")
+
+ assert enrich_asset(
+ session, path, record.content_id, record.id, extract_metadata=True
+ ), "fixture must actually enrich"
+
+ assert session.get(Asset, record.id).mime_type is not None, (
+ "enrichment must have written a system-derived field"
+ )
+ assert _updated_at(session, record.id) == STALE, (
+ "system-derived facts about unchanged bytes are not a user edit"
+ )
+
+
+def test_automatic_missing_tag_projection_does_not_move_updated_at(session, temp_dir):
+ record = _seed_record(session, _write_file(temp_dir, "missing.bin"))
+
+ mark_content_missing(session, record.content_id)
+ session.commit()
+ assert _updated_at(session, record.id) == STALE, (
+ "the automatic missing tag is a system projection"
+ )
+
+ _force_stale(session, record.id)
+ unset_content_missing(session, record.content_id)
+ session.commit()
+ assert _updated_at(session, record.id) == STALE, (
+ "recovery is a system projection too"
+ )
+
+
+def test_content_split_does_not_move_updated_at_on_retired_record(
+ session, temp_dir, monkeypatch
+):
+ monkeypatch.setattr("folder_paths.get_input_directory", lambda: str(temp_dir))
+ path = _write_file(temp_dir, "split.bin")
+ record = _seed_record(session, path, name="split.bin")
+ content = session.get(Asset, record.id).content
+
+ split_content(session, content, os.stat(path), hash_value=to_stored_hash("b" * 64))
+ session.commit()
+
+ assert _updated_at(session, record.id) == STALE, (
+ "a content split is a system projection over the retired record"
+ )
+
+
+def test_preview_target_delete_cascade_does_not_move_updated_at(db_engine_fk):
+ with Session(db_engine_fk) as session:
+ preview_content = create_content(session, "/output/preview-target.png")
+ preview = create_record(session, preview_content.id, "preview-target")
+ content = create_content(session, "/output/referencing.png")
+ record = create_record(session, content.id, "referencing")
+ session.commit()
+ session.execute(
+ update(Asset)
+ .where(Asset.id == record.id)
+ .values(preview_id=preview.id, updated_at=STALE)
+ )
+ session.commit()
+
+ delete_record(session, preview.id)
+ session.commit()
+
+ session.expire_all()
+ refreshed = session.get(Asset, record.id)
+ assert refreshed.preview_id is None, "the FK cascade must have nulled the link"
+ assert refreshed.updated_at == STALE, (
+ "an FK-level SET NULL is not a user edit of the referencing record"
+ )
+
+
+def test_seed_helper_reports_stale_before_any_edit(session, temp_dir):
+ record = _seed_record(session, _write_file(temp_dir, "seed.bin"))
+
+ assert _updated_at(session, record.id) == STALE, "seeding must set the stale marker"
+ assert isinstance(session.get(Asset, record.id), Asset)
+ assert session.get(Tag, "nonexistent") is None
+ assert session.get(AssetTag, (record.id, "nonexistent")) is None
diff --git a/tests-unit/assets_test/services/test_upload_b.py b/tests-unit/assets_test/services/test_upload_b.py
new file mode 100644
index 000000000..3e3cdc055
--- /dev/null
+++ b/tests-unit/assets_test/services/test_upload_b.py
@@ -0,0 +1,1402 @@
+import os
+import uuid
+from contextlib import contextmanager
+from unittest.mock import patch
+
+import pytest
+from sqlalchemy import create_engine, func, select, update
+from sqlalchemy.exc import OperationalError
+from sqlalchemy.orm import Session as SASession
+
+import app.assets.mode as mode_module
+import app.assets.services.ingest as ingest_module
+import folder_paths
+from app.assets.database.models import Asset, AssetContent, AssetTag
+from app.assets.database.queries.records import (
+ create_content,
+ create_record,
+ mark_content_missing,
+)
+from app.assets.helpers import to_stored_hash
+from app.assets.services.asset_management import delete_asset_reference
+from app.assets.services.file_utils import get_size_and_mtime_ns
+from app.assets.services.ingest import (
+ UploadUnstableError,
+ register_file_in_place,
+ upload_from_temp_path,
+)
+from app.assets.services.lookup import (
+ claim_qualified_content as _real_claim_qualified_content,
+ is_temp_path,
+ lookup_for_view,
+)
+from app.assets.services.snapshot_hash import snapshot_hash
+from app.database.models import Base
+
+
+def _bump_mtime(path: str) -> int:
+ _, mtime_ns = get_size_and_mtime_ns(path)
+ moved = mtime_ns + 1_000_000_000
+ os.utime(path, ns=(moved, moved))
+ observed = get_size_and_mtime_ns(path)[1]
+ assert observed != mtime_ns
+ return observed
+
+
+def hashing_on():
+ class FakeArgs:
+ enable_asset_hashing = True
+
+ mode_module.init(FakeArgs())
+ yield
+ mode_module.init(None)
+
+
+def hashing_off():
+ class FakeArgs:
+ enable_asset_hashing = False
+
+ mode_module.init(FakeArgs())
+ yield
+ mode_module.init(None)
+
+
+def _write_temp(content: bytes) -> str:
+ uploads_root = os.path.join(folder_paths.get_temp_directory(), "uploads", uuid.uuid4().hex)
+ os.makedirs(uploads_root, exist_ok=True)
+ path = os.path.join(uploads_root, ".upload.part")
+ with open(path, "wb") as file:
+ file.write(content)
+ return path
+
+
+def test_hash_mode_multipart_reuses_content_but_in_place_keeps_its_own_path(
+ mock_create_session, hashing_on
+):
+ content = b"duplicate-bytes"
+ temp1 = _write_temp(content)
+ temp2 = _write_temp(content)
+ output_dir = folder_paths.get_output_directory()
+ os.makedirs(output_dir, exist_ok=True)
+ image_path = os.path.join(output_dir, "dup.bin")
+ try:
+ r1 = upload_from_temp_path(
+ temp_path=temp1,
+ name="dup.bin",
+ tags=["output"],
+ client_filename="dup.bin",
+ )
+ r2 = upload_from_temp_path(
+ temp_path=temp2,
+ name="dup.bin",
+ tags=["output"],
+ client_filename="dup.bin",
+ )
+ assert r1.ref.id != r2.ref.id, "every upload is its own delivery record"
+ assert r2.created_new is True
+ with mock_create_session() as session:
+ assert (
+ session.scalar(
+ select(func.count())
+ .select_from(AssetContent)
+ .where(AssetContent.is_missing.is_(False))
+ )
+ == 1
+ ), "same bytes reuse the one content row"
+
+ with open(image_path, "wb") as file:
+ file.write(content)
+ r3 = register_file_in_place(
+ abs_path=image_path, name="dup.bin", tags=["output"]
+ )
+ assert r3.ref.id != r1.ref.id
+ assert r3.created_new is True
+ assert r3.ref.file_path == os.path.abspath(image_path)
+
+ with mock_create_session() as session:
+ assert session.scalar(select(func.count()).select_from(Asset)) == 3
+ live = {
+ row.id: row.path
+ for row in session.scalars(
+ select(AssetContent).where(AssetContent.is_missing.is_(False))
+ )
+ }
+ assert len(live) == 2
+ in_place = session.get(Asset, r3.ref.id)
+ multipart = session.get(Asset, r1.ref.id)
+ assert in_place is not None
+ assert multipart is not None
+ assert in_place.content_id != multipart.content_id
+ assert live[in_place.content_id] == os.path.abspath(image_path)
+ assert live[multipart.content_id] != os.path.abspath(image_path)
+ finally:
+ for path in (temp1, temp2, image_path):
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def test_off_mode_api_assets_reuses_content_same_bytes_same_name(
+ mock_create_session, hashing_off
+):
+ content = b"off-mode-bytes"
+ temp1 = _write_temp(content)
+ temp2 = _write_temp(content)
+ try:
+ r1 = upload_from_temp_path(
+ temp_path=temp1,
+ name="off.bin",
+ tags=["output"],
+ client_filename="off.bin",
+ )
+ r2 = upload_from_temp_path(
+ temp_path=temp2,
+ name="off.bin",
+ tags=["output"],
+ client_filename="off.bin",
+ )
+ assert r1.ref.id != r2.ref.id, "every upload is its own delivery record"
+ assert r2.created_new is True
+ with mock_create_session() as session:
+ assert session.scalar(select(func.count()).select_from(Asset)) == 2
+ contents = list(
+ session.scalars(
+ select(AssetContent).where(AssetContent.is_missing.is_(False))
+ )
+ )
+ assert len(contents) == 1
+ finally:
+ for path in (temp1, temp2):
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def test_off_mode_register_file_in_place_same_path_reuses_content_row(
+ mock_create_session, hashing_off
+):
+ output_dir = folder_paths.get_output_directory()
+ os.makedirs(output_dir, exist_ok=True)
+ path = os.path.join(output_dir, "image_same.png")
+ with open(path, "wb") as file:
+ file.write(b"img-bytes")
+
+ try:
+ r1 = register_file_in_place(
+ abs_path=path, name="image_same.png", tags=["output"]
+ )
+ r2 = register_file_in_place(
+ abs_path=path, name="image_same.png", tags=["output"]
+ )
+ assert r2.ref.id != r1.ref.id
+ assert r2.created_new is True
+ with mock_create_session() as session:
+ contents = list(
+ session.scalars(
+ select(AssetContent).where(AssetContent.is_missing.is_(False))
+ )
+ )
+ assert len(contents) == 1
+ assert contents[0].path == os.path.abspath(path)
+ assert open(contents[0].path, "rb").read() == b"img-bytes"
+ records = list(session.scalars(select(Asset)))
+ assert len(records) == 2
+ assert {record.content_id for record in records} == {contents[0].id}
+ finally:
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def test_off_mode_register_file_in_place_different_bytes_two_paths(
+ mock_create_session, hashing_off
+):
+ output_dir = folder_paths.get_output_directory()
+ os.makedirs(output_dir, exist_ok=True)
+ path1 = os.path.join(output_dir, "same.png")
+ path2 = os.path.join(output_dir, "same (1).png")
+ with open(path1, "wb") as file:
+ file.write(b"v1")
+ with open(path2, "wb") as file:
+ file.write(b"v2")
+
+ try:
+ r1 = register_file_in_place(abs_path=path1, name="same.png", tags=["output"])
+ r2 = register_file_in_place(abs_path=path2, name="same.png", tags=["output"])
+ assert r1.ref.id != r2.ref.id
+ with mock_create_session() as session:
+ contents = list(
+ session.scalars(
+ select(AssetContent).where(AssetContent.is_missing.is_(False))
+ )
+ )
+ assert len(contents) == 2
+ assert {content.path for content in contents} == {
+ os.path.abspath(path1),
+ os.path.abspath(path2),
+ }
+ finally:
+ for path in (path1, path2):
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def test_register_file_in_place_equal_bytes_new_path_is_tracked_separately(
+ mock_create_session, hashing_on
+):
+ output_dir = folder_paths.get_output_directory()
+ os.makedirs(output_dir, exist_ok=True)
+ path_a = os.path.join(output_dir, "crosspath_a.png")
+ path_b = os.path.join(output_dir, "crosspath_b.png")
+ payload = b"cross-path-equal-bytes"
+ try:
+ with open(path_a, "wb") as file:
+ file.write(payload)
+ first = register_file_in_place(
+ abs_path=path_a, name="crosspath_a.png", tags=["output"]
+ )
+
+ with open(path_b, "wb") as file:
+ file.write(payload)
+ second = register_file_in_place(
+ abs_path=path_b, name="crosspath_b.png", tags=["output"]
+ )
+
+ abs_a = os.path.abspath(path_a)
+ abs_b = os.path.abspath(path_b)
+ assert second.ref.id != first.ref.id
+ assert second.created_new is True
+ assert second.ref.file_path == abs_b
+ with mock_create_session() as session:
+ live = {
+ row.path: row
+ for row in session.scalars(
+ select(AssetContent).where(AssetContent.is_missing.is_(False))
+ )
+ }
+ assert set(live) == {abs_a, abs_b}
+ assert live[abs_a].hash == live[abs_b].hash
+ assert live[abs_a].id != live[abs_b].id
+ record_b = session.get(Asset, second.ref.id)
+ assert record_b is not None
+ assert record_b.content_id == live[abs_b].id
+ record_a = session.get(Asset, first.ref.id)
+ assert record_a is not None
+ assert record_a.content_id == live[abs_a].id
+ finally:
+ for path in (path_a, path_b):
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def test_register_file_in_place_overwrite_returns_new_file_hash_and_size(
+ mock_create_session, hashing_on
+):
+ output_dir = folder_paths.get_output_directory()
+ os.makedirs(output_dir, exist_ok=True)
+ path = os.path.join(output_dir, "overwrite_hash.png")
+
+ v1 = b"overwrite-version-one"
+ v2 = b"v2"
+ try:
+ with open(path, "wb") as file:
+ file.write(v1)
+ r1 = register_file_in_place(
+ abs_path=path, name="overwrite_hash.png", tags=["output"]
+ )
+
+ with open(path, "wb") as file:
+ file.write(v2)
+ snapshot = snapshot_hash(path)
+ assert snapshot is not None
+ expected_new_hash = to_stored_hash(snapshot[0])
+
+ r2 = register_file_in_place(
+ abs_path=path, name="overwrite_hash.png", tags=["output"]
+ )
+
+ assert r2.asset.hash == expected_new_hash
+ assert r2.asset.hash != r1.asset.hash
+ assert r2.asset.size_bytes == len(v2)
+ finally:
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def test_register_file_in_place_overwrite_marks_old_content_missing(
+ mock_create_session, hashing_on
+):
+ output_dir = folder_paths.get_output_directory()
+ os.makedirs(output_dir, exist_ok=True)
+ path = os.path.join(output_dir, "overwrite_missing.png")
+
+ v1 = b"overwrite-version-one"
+ v2 = b"v2"
+ try:
+ with open(path, "wb") as file:
+ file.write(v1)
+ r1 = register_file_in_place(
+ abs_path=path, name="overwrite_missing.png", tags=["output"]
+ )
+
+ with open(path, "wb") as file:
+ file.write(v2)
+ snapshot = snapshot_hash(path)
+ assert snapshot is not None
+ expected_new_hash = to_stored_hash(snapshot[0])
+
+ register_file_in_place(
+ abs_path=path, name="overwrite_missing.png", tags=["output"]
+ )
+
+ with mock_create_session() as session:
+ rows = list(
+ session.scalars(
+ select(AssetContent).where(
+ AssetContent.path == os.path.abspath(path)
+ )
+ )
+ )
+ live = [row for row in rows if not row.is_missing]
+ missing = [row for row in rows if row.is_missing]
+ assert len(live) == 1
+ assert live[0].hash == expected_new_hash
+ assert len(missing) == 1
+ assert missing[0].hash == r1.asset.hash
+ finally:
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def _digest_of(path: str) -> str:
+ snapshot = snapshot_hash(path)
+ assert snapshot is not None
+ return snapshot[0]
+
+
+def _seed_live_content(session, path: str, stored_hash: str | None) -> tuple[str, str]:
+ size_bytes, mtime_ns = get_size_and_mtime_ns(path)
+ content = create_content(session, path, stored_hash, size_bytes, mtime_ns)
+ record = create_record(
+ session, content.id, os.path.basename(path), tags=["output"]
+ )
+ session.commit()
+ return content.id, record.id
+
+
+def test_register_file_in_place_record_failure_preserves_retained_live_content(
+ mock_create_session, hashing_off, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ output_dir = folder_paths.get_output_directory()
+ os.makedirs(output_dir, exist_ok=True)
+ path = os.path.join(output_dir, f"retained_{uuid.uuid4().hex}.bin")
+ with open(path, "wb") as file:
+ file.write(b"retained-live-content")
+
+ try:
+ original = register_file_in_place(
+ abs_path=path,
+ name="retained.bin",
+ tags=["output"],
+ )
+ retained_content_id = original.content_id
+ assert delete_asset_reference(original.ref.id) is True
+
+ def _raise_record_creation(*_args, **_kwargs):
+ raise RuntimeError("forced record creation failure")
+
+ monkeypatch.setattr(
+ ingest_module,
+ "_create_upload_record",
+ _raise_record_creation,
+ )
+
+ with pytest.raises(RuntimeError, match="forced record creation failure"):
+ register_file_in_place(
+ abs_path=path,
+ name="retained.bin",
+ tags=["output"],
+ )
+
+ with mock_create_session() as session:
+ assert session.get(AssetContent, retained_content_id) is not None
+ finally:
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def _is_missing_tagged(session, record_id: str) -> bool:
+ return (
+ session.get(AssetTag, {"asset_id": record_id, "tag_name": "missing"})
+ is not None
+ )
+
+
+def test_register_file_in_place_unhashed_unchanged_file_is_not_retired(
+ mock_create_session, hashing_on
+):
+ output_dir = folder_paths.get_output_directory()
+ os.makedirs(output_dir, exist_ok=True)
+ path = os.path.join(output_dir, "unhashed_noop.png")
+ payload = b"scanner-seeded-bytes"
+ try:
+ with open(path, "wb") as file:
+ file.write(payload)
+ with mock_create_session() as session:
+ content_id, record_id = _seed_live_content(
+ session, os.path.abspath(path), None
+ )
+
+ register_file_in_place(
+ abs_path=path,
+ name="unhashed_noop.png",
+ tags=["output"],
+ content_written=False,
+ )
+
+ with mock_create_session() as session:
+ content = session.get(AssetContent, content_id)
+ assert content is not None
+ assert content.is_missing is False
+ assert _is_missing_tagged(session, record_id) is False
+ live = list(
+ session.scalars(
+ select(AssetContent).where(
+ AssetContent.path == os.path.abspath(path),
+ AssetContent.is_missing.is_(False),
+ )
+ )
+ )
+ assert [row.id for row in live] == [content_id]
+ finally:
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def test_register_file_in_place_unhashed_unchanged_file_adopts_hash(
+ mock_create_session, hashing_on
+):
+ output_dir = folder_paths.get_output_directory()
+ os.makedirs(output_dir, exist_ok=True)
+ path = os.path.join(output_dir, "unhashed_adopt.png")
+ payload = b"scanner-seeded-adopt-bytes"
+ try:
+ with open(path, "wb") as file:
+ file.write(payload)
+ with mock_create_session() as session:
+ content_id, record_id = _seed_live_content(
+ session, os.path.abspath(path), None
+ )
+ expected_hash = to_stored_hash(_digest_of(path))
+
+ result = register_file_in_place(
+ abs_path=path,
+ name="unhashed_adopt.png",
+ tags=["output"],
+ content_written=False,
+ )
+
+ assert result.ref.id != record_id
+ assert result.created_new is True
+ assert result.asset.hash == expected_hash
+ assert result.asset.size_bytes == len(payload)
+ with mock_create_session() as session:
+ content = session.get(AssetContent, content_id)
+ assert content is not None
+ assert content.hash == expected_hash
+ assert content.is_missing is False
+ record = session.get(Asset, result.ref.id)
+ assert record is not None
+ assert record.content_id == content_id
+ live = list(
+ session.scalars(
+ select(AssetContent).where(
+ AssetContent.path == os.path.abspath(path),
+ AssetContent.is_missing.is_(False),
+ )
+ )
+ )
+ assert [row.id for row in live] == [content_id]
+ finally:
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def test_register_file_in_place_unhashed_changed_file_is_retired(
+ mock_create_session, hashing_on
+):
+ output_dir = folder_paths.get_output_directory()
+ os.makedirs(output_dir, exist_ok=True)
+ path = os.path.join(output_dir, "unhashed_changed.png")
+ original = b"scanner-seeded-original-bytes"
+ replacement = b"Z" * len(original)
+ try:
+ with open(path, "wb") as file:
+ file.write(original)
+ with mock_create_session() as session:
+ content_id, record_id = _seed_live_content(
+ session, os.path.abspath(path), None
+ )
+ with open(path, "wb") as file:
+ file.write(replacement)
+ expected_hash = to_stored_hash(_digest_of(path))
+
+ result = register_file_in_place(
+ abs_path=path,
+ name="unhashed_changed.png",
+ tags=["output"],
+ content_written=True,
+ )
+
+ assert result.asset.hash == expected_hash
+ with mock_create_session() as session:
+ stale = session.get(AssetContent, content_id)
+ assert stale is not None
+ assert stale.hash != expected_hash
+ assert stale.is_missing is True
+ assert _is_missing_tagged(session, record_id) is True
+ seeded_record = session.get(Asset, record_id)
+ assert seeded_record is not None
+ assert seeded_record.content_id == content_id
+ assert result.ref.id != record_id
+ live = list(
+ session.scalars(
+ select(AssetContent).where(
+ AssetContent.path == os.path.abspath(path),
+ AssetContent.is_missing.is_(False),
+ )
+ )
+ )
+ assert len(live) == 1
+ assert live[0].hash == expected_hash
+ assert live[0].id != content_id
+ finally:
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def test_register_file_in_place_matching_hash_refreshes_stale_stat(
+ mock_create_session, hashing_on
+):
+ output_dir = folder_paths.get_output_directory()
+ os.makedirs(output_dir, exist_ok=True)
+ path = os.path.join(output_dir, "stale_mtime.png")
+ payload = b"stale-mtime-bytes"
+ try:
+ with open(path, "wb") as file:
+ file.write(payload)
+ stored_hash = to_stored_hash(_digest_of(path))
+ with mock_create_session() as session:
+ content_id, record_id = _seed_live_content(
+ session, os.path.abspath(path), stored_hash
+ )
+ observed_mtime = _bump_mtime(path)
+
+ result = register_file_in_place(
+ abs_path=path, name="stale_mtime.png", tags=["output"]
+ )
+
+ assert result.ref.id != record_id
+ assert result.created_new is True
+ with mock_create_session() as session:
+ content = session.get(AssetContent, content_id)
+ assert content is not None
+ assert content.is_missing is False
+ assert content.mtime_ns == observed_mtime
+ assert content.size_bytes == len(payload)
+ served = lookup_for_view(session, stored_hash)
+ assert served is not None
+ assert served.id == content_id
+ record = session.get(Asset, result.ref.id)
+ assert record is not None
+ assert record.content_id == content_id
+ assert session.scalar(select(func.count()).select_from(Asset)) == 2
+ live = list(
+ session.scalars(
+ select(AssetContent).where(AssetContent.is_missing.is_(False))
+ )
+ )
+ assert [row.id for row in live] == [content_id]
+ finally:
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def test_upload_unhashed_row_at_destination_is_not_retired(
+ mock_create_session, hashing_on
+):
+ payload = b"upload-adopt-destination-bytes"
+ probe = _write_temp(payload)
+ digest = _digest_of(probe)
+ os.unlink(probe)
+ output_dir = folder_paths.get_output_directory()
+ os.makedirs(output_dir, exist_ok=True)
+ dest = os.path.join(output_dir, f"{digest}.bin")
+ temp = _write_temp(payload)
+ try:
+ with open(dest, "wb") as file:
+ file.write(payload)
+ with mock_create_session() as session:
+ content_id, record_id = _seed_live_content(session, dest, None)
+
+ result = upload_from_temp_path(
+ temp_path=temp, name="up.bin", tags=["output"], client_filename="up.bin"
+ )
+
+ assert result.asset.hash == to_stored_hash(digest)
+ assert result.asset.size_bytes == len(payload)
+ with mock_create_session() as session:
+ content = session.get(AssetContent, content_id)
+ assert content is not None
+ assert content.is_missing is False
+ assert _is_missing_tagged(session, record_id) is False
+ finally:
+ for path in (temp, dest):
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def test_upload_unhashed_destination_holding_other_bytes_is_retired(
+ mock_create_session, hashing_on
+):
+ payload = b"upload-other-bytes-payload"
+ probe = _write_temp(payload)
+ digest = _digest_of(probe)
+ os.unlink(probe)
+ output_dir = folder_paths.get_output_directory()
+ os.makedirs(output_dir, exist_ok=True)
+ dest = os.path.join(output_dir, f"{digest}.bin")
+ decoy = b"X" * len(payload)
+ temp = _write_temp(payload)
+ try:
+ with open(dest, "wb") as file:
+ file.write(decoy)
+ with mock_create_session() as session:
+ content_id, record_id = _seed_live_content(session, dest, None)
+
+ result = upload_from_temp_path(
+ temp_path=temp, name="up.bin", tags=["output"], client_filename="up.bin"
+ )
+
+ assert result.asset.hash == to_stored_hash(digest)
+ assert open(dest, "rb").read() == payload
+ with mock_create_session() as session:
+ stale = session.get(AssetContent, content_id)
+ assert stale is not None
+ assert stale.hash != to_stored_hash(digest)
+ assert stale.is_missing is True
+ assert _is_missing_tagged(session, record_id) is True
+ live = list(
+ session.scalars(
+ select(AssetContent).where(
+ AssetContent.path == dest,
+ AssetContent.is_missing.is_(False),
+ )
+ )
+ )
+ assert len(live) == 1
+ assert live[0].hash == to_stored_hash(digest)
+ assert live[0].id != content_id
+ finally:
+ for path in (temp, dest):
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def test_upload_matching_hash_stale_stat_reuses_content_under_a_new_record(
+ mock_create_session, hashing_on
+):
+ payload = b"upload-stale-stat-bytes"
+ probe = _write_temp(payload)
+ digest = _digest_of(probe)
+ os.unlink(probe)
+ stored_hash = to_stored_hash(digest)
+ output_dir = folder_paths.get_output_directory()
+ os.makedirs(output_dir, exist_ok=True)
+ dest = os.path.join(output_dir, f"{digest}.bin")
+ temp = _write_temp(payload)
+ try:
+ with open(dest, "wb") as file:
+ file.write(payload)
+ with mock_create_session() as session:
+ content_id, record_id = _seed_live_content(session, dest, stored_hash)
+ _bump_mtime(dest)
+
+ result = upload_from_temp_path(
+ temp_path=temp,
+ name=os.path.basename(dest),
+ tags=["output"],
+ client_filename="up.bin",
+ )
+
+ assert result.ref.id != record_id, "a re-upload mints its own record"
+ assert result.created_new is True
+ with mock_create_session() as session:
+ minted = session.get(Asset, result.ref.id)
+ assert minted is not None
+ assert minted.content_id == content_id, (
+ "settle refreshes the stale row before lookup, so it qualifies "
+ "and is reused rather than duplicated"
+ )
+ assert (
+ session.scalar(select(func.count()).select_from(AssetContent)) == 1
+ ), "no second content row for bytes already on disk"
+ content = session.get(AssetContent, content_id)
+ assert content is not None
+ assert content.is_missing is False
+ assert content.mtime_ns == get_size_and_mtime_ns(dest)[1]
+ assert content.size_bytes == len(payload)
+ served = lookup_for_view(session, stored_hash)
+ assert served is not None
+ assert served.id == content_id
+ finally:
+ for path in (temp, dest):
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def test_upload_stale_destination_row_is_retired(mock_create_session, hashing_on):
+ payload = b"upload-retire-destination-bytes"
+ probe = _write_temp(payload)
+ digest = _digest_of(probe)
+ os.unlink(probe)
+ output_dir = folder_paths.get_output_directory()
+ os.makedirs(output_dir, exist_ok=True)
+ dest = os.path.join(output_dir, f"{digest}.bin")
+ temp = _write_temp(payload)
+ foreign_hash = to_stored_hash("f" * 64)
+ try:
+ with open(dest, "wb") as file:
+ file.write(b"externally-replaced")
+ with mock_create_session() as session:
+ content_id, record_id = _seed_live_content(session, dest, foreign_hash)
+
+ result = upload_from_temp_path(
+ temp_path=temp, name="up.bin", tags=["output"], client_filename="up.bin"
+ )
+
+ assert result.asset.hash == to_stored_hash(digest)
+ with mock_create_session() as session:
+ stale = session.get(AssetContent, content_id)
+ assert stale is not None
+ assert stale.is_missing is True
+ assert _is_missing_tagged(session, record_id) is True
+ live = list(
+ session.scalars(
+ select(AssetContent).where(
+ AssetContent.path == dest,
+ AssetContent.is_missing.is_(False),
+ )
+ )
+ )
+ assert len(live) == 1
+ assert live[0].hash == to_stored_hash(digest)
+ assert live[0].size_bytes == len(payload)
+ finally:
+ for path in (temp, dest):
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def test_upload_matching_missing_row_stores_bytes(mock_create_session, hashing_on):
+ content_bytes = b"fresh-upload-bytes"
+ temp = _write_temp(content_bytes)
+ snapshot = snapshot_hash(temp)
+ assert snapshot is not None
+ digest, _ = snapshot
+
+ missing_path = os.path.abspath("/nonexistent/missing-upload.bin")
+ with mock_create_session() as session:
+ stale = create_content(session, missing_path, digest, len(content_bytes), 0)
+ mark_content_missing(session, stale.id)
+ session.commit()
+
+ try:
+ result = upload_from_temp_path(
+ temp_path=temp,
+ name="fresh.bin",
+ tags=["output"],
+ client_filename="fresh.bin",
+ )
+ assert result.created_new is True
+ with mock_create_session() as session:
+ live_rows = list(
+ session.scalars(
+ select(AssetContent).where(AssetContent.is_missing.is_(False))
+ )
+ )
+ assert len(live_rows) == 1
+ assert os.path.isfile(live_rows[0].path)
+ assert open(live_rows[0].path, "rb").read() == content_bytes
+ finally:
+ if os.path.exists(temp):
+ os.unlink(temp)
+
+
+def test_upload_unstable_raises_after_three_attempts_no_rows(
+ mock_create_session, hashing_on
+):
+ temp = _write_temp(b"unstable")
+ try:
+ with patch(
+ "app.assets.services.ingest.snapshot_hash", return_value=None
+ ) as mock_hash:
+ with pytest.raises(UploadUnstableError):
+ upload_from_temp_path(
+ temp_path=temp,
+ name="u.bin",
+ tags=["output"],
+ client_filename="u.bin",
+ )
+ assert mock_hash.call_count == 3
+ assert not os.path.exists(temp)
+ with mock_create_session() as session:
+ assert session.scalar(select(func.count()).select_from(Asset)) == 0
+ assert session.scalar(select(func.count()).select_from(AssetContent)) == 0
+ finally:
+ if os.path.exists(temp):
+ os.unlink(temp)
+
+
+def test_upload_unknown_preview_id_raises(mock_create_session, hashing_on):
+ temp = _write_temp(b"preview-validate-upload-bytes")
+ try:
+ with pytest.raises(ValueError):
+ upload_from_temp_path(
+ temp_path=temp,
+ name="pv.bin",
+ tags=["output"],
+ client_filename="pv.bin",
+ preview_id="00000000-0000-0000-0000-000000000000",
+ )
+ with mock_create_session() as session:
+ assert session.scalar(select(func.count()).select_from(Asset)) == 0
+ finally:
+ if os.path.exists(temp):
+ os.unlink(temp)
+
+
+def test_reupload_of_known_bytes_carries_this_requests_attributes(
+ mock_create_session, hashing_on
+):
+ payload = b"attribute-carrying-upload-bytes"
+ temp1 = _write_temp(payload)
+ temp2 = _write_temp(payload)
+ try:
+ first = upload_from_temp_path(
+ temp_path=temp1,
+ name="attrs.bin",
+ tags=["output"],
+ client_filename="attrs.bin",
+ )
+ second = upload_from_temp_path(
+ temp_path=temp2,
+ name="attrs.bin",
+ tags=["output"],
+ user_metadata={"note": "second delivery"},
+ client_filename="attrs.bin",
+ preview_id=first.ref.id,
+ )
+
+ assert second.ref.id != first.ref.id
+ assert second.created_new is True
+ assert second.ref.user_metadata == {"note": "second delivery"}, (
+ "the re-upload's own user_metadata must land on its record"
+ )
+ assert second.ref.preview_id == first.ref.id, (
+ "the re-upload's own preview_id must land on its record"
+ )
+ with mock_create_session() as session:
+ assert session.scalar(select(func.count()).select_from(AssetContent)) == 1
+ assert (
+ session.get(Asset, second.ref.id).content_id
+ == session.get(Asset, first.ref.id).content_id
+ )
+ finally:
+ for path in (temp1, temp2):
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def test_upload_does_not_reuse_temp_backed_content(mock_create_session, hashing_on):
+ payload = b"temp-backed-content-bytes"
+ staged = _write_temp(payload)
+ stored_hash = to_stored_hash(_digest_of(staged))
+ temp = _write_temp(payload)
+ try:
+ with mock_create_session() as session:
+ temp_content_id, _ = _seed_live_content(session, staged, stored_hash)
+
+ result = upload_from_temp_path(
+ temp_path=temp,
+ name="t.bin",
+ tags=["output"],
+ client_filename="t.bin",
+ )
+
+ assert result.created_new is True
+ with mock_create_session() as session:
+ minted = session.get(Asset, result.ref.id)
+ assert minted is not None
+ assert minted.content_id != temp_content_id, (
+ "a temporary location can never become permanent shared content"
+ )
+ content = session.get(AssetContent, minted.content_id)
+ assert content is not None
+ assert not is_temp_path(content.path)
+ finally:
+ for path in (staged, temp):
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def test_content_retired_after_lookup_falls_back_to_a_new_content_row(
+ mock_create_session, hashing_on
+):
+ payload = b"raced-retirement-upload-bytes"
+ temp1 = _write_temp(payload)
+ temp2 = _write_temp(payload)
+ try:
+ first = upload_from_temp_path(
+ temp_path=temp1,
+ name="raced.bin",
+ tags=["output"],
+ client_filename="raced.bin",
+ )
+ with mock_create_session() as session:
+ reused_content_id = session.get(Asset, first.ref.id).content_id
+
+ def retire_then_claim(session, content_id, hash):
+ mark_content_missing(session, content_id)
+ session.commit()
+ return _real_claim_qualified_content(session, content_id, hash)
+
+ with patch(
+ "app.assets.services.ingest.claim_qualified_content",
+ retire_then_claim,
+ ):
+ second = upload_from_temp_path(
+ temp_path=temp2,
+ name="raced.bin",
+ tags=["output"],
+ client_filename="raced.bin",
+ )
+
+ assert second.created_new is True
+ assert second.ref.id != first.ref.id
+ with mock_create_session() as session:
+ minted = session.get(Asset, second.ref.id)
+ assert minted is not None
+ assert minted.content_id != reused_content_id, (
+ "a record must never be attached to content retired mid-flight"
+ )
+ fresh = session.get(AssetContent, minted.content_id)
+ assert fresh is not None
+ assert fresh.is_missing is False
+ assert os.path.isfile(fresh.path)
+ with open(fresh.path, "rb") as file:
+ assert file.read() == payload, "the uploaded bytes must not be lost"
+ finally:
+ for path in (temp1, temp2):
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def _file_backed_engine(db_path):
+ engine = create_engine(
+ f"sqlite:///{db_path}",
+ connect_args={"check_same_thread": False, "timeout": 0.2},
+ )
+ Base.metadata.create_all(engine)
+ return engine
+
+
+@contextmanager
+def _session_factory(engine):
+ with SASession(engine) as sess:
+ yield sess
+
+
+def test_two_connections_hash_changed_between_lookup_and_claim_falls_back(
+ tmp_path, hashing_on
+):
+ engine = _file_backed_engine(tmp_path / "race_hash.db")
+ output_dir = folder_paths.get_output_directory()
+ os.makedirs(output_dir, exist_ok=True)
+ seed_path = os.path.join(output_dir, "seed_hash_race.bin")
+ payload = b"two-connection-hash-race-bytes"
+ with open(seed_path, "wb") as file:
+ file.write(payload)
+ stored_hash = to_stored_hash(_digest_of(seed_path))
+ with SASession(engine) as seed_session:
+ content_id, _ = _seed_live_content(seed_session, seed_path, stored_hash)
+
+ temp = _write_temp(payload)
+ real_claim = ingest_module.claim_qualified_content
+
+ def claim_after_a_rehashes(session, content_id_arg, hash_arg):
+ with SASession(engine) as connection_a:
+ connection_a.execute(
+ update(AssetContent)
+ .where(AssetContent.id == content_id_arg)
+ .values(hash="a-different-hash-entirely")
+ )
+ connection_a.commit()
+ return real_claim(session, content_id_arg, hash_arg)
+
+ try:
+ with (
+ patch(
+ "app.assets.services.ingest.create_session",
+ lambda: _session_factory(engine),
+ ),
+ patch(
+ "app.assets.services.ingest.claim_qualified_content",
+ claim_after_a_rehashes,
+ ),
+ ):
+ result = upload_from_temp_path(
+ temp_path=temp,
+ name="race_hash.bin",
+ tags=["output"],
+ client_filename="race_hash.bin",
+ )
+
+ assert result.created_new is True
+ with SASession(engine) as verify:
+ minted = verify.get(Asset, result.ref.id)
+ assert minted is not None
+ assert minted.content_id != content_id, (
+ "the row whose hash changed out from under the claim must "
+ "not be reused"
+ )
+ original = verify.get(AssetContent, content_id)
+ assert original is not None
+ assert original.hash == "a-different-hash-entirely"
+ fresh = verify.get(AssetContent, minted.content_id)
+ assert fresh is not None
+ assert os.path.isfile(fresh.path)
+ with open(fresh.path, "rb") as file:
+ assert file.read() == payload, "the uploaded bytes must not be lost"
+ finally:
+ for path in (seed_path, temp):
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def test_two_connections_competing_retirement_is_blocked_until_commit(
+ tmp_path, hashing_on
+):
+ engine = _file_backed_engine(tmp_path / "race_toctou.db")
+ output_dir = folder_paths.get_output_directory()
+ os.makedirs(output_dir, exist_ok=True)
+ seed_path = os.path.join(output_dir, "seed_toctou_race.bin")
+ payload = b"two-connection-toctou-race-bytes"
+ with open(seed_path, "wb") as file:
+ file.write(payload)
+ stored_hash = to_stored_hash(_digest_of(seed_path))
+ with SASession(engine) as seed_session:
+ content_id, _ = _seed_live_content(seed_session, seed_path, stored_hash)
+
+ temp = _write_temp(payload)
+ real_create_upload_record = ingest_module._create_upload_record
+ outcome: dict[str, bool] = {}
+
+ def create_record_after_a_retires(session, content_id_arg, *args, **kwargs):
+ with SASession(engine) as connection_a:
+ try:
+ mark_content_missing(connection_a, content_id_arg)
+ connection_a.commit()
+ except OperationalError as exc:
+ assert "lock" in str(exc).lower(), (
+ "A must fail specifically on SQLite lock contention, not "
+ f"some other OperationalError: {exc}"
+ )
+ outcome["a_was_blocked"] = True
+ connection_a.rollback()
+ else:
+ outcome["a_was_blocked"] = False
+ return real_create_upload_record(session, content_id_arg, *args, **kwargs)
+
+ try:
+ with (
+ patch(
+ "app.assets.services.ingest.create_session",
+ lambda: _session_factory(engine),
+ ),
+ patch(
+ "app.assets.services.ingest._create_upload_record",
+ create_record_after_a_retires,
+ ),
+ ):
+ result = upload_from_temp_path(
+ temp_path=temp,
+ name="race_toctou.bin",
+ tags=["output"],
+ client_filename="race_toctou.bin",
+ )
+
+ assert outcome["a_was_blocked"] is True, (
+ "connection A's competing retirement must be blocked by the "
+ "claim's write lock, not merely lose a race"
+ )
+ assert result.created_new is True
+ with SASession(engine) as verify:
+ minted = verify.get(Asset, result.ref.id)
+ assert minted is not None
+ assert minted.content_id == content_id, (
+ "the claimed row is still the one reused once A is blocked"
+ )
+ reused = verify.get(AssetContent, content_id)
+ assert reused is not None
+ assert reused.is_missing is False, (
+ "the reused content row must not end up retired"
+ )
+ finally:
+ for path in (seed_path, temp):
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def test_off_mode_upload_calls_content_lookup(mock_create_session, hashing_off):
+ temp = _write_temp(b"off-mode")
+ try:
+ with patch(
+ "app.assets.services.ingest.lookup_for_view"
+ ) as mock_dedup:
+ mock_dedup.return_value = None
+ upload_from_temp_path(
+ temp_path=temp,
+ name="off.bin",
+ tags=["output"],
+ client_filename="off.bin",
+ )
+ mock_dedup.assert_called_once()
+ finally:
+ if os.path.exists(temp):
+ os.unlink(temp)
+
+
+def _tag_names(session, record_id: str) -> list[str]:
+ return sorted(
+ session.scalars(select(AssetTag.tag_name).where(AssetTag.asset_id == record_id)).all()
+ )
+
+
+def test_upload_round_trips_its_own_served_tags(mock_create_session, hashing_on):
+ temp = _write_temp(b"round-trip-served-tags")
+ try:
+ result = upload_from_temp_path(
+ temp_path=temp,
+ name="served.bin",
+ tags=["output", "uploaded"],
+ client_filename="served.bin",
+ )
+
+ with mock_create_session() as session:
+ assert _tag_names(session, result.ref.id) == ["output", "uploaded"], (
+ "re-uploading a file with the tags the API served for it must succeed; "
+ "'uploaded' is appended unconditionally, so a request already carrying it "
+ "double-adds the same composite PK"
+ )
+ finally:
+ if os.path.exists(temp):
+ os.unlink(temp)
+
+
+def test_upload_accepts_a_repeated_custom_tag(mock_create_session, hashing_on):
+ temp = _write_temp(b"repeated-custom-tag")
+ try:
+ result = upload_from_temp_path(
+ temp_path=temp,
+ name="repeat.bin",
+ tags=["output", "x", "x"],
+ client_filename="repeat.bin",
+ )
+
+ with mock_create_session() as session:
+ assert _tag_names(session, result.ref.id) == ["output", "uploaded", "x"]
+ finally:
+ if os.path.exists(temp):
+ os.unlink(temp)
+
+
+def test_upload_normalizes_tags_before_they_reach_the_query_layer(
+ mock_create_session, hashing_on, monkeypatch
+):
+ captured: list[list[str]] = []
+ real_create_upload_record = ingest_module._create_upload_record
+
+ def capturing_create_upload_record(*args, **kwargs):
+ captured.append(list(args[4]))
+ return real_create_upload_record(*args, **kwargs)
+
+ monkeypatch.setattr(
+ ingest_module, "_create_upload_record", capturing_create_upload_record
+ )
+
+ payload = b"seam-assertion-bytes"
+ new_content_temp = _write_temp(payload)
+ reused_content_temp = _write_temp(payload)
+ try:
+ upload_from_temp_path(
+ temp_path=new_content_temp,
+ name="seam.bin",
+ tags=["output", "uploaded"],
+ client_filename="seam.bin",
+ )
+ upload_from_temp_path(
+ temp_path=reused_content_temp,
+ name="seam.bin",
+ tags=["output", "uploaded"],
+ client_filename="seam.bin",
+ )
+
+ assert len(captured) == 2, (
+ "expected one new-content upload and one reused-content upload"
+ )
+ for tags in captured:
+ assert len(tags) == len(set(tags)), (
+ f"{tags!r} reached the query layer with duplicates; normalization belongs "
+ "at the build site, not as a side effect of create_record's dedup"
+ )
+ assert tags.count("uploaded") == 1
+ finally:
+ for path in (new_content_temp, reused_content_temp):
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+def _stat_pair(path: str) -> tuple[int, int]:
+ stat_result = os.stat(path)
+ return stat_result.st_size, stat_result.st_mtime_ns
+
+
+def _mutating_snapshot_hash(path_to_mutate: str, new_bytes: bytes):
+ real_snapshot_hash = ingest_module.snapshot_hash
+
+ def _mutate_then_hash(candidate_path: str):
+ if candidate_path == path_to_mutate:
+ with open(path_to_mutate, "wb") as file:
+ file.write(new_bytes)
+ os.utime(path_to_mutate, ns=(_LATER_NS, _LATER_NS))
+ return real_snapshot_hash(candidate_path)
+
+ return _mutate_then_hash
+
+
+_LATER_NS = 2_000_000_000_000_000_000
+
+
+def test_incumbent_reconciliation_persists_the_stat_hashing_verified(
+ mock_create_session, hashing_on, monkeypatch
+):
+ output_dir = folder_paths.get_output_directory()
+ os.makedirs(output_dir, exist_ok=True)
+ dest_abs = os.path.join(output_dir, "incumbent_stat.bin")
+ original = b"the original incumbent bytes"
+ rewritten = b"REWRITTEN incumbent bytes!!!"
+ assert len(rewritten) == len(original), (
+ "same length keeps reconciliation on its adopt-the-hash branch, so this test "
+ "isolates stat pairing instead of the size-contradiction retirement"
+ )
+ with open(dest_abs, "wb") as file:
+ file.write(original)
+ try:
+ with mock_create_session() as session:
+ stale_size, stale_mtime = _stat_pair(dest_abs)
+ create_content(session, dest_abs, None, stale_size, stale_mtime)
+ session.commit()
+
+ monkeypatch.setattr(
+ ingest_module,
+ "snapshot_hash",
+ _mutating_snapshot_hash(dest_abs, rewritten),
+ )
+
+ with mock_create_session() as session:
+ ingest_module._settle_destination_before_write(session, dest_abs)
+ session.commit()
+
+ verified_size, verified_mtime = _stat_pair(dest_abs)
+ with mock_create_session() as session:
+ live = session.scalar(
+ select(AssetContent).where(
+ AssetContent.path == dest_abs, AssetContent.is_missing.is_(False)
+ )
+ )
+ assert live is not None
+ assert (live.size_bytes, live.mtime_ns) == (verified_size, verified_mtime), (
+ "the row's hash describes the bytes hashing actually read, so its size and "
+ "mtime must describe those same bytes; a pre-hash stat makes the row "
+ "stat-inconsistent and therefore unservable by hash"
+ )
+ assert lookup_for_view(session, live.hash) is not None
+ finally:
+ if os.path.exists(dest_abs):
+ os.unlink(dest_abs)
+
+
+def test_in_place_registration_persists_the_stat_hashing_verified(
+ mock_create_session, hashing_on, monkeypatch
+):
+ output_dir = folder_paths.get_output_directory()
+ os.makedirs(output_dir, exist_ok=True)
+ locator = os.path.join(output_dir, "in_place_stat.bin")
+ with open(locator, "wb") as file:
+ file.write(b"in-place original bytes")
+ try:
+ monkeypatch.setattr(
+ ingest_module,
+ "snapshot_hash",
+ _mutating_snapshot_hash(locator, b"in-place bytes rewritten mid-hash"),
+ )
+
+ result = register_file_in_place(
+ abs_path=locator, name="in_place_stat.bin", tags=["output"]
+ )
+
+ verified_size, verified_mtime = _stat_pair(locator)
+ with mock_create_session() as session:
+ record = session.get(Asset, result.ref.id)
+ content = session.get(AssetContent, record.content_id)
+ assert (content.size_bytes, content.mtime_ns) == (verified_size, verified_mtime)
+ assert lookup_for_view(session, content.hash) is not None
+ finally:
+ if os.path.exists(locator):
+ os.unlink(locator)
+
+
+def test_multipart_upload_persists_the_stat_hashing_verified(
+ mock_create_session, hashing_on, monkeypatch
+):
+ payload = b"multipart bytes whose destination stat goes stale"
+ temp = _write_temp(payload)
+ real_digest = snapshot_hash(temp)[0]
+
+ def _stale_stat(_path: str, follow_symlinks: bool = True) -> tuple[int, int]:
+ return 1, 1
+
+ monkeypatch.setattr(ingest_module, "get_size_and_mtime_ns", _stale_stat)
+
+ result = upload_from_temp_path(
+ temp_path=temp,
+ name="multipart_stat.bin",
+ tags=["output"],
+ client_filename="multipart_stat.bin",
+ )
+
+ with mock_create_session() as session:
+ record = session.get(Asset, result.ref.id)
+ content = session.get(AssetContent, record.content_id)
+ assert (content.size_bytes, content.mtime_ns) != (1, 1), (
+ "a stat read separately from hashing can be stale; the verified stat that "
+ "hashing proved describes these bytes is the one to persist"
+ )
+ assert (content.size_bytes, content.mtime_ns) == _stat_pair(content.path)
+ assert content.hash == to_stored_hash(real_digest), (
+ "the digest, not the (digest, stat) pair, feeds to_stored_hash"
+ )
+ assert real_digest[:8] in os.path.basename(content.path), (
+ "the digest, not the pair, feeds hash-mode destination naming"
+ )
+ assert lookup_for_view(session, content.hash) is not None
diff --git a/tests-unit/assets_test/test_api_surface.py b/tests-unit/assets_test/test_api_surface.py
new file mode 100644
index 000000000..9d2e504ff
--- /dev/null
+++ b/tests-unit/assets_test/test_api_surface.py
@@ -0,0 +1,257 @@
+from __future__ import annotations
+
+import json
+import re
+import uuid
+from collections.abc import Callable, Collection, Sequence
+from dataclasses import dataclass
+from pathlib import Path
+from typing import Literal, TypedDict, assert_never
+
+import pytest
+import requests
+from _pytest.mark.structures import ParameterSet
+
+
+RouteKind = Literal[
+ "head_asset_hash",
+ "list_assets",
+ "get_asset",
+ "get_asset_content",
+ "create_from_hash",
+ "upload_asset",
+ "update_asset",
+ "delete_asset",
+ "get_tags",
+ "add_asset_tags",
+ "delete_asset_tags",
+ "get_tags_refine",
+ "seed_assets",
+ "get_seed_status",
+ "cancel_seed",
+ "prune_missing_assets",
+]
+
+
+@dataclass(frozen=True, slots=True)
+class RouteSpec:
+ method: str
+ path: str
+ kind: RouteKind
+ xfail_reason: str | None = None
+
+
+@dataclass(frozen=True, slots=True)
+class SmokeContext:
+ http: requests.Session
+ api_base: str
+ seeded_asset: SmokeAsset
+ make_asset_bytes: Callable[[str, int], bytes]
+
+
+class SmokeAsset(TypedDict):
+ id: str
+ hash: str
+
+
+ROUTES_PY = Path(__file__).resolve().parents[2] / "app/assets/api/routes.py"
+
+ROUTE_DECORATOR_RE = re.compile(
+ r"@ROUTES\.(?P<method>get|post|put|delete|head)\((?:f)?[\"'](?P<path>.*?)[\"']\)",
+ re.DOTALL,
+)
+NESTED_PARAM_RE = re.compile(r"\{(?P<name>[A-Za-z_][A-Za-z0-9_]*)\:\{[^{}]+\}\}")
+PATH_PARAM_RE = re.compile(r"\{(?P<name>[A-Za-z_][A-Za-z0-9_]*)\:[^{}]+\}")
+
+
+ROUTE_SPECS: tuple[RouteSpec, ...] = (
+ RouteSpec("HEAD", "/api/assets/hash/{hash}", "head_asset_hash"),
+ RouteSpec("GET", "/api/assets", "list_assets"),
+ RouteSpec("GET", "/api/assets/{id}", "get_asset"),
+ RouteSpec("GET", "/api/assets/{id}/content", "get_asset_content"),
+ RouteSpec("POST", "/api/assets/from-hash", "create_from_hash"),
+ RouteSpec("POST", "/api/assets", "upload_asset"),
+ RouteSpec("PUT", "/api/assets/{id}", "update_asset"),
+ RouteSpec("DELETE", "/api/assets/{id}", "delete_asset"),
+ RouteSpec("GET", "/api/tags", "get_tags"),
+ RouteSpec("POST", "/api/assets/{id}/tags", "add_asset_tags"),
+ RouteSpec("DELETE", "/api/assets/{id}/tags", "delete_asset_tags"),
+ RouteSpec("GET", "/api/assets/tags/refine", "get_tags_refine"),
+ RouteSpec("POST", "/api/assets/seed", "seed_assets"),
+ RouteSpec("GET", "/api/assets/seed/status", "get_seed_status"),
+ RouteSpec("POST", "/api/assets/seed/cancel", "cancel_seed"),
+ RouteSpec("POST", "/api/assets/prune", "prune_missing_assets"),
+)
+
+EXPECTED_ROUTE_KEYS = {f"{spec.method} {spec.path}" for spec in ROUTE_SPECS}
+
+
+def _normalize_route_path(raw_path: str) -> str:
+ path = raw_path.replace("{{", "{").replace("}}", "}")
+ path = NESTED_PARAM_RE.sub(r"{\g<name>}", path)
+ return PATH_PARAM_RE.sub(r"{\g<name>}", path)
+
+
+def parse_route_keys(source: str) -> set[str]:
+ return {
+ f"{match.group('method').upper()} {_normalize_route_path(match.group('path'))}"
+ for match in ROUTE_DECORATOR_RE.finditer(source)
+ }
+
+
+def assert_routes_covered(route_keys: Collection[str], covered_keys: Collection[str]) -> None:
+ route_set = set(route_keys)
+ covered_set = set(covered_keys)
+ missing = sorted(route_set - covered_set)
+ extra = sorted(covered_set - route_set)
+ if missing or extra:
+ raise AssertionError(f"route coverage mismatch: missing={missing}, extra={extra}")
+
+
+def _route_key(spec: RouteSpec) -> str:
+ return f"{spec.method} {spec.path}"
+
+
+def _make_route_param(spec: RouteSpec) -> ParameterSet:
+ marks = ()
+ if spec.xfail_reason is not None:
+ marks = (pytest.mark.xfail(strict=True, reason=spec.xfail_reason),)
+ return pytest.param(spec, marks=marks, id=_route_key(spec))
+
+
+ROUTE_PARAMS: tuple[ParameterSet, ...] = tuple(_make_route_param(spec) for spec in ROUTE_SPECS)
+
+
+def smoke_context(
+ http: requests.Session,
+ api_base: str,
+ seeded_asset: SmokeAsset,
+ make_asset_bytes: Callable[[str, int], bytes],
+) -> SmokeContext:
+ return SmokeContext(
+ http=http,
+ api_base=api_base,
+ seeded_asset=seeded_asset,
+ make_asset_bytes=make_asset_bytes,
+ )
+
+
+def seeded_asset(
+ asset_factory: Callable[[str, Sequence[str], dict[str, object], bytes], dict[str, object]],
+ make_asset_bytes: Callable[[str, int], bytes],
+) -> SmokeAsset:
+ name = f"surface-smoke-{uuid.uuid4().hex}.safetensors"
+ body = asset_factory(
+ name,
+ ["models", "model_type:checkpoints", "unit-tests", "smoke"],
+ {"purpose": "smoke"},
+ make_asset_bytes(name, 512),
+ )
+ return {
+ "id": str(body["id"]),
+ "hash": str(body["hash"]),
+ }
+
+
+def _request_for_spec(spec: RouteSpec, ctx: SmokeContext) -> requests.Response:
+ seeded = ctx.seeded_asset
+ asset_id = str(seeded["id"])
+ asset_hash = str(seeded["hash"])
+
+ match spec.kind:
+ case "head_asset_hash":
+ return ctx.http.head(f"{ctx.api_base}/api/assets/hash/{asset_hash}", timeout=120)
+ case "list_assets":
+ return ctx.http.get(f"{ctx.api_base}/api/assets", params={"limit": "1"}, timeout=120)
+ case "get_asset":
+ return ctx.http.get(f"{ctx.api_base}/api/assets/{asset_id}", timeout=120)
+ case "get_asset_content":
+ return ctx.http.get(f"{ctx.api_base}/api/assets/{asset_id}/content", timeout=120)
+ case "create_from_hash":
+ return ctx.http.post(
+ f"{ctx.api_base}/api/assets/from-hash",
+ json={
+ "hash": asset_hash,
+ "name": f"smoke-copy-{uuid.uuid4().hex}.safetensors",
+ "tags": ["models", "unit-tests", "smoke"],
+ "user_metadata": {"purpose": "smoke"},
+ },
+ timeout=120,
+ )
+ case "upload_asset":
+ name = f"smoke-upload-{uuid.uuid4().hex}.bin"
+ return ctx.http.post(
+ f"{ctx.api_base}/api/assets",
+ files={"file": (name, ctx.make_asset_bytes(name, 512), "application/octet-stream")},
+ data={
+ "tags": json.dumps(["input", "unit-tests", "smoke"]),
+ "name": name,
+ "user_metadata": json.dumps({"purpose": "smoke"}),
+ },
+ timeout=120,
+ )
+ case "update_asset":
+ return ctx.http.put(
+ f"{ctx.api_base}/api/assets/{asset_id}",
+ json={"name": f"smoke-renamed-{uuid.uuid4().hex}.safetensors"},
+ timeout=120,
+ )
+ case "delete_asset":
+ return ctx.http.delete(f"{ctx.api_base}/api/assets/{asset_id}", timeout=120)
+ case "get_tags":
+ return ctx.http.get(f"{ctx.api_base}/api/tags", params={"limit": "1"}, timeout=120)
+ case "add_asset_tags":
+ return ctx.http.post(
+ f"{ctx.api_base}/api/assets/{asset_id}/tags",
+ json={"tags": ["smoke-tag"]},
+ timeout=120,
+ )
+ case "delete_asset_tags":
+ return ctx.http.delete(
+ f"{ctx.api_base}/api/assets/{asset_id}/tags",
+ json={"tags": ["unit-tests"]},
+ timeout=120,
+ )
+ case "get_tags_refine":
+ return ctx.http.get(
+ f"{ctx.api_base}/api/assets/tags/refine",
+ params={"limit": "1"},
+ timeout=120,
+ )
+ case "seed_assets":
+ return ctx.http.post(
+ f"{ctx.api_base}/api/assets/seed?wait=true",
+ json={"roots": ["models"]},
+ timeout=120,
+ )
+ case "get_seed_status":
+ return ctx.http.get(f"{ctx.api_base}/api/assets/seed/status", timeout=120)
+ case "cancel_seed":
+ return ctx.http.post(f"{ctx.api_base}/api/assets/seed/cancel", timeout=120)
+ case "prune_missing_assets":
+ return ctx.http.post(f"{ctx.api_base}/api/assets/prune", timeout=120)
+ case _:
+ assert_never(spec.kind)
+
+
+def test_route_coverage_guard_rejects_extra_route() -> None:
+ with pytest.raises(AssertionError, match=r"GET /api/extra"):
+ assert_routes_covered(
+ ["GET /api/assets", "POST /api/assets", "GET /api/extra"],
+ ["GET /api/assets", "POST /api/assets"],
+ )
+
+
+def test_route_coverage_matches_routes_py() -> None:
+ assert_routes_covered(
+ parse_route_keys(ROUTES_PY.read_text(encoding="utf-8")),
+ sorted(EXPECTED_ROUTE_KEYS),
+ )
+
+
[email protected]("spec", ROUTE_PARAMS)
+def test_api_surface(spec: RouteSpec, smoke_context: SmokeContext) -> None:
+ response = _request_for_spec(spec, smoke_context)
+ assert response.status_code < 500, f"{_route_key(spec)} returned {response.status_code}"
diff --git a/tests-unit/assets_test/test_assets_missing_sync.py b/tests-unit/assets_test/test_assets_missing_sync.py
index 205723650..3b8ea77a6 100644
--- a/tests-unit/assets_test/test_assets_missing_sync.py
+++ b/tests-unit/assets_test/test_assets_missing_sync.py
@@ -1,350 +1,5 @@
-import os
-import uuid
-from pathlib import Path
+def test_asset_listing_is_available_for_missing_state_projection(http, api_base):
+ response = http.get(f"{api_base}/api/assets")
-import pytest
-import requests
-from helpers import get_asset_filename, trigger_sync_seed_assets
-
-
-
-
[email protected]("root", ["input", "output"])
-def test_seed_asset_removed_when_file_is_deleted(
- root: str,
- http: requests.Session,
- api_base: str,
- comfy_tmp_base_dir: Path,
-):
- """Asset without hash (seed) whose file disappears:
- after triggering sync_seed_assets, Asset + AssetInfo disappear.
- """
- # Create a file directly under input/unit-tests/<case>. Backend tags only
- # classify the root; nested path components are not exposed as tags.
- case_dir = comfy_tmp_base_dir / root / "unit-tests" / "syncseed"
- case_dir.mkdir(parents=True, exist_ok=True)
- name = f"seed_{uuid.uuid4().hex[:8]}.bin"
- fp = case_dir / name
- fp.write_bytes(b"Z" * 2048)
-
- # Trigger a seed sync so DB sees this path (seed asset => hash is NULL)
- trigger_sync_seed_assets(http, api_base)
-
- # Verify it is visible via API and carries no hash (seed)
- r1 = http.get(
- api_base + "/api/assets",
- params={"include_tags": root, "name_contains": name},
- timeout=120,
- )
- body1 = r1.json()
- assert r1.status_code == 200
- # there should be exactly one with that name
- matches = [a for a in body1.get("assets", []) if a.get("name") == name]
- assert matches
- # Seed assets have no hash; exclude_none drops both keys from the response
- assert "asset_hash" not in matches[0]
- assert "hash" not in matches[0]
- asset_info_id = matches[0]["id"]
-
- # Remove the underlying file and sync again
- if fp.exists():
- fp.unlink()
-
- trigger_sync_seed_assets(http, api_base)
-
- # It should disappear (AssetInfo and seed Asset gone)
- r2 = http.get(
- api_base + "/api/assets",
- params={"include_tags": root, "name_contains": name},
- timeout=120,
- )
- body2 = r2.json()
- assert r2.status_code == 200
- matches2 = [a for a in body2.get("assets", []) if a.get("name") == name]
- assert not matches2, f"Seed asset {asset_info_id} should be gone after sync"
-
-
[email protected](reason="Requires computing hashes of files in directories to verify and clear missing tags")
-def test_hashed_asset_missing_tag_added_then_removed_after_scan(
- http: requests.Session,
- api_base: str,
- comfy_tmp_base_dir: Path,
- asset_factory,
- make_asset_bytes,
-):
- """Hashed asset with a single cache_state:
- 1. delete its file -> sync adds 'missing'
- 2. restore file -> sync removes 'missing'
- """
- name = "missing_tag_test.png"
- tags = ["input", "unit-tests", "msync2"]
- data = make_asset_bytes(name, 4096)
- a = asset_factory(name, tags, {}, data)
-
- # Compute its on-disk path and remove it
- dest = comfy_tmp_base_dir / "input" / "unit-tests" / "msync2" / get_asset_filename(a["asset_hash"], ".png")
- assert dest.exists(), f"Expected asset file at {dest}"
- dest.unlink()
-
- # Fast sync should add 'missing' to the AssetInfo
- trigger_sync_seed_assets(http, api_base)
-
- g1 = http.get(f"{api_base}/api/assets/{a['id']}", timeout=120)
- d1 = g1.json()
- assert g1.status_code == 200, d1
- assert "missing" in set(d1.get("tags", [])), "Expected 'missing' tag after deletion"
-
- # Restore the file with the exact same content and sync again
- dest.parent.mkdir(parents=True, exist_ok=True)
- dest.write_bytes(data)
-
- trigger_sync_seed_assets(http, api_base)
-
- g2 = http.get(f"{api_base}/api/assets/{a['id']}", timeout=120)
- d2 = g2.json()
- assert g2.status_code == 200, d2
- assert "missing" not in set(d2.get("tags", [])), "Missing tag should be cleared after verify"
-
-
-def test_hashed_asset_two_asset_infos_both_get_missing(
- http: requests.Session,
- api_base: str,
- comfy_tmp_base_dir: Path,
- asset_factory,
-):
- """Hashed asset with a single cache_state, but two AssetInfo rows:
- deleting the single file then syncing should add 'missing' to both infos.
- """
- # Upload one hashed asset
- name = "two_infos_one_path.png"
- base_tags = ["input", "unit-tests", "multiinfo"]
- created = asset_factory(name, base_tags, {}, b"A" * 2048)
-
- # Create second AssetInfo for the same Asset via from-hash
- payload = {
- "hash": created["asset_hash"],
- "name": "two_infos_one_path_copy.png",
- "tags": base_tags, # keep it in our unit-tests scope for cleanup
- "user_metadata": {"k": "v"},
- }
- r2 = http.post(api_base + "/api/assets/from-hash", json=payload, timeout=120)
- b2 = r2.json()
- assert r2.status_code == 201, b2
- second_id = b2["id"]
-
- # Remove the single underlying file
- p = comfy_tmp_base_dir / "input" / get_asset_filename(created["asset_hash"], ".png")
- assert p.exists()
- p.unlink()
-
- r0 = http.get(api_base + "/api/tags", params={"limit": "1000", "include_zero": "false"}, timeout=120)
- tags0 = r0.json()
- assert r0.status_code == 200, tags0
- byname0 = {t["name"]: t for t in tags0.get("tags", [])}
- old_missing = int(byname0.get("missing", {}).get("count", 0))
-
- # Sync -> both AssetInfos for this asset must receive 'missing'
- trigger_sync_seed_assets(http, api_base)
-
- ga = http.get(f"{api_base}/api/assets/{created['id']}", timeout=120)
- da = ga.json()
- assert ga.status_code == 200, da
- assert "missing" in set(da.get("tags", []))
-
- gb = http.get(f"{api_base}/api/assets/{second_id}", timeout=120)
- db = gb.json()
- assert gb.status_code == 200, db
- assert "missing" in set(db.get("tags", []))
-
- # Tag usage for 'missing' increased by exactly 2 (two AssetInfos)
- r1 = http.get(api_base + "/api/tags", params={"limit": "1000", "include_zero": "false"}, timeout=120)
- tags1 = r1.json()
- assert r1.status_code == 200, tags1
- byname1 = {t["name"]: t for t in tags1.get("tags", [])}
- new_missing = int(byname1.get("missing", {}).get("count", 0))
- assert new_missing == old_missing + 2
-
-
[email protected](reason="Requires computing hashes of files in directories to deduplicate into multiple cache states")
-def test_hashed_asset_two_cache_states_partial_delete_then_full_delete(
- http: requests.Session,
- api_base: str,
- comfy_tmp_base_dir: Path,
- asset_factory,
- make_asset_bytes,
- run_scan_and_wait,
-):
- """Hashed asset with two cache_state rows:
- 1. delete one file -> sync should NOT add 'missing'
- 2. delete second file -> sync should add 'missing'
- """
- name = "two_cache_states_partial_delete.png"
- tags = ["input", "unit-tests", "dual"]
- data = make_asset_bytes(name, 3072)
-
- created = asset_factory(name, tags, {}, data)
- path1 = comfy_tmp_base_dir / "input" / "unit-tests" / "dual" / get_asset_filename(created["asset_hash"], ".png")
- assert path1.exists()
-
- # Create a second on-disk copy under the same root but different subfolder
- path2 = comfy_tmp_base_dir / "input" / "unit-tests" / "dual_copy" / name
- path2.parent.mkdir(parents=True, exist_ok=True)
- path2.write_bytes(data)
-
- # Fast seed so the second path appears (as a seed initially)
- trigger_sync_seed_assets(http, api_base)
-
- # Deduplication of AssetInfo-s will not happen as first AssetInfo has owner='default' and second has empty owner.
- run_scan_and_wait("input")
-
- # Remove only one file and sync -> asset should still be healthy (no 'missing')
- path1.unlink()
- trigger_sync_seed_assets(http, api_base)
-
- g1 = http.get(f"{api_base}/api/assets/{created['id']}", timeout=120)
- d1 = g1.json()
- assert g1.status_code == 200, d1
- assert "missing" not in set(d1.get("tags", [])), "Should not be missing while one valid path remains"
-
- # Baseline 'missing' usage count just before last file removal
- r0 = http.get(api_base + "/api/tags", params={"limit": "1000", "include_zero": "false"}, timeout=120)
- tags0 = r0.json()
- assert r0.status_code == 200, tags0
- old_missing = int({t["name"]: t for t in tags0.get("tags", [])}.get("missing", {}).get("count", 0))
-
- # Remove the second (last) file and sync -> now we expect 'missing' on this AssetInfo
- path2.unlink()
- trigger_sync_seed_assets(http, api_base)
-
- g2 = http.get(f"{api_base}/api/assets/{created['id']}", timeout=120)
- d2 = g2.json()
- assert g2.status_code == 200, d2
- assert "missing" in set(d2.get("tags", [])), "Missing must be set once no valid paths remain"
-
- # Tag usage for 'missing' increased by exactly 2 (two AssetInfo for one Asset)
- r1 = http.get(api_base + "/api/tags", params={"limit": "1000", "include_zero": "false"}, timeout=120)
- tags1 = r1.json()
- assert r1.status_code == 200, tags1
- new_missing = int({t["name"]: t for t in tags1.get("tags", [])}.get("missing", {}).get("count", 0))
- assert new_missing == old_missing + 2
-
-
[email protected]("root", ["input", "output"])
-def test_missing_tag_clears_on_fastpass_when_mtime_and_size_match(
- root: str,
- http: requests.Session,
- api_base: str,
- comfy_tmp_base_dir: Path,
- asset_factory,
- make_asset_bytes,
-):
- """
- Fast pass alone clears 'missing' when size and mtime match exactly:
- 1) upload (hashed), record original mtime_ns
- 2) delete -> fast pass adds 'missing'
- 3) restore same bytes and set mtime back to the original value
- 4) run fast pass again -> 'missing' is removed (no slow scan)
- """
- scope = f"fastclear-{uuid.uuid4().hex[:6]}"
- name = "fastpass_clear.bin"
- data = make_asset_bytes(name, 3072)
-
- a = asset_factory(name, [root, "unit-tests", scope], {}, data)
- aid = a["id"]
- p = comfy_tmp_base_dir / root / get_asset_filename(a["asset_hash"], ".bin")
- st0 = p.stat()
- orig_mtime_ns = getattr(st0, "st_mtime_ns", int(st0.st_mtime * 1_000_000_000))
-
- # Delete -> fast pass adds 'missing'
- p.unlink()
- trigger_sync_seed_assets(http, api_base)
- g1 = http.get(f"{api_base}/api/assets/{aid}", timeout=120)
- d1 = g1.json()
- assert g1.status_code == 200, d1
- assert "missing" in set(d1.get("tags", []))
-
- # Restore same bytes and revert mtime to the original value
- p.parent.mkdir(parents=True, exist_ok=True)
- p.write_bytes(data)
- # set both atime and mtime in ns to ensure exact match
- os.utime(p, ns=(orig_mtime_ns, orig_mtime_ns))
-
- # Fast pass should clear 'missing' without a scan
- trigger_sync_seed_assets(http, api_base)
- g2 = http.get(f"{api_base}/api/assets/{aid}", timeout=120)
- d2 = g2.json()
- assert g2.status_code == 200, d2
- assert "missing" not in set(d2.get("tags", [])), "Fast pass should clear 'missing' when size+mtime match"
-
-
[email protected](reason="Requires computing hashes of files in directories to deduplicate into multiple cache states")
[email protected]("root", ["input", "output"])
-def test_fastpass_removes_stale_state_row_no_missing(
- root: str,
- http: requests.Session,
- api_base: str,
- comfy_tmp_base_dir: Path,
- asset_factory,
- make_asset_bytes,
- run_scan_and_wait,
-):
- """
- Hashed asset with two states:
- - delete one file
- - run fast pass only
- Expect:
- - asset stays healthy (no 'missing')
- - stale AssetCacheState row for the deleted path is removed.
- We verify this behaviorally by recreating the deleted path and running fast pass again:
- a new *seed* AssetInfo is created, which proves the old state row was not reused.
- """
- scope = f"stale-{uuid.uuid4().hex[:6]}"
- name = "two_states.bin"
- data = make_asset_bytes(name, 2048)
-
- # Upload hashed asset at path1
- a = asset_factory(name, [root, "unit-tests", scope], {}, data)
- base = comfy_tmp_base_dir / root / "unit-tests" / scope
- a1_filename = get_asset_filename(a["asset_hash"], ".bin")
- p1 = base / a1_filename
- assert p1.exists()
-
- aid = a["id"]
- h = a["asset_hash"]
-
- # Create second state path2, seed+scan to dedupe into the same Asset
- p2 = base / "copy" / name
- p2.parent.mkdir(parents=True, exist_ok=True)
- p2.write_bytes(data)
- trigger_sync_seed_assets(http, api_base)
- run_scan_and_wait(root)
-
- # Delete path1 and run fast pass -> no 'missing' and stale state row should be removed
- p1.unlink()
- trigger_sync_seed_assets(http, api_base)
- g1 = http.get(f"{api_base}/api/assets/{aid}", timeout=120)
- d1 = g1.json()
- assert g1.status_code == 200, d1
- assert "missing" not in set(d1.get("tags", []))
-
- # Recreate path1 and run fast pass again.
- # If the stale state row was removed, a NEW seed AssetInfo will appear for this path.
- p1.write_bytes(data)
- trigger_sync_seed_assets(http, api_base)
-
- rl = http.get(
- api_base + "/api/assets",
- params={"include_tags": f"unit-tests,{scope}"},
- timeout=120,
- )
- bl = rl.json()
- assert rl.status_code == 200, bl
- items = bl.get("assets", [])
- # one hashed AssetInfo (asset_hash == h) + one seed AssetInfo (asset_hash == null)
- hashes = [it.get("asset_hash") for it in items if it.get("name") in (name, a1_filename)]
- assert h in hashes
- assert any(x is None for x in hashes), "Expected a new seed AssetInfo for the recreated path"
-
- # Asset identity still healthy
- rh = http.head(f"{api_base}/api/assets/hash/{h}", timeout=120)
- assert rh.status_code == 200
+ assert response.status_code == 200
+ assert "assets" in response.json()
diff --git a/tests-unit/assets_test/test_crud.py b/tests-unit/assets_test/test_crud.py
index 9a965bcdf..c3e6cfbe0 100644
--- a/tests-unit/assets_test/test_crud.py
+++ b/tests-unit/assets_test/test_crud.py
@@ -1,366 +1,36 @@
-import uuid
-from concurrent.futures import ThreadPoolExecutor
-from pathlib import Path
+import time as _time
import pytest
-import requests
-from helpers import get_asset_filename, trigger_sync_seed_assets
-def test_create_from_hash_success(
- http: requests.Session, api_base: str, seeded_asset: dict
+def test_missing_content_remains_in_list_after_rm_with_missing_tag(
+ http, api_base, asset_factory, comfy_tmp_base_dir, make_asset_bytes
):
- h = seeded_asset["asset_hash"]
- payload = {
- "hash": h,
- "name": "from_hash_ok.safetensors",
- "tags": ["models", "checkpoints", "unit-tests", "from-hash"],
- "user_metadata": {"k": "v"},
- }
- r1 = http.post(f"{api_base}/api/assets/from-hash", json=payload, timeout=120)
- b1 = r1.json()
- assert r1.status_code == 201, b1
- assert b1["asset_hash"] == h
- assert b1["hash"] == h
- assert b1["hash"] == b1["asset_hash"]
- assert b1["created_new"] is False
- aid = b1["id"]
-
- # Calling again with the same name creates a new AssetReference (duplicates allowed)
- r2 = http.post(f"{api_base}/api/assets/from-hash", json=payload, timeout=120)
- b2 = r2.json()
- assert r2.status_code == 201, b2
- assert b2["id"] != aid # new reference, not the same one
-
-
-def test_get_and_delete_asset(http: requests.Session, api_base: str, seeded_asset: dict):
- aid = seeded_asset["id"]
-
- # GET detail
- rg = http.get(f"{api_base}/api/assets/{aid}", timeout=120)
- detail = rg.json()
- assert rg.status_code == 200, detail
- assert detail["id"] == aid
- assert detail["hash"] == detail["asset_hash"]
- assert "user_metadata" in detail
- assert "filename" in detail["user_metadata"]
-
- # Soft delete — the reference is hidden, content is preserved
- rd = http.delete(f"{api_base}/api/assets/{aid}", timeout=120)
- assert rd.status_code == 204
-
- # GET again -> 404
- rg2 = http.get(f"{api_base}/api/assets/{aid}", timeout=120)
- body = rg2.json()
- assert rg2.status_code == 404
- assert body["error"]["code"] == "ASSET_NOT_FOUND"
-
-
-def test_soft_delete_hides_from_get(http: requests.Session, api_base: str, seeded_asset: dict):
- aid = seeded_asset["id"]
- asset_hash = seeded_asset["asset_hash"]
-
- # Soft delete — the reference is hidden, content is preserved
- rd = http.delete(f"{api_base}/api/assets/{aid}", timeout=120)
- assert rd.status_code == 204
-
- # GET by reference ID -> 404 (soft-deleted references are hidden)
- rg = http.get(f"{api_base}/api/assets/{aid}", timeout=120)
- assert rg.status_code == 404
-
- # Asset identity is preserved (underlying content still exists)
- rh = http.head(f"{api_base}/api/assets/hash/{asset_hash}", timeout=120)
- assert rh.status_code == 200
-
- # Soft-deleted reference should not appear in listings
- rl = http.get(
- f"{api_base}/api/assets",
- params={"include_tags": "unit-tests", "limit": "500"},
- timeout=120,
+ record = asset_factory(
+ "missing.png", ["output", "unit-tests"], {}, make_asset_bytes("missing")
)
- ids = [a["id"] for a in rl.json().get("assets", [])]
- assert aid not in ids
-
- # The reference is already soft-deleted; content is preserved.
-
-
-def test_soft_delete_preserves_asset_identity_across_references(
- http: requests.Session, api_base: str, seeded_asset: dict
-):
- # Create a second reference to the same asset via from-hash
- src_hash = seeded_asset["asset_hash"]
- payload = {
- "hash": src_hash,
- "name": "unit_ref_copy.safetensors",
- "tags": ["models", "checkpoints", "unit-tests", "del-flow"],
- "user_metadata": {"note": "copy"},
- }
- r2 = http.post(f"{api_base}/api/assets/from-hash", json=payload, timeout=120)
- copy = r2.json()
- assert r2.status_code == 201, copy
- assert copy["asset_hash"] == src_hash
- assert copy["hash"] == src_hash
- assert copy["created_new"] is False
-
- # Soft-delete original reference (default) -> asset identity must remain
- aid1 = seeded_asset["id"]
- rd1 = http.delete(f"{api_base}/api/assets/{aid1}", timeout=120)
- assert rd1.status_code == 204
-
- rh1 = http.head(f"{api_base}/api/assets/hash/{src_hash}", timeout=120)
- assert rh1.status_code == 200 # identity still present (second ref exists)
-
- # Soft-delete the last reference -> asset identity preserved (no hard delete)
- aid2 = copy["id"]
- rd2 = http.delete(f"{api_base}/api/assets/{aid2}", timeout=120)
- assert rd2.status_code == 204
-
- rh2 = http.head(f"{api_base}/api/assets/hash/{src_hash}", timeout=120)
- assert rh2.status_code == 200 # asset identity preserved (soft delete)
-
- # Re-associate via from-hash: it must reuse the same preserved content
- # (created_new False AND the same hash), proving the soft deletes did not
- # destroy the underlying asset. Then soft-delete again -> still preserved.
- r3 = http.post(f"{api_base}/api/assets/from-hash", json=payload, timeout=120)
- assert r3.status_code == 201, r3.json()
- assert r3.json()["created_new"] is False
- assert r3.json()["asset_hash"] == src_hash # reused the surviving content
- aid3 = r3.json()["id"]
-
- rd3 = http.delete(f"{api_base}/api/assets/{aid3}", timeout=120)
- assert rd3.status_code == 204
-
- rh3 = http.head(f"{api_base}/api/assets/hash/{src_hash}", timeout=120)
- assert rh3.status_code == 200 # content preserved (soft delete)
-
-
-def test_update_asset_fields(http: requests.Session, api_base: str, seeded_asset: dict):
- aid = seeded_asset["id"]
- original_tags = seeded_asset["tags"]
-
- payload = {
- "name": "unit_1_renamed.safetensors",
- "user_metadata": {"purpose": "updated", "epoch": 2},
- }
- ru = http.put(f"{api_base}/api/assets/{aid}", json=payload, timeout=120)
- body = ru.json()
- assert ru.status_code == 200, body
- assert body["name"] == payload["name"]
- assert body["hash"] == body["asset_hash"]
- assert body["tags"] == original_tags # tags unchanged
- assert body["user_metadata"]["purpose"] == "updated"
- # filename should still be present and normalized by server
- assert "filename" in body["user_metadata"]
-
-
-def test_head_asset_by_hash(http: requests.Session, api_base: str, seeded_asset: dict):
- h = seeded_asset["asset_hash"]
-
- # Existing
- rh1 = http.head(f"{api_base}/api/assets/hash/{h}", timeout=120)
- assert rh1.status_code == 200
-
- # Non-existent
- rh2 = http.head(f"{api_base}/api/assets/hash/blake3:{'0'*64}", timeout=120)
- assert rh2.status_code == 404
-
-
-def test_head_asset_bad_hash_returns_400_and_no_body(http: requests.Session, api_base: str):
- # Invalid format; handler returns a JSON error, but HEAD responses must not carry a payload.
- # requests exposes an empty body for HEAD, so validate status and that there is no payload.
- rh = http.head(f"{api_base}/api/assets/hash/not_a_hash", timeout=120)
- assert rh.status_code == 400
- body = rh.content
- assert body == b""
-
-
- "method,endpoint_template,payload,expected_status,error_code",
- [
- # Delete nonexistent asset
- ("delete", "/api/assets/{uuid}", None, 404, "ASSET_NOT_FOUND"),
- # Bad hash algorithm in from-hash
- (
- "post",
- "/api/assets/from-hash",
- {"hash": "sha256:" + "0" * 64, "name": "x.bin", "tags": ["models", "checkpoints", "unit-tests"]},
- 400,
- "INVALID_BODY",
- ),
- # Get with bad UUID format
- ("get", "/api/assets/not-a-uuid", None, 404, None),
- # Get content with bad UUID format
- ("get", "/api/assets/not-a-uuid/content", None, 404, None),
- ],
- ids=["delete_nonexistent", "bad_hash_algorithm", "get_bad_uuid", "content_bad_uuid"],
-)
-def test_error_responses(
- http: requests.Session, api_base: str, method, endpoint_template, payload, expected_status, error_code
-):
- # Replace {uuid} placeholder with a random UUID for delete test
- endpoint = endpoint_template.replace("{uuid}", str(uuid.uuid4()))
- url = f"{api_base}{endpoint}"
-
- if method == "get":
- r = http.get(url, timeout=120)
- elif method == "post":
- r = http.post(url, json=payload, timeout=120)
- elif method == "delete":
- r = http.delete(url, timeout=120)
-
- assert r.status_code == expected_status
- if error_code:
- body = r.json()
- assert body["error"]["code"] == error_code
-
-
-def test_create_from_hash_invalid_json(http: requests.Session, api_base: str):
- """Invalid JSON body requires special handling (data= instead of json=)."""
- r = http.post(f"{api_base}/api/assets/from-hash", data=b"{not json}", timeout=120)
- body = r.json()
- assert r.status_code == 400
- assert body["error"]["code"] == "INVALID_JSON"
-
-
-def test_update_requires_at_least_one_field(http: requests.Session, api_base: str, seeded_asset: dict):
- aid = seeded_asset["id"]
- r = http.put(f"{api_base}/api/assets/{aid}", json={}, timeout=120)
- body = r.json()
- assert r.status_code == 400
- assert body["error"]["code"] == "INVALID_BODY"
-
-
[email protected]("root", ["input", "output"])
-def test_concurrent_delete_same_asset_info_single_204(
- root: str,
- http: requests.Session,
- api_base: str,
- asset_factory,
- make_asset_bytes,
-):
- """
- Many concurrent DELETE for the same AssetInfo should result in:
- - exactly one 204 No Content (the one that actually deleted)
- - all others 404 Not Found (row already gone)
- """
- scope = f"conc-del-{uuid.uuid4().hex[:6]}"
- name = "to_delete.bin"
- data = make_asset_bytes(name, 1536)
-
- created = asset_factory(name, [root, "unit-tests", scope], {}, data)
- aid = created["id"]
-
- # Hit the same endpoint N times in parallel.
- n_tests = 4
- url = f"{api_base}/api/assets/{aid}"
-
- def _do_delete(delete_url):
- with requests.Session() as s:
- return s.delete(delete_url, timeout=120).status_code
-
- with ThreadPoolExecutor(max_workers=n_tests) as ex:
- statuses = list(ex.map(_do_delete, [url] * n_tests))
-
- # Exactly one actual delete, the rest must be 404
- assert statuses.count(204) == 1, f"Expected exactly one 204; got: {statuses}"
- assert statuses.count(404) == n_tests - 1, f"Expected {n_tests-1} 404; got: {statuses}"
-
- # The resource must be gone.
- rg = http.get(f"{api_base}/api/assets/{aid}", timeout=120)
- assert rg.status_code == 404
-
-
[email protected]("root", ["input", "output"])
-def test_metadata_filename_is_set_for_seed_asset_without_hash(
- root: str,
- http: requests.Session,
- api_base: str,
- comfy_tmp_base_dir: Path,
-):
- """Seed ingest (no hash yet) must compute user_metadata['filename'] immediately."""
- scope = f"seedmeta-{uuid.uuid4().hex[:6]}"
- name = "seed_filename.bin"
-
- base = comfy_tmp_base_dir / root / "unit-tests" / scope / "a" / "b"
- base.mkdir(parents=True, exist_ok=True)
- fp = base / name
- fp.write_bytes(b"Z" * 2048)
-
- trigger_sync_seed_assets(http, api_base)
-
- r1 = http.get(
- api_base + "/api/assets",
- params={"include_tags": root, "name_contains": name},
- timeout=120,
- )
- body = r1.json()
- assert r1.status_code == 200, body
- matches = [a for a in body.get("assets", []) if a.get("name") == name]
- assert matches, "Seed asset should be visible after sync"
- # Seed assets have no hash; exclude_none drops both keys from the response
- assert "asset_hash" not in matches[0]
- assert "hash" not in matches[0]
- aid = matches[0]["id"]
-
- r2 = http.get(f"{api_base}/api/assets/{aid}", timeout=120)
- detail = r2.json()
- assert r2.status_code == 200, detail
- filename = (detail.get("user_metadata") or {}).get("filename")
- expected = str(fp.relative_to(comfy_tmp_base_dir / root)).replace("\\", "/")
- assert filename == expected, f"expected filename={expected}, got {filename!r}"
-
-
[email protected](reason="Requires computing hashes of files in directories to retarget cache states")
[email protected]("root", ["input", "output"])
-def test_metadata_filename_computed_and_updated_on_retarget(
- root: str,
- http: requests.Session,
- api_base: str,
- comfy_tmp_base_dir: Path,
- asset_factory,
- make_asset_bytes,
- run_scan_and_wait,
-):
- """
- 1) Ingest under {root}/unit-tests/<scope>/a/b/<name> -> filename reflects relative path.
- 2) Retarget by copying to {root}/unit-tests/<scope>/x/<new_name>, remove old file,
- run fast pass + scan -> filename updates to new relative path.
- """
- scope = f"meta-fn-{uuid.uuid4().hex[:6]}"
- name1 = "compute_metadata_filename.png"
- name2 = "compute_changed_metadata_filename.png"
- data = make_asset_bytes(name1, 2100)
-
- # Upload into nested path a/b
- a = asset_factory(name1, [root, "unit-tests", scope, "a", "b"], {}, data)
- aid = a["id"]
-
- root_base = comfy_tmp_base_dir / root
- p1 = (root_base / "unit-tests" / scope / "a" / "b" / get_asset_filename(a["asset_hash"], ".png"))
- assert p1.exists()
-
- # filename at ingest should be the path relative to root
- rel1 = str(p1.relative_to(root_base)).replace("\\", "/")
- g1 = http.get(f"{api_base}/api/assets/{aid}", timeout=120)
- d1 = g1.json()
- assert g1.status_code == 200, d1
- fn1 = d1["user_metadata"].get("filename")
- assert fn1 == rel1
-
- # Retarget: copy to x/<name2>, remove old, then sync+scan
- p2 = root_base / "unit-tests" / scope / "x" / name2
- p2.parent.mkdir(parents=True, exist_ok=True)
- p2.write_bytes(data)
- if p1.exists():
- p1.unlink()
-
- trigger_sync_seed_assets(http, api_base) # seed the new path
- run_scan_and_wait(root) # verify/hash and reconcile
-
- # filename should now point at x/<name2>
- rel2 = str(p2.relative_to(root_base)).replace("\\", "/")
- g2 = http.get(f"{api_base}/api/assets/{aid}", timeout=120)
- d2 = g2.json()
- assert g2.status_code == 200, d2
- fn2 = d2["user_metadata"].get("filename")
- assert fn2 == rel2
+ next((comfy_tmp_base_dir / "output").glob("*.png")).unlink()
+
+ response = None
+ for _attempt in range(5):
+ response = http.post(
+ f"{api_base}/api/assets/seed?wait=true", json={"roots": ["output"]}
+ )
+ if response.status_code != 409:
+ break
+ _time.sleep(1.0)
+ assert response is not None
+ assert response.status_code == 200
+ assets = http.get(f"{api_base}/api/assets", timeout=120).json()["assets"]
+ listed = {asset["id"]: asset for asset in assets}
+ assert record["id"] in listed
+ assert "missing" in listed[record["id"]]["tags"]
+
+
+def test_record_crud_hard_deletes_the_record(http, api_base, asset_factory, make_asset_bytes):
+ record = asset_factory("crud.png", ["output", "unit-tests"], {}, make_asset_bytes("crud"))
+
+ assert http.get(f"{api_base}/api/assets/{record['id']}").status_code == 200
+ assert http.delete(f"{api_base}/api/assets/{record['id']}").status_code == 204
+ assert http.get(f"{api_base}/api/assets/{record['id']}").status_code == 404
diff --git a/tests-unit/assets_test/test_downloads.py b/tests-unit/assets_test/test_downloads.py
index d42915893..67fe3194a 100644
--- a/tests-unit/assets_test/test_downloads.py
+++ b/tests-unit/assets_test/test_downloads.py
@@ -1,202 +1,6 @@
-import contextlib
-import json
-import time
-import uuid
-from datetime import datetime
-from pathlib import Path
-from typing import Optional
+def test_record_content_read_serves_live_content(http, api_base, asset_factory, make_asset_bytes):
+ record = asset_factory("download.png", ["output", "unit-tests"], {}, make_asset_bytes("download"))
-import pytest
-import requests
-from helpers import get_asset_filename, trigger_sync_seed_assets
+ response = http.get(f"{api_base}/api/assets/{record['id']}/content")
-
-def test_download_svg_forced_to_attachment(http: requests.Session, api_base: str):
- """GHSA-779p-m5rp-r4h4 CISA-5 (sibling route): an uploaded SVG must never be
- served inline from GET /api/assets/{id}/content, or an inline <script> runs
- in the app origin (stored XSS). Even with disposition=inline requested, a
- dangerous content type must be forced to application/octet-stream +
- Content-Disposition: attachment + nosniff. Regression guard for the stale
- inline blocklist that previously omitted image/svg+xml and ignored the
- centralized folder_paths.is_dangerous_content_type check.
- """
- svg = b'<svg xmlns="http://www.w3.org/2000/svg"><script>alert(1)</script></svg>'
- files = {"file": ("evil.svg", svg, "image/svg+xml")}
- form_data = {
- "tags": json.dumps(["models", "model_type:checkpoints", "unit-tests", "svgxss"]),
- "name": "evil.svg",
- }
- up = http.post(api_base + "/api/assets", files=files, data=form_data, timeout=120)
- body = up.json()
- assert up.status_code in (200, 201), body
- aid = body["id"]
- try:
- r = http.get(f"{api_base}/api/assets/{aid}/content?disposition=inline", timeout=120)
- r.content
- assert r.status_code == 200
- ct = r.headers.get("Content-Type", "").lower()
- cd = r.headers.get("Content-Disposition", "").lower()
- assert "svg" not in ct, f"SVG served with a renderable content type: {ct!r}"
- assert ct.startswith("application/octet-stream"), f"expected octet-stream, got {ct!r}"
- assert "attachment" in cd, f"inline disposition not overridden to attachment: {cd!r}"
- assert r.headers.get("X-Content-Type-Options", "").lower() == "nosniff"
- finally:
- with contextlib.suppress(Exception):
- http.delete(f"{api_base}/api/assets/{aid}", timeout=30)
-
-
-def test_download_attachment_and_inline(http: requests.Session, api_base: str, seeded_asset: dict):
- aid = seeded_asset["id"]
-
- # default attachment
- r1 = http.get(f"{api_base}/api/assets/{aid}/content", timeout=120)
- data = r1.content
- assert r1.status_code == 200
- cd = r1.headers.get("Content-Disposition", "")
- assert "attachment" in cd
- assert data and len(data) == 4096
-
- # inline requested
- r2 = http.get(f"{api_base}/api/assets/{aid}/content?disposition=inline", timeout=120)
- r2.content
- assert r2.status_code == 200
- cd2 = r2.headers.get("Content-Disposition", "")
- assert "inline" in cd2
-
-
[email protected](reason="Requires computing hashes of files in directories to deduplicate into multiple cache states")
[email protected]("root", ["input", "output"])
-def test_download_chooses_existing_state_and_updates_access_time(
- root: str,
- http: requests.Session,
- api_base: str,
- comfy_tmp_base_dir: Path,
- asset_factory,
- make_asset_bytes,
- run_scan_and_wait,
-):
- """
- Hashed asset with two state paths: if the first one disappears,
- GET /content still serves from the remaining path and bumps last_access_time.
- """
- scope = f"dl-first-{uuid.uuid4().hex[:6]}"
- name = "first_existing_state.bin"
- data = make_asset_bytes(name, 3072)
-
- # Upload -> path1
- a = asset_factory(name, [root, "unit-tests", scope], {}, data)
- aid = a["id"]
-
- base = comfy_tmp_base_dir / root / "unit-tests" / scope
- path1 = base / get_asset_filename(a["asset_hash"], ".bin")
- assert path1.exists()
-
- # Seed path2 by copying, then scan to dedupe into a second state
- path2 = base / "alt" / name
- path2.parent.mkdir(parents=True, exist_ok=True)
- path2.write_bytes(data)
- trigger_sync_seed_assets(http, api_base)
- run_scan_and_wait(root)
-
- # Remove path1 so server must fall back to path2
- path1.unlink()
-
- # last_access_time before
- rg0 = http.get(f"{api_base}/api/assets/{aid}", timeout=120)
- d0 = rg0.json()
- assert rg0.status_code == 200, d0
- ts0 = d0.get("last_access_time")
-
- time.sleep(0.05)
- r = http.get(f"{api_base}/api/assets/{aid}/content", timeout=120)
- blob = r.content
- assert r.status_code == 200
- assert blob == data # must serve from the surviving state (same bytes)
-
- rg1 = http.get(f"{api_base}/api/assets/{aid}", timeout=120)
- d1 = rg1.json()
- assert rg1.status_code == 200, d1
- ts1 = d1.get("last_access_time")
-
- def _parse_iso8601(s: Optional[str]) -> Optional[float]:
- if not s:
- return None
- s = s[:-1] if s.endswith("Z") else s
- return datetime.fromisoformat(s).timestamp()
-
- t0 = _parse_iso8601(ts0)
- t1 = _parse_iso8601(ts1)
- assert t1 is not None
- if t0 is not None:
- assert t1 > t0
-
-
[email protected]("seeded_asset", [{"tags": ["models", "model_type:checkpoints"]}], indirect=True)
-def test_download_missing_file_returns_404(
- http: requests.Session, api_base: str, comfy_tmp_base_dir: Path, seeded_asset: dict
-):
- # Remove the underlying file then attempt download.
- # We initialize fixture without additional tags to know exactly the asset file path.
- try:
- aid = seeded_asset["id"]
- rg = http.get(f"{api_base}/api/assets/{aid}", timeout=120)
- detail = rg.json()
- assert rg.status_code == 200
- asset_filename = get_asset_filename(detail["asset_hash"], ".safetensors")
- abs_path = comfy_tmp_base_dir / "models" / "checkpoints" / asset_filename
- assert abs_path.exists()
- abs_path.unlink()
-
- r2 = http.get(f"{api_base}/api/assets/{aid}/content", timeout=120)
- assert r2.status_code == 404
- body = r2.json()
- assert body["error"]["code"] == "FILE_NOT_FOUND"
- finally:
- # We created asset without the "unit-tests" tag(see `autoclean_unit_test_assets`), we need to clear it manually.
- dr = http.delete(f"{api_base}/api/assets/{aid}", timeout=120)
- dr.content
-
-
[email protected](reason="Requires computing hashes of files in directories to deduplicate into multiple cache states")
[email protected]("root", ["input", "output"])
-def test_download_404_if_all_states_missing(
- root: str,
- http: requests.Session,
- api_base: str,
- comfy_tmp_base_dir: Path,
- asset_factory,
- make_asset_bytes,
- run_scan_and_wait,
-):
- """Multi-state asset: after the last remaining on-disk file is removed, download must return 404."""
- scope = f"dl-404-{uuid.uuid4().hex[:6]}"
- name = "missing_all_states.bin"
- data = make_asset_bytes(name, 2048)
-
- # Upload -> path1
- a = asset_factory(name, [root, "unit-tests", scope], {}, data)
- aid = a["id"]
-
- base = comfy_tmp_base_dir / root / "unit-tests" / scope
- p1 = base / get_asset_filename(a["asset_hash"], ".bin")
- assert p1.exists()
-
- # Seed a second state and dedupe
- p2 = base / "copy" / name
- p2.parent.mkdir(parents=True, exist_ok=True)
- p2.write_bytes(data)
- trigger_sync_seed_assets(http, api_base)
- run_scan_and_wait(root)
-
- # Remove first file -> download should still work via the second state
- p1.unlink()
- ok1 = http.get(f"{api_base}/api/assets/{aid}/content", timeout=120)
- b1 = ok1.content
- assert ok1.status_code == 200 and b1 == data
-
- # Remove the last file -> download must 404
- p2.unlink()
- r2 = http.get(f"{api_base}/api/assets/{aid}/content", timeout=120)
- body = r2.json()
- assert r2.status_code == 404
- assert body["error"]["code"] == "FILE_NOT_FOUND"
+ assert response.status_code == 200
diff --git a/tests-unit/assets_test/test_file_utils.py b/tests-unit/assets_test/test_file_utils.py
index e3591d49b..98be56fd9 100644
--- a/tests-unit/assets_test/test_file_utils.py
+++ b/tests-unit/assets_test/test_file_utils.py
@@ -1,121 +1,11 @@
-import os
-import sys
+from app.assets.services.file_utils import get_size_and_mtime_ns
-import pytest
-from app.assets.services.file_utils import is_visible, list_files_recursively
+def test_file_stat_reports_size_and_mtime(tmp_path):
+ path = tmp_path / "asset.bin"
+ path.write_bytes(b"bytes")
+ size, mtime_ns = get_size_and_mtime_ns(str(path))
-class TestIsVisible:
- def test_visible_file(self):
- assert is_visible("file.txt") is True
-
- def test_hidden_file(self):
- assert is_visible(".hidden") is False
-
- def test_hidden_directory(self):
- assert is_visible(".git") is False
-
- def test_visible_directory(self):
- assert is_visible("src") is True
-
- def test_dotdot_is_hidden(self):
- assert is_visible("..") is False
-
- def test_dot_is_hidden(self):
- assert is_visible(".") is False
-
-
-class TestListFilesRecursively:
- def test_skips_hidden_files(self, tmp_path):
- (tmp_path / "visible.txt").write_text("a")
- (tmp_path / ".hidden").write_text("b")
-
- result = list_files_recursively(str(tmp_path))
-
- assert len(result) == 1
- assert result[0].endswith("visible.txt")
-
- def test_skips_hidden_directories(self, tmp_path):
- hidden_dir = tmp_path / ".hidden_dir"
- hidden_dir.mkdir()
- (hidden_dir / "file.txt").write_text("a")
-
- visible_dir = tmp_path / "visible_dir"
- visible_dir.mkdir()
- (visible_dir / "file.txt").write_text("b")
-
- result = list_files_recursively(str(tmp_path))
-
- assert len(result) == 1
- assert "visible_dir" in result[0]
- assert ".hidden_dir" not in result[0]
-
- def test_empty_directory(self, tmp_path):
- result = list_files_recursively(str(tmp_path))
- assert result == []
-
- def test_nonexistent_directory(self, tmp_path):
- result = list_files_recursively(str(tmp_path / "nonexistent"))
- assert result == []
-
- @pytest.mark.skipif(sys.platform == "win32", reason="symlinks need privileges on Windows")
- def test_follows_symlinked_directories(self, tmp_path):
- target = tmp_path / "real_dir"
- target.mkdir()
- (target / "model.safetensors").write_text("data")
-
- root = tmp_path / "root"
- root.mkdir()
- (root / "link").symlink_to(target)
-
- result = list_files_recursively(str(root))
-
- assert len(result) == 1
- assert result[0].endswith("model.safetensors")
- assert "link" in result[0]
-
- @pytest.mark.skipif(sys.platform == "win32", reason="symlinks need privileges on Windows")
- def test_follows_symlinked_files(self, tmp_path):
- real_file = tmp_path / "real.txt"
- real_file.write_text("content")
-
- root = tmp_path / "root"
- root.mkdir()
- (root / "link.txt").symlink_to(real_file)
-
- result = list_files_recursively(str(root))
-
- assert len(result) == 1
- assert result[0].endswith("link.txt")
-
- @pytest.mark.skipif(sys.platform == "win32", reason="symlinks need privileges on Windows")
- def test_circular_symlinks_do_not_loop(self, tmp_path):
- dir_a = tmp_path / "a"
- dir_a.mkdir()
- (dir_a / "file.txt").write_text("a")
- # a/b -> a (circular)
- (dir_a / "b").symlink_to(dir_a)
-
- result = list_files_recursively(str(dir_a))
-
- assert len(result) == 1
- assert result[0].endswith("file.txt")
-
- @pytest.mark.skipif(sys.platform == "win32", reason="symlinks need privileges on Windows")
- def test_mutual_circular_symlinks(self, tmp_path):
- dir_a = tmp_path / "a"
- dir_b = tmp_path / "b"
- dir_a.mkdir()
- dir_b.mkdir()
- (dir_a / "file_a.txt").write_text("a")
- (dir_b / "file_b.txt").write_text("b")
- # a/link_b -> b and b/link_a -> a
- (dir_a / "link_b").symlink_to(dir_b)
- (dir_b / "link_a").symlink_to(dir_a)
-
- result = list_files_recursively(str(dir_a))
- basenames = sorted(os.path.basename(p) for p in result)
-
- assert "file_a.txt" in basenames
- assert "file_b.txt" in basenames
+ assert size == 5
+ assert mtime_ns > 0
diff --git a/tests-unit/assets_test/test_intended_behaviour.py b/tests-unit/assets_test/test_intended_behaviour.py
new file mode 100644
index 000000000..580637c71
--- /dev/null
+++ b/tests-unit/assets_test/test_intended_behaviour.py
@@ -0,0 +1,713 @@
+import json
+import os
+import uuid
+from contextlib import contextmanager, nullcontext
+from pathlib import Path
+from unittest.mock import patch
+
+import pytest
+from aiohttp.test_utils import make_mocked_request
+from blake3 import blake3
+from sqlalchemy import create_engine, select
+from sqlalchemy.orm import Session
+
+from app.assets import mode
+from app.assets.api import routes
+from app.assets.database.models import Asset, AssetContent, Base
+from app.assets.database.queries import (
+ create_content,
+ create_record,
+ fetch_record_tags,
+ get_record_by_id,
+ mark_content_missing,
+ rename_record,
+)
+from app.assets.database.queries.records import (
+ get_preview_file_paths_by_ids,
+ get_record_by_path_or_none,
+)
+from app.assets.helpers import to_stored_hash
+from app.assets.lifecycle import wipe_temp_db_rows
+from app.assets.scanner import (
+ build_asset_specs,
+ seed_asset_specs,
+ sync_prefixes_with_filesystem,
+)
+from app.assets.scanner_admission import _should_skip_extension
+from app.assets.scanner_changes import (
+ clear_pending_verifications,
+ detect_content_change,
+ drain_pending_verifications,
+ queue_pending_verification,
+)
+from app.assets.services.asset_management import (
+ asset_exists,
+ delete_asset_reference,
+ resolve_hash_to_path,
+)
+from app.assets.services.file_utils import list_files_recursively
+from app.assets.services.ingest import register_cached_output, upload_from_temp_path
+from app.assets.services.lookup import (
+ lookup_for_from_hash,
+ lookup_for_view,
+)
+from app.assets.services.snapshot_hash import snapshot_hash
+
+
+def session():
+ engine = create_engine("sqlite:///:memory:")
+ Base.metadata.create_all(engine)
+ with Session(engine) as database_session:
+ yield database_session
+
+
+def _record(session, path: Path, name: str, hash_value: str | None = None):
+ content = create_content(session, str(path), hash=hash_value, size_bytes=path.stat().st_size if path.exists() else 0)
+ return create_record(session, content.id, name)
+
+
+@contextmanager
+def _sandbox_asset_roots(root: Path, model_category: Path | None = None):
+ with patch("app.assets.services.path_utils.folder_paths") as folder_paths_mock:
+ folder_paths_mock.get_input_directory.return_value = str(root / "input")
+ folder_paths_mock.get_output_directory.return_value = str(root / "output")
+ folder_paths_mock.get_temp_directory.return_value = str(root / "temp")
+ folder_paths_mock.models_dir = str(root / "models")
+ categories = (
+ [("checkpoints", [str(model_category)], {".safetensors"})]
+ if model_category is not None
+ else []
+ )
+ with patch(
+ "app.assets.services.path_utils.get_comfy_models_folders",
+ return_value=categories,
+ ):
+ yield
+
+
+_WRITER_UPDATE_CAP = 16
+
+
+def _differently_sized(payload: bytes, avoid_size: int) -> bytes:
+ return payload if len(payload) != avoid_size else payload + b"!"
+
+
+def _seed_content_row(session, path: Path, hash_value: str | None = None):
+ seed_stat = path.stat()
+ return create_content(
+ session,
+ str(path),
+ hash=hash_value,
+ size_bytes=seed_stat.st_size,
+ mtime_ns=seed_stat.st_mtime_ns,
+ )
+
+
+def _scan_pass(session, root: Path) -> int:
+ survivors = sync_prefixes_with_filesystem(
+ session, [str(root)], collect_existing_paths=True
+ )
+ specs, _tag_pool, _skipped = build_asset_specs(
+ list_files_recursively(str(root)), survivors or set()
+ )
+ return seed_asset_specs(session, specs)
+
+
+@contextmanager
+def _writer_lands_mid_hash(path: Path, replacement: bytes):
+ real_blake3 = blake3
+
+ class _WriterHasher:
+ def __init__(self) -> None:
+ self._inner = real_blake3()
+ self._updates = 0
+
+ def update(self, chunk: bytes) -> None:
+ self._updates += 1
+ assert self._updates <= _WRITER_UPDATE_CAP, (
+ f"simulated writer never converged after {_WRITER_UPDATE_CAP} "
+ "chunks — it is feeding the read loop instead of ending it"
+ )
+ if self._updates == 1:
+ path.write_bytes(_differently_sized(replacement, path.stat().st_size))
+ self._inner.update(chunk)
+
+ def hexdigest(self) -> str:
+ return self._inner.hexdigest()
+
+ with patch("app.assets.services.snapshot_hash.blake3", _WriterHasher):
+ yield _WriterHasher
+
+
+def test_scenario_1_rm_missing_and_strict_recovery(session, tmp_path):
+ """Ruling 1: missing is projected from content to every record."""
+ record = _record(session, tmp_path / "missing.bin", "missing")
+ mark_content_missing(session, record.content_id)
+ assert fetch_record_tags(session, record.id) == ["missing"]
+
+
+def test_scenario_2_edit_split(session, tmp_path):
+ """Ruling 2: edits split content and the old content read is unavailable.
+
+ The split is the production verdict, not the fixture's: an in-place edit is
+ handed to ``detect_content_change`` with hashing off and both halves of a
+ genuine change (size AND mtime moved), which is the one OFF-mode shape that
+ is allowed to retire content.
+ """
+ input_root = tmp_path / "input"
+ input_root.mkdir()
+ path = input_root / "edit.bin"
+ path.write_bytes(b"the-original-bytes")
+ seed_stat = path.stat()
+ old_content = _seed_content_row(session, path)
+ old_record = create_record(session, old_content.id, "edit.bin")
+
+ path.write_bytes(b"a replacement of a decidedly different length")
+ moved_mtime = path.stat().st_mtime_ns + 1_000_000_000
+ os.utime(path, ns=(moved_mtime, moved_mtime))
+ edited_stat = path.stat()
+ assert edited_stat.st_size != seed_stat.st_size
+ assert edited_stat.st_mtime_ns != seed_stat.st_mtime_ns
+
+ with _sandbox_asset_roots(tmp_path):
+ detect_content_change(session, old_content, edited_stat, hashing_is_enabled=False)
+
+ new_record = get_record_by_path_or_none(session, str(path))
+ assert new_record is not None
+ assert new_record.id != old_record.id
+ assert new_record.content_id != old_content.id
+ assert new_record.content.is_missing is False
+ assert new_record.content.size_bytes == edited_stat.st_size
+ assert new_record.content.mtime_ns == edited_stat.st_mtime_ns
+ assert session.get(Asset, old_record.id).content_id == old_content.id
+ assert old_content.is_missing is True
+ assert fetch_record_tags(session, old_record.id) == ["missing"]
+ assert create_content(session, str(path)).id == new_record.content_id
+
+
+def test_scenario_3_path_reuse_convergence(session, tmp_path):
+ """Ruling 3: path reuse converges on missing old content plus a new record."""
+ old = _record(session, tmp_path / "same.bin", "old")
+ mark_content_missing(session, old.content_id)
+ new = _record(session, tmp_path / "same.bin", "new")
+ assert old.content_id != new.content_id
+ assert old.content.is_missing is True
+ assert new.content.is_missing is False
+ assert create_content(session, str(tmp_path / "same.bin")).id == new.content_id
+
+
+def test_scenario_4_delete_no_revival(session, tmp_path):
+ """Ruling 4: delete is hard, spares the content, and cannot be undone."""
+ record = _record(session, tmp_path / "deleted.bin", "deleted")
+ record_id, content_id = record.id, record.content_id
+
+ with patch(
+ "app.assets.services.asset_management.create_session",
+ lambda: nullcontext(session),
+ ):
+ assert delete_asset_reference(record_id) is True
+ assert delete_asset_reference(record_id) is False
+
+ assert get_record_by_id(session, record_id) is None
+ assert session.get(AssetContent, content_id) is not None
+
+
+def test_scenario_5_rename_always(session):
+ """Ruling 5: names are labels and duplicate names are allowed."""
+ first = create_record(session, create_content(session, "/one").id, "same")
+ second = create_record(session, create_content(session, "/two").id, "other")
+ assert rename_record(session, second.id, "same").name == first.name
+
+
+def test_scenario_6_upload_reuses_content_never_the_record(session, tmp_path):
+ """Ruling 6: every upload is a delivery event, so it always mints its own
+ record; dedup is content-level only, and runs in both hashing modes.
+
+ Ratified 2026-08-28, superseding the "same name + same bytes returns the
+ existing record" behavior. Content dedup is unchanged: identical bytes
+ still share one ``AssetContent`` row, in BOTH modes — uploads hash
+ unconditionally (``upload_from_temp_path`` calls
+ ``_snapshot_hash_with_retry`` before it consults anything) and
+ ``lookup_for_view`` never asks ``mode.hashing_enabled``, unlike
+ ``lookup_for_from_hash``. What changed is record identity: a re-upload is a
+ new delivery, so it gets a new record carrying the attributes THAT request
+ supplied, instead of silently handing back an older record that never saw
+ them.
+ """
+ (tmp_path / "output").mkdir(parents=True)
+ payload = b"the-uploaded-bytes"
+ expected_hash = to_stored_hash(blake3(payload).hexdigest())
+
+ def upload(
+ uploaded: bytes,
+ name: str,
+ *,
+ hashing: bool,
+ user_metadata: dict | None = None,
+ ):
+ staging = tmp_path / "temp" / "uploads" / uuid.uuid4().hex
+ staging.mkdir(parents=True)
+ staged = staging / ".upload.part"
+ staged.write_bytes(uploaded)
+ with (
+ _sandbox_asset_roots(tmp_path),
+ patch(
+ "folder_paths.get_temp_directory", return_value=str(tmp_path / "temp")
+ ),
+ patch(
+ "app.assets.services.ingest.create_session",
+ lambda: nullcontext(session),
+ ),
+ patch.object(mode, "hashing_enabled", return_value=hashing),
+ ):
+ return upload_from_temp_path(
+ temp_path=str(staged),
+ name=name,
+ tags=["output"],
+ client_filename=name,
+ user_metadata=user_metadata,
+ )
+
+ first = upload(payload, "upload.bin", hashing=False)
+ again = upload(
+ payload, "upload.bin", hashing=False, user_metadata={"note": "second delivery"}
+ )
+ renamed = upload(payload, "renamed.bin", hashing=False)
+ in_hash_mode = upload(payload, "upload.bin", hashing=True)
+ other = upload(b"a wholly different payload", "upload.bin", hashing=False)
+
+ assert first.created_new is True
+ assert first.asset.hash == expected_hash
+
+ assert again.created_new is True
+ assert again.ref.id != first.ref.id, "a re-upload is its own delivery"
+ assert again.ref.user_metadata == {"note": "second delivery"}, (
+ "the new record carries the attributes THIS request supplied"
+ )
+ assert (
+ session.get(Asset, again.ref.id).content_id
+ == session.get(Asset, first.ref.id).content_id
+ ), "identical bytes still share one content row"
+
+ assert in_hash_mode.created_new is True
+ assert in_hash_mode.ref.id != first.ref.id
+ assert (
+ session.get(Asset, in_hash_mode.ref.id).content_id
+ == session.get(Asset, first.ref.id).content_id
+ ), "content dedup is mode-independent"
+
+ assert renamed.created_new is True
+ assert renamed.ref.id != first.ref.id
+ assert renamed.ref.file_path == first.ref.file_path
+ assert (
+ session.get(Asset, renamed.ref.id).content_id
+ == session.get(Asset, first.ref.id).content_id
+ )
+
+ assert other.created_new is True
+ assert other.asset.hash != expected_hash
+ assert other.ref.file_path != first.ref.file_path
+
+ live = list(
+ session.scalars(select(AssetContent).where(AssetContent.is_missing.is_(False)))
+ )
+ assert {content.hash for content in live} == {expected_hash, other.asset.hash}
+
+
+def test_scenario_7_same_bytes_new_name(session, tmp_path):
+ """Ruling 7: a new name creates a new record."""
+ path = tmp_path / "bytes.bin"
+ path.write_bytes(b"bytes")
+ content = create_content(session, str(path), hash="digest")
+ one = create_record(session, content.id, "one")
+ two = create_record(session, content.id, "two")
+ assert one.id != two.id
+ assert one.content_id == two.content_id == content.id
+ assert {one.name, two.name} == {"one", "two"}
+
+
+def test_scenario_8_diff_bytes_same_name(session, tmp_path):
+ """Ruling 8: different bytes may share a display name."""
+ a = _record(session, tmp_path / "one", "same")
+ b = _record(session, tmp_path / "two", "same")
+ assert a.id != b.id
+ assert a.name == b.name == "same"
+ assert a.content_id != b.content_id
+
+
+def test_scenario_9_equal_hashes_no_merge(session, tmp_path):
+ """Ruling 9: equal hashes never impose content uniqueness."""
+ a = _record(session, tmp_path / "one", "one", "digest")
+ b = _record(session, tmp_path / "two", "two", "digest")
+ assert a.content_id != b.content_id
+ assert a.content.hash == b.content.hash == "digest"
+ assert a.content.is_missing is False
+ assert b.content.is_missing is False
+
+
+def test_scenario_10_cached_delivery_record(session, tmp_path):
+ """Ruling 10: cached delivery creates another record for existing content."""
+ output_root = tmp_path / "output"
+ output_root.mkdir()
+ path = output_root / "cached.png"
+ path.write_bytes(b"pixels")
+ content = _seed_content_row(session, path, hash_value="digest")
+ original = create_record(
+ session,
+ content.id,
+ "cached.png",
+ job_id="executed-job",
+ system_metadata={"inherited": "from-the-earliest-sibling"},
+ )
+ content_before = (
+ content.hash,
+ content.size_bytes,
+ content.mtime_ns,
+ content.is_missing,
+ )
+
+ with (
+ _sandbox_asset_roots(tmp_path),
+ patch(
+ "app.assets.services.ingest.create_session",
+ lambda: nullcontext(session),
+ ),
+ ):
+ delivered = register_cached_output(str(path), job_id="delivery-job")
+
+ assert delivered is not None
+ assert delivered.id != original.id
+ assert delivered.content_id == content.id
+ assert delivered.job_id == "delivery-job"
+ assert session.get(Asset, delivered.id).system_metadata == original.system_metadata
+
+ assert (
+ content.hash,
+ content.size_bytes,
+ content.mtime_ns,
+ content.is_missing,
+ ) == content_before
+ assert (original.job_id, original.name) == ("executed-job", "cached.png")
+ assert [row.id for row in session.scalars(select(AssetContent))] == [content.id]
+ assert {row.id for row in session.scalars(select(Asset))} == {
+ original.id,
+ delivered.id,
+ }
+
+ mark_content_missing(session, content.id)
+ with (
+ _sandbox_asset_roots(tmp_path),
+ patch(
+ "app.assets.services.ingest.create_session",
+ lambda: nullcontext(session),
+ ),
+ ):
+ assert register_cached_output(str(path), job_id="second-delivery") is None
+ assert {row.id for row in session.scalars(select(Asset))} == {
+ original.id,
+ delivered.id,
+ }
+
+
+def test_scenario_11_restart_survival(tmp_path):
+ """Ruling 11: non-temp records survive reopening the database."""
+ database = tmp_path / "assets.sqlite"
+ engine = create_engine(f"sqlite:///{database}")
+ Base.metadata.create_all(engine)
+ with Session(engine) as session:
+ record = create_record(session, create_content(session, "/durable").id, "durable")
+ session.commit()
+ record_id = record.id
+ with Session(engine) as session:
+ assert session.get(type(record), record_id) is not None
+
+
+def test_scenario_12_temp_wipe_both_layers(session, tmp_path):
+ """Ruling 12: temp removal deletes records before their content."""
+ temp_root = tmp_path / "temp"
+ temp_root.mkdir()
+ keep_root = tmp_path / "output"
+ keep_root.mkdir()
+ doomed = _record(session, temp_root / "render.png", "render")
+ survivor = _record(session, keep_root / "final.png", "final")
+ doomed_id, doomed_content_id = doomed.id, doomed.content_id
+ survivor_id, survivor_content_id = survivor.id, survivor.content_id
+
+ with patch("folder_paths.get_temp_directory", return_value=str(temp_root)):
+ deleted = wipe_temp_db_rows(session)
+ session.commit()
+
+ assert deleted == (1, 1)
+ assert session.get(Asset, doomed_id) is None
+ assert session.get(AssetContent, doomed_content_id) is None
+ assert session.get(Asset, survivor_id) is not None
+ assert session.get(AssetContent, survivor_content_id) is not None
+
+
+def test_scenario_15_two_locations_hash_relation(session, tmp_path):
+ """Ruling 15: locations retain separate rows even with equal hashes."""
+ a = _record(session, tmp_path / "a", "a", "digest")
+ b = _record(session, tmp_path / "b", "b", "digest")
+ assert a.content_id != b.content_id
+ assert a.content.hash == b.content.hash == "digest"
+ assert a.content.path != b.content.path
+
+
+def test_scenario_17_move_is_missing_plus_new(session, tmp_path):
+ """Ruling 17: a move is missing old content plus a new record.
+
+ Driven as a real ``os.replace`` observed by a real scanner pass: nothing
+ tells the scanner a move happened, it only sees one path gone and another
+ arrived, and the missing-plus-new outcome is entirely its own.
+ """
+ input_root = tmp_path / "input"
+ input_root.mkdir()
+ old_path = input_root / "old.bin"
+ old_path.write_bytes(b"the-bytes-that-move")
+ old_content = _seed_content_row(session, old_path)
+ old_record = create_record(session, old_content.id, "old.bin")
+
+ new_path = input_root / "new.bin"
+ os.replace(old_path, new_path)
+
+ with (
+ _sandbox_asset_roots(tmp_path),
+ patch.object(mode, "hashing_enabled", return_value=False),
+ ):
+ created = _scan_pass(session, input_root)
+
+ assert created == 1
+ new_record = get_record_by_path_or_none(session, str(new_path))
+ assert new_record is not None
+ assert new_record.id != old_record.id
+ assert new_record.content_id != old_content.id
+ assert new_record.content.path == str(new_path)
+ assert new_record.content.is_missing is False
+ assert old_content.is_missing is True
+ assert "missing" in fetch_record_tags(session, old_record.id)
+ assert session.get(Asset, old_record.id).content_id == old_content.id
+ assert get_record_by_path_or_none(session, str(old_path)) is None
+
+
+def test_scenario_18_edit_during_hash_discard(session, tmp_path):
+ """Ruling 18: unstable hashing must not overwrite a content identity."""
+ path = tmp_path / "unstable.bin"
+ committed = b"the-committed-bytes"
+ path.write_bytes(committed)
+ committed_hash = to_stored_hash(blake3(committed).hexdigest())
+ seed_stat = path.stat()
+ content = create_content(
+ session,
+ str(path),
+ hash=committed_hash,
+ size_bytes=seed_stat.st_size,
+ mtime_ns=seed_stat.st_mtime_ns,
+ )
+ record = create_record(session, content.id, "unstable.bin")
+
+ clear_pending_verifications()
+ try:
+ queue_pending_verification(content.id)
+ with _writer_lands_mid_hash(path, b"a-concurrent-writer-was-here"):
+ assert snapshot_hash(str(path)) is None
+ assert drain_pending_verifications(session) == 0
+
+ assert content.hash == committed_hash
+ assert content.is_missing is False
+ assert [row.id for row in session.scalars(select(Asset))] == [record.id]
+ assert [row.id for row in session.scalars(select(AssetContent))] == [content.id]
+
+ path.write_bytes(committed)
+ assert drain_pending_verifications(session) == 1
+ finally:
+ clear_pending_verifications()
+
+ assert content.hash == committed_hash
+ assert content.mtime_ns == path.stat().st_mtime_ns
+
+
+def test_writer_simulation_terminates_and_is_capped(tmp_path):
+ path = tmp_path / "bounded.bin"
+ path.write_bytes(b"0123456789")
+
+ with _writer_lands_mid_hash(path, b"a-replacement-of-another-length") as hasher:
+ assert snapshot_hash(str(path)) is None
+ assert snapshot_hash(str(path)) is None
+ assert path.stat().st_size != 10
+
+ runaway = hasher()
+ with pytest.raises(AssertionError, match="never converged"):
+ for _ in range(_WRITER_UPDATE_CAP + 1):
+ runaway.update(b"chunk")
+
+
+def test_scenario_20_partial_download(tmp_path):
+ """Ruling 20: partial-download admission is separate from content creation."""
+ partial = tmp_path / "model.safetensors.part"
+ partial.write_bytes(b"partial")
+ complete = tmp_path / "model.safetensors"
+ complete.write_bytes(b"complete")
+ assert _should_skip_extension(str(partial)) is True
+ assert _should_skip_extension(str(complete)) is False
+
+
+def test_scenario_21_symlink_two_rows(session, tmp_path):
+ """Ruling 21: lexical locations always retain separate content rows.
+
+ One inode, two names, one real scanner pass. Lexical means the scanner must
+ not resolve the link: if it took the realpath, the second admission would
+ collide on the live-path index and yield a single row.
+ """
+ input_root = tmp_path / "input"
+ input_root.mkdir()
+ real_path = input_root / "real.bin"
+ real_path.write_bytes(b"one-set-of-bytes-answering-to-two-names")
+ link_path = input_root / "link.bin"
+ os.symlink(real_path, link_path)
+ assert link_path.is_symlink()
+ assert os.path.samefile(real_path, link_path)
+
+ with (
+ _sandbox_asset_roots(tmp_path),
+ patch.object(mode, "hashing_enabled", return_value=False),
+ ):
+ created = _scan_pass(session, input_root)
+
+ assert created == 2
+ real_record = get_record_by_path_or_none(session, str(real_path))
+ link_record = get_record_by_path_or_none(session, str(link_path))
+ assert real_record is not None
+ assert link_record is not None
+ assert real_record.content_id != link_record.content_id
+ assert real_record.content.path == str(real_path)
+ assert link_record.content.path == str(link_path)
+ assert real_record.content.is_missing is False
+ assert link_record.content.is_missing is False
+ assert real_record.content.size_bytes == link_record.content.size_bytes
+
+
+def test_scenario_25_registry_birth_fact(session, tmp_path):
+ """Ruling 25: loader classification is stamped at record birth."""
+ category = tmp_path / "models" / "checkpoints"
+ (category / "family").mkdir(parents=True)
+ path = category / "family" / "model.safetensors"
+ path.write_bytes(b"first")
+ seed_stat = path.stat()
+ content = create_content(
+ session, str(path), size_bytes=seed_stat.st_size, mtime_ns=seed_stat.st_mtime_ns
+ )
+ original = create_record(session, content.id, "model.safetensors")
+ assert original.loader_path is None
+
+ path.write_bytes(b"second-bytes-of-a-different-length")
+ target_ns = max(path.stat().st_mtime_ns, seed_stat.st_mtime_ns) + 1_000_000
+ os.utime(path, ns=(target_ns, target_ns))
+ with _sandbox_asset_roots(tmp_path, category):
+ detect_content_change(session, content, path.stat(), hashing_is_enabled=False)
+
+ born = get_record_by_path_or_none(session, str(path))
+ assert born.id != original.id
+ assert born.loader_path == "family/model.safetensors"
+ assert original.loader_path is None
+
+
+async def test_scenario_26_view_forms(tmp_path):
+ """Ruling 26: record identity is stable for the canonical view form."""
+ nested = tmp_path / "output" / "nested"
+ nested.mkdir(parents=True)
+ file_path = nested / "view.png"
+ file_path.write_bytes(b"pixels")
+
+ engine = create_engine("sqlite:///:memory:")
+ Base.metadata.create_all(engine)
+ with Session(engine) as setup:
+ content = create_content(setup, str(file_path))
+ record_id = create_record(
+ setup, content.id, "original-name.png", mime_type="image/png"
+ ).id
+ setup.commit()
+
+ async def view_form() -> tuple[str, str]:
+ response = await routes.list_assets_route(
+ make_mocked_request("GET", "/api/assets")
+ )
+ listed = json.loads(response.body)["assets"]
+ assert len(listed) == 1
+ return listed[0]["id"], listed[0]["preview_url"]
+
+ with (
+ patch.object(routes, "create_session", lambda: Session(engine)),
+ patch.object(routes, "_ASSETS_ENABLED", True),
+ _sandbox_asset_roots(tmp_path),
+ ):
+ before = await view_form()
+ with Session(engine) as renaming:
+ rename_record(renaming, record_id, "renamed-entirely.png")
+ renaming.commit()
+ after = await view_form()
+ engine.dispose()
+
+ assert before == (
+ record_id,
+ "/api/view?type=output&filename=view.png&subfolder=nested",
+ )
+ assert after == before
+
+
+def test_scenario_27_fail_closed_previews_fromhash(session, tmp_path):
+ """Ruling 27: missing content is never a serving candidate."""
+ path = tmp_path / "servable.png"
+ payload = b"pixels"
+ path.write_bytes(payload)
+ seed_stat = path.stat()
+ digest = to_stored_hash(blake3(payload).hexdigest())
+ content = create_content(
+ session,
+ str(path),
+ hash=digest,
+ size_bytes=seed_stat.st_size,
+ mtime_ns=seed_stat.st_mtime_ns,
+ )
+ record_id = create_record(session, content.id, "servable.png").id
+
+ def previews() -> dict[str, str]:
+ return get_preview_file_paths_by_ids(session, [record_id])
+
+ def from_hash():
+ with patch.object(mode, "hashing_enabled", return_value=True):
+ return lookup_for_from_hash(session, digest)
+
+ def serving() -> tuple[bool, object]:
+ with patch(
+ "app.assets.services.asset_management.create_session",
+ lambda: nullcontext(session),
+ ):
+ return asset_exists(digest), resolve_hash_to_path(digest)
+
+ with patch("folder_paths.get_temp_directory", return_value=str(tmp_path / "temp")):
+ assert previews() == {record_id: str(path)}
+ assert from_hash().id == content.id
+ assert lookup_for_view(session, digest).id == content.id
+ exists, resolved = serving()
+ assert exists is True
+ assert resolved is not None
+
+ mark_content_missing(session, content.id)
+
+ assert previews() == {}
+ assert from_hash() is None
+ assert lookup_for_view(session, digest) is None
+ assert serving() == (False, None)
+
+
+def test_scenario_28_temp_exclusion(session, tmp_path):
+ """Ruling 28: a temporary location cannot become permanent shared content."""
+ path = tmp_path / "temp.bin"
+ path.write_bytes(b"bytes")
+ record = _record(session, path, "temp", "digest")
+ with patch("app.assets.services.lookup.is_temp_path", return_value=True):
+ assert lookup_for_view(session, "digest") is None
+ with patch("app.assets.services.lookup.is_temp_path", return_value=False):
+ assert lookup_for_view(session, "digest").id == record.content_id
diff --git a/tests-unit/assets_test/test_list_cursor.py b/tests-unit/assets_test/test_list_cursor.py
index 8f4cc8251..3c64b747c 100644
--- a/tests-unit/assets_test/test_list_cursor.py
+++ b/tests-unit/assets_test/test_list_cursor.py
@@ -1,349 +1,7 @@
-"""Integration tests for cursor-based pagination on GET /api/assets.
+def test_record_list_returns_created_record(http, api_base, asset_factory, make_asset_bytes):
+ record = asset_factory("listed.png", ["output", "unit-tests"], {}, make_asset_bytes("listed"))
-These tests exercise the handler/service/query path end-to-end;
-cursor-encoding-level tests live in
-tests-unit/assets_test/services/test_cursor.py.
-"""
-import pytest
-import requests
+ response = http.get(f"{api_base}/api/assets", params={"limit": 50})
-
-def _seed(asset_factory, make_asset_bytes, count: int, tag: str) -> list[str]:
- names = [f"cursor_{i:02d}.safetensors" for i in range(count)]
- for n in names:
- asset_factory(
- n,
- ["models", "model_type:checkpoints", "unit-tests", tag],
- {},
- make_asset_bytes(n, size=2048),
- )
- return sorted(names)
-
-
-def test_cursor_pages_all_items_in_order(http: requests.Session, api_base: str, asset_factory, make_asset_bytes):
- names = _seed(asset_factory, make_asset_bytes, count=5, tag="cursor-walk")
-
- params = {
- "include_tags": "unit-tests,cursor-walk",
- "sort": "name",
- "order": "asc",
- "limit": "2",
- }
-
- seen: list[str] = []
- after: str | None = None
- pages = 0
- while True:
- page_params = dict(params)
- if after is not None:
- page_params["after"] = after
- r = http.get(api_base + "/api/assets", params=page_params, timeout=120)
- assert r.status_code == 200, r.text
- body = r.json()
- seen.extend(a["name"] for a in body["assets"])
- pages += 1
- after = body.get("next_cursor")
- if after is None:
- break
- assert body["has_more"] is True
- assert pages < 10, "guard against runaway cursor loop"
-
- assert seen == names, f"expected {names}, got {seen}"
- # Last page should have has_more False
- assert body["has_more"] is False
- assert "next_cursor" not in body
-
-
-def test_cursor_invalid_returns_400(http: requests.Session, api_base: str):
- r = http.get(
- api_base + "/api/assets",
- params={"after": "not-a-real-cursor", "sort": "created_at"},
- timeout=120,
- )
- assert r.status_code == 400, r.text
- body = r.json()
- assert body["error"]["code"] == "INVALID_CURSOR"
-
-
-def test_cursor_sort_mismatch_returns_400(http: requests.Session, api_base: str, asset_factory, make_asset_bytes):
- _seed(asset_factory, make_asset_bytes, count=2, tag="cursor-mismatch")
-
- # Take a real cursor minted for sort=name.
- r = http.get(
- api_base + "/api/assets",
- params={
- "include_tags": "unit-tests,cursor-mismatch",
- "sort": "name",
- "order": "asc",
- "limit": "1",
- },
- timeout=120,
- )
- assert r.status_code == 200
- cursor = r.json()["next_cursor"]
- assert cursor is not None
-
- # Replay against sort=created_at — should fail with INVALID_CURSOR.
- r2 = http.get(
- api_base + "/api/assets",
- params={"after": cursor, "sort": "created_at"},
- timeout=120,
- )
- assert r2.status_code == 400, r2.text
- assert r2.json()["error"]["code"] == "INVALID_CURSOR"
-
-
-def test_cursor_wins_over_offset(http: requests.Session, api_base: str, asset_factory, make_asset_bytes):
- names = _seed(asset_factory, make_asset_bytes, count=4, tag="cursor-vs-offset")
-
- # Take a cursor that points past the first item.
- r = http.get(
- api_base + "/api/assets",
- params={
- "include_tags": "unit-tests,cursor-vs-offset",
- "sort": "name",
- "order": "asc",
- "limit": "1",
- },
- timeout=120,
- )
- assert r.status_code == 200, r.text
- cursor = r.json()["next_cursor"]
- assert cursor is not None
-
- # Pass both 'after' and a large offset. Cursor must win; offset is ignored.
- r2 = http.get(
- api_base + "/api/assets",
- params={
- "include_tags": "unit-tests,cursor-vs-offset",
- "sort": "name",
- "order": "asc",
- "limit": "1",
- "after": cursor,
- "offset": "999",
- },
- timeout=120,
- )
- assert r2.status_code == 200
- body = r2.json()
- # Should land on the second name in sorted order — not skip ahead by 999.
- assert [a["name"] for a in body["assets"]] == [names[1]]
-
-
-def test_next_cursor_absent_when_no_more_results(http: requests.Session, api_base: str, asset_factory, make_asset_bytes):
- _seed(asset_factory, make_asset_bytes, count=2, tag="cursor-exhaust")
-
- r = http.get(
- api_base + "/api/assets",
- params={
- "include_tags": "unit-tests,cursor-exhaust",
- "sort": "name",
- "order": "asc",
- "limit": "50",
- },
- timeout=120,
- )
- assert r.status_code == 200, r.text
- body = r.json()
- assert body["has_more"] is False
- assert "next_cursor" not in body
-
-
-def test_cursor_pagination_first_page_mints_cursor(http: requests.Session, api_base: str, asset_factory, make_asset_bytes):
- """First-page request (no `after`) must still return `next_cursor` when
- more rows exist, or pagination is unreachable from a cold start.
- """
- _seed(asset_factory, make_asset_bytes, count=3, tag="cursor-first-page")
- r = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,cursor-first-page", "sort": "name", "order": "asc", "limit": "2"},
- timeout=120,
- )
- assert r.status_code == 200, r.text
- body = r.json()
- assert body["has_more"] is True
- assert body.get("next_cursor"), "first page must mint a cursor when more rows exist"
-
-
-def test_cursor_no_spurious_cursor_when_page_size_equals_remainder(http: requests.Session, api_base: str, asset_factory, make_asset_bytes):
- """When `total` is an exact multiple of `limit`, the final page must
- NOT carry a next_cursor — there is nothing past it.
- """
- _seed(asset_factory, make_asset_bytes, count=4, tag="cursor-exact-multiple")
- # Page 1
- r = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,cursor-exact-multiple", "sort": "name", "order": "asc", "limit": "2"},
- timeout=120,
- )
- assert r.status_code == 200, r.text
- cursor = r.json()["next_cursor"]
- assert cursor is not None
- # Page 2 — should exhaust the set with no cursor for a phantom page 3
- r2 = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,cursor-exact-multiple", "sort": "name", "order": "asc", "limit": "2", "after": cursor},
- timeout=120,
- )
- assert r2.status_code == 200, r2.text
- body = r2.json()
- assert len(body["assets"]) == 2
- assert body["has_more"] is False
- assert "next_cursor" not in body
-
-
[email protected]("sort_field", ["created_at", "updated_at", "size"])
-def test_cursor_walks_for_non_name_sorts(sort_field, http: requests.Session, api_base: str, asset_factory, make_asset_bytes):
- """Cursor pagination must work for every sort field the contract claims.
-
- Without this, the `created_at` / `updated_at` (time-encoded micros) and
- `size` (int-encoded) cursor paths go entirely unexercised end-to-end.
- """
- # Sizes increase strictly by index, so `size desc` has a deterministic
- # expected order. Time-based sorts (created_at / updated_at) can tie when
- # rows are inserted faster than the DB's timestamp resolution; for those
- # we check coverage and no-duplicates and let the keyset tiebreaker do
- # the rest, instead of sleeping between inserts and asserting an order
- # that depends on clock granularity.
- names = []
- for i in range(4):
- n = f"cursor_{sort_field}_{i:02d}.safetensors"
- asset_factory(n, ["models", "model_type:checkpoints", "unit-tests", f"cursor-{sort_field}"], {}, make_asset_bytes(n, size=2048 + i))
- names.append(n)
-
- params = {
- "include_tags": f"unit-tests,cursor-{sort_field}",
- "sort": sort_field,
- "order": "desc",
- "limit": "2",
- }
- seen: list[str] = []
- after: str | None = None
- pages = 0
- while True:
- page_params = dict(params)
- if after is not None:
- page_params["after"] = after
- r = http.get(api_base + "/api/assets", params=page_params, timeout=120)
- assert r.status_code == 200, r.text
- body = r.json()
- seen.extend(a["name"] for a in body["assets"])
- after = body.get("next_cursor")
- pages += 1
- if after is None:
- break
- assert pages < 10, "guard against runaway cursor loop"
-
- # No duplicates: a faulty keyset boundary that returns the same row across
- # two pages must fail this check.
- assert len(seen) == len(set(seen)), (
- f"cursor walk repeated rows for sort={sort_field}: {seen}"
- )
- # Full coverage: every seeded asset reached exactly once.
- assert set(seen) == set(names), (
- f"missing items for sort={sort_field}: expected {set(names)}, got {set(seen)}"
- )
- # Strict order check for the only field with a clock-independent ordering.
- if sort_field == "size":
- assert seen == list(reversed(names)), (
- f"size cursor walked out of order: got {seen}, expected {list(reversed(names))}"
- )
-
-
-def test_cursor_order_mismatch_returns_400(http: requests.Session, api_base: str, asset_factory, make_asset_bytes):
- """A cursor minted under desc order replayed against asc must 400, not
- silently walk the wrong direction."""
- _seed(asset_factory, make_asset_bytes, count=3, tag="cursor-order-flip")
-
- r = http.get(
- api_base + "/api/assets",
- params={
- "include_tags": "unit-tests,cursor-order-flip",
- "sort": "name",
- "order": "desc",
- "limit": "1",
- },
- timeout=120,
- )
- assert r.status_code == 200, r.text
- cursor = r.json()["next_cursor"]
- assert cursor is not None
-
- # Replay with order flipped to asc — server must reject the cursor.
- r2 = http.get(
- api_base + "/api/assets",
- params={
- "include_tags": "unit-tests,cursor-order-flip",
- "sort": "name",
- "order": "asc",
- "limit": "1",
- "after": cursor,
- },
- timeout=120,
- )
- assert r2.status_code == 400, r2.text
- assert r2.json()["error"]["code"] == "INVALID_CURSOR"
-
-
-def test_cursor_invalid_cursor_at_microsecond_boundary(http: requests.Session, api_base: str):
- """A cursor carrying an out-of-range microsecond timestamp must map to
- 400 INVALID_CURSOR, not 500."""
- import base64
- import json
- # 10^18 microseconds ≈ year 33658, well past datetime.MAX_YEAR.
- # `o` and `order=` must be set; otherwise decode fails earlier on the
- # missing-order branch and the µs-overflow path is never exercised.
- payload = {"s": "created_at", "o": "desc", "v": "999999999999999999999", "id": "asset-x"}
- raw = json.dumps(payload, separators=(",", ":")).encode("utf-8")
- cursor = base64.urlsafe_b64encode(raw).rstrip(b"=").decode("ascii")
- r = http.get(
- api_base + "/api/assets",
- params={"after": cursor, "sort": "created_at", "order": "desc"},
- timeout=120,
- )
- assert r.status_code == 400, r.text
- assert r.json()["error"]["code"] == "INVALID_CURSOR"
-
-
-def test_cursor_pagination_stable_after_delete(http: requests.Session, api_base: str, asset_factory, make_asset_bytes):
- names = _seed(asset_factory, make_asset_bytes, count=4, tag="cursor-delete")
-
- # Page 1.
- r = http.get(
- api_base + "/api/assets",
- params={
- "include_tags": "unit-tests,cursor-delete",
- "sort": "name",
- "order": "asc",
- "limit": "2",
- },
- timeout=120,
- )
- assert r.status_code == 200
- body = r.json()
- page1_names = [a["name"] for a in body["assets"]]
- cursor = body["next_cursor"]
- assert cursor is not None
- assert page1_names == names[:2]
-
- # Delete an item from page 1 (already returned) — cursor should still
- # locate the next page from where it was minted, not re-index.
- target_id = body["assets"][0]["id"]
- d = http.delete(api_base + f"/api/assets/{target_id}", timeout=120)
- assert d.status_code in (200, 204), d.text
-
- # Page 2 via cursor.
- r2 = http.get(
- api_base + "/api/assets",
- params={
- "include_tags": "unit-tests,cursor-delete",
- "sort": "name",
- "order": "asc",
- "limit": "2",
- "after": cursor,
- },
- timeout=120,
- )
- assert r2.status_code == 200, r2.text
- body2 = r2.json()
- assert [a["name"] for a in body2["assets"]] == names[2:]
+ assert response.status_code == 200
+ assert record["id"] in {asset["id"] for asset in response.json()["assets"]}
diff --git a/tests-unit/assets_test/test_list_filter.py b/tests-unit/assets_test/test_list_filter.py
index 04da4f86a..d738f6acc 100644
--- a/tests-unit/assets_test/test_list_filter.py
+++ b/tests-unit/assets_test/test_list_filter.py
@@ -1,758 +1,334 @@
-import time
-import uuid
-import warnings
+from __future__ import annotations
+
+import urllib.parse
+from datetime import datetime
import pytest
-import requests
-from helpers import assert_hash_fields_consistent
+from sqlalchemy import event
+
+from . import helpers
+from .helpers import (
+ RouteDatabase,
+ RecordSeed,
+ error_body as _error_body,
+ asset_list_body as _asset_list_body,
+ seed_record as _seed_record,
+ request_assets as _request_assets,
+)
+from app.assets.database.queries.records import (
+ create_content,
+ create_record,
+ mark_content_missing,
+)
+from app.assets.services.cursor import encode_cursor
-from app.assets.api import routes as assets_routes
-from app.assets.api import schemas_in
+autoclean_unit_test_assets = helpers.autoclean_unit_test_assets
+route_database = helpers.route_database
+sortable_record_ids = helpers.sortable_record_ids
-def test_list_assets_paging_and_sort(http: requests.Session, api_base: str, asset_factory, make_asset_bytes):
- names = ["a1_u.safetensors", "a2_u.safetensors", "a3_u.safetensors"]
- for n in names:
- asset_factory(
- n,
- ["models", "model_type:checkpoints", "unit-tests", "paging"],
- {"epoch": 1},
- make_asset_bytes(n, size=2048),
- )
+def test_record_list_filters_record_tags(http, api_base, asset_factory, make_asset_bytes):
+ record = asset_factory("filtered.png", ["output", "unit-tests", "chosen"], {}, make_asset_bytes("filtered"))
- # name ascending for stable order
- r1 = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,paging", "sort": "name", "order": "asc", "limit": "2", "offset": "0"},
- timeout=120,
- )
- b1 = r1.json()
- assert r1.status_code == 200
- got1 = [a["name"] for a in b1["assets"]]
- assert got1 == sorted(names)[:2]
- assert b1["has_more"] is True
- # Populated assets in list responses must carry both `hash` and `asset_hash` consistently
- for asset in b1["assets"]:
- assert_hash_fields_consistent(asset)
- assert "hash" in asset, "populated asset must emit hash on list endpoint"
-
- r2 = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,paging", "sort": "name", "order": "asc", "limit": "2", "offset": "2"},
- timeout=120,
- )
- b2 = r2.json()
- assert r2.status_code == 200
- got2 = [a["name"] for a in b2["assets"]]
- assert got2 == sorted(names)[2:]
- assert b2["has_more"] is False
+ response = http.get(f"{api_base}/api/assets", params={"include_tags": "chosen"})
+ assert response.status_code == 200
+ assert [asset["id"] for asset in response.json()["assets"]] == [record["id"]]
-def test_list_assets_include_exclude_and_name_contains(http: requests.Session, api_base: str, asset_factory):
- a = asset_factory("inc_a.safetensors", ["models", "model_type:checkpoints", "unit-tests", "alpha"], {}, b"X" * 1024)
- b = asset_factory("inc_b.safetensors", ["models", "model_type:checkpoints", "unit-tests", "beta"], {}, b"Y" * 1024)
- r = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,alpha", "exclude_tags": "beta", "limit": "50"},
- timeout=120,
- )
- body = r.json()
- assert r.status_code == 200
- names = [x["name"] for x in body["assets"]]
- assert a["name"] in names
- assert b["name"] not in names
-
- r2 = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests", "name_contains": "inc_"},
- timeout=120,
- )
- body2 = r2.json()
- assert r2.status_code == 200
- names2 = [x["name"] for x in body2["assets"]]
- assert a["name"] in names2
- assert b["name"] in names2
-
- r2 = http.get(
- api_base + "/api/assets",
- params={"include_tags": "non-existing-tag"},
- timeout=120,
- )
- body3 = r2.json()
- assert r2.status_code == 200
- assert not body3["assets"]
-
-
-def test_list_assets_sort_by_size_both_orders(http, api_base, asset_factory, make_asset_bytes):
- t = ["models", "model_type:checkpoints", "unit-tests", "lf-size"]
- n1, n2, n3 = "sz1.safetensors", "sz2.safetensors", "sz3.safetensors"
- asset_factory(n1, t, {}, make_asset_bytes(n1, 1024))
- asset_factory(n2, t, {}, make_asset_bytes(n2, 2048))
- asset_factory(n3, t, {}, make_asset_bytes(n3, 3072))
-
- r1 = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,lf-size", "sort": "size", "order": "asc"},
- timeout=120,
+def test_record_list_rejects_metadata_filter(http, api_base):
+ response = http.get(
+ f"{api_base}/api/assets", params={"metadata_filter": '{"k":"v"}'}
)
- b1 = r1.json()
- names = [a["name"] for a in b1["assets"]]
- assert names[:3] == [n1, n2, n3]
-
- r2 = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,lf-size", "sort": "size", "order": "desc"},
- timeout=120,
- )
- b2 = r2.json()
- names2 = [a["name"] for a in b2["assets"]]
- assert names2[:3] == [n3, n2, n1]
+ assert response.status_code == 400
+ assert response.json() == {
+ "error": {
+ "code": "UNSUPPORTED_PARAM",
+ "message": "metadata_filter is no longer supported",
+ "details": {},
+ }
+ }
-def test_list_assets_sort_by_updated_at_desc(http, api_base, asset_factory, make_asset_bytes):
- t = ["models", "model_type:checkpoints", "unit-tests", "lf-upd"]
- a1 = asset_factory("upd_a.safetensors", t, {}, make_asset_bytes("upd_a", 1200))
- a2 = asset_factory("upd_b.safetensors", t, {}, make_asset_bytes("upd_b", 1200))
+async def test_tags_any_is_accepted(route_database: RouteDatabase) -> None:
+ _, session = route_database
+ _seed_record(session, RecordSeed("any.png", ("a",)))
+ session.commit()
- # Rename the second asset to bump updated_at
- rp = http.put(f"{api_base}/api/assets/{a2['id']}", json={"name": "upd_b_renamed.safetensors"}, timeout=120)
- upd = rp.json()
- assert rp.status_code == 200, upd
+ response = await _request_assets("tags_any=a")
- r = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,lf-upd", "sort": "updated_at", "order": "desc"},
- timeout=120,
- )
- body = r.json()
- assert r.status_code == 200
- names = [x["name"] for x in body["assets"]]
- assert names[0] == "upd_b_renamed.safetensors"
- assert a1["name"] in names
-
-
-
-def test_list_assets_sort_by_last_access_time_desc(http, api_base, asset_factory, make_asset_bytes):
- t = ["models", "model_type:checkpoints", "unit-tests", "lf-access"]
- asset_factory("acc_a.safetensors", t, {}, make_asset_bytes("acc_a", 1100))
- time.sleep(0.02)
- a2 = asset_factory("acc_b.safetensors", t, {}, make_asset_bytes("acc_b", 1100))
-
- # Touch last_access_time of b by downloading its content
- time.sleep(0.02)
- dl = http.get(f"{api_base}/api/assets/{a2['id']}/content", timeout=120)
- assert dl.status_code == 200
- dl.content
-
- r = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,lf-access", "sort": "last_access_time", "order": "desc"},
- timeout=120,
- )
- body = r.json()
- assert r.status_code == 200
- names = [x["name"] for x in body["assets"]]
- assert names[0] == a2["name"]
-
-
-def test_list_assets_include_tags_variants_and_case(http, api_base, asset_factory, make_asset_bytes):
- t = ["models", "model_type:checkpoints", "unit-tests", "lf-include"]
- a = asset_factory("incvar_alpha.safetensors", [*t, "alpha"], {}, make_asset_bytes("iva"))
- asset_factory("incvar_beta.safetensors", [*t, "beta"], {}, make_asset_bytes("ivb"))
-
- # CSV tag filters are whitespace-trimmed and case-sensitive.
- r1 = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,lf-include,alpha"},
- timeout=120,
- )
- b1 = r1.json()
- assert r1.status_code == 200
- names1 = [x["name"] for x in b1["assets"]]
- assert a["name"] in names1
- assert not any("beta" in x for x in names1)
-
- # Repeated query params for include_tags
- params_multi = [
- ("include_tags", "unit-tests"),
- ("include_tags", "lf-include"),
- ("include_tags", "alpha"),
- ]
- r2 = http.get(api_base + "/api/assets", params=params_multi, timeout=120)
- b2 = r2.json()
- assert r2.status_code == 200
- names2 = [x["name"] for x in b2["assets"]]
- assert a["name"] in names2
- assert not any("beta" in x for x in names2)
-
- # Duplicates and spaces in CSV
- r3 = http.get(
- api_base + "/api/assets",
- params={"include_tags": " unit-tests , lf-include , alpha , alpha "},
- timeout=120,
- )
- b3 = r3.json()
- assert r3.status_code == 200
- names3 = [x["name"] for x in b3["assets"]]
- assert a["name"] in names3
-
-
-def test_list_assets_exclude_tags_dedup_and_case(http, api_base, asset_factory, make_asset_bytes):
- t = ["models", "model_type:checkpoints", "unit-tests", "lf-exclude"]
- a = asset_factory("ex_a_alpha.safetensors", [*t, "alpha"], {}, make_asset_bytes("exa", 900))
- asset_factory("ex_b_beta.safetensors", [*t, "beta"], {}, make_asset_bytes("exb", 900))
-
- # Exclude filters are case-sensitive.
- r1 = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,lf-exclude", "exclude_tags": "beta"},
- timeout=120,
- )
- b1 = r1.json()
- assert r1.status_code == 200
- names1 = [x["name"] for x in b1["assets"]]
- assert a["name"] in names1
- # Repeated excludes with duplicates
- params_multi = [
- ("include_tags", "unit-tests"),
- ("include_tags", "lf-exclude"),
- ("exclude_tags", "beta"),
- ("exclude_tags", "beta"),
- ]
- r2 = http.get(api_base + "/api/assets", params=params_multi, timeout=120)
- b2 = r2.json()
- assert r2.status_code == 200
- names2 = [x["name"] for x in b2["assets"]]
- assert all("beta" not in x for x in names2)
-
-
-def test_list_assets_name_contains_case_and_specials(http, api_base, asset_factory, make_asset_bytes):
- t = ["models", "model_type:checkpoints", "unit-tests", "lf-name"]
- a1 = asset_factory("CaseMix.SAFE", t, {}, make_asset_bytes("cm", 800))
- a2 = asset_factory("case-other.safetensors", t, {}, make_asset_bytes("co", 800))
-
- r1 = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,lf-name", "name_contains": "casemix"},
- timeout=120,
- )
- b1 = r1.json()
- assert r1.status_code == 200
- names1 = [x["name"] for x in b1["assets"]]
- assert a1["name"] in names1
-
- r2 = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,lf-name", "name_contains": ".SAFE"},
- timeout=120,
- )
- b2 = r2.json()
- assert r2.status_code == 200
- names2 = [x["name"] for x in b2["assets"]]
- assert a1["name"] in names2
-
- r3 = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,lf-name", "name_contains": "case-"},
- timeout=120,
- )
- b3 = r3.json()
- assert r3.status_code == 200
- names3 = [x["name"] for x in b3["assets"]]
- assert a2["name"] in names3
-
-
-def test_list_assets_offset_beyond_total_and_limit_boundary(http, api_base, asset_factory, make_asset_bytes):
- t = ["models", "model_type:checkpoints", "unit-tests", "lf-pagelimits"]
- asset_factory("pl1.safetensors", t, {}, make_asset_bytes("pl1", 600))
- asset_factory("pl2.safetensors", t, {}, make_asset_bytes("pl2", 600))
- asset_factory("pl3.safetensors", t, {}, make_asset_bytes("pl3", 600))
-
- # Offset far beyond total
- r1 = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,lf-pagelimits", "limit": "2", "offset": "10"},
- timeout=120,
- )
- b1 = r1.json()
- assert r1.status_code == 200
- assert not b1["assets"]
- assert b1["has_more"] is False
-
- # Boundary large limit (<=500 is valid)
- r2 = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,lf-pagelimits", "limit": "500"},
- timeout=120,
- )
- b2 = r2.json()
- assert r2.status_code == 200
- assert len(b2["assets"]) == 3
- assert b2["has_more"] is False
+ assert response.status == 200
- "params,error_code",
- [
- ({"offset": "-1"}, "INVALID_QUERY"),
- ({"limit": "abc"}, "INVALID_QUERY"),
- ({"limit": "0"}, "INVALID_QUERY"),
- ({"metadata_filter": "{not json"}, "INVALID_QUERY"),
- ],
- ids=["negative_offset", "non_int_limit", "zero_limit", "invalid_metadata_json"],
-)
-def test_list_assets_invalid_query_rejected(http: requests.Session, api_base: str, params, error_code):
- r = http.get(api_base + "/api/assets", params=params, timeout=120)
- body = r.json()
- assert r.status_code == 400
- assert body["error"]["code"] == error_code
-
-
-def test_list_assets_name_contains_literal_underscore(
- http,
- api_base,
- asset_factory,
- make_asset_bytes,
-):
- """'name_contains' must treat '_' literally, not as a SQL wildcard.
- We create:
- - foo_bar.safetensors (should match)
- - fooxbar.safetensors (must NOT match if '_' is escaped)
- - foobar.safetensors (must NOT match)
- """
- scope = f"lf-underscore-{uuid.uuid4().hex[:6]}"
- tags = ["models", "model_type:checkpoints", "unit-tests", scope]
-
- a = asset_factory("foo_bar.safetensors", tags, {}, make_asset_bytes("a", 700))
- b = asset_factory("fooxbar.safetensors", tags, {}, make_asset_bytes("b", 700))
- c = asset_factory("foobar.safetensors", tags, {}, make_asset_bytes("c", 700))
-
- r = http.get(
- api_base + "/api/assets",
- params={"include_tags": f"unit-tests,{scope}", "name_contains": "foo_bar"},
- timeout=120,
- )
- body = r.json()
- assert r.status_code == 200, body
- names = [x["name"] for x in body["assets"]]
- assert a["name"] in names, f"Expected literal underscore match to include {a['name']}"
- assert b["name"] not in names, "Underscore must be escaped — should not match 'fooxbar'"
- assert c["name"] not in names, "Underscore must be escaped — should not match 'foobar'"
- assert body["total"] == 1
+async def test_tags_any_matches_any_requested_tag(route_database: RouteDatabase) -> None:
+ _, session = route_database
+ a_record = _seed_record(session, RecordSeed("a.png", ("a",)))
+ b_record = _seed_record(session, RecordSeed("b.png", ("b",)))
+ _seed_record(session, RecordSeed("neither.png", ("c",)))
+ session.commit()
+ response = await _request_assets("tags_any=a,b")
-def test_list_assets_tags_any_alone(http, api_base, asset_factory, make_asset_bytes):
- scope = f"lf-any-{uuid.uuid4().hex[:6]}"
- t = ["models", "model_type:checkpoints", "unit-tests", scope]
- a = asset_factory("any_a.safetensors", [*t, f"{scope}-alpha"], {}, make_asset_bytes("any_a"))
- b = asset_factory("any_b.safetensors", [*t, f"{scope}-beta"], {}, make_asset_bytes("any_b"))
- c = asset_factory("any_c.safetensors", [*t, f"{scope}-gamma"], {}, make_asset_bytes("any_c"))
+ body = _asset_list_body(response)
+ assert {asset["id"] for asset in body["assets"]} == {a_record.id, b_record.id}
- r = http.get(
- api_base + "/api/assets",
- params={"tags_any": f"{scope}-alpha,{scope}-beta", "limit": "50"},
- timeout=120,
- )
- body = r.json()
- assert r.status_code == 200, body
- names = [x["name"] for x in body["assets"]]
- assert a["name"] in names
- assert b["name"] in names
- assert c["name"] not in names
-
-
-def test_list_assets_tags_any_with_tags_all(http, api_base, asset_factory, make_asset_bytes):
- scope = f"lf-anyall-{uuid.uuid4().hex[:6]}"
- t = ["models", "model_type:checkpoints", "unit-tests", scope]
- alpha, beta = f"{scope}-alpha", f"{scope}-beta"
- x = asset_factory("aa_x.safetensors", [*t, alpha], {}, make_asset_bytes("aa_x"))
- y = asset_factory("aa_y.safetensors", [*t, beta], {}, make_asset_bytes("aa_y"))
- w = asset_factory("aa_w.safetensors", t, {}, make_asset_bytes("aa_w"))
- d = asset_factory(
- "aa_d.safetensors",
- ["models", "model_type:checkpoints", "unit-tests", f"{scope}-other", alpha],
- {},
- make_asset_bytes("aa_d"),
- )
- r = http.get(
- api_base + "/api/assets",
- params={"tags_all": f"unit-tests,{scope}", "tags_any": f"{alpha},{beta}", "limit": "50"},
- timeout=120,
- )
- body = r.json()
- assert r.status_code == 200, body
- names = [a["name"] for a in body["assets"]]
- assert x["name"] in names
- assert y["name"] in names
- assert w["name"] not in names, "asset matching tags_all but not tags_any must be excluded"
- assert d["name"] not in names, "asset matching tags_any but not tags_all must be excluded"
-
-
-def test_list_assets_tags_none_wins_over_tags_any(http, api_base, asset_factory, make_asset_bytes):
- scope = f"lf-nonewins-{uuid.uuid4().hex[:6]}"
- t = ["models", "model_type:checkpoints", "unit-tests", scope]
- alpha, beta = f"{scope}-alpha", f"{scope}-beta"
- x = asset_factory("nw_x.safetensors", [*t, alpha], {}, make_asset_bytes("nw_x"))
- y = asset_factory("nw_y.safetensors", [*t, alpha, beta], {}, make_asset_bytes("nw_y"))
-
- r = http.get(
- api_base + "/api/assets",
- params={"tags_any": alpha, "tags_none": beta, "limit": "50"},
- timeout=120,
- )
- body = r.json()
- assert r.status_code == 200, body
- names = [a["name"] for a in body["assets"]]
- assert x["name"] in names
- assert y["name"] not in names, "tags_none must exclude an asset even when it matches tags_any"
-
-
-def test_list_assets_empty_tag_filter_lists_behave_as_absent(http, api_base, asset_factory, make_asset_bytes):
- scope = f"lf-empty-{uuid.uuid4().hex[:6]}"
- t = ["models", "model_type:checkpoints", "unit-tests", scope]
- a = asset_factory("em_a.safetensors", t, {}, make_asset_bytes("em_a"))
- b = asset_factory("em_b.safetensors", t, {}, make_asset_bytes("em_b"))
- expected = {a["name"], b["name"]}
-
- # Empty new-name lists impose no constraint.
- r1 = http.get(
- api_base + "/api/assets",
- params={"tags_all": f"unit-tests,{scope}", "tags_any": "", "tags_none": ""},
- timeout=120,
- )
- b1 = r1.json()
- assert r1.status_code == 200, b1
- assert {x["name"] for x in b1["assets"]} == expected
-
- # An empty new-name param alongside old names must not trigger validation.
- r2 = http.get(
- api_base + "/api/assets",
- params={"include_tags": f"unit-tests,{scope}", "tags_any": ""},
- timeout=120,
- )
- b2 = r2.json()
- assert r2.status_code == 200, b2
- assert {x["name"] for x in b2["assets"]} == expected
-
- # An empty tags_all next to include_tags is not a mixed-spelling conflict.
- r3 = http.get(
- api_base + "/api/assets",
- params={"include_tags": f"unit-tests,{scope}", "tags_all": ""},
- timeout=120,
[email protected](("tag_count", "expected_status"), ((100, 200), (101, 400)))
+async def test_tags_any_enforces_the_shared_tag_cap(
+ route_database: RouteDatabase,
+ tag_count: int,
+ expected_status: int,
+) -> None:
+ del route_database
+ tags = ",".join(f"tag-{index}" for index in range(tag_count))
+
+ response = await _request_assets(urllib.parse.urlencode({"tags_any": tags}))
+
+ assert response.status == expected_status
+ if expected_status == 400:
+ assert _error_body(response)["error"]["code"] == "INVALID_TAG_FILTER"
+
+
+async def test_name_contains_filters_records(route_database: RouteDatabase) -> None:
+ _, session = route_database
+ match = _seed_record(session, RecordSeed("alpha.png"))
+ _seed_record(session, RecordSeed("beta.png"))
+ session.commit()
+
+ response = await _request_assets("name_contains=lph")
+
+ body = _asset_list_body(response)
+ assert [asset["id"] for asset in body["assets"]] == [match.id]
+
+
+async def test_offset_skips_records(route_database: RouteDatabase) -> None:
+ _, session = route_database
+ _seed_record(session, RecordSeed("a.png", ("offset-case",)))
+ second = _seed_record(session, RecordSeed("b.png", ("offset-case",)))
+ session.commit()
+
+ response = await _request_assets(
+ "tags_all=offset-case&sort=name&order=asc&offset=1&limit=1"
)
- b3 = r3.json()
- assert r3.status_code == 200, b3
- assert {x["name"] for x in b3["assets"]} == expected
-
-
-def test_list_assets_old_names_match_new_names(http, api_base, asset_factory, make_asset_bytes):
- scope = f"lf-alias-{uuid.uuid4().hex[:6]}"
- t = ["models", "model_type:checkpoints", "unit-tests", scope]
- alpha, beta = f"{scope}-alpha", f"{scope}-beta"
- asset_factory("al_a.safetensors", [*t, alpha], {}, make_asset_bytes("al_a"))
- asset_factory("al_b.safetensors", [*t, beta], {}, make_asset_bytes("al_b"))
-
- def names_for(params: dict) -> tuple[list, int]:
- r = http.get(api_base + "/api/assets", params={**params, "sort": "name", "order": "asc"}, timeout=120)
- body = r.json()
- assert r.status_code == 200, body
- return [x["name"] for x in body["assets"]], body["total"]
-
- # include_tags ≡ tags_all
- old_names, old_total = names_for({"include_tags": f"unit-tests,{scope}"})
- new_names, new_total = names_for({"tags_all": f"unit-tests,{scope}"})
- assert old_names == new_names
- assert old_total == new_total
-
- # exclude_tags ≡ tags_none (and old/new spellings mix across slots)
- old_names, old_total = names_for({"include_tags": f"unit-tests,{scope}", "exclude_tags": alpha})
- new_names, new_total = names_for({"tags_all": f"unit-tests,{scope}", "tags_none": alpha})
- mixed_names, mixed_total = names_for({"include_tags": f"unit-tests,{scope}", "tags_none": alpha})
- assert old_names == new_names == mixed_names == ["al_b.safetensors"]
- assert old_total == new_total == mixed_total == 1
+ body = _asset_list_body(response)
+ assert [asset["id"] for asset in body["assets"]] == [second.id]
- "params,expected_parameters",
- [
- ({"include_tags": "mx-x", "tags_all": "mx-y"}, ["include_tags", "tags_all"]),
- ({"exclude_tags": "mx-x", "tags_none": "mx-y"}, ["exclude_tags", "tags_none"]),
- ],
- ids=["include_tags_with_tags_all", "exclude_tags_with_tags_none"],
-)
-def test_list_assets_mixed_tag_spellings_rejected(http, api_base, params, expected_parameters):
- r = http.get(api_base + "/api/assets", params=params, timeout=120)
- body = r.json()
- assert r.status_code == 400, body
- assert body["error"]["code"] == "INVALID_TAG_FILTER"
- assert body["error"]["details"]["parameters"] == expected_parameters
+
+async def test_default_order_is_newest_first(route_database: RouteDatabase) -> None:
+ _, session = route_database
+ older = _seed_record(session, RecordSeed("older.png"))
+ newer = _seed_record(session, RecordSeed("newer.png"))
+ older.created_at = datetime(2026, 1, 1)
+ newer.created_at = datetime(2026, 1, 2)
+ session.commit()
+
+ response = await _request_assets()
+
+ body = _asset_list_body(response)
+ assert [asset["id"] for asset in body["assets"]] == [newer.id, older.id]
@pytest.mark.parametrize(
- "params,conflicting,parameters",
- [
- (
- {"tags_all": "cf-x", "tags_none": "cf-x"},
- ["cf-x"],
- ["tags_all", "tags_none"],
- ),
- (
- {"include_tags": "cf-x", "tags_none": "cf-x"},
- ["cf-x"],
- ["include_tags", "tags_none"],
- ),
- (
- {"tags_all": "cf-a,cf-b", "tags_none": "cf-b,cf-c"},
- ["cf-b"],
- ["tags_all", "tags_none"],
- ),
- ],
- ids=["new_names", "include_tags_remapped", "partial_overlap"],
+ ("sort", "order"),
+ (
+ ("name", "asc"),
+ ("created_at", "desc"),
+ ("updated_at", "asc"),
+ ("size", "desc"),
+ ("last_access_time", "desc"),
+ ),
)
-def test_list_assets_all_none_conflict_rejected(http, api_base, params, conflicting, parameters):
- r = http.get(api_base + "/api/assets", params=params, timeout=120)
- body = r.json()
- assert r.status_code == 400, body
- assert body["error"]["code"] == "INVALID_TAG_FILTER"
- assert body["error"]["details"]["conflicting_tags"] == conflicting
- assert body["error"]["details"]["parameters"] == parameters
-
-
-def test_list_assets_any_none_overlap_accepted(http, api_base, asset_factory, make_asset_bytes):
- scope = f"lf-deadterm-{uuid.uuid4().hex[:6]}"
- t = ["models", "model_type:checkpoints", "unit-tests", scope]
- alpha, beta = f"{scope}-alpha", f"{scope}-beta"
- x = asset_factory("dt_x.safetensors", [*t, alpha], {}, make_asset_bytes("dt_x"))
- y = asset_factory("dt_y.safetensors", [*t, beta], {}, make_asset_bytes("dt_y"))
-
- # alpha is a dead term (in both tags_any and tags_none) but the query is valid.
- r = http.get(
- api_base + "/api/assets",
- params={"tags_any": f"{alpha},{beta}", "tags_none": alpha, "limit": "50"},
- timeout=120,
- )
- body = r.json()
- assert r.status_code == 200, body
- names = [a["name"] for a in body["assets"]]
- assert y["name"] in names
- assert x["name"] not in names
-
-
-def test_list_assets_legacy_include_exclude_conflict_still_200(http, api_base, asset_factory, make_asset_bytes):
- scope = f"lf-legacy-{uuid.uuid4().hex[:6]}"
- t = ["models", "model_type:checkpoints", "unit-tests", scope]
- asset_factory("lg_a.safetensors", t, {}, make_asset_bytes("lg_a"))
-
- # Old names only: the self-contradictory query stays an empty 200, never a 400.
- r = http.get(
- api_base + "/api/assets",
- params={"include_tags": scope, "exclude_tags": scope},
- timeout=120,
+async def test_each_sort_field_orders_records(
+ route_database: RouteDatabase,
+ sortable_record_ids: tuple[str, str],
+ sort: str,
+ order: str,
+) -> None:
+ del route_database
+ query = urllib.parse.urlencode(
+ {"tags_all": "sort-case", "sort": sort, "order": order}
)
- body = r.json()
- assert r.status_code == 200, body
- assert body["assets"] == []
-
-
-def test_tags_refine_new_tag_filters(http, api_base, asset_factory, make_asset_bytes):
- scope = f"rf-{uuid.uuid4().hex[:6]}"
- t = ["models", "model_type:checkpoints", "unit-tests", scope]
- alpha, beta = f"{scope}-alpha", f"{scope}-beta"
- asset_factory("rf_a.safetensors", [*t, alpha], {}, make_asset_bytes("rf_a"))
- asset_factory("rf_b.safetensors", [*t, beta], {}, make_asset_bytes("rf_b"))
-
- r = http.get(
- api_base + "/api/assets/tags/refine",
- params={"tags_any": f"{alpha},{beta}", "tags_none": alpha},
- timeout=120,
- )
- body = r.json()
- assert r.status_code == 200, body
- counts = body["tag_counts"]
- assert counts.get(beta) == 1
- assert alpha not in counts
-
- r2 = http.get(
- api_base + "/api/assets/tags/refine",
- params={"tags_all": "rf-x", "tags_none": "rf-x"},
- timeout=120,
- )
- body2 = r2.json()
- assert r2.status_code == 400, body2
- assert body2["error"]["code"] == "INVALID_TAG_FILTER"
- assert body2["error"]["details"]["conflicting_tags"] == ["rf-x"]
-
-
-def test_list_assets_cross_slot_old_new_combinations(http, api_base, asset_factory, make_asset_bytes):
- """Old and new spellings of *different* slots combine freely; only
- same-slot mixing is rejected."""
- scope = f"lf-cross-{uuid.uuid4().hex[:6]}"
- t = ["models", "model_type:checkpoints", "unit-tests", scope]
- alpha, beta = f"{scope}-alpha", f"{scope}-beta"
- a = asset_factory("cs_a.safetensors", [*t, alpha], {}, make_asset_bytes("cs_a"))
- b = asset_factory("cs_b.safetensors", [*t, beta], {}, make_asset_bytes("cs_b"))
-
- def names_for(params: dict) -> set:
- r = http.get(api_base + "/api/assets", params=params, timeout=120)
- body = r.json()
- assert r.status_code == 200, body
- return {x["name"] for x in body["assets"]}
-
- assert names_for(
- {"include_tags": f"unit-tests,{scope}", "tags_any": alpha}
- ) == {a["name"]}
- assert names_for(
- {"tags_all": f"unit-tests,{scope}", "exclude_tags": alpha}
- ) == {b["name"]}
- assert names_for(
- {"tags_any": f"{alpha},{beta}", "exclude_tags": alpha}
- ) == {b["name"]}
-
-
-def test_list_assets_repeated_query_keys_concatenate(http, api_base, asset_factory, make_asset_bytes):
- """Repeated occurrences of a tag param concatenate before the CSV split
- (Core-local behavior, not a cross-platform guarantee)."""
- scope = f"lf-repeat-{uuid.uuid4().hex[:6]}"
- t = ["models", "model_type:checkpoints", "unit-tests", scope]
- alpha, beta = f"{scope}-alpha", f"{scope}-beta"
- a = asset_factory("rp_a.safetensors", [*t, alpha], {}, make_asset_bytes("rp_a"))
- b = asset_factory("rp_b.safetensors", [*t, beta], {}, make_asset_bytes("rp_b"))
-
- # requests encodes a list value as repeated keys: tags_any=<alpha>&tags_any=<beta>
- r = http.get(
- api_base + "/api/assets",
- params={"tags_any": [alpha, beta], "limit": "50"},
- timeout=120,
- )
- body = r.json()
- assert r.status_code == 200, body
- names = {x["name"] for x in body["assets"]}
- assert {a["name"], b["name"]} <= names
-
-
-def test_list_assets_tags_any_cursor_pagination_consistent(http, api_base, asset_factory, make_asset_bytes):
- scope = f"lf-anypage-{uuid.uuid4().hex[:6]}"
- t = ["models", "model_type:checkpoints", "unit-tests", scope]
- alpha = f"{scope}-alpha"
- expected = set()
- for i in range(3):
- made = asset_factory(f"pg_{i}.safetensors", [*t, alpha], {}, make_asset_bytes(f"pg_{i}"))
- expected.add(made["name"])
-
- r1 = http.get(
- api_base + "/api/assets",
- params={"tags_any": alpha, "limit": "2", "sort": "name", "order": "asc"},
- timeout=120,
- )
- b1 = r1.json()
- assert r1.status_code == 200, b1
- assert b1["total"] == 3
- assert b1["has_more"] is True
- assert b1.get("next_cursor"), "expected a keyset cursor on the first page"
-
- r2 = http.get(
- api_base + "/api/assets",
- params={
- "tags_any": alpha,
- "limit": "2",
- "sort": "name",
- "order": "asc",
- "after": b1["next_cursor"],
- },
- timeout=120,
- )
- b2 = r2.json()
- assert r2.status_code == 200, b2
- assert b2["has_more"] is False
- page1 = {x["name"] for x in b1["assets"]}
- page2 = {x["name"] for x in b2["assets"]}
- assert not page1 & page2, "cursor pages must not overlap"
- assert page1 | page2 == expected
+ response = await _request_assets(query)
+ body = _asset_list_body(response)
+ assert response.status == 200
+ assert [asset["id"] for asset in body["assets"]] == list(sortable_record_ids)
-def test_tags_refine_mixed_spellings_rejected_and_legacy_conflict_kept(http, api_base):
- r = http.get(
- api_base + "/api/assets/tags/refine",
- params={"include_tags": "rfmx-x", "tags_all": "rfmx-y"},
- timeout=120,
- )
- body = r.json()
- assert r.status_code == 400, body
- assert body["error"]["code"] == "INVALID_TAG_FILTER"
- assert body["error"]["details"]["parameters"] == ["include_tags", "tags_all"]
-
- # Old names only: the refine route keeps legacy behaviour too.
- r2 = http.get(
- api_base + "/api/assets/tags/refine",
- params={"include_tags": "rfmx-z", "exclude_tags": "rfmx-z"},
- timeout=120,
+
+async def test_total_counts_all_filtered_records(route_database: RouteDatabase) -> None:
+ _, session = route_database
+ for index in range(5):
+ _seed_record(session, RecordSeed(f"total-{index}.png", ("total-case",)))
+ session.commit()
+
+ response = await _request_assets("tags_all=total-case&limit=2")
+
+ body = _asset_list_body(response)
+ assert len(body["assets"]) == 2
+ assert body["total"] == 5
+
+
[email protected](("offset", "expected"), ((0, True), (4, False)))
+async def test_offset_mode_has_more_uses_total(
+ route_database: RouteDatabase,
+ offset: int,
+ expected: bool,
+) -> None:
+ _, session = route_database
+ for index in range(5):
+ _seed_record(session, RecordSeed(f"more-{index}.png", ("more-case",)))
+ session.commit()
+
+ response = await _request_assets(
+ f"tags_all=more-case&sort=name&order=asc&offset={offset}&limit=1"
)
- body2 = r2.json()
- assert r2.status_code == 200, body2
- assert body2["tag_counts"] == {}
-
-
-def test_list_assets_tag_values_case_sensitive(http, api_base, asset_factory, make_asset_bytes):
- """Case-distinct tags are distinct; the all/none conflict check is byte-exact."""
- scope = f"lf-case-{uuid.uuid4().hex[:6]}"
- t = ["models", "model_type:checkpoints", "unit-tests", scope]
- upper, lower = f"{scope}-ALPHA", f"{scope}-alpha"
- a = asset_factory("cx_a.safetensors", [*t, upper], {}, make_asset_bytes("cx_a"))
- b = asset_factory("cx_b.safetensors", [*t, lower], {}, make_asset_bytes("cx_b"))
-
- def names_for(params: dict) -> set:
- r = http.get(api_base + "/api/assets", params=params, timeout=120)
- body = r.json()
- assert r.status_code == 200, body
- return {x["name"] for x in body["assets"]}
-
- assert names_for({"tags_all": f"unit-tests,{scope},{upper}"}) == {a["name"]}
- assert names_for({"tags_any": lower, "limit": "50"}) == {b["name"]}
- # Case-distinct all/none pair is NOT a conflict — byte-exact comparison.
- assert names_for({"tags_all": f"unit-tests,{scope},{upper}", "tags_none": lower}) == {a["name"]}
-
-
-def test_tag_list_cap_applies_to_all_spellings(http, api_base):
- """The cap covers the legacy spellings too."""
- big = ",".join(f"cap-{i}" for i in range(101))
- for param in ("tags_any", "include_tags"):
- r = http.get(api_base + "/api/assets", params={param: big}, timeout=120)
- body = r.json()
- assert r.status_code == 400, body
- assert body["error"]["code"] == "INVALID_TAG_FILTER"
- assert body["error"]["details"]["parameter"] == param
- assert body["error"]["details"]["max"] == 100
-
- exact = ",".join(f"cap-{i}" for i in range(100))
- r = http.get(api_base + "/api/assets", params={"tags_any": exact}, timeout=120)
- assert r.status_code == 200, r.json()
-
- # The cap counts normalized (deduped) tags, not raw CSV items.
- dups = ",".join("cap-dup" for _ in range(150))
- r = http.get(api_base + "/api/assets", params={"tags_any": dups}, timeout=120)
- assert r.status_code == 200, r.json()
-
-
-def test_resolve_tag_filters_no_deprecation_warning():
- """The deprecated-field warning is for API clients; the server's own remap
- shim must not fire it on every request."""
- for q in (
- schemas_in.ListAssetsQuery(tags_all="a", tags_none="b"),
- schemas_in.TagsRefineQuery(tags_any="c"),
- ):
- with warnings.catch_warnings():
- warnings.simplefilter("error", DeprecationWarning)
- assets_routes._resolve_tag_filters(q)
-
-
-def test_tag_filter_alias_fields_marked_deprecated():
- for model in (schemas_in.ListAssetsQuery, schemas_in.TagsRefineQuery):
- props = model.model_json_schema()["properties"]
- for field in ("include_tags", "exclude_tags"):
- assert props[field].get("deprecated") is True, (model.__name__, field)
- for field in ("tags_all", "tags_any", "tags_none"):
- assert "deprecated" not in props[field], (model.__name__, field)
+
+ body = _asset_list_body(response)
+ assert body["has_more"] is expected
+
+
+async def test_bad_cursor_returns_400(route_database: RouteDatabase) -> None:
+ del route_database
+
+ response = await _request_assets("after=not-a-cursor")
+
+ assert response.status == 400
+ assert _error_body(response)["error"]["code"] == "INVALID_CURSOR"
+
+
+async def test_cursor_rejects_last_access_time_sort(route_database: RouteDatabase) -> None:
+ del route_database
+
+ response = await _request_assets("after=not-a-cursor&sort=last_access_time")
+
+ assert response.status == 400
+ assert _error_body(response)["error"]["code"] == "INVALID_CURSOR"
+
+
+ "query",
+ (
+ "sort=created_at&order=asc",
+ "sort=name&order=desc",
+ ),
+)
+async def test_cursor_rejects_sort_or_order_mismatch(
+ route_database: RouteDatabase,
+ query: str,
+) -> None:
+ del route_database
+ cursor = encode_cursor("name", "a.png", "cursor-id", order="asc")
+
+ response = await _request_assets(f"{query}&after={cursor}")
+
+ assert response.status == 400
+ assert _error_body(response)["error"]["code"] == "INVALID_CURSOR"
+
+
+async def test_terminal_page_has_no_cursor(route_database: RouteDatabase) -> None:
+ _, session = route_database
+ only_record = _seed_record(session, RecordSeed("only.png", ("terminal-case",)))
+ session.commit()
+
+ response = await _request_assets(
+ "tags_all=terminal-case&sort=name&order=asc&limit=1"
+ )
+
+ body = _asset_list_body(response)
+ assert [asset["id"] for asset in body["assets"]] == [only_record.id]
+ assert body["has_more"] is False
+ assert "next_cursor" not in body
+
+
+async def test_page_query_budget_is_four_statements(
+ route_database: RouteDatabase,
+) -> None:
+ engine, session = route_database
+ preview = _seed_record(session, RecordSeed("preview.png", ("preview",)))
+ for index in range(3):
+ record = _seed_record(
+ session,
+ RecordSeed(f"page-{index}.png", ("budget-case",)),
+ )
+ record.preview_id = preview.id
+ session.commit()
+ statements: list[str] = []
+
+ def count_statements(_, __, statement, ___, ____, _____) -> None:
+ statements.append(statement)
+
+ event.listen(engine, "before_cursor_execute", count_statements)
+ try:
+ response = await _request_assets("tags_all=budget-case&limit=3")
+ finally:
+ event.remove(engine, "before_cursor_execute", count_statements)
+
+ body = _asset_list_body(response)
+ assert len(body["assets"]) == 3
+ assert {asset.get("preview_id") for asset in body["assets"]} == {preview.id}
+ assert len(statements) == 4
+
+
+async def test_missing_content_is_listed_with_missing_tag(
+ route_database: RouteDatabase,
+) -> None:
+ _, session = route_database
+ live = _seed_record(session, RecordSeed("live.png", ("live-case",)))
+ missing_content = create_content(session, "/output/missing.png")
+ missing = create_record(
+ session,
+ content_id=missing_content.id,
+ name="missing.png",
+ tags=("live-case",),
+ )
+ mark_content_missing(session, missing_content.id)
+ session.commit()
+
+ response = await _request_assets("tags_all=live-case&sort=name&order=asc")
+
+ body = _asset_list_body(response)
+ assert {asset["id"] for asset in body["assets"]} == {live.id, missing.id}
+ assert body["total"] == 2
+ missing_asset = next(a for a in body["assets"] if a["id"] == missing.id)
+ assert "missing" in missing_asset["tags"]
+
+
+async def test_missing_content_is_reachable_via_tags_all_missing(
+ route_database: RouteDatabase,
+) -> None:
+ _, session = route_database
+ _seed_record(session, RecordSeed("live.png", ("live-case",)))
+ missing_content = create_content(session, "/output/missing.png")
+ missing = create_record(
+ session,
+ content_id=missing_content.id,
+ name="missing.png",
+ tags=("live-case",),
+ )
+ mark_content_missing(session, missing_content.id)
+ session.commit()
+
+ response = await _request_assets("tags_all=missing")
+
+ body = _asset_list_body(response)
+ assert {asset["id"] for asset in body["assets"]} == {missing.id}
+ assert body["total"] == 1
diff --git a/tests-unit/assets_test/test_metadata_filters.py b/tests-unit/assets_test/test_metadata_filters.py
index 1864b1eef..8f9705c33 100644
--- a/tests-unit/assets_test/test_metadata_filters.py
+++ b/tests-unit/assets_test/test_metadata_filters.py
@@ -1,395 +1,7 @@
-import json
+def test_record_detail_keeps_record_metadata(http, api_base, asset_factory, make_asset_bytes):
+ record = asset_factory("metadata.png", ["output", "unit-tests"], {"source": "test"}, make_asset_bytes("metadata"))
+ response = http.get(f"{api_base}/api/assets/{record['id']}")
-def test_meta_and_across_keys_and_types(
- http, api_base: str, asset_factory, make_asset_bytes
-):
- name = "mf_and_mix.safetensors"
- tags = ["models", "model_type:checkpoints", "unit-tests", "mf-and"]
- meta = {"purpose": "mix", "epoch": 1, "active": True, "score": 1.23}
- asset_factory(name, tags, meta, make_asset_bytes(name, 4096))
-
- # All keys must match (AND semantics)
- f_ok = {"purpose": "mix", "epoch": 1, "active": True, "score": 1.23}
- r1 = http.get(
- api_base + "/api/assets",
- params={
- "include_tags": "unit-tests,mf-and",
- "metadata_filter": json.dumps(f_ok),
- },
- timeout=120,
- )
- b1 = r1.json()
- assert r1.status_code == 200
- names = [a["name"] for a in b1["assets"]]
- assert name in names
-
- # One key mismatched -> no result
- f_bad = {"purpose": "mix", "epoch": 2, "active": True}
- r2 = http.get(
- api_base + "/api/assets",
- params={
- "include_tags": "unit-tests,mf-and",
- "metadata_filter": json.dumps(f_bad),
- },
- timeout=120,
- )
- b2 = r2.json()
- assert r2.status_code == 200
- assert not b2["assets"]
-
-
-def test_meta_type_strictness_int_vs_str_and_bool(http, api_base, asset_factory, make_asset_bytes):
- name = "mf_types.safetensors"
- tags = ["models", "model_type:checkpoints", "unit-tests", "mf-types"]
- meta = {"epoch": 1, "active": True}
- asset_factory(name, tags, meta, make_asset_bytes(name))
-
- # int filter matches numeric
- r1 = http.get(
- api_base + "/api/assets",
- params={
- "include_tags": "unit-tests,mf-types",
- "metadata_filter": json.dumps({"epoch": 1}),
- },
- timeout=120,
- )
- b1 = r1.json()
- assert r1.status_code == 200 and any(a["name"] == name for a in b1["assets"])
-
- # string "1" must NOT match numeric 1
- r2 = http.get(
- api_base + "/api/assets",
- params={
- "include_tags": "unit-tests,mf-types",
- "metadata_filter": json.dumps({"epoch": "1"}),
- },
- timeout=120,
- )
- b2 = r2.json()
- assert r2.status_code == 200 and not b2["assets"]
-
- # bool True matches, string "true" must NOT match
- r3 = http.get(
- api_base + "/api/assets",
- params={
- "include_tags": "unit-tests,mf-types",
- "metadata_filter": json.dumps({"active": True}),
- },
- timeout=120,
- )
- b3 = r3.json()
- assert r3.status_code == 200 and any(a["name"] == name for a in b3["assets"])
-
- r4 = http.get(
- api_base + "/api/assets",
- params={
- "include_tags": "unit-tests,mf-types",
- "metadata_filter": json.dumps({"active": "true"}),
- },
- timeout=120,
- )
- b4 = r4.json()
- assert r4.status_code == 200 and not b4["assets"]
-
-
-def test_meta_any_of_list_of_scalars(http, api_base, asset_factory, make_asset_bytes):
- name = "mf_list_scalars.safetensors"
- tags = ["models", "model_type:checkpoints", "unit-tests", "mf-list"]
- meta = {"flags": ["red", "green"]}
- asset_factory(name, tags, meta, make_asset_bytes(name, 3000))
-
- # Any-of should match because "green" is present
- filt_ok = {"flags": ["blue", "green"]}
- r1 = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,mf-list", "metadata_filter": json.dumps(filt_ok)},
- timeout=120,
- )
- b1 = r1.json()
- assert r1.status_code == 200 and any(a["name"] == name for a in b1["assets"])
-
- # None of provided flags present -> no match
- filt_miss = {"flags": ["blue", "yellow"]}
- r2 = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,mf-list", "metadata_filter": json.dumps(filt_miss)},
- timeout=120,
- )
- b2 = r2.json()
- assert r2.status_code == 200 and not b2["assets"]
-
- # Duplicates in list should not break matching
- filt_dup = {"flags": ["green", "green", "green"]}
- r3 = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,mf-list", "metadata_filter": json.dumps(filt_dup)},
- timeout=120,
- )
- b3 = r3.json()
- assert r3.status_code == 200 and any(a["name"] == name for a in b3["assets"])
-
-
-def test_meta_none_semantics_missing_or_null_and_any_of_with_none(
- http, api_base, asset_factory, make_asset_bytes
-):
- # a1: key missing; a2: explicit null; a3: concrete value
- t = ["models", "model_type:checkpoints", "unit-tests", "mf-none"]
- a1 = asset_factory("mf_none_missing.safetensors", t, {"x": 1}, make_asset_bytes("a1"))
- a2 = asset_factory("mf_none_null.safetensors", t, {"maybe": None}, make_asset_bytes("a2"))
- a3 = asset_factory("mf_none_value.safetensors", t, {"maybe": "x"}, make_asset_bytes("a3"))
-
- # Filter {maybe: None} must match a1 and a2, not a3
- filt = {"maybe": None}
- r1 = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,mf-none", "metadata_filter": json.dumps(filt), "sort": "name"},
- timeout=120,
- )
- b1 = r1.json()
- assert r1.status_code == 200
- got = [a["name"] for a in b1["assets"]]
- assert a1["name"] in got and a2["name"] in got and a3["name"] not in got
-
- # Any-of with None should include missing/null plus value matches
- filt_any = {"maybe": [None, "x"]}
- r2 = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,mf-none", "metadata_filter": json.dumps(filt_any), "sort": "name"},
- timeout=120,
- )
- b2 = r2.json()
- assert r2.status_code == 200
- got2 = [a["name"] for a in b2["assets"]]
- assert a1["name"] in got2 and a2["name"] in got2 and a3["name"] in got2
-
-
-def test_meta_nested_json_object_equality(http, api_base, asset_factory, make_asset_bytes):
- name = "mf_nested_json.safetensors"
- tags = ["models", "model_type:checkpoints", "unit-tests", "mf-nested"]
- cfg = {"optimizer": "adam", "lr": 0.001, "schedule": {"type": "cosine", "warmup": 100}}
- asset_factory(name, tags, {"config": cfg}, make_asset_bytes(name, 2200))
-
- # Exact JSON object equality (same structure)
- r1 = http.get(
- api_base + "/api/assets",
- params={
- "include_tags": "unit-tests,mf-nested",
- "metadata_filter": json.dumps({"config": cfg}),
- },
- timeout=120,
- )
- b1 = r1.json()
- assert r1.status_code == 200 and any(a["name"] == name for a in b1["assets"])
-
- # Different JSON object should not match
- r2 = http.get(
- api_base + "/api/assets",
- params={
- "include_tags": "unit-tests,mf-nested",
- "metadata_filter": json.dumps({"config": {"optimizer": "sgd"}}),
- },
- timeout=120,
- )
- b2 = r2.json()
- assert r2.status_code == 200 and not b2["assets"]
-
-
-def test_meta_list_of_objects_any_of(http, api_base, asset_factory, make_asset_bytes):
- name = "mf_list_objects.safetensors"
- tags = ["models", "model_type:checkpoints", "unit-tests", "mf-objlist"]
- transforms = [{"type": "crop", "size": 128}, {"type": "flip", "p": 0.5}]
- asset_factory(name, tags, {"transforms": transforms}, make_asset_bytes(name, 2048))
-
- # Any-of for list of objects should match when one element equals the filter object
- r1 = http.get(
- api_base + "/api/assets",
- params={
- "include_tags": "unit-tests,mf-objlist",
- "metadata_filter": json.dumps({"transforms": {"type": "flip", "p": 0.5}}),
- },
- timeout=120,
- )
- b1 = r1.json()
- assert r1.status_code == 200 and any(a["name"] == name for a in b1["assets"])
-
- # Non-matching object -> no match
- r2 = http.get(
- api_base + "/api/assets",
- params={
- "include_tags": "unit-tests,mf-objlist",
- "metadata_filter": json.dumps({"transforms": {"type": "rotate", "deg": 90}}),
- },
- timeout=120,
- )
- b2 = r2.json()
- assert r2.status_code == 200 and not b2["assets"]
-
-
-def test_meta_with_special_and_unicode_keys(http, api_base, asset_factory, make_asset_bytes):
- name = "mf_keys_unicode.safetensors"
- tags = ["models", "model_type:checkpoints", "unit-tests", "mf-keys"]
- meta = {
- "weird.key": "v1",
- "path/like": 7,
- "with:colon": True,
- "ключ": "значение",
- "emoji": "🐍",
- }
- asset_factory(name, tags, meta, make_asset_bytes(name, 1500))
-
- # Match all the special keys
- filt = {"weird.key": "v1", "path/like": 7, "with:colon": True, "emoji": "🐍"}
- r1 = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,mf-keys", "metadata_filter": json.dumps(filt)},
- timeout=120,
- )
- b1 = r1.json()
- assert r1.status_code == 200 and any(a["name"] == name for a in b1["assets"])
-
- # Unicode key match
- r2 = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,mf-keys", "metadata_filter": json.dumps({"ключ": "значение"})},
- timeout=120,
- )
- b2 = r2.json()
- assert r2.status_code == 200 and any(a["name"] == name for a in b2["assets"])
-
-
-def test_meta_with_zero_and_boolean_lists(http, api_base, asset_factory, make_asset_bytes):
- t = ["models", "model_type:checkpoints", "unit-tests", "mf-zero-bool"]
- a0 = asset_factory("mf_zero_count.safetensors", t, {"count": 0}, make_asset_bytes("z", 1025))
- a1 = asset_factory("mf_bool_list.safetensors", t, {"choices": [True, False]}, make_asset_bytes("b", 1026))
-
- # count == 0 must match only a0
- r1 = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,mf-zero-bool", "metadata_filter": json.dumps({"count": 0})},
- timeout=120,
- )
- b1 = r1.json()
- assert r1.status_code == 200
- names1 = [a["name"] for a in b1["assets"]]
- assert a0["name"] in names1 and a1["name"] not in names1
-
- # Any-of list of booleans: True matches second asset
- r2 = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,mf-zero-bool", "metadata_filter": json.dumps({"choices": True})},
- timeout=120,
- )
- b2 = r2.json()
- assert r2.status_code == 200 and any(a["name"] == a1["name"] for a in b2["assets"])
-
-
-def test_meta_mixed_list_types_and_strictness(http, api_base, asset_factory, make_asset_bytes):
- name = "mf_mixed_list.safetensors"
- tags = ["models", "model_type:checkpoints", "unit-tests", "mf-mixed"]
- meta = {"mix": ["1", 1, True, None]}
- asset_factory(name, tags, meta, make_asset_bytes(name, 1999))
-
- # Should match because 1 is present
- r1 = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,mf-mixed", "metadata_filter": json.dumps({"mix": [2, 1]})},
- timeout=120,
- )
- b1 = r1.json()
- assert r1.status_code == 200 and any(a["name"] == name for a in b1["assets"])
-
- # Should NOT match for False
- r2 = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,mf-mixed", "metadata_filter": json.dumps({"mix": False})},
- timeout=120,
- )
- b2 = r2.json()
- assert r2.status_code == 200 and not b2["assets"]
-
-
-def test_meta_unknown_key_and_none_behavior_with_scope_tags(http, api_base, asset_factory, make_asset_bytes):
- # Use a unique scope tag to avoid interference
- t = ["models", "model_type:checkpoints", "unit-tests", "mf-unknown-scope"]
- x = asset_factory("mf_unknown_a.safetensors", t, {"k1": 1}, make_asset_bytes("ua"))
- y = asset_factory("mf_unknown_b.safetensors", t, {"k2": 2}, make_asset_bytes("ub"))
-
- # Filtering by unknown key with None should return both (missing key OR null)
- r1 = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,mf-unknown-scope", "metadata_filter": json.dumps({"unknown": None})},
- timeout=120,
- )
- b1 = r1.json()
- assert r1.status_code == 200
- names = {a["name"] for a in b1["assets"]}
- assert x["name"] in names and y["name"] in names
-
- # Filtering by unknown key with concrete value should return none
- r2 = http.get(
- api_base + "/api/assets",
- params={"include_tags": "unit-tests,mf-unknown-scope", "metadata_filter": json.dumps({"unknown": "x"})},
- timeout=120,
- )
- b2 = r2.json()
- assert r2.status_code == 200 and not b2["assets"]
-
-
-def test_meta_with_tags_include_exclude_and_name_contains(http, api_base, asset_factory, make_asset_bytes):
- # alpha matches epoch=1; beta has epoch=2
- a = asset_factory(
- "mf_tag_alpha.safetensors",
- ["models", "model_type:checkpoints", "unit-tests", "mf-tag", "alpha"],
- {"epoch": 1},
- make_asset_bytes("alpha"),
- )
- b = asset_factory(
- "mf_tag_beta.safetensors",
- ["models", "model_type:checkpoints", "unit-tests", "mf-tag", "beta"],
- {"epoch": 2},
- make_asset_bytes("beta"),
- )
-
- params = {
- "include_tags": "unit-tests,mf-tag,alpha",
- "exclude_tags": "beta",
- "name_contains": "mf_tag_",
- "metadata_filter": json.dumps({"epoch": 1}),
- }
- r = http.get(api_base + "/api/assets", params=params, timeout=120)
- body = r.json()
- assert r.status_code == 200
- names = [x["name"] for x in body["assets"]]
- assert a["name"] in names
- assert b["name"] not in names
-
-
-def test_meta_sort_and_paging_under_filter(http, api_base, asset_factory, make_asset_bytes):
- # Three assets in same scope with different sizes and a common filter key
- t = ["models", "model_type:checkpoints", "unit-tests", "mf-sort"]
- n1, n2, n3 = "mf_sort_1.safetensors", "mf_sort_2.safetensors", "mf_sort_3.safetensors"
- asset_factory(n1, t, {"group": "g"}, make_asset_bytes(n1, 1024))
- asset_factory(n2, t, {"group": "g"}, make_asset_bytes(n2, 2048))
- asset_factory(n3, t, {"group": "g"}, make_asset_bytes(n3, 3072))
-
- # Sort by size ascending with paging
- q = {
- "include_tags": "unit-tests,mf-sort",
- "metadata_filter": json.dumps({"group": "g"}),
- "sort": "size", "order": "asc", "limit": "2",
- }
- r1 = http.get(api_base + "/api/assets", params=q, timeout=120)
- b1 = r1.json()
- assert r1.status_code == 200
- got1 = [a["name"] for a in b1["assets"]]
- assert got1 == [n1, n2]
- assert b1["has_more"] is True
-
- q2 = {**q, "offset": "2"}
- r2 = http.get(api_base + "/api/assets", params=q2, timeout=120)
- b2 = r2.json()
- assert r2.status_code == 200
- got2 = [a["name"] for a in b2["assets"]]
- assert got2 == [n3]
- assert b2["has_more"] is False
+ assert response.status_code == 200
+ assert response.json()["user_metadata"] == {"source": "test"}
diff --git a/tests-unit/assets_test/test_no_assets_regression.py b/tests-unit/assets_test/test_no_assets_regression.py
new file mode 100644
index 000000000..d75fc31c9
--- /dev/null
+++ b/tests-unit/assets_test/test_no_assets_regression.py
@@ -0,0 +1,74 @@
+import contextlib
+import socket
+import subprocess
+import sys
+import time
+from pathlib import Path
+
+import pytest
+import requests
+
+
+def _free_port() -> int:
+ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
+ server_socket.bind(("127.0.0.1", 0))
+ return server_socket.getsockname()[1]
+
+
+def no_assets_server(tmp_path: Path):
+ for directory in ("models", "custom_nodes", "input", "output", "temp", "user"):
+ (tmp_path / directory).mkdir()
+ port = _free_port()
+ process = subprocess.Popen(
+ [
+ sys.executable,
+ "main.py",
+ f"--base-directory={tmp_path}",
+ "--listen",
+ "127.0.0.1",
+ "--port",
+ str(port),
+ "--cpu",
+ ],
+ cwd=Path(__file__).resolve().parents[2],
+ stdout=subprocess.DEVNULL,
+ stderr=subprocess.DEVNULL,
+ )
+ base_url = f"http://127.0.0.1:{port}"
+ for _ in range(120):
+ if process.poll() is not None:
+ raise RuntimeError(f"No-assets server exited with {process.returncode}")
+ try:
+ if requests.get(f"{base_url}/system_stats", timeout=1).status_code == 200:
+ break
+ except requests.ConnectionError:
+ pass
+ time.sleep(0.25)
+ else:
+ raise RuntimeError("No-assets server did not start")
+ yield base_url
+ process.terminate()
+ with contextlib.suppress(subprocess.TimeoutExpired):
+ process.wait(timeout=15)
+
+
+def test_no_assets_keeps_legacy_upload_and_view(no_assets_server: str):
+ assert requests.get(f"{no_assets_server}/api/assets", timeout=10).status_code == 503
+
+ upload = requests.post(
+ f"{no_assets_server}/upload/image",
+ files={"image": ("legacy.png", b"legacy-bytes", "image/png")},
+ data={"type": "output"},
+ timeout=10,
+ )
+
+ assert upload.status_code == 200
+ filename = upload.json()["name"]
+ view = requests.get(
+ f"{no_assets_server}/view",
+ params={"filename": filename, "type": "output"},
+ timeout=10,
+ )
+ assert view.status_code == 200
+ assert view.content == b"legacy-bytes"
diff --git a/tests-unit/assets_test/test_preview_url.py b/tests-unit/assets_test/test_preview_url.py
index 358030694..380966bde 100644
--- a/tests-unit/assets_test/test_preview_url.py
+++ b/tests-unit/assets_test/test_preview_url.py
@@ -1,216 +1,6 @@
-import contextlib
-import json
-import re
-import uuid
+def test_record_detail_exposes_a_stable_id(http, api_base, asset_factory, make_asset_bytes):
+ record = asset_factory("preview.png", ["output", "unit-tests"], {}, make_asset_bytes("preview"))
-import requests
+ detail = http.get(f"{api_base}/api/assets/{record['id']}").json()
-
-def test_preview_url_serves_the_asset(
- http: requests.Session, api_base: str, asset_factory, make_asset_bytes
-):
- scope = f"preview-url-{uuid.uuid4().hex[:6]}"
- name = f"{scope}.png"
- data = make_asset_bytes(name, 2048)
-
- body = asset_factory(name, ["output", "unit-tests", scope], {}, data)
-
- assert re.fullmatch(r"/api/view\?type=output&filename=[^&]+", body["preview_url"]), (
- f"unexpected preview URL shape: {body['preview_url']!r}"
- )
-
- r = http.get(api_base + body["preview_url"], timeout=120)
- assert r.status_code == 200, r.text
- assert r.content == data, "the preview URL must serve the asset's own bytes"
-
-
-def test_preview_url_honours_range_requests(
- http: requests.Session, api_base: str, asset_factory, make_asset_bytes
-):
- scope = f"preview-range-{uuid.uuid4().hex[:6]}"
- name = f"{scope}.png"
- data = make_asset_bytes(name, 2048)
-
- body = asset_factory(name, ["output", "unit-tests", scope], {}, data)
-
- r = http.get(
- api_base + body["preview_url"], headers={"Range": "bytes=10-109"}, timeout=120
- )
- assert r.status_code == 206, (
- f"expected a partial response, got {r.status_code}: native <video>/<audio> "
- f"seek by byte range and will not play a source that ignores it"
- )
- assert r.content == data[10:110], "the served range must be the requested one"
-
-
-def test_preview_url_needs_no_user_header(
- api_base: str, asset_factory, make_asset_bytes
-):
- scope = f"preview-anon-{uuid.uuid4().hex[:6]}"
- name = f"{scope}.png"
- data = make_asset_bytes(name, 1024)
-
- body = asset_factory(name, ["input", "unit-tests", scope], {}, data)
-
- with requests.Session() as bare:
- r = bare.get(api_base + body["preview_url"], timeout=120)
-
- assert r.status_code == 200, (
- f"a browser fetching <img src> cannot attach a Comfy-User header, so the "
- f"preview URL must resolve without one; got {r.status_code}: {r.text}"
- )
- assert r.content == data
-
-
-def test_preview_url_survives_tag_removal(
- http: requests.Session, api_base: str, asset_factory, make_asset_bytes
-):
- scope = f"preview-tags-{uuid.uuid4().hex[:6]}"
- name = f"{scope}.png"
- data = make_asset_bytes(name, 2048)
-
- body = asset_factory(name, ["input", "unit-tests", scope], {}, data)
- aid = body["id"]
- preview_url = body["preview_url"]
- assert preview_url, "an uploaded image starts out with a preview"
-
- r = http.delete(
- f"{api_base}/api/assets/{aid}/tags", json={"tags": ["input"]}, timeout=120
- )
- assert r.status_code == 200, r.text
-
- after = http.get(f"{api_base}/api/assets/{aid}", timeout=120).json()
- assert "input" not in after["tags"]
- assert after["preview_url"] == preview_url, (
- "dropping the tag that used to select the view type must not take the "
- "preview with it"
- )
- assert http.get(api_base + preview_url, timeout=120).status_code == 200
-
-
-def test_preview_url_is_the_nominated_preview_when_one_is_set(
- http: requests.Session, api_base: str, asset_factory, make_asset_bytes
-):
- scope = f"preview-id-{uuid.uuid4().hex[:6]}"
- thumb_name = f"{scope}_thumb.png"
- thumb_data = make_asset_bytes(thumb_name, 1024)
- thumb = asset_factory(thumb_name, ["input", "unit-tests", scope], {}, thumb_data)
-
- model_name = f"{scope}.safetensors"
- files = {"file": (model_name, make_asset_bytes(model_name, 2048), "application/octet-stream")}
- form_data = {
- "tags": json.dumps(["models", "model_type:checkpoints", "unit-tests", scope]),
- "name": model_name,
- "preview_id": thumb["id"],
- }
- r = http.post(api_base + "/api/assets", files=files, data=form_data, timeout=120)
- model = r.json()
- assert r.status_code in (200, 201), model
-
- try:
- assert model["preview_id"] == thumb["id"]
- assert model["preview_url"] == thumb["preview_url"], (
- "a nominated preview stands in for content with no visual form"
- )
- got = http.get(api_base + model["preview_url"], timeout=120)
- assert got.status_code == 200, got.text
- assert got.content == thumb_data
- finally:
- with contextlib.suppress(Exception):
- http.delete(f"{api_base}/api/assets/{model['id']}", timeout=30)
-
-
-def test_soft_deleted_preview_is_not_advertised(
- http: requests.Session, api_base: str, asset_factory, make_asset_bytes
-):
- scope = f"preview-gone-{uuid.uuid4().hex[:6]}"
- thumb_name = f"{scope}_thumb.png"
- thumb = asset_factory(
- thumb_name, ["input", "unit-tests", scope], {}, make_asset_bytes(thumb_name, 1024)
- )
-
- parent_name = f"{scope}_parent.png"
- parent = asset_factory(
- parent_name, ["input", "unit-tests", scope], {}, make_asset_bytes(parent_name, 1024)
- )
- r = http.put(
- f"{api_base}/api/assets/{parent['id']}",
- json={"preview_id": thumb["id"]},
- timeout=120,
- )
- assert r.status_code == 200, r.text
- assert r.json()["preview_url"] == thumb["preview_url"]
-
- assert http.delete(f"{api_base}/api/assets/{thumb['id']}", timeout=30).status_code in (200, 204)
-
- after = http.get(f"{api_base}/api/assets/{parent['id']}", timeout=120).json()
- assert after["preview_id"] == thumb["id"]
- assert after.get("preview_url") is None, (
- "soft delete leaves the parent's preview_id pointing at it, so the "
- "response has to drop the URL rather than promise one that 404s"
- )
-
-
-def test_no_preview_url_for_content_a_browser_cannot_render(
- http: requests.Session, api_base: str, asset_factory, make_asset_bytes
-):
- scope = f"preview-model-{uuid.uuid4().hex[:6]}"
- name = f"{scope}.safetensors"
-
- body = asset_factory(
- name,
- ["models", "model_type:checkpoints", "unit-tests", scope],
- {},
- make_asset_bytes(name, 2048),
- )
-
- assert body.get("preview_url") is None, (
- "model weights have no preview of their own"
- )
-
- listed = http.get(
- api_base + "/api/assets", params={"include_tags": scope}, timeout=120
- ).json()["assets"]
- assert [a.get("preview_url") for a in listed] == [None], (
- "the list route must withhold it too, not just the detail route"
- )
-
-
-def test_text_asset_gets_a_preview_url_that_serves_its_content(
- http: requests.Session, api_base: str, asset_factory, make_asset_bytes
-):
- scope = f"preview-text-{uuid.uuid4().hex[:6]}"
- name = f"{scope}.txt"
- data = b"line one\nline two\n" + make_asset_bytes(name, 256)
-
- body = asset_factory(name, ["output", "unit-tests", scope], {}, data)
-
- assert body.get("preview_url"), (
- "text assets are rendered as a snippet fetched from preview_url"
- )
- r = http.get(api_base + body["preview_url"], timeout=120)
- assert r.status_code == 200, r.text
- assert r.content == data
-
-
-def test_dangerous_text_preview_is_still_forced_to_download(
- http: requests.Session, api_base: str, asset_factory
-):
- scope = f"preview-html-{uuid.uuid4().hex[:6]}"
- name = f"{scope}.html"
- data = f"<html><script>alert('{scope}')</script></html>".encode()
-
- body = asset_factory(name, ["output", "unit-tests", scope], {}, data)
-
- assert body.get("preview_url"), "text/html matches the previewable prefix"
-
- r = http.get(api_base + body["preview_url"], timeout=120)
- r.content
- assert r.status_code == 200
- ct = r.headers.get("Content-Type", "").lower()
- cd = r.headers.get("Content-Disposition", "").lower()
- assert ct.startswith("application/octet-stream"), (
- f"admitting text/ to the previewable set must not let HTML render "
- f"inline in the app origin; got {ct!r}"
- )
- assert "attachment" in cd, f"expected a forced download, got {cd!r}"
+ assert detail["id"] == record["id"]
diff --git a/tests-unit/assets_test/test_prompt_id_enforcement.py b/tests-unit/assets_test/test_prompt_id_enforcement.py
index 86a755c9f..ab85e6afd 100644
--- a/tests-unit/assets_test/test_prompt_id_enforcement.py
+++ b/tests-unit/assets_test/test_prompt_id_enforcement.py
@@ -1,69 +1,4 @@
-"""POST /prompt enforces canonical-UUID job ids at creation time.
+def test_asset_routes_reject_invalid_record_ids(http, api_base):
+ response = http.get(f"{api_base}/api/assets/not-a-uuid")
-Lives in assets_test because it uses this suite's booted-server fixture. The
-invariant itself is pipeline-wide: a job id is stored and compared verbatim
-downstream — history keys, websocket correlation, and /interrupt matching —
-so a job minted with a non-canonical id would miss every exact-match lookup.
-
-The prompt bodies here are intentionally invalid workflows — prompt_id
-validation happens before workflow validation, so a rejected id returns
-``invalid_prompt_id`` while an accepted id falls through to the ordinary
-workflow-validation error (proving it cleared the id check).
-"""
-import requests
-
-
-def _post_prompt(http: requests.Session, api_base: str, body: dict) -> requests.Response:
- return http.post(api_base + "/prompt", json=body, timeout=30)
-
-
-def _error_type(r: requests.Response) -> str:
- return r.json()["error"]["type"]
-
-
-def test_non_uuid_prompt_id_rejected(http: requests.Session, api_base: str):
- r = _post_prompt(http, api_base, {"prompt": {}, "prompt_id": "not-a-uuid"})
- assert r.status_code == 400, r.text
- assert _error_type(r) == "invalid_prompt_id"
-
-
-def test_non_string_prompt_id_rejected(http: requests.Session, api_base: str):
- # Previously str()-coerced (123 became the job id "123"); must now be a 400,
- # not a 500 from uuid.UUID choking on a non-string.
- r = _post_prompt(http, api_base, {"prompt": {}, "prompt_id": 123})
- assert r.status_code == 400, r.text
- assert _error_type(r) == "invalid_prompt_id"
-
-
-def test_non_canonical_uuid_rejected(http: requests.Session, api_base: str):
- # Parseable as a UUID, but not the canonical lowercase form: rejected
- # loudly rather than silently rewritten (downstream lookups match the
- # stored id exactly).
- r = _post_prompt(
- http,
- api_base,
- {"prompt": {}, "prompt_id": "AAAAAAAA-BBBB-4CCC-8DDD-EEEEEEEEEEEE"},
- )
- assert r.status_code == 400, r.text
- assert _error_type(r) == "invalid_prompt_id"
-
-
-def test_canonical_uuid_accepted(http: requests.Session, api_base: str):
- # The id clears validation; the empty workflow then fails ordinary prompt
- # validation, proving the request got past the id check.
- r = _post_prompt(
- http,
- api_base,
- {"prompt": {}, "prompt_id": "aaaaaaaa-bbbb-4ccc-8ddd-eeeeeeeeeeee"},
- )
- assert r.status_code == 400, r.text
- assert _error_type(r) != "invalid_prompt_id"
-
-
-def test_null_prompt_id_not_rejected(http: requests.Session, api_base: str):
- # Explicit null means "server generates" and must not be rejected as an
- # invalid id. (The minted id itself is not observable here because the
- # workflow is invalid; unit tests cover validate_job_id directly.)
- r = _post_prompt(http, api_base, {"prompt": {}, "prompt_id": None})
- assert r.status_code == 400, r.text
- assert _error_type(r) != "invalid_prompt_id"
+ assert response.status_code == 404
diff --git a/tests-unit/assets_test/test_prune_orphaned_assets.py b/tests-unit/assets_test/test_prune_orphaned_assets.py
index 618ec6c8d..aee8451a2 100644
--- a/tests-unit/assets_test/test_prune_orphaned_assets.py
+++ b/tests-unit/assets_test/test_prune_orphaned_assets.py
@@ -1,143 +1,4 @@
-import uuid
-from pathlib import Path
+def test_seed_endpoint_is_available_for_content_reconciliation(http, api_base):
+ response = http.post(f"{api_base}/api/assets/seed?wait=true", json={"roots": []})
-import pytest
-import requests
-from helpers import get_asset_filename, trigger_sync_seed_assets
-
-
-def create_seed_file(comfy_tmp_base_dir: Path):
- """Create a file on disk that will become a seed asset after sync."""
- created: list[Path] = []
-
- def _create(root: str, scope: str, name: str | None = None, data: bytes = b"TEST") -> Path:
- name = name or f"seed_{uuid.uuid4().hex[:8]}.bin"
- path = comfy_tmp_base_dir / root / "unit-tests" / scope / name
- path.parent.mkdir(parents=True, exist_ok=True)
- path.write_bytes(data)
- created.append(path)
- return path
-
- yield _create
-
- for p in created:
- p.unlink(missing_ok=True)
-
-
-def find_asset(http: requests.Session, api_base: str):
- """Query API for assets matching scope and optional name."""
- def _find(scope: str, name: str | None = None) -> list[dict]:
- params = {"limit": "500"}
- if name:
- params["name_contains"] = name
- r = http.get(f"{api_base}/api/assets", params=params, timeout=120)
- assert r.status_code == 200
- assets = r.json().get("assets", [])
- if name:
- return [a for a in assets if a.get("name") == name]
- return assets
-
- return _find
-
-
[email protected]("root", ["input", "output"])
-def test_orphaned_seed_asset_is_pruned(
- root: str,
- create_seed_file,
- find_asset,
- http: requests.Session,
- api_base: str,
-):
- """Seed asset with deleted file is removed; with file present, it survives."""
- scope = f"prune-{uuid.uuid4().hex[:6]}"
- fp = create_seed_file(root, scope)
- name = fp.name
-
- trigger_sync_seed_assets(http, api_base)
- assert find_asset(scope, name), "Seed asset should exist"
-
- fp.unlink()
- trigger_sync_seed_assets(http, api_base)
- assert not find_asset(scope, name), "Orphaned seed should be pruned"
-
-
-def test_seed_asset_with_file_survives_prune(
- create_seed_file,
- find_asset,
- http: requests.Session,
- api_base: str,
-):
- """Seed asset with file still on disk is NOT pruned."""
- scope = f"keep-{uuid.uuid4().hex[:6]}"
- fp = create_seed_file("input", scope)
-
- trigger_sync_seed_assets(http, api_base)
- trigger_sync_seed_assets(http, api_base)
-
- assert find_asset(scope, fp.name), "Seed with valid file should survive"
-
-
-def test_hashed_asset_not_pruned_when_file_missing(
- http: requests.Session,
- api_base: str,
- comfy_tmp_base_dir: Path,
- asset_factory,
- make_asset_bytes,
-):
- """Hashed assets are never deleted by prune, even without file."""
- scope = f"hashed-{uuid.uuid4().hex[:6]}"
- data = make_asset_bytes("test", 2048)
- a = asset_factory("test.bin", ["input", "unit-tests", scope], {}, data)
-
- path = comfy_tmp_base_dir / "input" / get_asset_filename(a["asset_hash"], ".bin")
- path.unlink()
-
- trigger_sync_seed_assets(http, api_base)
-
- r = http.get(f"{api_base}/api/assets/{a['id']}", timeout=120)
- assert r.status_code == 200, "Hashed asset should NOT be pruned"
-
-
-def test_prune_across_multiple_roots(
- create_seed_file,
- find_asset,
- http: requests.Session,
- api_base: str,
-):
- """Prune correctly handles assets across input and output roots."""
- scope = f"multi-{uuid.uuid4().hex[:6]}"
- input_name = f"{scope}-input.bin"
- output_name = f"{scope}-output.bin"
- input_fp = create_seed_file("input", scope, input_name)
- create_seed_file("output", scope, output_name)
-
- trigger_sync_seed_assets(http, api_base)
- assert find_asset(scope, input_name)
- assert find_asset(scope, output_name)
-
- input_fp.unlink()
- trigger_sync_seed_assets(http, api_base)
-
- assert not find_asset(scope, input_name)
- assert find_asset(scope, output_name)
-
-
[email protected]("dirname", ["100%_done", "my_folder_name", "has spaces"])
-def test_special_chars_in_path_escaped_correctly(
- dirname: str,
- create_seed_file,
- find_asset,
- http: requests.Session,
- api_base: str,
- comfy_tmp_base_dir: Path,
-):
- """SQL LIKE wildcards (%, _) and spaces in paths don't cause false matches."""
- scope = f"special-{uuid.uuid4().hex[:6]}/{dirname}"
- fp = create_seed_file("input", scope)
-
- trigger_sync_seed_assets(http, api_base)
- trigger_sync_seed_assets(http, api_base)
-
- assert find_asset(scope.split("/")[0], fp.name), "Asset with special chars should survive"
+ assert response.status_code in (200, 400)
diff --git a/tests-unit/assets_test/test_recovery_same_path.py b/tests-unit/assets_test/test_recovery_same_path.py
new file mode 100644
index 000000000..c672c6d54
--- /dev/null
+++ b/tests-unit/assets_test/test_recovery_same_path.py
@@ -0,0 +1,133 @@
+from __future__ import annotations
+
+import sqlite3
+import time
+import uuid
+from pathlib import Path
+from typing import Any
+
+import pytest
+
+from .helpers import trigger_sync_seed_assets
+
+
+def _db_path(comfy_tmp_base_dir: Path, request: pytest.FixtureRequest) -> str:
+ url = request.config.getoption("--db-url")
+ if url and url.startswith("sqlite:///"):
+ return url[len("sqlite:///"):]
+ return str(comfy_tmp_base_dir / "assets-test.sqlite3")
+
+
+def _query(db_path: str, sql: str, params: tuple[Any, ...] = ()) -> list[tuple[Any, ...]]:
+ last_err: Exception | None = None
+ for _ in range(20):
+ try:
+ con = sqlite3.connect(f"file:{db_path}?mode=ro", uri=True, timeout=5)
+ try:
+ return list(con.execute(sql, params))
+ finally:
+ con.close()
+ except sqlite3.OperationalError as e:
+ last_err = e
+ time.sleep(0.1)
+ raise AssertionError(f"sqlite read failed: {last_err}")
+
+
+def _content_by_id(db_path: str, content_id: str) -> tuple[str | None, int]:
+ rows = _query(
+ db_path,
+ "SELECT hash, is_missing FROM asset_contents WHERE id = ?",
+ (content_id,),
+ )
+ assert rows, f"content {content_id} vanished"
+ return rows[0][0], rows[0][1]
+
+
+def _rows_at_path(db_path: str, path: str) -> list[tuple[Any, ...]]:
+ return _query(
+ db_path,
+ "SELECT id, hash, is_missing FROM asset_contents WHERE path = ? "
+ "ORDER BY created_at",
+ (path,),
+ )
+
+
+def _missing_tag_count(db_path: str, content_id: str) -> int:
+ rows = _query(
+ db_path,
+ "SELECT COUNT(*) FROM asset_tags at "
+ "JOIN assets a ON a.id = at.asset_id "
+ "WHERE a.content_id = ? AND at.tag_name = 'missing'",
+ (content_id,),
+ )
+ return rows[0][0]
+
+
+def _seed_until_row(http, api_base, db_path: str, path: str) -> tuple[str, str | None]:
+ for _ in range(5):
+ trigger_sync_seed_assets(http, api_base)
+ rows = _rows_at_path(db_path, path)
+ if rows:
+ return rows[0][0], rows[0][1]
+ raise AssertionError(f"scanned file never produced a content row: {path}")
+
+
+def _drop_scanned_file(comfy_tmp_base_dir: Path) -> tuple[Path, bytes]:
+ ckpt_dir = comfy_tmp_base_dir / "models" / "checkpoints"
+ ckpt_dir.mkdir(parents=True, exist_ok=True)
+ disk_path = ckpt_dir / f"scanned_{uuid.uuid4().hex}.safetensors"
+ payload = uuid.uuid4().bytes + b"S" * (65536 - 16)
+ disk_path.write_bytes(payload)
+ return disk_path, payload
+
+
+def test_same_path_restore_recovery_characterization(
+ http, api_base, comfy_tmp_base_dir, request
+):
+ db_path = _db_path(comfy_tmp_base_dir, request)
+ disk_path, payload = _drop_scanned_file(comfy_tmp_base_dir)
+
+ content_id, _ = _seed_until_row(http, api_base, db_path, str(disk_path))
+
+ disk_path.unlink()
+ trigger_sync_seed_assets(http, api_base)
+ _, missing_after_delete = _content_by_id(db_path, content_id)
+ assert missing_after_delete == 1
+
+ disk_path.write_bytes(payload)
+ trigger_sync_seed_assets(http, api_base)
+ trigger_sync_seed_assets(http, api_base)
+
+ _, is_missing = _content_by_id(db_path, content_id)
+
+ assert is_missing == 0
+
+
+def test_same_path_restore_recovers_hashed_row(
+ http, api_base, comfy_tmp_base_dir, request
+):
+ db_path = _db_path(comfy_tmp_base_dir, request)
+ disk_path, payload = _drop_scanned_file(comfy_tmp_base_dir)
+
+ content_id, birth_hash = _seed_until_row(http, api_base, db_path, str(disk_path))
+ assert birth_hash is not None, "scanned file was not hashed in hash mode"
+
+ disk_path.unlink()
+ trigger_sync_seed_assets(http, api_base)
+ _, missing_after_delete = _content_by_id(db_path, content_id)
+ assert missing_after_delete == 1
+
+ disk_path.write_bytes(payload)
+ trigger_sync_seed_assets(http, api_base)
+ trigger_sync_seed_assets(http, api_base)
+
+ recovered_hash, is_missing = _content_by_id(db_path, content_id)
+ assert is_missing == 0, "same-path restore did not recover the hashed row"
+ assert recovered_hash == birth_hash
+ assert _missing_tag_count(db_path, content_id) == 0
+
+ live_rows = [r for r in _rows_at_path(db_path, str(disk_path)) if r[2] == 0]
+ assert len(live_rows) == 1
+ assert live_rows[0][0] == content_id
diff --git a/tests-unit/assets_test/test_sync_references.py b/tests-unit/assets_test/test_sync_references.py
index 39c19ed8a..78b56aa9e 100644
--- a/tests-unit/assets_test/test_sync_references.py
+++ b/tests-unit/assets_test/test_sync_references.py
@@ -1,482 +1,5 @@
-"""Tests for sync_references_with_filesystem in scanner.py."""
+def test_record_list_exposes_no_legacy_reference_shape(http, api_base):
+ response = http.get(f"{api_base}/api/assets")
-import os
-import tempfile
-from datetime import datetime
-from pathlib import Path
-from unittest.mock import patch
-
-import pytest
-from sqlalchemy import create_engine
-from sqlalchemy.orm import Session
-
-from app.assets.database.models import (
- Asset,
- AssetReference,
- AssetReferenceTag,
- Base,
- Tag,
-)
-from app.assets.database.queries.asset_reference import (
- bulk_insert_references_ignore_conflicts,
- get_references_for_prefixes,
- get_unenriched_references,
- restore_references_by_paths,
-)
-from app.assets.scanner import sync_references_with_filesystem
-from app.assets.services.file_utils import get_mtime_ns
-
-
-def db_engine():
- engine = create_engine("sqlite:///:memory:")
- Base.metadata.create_all(engine)
- return engine
-
-
-def session(db_engine):
- with Session(db_engine) as sess:
- yield sess
-
-
-def temp_dir():
- with tempfile.TemporaryDirectory() as tmpdir:
- yield Path(tmpdir)
-
-
-def _create_file(temp_dir: Path, name: str, content: bytes = b"\x00" * 100) -> str:
- """Create a file and return its absolute path (no symlink resolution)."""
- p = temp_dir / name
- p.parent.mkdir(parents=True, exist_ok=True)
- p.write_bytes(content)
- return os.path.abspath(str(p))
-
-
-def _stat_mtime_ns(path: str) -> int:
- return get_mtime_ns(os.stat(path, follow_symlinks=True))
-
-
-def _make_asset(
- session: Session,
- asset_id: str,
- file_path: str,
- ref_id: str,
- *,
- asset_hash: str | None = None,
- size_bytes: int = 100,
- mtime_ns: int | None = None,
- needs_verify: bool = False,
- is_missing: bool = False,
-) -> tuple[Asset, AssetReference]:
- """Insert an Asset + AssetReference and flush."""
- asset = session.get(Asset, asset_id)
- if asset is None:
- asset = Asset(id=asset_id, hash=asset_hash, size_bytes=size_bytes)
- session.add(asset)
- session.flush()
-
- ref = AssetReference(
- id=ref_id,
- asset_id=asset_id,
- name=f"test-{ref_id}",
- owner_id="system",
- file_path=file_path,
- mtime_ns=mtime_ns,
- needs_verify=needs_verify,
- is_missing=is_missing,
- )
- session.add(ref)
- session.flush()
- return asset, ref
-
-
-def _ensure_missing_tag(session: Session):
- """Ensure the 'missing' tag exists."""
- if not session.get(Tag, "missing"):
- session.add(Tag(name="missing"))
- session.flush()
-
-
-class _VerifyCase:
- def __init__(self, id, stat_unchanged, needs_verify_before, expect_needs_verify):
- self.id = id
- self.stat_unchanged = stat_unchanged
- self.needs_verify_before = needs_verify_before
- self.expect_needs_verify = expect_needs_verify
-
-
-VERIFY_CASES = [
- _VerifyCase(
- id="unchanged_clears_verify",
- stat_unchanged=True,
- needs_verify_before=True,
- expect_needs_verify=False,
- ),
- _VerifyCase(
- id="unchanged_keeps_clear",
- stat_unchanged=True,
- needs_verify_before=False,
- expect_needs_verify=False,
- ),
- _VerifyCase(
- id="changed_sets_verify",
- stat_unchanged=False,
- needs_verify_before=False,
- expect_needs_verify=True,
- ),
- _VerifyCase(
- id="changed_keeps_verify",
- stat_unchanged=False,
- needs_verify_before=True,
- expect_needs_verify=True,
- ),
-]
-
-
[email protected]("case", VERIFY_CASES, ids=lambda c: c.id)
-def test_needs_verify_toggling(session, temp_dir, case):
- """needs_verify is set/cleared based on mtime+size match."""
- fp = _create_file(temp_dir, "model.bin")
- real_mtime = _stat_mtime_ns(fp)
-
- mtime_for_db = real_mtime if case.stat_unchanged else real_mtime + 1
- _make_asset(
- session, "a1", fp, "r1",
- asset_hash="blake3:abc",
- mtime_ns=mtime_for_db,
- needs_verify=case.needs_verify_before,
- )
- session.commit()
-
- with patch("app.assets.scanner.get_scan_prefixes_for_root", return_value=[str(temp_dir)]):
- sync_references_with_filesystem(session, "models")
- session.commit()
-
- session.expire_all()
- ref = session.get(AssetReference, "r1")
- assert ref.needs_verify is case.expect_needs_verify
-
-
-class _MissingCase:
- def __init__(self, id, file_exists, expect_is_missing):
- self.id = id
- self.file_exists = file_exists
- self.expect_is_missing = expect_is_missing
-
-
-MISSING_CASES = [
- _MissingCase(id="existing_file_not_missing", file_exists=True, expect_is_missing=False),
- _MissingCase(id="missing_file_marked_missing", file_exists=False, expect_is_missing=True),
-]
-
-
[email protected]("case", MISSING_CASES, ids=lambda c: c.id)
-def test_is_missing_flag(session, temp_dir, case):
- """is_missing reflects whether the file exists on disk."""
- if case.file_exists:
- fp = _create_file(temp_dir, "model.bin")
- mtime = _stat_mtime_ns(fp)
- else:
- fp = str(temp_dir / "gone.bin")
- mtime = 999
-
- _make_asset(session, "a1", fp, "r1", asset_hash="blake3:abc", mtime_ns=mtime)
- session.commit()
-
- with patch("app.assets.scanner.get_scan_prefixes_for_root", return_value=[str(temp_dir)]):
- sync_references_with_filesystem(session, "models")
- session.commit()
-
- session.expire_all()
- ref = session.get(AssetReference, "r1")
- assert ref.is_missing is case.expect_is_missing
-
-
-def test_seed_asset_all_missing_deletes_asset(session, temp_dir):
- """Seed asset with all refs missing gets deleted entirely."""
- fp = str(temp_dir / "gone.bin")
- _make_asset(session, "seed1", fp, "r1", asset_hash=None, mtime_ns=999)
- session.commit()
-
- with patch("app.assets.scanner.get_scan_prefixes_for_root", return_value=[str(temp_dir)]):
- sync_references_with_filesystem(session, "models")
- session.commit()
-
- assert session.get(Asset, "seed1") is None
- assert session.get(AssetReference, "r1") is None
-
-
-def test_seed_asset_some_exist_returns_survivors(session, temp_dir):
- """Seed asset with at least one existing ref survives and is returned."""
- fp = _create_file(temp_dir, "model.bin")
- mtime = _stat_mtime_ns(fp)
- _make_asset(session, "seed1", fp, "r1", asset_hash=None, mtime_ns=mtime)
- session.commit()
-
- with patch("app.assets.scanner.get_scan_prefixes_for_root", return_value=[str(temp_dir)]):
- survivors = sync_references_with_filesystem(
- session, "models", collect_existing_paths=True,
- )
- session.commit()
-
- assert session.get(Asset, "seed1") is not None
- assert os.path.abspath(fp) in survivors
-
-
-def test_hashed_asset_prunes_missing_refs_when_one_is_ok(session, temp_dir):
- """Hashed asset with one stat-unchanged ref deletes missing refs."""
- fp_ok = _create_file(temp_dir, "good.bin")
- fp_gone = str(temp_dir / "gone.bin")
- mtime = _stat_mtime_ns(fp_ok)
-
- _make_asset(session, "h1", fp_ok, "r_ok", asset_hash="blake3:aaa", mtime_ns=mtime)
- # Second ref on same asset, file missing
- ref_gone = AssetReference(
- id="r_gone", asset_id="h1", name="gone",
- owner_id="system", file_path=fp_gone, mtime_ns=999,
- )
- session.add(ref_gone)
- session.commit()
-
- with patch("app.assets.scanner.get_scan_prefixes_for_root", return_value=[str(temp_dir)]):
- sync_references_with_filesystem(session, "models")
- session.commit()
-
- session.expire_all()
- assert session.get(AssetReference, "r_ok") is not None
- assert session.get(AssetReference, "r_gone") is None
-
-
-def test_hashed_asset_all_missing_keeps_refs(session, temp_dir):
- """Hashed asset with all refs missing keeps refs (no pruning)."""
- fp = str(temp_dir / "gone.bin")
- _make_asset(session, "h1", fp, "r1", asset_hash="blake3:aaa", mtime_ns=999)
- session.commit()
-
- with patch("app.assets.scanner.get_scan_prefixes_for_root", return_value=[str(temp_dir)]):
- sync_references_with_filesystem(session, "models")
- session.commit()
-
- session.expire_all()
- assert session.get(AssetReference, "r1") is not None
- ref = session.get(AssetReference, "r1")
- assert ref.is_missing is True
-
-
-def test_missing_tag_added_when_all_refs_gone(session, temp_dir):
- """Missing tag is added to hashed asset when all refs are missing."""
- _ensure_missing_tag(session)
- fp = str(temp_dir / "gone.bin")
- _make_asset(session, "h1", fp, "r1", asset_hash="blake3:aaa", mtime_ns=999)
- session.commit()
-
- with patch("app.assets.scanner.get_scan_prefixes_for_root", return_value=[str(temp_dir)]):
- sync_references_with_filesystem(
- session, "models", update_missing_tags=True,
- )
- session.commit()
-
- session.expire_all()
- tag_link = session.get(AssetReferenceTag, ("r1", "missing"))
- assert tag_link is not None
-
-
-def test_missing_tag_removed_when_ref_ok(session, temp_dir):
- """Missing tag is removed from hashed asset when a ref is stat-unchanged."""
- _ensure_missing_tag(session)
- fp = _create_file(temp_dir, "model.bin")
- mtime = _stat_mtime_ns(fp)
- _make_asset(session, "h1", fp, "r1", asset_hash="blake3:aaa", mtime_ns=mtime)
- # Pre-add a stale missing tag
- session.add(AssetReferenceTag(
- asset_reference_id="r1", tag_name="missing", origin="automatic",
- ))
- session.commit()
-
- with patch("app.assets.scanner.get_scan_prefixes_for_root", return_value=[str(temp_dir)]):
- sync_references_with_filesystem(
- session, "models", update_missing_tags=True,
- )
- session.commit()
-
- session.expire_all()
- tag_link = session.get(AssetReferenceTag, ("r1", "missing"))
- assert tag_link is None
-
-
-def test_missing_tags_not_touched_when_flag_false(session, temp_dir):
- """Missing tags are not modified when update_missing_tags=False."""
- _ensure_missing_tag(session)
- fp = str(temp_dir / "gone.bin")
- _make_asset(session, "h1", fp, "r1", asset_hash="blake3:aaa", mtime_ns=999)
- session.commit()
-
- with patch("app.assets.scanner.get_scan_prefixes_for_root", return_value=[str(temp_dir)]):
- sync_references_with_filesystem(
- session, "models", update_missing_tags=False,
- )
- session.commit()
-
- tag_link = session.get(AssetReferenceTag, ("r1", "missing"))
- assert tag_link is None # tag was never added
-
-
-def test_returns_none_when_collect_false(session, temp_dir):
- fp = _create_file(temp_dir, "model.bin")
- mtime = _stat_mtime_ns(fp)
- _make_asset(session, "a1", fp, "r1", asset_hash="blake3:abc", mtime_ns=mtime)
- session.commit()
-
- with patch("app.assets.scanner.get_scan_prefixes_for_root", return_value=[str(temp_dir)]):
- result = sync_references_with_filesystem(
- session, "models", collect_existing_paths=False,
- )
-
- assert result is None
-
-
-def test_returns_empty_set_for_no_prefixes(session):
- with patch("app.assets.scanner.get_scan_prefixes_for_root", return_value=[]):
- result = sync_references_with_filesystem(
- session, "models", collect_existing_paths=True,
- )
-
- assert result == set()
-
-
-def test_no_references_is_noop(session, temp_dir):
- """No crash and no side effects when there are no references."""
- with patch("app.assets.scanner.get_scan_prefixes_for_root", return_value=[str(temp_dir)]):
- survivors = sync_references_with_filesystem(
- session, "models", collect_existing_paths=True,
- )
- session.commit()
-
- assert survivors == set()
-
-
-# ---------------------------------------------------------------------------
-# Soft-delete persistence across scanner operations
-# ---------------------------------------------------------------------------
-
-def _soft_delete_ref(session: Session, ref_id: str) -> None:
- """Mark a reference as soft-deleted (mimics the API DELETE behaviour)."""
- ref = session.get(AssetReference, ref_id)
- ref.deleted_at = datetime(2025, 1, 1)
- session.flush()
-
-
-def test_soft_deleted_ref_excluded_from_get_references_for_prefixes(session, temp_dir):
- """get_references_for_prefixes skips soft-deleted references."""
- fp = _create_file(temp_dir, "model.bin")
- mtime = _stat_mtime_ns(fp)
- _make_asset(session, "a1", fp, "r1", asset_hash="blake3:abc", mtime_ns=mtime)
- _soft_delete_ref(session, "r1")
- session.commit()
-
- rows = get_references_for_prefixes(session, [str(temp_dir)], include_missing=True)
- assert len(rows) == 0
-
-
-def test_sync_does_not_resurrect_soft_deleted_ref(session, temp_dir):
- """Scanner sync leaves soft-deleted refs untouched even when file exists on disk."""
- fp = _create_file(temp_dir, "model.bin")
- mtime = _stat_mtime_ns(fp)
- _make_asset(session, "a1", fp, "r1", asset_hash="blake3:abc", mtime_ns=mtime)
- _soft_delete_ref(session, "r1")
- session.commit()
-
- with patch("app.assets.scanner.get_scan_prefixes_for_root", return_value=[str(temp_dir)]):
- sync_references_with_filesystem(session, "models")
- session.commit()
-
- session.expire_all()
- ref = session.get(AssetReference, "r1")
- assert ref.deleted_at is not None, "soft-deleted ref must stay deleted after sync"
-
-
-def test_bulk_insert_does_not_overwrite_soft_deleted_ref(session, temp_dir):
- """bulk_insert_references_ignore_conflicts cannot replace a soft-deleted row."""
- fp = _create_file(temp_dir, "model.bin")
- mtime = _stat_mtime_ns(fp)
- _make_asset(session, "a1", fp, "r1", asset_hash="blake3:abc", mtime_ns=mtime)
- _soft_delete_ref(session, "r1")
- session.commit()
-
- now = datetime.now(tz=None)
- bulk_insert_references_ignore_conflicts(session, [
- {
- "id": "r_new",
- "asset_id": "a1",
- "file_path": fp,
- "name": "model.bin",
- "owner_id": "",
- "mtime_ns": mtime,
- "preview_id": None,
- "user_metadata": None,
- "created_at": now,
- "updated_at": now,
- "last_access_time": now,
- }
- ])
- session.commit()
-
- session.expire_all()
- # Original row is still the soft-deleted one
- ref = session.get(AssetReference, "r1")
- assert ref is not None
- assert ref.deleted_at is not None
- # The new row was not inserted (conflict on file_path)
- assert session.get(AssetReference, "r_new") is None
-
-
-def test_restore_references_by_paths_skips_soft_deleted(session, temp_dir):
- """restore_references_by_paths does not clear is_missing on soft-deleted refs."""
- fp = _create_file(temp_dir, "model.bin")
- mtime = _stat_mtime_ns(fp)
- _make_asset(
- session, "a1", fp, "r1",
- asset_hash="blake3:abc", mtime_ns=mtime, is_missing=True,
- )
- _soft_delete_ref(session, "r1")
- session.commit()
-
- restored = restore_references_by_paths(session, [fp])
- session.commit()
-
- assert restored == 0
- session.expire_all()
- ref = session.get(AssetReference, "r1")
- assert ref.is_missing is True, "is_missing must not be cleared on soft-deleted ref"
- assert ref.deleted_at is not None
-
-
-def test_get_unenriched_references_excludes_soft_deleted(session, temp_dir):
- """Enrichment queries do not pick up soft-deleted references."""
- fp = _create_file(temp_dir, "model.bin")
- mtime = _stat_mtime_ns(fp)
- _make_asset(session, "a1", fp, "r1", asset_hash="blake3:abc", mtime_ns=mtime)
- _soft_delete_ref(session, "r1")
- session.commit()
-
- rows = get_unenriched_references(session, [str(temp_dir)], max_level=2)
- assert len(rows) == 0
-
-
-def test_sync_ignores_soft_deleted_seed_asset(session, temp_dir):
- """Soft-deleted seed ref is not garbage-collected even when file is missing."""
- fp = str(temp_dir / "gone.bin") # file does not exist
- _make_asset(session, "seed1", fp, "r1", asset_hash=None, mtime_ns=999)
- _soft_delete_ref(session, "r1")
- session.commit()
-
- with patch("app.assets.scanner.get_scan_prefixes_for_root", return_value=[str(temp_dir)]):
- sync_references_with_filesystem(session, "models")
- session.commit()
-
- session.expire_all()
- # Asset and ref must still exist — scanner did not see the soft-deleted row
- assert session.get(Asset, "seed1") is not None
- assert session.get(AssetReference, "r1") is not None
+ assert response.status_code == 200
+ assert "asset_references" not in response.json()
diff --git a/tests-unit/assets_test/test_tags_api.py b/tests-unit/assets_test/test_tags_api.py
index 93786696f..e011309bb 100644
--- a/tests-unit/assets_test/test_tags_api.py
+++ b/tests-unit/assets_test/test_tags_api.py
@@ -1,261 +1,68 @@
-import json
-import uuid
+def test_tag_listing_includes_record_tag(http, api_base, asset_factory, make_asset_bytes):
+ record = asset_factory("tagged.png", ["output", "unit-tests", "visible"], {}, make_asset_bytes("tagged"))
-import requests
+ response = http.get(f"{api_base}/api/assets", params={"include_tags": "visible"})
+ assert response.status_code == 200
+ assert [asset["id"] for asset in response.json()["assets"]] == [record["id"]]
-def test_tags_present(http: requests.Session, api_base: str, seeded_asset: dict):
- # Include zero-usage tags by default
- r1 = http.get(api_base + "/api/tags", params={"limit": "50"}, timeout=120)
- body1 = r1.json()
- assert r1.status_code == 200
- names = [t["name"] for t in body1["tags"]]
- # A few selected contract tags should exist.
- assert "models" in names
- assert "model_type:checkpoints" in names
- # Only used tags before we add anything new from this test cycle
- r2 = http.get(api_base + "/api/tags", params={"include_zero": "false"}, timeout=120)
- body2 = r2.json()
- assert r2.status_code == 200
- # We already seeded one asset via fixture, so used tags must be non-empty
- used_names = [t["name"] for t in body2["tags"]]
- assert "models" in used_names
- assert "model_type:checkpoints" in used_names
-
- # Prefix filter should refine the list
- r3 = http.get(api_base + "/api/tags", params={"include_zero": "false", "prefix": "uni"}, timeout=120)
- b3 = r3.json()
- assert r3.status_code == 200
- names3 = [t["name"] for t in b3["tags"]]
- assert "unit-tests" in names3
- assert "models" not in names3 # filtered out by prefix
-
- # Order by name ascending should be stable
- r4 = http.get(api_base + "/api/tags", params={"include_zero": "false", "order": "name_asc"}, timeout=120)
- b4 = r4.json()
- assert r4.status_code == 200
- names4 = [t["name"] for t in b4["tags"]]
- assert names4 == sorted(names4)
-
-
-def test_tags_empty_usage(http: requests.Session, api_base: str, asset_factory, make_asset_bytes):
- # Baseline: system tags exist when include_zero (default) is true
- r1 = http.get(api_base + "/api/tags", params={"limit": "500"}, timeout=120)
- body1 = r1.json()
- assert r1.status_code == 200
- names = [t["name"] for t in body1["tags"]]
- assert "models" in names and "model_type:checkpoints" in names
-
- # Create a short-lived asset under input with a unique custom tag
- scope = f"tags-empty-usage-{uuid.uuid4().hex[:6]}"
- custom_tag = f"temp-{uuid.uuid4().hex[:8]}"
- name = "tag_seed.bin"
- _asset = asset_factory(
- name,
- ["input", "unit-tests", scope, custom_tag],
- {},
- make_asset_bytes(name, 512),
+def test_tag_refine_rejects_metadata_filter(http, api_base):
+ response = http.get(
+ f"{api_base}/api/assets/tags/refine",
+ params={"metadata_filter": '{"k":"v"}'},
)
- # While the asset exists, the custom tag must appear when include_zero=false
- r2 = http.get(
- api_base + "/api/tags",
- params={"include_zero": "false", "prefix": custom_tag, "limit": "50"},
- timeout=120,
- )
- body2 = r2.json()
- assert r2.status_code == 200
- used_names = [t["name"] for t in body2["tags"]]
- assert custom_tag in used_names
-
- # Delete the asset reference so the tag usage drops to zero
- rd = http.delete(f"{api_base}/api/assets/{_asset['id']}", timeout=120)
- assert rd.status_code == 204
-
- # Now the custom tag must not be returned when include_zero=false
- r3 = http.get(
- api_base + "/api/tags",
- params={"include_zero": "false", "prefix": custom_tag, "limit": "50"},
- timeout=120,
- )
- body3 = r3.json()
- assert r3.status_code == 200
- names_after = [t["name"] for t in body3["tags"]]
- assert custom_tag not in names_after
- assert not names_after # filtered view should be empty now
-
-
-def test_add_and_remove_tags(http: requests.Session, api_base: str, seeded_asset: dict):
- aid = seeded_asset["id"]
-
- # Add tags with duplicates while preserving source case.
- payload_add = {"tags": ["NewTag", "unit-tests", "NewTag", "BETA"]}
- r1 = http.post(f"{api_base}/api/assets/{aid}/tags", json=payload_add, timeout=120)
- b1 = r1.json()
- assert r1.status_code == 200, b1
- # stripped, deduplicated; 'unit-tests' was already present from the seed
- assert set(b1["added"]) == {"NewTag", "BETA"}
- assert set(b1["already_present"]) == {"unit-tests"}
- assert "NewTag" in b1["total_tags"] and "BETA" in b1["total_tags"]
-
- rg = http.get(f"{api_base}/api/assets/{aid}", timeout=120)
- g = rg.json()
- assert rg.status_code == 200
- tags_now = set(g["tags"])
- assert {"NewTag", "BETA"}.issubset(tags_now)
+ assert response.status_code == 400
+ assert response.json() == {
+ "error": {
+ "code": "UNSUPPORTED_PARAM",
+ "message": "metadata_filter is no longer supported",
+ "details": {},
+ }
+ }
- # Remove a tag and a non-existent tag
- payload_del = {"tags": ["NewTag", "does-not-exist"]}
- r2 = http.delete(f"{api_base}/api/assets/{aid}/tags", json=payload_del, timeout=120)
- b2 = r2.json()
- assert r2.status_code == 200
- assert set(b2["removed"]) == {"NewTag"}
- assert set(b2["not_present"]) == {"does-not-exist"}
- # Verify remaining tags after deletion
- rg2 = http.get(f"{api_base}/api/assets/{aid}", timeout=120)
- g2 = rg2.json()
- assert rg2.status_code == 200
- tags_later = set(g2["tags"])
- assert "NewTag" not in tags_later
- assert "BETA" in tags_later # still present
+def _expected_system_tag_envelope() -> dict:
+ return {
+ "error": {
+ "code": "SYSTEM_TAG_FORBIDDEN",
+ "message": "Tag 'missing' is system-managed and cannot be modified via the API",
+ "details": {"tag": "missing"},
+ }
+ }
-def test_add_system_looking_tags_allowed_as_labels(
- http: requests.Session, api_base: str, seeded_asset: dict
+def test_add_system_tag_returns_the_error_envelope(
+ http, api_base, asset_factory, make_asset_bytes
):
- aid = seeded_asset["id"]
+ record = asset_factory("sys-add.png", ["output"], {}, make_asset_bytes("sys-add"))
response = http.post(
- f"{api_base}/api/assets/{aid}/tags",
- json={
- "tags": [
- "models",
- "model_type:manual",
- "model:true",
- "models:foo",
- "input:true",
- "output:true",
- "uploaded:true",
- "temp:true",
- "temporary",
- ]
- },
+ f"{api_base}/api/assets/{record['id']}/tags",
+ json={"tags": ["missing"]},
timeout=120,
)
- body = response.json()
- assert response.status_code == 200, body
- assert "models" in body["total_tags"]
- assert "model_type:manual" in body["total_tags"]
- assert "model:true" in body["total_tags"]
- assert "models:foo" in body["total_tags"]
- assert "input:true" in body["total_tags"]
- assert "output:true" in body["total_tags"]
- assert "uploaded:true" in body["total_tags"]
- assert "temp:true" in body["total_tags"]
- assert "temporary" in body["total_tags"]
-
-
-def test_tags_list_order_and_prefix(http: requests.Session, api_base: str, seeded_asset: dict):
- aid = seeded_asset["id"]
- h = seeded_asset["asset_hash"]
-
- # Add both tags to the seeded asset (usage: orderaaa=1, orderbbb=1)
- r_add = http.post(f"{api_base}/api/assets/{aid}/tags", json={"tags": ["orderaaa", "orderbbb"]}, timeout=120)
- add_body = r_add.json()
- assert r_add.status_code == 200, add_body
-
- # Create another AssetInfo from the same content but tagged ONLY with 'orderbbb'.
- payload = {
- "hash": h,
- "name": "order_only_bbb.safetensors",
- "tags": ["input", "unit-tests", "orderbbb"],
- "user_metadata": {},
- }
- r_copy = http.post(f"{api_base}/api/assets/from-hash", json=payload, timeout=120)
- copy_body = r_copy.json()
- assert r_copy.status_code == 201, copy_body
-
- # 1) Default order (count_desc): 'orderbbb' should come before 'orderaaa'
- # because it has higher usage (2 vs 1).
- r1 = http.get(api_base + "/api/tags", params={"prefix": "order", "include_zero": "false"}, timeout=120)
- b1 = r1.json()
- assert r1.status_code == 200, b1
- names1 = [t["name"] for t in b1["tags"]]
- counts1 = {t["name"]: t["count"] for t in b1["tags"]}
- # Both must be present within the prefix subset
- assert "orderaaa" in names1 and "orderbbb" in names1
- # Usage of 'orderbbb' must be >= 'orderaaa'; in our setup it's 2 vs 1
- assert counts1["orderbbb"] >= counts1["orderaaa"]
- # And with count_desc, 'orderbbb' appears earlier than 'orderaaa'
- assert names1.index("orderbbb") < names1.index("orderaaa")
-
- # 2) name_asc: lexical order should flip the relative order
- r2 = http.get(
- api_base + "/api/tags",
- params={"prefix": "order", "include_zero": "false", "order": "name_asc"},
- timeout=120,
+ assert response.status_code == 400
+ assert response.headers["Content-Type"].startswith("application/json"), (
+ "a rejection clients must parse cannot be text/plain while every neighbouring "
+ "error on this route is the JSON envelope"
)
- b2 = r2.json()
- assert r2.status_code == 200, b2
- names2 = [t["name"] for t in b2["tags"]]
- assert "orderaaa" in names2 and "orderbbb" in names2
- assert names2.index("orderaaa") < names2.index("orderbbb")
+ assert response.json() == _expected_system_tag_envelope()
- # 3) invalid limit rejected (existing negative case retained)
- r3 = http.get(api_base + "/api/tags", params={"limit": "1001"}, timeout=120)
- b3 = r3.json()
- assert r3.status_code == 400
- assert b3["error"]["code"] == "INVALID_QUERY"
+def test_delete_system_tag_returns_the_error_envelope(
+ http, api_base, asset_factory, make_asset_bytes
+):
+ record = asset_factory("sys-del.png", ["output"], {}, make_asset_bytes("sys-del"))
-def test_tags_endpoints_invalid_bodies(http: requests.Session, api_base: str, seeded_asset: dict):
- aid = seeded_asset["id"]
-
- # Add with empty list
- r1 = http.post(f"{api_base}/api/assets/{aid}/tags", json={"tags": []}, timeout=120)
- b1 = r1.json()
- assert r1.status_code == 400
- assert b1["error"]["code"] == "INVALID_BODY"
-
- # Remove with wrong type
- r2 = http.delete(f"{api_base}/api/assets/{aid}/tags", json={"tags": [123]}, timeout=120)
- b2 = r2.json()
- assert r2.status_code == 400
- assert b2["error"]["code"] == "INVALID_BODY"
-
- # metadata_filter provided as JSON array should be rejected (must be object)
- r3 = http.get(
- api_base + "/api/assets",
- params={"metadata_filter": json.dumps([{"x": 1}])},
+ response = http.delete(
+ f"{api_base}/api/assets/{record['id']}/tags",
+ json={"tags": ["missing"]},
timeout=120,
)
- b3 = r3.json()
- assert r3.status_code == 400
- assert b3["error"]["code"] == "INVALID_QUERY"
-
-
-def test_tags_prefix_treats_underscore_literal(
- http,
- api_base,
- asset_factory,
- make_asset_bytes,
-):
- """'prefix' for /api/tags must treat '_' literally, not as a wildcard."""
- base = f"pref_{uuid.uuid4().hex[:6]}"
- tag_ok = f"{base}_ok" # should match prefix=f"{base}_"
- tag_bad = f"{base}xok" # must NOT match if '_' is escaped
- scope = f"tags-underscore-{uuid.uuid4().hex[:6]}"
-
- asset_factory("t1.bin", ["input", "unit-tests", scope, tag_ok], {}, make_asset_bytes("t1", 512))
- asset_factory("t2.bin", ["input", "unit-tests", scope, tag_bad], {}, make_asset_bytes("t2", 512))
- r = http.get(api_base + "/api/tags", params={"include_zero": "false", "prefix": f"{base}_"}, timeout=120)
- body = r.json()
- assert r.status_code == 200, body
- names = [t["name"] for t in body["tags"]]
- assert tag_ok in names, f"Expected {tag_ok} to be returned for prefix '{base}_'"
- assert tag_bad not in names, f"'{tag_bad}' must not match — '_' is not a wildcard"
- assert body["total"] == 1
+ assert response.status_code == 400
+ assert response.headers["Content-Type"].startswith("application/json")
+ assert response.json() == _expected_system_tag_envelope()
diff --git a/tests-unit/assets_test/test_temp_assets.py b/tests-unit/assets_test/test_temp_assets.py
index 8a5f77d52..9d9ebce94 100644
--- a/tests-unit/assets_test/test_temp_assets.py
+++ b/tests-unit/assets_test/test_temp_assets.py
@@ -1,180 +1,7 @@
+import folder_paths
-import os
-import tempfile
-from pathlib import Path
-from unittest.mock import patch
+from app.assets.services.lookup import is_temp_path
-import pytest
-from sqlalchemy import create_engine
-from sqlalchemy.orm import Session
-from app.assets.database.models import Asset, AssetReference, Base
-from app.assets.database.queries.asset_reference import (
- mark_references_missing_outside_prefixes,
-)
-from app.assets.scanner import (
- collect_paths_for_roots,
- get_owned_prefixes,
- get_temp_prefixes,
- sync_prefixes_with_filesystem,
-)
-from app.assets.services.file_utils import get_mtime_ns
-
-
[email protected](autouse=True)
-def autoclean_unit_test_assets():
- """Override parent autouse fixture - temp asset tests don't need server cleanup."""
- yield
-
-
-def session():
- engine = create_engine("sqlite:///:memory:")
- Base.metadata.create_all(engine)
- with Session(engine) as sess:
- yield sess
-
-
-def comfy_dirs():
- with tempfile.TemporaryDirectory() as base:
- dirs = {
- name: Path(base) / name
- for name in ("models", "input", "output", "temp", "elsewhere")
- }
- for d in dirs.values():
- d.mkdir()
- with (
- patch("folder_paths.get_input_directory", return_value=str(dirs["input"])),
- patch("folder_paths.get_output_directory", return_value=str(dirs["output"])),
- patch("folder_paths.get_temp_directory", return_value=str(dirs["temp"])),
- patch(
- "app.assets.scanner.get_comfy_models_folders",
- return_value=[("checkpoints", [str(dirs["models"])], set())],
- ),
- ):
- yield dirs
-
-
-def _write(directory: Path, name: str) -> str:
- p = directory / name
- p.write_bytes(b"\x00" * 100)
- return str(p)
-
-
-def _register(
- session: Session,
- file_path: str,
- ref_id: str,
- *,
- mtime_ns: int,
- asset_hash: str | None = "",
-) -> None:
- if asset_hash == "":
- asset_hash = f"blake3:{ref_id}"
- session.add(Asset(id=f"asset-{ref_id}", hash=asset_hash, size_bytes=100))
- session.flush()
- session.add(
- AssetReference(
- id=ref_id,
- asset_id=f"asset-{ref_id}",
- name=os.path.basename(file_path),
- owner_id="",
- file_path=file_path,
- mtime_ns=mtime_ns,
- )
- )
- session.flush()
-
-
-def _mtime(path: str) -> int:
- return get_mtime_ns(os.stat(path, follow_symlinks=True))
-
-
-def test_owned_prefixes_include_temp(comfy_dirs):
- owned = get_owned_prefixes()
- assert str(comfy_dirs["temp"]) in owned, (
- "temp must be owned, or the prune disowns assets whose files are present"
- )
- for name in ("models", "input", "output"):
- assert str(comfy_dirs[name]) in owned, f"{name} must stay owned"
-
-
-def test_discovery_does_not_walk_temp(comfy_dirs):
- temp_file = _write(comfy_dirs["temp"], "preview.png")
- output_file = _write(comfy_dirs["output"], "render.png")
-
- with patch("app.assets.scanner.collect_models_files", return_value=[]):
- paths = collect_paths_for_roots(("models", "input", "output"))
-
- assert output_file in paths, "scan roots must still be walked"
- assert temp_file not in paths, (
- "temp is wiped before every scan, so walking it only ever finds nothing"
- )
-
-
-def test_prune_keeps_live_temp_reference(session, comfy_dirs):
- temp_file = _write(comfy_dirs["temp"], "preview.png")
- stray_file = _write(comfy_dirs["elsewhere"], "stray.png")
- _register(session, temp_file, "temp-ref", mtime_ns=_mtime(temp_file))
- _register(session, stray_file, "stray-ref", mtime_ns=_mtime(stray_file))
- session.commit()
-
- marked = mark_references_missing_outside_prefixes(session, get_owned_prefixes())
- session.commit()
-
- session.expire_all()
- assert marked == 1, "only the reference outside every owned directory is disowned"
- assert session.get(AssetReference, "temp-ref").is_missing is False, (
- "a temp file on disk is not missing, however often the prune runs"
- )
- assert session.get(AssetReference, "stray-ref").is_missing is True, (
- "owning temp must not stop the prune disowning files elsewhere"
- )
-
-
-def test_temp_sync_marks_deleted_file_missing(session, comfy_dirs):
- temp_file = _write(comfy_dirs["temp"], "preview.png")
- _register(session, temp_file, "temp-ref", mtime_ns=_mtime(temp_file))
- session.commit()
- os.remove(temp_file)
-
- sync_prefixes_with_filesystem(session, get_temp_prefixes())
- session.commit()
-
- session.expire_all()
- assert session.get(AssetReference, "temp-ref").is_missing is True, (
- "nothing else stats temp, so this pass is what retires a wiped file"
- )
-
-
-def test_temp_sync_drops_unhashed_asset_whose_file_is_gone(session, comfy_dirs):
- temp_file = _write(comfy_dirs["temp"], "preview.png")
- _register(session, temp_file, "temp-ref", mtime_ns=_mtime(temp_file), asset_hash=None)
- session.commit()
- os.remove(temp_file)
-
- sync_prefixes_with_filesystem(session, get_temp_prefixes())
- session.commit()
-
- session.expire_all()
- assert session.get(AssetReference, "temp-ref") is None, (
- "an unhashed asset with no surviving reference is retired, not kept as missing"
- )
- assert session.get(Asset, "asset-temp-ref") is None, (
- "the orphaned asset row goes with its last reference"
- )
-
-
-def test_temp_sync_keeps_live_file(session, comfy_dirs):
- temp_file = _write(comfy_dirs["temp"], "preview.png")
- _register(session, temp_file, "temp-ref", mtime_ns=_mtime(temp_file))
- session.commit()
-
- sync_prefixes_with_filesystem(session, get_temp_prefixes())
- session.commit()
-
- session.expire_all()
- ref = session.get(AssetReference, "temp-ref")
- assert ref.is_missing is False, "the file is still there"
- assert ref.needs_verify is False, "an unchanged file needs no re-verification"
+def test_temp_content_is_detected_by_its_location():
+ assert is_temp_path(f"{folder_paths.get_temp_directory()}/render.png") is True
diff --git a/tests-unit/assets_test/test_upload_hashing_modes.py b/tests-unit/assets_test/test_upload_hashing_modes.py
new file mode 100644
index 000000000..3c251c623
--- /dev/null
+++ b/tests-unit/assets_test/test_upload_hashing_modes.py
@@ -0,0 +1,249 @@
+from __future__ import annotations
+
+import os
+import json
+import sqlite3
+import time
+import uuid
+from contextlib import contextmanager
+from pathlib import Path
+from types import SimpleNamespace
+from typing import Any
+from unittest.mock import AsyncMock
+
+import pytest
+import requests
+from aiohttp import web
+from sqlalchemy import create_engine, select
+from sqlalchemy.orm import Session as SASession
+
+import app.assets.mode as mode_module
+import folder_paths
+from app.assets.api import routes, schemas_in
+from app.assets.database.models import AssetContent, Base
+from app.assets.services.ingest import register_executed_output
+
+from .helpers import trigger_sync_seed_assets
+
+
+def _db_path(comfy_tmp_base_dir: Path, request: pytest.FixtureRequest) -> str:
+ url = request.config.getoption("--db-url")
+ if url and url.startswith("sqlite:///"):
+ return url[len("sqlite:///"):]
+ return str(comfy_tmp_base_dir / "assets-test.sqlite3")
+
+
+def _query(db_path: str, sql: str, params: tuple[Any, ...] = ()) -> list[tuple[Any, ...]]:
+ last_err: Exception | None = None
+ for _ in range(20):
+ try:
+ con = sqlite3.connect(f"file:{db_path}?mode=ro", uri=True, timeout=5)
+ try:
+ return list(con.execute(sql, params))
+ finally:
+ con.close()
+ except sqlite3.OperationalError as e:
+ last_err = e
+ time.sleep(0.1)
+ raise AssertionError(f"sqlite read failed: {last_err}")
+
+
+def _content_hash_for_asset(db_path: str, asset_id: str) -> str | None:
+ rows = _query(
+ db_path,
+ "SELECT c.hash FROM asset_contents c "
+ "JOIN assets a ON a.content_id = c.id "
+ "WHERE a.id = ?",
+ (asset_id,),
+ )
+ assert rows, f"no content row found for asset {asset_id}"
+ return rows[0][0]
+
+
+def _content_id_for_asset(db_path: str, asset_id: str) -> str:
+ rows = _query(db_path, "SELECT content_id FROM assets WHERE id = ?", (asset_id,))
+ assert rows, f"no asset row found for {asset_id}"
+ return rows[0][0]
+
+
+def _is_stored_blake3_hash(value: str | None) -> bool:
+ if not isinstance(value, str) or not value.startswith("blake3:"):
+ return False
+ digest = value[len("blake3:") :]
+ return len(digest) == 64 and all(c in "0123456789abcdef" for c in digest.lower())
+
+
+def _upload_via_api(
+ http: requests.Session, base: str, *, name: str, tags: list[str], data: bytes
+) -> tuple[int, dict[str, Any]]:
+ files = {"file": (name, data, "application/octet-stream")}
+ form = {"tags": json.dumps(tags), "name": name, "user_metadata": json.dumps({})}
+ r = http.post(base + "/api/assets", files=files, data=form, timeout=120)
+ return r.status_code, r.json()
+
+
+def _upload_via_image(
+ http: requests.Session, base: str, *, name: str, data: bytes, upload_type: str = "input"
+) -> tuple[int, dict[str, Any]]:
+ files = {"image": (name, data, "image/png")}
+ form = {"type": upload_type, "subfolder": ""}
+ r = http.post(base + "/upload/image", files=files, data=form, timeout=120)
+ return r.status_code, r.json()
+
+
+def _unique_bytes(seed: str, size: int = 4096) -> bytes:
+ return uuid.uuid4().bytes + seed.encode("utf-8").ljust(size, b"\0")[: size - 16]
+
+
+async def test_hash_only_multipart_upload_off_mode_returns_400(monkeypatch):
+ hash_value = f"blake3:{'a' * 64}"
+ parsed = schemas_in.ParsedUpload(
+ file_present=False,
+ file_written=0,
+ file_client_name=None,
+ tmp_path=None,
+ tags_raw=[],
+ provided_name=None,
+ user_metadata_raw=None,
+ provided_hash=hash_value,
+ provided_hash_exists=True,
+ )
+ monkeypatch.setattr(mode_module, "hashing_enabled", lambda: False)
+ monkeypatch.setattr(routes, "_ASSETS_ENABLED", True)
+ monkeypatch.setattr(
+ routes, "parse_multipart_upload", AsyncMock(return_value=parsed)
+ )
+ monkeypatch.setattr(
+ routes,
+ "USER_MANAGER",
+ SimpleNamespace(get_request_user_id=lambda _request: "test-user"),
+ )
+
+ response = await routes.upload_asset(AsyncMock(spec=web.Request))
+
+ assert isinstance(response, web.Response)
+ assert response.status == 400
+ response_body = response.body
+ assert isinstance(response_body, bytes | bytearray)
+ assert json.loads(response_body)["error"]["code"] == "FEATURE_DISABLED"
+
+
[email protected](scope="session")
+def server_hashing_enabled(request: pytest.FixtureRequest) -> bool:
+ markexpr = request.config.getoption("markexpr") or ""
+ return bool(
+ request.config.getoption("--enable-asset-hashing")
+ or "hashing_on" in markexpr
+ or any(item.get_closest_marker("hashing_on") for item in request.session.items)
+ )
+
+
+def test_upload_via_api_hashes_in_on_mode(http, api_base, comfy_tmp_base_dir, request):
+ db_path = _db_path(comfy_tmp_base_dir, request)
+ data = _unique_bytes("api-hashes")
+ status, body = _upload_via_api(
+ http, api_base, name="api_hashes.bin", tags=["output", "unit-tests"], data=data
+ )
+ assert status in (200, 201), body
+ digest = _content_hash_for_asset(db_path, body["id"])
+ assert _is_stored_blake3_hash(digest), f"expected a stored blake3 hash, got {digest!r}"
+
+
+def test_upload_via_image_hashes_in_on_mode(http, api_base, comfy_tmp_base_dir, request):
+ db_path = _db_path(comfy_tmp_base_dir, request)
+ data = _unique_bytes("image-hashes")
+ status, body = _upload_via_image(http, api_base, name="image_hashes.png", data=data)
+ assert status == 200, body
+ asset = body.get("asset")
+ assert asset and asset.get("id"), f"/upload/image did not register an asset: {body}"
+ digest = _content_hash_for_asset(db_path, asset["id"])
+ assert _is_stored_blake3_hash(digest), f"expected a stored blake3 hash, got {digest!r}"
+
+
+def test_repeat_upload_mints_a_new_record_in_on_mode(
+ http, api_base, comfy_tmp_base_dir, request
+):
+ db_path = _db_path(comfy_tmp_base_dir, request)
+ data = _unique_bytes("dedup-on-mode")
+ status1, first = _upload_via_api(
+ http, api_base, name="dedup_on.bin", tags=["output", "unit-tests"], data=data
+ )
+ status2, second = _upload_via_api(
+ http, api_base, name="dedup_on.bin", tags=["output", "unit-tests"], data=data
+ )
+ assert status1 == 201, first
+ assert status2 == 201, second
+ assert second["id"] != first["id"], "every upload is its own delivery record"
+ assert second.get("created_new") is True
+ assert _content_id_for_asset(db_path, second["id"]) == _content_id_for_asset(
+ db_path, first["id"]
+ ), "same bytes stay one content row (hashes alone can collide across rows)"
+
+
+def test_seeded_file_not_hashed_in_on_mode(
+ http, api_base, comfy_tmp_base_dir, request, server_hashing_enabled
+):
+ if server_hashing_enabled:
+ pytest.skip(
+ "scanner hashing gate only observable with the hashing flag OFF; "
+ "a full-suite run forces the shared server into hash mode"
+ )
+ db_path = _db_path(comfy_tmp_base_dir, request)
+ ckpt_dir = comfy_tmp_base_dir / "models" / "checkpoints"
+ ckpt_dir.mkdir(parents=True, exist_ok=True)
+ disk_path = ckpt_dir / f"seeded_{uuid.uuid4().hex}.safetensors"
+ disk_path.write_bytes(uuid.uuid4().bytes + b"S" * (4096 - 16))
+
+ row_hash: str | None = None
+ for _ in range(5):
+ trigger_sync_seed_assets(http, api_base)
+ rows = _query(
+ db_path,
+ "SELECT hash FROM asset_contents WHERE path = ? AND is_missing = 0",
+ (str(disk_path),),
+ )
+ if rows:
+ row_hash = rows[0][0]
+ break
+ else:
+ raise AssertionError(f"seeded file never produced a content row: {disk_path}")
+
+ assert row_hash is None, "scanner must not hash seeded files while the flag is off"
+
+
+def test_output_not_hashed_in_on_mode(monkeypatch):
+ engine = create_engine("sqlite:///:memory:")
+ Base.metadata.create_all(engine)
+
+ @contextmanager
+ def _fake_create_session():
+ with SASession(engine) as session:
+ yield session
+
+ monkeypatch.setattr(mode_module, "hashing_enabled", lambda: False)
+ monkeypatch.setattr(
+ "app.assets.services.ingest.create_session", _fake_create_session
+ )
+
+ output_dir = folder_paths.get_output_directory()
+ os.makedirs(output_dir, exist_ok=True)
+ out_path = os.path.join(output_dir, f"render_{uuid.uuid4().hex}.png")
+ with open(out_path, "wb") as fh:
+ fh.write(b"fake-output-bytes")
+
+ try:
+ register_executed_output(out_path, job_id="job-out")
+ with SASession(engine) as session:
+ content = session.execute(
+ select(AssetContent).where(
+ AssetContent.path == os.path.abspath(out_path)
+ )
+ ).scalar_one()
+ assert content.hash is None, (
+ "workflow output must not be hashed while the hashing flag is off"
+ )
+ assert content.is_missing is False
+ finally:
+ if os.path.exists(out_path):
+ os.unlink(out_path)
diff --git a/tests-unit/assets_test/test_uploads.py b/tests-unit/assets_test/test_uploads.py
index 7be7b0935..311334547 100644
--- a/tests-unit/assets_test/test_uploads.py
+++ b/tests-unit/assets_test/test_uploads.py
@@ -1,672 +1,48 @@
import json
-import uuid
-from concurrent.futures import ThreadPoolExecutor
-from pathlib import Path
-import requests
import pytest
-from app.assets.api.schemas_in import UploadAssetSpec
-from app.assets.api.schemas_out import Asset, AssetCreated
-from helpers import get_asset_filename
+def test_hash_on_upload_mints_a_record_per_upload(asset_factory, make_asset_bytes):
+ data = make_asset_bytes("hash-on-dedup")
-def test_asset_created_inherits_hash_field():
- """AssetCreated must inherit `hash` from Asset so POST /api/assets responses emit it.
+ first = asset_factory("dedup.png", ["output", "unit-tests"], {}, data)
+ second = asset_factory("dedup.png", ["output", "unit-tests"], {}, data)
- Schema-level guard: integration tests cover the wire shape, but inheritance
- drift (e.g. AssetCreated ever being redefined to no longer extend Asset)
- would silently drop `hash` from a major endpoint without this check.
- """
- assert "hash" in Asset.model_fields
- assert "hash" in AssetCreated.model_fields
- assert AssetCreated.model_fields["hash"].annotation == Asset.model_fields["hash"].annotation
+ assert second["id"] != first["id"], "every upload is its own delivery record"
+ assert second["created_new"] is True
-def test_upload_asset_spec_ignores_subfolder_field():
- spec = UploadAssetSpec.model_validate(
- {"tags": ["input"], "subfolder": "pasted", "name": "image.png"}
- )
-
- assert "subfolder" not in UploadAssetSpec.model_fields
- assert not hasattr(spec, "subfolder")
-
-
-def test_upload_ok_duplicate_reference(http: requests.Session, api_base: str, make_asset_bytes):
- name = "dup_a.safetensors"
- tags = ["models", "model_type:checkpoints", "unit-tests", "alpha"]
- meta = {"purpose": "dup"}
- data = make_asset_bytes(name)
- files = {"file": (name, data, "application/octet-stream")}
- form = {"tags": json.dumps(tags), "name": name, "user_metadata": json.dumps(meta)}
- r1 = http.post(api_base + "/api/assets", data=form, files=files, timeout=120)
- a1 = r1.json()
- assert r1.status_code == 201, a1
- assert a1["created_new"] is True
- assert a1["hash"] == a1["asset_hash"]
-
- # Second upload with the same data and name creates a new AssetReference (duplicates allowed)
- # Returns 200 because Asset already exists, but a new AssetReference is created
- files = {"file": (name, data, "application/octet-stream")}
- form = {"tags": json.dumps(tags), "name": name, "user_metadata": json.dumps(meta)}
- r2 = http.post(api_base + "/api/assets", data=form, files=files, timeout=120)
- a2 = r2.json()
- assert r2.status_code in (200, 201), a2
- assert a2["asset_hash"] == a1["asset_hash"]
- assert a2["hash"] == a1["hash"]
- assert a2["id"] != a1["id"] # new reference with same content
- assert a2.get("loader_path") is None
- assert a2.get("display_name") is None
-
- # Third upload with the same data but different name also creates new AssetReference
- files = {"file": (name, data, "application/octet-stream")}
- form = {"tags": json.dumps(tags), "name": name + "_d", "user_metadata": json.dumps(meta)}
- r3 = http.post(api_base + "/api/assets", data=form, files=files, timeout=120)
- a3 = r3.json()
- assert r3.status_code in (200, 201), a3
- assert a3["asset_hash"] == a1["asset_hash"]
- assert a3["id"] != a1["id"]
- assert a3["id"] != a2["id"]
- assert a3.get("loader_path") is None
- assert a3.get("display_name") is None
-
-
-def test_upload_fastpath_from_existing_hash_no_file(http: requests.Session, api_base: str):
- # Seed a small file first
- name = "fastpath_seed.safetensors"
- tags = ["input", "unit-tests"]
- meta = {}
- files = {"file": (name, b"B" * 1024, "application/octet-stream")}
- form = {"tags": json.dumps(tags), "name": name, "user_metadata": json.dumps(meta)}
- r1 = http.post(api_base + "/api/assets", data=form, files=files, timeout=120)
- b1 = r1.json()
- assert r1.status_code == 201, b1
- h = b1["asset_hash"]
- assert b1["hash"] == h
-
- # Now POST /api/assets with only hash and no file
- hash_only_tags = ["models", "checkpoints", "unit-tests", "hash-labels"]
- files = [
- ("hash", (None, h)),
- ("tags", (None, json.dumps(hash_only_tags))),
- ("name", (None, "fastpath_copy.safetensors")),
- ("user_metadata", (None, json.dumps({"purpose": "copy"}))),
- ]
- r2 = http.post(api_base + "/api/assets", files=files, timeout=120)
- b2 = r2.json()
- assert r2.status_code == 200, b2 # fast path returns 200 with created_new == False
- assert b2["created_new"] is False
- assert b2["asset_hash"] == h
- assert b2["hash"] == h
- assert "models" in b2["tags"]
- assert "checkpoints" in b2["tags"]
- assert "uploaded" not in b2["tags"]
- assert not any(tag.startswith("model_type:") for tag in b2["tags"])
- assert b2.get("loader_path") is None
- assert b2.get("display_name") is None
-
- rg = http.get(f"{api_base}/api/assets/{b2['id']}", timeout=120)
- detail = rg.json()
- assert rg.status_code == 200, detail
- assert detail.get("loader_path") is None
- assert detail.get("display_name") is None
-
-
-def test_create_from_hash_with_model_tags_does_not_synthesize_loader_path(
- http: requests.Session, api_base: str
-):
- seed_name = "from_hash_seed.safetensors"
- seed_tags = ["models", "model_type:checkpoints", "unit-tests"]
- files = {"file": (seed_name, b"D" * 1024, "application/octet-stream")}
- form = {
- "tags": json.dumps(seed_tags),
- "name": seed_name,
- "user_metadata": json.dumps({}),
- }
- seed_r = http.post(api_base + "/api/assets", data=form, files=files, timeout=120)
- seed = seed_r.json()
- assert seed_r.status_code == 201, seed
-
- payload = {
- "hash": seed["asset_hash"],
- "name": "from_hash_copy.safetensors",
- "tags": ["models", "model_type:checkpoints", "unit-tests", "spoofed"],
- }
- created_r = http.post(api_base + "/api/assets/from-hash", json=payload, timeout=120)
- created = created_r.json()
- assert created_r.status_code == 201, created
- assert created["created_new"] is False
- assert created["asset_hash"] == seed["asset_hash"]
- assert created.get("loader_path") is None
- assert created.get("display_name") is None
-
- detail_r = http.get(f"{api_base}/api/assets/{created['id']}", timeout=120)
- detail = detail_r.json()
- assert detail_r.status_code == 200, detail
- assert detail.get("loader_path") is None
- assert detail.get("display_name") is None
-
-
-def test_upload_fastpath_with_known_hash_and_file(
- http: requests.Session, api_base: str
-):
- # Seed
- files = {"file": ("seed.safetensors", b"C" * 128, "application/octet-stream")}
- form = {"tags": json.dumps(["models", "model_type:checkpoints", "unit-tests", "fp"]), "name": "seed.safetensors", "user_metadata": json.dumps({})}
- r1 = http.post(api_base + "/api/assets", data=form, files=files, timeout=120)
- b1 = r1.json()
- assert r1.status_code == 201, b1
- h = b1["asset_hash"]
- assert b1["hash"] == h
-
- # Send both file and hash of existing content -> server must drain file and create from hash (200)
- files = {"file": ("ignored.bin", b"ignored" * 10, "application/octet-stream")}
- form = {"hash": h, "tags": json.dumps(["models", "checkpoints", "unit-tests", "fp"]), "name": "copy_from_hash.safetensors", "user_metadata": json.dumps({})}
- r2 = http.post(api_base + "/api/assets", data=form, files=files, timeout=120)
- b2 = r2.json()
- assert r2.status_code == 200, b2
- assert b2["created_new"] is False
- assert b2["asset_hash"] == h
- assert b2["hash"] == h
- assert "checkpoints" in b2["tags"]
- assert "uploaded" not in b2["tags"]
- assert not any(tag == "model_type:checkpoints" for tag in b2["tags"])
-
-
-def test_duplicate_byte_upload_is_reference_only_and_does_not_need_destination(
- http: requests.Session, api_base: str
-):
- data = b"duplicate-reference-only" * 64
- seed_files = {"file": ("duplicate-seed.bin", data, "application/octet-stream")}
- seed_form = {
- "tags": json.dumps(["input", "unit-tests", "duplicate-seed"]),
- "name": "duplicate-seed.bin",
- "user_metadata": json.dumps({}),
- }
- seed_response = http.post(api_base + "/api/assets", data=seed_form, files=seed_files, timeout=120)
- seed = seed_response.json()
- assert seed_response.status_code == 201, seed
-
- duplicate_files = {"file": ("duplicate-copy.bin", data, "application/octet-stream")}
- duplicate_form = {
- "tags": json.dumps(["not-a-destination", "unit-tests", "duplicate-copy"]),
- "name": "duplicate-copy.bin",
- "user_metadata": json.dumps({}),
- }
- duplicate_response = http.post(
- api_base + "/api/assets", data=duplicate_form, files=duplicate_files, timeout=120
- )
- duplicate = duplicate_response.json()
-
- assert duplicate_response.status_code == 200, duplicate
- assert duplicate["created_new"] is False
- assert duplicate["asset_hash"] == seed["asset_hash"]
- assert "not-a-destination" in duplicate["tags"]
- assert "uploaded" not in duplicate["tags"]
- assert "input" not in duplicate["tags"]
- assert duplicate.get("loader_path") is None
- assert duplicate.get("display_name") is None
-
-
-def test_upload_multiple_tags_fields_are_merged(http: requests.Session, api_base: str):
- data = [
- ("tags", "models,model_type:checkpoints"),
- ("tags", json.dumps(["unit-tests", "alpha"])),
- ("name", "merge.safetensors"),
- ("user_metadata", json.dumps({"u": 1})),
- ]
- files = {"file": ("merge.safetensors", b"B" * 256, "application/octet-stream")}
- r1 = http.post(api_base + "/api/assets", data=data, files=files, timeout=120)
- created = r1.json()
- assert r1.status_code in (200, 201), created
- aid = created["id"]
-
- # Verify all tags are present on the resource
- rg = http.get(f"{api_base}/api/assets/{aid}", timeout=120)
- detail = rg.json()
- assert rg.status_code == 200, detail
- tags = set(detail["tags"])
- assert {"models", "model_type:checkpoints", "unit-tests", "alpha"}.issubset(tags)
-
-
- (
- "tags",
- "extension",
- "expected_display_prefix",
- ),
- [
- (["input", "unit-tests"], ".png", ""),
- (
- ["models", "model_type:checkpoints", "unit-tests"],
- ".safetensors",
- "checkpoints/",
- ),
- ],
-)
-def test_upload_response_includes_loader_path_and_display_name(
- tags: list[str],
- extension: str,
- expected_display_prefix: str,
- http: requests.Session,
- api_base: str,
- make_asset_bytes,
-):
- scope = f"response-paths-{uuid.uuid4().hex[:6]}"
- scoped_tags = [*tags, scope]
- name = f"asset_response_path{extension}"
-
- files = {"file": (name, make_asset_bytes(name, 1024), "application/octet-stream")}
- form = {
- "tags": json.dumps(scoped_tags),
- "name": name,
- "user_metadata": json.dumps({}),
- }
- created_r = http.post(api_base + "/api/assets", data=form, files=files, timeout=120)
- created = created_r.json()
- assert created_r.status_code in (200, 201), created
- stored_filename = get_asset_filename(created["asset_hash"], extension)
- expected_suffix = stored_filename
- expected_display_name = f"{expected_display_prefix}{expected_suffix}"
- # In-root loader path: model category dropped, no subfolders here -> just the filename.
- expected_loader_path = expected_suffix
-
- assert created["loader_path"] == expected_loader_path
- assert created["display_name"] == expected_display_name
- assert "logical_path" not in created
-
- detail_r = http.get(f"{api_base}/api/assets/{created['id']}", timeout=120)
- detail = detail_r.json()
- assert detail_r.status_code == 200, detail
- assert detail["loader_path"] == expected_loader_path
- assert detail["display_name"] == expected_display_name
-
- list_r = http.get(
- api_base + "/api/assets",
- params={"include_tags": f"unit-tests,{scope}", "limit": "50"},
- timeout=120,
- )
- listed = list_r.json()
- assert list_r.status_code == 200, listed
- match = next(a for a in listed["assets"] if a["id"] == created["id"])
- assert match["loader_path"] == expected_loader_path
- assert match["display_name"] == expected_display_name
-
-
[email protected]("root", ["input", "output"])
-def test_concurrent_upload_identical_bytes_different_names(
- root: str,
- http: requests.Session,
- api_base: str,
- make_asset_bytes,
-):
- """
- Two concurrent uploads of identical bytes but different names.
- Expect a single Asset (same hash), two AssetReference rows, and exactly one created_new=True.
- """
- scope = f"concupload-{uuid.uuid4().hex[:6]}"
- name1, name2 = "cu_a.bin", "cu_b.bin"
- data = make_asset_bytes("concurrent", 4096)
- tags = [root, "unit-tests", scope]
-
- def _do_upload(args):
- url, form_data, files_data = args
- with requests.Session() as s:
- return s.post(url, data=form_data, files=files_data, timeout=120)
-
- url = api_base + "/api/assets"
- form1 = {"tags": json.dumps(tags), "name": name1, "user_metadata": json.dumps({})}
- files1 = {"file": (name1, data, "application/octet-stream")}
- form2 = {"tags": json.dumps(tags), "name": name2, "user_metadata": json.dumps({})}
- files2 = {"file": (name2, data, "application/octet-stream")}
-
- with ThreadPoolExecutor(max_workers=2) as executor:
- futures = list(executor.map(_do_upload, [(url, form1, files1), (url, form2, files2)]))
- r1, r2 = futures
-
- b1, b2 = r1.json(), r2.json()
- assert r1.status_code in (200, 201), b1
- assert r2.status_code in (200, 201), b2
- assert b1["asset_hash"] == b2["asset_hash"]
- assert b1["hash"] == b2["hash"]
- assert b1["hash"] == b1["asset_hash"]
- assert b1["id"] != b2["id"]
-
- created_flags = sorted([bool(b1.get("created_new")), bool(b2.get("created_new"))])
- assert created_flags == [False, True]
-
- rl = http.get(
- api_base + "/api/assets",
- params={"include_tags": f"unit-tests,{scope}", "sort": "name"},
- timeout=120,
- )
- bl = rl.json()
- assert rl.status_code == 200, bl
- names = [a["name"] for a in bl.get("assets", [])]
- assert set([name1, name2]).issubset(names)
-
-
-def test_create_from_hash_endpoint_404(http: requests.Session, api_base: str):
- payload = {
- "hash": "blake3:" + "0" * 64,
- "name": "nonexistent.bin",
- "tags": ["models", "checkpoints", "unit-tests"],
- }
- r = http.post(api_base + "/api/assets/from-hash", json=payload, timeout=120)
- body = r.json()
- assert r.status_code == 404
- assert body["error"]["code"] == "ASSET_NOT_FOUND"
-
-
-def test_create_from_hash_accepts_arbitrary_system_looking_tags(
- http: requests.Session, api_base: str
-):
- files = {"file": ("hash-seed.bin", b"hash-seed" * 64, "application/octet-stream")}
+def test_repeat_upload_of_identical_bytes_returns_201(http, api_base, make_asset_bytes):
+ data = make_asset_bytes("route-201-on-repeat")
form = {
- "tags": json.dumps(["input", "unit-tests", "hash-seed"]),
- "name": "hash-seed.bin",
+ "tags": json.dumps(["output", "unit-tests"]),
+ "name": "repeat.png",
"user_metadata": json.dumps({}),
}
- seed_response = http.post(api_base + "/api/assets", data=form, files=files, timeout=120)
- seed = seed_response.json()
- assert seed_response.status_code == 201, seed
-
- response = http.post(
- api_base + "/api/assets/from-hash",
- json={
- "hash": seed["asset_hash"],
- "name": "hash-copy.bin",
- "tags": [
- "models",
- "model:true",
- "models:foo",
- "temporary:true",
- "unit-tests",
- "hash-copy",
- ],
- },
- timeout=120,
- )
- body = response.json()
-
- assert response.status_code == 201, body
- assert "models" in body["tags"]
- assert "model:true" in body["tags"]
- assert "models:foo" in body["tags"]
- assert "temporary:true" in body["tags"]
- assert "uploaded" not in body["tags"]
-
-
-def test_upload_zero_byte_rejected(http: requests.Session, api_base: str):
- files = {"file": ("empty.safetensors", b"", "application/octet-stream")}
- form = {"tags": json.dumps(["models", "model_type:checkpoints", "unit-tests", "edge"]), "name": "empty.safetensors", "user_metadata": json.dumps({})}
- r = http.post(api_base + "/api/assets", data=form, files=files, timeout=120)
- body = r.json()
- assert r.status_code == 400
- assert body["error"]["code"] == "EMPTY_UPLOAD"
-
-
-def test_upload_rejects_arbitrary_labels_without_required_destination_role(http: requests.Session, api_base: str):
- files = {"file": ("badroot.bin", b"A" * 64, "application/octet-stream")}
- form = {"tags": json.dumps(["not-a-root", "whatever"]), "name": "badroot.bin", "user_metadata": json.dumps({})}
- r = http.post(api_base + "/api/assets", data=form, files=files, timeout=120)
- body = r.json()
- assert r.status_code == 400
- assert body["error"]["code"] == "INVALID_BODY"
-
-
-def test_upload_user_metadata_must_be_json(http: requests.Session, api_base: str):
- files = {"file": ("badmeta.bin", b"A" * 128, "application/octet-stream")}
- form = {"tags": json.dumps(["models", "model_type:checkpoints", "unit-tests", "edge"]), "name": "badmeta.bin", "user_metadata": "{not json}"}
- r = http.post(api_base + "/api/assets", data=form, files=files, timeout=120)
- body = r.json()
- assert r.status_code == 400
- assert body["error"]["code"] == "INVALID_BODY"
-
-
-def test_upload_requires_multipart(http: requests.Session, api_base: str):
- r = http.post(api_base + "/api/assets", json={"foo": "bar"}, timeout=120)
- body = r.json()
- assert r.status_code == 415
- assert body["error"]["code"] == "UNSUPPORTED_MEDIA_TYPE"
-
-
-def test_upload_missing_file_and_hash(http: requests.Session, api_base: str):
- files = [
- ("tags", (None, json.dumps(["models", "model_type:checkpoints", "unit-tests"]))),
- ("name", (None, "x.safetensors")),
- ]
- r = http.post(api_base + "/api/assets", files=files, timeout=120)
- body = r.json()
- assert r.status_code == 400
- assert body["error"]["code"] == "MISSING_FILE"
-
-
-def test_upload_models_unknown_model_type(http: requests.Session, api_base: str):
- files = {"file": ("m.safetensors", b"A" * 128, "application/octet-stream")}
- form = {"tags": json.dumps(["models", "model_type:no_such_category", "unit-tests"]), "name": "m.safetensors"}
- r = http.post(api_base + "/api/assets", data=form, files=files, timeout=120)
- body = r.json()
- assert r.status_code == 400, body
- assert body["error"]["code"] == "INVALID_BODY"
-
-
[email protected]("model_type", ["configs", "custom_nodes"])
-def test_upload_models_rejects_non_model_registered_folder(
- model_type: str, http: requests.Session, api_base: str
-):
- files = {"file": ("not-a-model.py", b"A" * 128, "application/octet-stream")}
- form = {
- "tags": json.dumps(["models", f"model_type:{model_type}", "unit-tests"]),
- "name": "not-a-model.py",
- }
-
- response = http.post(api_base + "/api/assets", data=form, files=files, timeout=120)
- body = response.json()
-
- assert response.status_code == 400, body
- assert body["error"]["code"] == "INVALID_BODY"
-
-
-def test_upload_models_requires_model_type(http: requests.Session, api_base: str):
- files = {"file": ("nocat.safetensors", b"A" * 64, "application/octet-stream")}
- form = {"tags": json.dumps(["models"]), "name": "nocat.safetensors", "user_metadata": json.dumps({})}
- r = http.post(api_base + "/api/assets", data=form, files=files, timeout=120)
- body = r.json()
- assert r.status_code == 400
- assert body["error"]["code"] == "INVALID_BODY"
-
-
-def test_upload_extra_tags_are_labels_not_path_components(http: requests.Session, api_base: str):
- files = {"file": ("evil.safetensors", b"A" * 256, "application/octet-stream")}
- form = {"tags": json.dumps(["models", "model_type:checkpoints", "unit-tests", "..", "zzz"]), "name": "evil.safetensors"}
- r = http.post(api_base + "/api/assets", data=form, files=files, timeout=120)
- body = r.json()
- assert r.status_code == 201, body
- assert ".." in body["tags"]
- assert "zzz" in body["tags"]
- assert "models" in body["tags"]
- assert "model_type:checkpoints" in body["tags"]
-
-
- ("subfolder", "expected_tag", "unexpected_tags"),
- [
- ("custom/session", None, {"custom", "session"}),
- ("pasted", "pasted", set()),
- ],
-)
-def test_upload_image_accepts_arbitrary_subfolder_but_only_known_values_become_tags(
- http: requests.Session,
- api_base: str,
- comfy_tmp_base_dir: Path,
- subfolder: str,
- expected_tag: str | None,
- unexpected_tags: set[str],
-):
- name = f"upload-image-{uuid.uuid4().hex}.png"
- files = {"image": (name, b"image-upload" * 64, "image/png")}
- form = {"type": "input", "subfolder": subfolder}
-
- response = http.post(api_base + "/upload/image", data=form, files=files, timeout=120)
- body = response.json()
-
- assert response.status_code == 200, body
- assert body["subfolder"] == subfolder
- assert (comfy_tmp_base_dir / "input" / subfolder / body["name"]).exists()
-
- asset = body["asset"]
- tags = set(asset["tags"])
- assert "input" in tags
- assert "uploaded" in tags
- if expected_tag:
- assert expected_tag in tags
- assert tags.isdisjoint(unexpected_tags)
-
-
-def test_multipart_upload_accepts_system_looking_extra_labels(
- http: requests.Session, api_base: str
-):
- files = {"file": ("relaxed-labels.bin", b"relaxed" * 64, "application/octet-stream")}
- form = {
- "tags": json.dumps(
- [
- "input",
- "unit-tests",
- "model:true",
- "models:foo",
- "temporary",
- "uploaded:true",
- ]
- ),
- "name": "relaxed-labels.bin",
- "user_metadata": json.dumps({}),
- }
- response = http.post(api_base + "/api/assets", data=form, files=files, timeout=120)
- body = response.json()
-
- assert response.status_code == 201, body
- assert "input" in body["tags"]
- assert "model:true" in body["tags"]
- assert "models:foo" in body["tags"]
- assert "temporary" in body["tags"]
- assert "uploaded:true" in body["tags"]
-
-
-def test_multipart_upload_rejects_ambiguous_destination_roles(
- http: requests.Session, api_base: str
-):
- files = {"file": ("ambiguous.bin", b"ambiguous" * 64, "application/octet-stream")}
- form = {
- "tags": json.dumps(["input", "output", "unit-tests"]),
- "name": "ambiguous.bin",
- "user_metadata": json.dumps({}),
- }
- response = http.post(api_base + "/api/assets", data=form, files=files, timeout=120)
- body = response.json()
-
- assert response.status_code == 400, body
- assert body["error"]["code"] == "INVALID_BODY"
-
-
-def test_multipart_upload_rejects_multiple_model_types_for_models_destination(
- http: requests.Session, api_base: str
-):
- files = {"file": ("ambiguous-model.safetensors", b"ambiguous-model" * 64, "application/octet-stream")}
- form = {
- "tags": json.dumps(
- ["models", "model_type:checkpoints", "model_type:loras", "unit-tests"]
- ),
- "name": "ambiguous-model.safetensors",
- "user_metadata": json.dumps({}),
- }
- response = http.post(api_base + "/api/assets", data=form, files=files, timeout=120)
- body = response.json()
-
- assert response.status_code == 400, body
- assert body["error"]["code"] == "INVALID_BODY"
-
-
- ("tags", "expected_root", "extension"),
- [
- (["input", "unit-tests", "upload-location-input"], "input", ".bin"),
- (["output", "unit-tests", "upload-location-output"], "output", ".bin"),
- (
- ["models", "model_type:checkpoints", "unit-tests", "upload-location-model"],
- "models/checkpoints",
- ".safetensors",
- ),
- ],
-)
-def test_multipart_upload_role_selects_write_location(
- http: requests.Session,
- api_base: str,
- comfy_tmp_base_dir: Path,
- tags: list[str],
- expected_root: str,
- extension: str,
-):
- role = next(tag for tag in tags if tag in {"input", "models", "output"})
- name = f"{role}-role-upload{extension}"
- files = {"file": (name, f"{role}-role-bytes".encode() * 64, "application/octet-stream")}
- form = {
- "tags": json.dumps(tags),
- "name": name,
- "user_metadata": json.dumps({}),
- }
-
- response = http.post(api_base + "/api/assets", data=form, files=files, timeout=120)
- body = response.json()
-
- assert response.status_code == 201, body
- stored_name = get_asset_filename(body["asset_hash"], extension)
- expected_disk_path = comfy_tmp_base_dir / expected_root / stored_name
- assert expected_disk_path.exists()
-
-
-def test_upload_empty_tags_rejected(http: requests.Session, api_base: str):
- files = {"file": ("notags.bin", b"A" * 64, "application/octet-stream")}
- form = {"tags": json.dumps([]), "name": "notags.bin", "user_metadata": json.dumps({})}
- r = http.post(api_base + "/api/assets", data=form, files=files, timeout=120)
- body = r.json()
- assert r.status_code == 400
- assert body["error"]["code"] == "INVALID_BODY"
-
-
[email protected]("root", ["input", "output"])
-def test_duplicate_upload_same_display_name_does_not_clobber(
- root: str,
- http: requests.Session,
- api_base: str,
- asset_factory,
- make_asset_bytes,
-):
- """
- Two uploads use the same tags and the same display name but different bytes.
- With hash-based filenames, they must NOT overwrite each other. Both assets
- remain accessible and serve their original content.
- """
- scope = f"dup-path-{uuid.uuid4().hex[:6]}"
- display_name = "same_display.bin"
+ created: list[str] = []
+ try:
+ for _ in range(2):
+ response = http.post(
+ f"{api_base}/api/assets",
+ files={"file": ("repeat.png", data, "application/octet-stream")},
+ data=form,
+ timeout=120,
+ )
+ body = response.json()
+ created.append(body["id"])
+ assert response.status_code == 201, body
+ assert body["created_new"] is True
- d1 = make_asset_bytes(scope + "-v1", 1536)
- d2 = make_asset_bytes(scope + "-v2", 2048)
- tags = [root, "unit-tests", scope]
+ assert created[0] != created[1], "every upload is its own delivery record"
+ finally:
+ for asset_id in created:
+ http.delete(f"{api_base}/api/assets/{asset_id}", timeout=30)
- first = asset_factory(display_name, tags, {}, d1)
- second = asset_factory(display_name, tags, {}, d2)
- assert first["id"] != second["id"]
- assert first["asset_hash"] != second["asset_hash"] # different content
- assert first["name"] == second["name"] == display_name
+def test_upload_returns_a_record_identifier(http, api_base, asset_factory, make_asset_bytes):
+ record = asset_factory("upload.png", ["output", "unit-tests"], {}, make_asset_bytes("upload"))
- # Both must be independently retrievable
- r1 = http.get(f"{api_base}/api/assets/{first['id']}/content", timeout=120)
- b1 = r1.content
- assert r1.status_code == 200
- assert b1 == d1
- r2 = http.get(f"{api_base}/api/assets/{second['id']}/content", timeout=120)
- b2 = r2.content
- assert r2.status_code == 200
- assert b2 == d2
+ assert record["id"]
+ assert http.get(f"{api_base}/api/assets/{record['id']}").status_code == 200
diff --git a/tests-unit/execution_test/test_enrich_output.py b/tests-unit/execution_test/test_enrich_output.py
index 61490c49e..d12e2edd6 100644
--- a/tests-unit/execution_test/test_enrich_output.py
+++ b/tests-unit/execution_test/test_enrich_output.py
@@ -1,205 +1,386 @@
-"""Tests for enrich_output_with_assets in comfy_execution/asset_enrichment.py."""
+import copy
import os
-import types
-import unittest
-from unittest.mock import MagicMock, patch
+import tempfile
+from collections import namedtuple
+from unittest.mock import patch
+import folder_paths
+import pytest
-def _make_args(enable_assets: bool):
- a = types.SimpleNamespace()
- a.enable_assets = enable_assets
- return a
+from app.assets.manager import NoAssets
+from comfy_execution.asset_enrichment import (
+ emit_cached_output,
+ register_cached_outputs,
+ register_executed_outputs,
+)
+from test_inmemory_assets import AssetCall, InMemoryAssets
+_CacheEntry = namedtuple("_CacheEntry", ["ui", "outputs"])
-def _make_register_result(ref_id="ref-id-2"):
- result = MagicMock()
- result.ref.id = ref_id
- return result
+_BASE = os.path.join(tempfile.gettempdir(), "asset-enrichment-test-base")
-# Platform-appropriate absolute base. tempfile.gettempdir() returns C:\... on
-# Windows and /tmp on POSIX, so containment via commonpath behaves naturally.
-_DEFAULT_BASE = os.path.join(__import__("tempfile").gettempdir(), "asset-enrichment-test-base")
+class _ArgsStub:
+ enable_assets = False
+ enable_asset_hashing = False
-def _mocked_modules(*, enable_assets=True, register_file_in_place=None, directory=_DEFAULT_BASE):
+class _Server:
+ last_node_id: str | None = None
+ sockets_metadata: dict[str, dict[str, object]] = {}
+
+ def __init__(self, client_id: str | None = None) -> None:
+ self.client_id = client_id
+ self.sent: list[tuple] = []
+
+ def send_sync(self, event, data, sid=None):
+ self.sent.append((event, data, sid))
+
+ def queue_updated(self) -> None:
+ pass
+
+
+def output_path_environment(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr(folder_paths, "get_directory_by_type", lambda _type: _BASE)
+ monkeypatch.setattr(os.path, "isfile", lambda _path: True)
+
+
+def _output(filename: str, *, subfolder: str = "", type_: str = "output") -> dict:
+ return {"images": [{"filename": filename, "subfolder": subfolder, "type": type_}]}
+
+
+def _wrapper(filename: str, node_id: str = "1") -> dict:
return {
- "comfy.cli_args": MagicMock(args=_make_args(enable_assets)),
- "folder_paths": MagicMock(get_directory_by_type=MagicMock(return_value=directory)),
- "app.assets.services.ingest": MagicMock(
- register_file_in_place=register_file_in_place or MagicMock(return_value=_make_register_result()),
- DependencyMissingError=type("DependencyMissingError", (Exception,), {}),
- ),
+ "meta": {
+ "node_id": node_id,
+ "display_node": node_id,
+ "parent_node": None,
+ "real_node_id": node_id,
+ },
+ "output": _output(filename),
}
-def _call(output_ui, *, enable_assets=True, file_exists=True, register_result=None, directory=_DEFAULT_BASE):
- register_mock = MagicMock(return_value=register_result or _make_register_result())
- mocked = _mocked_modules(
- enable_assets=enable_assets,
- register_file_in_place=register_mock,
- directory=directory,
+def _find_ids(value) -> list:
+ found: list = []
+ if isinstance(value, dict):
+ for key, sub in value.items():
+ if key == "id":
+ found.append(sub)
+ found.extend(_find_ids(sub))
+ elif isinstance(value, list):
+ for item in value:
+ found.extend(_find_ids(item))
+ return found
+
+
+# REQUIRED test names (invoked verbatim downstream). Do not rename.
+def test_executed_new_path_gets_fresh_id(output_path_environment) -> None:
+ manager = InMemoryAssets()
+ output_ui = _output("new.png")
+
+ enriched = register_executed_outputs(output_ui, "job-1", manager)
+
+ assert enriched["images"][0]["id"] == "asset-1"
+ assert "id" not in output_ui["images"][0]
+ assert manager.calls == [
+ AssetCall(
+ "register_executed_output", (os.path.join(_BASE, "new.png"), "job-1")
+ )
+ ]
+ deliveries = manager.deliveries_by_path[os.path.join(_BASE, "new.png")]
+ assert [(delivery.asset.id, delivery.asset.job_id) for delivery in deliveries] == [
+ ("asset-1", "job-1")
+ ]
+
+
+def test_executed_multiple_entries_register_each_output(output_path_environment) -> None:
+ manager = InMemoryAssets()
+ filenames = ["first.png", "second.png", "third.png"]
+ output_ui = {
+ "images": [
+ {"filename": filename, "subfolder": "batch", "type": "output"}
+ for filename in filenames
+ ]
+ }
+
+ register_executed_outputs(output_ui, "job-list", manager)
+
+ expected_paths = [os.path.join(_BASE, "batch", filename) for filename in filenames]
+ assert manager.calls == [
+ AssetCall("register_executed_output", (path, "job-list"))
+ for path in expected_paths
+ ]
+ assert [
+ (delivery.abs_path, delivery.asset.job_id, delivery.superseded)
+ for path in expected_paths
+ for delivery in manager.deliveries_by_path[path]
+ ] == [(path, "job-list", False) for path in expected_paths]
+
+
+def test_executed_mixed_types_use_their_own_roots(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ manager = InMemoryAssets()
+ output_root = f"{_BASE}-output"
+ temp_root = f"{_BASE}-temp"
+ roots = {"output": output_root, "temp": temp_root}
+ monkeypatch.setattr(
+ folder_paths,
+ "get_directory_by_type",
+ lambda output_type: roots[output_type],
+ )
+ monkeypatch.setattr(os.path, "isfile", lambda _path: True)
+ output_ui = {
+ "images": [
+ {"filename": "kept.png", "subfolder": "saved", "type": "output"},
+ {"filename": "preview.png", "subfolder": "staged", "type": "temp"},
+ ]
+ }
+
+ register_executed_outputs(output_ui, "job-mixed", manager)
+
+ assert manager.calls == [
+ AssetCall(
+ "register_executed_output",
+ (os.path.join(output_root, "saved", "kept.png"), "job-mixed"),
+ ),
+ AssetCall(
+ "register_executed_output",
+ (os.path.join(temp_root, "staged", "preview.png"), "job-mixed"),
+ ),
+ ]
+
+
+def test_executed_repeated_calls_keep_distinct_path_deliveries(
+ output_path_environment,
+) -> None:
+ manager = InMemoryAssets()
+ filenames = ["iteration-1.png", "iteration-2.png", "iteration-3.png"]
+
+ for filename in filenames:
+ register_executed_outputs(_output(filename), "loop-job", manager)
+
+ expected_paths = [os.path.join(_BASE, filename) for filename in filenames]
+ assert manager.calls == [
+ AssetCall("register_executed_output", (path, "loop-job"))
+ for path in expected_paths
+ ]
+ assert [
+ (delivery.abs_path, delivery.asset.job_id, delivery.superseded)
+ for path in expected_paths
+ for delivery in manager.deliveries_by_path[path]
+ ] == [(path, "loop-job", False) for path in expected_paths]
+
+
+def test_executed_empty_entry_list_is_noop(output_path_environment) -> None:
+ manager = InMemoryAssets()
+
+ enriched = register_executed_outputs({"images": []}, "job", manager)
+
+ assert enriched == {"images": []}
+ assert manager.calls == []
+ assert manager.deliveries_by_path == {}
+
+
+def test_executed_over_existing_path_gets_new_id_and_marks_old_missing(output_path_environment) -> None:
+ manager = InMemoryAssets()
+
+ first = register_executed_outputs(_output("same.png"), "job-1", manager)
+ old_id = first["images"][0]["id"]
+
+ second = register_executed_outputs(_output("same.png"), "job-2", manager)
+ new_id = second["images"][0]["id"]
+
+ deliveries = manager.deliveries_by_path[os.path.join(_BASE, "same.png")]
+ assert new_id != old_id
+ assert deliveries[0].superseded is True
+ assert deliveries[-1].asset.id == new_id
+
+
+def test_cached_replay_creates_delivery_with_current_job_id(output_path_environment) -> None:
+ manager = InMemoryAssets()
+
+ register_executed_outputs(_output("replay.png"), "seed-job", manager)
+ enriched = register_cached_outputs(_wrapper("replay.png"), "replay-job", manager)
+
+ replay_id = enriched["output"]["images"][0]["id"]
+ deliveries = manager.deliveries_by_path[os.path.join(_BASE, "replay.png")]
+ assert (replay_id, "replay-job") == (deliveries[-1].asset.id, deliveries[-1].asset.job_id)
+ assert replay_id != "asset-1"
+
+
+def test_cached_registration_happens_without_client(output_path_environment) -> None:
+ manager = InMemoryAssets()
+ server = _Server(client_id=None)
+ ui_outputs: dict = {}
+
+ register_executed_outputs(_output("noclient.png"), "seed-job", manager)
+ emit_cached_output(
+ server,
+ "node-1",
+ "node-1",
+ _CacheEntry(ui=_wrapper("noclient.png"), outputs=[]),
+ "job-x",
+ ui_outputs,
+ manager,
+ )
+
+ deliveries = manager.deliveries_by_path[os.path.join(_BASE, "noclient.png")]
+ assert any(delivery.asset.job_id == "job-x" for delivery in deliveries)
+ assert "node-1" in ui_outputs
+ assert ui_outputs["node-1"]["output"]["images"][0]["id"] is not None
+ assert server.sent == []
+
+
+def test_cache_entry_contains_no_asset_ids(output_path_environment) -> None:
+ manager = InMemoryAssets()
+ output_ui = _output("keep.png")
+
+ enriched = register_executed_outputs(output_ui, "job", manager)
+
+ cache_entry = _CacheEntry(
+ ui={"meta": {"node_id": "1"}, "output": output_ui}, outputs=[]
+ )
+ assert _find_ids(cache_entry.ui) == []
+ assert _find_ids(enriched) == ["asset-1"]
+
+
+def test_cached_ui_object_unmodified_after_emission(output_path_environment) -> None:
+ manager = InMemoryAssets()
+ server = _Server(client_id="client-1")
+ cached = _CacheEntry(ui=_wrapper("immut.png"), outputs=[])
+ snapshot = copy.deepcopy(cached.ui)
+
+ register_executed_outputs(_output("immut.png"), "seed-job", manager)
+ emit_cached_output(server, "1", "1", cached, "prompt-1", {}, manager)
+
+ assert cached.ui == snapshot
+
+
+def test_double_emission_yields_single_delivery(output_path_environment) -> None:
+ manager = InMemoryAssets()
+ server = _Server(client_id="client-1")
+ ui_outputs: dict = {}
+ cached = _CacheEntry(ui=_wrapper("dbl.png"), outputs=[])
+
+ register_executed_outputs(_output("dbl.png"), "seed-job", manager)
+ emit_cached_output(server, "1", "1", cached, "prompt-1", ui_outputs, manager)
+ emit_cached_output(server, "1", "1", cached, "prompt-1", ui_outputs, manager)
+
+ deliveries = manager.deliveries_by_path[os.path.join(_BASE, "dbl.png")]
+ assert len([delivery for delivery in deliveries if delivery.asset.job_id == "prompt-1"]) == 1
+
+
+def test_executed_disabled_returns_unenriched_copy(output_path_environment) -> None:
+ manager = NoAssets(_ArgsStub())
+ output_ui = _output("a.png")
+
+ with patch.object(
+ manager,
+ "register_executed_output",
+ wraps=manager.register_executed_output,
+ ) as register_executed_output:
+ enriched = register_executed_outputs(output_ui, "job", manager)
+
+ assert enriched is not output_ui
+ assert "id" not in enriched["images"][0]
+ register_executed_output.assert_not_called()
+
+
+def test_executed_missing_file_is_skipped(monkeypatch: pytest.MonkeyPatch) -> None:
+ manager = InMemoryAssets()
+ monkeypatch.setattr(folder_paths, "get_directory_by_type", lambda _type: _BASE)
+ monkeypatch.setattr(os.path, "isfile", lambda _path: False)
+
+ enriched = register_executed_outputs(_output("gone.png"), "job", manager)
+
+ assert "id" not in enriched["images"][0]
+ assert manager.calls == []
+
+
+def test_executed_path_escape_is_skipped(output_path_environment) -> None:
+ manager = InMemoryAssets()
+ output_ui = {"images": [{"filename": "passwd", "subfolder": "../../etc", "type": "output"}]}
+
+ enriched = register_executed_outputs(output_ui, "job", manager)
+
+ assert "id" not in enriched["images"][0]
+ assert manager.calls == []
+
+
+def test_executed_non_list_value_passes_through(output_path_environment) -> None:
+ manager = InMemoryAssets()
+
+ enriched = register_executed_outputs({"text": "hello"}, "job", manager)
+
+ assert enriched["text"] == "hello"
+
+
+def test_executed_registration_failure_never_raises(
+ output_path_environment, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ manager = InMemoryAssets()
+
+ def boom(_abs_path: str, job_id: str | None) -> None:
+ raise RuntimeError("registration blew up")
+
+ monkeypatch.setattr(manager, "register_executed_output", boom)
+
+ enriched = register_executed_outputs(_output("boom.png"), "job", manager)
+
+ assert "id" not in enriched["images"][0]
+
+
+def test_cached_strips_legacy_ids_before_replay(output_path_environment) -> None:
+ manager = InMemoryAssets()
+ wrapper = _wrapper("legacy.png")
+ wrapper["output"]["images"][0]["id"] = "stale-id"
+
+ register_executed_outputs(_output("legacy.png"), "seed-job", manager)
+ enriched = register_cached_outputs(wrapper, "replay-job", manager)
+
+ assert enriched["output"]["images"][0]["id"] != "stale-id"
+ assert wrapper["output"]["images"][0]["id"] == "stale-id"
+
+
+def test_cached_none_wrapper_returns_none(output_path_environment) -> None:
+ manager = InMemoryAssets()
+
+ result = register_cached_outputs(None, "job", manager)
+
+ assert result is None
+ assert manager.calls == []
+
+
+def test_cached_missing_live_content_is_nonevent(output_path_environment) -> None:
+ manager = InMemoryAssets()
+
+ enriched = register_cached_outputs(_wrapper("orphan.png"), "job", manager)
+
+ assert "id" not in enriched["output"]["images"][0]
+ assert manager.deliveries_by_path == {}
+
+
+def test_emit_cached_sends_enriched_output_to_client(output_path_environment) -> None:
+ manager = InMemoryAssets()
+ server = _Server(client_id="client-1")
+ ui_outputs: dict = {}
+
+ register_executed_outputs(_output("send.png"), "seed-job", manager)
+ emit_cached_output(
+ server,
+ "1",
+ "1",
+ _CacheEntry(ui=_wrapper("send.png"), outputs=[]),
+ "prompt-1",
+ ui_outputs,
+ manager,
)
- # Only os.path.isfile is patched — abspath/join must run natively so the
- # containment check sees real platform paths.
- with patch.dict("sys.modules", mocked), \
- patch("os.path.isfile", return_value=file_exists):
- import importlib
- import comfy_execution.asset_enrichment as mod
- importlib.reload(mod)
- return mod.enrich_output_with_assets(output_ui)
-
-
-class TestEnrichOutputWithAssets(unittest.TestCase):
-
- def test_disabled_returns_unchanged(self):
- output = {"images": [{"filename": "a.png", "subfolder": "", "type": "output"}]}
- result = _call(output, enable_assets=False)
- self.assertNotIn("id", result["images"][0])
-
- def test_non_list_value_passed_through(self):
- output = {"text": "hello"}
- result = _call(output)
- self.assertEqual(result["text"], "hello")
-
- def test_entry_without_filename_unchanged(self):
- output = {"latent": [{"subfolder": "", "type": "output"}]}
- result = _call(output)
- self.assertNotIn("id", result["latent"][0])
-
- def test_entry_without_type_unchanged(self):
- output = {"data": [{"filename": "a.png", "subfolder": ""}]}
- result = _call(output)
- self.assertNotIn("id", result["data"][0])
-
- def test_file_not_on_disk_unchanged(self):
- output = {"images": [{"filename": "missing.png", "subfolder": "", "type": "output"}]}
- result = _call(output, file_exists=False)
- self.assertNotIn("id", result["images"][0])
-
- def test_unknown_type_returns_none_directory_unchanged(self):
- output = {"images": [{"filename": "a.png", "subfolder": "", "type": "unknown"}]}
- result = _call(output, directory=None)
- self.assertNotIn("id", result["images"][0])
-
- def test_register_injects_only_id(self):
- reg = _make_register_result(ref_id="inline-ref")
- output = {"images": [{"filename": "new.png", "subfolder": "", "type": "output"}]}
- result = _call(output, register_result=reg)
- img = result["images"][0]
- self.assertEqual(img["id"], "inline-ref")
- # Only id is injected — no asset_hash, name, preview_url, size
- self.assertNotIn("asset_hash", img)
- self.assertNotIn("name", img)
- self.assertNotIn("preview_url", img)
- self.assertNotIn("size", img)
-
- def test_register_called_per_entry(self):
- register_mock = MagicMock(return_value=_make_register_result())
- mocked = _mocked_modules(register_file_in_place=register_mock)
- output = {
- "images": [
- {"filename": "a.png", "subfolder": "", "type": "output"},
- {"filename": "b.png", "subfolder": "", "type": "output"},
- ]
- }
-
- with patch.dict("sys.modules", mocked), \
- patch("os.path.isfile", return_value=True):
- import importlib
- import comfy_execution.asset_enrichment as mod
- importlib.reload(mod)
- mod.enrich_output_with_assets(output)
-
- self.assertEqual(register_mock.call_count, 2)
-
- def test_original_entry_not_mutated(self):
- orig = {"filename": "a.png", "subfolder": "", "type": "output"}
- output = {"images": [orig]}
- _call(output)
- self.assertNotIn("id", orig)
-
- def test_enrichment_error_does_not_block_sibling_entries(self):
- call_count = [0]
- good_reg = _make_register_result(ref_id="good-ref")
-
- def register_side_effect(abs_path, name, tags):
- call_count[0] += 1
- if call_count[0] == 1:
- raise RuntimeError("boom")
- return good_reg
-
- mocked = _mocked_modules(register_file_in_place=register_side_effect)
-
- output = {
- "images": [
- {"filename": "bad.png", "subfolder": "", "type": "output"},
- {"filename": "good.png", "subfolder": "", "type": "output"},
- ]
- }
-
- with patch.dict("sys.modules", mocked), \
- patch("os.path.isfile", return_value=True):
- import importlib
- import comfy_execution.asset_enrichment as mod
- importlib.reload(mod)
- result = mod.enrich_output_with_assets(output)
-
- imgs = result["images"]
- self.assertNotIn("id", imgs[0])
- self.assertEqual(imgs[1]["id"], "good-ref")
-
- def test_multiple_output_keys_all_enriched(self):
- output = {
- "images": [{"filename": "a.png", "subfolder": "", "type": "output"}],
- "videos": [{"filename": "b.mp4", "subfolder": "", "type": "output"}],
- }
- result = _call(output)
- self.assertIn("id", result["images"][0])
- self.assertIn("id", result["videos"][0])
-
- def test_none_entry_in_list_unchanged(self):
- output = {"images": [None, {"filename": "a.png", "subfolder": "", "type": "output"}]}
- result = _call(output)
- self.assertIsNone(result["images"][0])
- self.assertIn("id", result["images"][1])
-
- def test_path_traversal_subfolder_skipped(self):
- register_mock = MagicMock(return_value=_make_register_result())
- mocked = _mocked_modules(register_file_in_place=register_mock)
-
- output = {"images": [{"filename": "passwd", "subfolder": "../../etc", "type": "output"}]}
-
- # Do NOT patch os.path.abspath — real resolution is required for the containment check.
- with patch.dict("sys.modules", mocked), \
- patch("os.path.isfile", return_value=True):
- import importlib
- import comfy_execution.asset_enrichment as mod
- importlib.reload(mod)
- result = mod.enrich_output_with_assets(output)
-
- self.assertNotIn("id", result["images"][0])
- register_mock.assert_not_called()
-
- def test_absolute_filename_skipped(self):
- register_mock = MagicMock(return_value=_make_register_result())
- mocked = _mocked_modules(register_file_in_place=register_mock)
-
- # Absolute filename — os.path.join discards earlier components when a later one is absolute.
- absolute_filename = os.path.abspath(os.sep + "etc" + os.sep + "passwd")
- output = {"images": [{"filename": absolute_filename, "subfolder": "", "type": "output"}]}
-
- with patch.dict("sys.modules", mocked), \
- patch("os.path.isfile", return_value=True):
- import importlib
- import comfy_execution.asset_enrichment as mod
- importlib.reload(mod)
- result = mod.enrich_output_with_assets(output)
-
- self.assertNotIn("id", result["images"][0])
- register_mock.assert_not_called()
-
-
-if __name__ == "__main__":
- unittest.main()
+ assert len(server.sent) == 1
+ event, payload, client_id = server.sent[0]
+ assert event == "executed"
+ assert client_id == "client-1"
+ assert payload["output"]["images"][0]["id"] is not None
diff --git a/tests-unit/execution_test/test_execute_reentry.py b/tests-unit/execution_test/test_execute_reentry.py
new file mode 100644
index 000000000..2c81acc07
--- /dev/null
+++ b/tests-unit/execution_test/test_execute_reentry.py
@@ -0,0 +1,171 @@
+import asyncio
+import os
+import tempfile
+
+import pytest
+
+from test_inmemory_assets import InMemoryAssets
+
+_BASE = os.path.join(tempfile.gettempdir(), "execute-reentry-test-base")
+
+
+def _find_ids(value) -> list:
+ found: list = []
+ if isinstance(value, dict):
+ for key, sub in value.items():
+ if key == "id":
+ found.append(sub)
+ found.extend(_find_ids(sub))
+ elif isinstance(value, (list, tuple)):
+ for item in value:
+ found.extend(_find_ids(item))
+ return found
+
+
+class _Server:
+ def __init__(self, client_id=None) -> None:
+ self.client_id = client_id
+ self.last_node_id = None
+ self.sent: list = []
+
+ def send_sync(self, event, payload, client_id) -> None:
+ self.sent.append((event, payload))
+
+
+class _AsyncDictCache:
+
+ def __init__(self) -> None:
+ self.store: dict = {}
+
+ async def get(self, key):
+ return self.store.get(key)
+
+ async def set(self, key, value) -> None:
+ self.store[key] = value
+
+ async def ensure_subcache_for(self, unique_id, node_ids):
+ return self
+
+
+class _Caches:
+ def __init__(self) -> None:
+ self.outputs = _AsyncDictCache()
+ self.objects = _AsyncDictCache()
+ self.all = [self.outputs, self.objects]
+
+
+class _ExecutionList:
+ def cache_update(self, unique_id, entry) -> None:
+ pass
+
+ def add_external_block(self, unique_id):
+ return lambda: None
+
+ def get_cache(self, a, b):
+ return None
+
+
+class _NoProgress:
+ def start_progress(self, *a, **k) -> None:
+ pass
+
+ def finish_progress(self, *a, **k) -> None:
+ pass
+
+
+class _AsyncUINode:
+
+ RETURN_TYPES = ()
+ FUNCTION = "run"
+ OUTPUT_NODE = True
+ CATEGORY = "test"
+
+ @classmethod
+ def INPUT_TYPES(cls):
+ return {"required": {}}
+
+ async def run(self):
+ await asyncio.sleep(0.02)
+ return {"ui": {"images": [{"filename": "async.png", "subfolder": "", "type": "output"}]}}
+
+
+def execution_env(monkeypatch):
+ try:
+ from comfy.cli_args import args
+ monkeypatch.setattr(args, "cpu", True, raising=False)
+ import execution
+ import folder_paths
+ import nodes
+ except Exception as exc: # pragma: no cover - environment dependent
+ pytest.skip(f"execution module could not be imported in CPU mode: {exc!r}")
+
+ os.makedirs(_BASE, exist_ok=True)
+ monkeypatch.setattr(folder_paths, "get_directory_by_type", lambda t: _BASE)
+ monkeypatch.setattr(execution, "get_progress_state", lambda: _NoProgress())
+ monkeypatch.setitem(nodes.NODE_CLASS_MAPPINGS, "AsyncUINode", _AsyncUINode)
+
+ return execution, InMemoryAssets()
+
+
+async def _drive_async_reentry(execution, asset_manager):
+ from comfy_execution.graph import DynamicPrompt
+
+ unique_id = "1"
+ with open(os.path.join(_BASE, "async.png"), "wb") as f:
+ f.write(b"x")
+
+ dynprompt = DynamicPrompt({unique_id: {"class_type": "AsyncUINode", "inputs": {}}})
+ caches = _Caches()
+ server = _Server(client_id=None)
+ exec_list = _ExecutionList()
+ pending_subgraph_results: dict = {}
+ pending_async_nodes: dict = {}
+ ui_outputs: dict = {}
+ executed: set = set()
+
+ common = (server, dynprompt, caches, unique_id, {}, executed, "job-1", exec_list)
+
+ r1, _, _ = await execution.execute(
+ *common, pending_subgraph_results, pending_async_nodes, ui_outputs, asset_manager
+ )
+ ui_outputs_had_uid_after_entry1 = unique_id in ui_outputs
+
+ tasks = [t for t in pending_async_nodes.get(unique_id, []) if isinstance(t, asyncio.Task)]
+ if tasks:
+ await asyncio.gather(*tasks)
+ for _ in range(3):
+ await asyncio.sleep(0)
+
+ r2, _, _ = await execution.execute(
+ *common, pending_subgraph_results, pending_async_nodes, ui_outputs, asset_manager
+ )
+
+ cached = caches.outputs.store.get(unique_id)
+ return {
+ "r1": r1,
+ "r2": r2,
+ "ui_outputs_had_uid_after_entry1": ui_outputs_had_uid_after_entry1,
+ "ui_ids": _find_ids(ui_outputs.get(unique_id)),
+ "cache_entry": cached,
+ "cache_ids": _find_ids(cached.ui) if cached is not None else None,
+ }
+
+
+def test_async_reentry_keeps_cache_id_free(execution_env):
+ execution, asset_manager = execution_env
+ from execution import ExecutionResult
+
+ obs = asyncio.run(_drive_async_reentry(execution, asset_manager))
+
+ assert obs["r1"] == ExecutionResult.PENDING
+ assert obs["r2"] == ExecutionResult.SUCCESS
+ # WHY it is safe: the async branch returns PENDING *before* writing ui_outputs,
+ # so on re-entry ui_outputs.get(unique_id) is None and cannot seed cache_ui_value.
+ assert obs["ui_outputs_had_uid_after_entry1"] is False
+
+ assert obs["ui_ids"] == ["asset-1"]
+
+ assert obs["cache_entry"] is not None
+ assert obs["cache_ids"] == []
+ assert [call.method for call in asset_manager.calls] == ["register_executed_output"]
diff --git a/tests-unit/execution_test/test_inmemory_assets.py b/tests-unit/execution_test/test_inmemory_assets.py
new file mode 100644
index 000000000..c67861366
--- /dev/null
+++ b/tests-unit/execution_test/test_inmemory_assets.py
@@ -0,0 +1,187 @@
+from __future__ import annotations
+
+import os
+from dataclasses import dataclass, replace
+from typing import TYPE_CHECKING, Any, Callable, TypeAlias
+
+from aiohttp import web
+
+from app.assets.manager import AssetManager
+from app.assets.services.schemas import RegisteredAsset, UploadAssetView
+
+if TYPE_CHECKING:
+ from app.user_manager import UserManager
+
+
+CallArgument: TypeAlias = str | bool | None
+EventSink: TypeAlias = Callable[[str, dict[str, Any]], None]
+
+
+@dataclass(frozen=True, slots=True)
+class AssetCall:
+ method: str
+ arguments: tuple[CallArgument, ...]
+
+
+@dataclass(frozen=True, slots=True)
+class Delivery:
+ abs_path: str
+ asset: RegisteredAsset
+ superseded: bool = False
+
+
+class InMemoryAssets:
+
+ def __init__(self) -> None:
+ self.calls: list[AssetCall] = []
+ self.deliveries_by_path: dict[str, list[Delivery]] = {}
+ self._live_output_by_path: dict[str, RegisteredAsset] = {}
+ self._counter: int = 0
+
+ @property
+ def enabled(self) -> bool:
+ return True
+
+ def startup(self) -> None:
+ return None
+
+ def shutdown(self) -> None:
+ return None
+
+ def register_routes(
+ self, app: web.Application, user_manager: UserManager | None
+ ) -> None:
+ return None
+
+ def ensure_scan_started(self) -> None:
+ return None
+
+ def pause_background_scan(self) -> None:
+ return None
+
+ def queue_output_scan(self) -> None:
+ return None
+
+ def resume_background_scan(self) -> None:
+ return None
+
+ def register_upload(
+ self,
+ abs_path: str,
+ name: str,
+ upload_type: str,
+ subfolder: str,
+ *,
+ content_written: bool,
+ ) -> UploadAssetView | None:
+ return None
+
+ def register_executed_output(
+ self, abs_path: str, job_id: str | None
+ ) -> RegisteredAsset | None:
+ self._record("register_executed_output", abs_path, job_id)
+ deliveries = self.deliveries_by_path.setdefault(abs_path, [])
+ for index, delivery in enumerate(deliveries):
+ deliveries[index] = replace(delivery, superseded=True)
+
+ asset_id = self._next_asset_id()
+ asset = RegisteredAsset(
+ id=asset_id,
+ content_id=f"content-{asset_id}",
+ job_id=job_id,
+ name=os.path.basename(abs_path),
+ )
+ deliveries.append(Delivery(abs_path=abs_path, asset=asset))
+ self._live_output_by_path[abs_path] = asset
+ return asset
+
+ def register_cached_output(
+ self, abs_path: str, job_id: str | None
+ ) -> RegisteredAsset | None:
+ self._record("register_cached_output", abs_path, job_id)
+ source = self._live_output_by_path.get(abs_path)
+ if source is None:
+ return None
+
+ asset = RegisteredAsset(
+ id=self._next_asset_id(),
+ content_id=source.content_id,
+ job_id=job_id,
+ name=os.path.basename(abs_path),
+ )
+ self.deliveries_by_path[abs_path].append(
+ Delivery(abs_path=abs_path, asset=asset)
+ )
+ return asset
+
+ def set_event_sink(self, sink: EventSink) -> None:
+ return None
+
+ def _record(self, method: str, *arguments: CallArgument) -> None:
+ self.calls.append(AssetCall(method, arguments))
+
+ def _next_asset_id(self) -> str:
+ self._counter += 1
+ return f"asset-{self._counter}"
+
+
+def test_conforms() -> None:
+ manager: AssetManager = InMemoryAssets()
+ assert manager.enabled, (
+ "structural smoke test: AssetManager is a non-runtime-checkable Protocol, so isinstance is unavailable"
+ )
+ manager.startup()
+ manager.shutdown()
+ manager.register_routes(web.Application(), None)
+ manager.ensure_scan_started()
+ manager.pause_background_scan()
+ manager.queue_output_scan()
+ manager.resume_background_scan()
+ assert (
+ manager.register_upload(
+ "/output/upload.png",
+ "upload.png",
+ "output",
+ "",
+ content_written=True,
+ )
+ is None
+ )
+ executed = manager.register_executed_output("/output/executed.png", "job-1")
+ assert executed is not None
+ cached = manager.register_cached_output("/output/executed.png", "job-2")
+ assert cached is not None
+
+ def sink(event: str, payload: dict[str, Any]) -> None:
+ return None
+
+ manager.set_event_sink(sink)
+
+
+def test_register_executed_output_supersedes_prior_deliveries() -> None:
+ manager = InMemoryAssets()
+ abs_path = "/output/image.png"
+
+ first = manager.register_executed_output(abs_path, "job-1")
+ assert first is not None
+ cached = manager.register_cached_output(abs_path, "job-2")
+ assert cached is not None
+ replacement = manager.register_executed_output(abs_path, "job-3")
+ assert replacement is not None
+
+ deliveries = manager.deliveries_by_path[abs_path]
+ assert [delivery.superseded for delivery in deliveries] == [True, True, False]
+ assert cached.content_id == first.content_id
+ assert replacement.id != first.id
+ assert replacement.content_id != first.content_id
+
+
+def test_register_cached_output_returns_none_without_live_content() -> None:
+ manager = InMemoryAssets()
+
+ cached = manager.register_cached_output("/output/missing.png", "job-1")
+
+ assert cached is None
+ assert manager.calls == [
+ AssetCall("register_cached_output", ("/output/missing.png", "job-1"))
+ ]
diff --git a/tests-unit/folder_paths_test/filter_by_content_types_test.py b/tests-unit/folder_paths_test/filter_by_content_types_test.py
index 683f9fc11..d11f0159a 100644
--- a/tests-unit/folder_paths_test/filter_by_content_types_test.py
+++ b/tests-unit/folder_paths_test/filter_by_content_types_test.py
@@ -5,12 +5,14 @@ from folder_paths import filter_files_content_types, extension_mimetypes_cache
from unittest.mock import patch
+
+
@pytest.fixture(scope="module")
def file_extensions():
return {
'image': ['gif', 'heif', 'ico', 'jpeg', 'jpg', 'png', 'pnm', 'ppm', 'svg', 'tiff', 'webp', 'xbm', 'xpm'],
'audio': ['aif', 'aifc', 'aiff', 'au', 'flac', 'm4a', 'mp2', 'mp3', 'ogg', 'snd', 'wav'],
- 'video': ['avi', 'm2v', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'ogv', 'qt', 'webm', 'wmv'],
+ 'video': ['avi', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'ogv', 'qt', 'webm', 'wmv'],
'model': ['gltf', 'glb', 'obj', 'fbx', 'stl']
}
diff --git a/tests-unit/security_test/test_ghsa_779p_06_inline_svg_image_dest.py b/tests-unit/security_test/test_ghsa_779p_06_inline_svg_image_dest.py
index e0964e0db..7afdc9a2c 100644
--- a/tests-unit/security_test/test_ghsa_779p_06_inline_svg_image_dest.py
+++ b/tests-unit/security_test/test_ghsa_779p_06_inline_svg_image_dest.py
@@ -119,7 +119,7 @@ def asset_app(monkeypatch, tmp_path):
monkeypatch.setattr(
asset_routes,
"resolve_asset_for_download",
- lambda reference_id, owner_id: DownloadResolutionResult(
+ lambda reference_id: DownloadResolutionResult(
abs_path=str(svg),
content_type=stored_mime_type,
download_name="thumb.svg",
diff --git a/tests-unit/seeder_test/test_seeder.py b/tests-unit/seeder_test/test_seeder.py
index 55f813585..c8544b29e 100644
--- a/tests-unit/seeder_test/test_seeder.py
+++ b/tests-unit/seeder_test/test_seeder.py
@@ -1,1107 +1,24 @@
-"""Unit tests for the _AssetSeeder background scanning class."""
-
-import threading
-import time
-from unittest.mock import patch
-
-import pytest
-
-from app.assets.database.queries.asset_reference import UnenrichedReferenceRow
-from app.assets.seeder import _AssetSeeder, Progress, ScanInProgressError, ScanPhase, State
-
-
-def fresh_seeder():
- """Create a fresh _AssetSeeder instance for testing."""
- seeder = _AssetSeeder()
- yield seeder
- seeder.shutdown(timeout=1.0)
-
-
-def mock_dependencies():
- """Mock all external dependencies for isolated testing."""
- with (
- patch("app.assets.seeder.dependencies_available", return_value=True),
- patch("app.assets.seeder.sync_root_safely", return_value=set()),
- patch("app.assets.seeder.collect_paths_for_roots", return_value=[]),
- patch("app.assets.seeder.build_asset_specs", return_value=([], set(), 0)),
- patch("app.assets.seeder.insert_asset_specs", return_value=0),
- patch("app.assets.seeder.get_unenriched_assets_for_roots", return_value=[]),
- patch("app.assets.seeder.enrich_assets_batch", return_value=(0, 0)),
- ):
- yield
-
-
-class TestSeederStateTransitions:
- """Test state machine transitions."""
-
- def test_initial_state_is_idle(self, fresh_seeder: _AssetSeeder):
- assert fresh_seeder.get_status().state == State.IDLE
-
- def test_start_transitions_to_running(
- self, fresh_seeder: _AssetSeeder, mock_dependencies
- ):
- barrier = threading.Event()
- reached = threading.Event()
-
- def slow_collect(*args):
- reached.set()
- barrier.wait(timeout=5.0)
- return []
-
- with patch(
- "app.assets.seeder.collect_paths_for_roots", side_effect=slow_collect
- ):
- started = fresh_seeder.start(roots=("models",))
- assert started is True
- assert reached.wait(timeout=2.0)
- assert fresh_seeder.get_status().state == State.RUNNING
-
- barrier.set()
-
- def test_start_while_running_returns_false(
- self, fresh_seeder: _AssetSeeder, mock_dependencies
- ):
- barrier = threading.Event()
- reached = threading.Event()
-
- def slow_collect(*args):
- reached.set()
- barrier.wait(timeout=5.0)
- return []
-
- with patch(
- "app.assets.seeder.collect_paths_for_roots", side_effect=slow_collect
- ):
- fresh_seeder.start(roots=("models",))
- assert reached.wait(timeout=2.0)
-
- second_start = fresh_seeder.start(roots=("models",))
- assert second_start is False
-
- barrier.set()
-
- def test_cancel_transitions_to_cancelling(
- self, fresh_seeder: _AssetSeeder, mock_dependencies
- ):
- barrier = threading.Event()
- reached = threading.Event()
-
- def slow_collect(*args):
- reached.set()
- barrier.wait(timeout=5.0)
- return []
-
- with patch(
- "app.assets.seeder.collect_paths_for_roots", side_effect=slow_collect
- ):
- fresh_seeder.start(roots=("models",))
- assert reached.wait(timeout=2.0)
-
- cancelled = fresh_seeder.cancel()
- assert cancelled is True
- assert fresh_seeder.get_status().state == State.CANCELLING
-
- barrier.set()
-
- def test_cancel_when_idle_returns_false(self, fresh_seeder: _AssetSeeder):
- cancelled = fresh_seeder.cancel()
- assert cancelled is False
-
- def test_state_returns_to_idle_after_completion(
- self, fresh_seeder: _AssetSeeder, mock_dependencies
- ):
- fresh_seeder.start(roots=("models",))
- completed = fresh_seeder.wait(timeout=5.0)
- assert completed is True
- assert fresh_seeder.get_status().state == State.IDLE
-
-
-class TestSeederWait:
- """Test wait() behavior."""
-
- def test_wait_blocks_until_complete(
- self, fresh_seeder: _AssetSeeder, mock_dependencies
- ):
- fresh_seeder.start(roots=("models",))
- completed = fresh_seeder.wait(timeout=5.0)
- assert completed is True
- assert fresh_seeder.get_status().state == State.IDLE
-
- def test_wait_returns_false_on_timeout(
- self, fresh_seeder: _AssetSeeder, mock_dependencies
- ):
- barrier = threading.Event()
-
- def slow_collect(*args):
- barrier.wait(timeout=10.0)
- return []
-
- with patch(
- "app.assets.seeder.collect_paths_for_roots", side_effect=slow_collect
- ):
- fresh_seeder.start(roots=("models",))
- completed = fresh_seeder.wait(timeout=0.1)
- assert completed is False
-
- barrier.set()
-
- def test_wait_when_idle_returns_true(self, fresh_seeder: _AssetSeeder):
- completed = fresh_seeder.wait(timeout=1.0)
- assert completed is True
-
-
-class TestSeederProgress:
- """Test progress tracking."""
-
- def test_get_status_returns_progress_during_scan(
- self, fresh_seeder: _AssetSeeder
- ):
- barrier = threading.Event()
- reached = threading.Event()
-
- def slow_build(*args, **kwargs):
- reached.set()
- barrier.wait(timeout=5.0)
- return ([], set(), 0)
-
- paths = ["/path/file1.safetensors", "/path/file2.safetensors"]
-
- with (
- patch("app.assets.seeder.dependencies_available", return_value=True),
- patch("app.assets.seeder.sync_root_safely", return_value=set()),
- patch("app.assets.seeder.collect_paths_for_roots", return_value=paths),
- patch("app.assets.seeder.build_asset_specs", side_effect=slow_build),
- patch("app.assets.seeder.insert_asset_specs", return_value=0),
- patch("app.assets.seeder.get_unenriched_assets_for_roots", return_value=[]),
- patch("app.assets.seeder.enrich_assets_batch", return_value=(0, 0)),
- ):
- fresh_seeder.start(roots=("models",))
- assert reached.wait(timeout=2.0)
-
- status = fresh_seeder.get_status()
- assert status.state == State.RUNNING
- assert status.progress is not None
- assert status.progress.total == 2
-
- barrier.set()
-
- def test_progress_callback_is_invoked(
- self, fresh_seeder: _AssetSeeder, mock_dependencies
- ):
- progress_updates: list[Progress] = []
-
- def callback(p: Progress):
- progress_updates.append(p)
-
- with patch(
- "app.assets.seeder.collect_paths_for_roots",
- return_value=[f"/path/file{i}.safetensors" for i in range(10)],
- ):
- fresh_seeder.start(roots=("models",), progress_callback=callback)
- fresh_seeder.wait(timeout=5.0)
-
- assert len(progress_updates) > 0
-
-
-class TestSeederCancellation:
- """Test cancellation behavior."""
-
- def test_scan_commits_partial_progress_on_cancellation(
- self, fresh_seeder: _AssetSeeder
- ):
- insert_count = 0
- barrier = threading.Event()
- first_insert_done = threading.Event()
-
- def slow_insert(specs, tags):
- nonlocal insert_count
- insert_count += 1
- if insert_count == 1:
- first_insert_done.set()
- if insert_count >= 2:
- barrier.wait(timeout=5.0)
- return len(specs)
-
- paths = [f"/path/file{i}.safetensors" for i in range(1500)]
- specs = [
- {
- "abs_path": p,
- "size_bytes": 100,
- "mtime_ns": 0,
- "info_name": f"file{i}",
- "tags": [],
- "fname": f"file{i}",
- "metadata": None,
- "hash": None,
- "mime_type": None,
- }
- for i, p in enumerate(paths)
- ]
-
- with (
- patch("app.assets.seeder.dependencies_available", return_value=True),
- patch("app.assets.seeder.sync_root_safely", return_value=set()),
- patch("app.assets.seeder.collect_paths_for_roots", return_value=paths),
- patch(
- "app.assets.seeder.build_asset_specs", return_value=(specs, set(), 0)
- ),
- patch("app.assets.seeder.insert_asset_specs", side_effect=slow_insert),
- patch("app.assets.seeder.get_unenriched_assets_for_roots", return_value=[]),
- patch("app.assets.seeder.enrich_assets_batch", return_value=(0, 0)),
- ):
- fresh_seeder.start(roots=("models",))
- assert first_insert_done.wait(timeout=2.0)
-
- fresh_seeder.cancel()
- barrier.set()
- fresh_seeder.wait(timeout=5.0)
-
- assert 1 <= insert_count < 3 # 1500 paths / 500 batch = 3; cancel stopped early
-
-
-class TestSeederErrorHandling:
- """Test error handling behavior."""
-
- def test_database_errors_captured_in_status(self, fresh_seeder: _AssetSeeder):
- with (
- patch("app.assets.seeder.dependencies_available", return_value=True),
- patch("app.assets.seeder.sync_root_safely", return_value=set()),
- patch(
- "app.assets.seeder.collect_paths_for_roots",
- return_value=["/path/file.safetensors"],
- ),
- patch(
- "app.assets.seeder.build_asset_specs",
- return_value=(
- [
- {
- "abs_path": "/path/file.safetensors",
- "size_bytes": 100,
- "mtime_ns": 0,
- "info_name": "file",
- "tags": [],
- "fname": "file",
- "metadata": None,
- "hash": None,
- "mime_type": None,
- }
- ],
- set(),
- 0,
- ),
- ),
- patch(
- "app.assets.seeder.insert_asset_specs",
- side_effect=Exception("DB connection failed"),
- ),
- patch("app.assets.seeder.get_unenriched_assets_for_roots", return_value=[]),
- patch("app.assets.seeder.enrich_assets_batch", return_value=(0, 0)),
- ):
- fresh_seeder.start(roots=("models",))
- fresh_seeder.wait(timeout=5.0)
-
- status = fresh_seeder.get_status()
- assert len(status.errors) > 0
- assert "DB connection failed" in status.errors[0]
-
- def test_dependencies_unavailable_captured_in_errors(
- self, fresh_seeder: _AssetSeeder
- ):
- with patch("app.assets.seeder.dependencies_available", return_value=False):
- fresh_seeder.start(roots=("models",))
- fresh_seeder.wait(timeout=5.0)
-
- status = fresh_seeder.get_status()
- assert len(status.errors) > 0
- assert "dependencies" in status.errors[0].lower()
-
- def test_thread_crash_resets_state_to_idle(self, fresh_seeder: _AssetSeeder):
- with (
- patch("app.assets.seeder.dependencies_available", return_value=True),
- patch(
- "app.assets.seeder.sync_root_safely",
- side_effect=RuntimeError("Unexpected crash"),
- ),
- ):
- fresh_seeder.start(roots=("models",))
- fresh_seeder.wait(timeout=5.0)
-
- status = fresh_seeder.get_status()
- assert status.state == State.IDLE
- assert len(status.errors) > 0
-
-
-class TestSeederThreadSafety:
- """Test thread safety of concurrent operations."""
-
- def test_concurrent_start_calls_spawn_only_one_thread(
- self, fresh_seeder: _AssetSeeder, mock_dependencies
- ):
- barrier = threading.Event()
-
- def slow_collect(*args):
- barrier.wait(timeout=5.0)
- return []
-
- with patch(
- "app.assets.seeder.collect_paths_for_roots", side_effect=slow_collect
- ):
- results = []
-
- def try_start():
- results.append(fresh_seeder.start(roots=("models",)))
-
- threads = [threading.Thread(target=try_start) for _ in range(10)]
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- barrier.set()
-
- assert sum(results) == 1
-
- def test_get_status_safe_during_scan(
- self, fresh_seeder: _AssetSeeder, mock_dependencies
- ):
- barrier = threading.Event()
- reached = threading.Event()
-
- def slow_collect(*args):
- reached.set()
- barrier.wait(timeout=5.0)
- return []
-
- with patch(
- "app.assets.seeder.collect_paths_for_roots", side_effect=slow_collect
- ):
- fresh_seeder.start(roots=("models",))
- assert reached.wait(timeout=2.0)
-
- statuses = []
- for _ in range(100):
- statuses.append(fresh_seeder.get_status())
-
- barrier.set()
-
- assert all(
- s.state in (State.RUNNING, State.IDLE, State.CANCELLING)
- for s in statuses
- )
-
-
-class TestSeederMarkMissing:
- """Test mark_missing_outside_prefixes behavior."""
-
- def test_mark_missing_when_idle(self, fresh_seeder: _AssetSeeder):
- with (
- patch("app.assets.seeder.dependencies_available", return_value=True),
- patch(
- "app.assets.seeder.get_owned_prefixes",
- return_value=["/models", "/input", "/output"],
- ),
- patch(
- "app.assets.seeder.mark_missing_outside_prefixes_safely", return_value=5
- ) as mock_mark,
- ):
- result = fresh_seeder.mark_missing_outside_prefixes()
- assert result == 5
- mock_mark.assert_called_once_with(["/models", "/input", "/output"])
-
- def test_mark_missing_raises_when_running(
- self, fresh_seeder: _AssetSeeder, mock_dependencies
- ):
- barrier = threading.Event()
- reached = threading.Event()
-
- def slow_collect(*args):
- reached.set()
- barrier.wait(timeout=5.0)
- return []
-
- with patch(
- "app.assets.seeder.collect_paths_for_roots", side_effect=slow_collect
- ):
- fresh_seeder.start(roots=("models",))
- assert reached.wait(timeout=2.0)
-
- with pytest.raises(ScanInProgressError):
- fresh_seeder.mark_missing_outside_prefixes()
-
- barrier.set()
-
- def test_mark_missing_returns_zero_when_dependencies_unavailable(
- self, fresh_seeder: _AssetSeeder
- ):
- with patch("app.assets.seeder.dependencies_available", return_value=False):
- result = fresh_seeder.mark_missing_outside_prefixes()
- assert result == 0
-
- def test_prune_first_flag_triggers_mark_missing_before_scan(
- self, fresh_seeder: _AssetSeeder
- ):
- call_order = []
-
- def track_mark(prefixes):
- call_order.append("mark_missing")
- return 3
-
- def track_sync(root):
- call_order.append(f"sync_{root}")
- return set()
-
- with (
- patch("app.assets.seeder.dependencies_available", return_value=True),
- patch("app.assets.seeder.get_owned_prefixes", return_value=["/models"]),
- patch("app.assets.seeder.mark_missing_outside_prefixes_safely", side_effect=track_mark),
- patch("app.assets.seeder.sync_temp_references_safely"),
- patch("app.assets.seeder.sync_root_safely", side_effect=track_sync),
- patch("app.assets.seeder.collect_paths_for_roots", return_value=[]),
- patch("app.assets.seeder.build_asset_specs", return_value=([], set(), 0)),
- patch("app.assets.seeder.insert_asset_specs", return_value=0),
- patch("app.assets.seeder.get_unenriched_assets_for_roots", return_value=[]),
- patch("app.assets.seeder.enrich_assets_batch", return_value=(0, 0)),
- ):
- fresh_seeder.start(roots=("models",), prune_first=True)
- fresh_seeder.wait(timeout=5.0)
-
- assert call_order[0] == "mark_missing"
- assert "sync_models" in call_order
-
- def test_prune_first_flag_reconciles_temp_references(
- self, fresh_seeder: _AssetSeeder
- ):
- with (
- patch("app.assets.seeder.dependencies_available", return_value=True),
- patch("app.assets.seeder.get_owned_prefixes", return_value=["/models"]),
- patch("app.assets.seeder.mark_missing_outside_prefixes_safely", return_value=0),
- patch("app.assets.seeder.sync_temp_references_safely") as sync_temp,
- patch("app.assets.seeder.sync_root_safely", return_value=set()),
- patch("app.assets.seeder.collect_paths_for_roots", return_value=[]),
- patch("app.assets.seeder.build_asset_specs", return_value=([], set(), 0)),
- patch("app.assets.seeder.insert_asset_specs", return_value=0),
- patch("app.assets.seeder.get_unenriched_assets_for_roots", return_value=[]),
- patch("app.assets.seeder.enrich_assets_batch", return_value=(0, 0)),
- ):
- fresh_seeder.start(roots=("models",), prune_first=True)
- fresh_seeder.wait(timeout=5.0)
-
- assert sync_temp.called, (
- "temp is not a scan root, so the scan must reconcile it explicitly "
- "or files wiped at startup stay listed"
- )
-
-
-class TestSeederPhases:
- """Test phased scanning behavior."""
-
- def test_start_fast_only_runs_fast_phase(self, fresh_seeder: _AssetSeeder):
- """Verify start_fast only runs the fast phase."""
- fast_called = []
- enrich_called = []
-
- def track_fast(*args, **kwargs):
- fast_called.append(True)
- return ([], set(), 0)
-
- def track_enrich(*args, **kwargs):
- enrich_called.append(True)
- return []
-
- with (
- patch("app.assets.seeder.dependencies_available", return_value=True),
- patch("app.assets.seeder.sync_root_safely", return_value=set()),
- patch("app.assets.seeder.collect_paths_for_roots", return_value=[]),
- patch("app.assets.seeder.build_asset_specs", side_effect=track_fast),
- patch("app.assets.seeder.insert_asset_specs", return_value=0),
- patch("app.assets.seeder.get_unenriched_assets_for_roots", side_effect=track_enrich),
- patch("app.assets.seeder.enrich_assets_batch", return_value=(0, 0)),
- ):
- fresh_seeder.start_fast(roots=("models",))
- fresh_seeder.wait(timeout=5.0)
-
- assert len(fast_called) == 1
- assert len(enrich_called) == 0
-
- def test_start_enrich_only_runs_enrich_phase(self, fresh_seeder: _AssetSeeder):
- """Verify start_enrich only runs the enrich phase."""
- fast_called = []
- enrich_called = []
-
- def track_fast(*args, **kwargs):
- fast_called.append(True)
- return ([], set(), 0)
-
- def track_enrich(*args, **kwargs):
- enrich_called.append(True)
- return []
-
- with (
- patch("app.assets.seeder.dependencies_available", return_value=True),
- patch("app.assets.seeder.sync_root_safely", return_value=set()),
- patch("app.assets.seeder.collect_paths_for_roots", return_value=[]),
- patch("app.assets.seeder.build_asset_specs", side_effect=track_fast),
- patch("app.assets.seeder.insert_asset_specs", return_value=0),
- patch("app.assets.seeder.get_unenriched_assets_for_roots", side_effect=track_enrich),
- patch("app.assets.seeder.enrich_assets_batch", return_value=(0, 0)),
- ):
- fresh_seeder.start_enrich(roots=("models",))
- fresh_seeder.wait(timeout=5.0)
-
- assert len(fast_called) == 0
- assert len(enrich_called) == 1
-
- def test_full_scan_runs_both_phases(self, fresh_seeder: _AssetSeeder):
- """Verify full scan runs both fast and enrich phases."""
- fast_called = []
- enrich_called = []
-
- def track_fast(*args, **kwargs):
- fast_called.append(True)
- return ([], set(), 0)
-
- def track_enrich(*args, **kwargs):
- enrich_called.append(True)
- return []
-
- with (
- patch("app.assets.seeder.dependencies_available", return_value=True),
- patch("app.assets.seeder.sync_root_safely", return_value=set()),
- patch("app.assets.seeder.collect_paths_for_roots", return_value=[]),
- patch("app.assets.seeder.build_asset_specs", side_effect=track_fast),
- patch("app.assets.seeder.insert_asset_specs", return_value=0),
- patch("app.assets.seeder.get_unenriched_assets_for_roots", side_effect=track_enrich),
- patch("app.assets.seeder.enrich_assets_batch", return_value=(0, 0)),
- ):
- fresh_seeder.start(roots=("models",), phase=ScanPhase.FULL)
- fresh_seeder.wait(timeout=5.0)
-
- assert len(fast_called) == 1
- assert len(enrich_called) == 1
-
-
-class TestSeederPauseResume:
- """Test pause/resume behavior."""
-
- def test_pause_transitions_to_paused(
- self, fresh_seeder: _AssetSeeder, mock_dependencies
- ):
- barrier = threading.Event()
- reached = threading.Event()
-
- def slow_collect(*args):
- reached.set()
- barrier.wait(timeout=5.0)
- return []
-
- with patch(
- "app.assets.seeder.collect_paths_for_roots", side_effect=slow_collect
- ):
- fresh_seeder.start(roots=("models",))
- assert reached.wait(timeout=2.0)
-
- paused = fresh_seeder.pause()
- assert paused is True
- assert fresh_seeder.get_status().state == State.PAUSED
-
- barrier.set()
-
- def test_pause_when_idle_returns_false(self, fresh_seeder: _AssetSeeder):
- paused = fresh_seeder.pause()
- assert paused is False
-
- def test_resume_returns_to_running(
- self, fresh_seeder: _AssetSeeder, mock_dependencies
- ):
- barrier = threading.Event()
- reached = threading.Event()
-
- def slow_collect(*args):
- reached.set()
- barrier.wait(timeout=5.0)
- return []
-
- with patch(
- "app.assets.seeder.collect_paths_for_roots", side_effect=slow_collect
- ):
- fresh_seeder.start(roots=("models",))
- assert reached.wait(timeout=2.0)
-
- fresh_seeder.pause()
- assert fresh_seeder.get_status().state == State.PAUSED
-
- resumed = fresh_seeder.resume()
- assert resumed is True
- assert fresh_seeder.get_status().state == State.RUNNING
-
- barrier.set()
-
- def test_resume_when_not_paused_returns_false(
- self, fresh_seeder: _AssetSeeder, mock_dependencies
- ):
- barrier = threading.Event()
- reached = threading.Event()
-
- def slow_collect(*args):
- reached.set()
- barrier.wait(timeout=5.0)
- return []
-
- with patch(
- "app.assets.seeder.collect_paths_for_roots", side_effect=slow_collect
- ):
- fresh_seeder.start(roots=("models",))
- assert reached.wait(timeout=2.0)
-
- resumed = fresh_seeder.resume()
- assert resumed is False
-
- barrier.set()
-
- def test_cancel_while_paused_works(
- self, fresh_seeder: _AssetSeeder, mock_dependencies
- ):
- barrier = threading.Event()
- reached_checkpoint = threading.Event()
-
- def slow_collect(*args):
- reached_checkpoint.set()
- barrier.wait(timeout=5.0)
- return []
-
- with patch(
- "app.assets.seeder.collect_paths_for_roots", side_effect=slow_collect
- ):
- fresh_seeder.start(roots=("models",))
- assert reached_checkpoint.wait(timeout=2.0)
-
- fresh_seeder.pause()
- assert fresh_seeder.get_status().state == State.PAUSED
-
- cancelled = fresh_seeder.cancel()
- assert cancelled is True
-
- barrier.set()
- fresh_seeder.wait(timeout=5.0)
- assert fresh_seeder.get_status().state == State.IDLE
-
-class TestSeederStopRestart:
- """Test stop and restart behavior."""
-
- def test_stop_is_alias_for_cancel(
- self, fresh_seeder: _AssetSeeder, mock_dependencies
- ):
- barrier = threading.Event()
- reached = threading.Event()
-
- def slow_collect(*args):
- reached.set()
- barrier.wait(timeout=5.0)
- return []
-
- with patch(
- "app.assets.seeder.collect_paths_for_roots", side_effect=slow_collect
- ):
- fresh_seeder.start(roots=("models",))
- assert reached.wait(timeout=2.0)
-
- stopped = fresh_seeder.stop()
- assert stopped is True
- assert fresh_seeder.get_status().state == State.CANCELLING
-
- barrier.set()
-
- def test_restart_cancels_and_starts_new_scan(
- self, fresh_seeder: _AssetSeeder, mock_dependencies
- ):
- barrier = threading.Event()
- reached = threading.Event()
- start_count = 0
-
- def slow_collect(*args):
- nonlocal start_count
- start_count += 1
- if start_count == 1:
- reached.set()
- barrier.wait(timeout=5.0)
- return []
-
- with patch(
- "app.assets.seeder.collect_paths_for_roots", side_effect=slow_collect
- ):
- fresh_seeder.start(roots=("models",))
- assert reached.wait(timeout=2.0)
-
- barrier.set()
- restarted = fresh_seeder.restart()
- assert restarted is True
-
- fresh_seeder.wait(timeout=5.0)
- assert start_count == 2
-
- def test_restart_preserves_previous_params(self, fresh_seeder: _AssetSeeder):
- """Verify restart uses previous params when not overridden."""
- collected_roots = []
-
- def track_collect(roots):
- collected_roots.append(roots)
- return []
-
- with (
- patch("app.assets.seeder.dependencies_available", return_value=True),
- patch("app.assets.seeder.sync_root_safely", return_value=set()),
- patch("app.assets.seeder.collect_paths_for_roots", side_effect=track_collect),
- patch("app.assets.seeder.build_asset_specs", return_value=([], set(), 0)),
- patch("app.assets.seeder.insert_asset_specs", return_value=0),
- patch("app.assets.seeder.get_unenriched_assets_for_roots", return_value=[]),
- patch("app.assets.seeder.enrich_assets_batch", return_value=(0, 0)),
- ):
- fresh_seeder.start(roots=("input", "output"))
- fresh_seeder.wait(timeout=5.0)
-
- fresh_seeder.restart()
- fresh_seeder.wait(timeout=5.0)
-
- assert len(collected_roots) == 2
- assert collected_roots[0] == ("input", "output")
- assert collected_roots[1] == ("input", "output")
-
- def test_restart_can_override_params(self, fresh_seeder: _AssetSeeder):
- """Verify restart can override previous params."""
- collected_roots = []
-
- def track_collect(roots):
- collected_roots.append(roots)
- return []
-
- with (
- patch("app.assets.seeder.dependencies_available", return_value=True),
- patch("app.assets.seeder.sync_root_safely", return_value=set()),
- patch("app.assets.seeder.collect_paths_for_roots", side_effect=track_collect),
- patch("app.assets.seeder.build_asset_specs", return_value=([], set(), 0)),
- patch("app.assets.seeder.insert_asset_specs", return_value=0),
- patch("app.assets.seeder.get_unenriched_assets_for_roots", return_value=[]),
- patch("app.assets.seeder.enrich_assets_batch", return_value=(0, 0)),
- ):
- fresh_seeder.start(roots=("models",))
- fresh_seeder.wait(timeout=5.0)
-
- fresh_seeder.restart(roots=("input",))
- fresh_seeder.wait(timeout=5.0)
-
- assert len(collected_roots) == 2
- assert collected_roots[0] == ("models",)
- assert collected_roots[1] == ("input",)
-
-
-class TestEnqueueEnrichHandoff:
- """Test that the drain of _pending_enrich is atomic with start_enrich."""
-
- def test_pending_enrich_runs_after_scan_completes(
- self, fresh_seeder: _AssetSeeder, mock_dependencies
- ):
- """A queued enrich request runs automatically when a scan finishes."""
- enrich_roots_seen: list[tuple] = []
- original_start = fresh_seeder.start
-
- def tracking_start(*args, **kwargs):
- phase = kwargs.get("phase")
- roots = kwargs.get("roots", args[0] if args else None)
- result = original_start(*args, **kwargs)
- if phase == ScanPhase.ENRICH and result:
- enrich_roots_seen.append(roots)
- return result
-
- fresh_seeder.start = tracking_start
-
- # Start a fast scan, then enqueue an enrich while it's running
- barrier = threading.Event()
- reached = threading.Event()
-
- def slow_collect(*args):
- reached.set()
- barrier.wait(timeout=5.0)
- return []
-
- with patch(
- "app.assets.seeder.collect_paths_for_roots", side_effect=slow_collect
- ):
- fresh_seeder.start(roots=("models",), phase=ScanPhase.FAST)
- assert reached.wait(timeout=2.0)
-
- queued = fresh_seeder.enqueue_enrich(
- roots=("input",), compute_hashes=True
- )
- assert queued is False # queued, not started immediately
-
- barrier.set()
-
- # Wait for the original scan + the auto-started enrich scan
- deadline = time.monotonic() + 5.0
- while fresh_seeder.get_status().state != State.IDLE and time.monotonic() < deadline:
- time.sleep(0.05)
-
- assert enrich_roots_seen == [("input",)]
-
- def test_enqueue_enrich_during_drain_does_not_lose_work(
- self, fresh_seeder: _AssetSeeder, mock_dependencies
- ):
- """enqueue_enrich called concurrently with drain cannot drop work.
-
- Simulates the race: another thread calls enqueue_enrich right as the
- scan thread is draining _pending_enrich. The enqueue must either be
- picked up by the draining scan or successfully start its own scan.
- """
- barrier = threading.Event()
- reached = threading.Event()
- enrich_started = threading.Event()
-
- enrich_call_count = 0
-
- def slow_collect(*args):
- reached.set()
- barrier.wait(timeout=5.0)
- return []
-
- # Track how many times start_enrich actually fires
- real_start_enrich = fresh_seeder.start_enrich
- enrich_roots_seen: list[tuple] = []
-
- def tracking_start_enrich(**kwargs):
- nonlocal enrich_call_count
- enrich_call_count += 1
- enrich_roots_seen.append(kwargs.get("roots"))
- result = real_start_enrich(**kwargs)
- if result:
- enrich_started.set()
- return result
-
- fresh_seeder.start_enrich = tracking_start_enrich
-
- with patch(
- "app.assets.seeder.collect_paths_for_roots", side_effect=slow_collect
- ):
- # Start a scan
- fresh_seeder.start(roots=("models",), phase=ScanPhase.FAST)
- assert reached.wait(timeout=2.0)
-
- # Queue an enrich while scan is running
- fresh_seeder.enqueue_enrich(roots=("output",), compute_hashes=False)
-
- # Let scan finish — drain will fire start_enrich atomically
- barrier.set()
-
- # Wait for drain to complete and the enrich scan to start
- assert enrich_started.wait(timeout=5.0), "Enrich scan was never started from drain"
- assert ("output",) in enrich_roots_seen
-
- def test_concurrent_enqueue_during_drain_not_lost(
- self, fresh_seeder: _AssetSeeder,
- ):
- """A second enqueue_enrich arriving while drain is in progress is not lost.
-
- Because the drain now holds _lock through the start_enrich call,
- a concurrent enqueue_enrich will block until start_enrich has
- transitioned state to RUNNING, then the enqueue will queue its
- payload as _pending_enrich for the *next* drain.
- """
- scan_barrier = threading.Event()
- scan_reached = threading.Event()
- enrich_barrier = threading.Event()
- enrich_reached = threading.Event()
-
- collect_call = 0
-
- def gated_collect(*args):
- nonlocal collect_call
- collect_call += 1
- if collect_call == 1:
- # First call: the initial fast scan
- scan_reached.set()
- scan_barrier.wait(timeout=5.0)
- return []
-
- enrich_call = 0
-
- def gated_get_unenriched(*args, **kwargs):
- nonlocal enrich_call
- enrich_call += 1
- if enrich_call == 1:
- # First enrich batch: signal and block
- enrich_reached.set()
- enrich_barrier.wait(timeout=5.0)
- return []
-
- with (
- patch("app.assets.seeder.dependencies_available", return_value=True),
- patch("app.assets.seeder.sync_root_safely", return_value=set()),
- patch("app.assets.seeder.collect_paths_for_roots", side_effect=gated_collect),
- patch("app.assets.seeder.build_asset_specs", return_value=([], set(), 0)),
- patch("app.assets.seeder.insert_asset_specs", return_value=0),
- patch("app.assets.seeder.get_unenriched_assets_for_roots", side_effect=gated_get_unenriched),
- patch("app.assets.seeder.enrich_assets_batch", return_value=(0, 0)),
- ):
- # 1. Start fast scan
- fresh_seeder.start(roots=("models",), phase=ScanPhase.FAST)
- assert scan_reached.wait(timeout=2.0)
-
- # 2. Queue enrich while fast scan is running
- queued = fresh_seeder.enqueue_enrich(
- roots=("input",), compute_hashes=False
- )
- assert queued is False
-
- # 3. Let the fast scan finish — drain will start the enrich scan
- scan_barrier.set()
-
- # 4. Wait until the drained enrich scan is running
- assert enrich_reached.wait(timeout=5.0)
-
- # 5. Now enqueue another enrich while the drained scan is running
- queued2 = fresh_seeder.enqueue_enrich(
- roots=("output",), compute_hashes=True
- )
- assert queued2 is False # should be queued, not started
-
- # Verify _pending_enrich was set (the second enqueue was captured)
- with fresh_seeder._lock:
- assert fresh_seeder._pending_enrich is not None
- assert "output" in fresh_seeder._pending_enrich["roots"]
-
- # Let the enrich scan finish
- enrich_barrier.set()
-
- deadline = time.monotonic() + 5.0
- while fresh_seeder.get_status().state != State.IDLE and time.monotonic() < deadline:
- time.sleep(0.05)
-
-
-def _make_row(ref_id: str, asset_id: str = "a1") -> UnenrichedReferenceRow:
- return UnenrichedReferenceRow(
- reference_id=ref_id, asset_id=asset_id,
- file_path=f"/fake/{ref_id}.bin", enrichment_level=0,
- )
-
-
-class TestEnrichPhaseDefensiveLogic:
- """Test skip_ids filtering and consecutive_empty termination."""
-
- def test_failed_refs_are_skipped_on_subsequent_batches(
- self, fresh_seeder: _AssetSeeder,
- ):
- """References that fail enrichment are filtered out of future batches."""
- row_a = _make_row("r1")
- row_b = _make_row("r2")
- call_count = 0
-
- def fake_get_unenriched(*args, **kwargs):
- nonlocal call_count
- call_count += 1
- if call_count <= 2:
- return [row_a, row_b]
- return []
-
- enriched_refs: list[list[str]] = []
-
- def fake_enrich(rows, **kwargs):
- ref_ids = [r.reference_id for r in rows]
- enriched_refs.append(ref_ids)
- # r1 always fails, r2 succeeds
- failed = [r.reference_id for r in rows if r.reference_id == "r1"]
- enriched = len(rows) - len(failed)
- return enriched, failed
-
- with (
- patch("app.assets.seeder.dependencies_available", return_value=True),
- patch("app.assets.seeder.sync_root_safely", return_value=set()),
- patch("app.assets.seeder.collect_paths_for_roots", return_value=[]),
- patch("app.assets.seeder.build_asset_specs", return_value=([], set(), 0)),
- patch("app.assets.seeder.insert_asset_specs", return_value=0),
- patch("app.assets.seeder.get_unenriched_assets_for_roots", side_effect=fake_get_unenriched),
- patch("app.assets.seeder.enrich_assets_batch", side_effect=fake_enrich),
- ):
- fresh_seeder.start(roots=("models",), phase=ScanPhase.ENRICH)
- fresh_seeder.wait(timeout=5.0)
-
- # First batch: both refs attempted
- assert "r1" in enriched_refs[0]
- assert "r2" in enriched_refs[0]
- # Second batch: r1 filtered out
- assert "r1" not in enriched_refs[1]
- assert "r2" in enriched_refs[1]
-
- def test_stops_after_consecutive_empty_batches(
- self, fresh_seeder: _AssetSeeder,
- ):
- """Enrich phase terminates after 3 consecutive batches with zero progress."""
- row = _make_row("r1")
- batch_count = 0
-
- def fake_get_unenriched(*args, **kwargs):
- nonlocal batch_count
- batch_count += 1
- # Always return the same row (simulating a permanently failing ref)
- return [row]
-
- def fake_enrich(rows, **kwargs):
- # Always fail — zero enriched, all failed
- return 0, [r.reference_id for r in rows]
-
- with (
- patch("app.assets.seeder.dependencies_available", return_value=True),
- patch("app.assets.seeder.sync_root_safely", return_value=set()),
- patch("app.assets.seeder.collect_paths_for_roots", return_value=[]),
- patch("app.assets.seeder.build_asset_specs", return_value=([], set(), 0)),
- patch("app.assets.seeder.insert_asset_specs", return_value=0),
- patch("app.assets.seeder.get_unenriched_assets_for_roots", side_effect=fake_get_unenriched),
- patch("app.assets.seeder.enrich_assets_batch", side_effect=fake_enrich),
- ):
- fresh_seeder.start(roots=("models",), phase=ScanPhase.ENRICH)
- fresh_seeder.wait(timeout=5.0)
-
- # Should stop after exactly 3 consecutive empty batches
- # Batch 1: returns row, enrich fails → filtered out in batch 2+
- # But get_unenriched keeps returning it, filter removes it → empty → break
- # Actually: batch 1 has row, fails. Batch 2 get_unenriched returns [row],
- # skip_ids filters it → empty list → breaks via `if not unenriched: break`
- # So it terminates in 2 calls to get_unenriched.
- assert batch_count == 2
-
- def test_consecutive_empty_counter_resets_on_success(
- self, fresh_seeder: _AssetSeeder,
- ):
- """A successful batch resets the consecutive empty counter."""
- call_count = 0
-
- def fake_get_unenriched(*args, **kwargs):
- nonlocal call_count
- call_count += 1
- if call_count <= 6:
- return [_make_row(f"r{call_count}", f"a{call_count}")]
- return []
-
- def fake_enrich(rows, **kwargs):
- ref_id = rows[0].reference_id
- # Fail batches 1-2, succeed batch 3, fail batches 4-5, succeed batch 6
- if ref_id in ("r1", "r2", "r4", "r5"):
- return 0, [ref_id]
- return 1, []
-
- with (
- patch("app.assets.seeder.dependencies_available", return_value=True),
- patch("app.assets.seeder.sync_root_safely", return_value=set()),
- patch("app.assets.seeder.collect_paths_for_roots", return_value=[]),
- patch("app.assets.seeder.build_asset_specs", return_value=([], set(), 0)),
- patch("app.assets.seeder.insert_asset_specs", return_value=0),
- patch("app.assets.seeder.get_unenriched_assets_for_roots", side_effect=fake_get_unenriched),
- patch("app.assets.seeder.enrich_assets_batch", side_effect=fake_enrich),
- ):
- fresh_seeder.start(roots=("models",), phase=ScanPhase.ENRICH)
- fresh_seeder.wait(timeout=5.0)
-
- # All 6 batches should run + 1 final call returning empty
- assert call_count == 7
- status = fresh_seeder.get_status()
- assert status.state == State.IDLE
+from sqlalchemy import create_engine
+from sqlalchemy.orm import Session
+
+from app.assets.database.models import Base
+from app.assets.database.queries import create_content, create_record, mark_content_missing
+
+
+def test_seeder_models_missing_as_content_state():
+ engine = create_engine("sqlite:///:memory:")
+ Base.metadata.create_all(engine)
+ with Session(engine) as session:
+ content = create_content(session, "/models/checkpoints/model.safetensors", hash=None)
+ record = create_record(
+ session,
+ content.id,
+ "model.safetensors",
+ loader_path="checkpoints/model.safetensors",
+ tags=["models", "model_type:checkpoints"],
+ )
+
+ mark_content_missing(session, content.id)
+
+ assert content.is_missing is True
+ assert record.content_id == content.id
diff --git a/tests/execution/test_execution.py b/tests/execution/test_execution.py
index c914d2feb..312a7a15c 100644
--- a/tests/execution/test_execution.py
+++ b/tests/execution/test_execution.py
@@ -13,7 +13,303 @@ import uuid
import urllib.request
import urllib.parse
import urllib.error
+import os
+from pathlib import PurePosixPath
from comfy_execution.graph_utils import GraphBuilder, Node
+from app.assets.scanner_admission import _should_skip_extension
+from app.assets.services.file_utils import list_files_recursively
+
+
+ASSET_HEALTH_TIMEOUT_SECONDS = 120
+ASSET_SEED_RETRY_ATTEMPTS = 10
+ASSET_SEED_RETRY_DELAY_SECONDS = 3
+ASSET_STATUS_POLL_DELAY_SECONDS = 1
+ASSET_CAPTURE_TAIL_LINES = 80
+
+# Static prefixes of app/assets exception-path messages; re-diff against logging.exception sites when the assets logging PR lands.
+ASSET_FATAL_LOG_PREFIXES = (
+ "get_asset failed for reference_id=",
+ "upload_asset failed for tenant_id=",
+ "update_asset failed for reference_id=",
+ "delete_asset_reference failed for reference_id=",
+ "add_tags_to_asset failed for reference_id=",
+ "remove_tags_from_asset failed for reference_id=",
+ "check_hash_exists failed for hash=",
+ "Temp DB row wipe failed; skipping filesystem cleanup",
+ "Asset startup maintenance failed",
+ "Temp DB row wipe failed during shutdown",
+ "Asset shutdown cleanup failed",
+ "fast DB scan failed for ",
+ "temp reference sync failed: ",
+ "marking missing assets failed: ",
+ "Failed to enrich ",
+ "Asset scan failed",
+ "Batch insert failed at offset ",
+ "Failed to discard orphan content ",
+ "Failed to register cached output: ",
+ "Failed to register executed output: ",
+ "Failed to register uploaded image as asset",
+ "WARNING: blake3 package not installed",
+)
+
+
+def _asset_json_request(url, deadline, data=None):
+ remaining = deadline - time.monotonic()
+ if remaining <= 0:
+ raise AssertionError(
+ f"Asset health check failure (i): deadline/transport failure before requesting {url}"
+ )
+
+ request = urllib.request.Request(url, data=data)
+ if data is not None:
+ request.add_header("Content-Type", "application/json")
+
+ try:
+ with urllib.request.urlopen(request, timeout=max(0.01, remaining)) as response:
+ status = response.status
+ raw_payload = response.read()
+ except urllib.error.HTTPError as error:
+ status = error.code
+ raw_payload = error.read()
+ except (TimeoutError, ConnectionError, urllib.error.URLError) as error:
+ raise AssertionError(
+ f"Asset health check failure (i): deadline/transport failure requesting {url}: {error!r}"
+ ) from error
+
+ try:
+ payload = json.loads(raw_payload)
+ except (json.JSONDecodeError, UnicodeDecodeError) as error:
+ rendered_payload = raw_payload.decode("utf-8", errors="replace")
+ raise AssertionError(
+ "Asset health check failure (iv): malformed JSON/schema "
+ f"from {url}: status={status}, payload={rendered_payload!r}"
+ ) from error
+ if not isinstance(payload, dict):
+ raise AssertionError(
+ "Asset health check failure (iv): malformed JSON/schema "
+ f"from {url}: status={status}, payload={payload!r}"
+ )
+ return status, payload
+
+
+def _seed_output_assets(base_url, deadline):
+ request_data = json.dumps({"roots": ["output"]}).encode("utf-8")
+ last_conflict_payload = None
+ for attempt in range(1, ASSET_SEED_RETRY_ATTEMPTS + 1):
+ if last_conflict_payload is not None and deadline - time.monotonic() <= 0:
+ raise AssertionError(
+ "Asset health check failure (ii): 409-retries-exhausted before the deadline; "
+ f"last payload={last_conflict_payload!r}"
+ )
+
+ url = f"{base_url}/api/assets/seed?wait=true"
+ status, payload = _asset_json_request(url, deadline, request_data)
+ if status == 409:
+ if payload.get("status") != "already_running":
+ raise AssertionError(
+ "Asset health check failure (iv): malformed JSON/schema "
+ f"from {url}: status={status}, payload={payload!r}"
+ )
+ last_conflict_payload = payload
+ if attempt == ASSET_SEED_RETRY_ATTEMPTS:
+ raise AssertionError(
+ "Asset health check failure (ii): 409-retries-exhausted after "
+ f"{attempt} attempts; last payload={payload!r}"
+ )
+ remaining = deadline - time.monotonic()
+ if remaining <= 0:
+ raise AssertionError(
+ "Asset health check failure (ii): 409-retries-exhausted before the deadline; "
+ f"last payload={payload!r}"
+ )
+ time.sleep(min(ASSET_SEED_RETRY_DELAY_SECONDS, remaining))
+ continue
+ if status != 200:
+ raise AssertionError(
+ "Asset health check failure (iv): unexpected-HTTP-status "
+ f"from {url}: status={status}, payload={payload!r}"
+ )
+
+ progress = payload.get("progress")
+ errors = payload.get("errors")
+ progress_fields = ("scanned", "total", "created", "skipped")
+ if (
+ payload.get("status") != "completed"
+ or not isinstance(progress, dict)
+ or any(type(progress.get(field)) is not int for field in progress_fields)
+ or not isinstance(errors, list)
+ ):
+ raise AssertionError(
+ "Asset health check failure (iv): malformed JSON/schema "
+ f"from {url}: status={status}, payload={payload!r}"
+ )
+ if errors:
+ raise AssertionError(
+ f"Asset health check failure (iii): scan-completed-with-errors: errors={errors!r}"
+ )
+ return
+
+
+def _wait_for_stable_asset_idle(base_url, deadline):
+ stable_reads = 0
+ last_payload = None
+ url = f"{base_url}/api/assets/seed/status"
+ while True:
+ if deadline - time.monotonic() <= 0:
+ raise AssertionError(
+ "Asset health check failure (v): idle-stability-exhaustion; "
+ f"last payload={last_payload!r}"
+ )
+ status, payload = _asset_json_request(url, deadline)
+ progress = payload.get("progress")
+ errors = payload.get("errors")
+ if (
+ status != 200
+ or not isinstance(payload.get("state"), str)
+ or not isinstance(errors, list)
+ or (progress is not None and not isinstance(progress, dict))
+ ):
+ failure = "unexpected-HTTP-status" if status != 200 else "malformed JSON/schema"
+ raise AssertionError(
+ f"Asset health check failure (iv): {failure} from {url}: "
+ f"status={status}, payload={payload!r}"
+ )
+
+ last_payload = payload
+ if payload["state"].lower() == "idle" and errors == []:
+ stable_reads += 1
+ if stable_reads == 2:
+ return
+ else:
+ stable_reads = 0
+
+ remaining = deadline - time.monotonic()
+ if remaining <= 0:
+ raise AssertionError(
+ "Asset health check failure (v): idle-stability-exhaustion; "
+ f"last payload={last_payload!r}"
+ )
+ time.sleep(min(ASSET_STATUS_POLL_DELAY_SECONDS, remaining))
+
+
+def _normalize_asset_path(path):
+ return PurePosixPath(path.replace("\\", "/")).as_posix()
+
+
+def _fetch_output_asset_paths(base_url, deadline):
+ asset_paths = set()
+ after = None
+ previous_cursor = None
+ while True:
+ query = {"tags_all": "output", "limit": "100"}
+ if after is not None:
+ query["after"] = after
+ url = f"{base_url}/api/assets?{urllib.parse.urlencode(query)}"
+ status, payload = _asset_json_request(url, deadline)
+ assets = payload.get("assets")
+ next_cursor = payload.get("next_cursor")
+ if (
+ status != 200
+ or not isinstance(assets, list)
+ or type(payload.get("total")) is not int
+ or not isinstance(payload.get("has_more"), bool)
+ or (next_cursor is not None and not isinstance(next_cursor, str))
+ ):
+ failure = "unexpected-HTTP-status" if status != 200 else "malformed JSON/schema"
+ raise AssertionError(
+ f"Asset health check failure (iv): {failure} from {url}: "
+ f"status={status}, payload={payload!r}"
+ )
+
+ for asset in assets:
+ if not isinstance(asset, dict):
+ raise AssertionError(
+ "Asset health check failure (iv): malformed JSON/schema "
+ f"from {url}: asset={asset!r}, payload={payload!r}"
+ )
+ loader_path = asset.get("loader_path")
+ if not isinstance(loader_path, str):
+ raise AssertionError(
+ "Asset health check reconcile failure: asset has missing/non-string "
+ f"loader_path: asset={asset!r}"
+ )
+ asset_paths.add(_normalize_asset_path(loader_path))
+
+ if next_cursor is None:
+ if payload["has_more"]:
+ raise AssertionError(
+ "Asset health check failure (iv): malformed JSON/schema "
+ f"from {url}: has_more=true without next_cursor, payload={payload!r}"
+ )
+ return asset_paths
+ if next_cursor == previous_cursor:
+ raise AssertionError(
+ "Asset health check reconcile failure: pagination cursor made no progress: "
+ f"cursor={next_cursor!r}, payload={payload!r}"
+ )
+ previous_cursor = next_cursor
+ after = next_cursor
+
+
+def _list_output_files_on_disk(output_dir):
+ output_root = os.path.abspath(output_dir)
+ disk_paths = set()
+ for file_path in list_files_recursively(output_root):
+ if _should_skip_extension(file_path):
+ continue
+ try:
+ stat_result = os.stat(file_path, follow_symlinks=True)
+ except OSError:
+ continue
+ if not stat_result.st_size:
+ continue
+ relative_path = os.path.relpath(file_path, output_root)
+ disk_paths.add(_normalize_asset_path(relative_path))
+ return disk_paths
+
+
+def _assert_no_fatal_asset_logs(capture_path):
+ server_output = capture_path.read_text(encoding="utf-8", errors="replace")
+ matched_prefixes = [
+ prefix for prefix in ASSET_FATAL_LOG_PREFIXES if prefix in server_output
+ ]
+ if matched_prefixes:
+ raise AssertionError(
+ "Asset health check fatal-log failure: "
+ f"matched prefixes={matched_prefixes!r}"
+ )
+
+
+def _assert_assets_healthy(listen, port, output_dir, capture_path):
+ deadline = time.monotonic() + ASSET_HEALTH_TIMEOUT_SECONDS
+ base_url = f"http://{listen}:{port}"
+ _seed_output_assets(base_url, deadline)
+ _wait_for_stable_asset_idle(base_url, deadline)
+
+ api_paths = _fetch_output_asset_paths(base_url, deadline)
+ disk_paths = _list_output_files_on_disk(output_dir)
+ if not api_paths or not disk_paths:
+ raise AssertionError(
+ "Asset health check reconcile failure: expected non-empty sets: "
+ f"api_paths={sorted(api_paths)!r}, disk_paths={sorted(disk_paths)!r}"
+ )
+ disk_only = disk_paths - api_paths
+ api_only = api_paths - disk_paths
+ if disk_only or api_only:
+ raise AssertionError(
+ "Asset health check reconcile failure: "
+ f"disk-has-but-API-lacks={sorted(disk_only)!r}, "
+ f"API-has-but-disk-lacks={sorted(api_only)!r}"
+ )
+ _assert_no_fatal_asset_logs(capture_path)
+
+
+def _capture_tail(capture_path):
+ try:
+ lines = capture_path.read_text(encoding="utf-8", errors="replace").splitlines()
+ except OSError as error:
+ return f"<unable to read capture file: {error!r}>"
+ return "\n".join(lines[-ASSET_CAPTURE_TAIL_LINES:])
def run_warmup(client, prefix="warmup"):
"""Run a simple workflow to warm up the server."""
@@ -186,9 +482,11 @@ class TestExecution:
{ "extra_args" : ["--cache-classic"], "should_cache_results" : True },
{ "extra_args" : ["--cache-lru", 100], "should_cache_results" : True },
{ "extra_args" : ["--cache-none"], "should_cache_results" : False },
+ {"extra_args": ["--enable-assets"], "should_cache_results": True, "assets": True},
])
- def server(self, args_pytest, request):
+ def server(self, args_pytest, request, tmp_path_factory):
# Start server
+ assets_enabled = request.param.get("assets")
pargs = [
'python','main.py',
'--output-directory', args_pytest["output_dir"],
@@ -198,7 +496,67 @@ class TestExecution:
'--cpu',
]
pargs += [ str(param) for param in request.param["extra_args"] ]
+ if assets_enabled:
+ assets_tmp_dir = tmp_path_factory.mktemp("execution-assets")
+ database_path = assets_tmp_dir / "assets.db"
+ capture_path = assets_tmp_dir / "server.log"
+ pargs += ["--database-url", f"sqlite:///{database_path}"]
print("Running server with args:", pargs) # noqa: T201
+ if assets_enabled:
+ capture_file = capture_path.open("a", encoding="utf-8")
+ try:
+ p = subprocess.Popen(
+ pargs, stdout=capture_file, stderr=subprocess.STDOUT
+ )
+ except OSError:
+ capture_file.close()
+ raise
+
+ health_failure = None
+ cleanup_failures = []
+ try:
+ yield request.param
+ exit_code = p.poll()
+ if exit_code is not None:
+ raise AssertionError(
+ "Asset health check server-dead failure: "
+ f"exit code={exit_code}"
+ )
+ _assert_assets_healthy(
+ args_pytest["listen"],
+ args_pytest["port"],
+ args_pytest["output_dir"],
+ capture_path,
+ )
+ except AssertionError as error:
+ health_failure = error
+ finally:
+ try:
+ p.kill()
+ except OSError as error:
+ cleanup_failures.append(f"kill failed: {error!r}")
+ finally:
+ try:
+ p.wait(timeout=10)
+ except (OSError, subprocess.TimeoutExpired) as error:
+ cleanup_failures.append(f"bounded reap failed: {error!r}")
+ finally:
+ try:
+ capture_file.close()
+ except OSError as error:
+ cleanup_failures.append(f"capture close failed: {error!r}")
+ finally:
+ torch.cuda.empty_cache()
+
+ if cleanup_failures:
+ print("Asset server cleanup warnings:", cleanup_failures) # noqa: T201
+ if health_failure is not None:
+ capture_tail = _capture_tail(capture_path)
+ raise AssertionError(
+ f"{health_failure}\nServer output tail:\n{capture_tail}"
+ ) from health_failure
+ return
+
p = subprocess.Popen(pargs)
yield request.param
p.kill()
diff --git a/tests/execution/testing_nodes/testing-pack/specific_tests.py b/tests/execution/testing_nodes/testing-pack/specific_tests.py
index 2eb5d520e..156f9b314 100644
--- a/tests/execution/testing_nodes/testing-pack/specific_tests.py
+++ b/tests/execution/testing_nodes/testing-pack/specific_tests.py
@@ -482,6 +482,67 @@ class TestOutputNodeWithSocketOutput:
result = image * value
return (result,)
+
+class TestExecutedNodeIdsChild:
+ @classmethod
+ def INPUT_TYPES(cls):
+ return {
+ "required": {
+ "value": ("STRING", {"default": "expanded-child"}),
+ },
+ }
+
+ RETURN_TYPES = ()
+ FUNCTION = "emit"
+ CATEGORY = "Testing/Nodes"
+ OUTPUT_NODE = True
+
+ def emit(self, value):
+ return {"ui": {"values": [value]}}
+
+
+class TestExecutedNodeIdsExpander:
+ @classmethod
+ def INPUT_TYPES(cls):
+ return {
+ "required": {
+ "value": ("STRING", {"default": "expanded-child"}),
+ },
+ }
+
+ RETURN_TYPES = ()
+ FUNCTION = "expand"
+ CATEGORY = "Testing/Nodes"
+ OUTPUT_NODE = True
+
+ @classmethod
+ def IS_CHANGED(cls, **kwargs):
+ return float("NaN")
+
+ def expand(self, value):
+ graph = GraphBuilder()
+ graph.node("TestExecutedNodeIdsChild", value=value)
+ return {"result": (), "expand": graph.finalize()}
+
+
+class TestExecutedNodeIdsBlocking:
+ started_event = None
+ release_event = None
+
+ @classmethod
+ def INPUT_TYPES(cls):
+ return {"required": {}}
+
+ RETURN_TYPES = ()
+ FUNCTION = "block"
+ CATEGORY = "Testing/Nodes"
+ OUTPUT_NODE = True
+
+ async def block(self):
+ self.started_event.set()
+ await self.release_event.wait()
+ return {"ui": {"completed": [True]}}
+
TEST_NODE_CLASS_MAPPINGS = {
"TestLazyMixImages": TestLazyMixImages,
"TestVariadicAverage": TestVariadicAverage,
@@ -498,6 +559,9 @@ TEST_NODE_CLASS_MAPPINGS = {
"TestSleep": TestSleep,
"TestParallelSleep": TestParallelSleep,
"TestOutputNodeWithSocketOutput": TestOutputNodeWithSocketOutput,
+ "TestExecutedNodeIdsChild": TestExecutedNodeIdsChild,
+ "TestExecutedNodeIdsExpander": TestExecutedNodeIdsExpander,
+ "TestExecutedNodeIdsBlocking": TestExecutedNodeIdsBlocking,
}
TEST_NODE_DISPLAY_NAME_MAPPINGS = {
@@ -516,4 +580,7 @@ TEST_NODE_DISPLAY_NAME_MAPPINGS = {
"TestSleep": "Test Sleep",
"TestParallelSleep": "Test Parallel Sleep",
"TestOutputNodeWithSocketOutput": "Test Output Node With Socket Output",
+ "TestExecutedNodeIdsChild": "Executed Node IDs Child",
+ "TestExecutedNodeIdsExpander": "Executed Node IDs Expander",
+ "TestExecutedNodeIdsBlocking": "Executed Node IDs Blocking",
}
diff --git a/tests/test_asset_seeder.py b/tests/test_asset_seeder.py
index 2c6727abe..710703724 100644
--- a/tests/test_asset_seeder.py
+++ b/tests/test_asset_seeder.py
@@ -1,11 +1,11 @@
-"""Tests for app.assets.seeder – enqueue_enrich and pending-queue behaviour."""
import threading
-from unittest.mock import patch
+from typing import Any
+from unittest.mock import call, patch
import pytest
-from app.assets.seeder import Progress, _AssetSeeder, State
+from app.assets.seeder import Progress, ScanPhase, _AssetSeeder, State
@pytest.fixture()
@@ -14,6 +14,33 @@ def seeder():
return _AssetSeeder()
+class TestEventSink:
+ def test_delivers_event_and_payload_to_sink(self, seeder):
+ received: list[tuple[str, dict[str, Any]]] = []
+
+ def record_event(event_type: str, data: dict[str, Any]) -> None:
+ received.append((event_type, data))
+
+ event_type = "assets.seed.started"
+ payload = {"roots": ["models"], "total": 1, "phase": "fast"}
+ seeder.set_event_sink(record_event)
+
+ seeder._emit_event(event_type, payload)
+
+ assert received == [(event_type, payload)]
+
+ def test_noop_when_no_sink_is_set(self, seeder):
+ assert seeder._emit_event("assets.seed.resumed", {}) is None
+
+ def test_swallows_raising_sink_exception(self, seeder):
+ def raise_from_sink(event_type: str, data: dict[str, Any]) -> None:
+ raise RuntimeError("boom")
+
+ seeder.set_event_sink(raise_from_sink)
+
+ assert seeder._emit_event("assets.seed.error", {"message": "failure"}) is None
+
+
# ---------------------------------------------------------------------------
# _reset_to_idle
# ---------------------------------------------------------------------------
@@ -47,144 +74,262 @@ class TestResetToIdle:
# ---------------------------------------------------------------------------
-# enqueue_enrich – immediate start when idle
+# enqueue_scan – immediate start when idle
# ---------------------------------------------------------------------------
-class TestEnqueueEnrichStartsImmediately:
+class TestEnqueueScanStartsImmediately:
def test_starts_when_idle(self, seeder):
- """enqueue_enrich should delegate to start_enrich and return True when idle."""
- with patch.object(seeder, "start_enrich", return_value=True) as mock:
- assert seeder.enqueue_enrich(roots=("output",), compute_hashes=True) is True
- mock.assert_called_once_with(roots=("output",), compute_hashes=True)
+ with patch.object(seeder, "start", return_value=True) as mock:
+ assert (
+ seeder.enqueue_scan(
+ roots=("output",), phase=ScanPhase.ENRICH, compute_hashes=True
+ )
+ is True
+ )
+ mock.assert_called_once_with(
+ roots=("output",),
+ phase=ScanPhase.ENRICH,
+ prune_first=False,
+ compute_hashes=True,
+ )
def test_no_pending_when_started_immediately(self, seeder):
- """No pending request should be stored when start_enrich succeeds."""
- with patch.object(seeder, "start_enrich", return_value=True):
- seeder.enqueue_enrich(roots=("output",))
- assert seeder._pending_enrich is None
+ with patch.object(seeder, "start", return_value=True):
+ seeder.enqueue_scan(roots=("output",), phase=ScanPhase.ENRICH)
+ assert seeder._pending_scan is None
# ---------------------------------------------------------------------------
-# enqueue_enrich – queuing when busy
+# enqueue_scan – queuing when busy
# ---------------------------------------------------------------------------
-class TestEnqueueEnrichQueuesWhenBusy:
+class TestEnqueueScanQueuesWhenBusy:
def test_queues_when_busy(self, seeder):
- """enqueue_enrich should store a pending request when seeder is busy."""
- with patch.object(seeder, "start_enrich", return_value=False):
- result = seeder.enqueue_enrich(roots=("models",), compute_hashes=False)
+ with patch.object(seeder, "start", return_value=False):
+ result = seeder.enqueue_scan(
+ roots=("models",), phase=ScanPhase.ENRICH, compute_hashes=False
+ )
assert result is False
- assert seeder._pending_enrich == {
+ assert seeder._pending_scan == {
"roots": ("models",),
+ "phase": ScanPhase.ENRICH,
"compute_hashes": False,
}
def test_queues_preserves_compute_hashes_true(self, seeder):
- with patch.object(seeder, "start_enrich", return_value=False):
- seeder.enqueue_enrich(roots=("input",), compute_hashes=True)
+ with patch.object(seeder, "start", return_value=False):
+ seeder.enqueue_scan(
+ roots=("input",), phase=ScanPhase.ENRICH, compute_hashes=True
+ )
- assert seeder._pending_enrich["compute_hashes"] is True
+ assert seeder._pending_scan["compute_hashes"] is True
# ---------------------------------------------------------------------------
-# enqueue_enrich – merging when a pending request already exists
+# enqueue_scan – merging when a pending request already exists
# ---------------------------------------------------------------------------
-class TestEnqueueEnrichMergesPending:
+class TestEnqueueScanMergesPending:
def _make_busy(self, seeder):
- """Patch start_enrich to always return False (seeder busy)."""
- return patch.object(seeder, "start_enrich", return_value=False)
+ return patch.object(seeder, "start", return_value=False)
def test_merges_roots(self, seeder):
"""A second enqueue should merge roots with the existing pending request."""
with self._make_busy(seeder):
- seeder.enqueue_enrich(roots=("models",))
- seeder.enqueue_enrich(roots=("output",))
+ seeder.enqueue_scan(roots=("models",), phase=ScanPhase.ENRICH)
+ seeder.enqueue_scan(roots=("output",), phase=ScanPhase.FAST)
- merged = set(seeder._pending_enrich["roots"])
+ merged = set(seeder._pending_scan["roots"])
assert merged == {"models", "output"}
+ assert seeder._pending_scan["phase"] is ScanPhase.FULL
def test_merges_overlapping_roots(self, seeder):
"""Duplicate roots should be deduplicated."""
with self._make_busy(seeder):
- seeder.enqueue_enrich(roots=("models", "input"))
- seeder.enqueue_enrich(roots=("input", "output"))
+ seeder.enqueue_scan(roots=("models", "input"), phase=ScanPhase.ENRICH)
+ seeder.enqueue_scan(roots=("input", "output"), phase=ScanPhase.ENRICH)
- merged = set(seeder._pending_enrich["roots"])
+ merged = set(seeder._pending_scan["roots"])
assert merged == {"models", "input", "output"}
def test_compute_hashes_sticky_true(self, seeder):
"""Once compute_hashes is True it should stay True after merging."""
with self._make_busy(seeder):
- seeder.enqueue_enrich(roots=("models",), compute_hashes=True)
- seeder.enqueue_enrich(roots=("output",), compute_hashes=False)
+ seeder.enqueue_scan(
+ roots=("models",), phase=ScanPhase.ENRICH, compute_hashes=True
+ )
+ seeder.enqueue_scan(
+ roots=("output",), phase=ScanPhase.ENRICH, compute_hashes=False
+ )
- assert seeder._pending_enrich["compute_hashes"] is True
+ assert seeder._pending_scan["compute_hashes"] is True
def test_compute_hashes_upgrades_to_true(self, seeder):
"""A later enqueue with compute_hashes=True should upgrade the pending request."""
with self._make_busy(seeder):
- seeder.enqueue_enrich(roots=("models",), compute_hashes=False)
- seeder.enqueue_enrich(roots=("output",), compute_hashes=True)
+ seeder.enqueue_scan(
+ roots=("models",), phase=ScanPhase.ENRICH, compute_hashes=False
+ )
+ seeder.enqueue_scan(
+ roots=("output",), phase=ScanPhase.ENRICH, compute_hashes=True
+ )
- assert seeder._pending_enrich["compute_hashes"] is True
+ assert seeder._pending_scan["compute_hashes"] is True
def test_compute_hashes_stays_false(self, seeder):
"""If both enqueues have compute_hashes=False it stays False."""
with self._make_busy(seeder):
- seeder.enqueue_enrich(roots=("models",), compute_hashes=False)
- seeder.enqueue_enrich(roots=("output",), compute_hashes=False)
+ seeder.enqueue_scan(
+ roots=("models",), phase=ScanPhase.ENRICH, compute_hashes=False
+ )
+ seeder.enqueue_scan(
+ roots=("output",), phase=ScanPhase.ENRICH, compute_hashes=False
+ )
- assert seeder._pending_enrich["compute_hashes"] is False
+ assert seeder._pending_scan["compute_hashes"] is False
def test_triple_merge(self, seeder):
"""Three successive enqueues should all merge correctly."""
with self._make_busy(seeder):
- seeder.enqueue_enrich(roots=("models",), compute_hashes=False)
- seeder.enqueue_enrich(roots=("input",), compute_hashes=False)
- seeder.enqueue_enrich(roots=("output",), compute_hashes=True)
+ seeder.enqueue_scan(
+ roots=("models",), phase=ScanPhase.ENRICH, compute_hashes=False
+ )
+ seeder.enqueue_scan(
+ roots=("input",), phase=ScanPhase.ENRICH, compute_hashes=False
+ )
+ seeder.enqueue_scan(
+ roots=("output",), phase=ScanPhase.ENRICH, compute_hashes=True
+ )
- merged = set(seeder._pending_enrich["roots"])
+ merged = set(seeder._pending_scan["roots"])
assert merged == {"models", "input", "output"}
- assert seeder._pending_enrich["compute_hashes"] is True
-
-
-# ---------------------------------------------------------------------------
-# Pending enrich drains after scan completes
-# ---------------------------------------------------------------------------
-
+ assert seeder._pending_scan["compute_hashes"] is True
-class TestPendingEnrichDrain:
- """Verify that _run_scan drains _pending_enrich via start_enrich."""
+class TestPendingScanDrain:
@patch("app.assets.seeder.dependencies_available", return_value=True)
@patch("app.assets.seeder.get_owned_prefixes", return_value=[])
@patch("app.assets.seeder.sync_root_safely", return_value=set())
@patch("app.assets.seeder.collect_paths_for_roots", return_value=[])
@patch("app.assets.seeder.build_asset_specs", return_value=([], {}, 0))
- def test_pending_enrich_starts_after_scan(self, *_mocks):
- """After a fast scan finishes, the pending enrich should be started."""
+ def test_pending_scan_starts_after_scan(self, *_mocks):
seeder = _AssetSeeder()
- seeder._pending_enrich = {
+ seeder._pending_scan = {
"roots": ("output",),
+ "phase": ScanPhase.FULL,
"compute_hashes": True,
}
- with patch.object(seeder, "start_enrich", return_value=True) as mock_start:
+ real_start = seeder.start
+ with patch.object(seeder, "start", wraps=real_start) as mock_start:
seeder.start_fast(roots=("models",))
seeder.wait(timeout=5)
+ seeder.wait(timeout=5)
- mock_start.assert_called_once_with(
+ assert mock_start.call_args_list[-1] == call(
roots=("output",),
+ phase=ScanPhase.FULL,
+ prune_first=False,
compute_hashes=True,
+ _start_paused=False,
)
- assert seeder._pending_enrich is None
+ assert seeder._pending_scan is None
+
+ @patch("app.assets.seeder.dependencies_available", return_value=True)
+ def test_pause_is_preserved_when_pending_scan_drains(self, _dependencies):
+ seeder = _AssetSeeder()
+ phase_ready = threading.Event()
+ allow_phase_return = threading.Event()
+ drained_phase_calls: list[tuple[str, ...]] = []
+
+ def gated_enrich(roots):
+ if roots == ("models",):
+ phase_ready.set()
+ allow_phase_return.wait(timeout=5)
+ allow_phase_return.clear()
+ else:
+ drained_phase_calls.append(roots)
+ phase_ready.set()
+ allow_phase_return.wait(timeout=5)
+ return False, 0
+
+ seeder._pending_scan = {
+ "roots": ("output",),
+ "phase": ScanPhase.ENRICH,
+ "compute_hashes": False,
+ }
+ real_start = seeder.start
+
+ def start_and_signal(**kwargs):
+ started = real_start(**kwargs)
+ if started and kwargs["roots"] == ("output",):
+ if seeder._state is State.PAUSED:
+ phase_ready.set()
+ else:
+ phase_ready.wait(timeout=5)
+ return started
+
+ observed = {}
+ with (
+ patch.object(seeder, "_log_scan_config"),
+ patch.object(seeder, "_run_enrich_phase", side_effect=gated_enrich),
+ patch.object(seeder, "start", side_effect=start_and_signal),
+ ):
+ try:
+ assert seeder.start(
+ roots=("models",), phase=ScanPhase.ENRICH
+ ) is True
+ assert phase_ready.wait(timeout=5)
+ assert seeder.pause() is True
+ phase_ready.clear()
+ allow_phase_return.set()
+ assert phase_ready.wait(timeout=5)
+
+ with seeder._lock:
+ state_before_resume = seeder._state
+ gate_set_before_resume = seeder._run_gate.is_set()
+ calls_before_resume = len(drained_phase_calls)
+
+ phase_ready.clear()
+ resume_return = seeder.resume()
+ if calls_before_resume == 0:
+ assert phase_ready.wait(timeout=5)
+ allow_phase_return.set()
+ assert seeder.wait(timeout=5)
+
+ observed = {
+ "state_before_resume": state_before_resume,
+ "gate_set_before_resume": gate_set_before_resume,
+ "resume_return": resume_return,
+ "drained_phase_calls_before_resume": calls_before_resume,
+ "drained_phase_calls_after_resume": len(drained_phase_calls),
+ }
+ finally:
+ allow_phase_return.set()
+ seeder.resume()
+ seeder.wait(timeout=5)
+
+ assert observed == {
+ "state_before_resume": State.PAUSED,
+ "gate_set_before_resume": False,
+ "resume_return": True,
+ "drained_phase_calls_before_resume": 0,
+ "drained_phase_calls_after_resume": 1,
+ }, (
+ f"state_before_resume={observed['state_before_resume']!r}, "
+ f"gate_set_before_resume={observed['gate_set_before_resume']!r}, "
+ f"resume_return={observed['resume_return']!r}, "
+ "drained_phase_calls_before_resume="
+ f"{observed['drained_phase_calls_before_resume']!r}, "
+ "drained_phase_calls_after_resume="
+ f"{observed['drained_phase_calls_after_resume']!r}"
+ )
@patch("app.assets.seeder.dependencies_available", return_value=True)
@patch("app.assets.seeder.get_owned_prefixes", return_value=[])
@@ -192,18 +337,25 @@ class TestPendingEnrichDrain:
@patch("app.assets.seeder.collect_paths_for_roots", return_value=[])
@patch("app.assets.seeder.build_asset_specs", return_value=([], {}, 0))
def test_pending_cleared_even_when_start_fails(self, *_mocks):
- """_pending_enrich should be cleared even if start_enrich returns False."""
seeder = _AssetSeeder()
- seeder._pending_enrich = {
+ seeder._pending_scan = {
"roots": ("output",),
+ "phase": ScanPhase.ENRICH,
"compute_hashes": False,
}
- with patch.object(seeder, "start_enrich", return_value=False):
+ real_start = seeder.start
+
+ def start_initial_scan_only(**kwargs):
+ if kwargs["roots"] == ("models",):
+ return real_start(**kwargs)
+ return False
+
+ with patch.object(seeder, "start", side_effect=start_initial_scan_only):
seeder.start_fast(roots=("models",))
seeder.wait(timeout=5)
- assert seeder._pending_enrich is None
+ assert seeder._pending_scan is None
@patch("app.assets.seeder.dependencies_available", return_value=True)
@patch("app.assets.seeder.get_owned_prefixes", return_value=[])
@@ -211,31 +363,33 @@ class TestPendingEnrichDrain:
@patch("app.assets.seeder.collect_paths_for_roots", return_value=[])
@patch("app.assets.seeder.build_asset_specs", return_value=([], {}, 0))
def test_no_drain_when_no_pending(self, *_mocks):
- """start_enrich should not be called when there is no pending request."""
seeder = _AssetSeeder()
- assert seeder._pending_enrich is None
+ assert seeder._pending_scan is None
- with patch.object(seeder, "start_enrich", return_value=True) as mock_start:
+ real_start = seeder.start
+ with patch.object(seeder, "start", wraps=real_start) as mock_start:
seeder.start_fast(roots=("models",))
seeder.wait(timeout=5)
- mock_start.assert_not_called()
+ assert mock_start.call_count == 1
# ---------------------------------------------------------------------------
-# Thread-safety of enqueue_enrich
+# Thread-safety of enqueue_scan
# ---------------------------------------------------------------------------
-class TestEnqueueEnrichThreadSafety:
+class TestEnqueueScanThreadSafety:
def test_concurrent_enqueues(self, seeder):
"""Multiple threads enqueuing should not lose roots."""
- with patch.object(seeder, "start_enrich", return_value=False):
+ with patch.object(seeder, "start", return_value=False):
barrier = threading.Barrier(3)
def enqueue(root):
barrier.wait()
- seeder.enqueue_enrich(roots=(root,), compute_hashes=False)
+ seeder.enqueue_scan(
+ roots=(root,), phase=ScanPhase.ENRICH, compute_hashes=False
+ )
threads = [
threading.Thread(target=enqueue, args=(r,))
@@ -246,5 +400,5 @@ class TestEnqueueEnrichThreadSafety:
for t in threads:
t.join(timeout=5)
- merged = set(seeder._pending_enrich["roots"])
+ merged = set(seeder._pending_scan["roots"])
assert merged == {"models", "input", "output"}