mail: take SMTP settings out of a bundle before deleting its user
Ruled by Paul, 2026-11-14: switch-on removes SMTP settings before deleting the user. ReapEnvironment deleted the environment's keys and user and only then took the five out of its bundle, so a failed bundle write left a switched app's environment holding its From beside a dead key, and an instance started while the agent could not read the switch took SMTP from it (Astra, census review of 76f4f582c). The bundle write now comes first; a re-run finishes the deletion. The week-7 reap of the app-level user gets the same order: the default bundle is made a switched app's (its From, none of the five) before the user goes. The seed sweep found it (seed 2009: a switch-on that failed after On:true left the five in the default bundle, then the reap deleted the user under them).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+27
−5
internal/mail/sweep.go+12 −0internal/mail/walk_environment.go+15 −5
diff --git a/internal/mail/sweep.go b/internal/mail/sweep.go| index cfb208f24..69235dee0 100644 |
| --- a/internal/mail/sweep.go |
| +++ b/internal/mail/sweep.go |
| @@ -155,6 +155,18 @@ func claimUnserializedReapAppUserAfterSubmission(ctx context.Context, s release. |
| if recent[CallerClassAppUser] != 0 || recent[CallerClassUnrecorded] != 0 { |
| return false, nil |
| } |
| + // The default bundle is made a switched app's (its From, none of the |
| + // five) before the user's keys go, the order Paul ruled for switch-on |
| + // (2026-11-14: SMTP settings out before the user is deleted). A |
| + // switch-on that failed after writing On:true can leave the five there |
| + // beside the From; deleting the user under them left a bundle naming a |
| + // dead key, which an instance started while the agent could not read |
| + // the switch would take. |
| + if rec.Declared != nil { |
| + if err := writeMailFromIn(ctx, s, acct, app, "", rec.Declared.EnvPrefix, rec.Declared.From); err != nil { |
| + return false, err |
| + } |
| + } |
| if err := p.ReapKeys(ctx, user, nil); err != nil { |
| return false, fmt.Errorf("mail: reap keys for %s: %w", user, err) |
| } |
diff --git a/internal/mail/walk_environment.go b/internal/mail/walk_environment.go| index 9a73108ed..fb3643576 100644 |
| --- a/internal/mail/walk_environment.go |
| +++ b/internal/mail/walk_environment.go |
| @@ -178,6 +178,21 @@ func claimUnserializedReapEnvironment(ctx context.Context, s release.Store, p Pr |
| } |
| return rec, nil |
| } |
| + // The variables come out of the bundle first, and only then do the |
| + // keys and the user go (ruled by Paul, 2026-11-14: switch-on removes |
| + // SMTP settings before deleting the user). A failure part-way then |
| + // leaves either a bundle still naming a live key, or one naming none |
| + // beside a user the record still holds, which a re-run deletes; never |
| + // a bundle naming a dead key. The other order left a switched app's |
| + // environment holding its From beside the five of a deleted user, and |
| + // an instance started while the agent could not read the switch took |
| + // SMTP from them and had no working channel (Astra, census review of |
| + // 76f4f582c). |
| + if rec.Declared != nil { |
| + if err := unsetSMTPEnvIn(ctx, s, acct, app, env, rec.Declared.EnvPrefix, keepFrom); err != nil { |
| + return nil, err |
| + } |
| + } |
| if err := p.ReapKeys(ctx, ep.IAMUser, nil); err != nil { |
| return nil, fmt.Errorf("mail: reap keys for %s: %w", ep.IAMUser, err) |
| } |
| @@ -187,11 +202,6 @@ func claimUnserializedReapEnvironment(ctx context.Context, s release.Store, p Pr |
| if err := DeleteCallerIndex(ctx, s, ep.IAMUser, acct, app, SenderAppID); err != nil { |
| return nil, err |
| } |
| - if rec.Declared != nil { |
| - if err := unsetSMTPEnvIn(ctx, s, acct, app, env, rec.Declared.EnvPrefix, keepFrom); err != nil { |
| - return nil, err |
| - } |
| - } |
| delete(rec.Environments, env) |
| if len(rec.Environments) == 0 { |
| rec.Environments = nil |