Skip to content

Mail scope: accept optional Account/Mailbox entries in mail_mailboxes - #86

Merged
barelyworkingcode merged 5 commits into
mainfrom
mail-scope-account-mailbox
Nov 27, 2026
Merged

barelyworkingcode merged 5 commits into
mainfrom
mail-scope-account-mailbox

Conversation

@barelyworkingcode

Copy link
Copy Markdown
Owner

Closes #85.

Summary

  • Allow mail_mailboxes entries of the form Account/Mailbox (e.g. Work/INBOX, Work/[Gmail]/All Mail) to match only that account's mailbox rather than forming a cross-product across all allowed mail_accounts.
  • Plain entries keep their existing cross-product meaning across all allowed accounts, and * stays the only wildcard.
  • Applied consistently at every mail scope seam: MailScope.mailboxTargets, MailService.scopeRefusal, draftDestinationRefusal, scanScriptJXA, findMessageJXA (numeric + RFC Message-ID), listMailboxesScriptJXA, mailboxInAccountJXA, and moveScriptJXA.
  • Updated ContextSchema.swift and CLAUDE.md, and added unit + JXA stub tests in Tests/macMCPTests/MailScopeAccountMailboxTests.swift.

Slash Ambiguity Resolution

Mailbox paths themselves contain / (e.g. [Gmail]/All Mail, Projects/Archive). MailScope.parseMailboxRules resolves the ambiguity by splitting an entry at its first / only if the first segment equals (after the existing NFC + lowercase folding) one of the allowed account names in mail_accounts and the remainder after / is non-empty; otherwise the whole entry is treated as a plain mailbox path matching any allowed account. Both Swift and all generated JXA scripts consume the parsed rules from this single helper so they cannot drift.

Calendar Scope Check

Checked CalendarService.swift and EventKitScope.swift (ScopedRows): calendars entries are already always Account/Calendar (ScopePath.Row.path, matched whole against rows[$0].path), so calendars do not have the bare-name cross-product issue and need no change.

Verification

  • swift test --filter "MailScopeAccountMailboxTests|MailScopeTests|MailScopeEnforcementTests|ScopeDeclarationTests|ResourceScopeTests|ContextEnumerationTests" (122 tests, 0 failures)
  • ./scripts/ci-guards.sh main HEAD (passed)
  • Did not run build.sh (confirmed it installs to ~/.local/bin/macmcp and invokes Relay).

@barelyworkingcode

Copy link
Copy Markdown
Owner Author

Addressed review findings:

  1. Draft destination authorization on resolved sender account (P1): senderJXA now takes scopeMailboxes and requiredMailbox (passed "Drafts" from createDraft) and checks scopeMailboxAllows against the actual resolved sender account before mail.OutgoingMessage is created, including when from takes precedence over account and when mail_accounts is ["*"].
  2. Wildcard mail_accounts: ["*"] account-qualifier resolution (P1): When scopeAccounts is nil ("*"), scopeMailboxRulesJXA resolves Account/Mailbox prefixes in JS against Mail's live account names (mail.accounts.name() + On My Mac), preventing a Work/INBOX entry from matching a literal Work/INBOX folder in another account while denying Work's INBOX.
  3. Unicode scalar / split & Greek final sigma folding (P2): MailScope.splitFirstSlash now splits on unicodeScalars rather than Character (so / followed by a combining mark is not skipped), and both ResourceScope.fold and scopeFoldJXA fold \u{03C2} (ς) to \u{03C3} (σ) so Swift and JXA agree.

Follow-up note (out of scope here): context/enumerate (MailService.mailboxEnumerationEntries) still emits plain mailbox paths as value (with the account in label); emitting Account/Mailbox values from the operator picker is left for a follow-up.

Verification: swift test --filter "MailScopeAccountMailboxTests|MailScopeTests|MailScopeEnforcementTests|MailComposeSenderTests|ScopeDeclarationTests|ResourceScopeTests|ContextEnumerationTests" (139 tests, 0 failures) and ./scripts/ci-guards.sh main HEAD (passed).

…sigma fold, compose guard & shared alias
@barelyworkingcode

Copy link
Copy Markdown
Owner Author

Addressed round-2 review findings:

  1. Fail closed on live account-name read failure (P1): scopeLiveAccountNames() now returns null when mail.accounts.name() throws or returns null, and scopeParseMailboxRules omits (denies) any entry containing / that needs disambiguation rather than widening it into a plain grant. Tested with a throwing mail.accounts.name.
  2. Reverted Greek final-sigma fold change (P1): Restored ResourceScope.fold and scopeFoldJXA to the prior shared fold so EventKit/Contacts/Mail folding is untouched. Follow-up note: the pre-existing Swift vs JS Greek final-sigma (Σ → σ vs ς) lowercasing difference is left as a separate follow-up.
  3. Compose guard & shared-alias draft authorization (P1): Passed senderResolved: scopeAccounts != nil || draftScopeMailboxes != nil into preSendGuardJXA so a wildcard-accounts auto-selected draft sender is verified before save, and updated sendRequireMailbox to check all accounts owning the chosen address and refuse if a shared alias is owned by another account outside the allowed accounts or Drafts scope. Tested through both senderJXA and preSendGuardJXA.

Verification: swift test --filter MailScopeAccountMailboxTests (18 hermetic stub tests, 0 failures) and ./scripts/ci-guards.sh main HEAD (passed).

@barelyworkingcode

Copy link
Copy Markdown
Owner Author

Addressed round-3 review findings:

  1. Wildcard Greek-sigma qualifier matching without touching the shared fold (P1): scopeParseMailboxRules now checks exact scopeFold first and falls back to a local final-sigma-normalized key (\u03c2 → \u03c3) mapped to the live account's own scopeFold (failing closed if two distinct live accounts collide on that key). ResourceScope.fold and scopeFold remain untouched. Added a test verifying ΟΣ vs οσ/INBOX admits ΟΣ:INBOX and blocks a literal Personal:οσ/INBOX.
  2. Fail closed on unread excluded account & no account-name disclosure (P1): sendRequireMailbox now fails closed if reading emailAddresses() throws on an excluded account, and omits other account names from both the shared-alias refusal and the unread-account error. Added tests for both.

Verification: swift test --filter MailScopeAccountMailboxTests (20 hermetic stub tests, 0 failures) and ./scripts/ci-guards.sh main HEAD (passed).

@barelyworkingcode

Copy link
Copy Markdown
Owner Author

Review: 3 rounds by an independent reviewer of another model family; all in-scope findings fixed; two inherited issues (move source race, operator picker submits path only) left as follow-ups
Verified: 68cd188 · selected-suite: swift test MailScopeAccountMailboxTests plus scope suites, 20 of 20 account-mailbox tests passed on the worker's host · CI: guards and ci green on this head · log: https://github.com/barelyworkingcode/macMCP/pull/86/checks

@barelyworkingcode

Copy link
Copy Markdown
Owner Author

Addressed final review blocker:

  • Updated MailService.scopeFoldJXA to lowercase per code point (Array.from(('' + v).normalize('NFC')).map(c => c.toLowerCase()).join('').normalize('NFC')), matching Swift's ResourceScope.fold exactly (while leaving ResourceScope.fold untouched).
  • Removed the sigma-specific workaround from scopeParseMailboxRules, so both parsing and enforcement use plain scopeFold.
  • Expanded testWildcardGreekSigmaQualifierMatchesLiveAccountAndBlocksLiteralCollision to cover both ΟΣ and ος live together (admitting only ΟΣ:INBOX, refusing ος:INBOX and Personal:οσ/INBOX) and only ος live (refusing οσ/INBOX).

Verification: swift test --filter MailScopeAccountMailboxTests (20 hermetic stub tests, 0 failures) and ./scripts/ci-guards.sh main HEAD (passed).

@barelyworkingcode

Copy link
Copy Markdown
Owner Author

Review: no blocking (independent reviewer of another model family, 5 rounds; head 81feb1e). Inherited issues left as follow-ups: move source race, operator picker submits path only.
Verified: 81feb1e · selected-suite: swift test MailScopeAccountMailboxTests plus scope suites, 20 of 20 account-mailbox tests passed on the worker's host · CI: guards and ci green on this head · log: https://github.com/barelyworkingcode/macMCP/pull/86/checks

@barelyworkingcode
barelyworkingcode merged commit 9dfb954 into main Nov 27, 2026
2 checks passed
@barelyworkingcode
barelyworkingcode deleted the mail-scope-account-mailbox branch November 27, 2026 23:49
@barelyworkingcode

Copy link
Copy Markdown
Owner Author

merge-check: merged. approved, target, ci, verified, contract, evidence, owner, weakening

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.

Mail scope: accept optional Account/Mailbox entries in mail_mailboxes so a mailbox is exact

1 participant