Skip to content

clipboard: bind the first wl_seat only - #3989

Merged
bbedward merged 3 commits into
duxlabs-dev:masterfrom
sijin-xb:fix/clipboard-seat-binding
Oct 28, 2026
Merged

bbedward merged 3 commits into
duxlabs-dev:masterfrom
sijin-xb:fix/clipboard-seat-binding

Conversation

@sijin-xb

@sijin-xb sijin-xb commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Description

Clipboard history silently stops recording when the compositor advertises more than one wl_seat. niri does: it exposes seat0 alongside a niri-agent seat.

setupRegistry binds every wl_seat global it sees and assigns each one to m.seat, so the last seat wins. A data device created on niri-agent is accepted by the compositor and never delivers a selection or data_offer event. Nothing fails, so startup still logs a clean Data device setup complete while history stays frozen.

Measured on niri with the same binary, changing only which seat is bound:

Bound seat Selection events from 3 copies
seat0 (first) 3
niri-agent (last, current behaviour) 1, the replay at startup with id == nil

core/internal/clipboard/wl.go already guards against this for the CLI watcher (if s.seat != nil { return }), which is why dms clipboard watch keeps working on an affected compositor while the daemon's history does not. The manager was missing the same guard.

The second commit fixes an unrelated defect in the same path. TouchEntry routed through storeEntry, which deduplicates the row against itself and assigns a fresh sequence number. The id the panel was holding went stale as soon as an entry was touched, so restoring an entry could serve the previous clipboard content. The row is now rewritten in place, which means GetHistory has to sort by timestamp rather than relying on key order.

Fixes #3812, though the root cause there was misattributed to the shared wayland dispatcher at the time and the issue was closed before it was found.

A third commit answers review feedback on the in-place update. Rewriting a row without touching its key broke the assumption that bucket key order tracks recency, which trimLengthInTx and GetPinnedEntries leaned on. Rows now store nanosecond stamps, every listing shares one comparator that falls back to id, and trimLengthInTx ranks by that comparator instead of walking keys backwards. Legacy rows keep decoding at second resolution, told apart by magnitude.

Type of change

  • Bug fix (non-breaking change that fixes an issue)

Related issues

Fixes #3812

Screenshots / video

Not a visual change. Verification below.

Verification

Built and ran against niri, which advertises both seats.

INFO  go: Bound wl_seat                    # once, instead of twice
INFO  go: Data device setup complete

Three consecutive copies now appear in dms clipboard history, newest first. Restoring an entry keeps its id, so a paste immediately after selecting from the panel serves the selected entry.

$ dms clipboard history
ID: 4 | text | ...
  e2e-b-...
ID: 3 | text | ...
  e2e-a-...
  • make test passes
  • make fmt produces no diff
  • go build ./... clean

Checklist

  • My code follows the conventions in CONTRIBUTING.md
  • I have tested my changes locally
  • New user-facing strings are wrapped in I18n.tr() with translator context, reusing existing terms where possible
  • Go changes: ran make fmt, added/updated tests, make test passes, and go mod tidy is clean
  • QML changes: ran make lint-qml with no new warnings
  • I have opened a corresponding pull request in dlx-docs to document any new behaviors

The QML change is a sort comparator swap on an existing array, with no new strings or user-visible behaviour beyond ordering. No docs change applies: this restores documented behaviour rather than adding any.

niri advertises two seats, seat0 and niri-agent. setupRegistry bound every
wl_seat it saw, so the second one overwrote m.seat. A data device created
on niri-agent is valid and silent: it never receives selection or
data_offer events, so clipboard history stops recording while startup
still logs a clean "Data device setup complete".

Bind the first seat and skip the rest. connectSession already does this
for the CLI watcher, which is why `dms clipboard watch` kept working on
the same compositor.
TouchEntry routed through storeEntry, which deduplicates the row against
itself and assigns a fresh sequence number. The id the panel was holding
went stale the moment it was touched, so pasting an entry right after
selecting it could serve the previous clipboard content.

Rewrite the row in place so the id survives. GetHistory then has to sort
by timestamp: key order no longer tracks recency once rows are updated
without new keys. The QML list sorts the same way.
@bbedward

Copy link
Copy Markdown
Member

/claude review

if err != nil {
return err
}
return b.Put(itob(id), encoded)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A touched entry keeps its old (low) key, but trimLengthInTx still evicts by key order (c.Last()/c.Prev()). With history at MaxHistory, restoring the oldest entry and then copying anything new deletes the entry the user just restored, even though it's now the most recent. Before this change the touch moved it to a new top key, so it survived. trimLengthInTx (and Search, which also lists in key order) need to rank by timestamp too, or touch has to keep re-keying.

// place to keep its id stable for the UI.
slices.SortFunc(history, func(a, b Entry) int {
return b.Timestamp.Compare(a.Timestamp)
})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Timestamps are stored with one-second resolution (encodeEntry writes Timestamp.Unix()), and slices.SortFunc isn't stable. So two copies made within the same second can come back in either order, and the newest copy may not be at the top. Use ID as the tie-break, since that was the old ordering:

Suggested change
})
slices.SortFunc(history, func(a, b Entry) int {
if c := b.Timestamp.Compare(a.Timestamp); c != 0 {
return c
}
return cmp.Compare(b.ID, a.ID)
})

(needs cmp imported). ClipboardService.qml should break ties on id the same way.

@claude

claude Bot commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Claude review

The seat fix is good. The in-place touch breaks history trimming and makes the order unreliable for copies made within the same second.

  • Restored entry keeps its old key, so trimLengthInTx evicts it on the next copy when history is full, core/internal/server/clipboard/manager.go:1268
  • Unstable timestamp sort with one-second resolution has no ID tie-break, core/internal/server/clipboard/manager.go:1115
    Checked: wl_seat guard, TouchEntry/GetHistory/trim/dedupe/unpin/search ordering, the QML sort, and the updated test. Model: claude-opus-5-5.

TouchEntry rewrites a row in place to keep its id stable for the UI, which
breaks the assumption that bucket key order tracks recency. Three places
still relied on it:

- trimLengthInTx walked the bucket backwards and evicted the lowest keys,
  so a restored entry was the first casualty of the next copy once history
  was full.
- GetHistory sorted on a second-resolution stamp, leaving rows copied
  within the same second in bucket order.
- GetPinnedEntries never sorted at all, so a restored pinned entry sank to
  the bottom.

Rows now carry nanosecond stamps and every listing sorts through one
comparator that falls back to id. Legacy rows keep their second-resolution
stamp: the decoder tells the two apart by magnitude, since no
second-resolution stamp reaches 1e12 before the year 33658.

The QML list mirrors the same tie-break, because JS Date drops sub-
millisecond digits.
@sijin-xb

Copy link
Copy Markdown
Contributor Author

Both points are fixed in 047a2c4.

trimLengthInTx now ranks rows with the same comparator GetHistory uses instead of walking keys backwards, so a restored entry is no longer the first eviction.

The unstable sort had a deeper cause than a missing tie-break: encodeEntry stored Timestamp.Unix(), so rows copied within one second were indistinguishable. Rows now carry UnixNano(). The decoder tells legacy rows apart by magnitude, since no second-resolution stamp reaches 1e12 before the year 33658.

While auditing the other callers I found a third one your review did not mention: GetPinnedEntries never sorted at all. It returned bucket order, so restoring a pinned entry sank it to the bottom of the pinned list. It sorts now too.

Two regression tests cover the trim eviction and the same-second ordering. Both fail on the previous commit and pass now:

--- FAIL: TestTouchEntry_SurvivesTrimWhenHistoryFull   (before)
--- FAIL: TestGetHistory_SameSecondOrdersByID          (before)

Verified against a copy of a real 152-entry database: legacy rows decode unchanged, new rows store nanoseconds, and three copies in the same second list newest-first.

@bbedward

Copy link
Copy Markdown
Member

/claude review

@claude

claude Bot commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Claude review

No issues found. The follow-up commit fixes both earlier findings: trimLengthInTx now ranks by timestamp with an id tie-break, and rows store nanosecond stamps while legacy second-resolution rows still decode.
Checked: wl_seat guard, TouchEntry in-place rewrite, decoding of mixed legacy and nanosecond stamps, trim/GetHistory/GetPinnedEntries ordering, unpin and edit paths, the QML comparator, and the new tests. Search still lists in key order, as the earlier comment said. Model: claude-opus-5-5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clipboard history silently stops recording while the daemon reports a clean start

2 participants