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
fix(webv2): roll back an auto-created paint layer when the stroke commits nothing
joshistoastd2aa5d8688cb
diff --git a/invokeai/frontend/webv2/performance/browser-baseline.json b/invokeai/frontend/webv2/performance/browser-baseline.jsonindex 4542f3d4ae..d63dcb192b 100644--- a/invokeai/frontend/webv2/performance/browser-baseline.json+++ b/invokeai/frontend/webv2/performance/browser-baseline.json@@ -1,6 +1,6 @@{"browserExecutable": "/home/toast/.cache/ms-playwright/chromium-1228/chrome-linux64/chrome",- "capturedAt": "2026-08-03",+ "capturedAt": "2026-08-04","routes": [{"activatedResourceBaseline": {@@ -25,11 +25,11 @@"scriptRequestCount": 0,"totalRawBytes": 0},- "domContentLoadedMedianMs": 106.09999999776483,+ "domContentLoadedMedianMs": 101.90000000037253,"id": "launchpad","layoutSwitchMedianMs": 0,- "loadMedianMs": 106.09999999776483,- "longestTaskMaxMs": 67,+ "loadMedianMs": 101.90000000037253,+ "longestTaskMaxMs": 65,"owner": "app","projectSwitchMedianMs": 0,"readyMark": "invokeai:ready:launchpad",@@ -56,7 +56,7 @@"scriptRequestCount": 16,"totalRawBytes": 1657498},- "routeReadyMedianMs": 229.30000000074506,+ "routeReadyMedianMs": 218.19999999925494,"scriptSourceOwnerSet": "launchpad-static","stateProfile": "empty"},@@ -83,11 +83,11 @@"scriptRequestCount": 0,"totalRawBytes": 0},- "domContentLoadedMedianMs": 109.20000000298023,+ "domContentLoadedMedianMs": 104.70000000018626,"id": "launchpad","layoutSwitchMedianMs": 0,- "loadMedianMs": 109.20000000298023,- "longestTaskMaxMs": 70,+ "loadMedianMs": 104.70000000018626,+ "longestTaskMaxMs": 64,"owner": "app","projectSwitchMedianMs": 0,"readyMark": "invokeai:ready:launchpad",@@ -114,7 +114,7 @@"scriptRequestCount": 16,"totalRawBytes": 1657498},- "routeReadyMedianMs": 288.70000000298023,+ "routeReadyMedianMs": 277.29999999981374,"scriptSourceOwnerSet": "launchpad-static","stateProfile": "representative"},@@ -141,13 +141,13 @@"scriptRequestCount": 0,"totalRawBytes": 0},- "domContentLoadedMedianMs": 108.90000000223517,+ "domContentLoadedMedianMs": 102.70000000018626,"id": "editor-minimal","layoutSwitchMedianMs": 0,- "loadMedianMs": 108.90000000223517,- "longestTaskMaxMs": 92,+ "loadMedianMs": 102.70000000018626,+ "longestTaskMaxMs": 89,"owner": "workbench",- "projectSwitchMedianMs": 70.42180999999982,+ "projectSwitchMedianMs": 65.76128900000003,"readyMark": "invokeai:ready:widget:center:preview","remediationTicket": "deepen-widget-registry-loading","resourceBaseline": {@@ -157,9 +157,9 @@"largestAssetRawBytes": 746428,"otherRawBytes": 122068,"requestCount": 99,- "scriptRawBytes": 3229475,+ "scriptRawBytes": 3232216,"scriptRequestCount": 93,- "totalRawBytes": 3549155+ "totalRawBytes": 3551896},"resourceLimits": {"cssRawBytes": 17748,@@ -168,11 +168,11 @@"largestAssetRawBytes": 753893,"otherRawBytes": 123289,"requestCount": 99,- "scriptRawBytes": 3261770,+ "scriptRawBytes": 3264539,"scriptRequestCount": 93,- "totalRawBytes": 3584647+ "totalRawBytes": 3587415},- "routeReadyMedianMs": 624.2999999970198,+ "routeReadyMedianMs": 609.4000000003725,"scriptSourceOwnerSet": "editor-minimal-static","stateProfile": "empty"},@@ -199,13 +199,13 @@"scriptRequestCount": 0,"totalRawBytes": 0},- "domContentLoadedMedianMs": 107.30000000074506,+ "domContentLoadedMedianMs": 104.3999999994412,"id": "editor-minimal","layoutSwitchMedianMs": 0,- "loadMedianMs": 107.30000000074506,- "longestTaskMaxMs": 102,+ "loadMedianMs": 104.3999999994412,+ "longestTaskMaxMs": 94,"owner": "workbench",- "projectSwitchMedianMs": 93.74111899999843,+ "projectSwitchMedianMs": 86.2747640000016,"readyMark": "invokeai:ready:widget:center:preview","remediationTicket": "deepen-widget-registry-loading","resourceBaseline": {@@ -215,9 +215,9 @@"largestAssetRawBytes": 746428,"otherRawBytes": 122068,"requestCount": 99,- "scriptRawBytes": 3229475,+ "scriptRawBytes": 3232216,"scriptRequestCount": 93,- "totalRawBytes": 3549155+ "totalRawBytes": 3551896},"resourceLimits": {"cssRawBytes": 17748,@@ -226,11 +226,11 @@"largestAssetRawBytes": 753893,"otherRawBytes": 123289,"requestCount": 99,- "scriptRawBytes": 3261770,+ "scriptRawBytes": 3264539,"scriptRequestCount": 93,- "totalRawBytes": 3584647+ "totalRawBytes": 3587415},- "routeReadyMedianMs": 683.3000000007451,+ "routeReadyMedianMs": 655.2000000001863,"scriptSourceOwnerSet": "editor-minimal-static","stateProfile": "representative"},@@ -257,11 +257,11 @@"scriptRequestCount": 4,"totalRawBytes": 280457},- "domContentLoadedMedianMs": 110.69999999925494,+ "domContentLoadedMedianMs": 103.6000000005588,"id": "editor-canvas",- "layoutSwitchMedianMs": 61.900000002235174,- "loadMedianMs": 110.69999999925494,- "longestTaskMaxMs": 97,+ "layoutSwitchMedianMs": 60.60000000055879,+ "loadMedianMs": 103.6000000005588,+ "longestTaskMaxMs": 87,"owner": "canvas","projectSwitchMedianMs": 0,"readyMark": "invokeai:ready:widget:center:canvas",@@ -273,9 +273,9 @@"largestAssetRawBytes": 746428,"otherRawBytes": 122068,"requestCount": 103,- "scriptRawBytes": 3507155,+ "scriptRawBytes": 3509896,"scriptRequestCount": 97,- "totalRawBytes": 3826835+ "totalRawBytes": 3829576},"resourceLimits": {"cssRawBytes": 17748,@@ -284,11 +284,11 @@"largestAssetRawBytes": 753893,"otherRawBytes": 123289,"requestCount": 103,- "scriptRawBytes": 3542227,+ "scriptRawBytes": 3544995,"scriptRequestCount": 97,- "totalRawBytes": 3865104+ "totalRawBytes": 3867872},- "routeReadyMedianMs": 1090.199999999255,+ "routeReadyMedianMs": 1042.0999999996275,"scriptSourceOwnerSet": "editor-canvas-static","stateProfile": "empty"},@@ -315,11 +315,11 @@"scriptRequestCount": 4,"totalRawBytes": 280457},- "domContentLoadedMedianMs": 107.09999999776483,+ "domContentLoadedMedianMs": 104.29999999981374,"id": "editor-canvas",- "layoutSwitchMedianMs": 180.90000000223517,- "loadMedianMs": 107.09999999776483,- "longestTaskMaxMs": 379,+ "layoutSwitchMedianMs": 163.79999999981374,+ "loadMedianMs": 104.29999999981374,+ "longestTaskMaxMs": 334,"owner": "canvas","projectSwitchMedianMs": 0,"readyMark": "invokeai:ready:widget:center:canvas",@@ -331,9 +331,9 @@"largestAssetRawBytes": 746428,"otherRawBytes": 122068,"requestCount": 103,- "scriptRawBytes": 3507155,+ "scriptRawBytes": 3509896,"scriptRequestCount": 97,- "totalRawBytes": 3826835+ "totalRawBytes": 3829576},"resourceLimits": {"cssRawBytes": 17748,@@ -342,11 +342,11 @@"largestAssetRawBytes": 753893,"otherRawBytes": 123289,"requestCount": 103,- "scriptRawBytes": 3542227,+ "scriptRawBytes": 3544995,"scriptRequestCount": 97,- "totalRawBytes": 3865104+ "totalRawBytes": 3867872},- "routeReadyMedianMs": 1230.3999999985099,+ "routeReadyMedianMs": 1194,"scriptSourceOwnerSet": "editor-canvas-static","stateProfile": "representative"},@@ -373,11 +373,11 @@"scriptRequestCount": 0,"totalRawBytes": 0},- "domContentLoadedMedianMs": 109,+ "domContentLoadedMedianMs": 103.70000000018626,"id": "editor-workflow",- "layoutSwitchMedianMs": 62.600000001490116,- "loadMedianMs": 109,- "longestTaskMaxMs": 96,+ "layoutSwitchMedianMs": 59.60000000055879,+ "loadMedianMs": 103.79999999981374,+ "longestTaskMaxMs": 87,"owner": "workflow","projectSwitchMedianMs": 0,"readyMark": "invokeai:ready:widget:center:workflow",@@ -389,9 +389,9 @@"largestAssetRawBytes": 746428,"otherRawBytes": 122068,"requestCount": 99,- "scriptRawBytes": 3229475,+ "scriptRawBytes": 3232216,"scriptRequestCount": 93,- "totalRawBytes": 3549155+ "totalRawBytes": 3551896},"resourceLimits": {"cssRawBytes": 17748,@@ -400,11 +400,11 @@"largestAssetRawBytes": 753893,"otherRawBytes": 123289,"requestCount": 99,- "scriptRawBytes": 3261770,+ "scriptRawBytes": 3264539,"scriptRequestCount": 93,- "totalRawBytes": 3584647+ "totalRawBytes": 3587415},- "routeReadyMedianMs": 1076.1000000014901,+ "routeReadyMedianMs": 1041,"scriptSourceOwnerSet": "editor-minimal-static","stateProfile": "empty"},@@ -431,11 +431,11 @@"scriptRequestCount": 0,"totalRawBytes": 0},- "domContentLoadedMedianMs": 107.69999999925494,+ "domContentLoadedMedianMs": 103.5,"id": "editor-workflow",- "layoutSwitchMedianMs": 104.19999999925494,- "loadMedianMs": 107.69999999925494,- "longestTaskMaxMs": 368,+ "layoutSwitchMedianMs": 96.90000000037253,+ "loadMedianMs": 103.5,+ "longestTaskMaxMs": 337,"owner": "workflow","projectSwitchMedianMs": 0,"readyMark": "invokeai:ready:widget:center:workflow",@@ -447,9 +447,9 @@"largestAssetRawBytes": 746428,"otherRawBytes": 122068,"requestCount": 99,- "scriptRawBytes": 3229475,+ "scriptRawBytes": 3232216,"scriptRequestCount": 93,- "totalRawBytes": 3549155+ "totalRawBytes": 3551896},"resourceLimits": {"cssRawBytes": 17748,@@ -458,11 +458,11 @@"largestAssetRawBytes": 753893,"otherRawBytes": 123289,"requestCount": 99,- "scriptRawBytes": 3261770,+ "scriptRawBytes": 3264539,"scriptRequestCount": 93,- "totalRawBytes": 3584647+ "totalRawBytes": 3587415},- "routeReadyMedianMs": 1150.800000000745,+ "routeReadyMedianMs": 1106.2000000001863,"scriptSourceOwnerSet": "editor-minimal-static","stateProfile": "representative"},@@ -489,11 +489,11 @@"scriptRequestCount": 0,"totalRawBytes": 0},- "domContentLoadedMedianMs": 109.40000000223517,+ "domContentLoadedMedianMs": 104.8999999994412,"id": "editor-gallery",- "layoutSwitchMedianMs": 28.199999999254942,- "loadMedianMs": 109.40000000223517,- "longestTaskMaxMs": 93,+ "layoutSwitchMedianMs": 24.40000000037253,+ "loadMedianMs": 104.8999999994412,+ "longestTaskMaxMs": 90,"owner": "gallery","projectSwitchMedianMs": 0,"readyMark": "invokeai:ready:widget:center:gallery",@@ -505,9 +505,9 @@"largestAssetRawBytes": 746428,"otherRawBytes": 122068,"requestCount": 99,- "scriptRawBytes": 3229475,+ "scriptRawBytes": 3232216,"scriptRequestCount": 93,- "totalRawBytes": 3549155+ "totalRawBytes": 3551896},"resourceLimits": {"cssRawBytes": 17748,@@ -516,11 +516,11 @@"largestAssetRawBytes": 753893,"otherRawBytes": 123289,"requestCount": 99,- "scriptRawBytes": 3261770,+ "scriptRawBytes": 3264539,"scriptRequestCount": 93,- "totalRawBytes": 3584647+ "totalRawBytes": 3587415},- "routeReadyMedianMs": 1328.699999999255,+ "routeReadyMedianMs": 1284.300000000745,"scriptSourceOwnerSet": "editor-minimal-static","stateProfile": "empty"},@@ -547,11 +547,11 @@"scriptRequestCount": 0,"totalRawBytes": 0},- "domContentLoadedMedianMs": 110,+ "domContentLoadedMedianMs": 103.6000000005588,"id": "editor-gallery",- "layoutSwitchMedianMs": 51.80000000074506,- "loadMedianMs": 110,- "longestTaskMaxMs": 104,+ "layoutSwitchMedianMs": 49.5,+ "loadMedianMs": 103.6000000005588,+ "longestTaskMaxMs": 91,"owner": "gallery","projectSwitchMedianMs": 0,"readyMark": "invokeai:ready:widget:center:gallery",@@ -563,9 +563,9 @@"largestAssetRawBytes": 746428,"otherRawBytes": 122068,"requestCount": 99,- "scriptRawBytes": 3229475,+ "scriptRawBytes": 3232216,"scriptRequestCount": 93,- "totalRawBytes": 3549155+ "totalRawBytes": 3551896},"resourceLimits": {"cssRawBytes": 17748,@@ -574,11 +574,11 @@"largestAssetRawBytes": 753893,"otherRawBytes": 123289,"requestCount": 99,- "scriptRawBytes": 3261770,+ "scriptRawBytes": 3264539,"scriptRequestCount": 93,- "totalRawBytes": 3584647+ "totalRawBytes": 3587415},- "routeReadyMedianMs": 1387.5,+ "routeReadyMedianMs": 1352.1000000005588,"scriptSourceOwnerSet": "editor-minimal-static","stateProfile": "representative"}@@ -1442,6 +1442,7 @@"source:src/workbench/canvas-engine/rasterSnapshotCapture.ts","source:src/workbench/canvas-engine/render/adjustedSurfaceCache.ts","source:src/workbench/canvas-engine/render/adjustments.ts",+ "source:src/workbench/canvas-engine/render/alphaBounds.ts","source:src/workbench/canvas-engine/render/colorSample.ts","source:src/workbench/canvas-engine/render/compositeFrame.ts","source:src/workbench/canvas-engine/render/compositor.ts",@@ -1456,6 +1457,7 @@"source:src/workbench/canvas-engine/render/maskFill.ts","source:src/workbench/canvas-engine/render/overlayFrame.ts","source:src/workbench/canvas-engine/render/overlayRenderer.ts",+ "source:src/workbench/canvas-engine/render/paintCacheTrim.ts","source:src/workbench/canvas-engine/render/raster.ts","source:src/workbench/canvas-engine/render/rasterComposite.ts","source:src/workbench/canvas-engine/render/rasterizers/gradientRasterizer.ts",@@ -2333,6 +2335,7 @@"source:src/workbench/canvas-engine/rasterSnapshotCapture.ts","source:src/workbench/canvas-engine/render/adjustedSurfaceCache.ts","source:src/workbench/canvas-engine/render/adjustments.ts",+ "source:src/workbench/canvas-engine/render/alphaBounds.ts","source:src/workbench/canvas-engine/render/colorSample.ts","source:src/workbench/canvas-engine/render/compositeFrame.ts","source:src/workbench/canvas-engine/render/compositor.ts",@@ -2347,6 +2350,7 @@"source:src/workbench/canvas-engine/render/maskFill.ts","source:src/workbench/canvas-engine/render/overlayFrame.ts","source:src/workbench/canvas-engine/render/overlayRenderer.ts",+ "source:src/workbench/canvas-engine/render/paintCacheTrim.ts","source:src/workbench/canvas-engine/render/raster.ts","source:src/workbench/canvas-engine/render/rasterComposite.ts","source:src/workbench/canvas-engine/render/rasterizers/gradientRasterizer.ts",diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/moveHitTest.test.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/moveHitTest.test.tsindex befd78715e..33b8f44072 100644--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/moveHitTest.test.ts+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/moveHitTest.test.ts@@ -257,3 +257,51 @@ describe('live cache rect (freshly-painted, not-yet-flushed content)', () => {expect(hitTestLayer(layer, doc([layer]), { x: 5, y: 5 }, liveRect)).toBe(true);});});++describe('a layer erased to nothing behaves exactly like a brand-new one', () => {+ /**+ * The reported bug: a layer whose pixels had all been erased kept a full-size+ * bitmap of transparent pixels, so it still drew a movable, transformable blue+ * outline around an empty region.+ *+ * The paint-cache trim now clears such a layer to `{ bitmap: null }` — the same+ * source a freshly-added layer is created with — so every affordance derived from+ * `hittableLayerRect` disappears. These pin that equivalence, since it is the+ * whole reason the fix works without touching the overlay or the tools.+ */+ it('reports no hittable extent once its bitmap has been cleared', () => {+ const erased = paintLayer('erased');+ const fresh = paintLayer('fresh');++ expect(hittableLayerSize(erased, doc([erased]))).toBeNull();+ expect(hittableLayerSize(erased, doc([erased]))).toEqual(hittableLayerSize(fresh, doc([fresh])));+ });++ it('draws no move outline, even under a non-identity transform', () => {+ // Scale and rotation are exactly where a phantom rect is most visible, so the+ // suppression must not depend on the transform being trivial.+ const erased: CanvasLayerContract = {+ ...paintLayer('erased'),+ transform: { rotation: 0.6, scaleX: 2.5, scaleY: 1.75, x: 40, y: 25 },+ };+ expect(layerOutlineCorners(erased, doc([erased]))).toBeNull();+ expect(layerOutlineCorners(erased, doc([erased]), { x: 10, y: 10 })).toBeNull();+ });++ it('cannot be grabbed anywhere it used to cover', () => {+ const erased = paintLayer('erased');+ for (const point of [+ { x: 0, y: 0 },+ { x: 20, y: 20 },+ { x: 60, y: 40 },+ ]) {+ expect(hitTestLayer(erased, doc([erased]), point)).toBe(false);+ }+ });++ it('applies to a cleared MASK too, since masks share the paint path', () => {+ const erased = maskLayer('erased-mask');+ expect(hittableLayerSize(erased, doc([erased]))).toBeNull();+ expect(layerOutlineCorners(erased, doc([erased]))).toBeNull();+ });+});diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/paintTool.test.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/paintTool.test.tsindex 8c1f7697bd..1266311bda 100644--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/paintTool.test.ts+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/paintTool.test.ts@@ -508,3 +508,116 @@ describe('mask strokes are forced opaque', () => {expect(lastGlobalAlpha(cacheSurface(h, 'paint1'))).toBe(0.5);});});++describe('auto-created layer rollback (a gesture that commits nothing leaves no trace)', () => {+ /**+ * The auto-create dispatch happens at pointer-DOWN, before any pixel exists, and+ * deliberately outside history — so a gesture that produces no dirty rect has to+ * roll the layer back itself, or it strands a layer the user cannot undo.+ */+ const clipped = (doc: CanvasDocumentContractV2) => {+ const h = createHarness(doc);+ // Clip-to-bbox on, with the whole stroke outside the frame: every `paint()`+ // call bails, so `commit()` returns null.+ const ctx: ToolContext = { ...h.ctx, getStrokeClipRect: () => ({ height: 10, width: 10, x: 0, y: 0 }) };+ return { ...h, ctx };+ };++ const kinds = (h: { dispatched: CanvasProjectMutation[] }): string[] => h.dispatched.map((action) => action.type);++ it('removes the layer and restores the prior selection when the stroke is fully clipped away', () => {+ const h = clipped(makeDoc([imageLayer('img1'), paintLayer('p1')], 'img1'));+ const brush = createBrushTool();++ down(brush, h.ctx, pointer(80, 80));+ move(brush, h.ctx, pointer(90, 90), [pointer(90, 90)]);+ up(brush, h.ctx, pointer(90, 90));++ expect(h.strokes).toHaveLength(0);+ expect(kinds(h)).toEqual(['addCanvasLayer', 'removeCanvasLayers', 'setCanvasSelectedLayer']);+ const removal = h.dispatched[1]!;+ expect(removal.type === 'removeCanvasLayers' && removal.ids).toEqual([h.createdIds[0]]);+ // The reducer's nearest-neighbour fallback would have picked the top layer, not+ // the image layer the user actually had selected.+ const reselect = h.dispatched[2]!;+ expect(reselect.type === 'setCanvasSelectedLayer' && reselect.id).toBe('img1');+ });++ it('drops the provisional cache entry too', () => {+ const h = clipped(makeDoc([imageLayer('img1')], 'img1'));+ const brush = createBrushTool();++ down(brush, h.ctx, pointer(80, 80));+ expect(h.layers.peek(h.createdIds[0]!)).toBeDefined();+ up(brush, h.ctx, pointer(80, 80));++ expect(h.layers.peek(h.createdIds[0]!)).toBeUndefined();+ });++ it('rolls back on pointercancel mid-drag', () => {+ const h = createHarness(makeDoc([imageLayer('img1')], 'img1'));+ const brush = createBrushTool();++ down(brush, h.ctx, pointer(20, 20));+ move(brush, h.ctx, pointer(30, 30), [pointer(30, 30)]);+ cancel(brush, h.ctx);++ expect(h.strokes).toHaveLength(0);+ expect(kinds(h)).toEqual(['addCanvasLayer', 'removeCanvasLayers', 'setCanvasSelectedLayer']);+ expect(h.layers.peek(h.createdIds[0]!)).toBeUndefined();+ });++ it('rolls back when the tool is switched away mid-drag', () => {+ const h = createHarness(makeDoc([imageLayer('img1')], 'img1'));+ const brush = createBrushTool();++ down(brush, h.ctx, pointer(20, 20));+ brush.onDeactivate?.(h.ctx, undefined);++ expect(kinds(h)).toEqual(['addCanvasLayer', 'removeCanvasLayers', 'setCanvasSelectedLayer']);+ expect(h.layers.peek(h.createdIds[0]!)).toBeUndefined();+ });++ it('does NOT roll back for a temporary tool switch (space/alt hold keeps the gesture)', () => {+ const h = createHarness(makeDoc([imageLayer('img1')], 'img1'));+ const brush = createBrushTool();++ down(brush, h.ctx, pointer(20, 20));+ brush.onDeactivate?.(h.ctx, { temporary: true });++ expect(kinds(h)).toEqual(['addCanvasLayer']);+ expect(h.layers.peek(h.createdIds[0]!)).toBeDefined();+ });++ it('omits the reselect when nothing was selected to begin with', () => {+ const h = clipped(makeDoc([paintLayer('p1')], null));+ const brush = createBrushTool();++ down(brush, h.ctx, pointer(80, 80));+ up(brush, h.ctx, pointer(80, 80));++ expect(kinds(h)).toEqual(['addCanvasLayer', 'removeCanvasLayers']);+ });++ it('keeps the layer when the stroke DOES commit pixels', () => {+ const h = createHarness(makeDoc([imageLayer('img1')], 'img1'));+ const brush = createBrushTool();++ down(brush, h.ctx, pointer(20, 20));+ up(brush, h.ctx, pointer(20, 20));++ expect(h.strokes).toHaveLength(1);+ expect(kinds(h)).toEqual(['addCanvasLayer']);+ expect(h.layers.peek(h.createdIds[0]!)).toBeDefined();+ });++ it('never rolls back a layer it did not create (an existing paint target)', () => {+ const h = clipped(makeDoc([paintLayer('p1')], 'p1'));+ const brush = createBrushTool();++ down(brush, h.ctx, pointer(80, 80));+ up(brush, h.ctx, pointer(80, 80));++ expect(h.dispatched).toHaveLength(0);+ });+});diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/paintTool.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/paintTool.tsindex 12218fc2b3..8477b4ed90 100644--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/paintTool.ts+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/paintTool.ts@@ -131,6 +131,7 @@ const resolveTarget = (ctx: ToolContext): PaintTarget | null => {// fresh paint layer (inserted on top and selected by the reducer) and paint// into it. This is the single allowed gesture-start dispatch.const layerId = ctx.createLayerId();+ const previousSelectedLayerId = doc.selectedLayerId;const layer: CanvasRasterLayerContractV2 = {blendMode: 'normal',id: layerId,@@ -151,7 +152,23 @@ const resolveTarget = (ctx: ToolContext): PaintTarget | null => {const entry = ctx.layers.getOrCreateRect(layerId, { height: 0, width: 0, x: 0, y: 0 });entry.stale = false;return {- cancel: () => undefined,+ // This dispatch happens at pointer-DOWN, before a single pixel exists, and it+ // is deliberately outside history (the stroke's own composed entry owns the+ // create+paint pair). So it needs a real transactional rollback, like the+ // control branch above: a gesture that commits nothing must leave no trace.+ // Reachable whenever `strokeSession.commit()` returns null — every stroke+ // point clipped away by the generation frame or the pixel selection — and from+ // pointercancel, a tool switch mid-drag, or a throwing session construction.+ cancel: () => {+ ctx.layers.delete(layerId);+ ctx.dispatch({ ids: [layerId], type: 'removeCanvasLayers' });+ // Removing the top layer would otherwise leave the reducer's generic+ // "nearest remaining neighbour" fallback selecting whatever sits at the top,+ // not the layer the user actually had selected when the gesture began.+ if (previousSelectedLayerId !== null) {+ ctx.dispatch({ id: previousSelectedLayerId, type: 'setCanvasSelectedLayer' });+ }+ },commit: (event) => ctx.emitStrokeCommitted(event),createdLayer: { index: 0, layer },layerId,