mirror of
https://github.com/J3vb/OwnCord.git
synced 2026-09-03 03:50:00 +03:00
Phase B Step 8 (OpenTelemetry) and Phase C Step 9 (Wazero plugin runtime)
were structurally scaffolded but the tagged builds were placeholders that
errored at runtime. This commit lands the real implementations behind the
existing build tags, plus three review passes worth of fixes across the
plugin admin handler, plugin registry, telemetry adapter, and Solid client.
Telemetry (Phase B Step 8)
- Add real go.opentelemetry.io/otel{,/sdk,/exporters/{prometheus,otlp...}}
modules to go.mod plus contrib/instrumentation/net/http/otelhttp.
- Replace the telemetry_otel.go skeleton with a working Provider that
wires Prometheus + OTLP/gRPC exporters, otelhttp middleware, span and
meter adapters, and an idempotent Shutdown.
- AppMetrics cache is now reset *before* SetGlobal to close a race where
a concurrent NewAppMetrics() could observe a swapped provider but read
stale no-op instruments.
- Init releases the trace provider on a later prometheus exporter
failure so Init never leaks gRPC connections.
- convertAttrs handles int32/uint/uint32/uint64/float32 explicitly;
uint64 values that exceed math.MaxInt64 fall back to a STRING attr
rather than wrapping into a negative int64 and corrupting metrics.
- Tests under -tags otel cover the prometheus scrape, span lifecycle,
histogram recording, shutdown idempotency, AppMetrics rebind, and
the uint64 overflow fallback.
Plugin runtime (Phase C Step 9)
- Add github.com/tetratelabs/wazero v1.11.0 to go.mod.
- platformInit creates a shared wazero.Runtime with WASI preview1
pre-instantiated; activateWithRuntime compiles + instantiates each
plugin module under that runtime; platformDeactivate closes per-
plugin modules without tearing down the runtime.
- DisablePlugin now calls platformDeactivate so the wazero module is
freed immediately instead of leaking until registry Close.
- activate() captures runtimePlatform under r.mu.RLock and passes it as
a parameter to activateWithRuntime; the call no longer re-reads the
field, closing a race with concurrent Close.
- invokeCommand calls the plugin's command_dispatch export when
present; missing/broken exports return a user-facing diagnostic
instead of crashing the dispatcher.
- Tests under -tags wazero cover registry creation, module compilation,
re-enable after disable (verifies the leak fix), close-twice safety,
invalid wasm rejection, and DispatchCommand with a missing export.
Fixture is a 41-byte embedded add.wasm; no external asset required.
Plugin admin handler hardening
- /api/v1/admin/plugins/install now rejects uploads whose multipart
Content-Type is not application/zip|x-zip-compressed|octet-stream
(415) and uploads whose body lacks the PK\\x03\\x04 / PK\\x05\\x06
zip magic (400). The 16 MiB cap and registry-side zip-slip / symlink
/ size-bomb defences are still applied as before.
- New plugins_handler_test.go covers list-empty, install-503-when-nil,
content-type rejection, magic rejection, happy path, lifecycle 503,
invalid id, and isZipContentType / hasZipMagic helpers.
Solid client (Phase B Step 6) cleanup
- vitest.config.ts now wires vite-plugin-solid and broadens the test
glob to include src/**/*.test.tsx so Badge.test.tsx is actually
discovered (it was silently skipped).
- pluginBridge.ts targets postMessage at window.location.origin
instead of "*", and exposes a destroy() that detaches the message
listener and clears mounted frames.
- solidMount.ts imports the JSX type from "solid-js" instead of
"solid-js/web" (the latter does not re-export it), unblocking
npx tsc --noEmit.
Build/test status
- go build succeeds on default, -tags otel, -tags wazero, and
-tags otel,wazero.
- go test passes on every tag combination across telemetry, plugin,
api, ws, service, store, and the rest of the tree.
- Client: npx tsc --noEmit clean; vitest 3188/3188 across 112 files.
PHASE_BC_LOCAL_TODO.md is updated to mark the OTel modules + real Init,
the wazero module + real platformInit, and the test coverage that
landed in this commit as completed.
https://claude.ai/code/session_01AZni6CDSQeu67WSWY1YCDX
229 lines
6.9 KiB
Go
229 lines
6.9 KiB
Go
//go:build wazero
|
|
|
|
// Phase C Step 9 — Wazero runtime integration tests. Only compiled with
|
|
// `-tags wazero`, alongside sandbox_wazero.go. These tests exercise the
|
|
// behaviours the default build cannot:
|
|
//
|
|
// - NewRegistry stands up a real wazero.Runtime
|
|
// - activateWithRuntime compiles a real .wasm module and tracks the
|
|
// resulting instance on the Instance struct
|
|
// - EnablePlugin takes a manifest from a PluginStore row through
|
|
// activation end-to-end
|
|
// - invokeCommand gracefully returns a user-facing error when the plugin
|
|
// does not export command_dispatch
|
|
// - Close tears the runtime down without panicking
|
|
//
|
|
// Test fixture: the 41-byte `add.wasm` module from the wazero examples, a
|
|
// trivial module that exports `add(i32,i32) -> i32`. It does NOT export
|
|
// `command_dispatch`, which is intentional — the command path must handle
|
|
// that case.
|
|
package plugin
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/owncord/server/store"
|
|
)
|
|
|
|
// addWASM is the bytes of a minimal (module (func (export "add") ... )).
|
|
// Verified against the wazero examples fixture; 41 bytes. Using a literal
|
|
// here avoids dragging a binary asset into the repo.
|
|
var addWASM = []byte{
|
|
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
|
|
0x01, 0x07, 0x01, 0x60, 0x02, 0x7f, 0x7f, 0x01,
|
|
0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x07, 0x01,
|
|
0x03, 0x61, 0x64, 0x64, 0x00, 0x00, 0x0a, 0x09,
|
|
0x01, 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6a,
|
|
0x0b,
|
|
}
|
|
|
|
func writeTestPlugin(t *testing.T, root, name string, manifest string, wasmBytes []byte) {
|
|
t.Helper()
|
|
pluginDir := filepath.Join(root, name)
|
|
if err := os.MkdirAll(pluginDir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(pluginDir, "plugin.json"), []byte(manifest), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(pluginDir, "hello.wasm"), wasmBytes, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func newWazeroTestRegistry(t *testing.T, dir string) (*Registry, store.PluginStore) {
|
|
t.Helper()
|
|
mem := store.NewMemStore()
|
|
reg, err := NewRegistry(Config{
|
|
Directory: dir,
|
|
MaxMemoryMB: 16,
|
|
CPUBudgetMs: 100,
|
|
Store: mem,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("NewRegistry: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = reg.Close(context.Background()) })
|
|
return reg, mem
|
|
}
|
|
|
|
func TestWazeroRegistryCreatesRuntime(t *testing.T) {
|
|
reg, _ := newWazeroTestRegistry(t, t.TempDir())
|
|
if reg.runtimePlatform == nil {
|
|
t.Fatal("expected wazero runtime to be wired in tagged build")
|
|
}
|
|
if reg.platformClose == nil {
|
|
t.Fatal("expected platformClose to be set")
|
|
}
|
|
}
|
|
|
|
func TestWazeroActivateCompilesModule(t *testing.T) {
|
|
dir := t.TempDir()
|
|
manifest := `{"name":"hello","version":"0.1.0","entrypoint":"hello.wasm","permissions":["commands"]}`
|
|
writeTestPlugin(t, dir, "hello", manifest, addWASM)
|
|
|
|
reg, mem := newWazeroTestRegistry(t, dir)
|
|
ctx := context.Background()
|
|
if err := reg.LoadAll(ctx); err != nil {
|
|
t.Fatalf("LoadAll: %v", err)
|
|
}
|
|
|
|
rows, err := mem.ListPlugins(ctx)
|
|
if err != nil || len(rows) != 1 {
|
|
t.Fatalf("ListPlugins: rows=%+v err=%v", rows, err)
|
|
}
|
|
|
|
// The plugin starts disabled; enable it and confirm the module compiles.
|
|
if err := reg.EnablePlugin(ctx, rows[0].ID); err != nil {
|
|
t.Fatalf("EnablePlugin: %v", err)
|
|
}
|
|
|
|
reg.mu.RLock()
|
|
inst := reg.plugins[rows[0].ID]
|
|
reg.mu.RUnlock()
|
|
if inst == nil {
|
|
t.Fatal("instance not registered after enable")
|
|
}
|
|
if !inst.Enabled {
|
|
t.Fatal("instance should be enabled after EnablePlugin")
|
|
}
|
|
if inst.module == nil {
|
|
t.Fatal("expected inst.module to be populated after activation")
|
|
}
|
|
}
|
|
|
|
func TestWazeroDispatchCommandMissingExport(t *testing.T) {
|
|
dir := t.TempDir()
|
|
manifest := `{"name":"hello","version":"0.1.0","entrypoint":"hello.wasm","permissions":["commands"]}`
|
|
writeTestPlugin(t, dir, "hello", manifest, addWASM)
|
|
|
|
reg, mem := newWazeroTestRegistry(t, dir)
|
|
ctx := context.Background()
|
|
if err := reg.LoadAll(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rows, _ := mem.ListPlugins(ctx)
|
|
if err := reg.EnablePlugin(ctx, rows[0].ID); err != nil {
|
|
t.Fatalf("EnablePlugin: %v", err)
|
|
}
|
|
|
|
reg.mu.RLock()
|
|
inst := reg.plugins[rows[0].ID]
|
|
reg.mu.RUnlock()
|
|
if err := reg.RegisterCommand("hello", inst); err != nil {
|
|
t.Fatalf("RegisterCommand: %v", err)
|
|
}
|
|
|
|
result, ok := reg.DispatchCommand(ctx, 1, 2, "hello", nil)
|
|
if !ok {
|
|
t.Fatal("expected DispatchCommand to return a result")
|
|
}
|
|
if result == nil || result.Reply == "" {
|
|
t.Fatal("expected a non-empty reply when export is missing")
|
|
}
|
|
}
|
|
|
|
func TestWazeroCloseTearsDownRuntime(t *testing.T) {
|
|
dir := t.TempDir()
|
|
manifest := `{"name":"hello","version":"0.1.0","entrypoint":"hello.wasm","permissions":["commands"]}`
|
|
writeTestPlugin(t, dir, "hello", manifest, addWASM)
|
|
|
|
reg, mem := newWazeroTestRegistry(t, dir)
|
|
ctx := context.Background()
|
|
if err := reg.LoadAll(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rows, _ := mem.ListPlugins(ctx)
|
|
if err := reg.EnablePlugin(ctx, rows[0].ID); err != nil {
|
|
t.Fatalf("EnablePlugin: %v", err)
|
|
}
|
|
|
|
if err := reg.Close(ctx); err != nil {
|
|
t.Fatalf("Close: %v", err)
|
|
}
|
|
if reg.runtimePlatform != nil || reg.platformClose != nil {
|
|
t.Fatal("Close should clear platform fields")
|
|
}
|
|
// Calling Close twice must not panic.
|
|
if err := reg.Close(ctx); err != nil {
|
|
t.Fatalf("Close (second): %v", err)
|
|
}
|
|
}
|
|
|
|
func TestWazeroDisablePluginFreesModule(t *testing.T) {
|
|
dir := t.TempDir()
|
|
manifest := `{"name":"hello","version":"0.1.0","entrypoint":"hello.wasm","permissions":["commands"]}`
|
|
writeTestPlugin(t, dir, "hello", manifest, addWASM)
|
|
|
|
reg, mem := newWazeroTestRegistry(t, dir)
|
|
ctx := context.Background()
|
|
if err := reg.LoadAll(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rows, _ := mem.ListPlugins(ctx)
|
|
if err := reg.EnablePlugin(ctx, rows[0].ID); err != nil {
|
|
t.Fatalf("EnablePlugin: %v", err)
|
|
}
|
|
reg.mu.RLock()
|
|
inst := reg.plugins[rows[0].ID]
|
|
reg.mu.RUnlock()
|
|
if inst.module == nil {
|
|
t.Fatal("pre-condition: module should be populated after EnablePlugin")
|
|
}
|
|
if err := reg.DisablePlugin(ctx, rows[0].ID); err != nil {
|
|
t.Fatalf("DisablePlugin: %v", err)
|
|
}
|
|
if inst.module != nil {
|
|
t.Fatal("DisablePlugin must release the wazero module (inst.module != nil)")
|
|
}
|
|
if inst.Enabled {
|
|
t.Fatal("DisablePlugin must clear inst.Enabled")
|
|
}
|
|
// Re-enabling must rebuild a new module.
|
|
if err := reg.EnablePlugin(ctx, rows[0].ID); err != nil {
|
|
t.Fatalf("re-EnablePlugin: %v", err)
|
|
}
|
|
if inst.module == nil {
|
|
t.Fatal("re-Enable should repopulate inst.module")
|
|
}
|
|
}
|
|
|
|
func TestWazeroInvalidWASMFailsActivation(t *testing.T) {
|
|
dir := t.TempDir()
|
|
manifest := `{"name":"brokey","version":"0.1.0","entrypoint":"hello.wasm","permissions":["commands"]}`
|
|
writeTestPlugin(t, dir, "brokey", manifest, []byte("not a wasm"))
|
|
|
|
reg, mem := newWazeroTestRegistry(t, dir)
|
|
ctx := context.Background()
|
|
if err := reg.LoadAll(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rows, _ := mem.ListPlugins(ctx)
|
|
if err := reg.EnablePlugin(ctx, rows[0].ID); err == nil {
|
|
t.Fatal("expected EnablePlugin to fail on invalid WASM")
|
|
}
|
|
}
|