Page:
ADR 0006 decouple gateway orchestration into pipeline
Pages
0001 deepen classification safety auditor
0002 deepen rules registry
0003 prune legacy script shims
0004 decouple rfc codecs from mailbox gateway
0005 collapse workflow into mailbox pipeline
0006 decouple gateway orchestration into pipeline
0007 incremental scan cache and progress feedback
0008 port mailsweep to synchronous rust
0009 retire python baseline and complete rust cutover
0010 automated multi platform forgejo release workflow
ADR 0001 deepen classification safety auditor
ADR 0002 deepen rules registry
ADR 0003 prune legacy script shims
ADR 0004 decouple rfc codecs from mailbox gateway
ADR 0005 collapse workflow into mailbox pipeline
ADR 0006 decouple gateway orchestration into pipeline
ADR 0007 incremental scan cache and progress feedback
ADR 0008 port mailsweep to synchronous rust
ADR 0009 retire python baseline and complete rust cutover
ADR 0010 automated multi platform forgejo release workflow
ADRs
CLI Reference
Domain Context
FAQ
Home
Internals
Python API
Rules Schema
Rust API
No results
1
ADR 0006 decouple gateway orchestration into pipeline
MailSweep Doc Bot edited this page 2026-08-24 02:23:29 +00:00
Table of Contents
6. Decouple Gateway Orchestration into Mailbox Pipeline
Date: 2026-08-24
Status
Accepted
Context
MailboxGateway manages IMAP connection lifecycles, protocol negotiation (MOVE, UIDPLUS), folder encoding, and atomic message operations.
Previously, MailboxGateway also acted as a domain-level orchestrator by exposing backup(), execute_cleanup(), and restore_archive(). This caused several architectural issues:
MailboxGatewaycontained circular import workarounds (TYPE_CHECKINGblocks and inside-function lazy imports ofBackupArchiveandCleanupPlan).- Domain operations were duplicated across
MailboxGatewayandMailboxPipeline, creating redundant pass-through delegation layers. MailboxGatewayexpanded to over 1,000 lines with mixed responsibilities (low-level IMAP network protocol handling vs high-level file archive packaging and plan resolution).
Decision
We cleanly decoupled all high-level domain orchestration from MailboxGateway and concentrated it in MailboxPipeline:
-
MailboxGateway(src/mailsweep/gateway.py):- Stripped of
backup(),execute_cleanup(),restore_archive(), and all lazyTYPE_CHECKINGcircular import workarounds. - Constrained to core IMAP protocol operations: folder selection, header scanning (
fetch_headers), raw message streaming (fetch_raw_messages), and atomic mutations (trash_messages,move_messages,mark_read,append_message,find_trash_folder).
- Stripped of
-
MailboxPipeline(src/mailsweep/pipeline.py):pipeline.backup(...): Directly coordinatesBackupArchive.create(...)by providing a message fetcher stream adapter connected togateway.fetch_raw_messages.pipeline.clean(...): EvaluatesCleanupPlan.by_folder(), validates UIDVALIDITY snapshot invariants, and executes batch mutations viagateway.trash_messages(),gateway.move_messages(), andgateway.mark_read().pipeline.restore(...): Manages the complete restore lifecycle fromBackupArchive, enforcing automatic SHA-256 integrity verification before appending raw messages to the server.
-
CLI& Tests (src/mailsweep/cli.py,test_mailsweep.py):- Standardized all CLI restore execution (
cmd_restore) throughMailboxPipeline.restore(...). - Updated integration test fixtures to verify lifecycle workflows through
MailboxPipeline.
- Standardized all CLI restore execution (
Consequences
MailboxGatewayshrinks and focuses entirely on the IMAP protocol, eliminating circular import dependencies.MailboxPipelineis the single canonical coordinator for all end-to-end mailbox operations (scan, plan, audit, backup, clean, restore, sweep).- Archive creation and restore operations are 100% testable in-memory with fake transport adapters.
- All 75 tests execute in ~0.35s with zero disk or network I/O.