From ab21eb4e888c1e76ed36ff8c231d437753bd4a29 Mon Sep 17 00:00:00 2001 From: Alex Dunmow Date: Thu, 19 Nov 2026 23:31:10 +0800 Subject: [PATCH] Build codeless artifacts through the pluginsdk packer The codeless build, declarative validation and tar.zst packer moved to block/pluginsdk/bnp (v0.3.10). ninja plugin build and publish call sdkbnp.BuildCodeless; the wasm build keeps DESCRIBE extraction and uses the SDK helpers over os.DirFS. weareblissful and the wasm fixture build to the same sha256 as before. ADR 0003. Co-Authored-By: Claude Fable 5.2 --- CLAUDE.md | 2 +- cmd/ninja/cmd/plugin.go | 7 +- cmd/ninja/cmd/plugin_build.go | 9 +- cmd/ninja/cmd/plugin_build_test.go | 7 +- ...odeless-builds-use-the-pluginsdk-packer.md | 28 + go.mod | 6 +- go.sum | 5 +- internal/bnp/build.go | 84 +-- internal/bnp/codeless.go | 504 +----------------- internal/bnp/codeless_test.go | 75 +-- internal/bnp/load_once_test.go | 7 +- internal/bnp/mcp_tools_test.go | 7 +- internal/bnp/mixed_test.go | 79 --- internal/bnp/pack.go | 130 +---- internal/bnp/screenshots.go | 121 ----- internal/bnp/screenshots_test.go | 152 ------ internal/bnp/verify.go | 21 +- 17 files changed, 116 insertions(+), 1128 deletions(-) create mode 100644 docs/adr/0003-codeless-builds-use-the-pluginsdk-packer.md delete mode 100644 internal/bnp/mixed_test.go delete mode 100644 internal/bnp/screenshots.go delete mode 100644 internal/bnp/screenshots_test.go diff --git a/CLAUDE.md b/CLAUDE.md index 0e3b8d4..c147c67 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -38,7 +38,7 @@ make safety-check # cd ../check-safety && go run . ../cli ## `.bnp` artifacts A `.bnp` is a **zstd-compressed tar** (NOT gzip — `tar tzf` won't read it; use -`tar tf` or `ninja plugin verify`). `internal/bnp` owns build+verify; wasm +`tar tf` or `ninja plugin verify`). `internal/bnp` owns the wasm build and verify; the codeless build, declarative validation and the packer live in `block/pluginsdk/bnp` (ADR 0003); wasm plugins and codeless plugins/themes both pack into one. Optionally packs a root `preview.png` and a `screenshots/` dir (validated: png/jpg/jpeg/webp, ≤8MB/file, ≤12 files, no subdirs). `internal/shot` renders pages via chromedp for diff --git a/cmd/ninja/cmd/plugin.go b/cmd/ninja/cmd/plugin.go index b691f9c..31fcfdb 100644 --- a/cmd/ninja/cmd/plugin.go +++ b/cmd/ninja/cmd/plugin.go @@ -21,6 +21,7 @@ import ( "git.dev.alexdunmow.com/block/cli/internal/bnp" "git.dev.alexdunmow.com/block/cli/internal/creds" "git.dev.alexdunmow.com/block/cli/internal/orchclient" + sdkbnp "git.dev.alexdunmow.com/block/pluginsdk/bnp" ) func newPluginCmd() *cobra.Command { @@ -771,11 +772,11 @@ func newPluginPublishCmd() *cobra.Command { if err != nil { return err } - var res *bnp.BuildResult + var res *sdkbnp.BuildResult if isCodeless { - res, err = bnp.BuildCodeless(context.Background(), bnp.BuildOptions{Dir: "."}) + res, err = sdkbnp.BuildCodeless(sdkbnp.BuildOptions{Dir: "."}) } else { - res, err = bnp.Build(context.Background(), bnp.BuildOptions{Dir: "."}) + res, err = bnp.Build(context.Background(), sdkbnp.BuildOptions{Dir: "."}) } if err != nil { return fmt.Errorf("build .bnp: %w", err) diff --git a/cmd/ninja/cmd/plugin_build.go b/cmd/ninja/cmd/plugin_build.go index 9708c99..c06ab07 100644 --- a/cmd/ninja/cmd/plugin_build.go +++ b/cmd/ninja/cmd/plugin_build.go @@ -8,6 +8,7 @@ import ( "github.com/spf13/cobra" "git.dev.alexdunmow.com/block/cli/internal/bnp" + sdkbnp "git.dev.alexdunmow.com/block/pluginsdk/bnp" ) func newPluginBuildCmd() *cobra.Command { @@ -34,11 +35,11 @@ plus wazero. This replaces the in-container .so compile.`, if codeless && !isCodeless { return fmt.Errorf("--codeless asserted but %s contains Go source — a codeless plugin has no code (delete the Go or drop the flag)", dir) } - var res *bnp.BuildResult + var res *sdkbnp.BuildResult if isCodeless { - res, err = bnp.BuildCodeless(context.Background(), bnp.BuildOptions{Dir: dir, Output: output}) + res, err = sdkbnp.BuildCodeless(sdkbnp.BuildOptions{Dir: dir, Output: output}) } else { - res, err = bnp.Build(context.Background(), bnp.BuildOptions{Dir: dir, Output: output}) + res, err = bnp.Build(context.Background(), sdkbnp.BuildOptions{Dir: dir, Output: output}) } if err != nil { return err @@ -74,7 +75,7 @@ plugin.mod. Prints a named reason for each malformed class.`, return cmd } -func printBuildSummary(r *bnp.BuildResult) { +func printBuildSummary(r *sdkbnp.BuildResult) { hooks := "(none)" if len(r.Hooks) > 0 { hooks = strings.Join(r.Hooks, ", ") diff --git a/cmd/ninja/cmd/plugin_build_test.go b/cmd/ninja/cmd/plugin_build_test.go index 2477938..893a999 100644 --- a/cmd/ninja/cmd/plugin_build_test.go +++ b/cmd/ninja/cmd/plugin_build_test.go @@ -11,6 +11,7 @@ import ( abiv1 "git.dev.alexdunmow.com/block/pluginsdk/abi/v1" "git.dev.alexdunmow.com/block/cli/internal/bnp" + sdkbnp "git.dev.alexdunmow.com/block/pluginsdk/bnp" "github.com/klauspost/compress/zstd" "google.golang.org/protobuf/proto" ) @@ -39,7 +40,7 @@ func TestPluginBuildAndVerify(t *testing.T) { } out := filepath.Join(t.TempDir(), "wasmfixture-0.0.1.bnp") - res, err := bnp.Build(context.Background(), bnp.BuildOptions{ + res, err := bnp.Build(context.Background(), sdkbnp.BuildOptions{ Dir: fixtureDir(t, "fixture"), Output: out, }) @@ -104,7 +105,7 @@ func TestPluginBuildDefaultOutputName(t *testing.T) { if err := os.Chdir(tmp); err != nil { t.Fatalf("chdir: %v", err) } - res, err := bnp.Build(context.Background(), bnp.BuildOptions{Dir: dir}) + res, err := bnp.Build(context.Background(), sdkbnp.BuildOptions{Dir: dir}) if err != nil { t.Fatalf("build: %v", err) } @@ -123,7 +124,7 @@ func TestPluginBuildCapabilityInRegisterFails(t *testing.T) { t.Skip("compiles a wasm module; skipped in -short") } out := filepath.Join(t.TempDir(), "capfixture.bnp") - _, err := bnp.Build(context.Background(), bnp.BuildOptions{ + _, err := bnp.Build(context.Background(), sdkbnp.BuildOptions{ Dir: fixtureDir(t, "capfixture"), Output: out, }) diff --git a/docs/adr/0003-codeless-builds-use-the-pluginsdk-packer.md b/docs/adr/0003-codeless-builds-use-the-pluginsdk-packer.md new file mode 100644 index 0000000..e2925d1 --- /dev/null +++ b/docs/adr/0003-codeless-builds-use-the-pluginsdk-packer.md @@ -0,0 +1,28 @@ +# Codeless builds use the pluginsdk packer + +The orchestrator's website factory needs to pack a codeless `.bnp` in its own +process, and Go's `internal` rule kept the CLI's packer out of reach. The +codeless build, the declarative validation and the tar.zst packer moved to the +public `bnp` package of `block/pluginsdk` (pluginsdk ADR 0009, v0.3.10). + +Decision: `ninja plugin build` and `ninja plugin publish` call +`sdkbnp.BuildCodeless` for a codeless repo. The wasm build in `internal/bnp` +keeps compiling the guest and extracting its manifest through DESCRIBE, then +uses the same SDK helpers over `os.DirFS` for `manifest.yaml` folding, +`blocks/` and `seed/` validation, directory collection, presentation members +and packing. `BuildOptions` and `BuildResult` come from the SDK. Repo +classification (`IsCodelessRepo`), `ninja plugin verify` and +`CodelessHookViolation` stay in the CLI. + +Consequences: + +- The CLI and the orchestrator produce identical bytes for the same tree. The + weareblissful site and the wasm test fixture build to the same sha256 before + and after the change. +- The pluginsdk pin moves from v0.3.7 to v0.3.10. +- Codeless validation and packing tests live in pluginsdk; the CLI keeps the + build plus `Verify` round trips. + +Keywords: plugin CLI, codeless, BuildCodeless, BuildCodelessFS, pluginsdk bnp, +sdkbnp, packArtifact, Pack, os.DirFS, manifest.yaml, BNP, tar.zst, +byte-identical build, website factory, WO-076 diff --git a/go.mod b/go.mod index 75e6902..f052246 100644 --- a/go.mod +++ b/go.mod @@ -5,14 +5,13 @@ go 1.26.4 require ( connectrpc.com/connect v1.20.0 git.dev.alexdunmow.com/block/core v0.20.6 - git.dev.alexdunmow.com/block/pluginsdk v0.3.7 + git.dev.alexdunmow.com/block/pluginsdk v0.3.10 github.com/chromedp/cdproto v0.0.0-20260321001828-e3e3800016bc github.com/chromedp/chromedp v0.15.1 github.com/klauspost/compress v1.18.6 github.com/spf13/cobra v1.10.2 github.com/tetratelabs/wazero v1.12.0 google.golang.org/protobuf v1.36.11 - gopkg.in/yaml.v3 v3.0.1 ) require ( @@ -27,11 +26,10 @@ require ( github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/pgx/v5 v5.10.0 // indirect - github.com/kr/text v0.2.0 // indirect - github.com/rogpeppe/go-internal v1.16.0 // indirect github.com/spf13/pflag v1.0.9 // indirect golang.org/x/mod v0.37.0 // indirect golang.org/x/net v0.56.0 // indirect golang.org/x/sys v0.46.0 // indirect golang.org/x/text v0.38.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 6408a36..e836e43 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ connectrpc.com/connect v1.20.0 h1:6TNDAB+WeNd2uolWNlYczB5E0KNNaVMNUEx8JEUsPmQ= connectrpc.com/connect v1.20.0/go.mod h1:A2ygJrukXwWy32vkCAAHNVguZrqZ+jeZ9rGRnGR4dN4= git.dev.alexdunmow.com/block/core v0.20.6 h1:/f6zP1SKz44ftE5hVDOU4hJvl6TLz3vufGSkqvc2cSg= git.dev.alexdunmow.com/block/core v0.20.6/go.mod h1:G3MNnwiM4i9roLamhs9DAlVO2mXiYAlTRQoHPpw1AmI= -git.dev.alexdunmow.com/block/pluginsdk v0.3.7 h1:PUT1YElhXre43m1UejDabSqRPAnx1BV7BEWKpG++7Rs= -git.dev.alexdunmow.com/block/pluginsdk v0.3.7/go.mod h1:Z+eG+WZxAP0jfreLqlGcc0kkWKt8RWevzWyWn8d+dhM= +git.dev.alexdunmow.com/block/pluginsdk v0.3.10 h1:Hh864yQnXAAMMh9uCuOjCS4ZrXwHh1nnW15hksrWNkY= +git.dev.alexdunmow.com/block/pluginsdk v0.3.10/go.mod h1:SAXv5Lv8mJIZHjAmDSe1LnMsftEdmVIy32T5BpGfgwg= github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk= github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= github.com/chromedp/cdproto v0.0.0-20260321001828-e3e3800016bc h1:wkN/LMi5vc60pBRWx6qpbk/aEvq3/ZVNpnMvsw8PVVU= @@ -13,7 +13,6 @@ github.com/chromedp/chromedp v0.15.1/go.mod h1:CdTHtUqD/dqaFw/cvFWtTydoEQS44wLBu github.com/chromedp/sysutil v1.1.0 h1:PUFNv5EcprjqXZD9nJb9b/c9ibAbxiYo4exNWZyipwM= github.com/chromedp/sysutil v1.1.0/go.mod h1:WiThHUdltqCNKGc4gaU50XgYjwjYIhKWoHGPTUfWTJ8= github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= -github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/internal/bnp/build.go b/internal/bnp/build.go index 913afa9..3a12851 100644 --- a/internal/bnp/build.go +++ b/internal/bnp/build.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "fmt" + "io/fs" "os" "os/exec" "path/filepath" @@ -12,17 +13,14 @@ import ( "strconv" abiv1 "git.dev.alexdunmow.com/block/pluginsdk/abi/v1" + sdkbnp "git.dev.alexdunmow.com/block/pluginsdk/bnp" "git.dev.alexdunmow.com/block/pluginsdk/egress" core "git.dev.alexdunmow.com/block/pluginsdk/plugin" "google.golang.org/protobuf/proto" ) -// Required top-level artifact members (mirrors the CMS reader). -const ( - fileWasm = "plugin.wasm" - fileMod = "plugin.mod" - fileManifest = "manifest.pb" -) +// fileWasm is the guest module member of a wasm artifact (mirrors the CMS reader). +const fileWasm = "plugin.wasm" // minGoMajor/minGoMinor is the lowest Go toolchain that can emit reactor-mode // (`-buildmode=c-shared`) wasip1 modules the guest shim relies on. @@ -31,44 +29,10 @@ const ( minGoMinor = 24 ) -// BuildOptions configures Build. -type BuildOptions struct { - // Dir is the plugin repo root (holds plugin.mod and the main Go package). - Dir string - // Output is the destination .bnp path. Empty → "-.bnp" in - // the current working directory. - Output string -} - -// BuildResult summarizes a produced artifact for the CLI summary table. -type BuildResult struct { - OutputPath string - Name string - Version string - // Codeless marks a declarative artifact (no plugin.wasm, WO-WZ-020). - Codeless bool - WasmBytes int64 - ManifestBytes int64 - ArtifactBytes int64 // on-disk .bnp size - UncompBytes int64 // sum of packed file sizes - BlockCount int - TemplateCount int - AdminPages int - JobTypes int - Hooks []string - DataDir bool - IncludedDirs []string - // PreviewIncluded/ScreenshotCount report the packed presentation members - // (root preview.png + screenshots/); the registry ingests these into the - // plugin's gallery at publish. - PreviewIncluded bool - ScreenshotCount int -} - // Build compiles the plugin in opts.Dir to wasm, extracts its manifest, and // packs a .bnp. No Docker/podman: the whole pipeline is the local Go // toolchain + wazero + tar.zst. -func Build(ctx context.Context, opts BuildOptions) (*BuildResult, error) { +func Build(ctx context.Context, opts sdkbnp.BuildOptions) (*sdkbnp.BuildResult, error) { dir := opts.Dir if dir == "" { dir = "." @@ -78,8 +42,8 @@ func Build(ctx context.Context, opts BuildOptions) (*BuildResult, error) { return nil, fmt.Errorf("resolve dir: %w", err) } - modPath := filepath.Join(dir, fileMod) - modBytes, err := os.ReadFile(modPath) + fsys := os.DirFS(dir) + modBytes, err := fs.ReadFile(fsys, sdkbnp.FileMod) if err != nil { return nil, fmt.Errorf("read plugin.mod (is %s a plugin repo?): %w", dir, err) } @@ -160,9 +124,9 @@ func Build(ctx context.Context, opts BuildOptions) (*BuildResult, error) { // manifest.pb (not packed separately, same as codeless). The templates/ // dir carrying the .ninjatpl sources already rides the optional-dir // pack below. A repo with NO manifest.yaml is a no-op here - // (applyManifestYAML returns nil on os.IsNotExist), so an existing + // (ApplyManifestYAML returns nil on fs.ErrNotExist), so an existing // pure-wasm plugin builds byte-for-byte as before. - if err := applyManifestYAML(dir, manifest); err != nil { + if err := sdkbnp.ApplyManifestYAML(fsys, manifest); err != nil { return nil, err } if err := ValidateLoadOnceManifest(manifest); err != nil { @@ -178,33 +142,33 @@ func Build(ctx context.Context, opts BuildOptions) (*BuildResult, error) { } // 5. Assemble artifact entries. - entries := []packEntry{ - {ArtifactPath: fileWasm, Source: wasmPath}, - {ArtifactPath: fileMod, Source: modPath}, - {ArtifactPath: fileManifest, Data: manifestBytes}, + entries := []sdkbnp.Entry{ + {ArtifactPath: fileWasm, Data: wasm}, + {ArtifactPath: sdkbnp.FileMod, Data: modBytes}, + {ArtifactPath: sdkbnp.FileManifest, Data: manifestBytes}, } var includedDirs []string // dir-name → source subpath. web ships the Module Federation build output // (web/dist) flattened under web/ so the reader's dirExists("web") fires. optional := []struct{ artifact, src string }{ - {dirBlocks, dirBlocks}, - {dirTemplates, dirTemplates}, - {dirSeed, dirSeed}, + {sdkbnp.DirBlocks, sdkbnp.DirBlocks}, + {sdkbnp.DirTemplates, sdkbnp.DirTemplates}, + {sdkbnp.DirSeed, sdkbnp.DirSeed}, {"migrations", "migrations"}, {"schemas", "schemas"}, {"assets", "assets"}, - {"web", filepath.Join("web", "dist")}, + {"web", "web/dist"}, } // Declarative dirs ride along on wasm artifacts too (a reduced plugin may // mix definition-backed blocks with logic); validate them identically. - if _, err := validateBlocksDir(dir); err != nil { + if _, err := sdkbnp.ValidateBlocksDir(fsys); err != nil { return nil, err } - if err := validateSeedDir(dir); err != nil { + if err := sdkbnp.ValidateSeedDir(fsys); err != nil { return nil, err } for _, o := range optional { - dirEntries, has, dErr := collectDir(filepath.Join(dir, o.src), o.artifact) + dirEntries, has, dErr := sdkbnp.CollectDir(fsys, o.src, o.artifact) if dErr != nil { return nil, dErr } @@ -213,13 +177,13 @@ func Build(ctx context.Context, opts BuildOptions) (*BuildResult, error) { includedDirs = append(includedDirs, o.artifact) } } - presEntries, previewIncluded, screenshotCount, err := presentationEntries(dir) + presEntries, previewIncluded, screenshotCount, err := sdkbnp.PresentationEntries(fsys) if err != nil { return nil, err } entries = append(entries, presEntries...) if screenshotCount > 0 { - includedDirs = append(includedDirs, dirScreenshots) + includedDirs = append(includedDirs, sdkbnp.DirScreenshots) } // 6. Pack. @@ -227,7 +191,7 @@ func Build(ctx context.Context, opts BuildOptions) (*BuildResult, error) { if outPath == "" { outPath = fmt.Sprintf("%s-%s.bnp", mod.Plugin.Name, mod.Plugin.Version) } - uncomp, err := packArtifact(outPath, entries) + uncomp, err := packArtifact(outPath, fsys, entries) if err != nil { return nil, err } @@ -238,7 +202,7 @@ func Build(ctx context.Context, opts BuildOptions) (*BuildResult, error) { } wasmInfo, _ := os.Stat(wasmPath) - return &BuildResult{ + return &sdkbnp.BuildResult{ OutputPath: outPath, Name: manifest.GetName(), Version: manifest.GetVersion(), diff --git a/internal/bnp/codeless.go b/internal/bnp/codeless.go index 3d4ca82..f9d7741 100644 --- a/internal/bnp/codeless.go +++ b/internal/bnp/codeless.go @@ -1,157 +1,18 @@ package bnp import ( - "context" - "encoding/json" "fmt" "os" "path/filepath" "strings" - abiv1 "git.dev.alexdunmow.com/block/pluginsdk/abi/v1" - core "git.dev.alexdunmow.com/block/pluginsdk/plugin" - "google.golang.org/protobuf/proto" - "gopkg.in/yaml.v3" + sdkbnp "git.dev.alexdunmow.com/block/pluginsdk/bnp" ) -// Codeless artifacts (WO-WZ-020): a .bnp with NO plugin.wasm — pure -// declaration the host runs. Classification is by repo shape: a plugin repo -// with no Go source builds codeless; a repo with Go builds wasm as always -// (a converted repo DELETES its Go — that's the point). Both artifact kinds -// may carry blocks/, templates/, and seed/; codeless just has nothing else. - -// Declarative artifact dirs shared by both build paths. -const ( - dirBlocks = "blocks" - dirTemplates = "templates" - dirSeed = "seed" -) - -// manifestYAMLName is the optional root-level declarative manifest source for -// codeless plugins (the counterpart of what DESCRIBE captures from Go code). -const manifestYAMLName = "manifest.yaml" - -// manifestYAML mirrors the declarative PluginManifest fields a codeless -// plugin can set. File-valued keys are paths relative to the repo root. -type manifestYAML struct { - ThemePresets string `yaml:"theme_presets"` // JSON file - BundledFonts string `yaml:"bundled_fonts"` // JSON file - SettingsSchema string `yaml:"settings_schema"` // JSON file - MasterPages string `yaml:"master_pages"` // JSON file ([]masterPageJSON) - RequiredIconPacks []string `yaml:"required_icon_packs"` - SystemTemplates []struct { - Key string `yaml:"key"` - Title string `yaml:"title"` - Description string `yaml:"description"` - } `yaml:"system_templates"` - PageTemplates []struct { - System string `yaml:"system"` - Key string `yaml:"key"` - Title string `yaml:"title"` - Description string `yaml:"description"` - Slots []string `yaml:"slots"` - } `yaml:"page_templates"` - // TemplateOverrides declare theme-scoped overrides of builtin block - // rendering. Source convention: templates/overrides/