Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Verify kernel plugin source polling https://github.com/siyuan-note/siyuan/issues/18387

Daniel56ec3c335b58
diff --git a/kernel/plugin/source_watcher.go b/kernel/plugin/source_watcher.go
index 502517b1d2..26e510e89f 100644
--- a/kernel/plugin/source_watcher.go
+++ b/kernel/plugin/source_watcher.go
@@ -31,8 +31,9 @@ import (
)
const (
- pluginSourceReloadDelay = 300 * time.Millisecond
- pluginSourcePollInterval = time.Second
+ pluginSourceReloadDelay = 300 * time.Millisecond
+ pluginSourcePollInterval = time.Second
+ pluginSourceFullScanInterval = 30 * time.Second
)
var errPluginFileWatchUnsupported = errors.New("plugin file watcher is not supported on mobile")
@@ -50,6 +51,8 @@ type pluginSourceWatchEntry struct {
signature [sha256.Size]byte
fileState pluginSourceFileState
lastError string
+ verified bool
+ nextScan time.Time
generation uint64
timer *time.Timer
}
@@ -65,14 +68,18 @@ type pluginSourceWatchState struct {
entries map[string]*pluginSourceWatchEntry
generation uint64
delay time.Duration
+ fullScan time.Duration
+ now func() time.Time
reload func(name string)
}
func newPluginSourceWatchState(reload func(name string)) pluginSourceWatchState {
return pluginSourceWatchState{
- entries: map[string]*pluginSourceWatchEntry{},
- delay: pluginSourceReloadDelay,
- reload: reload,
+ entries: map[string]*pluginSourceWatchEntry{},
+ delay: pluginSourceReloadDelay,
+ fullScan: pluginSourceFullScanInterval,
+ now: time.Now,
+ reload: reload,
}
}
@@ -104,12 +111,14 @@ func (s *pluginSourceWatchState) register(name, path, source string) {
old.timer.Stop()
}
s.generation++
- s.entries[name] = &pluginSourceWatchEntry{
+ entry := &pluginSourceWatchEntry{
path: path,
signature: sha256.Sum256([]byte(source)),
fileState: fileState,
generation: s.generation,
}
+ s.entries[name] = entry
+ s.scheduleLocked(name, entry, &fileState)
}
func (s *pluginSourceWatchState) unregister(name string) {
@@ -130,17 +139,20 @@ func (s *pluginSourceWatchState) scan() {
path: entry.path,
generation: entry.generation,
fileState: entry.fileState,
+ verified: entry.verified,
+ nextScan: entry.nextScan,
}
}
s.mu.Unlock()
+ now := s.now()
for name, snapshot := range entries {
fileState, err := readPluginSourceFileState(snapshot.path)
if err != nil {
s.logReadError(name, snapshot.generation, snapshot.path, err)
continue
}
- if fileState == snapshot.fileState {
+ if snapshot.verified && fileState == snapshot.fileState && now.Before(snapshot.nextScan) {
s.clearReadError(name, snapshot.generation)
continue
}
@@ -242,6 +254,8 @@ func (s *pluginSourceWatchState) finishPluginSourceCheck(
entry.fileState = fileState
entry.lastError = ""
entry.signature = signature
+ entry.verified = true
+ entry.nextScan = s.now().Add(s.fullScan)
entry.timer = nil
reload := s.reload
s.mu.Unlock()
@@ -304,6 +318,8 @@ type pluginSourceWatchSnapshot struct {
path string
generation uint64
fileState pluginSourceFileState
+ verified bool
+ nextScan time.Time
}
func readPluginSourceFileState(path string) (pluginSourceFileState, error) {
diff --git a/kernel/plugin/watcher_test.go b/kernel/plugin/watcher_test.go
index c429605908..928cca3d83 100644
--- a/kernel/plugin/watcher_test.go
+++ b/kernel/plugin/watcher_test.go
@@ -136,6 +136,7 @@ func TestPluginSourceWatchStateScansChangedContent(t *testing.T) {
state.unregister(name)
})
+ waitForPluginSourceVerification(t, &state, name)
state.scan()
state.mu.Lock()
timer := state.entries[name].timer
@@ -177,6 +178,67 @@ func TestPluginSourceWatchStateScansChangedContent(t *testing.T) {
waitForPluginReload(t, reloaded, name)
}
+func TestPluginSourceWatchStateVerifiesRegistrationContent(t *testing.T) {
+ pluginsDir := t.TempDir()
+ name := "test-plugin"
+ sourcePath := filepath.Join(pluginsDir, "kernel.js")
+ if err := os.WriteFile(sourcePath, []byte("changed-after-load"), 0644); err != nil {
+ t.Fatal(err)
+ }
+
+ reloaded := make(chan string, 1)
+ state := newPluginSourceWatchState(func(pluginName string) {
+ reloaded <- pluginName
+ })
+ state.delay = 20 * time.Millisecond
+ state.register(name, sourcePath, "loaded-source")
+ t.Cleanup(func() {
+ state.unregister(name)
+ })
+
+ waitForPluginReload(t, reloaded, name)
+}
+
+func TestPluginSourceWatchStatePeriodicallyChecksContent(t *testing.T) {
+ pluginsDir := t.TempDir()
+ name := "test-plugin"
+ sourcePath := filepath.Join(pluginsDir, "kernel.js")
+ if err := os.WriteFile(sourcePath, []byte("initial"), 0644); err != nil {
+ t.Fatal(err)
+ }
+
+ now := time.Now()
+ reloaded := make(chan string, 1)
+ state := newPluginSourceWatchState(func(pluginName string) {
+ reloaded <- pluginName
+ })
+ state.delay = 20 * time.Millisecond
+ state.now = func() time.Time {
+ return now
+ }
+ state.register(name, sourcePath, "initial")
+ t.Cleanup(func() {
+ state.unregister(name)
+ })
+ waitForPluginSourceVerification(t, &state, name)
+
+ if err := os.WriteFile(sourcePath, []byte("changed"), 0644); err != nil {
+ t.Fatal(err)
+ }
+ fileState, err := readPluginSourceFileState(sourcePath)
+ if err != nil {
+ t.Fatal(err)
+ }
+ state.mu.Lock()
+ entry := state.entries[name]
+ entry.fileState = fileState
+ entry.nextScan = now
+ state.mu.Unlock()
+
+ state.scan()
+ waitForPluginReload(t, reloaded, name)
+}
+
func TestPluginSourceWatchStateIgnoresStaleRegistration(t *testing.T) {
pluginsDir := t.TempDir()
name := "test-plugin"
@@ -343,3 +405,20 @@ func expectNoPluginReload(t *testing.T, reloaded <-chan string) {
case <-time.After(80 * time.Millisecond):
}
}
+
+func waitForPluginSourceVerification(t *testing.T, state *pluginSourceWatchState, name string) {
+ t.Helper()
+
+ deadline := time.Now().Add(time.Second)
+ for time.Now().Before(deadline) {
+ state.mu.Lock()
+ entry := state.entries[name]
+ verified := entry != nil && entry.verified && entry.timer == nil
+ state.mu.Unlock()
+ if verified {
+ return
+ }
+ time.Sleep(5 * time.Millisecond)
+ }
+ t.Fatal("timed out waiting for plugin source verification")
+}