Validate seed content sections and gate them on SDK v0.3.11

A seed that sets master page content, a theme preset, mailing lists, posts,
email templates or page SEO must declare a plugin_sdk constraint that refuses
hosts before v0.3.11, so an older CMS rejects the artifact instead of applying
half a site.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Alex Dunmow 2026-11-20 00:55:21 +08:00
parent 3b13dccdbb
commit 4970bf4fa1
3 changed files with 277 additions and 1 deletions

View File

@ -12,9 +12,11 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"strings" "strings"
"time"
abiv1 "git.dev.alexdunmow.com/block/pluginsdk/abi/v1" abiv1 "git.dev.alexdunmow.com/block/pluginsdk/abi/v1"
core "git.dev.alexdunmow.com/block/pluginsdk/plugin" core "git.dev.alexdunmow.com/block/pluginsdk/plugin"
"golang.org/x/mod/semver"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@ -187,6 +189,8 @@ type seedJSON struct {
Slot string `json:"slot"` Slot string `json:"slot"`
SortOrder int32 `json:"sort_order"` SortOrder int32 `json:"sort_order"`
} `json:"blocks"` } `json:"blocks"`
SEO *seedSEO `json:"seo"`
ReplaceExisting bool `json:"replace_existing"`
} `json:"pages"` } `json:"pages"`
MenuItems []struct { MenuItems []struct {
Menu string `json:"menu"` Menu string `json:"menu"`
@ -195,8 +199,53 @@ type seedJSON struct {
PageSlug string `json:"page_slug"` PageSlug string `json:"page_slug"`
SortOrder int32 `json:"sort_order"` SortOrder int32 `json:"sort_order"`
} `json:"menu_items"` } `json:"menu_items"`
DataTables []struct {
Key string `json:"key"`
} `json:"data_tables"`
MasterPages []struct {
Template string `json:"template"`
Blocks []struct {
BlockKey string `json:"block_key"`
Content map[string]any `json:"content"`
} `json:"blocks"`
} `json:"master_pages"`
ThemePreset *struct {
Template string `json:"template"`
Preset string `json:"preset"`
} `json:"theme_preset"`
MailingLists []struct {
Slug string `json:"slug"`
Name string `json:"name"`
} `json:"mailing_lists"`
Posts []struct {
Key string `json:"key"`
Slug string `json:"slug"`
Title string `json:"title"`
Body []map[string]any `json:"body"`
Status string `json:"status"`
PostDate string `json:"post_date"`
SEO *seedSEO `json:"seo"`
} `json:"posts"`
EmailTemplates []struct {
Name string `json:"name"`
Subject string `json:"subject"`
Body string `json:"body"`
Table string `json:"table"`
} `json:"email_templates"`
} }
type seedSEO struct {
MetaTitle string `json:"meta_title"`
MetaDescription string `json:"meta_description"`
}
// SeedContentMinSDK is the plugin SDK release whose hosts apply the seed
// sections master_pages, theme_preset, mailing_lists, posts, email_templates
// and the page fields seo and replace_existing. An older host ignores them, so
// an artifact that uses them must refuse such hosts through
// compatibility.plugin_sdk.
const SeedContentMinSDK = "v0.3.11"
// BuildCodeless builds the codeless plugin repo in opts.Dir with // BuildCodeless builds the codeless plugin repo in opts.Dir with
// BuildCodelessFS and writes the artifact to opts.Output. // BuildCodelessFS and writes the artifact to opts.Output.
func BuildCodeless(opts BuildOptions) (*BuildResult, error) { func BuildCodeless(opts BuildOptions) (*BuildResult, error) {
@ -372,7 +421,9 @@ func ValidateBlocksDir(fsys fs.FS) (int, error) {
} }
// ValidateSeedDir validates seed/seed.json when the dir is present: it // ValidateSeedDir validates seed/seed.json when the dir is present: it
// parses, media entries carry an id + an existing file, pages carry slugs. // parses, media entries carry an id + an existing file, pages carry slugs, the
// content sections carry their required fields, and a seed that uses them
// declares a plugin_sdk constraint that refuses hosts before SeedContentMinSDK.
func ValidateSeedDir(fsys fs.FS) error { func ValidateSeedDir(fsys fs.FS) error {
root := DirSeed root := DirSeed
if _, err := fs.Stat(fsys, root); errors.Is(err, fs.ErrNotExist) { if _, err := fs.Stat(fsys, root); errors.Is(err, fs.ErrNotExist) {
@ -404,7 +455,130 @@ func ValidateSeedDir(fsys fs.FS) error {
return fmt.Errorf("seed.json: menu item %d needs menu and label", i) return fmt.Errorf("seed.json: menu item %d needs menu and label", i)
} }
} }
if err := validateSeedContent(&sf); err != nil {
return err
}
if !sf.usesContentSections() {
return nil return nil
}
modBytes, err := fs.ReadFile(fsys, FileMod)
if err != nil {
return fmt.Errorf("read plugin.mod: %w", err)
}
mod, err := core.ParseModFull(modBytes)
if err != nil {
return fmt.Errorf("parse plugin.mod: %w", err)
}
var constraint string
if mod.Compatibility != nil {
constraint = mod.Compatibility.PluginSDK
}
if !constraintExcludesBelow(constraint, SeedContentMinSDK) {
return fmt.Errorf("seed.json uses sections that hosts before plugin SDK %s ignore; set compatibility.plugin_sdk to \">=%s\" or later (now %q)",
SeedContentMinSDK, strings.TrimPrefix(SeedContentMinSDK, "v"), constraint)
}
return nil
}
func (sf *seedJSON) usesContentSections() bool {
if len(sf.MasterPages) > 0 || sf.ThemePreset != nil || len(sf.MailingLists) > 0 || len(sf.Posts) > 0 || len(sf.EmailTemplates) > 0 {
return true
}
for _, p := range sf.Pages {
if p.SEO != nil || p.ReplaceExisting {
return true
}
}
return false
}
func validateSeedContent(sf *seedJSON) error {
tables := map[string]bool{}
for _, dt := range sf.DataTables {
tables[dt.Key] = true
}
for i, mp := range sf.MasterPages {
if mp.Template == "" || len(mp.Blocks) == 0 {
return fmt.Errorf("seed.json: master_pages entry %d needs a template and blocks", i)
}
for j, b := range mp.Blocks {
if b.BlockKey == "" || len(b.Content) == 0 {
return fmt.Errorf("seed.json: master_pages %q block %d needs block_key and content", mp.Template, j)
}
}
}
if tp := sf.ThemePreset; tp != nil && (tp.Template == "" || tp.Preset == "") {
return fmt.Errorf("seed.json: theme_preset needs template and preset")
}
lists := map[string]bool{}
for i, ml := range sf.MailingLists {
if ml.Slug == "" || lists[ml.Slug] {
return fmt.Errorf("seed.json: mailing list %d needs a unique slug", i)
}
lists[ml.Slug] = true
}
keys, slugs := map[string]bool{}, map[string]bool{}
for i, p := range sf.Posts {
if p.Key == "" || p.Slug == "" || p.Title == "" {
return fmt.Errorf("seed.json: post %d needs key, slug and title", i)
}
if keys[p.Key] || slugs[p.Slug] {
return fmt.Errorf("seed.json: post %q repeats a key or slug", p.Key)
}
keys[p.Key], slugs[p.Slug] = true, true
if strings.Contains(p.Slug, "/") {
return fmt.Errorf("seed.json: post %q slug %q must be a single segment", p.Key, p.Slug)
}
if p.Status != "" && p.Status != "published" && p.Status != "draft" {
return fmt.Errorf("seed.json: post %q status %q must be published or draft", p.Key, p.Status)
}
if p.PostDate != "" {
if _, err := time.Parse("2006-01-02", p.PostDate); err != nil {
return fmt.Errorf("seed.json: post %q post_date %q must be YYYY-MM-DD", p.Key, p.PostDate)
}
}
if len(p.Body) == 0 {
return fmt.Errorf("seed.json: post %q needs a body", p.Key)
}
}
names := map[string]bool{}
for i, et := range sf.EmailTemplates {
if et.Name == "" || et.Subject == "" || et.Body == "" || names[et.Name] {
return fmt.Errorf("seed.json: email template %d needs a unique name, a subject and a body", i)
}
names[et.Name] = true
if et.Table != "" && !tables[et.Table] {
return fmt.Errorf("seed.json: email template %q names data table %q that data_tables does not declare", et.Name, et.Table)
}
}
return nil
}
// constraintExcludesBelow reports whether a plugin_sdk constraint such as
// ">=0.3.11 <0.4.0" admits no host version below minVersion.
func constraintExcludesBelow(constraint, minVersion string) bool {
for tok := range strings.FieldsSeq(constraint) {
op, v := "", tok
for _, o := range []string{">=", "==", ">", "="} {
if after, ok := strings.CutPrefix(tok, o); ok {
op, v = o, after
break
}
}
if op == "" {
continue
}
if !strings.HasPrefix(v, "v") {
v = "v" + v
}
if !semver.IsValid(v) {
continue
}
if semver.Compare(v, minVersion) >= 0 {
return true
}
}
return false
} }
// ApplyManifestYAML folds the optional root manifest.yaml into the manifest. // ApplyManifestYAML folds the optional root manifest.yaml into the manifest.

View File

@ -240,3 +240,57 @@ func TestCodelessBuildRejectsBadDeclarations(t *testing.T) {
}) })
} }
} }
const contentSeed = `{"data_tables":[{"key":"contact"}],
"pages":[{"slug":"/","title":"Home","replace_existing":true,"seo":{"meta_title":"Home","meta_description":"Hi"}}],
"master_pages":[{"template":"coffee","blocks":[{"block_key":"footer","content":{"companyName":"Dune","tagline":null}}]}],
"theme_preset":{"template":"coffee","preset":"dark-roast"},
"mailing_lists":[{"slug":"main","name":"Newsletter"}],
"posts":[{"key":"p1","slug":"hello","title":"Hello","post_date":"2026-11-20","body":[{"type":"paragraph"}]}],
"email_templates":[{"name":"Thanks","subject":"Thanks","body":"Hi","table":"contact"}]}`
func contentMod(constraint string) string {
return "[plugin]\nname = \"x\"\nversion = \"0.1.0\"\n\n[compatibility]\nplugin_sdk = \"" + constraint + "\"\n"
}
func TestValidateSeedContentSections(t *testing.T) {
dir := t.TempDir()
writeFixture(t, dir, map[string]string{"plugin.mod": contentMod(">=0.3.11 <0.4.0"), "seed/seed.json": contentSeed})
if err := ValidateSeedDir(os.DirFS(dir)); err != nil {
t.Fatalf("valid content seed rejected: %v", err)
}
cases := map[string]map[string]string{
"older host admitted": {"plugin.mod": contentMod(">=0.3.8 <0.4.0"), "seed/seed.json": contentSeed},
"no constraint": {"plugin.mod": "[plugin]\nname = \"x\"\nversion = \"0.1.0\"\n", "seed/seed.json": `{"posts":[{"key":"a","slug":"a","title":"A","body":[{}]}]}`},
"page seo needs gate": {"plugin.mod": contentMod(">=0.3.8"), "seed/seed.json": `{"pages":[{"slug":"/","title":"Home","seo":{"meta_title":"x"}}]}`},
"post without body": {"plugin.mod": contentMod(">=0.3.11"), "seed/seed.json": `{"posts":[{"key":"a","slug":"a","title":"A"}]}`},
"post nested slug": {"plugin.mod": contentMod(">=0.3.11"), "seed/seed.json": `{"posts":[{"key":"a","slug":"blog/a","title":"A","body":[{}]}]}`},
"post bad date": {"plugin.mod": contentMod(">=0.3.11"), "seed/seed.json": `{"posts":[{"key":"a","slug":"a","title":"A","body":[{}],"post_date":"20 Nov"}]}`},
"duplicate post key": {"plugin.mod": contentMod(">=0.3.11"), "seed/seed.json": `{"posts":[{"key":"a","slug":"a","title":"A","body":[{}]},{"key":"a","slug":"b","title":"B","body":[{}]}]}`},
"master without key": {"plugin.mod": contentMod(">=0.3.11"), "seed/seed.json": `{"master_pages":[{"template":"coffee","blocks":[{"content":{"a":1}}]}]}`},
"preset without id": {"plugin.mod": contentMod(">=0.3.11"), "seed/seed.json": `{"theme_preset":{"template":"coffee"}}`},
"list without slug": {"plugin.mod": contentMod(">=0.3.11"), "seed/seed.json": `{"mailing_lists":[{"name":"News"}]}`},
"template bad table": {"plugin.mod": contentMod(">=0.3.11"), "seed/seed.json": `{"email_templates":[{"name":"T","subject":"S","body":"B","table":"nope"}]}`},
}
for name, files := range cases {
t.Run(name, func(t *testing.T) {
dir := t.TempDir()
writeFixture(t, dir, files)
if err := ValidateSeedDir(os.DirFS(dir)); err == nil {
t.Fatal("accepted")
}
})
}
}
func TestConstraintExcludesBelow(t *testing.T) {
for c, want := range map[string]bool{
">=0.3.11 <0.4.0": true, ">0.3.11": true, "=0.3.12": true, ">=v0.4.0": true,
">=0.3.10 <0.4.0": false, "<0.4.0": false, "": false,
} {
if got := constraintExcludesBelow(c, SeedContentMinSDK); got != want {
t.Errorf("%q: got %v want %v", c, got, want)
}
}
}

View File

@ -0,0 +1,48 @@
# Seed content sections are validated and gated by SDK version
Status: accepted (WO-092, 2026-11-20)
The website factory compiles a site plugin whose seed must leave a complete
site with no AI pass: the theme's master page navbar and footer carry the
business, the chosen colour preset is active, every page has a meta title and
description, the blog has posts, the newsletter has its list and a contact form
sends its confirmation email. CMS ADR 0213 adds the seed sections that express
this: `master_pages`, `theme_preset`, `mailing_lists`, `posts`,
`email_templates`, and the page fields `seo` and `replace_existing`. Workflow
email steps gain `sender_name` and `reply_to` in `seed/workflows.json`.
A host that predates those sections parses `seed.json` without error and
ignores them, so it would apply half a site. The host already refuses an
artifact whose `compatibility.plugin_sdk` constraint its own plugin SDK version
does not satisfy (ADR 0005). That is the gate: hosts built with this release
apply the new sections, and an artifact that uses them must exclude every
older host.
Decision: `ValidateSeedDir` checks the new sections structurally. A master page
entry needs a template and blocks with a key and content; a theme preset needs
a template and a preset; mailing lists need unique slugs; posts need a unique
key and single-segment slug, a title, a body, `published` or `draft` status
and a `YYYY-MM-DD` date; email templates need a unique name, a subject and a
body, and a `table` must name a declared data table. When a seed uses any of
these, `plugin.mod` must declare a `plugin_sdk` constraint with a lower bound
at or above `SeedContentMinSDK` (`v0.3.11`), so the CMS loader of an older host
refuses the artifact with a message naming the constraint before anything is
applied. The CLI and the orchestrator inherit the check through
`BuildCodelessFS`.
Validating only in the CMS was rejected because an older CMS cannot run new
validation; the refusal has to come from data the old host already reads. A
separate seed schema version field was rejected for the same reason: an older
host would ignore it.
Consequences:
- `bnp/codeless.go`: `seedJSON` gains the sections, `validateSeedContent`,
`constraintExcludesBelow`, `SeedContentMinSDK`.
- Hosts must build with this SDK release or later to apply the sections.
- A seed that uses them with `plugin_sdk = ">=0.3.8 <0.4.0"` no longer builds.
Keywords: seed.json, ValidateSeedDir, master_pages, theme_preset,
mailing_lists, posts, email_templates, seo, replace_existing, sender_name,
reply_to, SeedContentMinSDK, constraintExcludesBelow, plugin_sdk,
compatibility, version gate, website factory, WO-092, codeless