Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(api): report failed image count in the index status event

Lincoln Steinc5f53f9ffbfc
diff --git a/invokeai/app/services/events/events_base.py b/invokeai/app/services/events/events_base.py
index 7bb65d6e1d..2ded5a7a94 100644
--- a/invokeai/app/services/events/events_base.py
+++ b/invokeai/app/services/events/events_base.py
@@ -321,9 +321,9 @@ class EventServiceBase:
# region Image index
- def emit_image_index_status(self, total: int, embedded: int, pending: int) -> None:
+ def emit_image_index_status(self, total: int, embedded: int, pending: int, failed: int = 0) -> None:
"""Emitted when the image embedding index makes progress"""
- self.dispatch(ImageIndexStatusEvent.build(total=total, embedded=embedded, pending=pending))
+ self.dispatch(ImageIndexStatusEvent.build(total=total, embedded=embedded, pending=pending, failed=failed))
def emit_image_index_updated(self, user_id: str) -> None:
"""Emitted to one user when their images were just (re)embedded"""
diff --git a/invokeai/app/services/events/events_common.py b/invokeai/app/services/events/events_common.py
index 50e80620ee..386157af8c 100644
--- a/invokeai/app/services/events/events_common.py
+++ b/invokeai/app/services/events/events_common.py
@@ -884,10 +884,17 @@ class ImageIndexStatusEvent(ImageIndexEventBase):
total: int = Field(description="Number of gallery images eligible for embedding")
embedded: int = Field(description="Number of eligible images that have an embedding")
pending: int = Field(description="Number of eligible images awaiting embedding")
+ failed: int = Field(
+ default=0,
+ description=(
+ "Eligible images that repeatedly failed to embed and were given up on. Excluded from "
+ "`pending`, so `total != embedded` with `pending == 0` means this many were skipped."
+ ),
+ )
@classmethod
- def build(cls, total: int, embedded: int, pending: int) -> "ImageIndexStatusEvent":
- return cls(total=total, embedded=embedded, pending=pending)
+ def build(cls, total: int, embedded: int, pending: int, failed: int = 0) -> "ImageIndexStatusEvent":
+ return cls(total=total, embedded=embedded, pending=pending, failed=failed)
@payload_schema.register
diff --git a/invokeai/app/services/image_index/image_index_default.py b/invokeai/app/services/image_index/image_index_default.py
index e36a79dc73..53a0f4c477 100644
--- a/invokeai/app/services/image_index/image_index_default.py
+++ b/invokeai/app/services/image_index/image_index_default.py
@@ -510,8 +510,12 @@ class ImageIndexService(ImageIndexServiceBase):
status = self.get_status()
if status is None:
return
+ # `failed` travels with the counts because it is the only thing that explains a
+ # settled index that is not complete: `pending` excludes failures, so without it a
+ # consumer sees pending == 0 with embedded < total and cannot tell "done" from
+ # "gave up on some".
self._invoker.services.events.emit_image_index_status(
- total=status.total, embedded=status.embedded, pending=status.pending
+ total=status.total, embedded=status.embedded, pending=status.pending, failed=status.failed
)
def _record_owner_pokes(self, image_names: list[str]) -> None:
diff --git a/invokeai/frontend/web/openapi.json b/invokeai/frontend/web/openapi.json
index 33aa8cb0b9..d4107d488a 100644
--- a/invokeai/frontend/web/openapi.json
+++ b/invokeai/frontend/web/openapi.json
@@ -40892,9 +40892,15 @@
"description": "Number of eligible images awaiting embedding",
"title": "Pending",
"type": "integer"
+ },
+ "failed": {
+ "default": 0,
+ "description": "Eligible images that repeatedly failed to embed and were given up on. Excluded from `pending`, so `total != embedded` with `pending == 0` means this many were skipped.",
+ "title": "Failed",
+ "type": "integer"
}
},
- "required": ["timestamp", "total", "embedded", "pending"],
+ "required": ["timestamp", "total", "embedded", "pending", "failed"],
"title": "ImageIndexStatusEvent",
"type": "object"
},
diff --git a/invokeai/frontend/web/src/services/api/schema.ts b/invokeai/frontend/web/src/services/api/schema.ts
index 893190d23c..7ab59961f2 100644
--- a/invokeai/frontend/web/src/services/api/schema.ts
+++ b/invokeai/frontend/web/src/services/api/schema.ts
@@ -17065,6 +17065,12 @@ export type components = {
* @description Number of eligible images awaiting embedding
*/
pending: number;
+ /**
+ * Failed
+ * @description Eligible images that repeatedly failed to embed and were given up on. Excluded from `pending`, so `total != embedded` with `pending == 0` means this many were skipped.
+ * @default 0
+ */
+ failed: number;
};
/**
* ImageIndexUpdatedEvent
diff --git a/tests/app/services/image_index/test_image_index_service.py b/tests/app/services/image_index/test_image_index_service.py
index 4c21310985..d16c1ce950 100644
--- a/tests/app/services/image_index/test_image_index_service.py
+++ b/tests/app/services/image_index/test_image_index_service.py
@@ -307,6 +307,40 @@ def test_broken_encoder_leaves_images_pending_rather_than_quarantined(
service.stop()
+def test_status_event_reports_failures_so_a_settled_index_is_not_mistaken_for_complete(
+ image_records: SqliteImageRecordStorage,
+ images_service: ImageService,
+ index_records: ImageIndexRecordsSqlite,
+) -> None:
+ """`pending` excludes failures, so on its own it cannot express "gave up on some".
+
+ Without `failed` in the event, a client sees pending == 0 with embedded < total and has no
+ way to tell a finished index from one that quietly skipped images — it would render
+ "complete" over a gallery with holes.
+ """
+
+ def get_pil_image(image_name: str) -> Image.Image:
+ if image_name == "bad.png":
+ raise FileNotFoundError(image_name)
+ return Image.new("RGB", (16, 16), "purple")
+
+ images_service.get_pil_image = get_pil_image # type: ignore[method-assign]
+
+ service = ImageIndexService(encode_fn=_fake_encode, model_id=MODEL_ID)
+ try:
+ _save_image(image_records, "good.png")
+ _save_image(image_records, "bad.png")
+ invoker = _make_invoker(images_service, index_records)
+ service.start(invoker)
+
+ _wait_until(lambda: any(e.failed == 1 and e.pending == 0 for e in _status_events(invoker)), timeout=20.0)
+ settled = [e for e in _status_events(invoker) if e.pending == 0][-1]
+ # The three numbers together are what make the state legible.
+ assert (settled.total, settled.embedded, settled.failed) == (2, 1, 1)
+ finally:
+ service.stop()
+
+
def test_index_recovers_from_an_encoder_outage_without_a_restart(
image_records: SqliteImageRecordStorage,
images_service: ImageService,