Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

agent: hook registry methods refuse a zero-value Agent instead of dereferencing nil

Adeshina Lasisif567ea6ef0f9
diff --git a/laya-go/agent/hooks.go b/laya-go/agent/hooks.go
index 31d29dc5..8e4e580b 100644
--- a/laya-go/agent/hooks.go
+++ b/laya-go/agent/hooks.go
@@ -29,6 +29,7 @@ import (
"context"
"crypto/rand"
"encoding/hex"
+ "errors"
"fmt"
"log"
"math"
@@ -305,6 +306,9 @@ func (a *Agent) AddHook(hooks ...Hook) error {
if err := checkHooks(hooks); err != nil {
return err
}
+ if a.hk == nil {
+ return errors.New("agent: hooks need an Agent from Open")
+ }
a.hk.mu.Lock()
a.hk.installed = append(a.hk.installed, hooks...)
a.hk.mu.Unlock()
@@ -315,6 +319,9 @@ func (a *Agent) AddHook(hooks ...Hook) error {
// there was one. A hook whose dynamic type cannot be compared (a struct holding a func, used as
// a value) is never found; pass a pointer.
func (a *Agent) RemoveHook(hook Hook) bool {
+ if a.hk == nil {
+ return false
+ }
a.hk.mu.Lock()
defer a.hk.mu.Unlock()
kept := a.hk.installed[:0:0]
@@ -354,6 +361,9 @@ func (a *Agent) InstallHooks(hooks ...Hook) (undo func(), err error) {
// Hooks returns a copy of the installed hooks.
func (a *Agent) Hooks() []Hook {
+ if a.hk == nil {
+ return nil
+ }
a.hk.mu.RLock()
defer a.hk.mu.RUnlock()
return append([]Hook(nil), a.hk.installed...)
diff --git a/laya-go/agent/hooks_test.go b/laya-go/agent/hooks_test.go
index 4d83c6b6..ab49e6f6 100644
--- a/laya-go/agent/hooks_test.go
+++ b/laya-go/agent/hooks_test.go
@@ -192,3 +192,16 @@ func TestOpenOptionError(t *testing.T) {
t.Fatal("a negative timeout was accepted")
}
}
+
+func TestZeroAgentHookMethodsDoNotPanic(t *testing.T) {
+ var a Agent
+ if err := a.AddHook(&StubHook{name: "h", rec: &recorder{}}); err == nil {
+ t.Error("AddHook on a zero Agent was accepted")
+ }
+ if _, err := a.InstallHooks(&StubHook{name: "h", rec: &recorder{}}); err == nil {
+ t.Error("InstallHooks on a zero Agent was accepted")
+ }
+ if a.RemoveHook(&StubHook{}) || a.Hooks() != nil {
+ t.Error("zero Agent reports hooks")
+ }
+}