Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (claude): bake orientation into diffusion input so mask aligns with an oriented source when metadata step is off (#1763)

[._.]/ Adam Eivy8ed58976b5c3
diff --git a/server/routes/imageClean.js b/server/routes/imageClean.js
index 5430fc7f12..02c036dcbc 100644
--- a/server/routes/imageClean.js
+++ b/server/routes/imageClean.js
@@ -10,6 +10,7 @@
import { Router, raw } from 'express';
import { z } from 'zod';
+import sharp from 'sharp';
import { asyncHandler, ServerError } from '../lib/errorHandler.js';
import { validateRequest } from '../lib/validation.js';
import { cleanImageBuffer, CLEAN_LEVELS, compositeIgnoreZone, IGNORE_ZONE_FEATHER_DEFAULT } from '../lib/imageClean.js';
@@ -127,7 +128,7 @@ router.post(
// The original bytes are captured BEFORE any cleaning so the ignore-zone
// composite can restore true source pixels into the preserved regions (the
// whole point is to undo the diffusion inside the mask, not re-diffuse it).
- const originalBuffer = buffer;
+ let originalBuffer = buffer;
const result = await cleanImageBuffer(buffer, { metadata, denoise });
@@ -146,7 +147,25 @@ router.post(
const steps = [...result.steps];
if (diffusion === 'light') {
- const light = await applyLightRegen(result.data);
+ // Orientation parity for the ignore-zone composite: the user paints the
+ // mask on the browser preview (EXIF-oriented visual space), but when the
+ // metadata step is OFF `cleanImageBuffer` returns the source bytes with
+ // their orientation tag intact, and `applyLightRegen` reads UN-oriented
+ // dims. Left as-is, the mask (oriented) and the diffused base (stored
+ // space) would misalign under the composite's fit:fill resize on an
+ // Orientation 6/8 source. Bake orientation into BOTH the diffusion input
+ // and the preserved-original buffer so base + original + mask all share
+ // one visual space. No-op for the common already-oriented/no-orientation
+ // cases (a `.rotate()` re-encode with no EXIF tag just re-emits pixels).
+ let diffusionInput = result.data;
+ if (maskBuffer) {
+ const orientedPng = await sharp(result.data).rotate().png().toBuffer().catch(() => null);
+ if (orientedPng) {
+ diffusionInput = orientedPng;
+ originalBuffer = orientedPng;
+ }
+ }
+ const light = await applyLightRegen(diffusionInput);
if (!light) {
throw new ServerError('Invalid or corrupt image', {
status: 400,
diff --git a/server/routes/imageClean.test.js b/server/routes/imageClean.test.js
index f703816310..ddbe45ac3e 100644
--- a/server/routes/imageClean.test.js
+++ b/server/routes/imageClean.test.js
@@ -254,6 +254,35 @@ describe('POST /api/image-clean (raw transport)', () => {
expect(report.width).toBe(4);
});
+ it('aligns the mask with the diffused base on an EXIF-oriented source with metadata OFF', async () => {
+ // Orientation=6 (rotate 90° CW) JPEG: visual space is 40×20, stored is 20×40.
+ // With metadata=0 the clean step returns un-rotated bytes, so the route must
+ // bake orientation into the diffusion input to match the (oriented) mask.
+ const raw = await sharp({
+ create: { width: 20, height: 40, channels: 3, background: { r: 120, g: 90, b: 60 } },
+ }).jpeg().toBuffer();
+ const oriented = await sharp(raw).withMetadata({ orientation: 6 }).jpeg().toBuffer();
+ // Mask painted in VISUAL space (40 wide × 20 tall), left half white.
+ const vw = 40;
+ const vh = 20;
+ const maskRaw = Buffer.alloc(vw * vh, 0);
+ for (let y = 0; y < vh; y += 1) for (let x = 0; x < vw; x += 1) if (x < vw / 2) maskRaw[y * vw + x] = 255;
+ const maskPng = await sharp(maskRaw, { raw: { width: vw, height: vh, channels: 1 } }).png().toBuffer();
+ const lenPrefix = Buffer.alloc(4);
+ lenPrefix.writeUInt32BE(maskPng.length, 0);
+ const envelope = Buffer.concat([lenPrefix, maskPng, oriented]);
+
+ const res = await postImage('?metadata=0&denoise=0&diffusion=light&mask=1&feather=0', envelope, 'image/jpeg');
+ expect(res.status).toBe(200);
+ const report = reportOf(res);
+ // Output is delivered in the oriented (visual) dims — 40×20 — so the mask,
+ // base, and original all agree. A misaligned (un-baked) path would report
+ // 20×40 here.
+ expect(report.width).toBe(vw);
+ expect(report.height).toBe(vh);
+ expect(report.steps.some((s) => s.step === 'ignore-zone' && s.status === 'applied')).toBe(true);
+ });
+
it('degrades to no-mask when the envelope length prefix is malformed', async () => {
// A length that overruns the buffer → splitMaskEnvelope treats the whole
// body as the image, so a plain PNG (no real envelope) still cleans.