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 (codex r4): clip gate covers pinned keys and anchor locks

[._.]/ Adam Eivya49d3d4b3a5e
diff --git a/client/src/components/sprites/ReferenceWorkflow.jsx b/client/src/components/sprites/ReferenceWorkflow.jsx
index 6b6eeb763d..406d95bcf8 100644
--- a/client/src/components/sprites/ReferenceWorkflow.jsx
+++ b/client/src/components/sprites/ReferenceWorkflow.jsx
@@ -343,7 +343,7 @@ export default function ReferenceWorkflow({ record, reference, onChanged }) {
: <><Sparkles className="w-3 h-3" /> Generate</>}
</button>
{(candidatesByTarget[anchor.direction] || []).map((c) => (
- <CandidateTile key={c.path} recordId={recordId} candidate={c} locking={locking} onLock={(cand) => lock(anchor.direction, cand)} />
+ <CandidateTile key={c.path} recordId={recordId} candidate={c} locking={locking} clipRisk={clipRisks[c.path]} onLock={(cand, accept) => lock(anchor.direction, cand, accept)} />
))}
</div>
)}
diff --git a/server/services/sprites/chromaKey.js b/server/services/sprites/chromaKey.js
index 0d4a318a48..d8c007a7f2 100644
--- a/server/services/sprites/chromaKey.js
+++ b/server/services/sprites/chromaKey.js
@@ -83,15 +83,19 @@ function significantHues(palette, { minCountFrac = 0.005, minSaturation = 0.25,
* silently clipped from the immutable locked artifact. Returns a warning
* string or null.
*/
-export function keyProximityWarning(palette, genKeyHex, opts = {}) {
- const { r, g, b } = hexToRgb(genKeyHex);
+export function keyProximityWarning(palette, keyHex, { role = 'generation', ...opts } = {}) {
+ const { r, g, b } = hexToRgb(keyHex);
const keyHue = rgbToHsv(r, g, b).h;
const significant = significantHues(palette, opts);
if (!significant.length) return null;
const minDist = Math.min(...significant.map((c) => hueDistance(keyHue, c.h)));
- return minDist < MIN_HUE_SEPARATION
- ? `Character palette sits within ${Math.round(minDist)}° of the generation key ${genKeyHex} — exact-key details may have been clipped by the mask; consider pinning a different key and regenerating before locking`
- : null;
+ if (minDist >= MIN_HUE_SEPARATION) return null;
+ // `selected` = the key the artifact will be COMPOSITED onto (runtime keying
+ // would clip character pixels); `generation` = the key it was RENDERED on
+ // (exact-key details are already gone from the mask).
+ return role === 'selected'
+ ? `Character palette sits within ${Math.round(minDist)}° of the selected key ${keyHex} — runtime keying on it would clip character pixels; pick a different key before locking`
+ : `Character palette sits within ${Math.round(minDist)}° of the generation key ${keyHex} — exact-key details may have been clipped by the mask; consider pinning a different key and regenerating before locking`;
}
export function pickChromaKey(palette, opts = {}) {
diff --git a/server/services/sprites/reference.js b/server/services/sprites/reference.js
index 5037d6c3ae..d7178e2325 100644
--- a/server/services/sprites/reference.js
+++ b/server/services/sprites/reference.js
@@ -34,7 +34,7 @@ import {
buildMainReferencePrompt, buildAnchorPrompt,
} from './prompts.js';
import { pickChromaKey, keyProximityWarning, CHROMA_KEY_HEXES, DEFAULT_CHROMA_KEY } from './chromaKey.js';
-import { analyzeForeground, paletteFromAnalysis, normalizeFromAnalysis, normalizeAnchorFrame } from './normalize.js';
+import { analyzeForeground, paletteFromAnalysis, normalizeFromAnalysis } from './normalize.js';
// Default i2i strengths: anchors redraw a NEW facing from the main (mostly
// follow the prompt, borrow identity), an uploaded visual reference guides
@@ -140,6 +140,19 @@ function findAnchor(manifest, anchorId) {
return manifest.anchors.find((a) => a.id === anchorId) || null;
}
+// Clip risk against BOTH keys a lock touches: the generation key (near-key
+// details are already masked away) and, when different, the canvas key the
+// artifact is composited onto (runtime keying on it would clip character
+// pixels — e.g. green clothing locked onto a user-pinned green key).
+function combinedClipWarning(palette, maskKeyHex, canvasKeyHex) {
+ return [
+ keyProximityWarning(palette, maskKeyHex),
+ canvasKeyHex.toUpperCase() !== maskKeyHex.toUpperCase()
+ ? keyProximityWarning(palette, canvasKeyHex, { role: 'selected' })
+ : null,
+ ].filter(Boolean).join(' — ') || null;
+}
+
/**
* Reference-set view for the detail endpoint: the manifest (null until the
* first generate) plus the reviewable candidates parsed from their
@@ -364,9 +377,10 @@ async function lockReferenceImpl(recordId, { target, candidate, acceptClipRisk =
const selectedKey = userPinned ? record.chromaKey : auto.hex;
// Selection only sees pixels that SURVIVED the generation-key mask —
// exact-key details (magenta garment on the magenta default) are already
- // gone from the palette, so surface the risk instead of silently locking
- // a clipped identity root.
- const clipWarning = keyProximityWarning(palette, maskKey);
+ // gone from the palette — and a pinned key can collide with the palette
+ // that DID survive. Surface both risks instead of silently locking a
+ // clipped identity root.
+ const clipWarning = combinedClipWarning(palette, maskKey, selectedKey);
// A clip-risk lock is irreversible (the frozen root may already be
// missing exact-key details) — refuse unless the caller explicitly
// accepts, so the user learns BEFORE the 409 wall goes up, not after.
@@ -409,11 +423,20 @@ async function lockReferenceImpl(recordId, { target, candidate, acceptClipRisk =
}
// Manifest key is canonical after main lock — see startReferenceGeneration.
const canvasKey = manifest.chromaKey || record.chromaKey || DEFAULT_CHROMA_KEY;
+ // A direction can reveal key-colored detail the front view never showed
+ // (a green backpack on a green key) — anchors get the same clip gate as
+ // the main.
+ const analysis = await analyzeForeground(candAbs, maskKey);
+ const clipWarning = combinedClipWarning(paletteFromAnalysis(analysis), maskKey, canvasKey);
+ if (clipWarning && !acceptClipRisk) {
+ throw new ServerError(`${clipWarning}. Re-send with acceptClipRisk to lock anyway.`, { status: 409, code: 'CHROMA_CLIP_RISK' });
+ }
const rel = `reference/${await nextVersionPath(refDir, `${recordId}-${anchorId}`)}`;
const destAbs = join(spriteDir(recordId), rel);
- await normalizeAnchorFrame(candAbs, destAbs, { maskKeyHex: maskKey, canvasKeyHex: canvasKey });
+ await normalizeFromAnalysis(analysis, candAbs, destAbs, canvasKey);
Object.assign(anchor, {
status: 'locked', path: rel, lockedFrom: candidate, lockedAt: now, sha256: await sha256File(destAbs),
+ ...(clipWarning ? { clipWarning } : {}),
});
const allLocked = manifest.anchors.every((a) => a.status === 'locked');
if (allLocked) manifest.status = 'complete';
diff --git a/server/services/sprites/reference.test.js b/server/services/sprites/reference.test.js
index e6206b78f5..bfe93ec7de 100644
--- a/server/services/sprites/reference.test.js
+++ b/server/services/sprites/reference.test.js
@@ -266,6 +266,39 @@ describe('lockReference', () => {
expect(result.manifest.chromaKeyWarning).toMatch(/generation key #FF00FF/);
});
+ it('409s a lock whose PINNED key collides with the surviving palette', async () => {
+ const id = newId();
+ await createCharacter(id);
+ // Leaf-green clothing (hue ~138 after quantization) + user pins the
+ // green key (hue 120): compositing onto green would make runtime keying
+ // eat the clothing.
+ await records.updateRecord(id, { chromaKey: '#00FF00' });
+ const rel = await placeCandidate(id, 'main', 'walk-south-candidate-01.png', {
+ fg: { r: 40, g: 200, b: 80 }, // green clothing on the magenta gen key
+ });
+ await expect(lockReference(id, { target: 'main', candidate: rel }))
+ .rejects.toMatchObject({ status: 409, code: 'CHROMA_CLIP_RISK' });
+ const accepted = await lockReference(id, { target: 'main', candidate: rel, acceptClipRisk: true });
+ expect(accepted.manifest.chromaKey).toBe('#00FF00');
+ expect(accepted.manifest.chromaKeyWarning).toMatch(/selected key #00FF00/);
+ });
+
+ it('gates anchor locks on clip risk against the canonical key', async () => {
+ const id = newId();
+ await createCharacter(id);
+ await lockMain(id); // teal char → magenta key
+ // This facing reveals a pink feature near the magenta generation key.
+ const rel = await placeCandidate(id, 'east', 'walk-east-candidate-01.png', {
+ fg: { r: 255, g: 80, b: 230 },
+ });
+ await expect(lockReference(id, { target: 'east', candidate: rel }))
+ .rejects.toMatchObject({ status: 409, code: 'CHROMA_CLIP_RISK' });
+ const accepted = await lockReference(id, { target: 'east', candidate: rel, acceptClipRisk: true });
+ const east = accepted.manifest.anchors.find((a) => a.id === 'walk-east');
+ expect(east.status).toBe('locked');
+ expect(east.clipWarning).toMatch(/generation key #FF00FF/);
+ });
+
it('threads the saved local model into local-mode renders and provenance', async () => {
const id = newId();
await createCharacter(id);