Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

plugin: HasGateway looks at the plugins once a second, not on every request (#3725)

yetonef2aa7fe3462c
diff --git a/internal/plugin/gateway.go b/internal/plugin/gateway.go
index 1a3b76732..f02e45ef7 100644
--- a/internal/plugin/gateway.go
+++ b/internal/plugin/gateway.go
@@ -7,7 +7,10 @@ import (
"path/filepath"
"strings"
"sync"
+ "sync/atomic"
"time"
+
+ "github.com/yetone/magpie/internal/filememo"
)
// Gateway is the asynchronous request-hook entry a package runs in Bun:
@@ -122,15 +125,37 @@ func GatewayStates(ctx context.Context) map[string]GatewayState {
}
// HasGateway avoids starting Bun for users who have no asynchronous hooks.
+// Every request asks it, so the plugins are looked at once a second, and at
+// once after magpie wrote a file of its own (filememo.Gen): reading
+// plugins.json and each plugin's package.json took ~1ms a request with 12
+// plugins on Windows (#3725).
func HasGateway() bool {
+ now, g, list := time.Now().UnixNano(), filememo.Gen(), listPath()
+ if c := hasGateway.Load(); c != nil && c.gen == g && c.list == list && now < c.until {
+ return c.on
+ }
+ on := false
for _, e := range Load().Plugins {
if !e.Off && Gateway(Target(e.Spec)) != "" {
- return true
+ on = true
+ break
}
}
- return false
+ hasGateway.Store(&gatewaySeen{on: on, gen: g, list: list, until: now + int64(time.Second)})
+ return on
}
+// gatewaySeen is HasGateway's last look: for the plugins.json at list,
+// while filememo's generation is gen, until until (UnixNano).
+type gatewaySeen struct {
+ on bool
+ gen uint64
+ list string
+ until int64
+}
+
+var hasGateway atomic.Pointer[gatewaySeen]
+
// GatewayRun belongs to the host that began it, even if the plugin list changes
// before the response. Close releases its private state and cancels active I/O.
type GatewayRun struct {
diff --git a/internal/plugin/gateway_test.go b/internal/plugin/gateway_test.go
index 97b896194..a324a46b7 100644
--- a/internal/plugin/gateway_test.go
+++ b/internal/plugin/gateway_test.go
@@ -11,6 +11,8 @@ import (
"strings"
"testing"
"time"
+
+ "github.com/yetone/magpie/internal/filememo"
)
func gatewayFixture(t *testing.T, source string) string {
@@ -28,6 +30,38 @@ func gatewayFixture(t *testing.T, source string) string {
return dir
}
+// TestHasGatewayLooksOnceASecond: every request asks HasGateway, so it
+// reads the plugins at most once a second (#3725), and at once after magpie
+// wrote a file of its own.
+func TestHasGatewayLooksOnceASecond(t *testing.T) {
+ sandbox(t)
+ dir := gatewayFixture(t, `export function onRequest(body){return body}`)
+ if !HasGateway() {
+ t.Fatal("a plugin with hooks has no gateway")
+ }
+ pkg := filepath.Join(dir, "package.json")
+ if err := os.WriteFile(pkg, []byte(`{"name":"x"}`), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if !HasGateway() {
+ t.Fatal("the plugins were read again within the second")
+ }
+ filememo.Forget()
+ if HasGateway() {
+ t.Fatal("magpie's own write wasn't looked at")
+ }
+ if err := os.WriteFile(pkg, []byte(`{"magpie":{"hooks":"./request.js"}}`), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ // a second later: the look is due
+ c := *hasGateway.Load()
+ c.until = time.Now().UnixNano()
+ hasGateway.Store(&c)
+ if !HasGateway() {
+ t.Fatal("another program's change wasn't seen a second later")
+ }
+}
+
func TestGatewayHookWaitsForStartingHost(t *testing.T) {
sandbox(t)
gatewayFixture(t, `export function onRequest(body){return {...body,note:"recalled"}}`)