Alex Dunmow bec3a43f55 feat(wasmguest): guest capability stubs over host calls (WO-WZ-003)
Every CoreServices interface (core/plugin/deps.go) now has a guest-side stub
that marshals to the WO-WZ-001 capability messages and dispatches through the
generic host_call transport, so plugin service code compiles and runs
unchanged against content.Content, settings.Settings, plugin.PluginBridge, etc.

- core/plugin/wasmguest/caps/: one file per family (17 families, 38 methods),
  a var _ <iface> = (*stub)(nil) compile proof each, and NewCoreServices(call)
  assembling them. The transport is injected (CallFunc) so marshaling is
  natively testable; the wasm shim binds it to CallHost, DESCRIBE probes pass
  nil (capability calls fail cleanly instead of nil-panicking).
- Error mapping wraps AbiError with <family>.<method> context and maps
  DEADLINE_EXCEEDED onto context.DeadlineExceeded.
- RAGService.RegisterContentFetcher stays guest-side (RAGStub) for
  HOOK_RAG_FETCH dispatch; Query/OnContentChanged marshal out. dispatch.go and
  describe.go now source fetchers from the caps RAG stub.
- caps_roundtrip_test.go: fake transport + 76 deterministic golden payloads
  (family_method_{req,resp}.pb) covering 100% of families, plus error-mapping,
  deadline, nil-transport, and guest-side-fetcher tests. WO-WZ-006 replays the
  same goldens to prevent host/guest drift.
- Acceptance: testdata/fixture Load hook calls deps.Content/Settings/Bridge
  unchanged (compiles for wasip1); caps_wasmhost_test.go drives it end-to-end
  through a real wazero module + fake host_call table.
- Disposition table in docs/wasm-abi.md: every member stub | host-side
  (Pool, Interceptors, AppURL/MediaPath, CoreServiceBindings host-side).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-03 14:23:58 +08:00

83 lines
2.8 KiB
Go

// Package main is the WO-WZ-002 acceptance fixture: a minimal plugin with
// one block, one template, and one admin page, compiled to wasm with only
// the wasmguest boilerplate (main.go).
package main
import (
"context"
"fmt"
"io"
"git.dev.alexdunmow.com/block/core/blocks"
"git.dev.alexdunmow.com/block/core/plugin"
"git.dev.alexdunmow.com/block/core/templates"
)
// capReport is populated by the Load hook from capability calls, then
// surfaced through the greeting block ({"report": true}). It is the runtime
// proof that deps.Content / deps.Settings / deps.Bridge cross the wasm ABI
// end-to-end through the guest capability stubs (WO-WZ-003 acceptance).
var capReport string
// Registration is the fixture's plugin registration — the same shape a .so
// plugin exports, untouched by the wasm migration.
var Registration = plugin.PluginRegistration{
Name: "wasmfixture",
Version: "0.0.1",
Register: func(tr templates.TemplateRegistry, br blocks.BlockRegistry) error {
br.Register(blocks.BlockMeta{
Key: "greeting",
Title: "Greeting",
Description: "Renders a greeting",
Category: blocks.CategoryContent,
}, func(ctx context.Context, content map[string]any) string {
if boom, _ := content["boom"].(bool); boom {
panic("fixture kaboom")
}
if report, _ := content["report"].(bool); report {
return "<p>capreport:" + capReport + "</p>"
}
name, _ := content["name"].(string)
if name == "" {
name = "world"
}
return fmt.Sprintf("<p>hello %s</p>", name)
})
return tr.Register("wasmfixture-page", func(ctx context.Context, doc map[string]any) templates.HTMLComponent {
title, _ := doc["title"].(string)
return htmlString("<!doctype html><title>" + title + "</title>")
})
},
// Load exercises the guest capability stubs exactly as a real plugin's
// service code does — unchanged calls against deps.Content / deps.Settings
// / deps.Bridge — proving they compile and cross the ABI for wasip1.
Load: func(deps plugin.CoreServices) error {
page, err := deps.Content.GetPage(context.Background(), "about")
if err != nil {
capReport = "content-err:" + err.Error()
return nil
}
site, err := deps.Settings.GetSiteSettings(context.Background())
if err != nil {
capReport = "settings-err:" + err.Error()
return nil
}
svc := deps.Bridge.GetService("other", "search") // cannot cross; expected nil
capReport = fmt.Sprintf("page=%s site=%v bridge_nil=%t",
page.Title, site["site_name"], svc == nil)
return nil
},
AdminPages: func() []plugin.AdminPage {
return []plugin.AdminPage{{
Key: "wasmfixture", Title: "Wasm Fixture", Icon: "flask", Route: "/admin/wasmfixture",
}}
},
}
type htmlString string
func (s htmlString) Render(_ context.Context, w io.Writer) error {
_, err := io.WriteString(w, string(s))
return err
}