From 31cf2f71471c2f69d1a9261d910c9601a3ca23e5 Mon Sep 17 00:00:00 2001 From: "congvc-bot[bot]" <3634010+congvc-bot[bot]@users.noreply.github.com> Date: Thu, 19 Nov 2026 11:28:53 +0700 Subject: [PATCH] fix(doop): update_frame free-space hint is the frame's own x/y (CHE-6119) [squash] The refusal told an agent the free spot of the turned (on-screen) box. Applied as x/y of a rotated frame it landed offset from that spot. Share one helper with create_frame so both return the unrotated rect position that puts the turned box on the free spot. Co-Authored-By: Claude Sonnet 5.5 --- server/actions.ts | 8 +++----- server/frameLayout.ts | 11 +++++++++++ server/pageTools.ts | 8 ++++++-- tests/frameOverlapGuard.test.ts | 19 +++++++++++++++++++ 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/server/actions.ts b/server/actions.ts index c06d5321a..bc85f5b59 100644 --- a/server/actions.ts +++ b/server/actions.ts @@ -7,7 +7,7 @@ import { colorFor } from '../shared/types.ts' import type { StackWrite } from '../shared/frameStack.ts' import { DEFAULT_ROLE_ID, mentionedRole, normalizePipeline, roleByAgentName, roleName } from '../shared/agents.ts' import { canvasPages, framePageId, pageFrames } from '../shared/pages.ts' -import { conflicts, defaultSpot, freeSpot, visibleBox } from './frameLayout.ts' +import { conflicts, defaultSpot, freeSpotFor, visibleBox } from './frameLayout.ts' import { normalizeRotation } from '../shared/frameRotation.ts' import { SnapshotError, capturePage, compareFrames, sameState, signature, stateOf } from './pageSnapshots.ts' import { FRAME_COMMENT_SELECTOR, isCanvasComment, trimComments, type CommentAnchorInput } from '../shared/comments.ts' @@ -2181,10 +2181,8 @@ function clearOfOverlap< }) if (!conflicts(shown, frames).length) return { input, moved: false } /* the free spot is for the visible box, so the unrotated rect sits where its turned bounds would */ - const spot = freeSpot(frames) - const w = input.width ?? 640 - const h = input.height ?? 480 - return { input: { ...input, x: spot.x + (shown.width - w) / 2, y: spot.y + (shown.height - h) / 2 }, moved: true } + const size = { width: input.width ?? 640, height: input.height ?? 480 } + return { input: { ...input, ...freeSpotFor(frames, shown, size) }, moved: true } } export function createFrame( diff --git a/server/frameLayout.ts b/server/frameLayout.ts index 6e2f52dec..d750f0977 100644 --- a/server/frameLayout.ts +++ b/server/frameLayout.ts @@ -45,6 +45,17 @@ export function freeSpot(frames: Placed[]): { x: number; y: number } { } } +/** Free spot for a frame that shows as `shown` (its `visibleBox`): the x/y to give the frame itself, so that its + * turned box lands on `freeSpot`. A turned frame's x/y is its unrotated rect, offset from its on-screen box. */ +export function freeSpotFor( + frames: Placed[], + shown: Box, + size: { width: number; height: number }, +): { x: number; y: number } { + const spot = freeSpot(frames) + return { x: spot.x + (shown.width - size.width) / 2, y: spot.y + (shown.height - size.height) / 2 } +} + /** Where the store puts a frame with no x/y: right of the right-most frame, or the page origin when empty. */ export function defaultSpot(frames: Pick[]): { x: number diff --git a/server/pageTools.ts b/server/pageTools.ts index d75491323..870d50fb4 100644 --- a/server/pageTools.ts +++ b/server/pageTools.ts @@ -2,7 +2,7 @@ import { normalizeRotation } from '../shared/frameRotation.ts' import * as actions from './actions.ts' import { MAX_PAGES, PageLimitError } from './store.ts' import { canvasPages, framePageId, MAX_PAGE_NAME, pageFrames, samePages } from '../shared/pages.ts' -import { conflicts, freeSpot, visibleBox, FRAME_GAP } from './frameLayout.ts' +import { conflicts, freeSpotFor, visibleBox, FRAME_GAP } from './frameLayout.ts' import type { Actor, Canvas, CanvasPageInfo, Frame } from '../shared/types.ts' /** @@ -99,7 +99,11 @@ function overlapRefusal(canvas: Canvas, frame: Frame, patch: FrameMetaPatch): st const others = pageFrames(canvas, pageId) const hit = conflicts(shown, others, frame.id) if (!hit.length) return undefined - const spot = freeSpot(others.filter((f) => f.id !== frame.id)) + const spot = freeSpotFor( + others.filter((f) => f.id !== frame.id), + shown, + box, + ) return `frames may not overlap or sit closer than ${FRAME_GAP}px: this ${Math.round(shown.width)}x${Math.round(shown.height)} box at ${Math.round(shown.x)},${Math.round(shown.y)} hits ${hit.map((f) => `${f.id} “${f.name}”`).join(', ')}. Free space: x ${spot.x}, y ${spot.y}.` } diff --git a/tests/frameOverlapGuard.test.ts b/tests/frameOverlapGuard.test.ts index 365b7bd28..265a2714a 100644 --- a/tests/frameOverlapGuard.test.ts +++ b/tests/frameOverlapGuard.test.ts @@ -100,6 +100,25 @@ describe('agent writes through MCP', () => { expect(overlapping(await frames(canvas.id))).toEqual([]) }) + it('the free-space hint of a refused update is the unrotated x/y: applying it puts the turned frame in that spot', async () => { + const canvas: Canvas = await (await owner.post('/api/canvases', { name: 'Hint' })).json() + const mk = async (name: string, x: number, y: number, width: number, height: number) => + JSON.parse((await call('create_frame', { canvas_id: canvas.id, name, html: '', x, y, width, height })).text) + const a = await mk('A', 0, 0, 400, 100) + await mk('B', 150, 200, 100, 100) + const refused = await call('update_frame', { frame_id: a.frame.id, rotation: 90 }) + expect(refused.isError).toBe(true) + const hint = /Free space: x (-?[\d.]+), y (-?[\d.]+)/.exec(refused.text)! + const spot = { x: Number(hint[1]), y: Number(hint[2]) } + const applied = await call('update_frame', { frame_id: a.frame.id, rotation: 90, ...spot }) + expect(applied.isError).toBe(false) + const shown = visibleBox((await frames(canvas.id)).find((f) => f.id === a.frame.id)!) + // the hint is where the visible (turned) box should land: left edge 150 (B's), below B (300) plus the 80px gap + expect(shown.x).toBeCloseTo(150) + expect(shown.y).toBeCloseTo(380) + expect(overlapping(await frames(canvas.id))).toEqual([]) + }) + it('create_frame at an overlapping x/y lands in free space and says so', async () => { const canvas: Canvas = await (await owner.post('/api/canvases', { name: 'Create' })).json() const a = JSON.parse((await call('create_frame', { canvas_id: canvas.id, name: 'A', html: '', x: 0, y: 0 })).text)