Agent peripheral shape clustering does not group nearby shapes
Problem
The agent README describes PeripheralShapeCluster as grouping nearby shapes outside the viewport so the model gets a compressed spatial summary.
On current main, convertTldrawShapesToPeripheralShapes() expands every shape bounds by padding (the caller uses 75), but then merges a shape only when:
group.bounds.includes(item.bounds)That is containment, not proximity / overlap.
Two same-sized shapes 20px apart have heavily overlapping 75px-expanded bounds, but neither expanded box contains the other, so they become two peripheral clusters. A dense knowledge-map / diagram made of similarly sized nodes can therefore approach one cluster per offscreen shape despite the padding.
There is a second edge case: each item is merged into at most one existing group. If a later shape bridges two existing groups, the groups themselves are not merged, so connected spatial neighborhoods can remain split depending on insertion order.
I couldn't find regression coverage for this helper.
Expected
With positive padding, nearby shapes whose padded bounds form one connected spatial component should normally collapse into one peripheral cluster.
For example, three 100×100 shapes spaced 20px apart with padding: 75 should summarize as one cluster, not three.
Suggested direction
Treat padded-bounds intersection as the adjacency relation and merge connected components, including existing groups that become connected through a bridging shape.
This could be implemented with a small iterative merge for the starter-kit scale, or a spatial index / union-find if large pages make the quadratic path relevant.
Why this matters
PeripheralShapesPartUtil runs every agent request over every current-page shape outside the context bounds. The peripheral format is specifically the lowest-detail compression layer; if it fails to cluster ordinary adjacent nodes, both prompt size and spatial noise grow with offscreen canvas density.
Acceptance criteria
- nearby equal-sized shapes cluster when their padded bounds overlap
- separated groups remain separate
- a bridging shape merges two previously separate groups
- result does not depend on input ordering for those cases
- nested / contained shapes still cluster
- regression tests cover the cases above
Source: tldraw/tldraw