Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions server/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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(
Expand Down
11 changes: 11 additions & 0 deletions server/frameLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Frame, 'x' | 'y' | 'width' | 'height' | 'rotation'>[]): {
x: number
Expand Down
8 changes: 6 additions & 2 deletions server/pageTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

/**
Expand Down Expand Up @@ -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}.`
}

Expand Down
19 changes: 19 additions & 0 deletions tests/frameOverlapGuard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading