98 lines
2.9 KiB
Go
98 lines
2.9 KiB
Go
package multiagent
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"cyberstrike-ai/internal/config"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func TestPiChatDisclosesSkillsThroughNativeRuntime(t *testing.T) {
|
|
cfg := piTestConfig(t)
|
|
cfg.MultiAgent.Skills = config.AgentSkillsConfig{FilesystemTools: new(bool)}
|
|
cfg.SkillsDir = t.TempDir()
|
|
skillDir := filepath.Join(cfg.SkillsDir, "fixture-skill")
|
|
if err := os.MkdirAll(skillDir, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(skillDir, "SKILL.md"),
|
|
[]byte("---\nname: fixture-skill\ndescription: Read isolated fixture evidence\n---\nPRIVATE_SKILL_BODY_ONLY_AFTER_LOAD\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
var system string
|
|
var tools []string
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
var body struct {
|
|
Messages []struct {
|
|
Role string
|
|
Content json.RawMessage
|
|
}
|
|
Tools []struct {
|
|
Function struct{ Name string }
|
|
}
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
t.Error(err)
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
for _, message := range body.Messages {
|
|
if message.Role == "system" || message.Role == "developer" {
|
|
system += promptMessageText(message.Content) + "\n"
|
|
}
|
|
}
|
|
for _, tool := range body.Tools {
|
|
tools = append(tools, tool.Function.Name)
|
|
}
|
|
piReply(w, "", "", nil, "ok")
|
|
}))
|
|
defer server.Close()
|
|
cfg.OpenAI.BaseURL = server.URL + "/v1"
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
res, err := RunSingleAgent(ctx, cfg, &cfg.MultiAgent, piTestAgent(cfg, nil), nil, zap.NewNop(), "", "", "Say ok", nil, []string{"echo"}, nil, nil, "")
|
|
if err != nil || res == nil || res.Response != "ok" {
|
|
t.Fatalf("result=%+v err=%v", res, err)
|
|
}
|
|
if !strings.Contains(system, "fixture-skill") || !strings.Contains(system, "Read isolated fixture evidence") {
|
|
t.Fatalf("native skill disclosure missing: %q", system)
|
|
}
|
|
if strings.Contains(system, "PRIVATE_SKILL_BODY_ONLY_AFTER_LOAD") {
|
|
t.Fatal("skill body leaked before an explicit load")
|
|
}
|
|
if strings.Contains(system, "Available skills (load a matching skill with the skill tool") {
|
|
t.Fatal("Go still splices its own skill index into the prompt")
|
|
}
|
|
// The restricted host loader stays available when generic file reads are off.
|
|
if !slices.Contains(tools, "skill") {
|
|
t.Fatalf("restricted skill loader missing from catalog: %v", tools)
|
|
}
|
|
}
|
|
func promptMessageText(raw json.RawMessage) string {
|
|
var text string
|
|
if json.Unmarshal(raw, &text) == nil {
|
|
return text
|
|
}
|
|
var blocks []struct{ Type, Text string }
|
|
if json.Unmarshal(raw, &blocks) != nil {
|
|
return ""
|
|
}
|
|
var parts []string
|
|
for _, block := range blocks {
|
|
if block.Type == "text" {
|
|
parts = append(parts, block.Text)
|
|
}
|
|
}
|
|
return strings.Join(parts, "\n")
|
|
}
|