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.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
Application backend
Ownership and contracts
- Routers validate requests, enforce authentication/authorization, map service errors to HTTP. Services own domain policy. Storage owns persistence.
- Before changing flows, read
api/dependencies.py, affected routers, service base/default/storage implementations, tests. Keep dependency/start/stop lifecycles. No parallel global services. - Server-authorize every account-owned resource, including queries, writes, events, exports, recovery. UI guards are not enough.
- Before changing Pydantic models, invocation fields/versions, HTTP DTOs, or socket-event names/defaults/validation/errors: trace frontend and saved-workflow consumers.
- API changes may need regenerated
frontend/api/openapi.jsonandfrontend/api/schema.tsunderinvokeai/, webv2 included. Follow shared API package generation guidance. Never hand-edit output.
Persistence and lifecycle
- Preserve atomicity, revision checks, idempotency, explicit conflicts. Stored-data change: read migrations and old-record readers; add needed migration/compatibility tests.
- SQL lives only in
services/shared/database/, reached throughDatabase.queries. The database access guard enforces this: add queries there, never exempt callers. The guard proves nothing about transactions, authorization, or concurrency. - Query modules build statements once, at module level, with
bindparam()(per-call building costs several times execution). Read rows by position; build DTOs with@mapped(...)outside the transaction. Shape-dependent statement: cached builder whose shapes a client cannot multiply (normalize lists and flags, round list lengths; see the guide). - Multi-record writes: one transaction owner,
with db.queries.transaction() as q:, passqto helpers. Second transaction on the same thread raisesNestedTransactionError. Seeservices/shared/database/database.py. - Concurrency. SQLite serialises everything behind one lock; MySQL/MariaDB do not.
- Single-statement invariant ("only if still pending"): put it in the statement, a conditional
UPDATEwith checked row count. Never a read then a write. - Invariant spanning rows (last active administrator): a
DatabaseLock. Every transaction that changes rows the invariant reads acquires the lock as its first call (q.locks.acquire(...)). A migration adds its row. - Decision about one row (may this board be claimed?): lock that row before reading what the decision uses (
@lockingquery method,SELECT ... FOR UPDATE). Every transaction changing those inputs writes or locks the same row. Lock rows in one order (e.g. project before its board). - Unit with no effects outside the database: run with
db.queries.run(work); it retries after a deadlock.
- Single-statement invariant ("only if still pending"): put it in the statement, a conditional
- Tables live in
services/shared/database/schema/and must match the migrated SQLite schema exactly (schema parity check). Change schema with a migration plus the same metadata change, in one change. - No database has triggers. Queries set
updated_at(upserts too) and the queue'sstarted_at,completed_at,session_revision. - New migrations:
PortableMigrations (Alembic operations; tables viacontext.create_table()), idempotent. MySQL/MariaDB commit DDL as it runs, so a half-failed migration runs again. On SQLite they run with foreign keys off and are checked before commit. Migrations up toPORTABLE_CUTOVERstay SQLite-only and unchanged. See the Database Migrations guide. - Preserve project/account isolation, document bounds, queue admission/receipts, lost-response retry safety. Never silently discard recovery data. Never resolve conflicts with last-write-wins.
- Bound concurrency. Support cancellation, timeouts/retries, file/process/listener/task cleanup. Keep CPU and blocking I/O off the async server loop.
- Preserve file/path validation, remote-fetch protections, sanitized errors. Never modify real user models, databases, outputs, or credentials in tests or debugging.
- Every denoise invocation (
*denoise*ininvokeai/app/invocations/) must inheritWithMetadata, must not inheritWithBoard, must returnLatentsMetaOutput(or an output subclassingMetadataOutputfor multi-tensor outputs), and must build a newMetadataFieldviabuild_denoise_metadatawithout mutatingself.metadata.root. Never create new*_denoise_metawrapper nodes; enforced bytests/app/invocations/test_denoise_metadata.py. - Every metadata label, standalone model identifier, or generation parameter emitted into workflow metadata must be registered in
CORE_METADATA_REGISTRYininvokeai/app/invocations/metadata_constants.pyusing itsMetadataType(T.MODEL,T.STRING,T.FLOAT, etc.). Never declare or hardcode separate label sets or literals inmetadata_linked.pyormetadata.py; all dropdown literals andCORE_MODEL_LABELSare derived automatically from this registry and enforced bytests/app/invocations/test_core_metadata.py.
Efficiency and verification
- Check queries for repeated lookups, full scans/materialization, missing bounds, serialization. Measure representative data and query counts before adding caches or indexes.
- Transaction, migration, revision, ownership tests: real temporary SQLite. Cover affected malformed input, cross-user access, cancellation, repeated requests, partial failures. Database-fixture tests also run against MySQL and MariaDB when
INVOKEAI_TEST_DB_URLis set (tests/fixtures/database.py). - Read
tests/AGENTS.md. Usetests/app/or the existing owning suite. Run focused pytest and root Ruff. Milestones: broaden to relevant service/router/invocation suites. - Graph execution: read
services/shared/README.md. Test scheduling and invocation lifecycles through owning interfaces. - Report schema generation, migration validation, unavailable integration/hardware checks, and the database dialects exercised. SQLite alone does not prove MySQL/MariaDB locking or interrupted-DDL recovery.