Skip to content

#2036: a run cut off after the server made a folder no longer leaves it here twice - #2251

Merged
benandez merged 1 commit into
UCSLabs:mainfrom
easier-digital:fix/2036-listed-folder-waiting
Dec 3, 2026
Merged

benandez merged 1 commit into
UCSLabs:mainfrom
easier-digital:fix/2036-listed-folder-waiting

Conversation

@inluck

@inluck inluck commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Part of #2036: the main case. The "Related" case in the issue (a mailbox the server never lists, so CREATE answers that it exists) is left open.

What changes for the person using MailCraft

Moving mail into a folder made only on this computer makes that folder on the IMAP server. Suppose the Send/Receive that made it was cut off before its answer was kept (the program closed). The next Send/Receive then made a second folder for the server's mailbox ("Acme (2)", or "Clients.Acme" at the top as reported) and brought the mail down into it. The mail was then here twice. The folder it had been moved into stayed waiting: its mail never got a server copy of its own, and anything moved there afterwards failed at every run with "it has another folder of that name".

Now the next Send/Receive ties the waiting folder to the mailbox the cut-off run made. The mail is here once, and what is moved there afterwards goes up.

A folder that an earlier version already made twice stays as it is. Putting two folders back together is a decision of its own, and the release note says so.

Changes a default: no

How it works

A run hands over the folder list as a piece of its own before it pushes anything (crates/net/src/sync.rs), and the app takes that piece in first. adopt_listed gave every listed mailbox without a folder here a new folder. When the push then adopted the mailbox for the waiting folder, the engine refused its created, because another folder followed the mailbox by then.

The piece with the list now also says, as created, which folders here wait for a mailbox it lists. That is push::already_made, in crates/net/src/push.rs:

  • Which folders: those that the moves and places of the request go into, and that are ordinary folders made here.
  • How their mailbox is worked out: exactly as the push works it out, with name_below and already_there and the personal namespace from NAMESPACE (cPanel's and Courier's INBOX.). Nothing is asked of the server.
  • Why the order works: take_in_news takes created in before listed, so the waiting folder follows the mailbox before the list could give it a new folder.
  • No second report: what was found goes to the push as its destinations for those folders (AlreadyMade). The push then neither works them out again nor reports created a second time.
  • When it stands back: a mailbox that two waiting folders would both be made as is left to the push, which says so, as now.

A server may also list the levels above such a mailbox as mailboxes of their own (RFC 3501, 6.3.3 says it should make them). A large import hits this without any run being cut off: a run carries at most MAX_PLACES messages, so a parent's own mail can still be waiting when the level is listed. adopt_listed now gives such a level to the parent made here that it stands for, instead of a new folder (above, in crates/engine/src/cmd/account.rs). It does so only where the two trees agree all the way up:

  • Names: each level's last part is the parent's name as the push makes a level of it (made_level, mirroring name_below), using the delimiter the list itself shows between the level and the one above.
  • Parents: each parent here is an ordinary folder of the account that follows no mailbox and holds no mail except what is waiting to go up.
  • Where it stops: at a level that the parent's own parent here follows, or at the top of both trees.

Folder moves here aren't made on the server, so the trees can differ. Then nothing is tied, and a level two parents would stand for is tied to neither. Nothing about the server's delimiter or prefix is guessed.

This went through two rounds of adversarial review before I opened it:

  1. The first version matched paths in the engine. It missed INBOX. servers and names that hold the delimiter, and could tie a folder called "shared" to the level that holds other users' folders. Working the mailboxes out where the push works them out (already_made) avoids all three.
  2. The first version of the parent rule checked only where folders sit in the tree. It could tie an empty folder that had been moved here to an unrelated level, and it missed the large-import case. The name and line-up checks above, and counting only mail that isn't waiting to go up, came from that review.

Not covered:

  • Folders that an earlier version already made twice (the seven reported here) stay as they are: the folder the mail was moved into, and the second folder that follows the server's mailbox. Putting them back together means merging two folders' mail, which seemed a decision for you. If you'd like it, I can propose how in a follow-up.
  • The "Related" case above, a mailbox the server never lists: a follow-up pull request, as its cause is CREATE's answer, not the order of the pieces.
  • A level above, on a server with a personal prefix, where the parent made here is at the top (local "Clients" for INBOX.Clients): the trees don't line up, as the engine isn't told the prefix, so that level still gets a new folder. The waiting folder below it is tied all the same.
  • Two waiting folders the server would make as one mailbox (v1.0 and v1_0 on a . server) are left to the push, which says so, as now.
  • Mail beyond a run's request is covered by the parent rule for levels above. A folder whose own mailbox a cut-off run made has its mail in the next run's request first.

How it was checked

  • New apps/mailcraft-cli/tests/folders_a_lost_run_made.rs, 14 tests against the fake IMAP server. Each run is taken in pieces, as the app does (sync_with + apply_sync_part), mostly after a run cut off once the server made the folder:

    • a folder under the Inbox: one folder follows the mailbox, the message is here once with its UID, and a message moved there afterwards goes up, with no problem said;
    • "Clients" and "Acme" under it, both made here: "Acme" is tied although the server doesn't list INBOX/Clients, and no folder is made beside it;
    • as reported: folders at the top of a server that parts levels with . (Clients.Acme);
    • a folder at the top under a personal prefix (INBOX.Acme);
    • a name holding the delimiter (J. Smith, made J_ Smith);
    • mail moved within the account (Pending::Move), not only placed from elsewhere;
    • a server that also lists the level above (INBOX/Clients): "Clients" is tied, and mail moved into it afterwards goes up;
    • two levels above (INBOX/Clients, INBOX/Clients/Sub) are tied all the way up;
    • no run cut off, a 401-message import into "Acme" and one message into "Clients", with the level listed before "Clients"' own mail goes up: tied, no new folder, everything ends up on the server;
    • must not tie: the owner's folder "shared" and the level holding others' shared folders; a parent with mail of its own; a flat folder moved under a new, empty folder ("Projects" is not Work); a child moved under a new parent ("Old" is not Clients);
    • taken in whole, the same run ties the same folder (a guard for that path).
  • On main, 10 of them fail. The 4 guards pass both ways (the "shared", "Projects", "Old" and whole-run tests).

  • The existing moves_between_accounts end-to-end tests and all 1,038 mailcraft-net tests pass.

  • macOS 26.6 (Apple silicon), Rust 1.98.1: cargo xtask ci passes all 15 steps with #2189: clippy passes on macOS: pin_for is used on Linux and FreeBSD alone #2190's pin_for fix applied locally. Without it, clippy stops on macOS (cargo xtask ci fails at clippy on macOS: pin_for is never used #2189). This branch doesn't include that fix.

  • Not built on Windows or Linux.

  • cargo xtask ci passes

  • A crash or data-loss fix has its regression test

  • "Allow edits by maintainers": the fork belongs to an organisation, where GitHub doesn't offer that box. If you'd like to push to this branch, say so and you'll have write access to easier-digital/mailcraft.

  • I have signed the CLA, or will by commenting on this pull request

Human-reviewed, AI-assisted, nobody's mailbox harmed.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed an issue where an interrupted Send/Receive could leave a locally created folder duplicated on the mail server. On the next sync, the local folder is linked to the server copy, keeping its mail together and allowing later moves to upload normally.
    • Duplicate folders created by earlier versions are not changed automatically.

…leaves it here twice

A run hands over the folder list as a piece of its own before its push,
and the app takes that piece in first. After a run that made a mailbox
for a folder made here and was cut off before its answer was kept,
adopt_listed gave that mailbox a new folder of its own: the server's
copies of the mail came down into it (here twice), and the push's
`created` for the waiting folder was refused, so that folder failed at
every run with "it has another folder of that name".

The piece with the list now also says, as `created`, which folders here
wait for a mailbox it lists (push::already_made): the moves and places of
the request into ordinary folders made here, worked out as the push does
(name_below, already_there, the personal namespace), nothing asked of the
server; the push takes them as its destinations and doesn't say them
again. The engine takes `created` in before the list, so the waiting
folder follows the mailbox and no new folder is made for it.

A server may also list the levels above such a mailbox as mailboxes of
their own (RFC 3501, 6.3.3). adopt_listed gives such a level to the
parent made here it stands for, only where the trees agree all the way
up: each level's last part is the parent's name as the push makes it, by
the delimiter the list shows; each parent follows nothing and holds no
mail but what waits to go up; the walk ends at a level the parent's own
parent follows, or at the top of both. Folder moves here aren't made on
the server, so the trees can differ: then nothing is tied, and a level
two parents would stand for is tied to neither.

Human-reviewed, AI-assisted, nobody's mailbox harmed.
@inluck
inluck requested a review from benandez as a code owner December 2, 2026 00:53
@coderabbitai

coderabbitai Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The sync flow now detects when a listed IMAP mailbox matches a locally pending folder creation. It reuses the matching local folder, avoids creating a duplicate, and adds integration tests for repeated and whole-run syncs and varied mailbox paths.

Changes: Folder reconciliation

Layer / File(s) Summary
Detect existing server mailboxes
crates/net/src/push.rs, crates/net/src/sync.rs
exchange retains the personal namespace and calls already_made before pushing. push receives the detected folders and their cached destinations.
Adopt matching local folders
crates/engine/src/cmd/account.rs, apps/mailcraft-cli/tests/folders_a_lost_run_made.rs, packaging/release-notes.d/fixes/2036.md
adopt_listed matches eligible local folders to listed mailbox levels and reuses unique matches. Integration tests cover nested paths, delimiters, namespaces, message placement, and ambiguous matches. The release note describes the interrupted-sync case.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix · Severity of issue fixed: Low

Possibly related issues

  • Issue 2036 describes a locally created folder that remains unlinked after an interrupted sync. This change addresses the case where that folder’s mailbox is listed by the server. The issue’s unlisted-folder case remains outside this change.

Suggested reviewers: kartas39, linuxrrze

Merge Risk: Minimal · up to 7d2ed · assessed changes only

Path filters excluded changed files. This rating applies only to the assessed changes.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names issue #2036 and states the user-visible outcome: a run cut off after the server created a folder no longer leaves the folder duplicated locally. The changeset does this: it ties the wa…
Description check Passed The description follows the template well. It has a 'What changes for the person using MailCraft' section that explains the user-facing effect, and a 'How it was checked' section that lists the new re…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names issue #2036 and states the user-visible outcome: a run cut off after the server created a folder no longer leaves the folder duplicated locally. The changeset does this: it ties the waiting local folder to the server mailbox the interrupted run made (push::already_made, adopt_listed changes). The title is specific and matches the main change.

Full details: Description check

Explanation

The description follows the template well. It has a 'What changes for the person using MailCraft' section that explains the user-facing effect, and a 'How it was checked' section that lists the new regression tests, the CI result, and the platforms tested. The 'Closes #' line is not filled in, but the description says 'Part of #2036' and explains why the issue stays open, so the missing closing reference is explained. The checklist is present, and the maintainer-edits box is left unticked with a stated reason. The description also has one item that is not in the template: the 'Changes a default: no' line. The 'Related' case and the other follow-ups are stated as out of scope.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • 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

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 0afdca8 and 7d2ed42.

Files ignored due to path filters: 1

  • docs/log.d/ROADMAP/progress/2036.md is excluded by !docs/**
Files selected for processing: 5

  • apps/mailcraft-cli/tests/folders_a_lost_run_made.rs
  • crates/engine/src/cmd/account.rs
  • crates/net/src/push.rs
  • crates/net/src/sync.rs
  • packaging/release-notes.d/fixes/2036.md

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@sonarqubecloud

sonarqubecloud Bot commented Dec 2, 2026

Copy link
Copy Markdown

@vangel76 vangel76 left a comment

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.

Checked IMAP path and namespace calculation, LIST-first and whole-result apply, account folder roles, pending moves and placements, parent adoption, and the synthetic recovery tests against current main.

Source review only. I did not build the project, run tests, or check CI.

Comment thread crates/net/src/sync.rs
// And, in the same piece, the folders here that wait for a mailbox it lists (made by a run
// whose answer was never kept, #2036): taken in before the list, so that the list doesn't
// give that mailbox a new folder of its own.
let made = crate::push::already_made(request, &mailboxes, personal.as_ref());

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.

P2. Preserve the server's standard folder when adopting a listed destination

If a local IMAP folder called Spam has a message waiting to upload and the server LIST identifies Spam as the account's Junk mailbox, already_made sees the raw name and claims it for the custom folder before adopt_listed runs. The normal LIST path would link the account's Junk Email folder to that mailbox. Instead, the existing-remote branch leaves Spam custom and Junk Email unlinked, so the server's spam appears in an ordinary folder and future listings keep finding the name already claimed. Use the classified LIST plan for early adoption and leave server-designated standard folders to the standard-folder mapping. Add a piece-by-piece test with a pending placement into a custom Spam and a listed Spam carrying \Junk.

@benandez benandez left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Approve. Two independent reads found no path to mail loss, duplicate mail or a folder tied to the wrong mailbox. Ties are by name, only onto folders that follow nothing; the engine's free check still guards created; the parent rule matches level by level and stops at MAX_LEVELS; and created is taken in before listed, so a run cut off after the listing piece leaves a correct tie.

Non-blocking:

  • push.rs:147-148 and the PR body say a mailbox two waiting folders would both be made as "is left to the push, which says so", but already_there only checks the request's own snapshot, so the push says nothing unless its own CREATE fails. Fix the comment, or add a check.
  • A same-named mailbox another program made meanwhile is now adopted (mail goes both ways, nothing lost). That matches the push's stated intent, but a test pinning it would help.
  • already_made builds its own Push literal; a shared Push::new would keep it from drifting from push().

@benandez
benandez added this pull request to the merge queue Dec 3, 2026
Merged via the queue into UCSLabs:main with commit 5f15664 Dec 3, 2026
20 checks passed
bnjgoe pushed a commit to bnjgoe/mailcraft that referenced this pull request Dec 6, 2026
…tays the Junk folder's

After UCSLabs#2251's review. A mailbox that two waiting folders would both be
made as is the first one's, in the order the push takes them, and the
push tells the other. A mailbox the server flags for a standard job is
the standard folder's, unless it holds the waiting folder's own mail,
put there by a run that was cut off: a move into a folder that only
exists here now carries the message's Message-ID, so it can be looked
for there. One Push::new for push() and already_made.

Human-reviewed, AI-assisted, nobody's mailbox harmed.
bnjgoe pushed a commit to bnjgoe/mailcraft that referenced this pull request Dec 6, 2026
UCSLabs#2036: one waiting folder per mailbox, and the server's junk stays the Junk folder's (UCSLabs#2251 follow-ups)
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.

3 participants