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
feat(workbench): add project-scoped diagnostics logger
joshistoastc0a7419aacce
diff --git a/invokeai/frontend/webv2/src/workbench/diagnostics/logger.test.ts b/invokeai/frontend/webv2/src/workbench/diagnostics/logger.test.tsnew file mode 100644index 0000000000..a2adad10d7--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/diagnostics/logger.test.ts@@ -0,0 +1,129 @@+import { beforeEach, describe, expect, it, vi } from 'vitest';++import {+ clearProjectDiagnostics,+ configureDiagnostics,+ createProjectLogger,+ getProjectDiagnostics,+ recordDiagnosticTiming,+} from './logger';++beforeEach(() => {+ clearProjectDiagnostics('project-a');+ clearProjectDiagnostics('project-b');+ configureDiagnostics({+ enabled: true,+ level: 'trace',+ namespaces: ['workflows', 'system', 'queue'],+ performanceTimingsEnabled: true,+ });+ vi.setSystemTime(new Date('2026-06-29T00:00:00.000Z'));+});++describe('diagnostics logger', () => {+ it('records entries scoped to the source project', () => {+ const logger = createProjectLogger('workflows', {+ instanceId: 'workflow:center',+ kind: 'widget',+ projectId: 'project-a',+ region: 'center',+ typeId: 'workflow',+ });++ logger.info({ nodes: 120 }, 'Workflow loaded');++ expect(getProjectDiagnostics('project-a')).toMatchObject([+ {+ context: { nodes: 120 },+ createdAt: '2026-06-29T00:00:00.000Z',+ level: 'info',+ message: 'Workflow loaded',+ namespace: 'workflows',+ source: {+ instanceId: 'workflow:center',+ kind: 'widget',+ projectId: 'project-a',+ region: 'center',+ typeId: 'workflow',+ },+ },+ ]);+ expect(getProjectDiagnostics('project-b')).toEqual([]);+ });++ it('records timing entries as normal diagnostics', () => {+ recordDiagnosticTiming({ area: 'persistence', kind: 'workbench', projectId: 'project-a' }, 'workbench:save', 12.34);++ expect(getProjectDiagnostics('project-a')).toMatchObject([+ {+ durationMs: 12.34,+ level: 'debug',+ message: 'workbench:save completed in 12.3ms',+ namespace: 'performance',+ source: { area: 'persistence', kind: 'workbench', projectId: 'project-a' },+ },+ ]);+ });++ it('clears a single project without clearing other projects', () => {+ createProjectLogger('system', { area: 'runtime', kind: 'workbench', projectId: 'project-a' }).error('A failed');+ createProjectLogger('system', { area: 'runtime', kind: 'workbench', projectId: 'project-b' }).warn('B warned');++ clearProjectDiagnostics('project-a');++ expect(getProjectDiagnostics('project-a')).toEqual([]);+ expect(getProjectDiagnostics('project-b')).toHaveLength(1);+ });++ it('filters entries by project logging settings', () => {+ configureDiagnostics({+ enabled: true,+ level: 'warn',+ namespaces: ['system'],+ performanceTimingsEnabled: true,+ });++ createProjectLogger('workflows', { area: 'runtime', kind: 'workbench', projectId: 'project-a' }).error(+ 'Wrong namespace'+ );+ createProjectLogger('system', { area: 'runtime', kind: 'workbench', projectId: 'project-a' }).debug('Too quiet');+ createProjectLogger('system', { area: 'runtime', kind: 'workbench', projectId: 'project-a' }).warn('Recorded');++ expect(getProjectDiagnostics('project-a')).toMatchObject([{ level: 'warn', message: 'Recorded' }]);+ });++ it('can disable project diagnostics and performance timings independently', () => {+ configureDiagnostics({+ enabled: false,+ level: 'trace',+ namespaces: ['system'],+ performanceTimingsEnabled: true,+ });++ createProjectLogger('system', { area: 'runtime', kind: 'workbench', projectId: 'project-a' }).error('Skipped');++ configureDiagnostics({+ enabled: true,+ level: 'trace',+ namespaces: ['system'],+ performanceTimingsEnabled: false,+ });++ recordDiagnosticTiming({ area: 'runtime', kind: 'workbench', projectId: 'project-a' }, 'workbench:test', 1.2);++ expect(getProjectDiagnostics('project-a')).toEqual([]);+ });++ it('applies the current diagnostics config to every project', () => {+ configureDiagnostics({+ enabled: true,+ level: 'warn',+ namespaces: ['queue'],+ performanceTimingsEnabled: true,+ });++ createProjectLogger('queue', { area: 'autosave', kind: 'workbench', projectId: 'project-b' }).error('Recorded');++ expect(getProjectDiagnostics('project-b')).toMatchObject([{ level: 'error', message: 'Recorded' }]);+ });+});diff --git a/invokeai/frontend/webv2/src/workbench/diagnostics/logger.ts b/invokeai/frontend/webv2/src/workbench/diagnostics/logger.tsnew file mode 100644index 0000000000..e920d613c2--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/diagnostics/logger.ts@@ -0,0 +1,193 @@+import type { DeveloperLogLevel, DeveloperLogNamespace, WidgetContributionSource } from '@workbench/types';++import { useSyncExternalStore } from 'react';++const MAX_PROJECT_DIAGNOSTIC_ENTRIES = 500;+const EMPTY_DIAGNOSTIC_ENTRIES: DiagnosticEntry[] = [];++const LOG_LEVEL_ORDER: Record<DeveloperLogLevel, number> = {+ debug: 20,+ error: 50,+ fatal: 60,+ info: 30,+ trace: 10,+ warn: 40,+};++export type DiagnosticSource =+ | ({ kind: 'widget' } & WidgetContributionSource)+ | { area: string; kind: 'workbench'; projectId?: string };++export interface DiagnosticEntry {+ context?: Record<string, unknown>;+ createdAt: string;+ durationMs?: number;+ id: string;+ level: DeveloperLogLevel;+ message: string;+ namespace: DeveloperLogNamespace | 'performance';+ source: DiagnosticSource;+}++export interface DiagnosticsConfig {+ enabled: boolean;+ level: DeveloperLogLevel;+ namespaces: DeveloperLogNamespace[];+ performanceTimingsEnabled: boolean;+}++type DiagnosticListener = () => void;++type LogArgument = string | Record<string, unknown>;++const entriesByProjectId = new Map<string, DiagnosticEntry[]>();+const listenersByProjectId = new Map<string, Set<DiagnosticListener>>();++let diagnosticsConfig: DiagnosticsConfig = {+ enabled: true,+ level: 'warn',+ namespaces: ['system', 'queue', 'workflows'],+ performanceTimingsEnabled: false,+};++let nextDiagnosticEntryId = 0;++const getProjectIdFromSource = (source: DiagnosticSource): string | null => source.projectId ?? null;++const shouldRecordEntry = (entry: Omit<DiagnosticEntry, 'createdAt' | 'id'>): boolean => {+ if (entry.namespace === 'performance') {+ return diagnosticsConfig.performanceTimingsEnabled;+ }++ return (+ diagnosticsConfig.enabled &&+ LOG_LEVEL_ORDER[entry.level] >= LOG_LEVEL_ORDER[diagnosticsConfig.level] &&+ diagnosticsConfig.namespaces.includes(entry.namespace)+ );+};++const getProjectEntries = (projectId: string): DiagnosticEntry[] => {+ const entries = entriesByProjectId.get(projectId) ?? [];++ if (!entriesByProjectId.has(projectId)) {+ entriesByProjectId.set(projectId, entries);+ }++ return entries;+};++const getProjectEntriesSnapshot = (projectId: string): DiagnosticEntry[] =>+ entriesByProjectId.get(projectId) ?? EMPTY_DIAGNOSTIC_ENTRIES;++const notifyProjectDiagnostics = (projectId: string): void => {+ for (const listener of listenersByProjectId.get(projectId) ?? []) {+ listener();+ }+};++const normalizeLogArgs = (+ first: LogArgument,+ second?: string+): { context?: Record<string, unknown>; message: string } => {+ if (typeof first === 'string') {+ return { message: first };+ }++ return { context: first, message: second ?? '' };+};++export const recordDiagnosticEntry = (entry: Omit<DiagnosticEntry, 'createdAt' | 'id'>): DiagnosticEntry | null => {+ const projectId = getProjectIdFromSource(entry.source);++ if (!projectId) {+ return null;+ }++ if (!shouldRecordEntry(entry)) {+ return null;+ }++ const nextEntry: DiagnosticEntry = {+ ...entry,+ createdAt: new Date().toISOString(),+ id: `diagnostic-${nextDiagnosticEntryId++}`,+ };+ const entries = [nextEntry, ...getProjectEntries(projectId)].slice(0, MAX_PROJECT_DIAGNOSTIC_ENTRIES);++ entriesByProjectId.set(projectId, entries);++ notifyProjectDiagnostics(projectId);++ return nextEntry;+};++export const createProjectLogger = (namespace: DeveloperLogNamespace, source: DiagnosticSource) => {+ const log = (level: DeveloperLogLevel, first: LogArgument, second?: string): void => {+ const { context, message } = normalizeLogArgs(first, second);++ recordDiagnosticEntry({ context, level, message, namespace, source });+ };++ return {+ debug: (first: LogArgument, second?: string) => log('debug', first, second),+ error: (first: LogArgument, second?: string) => log('error', first, second),+ fatal: (first: LogArgument, second?: string) => log('fatal', first, second),+ info: (first: LogArgument, second?: string) => log('info', first, second),+ trace: (first: LogArgument, second?: string) => log('trace', first, second),+ warn: (first: LogArgument, second?: string) => log('warn', first, second),+ };+};++export const configureDiagnostics = (config: DiagnosticsConfig): void => {+ diagnosticsConfig = { ...config, namespaces: [...config.namespaces] };+};++export const canRecordDiagnosticTiming = (source?: DiagnosticSource): source is DiagnosticSource => {+ if (!source?.projectId) {+ return false;+ }++ return diagnosticsConfig.performanceTimingsEnabled;+};++export const recordDiagnosticTiming = (+ source: DiagnosticSource,+ name: string,+ durationMs: number+): DiagnosticEntry | null =>+ recordDiagnosticEntry({+ durationMs,+ level: 'debug',+ message: `${name} completed in ${durationMs.toFixed(1)}ms`,+ namespace: 'performance',+ source,+ });++export const getProjectDiagnostics = (projectId: string): DiagnosticEntry[] => [...getProjectEntries(projectId)];++export const useProjectDiagnostics = (projectId: string): DiagnosticEntry[] =>+ useSyncExternalStore(+ (listener) => subscribeProjectDiagnostics(projectId, listener),+ () => getProjectEntriesSnapshot(projectId),+ () => EMPTY_DIAGNOSTIC_ENTRIES+ );++export const clearProjectDiagnostics = (projectId: string): void => {+ entriesByProjectId.delete(projectId);+ notifyProjectDiagnostics(projectId);+};++export const subscribeProjectDiagnostics = (projectId: string, listener: DiagnosticListener): (() => void) => {+ const listeners = listenersByProjectId.get(projectId) ?? new Set<DiagnosticListener>();++ listeners.add(listener);+ listenersByProjectId.set(projectId, listeners);++ return () => {+ listeners.delete(listener);++ if (listeners.size === 0) {+ listenersByProjectId.delete(projectId);+ }+ };+};diff --git a/invokeai/frontend/webv2/src/workbench/types.ts b/invokeai/frontend/webv2/src/workbench/types.tsindex cf9a78afbd..5d981bc43b 100644--- a/invokeai/frontend/webv2/src/workbench/types.ts+++ b/invokeai/frontend/webv2/src/workbench/types.ts@@ -165,6 +165,7 @@ export interface WidgetRuntimeApi<State extends Record<string, unknown> = RecordtypeId: WidgetTypeId;region: WorkbenchRegion;state: WidgetRuntimeStateApi<State>;+ diagnostics: WidgetDiagnosticsApi;commands: WidgetCommandApi;hotkeys: WidgetHotkeyApi;menus: WidgetMenuApi;@@ -174,6 +175,17 @@ export interface WidgetRuntimeApi<State extends Record<string, unknown> = Recordworkbench: WidgetWorkbenchApi;}+export interface WidgetDiagnosticsApi {+ logger: (namespace: DeveloperLogNamespace) => {+ debug: (messageOrContext: string | Record<string, unknown>, message?: string) => void;+ error: (messageOrContext: string | Record<string, unknown>, message?: string) => void;+ fatal: (messageOrContext: string | Record<string, unknown>, message?: string) => void;+ info: (messageOrContext: string | Record<string, unknown>, message?: string) => void;+ trace: (messageOrContext: string | Record<string, unknown>, message?: string) => void;+ warn: (messageOrContext: string | Record<string, unknown>, message?: string) => void;+ };+}+export interface WidgetRuntimeStateApi<State extends Record<string, unknown> = Record<string, unknown>> {getSnapshot: () => Readonly<State>;useSelector: <Selected>(@@ -514,7 +526,6 @@ export interface WorkbenchState {projects: Project[];activeProjectId: string;backendConnection: BackendConnectionState;- errorLog: string[];notifications: WorkbenchNotification[];autosave: AutosaveState;account: AccountState;@@ -705,6 +716,7 @@ export interface WorkbenchPreferences {developerLogEnabled: boolean;developerLogLevel: DeveloperLogLevel;developerLogNamespaces: DeveloperLogNamespace[];+ developerPerformanceTimingsEnabled: boolean;/** Always snap workflow nodes to the grid (Ctrl snaps temporarily when off). */workflowSnapToGrid: boolean;/** Show the minimap in the workflow editor. */diff --git a/invokeai/frontend/webv2/src/workbench/widget-frame/createWidgetRuntime.test.ts b/invokeai/frontend/webv2/src/workbench/widget-frame/createWidgetRuntime.test.tsindex a2f8090855..e876abe13a 100644--- a/invokeai/frontend/webv2/src/workbench/widget-frame/createWidgetRuntime.test.ts+++ b/invokeai/frontend/webv2/src/workbench/widget-frame/createWidgetRuntime.test.ts@@ -8,7 +8,8 @@ import type {} from '@workbench/types';import type { WorkbenchAction } from '@workbench/workbenchState';-import { describe, expect, it, vi } from 'vitest';+import { clearProjectDiagnostics, configureDiagnostics, getProjectDiagnostics } from '@workbench/diagnostics/logger';+import { beforeEach, describe, expect, it, vi } from 'vitest';import { cloneWidgetRuntimeState, createWidgetRuntime, getProjectWidgetRuntimeState } from './createWidgetRuntime';@@ -82,6 +83,16 @@ const createPlacementProject = (});describe('createWidgetRuntime', () => {+ beforeEach(() => {+ clearProjectDiagnostics('project-1');+ configureDiagnostics({+ enabled: true,+ level: 'trace',+ namespaces: ['workflows'],+ performanceTimingsEnabled: false,+ });+ });+it('deep clones runtime state snapshots', () => {const values = { nested: { mutable: 'before' }, tags: ['before'] };const snapshot = cloneWidgetRuntimeState(values);@@ -118,6 +129,36 @@ describe('createWidgetRuntime', () => {expect(state.set).toHaveBeenCalledWith({ replaced: true });});+ it('binds diagnostics to the current widget source', () => {+ const { dispatch } = createDispatch();+ const runtime = createWidgetRuntime({+ dispatch,+ getWidgetById: () => undefined,+ getWidgetsForRegion: createRegistry({}),+ instance: createInstance('workflow:center', 'workflow'),+ project: createPlacementProject(),+ region: 'center',+ state: createStateApi(),+ });++ runtime.diagnostics.logger('workflows').info('Widget rendered');++ expect(getProjectDiagnostics('project-1')).toMatchObject([+ {+ level: 'info',+ message: 'Widget rendered',+ namespace: 'workflows',+ source: {+ instanceId: 'workflow:center',+ kind: 'widget',+ projectId: 'project-1',+ region: 'center',+ typeId: 'workflow',+ },+ },+ ]);+ });+it('opens widgets through canonical validation and seeded initial values', () => {const { actions, dispatch } = createDispatch();const createInitial = vi.fn(() => ({ seeded: true }));diff --git a/invokeai/frontend/webv2/src/workbench/widget-frame/createWidgetRuntime.ts b/invokeai/frontend/webv2/src/workbench/widget-frame/createWidgetRuntime.tsindex 98fffdf54f..9eb3e095af 100644--- a/invokeai/frontend/webv2/src/workbench/widget-frame/createWidgetRuntime.ts+++ b/invokeai/frontend/webv2/src/workbench/widget-frame/createWidgetRuntime.ts@@ -15,6 +15,7 @@ import type {import type { WidgetPlacementProject } from '@workbench/widgetPlacementCommands';import type { WorkbenchAction } from '@workbench/workbenchState';+import { createProjectLogger } from '@workbench/diagnostics/logger';import {commandApi as sharedCommandApi,commandPaletteApi,@@ -110,9 +111,13 @@ export const createWidgetRuntime = ({const menus = {register: (menu) => menuApi.register({ ...menu, source }),} satisfies WidgetRuntimeApi['menus'];+ const diagnostics = {+ logger: (namespace) => createProjectLogger(namespace, { ...source, kind: 'widget' }),+ } satisfies WidgetRuntimeApi['diagnostics'];return {commands,+ diagnostics,hotkeys,instanceId: instance.id,menus,