Skip to content

feat(remote-peer): browse and save a conversation's files - #964

Merged
huichen merged 5 commits into
mainfrom
feat/remote-peer-files
Oct 10, 2026
Merged

huichen merged 5 commits into
mainfrom
feat/remote-peer-files

Conversation

@huichen

@huichen huichen commented Oct 10, 2026

Copy link
Copy Markdown
Collaborator

What this does

The client role could read a conversation on another machine and drive it, but not take a file off it. This is the whole path: browse a conversation's files, walk the host's directories, pull a file in chunks, verify it, and save it where the user chose.

The protocol is why this is not a thin wrapper

A chunk does not come back on the reply subject. A pull makes the host publish the chunk on xfer.down.{transfer}.chunk.{index} and then acknowledge — deliberately ordered so the bytes are observably on the wire first. So the two halves of one answer arrive by different routes and have to be joined, which the stream task does against an inbox keyed by (desktop, transfer, index). Keyed by all three because the same index appears in every transfer, and the subscription is taken at connect time because a subscription opened after the request would race the very publish it is waiting for.

NATS Core is at-most-once, so a chunk that never arrives looks exactly like one still in flight. Two consequences:

  • A pull is retried — but only on a transport fault. A host that answered and refused will refuse again, and three round trips to be told the same thing only delays the error.
  • Because a retry also covers a lost answer rather than a lost chunk, the finished file is checked against the size and SHA-256 the host declared. That check, not the retries, is what makes a lossy transport safe to move a file over.

The write is atomic: bytes go to a temporary neighbour and are renamed into place, and a failed rename removes the temporary. A partial file that looks complete is the one failure a download must not have.

The UI decisions that matter

  • A download always goes through a save dialog. Guessing a destination means writing somewhere the user did not choose, and a file that lands unexpectedly is indistinguishable from one that was never saved.
  • The name reported afterwards is the host's, because a preview variant is renamed on the host side. Reporting the requested name would send the user looking for a file that is not there.
  • Navigation uses the host's own paths and never joins one. The separator and case rules are the host's; "up" trims the path the host reported rather than rebuilding it, and stops at the session root — a wrong "up" walks out of the session, where every read is refused.
  • The browser is keyed on the conversation, so switching closes it rather than leaving one conversation's files under another's title. A draft has no conversation on its host and is offered none.

Tests

Rust: 22 transfer tests, including 8 that exercise the arms a healthy host never produces — forged bytes on the chunk subject, a non-numeric index, a chunk that never arrives, a request on a connection that died between the lookup and the send, a declaration that disagrees with what the host serves, and each write failure. Where the assertion is subtle I verified it by mutation: disabling the hash check, making the inbox key ignore the transfer id, and removing the failed-rename cleanup each make the corresponding test fail.

Two of those tests were originally passing vacuously and were fixed, not kept:

  1. two_downloads_pull_independently pulled sequentially, so no two waiters were ever registered together and a key that ignored the transfer id was never contradicted. It now pulls both in flight at once, and the mutation above proves it.
  2. A test for authenticated-but-misshaped pushes could not reach the parse at all (the injection needed the host's traffic key), so it passed either way. It is now a forged-bytes test, which the injection can deliver — proven by a control run — and which mutation-verifies the authentication step.

TS: tsc, full eslint (0 errors), stylelint, and 3469 tests pass — the same scope CI runs.

Coverage: 0 uncovered lines added, both sides — Rust via the documented llvm-cov procedure, TS via the repo's v8 method, each intersected with this branch's own diff.

The two Rust lines I could not reach honestly were deleted rather than hidden: a per-chunk length check that only a host lying about what it served could trigger (the whole-file size check subsumes it), and an in-flight guard whose button is disabled. Also fixed a ?-on-its-own-line attribution artifact by keeping that call to one line.

Not in this PR

Uploading. The host has the routes (upload_init/upload_complete/upload_cancel) and the same chunk framing, but the client has no send side — and a half-wired upload button is worse than none, since a failed upload loses the user's file rather than merely failing to fetch one.

Related

Follows #955–#961. The one desktop-client gap I flagged as remaining.

The client role could read a conversation and drive it, but not take a file
off the other machine. This is the transport half: prepare a transfer, pull it
in chunks, verify it, and write it where the caller chose.

The protocol is the phone's, and its shape is what the design has to answer
for. The bytes do not come back on the reply subject: a pull makes the host
publish the chunk on `xfer.down.{transfer}.chunk.{index}` and *then*
acknowledge. So the two halves of one answer arrive by different routes and
have to be joined, which the stream task does against an inbox keyed by
`(desktop, transfer, index)` — keyed by all three because the same index
appears in every transfer, and the subscription is taken at connect time
because the host publishes before it acknowledges (a subscription opened after
the request would race the publish it is waiting for).

NATS Core is at-most-once, so a chunk that never arrives looks exactly like one
still in flight. A pull is therefore retried, but only on a *transport* fault:
a host that answered and refused will refuse again, and three round trips to be
told the same thing only delays the error. Because a retry can also mean a lost
answer rather than a lost chunk, the finished file is checked against the size
and SHA-256 the host declared rather than merely assembled — that check, not
the retries, is what makes a lossy transport safe to move a file over.

The write is atomic: bytes go to a temporary neighbour and are renamed into
place, so a partial download never appears to be a complete one.

The host's policy (which files a paired client may ask for) is unchanged and
untouched; this adds the client's half only.
The pull path's arms are the ones a healthy host never produces, and each of
them is a way a file can be wrong rather than merely missing: forged bytes
arriving on the chunk subject (substituted content), a chunk that never arrives
(a download that waits forever), a request on a connection that died between
the lookup and the send, and a declaration that disagrees with what the host
serves. Each now has a test that fails when its arm is removed.

Three things came out of that work rather than being planned:

* The chunk subscription is narrowed to `xfer.down.*.chunk.*`. The trailing
  wildcard was what made the receive path's parse total — with it, a delivery
  that is not a chunk cannot arrive, so there is no "what else could this be"
  to answer.
* The subject is parsed *before* decrypting. A malformed subject is now dropped
  without spending a decryption (and the traffic-key lock) on it, so a relay
  that spams the transfer prefix cannot make this client work to discard the
  messages.
* The transport-failure recovery is shared with the session-keyed lane. Both
  talk to the same socket, so both must leave the same state behind when it
  dies; a second copy was a second place for the recovery to drift.

The chunk-pull timeout is milliseconds under test and seconds in production,
the same scaling `delay_unit` uses: an arm that exists to catch a host which
acknowledged a publish that never arrived cannot cost the suite 15 s per
attempt.
The transport landed first; this is the surface over it. A conversation that
lives on another machine can now have its files listed, walked, and saved
locally.

Navigation is expressed in the host's own paths and the client never joins one
itself: the separator and the case rules are the host's, not this machine's, so
"up" trims the path the host reported rather than rebuilding it. The current
directory is shown for the same reason — the user is looking at someone else's
filesystem, and a path that is not the host's would be a lie about where they
are.

A download always goes through a save dialog. Guessing a destination would mean
writing somewhere the user did not choose, on a machine whose conventions we are
guessing at, and a file that lands unexpectedly is indistinguishable from one
that was never saved. The name reported afterwards is the one the *host* chose,
because a preview variant is renamed on its side and reporting the requested
name would send the user looking for a file that is not there.

The path arithmetic is its own module rather than part of the dialog: it is the
part that can be wrong in a way the UI hides — a wrong "up" walks the user out
of the session, where every read is refused — so it is unit-tested directly, and
a module exporting it beside a component would break fast refresh.

The browser is keyed on the open conversation, so switching to another one
closes it rather than leaving one conversation's files on screen under another's
title. A draft has no conversation on its host and so is offered no browser.
@huichen
huichen enabled auto-merge (squash) October 10, 2026 02:54
Linux CI caught this: a multi-chunk download failed with `peer_not_connected`.

The byte-wait budget under test (50 ms, scaled so an arm that waits for a chunk
that never arrives does not cost the suite 15 s) was also being passed as the
*request* budget. That request makes the host read the chunk off disk, publish
654 KiB and flush — so on a loaded runner it exceeded 50 ms, the transport layer
read the command timeout as a dead socket, and the connection was dropped. The
retry then reported `peer_not_connected`, i.e. a slow disk looked exactly like a
network fault.

The two budgets are different things and are now separate: the request keeps the
session command timeout, and only the wait for bytes uses the shortened one. The
host publishes before it acknowledges, so by the time the request returns the
bytes have already been sent.

Also: a retry that hits a dropped connection now reports the *first* transport
failure rather than the last one. "Not connected" is the symptom; the time out
that caused it is the cause, and reporting the symptom sends a support session
looking at pairing when the fault was the network.
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