Skip to content

feat(uploads): delete removed uploads from the host even across dropped connections - #382

Merged
andrebrait merged 5 commits into
ompweb-dev:mainfrom
andrebrait:feat/durable-upload-cancel
Nov 3, 2026
Merged

andrebrait merged 5 commits into
ompweb-dev:mainfrom
andrebrait:feat/durable-upload-cancel

Conversation

@andrebrait

Copy link
Copy Markdown
Collaborator

Follow-up to #372. When you remove an upload chip, or discard a finished upload, the host now deletes its staged copy even if the connection was down at the time. Before, the cancel was lost whenever the browser was disconnected, the host was offline, or the server no longer knew the upload, and the partial file (up to 2 GiB) stayed on the host until a later upload or a daemon restart swept it after a day.

What changes

Browser (tab and worker)

  • The tab always hands the cancel to the worker as {op: "up_abort", id, host}. Only a tab without a worker sends a DELETE itself.
  • The worker keeps each cancel until the server has it (at most 256):
    • WebSocket mode: an up_abort line. If the link is down, the worker dials for it. The line is kept until the server acks it and is sent again after a resync.
    • SSE mode, or on a switch to it: DELETE /api/uploads/<id>?host=<host>, with a 40 s deadline. A 2xx, or a 4xx other than 408 and 429, settles it. Anything else is retried after 5 s, 15 s, then every 60 s, and at once when the network comes back or the tab becomes visible.

Server

  • Host offline: an uploadAbort the host could not take (503 host_offline, 504 host_timeout) is kept for that host, at most 1000 per host, and sent again on its next hello.
  • Retry of the same id: a new uploadStart of that id drops its pending abort, so a Retry can't be deleted by the abort its own failure left behind.
  • Upload no longer registered (the server restarted, or forgot an idle or failed upload): the abort goes to the host the browser names, if that host is known. Another owner's registered upload is never aborted.

Daemon

  • Expired staged files (older than 24 h) are swept every hour, not only at start and on uploadStart. This is the backstop for the cases above that are kept in memory only.

Verification

  • npm run typecheck, npm run lint (0 errors), npm test (2492 pass, 0 fail) and npm run build pass.
  • Each new test failed before its change. For the review fixes, I also broke each fix on purpose and confirmed its test then fails:
    • Server: the pending abort is replayed on the next hello and delivered once; the per-host cap; no pending entry for an unknown host; a Retry's own pending abort is dropped; DELETE ?host= on an upload the server forgot; up_abort {host} sent through the browser link.
    • Worker: dialing for a cancel; resending after a resync; switching to SSE; the HTTP retry schedule and the wake; the 40 s deadline on a hanging DELETE; 408 and 429 retried; the host fallback when a tab closes.
    • Daemon: the hourly sweep deletes an expired file on an idle host.
  • Real smoke (headless Chromium through a TCP proxy I could kill, plus a real server and daemon, 400 MB file):
    • Browser disconnected: with the proxy killed, I removed the chip. It disappeared at once and the staged file stayed. After I restarted the proxy, the file was deleted within 60 s.
    • Host offline: with the daemon killed, I removed the chip and the file stayed. After the daemon relinked, it was deleted. The daemon's start-up sweep only deletes files older than 24 h, so the replayed abort is what deleted it.
  • One adversarial review round. Its four should-fix findings and the test gaps it found are fixed in the second commit.

Known limit

Pending cancels live in memory. A cancel is lost if the browser closes before delivering it, or if the server restarts while holding one for an offline host. The hourly sweep then deletes the file once it is 24 h old.

Not tested: Windows, and a real remote host.

@coderabbitai

coderabbitai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 27 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 7c82086a-c9c4-4d09-a516-4043d69c8b4f
Commits

Reviewing files that changed from the base of the PR and between a1e7fef and 6f8fde7.

Files selected for processing: 19
  • AGENTS.md
  • CHANGELOG.md
  • app/api/uploads/[id]/route.ts
  • components/ChatInput.tsx
  • components/ChatInput.uploads.test.mjs
  • docs/specs/2026-11-02-file-uploads-design.md
  • graphify-out/graph.json
  • lib/browser-link.test.mjs
  • lib/browser-link.ts
  • lib/event-stream-worker.test.mjs
  • lib/event-stream-worker.ts
  • lib/event-stream.test.mjs
  • lib/event-stream.ts
  • lib/hosts/daemon.test.mjs
  • lib/hosts/daemon.ts
  • lib/hosts/uploads.test.mjs
  • lib/hosts/uploads.ts
  • lib/uploads.test.mjs
  • lib/uploads.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.

@kilo-code-bot

kilo-code-bot Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Code Review Roast 🔥

Verdict: No Issues Found | Recommendation: Merge

Oh wait, this PR is actually clean. I need to sit down. I had my flamethrower warmed up and everything.

📊 Overall: Like finding a unicorn in production — I didn't think clean PRs existed anymore, but here we are. This is a one-method fix with its own regression test, and both sides check out.

The incremental diff's production change is lib/hosts/uploads.ts: StagedUploads.sweep() now catches a failed abort of an idle upload (e.g. EPERM, or a .part path replaced by a directory), logs it, and continues — so it never rejects, neither from the hourly timer nor before uploadStart, which sweeps first and would otherwise fail every new upload. The upload is already closed, the per-id control entry is drained by control()'s finally, and the file scan retries the remaining files on every sweep. The new test in lib/hosts/uploads.test.mjs drives exactly the directory case and asserts sweep resolves, the other expired files still go, and a new upload can start. AGENTS.md documents the same behavior, and generated graphify-out/graph.json is skipped.

Files Reviewed (3 files)
  • lib/hosts/uploads.ts
  • lib/hosts/uploads.test.mjs
  • AGENTS.md
Previous Review Summaries (3 snapshots, latest commit ed8b649)

Current summary above is authoritative. Previous snapshots are kept for context only.

Previous review (commit ed8b649)

Verdict: No Issues Found | Recommendation: Merge

Oh wait, this PR is actually clean. I need to sit down. I had my flamethrower warmed up and everything.

📊 Overall: Like finding a unicorn in production — I didn't think clean PRs existed anymore, but here we are.

The four prior adversarial findings (retained-worker abort replay on Retry, in-flight host abort re-parking, SSE retry not notifying the worker, and outstanding HTTP deletions clobbering a newer attempt) are all marked fixed by the author on lib/event-stream-worker.ts and lib/uploads.ts, neither of which changed in this incremental diff, so they're dropped.

This diff's only production change is components/ChatInput.tsx: on a 410 bfcache failure it hands the chip a fresh newUploadId() so a pending old-id cancel (worker up_abort, in-flight HTTP DELETE, or host abort) can never reach the Retry's new upload. The import (UploadFailedError, exported at lib/event-stream.ts:306), the object-spread ternary (only evaluates newUploadId() on the 410 branch), and settle→patchUpload (resolves against the captured original id) all check out. The new regression test and the update-worker test-infra hardening look correct too, and generated graphify-out/graph.json is skipped.

Files Reviewed (6 files)
  • AGENTS.md
  • components/ChatInput.tsx
  • components/ChatInput.uploads.test.mjs
  • docs/specs/2026-11-02-file-uploads-design.md
  • lib/hosts/update-fixture.test-helper.mjs
  • lib/hosts/update-worker.test.mjs

Previous review (commit c06b24f)

Verdict: No Issues Found | Recommendation: Merge

Oh wait, this PR is actually clean. I need to sit down. I had my flamethrower warmed up and everything.

📊 Overall: Like finding a unicorn in production — I didn't think clean PRs existed anymore, but here we are.

Files Reviewed (7 files)
  • AGENTS.md
  • docs/specs/2026-11-02-file-uploads-design.md
  • graphify-out/graph.json
  • lib/event-stream-worker.test.mjs
  • lib/event-stream-worker.ts
  • lib/uploads.test.mjs
  • lib/uploads.ts

Previous review (commit b03db90)

Verdict: No Issues Found | Recommendation: Merge

Oh wait, this PR is actually clean. I need to sit down. I had my flamethrower warmed up and everything.

📊 Overall: Like finding a unicorn in production — I didn't think clean PRs existed anymore, but here we are.

Files Reviewed (3 files)
  • lib/uploads.ts
  • app/api/uploads/[id]/route.ts
  • lib/event-stream-worker.ts

Reviewed by free · Input: 79.3K · Output: 18.8K · Cached: 186.4K

@andrebrait

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-03T10:54:12.923931Z 6f8fde7 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b03db90208

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread lib/event-stream-worker.ts Resolved
Comment thread lib/uploads.ts Resolved
@andrebrait
andrebrait force-pushed the feat/durable-upload-cancel branch from b03db90 to c06b24f Compare November 3, 2026 08:32
@andrebrait

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c06b24f854

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread lib/event-stream-worker.ts Resolved
Comment thread lib/event-stream-worker.ts Resolved
Removing an upload chip, or discarding a finished upload, now deletes the host's
staged copy once the browser, server and host can reach each other again:
- the worker keeps each abort until the server has it (link line until acked and
  resent on a resync; in SSE mode a DELETE retried with back-off and on wake);
- main keeps an uploadAbort its host could not take and resends it on that host's
  next hello, and aborts ids it no longer registers on the host the browser names;
- the daemon sweeps expired staged files every hour, not only at start and on
  uploadStart.
…ng abort, cover the new paths

Review: a hanging DELETE blocked every retry (40 s deadline now; 408/429 retry);
a parked uploadAbort replayed on a host's next hello could delete a Retry of the
same id (a new uploadStart drops it); the route, browser-link host pass-through,
the per-host cap and the unknown-host guard had no tests. Also drops a redundant
resend loop and fixes an AGENTS.md mis-splice.
… layer

Codex review: a cancel still pending in the worker was resent after a resync and
deleted a completed Retry of the same id; on main, an uploadAbort still in flight
when the Retry started was parked again when it timed out and replayed on the
next hello. The worker now drops its pending cancel when the id is uploaded again,
and main does not park an abort whose id was registered again meanwhile.
… upload id

Codex review: the only uploads that are both cancelled and retried are the ones a
bfcache pagehide made the worker cancel (410). Retrying them under the same id
raced that cancel on every path: an SSE Retry bypasses the worker, and an HTTP
DELETE already in flight could land after the Retry finished. Their host copy is
deleted anyway, so the chip now takes a new id on that failure and its Retry
starts over; the old id's cancel can no longer reach it.
@andrebrait
andrebrait force-pushed the feat/durable-upload-cancel branch from b8a9a0b to ed8b649 Compare November 3, 2026 09:04
@andrebrait

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ed8b649236

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread lib/hosts/daemon.ts Resolved
…he host

Codex review: StagedUploads.sweep() rejected when an idle upload's files could
not be removed (a .part replaced by a directory, EPERM). From the new hourly
timer that is an unhandled rejection that exits the daemon; before uploadStart it
failed every new upload. The sweep now logs that failure and goes on.
@andrebrait

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 6f8fde7a34

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@andrebrait
andrebrait merged commit 2ca0bca into ompweb-dev:main Nov 3, 2026
5 checks passed
@andrebrait
andrebrait deleted the feat/durable-upload-cancel branch November 3, 2026 13:00
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