carlos / platform Public

appconfig: write a bundle at the version the page showed

BundleVersion (wire form "absent" or "e:<etag>"), GetBundle (default or
named bundle with its version) and ApplyAtVersion, wrapped in
WithAppMutation like Apply. The write refuses with ErrCASConflict when
the bundle's (found, etag) differs from the shown version, then is
apply's merge, unset-only refusal, CAS put and generation touch.
applyAt's existing callers are unchanged (want nil).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 8c7d107acd03269bbe2759c1867b67547fa4f105 parent b00645f
4 files changed, +293 −0
  • internal/appconfig/bundleversion.go +101 −0
  • internal/appconfig/generation.go +7 −0
  • internal/appconfig/store.go +13 −0
  • internal/appconfig/store_test.go +172 −0
diff --git a/internal/appconfig/bundleversion.go b/internal/appconfig/bundleversion.go
new file mode 100644
index 000000000..fe0ab9f60
--- /dev/null
+++ b/internal/appconfig/bundleversion.go
@@ -0,0 +1,101 @@
+// A bundle's version as a page displayed it, so a later write lands only
+// on that version (console secrets slice 2, the concurrency section of the
+// spec): the console carries it in the settings form and its save handler
+// writes through ApplyAtVersion.
+package appconfig
+
+import (
+ "context"
+ "fmt"
+ "strings"
+
+ "github.com/carlosframework/platform/internal/release"
+)
+
+// BundleVersion is a bundle object's state at a read: whether it exists,
+// and its etag when it does. The zero value is "absent".
+type BundleVersion struct {
+ Found bool
+ ETag string
+}
+
+// String is the version's wire form, the one a form field carries:
+// "absent", or "e:" followed by the etag.
+func (v BundleVersion) String() string {
+ if !v.Found {
+ return "absent"
+ }
+ return "e:" + v.ETag
+}
+
+// ParseBundleVersion reads String's wire form back. ok is false for
+// anything else.
+func ParseBundleVersion(s string) (BundleVersion, bool) {
+ if s == "absent" {
+ return BundleVersion{}, true
+ }
+ if etag, ok := strings.CutPrefix(s, "e:"); ok {
+ return BundleVersion{Found: true, ETag: etag}, true
+ }
+ return BundleVersion{}, false
+}
+
+// bundleKey validates acct, app and envName as GetEnvironment's readers do
+// and returns the bundle's key and its human name: envName "" is the
+// default bundle (Key), anything else a named environment
+// (EnvironmentKey).
+func bundleKey(acct, app, envName string) (string, string, error) {
+ if !release.ValidAccount(acct) {
+ return "", "", fmt.Errorf("config: invalid account %q", acct)
+ }
+ if !release.ValidApp(app) {
+ return "", "", fmt.Errorf("config: invalid app %q", app)
+ }
+ if envName == "" {
+ return Key(acct, app), DefaultBundle, nil
+ }
+ if !ValidEnvironmentName(envName) {
+ return "", "", fmt.Errorf("config: invalid environment name %q", envName)
+ }
+ return EnvironmentKey(acct, app, envName), envName, nil
+}
+
+// GetBundle reads one bundle (envName "" for the default) with its
+// version. A bundle that does not exist reads as an empty manifest at
+// version absent, not an error.
+func GetBundle(ctx context.Context, s release.Store, acct, app, envName string) (*Manifest, BundleVersion, error) {
+ key, _, err := bundleKey(acct, app, envName)
+ if err != nil {
+ return nil, BundleVersion{}, err
+ }
+ m, etag, found, err := getIfExistsAtETag(ctx, s, key)
+ if err != nil {
+ return nil, BundleVersion{}, err
+ }
+ if !found {
+ return &Manifest{Schema: 1, App: app, Vars: map[string]Var{}}, BundleVersion{}, nil
+ }
+ if m.Vars == nil {
+ m.Vars = map[string]Var{}
+ }
+ return m, BundleVersion{Found: true, ETag: etag}, nil
+}
+
+// ApplyAtVersion (this, under the app mutation) is apply and
+// applyEnvironment with an expectation: the write lands only while the
+// bundle is still at want, else it returns release.ErrCASConflict and
+// writes nothing. Otherwise it is their merge, their refusal to create a
+// bundle on an unset-only call, and their generation touch. Unsetting a
+// key that is already gone counts nothing and is not an error.
+func claimRawApplyAtVersion(ctx context.Context, s release.Store, acct, app, envName string, want BundleVersion, sets map[string]Var, unsets []string) (int, error) {
+ key, bundle, err := bundleKey(acct, app, envName)
+ if err != nil {
+ return 0, err
+ }
+ removed, _, err := applyAtExpecting(ctx, s, key, app, bundle, &want, sets, unsets)
+ if err != nil {
+ return 0, err
+ }
+ touchLogged(ctx, s, acct, app)
+ return removed, nil
+}
diff --git a/internal/appconfig/generation.go b/internal/appconfig/generation.go
index 4f8769124..206dd0c8d 100644
--- a/internal/appconfig/generation.go
+++ b/internal/appconfig/generation.go
@@ -140,6 +140,13 @@ func ApplyEnvironment(ctx context.Context, s release.Store, acct, app, envName s
})
return
}
+func ApplyAtVersion(ctx context.Context, s release.Store, acct, app, envName string, want BundleVersion, sets map[string]Var, unsets []string) (claimResult0 int, claimResult1 error) {
+ claimResult1 = release.WithAppMutation(ctx, s, acct, app, "appconfig.ApplyAtVersion", func(claimContext context.Context) error {
+ claimResult0, claimResult1 = claimRawApplyAtVersion(claimContext, s, acct, app, envName, want, sets, unsets)
+ return claimResult1
+ })
+ return
+}
func TouchGeneration(ctx context.Context, s release.Store, acct, app string) (claimResult0 error) {
claimResult0 = release.WithAppMutation(ctx, s, acct, app, "appconfig.TouchGeneration", func(claimContext context.Context) error {
claimResult0 = claimRawTouchGeneration(claimContext, s, acct, app)
diff --git a/internal/appconfig/store.go b/internal/appconfig/store.go
index 7993b0b85..174ef843b 100644
--- a/internal/appconfig/store.go
+++ b/internal/appconfig/store.go
@@ -160,6 +160,16 @@ func apply(ctx context.Context, s release.Store, acct, app, bundle string, sets
// existence rail. Sets still create, which is how a bundle is meant to come
// into being.
func applyAt(ctx context.Context, s release.Store, key, app, bundle string, sets map[string]Var, unsets []string) (int, string, error) {
+ return applyAtExpecting(ctx, s, key, app, bundle, nil, sets, unsets)
+}
+
+// applyAtExpecting is applyAt with an expectation: when want is not nil,
+// the object's state as read for the write (found, etag) must be *want, or
+// nothing is written and the call returns release.ErrCASConflict. The
+// check sits between the read and the write, and the write is at the read
+// etag, so a change made after the read surfaces as the store's own CAS
+// conflict. applyAt passes nil, which is applyAt's behaviour unchanged.
+func applyAtExpecting(ctx context.Context, s release.Store, key, app, bundle string, want *BundleVersion, sets map[string]Var, unsets []string) (int, string, error) {
for k, v := range sets {
if !ValidVarName(k) {
return 0, "", fmt.Errorf("config: invalid var name %q", k)
@@ -179,6 +189,9 @@ func applyAt(ctx context.Context, s release.Store, key, app, bundle string, sets
if err != nil {
return 0, "", err
}
+ if want != nil && (found != want.Found || (found && etag != want.ETag)) {
+ return 0, "", fmt.Errorf("config: %s %s bundle changed since it was read: %w", app, bundle, release.ErrCASConflict)
+ }
if !found && len(sets) == 0 {
return 0, "", fmt.Errorf("config: %s has no %s bundle — nothing to unset", app, bundle)
}
diff --git a/internal/appconfig/store_test.go b/internal/appconfig/store_test.go
index 161be3f7f..92d497950 100644
--- a/internal/appconfig/store_test.go
+++ b/internal/appconfig/store_test.go
@@ -308,3 +308,175 @@ func TestNewerSchemaIsRefusedNotDowngraded(t *testing.T) {
t.Fatalf("schema-3 object was modified: %s", got)
}
}
+
+// Console secrets slice 2, Task 2: the save handler writes a bundle only at
+// the version (found, etag) the page showed.
+
+func TestBundleVersionWireForm(t *testing.T) {
+ for _, v := range []BundleVersion{{}, {Found: true, ETag: "abc"}, {Found: true}} {
+ got, ok := ParseBundleVersion(v.String())
+ if !ok || got != v {
+ t.Fatalf("%+v -> %q -> %+v (%v)", v, v.String(), got, ok)
+ }
+ }
+ if s := (BundleVersion{}).String(); s != "absent" {
+ t.Fatalf("absent = %q", s)
+ }
+ if s := (BundleVersion{Found: true, ETag: "x"}).String(); s != "e:x" {
+ t.Fatalf("found = %q", s)
+ }
+ for _, bad := range []string{"", "e", "abc", "Absent", "x:abc"} {
+ if _, ok := ParseBundleVersion(bad); ok {
+ t.Fatalf("ParseBundleVersion(%q) accepted", bad)
+ }
+ }
+}
+
+func TestApplyAtVersionWritesAtDisplayedVersion(t *testing.T) {
+ for _, env := range []string{"", "staging"} {
+ t.Run("env="+env, func(t *testing.T) {
+ s := release.NewDirStore(t.TempDir())
+ ctx := context.Background()
+ m, v, err := GetBundle(ctx, s, "bab", "keymail", env)
+ if err != nil || v.Found || len(m.Vars) != 0 {
+ t.Fatalf("GetBundle on nothing = %+v %+v %v", m, v, err)
+ }
+ if _, err := ApplyAtVersion(ctx, s, "bab", "keymail", env, v, map[string]Var{"A": {Kind: "env", Value: "1"}}, nil); err != nil {
+ t.Fatalf("create at absent: %v", err)
+ }
+ m, v, err = GetBundle(ctx, s, "bab", "keymail", env)
+ if err != nil || !v.Found || m.Vars["A"].Value != "1" {
+ t.Fatalf("after create = %+v %+v %v", m, v, err)
+ }
+ removed, err := ApplyAtVersion(ctx, s, "bab", "keymail", env, v, map[string]Var{"B": {Kind: "env", Value: "2"}}, []string{"A"})
+ if err != nil || removed != 1 {
+ t.Fatalf("write at shown version = %d, %v", removed, err)
+ }
+ m, _, err = GetBundle(ctx, s, "bab", "keymail", env)
+ if err != nil || len(m.Vars) != 1 || m.Vars["B"].Value != "2" {
+ t.Fatalf("after write = %+v %v", m, err)
+ }
+ // The other bundle is untouched.
+ other := "staging"
+ if env != "" {
+ other = ""
+ }
+ if _, ov, err := GetBundle(ctx, s, "bab", "keymail", other); err != nil || ov.Found {
+ t.Fatalf("other bundle %q = %+v %v", other, ov, err)
+ }
+ if _, _, err := s.Get(ctx, GenKey("bab", "keymail")); err != nil {
+ t.Fatalf("generation not touched: %v", err)
+ }
+ })
+ }
+}
+
+func TestApplyAtVersionStaleETag(t *testing.T) {
+ for _, env := range []string{"", "staging"} {
+ t.Run("env="+env, func(t *testing.T) {
+ s := release.NewDirStore(t.TempDir())
+ ctx := context.Background()
+ if _, err := ApplyAtVersion(ctx, s, "bab", "keymail", env, BundleVersion{}, map[string]Var{"A": {Kind: "env", Value: "1"}}, nil); err != nil {
+ t.Fatal(err)
+ }
+ _, shown, err := GetBundle(ctx, s, "bab", "keymail", env)
+ if err != nil {
+ t.Fatal(err)
+ }
+ // Someone else saves after the page was shown.
+ if _, err := ApplyAtVersion(ctx, s, "bab", "keymail", env, shown, map[string]Var{"A": {Kind: "env", Value: "other"}}, nil); err != nil {
+ t.Fatal(err)
+ }
+ key := Key("bab", "keymail")
+ if env != "" {
+ key = EnvironmentKey("bab", "keymail", env)
+ }
+ before, _, err := s.Get(ctx, key)
+ if err != nil {
+ t.Fatal(err)
+ }
+ _, err = ApplyAtVersion(ctx, s, "bab", "keymail", env, shown, map[string]Var{"A": {Kind: "env", Value: "mine"}}, nil)
+ if !errors.Is(err, release.ErrCASConflict) {
+ t.Fatalf("stale write = %v, want ErrCASConflict", err)
+ }
+ after, _, err := s.Get(ctx, key)
+ if err != nil || string(after) != string(before) {
+ t.Fatalf("stale write changed the object:\nbefore %s\nafter %s (%v)", before, after, err)
+ }
+ })
+ }
+}
+
+func TestApplyAtVersionAbsentThenCreated(t *testing.T) {
+ for _, env := range []string{"", "staging"} {
+ t.Run("env="+env, func(t *testing.T) {
+ s := release.NewDirStore(t.TempDir())
+ ctx := context.Background()
+ _, shown, err := GetBundle(ctx, s, "bab", "keymail", env)
+ if err != nil || shown.Found {
+ t.Fatalf("shown = %+v %v", shown, err)
+ }
+ if env == "" {
+ err = SetEnv(ctx, s, "bab", "keymail", map[string]string{"A": "first"})
+ } else {
+ err = SaveEnvironment(ctx, s, "bab", "keymail", env, map[string]Var{"A": {Kind: "env", Value: "first"}}, nil)
+ }
+ if err != nil {
+ t.Fatal(err)
+ }
+ _, err = ApplyAtVersion(ctx, s, "bab", "keymail", env, shown, map[string]Var{"A": {Kind: "env", Value: "mine"}}, nil)
+ if !errors.Is(err, release.ErrCASConflict) {
+ t.Fatalf("write at absent after a create = %v, want ErrCASConflict", err)
+ }
+ m, _, err := GetBundle(ctx, s, "bab", "keymail", env)
+ if err != nil || m.Vars["A"].Value != "first" {
+ t.Fatalf("bundle = %+v %v, want the first writer's", m, err)
+ }
+ // And the other way: shown as existing, absent now.
+ _, err = ApplyAtVersion(ctx, s, "bab", "fresh", env, BundleVersion{Found: true, ETag: "gone"}, map[string]Var{"A": {Kind: "env", Value: "x"}}, nil)
+ if !errors.Is(err, release.ErrCASConflict) {
+ t.Fatalf("write at a version of a bundle that is absent = %v, want ErrCASConflict", err)
+ }
+ if _, v, err := GetBundle(ctx, s, "bab", "fresh", env); err != nil || v.Found {
+ t.Fatalf("a refused write created the bundle: %+v %v", v, err)
+ }
+ })
+ }
+}
+
+func TestApplyAtVersionUnsetGoneIsNoop(t *testing.T) {
+ for _, env := range []string{"", "staging"} {
+ t.Run("env="+env, func(t *testing.T) {
+ s := release.NewDirStore(t.TempDir())
+ ctx := context.Background()
+ if _, err := ApplyAtVersion(ctx, s, "bab", "keymail", env, BundleVersion{}, map[string]Var{"A": {Kind: "env", Value: "1"}}, nil); err != nil {
+ t.Fatal(err)
+ }
+ _, shown, err := GetBundle(ctx, s, "bab", "keymail", env)
+ if err != nil {
+ t.Fatal(err)
+ }
+ removed, err := ApplyAtVersion(ctx, s, "bab", "keymail", env, shown, nil, []string{"GONE"})
+ if err != nil || removed != 0 {
+ t.Fatalf("unset of a gone key = %d, %v, want 0, nil", removed, err)
+ }
+ m, _, err := GetBundle(ctx, s, "bab", "keymail", env)
+ if err != nil || len(m.Vars) != 1 || m.Vars["A"].Value != "1" {
+ t.Fatalf("bundle = %+v %v", m, err)
+ }
+ })
+ }
+}
+
+func TestApplyAtVersionRefusesInvalidNames(t *testing.T) {
+ s := release.NewDirStore(t.TempDir())
+ ctx := context.Background()
+ for _, tc := range []struct{ acct, app, env string }{{"BAD!", "keymail", ""}, {"bab", "BAD!", ""}, {"bab", "keymail", "default"}, {"bab", "keymail", "Bad"}} {
+ if _, _, err := GetBundle(ctx, s, tc.acct, tc.app, tc.env); err == nil {
+ t.Errorf("GetBundle%+v accepted", tc)
+ }
+ if _, err := ApplyAtVersion(ctx, s, tc.acct, tc.app, tc.env, BundleVersion{}, map[string]Var{"A": {Kind: "env", Value: "1"}}, nil); err == nil {
+ t.Errorf("ApplyAtVersion%+v accepted", tc)
+ }
+ }
+}