carlos / platform Public

console: the simulation checks every SMTP key an instance would use

At every step of TestSubmissionChannelSimulation and the seed sweeps:
after a switch-off, every bundle's SMTP key is one AWS holds, active,
for a user it still has; and while switched, wherever the agent's rule
for an unreadable switch (activator.MailUnknownSubmits, now exported)
picks SMTP, that key is live too. Scripted steps add a key lost in AWS
before a switch-off, and a switch-off whose key state cannot be read,
which must refuse with the switch still on.

Mutation-checked: without the key check, the lost-key step fails; with
the old ReapEnvironment order, the part-failed switch-on (team's write
#2) fails; without the week-7 reap's bundle write, seed 2009 fails.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 8ac038738c2a28637544abd6d39615b9a3cf3a27 parent 561ff74
3 files changed, +74 −16
  • internal/activator/backend_exec.go +3 −3
  • internal/activator/mailswitch.go +3 −3
  • internal/console/submission_sim_test.go +68 −10
diff --git a/internal/activator/backend_exec.go b/internal/activator/backend_exec.go
index 13b266aba..41bb7965e 100644
--- a/internal/activator/backend_exec.go
+++ b/internal/activator/backend_exec.go
@@ -232,7 +232,7 @@ type ExecBackend struct {
// (the app sender's submission block, cached by the agent): on, off, or
// unknown when the agent cannot read it. It decides CARLOS_MAIL with the
// bind (agentOwnedVars); an unknown answer is decided by the instance's
- // own environment, never read as off (mailUnknownSubmits). Nil is off
+ // own environment, never read as off (MailUnknownSubmits). Nil is off
// for every app.
MailSwitch func(acct, app string) MailSwitchState
// mailUnknownLogged holds the hosts agentOwnedVars has logged as
@@ -2865,7 +2865,7 @@ func (b *ExecBackend) agentOwnedVars(rt registry.Route, control string, fileEnv
//
// A switch the agent cannot read (MailSwitchUnknown) is never read as off
// (Fable's final review, P2-2, ruled 2026-11-14): fileEnv, the environment
-// the instance is about to start with, decides it (mailUnknownSubmits). A
+// the instance is about to start with, decides it (MailUnknownSubmits). A
// bundle holding a <stem>_MAIL_FROM and no <P>_SMTP_HOST submits; any
// other keeps the SMTP channel its bundle carries. That is logged once per
// host.
@@ -2879,7 +2879,7 @@ func (b *ExecBackend) mailSubmits(rt registry.Route, haveControl bool, fileEnv m
switch b.MailSwitch(acct, rt.App) {
case MailSwitchOn:
case MailSwitchUnknown:
- submit := mailUnknownSubmits(fileEnv)
+ submit := MailUnknownSubmits(fileEnv)
if _, seen := b.mailUnknownLogged.LoadOrStore(rt.Host, true); !seen {
channel := "its bundle's SMTP settings, if it has any"
if submit {
diff --git a/internal/activator/mailswitch.go b/internal/activator/mailswitch.go
index b5e3c8f7c..5d49d9c48 100644
--- a/internal/activator/mailswitch.go
+++ b/internal/activator/mailswitch.go
@@ -10,7 +10,7 @@ import "strings"
// the five <P>_SMTP_*, left its instances with neither channel, the dead
// state decision 11 rejected (Fable's final review, P2-2, ruled 2026-11-14).
// An unknown answer is never read as off: the instance's own bundle decides
-// (mailUnknownSubmits).
+// (MailUnknownSubmits).
type MailSwitchState uint8
const (
@@ -36,7 +36,7 @@ func (s MailSwitchState) String() string {
return "invalid"
}
-// mailUnknownSubmits is how an unreadable switch is decided for one
+// MailUnknownSubmits is how an unreadable switch is decided for one
// instance, from the environment it is about to start with: submit when it
// holds a <stem>_MAIL_FROM and no <P>_SMTP_HOST, and otherwise the SMTP
// channel its bundle carries, if any.
@@ -50,7 +50,7 @@ func (s MailSwitchState) String() string {
// delivered key, whichever side of a flip the app is on. Submitting for an
// app that is not in fact switched is safe: the mailer reads the app's
// switch itself and refuses it (403 SUBMISSION_OFF).
-func mailUnknownSubmits(env map[string]string) bool {
+func MailUnknownSubmits(env map[string]string) bool {
from, smtp := false, false
for k, v := range env {
if v == "" {
diff --git a/internal/console/submission_sim_test.go b/internal/console/submission_sim_test.go
index b275e06b7..4a4dc3dfa 100644
--- a/internal/console/submission_sim_test.go
+++ b/internal/console/submission_sim_test.go
@@ -17,6 +17,7 @@ import (
"testing"
"time"
+ "github.com/carlosframework/platform/internal/activator"
"github.com/carlosframework/platform/internal/appconfig"
"github.com/carlosframework/platform/internal/mail"
"github.com/carlosframework/platform/internal/mailwire"
@@ -287,7 +288,6 @@ type channelSim struct {
envs []string // owned environments: "team" and the made ones
reported map[string]string
dry bool // a dry pass: no bookkeeping, no honesty checks
- liveKeys bool // check also checks that a switch-off's SMTP keys work
nextID int
steps []string
}
@@ -451,12 +451,14 @@ func (sim *channelSim) declared(version string) bool {
}
// check asserts the invariant after a step. wasSwitched is the switch
-// before it: a step that turned it off must leave every instance on SMTP.
-// With liveKeys set, each of those SMTP keys must also be one AWS still
-// holds, active, for a user it still has (P2-3 of Fable's final review: a
-// reap that ran around a switch-off deleted the user the switch-off had
-// just delivered). liveKeys is set for that step only: the other steps'
-// faults can leave a key that is not live (reported with P2-3).
+// before it: a step that turned it off must leave every instance on SMTP,
+// with a key AWS still holds, active, for a user it still has (P2-3 of
+// Fable's final review; and Paul's ruling of 2026-11-14, switch-off checks
+// keys). While switched, an instance the agent starts without being able
+// to read the switch decides its channel from its bundle
+// (activator.MailUnknownSubmits); where that is SMTP, the key must be live
+// too (Astra's census review of 76f4f582c; ruled 2026-11-14, switch-on
+// removes SMTP settings before deleting the user).
// While switched, every instance resolves to a declared build (and a box
// that has not adopted yet serves a declared one too), and its bundle
// holds a From the box's mailer sends.
@@ -487,13 +489,22 @@ func (sim *channelSim) check(wasSwitched bool) {
if got := vars[mf]; got.Kind != "env" || !sim.sendable(sn, got.Value) {
bad = append(bad, fmt.Sprintf("%s (environment %q) has no %s the mailer sends (%q)", rec.Host, rec.Environment, mf, got.Value))
}
+ plain := map[string]string{}
+ for k, v := range vars {
+ plain[k] = v.Value
+ }
+ if !activator.MailUnknownSubmits(plain) {
+ if key := vars[user].Value; key == "" || !sim.keyLive(key) {
+ bad = append(bad, fmt.Sprintf("%s (environment %q), started with the switch unreadable, takes SMTP from its bundle, and key %q is not live", rec.Host, rec.Environment, key))
+ }
+ }
case wasSwitched:
for _, k := range []string{host, port, user, pass, from} {
if _, ok := vars[k]; !ok {
bad = append(bad, fmt.Sprintf("%s (environment %q) was switched off without %s", rec.Host, rec.Environment, k))
}
}
- if key := vars[user].Value; sim.liveKeys && key != "" && !sim.keyLive(key) {
+ if key := vars[user].Value; key != "" && !sim.keyLive(key) {
bad = append(bad, fmt.Sprintf("%s (environment %q) was switched off onto key %s, which no user holds active", rec.Host, rec.Environment, key))
}
}
@@ -973,16 +984,15 @@ func (sim *channelSim) scripted() {
// P2-3 of Fable's final review: a switch-off started inside a reap
// paused before its deletes.
reached := false
- sim.liveKeys = true
sim.do("the hourly reap, a switch-off started inside it", false, func() string {
out, r := sim.reapAroundSwitchOff()
reached = r
return out
})
- sim.liveKeys = false
if !reached {
sim.t.Fatalf("the reap never reached the app user's keys, so the step proved nothing\nsteps:\n %s", strings.Join(sim.steps, "\n "))
}
+ sim.keysChecked()
sim.do("switch on", false, sim.switchOn)
for _, env := range slices.Clone(sim.envs) {
if env != "team" {
@@ -991,6 +1001,54 @@ func (sim *channelSim) scripted() {
}
}
+// switchOnLeavingTheFive is a switch-on whose last write to the default
+// bundle (the five out) fails: the app is switched, and the default bundle
+// still holds the five, naming the app user's live key, beside its From.
+func (sim *channelSim) switchOnLeavingTheFive() {
+ sim.t.Helper()
+ sim.ready()
+ sim.faults.armMatching("config.json", 2)
+ sim.do("switch on, the default bundle's last write failing", false, sim.switchOn)
+ if !sim.switched() || sim.bundleVars("")["TITO_SMTP_HOST"].Value == "" {
+ sim.t.Fatalf("the switch-on did not stop with the five in the default bundle, so the steps after it prove nothing\nsteps:\n %s", strings.Join(sim.steps, "\n "))
+ }
+}
+
+// keysChecked are the steps for Paul's ruling of 2026-11-14 (switch-off
+// checks keys): a switch-off that finds a bundle holding the recorded key
+// asks AWS whether that key is still live before it skips the mint.
+func (sim *channelSim) keysChecked() {
+ sim.t.Helper()
+ ctx := context.Background()
+ user := mail.UserName("bab", "titogo")
+ // The key the default bundle names goes, in AWS, before a switch-off.
+ // Read as delivered, it would switch every default-bundle instance
+ // onto a key no user holds.
+ sim.switchOnLeavingTheFive()
+ sim.do("AWS loses the app user's key, then switch off", false, func() string {
+ key := sim.bundleVars("")["TITO_SMTP_USER"].Value
+ if err := sim.prov.DeleteKey(ctx, user, key); err != nil {
+ return "setup: " + err.Error()
+ }
+ return sim.switchOff()
+ })
+ // A key whose state cannot be read is neither live nor missing: the
+ // switch-off stops before it writes the switch, and the app keeps
+ // submitting.
+ sim.switchOnLeavingTheFive()
+ sim.prov.FailNext = map[string]error{"KeyState": errors.New("simulated AWS failure")}
+ var st int
+ sim.do("switch off, the key's state unreadable", false, func() string {
+ var out string
+ st, out = sim.w.submit(sim.t, false)
+ return short(st, out)
+ })
+ if st/100 == 2 || !sim.switched() {
+ sim.t.Fatalf("a switch-off that could not read a key's state answered %d (switched %v), want a refusal with the switch still on\nsteps:\n %s", st, sim.switched(), strings.Join(sim.steps, "\n "))
+ }
+ sim.do("switch off", false, sim.switchOff)
+}
+
// random runs n random steps.
func (sim *channelSim) random(n int) {
sim.t.Helper()