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.

Merge upstream/main into v7 integration

joshistoast2dbe3f92fd52
diff --cc invokeai/app/api/routers/session_queue.py
index 5e6dffce33,9850364af2..34132e34d2
--- a/invokeai/app/api/routers/session_queue.py
+++ b/invokeai/app/api/routers/session_queue.py
@@@ -544,17 -467,14 +544,21 @@@ async def get_next_queue_item
async def get_queue_status(
current_user: CurrentUserOrDefault,
queue_id: str = Path(description="The queue id to perform this operation on"),
+ origin_prefix: Optional[str] = Query(
+ default=None, description="Only include queue items whose origin starts with this prefix"
+ ),
) -> SessionQueueAndProcessorStatus:
- """Gets the status of the session queue. Returns global counts; non-admin users additionally
- get their own pending/in_progress counts (so the UI can show an X/Y badge) and cannot see the
- current item's identifiers unless they own it."""
+ """Gets the status of the session queue. Returns global counts; every user additionally gets
+ their own pending/in_progress counts (so the UI can show an X/Y badge and scope personal UI
+ like the progress bar to the user's own activity). Non-admin users cannot see the current
+ item's identifiers unless they own it."""
try:
- user_id = None if current_user.is_admin else current_user.user_id
queue = ApiDependencies.invoker.services.session_queue.get_queue_status(
- queue_id, user_id=user_id, origin_prefix=origin_prefix
- queue_id, user_id=current_user.user_id, is_admin=current_user.is_admin
++ queue_id,
++ user_id=current_user.user_id,
++ acting_user_id=current_user.user_id,
++ origin_prefix=origin_prefix,
++ is_admin=current_user.is_admin,
)
processor = ApiDependencies.invoker.services.session_processor.get_status()
return SessionQueueAndProcessorStatus(queue=queue, processor=processor)
diff --cc invokeai/app/api/sockets.py
index 19392a107b,94ccd3ba21..4f3e625f7b
--- a/invokeai/app/api/sockets.py
+++ b/invokeai/app/api/sockets.py
@@@ -503,10 -588,20 +600,25 @@@ class SocketIO
async def _handle_model_event(self, event: FastAPIEvent[ModelEventBase | DownloadEventBase]) -> None:
event_name, event_data = event
+
+ # Model load events only drive personal UI (the loading-models spinner that puts the
+ # progress bar into indeterminate mode), so they are routed to the user whose action
+ # triggered the load. Broadcasting them made every user's progress bar animate whenever
+ # any user's generation loaded a model. In single-user mode the owner is "system" and
+ # every socket is in user:system, preserving the old behavior.
+ if isinstance(event_data, (ModelLoadStartedEvent, ModelLoadCompleteEvent)):
+ await self._sio.emit(
+ event=event_name, data=event_data.model_dump(mode="json"), room=f"user:{event_data.user_id}"
+ )
+ return
+
- # Model install / download events remain broadcast to all connected sockets - they feed
- # the model manager UI, which is not per-user.
++ # Model install events feed an admin-only UI and may expose details about privileged
++ # operations, so keep the fork's admin-only routing. Other download events retain their
++ # existing broadcast behavior.
+ if isinstance(event_data, MODEL_INSTALL_EVENTS):
+ await self._sio.emit(event=event_name, data=event_data.model_dump(mode="json"), room="admin")
+ return
+
await self._sio.emit(event=event_name, data=event_data.model_dump(mode="json"))
async def _handle_bulk_image_download_event(self, event: FastAPIEvent[BulkDownloadEventBase]) -> None:
diff --cc invokeai/app/services/session_queue/session_queue_base.py
index a3882524fb,2737816d71..166704cee6
--- a/invokeai/app/services/session_queue/session_queue_base.py
+++ b/invokeai/app/services/session_queue/session_queue_base.py
@@@ -79,7 -79,7 +79,8 @@@ class SessionQueueBase(ABC)
queue_id: str,
user_id: Optional[str] = None,
acting_user_id: Optional[str] = None,
+ origin_prefix: Optional[str] = None,
+ is_admin: bool = False,
) -> SessionQueueStatus:
"""Gets the status of the queue.
diff --cc invokeai/app/services/session_queue/session_queue_common.py
index 8d068b3fa6,8d068b3fa6..e9107256af
--- a/invokeai/app/services/session_queue/session_queue_common.py
+++ b/invokeai/app/services/session_queue/session_queue_common.py
@@@ -328,11 -328,11 +328,11 @@@ class SessionQueueStatus(BaseModel)
total: int = Field(..., description="Total number of queue items")
user_pending: Optional[int] = Field(
default=None,
-- description="Number of the requesting user's queue items with status 'pending' (None for admins/global callers)",
++ description="Number of the requesting user's queue items with status 'pending' (None for global callers)",
)
user_in_progress: Optional[int] = Field(
default=None,
-- description="Number of the requesting user's queue items with status 'in_progress' (None for admins/global callers)",
++ description="Number of the requesting user's queue items with status 'in_progress' (None for global callers)",
)
diff --cc invokeai/app/services/session_queue/session_queue_sqlite.py
index 9f4f82817f,e845d663ef..0f3a2ecc73
--- a/invokeai/app/services/session_queue/session_queue_sqlite.py
+++ b/invokeai/app/services/session_queue/session_queue_sqlite.py
@@@ -1160,11 -1160,14 +1177,12 @@@ class SqliteSessionQueue(SessionQueueBa
queue_id: str,
user_id: Optional[str] = None,
acting_user_id: Optional[str] = None,
+ origin_prefix: Optional[str] = None,
+ is_admin: bool = False,
) -> SessionQueueStatus:
with self._db.transaction() as cursor:
- # Aggregate counts are always global (across all users). This lets a non-admin's
- # badge show "own / total" — their share of the whole queue — and lets the queue
- # list surface (redacted) entries belonging to other users.
- cursor.execute(
- """--sql
+ # Aggregate counts are global across all users within the requested scope.
+ query = """--sql
SELECT status, count(*)
FROM session_queue
WHERE queue_id = ?
diff --cc invokeai/frontend/web/openapi.json
index 4191149397,c877494d23..488724a4d3
--- a/invokeai/frontend/web/openapi.json
+++ b/invokeai/frontend/web/openapi.json
@@@ -67053,7 -66643,7 +67135,7 @@@
}
],
"title": "User Pending",
-- "description": "Number of the requesting user's queue items with status 'pending' (None for admins/global callers)"
++ "description": "Number of the requesting user's queue items with status 'pending' (None for global callers)"
},
"user_in_progress": {
"anyOf": [
@@@ -67065,7 -66655,7 +67147,7 @@@
}
],
"title": "User In Progress",
-- "description": "Number of the requesting user's queue items with status 'in_progress' (None for admins/global callers)"
++ "description": "Number of the requesting user's queue items with status 'in_progress' (None for global callers)"
}
},
"type": "object",
diff --cc invokeai/frontend/web/src/services/api/schema.ts
index 6ec09bef53,29d9e6f2d7..81d0eaa291
--- a/invokeai/frontend/web/src/services/api/schema.ts
+++ b/invokeai/frontend/web/src/services/api/schema.ts
@@@ -29006,12 -28867,12 +29069,12 @@@ export type components =
total: number;
/**
* User Pending
-- * @description Number of the requesting user's queue items with status 'pending' (None for admins/global callers)
++ * @description Number of the requesting user's queue items with status 'pending' (None for global callers)
*/
user_pending?: number | null;
/**
* User In Progress
-- * @description Number of the requesting user's queue items with status 'in_progress' (None for admins/global callers)
++ * @description Number of the requesting user's queue items with status 'in_progress' (None for global callers)
*/
user_in_progress?: number | null;
};
diff --cc tests/app/services/session_queue/test_session_queue_status_user_scoping.py
index d58933879f,c9e2147b0e..65ced9b875
--- a/tests/app/services/session_queue/test_session_queue_status_user_scoping.py
+++ b/tests/app/services/session_queue/test_session_queue_status_user_scoping.py
@@@ -33,7 -33,7 +33,7 @@@ def session_queue(mock_invoker: Invoker
return queue
--def _insert_queue_item(session_queue: SqliteSessionQueue, user_id: str) -> int:
++def _insert_queue_item(session_queue: SqliteSessionQueue, user_id: str, origin: str | None = None) -> int:
graph = Graph()
graph.add_node(PromptTestInvocation(id="prompt", prompt="test"))
session = GraphExecutionState(graph=graph)
@@@ -48,7 -48,7 +48,7 @@@
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
-- ("default", session_json, session.id, batch_id, None, 0, None, None, None, None, user_id),
++ ("default", session_json, session.id, batch_id, None, 0, None, origin, None, None, user_id),
)
return cursor.lastrowid # type: ignore[return-value]
@@@ -107,6 -107,32 +107,61 @@@ def test_status_current_item_redacted_f
assert status.user_pending == 1 # A's single pending item
+ def test_status_admin_caller_gets_user_subcounts_and_current_item_identifiers(
+ session_queue: SqliteSessionQueue,
+ ) -> None:
+ """An admin caller (user_id set, is_admin=True) gets their own subcounts - so personal UI
+ like the progress bar can distinguish the admin's own activity from other users' - while
+ still seeing the identifiers of another user's current item."""
+ admin_id = "admin-1"
+ user_b = "user-b"
+ b_item_id = _insert_queue_item(session_queue, user_id=user_b)
+ _insert_queue_item(session_queue, user_id=admin_id)
+
+ in_progress = session_queue.dequeue()
+ assert in_progress is not None and in_progress.item_id == b_item_id
+
+ status = session_queue.get_queue_status(queue_id="default", user_id=admin_id, is_admin=True)
+
+ # Admins are not subject to current-item redaction.
+ assert status.item_id == b_item_id
+ assert status.session_id is not None
+ assert status.batch_id is not None
+ # Global counts plus the admin's own subcounts.
+ assert status.in_progress == 1 # B's item, counted globally
+ assert status.user_in_progress == 0 # the admin owns none in progress
+ assert status.user_pending == 1 # the admin's single pending item
+
+
++def test_status_origin_scope_preserves_admin_subcounts_and_current_item_visibility(
++ session_queue: SqliteSessionQueue,
++) -> None:
++ """Origin scoping applies consistently to aggregate counts, personal counts, and the
++ current-item snapshot without hiding another owner's identifiers from an admin."""
++ admin_id = "admin-1"
++ scoped_item_id = _insert_queue_item(session_queue, user_id="user-b", origin="project-a:canvas")
++ _insert_queue_item(session_queue, user_id=admin_id, origin="project-a:workflows")
++ _insert_queue_item(session_queue, user_id="user-c", origin="project-b:canvas")
++
++ in_progress = session_queue.dequeue()
++ assert in_progress is not None and in_progress.item_id == scoped_item_id
++
++ status = session_queue.get_queue_status(
++ queue_id="default",
++ user_id=admin_id,
++ acting_user_id=admin_id,
++ origin_prefix="project-a:",
++ is_admin=True,
++ )
++
++ assert status.item_id == scoped_item_id
++ assert status.pending == 1
++ assert status.in_progress == 1
++ assert status.total == 2
++ assert status.user_pending == 1
++ assert status.user_in_progress == 0
++
++
def test_get_queue_item_ids_returns_all_users_ids(session_queue: SqliteSessionQueue) -> None:
"""get_queue_item_ids returns ids for every user so the virtualized list can show the
(redacted) entries belonging to other users. Redaction happens at hydration time."""
diff --cc tests/app/test_invocation_event_socketio.py
index 0000000000,33c71f0cda..63c6b2ccba
mode 000000,100644..100644
--- a/tests/app/test_invocation_event_socketio.py
+++ b/tests/app/test_invocation_event_socketio.py
@@@ -1,0 -1,169 +1,183 @@@
+ """Tests for socket routing of invocation events in multiuser mode.
+
+ Invocation progress events drive personal UI (the global progress bar and progress image
+ previews) and must be delivered only to the owner - admins receiving other users' progress
+ would see their own progress display hijacked. The other invocation events (started,
+ complete, error) also feed admins' gallery cache updates, so they go to the owner and the
+ admin room - but in a single emit so that an admin who owns the queue item (which includes
+ the "system" user in single-user mode) receives exactly one copy.
+ """
+
+ from unittest.mock import AsyncMock
+
+ import pytest
+ from fastapi import FastAPI
+
+ from invokeai.app.api.sockets import SocketIO
+ from invokeai.app.services.events.events_common import (
+ InvocationCompleteEvent,
+ InvocationErrorEvent,
+ InvocationProgressEvent,
+ InvocationStartedEvent,
+ )
+
+
+ @pytest.fixture
+ def anyio_backend() -> str:
+ return "asyncio"
+
+
+ _COMMON_FIELDS = {
+ "queue_id": "default",
+ "item_id": 1,
+ "batch_id": "batch-1",
+ "user_id": "owner-1",
+ "session_id": "session-1",
+ "invocation": {"type": "add", "id": "node-1", "a": 1, "b": 2},
+ "invocation_source_id": "node-1",
+ }
+
+
+ @pytest.mark.anyio
+ async def test_progress_event_is_emitted_only_to_owner() -> None:
+ socketio = SocketIO(FastAPI())
+ socketio._sio.emit = AsyncMock()
+
+ event = InvocationProgressEvent(**_COMMON_FIELDS, message="denoising", percentage=0.5)
+
+ await socketio._handle_queue_event(("invocation_progress", event))
+
+ socketio._sio.emit.assert_awaited_once_with(
+ event="invocation_progress",
+ data=event.model_dump(mode="json"),
+ room="user:owner-1",
+ )
+
+
+ @pytest.mark.anyio
+ async def test_complete_event_is_emitted_once_to_owner_and_admin_rooms() -> None:
+ socketio = SocketIO(FastAPI())
+ socketio._sio.emit = AsyncMock()
+
+ event = InvocationCompleteEvent(**_COMMON_FIELDS, result={"type": "integer_output", "value": 3})
+
+ await socketio._handle_queue_event(("invocation_complete", event))
+
+ # A single emit to the union of rooms - python-socketio dedupes recipients across a room
+ # list, so an admin owner (or the single-user "system" user) receives exactly one copy.
+ socketio._sio.emit.assert_awaited_once_with(
+ event="invocation_complete",
+ data=event.model_dump(mode="json"),
+ room=["user:owner-1", "admin"],
+ )
+
+
+ @pytest.mark.anyio
+ async def test_started_event_is_emitted_once_to_owner_and_admin_rooms() -> None:
+ socketio = SocketIO(FastAPI())
+ socketio._sio.emit = AsyncMock()
+
+ event = InvocationStartedEvent(**_COMMON_FIELDS)
+
+ await socketio._handle_queue_event(("invocation_started", event))
+
+ socketio._sio.emit.assert_awaited_once_with(
+ event="invocation_started",
+ data=event.model_dump(mode="json"),
+ room=["user:owner-1", "admin"],
+ )
+
+
+ @pytest.mark.anyio
+ async def test_error_event_is_emitted_once_to_owner_and_admin_rooms() -> None:
+ socketio = SocketIO(FastAPI())
+ socketio._sio.emit = AsyncMock()
+
+ event = InvocationErrorEvent(
+ **_COMMON_FIELDS,
+ error_type="ValueError",
+ error_message="oops",
+ error_traceback="Traceback (most recent call last): ...",
+ )
+
+ await socketio._handle_queue_event(("invocation_error", event))
+
+ socketio._sio.emit.assert_awaited_once_with(
+ event="invocation_error",
+ data=event.model_dump(mode="json"),
+ room=["user:owner-1", "admin"],
+ )
+
+
+ def _make_model_config():
+ from invokeai.backend.model_manager.configs.factory import AnyModelConfigValidator
+
+ return AnyModelConfigValidator.validate_python(
+ {
+ "key": "model-key-1",
+ "hash": "hash-1",
+ "path": "/models/some-model",
+ "name": "some-model",
+ "base": "sd-1",
+ "type": "vae",
+ "format": "diffusers",
+ "source": "/models/some-model",
+ "source_type": "path",
+ "file_size": 1024,
+ }
+ )
+
+
+ @pytest.mark.anyio
+ async def test_model_load_events_are_emitted_only_to_triggering_user() -> None:
+ from invokeai.app.services.events.events_common import ModelLoadCompleteEvent, ModelLoadStartedEvent
+
+ socketio = SocketIO(FastAPI())
+ socketio._sio.emit = AsyncMock()
+
+ config = _make_model_config()
+ started = ModelLoadStartedEvent.build(config, user_id="owner-1")
+ complete = ModelLoadCompleteEvent.build(config, user_id="owner-1")
+
+ await socketio._handle_model_event(("model_load_started", started))
+ await socketio._handle_model_event(("model_load_complete", complete))
+
+ socketio._sio.emit.assert_any_await(
+ event="model_load_started", data=started.model_dump(mode="json"), room="user:owner-1"
+ )
+ socketio._sio.emit.assert_any_await(
+ event="model_load_complete", data=complete.model_dump(mode="json"), room="user:owner-1"
+ )
+ assert socketio._sio.emit.await_count == 2
+
+
+ @pytest.mark.anyio
-async def test_model_install_events_remain_broadcast() -> None:
++async def test_model_install_events_are_emitted_only_to_admins() -> None:
+ from invokeai.app.services.events.events_common import ModelInstallStartedEvent
+
+ socketio = SocketIO(FastAPI())
+ socketio._sio.emit = AsyncMock()
+
++ event = ModelInstallStartedEvent(
++ id=1, source={"type": "url", "url": "https://example.com/model.safetensors"}
++ )
++
++ await socketio._handle_model_event(("model_install_started", event))
++
++ socketio._sio.emit.assert_awaited_once_with(
++ event="model_install_started", data=event.model_dump(mode="json"), room="admin"
++ )
++
++
++async def test_generic_download_events_remain_broadcast() -> None:
++ socketio = SocketIO(FastAPI())
++ socketio._sio.emit = AsyncMock()
++
+ from types import SimpleNamespace
+
+ event = SimpleNamespace(model_dump=lambda mode="json": {"id": 1})
- # Not a load event, so it takes the broadcast path (no room argument).
- assert not isinstance(event, ModelInstallStartedEvent)
+
- await socketio._handle_model_event(("model_install_started", event))
++ await socketio._handle_model_event(("download_started", event))
+
- socketio._sio.emit.assert_awaited_once_with(event="model_install_started", data={"id": 1})
++ socketio._sio.emit.assert_awaited_once_with(event="download_started", data={"id": 1})