Skip to content

Prompt uploads in remote (SSH) sessions: copy the checked file over ssh #837

Open

Description

@Randalix

The gap

  1. In a remote (SSH) session, paste, drop and Attach all fail with 400 Prompt uploads are not supported for remote (SSH) sessions. The refusal is right for the old code: workingDir is the remote path, so a file written on the Codeman host is one the agent on the other host can never read.
  2. That leaves no way to hand a screenshot, a PDF or a phone video to an agent running on another machine, which is most of what I use remote sessions for.

What I built (branch: https://github.com/Randalix/Codeman/tree/feat/remote-upload-upstream, one commit on current master). I'm asking before opening a PR.

  • The route receives and checks the upload exactly as it does for a local session (multipart limits, magic bytes, HEIC to JPEG, the document and ?kind=video paths, both rate buckets), but into a private staging folder under dataPath('upload-staging') instead of the workspace. Not os.tmpdir(): /tmp is a tmpfs on my host, and a 1GB video would sit in RAM for the whole copy.
  • remoteWriteUpload() in src/remote-files.ts then streams the staged file over the ssh child's stdin into the remote <workspace>/.codeman-uploads/, and the staging folder is removed in a finally. The response carries the remote path, so the client types a path the agent can open. No client change.
  • The ssh line is the existing buildRemoteFileCommand() (so buildSshConnectionArgs and remoteLoginShellLine), run under runWithRemoteSshLimit. Timeout is 30 s plus the size at 256 KiB/s, as for the edit-history reads.

The invariants I would want reviewed

  1. The remote script keeps the local folder rules in sh: a symlink or non-directory at .codeman-uploads is refused (403) and nothing is written through it, mkdir without -p, and the .gitignore of * is written only when nothing is there (noclobber).
  2. No partial file survives. A cut transfer ends cat with a clean EOF, so the bytes go to <name>.part and are renamed only when wc -c equals the size that was sent; otherwise the .part is removed and the route answers 502.
  3. An upload never wakes a host. A sleeping host gives 502 with the ssh reason.
  4. The name reaching the remote shell must match paste-[A-Za-z0-9._-]+, and a ~/ workspace resolves against the remote $HOME.

Not covered: the hourly GC still lists nothing for a remote session (uploadDirs()), so remote uploads are not aged out. I left that alone, since the sweep deletes and I didn't want to add a remote delete to this change.

Verification

  • New test/remote-upload-script.test.ts runs the generated script for real under a local sh, wrapped by remoteLoginShellLine as sshd's login shell gets it: binary bytes with NUL, quote and backslash, symlinked folder (nothing written), file in place of the folder, missing workspace, short transfer (neither file nor .part left), ~/ workspace, a workspace named it's a $(touch pwned) dir. The two "refuses remote" tests in test/routes/session-routes.test.ts are replaced by five that mock only remoteWriteUpload.
  • Targeted suites (7 files, 256 tests), tsc, eslint, prettier pass. I could not finish a full npm test on this box: my memory guard stopped it, and the only failures up to that point were three known environment ones (remote-edit-sources E2BIG, remote-pane-command-shell with SHELL unset, frontend-public-tooling without vendor assets).
  • Live against a Linux host over LAN: 3,000,000 random bytes in 0.59 s, SHA-256 identical on both ends, .gitignore created. Through the running server with curl into a real session: the .md landed in the remote .codeman-uploads/ and the staging folder was empty afterwards.

Most of the session-routes.ts diff is the handler body moving into an inner receiveUpload(), so git diff -w reads much smaller.

PR welcome?

Activity

  1. Ark0N commented on Nov 18, 2026

    @Ark0N
    Owner

    Thanks for asking before opening the PR, and for the detailed write-up.

    On current master, the refusal you hit is the session.remote guard in the paste-image route (src/web/routes/session-routes.ts). No PR or other issue covers remote uploads. Your branch is one commit on top of current master and touches seven files, none of them under src/web/public/, so the client really is unchanged.

    What is still open is whether Codeman should write to a remote host at all. Right now the docs treat that as out of scope on purpose:

    • CLAUDE.md, Remote sessions paragraph: "no writes over ssh"
    • the @fileoverview of src/remote-files.ts: "Read-only by design"
    • docs/remote-sessions.md: "Not available over ssh (by choice, not by accident)"
    • docs/file-viewer-edit-plan.md section 6 and docs/architecture-invariants.md (Remote SSH cases)

    Your branch updates the Prompt uploads paragraphs but leaves those as they are, so taking it means changing that rule. That decision hasn't been made, so I can't say yet whether a PR for this would be accepted.

    Either way, a review would look at these:

    1. remoteWriteUpload() runs inside runWithRemoteSshLimit(), a process-wide pool of 4 slots (CODEMAN_MAX_REMOTE_FILE_SSH) that every remote preview, probe and history read shares. The limiter leaves long-lived streams out of that pool on purpose. With your timeout (30 s plus the size at 256 KiB/s), a 1 GiB video (MAX_PASTE_VIDEO_BYTES) on a slow link can hold a slot for about 69 minutes, and four such uploads would leave every other remote file read on the server queued behind them.
    2. The staging folder is removed in a finally, but if the server restarts mid-upload, upload-staging/remote-* is left behind and nothing sweeps it at boot.
    3. The cleanup gap you mentioned is a bit wider than described: uploadDirs() in src/web/paste-image-gc.ts returns nothing for a remote session, and the session-delete cleanup uses the same function, so remote uploads are neither aged out nor removed when the session is closed.
  2. Randalix commented on Nov 23, 2026

    @Randalix
    ContributorAuthor

    I fixed your points 1 and 2 in f33753f, on top of the same branch (feat/remote-upload-upstream).

    1. The limiter is now an SshSlotPool class. The shared functions keep the default pool of 4, and uploads run in their own pool (runWithRemoteUploadLimit, 2 slots, CODEMAN_MAX_REMOTE_UPLOAD_SSH). A long upload can no longer hold a slot that previews, probes and history reads need, and 4 + 2 stays under OpenSSH's default of 10 concurrent unauthenticated connections.
    2. sweepRemoteUploadStaging() runs once at route registration and removes upload-staging/remote-* folders older than the call. An upload that starts in the meantime keeps its folder, and a symlink is removed as a link.

    Tests: 6 new in test/remote-upload-pool.test.ts. Live, a planted leftover folder was gone after a restart, and a .md upload to a session on an SSH host arrived with the right content and left the staging folder empty.

    Point 3 I've left alone. Aging out and deleting remote uploads means deleting files on the host over ssh, so it depends on the same decision about writes over ssh, and I'd rather wait for that than build a remote delete you might not want.

  3. Ark0N commented on Nov 23, 2026

    @Ark0N
    Owner

    Thanks for the follow-up.

    I went through f33753f on feat/remote-upload-upstream. Both fixes work the way you describe:

    1. remoteWriteUpload() now runs in runWithRemoteUploadLimit(), a separate SshSlotPool of 2 (CODEMAN_MAX_REMOTE_UPLOAD_SSH), while runWithRemoteSshLimit() keeps the shared pool of 4 (CODEMAN_MAX_REMOTE_FILE_SSH). A long copy no longer holds a slot that previews, probes and history reads wait on.
    2. sweepRemoteUploadStaging() is called from registerSessionRoutes(), which start() in src/web/server.ts runs before the server listens, so the sweep can't list the folder of an upload that is still running. It only touches remote-* entries older than the call, and a symlink is removed as a link.

    I also copied the remote script's steps from the diff and ran them under a local /bin/sh (dash). A full transfer lands next to the .gitignore, a short one leaves neither the file nor the .part (exit 6), a symlinked .codeman-uploads writes nothing (exit 4), and a missing workspace exits 5. I did not run your test files.

    The new pool still has one gap. runWithRemoteUploadLimit() waits for a slot with no bound (there is no maxWaitMs), and the 30 s plus size timer only starts once a slot is held. If both slots are busy with long video copies, a pasted screenshot is received and staged, then waits behind them, with its request open, for as long as they take.

    The two commits are now 62 behind master, and the combined diff still applies cleanly to current master.

    The policy question hasn't changed: should Codeman write to a remote host over ssh at all? f33753f leaves the texts that state the current rule as they are: "no writes over ssh" in CLAUDE.md, "Read-only by design" in the @fileoverview of src/remote-files.ts (which now sits above remoteWriteUpload()), and "Not available over ssh (by choice, not by accident)" in docs/remote-sessions.md. Your point 3 depends on the answer. If writes over ssh are allowed, there is a second question: can remote uploads land while uploadDirs() in src/web/paste-image-gc.ts still returns nothing for a remote session (so the files are never aged out and never removed when the session is closed), or does a remote delete have to come with them? Neither has been decided, so I still can't say whether a PR for this would be accepted.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions