Skip to content

pair: merge a computer's own chats when it joins, instead of replacing them - #2031

Merged
AbirAbbas merged 8 commits into
app/v3from
feat/pair-merge
Oct 29, 2026
Merged

AbirAbbas merged 8 commits into
app/v3from
feat/pair-merge

Conversation

@AbirAbbas

@AbirAbbas AbirAbbas commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Why

Pairing treats all paired computers as one identity. The computer that enters the code (or opens the link) adopts the identity of the computer that shows it. If that computer already had chats, the only choice was to give them up. The app asked "Replace this computer's chats?", and replacing cleared its preferences, shared memories and saved keys. The terminal refused unless --replace was passed.

That is the wrong model. Paired computers should share their chats, and pairing should never destroy anything.

What changes

  • Pairing merges. A computer that has chats but has never been paired joins by code or link without a replace question. It keeps its full histories, saved keys, memory and preferences. Its chats are published under the shared keys with their original ids, and this computer stays their holder. Both computers list both sets of chats. Continue here (agents.take) moves a chat in either direction, as before.
  • Safe to interrupt. The approved switch is recorded before the keys are restaged, so a later start finishes an interrupted merge with the same device. Background sharing retries only the chats still waiting, with pauses growing from 5 s to 1 min. A merged, idle computer makes no new periodic relay requests.
  • Saved keys. Vault writes and the pairing switch share one per-home file lock, and each write re-reads the current key under it.
  • Terminal. codeaf pair <code> and link joins merge too. They say "Sharing finishes the next time codeaf runs." --replace stays an explicit, confirmed clean slate. It records its phases, so recovery never repeats the reset, and it sets the old chat folders aside.
  • Words. The app's joined line is "Paired. Your chats are kept. Both computers' chats will appear as sharing finishes." The manual pages that described replacing chats on pairing are updated.
  • No protocol change. The app path still ends with done and a restart. Arrivals use the existing chat.changed and agent.changed events, and progress uses the existing sync.changed reason.

The desktop needs no change for this. codeaf-desk #67 separately stops older engines' replace question from highlighting Replace.

Validation contract

  • M1 No loss, no question. A used, never-paired computer joins by code, link, terminal or app. No replace question is asked, and every chat, saved key, memory and preference is kept.
  • M2 Both sides see everything. Each computer lists the other's chats with Continue here available. Its own chats stay here, and chats.list agrees on both.
  • M3 Pull works both ways, with history intact.
  • M4 The computer that shows the code is unchanged.
  • M5 An interrupted merge loses nothing. It finishes on the next start without duplicates.
  • M6 Unchanged paths stay unchanged. Same set reports already paired. The successor path and the fresh-home merge are as before.
  • M7 A used computer that already shares another set is refused with one plain sentence, and nothing changes.
  • M8 Replace is gone from the default paths. Terminal --replace remains opt-in.
  • M9 Person-facing lines use plain words, and the manual is updated.
  • M10 The desktop sees it with no protocol change.

Review and fixes

A read-only review of the first version found seven items. Six were real, and each was fixed with a reproducing TestR1_* test:

  • a saved-key write racing the switch
  • --replace recovery repeating its reset
  • background publication releasing a chat opened meanwhile
  • terminal pairing never starting sharing
  • a retry storm on one failing chat
  • a state round every interval on idle paired computers

The seventh was already handled: opening an older chat converts and publishes it, with no age limit. The manual now says so.

The first GitHub full check found four failures in touched packages, all fixed:

  • a removed computer re-pairing by link with --replace now gets its new device installed
  • two manual sentences were missing
  • two tui3 join-line tests assumed the longer joined line fits on one row

A second review confirmed all seven fixes and found three more, all fixed with reproducing TestR3_* tests (each fails on the code before the fix):

  • a state round refuses to upload saved keys sealed under a different key than it started with, so a switch mid-round never sends the new set's keys to the old set
  • replace recovery that stops before the destination is installed repeats the reset, so the saved-keys file always opens afterwards
  • a chat with nothing saved yet stays waiting instead of being marked shared

A third, narrow review of that last round confirmed the first two. On the third it noted that the background worker isn't woken by a waiting chat's first save. That save goes through the chat's own drive, which creates the relay record (Publisher.create), the same path every chat created after pairing uses. The worker does re-check waiting chats locally on each tick while some other chat keeps failing. That is local only, and left as is.

Tests (Linux, focused, serial, under an 8 GB memory cap)

  • go test ./internal/pair ./internal/identity ./internal/keys: 292 pass, 0 fail.
  • go test ./internal/tui3 -run 'Pair|FirstRun|Join|Link': 173 pass, 0 fail.
  • go test ./cmd/codeaf -run 'Pair|Devices|Device|Sync|Chats|Take|OlderChats|Join|Link|Replace|R1_|M[0-9]': 489 pass, 1 existing skip. The 1 failure is the take-back test listed below, unchanged from app/v3.
  • -run 'R3_' in internal/pair and internal/syncsetup: 8 pass, 0 fail.
  • go test ./internal/manual/...: 113 pass, 0 fail.
  • internal/syncsetup: all pairing-merge and review tests pass, including real two-home tests over a loopback relay. The 12 failures in the package are furrow-only tests that fail identically on 12dadd6 (checked with the changed files overlaid back to their base versions) and skip on CI.
  • Live check. The built binary ran with a loopback relay and two app processes driven over JSON lines, each home holding a real three-turn chat. B joined A by code. The only question was A's approval. After restart both homes' chats.list listed both chats, each own chat here. Continue here in each direction kept the history byte-identical.

CI

GitHub's PR gate and full check are compared against the app/v3 full check at the same base (12dadd6), by failing test names and by the file sites the law tests list (line numbers stripped):

  • No run of this branch adds a law-test site.
  • The PR gate fails only the five law tests app/v3 already fails.
  • The first run's four new full-check failures are fixed.
  • The second run's only failures outside the baseline are TestTheSourceHoldsEveryRowTheAppLists, a known fixture teardown race, and TestOneTaskReadDoesNotCopyTheWholeGraph, an allocation count in internal/session, which this change doesn't touch and which passed on the first run.

On the final head (a26ce90), the PR gate fails only the same five law tests, with no new sites. Every full-check attempt failed some test outside the baseline, a different one each time, all in packages this change does not touch or flaky on app/v3 too:

  • TestR26R27C30OpenLearnerIdleAndStateWake timed out at 31 s on the first attempt. It passed 6/6 locally (1.7–3.1 s), before and after the last fixes, and passed on the second attempt.
  • TestR7PersonalLeaderFallbackAndNoAutomaticRegain/sync-off failed on the second attempt. It fails 1 in 5 locally on both this head and unmodified 12dadd6, with the same message.
  • TestAppCrewProposalEndsInOneReceipt failed on the second attempt. It is in Inbox and crew-proposal code this change does not touch.
  • TestAWarmNoSendsBackToThePlanWithTheWordsAndAsksAgain and TestTheSourceHoldsEveryRowTheAppLists failed on the third attempt. The first also fails on two earlier app/v3 full checks, and the second is the known fixture teardown race.

Against a fresh app/v3 full check at the same base (12dadd6, run after the last push): the PR gate fails the same five law tests, the lock law lists the same sites, and the only full-check failure outside it is TestAWarmNoSendsBackToThePlanWithTheWordsAndAsksAgain in internal/factory/worker, which this change does not touch.

On the third attempt, no package this change touches (internal/pair, keys, identity, vaultsync, syncsetup, tui3) failed.

Existing failures, not touched here

  • TestHostedTakeBackReachesAcquireWithoutSharedUploadsOrReleaseDelay fails 3/3 on unmodified 12dadd6 when furrow is present ("made 1 acquires; want both") and skips on CI. Left as is.

Limits

  • A used computer that already shares another set is refused (M7) rather than moving its chats between sets.
  • Chats from before the current format follow the existing policy: chats active in the last 30 days are copied in the background, and older ones are copied when opened.

🤖 Generated with Claude Code

AbirAbbas and others added 8 commits October 29, 2026 10:42
…g them

A computer that already has chats and has never been paired now joins by code
or link without being asked to replace them. Its full local histories, saved
keys, memory and preferences are kept; its histories are published under the
shared keys with their original chat ids and holders. Both computers then list
both sets of chats, and Continue here moves a chat either way through the
existing app protocol and events.

The approved switch is recorded before keys are restaged, so a start after an
interruption finishes the merge with the same device. Background publication
retries failed uploads and reports status for unopened chats through the
existing sync.changed reason. A used computer that already shares another set
is refused without changing anything. Terminal --replace stays an explicit,
confirmed clean slate and now sets the old chat folders aside.

Validation: focused internal/pair, internal/identity, internal/syncsetup and
cmd/codeaf pairing tests, run serially under an 8 GB cap; one live check that
paired two homes with three-turn chats over a loopback relay through the app's
JSON lines, checked both inventories after restart, and compared byte-identical
histories after Continue here in both directions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The chat manual no longer tells a computer with chats of its own that pairing
replaces them. It explains that both computers keep their chats and see each
other's, that a computer already sharing another set is refused, and that
terminal --replace is the explicit clean slate. Adds the change entry.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A saved-key write that read the old key before pairing switched this computer
to the shared keys could replace the resealed file afterwards, leaving saved
keys that no longer open. Vault transactions and the pairing switch now take
the same per-home file lock, and a transaction re-reads the current key under
it. The lock's bookkeeping file does not make a home count as used.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- An interrupted --replace records its phases, so a later start never repeats
  the reset and never touches state saved after the switch.
- Background sharing and opening a chat take the same per-chat lock, so the
  other computer cannot continue a chat that was just opened here.
- Terminal pairing says sharing finishes the next time codeaf runs, and the
  next start does it.
- Only chats still waiting to be shared are retried, with pauses growing from
  5 seconds to a minute, outside the chat list's lock. Approving a pairing
  picks up chats saved after the startup scan.
- Keys, memory and preferences are synced only while a merge is unfinished; a
  merged, idle computer makes no new periodic relay requests.
- A computer that was removed and pairs again with --replace by link keeps the
  shared identity and gets its new device installed.
- The tui3 join-line tests compare rendered text with whitespace normalized,
  since the longer joined line wraps.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The manual states the new pairing lines word for word, that terminal pairing
finishes sharing the next time codeaf runs, and that an older chat appears on
the other computer once it is opened here.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s first save

- A state round refuses to upload a saved-keys envelope sealed under a key other
  than the one the round started with, so a switch mid-round never sends the new
  set's keys to the old set. The next round starts on the new binding.
- Replace recovery that stops before the destination is installed repeats the
  reset, so the saved-keys file always opens under the destination afterwards.
  Recovery after installation still never repeats it.
- A chat with nothing saved yet stays waiting without a backoff, and is shared as
  soon as its first save lands rather than at the next start.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@AbirAbbas
AbirAbbas marked this pull request as ready for review October 29, 2026 17:36
@AbirAbbas
AbirAbbas merged commit 1c4ec8a into app/v3 Oct 29, 2026
34 of 48 checks passed
@AbirAbbas
AbirAbbas deleted the feat/pair-merge branch October 29, 2026 19:24
santoshkumarradha added a commit that referenced this pull request Nov 3, 2026
…st (#2181)

* tests: R2 and R3 wait for the startup publication instead of chats.list

#2031 moved the startup pass that publishes existing chats out of chats.list
into a background loop, and TestR1_6InventoryDoesNotWaitForPublication pins
that chats.list never waits for it. R2 and R3 still called ListChats to join
that pass, so with the real engine they read the directory before anything
was published. They now wait for the startup pass, and R3's restart observes
one whole settled publication round before checking that nothing is released
again.

* docs: change entry for this pull request
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.

1 participant