Files
kandev/apps/backend/internal/plugins/runtime/manager_test.go
T
João Salavisa e145d48f22 feat: add plugin system with grpc runtime and native ui plugins (#1742)
* feat: add plugin system with grpc runtime and native ui plugins

- go plugin backends ship as release tarballs (per-platform binaries, checksums) that
  kandev installs from url/upload or filesystem sideload+sync, then spawns and
  supervises via hashicorp/go-plugin (unix socket / loopback tcp+automtls)
- strict typed grpc contract (kandev.plugin.v1): DeliverEvent/InvokeTool/HandleWebhook
  plus a capability-gated host service (state, secrets, emit); author sdk in
  pkg/pluginsdk; packaging cli in cmd/plugin-pack
- native frontend bundles: window.registerKandevPlugin + registry/host loader with
  dynamic routes, nav items, slots and ws handlers; plugins settings page with
  install dialog, sync button and enable/disable/uninstall
- gated behind the plugins feature flag (prod off, dev/e2e on); covered by unit and
  integration tests plus playwright e2e against a real packaged fixture plugin

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(plugins): hydrate base app context on plugin routes

Plugin-owned SPA routes classify as RouteUnknown, so the boot payload shipped
only feature flags — no workspaces, workflows, or kanban context. Native plugin
UI (e.g. host.ui.TaskCreateDialog) then had an empty store. Hydrate the same
base context as the Home route for unknown SPA routes.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(plugins): expose TaskCreateDialog on host.ui

Lets a native plugin open kandev's real create-task modal (prefilled via
initialValues) instead of POSTing directly.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(plugins): harden filesystem/URL sinks against CodeQL findings

Add sink-level guards on the plugin trust boundary: securejoin containment on
archive extraction (zip-slip), plugin-id validation before every fs_store path,
http/https scheme validation on install-from-URL, and non-interpolated ws-handler
error logging.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(plugins): address review findings (races, resource caps, lifecycle)

- serialize per-plugin startup to close a TOCTOU double-spawn race, and let a
  plugin restart after exhausting attempts
- bound plugin startup with an explicit 30s timeout
- reject backslash paths and cap decompressed bytes by actual size in pkgtar;
  only mark a package Signed when a verifier actually ran and passed
- delete plugin_state on uninstall; validate webhook keys against the manifest
  and cap webhook body size; restart an active plugin when a new version installs

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(plugins): sanitize plugin-id path sinks for CodeQL

Pin the plugin id to a single path segment with filepath.Base at recordPath/
configPath and pkgtar.Remove — a barrier the taint analysis recognizes.
Behaviour-identical for valid ids (already validated by safePluginID); clears
the go/path-injection alerts.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(plugins): inline plugin-id validation at path sinks for CodeQL

filepath.Base does not strip '..', so it is not a path-injection barrier. Call
the recognized safePluginID / id-pattern guard inline in Delete/GetConfig/
SetConfig and pkgtar.Remove — the same barrier that already cleared the Get sink.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(plugins): address code-review findings (backend)

- add capability-gating tests for the Host RPC service (was untested)
- close a stop-during-start orphan race so Disable/Uninstall can't leave a
  supervised subprocess behind
- on failed upgrade, remove only the new version dir (not all versions + data);
  restart the prior version
- validate manifest version and reject ids ending in .config (record-file alias)
- make Deliverer.Refresh non-blocking and cancel in-flight delivery on worker stop
- clamp out-of-range webhook status, skip-and-warn corrupt records in List,
  write records atomically, semver-aware sideload version pick

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(plugins): address code-review findings (frontend)

- wrap plugin routes + settings routes in a shared PluginErrorBoundary so a
  throwing plugin page shows a fallback instead of white-screening the SPA
- fix disable/re-enable in one session: keep the cached registration and
  re-run initialize instead of relying on a cached ES-module re-import
- prefix root-relative bundle/style URLs with apiBaseUrl (split-origin/Tauri)
- surface partial-install warnings via toast.warning instead of a bare success
- dedupe/remove injected style links on unload; gate PluginNavItems on the flag

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(plugins): serialize per-plugin lifecycle + guard config writes

- add a per-plugin keyed lock in Service so concurrent Enable/Disable/Install/
  Uninstall/UpdateConfig for one id are serialized (no status-machine race)
- take s.mu in SetConfig/GetConfig and write config atomically
- bound each plugin initialize() with a timeout so one hung bundle can't stall
  the rest of the boot load

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* test(plugins): split service_test.go to satisfy file-length lint

Move the install/URL test group into service_install_test.go; no logic change.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* ci: build and ship the e2e fixture plugin package to E2E shards

The plugin E2E specs' global-setup requires apps/backend/.build/kandev-plugin-e2e-1.0.0.tar.gz.
Build it in the E2E build job, upload it as an artifact, and download it in both
the regular and container shard jobs.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Kandev Agent <agent@kandev.dev>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-17 12:00:26 +01:00

452 lines
15 KiB
Go

package runtime
import (
"context"
"errors"
"os"
"path/filepath"
goruntime "runtime"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/kandev/kandev/internal/plugins/manifest"
"github.com/kandev/kandev/internal/plugins/store"
"github.com/kandev/kandev/pkg/pluginsdk"
)
// fakeHost is a minimal in-memory pluginsdk.Host for end-to-end tests: it
// only implements SetState (with a channel so tests can synchronize on
// delivery without a sleep), and errors on everything else.
type fakeHost struct {
mu sync.Mutex
states map[string]map[string]any
setCh chan struct{}
}
func newFakeHost() *fakeHost {
return &fakeHost{states: map[string]map[string]any{}, setCh: make(chan struct{}, 16)}
}
func (h *fakeHost) GetState(context.Context, string, string, string) (map[string]any, bool, error) {
return nil, false, nil
}
func (h *fakeHost) SetState(_ context.Context, scope, scopeID, key string, value map[string]any) error {
h.mu.Lock()
h.states[scope+"|"+scopeID+"|"+key] = value
h.mu.Unlock()
select {
case h.setCh <- struct{}{}:
default:
}
return nil
}
func (h *fakeHost) DeleteState(context.Context, string, string, string) error { return nil }
func (h *fakeHost) ListState(context.Context, string, string) ([]pluginsdk.StateEntry, error) {
return nil, nil
}
func (h *fakeHost) RevealSecret(context.Context, string) (string, error) { return "", nil }
func (h *fakeHost) EmitEvent(context.Context, string, map[string]any) error { return nil }
func (h *fakeHost) get(scope, scopeID, key string) (map[string]any, bool) {
h.mu.Lock()
defer h.mu.Unlock()
v, ok := h.states[scope+"|"+scopeID+"|"+key]
return v, ok
}
// buildFixtureRecord copies the real fixture plugin binary into a fresh
// InstallPath and returns a *store.Record ready for Manager.Start,
// skipping the test if -short suppressed the fixture build.
func buildFixtureRecord(t *testing.T, id string) *store.Record {
t.Helper()
if fixtureBinPath == "" {
t.Skip("fixture plugin binary not built (test run with -short)")
}
installPath := t.TempDir()
platformKey := goruntime.GOOS + "-" + goruntime.GOARCH
relExec := filepath.Join("server", "plugin-"+platformKey)
destExec := filepath.Join(installPath, relExec)
if err := os.MkdirAll(filepath.Dir(destExec), 0o755); err != nil {
t.Fatalf("mkdir server dir: %v", err)
}
copyFile(t, fixtureBinPath, destExec, 0o755)
return &store.Record{
Manifest: manifest.Manifest{
ID: id,
APIVersion: 1,
Version: "1.0.0",
Runtime: manifest.Runtime{
Type: "binary",
Executables: map[string]string{platformKey: relExec},
},
},
Status: store.StatusRegistered,
InstallPath: installPath,
}
}
func copyFile(t *testing.T, src, dst string, mode os.FileMode) {
t.Helper()
data, err := os.ReadFile(src)
if err != nil {
t.Fatalf("read %s: %v", src, err)
}
if err := os.WriteFile(dst, data, mode); err != nil {
t.Fatalf("write %s: %v", dst, err)
}
}
func TestManager_StartDeliverEventInvokeToolStop(t *testing.T) {
rec := buildFixtureRecord(t, "kandev-fixture-plugin")
pluginsDir := t.TempDir()
host := newFakeHost()
m := NewManager(pluginsDir, nil, testLogger(t))
t.Cleanup(m.StopAll)
ctx := context.Background()
if err := m.Start(ctx, rec, func(string) pluginsdk.Host { return host }); err != nil {
t.Fatalf("Start() unexpected error: %v", err)
}
if !m.Running(rec.ID) {
t.Fatal("Running() = false right after a successful Start()")
}
remote, ok := m.Get(rec.ID)
if !ok {
t.Fatal("Get() ok = false right after a successful Start()")
}
t.Run("InvokeTool echoes input over the real subprocess", func(t *testing.T) {
resp, err := remote.InvokeTool(ctx, &pluginsdk.ToolRequest{ToolName: "echo", Input: map[string]any{"x": float64(1)}})
if err != nil {
t.Fatalf("InvokeTool() unexpected error: %v", err)
}
if resp.Output["x"] != float64(1) {
t.Fatalf("InvokeTool().Output = %v, want echoed input", resp.Output)
}
})
t.Run("DeliverEvent reaches the plugin, which calls back into Host.SetState", func(t *testing.T) {
err := remote.DeliverEvent(ctx, &pluginsdk.Event{EventID: "e1", EventType: "task.created"})
if err != nil {
t.Fatalf("DeliverEvent() unexpected error: %v", err)
}
select {
case <-host.setCh:
case <-time.After(5 * time.Second):
t.Fatal("timed out waiting for the plugin's Host.SetState callback")
}
value, found := host.get("instance", "", "last_event")
if !found {
t.Fatal("fakeHost never recorded the delivered event")
}
if value["event_type"] != "task.created" {
t.Fatalf("recorded event_type = %v, want %q", value["event_type"], "task.created")
}
})
t.Run("Ping succeeds against the live subprocess", func(t *testing.T) {
if err := m.Ping(rec.ID); err != nil {
t.Fatalf("Ping() unexpected error: %v", err)
}
})
m.Stop(rec.ID)
if m.Running(rec.ID) {
t.Fatal("Running() = true after Stop()")
}
}
func TestManager_CrashTriggersAutomaticRestart(t *testing.T) {
rec := buildFixtureRecord(t, "kandev-fixture-plugin-crash")
pluginsDir := t.TempDir()
host := newFakeHost()
var mu sync.Mutex
var transitions []bool
onStatusChange := func(_ string, healthy bool) {
mu.Lock()
transitions = append(transitions, healthy)
mu.Unlock()
}
m := NewManager(pluginsDir, onStatusChange, testLogger(t),
WithPingInterval(20*time.Millisecond),
WithMaxConsecutiveFailures(1),
WithRestartBackoff([]time.Duration{10 * time.Millisecond}),
WithMaxRestartAttempts(3),
)
t.Cleanup(m.StopAll)
ctx := context.Background()
if err := m.Start(ctx, rec, func(string) pluginsdk.Host { return host }); err != nil {
t.Fatalf("Start() unexpected error: %v", err)
}
remote, ok := m.Get(rec.ID)
if !ok {
t.Fatal("Get() ok = false right after Start()")
}
// Invoking the "crash" tool exits the subprocess immediately; the RPC
// call itself is expected to error (connection dropped mid-call) or
// hang up cleanly - either way we only assert on the recovery below.
_, _ = remote.InvokeTool(ctx, &pluginsdk.ToolRequest{ToolName: "crash"})
require.Eventually(t, func() bool {
mu.Lock()
defer mu.Unlock()
return len(transitions) >= 2 && !transitions[0] && transitions[1]
}, 10*time.Second, 20*time.Millisecond,
"onStatusChange should observe degraded (false) then recovered (true) after the crash")
}
// TestManager_ConcurrentStartOnlySpawnsOnce pins the fix for the TOCTOU
// double-spawn race: two Start calls for the same plugin id racing the
// "already running" check must not both reach spawnFn. The first goroutine
// is deliberately paused *inside* its (slow) spawn — the exact window the
// old code released the manager lock across — before the second goroutine's
// startProcess call is allowed to race in.
func TestManager_ConcurrentStartOnlySpawnsOnce(t *testing.T) {
m := NewManager(t.TempDir(), nil, testLogger(t))
t.Cleanup(m.StopAll)
var spawnCalls int32
firstEntered := make(chan struct{})
release := make(chan struct{})
var closeOnce sync.Once
spawnFn := func() (spawnedProcess, error) {
if atomic.AddInt32(&spawnCalls, 1) == 1 {
closeOnce.Do(func() { close(firstEntered) })
<-release // hold the first spawn open so the second Start can race in
}
return &fakeSpawnedProcess{}, nil
}
var wg sync.WaitGroup
errs := make([]error, 2)
wg.Add(1)
go func() {
defer wg.Done()
errs[0] = m.startProcess("race-plugin", spawnFn)
}()
<-firstEntered // the first Start is now mid-spawn, before it has registered anything
errs[1] = m.startProcess("race-plugin", spawnFn)
close(release)
wg.Wait()
if got := atomic.LoadInt32(&spawnCalls); got != 1 {
t.Fatalf("spawnFn called %d times for a concurrent Start of the same id, want exactly 1 (no double-spawn)", got)
}
if errs[0] != nil {
t.Fatalf("first startProcess() unexpected error: %v", errs[0])
}
if errs[1] == nil {
t.Fatal("second concurrent startProcess() for the same id succeeded, want an error (already starting)")
}
if !m.Running("race-plugin") {
t.Fatal("Running() = false after the winning Start() completed")
}
}
// TestManager_StopDuringStartAbortsAndLeavesNoOrphanProcess pins the fix for
// the Stop-during-Start orphan race: Stop previously only consulted
// m.processes, so a Disable/Uninstall racing an in-flight Start was a
// silent no-op — the spawn then completed and inserted a live supervised
// process for a plugin whose record/files may already be gone by the time
// the spawn finished. claimStart's "starting" reservation now doubles as
// the hook Stop uses to request an abort: startProcess checks it right
// after the (slow) spawn succeeds and, if set, kills the freshly spawned
// process and never inserts it into m.processes.
func TestManager_StopDuringStartAbortsAndLeavesNoOrphanProcess(t *testing.T) {
m := NewManager(t.TempDir(), nil, testLogger(t))
t.Cleanup(m.StopAll)
entered := make(chan struct{})
release := make(chan struct{})
var closeOnce sync.Once
spawned := &fakeSpawnedProcess{}
spawnFn := func() (spawnedProcess, error) {
closeOnce.Do(func() { close(entered) })
<-release // hold the spawn open until the test has called Stop
return spawned, nil
}
errCh := make(chan error, 1)
go func() {
errCh <- m.startProcess("racing-plugin", spawnFn)
}()
<-entered // the spawn is in flight, before startProcess has registered anything
m.Stop("racing-plugin") // races the in-flight Start: must not be a no-op
close(release) // let the spawn finish
err := <-errCh
if err == nil {
t.Fatal("startProcess() expected an error when Stop() raced an in-flight Start, got nil")
}
if m.Running("racing-plugin") {
t.Fatal("Running() = true after Stop() raced an in-flight Start; an orphan process was left registered")
}
if !spawned.isKilled() {
t.Fatal("the spawned process was never killed after Stop() raced an in-flight Start (orphan subprocess)")
}
}
// TestManager_StopAllDuringStartAbortsAndLeavesNoOrphanProcess is the same
// race as TestManager_StopDuringStartAbortsAndLeavesNoOrphanProcess, but
// through StopAll (graceful shutdown), which previously only iterated
// m.processes too.
func TestManager_StopAllDuringStartAbortsAndLeavesNoOrphanProcess(t *testing.T) {
m := NewManager(t.TempDir(), nil, testLogger(t))
t.Cleanup(m.StopAll)
entered := make(chan struct{})
release := make(chan struct{})
var closeOnce sync.Once
spawned := &fakeSpawnedProcess{}
spawnFn := func() (spawnedProcess, error) {
closeOnce.Do(func() { close(entered) })
<-release
return spawned, nil
}
errCh := make(chan error, 1)
go func() {
errCh <- m.startProcess("racing-plugin-2", spawnFn)
}()
<-entered
m.StopAll()
close(release)
err := <-errCh
if err == nil {
t.Fatal("startProcess() expected an error when StopAll() raced an in-flight Start, got nil")
}
if m.Running("racing-plugin-2") {
t.Fatal("Running() = true after StopAll() raced an in-flight Start; an orphan process was left registered")
}
if !spawned.isKilled() {
t.Fatal("the spawned process was never killed after StopAll() raced an in-flight Start (orphan subprocess)")
}
}
// TestManager_StartAfterExhaustionRespawns pins the fix that lets a fresh
// Start cleanly replace an entry whose restart attempts were exhausted
// (gaveUp==true, current==nil): before the fix, Start saw the stale map
// entry and always errored "already running" instead of respawning.
func TestManager_StartAfterExhaustionRespawns(t *testing.T) {
m := NewManager(t.TempDir(), nil, testLogger(t),
WithPingInterval(5*time.Millisecond),
WithMaxConsecutiveFailures(1),
WithRestartBackoff([]time.Duration{0}),
WithMaxRestartAttempts(1),
)
t.Cleanup(m.StopAll)
var mu sync.Mutex
firstSpawnDone := false
spawnFn := func() (spawnedProcess, error) {
mu.Lock()
defer mu.Unlock()
if !firstSpawnDone {
firstSpawnDone = true
return &fakeSpawnedProcess{exited: true}, nil // dies immediately, forcing an exhausting restart loop
}
return nil, errors.New("respawn always fails") // every restart attempt fails too
}
if err := m.startProcess("exhaust-plugin", spawnFn); err != nil {
t.Fatalf("startProcess() unexpected error: %v", err)
}
// Wait for the supervision loop to actually finish giving up (gaveUp),
// not just for current to go nil — current is cleared at the start of
// handleFailureAndRestart, before the (failing) respawn attempt(s) run,
// so polling only Running()==false races the loop's own completion.
require.Eventually(t, func() bool {
m.mu.Lock()
p, ok := m.processes["exhaust-plugin"]
m.mu.Unlock()
return ok && p.isGaveUp()
}, 2*time.Second, 5*time.Millisecond,
"plugin's process should have given up once restart attempts are exhausted")
healthy := &fakeSpawnedProcess{}
respawnCalls := 0
freshSpawnFn := func() (spawnedProcess, error) {
respawnCalls++
return healthy, nil
}
if err := m.startProcess("exhaust-plugin", freshSpawnFn); err != nil {
t.Fatalf("startProcess() after exhaustion unexpected error: %v", err)
}
if respawnCalls != 1 {
t.Fatalf("respawn spawnFn called %d times, want 1", respawnCalls)
}
if !m.Running("exhaust-plugin") {
t.Fatal("Running() = false after a fresh Start() following exhaustion")
}
}
// TestManager_DefaultStartTimeoutIs30s pins the fix for bounded startup:
// Manager.Start previously spawned via a hcplugin.Client with no configured
// StartTimeout, defaulting to go-plugin's own 1-minute handshake timeout, so
// a hung plugin binary could block Enable/Install for up to a minute. A
// fresh Manager must default to a much shorter, explicit bound.
func TestManager_DefaultStartTimeoutIs30s(t *testing.T) {
m := NewManager(t.TempDir(), nil, testLogger(t))
if m.startTimeout != 30*time.Second {
t.Fatalf("default startTimeout = %v, want 30s", m.startTimeout)
}
}
// TestManager_WithStartTimeoutOverridesDefault pins the escape hatch for
// callers (and this test file) that need a different bound.
func TestManager_WithStartTimeoutOverridesDefault(t *testing.T) {
m := NewManager(t.TempDir(), nil, testLogger(t), WithStartTimeout(5*time.Second))
if m.startTimeout != 5*time.Second {
t.Fatalf("startTimeout = %v, want 5s (WithStartTimeout override)", m.startTimeout)
}
}
// TestManager_StartFailsFastOnHandshakeTimeout proves the configured
// startTimeout actually reaches the underlying hcplugin.Client: an
// unreasonably short timeout must make Start() return an error quickly
// instead of ever completing the handshake, even against a real, working
// fixture binary.
func TestManager_StartFailsFastOnHandshakeTimeout(t *testing.T) {
rec := buildFixtureRecord(t, "kandev-fixture-plugin-timeout")
m := NewManager(t.TempDir(), nil, testLogger(t), WithStartTimeout(1*time.Nanosecond))
t.Cleanup(m.StopAll)
started := time.Now()
err := m.Start(context.Background(), rec, func(string) pluginsdk.Host { return newFakeHost() })
elapsed := time.Since(started)
if err == nil {
t.Fatal("Start() with a 1ns startTimeout unexpectedly succeeded")
}
if elapsed > 10*time.Second {
t.Fatalf("Start() took %v to fail, want well under go-plugin's 1-minute default handshake timeout", elapsed)
}
}