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(webv2): preserve in-flight workflow invalidations

JPPhotob86e27932132
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/editor/CallSavedWorkflowSyncRuntime.browser.test.tsx b/invokeai/frontend/webv2/src/features/workflow/ui/editor/CallSavedWorkflowSyncRuntime.browser.test.tsx
index 43044667bc..7aff7d08a6 100644
--- a/invokeai/frontend/webv2/src/features/workflow/ui/editor/CallSavedWorkflowSyncRuntime.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/editor/CallSavedWorkflowSyncRuntime.browser.test.tsx
@@ -231,6 +231,37 @@ describe('CallSavedWorkflowSyncRuntime with an unreachable child workflow', () =
expect(readStatuses(readGraph())).toEqual(['error', 'error', 'error']);
});
+ it('keeps an invalidation armed while an existing detail request settles', async () => {
+ let resolveFirstRequest:
+ | ((record: { name: string; workflow: { edges: never[]; nodes: never[] }; workflow_id: string }) => void)
+ | undefined;
+ let attempts = 0;
+ getLibraryWorkflowRecordMock.mockImplementation((workflowId: string) => {
+ attempts += 1;
+
+ if (attempts === 1) {
+ return new Promise((resolve) => {
+ resolveFirstRequest = resolve;
+ });
+ }
+
+ return Promise.resolve({ name: workflowId, workflow: { edges: [], nodes: [] }, workflow_id: workflowId });
+ });
+
+ const { readGraph } = await mountWith([buildCallNode('call-1')]);
+ await settle(20);
+ invalidateWorkflowLibraryCache(MISSING_WORKFLOW_ID);
+ resolveFirstRequest?.({
+ name: MISSING_WORKFLOW_ID,
+ workflow: { edges: [], nodes: [] },
+ workflow_id: MISSING_WORKFLOW_ID,
+ });
+ await settle(80);
+
+ expect(getLibraryWorkflowRecordMock).toHaveBeenCalledTimes(2);
+ expect(readStatuses(readGraph())).toEqual(['ready']);
+ });
+
it('keeps an invalidation retry available when the first matching node changes', async () => {
const attempts = mockWorkflowThatRecoversAfterOneFailure(MISSING_WORKFLOW_ID);
const { readGraph, updateGraph } = await mountWith([buildCallNode('call-1'), buildCallNode('call-2')]);
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/editor/CallSavedWorkflowSyncRuntime.tsx b/invokeai/frontend/webv2/src/features/workflow/ui/editor/CallSavedWorkflowSyncRuntime.tsx
index 507c9765e0..3783a703d8 100644
--- a/invokeai/frontend/webv2/src/features/workflow/ui/editor/CallSavedWorkflowSyncRuntime.tsx
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/editor/CallSavedWorkflowSyncRuntime.tsx
@@ -83,6 +83,8 @@ export const pruneStaleCallSavedWorkflowNodeState = <T,>(
}
};
+type RetryableDetailWorkflowNode = { token: number; workflowId: string };
+
const hasSameFieldType = (left: unknown, right: unknown): boolean => {
if (!left || !right || typeof left !== 'object' || typeof right !== 'object') {
return false;
@@ -148,11 +150,18 @@ const needsDynamicFieldSync = (
export const CallSavedWorkflowSyncRuntime = () => {
const queryClient = useQueryClient();
const { commands, project: projectPort } = useWorkflowUi();
- const retryableDetailWorkflowIds = useRef(new Map<string, string>());
- const retryableDetailWorkflowQueries = useRef(new Set<string>());
+ const retryableDetailWorkflowIds = useRef(new Map<string, RetryableDetailWorkflowNode>());
+ const retryableDetailWorkflowQueries = useRef(new Map<string, number>());
+ const nextRetryToken = useRef(0);
const previousDetailWorkflowIds = useRef(new Map<string, string>());
const previousDetailStatuses = useRef(new Map<string, 'loading' | 'ready' | 'error'>());
const parseChildWorkflow = useMemo(() => createSavedWorkflowDocumentParser(), []);
+ const authorizeNodeRetry = (nodeId: string, workflowId: string) => {
+ retryableDetailWorkflowIds.current.set(nodeId, { token: ++nextRetryToken.current, workflowId });
+ };
+ const authorizeWorkflowRetry = (workflowId: string) => {
+ retryableDetailWorkflowQueries.current.set(workflowId, ++nextRetryToken.current);
+ };
const reconcile = () => {
const templatesSnapshot = getInvocationTemplatesSnapshot();
@@ -226,12 +235,14 @@ export const CallSavedWorkflowSyncRuntime = () => {
previousStatus === 'error' &&
query?.state.status === 'error')
) {
- retryableDetailWorkflowIds.current.set(node.id, workflowId);
+ authorizeNodeRetry(node.id, workflowId);
}
- const retryWasAuthorized =
- retryableDetailWorkflowIds.current.get(node.id) === workflowId ||
- retryableDetailWorkflowQueries.current.has(workflowId);
+ const nodeRetryAuthorization = retryableDetailWorkflowIds.current.get(node.id);
+ const nodeRetryToken =
+ nodeRetryAuthorization?.workflowId === workflowId ? nodeRetryAuthorization.token : undefined;
+ const workflowRetryToken = retryableDetailWorkflowQueries.current.get(workflowId);
+ const retryWasAuthorized = nodeRetryToken !== undefined || workflowRetryToken !== undefined;
if (
shouldFetchSavedWorkflowDetail(query, {
@@ -247,16 +258,34 @@ export const CallSavedWorkflowSyncRuntime = () => {
void queryClient
.fetchQuery(detailOptions)
.then(() => {
- if (retryableDetailWorkflowIds.current.get(node.id) === workflowId) {
+ const currentNodeRetry = retryableDetailWorkflowIds.current.get(node.id);
+ const currentWorkflowRetryToken = retryableDetailWorkflowQueries.current.get(workflowId);
+ const hasNewerWorkflowRetry = currentWorkflowRetryToken !== workflowRetryToken;
+
+ if (nodeRetryToken !== undefined && currentNodeRetry?.token === nodeRetryToken) {
retryableDetailWorkflowIds.current.delete(node.id);
}
- retryableDetailWorkflowQueries.current.delete(workflowId);
+ if (workflowRetryToken !== undefined && currentWorkflowRetryToken === workflowRetryToken) {
+ retryableDetailWorkflowQueries.current.delete(workflowId);
+ }
+
+ if (hasNewerWorkflowRetry) {
+ void queryClient.invalidateQueries({
+ exact: true,
+ queryKey: detailOptions.queryKey,
+ refetchType: 'none',
+ });
+ }
})
.catch(() => {
- if (retryableDetailWorkflowIds.current.get(node.id) === workflowId) {
+ const currentNodeRetry = retryableDetailWorkflowIds.current.get(node.id);
+ const currentWorkflowRetryToken = retryableDetailWorkflowQueries.current.get(workflowId);
+ if (nodeRetryToken !== undefined && currentNodeRetry?.token === nodeRetryToken) {
retryableDetailWorkflowIds.current.delete(node.id);
}
- retryableDetailWorkflowQueries.current.delete(workflowId);
+ if (workflowRetryToken !== undefined && currentWorkflowRetryToken === workflowRetryToken) {
+ retryableDetailWorkflowQueries.current.delete(workflowId);
+ }
const failedQuery = queryClient.getQueryCache().find({ queryKey: detailOptions.queryKey });
const currentNode = projectPort
.getSnapshot()
@@ -275,7 +304,7 @@ export const CallSavedWorkflowSyncRuntime = () => {
// Keep the node blocked while stale data is being retried. This
// permits transient revalidation failures to recover without
// allowing a deleted child workflow to enqueue stale inputs.
- retryableDetailWorkflowIds.current.set(node.id, workflowId);
+ authorizeNodeRetry(node.id, workflowId);
}
setStatus(node.id, workflowId, 'error');
});
@@ -345,10 +374,6 @@ export const CallSavedWorkflowSyncRuntime = () => {
}
});
const unsubscribeLibrary = onWorkflowLibraryCacheInvalidated((workflowId) => {
- const authorizeWorkflowRetry = (workflowId: string) => {
- retryableDetailWorkflowQueries.current.add(workflowId);
- };
-
if (workflowId) {
authorizeWorkflowRetry(workflowId);
void queryClient.invalidateQueries({